readelf: Support RELR in -S and -d and output
[binutils-gdb.git] / bfd / elf64-ppc.c
blob40ae96b00d5e67ff08d987a53a0ff06048551f6c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2021 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_RELROPAGESIZE ELF_MAXPAGESIZE
79 #define elf_info_to_howto ppc64_elf_info_to_howto
81 #define elf_backend_want_got_sym 0
82 #define elf_backend_want_plt_sym 0
83 #define elf_backend_plt_alignment 3
84 #define elf_backend_plt_not_loaded 1
85 #define elf_backend_got_header_size 8
86 #define elf_backend_want_dynrelro 1
87 #define elf_backend_can_gc_sections 1
88 #define elf_backend_can_refcount 1
89 #define elf_backend_rela_normal 1
90 #define elf_backend_dtrel_excludes_plt 1
91 #define elf_backend_default_execstack 0
93 #define bfd_elf64_mkobject ppc64_elf_mkobject
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 /* Relocation HOWTO's. */
302 /* Like other ELF RELA targets that don't apply multiple
303 field-altering relocations to the same localation, src_mask is
304 always zero and pcrel_offset is the same as pc_relative.
305 PowerPC can always use a zero bitpos, even when the field is not at
306 the LSB. For example, a REL24 could use rightshift=2, bisize=24
307 and bitpos=2 which matches the ABI description, or as we do here,
308 rightshift=0, bitsize=26 and bitpos=0. */
309 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
310 complain, special_func) \
311 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
312 complain_overflow_ ## complain, special_func, \
313 #type, false, 0, mask, pc_relative)
315 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
317 static reloc_howto_type ppc64_elf_howto_raw[] =
319 /* This reloc does nothing. */
320 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
321 bfd_elf_generic_reloc),
323 /* A standard 32 bit relocation. */
324 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
325 bfd_elf_generic_reloc),
327 /* An absolute 26 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
330 bfd_elf_generic_reloc),
332 /* A standard 16 bit relocation. */
333 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
334 bfd_elf_generic_reloc),
336 /* A 16 bit relocation without overflow. */
337 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
338 bfd_elf_generic_reloc),
340 /* Bits 16-31 of an address. */
341 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
342 bfd_elf_generic_reloc),
344 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
345 bits, treated as a signed number, is negative. */
346 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
347 ppc64_elf_ha_reloc),
349 /* An absolute 16 bit branch; the lower two bits must be zero.
350 FIXME: we don't check that, we just clear them. */
351 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
352 ppc64_elf_branch_reloc),
354 /* An absolute 16 bit branch, for which bit 10 should be set to
355 indicate that the branch is expected to be taken. The lower two
356 bits must be zero. */
357 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
358 ppc64_elf_brtaken_reloc),
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
364 ppc64_elf_brtaken_reloc),
366 /* A relative 26 bit branch; the lower two bits must be zero. */
367 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
368 ppc64_elf_branch_reloc),
370 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
371 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
372 ppc64_elf_branch_reloc),
374 /* A relative 16 bit branch; the lower two bits must be zero. */
375 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
376 ppc64_elf_branch_reloc),
378 /* A relative 16 bit branch. Bit 10 should be set to indicate that
379 the branch is expected to be taken. The lower two bits must be
380 zero. */
381 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
382 ppc64_elf_brtaken_reloc),
384 /* A relative 16 bit branch. Bit 10 should be set to indicate that
385 the branch is not expected to be taken. The lower two bits must
386 be zero. */
387 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
388 ppc64_elf_brtaken_reloc),
390 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
391 symbol. */
392 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
393 ppc64_elf_unhandled_reloc),
395 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
396 the symbol. */
397 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
398 ppc64_elf_unhandled_reloc),
400 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
401 the symbol. */
402 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
403 ppc64_elf_unhandled_reloc),
405 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
406 the symbol. */
407 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
408 ppc64_elf_unhandled_reloc),
410 /* This is used only by the dynamic linker. The symbol should exist
411 both in the object being run and in some shared library. The
412 dynamic linker copies the data addressed by the symbol from the
413 shared library into the object, because the object being
414 run has to have the data at some particular address. */
415 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
416 ppc64_elf_unhandled_reloc),
418 /* Like R_PPC64_ADDR64, but used when setting global offset table
419 entries. */
420 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
421 ppc64_elf_unhandled_reloc),
423 /* Created by the link editor. Marks a procedure linkage table
424 entry for a symbol. */
425 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
426 ppc64_elf_unhandled_reloc),
428 /* Used only by the dynamic linker. When the object is run, this
429 doubleword64 is set to the load address of the object, plus the
430 addend. */
431 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
432 bfd_elf_generic_reloc),
434 /* Like R_PPC64_ADDR32, but may be unaligned. */
435 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
436 bfd_elf_generic_reloc),
438 /* Like R_PPC64_ADDR16, but may be unaligned. */
439 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
440 bfd_elf_generic_reloc),
442 /* 32-bit PC relative. */
443 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
444 bfd_elf_generic_reloc),
446 /* 32-bit relocation to the symbol's procedure linkage table. */
447 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
448 ppc64_elf_unhandled_reloc),
450 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
451 FIXME: R_PPC64_PLTREL32 not supported. */
452 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
453 ppc64_elf_unhandled_reloc),
455 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
456 the symbol. */
457 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
458 ppc64_elf_unhandled_reloc),
460 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
461 the symbol. */
462 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
463 ppc64_elf_unhandled_reloc),
465 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
466 the symbol. */
467 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
468 ppc64_elf_unhandled_reloc),
470 /* 16-bit section relative relocation. */
471 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
472 ppc64_elf_sectoff_reloc),
474 /* Like R_PPC64_SECTOFF, but no overflow warning. */
475 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
476 ppc64_elf_sectoff_reloc),
478 /* 16-bit upper half section relative relocation. */
479 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
480 ppc64_elf_sectoff_reloc),
482 /* 16-bit upper half adjusted section relative relocation. */
483 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
484 ppc64_elf_sectoff_ha_reloc),
486 /* Like R_PPC64_REL24 without touching the two least significant bits. */
487 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
488 bfd_elf_generic_reloc),
490 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
492 /* A standard 64-bit relocation. */
493 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
494 bfd_elf_generic_reloc),
496 /* The bits 32-47 of an address. */
497 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
498 bfd_elf_generic_reloc),
500 /* The bits 32-47 of an address, plus 1 if the contents of the low
501 16 bits, treated as a signed number, is negative. */
502 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
503 ppc64_elf_ha_reloc),
505 /* The bits 48-63 of an address. */
506 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
507 bfd_elf_generic_reloc),
509 /* The bits 48-63 of an address, plus 1 if the contents of the low
510 16 bits, treated as a signed number, is negative. */
511 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
512 ppc64_elf_ha_reloc),
514 /* Like ADDR64, but may be unaligned. */
515 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
516 bfd_elf_generic_reloc),
518 /* 64-bit relative relocation. */
519 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
520 bfd_elf_generic_reloc),
522 /* 64-bit relocation to the symbol's procedure linkage table. */
523 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
524 ppc64_elf_unhandled_reloc),
526 /* 64-bit PC relative relocation to the symbol's procedure linkage
527 table. */
528 /* FIXME: R_PPC64_PLTREL64 not supported. */
529 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
530 ppc64_elf_unhandled_reloc),
532 /* 16 bit TOC-relative relocation. */
533 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
534 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
535 ppc64_elf_toc_reloc),
537 /* 16 bit TOC-relative relocation without overflow. */
538 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
539 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
540 ppc64_elf_toc_reloc),
542 /* 16 bit TOC-relative relocation, high 16 bits. */
543 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
544 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
545 ppc64_elf_toc_reloc),
547 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
548 contents of the low 16 bits, treated as a signed number, is
549 negative. */
550 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
551 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
552 ppc64_elf_toc_ha_reloc),
554 /* 64-bit relocation; insert value of TOC base (.TOC.). */
555 /* R_PPC64_TOC 51 doubleword64 .TOC. */
556 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
557 ppc64_elf_toc64_reloc),
559 /* Like R_PPC64_GOT16, but also informs the link editor that the
560 value to relocate may (!) refer to a PLT entry which the link
561 editor (a) may replace with the symbol value. If the link editor
562 is unable to fully resolve the symbol, it may (b) create a PLT
563 entry and store the address to the new PLT entry in the GOT.
564 This permits lazy resolution of function symbols at run time.
565 The link editor may also skip all of this and just (c) emit a
566 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
567 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
568 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
569 ppc64_elf_unhandled_reloc),
571 /* Like R_PPC64_PLTGOT16, but without overflow. */
572 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
573 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
574 ppc64_elf_unhandled_reloc),
576 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
577 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
578 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
579 ppc64_elf_unhandled_reloc),
581 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
582 1 if the contents of the low 16 bits, treated as a signed number,
583 is negative. */
584 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
585 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
586 ppc64_elf_unhandled_reloc),
588 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
589 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
590 bfd_elf_generic_reloc),
592 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
593 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
594 bfd_elf_generic_reloc),
596 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
597 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
598 ppc64_elf_unhandled_reloc),
600 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
601 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
602 ppc64_elf_unhandled_reloc),
604 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
605 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
606 ppc64_elf_unhandled_reloc),
608 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
609 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
610 ppc64_elf_sectoff_reloc),
612 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
613 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
614 ppc64_elf_sectoff_reloc),
616 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
617 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
618 ppc64_elf_toc_reloc),
620 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
621 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
622 ppc64_elf_toc_reloc),
624 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
625 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
626 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
627 ppc64_elf_unhandled_reloc),
629 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
630 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
631 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
632 ppc64_elf_unhandled_reloc),
634 /* Marker relocs for TLS. */
635 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
636 bfd_elf_generic_reloc),
638 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
639 bfd_elf_generic_reloc),
641 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
642 bfd_elf_generic_reloc),
644 /* Marker reloc for optimizing r2 save in prologue rather than on
645 each plt call stub. */
646 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
647 bfd_elf_generic_reloc),
649 /* Marker relocs on inline plt call instructions. */
650 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
651 bfd_elf_generic_reloc),
653 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
654 bfd_elf_generic_reloc),
656 /* Computes the load module index of the load module that contains the
657 definition of its TLS sym. */
658 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
659 ppc64_elf_unhandled_reloc),
661 /* Computes a dtv-relative displacement, the difference between the value
662 of sym+add and the base address of the thread-local storage block that
663 contains the definition of sym, minus 0x8000. */
664 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
665 ppc64_elf_unhandled_reloc),
667 /* A 16 bit dtprel reloc. */
668 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
669 ppc64_elf_unhandled_reloc),
671 /* Like DTPREL16, but no overflow. */
672 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
673 ppc64_elf_unhandled_reloc),
675 /* Like DTPREL16_LO, but next higher group of 16 bits. */
676 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
677 ppc64_elf_unhandled_reloc),
679 /* Like DTPREL16_HI, but adjust for low 16 bits. */
680 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
681 ppc64_elf_unhandled_reloc),
683 /* Like DTPREL16_HI, but next higher group of 16 bits. */
684 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
685 ppc64_elf_unhandled_reloc),
687 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
688 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
689 ppc64_elf_unhandled_reloc),
691 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
692 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
693 ppc64_elf_unhandled_reloc),
695 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
696 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
697 ppc64_elf_unhandled_reloc),
699 /* Like DTPREL16, but for insns with a DS field. */
700 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
701 ppc64_elf_unhandled_reloc),
703 /* Like DTPREL16_DS, but no overflow. */
704 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
705 ppc64_elf_unhandled_reloc),
707 /* Computes a tp-relative displacement, the difference between the value of
708 sym+add and the value of the thread pointer (r13). */
709 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
710 ppc64_elf_unhandled_reloc),
712 /* A 16 bit tprel reloc. */
713 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
714 ppc64_elf_unhandled_reloc),
716 /* Like TPREL16, but no overflow. */
717 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
718 ppc64_elf_unhandled_reloc),
720 /* Like TPREL16_LO, but next higher group of 16 bits. */
721 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
722 ppc64_elf_unhandled_reloc),
724 /* Like TPREL16_HI, but adjust for low 16 bits. */
725 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
726 ppc64_elf_unhandled_reloc),
728 /* Like TPREL16_HI, but next higher group of 16 bits. */
729 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
730 ppc64_elf_unhandled_reloc),
732 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
733 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
734 ppc64_elf_unhandled_reloc),
736 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
737 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
738 ppc64_elf_unhandled_reloc),
740 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
741 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
742 ppc64_elf_unhandled_reloc),
744 /* Like TPREL16, but for insns with a DS field. */
745 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
746 ppc64_elf_unhandled_reloc),
748 /* Like TPREL16_DS, but no overflow. */
749 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
750 ppc64_elf_unhandled_reloc),
752 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
753 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
754 to the first entry relative to the TOC base (r2). */
755 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
756 ppc64_elf_unhandled_reloc),
758 /* Like GOT_TLSGD16, but no overflow. */
759 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
760 ppc64_elf_unhandled_reloc),
762 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
763 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
764 ppc64_elf_unhandled_reloc),
766 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
767 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
768 ppc64_elf_unhandled_reloc),
770 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
771 with values (sym+add)@dtpmod and zero, and computes the offset to the
772 first entry relative to the TOC base (r2). */
773 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
774 ppc64_elf_unhandled_reloc),
776 /* Like GOT_TLSLD16, but no overflow. */
777 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
778 ppc64_elf_unhandled_reloc),
780 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
781 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
782 ppc64_elf_unhandled_reloc),
784 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
785 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
786 ppc64_elf_unhandled_reloc),
788 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
789 the offset to the entry relative to the TOC base (r2). */
790 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
791 ppc64_elf_unhandled_reloc),
793 /* Like GOT_DTPREL16_DS, but no overflow. */
794 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
795 ppc64_elf_unhandled_reloc),
797 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
798 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
799 ppc64_elf_unhandled_reloc),
801 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
802 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
803 ppc64_elf_unhandled_reloc),
805 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
806 offset to the entry relative to the TOC base (r2). */
807 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
808 ppc64_elf_unhandled_reloc),
810 /* Like GOT_TPREL16_DS, but no overflow. */
811 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
812 ppc64_elf_unhandled_reloc),
814 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
815 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
816 ppc64_elf_unhandled_reloc),
818 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
819 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
820 ppc64_elf_unhandled_reloc),
822 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
823 ppc64_elf_unhandled_reloc),
825 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
826 bfd_elf_generic_reloc),
828 /* A 16 bit relative relocation. */
829 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
830 bfd_elf_generic_reloc),
832 /* A 16 bit relative relocation without overflow. */
833 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
834 bfd_elf_generic_reloc),
836 /* The high order 16 bits of a relative address. */
837 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
838 bfd_elf_generic_reloc),
840 /* The high order 16 bits of a relative address, plus 1 if the contents of
841 the low 16 bits, treated as a signed number, is negative. */
842 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
843 ppc64_elf_ha_reloc),
845 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
846 bfd_elf_generic_reloc),
848 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
849 ppc64_elf_ha_reloc),
851 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
852 bfd_elf_generic_reloc),
854 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
855 ppc64_elf_ha_reloc),
857 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
858 bfd_elf_generic_reloc),
860 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
861 ppc64_elf_ha_reloc),
863 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
864 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
865 ppc64_elf_ha_reloc),
867 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
868 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
869 ppc64_elf_ha_reloc),
871 /* Like R_PPC64_ADDR16_HI, but no overflow. */
872 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
873 bfd_elf_generic_reloc),
875 /* Like R_PPC64_ADDR16_HA, but no overflow. */
876 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
877 ppc64_elf_ha_reloc),
879 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
880 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
881 ppc64_elf_unhandled_reloc),
883 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
884 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
885 ppc64_elf_unhandled_reloc),
887 /* Like R_PPC64_TPREL16_HI, but no overflow. */
888 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
889 ppc64_elf_unhandled_reloc),
891 /* Like R_PPC64_TPREL16_HA, but no overflow. */
892 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
893 ppc64_elf_unhandled_reloc),
895 /* Marker reloc on ELFv2 large-model function entry. */
896 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
897 bfd_elf_generic_reloc),
899 /* Like ADDR64, but use local entry point of function. */
900 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
901 bfd_elf_generic_reloc),
903 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
904 bfd_elf_generic_reloc),
906 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
907 bfd_elf_generic_reloc),
909 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
910 bfd_elf_generic_reloc),
912 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
913 ppc64_elf_prefix_reloc),
915 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
916 ppc64_elf_prefix_reloc),
918 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
919 ppc64_elf_prefix_reloc),
921 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
922 ppc64_elf_prefix_reloc),
924 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
925 ppc64_elf_prefix_reloc),
927 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
928 ppc64_elf_unhandled_reloc),
930 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
931 ppc64_elf_unhandled_reloc),
933 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
934 ppc64_elf_unhandled_reloc),
936 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
937 ppc64_elf_unhandled_reloc),
939 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
940 ppc64_elf_unhandled_reloc),
942 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
943 ppc64_elf_unhandled_reloc),
945 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
946 ppc64_elf_unhandled_reloc),
948 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
949 ppc64_elf_unhandled_reloc),
951 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
952 ppc64_elf_unhandled_reloc),
954 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
955 bfd_elf_generic_reloc),
957 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
958 ppc64_elf_ha_reloc),
960 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
961 bfd_elf_generic_reloc),
963 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
964 ppc64_elf_ha_reloc),
966 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
967 bfd_elf_generic_reloc),
969 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
970 ppc64_elf_ha_reloc),
972 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
973 bfd_elf_generic_reloc),
975 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
976 ppc64_elf_ha_reloc),
978 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
979 ppc64_elf_prefix_reloc),
981 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
982 ppc64_elf_prefix_reloc),
984 /* GNU extension to record C++ vtable hierarchy. */
985 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
986 NULL),
988 /* GNU extension to record C++ vtable member usage. */
989 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
990 NULL),
994 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
995 be done. */
997 static void
998 ppc_howto_init (void)
1000 unsigned int i, type;
1002 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1004 type = ppc64_elf_howto_raw[i].type;
1005 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1006 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1010 static reloc_howto_type *
1011 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1013 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1015 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1016 /* Initialize howto table if needed. */
1017 ppc_howto_init ();
1019 switch (code)
1021 default:
1022 /* xgettext:c-format */
1023 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1024 (int) code);
1025 bfd_set_error (bfd_error_bad_value);
1026 return NULL;
1028 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1029 break;
1030 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1031 break;
1032 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1033 break;
1034 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1035 break;
1036 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1037 break;
1038 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1039 break;
1040 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1041 break;
1042 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1043 break;
1044 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1045 break;
1046 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1047 break;
1048 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1049 break;
1050 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1051 break;
1052 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1053 break;
1054 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1055 break;
1056 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1057 break;
1058 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1059 break;
1060 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1061 break;
1062 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1063 break;
1064 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1065 break;
1066 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1067 break;
1068 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1069 break;
1070 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1071 break;
1072 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1073 break;
1074 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1075 break;
1076 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1077 break;
1078 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1079 break;
1080 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1081 break;
1082 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1083 break;
1084 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1085 break;
1086 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1087 break;
1088 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1089 break;
1090 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1091 break;
1092 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1093 break;
1094 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1095 break;
1096 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1097 break;
1098 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1099 break;
1100 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1101 break;
1102 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1103 break;
1104 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1105 break;
1106 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1107 break;
1108 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1109 break;
1110 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1111 break;
1112 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1113 break;
1114 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1115 break;
1116 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1117 break;
1118 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1119 break;
1120 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1121 break;
1122 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1123 break;
1124 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1125 break;
1126 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1127 break;
1128 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1129 break;
1130 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1131 break;
1132 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1133 break;
1134 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1135 break;
1136 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1137 break;
1138 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1139 break;
1140 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1141 break;
1142 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1143 break;
1144 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1145 break;
1146 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1147 break;
1148 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1149 break;
1150 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1151 break;
1152 case BFD_RELOC_PPC64_TLS_PCREL:
1153 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1154 break;
1155 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1156 break;
1157 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1158 break;
1159 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1160 break;
1161 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1162 break;
1163 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1164 break;
1165 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1166 break;
1167 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1168 break;
1169 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1170 break;
1171 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1172 break;
1173 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1174 break;
1175 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1176 break;
1177 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1178 break;
1179 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1180 break;
1181 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1182 break;
1183 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1184 break;
1185 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1186 break;
1187 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1212 break;
1213 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1214 break;
1215 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1216 break;
1217 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1218 break;
1219 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1220 break;
1221 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1222 break;
1223 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1224 break;
1225 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1226 break;
1227 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1228 break;
1229 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1232 break;
1233 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1234 break;
1235 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1236 break;
1237 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1238 break;
1239 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1240 break;
1241 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1244 break;
1245 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1246 break;
1247 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1248 break;
1249 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1250 break;
1251 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1252 break;
1253 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1254 break;
1255 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1256 break;
1257 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1258 break;
1259 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1260 break;
1261 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1262 break;
1263 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1264 break;
1265 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1266 break;
1267 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1268 break;
1269 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1270 break;
1271 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1272 break;
1273 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1274 break;
1275 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1276 break;
1277 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1278 break;
1279 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1280 break;
1281 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1282 break;
1283 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1284 break;
1285 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1286 break;
1287 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1288 break;
1289 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1290 break;
1291 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1292 break;
1293 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1294 break;
1295 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1296 break;
1297 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1298 break;
1299 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1300 break;
1301 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1302 break;
1303 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1304 break;
1305 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1306 break;
1307 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1308 break;
1309 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1310 break;
1311 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1312 break;
1313 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1314 break;
1315 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1316 break;
1317 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1318 break;
1319 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1320 break;
1321 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1322 break;
1325 return ppc64_elf_howto_table[r];
1328 static reloc_howto_type *
1329 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1331 unsigned int i;
1332 static char *compat_map[][2] = {
1333 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1334 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1335 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1336 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1339 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1340 if (ppc64_elf_howto_raw[i].name != NULL
1341 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1342 return &ppc64_elf_howto_raw[i];
1344 /* Handle old names of relocations in case they were used by
1345 .reloc directives.
1346 FIXME: Remove this soon. Mapping the reloc names is very likely
1347 completely unnecessary. */
1348 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1349 if (strcasecmp (compat_map[i][0], r_name) == 0)
1351 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1352 compat_map[i][1], compat_map[i][0]);
1353 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1356 return NULL;
1359 /* Set the howto pointer for a PowerPC ELF reloc. */
1361 static bool
1362 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1363 Elf_Internal_Rela *dst)
1365 unsigned int type;
1367 /* Initialize howto table if needed. */
1368 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1369 ppc_howto_init ();
1371 type = ELF64_R_TYPE (dst->r_info);
1372 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1374 /* xgettext:c-format */
1375 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1376 abfd, type);
1377 bfd_set_error (bfd_error_bad_value);
1378 return false;
1380 cache_ptr->howto = ppc64_elf_howto_table[type];
1381 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1383 /* xgettext:c-format */
1384 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1385 abfd, type);
1386 bfd_set_error (bfd_error_bad_value);
1387 return false;
1390 return true;
1393 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1395 static bfd_reloc_status_type
1396 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1397 void *data, asection *input_section,
1398 bfd *output_bfd, char **error_message)
1400 enum elf_ppc64_reloc_type r_type;
1401 long insn;
1402 bfd_size_type octets;
1403 bfd_vma value;
1405 /* If this is a relocatable link (output_bfd test tells us), just
1406 call the generic function. Any adjustment will be done at final
1407 link time. */
1408 if (output_bfd != NULL)
1409 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1410 input_section, output_bfd, error_message);
1412 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1413 We won't actually be using the low bits, so trashing them
1414 doesn't matter. */
1415 r_type = reloc_entry->howto->type;
1416 if (r_type == R_PPC64_ADDR16_HIGHERA34
1417 || r_type == R_PPC64_ADDR16_HIGHESTA34
1418 || r_type == R_PPC64_REL16_HIGHERA34
1419 || r_type == R_PPC64_REL16_HIGHESTA34)
1420 reloc_entry->addend += 1ULL << 33;
1421 else
1422 reloc_entry->addend += 1U << 15;
1423 if (r_type != R_PPC64_REL16DX_HA)
1424 return bfd_reloc_continue;
1426 value = 0;
1427 if (!bfd_is_com_section (symbol->section))
1428 value = symbol->value;
1429 value += (reloc_entry->addend
1430 + symbol->section->output_offset
1431 + symbol->section->output_section->vma);
1432 value -= (reloc_entry->address
1433 + input_section->output_offset
1434 + input_section->output_section->vma);
1435 value = (bfd_signed_vma) value >> 16;
1437 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1438 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1439 input_section, octets))
1440 return bfd_reloc_outofrange;
1442 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1443 insn &= ~0x1fffc1;
1444 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1445 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1446 if (value + 0x8000 > 0xffff)
1447 return bfd_reloc_overflow;
1448 return bfd_reloc_ok;
1451 static bfd_reloc_status_type
1452 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1453 void *data, asection *input_section,
1454 bfd *output_bfd, char **error_message)
1456 if (output_bfd != NULL)
1457 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1458 input_section, output_bfd, error_message);
1460 if (strcmp (symbol->section->name, ".opd") == 0
1461 && (symbol->section->owner->flags & DYNAMIC) == 0)
1463 bfd_vma dest = opd_entry_value (symbol->section,
1464 symbol->value + reloc_entry->addend,
1465 NULL, NULL, false);
1466 if (dest != (bfd_vma) -1)
1467 reloc_entry->addend = dest - (symbol->value
1468 + symbol->section->output_section->vma
1469 + symbol->section->output_offset);
1471 else
1473 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1475 if (symbol->section->owner != abfd
1476 && symbol->section->owner != NULL
1477 && abiversion (symbol->section->owner) >= 2)
1479 unsigned int i;
1481 for (i = 0; i < symbol->section->owner->symcount; ++i)
1483 asymbol *symdef = symbol->section->owner->outsymbols[i];
1485 if (strcmp (symdef->name, symbol->name) == 0)
1487 elfsym = (elf_symbol_type *) symdef;
1488 break;
1492 reloc_entry->addend
1493 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1495 return bfd_reloc_continue;
1498 static bfd_reloc_status_type
1499 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1500 void *data, asection *input_section,
1501 bfd *output_bfd, char **error_message)
1503 long insn;
1504 enum elf_ppc64_reloc_type r_type;
1505 bfd_size_type octets;
1506 /* Assume 'at' branch hints. */
1507 bool is_isa_v2 = true;
1509 /* If this is a relocatable link (output_bfd test tells us), just
1510 call the generic function. Any adjustment will be done at final
1511 link time. */
1512 if (output_bfd != NULL)
1513 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1514 input_section, output_bfd, error_message);
1516 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1517 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1518 input_section, octets))
1519 return bfd_reloc_outofrange;
1521 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1522 insn &= ~(0x01 << 21);
1523 r_type = reloc_entry->howto->type;
1524 if (r_type == R_PPC64_ADDR14_BRTAKEN
1525 || r_type == R_PPC64_REL14_BRTAKEN)
1526 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1528 if (is_isa_v2)
1530 /* Set 'a' bit. This is 0b00010 in BO field for branch
1531 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1532 for branch on CTR insns (BO == 1a00t or 1a01t). */
1533 if ((insn & (0x14 << 21)) == (0x04 << 21))
1534 insn |= 0x02 << 21;
1535 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1536 insn |= 0x08 << 21;
1537 else
1538 goto out;
1540 else
1542 bfd_vma target = 0;
1543 bfd_vma from;
1545 if (!bfd_is_com_section (symbol->section))
1546 target = symbol->value;
1547 target += symbol->section->output_section->vma;
1548 target += symbol->section->output_offset;
1549 target += reloc_entry->addend;
1551 from = (reloc_entry->address
1552 + input_section->output_offset
1553 + input_section->output_section->vma);
1555 /* Invert 'y' bit if not the default. */
1556 if ((bfd_signed_vma) (target - from) < 0)
1557 insn ^= 0x01 << 21;
1559 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1560 out:
1561 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1562 input_section, output_bfd, error_message);
1565 static bfd_reloc_status_type
1566 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1567 void *data, asection *input_section,
1568 bfd *output_bfd, char **error_message)
1570 /* If this is a relocatable link (output_bfd test tells us), just
1571 call the generic function. Any adjustment will be done at final
1572 link time. */
1573 if (output_bfd != NULL)
1574 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1575 input_section, output_bfd, error_message);
1577 /* Subtract the symbol section base address. */
1578 reloc_entry->addend -= symbol->section->output_section->vma;
1579 return bfd_reloc_continue;
1582 static bfd_reloc_status_type
1583 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1584 void *data, asection *input_section,
1585 bfd *output_bfd, char **error_message)
1587 /* If this is a relocatable link (output_bfd test tells us), just
1588 call the generic function. Any adjustment will be done at final
1589 link time. */
1590 if (output_bfd != NULL)
1591 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1592 input_section, output_bfd, error_message);
1594 /* Subtract the symbol section base address. */
1595 reloc_entry->addend -= symbol->section->output_section->vma;
1597 /* Adjust the addend for sign extension of the low 16 bits. */
1598 reloc_entry->addend += 0x8000;
1599 return bfd_reloc_continue;
1602 static bfd_reloc_status_type
1603 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1604 void *data, asection *input_section,
1605 bfd *output_bfd, char **error_message)
1607 bfd_vma TOCstart;
1609 /* If this is a relocatable link (output_bfd test tells us), just
1610 call the generic function. Any adjustment will be done at final
1611 link time. */
1612 if (output_bfd != NULL)
1613 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1614 input_section, output_bfd, error_message);
1616 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1617 if (TOCstart == 0)
1618 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1620 /* Subtract the TOC base address. */
1621 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1622 return bfd_reloc_continue;
1625 static bfd_reloc_status_type
1626 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1627 void *data, asection *input_section,
1628 bfd *output_bfd, char **error_message)
1630 bfd_vma TOCstart;
1632 /* If this is a relocatable link (output_bfd test tells us), just
1633 call the generic function. Any adjustment will be done at final
1634 link time. */
1635 if (output_bfd != NULL)
1636 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1637 input_section, output_bfd, error_message);
1639 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1640 if (TOCstart == 0)
1641 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1643 /* Subtract the TOC base address. */
1644 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1646 /* Adjust the addend for sign extension of the low 16 bits. */
1647 reloc_entry->addend += 0x8000;
1648 return bfd_reloc_continue;
1651 static bfd_reloc_status_type
1652 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1653 void *data, asection *input_section,
1654 bfd *output_bfd, char **error_message)
1656 bfd_vma TOCstart;
1657 bfd_size_type octets;
1659 /* If this is a relocatable link (output_bfd test tells us), just
1660 call the generic function. Any adjustment will be done at final
1661 link time. */
1662 if (output_bfd != NULL)
1663 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1664 input_section, output_bfd, error_message);
1666 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1667 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1668 input_section, octets))
1669 return bfd_reloc_outofrange;
1671 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1672 if (TOCstart == 0)
1673 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1675 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1676 return bfd_reloc_ok;
1679 static bfd_reloc_status_type
1680 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1681 void *data, asection *input_section,
1682 bfd *output_bfd, char **error_message)
1684 uint64_t insn;
1685 bfd_vma targ;
1686 bfd_size_type octets;
1688 if (output_bfd != NULL)
1689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1690 input_section, output_bfd, error_message);
1692 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1693 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1694 input_section, octets))
1695 return bfd_reloc_outofrange;
1697 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1698 insn <<= 32;
1699 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1701 targ = (symbol->section->output_section->vma
1702 + symbol->section->output_offset
1703 + reloc_entry->addend);
1704 if (!bfd_is_com_section (symbol->section))
1705 targ += symbol->value;
1706 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1707 targ += 1ULL << 33;
1708 if (reloc_entry->howto->pc_relative)
1710 bfd_vma from = (reloc_entry->address
1711 + input_section->output_offset
1712 + input_section->output_section->vma);
1713 targ -=from;
1715 targ >>= reloc_entry->howto->rightshift;
1716 insn &= ~reloc_entry->howto->dst_mask;
1717 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1718 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1719 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1720 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1721 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1722 >= 1ULL << reloc_entry->howto->bitsize))
1723 return bfd_reloc_overflow;
1724 return bfd_reloc_ok;
1727 static bfd_reloc_status_type
1728 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1729 void *data, asection *input_section,
1730 bfd *output_bfd, char **error_message)
1732 /* If this is a relocatable link (output_bfd test tells us), just
1733 call the generic function. Any adjustment will be done at final
1734 link time. */
1735 if (output_bfd != NULL)
1736 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1737 input_section, output_bfd, error_message);
1739 if (error_message != NULL)
1741 static char *message;
1742 free (message);
1743 if (asprintf (&message, _("generic linker can't handle %s"),
1744 reloc_entry->howto->name) < 0)
1745 message = NULL;
1746 *error_message = message;
1748 return bfd_reloc_dangerous;
1751 /* Track GOT entries needed for a given symbol. We might need more
1752 than one got entry per symbol. */
1753 struct got_entry
1755 struct got_entry *next;
1757 /* The symbol addend that we'll be placing in the GOT. */
1758 bfd_vma addend;
1760 /* Unlike other ELF targets, we use separate GOT entries for the same
1761 symbol referenced from different input files. This is to support
1762 automatic multiple TOC/GOT sections, where the TOC base can vary
1763 from one input file to another. After partitioning into TOC groups
1764 we merge entries within the group.
1766 Point to the BFD owning this GOT entry. */
1767 bfd *owner;
1769 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1770 TLS_TPREL or TLS_DTPREL for tls entries. */
1771 unsigned char tls_type;
1773 /* Non-zero if got.ent points to real entry. */
1774 unsigned char is_indirect;
1776 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1777 union
1779 bfd_signed_vma refcount;
1780 bfd_vma offset;
1781 struct got_entry *ent;
1782 } got;
1785 /* The same for PLT. */
1786 struct plt_entry
1788 struct plt_entry *next;
1790 bfd_vma addend;
1792 union
1794 bfd_signed_vma refcount;
1795 bfd_vma offset;
1796 } plt;
1799 struct ppc64_elf_obj_tdata
1801 struct elf_obj_tdata elf;
1803 /* Shortcuts to dynamic linker sections. */
1804 asection *got;
1805 asection *relgot;
1807 /* Used during garbage collection. We attach global symbols defined
1808 on removed .opd entries to this section so that the sym is removed. */
1809 asection *deleted_section;
1811 /* TLS local dynamic got entry handling. Support for multiple GOT
1812 sections means we potentially need one of these for each input bfd. */
1813 struct got_entry tlsld_got;
1815 union
1817 /* A copy of relocs before they are modified for --emit-relocs. */
1818 Elf_Internal_Rela *relocs;
1820 /* Section contents. */
1821 bfd_byte *contents;
1822 } opd;
1824 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1825 the reloc to be in the range -32768 to 32767. */
1826 unsigned int has_small_toc_reloc : 1;
1828 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1829 instruction not one we handle. */
1830 unsigned int unexpected_toc_insn : 1;
1832 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1833 this file. */
1834 unsigned int has_optrel : 1;
1837 #define ppc64_elf_tdata(bfd) \
1838 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1840 #define ppc64_tlsld_got(bfd) \
1841 (&ppc64_elf_tdata (bfd)->tlsld_got)
1843 #define is_ppc64_elf(bfd) \
1844 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1845 && elf_object_id (bfd) == PPC64_ELF_DATA)
1847 /* Override the generic function because we store some extras. */
1849 static bool
1850 ppc64_elf_mkobject (bfd *abfd)
1852 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1853 PPC64_ELF_DATA);
1856 /* Fix bad default arch selected for a 64 bit input bfd when the
1857 default is 32 bit. Also select arch based on apuinfo. */
1859 static bool
1860 ppc64_elf_object_p (bfd *abfd)
1862 if (!abfd->arch_info->the_default)
1863 return true;
1865 if (abfd->arch_info->bits_per_word == 32)
1867 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1869 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1871 /* Relies on arch after 32 bit default being 64 bit default. */
1872 abfd->arch_info = abfd->arch_info->next;
1873 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1876 return _bfd_elf_ppc_set_arch (abfd);
1879 /* Support for core dump NOTE sections. */
1881 static bool
1882 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1884 size_t offset, size;
1886 if (note->descsz != 504)
1887 return false;
1889 /* pr_cursig */
1890 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1892 /* pr_pid */
1893 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1895 /* pr_reg */
1896 offset = 112;
1897 size = 384;
1899 /* Make a ".reg/999" section. */
1900 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1901 size, note->descpos + offset);
1904 static bool
1905 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1907 if (note->descsz != 136)
1908 return false;
1910 elf_tdata (abfd)->core->pid
1911 = bfd_get_32 (abfd, note->descdata + 24);
1912 elf_tdata (abfd)->core->program
1913 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1914 elf_tdata (abfd)->core->command
1915 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1917 return true;
1920 static char *
1921 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1922 ...)
1924 switch (note_type)
1926 default:
1927 return NULL;
1929 case NT_PRPSINFO:
1931 char data[136] ATTRIBUTE_NONSTRING;
1932 va_list ap;
1934 va_start (ap, note_type);
1935 memset (data, 0, sizeof (data));
1936 strncpy (data + 40, va_arg (ap, const char *), 16);
1937 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1938 DIAGNOSTIC_PUSH;
1939 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1940 -Wstringop-truncation:
1941 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1943 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1944 #endif
1945 strncpy (data + 56, va_arg (ap, const char *), 80);
1946 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1947 DIAGNOSTIC_POP;
1948 #endif
1949 va_end (ap);
1950 return elfcore_write_note (abfd, buf, bufsiz,
1951 "CORE", note_type, data, sizeof (data));
1954 case NT_PRSTATUS:
1956 char data[504];
1957 va_list ap;
1958 long pid;
1959 int cursig;
1960 const void *greg;
1962 va_start (ap, note_type);
1963 memset (data, 0, 112);
1964 pid = va_arg (ap, long);
1965 bfd_put_32 (abfd, pid, data + 32);
1966 cursig = va_arg (ap, int);
1967 bfd_put_16 (abfd, cursig, data + 12);
1968 greg = va_arg (ap, const void *);
1969 memcpy (data + 112, greg, 384);
1970 memset (data + 496, 0, 8);
1971 va_end (ap);
1972 return elfcore_write_note (abfd, buf, bufsiz,
1973 "CORE", note_type, data, sizeof (data));
1978 /* Add extra PPC sections. */
1980 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1982 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1983 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1984 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1985 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1986 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1987 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1988 { NULL, 0, 0, 0, 0 }
1991 enum _ppc64_sec_type {
1992 sec_normal = 0,
1993 sec_opd = 1,
1994 sec_toc = 2
1997 struct _ppc64_elf_section_data
1999 struct bfd_elf_section_data elf;
2001 union
2003 /* An array with one entry for each opd function descriptor,
2004 and some spares since opd entries may be either 16 or 24 bytes. */
2005 #define OPD_NDX(OFF) ((OFF) >> 4)
2006 struct _opd_sec_data
2008 /* Points to the function code section for local opd entries. */
2009 asection **func_sec;
2011 /* After editing .opd, adjust references to opd local syms. */
2012 long *adjust;
2013 } opd;
2015 /* An array for toc sections, indexed by offset/8. */
2016 struct _toc_sec_data
2018 /* Specifies the relocation symbol index used at a given toc offset. */
2019 unsigned *symndx;
2021 /* And the relocation addend. */
2022 bfd_vma *add;
2023 } toc;
2024 } u;
2026 enum _ppc64_sec_type sec_type:2;
2028 /* Flag set when small branches are detected. Used to
2029 select suitable defaults for the stub group size. */
2030 unsigned int has_14bit_branch:1;
2032 /* Flag set when PLTCALL relocs are detected. */
2033 unsigned int has_pltcall:1;
2035 /* Flag set when section has PLT/GOT/TOC relocations that can be
2036 optimised. */
2037 unsigned int has_optrel:1;
2040 #define ppc64_elf_section_data(sec) \
2041 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2043 static bool
2044 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2046 if (!sec->used_by_bfd)
2048 struct _ppc64_elf_section_data *sdata;
2049 size_t amt = sizeof (*sdata);
2051 sdata = bfd_zalloc (abfd, amt);
2052 if (sdata == NULL)
2053 return false;
2054 sec->used_by_bfd = sdata;
2057 return _bfd_elf_new_section_hook (abfd, sec);
2060 static bool
2061 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2063 const char *name = hdr->bfd_section->name;
2065 if (startswith (name, ".sbss")
2066 || startswith (name, ".sdata"))
2067 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2069 return true;
2072 static struct _opd_sec_data *
2073 get_opd_info (asection * sec)
2075 if (sec != NULL
2076 && ppc64_elf_section_data (sec) != NULL
2077 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2078 return &ppc64_elf_section_data (sec)->u.opd;
2079 return NULL;
2082 /* Parameters for the qsort hook. */
2083 static bool synthetic_relocatable;
2084 static const asection *synthetic_opd;
2086 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2088 static int
2089 compare_symbols (const void *ap, const void *bp)
2091 const asymbol *a = *(const asymbol **) ap;
2092 const asymbol *b = *(const asymbol **) bp;
2094 /* Section symbols first. */
2095 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2096 return -1;
2097 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2098 return 1;
2100 /* then .opd symbols. */
2101 if (synthetic_opd != NULL)
2103 if (strcmp (a->section->name, ".opd") == 0
2104 && strcmp (b->section->name, ".opd") != 0)
2105 return -1;
2106 if (strcmp (a->section->name, ".opd") != 0
2107 && strcmp (b->section->name, ".opd") == 0)
2108 return 1;
2111 /* then other code symbols. */
2112 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2113 == (SEC_CODE | SEC_ALLOC))
2114 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2115 != (SEC_CODE | SEC_ALLOC)))
2116 return -1;
2118 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2119 != (SEC_CODE | SEC_ALLOC))
2120 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2121 == (SEC_CODE | SEC_ALLOC)))
2122 return 1;
2124 if (synthetic_relocatable)
2126 if (a->section->id < b->section->id)
2127 return -1;
2129 if (a->section->id > b->section->id)
2130 return 1;
2133 if (a->value + a->section->vma < b->value + b->section->vma)
2134 return -1;
2136 if (a->value + a->section->vma > b->value + b->section->vma)
2137 return 1;
2139 /* For syms with the same value, prefer strong dynamic global function
2140 syms over other syms. */
2141 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2142 return -1;
2144 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2145 return 1;
2147 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2148 return -1;
2150 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2151 return 1;
2153 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2154 return -1;
2156 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2157 return 1;
2159 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2160 return -1;
2162 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2163 return 1;
2165 /* Finally, sort on where the symbol is in memory. The symbols will
2166 be in at most two malloc'd blocks, one for static syms, one for
2167 dynamic syms, and we distinguish the two blocks above by testing
2168 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2169 originally in the same order as the symbols (and we're not
2170 sorting the symbols themselves), this ensures a stable sort. */
2171 if (a < b)
2172 return -1;
2173 if (a > b)
2174 return 1;
2175 return 0;
2178 /* Search SYMS for a symbol of the given VALUE. */
2180 static asymbol *
2181 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2182 bfd_vma value)
2184 size_t mid;
2186 if (id == (unsigned) -1)
2188 while (lo < hi)
2190 mid = (lo + hi) >> 1;
2191 if (syms[mid]->value + syms[mid]->section->vma < value)
2192 lo = mid + 1;
2193 else if (syms[mid]->value + syms[mid]->section->vma > value)
2194 hi = mid;
2195 else
2196 return syms[mid];
2199 else
2201 while (lo < hi)
2203 mid = (lo + hi) >> 1;
2204 if (syms[mid]->section->id < id)
2205 lo = mid + 1;
2206 else if (syms[mid]->section->id > id)
2207 hi = mid;
2208 else if (syms[mid]->value < value)
2209 lo = mid + 1;
2210 else if (syms[mid]->value > value)
2211 hi = mid;
2212 else
2213 return syms[mid];
2216 return NULL;
2219 static bool
2220 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2222 bfd_vma vma = *(bfd_vma *) ptr;
2223 return ((section->flags & SEC_ALLOC) != 0
2224 && section->vma <= vma
2225 && vma < section->vma + section->size);
2228 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2229 entry syms. Also generate @plt symbols for the glink branch table.
2230 Returns count of synthetic symbols in RET or -1 on error. */
2232 static long
2233 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2234 long static_count, asymbol **static_syms,
2235 long dyn_count, asymbol **dyn_syms,
2236 asymbol **ret)
2238 asymbol *s;
2239 size_t i, j, count;
2240 char *names;
2241 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2242 asection *opd = NULL;
2243 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2244 asymbol **syms;
2245 int abi = abiversion (abfd);
2247 *ret = NULL;
2249 if (abi < 2)
2251 opd = bfd_get_section_by_name (abfd, ".opd");
2252 if (opd == NULL && abi == 1)
2253 return 0;
2256 syms = NULL;
2257 codesecsym = 0;
2258 codesecsymend = 0;
2259 secsymend = 0;
2260 opdsymend = 0;
2261 symcount = 0;
2262 if (opd != NULL)
2264 symcount = static_count;
2265 if (!relocatable)
2266 symcount += dyn_count;
2267 if (symcount == 0)
2268 return 0;
2270 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2271 if (syms == NULL)
2272 return -1;
2274 if (!relocatable && static_count != 0 && dyn_count != 0)
2276 /* Use both symbol tables. */
2277 memcpy (syms, static_syms, static_count * sizeof (*syms));
2278 memcpy (syms + static_count, dyn_syms,
2279 (dyn_count + 1) * sizeof (*syms));
2281 else if (!relocatable && static_count == 0)
2282 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2283 else
2284 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2286 /* Trim uninteresting symbols. Interesting symbols are section,
2287 function, and notype symbols. */
2288 for (i = 0, j = 0; i < symcount; ++i)
2289 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2290 | BSF_RELC | BSF_SRELC)) == 0)
2291 syms[j++] = syms[i];
2292 symcount = j;
2294 synthetic_relocatable = relocatable;
2295 synthetic_opd = opd;
2296 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2298 if (!relocatable && symcount > 1)
2300 /* Trim duplicate syms, since we may have merged the normal
2301 and dynamic symbols. Actually, we only care about syms
2302 that have different values, so trim any with the same
2303 value. Don't consider ifunc and ifunc resolver symbols
2304 duplicates however, because GDB wants to know whether a
2305 text symbol is an ifunc resolver. */
2306 for (i = 1, j = 1; i < symcount; ++i)
2308 const asymbol *s0 = syms[i - 1];
2309 const asymbol *s1 = syms[i];
2311 if ((s0->value + s0->section->vma
2312 != s1->value + s1->section->vma)
2313 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2314 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2315 syms[j++] = syms[i];
2317 symcount = j;
2320 i = 0;
2321 /* Note that here and in compare_symbols we can't compare opd and
2322 sym->section directly. With separate debug info files, the
2323 symbols will be extracted from the debug file while abfd passed
2324 to this function is the real binary. */
2325 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2326 && strcmp (syms[i]->section->name, ".opd") == 0)
2327 ++i;
2328 codesecsym = i;
2330 for (; i < symcount; ++i)
2331 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2332 | SEC_THREAD_LOCAL))
2333 != (SEC_CODE | SEC_ALLOC))
2334 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2335 break;
2336 codesecsymend = i;
2338 for (; i < symcount; ++i)
2339 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2340 break;
2341 secsymend = i;
2343 for (; i < symcount; ++i)
2344 if (strcmp (syms[i]->section->name, ".opd") != 0)
2345 break;
2346 opdsymend = i;
2348 for (; i < symcount; ++i)
2349 if (((syms[i]->section->flags
2350 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2351 != (SEC_CODE | SEC_ALLOC))
2352 break;
2353 symcount = i;
2355 count = 0;
2357 if (relocatable)
2359 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2360 arelent *r;
2361 size_t size;
2362 size_t relcount;
2364 if (opdsymend == secsymend)
2365 goto done;
2367 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2368 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2369 if (relcount == 0)
2370 goto done;
2372 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2374 count = -1;
2375 goto done;
2378 size = 0;
2379 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2381 asymbol *sym;
2383 while (r < opd->relocation + relcount
2384 && r->address < syms[i]->value + opd->vma)
2385 ++r;
2387 if (r == opd->relocation + relcount)
2388 break;
2390 if (r->address != syms[i]->value + opd->vma)
2391 continue;
2393 if (r->howto->type != R_PPC64_ADDR64)
2394 continue;
2396 sym = *r->sym_ptr_ptr;
2397 if (!sym_exists_at (syms, opdsymend, symcount,
2398 sym->section->id, sym->value + r->addend))
2400 ++count;
2401 size += sizeof (asymbol);
2402 size += strlen (syms[i]->name) + 2;
2406 if (size == 0)
2407 goto done;
2408 s = *ret = bfd_malloc (size);
2409 if (s == NULL)
2411 count = -1;
2412 goto done;
2415 names = (char *) (s + count);
2417 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2419 asymbol *sym;
2421 while (r < opd->relocation + relcount
2422 && r->address < syms[i]->value + opd->vma)
2423 ++r;
2425 if (r == opd->relocation + relcount)
2426 break;
2428 if (r->address != syms[i]->value + opd->vma)
2429 continue;
2431 if (r->howto->type != R_PPC64_ADDR64)
2432 continue;
2434 sym = *r->sym_ptr_ptr;
2435 if (!sym_exists_at (syms, opdsymend, symcount,
2436 sym->section->id, sym->value + r->addend))
2438 size_t len;
2440 *s = *syms[i];
2441 s->flags |= BSF_SYNTHETIC;
2442 s->section = sym->section;
2443 s->value = sym->value + r->addend;
2444 s->name = names;
2445 *names++ = '.';
2446 len = strlen (syms[i]->name);
2447 memcpy (names, syms[i]->name, len + 1);
2448 names += len + 1;
2449 /* Have udata.p point back to the original symbol this
2450 synthetic symbol was derived from. */
2451 s->udata.p = syms[i];
2452 s++;
2456 else
2458 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2459 bfd_byte *contents = NULL;
2460 size_t size;
2461 size_t plt_count = 0;
2462 bfd_vma glink_vma = 0, resolv_vma = 0;
2463 asection *dynamic, *glink = NULL, *relplt = NULL;
2464 arelent *p;
2466 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2468 free_contents_and_exit_err:
2469 count = -1;
2470 free_contents_and_exit:
2471 free (contents);
2472 goto done;
2475 size = 0;
2476 for (i = secsymend; i < opdsymend; ++i)
2478 bfd_vma ent;
2480 /* Ignore bogus symbols. */
2481 if (syms[i]->value > opd->size - 8)
2482 continue;
2484 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2485 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2487 ++count;
2488 size += sizeof (asymbol);
2489 size += strlen (syms[i]->name) + 2;
2493 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2494 if (dyn_count != 0
2495 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2497 bfd_byte *dynbuf, *extdyn, *extdynend;
2498 size_t extdynsize;
2499 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2501 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2502 goto free_contents_and_exit_err;
2504 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2505 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2507 extdyn = dynbuf;
2508 extdynend = extdyn + dynamic->size;
2509 for (; extdyn < extdynend; extdyn += extdynsize)
2511 Elf_Internal_Dyn dyn;
2512 (*swap_dyn_in) (abfd, extdyn, &dyn);
2514 if (dyn.d_tag == DT_NULL)
2515 break;
2517 if (dyn.d_tag == DT_PPC64_GLINK)
2519 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2520 See comment in ppc64_elf_finish_dynamic_sections. */
2521 glink_vma = dyn.d_un.d_val + 8 * 4;
2522 /* The .glink section usually does not survive the final
2523 link; search for the section (usually .text) where the
2524 glink stubs now reside. */
2525 glink = bfd_sections_find_if (abfd, section_covers_vma,
2526 &glink_vma);
2527 break;
2531 free (dynbuf);
2534 if (glink != NULL)
2536 /* Determine __glink trampoline by reading the relative branch
2537 from the first glink stub. */
2538 bfd_byte buf[4];
2539 unsigned int off = 0;
2541 while (bfd_get_section_contents (abfd, glink, buf,
2542 glink_vma + off - glink->vma, 4))
2544 unsigned int insn = bfd_get_32 (abfd, buf);
2545 insn ^= B_DOT;
2546 if ((insn & ~0x3fffffc) == 0)
2548 resolv_vma
2549 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2550 break;
2552 off += 4;
2553 if (off > 4)
2554 break;
2557 if (resolv_vma)
2558 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2560 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2561 if (relplt != NULL)
2563 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2564 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2565 goto free_contents_and_exit_err;
2567 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2568 size += plt_count * sizeof (asymbol);
2570 p = relplt->relocation;
2571 for (i = 0; i < plt_count; i++, p++)
2573 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2574 if (p->addend != 0)
2575 size += sizeof ("+0x") - 1 + 16;
2580 if (size == 0)
2581 goto free_contents_and_exit;
2582 s = *ret = bfd_malloc (size);
2583 if (s == NULL)
2584 goto free_contents_and_exit_err;
2586 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2588 for (i = secsymend; i < opdsymend; ++i)
2590 bfd_vma ent;
2592 if (syms[i]->value > opd->size - 8)
2593 continue;
2595 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2596 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2598 size_t lo, hi;
2599 size_t len;
2600 asection *sec = abfd->sections;
2602 *s = *syms[i];
2603 lo = codesecsym;
2604 hi = codesecsymend;
2605 while (lo < hi)
2607 size_t mid = (lo + hi) >> 1;
2608 if (syms[mid]->section->vma < ent)
2609 lo = mid + 1;
2610 else if (syms[mid]->section->vma > ent)
2611 hi = mid;
2612 else
2614 sec = syms[mid]->section;
2615 break;
2619 if (lo >= hi && lo > codesecsym)
2620 sec = syms[lo - 1]->section;
2622 for (; sec != NULL; sec = sec->next)
2624 if (sec->vma > ent)
2625 break;
2626 /* SEC_LOAD may not be set if SEC is from a separate debug
2627 info file. */
2628 if ((sec->flags & SEC_ALLOC) == 0)
2629 break;
2630 if ((sec->flags & SEC_CODE) != 0)
2631 s->section = sec;
2633 s->flags |= BSF_SYNTHETIC;
2634 s->value = ent - s->section->vma;
2635 s->name = names;
2636 *names++ = '.';
2637 len = strlen (syms[i]->name);
2638 memcpy (names, syms[i]->name, len + 1);
2639 names += len + 1;
2640 /* Have udata.p point back to the original symbol this
2641 synthetic symbol was derived from. */
2642 s->udata.p = syms[i];
2643 s++;
2646 free (contents);
2648 if (glink != NULL && relplt != NULL)
2650 if (resolv_vma)
2652 /* Add a symbol for the main glink trampoline. */
2653 memset (s, 0, sizeof *s);
2654 s->the_bfd = abfd;
2655 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2656 s->section = glink;
2657 s->value = resolv_vma - glink->vma;
2658 s->name = names;
2659 memcpy (names, "__glink_PLTresolve",
2660 sizeof ("__glink_PLTresolve"));
2661 names += sizeof ("__glink_PLTresolve");
2662 s++;
2663 count++;
2666 /* FIXME: It would be very much nicer to put sym@plt on the
2667 stub rather than on the glink branch table entry. The
2668 objdump disassembler would then use a sensible symbol
2669 name on plt calls. The difficulty in doing so is
2670 a) finding the stubs, and,
2671 b) matching stubs against plt entries, and,
2672 c) there can be multiple stubs for a given plt entry.
2674 Solving (a) could be done by code scanning, but older
2675 ppc64 binaries used different stubs to current code.
2676 (b) is the tricky one since you need to known the toc
2677 pointer for at least one function that uses a pic stub to
2678 be able to calculate the plt address referenced.
2679 (c) means gdb would need to set multiple breakpoints (or
2680 find the glink branch itself) when setting breakpoints
2681 for pending shared library loads. */
2682 p = relplt->relocation;
2683 for (i = 0; i < plt_count; i++, p++)
2685 size_t len;
2687 *s = **p->sym_ptr_ptr;
2688 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2689 we are defining a symbol, ensure one of them is set. */
2690 if ((s->flags & BSF_LOCAL) == 0)
2691 s->flags |= BSF_GLOBAL;
2692 s->flags |= BSF_SYNTHETIC;
2693 s->section = glink;
2694 s->value = glink_vma - glink->vma;
2695 s->name = names;
2696 s->udata.p = NULL;
2697 len = strlen ((*p->sym_ptr_ptr)->name);
2698 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2699 names += len;
2700 if (p->addend != 0)
2702 memcpy (names, "+0x", sizeof ("+0x") - 1);
2703 names += sizeof ("+0x") - 1;
2704 bfd_sprintf_vma (abfd, names, p->addend);
2705 names += strlen (names);
2707 memcpy (names, "@plt", sizeof ("@plt"));
2708 names += sizeof ("@plt");
2709 s++;
2710 if (abi < 2)
2712 glink_vma += 8;
2713 if (i >= 0x8000)
2714 glink_vma += 4;
2716 else
2717 glink_vma += 4;
2719 count += plt_count;
2723 done:
2724 free (syms);
2725 return count;
2728 /* The following functions are specific to the ELF linker, while
2729 functions above are used generally. Those named ppc64_elf_* are
2730 called by the main ELF linker code. They appear in this file more
2731 or less in the order in which they are called. eg.
2732 ppc64_elf_check_relocs is called early in the link process,
2733 ppc64_elf_finish_dynamic_sections is one of the last functions
2734 called.
2736 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2737 functions have both a function code symbol and a function descriptor
2738 symbol. A call to foo in a relocatable object file looks like:
2740 . .text
2741 . x:
2742 . bl .foo
2743 . nop
2745 The function definition in another object file might be:
2747 . .section .opd
2748 . foo: .quad .foo
2749 . .quad .TOC.@tocbase
2750 . .quad 0
2752 . .text
2753 . .foo: blr
2755 When the linker resolves the call during a static link, the branch
2756 unsurprisingly just goes to .foo and the .opd information is unused.
2757 If the function definition is in a shared library, things are a little
2758 different: The call goes via a plt call stub, the opd information gets
2759 copied to the plt, and the linker patches the nop.
2761 . x:
2762 . bl .foo_stub
2763 . ld 2,40(1)
2766 . .foo_stub:
2767 . std 2,40(1) # in practice, the call stub
2768 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2769 . addi 11,11,Lfoo@toc@l # this is the general idea
2770 . ld 12,0(11)
2771 . ld 2,8(11)
2772 . mtctr 12
2773 . ld 11,16(11)
2774 . bctr
2776 . .section .plt
2777 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2779 The "reloc ()" notation is supposed to indicate that the linker emits
2780 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2781 copying.
2783 What are the difficulties here? Well, firstly, the relocations
2784 examined by the linker in check_relocs are against the function code
2785 sym .foo, while the dynamic relocation in the plt is emitted against
2786 the function descriptor symbol, foo. Somewhere along the line, we need
2787 to carefully copy dynamic link information from one symbol to the other.
2788 Secondly, the generic part of the elf linker will make .foo a dynamic
2789 symbol as is normal for most other backends. We need foo dynamic
2790 instead, at least for an application final link. However, when
2791 creating a shared library containing foo, we need to have both symbols
2792 dynamic so that references to .foo are satisfied during the early
2793 stages of linking. Otherwise the linker might decide to pull in a
2794 definition from some other object, eg. a static library.
2796 Update: As of August 2004, we support a new convention. Function
2797 calls may use the function descriptor symbol, ie. "bl foo". This
2798 behaves exactly as "bl .foo". */
2800 /* Of those relocs that might be copied as dynamic relocs, this
2801 function selects those that must be copied when linking a shared
2802 library or PIE, even when the symbol is local. */
2804 static int
2805 must_be_dyn_reloc (struct bfd_link_info *info,
2806 enum elf_ppc64_reloc_type r_type)
2808 switch (r_type)
2810 default:
2811 /* Only relative relocs can be resolved when the object load
2812 address isn't fixed. DTPREL64 is excluded because the
2813 dynamic linker needs to differentiate global dynamic from
2814 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2815 return 1;
2817 case R_PPC64_REL32:
2818 case R_PPC64_REL64:
2819 case R_PPC64_REL30:
2820 case R_PPC64_TOC16:
2821 case R_PPC64_TOC16_DS:
2822 case R_PPC64_TOC16_LO:
2823 case R_PPC64_TOC16_HI:
2824 case R_PPC64_TOC16_HA:
2825 case R_PPC64_TOC16_LO_DS:
2826 return 0;
2828 case R_PPC64_TPREL16:
2829 case R_PPC64_TPREL16_LO:
2830 case R_PPC64_TPREL16_HI:
2831 case R_PPC64_TPREL16_HA:
2832 case R_PPC64_TPREL16_DS:
2833 case R_PPC64_TPREL16_LO_DS:
2834 case R_PPC64_TPREL16_HIGH:
2835 case R_PPC64_TPREL16_HIGHA:
2836 case R_PPC64_TPREL16_HIGHER:
2837 case R_PPC64_TPREL16_HIGHERA:
2838 case R_PPC64_TPREL16_HIGHEST:
2839 case R_PPC64_TPREL16_HIGHESTA:
2840 case R_PPC64_TPREL64:
2841 case R_PPC64_TPREL34:
2842 /* These relocations are relative but in a shared library the
2843 linker doesn't know the thread pointer base. */
2844 return bfd_link_dll (info);
2848 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2849 copying dynamic variables from a shared lib into an app's .dynbss
2850 section, and instead use a dynamic relocation to point into the
2851 shared lib. With code that gcc generates it is vital that this be
2852 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2853 actually the address of a function descriptor which resides in the
2854 .opd section. gcc uses the descriptor directly rather than going
2855 via the GOT as some other ABIs do, which means that initialized
2856 function pointers reference the descriptor. Thus, a function
2857 pointer initialized to the address of a function in a shared
2858 library will either require a .dynbss copy and a copy reloc, or a
2859 dynamic reloc. Using a .dynbss copy redefines the function
2860 descriptor symbol to point to the copy. This presents a problem as
2861 a PLT entry for that function is also initialized from the function
2862 descriptor symbol and the copy may not be initialized first. */
2863 #define ELIMINATE_COPY_RELOCS 1
2865 /* Section name for stubs is the associated section name plus this
2866 string. */
2867 #define STUB_SUFFIX ".stub"
2869 /* Linker stubs.
2870 ppc_stub_long_branch:
2871 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2872 destination, but a 24 bit branch in a stub section will reach.
2873 . b dest
2875 ppc_stub_plt_branch:
2876 Similar to the above, but a 24 bit branch in the stub section won't
2877 reach its destination.
2878 . addis %r12,%r2,xxx@toc@ha
2879 . ld %r12,xxx@toc@l(%r12)
2880 . mtctr %r12
2881 . bctr
2883 ppc_stub_plt_call:
2884 Used to call a function in a shared library. If it so happens that
2885 the plt entry referenced crosses a 64k boundary, then an extra
2886 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2887 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2888 . addis %r11,%r2,xxx@toc@ha
2889 . ld %r12,xxx+0@toc@l(%r11)
2890 . mtctr %r12
2891 . ld %r2,xxx+8@toc@l(%r11)
2892 . ld %r11,xxx+16@toc@l(%r11)
2893 . bctr
2895 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2896 code to adjust the value and save r2 to support multiple toc sections.
2897 A ppc_stub_long_branch with an r2 offset looks like:
2898 . std %r2,40(%r1)
2899 . addis %r2,%r2,off@ha
2900 . addi %r2,%r2,off@l
2901 . b dest
2903 A ppc_stub_plt_branch with an r2 offset looks like:
2904 . std %r2,40(%r1)
2905 . addis %r12,%r2,xxx@toc@ha
2906 . ld %r12,xxx@toc@l(%r12)
2907 . addis %r2,%r2,off@ha
2908 . addi %r2,%r2,off@l
2909 . mtctr %r12
2910 . bctr
2912 All of the above stubs are shown as their ELFv1 variants. ELFv2
2913 variants exist too, simpler for plt calls since a new toc pointer
2914 and static chain are not loaded by the stub. In addition, ELFv2
2915 has some more complex stubs to handle calls marked with NOTOC
2916 relocs from functions where r2 is not a valid toc pointer. These
2917 come in two flavours, the ones shown below, and _both variants that
2918 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2919 one call is from a function where r2 is used as the toc pointer but
2920 needs a toc adjusting stub for small-model multi-toc, and another
2921 call is from a function where r2 is not valid.
2922 ppc_stub_long_branch_notoc:
2923 . mflr %r12
2924 . bcl 20,31,1f
2925 . 1:
2926 . mflr %r11
2927 . mtlr %r12
2928 . addis %r12,%r11,dest-1b@ha
2929 . addi %r12,%r12,dest-1b@l
2930 . b dest
2932 ppc_stub_plt_branch_notoc:
2933 . mflr %r12
2934 . bcl 20,31,1f
2935 . 1:
2936 . mflr %r11
2937 . mtlr %r12
2938 . lis %r12,xxx-1b@highest
2939 . ori %r12,%r12,xxx-1b@higher
2940 . sldi %r12,%r12,32
2941 . oris %r12,%r12,xxx-1b@high
2942 . ori %r12,%r12,xxx-1b@l
2943 . add %r12,%r11,%r12
2944 . mtctr %r12
2945 . bctr
2947 ppc_stub_plt_call_notoc:
2948 . mflr %r12
2949 . bcl 20,31,1f
2950 . 1:
2951 . mflr %r11
2952 . mtlr %r12
2953 . lis %r12,xxx-1b@highest
2954 . ori %r12,%r12,xxx-1b@higher
2955 . sldi %r12,%r12,32
2956 . oris %r12,%r12,xxx-1b@high
2957 . ori %r12,%r12,xxx-1b@l
2958 . ldx %r12,%r11,%r12
2959 . mtctr %r12
2960 . bctr
2962 There are also ELFv1 power10 variants of these stubs.
2963 ppc_stub_long_branch_notoc:
2964 . pla %r12,dest@pcrel
2965 . b dest
2966 ppc_stub_plt_branch_notoc:
2967 . lis %r11,(dest-1f)@highesta34
2968 . ori %r11,%r11,(dest-1f)@highera34
2969 . sldi %r11,%r11,34
2970 . 1: pla %r12,dest@pcrel
2971 . add %r12,%r11,%r12
2972 . mtctr %r12
2973 . bctr
2974 ppc_stub_plt_call_notoc:
2975 . lis %r11,(xxx-1f)@highesta34
2976 . ori %r11,%r11,(xxx-1f)@highera34
2977 . sldi %r11,%r11,34
2978 . 1: pla %r12,xxx@pcrel
2979 . ldx %r12,%r11,%r12
2980 . mtctr %r12
2981 . bctr
2983 In cases where the high instructions would add zero, they are
2984 omitted and following instructions modified in some cases.
2985 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2987 . pld %r12,xxx@pcrel
2988 . mtctr %r12
2989 . bctr
2991 For a given stub group (a set of sections all using the same toc
2992 pointer value) there will be just one stub type used for any
2993 particular function symbol. For example, if printf is called from
2994 code with the tocsave optimization (ie. r2 saved in function
2995 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2996 and from other code without the tocsave optimization requiring a
2997 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2998 type will be created. Calls with the tocsave optimization will
2999 enter this stub after the instruction saving r2. A similar
3000 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3001 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3002 to call an external function like printf. If other calls to printf
3003 require a ppc_stub_plt_call linkage stub then a single
3004 ppc_stub_plt_call_notoc linkage stub will be used for both types of
3005 call. If other calls to printf require a ppc_stub_plt_call_r2save
3006 linkage stub then a single ppc_stub_plt_call_both linkage stub will
3007 be created and calls not requiring r2 to be saved will enter the
3008 stub after the r2 save instruction. There is an analogous
3009 hierarchy of long branch and plt branch stubs for local call
3010 linkage. */
3012 enum ppc_stub_type
3014 ppc_stub_none,
3015 ppc_stub_long_branch,
3016 ppc_stub_long_branch_r2off,
3017 ppc_stub_long_branch_notoc,
3018 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
3019 ppc_stub_plt_branch,
3020 ppc_stub_plt_branch_r2off,
3021 ppc_stub_plt_branch_notoc,
3022 ppc_stub_plt_branch_both,
3023 ppc_stub_plt_call,
3024 ppc_stub_plt_call_r2save,
3025 ppc_stub_plt_call_notoc,
3026 ppc_stub_plt_call_both,
3027 ppc_stub_global_entry,
3028 ppc_stub_save_res
3031 /* Information on stub grouping. */
3032 struct map_stub
3034 /* The stub section. */
3035 asection *stub_sec;
3036 /* This is the section to which stubs in the group will be attached. */
3037 asection *link_sec;
3038 /* Next group. */
3039 struct map_stub *next;
3040 /* Whether to emit a copy of register save/restore functions in this
3041 group. */
3042 int needs_save_res;
3043 /* Current offset within stubs after the insn restoring lr in a
3044 _notoc or _both stub using bcl for pc-relative addressing, or
3045 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3046 unsigned int lr_restore;
3047 /* Accumulated size of EH info emitted to describe return address
3048 if stubs modify lr. Does not include 17 byte FDE header. */
3049 unsigned int eh_size;
3050 /* Offset in glink_eh_frame to the start of EH info for this group. */
3051 unsigned int eh_base;
3054 struct ppc_stub_hash_entry
3056 /* Base hash table entry structure. */
3057 struct bfd_hash_entry root;
3059 enum ppc_stub_type stub_type;
3061 /* Group information. */
3062 struct map_stub *group;
3064 /* Offset within stub_sec of the beginning of this stub. */
3065 bfd_vma stub_offset;
3067 /* Given the symbol's value and its section we can determine its final
3068 value when building the stubs (so the stub knows where to jump. */
3069 bfd_vma target_value;
3070 asection *target_section;
3072 /* The symbol table entry, if any, that this was derived from. */
3073 struct ppc_link_hash_entry *h;
3074 struct plt_entry *plt_ent;
3076 /* Symbol type. */
3077 unsigned char symtype;
3079 /* Symbol st_other. */
3080 unsigned char other;
3083 struct ppc_branch_hash_entry
3085 /* Base hash table entry structure. */
3086 struct bfd_hash_entry root;
3088 /* Offset within branch lookup table. */
3089 unsigned int offset;
3091 /* Generation marker. */
3092 unsigned int iter;
3095 /* Used to track dynamic relocations for local symbols. */
3096 struct ppc_dyn_relocs
3098 struct ppc_dyn_relocs *next;
3100 /* The input section of the reloc. */
3101 asection *sec;
3103 /* Total number of relocs copied for the input section. */
3104 unsigned int count : 31;
3106 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3107 unsigned int ifunc : 1;
3110 struct ppc_link_hash_entry
3112 struct elf_link_hash_entry elf;
3114 union
3116 /* A pointer to the most recently used stub hash entry against this
3117 symbol. */
3118 struct ppc_stub_hash_entry *stub_cache;
3120 /* A pointer to the next symbol starting with a '.' */
3121 struct ppc_link_hash_entry *next_dot_sym;
3122 } u;
3124 /* Link between function code and descriptor symbols. */
3125 struct ppc_link_hash_entry *oh;
3127 /* Flag function code and descriptor symbols. */
3128 unsigned int is_func:1;
3129 unsigned int is_func_descriptor:1;
3130 unsigned int fake:1;
3132 /* Whether global opd/toc sym has been adjusted or not.
3133 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3134 should be set for all globals defined in any opd/toc section. */
3135 unsigned int adjust_done:1;
3137 /* Set if this is an out-of-line register save/restore function,
3138 with non-standard calling convention. */
3139 unsigned int save_res:1;
3141 /* Set if a duplicate symbol with non-zero localentry is detected,
3142 even when the duplicate symbol does not provide a definition. */
3143 unsigned int non_zero_localentry:1;
3145 /* Contexts in which symbol is used in the GOT (or TOC).
3146 Bits are or'd into the mask as the corresponding relocs are
3147 encountered during check_relocs, with TLS_TLS being set when any
3148 of the other TLS bits are set. tls_optimize clears bits when
3149 optimizing to indicate the corresponding GOT entry type is not
3150 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3151 set TLS_GDIE when a GD reloc turns into an IE one.
3152 These flags are also kept for local symbols. */
3153 #define TLS_TLS 1 /* Any TLS reloc. */
3154 #define TLS_GD 2 /* GD reloc. */
3155 #define TLS_LD 4 /* LD reloc. */
3156 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3157 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3158 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3159 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3160 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3161 unsigned char tls_mask;
3163 /* The above field is also used to mark function symbols. In which
3164 case TLS_TLS will be 0. */
3165 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3166 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3167 #define NON_GOT 256 /* local symbol plt, not stored. */
3170 static inline struct ppc_link_hash_entry *
3171 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3173 return (struct ppc_link_hash_entry *) ent;
3176 static inline struct elf_link_hash_entry *
3177 elf_hash_entry (struct ppc_link_hash_entry *ent)
3179 return (struct elf_link_hash_entry *) ent;
3182 /* ppc64 ELF linker hash table. */
3184 struct ppc_link_hash_table
3186 struct elf_link_hash_table elf;
3188 /* The stub hash table. */
3189 struct bfd_hash_table stub_hash_table;
3191 /* Another hash table for plt_branch stubs. */
3192 struct bfd_hash_table branch_hash_table;
3194 /* Hash table for function prologue tocsave. */
3195 htab_t tocsave_htab;
3197 /* Various options and other info passed from the linker. */
3198 struct ppc64_elf_params *params;
3200 /* The size of sec_info below. */
3201 unsigned int sec_info_arr_size;
3203 /* Per-section array of extra section info. Done this way rather
3204 than as part of ppc64_elf_section_data so we have the info for
3205 non-ppc64 sections. */
3206 struct
3208 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3209 bfd_vma toc_off;
3211 union
3213 /* The section group that this section belongs to. */
3214 struct map_stub *group;
3215 /* A temp section list pointer. */
3216 asection *list;
3217 } u;
3218 } *sec_info;
3220 /* Linked list of groups. */
3221 struct map_stub *group;
3223 /* Temp used when calculating TOC pointers. */
3224 bfd_vma toc_curr;
3225 bfd *toc_bfd;
3226 asection *toc_first_sec;
3228 /* Used when adding symbols. */
3229 struct ppc_link_hash_entry *dot_syms;
3231 /* Shortcuts to get to dynamic linker sections. */
3232 asection *glink;
3233 asection *global_entry;
3234 asection *sfpr;
3235 asection *pltlocal;
3236 asection *relpltlocal;
3237 asection *brlt;
3238 asection *relbrlt;
3239 asection *glink_eh_frame;
3241 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3242 struct ppc_link_hash_entry *tls_get_addr;
3243 struct ppc_link_hash_entry *tls_get_addr_fd;
3244 struct ppc_link_hash_entry *tga_desc;
3245 struct ppc_link_hash_entry *tga_desc_fd;
3246 struct map_stub *tga_group;
3248 /* The size of reliplt used by got entry relocs. */
3249 bfd_size_type got_reli_size;
3251 /* Statistics. */
3252 unsigned long stub_count[ppc_stub_global_entry];
3254 /* Number of stubs against global syms. */
3255 unsigned long stub_globals;
3257 /* Set if we're linking code with function descriptors. */
3258 unsigned int opd_abi:1;
3260 /* Support for multiple toc sections. */
3261 unsigned int do_multi_toc:1;
3262 unsigned int multi_toc_needed:1;
3263 unsigned int second_toc_pass:1;
3264 unsigned int do_toc_opt:1;
3266 /* Set if tls optimization is enabled. */
3267 unsigned int do_tls_opt:1;
3269 /* Set if inline plt calls should be converted to direct calls. */
3270 unsigned int can_convert_all_inline_plt:1;
3272 /* Set on error. */
3273 unsigned int stub_error:1;
3275 /* Whether func_desc_adjust needs to be run over symbols. */
3276 unsigned int need_func_desc_adj:1;
3278 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3279 unsigned int has_plt_localentry0:1;
3281 /* Whether calls are made via the PLT from NOTOC functions. */
3282 unsigned int notoc_plt:1;
3284 /* Whether any code linked seems to be Power10. */
3285 unsigned int has_power10_relocs:1;
3287 /* Incremented every time we size stubs. */
3288 unsigned int stub_iteration;
3291 /* Rename some of the generic section flags to better document how they
3292 are used here. */
3294 /* Nonzero if this section has TLS related relocations. */
3295 #define has_tls_reloc sec_flg0
3297 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3298 relocations. */
3299 #define nomark_tls_get_addr sec_flg1
3301 /* Nonzero if this section has any toc or got relocs. */
3302 #define has_toc_reloc sec_flg2
3304 /* Nonzero if this section has a call to another section that uses
3305 the toc or got. */
3306 #define makes_toc_func_call sec_flg3
3308 /* Recursion protection when determining above flag. */
3309 #define call_check_in_progress sec_flg4
3310 #define call_check_done sec_flg5
3312 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3314 #define ppc_hash_table(p) \
3315 ((is_elf_hash_table ((p)->hash) \
3316 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3317 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3319 #define ppc_stub_hash_lookup(table, string, create, copy) \
3320 ((struct ppc_stub_hash_entry *) \
3321 bfd_hash_lookup ((table), (string), (create), (copy)))
3323 #define ppc_branch_hash_lookup(table, string, create, copy) \
3324 ((struct ppc_branch_hash_entry *) \
3325 bfd_hash_lookup ((table), (string), (create), (copy)))
3327 /* Create an entry in the stub hash table. */
3329 static struct bfd_hash_entry *
3330 stub_hash_newfunc (struct bfd_hash_entry *entry,
3331 struct bfd_hash_table *table,
3332 const char *string)
3334 /* Allocate the structure if it has not already been allocated by a
3335 subclass. */
3336 if (entry == NULL)
3338 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3339 if (entry == NULL)
3340 return entry;
3343 /* Call the allocation method of the superclass. */
3344 entry = bfd_hash_newfunc (entry, table, string);
3345 if (entry != NULL)
3347 struct ppc_stub_hash_entry *eh;
3349 /* Initialize the local fields. */
3350 eh = (struct ppc_stub_hash_entry *) entry;
3351 eh->stub_type = ppc_stub_none;
3352 eh->group = NULL;
3353 eh->stub_offset = 0;
3354 eh->target_value = 0;
3355 eh->target_section = NULL;
3356 eh->h = NULL;
3357 eh->plt_ent = NULL;
3358 eh->other = 0;
3361 return entry;
3364 /* Create an entry in the branch hash table. */
3366 static struct bfd_hash_entry *
3367 branch_hash_newfunc (struct bfd_hash_entry *entry,
3368 struct bfd_hash_table *table,
3369 const char *string)
3371 /* Allocate the structure if it has not already been allocated by a
3372 subclass. */
3373 if (entry == NULL)
3375 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3376 if (entry == NULL)
3377 return entry;
3380 /* Call the allocation method of the superclass. */
3381 entry = bfd_hash_newfunc (entry, table, string);
3382 if (entry != NULL)
3384 struct ppc_branch_hash_entry *eh;
3386 /* Initialize the local fields. */
3387 eh = (struct ppc_branch_hash_entry *) entry;
3388 eh->offset = 0;
3389 eh->iter = 0;
3392 return entry;
3395 /* Create an entry in a ppc64 ELF linker hash table. */
3397 static struct bfd_hash_entry *
3398 link_hash_newfunc (struct bfd_hash_entry *entry,
3399 struct bfd_hash_table *table,
3400 const char *string)
3402 /* Allocate the structure if it has not already been allocated by a
3403 subclass. */
3404 if (entry == NULL)
3406 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3407 if (entry == NULL)
3408 return entry;
3411 /* Call the allocation method of the superclass. */
3412 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3413 if (entry != NULL)
3415 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3417 memset (&eh->u.stub_cache, 0,
3418 (sizeof (struct ppc_link_hash_entry)
3419 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3421 /* When making function calls, old ABI code references function entry
3422 points (dot symbols), while new ABI code references the function
3423 descriptor symbol. We need to make any combination of reference and
3424 definition work together, without breaking archive linking.
3426 For a defined function "foo" and an undefined call to "bar":
3427 An old object defines "foo" and ".foo", references ".bar" (possibly
3428 "bar" too).
3429 A new object defines "foo" and references "bar".
3431 A new object thus has no problem with its undefined symbols being
3432 satisfied by definitions in an old object. On the other hand, the
3433 old object won't have ".bar" satisfied by a new object.
3435 Keep a list of newly added dot-symbols. */
3437 if (string[0] == '.')
3439 struct ppc_link_hash_table *htab;
3441 htab = (struct ppc_link_hash_table *) table;
3442 eh->u.next_dot_sym = htab->dot_syms;
3443 htab->dot_syms = eh;
3447 return entry;
3450 struct tocsave_entry
3452 asection *sec;
3453 bfd_vma offset;
3456 static hashval_t
3457 tocsave_htab_hash (const void *p)
3459 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3460 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3463 static int
3464 tocsave_htab_eq (const void *p1, const void *p2)
3466 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3467 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3468 return e1->sec == e2->sec && e1->offset == e2->offset;
3471 /* Destroy a ppc64 ELF linker hash table. */
3473 static void
3474 ppc64_elf_link_hash_table_free (bfd *obfd)
3476 struct ppc_link_hash_table *htab;
3478 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3479 if (htab->tocsave_htab)
3480 htab_delete (htab->tocsave_htab);
3481 bfd_hash_table_free (&htab->branch_hash_table);
3482 bfd_hash_table_free (&htab->stub_hash_table);
3483 _bfd_elf_link_hash_table_free (obfd);
3486 /* Create a ppc64 ELF linker hash table. */
3488 static struct bfd_link_hash_table *
3489 ppc64_elf_link_hash_table_create (bfd *abfd)
3491 struct ppc_link_hash_table *htab;
3492 size_t amt = sizeof (struct ppc_link_hash_table);
3494 htab = bfd_zmalloc (amt);
3495 if (htab == NULL)
3496 return NULL;
3498 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3499 sizeof (struct ppc_link_hash_entry),
3500 PPC64_ELF_DATA))
3502 free (htab);
3503 return NULL;
3506 /* Init the stub hash table too. */
3507 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3508 sizeof (struct ppc_stub_hash_entry)))
3510 _bfd_elf_link_hash_table_free (abfd);
3511 return NULL;
3514 /* And the branch hash table. */
3515 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3516 sizeof (struct ppc_branch_hash_entry)))
3518 bfd_hash_table_free (&htab->stub_hash_table);
3519 _bfd_elf_link_hash_table_free (abfd);
3520 return NULL;
3523 htab->tocsave_htab = htab_try_create (1024,
3524 tocsave_htab_hash,
3525 tocsave_htab_eq,
3526 NULL);
3527 if (htab->tocsave_htab == NULL)
3529 ppc64_elf_link_hash_table_free (abfd);
3530 return NULL;
3532 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3534 /* Initializing two fields of the union is just cosmetic. We really
3535 only care about glist, but when compiled on a 32-bit host the
3536 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3537 debugger inspection of these fields look nicer. */
3538 htab->elf.init_got_refcount.refcount = 0;
3539 htab->elf.init_got_refcount.glist = NULL;
3540 htab->elf.init_plt_refcount.refcount = 0;
3541 htab->elf.init_plt_refcount.glist = NULL;
3542 htab->elf.init_got_offset.offset = 0;
3543 htab->elf.init_got_offset.glist = NULL;
3544 htab->elf.init_plt_offset.offset = 0;
3545 htab->elf.init_plt_offset.glist = NULL;
3547 return &htab->elf.root;
3550 /* Create sections for linker generated code. */
3552 static bool
3553 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3555 struct ppc_link_hash_table *htab;
3556 flagword flags;
3558 htab = ppc_hash_table (info);
3560 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3561 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3562 if (htab->params->save_restore_funcs)
3564 /* Create .sfpr for code to save and restore fp regs. */
3565 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3566 flags);
3567 if (htab->sfpr == NULL
3568 || !bfd_set_section_alignment (htab->sfpr, 2))
3569 return false;
3572 if (bfd_link_relocatable (info))
3573 return true;
3575 /* Create .glink for lazy dynamic linking support. */
3576 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3577 flags);
3578 if (htab->glink == NULL
3579 || !bfd_set_section_alignment (htab->glink, 3))
3580 return false;
3582 /* The part of .glink used by global entry stubs, separate so that
3583 it can be aligned appropriately without affecting htab->glink. */
3584 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3585 flags);
3586 if (htab->global_entry == NULL
3587 || !bfd_set_section_alignment (htab->global_entry, 2))
3588 return false;
3590 if (!info->no_ld_generated_unwind_info)
3592 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3593 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3594 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3595 ".eh_frame",
3596 flags);
3597 if (htab->glink_eh_frame == NULL
3598 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3599 return false;
3602 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3603 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3604 if (htab->elf.iplt == NULL
3605 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3606 return false;
3608 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3609 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3610 htab->elf.irelplt
3611 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3612 if (htab->elf.irelplt == NULL
3613 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3614 return false;
3616 /* Create branch lookup table for plt_branch stubs. */
3617 flags = (SEC_ALLOC | SEC_LOAD
3618 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3619 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3620 flags);
3621 if (htab->brlt == NULL
3622 || !bfd_set_section_alignment (htab->brlt, 3))
3623 return false;
3625 /* Local plt entries, put in .branch_lt but a separate section for
3626 convenience. */
3627 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3628 flags);
3629 if (htab->pltlocal == NULL
3630 || !bfd_set_section_alignment (htab->pltlocal, 3))
3631 return false;
3633 if (!bfd_link_pic (info))
3634 return true;
3636 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3637 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3638 htab->relbrlt
3639 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3640 if (htab->relbrlt == NULL
3641 || !bfd_set_section_alignment (htab->relbrlt, 3))
3642 return false;
3644 htab->relpltlocal
3645 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3646 if (htab->relpltlocal == NULL
3647 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3648 return false;
3650 return true;
3653 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3655 bool
3656 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3657 struct ppc64_elf_params *params)
3659 struct ppc_link_hash_table *htab;
3661 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3663 /* Always hook our dynamic sections into the first bfd, which is the
3664 linker created stub bfd. This ensures that the GOT header is at
3665 the start of the output TOC section. */
3666 htab = ppc_hash_table (info);
3667 htab->elf.dynobj = params->stub_bfd;
3668 htab->params = params;
3670 return create_linkage_sections (htab->elf.dynobj, info);
3673 /* Build a name for an entry in the stub hash table. */
3675 static char *
3676 ppc_stub_name (const asection *input_section,
3677 const asection *sym_sec,
3678 const struct ppc_link_hash_entry *h,
3679 const Elf_Internal_Rela *rel)
3681 char *stub_name;
3682 ssize_t len;
3684 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3685 offsets from a sym as a branch target? In fact, we could
3686 probably assume the addend is always zero. */
3687 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3689 if (h)
3691 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3692 stub_name = bfd_malloc (len);
3693 if (stub_name == NULL)
3694 return stub_name;
3696 len = sprintf (stub_name, "%08x.%s+%x",
3697 input_section->id & 0xffffffff,
3698 h->elf.root.root.string,
3699 (int) rel->r_addend & 0xffffffff);
3701 else
3703 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3704 stub_name = bfd_malloc (len);
3705 if (stub_name == NULL)
3706 return stub_name;
3708 len = sprintf (stub_name, "%08x.%x:%x+%x",
3709 input_section->id & 0xffffffff,
3710 sym_sec->id & 0xffffffff,
3711 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3712 (int) rel->r_addend & 0xffffffff);
3714 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3715 stub_name[len - 2] = 0;
3716 return stub_name;
3719 /* If mixing power10 with non-power10 code and --power10-stubs is not
3720 specified (or is auto) then calls using @notoc relocations that
3721 need a stub will utilize power10 instructions in the stub, and
3722 calls without @notoc relocations will not use power10 instructions.
3723 The two classes of stubs are stored in separate stub_hash_table
3724 entries having the same key string. The two entries will always be
3725 adjacent on entry->root.next chain, even if hash table resizing
3726 occurs. This function selects the correct entry to use. */
3728 static struct ppc_stub_hash_entry *
3729 select_alt_stub (struct ppc_stub_hash_entry *entry, bool notoc)
3731 bool have_notoc;
3733 have_notoc = (entry->stub_type == ppc_stub_plt_call_notoc
3734 || entry->stub_type == ppc_stub_plt_branch_notoc
3735 || entry->stub_type == ppc_stub_long_branch_notoc);
3737 if (have_notoc != notoc)
3739 const char *stub_name = entry->root.string;
3741 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3742 if (entry != NULL
3743 && entry->root.string != stub_name)
3744 entry = NULL;
3747 return entry;
3750 /* Look up an entry in the stub hash. Stub entries are cached because
3751 creating the stub name takes a bit of time. */
3753 static struct ppc_stub_hash_entry *
3754 ppc_get_stub_entry (const asection *input_section,
3755 const asection *sym_sec,
3756 struct ppc_link_hash_entry *h,
3757 const Elf_Internal_Rela *rel,
3758 struct ppc_link_hash_table *htab)
3760 struct ppc_stub_hash_entry *stub_entry;
3761 struct map_stub *group;
3763 /* If this input section is part of a group of sections sharing one
3764 stub section, then use the id of the first section in the group.
3765 Stub names need to include a section id, as there may well be
3766 more than one stub used to reach say, printf, and we need to
3767 distinguish between them. */
3768 group = htab->sec_info[input_section->id].u.group;
3769 if (group == NULL)
3770 return NULL;
3772 if (h != NULL && h->u.stub_cache != NULL
3773 && h->u.stub_cache->h == h
3774 && h->u.stub_cache->group == group)
3776 stub_entry = h->u.stub_cache;
3778 else
3780 char *stub_name;
3782 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3783 if (stub_name == NULL)
3784 return NULL;
3786 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3787 stub_name, false, false);
3788 if (h != NULL)
3789 h->u.stub_cache = stub_entry;
3791 free (stub_name);
3794 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3796 bool notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
3798 stub_entry = select_alt_stub (stub_entry, notoc);
3801 return stub_entry;
3804 /* Add a new stub entry to the stub hash. Not all fields of the new
3805 stub entry are initialised. */
3807 static struct ppc_stub_hash_entry *
3808 ppc_add_stub (const char *stub_name,
3809 asection *section,
3810 struct bfd_link_info *info)
3812 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3813 struct map_stub *group;
3814 asection *link_sec;
3815 asection *stub_sec;
3816 struct ppc_stub_hash_entry *stub_entry;
3818 group = htab->sec_info[section->id].u.group;
3819 link_sec = group->link_sec;
3820 stub_sec = group->stub_sec;
3821 if (stub_sec == NULL)
3823 size_t namelen;
3824 bfd_size_type len;
3825 char *s_name;
3827 namelen = strlen (link_sec->name);
3828 len = namelen + sizeof (STUB_SUFFIX);
3829 s_name = bfd_alloc (htab->params->stub_bfd, len);
3830 if (s_name == NULL)
3831 return NULL;
3833 memcpy (s_name, link_sec->name, namelen);
3834 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3835 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3836 if (stub_sec == NULL)
3837 return NULL;
3838 group->stub_sec = stub_sec;
3841 /* Enter this entry into the linker stub hash table. */
3842 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3843 true, false);
3844 if (stub_entry == NULL)
3846 /* xgettext:c-format */
3847 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3848 section->owner, stub_name);
3849 return NULL;
3852 stub_entry->group = group;
3853 stub_entry->stub_offset = 0;
3854 return stub_entry;
3857 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3858 not already done. */
3860 static bool
3861 create_got_section (bfd *abfd, struct bfd_link_info *info)
3863 asection *got, *relgot;
3864 flagword flags;
3865 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3867 if (!is_ppc64_elf (abfd))
3868 return false;
3869 if (htab == NULL)
3870 return false;
3872 if (!htab->elf.sgot
3873 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3874 return false;
3876 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3877 | SEC_LINKER_CREATED);
3879 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3880 if (!got
3881 || !bfd_set_section_alignment (got, 3))
3882 return false;
3884 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3885 flags | SEC_READONLY);
3886 if (!relgot
3887 || !bfd_set_section_alignment (relgot, 3))
3888 return false;
3890 ppc64_elf_tdata (abfd)->got = got;
3891 ppc64_elf_tdata (abfd)->relgot = relgot;
3892 return true;
3895 /* Follow indirect and warning symbol links. */
3897 static inline struct bfd_link_hash_entry *
3898 follow_link (struct bfd_link_hash_entry *h)
3900 while (h->type == bfd_link_hash_indirect
3901 || h->type == bfd_link_hash_warning)
3902 h = h->u.i.link;
3903 return h;
3906 static inline struct elf_link_hash_entry *
3907 elf_follow_link (struct elf_link_hash_entry *h)
3909 return (struct elf_link_hash_entry *) follow_link (&h->root);
3912 static inline struct ppc_link_hash_entry *
3913 ppc_follow_link (struct ppc_link_hash_entry *h)
3915 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
3918 /* Merge PLT info on FROM with that on TO. */
3920 static void
3921 move_plt_plist (struct ppc_link_hash_entry *from,
3922 struct ppc_link_hash_entry *to)
3924 if (from->elf.plt.plist != NULL)
3926 if (to->elf.plt.plist != NULL)
3928 struct plt_entry **entp;
3929 struct plt_entry *ent;
3931 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3933 struct plt_entry *dent;
3935 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3936 if (dent->addend == ent->addend)
3938 dent->plt.refcount += ent->plt.refcount;
3939 *entp = ent->next;
3940 break;
3942 if (dent == NULL)
3943 entp = &ent->next;
3945 *entp = to->elf.plt.plist;
3948 to->elf.plt.plist = from->elf.plt.plist;
3949 from->elf.plt.plist = NULL;
3953 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3955 static void
3956 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3957 struct elf_link_hash_entry *dir,
3958 struct elf_link_hash_entry *ind)
3960 struct ppc_link_hash_entry *edir, *eind;
3962 edir = ppc_elf_hash_entry (dir);
3963 eind = ppc_elf_hash_entry (ind);
3965 edir->is_func |= eind->is_func;
3966 edir->is_func_descriptor |= eind->is_func_descriptor;
3967 edir->tls_mask |= eind->tls_mask;
3968 if (eind->oh != NULL)
3969 edir->oh = ppc_follow_link (eind->oh);
3971 if (edir->elf.versioned != versioned_hidden)
3972 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3973 edir->elf.ref_regular |= eind->elf.ref_regular;
3974 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3975 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3976 edir->elf.needs_plt |= eind->elf.needs_plt;
3977 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3979 /* If we were called to copy over info for a weak sym, don't copy
3980 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3981 in order to simplify readonly_dynrelocs and save a field in the
3982 symbol hash entry, but that means dyn_relocs can't be used in any
3983 tests about a specific symbol, or affect other symbol flags which
3984 are then tested. */
3985 if (eind->elf.root.type != bfd_link_hash_indirect)
3986 return;
3988 /* Copy over any dynamic relocs we may have on the indirect sym. */
3989 if (ind->dyn_relocs != NULL)
3991 if (dir->dyn_relocs != NULL)
3993 struct elf_dyn_relocs **pp;
3994 struct elf_dyn_relocs *p;
3996 /* Add reloc counts against the indirect sym to the direct sym
3997 list. Merge any entries against the same section. */
3998 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
4000 struct elf_dyn_relocs *q;
4002 for (q = dir->dyn_relocs; q != NULL; q = q->next)
4003 if (q->sec == p->sec)
4005 q->pc_count += p->pc_count;
4006 q->count += p->count;
4007 *pp = p->next;
4008 break;
4010 if (q == NULL)
4011 pp = &p->next;
4013 *pp = dir->dyn_relocs;
4016 dir->dyn_relocs = ind->dyn_relocs;
4017 ind->dyn_relocs = NULL;
4020 /* Copy over got entries that we may have already seen to the
4021 symbol which just became indirect. */
4022 if (eind->elf.got.glist != NULL)
4024 if (edir->elf.got.glist != NULL)
4026 struct got_entry **entp;
4027 struct got_entry *ent;
4029 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4031 struct got_entry *dent;
4033 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4034 if (dent->addend == ent->addend
4035 && dent->owner == ent->owner
4036 && dent->tls_type == ent->tls_type)
4038 dent->got.refcount += ent->got.refcount;
4039 *entp = ent->next;
4040 break;
4042 if (dent == NULL)
4043 entp = &ent->next;
4045 *entp = edir->elf.got.glist;
4048 edir->elf.got.glist = eind->elf.got.glist;
4049 eind->elf.got.glist = NULL;
4052 /* And plt entries. */
4053 move_plt_plist (eind, edir);
4055 if (eind->elf.dynindx != -1)
4057 if (edir->elf.dynindx != -1)
4058 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4059 edir->elf.dynstr_index);
4060 edir->elf.dynindx = eind->elf.dynindx;
4061 edir->elf.dynstr_index = eind->elf.dynstr_index;
4062 eind->elf.dynindx = -1;
4063 eind->elf.dynstr_index = 0;
4067 /* Find the function descriptor hash entry from the given function code
4068 hash entry FH. Link the entries via their OH fields. */
4070 static struct ppc_link_hash_entry *
4071 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4073 struct ppc_link_hash_entry *fdh = fh->oh;
4075 if (fdh == NULL)
4077 const char *fd_name = fh->elf.root.root.string + 1;
4079 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4080 false, false, false));
4081 if (fdh == NULL)
4082 return fdh;
4084 fdh->is_func_descriptor = 1;
4085 fdh->oh = fh;
4086 fh->is_func = 1;
4087 fh->oh = fdh;
4090 fdh = ppc_follow_link (fdh);
4091 fdh->is_func_descriptor = 1;
4092 fdh->oh = fh;
4093 return fdh;
4096 /* Make a fake function descriptor sym for the undefined code sym FH. */
4098 static struct ppc_link_hash_entry *
4099 make_fdh (struct bfd_link_info *info,
4100 struct ppc_link_hash_entry *fh)
4102 bfd *abfd = fh->elf.root.u.undef.abfd;
4103 struct bfd_link_hash_entry *bh = NULL;
4104 struct ppc_link_hash_entry *fdh;
4105 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4106 ? BSF_WEAK
4107 : BSF_GLOBAL);
4109 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4110 fh->elf.root.root.string + 1,
4111 flags, bfd_und_section_ptr, 0,
4112 NULL, false, false, &bh))
4113 return NULL;
4115 fdh = (struct ppc_link_hash_entry *) bh;
4116 fdh->elf.non_elf = 0;
4117 fdh->fake = 1;
4118 fdh->is_func_descriptor = 1;
4119 fdh->oh = fh;
4120 fh->is_func = 1;
4121 fh->oh = fdh;
4122 return fdh;
4125 /* Fix function descriptor symbols defined in .opd sections to be
4126 function type. */
4128 static bool
4129 ppc64_elf_add_symbol_hook (bfd *ibfd,
4130 struct bfd_link_info *info,
4131 Elf_Internal_Sym *isym,
4132 const char **name,
4133 flagword *flags ATTRIBUTE_UNUSED,
4134 asection **sec,
4135 bfd_vma *value)
4137 if (*sec != NULL
4138 && strcmp ((*sec)->name, ".opd") == 0)
4140 asection *code_sec;
4142 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4143 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4144 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4146 /* If the symbol is a function defined in .opd, and the function
4147 code is in a discarded group, let it appear to be undefined. */
4148 if (!bfd_link_relocatable (info)
4149 && (*sec)->reloc_count != 0
4150 && opd_entry_value (*sec, *value, &code_sec, NULL,
4151 false) != (bfd_vma) -1
4152 && discarded_section (code_sec))
4154 *sec = bfd_und_section_ptr;
4155 isym->st_shndx = SHN_UNDEF;
4158 else if (*sec != NULL
4159 && strcmp ((*sec)->name, ".toc") == 0
4160 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4162 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4163 if (htab != NULL)
4164 htab->params->object_in_toc = 1;
4167 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4169 if (abiversion (ibfd) == 0)
4170 set_abiversion (ibfd, 2);
4171 else if (abiversion (ibfd) == 1)
4173 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4174 " for ABI version 1"), *name);
4175 bfd_set_error (bfd_error_bad_value);
4176 return false;
4180 return true;
4183 /* Merge non-visibility st_other attributes: local entry point. */
4185 static void
4186 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4187 unsigned int st_other,
4188 bool definition,
4189 bool dynamic)
4191 if (definition && (!dynamic || !h->def_regular))
4192 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4193 | ELF_ST_VISIBILITY (h->other));
4196 /* Hook called on merging a symbol. We use this to clear "fake" since
4197 we now have a real symbol. */
4199 static bool
4200 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4201 const Elf_Internal_Sym *isym,
4202 asection **psec ATTRIBUTE_UNUSED,
4203 bool newdef ATTRIBUTE_UNUSED,
4204 bool olddef ATTRIBUTE_UNUSED,
4205 bfd *oldbfd ATTRIBUTE_UNUSED,
4206 const asection *oldsec ATTRIBUTE_UNUSED)
4208 ppc_elf_hash_entry (h)->fake = 0;
4209 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4210 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4211 return true;
4214 /* This function makes an old ABI object reference to ".bar" cause the
4215 inclusion of a new ABI object archive that defines "bar".
4216 NAME is a symbol defined in an archive. Return a symbol in the hash
4217 table that might be satisfied by the archive symbols. */
4219 static struct bfd_link_hash_entry *
4220 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4221 struct bfd_link_info *info,
4222 const char *name)
4224 struct bfd_link_hash_entry *h;
4225 char *dot_name;
4226 size_t len;
4228 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4229 if (h != NULL
4230 && ppc_hash_table (info) != NULL
4231 /* Don't return this sym if it is a fake function descriptor
4232 created by add_symbol_adjust. */
4233 && !((struct ppc_link_hash_entry *) h)->fake)
4234 return h;
4236 if (name[0] == '.')
4237 return h;
4239 len = strlen (name);
4240 dot_name = bfd_alloc (abfd, len + 2);
4241 if (dot_name == NULL)
4242 return (struct bfd_link_hash_entry *) -1;
4243 dot_name[0] = '.';
4244 memcpy (dot_name + 1, name, len + 1);
4245 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4246 bfd_release (abfd, dot_name);
4247 if (h != NULL)
4248 return h;
4250 if (strcmp (name, "__tls_get_addr_opt") == 0)
4251 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4252 return h;
4255 /* This function satisfies all old ABI object references to ".bar" if a
4256 new ABI object defines "bar". Well, at least, undefined dot symbols
4257 are made weak. This stops later archive searches from including an
4258 object if we already have a function descriptor definition. It also
4259 prevents the linker complaining about undefined symbols.
4260 We also check and correct mismatched symbol visibility here. The
4261 most restrictive visibility of the function descriptor and the
4262 function entry symbol is used. */
4264 static bool
4265 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4267 struct ppc_link_hash_table *htab;
4268 struct ppc_link_hash_entry *fdh;
4270 if (eh->elf.root.type == bfd_link_hash_warning)
4271 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4273 if (eh->elf.root.type == bfd_link_hash_indirect)
4274 return true;
4276 if (eh->elf.root.root.string[0] != '.')
4277 abort ();
4279 htab = ppc_hash_table (info);
4280 if (htab == NULL)
4281 return false;
4283 fdh = lookup_fdh (eh, htab);
4284 if (fdh == NULL
4285 && !bfd_link_relocatable (info)
4286 && (eh->elf.root.type == bfd_link_hash_undefined
4287 || eh->elf.root.type == bfd_link_hash_undefweak)
4288 && eh->elf.ref_regular)
4290 /* Make an undefined function descriptor sym, in order to
4291 pull in an --as-needed shared lib. Archives are handled
4292 elsewhere. */
4293 fdh = make_fdh (info, eh);
4294 if (fdh == NULL)
4295 return false;
4298 if (fdh != NULL)
4300 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4301 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4303 /* Make both descriptor and entry symbol have the most
4304 constraining visibility of either symbol. */
4305 if (entry_vis < descr_vis)
4306 fdh->elf.other += entry_vis - descr_vis;
4307 else if (entry_vis > descr_vis)
4308 eh->elf.other += descr_vis - entry_vis;
4310 /* Propagate reference flags from entry symbol to function
4311 descriptor symbol. */
4312 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4313 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4314 fdh->elf.ref_regular |= eh->elf.ref_regular;
4315 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4317 if (!fdh->elf.forced_local
4318 && fdh->elf.dynindx == -1
4319 && fdh->elf.versioned != versioned_hidden
4320 && (bfd_link_dll (info)
4321 || fdh->elf.def_dynamic
4322 || fdh->elf.ref_dynamic)
4323 && (eh->elf.ref_regular
4324 || eh->elf.def_regular))
4326 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4327 return false;
4331 return true;
4334 /* Set up opd section info and abiversion for IBFD, and process list
4335 of dot-symbols we made in link_hash_newfunc. */
4337 static bool
4338 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4340 struct ppc_link_hash_table *htab;
4341 struct ppc_link_hash_entry **p, *eh;
4342 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4344 if (opd != NULL && opd->size != 0)
4346 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4347 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4349 if (abiversion (ibfd) == 0)
4350 set_abiversion (ibfd, 1);
4351 else if (abiversion (ibfd) >= 2)
4353 /* xgettext:c-format */
4354 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4355 ibfd, abiversion (ibfd));
4356 bfd_set_error (bfd_error_bad_value);
4357 return false;
4361 if (is_ppc64_elf (info->output_bfd))
4363 /* For input files without an explicit abiversion in e_flags
4364 we should have flagged any with symbol st_other bits set
4365 as ELFv1 and above flagged those with .opd as ELFv2.
4366 Set the output abiversion if not yet set, and for any input
4367 still ambiguous, take its abiversion from the output.
4368 Differences in ABI are reported later. */
4369 if (abiversion (info->output_bfd) == 0)
4370 set_abiversion (info->output_bfd, abiversion (ibfd));
4371 else if (abiversion (ibfd) == 0)
4372 set_abiversion (ibfd, abiversion (info->output_bfd));
4375 htab = ppc_hash_table (info);
4376 if (htab == NULL)
4377 return true;
4379 if (opd != NULL && opd->size != 0
4380 && (ibfd->flags & DYNAMIC) == 0
4381 && (opd->flags & SEC_RELOC) != 0
4382 && opd->reloc_count != 0
4383 && !bfd_is_abs_section (opd->output_section)
4384 && info->gc_sections)
4386 /* Garbage collection needs some extra help with .opd sections.
4387 We don't want to necessarily keep everything referenced by
4388 relocs in .opd, as that would keep all functions. Instead,
4389 if we reference an .opd symbol (a function descriptor), we
4390 want to keep the function code symbol's section. This is
4391 easy for global symbols, but for local syms we need to keep
4392 information about the associated function section. */
4393 bfd_size_type amt;
4394 asection **opd_sym_map;
4395 Elf_Internal_Shdr *symtab_hdr;
4396 Elf_Internal_Rela *relocs, *rel_end, *rel;
4398 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4399 opd_sym_map = bfd_zalloc (ibfd, amt);
4400 if (opd_sym_map == NULL)
4401 return false;
4402 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4403 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4404 info->keep_memory);
4405 if (relocs == NULL)
4406 return false;
4407 symtab_hdr = &elf_symtab_hdr (ibfd);
4408 rel_end = relocs + opd->reloc_count - 1;
4409 for (rel = relocs; rel < rel_end; rel++)
4411 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4412 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4414 if (r_type == R_PPC64_ADDR64
4415 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4416 && r_symndx < symtab_hdr->sh_info)
4418 Elf_Internal_Sym *isym;
4419 asection *s;
4421 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4422 r_symndx);
4423 if (isym == NULL)
4425 if (elf_section_data (opd)->relocs != relocs)
4426 free (relocs);
4427 return false;
4430 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4431 if (s != NULL && s != opd)
4432 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4435 if (elf_section_data (opd)->relocs != relocs)
4436 free (relocs);
4439 p = &htab->dot_syms;
4440 while ((eh = *p) != NULL)
4442 *p = NULL;
4443 if (&eh->elf == htab->elf.hgot)
4445 else if (htab->elf.hgot == NULL
4446 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4447 htab->elf.hgot = &eh->elf;
4448 else if (abiversion (ibfd) <= 1)
4450 htab->need_func_desc_adj = 1;
4451 if (!add_symbol_adjust (eh, info))
4452 return false;
4454 p = &eh->u.next_dot_sym;
4456 return true;
4459 /* Undo hash table changes when an --as-needed input file is determined
4460 not to be needed. */
4462 static bool
4463 ppc64_elf_notice_as_needed (bfd *ibfd,
4464 struct bfd_link_info *info,
4465 enum notice_asneeded_action act)
4467 if (act == notice_not_needed)
4469 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4471 if (htab == NULL)
4472 return false;
4474 htab->dot_syms = NULL;
4476 return _bfd_elf_notice_as_needed (ibfd, info, act);
4479 /* If --just-symbols against a final linked binary, then assume we need
4480 toc adjusting stubs when calling functions defined there. */
4482 static void
4483 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4485 if ((sec->flags & SEC_CODE) != 0
4486 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4487 && is_ppc64_elf (sec->owner))
4489 if (abiversion (sec->owner) >= 2
4490 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4491 sec->has_toc_reloc = 1;
4493 _bfd_elf_link_just_syms (sec, info);
4496 static struct plt_entry **
4497 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4498 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4500 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4501 struct plt_entry **local_plt;
4502 unsigned char *local_got_tls_masks;
4504 if (local_got_ents == NULL)
4506 bfd_size_type size = symtab_hdr->sh_info;
4508 size *= (sizeof (*local_got_ents)
4509 + sizeof (*local_plt)
4510 + sizeof (*local_got_tls_masks));
4511 local_got_ents = bfd_zalloc (abfd, size);
4512 if (local_got_ents == NULL)
4513 return NULL;
4514 elf_local_got_ents (abfd) = local_got_ents;
4517 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4519 struct got_entry *ent;
4521 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4522 if (ent->addend == r_addend
4523 && ent->owner == abfd
4524 && ent->tls_type == tls_type)
4525 break;
4526 if (ent == NULL)
4528 size_t amt = sizeof (*ent);
4529 ent = bfd_alloc (abfd, amt);
4530 if (ent == NULL)
4531 return false;
4532 ent->next = local_got_ents[r_symndx];
4533 ent->addend = r_addend;
4534 ent->owner = abfd;
4535 ent->tls_type = tls_type;
4536 ent->is_indirect = false;
4537 ent->got.refcount = 0;
4538 local_got_ents[r_symndx] = ent;
4540 ent->got.refcount += 1;
4543 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4544 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4545 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4547 return local_plt + r_symndx;
4550 static bool
4551 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4553 struct plt_entry *ent;
4555 for (ent = *plist; ent != NULL; ent = ent->next)
4556 if (ent->addend == addend)
4557 break;
4558 if (ent == NULL)
4560 size_t amt = sizeof (*ent);
4561 ent = bfd_alloc (abfd, amt);
4562 if (ent == NULL)
4563 return false;
4564 ent->next = *plist;
4565 ent->addend = addend;
4566 ent->plt.refcount = 0;
4567 *plist = ent;
4569 ent->plt.refcount += 1;
4570 return true;
4573 static bool
4574 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4576 return (r_type == R_PPC64_REL24
4577 || r_type == R_PPC64_REL24_NOTOC
4578 || r_type == R_PPC64_REL14
4579 || r_type == R_PPC64_REL14_BRTAKEN
4580 || r_type == R_PPC64_REL14_BRNTAKEN
4581 || r_type == R_PPC64_ADDR24
4582 || r_type == R_PPC64_ADDR14
4583 || r_type == R_PPC64_ADDR14_BRTAKEN
4584 || r_type == R_PPC64_ADDR14_BRNTAKEN
4585 || r_type == R_PPC64_PLTCALL
4586 || r_type == R_PPC64_PLTCALL_NOTOC);
4589 /* Relocs on inline plt call sequence insns prior to the call. */
4591 static bool
4592 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4594 return (r_type == R_PPC64_PLT16_HA
4595 || r_type == R_PPC64_PLT16_HI
4596 || r_type == R_PPC64_PLT16_LO
4597 || r_type == R_PPC64_PLT16_LO_DS
4598 || r_type == R_PPC64_PLT_PCREL34
4599 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4600 || r_type == R_PPC64_PLTSEQ
4601 || r_type == R_PPC64_PLTSEQ_NOTOC);
4604 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4605 relocs, return true for those that operate on a dword. */
4607 static bool
4608 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4610 return (r_type == R_PPC64_PLT_PCREL34
4611 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4612 || r_type == R_PPC64_PLTCALL);
4615 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4616 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4618 static bool
4619 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4621 return offset <= sec->size && size <= sec->size - offset;
4624 /* Look through the relocs for a section during the first phase, and
4625 calculate needed space in the global offset table, procedure
4626 linkage table, and dynamic reloc sections. */
4628 static bool
4629 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4630 asection *sec, const Elf_Internal_Rela *relocs)
4632 struct ppc_link_hash_table *htab;
4633 Elf_Internal_Shdr *symtab_hdr;
4634 struct elf_link_hash_entry **sym_hashes;
4635 const Elf_Internal_Rela *rel;
4636 const Elf_Internal_Rela *rel_end;
4637 asection *sreloc;
4638 struct elf_link_hash_entry *tga, *dottga;
4639 bool is_opd;
4641 if (bfd_link_relocatable (info))
4642 return true;
4644 BFD_ASSERT (is_ppc64_elf (abfd));
4646 htab = ppc_hash_table (info);
4647 if (htab == NULL)
4648 return false;
4650 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4651 false, false, true);
4652 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4653 false, false, true);
4654 symtab_hdr = &elf_symtab_hdr (abfd);
4655 sym_hashes = elf_sym_hashes (abfd);
4656 sreloc = NULL;
4657 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4658 rel_end = relocs + sec->reloc_count;
4659 for (rel = relocs; rel < rel_end; rel++)
4661 unsigned long r_symndx;
4662 struct elf_link_hash_entry *h;
4663 enum elf_ppc64_reloc_type r_type;
4664 int tls_type;
4665 struct _ppc64_elf_section_data *ppc64_sec;
4666 struct plt_entry **ifunc, **plt_list;
4668 r_symndx = ELF64_R_SYM (rel->r_info);
4669 if (r_symndx < symtab_hdr->sh_info)
4670 h = NULL;
4671 else
4673 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4674 h = elf_follow_link (h);
4676 if (h == htab->elf.hgot)
4677 sec->has_toc_reloc = 1;
4680 r_type = ELF64_R_TYPE (rel->r_info);
4681 switch (r_type)
4683 case R_PPC64_D34:
4684 case R_PPC64_D34_LO:
4685 case R_PPC64_D34_HI30:
4686 case R_PPC64_D34_HA30:
4687 case R_PPC64_D28:
4688 case R_PPC64_TPREL34:
4689 case R_PPC64_DTPREL34:
4690 case R_PPC64_PCREL34:
4691 case R_PPC64_GOT_PCREL34:
4692 case R_PPC64_GOT_TLSGD_PCREL34:
4693 case R_PPC64_GOT_TLSLD_PCREL34:
4694 case R_PPC64_GOT_TPREL_PCREL34:
4695 case R_PPC64_GOT_DTPREL_PCREL34:
4696 case R_PPC64_PLT_PCREL34:
4697 case R_PPC64_PLT_PCREL34_NOTOC:
4698 case R_PPC64_PCREL28:
4699 htab->has_power10_relocs = 1;
4700 break;
4701 default:
4702 break;
4705 switch (r_type)
4707 case R_PPC64_PLT16_HA:
4708 case R_PPC64_GOT_TLSLD16_HA:
4709 case R_PPC64_GOT_TLSGD16_HA:
4710 case R_PPC64_GOT_TPREL16_HA:
4711 case R_PPC64_GOT_DTPREL16_HA:
4712 case R_PPC64_GOT16_HA:
4713 case R_PPC64_TOC16_HA:
4714 case R_PPC64_PLT16_LO:
4715 case R_PPC64_PLT16_LO_DS:
4716 case R_PPC64_GOT_TLSLD16_LO:
4717 case R_PPC64_GOT_TLSGD16_LO:
4718 case R_PPC64_GOT_TPREL16_LO_DS:
4719 case R_PPC64_GOT_DTPREL16_LO_DS:
4720 case R_PPC64_GOT16_LO:
4721 case R_PPC64_GOT16_LO_DS:
4722 case R_PPC64_TOC16_LO:
4723 case R_PPC64_TOC16_LO_DS:
4724 case R_PPC64_GOT_PCREL34:
4725 ppc64_elf_tdata (abfd)->has_optrel = 1;
4726 ppc64_elf_section_data (sec)->has_optrel = 1;
4727 break;
4728 default:
4729 break;
4732 ifunc = NULL;
4733 if (h != NULL)
4735 if (h->type == STT_GNU_IFUNC)
4737 h->needs_plt = 1;
4738 ifunc = &h->plt.plist;
4741 else
4743 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
4744 abfd, r_symndx);
4745 if (isym == NULL)
4746 return false;
4748 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4750 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4751 rel->r_addend,
4752 NON_GOT | PLT_IFUNC);
4753 if (ifunc == NULL)
4754 return false;
4758 tls_type = 0;
4759 switch (r_type)
4761 case R_PPC64_TLSGD:
4762 case R_PPC64_TLSLD:
4763 /* These special tls relocs tie a call to __tls_get_addr with
4764 its parameter symbol. */
4765 if (h != NULL)
4766 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4767 else
4768 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4769 rel->r_addend,
4770 NON_GOT | TLS_TLS | TLS_MARK))
4771 return false;
4772 sec->has_tls_reloc = 1;
4773 break;
4775 case R_PPC64_GOT_TLSLD16:
4776 case R_PPC64_GOT_TLSLD16_LO:
4777 case R_PPC64_GOT_TLSLD16_HI:
4778 case R_PPC64_GOT_TLSLD16_HA:
4779 case R_PPC64_GOT_TLSLD_PCREL34:
4780 tls_type = TLS_TLS | TLS_LD;
4781 goto dogottls;
4783 case R_PPC64_GOT_TLSGD16:
4784 case R_PPC64_GOT_TLSGD16_LO:
4785 case R_PPC64_GOT_TLSGD16_HI:
4786 case R_PPC64_GOT_TLSGD16_HA:
4787 case R_PPC64_GOT_TLSGD_PCREL34:
4788 tls_type = TLS_TLS | TLS_GD;
4789 goto dogottls;
4791 case R_PPC64_GOT_TPREL16_DS:
4792 case R_PPC64_GOT_TPREL16_LO_DS:
4793 case R_PPC64_GOT_TPREL16_HI:
4794 case R_PPC64_GOT_TPREL16_HA:
4795 case R_PPC64_GOT_TPREL_PCREL34:
4796 if (bfd_link_dll (info))
4797 info->flags |= DF_STATIC_TLS;
4798 tls_type = TLS_TLS | TLS_TPREL;
4799 goto dogottls;
4801 case R_PPC64_GOT_DTPREL16_DS:
4802 case R_PPC64_GOT_DTPREL16_LO_DS:
4803 case R_PPC64_GOT_DTPREL16_HI:
4804 case R_PPC64_GOT_DTPREL16_HA:
4805 case R_PPC64_GOT_DTPREL_PCREL34:
4806 tls_type = TLS_TLS | TLS_DTPREL;
4807 dogottls:
4808 sec->has_tls_reloc = 1;
4809 goto dogot;
4811 case R_PPC64_GOT16:
4812 case R_PPC64_GOT16_LO:
4813 case R_PPC64_GOT16_HI:
4814 case R_PPC64_GOT16_HA:
4815 case R_PPC64_GOT16_DS:
4816 case R_PPC64_GOT16_LO_DS:
4817 case R_PPC64_GOT_PCREL34:
4818 dogot:
4819 /* This symbol requires a global offset table entry. */
4820 sec->has_toc_reloc = 1;
4821 if (r_type == R_PPC64_GOT_TLSLD16
4822 || r_type == R_PPC64_GOT_TLSGD16
4823 || r_type == R_PPC64_GOT_TPREL16_DS
4824 || r_type == R_PPC64_GOT_DTPREL16_DS
4825 || r_type == R_PPC64_GOT16
4826 || r_type == R_PPC64_GOT16_DS)
4828 htab->do_multi_toc = 1;
4829 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4832 if (ppc64_elf_tdata (abfd)->got == NULL
4833 && !create_got_section (abfd, info))
4834 return false;
4836 if (h != NULL)
4838 struct ppc_link_hash_entry *eh;
4839 struct got_entry *ent;
4841 eh = ppc_elf_hash_entry (h);
4842 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4843 if (ent->addend == rel->r_addend
4844 && ent->owner == abfd
4845 && ent->tls_type == tls_type)
4846 break;
4847 if (ent == NULL)
4849 size_t amt = sizeof (*ent);
4850 ent = bfd_alloc (abfd, amt);
4851 if (ent == NULL)
4852 return false;
4853 ent->next = eh->elf.got.glist;
4854 ent->addend = rel->r_addend;
4855 ent->owner = abfd;
4856 ent->tls_type = tls_type;
4857 ent->is_indirect = false;
4858 ent->got.refcount = 0;
4859 eh->elf.got.glist = ent;
4861 ent->got.refcount += 1;
4862 eh->tls_mask |= tls_type;
4864 else
4865 /* This is a global offset table entry for a local symbol. */
4866 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4867 rel->r_addend, tls_type))
4868 return false;
4869 break;
4871 case R_PPC64_PLT16_HA:
4872 case R_PPC64_PLT16_HI:
4873 case R_PPC64_PLT16_LO:
4874 case R_PPC64_PLT16_LO_DS:
4875 case R_PPC64_PLT_PCREL34:
4876 case R_PPC64_PLT_PCREL34_NOTOC:
4877 case R_PPC64_PLT32:
4878 case R_PPC64_PLT64:
4879 /* This symbol requires a procedure linkage table entry. */
4880 plt_list = ifunc;
4881 if (h != NULL)
4883 h->needs_plt = 1;
4884 if (h->root.root.string[0] == '.'
4885 && h->root.root.string[1] != '\0')
4886 ppc_elf_hash_entry (h)->is_func = 1;
4887 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
4888 plt_list = &h->plt.plist;
4890 if (plt_list == NULL)
4891 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4892 rel->r_addend,
4893 NON_GOT | PLT_KEEP);
4894 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4895 return false;
4896 break;
4898 /* The following relocations don't need to propagate the
4899 relocation if linking a shared object since they are
4900 section relative. */
4901 case R_PPC64_SECTOFF:
4902 case R_PPC64_SECTOFF_LO:
4903 case R_PPC64_SECTOFF_HI:
4904 case R_PPC64_SECTOFF_HA:
4905 case R_PPC64_SECTOFF_DS:
4906 case R_PPC64_SECTOFF_LO_DS:
4907 case R_PPC64_DTPREL16:
4908 case R_PPC64_DTPREL16_LO:
4909 case R_PPC64_DTPREL16_HI:
4910 case R_PPC64_DTPREL16_HA:
4911 case R_PPC64_DTPREL16_DS:
4912 case R_PPC64_DTPREL16_LO_DS:
4913 case R_PPC64_DTPREL16_HIGH:
4914 case R_PPC64_DTPREL16_HIGHA:
4915 case R_PPC64_DTPREL16_HIGHER:
4916 case R_PPC64_DTPREL16_HIGHERA:
4917 case R_PPC64_DTPREL16_HIGHEST:
4918 case R_PPC64_DTPREL16_HIGHESTA:
4919 break;
4921 /* Nor do these. */
4922 case R_PPC64_REL16:
4923 case R_PPC64_REL16_LO:
4924 case R_PPC64_REL16_HI:
4925 case R_PPC64_REL16_HA:
4926 case R_PPC64_REL16_HIGH:
4927 case R_PPC64_REL16_HIGHA:
4928 case R_PPC64_REL16_HIGHER:
4929 case R_PPC64_REL16_HIGHERA:
4930 case R_PPC64_REL16_HIGHEST:
4931 case R_PPC64_REL16_HIGHESTA:
4932 case R_PPC64_REL16_HIGHER34:
4933 case R_PPC64_REL16_HIGHERA34:
4934 case R_PPC64_REL16_HIGHEST34:
4935 case R_PPC64_REL16_HIGHESTA34:
4936 case R_PPC64_REL16DX_HA:
4937 break;
4939 /* Not supported as a dynamic relocation. */
4940 case R_PPC64_ADDR64_LOCAL:
4941 if (bfd_link_pic (info))
4943 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4944 ppc_howto_init ();
4945 /* xgettext:c-format */
4946 info->callbacks->einfo (_("%H: %s reloc unsupported "
4947 "in shared libraries and PIEs\n"),
4948 abfd, sec, rel->r_offset,
4949 ppc64_elf_howto_table[r_type]->name);
4950 bfd_set_error (bfd_error_bad_value);
4951 return false;
4953 break;
4955 case R_PPC64_TOC16:
4956 case R_PPC64_TOC16_DS:
4957 htab->do_multi_toc = 1;
4958 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4959 /* Fall through. */
4960 case R_PPC64_TOC16_LO:
4961 case R_PPC64_TOC16_HI:
4962 case R_PPC64_TOC16_HA:
4963 case R_PPC64_TOC16_LO_DS:
4964 sec->has_toc_reloc = 1;
4965 if (h != NULL && bfd_link_executable (info))
4967 /* We may need a copy reloc. */
4968 h->non_got_ref = 1;
4969 /* Strongly prefer a copy reloc over a dynamic reloc.
4970 glibc ld.so as of 2019-08 will error out if one of
4971 these relocations is emitted. */
4972 h->needs_copy = 1;
4973 goto dodyn;
4975 break;
4977 /* Marker reloc. */
4978 case R_PPC64_ENTRY:
4979 break;
4981 /* This relocation describes the C++ object vtable hierarchy.
4982 Reconstruct it for later use during GC. */
4983 case R_PPC64_GNU_VTINHERIT:
4984 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4985 return false;
4986 break;
4988 /* This relocation describes which C++ vtable entries are actually
4989 used. Record for later use during GC. */
4990 case R_PPC64_GNU_VTENTRY:
4991 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4992 return false;
4993 break;
4995 case R_PPC64_REL14:
4996 case R_PPC64_REL14_BRTAKEN:
4997 case R_PPC64_REL14_BRNTAKEN:
4999 asection *dest = NULL;
5001 /* Heuristic: If jumping outside our section, chances are
5002 we are going to need a stub. */
5003 if (h != NULL)
5005 /* If the sym is weak it may be overridden later, so
5006 don't assume we know where a weak sym lives. */
5007 if (h->root.type == bfd_link_hash_defined)
5008 dest = h->root.u.def.section;
5010 else
5012 Elf_Internal_Sym *isym;
5014 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5015 abfd, r_symndx);
5016 if (isym == NULL)
5017 return false;
5019 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5022 if (dest != sec)
5023 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5025 goto rel24;
5027 case R_PPC64_PLTCALL:
5028 case R_PPC64_PLTCALL_NOTOC:
5029 ppc64_elf_section_data (sec)->has_pltcall = 1;
5030 /* Fall through. */
5032 case R_PPC64_REL24:
5033 case R_PPC64_REL24_NOTOC:
5034 rel24:
5035 plt_list = ifunc;
5036 if (h != NULL)
5038 h->needs_plt = 1;
5039 if (h->root.root.string[0] == '.'
5040 && h->root.root.string[1] != '\0')
5041 ppc_elf_hash_entry (h)->is_func = 1;
5043 if (h == tga || h == dottga)
5045 sec->has_tls_reloc = 1;
5046 if (rel != relocs
5047 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5048 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5049 /* We have a new-style __tls_get_addr call with
5050 a marker reloc. */
5052 else
5053 /* Mark this section as having an old-style call. */
5054 sec->nomark_tls_get_addr = 1;
5056 plt_list = &h->plt.plist;
5059 /* We may need a .plt entry if the function this reloc
5060 refers to is in a shared lib. */
5061 if (plt_list
5062 && !update_plt_info (abfd, plt_list, rel->r_addend))
5063 return false;
5064 break;
5066 case R_PPC64_ADDR14:
5067 case R_PPC64_ADDR14_BRNTAKEN:
5068 case R_PPC64_ADDR14_BRTAKEN:
5069 case R_PPC64_ADDR24:
5070 goto dodyn;
5072 case R_PPC64_TPREL64:
5073 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5074 if (bfd_link_dll (info))
5075 info->flags |= DF_STATIC_TLS;
5076 goto dotlstoc;
5078 case R_PPC64_DTPMOD64:
5079 if (rel + 1 < rel_end
5080 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5081 && rel[1].r_offset == rel->r_offset + 8)
5082 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5083 else
5084 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5085 goto dotlstoc;
5087 case R_PPC64_DTPREL64:
5088 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5089 if (rel != relocs
5090 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5091 && rel[-1].r_offset == rel->r_offset - 8)
5092 /* This is the second reloc of a dtpmod, dtprel pair.
5093 Don't mark with TLS_DTPREL. */
5094 goto dodyn;
5096 dotlstoc:
5097 sec->has_tls_reloc = 1;
5098 if (h != NULL)
5099 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5100 else
5101 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5102 rel->r_addend, tls_type))
5103 return false;
5105 ppc64_sec = ppc64_elf_section_data (sec);
5106 if (ppc64_sec->sec_type != sec_toc)
5108 bfd_size_type amt;
5110 /* One extra to simplify get_tls_mask. */
5111 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5112 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5113 if (ppc64_sec->u.toc.symndx == NULL)
5114 return false;
5115 amt = sec->size * sizeof (bfd_vma) / 8;
5116 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5117 if (ppc64_sec->u.toc.add == NULL)
5118 return false;
5119 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5120 ppc64_sec->sec_type = sec_toc;
5122 BFD_ASSERT (rel->r_offset % 8 == 0);
5123 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5124 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5126 /* Mark the second slot of a GD or LD entry.
5127 -1 to indicate GD and -2 to indicate LD. */
5128 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5129 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5130 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5131 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5132 goto dodyn;
5134 case R_PPC64_TPREL16_HI:
5135 case R_PPC64_TPREL16_HA:
5136 case R_PPC64_TPREL16_HIGH:
5137 case R_PPC64_TPREL16_HIGHA:
5138 case R_PPC64_TPREL16_HIGHER:
5139 case R_PPC64_TPREL16_HIGHERA:
5140 case R_PPC64_TPREL16_HIGHEST:
5141 case R_PPC64_TPREL16_HIGHESTA:
5142 sec->has_tls_reloc = 1;
5143 /* Fall through. */
5144 case R_PPC64_TPREL34:
5145 case R_PPC64_TPREL16:
5146 case R_PPC64_TPREL16_DS:
5147 case R_PPC64_TPREL16_LO:
5148 case R_PPC64_TPREL16_LO_DS:
5149 if (bfd_link_dll (info))
5150 info->flags |= DF_STATIC_TLS;
5151 goto dodyn;
5153 case R_PPC64_ADDR64:
5154 if (is_opd
5155 && rel + 1 < rel_end
5156 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5158 if (h != NULL)
5159 ppc_elf_hash_entry (h)->is_func = 1;
5161 /* Fall through. */
5163 case R_PPC64_ADDR16:
5164 case R_PPC64_ADDR16_DS:
5165 case R_PPC64_ADDR16_HA:
5166 case R_PPC64_ADDR16_HI:
5167 case R_PPC64_ADDR16_HIGH:
5168 case R_PPC64_ADDR16_HIGHA:
5169 case R_PPC64_ADDR16_HIGHER:
5170 case R_PPC64_ADDR16_HIGHERA:
5171 case R_PPC64_ADDR16_HIGHEST:
5172 case R_PPC64_ADDR16_HIGHESTA:
5173 case R_PPC64_ADDR16_LO:
5174 case R_PPC64_ADDR16_LO_DS:
5175 case R_PPC64_D34:
5176 case R_PPC64_D34_LO:
5177 case R_PPC64_D34_HI30:
5178 case R_PPC64_D34_HA30:
5179 case R_PPC64_ADDR16_HIGHER34:
5180 case R_PPC64_ADDR16_HIGHERA34:
5181 case R_PPC64_ADDR16_HIGHEST34:
5182 case R_PPC64_ADDR16_HIGHESTA34:
5183 case R_PPC64_D28:
5184 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5185 && rel->r_addend == 0)
5187 /* We may need a .plt entry if this reloc refers to a
5188 function in a shared lib. */
5189 if (!update_plt_info (abfd, &h->plt.plist, 0))
5190 return false;
5191 h->pointer_equality_needed = 1;
5193 /* Fall through. */
5195 case R_PPC64_REL30:
5196 case R_PPC64_REL32:
5197 case R_PPC64_REL64:
5198 case R_PPC64_ADDR32:
5199 case R_PPC64_UADDR16:
5200 case R_PPC64_UADDR32:
5201 case R_PPC64_UADDR64:
5202 case R_PPC64_TOC:
5203 if (h != NULL && bfd_link_executable (info))
5204 /* We may need a copy reloc. */
5205 h->non_got_ref = 1;
5207 /* Don't propagate .opd relocs. */
5208 if (NO_OPD_RELOCS && is_opd)
5209 break;
5211 /* If we are creating a shared library, and this is a reloc
5212 against a global symbol, or a non PC relative reloc
5213 against a local symbol, then we need to copy the reloc
5214 into the shared library. However, if we are linking with
5215 -Bsymbolic, we do not need to copy a reloc against a
5216 global symbol which is defined in an object we are
5217 including in the link (i.e., DEF_REGULAR is set). At
5218 this point we have not seen all the input files, so it is
5219 possible that DEF_REGULAR is not set now but will be set
5220 later (it is never cleared). In case of a weak definition,
5221 DEF_REGULAR may be cleared later by a strong definition in
5222 a shared library. We account for that possibility below by
5223 storing information in the dyn_relocs field of the hash
5224 table entry. A similar situation occurs when creating
5225 shared libraries and symbol visibility changes render the
5226 symbol local.
5228 If on the other hand, we are creating an executable, we
5229 may need to keep relocations for symbols satisfied by a
5230 dynamic library if we manage to avoid copy relocs for the
5231 symbol. */
5232 dodyn:
5233 if ((h != NULL
5234 && (h->root.type == bfd_link_hash_defweak
5235 || !h->def_regular))
5236 || (h != NULL
5237 && !bfd_link_executable (info)
5238 && !SYMBOLIC_BIND (info, h))
5239 || (bfd_link_pic (info)
5240 && must_be_dyn_reloc (info, r_type))
5241 || (!bfd_link_pic (info)
5242 && ifunc != NULL))
5244 /* We must copy these reloc types into the output file.
5245 Create a reloc section in dynobj and make room for
5246 this reloc. */
5247 if (sreloc == NULL)
5249 sreloc = _bfd_elf_make_dynamic_reloc_section
5250 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5252 if (sreloc == NULL)
5253 return false;
5256 /* If this is a global symbol, we count the number of
5257 relocations we need for this symbol. */
5258 if (h != NULL)
5260 struct elf_dyn_relocs *p;
5261 struct elf_dyn_relocs **head;
5263 head = &h->dyn_relocs;
5264 p = *head;
5265 if (p == NULL || p->sec != sec)
5267 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5268 if (p == NULL)
5269 return false;
5270 p->next = *head;
5271 *head = p;
5272 p->sec = sec;
5273 p->count = 0;
5274 p->pc_count = 0;
5276 p->count += 1;
5277 if (!must_be_dyn_reloc (info, r_type))
5278 p->pc_count += 1;
5280 else
5282 /* Track dynamic relocs needed for local syms too.
5283 We really need local syms available to do this
5284 easily. Oh well. */
5285 struct ppc_dyn_relocs *p;
5286 struct ppc_dyn_relocs **head;
5287 bool is_ifunc;
5288 asection *s;
5289 void *vpp;
5290 Elf_Internal_Sym *isym;
5292 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5293 abfd, r_symndx);
5294 if (isym == NULL)
5295 return false;
5297 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5298 if (s == NULL)
5299 s = sec;
5301 vpp = &elf_section_data (s)->local_dynrel;
5302 head = (struct ppc_dyn_relocs **) vpp;
5303 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5304 p = *head;
5305 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5306 p = p->next;
5307 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5309 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5310 if (p == NULL)
5311 return false;
5312 p->next = *head;
5313 *head = p;
5314 p->sec = sec;
5315 p->ifunc = is_ifunc;
5316 p->count = 0;
5318 p->count += 1;
5321 break;
5323 default:
5324 break;
5328 return true;
5331 /* Merge backend specific data from an object file to the output
5332 object file when linking. */
5334 static bool
5335 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5337 bfd *obfd = info->output_bfd;
5338 unsigned long iflags, oflags;
5340 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5341 return true;
5343 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5344 return true;
5346 if (!_bfd_generic_verify_endian_match (ibfd, info))
5347 return false;
5349 iflags = elf_elfheader (ibfd)->e_flags;
5350 oflags = elf_elfheader (obfd)->e_flags;
5352 if (iflags & ~EF_PPC64_ABI)
5354 _bfd_error_handler
5355 /* xgettext:c-format */
5356 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5357 bfd_set_error (bfd_error_bad_value);
5358 return false;
5360 else if (iflags != oflags && iflags != 0)
5362 _bfd_error_handler
5363 /* xgettext:c-format */
5364 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5365 ibfd, iflags, oflags);
5366 bfd_set_error (bfd_error_bad_value);
5367 return false;
5370 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5371 return false;
5373 /* Merge Tag_compatibility attributes and any common GNU ones. */
5374 return _bfd_elf_merge_object_attributes (ibfd, info);
5377 static bool
5378 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5380 /* Print normal ELF private data. */
5381 _bfd_elf_print_private_bfd_data (abfd, ptr);
5383 if (elf_elfheader (abfd)->e_flags != 0)
5385 FILE *file = ptr;
5387 fprintf (file, _("private flags = 0x%lx:"),
5388 elf_elfheader (abfd)->e_flags);
5390 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5391 fprintf (file, _(" [abiv%ld]"),
5392 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5393 fputc ('\n', file);
5396 return true;
5399 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5400 of the code entry point, and its section, which must be in the same
5401 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5403 static bfd_vma
5404 opd_entry_value (asection *opd_sec,
5405 bfd_vma offset,
5406 asection **code_sec,
5407 bfd_vma *code_off,
5408 bool in_code_sec)
5410 bfd *opd_bfd = opd_sec->owner;
5411 Elf_Internal_Rela *relocs;
5412 Elf_Internal_Rela *lo, *hi, *look;
5413 bfd_vma val;
5415 /* No relocs implies we are linking a --just-symbols object, or looking
5416 at a final linked executable with addr2line or somesuch. */
5417 if (opd_sec->reloc_count == 0)
5419 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5421 if (contents == NULL)
5423 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5424 return (bfd_vma) -1;
5425 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5428 /* PR 17512: file: 64b9dfbb. */
5429 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5430 return (bfd_vma) -1;
5432 val = bfd_get_64 (opd_bfd, contents + offset);
5433 if (code_sec != NULL)
5435 asection *sec, *likely = NULL;
5437 if (in_code_sec)
5439 sec = *code_sec;
5440 if (sec->vma <= val
5441 && val < sec->vma + sec->size)
5442 likely = sec;
5443 else
5444 val = -1;
5446 else
5447 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5448 if (sec->vma <= val
5449 && (sec->flags & SEC_LOAD) != 0
5450 && (sec->flags & SEC_ALLOC) != 0)
5451 likely = sec;
5452 if (likely != NULL)
5454 *code_sec = likely;
5455 if (code_off != NULL)
5456 *code_off = val - likely->vma;
5459 return val;
5462 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5464 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5465 if (relocs == NULL)
5466 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5467 /* PR 17512: file: df8e1fd6. */
5468 if (relocs == NULL)
5469 return (bfd_vma) -1;
5471 /* Go find the opd reloc at the sym address. */
5472 lo = relocs;
5473 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5474 val = (bfd_vma) -1;
5475 while (lo < hi)
5477 look = lo + (hi - lo) / 2;
5478 if (look->r_offset < offset)
5479 lo = look + 1;
5480 else if (look->r_offset > offset)
5481 hi = look;
5482 else
5484 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5486 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5487 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5489 unsigned long symndx = ELF64_R_SYM (look->r_info);
5490 asection *sec = NULL;
5492 if (symndx >= symtab_hdr->sh_info
5493 && elf_sym_hashes (opd_bfd) != NULL)
5495 struct elf_link_hash_entry **sym_hashes;
5496 struct elf_link_hash_entry *rh;
5498 sym_hashes = elf_sym_hashes (opd_bfd);
5499 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5500 if (rh != NULL)
5502 rh = elf_follow_link (rh);
5503 if (rh->root.type != bfd_link_hash_defined
5504 && rh->root.type != bfd_link_hash_defweak)
5505 break;
5506 if (rh->root.u.def.section->owner == opd_bfd)
5508 val = rh->root.u.def.value;
5509 sec = rh->root.u.def.section;
5514 if (sec == NULL)
5516 Elf_Internal_Sym *sym;
5518 if (symndx < symtab_hdr->sh_info)
5520 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5521 if (sym == NULL)
5523 size_t symcnt = symtab_hdr->sh_info;
5524 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5525 symcnt, 0,
5526 NULL, NULL, NULL);
5527 if (sym == NULL)
5528 break;
5529 symtab_hdr->contents = (bfd_byte *) sym;
5531 sym += symndx;
5533 else
5535 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5536 1, symndx,
5537 NULL, NULL, NULL);
5538 if (sym == NULL)
5539 break;
5541 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5542 if (sec == NULL)
5543 break;
5544 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5545 val = sym->st_value;
5548 val += look->r_addend;
5549 if (code_off != NULL)
5550 *code_off = val;
5551 if (code_sec != NULL)
5553 if (in_code_sec && *code_sec != sec)
5554 return -1;
5555 else
5556 *code_sec = sec;
5558 if (sec->output_section != NULL)
5559 val += sec->output_section->vma + sec->output_offset;
5561 break;
5565 return val;
5568 /* If the ELF symbol SYM might be a function in SEC, return the
5569 function size and set *CODE_OFF to the function's entry point,
5570 otherwise return zero. */
5572 static bfd_size_type
5573 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5574 bfd_vma *code_off)
5576 bfd_size_type size;
5577 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5579 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5580 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5581 return 0;
5583 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5585 /* In theory we should check that the symbol's type satisfies
5586 _bfd_elf_is_function_type(), but there are some function-like
5587 symbols which would fail this test. (eg _start). Instead
5588 we check for hidden, local, notype symbols with zero size.
5589 This type of symbol is generated by the annobin plugin for gcc
5590 and clang, and should not be considered to be a function symbol. */
5591 if (size == 0
5592 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5593 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5594 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5595 return 0;
5597 if (strcmp (sym->section->name, ".opd") == 0)
5599 struct _opd_sec_data *opd = get_opd_info (sym->section);
5600 bfd_vma symval = sym->value;
5602 if (opd != NULL
5603 && opd->adjust != NULL
5604 && elf_section_data (sym->section)->relocs != NULL)
5606 /* opd_entry_value will use cached relocs that have been
5607 adjusted, but with raw symbols. That means both local
5608 and global symbols need adjusting. */
5609 long adjust = opd->adjust[OPD_NDX (symval)];
5610 if (adjust == -1)
5611 return 0;
5612 symval += adjust;
5615 if (opd_entry_value (sym->section, symval,
5616 &sec, code_off, true) == (bfd_vma) -1)
5617 return 0;
5618 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5619 symbol. This size has nothing to do with the code size of the
5620 function, which is what we're supposed to return, but the
5621 code size isn't available without looking up the dot-sym.
5622 However, doing that would be a waste of time particularly
5623 since elf_find_function will look at the dot-sym anyway.
5624 Now, elf_find_function will keep the largest size of any
5625 function sym found at the code address of interest, so return
5626 1 here to avoid it incorrectly caching a larger function size
5627 for a small function. This does mean we return the wrong
5628 size for a new-ABI function of size 24, but all that does is
5629 disable caching for such functions. */
5630 if (size == 24)
5631 size = 1;
5633 else
5635 if (sym->section != sec)
5636 return 0;
5637 *code_off = sym->value;
5640 /* Do not return 0 for the function's size. */
5641 return size ? size : 1;
5644 /* Return true if symbol is a strong function defined in an ELFv2
5645 object with st_other localentry bits of zero, ie. its local entry
5646 point coincides with its global entry point. */
5648 static bool
5649 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5651 return (h != NULL
5652 && h->type == STT_FUNC
5653 && h->root.type == bfd_link_hash_defined
5654 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5655 && !ppc_elf_hash_entry (h)->non_zero_localentry
5656 && is_ppc64_elf (h->root.u.def.section->owner)
5657 && abiversion (h->root.u.def.section->owner) >= 2);
5660 /* Return true if symbol is defined in a regular object file. */
5662 static bool
5663 is_static_defined (struct elf_link_hash_entry *h)
5665 return ((h->root.type == bfd_link_hash_defined
5666 || h->root.type == bfd_link_hash_defweak)
5667 && h->root.u.def.section != NULL
5668 && h->root.u.def.section->output_section != NULL);
5671 /* If FDH is a function descriptor symbol, return the associated code
5672 entry symbol if it is defined. Return NULL otherwise. */
5674 static struct ppc_link_hash_entry *
5675 defined_code_entry (struct ppc_link_hash_entry *fdh)
5677 if (fdh->is_func_descriptor)
5679 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5680 if (fh->elf.root.type == bfd_link_hash_defined
5681 || fh->elf.root.type == bfd_link_hash_defweak)
5682 return fh;
5684 return NULL;
5687 /* If FH is a function code entry symbol, return the associated
5688 function descriptor symbol if it is defined. Return NULL otherwise. */
5690 static struct ppc_link_hash_entry *
5691 defined_func_desc (struct ppc_link_hash_entry *fh)
5693 if (fh->oh != NULL
5694 && fh->oh->is_func_descriptor)
5696 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5697 if (fdh->elf.root.type == bfd_link_hash_defined
5698 || fdh->elf.root.type == bfd_link_hash_defweak)
5699 return fdh;
5701 return NULL;
5704 /* Given H is a symbol that satisfies is_static_defined, return the
5705 value in the output file. */
5707 static bfd_vma
5708 defined_sym_val (struct elf_link_hash_entry *h)
5710 return (h->root.u.def.section->output_section->vma
5711 + h->root.u.def.section->output_offset
5712 + h->root.u.def.value);
5715 /* Return true if H matches __tls_get_addr or one of its variants. */
5717 static bool
5718 is_tls_get_addr (struct elf_link_hash_entry *h,
5719 struct ppc_link_hash_table *htab)
5721 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5722 || h == elf_hash_entry (htab->tga_desc_fd)
5723 || h == elf_hash_entry (htab->tls_get_addr)
5724 || h == elf_hash_entry (htab->tga_desc));
5727 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5729 /* Garbage collect sections, after first dealing with dot-symbols. */
5731 static bool
5732 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5734 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5736 if (htab != NULL && htab->need_func_desc_adj)
5738 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5739 htab->need_func_desc_adj = 0;
5741 return bfd_elf_gc_sections (abfd, info);
5744 /* Mark all our entry sym sections, both opd and code section. */
5746 static void
5747 ppc64_elf_gc_keep (struct bfd_link_info *info)
5749 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5750 struct bfd_sym_chain *sym;
5752 if (htab == NULL)
5753 return;
5755 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5757 struct ppc_link_hash_entry *eh, *fh;
5758 asection *sec;
5760 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5761 false, false, true));
5762 if (eh == NULL)
5763 continue;
5764 if (eh->elf.root.type != bfd_link_hash_defined
5765 && eh->elf.root.type != bfd_link_hash_defweak)
5766 continue;
5768 fh = defined_code_entry (eh);
5769 if (fh != NULL)
5771 sec = fh->elf.root.u.def.section;
5772 sec->flags |= SEC_KEEP;
5774 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5775 && opd_entry_value (eh->elf.root.u.def.section,
5776 eh->elf.root.u.def.value,
5777 &sec, NULL, false) != (bfd_vma) -1)
5778 sec->flags |= SEC_KEEP;
5780 sec = eh->elf.root.u.def.section;
5781 sec->flags |= SEC_KEEP;
5785 /* Mark sections containing dynamically referenced symbols. When
5786 building shared libraries, we must assume that any visible symbol is
5787 referenced. */
5789 static bool
5790 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5792 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5793 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5794 struct ppc_link_hash_entry *fdh;
5795 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5797 /* Dynamic linking info is on the func descriptor sym. */
5798 fdh = defined_func_desc (eh);
5799 if (fdh != NULL)
5800 eh = fdh;
5802 if ((eh->elf.root.type == bfd_link_hash_defined
5803 || eh->elf.root.type == bfd_link_hash_defweak)
5804 && (!eh->elf.start_stop
5805 || eh->elf.root.ldscript_def
5806 || !info->start_stop_gc)
5807 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5808 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5809 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5810 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5811 && (!bfd_link_executable (info)
5812 || info->gc_keep_exported
5813 || info->export_dynamic
5814 || (eh->elf.dynamic
5815 && d != NULL
5816 && (*d->match) (&d->head, NULL,
5817 eh->elf.root.root.string)))
5818 && (eh->elf.versioned >= versioned
5819 || !bfd_hide_sym_by_version (info->version_info,
5820 eh->elf.root.root.string)))))
5822 asection *code_sec;
5823 struct ppc_link_hash_entry *fh;
5825 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5827 /* Function descriptor syms cause the associated
5828 function code sym section to be marked. */
5829 fh = defined_code_entry (eh);
5830 if (fh != NULL)
5832 code_sec = fh->elf.root.u.def.section;
5833 code_sec->flags |= SEC_KEEP;
5835 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5836 && opd_entry_value (eh->elf.root.u.def.section,
5837 eh->elf.root.u.def.value,
5838 &code_sec, NULL, false) != (bfd_vma) -1)
5839 code_sec->flags |= SEC_KEEP;
5842 return true;
5845 /* Return the section that should be marked against GC for a given
5846 relocation. */
5848 static asection *
5849 ppc64_elf_gc_mark_hook (asection *sec,
5850 struct bfd_link_info *info,
5851 Elf_Internal_Rela *rel,
5852 struct elf_link_hash_entry *h,
5853 Elf_Internal_Sym *sym)
5855 asection *rsec;
5857 /* Syms return NULL if we're marking .opd, so we avoid marking all
5858 function sections, as all functions are referenced in .opd. */
5859 rsec = NULL;
5860 if (get_opd_info (sec) != NULL)
5861 return rsec;
5863 if (h != NULL)
5865 enum elf_ppc64_reloc_type r_type;
5866 struct ppc_link_hash_entry *eh, *fh, *fdh;
5868 r_type = ELF64_R_TYPE (rel->r_info);
5869 switch (r_type)
5871 case R_PPC64_GNU_VTINHERIT:
5872 case R_PPC64_GNU_VTENTRY:
5873 break;
5875 default:
5876 switch (h->root.type)
5878 case bfd_link_hash_defined:
5879 case bfd_link_hash_defweak:
5880 eh = ppc_elf_hash_entry (h);
5881 fdh = defined_func_desc (eh);
5882 if (fdh != NULL)
5884 /* -mcall-aixdesc code references the dot-symbol on
5885 a call reloc. Mark the function descriptor too
5886 against garbage collection. */
5887 fdh->elf.mark = 1;
5888 if (fdh->elf.is_weakalias)
5889 weakdef (&fdh->elf)->mark = 1;
5890 eh = fdh;
5893 /* Function descriptor syms cause the associated
5894 function code sym section to be marked. */
5895 fh = defined_code_entry (eh);
5896 if (fh != NULL)
5898 /* They also mark their opd section. */
5899 eh->elf.root.u.def.section->gc_mark = 1;
5901 rsec = fh->elf.root.u.def.section;
5903 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5904 && opd_entry_value (eh->elf.root.u.def.section,
5905 eh->elf.root.u.def.value,
5906 &rsec, NULL, false) != (bfd_vma) -1)
5907 eh->elf.root.u.def.section->gc_mark = 1;
5908 else
5909 rsec = h->root.u.def.section;
5910 break;
5912 case bfd_link_hash_common:
5913 rsec = h->root.u.c.p->section;
5914 break;
5916 default:
5917 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5921 else
5923 struct _opd_sec_data *opd;
5925 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5926 opd = get_opd_info (rsec);
5927 if (opd != NULL && opd->func_sec != NULL)
5929 rsec->gc_mark = 1;
5931 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5935 return rsec;
5938 /* The maximum size of .sfpr. */
5939 #define SFPR_MAX (218*4)
5941 struct sfpr_def_parms
5943 const char name[12];
5944 unsigned char lo, hi;
5945 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5946 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5949 /* Auto-generate _save*, _rest* functions in .sfpr.
5950 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5951 instead. */
5953 static bool
5954 sfpr_define (struct bfd_link_info *info,
5955 const struct sfpr_def_parms *parm,
5956 asection *stub_sec)
5958 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5959 unsigned int i;
5960 size_t len = strlen (parm->name);
5961 bool writing = false;
5962 char sym[16];
5964 if (htab == NULL)
5965 return false;
5967 memcpy (sym, parm->name, len);
5968 sym[len + 2] = 0;
5970 for (i = parm->lo; i <= parm->hi; i++)
5972 struct ppc_link_hash_entry *h;
5974 sym[len + 0] = i / 10 + '0';
5975 sym[len + 1] = i % 10 + '0';
5976 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
5977 writing, true, true));
5978 if (stub_sec != NULL)
5980 if (h != NULL
5981 && h->elf.root.type == bfd_link_hash_defined
5982 && h->elf.root.u.def.section == htab->sfpr)
5984 struct elf_link_hash_entry *s;
5985 char buf[32];
5986 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5987 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
5988 if (s == NULL)
5989 return false;
5990 if (s->root.type == bfd_link_hash_new)
5992 s->root.type = bfd_link_hash_defined;
5993 s->root.u.def.section = stub_sec;
5994 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5995 + h->elf.root.u.def.value);
5996 s->ref_regular = 1;
5997 s->def_regular = 1;
5998 s->ref_regular_nonweak = 1;
5999 s->forced_local = 1;
6000 s->non_elf = 0;
6001 s->root.linker_def = 1;
6004 continue;
6006 if (h != NULL)
6008 h->save_res = 1;
6009 if (!h->elf.def_regular)
6011 h->elf.root.type = bfd_link_hash_defined;
6012 h->elf.root.u.def.section = htab->sfpr;
6013 h->elf.root.u.def.value = htab->sfpr->size;
6014 h->elf.type = STT_FUNC;
6015 h->elf.def_regular = 1;
6016 h->elf.non_elf = 0;
6017 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6018 writing = true;
6019 if (htab->sfpr->contents == NULL)
6021 htab->sfpr->contents
6022 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6023 if (htab->sfpr->contents == NULL)
6024 return false;
6028 if (writing)
6030 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6031 if (i != parm->hi)
6032 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6033 else
6034 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6035 htab->sfpr->size = p - htab->sfpr->contents;
6039 return true;
6042 static bfd_byte *
6043 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6045 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6046 return p + 4;
6049 static bfd_byte *
6050 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6052 p = savegpr0 (abfd, p, r);
6053 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6054 p = p + 4;
6055 bfd_put_32 (abfd, BLR, p);
6056 return p + 4;
6059 static bfd_byte *
6060 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6062 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6063 return p + 4;
6066 static bfd_byte *
6067 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6069 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6070 p = p + 4;
6071 p = restgpr0 (abfd, p, r);
6072 bfd_put_32 (abfd, MTLR_R0, p);
6073 p = p + 4;
6074 if (r == 29)
6076 p = restgpr0 (abfd, p, 30);
6077 p = restgpr0 (abfd, p, 31);
6079 bfd_put_32 (abfd, BLR, p);
6080 return p + 4;
6083 static bfd_byte *
6084 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6086 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6087 return p + 4;
6090 static bfd_byte *
6091 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6093 p = savegpr1 (abfd, p, r);
6094 bfd_put_32 (abfd, BLR, p);
6095 return p + 4;
6098 static bfd_byte *
6099 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6101 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6102 return p + 4;
6105 static bfd_byte *
6106 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6108 p = restgpr1 (abfd, p, r);
6109 bfd_put_32 (abfd, BLR, p);
6110 return p + 4;
6113 static bfd_byte *
6114 savefpr (bfd *abfd, bfd_byte *p, int r)
6116 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6117 return p + 4;
6120 static bfd_byte *
6121 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6123 p = savefpr (abfd, p, r);
6124 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6125 p = p + 4;
6126 bfd_put_32 (abfd, BLR, p);
6127 return p + 4;
6130 static bfd_byte *
6131 restfpr (bfd *abfd, bfd_byte *p, int r)
6133 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6134 return p + 4;
6137 static bfd_byte *
6138 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6140 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6141 p = p + 4;
6142 p = restfpr (abfd, p, r);
6143 bfd_put_32 (abfd, MTLR_R0, p);
6144 p = p + 4;
6145 if (r == 29)
6147 p = restfpr (abfd, p, 30);
6148 p = restfpr (abfd, p, 31);
6150 bfd_put_32 (abfd, BLR, p);
6151 return p + 4;
6154 static bfd_byte *
6155 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6157 p = savefpr (abfd, p, r);
6158 bfd_put_32 (abfd, BLR, p);
6159 return p + 4;
6162 static bfd_byte *
6163 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6165 p = restfpr (abfd, p, r);
6166 bfd_put_32 (abfd, BLR, p);
6167 return p + 4;
6170 static bfd_byte *
6171 savevr (bfd *abfd, bfd_byte *p, int r)
6173 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6174 p = p + 4;
6175 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6176 return p + 4;
6179 static bfd_byte *
6180 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6182 p = savevr (abfd, p, r);
6183 bfd_put_32 (abfd, BLR, p);
6184 return p + 4;
6187 static bfd_byte *
6188 restvr (bfd *abfd, bfd_byte *p, int r)
6190 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6191 p = p + 4;
6192 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6193 return p + 4;
6196 static bfd_byte *
6197 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6199 p = restvr (abfd, p, r);
6200 bfd_put_32 (abfd, BLR, p);
6201 return p + 4;
6204 #define STDU_R1_0R1 0xf8210001
6205 #define ADDI_R1_R1 0x38210000
6207 /* Emit prologue of wrapper preserving regs around a call to
6208 __tls_get_addr_opt. */
6210 static bfd_byte *
6211 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6213 unsigned int i;
6215 bfd_put_32 (obfd, MFLR_R0, p);
6216 p += 4;
6217 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6218 p += 4;
6220 if (htab->opd_abi)
6222 for (i = 4; i < 12; i++)
6224 bfd_put_32 (obfd,
6225 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6226 p += 4;
6228 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6229 p += 4;
6231 else
6233 for (i = 4; i < 12; i++)
6235 bfd_put_32 (obfd,
6236 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6237 p += 4;
6239 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6240 p += 4;
6242 return p;
6245 /* Emit epilogue of wrapper preserving regs around a call to
6246 __tls_get_addr_opt. */
6248 static bfd_byte *
6249 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6251 unsigned int i;
6253 if (htab->opd_abi)
6255 for (i = 4; i < 12; i++)
6257 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6258 p += 4;
6260 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6261 p += 4;
6263 else
6265 for (i = 4; i < 12; i++)
6267 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6268 p += 4;
6270 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6271 p += 4;
6273 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6274 p += 4;
6275 bfd_put_32 (obfd, MTLR_R0, p);
6276 p += 4;
6277 bfd_put_32 (obfd, BLR, p);
6278 p += 4;
6279 return p;
6282 /* Called via elf_link_hash_traverse to transfer dynamic linking
6283 information on function code symbol entries to their corresponding
6284 function descriptor symbol entries. Must not be called twice for
6285 any given code symbol. */
6287 static bool
6288 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6290 struct bfd_link_info *info;
6291 struct ppc_link_hash_table *htab;
6292 struct ppc_link_hash_entry *fh;
6293 struct ppc_link_hash_entry *fdh;
6294 bool force_local;
6296 fh = ppc_elf_hash_entry (h);
6297 if (fh->elf.root.type == bfd_link_hash_indirect)
6298 return true;
6300 if (!fh->is_func)
6301 return true;
6303 if (fh->elf.root.root.string[0] != '.'
6304 || fh->elf.root.root.string[1] == '\0')
6305 return true;
6307 info = inf;
6308 htab = ppc_hash_table (info);
6309 if (htab == NULL)
6310 return false;
6312 /* Find the corresponding function descriptor symbol. */
6313 fdh = lookup_fdh (fh, htab);
6315 /* Resolve undefined references to dot-symbols as the value
6316 in the function descriptor, if we have one in a regular object.
6317 This is to satisfy cases like ".quad .foo". Calls to functions
6318 in dynamic objects are handled elsewhere. */
6319 if ((fh->elf.root.type == bfd_link_hash_undefined
6320 || fh->elf.root.type == bfd_link_hash_undefweak)
6321 && (fdh->elf.root.type == bfd_link_hash_defined
6322 || fdh->elf.root.type == bfd_link_hash_defweak)
6323 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6324 && opd_entry_value (fdh->elf.root.u.def.section,
6325 fdh->elf.root.u.def.value,
6326 &fh->elf.root.u.def.section,
6327 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6329 fh->elf.root.type = fdh->elf.root.type;
6330 fh->elf.forced_local = 1;
6331 fh->elf.def_regular = fdh->elf.def_regular;
6332 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6335 if (!fh->elf.dynamic)
6337 struct plt_entry *ent;
6339 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6340 if (ent->plt.refcount > 0)
6341 break;
6342 if (ent == NULL)
6344 if (fdh != NULL && fdh->fake)
6345 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6346 return true;
6350 /* Create a descriptor as undefined if necessary. */
6351 if (fdh == NULL
6352 && !bfd_link_executable (info)
6353 && (fh->elf.root.type == bfd_link_hash_undefined
6354 || fh->elf.root.type == bfd_link_hash_undefweak))
6356 fdh = make_fdh (info, fh);
6357 if (fdh == NULL)
6358 return false;
6361 /* We can't support overriding of symbols on a fake descriptor. */
6362 if (fdh != NULL
6363 && fdh->fake
6364 && (fh->elf.root.type == bfd_link_hash_defined
6365 || fh->elf.root.type == bfd_link_hash_defweak))
6366 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6368 /* Transfer dynamic linking information to the function descriptor. */
6369 if (fdh != NULL)
6371 fdh->elf.ref_regular |= fh->elf.ref_regular;
6372 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6373 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6374 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6375 fdh->elf.dynamic |= fh->elf.dynamic;
6376 fdh->elf.needs_plt |= (fh->elf.needs_plt
6377 || fh->elf.type == STT_FUNC
6378 || fh->elf.type == STT_GNU_IFUNC);
6379 move_plt_plist (fh, fdh);
6381 if (!fdh->elf.forced_local
6382 && fh->elf.dynindx != -1)
6383 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6384 return false;
6387 /* Now that the info is on the function descriptor, clear the
6388 function code sym info. Any function code syms for which we
6389 don't have a definition in a regular file, we force local.
6390 This prevents a shared library from exporting syms that have
6391 been imported from another library. Function code syms that
6392 are really in the library we must leave global to prevent the
6393 linker dragging in a definition from a static library. */
6394 force_local = (!fh->elf.def_regular
6395 || fdh == NULL
6396 || !fdh->elf.def_regular
6397 || fdh->elf.forced_local);
6398 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6400 return true;
6403 static const struct sfpr_def_parms save_res_funcs[] =
6405 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6406 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6407 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6408 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6409 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6410 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6411 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6412 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6413 { "._savef", 14, 31, savefpr, savefpr1_tail },
6414 { "._restf", 14, 31, restfpr, restfpr1_tail },
6415 { "_savevr_", 20, 31, savevr, savevr_tail },
6416 { "_restvr_", 20, 31, restvr, restvr_tail }
6419 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6420 this hook to a) run the edit functions in this file, b) provide
6421 some gcc support functions, and c) transfer dynamic linking
6422 information gathered so far on function code symbol entries, to
6423 their corresponding function descriptor symbol entries. */
6425 static bool
6426 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6428 struct ppc_link_hash_table *htab;
6430 htab = ppc_hash_table (info);
6431 if (htab == NULL)
6432 return false;
6434 /* Call back into the linker, which then runs the edit functions. */
6435 htab->params->edit ();
6437 /* Provide any missing _save* and _rest* functions. */
6438 if (htab->sfpr != NULL)
6440 unsigned int i;
6442 htab->sfpr->size = 0;
6443 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6444 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6445 return false;
6446 if (htab->sfpr->size == 0)
6447 htab->sfpr->flags |= SEC_EXCLUDE;
6450 if (bfd_link_relocatable (info))
6451 return true;
6453 if (htab->elf.hgot != NULL)
6455 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6456 /* Make .TOC. defined so as to prevent it being made dynamic.
6457 The wrong value here is fixed later in ppc64_elf_set_toc. */
6458 if (!htab->elf.hgot->def_regular
6459 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6461 htab->elf.hgot->root.type = bfd_link_hash_defined;
6462 htab->elf.hgot->root.u.def.value = 0;
6463 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6464 htab->elf.hgot->def_regular = 1;
6465 htab->elf.hgot->root.linker_def = 1;
6467 htab->elf.hgot->type = STT_OBJECT;
6468 htab->elf.hgot->other
6469 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6472 return true;
6475 /* Return true if we have dynamic relocs against H or any of its weak
6476 aliases, that apply to read-only sections. Cannot be used after
6477 size_dynamic_sections. */
6479 static bool
6480 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6482 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6485 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6486 return true;
6487 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6489 while (eh != NULL && &eh->elf != h);
6491 return false;
6494 /* Return whether EH has pc-relative dynamic relocs. */
6496 static bool
6497 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6499 struct elf_dyn_relocs *p;
6501 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
6502 if (p->pc_count != 0)
6503 return true;
6504 return false;
6507 /* Return true if a global entry stub will be created for H. Valid
6508 for ELFv2 before plt entries have been allocated. */
6510 static bool
6511 global_entry_stub (struct elf_link_hash_entry *h)
6513 struct plt_entry *pent;
6515 if (!h->pointer_equality_needed
6516 || h->def_regular)
6517 return false;
6519 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6520 if (pent->plt.refcount > 0
6521 && pent->addend == 0)
6522 return true;
6524 return false;
6527 /* Adjust a symbol defined by a dynamic object and referenced by a
6528 regular object. The current definition is in some section of the
6529 dynamic object, but we're not including those sections. We have to
6530 change the definition to something the rest of the link can
6531 understand. */
6533 static bool
6534 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6535 struct elf_link_hash_entry *h)
6537 struct ppc_link_hash_table *htab;
6538 asection *s, *srel;
6540 htab = ppc_hash_table (info);
6541 if (htab == NULL)
6542 return false;
6544 /* Deal with function syms. */
6545 if (h->type == STT_FUNC
6546 || h->type == STT_GNU_IFUNC
6547 || h->needs_plt)
6549 bool local = (ppc_elf_hash_entry (h)->save_res
6550 || SYMBOL_CALLS_LOCAL (info, h)
6551 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6552 /* Discard dyn_relocs when non-pic if we've decided that a
6553 function symbol is local and not an ifunc. We keep dynamic
6554 relocs for ifuncs when local rather than always emitting a
6555 plt call stub for them and defining the symbol on the call
6556 stub. We can't do that for ELFv1 anyway (a function symbol
6557 is defined on a descriptor, not code) and it can be faster at
6558 run-time due to not needing to bounce through a stub. The
6559 dyn_relocs for ifuncs will be applied even in a static
6560 executable. */
6561 if (!bfd_link_pic (info)
6562 && h->type != STT_GNU_IFUNC
6563 && local)
6564 h->dyn_relocs = NULL;
6566 /* Clear procedure linkage table information for any symbol that
6567 won't need a .plt entry. */
6568 struct plt_entry *ent;
6569 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6570 if (ent->plt.refcount > 0)
6571 break;
6572 if (ent == NULL
6573 || (h->type != STT_GNU_IFUNC
6574 && local
6575 && (htab->can_convert_all_inline_plt
6576 || (ppc_elf_hash_entry (h)->tls_mask
6577 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6579 h->plt.plist = NULL;
6580 h->needs_plt = 0;
6581 h->pointer_equality_needed = 0;
6583 else if (abiversion (info->output_bfd) >= 2)
6585 /* Taking a function's address in a read/write section
6586 doesn't require us to define the function symbol in the
6587 executable on a global entry stub. A dynamic reloc can
6588 be used instead. The reason we prefer a few more dynamic
6589 relocs is that calling via a global entry stub costs a
6590 few more instructions, and pointer_equality_needed causes
6591 extra work in ld.so when resolving these symbols. */
6592 if (global_entry_stub (h))
6594 if (!_bfd_elf_readonly_dynrelocs (h))
6596 h->pointer_equality_needed = 0;
6597 /* If we haven't seen a branch reloc and the symbol
6598 isn't an ifunc then we don't need a plt entry. */
6599 if (!h->needs_plt)
6600 h->plt.plist = NULL;
6602 else if (!bfd_link_pic (info))
6603 /* We are going to be defining the function symbol on the
6604 plt stub, so no dyn_relocs needed when non-pic. */
6605 h->dyn_relocs = NULL;
6608 /* ELFv2 function symbols can't have copy relocs. */
6609 return true;
6611 else if (!h->needs_plt
6612 && !_bfd_elf_readonly_dynrelocs (h))
6614 /* If we haven't seen a branch reloc and the symbol isn't an
6615 ifunc then we don't need a plt entry. */
6616 h->plt.plist = NULL;
6617 h->pointer_equality_needed = 0;
6618 return true;
6621 else
6622 h->plt.plist = NULL;
6624 /* If this is a weak symbol, and there is a real definition, the
6625 processor independent code will have arranged for us to see the
6626 real definition first, and we can just use the same value. */
6627 if (h->is_weakalias)
6629 struct elf_link_hash_entry *def = weakdef (h);
6630 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6631 h->root.u.def.section = def->root.u.def.section;
6632 h->root.u.def.value = def->root.u.def.value;
6633 if (def->root.u.def.section == htab->elf.sdynbss
6634 || def->root.u.def.section == htab->elf.sdynrelro)
6635 h->dyn_relocs = NULL;
6636 return true;
6639 /* If we are creating a shared library, we must presume that the
6640 only references to the symbol are via the global offset table.
6641 For such cases we need not do anything here; the relocations will
6642 be handled correctly by relocate_section. */
6643 if (!bfd_link_executable (info))
6644 return true;
6646 /* If there are no references to this symbol that do not use the
6647 GOT, we don't need to generate a copy reloc. */
6648 if (!h->non_got_ref)
6649 return true;
6651 /* Don't generate a copy reloc for symbols defined in the executable. */
6652 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6654 /* If -z nocopyreloc was given, don't generate them either. */
6655 || info->nocopyreloc
6657 /* If we don't find any dynamic relocs in read-only sections, then
6658 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6659 || (ELIMINATE_COPY_RELOCS
6660 && !h->needs_copy
6661 && !alias_readonly_dynrelocs (h))
6663 /* Protected variables do not work with .dynbss. The copy in
6664 .dynbss won't be used by the shared library with the protected
6665 definition for the variable. Text relocations are preferable
6666 to an incorrect program. */
6667 || h->protected_def)
6668 return true;
6670 if (h->type == STT_FUNC
6671 || h->type == STT_GNU_IFUNC)
6673 /* .dynbss copies of function symbols only work if we have
6674 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6675 use dot-symbols and set the function symbol size to the text
6676 size of the function rather than the size of the descriptor.
6677 That's wrong for copying a descriptor. */
6678 if (ppc_elf_hash_entry (h)->oh == NULL
6679 || !(h->size == 24 || h->size == 16))
6680 return true;
6682 /* We should never get here, but unfortunately there are old
6683 versions of gcc (circa gcc-3.2) that improperly for the
6684 ELFv1 ABI put initialized function pointers, vtable refs and
6685 suchlike in read-only sections. Allow them to proceed, but
6686 warn that this might break at runtime. */
6687 info->callbacks->einfo
6688 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6689 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6690 h->root.root.string);
6693 /* This is a reference to a symbol defined by a dynamic object which
6694 is not a function. */
6696 /* We must allocate the symbol in our .dynbss section, which will
6697 become part of the .bss section of the executable. There will be
6698 an entry for this symbol in the .dynsym section. The dynamic
6699 object will contain position independent code, so all references
6700 from the dynamic object to this symbol will go through the global
6701 offset table. The dynamic linker will use the .dynsym entry to
6702 determine the address it must put in the global offset table, so
6703 both the dynamic object and the regular object will refer to the
6704 same memory location for the variable. */
6705 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6707 s = htab->elf.sdynrelro;
6708 srel = htab->elf.sreldynrelro;
6710 else
6712 s = htab->elf.sdynbss;
6713 srel = htab->elf.srelbss;
6715 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6717 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6718 linker to copy the initial value out of the dynamic object
6719 and into the runtime process image. */
6720 srel->size += sizeof (Elf64_External_Rela);
6721 h->needs_copy = 1;
6724 /* We no longer want dyn_relocs. */
6725 h->dyn_relocs = NULL;
6726 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6729 /* If given a function descriptor symbol, hide both the function code
6730 sym and the descriptor. */
6731 static void
6732 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6733 struct elf_link_hash_entry *h,
6734 bool force_local)
6736 struct ppc_link_hash_entry *eh;
6737 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6739 if (ppc_hash_table (info) == NULL)
6740 return;
6742 eh = ppc_elf_hash_entry (h);
6743 if (eh->is_func_descriptor)
6745 struct ppc_link_hash_entry *fh = eh->oh;
6747 if (fh == NULL)
6749 const char *p, *q;
6750 struct elf_link_hash_table *htab = elf_hash_table (info);
6751 char save;
6753 /* We aren't supposed to use alloca in BFD because on
6754 systems which do not have alloca the version in libiberty
6755 calls xmalloc, which might cause the program to crash
6756 when it runs out of memory. This function doesn't have a
6757 return status, so there's no way to gracefully return an
6758 error. So cheat. We know that string[-1] can be safely
6759 accessed; It's either a string in an ELF string table,
6760 or allocated in an objalloc structure. */
6762 p = eh->elf.root.root.string - 1;
6763 save = *p;
6764 *(char *) p = '.';
6765 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6766 false, false));
6767 *(char *) p = save;
6769 /* Unfortunately, if it so happens that the string we were
6770 looking for was allocated immediately before this string,
6771 then we overwrote the string terminator. That's the only
6772 reason the lookup should fail. */
6773 if (fh == NULL)
6775 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6776 while (q >= eh->elf.root.root.string && *q == *p)
6777 --q, --p;
6778 if (q < eh->elf.root.root.string && *p == '.')
6779 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6780 false, false));
6782 if (fh != NULL)
6784 eh->oh = fh;
6785 fh->oh = eh;
6788 if (fh != NULL)
6789 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6793 static bool
6794 get_sym_h (struct elf_link_hash_entry **hp,
6795 Elf_Internal_Sym **symp,
6796 asection **symsecp,
6797 unsigned char **tls_maskp,
6798 Elf_Internal_Sym **locsymsp,
6799 unsigned long r_symndx,
6800 bfd *ibfd)
6802 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6804 if (r_symndx >= symtab_hdr->sh_info)
6806 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6807 struct elf_link_hash_entry *h;
6809 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6810 h = elf_follow_link (h);
6812 if (hp != NULL)
6813 *hp = h;
6815 if (symp != NULL)
6816 *symp = NULL;
6818 if (symsecp != NULL)
6820 asection *symsec = NULL;
6821 if (h->root.type == bfd_link_hash_defined
6822 || h->root.type == bfd_link_hash_defweak)
6823 symsec = h->root.u.def.section;
6824 *symsecp = symsec;
6827 if (tls_maskp != NULL)
6828 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6830 else
6832 Elf_Internal_Sym *sym;
6833 Elf_Internal_Sym *locsyms = *locsymsp;
6835 if (locsyms == NULL)
6837 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6838 if (locsyms == NULL)
6839 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6840 symtab_hdr->sh_info,
6841 0, NULL, NULL, NULL);
6842 if (locsyms == NULL)
6843 return false;
6844 *locsymsp = locsyms;
6846 sym = locsyms + r_symndx;
6848 if (hp != NULL)
6849 *hp = NULL;
6851 if (symp != NULL)
6852 *symp = sym;
6854 if (symsecp != NULL)
6855 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6857 if (tls_maskp != NULL)
6859 struct got_entry **lgot_ents;
6860 unsigned char *tls_mask;
6862 tls_mask = NULL;
6863 lgot_ents = elf_local_got_ents (ibfd);
6864 if (lgot_ents != NULL)
6866 struct plt_entry **local_plt = (struct plt_entry **)
6867 (lgot_ents + symtab_hdr->sh_info);
6868 unsigned char *lgot_masks = (unsigned char *)
6869 (local_plt + symtab_hdr->sh_info);
6870 tls_mask = &lgot_masks[r_symndx];
6872 *tls_maskp = tls_mask;
6875 return true;
6878 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6879 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6880 type suitable for optimization, and 1 otherwise. */
6882 static int
6883 get_tls_mask (unsigned char **tls_maskp,
6884 unsigned long *toc_symndx,
6885 bfd_vma *toc_addend,
6886 Elf_Internal_Sym **locsymsp,
6887 const Elf_Internal_Rela *rel,
6888 bfd *ibfd)
6890 unsigned long r_symndx;
6891 int next_r;
6892 struct elf_link_hash_entry *h;
6893 Elf_Internal_Sym *sym;
6894 asection *sec;
6895 bfd_vma off;
6897 r_symndx = ELF64_R_SYM (rel->r_info);
6898 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6899 return 0;
6901 if ((*tls_maskp != NULL
6902 && (**tls_maskp & TLS_TLS) != 0
6903 && **tls_maskp != (TLS_TLS | TLS_MARK))
6904 || sec == NULL
6905 || ppc64_elf_section_data (sec) == NULL
6906 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6907 return 1;
6909 /* Look inside a TOC section too. */
6910 if (h != NULL)
6912 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6913 off = h->root.u.def.value;
6915 else
6916 off = sym->st_value;
6917 off += rel->r_addend;
6918 BFD_ASSERT (off % 8 == 0);
6919 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6920 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6921 if (toc_symndx != NULL)
6922 *toc_symndx = r_symndx;
6923 if (toc_addend != NULL)
6924 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6925 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6926 return 0;
6927 if ((h == NULL || is_static_defined (h))
6928 && (next_r == -1 || next_r == -2))
6929 return 1 - next_r;
6930 return 1;
6933 /* Find (or create) an entry in the tocsave hash table. */
6935 static struct tocsave_entry *
6936 tocsave_find (struct ppc_link_hash_table *htab,
6937 enum insert_option insert,
6938 Elf_Internal_Sym **local_syms,
6939 const Elf_Internal_Rela *irela,
6940 bfd *ibfd)
6942 unsigned long r_indx;
6943 struct elf_link_hash_entry *h;
6944 Elf_Internal_Sym *sym;
6945 struct tocsave_entry ent, *p;
6946 hashval_t hash;
6947 struct tocsave_entry **slot;
6949 r_indx = ELF64_R_SYM (irela->r_info);
6950 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6951 return NULL;
6952 if (ent.sec == NULL || ent.sec->output_section == NULL)
6954 _bfd_error_handler
6955 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6956 return NULL;
6959 if (h != NULL)
6960 ent.offset = h->root.u.def.value;
6961 else
6962 ent.offset = sym->st_value;
6963 ent.offset += irela->r_addend;
6965 hash = tocsave_htab_hash (&ent);
6966 slot = ((struct tocsave_entry **)
6967 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6968 if (slot == NULL)
6969 return NULL;
6971 if (*slot == NULL)
6973 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6974 if (p == NULL)
6975 return NULL;
6976 *p = ent;
6977 *slot = p;
6979 return *slot;
6982 /* Adjust all global syms defined in opd sections. In gcc generated
6983 code for the old ABI, these will already have been done. */
6985 static bool
6986 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6988 struct ppc_link_hash_entry *eh;
6989 asection *sym_sec;
6990 struct _opd_sec_data *opd;
6992 if (h->root.type == bfd_link_hash_indirect)
6993 return true;
6995 if (h->root.type != bfd_link_hash_defined
6996 && h->root.type != bfd_link_hash_defweak)
6997 return true;
6999 eh = ppc_elf_hash_entry (h);
7000 if (eh->adjust_done)
7001 return true;
7003 sym_sec = eh->elf.root.u.def.section;
7004 opd = get_opd_info (sym_sec);
7005 if (opd != NULL && opd->adjust != NULL)
7007 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7008 if (adjust == -1)
7010 /* This entry has been deleted. */
7011 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7012 if (dsec == NULL)
7014 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7015 if (discarded_section (dsec))
7017 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7018 break;
7021 eh->elf.root.u.def.value = 0;
7022 eh->elf.root.u.def.section = dsec;
7024 else
7025 eh->elf.root.u.def.value += adjust;
7026 eh->adjust_done = 1;
7028 return true;
7031 /* Handles decrementing dynamic reloc counts for the reloc specified by
7032 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7033 have already been determined. */
7035 static bool
7036 dec_dynrel_count (bfd_vma r_info,
7037 asection *sec,
7038 struct bfd_link_info *info,
7039 Elf_Internal_Sym **local_syms,
7040 struct elf_link_hash_entry *h,
7041 Elf_Internal_Sym *sym)
7043 enum elf_ppc64_reloc_type r_type;
7044 asection *sym_sec = NULL;
7046 /* Can this reloc be dynamic? This switch, and later tests here
7047 should be kept in sync with the code in check_relocs. */
7048 r_type = ELF64_R_TYPE (r_info);
7049 switch (r_type)
7051 default:
7052 return true;
7054 case R_PPC64_TOC16:
7055 case R_PPC64_TOC16_DS:
7056 case R_PPC64_TOC16_LO:
7057 case R_PPC64_TOC16_HI:
7058 case R_PPC64_TOC16_HA:
7059 case R_PPC64_TOC16_LO_DS:
7060 if (h == NULL)
7061 return true;
7062 break;
7064 case R_PPC64_TPREL16:
7065 case R_PPC64_TPREL16_LO:
7066 case R_PPC64_TPREL16_HI:
7067 case R_PPC64_TPREL16_HA:
7068 case R_PPC64_TPREL16_DS:
7069 case R_PPC64_TPREL16_LO_DS:
7070 case R_PPC64_TPREL16_HIGH:
7071 case R_PPC64_TPREL16_HIGHA:
7072 case R_PPC64_TPREL16_HIGHER:
7073 case R_PPC64_TPREL16_HIGHERA:
7074 case R_PPC64_TPREL16_HIGHEST:
7075 case R_PPC64_TPREL16_HIGHESTA:
7076 case R_PPC64_TPREL64:
7077 case R_PPC64_TPREL34:
7078 case R_PPC64_DTPMOD64:
7079 case R_PPC64_DTPREL64:
7080 case R_PPC64_ADDR64:
7081 case R_PPC64_REL30:
7082 case R_PPC64_REL32:
7083 case R_PPC64_REL64:
7084 case R_PPC64_ADDR14:
7085 case R_PPC64_ADDR14_BRNTAKEN:
7086 case R_PPC64_ADDR14_BRTAKEN:
7087 case R_PPC64_ADDR16:
7088 case R_PPC64_ADDR16_DS:
7089 case R_PPC64_ADDR16_HA:
7090 case R_PPC64_ADDR16_HI:
7091 case R_PPC64_ADDR16_HIGH:
7092 case R_PPC64_ADDR16_HIGHA:
7093 case R_PPC64_ADDR16_HIGHER:
7094 case R_PPC64_ADDR16_HIGHERA:
7095 case R_PPC64_ADDR16_HIGHEST:
7096 case R_PPC64_ADDR16_HIGHESTA:
7097 case R_PPC64_ADDR16_LO:
7098 case R_PPC64_ADDR16_LO_DS:
7099 case R_PPC64_ADDR24:
7100 case R_PPC64_ADDR32:
7101 case R_PPC64_UADDR16:
7102 case R_PPC64_UADDR32:
7103 case R_PPC64_UADDR64:
7104 case R_PPC64_TOC:
7105 case R_PPC64_D34:
7106 case R_PPC64_D34_LO:
7107 case R_PPC64_D34_HI30:
7108 case R_PPC64_D34_HA30:
7109 case R_PPC64_ADDR16_HIGHER34:
7110 case R_PPC64_ADDR16_HIGHERA34:
7111 case R_PPC64_ADDR16_HIGHEST34:
7112 case R_PPC64_ADDR16_HIGHESTA34:
7113 case R_PPC64_D28:
7114 break;
7117 if (local_syms != NULL)
7119 unsigned long r_symndx;
7120 bfd *ibfd = sec->owner;
7122 r_symndx = ELF64_R_SYM (r_info);
7123 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7124 return false;
7127 if ((h != NULL
7128 && (h->root.type == bfd_link_hash_defweak
7129 || !h->def_regular))
7130 || (h != NULL
7131 && !bfd_link_executable (info)
7132 && !SYMBOLIC_BIND (info, h))
7133 || (bfd_link_pic (info)
7134 && must_be_dyn_reloc (info, r_type))
7135 || (!bfd_link_pic (info)
7136 && (h != NULL
7137 ? h->type == STT_GNU_IFUNC
7138 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7140 else
7141 return true;
7143 if (h != NULL)
7145 struct elf_dyn_relocs *p;
7146 struct elf_dyn_relocs **pp;
7147 pp = &h->dyn_relocs;
7149 /* elf_gc_sweep may have already removed all dyn relocs associated
7150 with local syms for a given section. Also, symbol flags are
7151 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7152 report a dynreloc miscount. */
7153 if (*pp == NULL && info->gc_sections)
7154 return true;
7156 while ((p = *pp) != NULL)
7158 if (p->sec == sec)
7160 if (!must_be_dyn_reloc (info, r_type))
7161 p->pc_count -= 1;
7162 p->count -= 1;
7163 if (p->count == 0)
7164 *pp = p->next;
7165 return true;
7167 pp = &p->next;
7170 else
7172 struct ppc_dyn_relocs *p;
7173 struct ppc_dyn_relocs **pp;
7174 void *vpp;
7175 bool is_ifunc;
7177 if (local_syms == NULL)
7178 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7179 if (sym_sec == NULL)
7180 sym_sec = sec;
7182 vpp = &elf_section_data (sym_sec)->local_dynrel;
7183 pp = (struct ppc_dyn_relocs **) vpp;
7185 if (*pp == NULL && info->gc_sections)
7186 return true;
7188 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7189 while ((p = *pp) != NULL)
7191 if (p->sec == sec && p->ifunc == is_ifunc)
7193 p->count -= 1;
7194 if (p->count == 0)
7195 *pp = p->next;
7196 return true;
7198 pp = &p->next;
7202 /* xgettext:c-format */
7203 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7204 sec->owner, sec);
7205 bfd_set_error (bfd_error_bad_value);
7206 return false;
7209 /* Remove unused Official Procedure Descriptor entries. Currently we
7210 only remove those associated with functions in discarded link-once
7211 sections, or weakly defined functions that have been overridden. It
7212 would be possible to remove many more entries for statically linked
7213 applications. */
7215 bool
7216 ppc64_elf_edit_opd (struct bfd_link_info *info)
7218 bfd *ibfd;
7219 bool some_edited = false;
7220 asection *need_pad = NULL;
7221 struct ppc_link_hash_table *htab;
7223 htab = ppc_hash_table (info);
7224 if (htab == NULL)
7225 return false;
7227 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7229 asection *sec;
7230 Elf_Internal_Rela *relstart, *rel, *relend;
7231 Elf_Internal_Shdr *symtab_hdr;
7232 Elf_Internal_Sym *local_syms;
7233 struct _opd_sec_data *opd;
7234 bool need_edit, add_aux_fields, broken;
7235 bfd_size_type cnt_16b = 0;
7237 if (!is_ppc64_elf (ibfd))
7238 continue;
7240 sec = bfd_get_section_by_name (ibfd, ".opd");
7241 if (sec == NULL || sec->size == 0)
7242 continue;
7244 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7245 continue;
7247 if (sec->output_section == bfd_abs_section_ptr)
7248 continue;
7250 /* Look through the section relocs. */
7251 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7252 continue;
7254 local_syms = NULL;
7255 symtab_hdr = &elf_symtab_hdr (ibfd);
7257 /* Read the relocations. */
7258 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7259 info->keep_memory);
7260 if (relstart == NULL)
7261 return false;
7263 /* First run through the relocs to check they are sane, and to
7264 determine whether we need to edit this opd section. */
7265 need_edit = false;
7266 broken = false;
7267 need_pad = sec;
7268 relend = relstart + sec->reloc_count;
7269 for (rel = relstart; rel < relend; )
7271 enum elf_ppc64_reloc_type r_type;
7272 unsigned long r_symndx;
7273 asection *sym_sec;
7274 struct elf_link_hash_entry *h;
7275 Elf_Internal_Sym *sym;
7276 bfd_vma offset;
7278 /* .opd contains an array of 16 or 24 byte entries. We're
7279 only interested in the reloc pointing to a function entry
7280 point. */
7281 offset = rel->r_offset;
7282 if (rel + 1 == relend
7283 || rel[1].r_offset != offset + 8)
7285 /* If someone messes with .opd alignment then after a
7286 "ld -r" we might have padding in the middle of .opd.
7287 Also, there's nothing to prevent someone putting
7288 something silly in .opd with the assembler. No .opd
7289 optimization for them! */
7290 broken_opd:
7291 _bfd_error_handler
7292 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7293 broken = true;
7294 break;
7297 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7298 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7300 _bfd_error_handler
7301 /* xgettext:c-format */
7302 (_("%pB: unexpected reloc type %u in .opd section"),
7303 ibfd, r_type);
7304 broken = true;
7305 break;
7308 r_symndx = ELF64_R_SYM (rel->r_info);
7309 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7310 r_symndx, ibfd))
7311 goto error_ret;
7313 if (sym_sec == NULL || sym_sec->owner == NULL)
7315 const char *sym_name;
7316 if (h != NULL)
7317 sym_name = h->root.root.string;
7318 else
7319 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7320 sym_sec);
7322 _bfd_error_handler
7323 /* xgettext:c-format */
7324 (_("%pB: undefined sym `%s' in .opd section"),
7325 ibfd, sym_name);
7326 broken = true;
7327 break;
7330 /* opd entries are always for functions defined in the
7331 current input bfd. If the symbol isn't defined in the
7332 input bfd, then we won't be using the function in this
7333 bfd; It must be defined in a linkonce section in another
7334 bfd, or is weak. It's also possible that we are
7335 discarding the function due to a linker script /DISCARD/,
7336 which we test for via the output_section. */
7337 if (sym_sec->owner != ibfd
7338 || sym_sec->output_section == bfd_abs_section_ptr)
7339 need_edit = true;
7341 rel += 2;
7342 if (rel + 1 == relend
7343 || (rel + 2 < relend
7344 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7345 ++rel;
7347 if (rel == relend)
7349 if (sec->size == offset + 24)
7351 need_pad = NULL;
7352 break;
7354 if (sec->size == offset + 16)
7356 cnt_16b++;
7357 break;
7359 goto broken_opd;
7361 else if (rel + 1 < relend
7362 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7363 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7365 if (rel[0].r_offset == offset + 16)
7366 cnt_16b++;
7367 else if (rel[0].r_offset != offset + 24)
7368 goto broken_opd;
7370 else
7371 goto broken_opd;
7374 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7376 if (!broken && (need_edit || add_aux_fields))
7378 Elf_Internal_Rela *write_rel;
7379 Elf_Internal_Shdr *rel_hdr;
7380 bfd_byte *rptr, *wptr;
7381 bfd_byte *new_contents;
7382 bfd_size_type amt;
7384 new_contents = NULL;
7385 amt = OPD_NDX (sec->size) * sizeof (long);
7386 opd = &ppc64_elf_section_data (sec)->u.opd;
7387 opd->adjust = bfd_zalloc (sec->owner, amt);
7388 if (opd->adjust == NULL)
7389 return false;
7391 /* This seems a waste of time as input .opd sections are all
7392 zeros as generated by gcc, but I suppose there's no reason
7393 this will always be so. We might start putting something in
7394 the third word of .opd entries. */
7395 if ((sec->flags & SEC_IN_MEMORY) == 0)
7397 bfd_byte *loc;
7398 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7400 free (loc);
7401 error_ret:
7402 if (symtab_hdr->contents != (unsigned char *) local_syms)
7403 free (local_syms);
7404 if (elf_section_data (sec)->relocs != relstart)
7405 free (relstart);
7406 return false;
7408 sec->contents = loc;
7409 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7412 elf_section_data (sec)->relocs = relstart;
7414 new_contents = sec->contents;
7415 if (add_aux_fields)
7417 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7418 if (new_contents == NULL)
7419 return false;
7420 need_pad = NULL;
7422 wptr = new_contents;
7423 rptr = sec->contents;
7424 write_rel = relstart;
7425 for (rel = relstart; rel < relend; )
7427 unsigned long r_symndx;
7428 asection *sym_sec;
7429 struct elf_link_hash_entry *h;
7430 struct ppc_link_hash_entry *fdh = NULL;
7431 Elf_Internal_Sym *sym;
7432 long opd_ent_size;
7433 Elf_Internal_Rela *next_rel;
7434 bool skip;
7436 r_symndx = ELF64_R_SYM (rel->r_info);
7437 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7438 r_symndx, ibfd))
7439 goto error_ret;
7441 next_rel = rel + 2;
7442 if (next_rel + 1 == relend
7443 || (next_rel + 2 < relend
7444 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7445 ++next_rel;
7447 /* See if the .opd entry is full 24 byte or
7448 16 byte (with fd_aux entry overlapped with next
7449 fd_func). */
7450 opd_ent_size = 24;
7451 if (next_rel == relend)
7453 if (sec->size == rel->r_offset + 16)
7454 opd_ent_size = 16;
7456 else if (next_rel->r_offset == rel->r_offset + 16)
7457 opd_ent_size = 16;
7459 if (h != NULL
7460 && h->root.root.string[0] == '.')
7462 fdh = ppc_elf_hash_entry (h)->oh;
7463 if (fdh != NULL)
7465 fdh = ppc_follow_link (fdh);
7466 if (fdh->elf.root.type != bfd_link_hash_defined
7467 && fdh->elf.root.type != bfd_link_hash_defweak)
7468 fdh = NULL;
7472 skip = (sym_sec->owner != ibfd
7473 || sym_sec->output_section == bfd_abs_section_ptr);
7474 if (skip)
7476 if (fdh != NULL && sym_sec->owner == ibfd)
7478 /* Arrange for the function descriptor sym
7479 to be dropped. */
7480 fdh->elf.root.u.def.value = 0;
7481 fdh->elf.root.u.def.section = sym_sec;
7483 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7485 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7486 rel = next_rel;
7487 else
7488 while (1)
7490 if (!dec_dynrel_count (rel->r_info, sec, info,
7491 NULL, h, sym))
7492 goto error_ret;
7494 if (++rel == next_rel)
7495 break;
7497 r_symndx = ELF64_R_SYM (rel->r_info);
7498 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7499 r_symndx, ibfd))
7500 goto error_ret;
7503 else
7505 /* We'll be keeping this opd entry. */
7506 long adjust;
7508 if (fdh != NULL)
7510 /* Redefine the function descriptor symbol to
7511 this location in the opd section. It is
7512 necessary to update the value here rather
7513 than using an array of adjustments as we do
7514 for local symbols, because various places
7515 in the generic ELF code use the value
7516 stored in u.def.value. */
7517 fdh->elf.root.u.def.value = wptr - new_contents;
7518 fdh->adjust_done = 1;
7521 /* Local syms are a bit tricky. We could
7522 tweak them as they can be cached, but
7523 we'd need to look through the local syms
7524 for the function descriptor sym which we
7525 don't have at the moment. So keep an
7526 array of adjustments. */
7527 adjust = (wptr - new_contents) - (rptr - sec->contents);
7528 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7530 if (wptr != rptr)
7531 memcpy (wptr, rptr, opd_ent_size);
7532 wptr += opd_ent_size;
7533 if (add_aux_fields && opd_ent_size == 16)
7535 memset (wptr, '\0', 8);
7536 wptr += 8;
7539 /* We need to adjust any reloc offsets to point to the
7540 new opd entries. */
7541 for ( ; rel != next_rel; ++rel)
7543 rel->r_offset += adjust;
7544 if (write_rel != rel)
7545 memcpy (write_rel, rel, sizeof (*rel));
7546 ++write_rel;
7550 rptr += opd_ent_size;
7553 sec->size = wptr - new_contents;
7554 sec->reloc_count = write_rel - relstart;
7555 if (add_aux_fields)
7557 free (sec->contents);
7558 sec->contents = new_contents;
7561 /* Fudge the header size too, as this is used later in
7562 elf_bfd_final_link if we are emitting relocs. */
7563 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7564 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7565 some_edited = true;
7567 else if (elf_section_data (sec)->relocs != relstart)
7568 free (relstart);
7570 if (local_syms != NULL
7571 && symtab_hdr->contents != (unsigned char *) local_syms)
7573 if (!info->keep_memory)
7574 free (local_syms);
7575 else
7576 symtab_hdr->contents = (unsigned char *) local_syms;
7580 if (some_edited)
7581 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7583 /* If we are doing a final link and the last .opd entry is just 16 byte
7584 long, add a 8 byte padding after it. */
7585 if (need_pad != NULL && !bfd_link_relocatable (info))
7587 bfd_byte *p;
7589 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7591 BFD_ASSERT (need_pad->size > 0);
7593 p = bfd_malloc (need_pad->size + 8);
7594 if (p == NULL)
7595 return false;
7597 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7598 p, 0, need_pad->size))
7599 return false;
7601 need_pad->contents = p;
7602 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7604 else
7606 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7607 if (p == NULL)
7608 return false;
7610 need_pad->contents = p;
7613 memset (need_pad->contents + need_pad->size, 0, 8);
7614 need_pad->size += 8;
7617 return true;
7620 /* Analyze inline PLT call relocations to see whether calls to locally
7621 defined functions can be converted to direct calls. */
7623 bool
7624 ppc64_elf_inline_plt (struct bfd_link_info *info)
7626 struct ppc_link_hash_table *htab;
7627 bfd *ibfd;
7628 asection *sec;
7629 bfd_vma low_vma, high_vma, limit;
7631 htab = ppc_hash_table (info);
7632 if (htab == NULL)
7633 return false;
7635 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7636 reduced somewhat to cater for possible stubs that might be added
7637 between the call and its destination. */
7638 if (htab->params->group_size < 0)
7640 limit = -htab->params->group_size;
7641 if (limit == 1)
7642 limit = 0x1e00000;
7644 else
7646 limit = htab->params->group_size;
7647 if (limit == 1)
7648 limit = 0x1c00000;
7651 low_vma = -1;
7652 high_vma = 0;
7653 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7654 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7656 if (low_vma > sec->vma)
7657 low_vma = sec->vma;
7658 if (high_vma < sec->vma + sec->size)
7659 high_vma = sec->vma + sec->size;
7662 /* If a "bl" can reach anywhere in local code sections, then we can
7663 convert all inline PLT sequences to direct calls when the symbol
7664 is local. */
7665 if (high_vma - low_vma < limit)
7667 htab->can_convert_all_inline_plt = 1;
7668 return true;
7671 /* Otherwise, go looking through relocs for cases where a direct
7672 call won't reach. Mark the symbol on any such reloc to disable
7673 the optimization and keep the PLT entry as it seems likely that
7674 this will be better than creating trampolines. Note that this
7675 will disable the optimization for all inline PLT calls to a
7676 particular symbol, not just those that won't reach. The
7677 difficulty in doing a more precise optimization is that the
7678 linker needs to make a decision depending on whether a
7679 particular R_PPC64_PLTCALL insn can be turned into a direct
7680 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7681 the sequence, and there is nothing that ties those relocs
7682 together except their symbol. */
7684 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7686 Elf_Internal_Shdr *symtab_hdr;
7687 Elf_Internal_Sym *local_syms;
7689 if (!is_ppc64_elf (ibfd))
7690 continue;
7692 local_syms = NULL;
7693 symtab_hdr = &elf_symtab_hdr (ibfd);
7695 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7696 if (ppc64_elf_section_data (sec)->has_pltcall
7697 && !bfd_is_abs_section (sec->output_section))
7699 Elf_Internal_Rela *relstart, *rel, *relend;
7701 /* Read the relocations. */
7702 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7703 info->keep_memory);
7704 if (relstart == NULL)
7705 return false;
7707 relend = relstart + sec->reloc_count;
7708 for (rel = relstart; rel < relend; rel++)
7710 enum elf_ppc64_reloc_type r_type;
7711 unsigned long r_symndx;
7712 asection *sym_sec;
7713 struct elf_link_hash_entry *h;
7714 Elf_Internal_Sym *sym;
7715 unsigned char *tls_maskp;
7717 r_type = ELF64_R_TYPE (rel->r_info);
7718 if (r_type != R_PPC64_PLTCALL
7719 && r_type != R_PPC64_PLTCALL_NOTOC)
7720 continue;
7722 r_symndx = ELF64_R_SYM (rel->r_info);
7723 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7724 r_symndx, ibfd))
7726 if (elf_section_data (sec)->relocs != relstart)
7727 free (relstart);
7728 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7729 free (local_syms);
7730 return false;
7733 if (sym_sec != NULL && sym_sec->output_section != NULL)
7735 bfd_vma from, to;
7736 if (h != NULL)
7737 to = h->root.u.def.value;
7738 else
7739 to = sym->st_value;
7740 to += (rel->r_addend
7741 + sym_sec->output_offset
7742 + sym_sec->output_section->vma);
7743 from = (rel->r_offset
7744 + sec->output_offset
7745 + sec->output_section->vma);
7746 if (to - from + limit < 2 * limit
7747 && !(r_type == R_PPC64_PLTCALL_NOTOC
7748 && (((h ? h->other : sym->st_other)
7749 & STO_PPC64_LOCAL_MASK)
7750 > 1 << STO_PPC64_LOCAL_BIT)))
7751 *tls_maskp &= ~PLT_KEEP;
7754 if (elf_section_data (sec)->relocs != relstart)
7755 free (relstart);
7758 if (local_syms != NULL
7759 && symtab_hdr->contents != (unsigned char *) local_syms)
7761 if (!info->keep_memory)
7762 free (local_syms);
7763 else
7764 symtab_hdr->contents = (unsigned char *) local_syms;
7768 return true;
7771 /* Set htab->tls_get_addr and various other info specific to TLS.
7772 This needs to run before dynamic symbols are processed in
7773 bfd_elf_size_dynamic_sections. */
7775 bool
7776 ppc64_elf_tls_setup (struct bfd_link_info *info)
7778 struct ppc_link_hash_table *htab;
7779 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7781 htab = ppc_hash_table (info);
7782 if (htab == NULL)
7783 return false;
7785 /* Move dynamic linking info to the function descriptor sym. */
7786 if (htab->need_func_desc_adj)
7788 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7789 htab->need_func_desc_adj = 0;
7792 if (abiversion (info->output_bfd) == 1)
7793 htab->opd_abi = 1;
7795 if (htab->params->no_multi_toc)
7796 htab->do_multi_toc = 0;
7797 else if (!htab->do_multi_toc)
7798 htab->params->no_multi_toc = 1;
7800 /* Default to --no-plt-localentry, as this option can cause problems
7801 with symbol interposition. For example, glibc libpthread.so and
7802 libc.so duplicate many pthread symbols, with a fallback
7803 implementation in libc.so. In some cases the fallback does more
7804 work than the pthread implementation. __pthread_condattr_destroy
7805 is one such symbol: the libpthread.so implementation is
7806 localentry:0 while the libc.so implementation is localentry:8.
7807 An app that "cleverly" uses dlopen to only load necessary
7808 libraries at runtime may omit loading libpthread.so when not
7809 running multi-threaded, which then results in the libc.so
7810 fallback symbols being used and ld.so complaining. Now there
7811 are workarounds in ld (see non_zero_localentry) to detect the
7812 pthread situation, but that may not be the only case where
7813 --plt-localentry can cause trouble. */
7814 if (htab->params->plt_localentry0 < 0)
7815 htab->params->plt_localentry0 = 0;
7816 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7818 /* The issue is that __glink_PLTresolve saves r2, which is done
7819 because glibc ld.so _dl_runtime_resolve restores r2 to support
7820 a glibc plt call optimisation where global entry code is
7821 skipped on calls that resolve to the same binary. The
7822 __glink_PLTresolve save of r2 is incompatible with code
7823 making tail calls, because the tail call might go via the
7824 resolver and thus overwrite the proper saved r2. */
7825 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7826 "power10 pc-relative code"));
7827 htab->params->plt_localentry0 = 0;
7829 if (htab->params->plt_localentry0
7830 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7831 false, false, false) == NULL)
7832 _bfd_error_handler
7833 (_("warning: --plt-localentry is especially dangerous without "
7834 "ld.so support to detect ABI violations"));
7836 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7837 false, false, true);
7838 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7839 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7840 false, false, true);
7841 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7843 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7844 false, false, true);
7845 htab->tga_desc = ppc_elf_hash_entry (desc);
7846 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7847 false, false, true);
7848 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7850 if (htab->params->tls_get_addr_opt)
7852 struct elf_link_hash_entry *opt, *opt_fd;
7854 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7855 false, false, true);
7856 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7857 false, false, true);
7858 if (opt_fd != NULL
7859 && (opt_fd->root.type == bfd_link_hash_defined
7860 || opt_fd->root.type == bfd_link_hash_defweak))
7862 /* If glibc supports an optimized __tls_get_addr call stub,
7863 signalled by the presence of __tls_get_addr_opt, and we'll
7864 be calling __tls_get_addr via a plt call stub, then
7865 make __tls_get_addr point to __tls_get_addr_opt. */
7866 if (!(htab->elf.dynamic_sections_created
7867 && tga_fd != NULL
7868 && (tga_fd->type == STT_FUNC
7869 || tga_fd->needs_plt)
7870 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7871 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7872 tga_fd = NULL;
7873 if (!(htab->elf.dynamic_sections_created
7874 && desc_fd != NULL
7875 && (desc_fd->type == STT_FUNC
7876 || desc_fd->needs_plt)
7877 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7878 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7879 desc_fd = NULL;
7881 if (tga_fd != NULL || desc_fd != NULL)
7883 struct plt_entry *ent = NULL;
7885 if (tga_fd != NULL)
7886 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7887 if (ent->plt.refcount > 0)
7888 break;
7889 if (ent == NULL && desc_fd != NULL)
7890 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7891 if (ent->plt.refcount > 0)
7892 break;
7893 if (ent != NULL)
7895 if (tga_fd != NULL)
7897 tga_fd->root.type = bfd_link_hash_indirect;
7898 tga_fd->root.u.i.link = &opt_fd->root;
7899 tga_fd->root.u.i.warning = NULL;
7900 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7902 if (desc_fd != NULL)
7904 desc_fd->root.type = bfd_link_hash_indirect;
7905 desc_fd->root.u.i.link = &opt_fd->root;
7906 desc_fd->root.u.i.warning = NULL;
7907 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7909 opt_fd->mark = 1;
7910 if (opt_fd->dynindx != -1)
7912 /* Use __tls_get_addr_opt in dynamic relocations. */
7913 opt_fd->dynindx = -1;
7914 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7915 opt_fd->dynstr_index);
7916 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7917 return false;
7919 if (tga_fd != NULL)
7921 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
7922 tga = elf_hash_entry (htab->tls_get_addr);
7923 if (opt != NULL && tga != NULL)
7925 tga->root.type = bfd_link_hash_indirect;
7926 tga->root.u.i.link = &opt->root;
7927 tga->root.u.i.warning = NULL;
7928 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7929 opt->mark = 1;
7930 _bfd_elf_link_hash_hide_symbol (info, opt,
7931 tga->forced_local);
7932 htab->tls_get_addr = ppc_elf_hash_entry (opt);
7934 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7935 htab->tls_get_addr_fd->is_func_descriptor = 1;
7936 if (htab->tls_get_addr != NULL)
7938 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7939 htab->tls_get_addr->is_func = 1;
7942 if (desc_fd != NULL)
7944 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7945 if (opt != NULL && desc != NULL)
7947 desc->root.type = bfd_link_hash_indirect;
7948 desc->root.u.i.link = &opt->root;
7949 desc->root.u.i.warning = NULL;
7950 ppc64_elf_copy_indirect_symbol (info, opt, desc);
7951 opt->mark = 1;
7952 _bfd_elf_link_hash_hide_symbol (info, opt,
7953 desc->forced_local);
7954 htab->tga_desc = ppc_elf_hash_entry (opt);
7956 htab->tga_desc_fd->oh = htab->tga_desc;
7957 htab->tga_desc_fd->is_func_descriptor = 1;
7958 if (htab->tga_desc != NULL)
7960 htab->tga_desc->oh = htab->tga_desc_fd;
7961 htab->tga_desc->is_func = 1;
7967 else if (htab->params->tls_get_addr_opt < 0)
7968 htab->params->tls_get_addr_opt = 0;
7971 if (htab->tga_desc_fd != NULL
7972 && htab->params->tls_get_addr_opt
7973 && htab->params->no_tls_get_addr_regsave == -1)
7974 htab->params->no_tls_get_addr_regsave = 0;
7976 return true;
7979 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7980 any of HASH1, HASH2, HASH3, or HASH4. */
7982 static bool
7983 branch_reloc_hash_match (bfd *ibfd,
7984 Elf_Internal_Rela *rel,
7985 struct ppc_link_hash_entry *hash1,
7986 struct ppc_link_hash_entry *hash2,
7987 struct ppc_link_hash_entry *hash3,
7988 struct ppc_link_hash_entry *hash4)
7990 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7991 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7992 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7994 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7996 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7997 struct elf_link_hash_entry *h;
7999 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8000 h = elf_follow_link (h);
8001 if (h == elf_hash_entry (hash1)
8002 || h == elf_hash_entry (hash2)
8003 || h == elf_hash_entry (hash3)
8004 || h == elf_hash_entry (hash4))
8005 return true;
8007 return false;
8010 /* Run through all the TLS relocs looking for optimization
8011 opportunities. The linker has been hacked (see ppc64elf.em) to do
8012 a preliminary section layout so that we know the TLS segment
8013 offsets. We can't optimize earlier because some optimizations need
8014 to know the tp offset, and we need to optimize before allocating
8015 dynamic relocations. */
8017 bool
8018 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8020 bfd *ibfd;
8021 asection *sec;
8022 struct ppc_link_hash_table *htab;
8023 unsigned char *toc_ref;
8024 int pass;
8026 if (!bfd_link_executable (info))
8027 return true;
8029 htab = ppc_hash_table (info);
8030 if (htab == NULL)
8031 return false;
8033 htab->do_tls_opt = 1;
8035 /* Make two passes over the relocs. On the first pass, mark toc
8036 entries involved with tls relocs, and check that tls relocs
8037 involved in setting up a tls_get_addr call are indeed followed by
8038 such a call. If they are not, we can't do any tls optimization.
8039 On the second pass twiddle tls_mask flags to notify
8040 relocate_section that optimization can be done, and adjust got
8041 and plt refcounts. */
8042 toc_ref = NULL;
8043 for (pass = 0; pass < 2; ++pass)
8044 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8046 Elf_Internal_Sym *locsyms = NULL;
8047 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8049 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8050 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8052 Elf_Internal_Rela *relstart, *rel, *relend;
8053 bool found_tls_get_addr_arg = 0;
8055 /* Read the relocations. */
8056 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8057 info->keep_memory);
8058 if (relstart == NULL)
8060 free (toc_ref);
8061 return false;
8064 relend = relstart + sec->reloc_count;
8065 for (rel = relstart; rel < relend; rel++)
8067 enum elf_ppc64_reloc_type r_type;
8068 unsigned long r_symndx;
8069 struct elf_link_hash_entry *h;
8070 Elf_Internal_Sym *sym;
8071 asection *sym_sec;
8072 unsigned char *tls_mask;
8073 unsigned int tls_set, tls_clear, tls_type = 0;
8074 bfd_vma value;
8075 bool ok_tprel, is_local;
8076 long toc_ref_index = 0;
8077 int expecting_tls_get_addr = 0;
8078 bool ret = false;
8080 r_symndx = ELF64_R_SYM (rel->r_info);
8081 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8082 r_symndx, ibfd))
8084 err_free_rel:
8085 if (elf_section_data (sec)->relocs != relstart)
8086 free (relstart);
8087 free (toc_ref);
8088 if (elf_symtab_hdr (ibfd).contents
8089 != (unsigned char *) locsyms)
8090 free (locsyms);
8091 return ret;
8094 if (h != NULL)
8096 if (h->root.type == bfd_link_hash_defined
8097 || h->root.type == bfd_link_hash_defweak)
8098 value = h->root.u.def.value;
8099 else if (h->root.type == bfd_link_hash_undefweak)
8100 value = 0;
8101 else
8103 found_tls_get_addr_arg = 0;
8104 continue;
8107 else
8108 /* Symbols referenced by TLS relocs must be of type
8109 STT_TLS. So no need for .opd local sym adjust. */
8110 value = sym->st_value;
8112 ok_tprel = false;
8113 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8114 if (is_local)
8116 if (h != NULL
8117 && h->root.type == bfd_link_hash_undefweak)
8118 ok_tprel = true;
8119 else if (sym_sec != NULL
8120 && sym_sec->output_section != NULL)
8122 value += sym_sec->output_offset;
8123 value += sym_sec->output_section->vma;
8124 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8125 /* Note that even though the prefix insns
8126 allow a 1<<33 offset we use the same test
8127 as for addis;addi. There may be a mix of
8128 pcrel and non-pcrel code and the decision
8129 to optimise is per symbol, not per TLS
8130 sequence. */
8131 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8135 r_type = ELF64_R_TYPE (rel->r_info);
8136 /* If this section has old-style __tls_get_addr calls
8137 without marker relocs, then check that each
8138 __tls_get_addr call reloc is preceded by a reloc
8139 that conceivably belongs to the __tls_get_addr arg
8140 setup insn. If we don't find matching arg setup
8141 relocs, don't do any tls optimization. */
8142 if (pass == 0
8143 && sec->nomark_tls_get_addr
8144 && h != NULL
8145 && is_tls_get_addr (h, htab)
8146 && !found_tls_get_addr_arg
8147 && is_branch_reloc (r_type))
8149 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8150 "TLS optimization disabled\n"),
8151 ibfd, sec, rel->r_offset);
8152 ret = true;
8153 goto err_free_rel;
8156 found_tls_get_addr_arg = 0;
8157 switch (r_type)
8159 case R_PPC64_GOT_TLSLD16:
8160 case R_PPC64_GOT_TLSLD16_LO:
8161 case R_PPC64_GOT_TLSLD_PCREL34:
8162 expecting_tls_get_addr = 1;
8163 found_tls_get_addr_arg = 1;
8164 /* Fall through. */
8166 case R_PPC64_GOT_TLSLD16_HI:
8167 case R_PPC64_GOT_TLSLD16_HA:
8168 /* These relocs should never be against a symbol
8169 defined in a shared lib. Leave them alone if
8170 that turns out to be the case. */
8171 if (!is_local)
8172 continue;
8174 /* LD -> LE */
8175 tls_set = 0;
8176 tls_clear = TLS_LD;
8177 tls_type = TLS_TLS | TLS_LD;
8178 break;
8180 case R_PPC64_GOT_TLSGD16:
8181 case R_PPC64_GOT_TLSGD16_LO:
8182 case R_PPC64_GOT_TLSGD_PCREL34:
8183 expecting_tls_get_addr = 1;
8184 found_tls_get_addr_arg = 1;
8185 /* Fall through. */
8187 case R_PPC64_GOT_TLSGD16_HI:
8188 case R_PPC64_GOT_TLSGD16_HA:
8189 if (ok_tprel)
8190 /* GD -> LE */
8191 tls_set = 0;
8192 else
8193 /* GD -> IE */
8194 tls_set = TLS_TLS | TLS_GDIE;
8195 tls_clear = TLS_GD;
8196 tls_type = TLS_TLS | TLS_GD;
8197 break;
8199 case R_PPC64_GOT_TPREL_PCREL34:
8200 case R_PPC64_GOT_TPREL16_DS:
8201 case R_PPC64_GOT_TPREL16_LO_DS:
8202 case R_PPC64_GOT_TPREL16_HI:
8203 case R_PPC64_GOT_TPREL16_HA:
8204 if (ok_tprel)
8206 /* IE -> LE */
8207 tls_set = 0;
8208 tls_clear = TLS_TPREL;
8209 tls_type = TLS_TLS | TLS_TPREL;
8210 break;
8212 continue;
8214 case R_PPC64_TLSLD:
8215 if (!is_local)
8216 continue;
8217 /* Fall through. */
8218 case R_PPC64_TLSGD:
8219 if (rel + 1 < relend
8220 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8222 if (pass != 0
8223 && (ELF64_R_TYPE (rel[1].r_info)
8224 != R_PPC64_PLTSEQ)
8225 && (ELF64_R_TYPE (rel[1].r_info)
8226 != R_PPC64_PLTSEQ_NOTOC))
8228 r_symndx = ELF64_R_SYM (rel[1].r_info);
8229 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8230 r_symndx, ibfd))
8231 goto err_free_rel;
8232 if (h != NULL)
8234 struct plt_entry *ent = NULL;
8236 for (ent = h->plt.plist;
8237 ent != NULL;
8238 ent = ent->next)
8239 if (ent->addend == rel[1].r_addend)
8240 break;
8242 if (ent != NULL
8243 && ent->plt.refcount > 0)
8244 ent->plt.refcount -= 1;
8247 continue;
8249 found_tls_get_addr_arg = 1;
8250 /* Fall through. */
8252 case R_PPC64_TLS:
8253 case R_PPC64_TOC16:
8254 case R_PPC64_TOC16_LO:
8255 if (sym_sec == NULL || sym_sec != toc)
8256 continue;
8258 /* Mark this toc entry as referenced by a TLS
8259 code sequence. We can do that now in the
8260 case of R_PPC64_TLS, and after checking for
8261 tls_get_addr for the TOC16 relocs. */
8262 if (toc_ref == NULL)
8263 toc_ref
8264 = bfd_zmalloc (toc->output_section->rawsize / 8);
8265 if (toc_ref == NULL)
8266 goto err_free_rel;
8268 if (h != NULL)
8269 value = h->root.u.def.value;
8270 else
8271 value = sym->st_value;
8272 value += rel->r_addend;
8273 if (value % 8 != 0)
8274 continue;
8275 BFD_ASSERT (value < toc->size
8276 && toc->output_offset % 8 == 0);
8277 toc_ref_index = (value + toc->output_offset) / 8;
8278 if (r_type == R_PPC64_TLS
8279 || r_type == R_PPC64_TLSGD
8280 || r_type == R_PPC64_TLSLD)
8282 toc_ref[toc_ref_index] = 1;
8283 continue;
8286 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8287 continue;
8289 tls_set = 0;
8290 tls_clear = 0;
8291 expecting_tls_get_addr = 2;
8292 break;
8294 case R_PPC64_TPREL64:
8295 if (pass == 0
8296 || sec != toc
8297 || toc_ref == NULL
8298 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8299 continue;
8300 if (ok_tprel)
8302 /* IE -> LE */
8303 tls_set = TLS_EXPLICIT;
8304 tls_clear = TLS_TPREL;
8305 break;
8307 continue;
8309 case R_PPC64_DTPMOD64:
8310 if (pass == 0
8311 || sec != toc
8312 || toc_ref == NULL
8313 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8314 continue;
8315 if (rel + 1 < relend
8316 && (rel[1].r_info
8317 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8318 && rel[1].r_offset == rel->r_offset + 8)
8320 if (ok_tprel)
8321 /* GD -> LE */
8322 tls_set = TLS_EXPLICIT | TLS_GD;
8323 else
8324 /* GD -> IE */
8325 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8326 tls_clear = TLS_GD;
8328 else
8330 if (!is_local)
8331 continue;
8333 /* LD -> LE */
8334 tls_set = TLS_EXPLICIT;
8335 tls_clear = TLS_LD;
8337 break;
8339 case R_PPC64_TPREL16_HA:
8340 if (pass == 0)
8342 unsigned char buf[4];
8343 unsigned int insn;
8344 bfd_vma off = rel->r_offset & ~3;
8345 if (!bfd_get_section_contents (ibfd, sec, buf,
8346 off, 4))
8347 goto err_free_rel;
8348 insn = bfd_get_32 (ibfd, buf);
8349 /* addis rt,13,imm */
8350 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8351 != ((15u << 26) | (13 << 16)))
8353 /* xgettext:c-format */
8354 info->callbacks->minfo
8355 (_("%H: warning: %s unexpected insn %#x.\n"),
8356 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8357 htab->do_tls_opt = 0;
8360 continue;
8362 case R_PPC64_TPREL16_HI:
8363 case R_PPC64_TPREL16_HIGH:
8364 case R_PPC64_TPREL16_HIGHA:
8365 case R_PPC64_TPREL16_HIGHER:
8366 case R_PPC64_TPREL16_HIGHERA:
8367 case R_PPC64_TPREL16_HIGHEST:
8368 case R_PPC64_TPREL16_HIGHESTA:
8369 /* These can all be used in sequences along with
8370 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8371 able to verify easily. */
8372 htab->do_tls_opt = 0;
8373 continue;
8375 default:
8376 continue;
8379 if (pass == 0)
8381 if (!expecting_tls_get_addr
8382 || !sec->nomark_tls_get_addr)
8383 continue;
8385 if (rel + 1 < relend
8386 && branch_reloc_hash_match (ibfd, rel + 1,
8387 htab->tls_get_addr_fd,
8388 htab->tga_desc_fd,
8389 htab->tls_get_addr,
8390 htab->tga_desc))
8392 if (expecting_tls_get_addr == 2)
8394 /* Check for toc tls entries. */
8395 unsigned char *toc_tls;
8396 int retval;
8398 retval = get_tls_mask (&toc_tls, NULL, NULL,
8399 &locsyms,
8400 rel, ibfd);
8401 if (retval == 0)
8402 goto err_free_rel;
8403 if (toc_tls != NULL)
8405 if ((*toc_tls & TLS_TLS) != 0
8406 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8407 found_tls_get_addr_arg = 1;
8408 if (retval > 1)
8409 toc_ref[toc_ref_index] = 1;
8412 continue;
8415 /* Uh oh, we didn't find the expected call. We
8416 could just mark this symbol to exclude it
8417 from tls optimization but it's safer to skip
8418 the entire optimization. */
8419 /* xgettext:c-format */
8420 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8421 "TLS optimization disabled\n"),
8422 ibfd, sec, rel->r_offset);
8423 ret = true;
8424 goto err_free_rel;
8427 /* If we don't have old-style __tls_get_addr calls
8428 without TLSGD/TLSLD marker relocs, and we haven't
8429 found a new-style __tls_get_addr call with a
8430 marker for this symbol, then we either have a
8431 broken object file or an -mlongcall style
8432 indirect call to __tls_get_addr without a marker.
8433 Disable optimization in this case. */
8434 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8435 && (tls_set & TLS_EXPLICIT) == 0
8436 && !sec->nomark_tls_get_addr
8437 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8438 != (TLS_TLS | TLS_MARK)))
8439 continue;
8441 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8443 struct plt_entry *ent = NULL;
8445 if (htab->tls_get_addr_fd != NULL)
8446 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8447 ent != NULL;
8448 ent = ent->next)
8449 if (ent->addend == 0)
8450 break;
8452 if (ent == NULL && htab->tga_desc_fd != NULL)
8453 for (ent = htab->tga_desc_fd->elf.plt.plist;
8454 ent != NULL;
8455 ent = ent->next)
8456 if (ent->addend == 0)
8457 break;
8459 if (ent == NULL && htab->tls_get_addr != NULL)
8460 for (ent = htab->tls_get_addr->elf.plt.plist;
8461 ent != NULL;
8462 ent = ent->next)
8463 if (ent->addend == 0)
8464 break;
8466 if (ent == NULL && htab->tga_desc != NULL)
8467 for (ent = htab->tga_desc->elf.plt.plist;
8468 ent != NULL;
8469 ent = ent->next)
8470 if (ent->addend == 0)
8471 break;
8473 if (ent != NULL
8474 && ent->plt.refcount > 0)
8475 ent->plt.refcount -= 1;
8478 if (tls_clear == 0)
8479 continue;
8481 if ((tls_set & TLS_EXPLICIT) == 0)
8483 struct got_entry *ent;
8485 /* Adjust got entry for this reloc. */
8486 if (h != NULL)
8487 ent = h->got.glist;
8488 else
8489 ent = elf_local_got_ents (ibfd)[r_symndx];
8491 for (; ent != NULL; ent = ent->next)
8492 if (ent->addend == rel->r_addend
8493 && ent->owner == ibfd
8494 && ent->tls_type == tls_type)
8495 break;
8496 if (ent == NULL)
8497 abort ();
8499 if (tls_set == 0)
8501 /* We managed to get rid of a got entry. */
8502 if (ent->got.refcount > 0)
8503 ent->got.refcount -= 1;
8506 else
8508 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8509 we'll lose one or two dyn relocs. */
8510 if (!dec_dynrel_count (rel->r_info, sec, info,
8511 NULL, h, sym))
8512 return false;
8514 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8516 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8517 NULL, h, sym))
8518 return false;
8522 *tls_mask |= tls_set & 0xff;
8523 *tls_mask &= ~tls_clear;
8526 if (elf_section_data (sec)->relocs != relstart)
8527 free (relstart);
8530 if (locsyms != NULL
8531 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8533 if (!info->keep_memory)
8534 free (locsyms);
8535 else
8536 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8540 free (toc_ref);
8541 return true;
8544 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8545 the values of any global symbols in a toc section that has been
8546 edited. Globals in toc sections should be a rarity, so this function
8547 sets a flag if any are found in toc sections other than the one just
8548 edited, so that further hash table traversals can be avoided. */
8550 struct adjust_toc_info
8552 asection *toc;
8553 unsigned long *skip;
8554 bool global_toc_syms;
8557 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8559 static bool
8560 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8562 struct ppc_link_hash_entry *eh;
8563 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8564 unsigned long i;
8566 if (h->root.type != bfd_link_hash_defined
8567 && h->root.type != bfd_link_hash_defweak)
8568 return true;
8570 eh = ppc_elf_hash_entry (h);
8571 if (eh->adjust_done)
8572 return true;
8574 if (eh->elf.root.u.def.section == toc_inf->toc)
8576 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8577 i = toc_inf->toc->rawsize >> 3;
8578 else
8579 i = eh->elf.root.u.def.value >> 3;
8581 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8583 _bfd_error_handler
8584 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8586 ++i;
8587 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8588 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8591 eh->elf.root.u.def.value -= toc_inf->skip[i];
8592 eh->adjust_done = 1;
8594 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8595 toc_inf->global_toc_syms = true;
8597 return true;
8600 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8601 on a _LO variety toc/got reloc. */
8603 static bool
8604 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8606 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8607 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8608 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8609 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8610 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8611 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8612 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8613 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8614 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8615 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8616 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8617 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8618 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8619 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8620 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8621 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8622 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8623 /* Exclude lfqu by testing reloc. If relocs are ever
8624 defined for the reduced D field in psq_lu then those
8625 will need testing too. */
8626 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8627 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8628 && (insn & 1) == 0)
8629 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8630 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8631 /* Exclude stfqu. psq_stu as above for psq_lu. */
8632 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8633 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8634 && (insn & 1) == 0));
8637 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8638 pld ra,symbol@got@pcrel
8639 load/store rt,off(ra)
8641 pla ra,symbol@pcrel
8642 load/store rt,off(ra)
8643 may be translated to
8644 pload/pstore rt,symbol+off@pcrel
8645 nop.
8646 This function returns true if the optimization is possible, placing
8647 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8649 On entry to this function, the linker has already determined that
8650 the pld can be replaced with pla: *PINSN1 is that pla insn,
8651 while *PINSN2 is the second instruction. */
8653 static bool
8654 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8656 uint64_t insn1 = *pinsn1;
8657 uint64_t insn2 = *pinsn2;
8658 bfd_signed_vma off;
8660 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8662 /* Check that regs match. */
8663 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8664 return false;
8666 /* P8LS or PMLS form, non-pcrel. */
8667 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8668 return false;
8670 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8671 *pinsn2 = PNOP;
8672 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8673 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8674 return true;
8677 insn2 >>= 32;
8679 /* Check that regs match. */
8680 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8681 return false;
8683 switch ((insn2 >> 26) & 63)
8685 default:
8686 return false;
8688 case 32: /* lwz */
8689 case 34: /* lbz */
8690 case 36: /* stw */
8691 case 38: /* stb */
8692 case 40: /* lhz */
8693 case 42: /* lha */
8694 case 44: /* sth */
8695 case 48: /* lfs */
8696 case 50: /* lfd */
8697 case 52: /* stfs */
8698 case 54: /* stfd */
8699 /* These are the PMLS cases, where we just need to tack a prefix
8700 on the insn. */
8701 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8702 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8703 off = insn2 & 0xffff;
8704 break;
8706 case 58: /* lwa, ld */
8707 if ((insn2 & 1) != 0)
8708 return false;
8709 insn1 = ((1ULL << 58) | (1ULL << 52)
8710 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8711 | (insn2 & (31ULL << 21)));
8712 off = insn2 & 0xfffc;
8713 break;
8715 case 57: /* lxsd, lxssp */
8716 if ((insn2 & 3) < 2)
8717 return false;
8718 insn1 = ((1ULL << 58) | (1ULL << 52)
8719 | ((40ULL | (insn2 & 3)) << 26)
8720 | (insn2 & (31ULL << 21)));
8721 off = insn2 & 0xfffc;
8722 break;
8724 case 61: /* stxsd, stxssp, lxv, stxv */
8725 if ((insn2 & 3) == 0)
8726 return false;
8727 else if ((insn2 & 3) >= 2)
8729 insn1 = ((1ULL << 58) | (1ULL << 52)
8730 | ((44ULL | (insn2 & 3)) << 26)
8731 | (insn2 & (31ULL << 21)));
8732 off = insn2 & 0xfffc;
8734 else
8736 insn1 = ((1ULL << 58) | (1ULL << 52)
8737 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8738 | (insn2 & (31ULL << 21)));
8739 off = insn2 & 0xfff0;
8741 break;
8743 case 56: /* lq */
8744 insn1 = ((1ULL << 58) | (1ULL << 52)
8745 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8746 off = insn2 & 0xffff;
8747 break;
8749 case 6: /* lxvp, stxvp */
8750 if ((insn2 & 0xe) != 0)
8751 return false;
8752 insn1 = ((1ULL << 58) | (1ULL << 52)
8753 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8754 | (insn2 & (31ULL << 21)));
8755 off = insn2 & 0xfff0;
8756 break;
8758 case 62: /* std, stq */
8759 if ((insn2 & 1) != 0)
8760 return false;
8761 insn1 = ((1ULL << 58) | (1ULL << 52)
8762 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8763 | (insn2 & (31ULL << 21)));
8764 off = insn2 & 0xfffc;
8765 break;
8768 *pinsn1 = insn1;
8769 *pinsn2 = (uint64_t) NOP << 32;
8770 *poff = (off ^ 0x8000) - 0x8000;
8771 return true;
8774 /* Examine all relocs referencing .toc sections in order to remove
8775 unused .toc entries. */
8777 bool
8778 ppc64_elf_edit_toc (struct bfd_link_info *info)
8780 bfd *ibfd;
8781 struct adjust_toc_info toc_inf;
8782 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8784 htab->do_toc_opt = 1;
8785 toc_inf.global_toc_syms = true;
8786 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8788 asection *toc, *sec;
8789 Elf_Internal_Shdr *symtab_hdr;
8790 Elf_Internal_Sym *local_syms;
8791 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8792 unsigned long *skip, *drop;
8793 unsigned char *used;
8794 unsigned char *keep, last, some_unused;
8796 if (!is_ppc64_elf (ibfd))
8797 continue;
8799 toc = bfd_get_section_by_name (ibfd, ".toc");
8800 if (toc == NULL
8801 || toc->size == 0
8802 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8803 || discarded_section (toc))
8804 continue;
8806 toc_relocs = NULL;
8807 local_syms = NULL;
8808 symtab_hdr = &elf_symtab_hdr (ibfd);
8810 /* Look at sections dropped from the final link. */
8811 skip = NULL;
8812 relstart = NULL;
8813 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8815 if (sec->reloc_count == 0
8816 || !discarded_section (sec)
8817 || get_opd_info (sec)
8818 || (sec->flags & SEC_ALLOC) == 0
8819 || (sec->flags & SEC_DEBUGGING) != 0)
8820 continue;
8822 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8823 if (relstart == NULL)
8824 goto error_ret;
8826 /* Run through the relocs to see which toc entries might be
8827 unused. */
8828 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8830 enum elf_ppc64_reloc_type r_type;
8831 unsigned long r_symndx;
8832 asection *sym_sec;
8833 struct elf_link_hash_entry *h;
8834 Elf_Internal_Sym *sym;
8835 bfd_vma val;
8837 r_type = ELF64_R_TYPE (rel->r_info);
8838 switch (r_type)
8840 default:
8841 continue;
8843 case R_PPC64_TOC16:
8844 case R_PPC64_TOC16_LO:
8845 case R_PPC64_TOC16_HI:
8846 case R_PPC64_TOC16_HA:
8847 case R_PPC64_TOC16_DS:
8848 case R_PPC64_TOC16_LO_DS:
8849 break;
8852 r_symndx = ELF64_R_SYM (rel->r_info);
8853 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8854 r_symndx, ibfd))
8855 goto error_ret;
8857 if (sym_sec != toc)
8858 continue;
8860 if (h != NULL)
8861 val = h->root.u.def.value;
8862 else
8863 val = sym->st_value;
8864 val += rel->r_addend;
8866 if (val >= toc->size)
8867 continue;
8869 /* Anything in the toc ought to be aligned to 8 bytes.
8870 If not, don't mark as unused. */
8871 if (val & 7)
8872 continue;
8874 if (skip == NULL)
8876 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8877 if (skip == NULL)
8878 goto error_ret;
8881 skip[val >> 3] = ref_from_discarded;
8884 if (elf_section_data (sec)->relocs != relstart)
8885 free (relstart);
8888 /* For largetoc loads of address constants, we can convert
8889 . addis rx,2,addr@got@ha
8890 . ld ry,addr@got@l(rx)
8892 . addis rx,2,addr@toc@ha
8893 . addi ry,rx,addr@toc@l
8894 when addr is within 2G of the toc pointer. This then means
8895 that the word storing "addr" in the toc is no longer needed. */
8897 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8898 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8899 && toc->reloc_count != 0)
8901 /* Read toc relocs. */
8902 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8903 info->keep_memory);
8904 if (toc_relocs == NULL)
8905 goto error_ret;
8907 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8909 enum elf_ppc64_reloc_type r_type;
8910 unsigned long r_symndx;
8911 asection *sym_sec;
8912 struct elf_link_hash_entry *h;
8913 Elf_Internal_Sym *sym;
8914 bfd_vma val, addr;
8916 r_type = ELF64_R_TYPE (rel->r_info);
8917 if (r_type != R_PPC64_ADDR64)
8918 continue;
8920 r_symndx = ELF64_R_SYM (rel->r_info);
8921 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8922 r_symndx, ibfd))
8923 goto error_ret;
8925 if (sym_sec == NULL
8926 || sym_sec->output_section == NULL
8927 || discarded_section (sym_sec))
8928 continue;
8930 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8931 continue;
8933 if (h != NULL)
8935 if (h->type == STT_GNU_IFUNC)
8936 continue;
8937 val = h->root.u.def.value;
8939 else
8941 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8942 continue;
8943 val = sym->st_value;
8945 val += rel->r_addend;
8946 val += sym_sec->output_section->vma + sym_sec->output_offset;
8948 /* We don't yet know the exact toc pointer value, but we
8949 know it will be somewhere in the toc section. Don't
8950 optimize if the difference from any possible toc
8951 pointer is outside [ff..f80008000, 7fff7fff]. */
8952 addr = toc->output_section->vma + TOC_BASE_OFF;
8953 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8954 continue;
8956 addr = toc->output_section->vma + toc->output_section->rawsize;
8957 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8958 continue;
8960 if (skip == NULL)
8962 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8963 if (skip == NULL)
8964 goto error_ret;
8967 skip[rel->r_offset >> 3]
8968 |= can_optimize | ((rel - toc_relocs) << 2);
8972 if (skip == NULL)
8973 continue;
8975 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8976 if (used == NULL)
8978 error_ret:
8979 if (symtab_hdr->contents != (unsigned char *) local_syms)
8980 free (local_syms);
8981 if (sec != NULL
8982 && elf_section_data (sec)->relocs != relstart)
8983 free (relstart);
8984 if (elf_section_data (toc)->relocs != toc_relocs)
8985 free (toc_relocs);
8986 free (skip);
8987 return false;
8990 /* Now check all kept sections that might reference the toc.
8991 Check the toc itself last. */
8992 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8993 : ibfd->sections);
8994 sec != NULL;
8995 sec = (sec == toc ? NULL
8996 : sec->next == NULL ? toc
8997 : sec->next == toc && toc->next ? toc->next
8998 : sec->next))
9000 int repeat;
9002 if (sec->reloc_count == 0
9003 || discarded_section (sec)
9004 || get_opd_info (sec)
9005 || (sec->flags & SEC_ALLOC) == 0
9006 || (sec->flags & SEC_DEBUGGING) != 0)
9007 continue;
9009 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9010 info->keep_memory);
9011 if (relstart == NULL)
9013 free (used);
9014 goto error_ret;
9017 /* Mark toc entries referenced as used. */
9020 repeat = 0;
9021 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9023 enum elf_ppc64_reloc_type r_type;
9024 unsigned long r_symndx;
9025 asection *sym_sec;
9026 struct elf_link_hash_entry *h;
9027 Elf_Internal_Sym *sym;
9028 bfd_vma val;
9030 r_type = ELF64_R_TYPE (rel->r_info);
9031 switch (r_type)
9033 case R_PPC64_TOC16:
9034 case R_PPC64_TOC16_LO:
9035 case R_PPC64_TOC16_HI:
9036 case R_PPC64_TOC16_HA:
9037 case R_PPC64_TOC16_DS:
9038 case R_PPC64_TOC16_LO_DS:
9039 /* In case we're taking addresses of toc entries. */
9040 case R_PPC64_ADDR64:
9041 break;
9043 default:
9044 continue;
9047 r_symndx = ELF64_R_SYM (rel->r_info);
9048 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9049 r_symndx, ibfd))
9051 free (used);
9052 goto error_ret;
9055 if (sym_sec != toc)
9056 continue;
9058 if (h != NULL)
9059 val = h->root.u.def.value;
9060 else
9061 val = sym->st_value;
9062 val += rel->r_addend;
9064 if (val >= toc->size)
9065 continue;
9067 if ((skip[val >> 3] & can_optimize) != 0)
9069 bfd_vma off;
9070 unsigned char opc;
9072 switch (r_type)
9074 case R_PPC64_TOC16_HA:
9075 break;
9077 case R_PPC64_TOC16_LO_DS:
9078 off = rel->r_offset;
9079 off += (bfd_big_endian (ibfd) ? -2 : 3);
9080 if (!bfd_get_section_contents (ibfd, sec, &opc,
9081 off, 1))
9083 free (used);
9084 goto error_ret;
9086 if ((opc & (0x3f << 2)) == (58u << 2))
9087 break;
9088 /* Fall through. */
9090 default:
9091 /* Wrong sort of reloc, or not a ld. We may
9092 as well clear ref_from_discarded too. */
9093 skip[val >> 3] = 0;
9097 if (sec != toc)
9098 used[val >> 3] = 1;
9099 /* For the toc section, we only mark as used if this
9100 entry itself isn't unused. */
9101 else if ((used[rel->r_offset >> 3]
9102 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9103 && !used[val >> 3])
9105 /* Do all the relocs again, to catch reference
9106 chains. */
9107 repeat = 1;
9108 used[val >> 3] = 1;
9112 while (repeat);
9114 if (elf_section_data (sec)->relocs != relstart)
9115 free (relstart);
9118 /* Merge the used and skip arrays. Assume that TOC
9119 doublewords not appearing as either used or unused belong
9120 to an entry more than one doubleword in size. */
9121 for (drop = skip, keep = used, last = 0, some_unused = 0;
9122 drop < skip + (toc->size + 7) / 8;
9123 ++drop, ++keep)
9125 if (*keep)
9127 *drop &= ~ref_from_discarded;
9128 if ((*drop & can_optimize) != 0)
9129 some_unused = 1;
9130 last = 0;
9132 else if ((*drop & ref_from_discarded) != 0)
9134 some_unused = 1;
9135 last = ref_from_discarded;
9137 else
9138 *drop = last;
9141 free (used);
9143 if (some_unused)
9145 bfd_byte *contents, *src;
9146 unsigned long off;
9147 Elf_Internal_Sym *sym;
9148 bool local_toc_syms = false;
9150 /* Shuffle the toc contents, and at the same time convert the
9151 skip array from booleans into offsets. */
9152 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9153 goto error_ret;
9155 elf_section_data (toc)->this_hdr.contents = contents;
9157 for (src = contents, off = 0, drop = skip;
9158 src < contents + toc->size;
9159 src += 8, ++drop)
9161 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9162 off += 8;
9163 else if (off != 0)
9165 *drop = off;
9166 memcpy (src - off, src, 8);
9169 *drop = off;
9170 toc->rawsize = toc->size;
9171 toc->size = src - contents - off;
9173 /* Adjust addends for relocs against the toc section sym,
9174 and optimize any accesses we can. */
9175 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9177 if (sec->reloc_count == 0
9178 || discarded_section (sec))
9179 continue;
9181 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9182 info->keep_memory);
9183 if (relstart == NULL)
9184 goto error_ret;
9186 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9188 enum elf_ppc64_reloc_type r_type;
9189 unsigned long r_symndx;
9190 asection *sym_sec;
9191 struct elf_link_hash_entry *h;
9192 bfd_vma val;
9194 r_type = ELF64_R_TYPE (rel->r_info);
9195 switch (r_type)
9197 default:
9198 continue;
9200 case R_PPC64_TOC16:
9201 case R_PPC64_TOC16_LO:
9202 case R_PPC64_TOC16_HI:
9203 case R_PPC64_TOC16_HA:
9204 case R_PPC64_TOC16_DS:
9205 case R_PPC64_TOC16_LO_DS:
9206 case R_PPC64_ADDR64:
9207 break;
9210 r_symndx = ELF64_R_SYM (rel->r_info);
9211 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9212 r_symndx, ibfd))
9213 goto error_ret;
9215 if (sym_sec != toc)
9216 continue;
9218 if (h != NULL)
9219 val = h->root.u.def.value;
9220 else
9222 val = sym->st_value;
9223 if (val != 0)
9224 local_toc_syms = true;
9227 val += rel->r_addend;
9229 if (val > toc->rawsize)
9230 val = toc->rawsize;
9231 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9232 continue;
9233 else if ((skip[val >> 3] & can_optimize) != 0)
9235 Elf_Internal_Rela *tocrel
9236 = toc_relocs + (skip[val >> 3] >> 2);
9237 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9239 switch (r_type)
9241 case R_PPC64_TOC16_HA:
9242 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9243 break;
9245 case R_PPC64_TOC16_LO_DS:
9246 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9247 break;
9249 default:
9250 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9251 ppc_howto_init ();
9252 info->callbacks->einfo
9253 /* xgettext:c-format */
9254 (_("%H: %s references "
9255 "optimized away TOC entry\n"),
9256 ibfd, sec, rel->r_offset,
9257 ppc64_elf_howto_table[r_type]->name);
9258 bfd_set_error (bfd_error_bad_value);
9259 goto error_ret;
9261 rel->r_addend = tocrel->r_addend;
9262 elf_section_data (sec)->relocs = relstart;
9263 continue;
9266 if (h != NULL || sym->st_value != 0)
9267 continue;
9269 rel->r_addend -= skip[val >> 3];
9270 elf_section_data (sec)->relocs = relstart;
9273 if (elf_section_data (sec)->relocs != relstart)
9274 free (relstart);
9277 /* We shouldn't have local or global symbols defined in the TOC,
9278 but handle them anyway. */
9279 if (local_syms != NULL)
9280 for (sym = local_syms;
9281 sym < local_syms + symtab_hdr->sh_info;
9282 ++sym)
9283 if (sym->st_value != 0
9284 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9286 unsigned long i;
9288 if (sym->st_value > toc->rawsize)
9289 i = toc->rawsize >> 3;
9290 else
9291 i = sym->st_value >> 3;
9293 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9295 if (local_toc_syms)
9296 _bfd_error_handler
9297 (_("%s defined on removed toc entry"),
9298 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9300 ++i;
9301 while ((skip[i] & (ref_from_discarded | can_optimize)));
9302 sym->st_value = (bfd_vma) i << 3;
9305 sym->st_value -= skip[i];
9306 symtab_hdr->contents = (unsigned char *) local_syms;
9309 /* Adjust any global syms defined in this toc input section. */
9310 if (toc_inf.global_toc_syms)
9312 toc_inf.toc = toc;
9313 toc_inf.skip = skip;
9314 toc_inf.global_toc_syms = false;
9315 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9316 &toc_inf);
9319 if (toc->reloc_count != 0)
9321 Elf_Internal_Shdr *rel_hdr;
9322 Elf_Internal_Rela *wrel;
9323 bfd_size_type sz;
9325 /* Remove unused toc relocs, and adjust those we keep. */
9326 if (toc_relocs == NULL)
9327 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9328 info->keep_memory);
9329 if (toc_relocs == NULL)
9330 goto error_ret;
9332 wrel = toc_relocs;
9333 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9334 if ((skip[rel->r_offset >> 3]
9335 & (ref_from_discarded | can_optimize)) == 0)
9337 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9338 wrel->r_info = rel->r_info;
9339 wrel->r_addend = rel->r_addend;
9340 ++wrel;
9342 else if (!dec_dynrel_count (rel->r_info, toc, info,
9343 &local_syms, NULL, NULL))
9344 goto error_ret;
9346 elf_section_data (toc)->relocs = toc_relocs;
9347 toc->reloc_count = wrel - toc_relocs;
9348 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9349 sz = rel_hdr->sh_entsize;
9350 rel_hdr->sh_size = toc->reloc_count * sz;
9353 else if (elf_section_data (toc)->relocs != toc_relocs)
9354 free (toc_relocs);
9356 if (local_syms != NULL
9357 && symtab_hdr->contents != (unsigned char *) local_syms)
9359 if (!info->keep_memory)
9360 free (local_syms);
9361 else
9362 symtab_hdr->contents = (unsigned char *) local_syms;
9364 free (skip);
9367 /* Look for cases where we can change an indirect GOT access to
9368 a GOT relative or PC relative access, possibly reducing the
9369 number of GOT entries. */
9370 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9372 asection *sec;
9373 Elf_Internal_Shdr *symtab_hdr;
9374 Elf_Internal_Sym *local_syms;
9375 Elf_Internal_Rela *relstart, *rel;
9376 bfd_vma got;
9378 if (!is_ppc64_elf (ibfd))
9379 continue;
9381 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9382 continue;
9384 sec = ppc64_elf_tdata (ibfd)->got;
9385 got = 0;
9386 if (sec != NULL)
9387 got = sec->output_section->vma + sec->output_offset + 0x8000;
9389 local_syms = NULL;
9390 symtab_hdr = &elf_symtab_hdr (ibfd);
9392 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9394 if (sec->reloc_count == 0
9395 || !ppc64_elf_section_data (sec)->has_optrel
9396 || discarded_section (sec))
9397 continue;
9399 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9400 info->keep_memory);
9401 if (relstart == NULL)
9403 got_error_ret:
9404 if (symtab_hdr->contents != (unsigned char *) local_syms)
9405 free (local_syms);
9406 if (sec != NULL
9407 && elf_section_data (sec)->relocs != relstart)
9408 free (relstart);
9409 return false;
9412 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9414 enum elf_ppc64_reloc_type r_type;
9415 unsigned long r_symndx;
9416 Elf_Internal_Sym *sym;
9417 asection *sym_sec;
9418 struct elf_link_hash_entry *h;
9419 struct got_entry *ent;
9420 bfd_vma val, pc;
9421 unsigned char buf[8];
9422 unsigned int insn;
9423 enum {no_check, check_lo, check_ha} insn_check;
9425 r_type = ELF64_R_TYPE (rel->r_info);
9426 switch (r_type)
9428 default:
9429 insn_check = no_check;
9430 break;
9432 case R_PPC64_PLT16_HA:
9433 case R_PPC64_GOT_TLSLD16_HA:
9434 case R_PPC64_GOT_TLSGD16_HA:
9435 case R_PPC64_GOT_TPREL16_HA:
9436 case R_PPC64_GOT_DTPREL16_HA:
9437 case R_PPC64_GOT16_HA:
9438 case R_PPC64_TOC16_HA:
9439 insn_check = check_ha;
9440 break;
9442 case R_PPC64_PLT16_LO:
9443 case R_PPC64_PLT16_LO_DS:
9444 case R_PPC64_GOT_TLSLD16_LO:
9445 case R_PPC64_GOT_TLSGD16_LO:
9446 case R_PPC64_GOT_TPREL16_LO_DS:
9447 case R_PPC64_GOT_DTPREL16_LO_DS:
9448 case R_PPC64_GOT16_LO:
9449 case R_PPC64_GOT16_LO_DS:
9450 case R_PPC64_TOC16_LO:
9451 case R_PPC64_TOC16_LO_DS:
9452 insn_check = check_lo;
9453 break;
9456 if (insn_check != no_check)
9458 bfd_vma off = rel->r_offset & ~3;
9460 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9461 goto got_error_ret;
9463 insn = bfd_get_32 (ibfd, buf);
9464 if (insn_check == check_lo
9465 ? !ok_lo_toc_insn (insn, r_type)
9466 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9467 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9469 char str[12];
9471 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9472 sprintf (str, "%#08x", insn);
9473 info->callbacks->einfo
9474 /* xgettext:c-format */
9475 (_("%H: got/toc optimization is not supported for"
9476 " %s instruction\n"),
9477 ibfd, sec, rel->r_offset & ~3, str);
9478 continue;
9482 switch (r_type)
9484 /* Note that we don't delete GOT entries for
9485 R_PPC64_GOT16_DS since we'd need a lot more
9486 analysis. For starters, the preliminary layout is
9487 before the GOT, PLT, dynamic sections and stubs are
9488 laid out. Then we'd need to allow for changes in
9489 distance between sections caused by alignment. */
9490 default:
9491 continue;
9493 case R_PPC64_GOT16_HA:
9494 case R_PPC64_GOT16_LO_DS:
9495 case R_PPC64_GOT_PCREL34:
9496 break;
9499 r_symndx = ELF64_R_SYM (rel->r_info);
9500 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9501 r_symndx, ibfd))
9502 goto got_error_ret;
9504 if (sym_sec == NULL
9505 || sym_sec->output_section == NULL
9506 || discarded_section (sym_sec))
9507 continue;
9509 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9510 continue;
9512 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9513 continue;
9515 if (h != NULL)
9516 val = h->root.u.def.value;
9517 else
9518 val = sym->st_value;
9519 val += rel->r_addend;
9520 val += sym_sec->output_section->vma + sym_sec->output_offset;
9522 /* Fudge factor to allow for the fact that the preliminary layout
9523 isn't exact. Reduce limits by this factor. */
9524 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9526 switch (r_type)
9528 default:
9529 continue;
9531 case R_PPC64_GOT16_HA:
9532 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9533 >= LIMIT_ADJUST (0x100000000ULL))
9534 continue;
9536 if (!bfd_get_section_contents (ibfd, sec, buf,
9537 rel->r_offset & ~3, 4))
9538 goto got_error_ret;
9539 insn = bfd_get_32 (ibfd, buf);
9540 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9541 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9542 continue;
9543 break;
9545 case R_PPC64_GOT16_LO_DS:
9546 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9547 >= LIMIT_ADJUST (0x100000000ULL))
9548 continue;
9549 if (!bfd_get_section_contents (ibfd, sec, buf,
9550 rel->r_offset & ~3, 4))
9551 goto got_error_ret;
9552 insn = bfd_get_32 (ibfd, buf);
9553 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9554 continue;
9555 break;
9557 case R_PPC64_GOT_PCREL34:
9558 pc = rel->r_offset;
9559 pc += sec->output_section->vma + sec->output_offset;
9560 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9561 >= LIMIT_ADJUST (1ULL << 34))
9562 continue;
9563 if (!bfd_get_section_contents (ibfd, sec, buf,
9564 rel->r_offset & ~3, 8))
9565 goto got_error_ret;
9566 insn = bfd_get_32 (ibfd, buf);
9567 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9568 continue;
9569 insn = bfd_get_32 (ibfd, buf + 4);
9570 if ((insn & (0x3fu << 26)) != 57u << 26)
9571 continue;
9572 break;
9574 #undef LIMIT_ADJUST
9576 if (h != NULL)
9577 ent = h->got.glist;
9578 else
9580 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9581 ent = local_got_ents[r_symndx];
9583 for (; ent != NULL; ent = ent->next)
9584 if (ent->addend == rel->r_addend
9585 && ent->owner == ibfd
9586 && ent->tls_type == 0)
9587 break;
9588 BFD_ASSERT (ent && ent->got.refcount > 0);
9589 ent->got.refcount -= 1;
9592 if (elf_section_data (sec)->relocs != relstart)
9593 free (relstart);
9596 if (local_syms != NULL
9597 && symtab_hdr->contents != (unsigned char *) local_syms)
9599 if (!info->keep_memory)
9600 free (local_syms);
9601 else
9602 symtab_hdr->contents = (unsigned char *) local_syms;
9606 return true;
9609 /* Return true iff input section I references the TOC using
9610 instructions limited to +/-32k offsets. */
9612 bool
9613 ppc64_elf_has_small_toc_reloc (asection *i)
9615 return (is_ppc64_elf (i->owner)
9616 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9619 /* Allocate space for one GOT entry. */
9621 static void
9622 allocate_got (struct elf_link_hash_entry *h,
9623 struct bfd_link_info *info,
9624 struct got_entry *gent)
9626 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9627 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9628 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9629 ? 16 : 8);
9630 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9631 ? 2 : 1) * sizeof (Elf64_External_Rela);
9632 asection *got = ppc64_elf_tdata (gent->owner)->got;
9634 gent->got.offset = got->size;
9635 got->size += entsize;
9637 if (h->type == STT_GNU_IFUNC)
9639 htab->elf.irelplt->size += rentsize;
9640 htab->got_reli_size += rentsize;
9642 else if (((bfd_link_pic (info)
9643 && !(gent->tls_type != 0
9644 && bfd_link_executable (info)
9645 && SYMBOL_REFERENCES_LOCAL (info, h)))
9646 || (htab->elf.dynamic_sections_created
9647 && h->dynindx != -1
9648 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9649 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9651 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9652 relgot->size += rentsize;
9656 /* This function merges got entries in the same toc group. */
9658 static void
9659 merge_got_entries (struct got_entry **pent)
9661 struct got_entry *ent, *ent2;
9663 for (ent = *pent; ent != NULL; ent = ent->next)
9664 if (!ent->is_indirect)
9665 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9666 if (!ent2->is_indirect
9667 && ent2->addend == ent->addend
9668 && ent2->tls_type == ent->tls_type
9669 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9671 ent2->is_indirect = true;
9672 ent2->got.ent = ent;
9676 /* If H is undefined, make it dynamic if that makes sense. */
9678 static bool
9679 ensure_undef_dynamic (struct bfd_link_info *info,
9680 struct elf_link_hash_entry *h)
9682 struct elf_link_hash_table *htab = elf_hash_table (info);
9684 if (htab->dynamic_sections_created
9685 && ((info->dynamic_undefined_weak != 0
9686 && h->root.type == bfd_link_hash_undefweak)
9687 || h->root.type == bfd_link_hash_undefined)
9688 && h->dynindx == -1
9689 && !h->forced_local
9690 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9691 return bfd_elf_link_record_dynamic_symbol (info, h);
9692 return true;
9695 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9696 usual htab->elf.splt section for a PLT entry. */
9698 static inline
9699 bool use_local_plt (struct bfd_link_info *info,
9700 struct elf_link_hash_entry *h)
9702 return (h == NULL
9703 || h->dynindx == -1
9704 || !elf_hash_table (info)->dynamic_sections_created);
9707 /* Allocate space in .plt, .got and associated reloc sections for
9708 dynamic relocs. */
9710 static bool
9711 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9713 struct bfd_link_info *info;
9714 struct ppc_link_hash_table *htab;
9715 asection *s;
9716 struct ppc_link_hash_entry *eh;
9717 struct got_entry **pgent, *gent;
9719 if (h->root.type == bfd_link_hash_indirect)
9720 return true;
9722 info = (struct bfd_link_info *) inf;
9723 htab = ppc_hash_table (info);
9724 if (htab == NULL)
9725 return false;
9727 eh = ppc_elf_hash_entry (h);
9728 /* Run through the TLS GD got entries first if we're changing them
9729 to TPREL. */
9730 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9731 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9732 if (gent->got.refcount > 0
9733 && (gent->tls_type & TLS_GD) != 0)
9735 /* This was a GD entry that has been converted to TPREL. If
9736 there happens to be a TPREL entry we can use that one. */
9737 struct got_entry *ent;
9738 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9739 if (ent->got.refcount > 0
9740 && (ent->tls_type & TLS_TPREL) != 0
9741 && ent->addend == gent->addend
9742 && ent->owner == gent->owner)
9744 gent->got.refcount = 0;
9745 break;
9748 /* If not, then we'll be using our own TPREL entry. */
9749 if (gent->got.refcount != 0)
9750 gent->tls_type = TLS_TLS | TLS_TPREL;
9753 /* Remove any list entry that won't generate a word in the GOT before
9754 we call merge_got_entries. Otherwise we risk merging to empty
9755 entries. */
9756 pgent = &h->got.glist;
9757 while ((gent = *pgent) != NULL)
9758 if (gent->got.refcount > 0)
9760 if ((gent->tls_type & TLS_LD) != 0
9761 && SYMBOL_REFERENCES_LOCAL (info, h))
9763 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9764 *pgent = gent->next;
9766 else
9767 pgent = &gent->next;
9769 else
9770 *pgent = gent->next;
9772 if (!htab->do_multi_toc)
9773 merge_got_entries (&h->got.glist);
9775 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9776 if (!gent->is_indirect)
9778 /* Ensure we catch all the cases where this symbol should
9779 be made dynamic. */
9780 if (!ensure_undef_dynamic (info, h))
9781 return false;
9783 if (!is_ppc64_elf (gent->owner))
9784 abort ();
9786 allocate_got (h, info, gent);
9789 /* If no dynamic sections we can't have dynamic relocs, except for
9790 IFUNCs which are handled even in static executables. */
9791 if (!htab->elf.dynamic_sections_created
9792 && h->type != STT_GNU_IFUNC)
9793 h->dyn_relocs = NULL;
9795 /* Discard relocs on undefined symbols that must be local. */
9796 else if (h->root.type == bfd_link_hash_undefined
9797 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9798 h->dyn_relocs = NULL;
9800 /* Also discard relocs on undefined weak syms with non-default
9801 visibility, or when dynamic_undefined_weak says so. */
9802 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9803 h->dyn_relocs = NULL;
9805 if (h->dyn_relocs != NULL)
9807 struct elf_dyn_relocs *p, **pp;
9809 /* In the shared -Bsymbolic case, discard space allocated for
9810 dynamic pc-relative relocs against symbols which turn out to
9811 be defined in regular objects. For the normal shared case,
9812 discard space for relocs that have become local due to symbol
9813 visibility changes. */
9814 if (bfd_link_pic (info))
9816 /* Relocs that use pc_count are those that appear on a call
9817 insn, or certain REL relocs (see must_be_dyn_reloc) that
9818 can be generated via assembly. We want calls to
9819 protected symbols to resolve directly to the function
9820 rather than going via the plt. If people want function
9821 pointer comparisons to work as expected then they should
9822 avoid writing weird assembly. */
9823 if (SYMBOL_CALLS_LOCAL (info, h))
9825 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
9827 p->count -= p->pc_count;
9828 p->pc_count = 0;
9829 if (p->count == 0)
9830 *pp = p->next;
9831 else
9832 pp = &p->next;
9836 if (h->dyn_relocs != NULL)
9838 /* Ensure we catch all the cases where this symbol
9839 should be made dynamic. */
9840 if (!ensure_undef_dynamic (info, h))
9841 return false;
9845 /* For a fixed position executable, discard space for
9846 relocs against symbols which are not dynamic. */
9847 else if (h->type != STT_GNU_IFUNC)
9849 if ((h->dynamic_adjusted
9850 || (h->ref_regular
9851 && h->root.type == bfd_link_hash_undefweak
9852 && (info->dynamic_undefined_weak > 0
9853 || !_bfd_elf_readonly_dynrelocs (h))))
9854 && !h->def_regular
9855 && !ELF_COMMON_DEF_P (h))
9857 /* Ensure we catch all the cases where this symbol
9858 should be made dynamic. */
9859 if (!ensure_undef_dynamic (info, h))
9860 return false;
9862 /* But if that didn't work out, discard dynamic relocs. */
9863 if (h->dynindx == -1)
9864 h->dyn_relocs = NULL;
9866 else
9867 h->dyn_relocs = NULL;
9870 /* Finally, allocate space. */
9871 for (p = h->dyn_relocs; p != NULL; p = p->next)
9873 asection *sreloc = elf_section_data (p->sec)->sreloc;
9874 if (eh->elf.type == STT_GNU_IFUNC)
9875 sreloc = htab->elf.irelplt;
9876 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9880 /* We might need a PLT entry when the symbol
9881 a) is dynamic, or
9882 b) is an ifunc, or
9883 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9884 d) has plt16 relocs and we are linking statically. */
9885 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9886 || h->type == STT_GNU_IFUNC
9887 || (h->needs_plt && h->dynamic_adjusted)
9888 || (h->needs_plt
9889 && h->def_regular
9890 && !htab->elf.dynamic_sections_created
9891 && !htab->can_convert_all_inline_plt
9892 && (ppc_elf_hash_entry (h)->tls_mask
9893 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9895 struct plt_entry *pent;
9896 bool doneone = false;
9897 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9898 if (pent->plt.refcount > 0)
9900 if (!ensure_undef_dynamic (info, h))
9901 return false;
9903 if (use_local_plt (info, h))
9905 if (h->type == STT_GNU_IFUNC)
9907 s = htab->elf.iplt;
9908 pent->plt.offset = s->size;
9909 s->size += PLT_ENTRY_SIZE (htab);
9910 s = htab->elf.irelplt;
9912 else
9914 s = htab->pltlocal;
9915 pent->plt.offset = s->size;
9916 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9917 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9920 else
9922 /* If this is the first .plt entry, make room for the special
9923 first entry. */
9924 s = htab->elf.splt;
9925 if (s->size == 0)
9926 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9928 pent->plt.offset = s->size;
9930 /* Make room for this entry. */
9931 s->size += PLT_ENTRY_SIZE (htab);
9933 /* Make room for the .glink code. */
9934 s = htab->glink;
9935 if (s->size == 0)
9936 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9937 if (htab->opd_abi)
9939 /* We need bigger stubs past index 32767. */
9940 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9941 s->size += 4;
9942 s->size += 2*4;
9944 else
9945 s->size += 4;
9947 /* We also need to make an entry in the .rela.plt section. */
9948 s = htab->elf.srelplt;
9950 if (s != NULL)
9951 s->size += sizeof (Elf64_External_Rela);
9952 doneone = true;
9954 else
9955 pent->plt.offset = (bfd_vma) -1;
9956 if (!doneone)
9958 h->plt.plist = NULL;
9959 h->needs_plt = 0;
9962 else
9964 h->plt.plist = NULL;
9965 h->needs_plt = 0;
9968 return true;
9971 #define PPC_LO(v) ((v) & 0xffff)
9972 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9973 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9974 #define D34(v) \
9975 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9976 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9978 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9979 to set up space for global entry stubs. These are put in glink,
9980 after the branch table. */
9982 static bool
9983 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9985 struct bfd_link_info *info;
9986 struct ppc_link_hash_table *htab;
9987 struct plt_entry *pent;
9988 asection *s, *plt;
9990 if (h->root.type == bfd_link_hash_indirect)
9991 return true;
9993 if (!h->pointer_equality_needed)
9994 return true;
9996 if (h->def_regular)
9997 return true;
9999 info = inf;
10000 htab = ppc_hash_table (info);
10001 if (htab == NULL)
10002 return false;
10004 s = htab->global_entry;
10005 plt = htab->elf.splt;
10006 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10007 if (pent->plt.offset != (bfd_vma) -1
10008 && pent->addend == 0)
10010 /* For ELFv2, if this symbol is not defined in a regular file
10011 and we are not generating a shared library or pie, then we
10012 need to define the symbol in the executable on a call stub.
10013 This is to avoid text relocations. */
10014 bfd_vma off, stub_align, stub_off, stub_size;
10015 unsigned int align_power;
10017 stub_size = 16;
10018 stub_off = s->size;
10019 if (htab->params->plt_stub_align >= 0)
10020 align_power = htab->params->plt_stub_align;
10021 else
10022 align_power = -htab->params->plt_stub_align;
10023 /* Setting section alignment is delayed until we know it is
10024 non-empty. Otherwise the .text output section will be
10025 aligned at least to plt_stub_align even when no global
10026 entry stubs are needed. */
10027 if (s->alignment_power < align_power)
10028 s->alignment_power = align_power;
10029 stub_align = (bfd_vma) 1 << align_power;
10030 if (htab->params->plt_stub_align >= 0
10031 || ((((stub_off + stub_size - 1) & -stub_align)
10032 - (stub_off & -stub_align))
10033 > ((stub_size - 1) & -stub_align)))
10034 stub_off = (stub_off + stub_align - 1) & -stub_align;
10035 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10036 off -= stub_off + s->output_offset + s->output_section->vma;
10037 /* Note that for --plt-stub-align negative we have a possible
10038 dependency between stub offset and size. Break that
10039 dependency by assuming the max stub size when calculating
10040 the stub offset. */
10041 if (PPC_HA (off) == 0)
10042 stub_size -= 4;
10043 h->root.type = bfd_link_hash_defined;
10044 h->root.u.def.section = s;
10045 h->root.u.def.value = stub_off;
10046 s->size = stub_off + stub_size;
10047 break;
10049 return true;
10052 /* Set the sizes of the dynamic sections. */
10054 static bool
10055 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10056 struct bfd_link_info *info)
10058 struct ppc_link_hash_table *htab;
10059 bfd *dynobj;
10060 asection *s;
10061 bool relocs;
10062 bfd *ibfd;
10063 struct got_entry *first_tlsld;
10065 htab = ppc_hash_table (info);
10066 if (htab == NULL)
10067 return false;
10069 dynobj = htab->elf.dynobj;
10070 if (dynobj == NULL)
10071 abort ();
10073 if (htab->elf.dynamic_sections_created)
10075 /* Set the contents of the .interp section to the interpreter. */
10076 if (bfd_link_executable (info) && !info->nointerp)
10078 s = bfd_get_linker_section (dynobj, ".interp");
10079 if (s == NULL)
10080 abort ();
10081 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10082 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10086 /* Set up .got offsets for local syms, and space for local dynamic
10087 relocs. */
10088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10090 struct got_entry **lgot_ents;
10091 struct got_entry **end_lgot_ents;
10092 struct plt_entry **local_plt;
10093 struct plt_entry **end_local_plt;
10094 unsigned char *lgot_masks;
10095 bfd_size_type locsymcount;
10096 Elf_Internal_Shdr *symtab_hdr;
10098 if (!is_ppc64_elf (ibfd))
10099 continue;
10101 for (s = ibfd->sections; s != NULL; s = s->next)
10103 struct ppc_dyn_relocs *p;
10105 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10107 if (!bfd_is_abs_section (p->sec)
10108 && bfd_is_abs_section (p->sec->output_section))
10110 /* Input section has been discarded, either because
10111 it is a copy of a linkonce section or due to
10112 linker script /DISCARD/, so we'll be discarding
10113 the relocs too. */
10115 else if (p->count != 0)
10117 asection *srel = elf_section_data (p->sec)->sreloc;
10118 if (p->ifunc)
10119 srel = htab->elf.irelplt;
10120 srel->size += p->count * sizeof (Elf64_External_Rela);
10121 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10122 info->flags |= DF_TEXTREL;
10127 lgot_ents = elf_local_got_ents (ibfd);
10128 if (!lgot_ents)
10129 continue;
10131 symtab_hdr = &elf_symtab_hdr (ibfd);
10132 locsymcount = symtab_hdr->sh_info;
10133 end_lgot_ents = lgot_ents + locsymcount;
10134 local_plt = (struct plt_entry **) end_lgot_ents;
10135 end_local_plt = local_plt + locsymcount;
10136 lgot_masks = (unsigned char *) end_local_plt;
10137 s = ppc64_elf_tdata (ibfd)->got;
10138 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10140 struct got_entry **pent, *ent;
10142 pent = lgot_ents;
10143 while ((ent = *pent) != NULL)
10144 if (ent->got.refcount > 0)
10146 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10148 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10149 *pent = ent->next;
10151 else
10153 unsigned int ent_size = 8;
10154 unsigned int rel_size = sizeof (Elf64_External_Rela);
10156 ent->got.offset = s->size;
10157 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10159 ent_size *= 2;
10160 rel_size *= 2;
10162 s->size += ent_size;
10163 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10165 htab->elf.irelplt->size += rel_size;
10166 htab->got_reli_size += rel_size;
10168 else if (bfd_link_pic (info)
10169 && !(ent->tls_type != 0
10170 && bfd_link_executable (info)))
10172 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10173 srel->size += rel_size;
10175 pent = &ent->next;
10178 else
10179 *pent = ent->next;
10182 /* Allocate space for plt calls to local syms. */
10183 lgot_masks = (unsigned char *) end_local_plt;
10184 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10186 struct plt_entry *ent;
10188 for (ent = *local_plt; ent != NULL; ent = ent->next)
10189 if (ent->plt.refcount > 0)
10191 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10193 s = htab->elf.iplt;
10194 ent->plt.offset = s->size;
10195 s->size += PLT_ENTRY_SIZE (htab);
10196 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10198 else if (htab->can_convert_all_inline_plt
10199 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10200 ent->plt.offset = (bfd_vma) -1;
10201 else
10203 s = htab->pltlocal;
10204 ent->plt.offset = s->size;
10205 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10206 if (bfd_link_pic (info))
10207 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10210 else
10211 ent->plt.offset = (bfd_vma) -1;
10215 /* Allocate global sym .plt and .got entries, and space for global
10216 sym dynamic relocs. */
10217 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10219 if (!htab->opd_abi && !bfd_link_pic (info))
10220 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10222 first_tlsld = NULL;
10223 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10225 struct got_entry *ent;
10227 if (!is_ppc64_elf (ibfd))
10228 continue;
10230 ent = ppc64_tlsld_got (ibfd);
10231 if (ent->got.refcount > 0)
10233 if (!htab->do_multi_toc && first_tlsld != NULL)
10235 ent->is_indirect = true;
10236 ent->got.ent = first_tlsld;
10238 else
10240 if (first_tlsld == NULL)
10241 first_tlsld = ent;
10242 s = ppc64_elf_tdata (ibfd)->got;
10243 ent->got.offset = s->size;
10244 ent->owner = ibfd;
10245 s->size += 16;
10246 if (bfd_link_dll (info))
10248 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10249 srel->size += sizeof (Elf64_External_Rela);
10253 else
10254 ent->got.offset = (bfd_vma) -1;
10257 /* We now have determined the sizes of the various dynamic sections.
10258 Allocate memory for them. */
10259 relocs = false;
10260 for (s = dynobj->sections; s != NULL; s = s->next)
10262 if ((s->flags & SEC_LINKER_CREATED) == 0)
10263 continue;
10265 if (s == htab->brlt || s == htab->relbrlt)
10266 /* These haven't been allocated yet; don't strip. */
10267 continue;
10268 else if (s == htab->elf.sgot
10269 || s == htab->elf.splt
10270 || s == htab->elf.iplt
10271 || s == htab->pltlocal
10272 || s == htab->glink
10273 || s == htab->global_entry
10274 || s == htab->elf.sdynbss
10275 || s == htab->elf.sdynrelro)
10277 /* Strip this section if we don't need it; see the
10278 comment below. */
10280 else if (s == htab->glink_eh_frame)
10282 if (!bfd_is_abs_section (s->output_section))
10283 /* Not sized yet. */
10284 continue;
10286 else if (startswith (s->name, ".rela"))
10288 if (s->size != 0)
10290 if (s != htab->elf.srelplt)
10291 relocs = true;
10293 /* We use the reloc_count field as a counter if we need
10294 to copy relocs into the output file. */
10295 s->reloc_count = 0;
10298 else
10300 /* It's not one of our sections, so don't allocate space. */
10301 continue;
10304 if (s->size == 0)
10306 /* If we don't need this section, strip it from the
10307 output file. This is mostly to handle .rela.bss and
10308 .rela.plt. We must create both sections in
10309 create_dynamic_sections, because they must be created
10310 before the linker maps input sections to output
10311 sections. The linker does that before
10312 adjust_dynamic_symbol is called, and it is that
10313 function which decides whether anything needs to go
10314 into these sections. */
10315 s->flags |= SEC_EXCLUDE;
10316 continue;
10319 if (bfd_is_abs_section (s->output_section))
10320 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10321 s->name);
10323 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10324 continue;
10326 /* Allocate memory for the section contents. We use bfd_zalloc
10327 here in case unused entries are not reclaimed before the
10328 section's contents are written out. This should not happen,
10329 but this way if it does we get a R_PPC64_NONE reloc in .rela
10330 sections instead of garbage.
10331 We also rely on the section contents being zero when writing
10332 the GOT and .dynrelro. */
10333 s->contents = bfd_zalloc (dynobj, s->size);
10334 if (s->contents == NULL)
10335 return false;
10338 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10340 if (!is_ppc64_elf (ibfd))
10341 continue;
10343 s = ppc64_elf_tdata (ibfd)->got;
10344 if (s != NULL && s != htab->elf.sgot)
10346 if (s->size == 0)
10347 s->flags |= SEC_EXCLUDE;
10348 else
10350 s->contents = bfd_zalloc (ibfd, s->size);
10351 if (s->contents == NULL)
10352 return false;
10355 s = ppc64_elf_tdata (ibfd)->relgot;
10356 if (s != NULL)
10358 if (s->size == 0)
10359 s->flags |= SEC_EXCLUDE;
10360 else
10362 s->contents = bfd_zalloc (ibfd, s->size);
10363 if (s->contents == NULL)
10364 return false;
10365 relocs = true;
10366 s->reloc_count = 0;
10371 if (htab->elf.dynamic_sections_created)
10373 bool tls_opt;
10375 /* Add some entries to the .dynamic section. We fill in the
10376 values later, in ppc64_elf_finish_dynamic_sections, but we
10377 must add the entries now so that we get the correct size for
10378 the .dynamic section. The DT_DEBUG entry is filled in by the
10379 dynamic linker and used by the debugger. */
10380 #define add_dynamic_entry(TAG, VAL) \
10381 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10383 if (bfd_link_executable (info))
10385 if (!add_dynamic_entry (DT_DEBUG, 0))
10386 return false;
10389 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10391 if (!add_dynamic_entry (DT_PLTGOT, 0)
10392 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10393 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10394 || !add_dynamic_entry (DT_JMPREL, 0)
10395 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10396 return false;
10399 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10401 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10402 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10403 return false;
10406 tls_opt = (htab->params->tls_get_addr_opt
10407 && ((htab->tls_get_addr_fd != NULL
10408 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10409 || (htab->tga_desc_fd != NULL
10410 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10411 if (tls_opt || !htab->opd_abi)
10413 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10414 return false;
10417 if (relocs)
10419 if (!add_dynamic_entry (DT_RELA, 0)
10420 || !add_dynamic_entry (DT_RELASZ, 0)
10421 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10422 return false;
10424 /* If any dynamic relocs apply to a read-only section,
10425 then we need a DT_TEXTREL entry. */
10426 if ((info->flags & DF_TEXTREL) == 0)
10427 elf_link_hash_traverse (&htab->elf,
10428 _bfd_elf_maybe_set_textrel, info);
10430 if ((info->flags & DF_TEXTREL) != 0)
10432 if (!add_dynamic_entry (DT_TEXTREL, 0))
10433 return false;
10437 #undef add_dynamic_entry
10439 return true;
10442 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10444 static bool
10445 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10447 if (h->plt.plist != NULL
10448 && !h->def_regular
10449 && !h->pointer_equality_needed)
10450 return false;
10452 return _bfd_elf_hash_symbol (h);
10455 /* Determine the type of stub needed, if any, for a call. */
10457 static inline enum ppc_stub_type
10458 ppc_type_of_stub (asection *input_sec,
10459 const Elf_Internal_Rela *rel,
10460 struct ppc_link_hash_entry **hash,
10461 struct plt_entry **plt_ent,
10462 bfd_vma destination,
10463 unsigned long local_off)
10465 struct ppc_link_hash_entry *h = *hash;
10466 bfd_vma location;
10467 bfd_vma branch_offset;
10468 bfd_vma max_branch_offset;
10469 enum elf_ppc64_reloc_type r_type;
10471 if (h != NULL)
10473 struct plt_entry *ent;
10474 struct ppc_link_hash_entry *fdh = h;
10475 if (h->oh != NULL
10476 && h->oh->is_func_descriptor)
10478 fdh = ppc_follow_link (h->oh);
10479 *hash = fdh;
10482 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10483 if (ent->addend == rel->r_addend
10484 && ent->plt.offset != (bfd_vma) -1)
10486 *plt_ent = ent;
10487 return ppc_stub_plt_call;
10490 /* Here, we know we don't have a plt entry. If we don't have a
10491 either a defined function descriptor or a defined entry symbol
10492 in a regular object file, then it is pointless trying to make
10493 any other type of stub. */
10494 if (!is_static_defined (&fdh->elf)
10495 && !is_static_defined (&h->elf))
10496 return ppc_stub_none;
10498 else if (elf_local_got_ents (input_sec->owner) != NULL)
10500 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10501 struct plt_entry **local_plt = (struct plt_entry **)
10502 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10503 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10505 if (local_plt[r_symndx] != NULL)
10507 struct plt_entry *ent;
10509 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10510 if (ent->addend == rel->r_addend
10511 && ent->plt.offset != (bfd_vma) -1)
10513 *plt_ent = ent;
10514 return ppc_stub_plt_call;
10519 /* Determine where the call point is. */
10520 location = (input_sec->output_offset
10521 + input_sec->output_section->vma
10522 + rel->r_offset);
10524 branch_offset = destination - location;
10525 r_type = ELF64_R_TYPE (rel->r_info);
10527 /* Determine if a long branch stub is needed. */
10528 max_branch_offset = 1 << 25;
10529 if (r_type == R_PPC64_REL14
10530 || r_type == R_PPC64_REL14_BRTAKEN
10531 || r_type == R_PPC64_REL14_BRNTAKEN)
10532 max_branch_offset = 1 << 15;
10534 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10535 /* We need a stub. Figure out whether a long_branch or plt_branch
10536 is needed later. */
10537 return ppc_stub_long_branch;
10539 return ppc_stub_none;
10542 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10543 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10544 . mflr %r12
10545 . bcl 20,31,1f
10546 .1: mflr %r11
10547 . mtlr %r12
10548 . lis %r12,xxx-1b@highest
10549 . ori %r12,%r12,xxx-1b@higher
10550 . sldi %r12,%r12,32
10551 . oris %r12,%r12,xxx-1b@high
10552 . ori %r12,%r12,xxx-1b@l
10553 . add/ldx %r12,%r11,%r12 */
10555 static bfd_byte *
10556 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10558 bfd_put_32 (abfd, MFLR_R12, p);
10559 p += 4;
10560 bfd_put_32 (abfd, BCL_20_31, p);
10561 p += 4;
10562 bfd_put_32 (abfd, MFLR_R11, p);
10563 p += 4;
10564 bfd_put_32 (abfd, MTLR_R12, p);
10565 p += 4;
10566 if (off + 0x8000 < 0x10000)
10568 if (load)
10569 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10570 else
10571 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10572 p += 4;
10574 else if (off + 0x80008000ULL < 0x100000000ULL)
10576 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10577 p += 4;
10578 if (load)
10579 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10580 else
10581 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10582 p += 4;
10584 else
10586 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10588 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10589 p += 4;
10591 else
10593 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10594 p += 4;
10595 if (((off >> 32) & 0xffff) != 0)
10597 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10598 p += 4;
10601 if (((off >> 32) & 0xffffffffULL) != 0)
10603 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10604 p += 4;
10606 if (PPC_HI (off) != 0)
10608 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10609 p += 4;
10611 if (PPC_LO (off) != 0)
10613 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10614 p += 4;
10616 if (load)
10617 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10618 else
10619 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10620 p += 4;
10622 return p;
10625 static unsigned int
10626 size_offset (bfd_vma off)
10628 unsigned int size;
10629 if (off + 0x8000 < 0x10000)
10630 size = 4;
10631 else if (off + 0x80008000ULL < 0x100000000ULL)
10632 size = 8;
10633 else
10635 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10636 size = 4;
10637 else
10639 size = 4;
10640 if (((off >> 32) & 0xffff) != 0)
10641 size += 4;
10643 if (((off >> 32) & 0xffffffffULL) != 0)
10644 size += 4;
10645 if (PPC_HI (off) != 0)
10646 size += 4;
10647 if (PPC_LO (off) != 0)
10648 size += 4;
10649 size += 4;
10651 return size + 16;
10654 static unsigned int
10655 num_relocs_for_offset (bfd_vma off)
10657 unsigned int num_rel;
10658 if (off + 0x8000 < 0x10000)
10659 num_rel = 1;
10660 else if (off + 0x80008000ULL < 0x100000000ULL)
10661 num_rel = 2;
10662 else
10664 num_rel = 1;
10665 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10666 && ((off >> 32) & 0xffff) != 0)
10667 num_rel += 1;
10668 if (PPC_HI (off) != 0)
10669 num_rel += 1;
10670 if (PPC_LO (off) != 0)
10671 num_rel += 1;
10673 return num_rel;
10676 static Elf_Internal_Rela *
10677 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10678 bfd_vma roff, bfd_vma targ, bfd_vma off)
10680 bfd_vma relative_targ = targ - (roff - 8);
10681 if (bfd_big_endian (info->output_bfd))
10682 roff += 2;
10683 r->r_offset = roff;
10684 r->r_addend = relative_targ + roff;
10685 if (off + 0x8000 < 0x10000)
10686 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10687 else if (off + 0x80008000ULL < 0x100000000ULL)
10689 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10690 ++r;
10691 roff += 4;
10692 r->r_offset = roff;
10693 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10694 r->r_addend = relative_targ + roff;
10696 else
10698 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10699 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10700 else
10702 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10703 if (((off >> 32) & 0xffff) != 0)
10705 ++r;
10706 roff += 4;
10707 r->r_offset = roff;
10708 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10709 r->r_addend = relative_targ + roff;
10712 if (((off >> 32) & 0xffffffffULL) != 0)
10713 roff += 4;
10714 if (PPC_HI (off) != 0)
10716 ++r;
10717 roff += 4;
10718 r->r_offset = roff;
10719 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10720 r->r_addend = relative_targ + roff;
10722 if (PPC_LO (off) != 0)
10724 ++r;
10725 roff += 4;
10726 r->r_offset = roff;
10727 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10728 r->r_addend = relative_targ + roff;
10731 return r;
10734 static bfd_byte *
10735 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10736 bool load)
10738 uint64_t insn;
10739 if (off - odd + (1ULL << 33) < 1ULL << 34)
10741 off -= odd;
10742 if (odd)
10744 bfd_put_32 (abfd, NOP, p);
10745 p += 4;
10747 if (load)
10748 insn = PLD_R12_PC;
10749 else
10750 insn = PADDI_R12_PC;
10751 insn |= D34 (off);
10752 bfd_put_32 (abfd, insn >> 32, p);
10753 p += 4;
10754 bfd_put_32 (abfd, insn, p);
10756 /* The minimum value for paddi is -0x200000000. The minimum value
10757 for li is -0x8000, which when shifted by 34 and added gives a
10758 minimum value of -0x2000200000000. The maximum value is
10759 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10760 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10762 off -= 8 - odd;
10763 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10764 p += 4;
10765 if (!odd)
10767 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10768 p += 4;
10770 insn = PADDI_R12_PC | D34 (off);
10771 bfd_put_32 (abfd, insn >> 32, p);
10772 p += 4;
10773 bfd_put_32 (abfd, insn, p);
10774 p += 4;
10775 if (odd)
10777 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10778 p += 4;
10780 if (load)
10781 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10782 else
10783 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10785 else
10787 off -= odd + 8;
10788 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10789 p += 4;
10790 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10791 p += 4;
10792 if (odd)
10794 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10795 p += 4;
10797 insn = PADDI_R12_PC | D34 (off);
10798 bfd_put_32 (abfd, insn >> 32, p);
10799 p += 4;
10800 bfd_put_32 (abfd, insn, p);
10801 p += 4;
10802 if (!odd)
10804 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10805 p += 4;
10807 if (load)
10808 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10809 else
10810 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10812 p += 4;
10813 return p;
10816 static unsigned int
10817 size_power10_offset (bfd_vma off, int odd)
10819 if (off - odd + (1ULL << 33) < 1ULL << 34)
10820 return odd + 8;
10821 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10822 return 20;
10823 else
10824 return 24;
10827 static unsigned int
10828 num_relocs_for_power10_offset (bfd_vma off, int odd)
10830 if (off - odd + (1ULL << 33) < 1ULL << 34)
10831 return 1;
10832 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10833 return 2;
10834 else
10835 return 3;
10838 static Elf_Internal_Rela *
10839 emit_relocs_for_power10_offset (struct bfd_link_info *info,
10840 Elf_Internal_Rela *r, bfd_vma roff,
10841 bfd_vma targ, bfd_vma off, int odd)
10843 if (off - odd + (1ULL << 33) < 1ULL << 34)
10844 roff += odd;
10845 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10847 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10848 r->r_offset = roff + d_offset;
10849 r->r_addend = targ + 8 - odd - d_offset;
10850 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10851 ++r;
10852 roff += 8 - odd;
10854 else
10856 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10857 r->r_offset = roff + d_offset;
10858 r->r_addend = targ + 8 + odd - d_offset;
10859 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10860 ++r;
10861 roff += 4;
10862 r->r_offset = roff + d_offset;
10863 r->r_addend = targ + 4 + odd - d_offset;
10864 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10865 ++r;
10866 roff += 4 + odd;
10868 r->r_offset = roff;
10869 r->r_addend = targ;
10870 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10871 return r;
10874 /* Emit .eh_frame opcode to advance pc by DELTA. */
10876 static bfd_byte *
10877 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10879 delta /= 4;
10880 if (delta < 64)
10881 *eh++ = DW_CFA_advance_loc + delta;
10882 else if (delta < 256)
10884 *eh++ = DW_CFA_advance_loc1;
10885 *eh++ = delta;
10887 else if (delta < 65536)
10889 *eh++ = DW_CFA_advance_loc2;
10890 bfd_put_16 (abfd, delta, eh);
10891 eh += 2;
10893 else
10895 *eh++ = DW_CFA_advance_loc4;
10896 bfd_put_32 (abfd, delta, eh);
10897 eh += 4;
10899 return eh;
10902 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10904 static unsigned int
10905 eh_advance_size (unsigned int delta)
10907 if (delta < 64 * 4)
10908 /* DW_CFA_advance_loc+[1..63]. */
10909 return 1;
10910 if (delta < 256 * 4)
10911 /* DW_CFA_advance_loc1, byte. */
10912 return 2;
10913 if (delta < 65536 * 4)
10914 /* DW_CFA_advance_loc2, 2 bytes. */
10915 return 3;
10916 /* DW_CFA_advance_loc4, 4 bytes. */
10917 return 5;
10920 /* With power7 weakly ordered memory model, it is possible for ld.so
10921 to update a plt entry in one thread and have another thread see a
10922 stale zero toc entry. To avoid this we need some sort of acquire
10923 barrier in the call stub. One solution is to make the load of the
10924 toc word seem to appear to depend on the load of the function entry
10925 word. Another solution is to test for r2 being zero, and branch to
10926 the appropriate glink entry if so.
10928 . fake dep barrier compare
10929 . ld 12,xxx(2) ld 12,xxx(2)
10930 . mtctr 12 mtctr 12
10931 . xor 11,12,12 ld 2,xxx+8(2)
10932 . add 2,2,11 cmpldi 2,0
10933 . ld 2,xxx+8(2) bnectr+
10934 . bctr b <glink_entry>
10936 The solution involving the compare turns out to be faster, so
10937 that's what we use unless the branch won't reach. */
10939 #define ALWAYS_USE_FAKE_DEP 0
10940 #define ALWAYS_EMIT_R2SAVE 0
10942 static inline unsigned int
10943 plt_stub_size (struct ppc_link_hash_table *htab,
10944 struct ppc_stub_hash_entry *stub_entry,
10945 bfd_vma off,
10946 unsigned int odd)
10948 unsigned size;
10950 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10952 if (htab->params->power10_stubs != 0)
10953 size = 8 + size_power10_offset (off, odd);
10954 else
10955 size = 8 + size_offset (off - 8);
10956 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10957 size += 4;
10959 else
10961 size = 12;
10962 if (ALWAYS_EMIT_R2SAVE
10963 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10964 size += 4;
10965 if (PPC_HA (off) != 0)
10966 size += 4;
10967 if (htab->opd_abi)
10969 size += 4;
10970 if (htab->params->plt_static_chain)
10971 size += 4;
10972 if (htab->params->plt_thread_safe
10973 && htab->elf.dynamic_sections_created
10974 && stub_entry->h != NULL
10975 && stub_entry->h->elf.dynindx != -1)
10976 size += 8;
10977 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
10978 != PPC_HA (off))
10979 size += 4;
10982 if (stub_entry->h != NULL
10983 && is_tls_get_addr (&stub_entry->h->elf, htab)
10984 && htab->params->tls_get_addr_opt)
10986 if (!htab->params->no_tls_get_addr_regsave)
10988 size += 30 * 4;
10989 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10990 || stub_entry->stub_type == ppc_stub_plt_call_both)
10991 size += 4;
10993 else
10995 size += 7 * 4;
10996 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10997 || stub_entry->stub_type == ppc_stub_plt_call_both)
10998 size += 6 * 4;
11001 return size;
11004 /* Depending on the sign of plt_stub_align:
11005 If positive, return the padding to align to a 2**plt_stub_align
11006 boundary.
11007 If negative, if this stub would cross fewer 2**plt_stub_align
11008 boundaries if we align, then return the padding needed to do so. */
11010 static inline unsigned int
11011 plt_stub_pad (struct ppc_link_hash_table *htab,
11012 struct ppc_stub_hash_entry *stub_entry,
11013 bfd_vma plt_off,
11014 unsigned int odd)
11016 int stub_align;
11017 unsigned stub_size;
11018 bfd_vma stub_off = stub_entry->group->stub_sec->size;
11020 if (htab->params->plt_stub_align >= 0)
11022 stub_align = 1 << htab->params->plt_stub_align;
11023 if ((stub_off & (stub_align - 1)) != 0)
11024 return stub_align - (stub_off & (stub_align - 1));
11025 return 0;
11028 stub_align = 1 << -htab->params->plt_stub_align;
11029 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
11030 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11031 > ((stub_size - 1) & -stub_align))
11032 return stub_align - (stub_off & (stub_align - 1));
11033 return 0;
11036 /* Build a .plt call stub. */
11038 static inline bfd_byte *
11039 build_plt_stub (struct ppc_link_hash_table *htab,
11040 struct ppc_stub_hash_entry *stub_entry,
11041 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11043 bfd *obfd = htab->params->stub_bfd;
11044 bool plt_load_toc = htab->opd_abi;
11045 bool plt_static_chain = htab->params->plt_static_chain;
11046 bool plt_thread_safe = (htab->params->plt_thread_safe
11047 && htab->elf.dynamic_sections_created
11048 && stub_entry->h != NULL
11049 && stub_entry->h->elf.dynindx != -1);
11050 bool use_fake_dep = plt_thread_safe;
11051 bfd_vma cmp_branch_off = 0;
11053 if (!ALWAYS_USE_FAKE_DEP
11054 && plt_load_toc
11055 && plt_thread_safe
11056 && !(stub_entry->h != NULL
11057 && is_tls_get_addr (&stub_entry->h->elf, htab)
11058 && htab->params->tls_get_addr_opt))
11060 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11061 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11062 / PLT_ENTRY_SIZE (htab));
11063 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11064 bfd_vma to, from;
11066 if (pltindex > 32768)
11067 glinkoff += (pltindex - 32768) * 4;
11068 to = (glinkoff
11069 + htab->glink->output_offset
11070 + htab->glink->output_section->vma);
11071 from = (p - stub_entry->group->stub_sec->contents
11072 + 4 * (ALWAYS_EMIT_R2SAVE
11073 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11074 + 4 * (PPC_HA (offset) != 0)
11075 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11076 != PPC_HA (offset))
11077 + 4 * (plt_static_chain != 0)
11078 + 20
11079 + stub_entry->group->stub_sec->output_offset
11080 + stub_entry->group->stub_sec->output_section->vma);
11081 cmp_branch_off = to - from;
11082 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11085 if (PPC_HA (offset) != 0)
11087 if (r != NULL)
11089 if (ALWAYS_EMIT_R2SAVE
11090 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11091 r[0].r_offset += 4;
11092 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11093 r[1].r_offset = r[0].r_offset + 4;
11094 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11095 r[1].r_addend = r[0].r_addend;
11096 if (plt_load_toc)
11098 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11100 r[2].r_offset = r[1].r_offset + 4;
11101 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11102 r[2].r_addend = r[0].r_addend;
11104 else
11106 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11107 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11108 r[2].r_addend = r[0].r_addend + 8;
11109 if (plt_static_chain)
11111 r[3].r_offset = r[2].r_offset + 4;
11112 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11113 r[3].r_addend = r[0].r_addend + 16;
11118 if (ALWAYS_EMIT_R2SAVE
11119 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11120 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11121 if (plt_load_toc)
11123 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11124 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11126 else
11128 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11129 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11131 if (plt_load_toc
11132 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11134 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11135 offset = 0;
11137 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11138 if (plt_load_toc)
11140 if (use_fake_dep)
11142 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11143 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11145 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11146 if (plt_static_chain)
11147 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11150 else
11152 if (r != NULL)
11154 if (ALWAYS_EMIT_R2SAVE
11155 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11156 r[0].r_offset += 4;
11157 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11158 if (plt_load_toc)
11160 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11162 r[1].r_offset = r[0].r_offset + 4;
11163 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11164 r[1].r_addend = r[0].r_addend;
11166 else
11168 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11169 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11170 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11171 if (plt_static_chain)
11173 r[2].r_offset = r[1].r_offset + 4;
11174 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11175 r[2].r_addend = r[0].r_addend + 8;
11180 if (ALWAYS_EMIT_R2SAVE
11181 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11182 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11183 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11184 if (plt_load_toc
11185 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11187 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11188 offset = 0;
11190 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11191 if (plt_load_toc)
11193 if (use_fake_dep)
11195 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11196 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11198 if (plt_static_chain)
11199 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11200 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11203 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11205 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11206 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11207 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11209 else
11210 bfd_put_32 (obfd, BCTR, p), p += 4;
11211 return p;
11214 /* Build a special .plt call stub for __tls_get_addr. */
11216 #define LD_R0_0R3 0xe8030000
11217 #define LD_R12_0R3 0xe9830000
11218 #define MR_R0_R3 0x7c601b78
11219 #define CMPDI_R0_0 0x2c200000
11220 #define ADD_R3_R12_R13 0x7c6c6a14
11221 #define BEQLR 0x4d820020
11222 #define MR_R3_R0 0x7c030378
11223 #define BCTRL 0x4e800421
11225 static bfd_byte *
11226 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11227 struct ppc_stub_hash_entry *stub_entry,
11228 bfd_byte *p)
11230 bfd *obfd = htab->params->stub_bfd;
11232 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11233 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11234 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11235 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11236 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11237 bfd_put_32 (obfd, BEQLR, p), p += 4;
11238 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11240 if (!htab->params->no_tls_get_addr_regsave)
11241 p = tls_get_addr_prologue (obfd, p, htab);
11242 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11243 || stub_entry->stub_type == ppc_stub_plt_call_both)
11245 bfd_put_32 (obfd, MFLR_R0, p);
11246 p += 4;
11247 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11248 p += 4;
11250 return p;
11253 static bfd_byte *
11254 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11255 struct ppc_stub_hash_entry *stub_entry,
11256 bfd_byte *p,
11257 bfd_byte *loc)
11259 bfd *obfd = htab->params->stub_bfd;
11261 if (!htab->params->no_tls_get_addr_regsave)
11263 bfd_put_32 (obfd, BCTRL, p - 4);
11265 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11266 || stub_entry->stub_type == ppc_stub_plt_call_both)
11268 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11269 p += 4;
11271 p = tls_get_addr_epilogue (obfd, p, htab);
11273 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11274 || stub_entry->stub_type == ppc_stub_plt_call_both)
11276 bfd_put_32 (obfd, BCTRL, p - 4);
11278 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11279 p += 4;
11280 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11281 p += 4;
11282 bfd_put_32 (obfd, MTLR_R0, p);
11283 p += 4;
11284 bfd_put_32 (obfd, BLR, p);
11285 p += 4;
11288 if (htab->glink_eh_frame != NULL
11289 && htab->glink_eh_frame->size != 0)
11291 bfd_byte *base, *eh;
11293 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11294 eh = base + stub_entry->group->eh_size;
11296 if (!htab->params->no_tls_get_addr_regsave)
11298 unsigned int cfa_updt, delta, i;
11300 /* After the bctrl, lr has been modified so we need to emit
11301 .eh_frame info saying the return address is on the stack. In
11302 fact we must put the EH info at or before the call rather
11303 than after it, because the EH info for a call needs to be
11304 specified by that point.
11305 See libgcc/unwind-dw2.c execute_cfa_program.
11306 Any stack pointer update must be described immediately after
11307 the instruction making the change, and since the stdu occurs
11308 after saving regs we put all the reg saves and the cfa
11309 change there. */
11310 cfa_updt = stub_entry->stub_offset + 18 * 4;
11311 delta = cfa_updt - stub_entry->group->lr_restore;
11312 stub_entry->group->lr_restore
11313 = stub_entry->stub_offset + (p - loc) - 4;
11314 eh = eh_advance (htab->elf.dynobj, eh, delta);
11315 *eh++ = DW_CFA_def_cfa_offset;
11316 if (htab->opd_abi)
11318 *eh++ = 128;
11319 *eh++ = 1;
11321 else
11322 *eh++ = 96;
11323 *eh++ = DW_CFA_offset_extended_sf;
11324 *eh++ = 65;
11325 *eh++ = (-16 / 8) & 0x7f;
11326 for (i = 4; i < 12; i++)
11328 *eh++ = DW_CFA_offset + i;
11329 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11331 *eh++ = (DW_CFA_advance_loc
11332 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11333 *eh++ = DW_CFA_def_cfa_offset;
11334 *eh++ = 0;
11335 for (i = 4; i < 12; i++)
11336 *eh++ = DW_CFA_restore + i;
11337 *eh++ = DW_CFA_advance_loc + 2;
11338 *eh++ = DW_CFA_restore_extended;
11339 *eh++ = 65;
11340 stub_entry->group->eh_size = eh - base;
11342 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11343 || stub_entry->stub_type == ppc_stub_plt_call_both)
11345 unsigned int lr_used, delta;
11347 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11348 delta = lr_used - stub_entry->group->lr_restore;
11349 stub_entry->group->lr_restore = lr_used + 16;
11350 eh = eh_advance (htab->elf.dynobj, eh, delta);
11351 *eh++ = DW_CFA_offset_extended_sf;
11352 *eh++ = 65;
11353 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11354 *eh++ = DW_CFA_advance_loc + 4;
11355 *eh++ = DW_CFA_restore_extended;
11356 *eh++ = 65;
11357 stub_entry->group->eh_size = eh - base;
11360 return p;
11363 static Elf_Internal_Rela *
11364 get_relocs (asection *sec, int count)
11366 Elf_Internal_Rela *relocs;
11367 struct bfd_elf_section_data *elfsec_data;
11369 elfsec_data = elf_section_data (sec);
11370 relocs = elfsec_data->relocs;
11371 if (relocs == NULL)
11373 bfd_size_type relsize;
11374 relsize = sec->reloc_count * sizeof (*relocs);
11375 relocs = bfd_alloc (sec->owner, relsize);
11376 if (relocs == NULL)
11377 return NULL;
11378 elfsec_data->relocs = relocs;
11379 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11380 sizeof (Elf_Internal_Shdr));
11381 if (elfsec_data->rela.hdr == NULL)
11382 return NULL;
11383 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11384 * sizeof (Elf64_External_Rela));
11385 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11386 sec->reloc_count = 0;
11388 relocs += sec->reloc_count;
11389 sec->reloc_count += count;
11390 return relocs;
11393 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11394 forms, to the equivalent relocs against the global symbol given by
11395 STUB_ENTRY->H. */
11397 static bool
11398 use_global_in_relocs (struct ppc_link_hash_table *htab,
11399 struct ppc_stub_hash_entry *stub_entry,
11400 Elf_Internal_Rela *r, unsigned int num_rel)
11402 struct elf_link_hash_entry **hashes;
11403 unsigned long symndx;
11404 struct ppc_link_hash_entry *h;
11405 bfd_vma symval;
11407 /* Relocs are always against symbols in their own object file. Fake
11408 up global sym hashes for the stub bfd (which has no symbols). */
11409 hashes = elf_sym_hashes (htab->params->stub_bfd);
11410 if (hashes == NULL)
11412 bfd_size_type hsize;
11414 /* When called the first time, stub_globals will contain the
11415 total number of symbols seen during stub sizing. After
11416 allocating, stub_globals is used as an index to fill the
11417 hashes array. */
11418 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11419 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11420 if (hashes == NULL)
11421 return false;
11422 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11423 htab->stub_globals = 1;
11425 symndx = htab->stub_globals++;
11426 h = stub_entry->h;
11427 hashes[symndx] = &h->elf;
11428 if (h->oh != NULL && h->oh->is_func)
11429 h = ppc_follow_link (h->oh);
11430 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11431 || h->elf.root.type == bfd_link_hash_defweak);
11432 symval = defined_sym_val (&h->elf);
11433 while (num_rel-- != 0)
11435 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11436 if (h->elf.root.u.def.section != stub_entry->target_section)
11438 /* H is an opd symbol. The addend must be zero, and the
11439 branch reloc is the only one we can convert. */
11440 r->r_addend = 0;
11441 break;
11443 else
11444 r->r_addend -= symval;
11445 --r;
11447 return true;
11450 static bfd_vma
11451 get_r2off (struct bfd_link_info *info,
11452 struct ppc_stub_hash_entry *stub_entry)
11454 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11455 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11457 if (r2off == 0)
11459 /* Support linking -R objects. Get the toc pointer from the
11460 opd entry. */
11461 char buf[8];
11462 if (!htab->opd_abi)
11463 return r2off;
11464 asection *opd = stub_entry->h->elf.root.u.def.section;
11465 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11467 if (strcmp (opd->name, ".opd") != 0
11468 || opd->reloc_count != 0)
11470 info->callbacks->einfo
11471 (_("%P: cannot find opd entry toc for `%pT'\n"),
11472 stub_entry->h->elf.root.root.string);
11473 bfd_set_error (bfd_error_bad_value);
11474 return (bfd_vma) -1;
11476 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11477 return (bfd_vma) -1;
11478 r2off = bfd_get_64 (opd->owner, buf);
11479 r2off -= elf_gp (info->output_bfd);
11481 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11482 return r2off;
11485 static bool
11486 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11488 struct ppc_stub_hash_entry *stub_entry;
11489 struct ppc_branch_hash_entry *br_entry;
11490 struct bfd_link_info *info;
11491 struct ppc_link_hash_table *htab;
11492 bfd *obfd;
11493 bfd_byte *loc;
11494 bfd_byte *p, *relp;
11495 bfd_vma targ, off;
11496 Elf_Internal_Rela *r;
11497 asection *plt;
11498 int num_rel;
11499 int odd;
11500 bool is_tga;
11502 /* Massage our args to the form they really have. */
11503 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11504 info = in_arg;
11506 /* Fail if the target section could not be assigned to an output
11507 section. The user should fix his linker script. */
11508 if (stub_entry->target_section != NULL
11509 && stub_entry->target_section->output_section == NULL
11510 && info->non_contiguous_regions)
11511 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11512 "Retry without --enable-non-contiguous-regions.\n"),
11513 stub_entry->target_section);
11515 /* Same for the group. */
11516 if (stub_entry->group->stub_sec != NULL
11517 && stub_entry->group->stub_sec->output_section == NULL
11518 && info->non_contiguous_regions)
11519 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11520 "output section. Retry without "
11521 "--enable-non-contiguous-regions.\n"),
11522 stub_entry->group->stub_sec,
11523 stub_entry->target_section);
11525 htab = ppc_hash_table (info);
11526 if (htab == NULL)
11527 return false;
11529 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11530 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11532 htab->stub_count[stub_entry->stub_type - 1] += 1;
11533 switch (stub_entry->stub_type)
11535 case ppc_stub_long_branch:
11536 case ppc_stub_long_branch_r2off:
11537 /* Branches are relative. This is where we are going to. */
11538 targ = (stub_entry->target_value
11539 + stub_entry->target_section->output_offset
11540 + stub_entry->target_section->output_section->vma);
11541 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11543 /* And this is where we are coming from. */
11544 off = (stub_entry->stub_offset
11545 + stub_entry->group->stub_sec->output_offset
11546 + stub_entry->group->stub_sec->output_section->vma);
11547 off = targ - off;
11549 p = loc;
11550 obfd = htab->params->stub_bfd;
11551 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11553 bfd_vma r2off = get_r2off (info, stub_entry);
11555 if (r2off == (bfd_vma) -1)
11557 htab->stub_error = true;
11558 return false;
11560 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11561 p += 4;
11562 if (PPC_HA (r2off) != 0)
11564 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11565 p += 4;
11567 if (PPC_LO (r2off) != 0)
11569 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11570 p += 4;
11572 off -= p - loc;
11574 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11575 p += 4;
11577 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11579 _bfd_error_handler
11580 (_("long branch stub `%s' offset overflow"),
11581 stub_entry->root.string);
11582 htab->stub_error = true;
11583 return false;
11586 if (info->emitrelocations)
11588 r = get_relocs (stub_entry->group->stub_sec, 1);
11589 if (r == NULL)
11590 return false;
11591 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11592 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11593 r->r_addend = targ;
11594 if (stub_entry->h != NULL
11595 && !use_global_in_relocs (htab, stub_entry, r, 1))
11596 return false;
11598 break;
11600 case ppc_stub_plt_branch:
11601 case ppc_stub_plt_branch_r2off:
11602 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11603 stub_entry->root.string + 9,
11604 false, false);
11605 if (br_entry == NULL)
11607 _bfd_error_handler (_("can't find branch stub `%s'"),
11608 stub_entry->root.string);
11609 htab->stub_error = true;
11610 return false;
11613 targ = (stub_entry->target_value
11614 + stub_entry->target_section->output_offset
11615 + stub_entry->target_section->output_section->vma);
11616 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11617 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11619 bfd_put_64 (htab->brlt->owner, targ,
11620 htab->brlt->contents + br_entry->offset);
11622 if (br_entry->iter == htab->stub_iteration)
11624 br_entry->iter = 0;
11626 if (htab->relbrlt != NULL)
11628 /* Create a reloc for the branch lookup table entry. */
11629 Elf_Internal_Rela rela;
11630 bfd_byte *rl;
11632 rela.r_offset = (br_entry->offset
11633 + htab->brlt->output_offset
11634 + htab->brlt->output_section->vma);
11635 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11636 rela.r_addend = targ;
11638 rl = htab->relbrlt->contents;
11639 rl += (htab->relbrlt->reloc_count++
11640 * sizeof (Elf64_External_Rela));
11641 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11643 else if (info->emitrelocations)
11645 r = get_relocs (htab->brlt, 1);
11646 if (r == NULL)
11647 return false;
11648 /* brlt, being SEC_LINKER_CREATED does not go through the
11649 normal reloc processing. Symbols and offsets are not
11650 translated from input file to output file form, so
11651 set up the offset per the output file. */
11652 r->r_offset = (br_entry->offset
11653 + htab->brlt->output_offset
11654 + htab->brlt->output_section->vma);
11655 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11656 r->r_addend = targ;
11660 targ = (br_entry->offset
11661 + htab->brlt->output_offset
11662 + htab->brlt->output_section->vma);
11664 off = (elf_gp (info->output_bfd)
11665 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11666 off = targ - off;
11668 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11670 info->callbacks->einfo
11671 (_("%P: linkage table error against `%pT'\n"),
11672 stub_entry->root.string);
11673 bfd_set_error (bfd_error_bad_value);
11674 htab->stub_error = true;
11675 return false;
11678 if (info->emitrelocations)
11680 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11681 if (r == NULL)
11682 return false;
11683 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11684 if (bfd_big_endian (info->output_bfd))
11685 r[0].r_offset += 2;
11686 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11687 r[0].r_offset += 4;
11688 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11689 r[0].r_addend = targ;
11690 if (PPC_HA (off) != 0)
11692 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11693 r[1].r_offset = r[0].r_offset + 4;
11694 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11695 r[1].r_addend = r[0].r_addend;
11699 p = loc;
11700 obfd = htab->params->stub_bfd;
11701 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11703 if (PPC_HA (off) != 0)
11705 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11706 p += 4;
11707 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11709 else
11710 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11712 else
11714 bfd_vma r2off = get_r2off (info, stub_entry);
11716 if (r2off == (bfd_vma) -1)
11718 htab->stub_error = true;
11719 return false;
11722 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11723 p += 4;
11724 if (PPC_HA (off) != 0)
11726 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11727 p += 4;
11728 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11730 else
11731 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11733 if (PPC_HA (r2off) != 0)
11735 p += 4;
11736 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11738 if (PPC_LO (r2off) != 0)
11740 p += 4;
11741 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11744 p += 4;
11745 bfd_put_32 (obfd, MTCTR_R12, p);
11746 p += 4;
11747 bfd_put_32 (obfd, BCTR, p);
11748 p += 4;
11749 break;
11751 case ppc_stub_long_branch_notoc:
11752 case ppc_stub_long_branch_both:
11753 case ppc_stub_plt_branch_notoc:
11754 case ppc_stub_plt_branch_both:
11755 case ppc_stub_plt_call_notoc:
11756 case ppc_stub_plt_call_both:
11757 p = loc;
11758 off = (stub_entry->stub_offset
11759 + stub_entry->group->stub_sec->output_offset
11760 + stub_entry->group->stub_sec->output_section->vma);
11761 obfd = htab->params->stub_bfd;
11762 is_tga = ((stub_entry->stub_type == ppc_stub_plt_call_notoc
11763 || stub_entry->stub_type == ppc_stub_plt_call_both)
11764 && stub_entry->h != NULL
11765 && is_tls_get_addr (&stub_entry->h->elf, htab)
11766 && htab->params->tls_get_addr_opt);
11767 if (is_tga)
11769 p = build_tls_get_addr_head (htab, stub_entry, p);
11770 off += p - loc;
11772 if (stub_entry->stub_type == ppc_stub_long_branch_both
11773 || stub_entry->stub_type == ppc_stub_plt_branch_both
11774 || stub_entry->stub_type == ppc_stub_plt_call_both)
11776 off += 4;
11777 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11778 p += 4;
11780 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11782 targ = stub_entry->plt_ent->plt.offset & ~1;
11783 if (targ >= (bfd_vma) -2)
11784 abort ();
11786 plt = htab->elf.splt;
11787 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11789 if (stub_entry->symtype == STT_GNU_IFUNC)
11790 plt = htab->elf.iplt;
11791 else
11792 plt = htab->pltlocal;
11794 targ += plt->output_offset + plt->output_section->vma;
11796 else
11797 targ = (stub_entry->target_value
11798 + stub_entry->target_section->output_offset
11799 + stub_entry->target_section->output_section->vma);
11800 odd = off & 4;
11801 off = targ - off;
11803 relp = p;
11804 num_rel = 0;
11805 if (htab->params->power10_stubs != 0)
11807 bool load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11808 p = build_power10_offset (obfd, p, off, odd, load);
11810 else
11812 if (htab->glink_eh_frame != NULL
11813 && htab->glink_eh_frame->size != 0)
11815 bfd_byte *base, *eh;
11816 unsigned int lr_used, delta;
11818 base = (htab->glink_eh_frame->contents
11819 + stub_entry->group->eh_base + 17);
11820 eh = base + stub_entry->group->eh_size;
11821 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11822 delta = lr_used - stub_entry->group->lr_restore;
11823 stub_entry->group->lr_restore = lr_used + 8;
11824 eh = eh_advance (htab->elf.dynobj, eh, delta);
11825 *eh++ = DW_CFA_register;
11826 *eh++ = 65;
11827 *eh++ = 12;
11828 *eh++ = DW_CFA_advance_loc + 2;
11829 *eh++ = DW_CFA_restore_extended;
11830 *eh++ = 65;
11831 stub_entry->group->eh_size = eh - base;
11834 /* The notoc stubs calculate their target (either a PLT entry or
11835 the global entry point of a function) relative to the PC
11836 returned by the "bcl" two instructions past the start of the
11837 sequence emitted by build_offset. The offset is therefore 8
11838 less than calculated from the start of the sequence. */
11839 off -= 8;
11840 p = build_offset (obfd, p, off,
11841 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11844 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11846 bfd_vma from;
11847 num_rel = 1;
11848 from = (stub_entry->stub_offset
11849 + stub_entry->group->stub_sec->output_offset
11850 + stub_entry->group->stub_sec->output_section->vma
11851 + (p - loc));
11852 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
11854 else
11856 bfd_put_32 (obfd, MTCTR_R12, p);
11857 p += 4;
11858 bfd_put_32 (obfd, BCTR, p);
11860 p += 4;
11862 if (is_tga)
11863 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11865 if (info->emitrelocations)
11867 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11868 if (htab->params->power10_stubs != 0)
11869 num_rel += num_relocs_for_power10_offset (off, odd);
11870 else
11872 num_rel += num_relocs_for_offset (off);
11873 roff += 16;
11875 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11876 if (r == NULL)
11877 return false;
11878 if (htab->params->power10_stubs != 0)
11879 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
11880 else
11881 r = emit_relocs_for_offset (info, r, roff, targ, off);
11882 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11883 || stub_entry->stub_type == ppc_stub_long_branch_both)
11885 ++r;
11886 roff = p - 4 - stub_entry->group->stub_sec->contents;
11887 r->r_offset = roff;
11888 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11889 r->r_addend = targ;
11890 if (stub_entry->h != NULL
11891 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11892 return false;
11895 break;
11897 case ppc_stub_plt_call:
11898 case ppc_stub_plt_call_r2save:
11899 if (stub_entry->h != NULL
11900 && stub_entry->h->is_func_descriptor
11901 && stub_entry->h->oh != NULL)
11903 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11905 /* If the old-ABI "dot-symbol" is undefined make it weak so
11906 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11907 if (fh->elf.root.type == bfd_link_hash_undefined
11908 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11909 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11910 fh->elf.root.type = bfd_link_hash_undefweak;
11913 /* Now build the stub. */
11914 targ = stub_entry->plt_ent->plt.offset & ~1;
11915 if (targ >= (bfd_vma) -2)
11916 abort ();
11918 plt = htab->elf.splt;
11919 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11921 if (stub_entry->symtype == STT_GNU_IFUNC)
11922 plt = htab->elf.iplt;
11923 else
11924 plt = htab->pltlocal;
11926 targ += plt->output_offset + plt->output_section->vma;
11928 off = (elf_gp (info->output_bfd)
11929 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11930 off = targ - off;
11932 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11934 info->callbacks->einfo
11935 /* xgettext:c-format */
11936 (_("%P: linkage table error against `%pT'\n"),
11937 stub_entry->h != NULL
11938 ? stub_entry->h->elf.root.root.string
11939 : "<local sym>");
11940 bfd_set_error (bfd_error_bad_value);
11941 htab->stub_error = true;
11942 return false;
11945 r = NULL;
11946 if (info->emitrelocations)
11948 r = get_relocs (stub_entry->group->stub_sec,
11949 ((PPC_HA (off) != 0)
11950 + (htab->opd_abi
11951 ? 2 + (htab->params->plt_static_chain
11952 && PPC_HA (off + 16) == PPC_HA (off))
11953 : 1)));
11954 if (r == NULL)
11955 return false;
11956 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11957 if (bfd_big_endian (info->output_bfd))
11958 r[0].r_offset += 2;
11959 r[0].r_addend = targ;
11961 p = loc;
11962 obfd = htab->params->stub_bfd;
11963 is_tga = (stub_entry->h != NULL
11964 && is_tls_get_addr (&stub_entry->h->elf, htab)
11965 && htab->params->tls_get_addr_opt);
11966 if (is_tga)
11968 p = build_tls_get_addr_head (htab, stub_entry, p);
11969 if (r != NULL)
11970 r[0].r_offset += p - loc;
11972 p = build_plt_stub (htab, stub_entry, p, off, r);
11973 if (is_tga)
11974 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11975 break;
11977 case ppc_stub_save_res:
11978 return true;
11980 default:
11981 BFD_FAIL ();
11982 return false;
11985 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11987 if (htab->params->emit_stub_syms)
11989 struct elf_link_hash_entry *h;
11990 size_t len1, len2;
11991 char *name;
11992 const char *const stub_str[] = { "long_branch",
11993 "long_branch",
11994 "long_branch",
11995 "long_branch",
11996 "plt_branch",
11997 "plt_branch",
11998 "plt_branch",
11999 "plt_branch",
12000 "plt_call",
12001 "plt_call",
12002 "plt_call",
12003 "plt_call" };
12005 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
12006 len2 = strlen (stub_entry->root.string);
12007 name = bfd_malloc (len1 + len2 + 2);
12008 if (name == NULL)
12009 return false;
12010 memcpy (name, stub_entry->root.string, 9);
12011 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
12012 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12013 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12014 if (h == NULL)
12015 return false;
12016 if (h->root.type == bfd_link_hash_new)
12018 h->root.type = bfd_link_hash_defined;
12019 h->root.u.def.section = stub_entry->group->stub_sec;
12020 h->root.u.def.value = stub_entry->stub_offset;
12021 h->ref_regular = 1;
12022 h->def_regular = 1;
12023 h->ref_regular_nonweak = 1;
12024 h->forced_local = 1;
12025 h->non_elf = 0;
12026 h->root.linker_def = 1;
12030 return true;
12033 /* As above, but don't actually build the stub. Just bump offset so
12034 we know stub section sizes, and select plt_branch stubs where
12035 long_branch stubs won't do. */
12037 static bool
12038 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12040 struct ppc_stub_hash_entry *stub_entry;
12041 struct bfd_link_info *info;
12042 struct ppc_link_hash_table *htab;
12043 asection *plt;
12044 bfd_vma targ, off, r2off;
12045 unsigned int size, extra, lr_used, delta, odd;
12047 /* Massage our args to the form they really have. */
12048 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12049 info = in_arg;
12051 htab = ppc_hash_table (info);
12052 if (htab == NULL)
12053 return false;
12055 /* Fail if the target section could not be assigned to an output
12056 section. The user should fix his linker script. */
12057 if (stub_entry->target_section != NULL
12058 && stub_entry->target_section->output_section == NULL
12059 && info->non_contiguous_regions)
12060 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12061 "Retry without --enable-non-contiguous-regions.\n"),
12062 stub_entry->target_section);
12064 /* Same for the group. */
12065 if (stub_entry->group->stub_sec != NULL
12066 && stub_entry->group->stub_sec->output_section == NULL
12067 && info->non_contiguous_regions)
12068 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12069 "output section. Retry without "
12070 "--enable-non-contiguous-regions.\n"),
12071 stub_entry->group->stub_sec,
12072 stub_entry->target_section);
12074 /* Make a note of the offset within the stubs for this entry. */
12075 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12077 if (stub_entry->h != NULL
12078 && stub_entry->h->save_res
12079 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12080 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12082 /* Don't make stubs to out-of-line register save/restore
12083 functions. Instead, emit copies of the functions. */
12084 stub_entry->group->needs_save_res = 1;
12085 stub_entry->stub_type = ppc_stub_save_res;
12086 return true;
12089 switch (stub_entry->stub_type)
12091 case ppc_stub_plt_branch:
12092 case ppc_stub_plt_branch_r2off:
12093 /* Reset the stub type from the plt branch variant in case we now
12094 can reach with a shorter stub. */
12095 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12096 /* Fall through. */
12097 case ppc_stub_long_branch:
12098 case ppc_stub_long_branch_r2off:
12099 targ = (stub_entry->target_value
12100 + stub_entry->target_section->output_offset
12101 + stub_entry->target_section->output_section->vma);
12102 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12103 off = (stub_entry->stub_offset
12104 + stub_entry->group->stub_sec->output_offset
12105 + stub_entry->group->stub_sec->output_section->vma);
12107 size = 4;
12108 r2off = 0;
12109 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12111 r2off = get_r2off (info, stub_entry);
12112 if (r2off == (bfd_vma) -1)
12114 htab->stub_error = true;
12115 return false;
12117 size = 8;
12118 if (PPC_HA (r2off) != 0)
12119 size += 4;
12120 if (PPC_LO (r2off) != 0)
12121 size += 4;
12122 off += size - 4;
12124 off = targ - off;
12126 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12127 Do the same for -R objects without function descriptors. */
12128 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12129 && r2off == 0
12130 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12131 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12133 struct ppc_branch_hash_entry *br_entry;
12135 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12136 stub_entry->root.string + 9,
12137 true, false);
12138 if (br_entry == NULL)
12140 _bfd_error_handler (_("can't build branch stub `%s'"),
12141 stub_entry->root.string);
12142 htab->stub_error = true;
12143 return false;
12146 if (br_entry->iter != htab->stub_iteration)
12148 br_entry->iter = htab->stub_iteration;
12149 br_entry->offset = htab->brlt->size;
12150 htab->brlt->size += 8;
12152 if (htab->relbrlt != NULL)
12153 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12154 else if (info->emitrelocations)
12156 htab->brlt->reloc_count += 1;
12157 htab->brlt->flags |= SEC_RELOC;
12161 targ = (br_entry->offset
12162 + htab->brlt->output_offset
12163 + htab->brlt->output_section->vma);
12164 off = (elf_gp (info->output_bfd)
12165 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12166 off = targ - off;
12168 if (info->emitrelocations)
12170 stub_entry->group->stub_sec->reloc_count
12171 += 1 + (PPC_HA (off) != 0);
12172 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12175 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12176 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12178 size = 12;
12179 if (PPC_HA (off) != 0)
12180 size = 16;
12182 else
12184 size = 16;
12185 if (PPC_HA (off) != 0)
12186 size += 4;
12188 if (PPC_HA (r2off) != 0)
12189 size += 4;
12190 if (PPC_LO (r2off) != 0)
12191 size += 4;
12194 else if (info->emitrelocations)
12196 stub_entry->group->stub_sec->reloc_count += 1;
12197 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12199 break;
12201 case ppc_stub_plt_branch_notoc:
12202 case ppc_stub_plt_branch_both:
12203 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12204 /* Fall through. */
12205 case ppc_stub_long_branch_notoc:
12206 case ppc_stub_long_branch_both:
12207 off = (stub_entry->stub_offset
12208 + stub_entry->group->stub_sec->output_offset
12209 + stub_entry->group->stub_sec->output_section->vma);
12210 size = 0;
12211 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12212 size = 4;
12213 off += size;
12214 targ = (stub_entry->target_value
12215 + stub_entry->target_section->output_offset
12216 + stub_entry->target_section->output_section->vma);
12217 odd = off & 4;
12218 off = targ - off;
12220 if (info->emitrelocations)
12222 unsigned int num_rel;
12223 if (htab->params->power10_stubs != 0)
12224 num_rel = num_relocs_for_power10_offset (off, odd);
12225 else
12226 num_rel = num_relocs_for_offset (off - 8);
12227 stub_entry->group->stub_sec->reloc_count += num_rel;
12228 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12231 if (htab->params->power10_stubs != 0)
12232 extra = size_power10_offset (off, odd);
12233 else
12234 extra = size_offset (off - 8);
12235 /* Include branch insn plus those in the offset sequence. */
12236 size += 4 + extra;
12237 /* The branch insn is at the end, or "extra" bytes along. So
12238 its offset will be "extra" bytes less that that already
12239 calculated. */
12240 off -= extra;
12242 if (htab->params->power10_stubs == 0)
12244 /* After the bcl, lr has been modified so we need to emit
12245 .eh_frame info saying the return address is in r12. */
12246 lr_used = stub_entry->stub_offset + 8;
12247 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12248 lr_used += 4;
12249 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12250 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12251 DW_CFA_restore_extended 65. */
12252 delta = lr_used - stub_entry->group->lr_restore;
12253 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12254 stub_entry->group->lr_restore = lr_used + 8;
12257 /* If the branch can't reach, use a plt_branch. */
12258 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12260 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12261 - ppc_stub_long_branch_notoc);
12262 size += 4;
12264 else if (info->emitrelocations)
12265 stub_entry->group->stub_sec->reloc_count +=1;
12266 break;
12268 case ppc_stub_plt_call_notoc:
12269 case ppc_stub_plt_call_both:
12270 lr_used = 0;
12271 if (stub_entry->h != NULL
12272 && is_tls_get_addr (&stub_entry->h->elf, htab)
12273 && htab->params->tls_get_addr_opt)
12275 lr_used += 7 * 4;
12276 if (!htab->params->no_tls_get_addr_regsave)
12277 lr_used += 11 * 4;
12278 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12279 lr_used += 2 * 4;
12281 if (stub_entry->stub_type == ppc_stub_plt_call_both)
12282 lr_used += 4;
12283 targ = stub_entry->plt_ent->plt.offset & ~1;
12284 if (targ >= (bfd_vma) -2)
12285 abort ();
12287 plt = htab->elf.splt;
12288 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12290 if (stub_entry->symtype == STT_GNU_IFUNC)
12291 plt = htab->elf.iplt;
12292 else
12293 plt = htab->pltlocal;
12295 targ += plt->output_offset + plt->output_section->vma;
12296 off = (stub_entry->stub_offset
12297 + stub_entry->group->stub_sec->output_offset
12298 + stub_entry->group->stub_sec->output_section->vma
12299 + lr_used);
12300 odd = off & 4;
12301 off = targ - off;
12303 if (htab->params->plt_stub_align != 0)
12305 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
12307 stub_entry->group->stub_sec->size += pad;
12308 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12309 off -= pad;
12310 odd ^= pad & 4;
12313 if (info->emitrelocations)
12315 unsigned int num_rel;
12316 if (htab->params->power10_stubs != 0)
12317 num_rel = num_relocs_for_power10_offset (off, odd);
12318 else
12319 num_rel = num_relocs_for_offset (off - 8);
12320 stub_entry->group->stub_sec->reloc_count += num_rel;
12321 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12324 size = plt_stub_size (htab, stub_entry, off, odd);
12326 if (htab->params->power10_stubs == 0)
12328 /* After the bcl, lr has been modified so we need to emit
12329 .eh_frame info saying the return address is in r12. */
12330 lr_used += stub_entry->stub_offset + 8;
12331 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12332 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12333 DW_CFA_restore_extended 65. */
12334 delta = lr_used - stub_entry->group->lr_restore;
12335 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12336 stub_entry->group->lr_restore = lr_used + 8;
12338 if ((stub_entry->stub_type == ppc_stub_plt_call_notoc
12339 || stub_entry->stub_type == ppc_stub_plt_call_both)
12340 && stub_entry->h != NULL
12341 && is_tls_get_addr (&stub_entry->h->elf, htab)
12342 && htab->params->tls_get_addr_opt)
12344 if (!htab->params->no_tls_get_addr_regsave)
12346 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12347 delta = cfa_updt - stub_entry->group->lr_restore;
12348 stub_entry->group->eh_size += eh_advance_size (delta);
12349 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12350 stub_entry->group->lr_restore
12351 = stub_entry->stub_offset + size - 4;
12353 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12355 lr_used = stub_entry->stub_offset + size - 20;
12356 delta = lr_used - stub_entry->group->lr_restore;
12357 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12358 stub_entry->group->lr_restore
12359 = stub_entry->stub_offset + size - 4;
12362 break;
12364 case ppc_stub_plt_call:
12365 case ppc_stub_plt_call_r2save:
12366 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12367 if (targ >= (bfd_vma) -2)
12368 abort ();
12369 plt = htab->elf.splt;
12370 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12372 if (stub_entry->symtype == STT_GNU_IFUNC)
12373 plt = htab->elf.iplt;
12374 else
12375 plt = htab->pltlocal;
12377 targ += plt->output_offset + plt->output_section->vma;
12379 off = (elf_gp (info->output_bfd)
12380 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12381 off = targ - off;
12383 if (htab->params->plt_stub_align != 0)
12385 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
12387 stub_entry->group->stub_sec->size += pad;
12388 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12391 if (info->emitrelocations)
12393 stub_entry->group->stub_sec->reloc_count
12394 += ((PPC_HA (off) != 0)
12395 + (htab->opd_abi
12396 ? 2 + (htab->params->plt_static_chain
12397 && PPC_HA (off + 16) == PPC_HA (off))
12398 : 1));
12399 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12402 size = plt_stub_size (htab, stub_entry, off, 0);
12404 if (stub_entry->h != NULL
12405 && is_tls_get_addr (&stub_entry->h->elf, htab)
12406 && htab->params->tls_get_addr_opt
12407 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12409 if (!htab->params->no_tls_get_addr_regsave)
12411 /* Adjustments to r1 need to be described. */
12412 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12413 delta = cfa_updt - stub_entry->group->lr_restore;
12414 stub_entry->group->eh_size += eh_advance_size (delta);
12415 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12417 else
12419 lr_used = stub_entry->stub_offset + size - 20;
12420 /* The eh_frame info will consist of a DW_CFA_advance_loc
12421 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12422 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12423 delta = lr_used - stub_entry->group->lr_restore;
12424 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12426 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
12428 break;
12430 default:
12431 BFD_FAIL ();
12432 return false;
12435 stub_entry->group->stub_sec->size += size;
12436 return true;
12439 /* Set up various things so that we can make a list of input sections
12440 for each output section included in the link. Returns -1 on error,
12441 0 when no stubs will be needed, and 1 on success. */
12444 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12446 unsigned int id;
12447 size_t amt;
12448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12450 if (htab == NULL)
12451 return -1;
12453 htab->sec_info_arr_size = _bfd_section_id;
12454 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12455 htab->sec_info = bfd_zmalloc (amt);
12456 if (htab->sec_info == NULL)
12457 return -1;
12459 /* Set toc_off for com, und, abs and ind sections. */
12460 for (id = 0; id < 3; id++)
12461 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12463 return 1;
12466 /* Set up for first pass at multitoc partitioning. */
12468 void
12469 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12471 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12473 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12474 htab->toc_bfd = NULL;
12475 htab->toc_first_sec = NULL;
12478 /* The linker repeatedly calls this function for each TOC input section
12479 and linker generated GOT section. Group input bfds such that the toc
12480 within a group is less than 64k in size. */
12482 bool
12483 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12485 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12486 bfd_vma addr, off, limit;
12488 if (htab == NULL)
12489 return false;
12491 if (!htab->second_toc_pass)
12493 /* Keep track of the first .toc or .got section for this input bfd. */
12494 bool new_bfd = htab->toc_bfd != isec->owner;
12496 if (new_bfd)
12498 htab->toc_bfd = isec->owner;
12499 htab->toc_first_sec = isec;
12502 addr = isec->output_offset + isec->output_section->vma;
12503 off = addr - htab->toc_curr;
12504 limit = 0x80008000;
12505 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12506 limit = 0x10000;
12507 if (off + isec->size > limit)
12509 addr = (htab->toc_first_sec->output_offset
12510 + htab->toc_first_sec->output_section->vma);
12511 htab->toc_curr = addr;
12512 htab->toc_curr &= -TOC_BASE_ALIGN;
12515 /* toc_curr is the base address of this toc group. Set elf_gp
12516 for the input section to be the offset relative to the
12517 output toc base plus 0x8000. Making the input elf_gp an
12518 offset allows us to move the toc as a whole without
12519 recalculating input elf_gp. */
12520 off = htab->toc_curr - elf_gp (info->output_bfd);
12521 off += TOC_BASE_OFF;
12523 /* Die if someone uses a linker script that doesn't keep input
12524 file .toc and .got together. */
12525 if (new_bfd
12526 && elf_gp (isec->owner) != 0
12527 && elf_gp (isec->owner) != off)
12528 return false;
12530 elf_gp (isec->owner) = off;
12531 return true;
12534 /* During the second pass toc_first_sec points to the start of
12535 a toc group, and toc_curr is used to track the old elf_gp.
12536 We use toc_bfd to ensure we only look at each bfd once. */
12537 if (htab->toc_bfd == isec->owner)
12538 return true;
12539 htab->toc_bfd = isec->owner;
12541 if (htab->toc_first_sec == NULL
12542 || htab->toc_curr != elf_gp (isec->owner))
12544 htab->toc_curr = elf_gp (isec->owner);
12545 htab->toc_first_sec = isec;
12547 addr = (htab->toc_first_sec->output_offset
12548 + htab->toc_first_sec->output_section->vma);
12549 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12550 elf_gp (isec->owner) = off;
12552 return true;
12555 /* Called via elf_link_hash_traverse to merge GOT entries for global
12556 symbol H. */
12558 static bool
12559 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12561 if (h->root.type == bfd_link_hash_indirect)
12562 return true;
12564 merge_got_entries (&h->got.glist);
12566 return true;
12569 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12570 symbol H. */
12572 static bool
12573 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12575 struct got_entry *gent;
12577 if (h->root.type == bfd_link_hash_indirect)
12578 return true;
12580 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12581 if (!gent->is_indirect)
12582 allocate_got (h, (struct bfd_link_info *) inf, gent);
12583 return true;
12586 /* Called on the first multitoc pass after the last call to
12587 ppc64_elf_next_toc_section. This function removes duplicate GOT
12588 entries. */
12590 bool
12591 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12593 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12594 struct bfd *ibfd, *ibfd2;
12595 bool done_something;
12597 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12599 if (!htab->do_multi_toc)
12600 return false;
12602 /* Merge global sym got entries within a toc group. */
12603 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12605 /* And tlsld_got. */
12606 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12608 struct got_entry *ent, *ent2;
12610 if (!is_ppc64_elf (ibfd))
12611 continue;
12613 ent = ppc64_tlsld_got (ibfd);
12614 if (!ent->is_indirect
12615 && ent->got.offset != (bfd_vma) -1)
12617 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12619 if (!is_ppc64_elf (ibfd2))
12620 continue;
12622 ent2 = ppc64_tlsld_got (ibfd2);
12623 if (!ent2->is_indirect
12624 && ent2->got.offset != (bfd_vma) -1
12625 && elf_gp (ibfd2) == elf_gp (ibfd))
12627 ent2->is_indirect = true;
12628 ent2->got.ent = ent;
12634 /* Zap sizes of got sections. */
12635 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12636 htab->elf.irelplt->size -= htab->got_reli_size;
12637 htab->got_reli_size = 0;
12639 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12641 asection *got, *relgot;
12643 if (!is_ppc64_elf (ibfd))
12644 continue;
12646 got = ppc64_elf_tdata (ibfd)->got;
12647 if (got != NULL)
12649 got->rawsize = got->size;
12650 got->size = 0;
12651 relgot = ppc64_elf_tdata (ibfd)->relgot;
12652 relgot->rawsize = relgot->size;
12653 relgot->size = 0;
12657 /* Now reallocate the got, local syms first. We don't need to
12658 allocate section contents again since we never increase size. */
12659 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12661 struct got_entry **lgot_ents;
12662 struct got_entry **end_lgot_ents;
12663 struct plt_entry **local_plt;
12664 struct plt_entry **end_local_plt;
12665 unsigned char *lgot_masks;
12666 bfd_size_type locsymcount;
12667 Elf_Internal_Shdr *symtab_hdr;
12668 asection *s;
12670 if (!is_ppc64_elf (ibfd))
12671 continue;
12673 lgot_ents = elf_local_got_ents (ibfd);
12674 if (!lgot_ents)
12675 continue;
12677 symtab_hdr = &elf_symtab_hdr (ibfd);
12678 locsymcount = symtab_hdr->sh_info;
12679 end_lgot_ents = lgot_ents + locsymcount;
12680 local_plt = (struct plt_entry **) end_lgot_ents;
12681 end_local_plt = local_plt + locsymcount;
12682 lgot_masks = (unsigned char *) end_local_plt;
12683 s = ppc64_elf_tdata (ibfd)->got;
12684 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12686 struct got_entry *ent;
12688 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12690 unsigned int ent_size = 8;
12691 unsigned int rel_size = sizeof (Elf64_External_Rela);
12693 ent->got.offset = s->size;
12694 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12696 ent_size *= 2;
12697 rel_size *= 2;
12699 s->size += ent_size;
12700 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12702 htab->elf.irelplt->size += rel_size;
12703 htab->got_reli_size += rel_size;
12705 else if (bfd_link_pic (info)
12706 && !(ent->tls_type != 0
12707 && bfd_link_executable (info)))
12709 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12710 srel->size += rel_size;
12716 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12718 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12720 struct got_entry *ent;
12722 if (!is_ppc64_elf (ibfd))
12723 continue;
12725 ent = ppc64_tlsld_got (ibfd);
12726 if (!ent->is_indirect
12727 && ent->got.offset != (bfd_vma) -1)
12729 asection *s = ppc64_elf_tdata (ibfd)->got;
12730 ent->got.offset = s->size;
12731 s->size += 16;
12732 if (bfd_link_dll (info))
12734 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12735 srel->size += sizeof (Elf64_External_Rela);
12740 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12741 if (!done_something)
12742 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12744 asection *got;
12746 if (!is_ppc64_elf (ibfd))
12747 continue;
12749 got = ppc64_elf_tdata (ibfd)->got;
12750 if (got != NULL)
12752 done_something = got->rawsize != got->size;
12753 if (done_something)
12754 break;
12758 if (done_something)
12759 (*htab->params->layout_sections_again) ();
12761 /* Set up for second pass over toc sections to recalculate elf_gp
12762 on input sections. */
12763 htab->toc_bfd = NULL;
12764 htab->toc_first_sec = NULL;
12765 htab->second_toc_pass = true;
12766 return done_something;
12769 /* Called after second pass of multitoc partitioning. */
12771 void
12772 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12774 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12776 /* After the second pass, toc_curr tracks the TOC offset used
12777 for code sections below in ppc64_elf_next_input_section. */
12778 htab->toc_curr = TOC_BASE_OFF;
12781 /* No toc references were found in ISEC. If the code in ISEC makes no
12782 calls, then there's no need to use toc adjusting stubs when branching
12783 into ISEC. Actually, indirect calls from ISEC are OK as they will
12784 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12785 needed, and 2 if a cyclical call-graph was found but no other reason
12786 for a stub was detected. If called from the top level, a return of
12787 2 means the same as a return of 0. */
12789 static int
12790 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12792 int ret;
12794 /* Mark this section as checked. */
12795 isec->call_check_done = 1;
12797 /* We know none of our code bearing sections will need toc stubs. */
12798 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12799 return 0;
12801 if (isec->size == 0)
12802 return 0;
12804 if (isec->output_section == NULL)
12805 return 0;
12807 ret = 0;
12808 if (isec->reloc_count != 0)
12810 Elf_Internal_Rela *relstart, *rel;
12811 Elf_Internal_Sym *local_syms;
12812 struct ppc_link_hash_table *htab;
12814 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12815 info->keep_memory);
12816 if (relstart == NULL)
12817 return -1;
12819 /* Look for branches to outside of this section. */
12820 local_syms = NULL;
12821 htab = ppc_hash_table (info);
12822 if (htab == NULL)
12823 return -1;
12825 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12827 enum elf_ppc64_reloc_type r_type;
12828 unsigned long r_symndx;
12829 struct elf_link_hash_entry *h;
12830 struct ppc_link_hash_entry *eh;
12831 Elf_Internal_Sym *sym;
12832 asection *sym_sec;
12833 struct _opd_sec_data *opd;
12834 bfd_vma sym_value;
12835 bfd_vma dest;
12837 r_type = ELF64_R_TYPE (rel->r_info);
12838 if (r_type != R_PPC64_REL24
12839 && r_type != R_PPC64_REL24_NOTOC
12840 && r_type != R_PPC64_REL14
12841 && r_type != R_PPC64_REL14_BRTAKEN
12842 && r_type != R_PPC64_REL14_BRNTAKEN
12843 && r_type != R_PPC64_PLTCALL
12844 && r_type != R_PPC64_PLTCALL_NOTOC)
12845 continue;
12847 r_symndx = ELF64_R_SYM (rel->r_info);
12848 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12849 isec->owner))
12851 ret = -1;
12852 break;
12855 /* Calls to dynamic lib functions go through a plt call stub
12856 that uses r2. */
12857 eh = ppc_elf_hash_entry (h);
12858 if (eh != NULL
12859 && (eh->elf.plt.plist != NULL
12860 || (eh->oh != NULL
12861 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12863 ret = 1;
12864 break;
12867 if (sym_sec == NULL)
12868 /* Ignore other undefined symbols. */
12869 continue;
12871 /* Assume branches to other sections not included in the
12872 link need stubs too, to cover -R and absolute syms. */
12873 if (sym_sec->output_section == NULL)
12875 ret = 1;
12876 break;
12879 if (h == NULL)
12880 sym_value = sym->st_value;
12881 else
12883 if (h->root.type != bfd_link_hash_defined
12884 && h->root.type != bfd_link_hash_defweak)
12885 abort ();
12886 sym_value = h->root.u.def.value;
12888 sym_value += rel->r_addend;
12890 /* If this branch reloc uses an opd sym, find the code section. */
12891 opd = get_opd_info (sym_sec);
12892 if (opd != NULL)
12894 if (h == NULL && opd->adjust != NULL)
12896 long adjust;
12898 adjust = opd->adjust[OPD_NDX (sym_value)];
12899 if (adjust == -1)
12900 /* Assume deleted functions won't ever be called. */
12901 continue;
12902 sym_value += adjust;
12905 dest = opd_entry_value (sym_sec, sym_value,
12906 &sym_sec, NULL, false);
12907 if (dest == (bfd_vma) -1)
12908 continue;
12910 else
12911 dest = (sym_value
12912 + sym_sec->output_offset
12913 + sym_sec->output_section->vma);
12915 /* Ignore branch to self. */
12916 if (sym_sec == isec)
12917 continue;
12919 /* If the called function uses the toc, we need a stub. */
12920 if (sym_sec->has_toc_reloc
12921 || sym_sec->makes_toc_func_call)
12923 ret = 1;
12924 break;
12927 /* Assume any branch that needs a long branch stub might in fact
12928 need a plt_branch stub. A plt_branch stub uses r2. */
12929 else if (dest - (isec->output_offset
12930 + isec->output_section->vma
12931 + rel->r_offset) + (1 << 25)
12932 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12933 ? h->other
12934 : sym->st_other))
12936 ret = 1;
12937 break;
12940 /* If calling back to a section in the process of being
12941 tested, we can't say for sure that no toc adjusting stubs
12942 are needed, so don't return zero. */
12943 else if (sym_sec->call_check_in_progress)
12944 ret = 2;
12946 /* Branches to another section that itself doesn't have any TOC
12947 references are OK. Recursively call ourselves to check. */
12948 else if (!sym_sec->call_check_done)
12950 int recur;
12952 /* Mark current section as indeterminate, so that other
12953 sections that call back to current won't be marked as
12954 known. */
12955 isec->call_check_in_progress = 1;
12956 recur = toc_adjusting_stub_needed (info, sym_sec);
12957 isec->call_check_in_progress = 0;
12959 if (recur != 0)
12961 ret = recur;
12962 if (recur != 2)
12963 break;
12968 if (elf_symtab_hdr (isec->owner).contents
12969 != (unsigned char *) local_syms)
12970 free (local_syms);
12971 if (elf_section_data (isec)->relocs != relstart)
12972 free (relstart);
12975 if ((ret & 1) == 0
12976 && isec->map_head.s != NULL
12977 && (strcmp (isec->output_section->name, ".init") == 0
12978 || strcmp (isec->output_section->name, ".fini") == 0))
12980 if (isec->map_head.s->has_toc_reloc
12981 || isec->map_head.s->makes_toc_func_call)
12982 ret = 1;
12983 else if (!isec->map_head.s->call_check_done)
12985 int recur;
12986 isec->call_check_in_progress = 1;
12987 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12988 isec->call_check_in_progress = 0;
12989 if (recur != 0)
12990 ret = recur;
12994 if (ret == 1)
12995 isec->makes_toc_func_call = 1;
12997 return ret;
13000 /* The linker repeatedly calls this function for each input section,
13001 in the order that input sections are linked into output sections.
13002 Build lists of input sections to determine groupings between which
13003 we may insert linker stubs. */
13005 bool
13006 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13008 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13010 if (htab == NULL)
13011 return false;
13013 if ((isec->output_section->flags & SEC_CODE) != 0
13014 && isec->output_section->id < htab->sec_info_arr_size)
13016 /* This happens to make the list in reverse order,
13017 which is what we want. */
13018 htab->sec_info[isec->id].u.list
13019 = htab->sec_info[isec->output_section->id].u.list;
13020 htab->sec_info[isec->output_section->id].u.list = isec;
13023 if (htab->multi_toc_needed)
13025 /* Analyse sections that aren't already flagged as needing a
13026 valid toc pointer. Exclude .fixup for the linux kernel.
13027 .fixup contains branches, but only back to the function that
13028 hit an exception. */
13029 if (!(isec->has_toc_reloc
13030 || (isec->flags & SEC_CODE) == 0
13031 || strcmp (isec->name, ".fixup") == 0
13032 || isec->call_check_done))
13034 if (toc_adjusting_stub_needed (info, isec) < 0)
13035 return false;
13037 /* Make all sections use the TOC assigned for this object file.
13038 This will be wrong for pasted sections; We fix that in
13039 check_pasted_section(). */
13040 if (elf_gp (isec->owner) != 0)
13041 htab->toc_curr = elf_gp (isec->owner);
13044 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13045 return true;
13048 /* Check that all .init and .fini sections use the same toc, if they
13049 have toc relocs. */
13051 static bool
13052 check_pasted_section (struct bfd_link_info *info, const char *name)
13054 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13056 if (o != NULL)
13058 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13059 bfd_vma toc_off = 0;
13060 asection *i;
13062 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13063 if (i->has_toc_reloc)
13065 if (toc_off == 0)
13066 toc_off = htab->sec_info[i->id].toc_off;
13067 else if (toc_off != htab->sec_info[i->id].toc_off)
13068 return false;
13071 if (toc_off == 0)
13072 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13073 if (i->makes_toc_func_call)
13075 toc_off = htab->sec_info[i->id].toc_off;
13076 break;
13079 /* Make sure the whole pasted function uses the same toc offset. */
13080 if (toc_off != 0)
13081 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13082 htab->sec_info[i->id].toc_off = toc_off;
13084 return true;
13087 bool
13088 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13090 return (check_pasted_section (info, ".init")
13091 & check_pasted_section (info, ".fini"));
13094 /* See whether we can group stub sections together. Grouping stub
13095 sections may result in fewer stubs. More importantly, we need to
13096 put all .init* and .fini* stubs at the beginning of the .init or
13097 .fini output sections respectively, because glibc splits the
13098 _init and _fini functions into multiple parts. Putting a stub in
13099 the middle of a function is not a good idea. */
13101 static bool
13102 group_sections (struct bfd_link_info *info,
13103 bfd_size_type stub_group_size,
13104 bool stubs_always_before_branch)
13106 struct ppc_link_hash_table *htab;
13107 asection *osec;
13108 bool suppress_size_errors;
13110 htab = ppc_hash_table (info);
13111 if (htab == NULL)
13112 return false;
13114 suppress_size_errors = false;
13115 if (stub_group_size == 1)
13117 /* Default values. */
13118 if (stubs_always_before_branch)
13119 stub_group_size = 0x1e00000;
13120 else
13121 stub_group_size = 0x1c00000;
13122 suppress_size_errors = true;
13125 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13127 asection *tail;
13129 if (osec->id >= htab->sec_info_arr_size)
13130 continue;
13132 tail = htab->sec_info[osec->id].u.list;
13133 while (tail != NULL)
13135 asection *curr;
13136 asection *prev;
13137 bfd_size_type total;
13138 bool big_sec;
13139 bfd_vma curr_toc;
13140 struct map_stub *group;
13141 bfd_size_type group_size;
13143 curr = tail;
13144 total = tail->size;
13145 group_size = (ppc64_elf_section_data (tail) != NULL
13146 && ppc64_elf_section_data (tail)->has_14bit_branch
13147 ? stub_group_size >> 10 : stub_group_size);
13149 big_sec = total > group_size;
13150 if (big_sec && !suppress_size_errors)
13151 /* xgettext:c-format */
13152 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13153 tail->owner, tail);
13154 curr_toc = htab->sec_info[tail->id].toc_off;
13156 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13157 && ((total += curr->output_offset - prev->output_offset)
13158 < (ppc64_elf_section_data (prev) != NULL
13159 && ppc64_elf_section_data (prev)->has_14bit_branch
13160 ? (group_size = stub_group_size >> 10) : group_size))
13161 && htab->sec_info[prev->id].toc_off == curr_toc)
13162 curr = prev;
13164 /* OK, the size from the start of CURR to the end is less
13165 than group_size and thus can be handled by one stub
13166 section. (or the tail section is itself larger than
13167 group_size, in which case we may be toast.) We should
13168 really be keeping track of the total size of stubs added
13169 here, as stubs contribute to the final output section
13170 size. That's a little tricky, and this way will only
13171 break if stubs added make the total size more than 2^25,
13172 ie. for the default stub_group_size, if stubs total more
13173 than 2097152 bytes, or nearly 75000 plt call stubs. */
13174 group = bfd_alloc (curr->owner, sizeof (*group));
13175 if (group == NULL)
13176 return false;
13177 group->link_sec = curr;
13178 group->stub_sec = NULL;
13179 group->needs_save_res = 0;
13180 group->lr_restore = 0;
13181 group->eh_size = 0;
13182 group->eh_base = 0;
13183 group->next = htab->group;
13184 htab->group = group;
13187 prev = htab->sec_info[tail->id].u.list;
13188 /* Set up this stub group. */
13189 htab->sec_info[tail->id].u.group = group;
13191 while (tail != curr && (tail = prev) != NULL);
13193 /* But wait, there's more! Input sections up to group_size
13194 bytes before the stub section can be handled by it too.
13195 Don't do this if we have a really large section after the
13196 stubs, as adding more stubs increases the chance that
13197 branches may not reach into the stub section. */
13198 if (!stubs_always_before_branch && !big_sec)
13200 total = 0;
13201 while (prev != NULL
13202 && ((total += tail->output_offset - prev->output_offset)
13203 < (ppc64_elf_section_data (prev) != NULL
13204 && ppc64_elf_section_data (prev)->has_14bit_branch
13205 ? (group_size = stub_group_size >> 10)
13206 : group_size))
13207 && htab->sec_info[prev->id].toc_off == curr_toc)
13209 tail = prev;
13210 prev = htab->sec_info[tail->id].u.list;
13211 htab->sec_info[tail->id].u.group = group;
13214 tail = prev;
13217 return true;
13220 static const unsigned char glink_eh_frame_cie[] =
13222 0, 0, 0, 16, /* length. */
13223 0, 0, 0, 0, /* id. */
13224 1, /* CIE version. */
13225 'z', 'R', 0, /* Augmentation string. */
13226 4, /* Code alignment. */
13227 0x78, /* Data alignment. */
13228 65, /* RA reg. */
13229 1, /* Augmentation size. */
13230 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13231 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13234 /* Stripping output sections is normally done before dynamic section
13235 symbols have been allocated. This function is called later, and
13236 handles cases like htab->brlt which is mapped to its own output
13237 section. */
13239 static void
13240 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13242 if (isec->size == 0
13243 && isec->output_section->size == 0
13244 && !(isec->output_section->flags & SEC_KEEP)
13245 && !bfd_section_removed_from_list (info->output_bfd,
13246 isec->output_section)
13247 && elf_section_data (isec->output_section)->dynindx == 0)
13249 isec->output_section->flags |= SEC_EXCLUDE;
13250 bfd_section_list_remove (info->output_bfd, isec->output_section);
13251 info->output_bfd->section_count--;
13255 /* Determine and set the size of the stub section for a final link.
13257 The basic idea here is to examine all the relocations looking for
13258 PC-relative calls to a target that is unreachable with a "bl"
13259 instruction. */
13261 bool
13262 ppc64_elf_size_stubs (struct bfd_link_info *info)
13264 bfd_size_type stub_group_size;
13265 bool stubs_always_before_branch;
13266 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13268 if (htab == NULL)
13269 return false;
13271 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13272 htab->params->power10_stubs = 0;
13274 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13275 htab->params->plt_thread_safe = 1;
13276 if (!htab->opd_abi)
13277 htab->params->plt_thread_safe = 0;
13278 else if (htab->params->plt_thread_safe == -1)
13280 static const char *const thread_starter[] =
13282 "pthread_create",
13283 /* libstdc++ */
13284 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13285 /* librt */
13286 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13287 "mq_notify", "create_timer",
13288 /* libanl */
13289 "getaddrinfo_a",
13290 /* libgomp */
13291 "GOMP_parallel",
13292 "GOMP_parallel_start",
13293 "GOMP_parallel_loop_static",
13294 "GOMP_parallel_loop_static_start",
13295 "GOMP_parallel_loop_dynamic",
13296 "GOMP_parallel_loop_dynamic_start",
13297 "GOMP_parallel_loop_guided",
13298 "GOMP_parallel_loop_guided_start",
13299 "GOMP_parallel_loop_runtime",
13300 "GOMP_parallel_loop_runtime_start",
13301 "GOMP_parallel_sections",
13302 "GOMP_parallel_sections_start",
13303 /* libgo */
13304 "__go_go",
13306 unsigned i;
13308 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13310 struct elf_link_hash_entry *h;
13311 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13312 false, false, true);
13313 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13314 if (htab->params->plt_thread_safe)
13315 break;
13318 stubs_always_before_branch = htab->params->group_size < 0;
13319 if (htab->params->group_size < 0)
13320 stub_group_size = -htab->params->group_size;
13321 else
13322 stub_group_size = htab->params->group_size;
13324 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13325 return false;
13327 htab->tga_group = NULL;
13328 if (!htab->params->no_tls_get_addr_regsave
13329 && htab->tga_desc_fd != NULL
13330 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13331 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13332 && htab->tls_get_addr_fd != NULL
13333 && is_static_defined (&htab->tls_get_addr_fd->elf))
13335 asection *sym_sec, *code_sec, *stub_sec;
13336 bfd_vma sym_value;
13337 struct _opd_sec_data *opd;
13339 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13340 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13341 code_sec = sym_sec;
13342 opd = get_opd_info (sym_sec);
13343 if (opd != NULL)
13344 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13345 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13346 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13347 htab->tga_group->link_sec);
13348 if (stub_sec == NULL)
13349 return false;
13350 htab->tga_group->stub_sec = stub_sec;
13352 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13353 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13354 htab->tga_desc_fd->elf.root.u.def.value = 0;
13355 htab->tga_desc_fd->elf.type = STT_FUNC;
13356 htab->tga_desc_fd->elf.def_regular = 1;
13357 htab->tga_desc_fd->elf.non_elf = 0;
13358 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13361 #define STUB_SHRINK_ITER 20
13362 /* Loop until no stubs added. After iteration 20 of this loop we may
13363 exit on a stub section shrinking. This is to break out of a
13364 pathological case where adding stubs on one iteration decreases
13365 section gaps (perhaps due to alignment), which then requires
13366 fewer or smaller stubs on the next iteration. */
13368 while (1)
13370 bfd *input_bfd;
13371 unsigned int bfd_indx;
13372 struct map_stub *group;
13374 htab->stub_iteration += 1;
13376 for (input_bfd = info->input_bfds, bfd_indx = 0;
13377 input_bfd != NULL;
13378 input_bfd = input_bfd->link.next, bfd_indx++)
13380 Elf_Internal_Shdr *symtab_hdr;
13381 asection *section;
13382 Elf_Internal_Sym *local_syms = NULL;
13384 if (!is_ppc64_elf (input_bfd))
13385 continue;
13387 /* We'll need the symbol table in a second. */
13388 symtab_hdr = &elf_symtab_hdr (input_bfd);
13389 if (symtab_hdr->sh_info == 0)
13390 continue;
13392 /* Walk over each section attached to the input bfd. */
13393 for (section = input_bfd->sections;
13394 section != NULL;
13395 section = section->next)
13397 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13399 /* If there aren't any relocs, then there's nothing more
13400 to do. */
13401 if ((section->flags & SEC_RELOC) == 0
13402 || (section->flags & SEC_ALLOC) == 0
13403 || (section->flags & SEC_LOAD) == 0
13404 || (section->flags & SEC_CODE) == 0
13405 || section->reloc_count == 0)
13406 continue;
13408 /* If this section is a link-once section that will be
13409 discarded, then don't create any stubs. */
13410 if (section->output_section == NULL
13411 || section->output_section->owner != info->output_bfd)
13412 continue;
13414 /* Get the relocs. */
13415 internal_relocs
13416 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13417 info->keep_memory);
13418 if (internal_relocs == NULL)
13419 goto error_ret_free_local;
13421 /* Now examine each relocation. */
13422 irela = internal_relocs;
13423 irelaend = irela + section->reloc_count;
13424 for (; irela < irelaend; irela++)
13426 enum elf_ppc64_reloc_type r_type;
13427 unsigned int r_indx;
13428 enum ppc_stub_type stub_type;
13429 struct ppc_stub_hash_entry *stub_entry;
13430 asection *sym_sec, *code_sec;
13431 bfd_vma sym_value, code_value;
13432 bfd_vma destination;
13433 unsigned long local_off;
13434 bool ok_dest;
13435 struct ppc_link_hash_entry *hash;
13436 struct ppc_link_hash_entry *fdh;
13437 struct elf_link_hash_entry *h;
13438 Elf_Internal_Sym *sym;
13439 char *stub_name;
13440 const asection *id_sec;
13441 struct _opd_sec_data *opd;
13442 struct plt_entry *plt_ent;
13444 r_type = ELF64_R_TYPE (irela->r_info);
13445 r_indx = ELF64_R_SYM (irela->r_info);
13447 if (r_type >= R_PPC64_max)
13449 bfd_set_error (bfd_error_bad_value);
13450 goto error_ret_free_internal;
13453 /* Only look for stubs on branch instructions. */
13454 if (r_type != R_PPC64_REL24
13455 && r_type != R_PPC64_REL24_NOTOC
13456 && r_type != R_PPC64_REL14
13457 && r_type != R_PPC64_REL14_BRTAKEN
13458 && r_type != R_PPC64_REL14_BRNTAKEN)
13459 continue;
13461 /* Now determine the call target, its name, value,
13462 section. */
13463 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13464 r_indx, input_bfd))
13465 goto error_ret_free_internal;
13466 hash = ppc_elf_hash_entry (h);
13468 ok_dest = false;
13469 fdh = NULL;
13470 sym_value = 0;
13471 if (hash == NULL)
13473 sym_value = sym->st_value;
13474 if (sym_sec != NULL
13475 && sym_sec->output_section != NULL)
13476 ok_dest = true;
13478 else if (hash->elf.root.type == bfd_link_hash_defined
13479 || hash->elf.root.type == bfd_link_hash_defweak)
13481 sym_value = hash->elf.root.u.def.value;
13482 if (sym_sec->output_section != NULL)
13483 ok_dest = true;
13485 else if (hash->elf.root.type == bfd_link_hash_undefweak
13486 || hash->elf.root.type == bfd_link_hash_undefined)
13488 /* Recognise an old ABI func code entry sym, and
13489 use the func descriptor sym instead if it is
13490 defined. */
13491 if (hash->elf.root.root.string[0] == '.'
13492 && hash->oh != NULL)
13494 fdh = ppc_follow_link (hash->oh);
13495 if (fdh->elf.root.type == bfd_link_hash_defined
13496 || fdh->elf.root.type == bfd_link_hash_defweak)
13498 sym_sec = fdh->elf.root.u.def.section;
13499 sym_value = fdh->elf.root.u.def.value;
13500 if (sym_sec->output_section != NULL)
13501 ok_dest = true;
13503 else
13504 fdh = NULL;
13507 else
13509 bfd_set_error (bfd_error_bad_value);
13510 goto error_ret_free_internal;
13513 destination = 0;
13514 local_off = 0;
13515 if (ok_dest)
13517 sym_value += irela->r_addend;
13518 destination = (sym_value
13519 + sym_sec->output_offset
13520 + sym_sec->output_section->vma);
13521 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13522 ? hash->elf.other
13523 : sym->st_other);
13526 code_sec = sym_sec;
13527 code_value = sym_value;
13528 opd = get_opd_info (sym_sec);
13529 if (opd != NULL)
13531 bfd_vma dest;
13533 if (hash == NULL && opd->adjust != NULL)
13535 long adjust = opd->adjust[OPD_NDX (sym_value)];
13536 if (adjust == -1)
13537 continue;
13538 code_value += adjust;
13539 sym_value += adjust;
13541 dest = opd_entry_value (sym_sec, sym_value,
13542 &code_sec, &code_value, false);
13543 if (dest != (bfd_vma) -1)
13545 destination = dest;
13546 if (fdh != NULL)
13548 /* Fixup old ABI sym to point at code
13549 entry. */
13550 hash->elf.root.type = bfd_link_hash_defweak;
13551 hash->elf.root.u.def.section = code_sec;
13552 hash->elf.root.u.def.value = code_value;
13557 /* Determine what (if any) linker stub is needed. */
13558 plt_ent = NULL;
13559 stub_type = ppc_type_of_stub (section, irela, &hash,
13560 &plt_ent, destination,
13561 local_off);
13563 if (r_type == R_PPC64_REL24_NOTOC)
13565 if (stub_type == ppc_stub_plt_call)
13566 stub_type = ppc_stub_plt_call_notoc;
13567 else if (stub_type == ppc_stub_long_branch
13568 || (code_sec != NULL
13569 && code_sec->output_section != NULL
13570 && (((hash ? hash->elf.other : sym->st_other)
13571 & STO_PPC64_LOCAL_MASK)
13572 > 1 << STO_PPC64_LOCAL_BIT)))
13573 stub_type = ppc_stub_long_branch_notoc;
13575 else if (stub_type != ppc_stub_plt_call)
13577 /* Check whether we need a TOC adjusting stub.
13578 Since the linker pastes together pieces from
13579 different object files when creating the
13580 _init and _fini functions, it may be that a
13581 call to what looks like a local sym is in
13582 fact a call needing a TOC adjustment. */
13583 if ((code_sec != NULL
13584 && code_sec->output_section != NULL
13585 && (code_sec->has_toc_reloc
13586 || code_sec->makes_toc_func_call)
13587 && (htab->sec_info[code_sec->id].toc_off
13588 != htab->sec_info[section->id].toc_off))
13589 || (((hash ? hash->elf.other : sym->st_other)
13590 & STO_PPC64_LOCAL_MASK)
13591 == 1 << STO_PPC64_LOCAL_BIT))
13592 stub_type = ppc_stub_long_branch_r2off;
13595 if (stub_type == ppc_stub_none)
13596 continue;
13598 /* __tls_get_addr calls might be eliminated. */
13599 if (stub_type != ppc_stub_plt_call
13600 && stub_type != ppc_stub_plt_call_notoc
13601 && hash != NULL
13602 && is_tls_get_addr (&hash->elf, htab)
13603 && section->has_tls_reloc
13604 && irela != internal_relocs)
13606 /* Get tls info. */
13607 unsigned char *tls_mask;
13609 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13610 irela - 1, input_bfd))
13611 goto error_ret_free_internal;
13612 if ((*tls_mask & TLS_TLS) != 0
13613 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
13614 continue;
13617 if (stub_type == ppc_stub_plt_call)
13619 if (!htab->opd_abi
13620 && htab->params->plt_localentry0 != 0
13621 && is_elfv2_localentry0 (&hash->elf))
13622 htab->has_plt_localentry0 = 1;
13623 else if (irela + 1 < irelaend
13624 && irela[1].r_offset == irela->r_offset + 4
13625 && (ELF64_R_TYPE (irela[1].r_info)
13626 == R_PPC64_TOCSAVE))
13628 if (!tocsave_find (htab, INSERT,
13629 &local_syms, irela + 1, input_bfd))
13630 goto error_ret_free_internal;
13632 else
13633 stub_type = ppc_stub_plt_call_r2save;
13636 /* Support for grouping stub sections. */
13637 id_sec = htab->sec_info[section->id].u.group->link_sec;
13639 /* Get the name of this stub. */
13640 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13641 if (!stub_name)
13642 goto error_ret_free_internal;
13644 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13645 stub_name, false, false);
13646 if (stub_entry != NULL)
13648 enum ppc_stub_type old_type;
13650 /* A stub has already been created, but it may
13651 not be the required type. We shouldn't be
13652 transitioning from plt_call to long_branch
13653 stubs or vice versa, but we might be
13654 upgrading from plt_call to plt_call_r2save or
13655 from long_branch to long_branch_r2off. */
13656 free (stub_name);
13657 if (htab->params->power10_stubs == -1)
13659 /* For --power10-stubs=auto, don't merge _notoc
13660 and other varieties of stubs. (The _both
13661 variety won't be created.) */
13662 bool notoc = r_type == R_PPC64_REL24_NOTOC;
13663 struct ppc_stub_hash_entry *alt_stub
13664 = select_alt_stub (stub_entry, notoc);
13666 if (alt_stub == NULL)
13668 alt_stub = (struct ppc_stub_hash_entry *)
13669 stub_hash_newfunc (NULL,
13670 &htab->stub_hash_table,
13671 stub_entry->root.string);
13672 if (alt_stub == NULL)
13674 /* xgettext:c-format */
13675 _bfd_error_handler
13676 (_("%pB: cannot create stub entry %s"),
13677 section->owner, stub_entry->root.string);
13678 goto error_ret_free_internal;
13680 *alt_stub = *stub_entry;
13681 stub_entry->root.next = &alt_stub->root;
13682 if (notoc)
13683 /* Sort notoc stubs first, for no good
13684 reason. */
13685 alt_stub = stub_entry;
13686 alt_stub->stub_type = stub_type;
13688 stub_entry = alt_stub;
13690 old_type = stub_entry->stub_type;
13691 switch (old_type)
13693 default:
13694 abort ();
13696 case ppc_stub_save_res:
13697 continue;
13699 case ppc_stub_plt_call:
13700 case ppc_stub_plt_call_r2save:
13701 case ppc_stub_plt_call_notoc:
13702 case ppc_stub_plt_call_both:
13703 if (stub_type == ppc_stub_plt_call)
13704 continue;
13705 else if (stub_type == ppc_stub_plt_call_r2save)
13707 if (old_type == ppc_stub_plt_call_notoc)
13708 stub_type = ppc_stub_plt_call_both;
13710 else if (stub_type == ppc_stub_plt_call_notoc)
13712 if (old_type == ppc_stub_plt_call_r2save)
13713 stub_type = ppc_stub_plt_call_both;
13715 else
13716 abort ();
13717 break;
13719 case ppc_stub_plt_branch:
13720 case ppc_stub_plt_branch_r2off:
13721 case ppc_stub_plt_branch_notoc:
13722 case ppc_stub_plt_branch_both:
13723 old_type += (ppc_stub_long_branch
13724 - ppc_stub_plt_branch);
13725 /* Fall through. */
13726 case ppc_stub_long_branch:
13727 case ppc_stub_long_branch_r2off:
13728 case ppc_stub_long_branch_notoc:
13729 case ppc_stub_long_branch_both:
13730 if (stub_type == ppc_stub_long_branch)
13731 continue;
13732 else if (stub_type == ppc_stub_long_branch_r2off)
13734 if (old_type == ppc_stub_long_branch_notoc)
13735 stub_type = ppc_stub_long_branch_both;
13737 else if (stub_type == ppc_stub_long_branch_notoc)
13739 if (old_type == ppc_stub_long_branch_r2off)
13740 stub_type = ppc_stub_long_branch_both;
13742 else
13743 abort ();
13744 break;
13746 if (old_type < stub_type)
13747 stub_entry->stub_type = stub_type;
13748 continue;
13751 stub_entry = ppc_add_stub (stub_name, section, info);
13752 if (stub_entry == NULL)
13754 free (stub_name);
13755 error_ret_free_internal:
13756 if (elf_section_data (section)->relocs == NULL)
13757 free (internal_relocs);
13758 error_ret_free_local:
13759 if (symtab_hdr->contents
13760 != (unsigned char *) local_syms)
13761 free (local_syms);
13762 return false;
13765 stub_entry->stub_type = stub_type;
13766 if (stub_type >= ppc_stub_plt_call
13767 && stub_type <= ppc_stub_plt_call_both)
13769 stub_entry->target_value = sym_value;
13770 stub_entry->target_section = sym_sec;
13772 else
13774 stub_entry->target_value = code_value;
13775 stub_entry->target_section = code_sec;
13777 stub_entry->h = hash;
13778 stub_entry->plt_ent = plt_ent;
13779 stub_entry->symtype
13780 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13781 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13783 if (hash != NULL
13784 && (hash->elf.root.type == bfd_link_hash_defined
13785 || hash->elf.root.type == bfd_link_hash_defweak))
13786 htab->stub_globals += 1;
13789 /* We're done with the internal relocs, free them. */
13790 if (elf_section_data (section)->relocs != internal_relocs)
13791 free (internal_relocs);
13794 if (local_syms != NULL
13795 && symtab_hdr->contents != (unsigned char *) local_syms)
13797 if (!info->keep_memory)
13798 free (local_syms);
13799 else
13800 symtab_hdr->contents = (unsigned char *) local_syms;
13804 /* We may have added some stubs. Find out the new size of the
13805 stub sections. */
13806 for (group = htab->group; group != NULL; group = group->next)
13808 group->lr_restore = 0;
13809 group->eh_size = 0;
13810 if (group->stub_sec != NULL)
13812 asection *stub_sec = group->stub_sec;
13814 if (htab->stub_iteration <= STUB_SHRINK_ITER
13815 || stub_sec->rawsize < stub_sec->size)
13816 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13817 stub_sec->rawsize = stub_sec->size;
13818 stub_sec->size = 0;
13819 stub_sec->reloc_count = 0;
13820 stub_sec->flags &= ~SEC_RELOC;
13823 if (htab->tga_group != NULL)
13825 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13826 htab->tga_group->eh_size
13827 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13828 htab->tga_group->lr_restore = 23 * 4;
13829 htab->tga_group->stub_sec->size = 24 * 4;
13832 if (htab->stub_iteration <= STUB_SHRINK_ITER
13833 || htab->brlt->rawsize < htab->brlt->size)
13834 htab->brlt->rawsize = htab->brlt->size;
13835 htab->brlt->size = 0;
13836 htab->brlt->reloc_count = 0;
13837 htab->brlt->flags &= ~SEC_RELOC;
13838 if (htab->relbrlt != NULL)
13839 htab->relbrlt->size = 0;
13841 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13843 for (group = htab->group; group != NULL; group = group->next)
13844 if (group->needs_save_res)
13845 group->stub_sec->size += htab->sfpr->size;
13847 if (info->emitrelocations
13848 && htab->glink != NULL && htab->glink->size != 0)
13850 htab->glink->reloc_count = 1;
13851 htab->glink->flags |= SEC_RELOC;
13854 if (htab->glink_eh_frame != NULL
13855 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13856 && htab->glink_eh_frame->output_section->size > 8)
13858 size_t size = 0, align = 4;
13860 for (group = htab->group; group != NULL; group = group->next)
13861 if (group->eh_size != 0)
13862 size += (group->eh_size + 17 + align - 1) & -align;
13863 if (htab->glink != NULL && htab->glink->size != 0)
13864 size += (24 + align - 1) & -align;
13865 if (size != 0)
13866 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13867 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13868 size = (size + align - 1) & -align;
13869 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13870 htab->glink_eh_frame->size = size;
13873 if (htab->params->plt_stub_align != 0)
13874 for (group = htab->group; group != NULL; group = group->next)
13875 if (group->stub_sec != NULL)
13877 int align = abs (htab->params->plt_stub_align);
13878 group->stub_sec->size
13879 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13882 for (group = htab->group; group != NULL; group = group->next)
13883 if (group->stub_sec != NULL
13884 && group->stub_sec->rawsize != group->stub_sec->size
13885 && (htab->stub_iteration <= STUB_SHRINK_ITER
13886 || group->stub_sec->rawsize < group->stub_sec->size))
13887 break;
13889 if (group == NULL
13890 && (htab->brlt->rawsize == htab->brlt->size
13891 || (htab->stub_iteration > STUB_SHRINK_ITER
13892 && htab->brlt->rawsize > htab->brlt->size))
13893 && (htab->glink_eh_frame == NULL
13894 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13895 && (htab->tga_group == NULL
13896 || htab->stub_iteration > 1))
13897 break;
13899 /* Ask the linker to do its stuff. */
13900 (*htab->params->layout_sections_again) ();
13903 if (htab->glink_eh_frame != NULL
13904 && htab->glink_eh_frame->size != 0)
13906 bfd_vma val;
13907 bfd_byte *p, *last_fde;
13908 size_t last_fde_len, size, align, pad;
13909 struct map_stub *group;
13911 /* It is necessary to at least have a rough outline of the
13912 linker generated CIEs and FDEs written before
13913 bfd_elf_discard_info is run, in order for these FDEs to be
13914 indexed in .eh_frame_hdr. */
13915 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13916 if (p == NULL)
13917 return false;
13918 htab->glink_eh_frame->contents = p;
13919 last_fde = p;
13920 align = 4;
13922 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13923 /* CIE length (rewrite in case little-endian). */
13924 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13925 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13926 p += last_fde_len + 4;
13928 for (group = htab->group; group != NULL; group = group->next)
13929 if (group->eh_size != 0)
13931 group->eh_base = p - htab->glink_eh_frame->contents;
13932 last_fde = p;
13933 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13934 /* FDE length. */
13935 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13936 p += 4;
13937 /* CIE pointer. */
13938 val = p - htab->glink_eh_frame->contents;
13939 bfd_put_32 (htab->elf.dynobj, val, p);
13940 p += 4;
13941 /* Offset to stub section, written later. */
13942 p += 4;
13943 /* stub section size. */
13944 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13945 p += 4;
13946 /* Augmentation. */
13947 p += 1;
13948 /* Make sure we don't have all nops. This is enough for
13949 elf-eh-frame.c to detect the last non-nop opcode. */
13950 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13951 p = last_fde + last_fde_len + 4;
13953 if (htab->glink != NULL && htab->glink->size != 0)
13955 last_fde = p;
13956 last_fde_len = ((24 + align - 1) & -align) - 4;
13957 /* FDE length. */
13958 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13959 p += 4;
13960 /* CIE pointer. */
13961 val = p - htab->glink_eh_frame->contents;
13962 bfd_put_32 (htab->elf.dynobj, val, p);
13963 p += 4;
13964 /* Offset to .glink, written later. */
13965 p += 4;
13966 /* .glink size. */
13967 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13968 p += 4;
13969 /* Augmentation. */
13970 p += 1;
13972 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
13973 *p++ = DW_CFA_register;
13974 *p++ = 65;
13975 *p++ = htab->opd_abi ? 12 : 0;
13976 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
13977 *p++ = DW_CFA_restore_extended;
13978 *p++ = 65;
13979 p += ((24 + align - 1) & -align) - 24;
13981 /* Subsume any padding into the last FDE if user .eh_frame
13982 sections are aligned more than glink_eh_frame. Otherwise any
13983 zero padding will be seen as a terminator. */
13984 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13985 size = p - htab->glink_eh_frame->contents;
13986 pad = ((size + align - 1) & -align) - size;
13987 htab->glink_eh_frame->size = size + pad;
13988 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13991 maybe_strip_output (info, htab->brlt);
13992 if (htab->relbrlt != NULL)
13993 maybe_strip_output (info, htab->relbrlt);
13994 if (htab->glink_eh_frame != NULL)
13995 maybe_strip_output (info, htab->glink_eh_frame);
13997 return true;
14000 /* Called after we have determined section placement. If sections
14001 move, we'll be called again. Provide a value for TOCstart. */
14003 bfd_vma
14004 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14006 asection *s;
14007 bfd_vma TOCstart, adjust;
14009 if (info != NULL)
14011 struct elf_link_hash_entry *h;
14012 struct elf_link_hash_table *htab = elf_hash_table (info);
14014 if (is_elf_hash_table (&htab->root)
14015 && htab->hgot != NULL)
14016 h = htab->hgot;
14017 else
14019 h = (struct elf_link_hash_entry *)
14020 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14021 if (is_elf_hash_table (&htab->root))
14022 htab->hgot = h;
14024 if (h != NULL
14025 && h->root.type == bfd_link_hash_defined
14026 && !h->root.linker_def
14027 && (!is_elf_hash_table (&htab->root)
14028 || h->def_regular))
14030 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14031 _bfd_set_gp_value (obfd, TOCstart);
14032 return TOCstart;
14036 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14037 order. The TOC starts where the first of these sections starts. */
14038 s = bfd_get_section_by_name (obfd, ".got");
14039 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14040 s = bfd_get_section_by_name (obfd, ".toc");
14041 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14042 s = bfd_get_section_by_name (obfd, ".tocbss");
14043 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14044 s = bfd_get_section_by_name (obfd, ".plt");
14045 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14047 /* This may happen for
14048 o references to TOC base (SYM@toc / TOC[tc0]) without a
14049 .toc directive
14050 o bad linker script
14051 o --gc-sections and empty TOC sections
14053 FIXME: Warn user? */
14055 /* Look for a likely section. We probably won't even be
14056 using TOCstart. */
14057 for (s = obfd->sections; s != NULL; s = s->next)
14058 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14059 | SEC_EXCLUDE))
14060 == (SEC_ALLOC | SEC_SMALL_DATA))
14061 break;
14062 if (s == NULL)
14063 for (s = obfd->sections; s != NULL; s = s->next)
14064 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14065 == (SEC_ALLOC | SEC_SMALL_DATA))
14066 break;
14067 if (s == NULL)
14068 for (s = obfd->sections; s != NULL; s = s->next)
14069 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14070 == SEC_ALLOC)
14071 break;
14072 if (s == NULL)
14073 for (s = obfd->sections; s != NULL; s = s->next)
14074 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14075 break;
14078 TOCstart = 0;
14079 if (s != NULL)
14080 TOCstart = s->output_section->vma + s->output_offset;
14082 /* Force alignment. */
14083 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14084 TOCstart -= adjust;
14085 _bfd_set_gp_value (obfd, TOCstart);
14087 if (info != NULL && s != NULL)
14089 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14091 if (htab != NULL)
14093 if (htab->elf.hgot != NULL)
14095 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14096 htab->elf.hgot->root.u.def.section = s;
14099 else
14101 struct bfd_link_hash_entry *bh = NULL;
14102 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14103 s, TOC_BASE_OFF - adjust,
14104 NULL, false, false, &bh);
14107 return TOCstart;
14110 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14111 write out any global entry stubs, and PLT relocations. */
14113 static bool
14114 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14116 struct bfd_link_info *info;
14117 struct ppc_link_hash_table *htab;
14118 struct plt_entry *ent;
14119 asection *s;
14121 if (h->root.type == bfd_link_hash_indirect)
14122 return true;
14124 info = inf;
14125 htab = ppc_hash_table (info);
14126 if (htab == NULL)
14127 return false;
14129 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14130 if (ent->plt.offset != (bfd_vma) -1)
14132 /* This symbol has an entry in the procedure linkage
14133 table. Set it up. */
14134 Elf_Internal_Rela rela;
14135 asection *plt, *relplt;
14136 bfd_byte *loc;
14138 if (use_local_plt (info, h))
14140 if (!(h->def_regular
14141 && (h->root.type == bfd_link_hash_defined
14142 || h->root.type == bfd_link_hash_defweak)))
14143 continue;
14144 if (h->type == STT_GNU_IFUNC)
14146 plt = htab->elf.iplt;
14147 relplt = htab->elf.irelplt;
14148 htab->elf.ifunc_resolvers = true;
14149 if (htab->opd_abi)
14150 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14151 else
14152 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14154 else
14156 plt = htab->pltlocal;
14157 if (bfd_link_pic (info))
14159 relplt = htab->relpltlocal;
14160 if (htab->opd_abi)
14161 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14162 else
14163 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14165 else
14166 relplt = NULL;
14168 rela.r_addend = defined_sym_val (h) + ent->addend;
14170 if (relplt == NULL)
14172 loc = plt->contents + ent->plt.offset;
14173 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14174 if (htab->opd_abi)
14176 bfd_vma toc = elf_gp (info->output_bfd);
14177 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14178 bfd_put_64 (info->output_bfd, toc, loc + 8);
14181 else
14183 rela.r_offset = (plt->output_section->vma
14184 + plt->output_offset
14185 + ent->plt.offset);
14186 loc = relplt->contents + (relplt->reloc_count++
14187 * sizeof (Elf64_External_Rela));
14188 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14191 else
14193 rela.r_offset = (htab->elf.splt->output_section->vma
14194 + htab->elf.splt->output_offset
14195 + ent->plt.offset);
14196 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14197 rela.r_addend = ent->addend;
14198 loc = (htab->elf.srelplt->contents
14199 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14200 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14201 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14202 htab->elf.ifunc_resolvers = true;
14203 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14207 if (!h->pointer_equality_needed)
14208 return true;
14210 if (h->def_regular)
14211 return true;
14213 s = htab->global_entry;
14214 if (s == NULL || s->size == 0)
14215 return true;
14217 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14218 if (ent->plt.offset != (bfd_vma) -1
14219 && ent->addend == 0)
14221 bfd_byte *p;
14222 asection *plt;
14223 bfd_vma off;
14225 p = s->contents + h->root.u.def.value;
14226 plt = htab->elf.splt;
14227 if (use_local_plt (info, h))
14229 if (h->type == STT_GNU_IFUNC)
14230 plt = htab->elf.iplt;
14231 else
14232 plt = htab->pltlocal;
14234 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14235 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14237 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14239 info->callbacks->einfo
14240 (_("%P: linkage table error against `%pT'\n"),
14241 h->root.root.string);
14242 bfd_set_error (bfd_error_bad_value);
14243 htab->stub_error = true;
14246 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14247 if (htab->params->emit_stub_syms)
14249 size_t len = strlen (h->root.root.string);
14250 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14252 if (name == NULL)
14253 return false;
14255 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14256 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14257 if (h == NULL)
14258 return false;
14259 if (h->root.type == bfd_link_hash_new)
14261 h->root.type = bfd_link_hash_defined;
14262 h->root.u.def.section = s;
14263 h->root.u.def.value = p - s->contents;
14264 h->ref_regular = 1;
14265 h->def_regular = 1;
14266 h->ref_regular_nonweak = 1;
14267 h->forced_local = 1;
14268 h->non_elf = 0;
14269 h->root.linker_def = 1;
14273 if (PPC_HA (off) != 0)
14275 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14276 p += 4;
14278 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14279 p += 4;
14280 bfd_put_32 (s->owner, MTCTR_R12, p);
14281 p += 4;
14282 bfd_put_32 (s->owner, BCTR, p);
14283 break;
14285 return true;
14288 /* Write PLT relocs for locals. */
14290 static bool
14291 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14293 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14294 bfd *ibfd;
14296 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14298 struct got_entry **lgot_ents, **end_lgot_ents;
14299 struct plt_entry **local_plt, **lplt, **end_local_plt;
14300 Elf_Internal_Shdr *symtab_hdr;
14301 bfd_size_type locsymcount;
14302 Elf_Internal_Sym *local_syms = NULL;
14303 struct plt_entry *ent;
14305 if (!is_ppc64_elf (ibfd))
14306 continue;
14308 lgot_ents = elf_local_got_ents (ibfd);
14309 if (!lgot_ents)
14310 continue;
14312 symtab_hdr = &elf_symtab_hdr (ibfd);
14313 locsymcount = symtab_hdr->sh_info;
14314 end_lgot_ents = lgot_ents + locsymcount;
14315 local_plt = (struct plt_entry **) end_lgot_ents;
14316 end_local_plt = local_plt + locsymcount;
14317 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14318 for (ent = *lplt; ent != NULL; ent = ent->next)
14319 if (ent->plt.offset != (bfd_vma) -1)
14321 Elf_Internal_Sym *sym;
14322 asection *sym_sec;
14323 asection *plt, *relplt;
14324 bfd_byte *loc;
14325 bfd_vma val;
14327 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14328 lplt - local_plt, ibfd))
14330 if (symtab_hdr->contents != (unsigned char *) local_syms)
14331 free (local_syms);
14332 return false;
14335 val = sym->st_value + ent->addend;
14336 if (sym_sec != NULL && sym_sec->output_section != NULL)
14337 val += sym_sec->output_offset + sym_sec->output_section->vma;
14339 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14341 htab->elf.ifunc_resolvers = true;
14342 plt = htab->elf.iplt;
14343 relplt = htab->elf.irelplt;
14345 else
14347 plt = htab->pltlocal;
14348 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14351 if (relplt == NULL)
14353 loc = plt->contents + ent->plt.offset;
14354 bfd_put_64 (info->output_bfd, val, loc);
14355 if (htab->opd_abi)
14357 bfd_vma toc = elf_gp (ibfd);
14358 bfd_put_64 (info->output_bfd, toc, loc + 8);
14361 else
14363 Elf_Internal_Rela rela;
14364 rela.r_offset = (ent->plt.offset
14365 + plt->output_offset
14366 + plt->output_section->vma);
14367 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14369 if (htab->opd_abi)
14370 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14371 else
14372 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14374 else
14376 if (htab->opd_abi)
14377 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14378 else
14379 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14381 rela.r_addend = val;
14382 loc = relplt->contents + (relplt->reloc_count++
14383 * sizeof (Elf64_External_Rela));
14384 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14388 if (local_syms != NULL
14389 && symtab_hdr->contents != (unsigned char *) local_syms)
14391 if (!info->keep_memory)
14392 free (local_syms);
14393 else
14394 symtab_hdr->contents = (unsigned char *) local_syms;
14397 return true;
14400 /* Emit the static wrapper function preserving registers around a
14401 __tls_get_addr_opt call. */
14403 static bool
14404 emit_tga_desc (struct ppc_link_hash_table *htab)
14406 asection *stub_sec = htab->tga_group->stub_sec;
14407 unsigned int cfa_updt = 11 * 4;
14408 bfd_byte *p;
14409 bfd_vma to, from, delta;
14411 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14412 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14413 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14414 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14415 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14416 delta = to - from;
14417 if (delta + (1 << 25) >= 1 << 26)
14419 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14420 htab->stub_error = true;
14421 return false;
14424 p = stub_sec->contents;
14425 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14426 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14427 p += 4;
14428 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14429 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14432 /* Emit eh_frame describing the static wrapper function. */
14434 static bfd_byte *
14435 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14437 unsigned int cfa_updt = 11 * 4;
14438 unsigned int i;
14440 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14441 *p++ = DW_CFA_def_cfa_offset;
14442 if (htab->opd_abi)
14444 *p++ = 128;
14445 *p++ = 1;
14447 else
14448 *p++ = 96;
14449 *p++ = DW_CFA_offset_extended_sf;
14450 *p++ = 65;
14451 *p++ = (-16 / 8) & 0x7f;
14452 for (i = 4; i < 12; i++)
14454 *p++ = DW_CFA_offset + i;
14455 *p++ = (htab->opd_abi ? 13 : 12) - i;
14457 *p++ = DW_CFA_advance_loc + 10;
14458 *p++ = DW_CFA_def_cfa_offset;
14459 *p++ = 0;
14460 for (i = 4; i < 12; i++)
14461 *p++ = DW_CFA_restore + i;
14462 *p++ = DW_CFA_advance_loc + 2;
14463 *p++ = DW_CFA_restore_extended;
14464 *p++ = 65;
14465 return p;
14468 /* Build all the stubs associated with the current output file.
14469 The stubs are kept in a hash table attached to the main linker
14470 hash table. This function is called via gldelf64ppc_finish. */
14472 bool
14473 ppc64_elf_build_stubs (struct bfd_link_info *info,
14474 char **stats)
14476 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14477 struct map_stub *group;
14478 asection *stub_sec;
14479 bfd_byte *p;
14480 int stub_sec_count = 0;
14482 if (htab == NULL)
14483 return false;
14485 /* Allocate memory to hold the linker stubs. */
14486 for (group = htab->group; group != NULL; group = group->next)
14488 group->eh_size = 0;
14489 group->lr_restore = 0;
14490 if ((stub_sec = group->stub_sec) != NULL
14491 && stub_sec->size != 0)
14493 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14494 stub_sec->size);
14495 if (stub_sec->contents == NULL)
14496 return false;
14497 stub_sec->size = 0;
14501 if (htab->glink != NULL && htab->glink->size != 0)
14503 unsigned int indx;
14504 bfd_vma plt0;
14506 /* Build the .glink plt call stub. */
14507 if (htab->params->emit_stub_syms)
14509 struct elf_link_hash_entry *h;
14510 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14511 true, false, false);
14512 if (h == NULL)
14513 return false;
14514 if (h->root.type == bfd_link_hash_new)
14516 h->root.type = bfd_link_hash_defined;
14517 h->root.u.def.section = htab->glink;
14518 h->root.u.def.value = 8;
14519 h->ref_regular = 1;
14520 h->def_regular = 1;
14521 h->ref_regular_nonweak = 1;
14522 h->forced_local = 1;
14523 h->non_elf = 0;
14524 h->root.linker_def = 1;
14527 plt0 = (htab->elf.splt->output_section->vma
14528 + htab->elf.splt->output_offset
14529 - 16);
14530 if (info->emitrelocations)
14532 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14533 if (r == NULL)
14534 return false;
14535 r->r_offset = (htab->glink->output_offset
14536 + htab->glink->output_section->vma);
14537 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14538 r->r_addend = plt0;
14540 p = htab->glink->contents;
14541 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14542 bfd_put_64 (htab->glink->owner, plt0, p);
14543 p += 8;
14544 if (htab->opd_abi)
14546 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14547 p += 4;
14548 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14549 p += 4;
14550 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14551 p += 4;
14552 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14553 p += 4;
14554 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14555 p += 4;
14556 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14557 p += 4;
14558 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14559 p += 4;
14560 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14561 p += 4;
14562 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14563 p += 4;
14564 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14565 p += 4;
14567 else
14569 unsigned int insn;
14571 /* 0:
14572 . .quad plt0-1f # plt0 entry relative to 1:
14574 # We get here with r12 initially @ a glink branch
14575 # Load the address of _dl_runtime_resolve from plt0 and
14576 # jump to it, with r0 set to the index of the PLT entry
14577 # to be resolved and r11 the link map.
14578 __glink_PLTresolve:
14579 . std %r2,24(%r1) # optional
14580 . mflr %r0
14581 . bcl 20,31,1f
14583 . mflr %r11
14584 . mtlr %r0
14585 . ld %r0,(0b-1b)(%r11)
14586 . sub %r12,%r12,%r11
14587 . add %r11,%r0,%r11
14588 . addi %r0,%r12,1b-2f
14589 . ld %r12,0(%r11)
14590 . srdi %r0,%r0,2
14591 . mtctr %r12
14592 . ld %r11,8(%r11)
14593 . bctr
14595 . b __glink_PLTresolve
14596 . ...
14597 . b __glink_PLTresolve */
14599 if (htab->has_plt_localentry0)
14601 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14602 p += 4;
14604 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14605 p += 4;
14606 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14607 p += 4;
14608 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14609 p += 4;
14610 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14611 p += 4;
14612 if (htab->has_plt_localentry0)
14613 insn = LD_R0_0R11 | (-20 & 0xfffc);
14614 else
14615 insn = LD_R0_0R11 | (-16 & 0xfffc);
14616 bfd_put_32 (htab->glink->owner, insn, p);
14617 p += 4;
14618 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14619 p += 4;
14620 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
14621 p += 4;
14622 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
14623 p += 4;
14624 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14625 p += 4;
14626 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14627 p += 4;
14628 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14629 p += 4;
14630 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14631 p += 4;
14633 bfd_put_32 (htab->glink->owner, BCTR, p);
14634 p += 4;
14635 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14637 /* Build the .glink lazy link call stubs. */
14638 indx = 0;
14639 while (p < htab->glink->contents + htab->glink->size)
14641 if (htab->opd_abi)
14643 if (indx < 0x8000)
14645 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14646 p += 4;
14648 else
14650 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14651 p += 4;
14652 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14654 p += 4;
14657 bfd_put_32 (htab->glink->owner,
14658 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14659 indx++;
14660 p += 4;
14664 if (htab->tga_group != NULL)
14666 htab->tga_group->lr_restore = 23 * 4;
14667 htab->tga_group->stub_sec->size = 24 * 4;
14668 if (!emit_tga_desc (htab))
14669 return false;
14670 if (htab->glink_eh_frame != NULL
14671 && htab->glink_eh_frame->size != 0)
14673 size_t align = 4;
14675 p = htab->glink_eh_frame->contents;
14676 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14677 p += 17;
14678 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14682 /* Build .glink global entry stubs, and PLT relocs for globals. */
14683 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14685 if (!write_plt_relocs_for_local_syms (info))
14686 return false;
14688 if (htab->brlt != NULL && htab->brlt->size != 0)
14690 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
14691 htab->brlt->size);
14692 if (htab->brlt->contents == NULL)
14693 return false;
14695 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
14697 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
14698 htab->relbrlt->size);
14699 if (htab->relbrlt->contents == NULL)
14700 return false;
14703 /* Build the stubs as directed by the stub hash table. */
14704 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14706 for (group = htab->group; group != NULL; group = group->next)
14707 if (group->needs_save_res)
14708 group->stub_sec->size += htab->sfpr->size;
14710 if (htab->relbrlt != NULL)
14711 htab->relbrlt->reloc_count = 0;
14713 if (htab->params->plt_stub_align != 0)
14714 for (group = htab->group; group != NULL; group = group->next)
14715 if ((stub_sec = group->stub_sec) != NULL)
14717 int align = abs (htab->params->plt_stub_align);
14718 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14721 for (group = htab->group; group != NULL; group = group->next)
14722 if (group->needs_save_res)
14724 stub_sec = group->stub_sec;
14725 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14726 htab->sfpr->contents, htab->sfpr->size);
14727 if (htab->params->emit_stub_syms)
14729 unsigned int i;
14731 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14732 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14733 return false;
14737 if (htab->glink_eh_frame != NULL
14738 && htab->glink_eh_frame->size != 0)
14740 bfd_vma val;
14741 size_t align = 4;
14743 p = htab->glink_eh_frame->contents;
14744 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14746 for (group = htab->group; group != NULL; group = group->next)
14747 if (group->eh_size != 0)
14749 /* Offset to stub section. */
14750 val = (group->stub_sec->output_section->vma
14751 + group->stub_sec->output_offset);
14752 val -= (htab->glink_eh_frame->output_section->vma
14753 + htab->glink_eh_frame->output_offset
14754 + (p + 8 - htab->glink_eh_frame->contents));
14755 if (val + 0x80000000 > 0xffffffff)
14757 _bfd_error_handler
14758 (_("%s offset too large for .eh_frame sdata4 encoding"),
14759 group->stub_sec->name);
14760 return false;
14762 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14763 p += (group->eh_size + 17 + 3) & -4;
14765 if (htab->glink != NULL && htab->glink->size != 0)
14767 /* Offset to .glink. */
14768 val = (htab->glink->output_section->vma
14769 + htab->glink->output_offset
14770 + 8);
14771 val -= (htab->glink_eh_frame->output_section->vma
14772 + htab->glink_eh_frame->output_offset
14773 + (p + 8 - htab->glink_eh_frame->contents));
14774 if (val + 0x80000000 > 0xffffffff)
14776 _bfd_error_handler
14777 (_("%s offset too large for .eh_frame sdata4 encoding"),
14778 htab->glink->name);
14779 return false;
14781 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14782 p += (24 + align - 1) & -align;
14786 for (group = htab->group; group != NULL; group = group->next)
14787 if ((stub_sec = group->stub_sec) != NULL)
14789 stub_sec_count += 1;
14790 if (stub_sec->rawsize != stub_sec->size
14791 && (htab->stub_iteration <= STUB_SHRINK_ITER
14792 || stub_sec->rawsize < stub_sec->size))
14793 break;
14796 if (group != NULL)
14798 htab->stub_error = true;
14799 _bfd_error_handler (_("stubs don't match calculated size"));
14802 if (htab->stub_error)
14803 return false;
14805 if (stats != NULL)
14807 char *groupmsg;
14808 if (asprintf (&groupmsg,
14809 ngettext ("linker stubs in %u group\n",
14810 "linker stubs in %u groups\n",
14811 stub_sec_count),
14812 stub_sec_count) < 0)
14813 *stats = NULL;
14814 else
14816 if (asprintf (stats, _("%s"
14817 " branch %lu\n"
14818 " branch toc adj %lu\n"
14819 " branch notoc %lu\n"
14820 " branch both %lu\n"
14821 " long branch %lu\n"
14822 " long toc adj %lu\n"
14823 " long notoc %lu\n"
14824 " long both %lu\n"
14825 " plt call %lu\n"
14826 " plt call save %lu\n"
14827 " plt call notoc %lu\n"
14828 " plt call both %lu\n"
14829 " global entry %lu"),
14830 groupmsg,
14831 htab->stub_count[ppc_stub_long_branch - 1],
14832 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14833 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14834 htab->stub_count[ppc_stub_long_branch_both - 1],
14835 htab->stub_count[ppc_stub_plt_branch - 1],
14836 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14837 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14838 htab->stub_count[ppc_stub_plt_branch_both - 1],
14839 htab->stub_count[ppc_stub_plt_call - 1],
14840 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14841 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14842 htab->stub_count[ppc_stub_plt_call_both - 1],
14843 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14844 *stats = NULL;
14845 free (groupmsg);
14848 return true;
14851 /* What to do when ld finds relocations against symbols defined in
14852 discarded sections. */
14854 static unsigned int
14855 ppc64_elf_action_discarded (asection *sec)
14857 if (strcmp (".opd", sec->name) == 0)
14858 return 0;
14860 if (strcmp (".toc", sec->name) == 0)
14861 return 0;
14863 if (strcmp (".toc1", sec->name) == 0)
14864 return 0;
14866 return _bfd_elf_default_action_discarded (sec);
14869 /* These are the dynamic relocations supported by glibc. */
14871 static bool
14872 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14874 switch (r_type)
14876 case R_PPC64_RELATIVE:
14877 case R_PPC64_NONE:
14878 case R_PPC64_ADDR64:
14879 case R_PPC64_GLOB_DAT:
14880 case R_PPC64_IRELATIVE:
14881 case R_PPC64_JMP_IREL:
14882 case R_PPC64_JMP_SLOT:
14883 case R_PPC64_DTPMOD64:
14884 case R_PPC64_DTPREL64:
14885 case R_PPC64_TPREL64:
14886 case R_PPC64_TPREL16_LO_DS:
14887 case R_PPC64_TPREL16_DS:
14888 case R_PPC64_TPREL16:
14889 case R_PPC64_TPREL16_LO:
14890 case R_PPC64_TPREL16_HI:
14891 case R_PPC64_TPREL16_HIGH:
14892 case R_PPC64_TPREL16_HA:
14893 case R_PPC64_TPREL16_HIGHA:
14894 case R_PPC64_TPREL16_HIGHER:
14895 case R_PPC64_TPREL16_HIGHEST:
14896 case R_PPC64_TPREL16_HIGHERA:
14897 case R_PPC64_TPREL16_HIGHESTA:
14898 case R_PPC64_ADDR16_LO_DS:
14899 case R_PPC64_ADDR16_LO:
14900 case R_PPC64_ADDR16_HI:
14901 case R_PPC64_ADDR16_HIGH:
14902 case R_PPC64_ADDR16_HA:
14903 case R_PPC64_ADDR16_HIGHA:
14904 case R_PPC64_REL30:
14905 case R_PPC64_COPY:
14906 case R_PPC64_UADDR64:
14907 case R_PPC64_UADDR32:
14908 case R_PPC64_ADDR32:
14909 case R_PPC64_ADDR24:
14910 case R_PPC64_ADDR16:
14911 case R_PPC64_UADDR16:
14912 case R_PPC64_ADDR16_DS:
14913 case R_PPC64_ADDR16_HIGHER:
14914 case R_PPC64_ADDR16_HIGHEST:
14915 case R_PPC64_ADDR16_HIGHERA:
14916 case R_PPC64_ADDR16_HIGHESTA:
14917 case R_PPC64_ADDR14:
14918 case R_PPC64_ADDR14_BRTAKEN:
14919 case R_PPC64_ADDR14_BRNTAKEN:
14920 case R_PPC64_REL32:
14921 case R_PPC64_REL64:
14922 return true;
14924 default:
14925 return false;
14929 /* The RELOCATE_SECTION function is called by the ELF backend linker
14930 to handle the relocations for a section.
14932 The relocs are always passed as Rela structures; if the section
14933 actually uses Rel structures, the r_addend field will always be
14934 zero.
14936 This function is responsible for adjust the section contents as
14937 necessary, and (if using Rela relocs and generating a
14938 relocatable output file) adjusting the reloc addend as
14939 necessary.
14941 This function does not have to worry about setting the reloc
14942 address or the reloc symbol index.
14944 LOCAL_SYMS is a pointer to the swapped in local symbols.
14946 LOCAL_SECTIONS is an array giving the section in the input file
14947 corresponding to the st_shndx field of each local symbol.
14949 The global hash table entry for the global symbols can be found
14950 via elf_sym_hashes (input_bfd).
14952 When generating relocatable output, this function must handle
14953 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14954 going to be the section symbol corresponding to the output
14955 section, which means that the addend must be adjusted
14956 accordingly. */
14958 static int
14959 ppc64_elf_relocate_section (bfd *output_bfd,
14960 struct bfd_link_info *info,
14961 bfd *input_bfd,
14962 asection *input_section,
14963 bfd_byte *contents,
14964 Elf_Internal_Rela *relocs,
14965 Elf_Internal_Sym *local_syms,
14966 asection **local_sections)
14968 struct ppc_link_hash_table *htab;
14969 Elf_Internal_Shdr *symtab_hdr;
14970 struct elf_link_hash_entry **sym_hashes;
14971 Elf_Internal_Rela *rel;
14972 Elf_Internal_Rela *wrel;
14973 Elf_Internal_Rela *relend;
14974 Elf_Internal_Rela outrel;
14975 bfd_byte *loc;
14976 struct got_entry **local_got_ents;
14977 bfd_vma TOCstart;
14978 bool ret = true;
14979 bool is_opd;
14980 /* Assume 'at' branch hints. */
14981 bool is_isa_v2 = true;
14982 bool warned_dynamic = false;
14983 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14985 /* Initialize howto table if needed. */
14986 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14987 ppc_howto_init ();
14989 htab = ppc_hash_table (info);
14990 if (htab == NULL)
14991 return false;
14993 /* Don't relocate stub sections. */
14994 if (input_section->owner == htab->params->stub_bfd)
14995 return true;
14997 if (!is_ppc64_elf (input_bfd))
14999 bfd_set_error (bfd_error_wrong_format);
15000 return false;
15003 local_got_ents = elf_local_got_ents (input_bfd);
15004 TOCstart = elf_gp (output_bfd);
15005 symtab_hdr = &elf_symtab_hdr (input_bfd);
15006 sym_hashes = elf_sym_hashes (input_bfd);
15007 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15009 rel = wrel = relocs;
15010 relend = relocs + input_section->reloc_count;
15011 for (; rel < relend; wrel++, rel++)
15013 enum elf_ppc64_reloc_type r_type;
15014 bfd_vma addend;
15015 bfd_reloc_status_type r;
15016 Elf_Internal_Sym *sym;
15017 asection *sec;
15018 struct elf_link_hash_entry *h_elf;
15019 struct ppc_link_hash_entry *h;
15020 struct ppc_link_hash_entry *fdh;
15021 const char *sym_name;
15022 unsigned long r_symndx, toc_symndx;
15023 bfd_vma toc_addend;
15024 unsigned char tls_mask, tls_gd, tls_type;
15025 unsigned char sym_type;
15026 bfd_vma relocation;
15027 bool unresolved_reloc, save_unresolved_reloc;
15028 bool warned;
15029 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15030 unsigned int insn;
15031 unsigned int mask;
15032 struct ppc_stub_hash_entry *stub_entry;
15033 bfd_vma max_br_offset;
15034 bfd_vma from;
15035 Elf_Internal_Rela orig_rel;
15036 reloc_howto_type *howto;
15037 struct reloc_howto_struct alt_howto;
15038 uint64_t pinsn;
15039 bfd_vma offset;
15041 again:
15042 orig_rel = *rel;
15044 r_type = ELF64_R_TYPE (rel->r_info);
15045 r_symndx = ELF64_R_SYM (rel->r_info);
15047 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15048 symbol of the previous ADDR64 reloc. The symbol gives us the
15049 proper TOC base to use. */
15050 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15051 && wrel != relocs
15052 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15053 && is_opd)
15054 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15056 sym = NULL;
15057 sec = NULL;
15058 h_elf = NULL;
15059 sym_name = NULL;
15060 unresolved_reloc = false;
15061 warned = false;
15063 if (r_symndx < symtab_hdr->sh_info)
15065 /* It's a local symbol. */
15066 struct _opd_sec_data *opd;
15068 sym = local_syms + r_symndx;
15069 sec = local_sections[r_symndx];
15070 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15071 sym_type = ELF64_ST_TYPE (sym->st_info);
15072 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15073 opd = get_opd_info (sec);
15074 if (opd != NULL && opd->adjust != NULL)
15076 long adjust = opd->adjust[OPD_NDX (sym->st_value
15077 + rel->r_addend)];
15078 if (adjust == -1)
15079 relocation = 0;
15080 else
15082 /* If this is a relocation against the opd section sym
15083 and we have edited .opd, adjust the reloc addend so
15084 that ld -r and ld --emit-relocs output is correct.
15085 If it is a reloc against some other .opd symbol,
15086 then the symbol value will be adjusted later. */
15087 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15088 rel->r_addend += adjust;
15089 else
15090 relocation += adjust;
15094 else
15096 bool ignored;
15098 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15099 r_symndx, symtab_hdr, sym_hashes,
15100 h_elf, sec, relocation,
15101 unresolved_reloc, warned, ignored);
15102 sym_name = h_elf->root.root.string;
15103 sym_type = h_elf->type;
15104 if (sec != NULL
15105 && sec->owner == output_bfd
15106 && strcmp (sec->name, ".opd") == 0)
15108 /* This is a symbol defined in a linker script. All
15109 such are defined in output sections, even those
15110 defined by simple assignment from a symbol defined in
15111 an input section. Transfer the symbol to an
15112 appropriate input .opd section, so that a branch to
15113 this symbol will be mapped to the location specified
15114 by the opd entry. */
15115 struct bfd_link_order *lo;
15116 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15117 if (lo->type == bfd_indirect_link_order)
15119 asection *isec = lo->u.indirect.section;
15120 if (h_elf->root.u.def.value >= isec->output_offset
15121 && h_elf->root.u.def.value < (isec->output_offset
15122 + isec->size))
15124 h_elf->root.u.def.value -= isec->output_offset;
15125 h_elf->root.u.def.section = isec;
15126 sec = isec;
15127 break;
15132 h = ppc_elf_hash_entry (h_elf);
15134 if (sec != NULL && discarded_section (sec))
15136 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15137 input_bfd, input_section,
15138 contents, rel->r_offset);
15139 wrel->r_offset = rel->r_offset;
15140 wrel->r_info = 0;
15141 wrel->r_addend = 0;
15143 /* For ld -r, remove relocations in debug sections against
15144 symbols defined in discarded sections. Not done for
15145 non-debug to preserve relocs in .eh_frame which the
15146 eh_frame editing code expects to be present. */
15147 if (bfd_link_relocatable (info)
15148 && (input_section->flags & SEC_DEBUGGING))
15149 wrel--;
15151 continue;
15154 if (bfd_link_relocatable (info))
15155 goto copy_reloc;
15157 if (h != NULL && &h->elf == htab->elf.hgot)
15159 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15160 sec = bfd_abs_section_ptr;
15161 unresolved_reloc = false;
15164 /* TLS optimizations. Replace instruction sequences and relocs
15165 based on information we collected in tls_optimize. We edit
15166 RELOCS so that --emit-relocs will output something sensible
15167 for the final instruction stream. */
15168 tls_mask = 0;
15169 tls_gd = 0;
15170 toc_symndx = 0;
15171 if (h != NULL)
15172 tls_mask = h->tls_mask;
15173 else if (local_got_ents != NULL)
15175 struct plt_entry **local_plt = (struct plt_entry **)
15176 (local_got_ents + symtab_hdr->sh_info);
15177 unsigned char *lgot_masks = (unsigned char *)
15178 (local_plt + symtab_hdr->sh_info);
15179 tls_mask = lgot_masks[r_symndx];
15181 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15182 && (r_type == R_PPC64_TLS
15183 || r_type == R_PPC64_TLSGD
15184 || r_type == R_PPC64_TLSLD))
15186 /* Check for toc tls entries. */
15187 unsigned char *toc_tls;
15189 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15190 &local_syms, rel, input_bfd))
15191 return false;
15193 if (toc_tls)
15194 tls_mask = *toc_tls;
15197 /* Check that tls relocs are used with tls syms, and non-tls
15198 relocs are used with non-tls syms. */
15199 if (r_symndx != STN_UNDEF
15200 && r_type != R_PPC64_NONE
15201 && (h == NULL
15202 || h->elf.root.type == bfd_link_hash_defined
15203 || h->elf.root.type == bfd_link_hash_defweak)
15204 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15206 if ((tls_mask & TLS_TLS) != 0
15207 && (r_type == R_PPC64_TLS
15208 || r_type == R_PPC64_TLSGD
15209 || r_type == R_PPC64_TLSLD))
15210 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15212 else
15213 info->callbacks->einfo
15214 (!IS_PPC64_TLS_RELOC (r_type)
15215 /* xgettext:c-format */
15216 ? _("%H: %s used with TLS symbol `%pT'\n")
15217 /* xgettext:c-format */
15218 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15219 input_bfd, input_section, rel->r_offset,
15220 ppc64_elf_howto_table[r_type]->name,
15221 sym_name);
15224 /* Ensure reloc mapping code below stays sane. */
15225 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15226 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15227 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15228 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15229 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15230 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15231 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15232 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15233 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15234 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15235 abort ();
15237 switch (r_type)
15239 default:
15240 break;
15242 case R_PPC64_LO_DS_OPT:
15243 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15245 insn = bfd_get_32 (input_bfd,
15246 contents + rel->r_offset - d_offset);
15247 if ((insn & (0x3fu << 26)) != 58u << 26)
15248 abort ();
15249 insn += (14u << 26) - (58u << 26);
15250 bfd_put_32 (input_bfd, insn,
15251 contents + rel->r_offset - d_offset);
15252 r_type = R_PPC64_TOC16_LO;
15253 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15255 break;
15257 case R_PPC64_TOC16:
15258 case R_PPC64_TOC16_LO:
15259 case R_PPC64_TOC16_DS:
15260 case R_PPC64_TOC16_LO_DS:
15262 /* Check for toc tls entries. */
15263 unsigned char *toc_tls;
15264 int retval;
15266 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15267 &local_syms, rel, input_bfd);
15268 if (retval == 0)
15269 return false;
15271 if (toc_tls)
15273 tls_mask = *toc_tls;
15274 if (r_type == R_PPC64_TOC16_DS
15275 || r_type == R_PPC64_TOC16_LO_DS)
15277 if ((tls_mask & TLS_TLS) != 0
15278 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15279 goto toctprel;
15281 else
15283 /* If we found a GD reloc pair, then we might be
15284 doing a GD->IE transition. */
15285 if (retval == 2)
15287 tls_gd = TLS_GDIE;
15288 if ((tls_mask & TLS_TLS) != 0
15289 && (tls_mask & TLS_GD) == 0)
15290 goto tls_ldgd_opt;
15292 else if (retval == 3)
15294 if ((tls_mask & TLS_TLS) != 0
15295 && (tls_mask & TLS_LD) == 0)
15296 goto tls_ldgd_opt;
15301 break;
15303 case R_PPC64_GOT_TPREL16_HI:
15304 case R_PPC64_GOT_TPREL16_HA:
15305 if ((tls_mask & TLS_TLS) != 0
15306 && (tls_mask & TLS_TPREL) == 0
15307 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15309 rel->r_offset -= d_offset;
15310 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15311 r_type = R_PPC64_NONE;
15312 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15314 break;
15316 case R_PPC64_GOT_TPREL16_DS:
15317 case R_PPC64_GOT_TPREL16_LO_DS:
15318 if ((tls_mask & TLS_TLS) != 0
15319 && (tls_mask & TLS_TPREL) == 0
15320 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15322 toctprel:
15323 insn = bfd_get_32 (input_bfd,
15324 contents + rel->r_offset - d_offset);
15325 insn &= 31 << 21;
15326 insn |= 0x3c0d0000; /* addis 0,13,0 */
15327 bfd_put_32 (input_bfd, insn,
15328 contents + rel->r_offset - d_offset);
15329 r_type = R_PPC64_TPREL16_HA;
15330 if (toc_symndx != 0)
15332 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15333 rel->r_addend = toc_addend;
15334 /* We changed the symbol. Start over in order to
15335 get h, sym, sec etc. right. */
15336 goto again;
15338 else
15339 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15341 break;
15343 case R_PPC64_GOT_TPREL_PCREL34:
15344 if ((tls_mask & TLS_TLS) != 0
15345 && (tls_mask & TLS_TPREL) == 0
15346 && offset_in_range (input_section, rel->r_offset, 8))
15348 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15349 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15350 pinsn <<= 32;
15351 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15352 pinsn += ((2ULL << 56) + (-1ULL << 52)
15353 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15354 bfd_put_32 (input_bfd, pinsn >> 32,
15355 contents + rel->r_offset);
15356 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15357 contents + rel->r_offset + 4);
15358 r_type = R_PPC64_TPREL34;
15359 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15361 break;
15363 case R_PPC64_TLS:
15364 if ((tls_mask & TLS_TLS) != 0
15365 && (tls_mask & TLS_TPREL) == 0
15366 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15368 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15369 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15370 if (insn == 0)
15371 break;
15372 if ((rel->r_offset & 3) == 0)
15374 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15375 /* Was PPC64_TLS which sits on insn boundary, now
15376 PPC64_TPREL16_LO which is at low-order half-word. */
15377 rel->r_offset += d_offset;
15378 r_type = R_PPC64_TPREL16_LO;
15379 if (toc_symndx != 0)
15381 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15382 rel->r_addend = toc_addend;
15383 /* We changed the symbol. Start over in order to
15384 get h, sym, sec etc. right. */
15385 goto again;
15387 else
15388 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15390 else if ((rel->r_offset & 3) == 1)
15392 /* For pcrel IE to LE we already have the full
15393 offset and thus don't need an addi here. A nop
15394 or mr will do. */
15395 if ((insn & (0x3fu << 26)) == 14 << 26)
15397 /* Extract regs from addi rt,ra,si. */
15398 unsigned int rt = (insn >> 21) & 0x1f;
15399 unsigned int ra = (insn >> 16) & 0x1f;
15400 if (rt == ra)
15401 insn = NOP;
15402 else
15404 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15405 insn = (rt << 16) | (ra << 21) | (ra << 11);
15406 insn |= (31u << 26) | (444u << 1);
15409 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15412 break;
15414 case R_PPC64_GOT_TLSGD16_HI:
15415 case R_PPC64_GOT_TLSGD16_HA:
15416 tls_gd = TLS_GDIE;
15417 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15418 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15419 goto tls_gdld_hi;
15420 break;
15422 case R_PPC64_GOT_TLSLD16_HI:
15423 case R_PPC64_GOT_TLSLD16_HA:
15424 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15425 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15427 tls_gdld_hi:
15428 if ((tls_mask & tls_gd) != 0)
15429 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15430 + R_PPC64_GOT_TPREL16_DS);
15431 else
15433 rel->r_offset -= d_offset;
15434 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15435 r_type = R_PPC64_NONE;
15437 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15439 break;
15441 case R_PPC64_GOT_TLSGD16:
15442 case R_PPC64_GOT_TLSGD16_LO:
15443 tls_gd = TLS_GDIE;
15444 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15445 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15446 goto tls_ldgd_opt;
15447 break;
15449 case R_PPC64_GOT_TLSLD16:
15450 case R_PPC64_GOT_TLSLD16_LO:
15451 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15452 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15454 unsigned int insn1, insn2;
15456 tls_ldgd_opt:
15457 offset = (bfd_vma) -1;
15458 /* If not using the newer R_PPC64_TLSGD/LD to mark
15459 __tls_get_addr calls, we must trust that the call
15460 stays with its arg setup insns, ie. that the next
15461 reloc is the __tls_get_addr call associated with
15462 the current reloc. Edit both insns. */
15463 if (input_section->nomark_tls_get_addr
15464 && rel + 1 < relend
15465 && branch_reloc_hash_match (input_bfd, rel + 1,
15466 htab->tls_get_addr_fd,
15467 htab->tga_desc_fd,
15468 htab->tls_get_addr,
15469 htab->tga_desc))
15470 offset = rel[1].r_offset;
15471 /* We read the low GOT_TLS (or TOC16) insn because we
15472 need to keep the destination reg. It may be
15473 something other than the usual r3, and moved to r3
15474 before the call by intervening code. */
15475 insn1 = bfd_get_32 (input_bfd,
15476 contents + rel->r_offset - d_offset);
15477 if ((tls_mask & tls_gd) != 0)
15479 /* IE */
15480 insn1 &= (0x1f << 21) | (0x1f << 16);
15481 insn1 |= 58u << 26; /* ld */
15482 insn2 = 0x7c636a14; /* add 3,3,13 */
15483 if (offset != (bfd_vma) -1)
15484 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15485 if (r_type == R_PPC64_TOC16
15486 || r_type == R_PPC64_TOC16_LO)
15487 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15488 else
15489 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15490 + R_PPC64_GOT_TPREL16_DS);
15491 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15493 else
15495 /* LE */
15496 insn1 &= 0x1f << 21;
15497 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15498 insn2 = 0x38630000; /* addi 3,3,0 */
15499 if (tls_gd == 0)
15501 /* Was an LD reloc. */
15502 r_symndx = STN_UNDEF;
15503 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15505 else if (toc_symndx != 0)
15507 r_symndx = toc_symndx;
15508 rel->r_addend = toc_addend;
15510 r_type = R_PPC64_TPREL16_HA;
15511 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15512 if (offset != (bfd_vma) -1)
15514 rel[1].r_info = ELF64_R_INFO (r_symndx,
15515 R_PPC64_TPREL16_LO);
15516 rel[1].r_offset = offset + d_offset;
15517 rel[1].r_addend = rel->r_addend;
15520 bfd_put_32 (input_bfd, insn1,
15521 contents + rel->r_offset - d_offset);
15522 if (offset != (bfd_vma) -1
15523 && offset_in_range (input_section, offset, 4))
15525 bfd_put_32 (input_bfd, insn2, contents + offset);
15526 if (offset_in_range (input_section, offset + 4, 4))
15528 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15529 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15530 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15533 if ((tls_mask & tls_gd) == 0
15534 && (tls_gd == 0 || toc_symndx != 0))
15536 /* We changed the symbol. Start over in order
15537 to get h, sym, sec etc. right. */
15538 goto again;
15541 break;
15543 case R_PPC64_GOT_TLSGD_PCREL34:
15544 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15545 && offset_in_range (input_section, rel->r_offset, 8))
15547 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15548 pinsn <<= 32;
15549 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15550 if ((tls_mask & TLS_GDIE) != 0)
15552 /* IE, pla -> pld */
15553 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
15554 r_type = R_PPC64_GOT_TPREL_PCREL34;
15556 else
15558 /* LE, pla pcrel -> paddi r13 */
15559 pinsn += (-1ULL << 52) + (13ULL << 16);
15560 r_type = R_PPC64_TPREL34;
15562 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15563 bfd_put_32 (input_bfd, pinsn >> 32,
15564 contents + rel->r_offset);
15565 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15566 contents + rel->r_offset + 4);
15568 break;
15570 case R_PPC64_GOT_TLSLD_PCREL34:
15571 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15572 && offset_in_range (input_section, rel->r_offset, 8))
15574 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15575 pinsn <<= 32;
15576 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15577 pinsn += (-1ULL << 52) + (13ULL << 16);
15578 bfd_put_32 (input_bfd, pinsn >> 32,
15579 contents + rel->r_offset);
15580 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15581 contents + rel->r_offset + 4);
15582 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15583 r_symndx = STN_UNDEF;
15584 r_type = R_PPC64_TPREL34;
15585 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15586 goto again;
15588 break;
15590 case R_PPC64_TLSGD:
15591 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15592 && rel + 1 < relend
15593 && offset_in_range (input_section, rel->r_offset,
15594 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15595 ? 8 : 4))
15597 unsigned int insn2;
15598 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15600 offset = rel->r_offset;
15601 if (is_plt_seq_reloc (r_type1))
15603 bfd_put_32 (output_bfd, NOP, contents + offset);
15604 if (r_type1 == R_PPC64_PLT_PCREL34
15605 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15606 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15607 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15608 break;
15611 if (r_type1 == R_PPC64_PLTCALL)
15612 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15614 if ((tls_mask & TLS_GDIE) != 0)
15616 /* IE */
15617 r_type = R_PPC64_NONE;
15618 insn2 = 0x7c636a14; /* add 3,3,13 */
15620 else
15622 /* LE */
15623 if (toc_symndx != 0)
15625 r_symndx = toc_symndx;
15626 rel->r_addend = toc_addend;
15628 if (r_type1 == R_PPC64_REL24_NOTOC
15629 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15631 r_type = R_PPC64_NONE;
15632 insn2 = NOP;
15634 else
15636 rel->r_offset = offset + d_offset;
15637 r_type = R_PPC64_TPREL16_LO;
15638 insn2 = 0x38630000; /* addi 3,3,0 */
15641 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15642 /* Zap the reloc on the _tls_get_addr call too. */
15643 BFD_ASSERT (offset == rel[1].r_offset);
15644 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15645 bfd_put_32 (input_bfd, insn2, contents + offset);
15646 if ((tls_mask & TLS_GDIE) == 0
15647 && toc_symndx != 0
15648 && r_type != R_PPC64_NONE)
15649 goto again;
15651 break;
15653 case R_PPC64_TLSLD:
15654 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15655 && rel + 1 < relend
15656 && offset_in_range (input_section, rel->r_offset,
15657 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15658 ? 8 : 4))
15660 unsigned int insn2;
15661 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15663 offset = rel->r_offset;
15664 if (is_plt_seq_reloc (r_type1))
15666 bfd_put_32 (output_bfd, NOP, contents + offset);
15667 if (r_type1 == R_PPC64_PLT_PCREL34
15668 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15669 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15670 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15671 break;
15674 if (r_type1 == R_PPC64_PLTCALL)
15675 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15677 if (r_type1 == R_PPC64_REL24_NOTOC
15678 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15680 r_type = R_PPC64_NONE;
15681 insn2 = NOP;
15683 else
15685 rel->r_offset = offset + d_offset;
15686 r_symndx = STN_UNDEF;
15687 r_type = R_PPC64_TPREL16_LO;
15688 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15689 insn2 = 0x38630000; /* addi 3,3,0 */
15691 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15692 /* Zap the reloc on the _tls_get_addr call too. */
15693 BFD_ASSERT (offset == rel[1].r_offset);
15694 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15695 bfd_put_32 (input_bfd, insn2, contents + offset);
15696 if (r_type != R_PPC64_NONE)
15697 goto again;
15699 break;
15701 case R_PPC64_DTPMOD64:
15702 if (rel + 1 < relend
15703 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15704 && rel[1].r_offset == rel->r_offset + 8)
15706 if ((tls_mask & TLS_GD) == 0
15707 && offset_in_range (input_section, rel->r_offset, 8))
15709 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
15710 if ((tls_mask & TLS_GDIE) != 0)
15711 r_type = R_PPC64_TPREL64;
15712 else
15714 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15715 r_type = R_PPC64_NONE;
15717 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15720 else
15722 if ((tls_mask & TLS_LD) == 0
15723 && offset_in_range (input_section, rel->r_offset, 8))
15725 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15726 r_type = R_PPC64_NONE;
15727 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15730 break;
15732 case R_PPC64_TPREL64:
15733 if ((tls_mask & TLS_TPREL) == 0)
15735 r_type = R_PPC64_NONE;
15736 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15738 break;
15740 case R_PPC64_ENTRY:
15741 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15742 if (!bfd_link_pic (info)
15743 && !info->traditional_format
15744 && relocation + 0x80008000 <= 0xffffffff
15745 && offset_in_range (input_section, rel->r_offset, 8))
15747 unsigned int insn1, insn2;
15749 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15750 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15751 if ((insn1 & ~0xfffc) == LD_R2_0R12
15752 && insn2 == ADD_R2_R2_R12)
15754 bfd_put_32 (input_bfd,
15755 LIS_R2 + PPC_HA (relocation),
15756 contents + rel->r_offset);
15757 bfd_put_32 (input_bfd,
15758 ADDI_R2_R2 + PPC_LO (relocation),
15759 contents + rel->r_offset + 4);
15762 else
15764 relocation -= (rel->r_offset
15765 + input_section->output_offset
15766 + input_section->output_section->vma);
15767 if (relocation + 0x80008000 <= 0xffffffff
15768 && offset_in_range (input_section, rel->r_offset, 8))
15770 unsigned int insn1, insn2;
15772 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15773 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15774 if ((insn1 & ~0xfffc) == LD_R2_0R12
15775 && insn2 == ADD_R2_R2_R12)
15777 bfd_put_32 (input_bfd,
15778 ADDIS_R2_R12 + PPC_HA (relocation),
15779 contents + rel->r_offset);
15780 bfd_put_32 (input_bfd,
15781 ADDI_R2_R2 + PPC_LO (relocation),
15782 contents + rel->r_offset + 4);
15786 break;
15788 case R_PPC64_REL16_HA:
15789 /* If we are generating a non-PIC executable, edit
15790 . 0: addis 2,12,.TOC.-0b@ha
15791 . addi 2,2,.TOC.-0b@l
15792 used by ELFv2 global entry points to set up r2, to
15793 . lis 2,.TOC.@ha
15794 . addi 2,2,.TOC.@l
15795 if .TOC. is in range. */
15796 if (!bfd_link_pic (info)
15797 && !info->traditional_format
15798 && !htab->opd_abi
15799 && rel->r_addend == d_offset
15800 && h != NULL && &h->elf == htab->elf.hgot
15801 && rel + 1 < relend
15802 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15803 && rel[1].r_offset == rel->r_offset + 4
15804 && rel[1].r_addend == rel->r_addend + 4
15805 && relocation + 0x80008000 <= 0xffffffff
15806 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
15808 unsigned int insn1, insn2;
15809 offset = rel->r_offset - d_offset;
15810 insn1 = bfd_get_32 (input_bfd, contents + offset);
15811 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15812 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15813 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15815 r_type = R_PPC64_ADDR16_HA;
15816 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15817 rel->r_addend -= d_offset;
15818 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15819 rel[1].r_addend -= d_offset + 4;
15820 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15823 break;
15826 /* Handle other relocations that tweak non-addend part of insn. */
15827 insn = 0;
15828 max_br_offset = 1 << 25;
15829 addend = rel->r_addend;
15830 reloc_dest = DEST_NORMAL;
15831 switch (r_type)
15833 default:
15834 break;
15836 case R_PPC64_TOCSAVE:
15837 if (relocation + addend == (rel->r_offset
15838 + input_section->output_offset
15839 + input_section->output_section->vma)
15840 && tocsave_find (htab, NO_INSERT,
15841 &local_syms, rel, input_bfd)
15842 && offset_in_range (input_section, rel->r_offset, 4))
15844 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15845 if (insn == NOP
15846 || insn == CROR_151515 || insn == CROR_313131)
15847 bfd_put_32 (input_bfd,
15848 STD_R2_0R1 + STK_TOC (htab),
15849 contents + rel->r_offset);
15851 break;
15853 /* Branch taken prediction relocations. */
15854 case R_PPC64_ADDR14_BRTAKEN:
15855 case R_PPC64_REL14_BRTAKEN:
15856 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15857 /* Fall through. */
15859 /* Branch not taken prediction relocations. */
15860 case R_PPC64_ADDR14_BRNTAKEN:
15861 case R_PPC64_REL14_BRNTAKEN:
15862 if (!offset_in_range (input_section, rel->r_offset, 4))
15863 break;
15864 insn |= bfd_get_32 (input_bfd,
15865 contents + rel->r_offset) & ~(0x01 << 21);
15866 /* Fall through. */
15868 case R_PPC64_REL14:
15869 max_br_offset = 1 << 15;
15870 /* Fall through. */
15872 case R_PPC64_REL24:
15873 case R_PPC64_REL24_NOTOC:
15874 case R_PPC64_PLTCALL:
15875 case R_PPC64_PLTCALL_NOTOC:
15876 /* Calls to functions with a different TOC, such as calls to
15877 shared objects, need to alter the TOC pointer. This is
15878 done using a linkage stub. A REL24 branching to these
15879 linkage stubs needs to be followed by a nop, as the nop
15880 will be replaced with an instruction to restore the TOC
15881 base pointer. */
15882 fdh = h;
15883 if (h != NULL
15884 && h->oh != NULL
15885 && h->oh->is_func_descriptor)
15886 fdh = ppc_follow_link (h->oh);
15887 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15888 htab);
15889 if ((r_type == R_PPC64_PLTCALL
15890 || r_type == R_PPC64_PLTCALL_NOTOC)
15891 && stub_entry != NULL
15892 && stub_entry->stub_type >= ppc_stub_plt_call
15893 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15894 stub_entry = NULL;
15896 if (stub_entry != NULL
15897 && ((stub_entry->stub_type >= ppc_stub_plt_call
15898 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15899 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15900 || stub_entry->stub_type == ppc_stub_plt_branch_both
15901 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15902 || stub_entry->stub_type == ppc_stub_long_branch_both))
15904 bool can_plt_call = false;
15906 if (stub_entry->stub_type == ppc_stub_plt_call
15907 && !htab->opd_abi
15908 && htab->params->plt_localentry0 != 0
15909 && h != NULL
15910 && is_elfv2_localentry0 (&h->elf))
15912 /* The function doesn't use or change r2. */
15913 can_plt_call = true;
15915 else if (r_type == R_PPC64_REL24_NOTOC)
15917 /* NOTOC calls don't need to restore r2. */
15918 can_plt_call = true;
15921 /* All of these stubs may modify r2, so there must be a
15922 branch and link followed by a nop. The nop is
15923 replaced by an insn to restore r2. */
15924 else if (offset_in_range (input_section, rel->r_offset, 8))
15926 unsigned long br;
15928 br = bfd_get_32 (input_bfd,
15929 contents + rel->r_offset);
15930 if ((br & 1) != 0)
15932 unsigned long nop;
15934 nop = bfd_get_32 (input_bfd,
15935 contents + rel->r_offset + 4);
15936 if (nop == LD_R2_0R1 + STK_TOC (htab))
15937 can_plt_call = true;
15938 else if (nop == NOP
15939 || nop == CROR_151515
15940 || nop == CROR_313131)
15942 if (h != NULL
15943 && is_tls_get_addr (&h->elf, htab)
15944 && htab->params->tls_get_addr_opt)
15946 /* Special stub used, leave nop alone. */
15948 else
15949 bfd_put_32 (input_bfd,
15950 LD_R2_0R1 + STK_TOC (htab),
15951 contents + rel->r_offset + 4);
15952 can_plt_call = true;
15957 if (!can_plt_call && h != NULL)
15959 const char *name = h->elf.root.root.string;
15961 if (*name == '.')
15962 ++name;
15964 if (startswith (name, "__libc_start_main")
15965 && (name[17] == 0 || name[17] == '@'))
15967 /* Allow crt1 branch to go via a toc adjusting
15968 stub. Other calls that never return could do
15969 the same, if we could detect such. */
15970 can_plt_call = true;
15974 if (!can_plt_call)
15976 /* g++ as of 20130507 emits self-calls without a
15977 following nop. This is arguably wrong since we
15978 have conflicting information. On the one hand a
15979 global symbol and on the other a local call
15980 sequence, but don't error for this special case.
15981 It isn't possible to cheaply verify we have
15982 exactly such a call. Allow all calls to the same
15983 section. */
15984 asection *code_sec = sec;
15986 if (get_opd_info (sec) != NULL)
15988 bfd_vma off = (relocation + addend
15989 - sec->output_section->vma
15990 - sec->output_offset);
15992 opd_entry_value (sec, off, &code_sec, NULL, false);
15994 if (code_sec == input_section)
15995 can_plt_call = true;
15998 if (!can_plt_call)
16000 if (stub_entry->stub_type >= ppc_stub_plt_call
16001 && stub_entry->stub_type <= ppc_stub_plt_call_both)
16002 info->callbacks->einfo
16003 /* xgettext:c-format */
16004 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16005 "(plt call stub)\n"),
16006 input_bfd, input_section, rel->r_offset, sym_name);
16007 else
16008 info->callbacks->einfo
16009 /* xgettext:c-format */
16010 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16011 "(toc save/adjust stub)\n"),
16012 input_bfd, input_section, rel->r_offset, sym_name);
16014 bfd_set_error (bfd_error_bad_value);
16015 ret = false;
16018 if (can_plt_call
16019 && stub_entry->stub_type >= ppc_stub_plt_call
16020 && stub_entry->stub_type <= ppc_stub_plt_call_both)
16021 unresolved_reloc = false;
16024 if ((stub_entry == NULL
16025 || stub_entry->stub_type == ppc_stub_long_branch
16026 || stub_entry->stub_type == ppc_stub_plt_branch)
16027 && get_opd_info (sec) != NULL)
16029 /* The branch destination is the value of the opd entry. */
16030 bfd_vma off = (relocation + addend
16031 - sec->output_section->vma
16032 - sec->output_offset);
16033 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16034 if (dest != (bfd_vma) -1)
16036 relocation = dest;
16037 addend = 0;
16038 reloc_dest = DEST_OPD;
16042 /* If the branch is out of reach we ought to have a long
16043 branch stub. */
16044 from = (rel->r_offset
16045 + input_section->output_offset
16046 + input_section->output_section->vma);
16048 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16049 ? fdh->elf.other
16050 : sym->st_other);
16052 if (stub_entry != NULL
16053 && (stub_entry->stub_type == ppc_stub_long_branch
16054 || stub_entry->stub_type == ppc_stub_plt_branch)
16055 && (r_type == R_PPC64_ADDR14_BRTAKEN
16056 || r_type == R_PPC64_ADDR14_BRNTAKEN
16057 || (relocation + addend - from + max_br_offset
16058 < 2 * max_br_offset)))
16059 /* Don't use the stub if this branch is in range. */
16060 stub_entry = NULL;
16062 if (stub_entry != NULL
16063 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
16064 || stub_entry->stub_type == ppc_stub_long_branch_both
16065 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
16066 || stub_entry->stub_type == ppc_stub_plt_branch_both)
16067 && (r_type != R_PPC64_REL24_NOTOC
16068 || ((fdh ? fdh->elf.other : sym->st_other)
16069 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16070 && (relocation + addend - from + max_br_offset
16071 < 2 * max_br_offset))
16072 stub_entry = NULL;
16074 if (stub_entry != NULL
16075 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
16076 || stub_entry->stub_type == ppc_stub_long_branch_both
16077 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
16078 || stub_entry->stub_type == ppc_stub_plt_branch_both)
16079 && r_type == R_PPC64_REL24_NOTOC
16080 && (relocation + addend - from + max_br_offset
16081 < 2 * max_br_offset))
16082 stub_entry = NULL;
16084 if (stub_entry != NULL)
16086 /* Munge up the value and addend so that we call the stub
16087 rather than the procedure directly. */
16088 asection *stub_sec = stub_entry->group->stub_sec;
16090 if (stub_entry->stub_type == ppc_stub_save_res)
16091 relocation += (stub_sec->output_offset
16092 + stub_sec->output_section->vma
16093 + stub_sec->size - htab->sfpr->size
16094 - htab->sfpr->output_offset
16095 - htab->sfpr->output_section->vma);
16096 else
16097 relocation = (stub_entry->stub_offset
16098 + stub_sec->output_offset
16099 + stub_sec->output_section->vma);
16100 addend = 0;
16101 reloc_dest = DEST_STUB;
16103 if ((((stub_entry->stub_type == ppc_stub_plt_call
16104 && ALWAYS_EMIT_R2SAVE)
16105 || stub_entry->stub_type == ppc_stub_plt_call_r2save
16106 || stub_entry->stub_type == ppc_stub_plt_call_both)
16107 && rel + 1 < relend
16108 && rel[1].r_offset == rel->r_offset + 4
16109 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
16110 || ((stub_entry->stub_type == ppc_stub_long_branch_both
16111 || stub_entry->stub_type == ppc_stub_plt_branch_both
16112 || stub_entry->stub_type == ppc_stub_plt_call_both)
16113 && r_type == R_PPC64_REL24_NOTOC))
16115 /* Skip over the r2 store at the start of the stub. */
16116 if (!(stub_entry->stub_type >= ppc_stub_plt_call
16117 && htab->params->tls_get_addr_opt
16118 && h != NULL
16119 && is_tls_get_addr (&h->elf, htab)))
16120 relocation += 4;
16123 if (r_type == R_PPC64_REL24_NOTOC
16124 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
16125 || stub_entry->stub_type == ppc_stub_plt_call_both))
16126 htab->notoc_plt = 1;
16129 if (insn != 0)
16131 if (is_isa_v2)
16133 /* Set 'a' bit. This is 0b00010 in BO field for branch
16134 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16135 for branch on CTR insns (BO == 1a00t or 1a01t). */
16136 if ((insn & (0x14 << 21)) == (0x04 << 21))
16137 insn |= 0x02 << 21;
16138 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16139 insn |= 0x08 << 21;
16140 else
16141 break;
16143 else
16145 /* Invert 'y' bit if not the default. */
16146 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16147 insn ^= 0x01 << 21;
16150 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16153 /* NOP out calls to undefined weak functions.
16154 We can thus call a weak function without first
16155 checking whether the function is defined. */
16156 else if (h != NULL
16157 && h->elf.root.type == bfd_link_hash_undefweak
16158 && h->elf.dynindx == -1
16159 && (r_type == R_PPC64_REL24
16160 || r_type == R_PPC64_REL24_NOTOC)
16161 && relocation == 0
16162 && addend == 0
16163 && offset_in_range (input_section, rel->r_offset, 4))
16165 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16166 goto copy_reloc;
16168 break;
16170 case R_PPC64_GOT16_DS:
16171 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16172 || !htab->do_toc_opt)
16173 break;
16174 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16175 if (relocation + addend - from + 0x8000 < 0x10000
16176 && sec != NULL
16177 && sec->output_section != NULL
16178 && !discarded_section (sec)
16179 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16180 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16182 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16183 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16185 insn += (14u << 26) - (58u << 26);
16186 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16187 r_type = R_PPC64_TOC16;
16188 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16191 break;
16193 case R_PPC64_GOT16_LO_DS:
16194 case R_PPC64_GOT16_HA:
16195 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16196 || !htab->do_toc_opt)
16197 break;
16198 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16199 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16200 && sec != NULL
16201 && sec->output_section != NULL
16202 && !discarded_section (sec)
16203 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16204 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16206 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16207 if (r_type == R_PPC64_GOT16_LO_DS
16208 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16210 insn += (14u << 26) - (58u << 26);
16211 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16212 r_type = R_PPC64_TOC16_LO;
16213 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16215 else if (r_type == R_PPC64_GOT16_HA
16216 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16218 r_type = R_PPC64_TOC16_HA;
16219 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16222 break;
16224 case R_PPC64_GOT_PCREL34:
16225 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16226 || !htab->do_toc_opt)
16227 break;
16228 from = (rel->r_offset
16229 + input_section->output_section->vma
16230 + input_section->output_offset);
16231 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16232 && sec != NULL
16233 && sec->output_section != NULL
16234 && !discarded_section (sec)
16235 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16236 && offset_in_range (input_section, rel->r_offset, 8)))
16237 break;
16239 offset = rel->r_offset;
16240 pinsn = bfd_get_32 (input_bfd, contents + offset);
16241 pinsn <<= 32;
16242 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16243 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16244 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16245 break;
16247 /* Replace with paddi. */
16248 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16249 r_type = R_PPC64_PCREL34;
16250 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16251 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16252 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16253 /* Fall through. */
16255 case R_PPC64_PCREL34:
16256 if (!htab->params->no_pcrel_opt
16257 && rel + 1 < relend
16258 && rel[1].r_offset == rel->r_offset
16259 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16260 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16261 && offset_in_range (input_section, rel->r_offset, 8))
16263 offset = rel->r_offset;
16264 pinsn = bfd_get_32 (input_bfd, contents + offset);
16265 pinsn <<= 32;
16266 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16267 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16268 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16269 | (14ULL << 26) /* paddi */))
16271 bfd_vma off2 = rel[1].r_addend;
16272 if (off2 == 0)
16273 /* zero means next insn. */
16274 off2 = 8;
16275 off2 += offset;
16276 if (offset_in_range (input_section, off2, 4))
16278 uint64_t pinsn2;
16279 bfd_signed_vma addend_off;
16280 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16281 pinsn2 <<= 32;
16282 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16284 if (!offset_in_range (input_section, off2, 8))
16285 break;
16286 pinsn2 |= bfd_get_32 (input_bfd,
16287 contents + off2 + 4);
16289 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16291 addend += addend_off;
16292 rel->r_addend = addend;
16293 bfd_put_32 (input_bfd, pinsn >> 32,
16294 contents + offset);
16295 bfd_put_32 (input_bfd, pinsn,
16296 contents + offset + 4);
16297 bfd_put_32 (input_bfd, pinsn2 >> 32,
16298 contents + off2);
16299 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16300 bfd_put_32 (input_bfd, pinsn2,
16301 contents + off2 + 4);
16306 break;
16309 tls_type = 0;
16310 save_unresolved_reloc = unresolved_reloc;
16311 switch (r_type)
16313 default:
16314 /* xgettext:c-format */
16315 _bfd_error_handler (_("%pB: %s unsupported"),
16316 input_bfd, ppc64_elf_howto_table[r_type]->name);
16318 bfd_set_error (bfd_error_bad_value);
16319 ret = false;
16320 goto copy_reloc;
16322 case R_PPC64_NONE:
16323 case R_PPC64_TLS:
16324 case R_PPC64_TLSGD:
16325 case R_PPC64_TLSLD:
16326 case R_PPC64_TOCSAVE:
16327 case R_PPC64_GNU_VTINHERIT:
16328 case R_PPC64_GNU_VTENTRY:
16329 case R_PPC64_ENTRY:
16330 case R_PPC64_PCREL_OPT:
16331 goto copy_reloc;
16333 /* GOT16 relocations. Like an ADDR16 using the symbol's
16334 address in the GOT as relocation value instead of the
16335 symbol's value itself. Also, create a GOT entry for the
16336 symbol and put the symbol value there. */
16337 case R_PPC64_GOT_TLSGD16:
16338 case R_PPC64_GOT_TLSGD16_LO:
16339 case R_PPC64_GOT_TLSGD16_HI:
16340 case R_PPC64_GOT_TLSGD16_HA:
16341 case R_PPC64_GOT_TLSGD_PCREL34:
16342 tls_type = TLS_TLS | TLS_GD;
16343 goto dogot;
16345 case R_PPC64_GOT_TLSLD16:
16346 case R_PPC64_GOT_TLSLD16_LO:
16347 case R_PPC64_GOT_TLSLD16_HI:
16348 case R_PPC64_GOT_TLSLD16_HA:
16349 case R_PPC64_GOT_TLSLD_PCREL34:
16350 tls_type = TLS_TLS | TLS_LD;
16351 goto dogot;
16353 case R_PPC64_GOT_TPREL16_DS:
16354 case R_PPC64_GOT_TPREL16_LO_DS:
16355 case R_PPC64_GOT_TPREL16_HI:
16356 case R_PPC64_GOT_TPREL16_HA:
16357 case R_PPC64_GOT_TPREL_PCREL34:
16358 tls_type = TLS_TLS | TLS_TPREL;
16359 goto dogot;
16361 case R_PPC64_GOT_DTPREL16_DS:
16362 case R_PPC64_GOT_DTPREL16_LO_DS:
16363 case R_PPC64_GOT_DTPREL16_HI:
16364 case R_PPC64_GOT_DTPREL16_HA:
16365 case R_PPC64_GOT_DTPREL_PCREL34:
16366 tls_type = TLS_TLS | TLS_DTPREL;
16367 goto dogot;
16369 case R_PPC64_GOT16:
16370 case R_PPC64_GOT16_LO:
16371 case R_PPC64_GOT16_HI:
16372 case R_PPC64_GOT16_HA:
16373 case R_PPC64_GOT16_DS:
16374 case R_PPC64_GOT16_LO_DS:
16375 case R_PPC64_GOT_PCREL34:
16376 dogot:
16378 /* Relocation is to the entry for this symbol in the global
16379 offset table. */
16380 asection *got;
16381 bfd_vma *offp;
16382 bfd_vma off;
16383 unsigned long indx = 0;
16384 struct got_entry *ent;
16386 if (tls_type == (TLS_TLS | TLS_LD)
16387 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16388 ent = ppc64_tlsld_got (input_bfd);
16389 else
16391 if (h != NULL)
16393 if (!htab->elf.dynamic_sections_created
16394 || h->elf.dynindx == -1
16395 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16396 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16397 /* This is actually a static link, or it is a
16398 -Bsymbolic link and the symbol is defined
16399 locally, or the symbol was forced to be local
16400 because of a version file. */
16402 else
16404 indx = h->elf.dynindx;
16405 unresolved_reloc = false;
16407 ent = h->elf.got.glist;
16409 else
16411 if (local_got_ents == NULL)
16412 abort ();
16413 ent = local_got_ents[r_symndx];
16416 for (; ent != NULL; ent = ent->next)
16417 if (ent->addend == orig_rel.r_addend
16418 && ent->owner == input_bfd
16419 && ent->tls_type == tls_type)
16420 break;
16423 if (ent == NULL)
16424 abort ();
16425 if (ent->is_indirect)
16426 ent = ent->got.ent;
16427 offp = &ent->got.offset;
16428 got = ppc64_elf_tdata (ent->owner)->got;
16429 if (got == NULL)
16430 abort ();
16432 /* The offset must always be a multiple of 8. We use the
16433 least significant bit to record whether we have already
16434 processed this entry. */
16435 off = *offp;
16436 if ((off & 1) != 0)
16437 off &= ~1;
16438 else
16440 /* Generate relocs for the dynamic linker, except in
16441 the case of TLSLD where we'll use one entry per
16442 module. */
16443 asection *relgot;
16444 bool ifunc;
16446 *offp = off | 1;
16447 relgot = NULL;
16448 ifunc = (h != NULL
16449 ? h->elf.type == STT_GNU_IFUNC
16450 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16451 if (ifunc)
16453 relgot = htab->elf.irelplt;
16454 if (indx == 0 || is_static_defined (&h->elf))
16455 htab->elf.ifunc_resolvers = true;
16457 else if (indx != 0
16458 || (bfd_link_pic (info)
16459 && (h == NULL
16460 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16461 && !(tls_type != 0
16462 && bfd_link_executable (info)
16463 && (h == NULL
16464 || SYMBOL_REFERENCES_LOCAL (info,
16465 &h->elf)))))
16466 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16467 if (relgot != NULL)
16469 outrel.r_offset = (got->output_section->vma
16470 + got->output_offset
16471 + off);
16472 outrel.r_addend = orig_rel.r_addend;
16473 if (tls_type & (TLS_LD | TLS_GD))
16475 outrel.r_addend = 0;
16476 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16477 if (tls_type == (TLS_TLS | TLS_GD))
16479 loc = relgot->contents;
16480 loc += (relgot->reloc_count++
16481 * sizeof (Elf64_External_Rela));
16482 bfd_elf64_swap_reloca_out (output_bfd,
16483 &outrel, loc);
16484 outrel.r_offset += 8;
16485 outrel.r_addend = orig_rel.r_addend;
16486 outrel.r_info
16487 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16490 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16491 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16492 else if (tls_type == (TLS_TLS | TLS_TPREL))
16493 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16494 else if (indx != 0)
16495 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16496 else
16498 if (ifunc)
16499 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16500 else
16501 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16503 /* Write the .got section contents for the sake
16504 of prelink. */
16505 loc = got->contents + off;
16506 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16507 loc);
16510 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16512 outrel.r_addend += relocation;
16513 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16515 if (htab->elf.tls_sec == NULL)
16516 outrel.r_addend = 0;
16517 else
16518 outrel.r_addend -= htab->elf.tls_sec->vma;
16521 loc = relgot->contents;
16522 loc += (relgot->reloc_count++
16523 * sizeof (Elf64_External_Rela));
16524 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16527 /* Init the .got section contents here if we're not
16528 emitting a reloc. */
16529 else
16531 relocation += orig_rel.r_addend;
16532 if (tls_type != 0)
16534 if (htab->elf.tls_sec == NULL)
16535 relocation = 0;
16536 else
16538 if (tls_type & TLS_LD)
16539 relocation = 0;
16540 else
16541 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
16542 if (tls_type & TLS_TPREL)
16543 relocation += DTP_OFFSET - TP_OFFSET;
16546 if (tls_type & (TLS_GD | TLS_LD))
16548 bfd_put_64 (output_bfd, relocation,
16549 got->contents + off + 8);
16550 relocation = 1;
16553 bfd_put_64 (output_bfd, relocation,
16554 got->contents + off);
16558 if (off >= (bfd_vma) -2)
16559 abort ();
16561 relocation = got->output_section->vma + got->output_offset + off;
16562 addend = 0;
16563 if (!(r_type == R_PPC64_GOT_PCREL34
16564 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16565 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16566 || r_type == R_PPC64_GOT_TPREL_PCREL34
16567 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
16568 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
16570 break;
16572 case R_PPC64_PLT16_HA:
16573 case R_PPC64_PLT16_HI:
16574 case R_PPC64_PLT16_LO:
16575 case R_PPC64_PLT16_LO_DS:
16576 case R_PPC64_PLT_PCREL34:
16577 case R_PPC64_PLT_PCREL34_NOTOC:
16578 case R_PPC64_PLT32:
16579 case R_PPC64_PLT64:
16580 case R_PPC64_PLTSEQ:
16581 case R_PPC64_PLTSEQ_NOTOC:
16582 case R_PPC64_PLTCALL:
16583 case R_PPC64_PLTCALL_NOTOC:
16584 /* Relocation is to the entry for this symbol in the
16585 procedure linkage table. */
16586 unresolved_reloc = true;
16588 struct plt_entry **plt_list = NULL;
16589 if (h != NULL)
16590 plt_list = &h->elf.plt.plist;
16591 else if (local_got_ents != NULL)
16593 struct plt_entry **local_plt = (struct plt_entry **)
16594 (local_got_ents + symtab_hdr->sh_info);
16595 plt_list = local_plt + r_symndx;
16597 if (plt_list)
16599 struct plt_entry *ent;
16601 for (ent = *plt_list; ent != NULL; ent = ent->next)
16602 if (ent->plt.offset != (bfd_vma) -1
16603 && ent->addend == orig_rel.r_addend)
16605 asection *plt;
16606 bfd_vma got;
16608 plt = htab->elf.splt;
16609 if (use_local_plt (info, elf_hash_entry (h)))
16611 if (h != NULL
16612 ? h->elf.type == STT_GNU_IFUNC
16613 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16614 plt = htab->elf.iplt;
16615 else
16616 plt = htab->pltlocal;
16618 relocation = (plt->output_section->vma
16619 + plt->output_offset
16620 + ent->plt.offset);
16621 if (r_type == R_PPC64_PLT16_HA
16622 || r_type == R_PPC64_PLT16_HI
16623 || r_type == R_PPC64_PLT16_LO
16624 || r_type == R_PPC64_PLT16_LO_DS)
16626 got = (elf_gp (output_bfd)
16627 + htab->sec_info[input_section->id].toc_off);
16628 relocation -= got;
16630 addend = 0;
16631 unresolved_reloc = false;
16632 break;
16636 break;
16638 case R_PPC64_TOC:
16639 /* Relocation value is TOC base. */
16640 relocation = TOCstart;
16641 if (r_symndx == STN_UNDEF)
16642 relocation += htab->sec_info[input_section->id].toc_off;
16643 else if (unresolved_reloc)
16645 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16646 relocation += htab->sec_info[sec->id].toc_off;
16647 else
16648 unresolved_reloc = true;
16649 goto dodyn;
16651 /* TOC16 relocs. We want the offset relative to the TOC base,
16652 which is the address of the start of the TOC plus 0x8000.
16653 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16654 in this order. */
16655 case R_PPC64_TOC16:
16656 case R_PPC64_TOC16_LO:
16657 case R_PPC64_TOC16_HI:
16658 case R_PPC64_TOC16_DS:
16659 case R_PPC64_TOC16_LO_DS:
16660 case R_PPC64_TOC16_HA:
16661 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
16662 if (h != NULL)
16663 goto dodyn;
16664 break;
16666 /* Relocate against the beginning of the section. */
16667 case R_PPC64_SECTOFF:
16668 case R_PPC64_SECTOFF_LO:
16669 case R_PPC64_SECTOFF_HI:
16670 case R_PPC64_SECTOFF_DS:
16671 case R_PPC64_SECTOFF_LO_DS:
16672 case R_PPC64_SECTOFF_HA:
16673 if (sec != NULL)
16674 addend -= sec->output_section->vma;
16675 break;
16677 case R_PPC64_REL16:
16678 case R_PPC64_REL16_LO:
16679 case R_PPC64_REL16_HI:
16680 case R_PPC64_REL16_HA:
16681 case R_PPC64_REL16_HIGH:
16682 case R_PPC64_REL16_HIGHA:
16683 case R_PPC64_REL16_HIGHER:
16684 case R_PPC64_REL16_HIGHERA:
16685 case R_PPC64_REL16_HIGHEST:
16686 case R_PPC64_REL16_HIGHESTA:
16687 case R_PPC64_REL16_HIGHER34:
16688 case R_PPC64_REL16_HIGHERA34:
16689 case R_PPC64_REL16_HIGHEST34:
16690 case R_PPC64_REL16_HIGHESTA34:
16691 case R_PPC64_REL16DX_HA:
16692 case R_PPC64_REL14:
16693 case R_PPC64_REL14_BRNTAKEN:
16694 case R_PPC64_REL14_BRTAKEN:
16695 case R_PPC64_REL24:
16696 case R_PPC64_REL24_NOTOC:
16697 case R_PPC64_PCREL34:
16698 case R_PPC64_PCREL28:
16699 break;
16701 case R_PPC64_TPREL16:
16702 case R_PPC64_TPREL16_LO:
16703 case R_PPC64_TPREL16_HI:
16704 case R_PPC64_TPREL16_HA:
16705 case R_PPC64_TPREL16_DS:
16706 case R_PPC64_TPREL16_LO_DS:
16707 case R_PPC64_TPREL16_HIGH:
16708 case R_PPC64_TPREL16_HIGHA:
16709 case R_PPC64_TPREL16_HIGHER:
16710 case R_PPC64_TPREL16_HIGHERA:
16711 case R_PPC64_TPREL16_HIGHEST:
16712 case R_PPC64_TPREL16_HIGHESTA:
16713 if (h != NULL
16714 && h->elf.root.type == bfd_link_hash_undefweak
16715 && h->elf.dynindx == -1
16716 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
16718 /* Make this relocation against an undefined weak symbol
16719 resolve to zero. This is really just a tweak, since
16720 code using weak externs ought to check that they are
16721 defined before using them. */
16722 bfd_byte *p = contents + rel->r_offset - d_offset;
16724 insn = bfd_get_32 (input_bfd, p);
16725 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16726 if (insn != 0)
16727 bfd_put_32 (input_bfd, insn, p);
16728 break;
16730 /* Fall through. */
16732 case R_PPC64_TPREL34:
16733 if (htab->elf.tls_sec != NULL)
16734 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16735 /* The TPREL16 relocs shouldn't really be used in shared
16736 libs or with non-local symbols as that will result in
16737 DT_TEXTREL being set, but support them anyway. */
16738 goto dodyn;
16740 case R_PPC64_DTPREL16:
16741 case R_PPC64_DTPREL16_LO:
16742 case R_PPC64_DTPREL16_HI:
16743 case R_PPC64_DTPREL16_HA:
16744 case R_PPC64_DTPREL16_DS:
16745 case R_PPC64_DTPREL16_LO_DS:
16746 case R_PPC64_DTPREL16_HIGH:
16747 case R_PPC64_DTPREL16_HIGHA:
16748 case R_PPC64_DTPREL16_HIGHER:
16749 case R_PPC64_DTPREL16_HIGHERA:
16750 case R_PPC64_DTPREL16_HIGHEST:
16751 case R_PPC64_DTPREL16_HIGHESTA:
16752 case R_PPC64_DTPREL34:
16753 if (htab->elf.tls_sec != NULL)
16754 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16755 break;
16757 case R_PPC64_ADDR64_LOCAL:
16758 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16759 ? h->elf.other
16760 : sym->st_other);
16761 break;
16763 case R_PPC64_DTPMOD64:
16764 relocation = 1;
16765 addend = 0;
16766 goto dodyn;
16768 case R_PPC64_TPREL64:
16769 if (htab->elf.tls_sec != NULL)
16770 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16771 goto dodyn;
16773 case R_PPC64_DTPREL64:
16774 if (htab->elf.tls_sec != NULL)
16775 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16776 /* Fall through. */
16778 /* Relocations that may need to be propagated if this is a
16779 dynamic object. */
16780 case R_PPC64_REL30:
16781 case R_PPC64_REL32:
16782 case R_PPC64_REL64:
16783 case R_PPC64_ADDR14:
16784 case R_PPC64_ADDR14_BRNTAKEN:
16785 case R_PPC64_ADDR14_BRTAKEN:
16786 case R_PPC64_ADDR16:
16787 case R_PPC64_ADDR16_DS:
16788 case R_PPC64_ADDR16_HA:
16789 case R_PPC64_ADDR16_HI:
16790 case R_PPC64_ADDR16_HIGH:
16791 case R_PPC64_ADDR16_HIGHA:
16792 case R_PPC64_ADDR16_HIGHER:
16793 case R_PPC64_ADDR16_HIGHERA:
16794 case R_PPC64_ADDR16_HIGHEST:
16795 case R_PPC64_ADDR16_HIGHESTA:
16796 case R_PPC64_ADDR16_LO:
16797 case R_PPC64_ADDR16_LO_DS:
16798 case R_PPC64_ADDR16_HIGHER34:
16799 case R_PPC64_ADDR16_HIGHERA34:
16800 case R_PPC64_ADDR16_HIGHEST34:
16801 case R_PPC64_ADDR16_HIGHESTA34:
16802 case R_PPC64_ADDR24:
16803 case R_PPC64_ADDR32:
16804 case R_PPC64_ADDR64:
16805 case R_PPC64_UADDR16:
16806 case R_PPC64_UADDR32:
16807 case R_PPC64_UADDR64:
16808 case R_PPC64_D34:
16809 case R_PPC64_D34_LO:
16810 case R_PPC64_D34_HI30:
16811 case R_PPC64_D34_HA30:
16812 case R_PPC64_D28:
16813 dodyn:
16814 if ((input_section->flags & SEC_ALLOC) == 0)
16815 break;
16817 if (NO_OPD_RELOCS && is_opd)
16818 break;
16820 if (bfd_link_pic (info)
16821 ? ((h == NULL
16822 || h->elf.dyn_relocs != NULL)
16823 && ((h != NULL && pc_dynrelocs (h))
16824 || must_be_dyn_reloc (info, r_type)))
16825 : (h != NULL
16826 ? h->elf.dyn_relocs != NULL
16827 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16829 bool skip, relocate;
16830 asection *sreloc;
16831 bfd_vma out_off;
16832 long indx = 0;
16834 /* When generating a dynamic object, these relocations
16835 are copied into the output file to be resolved at run
16836 time. */
16838 skip = false;
16839 relocate = false;
16841 out_off = _bfd_elf_section_offset (output_bfd, info,
16842 input_section, rel->r_offset);
16843 if (out_off == (bfd_vma) -1)
16844 skip = true;
16845 else if (out_off == (bfd_vma) -2)
16846 skip = true, relocate = true;
16847 out_off += (input_section->output_section->vma
16848 + input_section->output_offset);
16849 outrel.r_offset = out_off;
16850 outrel.r_addend = rel->r_addend;
16852 /* Optimize unaligned reloc use. */
16853 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16854 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16855 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16856 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16857 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16858 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16859 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16860 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16861 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16863 if (skip)
16864 memset (&outrel, 0, sizeof outrel);
16865 else if (h != NULL
16866 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16867 && !is_opd
16868 && r_type != R_PPC64_TOC)
16870 indx = h->elf.dynindx;
16871 BFD_ASSERT (indx != -1);
16872 outrel.r_info = ELF64_R_INFO (indx, r_type);
16874 else
16876 /* This symbol is local, or marked to become local,
16877 or this is an opd section reloc which must point
16878 at a local function. */
16879 outrel.r_addend += relocation;
16880 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16882 if (is_opd && h != NULL)
16884 /* Lie about opd entries. This case occurs
16885 when building shared libraries and we
16886 reference a function in another shared
16887 lib. The same thing happens for a weak
16888 definition in an application that's
16889 overridden by a strong definition in a
16890 shared lib. (I believe this is a generic
16891 bug in binutils handling of weak syms.)
16892 In these cases we won't use the opd
16893 entry in this lib. */
16894 unresolved_reloc = false;
16896 if (!is_opd
16897 && r_type == R_PPC64_ADDR64
16898 && (h != NULL
16899 ? h->elf.type == STT_GNU_IFUNC
16900 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16901 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16902 else
16904 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16906 /* We need to relocate .opd contents for ld.so.
16907 Prelink also wants simple and consistent rules
16908 for relocs. This make all RELATIVE relocs have
16909 *r_offset equal to r_addend. */
16910 relocate = true;
16913 else
16915 if (h != NULL
16916 ? h->elf.type == STT_GNU_IFUNC
16917 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16919 info->callbacks->einfo
16920 /* xgettext:c-format */
16921 (_("%H: %s for indirect "
16922 "function `%pT' unsupported\n"),
16923 input_bfd, input_section, rel->r_offset,
16924 ppc64_elf_howto_table[r_type]->name,
16925 sym_name);
16926 ret = false;
16928 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16930 else if (sec == NULL || sec->owner == NULL)
16932 bfd_set_error (bfd_error_bad_value);
16933 return false;
16935 else
16937 asection *osec = sec->output_section;
16939 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16941 /* TLS symbol values are relative to the
16942 TLS segment. Dynamic relocations for
16943 local TLS symbols therefore can't be
16944 reduced to a relocation against their
16945 section symbol because it holds the
16946 address of the section, not a value
16947 relative to the TLS segment. We could
16948 change the .tdata dynamic section symbol
16949 to be zero value but STN_UNDEF works
16950 and is used elsewhere, eg. for TPREL64
16951 GOT relocs against local TLS symbols. */
16952 osec = htab->elf.tls_sec;
16953 indx = 0;
16955 else
16957 indx = elf_section_data (osec)->dynindx;
16958 if (indx == 0)
16960 if ((osec->flags & SEC_READONLY) == 0
16961 && htab->elf.data_index_section != NULL)
16962 osec = htab->elf.data_index_section;
16963 else
16964 osec = htab->elf.text_index_section;
16965 indx = elf_section_data (osec)->dynindx;
16967 BFD_ASSERT (indx != 0);
16970 /* We are turning this relocation into one
16971 against a section symbol, so subtract out
16972 the output section's address but not the
16973 offset of the input section in the output
16974 section. */
16975 outrel.r_addend -= osec->vma;
16978 outrel.r_info = ELF64_R_INFO (indx, r_type);
16982 sreloc = elf_section_data (input_section)->sreloc;
16983 if (h != NULL
16984 ? h->elf.type == STT_GNU_IFUNC
16985 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16987 sreloc = htab->elf.irelplt;
16988 if (indx == 0 || is_static_defined (&h->elf))
16989 htab->elf.ifunc_resolvers = true;
16991 if (sreloc == NULL)
16992 abort ();
16994 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16995 >= sreloc->size)
16996 abort ();
16997 loc = sreloc->contents;
16998 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16999 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17001 if (!warned_dynamic
17002 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17004 info->callbacks->einfo
17005 /* xgettext:c-format */
17006 (_("%X%P: %pB: %s against %pT "
17007 "is not supported by glibc as a dynamic relocation\n"),
17008 input_bfd,
17009 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17010 sym_name);
17011 warned_dynamic = true;
17014 /* If this reloc is against an external symbol, it will
17015 be computed at runtime, so there's no need to do
17016 anything now. However, for the sake of prelink ensure
17017 that the section contents are a known value. */
17018 if (!relocate)
17020 unresolved_reloc = false;
17021 /* The value chosen here is quite arbitrary as ld.so
17022 ignores section contents except for the special
17023 case of .opd where the contents might be accessed
17024 before relocation. Choose zero, as that won't
17025 cause reloc overflow. */
17026 relocation = 0;
17027 addend = 0;
17028 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17029 to improve backward compatibility with older
17030 versions of ld. */
17031 if (r_type == R_PPC64_ADDR64)
17032 addend = outrel.r_addend;
17033 /* Adjust pc_relative relocs to have zero in *r_offset. */
17034 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17035 addend = outrel.r_offset;
17038 break;
17040 case R_PPC64_COPY:
17041 case R_PPC64_GLOB_DAT:
17042 case R_PPC64_JMP_SLOT:
17043 case R_PPC64_JMP_IREL:
17044 case R_PPC64_RELATIVE:
17045 /* We shouldn't ever see these dynamic relocs in relocatable
17046 files. */
17047 /* Fall through. */
17049 case R_PPC64_PLTGOT16:
17050 case R_PPC64_PLTGOT16_DS:
17051 case R_PPC64_PLTGOT16_HA:
17052 case R_PPC64_PLTGOT16_HI:
17053 case R_PPC64_PLTGOT16_LO:
17054 case R_PPC64_PLTGOT16_LO_DS:
17055 case R_PPC64_PLTREL32:
17056 case R_PPC64_PLTREL64:
17057 /* These ones haven't been implemented yet. */
17059 info->callbacks->einfo
17060 /* xgettext:c-format */
17061 (_("%P: %pB: %s is not supported for `%pT'\n"),
17062 input_bfd,
17063 ppc64_elf_howto_table[r_type]->name, sym_name);
17065 bfd_set_error (bfd_error_invalid_operation);
17066 ret = false;
17067 goto copy_reloc;
17070 /* Multi-instruction sequences that access the TOC can be
17071 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17072 to nop; addi rb,r2,x; */
17073 switch (r_type)
17075 default:
17076 break;
17078 case R_PPC64_GOT_TLSLD16_HI:
17079 case R_PPC64_GOT_TLSGD16_HI:
17080 case R_PPC64_GOT_TPREL16_HI:
17081 case R_PPC64_GOT_DTPREL16_HI:
17082 case R_PPC64_GOT16_HI:
17083 case R_PPC64_TOC16_HI:
17084 /* These relocs would only be useful if building up an
17085 offset to later add to r2, perhaps in an indexed
17086 addressing mode instruction. Don't try to optimize.
17087 Unfortunately, the possibility of someone building up an
17088 offset like this or even with the HA relocs, means that
17089 we need to check the high insn when optimizing the low
17090 insn. */
17091 break;
17093 case R_PPC64_PLTCALL_NOTOC:
17094 if (!unresolved_reloc)
17095 htab->notoc_plt = 1;
17096 /* Fall through. */
17097 case R_PPC64_PLTCALL:
17098 if (unresolved_reloc
17099 && offset_in_range (input_section, rel->r_offset,
17100 r_type == R_PPC64_PLTCALL ? 8 : 4))
17102 /* No plt entry. Make this into a direct call. */
17103 bfd_byte *p = contents + rel->r_offset;
17104 insn = bfd_get_32 (input_bfd, p);
17105 insn &= 1;
17106 bfd_put_32 (input_bfd, B_DOT | insn, p);
17107 if (r_type == R_PPC64_PLTCALL)
17108 bfd_put_32 (input_bfd, NOP, p + 4);
17109 unresolved_reloc = save_unresolved_reloc;
17110 r_type = R_PPC64_REL24;
17112 break;
17114 case R_PPC64_PLTSEQ_NOTOC:
17115 case R_PPC64_PLTSEQ:
17116 if (unresolved_reloc)
17118 unresolved_reloc = false;
17119 goto nop_it;
17121 break;
17123 case R_PPC64_PLT_PCREL34_NOTOC:
17124 if (!unresolved_reloc)
17125 htab->notoc_plt = 1;
17126 /* Fall through. */
17127 case R_PPC64_PLT_PCREL34:
17128 if (unresolved_reloc
17129 && offset_in_range (input_section, rel->r_offset, 8))
17131 bfd_byte *p = contents + rel->r_offset;
17132 bfd_put_32 (input_bfd, PNOP >> 32, p);
17133 bfd_put_32 (input_bfd, PNOP, p + 4);
17134 unresolved_reloc = false;
17135 goto copy_reloc;
17137 break;
17139 case R_PPC64_PLT16_HA:
17140 if (unresolved_reloc)
17142 unresolved_reloc = false;
17143 goto nop_it;
17145 /* Fall through. */
17146 case R_PPC64_GOT_TLSLD16_HA:
17147 case R_PPC64_GOT_TLSGD16_HA:
17148 case R_PPC64_GOT_TPREL16_HA:
17149 case R_PPC64_GOT_DTPREL16_HA:
17150 case R_PPC64_GOT16_HA:
17151 case R_PPC64_TOC16_HA:
17152 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17153 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
17155 bfd_byte *p;
17156 nop_it:
17157 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17159 p = contents + (rel->r_offset & ~3);
17160 bfd_put_32 (input_bfd, NOP, p);
17161 goto copy_reloc;
17164 break;
17166 case R_PPC64_PLT16_LO:
17167 case R_PPC64_PLT16_LO_DS:
17168 if (unresolved_reloc)
17170 unresolved_reloc = false;
17171 goto nop_it;
17173 /* Fall through. */
17174 case R_PPC64_GOT_TLSLD16_LO:
17175 case R_PPC64_GOT_TLSGD16_LO:
17176 case R_PPC64_GOT_TPREL16_LO_DS:
17177 case R_PPC64_GOT_DTPREL16_LO_DS:
17178 case R_PPC64_GOT16_LO:
17179 case R_PPC64_GOT16_LO_DS:
17180 case R_PPC64_TOC16_LO:
17181 case R_PPC64_TOC16_LO_DS:
17182 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17183 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17184 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17186 bfd_byte *p = contents + (rel->r_offset & ~3);
17187 insn = bfd_get_32 (input_bfd, p);
17188 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17190 /* Transform addic to addi when we change reg. */
17191 insn &= ~((0x3fu << 26) | (0x1f << 16));
17192 insn |= (14u << 26) | (2 << 16);
17194 else
17196 insn &= ~(0x1f << 16);
17197 insn |= 2 << 16;
17199 bfd_put_32 (input_bfd, insn, p);
17201 break;
17203 case R_PPC64_TPREL16_HA:
17204 if (htab->do_tls_opt
17205 && relocation + addend + 0x8000 < 0x10000
17206 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17208 bfd_byte *p = contents + (rel->r_offset & ~3);
17209 bfd_put_32 (input_bfd, NOP, p);
17210 goto copy_reloc;
17212 break;
17214 case R_PPC64_TPREL16_LO:
17215 case R_PPC64_TPREL16_LO_DS:
17216 if (htab->do_tls_opt
17217 && relocation + addend + 0x8000 < 0x10000
17218 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17220 bfd_byte *p = contents + (rel->r_offset & ~3);
17221 insn = bfd_get_32 (input_bfd, p);
17222 insn &= ~(0x1f << 16);
17223 insn |= 13 << 16;
17224 bfd_put_32 (input_bfd, insn, p);
17226 break;
17229 /* Do any further special processing. */
17230 switch (r_type)
17232 default:
17233 break;
17235 case R_PPC64_REL16_HA:
17236 case R_PPC64_REL16_HIGHA:
17237 case R_PPC64_REL16_HIGHERA:
17238 case R_PPC64_REL16_HIGHESTA:
17239 case R_PPC64_REL16DX_HA:
17240 case R_PPC64_ADDR16_HA:
17241 case R_PPC64_ADDR16_HIGHA:
17242 case R_PPC64_ADDR16_HIGHERA:
17243 case R_PPC64_ADDR16_HIGHESTA:
17244 case R_PPC64_TOC16_HA:
17245 case R_PPC64_SECTOFF_HA:
17246 case R_PPC64_TPREL16_HA:
17247 case R_PPC64_TPREL16_HIGHA:
17248 case R_PPC64_TPREL16_HIGHERA:
17249 case R_PPC64_TPREL16_HIGHESTA:
17250 case R_PPC64_DTPREL16_HA:
17251 case R_PPC64_DTPREL16_HIGHA:
17252 case R_PPC64_DTPREL16_HIGHERA:
17253 case R_PPC64_DTPREL16_HIGHESTA:
17254 /* It's just possible that this symbol is a weak symbol
17255 that's not actually defined anywhere. In that case,
17256 'sec' would be NULL, and we should leave the symbol
17257 alone (it will be set to zero elsewhere in the link). */
17258 if (sec == NULL)
17259 break;
17260 /* Fall through. */
17262 case R_PPC64_GOT16_HA:
17263 case R_PPC64_PLTGOT16_HA:
17264 case R_PPC64_PLT16_HA:
17265 case R_PPC64_GOT_TLSGD16_HA:
17266 case R_PPC64_GOT_TLSLD16_HA:
17267 case R_PPC64_GOT_TPREL16_HA:
17268 case R_PPC64_GOT_DTPREL16_HA:
17269 /* Add 0x10000 if sign bit in 0:15 is set.
17270 Bits 0:15 are not used. */
17271 addend += 0x8000;
17272 break;
17274 case R_PPC64_D34_HA30:
17275 case R_PPC64_ADDR16_HIGHERA34:
17276 case R_PPC64_ADDR16_HIGHESTA34:
17277 case R_PPC64_REL16_HIGHERA34:
17278 case R_PPC64_REL16_HIGHESTA34:
17279 if (sec != NULL)
17280 addend += 1ULL << 33;
17281 break;
17283 case R_PPC64_ADDR16_DS:
17284 case R_PPC64_ADDR16_LO_DS:
17285 case R_PPC64_GOT16_DS:
17286 case R_PPC64_GOT16_LO_DS:
17287 case R_PPC64_PLT16_LO_DS:
17288 case R_PPC64_SECTOFF_DS:
17289 case R_PPC64_SECTOFF_LO_DS:
17290 case R_PPC64_TOC16_DS:
17291 case R_PPC64_TOC16_LO_DS:
17292 case R_PPC64_PLTGOT16_DS:
17293 case R_PPC64_PLTGOT16_LO_DS:
17294 case R_PPC64_GOT_TPREL16_DS:
17295 case R_PPC64_GOT_TPREL16_LO_DS:
17296 case R_PPC64_GOT_DTPREL16_DS:
17297 case R_PPC64_GOT_DTPREL16_LO_DS:
17298 case R_PPC64_TPREL16_DS:
17299 case R_PPC64_TPREL16_LO_DS:
17300 case R_PPC64_DTPREL16_DS:
17301 case R_PPC64_DTPREL16_LO_DS:
17302 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17303 break;
17304 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17305 mask = 3;
17306 /* If this reloc is against an lq, lxv, or stxv insn, then
17307 the value must be a multiple of 16. This is somewhat of
17308 a hack, but the "correct" way to do this by defining _DQ
17309 forms of all the _DS relocs bloats all reloc switches in
17310 this file. It doesn't make much sense to use these
17311 relocs in data, so testing the insn should be safe. */
17312 if ((insn & (0x3fu << 26)) == (56u << 26)
17313 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17314 mask = 15;
17315 relocation += addend;
17316 addend = insn & (mask ^ 3);
17317 if ((relocation & mask) != 0)
17319 relocation ^= relocation & mask;
17320 info->callbacks->einfo
17321 /* xgettext:c-format */
17322 (_("%H: error: %s not a multiple of %u\n"),
17323 input_bfd, input_section, rel->r_offset,
17324 ppc64_elf_howto_table[r_type]->name,
17325 mask + 1);
17326 bfd_set_error (bfd_error_bad_value);
17327 ret = false;
17328 goto copy_reloc;
17330 break;
17333 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17334 because such sections are not SEC_ALLOC and thus ld.so will
17335 not process them. */
17336 howto = ppc64_elf_howto_table[(int) r_type];
17337 if (unresolved_reloc
17338 && !((input_section->flags & SEC_DEBUGGING) != 0
17339 && h->elf.def_dynamic)
17340 && _bfd_elf_section_offset (output_bfd, info, input_section,
17341 rel->r_offset) != (bfd_vma) -1)
17343 info->callbacks->einfo
17344 /* xgettext:c-format */
17345 (_("%H: unresolvable %s against `%pT'\n"),
17346 input_bfd, input_section, rel->r_offset,
17347 howto->name,
17348 h->elf.root.root.string);
17349 ret = false;
17352 /* 16-bit fields in insns mostly have signed values, but a
17353 few insns have 16-bit unsigned values. Really, we should
17354 have different reloc types. */
17355 if (howto->complain_on_overflow != complain_overflow_dont
17356 && howto->dst_mask == 0xffff
17357 && (input_section->flags & SEC_CODE) != 0
17358 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17360 enum complain_overflow complain = complain_overflow_signed;
17362 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17363 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17364 complain = complain_overflow_bitfield;
17365 else if (howto->rightshift == 0
17366 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17367 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17368 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17369 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17370 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17371 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17372 complain = complain_overflow_unsigned;
17373 if (howto->complain_on_overflow != complain)
17375 alt_howto = *howto;
17376 alt_howto.complain_on_overflow = complain;
17377 howto = &alt_howto;
17381 switch (r_type)
17383 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17384 case R_PPC64_D34:
17385 case R_PPC64_D34_LO:
17386 case R_PPC64_D34_HI30:
17387 case R_PPC64_D34_HA30:
17388 case R_PPC64_PCREL34:
17389 case R_PPC64_GOT_PCREL34:
17390 case R_PPC64_TPREL34:
17391 case R_PPC64_DTPREL34:
17392 case R_PPC64_GOT_TLSGD_PCREL34:
17393 case R_PPC64_GOT_TLSLD_PCREL34:
17394 case R_PPC64_GOT_TPREL_PCREL34:
17395 case R_PPC64_GOT_DTPREL_PCREL34:
17396 case R_PPC64_PLT_PCREL34:
17397 case R_PPC64_PLT_PCREL34_NOTOC:
17398 case R_PPC64_D28:
17399 case R_PPC64_PCREL28:
17400 if (!offset_in_range (input_section, rel->r_offset, 8))
17401 r = bfd_reloc_outofrange;
17402 else
17404 relocation += addend;
17405 if (howto->pc_relative)
17406 relocation -= (rel->r_offset
17407 + input_section->output_offset
17408 + input_section->output_section->vma);
17409 relocation >>= howto->rightshift;
17411 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17412 pinsn <<= 32;
17413 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17415 pinsn &= ~howto->dst_mask;
17416 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17417 & howto->dst_mask);
17418 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17419 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17420 r = bfd_reloc_ok;
17421 if (howto->complain_on_overflow == complain_overflow_signed
17422 && (relocation + (1ULL << (howto->bitsize - 1))
17423 >= 1ULL << howto->bitsize))
17424 r = bfd_reloc_overflow;
17426 break;
17428 case R_PPC64_REL16DX_HA:
17429 if (!offset_in_range (input_section, rel->r_offset, 4))
17430 r = bfd_reloc_outofrange;
17431 else
17433 relocation += addend;
17434 relocation -= (rel->r_offset
17435 + input_section->output_offset
17436 + input_section->output_section->vma);
17437 relocation = (bfd_signed_vma) relocation >> 16;
17438 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17439 insn &= ~0x1fffc1;
17440 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17441 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17442 r = bfd_reloc_ok;
17443 if (relocation + 0x8000 > 0xffff)
17444 r = bfd_reloc_overflow;
17446 break;
17448 default:
17449 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17450 contents, rel->r_offset,
17451 relocation, addend);
17454 if (r != bfd_reloc_ok)
17456 char *more_info = NULL;
17457 const char *reloc_name = howto->name;
17459 if (reloc_dest != DEST_NORMAL)
17461 more_info = bfd_malloc (strlen (reloc_name) + 8);
17462 if (more_info != NULL)
17464 strcpy (more_info, reloc_name);
17465 strcat (more_info, (reloc_dest == DEST_OPD
17466 ? " (OPD)" : " (stub)"));
17467 reloc_name = more_info;
17471 if (r == bfd_reloc_overflow)
17473 /* On code like "if (foo) foo();" don't report overflow
17474 on a branch to zero when foo is undefined. */
17475 if (!warned
17476 && (reloc_dest == DEST_STUB
17477 || !(h != NULL
17478 && (h->elf.root.type == bfd_link_hash_undefweak
17479 || h->elf.root.type == bfd_link_hash_undefined)
17480 && is_branch_reloc (r_type))))
17481 info->callbacks->reloc_overflow
17482 (info, (struct bfd_link_hash_entry *) h, sym_name,
17483 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17484 rel->r_offset);
17486 else
17488 info->callbacks->einfo
17489 /* xgettext:c-format */
17490 (_("%H: %s against `%pT': error %d\n"),
17491 input_bfd, input_section, rel->r_offset,
17492 reloc_name, sym_name, (int) r);
17493 ret = false;
17495 free (more_info);
17497 copy_reloc:
17498 if (wrel != rel)
17499 *wrel = *rel;
17502 if (wrel != rel)
17504 Elf_Internal_Shdr *rel_hdr;
17505 size_t deleted = rel - wrel;
17507 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17508 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17509 if (rel_hdr->sh_size == 0)
17511 /* It is too late to remove an empty reloc section. Leave
17512 one NONE reloc.
17513 ??? What is wrong with an empty section??? */
17514 rel_hdr->sh_size = rel_hdr->sh_entsize;
17515 deleted -= 1;
17517 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17518 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17519 input_section->reloc_count -= deleted;
17522 /* If we're emitting relocations, then shortly after this function
17523 returns, reloc offsets and addends for this section will be
17524 adjusted. Worse, reloc symbol indices will be for the output
17525 file rather than the input. Save a copy of the relocs for
17526 opd_entry_value. */
17527 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
17529 bfd_size_type amt;
17530 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17531 rel = bfd_alloc (input_bfd, amt);
17532 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17533 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
17534 if (rel == NULL)
17535 return false;
17536 memcpy (rel, relocs, amt);
17538 return ret;
17541 /* Adjust the value of any local symbols in opd sections. */
17543 static int
17544 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17545 const char *name ATTRIBUTE_UNUSED,
17546 Elf_Internal_Sym *elfsym,
17547 asection *input_sec,
17548 struct elf_link_hash_entry *h)
17550 struct _opd_sec_data *opd;
17551 long adjust;
17552 bfd_vma value;
17554 if (h != NULL)
17555 return 1;
17557 opd = get_opd_info (input_sec);
17558 if (opd == NULL || opd->adjust == NULL)
17559 return 1;
17561 value = elfsym->st_value - input_sec->output_offset;
17562 if (!bfd_link_relocatable (info))
17563 value -= input_sec->output_section->vma;
17565 adjust = opd->adjust[OPD_NDX (value)];
17566 if (adjust == -1)
17567 return 2;
17569 elfsym->st_value += adjust;
17570 return 1;
17573 /* Finish up dynamic symbol handling. We set the contents of various
17574 dynamic sections here. */
17576 static bool
17577 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17578 struct bfd_link_info *info,
17579 struct elf_link_hash_entry *h,
17580 Elf_Internal_Sym *sym)
17582 struct ppc_link_hash_table *htab;
17583 struct plt_entry *ent;
17585 htab = ppc_hash_table (info);
17586 if (htab == NULL)
17587 return false;
17589 if (!htab->opd_abi && !h->def_regular)
17590 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17591 if (ent->plt.offset != (bfd_vma) -1)
17593 /* Mark the symbol as undefined, rather than as
17594 defined in glink. Leave the value if there were
17595 any relocations where pointer equality matters
17596 (this is a clue for the dynamic linker, to make
17597 function pointer comparisons work between an
17598 application and shared library), otherwise set it
17599 to zero. */
17600 sym->st_shndx = SHN_UNDEF;
17601 if (!h->pointer_equality_needed)
17602 sym->st_value = 0;
17603 else if (!h->ref_regular_nonweak)
17605 /* This breaks function pointer comparisons, but
17606 that is better than breaking tests for a NULL
17607 function pointer. */
17608 sym->st_value = 0;
17610 break;
17613 if (h->needs_copy
17614 && (h->root.type == bfd_link_hash_defined
17615 || h->root.type == bfd_link_hash_defweak)
17616 && (h->root.u.def.section == htab->elf.sdynbss
17617 || h->root.u.def.section == htab->elf.sdynrelro))
17619 /* This symbol needs a copy reloc. Set it up. */
17620 Elf_Internal_Rela rela;
17621 asection *srel;
17622 bfd_byte *loc;
17624 if (h->dynindx == -1)
17625 abort ();
17627 rela.r_offset = defined_sym_val (h);
17628 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17629 rela.r_addend = 0;
17630 if (h->root.u.def.section == htab->elf.sdynrelro)
17631 srel = htab->elf.sreldynrelro;
17632 else
17633 srel = htab->elf.srelbss;
17634 loc = srel->contents;
17635 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
17636 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
17639 return true;
17642 /* Used to decide how to sort relocs in an optimal manner for the
17643 dynamic linker, before writing them out. */
17645 static enum elf_reloc_type_class
17646 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17647 const asection *rel_sec,
17648 const Elf_Internal_Rela *rela)
17650 enum elf_ppc64_reloc_type r_type;
17651 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17653 if (rel_sec == htab->elf.irelplt)
17654 return reloc_class_ifunc;
17656 r_type = ELF64_R_TYPE (rela->r_info);
17657 switch (r_type)
17659 case R_PPC64_RELATIVE:
17660 return reloc_class_relative;
17661 case R_PPC64_JMP_SLOT:
17662 return reloc_class_plt;
17663 case R_PPC64_COPY:
17664 return reloc_class_copy;
17665 default:
17666 return reloc_class_normal;
17670 /* Finish up the dynamic sections. */
17672 static bool
17673 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17674 struct bfd_link_info *info)
17676 struct ppc_link_hash_table *htab;
17677 bfd *dynobj;
17678 asection *sdyn;
17680 htab = ppc_hash_table (info);
17681 if (htab == NULL)
17682 return false;
17684 dynobj = htab->elf.dynobj;
17685 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17687 if (htab->elf.dynamic_sections_created)
17689 Elf64_External_Dyn *dyncon, *dynconend;
17691 if (sdyn == NULL || htab->elf.sgot == NULL)
17692 abort ();
17694 dyncon = (Elf64_External_Dyn *) sdyn->contents;
17695 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
17696 for (; dyncon < dynconend; dyncon++)
17698 Elf_Internal_Dyn dyn;
17699 asection *s;
17701 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17703 switch (dyn.d_tag)
17705 default:
17706 continue;
17708 case DT_PPC64_GLINK:
17709 s = htab->glink;
17710 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17711 /* We stupidly defined DT_PPC64_GLINK to be the start
17712 of glink rather than the first entry point, which is
17713 what ld.so needs, and now have a bigger stub to
17714 support automatic multiple TOCs. */
17715 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
17716 break;
17718 case DT_PPC64_OPD:
17719 s = bfd_get_section_by_name (output_bfd, ".opd");
17720 if (s == NULL)
17721 continue;
17722 dyn.d_un.d_ptr = s->vma;
17723 break;
17725 case DT_PPC64_OPT:
17726 if ((htab->do_multi_toc && htab->multi_toc_needed)
17727 || htab->notoc_plt)
17728 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
17729 if (htab->has_plt_localentry0)
17730 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
17731 break;
17733 case DT_PPC64_OPDSZ:
17734 s = bfd_get_section_by_name (output_bfd, ".opd");
17735 if (s == NULL)
17736 continue;
17737 dyn.d_un.d_val = s->size;
17738 break;
17740 case DT_PLTGOT:
17741 s = htab->elf.splt;
17742 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17743 break;
17745 case DT_JMPREL:
17746 s = htab->elf.srelplt;
17747 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17748 break;
17750 case DT_PLTRELSZ:
17751 dyn.d_un.d_val = htab->elf.srelplt->size;
17752 break;
17754 case DT_TEXTREL:
17755 if (htab->elf.ifunc_resolvers)
17756 info->callbacks->einfo
17757 (_("%P: warning: text relocations and GNU indirect "
17758 "functions may result in a segfault at runtime\n"));
17759 continue;
17762 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
17766 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17767 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
17769 /* Fill in the first entry in the global offset table.
17770 We use it to hold the link-time TOCbase. */
17771 bfd_put_64 (output_bfd,
17772 elf_gp (output_bfd) + TOC_BASE_OFF,
17773 htab->elf.sgot->contents);
17775 /* Set .got entry size. */
17776 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17777 = 8;
17780 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17781 && htab->elf.splt->output_section != bfd_abs_section_ptr)
17783 /* Set .plt entry size. */
17784 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
17785 = PLT_ENTRY_SIZE (htab);
17788 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17789 brlt ourselves if emitrelocations. */
17790 if (htab->brlt != NULL
17791 && htab->brlt->reloc_count != 0
17792 && !_bfd_elf_link_output_relocs (output_bfd,
17793 htab->brlt,
17794 elf_section_data (htab->brlt)->rela.hdr,
17795 elf_section_data (htab->brlt)->relocs,
17796 NULL))
17797 return false;
17799 if (htab->glink != NULL
17800 && htab->glink->reloc_count != 0
17801 && !_bfd_elf_link_output_relocs (output_bfd,
17802 htab->glink,
17803 elf_section_data (htab->glink)->rela.hdr,
17804 elf_section_data (htab->glink)->relocs,
17805 NULL))
17806 return false;
17809 if (htab->glink_eh_frame != NULL
17810 && htab->glink_eh_frame->size != 0
17811 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17812 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17813 htab->glink_eh_frame,
17814 htab->glink_eh_frame->contents))
17815 return false;
17817 /* We need to handle writing out multiple GOT sections ourselves,
17818 since we didn't add them to DYNOBJ. We know dynobj is the first
17819 bfd. */
17820 while ((dynobj = dynobj->link.next) != NULL)
17822 asection *s;
17824 if (!is_ppc64_elf (dynobj))
17825 continue;
17827 s = ppc64_elf_tdata (dynobj)->got;
17828 if (s != NULL
17829 && s->size != 0
17830 && s->output_section != bfd_abs_section_ptr
17831 && !bfd_set_section_contents (output_bfd, s->output_section,
17832 s->contents, s->output_offset,
17833 s->size))
17834 return false;
17835 s = ppc64_elf_tdata (dynobj)->relgot;
17836 if (s != NULL
17837 && s->size != 0
17838 && s->output_section != bfd_abs_section_ptr
17839 && !bfd_set_section_contents (output_bfd, s->output_section,
17840 s->contents, s->output_offset,
17841 s->size))
17842 return false;
17845 return true;
17848 #include "elf64-target.h"
17850 /* FreeBSD support */
17852 #undef TARGET_LITTLE_SYM
17853 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
17854 #undef TARGET_LITTLE_NAME
17855 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
17857 #undef TARGET_BIG_SYM
17858 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
17859 #undef TARGET_BIG_NAME
17860 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17862 #undef ELF_OSABI
17863 #define ELF_OSABI ELFOSABI_FREEBSD
17865 #undef elf64_bed
17866 #define elf64_bed elf64_powerpc_fbsd_bed
17868 #include "elf64-target.h"