]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf64-ppc.c
193e00bc88ded286172252159044cc3077de939c
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2023 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.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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. */
22
23
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 */
27
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
32
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"
42
43 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44 #define OCTETS_PER_BYTE(ABFD, SEC) 1
45
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);
68
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
79
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
91
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
102
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
136
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"
140
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)
144
145 /* The initial size of the plt reserved for the dynamic linker. */
146 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
147
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)
155
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
160
161 /* Offset of tp and dtp pointers from start of TLS block. */
162 #define TP_OFFSET 0x7000
163 #define DTP_OFFSET 0x8000
164
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 */
175
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 */
181
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+ */
189
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) */
193
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 */
197
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
217
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 */
237
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 */
245
246 /* Pad with this. */
247 #define NOP 0x60000000
248
249 /* Some other nops. */
250 #define CROR_151515 0x4def7b82
251 #define CROR_313131 0x4ffffb82
252
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 . */
256
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 */
261
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 */
274
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
282
283 #ifndef ARRAY_SIZE
284 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
285 #endif
286
287 static inline int
288 abiversion (bfd *abfd)
289 {
290 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
291 }
292
293 static inline void
294 set_abiversion (bfd *abfd, int ver)
295 {
296 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
297 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
298 }
299 \f
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)
313
314 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
315
316 static reloc_howto_type ppc64_elf_howto_raw[] =
317 {
318 /* This reloc does nothing. */
319 HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
320 bfd_elf_generic_reloc),
321
322 /* A standard 32 bit relocation. */
323 HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
324 bfd_elf_generic_reloc),
325
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, 4, 26, 0x03fffffc, 0, false, bitfield,
329 bfd_elf_generic_reloc),
330
331 /* A standard 16 bit relocation. */
332 HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
333 bfd_elf_generic_reloc),
334
335 /* A 16 bit relocation without overflow. */
336 HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
337 bfd_elf_generic_reloc),
338
339 /* Bits 16-31 of an address. */
340 HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
341 bfd_elf_generic_reloc),
342
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, 2, 16, 0xffff, 16, false, signed,
346 ppc64_elf_ha_reloc),
347
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, 4, 16, 0x0000fffc, 0, false, signed,
351 ppc64_elf_branch_reloc),
352
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, 4, 16, 0x0000fffc, 0, false, signed,
357 ppc64_elf_brtaken_reloc),
358
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, 4, 16, 0x0000fffc, 0, false, signed,
363 ppc64_elf_brtaken_reloc),
364
365 /* A relative 26 bit branch; the lower two bits must be zero. */
366 HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
367 ppc64_elf_branch_reloc),
368
369 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
370 HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
371 ppc64_elf_branch_reloc),
372
373 /* Another variant, when p10 insns can't be used on stubs. */
374 HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
375 ppc64_elf_branch_reloc),
376
377 /* A relative 16 bit branch; the lower two bits must be zero. */
378 HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
379 ppc64_elf_branch_reloc),
380
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, 4, 16, 0x0000fffc, 0, true, signed,
385 ppc64_elf_brtaken_reloc),
386
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, 4, 16, 0x0000fffc, 0, true, signed,
391 ppc64_elf_brtaken_reloc),
392
393 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
394 symbol. */
395 HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
396 ppc64_elf_unhandled_reloc),
397
398 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
399 the symbol. */
400 HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
401 ppc64_elf_unhandled_reloc),
402
403 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
404 the symbol. */
405 HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
406 ppc64_elf_unhandled_reloc),
407
408 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
409 the symbol. */
410 HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
411 ppc64_elf_unhandled_reloc),
412
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),
420
421 /* Like R_PPC64_ADDR64, but used when setting global offset table
422 entries. */
423 HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
424 ppc64_elf_unhandled_reloc),
425
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),
430
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, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
435 bfd_elf_generic_reloc),
436
437 /* Like R_PPC64_ADDR32, but may be unaligned. */
438 HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
439 bfd_elf_generic_reloc),
440
441 /* Like R_PPC64_ADDR16, but may be unaligned. */
442 HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
443 bfd_elf_generic_reloc),
444
445 /* 32-bit PC relative. */
446 HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
447 bfd_elf_generic_reloc),
448
449 /* 32-bit relocation to the symbol's procedure linkage table. */
450 HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
451 ppc64_elf_unhandled_reloc),
452
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, 4, 32, 0xffffffff, 0, true, signed,
456 ppc64_elf_unhandled_reloc),
457
458 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
459 the symbol. */
460 HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
461 ppc64_elf_unhandled_reloc),
462
463 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
464 the symbol. */
465 HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
466 ppc64_elf_unhandled_reloc),
467
468 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
469 the symbol. */
470 HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
471 ppc64_elf_unhandled_reloc),
472
473 /* 16-bit section relative relocation. */
474 HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
475 ppc64_elf_sectoff_reloc),
476
477 /* Like R_PPC64_SECTOFF, but no overflow warning. */
478 HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
479 ppc64_elf_sectoff_reloc),
480
481 /* 16-bit upper half section relative relocation. */
482 HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
483 ppc64_elf_sectoff_reloc),
484
485 /* 16-bit upper half adjusted section relative relocation. */
486 HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
487 ppc64_elf_sectoff_ha_reloc),
488
489 /* Like R_PPC64_REL24 without touching the two least significant bits. */
490 HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
491 bfd_elf_generic_reloc),
492
493 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
494
495 /* A standard 64-bit relocation. */
496 HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
497 bfd_elf_generic_reloc),
498
499 /* The bits 32-47 of an address. */
500 HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
501 bfd_elf_generic_reloc),
502
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, 2, 16, 0xffff, 32, false, dont,
506 ppc64_elf_ha_reloc),
507
508 /* The bits 48-63 of an address. */
509 HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
510 bfd_elf_generic_reloc),
511
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, 2, 16, 0xffff, 48, false, dont,
515 ppc64_elf_ha_reloc),
516
517 /* Like ADDR64, but may be unaligned. */
518 HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
519 bfd_elf_generic_reloc),
520
521 /* 64-bit relative relocation. */
522 HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
523 bfd_elf_generic_reloc),
524
525 /* 64-bit relocation to the symbol's procedure linkage table. */
526 HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
527 ppc64_elf_unhandled_reloc),
528
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, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
533 ppc64_elf_unhandled_reloc),
534
535 /* 16 bit TOC-relative relocation. */
536 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
537 HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
538 ppc64_elf_toc_reloc),
539
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, 2, 16, 0xffff, 0, false, dont,
543 ppc64_elf_toc_reloc),
544
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, 2, 16, 0xffff, 16, false, signed,
548 ppc64_elf_toc_reloc),
549
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, 2, 16, 0xffff, 16, false, signed,
555 ppc64_elf_toc_ha_reloc),
556
557 /* 64-bit relocation; insert value of TOC base (.TOC.). */
558 /* R_PPC64_TOC 51 doubleword64 .TOC. */
559 HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
560 ppc64_elf_toc64_reloc),
561
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, 2, 16, 0xffff, 0, false,signed,
572 ppc64_elf_unhandled_reloc),
573
574 /* Like R_PPC64_PLTGOT16, but without overflow. */
575 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
576 HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
577 ppc64_elf_unhandled_reloc),
578
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, 2, 16, 0xffff, 16, false, signed,
582 ppc64_elf_unhandled_reloc),
583
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, 2, 16, 0xffff, 16, false, signed,
589 ppc64_elf_unhandled_reloc),
590
591 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
592 HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
593 bfd_elf_generic_reloc),
594
595 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
596 HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
597 bfd_elf_generic_reloc),
598
599 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
600 HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
601 ppc64_elf_unhandled_reloc),
602
603 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
604 HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
605 ppc64_elf_unhandled_reloc),
606
607 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
608 HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
609 ppc64_elf_unhandled_reloc),
610
611 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
612 HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
613 ppc64_elf_sectoff_reloc),
614
615 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
616 HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
617 ppc64_elf_sectoff_reloc),
618
619 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
620 HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
621 ppc64_elf_toc_reloc),
622
623 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
624 HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
625 ppc64_elf_toc_reloc),
626
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, 2, 16, 0xfffc, 0, false, signed,
630 ppc64_elf_unhandled_reloc),
631
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, 2, 16, 0xfffc, 0, false, dont,
635 ppc64_elf_unhandled_reloc),
636
637 /* Marker relocs for TLS. */
638 HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
639 bfd_elf_generic_reloc),
640
641 HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
642 bfd_elf_generic_reloc),
643
644 HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
645 bfd_elf_generic_reloc),
646
647 /* Marker reloc for optimizing r2 save in prologue rather than on
648 each plt call stub. */
649 HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
650 bfd_elf_generic_reloc),
651
652 /* Marker relocs on inline plt call instructions. */
653 HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
654 bfd_elf_generic_reloc),
655
656 HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
657 bfd_elf_generic_reloc),
658
659 /* Computes the load module index of the load module that contains the
660 definition of its TLS sym. */
661 HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
662 ppc64_elf_unhandled_reloc),
663
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, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
668 ppc64_elf_unhandled_reloc),
669
670 /* A 16 bit dtprel reloc. */
671 HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
672 ppc64_elf_unhandled_reloc),
673
674 /* Like DTPREL16, but no overflow. */
675 HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
676 ppc64_elf_unhandled_reloc),
677
678 /* Like DTPREL16_LO, but next higher group of 16 bits. */
679 HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
680 ppc64_elf_unhandled_reloc),
681
682 /* Like DTPREL16_HI, but adjust for low 16 bits. */
683 HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
684 ppc64_elf_unhandled_reloc),
685
686 /* Like DTPREL16_HI, but next higher group of 16 bits. */
687 HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
688 ppc64_elf_unhandled_reloc),
689
690 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
691 HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
692 ppc64_elf_unhandled_reloc),
693
694 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
695 HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
696 ppc64_elf_unhandled_reloc),
697
698 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
699 HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
700 ppc64_elf_unhandled_reloc),
701
702 /* Like DTPREL16, but for insns with a DS field. */
703 HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
704 ppc64_elf_unhandled_reloc),
705
706 /* Like DTPREL16_DS, but no overflow. */
707 HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
708 ppc64_elf_unhandled_reloc),
709
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, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
713 ppc64_elf_unhandled_reloc),
714
715 /* A 16 bit tprel reloc. */
716 HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
717 ppc64_elf_unhandled_reloc),
718
719 /* Like TPREL16, but no overflow. */
720 HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
721 ppc64_elf_unhandled_reloc),
722
723 /* Like TPREL16_LO, but next higher group of 16 bits. */
724 HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
725 ppc64_elf_unhandled_reloc),
726
727 /* Like TPREL16_HI, but adjust for low 16 bits. */
728 HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
729 ppc64_elf_unhandled_reloc),
730
731 /* Like TPREL16_HI, but next higher group of 16 bits. */
732 HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
733 ppc64_elf_unhandled_reloc),
734
735 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
736 HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
737 ppc64_elf_unhandled_reloc),
738
739 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
740 HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
741 ppc64_elf_unhandled_reloc),
742
743 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
744 HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
745 ppc64_elf_unhandled_reloc),
746
747 /* Like TPREL16, but for insns with a DS field. */
748 HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
749 ppc64_elf_unhandled_reloc),
750
751 /* Like TPREL16_DS, but no overflow. */
752 HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
753 ppc64_elf_unhandled_reloc),
754
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, 2, 16, 0xffff, 0, false, signed,
759 ppc64_elf_unhandled_reloc),
760
761 /* Like GOT_TLSGD16, but no overflow. */
762 HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
763 ppc64_elf_unhandled_reloc),
764
765 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
766 HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
767 ppc64_elf_unhandled_reloc),
768
769 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
770 HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
771 ppc64_elf_unhandled_reloc),
772
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, 2, 16, 0xffff, 0, false, signed,
777 ppc64_elf_unhandled_reloc),
778
779 /* Like GOT_TLSLD16, but no overflow. */
780 HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
781 ppc64_elf_unhandled_reloc),
782
783 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
784 HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
785 ppc64_elf_unhandled_reloc),
786
787 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
788 HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
789 ppc64_elf_unhandled_reloc),
790
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, 2, 16, 0xfffc, 0, false, signed,
794 ppc64_elf_unhandled_reloc),
795
796 /* Like GOT_DTPREL16_DS, but no overflow. */
797 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
798 ppc64_elf_unhandled_reloc),
799
800 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
801 HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
802 ppc64_elf_unhandled_reloc),
803
804 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
805 HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
806 ppc64_elf_unhandled_reloc),
807
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, 2, 16, 0xfffc, 0, false, signed,
811 ppc64_elf_unhandled_reloc),
812
813 /* Like GOT_TPREL16_DS, but no overflow. */
814 HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
815 ppc64_elf_unhandled_reloc),
816
817 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
818 HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
819 ppc64_elf_unhandled_reloc),
820
821 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
822 HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
823 ppc64_elf_unhandled_reloc),
824
825 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
826 ppc64_elf_unhandled_reloc),
827
828 HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
829 bfd_elf_generic_reloc),
830
831 /* A 16 bit relative relocation. */
832 HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
833 bfd_elf_generic_reloc),
834
835 /* A 16 bit relative relocation without overflow. */
836 HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
837 bfd_elf_generic_reloc),
838
839 /* The high order 16 bits of a relative address. */
840 HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
841 bfd_elf_generic_reloc),
842
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, 2, 16, 0xffff, 16, true, signed,
846 ppc64_elf_ha_reloc),
847
848 HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
849 bfd_elf_generic_reloc),
850
851 HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
852 ppc64_elf_ha_reloc),
853
854 HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
855 bfd_elf_generic_reloc),
856
857 HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
858 ppc64_elf_ha_reloc),
859
860 HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
861 bfd_elf_generic_reloc),
862
863 HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
864 ppc64_elf_ha_reloc),
865
866 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
867 HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
868 ppc64_elf_ha_reloc),
869
870 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
871 HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
872 ppc64_elf_ha_reloc),
873
874 /* Like R_PPC64_ADDR16_HI, but no overflow. */
875 HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
876 bfd_elf_generic_reloc),
877
878 /* Like R_PPC64_ADDR16_HA, but no overflow. */
879 HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
880 ppc64_elf_ha_reloc),
881
882 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
883 HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
884 ppc64_elf_unhandled_reloc),
885
886 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
887 HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
888 ppc64_elf_unhandled_reloc),
889
890 /* Like R_PPC64_TPREL16_HI, but no overflow. */
891 HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
892 ppc64_elf_unhandled_reloc),
893
894 /* Like R_PPC64_TPREL16_HA, but no overflow. */
895 HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
896 ppc64_elf_unhandled_reloc),
897
898 /* Marker reloc on ELFv2 large-model function entry. */
899 HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
900 bfd_elf_generic_reloc),
901
902 /* Like ADDR64, but use local entry point of function. */
903 HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
904 bfd_elf_generic_reloc),
905
906 HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
907 bfd_elf_generic_reloc),
908
909 HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
910 bfd_elf_generic_reloc),
911
912 HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
913 bfd_elf_generic_reloc),
914
915 HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
916 ppc64_elf_prefix_reloc),
917
918 HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
919 ppc64_elf_prefix_reloc),
920
921 HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
922 ppc64_elf_prefix_reloc),
923
924 HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
925 ppc64_elf_prefix_reloc),
926
927 HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
928 ppc64_elf_prefix_reloc),
929
930 HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
931 ppc64_elf_unhandled_reloc),
932
933 HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
934 ppc64_elf_unhandled_reloc),
935
936 HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
937 ppc64_elf_unhandled_reloc),
938
939 HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
940 ppc64_elf_unhandled_reloc),
941
942 HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
943 ppc64_elf_unhandled_reloc),
944
945 HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
946 ppc64_elf_unhandled_reloc),
947
948 HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
949 ppc64_elf_unhandled_reloc),
950
951 HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
952 ppc64_elf_unhandled_reloc),
953
954 HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
955 ppc64_elf_unhandled_reloc),
956
957 HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
958 bfd_elf_generic_reloc),
959
960 HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
961 ppc64_elf_ha_reloc),
962
963 HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
964 bfd_elf_generic_reloc),
965
966 HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
967 ppc64_elf_ha_reloc),
968
969 HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
970 bfd_elf_generic_reloc),
971
972 HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
973 ppc64_elf_ha_reloc),
974
975 HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
976 bfd_elf_generic_reloc),
977
978 HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
979 ppc64_elf_ha_reloc),
980
981 HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
982 ppc64_elf_prefix_reloc),
983
984 HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
985 ppc64_elf_prefix_reloc),
986
987 /* GNU extension to record C++ vtable hierarchy. */
988 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
989 NULL),
990
991 /* GNU extension to record C++ vtable member usage. */
992 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
993 NULL),
994 };
995
996 \f
997 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
998 be done. */
999
1000 static void
1001 ppc_howto_init (void)
1002 {
1003 unsigned int i, type;
1004
1005 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1006 {
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];
1010 }
1011 }
1012
1013 static reloc_howto_type *
1014 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1015 {
1016 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1017
1018 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1019 /* Initialize howto table if needed. */
1020 ppc_howto_init ();
1021
1022 switch (code)
1023 {
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;
1030
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;
1328 }
1329
1330 return ppc64_elf_howto_table[r];
1331 };
1332
1333 static reloc_howto_type *
1334 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1335 {
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" }
1342 };
1343
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];
1348
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)
1355 {
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]);
1359 }
1360
1361 return NULL;
1362 }
1363
1364 /* Set the howto pointer for a PowerPC ELF reloc. */
1365
1366 static bool
1367 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1368 Elf_Internal_Rela *dst)
1369 {
1370 unsigned int type;
1371
1372 /* Initialize howto table if needed. */
1373 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1374 ppc_howto_init ();
1375
1376 type = ELF64_R_TYPE (dst->r_info);
1377 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1378 {
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;
1384 }
1385 cache_ptr->howto = ppc64_elf_howto_table[type];
1386 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1387 {
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;
1393 }
1394
1395 return true;
1396 }
1397
1398 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1399
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)
1404 {
1405 enum elf_ppc64_reloc_type r_type;
1406 long insn;
1407 bfd_size_type octets;
1408 bfd_vma value;
1409
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);
1416
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;
1430
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;
1441
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;
1446
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;
1454 }
1455
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)
1460 {
1461 if (output_bfd != NULL)
1462 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1463 input_section, output_bfd, error_message);
1464
1465 if (strcmp (symbol->section->name, ".opd") == 0
1466 && (symbol->section->owner->flags & DYNAMIC) == 0)
1467 {
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);
1475 }
1476 else
1477 {
1478 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1479
1480 if (symbol->section->owner != abfd
1481 && symbol->section->owner != NULL
1482 && abiversion (symbol->section->owner) >= 2)
1483 {
1484 unsigned int i;
1485
1486 for (i = 0; i < symbol->section->owner->symcount; ++i)
1487 {
1488 asymbol *symdef = symbol->section->owner->outsymbols[i];
1489
1490 if (strcmp (symdef->name, symbol->name) == 0)
1491 {
1492 elfsym = (elf_symbol_type *) symdef;
1493 break;
1494 }
1495 }
1496 }
1497 reloc_entry->addend
1498 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1499 }
1500 return bfd_reloc_continue;
1501 }
1502
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)
1507 {
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;
1513
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);
1520
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;
1525
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. */
1532
1533 if (is_isa_v2)
1534 {
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;
1544 }
1545 else
1546 {
1547 bfd_vma target = 0;
1548 bfd_vma from;
1549
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;
1555
1556 from = (reloc_entry->address
1557 + input_section->output_offset
1558 + input_section->output_section->vma);
1559
1560 /* Invert 'y' bit if not the default. */
1561 if ((bfd_signed_vma) (target - from) < 0)
1562 insn ^= 0x01 << 21;
1563 }
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);
1568 }
1569
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)
1574 {
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);
1581
1582 /* Subtract the symbol section base address. */
1583 reloc_entry->addend -= symbol->section->output_section->vma;
1584 return bfd_reloc_continue;
1585 }
1586
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)
1591 {
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);
1598
1599 /* Subtract the symbol section base address. */
1600 reloc_entry->addend -= symbol->section->output_section->vma;
1601
1602 /* Adjust the addend for sign extension of the low 16 bits. */
1603 reloc_entry->addend += 0x8000;
1604 return bfd_reloc_continue;
1605 }
1606
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)
1611 {
1612 bfd_vma TOCstart;
1613
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);
1620
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);
1624
1625 /* Subtract the TOC base address. */
1626 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1627 return bfd_reloc_continue;
1628 }
1629
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)
1634 {
1635 bfd_vma TOCstart;
1636
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);
1643
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);
1647
1648 /* Subtract the TOC base address. */
1649 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1650
1651 /* Adjust the addend for sign extension of the low 16 bits. */
1652 reloc_entry->addend += 0x8000;
1653 return bfd_reloc_continue;
1654 }
1655
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)
1660 {
1661 bfd_vma TOCstart;
1662 bfd_size_type octets;
1663
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);
1670
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;
1675
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);
1679
1680 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1681 return bfd_reloc_ok;
1682 }
1683
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)
1688 {
1689 uint64_t insn;
1690 bfd_vma targ;
1691 bfd_size_type octets;
1692
1693 if (output_bfd != NULL)
1694 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1695 input_section, output_bfd, error_message);
1696
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;
1701
1702 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1703 insn <<= 32;
1704 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1705
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)
1714 {
1715 bfd_vma from = (reloc_entry->address
1716 + input_section->output_offset
1717 + input_section->output_section->vma);
1718 targ -=from;
1719 }
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;
1730 }
1731
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)
1736 {
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);
1743
1744 if (error_message != NULL)
1745 {
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;
1752 }
1753 return bfd_reloc_dangerous;
1754 }
1755
1756 /* Track GOT entries needed for a given symbol. We might need more
1757 than one got entry per symbol. */
1758 struct got_entry
1759 {
1760 struct got_entry *next;
1761
1762 /* The symbol addend that we'll be placing in the GOT. */
1763 bfd_vma addend;
1764
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.
1770
1771 Point to the BFD owning this GOT entry. */
1772 bfd *owner;
1773
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;
1777
1778 /* Non-zero if got.ent points to real entry. */
1779 unsigned char is_indirect;
1780
1781 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1782 union
1783 {
1784 bfd_signed_vma refcount;
1785 bfd_vma offset;
1786 struct got_entry *ent;
1787 } got;
1788 };
1789
1790 /* The same for PLT. */
1791 struct plt_entry
1792 {
1793 struct plt_entry *next;
1794
1795 bfd_vma addend;
1796
1797 union
1798 {
1799 bfd_signed_vma refcount;
1800 bfd_vma offset;
1801 } plt;
1802 };
1803
1804 struct ppc64_elf_obj_tdata
1805 {
1806 struct elf_obj_tdata elf;
1807
1808 /* Shortcuts to dynamic linker sections. */
1809 asection *got;
1810 asection *relgot;
1811
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;
1815
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;
1819
1820 union
1821 {
1822 /* A copy of relocs before they are modified for --emit-relocs. */
1823 Elf_Internal_Rela *relocs;
1824
1825 /* Section contents. */
1826 bfd_byte *contents;
1827 } opd;
1828
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;
1832
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;
1836
1837 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1838 this file. */
1839 unsigned int has_optrel : 1;
1840 };
1841
1842 #define ppc64_elf_tdata(bfd) \
1843 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1844
1845 #define ppc64_tlsld_got(bfd) \
1846 (&ppc64_elf_tdata (bfd)->tlsld_got)
1847
1848 #define is_ppc64_elf(bfd) \
1849 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1850 && elf_object_id (bfd) == PPC64_ELF_DATA)
1851
1852 /* Override the generic function because we store some extras. */
1853
1854 static bool
1855 ppc64_elf_mkobject (bfd *abfd)
1856 {
1857 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1858 PPC64_ELF_DATA);
1859 }
1860
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. */
1863
1864 static bool
1865 ppc64_elf_object_p (bfd *abfd)
1866 {
1867 if (!abfd->arch_info->the_default)
1868 return true;
1869
1870 if (abfd->arch_info->bits_per_word == 32)
1871 {
1872 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1873
1874 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1875 {
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);
1879 }
1880 }
1881 return _bfd_elf_ppc_set_arch (abfd);
1882 }
1883
1884 /* Support for core dump NOTE sections. */
1885
1886 static bool
1887 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1888 {
1889 size_t offset, size;
1890
1891 if (note->descsz != 504)
1892 return false;
1893
1894 /* pr_cursig */
1895 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1896
1897 /* pr_pid */
1898 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1899
1900 /* pr_reg */
1901 offset = 112;
1902 size = 384;
1903
1904 /* Make a ".reg/999" section. */
1905 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1906 size, note->descpos + offset);
1907 }
1908
1909 static bool
1910 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1911 {
1912 if (note->descsz != 136)
1913 return false;
1914
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);
1921
1922 return true;
1923 }
1924
1925 static char *
1926 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1927 ...)
1928 {
1929 switch (note_type)
1930 {
1931 default:
1932 return NULL;
1933
1934 case NT_PRPSINFO:
1935 {
1936 char data[136] ATTRIBUTE_NONSTRING;
1937 va_list ap;
1938
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
1947 */
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));
1957 }
1958
1959 case NT_PRSTATUS:
1960 {
1961 char data[504];
1962 va_list ap;
1963 long pid;
1964 int cursig;
1965 const void *greg;
1966
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));
1979 }
1980 }
1981 }
1982
1983 /* Add extra PPC sections. */
1984
1985 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1986 {
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 }
1994 };
1995
1996 enum _ppc64_sec_type {
1997 sec_normal = 0,
1998 sec_opd = 1,
1999 sec_toc = 2,
2000 sec_stub = 3
2001 };
2002
2003 struct _ppc64_elf_section_data
2004 {
2005 struct bfd_elf_section_data elf;
2006
2007 union
2008 {
2009 /* An array with one entry for each opd function descriptor,
2010 and some spares since opd entries may be either 16 or 24 bytes. */
2011 #define OPD_NDX(OFF) ((OFF) >> 4)
2012 struct _opd_sec_data
2013 {
2014 /* Points to the function code section for local opd entries. */
2015 asection **func_sec;
2016
2017 /* After editing .opd, adjust references to opd local syms. */
2018 long *adjust;
2019 } opd;
2020
2021 /* An array for toc sections, indexed by offset/8. */
2022 struct _toc_sec_data
2023 {
2024 /* Specifies the relocation symbol index used at a given toc offset. */
2025 unsigned *symndx;
2026
2027 /* And the relocation addend. */
2028 bfd_vma *add;
2029 } toc;
2030
2031 /* Stub debugging. */
2032 struct ppc_stub_hash_entry *last_ent;
2033 } u;
2034
2035 enum _ppc64_sec_type sec_type:2;
2036
2037 /* Flag set when small branches are detected. Used to
2038 select suitable defaults for the stub group size. */
2039 unsigned int has_14bit_branch:1;
2040
2041 /* Flag set when PLTCALL relocs are detected. */
2042 unsigned int has_pltcall:1;
2043
2044 /* Flag set when section has PLT/GOT/TOC relocations that can be
2045 optimised. */
2046 unsigned int has_optrel:1;
2047 };
2048
2049 #define ppc64_elf_section_data(sec) \
2050 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2051
2052 static bool
2053 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2054 {
2055 if (!sec->used_by_bfd)
2056 {
2057 struct _ppc64_elf_section_data *sdata;
2058 size_t amt = sizeof (*sdata);
2059
2060 sdata = bfd_zalloc (abfd, amt);
2061 if (sdata == NULL)
2062 return false;
2063 sec->used_by_bfd = sdata;
2064 }
2065
2066 return _bfd_elf_new_section_hook (abfd, sec);
2067 }
2068
2069 static bool
2070 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2071 {
2072 const char *name = hdr->bfd_section->name;
2073
2074 if (startswith (name, ".sbss")
2075 || startswith (name, ".sdata"))
2076 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2077
2078 return true;
2079 }
2080
2081 static struct _opd_sec_data *
2082 get_opd_info (asection * sec)
2083 {
2084 if (sec != NULL
2085 && ppc64_elf_section_data (sec) != NULL
2086 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2087 return &ppc64_elf_section_data (sec)->u.opd;
2088 return NULL;
2089 }
2090 \f
2091 /* Parameters for the qsort hook. */
2092 static bool synthetic_relocatable;
2093 static const asection *synthetic_opd;
2094
2095 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2096
2097 static int
2098 compare_symbols (const void *ap, const void *bp)
2099 {
2100 const asymbol *a = *(const asymbol **) ap;
2101 const asymbol *b = *(const asymbol **) bp;
2102
2103 /* Section symbols first. */
2104 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2105 return -1;
2106 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2107 return 1;
2108
2109 /* then .opd symbols. */
2110 if (synthetic_opd != NULL)
2111 {
2112 if (strcmp (a->section->name, ".opd") == 0
2113 && strcmp (b->section->name, ".opd") != 0)
2114 return -1;
2115 if (strcmp (a->section->name, ".opd") != 0
2116 && strcmp (b->section->name, ".opd") == 0)
2117 return 1;
2118 }
2119
2120 /* then other code symbols. */
2121 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2122 == (SEC_CODE | SEC_ALLOC))
2123 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2124 != (SEC_CODE | SEC_ALLOC)))
2125 return -1;
2126
2127 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2128 != (SEC_CODE | SEC_ALLOC))
2129 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2130 == (SEC_CODE | SEC_ALLOC)))
2131 return 1;
2132
2133 if (synthetic_relocatable)
2134 {
2135 if (a->section->id < b->section->id)
2136 return -1;
2137
2138 if (a->section->id > b->section->id)
2139 return 1;
2140 }
2141
2142 if (a->value + a->section->vma < b->value + b->section->vma)
2143 return -1;
2144
2145 if (a->value + a->section->vma > b->value + b->section->vma)
2146 return 1;
2147
2148 /* For syms with the same value, prefer strong dynamic global function
2149 syms over other syms. */
2150 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2151 return -1;
2152
2153 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2154 return 1;
2155
2156 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2157 return -1;
2158
2159 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2160 return 1;
2161
2162 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2163 return -1;
2164
2165 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2166 return 1;
2167
2168 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2169 return -1;
2170
2171 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2172 return 1;
2173
2174 /* Finally, sort on where the symbol is in memory. The symbols will
2175 be in at most two malloc'd blocks, one for static syms, one for
2176 dynamic syms, and we distinguish the two blocks above by testing
2177 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2178 originally in the same order as the symbols (and we're not
2179 sorting the symbols themselves), this ensures a stable sort. */
2180 if (a < b)
2181 return -1;
2182 if (a > b)
2183 return 1;
2184 return 0;
2185 }
2186
2187 /* Search SYMS for a symbol of the given VALUE. */
2188
2189 static asymbol *
2190 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2191 bfd_vma value)
2192 {
2193 size_t mid;
2194
2195 if (id == (unsigned) -1)
2196 {
2197 while (lo < hi)
2198 {
2199 mid = (lo + hi) >> 1;
2200 if (syms[mid]->value + syms[mid]->section->vma < value)
2201 lo = mid + 1;
2202 else if (syms[mid]->value + syms[mid]->section->vma > value)
2203 hi = mid;
2204 else
2205 return syms[mid];
2206 }
2207 }
2208 else
2209 {
2210 while (lo < hi)
2211 {
2212 mid = (lo + hi) >> 1;
2213 if (syms[mid]->section->id < id)
2214 lo = mid + 1;
2215 else if (syms[mid]->section->id > id)
2216 hi = mid;
2217 else if (syms[mid]->value < value)
2218 lo = mid + 1;
2219 else if (syms[mid]->value > value)
2220 hi = mid;
2221 else
2222 return syms[mid];
2223 }
2224 }
2225 return NULL;
2226 }
2227
2228 static bool
2229 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2230 {
2231 bfd_vma vma = *(bfd_vma *) ptr;
2232 return ((section->flags & SEC_ALLOC) != 0
2233 && section->vma <= vma
2234 && vma < section->vma + section->size);
2235 }
2236
2237 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2238 entry syms. Also generate @plt symbols for the glink branch table.
2239 Returns count of synthetic symbols in RET or -1 on error. */
2240
2241 static long
2242 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2243 long static_count, asymbol **static_syms,
2244 long dyn_count, asymbol **dyn_syms,
2245 asymbol **ret)
2246 {
2247 asymbol *s;
2248 size_t i, j, count;
2249 char *names;
2250 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2251 asection *opd = NULL;
2252 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2253 asymbol **syms;
2254 int abi = abiversion (abfd);
2255
2256 *ret = NULL;
2257
2258 if (abi < 2)
2259 {
2260 opd = bfd_get_section_by_name (abfd, ".opd");
2261 if (opd == NULL && abi == 1)
2262 return 0;
2263 }
2264
2265 syms = NULL;
2266 codesecsym = 0;
2267 codesecsymend = 0;
2268 secsymend = 0;
2269 opdsymend = 0;
2270 symcount = 0;
2271 if (opd != NULL)
2272 {
2273 symcount = static_count;
2274 if (!relocatable)
2275 symcount += dyn_count;
2276 if (symcount == 0)
2277 return 0;
2278
2279 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2280 if (syms == NULL)
2281 return -1;
2282
2283 if (!relocatable && static_count != 0 && dyn_count != 0)
2284 {
2285 /* Use both symbol tables. */
2286 memcpy (syms, static_syms, static_count * sizeof (*syms));
2287 memcpy (syms + static_count, dyn_syms,
2288 (dyn_count + 1) * sizeof (*syms));
2289 }
2290 else if (!relocatable && static_count == 0)
2291 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2292 else
2293 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2294
2295 /* Trim uninteresting symbols. Interesting symbols are section,
2296 function, and notype symbols. */
2297 for (i = 0, j = 0; i < symcount; ++i)
2298 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2299 | BSF_RELC | BSF_SRELC)) == 0)
2300 syms[j++] = syms[i];
2301 symcount = j;
2302
2303 synthetic_relocatable = relocatable;
2304 synthetic_opd = opd;
2305 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2306
2307 if (!relocatable && symcount > 1)
2308 {
2309 /* Trim duplicate syms, since we may have merged the normal
2310 and dynamic symbols. Actually, we only care about syms
2311 that have different values, so trim any with the same
2312 value. Don't consider ifunc and ifunc resolver symbols
2313 duplicates however, because GDB wants to know whether a
2314 text symbol is an ifunc resolver. */
2315 for (i = 1, j = 1; i < symcount; ++i)
2316 {
2317 const asymbol *s0 = syms[i - 1];
2318 const asymbol *s1 = syms[i];
2319
2320 if ((s0->value + s0->section->vma
2321 != s1->value + s1->section->vma)
2322 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2323 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2324 syms[j++] = syms[i];
2325 }
2326 symcount = j;
2327 }
2328
2329 i = 0;
2330 /* Note that here and in compare_symbols we can't compare opd and
2331 sym->section directly. With separate debug info files, the
2332 symbols will be extracted from the debug file while abfd passed
2333 to this function is the real binary. */
2334 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2335 && strcmp (syms[i]->section->name, ".opd") == 0)
2336 ++i;
2337 codesecsym = i;
2338
2339 for (; i < symcount; ++i)
2340 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2341 | SEC_THREAD_LOCAL))
2342 != (SEC_CODE | SEC_ALLOC))
2343 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2344 break;
2345 codesecsymend = i;
2346
2347 for (; i < symcount; ++i)
2348 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2349 break;
2350 secsymend = i;
2351
2352 for (; i < symcount; ++i)
2353 if (strcmp (syms[i]->section->name, ".opd") != 0)
2354 break;
2355 opdsymend = i;
2356
2357 for (; i < symcount; ++i)
2358 if (((syms[i]->section->flags
2359 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2360 != (SEC_CODE | SEC_ALLOC))
2361 break;
2362 symcount = i;
2363 }
2364 count = 0;
2365
2366 if (relocatable)
2367 {
2368 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2369 arelent *r;
2370 size_t size;
2371 size_t relcount;
2372
2373 if (opdsymend == secsymend)
2374 goto done;
2375
2376 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2377 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2378 if (relcount == 0)
2379 goto done;
2380
2381 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2382 {
2383 count = -1;
2384 goto done;
2385 }
2386
2387 size = 0;
2388 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2389 {
2390 asymbol *sym;
2391
2392 while (r < opd->relocation + relcount
2393 && r->address < syms[i]->value + opd->vma)
2394 ++r;
2395
2396 if (r == opd->relocation + relcount)
2397 break;
2398
2399 if (r->address != syms[i]->value + opd->vma)
2400 continue;
2401
2402 if (r->howto->type != R_PPC64_ADDR64)
2403 continue;
2404
2405 sym = *r->sym_ptr_ptr;
2406 if (!sym_exists_at (syms, opdsymend, symcount,
2407 sym->section->id, sym->value + r->addend))
2408 {
2409 ++count;
2410 size += sizeof (asymbol);
2411 size += strlen (syms[i]->name) + 2;
2412 }
2413 }
2414
2415 if (size == 0)
2416 goto done;
2417 s = *ret = bfd_malloc (size);
2418 if (s == NULL)
2419 {
2420 count = -1;
2421 goto done;
2422 }
2423
2424 names = (char *) (s + count);
2425
2426 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2427 {
2428 asymbol *sym;
2429
2430 while (r < opd->relocation + relcount
2431 && r->address < syms[i]->value + opd->vma)
2432 ++r;
2433
2434 if (r == opd->relocation + relcount)
2435 break;
2436
2437 if (r->address != syms[i]->value + opd->vma)
2438 continue;
2439
2440 if (r->howto->type != R_PPC64_ADDR64)
2441 continue;
2442
2443 sym = *r->sym_ptr_ptr;
2444 if (!sym_exists_at (syms, opdsymend, symcount,
2445 sym->section->id, sym->value + r->addend))
2446 {
2447 size_t len;
2448
2449 *s = *syms[i];
2450 s->flags |= BSF_SYNTHETIC;
2451 s->section = sym->section;
2452 s->value = sym->value + r->addend;
2453 s->name = names;
2454 *names++ = '.';
2455 len = strlen (syms[i]->name);
2456 memcpy (names, syms[i]->name, len + 1);
2457 names += len + 1;
2458 /* Have udata.p point back to the original symbol this
2459 synthetic symbol was derived from. */
2460 s->udata.p = syms[i];
2461 s++;
2462 }
2463 }
2464 }
2465 else
2466 {
2467 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2468 bfd_byte *contents = NULL;
2469 size_t size;
2470 size_t plt_count = 0;
2471 bfd_vma glink_vma = 0, resolv_vma = 0;
2472 asection *dynamic, *glink = NULL, *relplt = NULL;
2473 arelent *p;
2474
2475 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2476 {
2477 free_contents_and_exit_err:
2478 count = -1;
2479 free_contents_and_exit:
2480 free (contents);
2481 goto done;
2482 }
2483
2484 size = 0;
2485 for (i = secsymend; i < opdsymend; ++i)
2486 {
2487 bfd_vma ent;
2488
2489 /* Ignore bogus symbols. */
2490 if (syms[i]->value > opd->size - 8)
2491 continue;
2492
2493 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2494 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2495 {
2496 ++count;
2497 size += sizeof (asymbol);
2498 size += strlen (syms[i]->name) + 2;
2499 }
2500 }
2501
2502 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2503 if (dyn_count != 0
2504 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2505 {
2506 bfd_byte *dynbuf, *extdyn, *extdynend;
2507 size_t extdynsize;
2508 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2509
2510 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2511 goto free_contents_and_exit_err;
2512
2513 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2514 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2515
2516 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2517 (size_t) (extdynend - extdyn) >= extdynsize;
2518 extdyn += extdynsize)
2519 {
2520 Elf_Internal_Dyn dyn;
2521 (*swap_dyn_in) (abfd, extdyn, &dyn);
2522
2523 if (dyn.d_tag == DT_NULL)
2524 break;
2525
2526 if (dyn.d_tag == DT_PPC64_GLINK)
2527 {
2528 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2529 See comment in ppc64_elf_finish_dynamic_sections. */
2530 glink_vma = dyn.d_un.d_val + 8 * 4;
2531 /* The .glink section usually does not survive the final
2532 link; search for the section (usually .text) where the
2533 glink stubs now reside. */
2534 glink = bfd_sections_find_if (abfd, section_covers_vma,
2535 &glink_vma);
2536 break;
2537 }
2538 }
2539
2540 free (dynbuf);
2541 }
2542
2543 if (glink != NULL)
2544 {
2545 /* Determine __glink trampoline by reading the relative branch
2546 from the first glink stub. */
2547 bfd_byte buf[4];
2548 unsigned int off = 0;
2549
2550 while (bfd_get_section_contents (abfd, glink, buf,
2551 glink_vma + off - glink->vma, 4))
2552 {
2553 unsigned int insn = bfd_get_32 (abfd, buf);
2554 insn ^= B_DOT;
2555 if ((insn & ~0x3fffffc) == 0)
2556 {
2557 resolv_vma
2558 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2559 break;
2560 }
2561 off += 4;
2562 if (off > 4)
2563 break;
2564 }
2565
2566 if (resolv_vma)
2567 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2568
2569 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2570 if (relplt != NULL)
2571 {
2572 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2573 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2574 goto free_contents_and_exit_err;
2575
2576 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2577 size += plt_count * sizeof (asymbol);
2578
2579 p = relplt->relocation;
2580 for (i = 0; i < plt_count; i++, p++)
2581 {
2582 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2583 if (p->addend != 0)
2584 size += sizeof ("+0x") - 1 + 16;
2585 }
2586 }
2587 }
2588
2589 if (size == 0)
2590 goto free_contents_and_exit;
2591 s = *ret = bfd_malloc (size);
2592 if (s == NULL)
2593 goto free_contents_and_exit_err;
2594
2595 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2596
2597 for (i = secsymend; i < opdsymend; ++i)
2598 {
2599 bfd_vma ent;
2600
2601 if (syms[i]->value > opd->size - 8)
2602 continue;
2603
2604 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2605 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2606 {
2607 size_t lo, hi;
2608 size_t len;
2609 asection *sec = abfd->sections;
2610
2611 *s = *syms[i];
2612 lo = codesecsym;
2613 hi = codesecsymend;
2614 while (lo < hi)
2615 {
2616 size_t mid = (lo + hi) >> 1;
2617 if (syms[mid]->section->vma < ent)
2618 lo = mid + 1;
2619 else if (syms[mid]->section->vma > ent)
2620 hi = mid;
2621 else
2622 {
2623 sec = syms[mid]->section;
2624 break;
2625 }
2626 }
2627
2628 if (lo >= hi && lo > codesecsym)
2629 sec = syms[lo - 1]->section;
2630
2631 for (; sec != NULL; sec = sec->next)
2632 {
2633 if (sec->vma > ent)
2634 break;
2635 /* SEC_LOAD may not be set if SEC is from a separate debug
2636 info file. */
2637 if ((sec->flags & SEC_ALLOC) == 0)
2638 break;
2639 if ((sec->flags & SEC_CODE) != 0)
2640 s->section = sec;
2641 }
2642 s->flags |= BSF_SYNTHETIC;
2643 s->value = ent - s->section->vma;
2644 s->name = names;
2645 *names++ = '.';
2646 len = strlen (syms[i]->name);
2647 memcpy (names, syms[i]->name, len + 1);
2648 names += len + 1;
2649 /* Have udata.p point back to the original symbol this
2650 synthetic symbol was derived from. */
2651 s->udata.p = syms[i];
2652 s++;
2653 }
2654 }
2655 free (contents);
2656
2657 if (glink != NULL && relplt != NULL)
2658 {
2659 if (resolv_vma)
2660 {
2661 /* Add a symbol for the main glink trampoline. */
2662 memset (s, 0, sizeof *s);
2663 s->the_bfd = abfd;
2664 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2665 s->section = glink;
2666 s->value = resolv_vma - glink->vma;
2667 s->name = names;
2668 memcpy (names, "__glink_PLTresolve",
2669 sizeof ("__glink_PLTresolve"));
2670 names += sizeof ("__glink_PLTresolve");
2671 s++;
2672 count++;
2673 }
2674
2675 /* FIXME: It would be very much nicer to put sym@plt on the
2676 stub rather than on the glink branch table entry. The
2677 objdump disassembler would then use a sensible symbol
2678 name on plt calls. The difficulty in doing so is
2679 a) finding the stubs, and,
2680 b) matching stubs against plt entries, and,
2681 c) there can be multiple stubs for a given plt entry.
2682
2683 Solving (a) could be done by code scanning, but older
2684 ppc64 binaries used different stubs to current code.
2685 (b) is the tricky one since you need to known the toc
2686 pointer for at least one function that uses a pic stub to
2687 be able to calculate the plt address referenced.
2688 (c) means gdb would need to set multiple breakpoints (or
2689 find the glink branch itself) when setting breakpoints
2690 for pending shared library loads. */
2691 p = relplt->relocation;
2692 for (i = 0; i < plt_count; i++, p++)
2693 {
2694 size_t len;
2695
2696 *s = **p->sym_ptr_ptr;
2697 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2698 we are defining a symbol, ensure one of them is set. */
2699 if ((s->flags & BSF_LOCAL) == 0)
2700 s->flags |= BSF_GLOBAL;
2701 s->flags |= BSF_SYNTHETIC;
2702 s->section = glink;
2703 s->value = glink_vma - glink->vma;
2704 s->name = names;
2705 s->udata.p = NULL;
2706 len = strlen ((*p->sym_ptr_ptr)->name);
2707 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2708 names += len;
2709 if (p->addend != 0)
2710 {
2711 memcpy (names, "+0x", sizeof ("+0x") - 1);
2712 names += sizeof ("+0x") - 1;
2713 bfd_sprintf_vma (abfd, names, p->addend);
2714 names += strlen (names);
2715 }
2716 memcpy (names, "@plt", sizeof ("@plt"));
2717 names += sizeof ("@plt");
2718 s++;
2719 if (abi < 2)
2720 {
2721 glink_vma += 8;
2722 if (i >= 0x8000)
2723 glink_vma += 4;
2724 }
2725 else
2726 glink_vma += 4;
2727 }
2728 count += plt_count;
2729 }
2730 }
2731
2732 done:
2733 free (syms);
2734 return count;
2735 }
2736 \f
2737 /* The following functions are specific to the ELF linker, while
2738 functions above are used generally. Those named ppc64_elf_* are
2739 called by the main ELF linker code. They appear in this file more
2740 or less in the order in which they are called. eg.
2741 ppc64_elf_check_relocs is called early in the link process,
2742 ppc64_elf_finish_dynamic_sections is one of the last functions
2743 called.
2744
2745 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2746 functions have both a function code symbol and a function descriptor
2747 symbol. A call to foo in a relocatable object file looks like:
2748
2749 . .text
2750 . x:
2751 . bl .foo
2752 . nop
2753
2754 The function definition in another object file might be:
2755
2756 . .section .opd
2757 . foo: .quad .foo
2758 . .quad .TOC.@tocbase
2759 . .quad 0
2760 .
2761 . .text
2762 . .foo: blr
2763
2764 When the linker resolves the call during a static link, the branch
2765 unsurprisingly just goes to .foo and the .opd information is unused.
2766 If the function definition is in a shared library, things are a little
2767 different: The call goes via a plt call stub, the opd information gets
2768 copied to the plt, and the linker patches the nop.
2769
2770 . x:
2771 . bl .foo_stub
2772 . ld 2,40(1)
2773 .
2774 .
2775 . .foo_stub:
2776 . std 2,40(1) # in practice, the call stub
2777 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2778 . addi 11,11,Lfoo@toc@l # this is the general idea
2779 . ld 12,0(11)
2780 . ld 2,8(11)
2781 . mtctr 12
2782 . ld 11,16(11)
2783 . bctr
2784 .
2785 . .section .plt
2786 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2787
2788 The "reloc ()" notation is supposed to indicate that the linker emits
2789 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2790 copying.
2791
2792 What are the difficulties here? Well, firstly, the relocations
2793 examined by the linker in check_relocs are against the function code
2794 sym .foo, while the dynamic relocation in the plt is emitted against
2795 the function descriptor symbol, foo. Somewhere along the line, we need
2796 to carefully copy dynamic link information from one symbol to the other.
2797 Secondly, the generic part of the elf linker will make .foo a dynamic
2798 symbol as is normal for most other backends. We need foo dynamic
2799 instead, at least for an application final link. However, when
2800 creating a shared library containing foo, we need to have both symbols
2801 dynamic so that references to .foo are satisfied during the early
2802 stages of linking. Otherwise the linker might decide to pull in a
2803 definition from some other object, eg. a static library.
2804
2805 Update: As of August 2004, we support a new convention. Function
2806 calls may use the function descriptor symbol, ie. "bl foo". This
2807 behaves exactly as "bl .foo". */
2808
2809 /* Of those relocs that might be copied as dynamic relocs, this
2810 function selects those that must be copied when linking a shared
2811 library or PIE, even when the symbol is local. */
2812
2813 static int
2814 must_be_dyn_reloc (struct bfd_link_info *info,
2815 enum elf_ppc64_reloc_type r_type)
2816 {
2817 switch (r_type)
2818 {
2819 default:
2820 /* Only relative relocs can be resolved when the object load
2821 address isn't fixed. DTPREL64 is excluded because the
2822 dynamic linker needs to differentiate global dynamic from
2823 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2824 return 1;
2825
2826 case R_PPC64_REL32:
2827 case R_PPC64_REL64:
2828 case R_PPC64_REL30:
2829 case R_PPC64_TOC16:
2830 case R_PPC64_TOC16_DS:
2831 case R_PPC64_TOC16_LO:
2832 case R_PPC64_TOC16_HI:
2833 case R_PPC64_TOC16_HA:
2834 case R_PPC64_TOC16_LO_DS:
2835 return 0;
2836
2837 case R_PPC64_TPREL16:
2838 case R_PPC64_TPREL16_LO:
2839 case R_PPC64_TPREL16_HI:
2840 case R_PPC64_TPREL16_HA:
2841 case R_PPC64_TPREL16_DS:
2842 case R_PPC64_TPREL16_LO_DS:
2843 case R_PPC64_TPREL16_HIGH:
2844 case R_PPC64_TPREL16_HIGHA:
2845 case R_PPC64_TPREL16_HIGHER:
2846 case R_PPC64_TPREL16_HIGHERA:
2847 case R_PPC64_TPREL16_HIGHEST:
2848 case R_PPC64_TPREL16_HIGHESTA:
2849 case R_PPC64_TPREL64:
2850 case R_PPC64_TPREL34:
2851 /* These relocations are relative but in a shared library the
2852 linker doesn't know the thread pointer base. */
2853 return bfd_link_dll (info);
2854 }
2855 }
2856
2857 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2858 copying dynamic variables from a shared lib into an app's .dynbss
2859 section, and instead use a dynamic relocation to point into the
2860 shared lib. With code that gcc generates it is vital that this be
2861 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2862 actually the address of a function descriptor which resides in the
2863 .opd section. gcc uses the descriptor directly rather than going
2864 via the GOT as some other ABIs do, which means that initialized
2865 function pointers reference the descriptor. Thus, a function
2866 pointer initialized to the address of a function in a shared
2867 library will either require a .dynbss copy and a copy reloc, or a
2868 dynamic reloc. Using a .dynbss copy redefines the function
2869 descriptor symbol to point to the copy. This presents a problem as
2870 a PLT entry for that function is also initialized from the function
2871 descriptor symbol and the copy may not be initialized first. */
2872 #define ELIMINATE_COPY_RELOCS 1
2873
2874 /* Section name for stubs is the associated section name plus this
2875 string. */
2876 #define STUB_SUFFIX ".stub"
2877
2878 /* Linker stubs.
2879 ppc_stub_long_branch:
2880 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2881 destination, but a 24 bit branch in a stub section will reach.
2882 . b dest
2883
2884 ppc_stub_plt_branch:
2885 Similar to the above, but a 24 bit branch in the stub section won't
2886 reach its destination.
2887 . addis %r12,%r2,xxx@toc@ha
2888 . ld %r12,xxx@toc@l(%r12)
2889 . mtctr %r12
2890 . bctr
2891
2892 ppc_stub_plt_call:
2893 Used to call a function in a shared library. If it so happens that
2894 the plt entry referenced crosses a 64k boundary, then an extra
2895 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2896 An r2save variant starts with "std %r2,40(%r1)".
2897 . addis %r11,%r2,xxx@toc@ha
2898 . ld %r12,xxx+0@toc@l(%r11)
2899 . mtctr %r12
2900 . ld %r2,xxx+8@toc@l(%r11)
2901 . ld %r11,xxx+16@toc@l(%r11)
2902 . bctr
2903
2904 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2905 code to adjust the value and save r2 to support multiple toc sections.
2906 A ppc_stub_long_branch with an r2 offset looks like:
2907 . std %r2,40(%r1)
2908 . addis %r2,%r2,off@ha
2909 . addi %r2,%r2,off@l
2910 . b dest
2911
2912 A ppc_stub_plt_branch with an r2 offset looks like:
2913 . std %r2,40(%r1)
2914 . addis %r12,%r2,xxx@toc@ha
2915 . ld %r12,xxx@toc@l(%r12)
2916 . addis %r2,%r2,off@ha
2917 . addi %r2,%r2,off@l
2918 . mtctr %r12
2919 . bctr
2920
2921 All of the above stubs are shown as their ELFv1 variants. ELFv2
2922 variants exist too, simpler for plt calls since a new toc pointer
2923 and static chain are not loaded by the stub. In addition, ELFv2
2924 has some more complex stubs to handle calls marked with NOTOC
2925 relocs from functions where r2 is not a valid toc pointer.
2926 ppc_stub_long_branch_p9notoc:
2927 . mflr %r12
2928 . bcl 20,31,1f
2929 . 1:
2930 . mflr %r11
2931 . mtlr %r12
2932 . addis %r12,%r11,dest-1b@ha
2933 . addi %r12,%r12,dest-1b@l
2934 . b dest
2935
2936 ppc_stub_plt_branch_p9notoc:
2937 . mflr %r12
2938 . bcl 20,31,1f
2939 . 1:
2940 . mflr %r11
2941 . mtlr %r12
2942 . lis %r12,xxx-1b@highest
2943 . ori %r12,%r12,xxx-1b@higher
2944 . sldi %r12,%r12,32
2945 . oris %r12,%r12,xxx-1b@high
2946 . ori %r12,%r12,xxx-1b@l
2947 . add %r12,%r11,%r12
2948 . mtctr %r12
2949 . bctr
2950
2951 ppc_stub_plt_call_p9notoc:
2952 . mflr %r12
2953 . bcl 20,31,1f
2954 . 1:
2955 . mflr %r11
2956 . mtlr %r12
2957 . lis %r12,xxx-1b@highest
2958 . ori %r12,%r12,xxx-1b@higher
2959 . sldi %r12,%r12,32
2960 . oris %r12,%r12,xxx-1b@high
2961 . ori %r12,%r12,xxx-1b@l
2962 . ldx %r12,%r11,%r12
2963 . mtctr %r12
2964 . bctr
2965
2966 There are also ELFv1 power10 variants of these stubs.
2967 ppc_stub_long_branch_notoc:
2968 . pla %r12,dest@pcrel
2969 . b dest
2970 ppc_stub_plt_branch_notoc:
2971 . lis %r11,(dest-1f)@highesta34
2972 . ori %r11,%r11,(dest-1f)@highera34
2973 . sldi %r11,%r11,34
2974 . 1: pla %r12,dest@pcrel
2975 . add %r12,%r11,%r12
2976 . mtctr %r12
2977 . bctr
2978 ppc_stub_plt_call_notoc:
2979 . lis %r11,(xxx-1f)@highesta34
2980 . ori %r11,%r11,(xxx-1f)@highera34
2981 . sldi %r11,%r11,34
2982 . 1: pla %r12,xxx@pcrel
2983 . ldx %r12,%r11,%r12
2984 . mtctr %r12
2985 . bctr
2986
2987 In cases where the high instructions would add zero, they are
2988 omitted and following instructions modified in some cases.
2989 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2990 to
2991 . pld %r12,xxx@pcrel
2992 . mtctr %r12
2993 . bctr
2994
2995 Stub variants may be merged. For example, if printf is called from
2996 code with the tocsave optimization (ie. r2 saved in function
2997 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2998 and from other code without the tocsave optimization requiring a
2999 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
3000 type will be created. Calls with the tocsave optimization will
3001 enter this stub after the instruction saving r2. A similar
3002 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3003 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3004 to call an external function like printf. If other calls to printf
3005 require a ppc_stub_plt_call linkage stub then a single
3006 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3007 call. */
3008
3009 enum ppc_stub_main_type
3010 {
3011 ppc_stub_none,
3012 ppc_stub_long_branch,
3013 ppc_stub_plt_branch,
3014 ppc_stub_plt_call,
3015 ppc_stub_global_entry,
3016 ppc_stub_save_res
3017 };
3018
3019 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3020 these variations. */
3021
3022 enum ppc_stub_sub_type
3023 {
3024 ppc_stub_toc,
3025 ppc_stub_notoc,
3026 ppc_stub_p9notoc
3027 };
3028
3029 struct ppc_stub_type
3030 {
3031 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3032 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3033 unsigned int r2save : 1;
3034 };
3035
3036 /* Information on stub grouping. */
3037 struct map_stub
3038 {
3039 /* The stub section. */
3040 asection *stub_sec;
3041 /* This is the section to which stubs in the group will be attached. */
3042 asection *link_sec;
3043 /* Next group. */
3044 struct map_stub *next;
3045 /* Whether to emit a copy of register save/restore functions in this
3046 group. */
3047 int needs_save_res;
3048 /* Current offset within stubs after the insn restoring lr in a
3049 _notoc or _both stub using bcl for pc-relative addressing, or
3050 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3051 unsigned int lr_restore;
3052 /* Accumulated size of EH info emitted to describe return address
3053 if stubs modify lr. Does not include 17 byte FDE header. */
3054 unsigned int eh_size;
3055 /* Offset in glink_eh_frame to the start of EH info for this group. */
3056 unsigned int eh_base;
3057 };
3058
3059 struct ppc_stub_hash_entry
3060 {
3061 /* Base hash table entry structure. */
3062 struct bfd_hash_entry root;
3063
3064 struct ppc_stub_type type;
3065
3066 /* Group information. */
3067 struct map_stub *group;
3068
3069 /* Offset within stub_sec of the beginning of this stub. */
3070 bfd_vma stub_offset;
3071
3072 /* Given the symbol's value and its section we can determine its final
3073 value when building the stubs (so the stub knows where to jump. */
3074 bfd_vma target_value;
3075 asection *target_section;
3076
3077 /* The symbol table entry, if any, that this was derived from. */
3078 struct ppc_link_hash_entry *h;
3079 struct plt_entry *plt_ent;
3080
3081 /* Symbol type. */
3082 unsigned char symtype;
3083
3084 /* Symbol st_other. */
3085 unsigned char other;
3086
3087 /* Debug: Track hash table traversal. */
3088 unsigned int id;
3089 };
3090
3091 struct ppc_branch_hash_entry
3092 {
3093 /* Base hash table entry structure. */
3094 struct bfd_hash_entry root;
3095
3096 /* Offset within branch lookup table. */
3097 unsigned int offset;
3098
3099 /* Generation marker. */
3100 unsigned int iter;
3101 };
3102
3103 /* Used to track dynamic relocations. */
3104 struct ppc_dyn_relocs
3105 {
3106 struct ppc_dyn_relocs *next;
3107
3108 /* The input section of the reloc. */
3109 asection *sec;
3110
3111 /* Total number of relocs copied for the input section. */
3112 unsigned int count;
3113
3114 /* Number of pc-relative relocs copied for the input section. */
3115 unsigned int pc_count;
3116
3117 /* Number of relocs that might become R_PPC64_RELATIVE. */
3118 unsigned int rel_count;
3119 };
3120
3121 struct ppc_local_dyn_relocs
3122 {
3123 struct ppc_local_dyn_relocs *next;
3124
3125 /* The input section of the reloc. */
3126 asection *sec;
3127
3128 /* Total number of relocs copied for the input section. */
3129 unsigned int count;
3130
3131 /* Number of relocs that might become R_PPC64_RELATIVE. */
3132 unsigned int rel_count : 31;
3133
3134 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3135 unsigned int ifunc : 1;
3136 };
3137
3138 struct ppc_link_hash_entry
3139 {
3140 struct elf_link_hash_entry elf;
3141
3142 union
3143 {
3144 /* A pointer to the most recently used stub hash entry against this
3145 symbol. */
3146 struct ppc_stub_hash_entry *stub_cache;
3147
3148 /* A pointer to the next symbol starting with a '.' */
3149 struct ppc_link_hash_entry *next_dot_sym;
3150 } u;
3151
3152 /* Link between function code and descriptor symbols. */
3153 struct ppc_link_hash_entry *oh;
3154
3155 /* Flag function code and descriptor symbols. */
3156 unsigned int is_func:1;
3157 unsigned int is_func_descriptor:1;
3158 unsigned int fake:1;
3159
3160 /* Whether global opd/toc sym has been adjusted or not.
3161 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3162 should be set for all globals defined in any opd/toc section. */
3163 unsigned int adjust_done:1;
3164
3165 /* Set if this is an out-of-line register save/restore function,
3166 with non-standard calling convention. */
3167 unsigned int save_res:1;
3168
3169 /* Set if a duplicate symbol with non-zero localentry is detected,
3170 even when the duplicate symbol does not provide a definition. */
3171 unsigned int non_zero_localentry:1;
3172
3173 /* Contexts in which symbol is used in the GOT (or TOC).
3174 Bits are or'd into the mask as the corresponding relocs are
3175 encountered during check_relocs, with TLS_TLS being set when any
3176 of the other TLS bits are set. tls_optimize clears bits when
3177 optimizing to indicate the corresponding GOT entry type is not
3178 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3179 set TLS_GDIE when a GD reloc turns into an IE one.
3180 These flags are also kept for local symbols. */
3181 #define TLS_TLS 1 /* Any TLS reloc. */
3182 #define TLS_GD 2 /* GD reloc. */
3183 #define TLS_LD 4 /* LD reloc. */
3184 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3185 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3186 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3187 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3188 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3189 unsigned char tls_mask;
3190
3191 /* The above field is also used to mark function symbols. In which
3192 case TLS_TLS will be 0. */
3193 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3194 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3195 #define NON_GOT 256 /* local symbol plt, not stored. */
3196 };
3197
3198 static inline struct ppc_link_hash_entry *
3199 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3200 {
3201 return (struct ppc_link_hash_entry *) ent;
3202 }
3203
3204 static inline struct elf_link_hash_entry *
3205 elf_hash_entry (struct ppc_link_hash_entry *ent)
3206 {
3207 return (struct elf_link_hash_entry *) ent;
3208 }
3209
3210 /* ppc64 ELF linker hash table. */
3211
3212 struct ppc_link_hash_table
3213 {
3214 struct elf_link_hash_table elf;
3215
3216 /* The stub hash table. */
3217 struct bfd_hash_table stub_hash_table;
3218
3219 /* Another hash table for plt_branch stubs. */
3220 struct bfd_hash_table branch_hash_table;
3221
3222 /* Hash table for function prologue tocsave. */
3223 htab_t tocsave_htab;
3224
3225 /* Various options and other info passed from the linker. */
3226 struct ppc64_elf_params *params;
3227
3228 /* The size of sec_info below. */
3229 unsigned int sec_info_arr_size;
3230
3231 /* Per-section array of extra section info. Done this way rather
3232 than as part of ppc64_elf_section_data so we have the info for
3233 non-ppc64 sections. */
3234 struct
3235 {
3236 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3237 bfd_vma toc_off;
3238
3239 union
3240 {
3241 /* The section group that this section belongs to. */
3242 struct map_stub *group;
3243 /* A temp section list pointer. */
3244 asection *list;
3245 } u;
3246 } *sec_info;
3247
3248 /* Linked list of groups. */
3249 struct map_stub *group;
3250
3251 /* Temp used when calculating TOC pointers. */
3252 bfd_vma toc_curr;
3253 bfd *toc_bfd;
3254 asection *toc_first_sec;
3255
3256 /* Used when adding symbols. */
3257 struct ppc_link_hash_entry *dot_syms;
3258
3259 /* Shortcuts to get to dynamic linker sections. */
3260 asection *glink;
3261 asection *global_entry;
3262 asection *sfpr;
3263 asection *pltlocal;
3264 asection *relpltlocal;
3265 asection *brlt;
3266 asection *relbrlt;
3267 asection *glink_eh_frame;
3268
3269 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3270 struct ppc_link_hash_entry *tls_get_addr;
3271 struct ppc_link_hash_entry *tls_get_addr_fd;
3272 struct ppc_link_hash_entry *tga_desc;
3273 struct ppc_link_hash_entry *tga_desc_fd;
3274 struct map_stub *tga_group;
3275
3276 /* The size of reliplt used by got entry relocs. */
3277 bfd_size_type got_reli_size;
3278
3279 /* DT_RELR array of section/r_offset. */
3280 size_t relr_alloc;
3281 size_t relr_count;
3282 struct
3283 {
3284 asection *sec;
3285 bfd_vma off;
3286 } *relr;
3287
3288 /* Statistics. */
3289 unsigned long stub_count[ppc_stub_save_res];
3290
3291 /* Number of stubs against global syms. */
3292 unsigned long stub_globals;
3293
3294 /* Set if we're linking code with function descriptors. */
3295 unsigned int opd_abi:1;
3296
3297 /* Support for multiple toc sections. */
3298 unsigned int do_multi_toc:1;
3299 unsigned int multi_toc_needed:1;
3300 unsigned int second_toc_pass:1;
3301 unsigned int do_toc_opt:1;
3302
3303 /* Set if tls optimization is enabled. */
3304 unsigned int do_tls_opt:1;
3305
3306 /* Set if inline plt calls should be converted to direct calls. */
3307 unsigned int can_convert_all_inline_plt:1;
3308
3309 /* Set if a stub_offset changed. */
3310 unsigned int stub_changed:1;
3311
3312 /* Set on error. */
3313 unsigned int stub_error:1;
3314
3315 /* Whether func_desc_adjust needs to be run over symbols. */
3316 unsigned int need_func_desc_adj:1;
3317
3318 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3319 unsigned int has_plt_localentry0:1;
3320
3321 /* Whether calls are made via the PLT from NOTOC functions. */
3322 unsigned int notoc_plt:1;
3323
3324 /* Whether any code linked seems to be Power10. */
3325 unsigned int has_power10_relocs:1;
3326
3327 /* Incremented once for each stub sized. */
3328 unsigned int stub_id;
3329
3330 /* Incremented every time we size stubs. */
3331 unsigned int stub_iteration;
3332
3333 /* After 20 iterations of stub sizing we no longer allow stubs to
3334 shrink. This is to break out of a pathological case where adding
3335 stubs or increasing their size on one iteration decreases section
3336 gaps (perhaps due to alignment), which then results in smaller
3337 stubs on the next iteration. */
3338 #define STUB_SHRINK_ITER 20
3339 };
3340
3341 /* Rename some of the generic section flags to better document how they
3342 are used here. */
3343
3344 /* Nonzero if this section has TLS related relocations. */
3345 #define has_tls_reloc sec_flg0
3346
3347 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3348 relocations. */
3349 #define nomark_tls_get_addr sec_flg1
3350
3351 /* Nonzero if this section has any toc or got relocs. */
3352 #define has_toc_reloc sec_flg2
3353
3354 /* Nonzero if this section has a call to another section that uses
3355 the toc or got. */
3356 #define makes_toc_func_call sec_flg3
3357
3358 /* Recursion protection when determining above flag. */
3359 #define call_check_in_progress sec_flg4
3360 #define call_check_done sec_flg5
3361
3362 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3363
3364 #define ppc_hash_table(p) \
3365 ((is_elf_hash_table ((p)->hash) \
3366 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3367 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3368
3369 #define ppc_stub_hash_lookup(table, string, create, copy) \
3370 ((struct ppc_stub_hash_entry *) \
3371 bfd_hash_lookup ((table), (string), (create), (copy)))
3372
3373 #define ppc_branch_hash_lookup(table, string, create, copy) \
3374 ((struct ppc_branch_hash_entry *) \
3375 bfd_hash_lookup ((table), (string), (create), (copy)))
3376
3377 /* Create an entry in the stub hash table. */
3378
3379 static struct bfd_hash_entry *
3380 stub_hash_newfunc (struct bfd_hash_entry *entry,
3381 struct bfd_hash_table *table,
3382 const char *string)
3383 {
3384 /* Allocate the structure if it has not already been allocated by a
3385 subclass. */
3386 if (entry == NULL)
3387 {
3388 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3389 if (entry == NULL)
3390 return entry;
3391 }
3392
3393 /* Call the allocation method of the superclass. */
3394 entry = bfd_hash_newfunc (entry, table, string);
3395 if (entry != NULL)
3396 {
3397 struct ppc_stub_hash_entry *eh;
3398
3399 /* Initialize the local fields. */
3400 eh = (struct ppc_stub_hash_entry *) entry;
3401 eh->type.main = ppc_stub_none;
3402 eh->type.sub = ppc_stub_toc;
3403 eh->type.r2save = 0;
3404 eh->group = NULL;
3405 eh->stub_offset = 0;
3406 eh->target_value = 0;
3407 eh->target_section = NULL;
3408 eh->h = NULL;
3409 eh->plt_ent = NULL;
3410 eh->symtype = 0;
3411 eh->other = 0;
3412 eh->id = 0;
3413 }
3414
3415 return entry;
3416 }
3417
3418 /* Create an entry in the branch hash table. */
3419
3420 static struct bfd_hash_entry *
3421 branch_hash_newfunc (struct bfd_hash_entry *entry,
3422 struct bfd_hash_table *table,
3423 const char *string)
3424 {
3425 /* Allocate the structure if it has not already been allocated by a
3426 subclass. */
3427 if (entry == NULL)
3428 {
3429 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3430 if (entry == NULL)
3431 return entry;
3432 }
3433
3434 /* Call the allocation method of the superclass. */
3435 entry = bfd_hash_newfunc (entry, table, string);
3436 if (entry != NULL)
3437 {
3438 struct ppc_branch_hash_entry *eh;
3439
3440 /* Initialize the local fields. */
3441 eh = (struct ppc_branch_hash_entry *) entry;
3442 eh->offset = 0;
3443 eh->iter = 0;
3444 }
3445
3446 return entry;
3447 }
3448
3449 /* Create an entry in a ppc64 ELF linker hash table. */
3450
3451 static struct bfd_hash_entry *
3452 link_hash_newfunc (struct bfd_hash_entry *entry,
3453 struct bfd_hash_table *table,
3454 const char *string)
3455 {
3456 /* Allocate the structure if it has not already been allocated by a
3457 subclass. */
3458 if (entry == NULL)
3459 {
3460 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3461 if (entry == NULL)
3462 return entry;
3463 }
3464
3465 /* Call the allocation method of the superclass. */
3466 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3467 if (entry != NULL)
3468 {
3469 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3470
3471 memset (&eh->u.stub_cache, 0,
3472 (sizeof (struct ppc_link_hash_entry)
3473 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3474
3475 /* When making function calls, old ABI code references function entry
3476 points (dot symbols), while new ABI code references the function
3477 descriptor symbol. We need to make any combination of reference and
3478 definition work together, without breaking archive linking.
3479
3480 For a defined function "foo" and an undefined call to "bar":
3481 An old object defines "foo" and ".foo", references ".bar" (possibly
3482 "bar" too).
3483 A new object defines "foo" and references "bar".
3484
3485 A new object thus has no problem with its undefined symbols being
3486 satisfied by definitions in an old object. On the other hand, the
3487 old object won't have ".bar" satisfied by a new object.
3488
3489 Keep a list of newly added dot-symbols. */
3490
3491 if (string[0] == '.')
3492 {
3493 struct ppc_link_hash_table *htab;
3494
3495 htab = (struct ppc_link_hash_table *) table;
3496 eh->u.next_dot_sym = htab->dot_syms;
3497 htab->dot_syms = eh;
3498 }
3499 }
3500
3501 return entry;
3502 }
3503
3504 struct tocsave_entry
3505 {
3506 asection *sec;
3507 bfd_vma offset;
3508 };
3509
3510 static hashval_t
3511 tocsave_htab_hash (const void *p)
3512 {
3513 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3514 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3515 }
3516
3517 static int
3518 tocsave_htab_eq (const void *p1, const void *p2)
3519 {
3520 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3521 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3522 return e1->sec == e2->sec && e1->offset == e2->offset;
3523 }
3524
3525 /* Destroy a ppc64 ELF linker hash table. */
3526
3527 static void
3528 ppc64_elf_link_hash_table_free (bfd *obfd)
3529 {
3530 struct ppc_link_hash_table *htab;
3531
3532 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3533 if (htab->tocsave_htab)
3534 htab_delete (htab->tocsave_htab);
3535 bfd_hash_table_free (&htab->branch_hash_table);
3536 bfd_hash_table_free (&htab->stub_hash_table);
3537 _bfd_elf_link_hash_table_free (obfd);
3538 }
3539
3540 /* Create a ppc64 ELF linker hash table. */
3541
3542 static struct bfd_link_hash_table *
3543 ppc64_elf_link_hash_table_create (bfd *abfd)
3544 {
3545 struct ppc_link_hash_table *htab;
3546 size_t amt = sizeof (struct ppc_link_hash_table);
3547
3548 htab = bfd_zmalloc (amt);
3549 if (htab == NULL)
3550 return NULL;
3551
3552 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3553 sizeof (struct ppc_link_hash_entry),
3554 PPC64_ELF_DATA))
3555 {
3556 free (htab);
3557 return NULL;
3558 }
3559
3560 /* Init the stub hash table too. */
3561 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3562 sizeof (struct ppc_stub_hash_entry)))
3563 {
3564 _bfd_elf_link_hash_table_free (abfd);
3565 return NULL;
3566 }
3567
3568 /* And the branch hash table. */
3569 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3570 sizeof (struct ppc_branch_hash_entry)))
3571 {
3572 bfd_hash_table_free (&htab->stub_hash_table);
3573 _bfd_elf_link_hash_table_free (abfd);
3574 return NULL;
3575 }
3576
3577 htab->tocsave_htab = htab_try_create (1024,
3578 tocsave_htab_hash,
3579 tocsave_htab_eq,
3580 NULL);
3581 if (htab->tocsave_htab == NULL)
3582 {
3583 ppc64_elf_link_hash_table_free (abfd);
3584 return NULL;
3585 }
3586 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3587
3588 /* Initializing two fields of the union is just cosmetic. We really
3589 only care about glist, but when compiled on a 32-bit host the
3590 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3591 debugger inspection of these fields look nicer. */
3592 htab->elf.init_got_refcount.refcount = 0;
3593 htab->elf.init_got_refcount.glist = NULL;
3594 htab->elf.init_plt_refcount.refcount = 0;
3595 htab->elf.init_plt_refcount.glist = NULL;
3596 htab->elf.init_got_offset.offset = 0;
3597 htab->elf.init_got_offset.glist = NULL;
3598 htab->elf.init_plt_offset.offset = 0;
3599 htab->elf.init_plt_offset.glist = NULL;
3600
3601 return &htab->elf.root;
3602 }
3603
3604 /* Create sections for linker generated code. */
3605
3606 static bool
3607 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3608 {
3609 struct ppc_link_hash_table *htab;
3610 flagword flags;
3611
3612 htab = ppc_hash_table (info);
3613
3614 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3615 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3616 if (htab->params->save_restore_funcs)
3617 {
3618 /* Create .sfpr for code to save and restore fp regs. */
3619 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3620 flags);
3621 if (htab->sfpr == NULL
3622 || !bfd_set_section_alignment (htab->sfpr, 2))
3623 return false;
3624 }
3625
3626 if (bfd_link_relocatable (info))
3627 return true;
3628
3629 /* Create .glink for lazy dynamic linking support. */
3630 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3631 flags);
3632 if (htab->glink == NULL
3633 || !bfd_set_section_alignment (htab->glink, 3))
3634 return false;
3635
3636 /* The part of .glink used by global entry stubs, separate so that
3637 it can be aligned appropriately without affecting htab->glink. */
3638 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3639 flags);
3640 if (htab->global_entry == NULL
3641 || !bfd_set_section_alignment (htab->global_entry, 2))
3642 return false;
3643
3644 if (!info->no_ld_generated_unwind_info)
3645 {
3646 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3647 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3648 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3649 ".eh_frame",
3650 flags);
3651 if (htab->glink_eh_frame == NULL
3652 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3653 return false;
3654 }
3655
3656 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3657 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3658 if (htab->elf.iplt == NULL
3659 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3660 return false;
3661
3662 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3663 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3664 htab->elf.irelplt
3665 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3666 if (htab->elf.irelplt == NULL
3667 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3668 return false;
3669
3670 /* Create branch lookup table for plt_branch stubs. */
3671 flags = (SEC_ALLOC | SEC_LOAD
3672 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3673 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3674 flags);
3675 if (htab->brlt == NULL
3676 || !bfd_set_section_alignment (htab->brlt, 3))
3677 return false;
3678
3679 /* Local plt entries, put in .branch_lt but a separate section for
3680 convenience. */
3681 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3682 flags);
3683 if (htab->pltlocal == NULL
3684 || !bfd_set_section_alignment (htab->pltlocal, 3))
3685 return false;
3686
3687 if (!bfd_link_pic (info))
3688 return true;
3689
3690 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3691 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3692 htab->relbrlt
3693 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3694 if (htab->relbrlt == NULL
3695 || !bfd_set_section_alignment (htab->relbrlt, 3))
3696 return false;
3697
3698 htab->relpltlocal
3699 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3700 if (htab->relpltlocal == NULL
3701 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3702 return false;
3703
3704 return true;
3705 }
3706
3707 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3708
3709 bool
3710 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3711 struct ppc64_elf_params *params)
3712 {
3713 struct ppc_link_hash_table *htab;
3714
3715 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3716
3717 /* Always hook our dynamic sections into the first bfd, which is the
3718 linker created stub bfd. This ensures that the GOT header is at
3719 the start of the output TOC section. */
3720 htab = ppc_hash_table (info);
3721 htab->elf.dynobj = params->stub_bfd;
3722 htab->params = params;
3723
3724 return create_linkage_sections (htab->elf.dynobj, info);
3725 }
3726
3727 /* Build a name for an entry in the stub hash table. */
3728
3729 static char *
3730 ppc_stub_name (const asection *input_section,
3731 const asection *sym_sec,
3732 const struct ppc_link_hash_entry *h,
3733 const Elf_Internal_Rela *rel)
3734 {
3735 char *stub_name;
3736 ssize_t len;
3737
3738 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3739 offsets from a sym as a branch target? In fact, we could
3740 probably assume the addend is always zero. */
3741 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3742
3743 if (h)
3744 {
3745 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3746 stub_name = bfd_malloc (len);
3747 if (stub_name == NULL)
3748 return stub_name;
3749
3750 len = sprintf (stub_name, "%08x.%s+%x",
3751 input_section->id & 0xffffffff,
3752 h->elf.root.root.string,
3753 (int) rel->r_addend & 0xffffffff);
3754 }
3755 else
3756 {
3757 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3758 stub_name = bfd_malloc (len);
3759 if (stub_name == NULL)
3760 return stub_name;
3761
3762 len = sprintf (stub_name, "%08x.%x:%x+%x",
3763 input_section->id & 0xffffffff,
3764 sym_sec->id & 0xffffffff,
3765 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3766 (int) rel->r_addend & 0xffffffff);
3767 }
3768 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3769 stub_name[len - 2] = 0;
3770 return stub_name;
3771 }
3772
3773 /* If mixing power10 with non-power10 code and --power10-stubs is not
3774 specified (or is auto) then there may be multiple stub types for any
3775 given symbol. Up to three classes of stubs are stored in separate
3776 stub_hash_table entries having the same key string. The entries
3777 will always be adjacent on entry->root.next chain, even if hash
3778 table resizing occurs. This function selects the correct entry to
3779 use. */
3780
3781 static struct ppc_stub_hash_entry *
3782 select_alt_stub (struct ppc_stub_hash_entry *entry,
3783 enum elf_ppc64_reloc_type r_type)
3784 {
3785 enum ppc_stub_sub_type subt;
3786
3787 switch (r_type)
3788 {
3789 case R_PPC64_REL24_NOTOC:
3790 subt = ppc_stub_notoc;
3791 break;
3792 case R_PPC64_REL24_P9NOTOC:
3793 subt = ppc_stub_p9notoc;
3794 break;
3795 default:
3796 subt = ppc_stub_toc;
3797 break;
3798 }
3799
3800 while (entry != NULL && entry->type.sub != subt)
3801 {
3802 const char *stub_name = entry->root.string;
3803
3804 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3805 if (entry != NULL
3806 && entry->root.string != stub_name)
3807 entry = NULL;
3808 }
3809
3810 return entry;
3811 }
3812
3813 /* Look up an entry in the stub hash. Stub entries are cached because
3814 creating the stub name takes a bit of time. */
3815
3816 static struct ppc_stub_hash_entry *
3817 ppc_get_stub_entry (const asection *input_section,
3818 const asection *sym_sec,
3819 struct ppc_link_hash_entry *h,
3820 const Elf_Internal_Rela *rel,
3821 struct ppc_link_hash_table *htab)
3822 {
3823 struct ppc_stub_hash_entry *stub_entry;
3824 struct map_stub *group;
3825
3826 /* If this input section is part of a group of sections sharing one
3827 stub section, then use the id of the first section in the group.
3828 Stub names need to include a section id, as there may well be
3829 more than one stub used to reach say, printf, and we need to
3830 distinguish between them. */
3831 group = htab->sec_info[input_section->id].u.group;
3832 if (group == NULL)
3833 return NULL;
3834
3835 if (h != NULL && h->u.stub_cache != NULL
3836 && h->u.stub_cache->h == h
3837 && h->u.stub_cache->group == group)
3838 {
3839 stub_entry = h->u.stub_cache;
3840 }
3841 else
3842 {
3843 char *stub_name;
3844
3845 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3846 if (stub_name == NULL)
3847 return NULL;
3848
3849 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3850 stub_name, false, false);
3851 if (h != NULL)
3852 h->u.stub_cache = stub_entry;
3853
3854 free (stub_name);
3855 }
3856
3857 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3858 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3859
3860 return stub_entry;
3861 }
3862
3863 /* Add a new stub entry to the stub hash. Not all fields of the new
3864 stub entry are initialised. */
3865
3866 static struct ppc_stub_hash_entry *
3867 ppc_add_stub (const char *stub_name,
3868 asection *section,
3869 struct bfd_link_info *info)
3870 {
3871 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3872 struct map_stub *group;
3873 asection *link_sec;
3874 asection *stub_sec;
3875 struct ppc_stub_hash_entry *stub_entry;
3876
3877 group = htab->sec_info[section->id].u.group;
3878 link_sec = group->link_sec;
3879 stub_sec = group->stub_sec;
3880 if (stub_sec == NULL)
3881 {
3882 size_t namelen;
3883 bfd_size_type len;
3884 char *s_name;
3885
3886 namelen = strlen (link_sec->name);
3887 len = namelen + sizeof (STUB_SUFFIX);
3888 s_name = bfd_alloc (htab->params->stub_bfd, len);
3889 if (s_name == NULL)
3890 return NULL;
3891
3892 memcpy (s_name, link_sec->name, namelen);
3893 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3894 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3895 if (stub_sec == NULL)
3896 return NULL;
3897 group->stub_sec = stub_sec;
3898 }
3899
3900 /* Enter this entry into the linker stub hash table. */
3901 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3902 true, false);
3903 if (stub_entry == NULL)
3904 {
3905 /* xgettext:c-format */
3906 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3907 section->owner, stub_name);
3908 return NULL;
3909 }
3910
3911 stub_entry->group = group;
3912 stub_entry->stub_offset = 0;
3913 return stub_entry;
3914 }
3915
3916 /* A stub has already been created, but it may not be the required
3917 type. We shouldn't be transitioning from plt_call to long_branch
3918 stubs or vice versa, but we might be upgrading from plt_call to
3919 plt_call with r2save for example. */
3920
3921 static bool
3922 ppc_merge_stub (struct ppc_link_hash_table *htab,
3923 struct ppc_stub_hash_entry *stub_entry,
3924 struct ppc_stub_type stub_type,
3925 enum elf_ppc64_reloc_type r_type)
3926 {
3927 struct ppc_stub_type old_type = stub_entry->type;
3928
3929 if (old_type.main == ppc_stub_save_res)
3930 return true;
3931
3932 if (htab->params->power10_stubs == -1)
3933 {
3934 /* For --power10-stubs=auto, don't merge _notoc and other
3935 varieties of stubs. */
3936 struct ppc_stub_hash_entry *alt_stub;
3937
3938 alt_stub = select_alt_stub (stub_entry, r_type);
3939 if (alt_stub == NULL)
3940 {
3941 alt_stub = ((struct ppc_stub_hash_entry *)
3942 stub_hash_newfunc (NULL,
3943 &htab->stub_hash_table,
3944 stub_entry->root.string));
3945 if (alt_stub == NULL)
3946 return false;
3947
3948 *alt_stub = *stub_entry;
3949 stub_entry->root.next = &alt_stub->root;
3950
3951 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3952 Not that it matters, this just puts smaller stubs first. */
3953 if (stub_type.sub == ppc_stub_notoc)
3954 alt_stub = stub_entry;
3955 else if (stub_type.sub == ppc_stub_p9notoc
3956 && alt_stub->root.next
3957 && alt_stub->root.next->string == alt_stub->root.string)
3958 {
3959 struct ppc_stub_hash_entry *next
3960 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3961 alt_stub->type = next->type;
3962 alt_stub = next;
3963 }
3964 alt_stub->type = stub_type;
3965 return true;
3966 }
3967 stub_entry = alt_stub;
3968 }
3969
3970 old_type = stub_entry->type;
3971 if (old_type.main == ppc_stub_plt_branch)
3972 old_type.main = ppc_stub_long_branch;
3973
3974 if (old_type.main != stub_type.main
3975 || (old_type.sub != stub_type.sub
3976 && old_type.sub != ppc_stub_toc
3977 && stub_type.sub != ppc_stub_toc))
3978 abort ();
3979
3980 stub_entry->type.sub |= stub_type.sub;
3981 stub_entry->type.r2save |= stub_type.r2save;
3982 return true;
3983 }
3984
3985 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3986 not already done. */
3987
3988 static bool
3989 create_got_section (bfd *abfd, struct bfd_link_info *info)
3990 {
3991 asection *got, *relgot;
3992 flagword flags;
3993 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3994
3995 if (!is_ppc64_elf (abfd))
3996 return false;
3997 if (htab == NULL)
3998 return false;
3999
4000 if (!htab->elf.sgot
4001 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4002 return false;
4003
4004 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4005 | SEC_LINKER_CREATED);
4006
4007 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4008 if (!got
4009 || !bfd_set_section_alignment (got, 3))
4010 return false;
4011
4012 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4013 flags | SEC_READONLY);
4014 if (!relgot
4015 || !bfd_set_section_alignment (relgot, 3))
4016 return false;
4017
4018 ppc64_elf_tdata (abfd)->got = got;
4019 ppc64_elf_tdata (abfd)->relgot = relgot;
4020 return true;
4021 }
4022
4023 /* Follow indirect and warning symbol links. */
4024
4025 static inline struct bfd_link_hash_entry *
4026 follow_link (struct bfd_link_hash_entry *h)
4027 {
4028 while (h->type == bfd_link_hash_indirect
4029 || h->type == bfd_link_hash_warning)
4030 h = h->u.i.link;
4031 return h;
4032 }
4033
4034 static inline struct elf_link_hash_entry *
4035 elf_follow_link (struct elf_link_hash_entry *h)
4036 {
4037 return (struct elf_link_hash_entry *) follow_link (&h->root);
4038 }
4039
4040 static inline struct ppc_link_hash_entry *
4041 ppc_follow_link (struct ppc_link_hash_entry *h)
4042 {
4043 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4044 }
4045
4046 /* Merge PLT info on FROM with that on TO. */
4047
4048 static void
4049 move_plt_plist (struct ppc_link_hash_entry *from,
4050 struct ppc_link_hash_entry *to)
4051 {
4052 if (from->elf.plt.plist != NULL)
4053 {
4054 if (to->elf.plt.plist != NULL)
4055 {
4056 struct plt_entry **entp;
4057 struct plt_entry *ent;
4058
4059 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4060 {
4061 struct plt_entry *dent;
4062
4063 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4064 if (dent->addend == ent->addend)
4065 {
4066 dent->plt.refcount += ent->plt.refcount;
4067 *entp = ent->next;
4068 break;
4069 }
4070 if (dent == NULL)
4071 entp = &ent->next;
4072 }
4073 *entp = to->elf.plt.plist;
4074 }
4075
4076 to->elf.plt.plist = from->elf.plt.plist;
4077 from->elf.plt.plist = NULL;
4078 }
4079 }
4080
4081 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4082
4083 static void
4084 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4085 struct elf_link_hash_entry *dir,
4086 struct elf_link_hash_entry *ind)
4087 {
4088 struct ppc_link_hash_entry *edir, *eind;
4089
4090 edir = ppc_elf_hash_entry (dir);
4091 eind = ppc_elf_hash_entry (ind);
4092
4093 edir->is_func |= eind->is_func;
4094 edir->is_func_descriptor |= eind->is_func_descriptor;
4095 edir->tls_mask |= eind->tls_mask;
4096 if (eind->oh != NULL)
4097 edir->oh = ppc_follow_link (eind->oh);
4098
4099 if (edir->elf.versioned != versioned_hidden)
4100 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4101 edir->elf.ref_regular |= eind->elf.ref_regular;
4102 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4103 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4104 edir->elf.needs_plt |= eind->elf.needs_plt;
4105 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4106
4107 /* If we were called to copy over info for a weak sym, don't copy
4108 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4109 in order to simplify readonly_dynrelocs and save a field in the
4110 symbol hash entry, but that means dyn_relocs can't be used in any
4111 tests about a specific symbol, or affect other symbol flags which
4112 are then tested. */
4113 if (eind->elf.root.type != bfd_link_hash_indirect)
4114 return;
4115
4116 /* Copy over any dynamic relocs we may have on the indirect sym. */
4117 if (ind->dyn_relocs != NULL)
4118 {
4119 if (dir->dyn_relocs != NULL)
4120 {
4121 struct ppc_dyn_relocs **pp;
4122 struct ppc_dyn_relocs *p;
4123
4124 /* Add reloc counts against the indirect sym to the direct sym
4125 list. Merge any entries against the same section. */
4126 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4127 (p = *pp) != NULL;
4128 )
4129 {
4130 struct ppc_dyn_relocs *q;
4131
4132 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4133 q != NULL;
4134 q = q->next)
4135 if (q->sec == p->sec)
4136 {
4137 q->count += p->count;
4138 q->pc_count += p->pc_count;
4139 q->rel_count += p->rel_count;
4140 *pp = p->next;
4141 break;
4142 }
4143 if (q == NULL)
4144 pp = &p->next;
4145 }
4146 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4147 }
4148
4149 dir->dyn_relocs = ind->dyn_relocs;
4150 ind->dyn_relocs = NULL;
4151 }
4152
4153 /* Copy over got entries that we may have already seen to the
4154 symbol which just became indirect. */
4155 if (eind->elf.got.glist != NULL)
4156 {
4157 if (edir->elf.got.glist != NULL)
4158 {
4159 struct got_entry **entp;
4160 struct got_entry *ent;
4161
4162 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4163 {
4164 struct got_entry *dent;
4165
4166 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4167 if (dent->addend == ent->addend
4168 && dent->owner == ent->owner
4169 && dent->tls_type == ent->tls_type)
4170 {
4171 dent->got.refcount += ent->got.refcount;
4172 *entp = ent->next;
4173 break;
4174 }
4175 if (dent == NULL)
4176 entp = &ent->next;
4177 }
4178 *entp = edir->elf.got.glist;
4179 }
4180
4181 edir->elf.got.glist = eind->elf.got.glist;
4182 eind->elf.got.glist = NULL;
4183 }
4184
4185 /* And plt entries. */
4186 move_plt_plist (eind, edir);
4187
4188 if (eind->elf.dynindx != -1)
4189 {
4190 if (edir->elf.dynindx != -1)
4191 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4192 edir->elf.dynstr_index);
4193 edir->elf.dynindx = eind->elf.dynindx;
4194 edir->elf.dynstr_index = eind->elf.dynstr_index;
4195 eind->elf.dynindx = -1;
4196 eind->elf.dynstr_index = 0;
4197 }
4198 }
4199
4200 /* Find the function descriptor hash entry from the given function code
4201 hash entry FH. Link the entries via their OH fields. */
4202
4203 static struct ppc_link_hash_entry *
4204 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4205 {
4206 struct ppc_link_hash_entry *fdh = fh->oh;
4207
4208 if (fdh == NULL)
4209 {
4210 const char *fd_name = fh->elf.root.root.string + 1;
4211
4212 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4213 false, false, false));
4214 if (fdh == NULL)
4215 return fdh;
4216
4217 fdh->is_func_descriptor = 1;
4218 fdh->oh = fh;
4219 fh->is_func = 1;
4220 fh->oh = fdh;
4221 }
4222
4223 fdh = ppc_follow_link (fdh);
4224 fdh->is_func_descriptor = 1;
4225 fdh->oh = fh;
4226 return fdh;
4227 }
4228
4229 /* Make a fake function descriptor sym for the undefined code sym FH. */
4230
4231 static struct ppc_link_hash_entry *
4232 make_fdh (struct bfd_link_info *info,
4233 struct ppc_link_hash_entry *fh)
4234 {
4235 bfd *abfd = fh->elf.root.u.undef.abfd;
4236 struct bfd_link_hash_entry *bh = NULL;
4237 struct ppc_link_hash_entry *fdh;
4238 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4239 ? BSF_WEAK
4240 : BSF_GLOBAL);
4241
4242 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4243 fh->elf.root.root.string + 1,
4244 flags, bfd_und_section_ptr, 0,
4245 NULL, false, false, &bh))
4246 return NULL;
4247
4248 fdh = (struct ppc_link_hash_entry *) bh;
4249 fdh->elf.non_elf = 0;
4250 fdh->fake = 1;
4251 fdh->is_func_descriptor = 1;
4252 fdh->oh = fh;
4253 fh->is_func = 1;
4254 fh->oh = fdh;
4255 return fdh;
4256 }
4257
4258 /* Fix function descriptor symbols defined in .opd sections to be
4259 function type. */
4260
4261 static bool
4262 ppc64_elf_add_symbol_hook (bfd *ibfd,
4263 struct bfd_link_info *info,
4264 Elf_Internal_Sym *isym,
4265 const char **name,
4266 flagword *flags ATTRIBUTE_UNUSED,
4267 asection **sec,
4268 bfd_vma *value)
4269 {
4270 if (*sec != NULL
4271 && strcmp ((*sec)->name, ".opd") == 0)
4272 {
4273 asection *code_sec;
4274
4275 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4276 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4277 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4278
4279 /* If the symbol is a function defined in .opd, and the function
4280 code is in a discarded group, let it appear to be undefined. */
4281 if (!bfd_link_relocatable (info)
4282 && (*sec)->reloc_count != 0
4283 && opd_entry_value (*sec, *value, &code_sec, NULL,
4284 false) != (bfd_vma) -1
4285 && discarded_section (code_sec))
4286 {
4287 *sec = bfd_und_section_ptr;
4288 isym->st_shndx = SHN_UNDEF;
4289 }
4290 }
4291 else if (*sec != NULL
4292 && strcmp ((*sec)->name, ".toc") == 0
4293 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4294 {
4295 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4296 if (htab != NULL)
4297 htab->params->object_in_toc = 1;
4298 }
4299
4300 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4301 {
4302 if (abiversion (ibfd) == 0)
4303 set_abiversion (ibfd, 2);
4304 else if (abiversion (ibfd) == 1)
4305 {
4306 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4307 " for ABI version 1"), *name);
4308 bfd_set_error (bfd_error_bad_value);
4309 return false;
4310 }
4311 }
4312
4313 return true;
4314 }
4315
4316 /* Merge non-visibility st_other attributes: local entry point. */
4317
4318 static void
4319 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4320 unsigned int st_other,
4321 bool definition,
4322 bool dynamic)
4323 {
4324 if (definition && (!dynamic || !h->def_regular))
4325 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4326 | ELF_ST_VISIBILITY (h->other));
4327 }
4328
4329 /* Hook called on merging a symbol. We use this to clear "fake" since
4330 we now have a real symbol. */
4331
4332 static bool
4333 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4334 const Elf_Internal_Sym *isym,
4335 asection **psec ATTRIBUTE_UNUSED,
4336 bool newdef ATTRIBUTE_UNUSED,
4337 bool olddef ATTRIBUTE_UNUSED,
4338 bfd *oldbfd ATTRIBUTE_UNUSED,
4339 const asection *oldsec ATTRIBUTE_UNUSED)
4340 {
4341 ppc_elf_hash_entry (h)->fake = 0;
4342 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4343 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4344 return true;
4345 }
4346
4347 /* This function makes an old ABI object reference to ".bar" cause the
4348 inclusion of a new ABI object archive that defines "bar".
4349 NAME is a symbol defined in an archive. Return a symbol in the hash
4350 table that might be satisfied by the archive symbols. */
4351
4352 static struct bfd_link_hash_entry *
4353 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4354 struct bfd_link_info *info,
4355 const char *name)
4356 {
4357 struct bfd_link_hash_entry *h;
4358 char *dot_name;
4359 size_t len;
4360
4361 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4362 if (h != NULL
4363 && ppc_hash_table (info) != NULL
4364 /* Don't return this sym if it is a fake function descriptor
4365 created by add_symbol_adjust. */
4366 && !((struct ppc_link_hash_entry *) h)->fake)
4367 return h;
4368
4369 if (name[0] == '.')
4370 return h;
4371
4372 len = strlen (name);
4373 dot_name = bfd_alloc (abfd, len + 2);
4374 if (dot_name == NULL)
4375 return (struct bfd_link_hash_entry *) -1;
4376 dot_name[0] = '.';
4377 memcpy (dot_name + 1, name, len + 1);
4378 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4379 bfd_release (abfd, dot_name);
4380 if (h != NULL)
4381 return h;
4382
4383 if (strcmp (name, "__tls_get_addr_opt") == 0)
4384 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4385 return h;
4386 }
4387
4388 /* This function satisfies all old ABI object references to ".bar" if a
4389 new ABI object defines "bar". Well, at least, undefined dot symbols
4390 are made weak. This stops later archive searches from including an
4391 object if we already have a function descriptor definition. It also
4392 prevents the linker complaining about undefined symbols.
4393 We also check and correct mismatched symbol visibility here. The
4394 most restrictive visibility of the function descriptor and the
4395 function entry symbol is used. */
4396
4397 static bool
4398 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4399 {
4400 struct ppc_link_hash_table *htab;
4401 struct ppc_link_hash_entry *fdh;
4402
4403 if (eh->elf.root.type == bfd_link_hash_warning)
4404 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4405
4406 if (eh->elf.root.type == bfd_link_hash_indirect)
4407 return true;
4408
4409 if (eh->elf.root.root.string[0] != '.')
4410 abort ();
4411
4412 htab = ppc_hash_table (info);
4413 if (htab == NULL)
4414 return false;
4415
4416 fdh = lookup_fdh (eh, htab);
4417 if (fdh == NULL
4418 && !bfd_link_relocatable (info)
4419 && (eh->elf.root.type == bfd_link_hash_undefined
4420 || eh->elf.root.type == bfd_link_hash_undefweak)
4421 && eh->elf.ref_regular)
4422 {
4423 /* Make an undefined function descriptor sym, in order to
4424 pull in an --as-needed shared lib. Archives are handled
4425 elsewhere. */
4426 fdh = make_fdh (info, eh);
4427 if (fdh == NULL)
4428 return false;
4429 }
4430
4431 if (fdh != NULL)
4432 {
4433 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4434 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4435
4436 /* Make both descriptor and entry symbol have the most
4437 constraining visibility of either symbol. */
4438 if (entry_vis < descr_vis)
4439 fdh->elf.other += entry_vis - descr_vis;
4440 else if (entry_vis > descr_vis)
4441 eh->elf.other += descr_vis - entry_vis;
4442
4443 /* Propagate reference flags from entry symbol to function
4444 descriptor symbol. */
4445 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4446 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4447 fdh->elf.ref_regular |= eh->elf.ref_regular;
4448 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4449
4450 if (!fdh->elf.forced_local
4451 && fdh->elf.dynindx == -1
4452 && fdh->elf.versioned != versioned_hidden
4453 && (bfd_link_dll (info)
4454 || fdh->elf.def_dynamic
4455 || fdh->elf.ref_dynamic)
4456 && (eh->elf.ref_regular
4457 || eh->elf.def_regular))
4458 {
4459 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4460 return false;
4461 }
4462 }
4463
4464 return true;
4465 }
4466
4467 /* Set up opd section info and abiversion for IBFD, and process list
4468 of dot-symbols we made in link_hash_newfunc. */
4469
4470 static bool
4471 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4472 {
4473 struct ppc_link_hash_table *htab;
4474 struct ppc_link_hash_entry **p, *eh;
4475 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4476
4477 if (opd != NULL && opd->size != 0)
4478 {
4479 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4480 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4481
4482 if (abiversion (ibfd) == 0)
4483 set_abiversion (ibfd, 1);
4484 else if (abiversion (ibfd) >= 2)
4485 {
4486 /* xgettext:c-format */
4487 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4488 ibfd, abiversion (ibfd));
4489 bfd_set_error (bfd_error_bad_value);
4490 return false;
4491 }
4492 }
4493
4494 if (is_ppc64_elf (info->output_bfd))
4495 {
4496 /* For input files without an explicit abiversion in e_flags
4497 we should have flagged any with symbol st_other bits set
4498 as ELFv1 and above flagged those with .opd as ELFv2.
4499 Set the output abiversion if not yet set, and for any input
4500 still ambiguous, take its abiversion from the output.
4501 Differences in ABI are reported later. */
4502 if (abiversion (info->output_bfd) == 0)
4503 set_abiversion (info->output_bfd, abiversion (ibfd));
4504 else if (abiversion (ibfd) == 0)
4505 set_abiversion (ibfd, abiversion (info->output_bfd));
4506 }
4507
4508 htab = ppc_hash_table (info);
4509 if (htab == NULL)
4510 return true;
4511
4512 if (opd != NULL && opd->size != 0
4513 && (ibfd->flags & DYNAMIC) == 0
4514 && (opd->flags & SEC_RELOC) != 0
4515 && opd->reloc_count != 0
4516 && !bfd_is_abs_section (opd->output_section)
4517 && info->gc_sections)
4518 {
4519 /* Garbage collection needs some extra help with .opd sections.
4520 We don't want to necessarily keep everything referenced by
4521 relocs in .opd, as that would keep all functions. Instead,
4522 if we reference an .opd symbol (a function descriptor), we
4523 want to keep the function code symbol's section. This is
4524 easy for global symbols, but for local syms we need to keep
4525 information about the associated function section. */
4526 bfd_size_type amt;
4527 asection **opd_sym_map;
4528 Elf_Internal_Shdr *symtab_hdr;
4529 Elf_Internal_Rela *relocs, *rel_end, *rel;
4530
4531 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4532 opd_sym_map = bfd_zalloc (ibfd, amt);
4533 if (opd_sym_map == NULL)
4534 return false;
4535 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4536 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4537 info->keep_memory);
4538 if (relocs == NULL)
4539 return false;
4540 symtab_hdr = &elf_symtab_hdr (ibfd);
4541 rel_end = relocs + opd->reloc_count - 1;
4542 for (rel = relocs; rel < rel_end; rel++)
4543 {
4544 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4545 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4546
4547 if (r_type == R_PPC64_ADDR64
4548 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4549 && r_symndx < symtab_hdr->sh_info)
4550 {
4551 Elf_Internal_Sym *isym;
4552 asection *s;
4553
4554 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4555 r_symndx);
4556 if (isym == NULL)
4557 {
4558 if (elf_section_data (opd)->relocs != relocs)
4559 free (relocs);
4560 return false;
4561 }
4562
4563 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4564 if (s != NULL && s != opd)
4565 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4566 }
4567 }
4568 if (elf_section_data (opd)->relocs != relocs)
4569 free (relocs);
4570 }
4571
4572 p = &htab->dot_syms;
4573 while ((eh = *p) != NULL)
4574 {
4575 *p = NULL;
4576 if (&eh->elf == htab->elf.hgot)
4577 ;
4578 else if (htab->elf.hgot == NULL
4579 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4580 htab->elf.hgot = &eh->elf;
4581 else if (abiversion (ibfd) <= 1)
4582 {
4583 htab->need_func_desc_adj = 1;
4584 if (!add_symbol_adjust (eh, info))
4585 return false;
4586 }
4587 p = &eh->u.next_dot_sym;
4588 }
4589 return true;
4590 }
4591
4592 /* Undo hash table changes when an --as-needed input file is determined
4593 not to be needed. */
4594
4595 static bool
4596 ppc64_elf_notice_as_needed (bfd *ibfd,
4597 struct bfd_link_info *info,
4598 enum notice_asneeded_action act)
4599 {
4600 if (act == notice_not_needed)
4601 {
4602 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4603
4604 if (htab == NULL)
4605 return false;
4606
4607 htab->dot_syms = NULL;
4608 }
4609 return _bfd_elf_notice_as_needed (ibfd, info, act);
4610 }
4611
4612 /* If --just-symbols against a final linked binary, then assume we need
4613 toc adjusting stubs when calling functions defined there. */
4614
4615 static void
4616 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4617 {
4618 if ((sec->flags & SEC_CODE) != 0
4619 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4620 && is_ppc64_elf (sec->owner))
4621 {
4622 if (abiversion (sec->owner) >= 2
4623 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4624 sec->has_toc_reloc = 1;
4625 }
4626 _bfd_elf_link_just_syms (sec, info);
4627 }
4628
4629 static struct plt_entry **
4630 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4631 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4632 {
4633 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4634 struct plt_entry **local_plt;
4635 unsigned char *local_got_tls_masks;
4636
4637 if (local_got_ents == NULL)
4638 {
4639 bfd_size_type size = symtab_hdr->sh_info;
4640
4641 size *= (sizeof (*local_got_ents)
4642 + sizeof (*local_plt)
4643 + sizeof (*local_got_tls_masks));
4644 local_got_ents = bfd_zalloc (abfd, size);
4645 if (local_got_ents == NULL)
4646 return NULL;
4647 elf_local_got_ents (abfd) = local_got_ents;
4648 }
4649
4650 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4651 {
4652 struct got_entry *ent;
4653
4654 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4655 if (ent->addend == r_addend
4656 && ent->owner == abfd
4657 && ent->tls_type == tls_type)
4658 break;
4659 if (ent == NULL)
4660 {
4661 size_t amt = sizeof (*ent);
4662 ent = bfd_alloc (abfd, amt);
4663 if (ent == NULL)
4664 return false;
4665 ent->next = local_got_ents[r_symndx];
4666 ent->addend = r_addend;
4667 ent->owner = abfd;
4668 ent->tls_type = tls_type;
4669 ent->is_indirect = false;
4670 ent->got.refcount = 0;
4671 local_got_ents[r_symndx] = ent;
4672 }
4673 ent->got.refcount += 1;
4674 }
4675
4676 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4677 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4678 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4679
4680 return local_plt + r_symndx;
4681 }
4682
4683 static bool
4684 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4685 {
4686 struct plt_entry *ent;
4687
4688 for (ent = *plist; ent != NULL; ent = ent->next)
4689 if (ent->addend == addend)
4690 break;
4691 if (ent == NULL)
4692 {
4693 size_t amt = sizeof (*ent);
4694 ent = bfd_alloc (abfd, amt);
4695 if (ent == NULL)
4696 return false;
4697 ent->next = *plist;
4698 ent->addend = addend;
4699 ent->plt.refcount = 0;
4700 *plist = ent;
4701 }
4702 ent->plt.refcount += 1;
4703 return true;
4704 }
4705
4706 static bool
4707 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4708 {
4709 return (r_type == R_PPC64_REL24
4710 || r_type == R_PPC64_REL24_NOTOC
4711 || r_type == R_PPC64_REL24_P9NOTOC
4712 || r_type == R_PPC64_REL14
4713 || r_type == R_PPC64_REL14_BRTAKEN
4714 || r_type == R_PPC64_REL14_BRNTAKEN
4715 || r_type == R_PPC64_ADDR24
4716 || r_type == R_PPC64_ADDR14
4717 || r_type == R_PPC64_ADDR14_BRTAKEN
4718 || r_type == R_PPC64_ADDR14_BRNTAKEN
4719 || r_type == R_PPC64_PLTCALL
4720 || r_type == R_PPC64_PLTCALL_NOTOC);
4721 }
4722
4723 /* Relocs on inline plt call sequence insns prior to the call. */
4724
4725 static bool
4726 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4727 {
4728 return (r_type == R_PPC64_PLT16_HA
4729 || r_type == R_PPC64_PLT16_HI
4730 || r_type == R_PPC64_PLT16_LO
4731 || r_type == R_PPC64_PLT16_LO_DS
4732 || r_type == R_PPC64_PLT_PCREL34
4733 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4734 || r_type == R_PPC64_PLTSEQ
4735 || r_type == R_PPC64_PLTSEQ_NOTOC);
4736 }
4737
4738 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4739 relocs, return true for those that operate on a dword. */
4740
4741 static bool
4742 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4743 {
4744 return (r_type == R_PPC64_PLT_PCREL34
4745 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4746 || r_type == R_PPC64_PLTCALL);
4747 }
4748
4749 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4750 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4751
4752 static bool
4753 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4754 {
4755 return offset <= sec->size && size <= sec->size - offset;
4756 }
4757
4758 /* Look through the relocs for a section during the first phase, and
4759 calculate needed space in the global offset table, procedure
4760 linkage table, and dynamic reloc sections. */
4761
4762 static bool
4763 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4764 asection *sec, const Elf_Internal_Rela *relocs)
4765 {
4766 struct ppc_link_hash_table *htab;
4767 Elf_Internal_Shdr *symtab_hdr;
4768 struct elf_link_hash_entry **sym_hashes;
4769 const Elf_Internal_Rela *rel;
4770 const Elf_Internal_Rela *rel_end;
4771 asection *sreloc;
4772 struct elf_link_hash_entry *tga, *dottga;
4773 bool is_opd;
4774
4775 if (bfd_link_relocatable (info))
4776 return true;
4777
4778 BFD_ASSERT (is_ppc64_elf (abfd));
4779
4780 htab = ppc_hash_table (info);
4781 if (htab == NULL)
4782 return false;
4783
4784 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4785 false, false, true);
4786 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4787 false, false, true);
4788 symtab_hdr = &elf_symtab_hdr (abfd);
4789 sym_hashes = elf_sym_hashes (abfd);
4790 sreloc = NULL;
4791 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4792 rel_end = relocs + sec->reloc_count;
4793 for (rel = relocs; rel < rel_end; rel++)
4794 {
4795 unsigned long r_symndx;
4796 struct elf_link_hash_entry *h;
4797 Elf_Internal_Sym *isym;
4798 enum elf_ppc64_reloc_type r_type;
4799 int tls_type;
4800 struct _ppc64_elf_section_data *ppc64_sec;
4801 struct plt_entry **ifunc, **plt_list;
4802
4803 r_symndx = ELF64_R_SYM (rel->r_info);
4804 if (r_symndx < symtab_hdr->sh_info)
4805 {
4806 h = NULL;
4807 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4808 if (isym == NULL)
4809 return false;
4810 }
4811 else
4812 {
4813 isym = NULL;
4814 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4815 h = elf_follow_link (h);
4816
4817 if (h == htab->elf.hgot)
4818 sec->has_toc_reloc = 1;
4819 }
4820
4821 r_type = ELF64_R_TYPE (rel->r_info);
4822 switch (r_type)
4823 {
4824 case R_PPC64_D34:
4825 case R_PPC64_D34_LO:
4826 case R_PPC64_D34_HI30:
4827 case R_PPC64_D34_HA30:
4828 case R_PPC64_D28:
4829 case R_PPC64_TPREL34:
4830 case R_PPC64_DTPREL34:
4831 case R_PPC64_PCREL34:
4832 case R_PPC64_GOT_PCREL34:
4833 case R_PPC64_GOT_TLSGD_PCREL34:
4834 case R_PPC64_GOT_TLSLD_PCREL34:
4835 case R_PPC64_GOT_TPREL_PCREL34:
4836 case R_PPC64_GOT_DTPREL_PCREL34:
4837 case R_PPC64_PLT_PCREL34:
4838 case R_PPC64_PLT_PCREL34_NOTOC:
4839 case R_PPC64_PCREL28:
4840 htab->has_power10_relocs = 1;
4841 break;
4842 default:
4843 break;
4844 }
4845
4846 switch (r_type)
4847 {
4848 case R_PPC64_PLT16_HA:
4849 case R_PPC64_GOT_TLSLD16_HA:
4850 case R_PPC64_GOT_TLSGD16_HA:
4851 case R_PPC64_GOT_TPREL16_HA:
4852 case R_PPC64_GOT_DTPREL16_HA:
4853 case R_PPC64_GOT16_HA:
4854 case R_PPC64_TOC16_HA:
4855 case R_PPC64_PLT16_LO:
4856 case R_PPC64_PLT16_LO_DS:
4857 case R_PPC64_GOT_TLSLD16_LO:
4858 case R_PPC64_GOT_TLSGD16_LO:
4859 case R_PPC64_GOT_TPREL16_LO_DS:
4860 case R_PPC64_GOT_DTPREL16_LO_DS:
4861 case R_PPC64_GOT16_LO:
4862 case R_PPC64_GOT16_LO_DS:
4863 case R_PPC64_TOC16_LO:
4864 case R_PPC64_TOC16_LO_DS:
4865 case R_PPC64_GOT_PCREL34:
4866 ppc64_elf_tdata (abfd)->has_optrel = 1;
4867 ppc64_elf_section_data (sec)->has_optrel = 1;
4868 break;
4869 default:
4870 break;
4871 }
4872
4873 ifunc = NULL;
4874 if (h != NULL)
4875 {
4876 if (h->type == STT_GNU_IFUNC)
4877 {
4878 h->needs_plt = 1;
4879 ifunc = &h->plt.plist;
4880 }
4881 }
4882 else
4883 {
4884 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4885 {
4886 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4887 rel->r_addend,
4888 NON_GOT | PLT_IFUNC);
4889 if (ifunc == NULL)
4890 return false;
4891 }
4892 }
4893
4894 tls_type = 0;
4895 switch (r_type)
4896 {
4897 case R_PPC64_TLSGD:
4898 case R_PPC64_TLSLD:
4899 /* These special tls relocs tie a call to __tls_get_addr with
4900 its parameter symbol. */
4901 if (h != NULL)
4902 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4903 else
4904 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4905 rel->r_addend,
4906 NON_GOT | TLS_TLS | TLS_MARK))
4907 return false;
4908 sec->has_tls_reloc = 1;
4909 break;
4910
4911 case R_PPC64_GOT_TLSLD16:
4912 case R_PPC64_GOT_TLSLD16_LO:
4913 case R_PPC64_GOT_TLSLD16_HI:
4914 case R_PPC64_GOT_TLSLD16_HA:
4915 case R_PPC64_GOT_TLSLD_PCREL34:
4916 tls_type = TLS_TLS | TLS_LD;
4917 goto dogottls;
4918
4919 case R_PPC64_GOT_TLSGD16:
4920 case R_PPC64_GOT_TLSGD16_LO:
4921 case R_PPC64_GOT_TLSGD16_HI:
4922 case R_PPC64_GOT_TLSGD16_HA:
4923 case R_PPC64_GOT_TLSGD_PCREL34:
4924 tls_type = TLS_TLS | TLS_GD;
4925 goto dogottls;
4926
4927 case R_PPC64_GOT_TPREL16_DS:
4928 case R_PPC64_GOT_TPREL16_LO_DS:
4929 case R_PPC64_GOT_TPREL16_HI:
4930 case R_PPC64_GOT_TPREL16_HA:
4931 case R_PPC64_GOT_TPREL_PCREL34:
4932 if (bfd_link_dll (info))
4933 info->flags |= DF_STATIC_TLS;
4934 tls_type = TLS_TLS | TLS_TPREL;
4935 goto dogottls;
4936
4937 case R_PPC64_GOT_DTPREL16_DS:
4938 case R_PPC64_GOT_DTPREL16_LO_DS:
4939 case R_PPC64_GOT_DTPREL16_HI:
4940 case R_PPC64_GOT_DTPREL16_HA:
4941 case R_PPC64_GOT_DTPREL_PCREL34:
4942 tls_type = TLS_TLS | TLS_DTPREL;
4943 dogottls:
4944 sec->has_tls_reloc = 1;
4945 goto dogot;
4946
4947 case R_PPC64_GOT16:
4948 case R_PPC64_GOT16_LO:
4949 case R_PPC64_GOT16_HI:
4950 case R_PPC64_GOT16_HA:
4951 case R_PPC64_GOT16_DS:
4952 case R_PPC64_GOT16_LO_DS:
4953 case R_PPC64_GOT_PCREL34:
4954 dogot:
4955 /* This symbol requires a global offset table entry. */
4956 sec->has_toc_reloc = 1;
4957 if (r_type == R_PPC64_GOT_TLSLD16
4958 || r_type == R_PPC64_GOT_TLSGD16
4959 || r_type == R_PPC64_GOT_TPREL16_DS
4960 || r_type == R_PPC64_GOT_DTPREL16_DS
4961 || r_type == R_PPC64_GOT16
4962 || r_type == R_PPC64_GOT16_DS)
4963 {
4964 htab->do_multi_toc = 1;
4965 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4966 }
4967
4968 if (ppc64_elf_tdata (abfd)->got == NULL
4969 && !create_got_section (abfd, info))
4970 return false;
4971
4972 if (h != NULL)
4973 {
4974 struct ppc_link_hash_entry *eh;
4975 struct got_entry *ent;
4976
4977 eh = ppc_elf_hash_entry (h);
4978 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4979 if (ent->addend == rel->r_addend
4980 && ent->owner == abfd
4981 && ent->tls_type == tls_type)
4982 break;
4983 if (ent == NULL)
4984 {
4985 size_t amt = sizeof (*ent);
4986 ent = bfd_alloc (abfd, amt);
4987 if (ent == NULL)
4988 return false;
4989 ent->next = eh->elf.got.glist;
4990 ent->addend = rel->r_addend;
4991 ent->owner = abfd;
4992 ent->tls_type = tls_type;
4993 ent->is_indirect = false;
4994 ent->got.refcount = 0;
4995 eh->elf.got.glist = ent;
4996 }
4997 ent->got.refcount += 1;
4998 eh->tls_mask |= tls_type;
4999 }
5000 else
5001 /* This is a global offset table entry for a local symbol. */
5002 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5003 rel->r_addend, tls_type))
5004 return false;
5005 break;
5006
5007 case R_PPC64_PLT16_HA:
5008 case R_PPC64_PLT16_HI:
5009 case R_PPC64_PLT16_LO:
5010 case R_PPC64_PLT16_LO_DS:
5011 case R_PPC64_PLT_PCREL34:
5012 case R_PPC64_PLT_PCREL34_NOTOC:
5013 case R_PPC64_PLT32:
5014 case R_PPC64_PLT64:
5015 /* This symbol requires a procedure linkage table entry. */
5016 plt_list = ifunc;
5017 if (h != NULL)
5018 {
5019 h->needs_plt = 1;
5020 if (h->root.root.string[0] == '.'
5021 && h->root.root.string[1] != '\0')
5022 ppc_elf_hash_entry (h)->is_func = 1;
5023 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5024 plt_list = &h->plt.plist;
5025 }
5026 if (plt_list == NULL)
5027 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5028 rel->r_addend,
5029 NON_GOT | PLT_KEEP);
5030 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5031 return false;
5032 break;
5033
5034 /* The following relocations don't need to propagate the
5035 relocation if linking a shared object since they are
5036 section relative. */
5037 case R_PPC64_SECTOFF:
5038 case R_PPC64_SECTOFF_LO:
5039 case R_PPC64_SECTOFF_HI:
5040 case R_PPC64_SECTOFF_HA:
5041 case R_PPC64_SECTOFF_DS:
5042 case R_PPC64_SECTOFF_LO_DS:
5043 case R_PPC64_DTPREL16:
5044 case R_PPC64_DTPREL16_LO:
5045 case R_PPC64_DTPREL16_HI:
5046 case R_PPC64_DTPREL16_HA:
5047 case R_PPC64_DTPREL16_DS:
5048 case R_PPC64_DTPREL16_LO_DS:
5049 case R_PPC64_DTPREL16_HIGH:
5050 case R_PPC64_DTPREL16_HIGHA:
5051 case R_PPC64_DTPREL16_HIGHER:
5052 case R_PPC64_DTPREL16_HIGHERA:
5053 case R_PPC64_DTPREL16_HIGHEST:
5054 case R_PPC64_DTPREL16_HIGHESTA:
5055 break;
5056
5057 /* Nor do these. */
5058 case R_PPC64_REL16:
5059 case R_PPC64_REL16_LO:
5060 case R_PPC64_REL16_HI:
5061 case R_PPC64_REL16_HA:
5062 case R_PPC64_REL16_HIGH:
5063 case R_PPC64_REL16_HIGHA:
5064 case R_PPC64_REL16_HIGHER:
5065 case R_PPC64_REL16_HIGHERA:
5066 case R_PPC64_REL16_HIGHEST:
5067 case R_PPC64_REL16_HIGHESTA:
5068 case R_PPC64_REL16_HIGHER34:
5069 case R_PPC64_REL16_HIGHERA34:
5070 case R_PPC64_REL16_HIGHEST34:
5071 case R_PPC64_REL16_HIGHESTA34:
5072 case R_PPC64_REL16DX_HA:
5073 break;
5074
5075 /* Not supported as a dynamic relocation. */
5076 case R_PPC64_ADDR64_LOCAL:
5077 if (bfd_link_pic (info))
5078 {
5079 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5080 ppc_howto_init ();
5081 /* xgettext:c-format */
5082 info->callbacks->einfo (_("%H: %s reloc unsupported "
5083 "in shared libraries and PIEs\n"),
5084 abfd, sec, rel->r_offset,
5085 ppc64_elf_howto_table[r_type]->name);
5086 bfd_set_error (bfd_error_bad_value);
5087 return false;
5088 }
5089 break;
5090
5091 case R_PPC64_TOC16:
5092 case R_PPC64_TOC16_DS:
5093 htab->do_multi_toc = 1;
5094 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5095 /* Fall through. */
5096 case R_PPC64_TOC16_LO:
5097 case R_PPC64_TOC16_HI:
5098 case R_PPC64_TOC16_HA:
5099 case R_PPC64_TOC16_LO_DS:
5100 sec->has_toc_reloc = 1;
5101 if (h != NULL && bfd_link_executable (info))
5102 {
5103 /* We may need a copy reloc. */
5104 h->non_got_ref = 1;
5105 /* Strongly prefer a copy reloc over a dynamic reloc.
5106 glibc ld.so as of 2019-08 will error out if one of
5107 these relocations is emitted. */
5108 h->needs_copy = 1;
5109 goto dodyn;
5110 }
5111 break;
5112
5113 /* Marker reloc. */
5114 case R_PPC64_ENTRY:
5115 break;
5116
5117 /* This relocation describes the C++ object vtable hierarchy.
5118 Reconstruct it for later use during GC. */
5119 case R_PPC64_GNU_VTINHERIT:
5120 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5121 return false;
5122 break;
5123
5124 /* This relocation describes which C++ vtable entries are actually
5125 used. Record for later use during GC. */
5126 case R_PPC64_GNU_VTENTRY:
5127 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5128 return false;
5129 break;
5130
5131 case R_PPC64_REL14:
5132 case R_PPC64_REL14_BRTAKEN:
5133 case R_PPC64_REL14_BRNTAKEN:
5134 {
5135 asection *dest = NULL;
5136
5137 /* Heuristic: If jumping outside our section, chances are
5138 we are going to need a stub. */
5139 if (h != NULL)
5140 {
5141 /* If the sym is weak it may be overridden later, so
5142 don't assume we know where a weak sym lives. */
5143 if (h->root.type == bfd_link_hash_defined)
5144 dest = h->root.u.def.section;
5145 }
5146 else
5147 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5148
5149 if (dest != sec)
5150 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5151 }
5152 goto rel24;
5153
5154 case R_PPC64_PLTCALL:
5155 case R_PPC64_PLTCALL_NOTOC:
5156 ppc64_elf_section_data (sec)->has_pltcall = 1;
5157 /* Fall through. */
5158
5159 case R_PPC64_REL24:
5160 case R_PPC64_REL24_NOTOC:
5161 case R_PPC64_REL24_P9NOTOC:
5162 rel24:
5163 plt_list = ifunc;
5164 if (h != NULL)
5165 {
5166 h->needs_plt = 1;
5167 if (h->root.root.string[0] == '.'
5168 && h->root.root.string[1] != '\0')
5169 ppc_elf_hash_entry (h)->is_func = 1;
5170
5171 if (h == tga || h == dottga)
5172 {
5173 sec->has_tls_reloc = 1;
5174 if (rel != relocs
5175 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5176 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5177 /* We have a new-style __tls_get_addr call with
5178 a marker reloc. */
5179 ;
5180 else
5181 /* Mark this section as having an old-style call. */
5182 sec->nomark_tls_get_addr = 1;
5183 }
5184 plt_list = &h->plt.plist;
5185 }
5186
5187 /* We may need a .plt entry if the function this reloc
5188 refers to is in a shared lib. */
5189 if (plt_list
5190 && !update_plt_info (abfd, plt_list, rel->r_addend))
5191 return false;
5192 break;
5193
5194 case R_PPC64_ADDR14:
5195 case R_PPC64_ADDR14_BRNTAKEN:
5196 case R_PPC64_ADDR14_BRTAKEN:
5197 case R_PPC64_ADDR24:
5198 goto dodyn;
5199
5200 case R_PPC64_TPREL64:
5201 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5202 if (bfd_link_dll (info))
5203 info->flags |= DF_STATIC_TLS;
5204 goto dotlstoc;
5205
5206 case R_PPC64_DTPMOD64:
5207 if (rel + 1 < rel_end
5208 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5209 && rel[1].r_offset == rel->r_offset + 8)
5210 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5211 else
5212 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5213 goto dotlstoc;
5214
5215 case R_PPC64_DTPREL64:
5216 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5217 if (rel != relocs
5218 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5219 && rel[-1].r_offset == rel->r_offset - 8)
5220 /* This is the second reloc of a dtpmod, dtprel pair.
5221 Don't mark with TLS_DTPREL. */
5222 goto dodyn;
5223
5224 dotlstoc:
5225 sec->has_tls_reloc = 1;
5226 if (h != NULL)
5227 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5228 else
5229 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5230 rel->r_addend, tls_type))
5231 return false;
5232
5233 ppc64_sec = ppc64_elf_section_data (sec);
5234 if (ppc64_sec->sec_type != sec_toc)
5235 {
5236 bfd_size_type amt;
5237
5238 /* One extra to simplify get_tls_mask. */
5239 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5240 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5241 if (ppc64_sec->u.toc.symndx == NULL)
5242 return false;
5243 amt = sec->size * sizeof (bfd_vma) / 8;
5244 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5245 if (ppc64_sec->u.toc.add == NULL)
5246 return false;
5247 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5248 ppc64_sec->sec_type = sec_toc;
5249 }
5250 BFD_ASSERT (rel->r_offset % 8 == 0);
5251 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5252 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5253
5254 /* Mark the second slot of a GD or LD entry.
5255 -1 to indicate GD and -2 to indicate LD. */
5256 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5257 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5258 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5259 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5260 goto dodyn;
5261
5262 case R_PPC64_TPREL16_HI:
5263 case R_PPC64_TPREL16_HA:
5264 case R_PPC64_TPREL16_HIGH:
5265 case R_PPC64_TPREL16_HIGHA:
5266 case R_PPC64_TPREL16_HIGHER:
5267 case R_PPC64_TPREL16_HIGHERA:
5268 case R_PPC64_TPREL16_HIGHEST:
5269 case R_PPC64_TPREL16_HIGHESTA:
5270 sec->has_tls_reloc = 1;
5271 /* Fall through. */
5272 case R_PPC64_TPREL34:
5273 case R_PPC64_TPREL16:
5274 case R_PPC64_TPREL16_DS:
5275 case R_PPC64_TPREL16_LO:
5276 case R_PPC64_TPREL16_LO_DS:
5277 if (bfd_link_dll (info))
5278 info->flags |= DF_STATIC_TLS;
5279 goto dodyn;
5280
5281 case R_PPC64_ADDR64:
5282 if (is_opd
5283 && rel + 1 < rel_end
5284 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5285 {
5286 if (h != NULL)
5287 ppc_elf_hash_entry (h)->is_func = 1;
5288 }
5289 /* Fall through. */
5290
5291 case R_PPC64_ADDR16:
5292 case R_PPC64_ADDR16_DS:
5293 case R_PPC64_ADDR16_HA:
5294 case R_PPC64_ADDR16_HI:
5295 case R_PPC64_ADDR16_HIGH:
5296 case R_PPC64_ADDR16_HIGHA:
5297 case R_PPC64_ADDR16_HIGHER:
5298 case R_PPC64_ADDR16_HIGHERA:
5299 case R_PPC64_ADDR16_HIGHEST:
5300 case R_PPC64_ADDR16_HIGHESTA:
5301 case R_PPC64_ADDR16_LO:
5302 case R_PPC64_ADDR16_LO_DS:
5303 case R_PPC64_D34:
5304 case R_PPC64_D34_LO:
5305 case R_PPC64_D34_HI30:
5306 case R_PPC64_D34_HA30:
5307 case R_PPC64_ADDR16_HIGHER34:
5308 case R_PPC64_ADDR16_HIGHERA34:
5309 case R_PPC64_ADDR16_HIGHEST34:
5310 case R_PPC64_ADDR16_HIGHESTA34:
5311 case R_PPC64_D28:
5312 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5313 && rel->r_addend == 0)
5314 {
5315 /* We may need a .plt entry if this reloc refers to a
5316 function in a shared lib. */
5317 if (!update_plt_info (abfd, &h->plt.plist, 0))
5318 return false;
5319 h->pointer_equality_needed = 1;
5320 }
5321 /* Fall through. */
5322
5323 case R_PPC64_REL30:
5324 case R_PPC64_REL32:
5325 case R_PPC64_REL64:
5326 case R_PPC64_ADDR32:
5327 case R_PPC64_UADDR16:
5328 case R_PPC64_UADDR32:
5329 case R_PPC64_UADDR64:
5330 case R_PPC64_TOC:
5331 if (h != NULL && bfd_link_executable (info))
5332 /* We may need a copy reloc. */
5333 h->non_got_ref = 1;
5334
5335 /* Don't propagate .opd relocs. */
5336 if (NO_OPD_RELOCS && is_opd)
5337 break;
5338
5339 /* Set up information for symbols that might need dynamic
5340 relocations. At this point in linking we have read all
5341 the input files and resolved most symbols, but have not
5342 yet decided whether symbols are dynamic or finalized
5343 symbol flags. In some cases we might be setting dynamic
5344 reloc info for symbols that do not end up needing such.
5345 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5346 work together with this code. */
5347 dodyn:
5348 if ((h != NULL
5349 && !SYMBOL_REFERENCES_LOCAL (info, h))
5350 || (bfd_link_pic (info)
5351 && (h != NULL
5352 ? !bfd_is_abs_symbol (&h->root)
5353 : isym->st_shndx != SHN_ABS)
5354 && must_be_dyn_reloc (info, r_type))
5355 || (!bfd_link_pic (info)
5356 && ifunc != NULL))
5357 {
5358 /* We must copy these reloc types into the output file.
5359 Create a reloc section in dynobj and make room for
5360 this reloc. */
5361 if (sreloc == NULL)
5362 {
5363 sreloc = _bfd_elf_make_dynamic_reloc_section
5364 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5365
5366 if (sreloc == NULL)
5367 return false;
5368 }
5369
5370 /* If this is a global symbol, we count the number of
5371 relocations we need for this symbol. */
5372 if (h != NULL)
5373 {
5374 struct ppc_dyn_relocs *p;
5375 struct ppc_dyn_relocs **head;
5376
5377 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5378 p = *head;
5379 if (p == NULL || p->sec != sec)
5380 {
5381 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5382 if (p == NULL)
5383 return false;
5384 p->next = *head;
5385 *head = p;
5386 p->sec = sec;
5387 p->count = 0;
5388 p->pc_count = 0;
5389 p->rel_count = 0;
5390 }
5391 p->count += 1;
5392 if (!must_be_dyn_reloc (info, r_type))
5393 p->pc_count += 1;
5394 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5395 && rel->r_offset % 2 == 0
5396 && sec->alignment_power != 0)
5397 p->rel_count += 1;
5398 }
5399 else
5400 {
5401 /* Track dynamic relocs needed for local syms too. */
5402 struct ppc_local_dyn_relocs *p;
5403 struct ppc_local_dyn_relocs **head;
5404 bool is_ifunc;
5405 asection *s;
5406 void *vpp;
5407
5408 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5409 if (s == NULL)
5410 s = sec;
5411
5412 vpp = &elf_section_data (s)->local_dynrel;
5413 head = (struct ppc_local_dyn_relocs **) vpp;
5414 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5415 p = *head;
5416 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5417 p = p->next;
5418 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5419 {
5420 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5421 if (p == NULL)
5422 return false;
5423 p->next = *head;
5424 *head = p;
5425 p->sec = sec;
5426 p->count = 0;
5427 p->rel_count = 0;
5428 p->ifunc = is_ifunc;
5429 }
5430 p->count += 1;
5431 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5432 && rel->r_offset % 2 == 0
5433 && sec->alignment_power != 0)
5434 p->rel_count += 1;
5435 }
5436 }
5437 break;
5438
5439 default:
5440 break;
5441 }
5442 }
5443
5444 return true;
5445 }
5446
5447 /* Merge backend specific data from an object file to the output
5448 object file when linking. */
5449
5450 static bool
5451 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5452 {
5453 bfd *obfd = info->output_bfd;
5454 unsigned long iflags, oflags;
5455
5456 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5457 return true;
5458
5459 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5460 return true;
5461
5462 if (!_bfd_generic_verify_endian_match (ibfd, info))
5463 return false;
5464
5465 iflags = elf_elfheader (ibfd)->e_flags;
5466 oflags = elf_elfheader (obfd)->e_flags;
5467
5468 if (iflags & ~EF_PPC64_ABI)
5469 {
5470 _bfd_error_handler
5471 /* xgettext:c-format */
5472 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5473 bfd_set_error (bfd_error_bad_value);
5474 return false;
5475 }
5476 else if (iflags != oflags && iflags != 0)
5477 {
5478 _bfd_error_handler
5479 /* xgettext:c-format */
5480 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5481 ibfd, iflags, oflags);
5482 bfd_set_error (bfd_error_bad_value);
5483 return false;
5484 }
5485
5486 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5487 return false;
5488
5489 /* Merge Tag_compatibility attributes and any common GNU ones. */
5490 return _bfd_elf_merge_object_attributes (ibfd, info);
5491 }
5492
5493 static bool
5494 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5495 {
5496 /* Print normal ELF private data. */
5497 _bfd_elf_print_private_bfd_data (abfd, ptr);
5498
5499 if (elf_elfheader (abfd)->e_flags != 0)
5500 {
5501 FILE *file = ptr;
5502
5503 fprintf (file, _("private flags = 0x%lx:"),
5504 elf_elfheader (abfd)->e_flags);
5505
5506 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5507 fprintf (file, _(" [abiv%ld]"),
5508 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5509 fputc ('\n', file);
5510 }
5511
5512 return true;
5513 }
5514
5515 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5516 of the code entry point, and its section, which must be in the same
5517 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5518
5519 static bfd_vma
5520 opd_entry_value (asection *opd_sec,
5521 bfd_vma offset,
5522 asection **code_sec,
5523 bfd_vma *code_off,
5524 bool in_code_sec)
5525 {
5526 bfd *opd_bfd = opd_sec->owner;
5527 Elf_Internal_Rela *relocs;
5528 Elf_Internal_Rela *lo, *hi, *look;
5529 bfd_vma val;
5530
5531 /* No relocs implies we are linking a --just-symbols object, or looking
5532 at a final linked executable with addr2line or somesuch. */
5533 if (opd_sec->reloc_count == 0)
5534 {
5535 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5536
5537 if (contents == NULL)
5538 {
5539 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5540 return (bfd_vma) -1;
5541 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5542 }
5543
5544 /* PR 17512: file: 64b9dfbb. */
5545 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5546 return (bfd_vma) -1;
5547
5548 val = bfd_get_64 (opd_bfd, contents + offset);
5549 if (code_sec != NULL)
5550 {
5551 asection *sec, *likely = NULL;
5552
5553 if (in_code_sec)
5554 {
5555 sec = *code_sec;
5556 if (sec->vma <= val
5557 && val < sec->vma + sec->size)
5558 likely = sec;
5559 else
5560 val = -1;
5561 }
5562 else
5563 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5564 if (sec->vma <= val
5565 && (sec->flags & SEC_LOAD) != 0
5566 && (sec->flags & SEC_ALLOC) != 0)
5567 likely = sec;
5568 if (likely != NULL)
5569 {
5570 *code_sec = likely;
5571 if (code_off != NULL)
5572 *code_off = val - likely->vma;
5573 }
5574 }
5575 return val;
5576 }
5577
5578 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5579
5580 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5581 if (relocs == NULL)
5582 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5583 /* PR 17512: file: df8e1fd6. */
5584 if (relocs == NULL)
5585 return (bfd_vma) -1;
5586
5587 /* Go find the opd reloc at the sym address. */
5588 lo = relocs;
5589 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5590 val = (bfd_vma) -1;
5591 while (lo < hi)
5592 {
5593 look = lo + (hi - lo) / 2;
5594 if (look->r_offset < offset)
5595 lo = look + 1;
5596 else if (look->r_offset > offset)
5597 hi = look;
5598 else
5599 {
5600 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5601
5602 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5603 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5604 {
5605 unsigned long symndx = ELF64_R_SYM (look->r_info);
5606 asection *sec = NULL;
5607
5608 if (symndx >= symtab_hdr->sh_info
5609 && elf_sym_hashes (opd_bfd) != NULL)
5610 {
5611 struct elf_link_hash_entry **sym_hashes;
5612 struct elf_link_hash_entry *rh;
5613
5614 sym_hashes = elf_sym_hashes (opd_bfd);
5615 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5616 if (rh != NULL)
5617 {
5618 rh = elf_follow_link (rh);
5619 if (rh->root.type != bfd_link_hash_defined
5620 && rh->root.type != bfd_link_hash_defweak)
5621 break;
5622 if (rh->root.u.def.section->owner == opd_bfd)
5623 {
5624 val = rh->root.u.def.value;
5625 sec = rh->root.u.def.section;
5626 }
5627 }
5628 }
5629
5630 if (sec == NULL)
5631 {
5632 Elf_Internal_Sym *sym;
5633
5634 if (symndx < symtab_hdr->sh_info)
5635 {
5636 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5637 if (sym == NULL)
5638 {
5639 size_t symcnt = symtab_hdr->sh_info;
5640 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5641 symcnt, 0,
5642 NULL, NULL, NULL);
5643 if (sym == NULL)
5644 break;
5645 symtab_hdr->contents = (bfd_byte *) sym;
5646 }
5647 sym += symndx;
5648 }
5649 else
5650 {
5651 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5652 1, symndx,
5653 NULL, NULL, NULL);
5654 if (sym == NULL)
5655 break;
5656 }
5657 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5658 if (sec == NULL)
5659 break;
5660 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5661 val = sym->st_value;
5662 }
5663
5664 val += look->r_addend;
5665 if (code_off != NULL)
5666 *code_off = val;
5667 if (code_sec != NULL)
5668 {
5669 if (in_code_sec && *code_sec != sec)
5670 return -1;
5671 else
5672 *code_sec = sec;
5673 }
5674 if (sec->output_section != NULL)
5675 val += sec->output_section->vma + sec->output_offset;
5676 }
5677 break;
5678 }
5679 }
5680
5681 return val;
5682 }
5683
5684 /* If the ELF symbol SYM might be a function in SEC, return the
5685 function size and set *CODE_OFF to the function's entry point,
5686 otherwise return zero. */
5687
5688 static bfd_size_type
5689 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5690 bfd_vma *code_off)
5691 {
5692 bfd_size_type size;
5693 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5694
5695 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5696 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5697 return 0;
5698
5699 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5700
5701 /* In theory we should check that the symbol's type satisfies
5702 _bfd_elf_is_function_type(), but there are some function-like
5703 symbols which would fail this test. (eg _start). Instead
5704 we check for hidden, local, notype symbols with zero size.
5705 This type of symbol is generated by the annobin plugin for gcc
5706 and clang, and should not be considered to be a function symbol. */
5707 if (size == 0
5708 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5709 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5710 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5711 return 0;
5712
5713 if (strcmp (sym->section->name, ".opd") == 0)
5714 {
5715 struct _opd_sec_data *opd = get_opd_info (sym->section);
5716 bfd_vma symval = sym->value;
5717
5718 if (opd != NULL
5719 && opd->adjust != NULL
5720 && elf_section_data (sym->section)->relocs != NULL)
5721 {
5722 /* opd_entry_value will use cached relocs that have been
5723 adjusted, but with raw symbols. That means both local
5724 and global symbols need adjusting. */
5725 long adjust = opd->adjust[OPD_NDX (symval)];
5726 if (adjust == -1)
5727 return 0;
5728 symval += adjust;
5729 }
5730
5731 if (opd_entry_value (sym->section, symval,
5732 &sec, code_off, true) == (bfd_vma) -1)
5733 return 0;
5734 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5735 symbol. This size has nothing to do with the code size of the
5736 function, which is what we're supposed to return, but the
5737 code size isn't available without looking up the dot-sym.
5738 However, doing that would be a waste of time particularly
5739 since elf_find_function will look at the dot-sym anyway.
5740 Now, elf_find_function will keep the largest size of any
5741 function sym found at the code address of interest, so return
5742 1 here to avoid it incorrectly caching a larger function size
5743 for a small function. This does mean we return the wrong
5744 size for a new-ABI function of size 24, but all that does is
5745 disable caching for such functions. */
5746 if (size == 24)
5747 size = 1;
5748 }
5749 else
5750 {
5751 if (sym->section != sec)
5752 return 0;
5753 *code_off = sym->value;
5754 }
5755
5756 /* Do not return 0 for the function's size. */
5757 return size ? size : 1;
5758 }
5759
5760 /* Return true if symbol is a strong function defined in an ELFv2
5761 object with st_other localentry bits of zero, ie. its local entry
5762 point coincides with its global entry point. */
5763
5764 static bool
5765 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5766 {
5767 return (h != NULL
5768 && h->type == STT_FUNC
5769 && h->root.type == bfd_link_hash_defined
5770 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5771 && !ppc_elf_hash_entry (h)->non_zero_localentry
5772 && is_ppc64_elf (h->root.u.def.section->owner)
5773 && abiversion (h->root.u.def.section->owner) >= 2);
5774 }
5775
5776 /* Return true if symbol is defined in a regular object file. */
5777
5778 static bool
5779 is_static_defined (struct elf_link_hash_entry *h)
5780 {
5781 return ((h->root.type == bfd_link_hash_defined
5782 || h->root.type == bfd_link_hash_defweak)
5783 && h->root.u.def.section != NULL
5784 && h->root.u.def.section->output_section != NULL);
5785 }
5786
5787 /* If FDH is a function descriptor symbol, return the associated code
5788 entry symbol if it is defined. Return NULL otherwise. */
5789
5790 static struct ppc_link_hash_entry *
5791 defined_code_entry (struct ppc_link_hash_entry *fdh)
5792 {
5793 if (fdh->is_func_descriptor)
5794 {
5795 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5796 if (fh->elf.root.type == bfd_link_hash_defined
5797 || fh->elf.root.type == bfd_link_hash_defweak)
5798 return fh;
5799 }
5800 return NULL;
5801 }
5802
5803 /* If FH is a function code entry symbol, return the associated
5804 function descriptor symbol if it is defined. Return NULL otherwise. */
5805
5806 static struct ppc_link_hash_entry *
5807 defined_func_desc (struct ppc_link_hash_entry *fh)
5808 {
5809 if (fh->oh != NULL
5810 && fh->oh->is_func_descriptor)
5811 {
5812 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5813 if (fdh->elf.root.type == bfd_link_hash_defined
5814 || fdh->elf.root.type == bfd_link_hash_defweak)
5815 return fdh;
5816 }
5817 return NULL;
5818 }
5819
5820 /* Given H is a symbol that satisfies is_static_defined, return the
5821 value in the output file. */
5822
5823 static bfd_vma
5824 defined_sym_val (struct elf_link_hash_entry *h)
5825 {
5826 return (h->root.u.def.section->output_section->vma
5827 + h->root.u.def.section->output_offset
5828 + h->root.u.def.value);
5829 }
5830
5831 /* Return true if H matches __tls_get_addr or one of its variants. */
5832
5833 static bool
5834 is_tls_get_addr (struct elf_link_hash_entry *h,
5835 struct ppc_link_hash_table *htab)
5836 {
5837 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5838 || h == elf_hash_entry (htab->tga_desc_fd)
5839 || h == elf_hash_entry (htab->tls_get_addr)
5840 || h == elf_hash_entry (htab->tga_desc));
5841 }
5842
5843 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5844
5845 /* Garbage collect sections, after first dealing with dot-symbols. */
5846
5847 static bool
5848 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5849 {
5850 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5851
5852 if (htab != NULL && htab->need_func_desc_adj)
5853 {
5854 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5855 htab->need_func_desc_adj = 0;
5856 }
5857 return bfd_elf_gc_sections (abfd, info);
5858 }
5859
5860 /* Mark all our entry sym sections, both opd and code section. */
5861
5862 static void
5863 ppc64_elf_gc_keep (struct bfd_link_info *info)
5864 {
5865 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5866 struct bfd_sym_chain *sym;
5867
5868 if (htab == NULL)
5869 return;
5870
5871 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5872 {
5873 struct ppc_link_hash_entry *eh, *fh;
5874 asection *sec;
5875
5876 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5877 false, false, true));
5878 if (eh == NULL)
5879 continue;
5880 if (eh->elf.root.type != bfd_link_hash_defined
5881 && eh->elf.root.type != bfd_link_hash_defweak)
5882 continue;
5883
5884 fh = defined_code_entry (eh);
5885 if (fh != NULL)
5886 {
5887 sec = fh->elf.root.u.def.section;
5888 sec->flags |= SEC_KEEP;
5889 }
5890 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5891 && opd_entry_value (eh->elf.root.u.def.section,
5892 eh->elf.root.u.def.value,
5893 &sec, NULL, false) != (bfd_vma) -1)
5894 sec->flags |= SEC_KEEP;
5895
5896 sec = eh->elf.root.u.def.section;
5897 sec->flags |= SEC_KEEP;
5898 }
5899 }
5900
5901 /* Mark sections containing dynamically referenced symbols. When
5902 building shared libraries, we must assume that any visible symbol is
5903 referenced. */
5904
5905 static bool
5906 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5907 {
5908 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5909 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5910 struct ppc_link_hash_entry *fdh;
5911 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5912
5913 /* Dynamic linking info is on the func descriptor sym. */
5914 fdh = defined_func_desc (eh);
5915 if (fdh != NULL)
5916 eh = fdh;
5917
5918 if ((eh->elf.root.type == bfd_link_hash_defined
5919 || eh->elf.root.type == bfd_link_hash_defweak)
5920 && (!eh->elf.start_stop
5921 || eh->elf.root.ldscript_def
5922 || !info->start_stop_gc)
5923 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5924 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5925 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5926 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5927 && (!bfd_link_executable (info)
5928 || info->gc_keep_exported
5929 || info->export_dynamic
5930 || (eh->elf.dynamic
5931 && d != NULL
5932 && (*d->match) (&d->head, NULL,
5933 eh->elf.root.root.string)))
5934 && (eh->elf.versioned >= versioned
5935 || !bfd_hide_sym_by_version (info->version_info,
5936 eh->elf.root.root.string)))))
5937 {
5938 asection *code_sec;
5939 struct ppc_link_hash_entry *fh;
5940
5941 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5942
5943 /* Function descriptor syms cause the associated
5944 function code sym section to be marked. */
5945 fh = defined_code_entry (eh);
5946 if (fh != NULL)
5947 {
5948 code_sec = fh->elf.root.u.def.section;
5949 code_sec->flags |= SEC_KEEP;
5950 }
5951 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5952 && opd_entry_value (eh->elf.root.u.def.section,
5953 eh->elf.root.u.def.value,
5954 &code_sec, NULL, false) != (bfd_vma) -1)
5955 code_sec->flags |= SEC_KEEP;
5956 }
5957
5958 return true;
5959 }
5960
5961 /* Return the section that should be marked against GC for a given
5962 relocation. */
5963
5964 static asection *
5965 ppc64_elf_gc_mark_hook (asection *sec,
5966 struct bfd_link_info *info,
5967 Elf_Internal_Rela *rel,
5968 struct elf_link_hash_entry *h,
5969 Elf_Internal_Sym *sym)
5970 {
5971 asection *rsec;
5972
5973 /* Syms return NULL if we're marking .opd, so we avoid marking all
5974 function sections, as all functions are referenced in .opd. */
5975 rsec = NULL;
5976 if (get_opd_info (sec) != NULL)
5977 return rsec;
5978
5979 if (h != NULL)
5980 {
5981 enum elf_ppc64_reloc_type r_type;
5982 struct ppc_link_hash_entry *eh, *fh, *fdh;
5983
5984 r_type = ELF64_R_TYPE (rel->r_info);
5985 switch (r_type)
5986 {
5987 case R_PPC64_GNU_VTINHERIT:
5988 case R_PPC64_GNU_VTENTRY:
5989 break;
5990
5991 default:
5992 switch (h->root.type)
5993 {
5994 case bfd_link_hash_defined:
5995 case bfd_link_hash_defweak:
5996 eh = ppc_elf_hash_entry (h);
5997 fdh = defined_func_desc (eh);
5998 if (fdh != NULL)
5999 {
6000 /* -mcall-aixdesc code references the dot-symbol on
6001 a call reloc. Mark the function descriptor too
6002 against garbage collection. */
6003 fdh->elf.mark = 1;
6004 if (fdh->elf.is_weakalias)
6005 weakdef (&fdh->elf)->mark = 1;
6006 eh = fdh;
6007 }
6008
6009 /* Function descriptor syms cause the associated
6010 function code sym section to be marked. */
6011 fh = defined_code_entry (eh);
6012 if (fh != NULL)
6013 {
6014 /* They also mark their opd section. */
6015 eh->elf.root.u.def.section->gc_mark = 1;
6016
6017 rsec = fh->elf.root.u.def.section;
6018 }
6019 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6020 && opd_entry_value (eh->elf.root.u.def.section,
6021 eh->elf.root.u.def.value,
6022 &rsec, NULL, false) != (bfd_vma) -1)
6023 eh->elf.root.u.def.section->gc_mark = 1;
6024 else
6025 rsec = h->root.u.def.section;
6026 break;
6027
6028 case bfd_link_hash_common:
6029 rsec = h->root.u.c.p->section;
6030 break;
6031
6032 default:
6033 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6034 }
6035 }
6036 }
6037 else
6038 {
6039 struct _opd_sec_data *opd;
6040
6041 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6042 opd = get_opd_info (rsec);
6043 if (opd != NULL && opd->func_sec != NULL)
6044 {
6045 rsec->gc_mark = 1;
6046
6047 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6048 }
6049 }
6050
6051 return rsec;
6052 }
6053
6054 /* The maximum size of .sfpr. */
6055 #define SFPR_MAX (218*4)
6056
6057 struct sfpr_def_parms
6058 {
6059 const char name[12];
6060 unsigned char lo, hi;
6061 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6062 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6063 };
6064
6065 /* Auto-generate _save*, _rest* functions in .sfpr.
6066 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6067 instead. */
6068
6069 static bool
6070 sfpr_define (struct bfd_link_info *info,
6071 const struct sfpr_def_parms *parm,
6072 asection *stub_sec)
6073 {
6074 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6075 unsigned int i;
6076 size_t len = strlen (parm->name);
6077 bool writing = false;
6078 char sym[16];
6079
6080 if (htab == NULL)
6081 return false;
6082
6083 memcpy (sym, parm->name, len);
6084 sym[len + 2] = 0;
6085
6086 for (i = parm->lo; i <= parm->hi; i++)
6087 {
6088 struct ppc_link_hash_entry *h;
6089
6090 sym[len + 0] = i / 10 + '0';
6091 sym[len + 1] = i % 10 + '0';
6092 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6093 writing, true, true));
6094 if (stub_sec != NULL)
6095 {
6096 if (h != NULL
6097 && h->elf.root.type == bfd_link_hash_defined
6098 && h->elf.root.u.def.section == htab->sfpr)
6099 {
6100 struct elf_link_hash_entry *s;
6101 char buf[32];
6102 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6103 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6104 if (s == NULL)
6105 return false;
6106 if (s->root.type == bfd_link_hash_new)
6107 {
6108 s->root.type = bfd_link_hash_defined;
6109 s->root.u.def.section = stub_sec;
6110 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6111 + h->elf.root.u.def.value);
6112 s->ref_regular = 1;
6113 s->def_regular = 1;
6114 s->ref_regular_nonweak = 1;
6115 s->forced_local = 1;
6116 s->non_elf = 0;
6117 s->root.linker_def = 1;
6118 }
6119 }
6120 continue;
6121 }
6122 if (h != NULL)
6123 {
6124 h->save_res = 1;
6125 if (!h->elf.def_regular)
6126 {
6127 h->elf.root.type = bfd_link_hash_defined;
6128 h->elf.root.u.def.section = htab->sfpr;
6129 h->elf.root.u.def.value = htab->sfpr->size;
6130 h->elf.type = STT_FUNC;
6131 h->elf.def_regular = 1;
6132 h->elf.non_elf = 0;
6133 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6134 writing = true;
6135 if (htab->sfpr->contents == NULL)
6136 {
6137 htab->sfpr->contents
6138 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6139 if (htab->sfpr->contents == NULL)
6140 return false;
6141 }
6142 }
6143 }
6144 if (writing)
6145 {
6146 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6147 if (i != parm->hi)
6148 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6149 else
6150 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6151 htab->sfpr->size = p - htab->sfpr->contents;
6152 }
6153 }
6154
6155 return true;
6156 }
6157
6158 static bfd_byte *
6159 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6160 {
6161 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6162 return p + 4;
6163 }
6164
6165 static bfd_byte *
6166 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6167 {
6168 p = savegpr0 (abfd, p, r);
6169 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6170 p = p + 4;
6171 bfd_put_32 (abfd, BLR, p);
6172 return p + 4;
6173 }
6174
6175 static bfd_byte *
6176 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6177 {
6178 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6179 return p + 4;
6180 }
6181
6182 static bfd_byte *
6183 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6184 {
6185 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6186 p = p + 4;
6187 p = restgpr0 (abfd, p, r);
6188 bfd_put_32 (abfd, MTLR_R0, p);
6189 p = p + 4;
6190 if (r == 29)
6191 {
6192 p = restgpr0 (abfd, p, 30);
6193 p = restgpr0 (abfd, p, 31);
6194 }
6195 bfd_put_32 (abfd, BLR, p);
6196 return p + 4;
6197 }
6198
6199 static bfd_byte *
6200 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6201 {
6202 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6203 return p + 4;
6204 }
6205
6206 static bfd_byte *
6207 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6208 {
6209 p = savegpr1 (abfd, p, r);
6210 bfd_put_32 (abfd, BLR, p);
6211 return p + 4;
6212 }
6213
6214 static bfd_byte *
6215 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6216 {
6217 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6218 return p + 4;
6219 }
6220
6221 static bfd_byte *
6222 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6223 {
6224 p = restgpr1 (abfd, p, r);
6225 bfd_put_32 (abfd, BLR, p);
6226 return p + 4;
6227 }
6228
6229 static bfd_byte *
6230 savefpr (bfd *abfd, bfd_byte *p, int r)
6231 {
6232 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6233 return p + 4;
6234 }
6235
6236 static bfd_byte *
6237 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6238 {
6239 p = savefpr (abfd, p, r);
6240 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6241 p = p + 4;
6242 bfd_put_32 (abfd, BLR, p);
6243 return p + 4;
6244 }
6245
6246 static bfd_byte *
6247 restfpr (bfd *abfd, bfd_byte *p, int r)
6248 {
6249 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6250 return p + 4;
6251 }
6252
6253 static bfd_byte *
6254 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6255 {
6256 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6257 p = p + 4;
6258 p = restfpr (abfd, p, r);
6259 bfd_put_32 (abfd, MTLR_R0, p);
6260 p = p + 4;
6261 if (r == 29)
6262 {
6263 p = restfpr (abfd, p, 30);
6264 p = restfpr (abfd, p, 31);
6265 }
6266 bfd_put_32 (abfd, BLR, p);
6267 return p + 4;
6268 }
6269
6270 static bfd_byte *
6271 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6272 {
6273 p = savefpr (abfd, p, r);
6274 bfd_put_32 (abfd, BLR, p);
6275 return p + 4;
6276 }
6277
6278 static bfd_byte *
6279 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6280 {
6281 p = restfpr (abfd, p, r);
6282 bfd_put_32 (abfd, BLR, p);
6283 return p + 4;
6284 }
6285
6286 static bfd_byte *
6287 savevr (bfd *abfd, bfd_byte *p, int r)
6288 {
6289 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6290 p = p + 4;
6291 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6292 return p + 4;
6293 }
6294
6295 static bfd_byte *
6296 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6297 {
6298 p = savevr (abfd, p, r);
6299 bfd_put_32 (abfd, BLR, p);
6300 return p + 4;
6301 }
6302
6303 static bfd_byte *
6304 restvr (bfd *abfd, bfd_byte *p, int r)
6305 {
6306 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6307 p = p + 4;
6308 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6309 return p + 4;
6310 }
6311
6312 static bfd_byte *
6313 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6314 {
6315 p = restvr (abfd, p, r);
6316 bfd_put_32 (abfd, BLR, p);
6317 return p + 4;
6318 }
6319
6320 #define STDU_R1_0R1 0xf8210001
6321 #define ADDI_R1_R1 0x38210000
6322
6323 /* Emit prologue of wrapper preserving regs around a call to
6324 __tls_get_addr_opt. */
6325
6326 static bfd_byte *
6327 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6328 {
6329 unsigned int i;
6330
6331 bfd_put_32 (obfd, MFLR_R0, p);
6332 p += 4;
6333 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6334 p += 4;
6335
6336 if (htab->opd_abi)
6337 {
6338 for (i = 4; i < 12; i++)
6339 {
6340 bfd_put_32 (obfd,
6341 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6342 p += 4;
6343 }
6344 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6345 p += 4;
6346 }
6347 else
6348 {
6349 for (i = 4; i < 12; i++)
6350 {
6351 bfd_put_32 (obfd,
6352 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6353 p += 4;
6354 }
6355 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6356 p += 4;
6357 }
6358 return p;
6359 }
6360
6361 /* Emit epilogue of wrapper preserving regs around a call to
6362 __tls_get_addr_opt. */
6363
6364 static bfd_byte *
6365 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6366 {
6367 unsigned int i;
6368
6369 if (htab->opd_abi)
6370 {
6371 for (i = 4; i < 12; i++)
6372 {
6373 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6374 p += 4;
6375 }
6376 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6377 p += 4;
6378 }
6379 else
6380 {
6381 for (i = 4; i < 12; i++)
6382 {
6383 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6384 p += 4;
6385 }
6386 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6387 p += 4;
6388 }
6389 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6390 p += 4;
6391 bfd_put_32 (obfd, MTLR_R0, p);
6392 p += 4;
6393 bfd_put_32 (obfd, BLR, p);
6394 p += 4;
6395 return p;
6396 }
6397
6398 /* Called via elf_link_hash_traverse to transfer dynamic linking
6399 information on function code symbol entries to their corresponding
6400 function descriptor symbol entries. Must not be called twice for
6401 any given code symbol. */
6402
6403 static bool
6404 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6405 {
6406 struct bfd_link_info *info;
6407 struct ppc_link_hash_table *htab;
6408 struct ppc_link_hash_entry *fh;
6409 struct ppc_link_hash_entry *fdh;
6410 bool force_local;
6411
6412 fh = ppc_elf_hash_entry (h);
6413 if (fh->elf.root.type == bfd_link_hash_indirect)
6414 return true;
6415
6416 if (!fh->is_func)
6417 return true;
6418
6419 if (fh->elf.root.root.string[0] != '.'
6420 || fh->elf.root.root.string[1] == '\0')
6421 return true;
6422
6423 info = inf;
6424 htab = ppc_hash_table (info);
6425 if (htab == NULL)
6426 return false;
6427
6428 /* Find the corresponding function descriptor symbol. */
6429 fdh = lookup_fdh (fh, htab);
6430
6431 /* Resolve undefined references to dot-symbols as the value
6432 in the function descriptor, if we have one in a regular object.
6433 This is to satisfy cases like ".quad .foo". Calls to functions
6434 in dynamic objects are handled elsewhere. */
6435 if ((fh->elf.root.type == bfd_link_hash_undefined
6436 || fh->elf.root.type == bfd_link_hash_undefweak)
6437 && (fdh->elf.root.type == bfd_link_hash_defined
6438 || fdh->elf.root.type == bfd_link_hash_defweak)
6439 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6440 && opd_entry_value (fdh->elf.root.u.def.section,
6441 fdh->elf.root.u.def.value,
6442 &fh->elf.root.u.def.section,
6443 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6444 {
6445 fh->elf.root.type = fdh->elf.root.type;
6446 fh->elf.forced_local = 1;
6447 fh->elf.def_regular = fdh->elf.def_regular;
6448 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6449 }
6450
6451 if (!fh->elf.dynamic)
6452 {
6453 struct plt_entry *ent;
6454
6455 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6456 if (ent->plt.refcount > 0)
6457 break;
6458 if (ent == NULL)
6459 {
6460 if (fdh != NULL && fdh->fake)
6461 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6462 return true;
6463 }
6464 }
6465
6466 /* Create a descriptor as undefined if necessary. */
6467 if (fdh == NULL
6468 && !bfd_link_executable (info)
6469 && (fh->elf.root.type == bfd_link_hash_undefined
6470 || fh->elf.root.type == bfd_link_hash_undefweak))
6471 {
6472 fdh = make_fdh (info, fh);
6473 if (fdh == NULL)
6474 return false;
6475 }
6476
6477 /* We can't support overriding of symbols on a fake descriptor. */
6478 if (fdh != NULL
6479 && fdh->fake
6480 && (fh->elf.root.type == bfd_link_hash_defined
6481 || fh->elf.root.type == bfd_link_hash_defweak))
6482 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6483
6484 /* Transfer dynamic linking information to the function descriptor. */
6485 if (fdh != NULL)
6486 {
6487 fdh->elf.ref_regular |= fh->elf.ref_regular;
6488 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6489 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6490 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6491 fdh->elf.dynamic |= fh->elf.dynamic;
6492 fdh->elf.needs_plt |= (fh->elf.needs_plt
6493 || fh->elf.type == STT_FUNC
6494 || fh->elf.type == STT_GNU_IFUNC);
6495 move_plt_plist (fh, fdh);
6496
6497 if (!fdh->elf.forced_local
6498 && fh->elf.dynindx != -1)
6499 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6500 return false;
6501 }
6502
6503 /* Now that the info is on the function descriptor, clear the
6504 function code sym info. Any function code syms for which we
6505 don't have a definition in a regular file, we force local.
6506 This prevents a shared library from exporting syms that have
6507 been imported from another library. Function code syms that
6508 are really in the library we must leave global to prevent the
6509 linker dragging in a definition from a static library. */
6510 force_local = (!fh->elf.def_regular
6511 || fdh == NULL
6512 || !fdh->elf.def_regular
6513 || fdh->elf.forced_local);
6514 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6515
6516 return true;
6517 }
6518
6519 static const struct sfpr_def_parms save_res_funcs[] =
6520 {
6521 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6522 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6523 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6524 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6525 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6526 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6527 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6528 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6529 { "._savef", 14, 31, savefpr, savefpr1_tail },
6530 { "._restf", 14, 31, restfpr, restfpr1_tail },
6531 { "_savevr_", 20, 31, savevr, savevr_tail },
6532 { "_restvr_", 20, 31, restvr, restvr_tail }
6533 };
6534
6535 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6536 this hook to a) run the edit functions in this file, b) provide
6537 some gcc support functions, and c) transfer dynamic linking
6538 information gathered so far on function code symbol entries, to
6539 their corresponding function descriptor symbol entries. */
6540
6541 static bool
6542 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6543 {
6544 struct ppc_link_hash_table *htab;
6545
6546 htab = ppc_hash_table (info);
6547 if (htab == NULL)
6548 return false;
6549
6550 /* Call back into the linker, which then runs the edit functions. */
6551 htab->params->edit ();
6552
6553 /* Provide any missing _save* and _rest* functions. */
6554 if (htab->sfpr != NULL)
6555 {
6556 unsigned int i;
6557
6558 htab->sfpr->size = 0;
6559 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6560 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6561 return false;
6562 if (htab->sfpr->size == 0)
6563 htab->sfpr->flags |= SEC_EXCLUDE;
6564 }
6565
6566 if (bfd_link_relocatable (info))
6567 return true;
6568
6569 if (htab->elf.hgot != NULL)
6570 {
6571 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6572 /* Make .TOC. defined so as to prevent it being made dynamic.
6573 The wrong value here is fixed later in ppc64_elf_set_toc. */
6574 if (!htab->elf.hgot->def_regular
6575 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6576 {
6577 htab->elf.hgot->root.type = bfd_link_hash_defined;
6578 htab->elf.hgot->root.u.def.value = 0;
6579 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6580 htab->elf.hgot->def_regular = 1;
6581 htab->elf.hgot->root.linker_def = 1;
6582 }
6583 htab->elf.hgot->type = STT_OBJECT;
6584 htab->elf.hgot->other
6585 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6586 }
6587
6588 return true;
6589 }
6590
6591 /* Return true if we have dynamic relocs against H or any of its weak
6592 aliases, that apply to read-only sections. Cannot be used after
6593 size_dynamic_sections. */
6594
6595 static bool
6596 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6597 {
6598 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6599 do
6600 {
6601 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6602 return true;
6603 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6604 }
6605 while (eh != NULL && &eh->elf != h);
6606
6607 return false;
6608 }
6609
6610 /* Return whether EH has pc-relative dynamic relocs. */
6611
6612 static bool
6613 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6614 {
6615 struct ppc_dyn_relocs *p;
6616
6617 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6618 if (p->pc_count != 0)
6619 return true;
6620 return false;
6621 }
6622
6623 /* Return true if a global entry stub will be created for H. Valid
6624 for ELFv2 before plt entries have been allocated. */
6625
6626 static bool
6627 global_entry_stub (struct elf_link_hash_entry *h)
6628 {
6629 struct plt_entry *pent;
6630
6631 if (!h->pointer_equality_needed
6632 || h->def_regular)
6633 return false;
6634
6635 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6636 if (pent->plt.refcount > 0
6637 && pent->addend == 0)
6638 return true;
6639
6640 return false;
6641 }
6642
6643 /* Adjust a symbol defined by a dynamic object and referenced by a
6644 regular object. The current definition is in some section of the
6645 dynamic object, but we're not including those sections. We have to
6646 change the definition to something the rest of the link can
6647 understand. */
6648
6649 static bool
6650 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6651 struct elf_link_hash_entry *h)
6652 {
6653 struct ppc_link_hash_table *htab;
6654 asection *s, *srel;
6655
6656 htab = ppc_hash_table (info);
6657 if (htab == NULL)
6658 return false;
6659
6660 /* Deal with function syms. */
6661 if (h->type == STT_FUNC
6662 || h->type == STT_GNU_IFUNC
6663 || h->needs_plt)
6664 {
6665 bool local = (ppc_elf_hash_entry (h)->save_res
6666 || SYMBOL_CALLS_LOCAL (info, h)
6667 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6668 /* Discard dyn_relocs when non-pic if we've decided that a
6669 function symbol is local and not an ifunc. We keep dynamic
6670 relocs for ifuncs when local rather than always emitting a
6671 plt call stub for them and defining the symbol on the call
6672 stub. We can't do that for ELFv1 anyway (a function symbol
6673 is defined on a descriptor, not code) and it can be faster at
6674 run-time due to not needing to bounce through a stub. The
6675 dyn_relocs for ifuncs will be applied even in a static
6676 executable. */
6677 if (!bfd_link_pic (info)
6678 && h->type != STT_GNU_IFUNC
6679 && local)
6680 h->dyn_relocs = NULL;
6681
6682 /* Clear procedure linkage table information for any symbol that
6683 won't need a .plt entry. */
6684 struct plt_entry *ent;
6685 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6686 if (ent->plt.refcount > 0)
6687 break;
6688 if (ent == NULL
6689 || (h->type != STT_GNU_IFUNC
6690 && local
6691 && (htab->can_convert_all_inline_plt
6692 || (ppc_elf_hash_entry (h)->tls_mask
6693 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6694 {
6695 h->plt.plist = NULL;
6696 h->needs_plt = 0;
6697 h->pointer_equality_needed = 0;
6698 }
6699 else if (abiversion (info->output_bfd) >= 2)
6700 {
6701 /* Taking a function's address in a read/write section
6702 doesn't require us to define the function symbol in the
6703 executable on a global entry stub. A dynamic reloc can
6704 be used instead. The reason we prefer a few more dynamic
6705 relocs is that calling via a global entry stub costs a
6706 few more instructions, and pointer_equality_needed causes
6707 extra work in ld.so when resolving these symbols. */
6708 if (global_entry_stub (h))
6709 {
6710 if (!_bfd_elf_readonly_dynrelocs (h))
6711 {
6712 h->pointer_equality_needed = 0;
6713 /* If we haven't seen a branch reloc and the symbol
6714 isn't an ifunc then we don't need a plt entry. */
6715 if (!h->needs_plt)
6716 h->plt.plist = NULL;
6717 }
6718 else if (!bfd_link_pic (info))
6719 /* We are going to be defining the function symbol on the
6720 plt stub, so no dyn_relocs needed when non-pic. */
6721 h->dyn_relocs = NULL;
6722 }
6723
6724 /* ELFv2 function symbols can't have copy relocs. */
6725 return true;
6726 }
6727 else if (!h->needs_plt
6728 && !_bfd_elf_readonly_dynrelocs (h))
6729 {
6730 /* If we haven't seen a branch reloc and the symbol isn't an
6731 ifunc then we don't need a plt entry. */
6732 h->plt.plist = NULL;
6733 h->pointer_equality_needed = 0;
6734 return true;
6735 }
6736 }
6737 else
6738 h->plt.plist = NULL;
6739
6740 /* If this is a weak symbol, and there is a real definition, the
6741 processor independent code will have arranged for us to see the
6742 real definition first, and we can just use the same value. */
6743 if (h->is_weakalias)
6744 {
6745 struct elf_link_hash_entry *def = weakdef (h);
6746 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6747 h->root.u.def.section = def->root.u.def.section;
6748 h->root.u.def.value = def->root.u.def.value;
6749 if (def->root.u.def.section == htab->elf.sdynbss
6750 || def->root.u.def.section == htab->elf.sdynrelro)
6751 h->dyn_relocs = NULL;
6752 return true;
6753 }
6754
6755 /* If we are creating a shared library, we must presume that the
6756 only references to the symbol are via the global offset table.
6757 For such cases we need not do anything here; the relocations will
6758 be handled correctly by relocate_section. */
6759 if (!bfd_link_executable (info))
6760 return true;
6761
6762 /* If there are no references to this symbol that do not use the
6763 GOT, we don't need to generate a copy reloc. */
6764 if (!h->non_got_ref)
6765 return true;
6766
6767 /* Don't generate a copy reloc for symbols defined in the executable. */
6768 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6769
6770 /* If -z nocopyreloc was given, don't generate them either. */
6771 || info->nocopyreloc
6772
6773 /* If we don't find any dynamic relocs in read-only sections, then
6774 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6775 || (ELIMINATE_COPY_RELOCS
6776 && !h->needs_copy
6777 && !alias_readonly_dynrelocs (h))
6778
6779 /* Protected variables do not work with .dynbss. The copy in
6780 .dynbss won't be used by the shared library with the protected
6781 definition for the variable. Text relocations are preferable
6782 to an incorrect program. */
6783 || h->protected_def)
6784 return true;
6785
6786 if (h->type == STT_FUNC
6787 || h->type == STT_GNU_IFUNC)
6788 {
6789 /* .dynbss copies of function symbols only work if we have
6790 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6791 use dot-symbols and set the function symbol size to the text
6792 size of the function rather than the size of the descriptor.
6793 That's wrong for copying a descriptor. */
6794 if (ppc_elf_hash_entry (h)->oh == NULL
6795 || !(h->size == 24 || h->size == 16))
6796 return true;
6797
6798 /* We should never get here, but unfortunately there are old
6799 versions of gcc (circa gcc-3.2) that improperly for the
6800 ELFv1 ABI put initialized function pointers, vtable refs and
6801 suchlike in read-only sections. Allow them to proceed, but
6802 warn that this might break at runtime. */
6803 info->callbacks->einfo
6804 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6805 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6806 h->root.root.string);
6807 }
6808
6809 /* This is a reference to a symbol defined by a dynamic object which
6810 is not a function. */
6811
6812 /* We must allocate the symbol in our .dynbss section, which will
6813 become part of the .bss section of the executable. There will be
6814 an entry for this symbol in the .dynsym section. The dynamic
6815 object will contain position independent code, so all references
6816 from the dynamic object to this symbol will go through the global
6817 offset table. The dynamic linker will use the .dynsym entry to
6818 determine the address it must put in the global offset table, so
6819 both the dynamic object and the regular object will refer to the
6820 same memory location for the variable. */
6821 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6822 {
6823 s = htab->elf.sdynrelro;
6824 srel = htab->elf.sreldynrelro;
6825 }
6826 else
6827 {
6828 s = htab->elf.sdynbss;
6829 srel = htab->elf.srelbss;
6830 }
6831 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6832 {
6833 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6834 linker to copy the initial value out of the dynamic object
6835 and into the runtime process image. */
6836 srel->size += sizeof (Elf64_External_Rela);
6837 h->needs_copy = 1;
6838 }
6839
6840 /* We no longer want dyn_relocs. */
6841 h->dyn_relocs = NULL;
6842 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6843 }
6844
6845 /* If given a function descriptor symbol, hide both the function code
6846 sym and the descriptor. */
6847 static void
6848 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6849 struct elf_link_hash_entry *h,
6850 bool force_local)
6851 {
6852 struct ppc_link_hash_entry *eh;
6853 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6854
6855 if (ppc_hash_table (info) == NULL)
6856 return;
6857
6858 eh = ppc_elf_hash_entry (h);
6859 if (eh->is_func_descriptor)
6860 {
6861 struct ppc_link_hash_entry *fh = eh->oh;
6862
6863 if (fh == NULL)
6864 {
6865 const char *p, *q;
6866 struct elf_link_hash_table *htab = elf_hash_table (info);
6867 char save;
6868
6869 /* We aren't supposed to use alloca in BFD because on
6870 systems which do not have alloca the version in libiberty
6871 calls xmalloc, which might cause the program to crash
6872 when it runs out of memory. This function doesn't have a
6873 return status, so there's no way to gracefully return an
6874 error. So cheat. We know that string[-1] can be safely
6875 accessed; It's either a string in an ELF string table,
6876 or allocated in an objalloc structure. */
6877
6878 p = eh->elf.root.root.string - 1;
6879 save = *p;
6880 *(char *) p = '.';
6881 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6882 false, false));
6883 *(char *) p = save;
6884
6885 /* Unfortunately, if it so happens that the string we were
6886 looking for was allocated immediately before this string,
6887 then we overwrote the string terminator. That's the only
6888 reason the lookup should fail. */
6889 if (fh == NULL)
6890 {
6891 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6892 while (q >= eh->elf.root.root.string && *q == *p)
6893 --q, --p;
6894 if (q < eh->elf.root.root.string && *p == '.')
6895 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6896 false, false));
6897 }
6898 if (fh != NULL)
6899 {
6900 eh->oh = fh;
6901 fh->oh = eh;
6902 }
6903 }
6904 if (fh != NULL)
6905 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6906 }
6907 }
6908
6909 static bool
6910 get_sym_h (struct elf_link_hash_entry **hp,
6911 Elf_Internal_Sym **symp,
6912 asection **symsecp,
6913 unsigned char **tls_maskp,
6914 Elf_Internal_Sym **locsymsp,
6915 unsigned long r_symndx,
6916 bfd *ibfd)
6917 {
6918 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6919
6920 if (r_symndx >= symtab_hdr->sh_info)
6921 {
6922 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6923 struct elf_link_hash_entry *h;
6924
6925 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6926 h = elf_follow_link (h);
6927
6928 if (hp != NULL)
6929 *hp = h;
6930
6931 if (symp != NULL)
6932 *symp = NULL;
6933
6934 if (symsecp != NULL)
6935 {
6936 asection *symsec = NULL;
6937 if (h->root.type == bfd_link_hash_defined
6938 || h->root.type == bfd_link_hash_defweak)
6939 symsec = h->root.u.def.section;
6940 *symsecp = symsec;
6941 }
6942
6943 if (tls_maskp != NULL)
6944 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6945 }
6946 else
6947 {
6948 Elf_Internal_Sym *sym;
6949 Elf_Internal_Sym *locsyms = *locsymsp;
6950
6951 if (locsyms == NULL)
6952 {
6953 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6954 if (locsyms == NULL)
6955 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6956 symtab_hdr->sh_info,
6957 0, NULL, NULL, NULL);
6958 if (locsyms == NULL)
6959 return false;
6960 *locsymsp = locsyms;
6961 }
6962 sym = locsyms + r_symndx;
6963
6964 if (hp != NULL)
6965 *hp = NULL;
6966
6967 if (symp != NULL)
6968 *symp = sym;
6969
6970 if (symsecp != NULL)
6971 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6972
6973 if (tls_maskp != NULL)
6974 {
6975 struct got_entry **lgot_ents;
6976 unsigned char *tls_mask;
6977
6978 tls_mask = NULL;
6979 lgot_ents = elf_local_got_ents (ibfd);
6980 if (lgot_ents != NULL)
6981 {
6982 struct plt_entry **local_plt = (struct plt_entry **)
6983 (lgot_ents + symtab_hdr->sh_info);
6984 unsigned char *lgot_masks = (unsigned char *)
6985 (local_plt + symtab_hdr->sh_info);
6986 tls_mask = &lgot_masks[r_symndx];
6987 }
6988 *tls_maskp = tls_mask;
6989 }
6990 }
6991 return true;
6992 }
6993
6994 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6995 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6996 type suitable for optimization, and 1 otherwise. */
6997
6998 static int
6999 get_tls_mask (unsigned char **tls_maskp,
7000 unsigned long *toc_symndx,
7001 bfd_vma *toc_addend,
7002 Elf_Internal_Sym **locsymsp,
7003 const Elf_Internal_Rela *rel,
7004 bfd *ibfd)
7005 {
7006 unsigned long r_symndx;
7007 int next_r;
7008 struct elf_link_hash_entry *h;
7009 Elf_Internal_Sym *sym;
7010 asection *sec;
7011 bfd_vma off;
7012
7013 r_symndx = ELF64_R_SYM (rel->r_info);
7014 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7015 return 0;
7016
7017 if ((*tls_maskp != NULL
7018 && (**tls_maskp & TLS_TLS) != 0
7019 && **tls_maskp != (TLS_TLS | TLS_MARK))
7020 || sec == NULL
7021 || ppc64_elf_section_data (sec) == NULL
7022 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7023 return 1;
7024
7025 /* Look inside a TOC section too. */
7026 if (h != NULL)
7027 {
7028 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7029 off = h->root.u.def.value;
7030 }
7031 else
7032 off = sym->st_value;
7033 off += rel->r_addend;
7034 BFD_ASSERT (off % 8 == 0);
7035 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7036 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7037 if (toc_symndx != NULL)
7038 *toc_symndx = r_symndx;
7039 if (toc_addend != NULL)
7040 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7041 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7042 return 0;
7043 if ((h == NULL || is_static_defined (h))
7044 && (next_r == -1 || next_r == -2))
7045 return 1 - next_r;
7046 return 1;
7047 }
7048
7049 /* Find (or create) an entry in the tocsave hash table. */
7050
7051 static struct tocsave_entry *
7052 tocsave_find (struct ppc_link_hash_table *htab,
7053 enum insert_option insert,
7054 Elf_Internal_Sym **local_syms,
7055 const Elf_Internal_Rela *irela,
7056 bfd *ibfd)
7057 {
7058 unsigned long r_indx;
7059 struct elf_link_hash_entry *h;
7060 Elf_Internal_Sym *sym;
7061 struct tocsave_entry ent, *p;
7062 hashval_t hash;
7063 struct tocsave_entry **slot;
7064
7065 r_indx = ELF64_R_SYM (irela->r_info);
7066 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7067 return NULL;
7068 if (ent.sec == NULL || ent.sec->output_section == NULL)
7069 {
7070 _bfd_error_handler
7071 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7072 return NULL;
7073 }
7074
7075 if (h != NULL)
7076 ent.offset = h->root.u.def.value;
7077 else
7078 ent.offset = sym->st_value;
7079 ent.offset += irela->r_addend;
7080
7081 hash = tocsave_htab_hash (&ent);
7082 slot = ((struct tocsave_entry **)
7083 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7084 if (slot == NULL)
7085 return NULL;
7086
7087 if (*slot == NULL)
7088 {
7089 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7090 if (p == NULL)
7091 return NULL;
7092 *p = ent;
7093 *slot = p;
7094 }
7095 return *slot;
7096 }
7097
7098 /* Adjust all global syms defined in opd sections. In gcc generated
7099 code for the old ABI, these will already have been done. */
7100
7101 static bool
7102 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7103 {
7104 struct ppc_link_hash_entry *eh;
7105 asection *sym_sec;
7106 struct _opd_sec_data *opd;
7107
7108 if (h->root.type == bfd_link_hash_indirect)
7109 return true;
7110
7111 if (h->root.type != bfd_link_hash_defined
7112 && h->root.type != bfd_link_hash_defweak)
7113 return true;
7114
7115 eh = ppc_elf_hash_entry (h);
7116 if (eh->adjust_done)
7117 return true;
7118
7119 sym_sec = eh->elf.root.u.def.section;
7120 opd = get_opd_info (sym_sec);
7121 if (opd != NULL && opd->adjust != NULL)
7122 {
7123 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7124 if (adjust == -1)
7125 {
7126 /* This entry has been deleted. */
7127 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7128 if (dsec == NULL)
7129 {
7130 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7131 if (discarded_section (dsec))
7132 {
7133 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7134 break;
7135 }
7136 }
7137 eh->elf.root.u.def.value = 0;
7138 eh->elf.root.u.def.section = dsec;
7139 }
7140 else
7141 eh->elf.root.u.def.value += adjust;
7142 eh->adjust_done = 1;
7143 }
7144 return true;
7145 }
7146
7147 /* Handles decrementing dynamic reloc counts for the reloc specified by
7148 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7149 have already been determined. */
7150
7151 static bool
7152 dec_dynrel_count (const Elf_Internal_Rela *rel,
7153 asection *sec,
7154 struct bfd_link_info *info,
7155 Elf_Internal_Sym **local_syms,
7156 struct elf_link_hash_entry *h,
7157 Elf_Internal_Sym *sym)
7158 {
7159 enum elf_ppc64_reloc_type r_type;
7160 asection *sym_sec = NULL;
7161
7162 /* Can this reloc be dynamic? This switch, and later tests here
7163 should be kept in sync with the code in check_relocs. */
7164 r_type = ELF64_R_TYPE (rel->r_info);
7165 switch (r_type)
7166 {
7167 default:
7168 return true;
7169
7170 case R_PPC64_TOC16:
7171 case R_PPC64_TOC16_DS:
7172 case R_PPC64_TOC16_LO:
7173 case R_PPC64_TOC16_HI:
7174 case R_PPC64_TOC16_HA:
7175 case R_PPC64_TOC16_LO_DS:
7176 if (h == NULL)
7177 return true;
7178 break;
7179
7180 case R_PPC64_TPREL16:
7181 case R_PPC64_TPREL16_LO:
7182 case R_PPC64_TPREL16_HI:
7183 case R_PPC64_TPREL16_HA:
7184 case R_PPC64_TPREL16_DS:
7185 case R_PPC64_TPREL16_LO_DS:
7186 case R_PPC64_TPREL16_HIGH:
7187 case R_PPC64_TPREL16_HIGHA:
7188 case R_PPC64_TPREL16_HIGHER:
7189 case R_PPC64_TPREL16_HIGHERA:
7190 case R_PPC64_TPREL16_HIGHEST:
7191 case R_PPC64_TPREL16_HIGHESTA:
7192 case R_PPC64_TPREL64:
7193 case R_PPC64_TPREL34:
7194 case R_PPC64_DTPMOD64:
7195 case R_PPC64_DTPREL64:
7196 case R_PPC64_ADDR64:
7197 case R_PPC64_REL30:
7198 case R_PPC64_REL32:
7199 case R_PPC64_REL64:
7200 case R_PPC64_ADDR14:
7201 case R_PPC64_ADDR14_BRNTAKEN:
7202 case R_PPC64_ADDR14_BRTAKEN:
7203 case R_PPC64_ADDR16:
7204 case R_PPC64_ADDR16_DS:
7205 case R_PPC64_ADDR16_HA:
7206 case R_PPC64_ADDR16_HI:
7207 case R_PPC64_ADDR16_HIGH:
7208 case R_PPC64_ADDR16_HIGHA:
7209 case R_PPC64_ADDR16_HIGHER:
7210 case R_PPC64_ADDR16_HIGHERA:
7211 case R_PPC64_ADDR16_HIGHEST:
7212 case R_PPC64_ADDR16_HIGHESTA:
7213 case R_PPC64_ADDR16_LO:
7214 case R_PPC64_ADDR16_LO_DS:
7215 case R_PPC64_ADDR24:
7216 case R_PPC64_ADDR32:
7217 case R_PPC64_UADDR16:
7218 case R_PPC64_UADDR32:
7219 case R_PPC64_UADDR64:
7220 case R_PPC64_TOC:
7221 case R_PPC64_D34:
7222 case R_PPC64_D34_LO:
7223 case R_PPC64_D34_HI30:
7224 case R_PPC64_D34_HA30:
7225 case R_PPC64_ADDR16_HIGHER34:
7226 case R_PPC64_ADDR16_HIGHERA34:
7227 case R_PPC64_ADDR16_HIGHEST34:
7228 case R_PPC64_ADDR16_HIGHESTA34:
7229 case R_PPC64_D28:
7230 break;
7231 }
7232
7233 if (local_syms != NULL)
7234 {
7235 unsigned long r_symndx;
7236 bfd *ibfd = sec->owner;
7237
7238 r_symndx = ELF64_R_SYM (rel->r_info);
7239 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7240 return false;
7241 }
7242
7243 if ((h != NULL
7244 && !SYMBOL_REFERENCES_LOCAL (info, h))
7245 || (bfd_link_pic (info)
7246 && (h != NULL
7247 ? !bfd_is_abs_symbol (&h->root)
7248 : sym_sec != bfd_abs_section_ptr)
7249 && must_be_dyn_reloc (info, r_type))
7250 || (!bfd_link_pic (info)
7251 && (h != NULL
7252 ? h->type == STT_GNU_IFUNC
7253 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7254 ;
7255 else
7256 return true;
7257
7258 if (h != NULL)
7259 {
7260 struct ppc_dyn_relocs *p;
7261 struct ppc_dyn_relocs **pp;
7262 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7263
7264 /* elf_gc_sweep may have already removed all dyn relocs associated
7265 with local syms for a given section. Also, symbol flags are
7266 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7267 report a dynreloc miscount. */
7268 if (*pp == NULL && info->gc_sections)
7269 return true;
7270
7271 while ((p = *pp) != NULL)
7272 {
7273 if (p->sec == sec)
7274 {
7275 if (!must_be_dyn_reloc (info, r_type))
7276 p->pc_count -= 1;
7277 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7278 && rel->r_offset % 2 == 0
7279 && sec->alignment_power != 0)
7280 p->rel_count -= 1;
7281 p->count -= 1;
7282 if (p->count == 0)
7283 *pp = p->next;
7284 return true;
7285 }
7286 pp = &p->next;
7287 }
7288 }
7289 else
7290 {
7291 struct ppc_local_dyn_relocs *p;
7292 struct ppc_local_dyn_relocs **pp;
7293 void *vpp;
7294 bool is_ifunc;
7295
7296 if (local_syms == NULL)
7297 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7298 if (sym_sec == NULL)
7299 sym_sec = sec;
7300
7301 vpp = &elf_section_data (sym_sec)->local_dynrel;
7302 pp = (struct ppc_local_dyn_relocs **) vpp;
7303
7304 if (*pp == NULL && info->gc_sections)
7305 return true;
7306
7307 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7308 while ((p = *pp) != NULL)
7309 {
7310 if (p->sec == sec && p->ifunc == is_ifunc)
7311 {
7312 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7313 && rel->r_offset % 2 == 0
7314 && sec->alignment_power != 0)
7315 p->rel_count -= 1;
7316 p->count -= 1;
7317 if (p->count == 0)
7318 *pp = p->next;
7319 return true;
7320 }
7321 pp = &p->next;
7322 }
7323 }
7324
7325 /* xgettext:c-format */
7326 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7327 sec->owner, sec);
7328 bfd_set_error (bfd_error_bad_value);
7329 return false;
7330 }
7331
7332 /* Remove unused Official Procedure Descriptor entries. Currently we
7333 only remove those associated with functions in discarded link-once
7334 sections, or weakly defined functions that have been overridden. It
7335 would be possible to remove many more entries for statically linked
7336 applications. */
7337
7338 bool
7339 ppc64_elf_edit_opd (struct bfd_link_info *info)
7340 {
7341 bfd *ibfd;
7342 bool some_edited = false;
7343 asection *need_pad = NULL;
7344 struct ppc_link_hash_table *htab;
7345
7346 htab = ppc_hash_table (info);
7347 if (htab == NULL)
7348 return false;
7349
7350 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7351 {
7352 asection *sec;
7353 Elf_Internal_Rela *relstart, *rel, *relend;
7354 Elf_Internal_Shdr *symtab_hdr;
7355 Elf_Internal_Sym *local_syms;
7356 struct _opd_sec_data *opd;
7357 bool need_edit, add_aux_fields, broken;
7358 bfd_size_type cnt_16b = 0;
7359
7360 if (!is_ppc64_elf (ibfd))
7361 continue;
7362
7363 sec = bfd_get_section_by_name (ibfd, ".opd");
7364 if (sec == NULL || sec->size == 0)
7365 continue;
7366
7367 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7368 continue;
7369
7370 if (sec->output_section == bfd_abs_section_ptr)
7371 continue;
7372
7373 /* Look through the section relocs. */
7374 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7375 continue;
7376
7377 local_syms = NULL;
7378 symtab_hdr = &elf_symtab_hdr (ibfd);
7379
7380 /* Read the relocations. */
7381 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7382 info->keep_memory);
7383 if (relstart == NULL)
7384 return false;
7385
7386 /* First run through the relocs to check they are sane, and to
7387 determine whether we need to edit this opd section. */
7388 need_edit = false;
7389 broken = false;
7390 need_pad = sec;
7391 relend = relstart + sec->reloc_count;
7392 for (rel = relstart; rel < relend; )
7393 {
7394 enum elf_ppc64_reloc_type r_type;
7395 unsigned long r_symndx;
7396 asection *sym_sec;
7397 struct elf_link_hash_entry *h;
7398 Elf_Internal_Sym *sym;
7399 bfd_vma offset;
7400
7401 /* .opd contains an array of 16 or 24 byte entries. We're
7402 only interested in the reloc pointing to a function entry
7403 point. */
7404 offset = rel->r_offset;
7405 if (rel + 1 == relend
7406 || rel[1].r_offset != offset + 8)
7407 {
7408 /* If someone messes with .opd alignment then after a
7409 "ld -r" we might have padding in the middle of .opd.
7410 Also, there's nothing to prevent someone putting
7411 something silly in .opd with the assembler. No .opd
7412 optimization for them! */
7413 broken_opd:
7414 _bfd_error_handler
7415 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7416 broken = true;
7417 break;
7418 }
7419
7420 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7421 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7422 {
7423 _bfd_error_handler
7424 /* xgettext:c-format */
7425 (_("%pB: unexpected reloc type %u in .opd section"),
7426 ibfd, r_type);
7427 broken = true;
7428 break;
7429 }
7430
7431 r_symndx = ELF64_R_SYM (rel->r_info);
7432 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7433 r_symndx, ibfd))
7434 goto error_ret;
7435
7436 if (sym_sec == NULL || sym_sec->owner == NULL)
7437 {
7438 const char *sym_name;
7439 if (h != NULL)
7440 sym_name = h->root.root.string;
7441 else
7442 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7443 sym_sec);
7444
7445 _bfd_error_handler
7446 /* xgettext:c-format */
7447 (_("%pB: undefined sym `%s' in .opd section"),
7448 ibfd, sym_name);
7449 broken = true;
7450 break;
7451 }
7452
7453 /* opd entries are always for functions defined in the
7454 current input bfd. If the symbol isn't defined in the
7455 input bfd, then we won't be using the function in this
7456 bfd; It must be defined in a linkonce section in another
7457 bfd, or is weak. It's also possible that we are
7458 discarding the function due to a linker script /DISCARD/,
7459 which we test for via the output_section. */
7460 if (sym_sec->owner != ibfd
7461 || sym_sec->output_section == bfd_abs_section_ptr)
7462 need_edit = true;
7463
7464 rel += 2;
7465 if (rel + 1 == relend
7466 || (rel + 2 < relend
7467 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7468 ++rel;
7469
7470 if (rel == relend)
7471 {
7472 if (sec->size == offset + 24)
7473 {
7474 need_pad = NULL;
7475 break;
7476 }
7477 if (sec->size == offset + 16)
7478 {
7479 cnt_16b++;
7480 break;
7481 }
7482 goto broken_opd;
7483 }
7484 else if (rel + 1 < relend
7485 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7486 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7487 {
7488 if (rel[0].r_offset == offset + 16)
7489 cnt_16b++;
7490 else if (rel[0].r_offset != offset + 24)
7491 goto broken_opd;
7492 }
7493 else
7494 goto broken_opd;
7495 }
7496
7497 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7498
7499 if (!broken && (need_edit || add_aux_fields))
7500 {
7501 Elf_Internal_Rela *write_rel;
7502 Elf_Internal_Shdr *rel_hdr;
7503 bfd_byte *rptr, *wptr;
7504 bfd_byte *new_contents;
7505 bfd_size_type amt;
7506
7507 new_contents = NULL;
7508 amt = OPD_NDX (sec->size) * sizeof (long);
7509 opd = &ppc64_elf_section_data (sec)->u.opd;
7510 opd->adjust = bfd_zalloc (sec->owner, amt);
7511 if (opd->adjust == NULL)
7512 return false;
7513
7514 /* This seems a waste of time as input .opd sections are all
7515 zeros as generated by gcc, but I suppose there's no reason
7516 this will always be so. We might start putting something in
7517 the third word of .opd entries. */
7518 if ((sec->flags & SEC_IN_MEMORY) == 0)
7519 {
7520 bfd_byte *loc;
7521 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7522 {
7523 free (loc);
7524 error_ret:
7525 if (symtab_hdr->contents != (unsigned char *) local_syms)
7526 free (local_syms);
7527 if (elf_section_data (sec)->relocs != relstart)
7528 free (relstart);
7529 return false;
7530 }
7531 sec->contents = loc;
7532 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7533 }
7534
7535 elf_section_data (sec)->relocs = relstart;
7536
7537 new_contents = sec->contents;
7538 if (add_aux_fields)
7539 {
7540 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7541 if (new_contents == NULL)
7542 return false;
7543 need_pad = NULL;
7544 }
7545 wptr = new_contents;
7546 rptr = sec->contents;
7547 write_rel = relstart;
7548 for (rel = relstart; rel < relend; )
7549 {
7550 unsigned long r_symndx;
7551 asection *sym_sec;
7552 struct elf_link_hash_entry *h;
7553 struct ppc_link_hash_entry *fdh = NULL;
7554 Elf_Internal_Sym *sym;
7555 long opd_ent_size;
7556 Elf_Internal_Rela *next_rel;
7557 bool skip;
7558
7559 r_symndx = ELF64_R_SYM (rel->r_info);
7560 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7561 r_symndx, ibfd))
7562 goto error_ret;
7563
7564 next_rel = rel + 2;
7565 if (next_rel + 1 == relend
7566 || (next_rel + 2 < relend
7567 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7568 ++next_rel;
7569
7570 /* See if the .opd entry is full 24 byte or
7571 16 byte (with fd_aux entry overlapped with next
7572 fd_func). */
7573 opd_ent_size = 24;
7574 if (next_rel == relend)
7575 {
7576 if (sec->size == rel->r_offset + 16)
7577 opd_ent_size = 16;
7578 }
7579 else if (next_rel->r_offset == rel->r_offset + 16)
7580 opd_ent_size = 16;
7581
7582 if (h != NULL
7583 && h->root.root.string[0] == '.')
7584 {
7585 fdh = ppc_elf_hash_entry (h)->oh;
7586 if (fdh != NULL)
7587 {
7588 fdh = ppc_follow_link (fdh);
7589 if (fdh->elf.root.type != bfd_link_hash_defined
7590 && fdh->elf.root.type != bfd_link_hash_defweak)
7591 fdh = NULL;
7592 }
7593 }
7594
7595 skip = (sym_sec->owner != ibfd
7596 || sym_sec->output_section == bfd_abs_section_ptr);
7597 if (skip)
7598 {
7599 if (fdh != NULL && sym_sec->owner == ibfd)
7600 {
7601 /* Arrange for the function descriptor sym
7602 to be dropped. */
7603 fdh->elf.root.u.def.value = 0;
7604 fdh->elf.root.u.def.section = sym_sec;
7605 }
7606 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7607
7608 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7609 rel = next_rel;
7610 else
7611 while (1)
7612 {
7613 if (!dec_dynrel_count (rel, sec, info,
7614 NULL, h, sym))
7615 goto error_ret;
7616
7617 if (++rel == next_rel)
7618 break;
7619
7620 r_symndx = ELF64_R_SYM (rel->r_info);
7621 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7622 r_symndx, ibfd))
7623 goto error_ret;
7624 }
7625 }
7626 else
7627 {
7628 /* We'll be keeping this opd entry. */
7629 long adjust;
7630
7631 if (fdh != NULL)
7632 {
7633 /* Redefine the function descriptor symbol to
7634 this location in the opd section. It is
7635 necessary to update the value here rather
7636 than using an array of adjustments as we do
7637 for local symbols, because various places
7638 in the generic ELF code use the value
7639 stored in u.def.value. */
7640 fdh->elf.root.u.def.value = wptr - new_contents;
7641 fdh->adjust_done = 1;
7642 }
7643
7644 /* Local syms are a bit tricky. We could
7645 tweak them as they can be cached, but
7646 we'd need to look through the local syms
7647 for the function descriptor sym which we
7648 don't have at the moment. So keep an
7649 array of adjustments. */
7650 adjust = (wptr - new_contents) - (rptr - sec->contents);
7651 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7652
7653 if (wptr != rptr)
7654 memcpy (wptr, rptr, opd_ent_size);
7655 wptr += opd_ent_size;
7656 if (add_aux_fields && opd_ent_size == 16)
7657 {
7658 memset (wptr, '\0', 8);
7659 wptr += 8;
7660 }
7661
7662 /* We need to adjust any reloc offsets to point to the
7663 new opd entries. */
7664 for ( ; rel != next_rel; ++rel)
7665 {
7666 rel->r_offset += adjust;
7667 if (write_rel != rel)
7668 memcpy (write_rel, rel, sizeof (*rel));
7669 ++write_rel;
7670 }
7671 }
7672
7673 rptr += opd_ent_size;
7674 }
7675
7676 sec->size = wptr - new_contents;
7677 sec->reloc_count = write_rel - relstart;
7678 if (add_aux_fields)
7679 {
7680 free (sec->contents);
7681 sec->contents = new_contents;
7682 }
7683
7684 /* Fudge the header size too, as this is used later in
7685 elf_bfd_final_link if we are emitting relocs. */
7686 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7687 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7688 some_edited = true;
7689 }
7690 else if (elf_section_data (sec)->relocs != relstart)
7691 free (relstart);
7692
7693 if (local_syms != NULL
7694 && symtab_hdr->contents != (unsigned char *) local_syms)
7695 {
7696 if (!info->keep_memory)
7697 free (local_syms);
7698 else
7699 symtab_hdr->contents = (unsigned char *) local_syms;
7700 }
7701 }
7702
7703 if (some_edited)
7704 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7705
7706 /* If we are doing a final link and the last .opd entry is just 16 byte
7707 long, add a 8 byte padding after it. */
7708 if (need_pad != NULL && !bfd_link_relocatable (info))
7709 {
7710 bfd_byte *p;
7711
7712 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7713 {
7714 BFD_ASSERT (need_pad->size > 0);
7715
7716 p = bfd_malloc (need_pad->size + 8);
7717 if (p == NULL)
7718 return false;
7719
7720 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7721 p, 0, need_pad->size))
7722 return false;
7723
7724 need_pad->contents = p;
7725 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7726 }
7727 else
7728 {
7729 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7730 if (p == NULL)
7731 return false;
7732
7733 need_pad->contents = p;
7734 }
7735
7736 memset (need_pad->contents + need_pad->size, 0, 8);
7737 need_pad->size += 8;
7738 }
7739
7740 return true;
7741 }
7742
7743 /* Analyze inline PLT call relocations to see whether calls to locally
7744 defined functions can be converted to direct calls. */
7745
7746 bool
7747 ppc64_elf_inline_plt (struct bfd_link_info *info)
7748 {
7749 struct ppc_link_hash_table *htab;
7750 bfd *ibfd;
7751 asection *sec;
7752 bfd_vma low_vma, high_vma, limit;
7753
7754 htab = ppc_hash_table (info);
7755 if (htab == NULL)
7756 return false;
7757
7758 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7759 reduced somewhat to cater for possible stubs that might be added
7760 between the call and its destination. */
7761 if (htab->params->group_size < 0)
7762 {
7763 limit = -htab->params->group_size;
7764 if (limit == 1)
7765 limit = 0x1e00000;
7766 }
7767 else
7768 {
7769 limit = htab->params->group_size;
7770 if (limit == 1)
7771 limit = 0x1c00000;
7772 }
7773
7774 low_vma = -1;
7775 high_vma = 0;
7776 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7777 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7778 {
7779 if (low_vma > sec->vma)
7780 low_vma = sec->vma;
7781 if (high_vma < sec->vma + sec->size)
7782 high_vma = sec->vma + sec->size;
7783 }
7784
7785 /* If a "bl" can reach anywhere in local code sections, then we can
7786 convert all inline PLT sequences to direct calls when the symbol
7787 is local. */
7788 if (high_vma - low_vma < limit)
7789 {
7790 htab->can_convert_all_inline_plt = 1;
7791 return true;
7792 }
7793
7794 /* Otherwise, go looking through relocs for cases where a direct
7795 call won't reach. Mark the symbol on any such reloc to disable
7796 the optimization and keep the PLT entry as it seems likely that
7797 this will be better than creating trampolines. Note that this
7798 will disable the optimization for all inline PLT calls to a
7799 particular symbol, not just those that won't reach. The
7800 difficulty in doing a more precise optimization is that the
7801 linker needs to make a decision depending on whether a
7802 particular R_PPC64_PLTCALL insn can be turned into a direct
7803 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7804 the sequence, and there is nothing that ties those relocs
7805 together except their symbol. */
7806
7807 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7808 {
7809 Elf_Internal_Shdr *symtab_hdr;
7810 Elf_Internal_Sym *local_syms;
7811
7812 if (!is_ppc64_elf (ibfd))
7813 continue;
7814
7815 local_syms = NULL;
7816 symtab_hdr = &elf_symtab_hdr (ibfd);
7817
7818 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7819 if (ppc64_elf_section_data (sec)->has_pltcall
7820 && !bfd_is_abs_section (sec->output_section))
7821 {
7822 Elf_Internal_Rela *relstart, *rel, *relend;
7823
7824 /* Read the relocations. */
7825 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7826 info->keep_memory);
7827 if (relstart == NULL)
7828 return false;
7829
7830 relend = relstart + sec->reloc_count;
7831 for (rel = relstart; rel < relend; rel++)
7832 {
7833 enum elf_ppc64_reloc_type r_type;
7834 unsigned long r_symndx;
7835 asection *sym_sec;
7836 struct elf_link_hash_entry *h;
7837 Elf_Internal_Sym *sym;
7838 unsigned char *tls_maskp;
7839
7840 r_type = ELF64_R_TYPE (rel->r_info);
7841 if (r_type != R_PPC64_PLTCALL
7842 && r_type != R_PPC64_PLTCALL_NOTOC)
7843 continue;
7844
7845 r_symndx = ELF64_R_SYM (rel->r_info);
7846 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7847 r_symndx, ibfd))
7848 {
7849 if (elf_section_data (sec)->relocs != relstart)
7850 free (relstart);
7851 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7852 free (local_syms);
7853 return false;
7854 }
7855
7856 if (sym_sec != NULL && sym_sec->output_section != NULL)
7857 {
7858 bfd_vma from, to;
7859 if (h != NULL)
7860 to = h->root.u.def.value;
7861 else
7862 to = sym->st_value;
7863 to += (rel->r_addend
7864 + sym_sec->output_offset
7865 + sym_sec->output_section->vma);
7866 from = (rel->r_offset
7867 + sec->output_offset
7868 + sec->output_section->vma);
7869 if (to - from + limit < 2 * limit
7870 && !(r_type == R_PPC64_PLTCALL_NOTOC
7871 && (((h ? h->other : sym->st_other)
7872 & STO_PPC64_LOCAL_MASK)
7873 > 1 << STO_PPC64_LOCAL_BIT)))
7874 *tls_maskp &= ~PLT_KEEP;
7875 }
7876 }
7877 if (elf_section_data (sec)->relocs != relstart)
7878 free (relstart);
7879 }
7880
7881 if (local_syms != NULL
7882 && symtab_hdr->contents != (unsigned char *) local_syms)
7883 {
7884 if (!info->keep_memory)
7885 free (local_syms);
7886 else
7887 symtab_hdr->contents = (unsigned char *) local_syms;
7888 }
7889 }
7890
7891 return true;
7892 }
7893
7894 /* Set htab->tls_get_addr and various other info specific to TLS.
7895 This needs to run before dynamic symbols are processed in
7896 bfd_elf_size_dynamic_sections. */
7897
7898 bool
7899 ppc64_elf_tls_setup (struct bfd_link_info *info)
7900 {
7901 struct ppc_link_hash_table *htab;
7902 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7903
7904 htab = ppc_hash_table (info);
7905 if (htab == NULL)
7906 return false;
7907
7908 /* Move dynamic linking info to the function descriptor sym. */
7909 if (htab->need_func_desc_adj)
7910 {
7911 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7912 htab->need_func_desc_adj = 0;
7913 }
7914
7915 if (abiversion (info->output_bfd) == 1)
7916 htab->opd_abi = 1;
7917
7918 if (htab->params->no_multi_toc)
7919 htab->do_multi_toc = 0;
7920 else if (!htab->do_multi_toc)
7921 htab->params->no_multi_toc = 1;
7922
7923 /* Default to --no-plt-localentry, as this option can cause problems
7924 with symbol interposition. For example, glibc libpthread.so and
7925 libc.so duplicate many pthread symbols, with a fallback
7926 implementation in libc.so. In some cases the fallback does more
7927 work than the pthread implementation. __pthread_condattr_destroy
7928 is one such symbol: the libpthread.so implementation is
7929 localentry:0 while the libc.so implementation is localentry:8.
7930 An app that "cleverly" uses dlopen to only load necessary
7931 libraries at runtime may omit loading libpthread.so when not
7932 running multi-threaded, which then results in the libc.so
7933 fallback symbols being used and ld.so complaining. Now there
7934 are workarounds in ld (see non_zero_localentry) to detect the
7935 pthread situation, but that may not be the only case where
7936 --plt-localentry can cause trouble. */
7937 if (htab->params->plt_localentry0 < 0)
7938 htab->params->plt_localentry0 = 0;
7939 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7940 {
7941 /* The issue is that __glink_PLTresolve saves r2, which is done
7942 because glibc ld.so _dl_runtime_resolve restores r2 to support
7943 a glibc plt call optimisation where global entry code is
7944 skipped on calls that resolve to the same binary. The
7945 __glink_PLTresolve save of r2 is incompatible with code
7946 making tail calls, because the tail call might go via the
7947 resolver and thus overwrite the proper saved r2. */
7948 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7949 "power10 pc-relative code"));
7950 htab->params->plt_localentry0 = 0;
7951 }
7952 if (htab->params->plt_localentry0
7953 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7954 false, false, false) == NULL)
7955 _bfd_error_handler
7956 (_("warning: --plt-localentry is especially dangerous without "
7957 "ld.so support to detect ABI violations"));
7958
7959 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7960 false, false, true);
7961 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7962 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7963 false, false, true);
7964 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7965
7966 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7967 false, false, true);
7968 htab->tga_desc = ppc_elf_hash_entry (desc);
7969 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7970 false, false, true);
7971 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7972
7973 if (htab->params->tls_get_addr_opt)
7974 {
7975 struct elf_link_hash_entry *opt, *opt_fd;
7976
7977 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7978 false, false, true);
7979 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7980 false, false, true);
7981 if (opt_fd != NULL
7982 && (opt_fd->root.type == bfd_link_hash_defined
7983 || opt_fd->root.type == bfd_link_hash_defweak))
7984 {
7985 /* If glibc supports an optimized __tls_get_addr call stub,
7986 signalled by the presence of __tls_get_addr_opt, and we'll
7987 be calling __tls_get_addr via a plt call stub, then
7988 make __tls_get_addr point to __tls_get_addr_opt. */
7989 if (!(htab->elf.dynamic_sections_created
7990 && tga_fd != NULL
7991 && (tga_fd->type == STT_FUNC
7992 || tga_fd->needs_plt)
7993 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7994 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7995 tga_fd = NULL;
7996 if (!(htab->elf.dynamic_sections_created
7997 && desc_fd != NULL
7998 && (desc_fd->type == STT_FUNC
7999 || desc_fd->needs_plt)
8000 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8001 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8002 desc_fd = NULL;
8003
8004 if (tga_fd != NULL || desc_fd != NULL)
8005 {
8006 struct plt_entry *ent = NULL;
8007
8008 if (tga_fd != NULL)
8009 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8010 if (ent->plt.refcount > 0)
8011 break;
8012 if (ent == NULL && desc_fd != NULL)
8013 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8014 if (ent->plt.refcount > 0)
8015 break;
8016 if (ent != NULL)
8017 {
8018 if (tga_fd != NULL)
8019 {
8020 tga_fd->root.type = bfd_link_hash_indirect;
8021 tga_fd->root.u.i.link = &opt_fd->root;
8022 tga_fd->root.u.i.warning = NULL;
8023 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8024 }
8025 if (desc_fd != NULL)
8026 {
8027 desc_fd->root.type = bfd_link_hash_indirect;
8028 desc_fd->root.u.i.link = &opt_fd->root;
8029 desc_fd->root.u.i.warning = NULL;
8030 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8031 }
8032 opt_fd->mark = 1;
8033 if (opt_fd->dynindx != -1)
8034 {
8035 /* Use __tls_get_addr_opt in dynamic relocations. */
8036 opt_fd->dynindx = -1;
8037 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8038 opt_fd->dynstr_index);
8039 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8040 return false;
8041 }
8042 if (tga_fd != NULL)
8043 {
8044 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8045 tga = elf_hash_entry (htab->tls_get_addr);
8046 if (opt != NULL && tga != NULL)
8047 {
8048 tga->root.type = bfd_link_hash_indirect;
8049 tga->root.u.i.link = &opt->root;
8050 tga->root.u.i.warning = NULL;
8051 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8052 opt->mark = 1;
8053 _bfd_elf_link_hash_hide_symbol (info, opt,
8054 tga->forced_local);
8055 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8056 }
8057 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8058 htab->tls_get_addr_fd->is_func_descriptor = 1;
8059 if (htab->tls_get_addr != NULL)
8060 {
8061 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8062 htab->tls_get_addr->is_func = 1;
8063 }
8064 }
8065 if (desc_fd != NULL)
8066 {
8067 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8068 if (opt != NULL && desc != NULL)
8069 {
8070 desc->root.type = bfd_link_hash_indirect;
8071 desc->root.u.i.link = &opt->root;
8072 desc->root.u.i.warning = NULL;
8073 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8074 opt->mark = 1;
8075 _bfd_elf_link_hash_hide_symbol (info, opt,
8076 desc->forced_local);
8077 htab->tga_desc = ppc_elf_hash_entry (opt);
8078 }
8079 htab->tga_desc_fd->oh = htab->tga_desc;
8080 htab->tga_desc_fd->is_func_descriptor = 1;
8081 if (htab->tga_desc != NULL)
8082 {
8083 htab->tga_desc->oh = htab->tga_desc_fd;
8084 htab->tga_desc->is_func = 1;
8085 }
8086 }
8087 }
8088 }
8089 }
8090 else if (htab->params->tls_get_addr_opt < 0)
8091 htab->params->tls_get_addr_opt = 0;
8092 }
8093
8094 if (htab->tga_desc_fd != NULL
8095 && htab->params->tls_get_addr_opt
8096 && htab->params->no_tls_get_addr_regsave == -1)
8097 htab->params->no_tls_get_addr_regsave = 0;
8098
8099 return true;
8100 }
8101
8102 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8103 any of HASH1, HASH2, HASH3, or HASH4. */
8104
8105 static bool
8106 branch_reloc_hash_match (bfd *ibfd,
8107 Elf_Internal_Rela *rel,
8108 struct ppc_link_hash_entry *hash1,
8109 struct ppc_link_hash_entry *hash2,
8110 struct ppc_link_hash_entry *hash3,
8111 struct ppc_link_hash_entry *hash4)
8112 {
8113 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8114 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8115 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8116
8117 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8118 {
8119 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8120 struct elf_link_hash_entry *h;
8121
8122 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8123 h = elf_follow_link (h);
8124 if (h == elf_hash_entry (hash1)
8125 || h == elf_hash_entry (hash2)
8126 || h == elf_hash_entry (hash3)
8127 || h == elf_hash_entry (hash4))
8128 return true;
8129 }
8130 return false;
8131 }
8132
8133 /* Run through all the TLS relocs looking for optimization
8134 opportunities. The linker has been hacked (see ppc64elf.em) to do
8135 a preliminary section layout so that we know the TLS segment
8136 offsets. We can't optimize earlier because some optimizations need
8137 to know the tp offset, and we need to optimize before allocating
8138 dynamic relocations. */
8139
8140 bool
8141 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8142 {
8143 bfd *ibfd;
8144 asection *sec;
8145 struct ppc_link_hash_table *htab;
8146 unsigned char *toc_ref;
8147 int pass;
8148
8149 if (!bfd_link_executable (info))
8150 return true;
8151
8152 htab = ppc_hash_table (info);
8153 if (htab == NULL)
8154 return false;
8155
8156 htab->do_tls_opt = 1;
8157
8158 /* Make two passes over the relocs. On the first pass, mark toc
8159 entries involved with tls relocs, and check that tls relocs
8160 involved in setting up a tls_get_addr call are indeed followed by
8161 such a call. If they are not, we can't do any tls optimization.
8162 On the second pass twiddle tls_mask flags to notify
8163 relocate_section that optimization can be done, and adjust got
8164 and plt refcounts. */
8165 toc_ref = NULL;
8166 for (pass = 0; pass < 2; ++pass)
8167 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8168 {
8169 Elf_Internal_Sym *locsyms = NULL;
8170 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8171
8172 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8173 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8174 {
8175 Elf_Internal_Rela *relstart, *rel, *relend;
8176 bool found_tls_get_addr_arg = 0;
8177
8178 /* Read the relocations. */
8179 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8180 info->keep_memory);
8181 if (relstart == NULL)
8182 {
8183 free (toc_ref);
8184 return false;
8185 }
8186
8187 relend = relstart + sec->reloc_count;
8188 for (rel = relstart; rel < relend; rel++)
8189 {
8190 enum elf_ppc64_reloc_type r_type;
8191 unsigned long r_symndx;
8192 struct elf_link_hash_entry *h;
8193 Elf_Internal_Sym *sym;
8194 asection *sym_sec;
8195 unsigned char *tls_mask;
8196 unsigned int tls_set, tls_clear, tls_type = 0;
8197 bfd_vma value;
8198 bool ok_tprel, is_local;
8199 long toc_ref_index = 0;
8200 int expecting_tls_get_addr = 0;
8201 bool ret = false;
8202
8203 r_symndx = ELF64_R_SYM (rel->r_info);
8204 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8205 r_symndx, ibfd))
8206 {
8207 err_free_rel:
8208 if (elf_section_data (sec)->relocs != relstart)
8209 free (relstart);
8210 free (toc_ref);
8211 if (elf_symtab_hdr (ibfd).contents
8212 != (unsigned char *) locsyms)
8213 free (locsyms);
8214 return ret;
8215 }
8216
8217 if (h != NULL)
8218 {
8219 if (h->root.type == bfd_link_hash_defined
8220 || h->root.type == bfd_link_hash_defweak)
8221 value = h->root.u.def.value;
8222 else if (h->root.type == bfd_link_hash_undefweak)
8223 value = 0;
8224 else
8225 {
8226 found_tls_get_addr_arg = 0;
8227 continue;
8228 }
8229 }
8230 else
8231 /* Symbols referenced by TLS relocs must be of type
8232 STT_TLS. So no need for .opd local sym adjust. */
8233 value = sym->st_value;
8234
8235 ok_tprel = false;
8236 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8237 if (is_local)
8238 {
8239 if (h != NULL
8240 && h->root.type == bfd_link_hash_undefweak)
8241 ok_tprel = true;
8242 else if (sym_sec != NULL
8243 && sym_sec->output_section != NULL)
8244 {
8245 value += sym_sec->output_offset;
8246 value += sym_sec->output_section->vma;
8247 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8248 /* Note that even though the prefix insns
8249 allow a 1<<33 offset we use the same test
8250 as for addis;addi. There may be a mix of
8251 pcrel and non-pcrel code and the decision
8252 to optimise is per symbol, not per TLS
8253 sequence. */
8254 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8255 }
8256 }
8257
8258 r_type = ELF64_R_TYPE (rel->r_info);
8259 /* If this section has old-style __tls_get_addr calls
8260 without marker relocs, then check that each
8261 __tls_get_addr call reloc is preceded by a reloc
8262 that conceivably belongs to the __tls_get_addr arg
8263 setup insn. If we don't find matching arg setup
8264 relocs, don't do any tls optimization. */
8265 if (pass == 0
8266 && sec->nomark_tls_get_addr
8267 && h != NULL
8268 && is_tls_get_addr (h, htab)
8269 && !found_tls_get_addr_arg
8270 && is_branch_reloc (r_type))
8271 {
8272 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8273 "TLS optimization disabled\n"),
8274 ibfd, sec, rel->r_offset);
8275 ret = true;
8276 goto err_free_rel;
8277 }
8278
8279 found_tls_get_addr_arg = 0;
8280 switch (r_type)
8281 {
8282 case R_PPC64_GOT_TLSLD16:
8283 case R_PPC64_GOT_TLSLD16_LO:
8284 case R_PPC64_GOT_TLSLD_PCREL34:
8285 expecting_tls_get_addr = 1;
8286 found_tls_get_addr_arg = 1;
8287 /* Fall through. */
8288
8289 case R_PPC64_GOT_TLSLD16_HI:
8290 case R_PPC64_GOT_TLSLD16_HA:
8291 /* These relocs should never be against a symbol
8292 defined in a shared lib. Leave them alone if
8293 that turns out to be the case. */
8294 if (!is_local)
8295 continue;
8296
8297 /* LD -> LE */
8298 tls_set = 0;
8299 tls_clear = TLS_LD;
8300 tls_type = TLS_TLS | TLS_LD;
8301 break;
8302
8303 case R_PPC64_GOT_TLSGD16:
8304 case R_PPC64_GOT_TLSGD16_LO:
8305 case R_PPC64_GOT_TLSGD_PCREL34:
8306 expecting_tls_get_addr = 1;
8307 found_tls_get_addr_arg = 1;
8308 /* Fall through. */
8309
8310 case R_PPC64_GOT_TLSGD16_HI:
8311 case R_PPC64_GOT_TLSGD16_HA:
8312 if (ok_tprel)
8313 /* GD -> LE */
8314 tls_set = 0;
8315 else
8316 /* GD -> IE */
8317 tls_set = TLS_TLS | TLS_GDIE;
8318 tls_clear = TLS_GD;
8319 tls_type = TLS_TLS | TLS_GD;
8320 break;
8321
8322 case R_PPC64_GOT_TPREL_PCREL34:
8323 case R_PPC64_GOT_TPREL16_DS:
8324 case R_PPC64_GOT_TPREL16_LO_DS:
8325 case R_PPC64_GOT_TPREL16_HI:
8326 case R_PPC64_GOT_TPREL16_HA:
8327 if (ok_tprel)
8328 {
8329 /* IE -> LE */
8330 tls_set = 0;
8331 tls_clear = TLS_TPREL;
8332 tls_type = TLS_TLS | TLS_TPREL;
8333 break;
8334 }
8335 continue;
8336
8337 case R_PPC64_TLSLD:
8338 if (!is_local)
8339 continue;
8340 /* Fall through. */
8341 case R_PPC64_TLSGD:
8342 if (rel + 1 < relend
8343 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8344 {
8345 if (pass != 0
8346 && (ELF64_R_TYPE (rel[1].r_info)
8347 != R_PPC64_PLTSEQ)
8348 && (ELF64_R_TYPE (rel[1].r_info)
8349 != R_PPC64_PLTSEQ_NOTOC))
8350 {
8351 r_symndx = ELF64_R_SYM (rel[1].r_info);
8352 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8353 r_symndx, ibfd))
8354 goto err_free_rel;
8355 if (h != NULL)
8356 {
8357 struct plt_entry *ent = NULL;
8358
8359 for (ent = h->plt.plist;
8360 ent != NULL;
8361 ent = ent->next)
8362 if (ent->addend == rel[1].r_addend)
8363 break;
8364
8365 if (ent != NULL
8366 && ent->plt.refcount > 0)
8367 ent->plt.refcount -= 1;
8368 }
8369 }
8370 continue;
8371 }
8372 found_tls_get_addr_arg = 1;
8373 /* Fall through. */
8374
8375 case R_PPC64_TLS:
8376 case R_PPC64_TOC16:
8377 case R_PPC64_TOC16_LO:
8378 if (sym_sec == NULL || sym_sec != toc)
8379 continue;
8380
8381 /* Mark this toc entry as referenced by a TLS
8382 code sequence. We can do that now in the
8383 case of R_PPC64_TLS, and after checking for
8384 tls_get_addr for the TOC16 relocs. */
8385 if (toc_ref == NULL)
8386 toc_ref
8387 = bfd_zmalloc (toc->output_section->rawsize / 8);
8388 if (toc_ref == NULL)
8389 goto err_free_rel;
8390
8391 if (h != NULL)
8392 value = h->root.u.def.value;
8393 else
8394 value = sym->st_value;
8395 value += rel->r_addend;
8396 if (value % 8 != 0)
8397 continue;
8398 BFD_ASSERT (value < toc->size
8399 && toc->output_offset % 8 == 0);
8400 toc_ref_index = (value + toc->output_offset) / 8;
8401 if (r_type == R_PPC64_TLS
8402 || r_type == R_PPC64_TLSGD
8403 || r_type == R_PPC64_TLSLD)
8404 {
8405 toc_ref[toc_ref_index] = 1;
8406 continue;
8407 }
8408
8409 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8410 continue;
8411
8412 tls_set = 0;
8413 tls_clear = 0;
8414 expecting_tls_get_addr = 2;
8415 break;
8416
8417 case R_PPC64_TPREL64:
8418 if (pass == 0
8419 || sec != toc
8420 || toc_ref == NULL
8421 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8422 continue;
8423 if (ok_tprel)
8424 {
8425 /* IE -> LE */
8426 tls_set = TLS_EXPLICIT;
8427 tls_clear = TLS_TPREL;
8428 break;
8429 }
8430 continue;
8431
8432 case R_PPC64_DTPMOD64:
8433 if (pass == 0
8434 || sec != toc
8435 || toc_ref == NULL
8436 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8437 continue;
8438 if (rel + 1 < relend
8439 && (rel[1].r_info
8440 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8441 && rel[1].r_offset == rel->r_offset + 8)
8442 {
8443 if (ok_tprel)
8444 /* GD -> LE */
8445 tls_set = TLS_EXPLICIT | TLS_GD;
8446 else
8447 /* GD -> IE */
8448 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8449 tls_clear = TLS_GD;
8450 }
8451 else
8452 {
8453 if (!is_local)
8454 continue;
8455
8456 /* LD -> LE */
8457 tls_set = TLS_EXPLICIT;
8458 tls_clear = TLS_LD;
8459 }
8460 break;
8461
8462 case R_PPC64_TPREL16_HA:
8463 if (pass == 0)
8464 {
8465 unsigned char buf[4];
8466 unsigned int insn;
8467 bfd_vma off = rel->r_offset & ~3;
8468 if (!bfd_get_section_contents (ibfd, sec, buf,
8469 off, 4))
8470 goto err_free_rel;
8471 insn = bfd_get_32 (ibfd, buf);
8472 /* addis rt,13,imm */
8473 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8474 != ((15u << 26) | (13 << 16)))
8475 {
8476 /* xgettext:c-format */
8477 info->callbacks->minfo
8478 (_("%H: warning: %s unexpected insn %#x.\n"),
8479 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8480 htab->do_tls_opt = 0;
8481 }
8482 }
8483 continue;
8484
8485 case R_PPC64_TPREL16_HI:
8486 case R_PPC64_TPREL16_HIGH:
8487 case R_PPC64_TPREL16_HIGHA:
8488 case R_PPC64_TPREL16_HIGHER:
8489 case R_PPC64_TPREL16_HIGHERA:
8490 case R_PPC64_TPREL16_HIGHEST:
8491 case R_PPC64_TPREL16_HIGHESTA:
8492 /* These can all be used in sequences along with
8493 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8494 able to verify easily. */
8495 htab->do_tls_opt = 0;
8496 continue;
8497
8498 default:
8499 continue;
8500 }
8501
8502 if (pass == 0)
8503 {
8504 if (!expecting_tls_get_addr
8505 || !sec->nomark_tls_get_addr)
8506 continue;
8507
8508 if (rel + 1 < relend
8509 && branch_reloc_hash_match (ibfd, rel + 1,
8510 htab->tls_get_addr_fd,
8511 htab->tga_desc_fd,
8512 htab->tls_get_addr,
8513 htab->tga_desc))
8514 {
8515 if (expecting_tls_get_addr == 2)
8516 {
8517 /* Check for toc tls entries. */
8518 unsigned char *toc_tls;
8519 int retval;
8520
8521 retval = get_tls_mask (&toc_tls, NULL, NULL,
8522 &locsyms,
8523 rel, ibfd);
8524 if (retval == 0)
8525 goto err_free_rel;
8526 if (toc_tls != NULL)
8527 {
8528 if ((*toc_tls & TLS_TLS) != 0
8529 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8530 found_tls_get_addr_arg = 1;
8531 if (retval > 1)
8532 toc_ref[toc_ref_index] = 1;
8533 }
8534 }
8535 continue;
8536 }
8537
8538 /* Uh oh, we didn't find the expected call. We
8539 could just mark this symbol to exclude it
8540 from tls optimization but it's safer to skip
8541 the entire optimization. */
8542 /* xgettext:c-format */
8543 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8544 "TLS optimization disabled\n"),
8545 ibfd, sec, rel->r_offset);
8546 ret = true;
8547 goto err_free_rel;
8548 }
8549
8550 /* If we don't have old-style __tls_get_addr calls
8551 without TLSGD/TLSLD marker relocs, and we haven't
8552 found a new-style __tls_get_addr call with a
8553 marker for this symbol, then we either have a
8554 broken object file or an -mlongcall style
8555 indirect call to __tls_get_addr without a marker.
8556 Disable optimization in this case. */
8557 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8558 && (tls_set & TLS_EXPLICIT) == 0
8559 && !sec->nomark_tls_get_addr
8560 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8561 != (TLS_TLS | TLS_MARK)))
8562 continue;
8563
8564 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8565 {
8566 struct plt_entry *ent = NULL;
8567
8568 if (htab->tls_get_addr_fd != NULL)
8569 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8570 ent != NULL;
8571 ent = ent->next)
8572 if (ent->addend == 0)
8573 break;
8574
8575 if (ent == NULL && htab->tga_desc_fd != NULL)
8576 for (ent = htab->tga_desc_fd->elf.plt.plist;
8577 ent != NULL;
8578 ent = ent->next)
8579 if (ent->addend == 0)
8580 break;
8581
8582 if (ent == NULL && htab->tls_get_addr != NULL)
8583 for (ent = htab->tls_get_addr->elf.plt.plist;
8584 ent != NULL;
8585 ent = ent->next)
8586 if (ent->addend == 0)
8587 break;
8588
8589 if (ent == NULL && htab->tga_desc != NULL)
8590 for (ent = htab->tga_desc->elf.plt.plist;
8591 ent != NULL;
8592 ent = ent->next)
8593 if (ent->addend == 0)
8594 break;
8595
8596 if (ent != NULL
8597 && ent->plt.refcount > 0)
8598 ent->plt.refcount -= 1;
8599 }
8600
8601 if (tls_clear == 0)
8602 continue;
8603
8604 if ((tls_set & TLS_EXPLICIT) == 0)
8605 {
8606 struct got_entry *ent;
8607
8608 /* Adjust got entry for this reloc. */
8609 if (h != NULL)
8610 ent = h->got.glist;
8611 else
8612 ent = elf_local_got_ents (ibfd)[r_symndx];
8613
8614 for (; ent != NULL; ent = ent->next)
8615 if (ent->addend == rel->r_addend
8616 && ent->owner == ibfd
8617 && ent->tls_type == tls_type)
8618 break;
8619 if (ent == NULL)
8620 abort ();
8621
8622 if (tls_set == 0)
8623 {
8624 /* We managed to get rid of a got entry. */
8625 if (ent->got.refcount > 0)
8626 ent->got.refcount -= 1;
8627 }
8628 }
8629 else
8630 {
8631 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8632 we'll lose one or two dyn relocs. */
8633 if (!dec_dynrel_count (rel, sec, info,
8634 NULL, h, sym))
8635 return false;
8636
8637 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8638 {
8639 if (!dec_dynrel_count (rel + 1, sec, info,
8640 NULL, h, sym))
8641 return false;
8642 }
8643 }
8644
8645 *tls_mask |= tls_set & 0xff;
8646 *tls_mask &= ~tls_clear;
8647 }
8648
8649 if (elf_section_data (sec)->relocs != relstart)
8650 free (relstart);
8651 }
8652
8653 if (locsyms != NULL
8654 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8655 {
8656 if (!info->keep_memory)
8657 free (locsyms);
8658 else
8659 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8660 }
8661 }
8662
8663 free (toc_ref);
8664 return true;
8665 }
8666
8667 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8668 the values of any global symbols in a toc section that has been
8669 edited. Globals in toc sections should be a rarity, so this function
8670 sets a flag if any are found in toc sections other than the one just
8671 edited, so that further hash table traversals can be avoided. */
8672
8673 struct adjust_toc_info
8674 {
8675 asection *toc;
8676 unsigned long *skip;
8677 bool global_toc_syms;
8678 };
8679
8680 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8681
8682 static bool
8683 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8684 {
8685 struct ppc_link_hash_entry *eh;
8686 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8687 unsigned long i;
8688
8689 if (h->root.type != bfd_link_hash_defined
8690 && h->root.type != bfd_link_hash_defweak)
8691 return true;
8692
8693 eh = ppc_elf_hash_entry (h);
8694 if (eh->adjust_done)
8695 return true;
8696
8697 if (eh->elf.root.u.def.section == toc_inf->toc)
8698 {
8699 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8700 i = toc_inf->toc->rawsize >> 3;
8701 else
8702 i = eh->elf.root.u.def.value >> 3;
8703
8704 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8705 {
8706 _bfd_error_handler
8707 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8708 do
8709 ++i;
8710 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8711 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8712 }
8713
8714 eh->elf.root.u.def.value -= toc_inf->skip[i];
8715 eh->adjust_done = 1;
8716 }
8717 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8718 toc_inf->global_toc_syms = true;
8719
8720 return true;
8721 }
8722
8723 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8724 on a _LO variety toc/got reloc. */
8725
8726 static bool
8727 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8728 {
8729 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8730 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8731 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8732 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8733 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8734 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8735 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8736 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8737 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8738 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8739 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8740 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8741 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8742 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8743 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8744 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8745 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8746 /* Exclude lfqu by testing reloc. If relocs are ever
8747 defined for the reduced D field in psq_lu then those
8748 will need testing too. */
8749 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8750 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8751 && (insn & 1) == 0)
8752 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8753 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8754 /* Exclude stfqu. psq_stu as above for psq_lu. */
8755 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8756 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8757 && (insn & 1) == 0));
8758 }
8759
8760 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8761 pld ra,symbol@got@pcrel
8762 load/store rt,off(ra)
8763 or
8764 pla ra,symbol@pcrel
8765 load/store rt,off(ra)
8766 may be translated to
8767 pload/pstore rt,symbol+off@pcrel
8768 nop.
8769 This function returns true if the optimization is possible, placing
8770 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8771
8772 On entry to this function, the linker has already determined that
8773 the pld can be replaced with pla: *PINSN1 is that pla insn,
8774 while *PINSN2 is the second instruction. */
8775
8776 static bool
8777 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8778 {
8779 uint64_t insn1 = *pinsn1;
8780 uint64_t insn2 = *pinsn2;
8781 bfd_signed_vma off;
8782
8783 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8784 {
8785 /* Check that regs match. */
8786 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8787 return false;
8788
8789 /* P8LS or PMLS form, non-pcrel. */
8790 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8791 return false;
8792
8793 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8794 *pinsn2 = PNOP;
8795 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8796 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8797 return true;
8798 }
8799
8800 insn2 >>= 32;
8801
8802 /* Check that regs match. */
8803 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8804 return false;
8805
8806 switch ((insn2 >> 26) & 63)
8807 {
8808 default:
8809 return false;
8810
8811 case 32: /* lwz */
8812 case 34: /* lbz */
8813 case 36: /* stw */
8814 case 38: /* stb */
8815 case 40: /* lhz */
8816 case 42: /* lha */
8817 case 44: /* sth */
8818 case 48: /* lfs */
8819 case 50: /* lfd */
8820 case 52: /* stfs */
8821 case 54: /* stfd */
8822 /* These are the PMLS cases, where we just need to tack a prefix
8823 on the insn. */
8824 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8825 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8826 off = insn2 & 0xffff;
8827 break;
8828
8829 case 58: /* lwa, ld */
8830 if ((insn2 & 1) != 0)
8831 return false;
8832 insn1 = ((1ULL << 58) | (1ULL << 52)
8833 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8834 | (insn2 & (31ULL << 21)));
8835 off = insn2 & 0xfffc;
8836 break;
8837
8838 case 57: /* lxsd, lxssp */
8839 if ((insn2 & 3) < 2)
8840 return false;
8841 insn1 = ((1ULL << 58) | (1ULL << 52)
8842 | ((40ULL | (insn2 & 3)) << 26)
8843 | (insn2 & (31ULL << 21)));
8844 off = insn2 & 0xfffc;
8845 break;
8846
8847 case 61: /* stxsd, stxssp, lxv, stxv */
8848 if ((insn2 & 3) == 0)
8849 return false;
8850 else if ((insn2 & 3) >= 2)
8851 {
8852 insn1 = ((1ULL << 58) | (1ULL << 52)
8853 | ((44ULL | (insn2 & 3)) << 26)
8854 | (insn2 & (31ULL << 21)));
8855 off = insn2 & 0xfffc;
8856 }
8857 else
8858 {
8859 insn1 = ((1ULL << 58) | (1ULL << 52)
8860 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8861 | (insn2 & (31ULL << 21)));
8862 off = insn2 & 0xfff0;
8863 }
8864 break;
8865
8866 case 56: /* lq */
8867 insn1 = ((1ULL << 58) | (1ULL << 52)
8868 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8869 off = insn2 & 0xffff;
8870 break;
8871
8872 case 6: /* lxvp, stxvp */
8873 if ((insn2 & 0xe) != 0)
8874 return false;
8875 insn1 = ((1ULL << 58) | (1ULL << 52)
8876 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8877 | (insn2 & (31ULL << 21)));
8878 off = insn2 & 0xfff0;
8879 break;
8880
8881 case 62: /* std, stq */
8882 if ((insn2 & 1) != 0)
8883 return false;
8884 insn1 = ((1ULL << 58) | (1ULL << 52)
8885 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8886 | (insn2 & (31ULL << 21)));
8887 off = insn2 & 0xfffc;
8888 break;
8889 }
8890
8891 *pinsn1 = insn1;
8892 *pinsn2 = (uint64_t) NOP << 32;
8893 *poff = (off ^ 0x8000) - 0x8000;
8894 return true;
8895 }
8896
8897 /* Examine all relocs referencing .toc sections in order to remove
8898 unused .toc entries. */
8899
8900 bool
8901 ppc64_elf_edit_toc (struct bfd_link_info *info)
8902 {
8903 bfd *ibfd;
8904 struct adjust_toc_info toc_inf;
8905 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8906
8907 htab->do_toc_opt = 1;
8908 toc_inf.global_toc_syms = true;
8909 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8910 {
8911 asection *toc, *sec;
8912 Elf_Internal_Shdr *symtab_hdr;
8913 Elf_Internal_Sym *local_syms;
8914 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8915 unsigned long *skip, *drop;
8916 unsigned char *used;
8917 unsigned char *keep, last, some_unused;
8918
8919 if (!is_ppc64_elf (ibfd))
8920 continue;
8921
8922 toc = bfd_get_section_by_name (ibfd, ".toc");
8923 if (toc == NULL
8924 || toc->size == 0
8925 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8926 || discarded_section (toc))
8927 continue;
8928
8929 toc_relocs = NULL;
8930 local_syms = NULL;
8931 symtab_hdr = &elf_symtab_hdr (ibfd);
8932
8933 /* Look at sections dropped from the final link. */
8934 skip = NULL;
8935 relstart = NULL;
8936 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8937 {
8938 if (sec->reloc_count == 0
8939 || !discarded_section (sec)
8940 || get_opd_info (sec)
8941 || (sec->flags & SEC_ALLOC) == 0
8942 || (sec->flags & SEC_DEBUGGING) != 0)
8943 continue;
8944
8945 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8946 if (relstart == NULL)
8947 goto error_ret;
8948
8949 /* Run through the relocs to see which toc entries might be
8950 unused. */
8951 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8952 {
8953 enum elf_ppc64_reloc_type r_type;
8954 unsigned long r_symndx;
8955 asection *sym_sec;
8956 struct elf_link_hash_entry *h;
8957 Elf_Internal_Sym *sym;
8958 bfd_vma val;
8959
8960 r_type = ELF64_R_TYPE (rel->r_info);
8961 switch (r_type)
8962 {
8963 default:
8964 continue;
8965
8966 case R_PPC64_TOC16:
8967 case R_PPC64_TOC16_LO:
8968 case R_PPC64_TOC16_HI:
8969 case R_PPC64_TOC16_HA:
8970 case R_PPC64_TOC16_DS:
8971 case R_PPC64_TOC16_LO_DS:
8972 break;
8973 }
8974
8975 r_symndx = ELF64_R_SYM (rel->r_info);
8976 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8977 r_symndx, ibfd))
8978 goto error_ret;
8979
8980 if (sym_sec != toc)
8981 continue;
8982
8983 if (h != NULL)
8984 val = h->root.u.def.value;
8985 else
8986 val = sym->st_value;
8987 val += rel->r_addend;
8988
8989 if (val >= toc->size)
8990 continue;
8991
8992 /* Anything in the toc ought to be aligned to 8 bytes.
8993 If not, don't mark as unused. */
8994 if (val & 7)
8995 continue;
8996
8997 if (skip == NULL)
8998 {
8999 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9000 if (skip == NULL)
9001 goto error_ret;
9002 }
9003
9004 skip[val >> 3] = ref_from_discarded;
9005 }
9006
9007 if (elf_section_data (sec)->relocs != relstart)
9008 free (relstart);
9009 }
9010
9011 /* For largetoc loads of address constants, we can convert
9012 . addis rx,2,addr@got@ha
9013 . ld ry,addr@got@l(rx)
9014 to
9015 . addis rx,2,addr@toc@ha
9016 . addi ry,rx,addr@toc@l
9017 when addr is within 2G of the toc pointer. This then means
9018 that the word storing "addr" in the toc is no longer needed. */
9019
9020 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9021 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9022 && toc->reloc_count != 0)
9023 {
9024 /* Read toc relocs. */
9025 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9026 info->keep_memory);
9027 if (toc_relocs == NULL)
9028 goto error_ret;
9029
9030 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9031 {
9032 enum elf_ppc64_reloc_type r_type;
9033 unsigned long r_symndx;
9034 asection *sym_sec;
9035 struct elf_link_hash_entry *h;
9036 Elf_Internal_Sym *sym;
9037 bfd_vma val, addr;
9038
9039 r_type = ELF64_R_TYPE (rel->r_info);
9040 if (r_type != R_PPC64_ADDR64)
9041 continue;
9042
9043 r_symndx = ELF64_R_SYM (rel->r_info);
9044 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9045 r_symndx, ibfd))
9046 goto error_ret;
9047
9048 if (sym_sec == NULL
9049 || sym_sec->output_section == NULL
9050 || discarded_section (sym_sec))
9051 continue;
9052
9053 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9054 || (bfd_link_pic (info)
9055 && sym_sec == bfd_abs_section_ptr))
9056 continue;
9057
9058 if (h != NULL)
9059 {
9060 if (h->type == STT_GNU_IFUNC)
9061 continue;
9062 val = h->root.u.def.value;
9063 }
9064 else
9065 {
9066 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9067 continue;
9068 val = sym->st_value;
9069 }
9070 val += rel->r_addend;
9071 val += sym_sec->output_section->vma + sym_sec->output_offset;
9072
9073 /* We don't yet know the exact toc pointer value, but we
9074 know it will be somewhere in the toc section. Don't
9075 optimize if the difference from any possible toc
9076 pointer is outside [ff..f80008000, 7fff7fff]. */
9077 addr = toc->output_section->vma + TOC_BASE_OFF;
9078 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9079 continue;
9080
9081 addr = toc->output_section->vma + toc->output_section->rawsize;
9082 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9083 continue;
9084
9085 if (skip == NULL)
9086 {
9087 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9088 if (skip == NULL)
9089 goto error_ret;
9090 }
9091
9092 skip[rel->r_offset >> 3]
9093 |= can_optimize | ((rel - toc_relocs) << 2);
9094 }
9095 }
9096
9097 if (skip == NULL)
9098 continue;
9099
9100 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9101 if (used == NULL)
9102 {
9103 error_ret:
9104 if (symtab_hdr->contents != (unsigned char *) local_syms)
9105 free (local_syms);
9106 if (sec != NULL
9107 && elf_section_data (sec)->relocs != relstart)
9108 free (relstart);
9109 if (elf_section_data (toc)->relocs != toc_relocs)
9110 free (toc_relocs);
9111 free (skip);
9112 return false;
9113 }
9114
9115 /* Now check all kept sections that might reference the toc.
9116 Check the toc itself last. */
9117 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9118 : ibfd->sections);
9119 sec != NULL;
9120 sec = (sec == toc ? NULL
9121 : sec->next == NULL ? toc
9122 : sec->next == toc && toc->next ? toc->next
9123 : sec->next))
9124 {
9125 int repeat;
9126
9127 if (sec->reloc_count == 0
9128 || discarded_section (sec)
9129 || get_opd_info (sec)
9130 || (sec->flags & SEC_ALLOC) == 0
9131 || (sec->flags & SEC_DEBUGGING) != 0)
9132 continue;
9133
9134 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9135 info->keep_memory);
9136 if (relstart == NULL)
9137 {
9138 free (used);
9139 goto error_ret;
9140 }
9141
9142 /* Mark toc entries referenced as used. */
9143 do
9144 {
9145 repeat = 0;
9146 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9147 {
9148 enum elf_ppc64_reloc_type r_type;
9149 unsigned long r_symndx;
9150 asection *sym_sec;
9151 struct elf_link_hash_entry *h;
9152 Elf_Internal_Sym *sym;
9153 bfd_vma val;
9154
9155 r_type = ELF64_R_TYPE (rel->r_info);
9156 switch (r_type)
9157 {
9158 case R_PPC64_TOC16:
9159 case R_PPC64_TOC16_LO:
9160 case R_PPC64_TOC16_HI:
9161 case R_PPC64_TOC16_HA:
9162 case R_PPC64_TOC16_DS:
9163 case R_PPC64_TOC16_LO_DS:
9164 /* In case we're taking addresses of toc entries. */
9165 case R_PPC64_ADDR64:
9166 break;
9167
9168 default:
9169 continue;
9170 }
9171
9172 r_symndx = ELF64_R_SYM (rel->r_info);
9173 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9174 r_symndx, ibfd))
9175 {
9176 free (used);
9177 goto error_ret;
9178 }
9179
9180 if (sym_sec != toc)
9181 continue;
9182
9183 if (h != NULL)
9184 val = h->root.u.def.value;
9185 else
9186 val = sym->st_value;
9187 val += rel->r_addend;
9188
9189 if (val >= toc->size)
9190 continue;
9191
9192 if ((skip[val >> 3] & can_optimize) != 0)
9193 {
9194 bfd_vma off;
9195 unsigned char opc;
9196
9197 switch (r_type)
9198 {
9199 case R_PPC64_TOC16_HA:
9200 break;
9201
9202 case R_PPC64_TOC16_LO_DS:
9203 off = rel->r_offset;
9204 off += (bfd_big_endian (ibfd) ? -2 : 3);
9205 if (!bfd_get_section_contents (ibfd, sec, &opc,
9206 off, 1))
9207 {
9208 free (used);
9209 goto error_ret;
9210 }
9211 if ((opc & (0x3f << 2)) == (58u << 2))
9212 break;
9213 /* Fall through. */
9214
9215 default:
9216 /* Wrong sort of reloc, or not a ld. We may
9217 as well clear ref_from_discarded too. */
9218 skip[val >> 3] = 0;
9219 }
9220 }
9221
9222 if (sec != toc)
9223 used[val >> 3] = 1;
9224 /* For the toc section, we only mark as used if this
9225 entry itself isn't unused. */
9226 else if ((used[rel->r_offset >> 3]
9227 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9228 && !used[val >> 3])
9229 {
9230 /* Do all the relocs again, to catch reference
9231 chains. */
9232 repeat = 1;
9233 used[val >> 3] = 1;
9234 }
9235 }
9236 }
9237 while (repeat);
9238
9239 if (elf_section_data (sec)->relocs != relstart)
9240 free (relstart);
9241 }
9242
9243 /* Merge the used and skip arrays. Assume that TOC
9244 doublewords not appearing as either used or unused belong
9245 to an entry more than one doubleword in size. */
9246 for (drop = skip, keep = used, last = 0, some_unused = 0;
9247 drop < skip + (toc->size + 7) / 8;
9248 ++drop, ++keep)
9249 {
9250 if (*keep)
9251 {
9252 *drop &= ~ref_from_discarded;
9253 if ((*drop & can_optimize) != 0)
9254 some_unused = 1;
9255 last = 0;
9256 }
9257 else if ((*drop & ref_from_discarded) != 0)
9258 {
9259 some_unused = 1;
9260 last = ref_from_discarded;
9261 }
9262 else
9263 *drop = last;
9264 }
9265
9266 free (used);
9267
9268 if (some_unused)
9269 {
9270 bfd_byte *contents, *src;
9271 unsigned long off;
9272 Elf_Internal_Sym *sym;
9273 bool local_toc_syms = false;
9274
9275 /* Shuffle the toc contents, and at the same time convert the
9276 skip array from booleans into offsets. */
9277 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9278 goto error_ret;
9279
9280 elf_section_data (toc)->this_hdr.contents = contents;
9281
9282 for (src = contents, off = 0, drop = skip;
9283 src < contents + toc->size;
9284 src += 8, ++drop)
9285 {
9286 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9287 off += 8;
9288 else if (off != 0)
9289 {
9290 *drop = off;
9291 memcpy (src - off, src, 8);
9292 }
9293 }
9294 *drop = off;
9295 toc->rawsize = toc->size;
9296 toc->size = src - contents - off;
9297
9298 /* Adjust addends for relocs against the toc section sym,
9299 and optimize any accesses we can. */
9300 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9301 {
9302 if (sec->reloc_count == 0
9303 || discarded_section (sec))
9304 continue;
9305
9306 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9307 info->keep_memory);
9308 if (relstart == NULL)
9309 goto error_ret;
9310
9311 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9312 {
9313 enum elf_ppc64_reloc_type r_type;
9314 unsigned long r_symndx;
9315 asection *sym_sec;
9316 struct elf_link_hash_entry *h;
9317 bfd_vma val;
9318
9319 r_type = ELF64_R_TYPE (rel->r_info);
9320 switch (r_type)
9321 {
9322 default:
9323 continue;
9324
9325 case R_PPC64_TOC16:
9326 case R_PPC64_TOC16_LO:
9327 case R_PPC64_TOC16_HI:
9328 case R_PPC64_TOC16_HA:
9329 case R_PPC64_TOC16_DS:
9330 case R_PPC64_TOC16_LO_DS:
9331 case R_PPC64_ADDR64:
9332 break;
9333 }
9334
9335 r_symndx = ELF64_R_SYM (rel->r_info);
9336 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9337 r_symndx, ibfd))
9338 goto error_ret;
9339
9340 if (sym_sec != toc)
9341 continue;
9342
9343 if (h != NULL)
9344 val = h->root.u.def.value;
9345 else
9346 {
9347 val = sym->st_value;
9348 if (val != 0)
9349 local_toc_syms = true;
9350 }
9351
9352 val += rel->r_addend;
9353
9354 if (val > toc->rawsize)
9355 val = toc->rawsize;
9356 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9357 continue;
9358 else if ((skip[val >> 3] & can_optimize) != 0)
9359 {
9360 Elf_Internal_Rela *tocrel
9361 = toc_relocs + (skip[val >> 3] >> 2);
9362 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9363
9364 switch (r_type)
9365 {
9366 case R_PPC64_TOC16_HA:
9367 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9368 break;
9369
9370 case R_PPC64_TOC16_LO_DS:
9371 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9372 break;
9373
9374 default:
9375 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9376 ppc_howto_init ();
9377 info->callbacks->einfo
9378 /* xgettext:c-format */
9379 (_("%H: %s references "
9380 "optimized away TOC entry\n"),
9381 ibfd, sec, rel->r_offset,
9382 ppc64_elf_howto_table[r_type]->name);
9383 bfd_set_error (bfd_error_bad_value);
9384 goto error_ret;
9385 }
9386 rel->r_addend = tocrel->r_addend;
9387 elf_section_data (sec)->relocs = relstart;
9388 continue;
9389 }
9390
9391 if (h != NULL || sym->st_value != 0)
9392 continue;
9393
9394 rel->r_addend -= skip[val >> 3];
9395 elf_section_data (sec)->relocs = relstart;
9396 }
9397
9398 if (elf_section_data (sec)->relocs != relstart)
9399 free (relstart);
9400 }
9401
9402 /* We shouldn't have local or global symbols defined in the TOC,
9403 but handle them anyway. */
9404 if (local_syms != NULL)
9405 for (sym = local_syms;
9406 sym < local_syms + symtab_hdr->sh_info;
9407 ++sym)
9408 if (sym->st_value != 0
9409 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9410 {
9411 unsigned long i;
9412
9413 if (sym->st_value > toc->rawsize)
9414 i = toc->rawsize >> 3;
9415 else
9416 i = sym->st_value >> 3;
9417
9418 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9419 {
9420 if (local_toc_syms)
9421 _bfd_error_handler
9422 (_("%s defined on removed toc entry"),
9423 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9424 do
9425 ++i;
9426 while ((skip[i] & (ref_from_discarded | can_optimize)));
9427 sym->st_value = (bfd_vma) i << 3;
9428 }
9429
9430 sym->st_value -= skip[i];
9431 symtab_hdr->contents = (unsigned char *) local_syms;
9432 }
9433
9434 /* Adjust any global syms defined in this toc input section. */
9435 if (toc_inf.global_toc_syms)
9436 {
9437 toc_inf.toc = toc;
9438 toc_inf.skip = skip;
9439 toc_inf.global_toc_syms = false;
9440 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9441 &toc_inf);
9442 }
9443
9444 if (toc->reloc_count != 0)
9445 {
9446 Elf_Internal_Shdr *rel_hdr;
9447 Elf_Internal_Rela *wrel;
9448 bfd_size_type sz;
9449
9450 /* Remove unused toc relocs, and adjust those we keep. */
9451 if (toc_relocs == NULL)
9452 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9453 info->keep_memory);
9454 if (toc_relocs == NULL)
9455 goto error_ret;
9456
9457 wrel = toc_relocs;
9458 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9459 if ((skip[rel->r_offset >> 3]
9460 & (ref_from_discarded | can_optimize)) == 0)
9461 {
9462 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9463 wrel->r_info = rel->r_info;
9464 wrel->r_addend = rel->r_addend;
9465 ++wrel;
9466 }
9467 else if (!dec_dynrel_count (rel, toc, info,
9468 &local_syms, NULL, NULL))
9469 goto error_ret;
9470
9471 elf_section_data (toc)->relocs = toc_relocs;
9472 toc->reloc_count = wrel - toc_relocs;
9473 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9474 sz = rel_hdr->sh_entsize;
9475 rel_hdr->sh_size = toc->reloc_count * sz;
9476 }
9477 }
9478 else if (elf_section_data (toc)->relocs != toc_relocs)
9479 free (toc_relocs);
9480
9481 if (local_syms != NULL
9482 && symtab_hdr->contents != (unsigned char *) local_syms)
9483 {
9484 if (!info->keep_memory)
9485 free (local_syms);
9486 else
9487 symtab_hdr->contents = (unsigned char *) local_syms;
9488 }
9489 free (skip);
9490 }
9491
9492 /* Look for cases where we can change an indirect GOT access to
9493 a GOT relative or PC relative access, possibly reducing the
9494 number of GOT entries. */
9495 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9496 {
9497 asection *sec;
9498 Elf_Internal_Shdr *symtab_hdr;
9499 Elf_Internal_Sym *local_syms;
9500 Elf_Internal_Rela *relstart, *rel;
9501 bfd_vma got;
9502
9503 if (!is_ppc64_elf (ibfd))
9504 continue;
9505
9506 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9507 continue;
9508
9509 sec = ppc64_elf_tdata (ibfd)->got;
9510 got = 0;
9511 if (sec != NULL)
9512 got = sec->output_section->vma + sec->output_offset + 0x8000;
9513
9514 local_syms = NULL;
9515 symtab_hdr = &elf_symtab_hdr (ibfd);
9516
9517 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9518 {
9519 if (sec->reloc_count == 0
9520 || !ppc64_elf_section_data (sec)->has_optrel
9521 || discarded_section (sec))
9522 continue;
9523
9524 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9525 info->keep_memory);
9526 if (relstart == NULL)
9527 {
9528 got_error_ret:
9529 if (symtab_hdr->contents != (unsigned char *) local_syms)
9530 free (local_syms);
9531 if (sec != NULL
9532 && elf_section_data (sec)->relocs != relstart)
9533 free (relstart);
9534 return false;
9535 }
9536
9537 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9538 {
9539 enum elf_ppc64_reloc_type r_type;
9540 unsigned long r_symndx;
9541 Elf_Internal_Sym *sym;
9542 asection *sym_sec;
9543 struct elf_link_hash_entry *h;
9544 struct got_entry *ent;
9545 bfd_vma val, pc;
9546 unsigned char buf[8];
9547 unsigned int insn;
9548 enum {no_check, check_lo, check_ha} insn_check;
9549
9550 r_type = ELF64_R_TYPE (rel->r_info);
9551 switch (r_type)
9552 {
9553 default:
9554 insn_check = no_check;
9555 break;
9556
9557 case R_PPC64_PLT16_HA:
9558 case R_PPC64_GOT_TLSLD16_HA:
9559 case R_PPC64_GOT_TLSGD16_HA:
9560 case R_PPC64_GOT_TPREL16_HA:
9561 case R_PPC64_GOT_DTPREL16_HA:
9562 case R_PPC64_GOT16_HA:
9563 case R_PPC64_TOC16_HA:
9564 insn_check = check_ha;
9565 break;
9566
9567 case R_PPC64_PLT16_LO:
9568 case R_PPC64_PLT16_LO_DS:
9569 case R_PPC64_GOT_TLSLD16_LO:
9570 case R_PPC64_GOT_TLSGD16_LO:
9571 case R_PPC64_GOT_TPREL16_LO_DS:
9572 case R_PPC64_GOT_DTPREL16_LO_DS:
9573 case R_PPC64_GOT16_LO:
9574 case R_PPC64_GOT16_LO_DS:
9575 case R_PPC64_TOC16_LO:
9576 case R_PPC64_TOC16_LO_DS:
9577 insn_check = check_lo;
9578 break;
9579 }
9580
9581 if (insn_check != no_check)
9582 {
9583 bfd_vma off = rel->r_offset & ~3;
9584
9585 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9586 goto got_error_ret;
9587
9588 insn = bfd_get_32 (ibfd, buf);
9589 if (insn_check == check_lo
9590 ? !ok_lo_toc_insn (insn, r_type)
9591 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9592 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9593 {
9594 char str[12];
9595
9596 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9597 sprintf (str, "%#08x", insn);
9598 info->callbacks->einfo
9599 /* xgettext:c-format */
9600 (_("%H: got/toc optimization is not supported for"
9601 " %s instruction\n"),
9602 ibfd, sec, rel->r_offset & ~3, str);
9603 continue;
9604 }
9605 }
9606
9607 switch (r_type)
9608 {
9609 /* Note that we don't delete GOT entries for
9610 R_PPC64_GOT16_DS since we'd need a lot more
9611 analysis. For starters, the preliminary layout is
9612 before the GOT, PLT, dynamic sections and stubs are
9613 laid out. Then we'd need to allow for changes in
9614 distance between sections caused by alignment. */
9615 default:
9616 continue;
9617
9618 case R_PPC64_GOT16_HA:
9619 case R_PPC64_GOT16_LO_DS:
9620 case R_PPC64_GOT_PCREL34:
9621 break;
9622 }
9623
9624 r_symndx = ELF64_R_SYM (rel->r_info);
9625 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9626 r_symndx, ibfd))
9627 goto got_error_ret;
9628
9629 if (sym_sec == NULL
9630 || sym_sec->output_section == NULL
9631 || discarded_section (sym_sec))
9632 continue;
9633
9634 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9635 continue;
9636
9637 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9638 || (bfd_link_pic (info)
9639 && sym_sec == bfd_abs_section_ptr))
9640 continue;
9641
9642 if (h != NULL)
9643 val = h->root.u.def.value;
9644 else
9645 val = sym->st_value;
9646 val += rel->r_addend;
9647 val += sym_sec->output_section->vma + sym_sec->output_offset;
9648
9649 /* Fudge factor to allow for the fact that the preliminary layout
9650 isn't exact. Reduce limits by this factor. */
9651 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9652
9653 switch (r_type)
9654 {
9655 default:
9656 continue;
9657
9658 case R_PPC64_GOT16_HA:
9659 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9660 >= LIMIT_ADJUST (0x100000000ULL))
9661 continue;
9662
9663 if (!bfd_get_section_contents (ibfd, sec, buf,
9664 rel->r_offset & ~3, 4))
9665 goto got_error_ret;
9666 insn = bfd_get_32 (ibfd, buf);
9667 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9668 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9669 continue;
9670 break;
9671
9672 case R_PPC64_GOT16_LO_DS:
9673 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9674 >= LIMIT_ADJUST (0x100000000ULL))
9675 continue;
9676 if (!bfd_get_section_contents (ibfd, sec, buf,
9677 rel->r_offset & ~3, 4))
9678 goto got_error_ret;
9679 insn = bfd_get_32 (ibfd, buf);
9680 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9681 continue;
9682 break;
9683
9684 case R_PPC64_GOT_PCREL34:
9685 pc = rel->r_offset;
9686 pc += sec->output_section->vma + sec->output_offset;
9687 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9688 >= LIMIT_ADJUST (1ULL << 34))
9689 continue;
9690 if (!bfd_get_section_contents (ibfd, sec, buf,
9691 rel->r_offset & ~3, 8))
9692 goto got_error_ret;
9693 insn = bfd_get_32 (ibfd, buf);
9694 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9695 continue;
9696 insn = bfd_get_32 (ibfd, buf + 4);
9697 if ((insn & (0x3fu << 26)) != 57u << 26)
9698 continue;
9699 break;
9700 }
9701 #undef LIMIT_ADJUST
9702
9703 if (h != NULL)
9704 ent = h->got.glist;
9705 else
9706 {
9707 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9708 ent = local_got_ents[r_symndx];
9709 }
9710 for (; ent != NULL; ent = ent->next)
9711 if (ent->addend == rel->r_addend
9712 && ent->owner == ibfd
9713 && ent->tls_type == 0)
9714 break;
9715 BFD_ASSERT (ent && ent->got.refcount > 0);
9716 ent->got.refcount -= 1;
9717 }
9718
9719 if (elf_section_data (sec)->relocs != relstart)
9720 free (relstart);
9721 }
9722
9723 if (local_syms != NULL
9724 && symtab_hdr->contents != (unsigned char *) local_syms)
9725 {
9726 if (!info->keep_memory)
9727 free (local_syms);
9728 else
9729 symtab_hdr->contents = (unsigned char *) local_syms;
9730 }
9731 }
9732
9733 return true;
9734 }
9735
9736 /* Return true iff input section I references the TOC using
9737 instructions limited to +/-32k offsets. */
9738
9739 bool
9740 ppc64_elf_has_small_toc_reloc (asection *i)
9741 {
9742 return (is_ppc64_elf (i->owner)
9743 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9744 }
9745
9746 /* Allocate space for one GOT entry. */
9747
9748 static void
9749 allocate_got (struct elf_link_hash_entry *h,
9750 struct bfd_link_info *info,
9751 struct got_entry *gent)
9752 {
9753 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9754 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9755 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9756 ? 16 : 8);
9757 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9758 ? 2 : 1) * sizeof (Elf64_External_Rela);
9759 asection *got = ppc64_elf_tdata (gent->owner)->got;
9760
9761 gent->got.offset = got->size;
9762 got->size += entsize;
9763
9764 if (h->type == STT_GNU_IFUNC)
9765 {
9766 htab->elf.irelplt->size += rentsize;
9767 htab->got_reli_size += rentsize;
9768 }
9769 else if (((bfd_link_pic (info)
9770 && (gent->tls_type == 0
9771 ? !info->enable_dt_relr
9772 : !(bfd_link_executable (info)
9773 && SYMBOL_REFERENCES_LOCAL (info, h)))
9774 && !bfd_is_abs_symbol (&h->root))
9775 || (htab->elf.dynamic_sections_created
9776 && h->dynindx != -1
9777 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9778 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9779 {
9780 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9781 relgot->size += rentsize;
9782 }
9783 }
9784
9785 /* This function merges got entries in the same toc group. */
9786
9787 static void
9788 merge_got_entries (struct got_entry **pent)
9789 {
9790 struct got_entry *ent, *ent2;
9791
9792 for (ent = *pent; ent != NULL; ent = ent->next)
9793 if (!ent->is_indirect)
9794 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9795 if (!ent2->is_indirect
9796 && ent2->addend == ent->addend
9797 && ent2->tls_type == ent->tls_type
9798 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9799 {
9800 ent2->is_indirect = true;
9801 ent2->got.ent = ent;
9802 }
9803 }
9804
9805 /* If H is undefined, make it dynamic if that makes sense. */
9806
9807 static bool
9808 ensure_undef_dynamic (struct bfd_link_info *info,
9809 struct elf_link_hash_entry *h)
9810 {
9811 struct elf_link_hash_table *htab = elf_hash_table (info);
9812
9813 if (htab->dynamic_sections_created
9814 && ((info->dynamic_undefined_weak != 0
9815 && h->root.type == bfd_link_hash_undefweak)
9816 || h->root.type == bfd_link_hash_undefined)
9817 && h->dynindx == -1
9818 && !h->forced_local
9819 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9820 return bfd_elf_link_record_dynamic_symbol (info, h);
9821 return true;
9822 }
9823
9824 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9825 usual htab->elf.splt section for a PLT entry. */
9826
9827 static inline
9828 bool use_local_plt (struct bfd_link_info *info,
9829 struct elf_link_hash_entry *h)
9830 {
9831 return (h == NULL
9832 || h->dynindx == -1
9833 || !elf_hash_table (info)->dynamic_sections_created);
9834 }
9835
9836 /* Allocate space in .plt, .got and associated reloc sections for
9837 dynamic relocs. */
9838
9839 static bool
9840 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9841 {
9842 struct bfd_link_info *info;
9843 struct ppc_link_hash_table *htab;
9844 asection *s;
9845 struct ppc_link_hash_entry *eh;
9846 struct got_entry **pgent, *gent;
9847
9848 if (h->root.type == bfd_link_hash_indirect)
9849 return true;
9850
9851 info = (struct bfd_link_info *) inf;
9852 htab = ppc_hash_table (info);
9853 if (htab == NULL)
9854 return false;
9855
9856 eh = ppc_elf_hash_entry (h);
9857 /* Run through the TLS GD got entries first if we're changing them
9858 to TPREL. */
9859 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9860 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9861 if (gent->got.refcount > 0
9862 && (gent->tls_type & TLS_GD) != 0)
9863 {
9864 /* This was a GD entry that has been converted to TPREL. If
9865 there happens to be a TPREL entry we can use that one. */
9866 struct got_entry *ent;
9867 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9868 if (ent->got.refcount > 0
9869 && (ent->tls_type & TLS_TPREL) != 0
9870 && ent->addend == gent->addend
9871 && ent->owner == gent->owner)
9872 {
9873 gent->got.refcount = 0;
9874 break;
9875 }
9876
9877 /* If not, then we'll be using our own TPREL entry. */
9878 if (gent->got.refcount != 0)
9879 gent->tls_type = TLS_TLS | TLS_TPREL;
9880 }
9881
9882 /* Remove any list entry that won't generate a word in the GOT before
9883 we call merge_got_entries. Otherwise we risk merging to empty
9884 entries. */
9885 pgent = &h->got.glist;
9886 while ((gent = *pgent) != NULL)
9887 if (gent->got.refcount > 0)
9888 {
9889 if ((gent->tls_type & TLS_LD) != 0
9890 && SYMBOL_REFERENCES_LOCAL (info, h))
9891 {
9892 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9893 *pgent = gent->next;
9894 }
9895 else
9896 pgent = &gent->next;
9897 }
9898 else
9899 *pgent = gent->next;
9900
9901 if (!htab->do_multi_toc)
9902 merge_got_entries (&h->got.glist);
9903
9904 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9905 if (!gent->is_indirect)
9906 {
9907 /* Ensure we catch all the cases where this symbol should
9908 be made dynamic. */
9909 if (!ensure_undef_dynamic (info, h))
9910 return false;
9911
9912 if (!is_ppc64_elf (gent->owner))
9913 abort ();
9914
9915 allocate_got (h, info, gent);
9916 }
9917
9918 /* If no dynamic sections we can't have dynamic relocs, except for
9919 IFUNCs which are handled even in static executables. */
9920 if (!htab->elf.dynamic_sections_created
9921 && h->type != STT_GNU_IFUNC)
9922 h->dyn_relocs = NULL;
9923
9924 /* Discard relocs on undefined symbols that must be local. */
9925 else if (h->root.type == bfd_link_hash_undefined
9926 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9927 h->dyn_relocs = NULL;
9928
9929 /* Also discard relocs on undefined weak syms with non-default
9930 visibility, or when dynamic_undefined_weak says so. */
9931 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9932 h->dyn_relocs = NULL;
9933
9934 if (h->dyn_relocs != NULL)
9935 {
9936 struct ppc_dyn_relocs *p, **pp;
9937
9938 /* In the shared -Bsymbolic case, discard space allocated for
9939 dynamic pc-relative relocs against symbols which turn out to
9940 be defined in regular objects. For the normal shared case,
9941 discard space for relocs that have become local due to symbol
9942 visibility changes. */
9943 if (bfd_link_pic (info))
9944 {
9945 /* Relocs that use pc_count are those that appear on a call
9946 insn, or certain REL relocs (see must_be_dyn_reloc) that
9947 can be generated via assembly. We want calls to
9948 protected symbols to resolve directly to the function
9949 rather than going via the plt. If people want function
9950 pointer comparisons to work as expected then they should
9951 avoid writing weird assembly. */
9952 if (SYMBOL_CALLS_LOCAL (info, h))
9953 {
9954 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9955 (p = *pp) != NULL;
9956 )
9957 {
9958 p->count -= p->pc_count;
9959 p->pc_count = 0;
9960 if (p->count == 0)
9961 *pp = p->next;
9962 else
9963 pp = &p->next;
9964 }
9965 }
9966
9967 if (h->dyn_relocs != NULL)
9968 {
9969 /* Ensure we catch all the cases where this symbol
9970 should be made dynamic. */
9971 if (!ensure_undef_dynamic (info, h))
9972 return false;
9973 }
9974 }
9975
9976 /* For a fixed position executable, discard space for
9977 relocs against symbols which are not dynamic. */
9978 else if (h->type != STT_GNU_IFUNC)
9979 {
9980 if ((h->dynamic_adjusted
9981 || (h->ref_regular
9982 && h->root.type == bfd_link_hash_undefweak
9983 && (info->dynamic_undefined_weak > 0
9984 || !_bfd_elf_readonly_dynrelocs (h))))
9985 && !h->def_regular
9986 && !ELF_COMMON_DEF_P (h))
9987 {
9988 /* Ensure we catch all the cases where this symbol
9989 should be made dynamic. */
9990 if (!ensure_undef_dynamic (info, h))
9991 return false;
9992
9993 /* But if that didn't work out, discard dynamic relocs. */
9994 if (h->dynindx == -1)
9995 h->dyn_relocs = NULL;
9996 }
9997 else
9998 h->dyn_relocs = NULL;
9999 }
10000
10001 /* Finally, allocate space. */
10002 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10003 if (!discarded_section (p->sec))
10004 {
10005 unsigned int count;
10006 asection *sreloc = elf_section_data (p->sec)->sreloc;
10007 if (eh->elf.type == STT_GNU_IFUNC)
10008 sreloc = htab->elf.irelplt;
10009 count = p->count;
10010 if (info->enable_dt_relr
10011 && ((!NO_OPD_RELOCS
10012 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10013 || (eh->elf.type != STT_GNU_IFUNC
10014 && SYMBOL_REFERENCES_LOCAL (info, h))))
10015 count -= p->rel_count;
10016 sreloc->size += count * sizeof (Elf64_External_Rela);
10017 }
10018 }
10019
10020 /* We might need a PLT entry when the symbol
10021 a) is dynamic, or
10022 b) is an ifunc, or
10023 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10024 d) has plt16 relocs and we are linking statically. */
10025 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10026 || h->type == STT_GNU_IFUNC
10027 || (h->needs_plt && h->dynamic_adjusted)
10028 || (h->needs_plt
10029 && h->def_regular
10030 && !htab->elf.dynamic_sections_created
10031 && !htab->can_convert_all_inline_plt
10032 && (ppc_elf_hash_entry (h)->tls_mask
10033 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10034 {
10035 struct plt_entry *pent;
10036 bool doneone = false;
10037 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10038 if (pent->plt.refcount > 0)
10039 {
10040 if (!ensure_undef_dynamic (info, h))
10041 return false;
10042
10043 if (use_local_plt (info, h))
10044 {
10045 if (h->type == STT_GNU_IFUNC)
10046 {
10047 s = htab->elf.iplt;
10048 pent->plt.offset = s->size;
10049 s->size += PLT_ENTRY_SIZE (htab);
10050 s = htab->elf.irelplt;
10051 }
10052 else
10053 {
10054 s = htab->pltlocal;
10055 pent->plt.offset = s->size;
10056 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10057 s = NULL;
10058 if (bfd_link_pic (info)
10059 && !(info->enable_dt_relr && !htab->opd_abi))
10060 s = htab->relpltlocal;
10061 }
10062 }
10063 else
10064 {
10065 /* If this is the first .plt entry, make room for the special
10066 first entry. */
10067 s = htab->elf.splt;
10068 if (s->size == 0)
10069 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10070
10071 pent->plt.offset = s->size;
10072
10073 /* Make room for this entry. */
10074 s->size += PLT_ENTRY_SIZE (htab);
10075
10076 /* Make room for the .glink code. */
10077 s = htab->glink;
10078 if (s->size == 0)
10079 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10080 if (htab->opd_abi)
10081 {
10082 /* We need bigger stubs past index 32767. */
10083 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10084 s->size += 4;
10085 s->size += 2*4;
10086 }
10087 else
10088 s->size += 4;
10089
10090 /* We also need to make an entry in the .rela.plt section. */
10091 s = htab->elf.srelplt;
10092 }
10093 if (s != NULL)
10094 s->size += sizeof (Elf64_External_Rela);
10095 doneone = true;
10096 }
10097 else
10098 pent->plt.offset = (bfd_vma) -1;
10099 if (!doneone)
10100 {
10101 h->plt.plist = NULL;
10102 h->needs_plt = 0;
10103 }
10104 }
10105 else
10106 {
10107 h->plt.plist = NULL;
10108 h->needs_plt = 0;
10109 }
10110
10111 return true;
10112 }
10113
10114 #define PPC_LO(v) ((v) & 0xffff)
10115 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10116 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10117 #define D34(v) \
10118 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10119 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10120
10121 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10122 to set up space for global entry stubs. These are put in glink,
10123 after the branch table. */
10124
10125 static bool
10126 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10127 {
10128 struct bfd_link_info *info;
10129 struct ppc_link_hash_table *htab;
10130 struct plt_entry *pent;
10131 asection *s, *plt;
10132
10133 if (h->root.type == bfd_link_hash_indirect)
10134 return true;
10135
10136 if (!h->pointer_equality_needed)
10137 return true;
10138
10139 if (h->def_regular)
10140 return true;
10141
10142 info = inf;
10143 htab = ppc_hash_table (info);
10144 if (htab == NULL)
10145 return false;
10146
10147 s = htab->global_entry;
10148 plt = htab->elf.splt;
10149 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10150 if (pent->plt.offset != (bfd_vma) -1
10151 && pent->addend == 0)
10152 {
10153 /* For ELFv2, if this symbol is not defined in a regular file
10154 and we are not generating a shared library or pie, then we
10155 need to define the symbol in the executable on a call stub.
10156 This is to avoid text relocations. */
10157 bfd_vma off, stub_align, stub_off, stub_size;
10158 unsigned int align_power;
10159
10160 stub_size = 16;
10161 stub_off = s->size;
10162 if (htab->params->plt_stub_align >= 0)
10163 align_power = htab->params->plt_stub_align;
10164 else
10165 align_power = -htab->params->plt_stub_align;
10166 /* Setting section alignment is delayed until we know it is
10167 non-empty. Otherwise the .text output section will be
10168 aligned at least to plt_stub_align even when no global
10169 entry stubs are needed. */
10170 if (s->alignment_power < align_power)
10171 s->alignment_power = align_power;
10172 stub_align = (bfd_vma) 1 << align_power;
10173 if (htab->params->plt_stub_align >= 0
10174 || ((((stub_off + stub_size - 1) & -stub_align)
10175 - (stub_off & -stub_align))
10176 > ((stub_size - 1) & -stub_align)))
10177 stub_off = (stub_off + stub_align - 1) & -stub_align;
10178 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10179 off -= stub_off + s->output_offset + s->output_section->vma;
10180 /* Note that for --plt-stub-align negative we have a possible
10181 dependency between stub offset and size. Break that
10182 dependency by assuming the max stub size when calculating
10183 the stub offset. */
10184 if (PPC_HA (off) == 0)
10185 stub_size -= 4;
10186 h->root.type = bfd_link_hash_defined;
10187 h->root.u.def.section = s;
10188 h->root.u.def.value = stub_off;
10189 s->size = stub_off + stub_size;
10190 break;
10191 }
10192 return true;
10193 }
10194
10195 /* Set the sizes of the dynamic sections. */
10196
10197 static bool
10198 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10199 struct bfd_link_info *info)
10200 {
10201 struct ppc_link_hash_table *htab;
10202 bfd *dynobj;
10203 asection *s;
10204 bool relocs;
10205 bfd *ibfd;
10206 struct got_entry *first_tlsld;
10207
10208 htab = ppc_hash_table (info);
10209 if (htab == NULL)
10210 return false;
10211
10212 dynobj = htab->elf.dynobj;
10213 if (dynobj == NULL)
10214 abort ();
10215
10216 if (htab->elf.dynamic_sections_created)
10217 {
10218 /* Set the contents of the .interp section to the interpreter. */
10219 if (bfd_link_executable (info) && !info->nointerp)
10220 {
10221 s = bfd_get_linker_section (dynobj, ".interp");
10222 if (s == NULL)
10223 abort ();
10224 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10225 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10226 }
10227 }
10228
10229 /* Set up .got offsets for local syms, and space for local dynamic
10230 relocs. */
10231 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10232 {
10233 struct got_entry **lgot_ents;
10234 struct got_entry **end_lgot_ents;
10235 struct plt_entry **local_plt;
10236 struct plt_entry **end_local_plt;
10237 unsigned char *lgot_masks;
10238 bfd_size_type locsymcount;
10239 Elf_Internal_Shdr *symtab_hdr;
10240 Elf_Internal_Sym *local_syms;
10241 Elf_Internal_Sym *isym;
10242
10243 if (!is_ppc64_elf (ibfd))
10244 continue;
10245
10246 for (s = ibfd->sections; s != NULL; s = s->next)
10247 {
10248 struct ppc_local_dyn_relocs *p;
10249
10250 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10251 {
10252 if (discarded_section (p->sec))
10253 {
10254 /* Input section has been discarded, either because
10255 it is a copy of a linkonce section or due to
10256 linker script /DISCARD/, so we'll be discarding
10257 the relocs too. */
10258 }
10259 else if (p->count != 0)
10260 {
10261 unsigned int count;
10262 asection *srel;
10263
10264 count = p->count;
10265 if (info->enable_dt_relr
10266 && ((!NO_OPD_RELOCS
10267 && (ppc64_elf_section_data (p->sec)->sec_type
10268 == sec_opd))
10269 || !p->ifunc))
10270 count -= p->rel_count;
10271 srel = elf_section_data (p->sec)->sreloc;
10272 if (p->ifunc)
10273 srel = htab->elf.irelplt;
10274 srel->size += count * sizeof (Elf64_External_Rela);
10275 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10276 info->flags |= DF_TEXTREL;
10277 }
10278 }
10279 }
10280
10281 lgot_ents = elf_local_got_ents (ibfd);
10282 if (!lgot_ents)
10283 continue;
10284
10285 symtab_hdr = &elf_symtab_hdr (ibfd);
10286 locsymcount = symtab_hdr->sh_info;
10287 end_lgot_ents = lgot_ents + locsymcount;
10288 local_plt = (struct plt_entry **) end_lgot_ents;
10289 end_local_plt = local_plt + locsymcount;
10290 lgot_masks = (unsigned char *) end_local_plt;
10291 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10292 if (local_syms == NULL && locsymcount != 0)
10293 {
10294 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10295 0, NULL, NULL, NULL);
10296 if (local_syms == NULL)
10297 return false;
10298 }
10299 s = ppc64_elf_tdata (ibfd)->got;
10300 for (isym = local_syms;
10301 lgot_ents < end_lgot_ents;
10302 ++lgot_ents, ++lgot_masks, isym++)
10303 {
10304 struct got_entry **pent, *ent;
10305
10306 pent = lgot_ents;
10307 while ((ent = *pent) != NULL)
10308 if (ent->got.refcount > 0)
10309 {
10310 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10311 {
10312 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10313 *pent = ent->next;
10314 }
10315 else
10316 {
10317 unsigned int ent_size = 8;
10318 unsigned int rel_size = sizeof (Elf64_External_Rela);
10319
10320 ent->got.offset = s->size;
10321 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10322 {
10323 ent_size *= 2;
10324 rel_size *= 2;
10325 }
10326 s->size += ent_size;
10327 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10328 {
10329 htab->elf.irelplt->size += rel_size;
10330 htab->got_reli_size += rel_size;
10331 }
10332 else if (bfd_link_pic (info)
10333 && (ent->tls_type == 0
10334 ? !info->enable_dt_relr
10335 : !bfd_link_executable (info))
10336 && isym->st_shndx != SHN_ABS)
10337 {
10338 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10339 srel->size += rel_size;
10340 }
10341 pent = &ent->next;
10342 }
10343 }
10344 else
10345 *pent = ent->next;
10346 }
10347 if (local_syms != NULL
10348 && symtab_hdr->contents != (unsigned char *) local_syms)
10349 {
10350 if (!info->keep_memory)
10351 free (local_syms);
10352 else
10353 symtab_hdr->contents = (unsigned char *) local_syms;
10354 }
10355
10356 /* Allocate space for plt calls to local syms. */
10357 lgot_masks = (unsigned char *) end_local_plt;
10358 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10359 {
10360 struct plt_entry *ent;
10361
10362 for (ent = *local_plt; ent != NULL; ent = ent->next)
10363 if (ent->plt.refcount > 0)
10364 {
10365 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10366 {
10367 s = htab->elf.iplt;
10368 ent->plt.offset = s->size;
10369 s->size += PLT_ENTRY_SIZE (htab);
10370 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10371 }
10372 else if (htab->can_convert_all_inline_plt
10373 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10374 ent->plt.offset = (bfd_vma) -1;
10375 else
10376 {
10377 s = htab->pltlocal;
10378 ent->plt.offset = s->size;
10379 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10380 if (bfd_link_pic (info)
10381 && !(info->enable_dt_relr && !htab->opd_abi))
10382 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10383 }
10384 }
10385 else
10386 ent->plt.offset = (bfd_vma) -1;
10387 }
10388 }
10389
10390 /* Allocate global sym .plt and .got entries, and space for global
10391 sym dynamic relocs. */
10392 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10393
10394 if (!htab->opd_abi && !bfd_link_pic (info))
10395 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10396
10397 first_tlsld = NULL;
10398 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10399 {
10400 struct got_entry *ent;
10401
10402 if (!is_ppc64_elf (ibfd))
10403 continue;
10404
10405 ent = ppc64_tlsld_got (ibfd);
10406 if (ent->got.refcount > 0)
10407 {
10408 if (!htab->do_multi_toc && first_tlsld != NULL)
10409 {
10410 ent->is_indirect = true;
10411 ent->got.ent = first_tlsld;
10412 }
10413 else
10414 {
10415 if (first_tlsld == NULL)
10416 first_tlsld = ent;
10417 s = ppc64_elf_tdata (ibfd)->got;
10418 ent->got.offset = s->size;
10419 ent->owner = ibfd;
10420 s->size += 16;
10421 if (bfd_link_dll (info))
10422 {
10423 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10424 srel->size += sizeof (Elf64_External_Rela);
10425 }
10426 }
10427 }
10428 else
10429 ent->got.offset = (bfd_vma) -1;
10430 }
10431
10432 /* We now have determined the sizes of the various dynamic sections.
10433 Allocate memory for them. */
10434 relocs = false;
10435 for (s = dynobj->sections; s != NULL; s = s->next)
10436 {
10437 if ((s->flags & SEC_LINKER_CREATED) == 0)
10438 continue;
10439
10440 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10441 /* These haven't been allocated yet; don't strip. */
10442 continue;
10443 else if (s == htab->elf.sgot
10444 || s == htab->elf.splt
10445 || s == htab->elf.iplt
10446 || s == htab->pltlocal
10447 || s == htab->glink
10448 || s == htab->global_entry
10449 || s == htab->elf.sdynbss
10450 || s == htab->elf.sdynrelro)
10451 {
10452 /* Strip this section if we don't need it; see the
10453 comment below. */
10454 }
10455 else if (s == htab->glink_eh_frame)
10456 {
10457 if (!bfd_is_abs_section (s->output_section))
10458 /* Not sized yet. */
10459 continue;
10460 }
10461 else if (startswith (s->name, ".rela"))
10462 {
10463 if (s->size != 0)
10464 {
10465 if (s != htab->elf.srelplt)
10466 relocs = true;
10467
10468 /* We use the reloc_count field as a counter if we need
10469 to copy relocs into the output file. */
10470 s->reloc_count = 0;
10471 }
10472 }
10473 else
10474 {
10475 /* It's not one of our sections, so don't allocate space. */
10476 continue;
10477 }
10478
10479 if (s->size == 0)
10480 {
10481 /* If we don't need this section, strip it from the
10482 output file. This is mostly to handle .rela.bss and
10483 .rela.plt. We must create both sections in
10484 create_dynamic_sections, because they must be created
10485 before the linker maps input sections to output
10486 sections. The linker does that before
10487 adjust_dynamic_symbol is called, and it is that
10488 function which decides whether anything needs to go
10489 into these sections. */
10490 s->flags |= SEC_EXCLUDE;
10491 continue;
10492 }
10493
10494 if (bfd_is_abs_section (s->output_section))
10495 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10496 s->name);
10497
10498 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10499 continue;
10500
10501 /* Allocate memory for the section contents. We use bfd_zalloc
10502 here in case unused entries are not reclaimed before the
10503 section's contents are written out. This should not happen,
10504 but this way if it does we get a R_PPC64_NONE reloc in .rela
10505 sections instead of garbage.
10506 We also rely on the section contents being zero when writing
10507 the GOT and .dynrelro. */
10508 s->contents = bfd_zalloc (dynobj, s->size);
10509 if (s->contents == NULL)
10510 return false;
10511 }
10512
10513 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10514 {
10515 if (!is_ppc64_elf (ibfd))
10516 continue;
10517
10518 s = ppc64_elf_tdata (ibfd)->got;
10519 if (s != NULL && s != htab->elf.sgot)
10520 {
10521 if (s->size == 0)
10522 s->flags |= SEC_EXCLUDE;
10523 else
10524 {
10525 s->contents = bfd_zalloc (ibfd, s->size);
10526 if (s->contents == NULL)
10527 return false;
10528 }
10529 }
10530 s = ppc64_elf_tdata (ibfd)->relgot;
10531 if (s != NULL)
10532 {
10533 if (s->size == 0)
10534 s->flags |= SEC_EXCLUDE;
10535 else
10536 {
10537 s->contents = bfd_zalloc (ibfd, s->size);
10538 if (s->contents == NULL)
10539 return false;
10540 relocs = true;
10541 s->reloc_count = 0;
10542 }
10543 }
10544 }
10545
10546 if (htab->elf.dynamic_sections_created)
10547 {
10548 bool tls_opt;
10549
10550 /* Add some entries to the .dynamic section. We fill in the
10551 values later, in ppc64_elf_finish_dynamic_sections, but we
10552 must add the entries now so that we get the correct size for
10553 the .dynamic section. The DT_DEBUG entry is filled in by the
10554 dynamic linker and used by the debugger. */
10555 #define add_dynamic_entry(TAG, VAL) \
10556 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10557
10558 if (bfd_link_executable (info))
10559 {
10560 if (!add_dynamic_entry (DT_DEBUG, 0))
10561 return false;
10562 }
10563
10564 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10565 {
10566 if (!add_dynamic_entry (DT_PLTGOT, 0)
10567 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10568 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10569 || !add_dynamic_entry (DT_JMPREL, 0)
10570 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10571 return false;
10572 }
10573
10574 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10575 {
10576 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10577 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10578 return false;
10579 }
10580
10581 tls_opt = (htab->params->tls_get_addr_opt
10582 && ((htab->tls_get_addr_fd != NULL
10583 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10584 || (htab->tga_desc_fd != NULL
10585 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10586 if (tls_opt || !htab->opd_abi)
10587 {
10588 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10589 return false;
10590 }
10591
10592 if (relocs)
10593 {
10594 if (!add_dynamic_entry (DT_RELA, 0)
10595 || !add_dynamic_entry (DT_RELASZ, 0)
10596 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10597 return false;
10598
10599 /* If any dynamic relocs apply to a read-only section,
10600 then we need a DT_TEXTREL entry. */
10601 if ((info->flags & DF_TEXTREL) == 0)
10602 elf_link_hash_traverse (&htab->elf,
10603 _bfd_elf_maybe_set_textrel, info);
10604
10605 if ((info->flags & DF_TEXTREL) != 0)
10606 {
10607 if (!add_dynamic_entry (DT_TEXTREL, 0))
10608 return false;
10609 }
10610 }
10611 }
10612 #undef add_dynamic_entry
10613
10614 return true;
10615 }
10616
10617 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10618
10619 static bool
10620 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10621 {
10622 if (h->plt.plist != NULL
10623 && !h->def_regular
10624 && !h->pointer_equality_needed)
10625 return false;
10626
10627 return _bfd_elf_hash_symbol (h);
10628 }
10629
10630 /* Determine the type of stub needed, if any, for a call. */
10631
10632 static inline enum ppc_stub_main_type
10633 ppc_type_of_stub (asection *input_sec,
10634 const Elf_Internal_Rela *rel,
10635 struct ppc_link_hash_entry **hash,
10636 struct plt_entry **plt_ent,
10637 bfd_vma destination,
10638 unsigned long local_off)
10639 {
10640 struct ppc_link_hash_entry *h = *hash;
10641 bfd_vma location;
10642 bfd_vma branch_offset;
10643 bfd_vma max_branch_offset;
10644 enum elf_ppc64_reloc_type r_type;
10645
10646 if (h != NULL)
10647 {
10648 struct plt_entry *ent;
10649 struct ppc_link_hash_entry *fdh = h;
10650 if (h->oh != NULL
10651 && h->oh->is_func_descriptor)
10652 {
10653 fdh = ppc_follow_link (h->oh);
10654 *hash = fdh;
10655 }
10656
10657 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10658 if (ent->addend == rel->r_addend
10659 && ent->plt.offset != (bfd_vma) -1)
10660 {
10661 *plt_ent = ent;
10662 return ppc_stub_plt_call;
10663 }
10664
10665 /* Here, we know we don't have a plt entry. If we don't have a
10666 either a defined function descriptor or a defined entry symbol
10667 in a regular object file, then it is pointless trying to make
10668 any other type of stub. */
10669 if (!is_static_defined (&fdh->elf)
10670 && !is_static_defined (&h->elf))
10671 return ppc_stub_none;
10672 }
10673 else if (elf_local_got_ents (input_sec->owner) != NULL)
10674 {
10675 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10676 struct plt_entry **local_plt = (struct plt_entry **)
10677 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10678 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10679
10680 if (local_plt[r_symndx] != NULL)
10681 {
10682 struct plt_entry *ent;
10683
10684 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10685 if (ent->addend == rel->r_addend
10686 && ent->plt.offset != (bfd_vma) -1)
10687 {
10688 *plt_ent = ent;
10689 return ppc_stub_plt_call;
10690 }
10691 }
10692 }
10693
10694 /* Determine where the call point is. */
10695 location = (input_sec->output_offset
10696 + input_sec->output_section->vma
10697 + rel->r_offset);
10698
10699 branch_offset = destination - location;
10700 r_type = ELF64_R_TYPE (rel->r_info);
10701
10702 /* Determine if a long branch stub is needed. */
10703 max_branch_offset = 1 << 25;
10704 if (r_type == R_PPC64_REL14
10705 || r_type == R_PPC64_REL14_BRTAKEN
10706 || r_type == R_PPC64_REL14_BRNTAKEN)
10707 max_branch_offset = 1 << 15;
10708
10709 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10710 /* We need a stub. Figure out whether a long_branch or plt_branch
10711 is needed later. */
10712 return ppc_stub_long_branch;
10713
10714 return ppc_stub_none;
10715 }
10716
10717 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10718 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10719 . mflr %r12
10720 . bcl 20,31,1f
10721 .1: mflr %r11
10722 . mtlr %r12
10723 . lis %r12,xxx-1b@highest
10724 . ori %r12,%r12,xxx-1b@higher
10725 . sldi %r12,%r12,32
10726 . oris %r12,%r12,xxx-1b@high
10727 . ori %r12,%r12,xxx-1b@l
10728 . add/ldx %r12,%r11,%r12 */
10729
10730 static bfd_byte *
10731 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10732 {
10733 bfd_put_32 (abfd, MFLR_R12, p);
10734 p += 4;
10735 bfd_put_32 (abfd, BCL_20_31, p);
10736 p += 4;
10737 bfd_put_32 (abfd, MFLR_R11, p);
10738 p += 4;
10739 bfd_put_32 (abfd, MTLR_R12, p);
10740 p += 4;
10741 if (off + 0x8000 < 0x10000)
10742 {
10743 if (load)
10744 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10745 else
10746 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10747 p += 4;
10748 }
10749 else if (off + 0x80008000ULL < 0x100000000ULL)
10750 {
10751 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10752 p += 4;
10753 if (load)
10754 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10755 else
10756 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10757 p += 4;
10758 }
10759 else
10760 {
10761 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10762 {
10763 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10764 p += 4;
10765 }
10766 else
10767 {
10768 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10769 p += 4;
10770 if (((off >> 32) & 0xffff) != 0)
10771 {
10772 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10773 p += 4;
10774 }
10775 }
10776 if (((off >> 32) & 0xffffffffULL) != 0)
10777 {
10778 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10779 p += 4;
10780 }
10781 if (PPC_HI (off) != 0)
10782 {
10783 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10784 p += 4;
10785 }
10786 if (PPC_LO (off) != 0)
10787 {
10788 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10789 p += 4;
10790 }
10791 if (load)
10792 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10793 else
10794 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10795 p += 4;
10796 }
10797 return p;
10798 }
10799
10800 static unsigned int
10801 size_offset (bfd_vma off)
10802 {
10803 unsigned int size;
10804 if (off + 0x8000 < 0x10000)
10805 size = 4;
10806 else if (off + 0x80008000ULL < 0x100000000ULL)
10807 size = 8;
10808 else
10809 {
10810 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10811 size = 4;
10812 else
10813 {
10814 size = 4;
10815 if (((off >> 32) & 0xffff) != 0)
10816 size += 4;
10817 }
10818 if (((off >> 32) & 0xffffffffULL) != 0)
10819 size += 4;
10820 if (PPC_HI (off) != 0)
10821 size += 4;
10822 if (PPC_LO (off) != 0)
10823 size += 4;
10824 size += 4;
10825 }
10826 return size + 16;
10827 }
10828
10829 static unsigned int
10830 num_relocs_for_offset (bfd_vma off)
10831 {
10832 unsigned int num_rel;
10833 if (off + 0x8000 < 0x10000)
10834 num_rel = 1;
10835 else if (off + 0x80008000ULL < 0x100000000ULL)
10836 num_rel = 2;
10837 else
10838 {
10839 num_rel = 1;
10840 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10841 && ((off >> 32) & 0xffff) != 0)
10842 num_rel += 1;
10843 if (PPC_HI (off) != 0)
10844 num_rel += 1;
10845 if (PPC_LO (off) != 0)
10846 num_rel += 1;
10847 }
10848 return num_rel;
10849 }
10850
10851 static Elf_Internal_Rela *
10852 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10853 bfd_vma roff, bfd_vma targ, bfd_vma off)
10854 {
10855 bfd_vma relative_targ = targ - (roff - 8);
10856 if (bfd_big_endian (info->output_bfd))
10857 roff += 2;
10858 r->r_offset = roff;
10859 r->r_addend = relative_targ + roff;
10860 if (off + 0x8000 < 0x10000)
10861 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10862 else if (off + 0x80008000ULL < 0x100000000ULL)
10863 {
10864 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10865 ++r;
10866 roff += 4;
10867 r->r_offset = roff;
10868 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10869 r->r_addend = relative_targ + roff;
10870 }
10871 else
10872 {
10873 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10874 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10875 else
10876 {
10877 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10878 if (((off >> 32) & 0xffff) != 0)
10879 {
10880 ++r;
10881 roff += 4;
10882 r->r_offset = roff;
10883 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10884 r->r_addend = relative_targ + roff;
10885 }
10886 }
10887 if (((off >> 32) & 0xffffffffULL) != 0)
10888 roff += 4;
10889 if (PPC_HI (off) != 0)
10890 {
10891 ++r;
10892 roff += 4;
10893 r->r_offset = roff;
10894 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10895 r->r_addend = relative_targ + roff;
10896 }
10897 if (PPC_LO (off) != 0)
10898 {
10899 ++r;
10900 roff += 4;
10901 r->r_offset = roff;
10902 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10903 r->r_addend = relative_targ + roff;
10904 }
10905 }
10906 return r;
10907 }
10908
10909 static bfd_byte *
10910 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10911 bool load)
10912 {
10913 uint64_t insn;
10914 if (off - odd + (1ULL << 33) < 1ULL << 34)
10915 {
10916 off -= odd;
10917 if (odd)
10918 {
10919 bfd_put_32 (abfd, NOP, p);
10920 p += 4;
10921 }
10922 if (load)
10923 insn = PLD_R12_PC;
10924 else
10925 insn = PADDI_R12_PC;
10926 insn |= D34 (off);
10927 bfd_put_32 (abfd, insn >> 32, p);
10928 p += 4;
10929 bfd_put_32 (abfd, insn, p);
10930 }
10931 /* The minimum value for paddi is -0x200000000. The minimum value
10932 for li is -0x8000, which when shifted by 34 and added gives a
10933 minimum value of -0x2000200000000. The maximum value is
10934 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10935 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10936 {
10937 off -= 8 - odd;
10938 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10939 p += 4;
10940 if (!odd)
10941 {
10942 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10943 p += 4;
10944 }
10945 insn = PADDI_R12_PC | D34 (off);
10946 bfd_put_32 (abfd, insn >> 32, p);
10947 p += 4;
10948 bfd_put_32 (abfd, insn, p);
10949 p += 4;
10950 if (odd)
10951 {
10952 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10953 p += 4;
10954 }
10955 if (load)
10956 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10957 else
10958 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10959 }
10960 else
10961 {
10962 off -= odd + 8;
10963 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10964 p += 4;
10965 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10966 p += 4;
10967 if (odd)
10968 {
10969 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10970 p += 4;
10971 }
10972 insn = PADDI_R12_PC | D34 (off);
10973 bfd_put_32 (abfd, insn >> 32, p);
10974 p += 4;
10975 bfd_put_32 (abfd, insn, p);
10976 p += 4;
10977 if (!odd)
10978 {
10979 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10980 p += 4;
10981 }
10982 if (load)
10983 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10984 else
10985 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10986 }
10987 p += 4;
10988 return p;
10989 }
10990
10991 static unsigned int
10992 size_power10_offset (bfd_vma off, int odd)
10993 {
10994 if (off - odd + (1ULL << 33) < 1ULL << 34)
10995 return odd + 8;
10996 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10997 return 20;
10998 else
10999 return 24;
11000 }
11001
11002 static unsigned int
11003 num_relocs_for_power10_offset (bfd_vma off, int odd)
11004 {
11005 if (off - odd + (1ULL << 33) < 1ULL << 34)
11006 return 1;
11007 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11008 return 2;
11009 else
11010 return 3;
11011 }
11012
11013 static Elf_Internal_Rela *
11014 emit_relocs_for_power10_offset (struct bfd_link_info *info,
11015 Elf_Internal_Rela *r, bfd_vma roff,
11016 bfd_vma targ, bfd_vma off, int odd)
11017 {
11018 if (off - odd + (1ULL << 33) < 1ULL << 34)
11019 roff += odd;
11020 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11021 {
11022 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11023 r->r_offset = roff + d_offset;
11024 r->r_addend = targ + 8 - odd - d_offset;
11025 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11026 ++r;
11027 roff += 8 - odd;
11028 }
11029 else
11030 {
11031 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11032 r->r_offset = roff + d_offset;
11033 r->r_addend = targ + 8 + odd - d_offset;
11034 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11035 ++r;
11036 roff += 4;
11037 r->r_offset = roff + d_offset;
11038 r->r_addend = targ + 4 + odd - d_offset;
11039 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11040 ++r;
11041 roff += 4 + odd;
11042 }
11043 r->r_offset = roff;
11044 r->r_addend = targ;
11045 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11046 return r;
11047 }
11048
11049 /* Emit .eh_frame opcode to advance pc by DELTA. */
11050
11051 static bfd_byte *
11052 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11053 {
11054 delta /= 4;
11055 if (delta < 64)
11056 *eh++ = DW_CFA_advance_loc + delta;
11057 else if (delta < 256)
11058 {
11059 *eh++ = DW_CFA_advance_loc1;
11060 *eh++ = delta;
11061 }
11062 else if (delta < 65536)
11063 {
11064 *eh++ = DW_CFA_advance_loc2;
11065 bfd_put_16 (abfd, delta, eh);
11066 eh += 2;
11067 }
11068 else
11069 {
11070 *eh++ = DW_CFA_advance_loc4;
11071 bfd_put_32 (abfd, delta, eh);
11072 eh += 4;
11073 }
11074 return eh;
11075 }
11076
11077 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11078
11079 static unsigned int
11080 eh_advance_size (unsigned int delta)
11081 {
11082 if (delta < 64 * 4)
11083 /* DW_CFA_advance_loc+[1..63]. */
11084 return 1;
11085 if (delta < 256 * 4)
11086 /* DW_CFA_advance_loc1, byte. */
11087 return 2;
11088 if (delta < 65536 * 4)
11089 /* DW_CFA_advance_loc2, 2 bytes. */
11090 return 3;
11091 /* DW_CFA_advance_loc4, 4 bytes. */
11092 return 5;
11093 }
11094
11095 /* With power7 weakly ordered memory model, it is possible for ld.so
11096 to update a plt entry in one thread and have another thread see a
11097 stale zero toc entry. To avoid this we need some sort of acquire
11098 barrier in the call stub. One solution is to make the load of the
11099 toc word seem to appear to depend on the load of the function entry
11100 word. Another solution is to test for r2 being zero, and branch to
11101 the appropriate glink entry if so.
11102
11103 . fake dep barrier compare
11104 . ld 12,xxx(2) ld 12,xxx(2)
11105 . mtctr 12 mtctr 12
11106 . xor 11,12,12 ld 2,xxx+8(2)
11107 . add 2,2,11 cmpldi 2,0
11108 . ld 2,xxx+8(2) bnectr+
11109 . bctr b <glink_entry>
11110
11111 The solution involving the compare turns out to be faster, so
11112 that's what we use unless the branch won't reach. */
11113
11114 #define ALWAYS_USE_FAKE_DEP 0
11115 #define ALWAYS_EMIT_R2SAVE 0
11116
11117 static inline unsigned int
11118 plt_stub_size (struct ppc_link_hash_table *htab,
11119 struct ppc_stub_hash_entry *stub_entry,
11120 bfd_vma off,
11121 unsigned int odd)
11122 {
11123 unsigned size;
11124
11125 if (stub_entry->type.sub == ppc_stub_notoc)
11126 {
11127 size = 8 + size_power10_offset (off, odd);
11128 if (stub_entry->type.r2save)
11129 size += 4;
11130 }
11131 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11132 {
11133 size = 8 + size_offset (off - 8);
11134 if (stub_entry->type.r2save)
11135 size += 4;
11136 }
11137 else
11138 {
11139 size = 12;
11140 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11141 size += 4;
11142 if (PPC_HA (off) != 0)
11143 size += 4;
11144 if (htab->opd_abi)
11145 {
11146 size += 4;
11147 if (htab->params->plt_static_chain)
11148 size += 4;
11149 if (htab->params->plt_thread_safe
11150 && htab->elf.dynamic_sections_created
11151 && stub_entry->h != NULL
11152 && stub_entry->h->elf.dynindx != -1)
11153 size += 8;
11154 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11155 != PPC_HA (off))
11156 size += 4;
11157 }
11158 }
11159 if (stub_entry->h != NULL
11160 && is_tls_get_addr (&stub_entry->h->elf, htab)
11161 && htab->params->tls_get_addr_opt)
11162 {
11163 if (!htab->params->no_tls_get_addr_regsave)
11164 {
11165 size += 30 * 4;
11166 if (stub_entry->type.r2save)
11167 size += 4;
11168 }
11169 else
11170 {
11171 size += 7 * 4;
11172 if (stub_entry->type.r2save)
11173 size += 6 * 4;
11174 }
11175 }
11176 return size;
11177 }
11178
11179 /* Depending on the sign of plt_stub_align:
11180 If positive, return the padding to align to a 2**plt_stub_align
11181 boundary.
11182 If negative, if this stub would cross fewer 2**plt_stub_align
11183 boundaries if we align, then return the padding needed to do so. */
11184
11185 static inline unsigned int
11186 plt_stub_pad (int plt_stub_align,
11187 bfd_vma stub_off,
11188 unsigned int stub_size)
11189 {
11190 unsigned int stub_align;
11191
11192 if (plt_stub_align >= 0)
11193 stub_align = 1u << plt_stub_align;
11194 else
11195 {
11196 stub_align = 1u << -plt_stub_align;
11197 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11198 <= ((stub_size - 1) & -stub_align))
11199 return 0;
11200 }
11201 return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
11202 }
11203
11204 /* Build a toc using .plt call stub. */
11205
11206 static inline bfd_byte *
11207 build_plt_stub (struct ppc_link_hash_table *htab,
11208 struct ppc_stub_hash_entry *stub_entry,
11209 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11210 {
11211 bfd *obfd = htab->params->stub_bfd;
11212 bool plt_load_toc = htab->opd_abi;
11213 bool plt_static_chain = htab->params->plt_static_chain;
11214 bool plt_thread_safe = (htab->params->plt_thread_safe
11215 && htab->elf.dynamic_sections_created
11216 && stub_entry->h != NULL
11217 && stub_entry->h->elf.dynindx != -1);
11218 bool use_fake_dep = plt_thread_safe;
11219 bfd_vma cmp_branch_off = 0;
11220
11221 if (!ALWAYS_USE_FAKE_DEP
11222 && plt_load_toc
11223 && plt_thread_safe
11224 && !(stub_entry->h != NULL
11225 && is_tls_get_addr (&stub_entry->h->elf, htab)
11226 && htab->params->tls_get_addr_opt))
11227 {
11228 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11229 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11230 / PLT_ENTRY_SIZE (htab));
11231 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11232 bfd_vma to, from;
11233
11234 if (pltindex > 32768)
11235 glinkoff += (pltindex - 32768) * 4;
11236 to = (glinkoff
11237 + htab->glink->output_offset
11238 + htab->glink->output_section->vma);
11239 from = (p - stub_entry->group->stub_sec->contents
11240 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11241 + 4 * (PPC_HA (offset) != 0)
11242 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11243 != PPC_HA (offset))
11244 + 4 * (plt_static_chain != 0)
11245 + 20
11246 + stub_entry->group->stub_sec->output_offset
11247 + stub_entry->group->stub_sec->output_section->vma);
11248 cmp_branch_off = to - from;
11249 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11250 }
11251
11252 if (PPC_HA (offset) != 0)
11253 {
11254 if (r != NULL)
11255 {
11256 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11257 r[0].r_offset += 4;
11258 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11259 r[1].r_offset = r[0].r_offset + 4;
11260 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11261 r[1].r_addend = r[0].r_addend;
11262 if (plt_load_toc)
11263 {
11264 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11265 {
11266 r[2].r_offset = r[1].r_offset + 4;
11267 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11268 r[2].r_addend = r[0].r_addend;
11269 }
11270 else
11271 {
11272 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11273 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11274 r[2].r_addend = r[0].r_addend + 8;
11275 if (plt_static_chain)
11276 {
11277 r[3].r_offset = r[2].r_offset + 4;
11278 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11279 r[3].r_addend = r[0].r_addend + 16;
11280 }
11281 }
11282 }
11283 }
11284 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11285 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11286 if (plt_load_toc)
11287 {
11288 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11289 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11290 }
11291 else
11292 {
11293 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11294 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11295 }
11296 if (plt_load_toc
11297 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11298 {
11299 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11300 offset = 0;
11301 }
11302 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11303 if (plt_load_toc)
11304 {
11305 if (use_fake_dep)
11306 {
11307 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11308 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11309 }
11310 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11311 if (plt_static_chain)
11312 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11313 }
11314 }
11315 else
11316 {
11317 if (r != NULL)
11318 {
11319 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11320 r[0].r_offset += 4;
11321 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11322 if (plt_load_toc)
11323 {
11324 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11325 {
11326 r[1].r_offset = r[0].r_offset + 4;
11327 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11328 r[1].r_addend = r[0].r_addend;
11329 }
11330 else
11331 {
11332 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11333 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11334 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11335 if (plt_static_chain)
11336 {
11337 r[2].r_offset = r[1].r_offset + 4;
11338 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11339 r[2].r_addend = r[0].r_addend + 8;
11340 }
11341 }
11342 }
11343 }
11344 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11345 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11346 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11347 if (plt_load_toc
11348 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11349 {
11350 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11351 offset = 0;
11352 }
11353 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11354 if (plt_load_toc)
11355 {
11356 if (use_fake_dep)
11357 {
11358 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11359 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11360 }
11361 if (plt_static_chain)
11362 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11363 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11364 }
11365 }
11366 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11367 {
11368 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11369 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11370 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11371 }
11372 else
11373 bfd_put_32 (obfd, BCTR, p), p += 4;
11374 return p;
11375 }
11376
11377 /* Build a special .plt call stub for __tls_get_addr. */
11378
11379 #define LD_R0_0R3 0xe8030000
11380 #define LD_R12_0R3 0xe9830000
11381 #define MR_R0_R3 0x7c601b78
11382 #define CMPDI_R0_0 0x2c200000
11383 #define ADD_R3_R12_R13 0x7c6c6a14
11384 #define BEQLR 0x4d820020
11385 #define MR_R3_R0 0x7c030378
11386 #define BCTRL 0x4e800421
11387
11388 static bfd_byte *
11389 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11390 struct ppc_stub_hash_entry *stub_entry,
11391 bfd_byte *p)
11392 {
11393 bfd *obfd = htab->params->stub_bfd;
11394
11395 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11396 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11397 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11398 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11399 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11400 bfd_put_32 (obfd, BEQLR, p), p += 4;
11401 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11402
11403 if (!htab->params->no_tls_get_addr_regsave)
11404 p = tls_get_addr_prologue (obfd, p, htab);
11405 else if (stub_entry->type.r2save)
11406 {
11407 bfd_put_32 (obfd, MFLR_R0, p);
11408 p += 4;
11409 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11410 p += 4;
11411 }
11412 return p;
11413 }
11414
11415 static bfd_byte *
11416 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11417 struct ppc_stub_hash_entry *stub_entry,
11418 bfd_byte *p,
11419 bfd_byte *loc)
11420 {
11421 bfd *obfd = htab->params->stub_bfd;
11422
11423 if (!htab->params->no_tls_get_addr_regsave)
11424 {
11425 bfd_put_32 (obfd, BCTRL, p - 4);
11426
11427 if (stub_entry->type.r2save)
11428 {
11429 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11430 p += 4;
11431 }
11432 p = tls_get_addr_epilogue (obfd, p, htab);
11433 }
11434 else if (stub_entry->type.r2save)
11435 {
11436 bfd_put_32 (obfd, BCTRL, p - 4);
11437
11438 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11439 p += 4;
11440 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11441 p += 4;
11442 bfd_put_32 (obfd, MTLR_R0, p);
11443 p += 4;
11444 bfd_put_32 (obfd, BLR, p);
11445 p += 4;
11446 }
11447
11448 if (htab->glink_eh_frame != NULL
11449 && htab->glink_eh_frame->size != 0)
11450 {
11451 bfd_byte *base, *eh;
11452
11453 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11454 eh = base + stub_entry->group->eh_size;
11455
11456 if (!htab->params->no_tls_get_addr_regsave)
11457 {
11458 unsigned int cfa_updt, delta, i;
11459
11460 /* After the bctrl, lr has been modified so we need to emit
11461 .eh_frame info saying the return address is on the stack. In
11462 fact we must put the EH info at or before the call rather
11463 than after it, because the EH info for a call needs to be
11464 specified by that point.
11465 See libgcc/unwind-dw2.c execute_cfa_program.
11466 Any stack pointer update must be described immediately after
11467 the instruction making the change, and since the stdu occurs
11468 after saving regs we put all the reg saves and the cfa
11469 change there. */
11470 cfa_updt = stub_entry->stub_offset + 18 * 4;
11471 delta = cfa_updt - stub_entry->group->lr_restore;
11472 stub_entry->group->lr_restore
11473 = stub_entry->stub_offset + (p - loc) - 4;
11474 eh = eh_advance (htab->elf.dynobj, eh, delta);
11475 *eh++ = DW_CFA_def_cfa_offset;
11476 if (htab->opd_abi)
11477 {
11478 *eh++ = 128;
11479 *eh++ = 1;
11480 }
11481 else
11482 *eh++ = 96;
11483 *eh++ = DW_CFA_offset_extended_sf;
11484 *eh++ = 65;
11485 *eh++ = (-16 / 8) & 0x7f;
11486 for (i = 4; i < 12; i++)
11487 {
11488 *eh++ = DW_CFA_offset + i;
11489 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11490 }
11491 *eh++ = (DW_CFA_advance_loc
11492 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11493 *eh++ = DW_CFA_def_cfa_offset;
11494 *eh++ = 0;
11495 for (i = 4; i < 12; i++)
11496 *eh++ = DW_CFA_restore + i;
11497 *eh++ = DW_CFA_advance_loc + 2;
11498 *eh++ = DW_CFA_restore_extended;
11499 *eh++ = 65;
11500 stub_entry->group->eh_size = eh - base;
11501 }
11502 else if (stub_entry->type.r2save)
11503 {
11504 unsigned int lr_used, delta;
11505
11506 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11507 delta = lr_used - stub_entry->group->lr_restore;
11508 stub_entry->group->lr_restore = lr_used + 16;
11509 eh = eh_advance (htab->elf.dynobj, eh, delta);
11510 *eh++ = DW_CFA_offset_extended_sf;
11511 *eh++ = 65;
11512 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11513 *eh++ = DW_CFA_advance_loc + 4;
11514 *eh++ = DW_CFA_restore_extended;
11515 *eh++ = 65;
11516 stub_entry->group->eh_size = eh - base;
11517 }
11518 }
11519 return p;
11520 }
11521
11522 static Elf_Internal_Rela *
11523 get_relocs (asection *sec, int count)
11524 {
11525 Elf_Internal_Rela *relocs;
11526 struct bfd_elf_section_data *elfsec_data;
11527
11528 elfsec_data = elf_section_data (sec);
11529 relocs = elfsec_data->relocs;
11530 if (relocs == NULL)
11531 {
11532 bfd_size_type relsize;
11533 relsize = sec->reloc_count * sizeof (*relocs);
11534 relocs = bfd_alloc (sec->owner, relsize);
11535 if (relocs == NULL)
11536 return NULL;
11537 elfsec_data->relocs = relocs;
11538 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11539 sizeof (Elf_Internal_Shdr));
11540 if (elfsec_data->rela.hdr == NULL)
11541 return NULL;
11542 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11543 * sizeof (Elf64_External_Rela));
11544 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11545 sec->reloc_count = 0;
11546 }
11547 relocs += sec->reloc_count;
11548 sec->reloc_count += count;
11549 return relocs;
11550 }
11551
11552 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11553 forms, to the equivalent relocs against the global symbol given by
11554 STUB_ENTRY->H. */
11555
11556 static bool
11557 use_global_in_relocs (struct ppc_link_hash_table *htab,
11558 struct ppc_stub_hash_entry *stub_entry,
11559 Elf_Internal_Rela *r, unsigned int num_rel)
11560 {
11561 struct elf_link_hash_entry **hashes;
11562 unsigned long symndx;
11563 struct ppc_link_hash_entry *h;
11564 bfd_vma symval;
11565
11566 /* Relocs are always against symbols in their own object file. Fake
11567 up global sym hashes for the stub bfd (which has no symbols). */
11568 hashes = elf_sym_hashes (htab->params->stub_bfd);
11569 if (hashes == NULL)
11570 {
11571 bfd_size_type hsize;
11572
11573 /* When called the first time, stub_globals will contain the
11574 total number of symbols seen during stub sizing. After
11575 allocating, stub_globals is used as an index to fill the
11576 hashes array. */
11577 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11578 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11579 if (hashes == NULL)
11580 return false;
11581 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11582 htab->stub_globals = 1;
11583 }
11584 symndx = htab->stub_globals++;
11585 h = stub_entry->h;
11586 hashes[symndx] = &h->elf;
11587 if (h->oh != NULL && h->oh->is_func)
11588 h = ppc_follow_link (h->oh);
11589 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11590 || h->elf.root.type == bfd_link_hash_defweak);
11591 symval = defined_sym_val (&h->elf);
11592 while (num_rel-- != 0)
11593 {
11594 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11595 if (h->elf.root.u.def.section != stub_entry->target_section)
11596 {
11597 /* H is an opd symbol. The addend must be zero, and the
11598 branch reloc is the only one we can convert. */
11599 r->r_addend = 0;
11600 break;
11601 }
11602 else
11603 r->r_addend -= symval;
11604 --r;
11605 }
11606 return true;
11607 }
11608
11609 static bfd_vma
11610 get_r2off (struct bfd_link_info *info,
11611 struct ppc_stub_hash_entry *stub_entry)
11612 {
11613 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11614 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11615
11616 if (r2off == 0)
11617 {
11618 /* Support linking -R objects. Get the toc pointer from the
11619 opd entry. */
11620 char buf[8];
11621 if (!htab->opd_abi)
11622 return r2off;
11623 asection *opd = stub_entry->h->elf.root.u.def.section;
11624 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11625
11626 if (strcmp (opd->name, ".opd") != 0
11627 || opd->reloc_count != 0)
11628 {
11629 info->callbacks->einfo
11630 (_("%P: cannot find opd entry toc for `%pT'\n"),
11631 stub_entry->h->elf.root.root.string);
11632 bfd_set_error (bfd_error_bad_value);
11633 return (bfd_vma) -1;
11634 }
11635 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11636 return (bfd_vma) -1;
11637 r2off = bfd_get_64 (opd->owner, buf);
11638 r2off -= elf_gp (info->output_bfd);
11639 }
11640 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11641 return r2off;
11642 }
11643
11644 /* Debug dump. */
11645
11646 static void
11647 dump_stub (const char *header,
11648 struct ppc_stub_hash_entry *stub_entry,
11649 size_t end_offset)
11650 {
11651 const char *t1, *t2, *t3;
11652 switch (stub_entry->type.main)
11653 {
11654 case ppc_stub_none: t1 = "none"; break;
11655 case ppc_stub_long_branch: t1 = "long_branch"; break;
11656 case ppc_stub_plt_branch: t1 = "plt_branch"; break;
11657 case ppc_stub_plt_call: t1 = "plt_call"; break;
11658 case ppc_stub_global_entry: t1 = "global_entry"; break;
11659 case ppc_stub_save_res: t1 = "save_res"; break;
11660 default: t1 = "???"; break;
11661 }
11662 switch (stub_entry->type.sub)
11663 {
11664 case ppc_stub_toc: t2 = "toc"; break;
11665 case ppc_stub_notoc: t2 = "notoc"; break;
11666 case ppc_stub_p9notoc: t2 = "p9notoc"; break;
11667 default: t2 = "???"; break;
11668 }
11669 t3 = stub_entry->type.r2save ? "r2save" : "";
11670 fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11671 header, stub_entry->id, t1, t2, t3);
11672 fprintf (stderr, "name = %s\n", stub_entry->root.string);
11673 fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
11674 for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11675 {
11676 asection *stub_sec = stub_entry->group->stub_sec;
11677 uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11678 fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
11679 }
11680 fprintf (stderr, "\n");
11681 }
11682
11683 static bool
11684 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11685 {
11686 struct ppc_stub_hash_entry *stub_entry;
11687 struct ppc_branch_hash_entry *br_entry;
11688 struct bfd_link_info *info;
11689 struct ppc_link_hash_table *htab;
11690 bfd *obfd;
11691 bfd_byte *loc;
11692 bfd_byte *p, *relp;
11693 bfd_vma targ, off;
11694 Elf_Internal_Rela *r;
11695 asection *plt;
11696 int num_rel;
11697 int odd;
11698 bool is_tga;
11699
11700 /* Massage our args to the form they really have. */
11701 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11702 info = in_arg;
11703
11704 htab = ppc_hash_table (info);
11705 if (htab == NULL)
11706 return false;
11707
11708 struct _ppc64_elf_section_data *esd
11709 = ppc64_elf_section_data (stub_entry->group->stub_sec);
11710 ++htab->stub_id;
11711 if (stub_entry->id != htab->stub_id
11712 || (stub_entry->type.main != ppc_stub_save_res
11713 && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
11714 {
11715 BFD_ASSERT (0);
11716 if (stub_entry->id != htab->stub_id)
11717 fprintf (stderr, "Expected id %u, got %u\n",
11718 htab->stub_id, stub_entry->id);
11719 if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
11720 fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11721 PRIx64 "\n", stub_entry->group->stub_sec->size,
11722 stub_entry->stub_offset);
11723 if (esd->sec_type == sec_stub)
11724 dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11725 dump_stub ("Current:", stub_entry, 0);
11726 }
11727 if (esd->sec_type == sec_normal)
11728 esd->sec_type = sec_stub;
11729 if (esd->sec_type == sec_stub)
11730 esd->u.last_ent = stub_entry;
11731 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11732
11733 htab->stub_count[stub_entry->type.main - 1] += 1;
11734 if (stub_entry->type.main == ppc_stub_long_branch
11735 && stub_entry->type.sub == ppc_stub_toc)
11736 {
11737 /* Branches are relative. This is where we are going to. */
11738 targ = (stub_entry->target_value
11739 + stub_entry->target_section->output_offset
11740 + stub_entry->target_section->output_section->vma);
11741 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11742
11743 /* And this is where we are coming from. */
11744 off = (stub_entry->stub_offset
11745 + stub_entry->group->stub_sec->output_offset
11746 + stub_entry->group->stub_sec->output_section->vma);
11747 off = targ - off;
11748
11749 p = loc;
11750 obfd = htab->params->stub_bfd;
11751 if (stub_entry->type.r2save)
11752 {
11753 bfd_vma r2off = get_r2off (info, stub_entry);
11754
11755 if (r2off == (bfd_vma) -1)
11756 {
11757 htab->stub_error = true;
11758 return false;
11759 }
11760 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11761 p += 4;
11762 if (PPC_HA (r2off) != 0)
11763 {
11764 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11765 p += 4;
11766 }
11767 if (PPC_LO (r2off) != 0)
11768 {
11769 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11770 p += 4;
11771 }
11772 off -= p - loc;
11773 }
11774 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11775 p += 4;
11776
11777 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11778 {
11779 _bfd_error_handler
11780 (_("long branch stub `%s' offset overflow"),
11781 stub_entry->root.string);
11782 htab->stub_error = true;
11783 return false;
11784 }
11785
11786 if (info->emitrelocations)
11787 {
11788 r = get_relocs (stub_entry->group->stub_sec, 1);
11789 if (r == NULL)
11790 return false;
11791 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11792 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11793 r->r_addend = targ;
11794 if (stub_entry->h != NULL
11795 && !use_global_in_relocs (htab, stub_entry, r, 1))
11796 return false;
11797 }
11798 }
11799 else if (stub_entry->type.main == ppc_stub_plt_branch
11800 && stub_entry->type.sub == ppc_stub_toc)
11801 {
11802 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11803 stub_entry->root.string + 9,
11804 false, false);
11805 if (br_entry == NULL)
11806 {
11807 _bfd_error_handler (_("can't find branch stub `%s'"),
11808 stub_entry->root.string);
11809 htab->stub_error = true;
11810 return false;
11811 }
11812
11813 targ = (stub_entry->target_value
11814 + stub_entry->target_section->output_offset
11815 + stub_entry->target_section->output_section->vma);
11816 if (!stub_entry->type.r2save)
11817 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11818
11819 bfd_put_64 (htab->brlt->owner, targ,
11820 htab->brlt->contents + br_entry->offset);
11821
11822 if (br_entry->iter == htab->stub_iteration)
11823 {
11824 br_entry->iter = 0;
11825
11826 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11827 {
11828 /* Create a reloc for the branch lookup table entry. */
11829 Elf_Internal_Rela rela;
11830 bfd_byte *rl;
11831
11832 rela.r_offset = (br_entry->offset
11833 + htab->brlt->output_offset
11834 + htab->brlt->output_section->vma);
11835 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11836 rela.r_addend = targ;
11837
11838 rl = htab->relbrlt->contents;
11839 rl += (htab->relbrlt->reloc_count++
11840 * sizeof (Elf64_External_Rela));
11841 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11842 }
11843 else if (info->emitrelocations)
11844 {
11845 r = get_relocs (htab->brlt, 1);
11846 if (r == NULL)
11847 return false;
11848 /* brlt, being SEC_LINKER_CREATED does not go through the
11849 normal reloc processing. Symbols and offsets are not
11850 translated from input file to output file form, so
11851 set up the offset per the output file. */
11852 r->r_offset = (br_entry->offset
11853 + htab->brlt->output_offset
11854 + htab->brlt->output_section->vma);
11855 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11856 r->r_addend = targ;
11857 }
11858 }
11859
11860 targ = (br_entry->offset
11861 + htab->brlt->output_offset
11862 + htab->brlt->output_section->vma);
11863
11864 off = (elf_gp (info->output_bfd)
11865 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11866 off = targ - off;
11867
11868 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11869 {
11870 info->callbacks->einfo
11871 (_("%P: linkage table error against `%pT'\n"),
11872 stub_entry->root.string);
11873 bfd_set_error (bfd_error_bad_value);
11874 htab->stub_error = true;
11875 return false;
11876 }
11877
11878 if (info->emitrelocations)
11879 {
11880 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11881 if (r == NULL)
11882 return false;
11883 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11884 if (bfd_big_endian (info->output_bfd))
11885 r[0].r_offset += 2;
11886 if (stub_entry->type.r2save)
11887 r[0].r_offset += 4;
11888 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11889 r[0].r_addend = targ;
11890 if (PPC_HA (off) != 0)
11891 {
11892 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11893 r[1].r_offset = r[0].r_offset + 4;
11894 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11895 r[1].r_addend = r[0].r_addend;
11896 }
11897 }
11898
11899 p = loc;
11900 obfd = htab->params->stub_bfd;
11901 if (!stub_entry->type.r2save)
11902 {
11903 if (PPC_HA (off) != 0)
11904 {
11905 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11906 p += 4;
11907 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11908 }
11909 else
11910 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11911 }
11912 else
11913 {
11914 bfd_vma r2off = get_r2off (info, stub_entry);
11915
11916 if (r2off == (bfd_vma) -1)
11917 {
11918 htab->stub_error = true;
11919 return false;
11920 }
11921
11922 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11923 p += 4;
11924 if (PPC_HA (off) != 0)
11925 {
11926 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11927 p += 4;
11928 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11929 }
11930 else
11931 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11932
11933 if (PPC_HA (r2off) != 0)
11934 {
11935 p += 4;
11936 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11937 }
11938 if (PPC_LO (r2off) != 0)
11939 {
11940 p += 4;
11941 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11942 }
11943 }
11944 p += 4;
11945 bfd_put_32 (obfd, MTCTR_R12, p);
11946 p += 4;
11947 bfd_put_32 (obfd, BCTR, p);
11948 p += 4;
11949 }
11950 else if (stub_entry->type.sub >= ppc_stub_notoc)
11951 {
11952 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
11953 p = loc;
11954 off = (stub_entry->stub_offset
11955 + stub_entry->group->stub_sec->output_offset
11956 + stub_entry->group->stub_sec->output_section->vma);
11957 obfd = htab->params->stub_bfd;
11958 is_tga = (is_plt
11959 && stub_entry->h != NULL
11960 && is_tls_get_addr (&stub_entry->h->elf, htab)
11961 && htab->params->tls_get_addr_opt);
11962 if (is_tga)
11963 {
11964 p = build_tls_get_addr_head (htab, stub_entry, p);
11965 off += p - loc;
11966 }
11967 if (stub_entry->type.r2save)
11968 {
11969 off += 4;
11970 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11971 p += 4;
11972 }
11973 if (is_plt)
11974 {
11975 targ = stub_entry->plt_ent->plt.offset & ~1;
11976 if (targ >= (bfd_vma) -2)
11977 abort ();
11978
11979 plt = htab->elf.splt;
11980 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11981 {
11982 if (stub_entry->symtype == STT_GNU_IFUNC)
11983 plt = htab->elf.iplt;
11984 else
11985 plt = htab->pltlocal;
11986 }
11987 targ += plt->output_offset + plt->output_section->vma;
11988 }
11989 else
11990 targ = (stub_entry->target_value
11991 + stub_entry->target_section->output_offset
11992 + stub_entry->target_section->output_section->vma);
11993 odd = off & 4;
11994 off = targ - off;
11995
11996 relp = p;
11997 num_rel = 0;
11998 if (stub_entry->type.sub == ppc_stub_notoc)
11999 p = build_power10_offset (obfd, p, off, odd, is_plt);
12000 else
12001 {
12002 if (htab->glink_eh_frame != NULL
12003 && htab->glink_eh_frame->size != 0)
12004 {
12005 bfd_byte *base, *eh;
12006 unsigned int lr_used, delta;
12007
12008 base = (htab->glink_eh_frame->contents
12009 + stub_entry->group->eh_base + 17);
12010 eh = base + stub_entry->group->eh_size;
12011 lr_used = stub_entry->stub_offset + (p - loc) + 8;
12012 delta = lr_used - stub_entry->group->lr_restore;
12013 stub_entry->group->lr_restore = lr_used + 8;
12014 eh = eh_advance (htab->elf.dynobj, eh, delta);
12015 *eh++ = DW_CFA_register;
12016 *eh++ = 65;
12017 *eh++ = 12;
12018 *eh++ = DW_CFA_advance_loc + 2;
12019 *eh++ = DW_CFA_restore_extended;
12020 *eh++ = 65;
12021 stub_entry->group->eh_size = eh - base;
12022 }
12023
12024 /* The notoc stubs calculate their target (either a PLT entry or
12025 the global entry point of a function) relative to the PC
12026 returned by the "bcl" two instructions past the start of the
12027 sequence emitted by build_offset. The offset is therefore 8
12028 less than calculated from the start of the sequence. */
12029 off -= 8;
12030 p = build_offset (obfd, p, off, is_plt);
12031 }
12032
12033 if (stub_entry->type.main == ppc_stub_long_branch)
12034 {
12035 bfd_vma from;
12036 num_rel = 1;
12037 from = (stub_entry->stub_offset
12038 + stub_entry->group->stub_sec->output_offset
12039 + stub_entry->group->stub_sec->output_section->vma
12040 + (p - loc));
12041 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12042 }
12043 else
12044 {
12045 bfd_put_32 (obfd, MTCTR_R12, p);
12046 p += 4;
12047 bfd_put_32 (obfd, BCTR, p);
12048 }
12049 p += 4;
12050
12051 if (is_tga)
12052 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12053
12054 if (info->emitrelocations)
12055 {
12056 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12057 if (stub_entry->type.sub == ppc_stub_notoc)
12058 num_rel += num_relocs_for_power10_offset (off, odd);
12059 else
12060 {
12061 num_rel += num_relocs_for_offset (off);
12062 roff += 16;
12063 }
12064 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12065 if (r == NULL)
12066 return false;
12067 if (stub_entry->type.sub == ppc_stub_notoc)
12068 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12069 else
12070 r = emit_relocs_for_offset (info, r, roff, targ, off);
12071 if (stub_entry->type.main == ppc_stub_long_branch)
12072 {
12073 ++r;
12074 roff = p - 4 - stub_entry->group->stub_sec->contents;
12075 r->r_offset = roff;
12076 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12077 r->r_addend = targ;
12078 if (stub_entry->h != NULL
12079 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12080 return false;
12081 }
12082 }
12083 }
12084 else if (stub_entry->type.main == ppc_stub_plt_call)
12085 {
12086 if (stub_entry->h != NULL
12087 && stub_entry->h->is_func_descriptor
12088 && stub_entry->h->oh != NULL)
12089 {
12090 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12091
12092 /* If the old-ABI "dot-symbol" is undefined make it weak so
12093 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12094 if (fh->elf.root.type == bfd_link_hash_undefined
12095 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12096 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12097 fh->elf.root.type = bfd_link_hash_undefweak;
12098 }
12099
12100 /* Now build the stub. */
12101 targ = stub_entry->plt_ent->plt.offset & ~1;
12102 if (targ >= (bfd_vma) -2)
12103 abort ();
12104
12105 plt = htab->elf.splt;
12106 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12107 {
12108 if (stub_entry->symtype == STT_GNU_IFUNC)
12109 plt = htab->elf.iplt;
12110 else
12111 plt = htab->pltlocal;
12112 }
12113 targ += plt->output_offset + plt->output_section->vma;
12114
12115 off = (elf_gp (info->output_bfd)
12116 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12117 off = targ - off;
12118
12119 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12120 {
12121 info->callbacks->einfo
12122 /* xgettext:c-format */
12123 (_("%P: linkage table error against `%pT'\n"),
12124 stub_entry->h != NULL
12125 ? stub_entry->h->elf.root.root.string
12126 : "<local sym>");
12127 bfd_set_error (bfd_error_bad_value);
12128 htab->stub_error = true;
12129 return false;
12130 }
12131
12132 r = NULL;
12133 if (info->emitrelocations)
12134 {
12135 r = get_relocs (stub_entry->group->stub_sec,
12136 ((PPC_HA (off) != 0)
12137 + (htab->opd_abi
12138 ? 2 + (htab->params->plt_static_chain
12139 && PPC_HA (off + 16) == PPC_HA (off))
12140 : 1)));
12141 if (r == NULL)
12142 return false;
12143 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12144 if (bfd_big_endian (info->output_bfd))
12145 r[0].r_offset += 2;
12146 r[0].r_addend = targ;
12147 }
12148 p = loc;
12149 obfd = htab->params->stub_bfd;
12150 is_tga = (stub_entry->h != NULL
12151 && is_tls_get_addr (&stub_entry->h->elf, htab)
12152 && htab->params->tls_get_addr_opt);
12153 if (is_tga)
12154 {
12155 p = build_tls_get_addr_head (htab, stub_entry, p);
12156 if (r != NULL)
12157 r[0].r_offset += p - loc;
12158 }
12159 p = build_plt_stub (htab, stub_entry, p, off, r);
12160 if (is_tga)
12161 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12162 }
12163 else if (stub_entry->type.main == ppc_stub_save_res)
12164 return true;
12165 else
12166 {
12167 BFD_FAIL ();
12168 return false;
12169 }
12170
12171 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12172
12173 if (htab->params->emit_stub_syms)
12174 {
12175 struct elf_link_hash_entry *h;
12176 size_t len1, len2;
12177 char *name;
12178 const char *const stub_str[] = { "long_branch",
12179 "plt_branch",
12180 "plt_call" };
12181
12182 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12183 len2 = strlen (stub_entry->root.string);
12184 name = bfd_malloc (len1 + len2 + 2);
12185 if (name == NULL)
12186 return false;
12187 memcpy (name, stub_entry->root.string, 9);
12188 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12189 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12190 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12191 if (h == NULL)
12192 return false;
12193 if (h->root.type == bfd_link_hash_new)
12194 {
12195 h->root.type = bfd_link_hash_defined;
12196 h->root.u.def.section = stub_entry->group->stub_sec;
12197 h->root.u.def.value = stub_entry->stub_offset;
12198 h->ref_regular = 1;
12199 h->def_regular = 1;
12200 h->ref_regular_nonweak = 1;
12201 h->forced_local = 1;
12202 h->non_elf = 0;
12203 h->root.linker_def = 1;
12204 }
12205 }
12206
12207 return true;
12208 }
12209
12210 /* As above, but don't actually build the stub. Just bump offset so
12211 we know stub section sizes, and select plt_branch stubs where
12212 long_branch stubs won't do. */
12213
12214 static bool
12215 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12216 {
12217 struct ppc_stub_hash_entry *stub_entry;
12218 struct bfd_link_info *info;
12219 struct ppc_link_hash_table *htab;
12220 asection *plt;
12221 bfd_vma targ, off, r2off;
12222 unsigned int size, pad, extra, lr_used, delta, odd;
12223 bfd_vma stub_offset;
12224
12225 /* Massage our args to the form they really have. */
12226 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12227 info = in_arg;
12228
12229 htab = ppc_hash_table (info);
12230 if (htab == NULL)
12231 return false;
12232
12233 /* Fail if the target section could not be assigned to an output
12234 section. The user should fix his linker script. */
12235 if (stub_entry->target_section != NULL
12236 && stub_entry->target_section->output_section == NULL
12237 && info->non_contiguous_regions)
12238 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12239 "Retry without --enable-non-contiguous-regions.\n"),
12240 stub_entry->target_section);
12241
12242 /* Same for the group. */
12243 if (stub_entry->group->stub_sec != NULL
12244 && stub_entry->group->stub_sec->output_section == NULL
12245 && info->non_contiguous_regions)
12246 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12247 "Retry without --enable-non-contiguous-regions.\n"),
12248 stub_entry->group->stub_sec);
12249
12250 /* Make a note of the offset within the stubs for this entry. */
12251 stub_offset = stub_entry->group->stub_sec->size;
12252 if (htab->stub_iteration > STUB_SHRINK_ITER
12253 && stub_entry->stub_offset > stub_offset)
12254 stub_offset = stub_entry->stub_offset;
12255 stub_entry->id = ++htab->stub_id;
12256
12257 if (stub_entry->h != NULL
12258 && stub_entry->h->save_res
12259 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12260 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12261 {
12262 /* Don't make stubs to out-of-line register save/restore
12263 functions. Instead, emit copies of the functions. */
12264 stub_entry->group->needs_save_res = 1;
12265 stub_entry->type.main = ppc_stub_save_res;
12266 stub_entry->type.sub = ppc_stub_toc;
12267 stub_entry->type.r2save = 0;
12268 return true;
12269 }
12270
12271 if (stub_entry->type.main == ppc_stub_plt_branch)
12272 {
12273 /* Reset the stub type from the plt branch variant in case we now
12274 can reach with a shorter stub. */
12275 stub_entry->type.main = ppc_stub_long_branch;
12276 }
12277
12278 if (stub_entry->type.main == ppc_stub_long_branch
12279 && stub_entry->type.sub == ppc_stub_toc)
12280 {
12281 targ = (stub_entry->target_value
12282 + stub_entry->target_section->output_offset
12283 + stub_entry->target_section->output_section->vma);
12284 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12285 off = (stub_offset
12286 + stub_entry->group->stub_sec->output_offset
12287 + stub_entry->group->stub_sec->output_section->vma);
12288
12289 size = 4;
12290 r2off = 0;
12291 if (stub_entry->type.r2save)
12292 {
12293 r2off = get_r2off (info, stub_entry);
12294 if (r2off == (bfd_vma) -1)
12295 {
12296 htab->stub_error = true;
12297 return false;
12298 }
12299 size = 8;
12300 if (PPC_HA (r2off) != 0)
12301 size += 4;
12302 if (PPC_LO (r2off) != 0)
12303 size += 4;
12304 off += size - 4;
12305 }
12306 off = targ - off;
12307
12308 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12309 Do the same for -R objects without function descriptors. */
12310 if ((stub_entry->type.r2save
12311 && r2off == 0
12312 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12313 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12314 {
12315 struct ppc_branch_hash_entry *br_entry;
12316
12317 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12318 stub_entry->root.string + 9,
12319 true, false);
12320 if (br_entry == NULL)
12321 {
12322 _bfd_error_handler (_("can't build branch stub `%s'"),
12323 stub_entry->root.string);
12324 htab->stub_error = true;
12325 return false;
12326 }
12327
12328 if (br_entry->iter != htab->stub_iteration)
12329 {
12330 br_entry->iter = htab->stub_iteration;
12331 br_entry->offset = htab->brlt->size;
12332 htab->brlt->size += 8;
12333
12334 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12335 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12336 else if (info->emitrelocations)
12337 {
12338 htab->brlt->reloc_count += 1;
12339 htab->brlt->flags |= SEC_RELOC;
12340 }
12341 }
12342
12343 targ = (br_entry->offset
12344 + htab->brlt->output_offset
12345 + htab->brlt->output_section->vma);
12346 off = (elf_gp (info->output_bfd)
12347 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12348 off = targ - off;
12349
12350 if (info->emitrelocations)
12351 {
12352 stub_entry->group->stub_sec->reloc_count
12353 += 1 + (PPC_HA (off) != 0);
12354 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12355 }
12356
12357 stub_entry->type.main = ppc_stub_plt_branch;
12358 if (!stub_entry->type.r2save)
12359 {
12360 size = 12;
12361 if (PPC_HA (off) != 0)
12362 size = 16;
12363 }
12364 else
12365 {
12366 size = 16;
12367 if (PPC_HA (off) != 0)
12368 size += 4;
12369
12370 if (PPC_HA (r2off) != 0)
12371 size += 4;
12372 if (PPC_LO (r2off) != 0)
12373 size += 4;
12374 }
12375 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12376 stub_offset += pad;
12377 }
12378 else if (info->emitrelocations)
12379 {
12380 stub_entry->group->stub_sec->reloc_count += 1;
12381 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12382 }
12383 }
12384 else if (stub_entry->type.main == ppc_stub_long_branch)
12385 {
12386 off = (stub_offset
12387 + stub_entry->group->stub_sec->output_offset
12388 + stub_entry->group->stub_sec->output_section->vma);
12389 size = 0;
12390 if (stub_entry->type.r2save)
12391 size = 4;
12392 off += size;
12393 targ = (stub_entry->target_value
12394 + stub_entry->target_section->output_offset
12395 + stub_entry->target_section->output_section->vma);
12396 odd = off & 4;
12397 off = targ - off;
12398
12399 if (stub_entry->type.sub == ppc_stub_notoc)
12400 extra = size_power10_offset (off, odd);
12401 else
12402 extra = size_offset (off - 8);
12403 /* Include branch insn plus those in the offset sequence. */
12404 size += 4 + extra;
12405
12406 /* If the branch can't reach, use a plt_branch.
12407 The branch insn is at the end, or "extra" bytes along. So
12408 its offset will be "extra" bytes less that that already
12409 calculated. */
12410 if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12411 {
12412 stub_entry->type.main = ppc_stub_plt_branch;
12413 size += 4;
12414 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12415 if (pad != 0)
12416 {
12417 stub_offset += pad;
12418 off -= pad;
12419 odd ^= pad & 4;
12420 size -= extra;
12421 if (stub_entry->type.sub == ppc_stub_notoc)
12422 extra = size_power10_offset (off, odd);
12423 else
12424 extra = size_offset (off - 8);
12425 size += extra;
12426 }
12427 }
12428 else if (info->emitrelocations)
12429 stub_entry->group->stub_sec->reloc_count +=1;
12430
12431 if (info->emitrelocations)
12432 {
12433 unsigned int num_rel;
12434 if (stub_entry->type.sub == ppc_stub_notoc)
12435 num_rel = num_relocs_for_power10_offset (off, odd);
12436 else
12437 num_rel = num_relocs_for_offset (off - 8);
12438 stub_entry->group->stub_sec->reloc_count += num_rel;
12439 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12440 }
12441
12442 if (stub_entry->type.sub != ppc_stub_notoc)
12443 {
12444 /* After the bcl, lr has been modified so we need to emit
12445 .eh_frame info saying the return address is in r12. */
12446 lr_used = stub_offset + 8;
12447 if (stub_entry->type.r2save)
12448 lr_used += 4;
12449 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12450 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12451 DW_CFA_restore_extended 65. */
12452 delta = lr_used - stub_entry->group->lr_restore;
12453 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12454 stub_entry->group->lr_restore = lr_used + 8;
12455 }
12456 }
12457 else if (stub_entry->type.sub >= ppc_stub_notoc)
12458 {
12459 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12460 lr_used = 0;
12461 if (stub_entry->h != NULL
12462 && is_tls_get_addr (&stub_entry->h->elf, htab)
12463 && htab->params->tls_get_addr_opt)
12464 {
12465 lr_used += 7 * 4;
12466 if (!htab->params->no_tls_get_addr_regsave)
12467 lr_used += 11 * 4;
12468 else if (stub_entry->type.r2save)
12469 lr_used += 2 * 4;
12470 }
12471 if (stub_entry->type.r2save)
12472 lr_used += 4;
12473 targ = stub_entry->plt_ent->plt.offset & ~1;
12474 if (targ >= (bfd_vma) -2)
12475 abort ();
12476
12477 plt = htab->elf.splt;
12478 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12479 {
12480 if (stub_entry->symtype == STT_GNU_IFUNC)
12481 plt = htab->elf.iplt;
12482 else
12483 plt = htab->pltlocal;
12484 }
12485 targ += plt->output_offset + plt->output_section->vma;
12486 off = (stub_offset
12487 + stub_entry->group->stub_sec->output_offset
12488 + stub_entry->group->stub_sec->output_section->vma
12489 + lr_used);
12490 odd = off & 4;
12491 off = targ - off;
12492
12493 size = plt_stub_size (htab, stub_entry, off, odd);
12494 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12495 if (pad != 0)
12496 {
12497 stub_offset += pad;
12498 off -= pad;
12499 odd ^= pad & 4;
12500 size = plt_stub_size (htab, stub_entry, off, odd);
12501 }
12502
12503 if (info->emitrelocations)
12504 {
12505 unsigned int num_rel;
12506 if (stub_entry->type.sub == ppc_stub_notoc)
12507 num_rel = num_relocs_for_power10_offset (off, odd);
12508 else
12509 num_rel = num_relocs_for_offset (off - 8);
12510 stub_entry->group->stub_sec->reloc_count += num_rel;
12511 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12512 }
12513
12514 if (stub_entry->type.sub != ppc_stub_notoc)
12515 {
12516 /* After the bcl, lr has been modified so we need to emit
12517 .eh_frame info saying the return address is in r12. */
12518 lr_used += stub_offset + 8;
12519 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12520 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12521 DW_CFA_restore_extended 65. */
12522 delta = lr_used - stub_entry->group->lr_restore;
12523 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12524 stub_entry->group->lr_restore = lr_used + 8;
12525 }
12526 if (stub_entry->h != NULL
12527 && is_tls_get_addr (&stub_entry->h->elf, htab)
12528 && htab->params->tls_get_addr_opt)
12529 {
12530 if (!htab->params->no_tls_get_addr_regsave)
12531 {
12532 unsigned int cfa_updt = stub_offset + 18 * 4;
12533 delta = cfa_updt - stub_entry->group->lr_restore;
12534 stub_entry->group->eh_size += eh_advance_size (delta);
12535 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12536 stub_entry->group->lr_restore = stub_offset + size - 4;
12537 }
12538 else if (stub_entry->type.r2save)
12539 {
12540 lr_used = stub_offset + size - 20;
12541 delta = lr_used - stub_entry->group->lr_restore;
12542 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12543 stub_entry->group->lr_restore = stub_offset + size - 4;
12544 }
12545 }
12546 }
12547 else if (stub_entry->type.main == ppc_stub_plt_call)
12548 {
12549 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12550 if (targ >= (bfd_vma) -2)
12551 abort ();
12552 plt = htab->elf.splt;
12553 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12554 {
12555 if (stub_entry->symtype == STT_GNU_IFUNC)
12556 plt = htab->elf.iplt;
12557 else
12558 plt = htab->pltlocal;
12559 }
12560 targ += plt->output_offset + plt->output_section->vma;
12561
12562 off = (elf_gp (info->output_bfd)
12563 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12564 off = targ - off;
12565
12566 size = plt_stub_size (htab, stub_entry, off, 0);
12567 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12568 stub_offset += pad;
12569
12570 if (info->emitrelocations)
12571 {
12572 stub_entry->group->stub_sec->reloc_count
12573 += ((PPC_HA (off) != 0)
12574 + (htab->opd_abi
12575 ? 2 + (htab->params->plt_static_chain
12576 && PPC_HA (off + 16) == PPC_HA (off))
12577 : 1));
12578 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12579 }
12580
12581 if (stub_entry->h != NULL
12582 && is_tls_get_addr (&stub_entry->h->elf, htab)
12583 && htab->params->tls_get_addr_opt
12584 && stub_entry->type.r2save)
12585 {
12586 if (!htab->params->no_tls_get_addr_regsave)
12587 {
12588 /* Adjustments to r1 need to be described. */
12589 unsigned int cfa_updt = stub_offset + 18 * 4;
12590 delta = cfa_updt - stub_entry->group->lr_restore;
12591 stub_entry->group->eh_size += eh_advance_size (delta);
12592 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12593 }
12594 else
12595 {
12596 lr_used = stub_offset + size - 20;
12597 /* The eh_frame info will consist of a DW_CFA_advance_loc
12598 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12599 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12600 delta = lr_used - stub_entry->group->lr_restore;
12601 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12602 }
12603 stub_entry->group->lr_restore = stub_offset + size - 4;
12604 }
12605 }
12606 else
12607 {
12608 BFD_FAIL ();
12609 return false;
12610 }
12611
12612 if (stub_entry->stub_offset != stub_offset)
12613 htab->stub_changed = true;
12614 stub_entry->stub_offset = stub_offset;
12615 stub_entry->group->stub_sec->size = stub_offset + size;
12616 return true;
12617 }
12618
12619 /* Set up various things so that we can make a list of input sections
12620 for each output section included in the link. Returns -1 on error,
12621 0 when no stubs will be needed, and 1 on success. */
12622
12623 int
12624 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12625 {
12626 unsigned int id;
12627 size_t amt;
12628 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12629
12630 if (htab == NULL)
12631 return -1;
12632
12633 htab->sec_info_arr_size = _bfd_section_id;
12634 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12635 htab->sec_info = bfd_zmalloc (amt);
12636 if (htab->sec_info == NULL)
12637 return -1;
12638
12639 /* Set toc_off for com, und, abs and ind sections. */
12640 for (id = 0; id < 3; id++)
12641 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12642
12643 return 1;
12644 }
12645
12646 /* Set up for first pass at multitoc partitioning. */
12647
12648 void
12649 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12650 {
12651 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12652
12653 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12654 htab->toc_bfd = NULL;
12655 htab->toc_first_sec = NULL;
12656 }
12657
12658 /* The linker repeatedly calls this function for each TOC input section
12659 and linker generated GOT section. Group input bfds such that the toc
12660 within a group is less than 64k in size. */
12661
12662 bool
12663 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12664 {
12665 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12666 bfd_vma addr, off, limit;
12667
12668 if (htab == NULL)
12669 return false;
12670
12671 if (!htab->second_toc_pass)
12672 {
12673 /* Keep track of the first .toc or .got section for this input bfd. */
12674 bool new_bfd = htab->toc_bfd != isec->owner;
12675
12676 if (new_bfd)
12677 {
12678 htab->toc_bfd = isec->owner;
12679 htab->toc_first_sec = isec;
12680 }
12681
12682 addr = isec->output_offset + isec->output_section->vma;
12683 off = addr - htab->toc_curr;
12684 limit = 0x80008000;
12685 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12686 limit = 0x10000;
12687 if (off + isec->size > limit)
12688 {
12689 addr = (htab->toc_first_sec->output_offset
12690 + htab->toc_first_sec->output_section->vma);
12691 htab->toc_curr = addr;
12692 htab->toc_curr &= -TOC_BASE_ALIGN;
12693 }
12694
12695 /* toc_curr is the base address of this toc group. Set elf_gp
12696 for the input section to be the offset relative to the
12697 output toc base plus 0x8000. Making the input elf_gp an
12698 offset allows us to move the toc as a whole without
12699 recalculating input elf_gp. */
12700 off = htab->toc_curr - elf_gp (info->output_bfd);
12701 off += TOC_BASE_OFF;
12702
12703 /* Die if someone uses a linker script that doesn't keep input
12704 file .toc and .got together. */
12705 if (new_bfd
12706 && elf_gp (isec->owner) != 0
12707 && elf_gp (isec->owner) != off)
12708 return false;
12709
12710 elf_gp (isec->owner) = off;
12711 return true;
12712 }
12713
12714 /* During the second pass toc_first_sec points to the start of
12715 a toc group, and toc_curr is used to track the old elf_gp.
12716 We use toc_bfd to ensure we only look at each bfd once. */
12717 if (htab->toc_bfd == isec->owner)
12718 return true;
12719 htab->toc_bfd = isec->owner;
12720
12721 if (htab->toc_first_sec == NULL
12722 || htab->toc_curr != elf_gp (isec->owner))
12723 {
12724 htab->toc_curr = elf_gp (isec->owner);
12725 htab->toc_first_sec = isec;
12726 }
12727 addr = (htab->toc_first_sec->output_offset
12728 + htab->toc_first_sec->output_section->vma);
12729 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12730 elf_gp (isec->owner) = off;
12731
12732 return true;
12733 }
12734
12735 /* Called via elf_link_hash_traverse to merge GOT entries for global
12736 symbol H. */
12737
12738 static bool
12739 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12740 {
12741 if (h->root.type == bfd_link_hash_indirect)
12742 return true;
12743
12744 merge_got_entries (&h->got.glist);
12745
12746 return true;
12747 }
12748
12749 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12750 symbol H. */
12751
12752 static bool
12753 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12754 {
12755 struct got_entry *gent;
12756
12757 if (h->root.type == bfd_link_hash_indirect)
12758 return true;
12759
12760 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12761 if (!gent->is_indirect)
12762 allocate_got (h, (struct bfd_link_info *) inf, gent);
12763 return true;
12764 }
12765
12766 /* Called on the first multitoc pass after the last call to
12767 ppc64_elf_next_toc_section. This function removes duplicate GOT
12768 entries. */
12769
12770 bool
12771 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12772 {
12773 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12774 struct bfd *ibfd, *ibfd2;
12775 bool done_something;
12776
12777 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12778
12779 if (!htab->do_multi_toc)
12780 return false;
12781
12782 /* Merge global sym got entries within a toc group. */
12783 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12784
12785 /* And tlsld_got. */
12786 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12787 {
12788 struct got_entry *ent, *ent2;
12789
12790 if (!is_ppc64_elf (ibfd))
12791 continue;
12792
12793 ent = ppc64_tlsld_got (ibfd);
12794 if (!ent->is_indirect
12795 && ent->got.offset != (bfd_vma) -1)
12796 {
12797 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12798 {
12799 if (!is_ppc64_elf (ibfd2))
12800 continue;
12801
12802 ent2 = ppc64_tlsld_got (ibfd2);
12803 if (!ent2->is_indirect
12804 && ent2->got.offset != (bfd_vma) -1
12805 && elf_gp (ibfd2) == elf_gp (ibfd))
12806 {
12807 ent2->is_indirect = true;
12808 ent2->got.ent = ent;
12809 }
12810 }
12811 }
12812 }
12813
12814 /* Zap sizes of got sections. */
12815 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12816 htab->elf.irelplt->size -= htab->got_reli_size;
12817 htab->got_reli_size = 0;
12818
12819 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12820 {
12821 asection *got, *relgot;
12822
12823 if (!is_ppc64_elf (ibfd))
12824 continue;
12825
12826 got = ppc64_elf_tdata (ibfd)->got;
12827 if (got != NULL)
12828 {
12829 got->rawsize = got->size;
12830 got->size = 0;
12831 relgot = ppc64_elf_tdata (ibfd)->relgot;
12832 relgot->rawsize = relgot->size;
12833 relgot->size = 0;
12834 }
12835 }
12836
12837 /* Now reallocate the got, local syms first. We don't need to
12838 allocate section contents again since we never increase size. */
12839 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12840 {
12841 struct got_entry **lgot_ents;
12842 struct got_entry **end_lgot_ents;
12843 struct plt_entry **local_plt;
12844 struct plt_entry **end_local_plt;
12845 unsigned char *lgot_masks;
12846 bfd_size_type locsymcount;
12847 Elf_Internal_Shdr *symtab_hdr;
12848 asection *s;
12849 Elf_Internal_Sym *local_syms;
12850 Elf_Internal_Sym *isym;
12851
12852 if (!is_ppc64_elf (ibfd))
12853 continue;
12854
12855 lgot_ents = elf_local_got_ents (ibfd);
12856 if (!lgot_ents)
12857 continue;
12858
12859 symtab_hdr = &elf_symtab_hdr (ibfd);
12860 locsymcount = symtab_hdr->sh_info;
12861 end_lgot_ents = lgot_ents + locsymcount;
12862 local_plt = (struct plt_entry **) end_lgot_ents;
12863 end_local_plt = local_plt + locsymcount;
12864 lgot_masks = (unsigned char *) end_local_plt;
12865 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12866 if (local_syms == NULL && locsymcount != 0)
12867 {
12868 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12869 0, NULL, NULL, NULL);
12870 if (local_syms == NULL)
12871 return false;
12872 }
12873 s = ppc64_elf_tdata (ibfd)->got;
12874 for (isym = local_syms;
12875 lgot_ents < end_lgot_ents;
12876 ++lgot_ents, ++lgot_masks, isym++)
12877 {
12878 struct got_entry *ent;
12879
12880 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12881 {
12882 unsigned int ent_size = 8;
12883 unsigned int rel_size = sizeof (Elf64_External_Rela);
12884
12885 ent->got.offset = s->size;
12886 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12887 {
12888 ent_size *= 2;
12889 rel_size *= 2;
12890 }
12891 s->size += ent_size;
12892 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12893 {
12894 htab->elf.irelplt->size += rel_size;
12895 htab->got_reli_size += rel_size;
12896 }
12897 else if (bfd_link_pic (info)
12898 && (ent->tls_type == 0
12899 ? !info->enable_dt_relr
12900 : !bfd_link_executable (info))
12901 && isym->st_shndx != SHN_ABS)
12902 {
12903 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12904 srel->size += rel_size;
12905 }
12906 }
12907 }
12908 }
12909
12910 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12911
12912 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12913 {
12914 struct got_entry *ent;
12915
12916 if (!is_ppc64_elf (ibfd))
12917 continue;
12918
12919 ent = ppc64_tlsld_got (ibfd);
12920 if (!ent->is_indirect
12921 && ent->got.offset != (bfd_vma) -1)
12922 {
12923 asection *s = ppc64_elf_tdata (ibfd)->got;
12924 ent->got.offset = s->size;
12925 s->size += 16;
12926 if (bfd_link_dll (info))
12927 {
12928 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12929 srel->size += sizeof (Elf64_External_Rela);
12930 }
12931 }
12932 }
12933
12934 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12935 if (!done_something)
12936 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12937 {
12938 asection *got;
12939
12940 if (!is_ppc64_elf (ibfd))
12941 continue;
12942
12943 got = ppc64_elf_tdata (ibfd)->got;
12944 if (got != NULL)
12945 {
12946 done_something = got->rawsize != got->size;
12947 if (done_something)
12948 break;
12949 }
12950 }
12951
12952 if (done_something)
12953 (*htab->params->layout_sections_again) ();
12954
12955 /* Set up for second pass over toc sections to recalculate elf_gp
12956 on input sections. */
12957 htab->toc_bfd = NULL;
12958 htab->toc_first_sec = NULL;
12959 htab->second_toc_pass = true;
12960 return done_something;
12961 }
12962
12963 /* Called after second pass of multitoc partitioning. */
12964
12965 void
12966 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12967 {
12968 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12969
12970 /* After the second pass, toc_curr tracks the TOC offset used
12971 for code sections below in ppc64_elf_next_input_section. */
12972 htab->toc_curr = TOC_BASE_OFF;
12973 }
12974
12975 /* No toc references were found in ISEC. If the code in ISEC makes no
12976 calls, then there's no need to use toc adjusting stubs when branching
12977 into ISEC. Actually, indirect calls from ISEC are OK as they will
12978 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12979 needed, and 2 if a cyclical call-graph was found but no other reason
12980 for a stub was detected. If called from the top level, a return of
12981 2 means the same as a return of 0. */
12982
12983 static int
12984 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12985 {
12986 int ret;
12987
12988 /* Mark this section as checked. */
12989 isec->call_check_done = 1;
12990
12991 /* We know none of our code bearing sections will need toc stubs. */
12992 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12993 return 0;
12994
12995 if (isec->size == 0)
12996 return 0;
12997
12998 if (isec->output_section == NULL)
12999 return 0;
13000
13001 ret = 0;
13002 if (isec->reloc_count != 0)
13003 {
13004 Elf_Internal_Rela *relstart, *rel;
13005 Elf_Internal_Sym *local_syms;
13006 struct ppc_link_hash_table *htab;
13007
13008 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13009 info->keep_memory);
13010 if (relstart == NULL)
13011 return -1;
13012
13013 /* Look for branches to outside of this section. */
13014 local_syms = NULL;
13015 htab = ppc_hash_table (info);
13016 if (htab == NULL)
13017 return -1;
13018
13019 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
13020 {
13021 enum elf_ppc64_reloc_type r_type;
13022 unsigned long r_symndx;
13023 struct elf_link_hash_entry *h;
13024 struct ppc_link_hash_entry *eh;
13025 Elf_Internal_Sym *sym;
13026 asection *sym_sec;
13027 struct _opd_sec_data *opd;
13028 bfd_vma sym_value;
13029 bfd_vma dest;
13030
13031 r_type = ELF64_R_TYPE (rel->r_info);
13032 if (r_type != R_PPC64_REL24
13033 && r_type != R_PPC64_REL24_NOTOC
13034 && r_type != R_PPC64_REL24_P9NOTOC
13035 && r_type != R_PPC64_REL14
13036 && r_type != R_PPC64_REL14_BRTAKEN
13037 && r_type != R_PPC64_REL14_BRNTAKEN
13038 && r_type != R_PPC64_PLTCALL
13039 && r_type != R_PPC64_PLTCALL_NOTOC)
13040 continue;
13041
13042 r_symndx = ELF64_R_SYM (rel->r_info);
13043 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13044 isec->owner))
13045 {
13046 ret = -1;
13047 break;
13048 }
13049
13050 /* Calls to dynamic lib functions go through a plt call stub
13051 that uses r2. */
13052 eh = ppc_elf_hash_entry (h);
13053 if (eh != NULL
13054 && (eh->elf.plt.plist != NULL
13055 || (eh->oh != NULL
13056 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13057 {
13058 ret = 1;
13059 break;
13060 }
13061
13062 if (sym_sec == NULL)
13063 /* Ignore other undefined symbols. */
13064 continue;
13065
13066 /* Assume branches to other sections not included in the
13067 link need stubs too, to cover -R and absolute syms. */
13068 if (sym_sec->output_section == NULL)
13069 {
13070 ret = 1;
13071 break;
13072 }
13073
13074 if (h == NULL)
13075 sym_value = sym->st_value;
13076 else
13077 {
13078 if (h->root.type != bfd_link_hash_defined
13079 && h->root.type != bfd_link_hash_defweak)
13080 abort ();
13081 sym_value = h->root.u.def.value;
13082 }
13083 sym_value += rel->r_addend;
13084
13085 /* If this branch reloc uses an opd sym, find the code section. */
13086 opd = get_opd_info (sym_sec);
13087 if (opd != NULL)
13088 {
13089 if (h == NULL && opd->adjust != NULL)
13090 {
13091 long adjust;
13092
13093 adjust = opd->adjust[OPD_NDX (sym_value)];
13094 if (adjust == -1)
13095 /* Assume deleted functions won't ever be called. */
13096 continue;
13097 sym_value += adjust;
13098 }
13099
13100 dest = opd_entry_value (sym_sec, sym_value,
13101 &sym_sec, NULL, false);
13102 if (dest == (bfd_vma) -1)
13103 continue;
13104 }
13105 else
13106 dest = (sym_value
13107 + sym_sec->output_offset
13108 + sym_sec->output_section->vma);
13109
13110 /* Ignore branch to self. */
13111 if (sym_sec == isec)
13112 continue;
13113
13114 /* If the called function uses the toc, we need a stub. */
13115 if (sym_sec->has_toc_reloc
13116 || sym_sec->makes_toc_func_call)
13117 {
13118 ret = 1;
13119 break;
13120 }
13121
13122 /* Assume any branch that needs a long branch stub might in fact
13123 need a plt_branch stub. A plt_branch stub uses r2. */
13124 else if (dest - (isec->output_offset
13125 + isec->output_section->vma
13126 + rel->r_offset) + (1 << 25)
13127 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13128 ? h->other
13129 : sym->st_other))
13130 {
13131 ret = 1;
13132 break;
13133 }
13134
13135 /* If calling back to a section in the process of being
13136 tested, we can't say for sure that no toc adjusting stubs
13137 are needed, so don't return zero. */
13138 else if (sym_sec->call_check_in_progress)
13139 ret = 2;
13140
13141 /* Branches to another section that itself doesn't have any TOC
13142 references are OK. Recursively call ourselves to check. */
13143 else if (!sym_sec->call_check_done)
13144 {
13145 int recur;
13146
13147 /* Mark current section as indeterminate, so that other
13148 sections that call back to current won't be marked as
13149 known. */
13150 isec->call_check_in_progress = 1;
13151 recur = toc_adjusting_stub_needed (info, sym_sec);
13152 isec->call_check_in_progress = 0;
13153
13154 if (recur != 0)
13155 {
13156 ret = recur;
13157 if (recur != 2)
13158 break;
13159 }
13160 }
13161 }
13162
13163 if (elf_symtab_hdr (isec->owner).contents
13164 != (unsigned char *) local_syms)
13165 free (local_syms);
13166 if (elf_section_data (isec)->relocs != relstart)
13167 free (relstart);
13168 }
13169
13170 if ((ret & 1) == 0
13171 && isec->map_head.s != NULL
13172 && (strcmp (isec->output_section->name, ".init") == 0
13173 || strcmp (isec->output_section->name, ".fini") == 0))
13174 {
13175 if (isec->map_head.s->has_toc_reloc
13176 || isec->map_head.s->makes_toc_func_call)
13177 ret = 1;
13178 else if (!isec->map_head.s->call_check_done)
13179 {
13180 int recur;
13181 isec->call_check_in_progress = 1;
13182 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13183 isec->call_check_in_progress = 0;
13184 if (recur != 0)
13185 ret = recur;
13186 }
13187 }
13188
13189 if (ret == 1)
13190 isec->makes_toc_func_call = 1;
13191
13192 return ret;
13193 }
13194
13195 /* The linker repeatedly calls this function for each input section,
13196 in the order that input sections are linked into output sections.
13197 Build lists of input sections to determine groupings between which
13198 we may insert linker stubs. */
13199
13200 bool
13201 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13202 {
13203 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13204
13205 if (htab == NULL)
13206 return false;
13207
13208 if ((isec->output_section->flags & SEC_CODE) != 0
13209 && isec->output_section->id < htab->sec_info_arr_size)
13210 {
13211 /* This happens to make the list in reverse order,
13212 which is what we want. */
13213 htab->sec_info[isec->id].u.list
13214 = htab->sec_info[isec->output_section->id].u.list;
13215 htab->sec_info[isec->output_section->id].u.list = isec;
13216 }
13217
13218 if (htab->multi_toc_needed)
13219 {
13220 /* Analyse sections that aren't already flagged as needing a
13221 valid toc pointer. Exclude .fixup for the linux kernel.
13222 .fixup contains branches, but only back to the function that
13223 hit an exception. */
13224 if (!(isec->has_toc_reloc
13225 || (isec->flags & SEC_CODE) == 0
13226 || strcmp (isec->name, ".fixup") == 0
13227 || isec->call_check_done))
13228 {
13229 if (toc_adjusting_stub_needed (info, isec) < 0)
13230 return false;
13231 }
13232 /* Make all sections use the TOC assigned for this object file.
13233 This will be wrong for pasted sections; We fix that in
13234 check_pasted_section(). */
13235 if (elf_gp (isec->owner) != 0)
13236 htab->toc_curr = elf_gp (isec->owner);
13237 }
13238
13239 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13240 return true;
13241 }
13242
13243 /* Check that all .init and .fini sections use the same toc, if they
13244 have toc relocs. */
13245
13246 static bool
13247 check_pasted_section (struct bfd_link_info *info, const char *name)
13248 {
13249 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13250
13251 if (o != NULL)
13252 {
13253 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13254 bfd_vma toc_off = 0;
13255 asection *i;
13256
13257 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13258 if (i->has_toc_reloc)
13259 {
13260 if (toc_off == 0)
13261 toc_off = htab->sec_info[i->id].toc_off;
13262 else if (toc_off != htab->sec_info[i->id].toc_off)
13263 return false;
13264 }
13265
13266 if (toc_off == 0)
13267 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13268 if (i->makes_toc_func_call)
13269 {
13270 toc_off = htab->sec_info[i->id].toc_off;
13271 break;
13272 }
13273
13274 /* Make sure the whole pasted function uses the same toc offset. */
13275 if (toc_off != 0)
13276 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13277 htab->sec_info[i->id].toc_off = toc_off;
13278 }
13279 return true;
13280 }
13281
13282 bool
13283 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13284 {
13285 bool ret1 = check_pasted_section (info, ".init");
13286 bool ret2 = check_pasted_section (info, ".fini");
13287
13288 return ret1 && ret2;
13289 }
13290
13291 /* See whether we can group stub sections together. Grouping stub
13292 sections may result in fewer stubs. More importantly, we need to
13293 put all .init* and .fini* stubs at the beginning of the .init or
13294 .fini output sections respectively, because glibc splits the
13295 _init and _fini functions into multiple parts. Putting a stub in
13296 the middle of a function is not a good idea. */
13297
13298 static bool
13299 group_sections (struct bfd_link_info *info,
13300 bfd_size_type stub_group_size,
13301 bool stubs_always_before_branch)
13302 {
13303 struct ppc_link_hash_table *htab;
13304 asection *osec;
13305 bool suppress_size_errors;
13306
13307 htab = ppc_hash_table (info);
13308 if (htab == NULL)
13309 return false;
13310
13311 suppress_size_errors = false;
13312 if (stub_group_size == 1)
13313 {
13314 /* Default values. */
13315 if (stubs_always_before_branch)
13316 stub_group_size = 0x1e00000;
13317 else
13318 stub_group_size = 0x1c00000;
13319 suppress_size_errors = true;
13320 }
13321
13322 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13323 {
13324 asection *tail;
13325
13326 if (osec->id >= htab->sec_info_arr_size)
13327 continue;
13328
13329 tail = htab->sec_info[osec->id].u.list;
13330 while (tail != NULL)
13331 {
13332 asection *curr;
13333 asection *prev;
13334 bfd_size_type total;
13335 bool big_sec;
13336 bfd_vma curr_toc;
13337 struct map_stub *group;
13338 bfd_size_type group_size;
13339
13340 curr = tail;
13341 total = tail->size;
13342 group_size = (ppc64_elf_section_data (tail) != NULL
13343 && ppc64_elf_section_data (tail)->has_14bit_branch
13344 ? stub_group_size >> 10 : stub_group_size);
13345
13346 big_sec = total > group_size;
13347 if (big_sec && !suppress_size_errors)
13348 /* xgettext:c-format */
13349 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13350 tail->owner, tail);
13351 curr_toc = htab->sec_info[tail->id].toc_off;
13352
13353 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13354 && ((total += curr->output_offset - prev->output_offset)
13355 < (ppc64_elf_section_data (prev) != NULL
13356 && ppc64_elf_section_data (prev)->has_14bit_branch
13357 ? (group_size = stub_group_size >> 10) : group_size))
13358 && htab->sec_info[prev->id].toc_off == curr_toc)
13359 curr = prev;
13360
13361 /* OK, the size from the start of CURR to the end is less
13362 than group_size and thus can be handled by one stub
13363 section. (or the tail section is itself larger than
13364 group_size, in which case we may be toast.) We should
13365 really be keeping track of the total size of stubs added
13366 here, as stubs contribute to the final output section
13367 size. That's a little tricky, and this way will only
13368 break if stubs added make the total size more than 2^25,
13369 ie. for the default stub_group_size, if stubs total more
13370 than 2097152 bytes, or nearly 75000 plt call stubs. */
13371 group = bfd_alloc (curr->owner, sizeof (*group));
13372 if (group == NULL)
13373 return false;
13374 group->link_sec = curr;
13375 group->stub_sec = NULL;
13376 group->needs_save_res = 0;
13377 group->lr_restore = 0;
13378 group->eh_size = 0;
13379 group->eh_base = 0;
13380 group->next = htab->group;
13381 htab->group = group;
13382 do
13383 {
13384 prev = htab->sec_info[tail->id].u.list;
13385 /* Set up this stub group. */
13386 htab->sec_info[tail->id].u.group = group;
13387 }
13388 while (tail != curr && (tail = prev) != NULL);
13389
13390 /* But wait, there's more! Input sections up to group_size
13391 bytes before the stub section can be handled by it too.
13392 Don't do this if we have a really large section after the
13393 stubs, as adding more stubs increases the chance that
13394 branches may not reach into the stub section. */
13395 if (!stubs_always_before_branch && !big_sec)
13396 {
13397 total = 0;
13398 while (prev != NULL
13399 && ((total += tail->output_offset - prev->output_offset)
13400 < (ppc64_elf_section_data (prev) != NULL
13401 && ppc64_elf_section_data (prev)->has_14bit_branch
13402 ? (group_size = stub_group_size >> 10)
13403 : group_size))
13404 && htab->sec_info[prev->id].toc_off == curr_toc)
13405 {
13406 tail = prev;
13407 prev = htab->sec_info[tail->id].u.list;
13408 htab->sec_info[tail->id].u.group = group;
13409 }
13410 }
13411 tail = prev;
13412 }
13413 }
13414 return true;
13415 }
13416
13417 static const unsigned char glink_eh_frame_cie[] =
13418 {
13419 0, 0, 0, 16, /* length. */
13420 0, 0, 0, 0, /* id. */
13421 1, /* CIE version. */
13422 'z', 'R', 0, /* Augmentation string. */
13423 4, /* Code alignment. */
13424 0x78, /* Data alignment. */
13425 65, /* RA reg. */
13426 1, /* Augmentation size. */
13427 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13428 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13429 };
13430
13431 /* Stripping output sections is normally done before dynamic section
13432 symbols have been allocated. This function is called later, and
13433 handles cases like htab->brlt which is mapped to its own output
13434 section. */
13435
13436 static void
13437 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13438 {
13439 if (isec->size == 0
13440 && isec->output_section->size == 0
13441 && !(isec->output_section->flags & SEC_KEEP)
13442 && !bfd_section_removed_from_list (info->output_bfd,
13443 isec->output_section)
13444 && elf_section_data (isec->output_section)->dynindx == 0)
13445 {
13446 isec->output_section->flags |= SEC_EXCLUDE;
13447 bfd_section_list_remove (info->output_bfd, isec->output_section);
13448 info->output_bfd->section_count--;
13449 }
13450 }
13451
13452 /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13453 the array of such relocs. */
13454
13455 static bool
13456 append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13457 {
13458 if (htab->relr_count >= htab->relr_alloc)
13459 {
13460 if (htab->relr_alloc == 0)
13461 htab->relr_alloc = 4096;
13462 else
13463 htab->relr_alloc *= 2;
13464 htab->relr = bfd_realloc (htab->relr,
13465 htab->relr_alloc * sizeof (*htab->relr));
13466 if (htab->relr == NULL)
13467 return false;
13468 }
13469 htab->relr[htab->relr_count].sec = sec;
13470 htab->relr[htab->relr_count].off = off;
13471 htab->relr_count++;
13472 return true;
13473 }
13474
13475 /* qsort comparator for bfd_vma args. */
13476
13477 static int
13478 compare_relr_address (const void *arg1, const void *arg2)
13479 {
13480 bfd_vma a = *(bfd_vma *) arg1;
13481 bfd_vma b = *(bfd_vma *) arg2;
13482 return a < b ? -1 : a > b ? 1 : 0;
13483 }
13484
13485 /* Produce a malloc'd sorted array of reloc addresses from the info
13486 stored by append_relr_off. */
13487
13488 static bfd_vma *
13489 sort_relr (struct ppc_link_hash_table *htab)
13490 {
13491 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13492 if (addr == NULL)
13493 return NULL;
13494
13495 for (size_t i = 0; i < htab->relr_count; i++)
13496 addr[i] = (htab->relr[i].sec->output_section->vma
13497 + htab->relr[i].sec->output_offset
13498 + htab->relr[i].off);
13499
13500 if (htab->relr_count > 1)
13501 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13502
13503 return addr;
13504 }
13505
13506 /* Look over GOT and PLT entries saved on elf_local_got_ents for all
13507 input files, stashing info about needed relative relocs. */
13508
13509 static bool
13510 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13511 {
13512 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13513 bfd *ibfd;
13514
13515 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13516 {
13517 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13518 struct plt_entry **local_plt, **lplt, **end_local_plt;
13519 Elf_Internal_Shdr *symtab_hdr;
13520 bfd_size_type locsymcount;
13521 Elf_Internal_Sym *local_syms;
13522 Elf_Internal_Sym *isym;
13523 struct plt_entry *pent;
13524 struct got_entry *gent;
13525
13526 if (!is_ppc64_elf (ibfd))
13527 continue;
13528
13529 lgot_ents = elf_local_got_ents (ibfd);
13530 if (!lgot_ents)
13531 continue;
13532
13533 symtab_hdr = &elf_symtab_hdr (ibfd);
13534 locsymcount = symtab_hdr->sh_info;
13535 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13536 if (local_syms == NULL && locsymcount != 0)
13537 {
13538 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13539 0, NULL, NULL, NULL);
13540 if (local_syms == NULL)
13541 return false;
13542 }
13543 end_lgot_ents = lgot_ents + locsymcount;
13544 local_plt = (struct plt_entry **) end_lgot_ents;
13545 end_local_plt = local_plt + locsymcount;
13546 for (lgot = lgot_ents, isym = local_syms;
13547 lgot < end_lgot_ents;
13548 ++lgot, ++isym)
13549 for (gent = *lgot; gent != NULL; gent = gent->next)
13550 if (!gent->is_indirect
13551 && gent->tls_type == 0
13552 && gent->got.offset != (bfd_vma) -1
13553 && isym->st_shndx != SHN_ABS)
13554 {
13555 asection *got = ppc64_elf_tdata (gent->owner)->got;
13556 if (!append_relr_off (htab, got, gent->got.offset))
13557 {
13558 htab->stub_error = true;
13559 return false;
13560 }
13561 }
13562
13563 if (!htab->opd_abi)
13564 for (lplt = local_plt, isym = local_syms;
13565 lplt < end_local_plt;
13566 ++lplt, ++isym)
13567 for (pent = *lplt; pent != NULL; pent = pent->next)
13568 if (pent->plt.offset != (bfd_vma) -1
13569 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13570 {
13571 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13572 {
13573 if (symtab_hdr->contents != (unsigned char *) local_syms)
13574 free (local_syms);
13575 return false;
13576 }
13577 }
13578
13579 if (local_syms != NULL
13580 && symtab_hdr->contents != (unsigned char *) local_syms)
13581 {
13582 if (!info->keep_memory)
13583 free (local_syms);
13584 else
13585 symtab_hdr->contents = (unsigned char *) local_syms;
13586 }
13587 }
13588 return true;
13589 }
13590
13591 /* Stash info about needed GOT and PLT entry relative relocs for
13592 global symbol H. */
13593
13594 static bool
13595 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13596 {
13597 struct bfd_link_info *info;
13598 struct ppc_link_hash_table *htab;
13599 struct plt_entry *pent;
13600 struct got_entry *gent;
13601
13602 if (h->root.type == bfd_link_hash_indirect)
13603 return true;
13604
13605 info = (struct bfd_link_info *) inf;
13606 htab = ppc_hash_table (info);
13607 if (htab == NULL)
13608 return false;
13609
13610 if (h->type != STT_GNU_IFUNC
13611 && h->def_regular
13612 && (h->root.type == bfd_link_hash_defined
13613 || h->root.type == bfd_link_hash_defweak))
13614 {
13615 if ((!htab->elf.dynamic_sections_created
13616 || h->dynindx == -1
13617 || SYMBOL_REFERENCES_LOCAL (info, h))
13618 && !bfd_is_abs_symbol (&h->root))
13619 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13620 if (!gent->is_indirect
13621 && gent->tls_type == 0
13622 && gent->got.offset != (bfd_vma) -1)
13623 {
13624 asection *got = ppc64_elf_tdata (gent->owner)->got;
13625 if (!append_relr_off (htab, got, gent->got.offset))
13626 {
13627 htab->stub_error = true;
13628 return false;
13629 }
13630 }
13631
13632 if (!htab->opd_abi
13633 && use_local_plt (info, h))
13634 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13635 if (pent->plt.offset != (bfd_vma) -1)
13636 {
13637 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13638 {
13639 htab->stub_error = true;
13640 return false;
13641 }
13642 }
13643 }
13644 return true;
13645 }
13646
13647 /* Determine and set the size of the stub section for a final link.
13648
13649 The basic idea here is to examine all the relocations looking for
13650 PC-relative calls to a target that is unreachable with a "bl"
13651 instruction. */
13652
13653 bool
13654 ppc64_elf_size_stubs (struct bfd_link_info *info)
13655 {
13656 bfd_size_type stub_group_size;
13657 bool stubs_always_before_branch;
13658 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13659
13660 if (htab == NULL)
13661 return false;
13662
13663 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13664 htab->params->power10_stubs = 0;
13665
13666 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13667 htab->params->plt_thread_safe = 1;
13668 if (!htab->opd_abi)
13669 htab->params->plt_thread_safe = 0;
13670 else if (htab->params->plt_thread_safe == -1)
13671 {
13672 static const char *const thread_starter[] =
13673 {
13674 "pthread_create",
13675 /* libstdc++ */
13676 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13677 /* librt */
13678 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13679 "mq_notify", "create_timer",
13680 /* libanl */
13681 "getaddrinfo_a",
13682 /* libgomp */
13683 "GOMP_parallel",
13684 "GOMP_parallel_start",
13685 "GOMP_parallel_loop_static",
13686 "GOMP_parallel_loop_static_start",
13687 "GOMP_parallel_loop_dynamic",
13688 "GOMP_parallel_loop_dynamic_start",
13689 "GOMP_parallel_loop_guided",
13690 "GOMP_parallel_loop_guided_start",
13691 "GOMP_parallel_loop_runtime",
13692 "GOMP_parallel_loop_runtime_start",
13693 "GOMP_parallel_sections",
13694 "GOMP_parallel_sections_start",
13695 /* libgo */
13696 "__go_go",
13697 };
13698 unsigned i;
13699
13700 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13701 {
13702 struct elf_link_hash_entry *h;
13703 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13704 false, false, true);
13705 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13706 if (htab->params->plt_thread_safe)
13707 break;
13708 }
13709 }
13710 stubs_always_before_branch = htab->params->group_size < 0;
13711 if (htab->params->group_size < 0)
13712 stub_group_size = -htab->params->group_size;
13713 else
13714 stub_group_size = htab->params->group_size;
13715
13716 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13717 return false;
13718
13719 htab->tga_group = NULL;
13720 if (!htab->params->no_tls_get_addr_regsave
13721 && htab->tga_desc_fd != NULL
13722 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13723 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13724 && htab->tls_get_addr_fd != NULL
13725 && is_static_defined (&htab->tls_get_addr_fd->elf))
13726 {
13727 asection *sym_sec, *code_sec, *stub_sec;
13728 bfd_vma sym_value;
13729 struct _opd_sec_data *opd;
13730
13731 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13732 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13733 code_sec = sym_sec;
13734 opd = get_opd_info (sym_sec);
13735 if (opd != NULL)
13736 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13737 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13738 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13739 htab->tga_group->link_sec);
13740 if (stub_sec == NULL)
13741 return false;
13742 htab->tga_group->stub_sec = stub_sec;
13743
13744 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13745 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13746 htab->tga_desc_fd->elf.root.u.def.value = 0;
13747 htab->tga_desc_fd->elf.type = STT_FUNC;
13748 htab->tga_desc_fd->elf.def_regular = 1;
13749 htab->tga_desc_fd->elf.non_elf = 0;
13750 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13751 }
13752
13753 /* Loop until no stubs added. After iteration 20 of this loop we may
13754 exit on a stub section shrinking. */
13755
13756 while (1)
13757 {
13758 bfd *input_bfd;
13759 unsigned int bfd_indx;
13760 struct map_stub *group;
13761
13762 htab->stub_iteration += 1;
13763 htab->relr_count = 0;
13764
13765 for (input_bfd = info->input_bfds, bfd_indx = 0;
13766 input_bfd != NULL;
13767 input_bfd = input_bfd->link.next, bfd_indx++)
13768 {
13769 Elf_Internal_Shdr *symtab_hdr;
13770 asection *section;
13771 Elf_Internal_Sym *local_syms = NULL;
13772
13773 if (!is_ppc64_elf (input_bfd))
13774 continue;
13775
13776 /* We'll need the symbol table in a second. */
13777 symtab_hdr = &elf_symtab_hdr (input_bfd);
13778 if (symtab_hdr->sh_info == 0)
13779 continue;
13780
13781 /* Walk over each section attached to the input bfd. */
13782 for (section = input_bfd->sections;
13783 section != NULL;
13784 section = section->next)
13785 {
13786 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13787 bool is_opd;
13788
13789 /* If there aren't any relocs, then there's nothing more
13790 to do. */
13791 if ((section->flags & SEC_RELOC) == 0
13792 || (section->flags & SEC_ALLOC) == 0
13793 || (section->flags & SEC_LOAD) == 0
13794 || section->reloc_count == 0)
13795 continue;
13796
13797 if (!info->enable_dt_relr
13798 && (section->flags & SEC_CODE) == 0)
13799 continue;
13800
13801 /* If this section is a link-once section that will be
13802 discarded, then don't create any stubs. */
13803 if (section->output_section == NULL
13804 || section->output_section->owner != info->output_bfd)
13805 continue;
13806
13807 /* Get the relocs. */
13808 internal_relocs
13809 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13810 info->keep_memory);
13811 if (internal_relocs == NULL)
13812 goto error_ret_free_local;
13813
13814 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13815
13816 /* Now examine each relocation. */
13817 irela = internal_relocs;
13818 irelaend = irela + section->reloc_count;
13819 for (; irela < irelaend; irela++)
13820 {
13821 enum elf_ppc64_reloc_type r_type;
13822 unsigned int r_indx;
13823 struct ppc_stub_type stub_type;
13824 struct ppc_stub_hash_entry *stub_entry;
13825 asection *sym_sec, *code_sec;
13826 bfd_vma sym_value, code_value;
13827 bfd_vma destination;
13828 unsigned long local_off;
13829 bool ok_dest;
13830 struct ppc_link_hash_entry *hash;
13831 struct ppc_link_hash_entry *fdh;
13832 struct elf_link_hash_entry *h;
13833 Elf_Internal_Sym *sym;
13834 char *stub_name;
13835 const asection *id_sec;
13836 struct _opd_sec_data *opd;
13837 struct plt_entry *plt_ent;
13838
13839 r_type = ELF64_R_TYPE (irela->r_info);
13840 r_indx = ELF64_R_SYM (irela->r_info);
13841
13842 if (r_type >= R_PPC64_max)
13843 {
13844 bfd_set_error (bfd_error_bad_value);
13845 goto error_ret_free_internal;
13846 }
13847
13848 /* Only look for stubs on branch instructions. */
13849 switch (r_type)
13850 {
13851 default:
13852 continue;
13853
13854 case R_PPC64_REL24:
13855 case R_PPC64_REL24_NOTOC:
13856 case R_PPC64_REL24_P9NOTOC:
13857 case R_PPC64_REL14:
13858 case R_PPC64_REL14_BRTAKEN:
13859 case R_PPC64_REL14_BRNTAKEN:
13860 if ((section->flags & SEC_CODE) != 0)
13861 break;
13862 continue;
13863
13864 case R_PPC64_ADDR64:
13865 case R_PPC64_TOC:
13866 if (info->enable_dt_relr
13867 && irela->r_offset % 2 == 0
13868 && section->alignment_power != 0)
13869 break;
13870 continue;
13871 }
13872
13873 /* Now determine the call target, its name, value,
13874 section. */
13875 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13876 r_indx, input_bfd))
13877 goto error_ret_free_internal;
13878
13879 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13880 {
13881 /* Only locally defined symbols can possibly use
13882 relative relocations. */
13883 bfd_vma r_offset;
13884 if ((sym_sec == NULL
13885 || sym_sec->output_section == NULL)
13886 /* No symbol is OK too. */
13887 && !(sym != NULL && sym->st_shndx == 0)
13888 /* Hack for __ehdr_start, which is undefined
13889 at this point. */
13890 && !(h != NULL && h->root.linker_def))
13891 continue;
13892 if (NO_OPD_RELOCS && is_opd)
13893 continue;
13894 if (!is_opd
13895 && r_type == R_PPC64_ADDR64)
13896 {
13897 if (h != NULL
13898 ? h->type == STT_GNU_IFUNC
13899 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13900 continue;
13901 if (h != NULL
13902 ? bfd_is_abs_symbol (&h->root)
13903 : sym->st_shndx == SHN_ABS)
13904 continue;
13905 if (h != NULL
13906 && !SYMBOL_REFERENCES_LOCAL (info, h))
13907 continue;
13908 }
13909 r_offset = _bfd_elf_section_offset (info->output_bfd,
13910 info,
13911 section,
13912 irela->r_offset);
13913 if (r_offset >= (bfd_vma) -2)
13914 continue;
13915 if (!append_relr_off (htab, section, r_offset))
13916 goto error_ret_free_internal;
13917 continue;
13918 }
13919
13920 hash = ppc_elf_hash_entry (h);
13921 ok_dest = false;
13922 fdh = NULL;
13923 sym_value = 0;
13924 if (hash == NULL)
13925 {
13926 sym_value = sym->st_value;
13927 if (sym_sec != NULL
13928 && sym_sec->output_section != NULL)
13929 ok_dest = true;
13930 }
13931 else if (hash->elf.root.type == bfd_link_hash_defined
13932 || hash->elf.root.type == bfd_link_hash_defweak)
13933 {
13934 sym_value = hash->elf.root.u.def.value;
13935 if (sym_sec->output_section != NULL)
13936 ok_dest = true;
13937 }
13938 else if (hash->elf.root.type == bfd_link_hash_undefweak
13939 || hash->elf.root.type == bfd_link_hash_undefined)
13940 {
13941 /* Recognise an old ABI func code entry sym, and
13942 use the func descriptor sym instead if it is
13943 defined. */
13944 if (hash->elf.root.root.string[0] == '.'
13945 && hash->oh != NULL)
13946 {
13947 fdh = ppc_follow_link (hash->oh);
13948 if (fdh->elf.root.type == bfd_link_hash_defined
13949 || fdh->elf.root.type == bfd_link_hash_defweak)
13950 {
13951 sym_sec = fdh->elf.root.u.def.section;
13952 sym_value = fdh->elf.root.u.def.value;
13953 if (sym_sec->output_section != NULL)
13954 ok_dest = true;
13955 }
13956 else
13957 fdh = NULL;
13958 }
13959 }
13960 else
13961 {
13962 bfd_set_error (bfd_error_bad_value);
13963 goto error_ret_free_internal;
13964 }
13965
13966 destination = 0;
13967 local_off = 0;
13968 if (ok_dest)
13969 {
13970 sym_value += irela->r_addend;
13971 destination = (sym_value
13972 + sym_sec->output_offset
13973 + sym_sec->output_section->vma);
13974 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13975 ? hash->elf.other
13976 : sym->st_other);
13977 }
13978
13979 code_sec = sym_sec;
13980 code_value = sym_value;
13981 opd = get_opd_info (sym_sec);
13982 if (opd != NULL)
13983 {
13984 bfd_vma dest;
13985
13986 if (hash == NULL && opd->adjust != NULL)
13987 {
13988 long adjust = opd->adjust[OPD_NDX (sym_value)];
13989 if (adjust == -1)
13990 continue;
13991 code_value += adjust;
13992 sym_value += adjust;
13993 }
13994 dest = opd_entry_value (sym_sec, sym_value,
13995 &code_sec, &code_value, false);
13996 if (dest != (bfd_vma) -1)
13997 {
13998 destination = dest;
13999 if (fdh != NULL)
14000 {
14001 /* Fixup old ABI sym to point at code
14002 entry. */
14003 hash->elf.root.type = bfd_link_hash_defweak;
14004 hash->elf.root.u.def.section = code_sec;
14005 hash->elf.root.u.def.value = code_value;
14006 }
14007 }
14008 }
14009
14010 /* Determine what (if any) linker stub is needed. */
14011 plt_ent = NULL;
14012 stub_type.main = ppc_type_of_stub (section, irela, &hash,
14013 &plt_ent, destination,
14014 local_off);
14015 stub_type.sub = ppc_stub_toc;
14016 stub_type.r2save = 0;
14017
14018 if (r_type == R_PPC64_REL24_NOTOC
14019 || r_type == R_PPC64_REL24_P9NOTOC)
14020 {
14021 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14022 if (htab->params->power10_stubs == 0
14023 || (r_type == R_PPC64_REL24_P9NOTOC
14024 && htab->params->power10_stubs != 1))
14025 notoc = ppc_stub_p9notoc;
14026 if (stub_type.main == ppc_stub_plt_call)
14027 stub_type.sub = notoc;
14028 else if (stub_type.main == ppc_stub_long_branch
14029 || (code_sec != NULL
14030 && code_sec->output_section != NULL
14031 && (((hash ? hash->elf.other : sym->st_other)
14032 & STO_PPC64_LOCAL_MASK)
14033 > 1 << STO_PPC64_LOCAL_BIT)))
14034 {
14035 stub_type.main = ppc_stub_long_branch;
14036 stub_type.sub = notoc;
14037 stub_type.r2save = 0;
14038 }
14039 }
14040 else if (stub_type.main != ppc_stub_plt_call)
14041 {
14042 /* Check whether we need a TOC adjusting stub.
14043 Since the linker pastes together pieces from
14044 different object files when creating the
14045 _init and _fini functions, it may be that a
14046 call to what looks like a local sym is in
14047 fact a call needing a TOC adjustment. */
14048 if ((code_sec != NULL
14049 && code_sec->output_section != NULL
14050 && (code_sec->has_toc_reloc
14051 || code_sec->makes_toc_func_call)
14052 && (htab->sec_info[code_sec->id].toc_off
14053 != htab->sec_info[section->id].toc_off))
14054 || (((hash ? hash->elf.other : sym->st_other)
14055 & STO_PPC64_LOCAL_MASK)
14056 == 1 << STO_PPC64_LOCAL_BIT))
14057 {
14058 stub_type.main = ppc_stub_long_branch;
14059 stub_type.sub = ppc_stub_toc;
14060 stub_type.r2save = 1;
14061 }
14062 }
14063
14064 if (stub_type.main == ppc_stub_none)
14065 continue;
14066
14067 /* __tls_get_addr calls might be eliminated. */
14068 if (stub_type.main != ppc_stub_plt_call
14069 && hash != NULL
14070 && is_tls_get_addr (&hash->elf, htab)
14071 && section->has_tls_reloc
14072 && irela != internal_relocs)
14073 {
14074 /* Get tls info. */
14075 unsigned char *tls_mask;
14076
14077 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14078 irela - 1, input_bfd))
14079 goto error_ret_free_internal;
14080 if ((*tls_mask & TLS_TLS) != 0
14081 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14082 continue;
14083 }
14084
14085 if (stub_type.main == ppc_stub_plt_call
14086 && stub_type.sub == ppc_stub_toc)
14087 {
14088 if (!htab->opd_abi
14089 && htab->params->plt_localentry0 != 0
14090 && is_elfv2_localentry0 (&hash->elf))
14091 htab->has_plt_localentry0 = 1;
14092 else if (irela + 1 < irelaend
14093 && irela[1].r_offset == irela->r_offset + 4
14094 && (ELF64_R_TYPE (irela[1].r_info)
14095 == R_PPC64_TOCSAVE))
14096 {
14097 if (!tocsave_find (htab, INSERT,
14098 &local_syms, irela + 1, input_bfd))
14099 goto error_ret_free_internal;
14100 }
14101 else
14102 stub_type.r2save = 1;
14103 }
14104
14105 /* Support for grouping stub sections. */
14106 id_sec = htab->sec_info[section->id].u.group->link_sec;
14107
14108 /* Get the name of this stub. */
14109 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14110 if (!stub_name)
14111 goto error_ret_free_internal;
14112
14113 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14114 stub_name, false, false);
14115 if (stub_entry != NULL)
14116 {
14117 free (stub_name);
14118 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14119 {
14120 /* xgettext:c-format */
14121 _bfd_error_handler
14122 (_("%pB: cannot create stub entry %s"),
14123 section->owner, stub_entry->root.string);
14124 goto error_ret_free_internal;
14125 }
14126 continue;
14127 }
14128
14129 stub_entry = ppc_add_stub (stub_name, section, info);
14130 if (stub_entry == NULL)
14131 {
14132 free (stub_name);
14133 error_ret_free_internal:
14134 if (elf_section_data (section)->relocs == NULL)
14135 free (internal_relocs);
14136 error_ret_free_local:
14137 if (symtab_hdr->contents
14138 != (unsigned char *) local_syms)
14139 free (local_syms);
14140 return false;
14141 }
14142
14143 stub_entry->type = stub_type;
14144 if (stub_type.main == ppc_stub_plt_call)
14145 {
14146 stub_entry->target_value = sym_value;
14147 stub_entry->target_section = sym_sec;
14148 }
14149 else
14150 {
14151 stub_entry->target_value = code_value;
14152 stub_entry->target_section = code_sec;
14153 }
14154 stub_entry->h = hash;
14155 stub_entry->plt_ent = plt_ent;
14156 stub_entry->symtype
14157 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14158 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14159
14160 if (hash != NULL
14161 && (hash->elf.root.type == bfd_link_hash_defined
14162 || hash->elf.root.type == bfd_link_hash_defweak))
14163 htab->stub_globals += 1;
14164 }
14165
14166 /* We're done with the internal relocs, free them. */
14167 if (elf_section_data (section)->relocs != internal_relocs)
14168 free (internal_relocs);
14169 }
14170
14171 if (local_syms != NULL
14172 && symtab_hdr->contents != (unsigned char *) local_syms)
14173 {
14174 if (!info->keep_memory)
14175 free (local_syms);
14176 else
14177 symtab_hdr->contents = (unsigned char *) local_syms;
14178 }
14179 }
14180
14181 /* We may have added some stubs. Find out the new size of the
14182 stub sections. */
14183 for (group = htab->group; group != NULL; group = group->next)
14184 {
14185 group->lr_restore = 0;
14186 group->eh_size = 0;
14187 if (group->stub_sec != NULL)
14188 {
14189 asection *stub_sec = group->stub_sec;
14190
14191 stub_sec->rawsize = stub_sec->size;
14192 stub_sec->size = 0;
14193 stub_sec->reloc_count = 0;
14194 stub_sec->flags &= ~SEC_RELOC;
14195 }
14196 }
14197 if (htab->tga_group != NULL)
14198 {
14199 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14200 htab->tga_group->eh_size
14201 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14202 htab->tga_group->lr_restore = 23 * 4;
14203 htab->tga_group->stub_sec->size = 24 * 4;
14204 }
14205
14206 htab->brlt->rawsize = htab->brlt->size;
14207 htab->brlt->size = 0;
14208 htab->brlt->reloc_count = 0;
14209 htab->brlt->flags &= ~SEC_RELOC;
14210 if (htab->relbrlt != NULL)
14211 htab->relbrlt->size = 0;
14212
14213 if (htab->elf.srelrdyn != NULL)
14214 {
14215 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14216 htab->elf.srelrdyn->size = 0;
14217 }
14218
14219 htab->stub_changed = false;
14220 htab->stub_id = 0;
14221 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14222
14223 for (group = htab->group; group != NULL; group = group->next)
14224 if (group->needs_save_res)
14225 group->stub_sec->size += htab->sfpr->size;
14226
14227 if (info->emitrelocations
14228 && htab->glink != NULL && htab->glink->size != 0)
14229 {
14230 htab->glink->reloc_count = 1;
14231 htab->glink->flags |= SEC_RELOC;
14232 }
14233
14234 if (htab->glink_eh_frame != NULL
14235 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14236 && htab->glink_eh_frame->output_section->size > 8)
14237 {
14238 size_t size = 0, align = 4;
14239
14240 for (group = htab->group; group != NULL; group = group->next)
14241 if (group->eh_size != 0)
14242 size += (group->eh_size + 17 + align - 1) & -align;
14243 if (htab->glink != NULL && htab->glink->size != 0)
14244 size += (24 + align - 1) & -align;
14245 if (size != 0)
14246 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14247 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14248 size = (size + align - 1) & -align;
14249 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14250 htab->glink_eh_frame->size = size;
14251 }
14252
14253 if (htab->params->plt_stub_align != 0)
14254 for (group = htab->group; group != NULL; group = group->next)
14255 if (group->stub_sec != NULL)
14256 {
14257 int align = abs (htab->params->plt_stub_align);
14258 group->stub_sec->size
14259 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14260 }
14261
14262 if (htab->elf.srelrdyn != NULL)
14263 {
14264 bfd_vma r_offset;
14265
14266 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14267 if (!append_relr_off (htab, htab->brlt, r_offset))
14268 return false;
14269
14270 if (!got_and_plt_relr_for_local_syms (info))
14271 return false;
14272 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14273 if (htab->stub_error)
14274 return false;
14275
14276 bfd_vma *relr_addr = sort_relr (htab);
14277 if (htab->relr_count != 0 && relr_addr == NULL)
14278 return false;
14279
14280 size_t i = 0;
14281 while (i < htab->relr_count)
14282 {
14283 bfd_vma base = relr_addr[i];
14284 htab->elf.srelrdyn->size += 8;
14285 i++;
14286 /* Handle possible duplicate address. This can happen
14287 as sections increase in size when adding stubs. */
14288 while (i < htab->relr_count
14289 && relr_addr[i] == base)
14290 i++;
14291 base += 8;
14292 while (1)
14293 {
14294 size_t start_i = i;
14295 while (i < htab->relr_count
14296 && relr_addr[i] - base < 63 * 8
14297 && (relr_addr[i] - base) % 8 == 0)
14298 i++;
14299 if (i == start_i)
14300 break;
14301 htab->elf.srelrdyn->size += 8;
14302 base += 63 * 8;
14303 }
14304 }
14305 free (relr_addr);
14306 }
14307
14308 for (group = htab->group; group != NULL; group = group->next)
14309 if (group->stub_sec != NULL
14310 && group->stub_sec->rawsize != group->stub_sec->size
14311 && (htab->stub_iteration <= STUB_SHRINK_ITER
14312 || group->stub_sec->rawsize < group->stub_sec->size))
14313 break;
14314
14315 if (group == NULL
14316 && (!htab->stub_changed
14317 || htab->stub_iteration > STUB_SHRINK_ITER)
14318 && (htab->brlt->rawsize == htab->brlt->size
14319 || (htab->stub_iteration > STUB_SHRINK_ITER
14320 && htab->brlt->rawsize > htab->brlt->size))
14321 && (htab->elf.srelrdyn == NULL
14322 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14323 || (htab->stub_iteration > STUB_SHRINK_ITER
14324 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14325 && (htab->glink_eh_frame == NULL
14326 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14327 && (htab->tga_group == NULL
14328 || htab->stub_iteration > 1))
14329 break;
14330
14331 if (htab->stub_iteration > STUB_SHRINK_ITER)
14332 {
14333 for (group = htab->group; group != NULL; group = group->next)
14334 if (group->stub_sec != NULL
14335 && group->stub_sec->size < group->stub_sec->rawsize)
14336 group->stub_sec->size = group->stub_sec->rawsize;
14337
14338 if (htab->brlt->size < htab->brlt->rawsize)
14339 htab->brlt->size = htab->brlt->rawsize;
14340
14341 if (htab->elf.srelrdyn != NULL
14342 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14343 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14344 }
14345
14346 /* Ask the linker to do its stuff. */
14347 (*htab->params->layout_sections_again) ();
14348 }
14349
14350 if (htab->glink_eh_frame != NULL
14351 && htab->glink_eh_frame->size != 0)
14352 {
14353 bfd_vma val;
14354 bfd_byte *p, *last_fde;
14355 size_t last_fde_len, size, align, pad;
14356 struct map_stub *group;
14357
14358 /* It is necessary to at least have a rough outline of the
14359 linker generated CIEs and FDEs written before
14360 bfd_elf_discard_info is run, in order for these FDEs to be
14361 indexed in .eh_frame_hdr. */
14362 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14363 if (p == NULL)
14364 return false;
14365 htab->glink_eh_frame->contents = p;
14366 last_fde = p;
14367 align = 4;
14368
14369 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14370 /* CIE length (rewrite in case little-endian). */
14371 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14372 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14373 p += last_fde_len + 4;
14374
14375 for (group = htab->group; group != NULL; group = group->next)
14376 if (group->eh_size != 0)
14377 {
14378 group->eh_base = p - htab->glink_eh_frame->contents;
14379 last_fde = p;
14380 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14381 /* FDE length. */
14382 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14383 p += 4;
14384 /* CIE pointer. */
14385 val = p - htab->glink_eh_frame->contents;
14386 bfd_put_32 (htab->elf.dynobj, val, p);
14387 p += 4;
14388 /* Offset to stub section, written later. */
14389 p += 4;
14390 /* stub section size. */
14391 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14392 p += 4;
14393 /* Augmentation. */
14394 p += 1;
14395 /* Make sure we don't have all nops. This is enough for
14396 elf-eh-frame.c to detect the last non-nop opcode. */
14397 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14398 p = last_fde + last_fde_len + 4;
14399 }
14400 if (htab->glink != NULL && htab->glink->size != 0)
14401 {
14402 last_fde = p;
14403 last_fde_len = ((24 + align - 1) & -align) - 4;
14404 /* FDE length. */
14405 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14406 p += 4;
14407 /* CIE pointer. */
14408 val = p - htab->glink_eh_frame->contents;
14409 bfd_put_32 (htab->elf.dynobj, val, p);
14410 p += 4;
14411 /* Offset to .glink, written later. */
14412 p += 4;
14413 /* .glink size. */
14414 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14415 p += 4;
14416 /* Augmentation. */
14417 p += 1;
14418
14419 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14420 *p++ = DW_CFA_register;
14421 *p++ = 65;
14422 *p++ = htab->opd_abi ? 12 : 0;
14423 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14424 *p++ = DW_CFA_restore_extended;
14425 *p++ = 65;
14426 p += ((24 + align - 1) & -align) - 24;
14427 }
14428 /* Subsume any padding into the last FDE if user .eh_frame
14429 sections are aligned more than glink_eh_frame. Otherwise any
14430 zero padding will be seen as a terminator. */
14431 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14432 size = p - htab->glink_eh_frame->contents;
14433 pad = ((size + align - 1) & -align) - size;
14434 htab->glink_eh_frame->size = size + pad;
14435 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14436 }
14437
14438 maybe_strip_output (info, htab->brlt);
14439 if (htab->relbrlt != NULL)
14440 maybe_strip_output (info, htab->relbrlt);
14441 if (htab->glink_eh_frame != NULL)
14442 maybe_strip_output (info, htab->glink_eh_frame);
14443 if (htab->elf.srelrdyn != NULL)
14444 maybe_strip_output (info, htab->elf.srelrdyn);
14445
14446 return true;
14447 }
14448
14449 /* Called after we have determined section placement. If sections
14450 move, we'll be called again. Provide a value for TOCstart. */
14451
14452 bfd_vma
14453 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14454 {
14455 asection *s;
14456 bfd_vma TOCstart, adjust;
14457
14458 if (info != NULL)
14459 {
14460 struct elf_link_hash_entry *h;
14461 struct elf_link_hash_table *htab = elf_hash_table (info);
14462
14463 if (is_elf_hash_table (&htab->root)
14464 && htab->hgot != NULL)
14465 h = htab->hgot;
14466 else
14467 {
14468 h = (struct elf_link_hash_entry *)
14469 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14470 if (is_elf_hash_table (&htab->root))
14471 htab->hgot = h;
14472 }
14473 if (h != NULL
14474 && h->root.type == bfd_link_hash_defined
14475 && !h->root.linker_def
14476 && (!is_elf_hash_table (&htab->root)
14477 || h->def_regular))
14478 {
14479 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14480 _bfd_set_gp_value (obfd, TOCstart);
14481 return TOCstart;
14482 }
14483 }
14484
14485 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14486 order. The TOC starts where the first of these sections starts. */
14487 s = bfd_get_section_by_name (obfd, ".got");
14488 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14489 s = bfd_get_section_by_name (obfd, ".toc");
14490 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14491 s = bfd_get_section_by_name (obfd, ".tocbss");
14492 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14493 s = bfd_get_section_by_name (obfd, ".plt");
14494 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14495 {
14496 /* This may happen for
14497 o references to TOC base (SYM@toc / TOC[tc0]) without a
14498 .toc directive
14499 o bad linker script
14500 o --gc-sections and empty TOC sections
14501
14502 FIXME: Warn user? */
14503
14504 /* Look for a likely section. We probably won't even be
14505 using TOCstart. */
14506 for (s = obfd->sections; s != NULL; s = s->next)
14507 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14508 | SEC_EXCLUDE))
14509 == (SEC_ALLOC | SEC_SMALL_DATA))
14510 break;
14511 if (s == NULL)
14512 for (s = obfd->sections; s != NULL; s = s->next)
14513 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14514 == (SEC_ALLOC | SEC_SMALL_DATA))
14515 break;
14516 if (s == NULL)
14517 for (s = obfd->sections; s != NULL; s = s->next)
14518 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14519 == SEC_ALLOC)
14520 break;
14521 if (s == NULL)
14522 for (s = obfd->sections; s != NULL; s = s->next)
14523 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14524 break;
14525 }
14526
14527 TOCstart = 0;
14528 if (s != NULL)
14529 TOCstart = s->output_section->vma + s->output_offset;
14530
14531 /* Force alignment. */
14532 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14533 TOCstart -= adjust;
14534 _bfd_set_gp_value (obfd, TOCstart);
14535
14536 if (info != NULL && s != NULL)
14537 {
14538 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14539
14540 if (htab != NULL)
14541 {
14542 if (htab->elf.hgot != NULL)
14543 {
14544 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14545 htab->elf.hgot->root.u.def.section = s;
14546 }
14547 }
14548 else
14549 {
14550 struct bfd_link_hash_entry *bh = NULL;
14551 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14552 s, TOC_BASE_OFF - adjust,
14553 NULL, false, false, &bh);
14554 }
14555 }
14556 return TOCstart;
14557 }
14558
14559 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14560 write out any global entry stubs, and PLT relocations. */
14561
14562 static bool
14563 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14564 {
14565 struct bfd_link_info *info;
14566 struct ppc_link_hash_table *htab;
14567 struct plt_entry *ent;
14568 asection *s;
14569
14570 if (h->root.type == bfd_link_hash_indirect)
14571 return true;
14572
14573 info = inf;
14574 htab = ppc_hash_table (info);
14575 if (htab == NULL)
14576 return false;
14577
14578 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14579 if (ent->plt.offset != (bfd_vma) -1)
14580 {
14581 /* This symbol has an entry in the procedure linkage
14582 table. Set it up. */
14583 Elf_Internal_Rela rela;
14584 asection *plt, *relplt;
14585 bfd_byte *loc;
14586
14587 if (use_local_plt (info, h))
14588 {
14589 if (!(h->def_regular
14590 && (h->root.type == bfd_link_hash_defined
14591 || h->root.type == bfd_link_hash_defweak)))
14592 continue;
14593 if (h->type == STT_GNU_IFUNC)
14594 {
14595 plt = htab->elf.iplt;
14596 relplt = htab->elf.irelplt;
14597 htab->elf.ifunc_resolvers = true;
14598 if (htab->opd_abi)
14599 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14600 else
14601 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14602 }
14603 else
14604 {
14605 plt = htab->pltlocal;
14606 relplt = NULL;
14607 if (bfd_link_pic (info)
14608 && !(info->enable_dt_relr && !htab->opd_abi))
14609 {
14610 relplt = htab->relpltlocal;
14611 if (htab->opd_abi)
14612 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14613 else
14614 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14615 }
14616 }
14617 rela.r_addend = defined_sym_val (h) + ent->addend;
14618
14619 if (relplt == NULL)
14620 {
14621 loc = plt->contents + ent->plt.offset;
14622 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14623 if (htab->opd_abi)
14624 {
14625 bfd_vma toc = elf_gp (info->output_bfd);
14626 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14627 bfd_put_64 (info->output_bfd, toc, loc + 8);
14628 }
14629 }
14630 else
14631 {
14632 rela.r_offset = (plt->output_section->vma
14633 + plt->output_offset
14634 + ent->plt.offset);
14635 loc = relplt->contents + (relplt->reloc_count++
14636 * sizeof (Elf64_External_Rela));
14637 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14638 }
14639 }
14640 else
14641 {
14642 rela.r_offset = (htab->elf.splt->output_section->vma
14643 + htab->elf.splt->output_offset
14644 + ent->plt.offset);
14645 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14646 rela.r_addend = ent->addend;
14647 loc = (htab->elf.srelplt->contents
14648 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14649 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14650 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14651 htab->elf.ifunc_resolvers = true;
14652 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14653 }
14654 }
14655
14656 if (!h->pointer_equality_needed)
14657 return true;
14658
14659 if (h->def_regular)
14660 return true;
14661
14662 s = htab->global_entry;
14663 if (s == NULL || s->size == 0)
14664 return true;
14665
14666 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14667 if (ent->plt.offset != (bfd_vma) -1
14668 && ent->addend == 0)
14669 {
14670 bfd_byte *p;
14671 asection *plt;
14672 bfd_vma off;
14673
14674 p = s->contents + h->root.u.def.value;
14675 plt = htab->elf.splt;
14676 if (use_local_plt (info, h))
14677 {
14678 if (h->type == STT_GNU_IFUNC)
14679 plt = htab->elf.iplt;
14680 else
14681 plt = htab->pltlocal;
14682 }
14683 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14684 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14685
14686 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14687 {
14688 info->callbacks->einfo
14689 (_("%P: linkage table error against `%pT'\n"),
14690 h->root.root.string);
14691 bfd_set_error (bfd_error_bad_value);
14692 htab->stub_error = true;
14693 }
14694
14695 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14696 if (htab->params->emit_stub_syms)
14697 {
14698 size_t len = strlen (h->root.root.string);
14699 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14700
14701 if (name == NULL)
14702 return false;
14703
14704 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14705 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14706 if (h == NULL)
14707 return false;
14708 if (h->root.type == bfd_link_hash_new)
14709 {
14710 h->root.type = bfd_link_hash_defined;
14711 h->root.u.def.section = s;
14712 h->root.u.def.value = p - s->contents;
14713 h->ref_regular = 1;
14714 h->def_regular = 1;
14715 h->ref_regular_nonweak = 1;
14716 h->forced_local = 1;
14717 h->non_elf = 0;
14718 h->root.linker_def = 1;
14719 }
14720 }
14721
14722 if (PPC_HA (off) != 0)
14723 {
14724 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14725 p += 4;
14726 }
14727 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14728 p += 4;
14729 bfd_put_32 (s->owner, MTCTR_R12, p);
14730 p += 4;
14731 bfd_put_32 (s->owner, BCTR, p);
14732 break;
14733 }
14734 return true;
14735 }
14736
14737 /* Write PLT relocs for locals. */
14738
14739 static bool
14740 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14741 {
14742 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14743 bfd *ibfd;
14744
14745 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14746 {
14747 struct got_entry **lgot_ents, **end_lgot_ents;
14748 struct plt_entry **local_plt, **lplt, **end_local_plt;
14749 Elf_Internal_Shdr *symtab_hdr;
14750 bfd_size_type locsymcount;
14751 Elf_Internal_Sym *local_syms = NULL;
14752 struct plt_entry *ent;
14753
14754 if (!is_ppc64_elf (ibfd))
14755 continue;
14756
14757 lgot_ents = elf_local_got_ents (ibfd);
14758 if (!lgot_ents)
14759 continue;
14760
14761 symtab_hdr = &elf_symtab_hdr (ibfd);
14762 locsymcount = symtab_hdr->sh_info;
14763 end_lgot_ents = lgot_ents + locsymcount;
14764 local_plt = (struct plt_entry **) end_lgot_ents;
14765 end_local_plt = local_plt + locsymcount;
14766 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14767 for (ent = *lplt; ent != NULL; ent = ent->next)
14768 if (ent->plt.offset != (bfd_vma) -1)
14769 {
14770 Elf_Internal_Sym *sym;
14771 asection *sym_sec;
14772 asection *plt, *relplt;
14773 bfd_byte *loc;
14774 bfd_vma val;
14775
14776 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14777 lplt - local_plt, ibfd))
14778 {
14779 if (symtab_hdr->contents != (unsigned char *) local_syms)
14780 free (local_syms);
14781 return false;
14782 }
14783
14784 val = sym->st_value + ent->addend;
14785 if (sym_sec != NULL && sym_sec->output_section != NULL)
14786 val += sym_sec->output_offset + sym_sec->output_section->vma;
14787
14788 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14789 {
14790 htab->elf.ifunc_resolvers = true;
14791 plt = htab->elf.iplt;
14792 relplt = htab->elf.irelplt;
14793 }
14794 else
14795 {
14796 plt = htab->pltlocal;
14797 relplt = NULL;
14798 if (bfd_link_pic (info)
14799 && !(info->enable_dt_relr && !htab->opd_abi))
14800 relplt = htab->relpltlocal;
14801 }
14802
14803 if (relplt == NULL)
14804 {
14805 loc = plt->contents + ent->plt.offset;
14806 bfd_put_64 (info->output_bfd, val, loc);
14807 if (htab->opd_abi)
14808 {
14809 bfd_vma toc = elf_gp (ibfd);
14810 bfd_put_64 (info->output_bfd, toc, loc + 8);
14811 }
14812 }
14813 else
14814 {
14815 Elf_Internal_Rela rela;
14816 rela.r_offset = (ent->plt.offset
14817 + plt->output_offset
14818 + plt->output_section->vma);
14819 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14820 {
14821 if (htab->opd_abi)
14822 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14823 else
14824 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14825 }
14826 else
14827 {
14828 if (htab->opd_abi)
14829 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14830 else
14831 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14832 }
14833 rela.r_addend = val;
14834 loc = relplt->contents + (relplt->reloc_count++
14835 * sizeof (Elf64_External_Rela));
14836 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14837 }
14838 }
14839
14840 if (local_syms != NULL
14841 && symtab_hdr->contents != (unsigned char *) local_syms)
14842 {
14843 if (!info->keep_memory)
14844 free (local_syms);
14845 else
14846 symtab_hdr->contents = (unsigned char *) local_syms;
14847 }
14848 }
14849 return true;
14850 }
14851
14852 /* Emit the static wrapper function preserving registers around a
14853 __tls_get_addr_opt call. */
14854
14855 static bool
14856 emit_tga_desc (struct ppc_link_hash_table *htab)
14857 {
14858 asection *stub_sec = htab->tga_group->stub_sec;
14859 unsigned int cfa_updt = 11 * 4;
14860 bfd_byte *p;
14861 bfd_vma to, from, delta;
14862
14863 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14864 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14865 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14866 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14867 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14868 delta = to - from;
14869 if (delta + (1 << 25) >= 1 << 26)
14870 {
14871 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14872 htab->stub_error = true;
14873 return false;
14874 }
14875
14876 p = stub_sec->contents;
14877 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14878 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14879 p += 4;
14880 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14881 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14882 }
14883
14884 /* Emit eh_frame describing the static wrapper function. */
14885
14886 static bfd_byte *
14887 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14888 {
14889 unsigned int cfa_updt = 11 * 4;
14890 unsigned int i;
14891
14892 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14893 *p++ = DW_CFA_def_cfa_offset;
14894 if (htab->opd_abi)
14895 {
14896 *p++ = 128;
14897 *p++ = 1;
14898 }
14899 else
14900 *p++ = 96;
14901 *p++ = DW_CFA_offset_extended_sf;
14902 *p++ = 65;
14903 *p++ = (-16 / 8) & 0x7f;
14904 for (i = 4; i < 12; i++)
14905 {
14906 *p++ = DW_CFA_offset + i;
14907 *p++ = (htab->opd_abi ? 13 : 12) - i;
14908 }
14909 *p++ = DW_CFA_advance_loc + 10;
14910 *p++ = DW_CFA_def_cfa_offset;
14911 *p++ = 0;
14912 for (i = 4; i < 12; i++)
14913 *p++ = DW_CFA_restore + i;
14914 *p++ = DW_CFA_advance_loc + 2;
14915 *p++ = DW_CFA_restore_extended;
14916 *p++ = 65;
14917 return p;
14918 }
14919
14920 /* Build all the stubs associated with the current output file.
14921 The stubs are kept in a hash table attached to the main linker
14922 hash table. This function is called via gldelf64ppc_finish. */
14923
14924 bool
14925 ppc64_elf_build_stubs (struct bfd_link_info *info,
14926 char **stats)
14927 {
14928 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14929 struct map_stub *group;
14930 asection *stub_sec;
14931 bfd_byte *p;
14932 int stub_sec_count = 0;
14933
14934 if (htab == NULL)
14935 return false;
14936
14937 /* Allocate memory to hold the linker stubs. */
14938 for (group = htab->group; group != NULL; group = group->next)
14939 {
14940 group->eh_size = 0;
14941 group->lr_restore = 0;
14942 if ((stub_sec = group->stub_sec) != NULL
14943 && stub_sec->size != 0)
14944 {
14945 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14946 stub_sec->size);
14947 if (stub_sec->contents == NULL)
14948 return false;
14949 stub_sec->size = 0;
14950 }
14951 }
14952
14953 if (htab->glink != NULL && htab->glink->size != 0)
14954 {
14955 unsigned int indx;
14956 bfd_vma plt0;
14957
14958 /* Build the .glink plt call stub. */
14959 if (htab->params->emit_stub_syms)
14960 {
14961 struct elf_link_hash_entry *h;
14962 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14963 true, false, false);
14964 if (h == NULL)
14965 return false;
14966 if (h->root.type == bfd_link_hash_new)
14967 {
14968 h->root.type = bfd_link_hash_defined;
14969 h->root.u.def.section = htab->glink;
14970 h->root.u.def.value = 8;
14971 h->ref_regular = 1;
14972 h->def_regular = 1;
14973 h->ref_regular_nonweak = 1;
14974 h->forced_local = 1;
14975 h->non_elf = 0;
14976 h->root.linker_def = 1;
14977 }
14978 }
14979 plt0 = (htab->elf.splt->output_section->vma
14980 + htab->elf.splt->output_offset
14981 - 16);
14982 if (info->emitrelocations)
14983 {
14984 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14985 if (r == NULL)
14986 return false;
14987 r->r_offset = (htab->glink->output_offset
14988 + htab->glink->output_section->vma);
14989 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14990 r->r_addend = plt0;
14991 }
14992 p = htab->glink->contents;
14993 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14994 bfd_put_64 (htab->glink->owner, plt0, p);
14995 p += 8;
14996 if (htab->opd_abi)
14997 {
14998 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14999 p += 4;
15000 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15001 p += 4;
15002 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15003 p += 4;
15004 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15005 p += 4;
15006 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15007 p += 4;
15008 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15009 p += 4;
15010 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15011 p += 4;
15012 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15013 p += 4;
15014 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15015 p += 4;
15016 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15017 p += 4;
15018 }
15019 else
15020 {
15021 unsigned int insn;
15022
15023 /* 0:
15024 . .quad plt0-1f # plt0 entry relative to 1:
15025 #
15026 # We get here with r12 initially @ a glink branch
15027 # Load the address of _dl_runtime_resolve from plt0 and
15028 # jump to it, with r0 set to the index of the PLT entry
15029 # to be resolved and r11 the link map.
15030 __glink_PLTresolve:
15031 . std %r2,24(%r1) # optional
15032 . mflr %r0
15033 . bcl 20,31,1f
15034 1:
15035 . mflr %r11
15036 . mtlr %r0
15037 . ld %r0,(0b-1b)(%r11)
15038 . sub %r12,%r12,%r11
15039 . add %r11,%r0,%r11
15040 . addi %r0,%r12,1b-2f
15041 . ld %r12,0(%r11)
15042 . srdi %r0,%r0,2
15043 . mtctr %r12
15044 . ld %r11,8(%r11)
15045 . bctr
15046 2:
15047 . b __glink_PLTresolve
15048 . ...
15049 . b __glink_PLTresolve */
15050
15051 if (htab->has_plt_localentry0)
15052 {
15053 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15054 p += 4;
15055 }
15056 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15057 p += 4;
15058 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15059 p += 4;
15060 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15061 p += 4;
15062 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15063 p += 4;
15064 if (htab->has_plt_localentry0)
15065 insn = LD_R0_0R11 | (-20 & 0xfffc);
15066 else
15067 insn = LD_R0_0R11 | (-16 & 0xfffc);
15068 bfd_put_32 (htab->glink->owner, insn, p);
15069 p += 4;
15070 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15071 p += 4;
15072 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15073 p += 4;
15074 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15075 p += 4;
15076 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15077 p += 4;
15078 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15079 p += 4;
15080 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15081 p += 4;
15082 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15083 p += 4;
15084 }
15085 bfd_put_32 (htab->glink->owner, BCTR, p);
15086 p += 4;
15087 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15088
15089 /* Build the .glink lazy link call stubs. */
15090 indx = 0;
15091 while (p < htab->glink->contents + htab->glink->size)
15092 {
15093 if (htab->opd_abi)
15094 {
15095 if (indx < 0x8000)
15096 {
15097 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15098 p += 4;
15099 }
15100 else
15101 {
15102 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15103 p += 4;
15104 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15105 p);
15106 p += 4;
15107 }
15108 }
15109 bfd_put_32 (htab->glink->owner,
15110 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15111 indx++;
15112 p += 4;
15113 }
15114 }
15115
15116 if (htab->tga_group != NULL)
15117 {
15118 htab->tga_group->lr_restore = 23 * 4;
15119 htab->tga_group->stub_sec->size = 24 * 4;
15120 if (!emit_tga_desc (htab))
15121 return false;
15122 if (htab->glink_eh_frame != NULL
15123 && htab->glink_eh_frame->size != 0)
15124 {
15125 size_t align = 4;
15126
15127 p = htab->glink_eh_frame->contents;
15128 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15129 p += 17;
15130 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15131 }
15132 }
15133
15134 /* Build .glink global entry stubs, and PLT relocs for globals. */
15135 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15136
15137 if (!write_plt_relocs_for_local_syms (info))
15138 return false;
15139
15140 if (htab->brlt != NULL && htab->brlt->size != 0)
15141 {
15142 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15143 htab->brlt->size);
15144 if (htab->brlt->contents == NULL)
15145 return false;
15146 }
15147 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15148 {
15149 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15150 htab->relbrlt->size);
15151 if (htab->relbrlt->contents == NULL)
15152 return false;
15153 }
15154
15155 /* Build the stubs as directed by the stub hash table. */
15156 htab->stub_id = 0;
15157 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15158
15159 for (group = htab->group; group != NULL; group = group->next)
15160 if (group->needs_save_res)
15161 group->stub_sec->size += htab->sfpr->size;
15162
15163 if (htab->relbrlt != NULL)
15164 htab->relbrlt->reloc_count = 0;
15165
15166 if (htab->params->plt_stub_align != 0)
15167 for (group = htab->group; group != NULL; group = group->next)
15168 if ((stub_sec = group->stub_sec) != NULL)
15169 {
15170 int align = abs (htab->params->plt_stub_align);
15171 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15172 }
15173
15174 for (group = htab->group; group != NULL; group = group->next)
15175 if (group->needs_save_res)
15176 {
15177 stub_sec = group->stub_sec;
15178 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15179 htab->sfpr->contents, htab->sfpr->size);
15180 if (htab->params->emit_stub_syms)
15181 {
15182 unsigned int i;
15183
15184 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15185 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15186 return false;
15187 }
15188 }
15189
15190 if (htab->glink_eh_frame != NULL
15191 && htab->glink_eh_frame->size != 0)
15192 {
15193 bfd_vma val;
15194 size_t align = 4;
15195
15196 p = htab->glink_eh_frame->contents;
15197 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15198
15199 for (group = htab->group; group != NULL; group = group->next)
15200 if (group->eh_size != 0)
15201 {
15202 /* Offset to stub section. */
15203 val = (group->stub_sec->output_section->vma
15204 + group->stub_sec->output_offset);
15205 val -= (htab->glink_eh_frame->output_section->vma
15206 + htab->glink_eh_frame->output_offset
15207 + (p + 8 - htab->glink_eh_frame->contents));
15208 if (val + 0x80000000 > 0xffffffff)
15209 {
15210 _bfd_error_handler
15211 (_("%s offset too large for .eh_frame sdata4 encoding"),
15212 group->stub_sec->name);
15213 return false;
15214 }
15215 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15216 p += (group->eh_size + 17 + 3) & -4;
15217 }
15218 if (htab->glink != NULL && htab->glink->size != 0)
15219 {
15220 /* Offset to .glink. */
15221 val = (htab->glink->output_section->vma
15222 + htab->glink->output_offset
15223 + 8);
15224 val -= (htab->glink_eh_frame->output_section->vma
15225 + htab->glink_eh_frame->output_offset
15226 + (p + 8 - htab->glink_eh_frame->contents));
15227 if (val + 0x80000000 > 0xffffffff)
15228 {
15229 _bfd_error_handler
15230 (_("%s offset too large for .eh_frame sdata4 encoding"),
15231 htab->glink->name);
15232 return false;
15233 }
15234 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15235 p += (24 + align - 1) & -align;
15236 }
15237 }
15238
15239 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15240 {
15241 htab->elf.srelrdyn->contents
15242 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15243 if (htab->elf.srelrdyn->contents == NULL)
15244 return false;
15245
15246 bfd_vma *relr_addr = sort_relr (htab);
15247 if (htab->relr_count != 0 && relr_addr == NULL)
15248 return false;
15249
15250 size_t i = 0;
15251 bfd_byte *loc = htab->elf.srelrdyn->contents;
15252 while (i < htab->relr_count)
15253 {
15254 bfd_vma base = relr_addr[i];
15255 BFD_ASSERT (base % 2 == 0);
15256 bfd_put_64 (htab->elf.dynobj, base, loc);
15257 loc += 8;
15258 i++;
15259 while (i < htab->relr_count
15260 && relr_addr[i] == base)
15261 {
15262 htab->stub_error = true;
15263 i++;
15264 }
15265 base += 8;
15266 while (1)
15267 {
15268 bfd_vma bits = 0;
15269 while (i < htab->relr_count
15270 && relr_addr[i] - base < 63 * 8
15271 && (relr_addr[i] - base) % 8 == 0)
15272 {
15273 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15274 i++;
15275 }
15276 if (bits == 0)
15277 break;
15278 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15279 loc += 8;
15280 base += 63 * 8;
15281 }
15282 }
15283 free (relr_addr);
15284 /* Pad any excess with 1's, a do-nothing encoding. */
15285 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15286 < htab->elf.srelrdyn->size)
15287 {
15288 bfd_put_64 (htab->elf.dynobj, 1, loc);
15289 loc += 8;
15290 }
15291 }
15292
15293 for (group = htab->group; group != NULL; group = group->next)
15294 if ((stub_sec = group->stub_sec) != NULL)
15295 {
15296 stub_sec_count += 1;
15297 if (stub_sec->rawsize != stub_sec->size
15298 && (htab->stub_iteration <= STUB_SHRINK_ITER
15299 || stub_sec->rawsize < stub_sec->size))
15300 break;
15301 }
15302
15303 if (group != NULL)
15304 htab->stub_error = true;
15305
15306 if (htab->stub_error)
15307 {
15308 _bfd_error_handler (_("stubs don't match calculated size"));
15309 return false;
15310 }
15311
15312 if (stats != NULL)
15313 {
15314 char *groupmsg;
15315 if (asprintf (&groupmsg,
15316 ngettext ("linker stubs in %u group\n",
15317 "linker stubs in %u groups\n",
15318 stub_sec_count),
15319 stub_sec_count) < 0)
15320 *stats = NULL;
15321 else
15322 {
15323 if (asprintf (stats, _("%s"
15324 " branch %lu\n"
15325 " long branch %lu\n"
15326 " plt call %lu\n"
15327 " global entry %lu"),
15328 groupmsg,
15329 htab->stub_count[ppc_stub_long_branch - 1],
15330 htab->stub_count[ppc_stub_plt_branch - 1],
15331 htab->stub_count[ppc_stub_plt_call - 1],
15332 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15333 *stats = NULL;
15334 free (groupmsg);
15335 }
15336 }
15337 return true;
15338 }
15339
15340 /* What to do when ld finds relocations against symbols defined in
15341 discarded sections. */
15342
15343 static unsigned int
15344 ppc64_elf_action_discarded (asection *sec)
15345 {
15346 if (strcmp (".opd", sec->name) == 0)
15347 return 0;
15348
15349 if (strcmp (".toc", sec->name) == 0)
15350 return 0;
15351
15352 if (strcmp (".toc1", sec->name) == 0)
15353 return 0;
15354
15355 return _bfd_elf_default_action_discarded (sec);
15356 }
15357
15358 /* These are the dynamic relocations supported by glibc. */
15359
15360 static bool
15361 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15362 {
15363 switch (r_type)
15364 {
15365 case R_PPC64_RELATIVE:
15366 case R_PPC64_NONE:
15367 case R_PPC64_ADDR64:
15368 case R_PPC64_GLOB_DAT:
15369 case R_PPC64_IRELATIVE:
15370 case R_PPC64_JMP_IREL:
15371 case R_PPC64_JMP_SLOT:
15372 case R_PPC64_DTPMOD64:
15373 case R_PPC64_DTPREL64:
15374 case R_PPC64_TPREL64:
15375 case R_PPC64_TPREL16_LO_DS:
15376 case R_PPC64_TPREL16_DS:
15377 case R_PPC64_TPREL16:
15378 case R_PPC64_TPREL16_LO:
15379 case R_PPC64_TPREL16_HI:
15380 case R_PPC64_TPREL16_HIGH:
15381 case R_PPC64_TPREL16_HA:
15382 case R_PPC64_TPREL16_HIGHA:
15383 case R_PPC64_TPREL16_HIGHER:
15384 case R_PPC64_TPREL16_HIGHEST:
15385 case R_PPC64_TPREL16_HIGHERA:
15386 case R_PPC64_TPREL16_HIGHESTA:
15387 case R_PPC64_ADDR16_LO_DS:
15388 case R_PPC64_ADDR16_LO:
15389 case R_PPC64_ADDR16_HI:
15390 case R_PPC64_ADDR16_HIGH:
15391 case R_PPC64_ADDR16_HA:
15392 case R_PPC64_ADDR16_HIGHA:
15393 case R_PPC64_REL30:
15394 case R_PPC64_COPY:
15395 case R_PPC64_UADDR64:
15396 case R_PPC64_UADDR32:
15397 case R_PPC64_ADDR32:
15398 case R_PPC64_ADDR24:
15399 case R_PPC64_ADDR16:
15400 case R_PPC64_UADDR16:
15401 case R_PPC64_ADDR16_DS:
15402 case R_PPC64_ADDR16_HIGHER:
15403 case R_PPC64_ADDR16_HIGHEST:
15404 case R_PPC64_ADDR16_HIGHERA:
15405 case R_PPC64_ADDR16_HIGHESTA:
15406 case R_PPC64_ADDR14:
15407 case R_PPC64_ADDR14_BRTAKEN:
15408 case R_PPC64_ADDR14_BRNTAKEN:
15409 case R_PPC64_REL32:
15410 case R_PPC64_REL64:
15411 return true;
15412
15413 default:
15414 return false;
15415 }
15416 }
15417
15418 /* The RELOCATE_SECTION function is called by the ELF backend linker
15419 to handle the relocations for a section.
15420
15421 The relocs are always passed as Rela structures; if the section
15422 actually uses Rel structures, the r_addend field will always be
15423 zero.
15424
15425 This function is responsible for adjust the section contents as
15426 necessary, and (if using Rela relocs and generating a
15427 relocatable output file) adjusting the reloc addend as
15428 necessary.
15429
15430 This function does not have to worry about setting the reloc
15431 address or the reloc symbol index.
15432
15433 LOCAL_SYMS is a pointer to the swapped in local symbols.
15434
15435 LOCAL_SECTIONS is an array giving the section in the input file
15436 corresponding to the st_shndx field of each local symbol.
15437
15438 The global hash table entry for the global symbols can be found
15439 via elf_sym_hashes (input_bfd).
15440
15441 When generating relocatable output, this function must handle
15442 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15443 going to be the section symbol corresponding to the output
15444 section, which means that the addend must be adjusted
15445 accordingly. */
15446
15447 static int
15448 ppc64_elf_relocate_section (bfd *output_bfd,
15449 struct bfd_link_info *info,
15450 bfd *input_bfd,
15451 asection *input_section,
15452 bfd_byte *contents,
15453 Elf_Internal_Rela *relocs,
15454 Elf_Internal_Sym *local_syms,
15455 asection **local_sections)
15456 {
15457 struct ppc_link_hash_table *htab;
15458 Elf_Internal_Shdr *symtab_hdr;
15459 struct elf_link_hash_entry **sym_hashes;
15460 Elf_Internal_Rela *rel;
15461 Elf_Internal_Rela *wrel;
15462 Elf_Internal_Rela *relend;
15463 Elf_Internal_Rela outrel;
15464 bfd_byte *loc;
15465 struct got_entry **local_got_ents;
15466 bfd_vma TOCstart;
15467 bool ret = true;
15468 bool is_opd;
15469 /* Assume 'at' branch hints. */
15470 bool is_isa_v2 = true;
15471 bool warned_dynamic = false;
15472 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15473
15474 /* Initialize howto table if needed. */
15475 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15476 ppc_howto_init ();
15477
15478 htab = ppc_hash_table (info);
15479 if (htab == NULL)
15480 return false;
15481
15482 /* Don't relocate stub sections. */
15483 if (input_section->owner == htab->params->stub_bfd)
15484 return true;
15485
15486 if (!is_ppc64_elf (input_bfd))
15487 {
15488 bfd_set_error (bfd_error_wrong_format);
15489 return false;
15490 }
15491
15492 local_got_ents = elf_local_got_ents (input_bfd);
15493 TOCstart = elf_gp (output_bfd);
15494 symtab_hdr = &elf_symtab_hdr (input_bfd);
15495 sym_hashes = elf_sym_hashes (input_bfd);
15496 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15497
15498 rel = wrel = relocs;
15499 relend = relocs + input_section->reloc_count;
15500 for (; rel < relend; wrel++, rel++)
15501 {
15502 enum elf_ppc64_reloc_type r_type;
15503 bfd_vma addend;
15504 bfd_reloc_status_type r;
15505 Elf_Internal_Sym *sym;
15506 asection *sec;
15507 struct elf_link_hash_entry *h_elf;
15508 struct ppc_link_hash_entry *h;
15509 struct ppc_link_hash_entry *fdh;
15510 const char *sym_name;
15511 unsigned long r_symndx, toc_symndx;
15512 bfd_vma toc_addend;
15513 unsigned char tls_mask, tls_gd, tls_type;
15514 unsigned char sym_type;
15515 bfd_vma relocation;
15516 bool unresolved_reloc, save_unresolved_reloc;
15517 bool warned;
15518 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15519 unsigned int insn;
15520 unsigned int mask;
15521 struct ppc_stub_hash_entry *stub_entry;
15522 bfd_vma max_br_offset;
15523 bfd_vma from;
15524 Elf_Internal_Rela orig_rel;
15525 reloc_howto_type *howto;
15526 struct reloc_howto_struct alt_howto;
15527 uint64_t pinsn;
15528 bfd_vma offset;
15529
15530 again:
15531 orig_rel = *rel;
15532
15533 r_type = ELF64_R_TYPE (rel->r_info);
15534 r_symndx = ELF64_R_SYM (rel->r_info);
15535
15536 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15537 symbol of the previous ADDR64 reloc. The symbol gives us the
15538 proper TOC base to use. */
15539 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15540 && wrel != relocs
15541 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15542 && is_opd)
15543 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15544
15545 sym = NULL;
15546 sec = NULL;
15547 h_elf = NULL;
15548 sym_name = NULL;
15549 unresolved_reloc = false;
15550 warned = false;
15551
15552 if (r_symndx < symtab_hdr->sh_info)
15553 {
15554 /* It's a local symbol. */
15555 struct _opd_sec_data *opd;
15556
15557 sym = local_syms + r_symndx;
15558 sec = local_sections[r_symndx];
15559 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15560 sym_type = ELF64_ST_TYPE (sym->st_info);
15561 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15562 opd = get_opd_info (sec);
15563 if (opd != NULL && opd->adjust != NULL)
15564 {
15565 long adjust = opd->adjust[OPD_NDX (sym->st_value
15566 + rel->r_addend)];
15567 if (adjust == -1)
15568 relocation = 0;
15569 else
15570 {
15571 /* If this is a relocation against the opd section sym
15572 and we have edited .opd, adjust the reloc addend so
15573 that ld -r and ld --emit-relocs output is correct.
15574 If it is a reloc against some other .opd symbol,
15575 then the symbol value will be adjusted later. */
15576 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15577 rel->r_addend += adjust;
15578 else
15579 relocation += adjust;
15580 }
15581 }
15582 }
15583 else
15584 {
15585 bool ignored;
15586
15587 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15588 r_symndx, symtab_hdr, sym_hashes,
15589 h_elf, sec, relocation,
15590 unresolved_reloc, warned, ignored);
15591 sym_name = h_elf->root.root.string;
15592 sym_type = h_elf->type;
15593 if (sec != NULL
15594 && sec->owner == output_bfd
15595 && strcmp (sec->name, ".opd") == 0)
15596 {
15597 /* This is a symbol defined in a linker script. All
15598 such are defined in output sections, even those
15599 defined by simple assignment from a symbol defined in
15600 an input section. Transfer the symbol to an
15601 appropriate input .opd section, so that a branch to
15602 this symbol will be mapped to the location specified
15603 by the opd entry. */
15604 struct bfd_link_order *lo;
15605 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15606 if (lo->type == bfd_indirect_link_order)
15607 {
15608 asection *isec = lo->u.indirect.section;
15609 if (h_elf->root.u.def.value >= isec->output_offset
15610 && h_elf->root.u.def.value < (isec->output_offset
15611 + isec->size))
15612 {
15613 h_elf->root.u.def.value -= isec->output_offset;
15614 h_elf->root.u.def.section = isec;
15615 sec = isec;
15616 break;
15617 }
15618 }
15619 }
15620 }
15621 h = ppc_elf_hash_entry (h_elf);
15622
15623 if (sec != NULL && discarded_section (sec))
15624 {
15625 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15626 input_bfd, input_section,
15627 contents, rel->r_offset);
15628 wrel->r_offset = rel->r_offset;
15629 wrel->r_info = 0;
15630 wrel->r_addend = 0;
15631
15632 /* For ld -r, remove relocations in debug sections against
15633 symbols defined in discarded sections. Not done for
15634 non-debug to preserve relocs in .eh_frame which the
15635 eh_frame editing code expects to be present. */
15636 if (bfd_link_relocatable (info)
15637 && (input_section->flags & SEC_DEBUGGING))
15638 wrel--;
15639
15640 continue;
15641 }
15642
15643 if (bfd_link_relocatable (info))
15644 goto copy_reloc;
15645
15646 if (h != NULL && &h->elf == htab->elf.hgot)
15647 {
15648 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15649 sec = bfd_abs_section_ptr;
15650 unresolved_reloc = false;
15651 }
15652
15653 /* TLS optimizations. Replace instruction sequences and relocs
15654 based on information we collected in tls_optimize. We edit
15655 RELOCS so that --emit-relocs will output something sensible
15656 for the final instruction stream. */
15657 tls_mask = 0;
15658 tls_gd = 0;
15659 toc_symndx = 0;
15660 if (h != NULL)
15661 tls_mask = h->tls_mask;
15662 else if (local_got_ents != NULL)
15663 {
15664 struct plt_entry **local_plt = (struct plt_entry **)
15665 (local_got_ents + symtab_hdr->sh_info);
15666 unsigned char *lgot_masks = (unsigned char *)
15667 (local_plt + symtab_hdr->sh_info);
15668 tls_mask = lgot_masks[r_symndx];
15669 }
15670 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15671 && (r_type == R_PPC64_TLS
15672 || r_type == R_PPC64_TLSGD
15673 || r_type == R_PPC64_TLSLD))
15674 {
15675 /* Check for toc tls entries. */
15676 unsigned char *toc_tls;
15677
15678 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15679 &local_syms, rel, input_bfd))
15680 return false;
15681
15682 if (toc_tls)
15683 tls_mask = *toc_tls;
15684 }
15685
15686 /* Check that tls relocs are used with tls syms, and non-tls
15687 relocs are used with non-tls syms. */
15688 if (r_symndx != STN_UNDEF
15689 && r_type != R_PPC64_NONE
15690 && (h == NULL
15691 || h->elf.root.type == bfd_link_hash_defined
15692 || h->elf.root.type == bfd_link_hash_defweak)
15693 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15694 {
15695 if ((tls_mask & TLS_TLS) != 0
15696 && (r_type == R_PPC64_TLS
15697 || r_type == R_PPC64_TLSGD
15698 || r_type == R_PPC64_TLSLD))
15699 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15700 ;
15701 else
15702 info->callbacks->einfo
15703 (!IS_PPC64_TLS_RELOC (r_type)
15704 /* xgettext:c-format */
15705 ? _("%H: %s used with TLS symbol `%pT'\n")
15706 /* xgettext:c-format */
15707 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15708 input_bfd, input_section, rel->r_offset,
15709 ppc64_elf_howto_table[r_type]->name,
15710 sym_name);
15711 }
15712
15713 /* Ensure reloc mapping code below stays sane. */
15714 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15715 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15716 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15717 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15718 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15719 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15720 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15721 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15722 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15723 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15724 abort ();
15725
15726 switch (r_type)
15727 {
15728 default:
15729 break;
15730
15731 case R_PPC64_LO_DS_OPT:
15732 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15733 {
15734 insn = bfd_get_32 (input_bfd,
15735 contents + rel->r_offset - d_offset);
15736 if ((insn & (0x3fu << 26)) != 58u << 26)
15737 abort ();
15738 insn += (14u << 26) - (58u << 26);
15739 bfd_put_32 (input_bfd, insn,
15740 contents + rel->r_offset - d_offset);
15741 r_type = R_PPC64_TOC16_LO;
15742 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15743 }
15744 break;
15745
15746 case R_PPC64_TOC16:
15747 case R_PPC64_TOC16_LO:
15748 case R_PPC64_TOC16_DS:
15749 case R_PPC64_TOC16_LO_DS:
15750 {
15751 /* Check for toc tls entries. */
15752 unsigned char *toc_tls;
15753 int retval;
15754
15755 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15756 &local_syms, rel, input_bfd);
15757 if (retval == 0)
15758 return false;
15759
15760 if (toc_tls)
15761 {
15762 tls_mask = *toc_tls;
15763 if (r_type == R_PPC64_TOC16_DS
15764 || r_type == R_PPC64_TOC16_LO_DS)
15765 {
15766 if ((tls_mask & TLS_TLS) != 0
15767 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15768 goto toctprel;
15769 }
15770 else
15771 {
15772 /* If we found a GD reloc pair, then we might be
15773 doing a GD->IE transition. */
15774 if (retval == 2)
15775 {
15776 tls_gd = TLS_GDIE;
15777 if ((tls_mask & TLS_TLS) != 0
15778 && (tls_mask & TLS_GD) == 0)
15779 goto tls_ldgd_opt;
15780 }
15781 else if (retval == 3)
15782 {
15783 if ((tls_mask & TLS_TLS) != 0
15784 && (tls_mask & TLS_LD) == 0)
15785 goto tls_ldgd_opt;
15786 }
15787 }
15788 }
15789 }
15790 break;
15791
15792 case R_PPC64_GOT_TPREL16_HI:
15793 case R_PPC64_GOT_TPREL16_HA:
15794 if ((tls_mask & TLS_TLS) != 0
15795 && (tls_mask & TLS_TPREL) == 0
15796 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15797 {
15798 rel->r_offset -= d_offset;
15799 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15800 r_type = R_PPC64_NONE;
15801 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15802 }
15803 break;
15804
15805 case R_PPC64_GOT_TPREL16_DS:
15806 case R_PPC64_GOT_TPREL16_LO_DS:
15807 if ((tls_mask & TLS_TLS) != 0
15808 && (tls_mask & TLS_TPREL) == 0
15809 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15810 {
15811 toctprel:
15812 insn = bfd_get_32 (input_bfd,
15813 contents + rel->r_offset - d_offset);
15814 insn &= 31 << 21;
15815 insn |= 0x3c0d0000; /* addis 0,13,0 */
15816 bfd_put_32 (input_bfd, insn,
15817 contents + rel->r_offset - d_offset);
15818 r_type = R_PPC64_TPREL16_HA;
15819 if (toc_symndx != 0)
15820 {
15821 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15822 rel->r_addend = toc_addend;
15823 /* We changed the symbol. Start over in order to
15824 get h, sym, sec etc. right. */
15825 goto again;
15826 }
15827 else
15828 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15829 }
15830 break;
15831
15832 case R_PPC64_GOT_TPREL_PCREL34:
15833 if ((tls_mask & TLS_TLS) != 0
15834 && (tls_mask & TLS_TPREL) == 0
15835 && offset_in_range (input_section, rel->r_offset, 8))
15836 {
15837 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15838 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15839 pinsn <<= 32;
15840 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15841 pinsn += ((2ULL << 56) + (-1ULL << 52)
15842 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15843 bfd_put_32 (input_bfd, pinsn >> 32,
15844 contents + rel->r_offset);
15845 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15846 contents + rel->r_offset + 4);
15847 r_type = R_PPC64_TPREL34;
15848 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15849 }
15850 break;
15851
15852 case R_PPC64_TLS:
15853 if ((tls_mask & TLS_TLS) != 0
15854 && (tls_mask & TLS_TPREL) == 0
15855 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15856 {
15857 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15858 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15859 if (insn == 0)
15860 break;
15861 if ((rel->r_offset & 3) == 0)
15862 {
15863 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15864 /* Was PPC64_TLS which sits on insn boundary, now
15865 PPC64_TPREL16_LO which is at low-order half-word. */
15866 rel->r_offset += d_offset;
15867 r_type = R_PPC64_TPREL16_LO;
15868 if (toc_symndx != 0)
15869 {
15870 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15871 rel->r_addend = toc_addend;
15872 /* We changed the symbol. Start over in order to
15873 get h, sym, sec etc. right. */
15874 goto again;
15875 }
15876 else
15877 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15878 }
15879 else if ((rel->r_offset & 3) == 1)
15880 {
15881 /* For pcrel IE to LE we already have the full
15882 offset and thus don't need an addi here. A nop
15883 or mr will do. */
15884 if ((insn & (0x3fu << 26)) == 14 << 26)
15885 {
15886 /* Extract regs from addi rt,ra,si. */
15887 unsigned int rt = (insn >> 21) & 0x1f;
15888 unsigned int ra = (insn >> 16) & 0x1f;
15889 if (rt == ra)
15890 insn = NOP;
15891 else
15892 {
15893 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15894 insn = (rt << 16) | (ra << 21) | (ra << 11);
15895 insn |= (31u << 26) | (444u << 1);
15896 }
15897 }
15898 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15899 }
15900 }
15901 break;
15902
15903 case R_PPC64_GOT_TLSGD16_HI:
15904 case R_PPC64_GOT_TLSGD16_HA:
15905 tls_gd = TLS_GDIE;
15906 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15907 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15908 goto tls_gdld_hi;
15909 break;
15910
15911 case R_PPC64_GOT_TLSLD16_HI:
15912 case R_PPC64_GOT_TLSLD16_HA:
15913 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15914 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15915 {
15916 tls_gdld_hi:
15917 if ((tls_mask & tls_gd) != 0)
15918 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15919 + R_PPC64_GOT_TPREL16_DS);
15920 else
15921 {
15922 rel->r_offset -= d_offset;
15923 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15924 r_type = R_PPC64_NONE;
15925 }
15926 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15927 }
15928 break;
15929
15930 case R_PPC64_GOT_TLSGD16:
15931 case R_PPC64_GOT_TLSGD16_LO:
15932 tls_gd = TLS_GDIE;
15933 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15934 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15935 goto tls_ldgd_opt;
15936 break;
15937
15938 case R_PPC64_GOT_TLSLD16:
15939 case R_PPC64_GOT_TLSLD16_LO:
15940 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15941 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15942 {
15943 unsigned int insn1, insn2;
15944
15945 tls_ldgd_opt:
15946 offset = (bfd_vma) -1;
15947 /* If not using the newer R_PPC64_TLSGD/LD to mark
15948 __tls_get_addr calls, we must trust that the call
15949 stays with its arg setup insns, ie. that the next
15950 reloc is the __tls_get_addr call associated with
15951 the current reloc. Edit both insns. */
15952 if (input_section->nomark_tls_get_addr
15953 && rel + 1 < relend
15954 && branch_reloc_hash_match (input_bfd, rel + 1,
15955 htab->tls_get_addr_fd,
15956 htab->tga_desc_fd,
15957 htab->tls_get_addr,
15958 htab->tga_desc))
15959 offset = rel[1].r_offset;
15960 /* We read the low GOT_TLS (or TOC16) insn because we
15961 need to keep the destination reg. It may be
15962 something other than the usual r3, and moved to r3
15963 before the call by intervening code. */
15964 insn1 = bfd_get_32 (input_bfd,
15965 contents + rel->r_offset - d_offset);
15966 if ((tls_mask & tls_gd) != 0)
15967 {
15968 /* IE */
15969 insn1 &= (0x1f << 21) | (0x1f << 16);
15970 insn1 |= 58u << 26; /* ld */
15971 insn2 = 0x7c636a14; /* add 3,3,13 */
15972 if (offset != (bfd_vma) -1)
15973 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15974 if (r_type == R_PPC64_TOC16
15975 || r_type == R_PPC64_TOC16_LO)
15976 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15977 else
15978 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15979 + R_PPC64_GOT_TPREL16_DS);
15980 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15981 }
15982 else
15983 {
15984 /* LE */
15985 insn1 &= 0x1f << 21;
15986 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15987 insn2 = 0x38630000; /* addi 3,3,0 */
15988 if (tls_gd == 0)
15989 {
15990 /* Was an LD reloc. */
15991 r_symndx = STN_UNDEF;
15992 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15993 }
15994 else if (toc_symndx != 0)
15995 {
15996 r_symndx = toc_symndx;
15997 rel->r_addend = toc_addend;
15998 }
15999 r_type = R_PPC64_TPREL16_HA;
16000 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16001 if (offset != (bfd_vma) -1)
16002 {
16003 rel[1].r_info = ELF64_R_INFO (r_symndx,
16004 R_PPC64_TPREL16_LO);
16005 rel[1].r_offset = offset + d_offset;
16006 rel[1].r_addend = rel->r_addend;
16007 }
16008 }
16009 bfd_put_32 (input_bfd, insn1,
16010 contents + rel->r_offset - d_offset);
16011 if (offset != (bfd_vma) -1
16012 && offset_in_range (input_section, offset, 4))
16013 {
16014 bfd_put_32 (input_bfd, insn2, contents + offset);
16015 if (offset_in_range (input_section, offset + 4, 4))
16016 {
16017 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16018 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16019 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16020 }
16021 }
16022 if ((tls_mask & tls_gd) == 0
16023 && (tls_gd == 0 || toc_symndx != 0))
16024 {
16025 /* We changed the symbol. Start over in order
16026 to get h, sym, sec etc. right. */
16027 goto again;
16028 }
16029 }
16030 break;
16031
16032 case R_PPC64_GOT_TLSGD_PCREL34:
16033 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16034 && offset_in_range (input_section, rel->r_offset, 8))
16035 {
16036 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16037 pinsn <<= 32;
16038 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16039 if ((tls_mask & TLS_GDIE) != 0)
16040 {
16041 /* IE, pla -> pld */
16042 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16043 r_type = R_PPC64_GOT_TPREL_PCREL34;
16044 }
16045 else
16046 {
16047 /* LE, pla pcrel -> paddi r13 */
16048 pinsn += (-1ULL << 52) + (13ULL << 16);
16049 r_type = R_PPC64_TPREL34;
16050 }
16051 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16052 bfd_put_32 (input_bfd, pinsn >> 32,
16053 contents + rel->r_offset);
16054 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16055 contents + rel->r_offset + 4);
16056 }
16057 break;
16058
16059 case R_PPC64_GOT_TLSLD_PCREL34:
16060 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16061 && offset_in_range (input_section, rel->r_offset, 8))
16062 {
16063 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16064 pinsn <<= 32;
16065 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16066 pinsn += (-1ULL << 52) + (13ULL << 16);
16067 bfd_put_32 (input_bfd, pinsn >> 32,
16068 contents + rel->r_offset);
16069 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16070 contents + rel->r_offset + 4);
16071 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16072 r_symndx = STN_UNDEF;
16073 r_type = R_PPC64_TPREL34;
16074 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16075 goto again;
16076 }
16077 break;
16078
16079 case R_PPC64_TLSGD:
16080 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16081 && rel + 1 < relend
16082 && offset_in_range (input_section, rel->r_offset,
16083 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16084 ? 8 : 4))
16085 {
16086 unsigned int insn2;
16087 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16088
16089 offset = rel->r_offset;
16090 if (is_plt_seq_reloc (r_type1))
16091 {
16092 bfd_put_32 (output_bfd, NOP, contents + offset);
16093 if (r_type1 == R_PPC64_PLT_PCREL34
16094 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16095 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16096 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16097 break;
16098 }
16099
16100 if (r_type1 == R_PPC64_PLTCALL)
16101 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16102
16103 if ((tls_mask & TLS_GDIE) != 0)
16104 {
16105 /* IE */
16106 r_type = R_PPC64_NONE;
16107 insn2 = 0x7c636a14; /* add 3,3,13 */
16108 }
16109 else
16110 {
16111 /* LE */
16112 if (toc_symndx != 0)
16113 {
16114 r_symndx = toc_symndx;
16115 rel->r_addend = toc_addend;
16116 }
16117 if (r_type1 == R_PPC64_REL24_NOTOC
16118 || r_type1 == R_PPC64_REL24_P9NOTOC
16119 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16120 {
16121 r_type = R_PPC64_NONE;
16122 insn2 = NOP;
16123 }
16124 else
16125 {
16126 rel->r_offset = offset + d_offset;
16127 r_type = R_PPC64_TPREL16_LO;
16128 insn2 = 0x38630000; /* addi 3,3,0 */
16129 }
16130 }
16131 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16132 /* Zap the reloc on the _tls_get_addr call too. */
16133 BFD_ASSERT (offset == rel[1].r_offset);
16134 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16135 bfd_put_32 (input_bfd, insn2, contents + offset);
16136 if ((tls_mask & TLS_GDIE) == 0
16137 && toc_symndx != 0
16138 && r_type != R_PPC64_NONE)
16139 goto again;
16140 }
16141 break;
16142
16143 case R_PPC64_TLSLD:
16144 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16145 && rel + 1 < relend
16146 && offset_in_range (input_section, rel->r_offset,
16147 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16148 ? 8 : 4))
16149 {
16150 unsigned int insn2;
16151 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16152
16153 offset = rel->r_offset;
16154 if (is_plt_seq_reloc (r_type1))
16155 {
16156 bfd_put_32 (output_bfd, NOP, contents + offset);
16157 if (r_type1 == R_PPC64_PLT_PCREL34
16158 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16159 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16160 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16161 break;
16162 }
16163
16164 if (r_type1 == R_PPC64_PLTCALL)
16165 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16166
16167 if (r_type1 == R_PPC64_REL24_NOTOC
16168 || r_type1 == R_PPC64_REL24_P9NOTOC
16169 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16170 {
16171 r_type = R_PPC64_NONE;
16172 insn2 = NOP;
16173 }
16174 else
16175 {
16176 rel->r_offset = offset + d_offset;
16177 r_symndx = STN_UNDEF;
16178 r_type = R_PPC64_TPREL16_LO;
16179 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16180 insn2 = 0x38630000; /* addi 3,3,0 */
16181 }
16182 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16183 /* Zap the reloc on the _tls_get_addr call too. */
16184 BFD_ASSERT (offset == rel[1].r_offset);
16185 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16186 bfd_put_32 (input_bfd, insn2, contents + offset);
16187 if (r_type != R_PPC64_NONE)
16188 goto again;
16189 }
16190 break;
16191
16192 case R_PPC64_DTPMOD64:
16193 if (rel + 1 < relend
16194 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16195 && rel[1].r_offset == rel->r_offset + 8)
16196 {
16197 if ((tls_mask & TLS_GD) == 0
16198 && offset_in_range (input_section, rel->r_offset, 8))
16199 {
16200 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16201 if ((tls_mask & TLS_GDIE) != 0)
16202 r_type = R_PPC64_TPREL64;
16203 else
16204 {
16205 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16206 r_type = R_PPC64_NONE;
16207 }
16208 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16209 }
16210 }
16211 else
16212 {
16213 if ((tls_mask & TLS_LD) == 0
16214 && offset_in_range (input_section, rel->r_offset, 8))
16215 {
16216 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16217 r_type = R_PPC64_NONE;
16218 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16219 }
16220 }
16221 break;
16222
16223 case R_PPC64_TPREL64:
16224 if ((tls_mask & TLS_TPREL) == 0)
16225 {
16226 r_type = R_PPC64_NONE;
16227 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16228 }
16229 break;
16230
16231 case R_PPC64_ENTRY:
16232 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16233 if (!bfd_link_pic (info)
16234 && !info->traditional_format
16235 && relocation + 0x80008000 <= 0xffffffff
16236 && offset_in_range (input_section, rel->r_offset, 8))
16237 {
16238 unsigned int insn1, insn2;
16239
16240 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16241 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16242 if ((insn1 & ~0xfffc) == LD_R2_0R12
16243 && insn2 == ADD_R2_R2_R12)
16244 {
16245 bfd_put_32 (input_bfd,
16246 LIS_R2 + PPC_HA (relocation),
16247 contents + rel->r_offset);
16248 bfd_put_32 (input_bfd,
16249 ADDI_R2_R2 + PPC_LO (relocation),
16250 contents + rel->r_offset + 4);
16251 }
16252 }
16253 else
16254 {
16255 relocation -= (rel->r_offset
16256 + input_section->output_offset
16257 + input_section->output_section->vma);
16258 if (relocation + 0x80008000 <= 0xffffffff
16259 && offset_in_range (input_section, rel->r_offset, 8))
16260 {
16261 unsigned int insn1, insn2;
16262
16263 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16264 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16265 if ((insn1 & ~0xfffc) == LD_R2_0R12
16266 && insn2 == ADD_R2_R2_R12)
16267 {
16268 bfd_put_32 (input_bfd,
16269 ADDIS_R2_R12 + PPC_HA (relocation),
16270 contents + rel->r_offset);
16271 bfd_put_32 (input_bfd,
16272 ADDI_R2_R2 + PPC_LO (relocation),
16273 contents + rel->r_offset + 4);
16274 }
16275 }
16276 }
16277 break;
16278
16279 case R_PPC64_REL16_HA:
16280 /* If we are generating a non-PIC executable, edit
16281 . 0: addis 2,12,.TOC.-0b@ha
16282 . addi 2,2,.TOC.-0b@l
16283 used by ELFv2 global entry points to set up r2, to
16284 . lis 2,.TOC.@ha
16285 . addi 2,2,.TOC.@l
16286 if .TOC. is in range. */
16287 if (!bfd_link_pic (info)
16288 && !info->traditional_format
16289 && !htab->opd_abi
16290 && rel->r_addend == d_offset
16291 && h != NULL && &h->elf == htab->elf.hgot
16292 && rel + 1 < relend
16293 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16294 && rel[1].r_offset == rel->r_offset + 4
16295 && rel[1].r_addend == rel->r_addend + 4
16296 && relocation + 0x80008000 <= 0xffffffff
16297 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16298 {
16299 unsigned int insn1, insn2;
16300 offset = rel->r_offset - d_offset;
16301 insn1 = bfd_get_32 (input_bfd, contents + offset);
16302 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16303 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16304 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16305 {
16306 r_type = R_PPC64_ADDR16_HA;
16307 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16308 rel->r_addend -= d_offset;
16309 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16310 rel[1].r_addend -= d_offset + 4;
16311 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16312 }
16313 }
16314 break;
16315 }
16316
16317 /* Handle other relocations that tweak non-addend part of insn. */
16318 insn = 0;
16319 max_br_offset = 1 << 25;
16320 addend = rel->r_addend;
16321 reloc_dest = DEST_NORMAL;
16322 switch (r_type)
16323 {
16324 default:
16325 break;
16326
16327 case R_PPC64_TOCSAVE:
16328 if (relocation + addend == (rel->r_offset
16329 + input_section->output_offset
16330 + input_section->output_section->vma)
16331 && tocsave_find (htab, NO_INSERT,
16332 &local_syms, rel, input_bfd)
16333 && offset_in_range (input_section, rel->r_offset, 4))
16334 {
16335 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16336 if (insn == NOP
16337 || insn == CROR_151515 || insn == CROR_313131)
16338 bfd_put_32 (input_bfd,
16339 STD_R2_0R1 + STK_TOC (htab),
16340 contents + rel->r_offset);
16341 }
16342 break;
16343
16344 /* Branch taken prediction relocations. */
16345 case R_PPC64_ADDR14_BRTAKEN:
16346 case R_PPC64_REL14_BRTAKEN:
16347 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16348 /* Fall through. */
16349
16350 /* Branch not taken prediction relocations. */
16351 case R_PPC64_ADDR14_BRNTAKEN:
16352 case R_PPC64_REL14_BRNTAKEN:
16353 if (!offset_in_range (input_section, rel->r_offset, 4))
16354 break;
16355 insn |= bfd_get_32 (input_bfd,
16356 contents + rel->r_offset) & ~(0x01 << 21);
16357 /* Fall through. */
16358
16359 case R_PPC64_REL14:
16360 max_br_offset = 1 << 15;
16361 /* Fall through. */
16362
16363 case R_PPC64_REL24:
16364 case R_PPC64_REL24_NOTOC:
16365 case R_PPC64_REL24_P9NOTOC:
16366 case R_PPC64_PLTCALL:
16367 case R_PPC64_PLTCALL_NOTOC:
16368 /* Calls to functions with a different TOC, such as calls to
16369 shared objects, need to alter the TOC pointer. This is
16370 done using a linkage stub. A REL24 branching to these
16371 linkage stubs needs to be followed by a nop, as the nop
16372 will be replaced with an instruction to restore the TOC
16373 base pointer. */
16374 fdh = h;
16375 if (h != NULL
16376 && h->oh != NULL
16377 && h->oh->is_func_descriptor)
16378 fdh = ppc_follow_link (h->oh);
16379 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16380 htab);
16381 if ((r_type == R_PPC64_PLTCALL
16382 || r_type == R_PPC64_PLTCALL_NOTOC)
16383 && stub_entry != NULL
16384 && stub_entry->type.main == ppc_stub_plt_call)
16385 stub_entry = NULL;
16386
16387 if (stub_entry != NULL
16388 && (stub_entry->type.main == ppc_stub_plt_call
16389 || stub_entry->type.r2save))
16390 {
16391 bool can_plt_call = false;
16392
16393 if (r_type == R_PPC64_REL24_NOTOC
16394 || r_type == R_PPC64_REL24_P9NOTOC)
16395 {
16396 /* NOTOC calls don't need to restore r2. */
16397 can_plt_call = true;
16398 }
16399 else if (stub_entry->type.main == ppc_stub_plt_call
16400 && !htab->opd_abi
16401 && htab->params->plt_localentry0 != 0
16402 && h != NULL
16403 && is_elfv2_localentry0 (&h->elf))
16404 {
16405 /* The function doesn't use or change r2. */
16406 can_plt_call = true;
16407 }
16408
16409 /* All of these stubs may modify r2, so there must be a
16410 branch and link followed by a nop. The nop is
16411 replaced by an insn to restore r2. */
16412 else if (offset_in_range (input_section, rel->r_offset, 8))
16413 {
16414 unsigned long br;
16415
16416 br = bfd_get_32 (input_bfd,
16417 contents + rel->r_offset);
16418 if ((br & 1) != 0)
16419 {
16420 unsigned long nop;
16421
16422 nop = bfd_get_32 (input_bfd,
16423 contents + rel->r_offset + 4);
16424 if (nop == LD_R2_0R1 + STK_TOC (htab))
16425 can_plt_call = true;
16426 else if (nop == NOP
16427 || nop == CROR_151515
16428 || nop == CROR_313131)
16429 {
16430 if (h != NULL
16431 && is_tls_get_addr (&h->elf, htab)
16432 && htab->params->tls_get_addr_opt)
16433 {
16434 /* Special stub used, leave nop alone. */
16435 }
16436 else
16437 bfd_put_32 (input_bfd,
16438 LD_R2_0R1 + STK_TOC (htab),
16439 contents + rel->r_offset + 4);
16440 can_plt_call = true;
16441 }
16442 }
16443 }
16444
16445 if (!can_plt_call && h != NULL)
16446 {
16447 const char *name = h->elf.root.root.string;
16448
16449 if (*name == '.')
16450 ++name;
16451
16452 if (startswith (name, "__libc_start_main")
16453 && (name[17] == 0 || name[17] == '@'))
16454 {
16455 /* Allow crt1 branch to go via a toc adjusting
16456 stub. Other calls that never return could do
16457 the same, if we could detect such. */
16458 can_plt_call = true;
16459 }
16460 }
16461
16462 if (!can_plt_call)
16463 {
16464 /* g++ as of 20130507 emits self-calls without a
16465 following nop. This is arguably wrong since we
16466 have conflicting information. On the one hand a
16467 global symbol and on the other a local call
16468 sequence, but don't error for this special case.
16469 It isn't possible to cheaply verify we have
16470 exactly such a call. Allow all calls to the same
16471 section. */
16472 asection *code_sec = sec;
16473
16474 if (get_opd_info (sec) != NULL)
16475 {
16476 bfd_vma off = (relocation + addend
16477 - sec->output_section->vma
16478 - sec->output_offset);
16479
16480 opd_entry_value (sec, off, &code_sec, NULL, false);
16481 }
16482 if (code_sec == input_section)
16483 can_plt_call = true;
16484 }
16485
16486 if (!can_plt_call)
16487 {
16488 if (stub_entry->type.main == ppc_stub_plt_call)
16489 info->callbacks->einfo
16490 /* xgettext:c-format */
16491 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16492 "(plt call stub)\n"),
16493 input_bfd, input_section, rel->r_offset, sym_name);
16494 else
16495 info->callbacks->einfo
16496 /* xgettext:c-format */
16497 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16498 "(toc save/adjust stub)\n"),
16499 input_bfd, input_section, rel->r_offset, sym_name);
16500
16501 bfd_set_error (bfd_error_bad_value);
16502 ret = false;
16503 }
16504
16505 if (can_plt_call
16506 && stub_entry->type.main == ppc_stub_plt_call)
16507 unresolved_reloc = false;
16508 }
16509
16510 if ((stub_entry == NULL
16511 || stub_entry->type.main == ppc_stub_long_branch
16512 || stub_entry->type.main == ppc_stub_plt_branch)
16513 && get_opd_info (sec) != NULL)
16514 {
16515 /* The branch destination is the value of the opd entry. */
16516 bfd_vma off = (relocation + addend
16517 - sec->output_section->vma
16518 - sec->output_offset);
16519 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16520 if (dest != (bfd_vma) -1)
16521 {
16522 relocation = dest;
16523 addend = 0;
16524 reloc_dest = DEST_OPD;
16525 }
16526 }
16527
16528 /* If the branch is out of reach we ought to have a long
16529 branch stub. */
16530 from = (rel->r_offset
16531 + input_section->output_offset
16532 + input_section->output_section->vma);
16533
16534 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16535 ? fdh->elf.other
16536 : sym->st_other);
16537
16538 if (stub_entry != NULL
16539 && (stub_entry->type.main == ppc_stub_long_branch
16540 || stub_entry->type.main == ppc_stub_plt_branch))
16541 {
16542 if (stub_entry->type.sub == ppc_stub_toc
16543 && !stub_entry->type.r2save
16544 && (r_type == R_PPC64_ADDR14_BRTAKEN
16545 || r_type == R_PPC64_ADDR14_BRNTAKEN
16546 || (relocation + addend - from + max_br_offset
16547 < 2 * max_br_offset)))
16548 /* Don't use the stub if this branch is in range. */
16549 stub_entry = NULL;
16550
16551 if (stub_entry != NULL
16552 && stub_entry->type.sub >= ppc_stub_notoc
16553 && ((r_type != R_PPC64_REL24_NOTOC
16554 && r_type != R_PPC64_REL24_P9NOTOC)
16555 || ((fdh ? fdh->elf.other : sym->st_other)
16556 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16557 && (relocation + addend - from + max_br_offset
16558 < 2 * max_br_offset))
16559 stub_entry = NULL;
16560
16561 if (stub_entry != NULL
16562 && stub_entry->type.r2save
16563 && (r_type == R_PPC64_REL24_NOTOC
16564 || r_type == R_PPC64_REL24_P9NOTOC)
16565 && (relocation + addend - from + max_br_offset
16566 < 2 * max_br_offset))
16567 stub_entry = NULL;
16568 }
16569
16570 if (stub_entry != NULL)
16571 {
16572 /* Munge up the value and addend so that we call the stub
16573 rather than the procedure directly. */
16574 asection *stub_sec = stub_entry->group->stub_sec;
16575
16576 if (stub_entry->type.main == ppc_stub_save_res)
16577 relocation += (stub_sec->output_offset
16578 + stub_sec->output_section->vma
16579 + stub_sec->size - htab->sfpr->size
16580 - htab->sfpr->output_offset
16581 - htab->sfpr->output_section->vma);
16582 else
16583 relocation = (stub_entry->stub_offset
16584 + stub_sec->output_offset
16585 + stub_sec->output_section->vma);
16586 addend = 0;
16587 reloc_dest = DEST_STUB;
16588
16589 if (((stub_entry->type.r2save
16590 && (r_type == R_PPC64_REL24_NOTOC
16591 || r_type == R_PPC64_REL24_P9NOTOC))
16592 || ((stub_entry->type.main == ppc_stub_plt_call
16593 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16594 && rel + 1 < relend
16595 && rel[1].r_offset == rel->r_offset + 4
16596 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16597 && !(stub_entry->type.main == ppc_stub_plt_call
16598 && htab->params->tls_get_addr_opt
16599 && h != NULL
16600 && is_tls_get_addr (&h->elf, htab)))
16601 {
16602 /* Skip over the r2 store at the start of the stub. */
16603 relocation += 4;
16604 }
16605
16606 if ((r_type == R_PPC64_REL24_NOTOC
16607 || r_type == R_PPC64_REL24_P9NOTOC)
16608 && stub_entry->type.main == ppc_stub_plt_call
16609 && stub_entry->type.sub >= ppc_stub_notoc)
16610 htab->notoc_plt = 1;
16611 }
16612
16613 if (insn != 0)
16614 {
16615 if (is_isa_v2)
16616 {
16617 /* Set 'a' bit. This is 0b00010 in BO field for branch
16618 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16619 for branch on CTR insns (BO == 1a00t or 1a01t). */
16620 if ((insn & (0x14 << 21)) == (0x04 << 21))
16621 insn |= 0x02 << 21;
16622 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16623 insn |= 0x08 << 21;
16624 else
16625 break;
16626 }
16627 else
16628 {
16629 /* Invert 'y' bit if not the default. */
16630 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16631 insn ^= 0x01 << 21;
16632 }
16633
16634 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16635 }
16636
16637 /* NOP out calls to undefined weak functions.
16638 We can thus call a weak function without first
16639 checking whether the function is defined. */
16640 else if (h != NULL
16641 && h->elf.root.type == bfd_link_hash_undefweak
16642 && h->elf.dynindx == -1
16643 && (r_type == R_PPC64_REL24
16644 || r_type == R_PPC64_REL24_NOTOC
16645 || r_type == R_PPC64_REL24_P9NOTOC)
16646 && relocation == 0
16647 && addend == 0
16648 && offset_in_range (input_section, rel->r_offset, 4))
16649 {
16650 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16651 goto copy_reloc;
16652 }
16653 break;
16654
16655 case R_PPC64_GOT16_DS:
16656 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16657 || (bfd_link_pic (info)
16658 && sec == bfd_abs_section_ptr)
16659 || !htab->do_toc_opt)
16660 break;
16661 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16662 if (relocation + addend - from + 0x8000 < 0x10000
16663 && sec != NULL
16664 && sec->output_section != NULL
16665 && !discarded_section (sec)
16666 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16667 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16668 {
16669 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16670 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16671 {
16672 insn += (14u << 26) - (58u << 26);
16673 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16674 r_type = R_PPC64_TOC16;
16675 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16676 }
16677 }
16678 break;
16679
16680 case R_PPC64_GOT16_LO_DS:
16681 case R_PPC64_GOT16_HA:
16682 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16683 || (bfd_link_pic (info)
16684 && sec == bfd_abs_section_ptr)
16685 || !htab->do_toc_opt)
16686 break;
16687 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16688 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16689 && sec != NULL
16690 && sec->output_section != NULL
16691 && !discarded_section (sec)
16692 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16693 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16694 {
16695 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16696 if (r_type == R_PPC64_GOT16_LO_DS
16697 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16698 {
16699 insn += (14u << 26) - (58u << 26);
16700 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16701 r_type = R_PPC64_TOC16_LO;
16702 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16703 }
16704 else if (r_type == R_PPC64_GOT16_HA
16705 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16706 {
16707 r_type = R_PPC64_TOC16_HA;
16708 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16709 }
16710 }
16711 break;
16712
16713 case R_PPC64_GOT_PCREL34:
16714 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16715 || (bfd_link_pic (info)
16716 && sec == bfd_abs_section_ptr)
16717 || !htab->do_toc_opt)
16718 break;
16719 from = (rel->r_offset
16720 + input_section->output_section->vma
16721 + input_section->output_offset);
16722 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16723 && sec != NULL
16724 && sec->output_section != NULL
16725 && !discarded_section (sec)
16726 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16727 && offset_in_range (input_section, rel->r_offset, 8)))
16728 break;
16729
16730 offset = rel->r_offset;
16731 pinsn = bfd_get_32 (input_bfd, contents + offset);
16732 pinsn <<= 32;
16733 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16734 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16735 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16736 break;
16737
16738 /* Replace with paddi. */
16739 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16740 r_type = R_PPC64_PCREL34;
16741 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16742 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16743 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16744 /* Fall through. */
16745
16746 case R_PPC64_PCREL34:
16747 if (!htab->params->no_pcrel_opt
16748 && rel + 1 < relend
16749 && rel[1].r_offset == rel->r_offset
16750 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16751 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16752 && offset_in_range (input_section, rel->r_offset, 8))
16753 {
16754 offset = rel->r_offset;
16755 pinsn = bfd_get_32 (input_bfd, contents + offset);
16756 pinsn <<= 32;
16757 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16758 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16759 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16760 | (14ULL << 26) /* paddi */))
16761 {
16762 bfd_vma off2 = rel[1].r_addend;
16763 if (off2 == 0)
16764 /* zero means next insn. */
16765 off2 = 8;
16766 off2 += offset;
16767 if (offset_in_range (input_section, off2, 4))
16768 {
16769 uint64_t pinsn2;
16770 bfd_signed_vma addend_off;
16771 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16772 pinsn2 <<= 32;
16773 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16774 {
16775 if (!offset_in_range (input_section, off2, 8))
16776 break;
16777 pinsn2 |= bfd_get_32 (input_bfd,
16778 contents + off2 + 4);
16779 }
16780 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16781 {
16782 addend += addend_off;
16783 rel->r_addend = addend;
16784 bfd_put_32 (input_bfd, pinsn >> 32,
16785 contents + offset);
16786 bfd_put_32 (input_bfd, pinsn,
16787 contents + offset + 4);
16788 bfd_put_32 (input_bfd, pinsn2 >> 32,
16789 contents + off2);
16790 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16791 bfd_put_32 (input_bfd, pinsn2,
16792 contents + off2 + 4);
16793 }
16794 }
16795 }
16796 }
16797 break;
16798 }
16799
16800 tls_type = 0;
16801 save_unresolved_reloc = unresolved_reloc;
16802 switch (r_type)
16803 {
16804 default:
16805 /* xgettext:c-format */
16806 _bfd_error_handler (_("%pB: %s unsupported"),
16807 input_bfd, ppc64_elf_howto_table[r_type]->name);
16808
16809 bfd_set_error (bfd_error_bad_value);
16810 ret = false;
16811 goto copy_reloc;
16812
16813 case R_PPC64_NONE:
16814 case R_PPC64_TLS:
16815 case R_PPC64_TLSGD:
16816 case R_PPC64_TLSLD:
16817 case R_PPC64_TOCSAVE:
16818 case R_PPC64_GNU_VTINHERIT:
16819 case R_PPC64_GNU_VTENTRY:
16820 case R_PPC64_ENTRY:
16821 case R_PPC64_PCREL_OPT:
16822 goto copy_reloc;
16823
16824 /* GOT16 relocations. Like an ADDR16 using the symbol's
16825 address in the GOT as relocation value instead of the
16826 symbol's value itself. Also, create a GOT entry for the
16827 symbol and put the symbol value there. */
16828 case R_PPC64_GOT_TLSGD16:
16829 case R_PPC64_GOT_TLSGD16_LO:
16830 case R_PPC64_GOT_TLSGD16_HI:
16831 case R_PPC64_GOT_TLSGD16_HA:
16832 case R_PPC64_GOT_TLSGD_PCREL34:
16833 tls_type = TLS_TLS | TLS_GD;
16834 goto dogot;
16835
16836 case R_PPC64_GOT_TLSLD16:
16837 case R_PPC64_GOT_TLSLD16_LO:
16838 case R_PPC64_GOT_TLSLD16_HI:
16839 case R_PPC64_GOT_TLSLD16_HA:
16840 case R_PPC64_GOT_TLSLD_PCREL34:
16841 tls_type = TLS_TLS | TLS_LD;
16842 goto dogot;
16843
16844 case R_PPC64_GOT_TPREL16_DS:
16845 case R_PPC64_GOT_TPREL16_LO_DS:
16846 case R_PPC64_GOT_TPREL16_HI:
16847 case R_PPC64_GOT_TPREL16_HA:
16848 case R_PPC64_GOT_TPREL_PCREL34:
16849 tls_type = TLS_TLS | TLS_TPREL;
16850 goto dogot;
16851
16852 case R_PPC64_GOT_DTPREL16_DS:
16853 case R_PPC64_GOT_DTPREL16_LO_DS:
16854 case R_PPC64_GOT_DTPREL16_HI:
16855 case R_PPC64_GOT_DTPREL16_HA:
16856 case R_PPC64_GOT_DTPREL_PCREL34:
16857 tls_type = TLS_TLS | TLS_DTPREL;
16858 goto dogot;
16859
16860 case R_PPC64_GOT16:
16861 case R_PPC64_GOT16_LO:
16862 case R_PPC64_GOT16_HI:
16863 case R_PPC64_GOT16_HA:
16864 case R_PPC64_GOT16_DS:
16865 case R_PPC64_GOT16_LO_DS:
16866 case R_PPC64_GOT_PCREL34:
16867 dogot:
16868 {
16869 /* Relocation is to the entry for this symbol in the global
16870 offset table. */
16871 asection *got;
16872 bfd_vma *offp;
16873 bfd_vma off;
16874 unsigned long indx = 0;
16875 struct got_entry *ent;
16876
16877 if (tls_type == (TLS_TLS | TLS_LD)
16878 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16879 ent = ppc64_tlsld_got (input_bfd);
16880 else
16881 {
16882 if (h != NULL)
16883 {
16884 if (!htab->elf.dynamic_sections_created
16885 || h->elf.dynindx == -1
16886 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16887 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16888 /* This is actually a static link, or it is a
16889 -Bsymbolic link and the symbol is defined
16890 locally, or the symbol was forced to be local
16891 because of a version file. */
16892 ;
16893 else
16894 {
16895 indx = h->elf.dynindx;
16896 unresolved_reloc = false;
16897 }
16898 ent = h->elf.got.glist;
16899 }
16900 else
16901 {
16902 if (local_got_ents == NULL)
16903 abort ();
16904 ent = local_got_ents[r_symndx];
16905 }
16906
16907 for (; ent != NULL; ent = ent->next)
16908 if (ent->addend == orig_rel.r_addend
16909 && ent->owner == input_bfd
16910 && ent->tls_type == tls_type)
16911 break;
16912 }
16913
16914 if (ent == NULL)
16915 abort ();
16916 if (ent->is_indirect)
16917 ent = ent->got.ent;
16918 offp = &ent->got.offset;
16919 got = ppc64_elf_tdata (ent->owner)->got;
16920 if (got == NULL)
16921 abort ();
16922
16923 /* The offset must always be a multiple of 8. We use the
16924 least significant bit to record whether we have already
16925 processed this entry. */
16926 off = *offp;
16927 if ((off & 1) != 0)
16928 off &= ~1;
16929 else
16930 {
16931 /* Generate relocs for the dynamic linker, except in
16932 the case of TLSLD where we'll use one entry per
16933 module. */
16934 asection *relgot;
16935 bool ifunc;
16936
16937 *offp = off | 1;
16938 relgot = NULL;
16939 ifunc = (h != NULL
16940 ? h->elf.type == STT_GNU_IFUNC
16941 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16942 if (ifunc)
16943 {
16944 relgot = htab->elf.irelplt;
16945 if (indx == 0 || is_static_defined (&h->elf))
16946 htab->elf.ifunc_resolvers = true;
16947 }
16948 else if (indx != 0
16949 || (bfd_link_pic (info)
16950 && (h == NULL
16951 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16952 && !(tls_type != 0
16953 && bfd_link_executable (info)
16954 && (h == NULL
16955 || SYMBOL_REFERENCES_LOCAL (info,
16956 &h->elf)))
16957 && (h != NULL
16958 ? !bfd_is_abs_symbol (&h->elf.root)
16959 : sym->st_shndx != SHN_ABS)))
16960
16961 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16962 if (relgot != NULL)
16963 {
16964 outrel.r_offset = (got->output_section->vma
16965 + got->output_offset
16966 + off);
16967 outrel.r_addend = orig_rel.r_addend;
16968 if (tls_type & (TLS_LD | TLS_GD))
16969 {
16970 outrel.r_addend = 0;
16971 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16972 if (tls_type == (TLS_TLS | TLS_GD))
16973 {
16974 loc = relgot->contents;
16975 loc += (relgot->reloc_count++
16976 * sizeof (Elf64_External_Rela));
16977 bfd_elf64_swap_reloca_out (output_bfd,
16978 &outrel, loc);
16979 outrel.r_offset += 8;
16980 outrel.r_addend = orig_rel.r_addend;
16981 outrel.r_info
16982 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16983 }
16984 }
16985 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16986 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16987 else if (tls_type == (TLS_TLS | TLS_TPREL))
16988 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16989 else if (indx != 0)
16990 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16991 else
16992 {
16993 if (ifunc)
16994 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16995 else
16996 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16997
16998 /* Write the .got section contents for the sake
16999 of prelink. */
17000 loc = got->contents + off;
17001 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17002 loc);
17003 }
17004
17005 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
17006 {
17007 outrel.r_addend += relocation;
17008 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
17009 {
17010 if (htab->elf.tls_sec == NULL)
17011 outrel.r_addend = 0;
17012 else
17013 outrel.r_addend -= htab->elf.tls_sec->vma;
17014 }
17015 }
17016 if (!(info->enable_dt_relr
17017 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17018 {
17019 loc = relgot->contents;
17020 loc += (relgot->reloc_count++
17021 * sizeof (Elf64_External_Rela));
17022 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17023 }
17024 }
17025
17026 /* Init the .got section contents here if we're not
17027 emitting a reloc. */
17028 else
17029 {
17030 relocation += orig_rel.r_addend;
17031 if (tls_type != 0)
17032 {
17033 if (htab->elf.tls_sec == NULL)
17034 relocation = 0;
17035 else
17036 {
17037 if (tls_type & TLS_LD)
17038 relocation = 0;
17039 else
17040 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17041 if (tls_type & TLS_TPREL)
17042 relocation += DTP_OFFSET - TP_OFFSET;
17043 }
17044
17045 if (tls_type & (TLS_GD | TLS_LD))
17046 {
17047 bfd_put_64 (output_bfd, relocation,
17048 got->contents + off + 8);
17049 relocation = 1;
17050 }
17051 }
17052 bfd_put_64 (output_bfd, relocation,
17053 got->contents + off);
17054 }
17055 }
17056
17057 if (off >= (bfd_vma) -2)
17058 abort ();
17059
17060 relocation = got->output_section->vma + got->output_offset + off;
17061 addend = 0;
17062 if (!(r_type == R_PPC64_GOT_PCREL34
17063 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17064 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17065 || r_type == R_PPC64_GOT_TPREL_PCREL34
17066 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17067 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17068 }
17069 break;
17070
17071 case R_PPC64_PLT16_HA:
17072 case R_PPC64_PLT16_HI:
17073 case R_PPC64_PLT16_LO:
17074 case R_PPC64_PLT16_LO_DS:
17075 case R_PPC64_PLT_PCREL34:
17076 case R_PPC64_PLT_PCREL34_NOTOC:
17077 case R_PPC64_PLT32:
17078 case R_PPC64_PLT64:
17079 case R_PPC64_PLTSEQ:
17080 case R_PPC64_PLTSEQ_NOTOC:
17081 case R_PPC64_PLTCALL:
17082 case R_PPC64_PLTCALL_NOTOC:
17083 /* Relocation is to the entry for this symbol in the
17084 procedure linkage table. */
17085 unresolved_reloc = true;
17086 {
17087 struct plt_entry **plt_list = NULL;
17088 if (h != NULL)
17089 plt_list = &h->elf.plt.plist;
17090 else if (local_got_ents != NULL)
17091 {
17092 struct plt_entry **local_plt = (struct plt_entry **)
17093 (local_got_ents + symtab_hdr->sh_info);
17094 plt_list = local_plt + r_symndx;
17095 }
17096 if (plt_list)
17097 {
17098 struct plt_entry *ent;
17099
17100 for (ent = *plt_list; ent != NULL; ent = ent->next)
17101 if (ent->plt.offset != (bfd_vma) -1
17102 && ent->addend == orig_rel.r_addend)
17103 {
17104 asection *plt;
17105 bfd_vma got;
17106
17107 plt = htab->elf.splt;
17108 if (use_local_plt (info, elf_hash_entry (h)))
17109 {
17110 if (h != NULL
17111 ? h->elf.type == STT_GNU_IFUNC
17112 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17113 plt = htab->elf.iplt;
17114 else
17115 plt = htab->pltlocal;
17116 }
17117 relocation = (plt->output_section->vma
17118 + plt->output_offset
17119 + ent->plt.offset);
17120 if (r_type == R_PPC64_PLT16_HA
17121 || r_type == R_PPC64_PLT16_HI
17122 || r_type == R_PPC64_PLT16_LO
17123 || r_type == R_PPC64_PLT16_LO_DS)
17124 {
17125 got = (elf_gp (output_bfd)
17126 + htab->sec_info[input_section->id].toc_off);
17127 relocation -= got;
17128 }
17129 addend = 0;
17130 unresolved_reloc = false;
17131 break;
17132 }
17133 }
17134 }
17135 break;
17136
17137 case R_PPC64_TOC:
17138 /* Relocation value is TOC base. */
17139 relocation = TOCstart;
17140 if (r_symndx == STN_UNDEF)
17141 relocation += htab->sec_info[input_section->id].toc_off;
17142 else if (unresolved_reloc)
17143 ;
17144 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17145 relocation += htab->sec_info[sec->id].toc_off;
17146 else
17147 unresolved_reloc = true;
17148 if (unresolved_reloc
17149 || (!is_opd
17150 && h != NULL
17151 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17152 info->callbacks->einfo
17153 /* xgettext:c-format */
17154 (_("%H: %s against %pT is not supported\n"),
17155 input_bfd, input_section, rel->r_offset,
17156 ppc64_elf_howto_table[r_type]->name, sym_name);
17157 goto dodyn;
17158
17159 /* TOC16 relocs. We want the offset relative to the TOC base,
17160 which is the address of the start of the TOC plus 0x8000.
17161 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17162 in this order. */
17163 case R_PPC64_TOC16:
17164 case R_PPC64_TOC16_LO:
17165 case R_PPC64_TOC16_HI:
17166 case R_PPC64_TOC16_DS:
17167 case R_PPC64_TOC16_LO_DS:
17168 case R_PPC64_TOC16_HA:
17169 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17170 if (h != NULL)
17171 goto dodyn;
17172 break;
17173
17174 /* Relocate against the beginning of the section. */
17175 case R_PPC64_SECTOFF:
17176 case R_PPC64_SECTOFF_LO:
17177 case R_PPC64_SECTOFF_HI:
17178 case R_PPC64_SECTOFF_DS:
17179 case R_PPC64_SECTOFF_LO_DS:
17180 case R_PPC64_SECTOFF_HA:
17181 if (sec != NULL)
17182 addend -= sec->output_section->vma;
17183 break;
17184
17185 case R_PPC64_REL16:
17186 case R_PPC64_REL16_LO:
17187 case R_PPC64_REL16_HI:
17188 case R_PPC64_REL16_HA:
17189 case R_PPC64_REL16_HIGH:
17190 case R_PPC64_REL16_HIGHA:
17191 case R_PPC64_REL16_HIGHER:
17192 case R_PPC64_REL16_HIGHERA:
17193 case R_PPC64_REL16_HIGHEST:
17194 case R_PPC64_REL16_HIGHESTA:
17195 case R_PPC64_REL16_HIGHER34:
17196 case R_PPC64_REL16_HIGHERA34:
17197 case R_PPC64_REL16_HIGHEST34:
17198 case R_PPC64_REL16_HIGHESTA34:
17199 case R_PPC64_REL16DX_HA:
17200 case R_PPC64_REL14:
17201 case R_PPC64_REL14_BRNTAKEN:
17202 case R_PPC64_REL14_BRTAKEN:
17203 case R_PPC64_REL24:
17204 case R_PPC64_REL24_NOTOC:
17205 case R_PPC64_REL24_P9NOTOC:
17206 case R_PPC64_PCREL34:
17207 case R_PPC64_PCREL28:
17208 break;
17209
17210 case R_PPC64_TPREL16:
17211 case R_PPC64_TPREL16_LO:
17212 case R_PPC64_TPREL16_HI:
17213 case R_PPC64_TPREL16_HA:
17214 case R_PPC64_TPREL16_DS:
17215 case R_PPC64_TPREL16_LO_DS:
17216 case R_PPC64_TPREL16_HIGH:
17217 case R_PPC64_TPREL16_HIGHA:
17218 case R_PPC64_TPREL16_HIGHER:
17219 case R_PPC64_TPREL16_HIGHERA:
17220 case R_PPC64_TPREL16_HIGHEST:
17221 case R_PPC64_TPREL16_HIGHESTA:
17222 if (h != NULL
17223 && h->elf.root.type == bfd_link_hash_undefweak
17224 && h->elf.dynindx == -1
17225 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17226 {
17227 /* Make this relocation against an undefined weak symbol
17228 resolve to zero. This is really just a tweak, since
17229 code using weak externs ought to check that they are
17230 defined before using them. */
17231 bfd_byte *p = contents + rel->r_offset - d_offset;
17232
17233 insn = bfd_get_32 (input_bfd, p);
17234 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17235 if (insn != 0)
17236 bfd_put_32 (input_bfd, insn, p);
17237 break;
17238 }
17239 /* Fall through. */
17240
17241 case R_PPC64_TPREL34:
17242 if (htab->elf.tls_sec != NULL)
17243 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17244 /* The TPREL16 relocs shouldn't really be used in shared
17245 libs or with non-local symbols as that will result in
17246 DT_TEXTREL being set, but support them anyway. */
17247 goto dodyn;
17248
17249 case R_PPC64_DTPREL16:
17250 case R_PPC64_DTPREL16_LO:
17251 case R_PPC64_DTPREL16_HI:
17252 case R_PPC64_DTPREL16_HA:
17253 case R_PPC64_DTPREL16_DS:
17254 case R_PPC64_DTPREL16_LO_DS:
17255 case R_PPC64_DTPREL16_HIGH:
17256 case R_PPC64_DTPREL16_HIGHA:
17257 case R_PPC64_DTPREL16_HIGHER:
17258 case R_PPC64_DTPREL16_HIGHERA:
17259 case R_PPC64_DTPREL16_HIGHEST:
17260 case R_PPC64_DTPREL16_HIGHESTA:
17261 case R_PPC64_DTPREL34:
17262 if (htab->elf.tls_sec != NULL)
17263 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17264 break;
17265
17266 case R_PPC64_ADDR64_LOCAL:
17267 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17268 ? h->elf.other
17269 : sym->st_other);
17270 break;
17271
17272 case R_PPC64_DTPMOD64:
17273 relocation = 1;
17274 addend = 0;
17275 goto dodyn;
17276
17277 case R_PPC64_TPREL64:
17278 if (htab->elf.tls_sec != NULL)
17279 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17280 goto dodyn;
17281
17282 case R_PPC64_DTPREL64:
17283 if (htab->elf.tls_sec != NULL)
17284 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17285 /* Fall through. */
17286
17287 /* Relocations that may need to be propagated if this is a
17288 dynamic object. */
17289 case R_PPC64_REL30:
17290 case R_PPC64_REL32:
17291 case R_PPC64_REL64:
17292 case R_PPC64_ADDR14:
17293 case R_PPC64_ADDR14_BRNTAKEN:
17294 case R_PPC64_ADDR14_BRTAKEN:
17295 case R_PPC64_ADDR16:
17296 case R_PPC64_ADDR16_DS:
17297 case R_PPC64_ADDR16_HA:
17298 case R_PPC64_ADDR16_HI:
17299 case R_PPC64_ADDR16_HIGH:
17300 case R_PPC64_ADDR16_HIGHA:
17301 case R_PPC64_ADDR16_HIGHER:
17302 case R_PPC64_ADDR16_HIGHERA:
17303 case R_PPC64_ADDR16_HIGHEST:
17304 case R_PPC64_ADDR16_HIGHESTA:
17305 case R_PPC64_ADDR16_LO:
17306 case R_PPC64_ADDR16_LO_DS:
17307 case R_PPC64_ADDR16_HIGHER34:
17308 case R_PPC64_ADDR16_HIGHERA34:
17309 case R_PPC64_ADDR16_HIGHEST34:
17310 case R_PPC64_ADDR16_HIGHESTA34:
17311 case R_PPC64_ADDR24:
17312 case R_PPC64_ADDR32:
17313 case R_PPC64_ADDR64:
17314 case R_PPC64_UADDR16:
17315 case R_PPC64_UADDR32:
17316 case R_PPC64_UADDR64:
17317 case R_PPC64_D34:
17318 case R_PPC64_D34_LO:
17319 case R_PPC64_D34_HI30:
17320 case R_PPC64_D34_HA30:
17321 case R_PPC64_D28:
17322 dodyn:
17323 if ((input_section->flags & SEC_ALLOC) == 0)
17324 break;
17325
17326 if (NO_OPD_RELOCS && is_opd)
17327 break;
17328
17329 if (bfd_link_pic (info)
17330 ? ((h == NULL
17331 || h->elf.dyn_relocs != NULL)
17332 && ((h != NULL && pc_dynrelocs (h))
17333 || must_be_dyn_reloc (info, r_type)))
17334 : (h != NULL
17335 ? h->elf.dyn_relocs != NULL
17336 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17337 {
17338 bool skip, relocate;
17339 asection *sreloc;
17340 bfd_vma out_off;
17341 long indx = 0;
17342
17343 /* When generating a dynamic object, these relocations
17344 are copied into the output file to be resolved at run
17345 time. */
17346
17347 skip = false;
17348 relocate = false;
17349
17350 out_off = _bfd_elf_section_offset (output_bfd, info,
17351 input_section, rel->r_offset);
17352 if (out_off == (bfd_vma) -1)
17353 skip = true;
17354 else if (out_off == (bfd_vma) -2)
17355 skip = true, relocate = true;
17356 out_off += (input_section->output_section->vma
17357 + input_section->output_offset);
17358 outrel.r_offset = out_off;
17359 outrel.r_addend = rel->r_addend;
17360
17361 /* Optimize unaligned reloc use. */
17362 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17363 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17364 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17365 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17366 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17367 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17368 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17369 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17370 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17371
17372 if (skip)
17373 memset (&outrel, 0, sizeof outrel);
17374 else if (h != NULL
17375 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17376 && !is_opd
17377 && r_type != R_PPC64_TOC)
17378 {
17379 indx = h->elf.dynindx;
17380 BFD_ASSERT (indx != -1);
17381 outrel.r_info = ELF64_R_INFO (indx, r_type);
17382 }
17383 else
17384 {
17385 /* This symbol is local, or marked to become local,
17386 or this is an opd section reloc which must point
17387 at a local function. */
17388 outrel.r_addend += relocation;
17389 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17390 {
17391 if (is_opd && h != NULL)
17392 {
17393 /* Lie about opd entries. This case occurs
17394 when building shared libraries and we
17395 reference a function in another shared
17396 lib. The same thing happens for a weak
17397 definition in an application that's
17398 overridden by a strong definition in a
17399 shared lib. (I believe this is a generic
17400 bug in binutils handling of weak syms.)
17401 In these cases we won't use the opd
17402 entry in this lib. */
17403 unresolved_reloc = false;
17404 }
17405 if (!is_opd
17406 && r_type == R_PPC64_ADDR64
17407 && (h != NULL
17408 ? h->elf.type == STT_GNU_IFUNC
17409 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17410 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17411 else
17412 {
17413 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17414
17415 /* We need to relocate .opd contents for ld.so.
17416 Prelink also wants simple and consistent rules
17417 for relocs. This make all RELATIVE relocs have
17418 *r_offset equal to r_addend. */
17419 relocate = true;
17420 }
17421 }
17422 else
17423 {
17424 if (h != NULL
17425 ? h->elf.type == STT_GNU_IFUNC
17426 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17427 {
17428 info->callbacks->einfo
17429 /* xgettext:c-format */
17430 (_("%H: %s for indirect "
17431 "function `%pT' unsupported\n"),
17432 input_bfd, input_section, rel->r_offset,
17433 ppc64_elf_howto_table[r_type]->name,
17434 sym_name);
17435 ret = false;
17436 }
17437 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17438 ;
17439 else if (sec == NULL || sec->owner == NULL)
17440 {
17441 bfd_set_error (bfd_error_bad_value);
17442 return false;
17443 }
17444 else
17445 {
17446 asection *osec = sec->output_section;
17447
17448 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17449 {
17450 /* TLS symbol values are relative to the
17451 TLS segment. Dynamic relocations for
17452 local TLS symbols therefore can't be
17453 reduced to a relocation against their
17454 section symbol because it holds the
17455 address of the section, not a value
17456 relative to the TLS segment. We could
17457 change the .tdata dynamic section symbol
17458 to be zero value but STN_UNDEF works
17459 and is used elsewhere, eg. for TPREL64
17460 GOT relocs against local TLS symbols. */
17461 osec = htab->elf.tls_sec;
17462 indx = 0;
17463 }
17464 else
17465 {
17466 indx = elf_section_data (osec)->dynindx;
17467 if (indx == 0)
17468 {
17469 if ((osec->flags & SEC_READONLY) == 0
17470 && htab->elf.data_index_section != NULL)
17471 osec = htab->elf.data_index_section;
17472 else
17473 osec = htab->elf.text_index_section;
17474 indx = elf_section_data (osec)->dynindx;
17475 }
17476 BFD_ASSERT (indx != 0);
17477 }
17478
17479 /* We are turning this relocation into one
17480 against a section symbol, so subtract out
17481 the output section's address but not the
17482 offset of the input section in the output
17483 section. */
17484 outrel.r_addend -= osec->vma;
17485 }
17486
17487 outrel.r_info = ELF64_R_INFO (indx, r_type);
17488 }
17489 }
17490
17491 if (!(info->enable_dt_relr
17492 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17493 && rel->r_offset % 2 == 0
17494 && input_section->alignment_power != 0
17495 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
17496 {
17497 sreloc = elf_section_data (input_section)->sreloc;
17498 if (h != NULL
17499 ? h->elf.type == STT_GNU_IFUNC
17500 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17501 {
17502 sreloc = htab->elf.irelplt;
17503 if (indx == 0 || is_static_defined (&h->elf))
17504 htab->elf.ifunc_resolvers = true;
17505 }
17506 if (sreloc == NULL)
17507 abort ();
17508
17509 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17510 >= sreloc->size)
17511 abort ();
17512 loc = sreloc->contents;
17513 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17514 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17515 }
17516
17517 if (!warned_dynamic
17518 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17519 {
17520 info->callbacks->einfo
17521 /* xgettext:c-format */
17522 (_("%X%P: %pB: %s against %pT "
17523 "is not supported by glibc as a dynamic relocation\n"),
17524 input_bfd,
17525 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17526 sym_name);
17527 warned_dynamic = true;
17528 }
17529
17530 /* If this reloc is against an external symbol, it will
17531 be computed at runtime, so there's no need to do
17532 anything now. However, for the sake of prelink ensure
17533 that the section contents are a known value. */
17534 if (!relocate)
17535 {
17536 unresolved_reloc = false;
17537 /* The value chosen here is quite arbitrary as ld.so
17538 ignores section contents except for the special
17539 case of .opd where the contents might be accessed
17540 before relocation. Choose zero, as that won't
17541 cause reloc overflow. */
17542 relocation = 0;
17543 addend = 0;
17544 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17545 to improve backward compatibility with older
17546 versions of ld. */
17547 if (r_type == R_PPC64_ADDR64)
17548 addend = outrel.r_addend;
17549 /* Adjust pc_relative relocs to have zero in *r_offset. */
17550 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17551 addend = outrel.r_offset;
17552 }
17553 }
17554 break;
17555
17556 case R_PPC64_COPY:
17557 case R_PPC64_GLOB_DAT:
17558 case R_PPC64_JMP_SLOT:
17559 case R_PPC64_JMP_IREL:
17560 case R_PPC64_RELATIVE:
17561 /* We shouldn't ever see these dynamic relocs in relocatable
17562 files. */
17563 /* Fall through. */
17564
17565 case R_PPC64_PLTGOT16:
17566 case R_PPC64_PLTGOT16_DS:
17567 case R_PPC64_PLTGOT16_HA:
17568 case R_PPC64_PLTGOT16_HI:
17569 case R_PPC64_PLTGOT16_LO:
17570 case R_PPC64_PLTGOT16_LO_DS:
17571 case R_PPC64_PLTREL32:
17572 case R_PPC64_PLTREL64:
17573 /* These ones haven't been implemented yet. */
17574
17575 info->callbacks->einfo
17576 /* xgettext:c-format */
17577 (_("%P: %pB: %s is not supported for `%pT'\n"),
17578 input_bfd,
17579 ppc64_elf_howto_table[r_type]->name, sym_name);
17580
17581 bfd_set_error (bfd_error_invalid_operation);
17582 ret = false;
17583 goto copy_reloc;
17584 }
17585
17586 /* Multi-instruction sequences that access the TOC can be
17587 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17588 to nop; addi rb,r2,x; */
17589 switch (r_type)
17590 {
17591 default:
17592 break;
17593
17594 case R_PPC64_GOT_TLSLD16_HI:
17595 case R_PPC64_GOT_TLSGD16_HI:
17596 case R_PPC64_GOT_TPREL16_HI:
17597 case R_PPC64_GOT_DTPREL16_HI:
17598 case R_PPC64_GOT16_HI:
17599 case R_PPC64_TOC16_HI:
17600 /* These relocs would only be useful if building up an
17601 offset to later add to r2, perhaps in an indexed
17602 addressing mode instruction. Don't try to optimize.
17603 Unfortunately, the possibility of someone building up an
17604 offset like this or even with the HA relocs, means that
17605 we need to check the high insn when optimizing the low
17606 insn. */
17607 break;
17608
17609 case R_PPC64_PLTCALL_NOTOC:
17610 if (!unresolved_reloc)
17611 htab->notoc_plt = 1;
17612 /* Fall through. */
17613 case R_PPC64_PLTCALL:
17614 if (unresolved_reloc
17615 && offset_in_range (input_section, rel->r_offset,
17616 r_type == R_PPC64_PLTCALL ? 8 : 4))
17617 {
17618 /* No plt entry. Make this into a direct call. */
17619 bfd_byte *p = contents + rel->r_offset;
17620 insn = bfd_get_32 (input_bfd, p);
17621 insn &= 1;
17622 bfd_put_32 (input_bfd, B_DOT | insn, p);
17623 if (r_type == R_PPC64_PLTCALL)
17624 bfd_put_32 (input_bfd, NOP, p + 4);
17625 unresolved_reloc = save_unresolved_reloc;
17626 r_type = R_PPC64_REL24;
17627 }
17628 break;
17629
17630 case R_PPC64_PLTSEQ_NOTOC:
17631 case R_PPC64_PLTSEQ:
17632 if (unresolved_reloc)
17633 {
17634 unresolved_reloc = false;
17635 goto nop_it;
17636 }
17637 break;
17638
17639 case R_PPC64_PLT_PCREL34_NOTOC:
17640 if (!unresolved_reloc)
17641 htab->notoc_plt = 1;
17642 /* Fall through. */
17643 case R_PPC64_PLT_PCREL34:
17644 if (unresolved_reloc
17645 && offset_in_range (input_section, rel->r_offset, 8))
17646 {
17647 bfd_byte *p = contents + rel->r_offset;
17648 bfd_put_32 (input_bfd, PNOP >> 32, p);
17649 bfd_put_32 (input_bfd, PNOP, p + 4);
17650 unresolved_reloc = false;
17651 goto copy_reloc;
17652 }
17653 break;
17654
17655 case R_PPC64_PLT16_HA:
17656 if (unresolved_reloc)
17657 {
17658 unresolved_reloc = false;
17659 goto nop_it;
17660 }
17661 /* Fall through. */
17662 case R_PPC64_GOT_TLSLD16_HA:
17663 case R_PPC64_GOT_TLSGD16_HA:
17664 case R_PPC64_GOT_TPREL16_HA:
17665 case R_PPC64_GOT_DTPREL16_HA:
17666 case R_PPC64_GOT16_HA:
17667 case R_PPC64_TOC16_HA:
17668 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17669 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17670 && !(bfd_link_pic (info)
17671 && (h != NULL
17672 ? bfd_is_abs_symbol (&h->elf.root)
17673 : sec == bfd_abs_section_ptr)))
17674 {
17675 bfd_byte *p;
17676 nop_it:
17677 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17678 {
17679 p = contents + (rel->r_offset & ~3);
17680 bfd_put_32 (input_bfd, NOP, p);
17681 goto copy_reloc;
17682 }
17683 }
17684 break;
17685
17686 case R_PPC64_PLT16_LO:
17687 case R_PPC64_PLT16_LO_DS:
17688 if (unresolved_reloc)
17689 {
17690 unresolved_reloc = false;
17691 goto nop_it;
17692 }
17693 /* Fall through. */
17694 case R_PPC64_GOT_TLSLD16_LO:
17695 case R_PPC64_GOT_TLSGD16_LO:
17696 case R_PPC64_GOT_TPREL16_LO_DS:
17697 case R_PPC64_GOT_DTPREL16_LO_DS:
17698 case R_PPC64_GOT16_LO:
17699 case R_PPC64_GOT16_LO_DS:
17700 case R_PPC64_TOC16_LO:
17701 case R_PPC64_TOC16_LO_DS:
17702 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17703 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17704 && !(bfd_link_pic (info)
17705 && (h != NULL
17706 ? bfd_is_abs_symbol (&h->elf.root)
17707 : sec == bfd_abs_section_ptr))
17708 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17709 {
17710 bfd_byte *p = contents + (rel->r_offset & ~3);
17711 insn = bfd_get_32 (input_bfd, p);
17712 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17713 {
17714 /* Transform addic to addi when we change reg. */
17715 insn &= ~((0x3fu << 26) | (0x1f << 16));
17716 insn |= (14u << 26) | (2 << 16);
17717 }
17718 else
17719 {
17720 insn &= ~(0x1f << 16);
17721 insn |= 2 << 16;
17722 }
17723 bfd_put_32 (input_bfd, insn, p);
17724 }
17725 break;
17726
17727 case R_PPC64_TPREL16_HA:
17728 if (htab->do_tls_opt
17729 && relocation + addend + 0x8000 < 0x10000
17730 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17731 {
17732 bfd_byte *p = contents + (rel->r_offset & ~3);
17733 bfd_put_32 (input_bfd, NOP, p);
17734 goto copy_reloc;
17735 }
17736 break;
17737
17738 case R_PPC64_TPREL16_LO:
17739 case R_PPC64_TPREL16_LO_DS:
17740 if (htab->do_tls_opt
17741 && relocation + addend + 0x8000 < 0x10000
17742 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17743 {
17744 bfd_byte *p = contents + (rel->r_offset & ~3);
17745 insn = bfd_get_32 (input_bfd, p);
17746 insn &= ~(0x1f << 16);
17747 insn |= 13 << 16;
17748 bfd_put_32 (input_bfd, insn, p);
17749 }
17750 break;
17751 }
17752
17753 /* Do any further special processing. */
17754 switch (r_type)
17755 {
17756 default:
17757 break;
17758
17759 case R_PPC64_REL16_HA:
17760 case R_PPC64_REL16_HIGHA:
17761 case R_PPC64_REL16_HIGHERA:
17762 case R_PPC64_REL16_HIGHESTA:
17763 case R_PPC64_REL16DX_HA:
17764 case R_PPC64_ADDR16_HA:
17765 case R_PPC64_ADDR16_HIGHA:
17766 case R_PPC64_ADDR16_HIGHERA:
17767 case R_PPC64_ADDR16_HIGHESTA:
17768 case R_PPC64_TOC16_HA:
17769 case R_PPC64_SECTOFF_HA:
17770 case R_PPC64_TPREL16_HA:
17771 case R_PPC64_TPREL16_HIGHA:
17772 case R_PPC64_TPREL16_HIGHERA:
17773 case R_PPC64_TPREL16_HIGHESTA:
17774 case R_PPC64_DTPREL16_HA:
17775 case R_PPC64_DTPREL16_HIGHA:
17776 case R_PPC64_DTPREL16_HIGHERA:
17777 case R_PPC64_DTPREL16_HIGHESTA:
17778 /* It's just possible that this symbol is a weak symbol
17779 that's not actually defined anywhere. In that case,
17780 'sec' would be NULL, and we should leave the symbol
17781 alone (it will be set to zero elsewhere in the link). */
17782 if (sec == NULL)
17783 break;
17784 /* Fall through. */
17785
17786 case R_PPC64_GOT16_HA:
17787 case R_PPC64_PLTGOT16_HA:
17788 case R_PPC64_PLT16_HA:
17789 case R_PPC64_GOT_TLSGD16_HA:
17790 case R_PPC64_GOT_TLSLD16_HA:
17791 case R_PPC64_GOT_TPREL16_HA:
17792 case R_PPC64_GOT_DTPREL16_HA:
17793 /* Add 0x10000 if sign bit in 0:15 is set.
17794 Bits 0:15 are not used. */
17795 addend += 0x8000;
17796 break;
17797
17798 case R_PPC64_D34_HA30:
17799 case R_PPC64_ADDR16_HIGHERA34:
17800 case R_PPC64_ADDR16_HIGHESTA34:
17801 case R_PPC64_REL16_HIGHERA34:
17802 case R_PPC64_REL16_HIGHESTA34:
17803 if (sec != NULL)
17804 addend += 1ULL << 33;
17805 break;
17806
17807 case R_PPC64_ADDR16_DS:
17808 case R_PPC64_ADDR16_LO_DS:
17809 case R_PPC64_GOT16_DS:
17810 case R_PPC64_GOT16_LO_DS:
17811 case R_PPC64_PLT16_LO_DS:
17812 case R_PPC64_SECTOFF_DS:
17813 case R_PPC64_SECTOFF_LO_DS:
17814 case R_PPC64_TOC16_DS:
17815 case R_PPC64_TOC16_LO_DS:
17816 case R_PPC64_PLTGOT16_DS:
17817 case R_PPC64_PLTGOT16_LO_DS:
17818 case R_PPC64_GOT_TPREL16_DS:
17819 case R_PPC64_GOT_TPREL16_LO_DS:
17820 case R_PPC64_GOT_DTPREL16_DS:
17821 case R_PPC64_GOT_DTPREL16_LO_DS:
17822 case R_PPC64_TPREL16_DS:
17823 case R_PPC64_TPREL16_LO_DS:
17824 case R_PPC64_DTPREL16_DS:
17825 case R_PPC64_DTPREL16_LO_DS:
17826 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17827 break;
17828 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17829 mask = 3;
17830 /* If this reloc is against an lq, lxv, or stxv insn, then
17831 the value must be a multiple of 16. This is somewhat of
17832 a hack, but the "correct" way to do this by defining _DQ
17833 forms of all the _DS relocs bloats all reloc switches in
17834 this file. It doesn't make much sense to use these
17835 relocs in data, so testing the insn should be safe. */
17836 if ((insn & (0x3fu << 26)) == (56u << 26)
17837 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17838 mask = 15;
17839 relocation += addend;
17840 addend = insn & (mask ^ 3);
17841 if ((relocation & mask) != 0)
17842 {
17843 relocation ^= relocation & mask;
17844 info->callbacks->einfo
17845 /* xgettext:c-format */
17846 (_("%H: error: %s not a multiple of %u\n"),
17847 input_bfd, input_section, rel->r_offset,
17848 ppc64_elf_howto_table[r_type]->name,
17849 mask + 1);
17850 bfd_set_error (bfd_error_bad_value);
17851 ret = false;
17852 goto copy_reloc;
17853 }
17854 break;
17855 }
17856
17857 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17858 because such sections are not SEC_ALLOC and thus ld.so will
17859 not process them. */
17860 howto = ppc64_elf_howto_table[(int) r_type];
17861 if (unresolved_reloc
17862 && !((input_section->flags & SEC_DEBUGGING) != 0
17863 && h->elf.def_dynamic)
17864 && _bfd_elf_section_offset (output_bfd, info, input_section,
17865 rel->r_offset) != (bfd_vma) -1)
17866 {
17867 info->callbacks->einfo
17868 /* xgettext:c-format */
17869 (_("%H: unresolvable %s against `%pT'\n"),
17870 input_bfd, input_section, rel->r_offset,
17871 howto->name,
17872 h->elf.root.root.string);
17873 ret = false;
17874 }
17875
17876 /* 16-bit fields in insns mostly have signed values, but a
17877 few insns have 16-bit unsigned values. Really, we should
17878 have different reloc types. */
17879 if (howto->complain_on_overflow != complain_overflow_dont
17880 && howto->dst_mask == 0xffff
17881 && (input_section->flags & SEC_CODE) != 0
17882 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17883 {
17884 enum complain_overflow complain = complain_overflow_signed;
17885
17886 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17887 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17888 complain = complain_overflow_bitfield;
17889 else if (howto->rightshift == 0
17890 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17891 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17892 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17893 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17894 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17895 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17896 complain = complain_overflow_unsigned;
17897 if (howto->complain_on_overflow != complain)
17898 {
17899 alt_howto = *howto;
17900 alt_howto.complain_on_overflow = complain;
17901 howto = &alt_howto;
17902 }
17903 }
17904
17905 switch (r_type)
17906 {
17907 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17908 case R_PPC64_D34:
17909 case R_PPC64_D34_LO:
17910 case R_PPC64_D34_HI30:
17911 case R_PPC64_D34_HA30:
17912 case R_PPC64_PCREL34:
17913 case R_PPC64_GOT_PCREL34:
17914 case R_PPC64_TPREL34:
17915 case R_PPC64_DTPREL34:
17916 case R_PPC64_GOT_TLSGD_PCREL34:
17917 case R_PPC64_GOT_TLSLD_PCREL34:
17918 case R_PPC64_GOT_TPREL_PCREL34:
17919 case R_PPC64_GOT_DTPREL_PCREL34:
17920 case R_PPC64_PLT_PCREL34:
17921 case R_PPC64_PLT_PCREL34_NOTOC:
17922 case R_PPC64_D28:
17923 case R_PPC64_PCREL28:
17924 if (!offset_in_range (input_section, rel->r_offset, 8))
17925 r = bfd_reloc_outofrange;
17926 else
17927 {
17928 relocation += addend;
17929 if (howto->pc_relative)
17930 relocation -= (rel->r_offset
17931 + input_section->output_offset
17932 + input_section->output_section->vma);
17933 relocation >>= howto->rightshift;
17934
17935 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17936 pinsn <<= 32;
17937 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17938
17939 pinsn &= ~howto->dst_mask;
17940 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17941 & howto->dst_mask);
17942 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17943 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17944 r = bfd_reloc_ok;
17945 if (howto->complain_on_overflow == complain_overflow_signed
17946 && (relocation + (1ULL << (howto->bitsize - 1))
17947 >= 1ULL << howto->bitsize))
17948 r = bfd_reloc_overflow;
17949 }
17950 break;
17951
17952 case R_PPC64_REL16DX_HA:
17953 if (!offset_in_range (input_section, rel->r_offset, 4))
17954 r = bfd_reloc_outofrange;
17955 else
17956 {
17957 relocation += addend;
17958 relocation -= (rel->r_offset
17959 + input_section->output_offset
17960 + input_section->output_section->vma);
17961 relocation = (bfd_signed_vma) relocation >> 16;
17962 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17963 insn &= ~0x1fffc1;
17964 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17965 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17966 r = bfd_reloc_ok;
17967 if (relocation + 0x8000 > 0xffff)
17968 r = bfd_reloc_overflow;
17969 }
17970 break;
17971
17972 default:
17973 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17974 contents, rel->r_offset,
17975 relocation, addend);
17976 }
17977
17978 if (r != bfd_reloc_ok)
17979 {
17980 char *more_info = NULL;
17981 const char *reloc_name = howto->name;
17982
17983 if (reloc_dest != DEST_NORMAL)
17984 {
17985 more_info = bfd_malloc (strlen (reloc_name) + 8);
17986 if (more_info != NULL)
17987 {
17988 strcpy (more_info, reloc_name);
17989 strcat (more_info, (reloc_dest == DEST_OPD
17990 ? " (OPD)" : " (stub)"));
17991 reloc_name = more_info;
17992 }
17993 }
17994
17995 if (r == bfd_reloc_overflow)
17996 {
17997 /* On code like "if (foo) foo();" don't report overflow
17998 on a branch to zero when foo is undefined. */
17999 if (!warned
18000 && (reloc_dest == DEST_STUB
18001 || !(h != NULL
18002 && (h->elf.root.type == bfd_link_hash_undefweak
18003 || h->elf.root.type == bfd_link_hash_undefined)
18004 && is_branch_reloc (r_type))))
18005 info->callbacks->reloc_overflow
18006 (info, (struct bfd_link_hash_entry *) h, sym_name,
18007 reloc_name, orig_rel.r_addend, input_bfd, input_section,
18008 rel->r_offset);
18009 }
18010 else
18011 {
18012 info->callbacks->einfo
18013 /* xgettext:c-format */
18014 (_("%H: %s against `%pT': error %d\n"),
18015 input_bfd, input_section, rel->r_offset,
18016 reloc_name, sym_name, (int) r);
18017 ret = false;
18018 }
18019 free (more_info);
18020 }
18021 copy_reloc:
18022 if (wrel != rel)
18023 *wrel = *rel;
18024 }
18025
18026 if (wrel != rel)
18027 {
18028 Elf_Internal_Shdr *rel_hdr;
18029 size_t deleted = rel - wrel;
18030
18031 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18032 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18033 if (rel_hdr->sh_size == 0)
18034 {
18035 /* It is too late to remove an empty reloc section. Leave
18036 one NONE reloc.
18037 ??? What is wrong with an empty section??? */
18038 rel_hdr->sh_size = rel_hdr->sh_entsize;
18039 deleted -= 1;
18040 }
18041 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18042 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18043 input_section->reloc_count -= deleted;
18044 }
18045
18046 /* If we're emitting relocations, then shortly after this function
18047 returns, reloc offsets and addends for this section will be
18048 adjusted. Worse, reloc symbol indices will be for the output
18049 file rather than the input. Save a copy of the relocs for
18050 opd_entry_value. */
18051 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
18052 {
18053 bfd_size_type amt;
18054 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18055 rel = bfd_alloc (input_bfd, amt);
18056 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
18057 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
18058 if (rel == NULL)
18059 return false;
18060 memcpy (rel, relocs, amt);
18061 }
18062 return ret;
18063 }
18064
18065 /* Adjust the value of any local symbols in opd sections. */
18066
18067 static int
18068 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18069 const char *name ATTRIBUTE_UNUSED,
18070 Elf_Internal_Sym *elfsym,
18071 asection *input_sec,
18072 struct elf_link_hash_entry *h)
18073 {
18074 struct _opd_sec_data *opd;
18075 long adjust;
18076 bfd_vma value;
18077
18078 if (h != NULL)
18079 return 1;
18080
18081 opd = get_opd_info (input_sec);
18082 if (opd == NULL || opd->adjust == NULL)
18083 return 1;
18084
18085 value = elfsym->st_value - input_sec->output_offset;
18086 if (!bfd_link_relocatable (info))
18087 value -= input_sec->output_section->vma;
18088
18089 adjust = opd->adjust[OPD_NDX (value)];
18090 if (adjust == -1)
18091 return 2;
18092
18093 elfsym->st_value += adjust;
18094 return 1;
18095 }
18096
18097 /* Finish up dynamic symbol handling. We set the contents of various
18098 dynamic sections here. */
18099
18100 static bool
18101 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18102 struct bfd_link_info *info,
18103 struct elf_link_hash_entry *h,
18104 Elf_Internal_Sym *sym)
18105 {
18106 struct ppc_link_hash_table *htab;
18107 struct plt_entry *ent;
18108
18109 htab = ppc_hash_table (info);
18110 if (htab == NULL)
18111 return false;
18112
18113 if (!htab->opd_abi && !h->def_regular)
18114 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18115 if (ent->plt.offset != (bfd_vma) -1)
18116 {
18117 /* Mark the symbol as undefined, rather than as
18118 defined in glink. Leave the value if there were
18119 any relocations where pointer equality matters
18120 (this is a clue for the dynamic linker, to make
18121 function pointer comparisons work between an
18122 application and shared library), otherwise set it
18123 to zero. */
18124 sym->st_shndx = SHN_UNDEF;
18125 if (!h->pointer_equality_needed)
18126 sym->st_value = 0;
18127 else if (!h->ref_regular_nonweak)
18128 {
18129 /* This breaks function pointer comparisons, but
18130 that is better than breaking tests for a NULL
18131 function pointer. */
18132 sym->st_value = 0;
18133 }
18134 break;
18135 }
18136
18137 if (h->needs_copy
18138 && (h->root.type == bfd_link_hash_defined
18139 || h->root.type == bfd_link_hash_defweak)
18140 && (h->root.u.def.section == htab->elf.sdynbss
18141 || h->root.u.def.section == htab->elf.sdynrelro))
18142 {
18143 /* This symbol needs a copy reloc. Set it up. */
18144 Elf_Internal_Rela rela;
18145 asection *srel;
18146 bfd_byte *loc;
18147
18148 if (h->dynindx == -1)
18149 abort ();
18150
18151 rela.r_offset = defined_sym_val (h);
18152 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18153 rela.r_addend = 0;
18154 if (h->root.u.def.section == htab->elf.sdynrelro)
18155 srel = htab->elf.sreldynrelro;
18156 else
18157 srel = htab->elf.srelbss;
18158 loc = srel->contents;
18159 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
18160 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
18161 }
18162
18163 return true;
18164 }
18165
18166 /* Used to decide how to sort relocs in an optimal manner for the
18167 dynamic linker, before writing them out. */
18168
18169 static enum elf_reloc_type_class
18170 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18171 const asection *rel_sec,
18172 const Elf_Internal_Rela *rela)
18173 {
18174 enum elf_ppc64_reloc_type r_type;
18175 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18176
18177 if (rel_sec == htab->elf.irelplt)
18178 return reloc_class_ifunc;
18179
18180 r_type = ELF64_R_TYPE (rela->r_info);
18181 switch (r_type)
18182 {
18183 case R_PPC64_RELATIVE:
18184 return reloc_class_relative;
18185 case R_PPC64_JMP_SLOT:
18186 return reloc_class_plt;
18187 case R_PPC64_COPY:
18188 return reloc_class_copy;
18189 default:
18190 return reloc_class_normal;
18191 }
18192 }
18193
18194 /* Finish up the dynamic sections. */
18195
18196 static bool
18197 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18198 struct bfd_link_info *info)
18199 {
18200 struct ppc_link_hash_table *htab;
18201 bfd *dynobj;
18202 asection *sdyn;
18203
18204 htab = ppc_hash_table (info);
18205 if (htab == NULL)
18206 return false;
18207
18208 dynobj = htab->elf.dynobj;
18209 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18210
18211 if (htab->elf.dynamic_sections_created)
18212 {
18213 Elf64_External_Dyn *dyncon, *dynconend;
18214
18215 if (sdyn == NULL || htab->elf.sgot == NULL)
18216 abort ();
18217
18218 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18219 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18220 for (; dyncon < dynconend; dyncon++)
18221 {
18222 Elf_Internal_Dyn dyn;
18223 asection *s;
18224
18225 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18226
18227 switch (dyn.d_tag)
18228 {
18229 default:
18230 continue;
18231
18232 case DT_PPC64_GLINK:
18233 s = htab->glink;
18234 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18235 /* We stupidly defined DT_PPC64_GLINK to be the start
18236 of glink rather than the first entry point, which is
18237 what ld.so needs, and now have a bigger stub to
18238 support automatic multiple TOCs. */
18239 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18240 break;
18241
18242 case DT_PPC64_OPD:
18243 s = bfd_get_section_by_name (output_bfd, ".opd");
18244 if (s == NULL)
18245 continue;
18246 dyn.d_un.d_ptr = s->vma;
18247 break;
18248
18249 case DT_PPC64_OPT:
18250 if ((htab->do_multi_toc && htab->multi_toc_needed)
18251 || htab->notoc_plt)
18252 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18253 if (htab->has_plt_localentry0)
18254 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18255 break;
18256
18257 case DT_PPC64_OPDSZ:
18258 s = bfd_get_section_by_name (output_bfd, ".opd");
18259 if (s == NULL)
18260 continue;
18261 dyn.d_un.d_val = s->size;
18262 break;
18263
18264 case DT_PLTGOT:
18265 s = htab->elf.splt;
18266 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18267 break;
18268
18269 case DT_JMPREL:
18270 s = htab->elf.srelplt;
18271 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18272 break;
18273
18274 case DT_PLTRELSZ:
18275 dyn.d_un.d_val = htab->elf.srelplt->size;
18276 break;
18277
18278 case DT_TEXTREL:
18279 if (htab->elf.ifunc_resolvers)
18280 info->callbacks->einfo
18281 (_("%P: warning: text relocations and GNU indirect "
18282 "functions may result in a segfault at runtime\n"));
18283 continue;
18284 }
18285
18286 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18287 }
18288 }
18289
18290 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18291 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18292 {
18293 /* Fill in the first entry in the global offset table.
18294 We use it to hold the link-time TOCbase. */
18295 bfd_put_64 (output_bfd,
18296 elf_gp (output_bfd) + TOC_BASE_OFF,
18297 htab->elf.sgot->contents);
18298
18299 /* Set .got entry size. */
18300 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18301 = 8;
18302 }
18303
18304 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18305 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18306 {
18307 /* Set .plt entry size. */
18308 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18309 = PLT_ENTRY_SIZE (htab);
18310 }
18311
18312 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18313 brlt ourselves if emitrelocations. */
18314 if (htab->brlt != NULL
18315 && htab->brlt->reloc_count != 0
18316 && !_bfd_elf_link_output_relocs (output_bfd,
18317 htab->brlt,
18318 elf_section_data (htab->brlt)->rela.hdr,
18319 elf_section_data (htab->brlt)->relocs,
18320 NULL))
18321 return false;
18322
18323 if (htab->glink != NULL
18324 && htab->glink->reloc_count != 0
18325 && !_bfd_elf_link_output_relocs (output_bfd,
18326 htab->glink,
18327 elf_section_data (htab->glink)->rela.hdr,
18328 elf_section_data (htab->glink)->relocs,
18329 NULL))
18330 return false;
18331
18332
18333 if (htab->glink_eh_frame != NULL
18334 && htab->glink_eh_frame->size != 0
18335 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18336 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18337 htab->glink_eh_frame,
18338 htab->glink_eh_frame->contents))
18339 return false;
18340
18341 /* We need to handle writing out multiple GOT sections ourselves,
18342 since we didn't add them to DYNOBJ. We know dynobj is the first
18343 bfd. */
18344 while ((dynobj = dynobj->link.next) != NULL)
18345 {
18346 asection *s;
18347
18348 if (!is_ppc64_elf (dynobj))
18349 continue;
18350
18351 s = ppc64_elf_tdata (dynobj)->got;
18352 if (s != NULL
18353 && s->size != 0
18354 && s->output_section != bfd_abs_section_ptr
18355 && !bfd_set_section_contents (output_bfd, s->output_section,
18356 s->contents, s->output_offset,
18357 s->size))
18358 return false;
18359 s = ppc64_elf_tdata (dynobj)->relgot;
18360 if (s != NULL
18361 && s->size != 0
18362 && s->output_section != bfd_abs_section_ptr
18363 && !bfd_set_section_contents (output_bfd, s->output_section,
18364 s->contents, s->output_offset,
18365 s->size))
18366 return false;
18367 }
18368
18369 return true;
18370 }
18371
18372 #include "elf64-target.h"
18373
18374 /* FreeBSD support */
18375
18376 #undef TARGET_LITTLE_SYM
18377 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18378 #undef TARGET_LITTLE_NAME
18379 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18380
18381 #undef TARGET_BIG_SYM
18382 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18383 #undef TARGET_BIG_NAME
18384 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18385
18386 #undef ELF_OSABI
18387 #define ELF_OSABI ELFOSABI_FREEBSD
18388
18389 #undef elf64_bed
18390 #define elf64_bed elf64_powerpc_fbsd_bed
18391
18392 #include "elf64-target.h"