gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / bfd / elf64-ppc.c
blob29607bdb9fb27ed12d4bd1d7d63e698370f3070c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
28 /* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
33 #include "sysdep.h"
34 #include <stdarg.h>
35 #include "bfd.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #include "elf-bfd.h"
39 #include "elf/ppc64.h"
40 #include "elf64-ppc.h"
41 #include "dwarf2.h"
43 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44 #define OCTETS_PER_BYTE(ABFD, SEC) 1
46 static bfd_reloc_status_type ppc64_elf_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_toc_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type ppc64_elf_toc64_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_vma opd_entry_value
67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
69 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
70 #define TARGET_LITTLE_NAME "elf64-powerpcle"
71 #define TARGET_BIG_SYM powerpc_elf64_vec
72 #define TARGET_BIG_NAME "elf64-powerpc"
73 #define ELF_ARCH bfd_arch_powerpc
74 #define ELF_TARGET_ID PPC64_ELF_DATA
75 #define ELF_MACHINE_CODE EM_PPC64
76 #define ELF_MAXPAGESIZE 0x10000
77 #define ELF_COMMONPAGESIZE 0x1000
78 #define elf_info_to_howto ppc64_elf_info_to_howto
80 #define elf_backend_want_got_sym 0
81 #define elf_backend_want_plt_sym 0
82 #define elf_backend_plt_alignment 3
83 #define elf_backend_plt_not_loaded 1
84 #define elf_backend_got_header_size 8
85 #define elf_backend_want_dynrelro 1
86 #define elf_backend_can_gc_sections 1
87 #define elf_backend_can_refcount 1
88 #define elf_backend_rela_normal 1
89 #define elf_backend_dtrel_excludes_plt 1
90 #define elf_backend_default_execstack 0
92 #define bfd_elf64_mkobject ppc64_elf_mkobject
93 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
94 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
95 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
96 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
97 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
98 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
99 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
100 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
101 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
103 #define elf_backend_object_p ppc64_elf_object_p
104 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
105 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
106 #define elf_backend_write_core_note ppc64_elf_write_core_note
107 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
108 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
109 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
110 #define elf_backend_check_directives ppc64_elf_before_check_relocs
111 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
112 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
113 #define elf_backend_check_relocs ppc64_elf_check_relocs
114 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
115 #define elf_backend_gc_keep ppc64_elf_gc_keep
116 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
117 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
118 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
119 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
120 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
121 #define elf_backend_always_size_sections ppc64_elf_edit
122 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
123 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
124 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
125 #define elf_backend_action_discarded ppc64_elf_action_discarded
126 #define elf_backend_relocate_section ppc64_elf_relocate_section
127 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
128 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
129 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
130 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
131 #define elf_backend_special_sections ppc64_elf_special_sections
132 #define elf_backend_section_flags ppc64_elf_section_flags
133 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
134 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
135 #define elf_backend_get_reloc_section bfd_get_section_by_name
137 /* The name of the dynamic interpreter. This is put in the .interp
138 section. */
139 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
141 /* The size in bytes of an entry in the procedure linkage table. */
142 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
143 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
145 /* The initial size of the plt reserved for the dynamic linker. */
146 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
148 /* Offsets to some stack save slots. */
149 #define STK_LR 16
150 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
151 /* This one is dodgy. ELFv2 does not have a linker word, so use the
152 CR save slot. Used only by optimised __tls_get_addr call stub,
153 relying on __tls_get_addr_opt not saving CR.. */
154 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
156 /* TOC base pointers offset from start of TOC. */
157 #define TOC_BASE_OFF 0x8000
158 /* TOC base alignment. */
159 #define TOC_BASE_ALIGN 256
161 /* Offset of tp and dtp pointers from start of TLS block. */
162 #define TP_OFFSET 0x7000
163 #define DTP_OFFSET 0x8000
165 /* .plt call stub instructions. The normal stub is like this, but
166 sometimes the .plt entry crosses a 64k boundary and we need to
167 insert an addi to adjust r11. */
168 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
169 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
170 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
171 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
172 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
173 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
174 #define BCTR 0x4e800420 /* bctr */
176 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
177 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
178 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
179 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
180 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
182 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
183 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
184 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
185 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
186 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
187 #define BNECTR 0x4ca20420 /* bnectr+ */
188 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
190 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
191 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
192 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
194 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
195 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
196 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
198 #define LI_R11_0 0x39600000 /* li %r11,0 */
199 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
200 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
201 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
202 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
203 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
204 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
205 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
206 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
207 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
208 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
209 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
210 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
211 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
212 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
213 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
214 #define PADDI_R12_PC 0x0610000039800000ULL
215 #define PLD_R12_PC 0x04100000e5800000ULL
216 #define PNOP 0x0700000000000000ULL
218 /* __glink_PLTresolve stub instructions. We enter with the index in
219 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
220 #define GLINK_PLTRESOLVE_SIZE(htab) \
221 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
222 /* 0: */
223 /* .quad plt0-1f */
224 /* __glink: */
225 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
226 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
227 /* 1: */
228 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
229 /* ld %2,(0b-1b)(%11) */
230 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
231 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
232 /* ld %12,0(%11) */
233 /* ld %2,8(%11) */
234 /* mtctr %12 */
235 /* ld %11,16(%11) */
236 /* bctr */
238 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
239 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
240 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
241 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
242 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
243 #define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
244 #define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
246 /* Pad with this. */
247 #define NOP 0x60000000
249 /* Some other nops. */
250 #define CROR_151515 0x4def7b82
251 #define CROR_313131 0x4ffffb82
253 /* .glink entries for the first 32k functions are two instructions. */
254 #define LI_R0_0 0x38000000 /* li %r0,0 */
255 #define B_DOT 0x48000000 /* b . */
257 /* After that, we need two instructions to load the index, followed by
258 a branch. */
259 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
260 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
262 /* Instructions used by the save and restore reg functions. */
263 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
264 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
265 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
266 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
267 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
268 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
269 #define LI_R12_0 0x39800000 /* li %r12,0 */
270 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
271 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
272 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
273 #define BLR 0x4e800020 /* blr */
275 /* Since .opd is an array of descriptors and each entry will end up
276 with identical R_PPC64_RELATIVE relocs, there is really no need to
277 propagate .opd relocs; The dynamic linker should be taught to
278 relocate .opd without reloc entries. */
279 #ifndef NO_OPD_RELOCS
280 #define NO_OPD_RELOCS 0
281 #endif
283 #ifndef ARRAY_SIZE
284 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
285 #endif
287 static inline int
288 abiversion (bfd *abfd)
290 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
293 static inline void
294 set_abiversion (bfd *abfd, int ver)
296 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
297 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
300 /* Relocation HOWTO's. */
301 /* Like other ELF RELA targets that don't apply multiple
302 field-altering relocations to the same localation, src_mask is
303 always zero and pcrel_offset is the same as pc_relative.
304 PowerPC can always use a zero bitpos, even when the field is not at
305 the LSB. For example, a REL24 could use rightshift=2, bisize=24
306 and bitpos=2 which matches the ABI description, or as we do here,
307 rightshift=0, bitsize=26 and bitpos=0. */
308 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
309 complain, special_func) \
310 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
311 complain_overflow_ ## complain, special_func, \
312 #type, false, 0, mask, pc_relative)
314 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
316 static reloc_howto_type ppc64_elf_howto_raw[] =
318 /* This reloc does nothing. */
319 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
320 bfd_elf_generic_reloc),
322 /* A standard 32 bit relocation. */
323 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
324 bfd_elf_generic_reloc),
326 /* An absolute 26 bit branch; the lower two bits must be zero.
327 FIXME: we don't check that, we just clear them. */
328 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
329 bfd_elf_generic_reloc),
331 /* A standard 16 bit relocation. */
332 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
333 bfd_elf_generic_reloc),
335 /* A 16 bit relocation without overflow. */
336 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
337 bfd_elf_generic_reloc),
339 /* Bits 16-31 of an address. */
340 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
341 bfd_elf_generic_reloc),
343 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
344 bits, treated as a signed number, is negative. */
345 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
346 ppc64_elf_ha_reloc),
348 /* An absolute 16 bit branch; the lower two bits must be zero.
349 FIXME: we don't check that, we just clear them. */
350 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
351 ppc64_elf_branch_reloc),
353 /* An absolute 16 bit branch, for which bit 10 should be set to
354 indicate that the branch is expected to be taken. The lower two
355 bits must be zero. */
356 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
357 ppc64_elf_brtaken_reloc),
359 /* An absolute 16 bit branch, for which bit 10 should be set to
360 indicate that the branch is not expected to be taken. The lower
361 two bits must be zero. */
362 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
363 ppc64_elf_brtaken_reloc),
365 /* A relative 26 bit branch; the lower two bits must be zero. */
366 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
367 ppc64_elf_branch_reloc),
369 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
370 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
371 ppc64_elf_branch_reloc),
373 /* Another variant, when p10 insns can't be used on stubs. */
374 HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
375 ppc64_elf_branch_reloc),
377 /* A relative 16 bit branch; the lower two bits must be zero. */
378 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
379 ppc64_elf_branch_reloc),
381 /* A relative 16 bit branch. Bit 10 should be set to indicate that
382 the branch is expected to be taken. The lower two bits must be
383 zero. */
384 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
385 ppc64_elf_brtaken_reloc),
387 /* A relative 16 bit branch. Bit 10 should be set to indicate that
388 the branch is not expected to be taken. The lower two bits must
389 be zero. */
390 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
391 ppc64_elf_brtaken_reloc),
393 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
394 symbol. */
395 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
396 ppc64_elf_unhandled_reloc),
398 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
399 the symbol. */
400 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
401 ppc64_elf_unhandled_reloc),
403 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
404 the symbol. */
405 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
406 ppc64_elf_unhandled_reloc),
408 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
409 the symbol. */
410 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
411 ppc64_elf_unhandled_reloc),
413 /* This is used only by the dynamic linker. The symbol should exist
414 both in the object being run and in some shared library. The
415 dynamic linker copies the data addressed by the symbol from the
416 shared library into the object, because the object being
417 run has to have the data at some particular address. */
418 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
419 ppc64_elf_unhandled_reloc),
421 /* Like R_PPC64_ADDR64, but used when setting global offset table
422 entries. */
423 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
424 ppc64_elf_unhandled_reloc),
426 /* Created by the link editor. Marks a procedure linkage table
427 entry for a symbol. */
428 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
429 ppc64_elf_unhandled_reloc),
431 /* Used only by the dynamic linker. When the object is run, this
432 doubleword64 is set to the load address of the object, plus the
433 addend. */
434 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
435 bfd_elf_generic_reloc),
437 /* Like R_PPC64_ADDR32, but may be unaligned. */
438 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
439 bfd_elf_generic_reloc),
441 /* Like R_PPC64_ADDR16, but may be unaligned. */
442 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
443 bfd_elf_generic_reloc),
445 /* 32-bit PC relative. */
446 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
447 bfd_elf_generic_reloc),
449 /* 32-bit relocation to the symbol's procedure linkage table. */
450 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
451 ppc64_elf_unhandled_reloc),
453 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
454 FIXME: R_PPC64_PLTREL32 not supported. */
455 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
456 ppc64_elf_unhandled_reloc),
458 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
459 the symbol. */
460 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
461 ppc64_elf_unhandled_reloc),
463 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
464 the symbol. */
465 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
466 ppc64_elf_unhandled_reloc),
468 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
469 the symbol. */
470 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
471 ppc64_elf_unhandled_reloc),
473 /* 16-bit section relative relocation. */
474 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
475 ppc64_elf_sectoff_reloc),
477 /* Like R_PPC64_SECTOFF, but no overflow warning. */
478 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
479 ppc64_elf_sectoff_reloc),
481 /* 16-bit upper half section relative relocation. */
482 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
483 ppc64_elf_sectoff_reloc),
485 /* 16-bit upper half adjusted section relative relocation. */
486 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
487 ppc64_elf_sectoff_ha_reloc),
489 /* Like R_PPC64_REL24 without touching the two least significant bits. */
490 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
491 bfd_elf_generic_reloc),
493 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
495 /* A standard 64-bit relocation. */
496 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
497 bfd_elf_generic_reloc),
499 /* The bits 32-47 of an address. */
500 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
501 bfd_elf_generic_reloc),
503 /* The bits 32-47 of an address, plus 1 if the contents of the low
504 16 bits, treated as a signed number, is negative. */
505 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
506 ppc64_elf_ha_reloc),
508 /* The bits 48-63 of an address. */
509 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
510 bfd_elf_generic_reloc),
512 /* The bits 48-63 of an address, plus 1 if the contents of the low
513 16 bits, treated as a signed number, is negative. */
514 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
515 ppc64_elf_ha_reloc),
517 /* Like ADDR64, but may be unaligned. */
518 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
519 bfd_elf_generic_reloc),
521 /* 64-bit relative relocation. */
522 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
523 bfd_elf_generic_reloc),
525 /* 64-bit relocation to the symbol's procedure linkage table. */
526 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
527 ppc64_elf_unhandled_reloc),
529 /* 64-bit PC relative relocation to the symbol's procedure linkage
530 table. */
531 /* FIXME: R_PPC64_PLTREL64 not supported. */
532 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
533 ppc64_elf_unhandled_reloc),
535 /* 16 bit TOC-relative relocation. */
536 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
537 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
538 ppc64_elf_toc_reloc),
540 /* 16 bit TOC-relative relocation without overflow. */
541 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
542 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
543 ppc64_elf_toc_reloc),
545 /* 16 bit TOC-relative relocation, high 16 bits. */
546 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
547 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
548 ppc64_elf_toc_reloc),
550 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
551 contents of the low 16 bits, treated as a signed number, is
552 negative. */
553 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
554 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
555 ppc64_elf_toc_ha_reloc),
557 /* 64-bit relocation; insert value of TOC base (.TOC.). */
558 /* R_PPC64_TOC 51 doubleword64 .TOC. */
559 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
560 ppc64_elf_toc64_reloc),
562 /* Like R_PPC64_GOT16, but also informs the link editor that the
563 value to relocate may (!) refer to a PLT entry which the link
564 editor (a) may replace with the symbol value. If the link editor
565 is unable to fully resolve the symbol, it may (b) create a PLT
566 entry and store the address to the new PLT entry in the GOT.
567 This permits lazy resolution of function symbols at run time.
568 The link editor may also skip all of this and just (c) emit a
569 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
570 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
571 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
572 ppc64_elf_unhandled_reloc),
574 /* Like R_PPC64_PLTGOT16, but without overflow. */
575 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
576 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
577 ppc64_elf_unhandled_reloc),
579 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
580 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
581 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
582 ppc64_elf_unhandled_reloc),
584 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
585 1 if the contents of the low 16 bits, treated as a signed number,
586 is negative. */
587 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
588 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
589 ppc64_elf_unhandled_reloc),
591 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
592 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
593 bfd_elf_generic_reloc),
595 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
596 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
597 bfd_elf_generic_reloc),
599 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
600 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
601 ppc64_elf_unhandled_reloc),
603 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
604 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
605 ppc64_elf_unhandled_reloc),
607 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
608 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
609 ppc64_elf_unhandled_reloc),
611 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
612 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
613 ppc64_elf_sectoff_reloc),
615 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
616 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
617 ppc64_elf_sectoff_reloc),
619 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
620 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
621 ppc64_elf_toc_reloc),
623 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
624 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
625 ppc64_elf_toc_reloc),
627 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
628 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
629 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
630 ppc64_elf_unhandled_reloc),
632 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
633 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
634 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
635 ppc64_elf_unhandled_reloc),
637 /* Marker relocs for TLS. */
638 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
639 bfd_elf_generic_reloc),
641 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
642 bfd_elf_generic_reloc),
644 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
645 bfd_elf_generic_reloc),
647 /* Marker reloc for optimizing r2 save in prologue rather than on
648 each plt call stub. */
649 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
650 bfd_elf_generic_reloc),
652 /* Marker relocs on inline plt call instructions. */
653 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
654 bfd_elf_generic_reloc),
656 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
657 bfd_elf_generic_reloc),
659 /* Computes the load module index of the load module that contains the
660 definition of its TLS sym. */
661 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
662 ppc64_elf_unhandled_reloc),
664 /* Computes a dtv-relative displacement, the difference between the value
665 of sym+add and the base address of the thread-local storage block that
666 contains the definition of sym, minus 0x8000. */
667 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
668 ppc64_elf_unhandled_reloc),
670 /* A 16 bit dtprel reloc. */
671 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
672 ppc64_elf_unhandled_reloc),
674 /* Like DTPREL16, but no overflow. */
675 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
676 ppc64_elf_unhandled_reloc),
678 /* Like DTPREL16_LO, but next higher group of 16 bits. */
679 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
680 ppc64_elf_unhandled_reloc),
682 /* Like DTPREL16_HI, but adjust for low 16 bits. */
683 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
684 ppc64_elf_unhandled_reloc),
686 /* Like DTPREL16_HI, but next higher group of 16 bits. */
687 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
688 ppc64_elf_unhandled_reloc),
690 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
691 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
692 ppc64_elf_unhandled_reloc),
694 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
695 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
696 ppc64_elf_unhandled_reloc),
698 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
699 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
700 ppc64_elf_unhandled_reloc),
702 /* Like DTPREL16, but for insns with a DS field. */
703 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
704 ppc64_elf_unhandled_reloc),
706 /* Like DTPREL16_DS, but no overflow. */
707 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
708 ppc64_elf_unhandled_reloc),
710 /* Computes a tp-relative displacement, the difference between the value of
711 sym+add and the value of the thread pointer (r13). */
712 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
713 ppc64_elf_unhandled_reloc),
715 /* A 16 bit tprel reloc. */
716 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
717 ppc64_elf_unhandled_reloc),
719 /* Like TPREL16, but no overflow. */
720 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
721 ppc64_elf_unhandled_reloc),
723 /* Like TPREL16_LO, but next higher group of 16 bits. */
724 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
725 ppc64_elf_unhandled_reloc),
727 /* Like TPREL16_HI, but adjust for low 16 bits. */
728 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
729 ppc64_elf_unhandled_reloc),
731 /* Like TPREL16_HI, but next higher group of 16 bits. */
732 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
733 ppc64_elf_unhandled_reloc),
735 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
736 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
737 ppc64_elf_unhandled_reloc),
739 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
740 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
741 ppc64_elf_unhandled_reloc),
743 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
744 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
745 ppc64_elf_unhandled_reloc),
747 /* Like TPREL16, but for insns with a DS field. */
748 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
749 ppc64_elf_unhandled_reloc),
751 /* Like TPREL16_DS, but no overflow. */
752 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
753 ppc64_elf_unhandled_reloc),
755 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
756 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
757 to the first entry relative to the TOC base (r2). */
758 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
759 ppc64_elf_unhandled_reloc),
761 /* Like GOT_TLSGD16, but no overflow. */
762 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
763 ppc64_elf_unhandled_reloc),
765 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
766 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
767 ppc64_elf_unhandled_reloc),
769 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
770 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
771 ppc64_elf_unhandled_reloc),
773 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
774 with values (sym+add)@dtpmod and zero, and computes the offset to the
775 first entry relative to the TOC base (r2). */
776 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
777 ppc64_elf_unhandled_reloc),
779 /* Like GOT_TLSLD16, but no overflow. */
780 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
781 ppc64_elf_unhandled_reloc),
783 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
784 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
785 ppc64_elf_unhandled_reloc),
787 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
788 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
789 ppc64_elf_unhandled_reloc),
791 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
792 the offset to the entry relative to the TOC base (r2). */
793 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
794 ppc64_elf_unhandled_reloc),
796 /* Like GOT_DTPREL16_DS, but no overflow. */
797 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
798 ppc64_elf_unhandled_reloc),
800 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
801 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
802 ppc64_elf_unhandled_reloc),
804 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
805 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
806 ppc64_elf_unhandled_reloc),
808 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
809 offset to the entry relative to the TOC base (r2). */
810 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
811 ppc64_elf_unhandled_reloc),
813 /* Like GOT_TPREL16_DS, but no overflow. */
814 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
815 ppc64_elf_unhandled_reloc),
817 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
818 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
819 ppc64_elf_unhandled_reloc),
821 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
822 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
823 ppc64_elf_unhandled_reloc),
825 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
826 ppc64_elf_unhandled_reloc),
828 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
829 bfd_elf_generic_reloc),
831 /* A 16 bit relative relocation. */
832 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
833 bfd_elf_generic_reloc),
835 /* A 16 bit relative relocation without overflow. */
836 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
837 bfd_elf_generic_reloc),
839 /* The high order 16 bits of a relative address. */
840 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
841 bfd_elf_generic_reloc),
843 /* The high order 16 bits of a relative address, plus 1 if the contents of
844 the low 16 bits, treated as a signed number, is negative. */
845 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
846 ppc64_elf_ha_reloc),
848 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
849 bfd_elf_generic_reloc),
851 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
852 ppc64_elf_ha_reloc),
854 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
855 bfd_elf_generic_reloc),
857 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
858 ppc64_elf_ha_reloc),
860 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
861 bfd_elf_generic_reloc),
863 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
864 ppc64_elf_ha_reloc),
866 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
867 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
868 ppc64_elf_ha_reloc),
870 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
871 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
872 ppc64_elf_ha_reloc),
874 /* Like R_PPC64_ADDR16_HI, but no overflow. */
875 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
876 bfd_elf_generic_reloc),
878 /* Like R_PPC64_ADDR16_HA, but no overflow. */
879 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
880 ppc64_elf_ha_reloc),
882 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
883 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
884 ppc64_elf_unhandled_reloc),
886 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
887 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
888 ppc64_elf_unhandled_reloc),
890 /* Like R_PPC64_TPREL16_HI, but no overflow. */
891 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
892 ppc64_elf_unhandled_reloc),
894 /* Like R_PPC64_TPREL16_HA, but no overflow. */
895 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
896 ppc64_elf_unhandled_reloc),
898 /* Marker reloc on ELFv2 large-model function entry. */
899 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
900 bfd_elf_generic_reloc),
902 /* Like ADDR64, but use local entry point of function. */
903 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
904 bfd_elf_generic_reloc),
906 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
907 bfd_elf_generic_reloc),
909 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
910 bfd_elf_generic_reloc),
912 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
913 bfd_elf_generic_reloc),
915 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
916 ppc64_elf_prefix_reloc),
918 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
919 ppc64_elf_prefix_reloc),
921 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
922 ppc64_elf_prefix_reloc),
924 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
925 ppc64_elf_prefix_reloc),
927 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
928 ppc64_elf_prefix_reloc),
930 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
931 ppc64_elf_unhandled_reloc),
933 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
934 ppc64_elf_unhandled_reloc),
936 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
937 ppc64_elf_unhandled_reloc),
939 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
940 ppc64_elf_unhandled_reloc),
942 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
943 ppc64_elf_unhandled_reloc),
945 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
946 ppc64_elf_unhandled_reloc),
948 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
949 ppc64_elf_unhandled_reloc),
951 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
952 ppc64_elf_unhandled_reloc),
954 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
955 ppc64_elf_unhandled_reloc),
957 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
958 bfd_elf_generic_reloc),
960 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
961 ppc64_elf_ha_reloc),
963 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
964 bfd_elf_generic_reloc),
966 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
967 ppc64_elf_ha_reloc),
969 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
970 bfd_elf_generic_reloc),
972 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
973 ppc64_elf_ha_reloc),
975 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
976 bfd_elf_generic_reloc),
978 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
979 ppc64_elf_ha_reloc),
981 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
982 ppc64_elf_prefix_reloc),
984 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
985 ppc64_elf_prefix_reloc),
987 /* GNU extension to record C++ vtable hierarchy. */
988 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
989 NULL),
991 /* GNU extension to record C++ vtable member usage. */
992 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
993 NULL),
997 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
998 be done. */
1000 static void
1001 ppc_howto_init (void)
1003 unsigned int i, type;
1005 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1007 type = ppc64_elf_howto_raw[i].type;
1008 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1009 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1013 static reloc_howto_type *
1014 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1016 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1018 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1019 /* Initialize howto table if needed. */
1020 ppc_howto_init ();
1022 switch (code)
1024 default:
1025 /* xgettext:c-format */
1026 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1027 (int) code);
1028 bfd_set_error (bfd_error_bad_value);
1029 return NULL;
1031 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1032 break;
1033 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1034 break;
1035 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1036 break;
1037 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1038 break;
1039 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1040 break;
1041 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1042 break;
1043 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1044 break;
1045 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1046 break;
1047 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1048 break;
1049 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1050 break;
1051 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1052 break;
1053 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1054 break;
1055 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1056 break;
1057 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1058 break;
1059 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1060 break;
1061 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1062 break;
1063 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1064 break;
1065 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1066 break;
1067 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1068 break;
1069 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1070 break;
1071 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1072 break;
1073 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1074 break;
1075 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1076 break;
1077 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1078 break;
1079 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1080 break;
1081 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1082 break;
1083 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1084 break;
1085 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1086 break;
1087 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1088 break;
1089 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1090 break;
1091 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1092 break;
1093 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1094 break;
1095 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1096 break;
1097 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1098 break;
1099 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1100 break;
1101 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1102 break;
1103 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1104 break;
1105 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1106 break;
1107 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1108 break;
1109 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1110 break;
1111 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1112 break;
1113 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1114 break;
1115 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1116 break;
1117 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1118 break;
1119 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1120 break;
1121 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1122 break;
1123 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1124 break;
1125 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1126 break;
1127 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1128 break;
1129 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1130 break;
1131 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1132 break;
1133 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1134 break;
1135 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1136 break;
1137 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1138 break;
1139 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1140 break;
1141 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1142 break;
1143 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1144 break;
1145 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1146 break;
1147 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1148 break;
1149 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1150 break;
1151 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1152 break;
1153 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1154 break;
1155 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1156 break;
1157 case BFD_RELOC_PPC64_TLS_PCREL:
1158 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1159 break;
1160 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1161 break;
1162 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1163 break;
1164 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1165 break;
1166 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1167 break;
1168 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1169 break;
1170 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1171 break;
1172 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1173 break;
1174 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1175 break;
1176 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1177 break;
1178 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1179 break;
1180 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1181 break;
1182 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1183 break;
1184 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1185 break;
1186 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1187 break;
1188 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1189 break;
1190 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1191 break;
1192 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1193 break;
1194 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1195 break;
1196 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1197 break;
1198 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1199 break;
1200 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1201 break;
1202 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1203 break;
1204 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1205 break;
1206 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1207 break;
1208 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1209 break;
1210 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1211 break;
1212 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1213 break;
1214 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1215 break;
1216 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1217 break;
1218 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1219 break;
1220 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1221 break;
1222 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1223 break;
1224 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1225 break;
1226 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1227 break;
1228 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1229 break;
1230 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1231 break;
1232 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1233 break;
1234 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1235 break;
1236 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1237 break;
1238 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1239 break;
1240 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1241 break;
1242 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1243 break;
1244 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1245 break;
1246 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1247 break;
1248 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1249 break;
1250 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1251 break;
1252 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1253 break;
1254 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1255 break;
1256 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1257 break;
1258 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1259 break;
1260 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1261 break;
1262 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1263 break;
1264 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1265 break;
1266 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1267 break;
1268 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1269 break;
1270 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1271 break;
1272 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1273 break;
1274 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1275 break;
1276 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1277 break;
1278 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1279 break;
1280 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1281 break;
1282 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1283 break;
1284 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1285 break;
1286 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1287 break;
1288 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1289 break;
1290 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1291 break;
1292 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1293 break;
1294 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1295 break;
1296 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1297 break;
1298 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1299 break;
1300 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1301 break;
1302 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1303 break;
1304 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1305 break;
1306 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1307 break;
1308 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1309 break;
1310 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1311 break;
1312 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1313 break;
1314 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1315 break;
1316 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1317 break;
1318 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1319 break;
1320 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1321 break;
1322 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1323 break;
1324 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1325 break;
1326 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1327 break;
1330 return ppc64_elf_howto_table[r];
1333 static reloc_howto_type *
1334 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1336 unsigned int i;
1337 static char *compat_map[][2] = {
1338 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1339 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1340 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1341 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1344 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1345 if (ppc64_elf_howto_raw[i].name != NULL
1346 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1347 return &ppc64_elf_howto_raw[i];
1349 /* Handle old names of relocations in case they were used by
1350 .reloc directives.
1351 FIXME: Remove this soon. Mapping the reloc names is very likely
1352 completely unnecessary. */
1353 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1354 if (strcasecmp (compat_map[i][0], r_name) == 0)
1356 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1357 compat_map[i][1], compat_map[i][0]);
1358 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1361 return NULL;
1364 /* Set the howto pointer for a PowerPC ELF reloc. */
1366 static bool
1367 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1368 Elf_Internal_Rela *dst)
1370 unsigned int type;
1372 /* Initialize howto table if needed. */
1373 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1374 ppc_howto_init ();
1376 type = ELF64_R_TYPE (dst->r_info);
1377 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1379 /* xgettext:c-format */
1380 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1381 abfd, type);
1382 bfd_set_error (bfd_error_bad_value);
1383 return false;
1385 cache_ptr->howto = ppc64_elf_howto_table[type];
1386 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1388 /* xgettext:c-format */
1389 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1390 abfd, type);
1391 bfd_set_error (bfd_error_bad_value);
1392 return false;
1395 return true;
1398 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1400 static bfd_reloc_status_type
1401 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1402 void *data, asection *input_section,
1403 bfd *output_bfd, char **error_message)
1405 enum elf_ppc64_reloc_type r_type;
1406 long insn;
1407 bfd_size_type octets;
1408 bfd_vma value;
1410 /* If this is a relocatable link (output_bfd test tells us), just
1411 call the generic function. Any adjustment will be done at final
1412 link time. */
1413 if (output_bfd != NULL)
1414 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1415 input_section, output_bfd, error_message);
1417 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1418 We won't actually be using the low bits, so trashing them
1419 doesn't matter. */
1420 r_type = reloc_entry->howto->type;
1421 if (r_type == R_PPC64_ADDR16_HIGHERA34
1422 || r_type == R_PPC64_ADDR16_HIGHESTA34
1423 || r_type == R_PPC64_REL16_HIGHERA34
1424 || r_type == R_PPC64_REL16_HIGHESTA34)
1425 reloc_entry->addend += 1ULL << 33;
1426 else
1427 reloc_entry->addend += 1U << 15;
1428 if (r_type != R_PPC64_REL16DX_HA)
1429 return bfd_reloc_continue;
1431 value = 0;
1432 if (!bfd_is_com_section (symbol->section))
1433 value = symbol->value;
1434 value += (reloc_entry->addend
1435 + symbol->section->output_offset
1436 + symbol->section->output_section->vma);
1437 value -= (reloc_entry->address
1438 + input_section->output_offset
1439 + input_section->output_section->vma);
1440 value = (bfd_signed_vma) value >> 16;
1442 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1443 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1444 input_section, octets))
1445 return bfd_reloc_outofrange;
1447 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1448 insn &= ~0x1fffc1;
1449 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1450 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1451 if (value + 0x8000 > 0xffff)
1452 return bfd_reloc_overflow;
1453 return bfd_reloc_ok;
1456 static bfd_reloc_status_type
1457 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1458 void *data, asection *input_section,
1459 bfd *output_bfd, char **error_message)
1461 if (output_bfd != NULL)
1462 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1463 input_section, output_bfd, error_message);
1465 if (strcmp (symbol->section->name, ".opd") == 0
1466 && (symbol->section->owner->flags & DYNAMIC) == 0)
1468 bfd_vma dest = opd_entry_value (symbol->section,
1469 symbol->value + reloc_entry->addend,
1470 NULL, NULL, false);
1471 if (dest != (bfd_vma) -1)
1472 reloc_entry->addend = dest - (symbol->value
1473 + symbol->section->output_section->vma
1474 + symbol->section->output_offset);
1476 else
1478 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1480 if (symbol->section->owner != abfd
1481 && symbol->section->owner != NULL
1482 && abiversion (symbol->section->owner) >= 2)
1484 unsigned int i;
1486 for (i = 0; i < symbol->section->owner->symcount; ++i)
1488 asymbol *symdef = symbol->section->owner->outsymbols[i];
1490 if (strcmp (symdef->name, symbol->name) == 0)
1492 elfsym = (elf_symbol_type *) symdef;
1493 break;
1497 reloc_entry->addend
1498 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1500 return bfd_reloc_continue;
1503 static bfd_reloc_status_type
1504 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1505 void *data, asection *input_section,
1506 bfd *output_bfd, char **error_message)
1508 long insn;
1509 enum elf_ppc64_reloc_type r_type;
1510 bfd_size_type octets;
1511 /* Assume 'at' branch hints. */
1512 bool is_isa_v2 = true;
1514 /* If this is a relocatable link (output_bfd test tells us), just
1515 call the generic function. Any adjustment will be done at final
1516 link time. */
1517 if (output_bfd != NULL)
1518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1519 input_section, output_bfd, error_message);
1521 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1522 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1523 input_section, octets))
1524 return bfd_reloc_outofrange;
1526 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1527 insn &= ~(0x01 << 21);
1528 r_type = reloc_entry->howto->type;
1529 if (r_type == R_PPC64_ADDR14_BRTAKEN
1530 || r_type == R_PPC64_REL14_BRTAKEN)
1531 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1533 if (is_isa_v2)
1535 /* Set 'a' bit. This is 0b00010 in BO field for branch
1536 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1537 for branch on CTR insns (BO == 1a00t or 1a01t). */
1538 if ((insn & (0x14 << 21)) == (0x04 << 21))
1539 insn |= 0x02 << 21;
1540 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1541 insn |= 0x08 << 21;
1542 else
1543 goto out;
1545 else
1547 bfd_vma target = 0;
1548 bfd_vma from;
1550 if (!bfd_is_com_section (symbol->section))
1551 target = symbol->value;
1552 target += symbol->section->output_section->vma;
1553 target += symbol->section->output_offset;
1554 target += reloc_entry->addend;
1556 from = (reloc_entry->address
1557 + input_section->output_offset
1558 + input_section->output_section->vma);
1560 /* Invert 'y' bit if not the default. */
1561 if ((bfd_signed_vma) (target - from) < 0)
1562 insn ^= 0x01 << 21;
1564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1565 out:
1566 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1567 input_section, output_bfd, error_message);
1570 static bfd_reloc_status_type
1571 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1572 void *data, asection *input_section,
1573 bfd *output_bfd, char **error_message)
1575 /* If this is a relocatable link (output_bfd test tells us), just
1576 call the generic function. Any adjustment will be done at final
1577 link time. */
1578 if (output_bfd != NULL)
1579 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1580 input_section, output_bfd, error_message);
1582 /* Subtract the symbol section base address. */
1583 reloc_entry->addend -= symbol->section->output_section->vma;
1584 return bfd_reloc_continue;
1587 static bfd_reloc_status_type
1588 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1589 void *data, asection *input_section,
1590 bfd *output_bfd, char **error_message)
1592 /* If this is a relocatable link (output_bfd test tells us), just
1593 call the generic function. Any adjustment will be done at final
1594 link time. */
1595 if (output_bfd != NULL)
1596 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1597 input_section, output_bfd, error_message);
1599 /* Subtract the symbol section base address. */
1600 reloc_entry->addend -= symbol->section->output_section->vma;
1602 /* Adjust the addend for sign extension of the low 16 bits. */
1603 reloc_entry->addend += 0x8000;
1604 return bfd_reloc_continue;
1607 static bfd_reloc_status_type
1608 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609 void *data, asection *input_section,
1610 bfd *output_bfd, char **error_message)
1612 bfd_vma TOCstart;
1614 /* If this is a relocatable link (output_bfd test tells us), just
1615 call the generic function. Any adjustment will be done at final
1616 link time. */
1617 if (output_bfd != NULL)
1618 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1619 input_section, output_bfd, error_message);
1621 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1622 if (TOCstart == 0)
1623 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1625 /* Subtract the TOC base address. */
1626 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1627 return bfd_reloc_continue;
1630 static bfd_reloc_status_type
1631 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1632 void *data, asection *input_section,
1633 bfd *output_bfd, char **error_message)
1635 bfd_vma TOCstart;
1637 /* If this is a relocatable link (output_bfd test tells us), just
1638 call the generic function. Any adjustment will be done at final
1639 link time. */
1640 if (output_bfd != NULL)
1641 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1642 input_section, output_bfd, error_message);
1644 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1645 if (TOCstart == 0)
1646 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1648 /* Subtract the TOC base address. */
1649 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1651 /* Adjust the addend for sign extension of the low 16 bits. */
1652 reloc_entry->addend += 0x8000;
1653 return bfd_reloc_continue;
1656 static bfd_reloc_status_type
1657 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1658 void *data, asection *input_section,
1659 bfd *output_bfd, char **error_message)
1661 bfd_vma TOCstart;
1662 bfd_size_type octets;
1664 /* If this is a relocatable link (output_bfd test tells us), just
1665 call the generic function. Any adjustment will be done at final
1666 link time. */
1667 if (output_bfd != NULL)
1668 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1669 input_section, output_bfd, error_message);
1671 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1672 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1673 input_section, octets))
1674 return bfd_reloc_outofrange;
1676 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1677 if (TOCstart == 0)
1678 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1680 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1681 return bfd_reloc_ok;
1684 static bfd_reloc_status_type
1685 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1686 void *data, asection *input_section,
1687 bfd *output_bfd, char **error_message)
1689 uint64_t insn;
1690 bfd_vma targ;
1691 bfd_size_type octets;
1693 if (output_bfd != NULL)
1694 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1695 input_section, output_bfd, error_message);
1697 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1698 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1699 input_section, octets))
1700 return bfd_reloc_outofrange;
1702 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1703 insn <<= 32;
1704 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1706 targ = (symbol->section->output_section->vma
1707 + symbol->section->output_offset
1708 + reloc_entry->addend);
1709 if (!bfd_is_com_section (symbol->section))
1710 targ += symbol->value;
1711 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1712 targ += 1ULL << 33;
1713 if (reloc_entry->howto->pc_relative)
1715 bfd_vma from = (reloc_entry->address
1716 + input_section->output_offset
1717 + input_section->output_section->vma);
1718 targ -=from;
1720 targ >>= reloc_entry->howto->rightshift;
1721 insn &= ~reloc_entry->howto->dst_mask;
1722 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1723 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1724 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1725 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1726 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1727 >= 1ULL << reloc_entry->howto->bitsize))
1728 return bfd_reloc_overflow;
1729 return bfd_reloc_ok;
1732 static bfd_reloc_status_type
1733 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1734 void *data, asection *input_section,
1735 bfd *output_bfd, char **error_message)
1737 /* If this is a relocatable link (output_bfd test tells us), just
1738 call the generic function. Any adjustment will be done at final
1739 link time. */
1740 if (output_bfd != NULL)
1741 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1742 input_section, output_bfd, error_message);
1744 if (error_message != NULL)
1746 static char *message;
1747 free (message);
1748 if (asprintf (&message, _("generic linker can't handle %s"),
1749 reloc_entry->howto->name) < 0)
1750 message = NULL;
1751 *error_message = message;
1753 return bfd_reloc_dangerous;
1756 /* Track GOT entries needed for a given symbol. We might need more
1757 than one got entry per symbol. */
1758 struct got_entry
1760 struct got_entry *next;
1762 /* The symbol addend that we'll be placing in the GOT. */
1763 bfd_vma addend;
1765 /* Unlike other ELF targets, we use separate GOT entries for the same
1766 symbol referenced from different input files. This is to support
1767 automatic multiple TOC/GOT sections, where the TOC base can vary
1768 from one input file to another. After partitioning into TOC groups
1769 we merge entries within the group.
1771 Point to the BFD owning this GOT entry. */
1772 bfd *owner;
1774 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1775 TLS_TPREL or TLS_DTPREL for tls entries. */
1776 unsigned char tls_type;
1778 /* Non-zero if got.ent points to real entry. */
1779 unsigned char is_indirect;
1781 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1782 union
1784 bfd_signed_vma refcount;
1785 bfd_vma offset;
1786 struct got_entry *ent;
1787 } got;
1790 /* The same for PLT. */
1791 struct plt_entry
1793 struct plt_entry *next;
1795 bfd_vma addend;
1797 union
1799 bfd_signed_vma refcount;
1800 bfd_vma offset;
1801 } plt;
1804 struct ppc64_elf_obj_tdata
1806 struct elf_obj_tdata elf;
1808 /* Shortcuts to dynamic linker sections. */
1809 asection *got;
1810 asection *relgot;
1812 /* Used during garbage collection. We attach global symbols defined
1813 on removed .opd entries to this section so that the sym is removed. */
1814 asection *deleted_section;
1816 /* TLS local dynamic got entry handling. Support for multiple GOT
1817 sections means we potentially need one of these for each input bfd. */
1818 struct got_entry tlsld_got;
1820 union
1822 /* A copy of relocs before they are modified for --emit-relocs. */
1823 Elf_Internal_Rela *relocs;
1825 /* Section contents. */
1826 bfd_byte *contents;
1827 } opd;
1829 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1830 the reloc to be in the range -32768 to 32767. */
1831 unsigned int has_small_toc_reloc : 1;
1833 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1834 instruction not one we handle. */
1835 unsigned int unexpected_toc_insn : 1;
1837 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1838 this file. */
1839 unsigned int has_optrel : 1;
1842 #define ppc64_elf_tdata(bfd) \
1843 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1845 #define ppc64_tlsld_got(bfd) \
1846 (&ppc64_elf_tdata (bfd)->tlsld_got)
1848 #define is_ppc64_elf(bfd) \
1849 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1850 && elf_object_id (bfd) == PPC64_ELF_DATA)
1852 /* Override the generic function because we store some extras. */
1854 static bool
1855 ppc64_elf_mkobject (bfd *abfd)
1857 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1858 PPC64_ELF_DATA);
1861 /* Fix bad default arch selected for a 64 bit input bfd when the
1862 default is 32 bit. Also select arch based on apuinfo. */
1864 static bool
1865 ppc64_elf_object_p (bfd *abfd)
1867 if (!abfd->arch_info->the_default)
1868 return true;
1870 if (abfd->arch_info->bits_per_word == 32)
1872 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1874 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1876 /* Relies on arch after 32 bit default being 64 bit default. */
1877 abfd->arch_info = abfd->arch_info->next;
1878 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1881 return _bfd_elf_ppc_set_arch (abfd);
1884 /* Support for core dump NOTE sections. */
1886 static bool
1887 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1889 size_t offset, size;
1891 if (note->descsz != 504)
1892 return false;
1894 /* pr_cursig */
1895 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1897 /* pr_pid */
1898 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1900 /* pr_reg */
1901 offset = 112;
1902 size = 384;
1904 /* Make a ".reg/999" section. */
1905 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1906 size, note->descpos + offset);
1909 static bool
1910 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1912 if (note->descsz != 136)
1913 return false;
1915 elf_tdata (abfd)->core->pid
1916 = bfd_get_32 (abfd, note->descdata + 24);
1917 elf_tdata (abfd)->core->program
1918 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1919 elf_tdata (abfd)->core->command
1920 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1922 return true;
1925 static char *
1926 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1927 ...)
1929 switch (note_type)
1931 default:
1932 return NULL;
1934 case NT_PRPSINFO:
1936 char data[136] ATTRIBUTE_NONSTRING;
1937 va_list ap;
1939 va_start (ap, note_type);
1940 memset (data, 0, sizeof (data));
1941 strncpy (data + 40, va_arg (ap, const char *), 16);
1942 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1943 DIAGNOSTIC_PUSH;
1944 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1945 -Wstringop-truncation:
1946 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1948 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1949 #endif
1950 strncpy (data + 56, va_arg (ap, const char *), 80);
1951 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1952 DIAGNOSTIC_POP;
1953 #endif
1954 va_end (ap);
1955 return elfcore_write_note (abfd, buf, bufsiz,
1956 "CORE", note_type, data, sizeof (data));
1959 case NT_PRSTATUS:
1961 char data[504];
1962 va_list ap;
1963 long pid;
1964 int cursig;
1965 const void *greg;
1967 va_start (ap, note_type);
1968 memset (data, 0, 112);
1969 pid = va_arg (ap, long);
1970 bfd_put_32 (abfd, pid, data + 32);
1971 cursig = va_arg (ap, int);
1972 bfd_put_16 (abfd, cursig, data + 12);
1973 greg = va_arg (ap, const void *);
1974 memcpy (data + 112, greg, 384);
1975 memset (data + 496, 0, 8);
1976 va_end (ap);
1977 return elfcore_write_note (abfd, buf, bufsiz,
1978 "CORE", note_type, data, sizeof (data));
1983 /* Add extra PPC sections. */
1985 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1987 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1988 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1989 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1990 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1991 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1993 { NULL, 0, 0, 0, 0 }
1996 enum _ppc64_sec_type {
1997 sec_normal = 0,
1998 sec_opd = 1,
1999 sec_toc = 2
2002 struct _ppc64_elf_section_data
2004 struct bfd_elf_section_data elf;
2006 union
2008 /* An array with one entry for each opd function descriptor,
2009 and some spares since opd entries may be either 16 or 24 bytes. */
2010 #define OPD_NDX(OFF) ((OFF) >> 4)
2011 struct _opd_sec_data
2013 /* Points to the function code section for local opd entries. */
2014 asection **func_sec;
2016 /* After editing .opd, adjust references to opd local syms. */
2017 long *adjust;
2018 } opd;
2020 /* An array for toc sections, indexed by offset/8. */
2021 struct _toc_sec_data
2023 /* Specifies the relocation symbol index used at a given toc offset. */
2024 unsigned *symndx;
2026 /* And the relocation addend. */
2027 bfd_vma *add;
2028 } toc;
2029 } u;
2031 enum _ppc64_sec_type sec_type:2;
2033 /* Flag set when small branches are detected. Used to
2034 select suitable defaults for the stub group size. */
2035 unsigned int has_14bit_branch:1;
2037 /* Flag set when PLTCALL relocs are detected. */
2038 unsigned int has_pltcall:1;
2040 /* Flag set when section has PLT/GOT/TOC relocations that can be
2041 optimised. */
2042 unsigned int has_optrel:1;
2045 #define ppc64_elf_section_data(sec) \
2046 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2048 static bool
2049 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2051 if (!sec->used_by_bfd)
2053 struct _ppc64_elf_section_data *sdata;
2054 size_t amt = sizeof (*sdata);
2056 sdata = bfd_zalloc (abfd, amt);
2057 if (sdata == NULL)
2058 return false;
2059 sec->used_by_bfd = sdata;
2062 return _bfd_elf_new_section_hook (abfd, sec);
2065 static bool
2066 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2068 const char *name = hdr->bfd_section->name;
2070 if (startswith (name, ".sbss")
2071 || startswith (name, ".sdata"))
2072 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2074 return true;
2077 static struct _opd_sec_data *
2078 get_opd_info (asection * sec)
2080 if (sec != NULL
2081 && ppc64_elf_section_data (sec) != NULL
2082 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2083 return &ppc64_elf_section_data (sec)->u.opd;
2084 return NULL;
2087 /* Parameters for the qsort hook. */
2088 static bool synthetic_relocatable;
2089 static const asection *synthetic_opd;
2091 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2093 static int
2094 compare_symbols (const void *ap, const void *bp)
2096 const asymbol *a = *(const asymbol **) ap;
2097 const asymbol *b = *(const asymbol **) bp;
2099 /* Section symbols first. */
2100 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2101 return -1;
2102 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2103 return 1;
2105 /* then .opd symbols. */
2106 if (synthetic_opd != NULL)
2108 if (strcmp (a->section->name, ".opd") == 0
2109 && strcmp (b->section->name, ".opd") != 0)
2110 return -1;
2111 if (strcmp (a->section->name, ".opd") != 0
2112 && strcmp (b->section->name, ".opd") == 0)
2113 return 1;
2116 /* then other code symbols. */
2117 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2118 == (SEC_CODE | SEC_ALLOC))
2119 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2120 != (SEC_CODE | SEC_ALLOC)))
2121 return -1;
2123 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2124 != (SEC_CODE | SEC_ALLOC))
2125 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2126 == (SEC_CODE | SEC_ALLOC)))
2127 return 1;
2129 if (synthetic_relocatable)
2131 if (a->section->id < b->section->id)
2132 return -1;
2134 if (a->section->id > b->section->id)
2135 return 1;
2138 if (a->value + a->section->vma < b->value + b->section->vma)
2139 return -1;
2141 if (a->value + a->section->vma > b->value + b->section->vma)
2142 return 1;
2144 /* For syms with the same value, prefer strong dynamic global function
2145 syms over other syms. */
2146 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2147 return -1;
2149 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2150 return 1;
2152 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2153 return -1;
2155 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2156 return 1;
2158 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2159 return -1;
2161 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2162 return 1;
2164 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2165 return -1;
2167 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2168 return 1;
2170 /* Finally, sort on where the symbol is in memory. The symbols will
2171 be in at most two malloc'd blocks, one for static syms, one for
2172 dynamic syms, and we distinguish the two blocks above by testing
2173 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2174 originally in the same order as the symbols (and we're not
2175 sorting the symbols themselves), this ensures a stable sort. */
2176 if (a < b)
2177 return -1;
2178 if (a > b)
2179 return 1;
2180 return 0;
2183 /* Search SYMS for a symbol of the given VALUE. */
2185 static asymbol *
2186 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2187 bfd_vma value)
2189 size_t mid;
2191 if (id == (unsigned) -1)
2193 while (lo < hi)
2195 mid = (lo + hi) >> 1;
2196 if (syms[mid]->value + syms[mid]->section->vma < value)
2197 lo = mid + 1;
2198 else if (syms[mid]->value + syms[mid]->section->vma > value)
2199 hi = mid;
2200 else
2201 return syms[mid];
2204 else
2206 while (lo < hi)
2208 mid = (lo + hi) >> 1;
2209 if (syms[mid]->section->id < id)
2210 lo = mid + 1;
2211 else if (syms[mid]->section->id > id)
2212 hi = mid;
2213 else if (syms[mid]->value < value)
2214 lo = mid + 1;
2215 else if (syms[mid]->value > value)
2216 hi = mid;
2217 else
2218 return syms[mid];
2221 return NULL;
2224 static bool
2225 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2227 bfd_vma vma = *(bfd_vma *) ptr;
2228 return ((section->flags & SEC_ALLOC) != 0
2229 && section->vma <= vma
2230 && vma < section->vma + section->size);
2233 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2234 entry syms. Also generate @plt symbols for the glink branch table.
2235 Returns count of synthetic symbols in RET or -1 on error. */
2237 static long
2238 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2239 long static_count, asymbol **static_syms,
2240 long dyn_count, asymbol **dyn_syms,
2241 asymbol **ret)
2243 asymbol *s;
2244 size_t i, j, count;
2245 char *names;
2246 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2247 asection *opd = NULL;
2248 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2249 asymbol **syms;
2250 int abi = abiversion (abfd);
2252 *ret = NULL;
2254 if (abi < 2)
2256 opd = bfd_get_section_by_name (abfd, ".opd");
2257 if (opd == NULL && abi == 1)
2258 return 0;
2261 syms = NULL;
2262 codesecsym = 0;
2263 codesecsymend = 0;
2264 secsymend = 0;
2265 opdsymend = 0;
2266 symcount = 0;
2267 if (opd != NULL)
2269 symcount = static_count;
2270 if (!relocatable)
2271 symcount += dyn_count;
2272 if (symcount == 0)
2273 return 0;
2275 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2276 if (syms == NULL)
2277 return -1;
2279 if (!relocatable && static_count != 0 && dyn_count != 0)
2281 /* Use both symbol tables. */
2282 memcpy (syms, static_syms, static_count * sizeof (*syms));
2283 memcpy (syms + static_count, dyn_syms,
2284 (dyn_count + 1) * sizeof (*syms));
2286 else if (!relocatable && static_count == 0)
2287 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2288 else
2289 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2291 /* Trim uninteresting symbols. Interesting symbols are section,
2292 function, and notype symbols. */
2293 for (i = 0, j = 0; i < symcount; ++i)
2294 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2295 | BSF_RELC | BSF_SRELC)) == 0)
2296 syms[j++] = syms[i];
2297 symcount = j;
2299 synthetic_relocatable = relocatable;
2300 synthetic_opd = opd;
2301 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2303 if (!relocatable && symcount > 1)
2305 /* Trim duplicate syms, since we may have merged the normal
2306 and dynamic symbols. Actually, we only care about syms
2307 that have different values, so trim any with the same
2308 value. Don't consider ifunc and ifunc resolver symbols
2309 duplicates however, because GDB wants to know whether a
2310 text symbol is an ifunc resolver. */
2311 for (i = 1, j = 1; i < symcount; ++i)
2313 const asymbol *s0 = syms[i - 1];
2314 const asymbol *s1 = syms[i];
2316 if ((s0->value + s0->section->vma
2317 != s1->value + s1->section->vma)
2318 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2319 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2320 syms[j++] = syms[i];
2322 symcount = j;
2325 i = 0;
2326 /* Note that here and in compare_symbols we can't compare opd and
2327 sym->section directly. With separate debug info files, the
2328 symbols will be extracted from the debug file while abfd passed
2329 to this function is the real binary. */
2330 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2331 && strcmp (syms[i]->section->name, ".opd") == 0)
2332 ++i;
2333 codesecsym = i;
2335 for (; i < symcount; ++i)
2336 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2337 | SEC_THREAD_LOCAL))
2338 != (SEC_CODE | SEC_ALLOC))
2339 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2340 break;
2341 codesecsymend = i;
2343 for (; i < symcount; ++i)
2344 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2345 break;
2346 secsymend = i;
2348 for (; i < symcount; ++i)
2349 if (strcmp (syms[i]->section->name, ".opd") != 0)
2350 break;
2351 opdsymend = i;
2353 for (; i < symcount; ++i)
2354 if (((syms[i]->section->flags
2355 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2356 != (SEC_CODE | SEC_ALLOC))
2357 break;
2358 symcount = i;
2360 count = 0;
2362 if (relocatable)
2364 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2365 arelent *r;
2366 size_t size;
2367 size_t relcount;
2369 if (opdsymend == secsymend)
2370 goto done;
2372 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2373 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2374 if (relcount == 0)
2375 goto done;
2377 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2379 count = -1;
2380 goto done;
2383 size = 0;
2384 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2386 asymbol *sym;
2388 while (r < opd->relocation + relcount
2389 && r->address < syms[i]->value + opd->vma)
2390 ++r;
2392 if (r == opd->relocation + relcount)
2393 break;
2395 if (r->address != syms[i]->value + opd->vma)
2396 continue;
2398 if (r->howto->type != R_PPC64_ADDR64)
2399 continue;
2401 sym = *r->sym_ptr_ptr;
2402 if (!sym_exists_at (syms, opdsymend, symcount,
2403 sym->section->id, sym->value + r->addend))
2405 ++count;
2406 size += sizeof (asymbol);
2407 size += strlen (syms[i]->name) + 2;
2411 if (size == 0)
2412 goto done;
2413 s = *ret = bfd_malloc (size);
2414 if (s == NULL)
2416 count = -1;
2417 goto done;
2420 names = (char *) (s + count);
2422 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2424 asymbol *sym;
2426 while (r < opd->relocation + relcount
2427 && r->address < syms[i]->value + opd->vma)
2428 ++r;
2430 if (r == opd->relocation + relcount)
2431 break;
2433 if (r->address != syms[i]->value + opd->vma)
2434 continue;
2436 if (r->howto->type != R_PPC64_ADDR64)
2437 continue;
2439 sym = *r->sym_ptr_ptr;
2440 if (!sym_exists_at (syms, opdsymend, symcount,
2441 sym->section->id, sym->value + r->addend))
2443 size_t len;
2445 *s = *syms[i];
2446 s->flags |= BSF_SYNTHETIC;
2447 s->section = sym->section;
2448 s->value = sym->value + r->addend;
2449 s->name = names;
2450 *names++ = '.';
2451 len = strlen (syms[i]->name);
2452 memcpy (names, syms[i]->name, len + 1);
2453 names += len + 1;
2454 /* Have udata.p point back to the original symbol this
2455 synthetic symbol was derived from. */
2456 s->udata.p = syms[i];
2457 s++;
2461 else
2463 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2464 bfd_byte *contents = NULL;
2465 size_t size;
2466 size_t plt_count = 0;
2467 bfd_vma glink_vma = 0, resolv_vma = 0;
2468 asection *dynamic, *glink = NULL, *relplt = NULL;
2469 arelent *p;
2471 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2473 free_contents_and_exit_err:
2474 count = -1;
2475 free_contents_and_exit:
2476 free (contents);
2477 goto done;
2480 size = 0;
2481 for (i = secsymend; i < opdsymend; ++i)
2483 bfd_vma ent;
2485 /* Ignore bogus symbols. */
2486 if (syms[i]->value > opd->size - 8)
2487 continue;
2489 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2490 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2492 ++count;
2493 size += sizeof (asymbol);
2494 size += strlen (syms[i]->name) + 2;
2498 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2499 if (dyn_count != 0
2500 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2502 bfd_byte *dynbuf, *extdyn, *extdynend;
2503 size_t extdynsize;
2504 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2506 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2507 goto free_contents_and_exit_err;
2509 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2510 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2512 extdyn = dynbuf;
2513 extdynend = extdyn + dynamic->size;
2514 for (; extdyn < extdynend; extdyn += extdynsize)
2516 Elf_Internal_Dyn dyn;
2517 (*swap_dyn_in) (abfd, extdyn, &dyn);
2519 if (dyn.d_tag == DT_NULL)
2520 break;
2522 if (dyn.d_tag == DT_PPC64_GLINK)
2524 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2525 See comment in ppc64_elf_finish_dynamic_sections. */
2526 glink_vma = dyn.d_un.d_val + 8 * 4;
2527 /* The .glink section usually does not survive the final
2528 link; search for the section (usually .text) where the
2529 glink stubs now reside. */
2530 glink = bfd_sections_find_if (abfd, section_covers_vma,
2531 &glink_vma);
2532 break;
2536 free (dynbuf);
2539 if (glink != NULL)
2541 /* Determine __glink trampoline by reading the relative branch
2542 from the first glink stub. */
2543 bfd_byte buf[4];
2544 unsigned int off = 0;
2546 while (bfd_get_section_contents (abfd, glink, buf,
2547 glink_vma + off - glink->vma, 4))
2549 unsigned int insn = bfd_get_32 (abfd, buf);
2550 insn ^= B_DOT;
2551 if ((insn & ~0x3fffffc) == 0)
2553 resolv_vma
2554 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2555 break;
2557 off += 4;
2558 if (off > 4)
2559 break;
2562 if (resolv_vma)
2563 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2565 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2566 if (relplt != NULL)
2568 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2569 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2570 goto free_contents_and_exit_err;
2572 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2573 size += plt_count * sizeof (asymbol);
2575 p = relplt->relocation;
2576 for (i = 0; i < plt_count; i++, p++)
2578 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2579 if (p->addend != 0)
2580 size += sizeof ("+0x") - 1 + 16;
2585 if (size == 0)
2586 goto free_contents_and_exit;
2587 s = *ret = bfd_malloc (size);
2588 if (s == NULL)
2589 goto free_contents_and_exit_err;
2591 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2593 for (i = secsymend; i < opdsymend; ++i)
2595 bfd_vma ent;
2597 if (syms[i]->value > opd->size - 8)
2598 continue;
2600 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2601 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2603 size_t lo, hi;
2604 size_t len;
2605 asection *sec = abfd->sections;
2607 *s = *syms[i];
2608 lo = codesecsym;
2609 hi = codesecsymend;
2610 while (lo < hi)
2612 size_t mid = (lo + hi) >> 1;
2613 if (syms[mid]->section->vma < ent)
2614 lo = mid + 1;
2615 else if (syms[mid]->section->vma > ent)
2616 hi = mid;
2617 else
2619 sec = syms[mid]->section;
2620 break;
2624 if (lo >= hi && lo > codesecsym)
2625 sec = syms[lo - 1]->section;
2627 for (; sec != NULL; sec = sec->next)
2629 if (sec->vma > ent)
2630 break;
2631 /* SEC_LOAD may not be set if SEC is from a separate debug
2632 info file. */
2633 if ((sec->flags & SEC_ALLOC) == 0)
2634 break;
2635 if ((sec->flags & SEC_CODE) != 0)
2636 s->section = sec;
2638 s->flags |= BSF_SYNTHETIC;
2639 s->value = ent - s->section->vma;
2640 s->name = names;
2641 *names++ = '.';
2642 len = strlen (syms[i]->name);
2643 memcpy (names, syms[i]->name, len + 1);
2644 names += len + 1;
2645 /* Have udata.p point back to the original symbol this
2646 synthetic symbol was derived from. */
2647 s->udata.p = syms[i];
2648 s++;
2651 free (contents);
2653 if (glink != NULL && relplt != NULL)
2655 if (resolv_vma)
2657 /* Add a symbol for the main glink trampoline. */
2658 memset (s, 0, sizeof *s);
2659 s->the_bfd = abfd;
2660 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2661 s->section = glink;
2662 s->value = resolv_vma - glink->vma;
2663 s->name = names;
2664 memcpy (names, "__glink_PLTresolve",
2665 sizeof ("__glink_PLTresolve"));
2666 names += sizeof ("__glink_PLTresolve");
2667 s++;
2668 count++;
2671 /* FIXME: It would be very much nicer to put sym@plt on the
2672 stub rather than on the glink branch table entry. The
2673 objdump disassembler would then use a sensible symbol
2674 name on plt calls. The difficulty in doing so is
2675 a) finding the stubs, and,
2676 b) matching stubs against plt entries, and,
2677 c) there can be multiple stubs for a given plt entry.
2679 Solving (a) could be done by code scanning, but older
2680 ppc64 binaries used different stubs to current code.
2681 (b) is the tricky one since you need to known the toc
2682 pointer for at least one function that uses a pic stub to
2683 be able to calculate the plt address referenced.
2684 (c) means gdb would need to set multiple breakpoints (or
2685 find the glink branch itself) when setting breakpoints
2686 for pending shared library loads. */
2687 p = relplt->relocation;
2688 for (i = 0; i < plt_count; i++, p++)
2690 size_t len;
2692 *s = **p->sym_ptr_ptr;
2693 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2694 we are defining a symbol, ensure one of them is set. */
2695 if ((s->flags & BSF_LOCAL) == 0)
2696 s->flags |= BSF_GLOBAL;
2697 s->flags |= BSF_SYNTHETIC;
2698 s->section = glink;
2699 s->value = glink_vma - glink->vma;
2700 s->name = names;
2701 s->udata.p = NULL;
2702 len = strlen ((*p->sym_ptr_ptr)->name);
2703 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2704 names += len;
2705 if (p->addend != 0)
2707 memcpy (names, "+0x", sizeof ("+0x") - 1);
2708 names += sizeof ("+0x") - 1;
2709 bfd_sprintf_vma (abfd, names, p->addend);
2710 names += strlen (names);
2712 memcpy (names, "@plt", sizeof ("@plt"));
2713 names += sizeof ("@plt");
2714 s++;
2715 if (abi < 2)
2717 glink_vma += 8;
2718 if (i >= 0x8000)
2719 glink_vma += 4;
2721 else
2722 glink_vma += 4;
2724 count += plt_count;
2728 done:
2729 free (syms);
2730 return count;
2733 /* The following functions are specific to the ELF linker, while
2734 functions above are used generally. Those named ppc64_elf_* are
2735 called by the main ELF linker code. They appear in this file more
2736 or less in the order in which they are called. eg.
2737 ppc64_elf_check_relocs is called early in the link process,
2738 ppc64_elf_finish_dynamic_sections is one of the last functions
2739 called.
2741 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2742 functions have both a function code symbol and a function descriptor
2743 symbol. A call to foo in a relocatable object file looks like:
2745 . .text
2746 . x:
2747 . bl .foo
2748 . nop
2750 The function definition in another object file might be:
2752 . .section .opd
2753 . foo: .quad .foo
2754 . .quad .TOC.@tocbase
2755 . .quad 0
2757 . .text
2758 . .foo: blr
2760 When the linker resolves the call during a static link, the branch
2761 unsurprisingly just goes to .foo and the .opd information is unused.
2762 If the function definition is in a shared library, things are a little
2763 different: The call goes via a plt call stub, the opd information gets
2764 copied to the plt, and the linker patches the nop.
2766 . x:
2767 . bl .foo_stub
2768 . ld 2,40(1)
2771 . .foo_stub:
2772 . std 2,40(1) # in practice, the call stub
2773 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2774 . addi 11,11,Lfoo@toc@l # this is the general idea
2775 . ld 12,0(11)
2776 . ld 2,8(11)
2777 . mtctr 12
2778 . ld 11,16(11)
2779 . bctr
2781 . .section .plt
2782 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2784 The "reloc ()" notation is supposed to indicate that the linker emits
2785 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2786 copying.
2788 What are the difficulties here? Well, firstly, the relocations
2789 examined by the linker in check_relocs are against the function code
2790 sym .foo, while the dynamic relocation in the plt is emitted against
2791 the function descriptor symbol, foo. Somewhere along the line, we need
2792 to carefully copy dynamic link information from one symbol to the other.
2793 Secondly, the generic part of the elf linker will make .foo a dynamic
2794 symbol as is normal for most other backends. We need foo dynamic
2795 instead, at least for an application final link. However, when
2796 creating a shared library containing foo, we need to have both symbols
2797 dynamic so that references to .foo are satisfied during the early
2798 stages of linking. Otherwise the linker might decide to pull in a
2799 definition from some other object, eg. a static library.
2801 Update: As of August 2004, we support a new convention. Function
2802 calls may use the function descriptor symbol, ie. "bl foo". This
2803 behaves exactly as "bl .foo". */
2805 /* Of those relocs that might be copied as dynamic relocs, this
2806 function selects those that must be copied when linking a shared
2807 library or PIE, even when the symbol is local. */
2809 static int
2810 must_be_dyn_reloc (struct bfd_link_info *info,
2811 enum elf_ppc64_reloc_type r_type)
2813 switch (r_type)
2815 default:
2816 /* Only relative relocs can be resolved when the object load
2817 address isn't fixed. DTPREL64 is excluded because the
2818 dynamic linker needs to differentiate global dynamic from
2819 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2820 return 1;
2822 case R_PPC64_REL32:
2823 case R_PPC64_REL64:
2824 case R_PPC64_REL30:
2825 case R_PPC64_TOC16:
2826 case R_PPC64_TOC16_DS:
2827 case R_PPC64_TOC16_LO:
2828 case R_PPC64_TOC16_HI:
2829 case R_PPC64_TOC16_HA:
2830 case R_PPC64_TOC16_LO_DS:
2831 return 0;
2833 case R_PPC64_TPREL16:
2834 case R_PPC64_TPREL16_LO:
2835 case R_PPC64_TPREL16_HI:
2836 case R_PPC64_TPREL16_HA:
2837 case R_PPC64_TPREL16_DS:
2838 case R_PPC64_TPREL16_LO_DS:
2839 case R_PPC64_TPREL16_HIGH:
2840 case R_PPC64_TPREL16_HIGHA:
2841 case R_PPC64_TPREL16_HIGHER:
2842 case R_PPC64_TPREL16_HIGHERA:
2843 case R_PPC64_TPREL16_HIGHEST:
2844 case R_PPC64_TPREL16_HIGHESTA:
2845 case R_PPC64_TPREL64:
2846 case R_PPC64_TPREL34:
2847 /* These relocations are relative but in a shared library the
2848 linker doesn't know the thread pointer base. */
2849 return bfd_link_dll (info);
2853 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2854 copying dynamic variables from a shared lib into an app's .dynbss
2855 section, and instead use a dynamic relocation to point into the
2856 shared lib. With code that gcc generates it is vital that this be
2857 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2858 actually the address of a function descriptor which resides in the
2859 .opd section. gcc uses the descriptor directly rather than going
2860 via the GOT as some other ABIs do, which means that initialized
2861 function pointers reference the descriptor. Thus, a function
2862 pointer initialized to the address of a function in a shared
2863 library will either require a .dynbss copy and a copy reloc, or a
2864 dynamic reloc. Using a .dynbss copy redefines the function
2865 descriptor symbol to point to the copy. This presents a problem as
2866 a PLT entry for that function is also initialized from the function
2867 descriptor symbol and the copy may not be initialized first. */
2868 #define ELIMINATE_COPY_RELOCS 1
2870 /* Section name for stubs is the associated section name plus this
2871 string. */
2872 #define STUB_SUFFIX ".stub"
2874 /* Linker stubs.
2875 ppc_stub_long_branch:
2876 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2877 destination, but a 24 bit branch in a stub section will reach.
2878 . b dest
2880 ppc_stub_plt_branch:
2881 Similar to the above, but a 24 bit branch in the stub section won't
2882 reach its destination.
2883 . addis %r12,%r2,xxx@toc@ha
2884 . ld %r12,xxx@toc@l(%r12)
2885 . mtctr %r12
2886 . bctr
2888 ppc_stub_plt_call:
2889 Used to call a function in a shared library. If it so happens that
2890 the plt entry referenced crosses a 64k boundary, then an extra
2891 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2892 An r2save variant starts with "std %r2,40(%r1)".
2893 . addis %r11,%r2,xxx@toc@ha
2894 . ld %r12,xxx+0@toc@l(%r11)
2895 . mtctr %r12
2896 . ld %r2,xxx+8@toc@l(%r11)
2897 . ld %r11,xxx+16@toc@l(%r11)
2898 . bctr
2900 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2901 code to adjust the value and save r2 to support multiple toc sections.
2902 A ppc_stub_long_branch with an r2 offset looks like:
2903 . std %r2,40(%r1)
2904 . addis %r2,%r2,off@ha
2905 . addi %r2,%r2,off@l
2906 . b dest
2908 A ppc_stub_plt_branch with an r2 offset looks like:
2909 . std %r2,40(%r1)
2910 . addis %r12,%r2,xxx@toc@ha
2911 . ld %r12,xxx@toc@l(%r12)
2912 . addis %r2,%r2,off@ha
2913 . addi %r2,%r2,off@l
2914 . mtctr %r12
2915 . bctr
2917 All of the above stubs are shown as their ELFv1 variants. ELFv2
2918 variants exist too, simpler for plt calls since a new toc pointer
2919 and static chain are not loaded by the stub. In addition, ELFv2
2920 has some more complex stubs to handle calls marked with NOTOC
2921 relocs from functions where r2 is not a valid toc pointer.
2922 ppc_stub_long_branch_p9notoc:
2923 . mflr %r12
2924 . bcl 20,31,1f
2925 . 1:
2926 . mflr %r11
2927 . mtlr %r12
2928 . addis %r12,%r11,dest-1b@ha
2929 . addi %r12,%r12,dest-1b@l
2930 . b dest
2932 ppc_stub_plt_branch_p9notoc:
2933 . mflr %r12
2934 . bcl 20,31,1f
2935 . 1:
2936 . mflr %r11
2937 . mtlr %r12
2938 . lis %r12,xxx-1b@highest
2939 . ori %r12,%r12,xxx-1b@higher
2940 . sldi %r12,%r12,32
2941 . oris %r12,%r12,xxx-1b@high
2942 . ori %r12,%r12,xxx-1b@l
2943 . add %r12,%r11,%r12
2944 . mtctr %r12
2945 . bctr
2947 ppc_stub_plt_call_p9notoc:
2948 . mflr %r12
2949 . bcl 20,31,1f
2950 . 1:
2951 . mflr %r11
2952 . mtlr %r12
2953 . lis %r12,xxx-1b@highest
2954 . ori %r12,%r12,xxx-1b@higher
2955 . sldi %r12,%r12,32
2956 . oris %r12,%r12,xxx-1b@high
2957 . ori %r12,%r12,xxx-1b@l
2958 . ldx %r12,%r11,%r12
2959 . mtctr %r12
2960 . bctr
2962 There are also ELFv1 power10 variants of these stubs.
2963 ppc_stub_long_branch_notoc:
2964 . pla %r12,dest@pcrel
2965 . b dest
2966 ppc_stub_plt_branch_notoc:
2967 . lis %r11,(dest-1f)@highesta34
2968 . ori %r11,%r11,(dest-1f)@highera34
2969 . sldi %r11,%r11,34
2970 . 1: pla %r12,dest@pcrel
2971 . add %r12,%r11,%r12
2972 . mtctr %r12
2973 . bctr
2974 ppc_stub_plt_call_notoc:
2975 . lis %r11,(xxx-1f)@highesta34
2976 . ori %r11,%r11,(xxx-1f)@highera34
2977 . sldi %r11,%r11,34
2978 . 1: pla %r12,xxx@pcrel
2979 . ldx %r12,%r11,%r12
2980 . mtctr %r12
2981 . bctr
2983 In cases where the high instructions would add zero, they are
2984 omitted and following instructions modified in some cases.
2985 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2987 . pld %r12,xxx@pcrel
2988 . mtctr %r12
2989 . bctr
2991 Stub variants may be merged. For example, if printf is called from
2992 code with the tocsave optimization (ie. r2 saved in function
2993 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2994 and from other code without the tocsave optimization requiring a
2995 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2996 type will be created. Calls with the tocsave optimization will
2997 enter this stub after the instruction saving r2. A similar
2998 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2999 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3000 to call an external function like printf. If other calls to printf
3001 require a ppc_stub_plt_call linkage stub then a single
3002 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3003 call. */
3005 enum ppc_stub_main_type
3007 ppc_stub_none,
3008 ppc_stub_long_branch,
3009 ppc_stub_plt_branch,
3010 ppc_stub_plt_call,
3011 ppc_stub_global_entry,
3012 ppc_stub_save_res
3015 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3016 these variations. */
3018 enum ppc_stub_sub_type
3020 ppc_stub_toc,
3021 ppc_stub_notoc,
3022 ppc_stub_p9notoc
3025 struct ppc_stub_type
3027 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3028 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3029 unsigned int r2save : 1;
3032 /* Information on stub grouping. */
3033 struct map_stub
3035 /* The stub section. */
3036 asection *stub_sec;
3037 /* This is the section to which stubs in the group will be attached. */
3038 asection *link_sec;
3039 /* Next group. */
3040 struct map_stub *next;
3041 /* Whether to emit a copy of register save/restore functions in this
3042 group. */
3043 int needs_save_res;
3044 /* Current offset within stubs after the insn restoring lr in a
3045 _notoc or _both stub using bcl for pc-relative addressing, or
3046 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3047 unsigned int lr_restore;
3048 /* Accumulated size of EH info emitted to describe return address
3049 if stubs modify lr. Does not include 17 byte FDE header. */
3050 unsigned int eh_size;
3051 /* Offset in glink_eh_frame to the start of EH info for this group. */
3052 unsigned int eh_base;
3055 struct ppc_stub_hash_entry
3057 /* Base hash table entry structure. */
3058 struct bfd_hash_entry root;
3060 struct ppc_stub_type type;
3062 /* Group information. */
3063 struct map_stub *group;
3065 /* Offset within stub_sec of the beginning of this stub. */
3066 bfd_vma stub_offset;
3068 /* Given the symbol's value and its section we can determine its final
3069 value when building the stubs (so the stub knows where to jump. */
3070 bfd_vma target_value;
3071 asection *target_section;
3073 /* The symbol table entry, if any, that this was derived from. */
3074 struct ppc_link_hash_entry *h;
3075 struct plt_entry *plt_ent;
3077 /* Symbol type. */
3078 unsigned char symtype;
3080 /* Symbol st_other. */
3081 unsigned char other;
3084 struct ppc_branch_hash_entry
3086 /* Base hash table entry structure. */
3087 struct bfd_hash_entry root;
3089 /* Offset within branch lookup table. */
3090 unsigned int offset;
3092 /* Generation marker. */
3093 unsigned int iter;
3096 /* Used to track dynamic relocations. */
3097 struct ppc_dyn_relocs
3099 struct ppc_dyn_relocs *next;
3101 /* The input section of the reloc. */
3102 asection *sec;
3104 /* Total number of relocs copied for the input section. */
3105 unsigned int count;
3107 /* Number of pc-relative relocs copied for the input section. */
3108 unsigned int pc_count;
3110 /* Number of relocs that might become R_PPC64_RELATIVE. */
3111 unsigned int rel_count;
3114 struct ppc_local_dyn_relocs
3116 struct ppc_local_dyn_relocs *next;
3118 /* The input section of the reloc. */
3119 asection *sec;
3121 /* Total number of relocs copied for the input section. */
3122 unsigned int count;
3124 /* Number of relocs that might become R_PPC64_RELATIVE. */
3125 unsigned int rel_count : 31;
3127 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3128 unsigned int ifunc : 1;
3131 struct ppc_link_hash_entry
3133 struct elf_link_hash_entry elf;
3135 union
3137 /* A pointer to the most recently used stub hash entry against this
3138 symbol. */
3139 struct ppc_stub_hash_entry *stub_cache;
3141 /* A pointer to the next symbol starting with a '.' */
3142 struct ppc_link_hash_entry *next_dot_sym;
3143 } u;
3145 /* Link between function code and descriptor symbols. */
3146 struct ppc_link_hash_entry *oh;
3148 /* Flag function code and descriptor symbols. */
3149 unsigned int is_func:1;
3150 unsigned int is_func_descriptor:1;
3151 unsigned int fake:1;
3153 /* Whether global opd/toc sym has been adjusted or not.
3154 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3155 should be set for all globals defined in any opd/toc section. */
3156 unsigned int adjust_done:1;
3158 /* Set if this is an out-of-line register save/restore function,
3159 with non-standard calling convention. */
3160 unsigned int save_res:1;
3162 /* Set if a duplicate symbol with non-zero localentry is detected,
3163 even when the duplicate symbol does not provide a definition. */
3164 unsigned int non_zero_localentry:1;
3166 /* Contexts in which symbol is used in the GOT (or TOC).
3167 Bits are or'd into the mask as the corresponding relocs are
3168 encountered during check_relocs, with TLS_TLS being set when any
3169 of the other TLS bits are set. tls_optimize clears bits when
3170 optimizing to indicate the corresponding GOT entry type is not
3171 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3172 set TLS_GDIE when a GD reloc turns into an IE one.
3173 These flags are also kept for local symbols. */
3174 #define TLS_TLS 1 /* Any TLS reloc. */
3175 #define TLS_GD 2 /* GD reloc. */
3176 #define TLS_LD 4 /* LD reloc. */
3177 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3178 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3179 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3180 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3181 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3182 unsigned char tls_mask;
3184 /* The above field is also used to mark function symbols. In which
3185 case TLS_TLS will be 0. */
3186 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3187 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3188 #define NON_GOT 256 /* local symbol plt, not stored. */
3191 static inline struct ppc_link_hash_entry *
3192 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3194 return (struct ppc_link_hash_entry *) ent;
3197 static inline struct elf_link_hash_entry *
3198 elf_hash_entry (struct ppc_link_hash_entry *ent)
3200 return (struct elf_link_hash_entry *) ent;
3203 /* ppc64 ELF linker hash table. */
3205 struct ppc_link_hash_table
3207 struct elf_link_hash_table elf;
3209 /* The stub hash table. */
3210 struct bfd_hash_table stub_hash_table;
3212 /* Another hash table for plt_branch stubs. */
3213 struct bfd_hash_table branch_hash_table;
3215 /* Hash table for function prologue tocsave. */
3216 htab_t tocsave_htab;
3218 /* Various options and other info passed from the linker. */
3219 struct ppc64_elf_params *params;
3221 /* The size of sec_info below. */
3222 unsigned int sec_info_arr_size;
3224 /* Per-section array of extra section info. Done this way rather
3225 than as part of ppc64_elf_section_data so we have the info for
3226 non-ppc64 sections. */
3227 struct
3229 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3230 bfd_vma toc_off;
3232 union
3234 /* The section group that this section belongs to. */
3235 struct map_stub *group;
3236 /* A temp section list pointer. */
3237 asection *list;
3238 } u;
3239 } *sec_info;
3241 /* Linked list of groups. */
3242 struct map_stub *group;
3244 /* Temp used when calculating TOC pointers. */
3245 bfd_vma toc_curr;
3246 bfd *toc_bfd;
3247 asection *toc_first_sec;
3249 /* Used when adding symbols. */
3250 struct ppc_link_hash_entry *dot_syms;
3252 /* Shortcuts to get to dynamic linker sections. */
3253 asection *glink;
3254 asection *global_entry;
3255 asection *sfpr;
3256 asection *pltlocal;
3257 asection *relpltlocal;
3258 asection *brlt;
3259 asection *relbrlt;
3260 asection *glink_eh_frame;
3262 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3263 struct ppc_link_hash_entry *tls_get_addr;
3264 struct ppc_link_hash_entry *tls_get_addr_fd;
3265 struct ppc_link_hash_entry *tga_desc;
3266 struct ppc_link_hash_entry *tga_desc_fd;
3267 struct map_stub *tga_group;
3269 /* The size of reliplt used by got entry relocs. */
3270 bfd_size_type got_reli_size;
3272 /* DT_RELR array of section/r_offset. */
3273 size_t relr_alloc;
3274 size_t relr_count;
3275 struct
3277 asection *sec;
3278 bfd_vma off;
3279 } *relr;
3281 /* Statistics. */
3282 unsigned long stub_count[ppc_stub_save_res];
3284 /* Number of stubs against global syms. */
3285 unsigned long stub_globals;
3287 /* Set if we're linking code with function descriptors. */
3288 unsigned int opd_abi:1;
3290 /* Support for multiple toc sections. */
3291 unsigned int do_multi_toc:1;
3292 unsigned int multi_toc_needed:1;
3293 unsigned int second_toc_pass:1;
3294 unsigned int do_toc_opt:1;
3296 /* Set if tls optimization is enabled. */
3297 unsigned int do_tls_opt:1;
3299 /* Set if inline plt calls should be converted to direct calls. */
3300 unsigned int can_convert_all_inline_plt:1;
3302 /* Set if a stub_offset changed. */
3303 unsigned int stub_changed:1;
3305 /* Set on error. */
3306 unsigned int stub_error:1;
3308 /* Whether func_desc_adjust needs to be run over symbols. */
3309 unsigned int need_func_desc_adj:1;
3311 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3312 unsigned int has_plt_localentry0:1;
3314 /* Whether calls are made via the PLT from NOTOC functions. */
3315 unsigned int notoc_plt:1;
3317 /* Whether any code linked seems to be Power10. */
3318 unsigned int has_power10_relocs:1;
3320 /* Incremented every time we size stubs. */
3321 unsigned int stub_iteration;
3323 /* After 20 iterations of stub sizing we no longer allow stubs to
3324 shrink. This is to break out of a pathological case where adding
3325 stubs or increasing their size on one iteration decreases section
3326 gaps (perhaps due to alignment), which then results in smaller
3327 stubs on the next iteration. */
3328 #define STUB_SHRINK_ITER 20
3331 /* Rename some of the generic section flags to better document how they
3332 are used here. */
3334 /* Nonzero if this section has TLS related relocations. */
3335 #define has_tls_reloc sec_flg0
3337 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3338 relocations. */
3339 #define nomark_tls_get_addr sec_flg1
3341 /* Nonzero if this section has any toc or got relocs. */
3342 #define has_toc_reloc sec_flg2
3344 /* Nonzero if this section has a call to another section that uses
3345 the toc or got. */
3346 #define makes_toc_func_call sec_flg3
3348 /* Recursion protection when determining above flag. */
3349 #define call_check_in_progress sec_flg4
3350 #define call_check_done sec_flg5
3352 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3354 #define ppc_hash_table(p) \
3355 ((is_elf_hash_table ((p)->hash) \
3356 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3357 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3359 #define ppc_stub_hash_lookup(table, string, create, copy) \
3360 ((struct ppc_stub_hash_entry *) \
3361 bfd_hash_lookup ((table), (string), (create), (copy)))
3363 #define ppc_branch_hash_lookup(table, string, create, copy) \
3364 ((struct ppc_branch_hash_entry *) \
3365 bfd_hash_lookup ((table), (string), (create), (copy)))
3367 /* Create an entry in the stub hash table. */
3369 static struct bfd_hash_entry *
3370 stub_hash_newfunc (struct bfd_hash_entry *entry,
3371 struct bfd_hash_table *table,
3372 const char *string)
3374 /* Allocate the structure if it has not already been allocated by a
3375 subclass. */
3376 if (entry == NULL)
3378 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3379 if (entry == NULL)
3380 return entry;
3383 /* Call the allocation method of the superclass. */
3384 entry = bfd_hash_newfunc (entry, table, string);
3385 if (entry != NULL)
3387 struct ppc_stub_hash_entry *eh;
3389 /* Initialize the local fields. */
3390 eh = (struct ppc_stub_hash_entry *) entry;
3391 eh->type.main = ppc_stub_none;
3392 eh->type.sub = ppc_stub_toc;
3393 eh->type.r2save = 0;
3394 eh->group = NULL;
3395 eh->stub_offset = 0;
3396 eh->target_value = 0;
3397 eh->target_section = NULL;
3398 eh->h = NULL;
3399 eh->plt_ent = NULL;
3400 eh->other = 0;
3403 return entry;
3406 /* Create an entry in the branch hash table. */
3408 static struct bfd_hash_entry *
3409 branch_hash_newfunc (struct bfd_hash_entry *entry,
3410 struct bfd_hash_table *table,
3411 const char *string)
3413 /* Allocate the structure if it has not already been allocated by a
3414 subclass. */
3415 if (entry == NULL)
3417 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3418 if (entry == NULL)
3419 return entry;
3422 /* Call the allocation method of the superclass. */
3423 entry = bfd_hash_newfunc (entry, table, string);
3424 if (entry != NULL)
3426 struct ppc_branch_hash_entry *eh;
3428 /* Initialize the local fields. */
3429 eh = (struct ppc_branch_hash_entry *) entry;
3430 eh->offset = 0;
3431 eh->iter = 0;
3434 return entry;
3437 /* Create an entry in a ppc64 ELF linker hash table. */
3439 static struct bfd_hash_entry *
3440 link_hash_newfunc (struct bfd_hash_entry *entry,
3441 struct bfd_hash_table *table,
3442 const char *string)
3444 /* Allocate the structure if it has not already been allocated by a
3445 subclass. */
3446 if (entry == NULL)
3448 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3449 if (entry == NULL)
3450 return entry;
3453 /* Call the allocation method of the superclass. */
3454 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3455 if (entry != NULL)
3457 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3459 memset (&eh->u.stub_cache, 0,
3460 (sizeof (struct ppc_link_hash_entry)
3461 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3463 /* When making function calls, old ABI code references function entry
3464 points (dot symbols), while new ABI code references the function
3465 descriptor symbol. We need to make any combination of reference and
3466 definition work together, without breaking archive linking.
3468 For a defined function "foo" and an undefined call to "bar":
3469 An old object defines "foo" and ".foo", references ".bar" (possibly
3470 "bar" too).
3471 A new object defines "foo" and references "bar".
3473 A new object thus has no problem with its undefined symbols being
3474 satisfied by definitions in an old object. On the other hand, the
3475 old object won't have ".bar" satisfied by a new object.
3477 Keep a list of newly added dot-symbols. */
3479 if (string[0] == '.')
3481 struct ppc_link_hash_table *htab;
3483 htab = (struct ppc_link_hash_table *) table;
3484 eh->u.next_dot_sym = htab->dot_syms;
3485 htab->dot_syms = eh;
3489 return entry;
3492 struct tocsave_entry
3494 asection *sec;
3495 bfd_vma offset;
3498 static hashval_t
3499 tocsave_htab_hash (const void *p)
3501 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3502 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3505 static int
3506 tocsave_htab_eq (const void *p1, const void *p2)
3508 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3509 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3510 return e1->sec == e2->sec && e1->offset == e2->offset;
3513 /* Destroy a ppc64 ELF linker hash table. */
3515 static void
3516 ppc64_elf_link_hash_table_free (bfd *obfd)
3518 struct ppc_link_hash_table *htab;
3520 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3521 if (htab->tocsave_htab)
3522 htab_delete (htab->tocsave_htab);
3523 bfd_hash_table_free (&htab->branch_hash_table);
3524 bfd_hash_table_free (&htab->stub_hash_table);
3525 _bfd_elf_link_hash_table_free (obfd);
3528 /* Create a ppc64 ELF linker hash table. */
3530 static struct bfd_link_hash_table *
3531 ppc64_elf_link_hash_table_create (bfd *abfd)
3533 struct ppc_link_hash_table *htab;
3534 size_t amt = sizeof (struct ppc_link_hash_table);
3536 htab = bfd_zmalloc (amt);
3537 if (htab == NULL)
3538 return NULL;
3540 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3541 sizeof (struct ppc_link_hash_entry),
3542 PPC64_ELF_DATA))
3544 free (htab);
3545 return NULL;
3548 /* Init the stub hash table too. */
3549 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3550 sizeof (struct ppc_stub_hash_entry)))
3552 _bfd_elf_link_hash_table_free (abfd);
3553 return NULL;
3556 /* And the branch hash table. */
3557 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3558 sizeof (struct ppc_branch_hash_entry)))
3560 bfd_hash_table_free (&htab->stub_hash_table);
3561 _bfd_elf_link_hash_table_free (abfd);
3562 return NULL;
3565 htab->tocsave_htab = htab_try_create (1024,
3566 tocsave_htab_hash,
3567 tocsave_htab_eq,
3568 NULL);
3569 if (htab->tocsave_htab == NULL)
3571 ppc64_elf_link_hash_table_free (abfd);
3572 return NULL;
3574 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3576 /* Initializing two fields of the union is just cosmetic. We really
3577 only care about glist, but when compiled on a 32-bit host the
3578 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3579 debugger inspection of these fields look nicer. */
3580 htab->elf.init_got_refcount.refcount = 0;
3581 htab->elf.init_got_refcount.glist = NULL;
3582 htab->elf.init_plt_refcount.refcount = 0;
3583 htab->elf.init_plt_refcount.glist = NULL;
3584 htab->elf.init_got_offset.offset = 0;
3585 htab->elf.init_got_offset.glist = NULL;
3586 htab->elf.init_plt_offset.offset = 0;
3587 htab->elf.init_plt_offset.glist = NULL;
3589 return &htab->elf.root;
3592 /* Create sections for linker generated code. */
3594 static bool
3595 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3597 struct ppc_link_hash_table *htab;
3598 flagword flags;
3600 htab = ppc_hash_table (info);
3602 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3603 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3604 if (htab->params->save_restore_funcs)
3606 /* Create .sfpr for code to save and restore fp regs. */
3607 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3608 flags);
3609 if (htab->sfpr == NULL
3610 || !bfd_set_section_alignment (htab->sfpr, 2))
3611 return false;
3614 if (bfd_link_relocatable (info))
3615 return true;
3617 /* Create .glink for lazy dynamic linking support. */
3618 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3619 flags);
3620 if (htab->glink == NULL
3621 || !bfd_set_section_alignment (htab->glink, 3))
3622 return false;
3624 /* The part of .glink used by global entry stubs, separate so that
3625 it can be aligned appropriately without affecting htab->glink. */
3626 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3627 flags);
3628 if (htab->global_entry == NULL
3629 || !bfd_set_section_alignment (htab->global_entry, 2))
3630 return false;
3632 if (!info->no_ld_generated_unwind_info)
3634 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3635 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3636 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3637 ".eh_frame",
3638 flags);
3639 if (htab->glink_eh_frame == NULL
3640 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3641 return false;
3644 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3645 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3646 if (htab->elf.iplt == NULL
3647 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3648 return false;
3650 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3651 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3652 htab->elf.irelplt
3653 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3654 if (htab->elf.irelplt == NULL
3655 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3656 return false;
3658 /* Create branch lookup table for plt_branch stubs. */
3659 flags = (SEC_ALLOC | SEC_LOAD
3660 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3661 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3662 flags);
3663 if (htab->brlt == NULL
3664 || !bfd_set_section_alignment (htab->brlt, 3))
3665 return false;
3667 /* Local plt entries, put in .branch_lt but a separate section for
3668 convenience. */
3669 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3670 flags);
3671 if (htab->pltlocal == NULL
3672 || !bfd_set_section_alignment (htab->pltlocal, 3))
3673 return false;
3675 if (!bfd_link_pic (info))
3676 return true;
3678 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3679 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3680 htab->relbrlt
3681 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3682 if (htab->relbrlt == NULL
3683 || !bfd_set_section_alignment (htab->relbrlt, 3))
3684 return false;
3686 htab->relpltlocal
3687 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3688 if (htab->relpltlocal == NULL
3689 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3690 return false;
3692 return true;
3695 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3697 bool
3698 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3699 struct ppc64_elf_params *params)
3701 struct ppc_link_hash_table *htab;
3703 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3705 /* Always hook our dynamic sections into the first bfd, which is the
3706 linker created stub bfd. This ensures that the GOT header is at
3707 the start of the output TOC section. */
3708 htab = ppc_hash_table (info);
3709 htab->elf.dynobj = params->stub_bfd;
3710 htab->params = params;
3712 return create_linkage_sections (htab->elf.dynobj, info);
3715 /* Build a name for an entry in the stub hash table. */
3717 static char *
3718 ppc_stub_name (const asection *input_section,
3719 const asection *sym_sec,
3720 const struct ppc_link_hash_entry *h,
3721 const Elf_Internal_Rela *rel)
3723 char *stub_name;
3724 ssize_t len;
3726 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3727 offsets from a sym as a branch target? In fact, we could
3728 probably assume the addend is always zero. */
3729 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3731 if (h)
3733 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3734 stub_name = bfd_malloc (len);
3735 if (stub_name == NULL)
3736 return stub_name;
3738 len = sprintf (stub_name, "%08x.%s+%x",
3739 input_section->id & 0xffffffff,
3740 h->elf.root.root.string,
3741 (int) rel->r_addend & 0xffffffff);
3743 else
3745 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3746 stub_name = bfd_malloc (len);
3747 if (stub_name == NULL)
3748 return stub_name;
3750 len = sprintf (stub_name, "%08x.%x:%x+%x",
3751 input_section->id & 0xffffffff,
3752 sym_sec->id & 0xffffffff,
3753 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3754 (int) rel->r_addend & 0xffffffff);
3756 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3757 stub_name[len - 2] = 0;
3758 return stub_name;
3761 /* If mixing power10 with non-power10 code and --power10-stubs is not
3762 specified (or is auto) then there may be multiple stub types for any
3763 given symbol. Up to three classes of stubs are stored in separate
3764 stub_hash_table entries having the same key string. The entries
3765 will always be adjacent on entry->root.next chain, even if hash
3766 table resizing occurs. This function selects the correct entry to
3767 use. */
3769 static struct ppc_stub_hash_entry *
3770 select_alt_stub (struct ppc_stub_hash_entry *entry,
3771 enum elf_ppc64_reloc_type r_type)
3773 enum ppc_stub_sub_type subt;
3775 switch (r_type)
3777 case R_PPC64_REL24_NOTOC:
3778 subt = ppc_stub_notoc;
3779 break;
3780 case R_PPC64_REL24_P9NOTOC:
3781 subt = ppc_stub_p9notoc;
3782 break;
3783 default:
3784 subt = ppc_stub_toc;
3785 break;
3788 while (entry != NULL && entry->type.sub != subt)
3790 const char *stub_name = entry->root.string;
3792 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3793 if (entry != NULL
3794 && entry->root.string != stub_name)
3795 entry = NULL;
3798 return entry;
3801 /* Look up an entry in the stub hash. Stub entries are cached because
3802 creating the stub name takes a bit of time. */
3804 static struct ppc_stub_hash_entry *
3805 ppc_get_stub_entry (const asection *input_section,
3806 const asection *sym_sec,
3807 struct ppc_link_hash_entry *h,
3808 const Elf_Internal_Rela *rel,
3809 struct ppc_link_hash_table *htab)
3811 struct ppc_stub_hash_entry *stub_entry;
3812 struct map_stub *group;
3814 /* If this input section is part of a group of sections sharing one
3815 stub section, then use the id of the first section in the group.
3816 Stub names need to include a section id, as there may well be
3817 more than one stub used to reach say, printf, and we need to
3818 distinguish between them. */
3819 group = htab->sec_info[input_section->id].u.group;
3820 if (group == NULL)
3821 return NULL;
3823 if (h != NULL && h->u.stub_cache != NULL
3824 && h->u.stub_cache->h == h
3825 && h->u.stub_cache->group == group)
3827 stub_entry = h->u.stub_cache;
3829 else
3831 char *stub_name;
3833 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3834 if (stub_name == NULL)
3835 return NULL;
3837 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3838 stub_name, false, false);
3839 if (h != NULL)
3840 h->u.stub_cache = stub_entry;
3842 free (stub_name);
3845 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3846 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3848 return stub_entry;
3851 /* Add a new stub entry to the stub hash. Not all fields of the new
3852 stub entry are initialised. */
3854 static struct ppc_stub_hash_entry *
3855 ppc_add_stub (const char *stub_name,
3856 asection *section,
3857 struct bfd_link_info *info)
3859 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3860 struct map_stub *group;
3861 asection *link_sec;
3862 asection *stub_sec;
3863 struct ppc_stub_hash_entry *stub_entry;
3865 group = htab->sec_info[section->id].u.group;
3866 link_sec = group->link_sec;
3867 stub_sec = group->stub_sec;
3868 if (stub_sec == NULL)
3870 size_t namelen;
3871 bfd_size_type len;
3872 char *s_name;
3874 namelen = strlen (link_sec->name);
3875 len = namelen + sizeof (STUB_SUFFIX);
3876 s_name = bfd_alloc (htab->params->stub_bfd, len);
3877 if (s_name == NULL)
3878 return NULL;
3880 memcpy (s_name, link_sec->name, namelen);
3881 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3882 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3883 if (stub_sec == NULL)
3884 return NULL;
3885 group->stub_sec = stub_sec;
3888 /* Enter this entry into the linker stub hash table. */
3889 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3890 true, false);
3891 if (stub_entry == NULL)
3893 /* xgettext:c-format */
3894 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3895 section->owner, stub_name);
3896 return NULL;
3899 stub_entry->group = group;
3900 stub_entry->stub_offset = 0;
3901 return stub_entry;
3904 /* A stub has already been created, but it may not be the required
3905 type. We shouldn't be transitioning from plt_call to long_branch
3906 stubs or vice versa, but we might be upgrading from plt_call to
3907 plt_call with r2save for example. */
3909 static bool
3910 ppc_merge_stub (struct ppc_link_hash_table *htab,
3911 struct ppc_stub_hash_entry *stub_entry,
3912 struct ppc_stub_type stub_type,
3913 enum elf_ppc64_reloc_type r_type)
3915 struct ppc_stub_type old_type = stub_entry->type;
3917 if (old_type.main == ppc_stub_save_res)
3918 return true;
3920 if (htab->params->power10_stubs == -1)
3922 /* For --power10-stubs=auto, don't merge _notoc and other
3923 varieties of stubs. */
3924 struct ppc_stub_hash_entry *alt_stub;
3926 alt_stub = select_alt_stub (stub_entry, r_type);
3927 if (alt_stub == NULL)
3929 alt_stub = ((struct ppc_stub_hash_entry *)
3930 stub_hash_newfunc (NULL,
3931 &htab->stub_hash_table,
3932 stub_entry->root.string));
3933 if (alt_stub == NULL)
3934 return false;
3936 *alt_stub = *stub_entry;
3937 stub_entry->root.next = &alt_stub->root;
3939 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3940 Not that it matters, this just puts smaller stubs first. */
3941 if (stub_type.sub == ppc_stub_notoc)
3942 alt_stub = stub_entry;
3943 else if (stub_type.sub == ppc_stub_p9notoc
3944 && alt_stub->root.next
3945 && alt_stub->root.next->string == alt_stub->root.string)
3947 struct ppc_stub_hash_entry *next
3948 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3949 alt_stub->type = next->type;
3950 alt_stub = next;
3952 alt_stub->type = stub_type;
3953 return true;
3955 stub_entry = alt_stub;
3958 old_type = stub_entry->type;
3959 if (old_type.main == ppc_stub_plt_branch)
3960 old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
3962 if (old_type.main != stub_type.main
3963 || (old_type.sub != stub_type.sub
3964 && old_type.sub != ppc_stub_toc
3965 && stub_type.sub != ppc_stub_toc))
3966 abort ();
3968 stub_entry->type.sub |= stub_type.sub;
3969 stub_entry->type.r2save |= stub_type.r2save;
3970 return true;
3973 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3974 not already done. */
3976 static bool
3977 create_got_section (bfd *abfd, struct bfd_link_info *info)
3979 asection *got, *relgot;
3980 flagword flags;
3981 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3983 if (!is_ppc64_elf (abfd))
3984 return false;
3985 if (htab == NULL)
3986 return false;
3988 if (!htab->elf.sgot
3989 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3990 return false;
3992 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3993 | SEC_LINKER_CREATED);
3995 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3996 if (!got
3997 || !bfd_set_section_alignment (got, 3))
3998 return false;
4000 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4001 flags | SEC_READONLY);
4002 if (!relgot
4003 || !bfd_set_section_alignment (relgot, 3))
4004 return false;
4006 ppc64_elf_tdata (abfd)->got = got;
4007 ppc64_elf_tdata (abfd)->relgot = relgot;
4008 return true;
4011 /* Follow indirect and warning symbol links. */
4013 static inline struct bfd_link_hash_entry *
4014 follow_link (struct bfd_link_hash_entry *h)
4016 while (h->type == bfd_link_hash_indirect
4017 || h->type == bfd_link_hash_warning)
4018 h = h->u.i.link;
4019 return h;
4022 static inline struct elf_link_hash_entry *
4023 elf_follow_link (struct elf_link_hash_entry *h)
4025 return (struct elf_link_hash_entry *) follow_link (&h->root);
4028 static inline struct ppc_link_hash_entry *
4029 ppc_follow_link (struct ppc_link_hash_entry *h)
4031 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4034 /* Merge PLT info on FROM with that on TO. */
4036 static void
4037 move_plt_plist (struct ppc_link_hash_entry *from,
4038 struct ppc_link_hash_entry *to)
4040 if (from->elf.plt.plist != NULL)
4042 if (to->elf.plt.plist != NULL)
4044 struct plt_entry **entp;
4045 struct plt_entry *ent;
4047 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4049 struct plt_entry *dent;
4051 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4052 if (dent->addend == ent->addend)
4054 dent->plt.refcount += ent->plt.refcount;
4055 *entp = ent->next;
4056 break;
4058 if (dent == NULL)
4059 entp = &ent->next;
4061 *entp = to->elf.plt.plist;
4064 to->elf.plt.plist = from->elf.plt.plist;
4065 from->elf.plt.plist = NULL;
4069 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4071 static void
4072 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4073 struct elf_link_hash_entry *dir,
4074 struct elf_link_hash_entry *ind)
4076 struct ppc_link_hash_entry *edir, *eind;
4078 edir = ppc_elf_hash_entry (dir);
4079 eind = ppc_elf_hash_entry (ind);
4081 edir->is_func |= eind->is_func;
4082 edir->is_func_descriptor |= eind->is_func_descriptor;
4083 edir->tls_mask |= eind->tls_mask;
4084 if (eind->oh != NULL)
4085 edir->oh = ppc_follow_link (eind->oh);
4087 if (edir->elf.versioned != versioned_hidden)
4088 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4089 edir->elf.ref_regular |= eind->elf.ref_regular;
4090 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4091 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4092 edir->elf.needs_plt |= eind->elf.needs_plt;
4093 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4095 /* If we were called to copy over info for a weak sym, don't copy
4096 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4097 in order to simplify readonly_dynrelocs and save a field in the
4098 symbol hash entry, but that means dyn_relocs can't be used in any
4099 tests about a specific symbol, or affect other symbol flags which
4100 are then tested. */
4101 if (eind->elf.root.type != bfd_link_hash_indirect)
4102 return;
4104 /* Copy over any dynamic relocs we may have on the indirect sym. */
4105 if (ind->dyn_relocs != NULL)
4107 if (dir->dyn_relocs != NULL)
4109 struct ppc_dyn_relocs **pp;
4110 struct ppc_dyn_relocs *p;
4112 /* Add reloc counts against the indirect sym to the direct sym
4113 list. Merge any entries against the same section. */
4114 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4115 (p = *pp) != NULL;
4118 struct ppc_dyn_relocs *q;
4120 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4121 q != NULL;
4122 q = q->next)
4123 if (q->sec == p->sec)
4125 q->count += p->count;
4126 q->pc_count += p->pc_count;
4127 q->rel_count += p->rel_count;
4128 *pp = p->next;
4129 break;
4131 if (q == NULL)
4132 pp = &p->next;
4134 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4137 dir->dyn_relocs = ind->dyn_relocs;
4138 ind->dyn_relocs = NULL;
4141 /* Copy over got entries that we may have already seen to the
4142 symbol which just became indirect. */
4143 if (eind->elf.got.glist != NULL)
4145 if (edir->elf.got.glist != NULL)
4147 struct got_entry **entp;
4148 struct got_entry *ent;
4150 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4152 struct got_entry *dent;
4154 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4155 if (dent->addend == ent->addend
4156 && dent->owner == ent->owner
4157 && dent->tls_type == ent->tls_type)
4159 dent->got.refcount += ent->got.refcount;
4160 *entp = ent->next;
4161 break;
4163 if (dent == NULL)
4164 entp = &ent->next;
4166 *entp = edir->elf.got.glist;
4169 edir->elf.got.glist = eind->elf.got.glist;
4170 eind->elf.got.glist = NULL;
4173 /* And plt entries. */
4174 move_plt_plist (eind, edir);
4176 if (eind->elf.dynindx != -1)
4178 if (edir->elf.dynindx != -1)
4179 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4180 edir->elf.dynstr_index);
4181 edir->elf.dynindx = eind->elf.dynindx;
4182 edir->elf.dynstr_index = eind->elf.dynstr_index;
4183 eind->elf.dynindx = -1;
4184 eind->elf.dynstr_index = 0;
4188 /* Find the function descriptor hash entry from the given function code
4189 hash entry FH. Link the entries via their OH fields. */
4191 static struct ppc_link_hash_entry *
4192 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4194 struct ppc_link_hash_entry *fdh = fh->oh;
4196 if (fdh == NULL)
4198 const char *fd_name = fh->elf.root.root.string + 1;
4200 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4201 false, false, false));
4202 if (fdh == NULL)
4203 return fdh;
4205 fdh->is_func_descriptor = 1;
4206 fdh->oh = fh;
4207 fh->is_func = 1;
4208 fh->oh = fdh;
4211 fdh = ppc_follow_link (fdh);
4212 fdh->is_func_descriptor = 1;
4213 fdh->oh = fh;
4214 return fdh;
4217 /* Make a fake function descriptor sym for the undefined code sym FH. */
4219 static struct ppc_link_hash_entry *
4220 make_fdh (struct bfd_link_info *info,
4221 struct ppc_link_hash_entry *fh)
4223 bfd *abfd = fh->elf.root.u.undef.abfd;
4224 struct bfd_link_hash_entry *bh = NULL;
4225 struct ppc_link_hash_entry *fdh;
4226 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4227 ? BSF_WEAK
4228 : BSF_GLOBAL);
4230 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4231 fh->elf.root.root.string + 1,
4232 flags, bfd_und_section_ptr, 0,
4233 NULL, false, false, &bh))
4234 return NULL;
4236 fdh = (struct ppc_link_hash_entry *) bh;
4237 fdh->elf.non_elf = 0;
4238 fdh->fake = 1;
4239 fdh->is_func_descriptor = 1;
4240 fdh->oh = fh;
4241 fh->is_func = 1;
4242 fh->oh = fdh;
4243 return fdh;
4246 /* Fix function descriptor symbols defined in .opd sections to be
4247 function type. */
4249 static bool
4250 ppc64_elf_add_symbol_hook (bfd *ibfd,
4251 struct bfd_link_info *info,
4252 Elf_Internal_Sym *isym,
4253 const char **name,
4254 flagword *flags ATTRIBUTE_UNUSED,
4255 asection **sec,
4256 bfd_vma *value)
4258 if (*sec != NULL
4259 && strcmp ((*sec)->name, ".opd") == 0)
4261 asection *code_sec;
4263 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4264 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4265 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4267 /* If the symbol is a function defined in .opd, and the function
4268 code is in a discarded group, let it appear to be undefined. */
4269 if (!bfd_link_relocatable (info)
4270 && (*sec)->reloc_count != 0
4271 && opd_entry_value (*sec, *value, &code_sec, NULL,
4272 false) != (bfd_vma) -1
4273 && discarded_section (code_sec))
4275 *sec = bfd_und_section_ptr;
4276 isym->st_shndx = SHN_UNDEF;
4279 else if (*sec != NULL
4280 && strcmp ((*sec)->name, ".toc") == 0
4281 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4283 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4284 if (htab != NULL)
4285 htab->params->object_in_toc = 1;
4288 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4290 if (abiversion (ibfd) == 0)
4291 set_abiversion (ibfd, 2);
4292 else if (abiversion (ibfd) == 1)
4294 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4295 " for ABI version 1"), *name);
4296 bfd_set_error (bfd_error_bad_value);
4297 return false;
4301 return true;
4304 /* Merge non-visibility st_other attributes: local entry point. */
4306 static void
4307 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4308 unsigned int st_other,
4309 bool definition,
4310 bool dynamic)
4312 if (definition && (!dynamic || !h->def_regular))
4313 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4314 | ELF_ST_VISIBILITY (h->other));
4317 /* Hook called on merging a symbol. We use this to clear "fake" since
4318 we now have a real symbol. */
4320 static bool
4321 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4322 const Elf_Internal_Sym *isym,
4323 asection **psec ATTRIBUTE_UNUSED,
4324 bool newdef ATTRIBUTE_UNUSED,
4325 bool olddef ATTRIBUTE_UNUSED,
4326 bfd *oldbfd ATTRIBUTE_UNUSED,
4327 const asection *oldsec ATTRIBUTE_UNUSED)
4329 ppc_elf_hash_entry (h)->fake = 0;
4330 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4331 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4332 return true;
4335 /* This function makes an old ABI object reference to ".bar" cause the
4336 inclusion of a new ABI object archive that defines "bar".
4337 NAME is a symbol defined in an archive. Return a symbol in the hash
4338 table that might be satisfied by the archive symbols. */
4340 static struct bfd_link_hash_entry *
4341 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4342 struct bfd_link_info *info,
4343 const char *name)
4345 struct bfd_link_hash_entry *h;
4346 char *dot_name;
4347 size_t len;
4349 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4350 if (h != NULL
4351 && ppc_hash_table (info) != NULL
4352 /* Don't return this sym if it is a fake function descriptor
4353 created by add_symbol_adjust. */
4354 && !((struct ppc_link_hash_entry *) h)->fake)
4355 return h;
4357 if (name[0] == '.')
4358 return h;
4360 len = strlen (name);
4361 dot_name = bfd_alloc (abfd, len + 2);
4362 if (dot_name == NULL)
4363 return (struct bfd_link_hash_entry *) -1;
4364 dot_name[0] = '.';
4365 memcpy (dot_name + 1, name, len + 1);
4366 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4367 bfd_release (abfd, dot_name);
4368 if (h != NULL)
4369 return h;
4371 if (strcmp (name, "__tls_get_addr_opt") == 0)
4372 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4373 return h;
4376 /* This function satisfies all old ABI object references to ".bar" if a
4377 new ABI object defines "bar". Well, at least, undefined dot symbols
4378 are made weak. This stops later archive searches from including an
4379 object if we already have a function descriptor definition. It also
4380 prevents the linker complaining about undefined symbols.
4381 We also check and correct mismatched symbol visibility here. The
4382 most restrictive visibility of the function descriptor and the
4383 function entry symbol is used. */
4385 static bool
4386 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4388 struct ppc_link_hash_table *htab;
4389 struct ppc_link_hash_entry *fdh;
4391 if (eh->elf.root.type == bfd_link_hash_warning)
4392 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4394 if (eh->elf.root.type == bfd_link_hash_indirect)
4395 return true;
4397 if (eh->elf.root.root.string[0] != '.')
4398 abort ();
4400 htab = ppc_hash_table (info);
4401 if (htab == NULL)
4402 return false;
4404 fdh = lookup_fdh (eh, htab);
4405 if (fdh == NULL
4406 && !bfd_link_relocatable (info)
4407 && (eh->elf.root.type == bfd_link_hash_undefined
4408 || eh->elf.root.type == bfd_link_hash_undefweak)
4409 && eh->elf.ref_regular)
4411 /* Make an undefined function descriptor sym, in order to
4412 pull in an --as-needed shared lib. Archives are handled
4413 elsewhere. */
4414 fdh = make_fdh (info, eh);
4415 if (fdh == NULL)
4416 return false;
4419 if (fdh != NULL)
4421 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4422 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4424 /* Make both descriptor and entry symbol have the most
4425 constraining visibility of either symbol. */
4426 if (entry_vis < descr_vis)
4427 fdh->elf.other += entry_vis - descr_vis;
4428 else if (entry_vis > descr_vis)
4429 eh->elf.other += descr_vis - entry_vis;
4431 /* Propagate reference flags from entry symbol to function
4432 descriptor symbol. */
4433 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4434 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4435 fdh->elf.ref_regular |= eh->elf.ref_regular;
4436 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4438 if (!fdh->elf.forced_local
4439 && fdh->elf.dynindx == -1
4440 && fdh->elf.versioned != versioned_hidden
4441 && (bfd_link_dll (info)
4442 || fdh->elf.def_dynamic
4443 || fdh->elf.ref_dynamic)
4444 && (eh->elf.ref_regular
4445 || eh->elf.def_regular))
4447 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4448 return false;
4452 return true;
4455 /* Set up opd section info and abiversion for IBFD, and process list
4456 of dot-symbols we made in link_hash_newfunc. */
4458 static bool
4459 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4461 struct ppc_link_hash_table *htab;
4462 struct ppc_link_hash_entry **p, *eh;
4463 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4465 if (opd != NULL && opd->size != 0)
4467 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4468 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4470 if (abiversion (ibfd) == 0)
4471 set_abiversion (ibfd, 1);
4472 else if (abiversion (ibfd) >= 2)
4474 /* xgettext:c-format */
4475 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4476 ibfd, abiversion (ibfd));
4477 bfd_set_error (bfd_error_bad_value);
4478 return false;
4482 if (is_ppc64_elf (info->output_bfd))
4484 /* For input files without an explicit abiversion in e_flags
4485 we should have flagged any with symbol st_other bits set
4486 as ELFv1 and above flagged those with .opd as ELFv2.
4487 Set the output abiversion if not yet set, and for any input
4488 still ambiguous, take its abiversion from the output.
4489 Differences in ABI are reported later. */
4490 if (abiversion (info->output_bfd) == 0)
4491 set_abiversion (info->output_bfd, abiversion (ibfd));
4492 else if (abiversion (ibfd) == 0)
4493 set_abiversion (ibfd, abiversion (info->output_bfd));
4496 htab = ppc_hash_table (info);
4497 if (htab == NULL)
4498 return true;
4500 if (opd != NULL && opd->size != 0
4501 && (ibfd->flags & DYNAMIC) == 0
4502 && (opd->flags & SEC_RELOC) != 0
4503 && opd->reloc_count != 0
4504 && !bfd_is_abs_section (opd->output_section)
4505 && info->gc_sections)
4507 /* Garbage collection needs some extra help with .opd sections.
4508 We don't want to necessarily keep everything referenced by
4509 relocs in .opd, as that would keep all functions. Instead,
4510 if we reference an .opd symbol (a function descriptor), we
4511 want to keep the function code symbol's section. This is
4512 easy for global symbols, but for local syms we need to keep
4513 information about the associated function section. */
4514 bfd_size_type amt;
4515 asection **opd_sym_map;
4516 Elf_Internal_Shdr *symtab_hdr;
4517 Elf_Internal_Rela *relocs, *rel_end, *rel;
4519 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4520 opd_sym_map = bfd_zalloc (ibfd, amt);
4521 if (opd_sym_map == NULL)
4522 return false;
4523 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4524 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4525 info->keep_memory);
4526 if (relocs == NULL)
4527 return false;
4528 symtab_hdr = &elf_symtab_hdr (ibfd);
4529 rel_end = relocs + opd->reloc_count - 1;
4530 for (rel = relocs; rel < rel_end; rel++)
4532 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4533 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4535 if (r_type == R_PPC64_ADDR64
4536 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4537 && r_symndx < symtab_hdr->sh_info)
4539 Elf_Internal_Sym *isym;
4540 asection *s;
4542 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4543 r_symndx);
4544 if (isym == NULL)
4546 if (elf_section_data (opd)->relocs != relocs)
4547 free (relocs);
4548 return false;
4551 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4552 if (s != NULL && s != opd)
4553 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4556 if (elf_section_data (opd)->relocs != relocs)
4557 free (relocs);
4560 p = &htab->dot_syms;
4561 while ((eh = *p) != NULL)
4563 *p = NULL;
4564 if (&eh->elf == htab->elf.hgot)
4566 else if (htab->elf.hgot == NULL
4567 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4568 htab->elf.hgot = &eh->elf;
4569 else if (abiversion (ibfd) <= 1)
4571 htab->need_func_desc_adj = 1;
4572 if (!add_symbol_adjust (eh, info))
4573 return false;
4575 p = &eh->u.next_dot_sym;
4577 return true;
4580 /* Undo hash table changes when an --as-needed input file is determined
4581 not to be needed. */
4583 static bool
4584 ppc64_elf_notice_as_needed (bfd *ibfd,
4585 struct bfd_link_info *info,
4586 enum notice_asneeded_action act)
4588 if (act == notice_not_needed)
4590 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4592 if (htab == NULL)
4593 return false;
4595 htab->dot_syms = NULL;
4597 return _bfd_elf_notice_as_needed (ibfd, info, act);
4600 /* If --just-symbols against a final linked binary, then assume we need
4601 toc adjusting stubs when calling functions defined there. */
4603 static void
4604 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4606 if ((sec->flags & SEC_CODE) != 0
4607 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4608 && is_ppc64_elf (sec->owner))
4610 if (abiversion (sec->owner) >= 2
4611 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4612 sec->has_toc_reloc = 1;
4614 _bfd_elf_link_just_syms (sec, info);
4617 static struct plt_entry **
4618 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4619 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4621 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4622 struct plt_entry **local_plt;
4623 unsigned char *local_got_tls_masks;
4625 if (local_got_ents == NULL)
4627 bfd_size_type size = symtab_hdr->sh_info;
4629 size *= (sizeof (*local_got_ents)
4630 + sizeof (*local_plt)
4631 + sizeof (*local_got_tls_masks));
4632 local_got_ents = bfd_zalloc (abfd, size);
4633 if (local_got_ents == NULL)
4634 return NULL;
4635 elf_local_got_ents (abfd) = local_got_ents;
4638 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4640 struct got_entry *ent;
4642 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4643 if (ent->addend == r_addend
4644 && ent->owner == abfd
4645 && ent->tls_type == tls_type)
4646 break;
4647 if (ent == NULL)
4649 size_t amt = sizeof (*ent);
4650 ent = bfd_alloc (abfd, amt);
4651 if (ent == NULL)
4652 return false;
4653 ent->next = local_got_ents[r_symndx];
4654 ent->addend = r_addend;
4655 ent->owner = abfd;
4656 ent->tls_type = tls_type;
4657 ent->is_indirect = false;
4658 ent->got.refcount = 0;
4659 local_got_ents[r_symndx] = ent;
4661 ent->got.refcount += 1;
4664 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4665 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4666 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4668 return local_plt + r_symndx;
4671 static bool
4672 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4674 struct plt_entry *ent;
4676 for (ent = *plist; ent != NULL; ent = ent->next)
4677 if (ent->addend == addend)
4678 break;
4679 if (ent == NULL)
4681 size_t amt = sizeof (*ent);
4682 ent = bfd_alloc (abfd, amt);
4683 if (ent == NULL)
4684 return false;
4685 ent->next = *plist;
4686 ent->addend = addend;
4687 ent->plt.refcount = 0;
4688 *plist = ent;
4690 ent->plt.refcount += 1;
4691 return true;
4694 static bool
4695 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4697 return (r_type == R_PPC64_REL24
4698 || r_type == R_PPC64_REL24_NOTOC
4699 || r_type == R_PPC64_REL24_P9NOTOC
4700 || r_type == R_PPC64_REL14
4701 || r_type == R_PPC64_REL14_BRTAKEN
4702 || r_type == R_PPC64_REL14_BRNTAKEN
4703 || r_type == R_PPC64_ADDR24
4704 || r_type == R_PPC64_ADDR14
4705 || r_type == R_PPC64_ADDR14_BRTAKEN
4706 || r_type == R_PPC64_ADDR14_BRNTAKEN
4707 || r_type == R_PPC64_PLTCALL
4708 || r_type == R_PPC64_PLTCALL_NOTOC);
4711 /* Relocs on inline plt call sequence insns prior to the call. */
4713 static bool
4714 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4716 return (r_type == R_PPC64_PLT16_HA
4717 || r_type == R_PPC64_PLT16_HI
4718 || r_type == R_PPC64_PLT16_LO
4719 || r_type == R_PPC64_PLT16_LO_DS
4720 || r_type == R_PPC64_PLT_PCREL34
4721 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4722 || r_type == R_PPC64_PLTSEQ
4723 || r_type == R_PPC64_PLTSEQ_NOTOC);
4726 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4727 relocs, return true for those that operate on a dword. */
4729 static bool
4730 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4732 return (r_type == R_PPC64_PLT_PCREL34
4733 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4734 || r_type == R_PPC64_PLTCALL);
4737 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4738 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4740 static bool
4741 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4743 return offset <= sec->size && size <= sec->size - offset;
4746 /* Look through the relocs for a section during the first phase, and
4747 calculate needed space in the global offset table, procedure
4748 linkage table, and dynamic reloc sections. */
4750 static bool
4751 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4752 asection *sec, const Elf_Internal_Rela *relocs)
4754 struct ppc_link_hash_table *htab;
4755 Elf_Internal_Shdr *symtab_hdr;
4756 struct elf_link_hash_entry **sym_hashes;
4757 const Elf_Internal_Rela *rel;
4758 const Elf_Internal_Rela *rel_end;
4759 asection *sreloc;
4760 struct elf_link_hash_entry *tga, *dottga;
4761 bool is_opd;
4763 if (bfd_link_relocatable (info))
4764 return true;
4766 BFD_ASSERT (is_ppc64_elf (abfd));
4768 htab = ppc_hash_table (info);
4769 if (htab == NULL)
4770 return false;
4772 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4773 false, false, true);
4774 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4775 false, false, true);
4776 symtab_hdr = &elf_symtab_hdr (abfd);
4777 sym_hashes = elf_sym_hashes (abfd);
4778 sreloc = NULL;
4779 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4780 rel_end = relocs + sec->reloc_count;
4781 for (rel = relocs; rel < rel_end; rel++)
4783 unsigned long r_symndx;
4784 struct elf_link_hash_entry *h;
4785 Elf_Internal_Sym *isym;
4786 enum elf_ppc64_reloc_type r_type;
4787 int tls_type;
4788 struct _ppc64_elf_section_data *ppc64_sec;
4789 struct plt_entry **ifunc, **plt_list;
4791 r_symndx = ELF64_R_SYM (rel->r_info);
4792 if (r_symndx < symtab_hdr->sh_info)
4794 h = NULL;
4795 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4796 if (isym == NULL)
4797 return false;
4799 else
4801 isym = NULL;
4802 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4803 h = elf_follow_link (h);
4805 if (h == htab->elf.hgot)
4806 sec->has_toc_reloc = 1;
4809 r_type = ELF64_R_TYPE (rel->r_info);
4810 switch (r_type)
4812 case R_PPC64_D34:
4813 case R_PPC64_D34_LO:
4814 case R_PPC64_D34_HI30:
4815 case R_PPC64_D34_HA30:
4816 case R_PPC64_D28:
4817 case R_PPC64_TPREL34:
4818 case R_PPC64_DTPREL34:
4819 case R_PPC64_PCREL34:
4820 case R_PPC64_GOT_PCREL34:
4821 case R_PPC64_GOT_TLSGD_PCREL34:
4822 case R_PPC64_GOT_TLSLD_PCREL34:
4823 case R_PPC64_GOT_TPREL_PCREL34:
4824 case R_PPC64_GOT_DTPREL_PCREL34:
4825 case R_PPC64_PLT_PCREL34:
4826 case R_PPC64_PLT_PCREL34_NOTOC:
4827 case R_PPC64_PCREL28:
4828 htab->has_power10_relocs = 1;
4829 break;
4830 default:
4831 break;
4834 switch (r_type)
4836 case R_PPC64_PLT16_HA:
4837 case R_PPC64_GOT_TLSLD16_HA:
4838 case R_PPC64_GOT_TLSGD16_HA:
4839 case R_PPC64_GOT_TPREL16_HA:
4840 case R_PPC64_GOT_DTPREL16_HA:
4841 case R_PPC64_GOT16_HA:
4842 case R_PPC64_TOC16_HA:
4843 case R_PPC64_PLT16_LO:
4844 case R_PPC64_PLT16_LO_DS:
4845 case R_PPC64_GOT_TLSLD16_LO:
4846 case R_PPC64_GOT_TLSGD16_LO:
4847 case R_PPC64_GOT_TPREL16_LO_DS:
4848 case R_PPC64_GOT_DTPREL16_LO_DS:
4849 case R_PPC64_GOT16_LO:
4850 case R_PPC64_GOT16_LO_DS:
4851 case R_PPC64_TOC16_LO:
4852 case R_PPC64_TOC16_LO_DS:
4853 case R_PPC64_GOT_PCREL34:
4854 ppc64_elf_tdata (abfd)->has_optrel = 1;
4855 ppc64_elf_section_data (sec)->has_optrel = 1;
4856 break;
4857 default:
4858 break;
4861 ifunc = NULL;
4862 if (h != NULL)
4864 if (h->type == STT_GNU_IFUNC)
4866 h->needs_plt = 1;
4867 ifunc = &h->plt.plist;
4870 else
4872 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4874 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4875 rel->r_addend,
4876 NON_GOT | PLT_IFUNC);
4877 if (ifunc == NULL)
4878 return false;
4882 tls_type = 0;
4883 switch (r_type)
4885 case R_PPC64_TLSGD:
4886 case R_PPC64_TLSLD:
4887 /* These special tls relocs tie a call to __tls_get_addr with
4888 its parameter symbol. */
4889 if (h != NULL)
4890 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4891 else
4892 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4893 rel->r_addend,
4894 NON_GOT | TLS_TLS | TLS_MARK))
4895 return false;
4896 sec->has_tls_reloc = 1;
4897 break;
4899 case R_PPC64_GOT_TLSLD16:
4900 case R_PPC64_GOT_TLSLD16_LO:
4901 case R_PPC64_GOT_TLSLD16_HI:
4902 case R_PPC64_GOT_TLSLD16_HA:
4903 case R_PPC64_GOT_TLSLD_PCREL34:
4904 tls_type = TLS_TLS | TLS_LD;
4905 goto dogottls;
4907 case R_PPC64_GOT_TLSGD16:
4908 case R_PPC64_GOT_TLSGD16_LO:
4909 case R_PPC64_GOT_TLSGD16_HI:
4910 case R_PPC64_GOT_TLSGD16_HA:
4911 case R_PPC64_GOT_TLSGD_PCREL34:
4912 tls_type = TLS_TLS | TLS_GD;
4913 goto dogottls;
4915 case R_PPC64_GOT_TPREL16_DS:
4916 case R_PPC64_GOT_TPREL16_LO_DS:
4917 case R_PPC64_GOT_TPREL16_HI:
4918 case R_PPC64_GOT_TPREL16_HA:
4919 case R_PPC64_GOT_TPREL_PCREL34:
4920 if (bfd_link_dll (info))
4921 info->flags |= DF_STATIC_TLS;
4922 tls_type = TLS_TLS | TLS_TPREL;
4923 goto dogottls;
4925 case R_PPC64_GOT_DTPREL16_DS:
4926 case R_PPC64_GOT_DTPREL16_LO_DS:
4927 case R_PPC64_GOT_DTPREL16_HI:
4928 case R_PPC64_GOT_DTPREL16_HA:
4929 case R_PPC64_GOT_DTPREL_PCREL34:
4930 tls_type = TLS_TLS | TLS_DTPREL;
4931 dogottls:
4932 sec->has_tls_reloc = 1;
4933 goto dogot;
4935 case R_PPC64_GOT16:
4936 case R_PPC64_GOT16_LO:
4937 case R_PPC64_GOT16_HI:
4938 case R_PPC64_GOT16_HA:
4939 case R_PPC64_GOT16_DS:
4940 case R_PPC64_GOT16_LO_DS:
4941 case R_PPC64_GOT_PCREL34:
4942 dogot:
4943 /* This symbol requires a global offset table entry. */
4944 sec->has_toc_reloc = 1;
4945 if (r_type == R_PPC64_GOT_TLSLD16
4946 || r_type == R_PPC64_GOT_TLSGD16
4947 || r_type == R_PPC64_GOT_TPREL16_DS
4948 || r_type == R_PPC64_GOT_DTPREL16_DS
4949 || r_type == R_PPC64_GOT16
4950 || r_type == R_PPC64_GOT16_DS)
4952 htab->do_multi_toc = 1;
4953 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4956 if (ppc64_elf_tdata (abfd)->got == NULL
4957 && !create_got_section (abfd, info))
4958 return false;
4960 if (h != NULL)
4962 struct ppc_link_hash_entry *eh;
4963 struct got_entry *ent;
4965 eh = ppc_elf_hash_entry (h);
4966 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4967 if (ent->addend == rel->r_addend
4968 && ent->owner == abfd
4969 && ent->tls_type == tls_type)
4970 break;
4971 if (ent == NULL)
4973 size_t amt = sizeof (*ent);
4974 ent = bfd_alloc (abfd, amt);
4975 if (ent == NULL)
4976 return false;
4977 ent->next = eh->elf.got.glist;
4978 ent->addend = rel->r_addend;
4979 ent->owner = abfd;
4980 ent->tls_type = tls_type;
4981 ent->is_indirect = false;
4982 ent->got.refcount = 0;
4983 eh->elf.got.glist = ent;
4985 ent->got.refcount += 1;
4986 eh->tls_mask |= tls_type;
4988 else
4989 /* This is a global offset table entry for a local symbol. */
4990 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4991 rel->r_addend, tls_type))
4992 return false;
4993 break;
4995 case R_PPC64_PLT16_HA:
4996 case R_PPC64_PLT16_HI:
4997 case R_PPC64_PLT16_LO:
4998 case R_PPC64_PLT16_LO_DS:
4999 case R_PPC64_PLT_PCREL34:
5000 case R_PPC64_PLT_PCREL34_NOTOC:
5001 case R_PPC64_PLT32:
5002 case R_PPC64_PLT64:
5003 /* This symbol requires a procedure linkage table entry. */
5004 plt_list = ifunc;
5005 if (h != NULL)
5007 h->needs_plt = 1;
5008 if (h->root.root.string[0] == '.'
5009 && h->root.root.string[1] != '\0')
5010 ppc_elf_hash_entry (h)->is_func = 1;
5011 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5012 plt_list = &h->plt.plist;
5014 if (plt_list == NULL)
5015 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5016 rel->r_addend,
5017 NON_GOT | PLT_KEEP);
5018 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5019 return false;
5020 break;
5022 /* The following relocations don't need to propagate the
5023 relocation if linking a shared object since they are
5024 section relative. */
5025 case R_PPC64_SECTOFF:
5026 case R_PPC64_SECTOFF_LO:
5027 case R_PPC64_SECTOFF_HI:
5028 case R_PPC64_SECTOFF_HA:
5029 case R_PPC64_SECTOFF_DS:
5030 case R_PPC64_SECTOFF_LO_DS:
5031 case R_PPC64_DTPREL16:
5032 case R_PPC64_DTPREL16_LO:
5033 case R_PPC64_DTPREL16_HI:
5034 case R_PPC64_DTPREL16_HA:
5035 case R_PPC64_DTPREL16_DS:
5036 case R_PPC64_DTPREL16_LO_DS:
5037 case R_PPC64_DTPREL16_HIGH:
5038 case R_PPC64_DTPREL16_HIGHA:
5039 case R_PPC64_DTPREL16_HIGHER:
5040 case R_PPC64_DTPREL16_HIGHERA:
5041 case R_PPC64_DTPREL16_HIGHEST:
5042 case R_PPC64_DTPREL16_HIGHESTA:
5043 break;
5045 /* Nor do these. */
5046 case R_PPC64_REL16:
5047 case R_PPC64_REL16_LO:
5048 case R_PPC64_REL16_HI:
5049 case R_PPC64_REL16_HA:
5050 case R_PPC64_REL16_HIGH:
5051 case R_PPC64_REL16_HIGHA:
5052 case R_PPC64_REL16_HIGHER:
5053 case R_PPC64_REL16_HIGHERA:
5054 case R_PPC64_REL16_HIGHEST:
5055 case R_PPC64_REL16_HIGHESTA:
5056 case R_PPC64_REL16_HIGHER34:
5057 case R_PPC64_REL16_HIGHERA34:
5058 case R_PPC64_REL16_HIGHEST34:
5059 case R_PPC64_REL16_HIGHESTA34:
5060 case R_PPC64_REL16DX_HA:
5061 break;
5063 /* Not supported as a dynamic relocation. */
5064 case R_PPC64_ADDR64_LOCAL:
5065 if (bfd_link_pic (info))
5067 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5068 ppc_howto_init ();
5069 /* xgettext:c-format */
5070 info->callbacks->einfo (_("%H: %s reloc unsupported "
5071 "in shared libraries and PIEs\n"),
5072 abfd, sec, rel->r_offset,
5073 ppc64_elf_howto_table[r_type]->name);
5074 bfd_set_error (bfd_error_bad_value);
5075 return false;
5077 break;
5079 case R_PPC64_TOC16:
5080 case R_PPC64_TOC16_DS:
5081 htab->do_multi_toc = 1;
5082 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5083 /* Fall through. */
5084 case R_PPC64_TOC16_LO:
5085 case R_PPC64_TOC16_HI:
5086 case R_PPC64_TOC16_HA:
5087 case R_PPC64_TOC16_LO_DS:
5088 sec->has_toc_reloc = 1;
5089 if (h != NULL && bfd_link_executable (info))
5091 /* We may need a copy reloc. */
5092 h->non_got_ref = 1;
5093 /* Strongly prefer a copy reloc over a dynamic reloc.
5094 glibc ld.so as of 2019-08 will error out if one of
5095 these relocations is emitted. */
5096 h->needs_copy = 1;
5097 goto dodyn;
5099 break;
5101 /* Marker reloc. */
5102 case R_PPC64_ENTRY:
5103 break;
5105 /* This relocation describes the C++ object vtable hierarchy.
5106 Reconstruct it for later use during GC. */
5107 case R_PPC64_GNU_VTINHERIT:
5108 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5109 return false;
5110 break;
5112 /* This relocation describes which C++ vtable entries are actually
5113 used. Record for later use during GC. */
5114 case R_PPC64_GNU_VTENTRY:
5115 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5116 return false;
5117 break;
5119 case R_PPC64_REL14:
5120 case R_PPC64_REL14_BRTAKEN:
5121 case R_PPC64_REL14_BRNTAKEN:
5123 asection *dest = NULL;
5125 /* Heuristic: If jumping outside our section, chances are
5126 we are going to need a stub. */
5127 if (h != NULL)
5129 /* If the sym is weak it may be overridden later, so
5130 don't assume we know where a weak sym lives. */
5131 if (h->root.type == bfd_link_hash_defined)
5132 dest = h->root.u.def.section;
5134 else
5135 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5137 if (dest != sec)
5138 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5140 goto rel24;
5142 case R_PPC64_PLTCALL:
5143 case R_PPC64_PLTCALL_NOTOC:
5144 ppc64_elf_section_data (sec)->has_pltcall = 1;
5145 /* Fall through. */
5147 case R_PPC64_REL24:
5148 case R_PPC64_REL24_NOTOC:
5149 case R_PPC64_REL24_P9NOTOC:
5150 rel24:
5151 plt_list = ifunc;
5152 if (h != NULL)
5154 h->needs_plt = 1;
5155 if (h->root.root.string[0] == '.'
5156 && h->root.root.string[1] != '\0')
5157 ppc_elf_hash_entry (h)->is_func = 1;
5159 if (h == tga || h == dottga)
5161 sec->has_tls_reloc = 1;
5162 if (rel != relocs
5163 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5164 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5165 /* We have a new-style __tls_get_addr call with
5166 a marker reloc. */
5168 else
5169 /* Mark this section as having an old-style call. */
5170 sec->nomark_tls_get_addr = 1;
5172 plt_list = &h->plt.plist;
5175 /* We may need a .plt entry if the function this reloc
5176 refers to is in a shared lib. */
5177 if (plt_list
5178 && !update_plt_info (abfd, plt_list, rel->r_addend))
5179 return false;
5180 break;
5182 case R_PPC64_ADDR14:
5183 case R_PPC64_ADDR14_BRNTAKEN:
5184 case R_PPC64_ADDR14_BRTAKEN:
5185 case R_PPC64_ADDR24:
5186 goto dodyn;
5188 case R_PPC64_TPREL64:
5189 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5190 if (bfd_link_dll (info))
5191 info->flags |= DF_STATIC_TLS;
5192 goto dotlstoc;
5194 case R_PPC64_DTPMOD64:
5195 if (rel + 1 < rel_end
5196 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5197 && rel[1].r_offset == rel->r_offset + 8)
5198 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5199 else
5200 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5201 goto dotlstoc;
5203 case R_PPC64_DTPREL64:
5204 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5205 if (rel != relocs
5206 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5207 && rel[-1].r_offset == rel->r_offset - 8)
5208 /* This is the second reloc of a dtpmod, dtprel pair.
5209 Don't mark with TLS_DTPREL. */
5210 goto dodyn;
5212 dotlstoc:
5213 sec->has_tls_reloc = 1;
5214 if (h != NULL)
5215 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5216 else
5217 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5218 rel->r_addend, tls_type))
5219 return false;
5221 ppc64_sec = ppc64_elf_section_data (sec);
5222 if (ppc64_sec->sec_type != sec_toc)
5224 bfd_size_type amt;
5226 /* One extra to simplify get_tls_mask. */
5227 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5228 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5229 if (ppc64_sec->u.toc.symndx == NULL)
5230 return false;
5231 amt = sec->size * sizeof (bfd_vma) / 8;
5232 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5233 if (ppc64_sec->u.toc.add == NULL)
5234 return false;
5235 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5236 ppc64_sec->sec_type = sec_toc;
5238 BFD_ASSERT (rel->r_offset % 8 == 0);
5239 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5240 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5242 /* Mark the second slot of a GD or LD entry.
5243 -1 to indicate GD and -2 to indicate LD. */
5244 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5245 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5246 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5247 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5248 goto dodyn;
5250 case R_PPC64_TPREL16_HI:
5251 case R_PPC64_TPREL16_HA:
5252 case R_PPC64_TPREL16_HIGH:
5253 case R_PPC64_TPREL16_HIGHA:
5254 case R_PPC64_TPREL16_HIGHER:
5255 case R_PPC64_TPREL16_HIGHERA:
5256 case R_PPC64_TPREL16_HIGHEST:
5257 case R_PPC64_TPREL16_HIGHESTA:
5258 sec->has_tls_reloc = 1;
5259 /* Fall through. */
5260 case R_PPC64_TPREL34:
5261 case R_PPC64_TPREL16:
5262 case R_PPC64_TPREL16_DS:
5263 case R_PPC64_TPREL16_LO:
5264 case R_PPC64_TPREL16_LO_DS:
5265 if (bfd_link_dll (info))
5266 info->flags |= DF_STATIC_TLS;
5267 goto dodyn;
5269 case R_PPC64_ADDR64:
5270 if (is_opd
5271 && rel + 1 < rel_end
5272 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5274 if (h != NULL)
5275 ppc_elf_hash_entry (h)->is_func = 1;
5277 /* Fall through. */
5279 case R_PPC64_ADDR16:
5280 case R_PPC64_ADDR16_DS:
5281 case R_PPC64_ADDR16_HA:
5282 case R_PPC64_ADDR16_HI:
5283 case R_PPC64_ADDR16_HIGH:
5284 case R_PPC64_ADDR16_HIGHA:
5285 case R_PPC64_ADDR16_HIGHER:
5286 case R_PPC64_ADDR16_HIGHERA:
5287 case R_PPC64_ADDR16_HIGHEST:
5288 case R_PPC64_ADDR16_HIGHESTA:
5289 case R_PPC64_ADDR16_LO:
5290 case R_PPC64_ADDR16_LO_DS:
5291 case R_PPC64_D34:
5292 case R_PPC64_D34_LO:
5293 case R_PPC64_D34_HI30:
5294 case R_PPC64_D34_HA30:
5295 case R_PPC64_ADDR16_HIGHER34:
5296 case R_PPC64_ADDR16_HIGHERA34:
5297 case R_PPC64_ADDR16_HIGHEST34:
5298 case R_PPC64_ADDR16_HIGHESTA34:
5299 case R_PPC64_D28:
5300 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5301 && rel->r_addend == 0)
5303 /* We may need a .plt entry if this reloc refers to a
5304 function in a shared lib. */
5305 if (!update_plt_info (abfd, &h->plt.plist, 0))
5306 return false;
5307 h->pointer_equality_needed = 1;
5309 /* Fall through. */
5311 case R_PPC64_REL30:
5312 case R_PPC64_REL32:
5313 case R_PPC64_REL64:
5314 case R_PPC64_ADDR32:
5315 case R_PPC64_UADDR16:
5316 case R_PPC64_UADDR32:
5317 case R_PPC64_UADDR64:
5318 case R_PPC64_TOC:
5319 if (h != NULL && bfd_link_executable (info))
5320 /* We may need a copy reloc. */
5321 h->non_got_ref = 1;
5323 /* Don't propagate .opd relocs. */
5324 if (NO_OPD_RELOCS && is_opd)
5325 break;
5327 /* Set up information for symbols that might need dynamic
5328 relocations. At this point in linking we have read all
5329 the input files and resolved most symbols, but have not
5330 yet decided whether symbols are dynamic or finalized
5331 symbol flags. In some cases we might be setting dynamic
5332 reloc info for symbols that do not end up needing such.
5333 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5334 work together with this code. */
5335 dodyn:
5336 if ((h != NULL
5337 && !SYMBOL_REFERENCES_LOCAL (info, h))
5338 || (bfd_link_pic (info)
5339 && (h != NULL
5340 ? !bfd_is_abs_symbol (&h->root)
5341 : isym->st_shndx != SHN_ABS)
5342 && must_be_dyn_reloc (info, r_type))
5343 || (!bfd_link_pic (info)
5344 && ifunc != NULL))
5346 /* We must copy these reloc types into the output file.
5347 Create a reloc section in dynobj and make room for
5348 this reloc. */
5349 if (sreloc == NULL)
5351 sreloc = _bfd_elf_make_dynamic_reloc_section
5352 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5354 if (sreloc == NULL)
5355 return false;
5358 /* If this is a global symbol, we count the number of
5359 relocations we need for this symbol. */
5360 if (h != NULL)
5362 struct ppc_dyn_relocs *p;
5363 struct ppc_dyn_relocs **head;
5365 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5366 p = *head;
5367 if (p == NULL || p->sec != sec)
5369 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5370 if (p == NULL)
5371 return false;
5372 p->next = *head;
5373 *head = p;
5374 p->sec = sec;
5375 p->count = 0;
5376 p->pc_count = 0;
5377 p->rel_count = 0;
5379 p->count += 1;
5380 if (!must_be_dyn_reloc (info, r_type))
5381 p->pc_count += 1;
5382 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5383 && rel->r_offset % 2 == 0
5384 && sec->alignment_power != 0)
5385 p->rel_count += 1;
5387 else
5389 /* Track dynamic relocs needed for local syms too. */
5390 struct ppc_local_dyn_relocs *p;
5391 struct ppc_local_dyn_relocs **head;
5392 bool is_ifunc;
5393 asection *s;
5394 void *vpp;
5396 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5397 if (s == NULL)
5398 s = sec;
5400 vpp = &elf_section_data (s)->local_dynrel;
5401 head = (struct ppc_local_dyn_relocs **) vpp;
5402 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5403 p = *head;
5404 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5405 p = p->next;
5406 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5408 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5409 if (p == NULL)
5410 return false;
5411 p->next = *head;
5412 *head = p;
5413 p->sec = sec;
5414 p->count = 0;
5415 p->rel_count = 0;
5416 p->ifunc = is_ifunc;
5418 p->count += 1;
5419 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5420 && rel->r_offset % 2 == 0
5421 && sec->alignment_power != 0)
5422 p->rel_count += 1;
5425 break;
5427 default:
5428 break;
5432 return true;
5435 /* Merge backend specific data from an object file to the output
5436 object file when linking. */
5438 static bool
5439 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5441 bfd *obfd = info->output_bfd;
5442 unsigned long iflags, oflags;
5444 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5445 return true;
5447 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5448 return true;
5450 if (!_bfd_generic_verify_endian_match (ibfd, info))
5451 return false;
5453 iflags = elf_elfheader (ibfd)->e_flags;
5454 oflags = elf_elfheader (obfd)->e_flags;
5456 if (iflags & ~EF_PPC64_ABI)
5458 _bfd_error_handler
5459 /* xgettext:c-format */
5460 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5461 bfd_set_error (bfd_error_bad_value);
5462 return false;
5464 else if (iflags != oflags && iflags != 0)
5466 _bfd_error_handler
5467 /* xgettext:c-format */
5468 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5469 ibfd, iflags, oflags);
5470 bfd_set_error (bfd_error_bad_value);
5471 return false;
5474 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5475 return false;
5477 /* Merge Tag_compatibility attributes and any common GNU ones. */
5478 return _bfd_elf_merge_object_attributes (ibfd, info);
5481 static bool
5482 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5484 /* Print normal ELF private data. */
5485 _bfd_elf_print_private_bfd_data (abfd, ptr);
5487 if (elf_elfheader (abfd)->e_flags != 0)
5489 FILE *file = ptr;
5491 fprintf (file, _("private flags = 0x%lx:"),
5492 elf_elfheader (abfd)->e_flags);
5494 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5495 fprintf (file, _(" [abiv%ld]"),
5496 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5497 fputc ('\n', file);
5500 return true;
5503 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5504 of the code entry point, and its section, which must be in the same
5505 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5507 static bfd_vma
5508 opd_entry_value (asection *opd_sec,
5509 bfd_vma offset,
5510 asection **code_sec,
5511 bfd_vma *code_off,
5512 bool in_code_sec)
5514 bfd *opd_bfd = opd_sec->owner;
5515 Elf_Internal_Rela *relocs;
5516 Elf_Internal_Rela *lo, *hi, *look;
5517 bfd_vma val;
5519 /* No relocs implies we are linking a --just-symbols object, or looking
5520 at a final linked executable with addr2line or somesuch. */
5521 if (opd_sec->reloc_count == 0)
5523 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5525 if (contents == NULL)
5527 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5528 return (bfd_vma) -1;
5529 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5532 /* PR 17512: file: 64b9dfbb. */
5533 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5534 return (bfd_vma) -1;
5536 val = bfd_get_64 (opd_bfd, contents + offset);
5537 if (code_sec != NULL)
5539 asection *sec, *likely = NULL;
5541 if (in_code_sec)
5543 sec = *code_sec;
5544 if (sec->vma <= val
5545 && val < sec->vma + sec->size)
5546 likely = sec;
5547 else
5548 val = -1;
5550 else
5551 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5552 if (sec->vma <= val
5553 && (sec->flags & SEC_LOAD) != 0
5554 && (sec->flags & SEC_ALLOC) != 0)
5555 likely = sec;
5556 if (likely != NULL)
5558 *code_sec = likely;
5559 if (code_off != NULL)
5560 *code_off = val - likely->vma;
5563 return val;
5566 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5568 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5569 if (relocs == NULL)
5570 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5571 /* PR 17512: file: df8e1fd6. */
5572 if (relocs == NULL)
5573 return (bfd_vma) -1;
5575 /* Go find the opd reloc at the sym address. */
5576 lo = relocs;
5577 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5578 val = (bfd_vma) -1;
5579 while (lo < hi)
5581 look = lo + (hi - lo) / 2;
5582 if (look->r_offset < offset)
5583 lo = look + 1;
5584 else if (look->r_offset > offset)
5585 hi = look;
5586 else
5588 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5590 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5591 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5593 unsigned long symndx = ELF64_R_SYM (look->r_info);
5594 asection *sec = NULL;
5596 if (symndx >= symtab_hdr->sh_info
5597 && elf_sym_hashes (opd_bfd) != NULL)
5599 struct elf_link_hash_entry **sym_hashes;
5600 struct elf_link_hash_entry *rh;
5602 sym_hashes = elf_sym_hashes (opd_bfd);
5603 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5604 if (rh != NULL)
5606 rh = elf_follow_link (rh);
5607 if (rh->root.type != bfd_link_hash_defined
5608 && rh->root.type != bfd_link_hash_defweak)
5609 break;
5610 if (rh->root.u.def.section->owner == opd_bfd)
5612 val = rh->root.u.def.value;
5613 sec = rh->root.u.def.section;
5618 if (sec == NULL)
5620 Elf_Internal_Sym *sym;
5622 if (symndx < symtab_hdr->sh_info)
5624 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5625 if (sym == NULL)
5627 size_t symcnt = symtab_hdr->sh_info;
5628 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5629 symcnt, 0,
5630 NULL, NULL, NULL);
5631 if (sym == NULL)
5632 break;
5633 symtab_hdr->contents = (bfd_byte *) sym;
5635 sym += symndx;
5637 else
5639 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5640 1, symndx,
5641 NULL, NULL, NULL);
5642 if (sym == NULL)
5643 break;
5645 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5646 if (sec == NULL)
5647 break;
5648 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5649 val = sym->st_value;
5652 val += look->r_addend;
5653 if (code_off != NULL)
5654 *code_off = val;
5655 if (code_sec != NULL)
5657 if (in_code_sec && *code_sec != sec)
5658 return -1;
5659 else
5660 *code_sec = sec;
5662 if (sec->output_section != NULL)
5663 val += sec->output_section->vma + sec->output_offset;
5665 break;
5669 return val;
5672 /* If the ELF symbol SYM might be a function in SEC, return the
5673 function size and set *CODE_OFF to the function's entry point,
5674 otherwise return zero. */
5676 static bfd_size_type
5677 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5678 bfd_vma *code_off)
5680 bfd_size_type size;
5681 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5683 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5684 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5685 return 0;
5687 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5689 /* In theory we should check that the symbol's type satisfies
5690 _bfd_elf_is_function_type(), but there are some function-like
5691 symbols which would fail this test. (eg _start). Instead
5692 we check for hidden, local, notype symbols with zero size.
5693 This type of symbol is generated by the annobin plugin for gcc
5694 and clang, and should not be considered to be a function symbol. */
5695 if (size == 0
5696 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5697 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5698 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5699 return 0;
5701 if (strcmp (sym->section->name, ".opd") == 0)
5703 struct _opd_sec_data *opd = get_opd_info (sym->section);
5704 bfd_vma symval = sym->value;
5706 if (opd != NULL
5707 && opd->adjust != NULL
5708 && elf_section_data (sym->section)->relocs != NULL)
5710 /* opd_entry_value will use cached relocs that have been
5711 adjusted, but with raw symbols. That means both local
5712 and global symbols need adjusting. */
5713 long adjust = opd->adjust[OPD_NDX (symval)];
5714 if (adjust == -1)
5715 return 0;
5716 symval += adjust;
5719 if (opd_entry_value (sym->section, symval,
5720 &sec, code_off, true) == (bfd_vma) -1)
5721 return 0;
5722 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5723 symbol. This size has nothing to do with the code size of the
5724 function, which is what we're supposed to return, but the
5725 code size isn't available without looking up the dot-sym.
5726 However, doing that would be a waste of time particularly
5727 since elf_find_function will look at the dot-sym anyway.
5728 Now, elf_find_function will keep the largest size of any
5729 function sym found at the code address of interest, so return
5730 1 here to avoid it incorrectly caching a larger function size
5731 for a small function. This does mean we return the wrong
5732 size for a new-ABI function of size 24, but all that does is
5733 disable caching for such functions. */
5734 if (size == 24)
5735 size = 1;
5737 else
5739 if (sym->section != sec)
5740 return 0;
5741 *code_off = sym->value;
5744 /* Do not return 0 for the function's size. */
5745 return size ? size : 1;
5748 /* Return true if symbol is a strong function defined in an ELFv2
5749 object with st_other localentry bits of zero, ie. its local entry
5750 point coincides with its global entry point. */
5752 static bool
5753 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5755 return (h != NULL
5756 && h->type == STT_FUNC
5757 && h->root.type == bfd_link_hash_defined
5758 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5759 && !ppc_elf_hash_entry (h)->non_zero_localentry
5760 && is_ppc64_elf (h->root.u.def.section->owner)
5761 && abiversion (h->root.u.def.section->owner) >= 2);
5764 /* Return true if symbol is defined in a regular object file. */
5766 static bool
5767 is_static_defined (struct elf_link_hash_entry *h)
5769 return ((h->root.type == bfd_link_hash_defined
5770 || h->root.type == bfd_link_hash_defweak)
5771 && h->root.u.def.section != NULL
5772 && h->root.u.def.section->output_section != NULL);
5775 /* If FDH is a function descriptor symbol, return the associated code
5776 entry symbol if it is defined. Return NULL otherwise. */
5778 static struct ppc_link_hash_entry *
5779 defined_code_entry (struct ppc_link_hash_entry *fdh)
5781 if (fdh->is_func_descriptor)
5783 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5784 if (fh->elf.root.type == bfd_link_hash_defined
5785 || fh->elf.root.type == bfd_link_hash_defweak)
5786 return fh;
5788 return NULL;
5791 /* If FH is a function code entry symbol, return the associated
5792 function descriptor symbol if it is defined. Return NULL otherwise. */
5794 static struct ppc_link_hash_entry *
5795 defined_func_desc (struct ppc_link_hash_entry *fh)
5797 if (fh->oh != NULL
5798 && fh->oh->is_func_descriptor)
5800 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5801 if (fdh->elf.root.type == bfd_link_hash_defined
5802 || fdh->elf.root.type == bfd_link_hash_defweak)
5803 return fdh;
5805 return NULL;
5808 /* Given H is a symbol that satisfies is_static_defined, return the
5809 value in the output file. */
5811 static bfd_vma
5812 defined_sym_val (struct elf_link_hash_entry *h)
5814 return (h->root.u.def.section->output_section->vma
5815 + h->root.u.def.section->output_offset
5816 + h->root.u.def.value);
5819 /* Return true if H matches __tls_get_addr or one of its variants. */
5821 static bool
5822 is_tls_get_addr (struct elf_link_hash_entry *h,
5823 struct ppc_link_hash_table *htab)
5825 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5826 || h == elf_hash_entry (htab->tga_desc_fd)
5827 || h == elf_hash_entry (htab->tls_get_addr)
5828 || h == elf_hash_entry (htab->tga_desc));
5831 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5833 /* Garbage collect sections, after first dealing with dot-symbols. */
5835 static bool
5836 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5838 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5840 if (htab != NULL && htab->need_func_desc_adj)
5842 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5843 htab->need_func_desc_adj = 0;
5845 return bfd_elf_gc_sections (abfd, info);
5848 /* Mark all our entry sym sections, both opd and code section. */
5850 static void
5851 ppc64_elf_gc_keep (struct bfd_link_info *info)
5853 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5854 struct bfd_sym_chain *sym;
5856 if (htab == NULL)
5857 return;
5859 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5861 struct ppc_link_hash_entry *eh, *fh;
5862 asection *sec;
5864 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5865 false, false, true));
5866 if (eh == NULL)
5867 continue;
5868 if (eh->elf.root.type != bfd_link_hash_defined
5869 && eh->elf.root.type != bfd_link_hash_defweak)
5870 continue;
5872 fh = defined_code_entry (eh);
5873 if (fh != NULL)
5875 sec = fh->elf.root.u.def.section;
5876 sec->flags |= SEC_KEEP;
5878 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5879 && opd_entry_value (eh->elf.root.u.def.section,
5880 eh->elf.root.u.def.value,
5881 &sec, NULL, false) != (bfd_vma) -1)
5882 sec->flags |= SEC_KEEP;
5884 sec = eh->elf.root.u.def.section;
5885 sec->flags |= SEC_KEEP;
5889 /* Mark sections containing dynamically referenced symbols. When
5890 building shared libraries, we must assume that any visible symbol is
5891 referenced. */
5893 static bool
5894 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5896 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5897 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5898 struct ppc_link_hash_entry *fdh;
5899 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5901 /* Dynamic linking info is on the func descriptor sym. */
5902 fdh = defined_func_desc (eh);
5903 if (fdh != NULL)
5904 eh = fdh;
5906 if ((eh->elf.root.type == bfd_link_hash_defined
5907 || eh->elf.root.type == bfd_link_hash_defweak)
5908 && (!eh->elf.start_stop
5909 || eh->elf.root.ldscript_def
5910 || !info->start_stop_gc)
5911 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5912 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5913 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5914 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5915 && (!bfd_link_executable (info)
5916 || info->gc_keep_exported
5917 || info->export_dynamic
5918 || (eh->elf.dynamic
5919 && d != NULL
5920 && (*d->match) (&d->head, NULL,
5921 eh->elf.root.root.string)))
5922 && (eh->elf.versioned >= versioned
5923 || !bfd_hide_sym_by_version (info->version_info,
5924 eh->elf.root.root.string)))))
5926 asection *code_sec;
5927 struct ppc_link_hash_entry *fh;
5929 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5931 /* Function descriptor syms cause the associated
5932 function code sym section to be marked. */
5933 fh = defined_code_entry (eh);
5934 if (fh != NULL)
5936 code_sec = fh->elf.root.u.def.section;
5937 code_sec->flags |= SEC_KEEP;
5939 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5940 && opd_entry_value (eh->elf.root.u.def.section,
5941 eh->elf.root.u.def.value,
5942 &code_sec, NULL, false) != (bfd_vma) -1)
5943 code_sec->flags |= SEC_KEEP;
5946 return true;
5949 /* Return the section that should be marked against GC for a given
5950 relocation. */
5952 static asection *
5953 ppc64_elf_gc_mark_hook (asection *sec,
5954 struct bfd_link_info *info,
5955 Elf_Internal_Rela *rel,
5956 struct elf_link_hash_entry *h,
5957 Elf_Internal_Sym *sym)
5959 asection *rsec;
5961 /* Syms return NULL if we're marking .opd, so we avoid marking all
5962 function sections, as all functions are referenced in .opd. */
5963 rsec = NULL;
5964 if (get_opd_info (sec) != NULL)
5965 return rsec;
5967 if (h != NULL)
5969 enum elf_ppc64_reloc_type r_type;
5970 struct ppc_link_hash_entry *eh, *fh, *fdh;
5972 r_type = ELF64_R_TYPE (rel->r_info);
5973 switch (r_type)
5975 case R_PPC64_GNU_VTINHERIT:
5976 case R_PPC64_GNU_VTENTRY:
5977 break;
5979 default:
5980 switch (h->root.type)
5982 case bfd_link_hash_defined:
5983 case bfd_link_hash_defweak:
5984 eh = ppc_elf_hash_entry (h);
5985 fdh = defined_func_desc (eh);
5986 if (fdh != NULL)
5988 /* -mcall-aixdesc code references the dot-symbol on
5989 a call reloc. Mark the function descriptor too
5990 against garbage collection. */
5991 fdh->elf.mark = 1;
5992 if (fdh->elf.is_weakalias)
5993 weakdef (&fdh->elf)->mark = 1;
5994 eh = fdh;
5997 /* Function descriptor syms cause the associated
5998 function code sym section to be marked. */
5999 fh = defined_code_entry (eh);
6000 if (fh != NULL)
6002 /* They also mark their opd section. */
6003 eh->elf.root.u.def.section->gc_mark = 1;
6005 rsec = fh->elf.root.u.def.section;
6007 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6008 && opd_entry_value (eh->elf.root.u.def.section,
6009 eh->elf.root.u.def.value,
6010 &rsec, NULL, false) != (bfd_vma) -1)
6011 eh->elf.root.u.def.section->gc_mark = 1;
6012 else
6013 rsec = h->root.u.def.section;
6014 break;
6016 case bfd_link_hash_common:
6017 rsec = h->root.u.c.p->section;
6018 break;
6020 default:
6021 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6025 else
6027 struct _opd_sec_data *opd;
6029 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6030 opd = get_opd_info (rsec);
6031 if (opd != NULL && opd->func_sec != NULL)
6033 rsec->gc_mark = 1;
6035 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6039 return rsec;
6042 /* The maximum size of .sfpr. */
6043 #define SFPR_MAX (218*4)
6045 struct sfpr_def_parms
6047 const char name[12];
6048 unsigned char lo, hi;
6049 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6050 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6053 /* Auto-generate _save*, _rest* functions in .sfpr.
6054 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6055 instead. */
6057 static bool
6058 sfpr_define (struct bfd_link_info *info,
6059 const struct sfpr_def_parms *parm,
6060 asection *stub_sec)
6062 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6063 unsigned int i;
6064 size_t len = strlen (parm->name);
6065 bool writing = false;
6066 char sym[16];
6068 if (htab == NULL)
6069 return false;
6071 memcpy (sym, parm->name, len);
6072 sym[len + 2] = 0;
6074 for (i = parm->lo; i <= parm->hi; i++)
6076 struct ppc_link_hash_entry *h;
6078 sym[len + 0] = i / 10 + '0';
6079 sym[len + 1] = i % 10 + '0';
6080 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6081 writing, true, true));
6082 if (stub_sec != NULL)
6084 if (h != NULL
6085 && h->elf.root.type == bfd_link_hash_defined
6086 && h->elf.root.u.def.section == htab->sfpr)
6088 struct elf_link_hash_entry *s;
6089 char buf[32];
6090 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6091 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6092 if (s == NULL)
6093 return false;
6094 if (s->root.type == bfd_link_hash_new)
6096 s->root.type = bfd_link_hash_defined;
6097 s->root.u.def.section = stub_sec;
6098 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6099 + h->elf.root.u.def.value);
6100 s->ref_regular = 1;
6101 s->def_regular = 1;
6102 s->ref_regular_nonweak = 1;
6103 s->forced_local = 1;
6104 s->non_elf = 0;
6105 s->root.linker_def = 1;
6108 continue;
6110 if (h != NULL)
6112 h->save_res = 1;
6113 if (!h->elf.def_regular)
6115 h->elf.root.type = bfd_link_hash_defined;
6116 h->elf.root.u.def.section = htab->sfpr;
6117 h->elf.root.u.def.value = htab->sfpr->size;
6118 h->elf.type = STT_FUNC;
6119 h->elf.def_regular = 1;
6120 h->elf.non_elf = 0;
6121 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6122 writing = true;
6123 if (htab->sfpr->contents == NULL)
6125 htab->sfpr->contents
6126 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6127 if (htab->sfpr->contents == NULL)
6128 return false;
6132 if (writing)
6134 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6135 if (i != parm->hi)
6136 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6137 else
6138 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6139 htab->sfpr->size = p - htab->sfpr->contents;
6143 return true;
6146 static bfd_byte *
6147 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6149 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6150 return p + 4;
6153 static bfd_byte *
6154 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6156 p = savegpr0 (abfd, p, r);
6157 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6158 p = p + 4;
6159 bfd_put_32 (abfd, BLR, p);
6160 return p + 4;
6163 static bfd_byte *
6164 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6166 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6167 return p + 4;
6170 static bfd_byte *
6171 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6173 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6174 p = p + 4;
6175 p = restgpr0 (abfd, p, r);
6176 bfd_put_32 (abfd, MTLR_R0, p);
6177 p = p + 4;
6178 if (r == 29)
6180 p = restgpr0 (abfd, p, 30);
6181 p = restgpr0 (abfd, p, 31);
6183 bfd_put_32 (abfd, BLR, p);
6184 return p + 4;
6187 static bfd_byte *
6188 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6190 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6191 return p + 4;
6194 static bfd_byte *
6195 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6197 p = savegpr1 (abfd, p, r);
6198 bfd_put_32 (abfd, BLR, p);
6199 return p + 4;
6202 static bfd_byte *
6203 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6205 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6206 return p + 4;
6209 static bfd_byte *
6210 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6212 p = restgpr1 (abfd, p, r);
6213 bfd_put_32 (abfd, BLR, p);
6214 return p + 4;
6217 static bfd_byte *
6218 savefpr (bfd *abfd, bfd_byte *p, int r)
6220 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6221 return p + 4;
6224 static bfd_byte *
6225 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6227 p = savefpr (abfd, p, r);
6228 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6229 p = p + 4;
6230 bfd_put_32 (abfd, BLR, p);
6231 return p + 4;
6234 static bfd_byte *
6235 restfpr (bfd *abfd, bfd_byte *p, int r)
6237 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6238 return p + 4;
6241 static bfd_byte *
6242 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6244 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6245 p = p + 4;
6246 p = restfpr (abfd, p, r);
6247 bfd_put_32 (abfd, MTLR_R0, p);
6248 p = p + 4;
6249 if (r == 29)
6251 p = restfpr (abfd, p, 30);
6252 p = restfpr (abfd, p, 31);
6254 bfd_put_32 (abfd, BLR, p);
6255 return p + 4;
6258 static bfd_byte *
6259 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6261 p = savefpr (abfd, p, r);
6262 bfd_put_32 (abfd, BLR, p);
6263 return p + 4;
6266 static bfd_byte *
6267 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6269 p = restfpr (abfd, p, r);
6270 bfd_put_32 (abfd, BLR, p);
6271 return p + 4;
6274 static bfd_byte *
6275 savevr (bfd *abfd, bfd_byte *p, int r)
6277 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6278 p = p + 4;
6279 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6280 return p + 4;
6283 static bfd_byte *
6284 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6286 p = savevr (abfd, p, r);
6287 bfd_put_32 (abfd, BLR, p);
6288 return p + 4;
6291 static bfd_byte *
6292 restvr (bfd *abfd, bfd_byte *p, int r)
6294 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6295 p = p + 4;
6296 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6297 return p + 4;
6300 static bfd_byte *
6301 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6303 p = restvr (abfd, p, r);
6304 bfd_put_32 (abfd, BLR, p);
6305 return p + 4;
6308 #define STDU_R1_0R1 0xf8210001
6309 #define ADDI_R1_R1 0x38210000
6311 /* Emit prologue of wrapper preserving regs around a call to
6312 __tls_get_addr_opt. */
6314 static bfd_byte *
6315 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6317 unsigned int i;
6319 bfd_put_32 (obfd, MFLR_R0, p);
6320 p += 4;
6321 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6322 p += 4;
6324 if (htab->opd_abi)
6326 for (i = 4; i < 12; i++)
6328 bfd_put_32 (obfd,
6329 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6330 p += 4;
6332 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6333 p += 4;
6335 else
6337 for (i = 4; i < 12; i++)
6339 bfd_put_32 (obfd,
6340 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6341 p += 4;
6343 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6344 p += 4;
6346 return p;
6349 /* Emit epilogue of wrapper preserving regs around a call to
6350 __tls_get_addr_opt. */
6352 static bfd_byte *
6353 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6355 unsigned int i;
6357 if (htab->opd_abi)
6359 for (i = 4; i < 12; i++)
6361 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6362 p += 4;
6364 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6365 p += 4;
6367 else
6369 for (i = 4; i < 12; i++)
6371 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6372 p += 4;
6374 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6375 p += 4;
6377 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6378 p += 4;
6379 bfd_put_32 (obfd, MTLR_R0, p);
6380 p += 4;
6381 bfd_put_32 (obfd, BLR, p);
6382 p += 4;
6383 return p;
6386 /* Called via elf_link_hash_traverse to transfer dynamic linking
6387 information on function code symbol entries to their corresponding
6388 function descriptor symbol entries. Must not be called twice for
6389 any given code symbol. */
6391 static bool
6392 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6394 struct bfd_link_info *info;
6395 struct ppc_link_hash_table *htab;
6396 struct ppc_link_hash_entry *fh;
6397 struct ppc_link_hash_entry *fdh;
6398 bool force_local;
6400 fh = ppc_elf_hash_entry (h);
6401 if (fh->elf.root.type == bfd_link_hash_indirect)
6402 return true;
6404 if (!fh->is_func)
6405 return true;
6407 if (fh->elf.root.root.string[0] != '.'
6408 || fh->elf.root.root.string[1] == '\0')
6409 return true;
6411 info = inf;
6412 htab = ppc_hash_table (info);
6413 if (htab == NULL)
6414 return false;
6416 /* Find the corresponding function descriptor symbol. */
6417 fdh = lookup_fdh (fh, htab);
6419 /* Resolve undefined references to dot-symbols as the value
6420 in the function descriptor, if we have one in a regular object.
6421 This is to satisfy cases like ".quad .foo". Calls to functions
6422 in dynamic objects are handled elsewhere. */
6423 if ((fh->elf.root.type == bfd_link_hash_undefined
6424 || fh->elf.root.type == bfd_link_hash_undefweak)
6425 && (fdh->elf.root.type == bfd_link_hash_defined
6426 || fdh->elf.root.type == bfd_link_hash_defweak)
6427 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6428 && opd_entry_value (fdh->elf.root.u.def.section,
6429 fdh->elf.root.u.def.value,
6430 &fh->elf.root.u.def.section,
6431 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6433 fh->elf.root.type = fdh->elf.root.type;
6434 fh->elf.forced_local = 1;
6435 fh->elf.def_regular = fdh->elf.def_regular;
6436 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6439 if (!fh->elf.dynamic)
6441 struct plt_entry *ent;
6443 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6444 if (ent->plt.refcount > 0)
6445 break;
6446 if (ent == NULL)
6448 if (fdh != NULL && fdh->fake)
6449 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6450 return true;
6454 /* Create a descriptor as undefined if necessary. */
6455 if (fdh == NULL
6456 && !bfd_link_executable (info)
6457 && (fh->elf.root.type == bfd_link_hash_undefined
6458 || fh->elf.root.type == bfd_link_hash_undefweak))
6460 fdh = make_fdh (info, fh);
6461 if (fdh == NULL)
6462 return false;
6465 /* We can't support overriding of symbols on a fake descriptor. */
6466 if (fdh != NULL
6467 && fdh->fake
6468 && (fh->elf.root.type == bfd_link_hash_defined
6469 || fh->elf.root.type == bfd_link_hash_defweak))
6470 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6472 /* Transfer dynamic linking information to the function descriptor. */
6473 if (fdh != NULL)
6475 fdh->elf.ref_regular |= fh->elf.ref_regular;
6476 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6477 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6478 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6479 fdh->elf.dynamic |= fh->elf.dynamic;
6480 fdh->elf.needs_plt |= (fh->elf.needs_plt
6481 || fh->elf.type == STT_FUNC
6482 || fh->elf.type == STT_GNU_IFUNC);
6483 move_plt_plist (fh, fdh);
6485 if (!fdh->elf.forced_local
6486 && fh->elf.dynindx != -1)
6487 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6488 return false;
6491 /* Now that the info is on the function descriptor, clear the
6492 function code sym info. Any function code syms for which we
6493 don't have a definition in a regular file, we force local.
6494 This prevents a shared library from exporting syms that have
6495 been imported from another library. Function code syms that
6496 are really in the library we must leave global to prevent the
6497 linker dragging in a definition from a static library. */
6498 force_local = (!fh->elf.def_regular
6499 || fdh == NULL
6500 || !fdh->elf.def_regular
6501 || fdh->elf.forced_local);
6502 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6504 return true;
6507 static const struct sfpr_def_parms save_res_funcs[] =
6509 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6510 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6511 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6512 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6513 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6514 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6515 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6516 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6517 { "._savef", 14, 31, savefpr, savefpr1_tail },
6518 { "._restf", 14, 31, restfpr, restfpr1_tail },
6519 { "_savevr_", 20, 31, savevr, savevr_tail },
6520 { "_restvr_", 20, 31, restvr, restvr_tail }
6523 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6524 this hook to a) run the edit functions in this file, b) provide
6525 some gcc support functions, and c) transfer dynamic linking
6526 information gathered so far on function code symbol entries, to
6527 their corresponding function descriptor symbol entries. */
6529 static bool
6530 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6532 struct ppc_link_hash_table *htab;
6534 htab = ppc_hash_table (info);
6535 if (htab == NULL)
6536 return false;
6538 /* Call back into the linker, which then runs the edit functions. */
6539 htab->params->edit ();
6541 /* Provide any missing _save* and _rest* functions. */
6542 if (htab->sfpr != NULL)
6544 unsigned int i;
6546 htab->sfpr->size = 0;
6547 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6548 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6549 return false;
6550 if (htab->sfpr->size == 0)
6551 htab->sfpr->flags |= SEC_EXCLUDE;
6554 if (bfd_link_relocatable (info))
6555 return true;
6557 if (htab->elf.hgot != NULL)
6559 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6560 /* Make .TOC. defined so as to prevent it being made dynamic.
6561 The wrong value here is fixed later in ppc64_elf_set_toc. */
6562 if (!htab->elf.hgot->def_regular
6563 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6565 htab->elf.hgot->root.type = bfd_link_hash_defined;
6566 htab->elf.hgot->root.u.def.value = 0;
6567 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6568 htab->elf.hgot->def_regular = 1;
6569 htab->elf.hgot->root.linker_def = 1;
6571 htab->elf.hgot->type = STT_OBJECT;
6572 htab->elf.hgot->other
6573 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6576 return true;
6579 /* Return true if we have dynamic relocs against H or any of its weak
6580 aliases, that apply to read-only sections. Cannot be used after
6581 size_dynamic_sections. */
6583 static bool
6584 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6586 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6589 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6590 return true;
6591 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6593 while (eh != NULL && &eh->elf != h);
6595 return false;
6598 /* Return whether EH has pc-relative dynamic relocs. */
6600 static bool
6601 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6603 struct ppc_dyn_relocs *p;
6605 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6606 if (p->pc_count != 0)
6607 return true;
6608 return false;
6611 /* Return true if a global entry stub will be created for H. Valid
6612 for ELFv2 before plt entries have been allocated. */
6614 static bool
6615 global_entry_stub (struct elf_link_hash_entry *h)
6617 struct plt_entry *pent;
6619 if (!h->pointer_equality_needed
6620 || h->def_regular)
6621 return false;
6623 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6624 if (pent->plt.refcount > 0
6625 && pent->addend == 0)
6626 return true;
6628 return false;
6631 /* Adjust a symbol defined by a dynamic object and referenced by a
6632 regular object. The current definition is in some section of the
6633 dynamic object, but we're not including those sections. We have to
6634 change the definition to something the rest of the link can
6635 understand. */
6637 static bool
6638 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6639 struct elf_link_hash_entry *h)
6641 struct ppc_link_hash_table *htab;
6642 asection *s, *srel;
6644 htab = ppc_hash_table (info);
6645 if (htab == NULL)
6646 return false;
6648 /* Deal with function syms. */
6649 if (h->type == STT_FUNC
6650 || h->type == STT_GNU_IFUNC
6651 || h->needs_plt)
6653 bool local = (ppc_elf_hash_entry (h)->save_res
6654 || SYMBOL_CALLS_LOCAL (info, h)
6655 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6656 /* Discard dyn_relocs when non-pic if we've decided that a
6657 function symbol is local and not an ifunc. We keep dynamic
6658 relocs for ifuncs when local rather than always emitting a
6659 plt call stub for them and defining the symbol on the call
6660 stub. We can't do that for ELFv1 anyway (a function symbol
6661 is defined on a descriptor, not code) and it can be faster at
6662 run-time due to not needing to bounce through a stub. The
6663 dyn_relocs for ifuncs will be applied even in a static
6664 executable. */
6665 if (!bfd_link_pic (info)
6666 && h->type != STT_GNU_IFUNC
6667 && local)
6668 h->dyn_relocs = NULL;
6670 /* Clear procedure linkage table information for any symbol that
6671 won't need a .plt entry. */
6672 struct plt_entry *ent;
6673 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6674 if (ent->plt.refcount > 0)
6675 break;
6676 if (ent == NULL
6677 || (h->type != STT_GNU_IFUNC
6678 && local
6679 && (htab->can_convert_all_inline_plt
6680 || (ppc_elf_hash_entry (h)->tls_mask
6681 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6683 h->plt.plist = NULL;
6684 h->needs_plt = 0;
6685 h->pointer_equality_needed = 0;
6687 else if (abiversion (info->output_bfd) >= 2)
6689 /* Taking a function's address in a read/write section
6690 doesn't require us to define the function symbol in the
6691 executable on a global entry stub. A dynamic reloc can
6692 be used instead. The reason we prefer a few more dynamic
6693 relocs is that calling via a global entry stub costs a
6694 few more instructions, and pointer_equality_needed causes
6695 extra work in ld.so when resolving these symbols. */
6696 if (global_entry_stub (h))
6698 if (!_bfd_elf_readonly_dynrelocs (h))
6700 h->pointer_equality_needed = 0;
6701 /* If we haven't seen a branch reloc and the symbol
6702 isn't an ifunc then we don't need a plt entry. */
6703 if (!h->needs_plt)
6704 h->plt.plist = NULL;
6706 else if (!bfd_link_pic (info))
6707 /* We are going to be defining the function symbol on the
6708 plt stub, so no dyn_relocs needed when non-pic. */
6709 h->dyn_relocs = NULL;
6712 /* ELFv2 function symbols can't have copy relocs. */
6713 return true;
6715 else if (!h->needs_plt
6716 && !_bfd_elf_readonly_dynrelocs (h))
6718 /* If we haven't seen a branch reloc and the symbol isn't an
6719 ifunc then we don't need a plt entry. */
6720 h->plt.plist = NULL;
6721 h->pointer_equality_needed = 0;
6722 return true;
6725 else
6726 h->plt.plist = NULL;
6728 /* If this is a weak symbol, and there is a real definition, the
6729 processor independent code will have arranged for us to see the
6730 real definition first, and we can just use the same value. */
6731 if (h->is_weakalias)
6733 struct elf_link_hash_entry *def = weakdef (h);
6734 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6735 h->root.u.def.section = def->root.u.def.section;
6736 h->root.u.def.value = def->root.u.def.value;
6737 if (def->root.u.def.section == htab->elf.sdynbss
6738 || def->root.u.def.section == htab->elf.sdynrelro)
6739 h->dyn_relocs = NULL;
6740 return true;
6743 /* If we are creating a shared library, we must presume that the
6744 only references to the symbol are via the global offset table.
6745 For such cases we need not do anything here; the relocations will
6746 be handled correctly by relocate_section. */
6747 if (!bfd_link_executable (info))
6748 return true;
6750 /* If there are no references to this symbol that do not use the
6751 GOT, we don't need to generate a copy reloc. */
6752 if (!h->non_got_ref)
6753 return true;
6755 /* Don't generate a copy reloc for symbols defined in the executable. */
6756 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6758 /* If -z nocopyreloc was given, don't generate them either. */
6759 || info->nocopyreloc
6761 /* If we don't find any dynamic relocs in read-only sections, then
6762 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6763 || (ELIMINATE_COPY_RELOCS
6764 && !h->needs_copy
6765 && !alias_readonly_dynrelocs (h))
6767 /* Protected variables do not work with .dynbss. The copy in
6768 .dynbss won't be used by the shared library with the protected
6769 definition for the variable. Text relocations are preferable
6770 to an incorrect program. */
6771 || h->protected_def)
6772 return true;
6774 if (h->type == STT_FUNC
6775 || h->type == STT_GNU_IFUNC)
6777 /* .dynbss copies of function symbols only work if we have
6778 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6779 use dot-symbols and set the function symbol size to the text
6780 size of the function rather than the size of the descriptor.
6781 That's wrong for copying a descriptor. */
6782 if (ppc_elf_hash_entry (h)->oh == NULL
6783 || !(h->size == 24 || h->size == 16))
6784 return true;
6786 /* We should never get here, but unfortunately there are old
6787 versions of gcc (circa gcc-3.2) that improperly for the
6788 ELFv1 ABI put initialized function pointers, vtable refs and
6789 suchlike in read-only sections. Allow them to proceed, but
6790 warn that this might break at runtime. */
6791 info->callbacks->einfo
6792 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6793 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6794 h->root.root.string);
6797 /* This is a reference to a symbol defined by a dynamic object which
6798 is not a function. */
6800 /* We must allocate the symbol in our .dynbss section, which will
6801 become part of the .bss section of the executable. There will be
6802 an entry for this symbol in the .dynsym section. The dynamic
6803 object will contain position independent code, so all references
6804 from the dynamic object to this symbol will go through the global
6805 offset table. The dynamic linker will use the .dynsym entry to
6806 determine the address it must put in the global offset table, so
6807 both the dynamic object and the regular object will refer to the
6808 same memory location for the variable. */
6809 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6811 s = htab->elf.sdynrelro;
6812 srel = htab->elf.sreldynrelro;
6814 else
6816 s = htab->elf.sdynbss;
6817 srel = htab->elf.srelbss;
6819 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6821 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6822 linker to copy the initial value out of the dynamic object
6823 and into the runtime process image. */
6824 srel->size += sizeof (Elf64_External_Rela);
6825 h->needs_copy = 1;
6828 /* We no longer want dyn_relocs. */
6829 h->dyn_relocs = NULL;
6830 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6833 /* If given a function descriptor symbol, hide both the function code
6834 sym and the descriptor. */
6835 static void
6836 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6837 struct elf_link_hash_entry *h,
6838 bool force_local)
6840 struct ppc_link_hash_entry *eh;
6841 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6843 if (ppc_hash_table (info) == NULL)
6844 return;
6846 eh = ppc_elf_hash_entry (h);
6847 if (eh->is_func_descriptor)
6849 struct ppc_link_hash_entry *fh = eh->oh;
6851 if (fh == NULL)
6853 const char *p, *q;
6854 struct elf_link_hash_table *htab = elf_hash_table (info);
6855 char save;
6857 /* We aren't supposed to use alloca in BFD because on
6858 systems which do not have alloca the version in libiberty
6859 calls xmalloc, which might cause the program to crash
6860 when it runs out of memory. This function doesn't have a
6861 return status, so there's no way to gracefully return an
6862 error. So cheat. We know that string[-1] can be safely
6863 accessed; It's either a string in an ELF string table,
6864 or allocated in an objalloc structure. */
6866 p = eh->elf.root.root.string - 1;
6867 save = *p;
6868 *(char *) p = '.';
6869 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6870 false, false));
6871 *(char *) p = save;
6873 /* Unfortunately, if it so happens that the string we were
6874 looking for was allocated immediately before this string,
6875 then we overwrote the string terminator. That's the only
6876 reason the lookup should fail. */
6877 if (fh == NULL)
6879 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6880 while (q >= eh->elf.root.root.string && *q == *p)
6881 --q, --p;
6882 if (q < eh->elf.root.root.string && *p == '.')
6883 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6884 false, false));
6886 if (fh != NULL)
6888 eh->oh = fh;
6889 fh->oh = eh;
6892 if (fh != NULL)
6893 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6897 static bool
6898 get_sym_h (struct elf_link_hash_entry **hp,
6899 Elf_Internal_Sym **symp,
6900 asection **symsecp,
6901 unsigned char **tls_maskp,
6902 Elf_Internal_Sym **locsymsp,
6903 unsigned long r_symndx,
6904 bfd *ibfd)
6906 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6908 if (r_symndx >= symtab_hdr->sh_info)
6910 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6911 struct elf_link_hash_entry *h;
6913 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6914 h = elf_follow_link (h);
6916 if (hp != NULL)
6917 *hp = h;
6919 if (symp != NULL)
6920 *symp = NULL;
6922 if (symsecp != NULL)
6924 asection *symsec = NULL;
6925 if (h->root.type == bfd_link_hash_defined
6926 || h->root.type == bfd_link_hash_defweak)
6927 symsec = h->root.u.def.section;
6928 *symsecp = symsec;
6931 if (tls_maskp != NULL)
6932 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6934 else
6936 Elf_Internal_Sym *sym;
6937 Elf_Internal_Sym *locsyms = *locsymsp;
6939 if (locsyms == NULL)
6941 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6942 if (locsyms == NULL)
6943 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6944 symtab_hdr->sh_info,
6945 0, NULL, NULL, NULL);
6946 if (locsyms == NULL)
6947 return false;
6948 *locsymsp = locsyms;
6950 sym = locsyms + r_symndx;
6952 if (hp != NULL)
6953 *hp = NULL;
6955 if (symp != NULL)
6956 *symp = sym;
6958 if (symsecp != NULL)
6959 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6961 if (tls_maskp != NULL)
6963 struct got_entry **lgot_ents;
6964 unsigned char *tls_mask;
6966 tls_mask = NULL;
6967 lgot_ents = elf_local_got_ents (ibfd);
6968 if (lgot_ents != NULL)
6970 struct plt_entry **local_plt = (struct plt_entry **)
6971 (lgot_ents + symtab_hdr->sh_info);
6972 unsigned char *lgot_masks = (unsigned char *)
6973 (local_plt + symtab_hdr->sh_info);
6974 tls_mask = &lgot_masks[r_symndx];
6976 *tls_maskp = tls_mask;
6979 return true;
6982 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6983 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6984 type suitable for optimization, and 1 otherwise. */
6986 static int
6987 get_tls_mask (unsigned char **tls_maskp,
6988 unsigned long *toc_symndx,
6989 bfd_vma *toc_addend,
6990 Elf_Internal_Sym **locsymsp,
6991 const Elf_Internal_Rela *rel,
6992 bfd *ibfd)
6994 unsigned long r_symndx;
6995 int next_r;
6996 struct elf_link_hash_entry *h;
6997 Elf_Internal_Sym *sym;
6998 asection *sec;
6999 bfd_vma off;
7001 r_symndx = ELF64_R_SYM (rel->r_info);
7002 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7003 return 0;
7005 if ((*tls_maskp != NULL
7006 && (**tls_maskp & TLS_TLS) != 0
7007 && **tls_maskp != (TLS_TLS | TLS_MARK))
7008 || sec == NULL
7009 || ppc64_elf_section_data (sec) == NULL
7010 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7011 return 1;
7013 /* Look inside a TOC section too. */
7014 if (h != NULL)
7016 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7017 off = h->root.u.def.value;
7019 else
7020 off = sym->st_value;
7021 off += rel->r_addend;
7022 BFD_ASSERT (off % 8 == 0);
7023 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7024 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7025 if (toc_symndx != NULL)
7026 *toc_symndx = r_symndx;
7027 if (toc_addend != NULL)
7028 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7029 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7030 return 0;
7031 if ((h == NULL || is_static_defined (h))
7032 && (next_r == -1 || next_r == -2))
7033 return 1 - next_r;
7034 return 1;
7037 /* Find (or create) an entry in the tocsave hash table. */
7039 static struct tocsave_entry *
7040 tocsave_find (struct ppc_link_hash_table *htab,
7041 enum insert_option insert,
7042 Elf_Internal_Sym **local_syms,
7043 const Elf_Internal_Rela *irela,
7044 bfd *ibfd)
7046 unsigned long r_indx;
7047 struct elf_link_hash_entry *h;
7048 Elf_Internal_Sym *sym;
7049 struct tocsave_entry ent, *p;
7050 hashval_t hash;
7051 struct tocsave_entry **slot;
7053 r_indx = ELF64_R_SYM (irela->r_info);
7054 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7055 return NULL;
7056 if (ent.sec == NULL || ent.sec->output_section == NULL)
7058 _bfd_error_handler
7059 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7060 return NULL;
7063 if (h != NULL)
7064 ent.offset = h->root.u.def.value;
7065 else
7066 ent.offset = sym->st_value;
7067 ent.offset += irela->r_addend;
7069 hash = tocsave_htab_hash (&ent);
7070 slot = ((struct tocsave_entry **)
7071 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7072 if (slot == NULL)
7073 return NULL;
7075 if (*slot == NULL)
7077 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7078 if (p == NULL)
7079 return NULL;
7080 *p = ent;
7081 *slot = p;
7083 return *slot;
7086 /* Adjust all global syms defined in opd sections. In gcc generated
7087 code for the old ABI, these will already have been done. */
7089 static bool
7090 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7092 struct ppc_link_hash_entry *eh;
7093 asection *sym_sec;
7094 struct _opd_sec_data *opd;
7096 if (h->root.type == bfd_link_hash_indirect)
7097 return true;
7099 if (h->root.type != bfd_link_hash_defined
7100 && h->root.type != bfd_link_hash_defweak)
7101 return true;
7103 eh = ppc_elf_hash_entry (h);
7104 if (eh->adjust_done)
7105 return true;
7107 sym_sec = eh->elf.root.u.def.section;
7108 opd = get_opd_info (sym_sec);
7109 if (opd != NULL && opd->adjust != NULL)
7111 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7112 if (adjust == -1)
7114 /* This entry has been deleted. */
7115 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7116 if (dsec == NULL)
7118 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7119 if (discarded_section (dsec))
7121 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7122 break;
7125 eh->elf.root.u.def.value = 0;
7126 eh->elf.root.u.def.section = dsec;
7128 else
7129 eh->elf.root.u.def.value += adjust;
7130 eh->adjust_done = 1;
7132 return true;
7135 /* Handles decrementing dynamic reloc counts for the reloc specified by
7136 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7137 have already been determined. */
7139 static bool
7140 dec_dynrel_count (const Elf_Internal_Rela *rel,
7141 asection *sec,
7142 struct bfd_link_info *info,
7143 Elf_Internal_Sym **local_syms,
7144 struct elf_link_hash_entry *h,
7145 Elf_Internal_Sym *sym)
7147 enum elf_ppc64_reloc_type r_type;
7148 asection *sym_sec = NULL;
7150 /* Can this reloc be dynamic? This switch, and later tests here
7151 should be kept in sync with the code in check_relocs. */
7152 r_type = ELF64_R_TYPE (rel->r_info);
7153 switch (r_type)
7155 default:
7156 return true;
7158 case R_PPC64_TOC16:
7159 case R_PPC64_TOC16_DS:
7160 case R_PPC64_TOC16_LO:
7161 case R_PPC64_TOC16_HI:
7162 case R_PPC64_TOC16_HA:
7163 case R_PPC64_TOC16_LO_DS:
7164 if (h == NULL)
7165 return true;
7166 break;
7168 case R_PPC64_TPREL16:
7169 case R_PPC64_TPREL16_LO:
7170 case R_PPC64_TPREL16_HI:
7171 case R_PPC64_TPREL16_HA:
7172 case R_PPC64_TPREL16_DS:
7173 case R_PPC64_TPREL16_LO_DS:
7174 case R_PPC64_TPREL16_HIGH:
7175 case R_PPC64_TPREL16_HIGHA:
7176 case R_PPC64_TPREL16_HIGHER:
7177 case R_PPC64_TPREL16_HIGHERA:
7178 case R_PPC64_TPREL16_HIGHEST:
7179 case R_PPC64_TPREL16_HIGHESTA:
7180 case R_PPC64_TPREL64:
7181 case R_PPC64_TPREL34:
7182 case R_PPC64_DTPMOD64:
7183 case R_PPC64_DTPREL64:
7184 case R_PPC64_ADDR64:
7185 case R_PPC64_REL30:
7186 case R_PPC64_REL32:
7187 case R_PPC64_REL64:
7188 case R_PPC64_ADDR14:
7189 case R_PPC64_ADDR14_BRNTAKEN:
7190 case R_PPC64_ADDR14_BRTAKEN:
7191 case R_PPC64_ADDR16:
7192 case R_PPC64_ADDR16_DS:
7193 case R_PPC64_ADDR16_HA:
7194 case R_PPC64_ADDR16_HI:
7195 case R_PPC64_ADDR16_HIGH:
7196 case R_PPC64_ADDR16_HIGHA:
7197 case R_PPC64_ADDR16_HIGHER:
7198 case R_PPC64_ADDR16_HIGHERA:
7199 case R_PPC64_ADDR16_HIGHEST:
7200 case R_PPC64_ADDR16_HIGHESTA:
7201 case R_PPC64_ADDR16_LO:
7202 case R_PPC64_ADDR16_LO_DS:
7203 case R_PPC64_ADDR24:
7204 case R_PPC64_ADDR32:
7205 case R_PPC64_UADDR16:
7206 case R_PPC64_UADDR32:
7207 case R_PPC64_UADDR64:
7208 case R_PPC64_TOC:
7209 case R_PPC64_D34:
7210 case R_PPC64_D34_LO:
7211 case R_PPC64_D34_HI30:
7212 case R_PPC64_D34_HA30:
7213 case R_PPC64_ADDR16_HIGHER34:
7214 case R_PPC64_ADDR16_HIGHERA34:
7215 case R_PPC64_ADDR16_HIGHEST34:
7216 case R_PPC64_ADDR16_HIGHESTA34:
7217 case R_PPC64_D28:
7218 break;
7221 if (local_syms != NULL)
7223 unsigned long r_symndx;
7224 bfd *ibfd = sec->owner;
7226 r_symndx = ELF64_R_SYM (rel->r_info);
7227 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7228 return false;
7231 if ((h != NULL
7232 && !SYMBOL_REFERENCES_LOCAL (info, h))
7233 || (bfd_link_pic (info)
7234 && (h != NULL
7235 ? !bfd_is_abs_symbol (&h->root)
7236 : sym_sec != bfd_abs_section_ptr)
7237 && must_be_dyn_reloc (info, r_type))
7238 || (!bfd_link_pic (info)
7239 && (h != NULL
7240 ? h->type == STT_GNU_IFUNC
7241 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7243 else
7244 return true;
7246 if (h != NULL)
7248 struct ppc_dyn_relocs *p;
7249 struct ppc_dyn_relocs **pp;
7250 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7252 /* elf_gc_sweep may have already removed all dyn relocs associated
7253 with local syms for a given section. Also, symbol flags are
7254 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7255 report a dynreloc miscount. */
7256 if (*pp == NULL && info->gc_sections)
7257 return true;
7259 while ((p = *pp) != NULL)
7261 if (p->sec == sec)
7263 if (!must_be_dyn_reloc (info, r_type))
7264 p->pc_count -= 1;
7265 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7266 && rel->r_offset % 2 == 0
7267 && sec->alignment_power != 0)
7268 p->rel_count -= 1;
7269 p->count -= 1;
7270 if (p->count == 0)
7271 *pp = p->next;
7272 return true;
7274 pp = &p->next;
7277 else
7279 struct ppc_local_dyn_relocs *p;
7280 struct ppc_local_dyn_relocs **pp;
7281 void *vpp;
7282 bool is_ifunc;
7284 if (local_syms == NULL)
7285 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7286 if (sym_sec == NULL)
7287 sym_sec = sec;
7289 vpp = &elf_section_data (sym_sec)->local_dynrel;
7290 pp = (struct ppc_local_dyn_relocs **) vpp;
7292 if (*pp == NULL && info->gc_sections)
7293 return true;
7295 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7296 while ((p = *pp) != NULL)
7298 if (p->sec == sec && p->ifunc == is_ifunc)
7300 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7301 && rel->r_offset % 2 == 0
7302 && sec->alignment_power != 0)
7303 p->rel_count -= 1;
7304 p->count -= 1;
7305 if (p->count == 0)
7306 *pp = p->next;
7307 return true;
7309 pp = &p->next;
7313 /* xgettext:c-format */
7314 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7315 sec->owner, sec);
7316 bfd_set_error (bfd_error_bad_value);
7317 return false;
7320 /* Remove unused Official Procedure Descriptor entries. Currently we
7321 only remove those associated with functions in discarded link-once
7322 sections, or weakly defined functions that have been overridden. It
7323 would be possible to remove many more entries for statically linked
7324 applications. */
7326 bool
7327 ppc64_elf_edit_opd (struct bfd_link_info *info)
7329 bfd *ibfd;
7330 bool some_edited = false;
7331 asection *need_pad = NULL;
7332 struct ppc_link_hash_table *htab;
7334 htab = ppc_hash_table (info);
7335 if (htab == NULL)
7336 return false;
7338 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7340 asection *sec;
7341 Elf_Internal_Rela *relstart, *rel, *relend;
7342 Elf_Internal_Shdr *symtab_hdr;
7343 Elf_Internal_Sym *local_syms;
7344 struct _opd_sec_data *opd;
7345 bool need_edit, add_aux_fields, broken;
7346 bfd_size_type cnt_16b = 0;
7348 if (!is_ppc64_elf (ibfd))
7349 continue;
7351 sec = bfd_get_section_by_name (ibfd, ".opd");
7352 if (sec == NULL || sec->size == 0)
7353 continue;
7355 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7356 continue;
7358 if (sec->output_section == bfd_abs_section_ptr)
7359 continue;
7361 /* Look through the section relocs. */
7362 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7363 continue;
7365 local_syms = NULL;
7366 symtab_hdr = &elf_symtab_hdr (ibfd);
7368 /* Read the relocations. */
7369 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7370 info->keep_memory);
7371 if (relstart == NULL)
7372 return false;
7374 /* First run through the relocs to check they are sane, and to
7375 determine whether we need to edit this opd section. */
7376 need_edit = false;
7377 broken = false;
7378 need_pad = sec;
7379 relend = relstart + sec->reloc_count;
7380 for (rel = relstart; rel < relend; )
7382 enum elf_ppc64_reloc_type r_type;
7383 unsigned long r_symndx;
7384 asection *sym_sec;
7385 struct elf_link_hash_entry *h;
7386 Elf_Internal_Sym *sym;
7387 bfd_vma offset;
7389 /* .opd contains an array of 16 or 24 byte entries. We're
7390 only interested in the reloc pointing to a function entry
7391 point. */
7392 offset = rel->r_offset;
7393 if (rel + 1 == relend
7394 || rel[1].r_offset != offset + 8)
7396 /* If someone messes with .opd alignment then after a
7397 "ld -r" we might have padding in the middle of .opd.
7398 Also, there's nothing to prevent someone putting
7399 something silly in .opd with the assembler. No .opd
7400 optimization for them! */
7401 broken_opd:
7402 _bfd_error_handler
7403 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7404 broken = true;
7405 break;
7408 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7409 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7411 _bfd_error_handler
7412 /* xgettext:c-format */
7413 (_("%pB: unexpected reloc type %u in .opd section"),
7414 ibfd, r_type);
7415 broken = true;
7416 break;
7419 r_symndx = ELF64_R_SYM (rel->r_info);
7420 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7421 r_symndx, ibfd))
7422 goto error_ret;
7424 if (sym_sec == NULL || sym_sec->owner == NULL)
7426 const char *sym_name;
7427 if (h != NULL)
7428 sym_name = h->root.root.string;
7429 else
7430 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7431 sym_sec);
7433 _bfd_error_handler
7434 /* xgettext:c-format */
7435 (_("%pB: undefined sym `%s' in .opd section"),
7436 ibfd, sym_name);
7437 broken = true;
7438 break;
7441 /* opd entries are always for functions defined in the
7442 current input bfd. If the symbol isn't defined in the
7443 input bfd, then we won't be using the function in this
7444 bfd; It must be defined in a linkonce section in another
7445 bfd, or is weak. It's also possible that we are
7446 discarding the function due to a linker script /DISCARD/,
7447 which we test for via the output_section. */
7448 if (sym_sec->owner != ibfd
7449 || sym_sec->output_section == bfd_abs_section_ptr)
7450 need_edit = true;
7452 rel += 2;
7453 if (rel + 1 == relend
7454 || (rel + 2 < relend
7455 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7456 ++rel;
7458 if (rel == relend)
7460 if (sec->size == offset + 24)
7462 need_pad = NULL;
7463 break;
7465 if (sec->size == offset + 16)
7467 cnt_16b++;
7468 break;
7470 goto broken_opd;
7472 else if (rel + 1 < relend
7473 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7474 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7476 if (rel[0].r_offset == offset + 16)
7477 cnt_16b++;
7478 else if (rel[0].r_offset != offset + 24)
7479 goto broken_opd;
7481 else
7482 goto broken_opd;
7485 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7487 if (!broken && (need_edit || add_aux_fields))
7489 Elf_Internal_Rela *write_rel;
7490 Elf_Internal_Shdr *rel_hdr;
7491 bfd_byte *rptr, *wptr;
7492 bfd_byte *new_contents;
7493 bfd_size_type amt;
7495 new_contents = NULL;
7496 amt = OPD_NDX (sec->size) * sizeof (long);
7497 opd = &ppc64_elf_section_data (sec)->u.opd;
7498 opd->adjust = bfd_zalloc (sec->owner, amt);
7499 if (opd->adjust == NULL)
7500 return false;
7502 /* This seems a waste of time as input .opd sections are all
7503 zeros as generated by gcc, but I suppose there's no reason
7504 this will always be so. We might start putting something in
7505 the third word of .opd entries. */
7506 if ((sec->flags & SEC_IN_MEMORY) == 0)
7508 bfd_byte *loc;
7509 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7511 free (loc);
7512 error_ret:
7513 if (symtab_hdr->contents != (unsigned char *) local_syms)
7514 free (local_syms);
7515 if (elf_section_data (sec)->relocs != relstart)
7516 free (relstart);
7517 return false;
7519 sec->contents = loc;
7520 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7523 elf_section_data (sec)->relocs = relstart;
7525 new_contents = sec->contents;
7526 if (add_aux_fields)
7528 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7529 if (new_contents == NULL)
7530 return false;
7531 need_pad = NULL;
7533 wptr = new_contents;
7534 rptr = sec->contents;
7535 write_rel = relstart;
7536 for (rel = relstart; rel < relend; )
7538 unsigned long r_symndx;
7539 asection *sym_sec;
7540 struct elf_link_hash_entry *h;
7541 struct ppc_link_hash_entry *fdh = NULL;
7542 Elf_Internal_Sym *sym;
7543 long opd_ent_size;
7544 Elf_Internal_Rela *next_rel;
7545 bool skip;
7547 r_symndx = ELF64_R_SYM (rel->r_info);
7548 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7549 r_symndx, ibfd))
7550 goto error_ret;
7552 next_rel = rel + 2;
7553 if (next_rel + 1 == relend
7554 || (next_rel + 2 < relend
7555 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7556 ++next_rel;
7558 /* See if the .opd entry is full 24 byte or
7559 16 byte (with fd_aux entry overlapped with next
7560 fd_func). */
7561 opd_ent_size = 24;
7562 if (next_rel == relend)
7564 if (sec->size == rel->r_offset + 16)
7565 opd_ent_size = 16;
7567 else if (next_rel->r_offset == rel->r_offset + 16)
7568 opd_ent_size = 16;
7570 if (h != NULL
7571 && h->root.root.string[0] == '.')
7573 fdh = ppc_elf_hash_entry (h)->oh;
7574 if (fdh != NULL)
7576 fdh = ppc_follow_link (fdh);
7577 if (fdh->elf.root.type != bfd_link_hash_defined
7578 && fdh->elf.root.type != bfd_link_hash_defweak)
7579 fdh = NULL;
7583 skip = (sym_sec->owner != ibfd
7584 || sym_sec->output_section == bfd_abs_section_ptr);
7585 if (skip)
7587 if (fdh != NULL && sym_sec->owner == ibfd)
7589 /* Arrange for the function descriptor sym
7590 to be dropped. */
7591 fdh->elf.root.u.def.value = 0;
7592 fdh->elf.root.u.def.section = sym_sec;
7594 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7596 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7597 rel = next_rel;
7598 else
7599 while (1)
7601 if (!dec_dynrel_count (rel, sec, info,
7602 NULL, h, sym))
7603 goto error_ret;
7605 if (++rel == next_rel)
7606 break;
7608 r_symndx = ELF64_R_SYM (rel->r_info);
7609 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7610 r_symndx, ibfd))
7611 goto error_ret;
7614 else
7616 /* We'll be keeping this opd entry. */
7617 long adjust;
7619 if (fdh != NULL)
7621 /* Redefine the function descriptor symbol to
7622 this location in the opd section. It is
7623 necessary to update the value here rather
7624 than using an array of adjustments as we do
7625 for local symbols, because various places
7626 in the generic ELF code use the value
7627 stored in u.def.value. */
7628 fdh->elf.root.u.def.value = wptr - new_contents;
7629 fdh->adjust_done = 1;
7632 /* Local syms are a bit tricky. We could
7633 tweak them as they can be cached, but
7634 we'd need to look through the local syms
7635 for the function descriptor sym which we
7636 don't have at the moment. So keep an
7637 array of adjustments. */
7638 adjust = (wptr - new_contents) - (rptr - sec->contents);
7639 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7641 if (wptr != rptr)
7642 memcpy (wptr, rptr, opd_ent_size);
7643 wptr += opd_ent_size;
7644 if (add_aux_fields && opd_ent_size == 16)
7646 memset (wptr, '\0', 8);
7647 wptr += 8;
7650 /* We need to adjust any reloc offsets to point to the
7651 new opd entries. */
7652 for ( ; rel != next_rel; ++rel)
7654 rel->r_offset += adjust;
7655 if (write_rel != rel)
7656 memcpy (write_rel, rel, sizeof (*rel));
7657 ++write_rel;
7661 rptr += opd_ent_size;
7664 sec->size = wptr - new_contents;
7665 sec->reloc_count = write_rel - relstart;
7666 if (add_aux_fields)
7668 free (sec->contents);
7669 sec->contents = new_contents;
7672 /* Fudge the header size too, as this is used later in
7673 elf_bfd_final_link if we are emitting relocs. */
7674 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7675 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7676 some_edited = true;
7678 else if (elf_section_data (sec)->relocs != relstart)
7679 free (relstart);
7681 if (local_syms != NULL
7682 && symtab_hdr->contents != (unsigned char *) local_syms)
7684 if (!info->keep_memory)
7685 free (local_syms);
7686 else
7687 symtab_hdr->contents = (unsigned char *) local_syms;
7691 if (some_edited)
7692 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7694 /* If we are doing a final link and the last .opd entry is just 16 byte
7695 long, add a 8 byte padding after it. */
7696 if (need_pad != NULL && !bfd_link_relocatable (info))
7698 bfd_byte *p;
7700 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7702 BFD_ASSERT (need_pad->size > 0);
7704 p = bfd_malloc (need_pad->size + 8);
7705 if (p == NULL)
7706 return false;
7708 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7709 p, 0, need_pad->size))
7710 return false;
7712 need_pad->contents = p;
7713 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7715 else
7717 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7718 if (p == NULL)
7719 return false;
7721 need_pad->contents = p;
7724 memset (need_pad->contents + need_pad->size, 0, 8);
7725 need_pad->size += 8;
7728 return true;
7731 /* Analyze inline PLT call relocations to see whether calls to locally
7732 defined functions can be converted to direct calls. */
7734 bool
7735 ppc64_elf_inline_plt (struct bfd_link_info *info)
7737 struct ppc_link_hash_table *htab;
7738 bfd *ibfd;
7739 asection *sec;
7740 bfd_vma low_vma, high_vma, limit;
7742 htab = ppc_hash_table (info);
7743 if (htab == NULL)
7744 return false;
7746 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7747 reduced somewhat to cater for possible stubs that might be added
7748 between the call and its destination. */
7749 if (htab->params->group_size < 0)
7751 limit = -htab->params->group_size;
7752 if (limit == 1)
7753 limit = 0x1e00000;
7755 else
7757 limit = htab->params->group_size;
7758 if (limit == 1)
7759 limit = 0x1c00000;
7762 low_vma = -1;
7763 high_vma = 0;
7764 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7765 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7767 if (low_vma > sec->vma)
7768 low_vma = sec->vma;
7769 if (high_vma < sec->vma + sec->size)
7770 high_vma = sec->vma + sec->size;
7773 /* If a "bl" can reach anywhere in local code sections, then we can
7774 convert all inline PLT sequences to direct calls when the symbol
7775 is local. */
7776 if (high_vma - low_vma < limit)
7778 htab->can_convert_all_inline_plt = 1;
7779 return true;
7782 /* Otherwise, go looking through relocs for cases where a direct
7783 call won't reach. Mark the symbol on any such reloc to disable
7784 the optimization and keep the PLT entry as it seems likely that
7785 this will be better than creating trampolines. Note that this
7786 will disable the optimization for all inline PLT calls to a
7787 particular symbol, not just those that won't reach. The
7788 difficulty in doing a more precise optimization is that the
7789 linker needs to make a decision depending on whether a
7790 particular R_PPC64_PLTCALL insn can be turned into a direct
7791 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7792 the sequence, and there is nothing that ties those relocs
7793 together except their symbol. */
7795 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7797 Elf_Internal_Shdr *symtab_hdr;
7798 Elf_Internal_Sym *local_syms;
7800 if (!is_ppc64_elf (ibfd))
7801 continue;
7803 local_syms = NULL;
7804 symtab_hdr = &elf_symtab_hdr (ibfd);
7806 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7807 if (ppc64_elf_section_data (sec)->has_pltcall
7808 && !bfd_is_abs_section (sec->output_section))
7810 Elf_Internal_Rela *relstart, *rel, *relend;
7812 /* Read the relocations. */
7813 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7814 info->keep_memory);
7815 if (relstart == NULL)
7816 return false;
7818 relend = relstart + sec->reloc_count;
7819 for (rel = relstart; rel < relend; rel++)
7821 enum elf_ppc64_reloc_type r_type;
7822 unsigned long r_symndx;
7823 asection *sym_sec;
7824 struct elf_link_hash_entry *h;
7825 Elf_Internal_Sym *sym;
7826 unsigned char *tls_maskp;
7828 r_type = ELF64_R_TYPE (rel->r_info);
7829 if (r_type != R_PPC64_PLTCALL
7830 && r_type != R_PPC64_PLTCALL_NOTOC)
7831 continue;
7833 r_symndx = ELF64_R_SYM (rel->r_info);
7834 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7835 r_symndx, ibfd))
7837 if (elf_section_data (sec)->relocs != relstart)
7838 free (relstart);
7839 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7840 free (local_syms);
7841 return false;
7844 if (sym_sec != NULL && sym_sec->output_section != NULL)
7846 bfd_vma from, to;
7847 if (h != NULL)
7848 to = h->root.u.def.value;
7849 else
7850 to = sym->st_value;
7851 to += (rel->r_addend
7852 + sym_sec->output_offset
7853 + sym_sec->output_section->vma);
7854 from = (rel->r_offset
7855 + sec->output_offset
7856 + sec->output_section->vma);
7857 if (to - from + limit < 2 * limit
7858 && !(r_type == R_PPC64_PLTCALL_NOTOC
7859 && (((h ? h->other : sym->st_other)
7860 & STO_PPC64_LOCAL_MASK)
7861 > 1 << STO_PPC64_LOCAL_BIT)))
7862 *tls_maskp &= ~PLT_KEEP;
7865 if (elf_section_data (sec)->relocs != relstart)
7866 free (relstart);
7869 if (local_syms != NULL
7870 && symtab_hdr->contents != (unsigned char *) local_syms)
7872 if (!info->keep_memory)
7873 free (local_syms);
7874 else
7875 symtab_hdr->contents = (unsigned char *) local_syms;
7879 return true;
7882 /* Set htab->tls_get_addr and various other info specific to TLS.
7883 This needs to run before dynamic symbols are processed in
7884 bfd_elf_size_dynamic_sections. */
7886 bool
7887 ppc64_elf_tls_setup (struct bfd_link_info *info)
7889 struct ppc_link_hash_table *htab;
7890 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7892 htab = ppc_hash_table (info);
7893 if (htab == NULL)
7894 return false;
7896 /* Move dynamic linking info to the function descriptor sym. */
7897 if (htab->need_func_desc_adj)
7899 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7900 htab->need_func_desc_adj = 0;
7903 if (abiversion (info->output_bfd) == 1)
7904 htab->opd_abi = 1;
7906 if (htab->params->no_multi_toc)
7907 htab->do_multi_toc = 0;
7908 else if (!htab->do_multi_toc)
7909 htab->params->no_multi_toc = 1;
7911 /* Default to --no-plt-localentry, as this option can cause problems
7912 with symbol interposition. For example, glibc libpthread.so and
7913 libc.so duplicate many pthread symbols, with a fallback
7914 implementation in libc.so. In some cases the fallback does more
7915 work than the pthread implementation. __pthread_condattr_destroy
7916 is one such symbol: the libpthread.so implementation is
7917 localentry:0 while the libc.so implementation is localentry:8.
7918 An app that "cleverly" uses dlopen to only load necessary
7919 libraries at runtime may omit loading libpthread.so when not
7920 running multi-threaded, which then results in the libc.so
7921 fallback symbols being used and ld.so complaining. Now there
7922 are workarounds in ld (see non_zero_localentry) to detect the
7923 pthread situation, but that may not be the only case where
7924 --plt-localentry can cause trouble. */
7925 if (htab->params->plt_localentry0 < 0)
7926 htab->params->plt_localentry0 = 0;
7927 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7929 /* The issue is that __glink_PLTresolve saves r2, which is done
7930 because glibc ld.so _dl_runtime_resolve restores r2 to support
7931 a glibc plt call optimisation where global entry code is
7932 skipped on calls that resolve to the same binary. The
7933 __glink_PLTresolve save of r2 is incompatible with code
7934 making tail calls, because the tail call might go via the
7935 resolver and thus overwrite the proper saved r2. */
7936 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7937 "power10 pc-relative code"));
7938 htab->params->plt_localentry0 = 0;
7940 if (htab->params->plt_localentry0
7941 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7942 false, false, false) == NULL)
7943 _bfd_error_handler
7944 (_("warning: --plt-localentry is especially dangerous without "
7945 "ld.so support to detect ABI violations"));
7947 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7948 false, false, true);
7949 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7950 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7951 false, false, true);
7952 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7954 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7955 false, false, true);
7956 htab->tga_desc = ppc_elf_hash_entry (desc);
7957 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7958 false, false, true);
7959 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7961 if (htab->params->tls_get_addr_opt)
7963 struct elf_link_hash_entry *opt, *opt_fd;
7965 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7966 false, false, true);
7967 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7968 false, false, true);
7969 if (opt_fd != NULL
7970 && (opt_fd->root.type == bfd_link_hash_defined
7971 || opt_fd->root.type == bfd_link_hash_defweak))
7973 /* If glibc supports an optimized __tls_get_addr call stub,
7974 signalled by the presence of __tls_get_addr_opt, and we'll
7975 be calling __tls_get_addr via a plt call stub, then
7976 make __tls_get_addr point to __tls_get_addr_opt. */
7977 if (!(htab->elf.dynamic_sections_created
7978 && tga_fd != NULL
7979 && (tga_fd->type == STT_FUNC
7980 || tga_fd->needs_plt)
7981 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7982 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7983 tga_fd = NULL;
7984 if (!(htab->elf.dynamic_sections_created
7985 && desc_fd != NULL
7986 && (desc_fd->type == STT_FUNC
7987 || desc_fd->needs_plt)
7988 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7989 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7990 desc_fd = NULL;
7992 if (tga_fd != NULL || desc_fd != NULL)
7994 struct plt_entry *ent = NULL;
7996 if (tga_fd != NULL)
7997 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7998 if (ent->plt.refcount > 0)
7999 break;
8000 if (ent == NULL && desc_fd != NULL)
8001 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8002 if (ent->plt.refcount > 0)
8003 break;
8004 if (ent != NULL)
8006 if (tga_fd != NULL)
8008 tga_fd->root.type = bfd_link_hash_indirect;
8009 tga_fd->root.u.i.link = &opt_fd->root;
8010 tga_fd->root.u.i.warning = NULL;
8011 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8013 if (desc_fd != NULL)
8015 desc_fd->root.type = bfd_link_hash_indirect;
8016 desc_fd->root.u.i.link = &opt_fd->root;
8017 desc_fd->root.u.i.warning = NULL;
8018 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8020 opt_fd->mark = 1;
8021 if (opt_fd->dynindx != -1)
8023 /* Use __tls_get_addr_opt in dynamic relocations. */
8024 opt_fd->dynindx = -1;
8025 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8026 opt_fd->dynstr_index);
8027 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8028 return false;
8030 if (tga_fd != NULL)
8032 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8033 tga = elf_hash_entry (htab->tls_get_addr);
8034 if (opt != NULL && tga != NULL)
8036 tga->root.type = bfd_link_hash_indirect;
8037 tga->root.u.i.link = &opt->root;
8038 tga->root.u.i.warning = NULL;
8039 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8040 opt->mark = 1;
8041 _bfd_elf_link_hash_hide_symbol (info, opt,
8042 tga->forced_local);
8043 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8045 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8046 htab->tls_get_addr_fd->is_func_descriptor = 1;
8047 if (htab->tls_get_addr != NULL)
8049 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8050 htab->tls_get_addr->is_func = 1;
8053 if (desc_fd != NULL)
8055 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8056 if (opt != NULL && desc != NULL)
8058 desc->root.type = bfd_link_hash_indirect;
8059 desc->root.u.i.link = &opt->root;
8060 desc->root.u.i.warning = NULL;
8061 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8062 opt->mark = 1;
8063 _bfd_elf_link_hash_hide_symbol (info, opt,
8064 desc->forced_local);
8065 htab->tga_desc = ppc_elf_hash_entry (opt);
8067 htab->tga_desc_fd->oh = htab->tga_desc;
8068 htab->tga_desc_fd->is_func_descriptor = 1;
8069 if (htab->tga_desc != NULL)
8071 htab->tga_desc->oh = htab->tga_desc_fd;
8072 htab->tga_desc->is_func = 1;
8078 else if (htab->params->tls_get_addr_opt < 0)
8079 htab->params->tls_get_addr_opt = 0;
8082 if (htab->tga_desc_fd != NULL
8083 && htab->params->tls_get_addr_opt
8084 && htab->params->no_tls_get_addr_regsave == -1)
8085 htab->params->no_tls_get_addr_regsave = 0;
8087 return true;
8090 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8091 any of HASH1, HASH2, HASH3, or HASH4. */
8093 static bool
8094 branch_reloc_hash_match (bfd *ibfd,
8095 Elf_Internal_Rela *rel,
8096 struct ppc_link_hash_entry *hash1,
8097 struct ppc_link_hash_entry *hash2,
8098 struct ppc_link_hash_entry *hash3,
8099 struct ppc_link_hash_entry *hash4)
8101 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8102 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8103 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8105 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8107 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8108 struct elf_link_hash_entry *h;
8110 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8111 h = elf_follow_link (h);
8112 if (h == elf_hash_entry (hash1)
8113 || h == elf_hash_entry (hash2)
8114 || h == elf_hash_entry (hash3)
8115 || h == elf_hash_entry (hash4))
8116 return true;
8118 return false;
8121 /* Run through all the TLS relocs looking for optimization
8122 opportunities. The linker has been hacked (see ppc64elf.em) to do
8123 a preliminary section layout so that we know the TLS segment
8124 offsets. We can't optimize earlier because some optimizations need
8125 to know the tp offset, and we need to optimize before allocating
8126 dynamic relocations. */
8128 bool
8129 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8131 bfd *ibfd;
8132 asection *sec;
8133 struct ppc_link_hash_table *htab;
8134 unsigned char *toc_ref;
8135 int pass;
8137 if (!bfd_link_executable (info))
8138 return true;
8140 htab = ppc_hash_table (info);
8141 if (htab == NULL)
8142 return false;
8144 htab->do_tls_opt = 1;
8146 /* Make two passes over the relocs. On the first pass, mark toc
8147 entries involved with tls relocs, and check that tls relocs
8148 involved in setting up a tls_get_addr call are indeed followed by
8149 such a call. If they are not, we can't do any tls optimization.
8150 On the second pass twiddle tls_mask flags to notify
8151 relocate_section that optimization can be done, and adjust got
8152 and plt refcounts. */
8153 toc_ref = NULL;
8154 for (pass = 0; pass < 2; ++pass)
8155 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8157 Elf_Internal_Sym *locsyms = NULL;
8158 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8160 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8161 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8163 Elf_Internal_Rela *relstart, *rel, *relend;
8164 bool found_tls_get_addr_arg = 0;
8166 /* Read the relocations. */
8167 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8168 info->keep_memory);
8169 if (relstart == NULL)
8171 free (toc_ref);
8172 return false;
8175 relend = relstart + sec->reloc_count;
8176 for (rel = relstart; rel < relend; rel++)
8178 enum elf_ppc64_reloc_type r_type;
8179 unsigned long r_symndx;
8180 struct elf_link_hash_entry *h;
8181 Elf_Internal_Sym *sym;
8182 asection *sym_sec;
8183 unsigned char *tls_mask;
8184 unsigned int tls_set, tls_clear, tls_type = 0;
8185 bfd_vma value;
8186 bool ok_tprel, is_local;
8187 long toc_ref_index = 0;
8188 int expecting_tls_get_addr = 0;
8189 bool ret = false;
8191 r_symndx = ELF64_R_SYM (rel->r_info);
8192 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8193 r_symndx, ibfd))
8195 err_free_rel:
8196 if (elf_section_data (sec)->relocs != relstart)
8197 free (relstart);
8198 free (toc_ref);
8199 if (elf_symtab_hdr (ibfd).contents
8200 != (unsigned char *) locsyms)
8201 free (locsyms);
8202 return ret;
8205 if (h != NULL)
8207 if (h->root.type == bfd_link_hash_defined
8208 || h->root.type == bfd_link_hash_defweak)
8209 value = h->root.u.def.value;
8210 else if (h->root.type == bfd_link_hash_undefweak)
8211 value = 0;
8212 else
8214 found_tls_get_addr_arg = 0;
8215 continue;
8218 else
8219 /* Symbols referenced by TLS relocs must be of type
8220 STT_TLS. So no need for .opd local sym adjust. */
8221 value = sym->st_value;
8223 ok_tprel = false;
8224 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8225 if (is_local)
8227 if (h != NULL
8228 && h->root.type == bfd_link_hash_undefweak)
8229 ok_tprel = true;
8230 else if (sym_sec != NULL
8231 && sym_sec->output_section != NULL)
8233 value += sym_sec->output_offset;
8234 value += sym_sec->output_section->vma;
8235 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8236 /* Note that even though the prefix insns
8237 allow a 1<<33 offset we use the same test
8238 as for addis;addi. There may be a mix of
8239 pcrel and non-pcrel code and the decision
8240 to optimise is per symbol, not per TLS
8241 sequence. */
8242 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8246 r_type = ELF64_R_TYPE (rel->r_info);
8247 /* If this section has old-style __tls_get_addr calls
8248 without marker relocs, then check that each
8249 __tls_get_addr call reloc is preceded by a reloc
8250 that conceivably belongs to the __tls_get_addr arg
8251 setup insn. If we don't find matching arg setup
8252 relocs, don't do any tls optimization. */
8253 if (pass == 0
8254 && sec->nomark_tls_get_addr
8255 && h != NULL
8256 && is_tls_get_addr (h, htab)
8257 && !found_tls_get_addr_arg
8258 && is_branch_reloc (r_type))
8260 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8261 "TLS optimization disabled\n"),
8262 ibfd, sec, rel->r_offset);
8263 ret = true;
8264 goto err_free_rel;
8267 found_tls_get_addr_arg = 0;
8268 switch (r_type)
8270 case R_PPC64_GOT_TLSLD16:
8271 case R_PPC64_GOT_TLSLD16_LO:
8272 case R_PPC64_GOT_TLSLD_PCREL34:
8273 expecting_tls_get_addr = 1;
8274 found_tls_get_addr_arg = 1;
8275 /* Fall through. */
8277 case R_PPC64_GOT_TLSLD16_HI:
8278 case R_PPC64_GOT_TLSLD16_HA:
8279 /* These relocs should never be against a symbol
8280 defined in a shared lib. Leave them alone if
8281 that turns out to be the case. */
8282 if (!is_local)
8283 continue;
8285 /* LD -> LE */
8286 tls_set = 0;
8287 tls_clear = TLS_LD;
8288 tls_type = TLS_TLS | TLS_LD;
8289 break;
8291 case R_PPC64_GOT_TLSGD16:
8292 case R_PPC64_GOT_TLSGD16_LO:
8293 case R_PPC64_GOT_TLSGD_PCREL34:
8294 expecting_tls_get_addr = 1;
8295 found_tls_get_addr_arg = 1;
8296 /* Fall through. */
8298 case R_PPC64_GOT_TLSGD16_HI:
8299 case R_PPC64_GOT_TLSGD16_HA:
8300 if (ok_tprel)
8301 /* GD -> LE */
8302 tls_set = 0;
8303 else
8304 /* GD -> IE */
8305 tls_set = TLS_TLS | TLS_GDIE;
8306 tls_clear = TLS_GD;
8307 tls_type = TLS_TLS | TLS_GD;
8308 break;
8310 case R_PPC64_GOT_TPREL_PCREL34:
8311 case R_PPC64_GOT_TPREL16_DS:
8312 case R_PPC64_GOT_TPREL16_LO_DS:
8313 case R_PPC64_GOT_TPREL16_HI:
8314 case R_PPC64_GOT_TPREL16_HA:
8315 if (ok_tprel)
8317 /* IE -> LE */
8318 tls_set = 0;
8319 tls_clear = TLS_TPREL;
8320 tls_type = TLS_TLS | TLS_TPREL;
8321 break;
8323 continue;
8325 case R_PPC64_TLSLD:
8326 if (!is_local)
8327 continue;
8328 /* Fall through. */
8329 case R_PPC64_TLSGD:
8330 if (rel + 1 < relend
8331 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8333 if (pass != 0
8334 && (ELF64_R_TYPE (rel[1].r_info)
8335 != R_PPC64_PLTSEQ)
8336 && (ELF64_R_TYPE (rel[1].r_info)
8337 != R_PPC64_PLTSEQ_NOTOC))
8339 r_symndx = ELF64_R_SYM (rel[1].r_info);
8340 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8341 r_symndx, ibfd))
8342 goto err_free_rel;
8343 if (h != NULL)
8345 struct plt_entry *ent = NULL;
8347 for (ent = h->plt.plist;
8348 ent != NULL;
8349 ent = ent->next)
8350 if (ent->addend == rel[1].r_addend)
8351 break;
8353 if (ent != NULL
8354 && ent->plt.refcount > 0)
8355 ent->plt.refcount -= 1;
8358 continue;
8360 found_tls_get_addr_arg = 1;
8361 /* Fall through. */
8363 case R_PPC64_TLS:
8364 case R_PPC64_TOC16:
8365 case R_PPC64_TOC16_LO:
8366 if (sym_sec == NULL || sym_sec != toc)
8367 continue;
8369 /* Mark this toc entry as referenced by a TLS
8370 code sequence. We can do that now in the
8371 case of R_PPC64_TLS, and after checking for
8372 tls_get_addr for the TOC16 relocs. */
8373 if (toc_ref == NULL)
8374 toc_ref
8375 = bfd_zmalloc (toc->output_section->rawsize / 8);
8376 if (toc_ref == NULL)
8377 goto err_free_rel;
8379 if (h != NULL)
8380 value = h->root.u.def.value;
8381 else
8382 value = sym->st_value;
8383 value += rel->r_addend;
8384 if (value % 8 != 0)
8385 continue;
8386 BFD_ASSERT (value < toc->size
8387 && toc->output_offset % 8 == 0);
8388 toc_ref_index = (value + toc->output_offset) / 8;
8389 if (r_type == R_PPC64_TLS
8390 || r_type == R_PPC64_TLSGD
8391 || r_type == R_PPC64_TLSLD)
8393 toc_ref[toc_ref_index] = 1;
8394 continue;
8397 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8398 continue;
8400 tls_set = 0;
8401 tls_clear = 0;
8402 expecting_tls_get_addr = 2;
8403 break;
8405 case R_PPC64_TPREL64:
8406 if (pass == 0
8407 || sec != toc
8408 || toc_ref == NULL
8409 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8410 continue;
8411 if (ok_tprel)
8413 /* IE -> LE */
8414 tls_set = TLS_EXPLICIT;
8415 tls_clear = TLS_TPREL;
8416 break;
8418 continue;
8420 case R_PPC64_DTPMOD64:
8421 if (pass == 0
8422 || sec != toc
8423 || toc_ref == NULL
8424 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8425 continue;
8426 if (rel + 1 < relend
8427 && (rel[1].r_info
8428 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8429 && rel[1].r_offset == rel->r_offset + 8)
8431 if (ok_tprel)
8432 /* GD -> LE */
8433 tls_set = TLS_EXPLICIT | TLS_GD;
8434 else
8435 /* GD -> IE */
8436 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8437 tls_clear = TLS_GD;
8439 else
8441 if (!is_local)
8442 continue;
8444 /* LD -> LE */
8445 tls_set = TLS_EXPLICIT;
8446 tls_clear = TLS_LD;
8448 break;
8450 case R_PPC64_TPREL16_HA:
8451 if (pass == 0)
8453 unsigned char buf[4];
8454 unsigned int insn;
8455 bfd_vma off = rel->r_offset & ~3;
8456 if (!bfd_get_section_contents (ibfd, sec, buf,
8457 off, 4))
8458 goto err_free_rel;
8459 insn = bfd_get_32 (ibfd, buf);
8460 /* addis rt,13,imm */
8461 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8462 != ((15u << 26) | (13 << 16)))
8464 /* xgettext:c-format */
8465 info->callbacks->minfo
8466 (_("%H: warning: %s unexpected insn %#x.\n"),
8467 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8468 htab->do_tls_opt = 0;
8471 continue;
8473 case R_PPC64_TPREL16_HI:
8474 case R_PPC64_TPREL16_HIGH:
8475 case R_PPC64_TPREL16_HIGHA:
8476 case R_PPC64_TPREL16_HIGHER:
8477 case R_PPC64_TPREL16_HIGHERA:
8478 case R_PPC64_TPREL16_HIGHEST:
8479 case R_PPC64_TPREL16_HIGHESTA:
8480 /* These can all be used in sequences along with
8481 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8482 able to verify easily. */
8483 htab->do_tls_opt = 0;
8484 continue;
8486 default:
8487 continue;
8490 if (pass == 0)
8492 if (!expecting_tls_get_addr
8493 || !sec->nomark_tls_get_addr)
8494 continue;
8496 if (rel + 1 < relend
8497 && branch_reloc_hash_match (ibfd, rel + 1,
8498 htab->tls_get_addr_fd,
8499 htab->tga_desc_fd,
8500 htab->tls_get_addr,
8501 htab->tga_desc))
8503 if (expecting_tls_get_addr == 2)
8505 /* Check for toc tls entries. */
8506 unsigned char *toc_tls;
8507 int retval;
8509 retval = get_tls_mask (&toc_tls, NULL, NULL,
8510 &locsyms,
8511 rel, ibfd);
8512 if (retval == 0)
8513 goto err_free_rel;
8514 if (toc_tls != NULL)
8516 if ((*toc_tls & TLS_TLS) != 0
8517 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8518 found_tls_get_addr_arg = 1;
8519 if (retval > 1)
8520 toc_ref[toc_ref_index] = 1;
8523 continue;
8526 /* Uh oh, we didn't find the expected call. We
8527 could just mark this symbol to exclude it
8528 from tls optimization but it's safer to skip
8529 the entire optimization. */
8530 /* xgettext:c-format */
8531 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8532 "TLS optimization disabled\n"),
8533 ibfd, sec, rel->r_offset);
8534 ret = true;
8535 goto err_free_rel;
8538 /* If we don't have old-style __tls_get_addr calls
8539 without TLSGD/TLSLD marker relocs, and we haven't
8540 found a new-style __tls_get_addr call with a
8541 marker for this symbol, then we either have a
8542 broken object file or an -mlongcall style
8543 indirect call to __tls_get_addr without a marker.
8544 Disable optimization in this case. */
8545 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8546 && (tls_set & TLS_EXPLICIT) == 0
8547 && !sec->nomark_tls_get_addr
8548 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8549 != (TLS_TLS | TLS_MARK)))
8550 continue;
8552 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8554 struct plt_entry *ent = NULL;
8556 if (htab->tls_get_addr_fd != NULL)
8557 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8558 ent != NULL;
8559 ent = ent->next)
8560 if (ent->addend == 0)
8561 break;
8563 if (ent == NULL && htab->tga_desc_fd != NULL)
8564 for (ent = htab->tga_desc_fd->elf.plt.plist;
8565 ent != NULL;
8566 ent = ent->next)
8567 if (ent->addend == 0)
8568 break;
8570 if (ent == NULL && htab->tls_get_addr != NULL)
8571 for (ent = htab->tls_get_addr->elf.plt.plist;
8572 ent != NULL;
8573 ent = ent->next)
8574 if (ent->addend == 0)
8575 break;
8577 if (ent == NULL && htab->tga_desc != NULL)
8578 for (ent = htab->tga_desc->elf.plt.plist;
8579 ent != NULL;
8580 ent = ent->next)
8581 if (ent->addend == 0)
8582 break;
8584 if (ent != NULL
8585 && ent->plt.refcount > 0)
8586 ent->plt.refcount -= 1;
8589 if (tls_clear == 0)
8590 continue;
8592 if ((tls_set & TLS_EXPLICIT) == 0)
8594 struct got_entry *ent;
8596 /* Adjust got entry for this reloc. */
8597 if (h != NULL)
8598 ent = h->got.glist;
8599 else
8600 ent = elf_local_got_ents (ibfd)[r_symndx];
8602 for (; ent != NULL; ent = ent->next)
8603 if (ent->addend == rel->r_addend
8604 && ent->owner == ibfd
8605 && ent->tls_type == tls_type)
8606 break;
8607 if (ent == NULL)
8608 abort ();
8610 if (tls_set == 0)
8612 /* We managed to get rid of a got entry. */
8613 if (ent->got.refcount > 0)
8614 ent->got.refcount -= 1;
8617 else
8619 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8620 we'll lose one or two dyn relocs. */
8621 if (!dec_dynrel_count (rel, sec, info,
8622 NULL, h, sym))
8623 return false;
8625 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8627 if (!dec_dynrel_count (rel + 1, sec, info,
8628 NULL, h, sym))
8629 return false;
8633 *tls_mask |= tls_set & 0xff;
8634 *tls_mask &= ~tls_clear;
8637 if (elf_section_data (sec)->relocs != relstart)
8638 free (relstart);
8641 if (locsyms != NULL
8642 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8644 if (!info->keep_memory)
8645 free (locsyms);
8646 else
8647 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8651 free (toc_ref);
8652 return true;
8655 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8656 the values of any global symbols in a toc section that has been
8657 edited. Globals in toc sections should be a rarity, so this function
8658 sets a flag if any are found in toc sections other than the one just
8659 edited, so that further hash table traversals can be avoided. */
8661 struct adjust_toc_info
8663 asection *toc;
8664 unsigned long *skip;
8665 bool global_toc_syms;
8668 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8670 static bool
8671 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8673 struct ppc_link_hash_entry *eh;
8674 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8675 unsigned long i;
8677 if (h->root.type != bfd_link_hash_defined
8678 && h->root.type != bfd_link_hash_defweak)
8679 return true;
8681 eh = ppc_elf_hash_entry (h);
8682 if (eh->adjust_done)
8683 return true;
8685 if (eh->elf.root.u.def.section == toc_inf->toc)
8687 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8688 i = toc_inf->toc->rawsize >> 3;
8689 else
8690 i = eh->elf.root.u.def.value >> 3;
8692 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8694 _bfd_error_handler
8695 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8697 ++i;
8698 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8699 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8702 eh->elf.root.u.def.value -= toc_inf->skip[i];
8703 eh->adjust_done = 1;
8705 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8706 toc_inf->global_toc_syms = true;
8708 return true;
8711 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8712 on a _LO variety toc/got reloc. */
8714 static bool
8715 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8717 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8718 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8719 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8720 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8721 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8722 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8723 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8724 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8725 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8726 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8727 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8728 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8729 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8730 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8731 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8732 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8733 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8734 /* Exclude lfqu by testing reloc. If relocs are ever
8735 defined for the reduced D field in psq_lu then those
8736 will need testing too. */
8737 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8738 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8739 && (insn & 1) == 0)
8740 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8741 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8742 /* Exclude stfqu. psq_stu as above for psq_lu. */
8743 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8744 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8745 && (insn & 1) == 0));
8748 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8749 pld ra,symbol@got@pcrel
8750 load/store rt,off(ra)
8752 pla ra,symbol@pcrel
8753 load/store rt,off(ra)
8754 may be translated to
8755 pload/pstore rt,symbol+off@pcrel
8756 nop.
8757 This function returns true if the optimization is possible, placing
8758 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8760 On entry to this function, the linker has already determined that
8761 the pld can be replaced with pla: *PINSN1 is that pla insn,
8762 while *PINSN2 is the second instruction. */
8764 static bool
8765 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8767 uint64_t insn1 = *pinsn1;
8768 uint64_t insn2 = *pinsn2;
8769 bfd_signed_vma off;
8771 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8773 /* Check that regs match. */
8774 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8775 return false;
8777 /* P8LS or PMLS form, non-pcrel. */
8778 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8779 return false;
8781 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8782 *pinsn2 = PNOP;
8783 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8784 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8785 return true;
8788 insn2 >>= 32;
8790 /* Check that regs match. */
8791 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8792 return false;
8794 switch ((insn2 >> 26) & 63)
8796 default:
8797 return false;
8799 case 32: /* lwz */
8800 case 34: /* lbz */
8801 case 36: /* stw */
8802 case 38: /* stb */
8803 case 40: /* lhz */
8804 case 42: /* lha */
8805 case 44: /* sth */
8806 case 48: /* lfs */
8807 case 50: /* lfd */
8808 case 52: /* stfs */
8809 case 54: /* stfd */
8810 /* These are the PMLS cases, where we just need to tack a prefix
8811 on the insn. */
8812 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8813 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8814 off = insn2 & 0xffff;
8815 break;
8817 case 58: /* lwa, ld */
8818 if ((insn2 & 1) != 0)
8819 return false;
8820 insn1 = ((1ULL << 58) | (1ULL << 52)
8821 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8822 | (insn2 & (31ULL << 21)));
8823 off = insn2 & 0xfffc;
8824 break;
8826 case 57: /* lxsd, lxssp */
8827 if ((insn2 & 3) < 2)
8828 return false;
8829 insn1 = ((1ULL << 58) | (1ULL << 52)
8830 | ((40ULL | (insn2 & 3)) << 26)
8831 | (insn2 & (31ULL << 21)));
8832 off = insn2 & 0xfffc;
8833 break;
8835 case 61: /* stxsd, stxssp, lxv, stxv */
8836 if ((insn2 & 3) == 0)
8837 return false;
8838 else if ((insn2 & 3) >= 2)
8840 insn1 = ((1ULL << 58) | (1ULL << 52)
8841 | ((44ULL | (insn2 & 3)) << 26)
8842 | (insn2 & (31ULL << 21)));
8843 off = insn2 & 0xfffc;
8845 else
8847 insn1 = ((1ULL << 58) | (1ULL << 52)
8848 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8849 | (insn2 & (31ULL << 21)));
8850 off = insn2 & 0xfff0;
8852 break;
8854 case 56: /* lq */
8855 insn1 = ((1ULL << 58) | (1ULL << 52)
8856 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8857 off = insn2 & 0xffff;
8858 break;
8860 case 6: /* lxvp, stxvp */
8861 if ((insn2 & 0xe) != 0)
8862 return false;
8863 insn1 = ((1ULL << 58) | (1ULL << 52)
8864 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8865 | (insn2 & (31ULL << 21)));
8866 off = insn2 & 0xfff0;
8867 break;
8869 case 62: /* std, stq */
8870 if ((insn2 & 1) != 0)
8871 return false;
8872 insn1 = ((1ULL << 58) | (1ULL << 52)
8873 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8874 | (insn2 & (31ULL << 21)));
8875 off = insn2 & 0xfffc;
8876 break;
8879 *pinsn1 = insn1;
8880 *pinsn2 = (uint64_t) NOP << 32;
8881 *poff = (off ^ 0x8000) - 0x8000;
8882 return true;
8885 /* Examine all relocs referencing .toc sections in order to remove
8886 unused .toc entries. */
8888 bool
8889 ppc64_elf_edit_toc (struct bfd_link_info *info)
8891 bfd *ibfd;
8892 struct adjust_toc_info toc_inf;
8893 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8895 htab->do_toc_opt = 1;
8896 toc_inf.global_toc_syms = true;
8897 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8899 asection *toc, *sec;
8900 Elf_Internal_Shdr *symtab_hdr;
8901 Elf_Internal_Sym *local_syms;
8902 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8903 unsigned long *skip, *drop;
8904 unsigned char *used;
8905 unsigned char *keep, last, some_unused;
8907 if (!is_ppc64_elf (ibfd))
8908 continue;
8910 toc = bfd_get_section_by_name (ibfd, ".toc");
8911 if (toc == NULL
8912 || toc->size == 0
8913 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8914 || discarded_section (toc))
8915 continue;
8917 toc_relocs = NULL;
8918 local_syms = NULL;
8919 symtab_hdr = &elf_symtab_hdr (ibfd);
8921 /* Look at sections dropped from the final link. */
8922 skip = NULL;
8923 relstart = NULL;
8924 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8926 if (sec->reloc_count == 0
8927 || !discarded_section (sec)
8928 || get_opd_info (sec)
8929 || (sec->flags & SEC_ALLOC) == 0
8930 || (sec->flags & SEC_DEBUGGING) != 0)
8931 continue;
8933 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8934 if (relstart == NULL)
8935 goto error_ret;
8937 /* Run through the relocs to see which toc entries might be
8938 unused. */
8939 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8941 enum elf_ppc64_reloc_type r_type;
8942 unsigned long r_symndx;
8943 asection *sym_sec;
8944 struct elf_link_hash_entry *h;
8945 Elf_Internal_Sym *sym;
8946 bfd_vma val;
8948 r_type = ELF64_R_TYPE (rel->r_info);
8949 switch (r_type)
8951 default:
8952 continue;
8954 case R_PPC64_TOC16:
8955 case R_PPC64_TOC16_LO:
8956 case R_PPC64_TOC16_HI:
8957 case R_PPC64_TOC16_HA:
8958 case R_PPC64_TOC16_DS:
8959 case R_PPC64_TOC16_LO_DS:
8960 break;
8963 r_symndx = ELF64_R_SYM (rel->r_info);
8964 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8965 r_symndx, ibfd))
8966 goto error_ret;
8968 if (sym_sec != toc)
8969 continue;
8971 if (h != NULL)
8972 val = h->root.u.def.value;
8973 else
8974 val = sym->st_value;
8975 val += rel->r_addend;
8977 if (val >= toc->size)
8978 continue;
8980 /* Anything in the toc ought to be aligned to 8 bytes.
8981 If not, don't mark as unused. */
8982 if (val & 7)
8983 continue;
8985 if (skip == NULL)
8987 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8988 if (skip == NULL)
8989 goto error_ret;
8992 skip[val >> 3] = ref_from_discarded;
8995 if (elf_section_data (sec)->relocs != relstart)
8996 free (relstart);
8999 /* For largetoc loads of address constants, we can convert
9000 . addis rx,2,addr@got@ha
9001 . ld ry,addr@got@l(rx)
9003 . addis rx,2,addr@toc@ha
9004 . addi ry,rx,addr@toc@l
9005 when addr is within 2G of the toc pointer. This then means
9006 that the word storing "addr" in the toc is no longer needed. */
9008 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9009 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9010 && toc->reloc_count != 0)
9012 /* Read toc relocs. */
9013 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9014 info->keep_memory);
9015 if (toc_relocs == NULL)
9016 goto error_ret;
9018 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9020 enum elf_ppc64_reloc_type r_type;
9021 unsigned long r_symndx;
9022 asection *sym_sec;
9023 struct elf_link_hash_entry *h;
9024 Elf_Internal_Sym *sym;
9025 bfd_vma val, addr;
9027 r_type = ELF64_R_TYPE (rel->r_info);
9028 if (r_type != R_PPC64_ADDR64)
9029 continue;
9031 r_symndx = ELF64_R_SYM (rel->r_info);
9032 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9033 r_symndx, ibfd))
9034 goto error_ret;
9036 if (sym_sec == NULL
9037 || sym_sec->output_section == NULL
9038 || discarded_section (sym_sec))
9039 continue;
9041 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9042 || (bfd_link_pic (info)
9043 && sym_sec == bfd_abs_section_ptr))
9044 continue;
9046 if (h != NULL)
9048 if (h->type == STT_GNU_IFUNC)
9049 continue;
9050 val = h->root.u.def.value;
9052 else
9054 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9055 continue;
9056 val = sym->st_value;
9058 val += rel->r_addend;
9059 val += sym_sec->output_section->vma + sym_sec->output_offset;
9061 /* We don't yet know the exact toc pointer value, but we
9062 know it will be somewhere in the toc section. Don't
9063 optimize if the difference from any possible toc
9064 pointer is outside [ff..f80008000, 7fff7fff]. */
9065 addr = toc->output_section->vma + TOC_BASE_OFF;
9066 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9067 continue;
9069 addr = toc->output_section->vma + toc->output_section->rawsize;
9070 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9071 continue;
9073 if (skip == NULL)
9075 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9076 if (skip == NULL)
9077 goto error_ret;
9080 skip[rel->r_offset >> 3]
9081 |= can_optimize | ((rel - toc_relocs) << 2);
9085 if (skip == NULL)
9086 continue;
9088 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9089 if (used == NULL)
9091 error_ret:
9092 if (symtab_hdr->contents != (unsigned char *) local_syms)
9093 free (local_syms);
9094 if (sec != NULL
9095 && elf_section_data (sec)->relocs != relstart)
9096 free (relstart);
9097 if (elf_section_data (toc)->relocs != toc_relocs)
9098 free (toc_relocs);
9099 free (skip);
9100 return false;
9103 /* Now check all kept sections that might reference the toc.
9104 Check the toc itself last. */
9105 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9106 : ibfd->sections);
9107 sec != NULL;
9108 sec = (sec == toc ? NULL
9109 : sec->next == NULL ? toc
9110 : sec->next == toc && toc->next ? toc->next
9111 : sec->next))
9113 int repeat;
9115 if (sec->reloc_count == 0
9116 || discarded_section (sec)
9117 || get_opd_info (sec)
9118 || (sec->flags & SEC_ALLOC) == 0
9119 || (sec->flags & SEC_DEBUGGING) != 0)
9120 continue;
9122 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9123 info->keep_memory);
9124 if (relstart == NULL)
9126 free (used);
9127 goto error_ret;
9130 /* Mark toc entries referenced as used. */
9133 repeat = 0;
9134 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9136 enum elf_ppc64_reloc_type r_type;
9137 unsigned long r_symndx;
9138 asection *sym_sec;
9139 struct elf_link_hash_entry *h;
9140 Elf_Internal_Sym *sym;
9141 bfd_vma val;
9143 r_type = ELF64_R_TYPE (rel->r_info);
9144 switch (r_type)
9146 case R_PPC64_TOC16:
9147 case R_PPC64_TOC16_LO:
9148 case R_PPC64_TOC16_HI:
9149 case R_PPC64_TOC16_HA:
9150 case R_PPC64_TOC16_DS:
9151 case R_PPC64_TOC16_LO_DS:
9152 /* In case we're taking addresses of toc entries. */
9153 case R_PPC64_ADDR64:
9154 break;
9156 default:
9157 continue;
9160 r_symndx = ELF64_R_SYM (rel->r_info);
9161 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9162 r_symndx, ibfd))
9164 free (used);
9165 goto error_ret;
9168 if (sym_sec != toc)
9169 continue;
9171 if (h != NULL)
9172 val = h->root.u.def.value;
9173 else
9174 val = sym->st_value;
9175 val += rel->r_addend;
9177 if (val >= toc->size)
9178 continue;
9180 if ((skip[val >> 3] & can_optimize) != 0)
9182 bfd_vma off;
9183 unsigned char opc;
9185 switch (r_type)
9187 case R_PPC64_TOC16_HA:
9188 break;
9190 case R_PPC64_TOC16_LO_DS:
9191 off = rel->r_offset;
9192 off += (bfd_big_endian (ibfd) ? -2 : 3);
9193 if (!bfd_get_section_contents (ibfd, sec, &opc,
9194 off, 1))
9196 free (used);
9197 goto error_ret;
9199 if ((opc & (0x3f << 2)) == (58u << 2))
9200 break;
9201 /* Fall through. */
9203 default:
9204 /* Wrong sort of reloc, or not a ld. We may
9205 as well clear ref_from_discarded too. */
9206 skip[val >> 3] = 0;
9210 if (sec != toc)
9211 used[val >> 3] = 1;
9212 /* For the toc section, we only mark as used if this
9213 entry itself isn't unused. */
9214 else if ((used[rel->r_offset >> 3]
9215 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9216 && !used[val >> 3])
9218 /* Do all the relocs again, to catch reference
9219 chains. */
9220 repeat = 1;
9221 used[val >> 3] = 1;
9225 while (repeat);
9227 if (elf_section_data (sec)->relocs != relstart)
9228 free (relstart);
9231 /* Merge the used and skip arrays. Assume that TOC
9232 doublewords not appearing as either used or unused belong
9233 to an entry more than one doubleword in size. */
9234 for (drop = skip, keep = used, last = 0, some_unused = 0;
9235 drop < skip + (toc->size + 7) / 8;
9236 ++drop, ++keep)
9238 if (*keep)
9240 *drop &= ~ref_from_discarded;
9241 if ((*drop & can_optimize) != 0)
9242 some_unused = 1;
9243 last = 0;
9245 else if ((*drop & ref_from_discarded) != 0)
9247 some_unused = 1;
9248 last = ref_from_discarded;
9250 else
9251 *drop = last;
9254 free (used);
9256 if (some_unused)
9258 bfd_byte *contents, *src;
9259 unsigned long off;
9260 Elf_Internal_Sym *sym;
9261 bool local_toc_syms = false;
9263 /* Shuffle the toc contents, and at the same time convert the
9264 skip array from booleans into offsets. */
9265 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9266 goto error_ret;
9268 elf_section_data (toc)->this_hdr.contents = contents;
9270 for (src = contents, off = 0, drop = skip;
9271 src < contents + toc->size;
9272 src += 8, ++drop)
9274 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9275 off += 8;
9276 else if (off != 0)
9278 *drop = off;
9279 memcpy (src - off, src, 8);
9282 *drop = off;
9283 toc->rawsize = toc->size;
9284 toc->size = src - contents - off;
9286 /* Adjust addends for relocs against the toc section sym,
9287 and optimize any accesses we can. */
9288 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9290 if (sec->reloc_count == 0
9291 || discarded_section (sec))
9292 continue;
9294 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9295 info->keep_memory);
9296 if (relstart == NULL)
9297 goto error_ret;
9299 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9301 enum elf_ppc64_reloc_type r_type;
9302 unsigned long r_symndx;
9303 asection *sym_sec;
9304 struct elf_link_hash_entry *h;
9305 bfd_vma val;
9307 r_type = ELF64_R_TYPE (rel->r_info);
9308 switch (r_type)
9310 default:
9311 continue;
9313 case R_PPC64_TOC16:
9314 case R_PPC64_TOC16_LO:
9315 case R_PPC64_TOC16_HI:
9316 case R_PPC64_TOC16_HA:
9317 case R_PPC64_TOC16_DS:
9318 case R_PPC64_TOC16_LO_DS:
9319 case R_PPC64_ADDR64:
9320 break;
9323 r_symndx = ELF64_R_SYM (rel->r_info);
9324 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9325 r_symndx, ibfd))
9326 goto error_ret;
9328 if (sym_sec != toc)
9329 continue;
9331 if (h != NULL)
9332 val = h->root.u.def.value;
9333 else
9335 val = sym->st_value;
9336 if (val != 0)
9337 local_toc_syms = true;
9340 val += rel->r_addend;
9342 if (val > toc->rawsize)
9343 val = toc->rawsize;
9344 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9345 continue;
9346 else if ((skip[val >> 3] & can_optimize) != 0)
9348 Elf_Internal_Rela *tocrel
9349 = toc_relocs + (skip[val >> 3] >> 2);
9350 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9352 switch (r_type)
9354 case R_PPC64_TOC16_HA:
9355 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9356 break;
9358 case R_PPC64_TOC16_LO_DS:
9359 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9360 break;
9362 default:
9363 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9364 ppc_howto_init ();
9365 info->callbacks->einfo
9366 /* xgettext:c-format */
9367 (_("%H: %s references "
9368 "optimized away TOC entry\n"),
9369 ibfd, sec, rel->r_offset,
9370 ppc64_elf_howto_table[r_type]->name);
9371 bfd_set_error (bfd_error_bad_value);
9372 goto error_ret;
9374 rel->r_addend = tocrel->r_addend;
9375 elf_section_data (sec)->relocs = relstart;
9376 continue;
9379 if (h != NULL || sym->st_value != 0)
9380 continue;
9382 rel->r_addend -= skip[val >> 3];
9383 elf_section_data (sec)->relocs = relstart;
9386 if (elf_section_data (sec)->relocs != relstart)
9387 free (relstart);
9390 /* We shouldn't have local or global symbols defined in the TOC,
9391 but handle them anyway. */
9392 if (local_syms != NULL)
9393 for (sym = local_syms;
9394 sym < local_syms + symtab_hdr->sh_info;
9395 ++sym)
9396 if (sym->st_value != 0
9397 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9399 unsigned long i;
9401 if (sym->st_value > toc->rawsize)
9402 i = toc->rawsize >> 3;
9403 else
9404 i = sym->st_value >> 3;
9406 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9408 if (local_toc_syms)
9409 _bfd_error_handler
9410 (_("%s defined on removed toc entry"),
9411 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9413 ++i;
9414 while ((skip[i] & (ref_from_discarded | can_optimize)));
9415 sym->st_value = (bfd_vma) i << 3;
9418 sym->st_value -= skip[i];
9419 symtab_hdr->contents = (unsigned char *) local_syms;
9422 /* Adjust any global syms defined in this toc input section. */
9423 if (toc_inf.global_toc_syms)
9425 toc_inf.toc = toc;
9426 toc_inf.skip = skip;
9427 toc_inf.global_toc_syms = false;
9428 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9429 &toc_inf);
9432 if (toc->reloc_count != 0)
9434 Elf_Internal_Shdr *rel_hdr;
9435 Elf_Internal_Rela *wrel;
9436 bfd_size_type sz;
9438 /* Remove unused toc relocs, and adjust those we keep. */
9439 if (toc_relocs == NULL)
9440 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9441 info->keep_memory);
9442 if (toc_relocs == NULL)
9443 goto error_ret;
9445 wrel = toc_relocs;
9446 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9447 if ((skip[rel->r_offset >> 3]
9448 & (ref_from_discarded | can_optimize)) == 0)
9450 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9451 wrel->r_info = rel->r_info;
9452 wrel->r_addend = rel->r_addend;
9453 ++wrel;
9455 else if (!dec_dynrel_count (rel, toc, info,
9456 &local_syms, NULL, NULL))
9457 goto error_ret;
9459 elf_section_data (toc)->relocs = toc_relocs;
9460 toc->reloc_count = wrel - toc_relocs;
9461 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9462 sz = rel_hdr->sh_entsize;
9463 rel_hdr->sh_size = toc->reloc_count * sz;
9466 else if (elf_section_data (toc)->relocs != toc_relocs)
9467 free (toc_relocs);
9469 if (local_syms != NULL
9470 && symtab_hdr->contents != (unsigned char *) local_syms)
9472 if (!info->keep_memory)
9473 free (local_syms);
9474 else
9475 symtab_hdr->contents = (unsigned char *) local_syms;
9477 free (skip);
9480 /* Look for cases where we can change an indirect GOT access to
9481 a GOT relative or PC relative access, possibly reducing the
9482 number of GOT entries. */
9483 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9485 asection *sec;
9486 Elf_Internal_Shdr *symtab_hdr;
9487 Elf_Internal_Sym *local_syms;
9488 Elf_Internal_Rela *relstart, *rel;
9489 bfd_vma got;
9491 if (!is_ppc64_elf (ibfd))
9492 continue;
9494 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9495 continue;
9497 sec = ppc64_elf_tdata (ibfd)->got;
9498 got = 0;
9499 if (sec != NULL)
9500 got = sec->output_section->vma + sec->output_offset + 0x8000;
9502 local_syms = NULL;
9503 symtab_hdr = &elf_symtab_hdr (ibfd);
9505 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9507 if (sec->reloc_count == 0
9508 || !ppc64_elf_section_data (sec)->has_optrel
9509 || discarded_section (sec))
9510 continue;
9512 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9513 info->keep_memory);
9514 if (relstart == NULL)
9516 got_error_ret:
9517 if (symtab_hdr->contents != (unsigned char *) local_syms)
9518 free (local_syms);
9519 if (sec != NULL
9520 && elf_section_data (sec)->relocs != relstart)
9521 free (relstart);
9522 return false;
9525 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9527 enum elf_ppc64_reloc_type r_type;
9528 unsigned long r_symndx;
9529 Elf_Internal_Sym *sym;
9530 asection *sym_sec;
9531 struct elf_link_hash_entry *h;
9532 struct got_entry *ent;
9533 bfd_vma val, pc;
9534 unsigned char buf[8];
9535 unsigned int insn;
9536 enum {no_check, check_lo, check_ha} insn_check;
9538 r_type = ELF64_R_TYPE (rel->r_info);
9539 switch (r_type)
9541 default:
9542 insn_check = no_check;
9543 break;
9545 case R_PPC64_PLT16_HA:
9546 case R_PPC64_GOT_TLSLD16_HA:
9547 case R_PPC64_GOT_TLSGD16_HA:
9548 case R_PPC64_GOT_TPREL16_HA:
9549 case R_PPC64_GOT_DTPREL16_HA:
9550 case R_PPC64_GOT16_HA:
9551 case R_PPC64_TOC16_HA:
9552 insn_check = check_ha;
9553 break;
9555 case R_PPC64_PLT16_LO:
9556 case R_PPC64_PLT16_LO_DS:
9557 case R_PPC64_GOT_TLSLD16_LO:
9558 case R_PPC64_GOT_TLSGD16_LO:
9559 case R_PPC64_GOT_TPREL16_LO_DS:
9560 case R_PPC64_GOT_DTPREL16_LO_DS:
9561 case R_PPC64_GOT16_LO:
9562 case R_PPC64_GOT16_LO_DS:
9563 case R_PPC64_TOC16_LO:
9564 case R_PPC64_TOC16_LO_DS:
9565 insn_check = check_lo;
9566 break;
9569 if (insn_check != no_check)
9571 bfd_vma off = rel->r_offset & ~3;
9573 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9574 goto got_error_ret;
9576 insn = bfd_get_32 (ibfd, buf);
9577 if (insn_check == check_lo
9578 ? !ok_lo_toc_insn (insn, r_type)
9579 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9580 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9582 char str[12];
9584 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9585 sprintf (str, "%#08x", insn);
9586 info->callbacks->einfo
9587 /* xgettext:c-format */
9588 (_("%H: got/toc optimization is not supported for"
9589 " %s instruction\n"),
9590 ibfd, sec, rel->r_offset & ~3, str);
9591 continue;
9595 switch (r_type)
9597 /* Note that we don't delete GOT entries for
9598 R_PPC64_GOT16_DS since we'd need a lot more
9599 analysis. For starters, the preliminary layout is
9600 before the GOT, PLT, dynamic sections and stubs are
9601 laid out. Then we'd need to allow for changes in
9602 distance between sections caused by alignment. */
9603 default:
9604 continue;
9606 case R_PPC64_GOT16_HA:
9607 case R_PPC64_GOT16_LO_DS:
9608 case R_PPC64_GOT_PCREL34:
9609 break;
9612 r_symndx = ELF64_R_SYM (rel->r_info);
9613 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9614 r_symndx, ibfd))
9615 goto got_error_ret;
9617 if (sym_sec == NULL
9618 || sym_sec->output_section == NULL
9619 || discarded_section (sym_sec))
9620 continue;
9622 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9623 continue;
9625 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9626 || (bfd_link_pic (info)
9627 && sym_sec == bfd_abs_section_ptr))
9628 continue;
9630 if (h != NULL)
9631 val = h->root.u.def.value;
9632 else
9633 val = sym->st_value;
9634 val += rel->r_addend;
9635 val += sym_sec->output_section->vma + sym_sec->output_offset;
9637 /* Fudge factor to allow for the fact that the preliminary layout
9638 isn't exact. Reduce limits by this factor. */
9639 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9641 switch (r_type)
9643 default:
9644 continue;
9646 case R_PPC64_GOT16_HA:
9647 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9648 >= LIMIT_ADJUST (0x100000000ULL))
9649 continue;
9651 if (!bfd_get_section_contents (ibfd, sec, buf,
9652 rel->r_offset & ~3, 4))
9653 goto got_error_ret;
9654 insn = bfd_get_32 (ibfd, buf);
9655 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9656 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9657 continue;
9658 break;
9660 case R_PPC64_GOT16_LO_DS:
9661 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9662 >= LIMIT_ADJUST (0x100000000ULL))
9663 continue;
9664 if (!bfd_get_section_contents (ibfd, sec, buf,
9665 rel->r_offset & ~3, 4))
9666 goto got_error_ret;
9667 insn = bfd_get_32 (ibfd, buf);
9668 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9669 continue;
9670 break;
9672 case R_PPC64_GOT_PCREL34:
9673 pc = rel->r_offset;
9674 pc += sec->output_section->vma + sec->output_offset;
9675 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9676 >= LIMIT_ADJUST (1ULL << 34))
9677 continue;
9678 if (!bfd_get_section_contents (ibfd, sec, buf,
9679 rel->r_offset & ~3, 8))
9680 goto got_error_ret;
9681 insn = bfd_get_32 (ibfd, buf);
9682 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9683 continue;
9684 insn = bfd_get_32 (ibfd, buf + 4);
9685 if ((insn & (0x3fu << 26)) != 57u << 26)
9686 continue;
9687 break;
9689 #undef LIMIT_ADJUST
9691 if (h != NULL)
9692 ent = h->got.glist;
9693 else
9695 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9696 ent = local_got_ents[r_symndx];
9698 for (; ent != NULL; ent = ent->next)
9699 if (ent->addend == rel->r_addend
9700 && ent->owner == ibfd
9701 && ent->tls_type == 0)
9702 break;
9703 BFD_ASSERT (ent && ent->got.refcount > 0);
9704 ent->got.refcount -= 1;
9707 if (elf_section_data (sec)->relocs != relstart)
9708 free (relstart);
9711 if (local_syms != NULL
9712 && symtab_hdr->contents != (unsigned char *) local_syms)
9714 if (!info->keep_memory)
9715 free (local_syms);
9716 else
9717 symtab_hdr->contents = (unsigned char *) local_syms;
9721 return true;
9724 /* Return true iff input section I references the TOC using
9725 instructions limited to +/-32k offsets. */
9727 bool
9728 ppc64_elf_has_small_toc_reloc (asection *i)
9730 return (is_ppc64_elf (i->owner)
9731 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9734 /* Allocate space for one GOT entry. */
9736 static void
9737 allocate_got (struct elf_link_hash_entry *h,
9738 struct bfd_link_info *info,
9739 struct got_entry *gent)
9741 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9742 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9743 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9744 ? 16 : 8);
9745 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9746 ? 2 : 1) * sizeof (Elf64_External_Rela);
9747 asection *got = ppc64_elf_tdata (gent->owner)->got;
9749 gent->got.offset = got->size;
9750 got->size += entsize;
9752 if (h->type == STT_GNU_IFUNC)
9754 htab->elf.irelplt->size += rentsize;
9755 htab->got_reli_size += rentsize;
9757 else if (((bfd_link_pic (info)
9758 && (gent->tls_type == 0
9759 ? !info->enable_dt_relr
9760 : !(bfd_link_executable (info)
9761 && SYMBOL_REFERENCES_LOCAL (info, h)))
9762 && !bfd_is_abs_symbol (&h->root))
9763 || (htab->elf.dynamic_sections_created
9764 && h->dynindx != -1
9765 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9766 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9768 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9769 relgot->size += rentsize;
9773 /* This function merges got entries in the same toc group. */
9775 static void
9776 merge_got_entries (struct got_entry **pent)
9778 struct got_entry *ent, *ent2;
9780 for (ent = *pent; ent != NULL; ent = ent->next)
9781 if (!ent->is_indirect)
9782 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9783 if (!ent2->is_indirect
9784 && ent2->addend == ent->addend
9785 && ent2->tls_type == ent->tls_type
9786 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9788 ent2->is_indirect = true;
9789 ent2->got.ent = ent;
9793 /* If H is undefined, make it dynamic if that makes sense. */
9795 static bool
9796 ensure_undef_dynamic (struct bfd_link_info *info,
9797 struct elf_link_hash_entry *h)
9799 struct elf_link_hash_table *htab = elf_hash_table (info);
9801 if (htab->dynamic_sections_created
9802 && ((info->dynamic_undefined_weak != 0
9803 && h->root.type == bfd_link_hash_undefweak)
9804 || h->root.type == bfd_link_hash_undefined)
9805 && h->dynindx == -1
9806 && !h->forced_local
9807 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9808 return bfd_elf_link_record_dynamic_symbol (info, h);
9809 return true;
9812 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9813 usual htab->elf.splt section for a PLT entry. */
9815 static inline
9816 bool use_local_plt (struct bfd_link_info *info,
9817 struct elf_link_hash_entry *h)
9819 return (h == NULL
9820 || h->dynindx == -1
9821 || !elf_hash_table (info)->dynamic_sections_created);
9824 /* Allocate space in .plt, .got and associated reloc sections for
9825 dynamic relocs. */
9827 static bool
9828 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9830 struct bfd_link_info *info;
9831 struct ppc_link_hash_table *htab;
9832 asection *s;
9833 struct ppc_link_hash_entry *eh;
9834 struct got_entry **pgent, *gent;
9836 if (h->root.type == bfd_link_hash_indirect)
9837 return true;
9839 info = (struct bfd_link_info *) inf;
9840 htab = ppc_hash_table (info);
9841 if (htab == NULL)
9842 return false;
9844 eh = ppc_elf_hash_entry (h);
9845 /* Run through the TLS GD got entries first if we're changing them
9846 to TPREL. */
9847 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9848 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9849 if (gent->got.refcount > 0
9850 && (gent->tls_type & TLS_GD) != 0)
9852 /* This was a GD entry that has been converted to TPREL. If
9853 there happens to be a TPREL entry we can use that one. */
9854 struct got_entry *ent;
9855 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9856 if (ent->got.refcount > 0
9857 && (ent->tls_type & TLS_TPREL) != 0
9858 && ent->addend == gent->addend
9859 && ent->owner == gent->owner)
9861 gent->got.refcount = 0;
9862 break;
9865 /* If not, then we'll be using our own TPREL entry. */
9866 if (gent->got.refcount != 0)
9867 gent->tls_type = TLS_TLS | TLS_TPREL;
9870 /* Remove any list entry that won't generate a word in the GOT before
9871 we call merge_got_entries. Otherwise we risk merging to empty
9872 entries. */
9873 pgent = &h->got.glist;
9874 while ((gent = *pgent) != NULL)
9875 if (gent->got.refcount > 0)
9877 if ((gent->tls_type & TLS_LD) != 0
9878 && SYMBOL_REFERENCES_LOCAL (info, h))
9880 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9881 *pgent = gent->next;
9883 else
9884 pgent = &gent->next;
9886 else
9887 *pgent = gent->next;
9889 if (!htab->do_multi_toc)
9890 merge_got_entries (&h->got.glist);
9892 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9893 if (!gent->is_indirect)
9895 /* Ensure we catch all the cases where this symbol should
9896 be made dynamic. */
9897 if (!ensure_undef_dynamic (info, h))
9898 return false;
9900 if (!is_ppc64_elf (gent->owner))
9901 abort ();
9903 allocate_got (h, info, gent);
9906 /* If no dynamic sections we can't have dynamic relocs, except for
9907 IFUNCs which are handled even in static executables. */
9908 if (!htab->elf.dynamic_sections_created
9909 && h->type != STT_GNU_IFUNC)
9910 h->dyn_relocs = NULL;
9912 /* Discard relocs on undefined symbols that must be local. */
9913 else if (h->root.type == bfd_link_hash_undefined
9914 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9915 h->dyn_relocs = NULL;
9917 /* Also discard relocs on undefined weak syms with non-default
9918 visibility, or when dynamic_undefined_weak says so. */
9919 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9920 h->dyn_relocs = NULL;
9922 if (h->dyn_relocs != NULL)
9924 struct ppc_dyn_relocs *p, **pp;
9926 /* In the shared -Bsymbolic case, discard space allocated for
9927 dynamic pc-relative relocs against symbols which turn out to
9928 be defined in regular objects. For the normal shared case,
9929 discard space for relocs that have become local due to symbol
9930 visibility changes. */
9931 if (bfd_link_pic (info))
9933 /* Relocs that use pc_count are those that appear on a call
9934 insn, or certain REL relocs (see must_be_dyn_reloc) that
9935 can be generated via assembly. We want calls to
9936 protected symbols to resolve directly to the function
9937 rather than going via the plt. If people want function
9938 pointer comparisons to work as expected then they should
9939 avoid writing weird assembly. */
9940 if (SYMBOL_CALLS_LOCAL (info, h))
9942 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9943 (p = *pp) != NULL;
9946 p->count -= p->pc_count;
9947 p->pc_count = 0;
9948 if (p->count == 0)
9949 *pp = p->next;
9950 else
9951 pp = &p->next;
9955 if (h->dyn_relocs != NULL)
9957 /* Ensure we catch all the cases where this symbol
9958 should be made dynamic. */
9959 if (!ensure_undef_dynamic (info, h))
9960 return false;
9964 /* For a fixed position executable, discard space for
9965 relocs against symbols which are not dynamic. */
9966 else if (h->type != STT_GNU_IFUNC)
9968 if ((h->dynamic_adjusted
9969 || (h->ref_regular
9970 && h->root.type == bfd_link_hash_undefweak
9971 && (info->dynamic_undefined_weak > 0
9972 || !_bfd_elf_readonly_dynrelocs (h))))
9973 && !h->def_regular
9974 && !ELF_COMMON_DEF_P (h))
9976 /* Ensure we catch all the cases where this symbol
9977 should be made dynamic. */
9978 if (!ensure_undef_dynamic (info, h))
9979 return false;
9981 /* But if that didn't work out, discard dynamic relocs. */
9982 if (h->dynindx == -1)
9983 h->dyn_relocs = NULL;
9985 else
9986 h->dyn_relocs = NULL;
9989 /* Finally, allocate space. */
9990 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
9992 unsigned int count;
9993 asection *sreloc = elf_section_data (p->sec)->sreloc;
9994 if (eh->elf.type == STT_GNU_IFUNC)
9995 sreloc = htab->elf.irelplt;
9996 count = p->count;
9997 if (info->enable_dt_relr
9998 && ((!NO_OPD_RELOCS
9999 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10000 || (eh->elf.type != STT_GNU_IFUNC
10001 && SYMBOL_REFERENCES_LOCAL (info, h))))
10002 count -= p->rel_count;
10003 sreloc->size += count * sizeof (Elf64_External_Rela);
10007 /* We might need a PLT entry when the symbol
10008 a) is dynamic, or
10009 b) is an ifunc, or
10010 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10011 d) has plt16 relocs and we are linking statically. */
10012 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10013 || h->type == STT_GNU_IFUNC
10014 || (h->needs_plt && h->dynamic_adjusted)
10015 || (h->needs_plt
10016 && h->def_regular
10017 && !htab->elf.dynamic_sections_created
10018 && !htab->can_convert_all_inline_plt
10019 && (ppc_elf_hash_entry (h)->tls_mask
10020 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10022 struct plt_entry *pent;
10023 bool doneone = false;
10024 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10025 if (pent->plt.refcount > 0)
10027 if (!ensure_undef_dynamic (info, h))
10028 return false;
10030 if (use_local_plt (info, h))
10032 if (h->type == STT_GNU_IFUNC)
10034 s = htab->elf.iplt;
10035 pent->plt.offset = s->size;
10036 s->size += PLT_ENTRY_SIZE (htab);
10037 s = htab->elf.irelplt;
10039 else
10041 s = htab->pltlocal;
10042 pent->plt.offset = s->size;
10043 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10044 s = NULL;
10045 if (bfd_link_pic (info)
10046 && !(info->enable_dt_relr && !htab->opd_abi))
10047 s = htab->relpltlocal;
10050 else
10052 /* If this is the first .plt entry, make room for the special
10053 first entry. */
10054 s = htab->elf.splt;
10055 if (s->size == 0)
10056 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10058 pent->plt.offset = s->size;
10060 /* Make room for this entry. */
10061 s->size += PLT_ENTRY_SIZE (htab);
10063 /* Make room for the .glink code. */
10064 s = htab->glink;
10065 if (s->size == 0)
10066 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10067 if (htab->opd_abi)
10069 /* We need bigger stubs past index 32767. */
10070 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10071 s->size += 4;
10072 s->size += 2*4;
10074 else
10075 s->size += 4;
10077 /* We also need to make an entry in the .rela.plt section. */
10078 s = htab->elf.srelplt;
10080 if (s != NULL)
10081 s->size += sizeof (Elf64_External_Rela);
10082 doneone = true;
10084 else
10085 pent->plt.offset = (bfd_vma) -1;
10086 if (!doneone)
10088 h->plt.plist = NULL;
10089 h->needs_plt = 0;
10092 else
10094 h->plt.plist = NULL;
10095 h->needs_plt = 0;
10098 return true;
10101 #define PPC_LO(v) ((v) & 0xffff)
10102 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10103 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10104 #define D34(v) \
10105 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10106 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10108 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10109 to set up space for global entry stubs. These are put in glink,
10110 after the branch table. */
10112 static bool
10113 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10115 struct bfd_link_info *info;
10116 struct ppc_link_hash_table *htab;
10117 struct plt_entry *pent;
10118 asection *s, *plt;
10120 if (h->root.type == bfd_link_hash_indirect)
10121 return true;
10123 if (!h->pointer_equality_needed)
10124 return true;
10126 if (h->def_regular)
10127 return true;
10129 info = inf;
10130 htab = ppc_hash_table (info);
10131 if (htab == NULL)
10132 return false;
10134 s = htab->global_entry;
10135 plt = htab->elf.splt;
10136 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10137 if (pent->plt.offset != (bfd_vma) -1
10138 && pent->addend == 0)
10140 /* For ELFv2, if this symbol is not defined in a regular file
10141 and we are not generating a shared library or pie, then we
10142 need to define the symbol in the executable on a call stub.
10143 This is to avoid text relocations. */
10144 bfd_vma off, stub_align, stub_off, stub_size;
10145 unsigned int align_power;
10147 stub_size = 16;
10148 stub_off = s->size;
10149 if (htab->params->plt_stub_align >= 0)
10150 align_power = htab->params->plt_stub_align;
10151 else
10152 align_power = -htab->params->plt_stub_align;
10153 /* Setting section alignment is delayed until we know it is
10154 non-empty. Otherwise the .text output section will be
10155 aligned at least to plt_stub_align even when no global
10156 entry stubs are needed. */
10157 if (s->alignment_power < align_power)
10158 s->alignment_power = align_power;
10159 stub_align = (bfd_vma) 1 << align_power;
10160 if (htab->params->plt_stub_align >= 0
10161 || ((((stub_off + stub_size - 1) & -stub_align)
10162 - (stub_off & -stub_align))
10163 > ((stub_size - 1) & -stub_align)))
10164 stub_off = (stub_off + stub_align - 1) & -stub_align;
10165 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10166 off -= stub_off + s->output_offset + s->output_section->vma;
10167 /* Note that for --plt-stub-align negative we have a possible
10168 dependency between stub offset and size. Break that
10169 dependency by assuming the max stub size when calculating
10170 the stub offset. */
10171 if (PPC_HA (off) == 0)
10172 stub_size -= 4;
10173 h->root.type = bfd_link_hash_defined;
10174 h->root.u.def.section = s;
10175 h->root.u.def.value = stub_off;
10176 s->size = stub_off + stub_size;
10177 break;
10179 return true;
10182 /* Set the sizes of the dynamic sections. */
10184 static bool
10185 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10186 struct bfd_link_info *info)
10188 struct ppc_link_hash_table *htab;
10189 bfd *dynobj;
10190 asection *s;
10191 bool relocs;
10192 bfd *ibfd;
10193 struct got_entry *first_tlsld;
10195 htab = ppc_hash_table (info);
10196 if (htab == NULL)
10197 return false;
10199 dynobj = htab->elf.dynobj;
10200 if (dynobj == NULL)
10201 abort ();
10203 if (htab->elf.dynamic_sections_created)
10205 /* Set the contents of the .interp section to the interpreter. */
10206 if (bfd_link_executable (info) && !info->nointerp)
10208 s = bfd_get_linker_section (dynobj, ".interp");
10209 if (s == NULL)
10210 abort ();
10211 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10212 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10216 /* Set up .got offsets for local syms, and space for local dynamic
10217 relocs. */
10218 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10220 struct got_entry **lgot_ents;
10221 struct got_entry **end_lgot_ents;
10222 struct plt_entry **local_plt;
10223 struct plt_entry **end_local_plt;
10224 unsigned char *lgot_masks;
10225 bfd_size_type locsymcount;
10226 Elf_Internal_Shdr *symtab_hdr;
10227 Elf_Internal_Sym *local_syms;
10228 Elf_Internal_Sym *isym;
10230 if (!is_ppc64_elf (ibfd))
10231 continue;
10233 for (s = ibfd->sections; s != NULL; s = s->next)
10235 struct ppc_local_dyn_relocs *p;
10237 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10239 if (!bfd_is_abs_section (p->sec)
10240 && bfd_is_abs_section (p->sec->output_section))
10242 /* Input section has been discarded, either because
10243 it is a copy of a linkonce section or due to
10244 linker script /DISCARD/, so we'll be discarding
10245 the relocs too. */
10247 else if (p->count != 0)
10249 unsigned int count;
10250 asection *srel;
10252 count = p->count;
10253 if (info->enable_dt_relr
10254 && ((!NO_OPD_RELOCS
10255 && (ppc64_elf_section_data (p->sec)->sec_type
10256 == sec_opd))
10257 || !p->ifunc))
10258 count -= p->rel_count;
10259 srel = elf_section_data (p->sec)->sreloc;
10260 if (p->ifunc)
10261 srel = htab->elf.irelplt;
10262 srel->size += count * sizeof (Elf64_External_Rela);
10263 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10264 info->flags |= DF_TEXTREL;
10269 lgot_ents = elf_local_got_ents (ibfd);
10270 if (!lgot_ents)
10271 continue;
10273 symtab_hdr = &elf_symtab_hdr (ibfd);
10274 locsymcount = symtab_hdr->sh_info;
10275 end_lgot_ents = lgot_ents + locsymcount;
10276 local_plt = (struct plt_entry **) end_lgot_ents;
10277 end_local_plt = local_plt + locsymcount;
10278 lgot_masks = (unsigned char *) end_local_plt;
10279 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10280 if (local_syms == NULL && locsymcount != 0)
10282 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10283 0, NULL, NULL, NULL);
10284 if (local_syms == NULL)
10285 return false;
10287 s = ppc64_elf_tdata (ibfd)->got;
10288 for (isym = local_syms;
10289 lgot_ents < end_lgot_ents;
10290 ++lgot_ents, ++lgot_masks, isym++)
10292 struct got_entry **pent, *ent;
10294 pent = lgot_ents;
10295 while ((ent = *pent) != NULL)
10296 if (ent->got.refcount > 0)
10298 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10300 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10301 *pent = ent->next;
10303 else
10305 unsigned int ent_size = 8;
10306 unsigned int rel_size = sizeof (Elf64_External_Rela);
10308 ent->got.offset = s->size;
10309 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10311 ent_size *= 2;
10312 rel_size *= 2;
10314 s->size += ent_size;
10315 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10317 htab->elf.irelplt->size += rel_size;
10318 htab->got_reli_size += rel_size;
10320 else if (bfd_link_pic (info)
10321 && (ent->tls_type == 0
10322 ? !info->enable_dt_relr
10323 : !bfd_link_executable (info))
10324 && isym->st_shndx != SHN_ABS)
10326 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10327 srel->size += rel_size;
10329 pent = &ent->next;
10332 else
10333 *pent = ent->next;
10335 if (local_syms != NULL
10336 && symtab_hdr->contents != (unsigned char *) local_syms)
10338 if (!info->keep_memory)
10339 free (local_syms);
10340 else
10341 symtab_hdr->contents = (unsigned char *) local_syms;
10344 /* Allocate space for plt calls to local syms. */
10345 lgot_masks = (unsigned char *) end_local_plt;
10346 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10348 struct plt_entry *ent;
10350 for (ent = *local_plt; ent != NULL; ent = ent->next)
10351 if (ent->plt.refcount > 0)
10353 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10355 s = htab->elf.iplt;
10356 ent->plt.offset = s->size;
10357 s->size += PLT_ENTRY_SIZE (htab);
10358 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10360 else if (htab->can_convert_all_inline_plt
10361 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10362 ent->plt.offset = (bfd_vma) -1;
10363 else
10365 s = htab->pltlocal;
10366 ent->plt.offset = s->size;
10367 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10368 if (bfd_link_pic (info)
10369 && !(info->enable_dt_relr && !htab->opd_abi))
10370 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10373 else
10374 ent->plt.offset = (bfd_vma) -1;
10378 /* Allocate global sym .plt and .got entries, and space for global
10379 sym dynamic relocs. */
10380 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10382 if (!htab->opd_abi && !bfd_link_pic (info))
10383 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10385 first_tlsld = NULL;
10386 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10388 struct got_entry *ent;
10390 if (!is_ppc64_elf (ibfd))
10391 continue;
10393 ent = ppc64_tlsld_got (ibfd);
10394 if (ent->got.refcount > 0)
10396 if (!htab->do_multi_toc && first_tlsld != NULL)
10398 ent->is_indirect = true;
10399 ent->got.ent = first_tlsld;
10401 else
10403 if (first_tlsld == NULL)
10404 first_tlsld = ent;
10405 s = ppc64_elf_tdata (ibfd)->got;
10406 ent->got.offset = s->size;
10407 ent->owner = ibfd;
10408 s->size += 16;
10409 if (bfd_link_dll (info))
10411 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10412 srel->size += sizeof (Elf64_External_Rela);
10416 else
10417 ent->got.offset = (bfd_vma) -1;
10420 /* We now have determined the sizes of the various dynamic sections.
10421 Allocate memory for them. */
10422 relocs = false;
10423 for (s = dynobj->sections; s != NULL; s = s->next)
10425 if ((s->flags & SEC_LINKER_CREATED) == 0)
10426 continue;
10428 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10429 /* These haven't been allocated yet; don't strip. */
10430 continue;
10431 else if (s == htab->elf.sgot
10432 || s == htab->elf.splt
10433 || s == htab->elf.iplt
10434 || s == htab->pltlocal
10435 || s == htab->glink
10436 || s == htab->global_entry
10437 || s == htab->elf.sdynbss
10438 || s == htab->elf.sdynrelro)
10440 /* Strip this section if we don't need it; see the
10441 comment below. */
10443 else if (s == htab->glink_eh_frame)
10445 if (!bfd_is_abs_section (s->output_section))
10446 /* Not sized yet. */
10447 continue;
10449 else if (startswith (s->name, ".rela"))
10451 if (s->size != 0)
10453 if (s != htab->elf.srelplt)
10454 relocs = true;
10456 /* We use the reloc_count field as a counter if we need
10457 to copy relocs into the output file. */
10458 s->reloc_count = 0;
10461 else
10463 /* It's not one of our sections, so don't allocate space. */
10464 continue;
10467 if (s->size == 0)
10469 /* If we don't need this section, strip it from the
10470 output file. This is mostly to handle .rela.bss and
10471 .rela.plt. We must create both sections in
10472 create_dynamic_sections, because they must be created
10473 before the linker maps input sections to output
10474 sections. The linker does that before
10475 adjust_dynamic_symbol is called, and it is that
10476 function which decides whether anything needs to go
10477 into these sections. */
10478 s->flags |= SEC_EXCLUDE;
10479 continue;
10482 if (bfd_is_abs_section (s->output_section))
10483 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10484 s->name);
10486 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10487 continue;
10489 /* Allocate memory for the section contents. We use bfd_zalloc
10490 here in case unused entries are not reclaimed before the
10491 section's contents are written out. This should not happen,
10492 but this way if it does we get a R_PPC64_NONE reloc in .rela
10493 sections instead of garbage.
10494 We also rely on the section contents being zero when writing
10495 the GOT and .dynrelro. */
10496 s->contents = bfd_zalloc (dynobj, s->size);
10497 if (s->contents == NULL)
10498 return false;
10501 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10503 if (!is_ppc64_elf (ibfd))
10504 continue;
10506 s = ppc64_elf_tdata (ibfd)->got;
10507 if (s != NULL && s != htab->elf.sgot)
10509 if (s->size == 0)
10510 s->flags |= SEC_EXCLUDE;
10511 else
10513 s->contents = bfd_zalloc (ibfd, s->size);
10514 if (s->contents == NULL)
10515 return false;
10518 s = ppc64_elf_tdata (ibfd)->relgot;
10519 if (s != NULL)
10521 if (s->size == 0)
10522 s->flags |= SEC_EXCLUDE;
10523 else
10525 s->contents = bfd_zalloc (ibfd, s->size);
10526 if (s->contents == NULL)
10527 return false;
10528 relocs = true;
10529 s->reloc_count = 0;
10534 if (htab->elf.dynamic_sections_created)
10536 bool tls_opt;
10538 /* Add some entries to the .dynamic section. We fill in the
10539 values later, in ppc64_elf_finish_dynamic_sections, but we
10540 must add the entries now so that we get the correct size for
10541 the .dynamic section. The DT_DEBUG entry is filled in by the
10542 dynamic linker and used by the debugger. */
10543 #define add_dynamic_entry(TAG, VAL) \
10544 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10546 if (bfd_link_executable (info))
10548 if (!add_dynamic_entry (DT_DEBUG, 0))
10549 return false;
10552 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10554 if (!add_dynamic_entry (DT_PLTGOT, 0)
10555 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10556 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10557 || !add_dynamic_entry (DT_JMPREL, 0)
10558 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10559 return false;
10562 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10564 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10565 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10566 return false;
10569 tls_opt = (htab->params->tls_get_addr_opt
10570 && ((htab->tls_get_addr_fd != NULL
10571 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10572 || (htab->tga_desc_fd != NULL
10573 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10574 if (tls_opt || !htab->opd_abi)
10576 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10577 return false;
10580 if (relocs)
10582 if (!add_dynamic_entry (DT_RELA, 0)
10583 || !add_dynamic_entry (DT_RELASZ, 0)
10584 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10585 return false;
10587 /* If any dynamic relocs apply to a read-only section,
10588 then we need a DT_TEXTREL entry. */
10589 if ((info->flags & DF_TEXTREL) == 0)
10590 elf_link_hash_traverse (&htab->elf,
10591 _bfd_elf_maybe_set_textrel, info);
10593 if ((info->flags & DF_TEXTREL) != 0)
10595 if (!add_dynamic_entry (DT_TEXTREL, 0))
10596 return false;
10600 #undef add_dynamic_entry
10602 return true;
10605 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10607 static bool
10608 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10610 if (h->plt.plist != NULL
10611 && !h->def_regular
10612 && !h->pointer_equality_needed)
10613 return false;
10615 return _bfd_elf_hash_symbol (h);
10618 /* Determine the type of stub needed, if any, for a call. */
10620 static inline enum ppc_stub_main_type
10621 ppc_type_of_stub (asection *input_sec,
10622 const Elf_Internal_Rela *rel,
10623 struct ppc_link_hash_entry **hash,
10624 struct plt_entry **plt_ent,
10625 bfd_vma destination,
10626 unsigned long local_off)
10628 struct ppc_link_hash_entry *h = *hash;
10629 bfd_vma location;
10630 bfd_vma branch_offset;
10631 bfd_vma max_branch_offset;
10632 enum elf_ppc64_reloc_type r_type;
10634 if (h != NULL)
10636 struct plt_entry *ent;
10637 struct ppc_link_hash_entry *fdh = h;
10638 if (h->oh != NULL
10639 && h->oh->is_func_descriptor)
10641 fdh = ppc_follow_link (h->oh);
10642 *hash = fdh;
10645 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10646 if (ent->addend == rel->r_addend
10647 && ent->plt.offset != (bfd_vma) -1)
10649 *plt_ent = ent;
10650 return ppc_stub_plt_call;
10653 /* Here, we know we don't have a plt entry. If we don't have a
10654 either a defined function descriptor or a defined entry symbol
10655 in a regular object file, then it is pointless trying to make
10656 any other type of stub. */
10657 if (!is_static_defined (&fdh->elf)
10658 && !is_static_defined (&h->elf))
10659 return ppc_stub_none;
10661 else if (elf_local_got_ents (input_sec->owner) != NULL)
10663 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10664 struct plt_entry **local_plt = (struct plt_entry **)
10665 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10666 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10668 if (local_plt[r_symndx] != NULL)
10670 struct plt_entry *ent;
10672 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10673 if (ent->addend == rel->r_addend
10674 && ent->plt.offset != (bfd_vma) -1)
10676 *plt_ent = ent;
10677 return ppc_stub_plt_call;
10682 /* Determine where the call point is. */
10683 location = (input_sec->output_offset
10684 + input_sec->output_section->vma
10685 + rel->r_offset);
10687 branch_offset = destination - location;
10688 r_type = ELF64_R_TYPE (rel->r_info);
10690 /* Determine if a long branch stub is needed. */
10691 max_branch_offset = 1 << 25;
10692 if (r_type == R_PPC64_REL14
10693 || r_type == R_PPC64_REL14_BRTAKEN
10694 || r_type == R_PPC64_REL14_BRNTAKEN)
10695 max_branch_offset = 1 << 15;
10697 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10698 /* We need a stub. Figure out whether a long_branch or plt_branch
10699 is needed later. */
10700 return ppc_stub_long_branch;
10702 return ppc_stub_none;
10705 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10706 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10707 . mflr %r12
10708 . bcl 20,31,1f
10709 .1: mflr %r11
10710 . mtlr %r12
10711 . lis %r12,xxx-1b@highest
10712 . ori %r12,%r12,xxx-1b@higher
10713 . sldi %r12,%r12,32
10714 . oris %r12,%r12,xxx-1b@high
10715 . ori %r12,%r12,xxx-1b@l
10716 . add/ldx %r12,%r11,%r12 */
10718 static bfd_byte *
10719 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10721 bfd_put_32 (abfd, MFLR_R12, p);
10722 p += 4;
10723 bfd_put_32 (abfd, BCL_20_31, p);
10724 p += 4;
10725 bfd_put_32 (abfd, MFLR_R11, p);
10726 p += 4;
10727 bfd_put_32 (abfd, MTLR_R12, p);
10728 p += 4;
10729 if (off + 0x8000 < 0x10000)
10731 if (load)
10732 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10733 else
10734 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10735 p += 4;
10737 else if (off + 0x80008000ULL < 0x100000000ULL)
10739 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10740 p += 4;
10741 if (load)
10742 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10743 else
10744 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10745 p += 4;
10747 else
10749 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10751 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10752 p += 4;
10754 else
10756 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10757 p += 4;
10758 if (((off >> 32) & 0xffff) != 0)
10760 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10761 p += 4;
10764 if (((off >> 32) & 0xffffffffULL) != 0)
10766 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10767 p += 4;
10769 if (PPC_HI (off) != 0)
10771 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10772 p += 4;
10774 if (PPC_LO (off) != 0)
10776 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10777 p += 4;
10779 if (load)
10780 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10781 else
10782 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10783 p += 4;
10785 return p;
10788 static unsigned int
10789 size_offset (bfd_vma off)
10791 unsigned int size;
10792 if (off + 0x8000 < 0x10000)
10793 size = 4;
10794 else if (off + 0x80008000ULL < 0x100000000ULL)
10795 size = 8;
10796 else
10798 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10799 size = 4;
10800 else
10802 size = 4;
10803 if (((off >> 32) & 0xffff) != 0)
10804 size += 4;
10806 if (((off >> 32) & 0xffffffffULL) != 0)
10807 size += 4;
10808 if (PPC_HI (off) != 0)
10809 size += 4;
10810 if (PPC_LO (off) != 0)
10811 size += 4;
10812 size += 4;
10814 return size + 16;
10817 static unsigned int
10818 num_relocs_for_offset (bfd_vma off)
10820 unsigned int num_rel;
10821 if (off + 0x8000 < 0x10000)
10822 num_rel = 1;
10823 else if (off + 0x80008000ULL < 0x100000000ULL)
10824 num_rel = 2;
10825 else
10827 num_rel = 1;
10828 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10829 && ((off >> 32) & 0xffff) != 0)
10830 num_rel += 1;
10831 if (PPC_HI (off) != 0)
10832 num_rel += 1;
10833 if (PPC_LO (off) != 0)
10834 num_rel += 1;
10836 return num_rel;
10839 static Elf_Internal_Rela *
10840 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10841 bfd_vma roff, bfd_vma targ, bfd_vma off)
10843 bfd_vma relative_targ = targ - (roff - 8);
10844 if (bfd_big_endian (info->output_bfd))
10845 roff += 2;
10846 r->r_offset = roff;
10847 r->r_addend = relative_targ + roff;
10848 if (off + 0x8000 < 0x10000)
10849 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10850 else if (off + 0x80008000ULL < 0x100000000ULL)
10852 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10853 ++r;
10854 roff += 4;
10855 r->r_offset = roff;
10856 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10857 r->r_addend = relative_targ + roff;
10859 else
10861 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10862 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10863 else
10865 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10866 if (((off >> 32) & 0xffff) != 0)
10868 ++r;
10869 roff += 4;
10870 r->r_offset = roff;
10871 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10872 r->r_addend = relative_targ + roff;
10875 if (((off >> 32) & 0xffffffffULL) != 0)
10876 roff += 4;
10877 if (PPC_HI (off) != 0)
10879 ++r;
10880 roff += 4;
10881 r->r_offset = roff;
10882 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10883 r->r_addend = relative_targ + roff;
10885 if (PPC_LO (off) != 0)
10887 ++r;
10888 roff += 4;
10889 r->r_offset = roff;
10890 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10891 r->r_addend = relative_targ + roff;
10894 return r;
10897 static bfd_byte *
10898 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10899 bool load)
10901 uint64_t insn;
10902 if (off - odd + (1ULL << 33) < 1ULL << 34)
10904 off -= odd;
10905 if (odd)
10907 bfd_put_32 (abfd, NOP, p);
10908 p += 4;
10910 if (load)
10911 insn = PLD_R12_PC;
10912 else
10913 insn = PADDI_R12_PC;
10914 insn |= D34 (off);
10915 bfd_put_32 (abfd, insn >> 32, p);
10916 p += 4;
10917 bfd_put_32 (abfd, insn, p);
10919 /* The minimum value for paddi is -0x200000000. The minimum value
10920 for li is -0x8000, which when shifted by 34 and added gives a
10921 minimum value of -0x2000200000000. The maximum value is
10922 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10923 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10925 off -= 8 - odd;
10926 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10927 p += 4;
10928 if (!odd)
10930 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10931 p += 4;
10933 insn = PADDI_R12_PC | D34 (off);
10934 bfd_put_32 (abfd, insn >> 32, p);
10935 p += 4;
10936 bfd_put_32 (abfd, insn, p);
10937 p += 4;
10938 if (odd)
10940 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10941 p += 4;
10943 if (load)
10944 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10945 else
10946 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10948 else
10950 off -= odd + 8;
10951 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10952 p += 4;
10953 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10954 p += 4;
10955 if (odd)
10957 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10958 p += 4;
10960 insn = PADDI_R12_PC | D34 (off);
10961 bfd_put_32 (abfd, insn >> 32, p);
10962 p += 4;
10963 bfd_put_32 (abfd, insn, p);
10964 p += 4;
10965 if (!odd)
10967 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10968 p += 4;
10970 if (load)
10971 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10972 else
10973 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10975 p += 4;
10976 return p;
10979 static unsigned int
10980 size_power10_offset (bfd_vma off, int odd)
10982 if (off - odd + (1ULL << 33) < 1ULL << 34)
10983 return odd + 8;
10984 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10985 return 20;
10986 else
10987 return 24;
10990 static unsigned int
10991 num_relocs_for_power10_offset (bfd_vma off, int odd)
10993 if (off - odd + (1ULL << 33) < 1ULL << 34)
10994 return 1;
10995 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10996 return 2;
10997 else
10998 return 3;
11001 static Elf_Internal_Rela *
11002 emit_relocs_for_power10_offset (struct bfd_link_info *info,
11003 Elf_Internal_Rela *r, bfd_vma roff,
11004 bfd_vma targ, bfd_vma off, int odd)
11006 if (off - odd + (1ULL << 33) < 1ULL << 34)
11007 roff += odd;
11008 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11010 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11011 r->r_offset = roff + d_offset;
11012 r->r_addend = targ + 8 - odd - d_offset;
11013 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11014 ++r;
11015 roff += 8 - odd;
11017 else
11019 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11020 r->r_offset = roff + d_offset;
11021 r->r_addend = targ + 8 + odd - d_offset;
11022 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11023 ++r;
11024 roff += 4;
11025 r->r_offset = roff + d_offset;
11026 r->r_addend = targ + 4 + odd - d_offset;
11027 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11028 ++r;
11029 roff += 4 + odd;
11031 r->r_offset = roff;
11032 r->r_addend = targ;
11033 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11034 return r;
11037 /* Emit .eh_frame opcode to advance pc by DELTA. */
11039 static bfd_byte *
11040 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11042 delta /= 4;
11043 if (delta < 64)
11044 *eh++ = DW_CFA_advance_loc + delta;
11045 else if (delta < 256)
11047 *eh++ = DW_CFA_advance_loc1;
11048 *eh++ = delta;
11050 else if (delta < 65536)
11052 *eh++ = DW_CFA_advance_loc2;
11053 bfd_put_16 (abfd, delta, eh);
11054 eh += 2;
11056 else
11058 *eh++ = DW_CFA_advance_loc4;
11059 bfd_put_32 (abfd, delta, eh);
11060 eh += 4;
11062 return eh;
11065 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11067 static unsigned int
11068 eh_advance_size (unsigned int delta)
11070 if (delta < 64 * 4)
11071 /* DW_CFA_advance_loc+[1..63]. */
11072 return 1;
11073 if (delta < 256 * 4)
11074 /* DW_CFA_advance_loc1, byte. */
11075 return 2;
11076 if (delta < 65536 * 4)
11077 /* DW_CFA_advance_loc2, 2 bytes. */
11078 return 3;
11079 /* DW_CFA_advance_loc4, 4 bytes. */
11080 return 5;
11083 /* With power7 weakly ordered memory model, it is possible for ld.so
11084 to update a plt entry in one thread and have another thread see a
11085 stale zero toc entry. To avoid this we need some sort of acquire
11086 barrier in the call stub. One solution is to make the load of the
11087 toc word seem to appear to depend on the load of the function entry
11088 word. Another solution is to test for r2 being zero, and branch to
11089 the appropriate glink entry if so.
11091 . fake dep barrier compare
11092 . ld 12,xxx(2) ld 12,xxx(2)
11093 . mtctr 12 mtctr 12
11094 . xor 11,12,12 ld 2,xxx+8(2)
11095 . add 2,2,11 cmpldi 2,0
11096 . ld 2,xxx+8(2) bnectr+
11097 . bctr b <glink_entry>
11099 The solution involving the compare turns out to be faster, so
11100 that's what we use unless the branch won't reach. */
11102 #define ALWAYS_USE_FAKE_DEP 0
11103 #define ALWAYS_EMIT_R2SAVE 0
11105 static inline unsigned int
11106 plt_stub_size (struct ppc_link_hash_table *htab,
11107 struct ppc_stub_hash_entry *stub_entry,
11108 bfd_vma off,
11109 unsigned int odd)
11111 unsigned size;
11113 if (stub_entry->type.sub == ppc_stub_notoc)
11115 size = 8 + size_power10_offset (off, odd);
11116 if (stub_entry->type.r2save)
11117 size += 4;
11119 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11121 size = 8 + size_offset (off - 8);
11122 if (stub_entry->type.r2save)
11123 size += 4;
11125 else
11127 size = 12;
11128 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11129 size += 4;
11130 if (PPC_HA (off) != 0)
11131 size += 4;
11132 if (htab->opd_abi)
11134 size += 4;
11135 if (htab->params->plt_static_chain)
11136 size += 4;
11137 if (htab->params->plt_thread_safe
11138 && htab->elf.dynamic_sections_created
11139 && stub_entry->h != NULL
11140 && stub_entry->h->elf.dynindx != -1)
11141 size += 8;
11142 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11143 != PPC_HA (off))
11144 size += 4;
11147 if (stub_entry->h != NULL
11148 && is_tls_get_addr (&stub_entry->h->elf, htab)
11149 && htab->params->tls_get_addr_opt)
11151 if (!htab->params->no_tls_get_addr_regsave)
11153 size += 30 * 4;
11154 if (stub_entry->type.r2save)
11155 size += 4;
11157 else
11159 size += 7 * 4;
11160 if (stub_entry->type.r2save)
11161 size += 6 * 4;
11164 return size;
11167 /* Depending on the sign of plt_stub_align:
11168 If positive, return the padding to align to a 2**plt_stub_align
11169 boundary.
11170 If negative, if this stub would cross fewer 2**plt_stub_align
11171 boundaries if we align, then return the padding needed to do so. */
11173 static inline unsigned int
11174 plt_stub_pad (struct ppc_link_hash_table *htab,
11175 struct ppc_stub_hash_entry *stub_entry,
11176 bfd_vma stub_off,
11177 bfd_vma plt_off,
11178 unsigned int odd)
11180 int stub_align;
11181 unsigned stub_size;
11183 if (htab->params->plt_stub_align >= 0)
11185 stub_align = 1 << htab->params->plt_stub_align;
11186 if ((stub_off & (stub_align - 1)) != 0)
11187 return stub_align - (stub_off & (stub_align - 1));
11188 return 0;
11191 stub_align = 1 << -htab->params->plt_stub_align;
11192 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
11193 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11194 > ((stub_size - 1) & -stub_align))
11195 return stub_align - (stub_off & (stub_align - 1));
11196 return 0;
11199 /* Build a toc using .plt call stub. */
11201 static inline bfd_byte *
11202 build_plt_stub (struct ppc_link_hash_table *htab,
11203 struct ppc_stub_hash_entry *stub_entry,
11204 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11206 bfd *obfd = htab->params->stub_bfd;
11207 bool plt_load_toc = htab->opd_abi;
11208 bool plt_static_chain = htab->params->plt_static_chain;
11209 bool plt_thread_safe = (htab->params->plt_thread_safe
11210 && htab->elf.dynamic_sections_created
11211 && stub_entry->h != NULL
11212 && stub_entry->h->elf.dynindx != -1);
11213 bool use_fake_dep = plt_thread_safe;
11214 bfd_vma cmp_branch_off = 0;
11216 if (!ALWAYS_USE_FAKE_DEP
11217 && plt_load_toc
11218 && plt_thread_safe
11219 && !(stub_entry->h != NULL
11220 && is_tls_get_addr (&stub_entry->h->elf, htab)
11221 && htab->params->tls_get_addr_opt))
11223 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11224 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11225 / PLT_ENTRY_SIZE (htab));
11226 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11227 bfd_vma to, from;
11229 if (pltindex > 32768)
11230 glinkoff += (pltindex - 32768) * 4;
11231 to = (glinkoff
11232 + htab->glink->output_offset
11233 + htab->glink->output_section->vma);
11234 from = (p - stub_entry->group->stub_sec->contents
11235 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11236 + 4 * (PPC_HA (offset) != 0)
11237 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11238 != PPC_HA (offset))
11239 + 4 * (plt_static_chain != 0)
11240 + 20
11241 + stub_entry->group->stub_sec->output_offset
11242 + stub_entry->group->stub_sec->output_section->vma);
11243 cmp_branch_off = to - from;
11244 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11247 if (PPC_HA (offset) != 0)
11249 if (r != NULL)
11251 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11252 r[0].r_offset += 4;
11253 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11254 r[1].r_offset = r[0].r_offset + 4;
11255 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11256 r[1].r_addend = r[0].r_addend;
11257 if (plt_load_toc)
11259 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11261 r[2].r_offset = r[1].r_offset + 4;
11262 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11263 r[2].r_addend = r[0].r_addend;
11265 else
11267 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11268 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11269 r[2].r_addend = r[0].r_addend + 8;
11270 if (plt_static_chain)
11272 r[3].r_offset = r[2].r_offset + 4;
11273 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11274 r[3].r_addend = r[0].r_addend + 16;
11279 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11280 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11281 if (plt_load_toc)
11283 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11284 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11286 else
11288 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11289 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11291 if (plt_load_toc
11292 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11294 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11295 offset = 0;
11297 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11298 if (plt_load_toc)
11300 if (use_fake_dep)
11302 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11303 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11305 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11306 if (plt_static_chain)
11307 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11310 else
11312 if (r != NULL)
11314 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11315 r[0].r_offset += 4;
11316 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11317 if (plt_load_toc)
11319 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11321 r[1].r_offset = r[0].r_offset + 4;
11322 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11323 r[1].r_addend = r[0].r_addend;
11325 else
11327 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11328 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11329 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11330 if (plt_static_chain)
11332 r[2].r_offset = r[1].r_offset + 4;
11333 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11334 r[2].r_addend = r[0].r_addend + 8;
11339 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11340 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11341 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11342 if (plt_load_toc
11343 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11345 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11346 offset = 0;
11348 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11349 if (plt_load_toc)
11351 if (use_fake_dep)
11353 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11354 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11356 if (plt_static_chain)
11357 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11358 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11361 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11363 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11364 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11365 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11367 else
11368 bfd_put_32 (obfd, BCTR, p), p += 4;
11369 return p;
11372 /* Build a special .plt call stub for __tls_get_addr. */
11374 #define LD_R0_0R3 0xe8030000
11375 #define LD_R12_0R3 0xe9830000
11376 #define MR_R0_R3 0x7c601b78
11377 #define CMPDI_R0_0 0x2c200000
11378 #define ADD_R3_R12_R13 0x7c6c6a14
11379 #define BEQLR 0x4d820020
11380 #define MR_R3_R0 0x7c030378
11381 #define BCTRL 0x4e800421
11383 static bfd_byte *
11384 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11385 struct ppc_stub_hash_entry *stub_entry,
11386 bfd_byte *p)
11388 bfd *obfd = htab->params->stub_bfd;
11390 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11391 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11392 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11393 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11394 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11395 bfd_put_32 (obfd, BEQLR, p), p += 4;
11396 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11398 if (!htab->params->no_tls_get_addr_regsave)
11399 p = tls_get_addr_prologue (obfd, p, htab);
11400 else if (stub_entry->type.r2save)
11402 bfd_put_32 (obfd, MFLR_R0, p);
11403 p += 4;
11404 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11405 p += 4;
11407 return p;
11410 static bfd_byte *
11411 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11412 struct ppc_stub_hash_entry *stub_entry,
11413 bfd_byte *p,
11414 bfd_byte *loc)
11416 bfd *obfd = htab->params->stub_bfd;
11418 if (!htab->params->no_tls_get_addr_regsave)
11420 bfd_put_32 (obfd, BCTRL, p - 4);
11422 if (stub_entry->type.r2save)
11424 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11425 p += 4;
11427 p = tls_get_addr_epilogue (obfd, p, htab);
11429 else if (stub_entry->type.r2save)
11431 bfd_put_32 (obfd, BCTRL, p - 4);
11433 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11434 p += 4;
11435 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11436 p += 4;
11437 bfd_put_32 (obfd, MTLR_R0, p);
11438 p += 4;
11439 bfd_put_32 (obfd, BLR, p);
11440 p += 4;
11443 if (htab->glink_eh_frame != NULL
11444 && htab->glink_eh_frame->size != 0)
11446 bfd_byte *base, *eh;
11448 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11449 eh = base + stub_entry->group->eh_size;
11451 if (!htab->params->no_tls_get_addr_regsave)
11453 unsigned int cfa_updt, delta, i;
11455 /* After the bctrl, lr has been modified so we need to emit
11456 .eh_frame info saying the return address is on the stack. In
11457 fact we must put the EH info at or before the call rather
11458 than after it, because the EH info for a call needs to be
11459 specified by that point.
11460 See libgcc/unwind-dw2.c execute_cfa_program.
11461 Any stack pointer update must be described immediately after
11462 the instruction making the change, and since the stdu occurs
11463 after saving regs we put all the reg saves and the cfa
11464 change there. */
11465 cfa_updt = stub_entry->stub_offset + 18 * 4;
11466 delta = cfa_updt - stub_entry->group->lr_restore;
11467 stub_entry->group->lr_restore
11468 = stub_entry->stub_offset + (p - loc) - 4;
11469 eh = eh_advance (htab->elf.dynobj, eh, delta);
11470 *eh++ = DW_CFA_def_cfa_offset;
11471 if (htab->opd_abi)
11473 *eh++ = 128;
11474 *eh++ = 1;
11476 else
11477 *eh++ = 96;
11478 *eh++ = DW_CFA_offset_extended_sf;
11479 *eh++ = 65;
11480 *eh++ = (-16 / 8) & 0x7f;
11481 for (i = 4; i < 12; i++)
11483 *eh++ = DW_CFA_offset + i;
11484 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11486 *eh++ = (DW_CFA_advance_loc
11487 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11488 *eh++ = DW_CFA_def_cfa_offset;
11489 *eh++ = 0;
11490 for (i = 4; i < 12; i++)
11491 *eh++ = DW_CFA_restore + i;
11492 *eh++ = DW_CFA_advance_loc + 2;
11493 *eh++ = DW_CFA_restore_extended;
11494 *eh++ = 65;
11495 stub_entry->group->eh_size = eh - base;
11497 else if (stub_entry->type.r2save)
11499 unsigned int lr_used, delta;
11501 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11502 delta = lr_used - stub_entry->group->lr_restore;
11503 stub_entry->group->lr_restore = lr_used + 16;
11504 eh = eh_advance (htab->elf.dynobj, eh, delta);
11505 *eh++ = DW_CFA_offset_extended_sf;
11506 *eh++ = 65;
11507 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11508 *eh++ = DW_CFA_advance_loc + 4;
11509 *eh++ = DW_CFA_restore_extended;
11510 *eh++ = 65;
11511 stub_entry->group->eh_size = eh - base;
11514 return p;
11517 static Elf_Internal_Rela *
11518 get_relocs (asection *sec, int count)
11520 Elf_Internal_Rela *relocs;
11521 struct bfd_elf_section_data *elfsec_data;
11523 elfsec_data = elf_section_data (sec);
11524 relocs = elfsec_data->relocs;
11525 if (relocs == NULL)
11527 bfd_size_type relsize;
11528 relsize = sec->reloc_count * sizeof (*relocs);
11529 relocs = bfd_alloc (sec->owner, relsize);
11530 if (relocs == NULL)
11531 return NULL;
11532 elfsec_data->relocs = relocs;
11533 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11534 sizeof (Elf_Internal_Shdr));
11535 if (elfsec_data->rela.hdr == NULL)
11536 return NULL;
11537 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11538 * sizeof (Elf64_External_Rela));
11539 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11540 sec->reloc_count = 0;
11542 relocs += sec->reloc_count;
11543 sec->reloc_count += count;
11544 return relocs;
11547 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11548 forms, to the equivalent relocs against the global symbol given by
11549 STUB_ENTRY->H. */
11551 static bool
11552 use_global_in_relocs (struct ppc_link_hash_table *htab,
11553 struct ppc_stub_hash_entry *stub_entry,
11554 Elf_Internal_Rela *r, unsigned int num_rel)
11556 struct elf_link_hash_entry **hashes;
11557 unsigned long symndx;
11558 struct ppc_link_hash_entry *h;
11559 bfd_vma symval;
11561 /* Relocs are always against symbols in their own object file. Fake
11562 up global sym hashes for the stub bfd (which has no symbols). */
11563 hashes = elf_sym_hashes (htab->params->stub_bfd);
11564 if (hashes == NULL)
11566 bfd_size_type hsize;
11568 /* When called the first time, stub_globals will contain the
11569 total number of symbols seen during stub sizing. After
11570 allocating, stub_globals is used as an index to fill the
11571 hashes array. */
11572 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11573 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11574 if (hashes == NULL)
11575 return false;
11576 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11577 htab->stub_globals = 1;
11579 symndx = htab->stub_globals++;
11580 h = stub_entry->h;
11581 hashes[symndx] = &h->elf;
11582 if (h->oh != NULL && h->oh->is_func)
11583 h = ppc_follow_link (h->oh);
11584 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11585 || h->elf.root.type == bfd_link_hash_defweak);
11586 symval = defined_sym_val (&h->elf);
11587 while (num_rel-- != 0)
11589 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11590 if (h->elf.root.u.def.section != stub_entry->target_section)
11592 /* H is an opd symbol. The addend must be zero, and the
11593 branch reloc is the only one we can convert. */
11594 r->r_addend = 0;
11595 break;
11597 else
11598 r->r_addend -= symval;
11599 --r;
11601 return true;
11604 static bfd_vma
11605 get_r2off (struct bfd_link_info *info,
11606 struct ppc_stub_hash_entry *stub_entry)
11608 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11609 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11611 if (r2off == 0)
11613 /* Support linking -R objects. Get the toc pointer from the
11614 opd entry. */
11615 char buf[8];
11616 if (!htab->opd_abi)
11617 return r2off;
11618 asection *opd = stub_entry->h->elf.root.u.def.section;
11619 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11621 if (strcmp (opd->name, ".opd") != 0
11622 || opd->reloc_count != 0)
11624 info->callbacks->einfo
11625 (_("%P: cannot find opd entry toc for `%pT'\n"),
11626 stub_entry->h->elf.root.root.string);
11627 bfd_set_error (bfd_error_bad_value);
11628 return (bfd_vma) -1;
11630 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11631 return (bfd_vma) -1;
11632 r2off = bfd_get_64 (opd->owner, buf);
11633 r2off -= elf_gp (info->output_bfd);
11635 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11636 return r2off;
11639 static bool
11640 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11642 struct ppc_stub_hash_entry *stub_entry;
11643 struct ppc_branch_hash_entry *br_entry;
11644 struct bfd_link_info *info;
11645 struct ppc_link_hash_table *htab;
11646 bfd *obfd;
11647 bfd_byte *loc;
11648 bfd_byte *p, *relp;
11649 bfd_vma targ, off;
11650 Elf_Internal_Rela *r;
11651 asection *plt;
11652 int num_rel;
11653 int odd;
11654 bool is_tga;
11656 /* Massage our args to the form they really have. */
11657 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11658 info = in_arg;
11660 /* Fail if the target section could not be assigned to an output
11661 section. The user should fix his linker script. */
11662 if (stub_entry->target_section != NULL
11663 && stub_entry->target_section->output_section == NULL
11664 && info->non_contiguous_regions)
11665 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11666 "Retry without --enable-non-contiguous-regions.\n"),
11667 stub_entry->target_section);
11669 /* Same for the group. */
11670 if (stub_entry->group->stub_sec != NULL
11671 && stub_entry->group->stub_sec->output_section == NULL
11672 && info->non_contiguous_regions)
11673 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11674 "output section. Retry without "
11675 "--enable-non-contiguous-regions.\n"),
11676 stub_entry->group->stub_sec,
11677 stub_entry->target_section);
11679 htab = ppc_hash_table (info);
11680 if (htab == NULL)
11681 return false;
11683 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11684 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11686 htab->stub_count[stub_entry->type.main - 1] += 1;
11687 if (stub_entry->type.main == ppc_stub_long_branch
11688 && stub_entry->type.sub == ppc_stub_toc)
11690 /* Branches are relative. This is where we are going to. */
11691 targ = (stub_entry->target_value
11692 + stub_entry->target_section->output_offset
11693 + stub_entry->target_section->output_section->vma);
11694 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11696 /* And this is where we are coming from. */
11697 off = (stub_entry->stub_offset
11698 + stub_entry->group->stub_sec->output_offset
11699 + stub_entry->group->stub_sec->output_section->vma);
11700 off = targ - off;
11702 p = loc;
11703 obfd = htab->params->stub_bfd;
11704 if (stub_entry->type.r2save)
11706 bfd_vma r2off = get_r2off (info, stub_entry);
11708 if (r2off == (bfd_vma) -1)
11710 htab->stub_error = true;
11711 return false;
11713 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11714 p += 4;
11715 if (PPC_HA (r2off) != 0)
11717 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11718 p += 4;
11720 if (PPC_LO (r2off) != 0)
11722 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11723 p += 4;
11725 off -= p - loc;
11727 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11728 p += 4;
11730 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11732 _bfd_error_handler
11733 (_("long branch stub `%s' offset overflow"),
11734 stub_entry->root.string);
11735 htab->stub_error = true;
11736 return false;
11739 if (info->emitrelocations)
11741 r = get_relocs (stub_entry->group->stub_sec, 1);
11742 if (r == NULL)
11743 return false;
11744 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11745 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11746 r->r_addend = targ;
11747 if (stub_entry->h != NULL
11748 && !use_global_in_relocs (htab, stub_entry, r, 1))
11749 return false;
11752 else if (stub_entry->type.main == ppc_stub_plt_branch
11753 && stub_entry->type.sub == ppc_stub_toc)
11755 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11756 stub_entry->root.string + 9,
11757 false, false);
11758 if (br_entry == NULL)
11760 _bfd_error_handler (_("can't find branch stub `%s'"),
11761 stub_entry->root.string);
11762 htab->stub_error = true;
11763 return false;
11766 targ = (stub_entry->target_value
11767 + stub_entry->target_section->output_offset
11768 + stub_entry->target_section->output_section->vma);
11769 if (!stub_entry->type.r2save)
11770 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11772 bfd_put_64 (htab->brlt->owner, targ,
11773 htab->brlt->contents + br_entry->offset);
11775 if (br_entry->iter == htab->stub_iteration)
11777 br_entry->iter = 0;
11779 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11781 /* Create a reloc for the branch lookup table entry. */
11782 Elf_Internal_Rela rela;
11783 bfd_byte *rl;
11785 rela.r_offset = (br_entry->offset
11786 + htab->brlt->output_offset
11787 + htab->brlt->output_section->vma);
11788 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11789 rela.r_addend = targ;
11791 rl = htab->relbrlt->contents;
11792 rl += (htab->relbrlt->reloc_count++
11793 * sizeof (Elf64_External_Rela));
11794 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11796 else if (info->emitrelocations)
11798 r = get_relocs (htab->brlt, 1);
11799 if (r == NULL)
11800 return false;
11801 /* brlt, being SEC_LINKER_CREATED does not go through the
11802 normal reloc processing. Symbols and offsets are not
11803 translated from input file to output file form, so
11804 set up the offset per the output file. */
11805 r->r_offset = (br_entry->offset
11806 + htab->brlt->output_offset
11807 + htab->brlt->output_section->vma);
11808 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11809 r->r_addend = targ;
11813 targ = (br_entry->offset
11814 + htab->brlt->output_offset
11815 + htab->brlt->output_section->vma);
11817 off = (elf_gp (info->output_bfd)
11818 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11819 off = targ - off;
11821 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11823 info->callbacks->einfo
11824 (_("%P: linkage table error against `%pT'\n"),
11825 stub_entry->root.string);
11826 bfd_set_error (bfd_error_bad_value);
11827 htab->stub_error = true;
11828 return false;
11831 if (info->emitrelocations)
11833 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11834 if (r == NULL)
11835 return false;
11836 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11837 if (bfd_big_endian (info->output_bfd))
11838 r[0].r_offset += 2;
11839 if (stub_entry->type.r2save)
11840 r[0].r_offset += 4;
11841 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11842 r[0].r_addend = targ;
11843 if (PPC_HA (off) != 0)
11845 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11846 r[1].r_offset = r[0].r_offset + 4;
11847 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11848 r[1].r_addend = r[0].r_addend;
11852 p = loc;
11853 obfd = htab->params->stub_bfd;
11854 if (!stub_entry->type.r2save)
11856 if (PPC_HA (off) != 0)
11858 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11859 p += 4;
11860 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11862 else
11863 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11865 else
11867 bfd_vma r2off = get_r2off (info, stub_entry);
11869 if (r2off == (bfd_vma) -1)
11871 htab->stub_error = true;
11872 return false;
11875 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11876 p += 4;
11877 if (PPC_HA (off) != 0)
11879 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11880 p += 4;
11881 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11883 else
11884 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11886 if (PPC_HA (r2off) != 0)
11888 p += 4;
11889 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11891 if (PPC_LO (r2off) != 0)
11893 p += 4;
11894 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11897 p += 4;
11898 bfd_put_32 (obfd, MTCTR_R12, p);
11899 p += 4;
11900 bfd_put_32 (obfd, BCTR, p);
11901 p += 4;
11903 else if (stub_entry->type.sub >= ppc_stub_notoc)
11905 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
11906 p = loc;
11907 off = (stub_entry->stub_offset
11908 + stub_entry->group->stub_sec->output_offset
11909 + stub_entry->group->stub_sec->output_section->vma);
11910 obfd = htab->params->stub_bfd;
11911 is_tga = (is_plt
11912 && stub_entry->h != NULL
11913 && is_tls_get_addr (&stub_entry->h->elf, htab)
11914 && htab->params->tls_get_addr_opt);
11915 if (is_tga)
11917 p = build_tls_get_addr_head (htab, stub_entry, p);
11918 off += p - loc;
11920 if (stub_entry->type.r2save)
11922 off += 4;
11923 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11924 p += 4;
11926 if (is_plt)
11928 targ = stub_entry->plt_ent->plt.offset & ~1;
11929 if (targ >= (bfd_vma) -2)
11930 abort ();
11932 plt = htab->elf.splt;
11933 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11935 if (stub_entry->symtype == STT_GNU_IFUNC)
11936 plt = htab->elf.iplt;
11937 else
11938 plt = htab->pltlocal;
11940 targ += plt->output_offset + plt->output_section->vma;
11942 else
11943 targ = (stub_entry->target_value
11944 + stub_entry->target_section->output_offset
11945 + stub_entry->target_section->output_section->vma);
11946 odd = off & 4;
11947 off = targ - off;
11949 relp = p;
11950 num_rel = 0;
11951 if (stub_entry->type.sub == ppc_stub_notoc)
11952 p = build_power10_offset (obfd, p, off, odd, is_plt);
11953 else
11955 if (htab->glink_eh_frame != NULL
11956 && htab->glink_eh_frame->size != 0)
11958 bfd_byte *base, *eh;
11959 unsigned int lr_used, delta;
11961 base = (htab->glink_eh_frame->contents
11962 + stub_entry->group->eh_base + 17);
11963 eh = base + stub_entry->group->eh_size;
11964 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11965 delta = lr_used - stub_entry->group->lr_restore;
11966 stub_entry->group->lr_restore = lr_used + 8;
11967 eh = eh_advance (htab->elf.dynobj, eh, delta);
11968 *eh++ = DW_CFA_register;
11969 *eh++ = 65;
11970 *eh++ = 12;
11971 *eh++ = DW_CFA_advance_loc + 2;
11972 *eh++ = DW_CFA_restore_extended;
11973 *eh++ = 65;
11974 stub_entry->group->eh_size = eh - base;
11977 /* The notoc stubs calculate their target (either a PLT entry or
11978 the global entry point of a function) relative to the PC
11979 returned by the "bcl" two instructions past the start of the
11980 sequence emitted by build_offset. The offset is therefore 8
11981 less than calculated from the start of the sequence. */
11982 off -= 8;
11983 p = build_offset (obfd, p, off, is_plt);
11986 if (stub_entry->type.main == ppc_stub_long_branch)
11988 bfd_vma from;
11989 num_rel = 1;
11990 from = (stub_entry->stub_offset
11991 + stub_entry->group->stub_sec->output_offset
11992 + stub_entry->group->stub_sec->output_section->vma
11993 + (p - loc));
11994 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
11996 else
11998 bfd_put_32 (obfd, MTCTR_R12, p);
11999 p += 4;
12000 bfd_put_32 (obfd, BCTR, p);
12002 p += 4;
12004 if (is_tga)
12005 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12007 if (info->emitrelocations)
12009 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12010 if (stub_entry->type.sub == ppc_stub_notoc)
12011 num_rel += num_relocs_for_power10_offset (off, odd);
12012 else
12014 num_rel += num_relocs_for_offset (off);
12015 roff += 16;
12017 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12018 if (r == NULL)
12019 return false;
12020 if (stub_entry->type.sub == ppc_stub_notoc)
12021 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12022 else
12023 r = emit_relocs_for_offset (info, r, roff, targ, off);
12024 if (stub_entry->type.main == ppc_stub_long_branch)
12026 ++r;
12027 roff = p - 4 - stub_entry->group->stub_sec->contents;
12028 r->r_offset = roff;
12029 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12030 r->r_addend = targ;
12031 if (stub_entry->h != NULL
12032 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12033 return false;
12037 else if (stub_entry->type.main == ppc_stub_plt_call)
12039 if (stub_entry->h != NULL
12040 && stub_entry->h->is_func_descriptor
12041 && stub_entry->h->oh != NULL)
12043 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12045 /* If the old-ABI "dot-symbol" is undefined make it weak so
12046 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12047 if (fh->elf.root.type == bfd_link_hash_undefined
12048 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12049 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12050 fh->elf.root.type = bfd_link_hash_undefweak;
12053 /* Now build the stub. */
12054 targ = stub_entry->plt_ent->plt.offset & ~1;
12055 if (targ >= (bfd_vma) -2)
12056 abort ();
12058 plt = htab->elf.splt;
12059 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12061 if (stub_entry->symtype == STT_GNU_IFUNC)
12062 plt = htab->elf.iplt;
12063 else
12064 plt = htab->pltlocal;
12066 targ += plt->output_offset + plt->output_section->vma;
12068 off = (elf_gp (info->output_bfd)
12069 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12070 off = targ - off;
12072 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12074 info->callbacks->einfo
12075 /* xgettext:c-format */
12076 (_("%P: linkage table error against `%pT'\n"),
12077 stub_entry->h != NULL
12078 ? stub_entry->h->elf.root.root.string
12079 : "<local sym>");
12080 bfd_set_error (bfd_error_bad_value);
12081 htab->stub_error = true;
12082 return false;
12085 r = NULL;
12086 if (info->emitrelocations)
12088 r = get_relocs (stub_entry->group->stub_sec,
12089 ((PPC_HA (off) != 0)
12090 + (htab->opd_abi
12091 ? 2 + (htab->params->plt_static_chain
12092 && PPC_HA (off + 16) == PPC_HA (off))
12093 : 1)));
12094 if (r == NULL)
12095 return false;
12096 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12097 if (bfd_big_endian (info->output_bfd))
12098 r[0].r_offset += 2;
12099 r[0].r_addend = targ;
12101 p = loc;
12102 obfd = htab->params->stub_bfd;
12103 is_tga = (stub_entry->h != NULL
12104 && is_tls_get_addr (&stub_entry->h->elf, htab)
12105 && htab->params->tls_get_addr_opt);
12106 if (is_tga)
12108 p = build_tls_get_addr_head (htab, stub_entry, p);
12109 if (r != NULL)
12110 r[0].r_offset += p - loc;
12112 p = build_plt_stub (htab, stub_entry, p, off, r);
12113 if (is_tga)
12114 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12116 else if (stub_entry->type.main == ppc_stub_save_res)
12117 return true;
12118 else
12120 BFD_FAIL ();
12121 return false;
12124 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12126 if (htab->params->emit_stub_syms)
12128 struct elf_link_hash_entry *h;
12129 size_t len1, len2;
12130 char *name;
12131 const char *const stub_str[] = { "long_branch",
12132 "plt_branch",
12133 "plt_call" };
12135 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12136 len2 = strlen (stub_entry->root.string);
12137 name = bfd_malloc (len1 + len2 + 2);
12138 if (name == NULL)
12139 return false;
12140 memcpy (name, stub_entry->root.string, 9);
12141 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12142 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12143 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12144 if (h == NULL)
12145 return false;
12146 if (h->root.type == bfd_link_hash_new)
12148 h->root.type = bfd_link_hash_defined;
12149 h->root.u.def.section = stub_entry->group->stub_sec;
12150 h->root.u.def.value = stub_entry->stub_offset;
12151 h->ref_regular = 1;
12152 h->def_regular = 1;
12153 h->ref_regular_nonweak = 1;
12154 h->forced_local = 1;
12155 h->non_elf = 0;
12156 h->root.linker_def = 1;
12160 return true;
12163 /* As above, but don't actually build the stub. Just bump offset so
12164 we know stub section sizes, and select plt_branch stubs where
12165 long_branch stubs won't do. */
12167 static bool
12168 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12170 struct ppc_stub_hash_entry *stub_entry;
12171 struct bfd_link_info *info;
12172 struct ppc_link_hash_table *htab;
12173 asection *plt;
12174 bfd_vma targ, off, r2off;
12175 unsigned int size, extra, lr_used, delta, odd;
12176 bfd_vma stub_offset;
12178 /* Massage our args to the form they really have. */
12179 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12180 info = in_arg;
12182 htab = ppc_hash_table (info);
12183 if (htab == NULL)
12184 return false;
12186 /* Fail if the target section could not be assigned to an output
12187 section. The user should fix his linker script. */
12188 if (stub_entry->target_section != NULL
12189 && stub_entry->target_section->output_section == NULL
12190 && info->non_contiguous_regions)
12191 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12192 "Retry without --enable-non-contiguous-regions.\n"),
12193 stub_entry->target_section);
12195 /* Same for the group. */
12196 if (stub_entry->group->stub_sec != NULL
12197 && stub_entry->group->stub_sec->output_section == NULL
12198 && info->non_contiguous_regions)
12199 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12200 "output section. Retry without "
12201 "--enable-non-contiguous-regions.\n"),
12202 stub_entry->group->stub_sec,
12203 stub_entry->target_section);
12205 /* Make a note of the offset within the stubs for this entry. */
12206 stub_offset = stub_entry->group->stub_sec->size;
12207 if (htab->stub_iteration > STUB_SHRINK_ITER
12208 && stub_entry->stub_offset > stub_offset)
12209 stub_offset = stub_entry->stub_offset;
12211 if (stub_entry->h != NULL
12212 && stub_entry->h->save_res
12213 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12214 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12216 /* Don't make stubs to out-of-line register save/restore
12217 functions. Instead, emit copies of the functions. */
12218 stub_entry->group->needs_save_res = 1;
12219 stub_entry->type.main = ppc_stub_save_res;
12220 stub_entry->type.sub = ppc_stub_toc;
12221 stub_entry->type.r2save = 0;
12222 return true;
12225 if (stub_entry->type.main == ppc_stub_plt_branch)
12227 /* Reset the stub type from the plt branch variant in case we now
12228 can reach with a shorter stub. */
12229 stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
12232 if (stub_entry->type.main == ppc_stub_long_branch
12233 && stub_entry->type.sub == ppc_stub_toc)
12235 targ = (stub_entry->target_value
12236 + stub_entry->target_section->output_offset
12237 + stub_entry->target_section->output_section->vma);
12238 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12239 off = (stub_offset
12240 + stub_entry->group->stub_sec->output_offset
12241 + stub_entry->group->stub_sec->output_section->vma);
12243 size = 4;
12244 r2off = 0;
12245 if (stub_entry->type.r2save)
12247 r2off = get_r2off (info, stub_entry);
12248 if (r2off == (bfd_vma) -1)
12250 htab->stub_error = true;
12251 return false;
12253 size = 8;
12254 if (PPC_HA (r2off) != 0)
12255 size += 4;
12256 if (PPC_LO (r2off) != 0)
12257 size += 4;
12258 off += size - 4;
12260 off = targ - off;
12262 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12263 Do the same for -R objects without function descriptors. */
12264 if ((stub_entry->type.r2save
12265 && r2off == 0
12266 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12267 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12269 struct ppc_branch_hash_entry *br_entry;
12271 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12272 stub_entry->root.string + 9,
12273 true, false);
12274 if (br_entry == NULL)
12276 _bfd_error_handler (_("can't build branch stub `%s'"),
12277 stub_entry->root.string);
12278 htab->stub_error = true;
12279 return false;
12282 if (br_entry->iter != htab->stub_iteration)
12284 br_entry->iter = htab->stub_iteration;
12285 br_entry->offset = htab->brlt->size;
12286 htab->brlt->size += 8;
12288 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12289 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12290 else if (info->emitrelocations)
12292 htab->brlt->reloc_count += 1;
12293 htab->brlt->flags |= SEC_RELOC;
12297 targ = (br_entry->offset
12298 + htab->brlt->output_offset
12299 + htab->brlt->output_section->vma);
12300 off = (elf_gp (info->output_bfd)
12301 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12302 off = targ - off;
12304 if (info->emitrelocations)
12306 stub_entry->group->stub_sec->reloc_count
12307 += 1 + (PPC_HA (off) != 0);
12308 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12311 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12312 if (!stub_entry->type.r2save)
12314 size = 12;
12315 if (PPC_HA (off) != 0)
12316 size = 16;
12318 else
12320 size = 16;
12321 if (PPC_HA (off) != 0)
12322 size += 4;
12324 if (PPC_HA (r2off) != 0)
12325 size += 4;
12326 if (PPC_LO (r2off) != 0)
12327 size += 4;
12330 else if (info->emitrelocations)
12332 stub_entry->group->stub_sec->reloc_count += 1;
12333 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12336 else if (stub_entry->type.main == ppc_stub_long_branch)
12338 off = (stub_offset
12339 + stub_entry->group->stub_sec->output_offset
12340 + stub_entry->group->stub_sec->output_section->vma);
12341 size = 0;
12342 if (stub_entry->type.r2save)
12343 size = 4;
12344 off += size;
12345 targ = (stub_entry->target_value
12346 + stub_entry->target_section->output_offset
12347 + stub_entry->target_section->output_section->vma);
12348 odd = off & 4;
12349 off = targ - off;
12351 if (info->emitrelocations)
12353 unsigned int num_rel;
12354 if (stub_entry->type.sub == ppc_stub_notoc)
12355 num_rel = num_relocs_for_power10_offset (off, odd);
12356 else
12357 num_rel = num_relocs_for_offset (off - 8);
12358 stub_entry->group->stub_sec->reloc_count += num_rel;
12359 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12362 if (stub_entry->type.sub == ppc_stub_notoc)
12363 extra = size_power10_offset (off, odd);
12364 else
12365 extra = size_offset (off - 8);
12366 /* Include branch insn plus those in the offset sequence. */
12367 size += 4 + extra;
12368 /* The branch insn is at the end, or "extra" bytes along. So
12369 its offset will be "extra" bytes less that that already
12370 calculated. */
12371 off -= extra;
12373 if (stub_entry->type.sub != ppc_stub_notoc)
12375 /* After the bcl, lr has been modified so we need to emit
12376 .eh_frame info saying the return address is in r12. */
12377 lr_used = stub_offset + 8;
12378 if (stub_entry->type.r2save)
12379 lr_used += 4;
12380 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12381 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12382 DW_CFA_restore_extended 65. */
12383 delta = lr_used - stub_entry->group->lr_restore;
12384 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12385 stub_entry->group->lr_restore = lr_used + 8;
12388 /* If the branch can't reach, use a plt_branch. */
12389 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12391 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12392 size += 4;
12394 else if (info->emitrelocations)
12395 stub_entry->group->stub_sec->reloc_count +=1;
12397 else if (stub_entry->type.sub >= ppc_stub_notoc)
12399 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12400 lr_used = 0;
12401 if (stub_entry->h != NULL
12402 && is_tls_get_addr (&stub_entry->h->elf, htab)
12403 && htab->params->tls_get_addr_opt)
12405 lr_used += 7 * 4;
12406 if (!htab->params->no_tls_get_addr_regsave)
12407 lr_used += 11 * 4;
12408 else if (stub_entry->type.r2save)
12409 lr_used += 2 * 4;
12411 if (stub_entry->type.r2save)
12412 lr_used += 4;
12413 targ = stub_entry->plt_ent->plt.offset & ~1;
12414 if (targ >= (bfd_vma) -2)
12415 abort ();
12417 plt = htab->elf.splt;
12418 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12420 if (stub_entry->symtype == STT_GNU_IFUNC)
12421 plt = htab->elf.iplt;
12422 else
12423 plt = htab->pltlocal;
12425 targ += plt->output_offset + plt->output_section->vma;
12426 off = (stub_offset
12427 + stub_entry->group->stub_sec->output_offset
12428 + stub_entry->group->stub_sec->output_section->vma
12429 + lr_used);
12430 odd = off & 4;
12431 off = targ - off;
12433 if (htab->params->plt_stub_align != 0)
12435 unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, odd);
12437 stub_offset += pad;
12438 off -= pad;
12439 odd ^= pad & 4;
12442 if (info->emitrelocations)
12444 unsigned int num_rel;
12445 if (stub_entry->type.sub == ppc_stub_notoc)
12446 num_rel = num_relocs_for_power10_offset (off, odd);
12447 else
12448 num_rel = num_relocs_for_offset (off - 8);
12449 stub_entry->group->stub_sec->reloc_count += num_rel;
12450 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12453 size = plt_stub_size (htab, stub_entry, off, odd);
12455 if (stub_entry->type.sub != ppc_stub_notoc)
12457 /* After the bcl, lr has been modified so we need to emit
12458 .eh_frame info saying the return address is in r12. */
12459 lr_used += stub_offset + 8;
12460 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12461 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12462 DW_CFA_restore_extended 65. */
12463 delta = lr_used - stub_entry->group->lr_restore;
12464 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12465 stub_entry->group->lr_restore = lr_used + 8;
12467 if (stub_entry->h != NULL
12468 && is_tls_get_addr (&stub_entry->h->elf, htab)
12469 && htab->params->tls_get_addr_opt)
12471 if (!htab->params->no_tls_get_addr_regsave)
12473 unsigned int cfa_updt = stub_offset + 18 * 4;
12474 delta = cfa_updt - stub_entry->group->lr_restore;
12475 stub_entry->group->eh_size += eh_advance_size (delta);
12476 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12477 stub_entry->group->lr_restore = stub_offset + size - 4;
12479 else if (stub_entry->type.r2save)
12481 lr_used = stub_offset + size - 20;
12482 delta = lr_used - stub_entry->group->lr_restore;
12483 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12484 stub_entry->group->lr_restore = stub_offset + size - 4;
12488 else if (stub_entry->type.main == ppc_stub_plt_call)
12490 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12491 if (targ >= (bfd_vma) -2)
12492 abort ();
12493 plt = htab->elf.splt;
12494 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12496 if (stub_entry->symtype == STT_GNU_IFUNC)
12497 plt = htab->elf.iplt;
12498 else
12499 plt = htab->pltlocal;
12501 targ += plt->output_offset + plt->output_section->vma;
12503 off = (elf_gp (info->output_bfd)
12504 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12505 off = targ - off;
12507 if (htab->params->plt_stub_align != 0)
12509 unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, 0);
12511 stub_offset += pad;
12514 if (info->emitrelocations)
12516 stub_entry->group->stub_sec->reloc_count
12517 += ((PPC_HA (off) != 0)
12518 + (htab->opd_abi
12519 ? 2 + (htab->params->plt_static_chain
12520 && PPC_HA (off + 16) == PPC_HA (off))
12521 : 1));
12522 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12525 size = plt_stub_size (htab, stub_entry, off, 0);
12527 if (stub_entry->h != NULL
12528 && is_tls_get_addr (&stub_entry->h->elf, htab)
12529 && htab->params->tls_get_addr_opt
12530 && stub_entry->type.r2save)
12532 if (!htab->params->no_tls_get_addr_regsave)
12534 /* Adjustments to r1 need to be described. */
12535 unsigned int cfa_updt = stub_offset + 18 * 4;
12536 delta = cfa_updt - stub_entry->group->lr_restore;
12537 stub_entry->group->eh_size += eh_advance_size (delta);
12538 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12540 else
12542 lr_used = stub_offset + size - 20;
12543 /* The eh_frame info will consist of a DW_CFA_advance_loc
12544 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12545 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12546 delta = lr_used - stub_entry->group->lr_restore;
12547 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12549 stub_entry->group->lr_restore = stub_offset + size - 4;
12552 else
12554 BFD_FAIL ();
12555 return false;
12558 if (stub_entry->stub_offset != stub_offset)
12559 htab->stub_changed = true;
12560 stub_entry->stub_offset = stub_offset;
12561 stub_entry->group->stub_sec->size = stub_offset + size;
12562 return true;
12565 /* Set up various things so that we can make a list of input sections
12566 for each output section included in the link. Returns -1 on error,
12567 0 when no stubs will be needed, and 1 on success. */
12570 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12572 unsigned int id;
12573 size_t amt;
12574 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12576 if (htab == NULL)
12577 return -1;
12579 htab->sec_info_arr_size = _bfd_section_id;
12580 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12581 htab->sec_info = bfd_zmalloc (amt);
12582 if (htab->sec_info == NULL)
12583 return -1;
12585 /* Set toc_off for com, und, abs and ind sections. */
12586 for (id = 0; id < 3; id++)
12587 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12589 return 1;
12592 /* Set up for first pass at multitoc partitioning. */
12594 void
12595 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12597 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12599 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12600 htab->toc_bfd = NULL;
12601 htab->toc_first_sec = NULL;
12604 /* The linker repeatedly calls this function for each TOC input section
12605 and linker generated GOT section. Group input bfds such that the toc
12606 within a group is less than 64k in size. */
12608 bool
12609 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12611 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12612 bfd_vma addr, off, limit;
12614 if (htab == NULL)
12615 return false;
12617 if (!htab->second_toc_pass)
12619 /* Keep track of the first .toc or .got section for this input bfd. */
12620 bool new_bfd = htab->toc_bfd != isec->owner;
12622 if (new_bfd)
12624 htab->toc_bfd = isec->owner;
12625 htab->toc_first_sec = isec;
12628 addr = isec->output_offset + isec->output_section->vma;
12629 off = addr - htab->toc_curr;
12630 limit = 0x80008000;
12631 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12632 limit = 0x10000;
12633 if (off + isec->size > limit)
12635 addr = (htab->toc_first_sec->output_offset
12636 + htab->toc_first_sec->output_section->vma);
12637 htab->toc_curr = addr;
12638 htab->toc_curr &= -TOC_BASE_ALIGN;
12641 /* toc_curr is the base address of this toc group. Set elf_gp
12642 for the input section to be the offset relative to the
12643 output toc base plus 0x8000. Making the input elf_gp an
12644 offset allows us to move the toc as a whole without
12645 recalculating input elf_gp. */
12646 off = htab->toc_curr - elf_gp (info->output_bfd);
12647 off += TOC_BASE_OFF;
12649 /* Die if someone uses a linker script that doesn't keep input
12650 file .toc and .got together. */
12651 if (new_bfd
12652 && elf_gp (isec->owner) != 0
12653 && elf_gp (isec->owner) != off)
12654 return false;
12656 elf_gp (isec->owner) = off;
12657 return true;
12660 /* During the second pass toc_first_sec points to the start of
12661 a toc group, and toc_curr is used to track the old elf_gp.
12662 We use toc_bfd to ensure we only look at each bfd once. */
12663 if (htab->toc_bfd == isec->owner)
12664 return true;
12665 htab->toc_bfd = isec->owner;
12667 if (htab->toc_first_sec == NULL
12668 || htab->toc_curr != elf_gp (isec->owner))
12670 htab->toc_curr = elf_gp (isec->owner);
12671 htab->toc_first_sec = isec;
12673 addr = (htab->toc_first_sec->output_offset
12674 + htab->toc_first_sec->output_section->vma);
12675 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12676 elf_gp (isec->owner) = off;
12678 return true;
12681 /* Called via elf_link_hash_traverse to merge GOT entries for global
12682 symbol H. */
12684 static bool
12685 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12687 if (h->root.type == bfd_link_hash_indirect)
12688 return true;
12690 merge_got_entries (&h->got.glist);
12692 return true;
12695 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12696 symbol H. */
12698 static bool
12699 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12701 struct got_entry *gent;
12703 if (h->root.type == bfd_link_hash_indirect)
12704 return true;
12706 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12707 if (!gent->is_indirect)
12708 allocate_got (h, (struct bfd_link_info *) inf, gent);
12709 return true;
12712 /* Called on the first multitoc pass after the last call to
12713 ppc64_elf_next_toc_section. This function removes duplicate GOT
12714 entries. */
12716 bool
12717 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12719 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12720 struct bfd *ibfd, *ibfd2;
12721 bool done_something;
12723 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12725 if (!htab->do_multi_toc)
12726 return false;
12728 /* Merge global sym got entries within a toc group. */
12729 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12731 /* And tlsld_got. */
12732 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12734 struct got_entry *ent, *ent2;
12736 if (!is_ppc64_elf (ibfd))
12737 continue;
12739 ent = ppc64_tlsld_got (ibfd);
12740 if (!ent->is_indirect
12741 && ent->got.offset != (bfd_vma) -1)
12743 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12745 if (!is_ppc64_elf (ibfd2))
12746 continue;
12748 ent2 = ppc64_tlsld_got (ibfd2);
12749 if (!ent2->is_indirect
12750 && ent2->got.offset != (bfd_vma) -1
12751 && elf_gp (ibfd2) == elf_gp (ibfd))
12753 ent2->is_indirect = true;
12754 ent2->got.ent = ent;
12760 /* Zap sizes of got sections. */
12761 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12762 htab->elf.irelplt->size -= htab->got_reli_size;
12763 htab->got_reli_size = 0;
12765 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12767 asection *got, *relgot;
12769 if (!is_ppc64_elf (ibfd))
12770 continue;
12772 got = ppc64_elf_tdata (ibfd)->got;
12773 if (got != NULL)
12775 got->rawsize = got->size;
12776 got->size = 0;
12777 relgot = ppc64_elf_tdata (ibfd)->relgot;
12778 relgot->rawsize = relgot->size;
12779 relgot->size = 0;
12783 /* Now reallocate the got, local syms first. We don't need to
12784 allocate section contents again since we never increase size. */
12785 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12787 struct got_entry **lgot_ents;
12788 struct got_entry **end_lgot_ents;
12789 struct plt_entry **local_plt;
12790 struct plt_entry **end_local_plt;
12791 unsigned char *lgot_masks;
12792 bfd_size_type locsymcount;
12793 Elf_Internal_Shdr *symtab_hdr;
12794 asection *s;
12795 Elf_Internal_Sym *local_syms;
12796 Elf_Internal_Sym *isym;
12798 if (!is_ppc64_elf (ibfd))
12799 continue;
12801 lgot_ents = elf_local_got_ents (ibfd);
12802 if (!lgot_ents)
12803 continue;
12805 symtab_hdr = &elf_symtab_hdr (ibfd);
12806 locsymcount = symtab_hdr->sh_info;
12807 end_lgot_ents = lgot_ents + locsymcount;
12808 local_plt = (struct plt_entry **) end_lgot_ents;
12809 end_local_plt = local_plt + locsymcount;
12810 lgot_masks = (unsigned char *) end_local_plt;
12811 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12812 if (local_syms == NULL && locsymcount != 0)
12814 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12815 0, NULL, NULL, NULL);
12816 if (local_syms == NULL)
12817 return false;
12819 s = ppc64_elf_tdata (ibfd)->got;
12820 for (isym = local_syms;
12821 lgot_ents < end_lgot_ents;
12822 ++lgot_ents, ++lgot_masks, isym++)
12824 struct got_entry *ent;
12826 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12828 unsigned int ent_size = 8;
12829 unsigned int rel_size = sizeof (Elf64_External_Rela);
12831 ent->got.offset = s->size;
12832 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12834 ent_size *= 2;
12835 rel_size *= 2;
12837 s->size += ent_size;
12838 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12840 htab->elf.irelplt->size += rel_size;
12841 htab->got_reli_size += rel_size;
12843 else if (bfd_link_pic (info)
12844 && (ent->tls_type == 0
12845 ? !info->enable_dt_relr
12846 : !bfd_link_executable (info))
12847 && isym->st_shndx != SHN_ABS)
12849 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12850 srel->size += rel_size;
12856 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12858 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12860 struct got_entry *ent;
12862 if (!is_ppc64_elf (ibfd))
12863 continue;
12865 ent = ppc64_tlsld_got (ibfd);
12866 if (!ent->is_indirect
12867 && ent->got.offset != (bfd_vma) -1)
12869 asection *s = ppc64_elf_tdata (ibfd)->got;
12870 ent->got.offset = s->size;
12871 s->size += 16;
12872 if (bfd_link_dll (info))
12874 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12875 srel->size += sizeof (Elf64_External_Rela);
12880 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12881 if (!done_something)
12882 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12884 asection *got;
12886 if (!is_ppc64_elf (ibfd))
12887 continue;
12889 got = ppc64_elf_tdata (ibfd)->got;
12890 if (got != NULL)
12892 done_something = got->rawsize != got->size;
12893 if (done_something)
12894 break;
12898 if (done_something)
12899 (*htab->params->layout_sections_again) ();
12901 /* Set up for second pass over toc sections to recalculate elf_gp
12902 on input sections. */
12903 htab->toc_bfd = NULL;
12904 htab->toc_first_sec = NULL;
12905 htab->second_toc_pass = true;
12906 return done_something;
12909 /* Called after second pass of multitoc partitioning. */
12911 void
12912 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12914 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12916 /* After the second pass, toc_curr tracks the TOC offset used
12917 for code sections below in ppc64_elf_next_input_section. */
12918 htab->toc_curr = TOC_BASE_OFF;
12921 /* No toc references were found in ISEC. If the code in ISEC makes no
12922 calls, then there's no need to use toc adjusting stubs when branching
12923 into ISEC. Actually, indirect calls from ISEC are OK as they will
12924 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12925 needed, and 2 if a cyclical call-graph was found but no other reason
12926 for a stub was detected. If called from the top level, a return of
12927 2 means the same as a return of 0. */
12929 static int
12930 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12932 int ret;
12934 /* Mark this section as checked. */
12935 isec->call_check_done = 1;
12937 /* We know none of our code bearing sections will need toc stubs. */
12938 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12939 return 0;
12941 if (isec->size == 0)
12942 return 0;
12944 if (isec->output_section == NULL)
12945 return 0;
12947 ret = 0;
12948 if (isec->reloc_count != 0)
12950 Elf_Internal_Rela *relstart, *rel;
12951 Elf_Internal_Sym *local_syms;
12952 struct ppc_link_hash_table *htab;
12954 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12955 info->keep_memory);
12956 if (relstart == NULL)
12957 return -1;
12959 /* Look for branches to outside of this section. */
12960 local_syms = NULL;
12961 htab = ppc_hash_table (info);
12962 if (htab == NULL)
12963 return -1;
12965 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12967 enum elf_ppc64_reloc_type r_type;
12968 unsigned long r_symndx;
12969 struct elf_link_hash_entry *h;
12970 struct ppc_link_hash_entry *eh;
12971 Elf_Internal_Sym *sym;
12972 asection *sym_sec;
12973 struct _opd_sec_data *opd;
12974 bfd_vma sym_value;
12975 bfd_vma dest;
12977 r_type = ELF64_R_TYPE (rel->r_info);
12978 if (r_type != R_PPC64_REL24
12979 && r_type != R_PPC64_REL24_NOTOC
12980 && r_type != R_PPC64_REL24_P9NOTOC
12981 && r_type != R_PPC64_REL14
12982 && r_type != R_PPC64_REL14_BRTAKEN
12983 && r_type != R_PPC64_REL14_BRNTAKEN
12984 && r_type != R_PPC64_PLTCALL
12985 && r_type != R_PPC64_PLTCALL_NOTOC)
12986 continue;
12988 r_symndx = ELF64_R_SYM (rel->r_info);
12989 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12990 isec->owner))
12992 ret = -1;
12993 break;
12996 /* Calls to dynamic lib functions go through a plt call stub
12997 that uses r2. */
12998 eh = ppc_elf_hash_entry (h);
12999 if (eh != NULL
13000 && (eh->elf.plt.plist != NULL
13001 || (eh->oh != NULL
13002 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13004 ret = 1;
13005 break;
13008 if (sym_sec == NULL)
13009 /* Ignore other undefined symbols. */
13010 continue;
13012 /* Assume branches to other sections not included in the
13013 link need stubs too, to cover -R and absolute syms. */
13014 if (sym_sec->output_section == NULL)
13016 ret = 1;
13017 break;
13020 if (h == NULL)
13021 sym_value = sym->st_value;
13022 else
13024 if (h->root.type != bfd_link_hash_defined
13025 && h->root.type != bfd_link_hash_defweak)
13026 abort ();
13027 sym_value = h->root.u.def.value;
13029 sym_value += rel->r_addend;
13031 /* If this branch reloc uses an opd sym, find the code section. */
13032 opd = get_opd_info (sym_sec);
13033 if (opd != NULL)
13035 if (h == NULL && opd->adjust != NULL)
13037 long adjust;
13039 adjust = opd->adjust[OPD_NDX (sym_value)];
13040 if (adjust == -1)
13041 /* Assume deleted functions won't ever be called. */
13042 continue;
13043 sym_value += adjust;
13046 dest = opd_entry_value (sym_sec, sym_value,
13047 &sym_sec, NULL, false);
13048 if (dest == (bfd_vma) -1)
13049 continue;
13051 else
13052 dest = (sym_value
13053 + sym_sec->output_offset
13054 + sym_sec->output_section->vma);
13056 /* Ignore branch to self. */
13057 if (sym_sec == isec)
13058 continue;
13060 /* If the called function uses the toc, we need a stub. */
13061 if (sym_sec->has_toc_reloc
13062 || sym_sec->makes_toc_func_call)
13064 ret = 1;
13065 break;
13068 /* Assume any branch that needs a long branch stub might in fact
13069 need a plt_branch stub. A plt_branch stub uses r2. */
13070 else if (dest - (isec->output_offset
13071 + isec->output_section->vma
13072 + rel->r_offset) + (1 << 25)
13073 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13074 ? h->other
13075 : sym->st_other))
13077 ret = 1;
13078 break;
13081 /* If calling back to a section in the process of being
13082 tested, we can't say for sure that no toc adjusting stubs
13083 are needed, so don't return zero. */
13084 else if (sym_sec->call_check_in_progress)
13085 ret = 2;
13087 /* Branches to another section that itself doesn't have any TOC
13088 references are OK. Recursively call ourselves to check. */
13089 else if (!sym_sec->call_check_done)
13091 int recur;
13093 /* Mark current section as indeterminate, so that other
13094 sections that call back to current won't be marked as
13095 known. */
13096 isec->call_check_in_progress = 1;
13097 recur = toc_adjusting_stub_needed (info, sym_sec);
13098 isec->call_check_in_progress = 0;
13100 if (recur != 0)
13102 ret = recur;
13103 if (recur != 2)
13104 break;
13109 if (elf_symtab_hdr (isec->owner).contents
13110 != (unsigned char *) local_syms)
13111 free (local_syms);
13112 if (elf_section_data (isec)->relocs != relstart)
13113 free (relstart);
13116 if ((ret & 1) == 0
13117 && isec->map_head.s != NULL
13118 && (strcmp (isec->output_section->name, ".init") == 0
13119 || strcmp (isec->output_section->name, ".fini") == 0))
13121 if (isec->map_head.s->has_toc_reloc
13122 || isec->map_head.s->makes_toc_func_call)
13123 ret = 1;
13124 else if (!isec->map_head.s->call_check_done)
13126 int recur;
13127 isec->call_check_in_progress = 1;
13128 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13129 isec->call_check_in_progress = 0;
13130 if (recur != 0)
13131 ret = recur;
13135 if (ret == 1)
13136 isec->makes_toc_func_call = 1;
13138 return ret;
13141 /* The linker repeatedly calls this function for each input section,
13142 in the order that input sections are linked into output sections.
13143 Build lists of input sections to determine groupings between which
13144 we may insert linker stubs. */
13146 bool
13147 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13149 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13151 if (htab == NULL)
13152 return false;
13154 if ((isec->output_section->flags & SEC_CODE) != 0
13155 && isec->output_section->id < htab->sec_info_arr_size)
13157 /* This happens to make the list in reverse order,
13158 which is what we want. */
13159 htab->sec_info[isec->id].u.list
13160 = htab->sec_info[isec->output_section->id].u.list;
13161 htab->sec_info[isec->output_section->id].u.list = isec;
13164 if (htab->multi_toc_needed)
13166 /* Analyse sections that aren't already flagged as needing a
13167 valid toc pointer. Exclude .fixup for the linux kernel.
13168 .fixup contains branches, but only back to the function that
13169 hit an exception. */
13170 if (!(isec->has_toc_reloc
13171 || (isec->flags & SEC_CODE) == 0
13172 || strcmp (isec->name, ".fixup") == 0
13173 || isec->call_check_done))
13175 if (toc_adjusting_stub_needed (info, isec) < 0)
13176 return false;
13178 /* Make all sections use the TOC assigned for this object file.
13179 This will be wrong for pasted sections; We fix that in
13180 check_pasted_section(). */
13181 if (elf_gp (isec->owner) != 0)
13182 htab->toc_curr = elf_gp (isec->owner);
13185 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13186 return true;
13189 /* Check that all .init and .fini sections use the same toc, if they
13190 have toc relocs. */
13192 static bool
13193 check_pasted_section (struct bfd_link_info *info, const char *name)
13195 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13197 if (o != NULL)
13199 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13200 bfd_vma toc_off = 0;
13201 asection *i;
13203 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13204 if (i->has_toc_reloc)
13206 if (toc_off == 0)
13207 toc_off = htab->sec_info[i->id].toc_off;
13208 else if (toc_off != htab->sec_info[i->id].toc_off)
13209 return false;
13212 if (toc_off == 0)
13213 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13214 if (i->makes_toc_func_call)
13216 toc_off = htab->sec_info[i->id].toc_off;
13217 break;
13220 /* Make sure the whole pasted function uses the same toc offset. */
13221 if (toc_off != 0)
13222 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13223 htab->sec_info[i->id].toc_off = toc_off;
13225 return true;
13228 bool
13229 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13231 bool ret1 = check_pasted_section (info, ".init");
13232 bool ret2 = check_pasted_section (info, ".fini");
13234 return ret1 && ret2;
13237 /* See whether we can group stub sections together. Grouping stub
13238 sections may result in fewer stubs. More importantly, we need to
13239 put all .init* and .fini* stubs at the beginning of the .init or
13240 .fini output sections respectively, because glibc splits the
13241 _init and _fini functions into multiple parts. Putting a stub in
13242 the middle of a function is not a good idea. */
13244 static bool
13245 group_sections (struct bfd_link_info *info,
13246 bfd_size_type stub_group_size,
13247 bool stubs_always_before_branch)
13249 struct ppc_link_hash_table *htab;
13250 asection *osec;
13251 bool suppress_size_errors;
13253 htab = ppc_hash_table (info);
13254 if (htab == NULL)
13255 return false;
13257 suppress_size_errors = false;
13258 if (stub_group_size == 1)
13260 /* Default values. */
13261 if (stubs_always_before_branch)
13262 stub_group_size = 0x1e00000;
13263 else
13264 stub_group_size = 0x1c00000;
13265 suppress_size_errors = true;
13268 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13270 asection *tail;
13272 if (osec->id >= htab->sec_info_arr_size)
13273 continue;
13275 tail = htab->sec_info[osec->id].u.list;
13276 while (tail != NULL)
13278 asection *curr;
13279 asection *prev;
13280 bfd_size_type total;
13281 bool big_sec;
13282 bfd_vma curr_toc;
13283 struct map_stub *group;
13284 bfd_size_type group_size;
13286 curr = tail;
13287 total = tail->size;
13288 group_size = (ppc64_elf_section_data (tail) != NULL
13289 && ppc64_elf_section_data (tail)->has_14bit_branch
13290 ? stub_group_size >> 10 : stub_group_size);
13292 big_sec = total > group_size;
13293 if (big_sec && !suppress_size_errors)
13294 /* xgettext:c-format */
13295 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13296 tail->owner, tail);
13297 curr_toc = htab->sec_info[tail->id].toc_off;
13299 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13300 && ((total += curr->output_offset - prev->output_offset)
13301 < (ppc64_elf_section_data (prev) != NULL
13302 && ppc64_elf_section_data (prev)->has_14bit_branch
13303 ? (group_size = stub_group_size >> 10) : group_size))
13304 && htab->sec_info[prev->id].toc_off == curr_toc)
13305 curr = prev;
13307 /* OK, the size from the start of CURR to the end is less
13308 than group_size and thus can be handled by one stub
13309 section. (or the tail section is itself larger than
13310 group_size, in which case we may be toast.) We should
13311 really be keeping track of the total size of stubs added
13312 here, as stubs contribute to the final output section
13313 size. That's a little tricky, and this way will only
13314 break if stubs added make the total size more than 2^25,
13315 ie. for the default stub_group_size, if stubs total more
13316 than 2097152 bytes, or nearly 75000 plt call stubs. */
13317 group = bfd_alloc (curr->owner, sizeof (*group));
13318 if (group == NULL)
13319 return false;
13320 group->link_sec = curr;
13321 group->stub_sec = NULL;
13322 group->needs_save_res = 0;
13323 group->lr_restore = 0;
13324 group->eh_size = 0;
13325 group->eh_base = 0;
13326 group->next = htab->group;
13327 htab->group = group;
13330 prev = htab->sec_info[tail->id].u.list;
13331 /* Set up this stub group. */
13332 htab->sec_info[tail->id].u.group = group;
13334 while (tail != curr && (tail = prev) != NULL);
13336 /* But wait, there's more! Input sections up to group_size
13337 bytes before the stub section can be handled by it too.
13338 Don't do this if we have a really large section after the
13339 stubs, as adding more stubs increases the chance that
13340 branches may not reach into the stub section. */
13341 if (!stubs_always_before_branch && !big_sec)
13343 total = 0;
13344 while (prev != NULL
13345 && ((total += tail->output_offset - prev->output_offset)
13346 < (ppc64_elf_section_data (prev) != NULL
13347 && ppc64_elf_section_data (prev)->has_14bit_branch
13348 ? (group_size = stub_group_size >> 10)
13349 : group_size))
13350 && htab->sec_info[prev->id].toc_off == curr_toc)
13352 tail = prev;
13353 prev = htab->sec_info[tail->id].u.list;
13354 htab->sec_info[tail->id].u.group = group;
13357 tail = prev;
13360 return true;
13363 static const unsigned char glink_eh_frame_cie[] =
13365 0, 0, 0, 16, /* length. */
13366 0, 0, 0, 0, /* id. */
13367 1, /* CIE version. */
13368 'z', 'R', 0, /* Augmentation string. */
13369 4, /* Code alignment. */
13370 0x78, /* Data alignment. */
13371 65, /* RA reg. */
13372 1, /* Augmentation size. */
13373 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13374 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13377 /* Stripping output sections is normally done before dynamic section
13378 symbols have been allocated. This function is called later, and
13379 handles cases like htab->brlt which is mapped to its own output
13380 section. */
13382 static void
13383 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13385 if (isec->size == 0
13386 && isec->output_section->size == 0
13387 && !(isec->output_section->flags & SEC_KEEP)
13388 && !bfd_section_removed_from_list (info->output_bfd,
13389 isec->output_section)
13390 && elf_section_data (isec->output_section)->dynindx == 0)
13392 isec->output_section->flags |= SEC_EXCLUDE;
13393 bfd_section_list_remove (info->output_bfd, isec->output_section);
13394 info->output_bfd->section_count--;
13398 /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13399 the array of such relocs. */
13401 static bool
13402 append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13404 if (htab->relr_count >= htab->relr_alloc)
13406 if (htab->relr_alloc == 0)
13407 htab->relr_alloc = 4096;
13408 else
13409 htab->relr_alloc *= 2;
13410 htab->relr = bfd_realloc (htab->relr,
13411 htab->relr_alloc * sizeof (*htab->relr));
13412 if (htab->relr == NULL)
13413 return false;
13415 htab->relr[htab->relr_count].sec = sec;
13416 htab->relr[htab->relr_count].off = off;
13417 htab->relr_count++;
13418 return true;
13421 /* qsort comparator for bfd_vma args. */
13423 static int
13424 compare_relr_address (const void *arg1, const void *arg2)
13426 bfd_vma a = *(bfd_vma *) arg1;
13427 bfd_vma b = *(bfd_vma *) arg2;
13428 return a < b ? -1 : a > b ? 1 : 0;
13431 /* Produce a malloc'd sorted array of reloc addresses from the info
13432 stored by append_relr_off. */
13434 static bfd_vma *
13435 sort_relr (struct ppc_link_hash_table *htab)
13437 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13438 if (addr == NULL)
13439 return NULL;
13441 for (size_t i = 0; i < htab->relr_count; i++)
13442 addr[i] = (htab->relr[i].sec->output_section->vma
13443 + htab->relr[i].sec->output_offset
13444 + htab->relr[i].off);
13446 if (htab->relr_count > 1)
13447 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13449 return addr;
13452 /* Look over GOT and PLT entries saved on elf_local_got_ents for all
13453 input files, stashing info about needed relative relocs. */
13455 static bool
13456 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13458 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13459 bfd *ibfd;
13461 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13463 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13464 struct plt_entry **local_plt, **lplt, **end_local_plt;
13465 Elf_Internal_Shdr *symtab_hdr;
13466 bfd_size_type locsymcount;
13467 Elf_Internal_Sym *local_syms;
13468 Elf_Internal_Sym *isym;
13469 struct plt_entry *pent;
13470 struct got_entry *gent;
13472 if (!is_ppc64_elf (ibfd))
13473 continue;
13475 lgot_ents = elf_local_got_ents (ibfd);
13476 if (!lgot_ents)
13477 continue;
13479 symtab_hdr = &elf_symtab_hdr (ibfd);
13480 locsymcount = symtab_hdr->sh_info;
13481 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13482 if (local_syms == NULL && locsymcount != 0)
13484 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13485 0, NULL, NULL, NULL);
13486 if (local_syms == NULL)
13487 return false;
13489 end_lgot_ents = lgot_ents + locsymcount;
13490 local_plt = (struct plt_entry **) end_lgot_ents;
13491 end_local_plt = local_plt + locsymcount;
13492 for (lgot = lgot_ents, isym = local_syms;
13493 lgot < end_lgot_ents;
13494 ++lgot, ++isym)
13495 for (gent = *lgot; gent != NULL; gent = gent->next)
13496 if (!gent->is_indirect
13497 && gent->tls_type == 0
13498 && gent->got.offset != (bfd_vma) -1
13499 && isym->st_shndx != SHN_ABS)
13501 asection *got = ppc64_elf_tdata (gent->owner)->got;
13502 if (!append_relr_off (htab, got, gent->got.offset))
13504 htab->stub_error = true;
13505 return false;
13509 if (!htab->opd_abi)
13510 for (lplt = local_plt, isym = local_syms;
13511 lplt < end_local_plt;
13512 ++lplt, ++isym)
13513 for (pent = *lplt; pent != NULL; pent = pent->next)
13514 if (pent->plt.offset != (bfd_vma) -1
13515 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13517 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13519 if (symtab_hdr->contents != (unsigned char *) local_syms)
13520 free (local_syms);
13521 return false;
13525 if (local_syms != NULL
13526 && symtab_hdr->contents != (unsigned char *) local_syms)
13528 if (!info->keep_memory)
13529 free (local_syms);
13530 else
13531 symtab_hdr->contents = (unsigned char *) local_syms;
13534 return true;
13537 /* Stash info about needed GOT and PLT entry relative relocs for
13538 global symbol H. */
13540 static bool
13541 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13543 struct bfd_link_info *info;
13544 struct ppc_link_hash_table *htab;
13545 struct plt_entry *pent;
13546 struct got_entry *gent;
13548 if (h->root.type == bfd_link_hash_indirect)
13549 return true;
13551 info = (struct bfd_link_info *) inf;
13552 htab = ppc_hash_table (info);
13553 if (htab == NULL)
13554 return false;
13556 if (h->type != STT_GNU_IFUNC
13557 && h->def_regular
13558 && (h->root.type == bfd_link_hash_defined
13559 || h->root.type == bfd_link_hash_defweak))
13561 if ((!htab->elf.dynamic_sections_created
13562 || h->dynindx == -1
13563 || SYMBOL_REFERENCES_LOCAL (info, h))
13564 && !bfd_is_abs_symbol (&h->root))
13565 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13566 if (!gent->is_indirect
13567 && gent->tls_type == 0
13568 && gent->got.offset != (bfd_vma) -1)
13570 asection *got = ppc64_elf_tdata (gent->owner)->got;
13571 if (!append_relr_off (htab, got, gent->got.offset))
13573 htab->stub_error = true;
13574 return false;
13578 if (!htab->opd_abi
13579 && use_local_plt (info, h))
13580 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13581 if (pent->plt.offset != (bfd_vma) -1)
13583 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13585 htab->stub_error = true;
13586 return false;
13590 return true;
13593 /* Determine and set the size of the stub section for a final link.
13595 The basic idea here is to examine all the relocations looking for
13596 PC-relative calls to a target that is unreachable with a "bl"
13597 instruction. */
13599 bool
13600 ppc64_elf_size_stubs (struct bfd_link_info *info)
13602 bfd_size_type stub_group_size;
13603 bool stubs_always_before_branch;
13604 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13606 if (htab == NULL)
13607 return false;
13609 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13610 htab->params->power10_stubs = 0;
13612 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13613 htab->params->plt_thread_safe = 1;
13614 if (!htab->opd_abi)
13615 htab->params->plt_thread_safe = 0;
13616 else if (htab->params->plt_thread_safe == -1)
13618 static const char *const thread_starter[] =
13620 "pthread_create",
13621 /* libstdc++ */
13622 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13623 /* librt */
13624 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13625 "mq_notify", "create_timer",
13626 /* libanl */
13627 "getaddrinfo_a",
13628 /* libgomp */
13629 "GOMP_parallel",
13630 "GOMP_parallel_start",
13631 "GOMP_parallel_loop_static",
13632 "GOMP_parallel_loop_static_start",
13633 "GOMP_parallel_loop_dynamic",
13634 "GOMP_parallel_loop_dynamic_start",
13635 "GOMP_parallel_loop_guided",
13636 "GOMP_parallel_loop_guided_start",
13637 "GOMP_parallel_loop_runtime",
13638 "GOMP_parallel_loop_runtime_start",
13639 "GOMP_parallel_sections",
13640 "GOMP_parallel_sections_start",
13641 /* libgo */
13642 "__go_go",
13644 unsigned i;
13646 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13648 struct elf_link_hash_entry *h;
13649 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13650 false, false, true);
13651 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13652 if (htab->params->plt_thread_safe)
13653 break;
13656 stubs_always_before_branch = htab->params->group_size < 0;
13657 if (htab->params->group_size < 0)
13658 stub_group_size = -htab->params->group_size;
13659 else
13660 stub_group_size = htab->params->group_size;
13662 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13663 return false;
13665 htab->tga_group = NULL;
13666 if (!htab->params->no_tls_get_addr_regsave
13667 && htab->tga_desc_fd != NULL
13668 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13669 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13670 && htab->tls_get_addr_fd != NULL
13671 && is_static_defined (&htab->tls_get_addr_fd->elf))
13673 asection *sym_sec, *code_sec, *stub_sec;
13674 bfd_vma sym_value;
13675 struct _opd_sec_data *opd;
13677 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13678 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13679 code_sec = sym_sec;
13680 opd = get_opd_info (sym_sec);
13681 if (opd != NULL)
13682 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13683 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13684 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13685 htab->tga_group->link_sec);
13686 if (stub_sec == NULL)
13687 return false;
13688 htab->tga_group->stub_sec = stub_sec;
13690 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13691 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13692 htab->tga_desc_fd->elf.root.u.def.value = 0;
13693 htab->tga_desc_fd->elf.type = STT_FUNC;
13694 htab->tga_desc_fd->elf.def_regular = 1;
13695 htab->tga_desc_fd->elf.non_elf = 0;
13696 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13699 /* Loop until no stubs added. After iteration 20 of this loop we may
13700 exit on a stub section shrinking. */
13702 while (1)
13704 bfd *input_bfd;
13705 unsigned int bfd_indx;
13706 struct map_stub *group;
13708 htab->stub_iteration += 1;
13709 htab->relr_count = 0;
13711 for (input_bfd = info->input_bfds, bfd_indx = 0;
13712 input_bfd != NULL;
13713 input_bfd = input_bfd->link.next, bfd_indx++)
13715 Elf_Internal_Shdr *symtab_hdr;
13716 asection *section;
13717 Elf_Internal_Sym *local_syms = NULL;
13719 if (!is_ppc64_elf (input_bfd))
13720 continue;
13722 /* We'll need the symbol table in a second. */
13723 symtab_hdr = &elf_symtab_hdr (input_bfd);
13724 if (symtab_hdr->sh_info == 0)
13725 continue;
13727 /* Walk over each section attached to the input bfd. */
13728 for (section = input_bfd->sections;
13729 section != NULL;
13730 section = section->next)
13732 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13733 bool is_opd;
13735 /* If there aren't any relocs, then there's nothing more
13736 to do. */
13737 if ((section->flags & SEC_RELOC) == 0
13738 || (section->flags & SEC_ALLOC) == 0
13739 || (section->flags & SEC_LOAD) == 0
13740 || section->reloc_count == 0)
13741 continue;
13743 if (!info->enable_dt_relr
13744 && (section->flags & SEC_CODE) == 0)
13745 continue;
13747 /* If this section is a link-once section that will be
13748 discarded, then don't create any stubs. */
13749 if (section->output_section == NULL
13750 || section->output_section->owner != info->output_bfd)
13751 continue;
13753 /* Get the relocs. */
13754 internal_relocs
13755 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13756 info->keep_memory);
13757 if (internal_relocs == NULL)
13758 goto error_ret_free_local;
13760 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13762 /* Now examine each relocation. */
13763 irela = internal_relocs;
13764 irelaend = irela + section->reloc_count;
13765 for (; irela < irelaend; irela++)
13767 enum elf_ppc64_reloc_type r_type;
13768 unsigned int r_indx;
13769 struct ppc_stub_type stub_type;
13770 struct ppc_stub_hash_entry *stub_entry;
13771 asection *sym_sec, *code_sec;
13772 bfd_vma sym_value, code_value;
13773 bfd_vma destination;
13774 unsigned long local_off;
13775 bool ok_dest;
13776 struct ppc_link_hash_entry *hash;
13777 struct ppc_link_hash_entry *fdh;
13778 struct elf_link_hash_entry *h;
13779 Elf_Internal_Sym *sym;
13780 char *stub_name;
13781 const asection *id_sec;
13782 struct _opd_sec_data *opd;
13783 struct plt_entry *plt_ent;
13785 r_type = ELF64_R_TYPE (irela->r_info);
13786 r_indx = ELF64_R_SYM (irela->r_info);
13788 if (r_type >= R_PPC64_max)
13790 bfd_set_error (bfd_error_bad_value);
13791 goto error_ret_free_internal;
13794 /* Only look for stubs on branch instructions. */
13795 switch (r_type)
13797 default:
13798 continue;
13800 case R_PPC64_REL24:
13801 case R_PPC64_REL24_NOTOC:
13802 case R_PPC64_REL24_P9NOTOC:
13803 case R_PPC64_REL14:
13804 case R_PPC64_REL14_BRTAKEN:
13805 case R_PPC64_REL14_BRNTAKEN:
13806 if ((section->flags & SEC_CODE) != 0)
13807 break;
13808 continue;
13810 case R_PPC64_ADDR64:
13811 case R_PPC64_TOC:
13812 if (info->enable_dt_relr
13813 && irela->r_offset % 2 == 0
13814 && section->alignment_power != 0)
13815 break;
13816 continue;
13819 /* Now determine the call target, its name, value,
13820 section. */
13821 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13822 r_indx, input_bfd))
13823 goto error_ret_free_internal;
13825 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13827 /* Only locally defined symbols can possibly use
13828 relative relocations. */
13829 bfd_vma r_offset;
13830 if ((sym_sec == NULL
13831 || sym_sec->output_section == NULL)
13832 /* No symbol is OK too. */
13833 && !(sym != NULL && sym->st_shndx == 0)
13834 /* Hack for __ehdr_start, which is undefined
13835 at this point. */
13836 && !(h != NULL && h->root.linker_def))
13837 continue;
13838 if (NO_OPD_RELOCS && is_opd)
13839 continue;
13840 if (!is_opd
13841 && r_type == R_PPC64_ADDR64)
13843 if (h != NULL
13844 ? h->type == STT_GNU_IFUNC
13845 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13846 continue;
13847 if (h != NULL
13848 ? bfd_is_abs_symbol (&h->root)
13849 : sym->st_shndx == SHN_ABS)
13850 continue;
13851 if (h != NULL
13852 && !SYMBOL_REFERENCES_LOCAL (info, h))
13853 continue;
13855 r_offset = _bfd_elf_section_offset (info->output_bfd,
13856 info,
13857 section,
13858 irela->r_offset);
13859 if (r_offset >= (bfd_vma) -2)
13860 continue;
13861 if (!append_relr_off (htab, section, r_offset))
13862 goto error_ret_free_internal;
13863 continue;
13866 hash = ppc_elf_hash_entry (h);
13867 ok_dest = false;
13868 fdh = NULL;
13869 sym_value = 0;
13870 if (hash == NULL)
13872 sym_value = sym->st_value;
13873 if (sym_sec != NULL
13874 && sym_sec->output_section != NULL)
13875 ok_dest = true;
13877 else if (hash->elf.root.type == bfd_link_hash_defined
13878 || hash->elf.root.type == bfd_link_hash_defweak)
13880 sym_value = hash->elf.root.u.def.value;
13881 if (sym_sec->output_section != NULL)
13882 ok_dest = true;
13884 else if (hash->elf.root.type == bfd_link_hash_undefweak
13885 || hash->elf.root.type == bfd_link_hash_undefined)
13887 /* Recognise an old ABI func code entry sym, and
13888 use the func descriptor sym instead if it is
13889 defined. */
13890 if (hash->elf.root.root.string[0] == '.'
13891 && hash->oh != NULL)
13893 fdh = ppc_follow_link (hash->oh);
13894 if (fdh->elf.root.type == bfd_link_hash_defined
13895 || fdh->elf.root.type == bfd_link_hash_defweak)
13897 sym_sec = fdh->elf.root.u.def.section;
13898 sym_value = fdh->elf.root.u.def.value;
13899 if (sym_sec->output_section != NULL)
13900 ok_dest = true;
13902 else
13903 fdh = NULL;
13906 else
13908 bfd_set_error (bfd_error_bad_value);
13909 goto error_ret_free_internal;
13912 destination = 0;
13913 local_off = 0;
13914 if (ok_dest)
13916 sym_value += irela->r_addend;
13917 destination = (sym_value
13918 + sym_sec->output_offset
13919 + sym_sec->output_section->vma);
13920 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13921 ? hash->elf.other
13922 : sym->st_other);
13925 code_sec = sym_sec;
13926 code_value = sym_value;
13927 opd = get_opd_info (sym_sec);
13928 if (opd != NULL)
13930 bfd_vma dest;
13932 if (hash == NULL && opd->adjust != NULL)
13934 long adjust = opd->adjust[OPD_NDX (sym_value)];
13935 if (adjust == -1)
13936 continue;
13937 code_value += adjust;
13938 sym_value += adjust;
13940 dest = opd_entry_value (sym_sec, sym_value,
13941 &code_sec, &code_value, false);
13942 if (dest != (bfd_vma) -1)
13944 destination = dest;
13945 if (fdh != NULL)
13947 /* Fixup old ABI sym to point at code
13948 entry. */
13949 hash->elf.root.type = bfd_link_hash_defweak;
13950 hash->elf.root.u.def.section = code_sec;
13951 hash->elf.root.u.def.value = code_value;
13956 /* Determine what (if any) linker stub is needed. */
13957 plt_ent = NULL;
13958 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13959 &plt_ent, destination,
13960 local_off);
13961 stub_type.sub = ppc_stub_toc;
13962 stub_type.r2save = 0;
13964 if (r_type == R_PPC64_REL24_NOTOC
13965 || r_type == R_PPC64_REL24_P9NOTOC)
13967 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13968 if (htab->params->power10_stubs == 0
13969 || (r_type == R_PPC64_REL24_P9NOTOC
13970 && htab->params->power10_stubs != 1))
13971 notoc = ppc_stub_p9notoc;
13972 if (stub_type.main == ppc_stub_plt_call)
13973 stub_type.sub = notoc;
13974 else if (stub_type.main == ppc_stub_long_branch
13975 || (code_sec != NULL
13976 && code_sec->output_section != NULL
13977 && (((hash ? hash->elf.other : sym->st_other)
13978 & STO_PPC64_LOCAL_MASK)
13979 > 1 << STO_PPC64_LOCAL_BIT)))
13981 stub_type.main = ppc_stub_long_branch;
13982 stub_type.sub = notoc;
13983 stub_type.r2save = 0;
13986 else if (stub_type.main != ppc_stub_plt_call)
13988 /* Check whether we need a TOC adjusting stub.
13989 Since the linker pastes together pieces from
13990 different object files when creating the
13991 _init and _fini functions, it may be that a
13992 call to what looks like a local sym is in
13993 fact a call needing a TOC adjustment. */
13994 if ((code_sec != NULL
13995 && code_sec->output_section != NULL
13996 && (code_sec->has_toc_reloc
13997 || code_sec->makes_toc_func_call)
13998 && (htab->sec_info[code_sec->id].toc_off
13999 != htab->sec_info[section->id].toc_off))
14000 || (((hash ? hash->elf.other : sym->st_other)
14001 & STO_PPC64_LOCAL_MASK)
14002 == 1 << STO_PPC64_LOCAL_BIT))
14004 stub_type.main = ppc_stub_long_branch;
14005 stub_type.sub = ppc_stub_toc;
14006 stub_type.r2save = 1;
14010 if (stub_type.main == ppc_stub_none)
14011 continue;
14013 /* __tls_get_addr calls might be eliminated. */
14014 if (stub_type.main != ppc_stub_plt_call
14015 && hash != NULL
14016 && is_tls_get_addr (&hash->elf, htab)
14017 && section->has_tls_reloc
14018 && irela != internal_relocs)
14020 /* Get tls info. */
14021 unsigned char *tls_mask;
14023 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14024 irela - 1, input_bfd))
14025 goto error_ret_free_internal;
14026 if ((*tls_mask & TLS_TLS) != 0
14027 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14028 continue;
14031 if (stub_type.main == ppc_stub_plt_call
14032 && stub_type.sub == ppc_stub_toc)
14034 if (!htab->opd_abi
14035 && htab->params->plt_localentry0 != 0
14036 && is_elfv2_localentry0 (&hash->elf))
14037 htab->has_plt_localentry0 = 1;
14038 else if (irela + 1 < irelaend
14039 && irela[1].r_offset == irela->r_offset + 4
14040 && (ELF64_R_TYPE (irela[1].r_info)
14041 == R_PPC64_TOCSAVE))
14043 if (!tocsave_find (htab, INSERT,
14044 &local_syms, irela + 1, input_bfd))
14045 goto error_ret_free_internal;
14047 else
14048 stub_type.r2save = 1;
14051 /* Support for grouping stub sections. */
14052 id_sec = htab->sec_info[section->id].u.group->link_sec;
14054 /* Get the name of this stub. */
14055 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14056 if (!stub_name)
14057 goto error_ret_free_internal;
14059 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14060 stub_name, false, false);
14061 if (stub_entry != NULL)
14063 free (stub_name);
14064 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14066 /* xgettext:c-format */
14067 _bfd_error_handler
14068 (_("%pB: cannot create stub entry %s"),
14069 section->owner, stub_entry->root.string);
14070 goto error_ret_free_internal;
14072 continue;
14075 stub_entry = ppc_add_stub (stub_name, section, info);
14076 if (stub_entry == NULL)
14078 free (stub_name);
14079 error_ret_free_internal:
14080 if (elf_section_data (section)->relocs == NULL)
14081 free (internal_relocs);
14082 error_ret_free_local:
14083 if (symtab_hdr->contents
14084 != (unsigned char *) local_syms)
14085 free (local_syms);
14086 return false;
14089 stub_entry->type = stub_type;
14090 if (stub_type.main == ppc_stub_plt_call)
14092 stub_entry->target_value = sym_value;
14093 stub_entry->target_section = sym_sec;
14095 else
14097 stub_entry->target_value = code_value;
14098 stub_entry->target_section = code_sec;
14100 stub_entry->h = hash;
14101 stub_entry->plt_ent = plt_ent;
14102 stub_entry->symtype
14103 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14104 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14106 if (hash != NULL
14107 && (hash->elf.root.type == bfd_link_hash_defined
14108 || hash->elf.root.type == bfd_link_hash_defweak))
14109 htab->stub_globals += 1;
14112 /* We're done with the internal relocs, free them. */
14113 if (elf_section_data (section)->relocs != internal_relocs)
14114 free (internal_relocs);
14117 if (local_syms != NULL
14118 && symtab_hdr->contents != (unsigned char *) local_syms)
14120 if (!info->keep_memory)
14121 free (local_syms);
14122 else
14123 symtab_hdr->contents = (unsigned char *) local_syms;
14127 /* We may have added some stubs. Find out the new size of the
14128 stub sections. */
14129 for (group = htab->group; group != NULL; group = group->next)
14131 group->lr_restore = 0;
14132 group->eh_size = 0;
14133 if (group->stub_sec != NULL)
14135 asection *stub_sec = group->stub_sec;
14137 stub_sec->rawsize = stub_sec->size;
14138 stub_sec->size = 0;
14139 stub_sec->reloc_count = 0;
14140 stub_sec->flags &= ~SEC_RELOC;
14143 if (htab->tga_group != NULL)
14145 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14146 htab->tga_group->eh_size
14147 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14148 htab->tga_group->lr_restore = 23 * 4;
14149 htab->tga_group->stub_sec->size = 24 * 4;
14152 htab->brlt->rawsize = htab->brlt->size;
14153 htab->brlt->size = 0;
14154 htab->brlt->reloc_count = 0;
14155 htab->brlt->flags &= ~SEC_RELOC;
14156 if (htab->relbrlt != NULL)
14157 htab->relbrlt->size = 0;
14159 if (htab->elf.srelrdyn != NULL)
14161 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14162 htab->elf.srelrdyn->size = 0;
14165 htab->stub_changed = false;
14166 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14168 for (group = htab->group; group != NULL; group = group->next)
14169 if (group->needs_save_res)
14170 group->stub_sec->size += htab->sfpr->size;
14172 if (info->emitrelocations
14173 && htab->glink != NULL && htab->glink->size != 0)
14175 htab->glink->reloc_count = 1;
14176 htab->glink->flags |= SEC_RELOC;
14179 if (htab->glink_eh_frame != NULL
14180 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14181 && htab->glink_eh_frame->output_section->size > 8)
14183 size_t size = 0, align = 4;
14185 for (group = htab->group; group != NULL; group = group->next)
14186 if (group->eh_size != 0)
14187 size += (group->eh_size + 17 + align - 1) & -align;
14188 if (htab->glink != NULL && htab->glink->size != 0)
14189 size += (24 + align - 1) & -align;
14190 if (size != 0)
14191 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14192 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14193 size = (size + align - 1) & -align;
14194 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14195 htab->glink_eh_frame->size = size;
14198 if (htab->params->plt_stub_align != 0)
14199 for (group = htab->group; group != NULL; group = group->next)
14200 if (group->stub_sec != NULL)
14202 int align = abs (htab->params->plt_stub_align);
14203 group->stub_sec->size
14204 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14207 if (htab->elf.srelrdyn != NULL)
14209 bfd_vma r_offset;
14211 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14212 if (!append_relr_off (htab, htab->brlt, r_offset))
14213 return false;
14215 if (!got_and_plt_relr_for_local_syms (info))
14216 return false;
14217 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14218 if (htab->stub_error)
14219 return false;
14221 bfd_vma *relr_addr = sort_relr (htab);
14222 if (htab->relr_count != 0 && relr_addr == NULL)
14223 return false;
14225 size_t i = 0;
14226 while (i < htab->relr_count)
14228 bfd_vma base = relr_addr[i];
14229 htab->elf.srelrdyn->size += 8;
14230 i++;
14231 /* Handle possible duplicate address. This can happen
14232 as sections increase in size when adding stubs. */
14233 while (i < htab->relr_count
14234 && relr_addr[i] == base)
14235 i++;
14236 base += 8;
14237 while (1)
14239 size_t start_i = i;
14240 while (i < htab->relr_count
14241 && relr_addr[i] - base < 63 * 8
14242 && (relr_addr[i] - base) % 8 == 0)
14243 i++;
14244 if (i == start_i)
14245 break;
14246 htab->elf.srelrdyn->size += 8;
14247 base += 63 * 8;
14250 free (relr_addr);
14253 for (group = htab->group; group != NULL; group = group->next)
14254 if (group->stub_sec != NULL
14255 && group->stub_sec->rawsize != group->stub_sec->size
14256 && (htab->stub_iteration <= STUB_SHRINK_ITER
14257 || group->stub_sec->rawsize < group->stub_sec->size))
14258 break;
14260 if (group == NULL
14261 && (!htab->stub_changed
14262 || htab->stub_iteration > STUB_SHRINK_ITER)
14263 && (htab->brlt->rawsize == htab->brlt->size
14264 || (htab->stub_iteration > STUB_SHRINK_ITER
14265 && htab->brlt->rawsize > htab->brlt->size))
14266 && (htab->elf.srelrdyn == NULL
14267 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14268 || (htab->stub_iteration > STUB_SHRINK_ITER
14269 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14270 && (htab->glink_eh_frame == NULL
14271 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14272 && (htab->tga_group == NULL
14273 || htab->stub_iteration > 1))
14274 break;
14276 if (htab->stub_iteration > STUB_SHRINK_ITER)
14278 for (group = htab->group; group != NULL; group = group->next)
14279 if (group->stub_sec != NULL
14280 && group->stub_sec->size < group->stub_sec->rawsize)
14281 group->stub_sec->size = group->stub_sec->rawsize;
14283 if (htab->brlt->size < htab->brlt->rawsize)
14284 htab->brlt->size = htab->brlt->rawsize;
14286 if (htab->elf.srelrdyn != NULL
14287 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14288 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14291 /* Ask the linker to do its stuff. */
14292 (*htab->params->layout_sections_again) ();
14295 if (htab->glink_eh_frame != NULL
14296 && htab->glink_eh_frame->size != 0)
14298 bfd_vma val;
14299 bfd_byte *p, *last_fde;
14300 size_t last_fde_len, size, align, pad;
14301 struct map_stub *group;
14303 /* It is necessary to at least have a rough outline of the
14304 linker generated CIEs and FDEs written before
14305 bfd_elf_discard_info is run, in order for these FDEs to be
14306 indexed in .eh_frame_hdr. */
14307 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14308 if (p == NULL)
14309 return false;
14310 htab->glink_eh_frame->contents = p;
14311 last_fde = p;
14312 align = 4;
14314 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14315 /* CIE length (rewrite in case little-endian). */
14316 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14317 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14318 p += last_fde_len + 4;
14320 for (group = htab->group; group != NULL; group = group->next)
14321 if (group->eh_size != 0)
14323 group->eh_base = p - htab->glink_eh_frame->contents;
14324 last_fde = p;
14325 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14326 /* FDE length. */
14327 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14328 p += 4;
14329 /* CIE pointer. */
14330 val = p - htab->glink_eh_frame->contents;
14331 bfd_put_32 (htab->elf.dynobj, val, p);
14332 p += 4;
14333 /* Offset to stub section, written later. */
14334 p += 4;
14335 /* stub section size. */
14336 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14337 p += 4;
14338 /* Augmentation. */
14339 p += 1;
14340 /* Make sure we don't have all nops. This is enough for
14341 elf-eh-frame.c to detect the last non-nop opcode. */
14342 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14343 p = last_fde + last_fde_len + 4;
14345 if (htab->glink != NULL && htab->glink->size != 0)
14347 last_fde = p;
14348 last_fde_len = ((24 + align - 1) & -align) - 4;
14349 /* FDE length. */
14350 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14351 p += 4;
14352 /* CIE pointer. */
14353 val = p - htab->glink_eh_frame->contents;
14354 bfd_put_32 (htab->elf.dynobj, val, p);
14355 p += 4;
14356 /* Offset to .glink, written later. */
14357 p += 4;
14358 /* .glink size. */
14359 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14360 p += 4;
14361 /* Augmentation. */
14362 p += 1;
14364 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14365 *p++ = DW_CFA_register;
14366 *p++ = 65;
14367 *p++ = htab->opd_abi ? 12 : 0;
14368 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14369 *p++ = DW_CFA_restore_extended;
14370 *p++ = 65;
14371 p += ((24 + align - 1) & -align) - 24;
14373 /* Subsume any padding into the last FDE if user .eh_frame
14374 sections are aligned more than glink_eh_frame. Otherwise any
14375 zero padding will be seen as a terminator. */
14376 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14377 size = p - htab->glink_eh_frame->contents;
14378 pad = ((size + align - 1) & -align) - size;
14379 htab->glink_eh_frame->size = size + pad;
14380 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14383 maybe_strip_output (info, htab->brlt);
14384 if (htab->relbrlt != NULL)
14385 maybe_strip_output (info, htab->relbrlt);
14386 if (htab->glink_eh_frame != NULL)
14387 maybe_strip_output (info, htab->glink_eh_frame);
14388 if (htab->elf.srelrdyn != NULL)
14389 maybe_strip_output (info, htab->elf.srelrdyn);
14391 return true;
14394 /* Called after we have determined section placement. If sections
14395 move, we'll be called again. Provide a value for TOCstart. */
14397 bfd_vma
14398 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14400 asection *s;
14401 bfd_vma TOCstart, adjust;
14403 if (info != NULL)
14405 struct elf_link_hash_entry *h;
14406 struct elf_link_hash_table *htab = elf_hash_table (info);
14408 if (is_elf_hash_table (&htab->root)
14409 && htab->hgot != NULL)
14410 h = htab->hgot;
14411 else
14413 h = (struct elf_link_hash_entry *)
14414 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14415 if (is_elf_hash_table (&htab->root))
14416 htab->hgot = h;
14418 if (h != NULL
14419 && h->root.type == bfd_link_hash_defined
14420 && !h->root.linker_def
14421 && (!is_elf_hash_table (&htab->root)
14422 || h->def_regular))
14424 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14425 _bfd_set_gp_value (obfd, TOCstart);
14426 return TOCstart;
14430 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14431 order. The TOC starts where the first of these sections starts. */
14432 s = bfd_get_section_by_name (obfd, ".got");
14433 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14434 s = bfd_get_section_by_name (obfd, ".toc");
14435 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14436 s = bfd_get_section_by_name (obfd, ".tocbss");
14437 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14438 s = bfd_get_section_by_name (obfd, ".plt");
14439 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14441 /* This may happen for
14442 o references to TOC base (SYM@toc / TOC[tc0]) without a
14443 .toc directive
14444 o bad linker script
14445 o --gc-sections and empty TOC sections
14447 FIXME: Warn user? */
14449 /* Look for a likely section. We probably won't even be
14450 using TOCstart. */
14451 for (s = obfd->sections; s != NULL; s = s->next)
14452 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14453 | SEC_EXCLUDE))
14454 == (SEC_ALLOC | SEC_SMALL_DATA))
14455 break;
14456 if (s == NULL)
14457 for (s = obfd->sections; s != NULL; s = s->next)
14458 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14459 == (SEC_ALLOC | SEC_SMALL_DATA))
14460 break;
14461 if (s == NULL)
14462 for (s = obfd->sections; s != NULL; s = s->next)
14463 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14464 == SEC_ALLOC)
14465 break;
14466 if (s == NULL)
14467 for (s = obfd->sections; s != NULL; s = s->next)
14468 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14469 break;
14472 TOCstart = 0;
14473 if (s != NULL)
14474 TOCstart = s->output_section->vma + s->output_offset;
14476 /* Force alignment. */
14477 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14478 TOCstart -= adjust;
14479 _bfd_set_gp_value (obfd, TOCstart);
14481 if (info != NULL && s != NULL)
14483 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14485 if (htab != NULL)
14487 if (htab->elf.hgot != NULL)
14489 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14490 htab->elf.hgot->root.u.def.section = s;
14493 else
14495 struct bfd_link_hash_entry *bh = NULL;
14496 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14497 s, TOC_BASE_OFF - adjust,
14498 NULL, false, false, &bh);
14501 return TOCstart;
14504 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14505 write out any global entry stubs, and PLT relocations. */
14507 static bool
14508 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14510 struct bfd_link_info *info;
14511 struct ppc_link_hash_table *htab;
14512 struct plt_entry *ent;
14513 asection *s;
14515 if (h->root.type == bfd_link_hash_indirect)
14516 return true;
14518 info = inf;
14519 htab = ppc_hash_table (info);
14520 if (htab == NULL)
14521 return false;
14523 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14524 if (ent->plt.offset != (bfd_vma) -1)
14526 /* This symbol has an entry in the procedure linkage
14527 table. Set it up. */
14528 Elf_Internal_Rela rela;
14529 asection *plt, *relplt;
14530 bfd_byte *loc;
14532 if (use_local_plt (info, h))
14534 if (!(h->def_regular
14535 && (h->root.type == bfd_link_hash_defined
14536 || h->root.type == bfd_link_hash_defweak)))
14537 continue;
14538 if (h->type == STT_GNU_IFUNC)
14540 plt = htab->elf.iplt;
14541 relplt = htab->elf.irelplt;
14542 htab->elf.ifunc_resolvers = true;
14543 if (htab->opd_abi)
14544 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14545 else
14546 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14548 else
14550 plt = htab->pltlocal;
14551 relplt = NULL;
14552 if (bfd_link_pic (info)
14553 && !(info->enable_dt_relr && !htab->opd_abi))
14555 relplt = htab->relpltlocal;
14556 if (htab->opd_abi)
14557 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14558 else
14559 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14562 rela.r_addend = defined_sym_val (h) + ent->addend;
14564 if (relplt == NULL)
14566 loc = plt->contents + ent->plt.offset;
14567 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14568 if (htab->opd_abi)
14570 bfd_vma toc = elf_gp (info->output_bfd);
14571 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14572 bfd_put_64 (info->output_bfd, toc, loc + 8);
14575 else
14577 rela.r_offset = (plt->output_section->vma
14578 + plt->output_offset
14579 + ent->plt.offset);
14580 loc = relplt->contents + (relplt->reloc_count++
14581 * sizeof (Elf64_External_Rela));
14582 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14585 else
14587 rela.r_offset = (htab->elf.splt->output_section->vma
14588 + htab->elf.splt->output_offset
14589 + ent->plt.offset);
14590 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14591 rela.r_addend = ent->addend;
14592 loc = (htab->elf.srelplt->contents
14593 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14594 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14595 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14596 htab->elf.ifunc_resolvers = true;
14597 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14601 if (!h->pointer_equality_needed)
14602 return true;
14604 if (h->def_regular)
14605 return true;
14607 s = htab->global_entry;
14608 if (s == NULL || s->size == 0)
14609 return true;
14611 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14612 if (ent->plt.offset != (bfd_vma) -1
14613 && ent->addend == 0)
14615 bfd_byte *p;
14616 asection *plt;
14617 bfd_vma off;
14619 p = s->contents + h->root.u.def.value;
14620 plt = htab->elf.splt;
14621 if (use_local_plt (info, h))
14623 if (h->type == STT_GNU_IFUNC)
14624 plt = htab->elf.iplt;
14625 else
14626 plt = htab->pltlocal;
14628 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14629 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14631 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14633 info->callbacks->einfo
14634 (_("%P: linkage table error against `%pT'\n"),
14635 h->root.root.string);
14636 bfd_set_error (bfd_error_bad_value);
14637 htab->stub_error = true;
14640 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14641 if (htab->params->emit_stub_syms)
14643 size_t len = strlen (h->root.root.string);
14644 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14646 if (name == NULL)
14647 return false;
14649 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14650 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14651 if (h == NULL)
14652 return false;
14653 if (h->root.type == bfd_link_hash_new)
14655 h->root.type = bfd_link_hash_defined;
14656 h->root.u.def.section = s;
14657 h->root.u.def.value = p - s->contents;
14658 h->ref_regular = 1;
14659 h->def_regular = 1;
14660 h->ref_regular_nonweak = 1;
14661 h->forced_local = 1;
14662 h->non_elf = 0;
14663 h->root.linker_def = 1;
14667 if (PPC_HA (off) != 0)
14669 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14670 p += 4;
14672 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14673 p += 4;
14674 bfd_put_32 (s->owner, MTCTR_R12, p);
14675 p += 4;
14676 bfd_put_32 (s->owner, BCTR, p);
14677 break;
14679 return true;
14682 /* Write PLT relocs for locals. */
14684 static bool
14685 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14687 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14688 bfd *ibfd;
14690 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14692 struct got_entry **lgot_ents, **end_lgot_ents;
14693 struct plt_entry **local_plt, **lplt, **end_local_plt;
14694 Elf_Internal_Shdr *symtab_hdr;
14695 bfd_size_type locsymcount;
14696 Elf_Internal_Sym *local_syms = NULL;
14697 struct plt_entry *ent;
14699 if (!is_ppc64_elf (ibfd))
14700 continue;
14702 lgot_ents = elf_local_got_ents (ibfd);
14703 if (!lgot_ents)
14704 continue;
14706 symtab_hdr = &elf_symtab_hdr (ibfd);
14707 locsymcount = symtab_hdr->sh_info;
14708 end_lgot_ents = lgot_ents + locsymcount;
14709 local_plt = (struct plt_entry **) end_lgot_ents;
14710 end_local_plt = local_plt + locsymcount;
14711 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14712 for (ent = *lplt; ent != NULL; ent = ent->next)
14713 if (ent->plt.offset != (bfd_vma) -1)
14715 Elf_Internal_Sym *sym;
14716 asection *sym_sec;
14717 asection *plt, *relplt;
14718 bfd_byte *loc;
14719 bfd_vma val;
14721 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14722 lplt - local_plt, ibfd))
14724 if (symtab_hdr->contents != (unsigned char *) local_syms)
14725 free (local_syms);
14726 return false;
14729 val = sym->st_value + ent->addend;
14730 if (sym_sec != NULL && sym_sec->output_section != NULL)
14731 val += sym_sec->output_offset + sym_sec->output_section->vma;
14733 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14735 htab->elf.ifunc_resolvers = true;
14736 plt = htab->elf.iplt;
14737 relplt = htab->elf.irelplt;
14739 else
14741 plt = htab->pltlocal;
14742 relplt = NULL;
14743 if (bfd_link_pic (info)
14744 && !(info->enable_dt_relr && !htab->opd_abi))
14745 relplt = htab->relpltlocal;
14748 if (relplt == NULL)
14750 loc = plt->contents + ent->plt.offset;
14751 bfd_put_64 (info->output_bfd, val, loc);
14752 if (htab->opd_abi)
14754 bfd_vma toc = elf_gp (ibfd);
14755 bfd_put_64 (info->output_bfd, toc, loc + 8);
14758 else
14760 Elf_Internal_Rela rela;
14761 rela.r_offset = (ent->plt.offset
14762 + plt->output_offset
14763 + plt->output_section->vma);
14764 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14766 if (htab->opd_abi)
14767 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14768 else
14769 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14771 else
14773 if (htab->opd_abi)
14774 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14775 else
14776 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14778 rela.r_addend = val;
14779 loc = relplt->contents + (relplt->reloc_count++
14780 * sizeof (Elf64_External_Rela));
14781 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14785 if (local_syms != NULL
14786 && symtab_hdr->contents != (unsigned char *) local_syms)
14788 if (!info->keep_memory)
14789 free (local_syms);
14790 else
14791 symtab_hdr->contents = (unsigned char *) local_syms;
14794 return true;
14797 /* Emit the static wrapper function preserving registers around a
14798 __tls_get_addr_opt call. */
14800 static bool
14801 emit_tga_desc (struct ppc_link_hash_table *htab)
14803 asection *stub_sec = htab->tga_group->stub_sec;
14804 unsigned int cfa_updt = 11 * 4;
14805 bfd_byte *p;
14806 bfd_vma to, from, delta;
14808 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14809 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14810 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14811 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14812 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14813 delta = to - from;
14814 if (delta + (1 << 25) >= 1 << 26)
14816 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14817 htab->stub_error = true;
14818 return false;
14821 p = stub_sec->contents;
14822 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14823 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14824 p += 4;
14825 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14826 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14829 /* Emit eh_frame describing the static wrapper function. */
14831 static bfd_byte *
14832 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14834 unsigned int cfa_updt = 11 * 4;
14835 unsigned int i;
14837 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14838 *p++ = DW_CFA_def_cfa_offset;
14839 if (htab->opd_abi)
14841 *p++ = 128;
14842 *p++ = 1;
14844 else
14845 *p++ = 96;
14846 *p++ = DW_CFA_offset_extended_sf;
14847 *p++ = 65;
14848 *p++ = (-16 / 8) & 0x7f;
14849 for (i = 4; i < 12; i++)
14851 *p++ = DW_CFA_offset + i;
14852 *p++ = (htab->opd_abi ? 13 : 12) - i;
14854 *p++ = DW_CFA_advance_loc + 10;
14855 *p++ = DW_CFA_def_cfa_offset;
14856 *p++ = 0;
14857 for (i = 4; i < 12; i++)
14858 *p++ = DW_CFA_restore + i;
14859 *p++ = DW_CFA_advance_loc + 2;
14860 *p++ = DW_CFA_restore_extended;
14861 *p++ = 65;
14862 return p;
14865 /* Build all the stubs associated with the current output file.
14866 The stubs are kept in a hash table attached to the main linker
14867 hash table. This function is called via gldelf64ppc_finish. */
14869 bool
14870 ppc64_elf_build_stubs (struct bfd_link_info *info,
14871 char **stats)
14873 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14874 struct map_stub *group;
14875 asection *stub_sec;
14876 bfd_byte *p;
14877 int stub_sec_count = 0;
14879 if (htab == NULL)
14880 return false;
14882 /* Allocate memory to hold the linker stubs. */
14883 for (group = htab->group; group != NULL; group = group->next)
14885 group->eh_size = 0;
14886 group->lr_restore = 0;
14887 if ((stub_sec = group->stub_sec) != NULL
14888 && stub_sec->size != 0)
14890 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14891 stub_sec->size);
14892 if (stub_sec->contents == NULL)
14893 return false;
14894 stub_sec->size = 0;
14898 if (htab->glink != NULL && htab->glink->size != 0)
14900 unsigned int indx;
14901 bfd_vma plt0;
14903 /* Build the .glink plt call stub. */
14904 if (htab->params->emit_stub_syms)
14906 struct elf_link_hash_entry *h;
14907 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14908 true, false, false);
14909 if (h == NULL)
14910 return false;
14911 if (h->root.type == bfd_link_hash_new)
14913 h->root.type = bfd_link_hash_defined;
14914 h->root.u.def.section = htab->glink;
14915 h->root.u.def.value = 8;
14916 h->ref_regular = 1;
14917 h->def_regular = 1;
14918 h->ref_regular_nonweak = 1;
14919 h->forced_local = 1;
14920 h->non_elf = 0;
14921 h->root.linker_def = 1;
14924 plt0 = (htab->elf.splt->output_section->vma
14925 + htab->elf.splt->output_offset
14926 - 16);
14927 if (info->emitrelocations)
14929 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14930 if (r == NULL)
14931 return false;
14932 r->r_offset = (htab->glink->output_offset
14933 + htab->glink->output_section->vma);
14934 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14935 r->r_addend = plt0;
14937 p = htab->glink->contents;
14938 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14939 bfd_put_64 (htab->glink->owner, plt0, p);
14940 p += 8;
14941 if (htab->opd_abi)
14943 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14944 p += 4;
14945 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14946 p += 4;
14947 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14948 p += 4;
14949 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14950 p += 4;
14951 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14952 p += 4;
14953 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14954 p += 4;
14955 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14956 p += 4;
14957 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14958 p += 4;
14959 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14960 p += 4;
14961 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14962 p += 4;
14964 else
14966 unsigned int insn;
14968 /* 0:
14969 . .quad plt0-1f # plt0 entry relative to 1:
14971 # We get here with r12 initially @ a glink branch
14972 # Load the address of _dl_runtime_resolve from plt0 and
14973 # jump to it, with r0 set to the index of the PLT entry
14974 # to be resolved and r11 the link map.
14975 __glink_PLTresolve:
14976 . std %r2,24(%r1) # optional
14977 . mflr %r0
14978 . bcl 20,31,1f
14980 . mflr %r11
14981 . mtlr %r0
14982 . ld %r0,(0b-1b)(%r11)
14983 . sub %r12,%r12,%r11
14984 . add %r11,%r0,%r11
14985 . addi %r0,%r12,1b-2f
14986 . ld %r12,0(%r11)
14987 . srdi %r0,%r0,2
14988 . mtctr %r12
14989 . ld %r11,8(%r11)
14990 . bctr
14992 . b __glink_PLTresolve
14993 . ...
14994 . b __glink_PLTresolve */
14996 if (htab->has_plt_localentry0)
14998 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14999 p += 4;
15001 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15002 p += 4;
15003 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15004 p += 4;
15005 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15006 p += 4;
15007 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15008 p += 4;
15009 if (htab->has_plt_localentry0)
15010 insn = LD_R0_0R11 | (-20 & 0xfffc);
15011 else
15012 insn = LD_R0_0R11 | (-16 & 0xfffc);
15013 bfd_put_32 (htab->glink->owner, insn, p);
15014 p += 4;
15015 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15016 p += 4;
15017 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15018 p += 4;
15019 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15020 p += 4;
15021 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15022 p += 4;
15023 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15024 p += 4;
15025 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15026 p += 4;
15027 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15028 p += 4;
15030 bfd_put_32 (htab->glink->owner, BCTR, p);
15031 p += 4;
15032 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15034 /* Build the .glink lazy link call stubs. */
15035 indx = 0;
15036 while (p < htab->glink->contents + htab->glink->size)
15038 if (htab->opd_abi)
15040 if (indx < 0x8000)
15042 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15043 p += 4;
15045 else
15047 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15048 p += 4;
15049 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15051 p += 4;
15054 bfd_put_32 (htab->glink->owner,
15055 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15056 indx++;
15057 p += 4;
15061 if (htab->tga_group != NULL)
15063 htab->tga_group->lr_restore = 23 * 4;
15064 htab->tga_group->stub_sec->size = 24 * 4;
15065 if (!emit_tga_desc (htab))
15066 return false;
15067 if (htab->glink_eh_frame != NULL
15068 && htab->glink_eh_frame->size != 0)
15070 size_t align = 4;
15072 p = htab->glink_eh_frame->contents;
15073 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15074 p += 17;
15075 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15079 /* Build .glink global entry stubs, and PLT relocs for globals. */
15080 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15082 if (!write_plt_relocs_for_local_syms (info))
15083 return false;
15085 if (htab->brlt != NULL && htab->brlt->size != 0)
15087 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15088 htab->brlt->size);
15089 if (htab->brlt->contents == NULL)
15090 return false;
15092 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15094 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15095 htab->relbrlt->size);
15096 if (htab->relbrlt->contents == NULL)
15097 return false;
15100 /* Build the stubs as directed by the stub hash table. */
15101 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15103 for (group = htab->group; group != NULL; group = group->next)
15104 if (group->needs_save_res)
15105 group->stub_sec->size += htab->sfpr->size;
15107 if (htab->relbrlt != NULL)
15108 htab->relbrlt->reloc_count = 0;
15110 if (htab->params->plt_stub_align != 0)
15111 for (group = htab->group; group != NULL; group = group->next)
15112 if ((stub_sec = group->stub_sec) != NULL)
15114 int align = abs (htab->params->plt_stub_align);
15115 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15118 for (group = htab->group; group != NULL; group = group->next)
15119 if (group->needs_save_res)
15121 stub_sec = group->stub_sec;
15122 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15123 htab->sfpr->contents, htab->sfpr->size);
15124 if (htab->params->emit_stub_syms)
15126 unsigned int i;
15128 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15129 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15130 return false;
15134 if (htab->glink_eh_frame != NULL
15135 && htab->glink_eh_frame->size != 0)
15137 bfd_vma val;
15138 size_t align = 4;
15140 p = htab->glink_eh_frame->contents;
15141 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15143 for (group = htab->group; group != NULL; group = group->next)
15144 if (group->eh_size != 0)
15146 /* Offset to stub section. */
15147 val = (group->stub_sec->output_section->vma
15148 + group->stub_sec->output_offset);
15149 val -= (htab->glink_eh_frame->output_section->vma
15150 + htab->glink_eh_frame->output_offset
15151 + (p + 8 - htab->glink_eh_frame->contents));
15152 if (val + 0x80000000 > 0xffffffff)
15154 _bfd_error_handler
15155 (_("%s offset too large for .eh_frame sdata4 encoding"),
15156 group->stub_sec->name);
15157 return false;
15159 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15160 p += (group->eh_size + 17 + 3) & -4;
15162 if (htab->glink != NULL && htab->glink->size != 0)
15164 /* Offset to .glink. */
15165 val = (htab->glink->output_section->vma
15166 + htab->glink->output_offset
15167 + 8);
15168 val -= (htab->glink_eh_frame->output_section->vma
15169 + htab->glink_eh_frame->output_offset
15170 + (p + 8 - htab->glink_eh_frame->contents));
15171 if (val + 0x80000000 > 0xffffffff)
15173 _bfd_error_handler
15174 (_("%s offset too large for .eh_frame sdata4 encoding"),
15175 htab->glink->name);
15176 return false;
15178 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15179 p += (24 + align - 1) & -align;
15183 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15185 htab->elf.srelrdyn->contents
15186 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15187 if (htab->elf.srelrdyn->contents == NULL)
15188 return false;
15190 bfd_vma *relr_addr = sort_relr (htab);
15191 if (htab->relr_count != 0 && relr_addr == NULL)
15192 return false;
15194 size_t i = 0;
15195 bfd_byte *loc = htab->elf.srelrdyn->contents;
15196 while (i < htab->relr_count)
15198 bfd_vma base = relr_addr[i];
15199 BFD_ASSERT (base % 2 == 0);
15200 bfd_put_64 (htab->elf.dynobj, base, loc);
15201 loc += 8;
15202 i++;
15203 while (i < htab->relr_count
15204 && relr_addr[i] == base)
15206 htab->stub_error = true;
15207 i++;
15209 base += 8;
15210 while (1)
15212 bfd_vma bits = 0;
15213 while (i < htab->relr_count
15214 && relr_addr[i] - base < 63 * 8
15215 && (relr_addr[i] - base) % 8 == 0)
15217 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15218 i++;
15220 if (bits == 0)
15221 break;
15222 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15223 loc += 8;
15224 base += 63 * 8;
15227 free (relr_addr);
15228 /* Pad any excess with 1's, a do-nothing encoding. */
15229 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15230 < htab->elf.srelrdyn->size)
15232 bfd_put_64 (htab->elf.dynobj, 1, loc);
15233 loc += 8;
15237 for (group = htab->group; group != NULL; group = group->next)
15238 if ((stub_sec = group->stub_sec) != NULL)
15240 stub_sec_count += 1;
15241 if (stub_sec->rawsize != stub_sec->size
15242 && (htab->stub_iteration <= STUB_SHRINK_ITER
15243 || stub_sec->rawsize < stub_sec->size))
15244 break;
15247 if (group != NULL)
15248 htab->stub_error = true;
15250 if (htab->stub_error)
15252 _bfd_error_handler (_("stubs don't match calculated size"));
15253 return false;
15256 if (stats != NULL)
15258 char *groupmsg;
15259 if (asprintf (&groupmsg,
15260 ngettext ("linker stubs in %u group\n",
15261 "linker stubs in %u groups\n",
15262 stub_sec_count),
15263 stub_sec_count) < 0)
15264 *stats = NULL;
15265 else
15267 if (asprintf (stats, _("%s"
15268 " branch %lu\n"
15269 " long branch %lu\n"
15270 " plt call %lu\n"
15271 " global entry %lu"),
15272 groupmsg,
15273 htab->stub_count[ppc_stub_long_branch - 1],
15274 htab->stub_count[ppc_stub_plt_branch - 1],
15275 htab->stub_count[ppc_stub_plt_call - 1],
15276 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15277 *stats = NULL;
15278 free (groupmsg);
15281 return true;
15284 /* What to do when ld finds relocations against symbols defined in
15285 discarded sections. */
15287 static unsigned int
15288 ppc64_elf_action_discarded (asection *sec)
15290 if (strcmp (".opd", sec->name) == 0)
15291 return 0;
15293 if (strcmp (".toc", sec->name) == 0)
15294 return 0;
15296 if (strcmp (".toc1", sec->name) == 0)
15297 return 0;
15299 return _bfd_elf_default_action_discarded (sec);
15302 /* These are the dynamic relocations supported by glibc. */
15304 static bool
15305 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15307 switch (r_type)
15309 case R_PPC64_RELATIVE:
15310 case R_PPC64_NONE:
15311 case R_PPC64_ADDR64:
15312 case R_PPC64_GLOB_DAT:
15313 case R_PPC64_IRELATIVE:
15314 case R_PPC64_JMP_IREL:
15315 case R_PPC64_JMP_SLOT:
15316 case R_PPC64_DTPMOD64:
15317 case R_PPC64_DTPREL64:
15318 case R_PPC64_TPREL64:
15319 case R_PPC64_TPREL16_LO_DS:
15320 case R_PPC64_TPREL16_DS:
15321 case R_PPC64_TPREL16:
15322 case R_PPC64_TPREL16_LO:
15323 case R_PPC64_TPREL16_HI:
15324 case R_PPC64_TPREL16_HIGH:
15325 case R_PPC64_TPREL16_HA:
15326 case R_PPC64_TPREL16_HIGHA:
15327 case R_PPC64_TPREL16_HIGHER:
15328 case R_PPC64_TPREL16_HIGHEST:
15329 case R_PPC64_TPREL16_HIGHERA:
15330 case R_PPC64_TPREL16_HIGHESTA:
15331 case R_PPC64_ADDR16_LO_DS:
15332 case R_PPC64_ADDR16_LO:
15333 case R_PPC64_ADDR16_HI:
15334 case R_PPC64_ADDR16_HIGH:
15335 case R_PPC64_ADDR16_HA:
15336 case R_PPC64_ADDR16_HIGHA:
15337 case R_PPC64_REL30:
15338 case R_PPC64_COPY:
15339 case R_PPC64_UADDR64:
15340 case R_PPC64_UADDR32:
15341 case R_PPC64_ADDR32:
15342 case R_PPC64_ADDR24:
15343 case R_PPC64_ADDR16:
15344 case R_PPC64_UADDR16:
15345 case R_PPC64_ADDR16_DS:
15346 case R_PPC64_ADDR16_HIGHER:
15347 case R_PPC64_ADDR16_HIGHEST:
15348 case R_PPC64_ADDR16_HIGHERA:
15349 case R_PPC64_ADDR16_HIGHESTA:
15350 case R_PPC64_ADDR14:
15351 case R_PPC64_ADDR14_BRTAKEN:
15352 case R_PPC64_ADDR14_BRNTAKEN:
15353 case R_PPC64_REL32:
15354 case R_PPC64_REL64:
15355 return true;
15357 default:
15358 return false;
15362 /* The RELOCATE_SECTION function is called by the ELF backend linker
15363 to handle the relocations for a section.
15365 The relocs are always passed as Rela structures; if the section
15366 actually uses Rel structures, the r_addend field will always be
15367 zero.
15369 This function is responsible for adjust the section contents as
15370 necessary, and (if using Rela relocs and generating a
15371 relocatable output file) adjusting the reloc addend as
15372 necessary.
15374 This function does not have to worry about setting the reloc
15375 address or the reloc symbol index.
15377 LOCAL_SYMS is a pointer to the swapped in local symbols.
15379 LOCAL_SECTIONS is an array giving the section in the input file
15380 corresponding to the st_shndx field of each local symbol.
15382 The global hash table entry for the global symbols can be found
15383 via elf_sym_hashes (input_bfd).
15385 When generating relocatable output, this function must handle
15386 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15387 going to be the section symbol corresponding to the output
15388 section, which means that the addend must be adjusted
15389 accordingly. */
15391 static int
15392 ppc64_elf_relocate_section (bfd *output_bfd,
15393 struct bfd_link_info *info,
15394 bfd *input_bfd,
15395 asection *input_section,
15396 bfd_byte *contents,
15397 Elf_Internal_Rela *relocs,
15398 Elf_Internal_Sym *local_syms,
15399 asection **local_sections)
15401 struct ppc_link_hash_table *htab;
15402 Elf_Internal_Shdr *symtab_hdr;
15403 struct elf_link_hash_entry **sym_hashes;
15404 Elf_Internal_Rela *rel;
15405 Elf_Internal_Rela *wrel;
15406 Elf_Internal_Rela *relend;
15407 Elf_Internal_Rela outrel;
15408 bfd_byte *loc;
15409 struct got_entry **local_got_ents;
15410 bfd_vma TOCstart;
15411 bool ret = true;
15412 bool is_opd;
15413 /* Assume 'at' branch hints. */
15414 bool is_isa_v2 = true;
15415 bool warned_dynamic = false;
15416 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15418 /* Initialize howto table if needed. */
15419 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15420 ppc_howto_init ();
15422 htab = ppc_hash_table (info);
15423 if (htab == NULL)
15424 return false;
15426 /* Don't relocate stub sections. */
15427 if (input_section->owner == htab->params->stub_bfd)
15428 return true;
15430 if (!is_ppc64_elf (input_bfd))
15432 bfd_set_error (bfd_error_wrong_format);
15433 return false;
15436 local_got_ents = elf_local_got_ents (input_bfd);
15437 TOCstart = elf_gp (output_bfd);
15438 symtab_hdr = &elf_symtab_hdr (input_bfd);
15439 sym_hashes = elf_sym_hashes (input_bfd);
15440 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15442 rel = wrel = relocs;
15443 relend = relocs + input_section->reloc_count;
15444 for (; rel < relend; wrel++, rel++)
15446 enum elf_ppc64_reloc_type r_type;
15447 bfd_vma addend;
15448 bfd_reloc_status_type r;
15449 Elf_Internal_Sym *sym;
15450 asection *sec;
15451 struct elf_link_hash_entry *h_elf;
15452 struct ppc_link_hash_entry *h;
15453 struct ppc_link_hash_entry *fdh;
15454 const char *sym_name;
15455 unsigned long r_symndx, toc_symndx;
15456 bfd_vma toc_addend;
15457 unsigned char tls_mask, tls_gd, tls_type;
15458 unsigned char sym_type;
15459 bfd_vma relocation;
15460 bool unresolved_reloc, save_unresolved_reloc;
15461 bool warned;
15462 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15463 unsigned int insn;
15464 unsigned int mask;
15465 struct ppc_stub_hash_entry *stub_entry;
15466 bfd_vma max_br_offset;
15467 bfd_vma from;
15468 Elf_Internal_Rela orig_rel;
15469 reloc_howto_type *howto;
15470 struct reloc_howto_struct alt_howto;
15471 uint64_t pinsn;
15472 bfd_vma offset;
15474 again:
15475 orig_rel = *rel;
15477 r_type = ELF64_R_TYPE (rel->r_info);
15478 r_symndx = ELF64_R_SYM (rel->r_info);
15480 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15481 symbol of the previous ADDR64 reloc. The symbol gives us the
15482 proper TOC base to use. */
15483 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15484 && wrel != relocs
15485 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15486 && is_opd)
15487 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15489 sym = NULL;
15490 sec = NULL;
15491 h_elf = NULL;
15492 sym_name = NULL;
15493 unresolved_reloc = false;
15494 warned = false;
15496 if (r_symndx < symtab_hdr->sh_info)
15498 /* It's a local symbol. */
15499 struct _opd_sec_data *opd;
15501 sym = local_syms + r_symndx;
15502 sec = local_sections[r_symndx];
15503 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15504 sym_type = ELF64_ST_TYPE (sym->st_info);
15505 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15506 opd = get_opd_info (sec);
15507 if (opd != NULL && opd->adjust != NULL)
15509 long adjust = opd->adjust[OPD_NDX (sym->st_value
15510 + rel->r_addend)];
15511 if (adjust == -1)
15512 relocation = 0;
15513 else
15515 /* If this is a relocation against the opd section sym
15516 and we have edited .opd, adjust the reloc addend so
15517 that ld -r and ld --emit-relocs output is correct.
15518 If it is a reloc against some other .opd symbol,
15519 then the symbol value will be adjusted later. */
15520 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15521 rel->r_addend += adjust;
15522 else
15523 relocation += adjust;
15527 else
15529 bool ignored;
15531 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15532 r_symndx, symtab_hdr, sym_hashes,
15533 h_elf, sec, relocation,
15534 unresolved_reloc, warned, ignored);
15535 sym_name = h_elf->root.root.string;
15536 sym_type = h_elf->type;
15537 if (sec != NULL
15538 && sec->owner == output_bfd
15539 && strcmp (sec->name, ".opd") == 0)
15541 /* This is a symbol defined in a linker script. All
15542 such are defined in output sections, even those
15543 defined by simple assignment from a symbol defined in
15544 an input section. Transfer the symbol to an
15545 appropriate input .opd section, so that a branch to
15546 this symbol will be mapped to the location specified
15547 by the opd entry. */
15548 struct bfd_link_order *lo;
15549 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15550 if (lo->type == bfd_indirect_link_order)
15552 asection *isec = lo->u.indirect.section;
15553 if (h_elf->root.u.def.value >= isec->output_offset
15554 && h_elf->root.u.def.value < (isec->output_offset
15555 + isec->size))
15557 h_elf->root.u.def.value -= isec->output_offset;
15558 h_elf->root.u.def.section = isec;
15559 sec = isec;
15560 break;
15565 h = ppc_elf_hash_entry (h_elf);
15567 if (sec != NULL && discarded_section (sec))
15569 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15570 input_bfd, input_section,
15571 contents, rel->r_offset);
15572 wrel->r_offset = rel->r_offset;
15573 wrel->r_info = 0;
15574 wrel->r_addend = 0;
15576 /* For ld -r, remove relocations in debug sections against
15577 symbols defined in discarded sections. Not done for
15578 non-debug to preserve relocs in .eh_frame which the
15579 eh_frame editing code expects to be present. */
15580 if (bfd_link_relocatable (info)
15581 && (input_section->flags & SEC_DEBUGGING))
15582 wrel--;
15584 continue;
15587 if (bfd_link_relocatable (info))
15588 goto copy_reloc;
15590 if (h != NULL && &h->elf == htab->elf.hgot)
15592 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15593 sec = bfd_abs_section_ptr;
15594 unresolved_reloc = false;
15597 /* TLS optimizations. Replace instruction sequences and relocs
15598 based on information we collected in tls_optimize. We edit
15599 RELOCS so that --emit-relocs will output something sensible
15600 for the final instruction stream. */
15601 tls_mask = 0;
15602 tls_gd = 0;
15603 toc_symndx = 0;
15604 if (h != NULL)
15605 tls_mask = h->tls_mask;
15606 else if (local_got_ents != NULL)
15608 struct plt_entry **local_plt = (struct plt_entry **)
15609 (local_got_ents + symtab_hdr->sh_info);
15610 unsigned char *lgot_masks = (unsigned char *)
15611 (local_plt + symtab_hdr->sh_info);
15612 tls_mask = lgot_masks[r_symndx];
15614 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15615 && (r_type == R_PPC64_TLS
15616 || r_type == R_PPC64_TLSGD
15617 || r_type == R_PPC64_TLSLD))
15619 /* Check for toc tls entries. */
15620 unsigned char *toc_tls;
15622 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15623 &local_syms, rel, input_bfd))
15624 return false;
15626 if (toc_tls)
15627 tls_mask = *toc_tls;
15630 /* Check that tls relocs are used with tls syms, and non-tls
15631 relocs are used with non-tls syms. */
15632 if (r_symndx != STN_UNDEF
15633 && r_type != R_PPC64_NONE
15634 && (h == NULL
15635 || h->elf.root.type == bfd_link_hash_defined
15636 || h->elf.root.type == bfd_link_hash_defweak)
15637 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15639 if ((tls_mask & TLS_TLS) != 0
15640 && (r_type == R_PPC64_TLS
15641 || r_type == R_PPC64_TLSGD
15642 || r_type == R_PPC64_TLSLD))
15643 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15645 else
15646 info->callbacks->einfo
15647 (!IS_PPC64_TLS_RELOC (r_type)
15648 /* xgettext:c-format */
15649 ? _("%H: %s used with TLS symbol `%pT'\n")
15650 /* xgettext:c-format */
15651 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15652 input_bfd, input_section, rel->r_offset,
15653 ppc64_elf_howto_table[r_type]->name,
15654 sym_name);
15657 /* Ensure reloc mapping code below stays sane. */
15658 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15659 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15660 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15661 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15662 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15663 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15664 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15665 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15666 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15667 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15668 abort ();
15670 switch (r_type)
15672 default:
15673 break;
15675 case R_PPC64_LO_DS_OPT:
15676 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15678 insn = bfd_get_32 (input_bfd,
15679 contents + rel->r_offset - d_offset);
15680 if ((insn & (0x3fu << 26)) != 58u << 26)
15681 abort ();
15682 insn += (14u << 26) - (58u << 26);
15683 bfd_put_32 (input_bfd, insn,
15684 contents + rel->r_offset - d_offset);
15685 r_type = R_PPC64_TOC16_LO;
15686 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15688 break;
15690 case R_PPC64_TOC16:
15691 case R_PPC64_TOC16_LO:
15692 case R_PPC64_TOC16_DS:
15693 case R_PPC64_TOC16_LO_DS:
15695 /* Check for toc tls entries. */
15696 unsigned char *toc_tls;
15697 int retval;
15699 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15700 &local_syms, rel, input_bfd);
15701 if (retval == 0)
15702 return false;
15704 if (toc_tls)
15706 tls_mask = *toc_tls;
15707 if (r_type == R_PPC64_TOC16_DS
15708 || r_type == R_PPC64_TOC16_LO_DS)
15710 if ((tls_mask & TLS_TLS) != 0
15711 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15712 goto toctprel;
15714 else
15716 /* If we found a GD reloc pair, then we might be
15717 doing a GD->IE transition. */
15718 if (retval == 2)
15720 tls_gd = TLS_GDIE;
15721 if ((tls_mask & TLS_TLS) != 0
15722 && (tls_mask & TLS_GD) == 0)
15723 goto tls_ldgd_opt;
15725 else if (retval == 3)
15727 if ((tls_mask & TLS_TLS) != 0
15728 && (tls_mask & TLS_LD) == 0)
15729 goto tls_ldgd_opt;
15734 break;
15736 case R_PPC64_GOT_TPREL16_HI:
15737 case R_PPC64_GOT_TPREL16_HA:
15738 if ((tls_mask & TLS_TLS) != 0
15739 && (tls_mask & TLS_TPREL) == 0
15740 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15742 rel->r_offset -= d_offset;
15743 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15744 r_type = R_PPC64_NONE;
15745 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15747 break;
15749 case R_PPC64_GOT_TPREL16_DS:
15750 case R_PPC64_GOT_TPREL16_LO_DS:
15751 if ((tls_mask & TLS_TLS) != 0
15752 && (tls_mask & TLS_TPREL) == 0
15753 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15755 toctprel:
15756 insn = bfd_get_32 (input_bfd,
15757 contents + rel->r_offset - d_offset);
15758 insn &= 31 << 21;
15759 insn |= 0x3c0d0000; /* addis 0,13,0 */
15760 bfd_put_32 (input_bfd, insn,
15761 contents + rel->r_offset - d_offset);
15762 r_type = R_PPC64_TPREL16_HA;
15763 if (toc_symndx != 0)
15765 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15766 rel->r_addend = toc_addend;
15767 /* We changed the symbol. Start over in order to
15768 get h, sym, sec etc. right. */
15769 goto again;
15771 else
15772 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15774 break;
15776 case R_PPC64_GOT_TPREL_PCREL34:
15777 if ((tls_mask & TLS_TLS) != 0
15778 && (tls_mask & TLS_TPREL) == 0
15779 && offset_in_range (input_section, rel->r_offset, 8))
15781 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15782 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15783 pinsn <<= 32;
15784 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15785 pinsn += ((2ULL << 56) + (-1ULL << 52)
15786 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15787 bfd_put_32 (input_bfd, pinsn >> 32,
15788 contents + rel->r_offset);
15789 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15790 contents + rel->r_offset + 4);
15791 r_type = R_PPC64_TPREL34;
15792 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15794 break;
15796 case R_PPC64_TLS:
15797 if ((tls_mask & TLS_TLS) != 0
15798 && (tls_mask & TLS_TPREL) == 0
15799 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15801 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15802 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15803 if (insn == 0)
15804 break;
15805 if ((rel->r_offset & 3) == 0)
15807 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15808 /* Was PPC64_TLS which sits on insn boundary, now
15809 PPC64_TPREL16_LO which is at low-order half-word. */
15810 rel->r_offset += d_offset;
15811 r_type = R_PPC64_TPREL16_LO;
15812 if (toc_symndx != 0)
15814 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15815 rel->r_addend = toc_addend;
15816 /* We changed the symbol. Start over in order to
15817 get h, sym, sec etc. right. */
15818 goto again;
15820 else
15821 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15823 else if ((rel->r_offset & 3) == 1)
15825 /* For pcrel IE to LE we already have the full
15826 offset and thus don't need an addi here. A nop
15827 or mr will do. */
15828 if ((insn & (0x3fu << 26)) == 14 << 26)
15830 /* Extract regs from addi rt,ra,si. */
15831 unsigned int rt = (insn >> 21) & 0x1f;
15832 unsigned int ra = (insn >> 16) & 0x1f;
15833 if (rt == ra)
15834 insn = NOP;
15835 else
15837 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15838 insn = (rt << 16) | (ra << 21) | (ra << 11);
15839 insn |= (31u << 26) | (444u << 1);
15842 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15845 break;
15847 case R_PPC64_GOT_TLSGD16_HI:
15848 case R_PPC64_GOT_TLSGD16_HA:
15849 tls_gd = TLS_GDIE;
15850 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15851 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15852 goto tls_gdld_hi;
15853 break;
15855 case R_PPC64_GOT_TLSLD16_HI:
15856 case R_PPC64_GOT_TLSLD16_HA:
15857 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15858 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15860 tls_gdld_hi:
15861 if ((tls_mask & tls_gd) != 0)
15862 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15863 + R_PPC64_GOT_TPREL16_DS);
15864 else
15866 rel->r_offset -= d_offset;
15867 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15868 r_type = R_PPC64_NONE;
15870 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15872 break;
15874 case R_PPC64_GOT_TLSGD16:
15875 case R_PPC64_GOT_TLSGD16_LO:
15876 tls_gd = TLS_GDIE;
15877 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15878 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15879 goto tls_ldgd_opt;
15880 break;
15882 case R_PPC64_GOT_TLSLD16:
15883 case R_PPC64_GOT_TLSLD16_LO:
15884 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15885 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15887 unsigned int insn1, insn2;
15889 tls_ldgd_opt:
15890 offset = (bfd_vma) -1;
15891 /* If not using the newer R_PPC64_TLSGD/LD to mark
15892 __tls_get_addr calls, we must trust that the call
15893 stays with its arg setup insns, ie. that the next
15894 reloc is the __tls_get_addr call associated with
15895 the current reloc. Edit both insns. */
15896 if (input_section->nomark_tls_get_addr
15897 && rel + 1 < relend
15898 && branch_reloc_hash_match (input_bfd, rel + 1,
15899 htab->tls_get_addr_fd,
15900 htab->tga_desc_fd,
15901 htab->tls_get_addr,
15902 htab->tga_desc))
15903 offset = rel[1].r_offset;
15904 /* We read the low GOT_TLS (or TOC16) insn because we
15905 need to keep the destination reg. It may be
15906 something other than the usual r3, and moved to r3
15907 before the call by intervening code. */
15908 insn1 = bfd_get_32 (input_bfd,
15909 contents + rel->r_offset - d_offset);
15910 if ((tls_mask & tls_gd) != 0)
15912 /* IE */
15913 insn1 &= (0x1f << 21) | (0x1f << 16);
15914 insn1 |= 58u << 26; /* ld */
15915 insn2 = 0x7c636a14; /* add 3,3,13 */
15916 if (offset != (bfd_vma) -1)
15917 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15918 if (r_type == R_PPC64_TOC16
15919 || r_type == R_PPC64_TOC16_LO)
15920 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15921 else
15922 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15923 + R_PPC64_GOT_TPREL16_DS);
15924 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15926 else
15928 /* LE */
15929 insn1 &= 0x1f << 21;
15930 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15931 insn2 = 0x38630000; /* addi 3,3,0 */
15932 if (tls_gd == 0)
15934 /* Was an LD reloc. */
15935 r_symndx = STN_UNDEF;
15936 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15938 else if (toc_symndx != 0)
15940 r_symndx = toc_symndx;
15941 rel->r_addend = toc_addend;
15943 r_type = R_PPC64_TPREL16_HA;
15944 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15945 if (offset != (bfd_vma) -1)
15947 rel[1].r_info = ELF64_R_INFO (r_symndx,
15948 R_PPC64_TPREL16_LO);
15949 rel[1].r_offset = offset + d_offset;
15950 rel[1].r_addend = rel->r_addend;
15953 bfd_put_32 (input_bfd, insn1,
15954 contents + rel->r_offset - d_offset);
15955 if (offset != (bfd_vma) -1
15956 && offset_in_range (input_section, offset, 4))
15958 bfd_put_32 (input_bfd, insn2, contents + offset);
15959 if (offset_in_range (input_section, offset + 4, 4))
15961 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15962 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15963 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15966 if ((tls_mask & tls_gd) == 0
15967 && (tls_gd == 0 || toc_symndx != 0))
15969 /* We changed the symbol. Start over in order
15970 to get h, sym, sec etc. right. */
15971 goto again;
15974 break;
15976 case R_PPC64_GOT_TLSGD_PCREL34:
15977 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15978 && offset_in_range (input_section, rel->r_offset, 8))
15980 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15981 pinsn <<= 32;
15982 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15983 if ((tls_mask & TLS_GDIE) != 0)
15985 /* IE, pla -> pld */
15986 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
15987 r_type = R_PPC64_GOT_TPREL_PCREL34;
15989 else
15991 /* LE, pla pcrel -> paddi r13 */
15992 pinsn += (-1ULL << 52) + (13ULL << 16);
15993 r_type = R_PPC64_TPREL34;
15995 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15996 bfd_put_32 (input_bfd, pinsn >> 32,
15997 contents + rel->r_offset);
15998 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15999 contents + rel->r_offset + 4);
16001 break;
16003 case R_PPC64_GOT_TLSLD_PCREL34:
16004 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16005 && offset_in_range (input_section, rel->r_offset, 8))
16007 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16008 pinsn <<= 32;
16009 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16010 pinsn += (-1ULL << 52) + (13ULL << 16);
16011 bfd_put_32 (input_bfd, pinsn >> 32,
16012 contents + rel->r_offset);
16013 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16014 contents + rel->r_offset + 4);
16015 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16016 r_symndx = STN_UNDEF;
16017 r_type = R_PPC64_TPREL34;
16018 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16019 goto again;
16021 break;
16023 case R_PPC64_TLSGD:
16024 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16025 && rel + 1 < relend
16026 && offset_in_range (input_section, rel->r_offset,
16027 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16028 ? 8 : 4))
16030 unsigned int insn2;
16031 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16033 offset = rel->r_offset;
16034 if (is_plt_seq_reloc (r_type1))
16036 bfd_put_32 (output_bfd, NOP, contents + offset);
16037 if (r_type1 == R_PPC64_PLT_PCREL34
16038 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16039 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16040 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16041 break;
16044 if (r_type1 == R_PPC64_PLTCALL)
16045 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16047 if ((tls_mask & TLS_GDIE) != 0)
16049 /* IE */
16050 r_type = R_PPC64_NONE;
16051 insn2 = 0x7c636a14; /* add 3,3,13 */
16053 else
16055 /* LE */
16056 if (toc_symndx != 0)
16058 r_symndx = toc_symndx;
16059 rel->r_addend = toc_addend;
16061 if (r_type1 == R_PPC64_REL24_NOTOC
16062 || r_type1 == R_PPC64_REL24_P9NOTOC
16063 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16065 r_type = R_PPC64_NONE;
16066 insn2 = NOP;
16068 else
16070 rel->r_offset = offset + d_offset;
16071 r_type = R_PPC64_TPREL16_LO;
16072 insn2 = 0x38630000; /* addi 3,3,0 */
16075 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16076 /* Zap the reloc on the _tls_get_addr call too. */
16077 BFD_ASSERT (offset == rel[1].r_offset);
16078 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16079 bfd_put_32 (input_bfd, insn2, contents + offset);
16080 if ((tls_mask & TLS_GDIE) == 0
16081 && toc_symndx != 0
16082 && r_type != R_PPC64_NONE)
16083 goto again;
16085 break;
16087 case R_PPC64_TLSLD:
16088 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16089 && rel + 1 < relend
16090 && offset_in_range (input_section, rel->r_offset,
16091 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16092 ? 8 : 4))
16094 unsigned int insn2;
16095 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16097 offset = rel->r_offset;
16098 if (is_plt_seq_reloc (r_type1))
16100 bfd_put_32 (output_bfd, NOP, contents + offset);
16101 if (r_type1 == R_PPC64_PLT_PCREL34
16102 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16103 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16104 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16105 break;
16108 if (r_type1 == R_PPC64_PLTCALL)
16109 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16111 if (r_type1 == R_PPC64_REL24_NOTOC
16112 || r_type1 == R_PPC64_REL24_P9NOTOC
16113 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16115 r_type = R_PPC64_NONE;
16116 insn2 = NOP;
16118 else
16120 rel->r_offset = offset + d_offset;
16121 r_symndx = STN_UNDEF;
16122 r_type = R_PPC64_TPREL16_LO;
16123 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16124 insn2 = 0x38630000; /* addi 3,3,0 */
16126 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16127 /* Zap the reloc on the _tls_get_addr call too. */
16128 BFD_ASSERT (offset == rel[1].r_offset);
16129 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16130 bfd_put_32 (input_bfd, insn2, contents + offset);
16131 if (r_type != R_PPC64_NONE)
16132 goto again;
16134 break;
16136 case R_PPC64_DTPMOD64:
16137 if (rel + 1 < relend
16138 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16139 && rel[1].r_offset == rel->r_offset + 8)
16141 if ((tls_mask & TLS_GD) == 0
16142 && offset_in_range (input_section, rel->r_offset, 8))
16144 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16145 if ((tls_mask & TLS_GDIE) != 0)
16146 r_type = R_PPC64_TPREL64;
16147 else
16149 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16150 r_type = R_PPC64_NONE;
16152 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16155 else
16157 if ((tls_mask & TLS_LD) == 0
16158 && offset_in_range (input_section, rel->r_offset, 8))
16160 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16161 r_type = R_PPC64_NONE;
16162 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16165 break;
16167 case R_PPC64_TPREL64:
16168 if ((tls_mask & TLS_TPREL) == 0)
16170 r_type = R_PPC64_NONE;
16171 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16173 break;
16175 case R_PPC64_ENTRY:
16176 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16177 if (!bfd_link_pic (info)
16178 && !info->traditional_format
16179 && relocation + 0x80008000 <= 0xffffffff
16180 && offset_in_range (input_section, rel->r_offset, 8))
16182 unsigned int insn1, insn2;
16184 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16185 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16186 if ((insn1 & ~0xfffc) == LD_R2_0R12
16187 && insn2 == ADD_R2_R2_R12)
16189 bfd_put_32 (input_bfd,
16190 LIS_R2 + PPC_HA (relocation),
16191 contents + rel->r_offset);
16192 bfd_put_32 (input_bfd,
16193 ADDI_R2_R2 + PPC_LO (relocation),
16194 contents + rel->r_offset + 4);
16197 else
16199 relocation -= (rel->r_offset
16200 + input_section->output_offset
16201 + input_section->output_section->vma);
16202 if (relocation + 0x80008000 <= 0xffffffff
16203 && offset_in_range (input_section, rel->r_offset, 8))
16205 unsigned int insn1, insn2;
16207 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16208 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16209 if ((insn1 & ~0xfffc) == LD_R2_0R12
16210 && insn2 == ADD_R2_R2_R12)
16212 bfd_put_32 (input_bfd,
16213 ADDIS_R2_R12 + PPC_HA (relocation),
16214 contents + rel->r_offset);
16215 bfd_put_32 (input_bfd,
16216 ADDI_R2_R2 + PPC_LO (relocation),
16217 contents + rel->r_offset + 4);
16221 break;
16223 case R_PPC64_REL16_HA:
16224 /* If we are generating a non-PIC executable, edit
16225 . 0: addis 2,12,.TOC.-0b@ha
16226 . addi 2,2,.TOC.-0b@l
16227 used by ELFv2 global entry points to set up r2, to
16228 . lis 2,.TOC.@ha
16229 . addi 2,2,.TOC.@l
16230 if .TOC. is in range. */
16231 if (!bfd_link_pic (info)
16232 && !info->traditional_format
16233 && !htab->opd_abi
16234 && rel->r_addend == d_offset
16235 && h != NULL && &h->elf == htab->elf.hgot
16236 && rel + 1 < relend
16237 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16238 && rel[1].r_offset == rel->r_offset + 4
16239 && rel[1].r_addend == rel->r_addend + 4
16240 && relocation + 0x80008000 <= 0xffffffff
16241 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16243 unsigned int insn1, insn2;
16244 offset = rel->r_offset - d_offset;
16245 insn1 = bfd_get_32 (input_bfd, contents + offset);
16246 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16247 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16248 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16250 r_type = R_PPC64_ADDR16_HA;
16251 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16252 rel->r_addend -= d_offset;
16253 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16254 rel[1].r_addend -= d_offset + 4;
16255 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16258 break;
16261 /* Handle other relocations that tweak non-addend part of insn. */
16262 insn = 0;
16263 max_br_offset = 1 << 25;
16264 addend = rel->r_addend;
16265 reloc_dest = DEST_NORMAL;
16266 switch (r_type)
16268 default:
16269 break;
16271 case R_PPC64_TOCSAVE:
16272 if (relocation + addend == (rel->r_offset
16273 + input_section->output_offset
16274 + input_section->output_section->vma)
16275 && tocsave_find (htab, NO_INSERT,
16276 &local_syms, rel, input_bfd)
16277 && offset_in_range (input_section, rel->r_offset, 4))
16279 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16280 if (insn == NOP
16281 || insn == CROR_151515 || insn == CROR_313131)
16282 bfd_put_32 (input_bfd,
16283 STD_R2_0R1 + STK_TOC (htab),
16284 contents + rel->r_offset);
16286 break;
16288 /* Branch taken prediction relocations. */
16289 case R_PPC64_ADDR14_BRTAKEN:
16290 case R_PPC64_REL14_BRTAKEN:
16291 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16292 /* Fall through. */
16294 /* Branch not taken prediction relocations. */
16295 case R_PPC64_ADDR14_BRNTAKEN:
16296 case R_PPC64_REL14_BRNTAKEN:
16297 if (!offset_in_range (input_section, rel->r_offset, 4))
16298 break;
16299 insn |= bfd_get_32 (input_bfd,
16300 contents + rel->r_offset) & ~(0x01 << 21);
16301 /* Fall through. */
16303 case R_PPC64_REL14:
16304 max_br_offset = 1 << 15;
16305 /* Fall through. */
16307 case R_PPC64_REL24:
16308 case R_PPC64_REL24_NOTOC:
16309 case R_PPC64_REL24_P9NOTOC:
16310 case R_PPC64_PLTCALL:
16311 case R_PPC64_PLTCALL_NOTOC:
16312 /* Calls to functions with a different TOC, such as calls to
16313 shared objects, need to alter the TOC pointer. This is
16314 done using a linkage stub. A REL24 branching to these
16315 linkage stubs needs to be followed by a nop, as the nop
16316 will be replaced with an instruction to restore the TOC
16317 base pointer. */
16318 fdh = h;
16319 if (h != NULL
16320 && h->oh != NULL
16321 && h->oh->is_func_descriptor)
16322 fdh = ppc_follow_link (h->oh);
16323 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16324 htab);
16325 if ((r_type == R_PPC64_PLTCALL
16326 || r_type == R_PPC64_PLTCALL_NOTOC)
16327 && stub_entry != NULL
16328 && stub_entry->type.main == ppc_stub_plt_call)
16329 stub_entry = NULL;
16331 if (stub_entry != NULL
16332 && (stub_entry->type.main == ppc_stub_plt_call
16333 || stub_entry->type.r2save))
16335 bool can_plt_call = false;
16337 if (r_type == R_PPC64_REL24_NOTOC
16338 || r_type == R_PPC64_REL24_P9NOTOC)
16340 /* NOTOC calls don't need to restore r2. */
16341 can_plt_call = true;
16343 else if (stub_entry->type.main == ppc_stub_plt_call
16344 && !htab->opd_abi
16345 && htab->params->plt_localentry0 != 0
16346 && h != NULL
16347 && is_elfv2_localentry0 (&h->elf))
16349 /* The function doesn't use or change r2. */
16350 can_plt_call = true;
16353 /* All of these stubs may modify r2, so there must be a
16354 branch and link followed by a nop. The nop is
16355 replaced by an insn to restore r2. */
16356 else if (offset_in_range (input_section, rel->r_offset, 8))
16358 unsigned long br;
16360 br = bfd_get_32 (input_bfd,
16361 contents + rel->r_offset);
16362 if ((br & 1) != 0)
16364 unsigned long nop;
16366 nop = bfd_get_32 (input_bfd,
16367 contents + rel->r_offset + 4);
16368 if (nop == LD_R2_0R1 + STK_TOC (htab))
16369 can_plt_call = true;
16370 else if (nop == NOP
16371 || nop == CROR_151515
16372 || nop == CROR_313131)
16374 if (h != NULL
16375 && is_tls_get_addr (&h->elf, htab)
16376 && htab->params->tls_get_addr_opt)
16378 /* Special stub used, leave nop alone. */
16380 else
16381 bfd_put_32 (input_bfd,
16382 LD_R2_0R1 + STK_TOC (htab),
16383 contents + rel->r_offset + 4);
16384 can_plt_call = true;
16389 if (!can_plt_call && h != NULL)
16391 const char *name = h->elf.root.root.string;
16393 if (*name == '.')
16394 ++name;
16396 if (startswith (name, "__libc_start_main")
16397 && (name[17] == 0 || name[17] == '@'))
16399 /* Allow crt1 branch to go via a toc adjusting
16400 stub. Other calls that never return could do
16401 the same, if we could detect such. */
16402 can_plt_call = true;
16406 if (!can_plt_call)
16408 /* g++ as of 20130507 emits self-calls without a
16409 following nop. This is arguably wrong since we
16410 have conflicting information. On the one hand a
16411 global symbol and on the other a local call
16412 sequence, but don't error for this special case.
16413 It isn't possible to cheaply verify we have
16414 exactly such a call. Allow all calls to the same
16415 section. */
16416 asection *code_sec = sec;
16418 if (get_opd_info (sec) != NULL)
16420 bfd_vma off = (relocation + addend
16421 - sec->output_section->vma
16422 - sec->output_offset);
16424 opd_entry_value (sec, off, &code_sec, NULL, false);
16426 if (code_sec == input_section)
16427 can_plt_call = true;
16430 if (!can_plt_call)
16432 if (stub_entry->type.main == ppc_stub_plt_call)
16433 info->callbacks->einfo
16434 /* xgettext:c-format */
16435 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16436 "(plt call stub)\n"),
16437 input_bfd, input_section, rel->r_offset, sym_name);
16438 else
16439 info->callbacks->einfo
16440 /* xgettext:c-format */
16441 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16442 "(toc save/adjust stub)\n"),
16443 input_bfd, input_section, rel->r_offset, sym_name);
16445 bfd_set_error (bfd_error_bad_value);
16446 ret = false;
16449 if (can_plt_call
16450 && stub_entry->type.main == ppc_stub_plt_call)
16451 unresolved_reloc = false;
16454 if ((stub_entry == NULL
16455 || stub_entry->type.main == ppc_stub_long_branch
16456 || stub_entry->type.main == ppc_stub_plt_branch)
16457 && get_opd_info (sec) != NULL)
16459 /* The branch destination is the value of the opd entry. */
16460 bfd_vma off = (relocation + addend
16461 - sec->output_section->vma
16462 - sec->output_offset);
16463 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16464 if (dest != (bfd_vma) -1)
16466 relocation = dest;
16467 addend = 0;
16468 reloc_dest = DEST_OPD;
16472 /* If the branch is out of reach we ought to have a long
16473 branch stub. */
16474 from = (rel->r_offset
16475 + input_section->output_offset
16476 + input_section->output_section->vma);
16478 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16479 ? fdh->elf.other
16480 : sym->st_other);
16482 if (stub_entry != NULL
16483 && (stub_entry->type.main == ppc_stub_long_branch
16484 || stub_entry->type.main == ppc_stub_plt_branch))
16486 if (stub_entry->type.sub == ppc_stub_toc
16487 && !stub_entry->type.r2save
16488 && (r_type == R_PPC64_ADDR14_BRTAKEN
16489 || r_type == R_PPC64_ADDR14_BRNTAKEN
16490 || (relocation + addend - from + max_br_offset
16491 < 2 * max_br_offset)))
16492 /* Don't use the stub if this branch is in range. */
16493 stub_entry = NULL;
16495 if (stub_entry != NULL
16496 && stub_entry->type.sub >= ppc_stub_notoc
16497 && ((r_type != R_PPC64_REL24_NOTOC
16498 && r_type != R_PPC64_REL24_P9NOTOC)
16499 || ((fdh ? fdh->elf.other : sym->st_other)
16500 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16501 && (relocation + addend - from + max_br_offset
16502 < 2 * max_br_offset))
16503 stub_entry = NULL;
16505 if (stub_entry != NULL
16506 && stub_entry->type.r2save
16507 && (r_type == R_PPC64_REL24_NOTOC
16508 || r_type == R_PPC64_REL24_P9NOTOC)
16509 && (relocation + addend - from + max_br_offset
16510 < 2 * max_br_offset))
16511 stub_entry = NULL;
16514 if (stub_entry != NULL)
16516 /* Munge up the value and addend so that we call the stub
16517 rather than the procedure directly. */
16518 asection *stub_sec = stub_entry->group->stub_sec;
16520 if (stub_entry->type.main == ppc_stub_save_res)
16521 relocation += (stub_sec->output_offset
16522 + stub_sec->output_section->vma
16523 + stub_sec->size - htab->sfpr->size
16524 - htab->sfpr->output_offset
16525 - htab->sfpr->output_section->vma);
16526 else
16527 relocation = (stub_entry->stub_offset
16528 + stub_sec->output_offset
16529 + stub_sec->output_section->vma);
16530 addend = 0;
16531 reloc_dest = DEST_STUB;
16533 if (((stub_entry->type.r2save
16534 && (r_type == R_PPC64_REL24_NOTOC
16535 || r_type == R_PPC64_REL24_P9NOTOC))
16536 || ((stub_entry->type.main == ppc_stub_plt_call
16537 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16538 && rel + 1 < relend
16539 && rel[1].r_offset == rel->r_offset + 4
16540 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16541 && !(stub_entry->type.main == ppc_stub_plt_call
16542 && htab->params->tls_get_addr_opt
16543 && h != NULL
16544 && is_tls_get_addr (&h->elf, htab)))
16546 /* Skip over the r2 store at the start of the stub. */
16547 relocation += 4;
16550 if ((r_type == R_PPC64_REL24_NOTOC
16551 || r_type == R_PPC64_REL24_P9NOTOC)
16552 && stub_entry->type.main == ppc_stub_plt_call
16553 && stub_entry->type.sub >= ppc_stub_notoc)
16554 htab->notoc_plt = 1;
16557 if (insn != 0)
16559 if (is_isa_v2)
16561 /* Set 'a' bit. This is 0b00010 in BO field for branch
16562 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16563 for branch on CTR insns (BO == 1a00t or 1a01t). */
16564 if ((insn & (0x14 << 21)) == (0x04 << 21))
16565 insn |= 0x02 << 21;
16566 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16567 insn |= 0x08 << 21;
16568 else
16569 break;
16571 else
16573 /* Invert 'y' bit if not the default. */
16574 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16575 insn ^= 0x01 << 21;
16578 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16581 /* NOP out calls to undefined weak functions.
16582 We can thus call a weak function without first
16583 checking whether the function is defined. */
16584 else if (h != NULL
16585 && h->elf.root.type == bfd_link_hash_undefweak
16586 && h->elf.dynindx == -1
16587 && (r_type == R_PPC64_REL24
16588 || r_type == R_PPC64_REL24_NOTOC
16589 || r_type == R_PPC64_REL24_P9NOTOC)
16590 && relocation == 0
16591 && addend == 0
16592 && offset_in_range (input_section, rel->r_offset, 4))
16594 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16595 goto copy_reloc;
16597 break;
16599 case R_PPC64_GOT16_DS:
16600 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16601 || (bfd_link_pic (info)
16602 && sec == bfd_abs_section_ptr)
16603 || !htab->do_toc_opt)
16604 break;
16605 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16606 if (relocation + addend - from + 0x8000 < 0x10000
16607 && sec != NULL
16608 && sec->output_section != NULL
16609 && !discarded_section (sec)
16610 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16611 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16613 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16614 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16616 insn += (14u << 26) - (58u << 26);
16617 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16618 r_type = R_PPC64_TOC16;
16619 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16622 break;
16624 case R_PPC64_GOT16_LO_DS:
16625 case R_PPC64_GOT16_HA:
16626 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16627 || (bfd_link_pic (info)
16628 && sec == bfd_abs_section_ptr)
16629 || !htab->do_toc_opt)
16630 break;
16631 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16632 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16633 && sec != NULL
16634 && sec->output_section != NULL
16635 && !discarded_section (sec)
16636 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16637 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16639 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16640 if (r_type == R_PPC64_GOT16_LO_DS
16641 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16643 insn += (14u << 26) - (58u << 26);
16644 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16645 r_type = R_PPC64_TOC16_LO;
16646 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16648 else if (r_type == R_PPC64_GOT16_HA
16649 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16651 r_type = R_PPC64_TOC16_HA;
16652 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16655 break;
16657 case R_PPC64_GOT_PCREL34:
16658 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16659 || (bfd_link_pic (info)
16660 && sec == bfd_abs_section_ptr)
16661 || !htab->do_toc_opt)
16662 break;
16663 from = (rel->r_offset
16664 + input_section->output_section->vma
16665 + input_section->output_offset);
16666 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16667 && sec != NULL
16668 && sec->output_section != NULL
16669 && !discarded_section (sec)
16670 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16671 && offset_in_range (input_section, rel->r_offset, 8)))
16672 break;
16674 offset = rel->r_offset;
16675 pinsn = bfd_get_32 (input_bfd, contents + offset);
16676 pinsn <<= 32;
16677 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16678 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16679 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16680 break;
16682 /* Replace with paddi. */
16683 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16684 r_type = R_PPC64_PCREL34;
16685 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16686 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16687 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16688 /* Fall through. */
16690 case R_PPC64_PCREL34:
16691 if (!htab->params->no_pcrel_opt
16692 && rel + 1 < relend
16693 && rel[1].r_offset == rel->r_offset
16694 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16695 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16696 && offset_in_range (input_section, rel->r_offset, 8))
16698 offset = rel->r_offset;
16699 pinsn = bfd_get_32 (input_bfd, contents + offset);
16700 pinsn <<= 32;
16701 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16702 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16703 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16704 | (14ULL << 26) /* paddi */))
16706 bfd_vma off2 = rel[1].r_addend;
16707 if (off2 == 0)
16708 /* zero means next insn. */
16709 off2 = 8;
16710 off2 += offset;
16711 if (offset_in_range (input_section, off2, 4))
16713 uint64_t pinsn2;
16714 bfd_signed_vma addend_off;
16715 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16716 pinsn2 <<= 32;
16717 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16719 if (!offset_in_range (input_section, off2, 8))
16720 break;
16721 pinsn2 |= bfd_get_32 (input_bfd,
16722 contents + off2 + 4);
16724 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16726 addend += addend_off;
16727 rel->r_addend = addend;
16728 bfd_put_32 (input_bfd, pinsn >> 32,
16729 contents + offset);
16730 bfd_put_32 (input_bfd, pinsn,
16731 contents + offset + 4);
16732 bfd_put_32 (input_bfd, pinsn2 >> 32,
16733 contents + off2);
16734 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16735 bfd_put_32 (input_bfd, pinsn2,
16736 contents + off2 + 4);
16741 break;
16744 tls_type = 0;
16745 save_unresolved_reloc = unresolved_reloc;
16746 switch (r_type)
16748 default:
16749 /* xgettext:c-format */
16750 _bfd_error_handler (_("%pB: %s unsupported"),
16751 input_bfd, ppc64_elf_howto_table[r_type]->name);
16753 bfd_set_error (bfd_error_bad_value);
16754 ret = false;
16755 goto copy_reloc;
16757 case R_PPC64_NONE:
16758 case R_PPC64_TLS:
16759 case R_PPC64_TLSGD:
16760 case R_PPC64_TLSLD:
16761 case R_PPC64_TOCSAVE:
16762 case R_PPC64_GNU_VTINHERIT:
16763 case R_PPC64_GNU_VTENTRY:
16764 case R_PPC64_ENTRY:
16765 case R_PPC64_PCREL_OPT:
16766 goto copy_reloc;
16768 /* GOT16 relocations. Like an ADDR16 using the symbol's
16769 address in the GOT as relocation value instead of the
16770 symbol's value itself. Also, create a GOT entry for the
16771 symbol and put the symbol value there. */
16772 case R_PPC64_GOT_TLSGD16:
16773 case R_PPC64_GOT_TLSGD16_LO:
16774 case R_PPC64_GOT_TLSGD16_HI:
16775 case R_PPC64_GOT_TLSGD16_HA:
16776 case R_PPC64_GOT_TLSGD_PCREL34:
16777 tls_type = TLS_TLS | TLS_GD;
16778 goto dogot;
16780 case R_PPC64_GOT_TLSLD16:
16781 case R_PPC64_GOT_TLSLD16_LO:
16782 case R_PPC64_GOT_TLSLD16_HI:
16783 case R_PPC64_GOT_TLSLD16_HA:
16784 case R_PPC64_GOT_TLSLD_PCREL34:
16785 tls_type = TLS_TLS | TLS_LD;
16786 goto dogot;
16788 case R_PPC64_GOT_TPREL16_DS:
16789 case R_PPC64_GOT_TPREL16_LO_DS:
16790 case R_PPC64_GOT_TPREL16_HI:
16791 case R_PPC64_GOT_TPREL16_HA:
16792 case R_PPC64_GOT_TPREL_PCREL34:
16793 tls_type = TLS_TLS | TLS_TPREL;
16794 goto dogot;
16796 case R_PPC64_GOT_DTPREL16_DS:
16797 case R_PPC64_GOT_DTPREL16_LO_DS:
16798 case R_PPC64_GOT_DTPREL16_HI:
16799 case R_PPC64_GOT_DTPREL16_HA:
16800 case R_PPC64_GOT_DTPREL_PCREL34:
16801 tls_type = TLS_TLS | TLS_DTPREL;
16802 goto dogot;
16804 case R_PPC64_GOT16:
16805 case R_PPC64_GOT16_LO:
16806 case R_PPC64_GOT16_HI:
16807 case R_PPC64_GOT16_HA:
16808 case R_PPC64_GOT16_DS:
16809 case R_PPC64_GOT16_LO_DS:
16810 case R_PPC64_GOT_PCREL34:
16811 dogot:
16813 /* Relocation is to the entry for this symbol in the global
16814 offset table. */
16815 asection *got;
16816 bfd_vma *offp;
16817 bfd_vma off;
16818 unsigned long indx = 0;
16819 struct got_entry *ent;
16821 if (tls_type == (TLS_TLS | TLS_LD)
16822 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16823 ent = ppc64_tlsld_got (input_bfd);
16824 else
16826 if (h != NULL)
16828 if (!htab->elf.dynamic_sections_created
16829 || h->elf.dynindx == -1
16830 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16831 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16832 /* This is actually a static link, or it is a
16833 -Bsymbolic link and the symbol is defined
16834 locally, or the symbol was forced to be local
16835 because of a version file. */
16837 else
16839 indx = h->elf.dynindx;
16840 unresolved_reloc = false;
16842 ent = h->elf.got.glist;
16844 else
16846 if (local_got_ents == NULL)
16847 abort ();
16848 ent = local_got_ents[r_symndx];
16851 for (; ent != NULL; ent = ent->next)
16852 if (ent->addend == orig_rel.r_addend
16853 && ent->owner == input_bfd
16854 && ent->tls_type == tls_type)
16855 break;
16858 if (ent == NULL)
16859 abort ();
16860 if (ent->is_indirect)
16861 ent = ent->got.ent;
16862 offp = &ent->got.offset;
16863 got = ppc64_elf_tdata (ent->owner)->got;
16864 if (got == NULL)
16865 abort ();
16867 /* The offset must always be a multiple of 8. We use the
16868 least significant bit to record whether we have already
16869 processed this entry. */
16870 off = *offp;
16871 if ((off & 1) != 0)
16872 off &= ~1;
16873 else
16875 /* Generate relocs for the dynamic linker, except in
16876 the case of TLSLD where we'll use one entry per
16877 module. */
16878 asection *relgot;
16879 bool ifunc;
16881 *offp = off | 1;
16882 relgot = NULL;
16883 ifunc = (h != NULL
16884 ? h->elf.type == STT_GNU_IFUNC
16885 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16886 if (ifunc)
16888 relgot = htab->elf.irelplt;
16889 if (indx == 0 || is_static_defined (&h->elf))
16890 htab->elf.ifunc_resolvers = true;
16892 else if (indx != 0
16893 || (bfd_link_pic (info)
16894 && (h == NULL
16895 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16896 && !(tls_type != 0
16897 && bfd_link_executable (info)
16898 && (h == NULL
16899 || SYMBOL_REFERENCES_LOCAL (info,
16900 &h->elf)))
16901 && (h != NULL
16902 ? !bfd_is_abs_symbol (&h->elf.root)
16903 : sym->st_shndx != SHN_ABS)))
16905 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16906 if (relgot != NULL)
16908 outrel.r_offset = (got->output_section->vma
16909 + got->output_offset
16910 + off);
16911 outrel.r_addend = orig_rel.r_addend;
16912 if (tls_type & (TLS_LD | TLS_GD))
16914 outrel.r_addend = 0;
16915 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16916 if (tls_type == (TLS_TLS | TLS_GD))
16918 loc = relgot->contents;
16919 loc += (relgot->reloc_count++
16920 * sizeof (Elf64_External_Rela));
16921 bfd_elf64_swap_reloca_out (output_bfd,
16922 &outrel, loc);
16923 outrel.r_offset += 8;
16924 outrel.r_addend = orig_rel.r_addend;
16925 outrel.r_info
16926 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16929 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16930 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16931 else if (tls_type == (TLS_TLS | TLS_TPREL))
16932 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16933 else if (indx != 0)
16934 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16935 else
16937 if (ifunc)
16938 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16939 else
16940 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16942 /* Write the .got section contents for the sake
16943 of prelink. */
16944 loc = got->contents + off;
16945 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16946 loc);
16949 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16951 outrel.r_addend += relocation;
16952 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16954 if (htab->elf.tls_sec == NULL)
16955 outrel.r_addend = 0;
16956 else
16957 outrel.r_addend -= htab->elf.tls_sec->vma;
16960 if (!(info->enable_dt_relr
16961 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
16963 loc = relgot->contents;
16964 loc += (relgot->reloc_count++
16965 * sizeof (Elf64_External_Rela));
16966 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16970 /* Init the .got section contents here if we're not
16971 emitting a reloc. */
16972 else
16974 relocation += orig_rel.r_addend;
16975 if (tls_type != 0)
16977 if (htab->elf.tls_sec == NULL)
16978 relocation = 0;
16979 else
16981 if (tls_type & TLS_LD)
16982 relocation = 0;
16983 else
16984 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
16985 if (tls_type & TLS_TPREL)
16986 relocation += DTP_OFFSET - TP_OFFSET;
16989 if (tls_type & (TLS_GD | TLS_LD))
16991 bfd_put_64 (output_bfd, relocation,
16992 got->contents + off + 8);
16993 relocation = 1;
16996 bfd_put_64 (output_bfd, relocation,
16997 got->contents + off);
17001 if (off >= (bfd_vma) -2)
17002 abort ();
17004 relocation = got->output_section->vma + got->output_offset + off;
17005 addend = 0;
17006 if (!(r_type == R_PPC64_GOT_PCREL34
17007 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17008 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17009 || r_type == R_PPC64_GOT_TPREL_PCREL34
17010 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17011 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17013 break;
17015 case R_PPC64_PLT16_HA:
17016 case R_PPC64_PLT16_HI:
17017 case R_PPC64_PLT16_LO:
17018 case R_PPC64_PLT16_LO_DS:
17019 case R_PPC64_PLT_PCREL34:
17020 case R_PPC64_PLT_PCREL34_NOTOC:
17021 case R_PPC64_PLT32:
17022 case R_PPC64_PLT64:
17023 case R_PPC64_PLTSEQ:
17024 case R_PPC64_PLTSEQ_NOTOC:
17025 case R_PPC64_PLTCALL:
17026 case R_PPC64_PLTCALL_NOTOC:
17027 /* Relocation is to the entry for this symbol in the
17028 procedure linkage table. */
17029 unresolved_reloc = true;
17031 struct plt_entry **plt_list = NULL;
17032 if (h != NULL)
17033 plt_list = &h->elf.plt.plist;
17034 else if (local_got_ents != NULL)
17036 struct plt_entry **local_plt = (struct plt_entry **)
17037 (local_got_ents + symtab_hdr->sh_info);
17038 plt_list = local_plt + r_symndx;
17040 if (plt_list)
17042 struct plt_entry *ent;
17044 for (ent = *plt_list; ent != NULL; ent = ent->next)
17045 if (ent->plt.offset != (bfd_vma) -1
17046 && ent->addend == orig_rel.r_addend)
17048 asection *plt;
17049 bfd_vma got;
17051 plt = htab->elf.splt;
17052 if (use_local_plt (info, elf_hash_entry (h)))
17054 if (h != NULL
17055 ? h->elf.type == STT_GNU_IFUNC
17056 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17057 plt = htab->elf.iplt;
17058 else
17059 plt = htab->pltlocal;
17061 relocation = (plt->output_section->vma
17062 + plt->output_offset
17063 + ent->plt.offset);
17064 if (r_type == R_PPC64_PLT16_HA
17065 || r_type == R_PPC64_PLT16_HI
17066 || r_type == R_PPC64_PLT16_LO
17067 || r_type == R_PPC64_PLT16_LO_DS)
17069 got = (elf_gp (output_bfd)
17070 + htab->sec_info[input_section->id].toc_off);
17071 relocation -= got;
17073 addend = 0;
17074 unresolved_reloc = false;
17075 break;
17079 break;
17081 case R_PPC64_TOC:
17082 /* Relocation value is TOC base. */
17083 relocation = TOCstart;
17084 if (r_symndx == STN_UNDEF)
17085 relocation += htab->sec_info[input_section->id].toc_off;
17086 else if (unresolved_reloc)
17088 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17089 relocation += htab->sec_info[sec->id].toc_off;
17090 else
17091 unresolved_reloc = true;
17092 if (unresolved_reloc
17093 || (!is_opd
17094 && h != NULL
17095 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17096 info->callbacks->einfo
17097 /* xgettext:c-format */
17098 (_("%H: %s against %pT is not supported\n"),
17099 input_bfd, input_section, rel->r_offset,
17100 ppc64_elf_howto_table[r_type]->name, sym_name);
17101 goto dodyn;
17103 /* TOC16 relocs. We want the offset relative to the TOC base,
17104 which is the address of the start of the TOC plus 0x8000.
17105 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17106 in this order. */
17107 case R_PPC64_TOC16:
17108 case R_PPC64_TOC16_LO:
17109 case R_PPC64_TOC16_HI:
17110 case R_PPC64_TOC16_DS:
17111 case R_PPC64_TOC16_LO_DS:
17112 case R_PPC64_TOC16_HA:
17113 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17114 if (h != NULL)
17115 goto dodyn;
17116 break;
17118 /* Relocate against the beginning of the section. */
17119 case R_PPC64_SECTOFF:
17120 case R_PPC64_SECTOFF_LO:
17121 case R_PPC64_SECTOFF_HI:
17122 case R_PPC64_SECTOFF_DS:
17123 case R_PPC64_SECTOFF_LO_DS:
17124 case R_PPC64_SECTOFF_HA:
17125 if (sec != NULL)
17126 addend -= sec->output_section->vma;
17127 break;
17129 case R_PPC64_REL16:
17130 case R_PPC64_REL16_LO:
17131 case R_PPC64_REL16_HI:
17132 case R_PPC64_REL16_HA:
17133 case R_PPC64_REL16_HIGH:
17134 case R_PPC64_REL16_HIGHA:
17135 case R_PPC64_REL16_HIGHER:
17136 case R_PPC64_REL16_HIGHERA:
17137 case R_PPC64_REL16_HIGHEST:
17138 case R_PPC64_REL16_HIGHESTA:
17139 case R_PPC64_REL16_HIGHER34:
17140 case R_PPC64_REL16_HIGHERA34:
17141 case R_PPC64_REL16_HIGHEST34:
17142 case R_PPC64_REL16_HIGHESTA34:
17143 case R_PPC64_REL16DX_HA:
17144 case R_PPC64_REL14:
17145 case R_PPC64_REL14_BRNTAKEN:
17146 case R_PPC64_REL14_BRTAKEN:
17147 case R_PPC64_REL24:
17148 case R_PPC64_REL24_NOTOC:
17149 case R_PPC64_REL24_P9NOTOC:
17150 case R_PPC64_PCREL34:
17151 case R_PPC64_PCREL28:
17152 break;
17154 case R_PPC64_TPREL16:
17155 case R_PPC64_TPREL16_LO:
17156 case R_PPC64_TPREL16_HI:
17157 case R_PPC64_TPREL16_HA:
17158 case R_PPC64_TPREL16_DS:
17159 case R_PPC64_TPREL16_LO_DS:
17160 case R_PPC64_TPREL16_HIGH:
17161 case R_PPC64_TPREL16_HIGHA:
17162 case R_PPC64_TPREL16_HIGHER:
17163 case R_PPC64_TPREL16_HIGHERA:
17164 case R_PPC64_TPREL16_HIGHEST:
17165 case R_PPC64_TPREL16_HIGHESTA:
17166 if (h != NULL
17167 && h->elf.root.type == bfd_link_hash_undefweak
17168 && h->elf.dynindx == -1
17169 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17171 /* Make this relocation against an undefined weak symbol
17172 resolve to zero. This is really just a tweak, since
17173 code using weak externs ought to check that they are
17174 defined before using them. */
17175 bfd_byte *p = contents + rel->r_offset - d_offset;
17177 insn = bfd_get_32 (input_bfd, p);
17178 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17179 if (insn != 0)
17180 bfd_put_32 (input_bfd, insn, p);
17181 break;
17183 /* Fall through. */
17185 case R_PPC64_TPREL34:
17186 if (htab->elf.tls_sec != NULL)
17187 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17188 /* The TPREL16 relocs shouldn't really be used in shared
17189 libs or with non-local symbols as that will result in
17190 DT_TEXTREL being set, but support them anyway. */
17191 goto dodyn;
17193 case R_PPC64_DTPREL16:
17194 case R_PPC64_DTPREL16_LO:
17195 case R_PPC64_DTPREL16_HI:
17196 case R_PPC64_DTPREL16_HA:
17197 case R_PPC64_DTPREL16_DS:
17198 case R_PPC64_DTPREL16_LO_DS:
17199 case R_PPC64_DTPREL16_HIGH:
17200 case R_PPC64_DTPREL16_HIGHA:
17201 case R_PPC64_DTPREL16_HIGHER:
17202 case R_PPC64_DTPREL16_HIGHERA:
17203 case R_PPC64_DTPREL16_HIGHEST:
17204 case R_PPC64_DTPREL16_HIGHESTA:
17205 case R_PPC64_DTPREL34:
17206 if (htab->elf.tls_sec != NULL)
17207 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17208 break;
17210 case R_PPC64_ADDR64_LOCAL:
17211 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17212 ? h->elf.other
17213 : sym->st_other);
17214 break;
17216 case R_PPC64_DTPMOD64:
17217 relocation = 1;
17218 addend = 0;
17219 goto dodyn;
17221 case R_PPC64_TPREL64:
17222 if (htab->elf.tls_sec != NULL)
17223 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17224 goto dodyn;
17226 case R_PPC64_DTPREL64:
17227 if (htab->elf.tls_sec != NULL)
17228 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17229 /* Fall through. */
17231 /* Relocations that may need to be propagated if this is a
17232 dynamic object. */
17233 case R_PPC64_REL30:
17234 case R_PPC64_REL32:
17235 case R_PPC64_REL64:
17236 case R_PPC64_ADDR14:
17237 case R_PPC64_ADDR14_BRNTAKEN:
17238 case R_PPC64_ADDR14_BRTAKEN:
17239 case R_PPC64_ADDR16:
17240 case R_PPC64_ADDR16_DS:
17241 case R_PPC64_ADDR16_HA:
17242 case R_PPC64_ADDR16_HI:
17243 case R_PPC64_ADDR16_HIGH:
17244 case R_PPC64_ADDR16_HIGHA:
17245 case R_PPC64_ADDR16_HIGHER:
17246 case R_PPC64_ADDR16_HIGHERA:
17247 case R_PPC64_ADDR16_HIGHEST:
17248 case R_PPC64_ADDR16_HIGHESTA:
17249 case R_PPC64_ADDR16_LO:
17250 case R_PPC64_ADDR16_LO_DS:
17251 case R_PPC64_ADDR16_HIGHER34:
17252 case R_PPC64_ADDR16_HIGHERA34:
17253 case R_PPC64_ADDR16_HIGHEST34:
17254 case R_PPC64_ADDR16_HIGHESTA34:
17255 case R_PPC64_ADDR24:
17256 case R_PPC64_ADDR32:
17257 case R_PPC64_ADDR64:
17258 case R_PPC64_UADDR16:
17259 case R_PPC64_UADDR32:
17260 case R_PPC64_UADDR64:
17261 case R_PPC64_D34:
17262 case R_PPC64_D34_LO:
17263 case R_PPC64_D34_HI30:
17264 case R_PPC64_D34_HA30:
17265 case R_PPC64_D28:
17266 dodyn:
17267 if ((input_section->flags & SEC_ALLOC) == 0)
17268 break;
17270 if (NO_OPD_RELOCS && is_opd)
17271 break;
17273 if (bfd_link_pic (info)
17274 ? ((h == NULL
17275 || h->elf.dyn_relocs != NULL)
17276 && ((h != NULL && pc_dynrelocs (h))
17277 || must_be_dyn_reloc (info, r_type)))
17278 : (h != NULL
17279 ? h->elf.dyn_relocs != NULL
17280 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17282 bool skip, relocate;
17283 asection *sreloc;
17284 bfd_vma out_off;
17285 long indx = 0;
17287 /* When generating a dynamic object, these relocations
17288 are copied into the output file to be resolved at run
17289 time. */
17291 skip = false;
17292 relocate = false;
17294 out_off = _bfd_elf_section_offset (output_bfd, info,
17295 input_section, rel->r_offset);
17296 if (out_off == (bfd_vma) -1)
17297 skip = true;
17298 else if (out_off == (bfd_vma) -2)
17299 skip = true, relocate = true;
17300 out_off += (input_section->output_section->vma
17301 + input_section->output_offset);
17302 outrel.r_offset = out_off;
17303 outrel.r_addend = rel->r_addend;
17305 /* Optimize unaligned reloc use. */
17306 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17307 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17308 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17309 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17310 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17311 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17312 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17313 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17314 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17316 if (skip)
17317 memset (&outrel, 0, sizeof outrel);
17318 else if (h != NULL
17319 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17320 && !is_opd
17321 && r_type != R_PPC64_TOC)
17323 indx = h->elf.dynindx;
17324 BFD_ASSERT (indx != -1);
17325 outrel.r_info = ELF64_R_INFO (indx, r_type);
17327 else
17329 /* This symbol is local, or marked to become local,
17330 or this is an opd section reloc which must point
17331 at a local function. */
17332 outrel.r_addend += relocation;
17333 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17335 if (is_opd && h != NULL)
17337 /* Lie about opd entries. This case occurs
17338 when building shared libraries and we
17339 reference a function in another shared
17340 lib. The same thing happens for a weak
17341 definition in an application that's
17342 overridden by a strong definition in a
17343 shared lib. (I believe this is a generic
17344 bug in binutils handling of weak syms.)
17345 In these cases we won't use the opd
17346 entry in this lib. */
17347 unresolved_reloc = false;
17349 if (!is_opd
17350 && r_type == R_PPC64_ADDR64
17351 && (h != NULL
17352 ? h->elf.type == STT_GNU_IFUNC
17353 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17354 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17355 else
17357 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17359 /* We need to relocate .opd contents for ld.so.
17360 Prelink also wants simple and consistent rules
17361 for relocs. This make all RELATIVE relocs have
17362 *r_offset equal to r_addend. */
17363 relocate = true;
17366 else
17368 if (h != NULL
17369 ? h->elf.type == STT_GNU_IFUNC
17370 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17372 info->callbacks->einfo
17373 /* xgettext:c-format */
17374 (_("%H: %s for indirect "
17375 "function `%pT' unsupported\n"),
17376 input_bfd, input_section, rel->r_offset,
17377 ppc64_elf_howto_table[r_type]->name,
17378 sym_name);
17379 ret = false;
17381 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17383 else if (sec == NULL || sec->owner == NULL)
17385 bfd_set_error (bfd_error_bad_value);
17386 return false;
17388 else
17390 asection *osec = sec->output_section;
17392 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17394 /* TLS symbol values are relative to the
17395 TLS segment. Dynamic relocations for
17396 local TLS symbols therefore can't be
17397 reduced to a relocation against their
17398 section symbol because it holds the
17399 address of the section, not a value
17400 relative to the TLS segment. We could
17401 change the .tdata dynamic section symbol
17402 to be zero value but STN_UNDEF works
17403 and is used elsewhere, eg. for TPREL64
17404 GOT relocs against local TLS symbols. */
17405 osec = htab->elf.tls_sec;
17406 indx = 0;
17408 else
17410 indx = elf_section_data (osec)->dynindx;
17411 if (indx == 0)
17413 if ((osec->flags & SEC_READONLY) == 0
17414 && htab->elf.data_index_section != NULL)
17415 osec = htab->elf.data_index_section;
17416 else
17417 osec = htab->elf.text_index_section;
17418 indx = elf_section_data (osec)->dynindx;
17420 BFD_ASSERT (indx != 0);
17423 /* We are turning this relocation into one
17424 against a section symbol, so subtract out
17425 the output section's address but not the
17426 offset of the input section in the output
17427 section. */
17428 outrel.r_addend -= osec->vma;
17431 outrel.r_info = ELF64_R_INFO (indx, r_type);
17435 if (!(info->enable_dt_relr
17436 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17437 && rel->r_offset % 2 == 0
17438 && input_section->alignment_power != 0
17439 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
17441 sreloc = elf_section_data (input_section)->sreloc;
17442 if (h != NULL
17443 ? h->elf.type == STT_GNU_IFUNC
17444 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17446 sreloc = htab->elf.irelplt;
17447 if (indx == 0 || is_static_defined (&h->elf))
17448 htab->elf.ifunc_resolvers = true;
17450 if (sreloc == NULL)
17451 abort ();
17453 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17454 >= sreloc->size)
17455 abort ();
17456 loc = sreloc->contents;
17457 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17458 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17461 if (!warned_dynamic
17462 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17464 info->callbacks->einfo
17465 /* xgettext:c-format */
17466 (_("%X%P: %pB: %s against %pT "
17467 "is not supported by glibc as a dynamic relocation\n"),
17468 input_bfd,
17469 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17470 sym_name);
17471 warned_dynamic = true;
17474 /* If this reloc is against an external symbol, it will
17475 be computed at runtime, so there's no need to do
17476 anything now. However, for the sake of prelink ensure
17477 that the section contents are a known value. */
17478 if (!relocate)
17480 unresolved_reloc = false;
17481 /* The value chosen here is quite arbitrary as ld.so
17482 ignores section contents except for the special
17483 case of .opd where the contents might be accessed
17484 before relocation. Choose zero, as that won't
17485 cause reloc overflow. */
17486 relocation = 0;
17487 addend = 0;
17488 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17489 to improve backward compatibility with older
17490 versions of ld. */
17491 if (r_type == R_PPC64_ADDR64)
17492 addend = outrel.r_addend;
17493 /* Adjust pc_relative relocs to have zero in *r_offset. */
17494 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17495 addend = outrel.r_offset;
17498 break;
17500 case R_PPC64_COPY:
17501 case R_PPC64_GLOB_DAT:
17502 case R_PPC64_JMP_SLOT:
17503 case R_PPC64_JMP_IREL:
17504 case R_PPC64_RELATIVE:
17505 /* We shouldn't ever see these dynamic relocs in relocatable
17506 files. */
17507 /* Fall through. */
17509 case R_PPC64_PLTGOT16:
17510 case R_PPC64_PLTGOT16_DS:
17511 case R_PPC64_PLTGOT16_HA:
17512 case R_PPC64_PLTGOT16_HI:
17513 case R_PPC64_PLTGOT16_LO:
17514 case R_PPC64_PLTGOT16_LO_DS:
17515 case R_PPC64_PLTREL32:
17516 case R_PPC64_PLTREL64:
17517 /* These ones haven't been implemented yet. */
17519 info->callbacks->einfo
17520 /* xgettext:c-format */
17521 (_("%P: %pB: %s is not supported for `%pT'\n"),
17522 input_bfd,
17523 ppc64_elf_howto_table[r_type]->name, sym_name);
17525 bfd_set_error (bfd_error_invalid_operation);
17526 ret = false;
17527 goto copy_reloc;
17530 /* Multi-instruction sequences that access the TOC can be
17531 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17532 to nop; addi rb,r2,x; */
17533 switch (r_type)
17535 default:
17536 break;
17538 case R_PPC64_GOT_TLSLD16_HI:
17539 case R_PPC64_GOT_TLSGD16_HI:
17540 case R_PPC64_GOT_TPREL16_HI:
17541 case R_PPC64_GOT_DTPREL16_HI:
17542 case R_PPC64_GOT16_HI:
17543 case R_PPC64_TOC16_HI:
17544 /* These relocs would only be useful if building up an
17545 offset to later add to r2, perhaps in an indexed
17546 addressing mode instruction. Don't try to optimize.
17547 Unfortunately, the possibility of someone building up an
17548 offset like this or even with the HA relocs, means that
17549 we need to check the high insn when optimizing the low
17550 insn. */
17551 break;
17553 case R_PPC64_PLTCALL_NOTOC:
17554 if (!unresolved_reloc)
17555 htab->notoc_plt = 1;
17556 /* Fall through. */
17557 case R_PPC64_PLTCALL:
17558 if (unresolved_reloc
17559 && offset_in_range (input_section, rel->r_offset,
17560 r_type == R_PPC64_PLTCALL ? 8 : 4))
17562 /* No plt entry. Make this into a direct call. */
17563 bfd_byte *p = contents + rel->r_offset;
17564 insn = bfd_get_32 (input_bfd, p);
17565 insn &= 1;
17566 bfd_put_32 (input_bfd, B_DOT | insn, p);
17567 if (r_type == R_PPC64_PLTCALL)
17568 bfd_put_32 (input_bfd, NOP, p + 4);
17569 unresolved_reloc = save_unresolved_reloc;
17570 r_type = R_PPC64_REL24;
17572 break;
17574 case R_PPC64_PLTSEQ_NOTOC:
17575 case R_PPC64_PLTSEQ:
17576 if (unresolved_reloc)
17578 unresolved_reloc = false;
17579 goto nop_it;
17581 break;
17583 case R_PPC64_PLT_PCREL34_NOTOC:
17584 if (!unresolved_reloc)
17585 htab->notoc_plt = 1;
17586 /* Fall through. */
17587 case R_PPC64_PLT_PCREL34:
17588 if (unresolved_reloc
17589 && offset_in_range (input_section, rel->r_offset, 8))
17591 bfd_byte *p = contents + rel->r_offset;
17592 bfd_put_32 (input_bfd, PNOP >> 32, p);
17593 bfd_put_32 (input_bfd, PNOP, p + 4);
17594 unresolved_reloc = false;
17595 goto copy_reloc;
17597 break;
17599 case R_PPC64_PLT16_HA:
17600 if (unresolved_reloc)
17602 unresolved_reloc = false;
17603 goto nop_it;
17605 /* Fall through. */
17606 case R_PPC64_GOT_TLSLD16_HA:
17607 case R_PPC64_GOT_TLSGD16_HA:
17608 case R_PPC64_GOT_TPREL16_HA:
17609 case R_PPC64_GOT_DTPREL16_HA:
17610 case R_PPC64_GOT16_HA:
17611 case R_PPC64_TOC16_HA:
17612 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17613 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17614 && !(bfd_link_pic (info)
17615 && (h != NULL
17616 ? bfd_is_abs_symbol (&h->elf.root)
17617 : sec == bfd_abs_section_ptr)))
17619 bfd_byte *p;
17620 nop_it:
17621 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17623 p = contents + (rel->r_offset & ~3);
17624 bfd_put_32 (input_bfd, NOP, p);
17625 goto copy_reloc;
17628 break;
17630 case R_PPC64_PLT16_LO:
17631 case R_PPC64_PLT16_LO_DS:
17632 if (unresolved_reloc)
17634 unresolved_reloc = false;
17635 goto nop_it;
17637 /* Fall through. */
17638 case R_PPC64_GOT_TLSLD16_LO:
17639 case R_PPC64_GOT_TLSGD16_LO:
17640 case R_PPC64_GOT_TPREL16_LO_DS:
17641 case R_PPC64_GOT_DTPREL16_LO_DS:
17642 case R_PPC64_GOT16_LO:
17643 case R_PPC64_GOT16_LO_DS:
17644 case R_PPC64_TOC16_LO:
17645 case R_PPC64_TOC16_LO_DS:
17646 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17647 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17648 && !(bfd_link_pic (info)
17649 && (h != NULL
17650 ? bfd_is_abs_symbol (&h->elf.root)
17651 : sec == bfd_abs_section_ptr))
17652 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17654 bfd_byte *p = contents + (rel->r_offset & ~3);
17655 insn = bfd_get_32 (input_bfd, p);
17656 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17658 /* Transform addic to addi when we change reg. */
17659 insn &= ~((0x3fu << 26) | (0x1f << 16));
17660 insn |= (14u << 26) | (2 << 16);
17662 else
17664 insn &= ~(0x1f << 16);
17665 insn |= 2 << 16;
17667 bfd_put_32 (input_bfd, insn, p);
17669 break;
17671 case R_PPC64_TPREL16_HA:
17672 if (htab->do_tls_opt
17673 && relocation + addend + 0x8000 < 0x10000
17674 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17676 bfd_byte *p = contents + (rel->r_offset & ~3);
17677 bfd_put_32 (input_bfd, NOP, p);
17678 goto copy_reloc;
17680 break;
17682 case R_PPC64_TPREL16_LO:
17683 case R_PPC64_TPREL16_LO_DS:
17684 if (htab->do_tls_opt
17685 && relocation + addend + 0x8000 < 0x10000
17686 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17688 bfd_byte *p = contents + (rel->r_offset & ~3);
17689 insn = bfd_get_32 (input_bfd, p);
17690 insn &= ~(0x1f << 16);
17691 insn |= 13 << 16;
17692 bfd_put_32 (input_bfd, insn, p);
17694 break;
17697 /* Do any further special processing. */
17698 switch (r_type)
17700 default:
17701 break;
17703 case R_PPC64_REL16_HA:
17704 case R_PPC64_REL16_HIGHA:
17705 case R_PPC64_REL16_HIGHERA:
17706 case R_PPC64_REL16_HIGHESTA:
17707 case R_PPC64_REL16DX_HA:
17708 case R_PPC64_ADDR16_HA:
17709 case R_PPC64_ADDR16_HIGHA:
17710 case R_PPC64_ADDR16_HIGHERA:
17711 case R_PPC64_ADDR16_HIGHESTA:
17712 case R_PPC64_TOC16_HA:
17713 case R_PPC64_SECTOFF_HA:
17714 case R_PPC64_TPREL16_HA:
17715 case R_PPC64_TPREL16_HIGHA:
17716 case R_PPC64_TPREL16_HIGHERA:
17717 case R_PPC64_TPREL16_HIGHESTA:
17718 case R_PPC64_DTPREL16_HA:
17719 case R_PPC64_DTPREL16_HIGHA:
17720 case R_PPC64_DTPREL16_HIGHERA:
17721 case R_PPC64_DTPREL16_HIGHESTA:
17722 /* It's just possible that this symbol is a weak symbol
17723 that's not actually defined anywhere. In that case,
17724 'sec' would be NULL, and we should leave the symbol
17725 alone (it will be set to zero elsewhere in the link). */
17726 if (sec == NULL)
17727 break;
17728 /* Fall through. */
17730 case R_PPC64_GOT16_HA:
17731 case R_PPC64_PLTGOT16_HA:
17732 case R_PPC64_PLT16_HA:
17733 case R_PPC64_GOT_TLSGD16_HA:
17734 case R_PPC64_GOT_TLSLD16_HA:
17735 case R_PPC64_GOT_TPREL16_HA:
17736 case R_PPC64_GOT_DTPREL16_HA:
17737 /* Add 0x10000 if sign bit in 0:15 is set.
17738 Bits 0:15 are not used. */
17739 addend += 0x8000;
17740 break;
17742 case R_PPC64_D34_HA30:
17743 case R_PPC64_ADDR16_HIGHERA34:
17744 case R_PPC64_ADDR16_HIGHESTA34:
17745 case R_PPC64_REL16_HIGHERA34:
17746 case R_PPC64_REL16_HIGHESTA34:
17747 if (sec != NULL)
17748 addend += 1ULL << 33;
17749 break;
17751 case R_PPC64_ADDR16_DS:
17752 case R_PPC64_ADDR16_LO_DS:
17753 case R_PPC64_GOT16_DS:
17754 case R_PPC64_GOT16_LO_DS:
17755 case R_PPC64_PLT16_LO_DS:
17756 case R_PPC64_SECTOFF_DS:
17757 case R_PPC64_SECTOFF_LO_DS:
17758 case R_PPC64_TOC16_DS:
17759 case R_PPC64_TOC16_LO_DS:
17760 case R_PPC64_PLTGOT16_DS:
17761 case R_PPC64_PLTGOT16_LO_DS:
17762 case R_PPC64_GOT_TPREL16_DS:
17763 case R_PPC64_GOT_TPREL16_LO_DS:
17764 case R_PPC64_GOT_DTPREL16_DS:
17765 case R_PPC64_GOT_DTPREL16_LO_DS:
17766 case R_PPC64_TPREL16_DS:
17767 case R_PPC64_TPREL16_LO_DS:
17768 case R_PPC64_DTPREL16_DS:
17769 case R_PPC64_DTPREL16_LO_DS:
17770 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17771 break;
17772 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17773 mask = 3;
17774 /* If this reloc is against an lq, lxv, or stxv insn, then
17775 the value must be a multiple of 16. This is somewhat of
17776 a hack, but the "correct" way to do this by defining _DQ
17777 forms of all the _DS relocs bloats all reloc switches in
17778 this file. It doesn't make much sense to use these
17779 relocs in data, so testing the insn should be safe. */
17780 if ((insn & (0x3fu << 26)) == (56u << 26)
17781 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17782 mask = 15;
17783 relocation += addend;
17784 addend = insn & (mask ^ 3);
17785 if ((relocation & mask) != 0)
17787 relocation ^= relocation & mask;
17788 info->callbacks->einfo
17789 /* xgettext:c-format */
17790 (_("%H: error: %s not a multiple of %u\n"),
17791 input_bfd, input_section, rel->r_offset,
17792 ppc64_elf_howto_table[r_type]->name,
17793 mask + 1);
17794 bfd_set_error (bfd_error_bad_value);
17795 ret = false;
17796 goto copy_reloc;
17798 break;
17801 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17802 because such sections are not SEC_ALLOC and thus ld.so will
17803 not process them. */
17804 howto = ppc64_elf_howto_table[(int) r_type];
17805 if (unresolved_reloc
17806 && !((input_section->flags & SEC_DEBUGGING) != 0
17807 && h->elf.def_dynamic)
17808 && _bfd_elf_section_offset (output_bfd, info, input_section,
17809 rel->r_offset) != (bfd_vma) -1)
17811 info->callbacks->einfo
17812 /* xgettext:c-format */
17813 (_("%H: unresolvable %s against `%pT'\n"),
17814 input_bfd, input_section, rel->r_offset,
17815 howto->name,
17816 h->elf.root.root.string);
17817 ret = false;
17820 /* 16-bit fields in insns mostly have signed values, but a
17821 few insns have 16-bit unsigned values. Really, we should
17822 have different reloc types. */
17823 if (howto->complain_on_overflow != complain_overflow_dont
17824 && howto->dst_mask == 0xffff
17825 && (input_section->flags & SEC_CODE) != 0
17826 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17828 enum complain_overflow complain = complain_overflow_signed;
17830 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17831 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17832 complain = complain_overflow_bitfield;
17833 else if (howto->rightshift == 0
17834 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17835 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17836 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17837 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17838 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17839 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17840 complain = complain_overflow_unsigned;
17841 if (howto->complain_on_overflow != complain)
17843 alt_howto = *howto;
17844 alt_howto.complain_on_overflow = complain;
17845 howto = &alt_howto;
17849 switch (r_type)
17851 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17852 case R_PPC64_D34:
17853 case R_PPC64_D34_LO:
17854 case R_PPC64_D34_HI30:
17855 case R_PPC64_D34_HA30:
17856 case R_PPC64_PCREL34:
17857 case R_PPC64_GOT_PCREL34:
17858 case R_PPC64_TPREL34:
17859 case R_PPC64_DTPREL34:
17860 case R_PPC64_GOT_TLSGD_PCREL34:
17861 case R_PPC64_GOT_TLSLD_PCREL34:
17862 case R_PPC64_GOT_TPREL_PCREL34:
17863 case R_PPC64_GOT_DTPREL_PCREL34:
17864 case R_PPC64_PLT_PCREL34:
17865 case R_PPC64_PLT_PCREL34_NOTOC:
17866 case R_PPC64_D28:
17867 case R_PPC64_PCREL28:
17868 if (!offset_in_range (input_section, rel->r_offset, 8))
17869 r = bfd_reloc_outofrange;
17870 else
17872 relocation += addend;
17873 if (howto->pc_relative)
17874 relocation -= (rel->r_offset
17875 + input_section->output_offset
17876 + input_section->output_section->vma);
17877 relocation >>= howto->rightshift;
17879 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17880 pinsn <<= 32;
17881 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17883 pinsn &= ~howto->dst_mask;
17884 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17885 & howto->dst_mask);
17886 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17887 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17888 r = bfd_reloc_ok;
17889 if (howto->complain_on_overflow == complain_overflow_signed
17890 && (relocation + (1ULL << (howto->bitsize - 1))
17891 >= 1ULL << howto->bitsize))
17892 r = bfd_reloc_overflow;
17894 break;
17896 case R_PPC64_REL16DX_HA:
17897 if (!offset_in_range (input_section, rel->r_offset, 4))
17898 r = bfd_reloc_outofrange;
17899 else
17901 relocation += addend;
17902 relocation -= (rel->r_offset
17903 + input_section->output_offset
17904 + input_section->output_section->vma);
17905 relocation = (bfd_signed_vma) relocation >> 16;
17906 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17907 insn &= ~0x1fffc1;
17908 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17909 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17910 r = bfd_reloc_ok;
17911 if (relocation + 0x8000 > 0xffff)
17912 r = bfd_reloc_overflow;
17914 break;
17916 default:
17917 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17918 contents, rel->r_offset,
17919 relocation, addend);
17922 if (r != bfd_reloc_ok)
17924 char *more_info = NULL;
17925 const char *reloc_name = howto->name;
17927 if (reloc_dest != DEST_NORMAL)
17929 more_info = bfd_malloc (strlen (reloc_name) + 8);
17930 if (more_info != NULL)
17932 strcpy (more_info, reloc_name);
17933 strcat (more_info, (reloc_dest == DEST_OPD
17934 ? " (OPD)" : " (stub)"));
17935 reloc_name = more_info;
17939 if (r == bfd_reloc_overflow)
17941 /* On code like "if (foo) foo();" don't report overflow
17942 on a branch to zero when foo is undefined. */
17943 if (!warned
17944 && (reloc_dest == DEST_STUB
17945 || !(h != NULL
17946 && (h->elf.root.type == bfd_link_hash_undefweak
17947 || h->elf.root.type == bfd_link_hash_undefined)
17948 && is_branch_reloc (r_type))))
17949 info->callbacks->reloc_overflow
17950 (info, (struct bfd_link_hash_entry *) h, sym_name,
17951 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17952 rel->r_offset);
17954 else
17956 info->callbacks->einfo
17957 /* xgettext:c-format */
17958 (_("%H: %s against `%pT': error %d\n"),
17959 input_bfd, input_section, rel->r_offset,
17960 reloc_name, sym_name, (int) r);
17961 ret = false;
17963 free (more_info);
17965 copy_reloc:
17966 if (wrel != rel)
17967 *wrel = *rel;
17970 if (wrel != rel)
17972 Elf_Internal_Shdr *rel_hdr;
17973 size_t deleted = rel - wrel;
17975 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17976 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17977 if (rel_hdr->sh_size == 0)
17979 /* It is too late to remove an empty reloc section. Leave
17980 one NONE reloc.
17981 ??? What is wrong with an empty section??? */
17982 rel_hdr->sh_size = rel_hdr->sh_entsize;
17983 deleted -= 1;
17985 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17986 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17987 input_section->reloc_count -= deleted;
17990 /* If we're emitting relocations, then shortly after this function
17991 returns, reloc offsets and addends for this section will be
17992 adjusted. Worse, reloc symbol indices will be for the output
17993 file rather than the input. Save a copy of the relocs for
17994 opd_entry_value. */
17995 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
17997 bfd_size_type amt;
17998 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17999 rel = bfd_alloc (input_bfd, amt);
18000 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
18001 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
18002 if (rel == NULL)
18003 return false;
18004 memcpy (rel, relocs, amt);
18006 return ret;
18009 /* Adjust the value of any local symbols in opd sections. */
18011 static int
18012 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18013 const char *name ATTRIBUTE_UNUSED,
18014 Elf_Internal_Sym *elfsym,
18015 asection *input_sec,
18016 struct elf_link_hash_entry *h)
18018 struct _opd_sec_data *opd;
18019 long adjust;
18020 bfd_vma value;
18022 if (h != NULL)
18023 return 1;
18025 opd = get_opd_info (input_sec);
18026 if (opd == NULL || opd->adjust == NULL)
18027 return 1;
18029 value = elfsym->st_value - input_sec->output_offset;
18030 if (!bfd_link_relocatable (info))
18031 value -= input_sec->output_section->vma;
18033 adjust = opd->adjust[OPD_NDX (value)];
18034 if (adjust == -1)
18035 return 2;
18037 elfsym->st_value += adjust;
18038 return 1;
18041 /* Finish up dynamic symbol handling. We set the contents of various
18042 dynamic sections here. */
18044 static bool
18045 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18046 struct bfd_link_info *info,
18047 struct elf_link_hash_entry *h,
18048 Elf_Internal_Sym *sym)
18050 struct ppc_link_hash_table *htab;
18051 struct plt_entry *ent;
18053 htab = ppc_hash_table (info);
18054 if (htab == NULL)
18055 return false;
18057 if (!htab->opd_abi && !h->def_regular)
18058 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18059 if (ent->plt.offset != (bfd_vma) -1)
18061 /* Mark the symbol as undefined, rather than as
18062 defined in glink. Leave the value if there were
18063 any relocations where pointer equality matters
18064 (this is a clue for the dynamic linker, to make
18065 function pointer comparisons work between an
18066 application and shared library), otherwise set it
18067 to zero. */
18068 sym->st_shndx = SHN_UNDEF;
18069 if (!h->pointer_equality_needed)
18070 sym->st_value = 0;
18071 else if (!h->ref_regular_nonweak)
18073 /* This breaks function pointer comparisons, but
18074 that is better than breaking tests for a NULL
18075 function pointer. */
18076 sym->st_value = 0;
18078 break;
18081 if (h->needs_copy
18082 && (h->root.type == bfd_link_hash_defined
18083 || h->root.type == bfd_link_hash_defweak)
18084 && (h->root.u.def.section == htab->elf.sdynbss
18085 || h->root.u.def.section == htab->elf.sdynrelro))
18087 /* This symbol needs a copy reloc. Set it up. */
18088 Elf_Internal_Rela rela;
18089 asection *srel;
18090 bfd_byte *loc;
18092 if (h->dynindx == -1)
18093 abort ();
18095 rela.r_offset = defined_sym_val (h);
18096 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18097 rela.r_addend = 0;
18098 if (h->root.u.def.section == htab->elf.sdynrelro)
18099 srel = htab->elf.sreldynrelro;
18100 else
18101 srel = htab->elf.srelbss;
18102 loc = srel->contents;
18103 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
18104 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
18107 return true;
18110 /* Used to decide how to sort relocs in an optimal manner for the
18111 dynamic linker, before writing them out. */
18113 static enum elf_reloc_type_class
18114 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18115 const asection *rel_sec,
18116 const Elf_Internal_Rela *rela)
18118 enum elf_ppc64_reloc_type r_type;
18119 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18121 if (rel_sec == htab->elf.irelplt)
18122 return reloc_class_ifunc;
18124 r_type = ELF64_R_TYPE (rela->r_info);
18125 switch (r_type)
18127 case R_PPC64_RELATIVE:
18128 return reloc_class_relative;
18129 case R_PPC64_JMP_SLOT:
18130 return reloc_class_plt;
18131 case R_PPC64_COPY:
18132 return reloc_class_copy;
18133 default:
18134 return reloc_class_normal;
18138 /* Finish up the dynamic sections. */
18140 static bool
18141 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18142 struct bfd_link_info *info)
18144 struct ppc_link_hash_table *htab;
18145 bfd *dynobj;
18146 asection *sdyn;
18148 htab = ppc_hash_table (info);
18149 if (htab == NULL)
18150 return false;
18152 dynobj = htab->elf.dynobj;
18153 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18155 if (htab->elf.dynamic_sections_created)
18157 Elf64_External_Dyn *dyncon, *dynconend;
18159 if (sdyn == NULL || htab->elf.sgot == NULL)
18160 abort ();
18162 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18163 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18164 for (; dyncon < dynconend; dyncon++)
18166 Elf_Internal_Dyn dyn;
18167 asection *s;
18169 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18171 switch (dyn.d_tag)
18173 default:
18174 continue;
18176 case DT_PPC64_GLINK:
18177 s = htab->glink;
18178 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18179 /* We stupidly defined DT_PPC64_GLINK to be the start
18180 of glink rather than the first entry point, which is
18181 what ld.so needs, and now have a bigger stub to
18182 support automatic multiple TOCs. */
18183 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18184 break;
18186 case DT_PPC64_OPD:
18187 s = bfd_get_section_by_name (output_bfd, ".opd");
18188 if (s == NULL)
18189 continue;
18190 dyn.d_un.d_ptr = s->vma;
18191 break;
18193 case DT_PPC64_OPT:
18194 if ((htab->do_multi_toc && htab->multi_toc_needed)
18195 || htab->notoc_plt)
18196 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18197 if (htab->has_plt_localentry0)
18198 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18199 break;
18201 case DT_PPC64_OPDSZ:
18202 s = bfd_get_section_by_name (output_bfd, ".opd");
18203 if (s == NULL)
18204 continue;
18205 dyn.d_un.d_val = s->size;
18206 break;
18208 case DT_PLTGOT:
18209 s = htab->elf.splt;
18210 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18211 break;
18213 case DT_JMPREL:
18214 s = htab->elf.srelplt;
18215 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18216 break;
18218 case DT_PLTRELSZ:
18219 dyn.d_un.d_val = htab->elf.srelplt->size;
18220 break;
18222 case DT_TEXTREL:
18223 if (htab->elf.ifunc_resolvers)
18224 info->callbacks->einfo
18225 (_("%P: warning: text relocations and GNU indirect "
18226 "functions may result in a segfault at runtime\n"));
18227 continue;
18230 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18234 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18235 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18237 /* Fill in the first entry in the global offset table.
18238 We use it to hold the link-time TOCbase. */
18239 bfd_put_64 (output_bfd,
18240 elf_gp (output_bfd) + TOC_BASE_OFF,
18241 htab->elf.sgot->contents);
18243 /* Set .got entry size. */
18244 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18245 = 8;
18248 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18249 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18251 /* Set .plt entry size. */
18252 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18253 = PLT_ENTRY_SIZE (htab);
18256 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18257 brlt ourselves if emitrelocations. */
18258 if (htab->brlt != NULL
18259 && htab->brlt->reloc_count != 0
18260 && !_bfd_elf_link_output_relocs (output_bfd,
18261 htab->brlt,
18262 elf_section_data (htab->brlt)->rela.hdr,
18263 elf_section_data (htab->brlt)->relocs,
18264 NULL))
18265 return false;
18267 if (htab->glink != NULL
18268 && htab->glink->reloc_count != 0
18269 && !_bfd_elf_link_output_relocs (output_bfd,
18270 htab->glink,
18271 elf_section_data (htab->glink)->rela.hdr,
18272 elf_section_data (htab->glink)->relocs,
18273 NULL))
18274 return false;
18277 if (htab->glink_eh_frame != NULL
18278 && htab->glink_eh_frame->size != 0
18279 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18280 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18281 htab->glink_eh_frame,
18282 htab->glink_eh_frame->contents))
18283 return false;
18285 /* We need to handle writing out multiple GOT sections ourselves,
18286 since we didn't add them to DYNOBJ. We know dynobj is the first
18287 bfd. */
18288 while ((dynobj = dynobj->link.next) != NULL)
18290 asection *s;
18292 if (!is_ppc64_elf (dynobj))
18293 continue;
18295 s = ppc64_elf_tdata (dynobj)->got;
18296 if (s != NULL
18297 && s->size != 0
18298 && s->output_section != bfd_abs_section_ptr
18299 && !bfd_set_section_contents (output_bfd, s->output_section,
18300 s->contents, s->output_offset,
18301 s->size))
18302 return false;
18303 s = ppc64_elf_tdata (dynobj)->relgot;
18304 if (s != NULL
18305 && s->size != 0
18306 && s->output_section != bfd_abs_section_ptr
18307 && !bfd_set_section_contents (output_bfd, s->output_section,
18308 s->contents, s->output_offset,
18309 s->size))
18310 return false;
18313 return true;
18316 #include "elf64-target.h"
18318 /* FreeBSD support */
18320 #undef TARGET_LITTLE_SYM
18321 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18322 #undef TARGET_LITTLE_NAME
18323 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18325 #undef TARGET_BIG_SYM
18326 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18327 #undef TARGET_BIG_NAME
18328 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18330 #undef ELF_OSABI
18331 #define ELF_OSABI ELFOSABI_FREEBSD
18333 #undef elf64_bed
18334 #define elf64_bed elf64_powerpc_fbsd_bed
18336 #include "elf64-target.h"