aarch64: Add +fcma alias for +compnum
[binutils-gdb.git] / bfd / elf64-ppc.c
blob3d1fdd3a7c9f7c180ee13dbe62a9ee6b429357c8
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2024 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_always_size_sections ppc64_elf_edit
123 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_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),
1847 PPC64_ELF_DATA);
1850 /* Fix bad default arch selected for a 64 bit input bfd when the
1851 default is 32 bit. Also select arch based on apuinfo. */
1853 static bool
1854 ppc64_elf_object_p (bfd *abfd)
1856 if (!abfd->arch_info->the_default)
1857 return true;
1859 if (abfd->arch_info->bits_per_word == 32)
1861 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1863 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1865 /* Relies on arch after 32 bit default being 64 bit default. */
1866 abfd->arch_info = abfd->arch_info->next;
1867 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1870 return _bfd_elf_ppc_set_arch (abfd);
1873 /* Support for core dump NOTE sections. */
1875 static bool
1876 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1878 size_t offset, size;
1880 if (note->descsz != 504)
1881 return false;
1883 /* pr_cursig */
1884 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1886 /* pr_pid */
1887 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1889 /* pr_reg */
1890 offset = 112;
1891 size = 384;
1893 /* Make a ".reg/999" section. */
1894 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1895 size, note->descpos + offset);
1898 static bool
1899 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1901 if (note->descsz != 136)
1902 return false;
1904 elf_tdata (abfd)->core->pid
1905 = bfd_get_32 (abfd, note->descdata + 24);
1906 elf_tdata (abfd)->core->program
1907 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1908 elf_tdata (abfd)->core->command
1909 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1911 return true;
1914 static char *
1915 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1916 ...)
1918 switch (note_type)
1920 default:
1921 return NULL;
1923 case NT_PRPSINFO:
1925 char data[136] ATTRIBUTE_NONSTRING;
1926 va_list ap;
1928 va_start (ap, note_type);
1929 memset (data, 0, sizeof (data));
1930 strncpy (data + 40, va_arg (ap, const char *), 16);
1931 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1932 DIAGNOSTIC_PUSH;
1933 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1934 -Wstringop-truncation:
1935 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1937 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1938 #endif
1939 strncpy (data + 56, va_arg (ap, const char *), 80);
1940 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1941 DIAGNOSTIC_POP;
1942 #endif
1943 va_end (ap);
1944 return elfcore_write_note (abfd, buf, bufsiz,
1945 "CORE", note_type, data, sizeof (data));
1948 case NT_PRSTATUS:
1950 char data[504];
1951 va_list ap;
1952 long pid;
1953 int cursig;
1954 const void *greg;
1956 va_start (ap, note_type);
1957 memset (data, 0, 112);
1958 pid = va_arg (ap, long);
1959 bfd_put_32 (abfd, pid, data + 32);
1960 cursig = va_arg (ap, int);
1961 bfd_put_16 (abfd, cursig, data + 12);
1962 greg = va_arg (ap, const void *);
1963 memcpy (data + 112, greg, 384);
1964 memset (data + 496, 0, 8);
1965 va_end (ap);
1966 return elfcore_write_note (abfd, buf, bufsiz,
1967 "CORE", note_type, data, sizeof (data));
1972 /* Add extra PPC sections. */
1974 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1976 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1977 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1978 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1979 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1980 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1981 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1982 { NULL, 0, 0, 0, 0 }
1985 enum _ppc64_sec_type {
1986 sec_normal = 0,
1987 sec_opd = 1,
1988 sec_toc = 2,
1989 sec_stub = 3
1992 struct _ppc64_elf_section_data
1994 struct bfd_elf_section_data elf;
1996 union
1998 /* An array with one entry for each opd function descriptor,
1999 and some spares since opd entries may be either 16 or 24 bytes. */
2000 #define OPD_NDX(OFF) ((OFF) >> 4)
2001 struct _opd_sec_data
2003 /* Points to the function code section for local opd entries. */
2004 asection **func_sec;
2006 /* After editing .opd, adjust references to opd local syms. */
2007 long *adjust;
2009 union
2011 /* A copy of relocs before they are modified for --emit-relocs. */
2012 Elf_Internal_Rela *relocs;
2014 /* Section contents. */
2015 bfd_byte *contents;
2016 } u;
2017 } opd;
2019 /* An array for toc sections, indexed by offset/8. */
2020 struct _toc_sec_data
2022 /* Specifies the relocation symbol index used at a given toc offset. */
2023 unsigned *symndx;
2025 /* And the relocation addend. */
2026 bfd_vma *add;
2027 } toc;
2029 /* Stub debugging. */
2030 struct ppc_stub_hash_entry *last_ent;
2031 } u;
2033 enum _ppc64_sec_type sec_type:2;
2035 /* Flag set when small branches are detected. Used to
2036 select suitable defaults for the stub group size. */
2037 unsigned int has_14bit_branch:1;
2039 /* Flag set when PLTCALL relocs are detected. */
2040 unsigned int has_pltcall:1;
2042 /* Flag set when section has PLT/GOT/TOC relocations that can be
2043 optimised. */
2044 unsigned int has_optrel:1;
2047 #define ppc64_elf_section_data(sec) \
2048 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2050 static bool
2051 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2053 if (!sec->used_by_bfd)
2055 struct _ppc64_elf_section_data *sdata;
2056 size_t amt = sizeof (*sdata);
2058 sdata = bfd_zalloc (abfd, amt);
2059 if (sdata == NULL)
2060 return false;
2061 sec->used_by_bfd = sdata;
2064 return _bfd_elf_new_section_hook (abfd, sec);
2067 static bool
2068 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2070 const char *name = hdr->bfd_section->name;
2072 if (startswith (name, ".sbss")
2073 || startswith (name, ".sdata"))
2074 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2076 return true;
2079 static struct _opd_sec_data *
2080 get_opd_info (asection * sec)
2082 if (sec != NULL
2083 && ppc64_elf_section_data (sec) != NULL
2084 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2085 return &ppc64_elf_section_data (sec)->u.opd;
2086 return NULL;
2089 /* Parameters for the qsort hook. */
2090 static bool synthetic_relocatable;
2091 static const asection *synthetic_opd;
2093 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2095 static int
2096 compare_symbols (const void *ap, const void *bp)
2098 const asymbol *a = *(const asymbol **) ap;
2099 const asymbol *b = *(const asymbol **) bp;
2101 /* Section symbols first. */
2102 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2103 return -1;
2104 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2105 return 1;
2107 /* then .opd symbols. */
2108 if (synthetic_opd != NULL)
2110 if (strcmp (a->section->name, ".opd") == 0
2111 && strcmp (b->section->name, ".opd") != 0)
2112 return -1;
2113 if (strcmp (a->section->name, ".opd") != 0
2114 && strcmp (b->section->name, ".opd") == 0)
2115 return 1;
2118 /* then other code symbols. */
2119 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2120 == (SEC_CODE | SEC_ALLOC))
2121 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2122 != (SEC_CODE | SEC_ALLOC)))
2123 return -1;
2125 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2126 != (SEC_CODE | SEC_ALLOC))
2127 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2128 == (SEC_CODE | SEC_ALLOC)))
2129 return 1;
2131 if (synthetic_relocatable)
2133 if (a->section->id < b->section->id)
2134 return -1;
2136 if (a->section->id > b->section->id)
2137 return 1;
2140 if (a->value + a->section->vma < b->value + b->section->vma)
2141 return -1;
2143 if (a->value + a->section->vma > b->value + b->section->vma)
2144 return 1;
2146 /* For syms with the same value, prefer strong dynamic global function
2147 syms over other syms. */
2148 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2149 return -1;
2151 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2152 return 1;
2154 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2155 return -1;
2157 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2158 return 1;
2160 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2161 return -1;
2163 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2164 return 1;
2166 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2167 return -1;
2169 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2170 return 1;
2172 /* Finally, sort on where the symbol is in memory. The symbols will
2173 be in at most two malloc'd blocks, one for static syms, one for
2174 dynamic syms, and we distinguish the two blocks above by testing
2175 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2176 originally in the same order as the symbols (and we're not
2177 sorting the symbols themselves), this ensures a stable sort. */
2178 if (a < b)
2179 return -1;
2180 if (a > b)
2181 return 1;
2182 return 0;
2185 /* Search SYMS for a symbol of the given VALUE. */
2187 static asymbol *
2188 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2189 bfd_vma value)
2191 size_t mid;
2193 if (id == (unsigned) -1)
2195 while (lo < hi)
2197 mid = (lo + hi) >> 1;
2198 if (syms[mid]->value + syms[mid]->section->vma < value)
2199 lo = mid + 1;
2200 else if (syms[mid]->value + syms[mid]->section->vma > value)
2201 hi = mid;
2202 else
2203 return syms[mid];
2206 else
2208 while (lo < hi)
2210 mid = (lo + hi) >> 1;
2211 if (syms[mid]->section->id < id)
2212 lo = mid + 1;
2213 else if (syms[mid]->section->id > id)
2214 hi = mid;
2215 else if (syms[mid]->value < value)
2216 lo = mid + 1;
2217 else if (syms[mid]->value > value)
2218 hi = mid;
2219 else
2220 return syms[mid];
2223 return NULL;
2226 static bool
2227 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2229 bfd_vma vma = *(bfd_vma *) ptr;
2230 return ((section->flags & SEC_ALLOC) != 0
2231 && section->vma <= vma
2232 && vma < section->vma + section->size);
2235 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2236 entry syms. Also generate @plt symbols for the glink branch table.
2237 Returns count of synthetic symbols in RET or -1 on error. */
2239 static long
2240 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2241 long static_count, asymbol **static_syms,
2242 long dyn_count, asymbol **dyn_syms,
2243 asymbol **ret)
2245 asymbol *s;
2246 size_t i, j, count;
2247 char *names;
2248 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2249 asection *opd = NULL;
2250 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2251 asymbol **syms;
2252 int abi = abiversion (abfd);
2254 *ret = NULL;
2256 if (abi < 2)
2258 opd = bfd_get_section_by_name (abfd, ".opd");
2259 if (opd == NULL && abi == 1)
2260 return 0;
2263 syms = NULL;
2264 codesecsym = 0;
2265 codesecsymend = 0;
2266 secsymend = 0;
2267 opdsymend = 0;
2268 symcount = 0;
2269 if (opd != NULL)
2271 symcount = static_count;
2272 if (!relocatable)
2273 symcount += dyn_count;
2274 if (symcount == 0)
2275 return 0;
2277 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2278 if (syms == NULL)
2279 return -1;
2281 if (!relocatable && static_count != 0 && dyn_count != 0)
2283 /* Use both symbol tables. */
2284 memcpy (syms, static_syms, static_count * sizeof (*syms));
2285 memcpy (syms + static_count, dyn_syms,
2286 (dyn_count + 1) * sizeof (*syms));
2288 else if (!relocatable && static_count == 0)
2289 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2290 else
2291 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2293 /* Trim uninteresting symbols. Interesting symbols are section,
2294 function, and notype symbols. */
2295 for (i = 0, j = 0; i < symcount; ++i)
2296 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2297 | BSF_RELC | BSF_SRELC)) == 0)
2298 syms[j++] = syms[i];
2299 symcount = j;
2301 synthetic_relocatable = relocatable;
2302 synthetic_opd = opd;
2303 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2305 if (!relocatable && symcount > 1)
2307 /* Trim duplicate syms, since we may have merged the normal
2308 and dynamic symbols. Actually, we only care about syms
2309 that have different values, so trim any with the same
2310 value. Don't consider ifunc and ifunc resolver symbols
2311 duplicates however, because GDB wants to know whether a
2312 text symbol is an ifunc resolver. */
2313 for (i = 1, j = 1; i < symcount; ++i)
2315 const asymbol *s0 = syms[i - 1];
2316 const asymbol *s1 = syms[i];
2318 if ((s0->value + s0->section->vma
2319 != s1->value + s1->section->vma)
2320 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2321 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2322 syms[j++] = syms[i];
2324 symcount = j;
2327 i = 0;
2328 /* Note that here and in compare_symbols we can't compare opd and
2329 sym->section directly. With separate debug info files, the
2330 symbols will be extracted from the debug file while abfd passed
2331 to this function is the real binary. */
2332 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2333 && strcmp (syms[i]->section->name, ".opd") == 0)
2334 ++i;
2335 codesecsym = i;
2337 for (; i < symcount; ++i)
2338 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2339 | SEC_THREAD_LOCAL))
2340 != (SEC_CODE | SEC_ALLOC))
2341 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2342 break;
2343 codesecsymend = i;
2345 for (; i < symcount; ++i)
2346 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2347 break;
2348 secsymend = i;
2350 for (; i < symcount; ++i)
2351 if (strcmp (syms[i]->section->name, ".opd") != 0)
2352 break;
2353 opdsymend = i;
2355 for (; i < symcount; ++i)
2356 if (((syms[i]->section->flags
2357 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2358 != (SEC_CODE | SEC_ALLOC))
2359 break;
2360 symcount = i;
2362 count = 0;
2364 if (relocatable)
2366 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2367 arelent *r;
2368 size_t size;
2369 size_t relcount;
2371 if (opdsymend == secsymend)
2372 goto done;
2374 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2375 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2376 if (relcount == 0)
2377 goto done;
2379 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2381 count = -1;
2382 goto done;
2385 size = 0;
2386 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2388 asymbol *sym;
2390 while (r < opd->relocation + relcount
2391 && r->address < syms[i]->value + opd->vma)
2392 ++r;
2394 if (r == opd->relocation + relcount)
2395 break;
2397 if (r->address != syms[i]->value + opd->vma)
2398 continue;
2400 if (r->howto->type != R_PPC64_ADDR64)
2401 continue;
2403 sym = *r->sym_ptr_ptr;
2404 if (!sym_exists_at (syms, opdsymend, symcount,
2405 sym->section->id, sym->value + r->addend))
2407 ++count;
2408 size += sizeof (asymbol);
2409 size += strlen (syms[i]->name) + 2;
2413 if (size == 0)
2414 goto done;
2415 s = *ret = bfd_malloc (size);
2416 if (s == NULL)
2418 count = -1;
2419 goto done;
2422 names = (char *) (s + count);
2424 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2426 asymbol *sym;
2428 while (r < opd->relocation + relcount
2429 && r->address < syms[i]->value + opd->vma)
2430 ++r;
2432 if (r == opd->relocation + relcount)
2433 break;
2435 if (r->address != syms[i]->value + opd->vma)
2436 continue;
2438 if (r->howto->type != R_PPC64_ADDR64)
2439 continue;
2441 sym = *r->sym_ptr_ptr;
2442 if (!sym_exists_at (syms, opdsymend, symcount,
2443 sym->section->id, sym->value + r->addend))
2445 size_t len;
2447 *s = *syms[i];
2448 s->flags |= BSF_SYNTHETIC;
2449 s->section = sym->section;
2450 s->value = sym->value + r->addend;
2451 s->name = names;
2452 *names++ = '.';
2453 len = strlen (syms[i]->name);
2454 memcpy (names, syms[i]->name, len + 1);
2455 names += len + 1;
2456 /* Have udata.p point back to the original symbol this
2457 synthetic symbol was derived from. */
2458 s->udata.p = syms[i];
2459 s++;
2463 else
2465 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2466 bfd_byte *contents = NULL;
2467 size_t size;
2468 size_t plt_count = 0;
2469 bfd_vma glink_vma = 0, resolv_vma = 0;
2470 asection *dynamic, *glink = NULL, *relplt = NULL;
2471 arelent *p;
2473 if (opd != NULL
2474 && ((opd->flags & SEC_HAS_CONTENTS) == 0
2475 || !bfd_malloc_and_get_section (abfd, opd, &contents)))
2477 free_contents_and_exit_err:
2478 count = -1;
2479 free_contents_and_exit:
2480 free (contents);
2481 goto done;
2484 size = 0;
2485 for (i = secsymend; i < opdsymend; ++i)
2487 bfd_vma ent;
2489 /* Ignore bogus symbols. */
2490 if (syms[i]->value > opd->size - 8)
2491 continue;
2493 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2494 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2496 ++count;
2497 size += sizeof (asymbol);
2498 size += strlen (syms[i]->name) + 2;
2502 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2503 if (dyn_count != 0
2504 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2506 bfd_byte *dynbuf, *extdyn, *extdynend;
2507 size_t extdynsize;
2508 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2510 if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2511 || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2512 goto free_contents_and_exit_err;
2514 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2515 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2517 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2518 (size_t) (extdynend - extdyn) >= extdynsize;
2519 extdyn += extdynsize)
2521 Elf_Internal_Dyn dyn;
2522 (*swap_dyn_in) (abfd, extdyn, &dyn);
2524 if (dyn.d_tag == DT_NULL)
2525 break;
2527 if (dyn.d_tag == DT_PPC64_GLINK)
2529 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2530 See comment in ppc64_elf_finish_dynamic_sections. */
2531 glink_vma = dyn.d_un.d_val + 8 * 4;
2532 /* The .glink section usually does not survive the final
2533 link; search for the section (usually .text) where the
2534 glink stubs now reside. */
2535 glink = bfd_sections_find_if (abfd, section_covers_vma,
2536 &glink_vma);
2537 break;
2541 free (dynbuf);
2544 if (glink != NULL)
2546 /* Determine __glink trampoline by reading the relative branch
2547 from the first glink stub. */
2548 bfd_byte buf[4];
2549 unsigned int off = 0;
2551 while (bfd_get_section_contents (abfd, glink, buf,
2552 glink_vma + off - glink->vma, 4))
2554 unsigned int insn = bfd_get_32 (abfd, buf);
2555 insn ^= B_DOT;
2556 if ((insn & ~0x3fffffc) == 0)
2558 resolv_vma
2559 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2560 break;
2562 off += 4;
2563 if (off > 4)
2564 break;
2567 if (resolv_vma)
2568 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2570 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2571 if (relplt != NULL)
2573 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2574 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2575 goto free_contents_and_exit_err;
2577 plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
2578 size += plt_count * sizeof (asymbol);
2580 p = relplt->relocation;
2581 for (i = 0; i < plt_count; i++, p++)
2583 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2584 if (p->addend != 0)
2585 size += sizeof ("+0x") - 1 + 16;
2590 if (size == 0)
2591 goto free_contents_and_exit;
2592 s = *ret = bfd_malloc (size);
2593 if (s == NULL)
2594 goto free_contents_and_exit_err;
2596 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2598 for (i = secsymend; i < opdsymend; ++i)
2600 bfd_vma ent;
2602 if (syms[i]->value > opd->size - 8)
2603 continue;
2605 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2606 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2608 size_t lo, hi;
2609 size_t len;
2610 asection *sec = abfd->sections;
2612 *s = *syms[i];
2613 lo = codesecsym;
2614 hi = codesecsymend;
2615 while (lo < hi)
2617 size_t mid = (lo + hi) >> 1;
2618 if (syms[mid]->section->vma < ent)
2619 lo = mid + 1;
2620 else if (syms[mid]->section->vma > ent)
2621 hi = mid;
2622 else
2624 sec = syms[mid]->section;
2625 break;
2629 if (lo >= hi && lo > codesecsym)
2630 sec = syms[lo - 1]->section;
2632 for (; sec != NULL; sec = sec->next)
2634 if (sec->vma > ent)
2635 break;
2636 /* SEC_LOAD may not be set if SEC is from a separate debug
2637 info file. */
2638 if ((sec->flags & SEC_ALLOC) == 0)
2639 break;
2640 if ((sec->flags & SEC_CODE) != 0)
2641 s->section = sec;
2643 s->flags |= BSF_SYNTHETIC;
2644 s->value = ent - s->section->vma;
2645 s->name = names;
2646 *names++ = '.';
2647 len = strlen (syms[i]->name);
2648 memcpy (names, syms[i]->name, len + 1);
2649 names += len + 1;
2650 /* Have udata.p point back to the original symbol this
2651 synthetic symbol was derived from. */
2652 s->udata.p = syms[i];
2653 s++;
2656 free (contents);
2658 if (glink != NULL && relplt != NULL)
2660 if (resolv_vma)
2662 /* Add a symbol for the main glink trampoline. */
2663 memset (s, 0, sizeof *s);
2664 s->the_bfd = abfd;
2665 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2666 s->section = glink;
2667 s->value = resolv_vma - glink->vma;
2668 s->name = names;
2669 memcpy (names, "__glink_PLTresolve",
2670 sizeof ("__glink_PLTresolve"));
2671 names += sizeof ("__glink_PLTresolve");
2672 s++;
2673 count++;
2676 /* FIXME: It would be very much nicer to put sym@plt on the
2677 stub rather than on the glink branch table entry. The
2678 objdump disassembler would then use a sensible symbol
2679 name on plt calls. The difficulty in doing so is
2680 a) finding the stubs, and,
2681 b) matching stubs against plt entries, and,
2682 c) there can be multiple stubs for a given plt entry.
2684 Solving (a) could be done by code scanning, but older
2685 ppc64 binaries used different stubs to current code.
2686 (b) is the tricky one since you need to known the toc
2687 pointer for at least one function that uses a pic stub to
2688 be able to calculate the plt address referenced.
2689 (c) means gdb would need to set multiple breakpoints (or
2690 find the glink branch itself) when setting breakpoints
2691 for pending shared library loads. */
2692 p = relplt->relocation;
2693 for (i = 0; i < plt_count; i++, p++)
2695 size_t len;
2697 *s = **p->sym_ptr_ptr;
2698 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2699 we are defining a symbol, ensure one of them is set. */
2700 if ((s->flags & BSF_LOCAL) == 0)
2701 s->flags |= BSF_GLOBAL;
2702 s->flags |= BSF_SYNTHETIC;
2703 s->section = glink;
2704 s->value = glink_vma - glink->vma;
2705 s->name = names;
2706 s->udata.p = NULL;
2707 len = strlen ((*p->sym_ptr_ptr)->name);
2708 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2709 names += len;
2710 if (p->addend != 0)
2712 memcpy (names, "+0x", sizeof ("+0x") - 1);
2713 names += sizeof ("+0x") - 1;
2714 bfd_sprintf_vma (abfd, names, p->addend);
2715 names += strlen (names);
2717 memcpy (names, "@plt", sizeof ("@plt"));
2718 names += sizeof ("@plt");
2719 s++;
2720 if (abi < 2)
2722 glink_vma += 8;
2723 if (i >= 0x8000)
2724 glink_vma += 4;
2726 else
2727 glink_vma += 4;
2729 count += plt_count;
2733 done:
2734 free (syms);
2735 return count;
2738 /* The following functions are specific to the ELF linker, while
2739 functions above are used generally. Those named ppc64_elf_* are
2740 called by the main ELF linker code. They appear in this file more
2741 or less in the order in which they are called. eg.
2742 ppc64_elf_check_relocs is called early in the link process,
2743 ppc64_elf_finish_dynamic_sections is one of the last functions
2744 called.
2746 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2747 functions have both a function code symbol and a function descriptor
2748 symbol. A call to foo in a relocatable object file looks like:
2750 . .text
2751 . x:
2752 . bl .foo
2753 . nop
2755 The function definition in another object file might be:
2757 . .section .opd
2758 . foo: .quad .foo
2759 . .quad .TOC.@tocbase
2760 . .quad 0
2762 . .text
2763 . .foo: blr
2765 When the linker resolves the call during a static link, the branch
2766 unsurprisingly just goes to .foo and the .opd information is unused.
2767 If the function definition is in a shared library, things are a little
2768 different: The call goes via a plt call stub, the opd information gets
2769 copied to the plt, and the linker patches the nop.
2771 . x:
2772 . bl .foo_stub
2773 . ld 2,40(1)
2776 . .foo_stub:
2777 . std 2,40(1) # in practice, the call stub
2778 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2779 . addi 11,11,Lfoo@toc@l # this is the general idea
2780 . ld 12,0(11)
2781 . ld 2,8(11)
2782 . mtctr 12
2783 . ld 11,16(11)
2784 . bctr
2786 . .section .plt
2787 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2789 The "reloc ()" notation is supposed to indicate that the linker emits
2790 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2791 copying.
2793 What are the difficulties here? Well, firstly, the relocations
2794 examined by the linker in check_relocs are against the function code
2795 sym .foo, while the dynamic relocation in the plt is emitted against
2796 the function descriptor symbol, foo. Somewhere along the line, we need
2797 to carefully copy dynamic link information from one symbol to the other.
2798 Secondly, the generic part of the elf linker will make .foo a dynamic
2799 symbol as is normal for most other backends. We need foo dynamic
2800 instead, at least for an application final link. However, when
2801 creating a shared library containing foo, we need to have both symbols
2802 dynamic so that references to .foo are satisfied during the early
2803 stages of linking. Otherwise the linker might decide to pull in a
2804 definition from some other object, eg. a static library.
2806 Update: As of August 2004, we support a new convention. Function
2807 calls may use the function descriptor symbol, ie. "bl foo". This
2808 behaves exactly as "bl .foo". */
2810 /* Of those relocs that might be copied as dynamic relocs, this
2811 function selects those that must be copied when linking a shared
2812 library or PIE, even when the symbol is local. */
2814 static int
2815 must_be_dyn_reloc (struct bfd_link_info *info,
2816 enum elf_ppc64_reloc_type r_type)
2818 switch (r_type)
2820 default:
2821 /* Only relative relocs can be resolved when the object load
2822 address isn't fixed. DTPREL64 is excluded because the
2823 dynamic linker needs to differentiate global dynamic from
2824 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2825 return 1;
2827 case R_PPC64_REL32:
2828 case R_PPC64_REL64:
2829 case R_PPC64_REL30:
2830 case R_PPC64_TOC16:
2831 case R_PPC64_TOC16_DS:
2832 case R_PPC64_TOC16_LO:
2833 case R_PPC64_TOC16_HI:
2834 case R_PPC64_TOC16_HA:
2835 case R_PPC64_TOC16_LO_DS:
2836 return 0;
2838 case R_PPC64_TPREL16:
2839 case R_PPC64_TPREL16_LO:
2840 case R_PPC64_TPREL16_HI:
2841 case R_PPC64_TPREL16_HA:
2842 case R_PPC64_TPREL16_DS:
2843 case R_PPC64_TPREL16_LO_DS:
2844 case R_PPC64_TPREL16_HIGH:
2845 case R_PPC64_TPREL16_HIGHA:
2846 case R_PPC64_TPREL16_HIGHER:
2847 case R_PPC64_TPREL16_HIGHERA:
2848 case R_PPC64_TPREL16_HIGHEST:
2849 case R_PPC64_TPREL16_HIGHESTA:
2850 case R_PPC64_TPREL64:
2851 case R_PPC64_TPREL34:
2852 /* These relocations are relative but in a shared library the
2853 linker doesn't know the thread pointer base. */
2854 return bfd_link_dll (info);
2858 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2859 copying dynamic variables from a shared lib into an app's .dynbss
2860 section, and instead use a dynamic relocation to point into the
2861 shared lib. With code that gcc generates it is vital that this be
2862 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2863 actually the address of a function descriptor which resides in the
2864 .opd section. gcc uses the descriptor directly rather than going
2865 via the GOT as some other ABIs do, which means that initialized
2866 function pointers reference the descriptor. Thus, a function
2867 pointer initialized to the address of a function in a shared
2868 library will either require a .dynbss copy and a copy reloc, or a
2869 dynamic reloc. Using a .dynbss copy redefines the function
2870 descriptor symbol to point to the copy. This presents a problem as
2871 a PLT entry for that function is also initialized from the function
2872 descriptor symbol and the copy may not be initialized first. */
2873 #define ELIMINATE_COPY_RELOCS 1
2875 /* Section name for stubs is the associated section name plus this
2876 string. */
2877 #define STUB_SUFFIX ".stub"
2879 /* Linker stubs.
2880 ppc_stub_long_branch:
2881 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2882 destination, but a 24 bit branch in a stub section will reach.
2883 . b dest
2885 ppc_stub_plt_branch:
2886 Similar to the above, but a 24 bit branch in the stub section won't
2887 reach its destination.
2888 . addis %r12,%r2,xxx@toc@ha
2889 . ld %r12,xxx@toc@l(%r12)
2890 . mtctr %r12
2891 . bctr
2893 ppc_stub_plt_call:
2894 Used to call a function in a shared library. If it so happens that
2895 the plt entry referenced crosses a 64k boundary, then an extra
2896 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2897 An r2save variant starts with "std %r2,40(%r1)".
2898 . addis %r11,%r2,xxx@toc@ha
2899 . ld %r12,xxx+0@toc@l(%r11)
2900 . mtctr %r12
2901 . ld %r2,xxx+8@toc@l(%r11)
2902 . ld %r11,xxx+16@toc@l(%r11)
2903 . bctr
2905 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2906 code to adjust the value and save r2 to support multiple toc sections.
2907 A ppc_stub_long_branch with an r2 offset looks like:
2908 . std %r2,40(%r1)
2909 . addis %r2,%r2,off@ha
2910 . addi %r2,%r2,off@l
2911 . b dest
2913 A ppc_stub_plt_branch with an r2 offset looks like:
2914 . std %r2,40(%r1)
2915 . addis %r12,%r2,xxx@toc@ha
2916 . ld %r12,xxx@toc@l(%r12)
2917 . addis %r2,%r2,off@ha
2918 . addi %r2,%r2,off@l
2919 . mtctr %r12
2920 . bctr
2922 All of the above stubs are shown as their ELFv1 variants. ELFv2
2923 variants exist too, simpler for plt calls since a new toc pointer
2924 and static chain are not loaded by the stub. In addition, ELFv2
2925 has some more complex stubs to handle calls marked with NOTOC
2926 relocs from functions where r2 is not a valid toc pointer.
2927 ppc_stub_long_branch_p9notoc:
2928 . mflr %r12
2929 . bcl 20,31,1f
2930 . 1:
2931 . mflr %r11
2932 . mtlr %r12
2933 . addis %r12,%r11,dest-1b@ha
2934 . addi %r12,%r12,dest-1b@l
2935 . b dest
2937 ppc_stub_plt_branch_p9notoc:
2938 . mflr %r12
2939 . bcl 20,31,1f
2940 . 1:
2941 . mflr %r11
2942 . mtlr %r12
2943 . lis %r12,xxx-1b@highest
2944 . ori %r12,%r12,xxx-1b@higher
2945 . sldi %r12,%r12,32
2946 . oris %r12,%r12,xxx-1b@high
2947 . ori %r12,%r12,xxx-1b@l
2948 . add %r12,%r11,%r12
2949 . mtctr %r12
2950 . bctr
2952 ppc_stub_plt_call_p9notoc:
2953 . mflr %r12
2954 . bcl 20,31,1f
2955 . 1:
2956 . mflr %r11
2957 . mtlr %r12
2958 . lis %r12,xxx-1b@highest
2959 . ori %r12,%r12,xxx-1b@higher
2960 . sldi %r12,%r12,32
2961 . oris %r12,%r12,xxx-1b@high
2962 . ori %r12,%r12,xxx-1b@l
2963 . ldx %r12,%r11,%r12
2964 . mtctr %r12
2965 . bctr
2967 There are also ELFv1 power10 variants of these stubs.
2968 ppc_stub_long_branch_notoc:
2969 . pla %r12,dest@pcrel
2970 . b dest
2971 ppc_stub_plt_branch_notoc:
2972 . lis %r11,(dest-1f)@highesta34
2973 . ori %r11,%r11,(dest-1f)@highera34
2974 . sldi %r11,%r11,34
2975 . 1: pla %r12,dest@pcrel
2976 . add %r12,%r11,%r12
2977 . mtctr %r12
2978 . bctr
2979 ppc_stub_plt_call_notoc:
2980 . lis %r11,(xxx-1f)@highesta34
2981 . ori %r11,%r11,(xxx-1f)@highera34
2982 . sldi %r11,%r11,34
2983 . 1: pla %r12,xxx@pcrel
2984 . ldx %r12,%r11,%r12
2985 . mtctr %r12
2986 . bctr
2988 In cases where the high instructions would add zero, they are
2989 omitted and following instructions modified in some cases.
2990 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2992 . pld %r12,xxx@pcrel
2993 . mtctr %r12
2994 . bctr
2996 Stub variants may be merged. For example, if printf is called from
2997 code with the tocsave optimization (ie. r2 saved in function
2998 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2999 and from other code without the tocsave optimization requiring a
3000 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
3001 type will be created. Calls with the tocsave optimization will
3002 enter this stub after the instruction saving r2. A similar
3003 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3004 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3005 to call an external function like printf. If other calls to printf
3006 require a ppc_stub_plt_call linkage stub then a single
3007 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3008 call. */
3010 enum ppc_stub_main_type
3012 ppc_stub_none,
3013 ppc_stub_long_branch,
3014 ppc_stub_plt_branch,
3015 ppc_stub_plt_call,
3016 ppc_stub_global_entry,
3017 ppc_stub_save_res
3020 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3021 these variations. */
3023 enum ppc_stub_sub_type
3025 ppc_stub_toc,
3026 ppc_stub_notoc,
3027 ppc_stub_p9notoc
3030 struct ppc_stub_type
3032 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3033 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3034 unsigned int r2save : 1;
3037 /* Information on stub grouping. */
3038 struct map_stub
3040 /* The stub section. */
3041 asection *stub_sec;
3042 /* This is the section to which stubs in the group will be attached. */
3043 asection *link_sec;
3044 /* Next group. */
3045 struct map_stub *next;
3046 /* Whether to emit a copy of register save/restore functions in this
3047 group. */
3048 int needs_save_res;
3049 /* Current offset within stubs after the insn restoring lr in a
3050 _notoc or _both stub using bcl for pc-relative addressing, or
3051 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3052 unsigned int lr_restore;
3053 /* Accumulated size of EH info emitted to describe return address
3054 if stubs modify lr. Does not include 17 byte FDE header. */
3055 unsigned int eh_size;
3056 /* Offset in glink_eh_frame to the start of EH info for this group. */
3057 unsigned int eh_base;
3060 struct ppc_stub_hash_entry
3062 /* Base hash table entry structure. */
3063 struct bfd_hash_entry root;
3065 struct ppc_stub_type type;
3067 /* Group information. */
3068 struct map_stub *group;
3070 /* Offset within stub_sec of the beginning of this stub. */
3071 bfd_vma stub_offset;
3073 /* Given the symbol's value and its section we can determine its final
3074 value when building the stubs (so the stub knows where to jump. */
3075 bfd_vma target_value;
3076 asection *target_section;
3078 /* The symbol table entry, if any, that this was derived from. */
3079 struct ppc_link_hash_entry *h;
3080 struct plt_entry *plt_ent;
3082 /* Symbol type. */
3083 unsigned char symtype;
3085 /* Symbol st_other. */
3086 unsigned char other;
3088 /* Debug: Track hash table traversal. */
3089 unsigned int id;
3092 struct ppc_branch_hash_entry
3094 /* Base hash table entry structure. */
3095 struct bfd_hash_entry root;
3097 /* Offset within branch lookup table. */
3098 unsigned int offset;
3100 /* Generation marker. */
3101 unsigned int iter;
3104 /* Used to track dynamic relocations. */
3105 struct ppc_dyn_relocs
3107 struct ppc_dyn_relocs *next;
3109 /* The input section of the reloc. */
3110 asection *sec;
3112 /* Total number of relocs copied for the input section. */
3113 unsigned int count;
3115 /* Number of pc-relative relocs copied for the input section. */
3116 unsigned int pc_count;
3118 /* Number of relocs that might become R_PPC64_RELATIVE. */
3119 unsigned int rel_count;
3122 struct ppc_local_dyn_relocs
3124 struct ppc_local_dyn_relocs *next;
3126 /* The input section of the reloc. */
3127 asection *sec;
3129 /* Total number of relocs copied for the input section. */
3130 unsigned int count;
3132 /* Number of relocs that might become R_PPC64_RELATIVE. */
3133 unsigned int rel_count : 31;
3135 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3136 unsigned int ifunc : 1;
3139 struct ppc_link_hash_entry
3141 struct elf_link_hash_entry elf;
3143 union
3145 /* A pointer to the most recently used stub hash entry against this
3146 symbol. */
3147 struct ppc_stub_hash_entry *stub_cache;
3149 /* A pointer to the next symbol starting with a '.' */
3150 struct ppc_link_hash_entry *next_dot_sym;
3151 } u;
3153 /* Link between function code and descriptor symbols. */
3154 struct ppc_link_hash_entry *oh;
3156 /* Flag function code and descriptor symbols. */
3157 unsigned int is_func:1;
3158 unsigned int is_func_descriptor:1;
3159 unsigned int fake:1;
3161 /* Whether global opd/toc sym has been adjusted or not.
3162 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3163 should be set for all globals defined in any opd/toc section. */
3164 unsigned int adjust_done:1;
3166 /* Set if this is an out-of-line register save/restore function,
3167 with non-standard calling convention. */
3168 unsigned int save_res:1;
3170 /* Set if a duplicate symbol with non-zero localentry is detected,
3171 even when the duplicate symbol does not provide a definition. */
3172 unsigned int non_zero_localentry:1;
3174 /* Contexts in which symbol is used in the GOT (or TOC).
3175 Bits are or'd into the mask as the corresponding relocs are
3176 encountered during check_relocs, with TLS_TLS being set when any
3177 of the other TLS bits are set. tls_optimize clears bits when
3178 optimizing to indicate the corresponding GOT entry type is not
3179 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3180 set TLS_GDIE when a GD reloc turns into an IE one.
3181 These flags are also kept for local symbols. */
3182 #define TLS_TLS 1 /* Any TLS reloc. */
3183 #define TLS_GD 2 /* GD reloc. */
3184 #define TLS_LD 4 /* LD reloc. */
3185 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3186 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3187 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3188 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3189 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3190 unsigned char tls_mask;
3192 /* The above field is also used to mark function symbols. In which
3193 case TLS_TLS will be 0. */
3194 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3195 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3196 #define NON_GOT 256 /* local symbol plt, not stored. */
3199 static inline struct ppc_link_hash_entry *
3200 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3202 return (struct ppc_link_hash_entry *) ent;
3205 static inline struct elf_link_hash_entry *
3206 elf_hash_entry (struct ppc_link_hash_entry *ent)
3208 return (struct elf_link_hash_entry *) ent;
3211 /* ppc64 ELF linker hash table. */
3213 struct ppc_link_hash_table
3215 struct elf_link_hash_table elf;
3217 /* The stub hash table. */
3218 struct bfd_hash_table stub_hash_table;
3220 /* Another hash table for plt_branch stubs. */
3221 struct bfd_hash_table branch_hash_table;
3223 /* Hash table for function prologue tocsave. */
3224 htab_t tocsave_htab;
3226 /* Various options and other info passed from the linker. */
3227 struct ppc64_elf_params *params;
3229 /* The size of sec_info below. */
3230 unsigned int sec_info_arr_size;
3232 /* Per-section array of extra section info. Done this way rather
3233 than as part of ppc64_elf_section_data so we have the info for
3234 non-ppc64 sections. */
3235 struct
3237 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3238 bfd_vma toc_off;
3240 union
3242 /* The section group that this section belongs to. */
3243 struct map_stub *group;
3244 /* A temp section list pointer. */
3245 asection *list;
3246 } u;
3247 } *sec_info;
3249 /* Linked list of groups. */
3250 struct map_stub *group;
3252 /* Temp used when calculating TOC pointers. */
3253 bfd_vma toc_curr;
3254 bfd *toc_bfd;
3255 asection *toc_first_sec;
3257 /* Used when adding symbols. */
3258 struct ppc_link_hash_entry *dot_syms;
3260 /* Shortcuts to get to dynamic linker sections. */
3261 asection *glink;
3262 asection *global_entry;
3263 asection *sfpr;
3264 asection *pltlocal;
3265 asection *relpltlocal;
3266 asection *brlt;
3267 asection *relbrlt;
3268 asection *glink_eh_frame;
3270 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3271 struct ppc_link_hash_entry *tls_get_addr;
3272 struct ppc_link_hash_entry *tls_get_addr_fd;
3273 struct ppc_link_hash_entry *tga_desc;
3274 struct ppc_link_hash_entry *tga_desc_fd;
3275 struct map_stub *tga_group;
3277 /* The size of reliplt used by got entry relocs. */
3278 bfd_size_type got_reli_size;
3280 /* DT_RELR array of section/r_offset. */
3281 size_t relr_alloc;
3282 size_t relr_count;
3283 struct
3285 asection *sec;
3286 bfd_vma off;
3287 } *relr;
3289 /* Statistics. */
3290 unsigned long stub_count[ppc_stub_save_res];
3292 /* Number of stubs against global syms. */
3293 unsigned long stub_globals;
3295 /* Set if we're linking code with function descriptors. */
3296 unsigned int opd_abi:1;
3298 /* Support for multiple toc sections. */
3299 unsigned int do_multi_toc:1;
3300 unsigned int multi_toc_needed:1;
3301 unsigned int second_toc_pass:1;
3302 unsigned int do_toc_opt:1;
3304 /* Set if tls optimization is enabled. */
3305 unsigned int do_tls_opt:1;
3307 /* Set if inline plt calls should be converted to direct calls. */
3308 unsigned int can_convert_all_inline_plt:1;
3310 /* Set if a stub_offset changed. */
3311 unsigned int stub_changed:1;
3313 /* Set on error. */
3314 unsigned int stub_error:1;
3316 /* Whether func_desc_adjust needs to be run over symbols. */
3317 unsigned int need_func_desc_adj:1;
3319 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3320 unsigned int has_plt_localentry0:1;
3322 /* Whether calls are made via the PLT from NOTOC functions. */
3323 unsigned int notoc_plt:1;
3325 /* Whether any code linked seems to be Power10. */
3326 unsigned int has_power10_relocs:1;
3328 /* Incremented once for each stub sized. */
3329 unsigned int stub_id;
3331 /* Incremented every time we size stubs. */
3332 unsigned int stub_iteration;
3334 /* After 20 iterations of stub sizing we no longer allow stubs to
3335 shrink. This is to break out of a pathological case where adding
3336 stubs or increasing their size on one iteration decreases section
3337 gaps (perhaps due to alignment), which then results in smaller
3338 stubs on the next iteration. */
3339 #define STUB_SHRINK_ITER 20
3342 /* Rename some of the generic section flags to better document how they
3343 are used here. */
3345 /* Nonzero if this section has TLS related relocations. */
3346 #define has_tls_reloc sec_flg0
3348 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3349 relocations. */
3350 #define nomark_tls_get_addr sec_flg1
3352 /* Nonzero if this section has any toc or got relocs. */
3353 #define has_toc_reloc sec_flg2
3355 /* Nonzero if this section has a call to another section that uses
3356 the toc or got. */
3357 #define makes_toc_func_call sec_flg3
3359 /* Recursion protection when determining above flag. */
3360 #define call_check_in_progress sec_flg4
3361 #define call_check_done sec_flg5
3363 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3365 #define ppc_hash_table(p) \
3366 ((is_elf_hash_table ((p)->hash) \
3367 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3368 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3370 #define ppc_stub_hash_lookup(table, string, create, copy) \
3371 ((struct ppc_stub_hash_entry *) \
3372 bfd_hash_lookup ((table), (string), (create), (copy)))
3374 #define ppc_branch_hash_lookup(table, string, create, copy) \
3375 ((struct ppc_branch_hash_entry *) \
3376 bfd_hash_lookup ((table), (string), (create), (copy)))
3378 /* Create an entry in the stub hash table. */
3380 static struct bfd_hash_entry *
3381 stub_hash_newfunc (struct bfd_hash_entry *entry,
3382 struct bfd_hash_table *table,
3383 const char *string)
3385 /* Allocate the structure if it has not already been allocated by a
3386 subclass. */
3387 if (entry == NULL)
3389 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3390 if (entry == NULL)
3391 return entry;
3394 /* Call the allocation method of the superclass. */
3395 entry = bfd_hash_newfunc (entry, table, string);
3396 if (entry != NULL)
3398 struct ppc_stub_hash_entry *eh;
3400 /* Initialize the local fields. */
3401 eh = (struct ppc_stub_hash_entry *) entry;
3402 eh->type.main = ppc_stub_none;
3403 eh->type.sub = ppc_stub_toc;
3404 eh->type.r2save = 0;
3405 eh->group = NULL;
3406 eh->stub_offset = 0;
3407 eh->target_value = 0;
3408 eh->target_section = NULL;
3409 eh->h = NULL;
3410 eh->plt_ent = NULL;
3411 eh->symtype = 0;
3412 eh->other = 0;
3413 eh->id = 0;
3416 return entry;
3419 /* Create an entry in the branch hash table. */
3421 static struct bfd_hash_entry *
3422 branch_hash_newfunc (struct bfd_hash_entry *entry,
3423 struct bfd_hash_table *table,
3424 const char *string)
3426 /* Allocate the structure if it has not already been allocated by a
3427 subclass. */
3428 if (entry == NULL)
3430 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3431 if (entry == NULL)
3432 return entry;
3435 /* Call the allocation method of the superclass. */
3436 entry = bfd_hash_newfunc (entry, table, string);
3437 if (entry != NULL)
3439 struct ppc_branch_hash_entry *eh;
3441 /* Initialize the local fields. */
3442 eh = (struct ppc_branch_hash_entry *) entry;
3443 eh->offset = 0;
3444 eh->iter = 0;
3447 return entry;
3450 /* Create an entry in a ppc64 ELF linker hash table. */
3452 static struct bfd_hash_entry *
3453 link_hash_newfunc (struct bfd_hash_entry *entry,
3454 struct bfd_hash_table *table,
3455 const char *string)
3457 /* Allocate the structure if it has not already been allocated by a
3458 subclass. */
3459 if (entry == NULL)
3461 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3462 if (entry == NULL)
3463 return entry;
3466 /* Call the allocation method of the superclass. */
3467 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3468 if (entry != NULL)
3470 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3472 memset (&eh->u.stub_cache, 0,
3473 (sizeof (struct ppc_link_hash_entry)
3474 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3476 /* When making function calls, old ABI code references function entry
3477 points (dot symbols), while new ABI code references the function
3478 descriptor symbol. We need to make any combination of reference and
3479 definition work together, without breaking archive linking.
3481 For a defined function "foo" and an undefined call to "bar":
3482 An old object defines "foo" and ".foo", references ".bar" (possibly
3483 "bar" too).
3484 A new object defines "foo" and references "bar".
3486 A new object thus has no problem with its undefined symbols being
3487 satisfied by definitions in an old object. On the other hand, the
3488 old object won't have ".bar" satisfied by a new object.
3490 Keep a list of newly added dot-symbols. */
3492 if (string[0] == '.')
3494 struct ppc_link_hash_table *htab;
3496 htab = (struct ppc_link_hash_table *) table;
3497 eh->u.next_dot_sym = htab->dot_syms;
3498 htab->dot_syms = eh;
3502 return entry;
3505 struct tocsave_entry
3507 asection *sec;
3508 bfd_vma offset;
3511 static hashval_t
3512 tocsave_htab_hash (const void *p)
3514 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3515 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3518 static int
3519 tocsave_htab_eq (const void *p1, const void *p2)
3521 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3522 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3523 return e1->sec == e2->sec && e1->offset == e2->offset;
3526 /* Destroy a ppc64 ELF linker hash table. */
3528 static void
3529 ppc64_elf_link_hash_table_free (bfd *obfd)
3531 struct ppc_link_hash_table *htab;
3533 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3534 if (htab->tocsave_htab)
3535 htab_delete (htab->tocsave_htab);
3536 bfd_hash_table_free (&htab->branch_hash_table);
3537 bfd_hash_table_free (&htab->stub_hash_table);
3538 _bfd_elf_link_hash_table_free (obfd);
3541 /* Create a ppc64 ELF linker hash table. */
3543 static struct bfd_link_hash_table *
3544 ppc64_elf_link_hash_table_create (bfd *abfd)
3546 struct ppc_link_hash_table *htab;
3547 size_t amt = sizeof (struct ppc_link_hash_table);
3549 htab = bfd_zmalloc (amt);
3550 if (htab == NULL)
3551 return NULL;
3553 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3554 sizeof (struct ppc_link_hash_entry),
3555 PPC64_ELF_DATA))
3557 free (htab);
3558 return NULL;
3561 /* Init the stub hash table too. */
3562 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3563 sizeof (struct ppc_stub_hash_entry)))
3565 _bfd_elf_link_hash_table_free (abfd);
3566 return NULL;
3569 /* And the branch hash table. */
3570 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3571 sizeof (struct ppc_branch_hash_entry)))
3573 bfd_hash_table_free (&htab->stub_hash_table);
3574 _bfd_elf_link_hash_table_free (abfd);
3575 return NULL;
3578 htab->tocsave_htab = htab_try_create (1024,
3579 tocsave_htab_hash,
3580 tocsave_htab_eq,
3581 NULL);
3582 if (htab->tocsave_htab == NULL)
3584 ppc64_elf_link_hash_table_free (abfd);
3585 return NULL;
3587 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3589 /* Initializing two fields of the union is just cosmetic. We really
3590 only care about glist, but when compiled on a 32-bit host the
3591 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3592 debugger inspection of these fields look nicer. */
3593 htab->elf.init_got_refcount.refcount = 0;
3594 htab->elf.init_got_refcount.glist = NULL;
3595 htab->elf.init_plt_refcount.refcount = 0;
3596 htab->elf.init_plt_refcount.glist = NULL;
3597 htab->elf.init_got_offset.offset = 0;
3598 htab->elf.init_got_offset.glist = NULL;
3599 htab->elf.init_plt_offset.offset = 0;
3600 htab->elf.init_plt_offset.glist = NULL;
3602 return &htab->elf.root;
3605 /* Create sections for linker generated code. */
3607 static bool
3608 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3610 struct ppc_link_hash_table *htab;
3611 flagword flags;
3613 htab = ppc_hash_table (info);
3615 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3616 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3617 if (htab->params->save_restore_funcs)
3619 /* Create .sfpr for code to save and restore fp regs. */
3620 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3621 flags);
3622 if (htab->sfpr == NULL
3623 || !bfd_set_section_alignment (htab->sfpr, 2))
3624 return false;
3627 if (bfd_link_relocatable (info))
3628 return true;
3630 /* Create .glink for lazy dynamic linking support. */
3631 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3632 flags);
3633 if (htab->glink == NULL
3634 || !bfd_set_section_alignment (htab->glink, 3))
3635 return false;
3637 /* The part of .glink used by global entry stubs, separate so that
3638 it can be aligned appropriately without affecting htab->glink. */
3639 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3640 flags);
3641 if (htab->global_entry == NULL
3642 || !bfd_set_section_alignment (htab->global_entry, 2))
3643 return false;
3645 if (!info->no_ld_generated_unwind_info)
3647 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3648 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3649 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3650 ".eh_frame",
3651 flags);
3652 if (htab->glink_eh_frame == NULL
3653 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3654 return false;
3657 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3658 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3659 if (htab->elf.iplt == NULL
3660 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3661 return false;
3663 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3664 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3665 htab->elf.irelplt
3666 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3667 if (htab->elf.irelplt == NULL
3668 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3669 return false;
3671 /* Create branch lookup table for plt_branch stubs. */
3672 flags = (SEC_ALLOC | SEC_LOAD
3673 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3674 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3675 flags);
3676 if (htab->brlt == NULL
3677 || !bfd_set_section_alignment (htab->brlt, 3))
3678 return false;
3680 /* Local plt entries, put in .branch_lt but a separate section for
3681 convenience. */
3682 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3683 flags);
3684 if (htab->pltlocal == NULL
3685 || !bfd_set_section_alignment (htab->pltlocal, 3))
3686 return false;
3688 if (!bfd_link_pic (info))
3689 return true;
3691 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3692 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3693 htab->relbrlt
3694 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3695 if (htab->relbrlt == NULL
3696 || !bfd_set_section_alignment (htab->relbrlt, 3))
3697 return false;
3699 htab->relpltlocal
3700 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3701 if (htab->relpltlocal == NULL
3702 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3703 return false;
3705 return true;
3708 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3710 bool
3711 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3712 struct ppc64_elf_params *params)
3714 struct ppc_link_hash_table *htab;
3716 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3718 /* Always hook our dynamic sections into the first bfd, which is the
3719 linker created stub bfd. This ensures that the GOT header is at
3720 the start of the output TOC section. */
3721 htab = ppc_hash_table (info);
3722 htab->elf.dynobj = params->stub_bfd;
3723 htab->params = params;
3725 return create_linkage_sections (htab->elf.dynobj, info);
3728 /* Build a name for an entry in the stub hash table. */
3730 static char *
3731 ppc_stub_name (const asection *input_section,
3732 const asection *sym_sec,
3733 const struct ppc_link_hash_entry *h,
3734 const Elf_Internal_Rela *rel)
3736 char *stub_name;
3737 ssize_t len;
3739 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3740 offsets from a sym as a branch target? In fact, we could
3741 probably assume the addend is always zero. */
3742 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3744 if (h)
3746 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3747 stub_name = bfd_malloc (len);
3748 if (stub_name == NULL)
3749 return stub_name;
3751 len = sprintf (stub_name, "%08x.%s+%x",
3752 input_section->id & 0xffffffff,
3753 h->elf.root.root.string,
3754 (int) rel->r_addend & 0xffffffff);
3756 else
3758 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3759 stub_name = bfd_malloc (len);
3760 if (stub_name == NULL)
3761 return stub_name;
3763 len = sprintf (stub_name, "%08x.%x:%x+%x",
3764 input_section->id & 0xffffffff,
3765 sym_sec->id & 0xffffffff,
3766 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3767 (int) rel->r_addend & 0xffffffff);
3769 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3770 stub_name[len - 2] = 0;
3771 return stub_name;
3774 /* If mixing power10 with non-power10 code and --power10-stubs is not
3775 specified (or is auto) then there may be multiple stub types for any
3776 given symbol. Up to three classes of stubs are stored in separate
3777 stub_hash_table entries having the same key string. The entries
3778 will always be adjacent on entry->root.next chain, even if hash
3779 table resizing occurs. This function selects the correct entry to
3780 use. */
3782 static struct ppc_stub_hash_entry *
3783 select_alt_stub (struct ppc_stub_hash_entry *entry,
3784 enum elf_ppc64_reloc_type r_type)
3786 enum ppc_stub_sub_type subt;
3788 switch (r_type)
3790 case R_PPC64_REL24_NOTOC:
3791 subt = ppc_stub_notoc;
3792 break;
3793 case R_PPC64_REL24_P9NOTOC:
3794 subt = ppc_stub_p9notoc;
3795 break;
3796 default:
3797 subt = ppc_stub_toc;
3798 break;
3801 while (entry != NULL && entry->type.sub != subt)
3803 const char *stub_name = entry->root.string;
3805 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3806 if (entry != NULL
3807 && entry->root.string != stub_name)
3808 entry = NULL;
3811 return entry;
3814 /* Look up an entry in the stub hash. Stub entries are cached because
3815 creating the stub name takes a bit of time. */
3817 static struct ppc_stub_hash_entry *
3818 ppc_get_stub_entry (const asection *input_section,
3819 const asection *sym_sec,
3820 struct ppc_link_hash_entry *h,
3821 const Elf_Internal_Rela *rel,
3822 struct ppc_link_hash_table *htab)
3824 struct ppc_stub_hash_entry *stub_entry;
3825 struct map_stub *group;
3827 /* If this input section is part of a group of sections sharing one
3828 stub section, then use the id of the first section in the group.
3829 Stub names need to include a section id, as there may well be
3830 more than one stub used to reach say, printf, and we need to
3831 distinguish between them. */
3832 group = htab->sec_info[input_section->id].u.group;
3833 if (group == NULL)
3834 return NULL;
3836 if (h != NULL && h->u.stub_cache != NULL
3837 && h->u.stub_cache->h == h
3838 && h->u.stub_cache->group == group)
3840 stub_entry = h->u.stub_cache;
3842 else
3844 char *stub_name;
3846 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3847 if (stub_name == NULL)
3848 return NULL;
3850 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3851 stub_name, false, false);
3852 if (h != NULL)
3853 h->u.stub_cache = stub_entry;
3855 free (stub_name);
3858 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3859 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3861 return stub_entry;
3864 /* Add a new stub entry to the stub hash. Not all fields of the new
3865 stub entry are initialised. */
3867 static struct ppc_stub_hash_entry *
3868 ppc_add_stub (const char *stub_name,
3869 asection *section,
3870 struct bfd_link_info *info)
3872 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3873 struct map_stub *group;
3874 asection *link_sec;
3875 asection *stub_sec;
3876 struct ppc_stub_hash_entry *stub_entry;
3878 group = htab->sec_info[section->id].u.group;
3879 link_sec = group->link_sec;
3880 stub_sec = group->stub_sec;
3881 if (stub_sec == NULL)
3883 size_t namelen;
3884 bfd_size_type len;
3885 char *s_name;
3887 namelen = strlen (link_sec->name);
3888 len = namelen + sizeof (STUB_SUFFIX);
3889 s_name = bfd_alloc (htab->params->stub_bfd, len);
3890 if (s_name == NULL)
3891 return NULL;
3893 memcpy (s_name, link_sec->name, namelen);
3894 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3895 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3896 if (stub_sec == NULL)
3897 return NULL;
3898 group->stub_sec = stub_sec;
3901 /* Enter this entry into the linker stub hash table. */
3902 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3903 true, false);
3904 if (stub_entry == NULL)
3906 /* xgettext:c-format */
3907 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3908 section->owner, stub_name);
3909 return NULL;
3912 stub_entry->group = group;
3913 stub_entry->stub_offset = 0;
3914 return stub_entry;
3917 /* A stub has already been created, but it may not be the required
3918 type. We shouldn't be transitioning from plt_call to long_branch
3919 stubs or vice versa, but we might be upgrading from plt_call to
3920 plt_call with r2save for example. */
3922 static bool
3923 ppc_merge_stub (struct ppc_link_hash_table *htab,
3924 struct ppc_stub_hash_entry *stub_entry,
3925 struct ppc_stub_type stub_type,
3926 enum elf_ppc64_reloc_type r_type)
3928 struct ppc_stub_type old_type = stub_entry->type;
3930 if (old_type.main == ppc_stub_save_res)
3931 return true;
3933 if (htab->params->power10_stubs == -1)
3935 /* For --power10-stubs=auto, don't merge _notoc and other
3936 varieties of stubs. */
3937 struct ppc_stub_hash_entry *alt_stub;
3939 alt_stub = select_alt_stub (stub_entry, r_type);
3940 if (alt_stub == NULL)
3942 alt_stub = ((struct ppc_stub_hash_entry *)
3943 stub_hash_newfunc (NULL,
3944 &htab->stub_hash_table,
3945 stub_entry->root.string));
3946 if (alt_stub == NULL)
3947 return false;
3949 *alt_stub = *stub_entry;
3950 stub_entry->root.next = &alt_stub->root;
3952 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3953 Not that it matters, this just puts smaller stubs first. */
3954 if (stub_type.sub == ppc_stub_notoc)
3955 alt_stub = stub_entry;
3956 else if (stub_type.sub == ppc_stub_p9notoc
3957 && alt_stub->root.next
3958 && alt_stub->root.next->string == alt_stub->root.string)
3960 struct ppc_stub_hash_entry *next
3961 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3962 alt_stub->type = next->type;
3963 alt_stub = next;
3965 alt_stub->type = stub_type;
3966 return true;
3968 stub_entry = alt_stub;
3971 old_type = stub_entry->type;
3972 if (old_type.main == ppc_stub_plt_branch)
3973 old_type.main = ppc_stub_long_branch;
3975 if (old_type.main != stub_type.main
3976 || (old_type.sub != stub_type.sub
3977 && old_type.sub != ppc_stub_toc
3978 && stub_type.sub != ppc_stub_toc))
3979 abort ();
3981 stub_entry->type.sub |= stub_type.sub;
3982 stub_entry->type.r2save |= stub_type.r2save;
3983 return true;
3986 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3987 not already done. */
3989 static bool
3990 create_got_section (bfd *abfd, struct bfd_link_info *info)
3992 asection *got, *relgot;
3993 flagword flags;
3994 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3996 if (!is_ppc64_elf (abfd))
3997 return false;
3998 if (htab == NULL)
3999 return false;
4001 if (!htab->elf.sgot
4002 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4003 return false;
4005 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4006 | SEC_LINKER_CREATED);
4008 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4009 if (!got
4010 || !bfd_set_section_alignment (got, 3))
4011 return false;
4013 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4014 flags | SEC_READONLY);
4015 if (!relgot
4016 || !bfd_set_section_alignment (relgot, 3))
4017 return false;
4019 ppc64_elf_tdata (abfd)->got = got;
4020 ppc64_elf_tdata (abfd)->relgot = relgot;
4021 return true;
4024 /* Follow indirect and warning symbol links. */
4026 static inline struct bfd_link_hash_entry *
4027 follow_link (struct bfd_link_hash_entry *h)
4029 while (h->type == bfd_link_hash_indirect
4030 || h->type == bfd_link_hash_warning)
4031 h = h->u.i.link;
4032 return h;
4035 static inline struct elf_link_hash_entry *
4036 elf_follow_link (struct elf_link_hash_entry *h)
4038 return (struct elf_link_hash_entry *) follow_link (&h->root);
4041 static inline struct ppc_link_hash_entry *
4042 ppc_follow_link (struct ppc_link_hash_entry *h)
4044 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4047 /* Merge PLT info on FROM with that on TO. */
4049 static void
4050 move_plt_plist (struct ppc_link_hash_entry *from,
4051 struct ppc_link_hash_entry *to)
4053 if (from->elf.plt.plist != NULL)
4055 if (to->elf.plt.plist != NULL)
4057 struct plt_entry **entp;
4058 struct plt_entry *ent;
4060 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4062 struct plt_entry *dent;
4064 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4065 if (dent->addend == ent->addend)
4067 dent->plt.refcount += ent->plt.refcount;
4068 *entp = ent->next;
4069 break;
4071 if (dent == NULL)
4072 entp = &ent->next;
4074 *entp = to->elf.plt.plist;
4077 to->elf.plt.plist = from->elf.plt.plist;
4078 from->elf.plt.plist = NULL;
4082 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4084 static void
4085 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4086 struct elf_link_hash_entry *dir,
4087 struct elf_link_hash_entry *ind)
4089 struct ppc_link_hash_entry *edir, *eind;
4091 edir = ppc_elf_hash_entry (dir);
4092 eind = ppc_elf_hash_entry (ind);
4094 edir->is_func |= eind->is_func;
4095 edir->is_func_descriptor |= eind->is_func_descriptor;
4096 edir->tls_mask |= eind->tls_mask;
4097 if (eind->oh != NULL)
4098 edir->oh = ppc_follow_link (eind->oh);
4100 if (edir->elf.versioned != versioned_hidden)
4101 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4102 edir->elf.ref_regular |= eind->elf.ref_regular;
4103 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4104 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4105 edir->elf.needs_plt |= eind->elf.needs_plt;
4106 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4108 /* If we were called to copy over info for a weak sym, don't copy
4109 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4110 in order to simplify readonly_dynrelocs and save a field in the
4111 symbol hash entry, but that means dyn_relocs can't be used in any
4112 tests about a specific symbol, or affect other symbol flags which
4113 are then tested. */
4114 if (eind->elf.root.type != bfd_link_hash_indirect)
4115 return;
4117 /* Copy over any dynamic relocs we may have on the indirect sym. */
4118 if (ind->dyn_relocs != NULL)
4120 if (dir->dyn_relocs != NULL)
4122 struct ppc_dyn_relocs **pp;
4123 struct ppc_dyn_relocs *p;
4125 /* Add reloc counts against the indirect sym to the direct sym
4126 list. Merge any entries against the same section. */
4127 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4128 (p = *pp) != NULL;
4131 struct ppc_dyn_relocs *q;
4133 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4134 q != NULL;
4135 q = q->next)
4136 if (q->sec == p->sec)
4138 q->count += p->count;
4139 q->pc_count += p->pc_count;
4140 q->rel_count += p->rel_count;
4141 *pp = p->next;
4142 break;
4144 if (q == NULL)
4145 pp = &p->next;
4147 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4150 dir->dyn_relocs = ind->dyn_relocs;
4151 ind->dyn_relocs = NULL;
4154 /* Copy over got entries that we may have already seen to the
4155 symbol which just became indirect. */
4156 if (eind->elf.got.glist != NULL)
4158 if (edir->elf.got.glist != NULL)
4160 struct got_entry **entp;
4161 struct got_entry *ent;
4163 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4165 struct got_entry *dent;
4167 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4168 if (dent->addend == ent->addend
4169 && dent->owner == ent->owner
4170 && dent->tls_type == ent->tls_type)
4172 dent->got.refcount += ent->got.refcount;
4173 *entp = ent->next;
4174 break;
4176 if (dent == NULL)
4177 entp = &ent->next;
4179 *entp = edir->elf.got.glist;
4182 edir->elf.got.glist = eind->elf.got.glist;
4183 eind->elf.got.glist = NULL;
4186 /* And plt entries. */
4187 move_plt_plist (eind, edir);
4189 if (eind->elf.dynindx != -1)
4191 if (edir->elf.dynindx != -1)
4192 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4193 edir->elf.dynstr_index);
4194 edir->elf.dynindx = eind->elf.dynindx;
4195 edir->elf.dynstr_index = eind->elf.dynstr_index;
4196 eind->elf.dynindx = -1;
4197 eind->elf.dynstr_index = 0;
4201 /* Find the function descriptor hash entry from the given function code
4202 hash entry FH. Link the entries via their OH fields. */
4204 static struct ppc_link_hash_entry *
4205 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4207 struct ppc_link_hash_entry *fdh = fh->oh;
4209 if (fdh == NULL)
4211 const char *fd_name = fh->elf.root.root.string + 1;
4213 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4214 false, false, false));
4215 if (fdh == NULL)
4216 return fdh;
4218 fdh->is_func_descriptor = 1;
4219 fdh->oh = fh;
4220 fh->is_func = 1;
4221 fh->oh = fdh;
4224 fdh = ppc_follow_link (fdh);
4225 fdh->is_func_descriptor = 1;
4226 fdh->oh = fh;
4227 return fdh;
4230 /* Make a fake function descriptor sym for the undefined code sym FH. */
4232 static struct ppc_link_hash_entry *
4233 make_fdh (struct bfd_link_info *info,
4234 struct ppc_link_hash_entry *fh)
4236 bfd *abfd = fh->elf.root.u.undef.abfd;
4237 struct bfd_link_hash_entry *bh = NULL;
4238 struct ppc_link_hash_entry *fdh;
4239 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4240 ? BSF_WEAK
4241 : BSF_GLOBAL);
4243 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4244 fh->elf.root.root.string + 1,
4245 flags, bfd_und_section_ptr, 0,
4246 NULL, false, false, &bh))
4247 return NULL;
4249 fdh = (struct ppc_link_hash_entry *) bh;
4250 fdh->elf.non_elf = 0;
4251 fdh->fake = 1;
4252 fdh->is_func_descriptor = 1;
4253 fdh->oh = fh;
4254 fh->is_func = 1;
4255 fh->oh = fdh;
4256 return fdh;
4259 /* Fix function descriptor symbols defined in .opd sections to be
4260 function type. */
4262 static bool
4263 ppc64_elf_add_symbol_hook (bfd *ibfd,
4264 struct bfd_link_info *info,
4265 Elf_Internal_Sym *isym,
4266 const char **name,
4267 flagword *flags ATTRIBUTE_UNUSED,
4268 asection **sec,
4269 bfd_vma *value)
4271 if (*sec != NULL
4272 && strcmp ((*sec)->name, ".opd") == 0)
4274 asection *code_sec;
4276 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4277 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4278 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4280 /* If the symbol is a function defined in .opd, and the function
4281 code is in a discarded group, let it appear to be undefined. */
4282 if (!bfd_link_relocatable (info)
4283 && (*sec)->reloc_count != 0
4284 && opd_entry_value (*sec, *value, &code_sec, NULL,
4285 false) != (bfd_vma) -1
4286 && discarded_section (code_sec))
4288 *sec = bfd_und_section_ptr;
4289 isym->st_shndx = SHN_UNDEF;
4292 else if (*sec != NULL
4293 && strcmp ((*sec)->name, ".toc") == 0
4294 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4296 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4297 if (htab != NULL)
4298 htab->params->object_in_toc = 1;
4301 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4303 if (abiversion (ibfd) == 0)
4304 set_abiversion (ibfd, 2);
4305 else if (abiversion (ibfd) == 1)
4307 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4308 " for ABI version 1"), *name);
4309 bfd_set_error (bfd_error_bad_value);
4310 return false;
4314 return true;
4317 /* Merge non-visibility st_other attributes: local entry point. */
4319 static void
4320 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4321 unsigned int st_other,
4322 bool definition,
4323 bool dynamic)
4325 if (definition && (!dynamic || !h->def_regular))
4326 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4327 | ELF_ST_VISIBILITY (h->other));
4330 /* Hook called on merging a symbol. We use this to clear "fake" since
4331 we now have a real symbol. */
4333 static bool
4334 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4335 const Elf_Internal_Sym *isym,
4336 asection **psec ATTRIBUTE_UNUSED,
4337 bool newdef ATTRIBUTE_UNUSED,
4338 bool olddef ATTRIBUTE_UNUSED,
4339 bfd *oldbfd ATTRIBUTE_UNUSED,
4340 const asection *oldsec ATTRIBUTE_UNUSED)
4342 ppc_elf_hash_entry (h)->fake = 0;
4343 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4344 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4345 return true;
4348 /* This function makes an old ABI object reference to ".bar" cause the
4349 inclusion of a new ABI object archive that defines "bar".
4350 NAME is a symbol defined in an archive. Return a symbol in the hash
4351 table that might be satisfied by the archive symbols. */
4353 static struct bfd_link_hash_entry *
4354 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4355 struct bfd_link_info *info,
4356 const char *name)
4358 struct bfd_link_hash_entry *h;
4359 char *dot_name;
4360 size_t len;
4362 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4363 if (h != NULL
4364 && ppc_hash_table (info) != NULL
4365 /* Don't return this sym if it is a fake function descriptor
4366 created by add_symbol_adjust. */
4367 && !((struct ppc_link_hash_entry *) h)->fake)
4368 return h;
4370 if (name[0] == '.')
4371 return h;
4373 len = strlen (name);
4374 dot_name = bfd_alloc (abfd, len + 2);
4375 if (dot_name == NULL)
4376 return (struct bfd_link_hash_entry *) -1;
4377 dot_name[0] = '.';
4378 memcpy (dot_name + 1, name, len + 1);
4379 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4380 bfd_release (abfd, dot_name);
4381 if (h != NULL)
4382 return h;
4384 if (strcmp (name, "__tls_get_addr_opt") == 0)
4385 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4386 return h;
4389 /* This function satisfies all old ABI object references to ".bar" if a
4390 new ABI object defines "bar". Well, at least, undefined dot symbols
4391 are made weak. This stops later archive searches from including an
4392 object if we already have a function descriptor definition. It also
4393 prevents the linker complaining about undefined symbols.
4394 We also check and correct mismatched symbol visibility here. The
4395 most restrictive visibility of the function descriptor and the
4396 function entry symbol is used. */
4398 static bool
4399 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4401 struct ppc_link_hash_table *htab;
4402 struct ppc_link_hash_entry *fdh;
4404 if (eh->elf.root.type == bfd_link_hash_warning)
4405 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4407 if (eh->elf.root.type == bfd_link_hash_indirect)
4408 return true;
4410 if (eh->elf.root.root.string[0] != '.')
4411 abort ();
4413 htab = ppc_hash_table (info);
4414 if (htab == NULL)
4415 return false;
4417 fdh = lookup_fdh (eh, htab);
4418 if (fdh == NULL
4419 && !bfd_link_relocatable (info)
4420 && (eh->elf.root.type == bfd_link_hash_undefined
4421 || eh->elf.root.type == bfd_link_hash_undefweak)
4422 && eh->elf.ref_regular)
4424 /* Make an undefined function descriptor sym, in order to
4425 pull in an --as-needed shared lib. Archives are handled
4426 elsewhere. */
4427 fdh = make_fdh (info, eh);
4428 if (fdh == NULL)
4429 return false;
4432 if (fdh != NULL)
4434 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4435 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4437 /* Make both descriptor and entry symbol have the most
4438 constraining visibility of either symbol. */
4439 if (entry_vis < descr_vis)
4440 fdh->elf.other += entry_vis - descr_vis;
4441 else if (entry_vis > descr_vis)
4442 eh->elf.other += descr_vis - entry_vis;
4444 /* Propagate reference flags from entry symbol to function
4445 descriptor symbol. */
4446 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4447 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4448 fdh->elf.ref_regular |= eh->elf.ref_regular;
4449 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4451 if (!fdh->elf.forced_local
4452 && fdh->elf.dynindx == -1
4453 && fdh->elf.versioned != versioned_hidden
4454 && (bfd_link_dll (info)
4455 || fdh->elf.def_dynamic
4456 || fdh->elf.ref_dynamic)
4457 && (eh->elf.ref_regular
4458 || eh->elf.def_regular))
4460 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4461 return false;
4465 return true;
4468 /* Set up opd section info and abiversion for IBFD, and process list
4469 of dot-symbols we made in link_hash_newfunc. */
4471 static bool
4472 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4474 struct ppc_link_hash_table *htab;
4475 struct ppc_link_hash_entry **p, *eh;
4476 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4478 if (opd != NULL && opd->size != 0)
4480 if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4481 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4482 else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4483 BFD_FAIL ();
4485 if (abiversion (ibfd) == 0)
4486 set_abiversion (ibfd, 1);
4487 else if (abiversion (ibfd) >= 2)
4489 /* xgettext:c-format */
4490 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4491 ibfd, abiversion (ibfd));
4492 bfd_set_error (bfd_error_bad_value);
4493 return false;
4497 if (is_ppc64_elf (info->output_bfd))
4499 /* For input files without an explicit abiversion in e_flags
4500 we should have flagged any with symbol st_other bits set
4501 as ELFv2 and above flagged those with .opd as ELFv1.
4502 Set the output abiversion if not yet set, and for any input
4503 still ambiguous, take its abiversion from the output.
4504 Differences in ABI are reported later. */
4505 if (abiversion (info->output_bfd) == 0)
4506 set_abiversion (info->output_bfd, abiversion (ibfd));
4507 else if (abiversion (ibfd) == 0)
4508 set_abiversion (ibfd, abiversion (info->output_bfd));
4511 htab = ppc_hash_table (info);
4512 if (htab == NULL)
4513 return true;
4515 if (opd != NULL && opd->size != 0
4516 && (ibfd->flags & DYNAMIC) == 0
4517 && (opd->flags & SEC_RELOC) != 0
4518 && opd->reloc_count != 0
4519 && !bfd_is_abs_section (opd->output_section)
4520 && info->gc_sections)
4522 /* Garbage collection needs some extra help with .opd sections.
4523 We don't want to necessarily keep everything referenced by
4524 relocs in .opd, as that would keep all functions. Instead,
4525 if we reference an .opd symbol (a function descriptor), we
4526 want to keep the function code symbol's section. This is
4527 easy for global symbols, but for local syms we need to keep
4528 information about the associated function section. */
4529 bfd_size_type amt;
4530 asection **opd_sym_map;
4531 Elf_Internal_Shdr *symtab_hdr;
4532 Elf_Internal_Rela *relocs, *rel_end, *rel;
4534 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4535 opd_sym_map = bfd_zalloc (ibfd, amt);
4536 if (opd_sym_map == NULL)
4537 return false;
4538 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4539 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4540 info->keep_memory);
4541 if (relocs == NULL)
4542 return false;
4543 symtab_hdr = &elf_symtab_hdr (ibfd);
4544 rel_end = relocs + opd->reloc_count - 1;
4545 for (rel = relocs; rel < rel_end; rel++)
4547 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4548 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4550 if (r_type == R_PPC64_ADDR64
4551 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4552 && r_symndx < symtab_hdr->sh_info)
4554 Elf_Internal_Sym *isym;
4555 asection *s;
4557 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4558 r_symndx);
4559 if (isym == NULL)
4561 if (elf_section_data (opd)->relocs != relocs)
4562 free (relocs);
4563 return false;
4566 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4567 if (s != NULL && s != opd)
4568 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4571 if (elf_section_data (opd)->relocs != relocs)
4572 free (relocs);
4575 p = &htab->dot_syms;
4576 while ((eh = *p) != NULL)
4578 *p = NULL;
4579 if (&eh->elf == htab->elf.hgot)
4581 else if (htab->elf.hgot == NULL
4582 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4583 htab->elf.hgot = &eh->elf;
4584 else if (abiversion (ibfd) <= 1)
4586 htab->need_func_desc_adj = 1;
4587 if (!add_symbol_adjust (eh, info))
4588 return false;
4590 p = &eh->u.next_dot_sym;
4592 return true;
4595 /* Undo hash table changes when an --as-needed input file is determined
4596 not to be needed. */
4598 static bool
4599 ppc64_elf_notice_as_needed (bfd *ibfd,
4600 struct bfd_link_info *info,
4601 enum notice_asneeded_action act)
4603 if (act == notice_not_needed)
4605 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4607 if (htab == NULL)
4608 return false;
4610 htab->dot_syms = NULL;
4612 return _bfd_elf_notice_as_needed (ibfd, info, act);
4615 /* If --just-symbols against a final linked binary, then assume we need
4616 toc adjusting stubs when calling functions defined there. */
4618 static void
4619 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4621 if ((sec->flags & SEC_CODE) != 0
4622 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4623 && is_ppc64_elf (sec->owner))
4625 if (abiversion (sec->owner) >= 2
4626 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4627 sec->has_toc_reloc = 1;
4629 _bfd_elf_link_just_syms (sec, info);
4632 static struct plt_entry **
4633 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4634 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4636 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4637 struct plt_entry **local_plt;
4638 unsigned char *local_got_tls_masks;
4640 if (local_got_ents == NULL)
4642 bfd_size_type size = symtab_hdr->sh_info;
4644 size *= (sizeof (*local_got_ents)
4645 + sizeof (*local_plt)
4646 + sizeof (*local_got_tls_masks));
4647 local_got_ents = bfd_zalloc (abfd, size);
4648 if (local_got_ents == NULL)
4649 return NULL;
4650 elf_local_got_ents (abfd) = local_got_ents;
4653 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4655 struct got_entry *ent;
4657 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4658 if (ent->addend == r_addend
4659 && ent->owner == abfd
4660 && ent->tls_type == tls_type)
4661 break;
4662 if (ent == NULL)
4664 size_t amt = sizeof (*ent);
4665 ent = bfd_alloc (abfd, amt);
4666 if (ent == NULL)
4667 return false;
4668 ent->next = local_got_ents[r_symndx];
4669 ent->addend = r_addend;
4670 ent->owner = abfd;
4671 ent->tls_type = tls_type;
4672 ent->is_indirect = false;
4673 ent->got.refcount = 0;
4674 local_got_ents[r_symndx] = ent;
4676 ent->got.refcount += 1;
4679 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4680 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4681 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4683 return local_plt + r_symndx;
4686 static bool
4687 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4689 struct plt_entry *ent;
4691 for (ent = *plist; ent != NULL; ent = ent->next)
4692 if (ent->addend == addend)
4693 break;
4694 if (ent == NULL)
4696 size_t amt = sizeof (*ent);
4697 ent = bfd_alloc (abfd, amt);
4698 if (ent == NULL)
4699 return false;
4700 ent->next = *plist;
4701 ent->addend = addend;
4702 ent->plt.refcount = 0;
4703 *plist = ent;
4705 ent->plt.refcount += 1;
4706 return true;
4709 static bool
4710 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4712 return (r_type == R_PPC64_REL24
4713 || r_type == R_PPC64_REL24_NOTOC
4714 || r_type == R_PPC64_REL24_P9NOTOC
4715 || r_type == R_PPC64_REL14
4716 || r_type == R_PPC64_REL14_BRTAKEN
4717 || r_type == R_PPC64_REL14_BRNTAKEN
4718 || r_type == R_PPC64_ADDR24
4719 || r_type == R_PPC64_ADDR14
4720 || r_type == R_PPC64_ADDR14_BRTAKEN
4721 || r_type == R_PPC64_ADDR14_BRNTAKEN
4722 || r_type == R_PPC64_PLTCALL
4723 || r_type == R_PPC64_PLTCALL_NOTOC);
4726 /* Relocs on inline plt call sequence insns prior to the call. */
4728 static bool
4729 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4731 return (r_type == R_PPC64_PLT16_HA
4732 || r_type == R_PPC64_PLT16_HI
4733 || r_type == R_PPC64_PLT16_LO
4734 || r_type == R_PPC64_PLT16_LO_DS
4735 || r_type == R_PPC64_PLT_PCREL34
4736 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4737 || r_type == R_PPC64_PLTSEQ
4738 || r_type == R_PPC64_PLTSEQ_NOTOC);
4741 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4742 relocs, return true for those that operate on a dword. */
4744 static bool
4745 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4747 return (r_type == R_PPC64_PLT_PCREL34
4748 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4749 || r_type == R_PPC64_PLTCALL);
4752 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4753 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4755 static bool
4756 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4758 return offset <= sec->size && size <= sec->size - offset;
4761 /* Look through the relocs for a section during the first phase, and
4762 calculate needed space in the global offset table, procedure
4763 linkage table, and dynamic reloc sections. */
4765 static bool
4766 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4767 asection *sec, const Elf_Internal_Rela *relocs)
4769 struct ppc_link_hash_table *htab;
4770 Elf_Internal_Shdr *symtab_hdr;
4771 struct elf_link_hash_entry **sym_hashes;
4772 const Elf_Internal_Rela *rel;
4773 const Elf_Internal_Rela *rel_end;
4774 asection *sreloc;
4775 struct elf_link_hash_entry *tga, *dottga;
4776 bool is_opd;
4778 if (bfd_link_relocatable (info))
4779 return true;
4781 BFD_ASSERT (is_ppc64_elf (abfd));
4783 htab = ppc_hash_table (info);
4784 if (htab == NULL)
4785 return false;
4787 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4788 false, false, true);
4789 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4790 false, false, true);
4791 symtab_hdr = &elf_symtab_hdr (abfd);
4792 sym_hashes = elf_sym_hashes (abfd);
4793 sreloc = NULL;
4794 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4795 rel_end = relocs + sec->reloc_count;
4796 for (rel = relocs; rel < rel_end; rel++)
4798 unsigned long r_symndx;
4799 struct elf_link_hash_entry *h;
4800 Elf_Internal_Sym *isym;
4801 enum elf_ppc64_reloc_type r_type;
4802 int tls_type;
4803 struct _ppc64_elf_section_data *ppc64_sec;
4804 struct plt_entry **ifunc, **plt_list;
4806 r_symndx = ELF64_R_SYM (rel->r_info);
4807 if (r_symndx < symtab_hdr->sh_info)
4809 h = NULL;
4810 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4811 if (isym == NULL)
4812 return false;
4814 else
4816 isym = NULL;
4817 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4818 h = elf_follow_link (h);
4820 if (h == htab->elf.hgot)
4821 sec->has_toc_reloc = 1;
4824 r_type = ELF64_R_TYPE (rel->r_info);
4825 switch (r_type)
4827 case R_PPC64_D34:
4828 case R_PPC64_D34_LO:
4829 case R_PPC64_D34_HI30:
4830 case R_PPC64_D34_HA30:
4831 case R_PPC64_D28:
4832 case R_PPC64_TPREL34:
4833 case R_PPC64_DTPREL34:
4834 case R_PPC64_PCREL34:
4835 case R_PPC64_GOT_PCREL34:
4836 case R_PPC64_GOT_TLSGD_PCREL34:
4837 case R_PPC64_GOT_TLSLD_PCREL34:
4838 case R_PPC64_GOT_TPREL_PCREL34:
4839 case R_PPC64_GOT_DTPREL_PCREL34:
4840 case R_PPC64_PLT_PCREL34:
4841 case R_PPC64_PLT_PCREL34_NOTOC:
4842 case R_PPC64_PCREL28:
4843 htab->has_power10_relocs = 1;
4844 break;
4845 default:
4846 break;
4849 switch (r_type)
4851 case R_PPC64_PLT16_HA:
4852 case R_PPC64_GOT_TLSLD16_HA:
4853 case R_PPC64_GOT_TLSGD16_HA:
4854 case R_PPC64_GOT_TPREL16_HA:
4855 case R_PPC64_GOT_DTPREL16_HA:
4856 case R_PPC64_GOT16_HA:
4857 case R_PPC64_TOC16_HA:
4858 case R_PPC64_PLT16_LO:
4859 case R_PPC64_PLT16_LO_DS:
4860 case R_PPC64_GOT_TLSLD16_LO:
4861 case R_PPC64_GOT_TLSGD16_LO:
4862 case R_PPC64_GOT_TPREL16_LO_DS:
4863 case R_PPC64_GOT_DTPREL16_LO_DS:
4864 case R_PPC64_GOT16_LO:
4865 case R_PPC64_GOT16_LO_DS:
4866 case R_PPC64_TOC16_LO:
4867 case R_PPC64_TOC16_LO_DS:
4868 case R_PPC64_GOT_PCREL34:
4869 ppc64_elf_tdata (abfd)->has_optrel = 1;
4870 ppc64_elf_section_data (sec)->has_optrel = 1;
4871 break;
4872 default:
4873 break;
4876 ifunc = NULL;
4877 if (h != NULL)
4879 if (h->type == STT_GNU_IFUNC)
4881 h->needs_plt = 1;
4882 ifunc = &h->plt.plist;
4885 else
4887 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4889 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4890 rel->r_addend,
4891 NON_GOT | PLT_IFUNC);
4892 if (ifunc == NULL)
4893 return false;
4897 tls_type = 0;
4898 switch (r_type)
4900 case R_PPC64_TLSGD:
4901 case R_PPC64_TLSLD:
4902 /* These special tls relocs tie a call to __tls_get_addr with
4903 its parameter symbol. */
4904 if (h != NULL)
4905 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4906 else
4907 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4908 rel->r_addend,
4909 NON_GOT | TLS_TLS | TLS_MARK))
4910 return false;
4911 sec->has_tls_reloc = 1;
4912 break;
4914 case R_PPC64_GOT_TLSLD16:
4915 case R_PPC64_GOT_TLSLD16_LO:
4916 case R_PPC64_GOT_TLSLD16_HI:
4917 case R_PPC64_GOT_TLSLD16_HA:
4918 case R_PPC64_GOT_TLSLD_PCREL34:
4919 tls_type = TLS_TLS | TLS_LD;
4920 goto dogottls;
4922 case R_PPC64_GOT_TLSGD16:
4923 case R_PPC64_GOT_TLSGD16_LO:
4924 case R_PPC64_GOT_TLSGD16_HI:
4925 case R_PPC64_GOT_TLSGD16_HA:
4926 case R_PPC64_GOT_TLSGD_PCREL34:
4927 tls_type = TLS_TLS | TLS_GD;
4928 goto dogottls;
4930 case R_PPC64_GOT_TPREL16_DS:
4931 case R_PPC64_GOT_TPREL16_LO_DS:
4932 case R_PPC64_GOT_TPREL16_HI:
4933 case R_PPC64_GOT_TPREL16_HA:
4934 case R_PPC64_GOT_TPREL_PCREL34:
4935 if (bfd_link_dll (info))
4936 info->flags |= DF_STATIC_TLS;
4937 tls_type = TLS_TLS | TLS_TPREL;
4938 goto dogottls;
4940 case R_PPC64_GOT_DTPREL16_DS:
4941 case R_PPC64_GOT_DTPREL16_LO_DS:
4942 case R_PPC64_GOT_DTPREL16_HI:
4943 case R_PPC64_GOT_DTPREL16_HA:
4944 case R_PPC64_GOT_DTPREL_PCREL34:
4945 tls_type = TLS_TLS | TLS_DTPREL;
4946 dogottls:
4947 sec->has_tls_reloc = 1;
4948 goto dogot;
4950 case R_PPC64_GOT16:
4951 case R_PPC64_GOT16_LO:
4952 case R_PPC64_GOT16_HI:
4953 case R_PPC64_GOT16_HA:
4954 case R_PPC64_GOT16_DS:
4955 case R_PPC64_GOT16_LO_DS:
4956 case R_PPC64_GOT_PCREL34:
4957 dogot:
4958 /* This symbol requires a global offset table entry. */
4959 sec->has_toc_reloc = 1;
4960 if (r_type == R_PPC64_GOT_TLSLD16
4961 || r_type == R_PPC64_GOT_TLSGD16
4962 || r_type == R_PPC64_GOT_TPREL16_DS
4963 || r_type == R_PPC64_GOT_DTPREL16_DS
4964 || r_type == R_PPC64_GOT16
4965 || r_type == R_PPC64_GOT16_DS)
4967 htab->do_multi_toc = 1;
4968 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4971 if (ppc64_elf_tdata (abfd)->got == NULL
4972 && !create_got_section (abfd, info))
4973 return false;
4975 if (h != NULL)
4977 struct ppc_link_hash_entry *eh;
4978 struct got_entry *ent;
4980 eh = ppc_elf_hash_entry (h);
4981 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4982 if (ent->addend == rel->r_addend
4983 && ent->owner == abfd
4984 && ent->tls_type == tls_type)
4985 break;
4986 if (ent == NULL)
4988 size_t amt = sizeof (*ent);
4989 ent = bfd_alloc (abfd, amt);
4990 if (ent == NULL)
4991 return false;
4992 ent->next = eh->elf.got.glist;
4993 ent->addend = rel->r_addend;
4994 ent->owner = abfd;
4995 ent->tls_type = tls_type;
4996 ent->is_indirect = false;
4997 ent->got.refcount = 0;
4998 eh->elf.got.glist = ent;
5000 ent->got.refcount += 1;
5001 eh->tls_mask |= tls_type;
5003 else
5004 /* This is a global offset table entry for a local symbol. */
5005 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5006 rel->r_addend, tls_type))
5007 return false;
5008 break;
5010 case R_PPC64_PLT16_HA:
5011 case R_PPC64_PLT16_HI:
5012 case R_PPC64_PLT16_LO:
5013 case R_PPC64_PLT16_LO_DS:
5014 case R_PPC64_PLT_PCREL34:
5015 case R_PPC64_PLT_PCREL34_NOTOC:
5016 case R_PPC64_PLT32:
5017 case R_PPC64_PLT64:
5018 /* This symbol requires a procedure linkage table entry. */
5019 plt_list = ifunc;
5020 if (h != NULL)
5022 h->needs_plt = 1;
5023 if (h->root.root.string[0] == '.'
5024 && h->root.root.string[1] != '\0')
5025 ppc_elf_hash_entry (h)->is_func = 1;
5026 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5027 plt_list = &h->plt.plist;
5029 if (plt_list == NULL)
5030 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5031 rel->r_addend,
5032 NON_GOT | PLT_KEEP);
5033 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5034 return false;
5035 break;
5037 /* The following relocations don't need to propagate the
5038 relocation if linking a shared object since they are
5039 section relative. */
5040 case R_PPC64_SECTOFF:
5041 case R_PPC64_SECTOFF_LO:
5042 case R_PPC64_SECTOFF_HI:
5043 case R_PPC64_SECTOFF_HA:
5044 case R_PPC64_SECTOFF_DS:
5045 case R_PPC64_SECTOFF_LO_DS:
5046 case R_PPC64_DTPREL16:
5047 case R_PPC64_DTPREL16_LO:
5048 case R_PPC64_DTPREL16_HI:
5049 case R_PPC64_DTPREL16_HA:
5050 case R_PPC64_DTPREL16_DS:
5051 case R_PPC64_DTPREL16_LO_DS:
5052 case R_PPC64_DTPREL16_HIGH:
5053 case R_PPC64_DTPREL16_HIGHA:
5054 case R_PPC64_DTPREL16_HIGHER:
5055 case R_PPC64_DTPREL16_HIGHERA:
5056 case R_PPC64_DTPREL16_HIGHEST:
5057 case R_PPC64_DTPREL16_HIGHESTA:
5058 break;
5060 /* Nor do these. */
5061 case R_PPC64_REL16:
5062 case R_PPC64_REL16_LO:
5063 case R_PPC64_REL16_HI:
5064 case R_PPC64_REL16_HA:
5065 case R_PPC64_REL16_HIGH:
5066 case R_PPC64_REL16_HIGHA:
5067 case R_PPC64_REL16_HIGHER:
5068 case R_PPC64_REL16_HIGHERA:
5069 case R_PPC64_REL16_HIGHEST:
5070 case R_PPC64_REL16_HIGHESTA:
5071 case R_PPC64_REL16_HIGHER34:
5072 case R_PPC64_REL16_HIGHERA34:
5073 case R_PPC64_REL16_HIGHEST34:
5074 case R_PPC64_REL16_HIGHESTA34:
5075 case R_PPC64_REL16DX_HA:
5076 break;
5078 /* Not supported as a dynamic relocation. */
5079 case R_PPC64_ADDR64_LOCAL:
5080 if (bfd_link_pic (info))
5082 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5083 ppc_howto_init ();
5084 /* xgettext:c-format */
5085 info->callbacks->einfo (_("%H: %s reloc unsupported "
5086 "in shared libraries and PIEs\n"),
5087 abfd, sec, rel->r_offset,
5088 ppc64_elf_howto_table[r_type]->name);
5089 bfd_set_error (bfd_error_bad_value);
5090 return false;
5092 break;
5094 case R_PPC64_TOC16:
5095 case R_PPC64_TOC16_DS:
5096 htab->do_multi_toc = 1;
5097 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5098 /* Fall through. */
5099 case R_PPC64_TOC16_LO:
5100 case R_PPC64_TOC16_HI:
5101 case R_PPC64_TOC16_HA:
5102 case R_PPC64_TOC16_LO_DS:
5103 sec->has_toc_reloc = 1;
5104 if (h != NULL && bfd_link_executable (info))
5106 /* We may need a copy reloc. */
5107 h->non_got_ref = 1;
5108 /* Strongly prefer a copy reloc over a dynamic reloc.
5109 glibc ld.so as of 2019-08 will error out if one of
5110 these relocations is emitted. */
5111 h->needs_copy = 1;
5112 goto dodyn;
5114 break;
5116 /* Marker reloc. */
5117 case R_PPC64_ENTRY:
5118 break;
5120 /* This relocation describes the C++ object vtable hierarchy.
5121 Reconstruct it for later use during GC. */
5122 case R_PPC64_GNU_VTINHERIT:
5123 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5124 return false;
5125 break;
5127 /* This relocation describes which C++ vtable entries are actually
5128 used. Record for later use during GC. */
5129 case R_PPC64_GNU_VTENTRY:
5130 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5131 return false;
5132 break;
5134 case R_PPC64_REL14:
5135 case R_PPC64_REL14_BRTAKEN:
5136 case R_PPC64_REL14_BRNTAKEN:
5138 asection *dest = NULL;
5140 /* Heuristic: If jumping outside our section, chances are
5141 we are going to need a stub. */
5142 if (h != NULL)
5144 /* If the sym is weak it may be overridden later, so
5145 don't assume we know where a weak sym lives. */
5146 if (h->root.type == bfd_link_hash_defined)
5147 dest = h->root.u.def.section;
5149 else
5150 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5152 if (dest != sec)
5153 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5155 goto rel24;
5157 case R_PPC64_PLTCALL:
5158 case R_PPC64_PLTCALL_NOTOC:
5159 ppc64_elf_section_data (sec)->has_pltcall = 1;
5160 /* Fall through. */
5162 case R_PPC64_REL24:
5163 case R_PPC64_REL24_NOTOC:
5164 case R_PPC64_REL24_P9NOTOC:
5165 rel24:
5166 plt_list = ifunc;
5167 if (h != NULL)
5169 h->needs_plt = 1;
5170 if (h->root.root.string[0] == '.'
5171 && h->root.root.string[1] != '\0')
5172 ppc_elf_hash_entry (h)->is_func = 1;
5174 if (h == tga || h == dottga)
5176 sec->has_tls_reloc = 1;
5177 if (rel != relocs
5178 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5179 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5180 /* We have a new-style __tls_get_addr call with
5181 a marker reloc. */
5183 else
5184 /* Mark this section as having an old-style call. */
5185 sec->nomark_tls_get_addr = 1;
5187 plt_list = &h->plt.plist;
5190 /* We may need a .plt entry if the function this reloc
5191 refers to is in a shared lib. */
5192 if (plt_list
5193 && !update_plt_info (abfd, plt_list, rel->r_addend))
5194 return false;
5195 break;
5197 case R_PPC64_ADDR14:
5198 case R_PPC64_ADDR14_BRNTAKEN:
5199 case R_PPC64_ADDR14_BRTAKEN:
5200 case R_PPC64_ADDR24:
5201 goto dodyn;
5203 case R_PPC64_TPREL64:
5204 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5205 if (bfd_link_dll (info))
5206 info->flags |= DF_STATIC_TLS;
5207 goto dotlstoc;
5209 case R_PPC64_DTPMOD64:
5210 if (rel + 1 < rel_end
5211 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5212 && rel[1].r_offset == rel->r_offset + 8)
5213 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5214 else
5215 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5216 goto dotlstoc;
5218 case R_PPC64_DTPREL64:
5219 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5220 if (rel != relocs
5221 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5222 && rel[-1].r_offset == rel->r_offset - 8)
5223 /* This is the second reloc of a dtpmod, dtprel pair.
5224 Don't mark with TLS_DTPREL. */
5225 goto dodyn;
5227 dotlstoc:
5228 sec->has_tls_reloc = 1;
5229 if (h != NULL)
5230 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5231 else
5232 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5233 rel->r_addend, tls_type))
5234 return false;
5236 ppc64_sec = ppc64_elf_section_data (sec);
5237 if (ppc64_sec->sec_type == sec_normal)
5239 bfd_size_type amt;
5241 /* One extra to simplify get_tls_mask. */
5242 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5243 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5244 if (ppc64_sec->u.toc.symndx == NULL)
5245 return false;
5246 amt = sec->size * sizeof (bfd_vma) / 8;
5247 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5248 if (ppc64_sec->u.toc.add == NULL)
5249 return false;
5250 ppc64_sec->sec_type = sec_toc;
5252 if (ppc64_sec->sec_type != sec_toc
5253 || rel->r_offset % 8 != 0)
5255 info->callbacks->einfo (_("%H: %s reloc unsupported here\n"),
5256 abfd, sec, rel->r_offset,
5257 ppc64_elf_howto_table[r_type]->name);
5258 bfd_set_error (bfd_error_bad_value);
5259 return false;
5261 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5262 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5264 /* Mark the second slot of a GD or LD entry.
5265 -1 to indicate GD and -2 to indicate LD. */
5266 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5267 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5268 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5269 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5270 goto dodyn;
5272 case R_PPC64_TPREL16_HI:
5273 case R_PPC64_TPREL16_HA:
5274 case R_PPC64_TPREL16_HIGH:
5275 case R_PPC64_TPREL16_HIGHA:
5276 case R_PPC64_TPREL16_HIGHER:
5277 case R_PPC64_TPREL16_HIGHERA:
5278 case R_PPC64_TPREL16_HIGHEST:
5279 case R_PPC64_TPREL16_HIGHESTA:
5280 sec->has_tls_reloc = 1;
5281 /* Fall through. */
5282 case R_PPC64_TPREL34:
5283 case R_PPC64_TPREL16:
5284 case R_PPC64_TPREL16_DS:
5285 case R_PPC64_TPREL16_LO:
5286 case R_PPC64_TPREL16_LO_DS:
5287 if (bfd_link_dll (info))
5288 info->flags |= DF_STATIC_TLS;
5289 goto dodyn;
5291 case R_PPC64_ADDR64:
5292 if (is_opd
5293 && rel + 1 < rel_end
5294 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5296 if (h != NULL)
5297 ppc_elf_hash_entry (h)->is_func = 1;
5299 /* Fall through. */
5301 case R_PPC64_ADDR16:
5302 case R_PPC64_ADDR16_DS:
5303 case R_PPC64_ADDR16_HA:
5304 case R_PPC64_ADDR16_HI:
5305 case R_PPC64_ADDR16_HIGH:
5306 case R_PPC64_ADDR16_HIGHA:
5307 case R_PPC64_ADDR16_HIGHER:
5308 case R_PPC64_ADDR16_HIGHERA:
5309 case R_PPC64_ADDR16_HIGHEST:
5310 case R_PPC64_ADDR16_HIGHESTA:
5311 case R_PPC64_ADDR16_LO:
5312 case R_PPC64_ADDR16_LO_DS:
5313 case R_PPC64_D34:
5314 case R_PPC64_D34_LO:
5315 case R_PPC64_D34_HI30:
5316 case R_PPC64_D34_HA30:
5317 case R_PPC64_ADDR16_HIGHER34:
5318 case R_PPC64_ADDR16_HIGHERA34:
5319 case R_PPC64_ADDR16_HIGHEST34:
5320 case R_PPC64_ADDR16_HIGHESTA34:
5321 case R_PPC64_D28:
5322 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5323 && rel->r_addend == 0)
5325 /* We may need a .plt entry if this reloc refers to a
5326 function in a shared lib. */
5327 if (!update_plt_info (abfd, &h->plt.plist, 0))
5328 return false;
5329 h->pointer_equality_needed = 1;
5331 /* Fall through. */
5333 case R_PPC64_REL30:
5334 case R_PPC64_REL32:
5335 case R_PPC64_REL64:
5336 case R_PPC64_ADDR32:
5337 case R_PPC64_UADDR16:
5338 case R_PPC64_UADDR32:
5339 case R_PPC64_UADDR64:
5340 case R_PPC64_TOC:
5341 if (h != NULL && bfd_link_executable (info))
5342 /* We may need a copy reloc. */
5343 h->non_got_ref = 1;
5345 /* Don't propagate .opd relocs. */
5346 if (NO_OPD_RELOCS && is_opd)
5347 break;
5349 /* Set up information for symbols that might need dynamic
5350 relocations. At this point in linking we have read all
5351 the input files and resolved most symbols, but have not
5352 yet decided whether symbols are dynamic or finalized
5353 symbol flags. In some cases we might be setting dynamic
5354 reloc info for symbols that do not end up needing such.
5355 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5356 work together with this code. */
5357 dodyn:
5358 if ((h != NULL
5359 && !SYMBOL_REFERENCES_LOCAL (info, h))
5360 || (bfd_link_pic (info)
5361 && (h != NULL
5362 ? !bfd_is_abs_symbol (&h->root)
5363 : isym->st_shndx != SHN_ABS)
5364 && must_be_dyn_reloc (info, r_type))
5365 || (!bfd_link_pic (info)
5366 && ifunc != NULL))
5368 /* We must copy these reloc types into the output file.
5369 Create a reloc section in dynobj and make room for
5370 this reloc. */
5371 if (sreloc == NULL)
5373 sreloc = _bfd_elf_make_dynamic_reloc_section
5374 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5376 if (sreloc == NULL)
5377 return false;
5380 /* If this is a global symbol, we count the number of
5381 relocations we need for this symbol. */
5382 if (h != NULL)
5384 struct ppc_dyn_relocs *p;
5385 struct ppc_dyn_relocs **head;
5387 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5388 p = *head;
5389 if (p == NULL || p->sec != sec)
5391 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5392 if (p == NULL)
5393 return false;
5394 p->next = *head;
5395 *head = p;
5396 p->sec = sec;
5397 p->count = 0;
5398 p->pc_count = 0;
5399 p->rel_count = 0;
5401 p->count += 1;
5402 if (!must_be_dyn_reloc (info, r_type))
5403 p->pc_count += 1;
5404 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5405 && rel->r_offset % 2 == 0
5406 && sec->alignment_power != 0)
5407 p->rel_count += 1;
5409 else
5411 /* Track dynamic relocs needed for local syms too. */
5412 struct ppc_local_dyn_relocs *p;
5413 struct ppc_local_dyn_relocs **head;
5414 bool is_ifunc;
5415 asection *s;
5416 void *vpp;
5418 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5419 if (s == NULL)
5420 s = sec;
5422 vpp = &elf_section_data (s)->local_dynrel;
5423 head = (struct ppc_local_dyn_relocs **) vpp;
5424 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5425 p = *head;
5426 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5427 p = p->next;
5428 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5430 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5431 if (p == NULL)
5432 return false;
5433 p->next = *head;
5434 *head = p;
5435 p->sec = sec;
5436 p->count = 0;
5437 p->rel_count = 0;
5438 p->ifunc = is_ifunc;
5440 p->count += 1;
5441 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5442 && rel->r_offset % 2 == 0
5443 && sec->alignment_power != 0)
5444 p->rel_count += 1;
5447 break;
5449 default:
5450 break;
5454 return true;
5457 /* Merge backend specific data from an object file to the output
5458 object file when linking. */
5460 static bool
5461 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5463 bfd *obfd = info->output_bfd;
5464 unsigned long iflags, oflags;
5466 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5467 return true;
5469 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5470 return true;
5472 if (!_bfd_generic_verify_endian_match (ibfd, info))
5473 return false;
5475 iflags = elf_elfheader (ibfd)->e_flags;
5476 oflags = elf_elfheader (obfd)->e_flags;
5478 if (iflags & ~EF_PPC64_ABI)
5480 _bfd_error_handler
5481 /* xgettext:c-format */
5482 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5483 bfd_set_error (bfd_error_bad_value);
5484 return false;
5486 else if (iflags != oflags && iflags != 0)
5488 _bfd_error_handler
5489 /* xgettext:c-format */
5490 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5491 ibfd, iflags, oflags);
5492 bfd_set_error (bfd_error_bad_value);
5493 return false;
5496 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5497 return false;
5499 /* Merge Tag_compatibility attributes and any common GNU ones. */
5500 return _bfd_elf_merge_object_attributes (ibfd, info);
5503 static bool
5504 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5506 /* Print normal ELF private data. */
5507 _bfd_elf_print_private_bfd_data (abfd, ptr);
5509 if (elf_elfheader (abfd)->e_flags != 0)
5511 FILE *file = ptr;
5513 fprintf (file, _("private flags = 0x%lx:"),
5514 elf_elfheader (abfd)->e_flags);
5516 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5517 fprintf (file, _(" [abiv%ld]"),
5518 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5519 fputc ('\n', file);
5522 return true;
5525 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5526 of the code entry point, and its section, which must be in the same
5527 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5529 static bfd_vma
5530 opd_entry_value (asection *opd_sec,
5531 bfd_vma offset,
5532 asection **code_sec,
5533 bfd_vma *code_off,
5534 bool in_code_sec)
5536 bfd *opd_bfd = opd_sec->owner;
5537 Elf_Internal_Rela *relocs;
5538 Elf_Internal_Rela *lo, *hi, *look;
5539 bfd_vma val;
5541 if (!is_ppc64_elf (opd_bfd))
5542 return (bfd_vma) -1;
5544 if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5545 ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5546 else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5547 return (bfd_vma) -1;
5549 /* No relocs implies we are linking a --just-symbols object, or looking
5550 at a final linked executable with addr2line or somesuch. */
5551 if (opd_sec->reloc_count == 0)
5553 bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
5555 if (contents == NULL)
5557 if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5558 || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5559 return (bfd_vma) -1;
5560 ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
5563 /* PR 17512: file: 64b9dfbb. */
5564 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5565 return (bfd_vma) -1;
5567 val = bfd_get_64 (opd_bfd, contents + offset);
5568 if (code_sec != NULL)
5570 asection *sec, *likely = NULL;
5572 if (in_code_sec)
5574 sec = *code_sec;
5575 if (sec->vma <= val
5576 && val < sec->vma + sec->size)
5577 likely = sec;
5578 else
5579 val = -1;
5581 else
5582 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5583 if (sec->vma <= val
5584 && (sec->flags & SEC_LOAD) != 0
5585 && (sec->flags & SEC_ALLOC) != 0)
5586 likely = sec;
5587 if (likely != NULL)
5589 *code_sec = likely;
5590 if (code_off != NULL)
5591 *code_off = val - likely->vma;
5594 return val;
5597 relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
5598 if (relocs == NULL)
5599 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5600 /* PR 17512: file: df8e1fd6. */
5601 if (relocs == NULL)
5602 return (bfd_vma) -1;
5604 /* Go find the opd reloc at the sym address. */
5605 lo = relocs;
5606 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5607 val = (bfd_vma) -1;
5608 while (lo < hi)
5610 look = lo + (hi - lo) / 2;
5611 if (look->r_offset < offset)
5612 lo = look + 1;
5613 else if (look->r_offset > offset)
5614 hi = look;
5615 else
5617 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5619 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5620 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5622 unsigned long symndx = ELF64_R_SYM (look->r_info);
5623 asection *sec = NULL;
5625 if (symndx >= symtab_hdr->sh_info
5626 && elf_sym_hashes (opd_bfd) != NULL)
5628 struct elf_link_hash_entry **sym_hashes;
5629 struct elf_link_hash_entry *rh;
5631 sym_hashes = elf_sym_hashes (opd_bfd);
5632 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5633 if (rh != NULL)
5635 rh = elf_follow_link (rh);
5636 if (rh->root.type != bfd_link_hash_defined
5637 && rh->root.type != bfd_link_hash_defweak)
5638 break;
5639 if (rh->root.u.def.section->owner == opd_bfd)
5641 val = rh->root.u.def.value;
5642 sec = rh->root.u.def.section;
5647 if (sec == NULL)
5649 Elf_Internal_Sym *sym;
5651 if (symndx < symtab_hdr->sh_info)
5653 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5654 if (sym == NULL)
5656 size_t symcnt = symtab_hdr->sh_info;
5657 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5658 symcnt, 0,
5659 NULL, NULL, NULL);
5660 if (sym == NULL)
5661 break;
5662 symtab_hdr->contents = (bfd_byte *) sym;
5664 sym += symndx;
5666 else
5668 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5669 1, symndx,
5670 NULL, NULL, NULL);
5671 if (sym == NULL)
5672 break;
5674 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5675 if (sec == NULL)
5676 break;
5677 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5678 val = sym->st_value;
5681 val += look->r_addend;
5682 if (code_off != NULL)
5683 *code_off = val;
5684 if (code_sec != NULL)
5686 if (in_code_sec && *code_sec != sec)
5687 return -1;
5688 else
5689 *code_sec = sec;
5691 if (sec->output_section != NULL)
5692 val += sec->output_section->vma + sec->output_offset;
5694 break;
5698 return val;
5701 /* If the ELF symbol SYM might be a function in SEC, return the
5702 function size and set *CODE_OFF to the function's entry point,
5703 otherwise return zero. */
5705 static bfd_size_type
5706 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5707 bfd_vma *code_off)
5709 bfd_size_type size;
5710 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5712 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5713 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5714 return 0;
5716 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5718 /* In theory we should check that the symbol's type satisfies
5719 _bfd_elf_is_function_type(), but there are some function-like
5720 symbols which would fail this test. (eg _start). Instead
5721 we check for hidden, local, notype symbols with zero size.
5722 This type of symbol is generated by the annobin plugin for gcc
5723 and clang, and should not be considered to be a function symbol. */
5724 if (size == 0
5725 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5726 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5727 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5728 return 0;
5730 if (strcmp (sym->section->name, ".opd") == 0)
5732 struct _opd_sec_data *opd = get_opd_info (sym->section);
5733 bfd_vma symval = sym->value;
5735 if (opd != NULL
5736 && opd->adjust != NULL
5737 && elf_section_data (sym->section)->relocs != NULL)
5739 /* opd_entry_value will use cached relocs that have been
5740 adjusted, but with raw symbols. That means both local
5741 and global symbols need adjusting. */
5742 long adjust = opd->adjust[OPD_NDX (symval)];
5743 if (adjust == -1)
5744 return 0;
5745 symval += adjust;
5748 if (opd_entry_value (sym->section, symval,
5749 &sec, code_off, true) == (bfd_vma) -1)
5750 return 0;
5751 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5752 symbol. This size has nothing to do with the code size of the
5753 function, which is what we're supposed to return, but the
5754 code size isn't available without looking up the dot-sym.
5755 However, doing that would be a waste of time particularly
5756 since elf_find_function will look at the dot-sym anyway.
5757 Now, elf_find_function will keep the largest size of any
5758 function sym found at the code address of interest, so return
5759 1 here to avoid it incorrectly caching a larger function size
5760 for a small function. This does mean we return the wrong
5761 size for a new-ABI function of size 24, but all that does is
5762 disable caching for such functions. */
5763 if (size == 24)
5764 size = 1;
5766 else
5768 if (sym->section != sec)
5769 return 0;
5770 *code_off = sym->value;
5773 /* Do not return 0 for the function's size. */
5774 return size ? size : 1;
5777 /* Return true if symbol is a strong function defined in an ELFv2
5778 object with st_other localentry bits of zero, ie. its local entry
5779 point coincides with its global entry point. */
5781 static bool
5782 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5784 return (h != NULL
5785 && h->type == STT_FUNC
5786 && h->root.type == bfd_link_hash_defined
5787 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5788 && !ppc_elf_hash_entry (h)->non_zero_localentry
5789 && is_ppc64_elf (h->root.u.def.section->owner)
5790 && abiversion (h->root.u.def.section->owner) >= 2);
5793 /* Return true if symbol is defined in a regular object file. */
5795 static bool
5796 is_static_defined (struct elf_link_hash_entry *h)
5798 return ((h->root.type == bfd_link_hash_defined
5799 || h->root.type == bfd_link_hash_defweak)
5800 && h->root.u.def.section != NULL
5801 && h->root.u.def.section->output_section != NULL);
5804 /* If FDH is a function descriptor symbol, return the associated code
5805 entry symbol if it is defined. Return NULL otherwise. */
5807 static struct ppc_link_hash_entry *
5808 defined_code_entry (struct ppc_link_hash_entry *fdh)
5810 if (fdh->is_func_descriptor)
5812 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5813 if (fh->elf.root.type == bfd_link_hash_defined
5814 || fh->elf.root.type == bfd_link_hash_defweak)
5815 return fh;
5817 return NULL;
5820 /* If FH is a function code entry symbol, return the associated
5821 function descriptor symbol if it is defined. Return NULL otherwise. */
5823 static struct ppc_link_hash_entry *
5824 defined_func_desc (struct ppc_link_hash_entry *fh)
5826 if (fh->oh != NULL
5827 && fh->oh->is_func_descriptor)
5829 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5830 if (fdh->elf.root.type == bfd_link_hash_defined
5831 || fdh->elf.root.type == bfd_link_hash_defweak)
5832 return fdh;
5834 return NULL;
5837 /* Given H is a symbol that satisfies is_static_defined, return the
5838 value in the output file. */
5840 static bfd_vma
5841 defined_sym_val (struct elf_link_hash_entry *h)
5843 return (h->root.u.def.section->output_section->vma
5844 + h->root.u.def.section->output_offset
5845 + h->root.u.def.value);
5848 /* Return true if H matches __tls_get_addr or one of its variants. */
5850 static bool
5851 is_tls_get_addr (struct elf_link_hash_entry *h,
5852 struct ppc_link_hash_table *htab)
5854 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5855 || h == elf_hash_entry (htab->tga_desc_fd)
5856 || h == elf_hash_entry (htab->tls_get_addr)
5857 || h == elf_hash_entry (htab->tga_desc));
5860 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5862 /* Garbage collect sections, after first dealing with dot-symbols. */
5864 static bool
5865 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5867 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5869 if (htab != NULL && htab->need_func_desc_adj)
5871 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5872 htab->need_func_desc_adj = 0;
5874 return bfd_elf_gc_sections (abfd, info);
5877 /* Mark all our entry sym sections, both opd and code section. */
5879 static void
5880 ppc64_elf_gc_keep (struct bfd_link_info *info)
5882 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5883 struct bfd_sym_chain *sym;
5885 if (htab == NULL)
5886 return;
5888 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5890 struct ppc_link_hash_entry *eh, *fh;
5891 asection *sec;
5893 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5894 false, false, true));
5895 if (eh == NULL)
5896 continue;
5897 if (eh->elf.root.type != bfd_link_hash_defined
5898 && eh->elf.root.type != bfd_link_hash_defweak)
5899 continue;
5901 fh = defined_code_entry (eh);
5902 if (fh != NULL)
5904 sec = fh->elf.root.u.def.section;
5905 sec->flags |= SEC_KEEP;
5907 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5908 && opd_entry_value (eh->elf.root.u.def.section,
5909 eh->elf.root.u.def.value,
5910 &sec, NULL, false) != (bfd_vma) -1)
5911 sec->flags |= SEC_KEEP;
5913 sec = eh->elf.root.u.def.section;
5914 sec->flags |= SEC_KEEP;
5918 /* Mark sections containing dynamically referenced symbols. When
5919 building shared libraries, we must assume that any visible symbol is
5920 referenced. */
5922 static bool
5923 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5925 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5926 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5927 struct ppc_link_hash_entry *fdh;
5928 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5930 /* Dynamic linking info is on the func descriptor sym. */
5931 fdh = defined_func_desc (eh);
5932 if (fdh != NULL)
5933 eh = fdh;
5935 if ((eh->elf.root.type == bfd_link_hash_defined
5936 || eh->elf.root.type == bfd_link_hash_defweak)
5937 && (!eh->elf.start_stop
5938 || eh->elf.root.ldscript_def
5939 || !info->start_stop_gc)
5940 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5941 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5942 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5943 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5944 && (!bfd_link_executable (info)
5945 || info->gc_keep_exported
5946 || info->export_dynamic
5947 || (eh->elf.dynamic
5948 && d != NULL
5949 && (*d->match) (&d->head, NULL,
5950 eh->elf.root.root.string)))
5951 && (eh->elf.versioned >= versioned
5952 || !bfd_hide_sym_by_version (info->version_info,
5953 eh->elf.root.root.string)))))
5955 asection *code_sec;
5956 struct ppc_link_hash_entry *fh;
5958 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5960 /* Function descriptor syms cause the associated
5961 function code sym section to be marked. */
5962 fh = defined_code_entry (eh);
5963 if (fh != NULL)
5965 code_sec = fh->elf.root.u.def.section;
5966 code_sec->flags |= SEC_KEEP;
5968 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5969 && opd_entry_value (eh->elf.root.u.def.section,
5970 eh->elf.root.u.def.value,
5971 &code_sec, NULL, false) != (bfd_vma) -1)
5972 code_sec->flags |= SEC_KEEP;
5975 return true;
5978 /* Return the section that should be marked against GC for a given
5979 relocation. */
5981 static asection *
5982 ppc64_elf_gc_mark_hook (asection *sec,
5983 struct bfd_link_info *info,
5984 Elf_Internal_Rela *rel,
5985 struct elf_link_hash_entry *h,
5986 Elf_Internal_Sym *sym)
5988 asection *rsec;
5990 /* Syms return NULL if we're marking .opd, so we avoid marking all
5991 function sections, as all functions are referenced in .opd. */
5992 rsec = NULL;
5993 if (get_opd_info (sec) != NULL)
5994 return rsec;
5996 if (h != NULL)
5998 enum elf_ppc64_reloc_type r_type;
5999 struct ppc_link_hash_entry *eh, *fh, *fdh;
6001 r_type = ELF64_R_TYPE (rel->r_info);
6002 switch (r_type)
6004 case R_PPC64_GNU_VTINHERIT:
6005 case R_PPC64_GNU_VTENTRY:
6006 break;
6008 default:
6009 switch (h->root.type)
6011 case bfd_link_hash_defined:
6012 case bfd_link_hash_defweak:
6013 eh = ppc_elf_hash_entry (h);
6014 fdh = defined_func_desc (eh);
6015 if (fdh != NULL)
6017 /* -mcall-aixdesc code references the dot-symbol on
6018 a call reloc. Mark the function descriptor too
6019 against garbage collection. */
6020 fdh->elf.mark = 1;
6021 if (fdh->elf.is_weakalias)
6022 weakdef (&fdh->elf)->mark = 1;
6023 eh = fdh;
6026 /* Function descriptor syms cause the associated
6027 function code sym section to be marked. */
6028 fh = defined_code_entry (eh);
6029 if (fh != NULL)
6031 /* They also mark their opd section. */
6032 eh->elf.root.u.def.section->gc_mark = 1;
6034 rsec = fh->elf.root.u.def.section;
6036 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6037 && opd_entry_value (eh->elf.root.u.def.section,
6038 eh->elf.root.u.def.value,
6039 &rsec, NULL, false) != (bfd_vma) -1)
6040 eh->elf.root.u.def.section->gc_mark = 1;
6041 else
6042 rsec = h->root.u.def.section;
6043 break;
6045 case bfd_link_hash_common:
6046 rsec = h->root.u.c.p->section;
6047 break;
6049 default:
6050 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6054 else
6056 struct _opd_sec_data *opd;
6058 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6059 opd = get_opd_info (rsec);
6060 if (opd != NULL && opd->func_sec != NULL)
6062 rsec->gc_mark = 1;
6064 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6068 return rsec;
6071 /* The maximum size of .sfpr. */
6072 #define SFPR_MAX (218*4)
6074 struct sfpr_def_parms
6076 const char name[12];
6077 unsigned char lo, hi;
6078 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6079 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6082 /* Auto-generate _save*, _rest* functions in .sfpr.
6083 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6084 instead. */
6086 static bool
6087 sfpr_define (struct bfd_link_info *info,
6088 const struct sfpr_def_parms *parm,
6089 asection *stub_sec)
6091 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6092 unsigned int i;
6093 size_t len = strlen (parm->name);
6094 bool writing = false;
6095 char sym[16];
6097 if (htab == NULL)
6098 return false;
6100 memcpy (sym, parm->name, len);
6101 sym[len + 2] = 0;
6103 for (i = parm->lo; i <= parm->hi; i++)
6105 struct ppc_link_hash_entry *h;
6107 sym[len + 0] = i / 10 + '0';
6108 sym[len + 1] = i % 10 + '0';
6109 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6110 writing, true, true));
6111 if (stub_sec != NULL)
6113 if (h != NULL
6114 && h->elf.root.type == bfd_link_hash_defined
6115 && h->elf.root.u.def.section == htab->sfpr)
6117 struct elf_link_hash_entry *s;
6118 char buf[32];
6119 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6120 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6121 if (s == NULL)
6122 return false;
6123 if (s->root.type == bfd_link_hash_new)
6125 s->root.type = bfd_link_hash_defined;
6126 s->root.u.def.section = stub_sec;
6127 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6128 + h->elf.root.u.def.value);
6129 s->ref_regular = 1;
6130 s->def_regular = 1;
6131 s->ref_regular_nonweak = 1;
6132 s->forced_local = 1;
6133 s->non_elf = 0;
6134 s->root.linker_def = 1;
6137 continue;
6139 if (h != NULL)
6141 h->save_res = 1;
6142 if (!h->elf.def_regular)
6144 h->elf.root.type = bfd_link_hash_defined;
6145 h->elf.root.u.def.section = htab->sfpr;
6146 h->elf.root.u.def.value = htab->sfpr->size;
6147 h->elf.type = STT_FUNC;
6148 h->elf.def_regular = 1;
6149 h->elf.non_elf = 0;
6150 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6151 writing = true;
6152 if (htab->sfpr->contents == NULL)
6154 htab->sfpr->contents
6155 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6156 if (htab->sfpr->contents == NULL)
6157 return false;
6161 if (writing)
6163 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6164 if (i != parm->hi)
6165 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6166 else
6167 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6168 htab->sfpr->size = p - htab->sfpr->contents;
6172 return true;
6175 static bfd_byte *
6176 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6178 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6179 return p + 4;
6182 static bfd_byte *
6183 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6185 p = savegpr0 (abfd, p, r);
6186 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6187 p = p + 4;
6188 bfd_put_32 (abfd, BLR, p);
6189 return p + 4;
6192 static bfd_byte *
6193 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6195 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6196 return p + 4;
6199 static bfd_byte *
6200 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6202 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6203 p = p + 4;
6204 p = restgpr0 (abfd, p, r);
6205 bfd_put_32 (abfd, MTLR_R0, p);
6206 p = p + 4;
6207 if (r == 29)
6209 p = restgpr0 (abfd, p, 30);
6210 p = restgpr0 (abfd, p, 31);
6212 bfd_put_32 (abfd, BLR, p);
6213 return p + 4;
6216 static bfd_byte *
6217 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6219 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6220 return p + 4;
6223 static bfd_byte *
6224 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6226 p = savegpr1 (abfd, p, r);
6227 bfd_put_32 (abfd, BLR, p);
6228 return p + 4;
6231 static bfd_byte *
6232 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6234 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6235 return p + 4;
6238 static bfd_byte *
6239 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6241 p = restgpr1 (abfd, p, r);
6242 bfd_put_32 (abfd, BLR, p);
6243 return p + 4;
6246 static bfd_byte *
6247 savefpr (bfd *abfd, bfd_byte *p, int r)
6249 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6250 return p + 4;
6253 static bfd_byte *
6254 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6256 p = savefpr (abfd, p, r);
6257 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6258 p = p + 4;
6259 bfd_put_32 (abfd, BLR, p);
6260 return p + 4;
6263 static bfd_byte *
6264 restfpr (bfd *abfd, bfd_byte *p, int r)
6266 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6267 return p + 4;
6270 static bfd_byte *
6271 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6273 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6274 p = p + 4;
6275 p = restfpr (abfd, p, r);
6276 bfd_put_32 (abfd, MTLR_R0, p);
6277 p = p + 4;
6278 if (r == 29)
6280 p = restfpr (abfd, p, 30);
6281 p = restfpr (abfd, p, 31);
6283 bfd_put_32 (abfd, BLR, p);
6284 return p + 4;
6287 static bfd_byte *
6288 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6290 p = savefpr (abfd, p, r);
6291 bfd_put_32 (abfd, BLR, p);
6292 return p + 4;
6295 static bfd_byte *
6296 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6298 p = restfpr (abfd, p, r);
6299 bfd_put_32 (abfd, BLR, p);
6300 return p + 4;
6303 static bfd_byte *
6304 savevr (bfd *abfd, bfd_byte *p, int r)
6306 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6307 p = p + 4;
6308 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6309 return p + 4;
6312 static bfd_byte *
6313 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6315 p = savevr (abfd, p, r);
6316 bfd_put_32 (abfd, BLR, p);
6317 return p + 4;
6320 static bfd_byte *
6321 restvr (bfd *abfd, bfd_byte *p, int r)
6323 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6324 p = p + 4;
6325 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6326 return p + 4;
6329 static bfd_byte *
6330 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6332 p = restvr (abfd, p, r);
6333 bfd_put_32 (abfd, BLR, p);
6334 return p + 4;
6337 #define STDU_R1_0R1 0xf8210001
6338 #define ADDI_R1_R1 0x38210000
6340 /* Emit prologue of wrapper preserving regs around a call to
6341 __tls_get_addr_opt. */
6343 static bfd_byte *
6344 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6346 unsigned int i;
6348 bfd_put_32 (obfd, MFLR_R0, p);
6349 p += 4;
6350 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6351 p += 4;
6353 if (htab->opd_abi)
6355 for (i = 4; i < 12; i++)
6357 bfd_put_32 (obfd,
6358 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6359 p += 4;
6361 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6362 p += 4;
6364 else
6366 for (i = 4; i < 12; i++)
6368 bfd_put_32 (obfd,
6369 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6370 p += 4;
6372 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6373 p += 4;
6375 return p;
6378 /* Emit epilogue of wrapper preserving regs around a call to
6379 __tls_get_addr_opt. */
6381 static bfd_byte *
6382 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6384 unsigned int i;
6386 if (htab->opd_abi)
6388 for (i = 4; i < 12; i++)
6390 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6391 p += 4;
6393 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6394 p += 4;
6396 else
6398 for (i = 4; i < 12; i++)
6400 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6401 p += 4;
6403 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6404 p += 4;
6406 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6407 p += 4;
6408 bfd_put_32 (obfd, MTLR_R0, p);
6409 p += 4;
6410 bfd_put_32 (obfd, BLR, p);
6411 p += 4;
6412 return p;
6415 /* Called via elf_link_hash_traverse to transfer dynamic linking
6416 information on function code symbol entries to their corresponding
6417 function descriptor symbol entries. Must not be called twice for
6418 any given code symbol. */
6420 static bool
6421 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6423 struct bfd_link_info *info;
6424 struct ppc_link_hash_table *htab;
6425 struct ppc_link_hash_entry *fh;
6426 struct ppc_link_hash_entry *fdh;
6427 bool force_local;
6429 fh = ppc_elf_hash_entry (h);
6430 if (fh->elf.root.type == bfd_link_hash_indirect)
6431 return true;
6433 if (!fh->is_func)
6434 return true;
6436 if (fh->elf.root.root.string[0] != '.'
6437 || fh->elf.root.root.string[1] == '\0')
6438 return true;
6440 info = inf;
6441 htab = ppc_hash_table (info);
6442 if (htab == NULL)
6443 return false;
6445 /* Find the corresponding function descriptor symbol. */
6446 fdh = lookup_fdh (fh, htab);
6448 /* Resolve undefined references to dot-symbols as the value
6449 in the function descriptor, if we have one in a regular object.
6450 This is to satisfy cases like ".quad .foo". Calls to functions
6451 in dynamic objects are handled elsewhere. */
6452 if ((fh->elf.root.type == bfd_link_hash_undefined
6453 || fh->elf.root.type == bfd_link_hash_undefweak)
6454 && (fdh->elf.root.type == bfd_link_hash_defined
6455 || fdh->elf.root.type == bfd_link_hash_defweak)
6456 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6457 && opd_entry_value (fdh->elf.root.u.def.section,
6458 fdh->elf.root.u.def.value,
6459 &fh->elf.root.u.def.section,
6460 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6462 fh->elf.root.type = fdh->elf.root.type;
6463 fh->elf.forced_local = 1;
6464 fh->elf.def_regular = fdh->elf.def_regular;
6465 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6468 if (!fh->elf.dynamic)
6470 struct plt_entry *ent;
6472 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6473 if (ent->plt.refcount > 0)
6474 break;
6475 if (ent == NULL)
6477 if (fdh != NULL && fdh->fake)
6478 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6479 return true;
6483 /* Create a descriptor as undefined if necessary. */
6484 if (fdh == NULL
6485 && !bfd_link_executable (info)
6486 && (fh->elf.root.type == bfd_link_hash_undefined
6487 || fh->elf.root.type == bfd_link_hash_undefweak))
6489 fdh = make_fdh (info, fh);
6490 if (fdh == NULL)
6491 return false;
6494 /* We can't support overriding of symbols on a fake descriptor. */
6495 if (fdh != NULL
6496 && fdh->fake
6497 && (fh->elf.root.type == bfd_link_hash_defined
6498 || fh->elf.root.type == bfd_link_hash_defweak))
6499 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6501 /* Transfer dynamic linking information to the function descriptor. */
6502 if (fdh != NULL)
6504 fdh->elf.ref_regular |= fh->elf.ref_regular;
6505 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6506 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6507 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6508 fdh->elf.dynamic |= fh->elf.dynamic;
6509 fdh->elf.needs_plt |= (fh->elf.needs_plt
6510 || fh->elf.type == STT_FUNC
6511 || fh->elf.type == STT_GNU_IFUNC);
6512 move_plt_plist (fh, fdh);
6514 if (!fdh->elf.forced_local
6515 && fh->elf.dynindx != -1)
6516 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6517 return false;
6520 /* Now that the info is on the function descriptor, clear the
6521 function code sym info. Any function code syms for which we
6522 don't have a definition in a regular file, we force local.
6523 This prevents a shared library from exporting syms that have
6524 been imported from another library. Function code syms that
6525 are really in the library we must leave global to prevent the
6526 linker dragging in a definition from a static library. */
6527 force_local = (!fh->elf.def_regular
6528 || fdh == NULL
6529 || !fdh->elf.def_regular
6530 || fdh->elf.forced_local);
6531 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6533 return true;
6536 static const struct sfpr_def_parms save_res_funcs[] =
6538 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6539 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6540 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6541 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6542 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6543 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6544 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6545 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6546 { "._savef", 14, 31, savefpr, savefpr1_tail },
6547 { "._restf", 14, 31, restfpr, restfpr1_tail },
6548 { "_savevr_", 20, 31, savevr, savevr_tail },
6549 { "_restvr_", 20, 31, restvr, restvr_tail }
6552 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6553 this hook to a) run the edit functions in this file, b) provide
6554 some gcc support functions, and c) transfer dynamic linking
6555 information gathered so far on function code symbol entries, to
6556 their corresponding function descriptor symbol entries. */
6558 static bool
6559 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6561 struct ppc_link_hash_table *htab;
6563 htab = ppc_hash_table (info);
6564 if (htab == NULL)
6565 return false;
6567 /* Call back into the linker, which then runs the edit functions. */
6568 htab->params->edit ();
6570 /* Provide any missing _save* and _rest* functions. */
6571 if (htab->sfpr != NULL)
6573 unsigned int i;
6575 htab->sfpr->size = 0;
6576 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6577 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6578 return false;
6579 if (htab->sfpr->size == 0)
6580 htab->sfpr->flags |= SEC_EXCLUDE;
6583 if (bfd_link_relocatable (info))
6584 return true;
6586 if (htab->elf.hgot != NULL)
6588 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6589 /* Make .TOC. defined so as to prevent it being made dynamic.
6590 The wrong value here is fixed later in ppc64_elf_set_toc. */
6591 if (!htab->elf.hgot->def_regular
6592 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6594 htab->elf.hgot->root.type = bfd_link_hash_defined;
6595 htab->elf.hgot->root.u.def.value = 0;
6596 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6597 htab->elf.hgot->def_regular = 1;
6598 htab->elf.hgot->root.linker_def = 1;
6600 htab->elf.hgot->type = STT_OBJECT;
6601 htab->elf.hgot->other
6602 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6605 return true;
6608 /* Return true if we have dynamic relocs against H or any of its weak
6609 aliases, that apply to read-only sections. Cannot be used after
6610 size_dynamic_sections. */
6612 static bool
6613 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6615 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6618 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6619 return true;
6620 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6622 while (eh != NULL && &eh->elf != h);
6624 return false;
6627 /* Return whether EH has pc-relative dynamic relocs. */
6629 static bool
6630 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6632 struct ppc_dyn_relocs *p;
6634 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6635 if (p->pc_count != 0)
6636 return true;
6637 return false;
6640 /* Return true if a global entry stub will be created for H. Valid
6641 for ELFv2 before plt entries have been allocated. */
6643 static bool
6644 global_entry_stub (struct elf_link_hash_entry *h)
6646 struct plt_entry *pent;
6648 if (!h->pointer_equality_needed
6649 || h->def_regular)
6650 return false;
6652 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6653 if (pent->plt.refcount > 0
6654 && pent->addend == 0)
6655 return true;
6657 return false;
6660 /* Adjust a symbol defined by a dynamic object and referenced by a
6661 regular object. The current definition is in some section of the
6662 dynamic object, but we're not including those sections. We have to
6663 change the definition to something the rest of the link can
6664 understand. */
6666 static bool
6667 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6668 struct elf_link_hash_entry *h)
6670 struct ppc_link_hash_table *htab;
6671 asection *s, *srel;
6673 htab = ppc_hash_table (info);
6674 if (htab == NULL)
6675 return false;
6677 /* Deal with function syms. */
6678 if (h->type == STT_FUNC
6679 || h->type == STT_GNU_IFUNC
6680 || h->needs_plt)
6682 bool local = (ppc_elf_hash_entry (h)->save_res
6683 || SYMBOL_CALLS_LOCAL (info, h)
6684 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6685 /* Discard dyn_relocs when non-pic if we've decided that a
6686 function symbol is local and not an ifunc. We keep dynamic
6687 relocs for ifuncs when local rather than always emitting a
6688 plt call stub for them and defining the symbol on the call
6689 stub. We can't do that for ELFv1 anyway (a function symbol
6690 is defined on a descriptor, not code) and it can be faster at
6691 run-time due to not needing to bounce through a stub. The
6692 dyn_relocs for ifuncs will be applied even in a static
6693 executable. */
6694 if (!bfd_link_pic (info)
6695 && h->type != STT_GNU_IFUNC
6696 && local)
6697 h->dyn_relocs = NULL;
6699 /* Clear procedure linkage table information for any symbol that
6700 won't need a .plt entry. */
6701 struct plt_entry *ent;
6702 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6703 if (ent->plt.refcount > 0)
6704 break;
6705 if (ent == NULL
6706 || (h->type != STT_GNU_IFUNC
6707 && local
6708 && (htab->can_convert_all_inline_plt
6709 || (ppc_elf_hash_entry (h)->tls_mask
6710 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6712 h->plt.plist = NULL;
6713 h->needs_plt = 0;
6714 h->pointer_equality_needed = 0;
6716 else if (abiversion (info->output_bfd) >= 2)
6718 /* Taking a function's address in a read/write section
6719 doesn't require us to define the function symbol in the
6720 executable on a global entry stub. A dynamic reloc can
6721 be used instead. The reason we prefer a few more dynamic
6722 relocs is that calling via a global entry stub costs a
6723 few more instructions, and pointer_equality_needed causes
6724 extra work in ld.so when resolving these symbols. */
6725 if (global_entry_stub (h))
6727 if (!_bfd_elf_readonly_dynrelocs (h))
6729 h->pointer_equality_needed = 0;
6730 /* If we haven't seen a branch reloc and the symbol
6731 isn't an ifunc then we don't need a plt entry. */
6732 if (!h->needs_plt)
6733 h->plt.plist = NULL;
6735 else if (!bfd_link_pic (info))
6736 /* We are going to be defining the function symbol on the
6737 plt stub, so no dyn_relocs needed when non-pic. */
6738 h->dyn_relocs = NULL;
6741 /* ELFv2 function symbols can't have copy relocs. */
6742 return true;
6744 else if (!h->needs_plt
6745 && !_bfd_elf_readonly_dynrelocs (h))
6747 /* If we haven't seen a branch reloc and the symbol isn't an
6748 ifunc then we don't need a plt entry. */
6749 h->plt.plist = NULL;
6750 h->pointer_equality_needed = 0;
6751 return true;
6754 else
6755 h->plt.plist = NULL;
6757 /* If this is a weak symbol, and there is a real definition, the
6758 processor independent code will have arranged for us to see the
6759 real definition first, and we can just use the same value. */
6760 if (h->is_weakalias)
6762 struct elf_link_hash_entry *def = weakdef (h);
6763 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6764 h->root.u.def.section = def->root.u.def.section;
6765 h->root.u.def.value = def->root.u.def.value;
6766 if (def->root.u.def.section == htab->elf.sdynbss
6767 || def->root.u.def.section == htab->elf.sdynrelro)
6768 h->dyn_relocs = NULL;
6769 return true;
6772 /* If we are creating a shared library, we must presume that the
6773 only references to the symbol are via the global offset table.
6774 For such cases we need not do anything here; the relocations will
6775 be handled correctly by relocate_section. */
6776 if (!bfd_link_executable (info))
6777 return true;
6779 /* If there are no references to this symbol that do not use the
6780 GOT, we don't need to generate a copy reloc. */
6781 if (!h->non_got_ref)
6782 return true;
6784 /* Don't generate a copy reloc for symbols defined in the executable. */
6785 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6787 /* If -z nocopyreloc was given, don't generate them either. */
6788 || info->nocopyreloc
6790 /* If we don't find any dynamic relocs in read-only sections, then
6791 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6792 || (ELIMINATE_COPY_RELOCS
6793 && !h->needs_copy
6794 && !alias_readonly_dynrelocs (h))
6796 /* Protected variables do not work with .dynbss. The copy in
6797 .dynbss won't be used by the shared library with the protected
6798 definition for the variable. Text relocations are preferable
6799 to an incorrect program. */
6800 || h->protected_def)
6801 return true;
6803 if (h->type == STT_FUNC
6804 || h->type == STT_GNU_IFUNC)
6806 /* .dynbss copies of function symbols only work if we have
6807 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6808 use dot-symbols and set the function symbol size to the text
6809 size of the function rather than the size of the descriptor.
6810 That's wrong for copying a descriptor. */
6811 if (ppc_elf_hash_entry (h)->oh == NULL
6812 || !(h->size == 24 || h->size == 16))
6813 return true;
6815 /* We should never get here, but unfortunately there are old
6816 versions of gcc (circa gcc-3.2) that improperly for the
6817 ELFv1 ABI put initialized function pointers, vtable refs and
6818 suchlike in read-only sections. Allow them to proceed, but
6819 warn that this might break at runtime. */
6820 info->callbacks->einfo
6821 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6822 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6823 h->root.root.string);
6826 /* This is a reference to a symbol defined by a dynamic object which
6827 is not a function. */
6829 /* We must allocate the symbol in our .dynbss section, which will
6830 become part of the .bss section of the executable. There will be
6831 an entry for this symbol in the .dynsym section. The dynamic
6832 object will contain position independent code, so all references
6833 from the dynamic object to this symbol will go through the global
6834 offset table. The dynamic linker will use the .dynsym entry to
6835 determine the address it must put in the global offset table, so
6836 both the dynamic object and the regular object will refer to the
6837 same memory location for the variable. */
6838 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6840 s = htab->elf.sdynrelro;
6841 srel = htab->elf.sreldynrelro;
6843 else
6845 s = htab->elf.sdynbss;
6846 srel = htab->elf.srelbss;
6848 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6850 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6851 linker to copy the initial value out of the dynamic object
6852 and into the runtime process image. */
6853 srel->size += sizeof (Elf64_External_Rela);
6854 h->needs_copy = 1;
6857 /* We no longer want dyn_relocs. */
6858 h->dyn_relocs = NULL;
6859 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6862 /* If given a function descriptor symbol, hide both the function code
6863 sym and the descriptor. */
6864 static void
6865 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6866 struct elf_link_hash_entry *h,
6867 bool force_local)
6869 struct ppc_link_hash_entry *eh;
6870 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6872 if (ppc_hash_table (info) == NULL)
6873 return;
6875 eh = ppc_elf_hash_entry (h);
6876 if (eh->is_func_descriptor)
6878 struct ppc_link_hash_entry *fh = eh->oh;
6880 if (fh == NULL)
6882 const char *p, *q;
6883 struct elf_link_hash_table *htab = elf_hash_table (info);
6884 char save;
6886 /* We aren't supposed to use alloca in BFD because on
6887 systems which do not have alloca the version in libiberty
6888 calls xmalloc, which might cause the program to crash
6889 when it runs out of memory. This function doesn't have a
6890 return status, so there's no way to gracefully return an
6891 error. So cheat. We know that string[-1] can be safely
6892 accessed; It's either a string in an ELF string table,
6893 or allocated in an objalloc structure. */
6895 p = eh->elf.root.root.string - 1;
6896 save = *p;
6897 *(char *) p = '.';
6898 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6899 false, false));
6900 *(char *) p = save;
6902 /* Unfortunately, if it so happens that the string we were
6903 looking for was allocated immediately before this string,
6904 then we overwrote the string terminator. That's the only
6905 reason the lookup should fail. */
6906 if (fh == NULL)
6908 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6909 while (q >= eh->elf.root.root.string && *q == *p)
6910 --q, --p;
6911 if (q < eh->elf.root.root.string && *p == '.')
6912 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6913 false, false));
6915 if (fh != NULL)
6917 eh->oh = fh;
6918 fh->oh = eh;
6921 if (fh != NULL)
6922 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6926 static bool
6927 get_sym_h (struct elf_link_hash_entry **hp,
6928 Elf_Internal_Sym **symp,
6929 asection **symsecp,
6930 unsigned char **tls_maskp,
6931 Elf_Internal_Sym **locsymsp,
6932 unsigned long r_symndx,
6933 bfd *ibfd)
6935 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6937 if (r_symndx >= symtab_hdr->sh_info)
6939 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6940 struct elf_link_hash_entry *h;
6942 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6943 h = elf_follow_link (h);
6945 if (hp != NULL)
6946 *hp = h;
6948 if (symp != NULL)
6949 *symp = NULL;
6951 if (symsecp != NULL)
6953 asection *symsec = NULL;
6954 if (h->root.type == bfd_link_hash_defined
6955 || h->root.type == bfd_link_hash_defweak)
6956 symsec = h->root.u.def.section;
6957 *symsecp = symsec;
6960 if (tls_maskp != NULL)
6961 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6963 else
6965 Elf_Internal_Sym *sym;
6966 Elf_Internal_Sym *locsyms = *locsymsp;
6968 if (locsyms == NULL)
6970 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6971 if (locsyms == NULL)
6972 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6973 symtab_hdr->sh_info,
6974 0, NULL, NULL, NULL);
6975 if (locsyms == NULL)
6976 return false;
6977 *locsymsp = locsyms;
6979 sym = locsyms + r_symndx;
6981 if (hp != NULL)
6982 *hp = NULL;
6984 if (symp != NULL)
6985 *symp = sym;
6987 if (symsecp != NULL)
6988 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6990 if (tls_maskp != NULL)
6992 struct got_entry **lgot_ents;
6993 unsigned char *tls_mask;
6995 tls_mask = NULL;
6996 lgot_ents = elf_local_got_ents (ibfd);
6997 if (lgot_ents != NULL)
6999 struct plt_entry **local_plt = (struct plt_entry **)
7000 (lgot_ents + symtab_hdr->sh_info);
7001 unsigned char *lgot_masks = (unsigned char *)
7002 (local_plt + symtab_hdr->sh_info);
7003 tls_mask = &lgot_masks[r_symndx];
7005 *tls_maskp = tls_mask;
7008 return true;
7011 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7012 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7013 type suitable for optimization, and 1 otherwise. */
7015 static int
7016 get_tls_mask (unsigned char **tls_maskp,
7017 unsigned long *toc_symndx,
7018 bfd_vma *toc_addend,
7019 Elf_Internal_Sym **locsymsp,
7020 const Elf_Internal_Rela *rel,
7021 bfd *ibfd)
7023 unsigned long r_symndx;
7024 int next_r;
7025 struct elf_link_hash_entry *h;
7026 Elf_Internal_Sym *sym;
7027 asection *sec;
7028 bfd_vma off;
7030 r_symndx = ELF64_R_SYM (rel->r_info);
7031 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7032 return 0;
7034 if ((*tls_maskp != NULL
7035 && (**tls_maskp & TLS_TLS) != 0
7036 && **tls_maskp != (TLS_TLS | TLS_MARK))
7037 || sec == NULL
7038 || ppc64_elf_section_data (sec) == NULL
7039 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7040 return 1;
7042 /* Look inside a TOC section too. */
7043 if (h != NULL)
7045 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7046 off = h->root.u.def.value;
7048 else
7049 off = sym->st_value;
7050 off += rel->r_addend;
7051 BFD_ASSERT (off % 8 == 0);
7052 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7053 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7054 if (toc_symndx != NULL)
7055 *toc_symndx = r_symndx;
7056 if (toc_addend != NULL)
7057 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7058 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7059 return 0;
7060 if ((h == NULL || is_static_defined (h))
7061 && (next_r == -1 || next_r == -2))
7062 return 1 - next_r;
7063 return 1;
7066 /* Find (or create) an entry in the tocsave hash table. */
7068 static struct tocsave_entry *
7069 tocsave_find (struct ppc_link_hash_table *htab,
7070 enum insert_option insert,
7071 Elf_Internal_Sym **local_syms,
7072 const Elf_Internal_Rela *irela,
7073 bfd *ibfd)
7075 unsigned long r_indx;
7076 struct elf_link_hash_entry *h;
7077 Elf_Internal_Sym *sym;
7078 struct tocsave_entry ent, *p;
7079 hashval_t hash;
7080 struct tocsave_entry **slot;
7082 r_indx = ELF64_R_SYM (irela->r_info);
7083 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7084 return NULL;
7085 if (ent.sec == NULL || ent.sec->output_section == NULL)
7087 _bfd_error_handler
7088 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7089 return NULL;
7092 if (h != NULL)
7093 ent.offset = h->root.u.def.value;
7094 else
7095 ent.offset = sym->st_value;
7096 ent.offset += irela->r_addend;
7098 hash = tocsave_htab_hash (&ent);
7099 slot = ((struct tocsave_entry **)
7100 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7101 if (slot == NULL)
7102 return NULL;
7104 if (*slot == NULL)
7106 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7107 if (p == NULL)
7108 return NULL;
7109 *p = ent;
7110 *slot = p;
7112 return *slot;
7115 /* Adjust all global syms defined in opd sections. In gcc generated
7116 code for the old ABI, these will already have been done. */
7118 static bool
7119 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7121 struct ppc_link_hash_entry *eh;
7122 asection *sym_sec;
7123 struct _opd_sec_data *opd;
7125 if (h->root.type == bfd_link_hash_indirect)
7126 return true;
7128 if (h->root.type != bfd_link_hash_defined
7129 && h->root.type != bfd_link_hash_defweak)
7130 return true;
7132 eh = ppc_elf_hash_entry (h);
7133 if (eh->adjust_done)
7134 return true;
7136 sym_sec = eh->elf.root.u.def.section;
7137 opd = get_opd_info (sym_sec);
7138 if (opd != NULL && opd->adjust != NULL)
7140 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7141 if (adjust == -1)
7143 /* This entry has been deleted. */
7144 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7145 if (dsec == NULL)
7147 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7148 if (discarded_section (dsec))
7150 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7151 break;
7154 eh->elf.root.u.def.value = 0;
7155 eh->elf.root.u.def.section = dsec;
7157 else
7158 eh->elf.root.u.def.value += adjust;
7159 eh->adjust_done = 1;
7161 return true;
7164 /* Handles decrementing dynamic reloc counts for the reloc specified by
7165 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7166 have already been determined. */
7168 static bool
7169 dec_dynrel_count (const Elf_Internal_Rela *rel,
7170 asection *sec,
7171 struct bfd_link_info *info,
7172 Elf_Internal_Sym **local_syms,
7173 struct elf_link_hash_entry *h,
7174 Elf_Internal_Sym *sym)
7176 enum elf_ppc64_reloc_type r_type;
7177 asection *sym_sec = NULL;
7179 /* Can this reloc be dynamic? This switch, and later tests here
7180 should be kept in sync with the code in check_relocs. */
7181 r_type = ELF64_R_TYPE (rel->r_info);
7182 switch (r_type)
7184 default:
7185 return true;
7187 case R_PPC64_TOC16:
7188 case R_PPC64_TOC16_DS:
7189 case R_PPC64_TOC16_LO:
7190 case R_PPC64_TOC16_HI:
7191 case R_PPC64_TOC16_HA:
7192 case R_PPC64_TOC16_LO_DS:
7193 if (h == NULL)
7194 return true;
7195 break;
7197 case R_PPC64_TPREL16:
7198 case R_PPC64_TPREL16_LO:
7199 case R_PPC64_TPREL16_HI:
7200 case R_PPC64_TPREL16_HA:
7201 case R_PPC64_TPREL16_DS:
7202 case R_PPC64_TPREL16_LO_DS:
7203 case R_PPC64_TPREL16_HIGH:
7204 case R_PPC64_TPREL16_HIGHA:
7205 case R_PPC64_TPREL16_HIGHER:
7206 case R_PPC64_TPREL16_HIGHERA:
7207 case R_PPC64_TPREL16_HIGHEST:
7208 case R_PPC64_TPREL16_HIGHESTA:
7209 case R_PPC64_TPREL64:
7210 case R_PPC64_TPREL34:
7211 case R_PPC64_DTPMOD64:
7212 case R_PPC64_DTPREL64:
7213 case R_PPC64_ADDR64:
7214 case R_PPC64_REL30:
7215 case R_PPC64_REL32:
7216 case R_PPC64_REL64:
7217 case R_PPC64_ADDR14:
7218 case R_PPC64_ADDR14_BRNTAKEN:
7219 case R_PPC64_ADDR14_BRTAKEN:
7220 case R_PPC64_ADDR16:
7221 case R_PPC64_ADDR16_DS:
7222 case R_PPC64_ADDR16_HA:
7223 case R_PPC64_ADDR16_HI:
7224 case R_PPC64_ADDR16_HIGH:
7225 case R_PPC64_ADDR16_HIGHA:
7226 case R_PPC64_ADDR16_HIGHER:
7227 case R_PPC64_ADDR16_HIGHERA:
7228 case R_PPC64_ADDR16_HIGHEST:
7229 case R_PPC64_ADDR16_HIGHESTA:
7230 case R_PPC64_ADDR16_LO:
7231 case R_PPC64_ADDR16_LO_DS:
7232 case R_PPC64_ADDR24:
7233 case R_PPC64_ADDR32:
7234 case R_PPC64_UADDR16:
7235 case R_PPC64_UADDR32:
7236 case R_PPC64_UADDR64:
7237 case R_PPC64_TOC:
7238 case R_PPC64_D34:
7239 case R_PPC64_D34_LO:
7240 case R_PPC64_D34_HI30:
7241 case R_PPC64_D34_HA30:
7242 case R_PPC64_ADDR16_HIGHER34:
7243 case R_PPC64_ADDR16_HIGHERA34:
7244 case R_PPC64_ADDR16_HIGHEST34:
7245 case R_PPC64_ADDR16_HIGHESTA34:
7246 case R_PPC64_D28:
7247 break;
7250 if (local_syms != NULL)
7252 unsigned long r_symndx;
7253 bfd *ibfd = sec->owner;
7255 r_symndx = ELF64_R_SYM (rel->r_info);
7256 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7257 return false;
7260 if ((h != NULL
7261 && !SYMBOL_REFERENCES_LOCAL (info, h))
7262 || (bfd_link_pic (info)
7263 && (h != NULL
7264 ? !bfd_is_abs_symbol (&h->root)
7265 : sym_sec != bfd_abs_section_ptr)
7266 && must_be_dyn_reloc (info, r_type))
7267 || (!bfd_link_pic (info)
7268 && (h != NULL
7269 ? h->type == STT_GNU_IFUNC
7270 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7272 else
7273 return true;
7275 if (h != NULL)
7277 struct ppc_dyn_relocs *p;
7278 struct ppc_dyn_relocs **pp;
7279 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7281 /* elf_gc_sweep may have already removed all dyn relocs associated
7282 with local syms for a given section. Also, symbol flags are
7283 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7284 report a dynreloc miscount. */
7285 if (*pp == NULL && info->gc_sections)
7286 return true;
7288 while ((p = *pp) != NULL)
7290 if (p->sec == sec)
7292 if (!must_be_dyn_reloc (info, r_type))
7293 p->pc_count -= 1;
7294 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7295 && rel->r_offset % 2 == 0
7296 && sec->alignment_power != 0)
7297 p->rel_count -= 1;
7298 p->count -= 1;
7299 if (p->count == 0)
7300 *pp = p->next;
7301 return true;
7303 pp = &p->next;
7306 else
7308 struct ppc_local_dyn_relocs *p;
7309 struct ppc_local_dyn_relocs **pp;
7310 void *vpp;
7311 bool is_ifunc;
7313 if (local_syms == NULL)
7314 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7315 if (sym_sec == NULL)
7316 sym_sec = sec;
7318 vpp = &elf_section_data (sym_sec)->local_dynrel;
7319 pp = (struct ppc_local_dyn_relocs **) vpp;
7321 if (*pp == NULL && info->gc_sections)
7322 return true;
7324 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7325 while ((p = *pp) != NULL)
7327 if (p->sec == sec && p->ifunc == is_ifunc)
7329 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7330 && rel->r_offset % 2 == 0
7331 && sec->alignment_power != 0)
7332 p->rel_count -= 1;
7333 p->count -= 1;
7334 if (p->count == 0)
7335 *pp = p->next;
7336 return true;
7338 pp = &p->next;
7342 /* xgettext:c-format */
7343 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7344 sec->owner, sec);
7345 bfd_set_error (bfd_error_bad_value);
7346 return false;
7349 /* Remove unused Official Procedure Descriptor entries. Currently we
7350 only remove those associated with functions in discarded link-once
7351 sections, or weakly defined functions that have been overridden. It
7352 would be possible to remove many more entries for statically linked
7353 applications. */
7355 bool
7356 ppc64_elf_edit_opd (struct bfd_link_info *info)
7358 bfd *ibfd;
7359 bool some_edited = false;
7360 asection *need_pad = NULL;
7361 struct ppc_link_hash_table *htab;
7363 htab = ppc_hash_table (info);
7364 if (htab == NULL)
7365 return false;
7367 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7369 asection *sec;
7370 Elf_Internal_Rela *relstart, *rel, *relend;
7371 Elf_Internal_Shdr *symtab_hdr;
7372 Elf_Internal_Sym *local_syms;
7373 struct _opd_sec_data *opd;
7374 bool need_edit, add_aux_fields, broken;
7375 bfd_size_type cnt_16b = 0;
7377 if (!is_ppc64_elf (ibfd))
7378 continue;
7380 sec = bfd_get_section_by_name (ibfd, ".opd");
7381 if (sec == NULL
7382 || sec->size == 0
7383 || (sec->flags & SEC_HAS_CONTENTS) == 0)
7384 continue;
7386 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7387 continue;
7389 if (sec->output_section == bfd_abs_section_ptr)
7390 continue;
7392 /* Look through the section relocs. */
7393 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7394 continue;
7396 local_syms = NULL;
7397 symtab_hdr = &elf_symtab_hdr (ibfd);
7399 /* Read the relocations. */
7400 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7401 info->keep_memory);
7402 if (relstart == NULL)
7403 return false;
7405 /* First run through the relocs to check they are sane, and to
7406 determine whether we need to edit this opd section. */
7407 need_edit = false;
7408 broken = false;
7409 need_pad = sec;
7410 relend = relstart + sec->reloc_count;
7411 for (rel = relstart; rel < relend; )
7413 enum elf_ppc64_reloc_type r_type;
7414 unsigned long r_symndx;
7415 asection *sym_sec;
7416 struct elf_link_hash_entry *h;
7417 Elf_Internal_Sym *sym;
7418 bfd_vma offset;
7420 /* .opd contains an array of 16 or 24 byte entries. We're
7421 only interested in the reloc pointing to a function entry
7422 point. */
7423 offset = rel->r_offset;
7424 if (rel + 1 == relend
7425 || rel[1].r_offset != offset + 8)
7427 /* If someone messes with .opd alignment then after a
7428 "ld -r" we might have padding in the middle of .opd.
7429 Also, there's nothing to prevent someone putting
7430 something silly in .opd with the assembler. No .opd
7431 optimization for them! */
7432 broken_opd:
7433 _bfd_error_handler
7434 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7435 broken = true;
7436 break;
7439 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7440 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7442 _bfd_error_handler
7443 /* xgettext:c-format */
7444 (_("%pB: unexpected reloc type %u in .opd section"),
7445 ibfd, r_type);
7446 broken = true;
7447 break;
7450 r_symndx = ELF64_R_SYM (rel->r_info);
7451 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7452 r_symndx, ibfd))
7453 goto error_ret;
7455 if (sym_sec == NULL || sym_sec->owner == NULL)
7457 const char *sym_name;
7458 if (h != NULL)
7459 sym_name = h->root.root.string;
7460 else
7461 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7462 sym_sec);
7464 _bfd_error_handler
7465 /* xgettext:c-format */
7466 (_("%pB: undefined sym `%s' in .opd section"),
7467 ibfd, sym_name);
7468 broken = true;
7469 break;
7472 /* opd entries are always for functions defined in the
7473 current input bfd. If the symbol isn't defined in the
7474 input bfd, then we won't be using the function in this
7475 bfd; It must be defined in a linkonce section in another
7476 bfd, or is weak. It's also possible that we are
7477 discarding the function due to a linker script /DISCARD/,
7478 which we test for via the output_section. */
7479 if (sym_sec->owner != ibfd
7480 || sym_sec->output_section == bfd_abs_section_ptr)
7481 need_edit = true;
7483 rel += 2;
7484 if (rel + 1 == relend
7485 || (rel + 2 < relend
7486 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7487 ++rel;
7489 if (rel == relend)
7491 if (sec->size == offset + 24)
7493 need_pad = NULL;
7494 break;
7496 if (sec->size == offset + 16)
7498 cnt_16b++;
7499 break;
7501 goto broken_opd;
7503 else if (rel + 1 < relend
7504 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7505 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7507 if (rel[0].r_offset == offset + 16)
7508 cnt_16b++;
7509 else if (rel[0].r_offset != offset + 24)
7510 goto broken_opd;
7512 else
7513 goto broken_opd;
7516 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7518 if (!broken && (need_edit || add_aux_fields))
7520 Elf_Internal_Rela *write_rel;
7521 Elf_Internal_Shdr *rel_hdr;
7522 bfd_byte *rptr, *wptr;
7523 bfd_byte *new_contents;
7524 bfd_size_type amt;
7526 new_contents = NULL;
7527 amt = OPD_NDX (sec->size) * sizeof (long);
7528 opd = &ppc64_elf_section_data (sec)->u.opd;
7529 opd->adjust = bfd_zalloc (sec->owner, amt);
7530 if (opd->adjust == NULL)
7531 return false;
7533 /* This seems a waste of time as input .opd sections are all
7534 zeros as generated by gcc, but I suppose there's no reason
7535 this will always be so. We might start putting something in
7536 the third word of .opd entries. */
7537 if ((sec->flags & SEC_IN_MEMORY) == 0)
7539 bfd_byte *loc;
7540 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7542 free (loc);
7543 error_ret:
7544 if (symtab_hdr->contents != (unsigned char *) local_syms)
7545 free (local_syms);
7546 if (elf_section_data (sec)->relocs != relstart)
7547 free (relstart);
7548 return false;
7550 sec->contents = loc;
7551 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7554 elf_section_data (sec)->relocs = relstart;
7556 new_contents = sec->contents;
7557 if (add_aux_fields)
7559 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7560 if (new_contents == NULL)
7561 return false;
7562 need_pad = NULL;
7564 wptr = new_contents;
7565 rptr = sec->contents;
7566 write_rel = relstart;
7567 for (rel = relstart; rel < relend; )
7569 unsigned long r_symndx;
7570 asection *sym_sec;
7571 struct elf_link_hash_entry *h;
7572 struct ppc_link_hash_entry *fdh = NULL;
7573 Elf_Internal_Sym *sym;
7574 long opd_ent_size;
7575 Elf_Internal_Rela *next_rel;
7576 bool skip;
7578 r_symndx = ELF64_R_SYM (rel->r_info);
7579 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7580 r_symndx, ibfd))
7581 goto error_ret;
7583 next_rel = rel + 2;
7584 if (next_rel + 1 == relend
7585 || (next_rel + 2 < relend
7586 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7587 ++next_rel;
7589 /* See if the .opd entry is full 24 byte or
7590 16 byte (with fd_aux entry overlapped with next
7591 fd_func). */
7592 opd_ent_size = 24;
7593 if (next_rel == relend)
7595 if (sec->size == rel->r_offset + 16)
7596 opd_ent_size = 16;
7598 else if (next_rel->r_offset == rel->r_offset + 16)
7599 opd_ent_size = 16;
7601 if (h != NULL
7602 && h->root.root.string[0] == '.')
7604 fdh = ppc_elf_hash_entry (h)->oh;
7605 if (fdh != NULL)
7607 fdh = ppc_follow_link (fdh);
7608 if (fdh->elf.root.type != bfd_link_hash_defined
7609 && fdh->elf.root.type != bfd_link_hash_defweak)
7610 fdh = NULL;
7614 skip = (sym_sec->owner != ibfd
7615 || sym_sec->output_section == bfd_abs_section_ptr);
7616 if (skip)
7618 if (fdh != NULL && sym_sec->owner == ibfd)
7620 /* Arrange for the function descriptor sym
7621 to be dropped. */
7622 fdh->elf.root.u.def.value = 0;
7623 fdh->elf.root.u.def.section = sym_sec;
7625 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7627 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7628 rel = next_rel;
7629 else
7630 while (1)
7632 if (!dec_dynrel_count (rel, sec, info,
7633 NULL, h, sym))
7634 goto error_ret;
7636 if (++rel == next_rel)
7637 break;
7639 r_symndx = ELF64_R_SYM (rel->r_info);
7640 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7641 r_symndx, ibfd))
7642 goto error_ret;
7645 else
7647 /* We'll be keeping this opd entry. */
7648 long adjust;
7650 if (fdh != NULL)
7652 /* Redefine the function descriptor symbol to
7653 this location in the opd section. It is
7654 necessary to update the value here rather
7655 than using an array of adjustments as we do
7656 for local symbols, because various places
7657 in the generic ELF code use the value
7658 stored in u.def.value. */
7659 fdh->elf.root.u.def.value = wptr - new_contents;
7660 fdh->adjust_done = 1;
7663 /* Local syms are a bit tricky. We could
7664 tweak them as they can be cached, but
7665 we'd need to look through the local syms
7666 for the function descriptor sym which we
7667 don't have at the moment. So keep an
7668 array of adjustments. */
7669 adjust = (wptr - new_contents) - (rptr - sec->contents);
7670 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7672 if (wptr != rptr)
7673 memcpy (wptr, rptr, opd_ent_size);
7674 wptr += opd_ent_size;
7675 if (add_aux_fields && opd_ent_size == 16)
7677 memset (wptr, '\0', 8);
7678 wptr += 8;
7681 /* We need to adjust any reloc offsets to point to the
7682 new opd entries. */
7683 for ( ; rel != next_rel; ++rel)
7685 rel->r_offset += adjust;
7686 if (write_rel != rel)
7687 memcpy (write_rel, rel, sizeof (*rel));
7688 ++write_rel;
7692 rptr += opd_ent_size;
7695 sec->size = wptr - new_contents;
7696 sec->reloc_count = write_rel - relstart;
7697 if (add_aux_fields)
7699 free (sec->contents);
7700 sec->contents = new_contents;
7703 /* Fudge the header size too, as this is used later in
7704 elf_bfd_final_link if we are emitting relocs. */
7705 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7706 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7707 some_edited = true;
7709 else if (elf_section_data (sec)->relocs != relstart)
7710 free (relstart);
7712 if (local_syms != NULL
7713 && symtab_hdr->contents != (unsigned char *) local_syms)
7715 if (!info->keep_memory)
7716 free (local_syms);
7717 else
7718 symtab_hdr->contents = (unsigned char *) local_syms;
7722 if (some_edited)
7723 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7725 /* If we are doing a final link and the last .opd entry is just 16 byte
7726 long, add a 8 byte padding after it. */
7727 if (need_pad != NULL && !bfd_link_relocatable (info))
7729 bfd_byte *p;
7731 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7733 BFD_ASSERT (need_pad->size > 0);
7735 p = bfd_malloc (need_pad->size + 8);
7736 if (p == NULL)
7737 return false;
7739 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7740 p, 0, need_pad->size))
7741 return false;
7743 need_pad->contents = p;
7744 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7746 else
7748 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7749 if (p == NULL)
7750 return false;
7752 need_pad->contents = p;
7755 memset (need_pad->contents + need_pad->size, 0, 8);
7756 need_pad->size += 8;
7759 return true;
7762 /* Analyze inline PLT call relocations to see whether calls to locally
7763 defined functions can be converted to direct calls. */
7765 bool
7766 ppc64_elf_inline_plt (struct bfd_link_info *info)
7768 struct ppc_link_hash_table *htab;
7769 bfd *ibfd;
7770 asection *sec;
7771 bfd_vma low_vma, high_vma, limit;
7773 htab = ppc_hash_table (info);
7774 if (htab == NULL)
7775 return false;
7777 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7778 reduced somewhat to cater for possible stubs that might be added
7779 between the call and its destination. */
7780 if (htab->params->group_size < 0)
7782 limit = -htab->params->group_size;
7783 if (limit == 1)
7784 limit = 0x1e00000;
7786 else
7788 limit = htab->params->group_size;
7789 if (limit == 1)
7790 limit = 0x1c00000;
7793 low_vma = -1;
7794 high_vma = 0;
7795 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7796 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7798 if (low_vma > sec->vma)
7799 low_vma = sec->vma;
7800 if (high_vma < sec->vma + sec->size)
7801 high_vma = sec->vma + sec->size;
7804 /* If a "bl" can reach anywhere in local code sections, then we can
7805 convert all inline PLT sequences to direct calls when the symbol
7806 is local. */
7807 if (high_vma - low_vma < limit)
7809 htab->can_convert_all_inline_plt = 1;
7810 return true;
7813 /* Otherwise, go looking through relocs for cases where a direct
7814 call won't reach. Mark the symbol on any such reloc to disable
7815 the optimization and keep the PLT entry as it seems likely that
7816 this will be better than creating trampolines. Note that this
7817 will disable the optimization for all inline PLT calls to a
7818 particular symbol, not just those that won't reach. The
7819 difficulty in doing a more precise optimization is that the
7820 linker needs to make a decision depending on whether a
7821 particular R_PPC64_PLTCALL insn can be turned into a direct
7822 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7823 the sequence, and there is nothing that ties those relocs
7824 together except their symbol. */
7826 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7828 Elf_Internal_Shdr *symtab_hdr;
7829 Elf_Internal_Sym *local_syms;
7831 if (!is_ppc64_elf (ibfd))
7832 continue;
7834 local_syms = NULL;
7835 symtab_hdr = &elf_symtab_hdr (ibfd);
7837 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7838 if (ppc64_elf_section_data (sec)->has_pltcall
7839 && !bfd_is_abs_section (sec->output_section))
7841 Elf_Internal_Rela *relstart, *rel, *relend;
7843 /* Read the relocations. */
7844 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7845 info->keep_memory);
7846 if (relstart == NULL)
7847 return false;
7849 relend = relstart + sec->reloc_count;
7850 for (rel = relstart; rel < relend; rel++)
7852 enum elf_ppc64_reloc_type r_type;
7853 unsigned long r_symndx;
7854 asection *sym_sec;
7855 struct elf_link_hash_entry *h;
7856 Elf_Internal_Sym *sym;
7857 unsigned char *tls_maskp;
7859 r_type = ELF64_R_TYPE (rel->r_info);
7860 if (r_type != R_PPC64_PLTCALL
7861 && r_type != R_PPC64_PLTCALL_NOTOC)
7862 continue;
7864 r_symndx = ELF64_R_SYM (rel->r_info);
7865 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7866 r_symndx, ibfd))
7868 if (elf_section_data (sec)->relocs != relstart)
7869 free (relstart);
7870 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7871 free (local_syms);
7872 return false;
7875 if (sym_sec != NULL && sym_sec->output_section != NULL)
7877 bfd_vma from, to;
7878 if (h != NULL)
7879 to = h->root.u.def.value;
7880 else
7881 to = sym->st_value;
7882 to += (rel->r_addend
7883 + sym_sec->output_offset
7884 + sym_sec->output_section->vma);
7885 from = (rel->r_offset
7886 + sec->output_offset
7887 + sec->output_section->vma);
7888 if (to - from + limit < 2 * limit
7889 && !(r_type == R_PPC64_PLTCALL_NOTOC
7890 && (((h ? h->other : sym->st_other)
7891 & STO_PPC64_LOCAL_MASK)
7892 > 1 << STO_PPC64_LOCAL_BIT)))
7893 *tls_maskp &= ~PLT_KEEP;
7896 if (elf_section_data (sec)->relocs != relstart)
7897 free (relstart);
7900 if (local_syms != NULL
7901 && symtab_hdr->contents != (unsigned char *) local_syms)
7903 if (!info->keep_memory)
7904 free (local_syms);
7905 else
7906 symtab_hdr->contents = (unsigned char *) local_syms;
7910 return true;
7913 /* Set htab->tls_get_addr and various other info specific to TLS.
7914 This needs to run before dynamic symbols are processed in
7915 bfd_elf_size_dynamic_sections. */
7917 bool
7918 ppc64_elf_tls_setup (struct bfd_link_info *info)
7920 struct ppc_link_hash_table *htab;
7921 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7923 htab = ppc_hash_table (info);
7924 if (htab == NULL)
7925 return false;
7927 /* Move dynamic linking info to the function descriptor sym. */
7928 if (htab->need_func_desc_adj)
7930 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7931 htab->need_func_desc_adj = 0;
7934 if (abiversion (info->output_bfd) == 1)
7935 htab->opd_abi = 1;
7937 if (htab->params->no_multi_toc)
7938 htab->do_multi_toc = 0;
7939 else if (!htab->do_multi_toc)
7940 htab->params->no_multi_toc = 1;
7942 /* Default to --no-plt-localentry, as this option can cause problems
7943 with symbol interposition. For example, glibc libpthread.so and
7944 libc.so duplicate many pthread symbols, with a fallback
7945 implementation in libc.so. In some cases the fallback does more
7946 work than the pthread implementation. __pthread_condattr_destroy
7947 is one such symbol: the libpthread.so implementation is
7948 localentry:0 while the libc.so implementation is localentry:8.
7949 An app that "cleverly" uses dlopen to only load necessary
7950 libraries at runtime may omit loading libpthread.so when not
7951 running multi-threaded, which then results in the libc.so
7952 fallback symbols being used and ld.so complaining. Now there
7953 are workarounds in ld (see non_zero_localentry) to detect the
7954 pthread situation, but that may not be the only case where
7955 --plt-localentry can cause trouble. */
7956 if (htab->params->plt_localentry0 < 0)
7957 htab->params->plt_localentry0 = 0;
7958 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7960 /* The issue is that __glink_PLTresolve saves r2, which is done
7961 because glibc ld.so _dl_runtime_resolve restores r2 to support
7962 a glibc plt call optimisation where global entry code is
7963 skipped on calls that resolve to the same binary. The
7964 __glink_PLTresolve save of r2 is incompatible with code
7965 making tail calls, because the tail call might go via the
7966 resolver and thus overwrite the proper saved r2. */
7967 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7968 "power10 pc-relative code"));
7969 htab->params->plt_localentry0 = 0;
7971 if (htab->params->plt_localentry0
7972 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7973 false, false, false) == NULL)
7974 _bfd_error_handler
7975 (_("warning: --plt-localentry is especially dangerous without "
7976 "ld.so support to detect ABI violations"));
7978 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7979 false, false, true);
7980 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7981 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7982 false, false, true);
7983 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7985 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7986 false, false, true);
7987 htab->tga_desc = ppc_elf_hash_entry (desc);
7988 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7989 false, false, true);
7990 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7992 if (htab->params->tls_get_addr_opt)
7994 struct elf_link_hash_entry *opt, *opt_fd;
7996 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7997 false, false, true);
7998 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7999 false, false, true);
8000 if (opt_fd != NULL
8001 && (opt_fd->root.type == bfd_link_hash_defined
8002 || opt_fd->root.type == bfd_link_hash_defweak))
8004 /* If glibc supports an optimized __tls_get_addr call stub,
8005 signalled by the presence of __tls_get_addr_opt, and we'll
8006 be calling __tls_get_addr via a plt call stub, then
8007 make __tls_get_addr point to __tls_get_addr_opt. */
8008 if (!(htab->elf.dynamic_sections_created
8009 && tga_fd != NULL
8010 && (tga_fd->type == STT_FUNC
8011 || tga_fd->needs_plt)
8012 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8013 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8014 tga_fd = NULL;
8015 if (!(htab->elf.dynamic_sections_created
8016 && desc_fd != NULL
8017 && (desc_fd->type == STT_FUNC
8018 || desc_fd->needs_plt)
8019 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8020 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8021 desc_fd = NULL;
8023 if (tga_fd != NULL || desc_fd != NULL)
8025 struct plt_entry *ent = NULL;
8027 if (tga_fd != NULL)
8028 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8029 if (ent->plt.refcount > 0)
8030 break;
8031 if (ent == NULL && desc_fd != NULL)
8032 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8033 if (ent->plt.refcount > 0)
8034 break;
8035 if (ent != NULL)
8037 if (tga_fd != NULL)
8039 tga_fd->root.type = bfd_link_hash_indirect;
8040 tga_fd->root.u.i.link = &opt_fd->root;
8041 tga_fd->root.u.i.warning = NULL;
8042 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8044 if (desc_fd != NULL)
8046 desc_fd->root.type = bfd_link_hash_indirect;
8047 desc_fd->root.u.i.link = &opt_fd->root;
8048 desc_fd->root.u.i.warning = NULL;
8049 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8051 opt_fd->mark = 1;
8052 if (opt_fd->dynindx != -1)
8054 /* Use __tls_get_addr_opt in dynamic relocations. */
8055 opt_fd->dynindx = -1;
8056 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8057 opt_fd->dynstr_index);
8058 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8059 return false;
8061 if (tga_fd != NULL)
8063 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8064 tga = elf_hash_entry (htab->tls_get_addr);
8065 if (opt != NULL && tga != NULL)
8067 tga->root.type = bfd_link_hash_indirect;
8068 tga->root.u.i.link = &opt->root;
8069 tga->root.u.i.warning = NULL;
8070 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8071 opt->mark = 1;
8072 _bfd_elf_link_hash_hide_symbol (info, opt,
8073 tga->forced_local);
8074 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8076 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8077 htab->tls_get_addr_fd->is_func_descriptor = 1;
8078 if (htab->tls_get_addr != NULL)
8080 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8081 htab->tls_get_addr->is_func = 1;
8084 if (desc_fd != NULL)
8086 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8087 if (opt != NULL && desc != NULL)
8089 desc->root.type = bfd_link_hash_indirect;
8090 desc->root.u.i.link = &opt->root;
8091 desc->root.u.i.warning = NULL;
8092 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8093 opt->mark = 1;
8094 _bfd_elf_link_hash_hide_symbol (info, opt,
8095 desc->forced_local);
8096 htab->tga_desc = ppc_elf_hash_entry (opt);
8098 htab->tga_desc_fd->oh = htab->tga_desc;
8099 htab->tga_desc_fd->is_func_descriptor = 1;
8100 if (htab->tga_desc != NULL)
8102 htab->tga_desc->oh = htab->tga_desc_fd;
8103 htab->tga_desc->is_func = 1;
8109 else if (htab->params->tls_get_addr_opt < 0)
8110 htab->params->tls_get_addr_opt = 0;
8113 if (htab->tga_desc_fd != NULL
8114 && htab->params->tls_get_addr_opt
8115 && htab->params->no_tls_get_addr_regsave == -1)
8116 htab->params->no_tls_get_addr_regsave = 0;
8118 return true;
8121 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8122 any of HASH1, HASH2, HASH3, or HASH4. */
8124 static bool
8125 branch_reloc_hash_match (bfd *ibfd,
8126 Elf_Internal_Rela *rel,
8127 struct ppc_link_hash_entry *hash1,
8128 struct ppc_link_hash_entry *hash2,
8129 struct ppc_link_hash_entry *hash3,
8130 struct ppc_link_hash_entry *hash4)
8132 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8133 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8134 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8136 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8138 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8139 struct elf_link_hash_entry *h;
8141 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8142 h = elf_follow_link (h);
8143 if (h == elf_hash_entry (hash1)
8144 || h == elf_hash_entry (hash2)
8145 || h == elf_hash_entry (hash3)
8146 || h == elf_hash_entry (hash4))
8147 return true;
8149 return false;
8152 /* Run through all the TLS relocs looking for optimization
8153 opportunities. The linker has been hacked (see ppc64elf.em) to do
8154 a preliminary section layout so that we know the TLS segment
8155 offsets. We can't optimize earlier because some optimizations need
8156 to know the tp offset, and we need to optimize before allocating
8157 dynamic relocations. */
8159 bool
8160 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8162 bfd *ibfd;
8163 asection *sec;
8164 struct ppc_link_hash_table *htab;
8165 unsigned char *toc_ref;
8166 int pass;
8168 if (!bfd_link_executable (info))
8169 return true;
8171 htab = ppc_hash_table (info);
8172 if (htab == NULL)
8173 return false;
8175 htab->do_tls_opt = 1;
8177 /* Make two passes over the relocs. On the first pass, mark toc
8178 entries involved with tls relocs, and check that tls relocs
8179 involved in setting up a tls_get_addr call are indeed followed by
8180 such a call. If they are not, we can't do any tls optimization.
8181 On the second pass twiddle tls_mask flags to notify
8182 relocate_section that optimization can be done, and adjust got
8183 and plt refcounts. */
8184 toc_ref = NULL;
8185 for (pass = 0; pass < 2; ++pass)
8186 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8188 Elf_Internal_Sym *locsyms = NULL;
8189 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8191 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8192 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8194 Elf_Internal_Rela *relstart, *rel, *relend;
8195 bool found_tls_get_addr_arg = 0;
8197 /* Read the relocations. */
8198 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8199 info->keep_memory);
8200 if (relstart == NULL)
8202 free (toc_ref);
8203 return false;
8206 relend = relstart + sec->reloc_count;
8207 for (rel = relstart; rel < relend; rel++)
8209 enum elf_ppc64_reloc_type r_type;
8210 unsigned long r_symndx;
8211 struct elf_link_hash_entry *h;
8212 Elf_Internal_Sym *sym;
8213 asection *sym_sec;
8214 unsigned char *tls_mask;
8215 unsigned int tls_set, tls_clear, tls_type = 0;
8216 bfd_vma value;
8217 bool ok_tprel, is_local;
8218 long toc_ref_index = 0;
8219 int expecting_tls_get_addr = 0;
8220 bool ret = false;
8222 r_symndx = ELF64_R_SYM (rel->r_info);
8223 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8224 r_symndx, ibfd))
8226 err_free_rel:
8227 if (elf_section_data (sec)->relocs != relstart)
8228 free (relstart);
8229 free (toc_ref);
8230 if (elf_symtab_hdr (ibfd).contents
8231 != (unsigned char *) locsyms)
8232 free (locsyms);
8233 return ret;
8236 if (h != NULL)
8238 if (h->root.type == bfd_link_hash_defined
8239 || h->root.type == bfd_link_hash_defweak)
8240 value = h->root.u.def.value;
8241 else if (h->root.type == bfd_link_hash_undefweak)
8242 value = 0;
8243 else
8245 found_tls_get_addr_arg = 0;
8246 continue;
8249 else
8250 /* Symbols referenced by TLS relocs must be of type
8251 STT_TLS. So no need for .opd local sym adjust. */
8252 value = sym->st_value;
8254 ok_tprel = false;
8255 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8256 if (is_local)
8258 if (h != NULL
8259 && h->root.type == bfd_link_hash_undefweak)
8260 ok_tprel = true;
8261 else if (sym_sec != NULL
8262 && sym_sec->output_section != NULL)
8264 value += sym_sec->output_offset;
8265 value += sym_sec->output_section->vma;
8266 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8267 /* Note that even though the prefix insns
8268 allow a 1<<33 offset we use the same test
8269 as for addis;addi. There may be a mix of
8270 pcrel and non-pcrel code and the decision
8271 to optimise is per symbol, not per TLS
8272 sequence. */
8273 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8277 r_type = ELF64_R_TYPE (rel->r_info);
8278 /* If this section has old-style __tls_get_addr calls
8279 without marker relocs, then check that each
8280 __tls_get_addr call reloc is preceded by a reloc
8281 that conceivably belongs to the __tls_get_addr arg
8282 setup insn. If we don't find matching arg setup
8283 relocs, don't do any tls optimization. */
8284 if (pass == 0
8285 && sec->nomark_tls_get_addr
8286 && h != NULL
8287 && is_tls_get_addr (h, htab)
8288 && !found_tls_get_addr_arg
8289 && is_branch_reloc (r_type))
8291 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8292 "TLS optimization disabled\n"),
8293 ibfd, sec, rel->r_offset);
8294 ret = true;
8295 goto err_free_rel;
8298 found_tls_get_addr_arg = 0;
8299 switch (r_type)
8301 case R_PPC64_GOT_TLSLD16:
8302 case R_PPC64_GOT_TLSLD16_LO:
8303 case R_PPC64_GOT_TLSLD_PCREL34:
8304 expecting_tls_get_addr = 1;
8305 found_tls_get_addr_arg = 1;
8306 /* Fall through. */
8308 case R_PPC64_GOT_TLSLD16_HI:
8309 case R_PPC64_GOT_TLSLD16_HA:
8310 /* These relocs should never be against a symbol
8311 defined in a shared lib. Leave them alone if
8312 that turns out to be the case. */
8313 if (!is_local)
8314 continue;
8316 /* LD -> LE */
8317 tls_set = 0;
8318 tls_clear = TLS_LD;
8319 tls_type = TLS_TLS | TLS_LD;
8320 break;
8322 case R_PPC64_GOT_TLSGD16:
8323 case R_PPC64_GOT_TLSGD16_LO:
8324 case R_PPC64_GOT_TLSGD_PCREL34:
8325 expecting_tls_get_addr = 1;
8326 found_tls_get_addr_arg = 1;
8327 /* Fall through. */
8329 case R_PPC64_GOT_TLSGD16_HI:
8330 case R_PPC64_GOT_TLSGD16_HA:
8331 if (ok_tprel)
8332 /* GD -> LE */
8333 tls_set = 0;
8334 else
8335 /* GD -> IE */
8336 tls_set = TLS_TLS | TLS_GDIE;
8337 tls_clear = TLS_GD;
8338 tls_type = TLS_TLS | TLS_GD;
8339 break;
8341 case R_PPC64_GOT_TPREL_PCREL34:
8342 case R_PPC64_GOT_TPREL16_DS:
8343 case R_PPC64_GOT_TPREL16_LO_DS:
8344 case R_PPC64_GOT_TPREL16_HI:
8345 case R_PPC64_GOT_TPREL16_HA:
8346 if (ok_tprel)
8348 /* IE -> LE */
8349 tls_set = 0;
8350 tls_clear = TLS_TPREL;
8351 tls_type = TLS_TLS | TLS_TPREL;
8352 break;
8354 continue;
8356 case R_PPC64_TLSLD:
8357 if (!is_local)
8358 continue;
8359 /* Fall through. */
8360 case R_PPC64_TLSGD:
8361 if (rel + 1 < relend
8362 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8364 if (pass != 0
8365 && (ELF64_R_TYPE (rel[1].r_info)
8366 != R_PPC64_PLTSEQ)
8367 && (ELF64_R_TYPE (rel[1].r_info)
8368 != R_PPC64_PLTSEQ_NOTOC))
8370 r_symndx = ELF64_R_SYM (rel[1].r_info);
8371 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8372 r_symndx, ibfd))
8373 goto err_free_rel;
8374 if (h != NULL)
8376 struct plt_entry *ent = NULL;
8378 for (ent = h->plt.plist;
8379 ent != NULL;
8380 ent = ent->next)
8381 if (ent->addend == rel[1].r_addend)
8382 break;
8384 if (ent != NULL
8385 && ent->plt.refcount > 0)
8386 ent->plt.refcount -= 1;
8389 continue;
8391 found_tls_get_addr_arg = 1;
8392 /* Fall through. */
8394 case R_PPC64_TLS:
8395 case R_PPC64_TOC16:
8396 case R_PPC64_TOC16_LO:
8397 if (sym_sec == NULL || sym_sec != toc)
8398 continue;
8400 /* Mark this toc entry as referenced by a TLS
8401 code sequence. We can do that now in the
8402 case of R_PPC64_TLS, and after checking for
8403 tls_get_addr for the TOC16 relocs. */
8404 if (toc_ref == NULL)
8405 toc_ref
8406 = bfd_zmalloc (toc->output_section->rawsize / 8);
8407 if (toc_ref == NULL)
8408 goto err_free_rel;
8410 if (h != NULL)
8411 value = h->root.u.def.value;
8412 else
8413 value = sym->st_value;
8414 value += rel->r_addend;
8415 if (value % 8 != 0)
8416 continue;
8417 BFD_ASSERT (value < toc->size
8418 && toc->output_offset % 8 == 0);
8419 toc_ref_index = (value + toc->output_offset) / 8;
8420 if (r_type == R_PPC64_TLS
8421 || r_type == R_PPC64_TLSGD
8422 || r_type == R_PPC64_TLSLD)
8424 toc_ref[toc_ref_index] = 1;
8425 continue;
8428 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8429 continue;
8431 tls_set = 0;
8432 tls_clear = 0;
8433 expecting_tls_get_addr = 2;
8434 break;
8436 case R_PPC64_TPREL64:
8437 if (pass == 0
8438 || sec != toc
8439 || toc_ref == NULL
8440 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8441 continue;
8442 if (ok_tprel)
8444 /* IE -> LE */
8445 tls_set = TLS_EXPLICIT;
8446 tls_clear = TLS_TPREL;
8447 break;
8449 continue;
8451 case R_PPC64_DTPMOD64:
8452 if (pass == 0
8453 || sec != toc
8454 || toc_ref == NULL
8455 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8456 continue;
8457 if (rel + 1 < relend
8458 && (rel[1].r_info
8459 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8460 && rel[1].r_offset == rel->r_offset + 8)
8462 if (ok_tprel)
8463 /* GD -> LE */
8464 tls_set = TLS_EXPLICIT | TLS_GD;
8465 else
8466 /* GD -> IE */
8467 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8468 tls_clear = TLS_GD;
8470 else
8472 if (!is_local)
8473 continue;
8475 /* LD -> LE */
8476 tls_set = TLS_EXPLICIT;
8477 tls_clear = TLS_LD;
8479 break;
8481 case R_PPC64_TPREL16_HA:
8482 if (pass == 0)
8484 unsigned char buf[4];
8485 unsigned int insn;
8486 bfd_vma off = rel->r_offset & ~3;
8487 if (!bfd_get_section_contents (ibfd, sec, buf,
8488 off, 4))
8489 goto err_free_rel;
8490 insn = bfd_get_32 (ibfd, buf);
8491 /* addis rt,13,imm */
8492 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8493 != ((15u << 26) | (13 << 16)))
8495 /* xgettext:c-format */
8496 info->callbacks->minfo
8497 (_("%H: warning: %s unexpected insn %#x.\n"),
8498 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8499 htab->do_tls_opt = 0;
8502 continue;
8504 case R_PPC64_TPREL16_HI:
8505 case R_PPC64_TPREL16_HIGH:
8506 case R_PPC64_TPREL16_HIGHA:
8507 case R_PPC64_TPREL16_HIGHER:
8508 case R_PPC64_TPREL16_HIGHERA:
8509 case R_PPC64_TPREL16_HIGHEST:
8510 case R_PPC64_TPREL16_HIGHESTA:
8511 /* These can all be used in sequences along with
8512 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8513 able to verify easily. */
8514 htab->do_tls_opt = 0;
8515 continue;
8517 default:
8518 continue;
8521 if (pass == 0)
8523 if (!expecting_tls_get_addr
8524 || !sec->nomark_tls_get_addr)
8525 continue;
8527 if (rel + 1 < relend
8528 && branch_reloc_hash_match (ibfd, rel + 1,
8529 htab->tls_get_addr_fd,
8530 htab->tga_desc_fd,
8531 htab->tls_get_addr,
8532 htab->tga_desc))
8534 if (expecting_tls_get_addr == 2)
8536 /* Check for toc tls entries. */
8537 unsigned char *toc_tls;
8538 int retval;
8540 retval = get_tls_mask (&toc_tls, NULL, NULL,
8541 &locsyms,
8542 rel, ibfd);
8543 if (retval == 0)
8544 goto err_free_rel;
8545 if (toc_tls != NULL)
8547 if ((*toc_tls & TLS_TLS) != 0
8548 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8549 found_tls_get_addr_arg = 1;
8550 if (retval > 1)
8551 toc_ref[toc_ref_index] = 1;
8554 continue;
8557 /* Uh oh, we didn't find the expected call. We
8558 could just mark this symbol to exclude it
8559 from tls optimization but it's safer to skip
8560 the entire optimization. */
8561 /* xgettext:c-format */
8562 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8563 "TLS optimization disabled\n"),
8564 ibfd, sec, rel->r_offset);
8565 ret = true;
8566 goto err_free_rel;
8569 /* If we don't have old-style __tls_get_addr calls
8570 without TLSGD/TLSLD marker relocs, and we haven't
8571 found a new-style __tls_get_addr call with a
8572 marker for this symbol, then we either have a
8573 broken object file or an -mlongcall style
8574 indirect call to __tls_get_addr without a marker.
8575 Disable optimization in this case. */
8576 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8577 && (tls_set & TLS_EXPLICIT) == 0
8578 && !sec->nomark_tls_get_addr
8579 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8580 != (TLS_TLS | TLS_MARK)))
8581 continue;
8583 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8585 struct plt_entry *ent = NULL;
8587 if (htab->tls_get_addr_fd != NULL)
8588 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8589 ent != NULL;
8590 ent = ent->next)
8591 if (ent->addend == 0)
8592 break;
8594 if (ent == NULL && htab->tga_desc_fd != NULL)
8595 for (ent = htab->tga_desc_fd->elf.plt.plist;
8596 ent != NULL;
8597 ent = ent->next)
8598 if (ent->addend == 0)
8599 break;
8601 if (ent == NULL && htab->tls_get_addr != NULL)
8602 for (ent = htab->tls_get_addr->elf.plt.plist;
8603 ent != NULL;
8604 ent = ent->next)
8605 if (ent->addend == 0)
8606 break;
8608 if (ent == NULL && htab->tga_desc != NULL)
8609 for (ent = htab->tga_desc->elf.plt.plist;
8610 ent != NULL;
8611 ent = ent->next)
8612 if (ent->addend == 0)
8613 break;
8615 if (ent != NULL
8616 && ent->plt.refcount > 0)
8617 ent->plt.refcount -= 1;
8620 if (tls_clear == 0)
8621 continue;
8623 if ((tls_set & TLS_EXPLICIT) == 0)
8625 struct got_entry *ent;
8627 /* Adjust got entry for this reloc. */
8628 if (h != NULL)
8629 ent = h->got.glist;
8630 else
8631 ent = elf_local_got_ents (ibfd)[r_symndx];
8633 for (; ent != NULL; ent = ent->next)
8634 if (ent->addend == rel->r_addend
8635 && ent->owner == ibfd
8636 && ent->tls_type == tls_type)
8637 break;
8638 if (ent == NULL)
8639 abort ();
8641 if (tls_set == 0)
8643 /* We managed to get rid of a got entry. */
8644 if (ent->got.refcount > 0)
8645 ent->got.refcount -= 1;
8648 else
8650 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8651 we'll lose one or two dyn relocs. */
8652 if (!dec_dynrel_count (rel, sec, info,
8653 NULL, h, sym))
8654 return false;
8656 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8658 if (!dec_dynrel_count (rel + 1, sec, info,
8659 NULL, h, sym))
8660 return false;
8664 *tls_mask |= tls_set & 0xff;
8665 *tls_mask &= ~tls_clear;
8668 if (elf_section_data (sec)->relocs != relstart)
8669 free (relstart);
8672 if (locsyms != NULL
8673 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8675 if (!info->keep_memory)
8676 free (locsyms);
8677 else
8678 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8682 free (toc_ref);
8683 return true;
8686 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8687 the values of any global symbols in a toc section that has been
8688 edited. Globals in toc sections should be a rarity, so this function
8689 sets a flag if any are found in toc sections other than the one just
8690 edited, so that further hash table traversals can be avoided. */
8692 struct adjust_toc_info
8694 asection *toc;
8695 unsigned long *skip;
8696 bool global_toc_syms;
8699 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8701 static bool
8702 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8704 struct ppc_link_hash_entry *eh;
8705 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8706 unsigned long i;
8708 if (h->root.type != bfd_link_hash_defined
8709 && h->root.type != bfd_link_hash_defweak)
8710 return true;
8712 eh = ppc_elf_hash_entry (h);
8713 if (eh->adjust_done)
8714 return true;
8716 if (eh->elf.root.u.def.section == toc_inf->toc)
8718 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8719 i = toc_inf->toc->rawsize >> 3;
8720 else
8721 i = eh->elf.root.u.def.value >> 3;
8723 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8725 _bfd_error_handler
8726 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8728 ++i;
8729 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8730 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8733 eh->elf.root.u.def.value -= toc_inf->skip[i];
8734 eh->adjust_done = 1;
8736 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8737 toc_inf->global_toc_syms = true;
8739 return true;
8742 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8743 on a _LO variety toc/got reloc. */
8745 static bool
8746 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8748 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8749 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8750 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8751 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8752 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8753 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8754 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8755 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8756 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8757 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8758 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8759 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8760 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8761 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8762 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8763 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8764 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8765 /* Exclude lfqu by testing reloc. If relocs are ever
8766 defined for the reduced D field in psq_lu then those
8767 will need testing too. */
8768 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8769 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8770 && (insn & 1) == 0)
8771 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8772 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8773 /* Exclude stfqu. psq_stu as above for psq_lu. */
8774 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8775 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8776 && (insn & 1) == 0));
8779 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8780 pld ra,symbol@got@pcrel
8781 load/store rt,off(ra)
8783 pla ra,symbol@pcrel
8784 load/store rt,off(ra)
8785 may be translated to
8786 pload/pstore rt,symbol+off@pcrel
8787 nop.
8788 This function returns true if the optimization is possible, placing
8789 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8791 On entry to this function, the linker has already determined that
8792 the pld can be replaced with pla: *PINSN1 is that pla insn,
8793 while *PINSN2 is the second instruction. */
8795 static bool
8796 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8798 uint64_t insn1 = *pinsn1;
8799 uint64_t insn2 = *pinsn2;
8800 bfd_signed_vma off;
8802 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8804 /* Check that regs match. */
8805 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8806 return false;
8808 /* P8LS or PMLS form, non-pcrel. */
8809 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8810 return false;
8812 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8813 *pinsn2 = PNOP;
8814 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8815 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8816 return true;
8819 insn2 >>= 32;
8821 /* Check that regs match. */
8822 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8823 return false;
8825 switch ((insn2 >> 26) & 63)
8827 default:
8828 return false;
8830 case 32: /* lwz */
8831 case 34: /* lbz */
8832 case 36: /* stw */
8833 case 38: /* stb */
8834 case 40: /* lhz */
8835 case 42: /* lha */
8836 case 44: /* sth */
8837 case 48: /* lfs */
8838 case 50: /* lfd */
8839 case 52: /* stfs */
8840 case 54: /* stfd */
8841 /* These are the PMLS cases, where we just need to tack a prefix
8842 on the insn. */
8843 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8844 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8845 off = insn2 & 0xffff;
8846 break;
8848 case 58: /* lwa, ld */
8849 if ((insn2 & 1) != 0)
8850 return false;
8851 insn1 = ((1ULL << 58) | (1ULL << 52)
8852 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8853 | (insn2 & (31ULL << 21)));
8854 off = insn2 & 0xfffc;
8855 break;
8857 case 57: /* lxsd, lxssp */
8858 if ((insn2 & 3) < 2)
8859 return false;
8860 insn1 = ((1ULL << 58) | (1ULL << 52)
8861 | ((40ULL | (insn2 & 3)) << 26)
8862 | (insn2 & (31ULL << 21)));
8863 off = insn2 & 0xfffc;
8864 break;
8866 case 61: /* stxsd, stxssp, lxv, stxv */
8867 if ((insn2 & 3) == 0)
8868 return false;
8869 else if ((insn2 & 3) >= 2)
8871 insn1 = ((1ULL << 58) | (1ULL << 52)
8872 | ((44ULL | (insn2 & 3)) << 26)
8873 | (insn2 & (31ULL << 21)));
8874 off = insn2 & 0xfffc;
8876 else
8878 insn1 = ((1ULL << 58) | (1ULL << 52)
8879 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8880 | (insn2 & (31ULL << 21)));
8881 off = insn2 & 0xfff0;
8883 break;
8885 case 56: /* lq */
8886 insn1 = ((1ULL << 58) | (1ULL << 52)
8887 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8888 off = insn2 & 0xffff;
8889 break;
8891 case 6: /* lxvp, stxvp */
8892 if ((insn2 & 0xe) != 0)
8893 return false;
8894 insn1 = ((1ULL << 58) | (1ULL << 52)
8895 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8896 | (insn2 & (31ULL << 21)));
8897 off = insn2 & 0xfff0;
8898 break;
8900 case 62: /* std, stq */
8901 if ((insn2 & 1) != 0)
8902 return false;
8903 insn1 = ((1ULL << 58) | (1ULL << 52)
8904 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8905 | (insn2 & (31ULL << 21)));
8906 off = insn2 & 0xfffc;
8907 break;
8910 *pinsn1 = insn1;
8911 *pinsn2 = (uint64_t) NOP << 32;
8912 *poff = (off ^ 0x8000) - 0x8000;
8913 return true;
8916 /* Examine all relocs referencing .toc sections in order to remove
8917 unused .toc entries. */
8919 bool
8920 ppc64_elf_edit_toc (struct bfd_link_info *info)
8922 bfd *ibfd;
8923 struct adjust_toc_info toc_inf;
8924 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8926 htab->do_toc_opt = 1;
8927 toc_inf.global_toc_syms = true;
8928 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8930 asection *toc, *sec;
8931 Elf_Internal_Shdr *symtab_hdr;
8932 Elf_Internal_Sym *local_syms;
8933 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8934 unsigned long *skip, *drop;
8935 unsigned char *used;
8936 unsigned char *keep, last, some_unused;
8938 if (!is_ppc64_elf (ibfd))
8939 continue;
8941 toc = bfd_get_section_by_name (ibfd, ".toc");
8942 if (toc == NULL
8943 || toc->size == 0
8944 || (toc->flags & SEC_HAS_CONTENTS) == 0
8945 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8946 || discarded_section (toc))
8947 continue;
8949 toc_relocs = NULL;
8950 local_syms = NULL;
8951 symtab_hdr = &elf_symtab_hdr (ibfd);
8953 /* Look at sections dropped from the final link. */
8954 skip = NULL;
8955 relstart = NULL;
8956 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8958 if (sec->reloc_count == 0
8959 || !discarded_section (sec)
8960 || get_opd_info (sec)
8961 || (sec->flags & SEC_ALLOC) == 0
8962 || (sec->flags & SEC_DEBUGGING) != 0)
8963 continue;
8965 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8966 if (relstart == NULL)
8967 goto error_ret;
8969 /* Run through the relocs to see which toc entries might be
8970 unused. */
8971 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8973 enum elf_ppc64_reloc_type r_type;
8974 unsigned long r_symndx;
8975 asection *sym_sec;
8976 struct elf_link_hash_entry *h;
8977 Elf_Internal_Sym *sym;
8978 bfd_vma val;
8980 r_type = ELF64_R_TYPE (rel->r_info);
8981 switch (r_type)
8983 default:
8984 continue;
8986 case R_PPC64_TOC16:
8987 case R_PPC64_TOC16_LO:
8988 case R_PPC64_TOC16_HI:
8989 case R_PPC64_TOC16_HA:
8990 case R_PPC64_TOC16_DS:
8991 case R_PPC64_TOC16_LO_DS:
8992 break;
8995 r_symndx = ELF64_R_SYM (rel->r_info);
8996 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8997 r_symndx, ibfd))
8998 goto error_ret;
9000 if (sym_sec != toc)
9001 continue;
9003 if (h != NULL)
9004 val = h->root.u.def.value;
9005 else
9006 val = sym->st_value;
9007 val += rel->r_addend;
9009 if (val >= toc->size)
9010 continue;
9012 /* Anything in the toc ought to be aligned to 8 bytes.
9013 If not, don't mark as unused. */
9014 if (val & 7)
9015 continue;
9017 if (skip == NULL)
9019 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9020 if (skip == NULL)
9021 goto error_ret;
9024 skip[val >> 3] = ref_from_discarded;
9027 if (elf_section_data (sec)->relocs != relstart)
9028 free (relstart);
9031 /* For largetoc loads of address constants, we can convert
9032 . addis rx,2,addr@got@ha
9033 . ld ry,addr@got@l(rx)
9035 . addis rx,2,addr@toc@ha
9036 . addi ry,rx,addr@toc@l
9037 when addr is within 2G of the toc pointer. This then means
9038 that the word storing "addr" in the toc is no longer needed. */
9040 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9041 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9042 && toc->reloc_count != 0)
9044 /* Read toc relocs. */
9045 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9046 info->keep_memory);
9047 if (toc_relocs == NULL)
9048 goto error_ret;
9050 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9052 enum elf_ppc64_reloc_type r_type;
9053 unsigned long r_symndx;
9054 asection *sym_sec;
9055 struct elf_link_hash_entry *h;
9056 Elf_Internal_Sym *sym;
9057 bfd_vma val, addr;
9059 r_type = ELF64_R_TYPE (rel->r_info);
9060 if (r_type != R_PPC64_ADDR64)
9061 continue;
9063 r_symndx = ELF64_R_SYM (rel->r_info);
9064 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9065 r_symndx, ibfd))
9066 goto error_ret;
9068 if (sym_sec == NULL
9069 || sym_sec->output_section == NULL
9070 || discarded_section (sym_sec))
9071 continue;
9073 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9074 || (bfd_link_pic (info)
9075 && sym_sec == bfd_abs_section_ptr))
9076 continue;
9078 if (h != NULL)
9080 if (h->type == STT_GNU_IFUNC)
9081 continue;
9082 val = h->root.u.def.value;
9084 else
9086 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9087 continue;
9088 val = sym->st_value;
9090 val += rel->r_addend;
9091 val += sym_sec->output_section->vma + sym_sec->output_offset;
9093 /* We don't yet know the exact toc pointer value, but we
9094 know it will be somewhere in the toc section. Don't
9095 optimize if the difference from any possible toc
9096 pointer is outside [ff..f80008000, 7fff7fff]. */
9097 addr = toc->output_section->vma + TOC_BASE_OFF;
9098 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9099 continue;
9101 addr = toc->output_section->vma + toc->output_section->rawsize;
9102 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9103 continue;
9105 if (skip == NULL)
9107 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9108 if (skip == NULL)
9109 goto error_ret;
9112 skip[rel->r_offset >> 3]
9113 |= can_optimize | ((rel - toc_relocs) << 2);
9117 if (skip == NULL)
9118 continue;
9120 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9121 if (used == NULL)
9123 error_ret:
9124 if (symtab_hdr->contents != (unsigned char *) local_syms)
9125 free (local_syms);
9126 if (sec != NULL
9127 && elf_section_data (sec)->relocs != relstart)
9128 free (relstart);
9129 if (elf_section_data (toc)->relocs != toc_relocs)
9130 free (toc_relocs);
9131 free (skip);
9132 return false;
9135 /* Now check all kept sections that might reference the toc.
9136 Check the toc itself last. */
9137 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9138 : ibfd->sections);
9139 sec != NULL;
9140 sec = (sec == toc ? NULL
9141 : sec->next == NULL ? toc
9142 : sec->next == toc && toc->next ? toc->next
9143 : sec->next))
9145 int repeat;
9147 if (sec->reloc_count == 0
9148 || discarded_section (sec)
9149 || get_opd_info (sec)
9150 || (sec->flags & SEC_ALLOC) == 0
9151 || (sec->flags & SEC_DEBUGGING) != 0)
9152 continue;
9154 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9155 info->keep_memory);
9156 if (relstart == NULL)
9158 free (used);
9159 goto error_ret;
9162 /* Mark toc entries referenced as used. */
9165 repeat = 0;
9166 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9168 enum elf_ppc64_reloc_type r_type;
9169 unsigned long r_symndx;
9170 asection *sym_sec;
9171 struct elf_link_hash_entry *h;
9172 Elf_Internal_Sym *sym;
9173 bfd_vma val;
9175 r_type = ELF64_R_TYPE (rel->r_info);
9176 switch (r_type)
9178 case R_PPC64_TOC16:
9179 case R_PPC64_TOC16_LO:
9180 case R_PPC64_TOC16_HI:
9181 case R_PPC64_TOC16_HA:
9182 case R_PPC64_TOC16_DS:
9183 case R_PPC64_TOC16_LO_DS:
9184 /* In case we're taking addresses of toc entries. */
9185 case R_PPC64_ADDR64:
9186 break;
9188 default:
9189 continue;
9192 r_symndx = ELF64_R_SYM (rel->r_info);
9193 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9194 r_symndx, ibfd))
9196 free (used);
9197 goto error_ret;
9200 if (sym_sec != toc)
9201 continue;
9203 if (h != NULL)
9204 val = h->root.u.def.value;
9205 else
9206 val = sym->st_value;
9207 val += rel->r_addend;
9209 if (val >= toc->size)
9210 continue;
9212 if ((skip[val >> 3] & can_optimize) != 0)
9214 bfd_vma off;
9215 unsigned char opc;
9217 switch (r_type)
9219 case R_PPC64_TOC16_HA:
9220 break;
9222 case R_PPC64_TOC16_LO_DS:
9223 off = rel->r_offset;
9224 off += (bfd_big_endian (ibfd) ? -2 : 3);
9225 if (!bfd_get_section_contents (ibfd, sec, &opc,
9226 off, 1))
9228 free (used);
9229 goto error_ret;
9231 if ((opc & (0x3f << 2)) == (58u << 2))
9232 break;
9233 /* Fall through. */
9235 default:
9236 /* Wrong sort of reloc, or not a ld. We may
9237 as well clear ref_from_discarded too. */
9238 skip[val >> 3] = 0;
9242 if (sec != toc)
9243 used[val >> 3] = 1;
9244 /* For the toc section, we only mark as used if this
9245 entry itself isn't unused. */
9246 else if ((used[rel->r_offset >> 3]
9247 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9248 && !used[val >> 3])
9250 /* Do all the relocs again, to catch reference
9251 chains. */
9252 repeat = 1;
9253 used[val >> 3] = 1;
9257 while (repeat);
9259 if (elf_section_data (sec)->relocs != relstart)
9260 free (relstart);
9263 /* Merge the used and skip arrays. Assume that TOC
9264 doublewords not appearing as either used or unused belong
9265 to an entry more than one doubleword in size. */
9266 for (drop = skip, keep = used, last = 0, some_unused = 0;
9267 drop < skip + (toc->size + 7) / 8;
9268 ++drop, ++keep)
9270 if (*keep)
9272 *drop &= ~ref_from_discarded;
9273 if ((*drop & can_optimize) != 0)
9274 some_unused = 1;
9275 last = 0;
9277 else if ((*drop & ref_from_discarded) != 0)
9279 some_unused = 1;
9280 last = ref_from_discarded;
9282 else
9283 *drop = last;
9286 free (used);
9288 if (some_unused)
9290 bfd_byte *contents, *src;
9291 unsigned long off;
9292 Elf_Internal_Sym *sym;
9293 bool local_toc_syms = false;
9295 /* Shuffle the toc contents, and at the same time convert the
9296 skip array from booleans into offsets. */
9297 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9298 goto error_ret;
9300 elf_section_data (toc)->this_hdr.contents = contents;
9302 for (src = contents, off = 0, drop = skip;
9303 src < contents + toc->size;
9304 src += 8, ++drop)
9306 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9307 off += 8;
9308 else if (off != 0)
9310 *drop = off;
9311 memcpy (src - off, src, 8);
9314 *drop = off;
9315 toc->rawsize = toc->size;
9316 toc->size = src - contents - off;
9318 /* Adjust addends for relocs against the toc section sym,
9319 and optimize any accesses we can. */
9320 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9322 if (sec->reloc_count == 0
9323 || discarded_section (sec))
9324 continue;
9326 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9327 info->keep_memory);
9328 if (relstart == NULL)
9329 goto error_ret;
9331 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9333 enum elf_ppc64_reloc_type r_type;
9334 unsigned long r_symndx;
9335 asection *sym_sec;
9336 struct elf_link_hash_entry *h;
9337 bfd_vma val;
9339 r_type = ELF64_R_TYPE (rel->r_info);
9340 switch (r_type)
9342 default:
9343 continue;
9345 case R_PPC64_TOC16:
9346 case R_PPC64_TOC16_LO:
9347 case R_PPC64_TOC16_HI:
9348 case R_PPC64_TOC16_HA:
9349 case R_PPC64_TOC16_DS:
9350 case R_PPC64_TOC16_LO_DS:
9351 case R_PPC64_ADDR64:
9352 break;
9355 r_symndx = ELF64_R_SYM (rel->r_info);
9356 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9357 r_symndx, ibfd))
9358 goto error_ret;
9360 if (sym_sec != toc)
9361 continue;
9363 if (h != NULL)
9364 val = h->root.u.def.value;
9365 else
9367 val = sym->st_value;
9368 if (val != 0)
9369 local_toc_syms = true;
9372 val += rel->r_addend;
9374 if (val > toc->rawsize)
9375 val = toc->rawsize;
9376 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9377 continue;
9378 else if ((skip[val >> 3] & can_optimize) != 0)
9380 Elf_Internal_Rela *tocrel
9381 = toc_relocs + (skip[val >> 3] >> 2);
9382 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9384 switch (r_type)
9386 case R_PPC64_TOC16_HA:
9387 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9388 break;
9390 case R_PPC64_TOC16_LO_DS:
9391 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9392 break;
9394 default:
9395 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9396 ppc_howto_init ();
9397 info->callbacks->einfo
9398 /* xgettext:c-format */
9399 (_("%H: %s references "
9400 "optimized away TOC entry\n"),
9401 ibfd, sec, rel->r_offset,
9402 ppc64_elf_howto_table[r_type]->name);
9403 bfd_set_error (bfd_error_bad_value);
9404 goto error_ret;
9406 rel->r_addend = tocrel->r_addend;
9407 elf_section_data (sec)->relocs = relstart;
9408 continue;
9411 if (h != NULL || sym->st_value != 0)
9412 continue;
9414 rel->r_addend -= skip[val >> 3];
9415 elf_section_data (sec)->relocs = relstart;
9418 if (elf_section_data (sec)->relocs != relstart)
9419 free (relstart);
9422 /* We shouldn't have local or global symbols defined in the TOC,
9423 but handle them anyway. */
9424 if (local_syms != NULL)
9425 for (sym = local_syms;
9426 sym < local_syms + symtab_hdr->sh_info;
9427 ++sym)
9428 if (sym->st_value != 0
9429 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9431 unsigned long i;
9433 if (sym->st_value > toc->rawsize)
9434 i = toc->rawsize >> 3;
9435 else
9436 i = sym->st_value >> 3;
9438 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9440 if (local_toc_syms)
9441 _bfd_error_handler
9442 (_("%s defined on removed toc entry"),
9443 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9445 ++i;
9446 while ((skip[i] & (ref_from_discarded | can_optimize)));
9447 sym->st_value = (bfd_vma) i << 3;
9450 sym->st_value -= skip[i];
9451 symtab_hdr->contents = (unsigned char *) local_syms;
9454 /* Adjust any global syms defined in this toc input section. */
9455 if (toc_inf.global_toc_syms)
9457 toc_inf.toc = toc;
9458 toc_inf.skip = skip;
9459 toc_inf.global_toc_syms = false;
9460 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9461 &toc_inf);
9464 if (toc->reloc_count != 0)
9466 Elf_Internal_Shdr *rel_hdr;
9467 Elf_Internal_Rela *wrel;
9468 bfd_size_type sz;
9470 /* Remove unused toc relocs, and adjust those we keep. */
9471 if (toc_relocs == NULL)
9472 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9473 info->keep_memory);
9474 if (toc_relocs == NULL)
9475 goto error_ret;
9477 wrel = toc_relocs;
9478 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9479 if ((skip[rel->r_offset >> 3]
9480 & (ref_from_discarded | can_optimize)) == 0)
9482 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9483 wrel->r_info = rel->r_info;
9484 wrel->r_addend = rel->r_addend;
9485 ++wrel;
9487 else if (!dec_dynrel_count (rel, toc, info,
9488 &local_syms, NULL, NULL))
9489 goto error_ret;
9491 elf_section_data (toc)->relocs = toc_relocs;
9492 toc->reloc_count = wrel - toc_relocs;
9493 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9494 sz = rel_hdr->sh_entsize;
9495 rel_hdr->sh_size = toc->reloc_count * sz;
9498 else if (elf_section_data (toc)->relocs != toc_relocs)
9499 free (toc_relocs);
9501 if (local_syms != NULL
9502 && symtab_hdr->contents != (unsigned char *) local_syms)
9504 if (!info->keep_memory)
9505 free (local_syms);
9506 else
9507 symtab_hdr->contents = (unsigned char *) local_syms;
9509 free (skip);
9512 /* Look for cases where we can change an indirect GOT access to
9513 a GOT relative or PC relative access, possibly reducing the
9514 number of GOT entries. */
9515 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9517 asection *sec;
9518 Elf_Internal_Shdr *symtab_hdr;
9519 Elf_Internal_Sym *local_syms;
9520 Elf_Internal_Rela *relstart, *rel;
9521 bfd_vma got;
9523 if (!is_ppc64_elf (ibfd))
9524 continue;
9526 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9527 continue;
9529 sec = ppc64_elf_tdata (ibfd)->got;
9530 got = 0;
9531 if (sec != NULL)
9532 got = sec->output_section->vma + sec->output_offset + 0x8000;
9534 local_syms = NULL;
9535 symtab_hdr = &elf_symtab_hdr (ibfd);
9537 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9539 if (sec->reloc_count == 0
9540 || !ppc64_elf_section_data (sec)->has_optrel
9541 || discarded_section (sec))
9542 continue;
9544 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9545 info->keep_memory);
9546 if (relstart == NULL)
9548 got_error_ret:
9549 if (symtab_hdr->contents != (unsigned char *) local_syms)
9550 free (local_syms);
9551 if (sec != NULL
9552 && elf_section_data (sec)->relocs != relstart)
9553 free (relstart);
9554 return false;
9557 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9559 enum elf_ppc64_reloc_type r_type;
9560 unsigned long r_symndx;
9561 Elf_Internal_Sym *sym;
9562 asection *sym_sec;
9563 struct elf_link_hash_entry *h;
9564 struct got_entry *ent;
9565 bfd_vma val, pc;
9566 unsigned char buf[8];
9567 unsigned int insn;
9568 enum {no_check, check_lo, check_ha} insn_check;
9570 r_type = ELF64_R_TYPE (rel->r_info);
9571 switch (r_type)
9573 default:
9574 insn_check = no_check;
9575 break;
9577 case R_PPC64_PLT16_HA:
9578 case R_PPC64_GOT_TLSLD16_HA:
9579 case R_PPC64_GOT_TLSGD16_HA:
9580 case R_PPC64_GOT_TPREL16_HA:
9581 case R_PPC64_GOT_DTPREL16_HA:
9582 case R_PPC64_GOT16_HA:
9583 case R_PPC64_TOC16_HA:
9584 insn_check = check_ha;
9585 break;
9587 case R_PPC64_PLT16_LO:
9588 case R_PPC64_PLT16_LO_DS:
9589 case R_PPC64_GOT_TLSLD16_LO:
9590 case R_PPC64_GOT_TLSGD16_LO:
9591 case R_PPC64_GOT_TPREL16_LO_DS:
9592 case R_PPC64_GOT_DTPREL16_LO_DS:
9593 case R_PPC64_GOT16_LO:
9594 case R_PPC64_GOT16_LO_DS:
9595 case R_PPC64_TOC16_LO:
9596 case R_PPC64_TOC16_LO_DS:
9597 insn_check = check_lo;
9598 break;
9601 if (insn_check != no_check)
9603 bfd_vma off = rel->r_offset & ~3;
9605 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9606 goto got_error_ret;
9608 insn = bfd_get_32 (ibfd, buf);
9609 if (insn_check == check_lo
9610 ? !ok_lo_toc_insn (insn, r_type)
9611 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9612 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9614 char str[12];
9616 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9617 sprintf (str, "%#08x", insn);
9618 info->callbacks->einfo
9619 /* xgettext:c-format */
9620 (_("%H: got/toc optimization is not supported for"
9621 " %s instruction\n"),
9622 ibfd, sec, rel->r_offset & ~3, str);
9623 continue;
9627 switch (r_type)
9629 /* Note that we don't delete GOT entries for
9630 R_PPC64_GOT16_DS since we'd need a lot more
9631 analysis. For starters, the preliminary layout is
9632 before the GOT, PLT, dynamic sections and stubs are
9633 laid out. Then we'd need to allow for changes in
9634 distance between sections caused by alignment. */
9635 default:
9636 continue;
9638 case R_PPC64_GOT16_HA:
9639 case R_PPC64_GOT16_LO_DS:
9640 case R_PPC64_GOT_PCREL34:
9641 break;
9644 r_symndx = ELF64_R_SYM (rel->r_info);
9645 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9646 r_symndx, ibfd))
9647 goto got_error_ret;
9649 if (sym_sec == NULL
9650 || sym_sec->output_section == NULL
9651 || discarded_section (sym_sec))
9652 continue;
9654 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9655 continue;
9657 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9658 || (bfd_link_pic (info)
9659 && sym_sec == bfd_abs_section_ptr))
9660 continue;
9662 if (h != NULL)
9663 val = h->root.u.def.value;
9664 else
9665 val = sym->st_value;
9666 val += rel->r_addend;
9667 val += sym_sec->output_section->vma + sym_sec->output_offset;
9669 /* Fudge factor to allow for the fact that the preliminary layout
9670 isn't exact. Reduce limits by this factor. */
9671 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9673 switch (r_type)
9675 default:
9676 continue;
9678 case R_PPC64_GOT16_HA:
9679 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9680 >= LIMIT_ADJUST (0x100000000ULL))
9681 continue;
9683 if (!bfd_get_section_contents (ibfd, sec, buf,
9684 rel->r_offset & ~3, 4))
9685 goto got_error_ret;
9686 insn = bfd_get_32 (ibfd, buf);
9687 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9688 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9689 continue;
9690 break;
9692 case R_PPC64_GOT16_LO_DS:
9693 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9694 >= LIMIT_ADJUST (0x100000000ULL))
9695 continue;
9696 if (!bfd_get_section_contents (ibfd, sec, buf,
9697 rel->r_offset & ~3, 4))
9698 goto got_error_ret;
9699 insn = bfd_get_32 (ibfd, buf);
9700 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9701 continue;
9702 break;
9704 case R_PPC64_GOT_PCREL34:
9705 pc = rel->r_offset;
9706 pc += sec->output_section->vma + sec->output_offset;
9707 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9708 >= LIMIT_ADJUST (1ULL << 34))
9709 continue;
9710 if (!bfd_get_section_contents (ibfd, sec, buf,
9711 rel->r_offset & ~3, 8))
9712 goto got_error_ret;
9713 insn = bfd_get_32 (ibfd, buf);
9714 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9715 continue;
9716 insn = bfd_get_32 (ibfd, buf + 4);
9717 if ((insn & (0x3fu << 26)) != 57u << 26)
9718 continue;
9719 break;
9721 #undef LIMIT_ADJUST
9723 if (h != NULL)
9724 ent = h->got.glist;
9725 else
9727 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9728 ent = local_got_ents[r_symndx];
9730 for (; ent != NULL; ent = ent->next)
9731 if (ent->addend == rel->r_addend
9732 && ent->owner == ibfd
9733 && ent->tls_type == 0)
9734 break;
9735 BFD_ASSERT (ent && ent->got.refcount > 0);
9736 ent->got.refcount -= 1;
9739 if (elf_section_data (sec)->relocs != relstart)
9740 free (relstart);
9743 if (local_syms != NULL
9744 && symtab_hdr->contents != (unsigned char *) local_syms)
9746 if (!info->keep_memory)
9747 free (local_syms);
9748 else
9749 symtab_hdr->contents = (unsigned char *) local_syms;
9753 return true;
9756 /* Return true iff input section I references the TOC using
9757 instructions limited to +/-32k offsets. */
9759 bool
9760 ppc64_elf_has_small_toc_reloc (asection *i)
9762 return (is_ppc64_elf (i->owner)
9763 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9766 /* Allocate space for one GOT entry. */
9768 static void
9769 allocate_got (struct elf_link_hash_entry *h,
9770 struct bfd_link_info *info,
9771 struct got_entry *gent)
9773 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9774 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9775 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9776 ? 16 : 8);
9777 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9778 ? 2 : 1) * sizeof (Elf64_External_Rela);
9779 asection *got = ppc64_elf_tdata (gent->owner)->got;
9781 gent->got.offset = got->size;
9782 got->size += entsize;
9784 if (h->type == STT_GNU_IFUNC)
9786 htab->elf.irelplt->size += rentsize;
9787 htab->got_reli_size += rentsize;
9789 else if (((bfd_link_pic (info)
9790 && (gent->tls_type == 0
9791 ? !info->enable_dt_relr
9792 : !(bfd_link_executable (info)
9793 && SYMBOL_REFERENCES_LOCAL (info, h)))
9794 && !bfd_is_abs_symbol (&h->root))
9795 || (htab->elf.dynamic_sections_created
9796 && h->dynindx != -1
9797 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9798 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9800 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9801 relgot->size += rentsize;
9805 /* This function merges got entries in the same toc group. */
9807 static void
9808 merge_got_entries (struct got_entry **pent)
9810 struct got_entry *ent, *ent2;
9812 for (ent = *pent; ent != NULL; ent = ent->next)
9813 if (!ent->is_indirect)
9814 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9815 if (!ent2->is_indirect
9816 && ent2->addend == ent->addend
9817 && ent2->tls_type == ent->tls_type
9818 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9820 ent2->is_indirect = true;
9821 ent2->got.ent = ent;
9825 /* If H is undefined, make it dynamic if that makes sense. */
9827 static bool
9828 ensure_undef_dynamic (struct bfd_link_info *info,
9829 struct elf_link_hash_entry *h)
9831 struct elf_link_hash_table *htab = elf_hash_table (info);
9833 if (htab->dynamic_sections_created
9834 && ((info->dynamic_undefined_weak != 0
9835 && h->root.type == bfd_link_hash_undefweak)
9836 || h->root.type == bfd_link_hash_undefined)
9837 && h->dynindx == -1
9838 && !h->forced_local
9839 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9840 return bfd_elf_link_record_dynamic_symbol (info, h);
9841 return true;
9844 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9845 usual htab->elf.splt section for a PLT entry. */
9847 static inline
9848 bool use_local_plt (struct bfd_link_info *info,
9849 struct elf_link_hash_entry *h)
9851 return (h == NULL
9852 || h->dynindx == -1
9853 || !elf_hash_table (info)->dynamic_sections_created);
9856 /* Allocate space in .plt, .got and associated reloc sections for
9857 dynamic relocs. */
9859 static bool
9860 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9862 struct bfd_link_info *info;
9863 struct ppc_link_hash_table *htab;
9864 asection *s;
9865 struct ppc_link_hash_entry *eh;
9866 struct got_entry **pgent, *gent;
9868 if (h->root.type == bfd_link_hash_indirect)
9869 return true;
9871 info = (struct bfd_link_info *) inf;
9872 htab = ppc_hash_table (info);
9873 if (htab == NULL)
9874 return false;
9876 eh = ppc_elf_hash_entry (h);
9877 /* Run through the TLS GD got entries first if we're changing them
9878 to TPREL. */
9879 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9880 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9881 if (gent->got.refcount > 0
9882 && (gent->tls_type & TLS_GD) != 0)
9884 /* This was a GD entry that has been converted to TPREL. If
9885 there happens to be a TPREL entry we can use that one. */
9886 struct got_entry *ent;
9887 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9888 if (ent->got.refcount > 0
9889 && (ent->tls_type & TLS_TPREL) != 0
9890 && ent->addend == gent->addend
9891 && ent->owner == gent->owner)
9893 gent->got.refcount = 0;
9894 break;
9897 /* If not, then we'll be using our own TPREL entry. */
9898 if (gent->got.refcount != 0)
9899 gent->tls_type = TLS_TLS | TLS_TPREL;
9902 /* Remove any list entry that won't generate a word in the GOT before
9903 we call merge_got_entries. Otherwise we risk merging to empty
9904 entries. */
9905 pgent = &h->got.glist;
9906 while ((gent = *pgent) != NULL)
9907 if (gent->got.refcount > 0)
9909 if ((gent->tls_type & TLS_LD) != 0
9910 && SYMBOL_REFERENCES_LOCAL (info, h))
9912 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9913 *pgent = gent->next;
9915 else
9916 pgent = &gent->next;
9918 else
9919 *pgent = gent->next;
9921 if (!htab->do_multi_toc)
9922 merge_got_entries (&h->got.glist);
9924 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9925 if (!gent->is_indirect)
9927 /* Ensure we catch all the cases where this symbol should
9928 be made dynamic. */
9929 if (!ensure_undef_dynamic (info, h))
9930 return false;
9932 if (!is_ppc64_elf (gent->owner))
9933 abort ();
9935 allocate_got (h, info, gent);
9938 /* If no dynamic sections we can't have dynamic relocs, except for
9939 IFUNCs which are handled even in static executables. */
9940 if (!htab->elf.dynamic_sections_created
9941 && h->type != STT_GNU_IFUNC)
9942 h->dyn_relocs = NULL;
9944 /* Discard relocs on undefined symbols that must be local. */
9945 else if (h->root.type == bfd_link_hash_undefined
9946 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9947 h->dyn_relocs = NULL;
9949 /* Also discard relocs on undefined weak syms with non-default
9950 visibility, or when dynamic_undefined_weak says so. */
9951 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9952 h->dyn_relocs = NULL;
9954 if (h->dyn_relocs != NULL)
9956 struct ppc_dyn_relocs *p, **pp;
9958 /* In the shared -Bsymbolic case, discard space allocated for
9959 dynamic pc-relative relocs against symbols which turn out to
9960 be defined in regular objects. For the normal shared case,
9961 discard space for relocs that have become local due to symbol
9962 visibility changes. */
9963 if (bfd_link_pic (info))
9965 /* Relocs that use pc_count are those that appear on a call
9966 insn, or certain REL relocs (see must_be_dyn_reloc) that
9967 can be generated via assembly. We want calls to
9968 protected symbols to resolve directly to the function
9969 rather than going via the plt. If people want function
9970 pointer comparisons to work as expected then they should
9971 avoid writing weird assembly. */
9972 if (SYMBOL_CALLS_LOCAL (info, h))
9974 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9975 (p = *pp) != NULL;
9978 p->count -= p->pc_count;
9979 p->pc_count = 0;
9980 if (p->count == 0)
9981 *pp = p->next;
9982 else
9983 pp = &p->next;
9987 if (h->dyn_relocs != NULL)
9989 /* Ensure we catch all the cases where this symbol
9990 should be made dynamic. */
9991 if (!ensure_undef_dynamic (info, h))
9992 return false;
9996 /* For a fixed position executable, discard space for
9997 relocs against symbols which are not dynamic. */
9998 else if (h->type != STT_GNU_IFUNC)
10000 if ((h->dynamic_adjusted
10001 || (h->ref_regular
10002 && h->root.type == bfd_link_hash_undefweak
10003 && (info->dynamic_undefined_weak > 0
10004 || !_bfd_elf_readonly_dynrelocs (h))))
10005 && !h->def_regular
10006 && !ELF_COMMON_DEF_P (h))
10008 /* Ensure we catch all the cases where this symbol
10009 should be made dynamic. */
10010 if (!ensure_undef_dynamic (info, h))
10011 return false;
10013 /* But if that didn't work out, discard dynamic relocs. */
10014 if (h->dynindx == -1)
10015 h->dyn_relocs = NULL;
10017 else
10018 h->dyn_relocs = NULL;
10021 /* Finally, allocate space. */
10022 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10023 if (!discarded_section (p->sec))
10025 unsigned int count;
10026 asection *sreloc = elf_section_data (p->sec)->sreloc;
10027 if (eh->elf.type == STT_GNU_IFUNC)
10028 sreloc = htab->elf.irelplt;
10029 count = p->count;
10030 if (info->enable_dt_relr
10031 && ((!NO_OPD_RELOCS
10032 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10033 || (eh->elf.type != STT_GNU_IFUNC
10034 && SYMBOL_REFERENCES_LOCAL (info, h))))
10035 count -= p->rel_count;
10036 sreloc->size += count * sizeof (Elf64_External_Rela);
10040 /* We might need a PLT entry when the symbol
10041 a) is dynamic, or
10042 b) is an ifunc, or
10043 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10044 d) has plt16 relocs and we are linking statically. */
10045 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10046 || h->type == STT_GNU_IFUNC
10047 || (h->needs_plt && h->dynamic_adjusted)
10048 || (h->needs_plt
10049 && h->def_regular
10050 && !htab->elf.dynamic_sections_created
10051 && !htab->can_convert_all_inline_plt
10052 && (ppc_elf_hash_entry (h)->tls_mask
10053 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10055 struct plt_entry *pent;
10056 bool doneone = false;
10057 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10058 if (pent->plt.refcount > 0)
10060 if (!ensure_undef_dynamic (info, h))
10061 return false;
10063 if (use_local_plt (info, h))
10065 if (h->type == STT_GNU_IFUNC)
10067 s = htab->elf.iplt;
10068 pent->plt.offset = s->size;
10069 s->size += PLT_ENTRY_SIZE (htab);
10070 s = htab->elf.irelplt;
10072 else
10074 s = htab->pltlocal;
10075 pent->plt.offset = s->size;
10076 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10077 s = NULL;
10078 if (bfd_link_pic (info)
10079 && !(info->enable_dt_relr && !htab->opd_abi))
10080 s = htab->relpltlocal;
10083 else
10085 /* If this is the first .plt entry, make room for the special
10086 first entry. */
10087 s = htab->elf.splt;
10088 if (s->size == 0)
10089 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10091 pent->plt.offset = s->size;
10093 /* Make room for this entry. */
10094 s->size += PLT_ENTRY_SIZE (htab);
10096 /* Make room for the .glink code. */
10097 s = htab->glink;
10098 if (s->size == 0)
10099 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10100 if (htab->opd_abi)
10102 /* We need bigger stubs past index 32767. */
10103 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10104 s->size += 4;
10105 s->size += 2*4;
10107 else
10108 s->size += 4;
10110 /* We also need to make an entry in the .rela.plt section. */
10111 s = htab->elf.srelplt;
10113 if (s != NULL)
10114 s->size += sizeof (Elf64_External_Rela);
10115 doneone = true;
10117 else
10118 pent->plt.offset = (bfd_vma) -1;
10119 if (!doneone)
10121 h->plt.plist = NULL;
10122 h->needs_plt = 0;
10125 else
10127 h->plt.plist = NULL;
10128 h->needs_plt = 0;
10131 return true;
10134 #define PPC_LO(v) ((v) & 0xffff)
10135 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10136 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10137 #define D34(v) \
10138 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10139 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10141 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10142 to set up space for global entry stubs. These are put in glink,
10143 after the branch table. */
10145 static bool
10146 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10148 struct bfd_link_info *info;
10149 struct ppc_link_hash_table *htab;
10150 struct plt_entry *pent;
10151 asection *s, *plt;
10153 if (h->root.type == bfd_link_hash_indirect)
10154 return true;
10156 if (!h->pointer_equality_needed)
10157 return true;
10159 if (h->def_regular)
10160 return true;
10162 info = inf;
10163 htab = ppc_hash_table (info);
10164 if (htab == NULL)
10165 return false;
10167 s = htab->global_entry;
10168 plt = htab->elf.splt;
10169 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10170 if (pent->plt.offset != (bfd_vma) -1
10171 && pent->addend == 0)
10173 /* For ELFv2, if this symbol is not defined in a regular file
10174 and we are not generating a shared library or pie, then we
10175 need to define the symbol in the executable on a call stub.
10176 This is to avoid text relocations. */
10177 bfd_vma off, stub_align, stub_off, stub_size;
10178 unsigned int align_power;
10180 stub_size = 16;
10181 stub_off = s->size;
10182 if (htab->params->plt_stub_align >= 0)
10183 align_power = htab->params->plt_stub_align;
10184 else
10185 align_power = -htab->params->plt_stub_align;
10186 /* Setting section alignment is delayed until we know it is
10187 non-empty. Otherwise the .text output section will be
10188 aligned at least to plt_stub_align even when no global
10189 entry stubs are needed. */
10190 if (s->alignment_power < align_power)
10191 s->alignment_power = align_power;
10192 stub_align = (bfd_vma) 1 << align_power;
10193 if (htab->params->plt_stub_align >= 0
10194 || ((((stub_off + stub_size - 1) & -stub_align)
10195 - (stub_off & -stub_align))
10196 > ((stub_size - 1) & -stub_align)))
10197 stub_off = (stub_off + stub_align - 1) & -stub_align;
10198 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10199 off -= stub_off + s->output_offset + s->output_section->vma;
10200 /* Note that for --plt-stub-align negative we have a possible
10201 dependency between stub offset and size. Break that
10202 dependency by assuming the max stub size when calculating
10203 the stub offset. */
10204 if (PPC_HA (off) == 0)
10205 stub_size -= 4;
10206 h->root.type = bfd_link_hash_defined;
10207 h->root.u.def.section = s;
10208 h->root.u.def.value = stub_off;
10209 s->size = stub_off + stub_size;
10210 break;
10212 return true;
10215 /* Set the sizes of the dynamic sections. */
10217 static bool
10218 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10219 struct bfd_link_info *info)
10221 struct ppc_link_hash_table *htab;
10222 bfd *dynobj;
10223 asection *s;
10224 bool relocs;
10225 bfd *ibfd;
10226 struct got_entry *first_tlsld;
10228 htab = ppc_hash_table (info);
10229 if (htab == NULL)
10230 return false;
10232 dynobj = htab->elf.dynobj;
10233 if (dynobj == NULL)
10234 abort ();
10236 if (htab->elf.dynamic_sections_created)
10238 /* Set the contents of the .interp section to the interpreter. */
10239 if (bfd_link_executable (info) && !info->nointerp)
10241 s = bfd_get_linker_section (dynobj, ".interp");
10242 if (s == NULL)
10243 abort ();
10244 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10245 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10249 /* Set up .got offsets for local syms, and space for local dynamic
10250 relocs. */
10251 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10253 struct got_entry **lgot_ents;
10254 struct got_entry **end_lgot_ents;
10255 struct plt_entry **local_plt;
10256 struct plt_entry **end_local_plt;
10257 unsigned char *lgot_masks;
10258 bfd_size_type locsymcount;
10259 Elf_Internal_Shdr *symtab_hdr;
10260 Elf_Internal_Sym *local_syms;
10261 Elf_Internal_Sym *isym;
10263 if (!is_ppc64_elf (ibfd))
10264 continue;
10266 for (s = ibfd->sections; s != NULL; s = s->next)
10268 struct ppc_local_dyn_relocs *p;
10270 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10272 if (discarded_section (p->sec))
10274 /* Input section has been discarded, either because
10275 it is a copy of a linkonce section or due to
10276 linker script /DISCARD/, so we'll be discarding
10277 the relocs too. */
10279 else if (p->count != 0)
10281 unsigned int count;
10282 asection *srel;
10284 count = p->count;
10285 if (info->enable_dt_relr
10286 && ((!NO_OPD_RELOCS
10287 && (ppc64_elf_section_data (p->sec)->sec_type
10288 == sec_opd))
10289 || !p->ifunc))
10290 count -= p->rel_count;
10291 srel = elf_section_data (p->sec)->sreloc;
10292 if (p->ifunc)
10293 srel = htab->elf.irelplt;
10294 srel->size += count * sizeof (Elf64_External_Rela);
10295 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10296 info->flags |= DF_TEXTREL;
10301 lgot_ents = elf_local_got_ents (ibfd);
10302 if (!lgot_ents)
10303 continue;
10305 symtab_hdr = &elf_symtab_hdr (ibfd);
10306 locsymcount = symtab_hdr->sh_info;
10307 end_lgot_ents = lgot_ents + locsymcount;
10308 local_plt = (struct plt_entry **) end_lgot_ents;
10309 end_local_plt = local_plt + locsymcount;
10310 lgot_masks = (unsigned char *) end_local_plt;
10311 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10312 if (local_syms == NULL && locsymcount != 0)
10314 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10315 0, NULL, NULL, NULL);
10316 if (local_syms == NULL)
10317 return false;
10319 s = ppc64_elf_tdata (ibfd)->got;
10320 for (isym = local_syms;
10321 lgot_ents < end_lgot_ents;
10322 ++lgot_ents, ++lgot_masks, isym++)
10324 struct got_entry **pent, *ent;
10326 pent = lgot_ents;
10327 while ((ent = *pent) != NULL)
10328 if (ent->got.refcount > 0)
10330 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10332 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10333 *pent = ent->next;
10335 else
10337 unsigned int ent_size = 8;
10338 unsigned int rel_size = sizeof (Elf64_External_Rela);
10340 ent->got.offset = s->size;
10341 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10343 ent_size *= 2;
10344 rel_size *= 2;
10346 s->size += ent_size;
10347 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10349 htab->elf.irelplt->size += rel_size;
10350 htab->got_reli_size += rel_size;
10352 else if (bfd_link_pic (info)
10353 && (ent->tls_type == 0
10354 ? !info->enable_dt_relr
10355 : !bfd_link_executable (info))
10356 && isym->st_shndx != SHN_ABS)
10358 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10359 srel->size += rel_size;
10361 pent = &ent->next;
10364 else
10365 *pent = ent->next;
10367 if (local_syms != NULL
10368 && symtab_hdr->contents != (unsigned char *) local_syms)
10370 if (!info->keep_memory)
10371 free (local_syms);
10372 else
10373 symtab_hdr->contents = (unsigned char *) local_syms;
10376 /* Allocate space for plt calls to local syms. */
10377 lgot_masks = (unsigned char *) end_local_plt;
10378 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10380 struct plt_entry *ent;
10382 for (ent = *local_plt; ent != NULL; ent = ent->next)
10383 if (ent->plt.refcount > 0)
10385 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10387 s = htab->elf.iplt;
10388 ent->plt.offset = s->size;
10389 s->size += PLT_ENTRY_SIZE (htab);
10390 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10392 else if (htab->can_convert_all_inline_plt
10393 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10394 ent->plt.offset = (bfd_vma) -1;
10395 else
10397 s = htab->pltlocal;
10398 ent->plt.offset = s->size;
10399 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10400 if (bfd_link_pic (info)
10401 && !(info->enable_dt_relr && !htab->opd_abi))
10402 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10405 else
10406 ent->plt.offset = (bfd_vma) -1;
10410 /* Allocate global sym .plt and .got entries, and space for global
10411 sym dynamic relocs. */
10412 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10414 if (!htab->opd_abi && !bfd_link_pic (info))
10415 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10417 first_tlsld = NULL;
10418 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10420 struct got_entry *ent;
10422 if (!is_ppc64_elf (ibfd))
10423 continue;
10425 ent = ppc64_tlsld_got (ibfd);
10426 if (ent->got.refcount > 0)
10428 if (!htab->do_multi_toc && first_tlsld != NULL)
10430 ent->is_indirect = true;
10431 ent->got.ent = first_tlsld;
10433 else
10435 if (first_tlsld == NULL)
10436 first_tlsld = ent;
10437 s = ppc64_elf_tdata (ibfd)->got;
10438 ent->got.offset = s->size;
10439 ent->owner = ibfd;
10440 s->size += 16;
10441 if (bfd_link_dll (info))
10443 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10444 srel->size += sizeof (Elf64_External_Rela);
10448 else
10449 ent->got.offset = (bfd_vma) -1;
10452 /* We now have determined the sizes of the various dynamic sections.
10453 Allocate memory for them. */
10454 relocs = false;
10455 for (s = dynobj->sections; s != NULL; s = s->next)
10457 if ((s->flags & SEC_LINKER_CREATED) == 0)
10458 continue;
10460 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10461 /* These haven't been allocated yet; don't strip. */
10462 continue;
10463 else if (s == htab->elf.sgot
10464 || s == htab->elf.splt
10465 || s == htab->elf.iplt
10466 || s == htab->pltlocal
10467 || s == htab->glink
10468 || s == htab->global_entry
10469 || s == htab->elf.sdynbss
10470 || s == htab->elf.sdynrelro)
10472 /* Strip this section if we don't need it; see the
10473 comment below. */
10475 else if (s == htab->glink_eh_frame)
10477 if (!bfd_is_abs_section (s->output_section))
10478 /* Not sized yet. */
10479 continue;
10481 else if (startswith (s->name, ".rela"))
10483 if (s->size != 0)
10485 if (s != htab->elf.srelplt)
10486 relocs = true;
10488 /* We use the reloc_count field as a counter if we need
10489 to copy relocs into the output file. */
10490 s->reloc_count = 0;
10493 else
10495 /* It's not one of our sections, so don't allocate space. */
10496 continue;
10499 if (s->size == 0)
10501 /* If we don't need this section, strip it from the
10502 output file. This is mostly to handle .rela.bss and
10503 .rela.plt. We must create both sections in
10504 create_dynamic_sections, because they must be created
10505 before the linker maps input sections to output
10506 sections. The linker does that before
10507 adjust_dynamic_symbol is called, and it is that
10508 function which decides whether anything needs to go
10509 into these sections. */
10510 s->flags |= SEC_EXCLUDE;
10511 continue;
10514 if (bfd_is_abs_section (s->output_section))
10515 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10516 s->name);
10518 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10519 continue;
10521 /* Allocate memory for the section contents. We use bfd_zalloc
10522 here in case unused entries are not reclaimed before the
10523 section's contents are written out. This should not happen,
10524 but this way if it does we get a R_PPC64_NONE reloc in .rela
10525 sections instead of garbage.
10526 We also rely on the section contents being zero when writing
10527 the GOT and .dynrelro. */
10528 s->contents = bfd_zalloc (dynobj, s->size);
10529 if (s->contents == NULL)
10530 return false;
10533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10535 if (!is_ppc64_elf (ibfd))
10536 continue;
10538 s = ppc64_elf_tdata (ibfd)->got;
10539 if (s != NULL && s != htab->elf.sgot)
10541 if (s->size == 0)
10542 s->flags |= SEC_EXCLUDE;
10543 else
10545 s->contents = bfd_zalloc (ibfd, s->size);
10546 if (s->contents == NULL)
10547 return false;
10550 s = ppc64_elf_tdata (ibfd)->relgot;
10551 if (s != NULL)
10553 if (s->size == 0)
10554 s->flags |= SEC_EXCLUDE;
10555 else
10557 s->contents = bfd_zalloc (ibfd, s->size);
10558 if (s->contents == NULL)
10559 return false;
10560 relocs = true;
10561 s->reloc_count = 0;
10566 if (htab->elf.dynamic_sections_created)
10568 bool tls_opt;
10570 /* Add some entries to the .dynamic section. We fill in the
10571 values later, in ppc64_elf_finish_dynamic_sections, but we
10572 must add the entries now so that we get the correct size for
10573 the .dynamic section. The DT_DEBUG entry is filled in by the
10574 dynamic linker and used by the debugger. */
10575 #define add_dynamic_entry(TAG, VAL) \
10576 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10578 if (bfd_link_executable (info))
10580 if (!add_dynamic_entry (DT_DEBUG, 0))
10581 return false;
10584 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10586 if (!add_dynamic_entry (DT_PLTGOT, 0)
10587 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10588 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10589 || !add_dynamic_entry (DT_JMPREL, 0)
10590 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10591 return false;
10594 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10596 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10597 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10598 return false;
10601 tls_opt = (htab->params->tls_get_addr_opt
10602 && ((htab->tls_get_addr_fd != NULL
10603 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10604 || (htab->tga_desc_fd != NULL
10605 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10606 if (tls_opt || !htab->opd_abi)
10608 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10609 return false;
10612 if (relocs)
10614 if (!add_dynamic_entry (DT_RELA, 0)
10615 || !add_dynamic_entry (DT_RELASZ, 0)
10616 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10617 return false;
10619 /* If any dynamic relocs apply to a read-only section,
10620 then we need a DT_TEXTREL entry. */
10621 if ((info->flags & DF_TEXTREL) == 0)
10622 elf_link_hash_traverse (&htab->elf,
10623 _bfd_elf_maybe_set_textrel, info);
10625 if ((info->flags & DF_TEXTREL) != 0)
10627 if (!add_dynamic_entry (DT_TEXTREL, 0))
10628 return false;
10632 #undef add_dynamic_entry
10634 return true;
10637 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10639 static bool
10640 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10642 if (h->plt.plist != NULL
10643 && !h->def_regular
10644 && !h->pointer_equality_needed)
10645 return false;
10647 return _bfd_elf_hash_symbol (h);
10650 /* Determine the type of stub needed, if any, for a call. */
10652 static inline enum ppc_stub_main_type
10653 ppc_type_of_stub (asection *input_sec,
10654 const Elf_Internal_Rela *rel,
10655 struct ppc_link_hash_entry **hash,
10656 struct plt_entry **plt_ent,
10657 bfd_vma destination,
10658 unsigned long local_off)
10660 struct ppc_link_hash_entry *h = *hash;
10661 bfd_vma location;
10662 bfd_vma branch_offset;
10663 bfd_vma max_branch_offset;
10664 enum elf_ppc64_reloc_type r_type;
10666 if (h != NULL)
10668 struct plt_entry *ent;
10669 struct ppc_link_hash_entry *fdh = h;
10670 if (h->oh != NULL
10671 && h->oh->is_func_descriptor)
10673 fdh = ppc_follow_link (h->oh);
10674 *hash = fdh;
10677 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10678 if (ent->addend == rel->r_addend
10679 && ent->plt.offset != (bfd_vma) -1)
10681 *plt_ent = ent;
10682 return ppc_stub_plt_call;
10685 /* Here, we know we don't have a plt entry. If we don't have a
10686 either a defined function descriptor or a defined entry symbol
10687 in a regular object file, then it is pointless trying to make
10688 any other type of stub. */
10689 if (!is_static_defined (&fdh->elf)
10690 && !is_static_defined (&h->elf))
10691 return ppc_stub_none;
10693 else if (elf_local_got_ents (input_sec->owner) != NULL)
10695 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10696 struct plt_entry **local_plt = (struct plt_entry **)
10697 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10698 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10700 if (local_plt[r_symndx] != NULL)
10702 struct plt_entry *ent;
10704 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10705 if (ent->addend == rel->r_addend
10706 && ent->plt.offset != (bfd_vma) -1)
10708 *plt_ent = ent;
10709 return ppc_stub_plt_call;
10714 /* Determine where the call point is. */
10715 location = (input_sec->output_offset
10716 + input_sec->output_section->vma
10717 + rel->r_offset);
10719 branch_offset = destination - location;
10720 r_type = ELF64_R_TYPE (rel->r_info);
10722 /* Determine if a long branch stub is needed. */
10723 max_branch_offset = 1 << 25;
10724 if (r_type == R_PPC64_REL14
10725 || r_type == R_PPC64_REL14_BRTAKEN
10726 || r_type == R_PPC64_REL14_BRNTAKEN)
10727 max_branch_offset = 1 << 15;
10729 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10730 /* We need a stub. Figure out whether a long_branch or plt_branch
10731 is needed later. */
10732 return ppc_stub_long_branch;
10734 return ppc_stub_none;
10737 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10738 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10739 . mflr %r12
10740 . bcl 20,31,1f
10741 .1: mflr %r11
10742 . mtlr %r12
10743 . lis %r12,xxx-1b@highest
10744 . ori %r12,%r12,xxx-1b@higher
10745 . sldi %r12,%r12,32
10746 . oris %r12,%r12,xxx-1b@high
10747 . ori %r12,%r12,xxx-1b@l
10748 . add/ldx %r12,%r11,%r12 */
10750 static bfd_byte *
10751 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10753 bfd_put_32 (abfd, MFLR_R12, p);
10754 p += 4;
10755 bfd_put_32 (abfd, BCL_20_31, p);
10756 p += 4;
10757 bfd_put_32 (abfd, MFLR_R11, p);
10758 p += 4;
10759 bfd_put_32 (abfd, MTLR_R12, p);
10760 p += 4;
10761 if (off + 0x8000 < 0x10000)
10763 if (load)
10764 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10765 else
10766 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10767 p += 4;
10769 else if (off + 0x80008000ULL < 0x100000000ULL)
10771 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10772 p += 4;
10773 if (load)
10774 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10775 else
10776 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10777 p += 4;
10779 else
10781 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10783 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10784 p += 4;
10786 else
10788 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10789 p += 4;
10790 if (((off >> 32) & 0xffff) != 0)
10792 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10793 p += 4;
10796 if (((off >> 32) & 0xffffffffULL) != 0)
10798 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10799 p += 4;
10801 if (PPC_HI (off) != 0)
10803 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10804 p += 4;
10806 if (PPC_LO (off) != 0)
10808 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10809 p += 4;
10811 if (load)
10812 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10813 else
10814 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10815 p += 4;
10817 return p;
10820 static unsigned int
10821 size_offset (bfd_vma off)
10823 unsigned int size;
10824 if (off + 0x8000 < 0x10000)
10825 size = 4;
10826 else if (off + 0x80008000ULL < 0x100000000ULL)
10827 size = 8;
10828 else
10830 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10831 size = 4;
10832 else
10834 size = 4;
10835 if (((off >> 32) & 0xffff) != 0)
10836 size += 4;
10838 if (((off >> 32) & 0xffffffffULL) != 0)
10839 size += 4;
10840 if (PPC_HI (off) != 0)
10841 size += 4;
10842 if (PPC_LO (off) != 0)
10843 size += 4;
10844 size += 4;
10846 return size + 16;
10849 static unsigned int
10850 num_relocs_for_offset (bfd_vma off)
10852 unsigned int num_rel;
10853 if (off + 0x8000 < 0x10000)
10854 num_rel = 1;
10855 else if (off + 0x80008000ULL < 0x100000000ULL)
10856 num_rel = 2;
10857 else
10859 num_rel = 1;
10860 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10861 && ((off >> 32) & 0xffff) != 0)
10862 num_rel += 1;
10863 if (PPC_HI (off) != 0)
10864 num_rel += 1;
10865 if (PPC_LO (off) != 0)
10866 num_rel += 1;
10868 return num_rel;
10871 static Elf_Internal_Rela *
10872 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10873 bfd_vma roff, bfd_vma targ, bfd_vma off)
10875 bfd_vma relative_targ = targ - (roff - 8);
10876 if (bfd_big_endian (info->output_bfd))
10877 roff += 2;
10878 r->r_offset = roff;
10879 r->r_addend = relative_targ + roff;
10880 if (off + 0x8000 < 0x10000)
10881 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10882 else if (off + 0x80008000ULL < 0x100000000ULL)
10884 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10885 ++r;
10886 roff += 4;
10887 r->r_offset = roff;
10888 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10889 r->r_addend = relative_targ + roff;
10891 else
10893 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10894 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10895 else
10897 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10898 if (((off >> 32) & 0xffff) != 0)
10900 ++r;
10901 roff += 4;
10902 r->r_offset = roff;
10903 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10904 r->r_addend = relative_targ + roff;
10907 if (((off >> 32) & 0xffffffffULL) != 0)
10908 roff += 4;
10909 if (PPC_HI (off) != 0)
10911 ++r;
10912 roff += 4;
10913 r->r_offset = roff;
10914 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10915 r->r_addend = relative_targ + roff;
10917 if (PPC_LO (off) != 0)
10919 ++r;
10920 roff += 4;
10921 r->r_offset = roff;
10922 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10923 r->r_addend = relative_targ + roff;
10926 return r;
10929 static bfd_byte *
10930 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10931 bool load)
10933 uint64_t insn;
10934 if (off - odd + (1ULL << 33) < 1ULL << 34)
10936 off -= odd;
10937 if (odd)
10939 bfd_put_32 (abfd, NOP, p);
10940 p += 4;
10942 if (load)
10943 insn = PLD_R12_PC;
10944 else
10945 insn = PADDI_R12_PC;
10946 insn |= D34 (off);
10947 bfd_put_32 (abfd, insn >> 32, p);
10948 p += 4;
10949 bfd_put_32 (abfd, insn, p);
10951 /* The minimum value for paddi is -0x200000000. The minimum value
10952 for li is -0x8000, which when shifted by 34 and added gives a
10953 minimum value of -0x2000200000000. The maximum value is
10954 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10955 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10957 off -= 8 - odd;
10958 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10959 p += 4;
10960 if (!odd)
10962 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10963 p += 4;
10965 insn = PADDI_R12_PC | D34 (off);
10966 bfd_put_32 (abfd, insn >> 32, p);
10967 p += 4;
10968 bfd_put_32 (abfd, insn, p);
10969 p += 4;
10970 if (odd)
10972 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10973 p += 4;
10975 if (load)
10976 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10977 else
10978 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10980 else
10982 off -= odd + 8;
10983 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10984 p += 4;
10985 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10986 p += 4;
10987 if (odd)
10989 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10990 p += 4;
10992 insn = PADDI_R12_PC | D34 (off);
10993 bfd_put_32 (abfd, insn >> 32, p);
10994 p += 4;
10995 bfd_put_32 (abfd, insn, p);
10996 p += 4;
10997 if (!odd)
10999 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11000 p += 4;
11002 if (load)
11003 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11004 else
11005 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11007 p += 4;
11008 return p;
11011 static unsigned int
11012 size_power10_offset (bfd_vma off, int odd)
11014 if (off - odd + (1ULL << 33) < 1ULL << 34)
11015 return odd + 8;
11016 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11017 return 20;
11018 else
11019 return 24;
11022 static unsigned int
11023 num_relocs_for_power10_offset (bfd_vma off, int odd)
11025 if (off - odd + (1ULL << 33) < 1ULL << 34)
11026 return 1;
11027 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11028 return 2;
11029 else
11030 return 3;
11033 static Elf_Internal_Rela *
11034 emit_relocs_for_power10_offset (struct bfd_link_info *info,
11035 Elf_Internal_Rela *r, bfd_vma roff,
11036 bfd_vma targ, bfd_vma off, int odd)
11038 if (off - odd + (1ULL << 33) < 1ULL << 34)
11039 roff += odd;
11040 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11042 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11043 r->r_offset = roff + d_offset;
11044 r->r_addend = targ + 8 - odd - d_offset;
11045 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11046 ++r;
11047 roff += 8 - odd;
11049 else
11051 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11052 r->r_offset = roff + d_offset;
11053 r->r_addend = targ + 8 + odd - d_offset;
11054 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11055 ++r;
11056 roff += 4;
11057 r->r_offset = roff + d_offset;
11058 r->r_addend = targ + 4 + odd - d_offset;
11059 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11060 ++r;
11061 roff += 4 + odd;
11063 r->r_offset = roff;
11064 r->r_addend = targ;
11065 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11066 return r;
11069 /* Emit .eh_frame opcode to advance pc by DELTA. */
11071 static bfd_byte *
11072 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11074 delta /= 4;
11075 if (delta < 64)
11076 *eh++ = DW_CFA_advance_loc + delta;
11077 else if (delta < 256)
11079 *eh++ = DW_CFA_advance_loc1;
11080 *eh++ = delta;
11082 else if (delta < 65536)
11084 *eh++ = DW_CFA_advance_loc2;
11085 bfd_put_16 (abfd, delta, eh);
11086 eh += 2;
11088 else
11090 *eh++ = DW_CFA_advance_loc4;
11091 bfd_put_32 (abfd, delta, eh);
11092 eh += 4;
11094 return eh;
11097 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11099 static unsigned int
11100 eh_advance_size (unsigned int delta)
11102 if (delta < 64 * 4)
11103 /* DW_CFA_advance_loc+[1..63]. */
11104 return 1;
11105 if (delta < 256 * 4)
11106 /* DW_CFA_advance_loc1, byte. */
11107 return 2;
11108 if (delta < 65536 * 4)
11109 /* DW_CFA_advance_loc2, 2 bytes. */
11110 return 3;
11111 /* DW_CFA_advance_loc4, 4 bytes. */
11112 return 5;
11115 /* With power7 weakly ordered memory model, it is possible for ld.so
11116 to update a plt entry in one thread and have another thread see a
11117 stale zero toc entry. To avoid this we need some sort of acquire
11118 barrier in the call stub. One solution is to make the load of the
11119 toc word seem to appear to depend on the load of the function entry
11120 word. Another solution is to test for r2 being zero, and branch to
11121 the appropriate glink entry if so.
11123 . fake dep barrier compare
11124 . ld 12,xxx(2) ld 12,xxx(2)
11125 . mtctr 12 mtctr 12
11126 . xor 11,12,12 ld 2,xxx+8(2)
11127 . add 2,2,11 cmpldi 2,0
11128 . ld 2,xxx+8(2) bnectr+
11129 . bctr b <glink_entry>
11131 The solution involving the compare turns out to be faster, so
11132 that's what we use unless the branch won't reach. */
11134 #define ALWAYS_USE_FAKE_DEP 0
11135 #define ALWAYS_EMIT_R2SAVE 0
11137 static inline unsigned int
11138 plt_stub_size (struct ppc_link_hash_table *htab,
11139 struct ppc_stub_hash_entry *stub_entry,
11140 bfd_vma off,
11141 unsigned int odd)
11143 unsigned size;
11145 if (stub_entry->type.sub == ppc_stub_notoc)
11147 size = 8 + size_power10_offset (off, odd);
11148 if (stub_entry->type.r2save)
11149 size += 4;
11151 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11153 size = 8 + size_offset (off - 8);
11154 if (stub_entry->type.r2save)
11155 size += 4;
11157 else
11159 size = 12;
11160 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11161 size += 4;
11162 if (PPC_HA (off) != 0)
11163 size += 4;
11164 if (htab->opd_abi)
11166 size += 4;
11167 if (htab->params->plt_static_chain)
11168 size += 4;
11169 if (htab->params->plt_thread_safe
11170 && htab->elf.dynamic_sections_created
11171 && stub_entry->h != NULL
11172 && stub_entry->h->elf.dynindx != -1)
11173 size += 8;
11174 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11175 != PPC_HA (off))
11176 size += 4;
11179 if (stub_entry->h != NULL
11180 && is_tls_get_addr (&stub_entry->h->elf, htab)
11181 && htab->params->tls_get_addr_opt)
11183 if (!htab->params->no_tls_get_addr_regsave)
11185 size += 30 * 4;
11186 if (stub_entry->type.r2save)
11187 size += 4;
11189 else
11191 size += 7 * 4;
11192 if (stub_entry->type.r2save)
11193 size += 6 * 4;
11196 return size;
11199 /* Depending on the sign of plt_stub_align:
11200 If positive, return the padding to align to a 2**plt_stub_align
11201 boundary.
11202 If negative, if this stub would cross fewer 2**plt_stub_align
11203 boundaries if we align, then return the padding needed to do so. */
11205 static inline unsigned int
11206 plt_stub_pad (int plt_stub_align,
11207 bfd_vma stub_off,
11208 unsigned int stub_size)
11210 unsigned int stub_align;
11212 if (plt_stub_align >= 0)
11213 stub_align = 1u << plt_stub_align;
11214 else
11216 stub_align = 1u << -plt_stub_align;
11217 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11218 <= ((stub_size - 1) & -stub_align))
11219 return 0;
11221 return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
11224 /* Build a toc using .plt call stub. */
11226 static inline bfd_byte *
11227 build_plt_stub (struct ppc_link_hash_table *htab,
11228 struct ppc_stub_hash_entry *stub_entry,
11229 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11231 bfd *obfd = htab->params->stub_bfd;
11232 bool plt_load_toc = htab->opd_abi;
11233 bool plt_static_chain = htab->params->plt_static_chain;
11234 bool plt_thread_safe = (htab->params->plt_thread_safe
11235 && htab->elf.dynamic_sections_created
11236 && stub_entry->h != NULL
11237 && stub_entry->h->elf.dynindx != -1);
11238 bool use_fake_dep = plt_thread_safe;
11239 bfd_vma cmp_branch_off = 0;
11241 if (!ALWAYS_USE_FAKE_DEP
11242 && plt_load_toc
11243 && plt_thread_safe
11244 && !(stub_entry->h != NULL
11245 && is_tls_get_addr (&stub_entry->h->elf, htab)
11246 && htab->params->tls_get_addr_opt))
11248 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11249 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11250 / PLT_ENTRY_SIZE (htab));
11251 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11252 bfd_vma to, from;
11254 if (pltindex > 32768)
11255 glinkoff += (pltindex - 32768) * 4;
11256 to = (glinkoff
11257 + htab->glink->output_offset
11258 + htab->glink->output_section->vma);
11259 from = (p - stub_entry->group->stub_sec->contents
11260 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11261 + 4 * (PPC_HA (offset) != 0)
11262 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11263 != PPC_HA (offset))
11264 + 4 * (plt_static_chain != 0)
11265 + 20
11266 + stub_entry->group->stub_sec->output_offset
11267 + stub_entry->group->stub_sec->output_section->vma);
11268 cmp_branch_off = to - from;
11269 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11272 if (PPC_HA (offset) != 0)
11274 if (r != NULL)
11276 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11277 r[0].r_offset += 4;
11278 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11279 r[1].r_offset = r[0].r_offset + 4;
11280 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11281 r[1].r_addend = r[0].r_addend;
11282 if (plt_load_toc)
11284 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11286 r[2].r_offset = r[1].r_offset + 4;
11287 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11288 r[2].r_addend = r[0].r_addend;
11290 else
11292 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11293 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11294 r[2].r_addend = r[0].r_addend + 8;
11295 if (plt_static_chain)
11297 r[3].r_offset = r[2].r_offset + 4;
11298 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11299 r[3].r_addend = r[0].r_addend + 16;
11304 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11305 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11306 if (plt_load_toc)
11308 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11309 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11311 else
11313 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11314 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11316 if (plt_load_toc
11317 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11319 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11320 offset = 0;
11322 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11323 if (plt_load_toc)
11325 if (use_fake_dep)
11327 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11328 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11330 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11331 if (plt_static_chain)
11332 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11335 else
11337 if (r != NULL)
11339 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11340 r[0].r_offset += 4;
11341 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11342 if (plt_load_toc)
11344 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11346 r[1].r_offset = r[0].r_offset + 4;
11347 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11348 r[1].r_addend = r[0].r_addend;
11350 else
11352 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11353 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11354 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11355 if (plt_static_chain)
11357 r[2].r_offset = r[1].r_offset + 4;
11358 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11359 r[2].r_addend = r[0].r_addend + 8;
11364 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11365 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11366 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11367 if (plt_load_toc
11368 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11370 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11371 offset = 0;
11373 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11374 if (plt_load_toc)
11376 if (use_fake_dep)
11378 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11379 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11381 if (plt_static_chain)
11382 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11383 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11386 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11388 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11389 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11390 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11392 else
11393 bfd_put_32 (obfd, BCTR, p), p += 4;
11394 return p;
11397 /* Build a special .plt call stub for __tls_get_addr. */
11399 #define LD_R0_0R3 0xe8030000
11400 #define LD_R12_0R3 0xe9830000
11401 #define MR_R0_R3 0x7c601b78
11402 #define CMPDI_R0_0 0x2c200000
11403 #define ADD_R3_R12_R13 0x7c6c6a14
11404 #define BEQLR 0x4d820020
11405 #define MR_R3_R0 0x7c030378
11406 #define BCTRL 0x4e800421
11408 static bfd_byte *
11409 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11410 struct ppc_stub_hash_entry *stub_entry,
11411 bfd_byte *p)
11413 bfd *obfd = htab->params->stub_bfd;
11415 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11416 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11417 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11418 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11419 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11420 bfd_put_32 (obfd, BEQLR, p), p += 4;
11421 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11423 if (!htab->params->no_tls_get_addr_regsave)
11424 p = tls_get_addr_prologue (obfd, p, htab);
11425 else if (stub_entry->type.r2save)
11427 bfd_put_32 (obfd, MFLR_R0, p);
11428 p += 4;
11429 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11430 p += 4;
11432 return p;
11435 static bfd_byte *
11436 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11437 struct ppc_stub_hash_entry *stub_entry,
11438 bfd_byte *p,
11439 bfd_byte *loc)
11441 bfd *obfd = htab->params->stub_bfd;
11443 if (!htab->params->no_tls_get_addr_regsave)
11445 bfd_put_32 (obfd, BCTRL, p - 4);
11447 if (stub_entry->type.r2save)
11449 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11450 p += 4;
11452 p = tls_get_addr_epilogue (obfd, p, htab);
11454 else if (stub_entry->type.r2save)
11456 bfd_put_32 (obfd, BCTRL, p - 4);
11458 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11459 p += 4;
11460 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11461 p += 4;
11462 bfd_put_32 (obfd, MTLR_R0, p);
11463 p += 4;
11464 bfd_put_32 (obfd, BLR, p);
11465 p += 4;
11468 if (htab->glink_eh_frame != NULL
11469 && htab->glink_eh_frame->size != 0)
11471 bfd_byte *base, *eh;
11473 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11474 eh = base + stub_entry->group->eh_size;
11476 if (!htab->params->no_tls_get_addr_regsave)
11478 unsigned int cfa_updt, delta, i;
11480 /* After the bctrl, lr has been modified so we need to emit
11481 .eh_frame info saying the return address is on the stack. In
11482 fact we must put the EH info at or before the call rather
11483 than after it, because the EH info for a call needs to be
11484 specified by that point.
11485 See libgcc/unwind-dw2.c execute_cfa_program.
11486 Any stack pointer update must be described immediately after
11487 the instruction making the change, and since the stdu occurs
11488 after saving regs we put all the reg saves and the cfa
11489 change there. */
11490 cfa_updt = stub_entry->stub_offset + 18 * 4;
11491 delta = cfa_updt - stub_entry->group->lr_restore;
11492 stub_entry->group->lr_restore
11493 = stub_entry->stub_offset + (p - loc) - 4;
11494 eh = eh_advance (htab->elf.dynobj, eh, delta);
11495 *eh++ = DW_CFA_def_cfa_offset;
11496 if (htab->opd_abi)
11498 *eh++ = 128;
11499 *eh++ = 1;
11501 else
11502 *eh++ = 96;
11503 *eh++ = DW_CFA_offset_extended_sf;
11504 *eh++ = 65;
11505 *eh++ = (-16 / 8) & 0x7f;
11506 for (i = 4; i < 12; i++)
11508 *eh++ = DW_CFA_offset + i;
11509 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11511 *eh++ = (DW_CFA_advance_loc
11512 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11513 *eh++ = DW_CFA_def_cfa_offset;
11514 *eh++ = 0;
11515 for (i = 4; i < 12; i++)
11516 *eh++ = DW_CFA_restore + i;
11517 *eh++ = DW_CFA_advance_loc + 2;
11518 *eh++ = DW_CFA_restore_extended;
11519 *eh++ = 65;
11520 stub_entry->group->eh_size = eh - base;
11522 else if (stub_entry->type.r2save)
11524 unsigned int lr_used, delta;
11526 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11527 delta = lr_used - stub_entry->group->lr_restore;
11528 stub_entry->group->lr_restore = lr_used + 16;
11529 eh = eh_advance (htab->elf.dynobj, eh, delta);
11530 *eh++ = DW_CFA_offset_extended_sf;
11531 *eh++ = 65;
11532 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11533 *eh++ = DW_CFA_advance_loc + 4;
11534 *eh++ = DW_CFA_restore_extended;
11535 *eh++ = 65;
11536 stub_entry->group->eh_size = eh - base;
11539 return p;
11542 static Elf_Internal_Rela *
11543 get_relocs (asection *sec, int count)
11545 Elf_Internal_Rela *relocs;
11546 struct bfd_elf_section_data *elfsec_data;
11548 elfsec_data = elf_section_data (sec);
11549 relocs = elfsec_data->relocs;
11550 if (relocs == NULL)
11552 bfd_size_type relsize;
11553 relsize = sec->reloc_count * sizeof (*relocs);
11554 relocs = bfd_alloc (sec->owner, relsize);
11555 if (relocs == NULL)
11556 return NULL;
11557 elfsec_data->relocs = relocs;
11558 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11559 sizeof (Elf_Internal_Shdr));
11560 if (elfsec_data->rela.hdr == NULL)
11561 return NULL;
11562 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11563 * sizeof (Elf64_External_Rela));
11564 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11565 sec->reloc_count = 0;
11567 relocs += sec->reloc_count;
11568 sec->reloc_count += count;
11569 return relocs;
11572 static bool
11573 swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
11575 if ((size_t) (loc - s->contents) >= s->size)
11576 return false;
11577 bfd_elf64_swap_reloca_out (obfd, rel, loc);
11578 return true;
11581 static bool
11582 count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
11584 bfd_byte *loc = s->contents;
11585 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
11586 return swap_reloc_out (obfd, rel, loc, s);
11590 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11591 forms, to the equivalent relocs against the global symbol given by
11592 STUB_ENTRY->H. */
11594 static bool
11595 use_global_in_relocs (struct ppc_link_hash_table *htab,
11596 struct ppc_stub_hash_entry *stub_entry,
11597 Elf_Internal_Rela *r, unsigned int num_rel)
11599 struct elf_link_hash_entry **hashes;
11600 unsigned long symndx;
11601 struct ppc_link_hash_entry *h;
11602 bfd_vma symval;
11604 /* Relocs are always against symbols in their own object file. Fake
11605 up global sym hashes for the stub bfd (which has no symbols). */
11606 hashes = elf_sym_hashes (htab->params->stub_bfd);
11607 if (hashes == NULL)
11609 bfd_size_type hsize;
11611 /* When called the first time, stub_globals will contain the
11612 total number of symbols seen during stub sizing. After
11613 allocating, stub_globals is used as an index to fill the
11614 hashes array. */
11615 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11616 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11617 if (hashes == NULL)
11618 return false;
11619 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11620 htab->stub_globals = 1;
11622 symndx = htab->stub_globals++;
11623 h = stub_entry->h;
11624 hashes[symndx] = &h->elf;
11625 if (h->oh != NULL && h->oh->is_func)
11626 h = ppc_follow_link (h->oh);
11627 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11628 || h->elf.root.type == bfd_link_hash_defweak);
11629 symval = defined_sym_val (&h->elf);
11630 while (num_rel-- != 0)
11632 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11633 if (h->elf.root.u.def.section != stub_entry->target_section)
11635 /* H is an opd symbol. The addend must be zero, and the
11636 branch reloc is the only one we can convert. */
11637 r->r_addend = 0;
11638 break;
11640 else
11641 r->r_addend -= symval;
11642 --r;
11644 return true;
11647 static bfd_vma
11648 get_r2off (struct bfd_link_info *info,
11649 struct ppc_stub_hash_entry *stub_entry)
11651 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11652 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11654 if (r2off == 0)
11656 /* Support linking -R objects. Get the toc pointer from the
11657 opd entry. */
11658 char buf[8];
11659 if (!htab->opd_abi)
11660 return r2off;
11661 asection *opd = stub_entry->h->elf.root.u.def.section;
11662 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11664 if (strcmp (opd->name, ".opd") != 0
11665 || opd->reloc_count != 0)
11667 info->callbacks->einfo
11668 (_("%P: cannot find opd entry toc for `%pT'\n"),
11669 stub_entry->h->elf.root.root.string);
11670 bfd_set_error (bfd_error_bad_value);
11671 return (bfd_vma) -1;
11673 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11674 return (bfd_vma) -1;
11675 r2off = bfd_get_64 (opd->owner, buf);
11676 r2off -= elf_gp (info->output_bfd);
11678 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11679 return r2off;
11682 /* Debug dump. */
11684 static void
11685 dump_stub (const char *header,
11686 struct ppc_stub_hash_entry *stub_entry,
11687 size_t end_offset)
11689 const char *t1, *t2, *t3;
11690 switch (stub_entry->type.main)
11692 case ppc_stub_none: t1 = "none"; break;
11693 case ppc_stub_long_branch: t1 = "long_branch"; break;
11694 case ppc_stub_plt_branch: t1 = "plt_branch"; break;
11695 case ppc_stub_plt_call: t1 = "plt_call"; break;
11696 case ppc_stub_global_entry: t1 = "global_entry"; break;
11697 case ppc_stub_save_res: t1 = "save_res"; break;
11698 default: t1 = "???"; break;
11700 switch (stub_entry->type.sub)
11702 case ppc_stub_toc: t2 = "toc"; break;
11703 case ppc_stub_notoc: t2 = "notoc"; break;
11704 case ppc_stub_p9notoc: t2 = "p9notoc"; break;
11705 default: t2 = "???"; break;
11707 t3 = stub_entry->type.r2save ? "r2save" : "";
11708 fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11709 header, stub_entry->id, t1, t2, t3);
11710 fprintf (stderr, "name = %s\n", stub_entry->root.string);
11711 fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
11712 for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11714 asection *stub_sec = stub_entry->group->stub_sec;
11715 uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11716 fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
11718 fprintf (stderr, "\n");
11721 static bool
11722 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11724 struct ppc_stub_hash_entry *stub_entry;
11725 struct ppc_branch_hash_entry *br_entry;
11726 struct bfd_link_info *info;
11727 struct ppc_link_hash_table *htab;
11728 bfd *obfd;
11729 bfd_byte *loc;
11730 bfd_byte *p, *relp;
11731 bfd_vma targ, off;
11732 Elf_Internal_Rela *r;
11733 asection *plt;
11734 int num_rel;
11735 int odd;
11736 bool is_tga;
11738 /* Massage our args to the form they really have. */
11739 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11740 info = in_arg;
11742 htab = ppc_hash_table (info);
11743 if (htab == NULL)
11744 return false;
11746 struct _ppc64_elf_section_data *esd
11747 = ppc64_elf_section_data (stub_entry->group->stub_sec);
11748 ++htab->stub_id;
11749 if (stub_entry->id != htab->stub_id
11750 || (stub_entry->type.main != ppc_stub_save_res
11751 && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
11753 BFD_ASSERT (0);
11754 if (stub_entry->id != htab->stub_id)
11755 fprintf (stderr, "Expected id %u, got %u\n",
11756 htab->stub_id, stub_entry->id);
11757 if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
11758 fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11759 PRIx64 "\n", stub_entry->group->stub_sec->size,
11760 stub_entry->stub_offset);
11761 if (esd->sec_type == sec_stub)
11762 dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11763 dump_stub ("Current:", stub_entry, 0);
11765 if (esd->sec_type == sec_normal)
11766 esd->sec_type = sec_stub;
11767 if (esd->sec_type == sec_stub)
11768 esd->u.last_ent = stub_entry;
11769 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11771 htab->stub_count[stub_entry->type.main - 1] += 1;
11772 if (stub_entry->type.main == ppc_stub_long_branch
11773 && stub_entry->type.sub == ppc_stub_toc)
11775 /* Branches are relative. This is where we are going to. */
11776 targ = (stub_entry->target_value
11777 + stub_entry->target_section->output_offset
11778 + stub_entry->target_section->output_section->vma);
11779 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11781 /* And this is where we are coming from. */
11782 off = (stub_entry->stub_offset
11783 + stub_entry->group->stub_sec->output_offset
11784 + stub_entry->group->stub_sec->output_section->vma);
11785 off = targ - off;
11787 p = loc;
11788 obfd = htab->params->stub_bfd;
11789 if (stub_entry->type.r2save)
11791 bfd_vma r2off = get_r2off (info, stub_entry);
11793 if (r2off == (bfd_vma) -1)
11795 htab->stub_error = true;
11796 return false;
11798 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11799 p += 4;
11800 if (PPC_HA (r2off) != 0)
11802 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11803 p += 4;
11805 if (PPC_LO (r2off) != 0)
11807 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11808 p += 4;
11810 off -= p - loc;
11812 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11813 p += 4;
11815 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11817 _bfd_error_handler
11818 (_("long branch stub `%s' offset overflow"),
11819 stub_entry->root.string);
11820 htab->stub_error = true;
11821 return false;
11824 if (info->emitrelocations)
11826 r = get_relocs (stub_entry->group->stub_sec, 1);
11827 if (r == NULL)
11828 return false;
11829 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11830 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11831 r->r_addend = targ;
11832 if (stub_entry->h != NULL
11833 && !use_global_in_relocs (htab, stub_entry, r, 1))
11834 return false;
11837 else if (stub_entry->type.main == ppc_stub_plt_branch
11838 && stub_entry->type.sub == ppc_stub_toc)
11840 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11841 stub_entry->root.string + 9,
11842 false, false);
11843 if (br_entry == NULL)
11845 _bfd_error_handler (_("can't find branch stub `%s'"),
11846 stub_entry->root.string);
11847 htab->stub_error = true;
11848 return false;
11851 targ = (stub_entry->target_value
11852 + stub_entry->target_section->output_offset
11853 + stub_entry->target_section->output_section->vma);
11854 if (!stub_entry->type.r2save)
11855 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11857 bfd_put_64 (htab->brlt->owner, targ,
11858 htab->brlt->contents + br_entry->offset);
11860 if (br_entry->iter == htab->stub_iteration)
11862 br_entry->iter = 0;
11864 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11866 /* Create a reloc for the branch lookup table entry. */
11867 Elf_Internal_Rela rela;
11869 rela.r_offset = (br_entry->offset
11870 + htab->brlt->output_offset
11871 + htab->brlt->output_section->vma);
11872 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11873 rela.r_addend = targ;
11875 BFD_ASSERT (count_and_swap_reloc_out (htab->relbrlt->owner, &rela,
11876 htab->relbrlt));
11878 else if (info->emitrelocations)
11880 r = get_relocs (htab->brlt, 1);
11881 if (r == NULL)
11882 return false;
11883 /* brlt, being SEC_LINKER_CREATED does not go through the
11884 normal reloc processing. Symbols and offsets are not
11885 translated from input file to output file form, so
11886 set up the offset per the output file. */
11887 r->r_offset = (br_entry->offset
11888 + htab->brlt->output_offset
11889 + htab->brlt->output_section->vma);
11890 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11891 r->r_addend = targ;
11895 targ = (br_entry->offset
11896 + htab->brlt->output_offset
11897 + htab->brlt->output_section->vma);
11899 off = (elf_gp (info->output_bfd)
11900 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11901 off = targ - off;
11903 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11905 info->callbacks->einfo
11906 (_("%P: linkage table error against `%pT'\n"),
11907 stub_entry->root.string);
11908 bfd_set_error (bfd_error_bad_value);
11909 htab->stub_error = true;
11910 return false;
11913 if (info->emitrelocations)
11915 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11916 if (r == NULL)
11917 return false;
11918 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11919 if (bfd_big_endian (info->output_bfd))
11920 r[0].r_offset += 2;
11921 if (stub_entry->type.r2save)
11922 r[0].r_offset += 4;
11923 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11924 r[0].r_addend = targ;
11925 if (PPC_HA (off) != 0)
11927 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11928 r[1].r_offset = r[0].r_offset + 4;
11929 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11930 r[1].r_addend = r[0].r_addend;
11934 p = loc;
11935 obfd = htab->params->stub_bfd;
11936 if (!stub_entry->type.r2save)
11938 if (PPC_HA (off) != 0)
11940 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11941 p += 4;
11942 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11944 else
11945 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11947 else
11949 bfd_vma r2off = get_r2off (info, stub_entry);
11951 if (r2off == (bfd_vma) -1)
11953 htab->stub_error = true;
11954 return false;
11957 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11958 p += 4;
11959 if (PPC_HA (off) != 0)
11961 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11962 p += 4;
11963 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11965 else
11966 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11968 if (PPC_HA (r2off) != 0)
11970 p += 4;
11971 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11973 if (PPC_LO (r2off) != 0)
11975 p += 4;
11976 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11979 p += 4;
11980 bfd_put_32 (obfd, MTCTR_R12, p);
11981 p += 4;
11982 bfd_put_32 (obfd, BCTR, p);
11983 p += 4;
11985 else if (stub_entry->type.sub >= ppc_stub_notoc)
11987 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
11988 p = loc;
11989 off = (stub_entry->stub_offset
11990 + stub_entry->group->stub_sec->output_offset
11991 + stub_entry->group->stub_sec->output_section->vma);
11992 obfd = htab->params->stub_bfd;
11993 is_tga = (is_plt
11994 && stub_entry->h != NULL
11995 && is_tls_get_addr (&stub_entry->h->elf, htab)
11996 && htab->params->tls_get_addr_opt);
11997 if (is_tga)
11999 p = build_tls_get_addr_head (htab, stub_entry, p);
12000 off += p - loc;
12002 if (stub_entry->type.r2save)
12004 off += 4;
12005 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
12006 p += 4;
12008 if (is_plt)
12010 targ = stub_entry->plt_ent->plt.offset & ~1;
12011 if (targ >= (bfd_vma) -2)
12012 abort ();
12014 plt = htab->elf.splt;
12015 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12017 if (stub_entry->symtype == STT_GNU_IFUNC)
12018 plt = htab->elf.iplt;
12019 else
12020 plt = htab->pltlocal;
12022 targ += plt->output_offset + plt->output_section->vma;
12024 else
12025 targ = (stub_entry->target_value
12026 + stub_entry->target_section->output_offset
12027 + stub_entry->target_section->output_section->vma);
12028 odd = off & 4;
12029 off = targ - off;
12031 relp = p;
12032 num_rel = 0;
12033 if (stub_entry->type.sub == ppc_stub_notoc)
12034 p = build_power10_offset (obfd, p, off, odd, is_plt);
12035 else
12037 if (htab->glink_eh_frame != NULL
12038 && htab->glink_eh_frame->size != 0)
12040 bfd_byte *base, *eh;
12041 unsigned int lr_used, delta;
12043 base = (htab->glink_eh_frame->contents
12044 + stub_entry->group->eh_base + 17);
12045 eh = base + stub_entry->group->eh_size;
12046 lr_used = stub_entry->stub_offset + (p - loc) + 8;
12047 delta = lr_used - stub_entry->group->lr_restore;
12048 stub_entry->group->lr_restore = lr_used + 8;
12049 eh = eh_advance (htab->elf.dynobj, eh, delta);
12050 *eh++ = DW_CFA_register;
12051 *eh++ = 65;
12052 *eh++ = 12;
12053 *eh++ = DW_CFA_advance_loc + 2;
12054 *eh++ = DW_CFA_restore_extended;
12055 *eh++ = 65;
12056 stub_entry->group->eh_size = eh - base;
12059 /* The notoc stubs calculate their target (either a PLT entry or
12060 the global entry point of a function) relative to the PC
12061 returned by the "bcl" two instructions past the start of the
12062 sequence emitted by build_offset. The offset is therefore 8
12063 less than calculated from the start of the sequence. */
12064 off -= 8;
12065 p = build_offset (obfd, p, off, is_plt);
12068 if (stub_entry->type.main == ppc_stub_long_branch)
12070 bfd_vma from;
12071 num_rel = 1;
12072 from = (stub_entry->stub_offset
12073 + stub_entry->group->stub_sec->output_offset
12074 + stub_entry->group->stub_sec->output_section->vma
12075 + (p - loc));
12076 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12078 else
12080 bfd_put_32 (obfd, MTCTR_R12, p);
12081 p += 4;
12082 bfd_put_32 (obfd, BCTR, p);
12084 p += 4;
12086 if (is_tga)
12087 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12089 if (info->emitrelocations)
12091 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12092 if (stub_entry->type.sub == ppc_stub_notoc)
12093 num_rel += num_relocs_for_power10_offset (off, odd);
12094 else
12096 num_rel += num_relocs_for_offset (off);
12097 roff += 16;
12099 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12100 if (r == NULL)
12101 return false;
12102 if (stub_entry->type.sub == ppc_stub_notoc)
12103 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12104 else
12105 r = emit_relocs_for_offset (info, r, roff, targ, off);
12106 if (stub_entry->type.main == ppc_stub_long_branch)
12108 ++r;
12109 roff = p - 4 - stub_entry->group->stub_sec->contents;
12110 r->r_offset = roff;
12111 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12112 r->r_addend = targ;
12113 if (stub_entry->h != NULL
12114 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12115 return false;
12119 else if (stub_entry->type.main == ppc_stub_plt_call)
12121 if (stub_entry->h != NULL
12122 && stub_entry->h->is_func_descriptor
12123 && stub_entry->h->oh != NULL)
12125 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12127 /* If the old-ABI "dot-symbol" is undefined make it weak so
12128 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12129 if (fh->elf.root.type == bfd_link_hash_undefined
12130 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12131 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12132 fh->elf.root.type = bfd_link_hash_undefweak;
12135 /* Now build the stub. */
12136 targ = stub_entry->plt_ent->plt.offset & ~1;
12137 if (targ >= (bfd_vma) -2)
12138 abort ();
12140 plt = htab->elf.splt;
12141 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12143 if (stub_entry->symtype == STT_GNU_IFUNC)
12144 plt = htab->elf.iplt;
12145 else
12146 plt = htab->pltlocal;
12148 targ += plt->output_offset + plt->output_section->vma;
12150 off = (elf_gp (info->output_bfd)
12151 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12152 off = targ - off;
12154 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12156 info->callbacks->einfo
12157 /* xgettext:c-format */
12158 (_("%P: linkage table error against `%pT'\n"),
12159 stub_entry->h != NULL
12160 ? stub_entry->h->elf.root.root.string
12161 : "<local sym>");
12162 bfd_set_error (bfd_error_bad_value);
12163 htab->stub_error = true;
12164 return false;
12167 r = NULL;
12168 if (info->emitrelocations)
12170 r = get_relocs (stub_entry->group->stub_sec,
12171 ((PPC_HA (off) != 0)
12172 + (htab->opd_abi
12173 ? 2 + (htab->params->plt_static_chain
12174 && PPC_HA (off + 16) == PPC_HA (off))
12175 : 1)));
12176 if (r == NULL)
12177 return false;
12178 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12179 if (bfd_big_endian (info->output_bfd))
12180 r[0].r_offset += 2;
12181 r[0].r_addend = targ;
12183 p = loc;
12184 obfd = htab->params->stub_bfd;
12185 is_tga = (stub_entry->h != NULL
12186 && is_tls_get_addr (&stub_entry->h->elf, htab)
12187 && htab->params->tls_get_addr_opt);
12188 if (is_tga)
12190 p = build_tls_get_addr_head (htab, stub_entry, p);
12191 if (r != NULL)
12192 r[0].r_offset += p - loc;
12194 p = build_plt_stub (htab, stub_entry, p, off, r);
12195 if (is_tga)
12196 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12198 else if (stub_entry->type.main == ppc_stub_save_res)
12199 return true;
12200 else
12202 BFD_FAIL ();
12203 return false;
12206 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12208 if (htab->params->emit_stub_syms)
12210 struct elf_link_hash_entry *h;
12211 size_t len1, len2;
12212 char *name;
12213 const char *const stub_str[] = { "long_branch",
12214 "plt_branch",
12215 "plt_call" };
12217 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12218 len2 = strlen (stub_entry->root.string);
12219 name = bfd_malloc (len1 + len2 + 2);
12220 if (name == NULL)
12221 return false;
12222 memcpy (name, stub_entry->root.string, 9);
12223 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12224 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12225 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12226 if (h == NULL)
12227 return false;
12228 if (h->root.type == bfd_link_hash_new)
12230 h->root.type = bfd_link_hash_defined;
12231 h->root.u.def.section = stub_entry->group->stub_sec;
12232 h->root.u.def.value = stub_entry->stub_offset;
12233 h->ref_regular = 1;
12234 h->def_regular = 1;
12235 h->ref_regular_nonweak = 1;
12236 h->forced_local = 1;
12237 h->non_elf = 0;
12238 h->root.linker_def = 1;
12242 return true;
12245 /* As above, but don't actually build the stub. Just bump offset so
12246 we know stub section sizes, and select plt_branch stubs where
12247 long_branch stubs won't do. */
12249 static bool
12250 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12252 struct ppc_stub_hash_entry *stub_entry;
12253 struct bfd_link_info *info;
12254 struct ppc_link_hash_table *htab;
12255 asection *plt;
12256 bfd_vma targ, off, r2off;
12257 unsigned int size, pad, extra, lr_used, delta, odd;
12258 bfd_vma stub_offset;
12260 /* Massage our args to the form they really have. */
12261 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12262 info = in_arg;
12264 htab = ppc_hash_table (info);
12265 if (htab == NULL)
12266 return false;
12268 /* Fail if the target section could not be assigned to an output
12269 section. The user should fix his linker script. */
12270 if (stub_entry->target_section != NULL
12271 && stub_entry->target_section->output_section == NULL
12272 && info->non_contiguous_regions)
12273 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12274 "Retry without --enable-non-contiguous-regions.\n"),
12275 stub_entry->target_section);
12277 /* Same for the group. */
12278 if (stub_entry->group->stub_sec != NULL
12279 && stub_entry->group->stub_sec->output_section == NULL
12280 && info->non_contiguous_regions)
12281 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12282 "Retry without --enable-non-contiguous-regions.\n"),
12283 stub_entry->group->stub_sec);
12285 /* Make a note of the offset within the stubs for this entry. */
12286 stub_offset = stub_entry->group->stub_sec->size;
12287 if (htab->stub_iteration > STUB_SHRINK_ITER
12288 && stub_entry->stub_offset > stub_offset)
12289 stub_offset = stub_entry->stub_offset;
12290 stub_entry->id = ++htab->stub_id;
12292 if (stub_entry->h != NULL
12293 && stub_entry->h->save_res
12294 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12295 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12297 /* Don't make stubs to out-of-line register save/restore
12298 functions. Instead, emit copies of the functions. */
12299 stub_entry->group->needs_save_res = 1;
12300 stub_entry->type.main = ppc_stub_save_res;
12301 stub_entry->type.sub = ppc_stub_toc;
12302 stub_entry->type.r2save = 0;
12303 return true;
12306 if (stub_entry->type.main == ppc_stub_plt_branch)
12308 /* Reset the stub type from the plt branch variant in case we now
12309 can reach with a shorter stub. */
12310 stub_entry->type.main = ppc_stub_long_branch;
12313 if (stub_entry->type.main == ppc_stub_long_branch
12314 && stub_entry->type.sub == ppc_stub_toc)
12316 targ = (stub_entry->target_value
12317 + stub_entry->target_section->output_offset
12318 + stub_entry->target_section->output_section->vma);
12319 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12320 off = (stub_offset
12321 + stub_entry->group->stub_sec->output_offset
12322 + stub_entry->group->stub_sec->output_section->vma);
12324 size = 4;
12325 r2off = 0;
12326 if (stub_entry->type.r2save)
12328 r2off = get_r2off (info, stub_entry);
12329 if (r2off == (bfd_vma) -1)
12331 htab->stub_error = true;
12332 return false;
12334 size = 8;
12335 if (PPC_HA (r2off) != 0)
12336 size += 4;
12337 if (PPC_LO (r2off) != 0)
12338 size += 4;
12339 off += size - 4;
12341 off = targ - off;
12343 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12344 Do the same for -R objects without function descriptors. */
12345 if ((stub_entry->type.r2save
12346 && r2off == 0
12347 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12348 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12350 struct ppc_branch_hash_entry *br_entry;
12352 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12353 stub_entry->root.string + 9,
12354 true, false);
12355 if (br_entry == NULL)
12357 _bfd_error_handler (_("can't build branch stub `%s'"),
12358 stub_entry->root.string);
12359 htab->stub_error = true;
12360 return false;
12363 if (br_entry->iter != htab->stub_iteration)
12365 br_entry->iter = htab->stub_iteration;
12366 br_entry->offset = htab->brlt->size;
12367 htab->brlt->size += 8;
12369 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12370 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12371 else if (info->emitrelocations)
12373 htab->brlt->reloc_count += 1;
12374 htab->brlt->flags |= SEC_RELOC;
12378 targ = (br_entry->offset
12379 + htab->brlt->output_offset
12380 + htab->brlt->output_section->vma);
12381 off = (elf_gp (info->output_bfd)
12382 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12383 off = targ - off;
12385 if (info->emitrelocations)
12387 stub_entry->group->stub_sec->reloc_count
12388 += 1 + (PPC_HA (off) != 0);
12389 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12392 stub_entry->type.main = ppc_stub_plt_branch;
12393 if (!stub_entry->type.r2save)
12395 size = 12;
12396 if (PPC_HA (off) != 0)
12397 size = 16;
12399 else
12401 size = 16;
12402 if (PPC_HA (off) != 0)
12403 size += 4;
12405 if (PPC_HA (r2off) != 0)
12406 size += 4;
12407 if (PPC_LO (r2off) != 0)
12408 size += 4;
12410 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12411 stub_offset += pad;
12413 else if (info->emitrelocations)
12415 stub_entry->group->stub_sec->reloc_count += 1;
12416 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12419 else if (stub_entry->type.main == ppc_stub_long_branch)
12421 off = (stub_offset
12422 + stub_entry->group->stub_sec->output_offset
12423 + stub_entry->group->stub_sec->output_section->vma);
12424 size = 0;
12425 if (stub_entry->type.r2save)
12426 size = 4;
12427 off += size;
12428 targ = (stub_entry->target_value
12429 + stub_entry->target_section->output_offset
12430 + stub_entry->target_section->output_section->vma);
12431 odd = off & 4;
12432 off = targ - off;
12434 if (stub_entry->type.sub == ppc_stub_notoc)
12435 extra = size_power10_offset (off, odd);
12436 else
12437 extra = size_offset (off - 8);
12438 /* Include branch insn plus those in the offset sequence. */
12439 size += 4 + extra;
12441 /* If the branch can't reach, use a plt_branch.
12442 The branch insn is at the end, or "extra" bytes along. So
12443 its offset will be "extra" bytes less that that already
12444 calculated. */
12445 if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12447 stub_entry->type.main = ppc_stub_plt_branch;
12448 size += 4;
12449 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12450 if (pad != 0)
12452 stub_offset += pad;
12453 off -= pad;
12454 odd ^= pad & 4;
12455 size -= extra;
12456 if (stub_entry->type.sub == ppc_stub_notoc)
12457 extra = size_power10_offset (off, odd);
12458 else
12459 extra = size_offset (off - 8);
12460 size += extra;
12463 else if (info->emitrelocations)
12464 stub_entry->group->stub_sec->reloc_count +=1;
12466 if (info->emitrelocations)
12468 unsigned int num_rel;
12469 if (stub_entry->type.sub == ppc_stub_notoc)
12470 num_rel = num_relocs_for_power10_offset (off, odd);
12471 else
12472 num_rel = num_relocs_for_offset (off - 8);
12473 stub_entry->group->stub_sec->reloc_count += num_rel;
12474 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12477 if (stub_entry->type.sub != ppc_stub_notoc)
12479 /* After the bcl, lr has been modified so we need to emit
12480 .eh_frame info saying the return address is in r12. */
12481 lr_used = stub_offset + 8;
12482 if (stub_entry->type.r2save)
12483 lr_used += 4;
12484 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12485 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12486 DW_CFA_restore_extended 65. */
12487 delta = lr_used - stub_entry->group->lr_restore;
12488 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12489 stub_entry->group->lr_restore = lr_used + 8;
12492 else if (stub_entry->type.sub >= ppc_stub_notoc)
12494 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12495 lr_used = 0;
12496 if (stub_entry->h != NULL
12497 && is_tls_get_addr (&stub_entry->h->elf, htab)
12498 && htab->params->tls_get_addr_opt)
12500 lr_used += 7 * 4;
12501 if (!htab->params->no_tls_get_addr_regsave)
12502 lr_used += 11 * 4;
12503 else if (stub_entry->type.r2save)
12504 lr_used += 2 * 4;
12506 if (stub_entry->type.r2save)
12507 lr_used += 4;
12508 targ = stub_entry->plt_ent->plt.offset & ~1;
12509 if (targ >= (bfd_vma) -2)
12510 abort ();
12512 plt = htab->elf.splt;
12513 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12515 if (stub_entry->symtype == STT_GNU_IFUNC)
12516 plt = htab->elf.iplt;
12517 else
12518 plt = htab->pltlocal;
12520 targ += plt->output_offset + plt->output_section->vma;
12521 off = (stub_offset
12522 + stub_entry->group->stub_sec->output_offset
12523 + stub_entry->group->stub_sec->output_section->vma
12524 + lr_used);
12525 odd = off & 4;
12526 off = targ - off;
12528 size = plt_stub_size (htab, stub_entry, off, odd);
12529 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12530 if (pad != 0)
12532 stub_offset += pad;
12533 off -= pad;
12534 odd ^= pad & 4;
12535 size = plt_stub_size (htab, stub_entry, off, odd);
12538 if (info->emitrelocations)
12540 unsigned int num_rel;
12541 if (stub_entry->type.sub == ppc_stub_notoc)
12542 num_rel = num_relocs_for_power10_offset (off, odd);
12543 else
12544 num_rel = num_relocs_for_offset (off - 8);
12545 stub_entry->group->stub_sec->reloc_count += num_rel;
12546 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12549 if (stub_entry->type.sub != ppc_stub_notoc)
12551 /* After the bcl, lr has been modified so we need to emit
12552 .eh_frame info saying the return address is in r12. */
12553 lr_used += stub_offset + 8;
12554 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12555 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12556 DW_CFA_restore_extended 65. */
12557 delta = lr_used - stub_entry->group->lr_restore;
12558 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12559 stub_entry->group->lr_restore = lr_used + 8;
12561 if (stub_entry->h != NULL
12562 && is_tls_get_addr (&stub_entry->h->elf, htab)
12563 && htab->params->tls_get_addr_opt)
12565 if (!htab->params->no_tls_get_addr_regsave)
12567 unsigned int cfa_updt = stub_offset + 18 * 4;
12568 delta = cfa_updt - stub_entry->group->lr_restore;
12569 stub_entry->group->eh_size += eh_advance_size (delta);
12570 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12571 stub_entry->group->lr_restore = stub_offset + size - 4;
12573 else if (stub_entry->type.r2save)
12575 lr_used = stub_offset + size - 20;
12576 delta = lr_used - stub_entry->group->lr_restore;
12577 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12578 stub_entry->group->lr_restore = stub_offset + size - 4;
12582 else if (stub_entry->type.main == ppc_stub_plt_call)
12584 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12585 if (targ >= (bfd_vma) -2)
12586 abort ();
12587 plt = htab->elf.splt;
12588 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12590 if (stub_entry->symtype == STT_GNU_IFUNC)
12591 plt = htab->elf.iplt;
12592 else
12593 plt = htab->pltlocal;
12595 targ += plt->output_offset + plt->output_section->vma;
12597 off = (elf_gp (info->output_bfd)
12598 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12599 off = targ - off;
12601 size = plt_stub_size (htab, stub_entry, off, 0);
12602 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12603 stub_offset += pad;
12605 if (info->emitrelocations)
12607 stub_entry->group->stub_sec->reloc_count
12608 += ((PPC_HA (off) != 0)
12609 + (htab->opd_abi
12610 ? 2 + (htab->params->plt_static_chain
12611 && PPC_HA (off + 16) == PPC_HA (off))
12612 : 1));
12613 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12616 if (stub_entry->h != NULL
12617 && is_tls_get_addr (&stub_entry->h->elf, htab)
12618 && htab->params->tls_get_addr_opt
12619 && stub_entry->type.r2save)
12621 if (!htab->params->no_tls_get_addr_regsave)
12623 /* Adjustments to r1 need to be described. */
12624 unsigned int cfa_updt = stub_offset + 18 * 4;
12625 delta = cfa_updt - stub_entry->group->lr_restore;
12626 stub_entry->group->eh_size += eh_advance_size (delta);
12627 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12629 else
12631 lr_used = stub_offset + size - 20;
12632 /* The eh_frame info will consist of a DW_CFA_advance_loc
12633 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12634 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12635 delta = lr_used - stub_entry->group->lr_restore;
12636 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12638 stub_entry->group->lr_restore = stub_offset + size - 4;
12641 else
12643 BFD_FAIL ();
12644 return false;
12647 if (stub_entry->stub_offset != stub_offset)
12648 htab->stub_changed = true;
12649 stub_entry->stub_offset = stub_offset;
12650 stub_entry->group->stub_sec->size = stub_offset + size;
12651 return true;
12654 /* Set up various things so that we can make a list of input sections
12655 for each output section included in the link. Returns -1 on error,
12656 0 when no stubs will be needed, and 1 on success. */
12659 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12661 unsigned int id;
12662 size_t amt;
12663 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12665 if (htab == NULL)
12666 return -1;
12668 htab->sec_info_arr_size = _bfd_section_id;
12669 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12670 htab->sec_info = bfd_zmalloc (amt);
12671 if (htab->sec_info == NULL)
12672 return -1;
12674 /* Set toc_off for com, und, abs and ind sections. */
12675 for (id = 0; id < 3; id++)
12676 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12678 return 1;
12681 /* Set up for first pass at multitoc partitioning. */
12683 void
12684 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12686 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12688 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12689 htab->toc_bfd = NULL;
12690 htab->toc_first_sec = NULL;
12693 /* The linker repeatedly calls this function for each TOC input section
12694 and linker generated GOT section. Group input bfds such that the toc
12695 within a group is less than 64k in size. */
12697 bool
12698 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12700 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12701 bfd_vma addr, off, limit;
12703 if (htab == NULL)
12704 return false;
12706 if (!htab->second_toc_pass)
12708 /* Keep track of the first .toc or .got section for this input bfd. */
12709 bool new_bfd = htab->toc_bfd != isec->owner;
12711 if (new_bfd)
12713 htab->toc_bfd = isec->owner;
12714 htab->toc_first_sec = isec;
12717 addr = isec->output_offset + isec->output_section->vma;
12718 off = addr - htab->toc_curr;
12719 limit = 0x80008000;
12720 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12721 limit = 0x10000;
12722 if (off + isec->size > limit)
12724 addr = (htab->toc_first_sec->output_offset
12725 + htab->toc_first_sec->output_section->vma);
12726 htab->toc_curr = addr;
12727 htab->toc_curr &= -TOC_BASE_ALIGN;
12730 /* toc_curr is the base address of this toc group. Set elf_gp
12731 for the input section to be the offset relative to the
12732 output toc base plus 0x8000. Making the input elf_gp an
12733 offset allows us to move the toc as a whole without
12734 recalculating input elf_gp. */
12735 off = htab->toc_curr - elf_gp (info->output_bfd);
12736 off += TOC_BASE_OFF;
12738 /* Die if someone uses a linker script that doesn't keep input
12739 file .toc and .got together. */
12740 if (new_bfd
12741 && elf_gp (isec->owner) != 0
12742 && elf_gp (isec->owner) != off)
12743 return false;
12745 elf_gp (isec->owner) = off;
12746 return true;
12749 /* During the second pass toc_first_sec points to the start of
12750 a toc group, and toc_curr is used to track the old elf_gp.
12751 We use toc_bfd to ensure we only look at each bfd once. */
12752 if (htab->toc_bfd == isec->owner)
12753 return true;
12754 htab->toc_bfd = isec->owner;
12756 if (htab->toc_first_sec == NULL
12757 || htab->toc_curr != elf_gp (isec->owner))
12759 htab->toc_curr = elf_gp (isec->owner);
12760 htab->toc_first_sec = isec;
12762 addr = (htab->toc_first_sec->output_offset
12763 + htab->toc_first_sec->output_section->vma);
12764 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12765 elf_gp (isec->owner) = off;
12767 return true;
12770 /* Called via elf_link_hash_traverse to merge GOT entries for global
12771 symbol H. */
12773 static bool
12774 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12776 if (h->root.type == bfd_link_hash_indirect)
12777 return true;
12779 merge_got_entries (&h->got.glist);
12781 return true;
12784 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12785 symbol H. */
12787 static bool
12788 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12790 struct got_entry *gent;
12792 if (h->root.type == bfd_link_hash_indirect)
12793 return true;
12795 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12796 if (!gent->is_indirect)
12797 allocate_got (h, (struct bfd_link_info *) inf, gent);
12798 return true;
12801 /* Called on the first multitoc pass after the last call to
12802 ppc64_elf_next_toc_section. This function removes duplicate GOT
12803 entries. */
12805 bool
12806 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12808 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12809 struct bfd *ibfd, *ibfd2;
12810 bool done_something;
12812 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12814 if (!htab->do_multi_toc)
12815 return false;
12817 /* Merge global sym got entries within a toc group. */
12818 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12820 /* And tlsld_got. */
12821 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12823 struct got_entry *ent, *ent2;
12825 if (!is_ppc64_elf (ibfd))
12826 continue;
12828 ent = ppc64_tlsld_got (ibfd);
12829 if (!ent->is_indirect
12830 && ent->got.offset != (bfd_vma) -1)
12832 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12834 if (!is_ppc64_elf (ibfd2))
12835 continue;
12837 ent2 = ppc64_tlsld_got (ibfd2);
12838 if (!ent2->is_indirect
12839 && ent2->got.offset != (bfd_vma) -1
12840 && elf_gp (ibfd2) == elf_gp (ibfd))
12842 ent2->is_indirect = true;
12843 ent2->got.ent = ent;
12849 /* Zap sizes of got sections. */
12850 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12851 htab->elf.irelplt->size -= htab->got_reli_size;
12852 htab->got_reli_size = 0;
12854 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12856 asection *got, *relgot;
12858 if (!is_ppc64_elf (ibfd))
12859 continue;
12861 got = ppc64_elf_tdata (ibfd)->got;
12862 if (got != NULL)
12864 got->rawsize = got->size;
12865 got->size = 0;
12866 relgot = ppc64_elf_tdata (ibfd)->relgot;
12867 relgot->rawsize = relgot->size;
12868 relgot->size = 0;
12872 /* Now reallocate the got, local syms first. We don't need to
12873 allocate section contents again since we never increase size. */
12874 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12876 struct got_entry **lgot_ents;
12877 struct got_entry **end_lgot_ents;
12878 struct plt_entry **local_plt;
12879 struct plt_entry **end_local_plt;
12880 unsigned char *lgot_masks;
12881 bfd_size_type locsymcount;
12882 Elf_Internal_Shdr *symtab_hdr;
12883 asection *s;
12884 Elf_Internal_Sym *local_syms;
12885 Elf_Internal_Sym *isym;
12887 if (!is_ppc64_elf (ibfd))
12888 continue;
12890 lgot_ents = elf_local_got_ents (ibfd);
12891 if (!lgot_ents)
12892 continue;
12894 symtab_hdr = &elf_symtab_hdr (ibfd);
12895 locsymcount = symtab_hdr->sh_info;
12896 end_lgot_ents = lgot_ents + locsymcount;
12897 local_plt = (struct plt_entry **) end_lgot_ents;
12898 end_local_plt = local_plt + locsymcount;
12899 lgot_masks = (unsigned char *) end_local_plt;
12900 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12901 if (local_syms == NULL && locsymcount != 0)
12903 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12904 0, NULL, NULL, NULL);
12905 if (local_syms == NULL)
12906 return false;
12908 s = ppc64_elf_tdata (ibfd)->got;
12909 for (isym = local_syms;
12910 lgot_ents < end_lgot_ents;
12911 ++lgot_ents, ++lgot_masks, isym++)
12913 struct got_entry *ent;
12915 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12917 unsigned int ent_size = 8;
12918 unsigned int rel_size = sizeof (Elf64_External_Rela);
12920 ent->got.offset = s->size;
12921 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12923 ent_size *= 2;
12924 rel_size *= 2;
12926 s->size += ent_size;
12927 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12929 htab->elf.irelplt->size += rel_size;
12930 htab->got_reli_size += rel_size;
12932 else if (bfd_link_pic (info)
12933 && (ent->tls_type == 0
12934 ? !info->enable_dt_relr
12935 : !bfd_link_executable (info))
12936 && isym->st_shndx != SHN_ABS)
12938 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12939 srel->size += rel_size;
12945 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12947 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12949 struct got_entry *ent;
12951 if (!is_ppc64_elf (ibfd))
12952 continue;
12954 ent = ppc64_tlsld_got (ibfd);
12955 if (!ent->is_indirect
12956 && ent->got.offset != (bfd_vma) -1)
12958 asection *s = ppc64_elf_tdata (ibfd)->got;
12959 ent->got.offset = s->size;
12960 s->size += 16;
12961 if (bfd_link_dll (info))
12963 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12964 srel->size += sizeof (Elf64_External_Rela);
12969 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12970 if (!done_something)
12971 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12973 asection *got;
12975 if (!is_ppc64_elf (ibfd))
12976 continue;
12978 got = ppc64_elf_tdata (ibfd)->got;
12979 if (got != NULL)
12981 done_something = got->rawsize != got->size;
12982 if (done_something)
12983 break;
12987 if (done_something)
12988 (*htab->params->layout_sections_again) ();
12990 /* Set up for second pass over toc sections to recalculate elf_gp
12991 on input sections. */
12992 htab->toc_bfd = NULL;
12993 htab->toc_first_sec = NULL;
12994 htab->second_toc_pass = true;
12995 return done_something;
12998 /* Called after second pass of multitoc partitioning. */
13000 void
13001 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
13003 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13005 /* After the second pass, toc_curr tracks the TOC offset used
13006 for code sections below in ppc64_elf_next_input_section. */
13007 htab->toc_curr = TOC_BASE_OFF;
13010 /* No toc references were found in ISEC. If the code in ISEC makes no
13011 calls, then there's no need to use toc adjusting stubs when branching
13012 into ISEC. Actually, indirect calls from ISEC are OK as they will
13013 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
13014 needed, and 2 if a cyclical call-graph was found but no other reason
13015 for a stub was detected. If called from the top level, a return of
13016 2 means the same as a return of 0. */
13018 static int
13019 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
13021 int ret;
13023 /* Mark this section as checked. */
13024 isec->call_check_done = 1;
13026 /* We know none of our code bearing sections will need toc stubs. */
13027 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13028 return 0;
13030 if (isec->size == 0)
13031 return 0;
13033 if (isec->output_section == NULL)
13034 return 0;
13036 ret = 0;
13037 if (isec->reloc_count != 0)
13039 Elf_Internal_Rela *relstart, *rel;
13040 Elf_Internal_Sym *local_syms;
13041 struct ppc_link_hash_table *htab;
13043 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13044 info->keep_memory);
13045 if (relstart == NULL)
13046 return -1;
13048 /* Look for branches to outside of this section. */
13049 local_syms = NULL;
13050 htab = ppc_hash_table (info);
13051 if (htab == NULL)
13052 return -1;
13054 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
13056 enum elf_ppc64_reloc_type r_type;
13057 unsigned long r_symndx;
13058 struct elf_link_hash_entry *h;
13059 struct ppc_link_hash_entry *eh;
13060 Elf_Internal_Sym *sym;
13061 asection *sym_sec;
13062 struct _opd_sec_data *opd;
13063 bfd_vma sym_value;
13064 bfd_vma dest;
13066 r_type = ELF64_R_TYPE (rel->r_info);
13067 if (r_type != R_PPC64_REL24
13068 && r_type != R_PPC64_REL24_NOTOC
13069 && r_type != R_PPC64_REL24_P9NOTOC
13070 && r_type != R_PPC64_REL14
13071 && r_type != R_PPC64_REL14_BRTAKEN
13072 && r_type != R_PPC64_REL14_BRNTAKEN
13073 && r_type != R_PPC64_PLTCALL
13074 && r_type != R_PPC64_PLTCALL_NOTOC)
13075 continue;
13077 r_symndx = ELF64_R_SYM (rel->r_info);
13078 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13079 isec->owner))
13081 ret = -1;
13082 break;
13085 /* Calls to dynamic lib functions go through a plt call stub
13086 that uses r2. */
13087 eh = ppc_elf_hash_entry (h);
13088 if (eh != NULL
13089 && (eh->elf.plt.plist != NULL
13090 || (eh->oh != NULL
13091 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13093 ret = 1;
13094 break;
13097 if (sym_sec == NULL)
13098 /* Ignore other undefined symbols. */
13099 continue;
13101 /* Assume branches to other sections not included in the
13102 link need stubs too, to cover -R and absolute syms. */
13103 if (sym_sec->output_section == NULL)
13105 ret = 1;
13106 break;
13109 if (h == NULL)
13110 sym_value = sym->st_value;
13111 else
13113 if (h->root.type != bfd_link_hash_defined
13114 && h->root.type != bfd_link_hash_defweak)
13115 abort ();
13116 sym_value = h->root.u.def.value;
13118 sym_value += rel->r_addend;
13120 /* If this branch reloc uses an opd sym, find the code section. */
13121 opd = get_opd_info (sym_sec);
13122 if (opd != NULL)
13124 if (h == NULL && opd->adjust != NULL)
13126 long adjust;
13128 adjust = opd->adjust[OPD_NDX (sym_value)];
13129 if (adjust == -1)
13130 /* Assume deleted functions won't ever be called. */
13131 continue;
13132 sym_value += adjust;
13135 dest = opd_entry_value (sym_sec, sym_value,
13136 &sym_sec, NULL, false);
13137 if (dest == (bfd_vma) -1)
13138 continue;
13140 else
13141 dest = (sym_value
13142 + sym_sec->output_offset
13143 + sym_sec->output_section->vma);
13145 /* Ignore branch to self. */
13146 if (sym_sec == isec)
13147 continue;
13149 /* If the called function uses the toc, we need a stub. */
13150 if (sym_sec->has_toc_reloc
13151 || sym_sec->makes_toc_func_call)
13153 ret = 1;
13154 break;
13157 /* Assume any branch that needs a long branch stub might in fact
13158 need a plt_branch stub. A plt_branch stub uses r2. */
13159 else if (dest - (isec->output_offset
13160 + isec->output_section->vma
13161 + rel->r_offset) + (1 << 25)
13162 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13163 ? h->other
13164 : sym->st_other))
13166 ret = 1;
13167 break;
13170 /* If calling back to a section in the process of being
13171 tested, we can't say for sure that no toc adjusting stubs
13172 are needed, so don't return zero. */
13173 else if (sym_sec->call_check_in_progress)
13174 ret = 2;
13176 /* Branches to another section that itself doesn't have any TOC
13177 references are OK. Recursively call ourselves to check. */
13178 else if (!sym_sec->call_check_done)
13180 int recur;
13182 /* Mark current section as indeterminate, so that other
13183 sections that call back to current won't be marked as
13184 known. */
13185 isec->call_check_in_progress = 1;
13186 recur = toc_adjusting_stub_needed (info, sym_sec);
13187 isec->call_check_in_progress = 0;
13189 if (recur != 0)
13191 ret = recur;
13192 if (recur != 2)
13193 break;
13198 if (elf_symtab_hdr (isec->owner).contents
13199 != (unsigned char *) local_syms)
13200 free (local_syms);
13201 if (elf_section_data (isec)->relocs != relstart)
13202 free (relstart);
13205 if ((ret & 1) == 0
13206 && isec->map_head.s != NULL
13207 && (strcmp (isec->output_section->name, ".init") == 0
13208 || strcmp (isec->output_section->name, ".fini") == 0))
13210 if (isec->map_head.s->has_toc_reloc
13211 || isec->map_head.s->makes_toc_func_call)
13212 ret = 1;
13213 else if (!isec->map_head.s->call_check_done)
13215 int recur;
13216 isec->call_check_in_progress = 1;
13217 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13218 isec->call_check_in_progress = 0;
13219 if (recur != 0)
13220 ret = recur;
13224 if (ret == 1)
13225 isec->makes_toc_func_call = 1;
13227 return ret;
13230 /* The linker repeatedly calls this function for each input section,
13231 in the order that input sections are linked into output sections.
13232 Build lists of input sections to determine groupings between which
13233 we may insert linker stubs. */
13235 bool
13236 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13238 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13240 if (htab == NULL)
13241 return false;
13243 if ((isec->output_section->flags & SEC_CODE) != 0
13244 && isec->output_section->id < htab->sec_info_arr_size)
13246 /* This happens to make the list in reverse order,
13247 which is what we want. */
13248 htab->sec_info[isec->id].u.list
13249 = htab->sec_info[isec->output_section->id].u.list;
13250 htab->sec_info[isec->output_section->id].u.list = isec;
13253 if (htab->multi_toc_needed)
13255 /* Analyse sections that aren't already flagged as needing a
13256 valid toc pointer. Exclude .fixup for the linux kernel.
13257 .fixup contains branches, but only back to the function that
13258 hit an exception. */
13259 if (!(isec->has_toc_reloc
13260 || (isec->flags & SEC_CODE) == 0
13261 || strcmp (isec->name, ".fixup") == 0
13262 || isec->call_check_done))
13264 if (toc_adjusting_stub_needed (info, isec) < 0)
13265 return false;
13267 /* Make all sections use the TOC assigned for this object file.
13268 This will be wrong for pasted sections; We fix that in
13269 check_pasted_section(). */
13270 if (elf_gp (isec->owner) != 0)
13271 htab->toc_curr = elf_gp (isec->owner);
13274 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13275 return true;
13278 /* Check that all .init and .fini sections use the same toc, if they
13279 have toc relocs. */
13281 static bool
13282 check_pasted_section (struct bfd_link_info *info, const char *name)
13284 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13286 if (o != NULL)
13288 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13289 bfd_vma toc_off = 0;
13290 asection *i;
13292 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13293 if (i->has_toc_reloc)
13295 if (toc_off == 0)
13296 toc_off = htab->sec_info[i->id].toc_off;
13297 else if (toc_off != htab->sec_info[i->id].toc_off)
13298 return false;
13301 if (toc_off == 0)
13302 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13303 if (i->makes_toc_func_call)
13305 toc_off = htab->sec_info[i->id].toc_off;
13306 break;
13309 /* Make sure the whole pasted function uses the same toc offset. */
13310 if (toc_off != 0)
13311 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13312 htab->sec_info[i->id].toc_off = toc_off;
13314 return true;
13317 bool
13318 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13320 bool ret1 = check_pasted_section (info, ".init");
13321 bool ret2 = check_pasted_section (info, ".fini");
13323 return ret1 && ret2;
13326 /* See whether we can group stub sections together. Grouping stub
13327 sections may result in fewer stubs. More importantly, we need to
13328 put all .init* and .fini* stubs at the beginning of the .init or
13329 .fini output sections respectively, because glibc splits the
13330 _init and _fini functions into multiple parts. Putting a stub in
13331 the middle of a function is not a good idea. */
13333 static bool
13334 group_sections (struct bfd_link_info *info,
13335 bfd_size_type stub_group_size,
13336 bool stubs_always_before_branch)
13338 struct ppc_link_hash_table *htab;
13339 asection *osec;
13340 bool suppress_size_errors;
13342 htab = ppc_hash_table (info);
13343 if (htab == NULL)
13344 return false;
13346 suppress_size_errors = false;
13347 if (stub_group_size == 1)
13349 /* Default values. */
13350 if (stubs_always_before_branch)
13351 stub_group_size = 0x1e00000;
13352 else
13353 stub_group_size = 0x1c00000;
13354 suppress_size_errors = true;
13357 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13359 asection *tail;
13361 if (osec->id >= htab->sec_info_arr_size)
13362 continue;
13364 tail = htab->sec_info[osec->id].u.list;
13365 while (tail != NULL)
13367 asection *curr;
13368 asection *prev;
13369 bfd_size_type total;
13370 bool big_sec;
13371 bfd_vma curr_toc;
13372 struct map_stub *group;
13373 bfd_size_type group_size;
13375 curr = tail;
13376 total = tail->size;
13377 group_size = (ppc64_elf_section_data (tail) != NULL
13378 && ppc64_elf_section_data (tail)->has_14bit_branch
13379 ? stub_group_size >> 10 : stub_group_size);
13381 big_sec = total > group_size;
13382 if (big_sec && !suppress_size_errors)
13383 /* xgettext:c-format */
13384 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13385 tail->owner, tail);
13386 curr_toc = htab->sec_info[tail->id].toc_off;
13388 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13389 && ((total += curr->output_offset - prev->output_offset)
13390 < (ppc64_elf_section_data (prev) != NULL
13391 && ppc64_elf_section_data (prev)->has_14bit_branch
13392 ? (group_size = stub_group_size >> 10) : group_size))
13393 && htab->sec_info[prev->id].toc_off == curr_toc)
13394 curr = prev;
13396 /* OK, the size from the start of CURR to the end is less
13397 than group_size and thus can be handled by one stub
13398 section. (or the tail section is itself larger than
13399 group_size, in which case we may be toast.) We should
13400 really be keeping track of the total size of stubs added
13401 here, as stubs contribute to the final output section
13402 size. That's a little tricky, and this way will only
13403 break if stubs added make the total size more than 2^25,
13404 ie. for the default stub_group_size, if stubs total more
13405 than 2097152 bytes, or nearly 75000 plt call stubs. */
13406 group = bfd_alloc (curr->owner, sizeof (*group));
13407 if (group == NULL)
13408 return false;
13409 group->link_sec = curr;
13410 group->stub_sec = NULL;
13411 group->needs_save_res = 0;
13412 group->lr_restore = 0;
13413 group->eh_size = 0;
13414 group->eh_base = 0;
13415 group->next = htab->group;
13416 htab->group = group;
13419 prev = htab->sec_info[tail->id].u.list;
13420 /* Set up this stub group. */
13421 htab->sec_info[tail->id].u.group = group;
13423 while (tail != curr && (tail = prev) != NULL);
13425 /* But wait, there's more! Input sections up to group_size
13426 bytes before the stub section can be handled by it too.
13427 Don't do this if we have a really large section after the
13428 stubs, as adding more stubs increases the chance that
13429 branches may not reach into the stub section. */
13430 if (!stubs_always_before_branch && !big_sec)
13432 total = 0;
13433 while (prev != NULL
13434 && ((total += tail->output_offset - prev->output_offset)
13435 < (ppc64_elf_section_data (prev) != NULL
13436 && ppc64_elf_section_data (prev)->has_14bit_branch
13437 ? (group_size = stub_group_size >> 10)
13438 : group_size))
13439 && htab->sec_info[prev->id].toc_off == curr_toc)
13441 tail = prev;
13442 prev = htab->sec_info[tail->id].u.list;
13443 htab->sec_info[tail->id].u.group = group;
13446 tail = prev;
13449 return true;
13452 static const unsigned char glink_eh_frame_cie[] =
13454 0, 0, 0, 16, /* length. */
13455 0, 0, 0, 0, /* id. */
13456 1, /* CIE version. */
13457 'z', 'R', 0, /* Augmentation string. */
13458 4, /* Code alignment. */
13459 0x78, /* Data alignment. */
13460 65, /* RA reg. */
13461 1, /* Augmentation size. */
13462 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13463 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13466 /* Stripping output sections is normally done before dynamic section
13467 symbols have been allocated. This function is called later, and
13468 handles cases like htab->brlt which is mapped to its own output
13469 section. */
13471 static void
13472 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13474 if (isec->size == 0
13475 && isec->output_section->size == 0
13476 && !(isec->output_section->flags & SEC_KEEP)
13477 && !bfd_section_removed_from_list (info->output_bfd,
13478 isec->output_section)
13479 && elf_section_data (isec->output_section)->dynindx == 0)
13481 isec->output_section->flags |= SEC_EXCLUDE;
13482 bfd_section_list_remove (info->output_bfd, isec->output_section);
13483 info->output_bfd->section_count--;
13487 /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13488 the array of such relocs. */
13490 static bool
13491 append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13493 if (htab->relr_count >= htab->relr_alloc)
13495 if (htab->relr_alloc == 0)
13496 htab->relr_alloc = 4096;
13497 else
13498 htab->relr_alloc *= 2;
13499 htab->relr = bfd_realloc (htab->relr,
13500 htab->relr_alloc * sizeof (*htab->relr));
13501 if (htab->relr == NULL)
13502 return false;
13504 htab->relr[htab->relr_count].sec = sec;
13505 htab->relr[htab->relr_count].off = off;
13506 htab->relr_count++;
13507 return true;
13510 /* qsort comparator for bfd_vma args. */
13512 static int
13513 compare_relr_address (const void *arg1, const void *arg2)
13515 bfd_vma a = *(bfd_vma *) arg1;
13516 bfd_vma b = *(bfd_vma *) arg2;
13517 return a < b ? -1 : a > b ? 1 : 0;
13520 /* Produce a malloc'd sorted array of reloc addresses from the info
13521 stored by append_relr_off. */
13523 static bfd_vma *
13524 sort_relr (struct ppc_link_hash_table *htab)
13526 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13527 if (addr == NULL)
13528 return NULL;
13530 for (size_t i = 0; i < htab->relr_count; i++)
13531 addr[i] = (htab->relr[i].sec->output_section->vma
13532 + htab->relr[i].sec->output_offset
13533 + htab->relr[i].off);
13535 if (htab->relr_count > 1)
13536 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13538 return addr;
13541 /* Look over GOT and PLT entries saved on elf_local_got_ents for all
13542 input files, stashing info about needed relative relocs. */
13544 static bool
13545 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13547 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13548 bfd *ibfd;
13550 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13552 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13553 struct plt_entry **local_plt, **lplt, **end_local_plt;
13554 Elf_Internal_Shdr *symtab_hdr;
13555 bfd_size_type locsymcount;
13556 Elf_Internal_Sym *local_syms;
13557 Elf_Internal_Sym *isym;
13558 struct plt_entry *pent;
13559 struct got_entry *gent;
13561 if (!is_ppc64_elf (ibfd))
13562 continue;
13564 lgot_ents = elf_local_got_ents (ibfd);
13565 if (!lgot_ents)
13566 continue;
13568 symtab_hdr = &elf_symtab_hdr (ibfd);
13569 locsymcount = symtab_hdr->sh_info;
13570 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13571 if (local_syms == NULL && locsymcount != 0)
13573 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13574 0, NULL, NULL, NULL);
13575 if (local_syms == NULL)
13576 return false;
13578 end_lgot_ents = lgot_ents + locsymcount;
13579 local_plt = (struct plt_entry **) end_lgot_ents;
13580 end_local_plt = local_plt + locsymcount;
13581 for (lgot = lgot_ents, isym = local_syms;
13582 lgot < end_lgot_ents;
13583 ++lgot, ++isym)
13584 for (gent = *lgot; gent != NULL; gent = gent->next)
13585 if (!gent->is_indirect
13586 && gent->tls_type == 0
13587 && gent->got.offset != (bfd_vma) -1
13588 && isym->st_shndx != SHN_ABS)
13590 asection *got = ppc64_elf_tdata (gent->owner)->got;
13591 if (!append_relr_off (htab, got, gent->got.offset))
13593 htab->stub_error = true;
13594 return false;
13598 if (!htab->opd_abi)
13599 for (lplt = local_plt, isym = local_syms;
13600 lplt < end_local_plt;
13601 ++lplt, ++isym)
13602 for (pent = *lplt; pent != NULL; pent = pent->next)
13603 if (pent->plt.offset != (bfd_vma) -1
13604 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13606 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13608 if (symtab_hdr->contents != (unsigned char *) local_syms)
13609 free (local_syms);
13610 return false;
13614 if (local_syms != NULL
13615 && symtab_hdr->contents != (unsigned char *) local_syms)
13617 if (!info->keep_memory)
13618 free (local_syms);
13619 else
13620 symtab_hdr->contents = (unsigned char *) local_syms;
13623 return true;
13626 /* Stash info about needed GOT and PLT entry relative relocs for
13627 global symbol H. */
13629 static bool
13630 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13632 struct bfd_link_info *info;
13633 struct ppc_link_hash_table *htab;
13634 struct plt_entry *pent;
13635 struct got_entry *gent;
13637 if (h->root.type == bfd_link_hash_indirect)
13638 return true;
13640 info = (struct bfd_link_info *) inf;
13641 htab = ppc_hash_table (info);
13642 if (htab == NULL)
13643 return false;
13645 if (h->type != STT_GNU_IFUNC
13646 && h->def_regular
13647 && (h->root.type == bfd_link_hash_defined
13648 || h->root.type == bfd_link_hash_defweak))
13650 if ((!htab->elf.dynamic_sections_created
13651 || h->dynindx == -1
13652 || SYMBOL_REFERENCES_LOCAL (info, h))
13653 && !bfd_is_abs_symbol (&h->root))
13654 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13655 if (!gent->is_indirect
13656 && gent->tls_type == 0
13657 && gent->got.offset != (bfd_vma) -1)
13659 asection *got = ppc64_elf_tdata (gent->owner)->got;
13660 if (!append_relr_off (htab, got, gent->got.offset))
13662 htab->stub_error = true;
13663 return false;
13667 if (!htab->opd_abi
13668 && use_local_plt (info, h))
13669 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13670 if (pent->plt.offset != (bfd_vma) -1)
13672 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13674 htab->stub_error = true;
13675 return false;
13679 return true;
13682 /* Determine and set the size of the stub section for a final link.
13684 The basic idea here is to examine all the relocations looking for
13685 PC-relative calls to a target that is unreachable with a "bl"
13686 instruction. */
13688 bool
13689 ppc64_elf_size_stubs (struct bfd_link_info *info)
13691 bfd_size_type stub_group_size;
13692 bool stubs_always_before_branch;
13693 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13695 if (htab == NULL)
13696 return false;
13698 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13699 htab->params->power10_stubs = 0;
13701 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13702 htab->params->plt_thread_safe = 1;
13703 if (!htab->opd_abi)
13704 htab->params->plt_thread_safe = 0;
13705 else if (htab->params->plt_thread_safe == -1)
13707 static const char *const thread_starter[] =
13709 "pthread_create",
13710 /* libstdc++ */
13711 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13712 /* librt */
13713 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13714 "mq_notify", "create_timer",
13715 /* libanl */
13716 "getaddrinfo_a",
13717 /* libgomp */
13718 "GOMP_parallel",
13719 "GOMP_parallel_start",
13720 "GOMP_parallel_loop_static",
13721 "GOMP_parallel_loop_static_start",
13722 "GOMP_parallel_loop_dynamic",
13723 "GOMP_parallel_loop_dynamic_start",
13724 "GOMP_parallel_loop_guided",
13725 "GOMP_parallel_loop_guided_start",
13726 "GOMP_parallel_loop_runtime",
13727 "GOMP_parallel_loop_runtime_start",
13728 "GOMP_parallel_sections",
13729 "GOMP_parallel_sections_start",
13730 /* libgo */
13731 "__go_go",
13733 unsigned i;
13735 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13737 struct elf_link_hash_entry *h;
13738 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13739 false, false, true);
13740 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13741 if (htab->params->plt_thread_safe)
13742 break;
13745 stubs_always_before_branch = htab->params->group_size < 0;
13746 if (htab->params->group_size < 0)
13747 stub_group_size = -htab->params->group_size;
13748 else
13749 stub_group_size = htab->params->group_size;
13751 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13752 return false;
13754 htab->tga_group = NULL;
13755 if (!htab->params->no_tls_get_addr_regsave
13756 && htab->tga_desc_fd != NULL
13757 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13758 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13759 && htab->tls_get_addr_fd != NULL
13760 && is_static_defined (&htab->tls_get_addr_fd->elf))
13762 asection *sym_sec, *code_sec, *stub_sec;
13763 bfd_vma sym_value;
13764 struct _opd_sec_data *opd;
13766 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13767 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13768 code_sec = sym_sec;
13769 opd = get_opd_info (sym_sec);
13770 if (opd != NULL)
13771 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13772 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13773 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13774 htab->tga_group->link_sec);
13775 if (stub_sec == NULL)
13776 return false;
13777 htab->tga_group->stub_sec = stub_sec;
13779 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13780 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13781 htab->tga_desc_fd->elf.root.u.def.value = 0;
13782 htab->tga_desc_fd->elf.type = STT_FUNC;
13783 htab->tga_desc_fd->elf.def_regular = 1;
13784 htab->tga_desc_fd->elf.non_elf = 0;
13785 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13788 /* Loop until no stubs added. After iteration 20 of this loop we may
13789 exit on a stub section shrinking. */
13791 while (1)
13793 bfd *input_bfd;
13794 unsigned int bfd_indx;
13795 struct map_stub *group;
13797 htab->stub_iteration += 1;
13798 htab->relr_count = 0;
13800 for (input_bfd = info->input_bfds, bfd_indx = 0;
13801 input_bfd != NULL;
13802 input_bfd = input_bfd->link.next, bfd_indx++)
13804 Elf_Internal_Shdr *symtab_hdr;
13805 asection *section;
13806 Elf_Internal_Sym *local_syms = NULL;
13808 if (!is_ppc64_elf (input_bfd))
13809 continue;
13811 /* We'll need the symbol table in a second. */
13812 symtab_hdr = &elf_symtab_hdr (input_bfd);
13813 if (symtab_hdr->sh_info == 0)
13814 continue;
13816 /* Walk over each section attached to the input bfd. */
13817 for (section = input_bfd->sections;
13818 section != NULL;
13819 section = section->next)
13821 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13822 bool is_opd;
13824 /* If there aren't any relocs, then there's nothing more
13825 to do. */
13826 if ((section->flags & SEC_RELOC) == 0
13827 || (section->flags & SEC_ALLOC) == 0
13828 || (section->flags & SEC_LOAD) == 0
13829 || section->reloc_count == 0)
13830 continue;
13832 if (!info->enable_dt_relr
13833 && (section->flags & SEC_CODE) == 0)
13834 continue;
13836 /* If this section is a link-once section that will be
13837 discarded, then don't create any stubs. */
13838 if (section->output_section == NULL
13839 || section->output_section->owner != info->output_bfd)
13840 continue;
13842 /* Get the relocs. */
13843 internal_relocs
13844 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13845 info->keep_memory);
13846 if (internal_relocs == NULL)
13847 goto error_ret_free_local;
13849 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13851 /* Now examine each relocation. */
13852 irela = internal_relocs;
13853 irelaend = irela + section->reloc_count;
13854 for (; irela < irelaend; irela++)
13856 enum elf_ppc64_reloc_type r_type;
13857 unsigned int r_indx;
13858 struct ppc_stub_type stub_type;
13859 struct ppc_stub_hash_entry *stub_entry;
13860 asection *sym_sec, *code_sec;
13861 bfd_vma sym_value, code_value;
13862 bfd_vma destination;
13863 unsigned long local_off;
13864 bool ok_dest;
13865 struct ppc_link_hash_entry *hash;
13866 struct ppc_link_hash_entry *fdh;
13867 struct elf_link_hash_entry *h;
13868 Elf_Internal_Sym *sym;
13869 char *stub_name;
13870 const asection *id_sec;
13871 struct _opd_sec_data *opd;
13872 struct plt_entry *plt_ent;
13874 r_type = ELF64_R_TYPE (irela->r_info);
13875 r_indx = ELF64_R_SYM (irela->r_info);
13877 if (r_type >= R_PPC64_max)
13879 bfd_set_error (bfd_error_bad_value);
13880 goto error_ret_free_internal;
13883 /* Only look for stubs on branch instructions. */
13884 switch (r_type)
13886 default:
13887 continue;
13889 case R_PPC64_REL24:
13890 case R_PPC64_REL24_NOTOC:
13891 case R_PPC64_REL24_P9NOTOC:
13892 case R_PPC64_REL14:
13893 case R_PPC64_REL14_BRTAKEN:
13894 case R_PPC64_REL14_BRNTAKEN:
13895 if ((section->flags & SEC_CODE) != 0)
13896 break;
13897 continue;
13899 case R_PPC64_ADDR64:
13900 case R_PPC64_TOC:
13901 if (info->enable_dt_relr
13902 && irela->r_offset % 2 == 0
13903 && section->alignment_power != 0)
13904 break;
13905 continue;
13908 /* Now determine the call target, its name, value,
13909 section. */
13910 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13911 r_indx, input_bfd))
13912 goto error_ret_free_internal;
13914 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13916 /* Only locally defined symbols can possibly use
13917 relative relocations. */
13918 bfd_vma r_offset;
13919 if ((sym_sec == NULL
13920 || sym_sec->output_section == NULL)
13921 /* No symbol is OK too. */
13922 && !(sym != NULL && sym->st_shndx == 0)
13923 /* Hack for __ehdr_start, which is undefined
13924 at this point. */
13925 && !(h != NULL && h->root.linker_def))
13926 continue;
13927 if (NO_OPD_RELOCS && is_opd)
13928 continue;
13929 if (!is_opd
13930 && r_type == R_PPC64_ADDR64)
13932 if (h != NULL
13933 ? h->type == STT_GNU_IFUNC
13934 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13935 continue;
13936 if (h != NULL
13937 ? bfd_is_abs_symbol (&h->root)
13938 : sym->st_shndx == SHN_ABS)
13939 continue;
13940 if (h != NULL
13941 && !SYMBOL_REFERENCES_LOCAL (info, h))
13942 continue;
13944 r_offset = _bfd_elf_section_offset (info->output_bfd,
13945 info,
13946 section,
13947 irela->r_offset);
13948 if (r_offset >= (bfd_vma) -2)
13949 continue;
13950 if (!append_relr_off (htab, section, r_offset))
13951 goto error_ret_free_internal;
13952 continue;
13955 hash = ppc_elf_hash_entry (h);
13956 ok_dest = false;
13957 fdh = NULL;
13958 sym_value = 0;
13959 if (hash == NULL)
13961 sym_value = sym->st_value;
13962 if (sym_sec != NULL
13963 && sym_sec->output_section != NULL)
13964 ok_dest = true;
13966 else if (hash->elf.root.type == bfd_link_hash_defined
13967 || hash->elf.root.type == bfd_link_hash_defweak)
13969 sym_value = hash->elf.root.u.def.value;
13970 if (sym_sec->output_section != NULL)
13971 ok_dest = true;
13973 else if (hash->elf.root.type == bfd_link_hash_undefweak
13974 || hash->elf.root.type == bfd_link_hash_undefined)
13976 /* Recognise an old ABI func code entry sym, and
13977 use the func descriptor sym instead if it is
13978 defined. */
13979 if (hash->elf.root.root.string[0] == '.'
13980 && hash->oh != NULL)
13982 fdh = ppc_follow_link (hash->oh);
13983 if (fdh->elf.root.type == bfd_link_hash_defined
13984 || fdh->elf.root.type == bfd_link_hash_defweak)
13986 sym_sec = fdh->elf.root.u.def.section;
13987 sym_value = fdh->elf.root.u.def.value;
13988 if (sym_sec->output_section != NULL)
13989 ok_dest = true;
13991 else
13992 fdh = NULL;
13995 else
13997 bfd_set_error (bfd_error_bad_value);
13998 goto error_ret_free_internal;
14001 destination = 0;
14002 local_off = 0;
14003 if (ok_dest)
14005 sym_value += irela->r_addend;
14006 destination = (sym_value
14007 + sym_sec->output_offset
14008 + sym_sec->output_section->vma);
14009 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
14010 ? hash->elf.other
14011 : sym->st_other);
14014 code_sec = sym_sec;
14015 code_value = sym_value;
14016 opd = get_opd_info (sym_sec);
14017 if (opd != NULL)
14019 bfd_vma dest;
14021 if (hash == NULL && opd->adjust != NULL)
14023 long adjust = opd->adjust[OPD_NDX (sym_value)];
14024 if (adjust == -1)
14025 continue;
14026 code_value += adjust;
14027 sym_value += adjust;
14029 dest = opd_entry_value (sym_sec, sym_value,
14030 &code_sec, &code_value, false);
14031 if (dest != (bfd_vma) -1)
14033 destination = dest;
14034 if (fdh != NULL)
14036 /* Fixup old ABI sym to point at code
14037 entry. */
14038 hash->elf.root.type = bfd_link_hash_defweak;
14039 hash->elf.root.u.def.section = code_sec;
14040 hash->elf.root.u.def.value = code_value;
14045 /* Determine what (if any) linker stub is needed. */
14046 plt_ent = NULL;
14047 stub_type.main = ppc_type_of_stub (section, irela, &hash,
14048 &plt_ent, destination,
14049 local_off);
14050 stub_type.sub = ppc_stub_toc;
14051 stub_type.r2save = 0;
14053 if (r_type == R_PPC64_REL24_NOTOC
14054 || r_type == R_PPC64_REL24_P9NOTOC)
14056 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14057 if (htab->params->power10_stubs == 0
14058 || (r_type == R_PPC64_REL24_P9NOTOC
14059 && htab->params->power10_stubs != 1))
14060 notoc = ppc_stub_p9notoc;
14061 if (stub_type.main == ppc_stub_plt_call)
14062 stub_type.sub = notoc;
14063 else if (stub_type.main == ppc_stub_long_branch
14064 || (code_sec != NULL
14065 && code_sec->output_section != NULL
14066 && (((hash ? hash->elf.other : sym->st_other)
14067 & STO_PPC64_LOCAL_MASK)
14068 > 1 << STO_PPC64_LOCAL_BIT)))
14070 stub_type.main = ppc_stub_long_branch;
14071 stub_type.sub = notoc;
14072 stub_type.r2save = 0;
14075 else if (stub_type.main != ppc_stub_plt_call)
14077 /* Check whether we need a TOC adjusting stub.
14078 Since the linker pastes together pieces from
14079 different object files when creating the
14080 _init and _fini functions, it may be that a
14081 call to what looks like a local sym is in
14082 fact a call needing a TOC adjustment. */
14083 if ((code_sec != NULL
14084 && code_sec->output_section != NULL
14085 && (code_sec->has_toc_reloc
14086 || code_sec->makes_toc_func_call)
14087 && (htab->sec_info[code_sec->id].toc_off
14088 != htab->sec_info[section->id].toc_off))
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 = ppc_stub_toc;
14095 stub_type.r2save = 1;
14099 if (stub_type.main == ppc_stub_none)
14100 continue;
14102 /* __tls_get_addr calls might be eliminated. */
14103 if (stub_type.main != ppc_stub_plt_call
14104 && hash != NULL
14105 && is_tls_get_addr (&hash->elf, htab)
14106 && section->has_tls_reloc
14107 && irela != internal_relocs)
14109 /* Get tls info. */
14110 unsigned char *tls_mask;
14112 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14113 irela - 1, input_bfd))
14114 goto error_ret_free_internal;
14115 if ((*tls_mask & TLS_TLS) != 0
14116 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14117 continue;
14120 if (stub_type.main == ppc_stub_plt_call
14121 && stub_type.sub == ppc_stub_toc)
14123 if (!htab->opd_abi
14124 && htab->params->plt_localentry0 != 0
14125 && is_elfv2_localentry0 (&hash->elf))
14126 htab->has_plt_localentry0 = 1;
14127 else if (irela + 1 < irelaend
14128 && irela[1].r_offset == irela->r_offset + 4
14129 && (ELF64_R_TYPE (irela[1].r_info)
14130 == R_PPC64_TOCSAVE))
14132 if (!tocsave_find (htab, INSERT,
14133 &local_syms, irela + 1, input_bfd))
14134 goto error_ret_free_internal;
14136 else
14137 stub_type.r2save = 1;
14140 /* Support for grouping stub sections. */
14141 id_sec = htab->sec_info[section->id].u.group->link_sec;
14143 /* Get the name of this stub. */
14144 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14145 if (!stub_name)
14146 goto error_ret_free_internal;
14148 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14149 stub_name, false, false);
14150 if (stub_entry != NULL)
14152 free (stub_name);
14153 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14155 /* xgettext:c-format */
14156 _bfd_error_handler
14157 (_("%pB: cannot create stub entry %s"),
14158 section->owner, stub_entry->root.string);
14159 goto error_ret_free_internal;
14161 continue;
14164 stub_entry = ppc_add_stub (stub_name, section, info);
14165 if (stub_entry == NULL)
14167 free (stub_name);
14168 error_ret_free_internal:
14169 if (elf_section_data (section)->relocs == NULL)
14170 free (internal_relocs);
14171 error_ret_free_local:
14172 if (symtab_hdr->contents
14173 != (unsigned char *) local_syms)
14174 free (local_syms);
14175 return false;
14178 stub_entry->type = stub_type;
14179 if (stub_type.main == ppc_stub_plt_call)
14181 stub_entry->target_value = sym_value;
14182 stub_entry->target_section = sym_sec;
14184 else
14186 stub_entry->target_value = code_value;
14187 stub_entry->target_section = code_sec;
14189 stub_entry->h = hash;
14190 stub_entry->plt_ent = plt_ent;
14191 stub_entry->symtype
14192 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14193 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14195 if (hash != NULL
14196 && (hash->elf.root.type == bfd_link_hash_defined
14197 || hash->elf.root.type == bfd_link_hash_defweak))
14198 htab->stub_globals += 1;
14201 /* We're done with the internal relocs, free them. */
14202 if (elf_section_data (section)->relocs != internal_relocs)
14203 free (internal_relocs);
14206 if (local_syms != NULL
14207 && symtab_hdr->contents != (unsigned char *) local_syms)
14209 if (!info->keep_memory)
14210 free (local_syms);
14211 else
14212 symtab_hdr->contents = (unsigned char *) local_syms;
14216 /* We may have added some stubs. Find out the new size of the
14217 stub sections. */
14218 for (group = htab->group; group != NULL; group = group->next)
14220 group->lr_restore = 0;
14221 group->eh_size = 0;
14222 if (group->stub_sec != NULL)
14224 asection *stub_sec = group->stub_sec;
14226 stub_sec->rawsize = stub_sec->size;
14227 stub_sec->size = 0;
14228 stub_sec->reloc_count = 0;
14229 stub_sec->flags &= ~SEC_RELOC;
14232 if (htab->tga_group != NULL)
14234 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14235 htab->tga_group->eh_size
14236 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14237 htab->tga_group->lr_restore = 23 * 4;
14238 htab->tga_group->stub_sec->size = 24 * 4;
14241 htab->brlt->rawsize = htab->brlt->size;
14242 htab->brlt->size = 0;
14243 htab->brlt->reloc_count = 0;
14244 htab->brlt->flags &= ~SEC_RELOC;
14245 if (htab->relbrlt != NULL)
14246 htab->relbrlt->size = 0;
14248 if (htab->elf.srelrdyn != NULL)
14250 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14251 htab->elf.srelrdyn->size = 0;
14254 htab->stub_changed = false;
14255 htab->stub_id = 0;
14256 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14258 for (group = htab->group; group != NULL; group = group->next)
14259 if (group->needs_save_res)
14260 group->stub_sec->size += htab->sfpr->size;
14262 if (info->emitrelocations
14263 && htab->glink != NULL && htab->glink->size != 0)
14265 htab->glink->reloc_count = 1;
14266 htab->glink->flags |= SEC_RELOC;
14269 if (htab->glink_eh_frame != NULL
14270 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14271 && htab->glink_eh_frame->output_section->size > 8)
14273 size_t size = 0, align = 4;
14275 for (group = htab->group; group != NULL; group = group->next)
14276 if (group->eh_size != 0)
14277 size += (group->eh_size + 17 + align - 1) & -align;
14278 if (htab->glink != NULL && htab->glink->size != 0)
14279 size += (24 + align - 1) & -align;
14280 if (size != 0)
14281 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14282 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14283 size = (size + align - 1) & -align;
14284 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14285 htab->glink_eh_frame->size = size;
14288 if (htab->params->plt_stub_align != 0)
14289 for (group = htab->group; group != NULL; group = group->next)
14290 if (group->stub_sec != NULL)
14292 int align = abs (htab->params->plt_stub_align);
14293 group->stub_sec->size
14294 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14297 if (htab->elf.srelrdyn != NULL)
14299 bfd_vma r_offset;
14301 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14302 if (!append_relr_off (htab, htab->brlt, r_offset))
14303 return false;
14305 if (!got_and_plt_relr_for_local_syms (info))
14306 return false;
14307 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14308 if (htab->stub_error)
14309 return false;
14311 bfd_vma *relr_addr = sort_relr (htab);
14312 if (htab->relr_count != 0 && relr_addr == NULL)
14313 return false;
14315 size_t i = 0;
14316 while (i < htab->relr_count)
14318 bfd_vma base = relr_addr[i];
14319 htab->elf.srelrdyn->size += 8;
14320 i++;
14321 /* Handle possible duplicate address. This can happen
14322 as sections increase in size when adding stubs. */
14323 while (i < htab->relr_count
14324 && relr_addr[i] == base)
14325 i++;
14326 base += 8;
14327 while (1)
14329 size_t start_i = i;
14330 while (i < htab->relr_count
14331 && relr_addr[i] - base < 63 * 8
14332 && (relr_addr[i] - base) % 8 == 0)
14333 i++;
14334 if (i == start_i)
14335 break;
14336 htab->elf.srelrdyn->size += 8;
14337 base += 63 * 8;
14340 free (relr_addr);
14343 for (group = htab->group; group != NULL; group = group->next)
14344 if (group->stub_sec != NULL
14345 && group->stub_sec->rawsize != group->stub_sec->size
14346 && (htab->stub_iteration <= STUB_SHRINK_ITER
14347 || group->stub_sec->rawsize < group->stub_sec->size))
14348 break;
14350 if (group == NULL
14351 && (!htab->stub_changed
14352 || htab->stub_iteration > STUB_SHRINK_ITER)
14353 && (htab->brlt->rawsize == htab->brlt->size
14354 || (htab->stub_iteration > STUB_SHRINK_ITER
14355 && htab->brlt->rawsize > htab->brlt->size))
14356 && (htab->elf.srelrdyn == NULL
14357 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14358 || (htab->stub_iteration > STUB_SHRINK_ITER
14359 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14360 && (htab->glink_eh_frame == NULL
14361 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14362 && (htab->tga_group == NULL
14363 || htab->stub_iteration > 1))
14364 break;
14366 if (htab->stub_iteration > STUB_SHRINK_ITER)
14368 for (group = htab->group; group != NULL; group = group->next)
14369 if (group->stub_sec != NULL
14370 && group->stub_sec->size < group->stub_sec->rawsize)
14371 group->stub_sec->size = group->stub_sec->rawsize;
14373 if (htab->brlt->size < htab->brlt->rawsize)
14374 htab->brlt->size = htab->brlt->rawsize;
14376 if (htab->elf.srelrdyn != NULL
14377 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14378 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14381 /* Ask the linker to do its stuff. */
14382 (*htab->params->layout_sections_again) ();
14385 if (htab->glink_eh_frame != NULL
14386 && htab->glink_eh_frame->size != 0)
14388 bfd_vma val;
14389 bfd_byte *p, *last_fde;
14390 size_t last_fde_len, size, align, pad;
14391 struct map_stub *group;
14393 /* It is necessary to at least have a rough outline of the
14394 linker generated CIEs and FDEs written before
14395 bfd_elf_discard_info is run, in order for these FDEs to be
14396 indexed in .eh_frame_hdr. */
14397 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14398 if (p == NULL)
14399 return false;
14400 htab->glink_eh_frame->contents = p;
14401 last_fde = p;
14402 align = 4;
14404 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14405 /* CIE length (rewrite in case little-endian). */
14406 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14407 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14408 p += last_fde_len + 4;
14410 for (group = htab->group; group != NULL; group = group->next)
14411 if (group->eh_size != 0)
14413 group->eh_base = p - htab->glink_eh_frame->contents;
14414 last_fde = p;
14415 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14416 /* FDE length. */
14417 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14418 p += 4;
14419 /* CIE pointer. */
14420 val = p - htab->glink_eh_frame->contents;
14421 bfd_put_32 (htab->elf.dynobj, val, p);
14422 p += 4;
14423 /* Offset to stub section, written later. */
14424 p += 4;
14425 /* stub section size. */
14426 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14427 p += 4;
14428 /* Augmentation. */
14429 p += 1;
14430 /* Make sure we don't have all nops. This is enough for
14431 elf-eh-frame.c to detect the last non-nop opcode. */
14432 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14433 p = last_fde + last_fde_len + 4;
14435 if (htab->glink != NULL && htab->glink->size != 0)
14437 last_fde = p;
14438 last_fde_len = ((24 + align - 1) & -align) - 4;
14439 /* FDE length. */
14440 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14441 p += 4;
14442 /* CIE pointer. */
14443 val = p - htab->glink_eh_frame->contents;
14444 bfd_put_32 (htab->elf.dynobj, val, p);
14445 p += 4;
14446 /* Offset to .glink, written later. */
14447 p += 4;
14448 /* .glink size. */
14449 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14450 p += 4;
14451 /* Augmentation. */
14452 p += 1;
14454 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14455 *p++ = DW_CFA_register;
14456 *p++ = 65;
14457 *p++ = htab->opd_abi ? 12 : 0;
14458 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14459 *p++ = DW_CFA_restore_extended;
14460 *p++ = 65;
14461 p += ((24 + align - 1) & -align) - 24;
14463 /* Subsume any padding into the last FDE if user .eh_frame
14464 sections are aligned more than glink_eh_frame. Otherwise any
14465 zero padding will be seen as a terminator. */
14466 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14467 size = p - htab->glink_eh_frame->contents;
14468 pad = ((size + align - 1) & -align) - size;
14469 htab->glink_eh_frame->size = size + pad;
14470 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14473 maybe_strip_output (info, htab->brlt);
14474 if (htab->relbrlt != NULL)
14475 maybe_strip_output (info, htab->relbrlt);
14476 if (htab->glink_eh_frame != NULL)
14477 maybe_strip_output (info, htab->glink_eh_frame);
14478 if (htab->elf.srelrdyn != NULL)
14479 maybe_strip_output (info, htab->elf.srelrdyn);
14481 return true;
14484 /* Called after we have determined section placement. If sections
14485 move, we'll be called again. Provide a value for TOCstart. */
14487 bfd_vma
14488 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14490 asection *s;
14491 bfd_vma TOCstart, adjust;
14493 if (info != NULL)
14495 struct elf_link_hash_entry *h;
14496 struct elf_link_hash_table *htab = elf_hash_table (info);
14498 if (is_elf_hash_table (&htab->root)
14499 && htab->hgot != NULL)
14500 h = htab->hgot;
14501 else
14503 h = (struct elf_link_hash_entry *)
14504 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14505 if (is_elf_hash_table (&htab->root))
14506 htab->hgot = h;
14508 if (h != NULL
14509 && h->root.type == bfd_link_hash_defined
14510 && !h->root.linker_def
14511 && (!is_elf_hash_table (&htab->root)
14512 || h->def_regular))
14514 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14515 _bfd_set_gp_value (obfd, TOCstart);
14516 return TOCstart;
14520 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14521 order. The TOC starts where the first of these sections starts. */
14522 s = bfd_get_section_by_name (obfd, ".got");
14523 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14524 s = bfd_get_section_by_name (obfd, ".toc");
14525 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14526 s = bfd_get_section_by_name (obfd, ".tocbss");
14527 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14528 s = bfd_get_section_by_name (obfd, ".plt");
14529 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14531 /* This may happen for
14532 o references to TOC base (SYM@toc / TOC[tc0]) without a
14533 .toc directive
14534 o bad linker script
14535 o --gc-sections and empty TOC sections
14537 FIXME: Warn user? */
14539 /* Look for a likely section. We probably won't even be
14540 using TOCstart. */
14541 for (s = obfd->sections; s != NULL; s = s->next)
14542 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14543 | SEC_EXCLUDE))
14544 == (SEC_ALLOC | SEC_SMALL_DATA))
14545 break;
14546 if (s == NULL)
14547 for (s = obfd->sections; s != NULL; s = s->next)
14548 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14549 == (SEC_ALLOC | SEC_SMALL_DATA))
14550 break;
14551 if (s == NULL)
14552 for (s = obfd->sections; s != NULL; s = s->next)
14553 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14554 == SEC_ALLOC)
14555 break;
14556 if (s == NULL)
14557 for (s = obfd->sections; s != NULL; s = s->next)
14558 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14559 break;
14562 TOCstart = 0;
14563 if (s != NULL)
14564 TOCstart = s->output_section->vma + s->output_offset;
14566 /* Force alignment. */
14567 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14568 TOCstart -= adjust;
14569 _bfd_set_gp_value (obfd, TOCstart);
14571 if (info != NULL && s != NULL)
14573 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14575 if (htab != NULL)
14577 if (htab->elf.hgot != NULL)
14579 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14580 htab->elf.hgot->root.u.def.section = s;
14583 else
14585 struct bfd_link_hash_entry *bh = NULL;
14586 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14587 s, TOC_BASE_OFF - adjust,
14588 NULL, false, false, &bh);
14591 return TOCstart;
14594 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14595 write out any global entry stubs, and PLT relocations. */
14597 static bool
14598 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14600 struct bfd_link_info *info;
14601 struct ppc_link_hash_table *htab;
14602 struct plt_entry *ent;
14603 asection *s;
14605 if (h->root.type == bfd_link_hash_indirect)
14606 return true;
14608 info = inf;
14609 htab = ppc_hash_table (info);
14610 if (htab == NULL)
14611 return false;
14613 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14614 if (ent->plt.offset != (bfd_vma) -1)
14616 /* This symbol has an entry in the procedure linkage
14617 table. Set it up. */
14618 Elf_Internal_Rela rela;
14619 asection *plt, *relplt;
14620 bfd_byte *loc;
14622 if (use_local_plt (info, h))
14624 if (!(h->def_regular
14625 && (h->root.type == bfd_link_hash_defined
14626 || h->root.type == bfd_link_hash_defweak)))
14627 continue;
14628 if (h->type == STT_GNU_IFUNC)
14630 plt = htab->elf.iplt;
14631 relplt = htab->elf.irelplt;
14632 htab->elf.ifunc_resolvers = true;
14633 if (htab->opd_abi)
14634 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14635 else
14636 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14638 else
14640 plt = htab->pltlocal;
14641 relplt = NULL;
14642 if (bfd_link_pic (info)
14643 && !(info->enable_dt_relr && !htab->opd_abi))
14645 relplt = htab->relpltlocal;
14646 if (htab->opd_abi)
14647 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14648 else
14649 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14652 rela.r_addend = defined_sym_val (h) + ent->addend;
14654 if (relplt == NULL)
14656 loc = plt->contents + ent->plt.offset;
14657 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14658 if (htab->opd_abi)
14660 bfd_vma toc = elf_gp (info->output_bfd);
14661 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14662 bfd_put_64 (info->output_bfd, toc, loc + 8);
14665 else
14667 rela.r_offset = (plt->output_section->vma
14668 + plt->output_offset
14669 + ent->plt.offset);
14670 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
14671 relplt));
14674 else
14676 rela.r_offset = (htab->elf.splt->output_section->vma
14677 + htab->elf.splt->output_offset
14678 + ent->plt.offset);
14679 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14680 rela.r_addend = ent->addend;
14681 loc = (htab->elf.srelplt->contents
14682 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14683 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14684 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14685 htab->elf.ifunc_resolvers = true;
14686 BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
14687 loc, htab->elf.srelplt));
14691 if (!h->pointer_equality_needed)
14692 return true;
14694 if (h->def_regular)
14695 return true;
14697 s = htab->global_entry;
14698 if (s == NULL || s->size == 0)
14699 return true;
14701 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14702 if (ent->plt.offset != (bfd_vma) -1
14703 && ent->addend == 0)
14705 bfd_byte *p;
14706 asection *plt;
14707 bfd_vma off;
14709 p = s->contents + h->root.u.def.value;
14710 plt = htab->elf.splt;
14711 if (use_local_plt (info, h))
14713 if (h->type == STT_GNU_IFUNC)
14714 plt = htab->elf.iplt;
14715 else
14716 plt = htab->pltlocal;
14718 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14719 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14721 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14723 info->callbacks->einfo
14724 (_("%P: linkage table error against `%pT'\n"),
14725 h->root.root.string);
14726 bfd_set_error (bfd_error_bad_value);
14727 htab->stub_error = true;
14730 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14731 if (htab->params->emit_stub_syms)
14733 size_t len = strlen (h->root.root.string);
14734 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14736 if (name == NULL)
14737 return false;
14739 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14740 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14741 if (h == NULL)
14742 return false;
14743 if (h->root.type == bfd_link_hash_new)
14745 h->root.type = bfd_link_hash_defined;
14746 h->root.u.def.section = s;
14747 h->root.u.def.value = p - s->contents;
14748 h->ref_regular = 1;
14749 h->def_regular = 1;
14750 h->ref_regular_nonweak = 1;
14751 h->forced_local = 1;
14752 h->non_elf = 0;
14753 h->root.linker_def = 1;
14757 if (PPC_HA (off) != 0)
14759 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14760 p += 4;
14762 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14763 p += 4;
14764 bfd_put_32 (s->owner, MTCTR_R12, p);
14765 p += 4;
14766 bfd_put_32 (s->owner, BCTR, p);
14767 break;
14769 return true;
14772 /* Write PLT relocs for locals. */
14774 static bool
14775 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14777 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14778 bfd *ibfd;
14780 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14782 struct got_entry **lgot_ents, **end_lgot_ents;
14783 struct plt_entry **local_plt, **lplt, **end_local_plt;
14784 Elf_Internal_Shdr *symtab_hdr;
14785 bfd_size_type locsymcount;
14786 Elf_Internal_Sym *local_syms = NULL;
14787 struct plt_entry *ent;
14789 if (!is_ppc64_elf (ibfd))
14790 continue;
14792 lgot_ents = elf_local_got_ents (ibfd);
14793 if (!lgot_ents)
14794 continue;
14796 symtab_hdr = &elf_symtab_hdr (ibfd);
14797 locsymcount = symtab_hdr->sh_info;
14798 end_lgot_ents = lgot_ents + locsymcount;
14799 local_plt = (struct plt_entry **) end_lgot_ents;
14800 end_local_plt = local_plt + locsymcount;
14801 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14802 for (ent = *lplt; ent != NULL; ent = ent->next)
14803 if (ent->plt.offset != (bfd_vma) -1)
14805 Elf_Internal_Sym *sym;
14806 asection *sym_sec;
14807 asection *plt, *relplt;
14808 bfd_vma val;
14810 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14811 lplt - local_plt, ibfd))
14813 if (symtab_hdr->contents != (unsigned char *) local_syms)
14814 free (local_syms);
14815 return false;
14818 val = sym->st_value + ent->addend;
14819 if (sym_sec != NULL && sym_sec->output_section != NULL)
14820 val += sym_sec->output_offset + sym_sec->output_section->vma;
14822 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14824 htab->elf.ifunc_resolvers = true;
14825 plt = htab->elf.iplt;
14826 relplt = htab->elf.irelplt;
14828 else
14830 plt = htab->pltlocal;
14831 relplt = NULL;
14832 if (bfd_link_pic (info)
14833 && !(info->enable_dt_relr && !htab->opd_abi))
14834 relplt = htab->relpltlocal;
14837 if (relplt == NULL)
14839 bfd_byte *loc = plt->contents + ent->plt.offset;
14840 bfd_put_64 (info->output_bfd, val, loc);
14841 if (htab->opd_abi)
14843 bfd_vma toc = elf_gp (ibfd);
14844 bfd_put_64 (info->output_bfd, toc, loc + 8);
14847 else
14849 Elf_Internal_Rela rela;
14850 rela.r_offset = (ent->plt.offset
14851 + plt->output_offset
14852 + plt->output_section->vma);
14853 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14855 if (htab->opd_abi)
14856 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14857 else
14858 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14860 else
14862 if (htab->opd_abi)
14863 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14864 else
14865 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14867 rela.r_addend = val;
14868 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
14869 &rela, relplt));
14873 if (local_syms != NULL
14874 && symtab_hdr->contents != (unsigned char *) local_syms)
14876 if (!info->keep_memory)
14877 free (local_syms);
14878 else
14879 symtab_hdr->contents = (unsigned char *) local_syms;
14882 return true;
14885 /* Emit the static wrapper function preserving registers around a
14886 __tls_get_addr_opt call. */
14888 static bool
14889 emit_tga_desc (struct ppc_link_hash_table *htab)
14891 asection *stub_sec = htab->tga_group->stub_sec;
14892 unsigned int cfa_updt = 11 * 4;
14893 bfd_byte *p;
14894 bfd_vma to, from, delta;
14896 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14897 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14898 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14899 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14900 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14901 delta = to - from;
14902 if (delta + (1 << 25) >= 1 << 26)
14904 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14905 htab->stub_error = true;
14906 return false;
14909 p = stub_sec->contents;
14910 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14911 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14912 p += 4;
14913 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14914 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14917 /* Emit eh_frame describing the static wrapper function. */
14919 static bfd_byte *
14920 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14922 unsigned int cfa_updt = 11 * 4;
14923 unsigned int i;
14925 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14926 *p++ = DW_CFA_def_cfa_offset;
14927 if (htab->opd_abi)
14929 *p++ = 128;
14930 *p++ = 1;
14932 else
14933 *p++ = 96;
14934 *p++ = DW_CFA_offset_extended_sf;
14935 *p++ = 65;
14936 *p++ = (-16 / 8) & 0x7f;
14937 for (i = 4; i < 12; i++)
14939 *p++ = DW_CFA_offset + i;
14940 *p++ = (htab->opd_abi ? 13 : 12) - i;
14942 *p++ = DW_CFA_advance_loc + 10;
14943 *p++ = DW_CFA_def_cfa_offset;
14944 *p++ = 0;
14945 for (i = 4; i < 12; i++)
14946 *p++ = DW_CFA_restore + i;
14947 *p++ = DW_CFA_advance_loc + 2;
14948 *p++ = DW_CFA_restore_extended;
14949 *p++ = 65;
14950 return p;
14953 /* Build all the stubs associated with the current output file.
14954 The stubs are kept in a hash table attached to the main linker
14955 hash table. This function is called via gldelf64ppc_finish. */
14957 bool
14958 ppc64_elf_build_stubs (struct bfd_link_info *info,
14959 char **stats)
14961 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14962 struct map_stub *group;
14963 asection *stub_sec;
14964 bfd_byte *p;
14965 int stub_sec_count = 0;
14967 if (htab == NULL)
14968 return false;
14970 /* Allocate memory to hold the linker stubs. */
14971 for (group = htab->group; group != NULL; group = group->next)
14973 group->eh_size = 0;
14974 group->lr_restore = 0;
14975 if ((stub_sec = group->stub_sec) != NULL
14976 && stub_sec->size != 0)
14978 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14979 stub_sec->size);
14980 if (stub_sec->contents == NULL)
14981 return false;
14982 stub_sec->size = 0;
14986 if (htab->glink != NULL && htab->glink->size != 0)
14988 unsigned int indx;
14989 bfd_vma plt0;
14991 /* Build the .glink plt call stub. */
14992 if (htab->params->emit_stub_syms)
14994 struct elf_link_hash_entry *h;
14995 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14996 true, false, false);
14997 if (h == NULL)
14998 return false;
14999 if (h->root.type == bfd_link_hash_new)
15001 h->root.type = bfd_link_hash_defined;
15002 h->root.u.def.section = htab->glink;
15003 h->root.u.def.value = 8;
15004 h->ref_regular = 1;
15005 h->def_regular = 1;
15006 h->ref_regular_nonweak = 1;
15007 h->forced_local = 1;
15008 h->non_elf = 0;
15009 h->root.linker_def = 1;
15012 plt0 = (htab->elf.splt->output_section->vma
15013 + htab->elf.splt->output_offset
15014 - 16);
15015 if (info->emitrelocations)
15017 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15018 if (r == NULL)
15019 return false;
15020 r->r_offset = (htab->glink->output_offset
15021 + htab->glink->output_section->vma);
15022 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15023 r->r_addend = plt0;
15025 p = htab->glink->contents;
15026 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
15027 bfd_put_64 (htab->glink->owner, plt0, p);
15028 p += 8;
15029 if (htab->opd_abi)
15031 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15032 p += 4;
15033 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15034 p += 4;
15035 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15036 p += 4;
15037 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15038 p += 4;
15039 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15040 p += 4;
15041 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15042 p += 4;
15043 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15044 p += 4;
15045 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15046 p += 4;
15047 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15048 p += 4;
15049 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15050 p += 4;
15052 else
15054 unsigned int insn;
15056 /* 0:
15057 . .quad plt0-1f # plt0 entry relative to 1:
15059 # We get here with r12 initially @ a glink branch
15060 # Load the address of _dl_runtime_resolve from plt0 and
15061 # jump to it, with r0 set to the index of the PLT entry
15062 # to be resolved and r11 the link map.
15063 __glink_PLTresolve:
15064 . std %r2,24(%r1) # optional
15065 . mflr %r0
15066 . bcl 20,31,1f
15068 . mflr %r11
15069 . mtlr %r0
15070 . ld %r0,(0b-1b)(%r11)
15071 . sub %r12,%r12,%r11
15072 . add %r11,%r0,%r11
15073 . addi %r0,%r12,1b-2f
15074 . ld %r12,0(%r11)
15075 . srdi %r0,%r0,2
15076 . mtctr %r12
15077 . ld %r11,8(%r11)
15078 . bctr
15080 . b __glink_PLTresolve
15081 . ...
15082 . b __glink_PLTresolve */
15084 if (htab->has_plt_localentry0)
15086 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15087 p += 4;
15089 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15090 p += 4;
15091 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15092 p += 4;
15093 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15094 p += 4;
15095 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15096 p += 4;
15097 if (htab->has_plt_localentry0)
15098 insn = LD_R0_0R11 | (-20 & 0xfffc);
15099 else
15100 insn = LD_R0_0R11 | (-16 & 0xfffc);
15101 bfd_put_32 (htab->glink->owner, insn, p);
15102 p += 4;
15103 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15104 p += 4;
15105 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15106 p += 4;
15107 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15108 p += 4;
15109 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15110 p += 4;
15111 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15112 p += 4;
15113 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15114 p += 4;
15115 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15116 p += 4;
15118 bfd_put_32 (htab->glink->owner, BCTR, p);
15119 p += 4;
15120 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15122 /* Build the .glink lazy link call stubs. */
15123 indx = 0;
15124 while (p < htab->glink->contents + htab->glink->size)
15126 if (htab->opd_abi)
15128 if (indx < 0x8000)
15130 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15131 p += 4;
15133 else
15135 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15136 p += 4;
15137 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15139 p += 4;
15142 bfd_put_32 (htab->glink->owner,
15143 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15144 indx++;
15145 p += 4;
15149 if (htab->tga_group != NULL)
15151 htab->tga_group->lr_restore = 23 * 4;
15152 htab->tga_group->stub_sec->size = 24 * 4;
15153 if (!emit_tga_desc (htab))
15154 return false;
15155 if (htab->glink_eh_frame != NULL
15156 && htab->glink_eh_frame->size != 0)
15158 size_t align = 4;
15160 p = htab->glink_eh_frame->contents;
15161 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15162 p += 17;
15163 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15167 /* Build .glink global entry stubs, and PLT relocs for globals. */
15168 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15170 if (!write_plt_relocs_for_local_syms (info))
15171 return false;
15173 if (htab->brlt != NULL && htab->brlt->size != 0)
15175 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15176 htab->brlt->size);
15177 if (htab->brlt->contents == NULL)
15178 return false;
15180 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15182 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15183 htab->relbrlt->size);
15184 if (htab->relbrlt->contents == NULL)
15185 return false;
15188 /* Build the stubs as directed by the stub hash table. */
15189 htab->stub_id = 0;
15190 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15192 for (group = htab->group; group != NULL; group = group->next)
15193 if (group->needs_save_res)
15194 group->stub_sec->size += htab->sfpr->size;
15196 if (htab->relbrlt != NULL)
15197 htab->relbrlt->reloc_count = 0;
15199 if (htab->params->plt_stub_align != 0)
15200 for (group = htab->group; group != NULL; group = group->next)
15201 if ((stub_sec = group->stub_sec) != NULL)
15203 int align = abs (htab->params->plt_stub_align);
15204 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15207 for (group = htab->group; group != NULL; group = group->next)
15208 if (group->needs_save_res)
15210 stub_sec = group->stub_sec;
15211 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15212 htab->sfpr->contents, htab->sfpr->size);
15213 if (htab->params->emit_stub_syms)
15215 unsigned int i;
15217 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15218 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15219 return false;
15223 if (htab->glink_eh_frame != NULL
15224 && htab->glink_eh_frame->size != 0)
15226 bfd_vma val;
15227 size_t align = 4;
15229 p = htab->glink_eh_frame->contents;
15230 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15232 for (group = htab->group; group != NULL; group = group->next)
15233 if (group->eh_size != 0)
15235 /* Offset to stub section. */
15236 val = (group->stub_sec->output_section->vma
15237 + group->stub_sec->output_offset);
15238 val -= (htab->glink_eh_frame->output_section->vma
15239 + htab->glink_eh_frame->output_offset
15240 + (p + 8 - htab->glink_eh_frame->contents));
15241 if (val + 0x80000000 > 0xffffffff)
15243 _bfd_error_handler
15244 (_("%s offset too large for .eh_frame sdata4 encoding"),
15245 group->stub_sec->name);
15246 return false;
15248 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15249 p += (group->eh_size + 17 + 3) & -4;
15251 if (htab->glink != NULL && htab->glink->size != 0)
15253 /* Offset to .glink. */
15254 val = (htab->glink->output_section->vma
15255 + htab->glink->output_offset
15256 + 8);
15257 val -= (htab->glink_eh_frame->output_section->vma
15258 + htab->glink_eh_frame->output_offset
15259 + (p + 8 - htab->glink_eh_frame->contents));
15260 if (val + 0x80000000 > 0xffffffff)
15262 _bfd_error_handler
15263 (_("%s offset too large for .eh_frame sdata4 encoding"),
15264 htab->glink->name);
15265 return false;
15267 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15268 p += (24 + align - 1) & -align;
15272 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15274 htab->elf.srelrdyn->contents
15275 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15276 if (htab->elf.srelrdyn->contents == NULL)
15277 return false;
15279 bfd_vma *relr_addr = sort_relr (htab);
15280 if (htab->relr_count != 0 && relr_addr == NULL)
15281 return false;
15283 size_t i = 0;
15284 bfd_byte *loc = htab->elf.srelrdyn->contents;
15285 while (i < htab->relr_count)
15287 bfd_vma base = relr_addr[i];
15288 BFD_ASSERT (base % 2 == 0);
15289 bfd_put_64 (htab->elf.dynobj, base, loc);
15290 loc += 8;
15291 i++;
15292 while (i < htab->relr_count
15293 && relr_addr[i] == base)
15295 htab->stub_error = true;
15296 i++;
15298 base += 8;
15299 while (1)
15301 bfd_vma bits = 0;
15302 while (i < htab->relr_count
15303 && relr_addr[i] - base < 63 * 8
15304 && (relr_addr[i] - base) % 8 == 0)
15306 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15307 i++;
15309 if (bits == 0)
15310 break;
15311 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15312 loc += 8;
15313 base += 63 * 8;
15316 free (relr_addr);
15317 /* Pad any excess with 1's, a do-nothing encoding. */
15318 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15319 < htab->elf.srelrdyn->size)
15321 bfd_put_64 (htab->elf.dynobj, 1, loc);
15322 loc += 8;
15326 for (group = htab->group; group != NULL; group = group->next)
15327 if ((stub_sec = group->stub_sec) != NULL)
15329 stub_sec_count += 1;
15330 if (stub_sec->rawsize != stub_sec->size
15331 && (htab->stub_iteration <= STUB_SHRINK_ITER
15332 || stub_sec->rawsize < stub_sec->size))
15333 break;
15336 if (group != NULL)
15337 htab->stub_error = true;
15339 if (htab->stub_error)
15341 _bfd_error_handler (_("stubs don't match calculated size"));
15342 return false;
15345 if (stats != NULL)
15347 char *groupmsg;
15348 if (asprintf (&groupmsg,
15349 ngettext ("linker stubs in %u group",
15350 "linker stubs in %u groups",
15351 stub_sec_count),
15352 stub_sec_count) < 0)
15353 *stats = NULL;
15354 else
15356 if (asprintf (stats, _("%s, iter %u\n"
15357 " branch %lu\n"
15358 " long branch %lu\n"
15359 " plt call %lu\n"
15360 " global entry %lu"),
15361 groupmsg, htab->stub_iteration,
15362 htab->stub_count[ppc_stub_long_branch - 1],
15363 htab->stub_count[ppc_stub_plt_branch - 1],
15364 htab->stub_count[ppc_stub_plt_call - 1],
15365 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15366 *stats = NULL;
15367 free (groupmsg);
15370 return true;
15373 /* What to do when ld finds relocations against symbols defined in
15374 discarded sections. */
15376 static unsigned int
15377 ppc64_elf_action_discarded (asection *sec)
15379 if (strcmp (".opd", sec->name) == 0)
15380 return 0;
15382 if (strcmp (".toc", sec->name) == 0)
15383 return 0;
15385 if (strcmp (".toc1", sec->name) == 0)
15386 return 0;
15388 return _bfd_elf_default_action_discarded (sec);
15391 /* These are the dynamic relocations supported by glibc. */
15393 static bool
15394 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15396 switch (r_type)
15398 case R_PPC64_RELATIVE:
15399 case R_PPC64_NONE:
15400 case R_PPC64_ADDR64:
15401 case R_PPC64_GLOB_DAT:
15402 case R_PPC64_IRELATIVE:
15403 case R_PPC64_JMP_IREL:
15404 case R_PPC64_JMP_SLOT:
15405 case R_PPC64_DTPMOD64:
15406 case R_PPC64_DTPREL64:
15407 case R_PPC64_TPREL64:
15408 case R_PPC64_TPREL16_LO_DS:
15409 case R_PPC64_TPREL16_DS:
15410 case R_PPC64_TPREL16:
15411 case R_PPC64_TPREL16_LO:
15412 case R_PPC64_TPREL16_HI:
15413 case R_PPC64_TPREL16_HIGH:
15414 case R_PPC64_TPREL16_HA:
15415 case R_PPC64_TPREL16_HIGHA:
15416 case R_PPC64_TPREL16_HIGHER:
15417 case R_PPC64_TPREL16_HIGHEST:
15418 case R_PPC64_TPREL16_HIGHERA:
15419 case R_PPC64_TPREL16_HIGHESTA:
15420 case R_PPC64_ADDR16_LO_DS:
15421 case R_PPC64_ADDR16_LO:
15422 case R_PPC64_ADDR16_HI:
15423 case R_PPC64_ADDR16_HIGH:
15424 case R_PPC64_ADDR16_HA:
15425 case R_PPC64_ADDR16_HIGHA:
15426 case R_PPC64_REL30:
15427 case R_PPC64_COPY:
15428 case R_PPC64_UADDR64:
15429 case R_PPC64_UADDR32:
15430 case R_PPC64_ADDR32:
15431 case R_PPC64_ADDR24:
15432 case R_PPC64_ADDR16:
15433 case R_PPC64_UADDR16:
15434 case R_PPC64_ADDR16_DS:
15435 case R_PPC64_ADDR16_HIGHER:
15436 case R_PPC64_ADDR16_HIGHEST:
15437 case R_PPC64_ADDR16_HIGHERA:
15438 case R_PPC64_ADDR16_HIGHESTA:
15439 case R_PPC64_ADDR14:
15440 case R_PPC64_ADDR14_BRTAKEN:
15441 case R_PPC64_ADDR14_BRNTAKEN:
15442 case R_PPC64_REL32:
15443 case R_PPC64_REL64:
15444 return true;
15446 default:
15447 return false;
15451 /* The RELOCATE_SECTION function is called by the ELF backend linker
15452 to handle the relocations for a section.
15454 The relocs are always passed as Rela structures; if the section
15455 actually uses Rel structures, the r_addend field will always be
15456 zero.
15458 This function is responsible for adjust the section contents as
15459 necessary, and (if using Rela relocs and generating a
15460 relocatable output file) adjusting the reloc addend as
15461 necessary.
15463 This function does not have to worry about setting the reloc
15464 address or the reloc symbol index.
15466 LOCAL_SYMS is a pointer to the swapped in local symbols.
15468 LOCAL_SECTIONS is an array giving the section in the input file
15469 corresponding to the st_shndx field of each local symbol.
15471 The global hash table entry for the global symbols can be found
15472 via elf_sym_hashes (input_bfd).
15474 When generating relocatable output, this function must handle
15475 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15476 going to be the section symbol corresponding to the output
15477 section, which means that the addend must be adjusted
15478 accordingly. */
15480 static int
15481 ppc64_elf_relocate_section (bfd *output_bfd,
15482 struct bfd_link_info *info,
15483 bfd *input_bfd,
15484 asection *input_section,
15485 bfd_byte *contents,
15486 Elf_Internal_Rela *relocs,
15487 Elf_Internal_Sym *local_syms,
15488 asection **local_sections)
15490 struct ppc_link_hash_table *htab;
15491 Elf_Internal_Shdr *symtab_hdr;
15492 struct elf_link_hash_entry **sym_hashes;
15493 Elf_Internal_Rela *rel;
15494 Elf_Internal_Rela *wrel;
15495 Elf_Internal_Rela *relend;
15496 Elf_Internal_Rela outrel;
15497 bfd_byte *loc;
15498 struct got_entry **local_got_ents;
15499 bfd_vma TOCstart;
15500 bool ret = true;
15501 bool is_opd;
15502 /* Assume 'at' branch hints. */
15503 bool is_isa_v2 = true;
15504 bool warned_dynamic = false;
15505 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15507 /* Initialize howto table if needed. */
15508 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15509 ppc_howto_init ();
15511 htab = ppc_hash_table (info);
15512 if (htab == NULL)
15513 return false;
15515 /* Don't relocate stub sections. */
15516 if (input_section->owner == htab->params->stub_bfd)
15517 return true;
15519 if (!is_ppc64_elf (input_bfd))
15521 bfd_set_error (bfd_error_wrong_format);
15522 return false;
15525 local_got_ents = elf_local_got_ents (input_bfd);
15526 TOCstart = elf_gp (output_bfd);
15527 symtab_hdr = &elf_symtab_hdr (input_bfd);
15528 sym_hashes = elf_sym_hashes (input_bfd);
15529 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15531 rel = wrel = relocs;
15532 relend = relocs + input_section->reloc_count;
15533 for (; rel < relend; wrel++, rel++)
15535 enum elf_ppc64_reloc_type r_type;
15536 bfd_vma addend;
15537 bfd_reloc_status_type r;
15538 Elf_Internal_Sym *sym;
15539 asection *sec;
15540 struct elf_link_hash_entry *h_elf;
15541 struct ppc_link_hash_entry *h;
15542 struct ppc_link_hash_entry *fdh;
15543 const char *sym_name;
15544 unsigned long r_symndx, toc_symndx;
15545 bfd_vma toc_addend;
15546 unsigned char tls_mask, tls_gd, tls_type;
15547 unsigned char sym_type;
15548 bfd_vma relocation;
15549 bool unresolved_reloc, save_unresolved_reloc;
15550 bool warned;
15551 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15552 unsigned int insn;
15553 unsigned int mask;
15554 struct ppc_stub_hash_entry *stub_entry;
15555 bfd_vma max_br_offset;
15556 bfd_vma from;
15557 Elf_Internal_Rela orig_rel;
15558 reloc_howto_type *howto;
15559 struct reloc_howto_struct alt_howto;
15560 uint64_t pinsn;
15561 bfd_vma offset;
15563 again:
15564 orig_rel = *rel;
15566 r_type = ELF64_R_TYPE (rel->r_info);
15567 r_symndx = ELF64_R_SYM (rel->r_info);
15569 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15570 symbol of the previous ADDR64 reloc. The symbol gives us the
15571 proper TOC base to use. */
15572 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15573 && wrel != relocs
15574 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15575 && is_opd)
15576 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15578 sym = NULL;
15579 sec = NULL;
15580 h_elf = NULL;
15581 sym_name = NULL;
15582 unresolved_reloc = false;
15583 warned = false;
15585 if (r_symndx < symtab_hdr->sh_info)
15587 /* It's a local symbol. */
15588 struct _opd_sec_data *opd;
15590 sym = local_syms + r_symndx;
15591 sec = local_sections[r_symndx];
15592 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15593 sym_type = ELF64_ST_TYPE (sym->st_info);
15594 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15595 opd = get_opd_info (sec);
15596 if (opd != NULL && opd->adjust != NULL)
15598 long adjust = opd->adjust[OPD_NDX (sym->st_value
15599 + rel->r_addend)];
15600 if (adjust == -1)
15601 relocation = 0;
15602 else
15604 /* If this is a relocation against the opd section sym
15605 and we have edited .opd, adjust the reloc addend so
15606 that ld -r and ld --emit-relocs output is correct.
15607 If it is a reloc against some other .opd symbol,
15608 then the symbol value will be adjusted later. */
15609 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15610 rel->r_addend += adjust;
15611 else
15612 relocation += adjust;
15616 else
15618 bool ignored;
15620 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15621 r_symndx, symtab_hdr, sym_hashes,
15622 h_elf, sec, relocation,
15623 unresolved_reloc, warned, ignored);
15624 sym_name = h_elf->root.root.string;
15625 sym_type = h_elf->type;
15626 if (sec != NULL
15627 && sec->owner == output_bfd
15628 && strcmp (sec->name, ".opd") == 0)
15630 /* This is a symbol defined in a linker script. All
15631 such are defined in output sections, even those
15632 defined by simple assignment from a symbol defined in
15633 an input section. Transfer the symbol to an
15634 appropriate input .opd section, so that a branch to
15635 this symbol will be mapped to the location specified
15636 by the opd entry. */
15637 struct bfd_link_order *lo;
15638 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15639 if (lo->type == bfd_indirect_link_order)
15641 asection *isec = lo->u.indirect.section;
15642 if (h_elf->root.u.def.value >= isec->output_offset
15643 && h_elf->root.u.def.value < (isec->output_offset
15644 + isec->size))
15646 h_elf->root.u.def.value -= isec->output_offset;
15647 h_elf->root.u.def.section = isec;
15648 sec = isec;
15649 break;
15654 h = ppc_elf_hash_entry (h_elf);
15656 if (sec != NULL && discarded_section (sec))
15658 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15659 input_bfd, input_section,
15660 contents, rel->r_offset);
15661 wrel->r_offset = rel->r_offset;
15662 wrel->r_info = 0;
15663 wrel->r_addend = 0;
15665 /* For ld -r, remove relocations in debug sections against
15666 symbols defined in discarded sections. Not done for
15667 non-debug to preserve relocs in .eh_frame which the
15668 eh_frame editing code expects to be present. */
15669 if (bfd_link_relocatable (info)
15670 && (input_section->flags & SEC_DEBUGGING))
15671 wrel--;
15673 continue;
15676 if (bfd_link_relocatable (info))
15677 goto copy_reloc;
15679 if (h != NULL && &h->elf == htab->elf.hgot)
15681 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15682 sec = bfd_abs_section_ptr;
15683 unresolved_reloc = false;
15686 /* TLS optimizations. Replace instruction sequences and relocs
15687 based on information we collected in tls_optimize. We edit
15688 RELOCS so that --emit-relocs will output something sensible
15689 for the final instruction stream. */
15690 tls_mask = 0;
15691 tls_gd = 0;
15692 toc_symndx = 0;
15693 if (h != NULL)
15694 tls_mask = h->tls_mask;
15695 else if (local_got_ents != NULL)
15697 struct plt_entry **local_plt = (struct plt_entry **)
15698 (local_got_ents + symtab_hdr->sh_info);
15699 unsigned char *lgot_masks = (unsigned char *)
15700 (local_plt + symtab_hdr->sh_info);
15701 tls_mask = lgot_masks[r_symndx];
15703 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15704 && (r_type == R_PPC64_TLS
15705 || r_type == R_PPC64_TLSGD
15706 || r_type == R_PPC64_TLSLD))
15708 /* Check for toc tls entries. */
15709 unsigned char *toc_tls;
15711 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15712 &local_syms, rel, input_bfd))
15713 return false;
15715 if (toc_tls)
15716 tls_mask = *toc_tls;
15719 /* Check that tls relocs are used with tls syms, and non-tls
15720 relocs are used with non-tls syms. */
15721 if (r_symndx != STN_UNDEF
15722 && r_type != R_PPC64_NONE
15723 && (h == NULL
15724 || h->elf.root.type == bfd_link_hash_defined
15725 || h->elf.root.type == bfd_link_hash_defweak)
15726 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15728 if ((tls_mask & TLS_TLS) != 0
15729 && (r_type == R_PPC64_TLS
15730 || r_type == R_PPC64_TLSGD
15731 || r_type == R_PPC64_TLSLD))
15732 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15734 else
15735 info->callbacks->einfo
15736 (!IS_PPC64_TLS_RELOC (r_type)
15737 /* xgettext:c-format */
15738 ? _("%H: %s used with TLS symbol `%pT'\n")
15739 /* xgettext:c-format */
15740 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15741 input_bfd, input_section, rel->r_offset,
15742 ppc64_elf_howto_table[r_type]->name,
15743 sym_name);
15746 /* Ensure reloc mapping code below stays sane. */
15747 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15748 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15749 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15750 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15751 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15752 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15753 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15754 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15755 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15756 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15757 abort ();
15759 switch (r_type)
15761 default:
15762 break;
15764 case R_PPC64_LO_DS_OPT:
15765 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15767 insn = bfd_get_32 (input_bfd,
15768 contents + rel->r_offset - d_offset);
15769 if ((insn & (0x3fu << 26)) != 58u << 26)
15770 abort ();
15771 insn += (14u << 26) - (58u << 26);
15772 bfd_put_32 (input_bfd, insn,
15773 contents + rel->r_offset - d_offset);
15774 r_type = R_PPC64_TOC16_LO;
15775 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15777 break;
15779 case R_PPC64_TOC16:
15780 case R_PPC64_TOC16_LO:
15781 case R_PPC64_TOC16_DS:
15782 case R_PPC64_TOC16_LO_DS:
15784 /* Check for toc tls entries. */
15785 unsigned char *toc_tls;
15786 int retval;
15788 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15789 &local_syms, rel, input_bfd);
15790 if (retval == 0)
15791 return false;
15793 if (toc_tls)
15795 tls_mask = *toc_tls;
15796 if (r_type == R_PPC64_TOC16_DS
15797 || r_type == R_PPC64_TOC16_LO_DS)
15799 if ((tls_mask & TLS_TLS) != 0
15800 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15801 goto toctprel;
15803 else
15805 /* If we found a GD reloc pair, then we might be
15806 doing a GD->IE transition. */
15807 if (retval == 2)
15809 tls_gd = TLS_GDIE;
15810 if ((tls_mask & TLS_TLS) != 0
15811 && (tls_mask & TLS_GD) == 0)
15812 goto tls_ldgd_opt;
15814 else if (retval == 3)
15816 if ((tls_mask & TLS_TLS) != 0
15817 && (tls_mask & TLS_LD) == 0)
15818 goto tls_ldgd_opt;
15823 break;
15825 case R_PPC64_GOT_TPREL16_HI:
15826 case R_PPC64_GOT_TPREL16_HA:
15827 if ((tls_mask & TLS_TLS) != 0
15828 && (tls_mask & TLS_TPREL) == 0
15829 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15831 rel->r_offset -= d_offset;
15832 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15833 r_type = R_PPC64_NONE;
15834 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15836 break;
15838 case R_PPC64_GOT_TPREL16_DS:
15839 case R_PPC64_GOT_TPREL16_LO_DS:
15840 if ((tls_mask & TLS_TLS) != 0
15841 && (tls_mask & TLS_TPREL) == 0
15842 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15844 toctprel:
15845 insn = bfd_get_32 (input_bfd,
15846 contents + rel->r_offset - d_offset);
15847 insn &= 31 << 21;
15848 insn |= 0x3c0d0000; /* addis 0,13,0 */
15849 bfd_put_32 (input_bfd, insn,
15850 contents + rel->r_offset - d_offset);
15851 r_type = R_PPC64_TPREL16_HA;
15852 if (toc_symndx != 0)
15854 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15855 rel->r_addend = toc_addend;
15856 /* We changed the symbol. Start over in order to
15857 get h, sym, sec etc. right. */
15858 goto again;
15860 else
15861 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15863 break;
15865 case R_PPC64_GOT_TPREL_PCREL34:
15866 if ((tls_mask & TLS_TLS) != 0
15867 && (tls_mask & TLS_TPREL) == 0
15868 && offset_in_range (input_section, rel->r_offset, 8))
15870 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15871 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15872 pinsn <<= 32;
15873 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15874 pinsn += ((2ULL << 56) + (-1ULL << 52)
15875 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15876 bfd_put_32 (input_bfd, pinsn >> 32,
15877 contents + rel->r_offset);
15878 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15879 contents + rel->r_offset + 4);
15880 r_type = R_PPC64_TPREL34;
15881 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15883 break;
15885 case R_PPC64_TLS:
15886 if ((tls_mask & TLS_TLS) != 0
15887 && (tls_mask & TLS_TPREL) == 0
15888 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15890 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15891 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15892 if (insn == 0)
15893 break;
15894 if ((rel->r_offset & 3) == 0)
15896 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15897 /* Was PPC64_TLS which sits on insn boundary, now
15898 PPC64_TPREL16_LO which is at low-order half-word. */
15899 rel->r_offset += d_offset;
15900 r_type = R_PPC64_TPREL16_LO;
15901 if (toc_symndx != 0)
15903 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15904 rel->r_addend = toc_addend;
15905 /* We changed the symbol. Start over in order to
15906 get h, sym, sec etc. right. */
15907 goto again;
15909 else
15910 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15912 else if ((rel->r_offset & 3) == 1)
15914 /* For pcrel IE to LE we already have the full
15915 offset and thus don't need an addi here. A nop
15916 or mr will do. */
15917 if ((insn & (0x3fu << 26)) == 14 << 26)
15919 /* Extract regs from addi rt,ra,si. */
15920 unsigned int rt = (insn >> 21) & 0x1f;
15921 unsigned int ra = (insn >> 16) & 0x1f;
15922 if (rt == ra)
15923 insn = NOP;
15924 else
15926 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15927 insn = (rt << 16) | (ra << 21) | (ra << 11);
15928 insn |= (31u << 26) | (444u << 1);
15931 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15934 break;
15936 case R_PPC64_GOT_TLSGD16_HI:
15937 case R_PPC64_GOT_TLSGD16_HA:
15938 tls_gd = TLS_GDIE;
15939 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15940 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15941 goto tls_gdld_hi;
15942 break;
15944 case R_PPC64_GOT_TLSLD16_HI:
15945 case R_PPC64_GOT_TLSLD16_HA:
15946 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15947 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15949 tls_gdld_hi:
15950 if ((tls_mask & tls_gd) != 0)
15951 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15952 + R_PPC64_GOT_TPREL16_DS);
15953 else
15955 rel->r_offset -= d_offset;
15956 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15957 r_type = R_PPC64_NONE;
15959 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15961 break;
15963 case R_PPC64_GOT_TLSGD16:
15964 case R_PPC64_GOT_TLSGD16_LO:
15965 tls_gd = TLS_GDIE;
15966 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15967 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15968 goto tls_ldgd_opt;
15969 break;
15971 case R_PPC64_GOT_TLSLD16:
15972 case R_PPC64_GOT_TLSLD16_LO:
15973 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15974 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15976 unsigned int insn1, insn2;
15978 tls_ldgd_opt:
15979 offset = (bfd_vma) -1;
15980 /* If not using the newer R_PPC64_TLSGD/LD to mark
15981 __tls_get_addr calls, we must trust that the call
15982 stays with its arg setup insns, ie. that the next
15983 reloc is the __tls_get_addr call associated with
15984 the current reloc. Edit both insns. */
15985 if (input_section->nomark_tls_get_addr
15986 && rel + 1 < relend
15987 && branch_reloc_hash_match (input_bfd, rel + 1,
15988 htab->tls_get_addr_fd,
15989 htab->tga_desc_fd,
15990 htab->tls_get_addr,
15991 htab->tga_desc))
15992 offset = rel[1].r_offset;
15993 /* We read the low GOT_TLS (or TOC16) insn because we
15994 need to keep the destination reg. It may be
15995 something other than the usual r3, and moved to r3
15996 before the call by intervening code. */
15997 insn1 = bfd_get_32 (input_bfd,
15998 contents + rel->r_offset - d_offset);
15999 if ((tls_mask & tls_gd) != 0)
16001 /* IE */
16002 insn1 &= (0x1f << 21) | (0x1f << 16);
16003 insn1 |= 58u << 26; /* ld */
16004 insn2 = 0x7c636a14; /* add 3,3,13 */
16005 if (offset != (bfd_vma) -1)
16006 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16007 if (r_type == R_PPC64_TOC16
16008 || r_type == R_PPC64_TOC16_LO)
16009 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
16010 else
16011 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
16012 + R_PPC64_GOT_TPREL16_DS);
16013 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16015 else
16017 /* LE */
16018 insn1 &= 0x1f << 21;
16019 insn1 |= 0x3c0d0000; /* addis r,13,0 */
16020 insn2 = 0x38630000; /* addi 3,3,0 */
16021 if (tls_gd == 0)
16023 /* Was an LD reloc. */
16024 r_symndx = STN_UNDEF;
16025 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16027 else if (toc_symndx != 0)
16029 r_symndx = toc_symndx;
16030 rel->r_addend = toc_addend;
16032 r_type = R_PPC64_TPREL16_HA;
16033 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16034 if (offset != (bfd_vma) -1)
16036 rel[1].r_info = ELF64_R_INFO (r_symndx,
16037 R_PPC64_TPREL16_LO);
16038 rel[1].r_offset = offset + d_offset;
16039 rel[1].r_addend = rel->r_addend;
16042 bfd_put_32 (input_bfd, insn1,
16043 contents + rel->r_offset - d_offset);
16044 if (offset != (bfd_vma) -1
16045 && offset_in_range (input_section, offset, 4))
16047 bfd_put_32 (input_bfd, insn2, contents + offset);
16048 if (offset_in_range (input_section, offset + 4, 4))
16050 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16051 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16052 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16055 if ((tls_mask & tls_gd) == 0
16056 && (tls_gd == 0 || toc_symndx != 0))
16058 /* We changed the symbol. Start over in order
16059 to get h, sym, sec etc. right. */
16060 goto again;
16063 break;
16065 case R_PPC64_GOT_TLSGD_PCREL34:
16066 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16067 && offset_in_range (input_section, rel->r_offset, 8))
16069 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16070 pinsn <<= 32;
16071 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16072 if ((tls_mask & TLS_GDIE) != 0)
16074 /* IE, pla -> pld */
16075 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16076 r_type = R_PPC64_GOT_TPREL_PCREL34;
16078 else
16080 /* LE, pla pcrel -> paddi r13 */
16081 pinsn += (-1ULL << 52) + (13ULL << 16);
16082 r_type = R_PPC64_TPREL34;
16084 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16085 bfd_put_32 (input_bfd, pinsn >> 32,
16086 contents + rel->r_offset);
16087 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16088 contents + rel->r_offset + 4);
16090 break;
16092 case R_PPC64_GOT_TLSLD_PCREL34:
16093 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16094 && offset_in_range (input_section, rel->r_offset, 8))
16096 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16097 pinsn <<= 32;
16098 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16099 pinsn += (-1ULL << 52) + (13ULL << 16);
16100 bfd_put_32 (input_bfd, pinsn >> 32,
16101 contents + rel->r_offset);
16102 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16103 contents + rel->r_offset + 4);
16104 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16105 r_symndx = STN_UNDEF;
16106 r_type = R_PPC64_TPREL34;
16107 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16108 goto again;
16110 break;
16112 case R_PPC64_TLSGD:
16113 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16114 && rel + 1 < relend
16115 && offset_in_range (input_section, rel->r_offset,
16116 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16117 ? 8 : 4))
16119 unsigned int insn2;
16120 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16122 offset = rel->r_offset;
16123 if (is_plt_seq_reloc (r_type1))
16125 bfd_put_32 (output_bfd, NOP, contents + offset);
16126 if (r_type1 == R_PPC64_PLT_PCREL34
16127 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16128 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16129 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16130 break;
16133 if (r_type1 == R_PPC64_PLTCALL)
16134 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16136 if ((tls_mask & TLS_GDIE) != 0)
16138 /* IE */
16139 r_type = R_PPC64_NONE;
16140 insn2 = 0x7c636a14; /* add 3,3,13 */
16142 else
16144 /* LE */
16145 if (toc_symndx != 0)
16147 r_symndx = toc_symndx;
16148 rel->r_addend = toc_addend;
16150 if (r_type1 == R_PPC64_REL24_NOTOC
16151 || r_type1 == R_PPC64_REL24_P9NOTOC
16152 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16154 r_type = R_PPC64_NONE;
16155 insn2 = NOP;
16157 else
16159 rel->r_offset = offset + d_offset;
16160 r_type = R_PPC64_TPREL16_LO;
16161 insn2 = 0x38630000; /* addi 3,3,0 */
16164 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16165 /* Zap the reloc on the _tls_get_addr call too. */
16166 BFD_ASSERT (offset == rel[1].r_offset);
16167 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16168 bfd_put_32 (input_bfd, insn2, contents + offset);
16169 if ((tls_mask & TLS_GDIE) == 0
16170 && toc_symndx != 0
16171 && r_type != R_PPC64_NONE)
16172 goto again;
16174 break;
16176 case R_PPC64_TLSLD:
16177 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16178 && rel + 1 < relend
16179 && offset_in_range (input_section, rel->r_offset,
16180 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16181 ? 8 : 4))
16183 unsigned int insn2;
16184 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16186 offset = rel->r_offset;
16187 if (is_plt_seq_reloc (r_type1))
16189 bfd_put_32 (output_bfd, NOP, contents + offset);
16190 if (r_type1 == R_PPC64_PLT_PCREL34
16191 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16192 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16193 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16194 break;
16197 if (r_type1 == R_PPC64_PLTCALL)
16198 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16200 if (r_type1 == R_PPC64_REL24_NOTOC
16201 || r_type1 == R_PPC64_REL24_P9NOTOC
16202 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16204 r_type = R_PPC64_NONE;
16205 insn2 = NOP;
16207 else
16209 rel->r_offset = offset + d_offset;
16210 r_symndx = STN_UNDEF;
16211 r_type = R_PPC64_TPREL16_LO;
16212 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16213 insn2 = 0x38630000; /* addi 3,3,0 */
16215 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16216 /* Zap the reloc on the _tls_get_addr call too. */
16217 BFD_ASSERT (offset == rel[1].r_offset);
16218 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16219 bfd_put_32 (input_bfd, insn2, contents + offset);
16220 if (r_type != R_PPC64_NONE)
16221 goto again;
16223 break;
16225 case R_PPC64_DTPMOD64:
16226 if (rel + 1 < relend
16227 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16228 && rel[1].r_offset == rel->r_offset + 8)
16230 if ((tls_mask & TLS_GD) == 0
16231 && offset_in_range (input_section, rel->r_offset, 8))
16233 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16234 if ((tls_mask & TLS_GDIE) != 0)
16235 r_type = R_PPC64_TPREL64;
16236 else
16238 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16239 r_type = R_PPC64_NONE;
16241 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16244 else
16246 if ((tls_mask & TLS_LD) == 0
16247 && offset_in_range (input_section, rel->r_offset, 8))
16249 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16250 r_type = R_PPC64_NONE;
16251 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16254 break;
16256 case R_PPC64_TPREL64:
16257 if ((tls_mask & TLS_TPREL) == 0)
16259 r_type = R_PPC64_NONE;
16260 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16262 break;
16264 case R_PPC64_ENTRY:
16265 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16266 if (!bfd_link_pic (info)
16267 && !info->traditional_format
16268 && relocation + 0x80008000 <= 0xffffffff
16269 && offset_in_range (input_section, rel->r_offset, 8))
16271 unsigned int insn1, insn2;
16273 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16274 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16275 if ((insn1 & ~0xfffc) == LD_R2_0R12
16276 && insn2 == ADD_R2_R2_R12)
16278 bfd_put_32 (input_bfd,
16279 LIS_R2 + PPC_HA (relocation),
16280 contents + rel->r_offset);
16281 bfd_put_32 (input_bfd,
16282 ADDI_R2_R2 + PPC_LO (relocation),
16283 contents + rel->r_offset + 4);
16286 else
16288 relocation -= (rel->r_offset
16289 + input_section->output_offset
16290 + input_section->output_section->vma);
16291 if (relocation + 0x80008000 <= 0xffffffff
16292 && offset_in_range (input_section, rel->r_offset, 8))
16294 unsigned int insn1, insn2;
16296 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16297 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16298 if ((insn1 & ~0xfffc) == LD_R2_0R12
16299 && insn2 == ADD_R2_R2_R12)
16301 bfd_put_32 (input_bfd,
16302 ADDIS_R2_R12 + PPC_HA (relocation),
16303 contents + rel->r_offset);
16304 bfd_put_32 (input_bfd,
16305 ADDI_R2_R2 + PPC_LO (relocation),
16306 contents + rel->r_offset + 4);
16310 break;
16312 case R_PPC64_REL16_HA:
16313 /* If we are generating a non-PIC executable, edit
16314 . 0: addis 2,12,.TOC.-0b@ha
16315 . addi 2,2,.TOC.-0b@l
16316 used by ELFv2 global entry points to set up r2, to
16317 . lis 2,.TOC.@ha
16318 . addi 2,2,.TOC.@l
16319 if .TOC. is in range. */
16320 if (!bfd_link_pic (info)
16321 && !info->traditional_format
16322 && !htab->opd_abi
16323 && rel->r_addend == d_offset
16324 && h != NULL && &h->elf == htab->elf.hgot
16325 && rel + 1 < relend
16326 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16327 && rel[1].r_offset == rel->r_offset + 4
16328 && rel[1].r_addend == rel->r_addend + 4
16329 && relocation + 0x80008000 <= 0xffffffff
16330 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16332 unsigned int insn1, insn2;
16333 offset = rel->r_offset - d_offset;
16334 insn1 = bfd_get_32 (input_bfd, contents + offset);
16335 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16336 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16337 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16339 r_type = R_PPC64_ADDR16_HA;
16340 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16341 rel->r_addend -= d_offset;
16342 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16343 rel[1].r_addend -= d_offset + 4;
16344 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16347 break;
16350 /* Handle other relocations that tweak non-addend part of insn. */
16351 insn = 0;
16352 max_br_offset = 1 << 25;
16353 addend = rel->r_addend;
16354 reloc_dest = DEST_NORMAL;
16355 switch (r_type)
16357 default:
16358 break;
16360 case R_PPC64_TOCSAVE:
16361 if (relocation + addend == (rel->r_offset
16362 + input_section->output_offset
16363 + input_section->output_section->vma)
16364 && tocsave_find (htab, NO_INSERT,
16365 &local_syms, rel, input_bfd)
16366 && offset_in_range (input_section, rel->r_offset, 4))
16368 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16369 if (insn == NOP
16370 || insn == CROR_151515 || insn == CROR_313131)
16371 bfd_put_32 (input_bfd,
16372 STD_R2_0R1 + STK_TOC (htab),
16373 contents + rel->r_offset);
16375 break;
16377 /* Branch taken prediction relocations. */
16378 case R_PPC64_ADDR14_BRTAKEN:
16379 case R_PPC64_REL14_BRTAKEN:
16380 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16381 /* Fall through. */
16383 /* Branch not taken prediction relocations. */
16384 case R_PPC64_ADDR14_BRNTAKEN:
16385 case R_PPC64_REL14_BRNTAKEN:
16386 if (!offset_in_range (input_section, rel->r_offset, 4))
16387 break;
16388 insn |= bfd_get_32 (input_bfd,
16389 contents + rel->r_offset) & ~(0x01 << 21);
16390 /* Fall through. */
16392 case R_PPC64_REL14:
16393 max_br_offset = 1 << 15;
16394 /* Fall through. */
16396 case R_PPC64_REL24:
16397 case R_PPC64_REL24_NOTOC:
16398 case R_PPC64_REL24_P9NOTOC:
16399 case R_PPC64_PLTCALL:
16400 case R_PPC64_PLTCALL_NOTOC:
16401 /* Calls to functions with a different TOC, such as calls to
16402 shared objects, need to alter the TOC pointer. This is
16403 done using a linkage stub. A REL24 branching to these
16404 linkage stubs needs to be followed by a nop, as the nop
16405 will be replaced with an instruction to restore the TOC
16406 base pointer. */
16407 fdh = h;
16408 if (h != NULL
16409 && h->oh != NULL
16410 && h->oh->is_func_descriptor)
16411 fdh = ppc_follow_link (h->oh);
16412 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16413 htab);
16414 if ((r_type == R_PPC64_PLTCALL
16415 || r_type == R_PPC64_PLTCALL_NOTOC)
16416 && stub_entry != NULL
16417 && stub_entry->type.main == ppc_stub_plt_call)
16418 stub_entry = NULL;
16420 if (stub_entry != NULL
16421 && (stub_entry->type.main == ppc_stub_plt_call
16422 || stub_entry->type.r2save))
16424 bool can_plt_call = false;
16426 if (r_type == R_PPC64_REL24_NOTOC
16427 || r_type == R_PPC64_REL24_P9NOTOC)
16429 /* NOTOC calls don't need to restore r2. */
16430 can_plt_call = true;
16432 else if (stub_entry->type.main == ppc_stub_plt_call
16433 && !htab->opd_abi
16434 && htab->params->plt_localentry0 != 0
16435 && h != NULL
16436 && is_elfv2_localentry0 (&h->elf))
16438 /* The function doesn't use or change r2. */
16439 can_plt_call = true;
16442 /* All of these stubs may modify r2, so there must be a
16443 branch and link followed by a nop. The nop is
16444 replaced by an insn to restore r2. */
16445 else if (offset_in_range (input_section, rel->r_offset, 8))
16447 unsigned long br;
16449 br = bfd_get_32 (input_bfd,
16450 contents + rel->r_offset);
16451 if ((br & 1) != 0)
16453 unsigned long nop;
16455 nop = bfd_get_32 (input_bfd,
16456 contents + rel->r_offset + 4);
16457 if (nop == LD_R2_0R1 + STK_TOC (htab))
16458 can_plt_call = true;
16459 else if (nop == NOP
16460 || nop == CROR_151515
16461 || nop == CROR_313131)
16463 if (h != NULL
16464 && is_tls_get_addr (&h->elf, htab)
16465 && htab->params->tls_get_addr_opt)
16467 /* Special stub used, leave nop alone. */
16469 else
16470 bfd_put_32 (input_bfd,
16471 LD_R2_0R1 + STK_TOC (htab),
16472 contents + rel->r_offset + 4);
16473 can_plt_call = true;
16478 if (!can_plt_call && h != NULL)
16480 const char *name = h->elf.root.root.string;
16482 if (*name == '.')
16483 ++name;
16485 if (startswith (name, "__libc_start_main")
16486 && (name[17] == 0 || name[17] == '@'))
16488 /* Allow crt1 branch to go via a toc adjusting
16489 stub. Other calls that never return could do
16490 the same, if we could detect such. */
16491 can_plt_call = true;
16495 if (!can_plt_call)
16497 /* g++ as of 20130507 emits self-calls without a
16498 following nop. This is arguably wrong since we
16499 have conflicting information. On the one hand a
16500 global symbol and on the other a local call
16501 sequence, but don't error for this special case.
16502 It isn't possible to cheaply verify we have
16503 exactly such a call. Allow all calls to the same
16504 section. */
16505 asection *code_sec = sec;
16507 if (get_opd_info (sec) != NULL)
16509 bfd_vma off = (relocation + addend
16510 - sec->output_section->vma
16511 - sec->output_offset);
16513 opd_entry_value (sec, off, &code_sec, NULL, false);
16515 if (code_sec == input_section)
16516 can_plt_call = true;
16519 if (!can_plt_call)
16521 if (stub_entry->type.main == ppc_stub_plt_call)
16522 info->callbacks->einfo
16523 /* xgettext:c-format */
16524 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16525 "(plt call stub)\n"),
16526 input_bfd, input_section, rel->r_offset, sym_name);
16527 else
16528 info->callbacks->einfo
16529 /* xgettext:c-format */
16530 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16531 "(toc save/adjust stub)\n"),
16532 input_bfd, input_section, rel->r_offset, sym_name);
16534 bfd_set_error (bfd_error_bad_value);
16535 ret = false;
16538 if (can_plt_call
16539 && stub_entry->type.main == ppc_stub_plt_call)
16540 unresolved_reloc = false;
16543 if ((stub_entry == NULL
16544 || stub_entry->type.main == ppc_stub_long_branch
16545 || stub_entry->type.main == ppc_stub_plt_branch)
16546 && get_opd_info (sec) != NULL)
16548 /* The branch destination is the value of the opd entry. */
16549 bfd_vma off = (relocation + addend
16550 - sec->output_section->vma
16551 - sec->output_offset);
16552 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16553 if (dest != (bfd_vma) -1)
16555 relocation = dest;
16556 addend = 0;
16557 reloc_dest = DEST_OPD;
16561 /* If the branch is out of reach we ought to have a long
16562 branch stub. */
16563 from = (rel->r_offset
16564 + input_section->output_offset
16565 + input_section->output_section->vma);
16567 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16568 ? fdh->elf.other
16569 : sym->st_other);
16571 if (stub_entry != NULL
16572 && (stub_entry->type.main == ppc_stub_long_branch
16573 || stub_entry->type.main == ppc_stub_plt_branch))
16575 if (stub_entry->type.sub == ppc_stub_toc
16576 && !stub_entry->type.r2save
16577 && (r_type == R_PPC64_ADDR14_BRTAKEN
16578 || r_type == R_PPC64_ADDR14_BRNTAKEN
16579 || (relocation + addend - from + max_br_offset
16580 < 2 * max_br_offset)))
16581 /* Don't use the stub if this branch is in range. */
16582 stub_entry = NULL;
16584 if (stub_entry != NULL
16585 && stub_entry->type.sub >= ppc_stub_notoc
16586 && ((r_type != R_PPC64_REL24_NOTOC
16587 && r_type != R_PPC64_REL24_P9NOTOC)
16588 || ((fdh ? fdh->elf.other : sym->st_other)
16589 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16590 && (relocation + addend - from + max_br_offset
16591 < 2 * max_br_offset))
16592 stub_entry = NULL;
16594 if (stub_entry != NULL
16595 && stub_entry->type.r2save
16596 && (r_type == R_PPC64_REL24_NOTOC
16597 || r_type == R_PPC64_REL24_P9NOTOC)
16598 && (relocation + addend - from + max_br_offset
16599 < 2 * max_br_offset))
16600 stub_entry = NULL;
16603 if (stub_entry != NULL)
16605 /* Munge up the value and addend so that we call the stub
16606 rather than the procedure directly. */
16607 asection *stub_sec = stub_entry->group->stub_sec;
16609 if (stub_entry->type.main == ppc_stub_save_res)
16610 relocation += (stub_sec->output_offset
16611 + stub_sec->output_section->vma
16612 + stub_sec->size - htab->sfpr->size
16613 - htab->sfpr->output_offset
16614 - htab->sfpr->output_section->vma);
16615 else
16616 relocation = (stub_entry->stub_offset
16617 + stub_sec->output_offset
16618 + stub_sec->output_section->vma);
16619 addend = 0;
16620 reloc_dest = DEST_STUB;
16622 if (((stub_entry->type.r2save
16623 && (r_type == R_PPC64_REL24_NOTOC
16624 || r_type == R_PPC64_REL24_P9NOTOC))
16625 || ((stub_entry->type.main == ppc_stub_plt_call
16626 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16627 && rel + 1 < relend
16628 && rel[1].r_offset == rel->r_offset + 4
16629 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16630 && !(stub_entry->type.main == ppc_stub_plt_call
16631 && htab->params->tls_get_addr_opt
16632 && h != NULL
16633 && is_tls_get_addr (&h->elf, htab)))
16635 /* Skip over the r2 store at the start of the stub. */
16636 relocation += 4;
16639 if ((r_type == R_PPC64_REL24_NOTOC
16640 || r_type == R_PPC64_REL24_P9NOTOC)
16641 && stub_entry->type.main == ppc_stub_plt_call
16642 && stub_entry->type.sub >= ppc_stub_notoc)
16643 htab->notoc_plt = 1;
16646 if (insn != 0)
16648 if (is_isa_v2)
16650 /* Set 'a' bit. This is 0b00010 in BO field for branch
16651 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16652 for branch on CTR insns (BO == 1a00t or 1a01t). */
16653 if ((insn & (0x14 << 21)) == (0x04 << 21))
16654 insn |= 0x02 << 21;
16655 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16656 insn |= 0x08 << 21;
16657 else
16658 break;
16660 else
16662 /* Invert 'y' bit if not the default. */
16663 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16664 insn ^= 0x01 << 21;
16667 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16670 /* NOP out calls to undefined weak functions.
16671 We can thus call a weak function without first
16672 checking whether the function is defined. */
16673 else if (h != NULL
16674 && h->elf.root.type == bfd_link_hash_undefweak
16675 && h->elf.dynindx == -1
16676 && (r_type == R_PPC64_REL24
16677 || r_type == R_PPC64_REL24_NOTOC
16678 || r_type == R_PPC64_REL24_P9NOTOC)
16679 && relocation == 0
16680 && addend == 0
16681 && offset_in_range (input_section, rel->r_offset, 4))
16683 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16684 goto copy_reloc;
16686 break;
16688 case R_PPC64_GOT16_DS:
16689 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16690 || (bfd_link_pic (info)
16691 && sec == bfd_abs_section_ptr)
16692 || !htab->do_toc_opt)
16693 break;
16694 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16695 if (relocation + addend - from + 0x8000 < 0x10000
16696 && sec != NULL
16697 && sec->output_section != NULL
16698 && !discarded_section (sec)
16699 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16700 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16702 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16703 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16705 insn += (14u << 26) - (58u << 26);
16706 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16707 r_type = R_PPC64_TOC16;
16708 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16711 break;
16713 case R_PPC64_GOT16_LO_DS:
16714 case R_PPC64_GOT16_HA:
16715 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16716 || (bfd_link_pic (info)
16717 && sec == bfd_abs_section_ptr)
16718 || !htab->do_toc_opt)
16719 break;
16720 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16721 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16722 && sec != NULL
16723 && sec->output_section != NULL
16724 && !discarded_section (sec)
16725 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16726 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16728 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16729 if (r_type == R_PPC64_GOT16_LO_DS
16730 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16732 insn += (14u << 26) - (58u << 26);
16733 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16734 r_type = R_PPC64_TOC16_LO;
16735 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16737 else if (r_type == R_PPC64_GOT16_HA
16738 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16740 r_type = R_PPC64_TOC16_HA;
16741 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16744 break;
16746 case R_PPC64_GOT_PCREL34:
16747 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16748 || (bfd_link_pic (info)
16749 && sec == bfd_abs_section_ptr)
16750 || !htab->do_toc_opt)
16751 break;
16752 from = (rel->r_offset
16753 + input_section->output_section->vma
16754 + input_section->output_offset);
16755 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16756 && sec != NULL
16757 && sec->output_section != NULL
16758 && !discarded_section (sec)
16759 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16760 && offset_in_range (input_section, rel->r_offset, 8)))
16761 break;
16763 offset = rel->r_offset;
16764 pinsn = bfd_get_32 (input_bfd, contents + offset);
16765 pinsn <<= 32;
16766 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16767 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16768 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16769 break;
16771 /* Replace with paddi. */
16772 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16773 r_type = R_PPC64_PCREL34;
16774 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16775 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16776 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16777 /* Fall through. */
16779 case R_PPC64_PCREL34:
16780 if (!htab->params->no_pcrel_opt
16781 && rel + 1 < relend
16782 && rel[1].r_offset == rel->r_offset
16783 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16784 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16785 && offset_in_range (input_section, rel->r_offset, 8))
16787 offset = rel->r_offset;
16788 pinsn = bfd_get_32 (input_bfd, contents + offset);
16789 pinsn <<= 32;
16790 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16791 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16792 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16793 | (14ULL << 26) /* paddi */))
16795 bfd_vma off2 = rel[1].r_addend;
16796 if (off2 == 0)
16797 /* zero means next insn. */
16798 off2 = 8;
16799 off2 += offset;
16800 if (offset_in_range (input_section, off2, 4))
16802 uint64_t pinsn2;
16803 bfd_signed_vma addend_off;
16804 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16805 pinsn2 <<= 32;
16806 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16808 if (!offset_in_range (input_section, off2, 8))
16809 break;
16810 pinsn2 |= bfd_get_32 (input_bfd,
16811 contents + off2 + 4);
16813 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16815 addend += addend_off;
16816 rel->r_addend = addend;
16817 bfd_put_32 (input_bfd, pinsn >> 32,
16818 contents + offset);
16819 bfd_put_32 (input_bfd, pinsn,
16820 contents + offset + 4);
16821 bfd_put_32 (input_bfd, pinsn2 >> 32,
16822 contents + off2);
16823 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16824 bfd_put_32 (input_bfd, pinsn2,
16825 contents + off2 + 4);
16830 break;
16833 tls_type = 0;
16834 save_unresolved_reloc = unresolved_reloc;
16835 switch (r_type)
16837 default:
16838 /* xgettext:c-format */
16839 _bfd_error_handler (_("%pB: %s unsupported"),
16840 input_bfd, ppc64_elf_howto_table[r_type]->name);
16842 bfd_set_error (bfd_error_bad_value);
16843 ret = false;
16844 goto copy_reloc;
16846 case R_PPC64_NONE:
16847 case R_PPC64_TLS:
16848 case R_PPC64_TLSGD:
16849 case R_PPC64_TLSLD:
16850 case R_PPC64_TOCSAVE:
16851 case R_PPC64_GNU_VTINHERIT:
16852 case R_PPC64_GNU_VTENTRY:
16853 case R_PPC64_ENTRY:
16854 case R_PPC64_PCREL_OPT:
16855 goto copy_reloc;
16857 /* GOT16 relocations. Like an ADDR16 using the symbol's
16858 address in the GOT as relocation value instead of the
16859 symbol's value itself. Also, create a GOT entry for the
16860 symbol and put the symbol value there. */
16861 case R_PPC64_GOT_TLSGD16:
16862 case R_PPC64_GOT_TLSGD16_LO:
16863 case R_PPC64_GOT_TLSGD16_HI:
16864 case R_PPC64_GOT_TLSGD16_HA:
16865 case R_PPC64_GOT_TLSGD_PCREL34:
16866 tls_type = TLS_TLS | TLS_GD;
16867 goto dogot;
16869 case R_PPC64_GOT_TLSLD16:
16870 case R_PPC64_GOT_TLSLD16_LO:
16871 case R_PPC64_GOT_TLSLD16_HI:
16872 case R_PPC64_GOT_TLSLD16_HA:
16873 case R_PPC64_GOT_TLSLD_PCREL34:
16874 tls_type = TLS_TLS | TLS_LD;
16875 goto dogot;
16877 case R_PPC64_GOT_TPREL16_DS:
16878 case R_PPC64_GOT_TPREL16_LO_DS:
16879 case R_PPC64_GOT_TPREL16_HI:
16880 case R_PPC64_GOT_TPREL16_HA:
16881 case R_PPC64_GOT_TPREL_PCREL34:
16882 tls_type = TLS_TLS | TLS_TPREL;
16883 goto dogot;
16885 case R_PPC64_GOT_DTPREL16_DS:
16886 case R_PPC64_GOT_DTPREL16_LO_DS:
16887 case R_PPC64_GOT_DTPREL16_HI:
16888 case R_PPC64_GOT_DTPREL16_HA:
16889 case R_PPC64_GOT_DTPREL_PCREL34:
16890 tls_type = TLS_TLS | TLS_DTPREL;
16891 goto dogot;
16893 case R_PPC64_GOT16:
16894 case R_PPC64_GOT16_LO:
16895 case R_PPC64_GOT16_HI:
16896 case R_PPC64_GOT16_HA:
16897 case R_PPC64_GOT16_DS:
16898 case R_PPC64_GOT16_LO_DS:
16899 case R_PPC64_GOT_PCREL34:
16900 dogot:
16902 /* Relocation is to the entry for this symbol in the global
16903 offset table. */
16904 asection *got;
16905 bfd_vma *offp;
16906 bfd_vma off;
16907 unsigned long indx = 0;
16908 struct got_entry *ent;
16910 if (tls_type == (TLS_TLS | TLS_LD)
16911 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16912 ent = ppc64_tlsld_got (input_bfd);
16913 else
16915 if (h != NULL)
16917 if (!htab->elf.dynamic_sections_created
16918 || h->elf.dynindx == -1
16919 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16920 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16921 /* This is actually a static link, or it is a
16922 -Bsymbolic link and the symbol is defined
16923 locally, or the symbol was forced to be local
16924 because of a version file. */
16926 else
16928 indx = h->elf.dynindx;
16929 unresolved_reloc = false;
16931 ent = h->elf.got.glist;
16933 else
16935 if (local_got_ents == NULL)
16936 abort ();
16937 ent = local_got_ents[r_symndx];
16940 for (; ent != NULL; ent = ent->next)
16941 if (ent->addend == orig_rel.r_addend
16942 && ent->owner == input_bfd
16943 && ent->tls_type == tls_type)
16944 break;
16947 if (ent == NULL)
16948 abort ();
16949 if (ent->is_indirect)
16950 ent = ent->got.ent;
16951 offp = &ent->got.offset;
16952 got = ppc64_elf_tdata (ent->owner)->got;
16953 if (got == NULL)
16954 abort ();
16956 /* The offset must always be a multiple of 8. We use the
16957 least significant bit to record whether we have already
16958 processed this entry. */
16959 off = *offp;
16960 if ((off & 1) != 0)
16961 off &= ~1;
16962 else
16964 /* Generate relocs for the dynamic linker, except in
16965 the case of TLSLD where we'll use one entry per
16966 module. */
16967 asection *relgot;
16968 bool ifunc;
16970 *offp = off | 1;
16971 relgot = NULL;
16972 ifunc = (h != NULL
16973 ? h->elf.type == STT_GNU_IFUNC
16974 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16975 if (ifunc)
16977 relgot = htab->elf.irelplt;
16978 if (indx == 0 || is_static_defined (&h->elf))
16979 htab->elf.ifunc_resolvers = true;
16981 else if (indx != 0
16982 || (bfd_link_pic (info)
16983 && (h == NULL
16984 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16985 && !(tls_type != 0
16986 && bfd_link_executable (info)
16987 && (h == NULL
16988 || SYMBOL_REFERENCES_LOCAL (info,
16989 &h->elf)))
16990 && (h != NULL
16991 ? !bfd_is_abs_symbol (&h->elf.root)
16992 : sym->st_shndx != SHN_ABS)))
16994 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16995 if (relgot != NULL)
16997 outrel.r_offset = (got->output_section->vma
16998 + got->output_offset
16999 + off);
17000 outrel.r_addend = orig_rel.r_addend;
17001 if (tls_type & (TLS_LD | TLS_GD))
17003 outrel.r_addend = 0;
17004 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
17005 if (tls_type == (TLS_TLS | TLS_GD))
17007 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17008 &outrel,
17009 relgot));
17010 outrel.r_offset += 8;
17011 outrel.r_addend = orig_rel.r_addend;
17012 outrel.r_info
17013 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17016 else if (tls_type == (TLS_TLS | TLS_DTPREL))
17017 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17018 else if (tls_type == (TLS_TLS | TLS_TPREL))
17019 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
17020 else if (indx != 0)
17021 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17022 else
17024 if (ifunc)
17025 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17026 else
17027 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17029 /* Write the .got section contents for the sake
17030 of prelink. */
17031 loc = got->contents + off;
17032 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17033 loc);
17036 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
17038 outrel.r_addend += relocation;
17039 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
17041 if (htab->elf.tls_sec == NULL)
17042 outrel.r_addend = 0;
17043 else
17044 outrel.r_addend -= htab->elf.tls_sec->vma;
17047 if (!(info->enable_dt_relr
17048 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17049 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17050 &outrel, relgot));
17053 /* Init the .got section contents here if we're not
17054 emitting a reloc. */
17055 else
17057 relocation += orig_rel.r_addend;
17058 if (tls_type != 0)
17060 if (htab->elf.tls_sec == NULL)
17061 relocation = 0;
17062 else
17064 if (tls_type & TLS_LD)
17065 relocation = 0;
17066 else
17067 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17068 if (tls_type & TLS_TPREL)
17069 relocation += DTP_OFFSET - TP_OFFSET;
17072 if (tls_type & (TLS_GD | TLS_LD))
17074 bfd_put_64 (output_bfd, relocation,
17075 got->contents + off + 8);
17076 relocation = 1;
17079 bfd_put_64 (output_bfd, relocation,
17080 got->contents + off);
17084 if (off >= (bfd_vma) -2)
17085 abort ();
17087 relocation = got->output_section->vma + got->output_offset + off;
17088 addend = 0;
17089 if (!(r_type == R_PPC64_GOT_PCREL34
17090 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17091 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17092 || r_type == R_PPC64_GOT_TPREL_PCREL34
17093 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17094 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17096 break;
17098 case R_PPC64_PLT16_HA:
17099 case R_PPC64_PLT16_HI:
17100 case R_PPC64_PLT16_LO:
17101 case R_PPC64_PLT16_LO_DS:
17102 case R_PPC64_PLT_PCREL34:
17103 case R_PPC64_PLT_PCREL34_NOTOC:
17104 case R_PPC64_PLT32:
17105 case R_PPC64_PLT64:
17106 case R_PPC64_PLTSEQ:
17107 case R_PPC64_PLTSEQ_NOTOC:
17108 case R_PPC64_PLTCALL:
17109 case R_PPC64_PLTCALL_NOTOC:
17110 /* Relocation is to the entry for this symbol in the
17111 procedure linkage table. */
17112 unresolved_reloc = true;
17114 struct plt_entry **plt_list = NULL;
17115 if (h != NULL)
17116 plt_list = &h->elf.plt.plist;
17117 else if (local_got_ents != NULL)
17119 struct plt_entry **local_plt = (struct plt_entry **)
17120 (local_got_ents + symtab_hdr->sh_info);
17121 plt_list = local_plt + r_symndx;
17123 if (plt_list)
17125 struct plt_entry *ent;
17127 for (ent = *plt_list; ent != NULL; ent = ent->next)
17128 if (ent->plt.offset != (bfd_vma) -1
17129 && ent->addend == orig_rel.r_addend)
17131 asection *plt;
17132 bfd_vma got;
17134 plt = htab->elf.splt;
17135 if (use_local_plt (info, elf_hash_entry (h)))
17137 if (h != NULL
17138 ? h->elf.type == STT_GNU_IFUNC
17139 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17140 plt = htab->elf.iplt;
17141 else
17142 plt = htab->pltlocal;
17144 relocation = (plt->output_section->vma
17145 + plt->output_offset
17146 + ent->plt.offset);
17147 if (r_type == R_PPC64_PLT16_HA
17148 || r_type == R_PPC64_PLT16_HI
17149 || r_type == R_PPC64_PLT16_LO
17150 || r_type == R_PPC64_PLT16_LO_DS)
17152 got = (elf_gp (output_bfd)
17153 + htab->sec_info[input_section->id].toc_off);
17154 relocation -= got;
17156 addend = 0;
17157 unresolved_reloc = false;
17158 break;
17162 break;
17164 case R_PPC64_TOC:
17165 /* Relocation value is TOC base. */
17166 relocation = TOCstart;
17167 if (r_symndx == STN_UNDEF)
17168 relocation += htab->sec_info[input_section->id].toc_off;
17169 else if (unresolved_reloc)
17171 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17172 relocation += htab->sec_info[sec->id].toc_off;
17173 else
17174 unresolved_reloc = true;
17175 if (unresolved_reloc
17176 || (!is_opd
17177 && h != NULL
17178 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17179 info->callbacks->einfo
17180 /* xgettext:c-format */
17181 (_("%H: %s against %pT is not supported\n"),
17182 input_bfd, input_section, rel->r_offset,
17183 ppc64_elf_howto_table[r_type]->name, sym_name);
17184 goto dodyn;
17186 /* TOC16 relocs. We want the offset relative to the TOC base,
17187 which is the address of the start of the TOC plus 0x8000.
17188 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17189 in this order. */
17190 case R_PPC64_TOC16:
17191 case R_PPC64_TOC16_LO:
17192 case R_PPC64_TOC16_HI:
17193 case R_PPC64_TOC16_DS:
17194 case R_PPC64_TOC16_LO_DS:
17195 case R_PPC64_TOC16_HA:
17196 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17197 if (h != NULL)
17198 goto dodyn;
17199 break;
17201 /* Relocate against the beginning of the section. */
17202 case R_PPC64_SECTOFF:
17203 case R_PPC64_SECTOFF_LO:
17204 case R_PPC64_SECTOFF_HI:
17205 case R_PPC64_SECTOFF_DS:
17206 case R_PPC64_SECTOFF_LO_DS:
17207 case R_PPC64_SECTOFF_HA:
17208 if (sec != NULL)
17209 addend -= sec->output_section->vma;
17210 break;
17212 case R_PPC64_REL16:
17213 case R_PPC64_REL16_LO:
17214 case R_PPC64_REL16_HI:
17215 case R_PPC64_REL16_HA:
17216 case R_PPC64_REL16_HIGH:
17217 case R_PPC64_REL16_HIGHA:
17218 case R_PPC64_REL16_HIGHER:
17219 case R_PPC64_REL16_HIGHERA:
17220 case R_PPC64_REL16_HIGHEST:
17221 case R_PPC64_REL16_HIGHESTA:
17222 case R_PPC64_REL16_HIGHER34:
17223 case R_PPC64_REL16_HIGHERA34:
17224 case R_PPC64_REL16_HIGHEST34:
17225 case R_PPC64_REL16_HIGHESTA34:
17226 case R_PPC64_REL16DX_HA:
17227 case R_PPC64_REL14:
17228 case R_PPC64_REL14_BRNTAKEN:
17229 case R_PPC64_REL14_BRTAKEN:
17230 case R_PPC64_REL24:
17231 case R_PPC64_REL24_NOTOC:
17232 case R_PPC64_REL24_P9NOTOC:
17233 case R_PPC64_PCREL34:
17234 case R_PPC64_PCREL28:
17235 break;
17237 case R_PPC64_TPREL16:
17238 case R_PPC64_TPREL16_LO:
17239 case R_PPC64_TPREL16_HI:
17240 case R_PPC64_TPREL16_HA:
17241 case R_PPC64_TPREL16_DS:
17242 case R_PPC64_TPREL16_LO_DS:
17243 case R_PPC64_TPREL16_HIGH:
17244 case R_PPC64_TPREL16_HIGHA:
17245 case R_PPC64_TPREL16_HIGHER:
17246 case R_PPC64_TPREL16_HIGHERA:
17247 case R_PPC64_TPREL16_HIGHEST:
17248 case R_PPC64_TPREL16_HIGHESTA:
17249 if (h != NULL
17250 && h->elf.root.type == bfd_link_hash_undefweak
17251 && h->elf.dynindx == -1
17252 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17254 /* Make this relocation against an undefined weak symbol
17255 resolve to zero. This is really just a tweak, since
17256 code using weak externs ought to check that they are
17257 defined before using them. */
17258 bfd_byte *p = contents + rel->r_offset - d_offset;
17260 insn = bfd_get_32 (input_bfd, p);
17261 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17262 if (insn != 0)
17263 bfd_put_32 (input_bfd, insn, p);
17264 break;
17266 /* Fall through. */
17268 case R_PPC64_TPREL34:
17269 if (htab->elf.tls_sec != NULL)
17270 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17271 /* The TPREL16 relocs shouldn't really be used in shared
17272 libs or with non-local symbols as that will result in
17273 DT_TEXTREL being set, but support them anyway. */
17274 goto dodyn;
17276 case R_PPC64_DTPREL16:
17277 case R_PPC64_DTPREL16_LO:
17278 case R_PPC64_DTPREL16_HI:
17279 case R_PPC64_DTPREL16_HA:
17280 case R_PPC64_DTPREL16_DS:
17281 case R_PPC64_DTPREL16_LO_DS:
17282 case R_PPC64_DTPREL16_HIGH:
17283 case R_PPC64_DTPREL16_HIGHA:
17284 case R_PPC64_DTPREL16_HIGHER:
17285 case R_PPC64_DTPREL16_HIGHERA:
17286 case R_PPC64_DTPREL16_HIGHEST:
17287 case R_PPC64_DTPREL16_HIGHESTA:
17288 case R_PPC64_DTPREL34:
17289 if (htab->elf.tls_sec != NULL)
17290 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17291 break;
17293 case R_PPC64_ADDR64_LOCAL:
17294 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17295 ? h->elf.other
17296 : sym->st_other);
17297 break;
17299 case R_PPC64_DTPMOD64:
17300 relocation = 1;
17301 addend = 0;
17302 goto dodyn;
17304 case R_PPC64_TPREL64:
17305 if (htab->elf.tls_sec != NULL)
17306 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17307 goto dodyn;
17309 case R_PPC64_DTPREL64:
17310 if (htab->elf.tls_sec != NULL)
17311 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17312 /* Fall through. */
17314 /* Relocations that may need to be propagated if this is a
17315 dynamic object. */
17316 case R_PPC64_REL30:
17317 case R_PPC64_REL32:
17318 case R_PPC64_REL64:
17319 case R_PPC64_ADDR14:
17320 case R_PPC64_ADDR14_BRNTAKEN:
17321 case R_PPC64_ADDR14_BRTAKEN:
17322 case R_PPC64_ADDR16:
17323 case R_PPC64_ADDR16_DS:
17324 case R_PPC64_ADDR16_HA:
17325 case R_PPC64_ADDR16_HI:
17326 case R_PPC64_ADDR16_HIGH:
17327 case R_PPC64_ADDR16_HIGHA:
17328 case R_PPC64_ADDR16_HIGHER:
17329 case R_PPC64_ADDR16_HIGHERA:
17330 case R_PPC64_ADDR16_HIGHEST:
17331 case R_PPC64_ADDR16_HIGHESTA:
17332 case R_PPC64_ADDR16_LO:
17333 case R_PPC64_ADDR16_LO_DS:
17334 case R_PPC64_ADDR16_HIGHER34:
17335 case R_PPC64_ADDR16_HIGHERA34:
17336 case R_PPC64_ADDR16_HIGHEST34:
17337 case R_PPC64_ADDR16_HIGHESTA34:
17338 case R_PPC64_ADDR24:
17339 case R_PPC64_ADDR32:
17340 case R_PPC64_ADDR64:
17341 case R_PPC64_UADDR16:
17342 case R_PPC64_UADDR32:
17343 case R_PPC64_UADDR64:
17344 case R_PPC64_D34:
17345 case R_PPC64_D34_LO:
17346 case R_PPC64_D34_HI30:
17347 case R_PPC64_D34_HA30:
17348 case R_PPC64_D28:
17349 dodyn:
17350 if ((input_section->flags & SEC_ALLOC) == 0)
17351 break;
17353 if (NO_OPD_RELOCS && is_opd)
17354 break;
17356 if (bfd_link_pic (info)
17357 ? ((h == NULL
17358 || h->elf.dyn_relocs != NULL)
17359 && ((h != NULL && pc_dynrelocs (h))
17360 || must_be_dyn_reloc (info, r_type)))
17361 : (h != NULL
17362 ? h->elf.dyn_relocs != NULL
17363 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17365 bool skip, relocate;
17366 asection *sreloc;
17367 bfd_vma out_off;
17368 long indx = 0;
17370 /* When generating a dynamic object, these relocations
17371 are copied into the output file to be resolved at run
17372 time. */
17374 skip = false;
17375 relocate = false;
17377 out_off = _bfd_elf_section_offset (output_bfd, info,
17378 input_section, rel->r_offset);
17379 if (out_off == (bfd_vma) -1)
17380 skip = true;
17381 else if (out_off == (bfd_vma) -2)
17382 skip = true, relocate = true;
17383 out_off += (input_section->output_section->vma
17384 + input_section->output_offset);
17385 outrel.r_offset = out_off;
17386 outrel.r_addend = rel->r_addend;
17388 /* Optimize unaligned reloc use. */
17389 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17390 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17391 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17392 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17393 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17394 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17395 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17396 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17397 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17399 if (skip)
17400 memset (&outrel, 0, sizeof outrel);
17401 else if (h != NULL
17402 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17403 && !is_opd
17404 && r_type != R_PPC64_TOC)
17406 indx = h->elf.dynindx;
17407 BFD_ASSERT (indx != -1);
17408 outrel.r_info = ELF64_R_INFO (indx, r_type);
17410 else
17412 /* This symbol is local, or marked to become local,
17413 or this is an opd section reloc which must point
17414 at a local function. */
17415 outrel.r_addend += relocation;
17416 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17418 if (is_opd && h != NULL)
17420 /* Lie about opd entries. This case occurs
17421 when building shared libraries and we
17422 reference a function in another shared
17423 lib. The same thing happens for a weak
17424 definition in an application that's
17425 overridden by a strong definition in a
17426 shared lib. (I believe this is a generic
17427 bug in binutils handling of weak syms.)
17428 In these cases we won't use the opd
17429 entry in this lib. */
17430 unresolved_reloc = false;
17432 if (!is_opd
17433 && r_type == R_PPC64_ADDR64
17434 && (h != NULL
17435 ? h->elf.type == STT_GNU_IFUNC
17436 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17437 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17438 else
17440 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17442 /* We need to relocate .opd contents for ld.so.
17443 Prelink also wants simple and consistent rules
17444 for relocs. This make all RELATIVE relocs have
17445 *r_offset equal to r_addend. */
17446 relocate = true;
17449 else
17451 if (h != NULL
17452 ? h->elf.type == STT_GNU_IFUNC
17453 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17455 info->callbacks->einfo
17456 /* xgettext:c-format */
17457 (_("%H: %s for indirect "
17458 "function `%pT' unsupported\n"),
17459 input_bfd, input_section, rel->r_offset,
17460 ppc64_elf_howto_table[r_type]->name,
17461 sym_name);
17462 ret = false;
17464 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17466 else if (sec == NULL || sec->owner == NULL)
17468 bfd_set_error (bfd_error_bad_value);
17469 return false;
17471 else
17473 asection *osec = sec->output_section;
17475 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17477 /* TLS symbol values are relative to the
17478 TLS segment. Dynamic relocations for
17479 local TLS symbols therefore can't be
17480 reduced to a relocation against their
17481 section symbol because it holds the
17482 address of the section, not a value
17483 relative to the TLS segment. We could
17484 change the .tdata dynamic section symbol
17485 to be zero value but STN_UNDEF works
17486 and is used elsewhere, eg. for TPREL64
17487 GOT relocs against local TLS symbols. */
17488 osec = htab->elf.tls_sec;
17489 indx = 0;
17491 else
17493 indx = elf_section_data (osec)->dynindx;
17494 if (indx == 0)
17496 if ((osec->flags & SEC_READONLY) == 0
17497 && htab->elf.data_index_section != NULL)
17498 osec = htab->elf.data_index_section;
17499 else
17500 osec = htab->elf.text_index_section;
17501 indx = elf_section_data (osec)->dynindx;
17503 BFD_ASSERT (indx != 0);
17506 /* We are turning this relocation into one
17507 against a section symbol, so subtract out
17508 the output section's address but not the
17509 offset of the input section in the output
17510 section. */
17511 outrel.r_addend -= osec->vma;
17514 outrel.r_info = ELF64_R_INFO (indx, r_type);
17518 if (!(info->enable_dt_relr
17519 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17520 && rel->r_offset % 2 == 0
17521 && input_section->alignment_power != 0
17522 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
17524 sreloc = elf_section_data (input_section)->sreloc;
17525 if (h != NULL
17526 ? h->elf.type == STT_GNU_IFUNC
17527 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17529 sreloc = htab->elf.irelplt;
17530 if (indx == 0 || is_static_defined (&h->elf))
17531 htab->elf.ifunc_resolvers = true;
17533 if (sreloc == NULL)
17534 abort ();
17536 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &outrel,
17537 sreloc));
17540 if (!warned_dynamic
17541 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17543 info->callbacks->einfo
17544 /* xgettext:c-format */
17545 (_("%X%P: %pB: %s against %pT "
17546 "is not supported by glibc as a dynamic relocation\n"),
17547 input_bfd,
17548 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17549 sym_name);
17550 warned_dynamic = true;
17553 /* If this reloc is against an external symbol, it will
17554 be computed at runtime, so there's no need to do
17555 anything now. However, for the sake of prelink ensure
17556 that the section contents are a known value. */
17557 if (!relocate)
17559 unresolved_reloc = false;
17560 /* The value chosen here is quite arbitrary as ld.so
17561 ignores section contents except for the special
17562 case of .opd where the contents might be accessed
17563 before relocation. Choose zero, as that won't
17564 cause reloc overflow. */
17565 relocation = 0;
17566 addend = 0;
17567 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17568 to improve backward compatibility with older
17569 versions of ld. */
17570 if (r_type == R_PPC64_ADDR64)
17571 addend = outrel.r_addend;
17572 /* Adjust pc_relative relocs to have zero in *r_offset. */
17573 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17574 addend = outrel.r_offset;
17577 break;
17579 case R_PPC64_COPY:
17580 case R_PPC64_GLOB_DAT:
17581 case R_PPC64_JMP_SLOT:
17582 case R_PPC64_JMP_IREL:
17583 case R_PPC64_RELATIVE:
17584 /* We shouldn't ever see these dynamic relocs in relocatable
17585 files. */
17586 /* Fall through. */
17588 case R_PPC64_PLTGOT16:
17589 case R_PPC64_PLTGOT16_DS:
17590 case R_PPC64_PLTGOT16_HA:
17591 case R_PPC64_PLTGOT16_HI:
17592 case R_PPC64_PLTGOT16_LO:
17593 case R_PPC64_PLTGOT16_LO_DS:
17594 case R_PPC64_PLTREL32:
17595 case R_PPC64_PLTREL64:
17596 /* These ones haven't been implemented yet. */
17598 info->callbacks->einfo
17599 /* xgettext:c-format */
17600 (_("%P: %pB: %s is not supported for `%pT'\n"),
17601 input_bfd,
17602 ppc64_elf_howto_table[r_type]->name, sym_name);
17604 bfd_set_error (bfd_error_invalid_operation);
17605 ret = false;
17606 goto copy_reloc;
17609 /* Multi-instruction sequences that access the TOC can be
17610 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17611 to nop; addi rb,r2,x; */
17612 switch (r_type)
17614 default:
17615 break;
17617 case R_PPC64_GOT_TLSLD16_HI:
17618 case R_PPC64_GOT_TLSGD16_HI:
17619 case R_PPC64_GOT_TPREL16_HI:
17620 case R_PPC64_GOT_DTPREL16_HI:
17621 case R_PPC64_GOT16_HI:
17622 case R_PPC64_TOC16_HI:
17623 /* These relocs would only be useful if building up an
17624 offset to later add to r2, perhaps in an indexed
17625 addressing mode instruction. Don't try to optimize.
17626 Unfortunately, the possibility of someone building up an
17627 offset like this or even with the HA relocs, means that
17628 we need to check the high insn when optimizing the low
17629 insn. */
17630 break;
17632 case R_PPC64_PLTCALL_NOTOC:
17633 if (!unresolved_reloc)
17634 htab->notoc_plt = 1;
17635 /* Fall through. */
17636 case R_PPC64_PLTCALL:
17637 if (unresolved_reloc
17638 && offset_in_range (input_section, rel->r_offset,
17639 r_type == R_PPC64_PLTCALL ? 8 : 4))
17641 /* No plt entry. Make this into a direct call. */
17642 bfd_byte *p = contents + rel->r_offset;
17643 insn = bfd_get_32 (input_bfd, p);
17644 insn &= 1;
17645 bfd_put_32 (input_bfd, B_DOT | insn, p);
17646 if (r_type == R_PPC64_PLTCALL)
17647 bfd_put_32 (input_bfd, NOP, p + 4);
17648 unresolved_reloc = save_unresolved_reloc;
17649 r_type = R_PPC64_REL24;
17651 break;
17653 case R_PPC64_PLTSEQ_NOTOC:
17654 case R_PPC64_PLTSEQ:
17655 if (unresolved_reloc)
17657 unresolved_reloc = false;
17658 goto nop_it;
17660 break;
17662 case R_PPC64_PLT_PCREL34_NOTOC:
17663 if (!unresolved_reloc)
17664 htab->notoc_plt = 1;
17665 /* Fall through. */
17666 case R_PPC64_PLT_PCREL34:
17667 if (unresolved_reloc
17668 && offset_in_range (input_section, rel->r_offset, 8))
17670 bfd_byte *p = contents + rel->r_offset;
17671 bfd_put_32 (input_bfd, PNOP >> 32, p);
17672 bfd_put_32 (input_bfd, PNOP, p + 4);
17673 unresolved_reloc = false;
17674 goto copy_reloc;
17676 break;
17678 case R_PPC64_PLT16_HA:
17679 if (unresolved_reloc)
17681 unresolved_reloc = false;
17682 goto nop_it;
17684 /* Fall through. */
17685 case R_PPC64_GOT_TLSLD16_HA:
17686 case R_PPC64_GOT_TLSGD16_HA:
17687 case R_PPC64_GOT_TPREL16_HA:
17688 case R_PPC64_GOT_DTPREL16_HA:
17689 case R_PPC64_GOT16_HA:
17690 case R_PPC64_TOC16_HA:
17691 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17692 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17693 && !(bfd_link_pic (info)
17694 && (h != NULL
17695 ? bfd_is_abs_symbol (&h->elf.root)
17696 : sec == bfd_abs_section_ptr)))
17698 bfd_byte *p;
17699 nop_it:
17700 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17702 p = contents + (rel->r_offset & ~3);
17703 bfd_put_32 (input_bfd, NOP, p);
17704 goto copy_reloc;
17707 break;
17709 case R_PPC64_PLT16_LO:
17710 case R_PPC64_PLT16_LO_DS:
17711 if (unresolved_reloc)
17713 unresolved_reloc = false;
17714 goto nop_it;
17716 /* Fall through. */
17717 case R_PPC64_GOT_TLSLD16_LO:
17718 case R_PPC64_GOT_TLSGD16_LO:
17719 case R_PPC64_GOT_TPREL16_LO_DS:
17720 case R_PPC64_GOT_DTPREL16_LO_DS:
17721 case R_PPC64_GOT16_LO:
17722 case R_PPC64_GOT16_LO_DS:
17723 case R_PPC64_TOC16_LO:
17724 case R_PPC64_TOC16_LO_DS:
17725 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17726 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17727 && !(bfd_link_pic (info)
17728 && (h != NULL
17729 ? bfd_is_abs_symbol (&h->elf.root)
17730 : sec == bfd_abs_section_ptr))
17731 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17733 bfd_byte *p = contents + (rel->r_offset & ~3);
17734 insn = bfd_get_32 (input_bfd, p);
17735 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17737 /* Transform addic to addi when we change reg. */
17738 insn &= ~((0x3fu << 26) | (0x1f << 16));
17739 insn |= (14u << 26) | (2 << 16);
17741 else
17743 insn &= ~(0x1f << 16);
17744 insn |= 2 << 16;
17746 bfd_put_32 (input_bfd, insn, p);
17748 break;
17750 case R_PPC64_TPREL16_HA:
17751 if (htab->do_tls_opt
17752 && relocation + addend + 0x8000 < 0x10000
17753 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17755 bfd_byte *p = contents + (rel->r_offset & ~3);
17756 bfd_put_32 (input_bfd, NOP, p);
17757 goto copy_reloc;
17759 break;
17761 case R_PPC64_TPREL16_LO:
17762 case R_PPC64_TPREL16_LO_DS:
17763 if (htab->do_tls_opt
17764 && relocation + addend + 0x8000 < 0x10000
17765 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17767 bfd_byte *p = contents + (rel->r_offset & ~3);
17768 insn = bfd_get_32 (input_bfd, p);
17769 insn &= ~(0x1f << 16);
17770 insn |= 13 << 16;
17771 bfd_put_32 (input_bfd, insn, p);
17773 break;
17776 /* Do any further special processing. */
17777 switch (r_type)
17779 default:
17780 break;
17782 case R_PPC64_REL16_HA:
17783 case R_PPC64_REL16_HIGHA:
17784 case R_PPC64_REL16_HIGHERA:
17785 case R_PPC64_REL16_HIGHESTA:
17786 case R_PPC64_REL16DX_HA:
17787 case R_PPC64_ADDR16_HA:
17788 case R_PPC64_ADDR16_HIGHA:
17789 case R_PPC64_ADDR16_HIGHERA:
17790 case R_PPC64_ADDR16_HIGHESTA:
17791 case R_PPC64_TOC16_HA:
17792 case R_PPC64_SECTOFF_HA:
17793 case R_PPC64_TPREL16_HA:
17794 case R_PPC64_TPREL16_HIGHA:
17795 case R_PPC64_TPREL16_HIGHERA:
17796 case R_PPC64_TPREL16_HIGHESTA:
17797 case R_PPC64_DTPREL16_HA:
17798 case R_PPC64_DTPREL16_HIGHA:
17799 case R_PPC64_DTPREL16_HIGHERA:
17800 case R_PPC64_DTPREL16_HIGHESTA:
17801 /* It's just possible that this symbol is a weak symbol
17802 that's not actually defined anywhere. In that case,
17803 'sec' would be NULL, and we should leave the symbol
17804 alone (it will be set to zero elsewhere in the link). */
17805 if (sec == NULL)
17806 break;
17807 /* Fall through. */
17809 case R_PPC64_GOT16_HA:
17810 case R_PPC64_PLTGOT16_HA:
17811 case R_PPC64_PLT16_HA:
17812 case R_PPC64_GOT_TLSGD16_HA:
17813 case R_PPC64_GOT_TLSLD16_HA:
17814 case R_PPC64_GOT_TPREL16_HA:
17815 case R_PPC64_GOT_DTPREL16_HA:
17816 /* Add 0x10000 if sign bit in 0:15 is set.
17817 Bits 0:15 are not used. */
17818 addend += 0x8000;
17819 break;
17821 case R_PPC64_D34_HA30:
17822 case R_PPC64_ADDR16_HIGHERA34:
17823 case R_PPC64_ADDR16_HIGHESTA34:
17824 case R_PPC64_REL16_HIGHERA34:
17825 case R_PPC64_REL16_HIGHESTA34:
17826 if (sec != NULL)
17827 addend += 1ULL << 33;
17828 break;
17830 case R_PPC64_ADDR16_DS:
17831 case R_PPC64_ADDR16_LO_DS:
17832 case R_PPC64_GOT16_DS:
17833 case R_PPC64_GOT16_LO_DS:
17834 case R_PPC64_PLT16_LO_DS:
17835 case R_PPC64_SECTOFF_DS:
17836 case R_PPC64_SECTOFF_LO_DS:
17837 case R_PPC64_TOC16_DS:
17838 case R_PPC64_TOC16_LO_DS:
17839 case R_PPC64_PLTGOT16_DS:
17840 case R_PPC64_PLTGOT16_LO_DS:
17841 case R_PPC64_GOT_TPREL16_DS:
17842 case R_PPC64_GOT_TPREL16_LO_DS:
17843 case R_PPC64_GOT_DTPREL16_DS:
17844 case R_PPC64_GOT_DTPREL16_LO_DS:
17845 case R_PPC64_TPREL16_DS:
17846 case R_PPC64_TPREL16_LO_DS:
17847 case R_PPC64_DTPREL16_DS:
17848 case R_PPC64_DTPREL16_LO_DS:
17849 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17850 break;
17851 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17852 mask = 3;
17853 /* If this reloc is against an lq, lxv, or stxv insn, then
17854 the value must be a multiple of 16. This is somewhat of
17855 a hack, but the "correct" way to do this by defining _DQ
17856 forms of all the _DS relocs bloats all reloc switches in
17857 this file. It doesn't make much sense to use these
17858 relocs in data, so testing the insn should be safe. */
17859 if ((insn & (0x3fu << 26)) == (56u << 26)
17860 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17861 mask = 15;
17862 relocation += addend;
17863 addend = insn & (mask ^ 3);
17864 if ((relocation & mask) != 0)
17866 relocation ^= relocation & mask;
17867 info->callbacks->einfo
17868 /* xgettext:c-format */
17869 (_("%H: error: %s not a multiple of %u\n"),
17870 input_bfd, input_section, rel->r_offset,
17871 ppc64_elf_howto_table[r_type]->name,
17872 mask + 1);
17873 bfd_set_error (bfd_error_bad_value);
17874 ret = false;
17875 goto copy_reloc;
17877 break;
17880 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17881 because such sections are not SEC_ALLOC and thus ld.so will
17882 not process them. */
17883 howto = ppc64_elf_howto_table[(int) r_type];
17884 if (unresolved_reloc
17885 && !((input_section->flags & SEC_DEBUGGING) != 0
17886 && h->elf.def_dynamic)
17887 && _bfd_elf_section_offset (output_bfd, info, input_section,
17888 rel->r_offset) != (bfd_vma) -1)
17890 info->callbacks->einfo
17891 /* xgettext:c-format */
17892 (_("%H: unresolvable %s against `%pT'\n"),
17893 input_bfd, input_section, rel->r_offset,
17894 howto->name,
17895 h->elf.root.root.string);
17896 ret = false;
17899 /* 16-bit fields in insns mostly have signed values, but a
17900 few insns have 16-bit unsigned values. Really, we should
17901 have different reloc types. */
17902 if (howto->complain_on_overflow != complain_overflow_dont
17903 && howto->dst_mask == 0xffff
17904 && (input_section->flags & SEC_CODE) != 0
17905 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17907 enum complain_overflow complain = complain_overflow_signed;
17909 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17910 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17911 complain = complain_overflow_bitfield;
17912 else if (howto->rightshift == 0
17913 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17914 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17915 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17916 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17917 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17918 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17919 complain = complain_overflow_unsigned;
17920 if (howto->complain_on_overflow != complain)
17922 alt_howto = *howto;
17923 alt_howto.complain_on_overflow = complain;
17924 howto = &alt_howto;
17928 switch (r_type)
17930 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17931 case R_PPC64_D34:
17932 case R_PPC64_D34_LO:
17933 case R_PPC64_D34_HI30:
17934 case R_PPC64_D34_HA30:
17935 case R_PPC64_PCREL34:
17936 case R_PPC64_GOT_PCREL34:
17937 case R_PPC64_TPREL34:
17938 case R_PPC64_DTPREL34:
17939 case R_PPC64_GOT_TLSGD_PCREL34:
17940 case R_PPC64_GOT_TLSLD_PCREL34:
17941 case R_PPC64_GOT_TPREL_PCREL34:
17942 case R_PPC64_GOT_DTPREL_PCREL34:
17943 case R_PPC64_PLT_PCREL34:
17944 case R_PPC64_PLT_PCREL34_NOTOC:
17945 case R_PPC64_D28:
17946 case R_PPC64_PCREL28:
17947 if (!offset_in_range (input_section, rel->r_offset, 8))
17948 r = bfd_reloc_outofrange;
17949 else
17951 relocation += addend;
17952 if (howto->pc_relative)
17953 relocation -= (rel->r_offset
17954 + input_section->output_offset
17955 + input_section->output_section->vma);
17956 relocation >>= howto->rightshift;
17958 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17959 pinsn <<= 32;
17960 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17962 pinsn &= ~howto->dst_mask;
17963 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17964 & howto->dst_mask);
17965 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17966 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17967 r = bfd_reloc_ok;
17968 if (howto->complain_on_overflow == complain_overflow_signed
17969 && (relocation + (1ULL << (howto->bitsize - 1))
17970 >= 1ULL << howto->bitsize))
17971 r = bfd_reloc_overflow;
17973 break;
17975 case R_PPC64_REL16DX_HA:
17976 if (!offset_in_range (input_section, rel->r_offset, 4))
17977 r = bfd_reloc_outofrange;
17978 else
17980 relocation += addend;
17981 relocation -= (rel->r_offset
17982 + input_section->output_offset
17983 + input_section->output_section->vma);
17984 relocation = (bfd_signed_vma) relocation >> 16;
17985 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17986 insn &= ~0x1fffc1;
17987 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17988 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17989 r = bfd_reloc_ok;
17990 if (relocation + 0x8000 > 0xffff)
17991 r = bfd_reloc_overflow;
17993 break;
17995 default:
17996 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17997 contents, rel->r_offset,
17998 relocation, addend);
18001 if (r != bfd_reloc_ok)
18003 char *more_info = NULL;
18004 const char *reloc_name = howto->name;
18006 if (reloc_dest != DEST_NORMAL)
18008 more_info = bfd_malloc (strlen (reloc_name) + 8);
18009 if (more_info != NULL)
18011 strcpy (more_info, reloc_name);
18012 strcat (more_info, (reloc_dest == DEST_OPD
18013 ? " (OPD)" : " (stub)"));
18014 reloc_name = more_info;
18018 if (r == bfd_reloc_overflow)
18020 /* On code like "if (foo) foo();" don't report overflow
18021 on a branch to zero when foo is undefined. */
18022 if (!warned
18023 && (reloc_dest == DEST_STUB
18024 || !(h != NULL
18025 && (h->elf.root.type == bfd_link_hash_undefweak
18026 || h->elf.root.type == bfd_link_hash_undefined)
18027 && is_branch_reloc (r_type))))
18028 info->callbacks->reloc_overflow
18029 (info, (struct bfd_link_hash_entry *) h, sym_name,
18030 reloc_name, orig_rel.r_addend, input_bfd, input_section,
18031 rel->r_offset);
18033 else
18035 info->callbacks->einfo
18036 /* xgettext:c-format */
18037 (_("%H: %s against `%pT': error %d\n"),
18038 input_bfd, input_section, rel->r_offset,
18039 reloc_name, sym_name, (int) r);
18040 ret = false;
18042 free (more_info);
18044 copy_reloc:
18045 if (wrel != rel)
18046 *wrel = *rel;
18049 if (wrel != rel)
18051 Elf_Internal_Shdr *rel_hdr;
18052 size_t deleted = rel - wrel;
18054 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18055 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18056 if (rel_hdr->sh_size == 0)
18058 /* It is too late to remove an empty reloc section. Leave
18059 one NONE reloc.
18060 ??? What is wrong with an empty section??? */
18061 rel_hdr->sh_size = rel_hdr->sh_entsize;
18062 deleted -= 1;
18064 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18065 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18066 input_section->reloc_count -= deleted;
18069 /* If we're emitting relocations, then shortly after this function
18070 returns, reloc offsets and addends for this section will be
18071 adjusted. Worse, reloc symbol indices will be for the output
18072 file rather than the input. Save a copy of the relocs for
18073 opd_entry_value. */
18074 if (is_opd
18075 && (info->emitrelocations || bfd_link_relocatable (info))
18076 && input_section->reloc_count != 0)
18078 bfd_size_type amt;
18079 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18080 rel = bfd_alloc (input_bfd, amt);
18081 ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
18082 if (rel == NULL)
18083 return false;
18084 memcpy (rel, relocs, amt);
18086 return ret;
18089 /* Adjust the value of any local symbols in opd sections. */
18091 static int
18092 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18093 const char *name ATTRIBUTE_UNUSED,
18094 Elf_Internal_Sym *elfsym,
18095 asection *input_sec,
18096 struct elf_link_hash_entry *h)
18098 struct _opd_sec_data *opd;
18099 long adjust;
18100 bfd_vma value;
18102 if (h != NULL)
18103 return 1;
18105 opd = get_opd_info (input_sec);
18106 if (opd == NULL || opd->adjust == NULL)
18107 return 1;
18109 value = elfsym->st_value - input_sec->output_offset;
18110 if (!bfd_link_relocatable (info))
18111 value -= input_sec->output_section->vma;
18113 adjust = opd->adjust[OPD_NDX (value)];
18114 if (adjust == -1)
18115 return 2;
18117 elfsym->st_value += adjust;
18118 return 1;
18121 /* Finish up dynamic symbol handling. We set the contents of various
18122 dynamic sections here. */
18124 static bool
18125 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18126 struct bfd_link_info *info,
18127 struct elf_link_hash_entry *h,
18128 Elf_Internal_Sym *sym)
18130 struct ppc_link_hash_table *htab;
18131 struct plt_entry *ent;
18133 htab = ppc_hash_table (info);
18134 if (htab == NULL)
18135 return false;
18137 if (!htab->opd_abi && !h->def_regular)
18138 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18139 if (ent->plt.offset != (bfd_vma) -1)
18141 /* Mark the symbol as undefined, rather than as
18142 defined in glink. Leave the value if there were
18143 any relocations where pointer equality matters
18144 (this is a clue for the dynamic linker, to make
18145 function pointer comparisons work between an
18146 application and shared library), otherwise set it
18147 to zero. */
18148 sym->st_shndx = SHN_UNDEF;
18149 if (!h->pointer_equality_needed)
18150 sym->st_value = 0;
18151 else if (!h->ref_regular_nonweak)
18153 /* This breaks function pointer comparisons, but
18154 that is better than breaking tests for a NULL
18155 function pointer. */
18156 sym->st_value = 0;
18158 break;
18161 if (h->needs_copy
18162 && (h->root.type == bfd_link_hash_defined
18163 || h->root.type == bfd_link_hash_defweak)
18164 && (h->root.u.def.section == htab->elf.sdynbss
18165 || h->root.u.def.section == htab->elf.sdynrelro))
18167 /* This symbol needs a copy reloc. Set it up. */
18168 Elf_Internal_Rela rela;
18169 asection *srel;
18171 if (h->dynindx == -1)
18172 abort ();
18174 rela.r_offset = defined_sym_val (h);
18175 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18176 rela.r_addend = 0;
18177 if (h->root.u.def.section == htab->elf.sdynrelro)
18178 srel = htab->elf.sreldynrelro;
18179 else
18180 srel = htab->elf.srelbss;
18181 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &rela, srel));
18184 return true;
18187 /* Used to decide how to sort relocs in an optimal manner for the
18188 dynamic linker, before writing them out. */
18190 static enum elf_reloc_type_class
18191 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18192 const asection *rel_sec,
18193 const Elf_Internal_Rela *rela)
18195 enum elf_ppc64_reloc_type r_type;
18196 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18198 if (rel_sec == htab->elf.irelplt)
18199 return reloc_class_ifunc;
18201 r_type = ELF64_R_TYPE (rela->r_info);
18202 switch (r_type)
18204 case R_PPC64_RELATIVE:
18205 return reloc_class_relative;
18206 case R_PPC64_JMP_SLOT:
18207 return reloc_class_plt;
18208 case R_PPC64_COPY:
18209 return reloc_class_copy;
18210 default:
18211 return reloc_class_normal;
18215 /* Finish up the dynamic sections. */
18217 static bool
18218 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18219 struct bfd_link_info *info)
18221 struct ppc_link_hash_table *htab;
18222 bfd *dynobj;
18223 asection *sdyn;
18225 htab = ppc_hash_table (info);
18226 if (htab == NULL)
18227 return false;
18229 dynobj = htab->elf.dynobj;
18230 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18232 if (htab->elf.dynamic_sections_created)
18234 Elf64_External_Dyn *dyncon, *dynconend;
18236 if (sdyn == NULL || htab->elf.sgot == NULL)
18237 abort ();
18239 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18240 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18241 for (; dyncon < dynconend; dyncon++)
18243 Elf_Internal_Dyn dyn;
18244 asection *s;
18246 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18248 switch (dyn.d_tag)
18250 default:
18251 continue;
18253 case DT_PPC64_GLINK:
18254 s = htab->glink;
18255 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18256 /* We stupidly defined DT_PPC64_GLINK to be the start
18257 of glink rather than the first entry point, which is
18258 what ld.so needs, and now have a bigger stub to
18259 support automatic multiple TOCs. */
18260 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18261 break;
18263 case DT_PPC64_OPD:
18264 s = bfd_get_section_by_name (output_bfd, ".opd");
18265 if (s == NULL)
18266 continue;
18267 dyn.d_un.d_ptr = s->vma;
18268 break;
18270 case DT_PPC64_OPT:
18271 if ((htab->do_multi_toc && htab->multi_toc_needed)
18272 || htab->notoc_plt)
18273 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18274 if (htab->has_plt_localentry0)
18275 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18276 break;
18278 case DT_PPC64_OPDSZ:
18279 s = bfd_get_section_by_name (output_bfd, ".opd");
18280 if (s == NULL)
18281 continue;
18282 dyn.d_un.d_val = s->size;
18283 break;
18285 case DT_PLTGOT:
18286 s = htab->elf.splt;
18287 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18288 break;
18290 case DT_JMPREL:
18291 s = htab->elf.srelplt;
18292 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18293 break;
18295 case DT_PLTRELSZ:
18296 dyn.d_un.d_val = htab->elf.srelplt->size;
18297 break;
18299 case DT_TEXTREL:
18300 if (htab->elf.ifunc_resolvers)
18301 info->callbacks->einfo
18302 (_("%P: warning: text relocations and GNU indirect "
18303 "functions may result in a segfault at runtime\n"));
18304 continue;
18307 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18311 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18312 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18314 /* Fill in the first entry in the global offset table.
18315 We use it to hold the link-time TOCbase. */
18316 bfd_put_64 (output_bfd,
18317 elf_gp (output_bfd) + TOC_BASE_OFF,
18318 htab->elf.sgot->contents);
18320 /* Set .got entry size. */
18321 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18322 = 8;
18325 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18326 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18328 /* Set .plt entry size. */
18329 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18330 = PLT_ENTRY_SIZE (htab);
18333 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18334 brlt ourselves if emitrelocations. */
18335 if (htab->brlt != NULL
18336 && htab->brlt->reloc_count != 0
18337 && !_bfd_elf_link_output_relocs (output_bfd,
18338 htab->brlt,
18339 elf_section_data (htab->brlt)->rela.hdr,
18340 elf_section_data (htab->brlt)->relocs,
18341 NULL))
18342 return false;
18344 if (htab->glink != NULL
18345 && htab->glink->reloc_count != 0
18346 && !_bfd_elf_link_output_relocs (output_bfd,
18347 htab->glink,
18348 elf_section_data (htab->glink)->rela.hdr,
18349 elf_section_data (htab->glink)->relocs,
18350 NULL))
18351 return false;
18354 if (htab->glink_eh_frame != NULL
18355 && htab->glink_eh_frame->size != 0
18356 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18357 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18358 htab->glink_eh_frame,
18359 htab->glink_eh_frame->contents))
18360 return false;
18362 /* We need to handle writing out multiple GOT sections ourselves,
18363 since we didn't add them to DYNOBJ. We know dynobj is the first
18364 bfd. */
18365 while ((dynobj = dynobj->link.next) != NULL)
18367 asection *s;
18369 if (!is_ppc64_elf (dynobj))
18370 continue;
18372 s = ppc64_elf_tdata (dynobj)->got;
18373 if (s != NULL
18374 && s->size != 0
18375 && s->output_section != bfd_abs_section_ptr
18376 && !bfd_set_section_contents (output_bfd, s->output_section,
18377 s->contents, s->output_offset,
18378 s->size))
18379 return false;
18380 s = ppc64_elf_tdata (dynobj)->relgot;
18381 if (s != NULL
18382 && s->size != 0
18383 && s->output_section != bfd_abs_section_ptr
18384 && !bfd_set_section_contents (output_bfd, s->output_section,
18385 s->contents, s->output_offset,
18386 s->size))
18387 return false;
18390 return true;
18393 static bool
18394 ppc64_elf_free_cached_info (bfd *abfd)
18396 if (abfd->sections)
18397 for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18398 opd != NULL;
18399 opd = bfd_get_next_section_by_name (NULL, opd))
18400 if (opd->reloc_count == 0)
18401 free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18403 return _bfd_elf_free_cached_info (abfd);
18406 #include "elf64-target.h"
18408 /* FreeBSD support */
18410 #undef TARGET_LITTLE_SYM
18411 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18412 #undef TARGET_LITTLE_NAME
18413 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18415 #undef TARGET_BIG_SYM
18416 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18417 #undef TARGET_BIG_NAME
18418 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18420 #undef ELF_OSABI
18421 #define ELF_OSABI ELFOSABI_FREEBSD
18423 #undef elf64_bed
18424 #define elf64_bed elf64_powerpc_fbsd_bed
18426 #include "elf64-target.h"