05d2d9f91d3e62cdeba05eeba0afd9408cfc863e
[binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
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_RELROPAGESIZE ELF_MAXPAGESIZE
79 #define elf_info_to_howto ppc64_elf_info_to_howto
80
81 #define elf_backend_want_got_sym 0
82 #define elf_backend_want_plt_sym 0
83 #define elf_backend_plt_alignment 3
84 #define elf_backend_plt_not_loaded 1
85 #define elf_backend_got_header_size 8
86 #define elf_backend_want_dynrelro 1
87 #define elf_backend_can_gc_sections 1
88 #define elf_backend_can_refcount 1
89 #define elf_backend_rela_normal 1
90 #define elf_backend_dtrel_excludes_plt 1
91 #define elf_backend_default_execstack 0
92
93 #define bfd_elf64_mkobject ppc64_elf_mkobject
94 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
95 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
96 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
97 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
98 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
99 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
100 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
101 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
102 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
103
104 #define elf_backend_object_p ppc64_elf_object_p
105 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
106 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
107 #define elf_backend_write_core_note ppc64_elf_write_core_note
108 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
109 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
110 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
111 #define elf_backend_check_directives ppc64_elf_before_check_relocs
112 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
113 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
114 #define elf_backend_check_relocs ppc64_elf_check_relocs
115 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
116 #define elf_backend_gc_keep ppc64_elf_gc_keep
117 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
118 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
119 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
120 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
121 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
122 #define elf_backend_always_size_sections ppc64_elf_edit
123 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
124 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
125 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
126 #define elf_backend_action_discarded ppc64_elf_action_discarded
127 #define elf_backend_relocate_section ppc64_elf_relocate_section
128 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
129 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
130 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
131 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
132 #define elf_backend_special_sections ppc64_elf_special_sections
133 #define elf_backend_section_flags ppc64_elf_section_flags
134 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
135 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
136 #define elf_backend_get_reloc_section bfd_get_section_by_name
137
138 /* The name of the dynamic interpreter. This is put in the .interp
139 section. */
140 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
141
142 /* The size in bytes of an entry in the procedure linkage table. */
143 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
144 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
145
146 /* The initial size of the plt reserved for the dynamic linker. */
147 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
148
149 /* Offsets to some stack save slots. */
150 #define STK_LR 16
151 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
152 /* This one is dodgy. ELFv2 does not have a linker word, so use the
153 CR save slot. Used only by optimised __tls_get_addr call stub,
154 relying on __tls_get_addr_opt not saving CR.. */
155 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
156
157 /* TOC base pointers offset from start of TOC. */
158 #define TOC_BASE_OFF 0x8000
159 /* TOC base alignment. */
160 #define TOC_BASE_ALIGN 256
161
162 /* Offset of tp and dtp pointers from start of TLS block. */
163 #define TP_OFFSET 0x7000
164 #define DTP_OFFSET 0x8000
165
166 /* .plt call stub instructions. The normal stub is like this, but
167 sometimes the .plt entry crosses a 64k boundary and we need to
168 insert an addi to adjust r11. */
169 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
170 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
171 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
172 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
173 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
174 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
175 #define BCTR 0x4e800420 /* bctr */
176
177 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
178 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
179 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
180 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
181 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
182
183 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
184 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
185 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
186 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
187 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
188 #define BNECTR 0x4ca20420 /* bnectr+ */
189 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
190
191 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
192 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
193 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
194
195 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
196 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
197 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
198
199 #define LI_R11_0 0x39600000 /* li %r11,0 */
200 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
201 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
202 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
203 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
204 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
205 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
206 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
207 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
208 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
209 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
210 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
211 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
212 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
213 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
214 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
215 #define PADDI_R12_PC 0x0610000039800000ULL
216 #define PLD_R12_PC 0x04100000e5800000ULL
217 #define PNOP 0x0700000000000000ULL
218
219 /* __glink_PLTresolve stub instructions. We enter with the index in
220 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
221 #define GLINK_PLTRESOLVE_SIZE(htab) \
222 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
223 /* 0: */
224 /* .quad plt0-1f */
225 /* __glink: */
226 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
227 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
228 /* 1: */
229 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
230 /* ld %2,(0b-1b)(%11) */
231 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
232 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
233 /* ld %12,0(%11) */
234 /* ld %2,8(%11) */
235 /* mtctr %12 */
236 /* ld %11,16(%11) */
237 /* bctr */
238
239 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
240 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
241 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
242 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
243 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
244 #define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
245 #define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
246
247 /* Pad with this. */
248 #define NOP 0x60000000
249
250 /* Some other nops. */
251 #define CROR_151515 0x4def7b82
252 #define CROR_313131 0x4ffffb82
253
254 /* .glink entries for the first 32k functions are two instructions. */
255 #define LI_R0_0 0x38000000 /* li %r0,0 */
256 #define B_DOT 0x48000000 /* b . */
257
258 /* After that, we need two instructions to load the index, followed by
259 a branch. */
260 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
261 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
262
263 /* Instructions used by the save and restore reg functions. */
264 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
265 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
266 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
267 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
268 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
269 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
270 #define LI_R12_0 0x39800000 /* li %r12,0 */
271 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
272 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
273 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
274 #define BLR 0x4e800020 /* blr */
275
276 /* Since .opd is an array of descriptors and each entry will end up
277 with identical R_PPC64_RELATIVE relocs, there is really no need to
278 propagate .opd relocs; The dynamic linker should be taught to
279 relocate .opd without reloc entries. */
280 #ifndef NO_OPD_RELOCS
281 #define NO_OPD_RELOCS 0
282 #endif
283
284 #ifndef ARRAY_SIZE
285 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
286 #endif
287
288 static inline int
289 abiversion (bfd *abfd)
290 {
291 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
292 }
293
294 static inline void
295 set_abiversion (bfd *abfd, int ver)
296 {
297 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
298 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
299 }
300 \f
301 /* Relocation HOWTO's. */
302 /* Like other ELF RELA targets that don't apply multiple
303 field-altering relocations to the same localation, src_mask is
304 always zero and pcrel_offset is the same as pc_relative.
305 PowerPC can always use a zero bitpos, even when the field is not at
306 the LSB. For example, a REL24 could use rightshift=2, bisize=24
307 and bitpos=2 which matches the ABI description, or as we do here,
308 rightshift=0, bitsize=26 and bitpos=0. */
309 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
310 complain, special_func) \
311 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
312 complain_overflow_ ## complain, special_func, \
313 #type, false, 0, mask, pc_relative)
314
315 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
316
317 static reloc_howto_type ppc64_elf_howto_raw[] =
318 {
319 /* This reloc does nothing. */
320 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
321 bfd_elf_generic_reloc),
322
323 /* A standard 32 bit relocation. */
324 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
325 bfd_elf_generic_reloc),
326
327 /* An absolute 26 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
330 bfd_elf_generic_reloc),
331
332 /* A standard 16 bit relocation. */
333 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
334 bfd_elf_generic_reloc),
335
336 /* A 16 bit relocation without overflow. */
337 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
338 bfd_elf_generic_reloc),
339
340 /* Bits 16-31 of an address. */
341 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
342 bfd_elf_generic_reloc),
343
344 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
345 bits, treated as a signed number, is negative. */
346 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
347 ppc64_elf_ha_reloc),
348
349 /* An absolute 16 bit branch; the lower two bits must be zero.
350 FIXME: we don't check that, we just clear them. */
351 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
352 ppc64_elf_branch_reloc),
353
354 /* An absolute 16 bit branch, for which bit 10 should be set to
355 indicate that the branch is expected to be taken. The lower two
356 bits must be zero. */
357 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
358 ppc64_elf_brtaken_reloc),
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
364 ppc64_elf_brtaken_reloc),
365
366 /* A relative 26 bit branch; the lower two bits must be zero. */
367 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
368 ppc64_elf_branch_reloc),
369
370 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
371 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
372 ppc64_elf_branch_reloc),
373
374 /* Another variant, when p10 insns can't be used on stubs. */
375 HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
376 ppc64_elf_branch_reloc),
377
378 /* A relative 16 bit branch; the lower two bits must be zero. */
379 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
380 ppc64_elf_branch_reloc),
381
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is expected to be taken. The lower two bits must be
384 zero. */
385 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
386 ppc64_elf_brtaken_reloc),
387
388 /* A relative 16 bit branch. Bit 10 should be set to indicate that
389 the branch is not expected to be taken. The lower two bits must
390 be zero. */
391 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
392 ppc64_elf_brtaken_reloc),
393
394 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
395 symbol. */
396 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
397 ppc64_elf_unhandled_reloc),
398
399 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
400 the symbol. */
401 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
402 ppc64_elf_unhandled_reloc),
403
404 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
405 the symbol. */
406 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
407 ppc64_elf_unhandled_reloc),
408
409 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
410 the symbol. */
411 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
412 ppc64_elf_unhandled_reloc),
413
414 /* This is used only by the dynamic linker. The symbol should exist
415 both in the object being run and in some shared library. The
416 dynamic linker copies the data addressed by the symbol from the
417 shared library into the object, because the object being
418 run has to have the data at some particular address. */
419 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
420 ppc64_elf_unhandled_reloc),
421
422 /* Like R_PPC64_ADDR64, but used when setting global offset table
423 entries. */
424 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
425 ppc64_elf_unhandled_reloc),
426
427 /* Created by the link editor. Marks a procedure linkage table
428 entry for a symbol. */
429 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
430 ppc64_elf_unhandled_reloc),
431
432 /* Used only by the dynamic linker. When the object is run, this
433 doubleword64 is set to the load address of the object, plus the
434 addend. */
435 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
436 bfd_elf_generic_reloc),
437
438 /* Like R_PPC64_ADDR32, but may be unaligned. */
439 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
440 bfd_elf_generic_reloc),
441
442 /* Like R_PPC64_ADDR16, but may be unaligned. */
443 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
444 bfd_elf_generic_reloc),
445
446 /* 32-bit PC relative. */
447 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
448 bfd_elf_generic_reloc),
449
450 /* 32-bit relocation to the symbol's procedure linkage table. */
451 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
452 ppc64_elf_unhandled_reloc),
453
454 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
455 FIXME: R_PPC64_PLTREL32 not supported. */
456 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
457 ppc64_elf_unhandled_reloc),
458
459 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
460 the symbol. */
461 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
462 ppc64_elf_unhandled_reloc),
463
464 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
465 the symbol. */
466 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
467 ppc64_elf_unhandled_reloc),
468
469 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
470 the symbol. */
471 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
472 ppc64_elf_unhandled_reloc),
473
474 /* 16-bit section relative relocation. */
475 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
476 ppc64_elf_sectoff_reloc),
477
478 /* Like R_PPC64_SECTOFF, but no overflow warning. */
479 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
480 ppc64_elf_sectoff_reloc),
481
482 /* 16-bit upper half section relative relocation. */
483 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
484 ppc64_elf_sectoff_reloc),
485
486 /* 16-bit upper half adjusted section relative relocation. */
487 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
488 ppc64_elf_sectoff_ha_reloc),
489
490 /* Like R_PPC64_REL24 without touching the two least significant bits. */
491 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
492 bfd_elf_generic_reloc),
493
494 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
495
496 /* A standard 64-bit relocation. */
497 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
498 bfd_elf_generic_reloc),
499
500 /* The bits 32-47 of an address. */
501 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
502 bfd_elf_generic_reloc),
503
504 /* The bits 32-47 of an address, plus 1 if the contents of the low
505 16 bits, treated as a signed number, is negative. */
506 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
507 ppc64_elf_ha_reloc),
508
509 /* The bits 48-63 of an address. */
510 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
511 bfd_elf_generic_reloc),
512
513 /* The bits 48-63 of an address, plus 1 if the contents of the low
514 16 bits, treated as a signed number, is negative. */
515 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
516 ppc64_elf_ha_reloc),
517
518 /* Like ADDR64, but may be unaligned. */
519 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
520 bfd_elf_generic_reloc),
521
522 /* 64-bit relative relocation. */
523 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
524 bfd_elf_generic_reloc),
525
526 /* 64-bit relocation to the symbol's procedure linkage table. */
527 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
528 ppc64_elf_unhandled_reloc),
529
530 /* 64-bit PC relative relocation to the symbol's procedure linkage
531 table. */
532 /* FIXME: R_PPC64_PLTREL64 not supported. */
533 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
534 ppc64_elf_unhandled_reloc),
535
536 /* 16 bit TOC-relative relocation. */
537 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
538 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
539 ppc64_elf_toc_reloc),
540
541 /* 16 bit TOC-relative relocation without overflow. */
542 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
543 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
544 ppc64_elf_toc_reloc),
545
546 /* 16 bit TOC-relative relocation, high 16 bits. */
547 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
548 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
549 ppc64_elf_toc_reloc),
550
551 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
552 contents of the low 16 bits, treated as a signed number, is
553 negative. */
554 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
555 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
556 ppc64_elf_toc_ha_reloc),
557
558 /* 64-bit relocation; insert value of TOC base (.TOC.). */
559 /* R_PPC64_TOC 51 doubleword64 .TOC. */
560 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
561 ppc64_elf_toc64_reloc),
562
563 /* Like R_PPC64_GOT16, but also informs the link editor that the
564 value to relocate may (!) refer to a PLT entry which the link
565 editor (a) may replace with the symbol value. If the link editor
566 is unable to fully resolve the symbol, it may (b) create a PLT
567 entry and store the address to the new PLT entry in the GOT.
568 This permits lazy resolution of function symbols at run time.
569 The link editor may also skip all of this and just (c) emit a
570 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
571 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
572 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
573 ppc64_elf_unhandled_reloc),
574
575 /* Like R_PPC64_PLTGOT16, but without overflow. */
576 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
577 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
578 ppc64_elf_unhandled_reloc),
579
580 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
581 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
582 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
583 ppc64_elf_unhandled_reloc),
584
585 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
586 1 if the contents of the low 16 bits, treated as a signed number,
587 is negative. */
588 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
589 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
590 ppc64_elf_unhandled_reloc),
591
592 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
593 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
594 bfd_elf_generic_reloc),
595
596 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
597 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
598 bfd_elf_generic_reloc),
599
600 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
601 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
602 ppc64_elf_unhandled_reloc),
603
604 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
605 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
606 ppc64_elf_unhandled_reloc),
607
608 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
609 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
610 ppc64_elf_unhandled_reloc),
611
612 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
613 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
614 ppc64_elf_sectoff_reloc),
615
616 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
617 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
618 ppc64_elf_sectoff_reloc),
619
620 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
621 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
622 ppc64_elf_toc_reloc),
623
624 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
625 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
626 ppc64_elf_toc_reloc),
627
628 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
629 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
630 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
631 ppc64_elf_unhandled_reloc),
632
633 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
634 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
635 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
636 ppc64_elf_unhandled_reloc),
637
638 /* Marker relocs for TLS. */
639 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
640 bfd_elf_generic_reloc),
641
642 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
643 bfd_elf_generic_reloc),
644
645 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
646 bfd_elf_generic_reloc),
647
648 /* Marker reloc for optimizing r2 save in prologue rather than on
649 each plt call stub. */
650 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
651 bfd_elf_generic_reloc),
652
653 /* Marker relocs on inline plt call instructions. */
654 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
655 bfd_elf_generic_reloc),
656
657 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
658 bfd_elf_generic_reloc),
659
660 /* Computes the load module index of the load module that contains the
661 definition of its TLS sym. */
662 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
663 ppc64_elf_unhandled_reloc),
664
665 /* Computes a dtv-relative displacement, the difference between the value
666 of sym+add and the base address of the thread-local storage block that
667 contains the definition of sym, minus 0x8000. */
668 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
669 ppc64_elf_unhandled_reloc),
670
671 /* A 16 bit dtprel reloc. */
672 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
673 ppc64_elf_unhandled_reloc),
674
675 /* Like DTPREL16, but no overflow. */
676 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
677 ppc64_elf_unhandled_reloc),
678
679 /* Like DTPREL16_LO, but next higher group of 16 bits. */
680 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
681 ppc64_elf_unhandled_reloc),
682
683 /* Like DTPREL16_HI, but adjust for low 16 bits. */
684 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
685 ppc64_elf_unhandled_reloc),
686
687 /* Like DTPREL16_HI, but next higher group of 16 bits. */
688 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
689 ppc64_elf_unhandled_reloc),
690
691 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
692 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
693 ppc64_elf_unhandled_reloc),
694
695 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
696 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
697 ppc64_elf_unhandled_reloc),
698
699 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
700 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
701 ppc64_elf_unhandled_reloc),
702
703 /* Like DTPREL16, but for insns with a DS field. */
704 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
705 ppc64_elf_unhandled_reloc),
706
707 /* Like DTPREL16_DS, but no overflow. */
708 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
709 ppc64_elf_unhandled_reloc),
710
711 /* Computes a tp-relative displacement, the difference between the value of
712 sym+add and the value of the thread pointer (r13). */
713 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
714 ppc64_elf_unhandled_reloc),
715
716 /* A 16 bit tprel reloc. */
717 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
718 ppc64_elf_unhandled_reloc),
719
720 /* Like TPREL16, but no overflow. */
721 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
722 ppc64_elf_unhandled_reloc),
723
724 /* Like TPREL16_LO, but next higher group of 16 bits. */
725 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
726 ppc64_elf_unhandled_reloc),
727
728 /* Like TPREL16_HI, but adjust for low 16 bits. */
729 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
730 ppc64_elf_unhandled_reloc),
731
732 /* Like TPREL16_HI, but next higher group of 16 bits. */
733 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
734 ppc64_elf_unhandled_reloc),
735
736 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
737 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
738 ppc64_elf_unhandled_reloc),
739
740 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
741 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
742 ppc64_elf_unhandled_reloc),
743
744 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
745 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
746 ppc64_elf_unhandled_reloc),
747
748 /* Like TPREL16, but for insns with a DS field. */
749 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
750 ppc64_elf_unhandled_reloc),
751
752 /* Like TPREL16_DS, but no overflow. */
753 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
754 ppc64_elf_unhandled_reloc),
755
756 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
757 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
758 to the first entry relative to the TOC base (r2). */
759 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
760 ppc64_elf_unhandled_reloc),
761
762 /* Like GOT_TLSGD16, but no overflow. */
763 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
764 ppc64_elf_unhandled_reloc),
765
766 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
767 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
768 ppc64_elf_unhandled_reloc),
769
770 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
771 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
772 ppc64_elf_unhandled_reloc),
773
774 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
775 with values (sym+add)@dtpmod and zero, and computes the offset to the
776 first entry relative to the TOC base (r2). */
777 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
778 ppc64_elf_unhandled_reloc),
779
780 /* Like GOT_TLSLD16, but no overflow. */
781 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
782 ppc64_elf_unhandled_reloc),
783
784 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
785 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
786 ppc64_elf_unhandled_reloc),
787
788 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
789 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
790 ppc64_elf_unhandled_reloc),
791
792 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
793 the offset to the entry relative to the TOC base (r2). */
794 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
795 ppc64_elf_unhandled_reloc),
796
797 /* Like GOT_DTPREL16_DS, but no overflow. */
798 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
799 ppc64_elf_unhandled_reloc),
800
801 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
802 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
803 ppc64_elf_unhandled_reloc),
804
805 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
806 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
807 ppc64_elf_unhandled_reloc),
808
809 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
810 offset to the entry relative to the TOC base (r2). */
811 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
812 ppc64_elf_unhandled_reloc),
813
814 /* Like GOT_TPREL16_DS, but no overflow. */
815 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
816 ppc64_elf_unhandled_reloc),
817
818 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
819 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
820 ppc64_elf_unhandled_reloc),
821
822 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
823 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
824 ppc64_elf_unhandled_reloc),
825
826 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
827 ppc64_elf_unhandled_reloc),
828
829 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
830 bfd_elf_generic_reloc),
831
832 /* A 16 bit relative relocation. */
833 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
834 bfd_elf_generic_reloc),
835
836 /* A 16 bit relative relocation without overflow. */
837 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
838 bfd_elf_generic_reloc),
839
840 /* The high order 16 bits of a relative address. */
841 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
842 bfd_elf_generic_reloc),
843
844 /* The high order 16 bits of a relative address, plus 1 if the contents of
845 the low 16 bits, treated as a signed number, is negative. */
846 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
847 ppc64_elf_ha_reloc),
848
849 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
850 bfd_elf_generic_reloc),
851
852 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
853 ppc64_elf_ha_reloc),
854
855 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
856 bfd_elf_generic_reloc),
857
858 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
859 ppc64_elf_ha_reloc),
860
861 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
862 bfd_elf_generic_reloc),
863
864 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
865 ppc64_elf_ha_reloc),
866
867 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
868 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
869 ppc64_elf_ha_reloc),
870
871 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
872 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
873 ppc64_elf_ha_reloc),
874
875 /* Like R_PPC64_ADDR16_HI, but no overflow. */
876 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
877 bfd_elf_generic_reloc),
878
879 /* Like R_PPC64_ADDR16_HA, but no overflow. */
880 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
881 ppc64_elf_ha_reloc),
882
883 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
884 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
885 ppc64_elf_unhandled_reloc),
886
887 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
888 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
889 ppc64_elf_unhandled_reloc),
890
891 /* Like R_PPC64_TPREL16_HI, but no overflow. */
892 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
893 ppc64_elf_unhandled_reloc),
894
895 /* Like R_PPC64_TPREL16_HA, but no overflow. */
896 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
897 ppc64_elf_unhandled_reloc),
898
899 /* Marker reloc on ELFv2 large-model function entry. */
900 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
901 bfd_elf_generic_reloc),
902
903 /* Like ADDR64, but use local entry point of function. */
904 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
905 bfd_elf_generic_reloc),
906
907 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
908 bfd_elf_generic_reloc),
909
910 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
911 bfd_elf_generic_reloc),
912
913 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
914 bfd_elf_generic_reloc),
915
916 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
917 ppc64_elf_prefix_reloc),
918
919 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
920 ppc64_elf_prefix_reloc),
921
922 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
923 ppc64_elf_prefix_reloc),
924
925 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
926 ppc64_elf_prefix_reloc),
927
928 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
929 ppc64_elf_prefix_reloc),
930
931 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
932 ppc64_elf_unhandled_reloc),
933
934 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
935 ppc64_elf_unhandled_reloc),
936
937 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
938 ppc64_elf_unhandled_reloc),
939
940 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
941 ppc64_elf_unhandled_reloc),
942
943 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
944 ppc64_elf_unhandled_reloc),
945
946 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
947 ppc64_elf_unhandled_reloc),
948
949 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
950 ppc64_elf_unhandled_reloc),
951
952 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
953 ppc64_elf_unhandled_reloc),
954
955 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
956 ppc64_elf_unhandled_reloc),
957
958 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
959 bfd_elf_generic_reloc),
960
961 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
962 ppc64_elf_ha_reloc),
963
964 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
965 bfd_elf_generic_reloc),
966
967 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
968 ppc64_elf_ha_reloc),
969
970 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
971 bfd_elf_generic_reloc),
972
973 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
974 ppc64_elf_ha_reloc),
975
976 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
977 bfd_elf_generic_reloc),
978
979 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
980 ppc64_elf_ha_reloc),
981
982 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
983 ppc64_elf_prefix_reloc),
984
985 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
986 ppc64_elf_prefix_reloc),
987
988 /* GNU extension to record C++ vtable hierarchy. */
989 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
990 NULL),
991
992 /* GNU extension to record C++ vtable member usage. */
993 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
994 NULL),
995 };
996
997 \f
998 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
999 be done. */
1000
1001 static void
1002 ppc_howto_init (void)
1003 {
1004 unsigned int i, type;
1005
1006 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1007 {
1008 type = ppc64_elf_howto_raw[i].type;
1009 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1010 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1011 }
1012 }
1013
1014 static reloc_howto_type *
1015 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1016 {
1017 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1018
1019 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1020 /* Initialize howto table if needed. */
1021 ppc_howto_init ();
1022
1023 switch (code)
1024 {
1025 default:
1026 /* xgettext:c-format */
1027 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1028 (int) code);
1029 bfd_set_error (bfd_error_bad_value);
1030 return NULL;
1031
1032 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1033 break;
1034 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1035 break;
1036 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1037 break;
1038 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1039 break;
1040 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1041 break;
1042 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1043 break;
1044 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1045 break;
1046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1047 break;
1048 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1049 break;
1050 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1051 break;
1052 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1053 break;
1054 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1055 break;
1056 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1057 break;
1058 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1059 break;
1060 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1061 break;
1062 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1063 break;
1064 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1065 break;
1066 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1067 break;
1068 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1069 break;
1070 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1071 break;
1072 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1073 break;
1074 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1075 break;
1076 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1077 break;
1078 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1079 break;
1080 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1081 break;
1082 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1083 break;
1084 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1085 break;
1086 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1087 break;
1088 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1089 break;
1090 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1091 break;
1092 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1093 break;
1094 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1095 break;
1096 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1097 break;
1098 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1099 break;
1100 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1101 break;
1102 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1103 break;
1104 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1105 break;
1106 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1107 break;
1108 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1109 break;
1110 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1111 break;
1112 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1113 break;
1114 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1115 break;
1116 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1117 break;
1118 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1119 break;
1120 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1121 break;
1122 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1123 break;
1124 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1125 break;
1126 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1127 break;
1128 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1129 break;
1130 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1131 break;
1132 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1133 break;
1134 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1135 break;
1136 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1137 break;
1138 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1139 break;
1140 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1141 break;
1142 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1143 break;
1144 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1145 break;
1146 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1147 break;
1148 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1149 break;
1150 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1151 break;
1152 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1153 break;
1154 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1155 break;
1156 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1157 break;
1158 case BFD_RELOC_PPC64_TLS_PCREL:
1159 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1160 break;
1161 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1162 break;
1163 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1164 break;
1165 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1166 break;
1167 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1168 break;
1169 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1170 break;
1171 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1172 break;
1173 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1174 break;
1175 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1176 break;
1177 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1178 break;
1179 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1180 break;
1181 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1182 break;
1183 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1184 break;
1185 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1186 break;
1187 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1188 break;
1189 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1190 break;
1191 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1192 break;
1193 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1218 break;
1219 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1220 break;
1221 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1222 break;
1223 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1226 break;
1227 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1228 break;
1229 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1238 break;
1239 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1240 break;
1241 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1250 break;
1251 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1252 break;
1253 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1254 break;
1255 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1256 break;
1257 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1258 break;
1259 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1260 break;
1261 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1262 break;
1263 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1270 break;
1271 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1272 break;
1273 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1274 break;
1275 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1276 break;
1277 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1278 break;
1279 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1280 break;
1281 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1282 break;
1283 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1284 break;
1285 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1286 break;
1287 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1288 break;
1289 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1290 break;
1291 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1292 break;
1293 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1294 break;
1295 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1296 break;
1297 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1298 break;
1299 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1300 break;
1301 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1302 break;
1303 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1304 break;
1305 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1306 break;
1307 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1308 break;
1309 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1312 break;
1313 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1314 break;
1315 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1316 break;
1317 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1320 break;
1321 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1322 break;
1323 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1324 break;
1325 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1326 break;
1327 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1328 break;
1329 }
1330
1331 return ppc64_elf_howto_table[r];
1332 };
1333
1334 static reloc_howto_type *
1335 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1336 {
1337 unsigned int i;
1338 static char *compat_map[][2] = {
1339 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1340 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1341 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1342 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1343 };
1344
1345 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1346 if (ppc64_elf_howto_raw[i].name != NULL
1347 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1348 return &ppc64_elf_howto_raw[i];
1349
1350 /* Handle old names of relocations in case they were used by
1351 .reloc directives.
1352 FIXME: Remove this soon. Mapping the reloc names is very likely
1353 completely unnecessary. */
1354 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1355 if (strcasecmp (compat_map[i][0], r_name) == 0)
1356 {
1357 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1358 compat_map[i][1], compat_map[i][0]);
1359 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1360 }
1361
1362 return NULL;
1363 }
1364
1365 /* Set the howto pointer for a PowerPC ELF reloc. */
1366
1367 static bool
1368 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1369 Elf_Internal_Rela *dst)
1370 {
1371 unsigned int type;
1372
1373 /* Initialize howto table if needed. */
1374 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1375 ppc_howto_init ();
1376
1377 type = ELF64_R_TYPE (dst->r_info);
1378 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1379 {
1380 /* xgettext:c-format */
1381 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1382 abfd, type);
1383 bfd_set_error (bfd_error_bad_value);
1384 return false;
1385 }
1386 cache_ptr->howto = ppc64_elf_howto_table[type];
1387 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1388 {
1389 /* xgettext:c-format */
1390 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1391 abfd, type);
1392 bfd_set_error (bfd_error_bad_value);
1393 return false;
1394 }
1395
1396 return true;
1397 }
1398
1399 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1400
1401 static bfd_reloc_status_type
1402 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1403 void *data, asection *input_section,
1404 bfd *output_bfd, char **error_message)
1405 {
1406 enum elf_ppc64_reloc_type r_type;
1407 long insn;
1408 bfd_size_type octets;
1409 bfd_vma value;
1410
1411 /* If this is a relocatable link (output_bfd test tells us), just
1412 call the generic function. Any adjustment will be done at final
1413 link time. */
1414 if (output_bfd != NULL)
1415 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1416 input_section, output_bfd, error_message);
1417
1418 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1419 We won't actually be using the low bits, so trashing them
1420 doesn't matter. */
1421 r_type = reloc_entry->howto->type;
1422 if (r_type == R_PPC64_ADDR16_HIGHERA34
1423 || r_type == R_PPC64_ADDR16_HIGHESTA34
1424 || r_type == R_PPC64_REL16_HIGHERA34
1425 || r_type == R_PPC64_REL16_HIGHESTA34)
1426 reloc_entry->addend += 1ULL << 33;
1427 else
1428 reloc_entry->addend += 1U << 15;
1429 if (r_type != R_PPC64_REL16DX_HA)
1430 return bfd_reloc_continue;
1431
1432 value = 0;
1433 if (!bfd_is_com_section (symbol->section))
1434 value = symbol->value;
1435 value += (reloc_entry->addend
1436 + symbol->section->output_offset
1437 + symbol->section->output_section->vma);
1438 value -= (reloc_entry->address
1439 + input_section->output_offset
1440 + input_section->output_section->vma);
1441 value = (bfd_signed_vma) value >> 16;
1442
1443 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1444 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1445 input_section, octets))
1446 return bfd_reloc_outofrange;
1447
1448 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1449 insn &= ~0x1fffc1;
1450 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1451 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1452 if (value + 0x8000 > 0xffff)
1453 return bfd_reloc_overflow;
1454 return bfd_reloc_ok;
1455 }
1456
1457 static bfd_reloc_status_type
1458 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1459 void *data, asection *input_section,
1460 bfd *output_bfd, char **error_message)
1461 {
1462 if (output_bfd != NULL)
1463 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1464 input_section, output_bfd, error_message);
1465
1466 if (strcmp (symbol->section->name, ".opd") == 0
1467 && (symbol->section->owner->flags & DYNAMIC) == 0)
1468 {
1469 bfd_vma dest = opd_entry_value (symbol->section,
1470 symbol->value + reloc_entry->addend,
1471 NULL, NULL, false);
1472 if (dest != (bfd_vma) -1)
1473 reloc_entry->addend = dest - (symbol->value
1474 + symbol->section->output_section->vma
1475 + symbol->section->output_offset);
1476 }
1477 else
1478 {
1479 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1480
1481 if (symbol->section->owner != abfd
1482 && symbol->section->owner != NULL
1483 && abiversion (symbol->section->owner) >= 2)
1484 {
1485 unsigned int i;
1486
1487 for (i = 0; i < symbol->section->owner->symcount; ++i)
1488 {
1489 asymbol *symdef = symbol->section->owner->outsymbols[i];
1490
1491 if (strcmp (symdef->name, symbol->name) == 0)
1492 {
1493 elfsym = (elf_symbol_type *) symdef;
1494 break;
1495 }
1496 }
1497 }
1498 reloc_entry->addend
1499 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1500 }
1501 return bfd_reloc_continue;
1502 }
1503
1504 static bfd_reloc_status_type
1505 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1506 void *data, asection *input_section,
1507 bfd *output_bfd, char **error_message)
1508 {
1509 long insn;
1510 enum elf_ppc64_reloc_type r_type;
1511 bfd_size_type octets;
1512 /* Assume 'at' branch hints. */
1513 bool is_isa_v2 = true;
1514
1515 /* If this is a relocatable link (output_bfd test tells us), just
1516 call the generic function. Any adjustment will be done at final
1517 link time. */
1518 if (output_bfd != NULL)
1519 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1520 input_section, output_bfd, error_message);
1521
1522 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1523 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1524 input_section, octets))
1525 return bfd_reloc_outofrange;
1526
1527 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1528 insn &= ~(0x01 << 21);
1529 r_type = reloc_entry->howto->type;
1530 if (r_type == R_PPC64_ADDR14_BRTAKEN
1531 || r_type == R_PPC64_REL14_BRTAKEN)
1532 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1533
1534 if (is_isa_v2)
1535 {
1536 /* Set 'a' bit. This is 0b00010 in BO field for branch
1537 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1538 for branch on CTR insns (BO == 1a00t or 1a01t). */
1539 if ((insn & (0x14 << 21)) == (0x04 << 21))
1540 insn |= 0x02 << 21;
1541 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1542 insn |= 0x08 << 21;
1543 else
1544 goto out;
1545 }
1546 else
1547 {
1548 bfd_vma target = 0;
1549 bfd_vma from;
1550
1551 if (!bfd_is_com_section (symbol->section))
1552 target = symbol->value;
1553 target += symbol->section->output_section->vma;
1554 target += symbol->section->output_offset;
1555 target += reloc_entry->addend;
1556
1557 from = (reloc_entry->address
1558 + input_section->output_offset
1559 + input_section->output_section->vma);
1560
1561 /* Invert 'y' bit if not the default. */
1562 if ((bfd_signed_vma) (target - from) < 0)
1563 insn ^= 0x01 << 21;
1564 }
1565 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1566 out:
1567 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1568 input_section, output_bfd, error_message);
1569 }
1570
1571 static bfd_reloc_status_type
1572 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1573 void *data, asection *input_section,
1574 bfd *output_bfd, char **error_message)
1575 {
1576 /* If this is a relocatable link (output_bfd test tells us), just
1577 call the generic function. Any adjustment will be done at final
1578 link time. */
1579 if (output_bfd != NULL)
1580 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1581 input_section, output_bfd, error_message);
1582
1583 /* Subtract the symbol section base address. */
1584 reloc_entry->addend -= symbol->section->output_section->vma;
1585 return bfd_reloc_continue;
1586 }
1587
1588 static bfd_reloc_status_type
1589 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
1592 {
1593 /* If this is a relocatable link (output_bfd test tells us), just
1594 call the generic function. Any adjustment will be done at final
1595 link time. */
1596 if (output_bfd != NULL)
1597 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1598 input_section, output_bfd, error_message);
1599
1600 /* Subtract the symbol section base address. */
1601 reloc_entry->addend -= symbol->section->output_section->vma;
1602
1603 /* Adjust the addend for sign extension of the low 16 bits. */
1604 reloc_entry->addend += 0x8000;
1605 return bfd_reloc_continue;
1606 }
1607
1608 static bfd_reloc_status_type
1609 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1610 void *data, asection *input_section,
1611 bfd *output_bfd, char **error_message)
1612 {
1613 bfd_vma TOCstart;
1614
1615 /* If this is a relocatable link (output_bfd test tells us), just
1616 call the generic function. Any adjustment will be done at final
1617 link time. */
1618 if (output_bfd != NULL)
1619 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1620 input_section, output_bfd, error_message);
1621
1622 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1623 if (TOCstart == 0)
1624 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1625
1626 /* Subtract the TOC base address. */
1627 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1628 return bfd_reloc_continue;
1629 }
1630
1631 static bfd_reloc_status_type
1632 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1633 void *data, asection *input_section,
1634 bfd *output_bfd, char **error_message)
1635 {
1636 bfd_vma TOCstart;
1637
1638 /* If this is a relocatable link (output_bfd test tells us), just
1639 call the generic function. Any adjustment will be done at final
1640 link time. */
1641 if (output_bfd != NULL)
1642 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1643 input_section, output_bfd, error_message);
1644
1645 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1646 if (TOCstart == 0)
1647 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1648
1649 /* Subtract the TOC base address. */
1650 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1651
1652 /* Adjust the addend for sign extension of the low 16 bits. */
1653 reloc_entry->addend += 0x8000;
1654 return bfd_reloc_continue;
1655 }
1656
1657 static bfd_reloc_status_type
1658 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1659 void *data, asection *input_section,
1660 bfd *output_bfd, char **error_message)
1661 {
1662 bfd_vma TOCstart;
1663 bfd_size_type octets;
1664
1665 /* If this is a relocatable link (output_bfd test tells us), just
1666 call the generic function. Any adjustment will be done at final
1667 link time. */
1668 if (output_bfd != NULL)
1669 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1670 input_section, output_bfd, error_message);
1671
1672 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1673 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1674 input_section, octets))
1675 return bfd_reloc_outofrange;
1676
1677 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1678 if (TOCstart == 0)
1679 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1680
1681 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1682 return bfd_reloc_ok;
1683 }
1684
1685 static bfd_reloc_status_type
1686 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1687 void *data, asection *input_section,
1688 bfd *output_bfd, char **error_message)
1689 {
1690 uint64_t insn;
1691 bfd_vma targ;
1692 bfd_size_type octets;
1693
1694 if (output_bfd != NULL)
1695 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1696 input_section, output_bfd, error_message);
1697
1698 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1699 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1700 input_section, octets))
1701 return bfd_reloc_outofrange;
1702
1703 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1704 insn <<= 32;
1705 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1706
1707 targ = (symbol->section->output_section->vma
1708 + symbol->section->output_offset
1709 + reloc_entry->addend);
1710 if (!bfd_is_com_section (symbol->section))
1711 targ += symbol->value;
1712 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1713 targ += 1ULL << 33;
1714 if (reloc_entry->howto->pc_relative)
1715 {
1716 bfd_vma from = (reloc_entry->address
1717 + input_section->output_offset
1718 + input_section->output_section->vma);
1719 targ -=from;
1720 }
1721 targ >>= reloc_entry->howto->rightshift;
1722 insn &= ~reloc_entry->howto->dst_mask;
1723 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1724 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1725 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1726 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1727 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1728 >= 1ULL << reloc_entry->howto->bitsize))
1729 return bfd_reloc_overflow;
1730 return bfd_reloc_ok;
1731 }
1732
1733 static bfd_reloc_status_type
1734 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1735 void *data, asection *input_section,
1736 bfd *output_bfd, char **error_message)
1737 {
1738 /* If this is a relocatable link (output_bfd test tells us), just
1739 call the generic function. Any adjustment will be done at final
1740 link time. */
1741 if (output_bfd != NULL)
1742 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1743 input_section, output_bfd, error_message);
1744
1745 if (error_message != NULL)
1746 {
1747 static char *message;
1748 free (message);
1749 if (asprintf (&message, _("generic linker can't handle %s"),
1750 reloc_entry->howto->name) < 0)
1751 message = NULL;
1752 *error_message = message;
1753 }
1754 return bfd_reloc_dangerous;
1755 }
1756
1757 /* Track GOT entries needed for a given symbol. We might need more
1758 than one got entry per symbol. */
1759 struct got_entry
1760 {
1761 struct got_entry *next;
1762
1763 /* The symbol addend that we'll be placing in the GOT. */
1764 bfd_vma addend;
1765
1766 /* Unlike other ELF targets, we use separate GOT entries for the same
1767 symbol referenced from different input files. This is to support
1768 automatic multiple TOC/GOT sections, where the TOC base can vary
1769 from one input file to another. After partitioning into TOC groups
1770 we merge entries within the group.
1771
1772 Point to the BFD owning this GOT entry. */
1773 bfd *owner;
1774
1775 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1776 TLS_TPREL or TLS_DTPREL for tls entries. */
1777 unsigned char tls_type;
1778
1779 /* Non-zero if got.ent points to real entry. */
1780 unsigned char is_indirect;
1781
1782 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1783 union
1784 {
1785 bfd_signed_vma refcount;
1786 bfd_vma offset;
1787 struct got_entry *ent;
1788 } got;
1789 };
1790
1791 /* The same for PLT. */
1792 struct plt_entry
1793 {
1794 struct plt_entry *next;
1795
1796 bfd_vma addend;
1797
1798 union
1799 {
1800 bfd_signed_vma refcount;
1801 bfd_vma offset;
1802 } plt;
1803 };
1804
1805 struct ppc64_elf_obj_tdata
1806 {
1807 struct elf_obj_tdata elf;
1808
1809 /* Shortcuts to dynamic linker sections. */
1810 asection *got;
1811 asection *relgot;
1812
1813 /* Used during garbage collection. We attach global symbols defined
1814 on removed .opd entries to this section so that the sym is removed. */
1815 asection *deleted_section;
1816
1817 /* TLS local dynamic got entry handling. Support for multiple GOT
1818 sections means we potentially need one of these for each input bfd. */
1819 struct got_entry tlsld_got;
1820
1821 union
1822 {
1823 /* A copy of relocs before they are modified for --emit-relocs. */
1824 Elf_Internal_Rela *relocs;
1825
1826 /* Section contents. */
1827 bfd_byte *contents;
1828 } opd;
1829
1830 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1831 the reloc to be in the range -32768 to 32767. */
1832 unsigned int has_small_toc_reloc : 1;
1833
1834 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1835 instruction not one we handle. */
1836 unsigned int unexpected_toc_insn : 1;
1837
1838 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1839 this file. */
1840 unsigned int has_optrel : 1;
1841 };
1842
1843 #define ppc64_elf_tdata(bfd) \
1844 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1845
1846 #define ppc64_tlsld_got(bfd) \
1847 (&ppc64_elf_tdata (bfd)->tlsld_got)
1848
1849 #define is_ppc64_elf(bfd) \
1850 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1851 && elf_object_id (bfd) == PPC64_ELF_DATA)
1852
1853 /* Override the generic function because we store some extras. */
1854
1855 static bool
1856 ppc64_elf_mkobject (bfd *abfd)
1857 {
1858 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1859 PPC64_ELF_DATA);
1860 }
1861
1862 /* Fix bad default arch selected for a 64 bit input bfd when the
1863 default is 32 bit. Also select arch based on apuinfo. */
1864
1865 static bool
1866 ppc64_elf_object_p (bfd *abfd)
1867 {
1868 if (!abfd->arch_info->the_default)
1869 return true;
1870
1871 if (abfd->arch_info->bits_per_word == 32)
1872 {
1873 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1874
1875 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1876 {
1877 /* Relies on arch after 32 bit default being 64 bit default. */
1878 abfd->arch_info = abfd->arch_info->next;
1879 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1880 }
1881 }
1882 return _bfd_elf_ppc_set_arch (abfd);
1883 }
1884
1885 /* Support for core dump NOTE sections. */
1886
1887 static bool
1888 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1889 {
1890 size_t offset, size;
1891
1892 if (note->descsz != 504)
1893 return false;
1894
1895 /* pr_cursig */
1896 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1897
1898 /* pr_pid */
1899 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1900
1901 /* pr_reg */
1902 offset = 112;
1903 size = 384;
1904
1905 /* Make a ".reg/999" section. */
1906 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1907 size, note->descpos + offset);
1908 }
1909
1910 static bool
1911 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1912 {
1913 if (note->descsz != 136)
1914 return false;
1915
1916 elf_tdata (abfd)->core->pid
1917 = bfd_get_32 (abfd, note->descdata + 24);
1918 elf_tdata (abfd)->core->program
1919 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1920 elf_tdata (abfd)->core->command
1921 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1922
1923 return true;
1924 }
1925
1926 static char *
1927 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1928 ...)
1929 {
1930 switch (note_type)
1931 {
1932 default:
1933 return NULL;
1934
1935 case NT_PRPSINFO:
1936 {
1937 char data[136] ATTRIBUTE_NONSTRING;
1938 va_list ap;
1939
1940 va_start (ap, note_type);
1941 memset (data, 0, sizeof (data));
1942 strncpy (data + 40, va_arg (ap, const char *), 16);
1943 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1944 DIAGNOSTIC_PUSH;
1945 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1946 -Wstringop-truncation:
1947 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1948 */
1949 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1950 #endif
1951 strncpy (data + 56, va_arg (ap, const char *), 80);
1952 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1953 DIAGNOSTIC_POP;
1954 #endif
1955 va_end (ap);
1956 return elfcore_write_note (abfd, buf, bufsiz,
1957 "CORE", note_type, data, sizeof (data));
1958 }
1959
1960 case NT_PRSTATUS:
1961 {
1962 char data[504];
1963 va_list ap;
1964 long pid;
1965 int cursig;
1966 const void *greg;
1967
1968 va_start (ap, note_type);
1969 memset (data, 0, 112);
1970 pid = va_arg (ap, long);
1971 bfd_put_32 (abfd, pid, data + 32);
1972 cursig = va_arg (ap, int);
1973 bfd_put_16 (abfd, cursig, data + 12);
1974 greg = va_arg (ap, const void *);
1975 memcpy (data + 112, greg, 384);
1976 memset (data + 496, 0, 8);
1977 va_end (ap);
1978 return elfcore_write_note (abfd, buf, bufsiz,
1979 "CORE", note_type, data, sizeof (data));
1980 }
1981 }
1982 }
1983
1984 /* Add extra PPC sections. */
1985
1986 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1987 {
1988 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1989 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1990 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1991 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1993 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1994 { NULL, 0, 0, 0, 0 }
1995 };
1996
1997 enum _ppc64_sec_type {
1998 sec_normal = 0,
1999 sec_opd = 1,
2000 sec_toc = 2
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 } u;
2031
2032 enum _ppc64_sec_type sec_type:2;
2033
2034 /* Flag set when small branches are detected. Used to
2035 select suitable defaults for the stub group size. */
2036 unsigned int has_14bit_branch:1;
2037
2038 /* Flag set when PLTCALL relocs are detected. */
2039 unsigned int has_pltcall:1;
2040
2041 /* Flag set when section has PLT/GOT/TOC relocations that can be
2042 optimised. */
2043 unsigned int has_optrel:1;
2044 };
2045
2046 #define ppc64_elf_section_data(sec) \
2047 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2048
2049 static bool
2050 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2051 {
2052 if (!sec->used_by_bfd)
2053 {
2054 struct _ppc64_elf_section_data *sdata;
2055 size_t amt = sizeof (*sdata);
2056
2057 sdata = bfd_zalloc (abfd, amt);
2058 if (sdata == NULL)
2059 return false;
2060 sec->used_by_bfd = sdata;
2061 }
2062
2063 return _bfd_elf_new_section_hook (abfd, sec);
2064 }
2065
2066 static bool
2067 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2068 {
2069 const char *name = hdr->bfd_section->name;
2070
2071 if (startswith (name, ".sbss")
2072 || startswith (name, ".sdata"))
2073 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2074
2075 return true;
2076 }
2077
2078 static struct _opd_sec_data *
2079 get_opd_info (asection * sec)
2080 {
2081 if (sec != NULL
2082 && ppc64_elf_section_data (sec) != NULL
2083 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2084 return &ppc64_elf_section_data (sec)->u.opd;
2085 return NULL;
2086 }
2087 \f
2088 /* Parameters for the qsort hook. */
2089 static bool synthetic_relocatable;
2090 static const asection *synthetic_opd;
2091
2092 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2093
2094 static int
2095 compare_symbols (const void *ap, const void *bp)
2096 {
2097 const asymbol *a = *(const asymbol **) ap;
2098 const asymbol *b = *(const asymbol **) bp;
2099
2100 /* Section symbols first. */
2101 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2102 return -1;
2103 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2104 return 1;
2105
2106 /* then .opd symbols. */
2107 if (synthetic_opd != NULL)
2108 {
2109 if (strcmp (a->section->name, ".opd") == 0
2110 && strcmp (b->section->name, ".opd") != 0)
2111 return -1;
2112 if (strcmp (a->section->name, ".opd") != 0
2113 && strcmp (b->section->name, ".opd") == 0)
2114 return 1;
2115 }
2116
2117 /* then other code symbols. */
2118 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2119 == (SEC_CODE | SEC_ALLOC))
2120 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2121 != (SEC_CODE | SEC_ALLOC)))
2122 return -1;
2123
2124 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2125 != (SEC_CODE | SEC_ALLOC))
2126 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2127 == (SEC_CODE | SEC_ALLOC)))
2128 return 1;
2129
2130 if (synthetic_relocatable)
2131 {
2132 if (a->section->id < b->section->id)
2133 return -1;
2134
2135 if (a->section->id > b->section->id)
2136 return 1;
2137 }
2138
2139 if (a->value + a->section->vma < b->value + b->section->vma)
2140 return -1;
2141
2142 if (a->value + a->section->vma > b->value + b->section->vma)
2143 return 1;
2144
2145 /* For syms with the same value, prefer strong dynamic global function
2146 syms over other syms. */
2147 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2148 return -1;
2149
2150 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2151 return 1;
2152
2153 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 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_WEAK) == 0 && (b->flags & BSF_WEAK) != 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_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2166 return -1;
2167
2168 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2169 return 1;
2170
2171 /* Finally, sort on where the symbol is in memory. The symbols will
2172 be in at most two malloc'd blocks, one for static syms, one for
2173 dynamic syms, and we distinguish the two blocks above by testing
2174 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2175 originally in the same order as the symbols (and we're not
2176 sorting the symbols themselves), this ensures a stable sort. */
2177 if (a < b)
2178 return -1;
2179 if (a > b)
2180 return 1;
2181 return 0;
2182 }
2183
2184 /* Search SYMS for a symbol of the given VALUE. */
2185
2186 static asymbol *
2187 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2188 bfd_vma value)
2189 {
2190 size_t mid;
2191
2192 if (id == (unsigned) -1)
2193 {
2194 while (lo < hi)
2195 {
2196 mid = (lo + hi) >> 1;
2197 if (syms[mid]->value + syms[mid]->section->vma < value)
2198 lo = mid + 1;
2199 else if (syms[mid]->value + syms[mid]->section->vma > value)
2200 hi = mid;
2201 else
2202 return syms[mid];
2203 }
2204 }
2205 else
2206 {
2207 while (lo < hi)
2208 {
2209 mid = (lo + hi) >> 1;
2210 if (syms[mid]->section->id < id)
2211 lo = mid + 1;
2212 else if (syms[mid]->section->id > id)
2213 hi = mid;
2214 else if (syms[mid]->value < value)
2215 lo = mid + 1;
2216 else if (syms[mid]->value > value)
2217 hi = mid;
2218 else
2219 return syms[mid];
2220 }
2221 }
2222 return NULL;
2223 }
2224
2225 static bool
2226 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2227 {
2228 bfd_vma vma = *(bfd_vma *) ptr;
2229 return ((section->flags & SEC_ALLOC) != 0
2230 && section->vma <= vma
2231 && vma < section->vma + section->size);
2232 }
2233
2234 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2235 entry syms. Also generate @plt symbols for the glink branch table.
2236 Returns count of synthetic symbols in RET or -1 on error. */
2237
2238 static long
2239 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2240 long static_count, asymbol **static_syms,
2241 long dyn_count, asymbol **dyn_syms,
2242 asymbol **ret)
2243 {
2244 asymbol *s;
2245 size_t i, j, count;
2246 char *names;
2247 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2248 asection *opd = NULL;
2249 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2250 asymbol **syms;
2251 int abi = abiversion (abfd);
2252
2253 *ret = NULL;
2254
2255 if (abi < 2)
2256 {
2257 opd = bfd_get_section_by_name (abfd, ".opd");
2258 if (opd == NULL && abi == 1)
2259 return 0;
2260 }
2261
2262 syms = NULL;
2263 codesecsym = 0;
2264 codesecsymend = 0;
2265 secsymend = 0;
2266 opdsymend = 0;
2267 symcount = 0;
2268 if (opd != NULL)
2269 {
2270 symcount = static_count;
2271 if (!relocatable)
2272 symcount += dyn_count;
2273 if (symcount == 0)
2274 return 0;
2275
2276 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2277 if (syms == NULL)
2278 return -1;
2279
2280 if (!relocatable && static_count != 0 && dyn_count != 0)
2281 {
2282 /* Use both symbol tables. */
2283 memcpy (syms, static_syms, static_count * sizeof (*syms));
2284 memcpy (syms + static_count, dyn_syms,
2285 (dyn_count + 1) * sizeof (*syms));
2286 }
2287 else if (!relocatable && static_count == 0)
2288 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2289 else
2290 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2291
2292 /* Trim uninteresting symbols. Interesting symbols are section,
2293 function, and notype symbols. */
2294 for (i = 0, j = 0; i < symcount; ++i)
2295 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2296 | BSF_RELC | BSF_SRELC)) == 0)
2297 syms[j++] = syms[i];
2298 symcount = j;
2299
2300 synthetic_relocatable = relocatable;
2301 synthetic_opd = opd;
2302 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2303
2304 if (!relocatable && symcount > 1)
2305 {
2306 /* Trim duplicate syms, since we may have merged the normal
2307 and dynamic symbols. Actually, we only care about syms
2308 that have different values, so trim any with the same
2309 value. Don't consider ifunc and ifunc resolver symbols
2310 duplicates however, because GDB wants to know whether a
2311 text symbol is an ifunc resolver. */
2312 for (i = 1, j = 1; i < symcount; ++i)
2313 {
2314 const asymbol *s0 = syms[i - 1];
2315 const asymbol *s1 = syms[i];
2316
2317 if ((s0->value + s0->section->vma
2318 != s1->value + s1->section->vma)
2319 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2320 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2321 syms[j++] = syms[i];
2322 }
2323 symcount = j;
2324 }
2325
2326 i = 0;
2327 /* Note that here and in compare_symbols we can't compare opd and
2328 sym->section directly. With separate debug info files, the
2329 symbols will be extracted from the debug file while abfd passed
2330 to this function is the real binary. */
2331 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2332 && strcmp (syms[i]->section->name, ".opd") == 0)
2333 ++i;
2334 codesecsym = i;
2335
2336 for (; i < symcount; ++i)
2337 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2338 | SEC_THREAD_LOCAL))
2339 != (SEC_CODE | SEC_ALLOC))
2340 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2341 break;
2342 codesecsymend = i;
2343
2344 for (; i < symcount; ++i)
2345 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2346 break;
2347 secsymend = i;
2348
2349 for (; i < symcount; ++i)
2350 if (strcmp (syms[i]->section->name, ".opd") != 0)
2351 break;
2352 opdsymend = i;
2353
2354 for (; i < symcount; ++i)
2355 if (((syms[i]->section->flags
2356 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2357 != (SEC_CODE | SEC_ALLOC))
2358 break;
2359 symcount = i;
2360 }
2361 count = 0;
2362
2363 if (relocatable)
2364 {
2365 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2366 arelent *r;
2367 size_t size;
2368 size_t relcount;
2369
2370 if (opdsymend == secsymend)
2371 goto done;
2372
2373 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2374 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2375 if (relcount == 0)
2376 goto done;
2377
2378 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2379 {
2380 count = -1;
2381 goto done;
2382 }
2383
2384 size = 0;
2385 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2386 {
2387 asymbol *sym;
2388
2389 while (r < opd->relocation + relcount
2390 && r->address < syms[i]->value + opd->vma)
2391 ++r;
2392
2393 if (r == opd->relocation + relcount)
2394 break;
2395
2396 if (r->address != syms[i]->value + opd->vma)
2397 continue;
2398
2399 if (r->howto->type != R_PPC64_ADDR64)
2400 continue;
2401
2402 sym = *r->sym_ptr_ptr;
2403 if (!sym_exists_at (syms, opdsymend, symcount,
2404 sym->section->id, sym->value + r->addend))
2405 {
2406 ++count;
2407 size += sizeof (asymbol);
2408 size += strlen (syms[i]->name) + 2;
2409 }
2410 }
2411
2412 if (size == 0)
2413 goto done;
2414 s = *ret = bfd_malloc (size);
2415 if (s == NULL)
2416 {
2417 count = -1;
2418 goto done;
2419 }
2420
2421 names = (char *) (s + count);
2422
2423 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2424 {
2425 asymbol *sym;
2426
2427 while (r < opd->relocation + relcount
2428 && r->address < syms[i]->value + opd->vma)
2429 ++r;
2430
2431 if (r == opd->relocation + relcount)
2432 break;
2433
2434 if (r->address != syms[i]->value + opd->vma)
2435 continue;
2436
2437 if (r->howto->type != R_PPC64_ADDR64)
2438 continue;
2439
2440 sym = *r->sym_ptr_ptr;
2441 if (!sym_exists_at (syms, opdsymend, symcount,
2442 sym->section->id, sym->value + r->addend))
2443 {
2444 size_t len;
2445
2446 *s = *syms[i];
2447 s->flags |= BSF_SYNTHETIC;
2448 s->section = sym->section;
2449 s->value = sym->value + r->addend;
2450 s->name = names;
2451 *names++ = '.';
2452 len = strlen (syms[i]->name);
2453 memcpy (names, syms[i]->name, len + 1);
2454 names += len + 1;
2455 /* Have udata.p point back to the original symbol this
2456 synthetic symbol was derived from. */
2457 s->udata.p = syms[i];
2458 s++;
2459 }
2460 }
2461 }
2462 else
2463 {
2464 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2465 bfd_byte *contents = NULL;
2466 size_t size;
2467 size_t plt_count = 0;
2468 bfd_vma glink_vma = 0, resolv_vma = 0;
2469 asection *dynamic, *glink = NULL, *relplt = NULL;
2470 arelent *p;
2471
2472 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2473 {
2474 free_contents_and_exit_err:
2475 count = -1;
2476 free_contents_and_exit:
2477 free (contents);
2478 goto done;
2479 }
2480
2481 size = 0;
2482 for (i = secsymend; i < opdsymend; ++i)
2483 {
2484 bfd_vma ent;
2485
2486 /* Ignore bogus symbols. */
2487 if (syms[i]->value > opd->size - 8)
2488 continue;
2489
2490 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2491 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2492 {
2493 ++count;
2494 size += sizeof (asymbol);
2495 size += strlen (syms[i]->name) + 2;
2496 }
2497 }
2498
2499 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2500 if (dyn_count != 0
2501 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2502 {
2503 bfd_byte *dynbuf, *extdyn, *extdynend;
2504 size_t extdynsize;
2505 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2506
2507 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2508 goto free_contents_and_exit_err;
2509
2510 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2511 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2512
2513 extdyn = dynbuf;
2514 extdynend = extdyn + dynamic->size;
2515 for (; extdyn < extdynend; extdyn += extdynsize)
2516 {
2517 Elf_Internal_Dyn dyn;
2518 (*swap_dyn_in) (abfd, extdyn, &dyn);
2519
2520 if (dyn.d_tag == DT_NULL)
2521 break;
2522
2523 if (dyn.d_tag == DT_PPC64_GLINK)
2524 {
2525 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2526 See comment in ppc64_elf_finish_dynamic_sections. */
2527 glink_vma = dyn.d_un.d_val + 8 * 4;
2528 /* The .glink section usually does not survive the final
2529 link; search for the section (usually .text) where the
2530 glink stubs now reside. */
2531 glink = bfd_sections_find_if (abfd, section_covers_vma,
2532 &glink_vma);
2533 break;
2534 }
2535 }
2536
2537 free (dynbuf);
2538 }
2539
2540 if (glink != NULL)
2541 {
2542 /* Determine __glink trampoline by reading the relative branch
2543 from the first glink stub. */
2544 bfd_byte buf[4];
2545 unsigned int off = 0;
2546
2547 while (bfd_get_section_contents (abfd, glink, buf,
2548 glink_vma + off - glink->vma, 4))
2549 {
2550 unsigned int insn = bfd_get_32 (abfd, buf);
2551 insn ^= B_DOT;
2552 if ((insn & ~0x3fffffc) == 0)
2553 {
2554 resolv_vma
2555 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2556 break;
2557 }
2558 off += 4;
2559 if (off > 4)
2560 break;
2561 }
2562
2563 if (resolv_vma)
2564 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2565
2566 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2567 if (relplt != NULL)
2568 {
2569 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2570 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2571 goto free_contents_and_exit_err;
2572
2573 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2574 size += plt_count * sizeof (asymbol);
2575
2576 p = relplt->relocation;
2577 for (i = 0; i < plt_count; i++, p++)
2578 {
2579 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2580 if (p->addend != 0)
2581 size += sizeof ("+0x") - 1 + 16;
2582 }
2583 }
2584 }
2585
2586 if (size == 0)
2587 goto free_contents_and_exit;
2588 s = *ret = bfd_malloc (size);
2589 if (s == NULL)
2590 goto free_contents_and_exit_err;
2591
2592 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2593
2594 for (i = secsymend; i < opdsymend; ++i)
2595 {
2596 bfd_vma ent;
2597
2598 if (syms[i]->value > opd->size - 8)
2599 continue;
2600
2601 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2602 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2603 {
2604 size_t lo, hi;
2605 size_t len;
2606 asection *sec = abfd->sections;
2607
2608 *s = *syms[i];
2609 lo = codesecsym;
2610 hi = codesecsymend;
2611 while (lo < hi)
2612 {
2613 size_t mid = (lo + hi) >> 1;
2614 if (syms[mid]->section->vma < ent)
2615 lo = mid + 1;
2616 else if (syms[mid]->section->vma > ent)
2617 hi = mid;
2618 else
2619 {
2620 sec = syms[mid]->section;
2621 break;
2622 }
2623 }
2624
2625 if (lo >= hi && lo > codesecsym)
2626 sec = syms[lo - 1]->section;
2627
2628 for (; sec != NULL; sec = sec->next)
2629 {
2630 if (sec->vma > ent)
2631 break;
2632 /* SEC_LOAD may not be set if SEC is from a separate debug
2633 info file. */
2634 if ((sec->flags & SEC_ALLOC) == 0)
2635 break;
2636 if ((sec->flags & SEC_CODE) != 0)
2637 s->section = sec;
2638 }
2639 s->flags |= BSF_SYNTHETIC;
2640 s->value = ent - s->section->vma;
2641 s->name = names;
2642 *names++ = '.';
2643 len = strlen (syms[i]->name);
2644 memcpy (names, syms[i]->name, len + 1);
2645 names += len + 1;
2646 /* Have udata.p point back to the original symbol this
2647 synthetic symbol was derived from. */
2648 s->udata.p = syms[i];
2649 s++;
2650 }
2651 }
2652 free (contents);
2653
2654 if (glink != NULL && relplt != NULL)
2655 {
2656 if (resolv_vma)
2657 {
2658 /* Add a symbol for the main glink trampoline. */
2659 memset (s, 0, sizeof *s);
2660 s->the_bfd = abfd;
2661 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2662 s->section = glink;
2663 s->value = resolv_vma - glink->vma;
2664 s->name = names;
2665 memcpy (names, "__glink_PLTresolve",
2666 sizeof ("__glink_PLTresolve"));
2667 names += sizeof ("__glink_PLTresolve");
2668 s++;
2669 count++;
2670 }
2671
2672 /* FIXME: It would be very much nicer to put sym@plt on the
2673 stub rather than on the glink branch table entry. The
2674 objdump disassembler would then use a sensible symbol
2675 name on plt calls. The difficulty in doing so is
2676 a) finding the stubs, and,
2677 b) matching stubs against plt entries, and,
2678 c) there can be multiple stubs for a given plt entry.
2679
2680 Solving (a) could be done by code scanning, but older
2681 ppc64 binaries used different stubs to current code.
2682 (b) is the tricky one since you need to known the toc
2683 pointer for at least one function that uses a pic stub to
2684 be able to calculate the plt address referenced.
2685 (c) means gdb would need to set multiple breakpoints (or
2686 find the glink branch itself) when setting breakpoints
2687 for pending shared library loads. */
2688 p = relplt->relocation;
2689 for (i = 0; i < plt_count; i++, p++)
2690 {
2691 size_t len;
2692
2693 *s = **p->sym_ptr_ptr;
2694 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2695 we are defining a symbol, ensure one of them is set. */
2696 if ((s->flags & BSF_LOCAL) == 0)
2697 s->flags |= BSF_GLOBAL;
2698 s->flags |= BSF_SYNTHETIC;
2699 s->section = glink;
2700 s->value = glink_vma - glink->vma;
2701 s->name = names;
2702 s->udata.p = NULL;
2703 len = strlen ((*p->sym_ptr_ptr)->name);
2704 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2705 names += len;
2706 if (p->addend != 0)
2707 {
2708 memcpy (names, "+0x", sizeof ("+0x") - 1);
2709 names += sizeof ("+0x") - 1;
2710 bfd_sprintf_vma (abfd, names, p->addend);
2711 names += strlen (names);
2712 }
2713 memcpy (names, "@plt", sizeof ("@plt"));
2714 names += sizeof ("@plt");
2715 s++;
2716 if (abi < 2)
2717 {
2718 glink_vma += 8;
2719 if (i >= 0x8000)
2720 glink_vma += 4;
2721 }
2722 else
2723 glink_vma += 4;
2724 }
2725 count += plt_count;
2726 }
2727 }
2728
2729 done:
2730 free (syms);
2731 return count;
2732 }
2733 \f
2734 /* The following functions are specific to the ELF linker, while
2735 functions above are used generally. Those named ppc64_elf_* are
2736 called by the main ELF linker code. They appear in this file more
2737 or less in the order in which they are called. eg.
2738 ppc64_elf_check_relocs is called early in the link process,
2739 ppc64_elf_finish_dynamic_sections is one of the last functions
2740 called.
2741
2742 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2743 functions have both a function code symbol and a function descriptor
2744 symbol. A call to foo in a relocatable object file looks like:
2745
2746 . .text
2747 . x:
2748 . bl .foo
2749 . nop
2750
2751 The function definition in another object file might be:
2752
2753 . .section .opd
2754 . foo: .quad .foo
2755 . .quad .TOC.@tocbase
2756 . .quad 0
2757 .
2758 . .text
2759 . .foo: blr
2760
2761 When the linker resolves the call during a static link, the branch
2762 unsurprisingly just goes to .foo and the .opd information is unused.
2763 If the function definition is in a shared library, things are a little
2764 different: The call goes via a plt call stub, the opd information gets
2765 copied to the plt, and the linker patches the nop.
2766
2767 . x:
2768 . bl .foo_stub
2769 . ld 2,40(1)
2770 .
2771 .
2772 . .foo_stub:
2773 . std 2,40(1) # in practice, the call stub
2774 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2775 . addi 11,11,Lfoo@toc@l # this is the general idea
2776 . ld 12,0(11)
2777 . ld 2,8(11)
2778 . mtctr 12
2779 . ld 11,16(11)
2780 . bctr
2781 .
2782 . .section .plt
2783 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2784
2785 The "reloc ()" notation is supposed to indicate that the linker emits
2786 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2787 copying.
2788
2789 What are the difficulties here? Well, firstly, the relocations
2790 examined by the linker in check_relocs are against the function code
2791 sym .foo, while the dynamic relocation in the plt is emitted against
2792 the function descriptor symbol, foo. Somewhere along the line, we need
2793 to carefully copy dynamic link information from one symbol to the other.
2794 Secondly, the generic part of the elf linker will make .foo a dynamic
2795 symbol as is normal for most other backends. We need foo dynamic
2796 instead, at least for an application final link. However, when
2797 creating a shared library containing foo, we need to have both symbols
2798 dynamic so that references to .foo are satisfied during the early
2799 stages of linking. Otherwise the linker might decide to pull in a
2800 definition from some other object, eg. a static library.
2801
2802 Update: As of August 2004, we support a new convention. Function
2803 calls may use the function descriptor symbol, ie. "bl foo". This
2804 behaves exactly as "bl .foo". */
2805
2806 /* Of those relocs that might be copied as dynamic relocs, this
2807 function selects those that must be copied when linking a shared
2808 library or PIE, even when the symbol is local. */
2809
2810 static int
2811 must_be_dyn_reloc (struct bfd_link_info *info,
2812 enum elf_ppc64_reloc_type r_type)
2813 {
2814 switch (r_type)
2815 {
2816 default:
2817 /* Only relative relocs can be resolved when the object load
2818 address isn't fixed. DTPREL64 is excluded because the
2819 dynamic linker needs to differentiate global dynamic from
2820 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2821 return 1;
2822
2823 case R_PPC64_REL32:
2824 case R_PPC64_REL64:
2825 case R_PPC64_REL30:
2826 case R_PPC64_TOC16:
2827 case R_PPC64_TOC16_DS:
2828 case R_PPC64_TOC16_LO:
2829 case R_PPC64_TOC16_HI:
2830 case R_PPC64_TOC16_HA:
2831 case R_PPC64_TOC16_LO_DS:
2832 return 0;
2833
2834 case R_PPC64_TPREL16:
2835 case R_PPC64_TPREL16_LO:
2836 case R_PPC64_TPREL16_HI:
2837 case R_PPC64_TPREL16_HA:
2838 case R_PPC64_TPREL16_DS:
2839 case R_PPC64_TPREL16_LO_DS:
2840 case R_PPC64_TPREL16_HIGH:
2841 case R_PPC64_TPREL16_HIGHA:
2842 case R_PPC64_TPREL16_HIGHER:
2843 case R_PPC64_TPREL16_HIGHERA:
2844 case R_PPC64_TPREL16_HIGHEST:
2845 case R_PPC64_TPREL16_HIGHESTA:
2846 case R_PPC64_TPREL64:
2847 case R_PPC64_TPREL34:
2848 /* These relocations are relative but in a shared library the
2849 linker doesn't know the thread pointer base. */
2850 return bfd_link_dll (info);
2851 }
2852 }
2853
2854 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2855 copying dynamic variables from a shared lib into an app's .dynbss
2856 section, and instead use a dynamic relocation to point into the
2857 shared lib. With code that gcc generates it is vital that this be
2858 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2859 actually the address of a function descriptor which resides in the
2860 .opd section. gcc uses the descriptor directly rather than going
2861 via the GOT as some other ABIs do, which means that initialized
2862 function pointers reference the descriptor. Thus, a function
2863 pointer initialized to the address of a function in a shared
2864 library will either require a .dynbss copy and a copy reloc, or a
2865 dynamic reloc. Using a .dynbss copy redefines the function
2866 descriptor symbol to point to the copy. This presents a problem as
2867 a PLT entry for that function is also initialized from the function
2868 descriptor symbol and the copy may not be initialized first. */
2869 #define ELIMINATE_COPY_RELOCS 1
2870
2871 /* Section name for stubs is the associated section name plus this
2872 string. */
2873 #define STUB_SUFFIX ".stub"
2874
2875 /* Linker stubs.
2876 ppc_stub_long_branch:
2877 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2878 destination, but a 24 bit branch in a stub section will reach.
2879 . b dest
2880
2881 ppc_stub_plt_branch:
2882 Similar to the above, but a 24 bit branch in the stub section won't
2883 reach its destination.
2884 . addis %r12,%r2,xxx@toc@ha
2885 . ld %r12,xxx@toc@l(%r12)
2886 . mtctr %r12
2887 . bctr
2888
2889 ppc_stub_plt_call:
2890 Used to call a function in a shared library. If it so happens that
2891 the plt entry referenced crosses a 64k boundary, then an extra
2892 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2893 An r2save variant starts with "std %r2,40(%r1)".
2894 . addis %r11,%r2,xxx@toc@ha
2895 . ld %r12,xxx+0@toc@l(%r11)
2896 . mtctr %r12
2897 . ld %r2,xxx+8@toc@l(%r11)
2898 . ld %r11,xxx+16@toc@l(%r11)
2899 . bctr
2900
2901 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2902 code to adjust the value and save r2 to support multiple toc sections.
2903 A ppc_stub_long_branch with an r2 offset looks like:
2904 . std %r2,40(%r1)
2905 . addis %r2,%r2,off@ha
2906 . addi %r2,%r2,off@l
2907 . b dest
2908
2909 A ppc_stub_plt_branch with an r2 offset looks like:
2910 . std %r2,40(%r1)
2911 . addis %r12,%r2,xxx@toc@ha
2912 . ld %r12,xxx@toc@l(%r12)
2913 . addis %r2,%r2,off@ha
2914 . addi %r2,%r2,off@l
2915 . mtctr %r12
2916 . bctr
2917
2918 All of the above stubs are shown as their ELFv1 variants. ELFv2
2919 variants exist too, simpler for plt calls since a new toc pointer
2920 and static chain are not loaded by the stub. In addition, ELFv2
2921 has some more complex stubs to handle calls marked with NOTOC
2922 relocs from functions where r2 is not a valid toc pointer.
2923 ppc_stub_long_branch_p9notoc:
2924 . mflr %r12
2925 . bcl 20,31,1f
2926 . 1:
2927 . mflr %r11
2928 . mtlr %r12
2929 . addis %r12,%r11,dest-1b@ha
2930 . addi %r12,%r12,dest-1b@l
2931 . b dest
2932
2933 ppc_stub_plt_branch_p9notoc:
2934 . mflr %r12
2935 . bcl 20,31,1f
2936 . 1:
2937 . mflr %r11
2938 . mtlr %r12
2939 . lis %r12,xxx-1b@highest
2940 . ori %r12,%r12,xxx-1b@higher
2941 . sldi %r12,%r12,32
2942 . oris %r12,%r12,xxx-1b@high
2943 . ori %r12,%r12,xxx-1b@l
2944 . add %r12,%r11,%r12
2945 . mtctr %r12
2946 . bctr
2947
2948 ppc_stub_plt_call_p9notoc:
2949 . mflr %r12
2950 . bcl 20,31,1f
2951 . 1:
2952 . mflr %r11
2953 . mtlr %r12
2954 . lis %r12,xxx-1b@highest
2955 . ori %r12,%r12,xxx-1b@higher
2956 . sldi %r12,%r12,32
2957 . oris %r12,%r12,xxx-1b@high
2958 . ori %r12,%r12,xxx-1b@l
2959 . ldx %r12,%r11,%r12
2960 . mtctr %r12
2961 . bctr
2962
2963 There are also ELFv1 power10 variants of these stubs.
2964 ppc_stub_long_branch_notoc:
2965 . pla %r12,dest@pcrel
2966 . b dest
2967 ppc_stub_plt_branch_notoc:
2968 . lis %r11,(dest-1f)@highesta34
2969 . ori %r11,%r11,(dest-1f)@highera34
2970 . sldi %r11,%r11,34
2971 . 1: pla %r12,dest@pcrel
2972 . add %r12,%r11,%r12
2973 . mtctr %r12
2974 . bctr
2975 ppc_stub_plt_call_notoc:
2976 . lis %r11,(xxx-1f)@highesta34
2977 . ori %r11,%r11,(xxx-1f)@highera34
2978 . sldi %r11,%r11,34
2979 . 1: pla %r12,xxx@pcrel
2980 . ldx %r12,%r11,%r12
2981 . mtctr %r12
2982 . bctr
2983
2984 In cases where the high instructions would add zero, they are
2985 omitted and following instructions modified in some cases.
2986 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2987 to
2988 . pld %r12,xxx@pcrel
2989 . mtctr %r12
2990 . bctr
2991
2992 Stub variants may be merged. For example, if printf is called from
2993 code with the tocsave optimization (ie. r2 saved in function
2994 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2995 and from other code without the tocsave optimization requiring a
2996 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2997 type will be created. Calls with the tocsave optimization will
2998 enter this stub after the instruction saving r2. A similar
2999 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3000 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3001 to call an external function like printf. If other calls to printf
3002 require a ppc_stub_plt_call linkage stub then a single
3003 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3004 call. */
3005
3006 enum ppc_stub_main_type
3007 {
3008 ppc_stub_none,
3009 ppc_stub_long_branch,
3010 ppc_stub_plt_branch,
3011 ppc_stub_plt_call,
3012 ppc_stub_global_entry,
3013 ppc_stub_save_res
3014 };
3015
3016 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3017 these variations. */
3018
3019 enum ppc_stub_sub_type
3020 {
3021 ppc_stub_toc,
3022 ppc_stub_notoc,
3023 ppc_stub_p9notoc
3024 };
3025
3026 struct ppc_stub_type
3027 {
3028 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3029 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3030 unsigned int r2save : 1;
3031 };
3032
3033 /* Information on stub grouping. */
3034 struct map_stub
3035 {
3036 /* The stub section. */
3037 asection *stub_sec;
3038 /* This is the section to which stubs in the group will be attached. */
3039 asection *link_sec;
3040 /* Next group. */
3041 struct map_stub *next;
3042 /* Whether to emit a copy of register save/restore functions in this
3043 group. */
3044 int needs_save_res;
3045 /* Current offset within stubs after the insn restoring lr in a
3046 _notoc or _both stub using bcl for pc-relative addressing, or
3047 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3048 unsigned int lr_restore;
3049 /* Accumulated size of EH info emitted to describe return address
3050 if stubs modify lr. Does not include 17 byte FDE header. */
3051 unsigned int eh_size;
3052 /* Offset in glink_eh_frame to the start of EH info for this group. */
3053 unsigned int eh_base;
3054 };
3055
3056 struct ppc_stub_hash_entry
3057 {
3058 /* Base hash table entry structure. */
3059 struct bfd_hash_entry root;
3060
3061 struct ppc_stub_type type;
3062
3063 /* Group information. */
3064 struct map_stub *group;
3065
3066 /* Offset within stub_sec of the beginning of this stub. */
3067 bfd_vma stub_offset;
3068
3069 /* Given the symbol's value and its section we can determine its final
3070 value when building the stubs (so the stub knows where to jump. */
3071 bfd_vma target_value;
3072 asection *target_section;
3073
3074 /* The symbol table entry, if any, that this was derived from. */
3075 struct ppc_link_hash_entry *h;
3076 struct plt_entry *plt_ent;
3077
3078 /* Symbol type. */
3079 unsigned char symtype;
3080
3081 /* Symbol st_other. */
3082 unsigned char other;
3083 };
3084
3085 struct ppc_branch_hash_entry
3086 {
3087 /* Base hash table entry structure. */
3088 struct bfd_hash_entry root;
3089
3090 /* Offset within branch lookup table. */
3091 unsigned int offset;
3092
3093 /* Generation marker. */
3094 unsigned int iter;
3095 };
3096
3097 /* Used to track dynamic relocations. */
3098 struct ppc_dyn_relocs
3099 {
3100 struct ppc_dyn_relocs *next;
3101
3102 /* The input section of the reloc. */
3103 asection *sec;
3104
3105 /* Total number of relocs copied for the input section. */
3106 unsigned int count;
3107
3108 /* Number of pc-relative relocs copied for the input section. */
3109 unsigned int pc_count;
3110
3111 /* Number of relocs that might become R_PPC64_RELATIVE. */
3112 unsigned int rel_count;
3113 };
3114
3115 struct ppc_local_dyn_relocs
3116 {
3117 struct ppc_local_dyn_relocs *next;
3118
3119 /* The input section of the reloc. */
3120 asection *sec;
3121
3122 /* Total number of relocs copied for the input section. */
3123 unsigned int count;
3124
3125 /* Number of relocs that might become R_PPC64_RELATIVE. */
3126 unsigned int rel_count : 31;
3127
3128 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3129 unsigned int ifunc : 1;
3130 };
3131
3132 struct ppc_link_hash_entry
3133 {
3134 struct elf_link_hash_entry elf;
3135
3136 union
3137 {
3138 /* A pointer to the most recently used stub hash entry against this
3139 symbol. */
3140 struct ppc_stub_hash_entry *stub_cache;
3141
3142 /* A pointer to the next symbol starting with a '.' */
3143 struct ppc_link_hash_entry *next_dot_sym;
3144 } u;
3145
3146 /* Link between function code and descriptor symbols. */
3147 struct ppc_link_hash_entry *oh;
3148
3149 /* Flag function code and descriptor symbols. */
3150 unsigned int is_func:1;
3151 unsigned int is_func_descriptor:1;
3152 unsigned int fake:1;
3153
3154 /* Whether global opd/toc sym has been adjusted or not.
3155 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3156 should be set for all globals defined in any opd/toc section. */
3157 unsigned int adjust_done:1;
3158
3159 /* Set if this is an out-of-line register save/restore function,
3160 with non-standard calling convention. */
3161 unsigned int save_res:1;
3162
3163 /* Set if a duplicate symbol with non-zero localentry is detected,
3164 even when the duplicate symbol does not provide a definition. */
3165 unsigned int non_zero_localentry:1;
3166
3167 /* Contexts in which symbol is used in the GOT (or TOC).
3168 Bits are or'd into the mask as the corresponding relocs are
3169 encountered during check_relocs, with TLS_TLS being set when any
3170 of the other TLS bits are set. tls_optimize clears bits when
3171 optimizing to indicate the corresponding GOT entry type is not
3172 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3173 set TLS_GDIE when a GD reloc turns into an IE one.
3174 These flags are also kept for local symbols. */
3175 #define TLS_TLS 1 /* Any TLS reloc. */
3176 #define TLS_GD 2 /* GD reloc. */
3177 #define TLS_LD 4 /* LD reloc. */
3178 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3179 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3180 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3181 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3182 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3183 unsigned char tls_mask;
3184
3185 /* The above field is also used to mark function symbols. In which
3186 case TLS_TLS will be 0. */
3187 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3188 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3189 #define NON_GOT 256 /* local symbol plt, not stored. */
3190 };
3191
3192 static inline struct ppc_link_hash_entry *
3193 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3194 {
3195 return (struct ppc_link_hash_entry *) ent;
3196 }
3197
3198 static inline struct elf_link_hash_entry *
3199 elf_hash_entry (struct ppc_link_hash_entry *ent)
3200 {
3201 return (struct elf_link_hash_entry *) ent;
3202 }
3203
3204 /* ppc64 ELF linker hash table. */
3205
3206 struct ppc_link_hash_table
3207 {
3208 struct elf_link_hash_table elf;
3209
3210 /* The stub hash table. */
3211 struct bfd_hash_table stub_hash_table;
3212
3213 /* Another hash table for plt_branch stubs. */
3214 struct bfd_hash_table branch_hash_table;
3215
3216 /* Hash table for function prologue tocsave. */
3217 htab_t tocsave_htab;
3218
3219 /* Various options and other info passed from the linker. */
3220 struct ppc64_elf_params *params;
3221
3222 /* The size of sec_info below. */
3223 unsigned int sec_info_arr_size;
3224
3225 /* Per-section array of extra section info. Done this way rather
3226 than as part of ppc64_elf_section_data so we have the info for
3227 non-ppc64 sections. */
3228 struct
3229 {
3230 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3231 bfd_vma toc_off;
3232
3233 union
3234 {
3235 /* The section group that this section belongs to. */
3236 struct map_stub *group;
3237 /* A temp section list pointer. */
3238 asection *list;
3239 } u;
3240 } *sec_info;
3241
3242 /* Linked list of groups. */
3243 struct map_stub *group;
3244
3245 /* Temp used when calculating TOC pointers. */
3246 bfd_vma toc_curr;
3247 bfd *toc_bfd;
3248 asection *toc_first_sec;
3249
3250 /* Used when adding symbols. */
3251 struct ppc_link_hash_entry *dot_syms;
3252
3253 /* Shortcuts to get to dynamic linker sections. */
3254 asection *glink;
3255 asection *global_entry;
3256 asection *sfpr;
3257 asection *pltlocal;
3258 asection *relpltlocal;
3259 asection *brlt;
3260 asection *relbrlt;
3261 asection *glink_eh_frame;
3262
3263 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3264 struct ppc_link_hash_entry *tls_get_addr;
3265 struct ppc_link_hash_entry *tls_get_addr_fd;
3266 struct ppc_link_hash_entry *tga_desc;
3267 struct ppc_link_hash_entry *tga_desc_fd;
3268 struct map_stub *tga_group;
3269
3270 /* The size of reliplt used by got entry relocs. */
3271 bfd_size_type got_reli_size;
3272
3273 /* DT_RELR array of r_offset. */
3274 size_t relr_alloc;
3275 size_t relr_count;
3276 bfd_vma *relr_addr;
3277
3278 /* Statistics. */
3279 unsigned long stub_count[ppc_stub_save_res];
3280
3281 /* Number of stubs against global syms. */
3282 unsigned long stub_globals;
3283
3284 /* Set if we're linking code with function descriptors. */
3285 unsigned int opd_abi:1;
3286
3287 /* Support for multiple toc sections. */
3288 unsigned int do_multi_toc:1;
3289 unsigned int multi_toc_needed:1;
3290 unsigned int second_toc_pass:1;
3291 unsigned int do_toc_opt:1;
3292
3293 /* Set if tls optimization is enabled. */
3294 unsigned int do_tls_opt:1;
3295
3296 /* Set if inline plt calls should be converted to direct calls. */
3297 unsigned int can_convert_all_inline_plt:1;
3298
3299 /* Set on error. */
3300 unsigned int stub_error:1;
3301
3302 /* Whether func_desc_adjust needs to be run over symbols. */
3303 unsigned int need_func_desc_adj:1;
3304
3305 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3306 unsigned int has_plt_localentry0:1;
3307
3308 /* Whether calls are made via the PLT from NOTOC functions. */
3309 unsigned int notoc_plt:1;
3310
3311 /* Whether any code linked seems to be Power10. */
3312 unsigned int has_power10_relocs:1;
3313
3314 /* Incremented every time we size stubs. */
3315 unsigned int stub_iteration;
3316 };
3317
3318 /* Rename some of the generic section flags to better document how they
3319 are used here. */
3320
3321 /* Nonzero if this section has TLS related relocations. */
3322 #define has_tls_reloc sec_flg0
3323
3324 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3325 relocations. */
3326 #define nomark_tls_get_addr sec_flg1
3327
3328 /* Nonzero if this section has any toc or got relocs. */
3329 #define has_toc_reloc sec_flg2
3330
3331 /* Nonzero if this section has a call to another section that uses
3332 the toc or got. */
3333 #define makes_toc_func_call sec_flg3
3334
3335 /* Recursion protection when determining above flag. */
3336 #define call_check_in_progress sec_flg4
3337 #define call_check_done sec_flg5
3338
3339 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3340
3341 #define ppc_hash_table(p) \
3342 ((is_elf_hash_table ((p)->hash) \
3343 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3344 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3345
3346 #define ppc_stub_hash_lookup(table, string, create, copy) \
3347 ((struct ppc_stub_hash_entry *) \
3348 bfd_hash_lookup ((table), (string), (create), (copy)))
3349
3350 #define ppc_branch_hash_lookup(table, string, create, copy) \
3351 ((struct ppc_branch_hash_entry *) \
3352 bfd_hash_lookup ((table), (string), (create), (copy)))
3353
3354 /* Create an entry in the stub hash table. */
3355
3356 static struct bfd_hash_entry *
3357 stub_hash_newfunc (struct bfd_hash_entry *entry,
3358 struct bfd_hash_table *table,
3359 const char *string)
3360 {
3361 /* Allocate the structure if it has not already been allocated by a
3362 subclass. */
3363 if (entry == NULL)
3364 {
3365 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3366 if (entry == NULL)
3367 return entry;
3368 }
3369
3370 /* Call the allocation method of the superclass. */
3371 entry = bfd_hash_newfunc (entry, table, string);
3372 if (entry != NULL)
3373 {
3374 struct ppc_stub_hash_entry *eh;
3375
3376 /* Initialize the local fields. */
3377 eh = (struct ppc_stub_hash_entry *) entry;
3378 eh->type.main = ppc_stub_none;
3379 eh->type.sub = ppc_stub_toc;
3380 eh->type.r2save = 0;
3381 eh->group = NULL;
3382 eh->stub_offset = 0;
3383 eh->target_value = 0;
3384 eh->target_section = NULL;
3385 eh->h = NULL;
3386 eh->plt_ent = NULL;
3387 eh->other = 0;
3388 }
3389
3390 return entry;
3391 }
3392
3393 /* Create an entry in the branch hash table. */
3394
3395 static struct bfd_hash_entry *
3396 branch_hash_newfunc (struct bfd_hash_entry *entry,
3397 struct bfd_hash_table *table,
3398 const char *string)
3399 {
3400 /* Allocate the structure if it has not already been allocated by a
3401 subclass. */
3402 if (entry == NULL)
3403 {
3404 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3405 if (entry == NULL)
3406 return entry;
3407 }
3408
3409 /* Call the allocation method of the superclass. */
3410 entry = bfd_hash_newfunc (entry, table, string);
3411 if (entry != NULL)
3412 {
3413 struct ppc_branch_hash_entry *eh;
3414
3415 /* Initialize the local fields. */
3416 eh = (struct ppc_branch_hash_entry *) entry;
3417 eh->offset = 0;
3418 eh->iter = 0;
3419 }
3420
3421 return entry;
3422 }
3423
3424 /* Create an entry in a ppc64 ELF linker hash table. */
3425
3426 static struct bfd_hash_entry *
3427 link_hash_newfunc (struct bfd_hash_entry *entry,
3428 struct bfd_hash_table *table,
3429 const char *string)
3430 {
3431 /* Allocate the structure if it has not already been allocated by a
3432 subclass. */
3433 if (entry == NULL)
3434 {
3435 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3436 if (entry == NULL)
3437 return entry;
3438 }
3439
3440 /* Call the allocation method of the superclass. */
3441 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3442 if (entry != NULL)
3443 {
3444 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3445
3446 memset (&eh->u.stub_cache, 0,
3447 (sizeof (struct ppc_link_hash_entry)
3448 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3449
3450 /* When making function calls, old ABI code references function entry
3451 points (dot symbols), while new ABI code references the function
3452 descriptor symbol. We need to make any combination of reference and
3453 definition work together, without breaking archive linking.
3454
3455 For a defined function "foo" and an undefined call to "bar":
3456 An old object defines "foo" and ".foo", references ".bar" (possibly
3457 "bar" too).
3458 A new object defines "foo" and references "bar".
3459
3460 A new object thus has no problem with its undefined symbols being
3461 satisfied by definitions in an old object. On the other hand, the
3462 old object won't have ".bar" satisfied by a new object.
3463
3464 Keep a list of newly added dot-symbols. */
3465
3466 if (string[0] == '.')
3467 {
3468 struct ppc_link_hash_table *htab;
3469
3470 htab = (struct ppc_link_hash_table *) table;
3471 eh->u.next_dot_sym = htab->dot_syms;
3472 htab->dot_syms = eh;
3473 }
3474 }
3475
3476 return entry;
3477 }
3478
3479 struct tocsave_entry
3480 {
3481 asection *sec;
3482 bfd_vma offset;
3483 };
3484
3485 static hashval_t
3486 tocsave_htab_hash (const void *p)
3487 {
3488 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3489 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3490 }
3491
3492 static int
3493 tocsave_htab_eq (const void *p1, const void *p2)
3494 {
3495 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3496 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3497 return e1->sec == e2->sec && e1->offset == e2->offset;
3498 }
3499
3500 /* Destroy a ppc64 ELF linker hash table. */
3501
3502 static void
3503 ppc64_elf_link_hash_table_free (bfd *obfd)
3504 {
3505 struct ppc_link_hash_table *htab;
3506
3507 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3508 if (htab->tocsave_htab)
3509 htab_delete (htab->tocsave_htab);
3510 bfd_hash_table_free (&htab->branch_hash_table);
3511 bfd_hash_table_free (&htab->stub_hash_table);
3512 _bfd_elf_link_hash_table_free (obfd);
3513 }
3514
3515 /* Create a ppc64 ELF linker hash table. */
3516
3517 static struct bfd_link_hash_table *
3518 ppc64_elf_link_hash_table_create (bfd *abfd)
3519 {
3520 struct ppc_link_hash_table *htab;
3521 size_t amt = sizeof (struct ppc_link_hash_table);
3522
3523 htab = bfd_zmalloc (amt);
3524 if (htab == NULL)
3525 return NULL;
3526
3527 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3528 sizeof (struct ppc_link_hash_entry),
3529 PPC64_ELF_DATA))
3530 {
3531 free (htab);
3532 return NULL;
3533 }
3534
3535 /* Init the stub hash table too. */
3536 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3537 sizeof (struct ppc_stub_hash_entry)))
3538 {
3539 _bfd_elf_link_hash_table_free (abfd);
3540 return NULL;
3541 }
3542
3543 /* And the branch hash table. */
3544 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3545 sizeof (struct ppc_branch_hash_entry)))
3546 {
3547 bfd_hash_table_free (&htab->stub_hash_table);
3548 _bfd_elf_link_hash_table_free (abfd);
3549 return NULL;
3550 }
3551
3552 htab->tocsave_htab = htab_try_create (1024,
3553 tocsave_htab_hash,
3554 tocsave_htab_eq,
3555 NULL);
3556 if (htab->tocsave_htab == NULL)
3557 {
3558 ppc64_elf_link_hash_table_free (abfd);
3559 return NULL;
3560 }
3561 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3562
3563 /* Initializing two fields of the union is just cosmetic. We really
3564 only care about glist, but when compiled on a 32-bit host the
3565 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3566 debugger inspection of these fields look nicer. */
3567 htab->elf.init_got_refcount.refcount = 0;
3568 htab->elf.init_got_refcount.glist = NULL;
3569 htab->elf.init_plt_refcount.refcount = 0;
3570 htab->elf.init_plt_refcount.glist = NULL;
3571 htab->elf.init_got_offset.offset = 0;
3572 htab->elf.init_got_offset.glist = NULL;
3573 htab->elf.init_plt_offset.offset = 0;
3574 htab->elf.init_plt_offset.glist = NULL;
3575
3576 return &htab->elf.root;
3577 }
3578
3579 /* Create sections for linker generated code. */
3580
3581 static bool
3582 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3583 {
3584 struct ppc_link_hash_table *htab;
3585 flagword flags;
3586
3587 htab = ppc_hash_table (info);
3588
3589 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3590 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3591 if (htab->params->save_restore_funcs)
3592 {
3593 /* Create .sfpr for code to save and restore fp regs. */
3594 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3595 flags);
3596 if (htab->sfpr == NULL
3597 || !bfd_set_section_alignment (htab->sfpr, 2))
3598 return false;
3599 }
3600
3601 if (bfd_link_relocatable (info))
3602 return true;
3603
3604 /* Create .glink for lazy dynamic linking support. */
3605 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3606 flags);
3607 if (htab->glink == NULL
3608 || !bfd_set_section_alignment (htab->glink, 3))
3609 return false;
3610
3611 /* The part of .glink used by global entry stubs, separate so that
3612 it can be aligned appropriately without affecting htab->glink. */
3613 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3614 flags);
3615 if (htab->global_entry == NULL
3616 || !bfd_set_section_alignment (htab->global_entry, 2))
3617 return false;
3618
3619 if (!info->no_ld_generated_unwind_info)
3620 {
3621 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3622 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3623 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3624 ".eh_frame",
3625 flags);
3626 if (htab->glink_eh_frame == NULL
3627 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3628 return false;
3629 }
3630
3631 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3632 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3633 if (htab->elf.iplt == NULL
3634 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3635 return false;
3636
3637 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3638 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3639 htab->elf.irelplt
3640 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3641 if (htab->elf.irelplt == NULL
3642 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3643 return false;
3644
3645 /* Create branch lookup table for plt_branch stubs. */
3646 flags = (SEC_ALLOC | SEC_LOAD
3647 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3648 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3649 flags);
3650 if (htab->brlt == NULL
3651 || !bfd_set_section_alignment (htab->brlt, 3))
3652 return false;
3653
3654 /* Local plt entries, put in .branch_lt but a separate section for
3655 convenience. */
3656 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3657 flags);
3658 if (htab->pltlocal == NULL
3659 || !bfd_set_section_alignment (htab->pltlocal, 3))
3660 return false;
3661
3662 if (!bfd_link_pic (info))
3663 return true;
3664
3665 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3666 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3667 htab->relbrlt
3668 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3669 if (htab->relbrlt == NULL
3670 || !bfd_set_section_alignment (htab->relbrlt, 3))
3671 return false;
3672
3673 htab->relpltlocal
3674 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3675 if (htab->relpltlocal == NULL
3676 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3677 return false;
3678
3679 return true;
3680 }
3681
3682 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3683
3684 bool
3685 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3686 struct ppc64_elf_params *params)
3687 {
3688 struct ppc_link_hash_table *htab;
3689
3690 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3691
3692 /* Always hook our dynamic sections into the first bfd, which is the
3693 linker created stub bfd. This ensures that the GOT header is at
3694 the start of the output TOC section. */
3695 htab = ppc_hash_table (info);
3696 htab->elf.dynobj = params->stub_bfd;
3697 htab->params = params;
3698
3699 return create_linkage_sections (htab->elf.dynobj, info);
3700 }
3701
3702 /* Build a name for an entry in the stub hash table. */
3703
3704 static char *
3705 ppc_stub_name (const asection *input_section,
3706 const asection *sym_sec,
3707 const struct ppc_link_hash_entry *h,
3708 const Elf_Internal_Rela *rel)
3709 {
3710 char *stub_name;
3711 ssize_t len;
3712
3713 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3714 offsets from a sym as a branch target? In fact, we could
3715 probably assume the addend is always zero. */
3716 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3717
3718 if (h)
3719 {
3720 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3721 stub_name = bfd_malloc (len);
3722 if (stub_name == NULL)
3723 return stub_name;
3724
3725 len = sprintf (stub_name, "%08x.%s+%x",
3726 input_section->id & 0xffffffff,
3727 h->elf.root.root.string,
3728 (int) rel->r_addend & 0xffffffff);
3729 }
3730 else
3731 {
3732 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3733 stub_name = bfd_malloc (len);
3734 if (stub_name == NULL)
3735 return stub_name;
3736
3737 len = sprintf (stub_name, "%08x.%x:%x+%x",
3738 input_section->id & 0xffffffff,
3739 sym_sec->id & 0xffffffff,
3740 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3741 (int) rel->r_addend & 0xffffffff);
3742 }
3743 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3744 stub_name[len - 2] = 0;
3745 return stub_name;
3746 }
3747
3748 /* If mixing power10 with non-power10 code and --power10-stubs is not
3749 specified (or is auto) then there may be multiple stub types for any
3750 given symbol. Up to three classes of stubs are stored in separate
3751 stub_hash_table entries having the same key string. The entries
3752 will always be adjacent on entry->root.next chain, even if hash
3753 table resizing occurs. This function selects the correct entry to
3754 use. */
3755
3756 static struct ppc_stub_hash_entry *
3757 select_alt_stub (struct ppc_stub_hash_entry *entry,
3758 enum elf_ppc64_reloc_type r_type)
3759 {
3760 enum ppc_stub_sub_type subt;
3761
3762 switch (r_type)
3763 {
3764 case R_PPC64_REL24_NOTOC:
3765 subt = ppc_stub_notoc;
3766 break;
3767 case R_PPC64_REL24_P9NOTOC:
3768 subt = ppc_stub_p9notoc;
3769 break;
3770 default:
3771 subt = ppc_stub_toc;
3772 break;
3773 }
3774
3775 while (entry != NULL && entry->type.sub != subt)
3776 {
3777 const char *stub_name = entry->root.string;
3778
3779 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3780 if (entry != NULL
3781 && entry->root.string != stub_name)
3782 entry = NULL;
3783 }
3784
3785 return entry;
3786 }
3787
3788 /* Look up an entry in the stub hash. Stub entries are cached because
3789 creating the stub name takes a bit of time. */
3790
3791 static struct ppc_stub_hash_entry *
3792 ppc_get_stub_entry (const asection *input_section,
3793 const asection *sym_sec,
3794 struct ppc_link_hash_entry *h,
3795 const Elf_Internal_Rela *rel,
3796 struct ppc_link_hash_table *htab)
3797 {
3798 struct ppc_stub_hash_entry *stub_entry;
3799 struct map_stub *group;
3800
3801 /* If this input section is part of a group of sections sharing one
3802 stub section, then use the id of the first section in the group.
3803 Stub names need to include a section id, as there may well be
3804 more than one stub used to reach say, printf, and we need to
3805 distinguish between them. */
3806 group = htab->sec_info[input_section->id].u.group;
3807 if (group == NULL)
3808 return NULL;
3809
3810 if (h != NULL && h->u.stub_cache != NULL
3811 && h->u.stub_cache->h == h
3812 && h->u.stub_cache->group == group)
3813 {
3814 stub_entry = h->u.stub_cache;
3815 }
3816 else
3817 {
3818 char *stub_name;
3819
3820 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3821 if (stub_name == NULL)
3822 return NULL;
3823
3824 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3825 stub_name, false, false);
3826 if (h != NULL)
3827 h->u.stub_cache = stub_entry;
3828
3829 free (stub_name);
3830 }
3831
3832 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3833 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3834
3835 return stub_entry;
3836 }
3837
3838 /* Add a new stub entry to the stub hash. Not all fields of the new
3839 stub entry are initialised. */
3840
3841 static struct ppc_stub_hash_entry *
3842 ppc_add_stub (const char *stub_name,
3843 asection *section,
3844 struct bfd_link_info *info)
3845 {
3846 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3847 struct map_stub *group;
3848 asection *link_sec;
3849 asection *stub_sec;
3850 struct ppc_stub_hash_entry *stub_entry;
3851
3852 group = htab->sec_info[section->id].u.group;
3853 link_sec = group->link_sec;
3854 stub_sec = group->stub_sec;
3855 if (stub_sec == NULL)
3856 {
3857 size_t namelen;
3858 bfd_size_type len;
3859 char *s_name;
3860
3861 namelen = strlen (link_sec->name);
3862 len = namelen + sizeof (STUB_SUFFIX);
3863 s_name = bfd_alloc (htab->params->stub_bfd, len);
3864 if (s_name == NULL)
3865 return NULL;
3866
3867 memcpy (s_name, link_sec->name, namelen);
3868 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3869 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3870 if (stub_sec == NULL)
3871 return NULL;
3872 group->stub_sec = stub_sec;
3873 }
3874
3875 /* Enter this entry into the linker stub hash table. */
3876 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3877 true, false);
3878 if (stub_entry == NULL)
3879 {
3880 /* xgettext:c-format */
3881 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3882 section->owner, stub_name);
3883 return NULL;
3884 }
3885
3886 stub_entry->group = group;
3887 stub_entry->stub_offset = 0;
3888 return stub_entry;
3889 }
3890
3891 /* A stub has already been created, but it may not be the required
3892 type. We shouldn't be transitioning from plt_call to long_branch
3893 stubs or vice versa, but we might be upgrading from plt_call to
3894 plt_call with r2save for example. */
3895
3896 static bool
3897 ppc_merge_stub (struct ppc_link_hash_table *htab,
3898 struct ppc_stub_hash_entry *stub_entry,
3899 struct ppc_stub_type stub_type,
3900 enum elf_ppc64_reloc_type r_type)
3901 {
3902 struct ppc_stub_type old_type = stub_entry->type;
3903
3904 if (old_type.main == ppc_stub_save_res)
3905 return true;
3906
3907 if (htab->params->power10_stubs == -1)
3908 {
3909 /* For --power10-stubs=auto, don't merge _notoc and other
3910 varieties of stubs. */
3911 struct ppc_stub_hash_entry *alt_stub;
3912
3913 alt_stub = select_alt_stub (stub_entry, r_type);
3914 if (alt_stub == NULL)
3915 {
3916 alt_stub = ((struct ppc_stub_hash_entry *)
3917 stub_hash_newfunc (NULL,
3918 &htab->stub_hash_table,
3919 stub_entry->root.string));
3920 if (alt_stub == NULL)
3921 return false;
3922
3923 *alt_stub = *stub_entry;
3924 stub_entry->root.next = &alt_stub->root;
3925
3926 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3927 Not that it matters, this just puts smaller stubs first. */
3928 if (stub_type.sub == ppc_stub_notoc)
3929 alt_stub = stub_entry;
3930 else if (stub_type.sub == ppc_stub_p9notoc
3931 && alt_stub->root.next
3932 && alt_stub->root.next->string == alt_stub->root.string)
3933 {
3934 struct ppc_stub_hash_entry *next
3935 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3936 alt_stub->type = next->type;
3937 alt_stub = next;
3938 }
3939 alt_stub->type = stub_type;
3940 return true;
3941 }
3942 stub_entry = alt_stub;
3943 }
3944
3945 old_type = stub_entry->type;
3946 if (old_type.main == ppc_stub_plt_branch)
3947 old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
3948
3949 if (old_type.main != stub_type.main
3950 || (old_type.sub != stub_type.sub
3951 && old_type.sub != ppc_stub_toc
3952 && stub_type.sub != ppc_stub_toc))
3953 abort ();
3954
3955 stub_entry->type.sub |= stub_type.sub;
3956 stub_entry->type.r2save |= stub_type.r2save;
3957 return true;
3958 }
3959
3960 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3961 not already done. */
3962
3963 static bool
3964 create_got_section (bfd *abfd, struct bfd_link_info *info)
3965 {
3966 asection *got, *relgot;
3967 flagword flags;
3968 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3969
3970 if (!is_ppc64_elf (abfd))
3971 return false;
3972 if (htab == NULL)
3973 return false;
3974
3975 if (!htab->elf.sgot
3976 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3977 return false;
3978
3979 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3980 | SEC_LINKER_CREATED);
3981
3982 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3983 if (!got
3984 || !bfd_set_section_alignment (got, 3))
3985 return false;
3986
3987 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3988 flags | SEC_READONLY);
3989 if (!relgot
3990 || !bfd_set_section_alignment (relgot, 3))
3991 return false;
3992
3993 ppc64_elf_tdata (abfd)->got = got;
3994 ppc64_elf_tdata (abfd)->relgot = relgot;
3995 return true;
3996 }
3997
3998 /* Follow indirect and warning symbol links. */
3999
4000 static inline struct bfd_link_hash_entry *
4001 follow_link (struct bfd_link_hash_entry *h)
4002 {
4003 while (h->type == bfd_link_hash_indirect
4004 || h->type == bfd_link_hash_warning)
4005 h = h->u.i.link;
4006 return h;
4007 }
4008
4009 static inline struct elf_link_hash_entry *
4010 elf_follow_link (struct elf_link_hash_entry *h)
4011 {
4012 return (struct elf_link_hash_entry *) follow_link (&h->root);
4013 }
4014
4015 static inline struct ppc_link_hash_entry *
4016 ppc_follow_link (struct ppc_link_hash_entry *h)
4017 {
4018 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4019 }
4020
4021 /* Merge PLT info on FROM with that on TO. */
4022
4023 static void
4024 move_plt_plist (struct ppc_link_hash_entry *from,
4025 struct ppc_link_hash_entry *to)
4026 {
4027 if (from->elf.plt.plist != NULL)
4028 {
4029 if (to->elf.plt.plist != NULL)
4030 {
4031 struct plt_entry **entp;
4032 struct plt_entry *ent;
4033
4034 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4035 {
4036 struct plt_entry *dent;
4037
4038 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4039 if (dent->addend == ent->addend)
4040 {
4041 dent->plt.refcount += ent->plt.refcount;
4042 *entp = ent->next;
4043 break;
4044 }
4045 if (dent == NULL)
4046 entp = &ent->next;
4047 }
4048 *entp = to->elf.plt.plist;
4049 }
4050
4051 to->elf.plt.plist = from->elf.plt.plist;
4052 from->elf.plt.plist = NULL;
4053 }
4054 }
4055
4056 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4057
4058 static void
4059 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4060 struct elf_link_hash_entry *dir,
4061 struct elf_link_hash_entry *ind)
4062 {
4063 struct ppc_link_hash_entry *edir, *eind;
4064
4065 edir = ppc_elf_hash_entry (dir);
4066 eind = ppc_elf_hash_entry (ind);
4067
4068 edir->is_func |= eind->is_func;
4069 edir->is_func_descriptor |= eind->is_func_descriptor;
4070 edir->tls_mask |= eind->tls_mask;
4071 if (eind->oh != NULL)
4072 edir->oh = ppc_follow_link (eind->oh);
4073
4074 if (edir->elf.versioned != versioned_hidden)
4075 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4076 edir->elf.ref_regular |= eind->elf.ref_regular;
4077 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4078 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4079 edir->elf.needs_plt |= eind->elf.needs_plt;
4080 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4081
4082 /* If we were called to copy over info for a weak sym, don't copy
4083 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4084 in order to simplify readonly_dynrelocs and save a field in the
4085 symbol hash entry, but that means dyn_relocs can't be used in any
4086 tests about a specific symbol, or affect other symbol flags which
4087 are then tested. */
4088 if (eind->elf.root.type != bfd_link_hash_indirect)
4089 return;
4090
4091 /* Copy over any dynamic relocs we may have on the indirect sym. */
4092 if (ind->dyn_relocs != NULL)
4093 {
4094 if (dir->dyn_relocs != NULL)
4095 {
4096 struct ppc_dyn_relocs **pp;
4097 struct ppc_dyn_relocs *p;
4098
4099 /* Add reloc counts against the indirect sym to the direct sym
4100 list. Merge any entries against the same section. */
4101 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4102 (p = *pp) != NULL;
4103 )
4104 {
4105 struct ppc_dyn_relocs *q;
4106
4107 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4108 q != NULL;
4109 q = q->next)
4110 if (q->sec == p->sec)
4111 {
4112 q->count += p->count;
4113 q->pc_count += p->pc_count;
4114 q->rel_count += p->rel_count;
4115 *pp = p->next;
4116 break;
4117 }
4118 if (q == NULL)
4119 pp = &p->next;
4120 }
4121 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4122 }
4123
4124 dir->dyn_relocs = ind->dyn_relocs;
4125 ind->dyn_relocs = NULL;
4126 }
4127
4128 /* Copy over got entries that we may have already seen to the
4129 symbol which just became indirect. */
4130 if (eind->elf.got.glist != NULL)
4131 {
4132 if (edir->elf.got.glist != NULL)
4133 {
4134 struct got_entry **entp;
4135 struct got_entry *ent;
4136
4137 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4138 {
4139 struct got_entry *dent;
4140
4141 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4142 if (dent->addend == ent->addend
4143 && dent->owner == ent->owner
4144 && dent->tls_type == ent->tls_type)
4145 {
4146 dent->got.refcount += ent->got.refcount;
4147 *entp = ent->next;
4148 break;
4149 }
4150 if (dent == NULL)
4151 entp = &ent->next;
4152 }
4153 *entp = edir->elf.got.glist;
4154 }
4155
4156 edir->elf.got.glist = eind->elf.got.glist;
4157 eind->elf.got.glist = NULL;
4158 }
4159
4160 /* And plt entries. */
4161 move_plt_plist (eind, edir);
4162
4163 if (eind->elf.dynindx != -1)
4164 {
4165 if (edir->elf.dynindx != -1)
4166 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4167 edir->elf.dynstr_index);
4168 edir->elf.dynindx = eind->elf.dynindx;
4169 edir->elf.dynstr_index = eind->elf.dynstr_index;
4170 eind->elf.dynindx = -1;
4171 eind->elf.dynstr_index = 0;
4172 }
4173 }
4174
4175 /* Find the function descriptor hash entry from the given function code
4176 hash entry FH. Link the entries via their OH fields. */
4177
4178 static struct ppc_link_hash_entry *
4179 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4180 {
4181 struct ppc_link_hash_entry *fdh = fh->oh;
4182
4183 if (fdh == NULL)
4184 {
4185 const char *fd_name = fh->elf.root.root.string + 1;
4186
4187 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4188 false, false, false));
4189 if (fdh == NULL)
4190 return fdh;
4191
4192 fdh->is_func_descriptor = 1;
4193 fdh->oh = fh;
4194 fh->is_func = 1;
4195 fh->oh = fdh;
4196 }
4197
4198 fdh = ppc_follow_link (fdh);
4199 fdh->is_func_descriptor = 1;
4200 fdh->oh = fh;
4201 return fdh;
4202 }
4203
4204 /* Make a fake function descriptor sym for the undefined code sym FH. */
4205
4206 static struct ppc_link_hash_entry *
4207 make_fdh (struct bfd_link_info *info,
4208 struct ppc_link_hash_entry *fh)
4209 {
4210 bfd *abfd = fh->elf.root.u.undef.abfd;
4211 struct bfd_link_hash_entry *bh = NULL;
4212 struct ppc_link_hash_entry *fdh;
4213 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4214 ? BSF_WEAK
4215 : BSF_GLOBAL);
4216
4217 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4218 fh->elf.root.root.string + 1,
4219 flags, bfd_und_section_ptr, 0,
4220 NULL, false, false, &bh))
4221 return NULL;
4222
4223 fdh = (struct ppc_link_hash_entry *) bh;
4224 fdh->elf.non_elf = 0;
4225 fdh->fake = 1;
4226 fdh->is_func_descriptor = 1;
4227 fdh->oh = fh;
4228 fh->is_func = 1;
4229 fh->oh = fdh;
4230 return fdh;
4231 }
4232
4233 /* Fix function descriptor symbols defined in .opd sections to be
4234 function type. */
4235
4236 static bool
4237 ppc64_elf_add_symbol_hook (bfd *ibfd,
4238 struct bfd_link_info *info,
4239 Elf_Internal_Sym *isym,
4240 const char **name,
4241 flagword *flags ATTRIBUTE_UNUSED,
4242 asection **sec,
4243 bfd_vma *value)
4244 {
4245 if (*sec != NULL
4246 && strcmp ((*sec)->name, ".opd") == 0)
4247 {
4248 asection *code_sec;
4249
4250 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4251 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4252 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4253
4254 /* If the symbol is a function defined in .opd, and the function
4255 code is in a discarded group, let it appear to be undefined. */
4256 if (!bfd_link_relocatable (info)
4257 && (*sec)->reloc_count != 0
4258 && opd_entry_value (*sec, *value, &code_sec, NULL,
4259 false) != (bfd_vma) -1
4260 && discarded_section (code_sec))
4261 {
4262 *sec = bfd_und_section_ptr;
4263 isym->st_shndx = SHN_UNDEF;
4264 }
4265 }
4266 else if (*sec != NULL
4267 && strcmp ((*sec)->name, ".toc") == 0
4268 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4269 {
4270 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4271 if (htab != NULL)
4272 htab->params->object_in_toc = 1;
4273 }
4274
4275 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4276 {
4277 if (abiversion (ibfd) == 0)
4278 set_abiversion (ibfd, 2);
4279 else if (abiversion (ibfd) == 1)
4280 {
4281 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4282 " for ABI version 1"), *name);
4283 bfd_set_error (bfd_error_bad_value);
4284 return false;
4285 }
4286 }
4287
4288 return true;
4289 }
4290
4291 /* Merge non-visibility st_other attributes: local entry point. */
4292
4293 static void
4294 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4295 unsigned int st_other,
4296 bool definition,
4297 bool dynamic)
4298 {
4299 if (definition && (!dynamic || !h->def_regular))
4300 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4301 | ELF_ST_VISIBILITY (h->other));
4302 }
4303
4304 /* Hook called on merging a symbol. We use this to clear "fake" since
4305 we now have a real symbol. */
4306
4307 static bool
4308 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4309 const Elf_Internal_Sym *isym,
4310 asection **psec ATTRIBUTE_UNUSED,
4311 bool newdef ATTRIBUTE_UNUSED,
4312 bool olddef ATTRIBUTE_UNUSED,
4313 bfd *oldbfd ATTRIBUTE_UNUSED,
4314 const asection *oldsec ATTRIBUTE_UNUSED)
4315 {
4316 ppc_elf_hash_entry (h)->fake = 0;
4317 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4318 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4319 return true;
4320 }
4321
4322 /* This function makes an old ABI object reference to ".bar" cause the
4323 inclusion of a new ABI object archive that defines "bar".
4324 NAME is a symbol defined in an archive. Return a symbol in the hash
4325 table that might be satisfied by the archive symbols. */
4326
4327 static struct bfd_link_hash_entry *
4328 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4329 struct bfd_link_info *info,
4330 const char *name)
4331 {
4332 struct bfd_link_hash_entry *h;
4333 char *dot_name;
4334 size_t len;
4335
4336 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4337 if (h != NULL
4338 && ppc_hash_table (info) != NULL
4339 /* Don't return this sym if it is a fake function descriptor
4340 created by add_symbol_adjust. */
4341 && !((struct ppc_link_hash_entry *) h)->fake)
4342 return h;
4343
4344 if (name[0] == '.')
4345 return h;
4346
4347 len = strlen (name);
4348 dot_name = bfd_alloc (abfd, len + 2);
4349 if (dot_name == NULL)
4350 return (struct bfd_link_hash_entry *) -1;
4351 dot_name[0] = '.';
4352 memcpy (dot_name + 1, name, len + 1);
4353 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4354 bfd_release (abfd, dot_name);
4355 if (h != NULL)
4356 return h;
4357
4358 if (strcmp (name, "__tls_get_addr_opt") == 0)
4359 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4360 return h;
4361 }
4362
4363 /* This function satisfies all old ABI object references to ".bar" if a
4364 new ABI object defines "bar". Well, at least, undefined dot symbols
4365 are made weak. This stops later archive searches from including an
4366 object if we already have a function descriptor definition. It also
4367 prevents the linker complaining about undefined symbols.
4368 We also check and correct mismatched symbol visibility here. The
4369 most restrictive visibility of the function descriptor and the
4370 function entry symbol is used. */
4371
4372 static bool
4373 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4374 {
4375 struct ppc_link_hash_table *htab;
4376 struct ppc_link_hash_entry *fdh;
4377
4378 if (eh->elf.root.type == bfd_link_hash_warning)
4379 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4380
4381 if (eh->elf.root.type == bfd_link_hash_indirect)
4382 return true;
4383
4384 if (eh->elf.root.root.string[0] != '.')
4385 abort ();
4386
4387 htab = ppc_hash_table (info);
4388 if (htab == NULL)
4389 return false;
4390
4391 fdh = lookup_fdh (eh, htab);
4392 if (fdh == NULL
4393 && !bfd_link_relocatable (info)
4394 && (eh->elf.root.type == bfd_link_hash_undefined
4395 || eh->elf.root.type == bfd_link_hash_undefweak)
4396 && eh->elf.ref_regular)
4397 {
4398 /* Make an undefined function descriptor sym, in order to
4399 pull in an --as-needed shared lib. Archives are handled
4400 elsewhere. */
4401 fdh = make_fdh (info, eh);
4402 if (fdh == NULL)
4403 return false;
4404 }
4405
4406 if (fdh != NULL)
4407 {
4408 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4409 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4410
4411 /* Make both descriptor and entry symbol have the most
4412 constraining visibility of either symbol. */
4413 if (entry_vis < descr_vis)
4414 fdh->elf.other += entry_vis - descr_vis;
4415 else if (entry_vis > descr_vis)
4416 eh->elf.other += descr_vis - entry_vis;
4417
4418 /* Propagate reference flags from entry symbol to function
4419 descriptor symbol. */
4420 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4421 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4422 fdh->elf.ref_regular |= eh->elf.ref_regular;
4423 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4424
4425 if (!fdh->elf.forced_local
4426 && fdh->elf.dynindx == -1
4427 && fdh->elf.versioned != versioned_hidden
4428 && (bfd_link_dll (info)
4429 || fdh->elf.def_dynamic
4430 || fdh->elf.ref_dynamic)
4431 && (eh->elf.ref_regular
4432 || eh->elf.def_regular))
4433 {
4434 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4435 return false;
4436 }
4437 }
4438
4439 return true;
4440 }
4441
4442 /* Set up opd section info and abiversion for IBFD, and process list
4443 of dot-symbols we made in link_hash_newfunc. */
4444
4445 static bool
4446 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4447 {
4448 struct ppc_link_hash_table *htab;
4449 struct ppc_link_hash_entry **p, *eh;
4450 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4451
4452 if (opd != NULL && opd->size != 0)
4453 {
4454 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4455 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4456
4457 if (abiversion (ibfd) == 0)
4458 set_abiversion (ibfd, 1);
4459 else if (abiversion (ibfd) >= 2)
4460 {
4461 /* xgettext:c-format */
4462 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4463 ibfd, abiversion (ibfd));
4464 bfd_set_error (bfd_error_bad_value);
4465 return false;
4466 }
4467 }
4468
4469 if (is_ppc64_elf (info->output_bfd))
4470 {
4471 /* For input files without an explicit abiversion in e_flags
4472 we should have flagged any with symbol st_other bits set
4473 as ELFv1 and above flagged those with .opd as ELFv2.
4474 Set the output abiversion if not yet set, and for any input
4475 still ambiguous, take its abiversion from the output.
4476 Differences in ABI are reported later. */
4477 if (abiversion (info->output_bfd) == 0)
4478 set_abiversion (info->output_bfd, abiversion (ibfd));
4479 else if (abiversion (ibfd) == 0)
4480 set_abiversion (ibfd, abiversion (info->output_bfd));
4481 }
4482
4483 htab = ppc_hash_table (info);
4484 if (htab == NULL)
4485 return true;
4486
4487 if (opd != NULL && opd->size != 0
4488 && (ibfd->flags & DYNAMIC) == 0
4489 && (opd->flags & SEC_RELOC) != 0
4490 && opd->reloc_count != 0
4491 && !bfd_is_abs_section (opd->output_section)
4492 && info->gc_sections)
4493 {
4494 /* Garbage collection needs some extra help with .opd sections.
4495 We don't want to necessarily keep everything referenced by
4496 relocs in .opd, as that would keep all functions. Instead,
4497 if we reference an .opd symbol (a function descriptor), we
4498 want to keep the function code symbol's section. This is
4499 easy for global symbols, but for local syms we need to keep
4500 information about the associated function section. */
4501 bfd_size_type amt;
4502 asection **opd_sym_map;
4503 Elf_Internal_Shdr *symtab_hdr;
4504 Elf_Internal_Rela *relocs, *rel_end, *rel;
4505
4506 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4507 opd_sym_map = bfd_zalloc (ibfd, amt);
4508 if (opd_sym_map == NULL)
4509 return false;
4510 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4511 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4512 info->keep_memory);
4513 if (relocs == NULL)
4514 return false;
4515 symtab_hdr = &elf_symtab_hdr (ibfd);
4516 rel_end = relocs + opd->reloc_count - 1;
4517 for (rel = relocs; rel < rel_end; rel++)
4518 {
4519 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4520 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4521
4522 if (r_type == R_PPC64_ADDR64
4523 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4524 && r_symndx < symtab_hdr->sh_info)
4525 {
4526 Elf_Internal_Sym *isym;
4527 asection *s;
4528
4529 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4530 r_symndx);
4531 if (isym == NULL)
4532 {
4533 if (elf_section_data (opd)->relocs != relocs)
4534 free (relocs);
4535 return false;
4536 }
4537
4538 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4539 if (s != NULL && s != opd)
4540 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4541 }
4542 }
4543 if (elf_section_data (opd)->relocs != relocs)
4544 free (relocs);
4545 }
4546
4547 p = &htab->dot_syms;
4548 while ((eh = *p) != NULL)
4549 {
4550 *p = NULL;
4551 if (&eh->elf == htab->elf.hgot)
4552 ;
4553 else if (htab->elf.hgot == NULL
4554 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4555 htab->elf.hgot = &eh->elf;
4556 else if (abiversion (ibfd) <= 1)
4557 {
4558 htab->need_func_desc_adj = 1;
4559 if (!add_symbol_adjust (eh, info))
4560 return false;
4561 }
4562 p = &eh->u.next_dot_sym;
4563 }
4564 return true;
4565 }
4566
4567 /* Undo hash table changes when an --as-needed input file is determined
4568 not to be needed. */
4569
4570 static bool
4571 ppc64_elf_notice_as_needed (bfd *ibfd,
4572 struct bfd_link_info *info,
4573 enum notice_asneeded_action act)
4574 {
4575 if (act == notice_not_needed)
4576 {
4577 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4578
4579 if (htab == NULL)
4580 return false;
4581
4582 htab->dot_syms = NULL;
4583 }
4584 return _bfd_elf_notice_as_needed (ibfd, info, act);
4585 }
4586
4587 /* If --just-symbols against a final linked binary, then assume we need
4588 toc adjusting stubs when calling functions defined there. */
4589
4590 static void
4591 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4592 {
4593 if ((sec->flags & SEC_CODE) != 0
4594 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4595 && is_ppc64_elf (sec->owner))
4596 {
4597 if (abiversion (sec->owner) >= 2
4598 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4599 sec->has_toc_reloc = 1;
4600 }
4601 _bfd_elf_link_just_syms (sec, info);
4602 }
4603
4604 static struct plt_entry **
4605 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4606 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4607 {
4608 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4609 struct plt_entry **local_plt;
4610 unsigned char *local_got_tls_masks;
4611
4612 if (local_got_ents == NULL)
4613 {
4614 bfd_size_type size = symtab_hdr->sh_info;
4615
4616 size *= (sizeof (*local_got_ents)
4617 + sizeof (*local_plt)
4618 + sizeof (*local_got_tls_masks));
4619 local_got_ents = bfd_zalloc (abfd, size);
4620 if (local_got_ents == NULL)
4621 return NULL;
4622 elf_local_got_ents (abfd) = local_got_ents;
4623 }
4624
4625 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4626 {
4627 struct got_entry *ent;
4628
4629 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4630 if (ent->addend == r_addend
4631 && ent->owner == abfd
4632 && ent->tls_type == tls_type)
4633 break;
4634 if (ent == NULL)
4635 {
4636 size_t amt = sizeof (*ent);
4637 ent = bfd_alloc (abfd, amt);
4638 if (ent == NULL)
4639 return false;
4640 ent->next = local_got_ents[r_symndx];
4641 ent->addend = r_addend;
4642 ent->owner = abfd;
4643 ent->tls_type = tls_type;
4644 ent->is_indirect = false;
4645 ent->got.refcount = 0;
4646 local_got_ents[r_symndx] = ent;
4647 }
4648 ent->got.refcount += 1;
4649 }
4650
4651 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4652 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4653 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4654
4655 return local_plt + r_symndx;
4656 }
4657
4658 static bool
4659 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4660 {
4661 struct plt_entry *ent;
4662
4663 for (ent = *plist; ent != NULL; ent = ent->next)
4664 if (ent->addend == addend)
4665 break;
4666 if (ent == NULL)
4667 {
4668 size_t amt = sizeof (*ent);
4669 ent = bfd_alloc (abfd, amt);
4670 if (ent == NULL)
4671 return false;
4672 ent->next = *plist;
4673 ent->addend = addend;
4674 ent->plt.refcount = 0;
4675 *plist = ent;
4676 }
4677 ent->plt.refcount += 1;
4678 return true;
4679 }
4680
4681 static bool
4682 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4683 {
4684 return (r_type == R_PPC64_REL24
4685 || r_type == R_PPC64_REL24_NOTOC
4686 || r_type == R_PPC64_REL24_P9NOTOC
4687 || r_type == R_PPC64_REL14
4688 || r_type == R_PPC64_REL14_BRTAKEN
4689 || r_type == R_PPC64_REL14_BRNTAKEN
4690 || r_type == R_PPC64_ADDR24
4691 || r_type == R_PPC64_ADDR14
4692 || r_type == R_PPC64_ADDR14_BRTAKEN
4693 || r_type == R_PPC64_ADDR14_BRNTAKEN
4694 || r_type == R_PPC64_PLTCALL
4695 || r_type == R_PPC64_PLTCALL_NOTOC);
4696 }
4697
4698 /* Relocs on inline plt call sequence insns prior to the call. */
4699
4700 static bool
4701 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4702 {
4703 return (r_type == R_PPC64_PLT16_HA
4704 || r_type == R_PPC64_PLT16_HI
4705 || r_type == R_PPC64_PLT16_LO
4706 || r_type == R_PPC64_PLT16_LO_DS
4707 || r_type == R_PPC64_PLT_PCREL34
4708 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4709 || r_type == R_PPC64_PLTSEQ
4710 || r_type == R_PPC64_PLTSEQ_NOTOC);
4711 }
4712
4713 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4714 relocs, return true for those that operate on a dword. */
4715
4716 static bool
4717 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4718 {
4719 return (r_type == R_PPC64_PLT_PCREL34
4720 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4721 || r_type == R_PPC64_PLTCALL);
4722 }
4723
4724 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4725 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4726
4727 static bool
4728 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4729 {
4730 return offset <= sec->size && size <= sec->size - offset;
4731 }
4732
4733 /* Look through the relocs for a section during the first phase, and
4734 calculate needed space in the global offset table, procedure
4735 linkage table, and dynamic reloc sections. */
4736
4737 static bool
4738 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4739 asection *sec, const Elf_Internal_Rela *relocs)
4740 {
4741 struct ppc_link_hash_table *htab;
4742 Elf_Internal_Shdr *symtab_hdr;
4743 struct elf_link_hash_entry **sym_hashes;
4744 const Elf_Internal_Rela *rel;
4745 const Elf_Internal_Rela *rel_end;
4746 asection *sreloc;
4747 struct elf_link_hash_entry *tga, *dottga;
4748 bool is_opd;
4749
4750 if (bfd_link_relocatable (info))
4751 return true;
4752
4753 BFD_ASSERT (is_ppc64_elf (abfd));
4754
4755 htab = ppc_hash_table (info);
4756 if (htab == NULL)
4757 return false;
4758
4759 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4760 false, false, true);
4761 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4762 false, false, true);
4763 symtab_hdr = &elf_symtab_hdr (abfd);
4764 sym_hashes = elf_sym_hashes (abfd);
4765 sreloc = NULL;
4766 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4767 rel_end = relocs + sec->reloc_count;
4768 for (rel = relocs; rel < rel_end; rel++)
4769 {
4770 unsigned long r_symndx;
4771 struct elf_link_hash_entry *h;
4772 enum elf_ppc64_reloc_type r_type;
4773 int tls_type;
4774 struct _ppc64_elf_section_data *ppc64_sec;
4775 struct plt_entry **ifunc, **plt_list;
4776
4777 r_symndx = ELF64_R_SYM (rel->r_info);
4778 if (r_symndx < symtab_hdr->sh_info)
4779 h = NULL;
4780 else
4781 {
4782 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4783 h = elf_follow_link (h);
4784
4785 if (h == htab->elf.hgot)
4786 sec->has_toc_reloc = 1;
4787 }
4788
4789 r_type = ELF64_R_TYPE (rel->r_info);
4790 switch (r_type)
4791 {
4792 case R_PPC64_D34:
4793 case R_PPC64_D34_LO:
4794 case R_PPC64_D34_HI30:
4795 case R_PPC64_D34_HA30:
4796 case R_PPC64_D28:
4797 case R_PPC64_TPREL34:
4798 case R_PPC64_DTPREL34:
4799 case R_PPC64_PCREL34:
4800 case R_PPC64_GOT_PCREL34:
4801 case R_PPC64_GOT_TLSGD_PCREL34:
4802 case R_PPC64_GOT_TLSLD_PCREL34:
4803 case R_PPC64_GOT_TPREL_PCREL34:
4804 case R_PPC64_GOT_DTPREL_PCREL34:
4805 case R_PPC64_PLT_PCREL34:
4806 case R_PPC64_PLT_PCREL34_NOTOC:
4807 case R_PPC64_PCREL28:
4808 htab->has_power10_relocs = 1;
4809 break;
4810 default:
4811 break;
4812 }
4813
4814 switch (r_type)
4815 {
4816 case R_PPC64_PLT16_HA:
4817 case R_PPC64_GOT_TLSLD16_HA:
4818 case R_PPC64_GOT_TLSGD16_HA:
4819 case R_PPC64_GOT_TPREL16_HA:
4820 case R_PPC64_GOT_DTPREL16_HA:
4821 case R_PPC64_GOT16_HA:
4822 case R_PPC64_TOC16_HA:
4823 case R_PPC64_PLT16_LO:
4824 case R_PPC64_PLT16_LO_DS:
4825 case R_PPC64_GOT_TLSLD16_LO:
4826 case R_PPC64_GOT_TLSGD16_LO:
4827 case R_PPC64_GOT_TPREL16_LO_DS:
4828 case R_PPC64_GOT_DTPREL16_LO_DS:
4829 case R_PPC64_GOT16_LO:
4830 case R_PPC64_GOT16_LO_DS:
4831 case R_PPC64_TOC16_LO:
4832 case R_PPC64_TOC16_LO_DS:
4833 case R_PPC64_GOT_PCREL34:
4834 ppc64_elf_tdata (abfd)->has_optrel = 1;
4835 ppc64_elf_section_data (sec)->has_optrel = 1;
4836 break;
4837 default:
4838 break;
4839 }
4840
4841 ifunc = NULL;
4842 if (h != NULL)
4843 {
4844 if (h->type == STT_GNU_IFUNC)
4845 {
4846 h->needs_plt = 1;
4847 ifunc = &h->plt.plist;
4848 }
4849 }
4850 else
4851 {
4852 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
4853 abfd, r_symndx);
4854 if (isym == NULL)
4855 return false;
4856
4857 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4858 {
4859 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4860 rel->r_addend,
4861 NON_GOT | PLT_IFUNC);
4862 if (ifunc == NULL)
4863 return false;
4864 }
4865 }
4866
4867 tls_type = 0;
4868 switch (r_type)
4869 {
4870 case R_PPC64_TLSGD:
4871 case R_PPC64_TLSLD:
4872 /* These special tls relocs tie a call to __tls_get_addr with
4873 its parameter symbol. */
4874 if (h != NULL)
4875 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4876 else
4877 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4878 rel->r_addend,
4879 NON_GOT | TLS_TLS | TLS_MARK))
4880 return false;
4881 sec->has_tls_reloc = 1;
4882 break;
4883
4884 case R_PPC64_GOT_TLSLD16:
4885 case R_PPC64_GOT_TLSLD16_LO:
4886 case R_PPC64_GOT_TLSLD16_HI:
4887 case R_PPC64_GOT_TLSLD16_HA:
4888 case R_PPC64_GOT_TLSLD_PCREL34:
4889 tls_type = TLS_TLS | TLS_LD;
4890 goto dogottls;
4891
4892 case R_PPC64_GOT_TLSGD16:
4893 case R_PPC64_GOT_TLSGD16_LO:
4894 case R_PPC64_GOT_TLSGD16_HI:
4895 case R_PPC64_GOT_TLSGD16_HA:
4896 case R_PPC64_GOT_TLSGD_PCREL34:
4897 tls_type = TLS_TLS | TLS_GD;
4898 goto dogottls;
4899
4900 case R_PPC64_GOT_TPREL16_DS:
4901 case R_PPC64_GOT_TPREL16_LO_DS:
4902 case R_PPC64_GOT_TPREL16_HI:
4903 case R_PPC64_GOT_TPREL16_HA:
4904 case R_PPC64_GOT_TPREL_PCREL34:
4905 if (bfd_link_dll (info))
4906 info->flags |= DF_STATIC_TLS;
4907 tls_type = TLS_TLS | TLS_TPREL;
4908 goto dogottls;
4909
4910 case R_PPC64_GOT_DTPREL16_DS:
4911 case R_PPC64_GOT_DTPREL16_LO_DS:
4912 case R_PPC64_GOT_DTPREL16_HI:
4913 case R_PPC64_GOT_DTPREL16_HA:
4914 case R_PPC64_GOT_DTPREL_PCREL34:
4915 tls_type = TLS_TLS | TLS_DTPREL;
4916 dogottls:
4917 sec->has_tls_reloc = 1;
4918 goto dogot;
4919
4920 case R_PPC64_GOT16:
4921 case R_PPC64_GOT16_LO:
4922 case R_PPC64_GOT16_HI:
4923 case R_PPC64_GOT16_HA:
4924 case R_PPC64_GOT16_DS:
4925 case R_PPC64_GOT16_LO_DS:
4926 case R_PPC64_GOT_PCREL34:
4927 dogot:
4928 /* This symbol requires a global offset table entry. */
4929 sec->has_toc_reloc = 1;
4930 if (r_type == R_PPC64_GOT_TLSLD16
4931 || r_type == R_PPC64_GOT_TLSGD16
4932 || r_type == R_PPC64_GOT_TPREL16_DS
4933 || r_type == R_PPC64_GOT_DTPREL16_DS
4934 || r_type == R_PPC64_GOT16
4935 || r_type == R_PPC64_GOT16_DS)
4936 {
4937 htab->do_multi_toc = 1;
4938 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4939 }
4940
4941 if (ppc64_elf_tdata (abfd)->got == NULL
4942 && !create_got_section (abfd, info))
4943 return false;
4944
4945 if (h != NULL)
4946 {
4947 struct ppc_link_hash_entry *eh;
4948 struct got_entry *ent;
4949
4950 eh = ppc_elf_hash_entry (h);
4951 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4952 if (ent->addend == rel->r_addend
4953 && ent->owner == abfd
4954 && ent->tls_type == tls_type)
4955 break;
4956 if (ent == NULL)
4957 {
4958 size_t amt = sizeof (*ent);
4959 ent = bfd_alloc (abfd, amt);
4960 if (ent == NULL)
4961 return false;
4962 ent->next = eh->elf.got.glist;
4963 ent->addend = rel->r_addend;
4964 ent->owner = abfd;
4965 ent->tls_type = tls_type;
4966 ent->is_indirect = false;
4967 ent->got.refcount = 0;
4968 eh->elf.got.glist = ent;
4969 }
4970 ent->got.refcount += 1;
4971 eh->tls_mask |= tls_type;
4972 }
4973 else
4974 /* This is a global offset table entry for a local symbol. */
4975 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4976 rel->r_addend, tls_type))
4977 return false;
4978 break;
4979
4980 case R_PPC64_PLT16_HA:
4981 case R_PPC64_PLT16_HI:
4982 case R_PPC64_PLT16_LO:
4983 case R_PPC64_PLT16_LO_DS:
4984 case R_PPC64_PLT_PCREL34:
4985 case R_PPC64_PLT_PCREL34_NOTOC:
4986 case R_PPC64_PLT32:
4987 case R_PPC64_PLT64:
4988 /* This symbol requires a procedure linkage table entry. */
4989 plt_list = ifunc;
4990 if (h != NULL)
4991 {
4992 h->needs_plt = 1;
4993 if (h->root.root.string[0] == '.'
4994 && h->root.root.string[1] != '\0')
4995 ppc_elf_hash_entry (h)->is_func = 1;
4996 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
4997 plt_list = &h->plt.plist;
4998 }
4999 if (plt_list == NULL)
5000 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5001 rel->r_addend,
5002 NON_GOT | PLT_KEEP);
5003 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5004 return false;
5005 break;
5006
5007 /* The following relocations don't need to propagate the
5008 relocation if linking a shared object since they are
5009 section relative. */
5010 case R_PPC64_SECTOFF:
5011 case R_PPC64_SECTOFF_LO:
5012 case R_PPC64_SECTOFF_HI:
5013 case R_PPC64_SECTOFF_HA:
5014 case R_PPC64_SECTOFF_DS:
5015 case R_PPC64_SECTOFF_LO_DS:
5016 case R_PPC64_DTPREL16:
5017 case R_PPC64_DTPREL16_LO:
5018 case R_PPC64_DTPREL16_HI:
5019 case R_PPC64_DTPREL16_HA:
5020 case R_PPC64_DTPREL16_DS:
5021 case R_PPC64_DTPREL16_LO_DS:
5022 case R_PPC64_DTPREL16_HIGH:
5023 case R_PPC64_DTPREL16_HIGHA:
5024 case R_PPC64_DTPREL16_HIGHER:
5025 case R_PPC64_DTPREL16_HIGHERA:
5026 case R_PPC64_DTPREL16_HIGHEST:
5027 case R_PPC64_DTPREL16_HIGHESTA:
5028 break;
5029
5030 /* Nor do these. */
5031 case R_PPC64_REL16:
5032 case R_PPC64_REL16_LO:
5033 case R_PPC64_REL16_HI:
5034 case R_PPC64_REL16_HA:
5035 case R_PPC64_REL16_HIGH:
5036 case R_PPC64_REL16_HIGHA:
5037 case R_PPC64_REL16_HIGHER:
5038 case R_PPC64_REL16_HIGHERA:
5039 case R_PPC64_REL16_HIGHEST:
5040 case R_PPC64_REL16_HIGHESTA:
5041 case R_PPC64_REL16_HIGHER34:
5042 case R_PPC64_REL16_HIGHERA34:
5043 case R_PPC64_REL16_HIGHEST34:
5044 case R_PPC64_REL16_HIGHESTA34:
5045 case R_PPC64_REL16DX_HA:
5046 break;
5047
5048 /* Not supported as a dynamic relocation. */
5049 case R_PPC64_ADDR64_LOCAL:
5050 if (bfd_link_pic (info))
5051 {
5052 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5053 ppc_howto_init ();
5054 /* xgettext:c-format */
5055 info->callbacks->einfo (_("%H: %s reloc unsupported "
5056 "in shared libraries and PIEs\n"),
5057 abfd, sec, rel->r_offset,
5058 ppc64_elf_howto_table[r_type]->name);
5059 bfd_set_error (bfd_error_bad_value);
5060 return false;
5061 }
5062 break;
5063
5064 case R_PPC64_TOC16:
5065 case R_PPC64_TOC16_DS:
5066 htab->do_multi_toc = 1;
5067 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5068 /* Fall through. */
5069 case R_PPC64_TOC16_LO:
5070 case R_PPC64_TOC16_HI:
5071 case R_PPC64_TOC16_HA:
5072 case R_PPC64_TOC16_LO_DS:
5073 sec->has_toc_reloc = 1;
5074 if (h != NULL && bfd_link_executable (info))
5075 {
5076 /* We may need a copy reloc. */
5077 h->non_got_ref = 1;
5078 /* Strongly prefer a copy reloc over a dynamic reloc.
5079 glibc ld.so as of 2019-08 will error out if one of
5080 these relocations is emitted. */
5081 h->needs_copy = 1;
5082 goto dodyn;
5083 }
5084 break;
5085
5086 /* Marker reloc. */
5087 case R_PPC64_ENTRY:
5088 break;
5089
5090 /* This relocation describes the C++ object vtable hierarchy.
5091 Reconstruct it for later use during GC. */
5092 case R_PPC64_GNU_VTINHERIT:
5093 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5094 return false;
5095 break;
5096
5097 /* This relocation describes which C++ vtable entries are actually
5098 used. Record for later use during GC. */
5099 case R_PPC64_GNU_VTENTRY:
5100 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5101 return false;
5102 break;
5103
5104 case R_PPC64_REL14:
5105 case R_PPC64_REL14_BRTAKEN:
5106 case R_PPC64_REL14_BRNTAKEN:
5107 {
5108 asection *dest = NULL;
5109
5110 /* Heuristic: If jumping outside our section, chances are
5111 we are going to need a stub. */
5112 if (h != NULL)
5113 {
5114 /* If the sym is weak it may be overridden later, so
5115 don't assume we know where a weak sym lives. */
5116 if (h->root.type == bfd_link_hash_defined)
5117 dest = h->root.u.def.section;
5118 }
5119 else
5120 {
5121 Elf_Internal_Sym *isym;
5122
5123 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5124 abfd, r_symndx);
5125 if (isym == NULL)
5126 return false;
5127
5128 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5129 }
5130
5131 if (dest != sec)
5132 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5133 }
5134 goto rel24;
5135
5136 case R_PPC64_PLTCALL:
5137 case R_PPC64_PLTCALL_NOTOC:
5138 ppc64_elf_section_data (sec)->has_pltcall = 1;
5139 /* Fall through. */
5140
5141 case R_PPC64_REL24:
5142 case R_PPC64_REL24_NOTOC:
5143 case R_PPC64_REL24_P9NOTOC:
5144 rel24:
5145 plt_list = ifunc;
5146 if (h != NULL)
5147 {
5148 h->needs_plt = 1;
5149 if (h->root.root.string[0] == '.'
5150 && h->root.root.string[1] != '\0')
5151 ppc_elf_hash_entry (h)->is_func = 1;
5152
5153 if (h == tga || h == dottga)
5154 {
5155 sec->has_tls_reloc = 1;
5156 if (rel != relocs
5157 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5158 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5159 /* We have a new-style __tls_get_addr call with
5160 a marker reloc. */
5161 ;
5162 else
5163 /* Mark this section as having an old-style call. */
5164 sec->nomark_tls_get_addr = 1;
5165 }
5166 plt_list = &h->plt.plist;
5167 }
5168
5169 /* We may need a .plt entry if the function this reloc
5170 refers to is in a shared lib. */
5171 if (plt_list
5172 && !update_plt_info (abfd, plt_list, rel->r_addend))
5173 return false;
5174 break;
5175
5176 case R_PPC64_ADDR14:
5177 case R_PPC64_ADDR14_BRNTAKEN:
5178 case R_PPC64_ADDR14_BRTAKEN:
5179 case R_PPC64_ADDR24:
5180 goto dodyn;
5181
5182 case R_PPC64_TPREL64:
5183 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5184 if (bfd_link_dll (info))
5185 info->flags |= DF_STATIC_TLS;
5186 goto dotlstoc;
5187
5188 case R_PPC64_DTPMOD64:
5189 if (rel + 1 < rel_end
5190 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5191 && rel[1].r_offset == rel->r_offset + 8)
5192 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5193 else
5194 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5195 goto dotlstoc;
5196
5197 case R_PPC64_DTPREL64:
5198 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5199 if (rel != relocs
5200 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5201 && rel[-1].r_offset == rel->r_offset - 8)
5202 /* This is the second reloc of a dtpmod, dtprel pair.
5203 Don't mark with TLS_DTPREL. */
5204 goto dodyn;
5205
5206 dotlstoc:
5207 sec->has_tls_reloc = 1;
5208 if (h != NULL)
5209 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5210 else
5211 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5212 rel->r_addend, tls_type))
5213 return false;
5214
5215 ppc64_sec = ppc64_elf_section_data (sec);
5216 if (ppc64_sec->sec_type != sec_toc)
5217 {
5218 bfd_size_type amt;
5219
5220 /* One extra to simplify get_tls_mask. */
5221 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5222 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5223 if (ppc64_sec->u.toc.symndx == NULL)
5224 return false;
5225 amt = sec->size * sizeof (bfd_vma) / 8;
5226 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5227 if (ppc64_sec->u.toc.add == NULL)
5228 return false;
5229 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5230 ppc64_sec->sec_type = sec_toc;
5231 }
5232 BFD_ASSERT (rel->r_offset % 8 == 0);
5233 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5234 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5235
5236 /* Mark the second slot of a GD or LD entry.
5237 -1 to indicate GD and -2 to indicate LD. */
5238 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5239 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5240 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5241 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5242 goto dodyn;
5243
5244 case R_PPC64_TPREL16_HI:
5245 case R_PPC64_TPREL16_HA:
5246 case R_PPC64_TPREL16_HIGH:
5247 case R_PPC64_TPREL16_HIGHA:
5248 case R_PPC64_TPREL16_HIGHER:
5249 case R_PPC64_TPREL16_HIGHERA:
5250 case R_PPC64_TPREL16_HIGHEST:
5251 case R_PPC64_TPREL16_HIGHESTA:
5252 sec->has_tls_reloc = 1;
5253 /* Fall through. */
5254 case R_PPC64_TPREL34:
5255 case R_PPC64_TPREL16:
5256 case R_PPC64_TPREL16_DS:
5257 case R_PPC64_TPREL16_LO:
5258 case R_PPC64_TPREL16_LO_DS:
5259 if (bfd_link_dll (info))
5260 info->flags |= DF_STATIC_TLS;
5261 goto dodyn;
5262
5263 case R_PPC64_ADDR64:
5264 if (is_opd
5265 && rel + 1 < rel_end
5266 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5267 {
5268 if (h != NULL)
5269 ppc_elf_hash_entry (h)->is_func = 1;
5270 }
5271 /* Fall through. */
5272
5273 case R_PPC64_ADDR16:
5274 case R_PPC64_ADDR16_DS:
5275 case R_PPC64_ADDR16_HA:
5276 case R_PPC64_ADDR16_HI:
5277 case R_PPC64_ADDR16_HIGH:
5278 case R_PPC64_ADDR16_HIGHA:
5279 case R_PPC64_ADDR16_HIGHER:
5280 case R_PPC64_ADDR16_HIGHERA:
5281 case R_PPC64_ADDR16_HIGHEST:
5282 case R_PPC64_ADDR16_HIGHESTA:
5283 case R_PPC64_ADDR16_LO:
5284 case R_PPC64_ADDR16_LO_DS:
5285 case R_PPC64_D34:
5286 case R_PPC64_D34_LO:
5287 case R_PPC64_D34_HI30:
5288 case R_PPC64_D34_HA30:
5289 case R_PPC64_ADDR16_HIGHER34:
5290 case R_PPC64_ADDR16_HIGHERA34:
5291 case R_PPC64_ADDR16_HIGHEST34:
5292 case R_PPC64_ADDR16_HIGHESTA34:
5293 case R_PPC64_D28:
5294 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5295 && rel->r_addend == 0)
5296 {
5297 /* We may need a .plt entry if this reloc refers to a
5298 function in a shared lib. */
5299 if (!update_plt_info (abfd, &h->plt.plist, 0))
5300 return false;
5301 h->pointer_equality_needed = 1;
5302 }
5303 /* Fall through. */
5304
5305 case R_PPC64_REL30:
5306 case R_PPC64_REL32:
5307 case R_PPC64_REL64:
5308 case R_PPC64_ADDR32:
5309 case R_PPC64_UADDR16:
5310 case R_PPC64_UADDR32:
5311 case R_PPC64_UADDR64:
5312 case R_PPC64_TOC:
5313 if (h != NULL && bfd_link_executable (info))
5314 /* We may need a copy reloc. */
5315 h->non_got_ref = 1;
5316
5317 /* Don't propagate .opd relocs. */
5318 if (NO_OPD_RELOCS && is_opd)
5319 break;
5320
5321 /* If we are creating a shared library, and this is a reloc
5322 against a global symbol, or a non PC relative reloc
5323 against a local symbol, then we need to copy the reloc
5324 into the shared library. However, if we are linking with
5325 -Bsymbolic, we do not need to copy a reloc against a
5326 global symbol which is defined in an object we are
5327 including in the link (i.e., DEF_REGULAR is set). At
5328 this point we have not seen all the input files, so it is
5329 possible that DEF_REGULAR is not set now but will be set
5330 later (it is never cleared). In case of a weak definition,
5331 DEF_REGULAR may be cleared later by a strong definition in
5332 a shared library. We account for that possibility below by
5333 storing information in the dyn_relocs field of the hash
5334 table entry. A similar situation occurs when creating
5335 shared libraries and symbol visibility changes render the
5336 symbol local.
5337
5338 If on the other hand, we are creating an executable, we
5339 may need to keep relocations for symbols satisfied by a
5340 dynamic library if we manage to avoid copy relocs for the
5341 symbol. */
5342 dodyn:
5343 if ((h != NULL
5344 && (h->root.type == bfd_link_hash_defweak
5345 || !h->def_regular))
5346 || (h != NULL
5347 && !bfd_link_executable (info)
5348 && !SYMBOLIC_BIND (info, h))
5349 || (bfd_link_pic (info)
5350 && must_be_dyn_reloc (info, r_type))
5351 || (!bfd_link_pic (info)
5352 && ifunc != NULL))
5353 {
5354 /* We must copy these reloc types into the output file.
5355 Create a reloc section in dynobj and make room for
5356 this reloc. */
5357 if (sreloc == NULL)
5358 {
5359 sreloc = _bfd_elf_make_dynamic_reloc_section
5360 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5361
5362 if (sreloc == NULL)
5363 return false;
5364 }
5365
5366 /* If this is a global symbol, we count the number of
5367 relocations we need for this symbol. */
5368 if (h != NULL)
5369 {
5370 struct ppc_dyn_relocs *p;
5371 struct ppc_dyn_relocs **head;
5372
5373 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5374 p = *head;
5375 if (p == NULL || p->sec != sec)
5376 {
5377 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5378 if (p == NULL)
5379 return false;
5380 p->next = *head;
5381 *head = p;
5382 p->sec = sec;
5383 p->count = 0;
5384 p->pc_count = 0;
5385 p->rel_count = 0;
5386 }
5387 p->count += 1;
5388 if (!must_be_dyn_reloc (info, r_type))
5389 p->pc_count += 1;
5390 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5391 && rel->r_offset % 2 == 0
5392 && sec->alignment_power != 0)
5393 p->rel_count += 1;
5394 }
5395 else
5396 {
5397 /* Track dynamic relocs needed for local syms too.
5398 We really need local syms available to do this
5399 easily. Oh well. */
5400 struct ppc_local_dyn_relocs *p;
5401 struct ppc_local_dyn_relocs **head;
5402 bool is_ifunc;
5403 asection *s;
5404 void *vpp;
5405 Elf_Internal_Sym *isym;
5406
5407 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5408 abfd, r_symndx);
5409 if (isym == NULL)
5410 return false;
5411
5412 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5413 if (s == NULL)
5414 s = sec;
5415
5416 vpp = &elf_section_data (s)->local_dynrel;
5417 head = (struct ppc_local_dyn_relocs **) vpp;
5418 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5419 p = *head;
5420 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5421 p = p->next;
5422 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5423 {
5424 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5425 if (p == NULL)
5426 return false;
5427 p->next = *head;
5428 *head = p;
5429 p->sec = sec;
5430 p->count = 0;
5431 p->rel_count = 0;
5432 p->ifunc = is_ifunc;
5433 }
5434 p->count += 1;
5435 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5436 && rel->r_offset % 2 == 0
5437 && sec->alignment_power != 0)
5438 p->rel_count += 1;
5439 }
5440 }
5441 break;
5442
5443 default:
5444 break;
5445 }
5446 }
5447
5448 return true;
5449 }
5450
5451 /* Merge backend specific data from an object file to the output
5452 object file when linking. */
5453
5454 static bool
5455 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5456 {
5457 bfd *obfd = info->output_bfd;
5458 unsigned long iflags, oflags;
5459
5460 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5461 return true;
5462
5463 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5464 return true;
5465
5466 if (!_bfd_generic_verify_endian_match (ibfd, info))
5467 return false;
5468
5469 iflags = elf_elfheader (ibfd)->e_flags;
5470 oflags = elf_elfheader (obfd)->e_flags;
5471
5472 if (iflags & ~EF_PPC64_ABI)
5473 {
5474 _bfd_error_handler
5475 /* xgettext:c-format */
5476 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5477 bfd_set_error (bfd_error_bad_value);
5478 return false;
5479 }
5480 else if (iflags != oflags && iflags != 0)
5481 {
5482 _bfd_error_handler
5483 /* xgettext:c-format */
5484 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5485 ibfd, iflags, oflags);
5486 bfd_set_error (bfd_error_bad_value);
5487 return false;
5488 }
5489
5490 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5491 return false;
5492
5493 /* Merge Tag_compatibility attributes and any common GNU ones. */
5494 return _bfd_elf_merge_object_attributes (ibfd, info);
5495 }
5496
5497 static bool
5498 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5499 {
5500 /* Print normal ELF private data. */
5501 _bfd_elf_print_private_bfd_data (abfd, ptr);
5502
5503 if (elf_elfheader (abfd)->e_flags != 0)
5504 {
5505 FILE *file = ptr;
5506
5507 fprintf (file, _("private flags = 0x%lx:"),
5508 elf_elfheader (abfd)->e_flags);
5509
5510 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5511 fprintf (file, _(" [abiv%ld]"),
5512 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5513 fputc ('\n', file);
5514 }
5515
5516 return true;
5517 }
5518
5519 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5520 of the code entry point, and its section, which must be in the same
5521 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5522
5523 static bfd_vma
5524 opd_entry_value (asection *opd_sec,
5525 bfd_vma offset,
5526 asection **code_sec,
5527 bfd_vma *code_off,
5528 bool in_code_sec)
5529 {
5530 bfd *opd_bfd = opd_sec->owner;
5531 Elf_Internal_Rela *relocs;
5532 Elf_Internal_Rela *lo, *hi, *look;
5533 bfd_vma val;
5534
5535 /* No relocs implies we are linking a --just-symbols object, or looking
5536 at a final linked executable with addr2line or somesuch. */
5537 if (opd_sec->reloc_count == 0)
5538 {
5539 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5540
5541 if (contents == NULL)
5542 {
5543 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5544 return (bfd_vma) -1;
5545 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5546 }
5547
5548 /* PR 17512: file: 64b9dfbb. */
5549 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5550 return (bfd_vma) -1;
5551
5552 val = bfd_get_64 (opd_bfd, contents + offset);
5553 if (code_sec != NULL)
5554 {
5555 asection *sec, *likely = NULL;
5556
5557 if (in_code_sec)
5558 {
5559 sec = *code_sec;
5560 if (sec->vma <= val
5561 && val < sec->vma + sec->size)
5562 likely = sec;
5563 else
5564 val = -1;
5565 }
5566 else
5567 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5568 if (sec->vma <= val
5569 && (sec->flags & SEC_LOAD) != 0
5570 && (sec->flags & SEC_ALLOC) != 0)
5571 likely = sec;
5572 if (likely != NULL)
5573 {
5574 *code_sec = likely;
5575 if (code_off != NULL)
5576 *code_off = val - likely->vma;
5577 }
5578 }
5579 return val;
5580 }
5581
5582 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5583
5584 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5585 if (relocs == NULL)
5586 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5587 /* PR 17512: file: df8e1fd6. */
5588 if (relocs == NULL)
5589 return (bfd_vma) -1;
5590
5591 /* Go find the opd reloc at the sym address. */
5592 lo = relocs;
5593 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5594 val = (bfd_vma) -1;
5595 while (lo < hi)
5596 {
5597 look = lo + (hi - lo) / 2;
5598 if (look->r_offset < offset)
5599 lo = look + 1;
5600 else if (look->r_offset > offset)
5601 hi = look;
5602 else
5603 {
5604 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5605
5606 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5607 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5608 {
5609 unsigned long symndx = ELF64_R_SYM (look->r_info);
5610 asection *sec = NULL;
5611
5612 if (symndx >= symtab_hdr->sh_info
5613 && elf_sym_hashes (opd_bfd) != NULL)
5614 {
5615 struct elf_link_hash_entry **sym_hashes;
5616 struct elf_link_hash_entry *rh;
5617
5618 sym_hashes = elf_sym_hashes (opd_bfd);
5619 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5620 if (rh != NULL)
5621 {
5622 rh = elf_follow_link (rh);
5623 if (rh->root.type != bfd_link_hash_defined
5624 && rh->root.type != bfd_link_hash_defweak)
5625 break;
5626 if (rh->root.u.def.section->owner == opd_bfd)
5627 {
5628 val = rh->root.u.def.value;
5629 sec = rh->root.u.def.section;
5630 }
5631 }
5632 }
5633
5634 if (sec == NULL)
5635 {
5636 Elf_Internal_Sym *sym;
5637
5638 if (symndx < symtab_hdr->sh_info)
5639 {
5640 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5641 if (sym == NULL)
5642 {
5643 size_t symcnt = symtab_hdr->sh_info;
5644 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5645 symcnt, 0,
5646 NULL, NULL, NULL);
5647 if (sym == NULL)
5648 break;
5649 symtab_hdr->contents = (bfd_byte *) sym;
5650 }
5651 sym += symndx;
5652 }
5653 else
5654 {
5655 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5656 1, symndx,
5657 NULL, NULL, NULL);
5658 if (sym == NULL)
5659 break;
5660 }
5661 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5662 if (sec == NULL)
5663 break;
5664 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5665 val = sym->st_value;
5666 }
5667
5668 val += look->r_addend;
5669 if (code_off != NULL)
5670 *code_off = val;
5671 if (code_sec != NULL)
5672 {
5673 if (in_code_sec && *code_sec != sec)
5674 return -1;
5675 else
5676 *code_sec = sec;
5677 }
5678 if (sec->output_section != NULL)
5679 val += sec->output_section->vma + sec->output_offset;
5680 }
5681 break;
5682 }
5683 }
5684
5685 return val;
5686 }
5687
5688 /* If the ELF symbol SYM might be a function in SEC, return the
5689 function size and set *CODE_OFF to the function's entry point,
5690 otherwise return zero. */
5691
5692 static bfd_size_type
5693 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5694 bfd_vma *code_off)
5695 {
5696 bfd_size_type size;
5697 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5698
5699 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5700 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5701 return 0;
5702
5703 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5704
5705 /* In theory we should check that the symbol's type satisfies
5706 _bfd_elf_is_function_type(), but there are some function-like
5707 symbols which would fail this test. (eg _start). Instead
5708 we check for hidden, local, notype symbols with zero size.
5709 This type of symbol is generated by the annobin plugin for gcc
5710 and clang, and should not be considered to be a function symbol. */
5711 if (size == 0
5712 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5713 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5714 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5715 return 0;
5716
5717 if (strcmp (sym->section->name, ".opd") == 0)
5718 {
5719 struct _opd_sec_data *opd = get_opd_info (sym->section);
5720 bfd_vma symval = sym->value;
5721
5722 if (opd != NULL
5723 && opd->adjust != NULL
5724 && elf_section_data (sym->section)->relocs != NULL)
5725 {
5726 /* opd_entry_value will use cached relocs that have been
5727 adjusted, but with raw symbols. That means both local
5728 and global symbols need adjusting. */
5729 long adjust = opd->adjust[OPD_NDX (symval)];
5730 if (adjust == -1)
5731 return 0;
5732 symval += adjust;
5733 }
5734
5735 if (opd_entry_value (sym->section, symval,
5736 &sec, code_off, true) == (bfd_vma) -1)
5737 return 0;
5738 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5739 symbol. This size has nothing to do with the code size of the
5740 function, which is what we're supposed to return, but the
5741 code size isn't available without looking up the dot-sym.
5742 However, doing that would be a waste of time particularly
5743 since elf_find_function will look at the dot-sym anyway.
5744 Now, elf_find_function will keep the largest size of any
5745 function sym found at the code address of interest, so return
5746 1 here to avoid it incorrectly caching a larger function size
5747 for a small function. This does mean we return the wrong
5748 size for a new-ABI function of size 24, but all that does is
5749 disable caching for such functions. */
5750 if (size == 24)
5751 size = 1;
5752 }
5753 else
5754 {
5755 if (sym->section != sec)
5756 return 0;
5757 *code_off = sym->value;
5758 }
5759
5760 /* Do not return 0 for the function's size. */
5761 return size ? size : 1;
5762 }
5763
5764 /* Return true if symbol is a strong function defined in an ELFv2
5765 object with st_other localentry bits of zero, ie. its local entry
5766 point coincides with its global entry point. */
5767
5768 static bool
5769 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5770 {
5771 return (h != NULL
5772 && h->type == STT_FUNC
5773 && h->root.type == bfd_link_hash_defined
5774 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5775 && !ppc_elf_hash_entry (h)->non_zero_localentry
5776 && is_ppc64_elf (h->root.u.def.section->owner)
5777 && abiversion (h->root.u.def.section->owner) >= 2);
5778 }
5779
5780 /* Return true if symbol is defined in a regular object file. */
5781
5782 static bool
5783 is_static_defined (struct elf_link_hash_entry *h)
5784 {
5785 return ((h->root.type == bfd_link_hash_defined
5786 || h->root.type == bfd_link_hash_defweak)
5787 && h->root.u.def.section != NULL
5788 && h->root.u.def.section->output_section != NULL);
5789 }
5790
5791 /* If FDH is a function descriptor symbol, return the associated code
5792 entry symbol if it is defined. Return NULL otherwise. */
5793
5794 static struct ppc_link_hash_entry *
5795 defined_code_entry (struct ppc_link_hash_entry *fdh)
5796 {
5797 if (fdh->is_func_descriptor)
5798 {
5799 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5800 if (fh->elf.root.type == bfd_link_hash_defined
5801 || fh->elf.root.type == bfd_link_hash_defweak)
5802 return fh;
5803 }
5804 return NULL;
5805 }
5806
5807 /* If FH is a function code entry symbol, return the associated
5808 function descriptor symbol if it is defined. Return NULL otherwise. */
5809
5810 static struct ppc_link_hash_entry *
5811 defined_func_desc (struct ppc_link_hash_entry *fh)
5812 {
5813 if (fh->oh != NULL
5814 && fh->oh->is_func_descriptor)
5815 {
5816 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5817 if (fdh->elf.root.type == bfd_link_hash_defined
5818 || fdh->elf.root.type == bfd_link_hash_defweak)
5819 return fdh;
5820 }
5821 return NULL;
5822 }
5823
5824 /* Given H is a symbol that satisfies is_static_defined, return the
5825 value in the output file. */
5826
5827 static bfd_vma
5828 defined_sym_val (struct elf_link_hash_entry *h)
5829 {
5830 return (h->root.u.def.section->output_section->vma
5831 + h->root.u.def.section->output_offset
5832 + h->root.u.def.value);
5833 }
5834
5835 /* Return true if H matches __tls_get_addr or one of its variants. */
5836
5837 static bool
5838 is_tls_get_addr (struct elf_link_hash_entry *h,
5839 struct ppc_link_hash_table *htab)
5840 {
5841 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5842 || h == elf_hash_entry (htab->tga_desc_fd)
5843 || h == elf_hash_entry (htab->tls_get_addr)
5844 || h == elf_hash_entry (htab->tga_desc));
5845 }
5846
5847 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5848
5849 /* Garbage collect sections, after first dealing with dot-symbols. */
5850
5851 static bool
5852 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5853 {
5854 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5855
5856 if (htab != NULL && htab->need_func_desc_adj)
5857 {
5858 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5859 htab->need_func_desc_adj = 0;
5860 }
5861 return bfd_elf_gc_sections (abfd, info);
5862 }
5863
5864 /* Mark all our entry sym sections, both opd and code section. */
5865
5866 static void
5867 ppc64_elf_gc_keep (struct bfd_link_info *info)
5868 {
5869 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5870 struct bfd_sym_chain *sym;
5871
5872 if (htab == NULL)
5873 return;
5874
5875 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5876 {
5877 struct ppc_link_hash_entry *eh, *fh;
5878 asection *sec;
5879
5880 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5881 false, false, true));
5882 if (eh == NULL)
5883 continue;
5884 if (eh->elf.root.type != bfd_link_hash_defined
5885 && eh->elf.root.type != bfd_link_hash_defweak)
5886 continue;
5887
5888 fh = defined_code_entry (eh);
5889 if (fh != NULL)
5890 {
5891 sec = fh->elf.root.u.def.section;
5892 sec->flags |= SEC_KEEP;
5893 }
5894 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5895 && opd_entry_value (eh->elf.root.u.def.section,
5896 eh->elf.root.u.def.value,
5897 &sec, NULL, false) != (bfd_vma) -1)
5898 sec->flags |= SEC_KEEP;
5899
5900 sec = eh->elf.root.u.def.section;
5901 sec->flags |= SEC_KEEP;
5902 }
5903 }
5904
5905 /* Mark sections containing dynamically referenced symbols. When
5906 building shared libraries, we must assume that any visible symbol is
5907 referenced. */
5908
5909 static bool
5910 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5911 {
5912 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5913 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5914 struct ppc_link_hash_entry *fdh;
5915 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5916
5917 /* Dynamic linking info is on the func descriptor sym. */
5918 fdh = defined_func_desc (eh);
5919 if (fdh != NULL)
5920 eh = fdh;
5921
5922 if ((eh->elf.root.type == bfd_link_hash_defined
5923 || eh->elf.root.type == bfd_link_hash_defweak)
5924 && (!eh->elf.start_stop
5925 || eh->elf.root.ldscript_def
5926 || !info->start_stop_gc)
5927 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5928 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5929 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5930 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5931 && (!bfd_link_executable (info)
5932 || info->gc_keep_exported
5933 || info->export_dynamic
5934 || (eh->elf.dynamic
5935 && d != NULL
5936 && (*d->match) (&d->head, NULL,
5937 eh->elf.root.root.string)))
5938 && (eh->elf.versioned >= versioned
5939 || !bfd_hide_sym_by_version (info->version_info,
5940 eh->elf.root.root.string)))))
5941 {
5942 asection *code_sec;
5943 struct ppc_link_hash_entry *fh;
5944
5945 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5946
5947 /* Function descriptor syms cause the associated
5948 function code sym section to be marked. */
5949 fh = defined_code_entry (eh);
5950 if (fh != NULL)
5951 {
5952 code_sec = fh->elf.root.u.def.section;
5953 code_sec->flags |= SEC_KEEP;
5954 }
5955 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5956 && opd_entry_value (eh->elf.root.u.def.section,
5957 eh->elf.root.u.def.value,
5958 &code_sec, NULL, false) != (bfd_vma) -1)
5959 code_sec->flags |= SEC_KEEP;
5960 }
5961
5962 return true;
5963 }
5964
5965 /* Return the section that should be marked against GC for a given
5966 relocation. */
5967
5968 static asection *
5969 ppc64_elf_gc_mark_hook (asection *sec,
5970 struct bfd_link_info *info,
5971 Elf_Internal_Rela *rel,
5972 struct elf_link_hash_entry *h,
5973 Elf_Internal_Sym *sym)
5974 {
5975 asection *rsec;
5976
5977 /* Syms return NULL if we're marking .opd, so we avoid marking all
5978 function sections, as all functions are referenced in .opd. */
5979 rsec = NULL;
5980 if (get_opd_info (sec) != NULL)
5981 return rsec;
5982
5983 if (h != NULL)
5984 {
5985 enum elf_ppc64_reloc_type r_type;
5986 struct ppc_link_hash_entry *eh, *fh, *fdh;
5987
5988 r_type = ELF64_R_TYPE (rel->r_info);
5989 switch (r_type)
5990 {
5991 case R_PPC64_GNU_VTINHERIT:
5992 case R_PPC64_GNU_VTENTRY:
5993 break;
5994
5995 default:
5996 switch (h->root.type)
5997 {
5998 case bfd_link_hash_defined:
5999 case bfd_link_hash_defweak:
6000 eh = ppc_elf_hash_entry (h);
6001 fdh = defined_func_desc (eh);
6002 if (fdh != NULL)
6003 {
6004 /* -mcall-aixdesc code references the dot-symbol on
6005 a call reloc. Mark the function descriptor too
6006 against garbage collection. */
6007 fdh->elf.mark = 1;
6008 if (fdh->elf.is_weakalias)
6009 weakdef (&fdh->elf)->mark = 1;
6010 eh = fdh;
6011 }
6012
6013 /* Function descriptor syms cause the associated
6014 function code sym section to be marked. */
6015 fh = defined_code_entry (eh);
6016 if (fh != NULL)
6017 {
6018 /* They also mark their opd section. */
6019 eh->elf.root.u.def.section->gc_mark = 1;
6020
6021 rsec = fh->elf.root.u.def.section;
6022 }
6023 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6024 && opd_entry_value (eh->elf.root.u.def.section,
6025 eh->elf.root.u.def.value,
6026 &rsec, NULL, false) != (bfd_vma) -1)
6027 eh->elf.root.u.def.section->gc_mark = 1;
6028 else
6029 rsec = h->root.u.def.section;
6030 break;
6031
6032 case bfd_link_hash_common:
6033 rsec = h->root.u.c.p->section;
6034 break;
6035
6036 default:
6037 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6038 }
6039 }
6040 }
6041 else
6042 {
6043 struct _opd_sec_data *opd;
6044
6045 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6046 opd = get_opd_info (rsec);
6047 if (opd != NULL && opd->func_sec != NULL)
6048 {
6049 rsec->gc_mark = 1;
6050
6051 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6052 }
6053 }
6054
6055 return rsec;
6056 }
6057
6058 /* The maximum size of .sfpr. */
6059 #define SFPR_MAX (218*4)
6060
6061 struct sfpr_def_parms
6062 {
6063 const char name[12];
6064 unsigned char lo, hi;
6065 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6066 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6067 };
6068
6069 /* Auto-generate _save*, _rest* functions in .sfpr.
6070 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6071 instead. */
6072
6073 static bool
6074 sfpr_define (struct bfd_link_info *info,
6075 const struct sfpr_def_parms *parm,
6076 asection *stub_sec)
6077 {
6078 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6079 unsigned int i;
6080 size_t len = strlen (parm->name);
6081 bool writing = false;
6082 char sym[16];
6083
6084 if (htab == NULL)
6085 return false;
6086
6087 memcpy (sym, parm->name, len);
6088 sym[len + 2] = 0;
6089
6090 for (i = parm->lo; i <= parm->hi; i++)
6091 {
6092 struct ppc_link_hash_entry *h;
6093
6094 sym[len + 0] = i / 10 + '0';
6095 sym[len + 1] = i % 10 + '0';
6096 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6097 writing, true, true));
6098 if (stub_sec != NULL)
6099 {
6100 if (h != NULL
6101 && h->elf.root.type == bfd_link_hash_defined
6102 && h->elf.root.u.def.section == htab->sfpr)
6103 {
6104 struct elf_link_hash_entry *s;
6105 char buf[32];
6106 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6107 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6108 if (s == NULL)
6109 return false;
6110 if (s->root.type == bfd_link_hash_new)
6111 {
6112 s->root.type = bfd_link_hash_defined;
6113 s->root.u.def.section = stub_sec;
6114 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6115 + h->elf.root.u.def.value);
6116 s->ref_regular = 1;
6117 s->def_regular = 1;
6118 s->ref_regular_nonweak = 1;
6119 s->forced_local = 1;
6120 s->non_elf = 0;
6121 s->root.linker_def = 1;
6122 }
6123 }
6124 continue;
6125 }
6126 if (h != NULL)
6127 {
6128 h->save_res = 1;
6129 if (!h->elf.def_regular)
6130 {
6131 h->elf.root.type = bfd_link_hash_defined;
6132 h->elf.root.u.def.section = htab->sfpr;
6133 h->elf.root.u.def.value = htab->sfpr->size;
6134 h->elf.type = STT_FUNC;
6135 h->elf.def_regular = 1;
6136 h->elf.non_elf = 0;
6137 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6138 writing = true;
6139 if (htab->sfpr->contents == NULL)
6140 {
6141 htab->sfpr->contents
6142 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6143 if (htab->sfpr->contents == NULL)
6144 return false;
6145 }
6146 }
6147 }
6148 if (writing)
6149 {
6150 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6151 if (i != parm->hi)
6152 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6153 else
6154 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6155 htab->sfpr->size = p - htab->sfpr->contents;
6156 }
6157 }
6158
6159 return true;
6160 }
6161
6162 static bfd_byte *
6163 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6164 {
6165 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6166 return p + 4;
6167 }
6168
6169 static bfd_byte *
6170 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6171 {
6172 p = savegpr0 (abfd, p, r);
6173 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6174 p = p + 4;
6175 bfd_put_32 (abfd, BLR, p);
6176 return p + 4;
6177 }
6178
6179 static bfd_byte *
6180 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6181 {
6182 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6183 return p + 4;
6184 }
6185
6186 static bfd_byte *
6187 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6188 {
6189 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6190 p = p + 4;
6191 p = restgpr0 (abfd, p, r);
6192 bfd_put_32 (abfd, MTLR_R0, p);
6193 p = p + 4;
6194 if (r == 29)
6195 {
6196 p = restgpr0 (abfd, p, 30);
6197 p = restgpr0 (abfd, p, 31);
6198 }
6199 bfd_put_32 (abfd, BLR, p);
6200 return p + 4;
6201 }
6202
6203 static bfd_byte *
6204 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6205 {
6206 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6207 return p + 4;
6208 }
6209
6210 static bfd_byte *
6211 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6212 {
6213 p = savegpr1 (abfd, p, r);
6214 bfd_put_32 (abfd, BLR, p);
6215 return p + 4;
6216 }
6217
6218 static bfd_byte *
6219 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6220 {
6221 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6222 return p + 4;
6223 }
6224
6225 static bfd_byte *
6226 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6227 {
6228 p = restgpr1 (abfd, p, r);
6229 bfd_put_32 (abfd, BLR, p);
6230 return p + 4;
6231 }
6232
6233 static bfd_byte *
6234 savefpr (bfd *abfd, bfd_byte *p, int r)
6235 {
6236 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6237 return p + 4;
6238 }
6239
6240 static bfd_byte *
6241 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6242 {
6243 p = savefpr (abfd, p, r);
6244 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6245 p = p + 4;
6246 bfd_put_32 (abfd, BLR, p);
6247 return p + 4;
6248 }
6249
6250 static bfd_byte *
6251 restfpr (bfd *abfd, bfd_byte *p, int r)
6252 {
6253 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6254 return p + 4;
6255 }
6256
6257 static bfd_byte *
6258 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6259 {
6260 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6261 p = p + 4;
6262 p = restfpr (abfd, p, r);
6263 bfd_put_32 (abfd, MTLR_R0, p);
6264 p = p + 4;
6265 if (r == 29)
6266 {
6267 p = restfpr (abfd, p, 30);
6268 p = restfpr (abfd, p, 31);
6269 }
6270 bfd_put_32 (abfd, BLR, p);
6271 return p + 4;
6272 }
6273
6274 static bfd_byte *
6275 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6276 {
6277 p = savefpr (abfd, p, r);
6278 bfd_put_32 (abfd, BLR, p);
6279 return p + 4;
6280 }
6281
6282 static bfd_byte *
6283 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6284 {
6285 p = restfpr (abfd, p, r);
6286 bfd_put_32 (abfd, BLR, p);
6287 return p + 4;
6288 }
6289
6290 static bfd_byte *
6291 savevr (bfd *abfd, bfd_byte *p, int r)
6292 {
6293 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6294 p = p + 4;
6295 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6296 return p + 4;
6297 }
6298
6299 static bfd_byte *
6300 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6301 {
6302 p = savevr (abfd, p, r);
6303 bfd_put_32 (abfd, BLR, p);
6304 return p + 4;
6305 }
6306
6307 static bfd_byte *
6308 restvr (bfd *abfd, bfd_byte *p, int r)
6309 {
6310 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6311 p = p + 4;
6312 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6313 return p + 4;
6314 }
6315
6316 static bfd_byte *
6317 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6318 {
6319 p = restvr (abfd, p, r);
6320 bfd_put_32 (abfd, BLR, p);
6321 return p + 4;
6322 }
6323
6324 #define STDU_R1_0R1 0xf8210001
6325 #define ADDI_R1_R1 0x38210000
6326
6327 /* Emit prologue of wrapper preserving regs around a call to
6328 __tls_get_addr_opt. */
6329
6330 static bfd_byte *
6331 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6332 {
6333 unsigned int i;
6334
6335 bfd_put_32 (obfd, MFLR_R0, p);
6336 p += 4;
6337 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6338 p += 4;
6339
6340 if (htab->opd_abi)
6341 {
6342 for (i = 4; i < 12; i++)
6343 {
6344 bfd_put_32 (obfd,
6345 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6346 p += 4;
6347 }
6348 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6349 p += 4;
6350 }
6351 else
6352 {
6353 for (i = 4; i < 12; i++)
6354 {
6355 bfd_put_32 (obfd,
6356 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6357 p += 4;
6358 }
6359 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6360 p += 4;
6361 }
6362 return p;
6363 }
6364
6365 /* Emit epilogue of wrapper preserving regs around a call to
6366 __tls_get_addr_opt. */
6367
6368 static bfd_byte *
6369 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6370 {
6371 unsigned int i;
6372
6373 if (htab->opd_abi)
6374 {
6375 for (i = 4; i < 12; i++)
6376 {
6377 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6378 p += 4;
6379 }
6380 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6381 p += 4;
6382 }
6383 else
6384 {
6385 for (i = 4; i < 12; i++)
6386 {
6387 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6388 p += 4;
6389 }
6390 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6391 p += 4;
6392 }
6393 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6394 p += 4;
6395 bfd_put_32 (obfd, MTLR_R0, p);
6396 p += 4;
6397 bfd_put_32 (obfd, BLR, p);
6398 p += 4;
6399 return p;
6400 }
6401
6402 /* Called via elf_link_hash_traverse to transfer dynamic linking
6403 information on function code symbol entries to their corresponding
6404 function descriptor symbol entries. Must not be called twice for
6405 any given code symbol. */
6406
6407 static bool
6408 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6409 {
6410 struct bfd_link_info *info;
6411 struct ppc_link_hash_table *htab;
6412 struct ppc_link_hash_entry *fh;
6413 struct ppc_link_hash_entry *fdh;
6414 bool force_local;
6415
6416 fh = ppc_elf_hash_entry (h);
6417 if (fh->elf.root.type == bfd_link_hash_indirect)
6418 return true;
6419
6420 if (!fh->is_func)
6421 return true;
6422
6423 if (fh->elf.root.root.string[0] != '.'
6424 || fh->elf.root.root.string[1] == '\0')
6425 return true;
6426
6427 info = inf;
6428 htab = ppc_hash_table (info);
6429 if (htab == NULL)
6430 return false;
6431
6432 /* Find the corresponding function descriptor symbol. */
6433 fdh = lookup_fdh (fh, htab);
6434
6435 /* Resolve undefined references to dot-symbols as the value
6436 in the function descriptor, if we have one in a regular object.
6437 This is to satisfy cases like ".quad .foo". Calls to functions
6438 in dynamic objects are handled elsewhere. */
6439 if ((fh->elf.root.type == bfd_link_hash_undefined
6440 || fh->elf.root.type == bfd_link_hash_undefweak)
6441 && (fdh->elf.root.type == bfd_link_hash_defined
6442 || fdh->elf.root.type == bfd_link_hash_defweak)
6443 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6444 && opd_entry_value (fdh->elf.root.u.def.section,
6445 fdh->elf.root.u.def.value,
6446 &fh->elf.root.u.def.section,
6447 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6448 {
6449 fh->elf.root.type = fdh->elf.root.type;
6450 fh->elf.forced_local = 1;
6451 fh->elf.def_regular = fdh->elf.def_regular;
6452 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6453 }
6454
6455 if (!fh->elf.dynamic)
6456 {
6457 struct plt_entry *ent;
6458
6459 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6460 if (ent->plt.refcount > 0)
6461 break;
6462 if (ent == NULL)
6463 {
6464 if (fdh != NULL && fdh->fake)
6465 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6466 return true;
6467 }
6468 }
6469
6470 /* Create a descriptor as undefined if necessary. */
6471 if (fdh == NULL
6472 && !bfd_link_executable (info)
6473 && (fh->elf.root.type == bfd_link_hash_undefined
6474 || fh->elf.root.type == bfd_link_hash_undefweak))
6475 {
6476 fdh = make_fdh (info, fh);
6477 if (fdh == NULL)
6478 return false;
6479 }
6480
6481 /* We can't support overriding of symbols on a fake descriptor. */
6482 if (fdh != NULL
6483 && fdh->fake
6484 && (fh->elf.root.type == bfd_link_hash_defined
6485 || fh->elf.root.type == bfd_link_hash_defweak))
6486 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6487
6488 /* Transfer dynamic linking information to the function descriptor. */
6489 if (fdh != NULL)
6490 {
6491 fdh->elf.ref_regular |= fh->elf.ref_regular;
6492 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6493 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6494 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6495 fdh->elf.dynamic |= fh->elf.dynamic;
6496 fdh->elf.needs_plt |= (fh->elf.needs_plt
6497 || fh->elf.type == STT_FUNC
6498 || fh->elf.type == STT_GNU_IFUNC);
6499 move_plt_plist (fh, fdh);
6500
6501 if (!fdh->elf.forced_local
6502 && fh->elf.dynindx != -1)
6503 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6504 return false;
6505 }
6506
6507 /* Now that the info is on the function descriptor, clear the
6508 function code sym info. Any function code syms for which we
6509 don't have a definition in a regular file, we force local.
6510 This prevents a shared library from exporting syms that have
6511 been imported from another library. Function code syms that
6512 are really in the library we must leave global to prevent the
6513 linker dragging in a definition from a static library. */
6514 force_local = (!fh->elf.def_regular
6515 || fdh == NULL
6516 || !fdh->elf.def_regular
6517 || fdh->elf.forced_local);
6518 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6519
6520 return true;
6521 }
6522
6523 static const struct sfpr_def_parms save_res_funcs[] =
6524 {
6525 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6526 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6527 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6528 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6529 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6530 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6531 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6532 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6533 { "._savef", 14, 31, savefpr, savefpr1_tail },
6534 { "._restf", 14, 31, restfpr, restfpr1_tail },
6535 { "_savevr_", 20, 31, savevr, savevr_tail },
6536 { "_restvr_", 20, 31, restvr, restvr_tail }
6537 };
6538
6539 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6540 this hook to a) run the edit functions in this file, b) provide
6541 some gcc support functions, and c) transfer dynamic linking
6542 information gathered so far on function code symbol entries, to
6543 their corresponding function descriptor symbol entries. */
6544
6545 static bool
6546 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6547 {
6548 struct ppc_link_hash_table *htab;
6549
6550 htab = ppc_hash_table (info);
6551 if (htab == NULL)
6552 return false;
6553
6554 /* Call back into the linker, which then runs the edit functions. */
6555 htab->params->edit ();
6556
6557 /* Provide any missing _save* and _rest* functions. */
6558 if (htab->sfpr != NULL)
6559 {
6560 unsigned int i;
6561
6562 htab->sfpr->size = 0;
6563 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6564 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6565 return false;
6566 if (htab->sfpr->size == 0)
6567 htab->sfpr->flags |= SEC_EXCLUDE;
6568 }
6569
6570 if (bfd_link_relocatable (info))
6571 return true;
6572
6573 if (htab->elf.hgot != NULL)
6574 {
6575 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6576 /* Make .TOC. defined so as to prevent it being made dynamic.
6577 The wrong value here is fixed later in ppc64_elf_set_toc. */
6578 if (!htab->elf.hgot->def_regular
6579 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6580 {
6581 htab->elf.hgot->root.type = bfd_link_hash_defined;
6582 htab->elf.hgot->root.u.def.value = 0;
6583 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6584 htab->elf.hgot->def_regular = 1;
6585 htab->elf.hgot->root.linker_def = 1;
6586 }
6587 htab->elf.hgot->type = STT_OBJECT;
6588 htab->elf.hgot->other
6589 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6590 }
6591
6592 return true;
6593 }
6594
6595 /* Return true if we have dynamic relocs against H or any of its weak
6596 aliases, that apply to read-only sections. Cannot be used after
6597 size_dynamic_sections. */
6598
6599 static bool
6600 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6601 {
6602 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6603 do
6604 {
6605 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6606 return true;
6607 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6608 }
6609 while (eh != NULL && &eh->elf != h);
6610
6611 return false;
6612 }
6613
6614 /* Return whether EH has pc-relative dynamic relocs. */
6615
6616 static bool
6617 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6618 {
6619 struct ppc_dyn_relocs *p;
6620
6621 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6622 if (p->pc_count != 0)
6623 return true;
6624 return false;
6625 }
6626
6627 /* Return true if a global entry stub will be created for H. Valid
6628 for ELFv2 before plt entries have been allocated. */
6629
6630 static bool
6631 global_entry_stub (struct elf_link_hash_entry *h)
6632 {
6633 struct plt_entry *pent;
6634
6635 if (!h->pointer_equality_needed
6636 || h->def_regular)
6637 return false;
6638
6639 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6640 if (pent->plt.refcount > 0
6641 && pent->addend == 0)
6642 return true;
6643
6644 return false;
6645 }
6646
6647 /* Adjust a symbol defined by a dynamic object and referenced by a
6648 regular object. The current definition is in some section of the
6649 dynamic object, but we're not including those sections. We have to
6650 change the definition to something the rest of the link can
6651 understand. */
6652
6653 static bool
6654 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6655 struct elf_link_hash_entry *h)
6656 {
6657 struct ppc_link_hash_table *htab;
6658 asection *s, *srel;
6659
6660 htab = ppc_hash_table (info);
6661 if (htab == NULL)
6662 return false;
6663
6664 /* Deal with function syms. */
6665 if (h->type == STT_FUNC
6666 || h->type == STT_GNU_IFUNC
6667 || h->needs_plt)
6668 {
6669 bool local = (ppc_elf_hash_entry (h)->save_res
6670 || SYMBOL_CALLS_LOCAL (info, h)
6671 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6672 /* Discard dyn_relocs when non-pic if we've decided that a
6673 function symbol is local and not an ifunc. We keep dynamic
6674 relocs for ifuncs when local rather than always emitting a
6675 plt call stub for them and defining the symbol on the call
6676 stub. We can't do that for ELFv1 anyway (a function symbol
6677 is defined on a descriptor, not code) and it can be faster at
6678 run-time due to not needing to bounce through a stub. The
6679 dyn_relocs for ifuncs will be applied even in a static
6680 executable. */
6681 if (!bfd_link_pic (info)
6682 && h->type != STT_GNU_IFUNC
6683 && local)
6684 h->dyn_relocs = NULL;
6685
6686 /* Clear procedure linkage table information for any symbol that
6687 won't need a .plt entry. */
6688 struct plt_entry *ent;
6689 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6690 if (ent->plt.refcount > 0)
6691 break;
6692 if (ent == NULL
6693 || (h->type != STT_GNU_IFUNC
6694 && local
6695 && (htab->can_convert_all_inline_plt
6696 || (ppc_elf_hash_entry (h)->tls_mask
6697 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6698 {
6699 h->plt.plist = NULL;
6700 h->needs_plt = 0;
6701 h->pointer_equality_needed = 0;
6702 }
6703 else if (abiversion (info->output_bfd) >= 2)
6704 {
6705 /* Taking a function's address in a read/write section
6706 doesn't require us to define the function symbol in the
6707 executable on a global entry stub. A dynamic reloc can
6708 be used instead. The reason we prefer a few more dynamic
6709 relocs is that calling via a global entry stub costs a
6710 few more instructions, and pointer_equality_needed causes
6711 extra work in ld.so when resolving these symbols. */
6712 if (global_entry_stub (h))
6713 {
6714 if (!_bfd_elf_readonly_dynrelocs (h))
6715 {
6716 h->pointer_equality_needed = 0;
6717 /* If we haven't seen a branch reloc and the symbol
6718 isn't an ifunc then we don't need a plt entry. */
6719 if (!h->needs_plt)
6720 h->plt.plist = NULL;
6721 }
6722 else if (!bfd_link_pic (info))
6723 /* We are going to be defining the function symbol on the
6724 plt stub, so no dyn_relocs needed when non-pic. */
6725 h->dyn_relocs = NULL;
6726 }
6727
6728 /* ELFv2 function symbols can't have copy relocs. */
6729 return true;
6730 }
6731 else if (!h->needs_plt
6732 && !_bfd_elf_readonly_dynrelocs (h))
6733 {
6734 /* If we haven't seen a branch reloc and the symbol isn't an
6735 ifunc then we don't need a plt entry. */
6736 h->plt.plist = NULL;
6737 h->pointer_equality_needed = 0;
6738 return true;
6739 }
6740 }
6741 else
6742 h->plt.plist = NULL;
6743
6744 /* If this is a weak symbol, and there is a real definition, the
6745 processor independent code will have arranged for us to see the
6746 real definition first, and we can just use the same value. */
6747 if (h->is_weakalias)
6748 {
6749 struct elf_link_hash_entry *def = weakdef (h);
6750 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6751 h->root.u.def.section = def->root.u.def.section;
6752 h->root.u.def.value = def->root.u.def.value;
6753 if (def->root.u.def.section == htab->elf.sdynbss
6754 || def->root.u.def.section == htab->elf.sdynrelro)
6755 h->dyn_relocs = NULL;
6756 return true;
6757 }
6758
6759 /* If we are creating a shared library, we must presume that the
6760 only references to the symbol are via the global offset table.
6761 For such cases we need not do anything here; the relocations will
6762 be handled correctly by relocate_section. */
6763 if (!bfd_link_executable (info))
6764 return true;
6765
6766 /* If there are no references to this symbol that do not use the
6767 GOT, we don't need to generate a copy reloc. */
6768 if (!h->non_got_ref)
6769 return true;
6770
6771 /* Don't generate a copy reloc for symbols defined in the executable. */
6772 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6773
6774 /* If -z nocopyreloc was given, don't generate them either. */
6775 || info->nocopyreloc
6776
6777 /* If we don't find any dynamic relocs in read-only sections, then
6778 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6779 || (ELIMINATE_COPY_RELOCS
6780 && !h->needs_copy
6781 && !alias_readonly_dynrelocs (h))
6782
6783 /* Protected variables do not work with .dynbss. The copy in
6784 .dynbss won't be used by the shared library with the protected
6785 definition for the variable. Text relocations are preferable
6786 to an incorrect program. */
6787 || h->protected_def)
6788 return true;
6789
6790 if (h->type == STT_FUNC
6791 || h->type == STT_GNU_IFUNC)
6792 {
6793 /* .dynbss copies of function symbols only work if we have
6794 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6795 use dot-symbols and set the function symbol size to the text
6796 size of the function rather than the size of the descriptor.
6797 That's wrong for copying a descriptor. */
6798 if (ppc_elf_hash_entry (h)->oh == NULL
6799 || !(h->size == 24 || h->size == 16))
6800 return true;
6801
6802 /* We should never get here, but unfortunately there are old
6803 versions of gcc (circa gcc-3.2) that improperly for the
6804 ELFv1 ABI put initialized function pointers, vtable refs and
6805 suchlike in read-only sections. Allow them to proceed, but
6806 warn that this might break at runtime. */
6807 info->callbacks->einfo
6808 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6809 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6810 h->root.root.string);
6811 }
6812
6813 /* This is a reference to a symbol defined by a dynamic object which
6814 is not a function. */
6815
6816 /* We must allocate the symbol in our .dynbss section, which will
6817 become part of the .bss section of the executable. There will be
6818 an entry for this symbol in the .dynsym section. The dynamic
6819 object will contain position independent code, so all references
6820 from the dynamic object to this symbol will go through the global
6821 offset table. The dynamic linker will use the .dynsym entry to
6822 determine the address it must put in the global offset table, so
6823 both the dynamic object and the regular object will refer to the
6824 same memory location for the variable. */
6825 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6826 {
6827 s = htab->elf.sdynrelro;
6828 srel = htab->elf.sreldynrelro;
6829 }
6830 else
6831 {
6832 s = htab->elf.sdynbss;
6833 srel = htab->elf.srelbss;
6834 }
6835 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6836 {
6837 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6838 linker to copy the initial value out of the dynamic object
6839 and into the runtime process image. */
6840 srel->size += sizeof (Elf64_External_Rela);
6841 h->needs_copy = 1;
6842 }
6843
6844 /* We no longer want dyn_relocs. */
6845 h->dyn_relocs = NULL;
6846 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6847 }
6848
6849 /* If given a function descriptor symbol, hide both the function code
6850 sym and the descriptor. */
6851 static void
6852 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6853 struct elf_link_hash_entry *h,
6854 bool force_local)
6855 {
6856 struct ppc_link_hash_entry *eh;
6857 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6858
6859 if (ppc_hash_table (info) == NULL)
6860 return;
6861
6862 eh = ppc_elf_hash_entry (h);
6863 if (eh->is_func_descriptor)
6864 {
6865 struct ppc_link_hash_entry *fh = eh->oh;
6866
6867 if (fh == NULL)
6868 {
6869 const char *p, *q;
6870 struct elf_link_hash_table *htab = elf_hash_table (info);
6871 char save;
6872
6873 /* We aren't supposed to use alloca in BFD because on
6874 systems which do not have alloca the version in libiberty
6875 calls xmalloc, which might cause the program to crash
6876 when it runs out of memory. This function doesn't have a
6877 return status, so there's no way to gracefully return an
6878 error. So cheat. We know that string[-1] can be safely
6879 accessed; It's either a string in an ELF string table,
6880 or allocated in an objalloc structure. */
6881
6882 p = eh->elf.root.root.string - 1;
6883 save = *p;
6884 *(char *) p = '.';
6885 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6886 false, false));
6887 *(char *) p = save;
6888
6889 /* Unfortunately, if it so happens that the string we were
6890 looking for was allocated immediately before this string,
6891 then we overwrote the string terminator. That's the only
6892 reason the lookup should fail. */
6893 if (fh == NULL)
6894 {
6895 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6896 while (q >= eh->elf.root.root.string && *q == *p)
6897 --q, --p;
6898 if (q < eh->elf.root.root.string && *p == '.')
6899 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6900 false, false));
6901 }
6902 if (fh != NULL)
6903 {
6904 eh->oh = fh;
6905 fh->oh = eh;
6906 }
6907 }
6908 if (fh != NULL)
6909 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6910 }
6911 }
6912
6913 static bool
6914 get_sym_h (struct elf_link_hash_entry **hp,
6915 Elf_Internal_Sym **symp,
6916 asection **symsecp,
6917 unsigned char **tls_maskp,
6918 Elf_Internal_Sym **locsymsp,
6919 unsigned long r_symndx,
6920 bfd *ibfd)
6921 {
6922 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6923
6924 if (r_symndx >= symtab_hdr->sh_info)
6925 {
6926 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6927 struct elf_link_hash_entry *h;
6928
6929 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6930 h = elf_follow_link (h);
6931
6932 if (hp != NULL)
6933 *hp = h;
6934
6935 if (symp != NULL)
6936 *symp = NULL;
6937
6938 if (symsecp != NULL)
6939 {
6940 asection *symsec = NULL;
6941 if (h->root.type == bfd_link_hash_defined
6942 || h->root.type == bfd_link_hash_defweak)
6943 symsec = h->root.u.def.section;
6944 *symsecp = symsec;
6945 }
6946
6947 if (tls_maskp != NULL)
6948 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6949 }
6950 else
6951 {
6952 Elf_Internal_Sym *sym;
6953 Elf_Internal_Sym *locsyms = *locsymsp;
6954
6955 if (locsyms == NULL)
6956 {
6957 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6958 if (locsyms == NULL)
6959 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6960 symtab_hdr->sh_info,
6961 0, NULL, NULL, NULL);
6962 if (locsyms == NULL)
6963 return false;
6964 *locsymsp = locsyms;
6965 }
6966 sym = locsyms + r_symndx;
6967
6968 if (hp != NULL)
6969 *hp = NULL;
6970
6971 if (symp != NULL)
6972 *symp = sym;
6973
6974 if (symsecp != NULL)
6975 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6976
6977 if (tls_maskp != NULL)
6978 {
6979 struct got_entry **lgot_ents;
6980 unsigned char *tls_mask;
6981
6982 tls_mask = NULL;
6983 lgot_ents = elf_local_got_ents (ibfd);
6984 if (lgot_ents != NULL)
6985 {
6986 struct plt_entry **local_plt = (struct plt_entry **)
6987 (lgot_ents + symtab_hdr->sh_info);
6988 unsigned char *lgot_masks = (unsigned char *)
6989 (local_plt + symtab_hdr->sh_info);
6990 tls_mask = &lgot_masks[r_symndx];
6991 }
6992 *tls_maskp = tls_mask;
6993 }
6994 }
6995 return true;
6996 }
6997
6998 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6999 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7000 type suitable for optimization, and 1 otherwise. */
7001
7002 static int
7003 get_tls_mask (unsigned char **tls_maskp,
7004 unsigned long *toc_symndx,
7005 bfd_vma *toc_addend,
7006 Elf_Internal_Sym **locsymsp,
7007 const Elf_Internal_Rela *rel,
7008 bfd *ibfd)
7009 {
7010 unsigned long r_symndx;
7011 int next_r;
7012 struct elf_link_hash_entry *h;
7013 Elf_Internal_Sym *sym;
7014 asection *sec;
7015 bfd_vma off;
7016
7017 r_symndx = ELF64_R_SYM (rel->r_info);
7018 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7019 return 0;
7020
7021 if ((*tls_maskp != NULL
7022 && (**tls_maskp & TLS_TLS) != 0
7023 && **tls_maskp != (TLS_TLS | TLS_MARK))
7024 || sec == NULL
7025 || ppc64_elf_section_data (sec) == NULL
7026 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7027 return 1;
7028
7029 /* Look inside a TOC section too. */
7030 if (h != NULL)
7031 {
7032 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7033 off = h->root.u.def.value;
7034 }
7035 else
7036 off = sym->st_value;
7037 off += rel->r_addend;
7038 BFD_ASSERT (off % 8 == 0);
7039 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7040 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7041 if (toc_symndx != NULL)
7042 *toc_symndx = r_symndx;
7043 if (toc_addend != NULL)
7044 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7045 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7046 return 0;
7047 if ((h == NULL || is_static_defined (h))
7048 && (next_r == -1 || next_r == -2))
7049 return 1 - next_r;
7050 return 1;
7051 }
7052
7053 /* Find (or create) an entry in the tocsave hash table. */
7054
7055 static struct tocsave_entry *
7056 tocsave_find (struct ppc_link_hash_table *htab,
7057 enum insert_option insert,
7058 Elf_Internal_Sym **local_syms,
7059 const Elf_Internal_Rela *irela,
7060 bfd *ibfd)
7061 {
7062 unsigned long r_indx;
7063 struct elf_link_hash_entry *h;
7064 Elf_Internal_Sym *sym;
7065 struct tocsave_entry ent, *p;
7066 hashval_t hash;
7067 struct tocsave_entry **slot;
7068
7069 r_indx = ELF64_R_SYM (irela->r_info);
7070 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7071 return NULL;
7072 if (ent.sec == NULL || ent.sec->output_section == NULL)
7073 {
7074 _bfd_error_handler
7075 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7076 return NULL;
7077 }
7078
7079 if (h != NULL)
7080 ent.offset = h->root.u.def.value;
7081 else
7082 ent.offset = sym->st_value;
7083 ent.offset += irela->r_addend;
7084
7085 hash = tocsave_htab_hash (&ent);
7086 slot = ((struct tocsave_entry **)
7087 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7088 if (slot == NULL)
7089 return NULL;
7090
7091 if (*slot == NULL)
7092 {
7093 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7094 if (p == NULL)
7095 return NULL;
7096 *p = ent;
7097 *slot = p;
7098 }
7099 return *slot;
7100 }
7101
7102 /* Adjust all global syms defined in opd sections. In gcc generated
7103 code for the old ABI, these will already have been done. */
7104
7105 static bool
7106 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7107 {
7108 struct ppc_link_hash_entry *eh;
7109 asection *sym_sec;
7110 struct _opd_sec_data *opd;
7111
7112 if (h->root.type == bfd_link_hash_indirect)
7113 return true;
7114
7115 if (h->root.type != bfd_link_hash_defined
7116 && h->root.type != bfd_link_hash_defweak)
7117 return true;
7118
7119 eh = ppc_elf_hash_entry (h);
7120 if (eh->adjust_done)
7121 return true;
7122
7123 sym_sec = eh->elf.root.u.def.section;
7124 opd = get_opd_info (sym_sec);
7125 if (opd != NULL && opd->adjust != NULL)
7126 {
7127 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7128 if (adjust == -1)
7129 {
7130 /* This entry has been deleted. */
7131 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7132 if (dsec == NULL)
7133 {
7134 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7135 if (discarded_section (dsec))
7136 {
7137 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7138 break;
7139 }
7140 }
7141 eh->elf.root.u.def.value = 0;
7142 eh->elf.root.u.def.section = dsec;
7143 }
7144 else
7145 eh->elf.root.u.def.value += adjust;
7146 eh->adjust_done = 1;
7147 }
7148 return true;
7149 }
7150
7151 /* Handles decrementing dynamic reloc counts for the reloc specified by
7152 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7153 have already been determined. */
7154
7155 static bool
7156 dec_dynrel_count (const Elf_Internal_Rela *rel,
7157 asection *sec,
7158 struct bfd_link_info *info,
7159 Elf_Internal_Sym **local_syms,
7160 struct elf_link_hash_entry *h,
7161 Elf_Internal_Sym *sym)
7162 {
7163 enum elf_ppc64_reloc_type r_type;
7164 asection *sym_sec = NULL;
7165
7166 /* Can this reloc be dynamic? This switch, and later tests here
7167 should be kept in sync with the code in check_relocs. */
7168 r_type = ELF64_R_TYPE (rel->r_info);
7169 switch (r_type)
7170 {
7171 default:
7172 return true;
7173
7174 case R_PPC64_TOC16:
7175 case R_PPC64_TOC16_DS:
7176 case R_PPC64_TOC16_LO:
7177 case R_PPC64_TOC16_HI:
7178 case R_PPC64_TOC16_HA:
7179 case R_PPC64_TOC16_LO_DS:
7180 if (h == NULL)
7181 return true;
7182 break;
7183
7184 case R_PPC64_TPREL16:
7185 case R_PPC64_TPREL16_LO:
7186 case R_PPC64_TPREL16_HI:
7187 case R_PPC64_TPREL16_HA:
7188 case R_PPC64_TPREL16_DS:
7189 case R_PPC64_TPREL16_LO_DS:
7190 case R_PPC64_TPREL16_HIGH:
7191 case R_PPC64_TPREL16_HIGHA:
7192 case R_PPC64_TPREL16_HIGHER:
7193 case R_PPC64_TPREL16_HIGHERA:
7194 case R_PPC64_TPREL16_HIGHEST:
7195 case R_PPC64_TPREL16_HIGHESTA:
7196 case R_PPC64_TPREL64:
7197 case R_PPC64_TPREL34:
7198 case R_PPC64_DTPMOD64:
7199 case R_PPC64_DTPREL64:
7200 case R_PPC64_ADDR64:
7201 case R_PPC64_REL30:
7202 case R_PPC64_REL32:
7203 case R_PPC64_REL64:
7204 case R_PPC64_ADDR14:
7205 case R_PPC64_ADDR14_BRNTAKEN:
7206 case R_PPC64_ADDR14_BRTAKEN:
7207 case R_PPC64_ADDR16:
7208 case R_PPC64_ADDR16_DS:
7209 case R_PPC64_ADDR16_HA:
7210 case R_PPC64_ADDR16_HI:
7211 case R_PPC64_ADDR16_HIGH:
7212 case R_PPC64_ADDR16_HIGHA:
7213 case R_PPC64_ADDR16_HIGHER:
7214 case R_PPC64_ADDR16_HIGHERA:
7215 case R_PPC64_ADDR16_HIGHEST:
7216 case R_PPC64_ADDR16_HIGHESTA:
7217 case R_PPC64_ADDR16_LO:
7218 case R_PPC64_ADDR16_LO_DS:
7219 case R_PPC64_ADDR24:
7220 case R_PPC64_ADDR32:
7221 case R_PPC64_UADDR16:
7222 case R_PPC64_UADDR32:
7223 case R_PPC64_UADDR64:
7224 case R_PPC64_TOC:
7225 case R_PPC64_D34:
7226 case R_PPC64_D34_LO:
7227 case R_PPC64_D34_HI30:
7228 case R_PPC64_D34_HA30:
7229 case R_PPC64_ADDR16_HIGHER34:
7230 case R_PPC64_ADDR16_HIGHERA34:
7231 case R_PPC64_ADDR16_HIGHEST34:
7232 case R_PPC64_ADDR16_HIGHESTA34:
7233 case R_PPC64_D28:
7234 break;
7235 }
7236
7237 if (local_syms != NULL)
7238 {
7239 unsigned long r_symndx;
7240 bfd *ibfd = sec->owner;
7241
7242 r_symndx = ELF64_R_SYM (rel->r_info);
7243 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7244 return false;
7245 }
7246
7247 if ((h != NULL
7248 && (h->root.type == bfd_link_hash_defweak
7249 || !h->def_regular))
7250 || (h != NULL
7251 && !bfd_link_executable (info)
7252 && !SYMBOLIC_BIND (info, h))
7253 || (bfd_link_pic (info)
7254 && must_be_dyn_reloc (info, r_type))
7255 || (!bfd_link_pic (info)
7256 && (h != NULL
7257 ? h->type == STT_GNU_IFUNC
7258 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7259 ;
7260 else
7261 return true;
7262
7263 if (h != NULL)
7264 {
7265 struct ppc_dyn_relocs *p;
7266 struct ppc_dyn_relocs **pp;
7267 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7268
7269 /* elf_gc_sweep may have already removed all dyn relocs associated
7270 with local syms for a given section. Also, symbol flags are
7271 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7272 report a dynreloc miscount. */
7273 if (*pp == NULL && info->gc_sections)
7274 return true;
7275
7276 while ((p = *pp) != NULL)
7277 {
7278 if (p->sec == sec)
7279 {
7280 if (!must_be_dyn_reloc (info, r_type))
7281 p->pc_count -= 1;
7282 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7283 && rel->r_offset % 2 == 0
7284 && sec->alignment_power != 0)
7285 p->rel_count -= 1;
7286 p->count -= 1;
7287 if (p->count == 0)
7288 *pp = p->next;
7289 return true;
7290 }
7291 pp = &p->next;
7292 }
7293 }
7294 else
7295 {
7296 struct ppc_local_dyn_relocs *p;
7297 struct ppc_local_dyn_relocs **pp;
7298 void *vpp;
7299 bool is_ifunc;
7300
7301 if (local_syms == NULL)
7302 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7303 if (sym_sec == NULL)
7304 sym_sec = sec;
7305
7306 vpp = &elf_section_data (sym_sec)->local_dynrel;
7307 pp = (struct ppc_local_dyn_relocs **) vpp;
7308
7309 if (*pp == NULL && info->gc_sections)
7310 return true;
7311
7312 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7313 while ((p = *pp) != NULL)
7314 {
7315 if (p->sec == sec && p->ifunc == is_ifunc)
7316 {
7317 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7318 && rel->r_offset % 2 == 0
7319 && sec->alignment_power != 0)
7320 p->rel_count -= 1;
7321 p->count -= 1;
7322 if (p->count == 0)
7323 *pp = p->next;
7324 return true;
7325 }
7326 pp = &p->next;
7327 }
7328 }
7329
7330 /* xgettext:c-format */
7331 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7332 sec->owner, sec);
7333 bfd_set_error (bfd_error_bad_value);
7334 return false;
7335 }
7336
7337 /* Remove unused Official Procedure Descriptor entries. Currently we
7338 only remove those associated with functions in discarded link-once
7339 sections, or weakly defined functions that have been overridden. It
7340 would be possible to remove many more entries for statically linked
7341 applications. */
7342
7343 bool
7344 ppc64_elf_edit_opd (struct bfd_link_info *info)
7345 {
7346 bfd *ibfd;
7347 bool some_edited = false;
7348 asection *need_pad = NULL;
7349 struct ppc_link_hash_table *htab;
7350
7351 htab = ppc_hash_table (info);
7352 if (htab == NULL)
7353 return false;
7354
7355 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7356 {
7357 asection *sec;
7358 Elf_Internal_Rela *relstart, *rel, *relend;
7359 Elf_Internal_Shdr *symtab_hdr;
7360 Elf_Internal_Sym *local_syms;
7361 struct _opd_sec_data *opd;
7362 bool need_edit, add_aux_fields, broken;
7363 bfd_size_type cnt_16b = 0;
7364
7365 if (!is_ppc64_elf (ibfd))
7366 continue;
7367
7368 sec = bfd_get_section_by_name (ibfd, ".opd");
7369 if (sec == NULL || sec->size == 0)
7370 continue;
7371
7372 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7373 continue;
7374
7375 if (sec->output_section == bfd_abs_section_ptr)
7376 continue;
7377
7378 /* Look through the section relocs. */
7379 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7380 continue;
7381
7382 local_syms = NULL;
7383 symtab_hdr = &elf_symtab_hdr (ibfd);
7384
7385 /* Read the relocations. */
7386 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7387 info->keep_memory);
7388 if (relstart == NULL)
7389 return false;
7390
7391 /* First run through the relocs to check they are sane, and to
7392 determine whether we need to edit this opd section. */
7393 need_edit = false;
7394 broken = false;
7395 need_pad = sec;
7396 relend = relstart + sec->reloc_count;
7397 for (rel = relstart; rel < relend; )
7398 {
7399 enum elf_ppc64_reloc_type r_type;
7400 unsigned long r_symndx;
7401 asection *sym_sec;
7402 struct elf_link_hash_entry *h;
7403 Elf_Internal_Sym *sym;
7404 bfd_vma offset;
7405
7406 /* .opd contains an array of 16 or 24 byte entries. We're
7407 only interested in the reloc pointing to a function entry
7408 point. */
7409 offset = rel->r_offset;
7410 if (rel + 1 == relend
7411 || rel[1].r_offset != offset + 8)
7412 {
7413 /* If someone messes with .opd alignment then after a
7414 "ld -r" we might have padding in the middle of .opd.
7415 Also, there's nothing to prevent someone putting
7416 something silly in .opd with the assembler. No .opd
7417 optimization for them! */
7418 broken_opd:
7419 _bfd_error_handler
7420 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7421 broken = true;
7422 break;
7423 }
7424
7425 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7426 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7427 {
7428 _bfd_error_handler
7429 /* xgettext:c-format */
7430 (_("%pB: unexpected reloc type %u in .opd section"),
7431 ibfd, r_type);
7432 broken = true;
7433 break;
7434 }
7435
7436 r_symndx = ELF64_R_SYM (rel->r_info);
7437 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7438 r_symndx, ibfd))
7439 goto error_ret;
7440
7441 if (sym_sec == NULL || sym_sec->owner == NULL)
7442 {
7443 const char *sym_name;
7444 if (h != NULL)
7445 sym_name = h->root.root.string;
7446 else
7447 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7448 sym_sec);
7449
7450 _bfd_error_handler
7451 /* xgettext:c-format */
7452 (_("%pB: undefined sym `%s' in .opd section"),
7453 ibfd, sym_name);
7454 broken = true;
7455 break;
7456 }
7457
7458 /* opd entries are always for functions defined in the
7459 current input bfd. If the symbol isn't defined in the
7460 input bfd, then we won't be using the function in this
7461 bfd; It must be defined in a linkonce section in another
7462 bfd, or is weak. It's also possible that we are
7463 discarding the function due to a linker script /DISCARD/,
7464 which we test for via the output_section. */
7465 if (sym_sec->owner != ibfd
7466 || sym_sec->output_section == bfd_abs_section_ptr)
7467 need_edit = true;
7468
7469 rel += 2;
7470 if (rel + 1 == relend
7471 || (rel + 2 < relend
7472 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7473 ++rel;
7474
7475 if (rel == relend)
7476 {
7477 if (sec->size == offset + 24)
7478 {
7479 need_pad = NULL;
7480 break;
7481 }
7482 if (sec->size == offset + 16)
7483 {
7484 cnt_16b++;
7485 break;
7486 }
7487 goto broken_opd;
7488 }
7489 else if (rel + 1 < relend
7490 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7491 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7492 {
7493 if (rel[0].r_offset == offset + 16)
7494 cnt_16b++;
7495 else if (rel[0].r_offset != offset + 24)
7496 goto broken_opd;
7497 }
7498 else
7499 goto broken_opd;
7500 }
7501
7502 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7503
7504 if (!broken && (need_edit || add_aux_fields))
7505 {
7506 Elf_Internal_Rela *write_rel;
7507 Elf_Internal_Shdr *rel_hdr;
7508 bfd_byte *rptr, *wptr;
7509 bfd_byte *new_contents;
7510 bfd_size_type amt;
7511
7512 new_contents = NULL;
7513 amt = OPD_NDX (sec->size) * sizeof (long);
7514 opd = &ppc64_elf_section_data (sec)->u.opd;
7515 opd->adjust = bfd_zalloc (sec->owner, amt);
7516 if (opd->adjust == NULL)
7517 return false;
7518
7519 /* This seems a waste of time as input .opd sections are all
7520 zeros as generated by gcc, but I suppose there's no reason
7521 this will always be so. We might start putting something in
7522 the third word of .opd entries. */
7523 if ((sec->flags & SEC_IN_MEMORY) == 0)
7524 {
7525 bfd_byte *loc;
7526 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7527 {
7528 free (loc);
7529 error_ret:
7530 if (symtab_hdr->contents != (unsigned char *) local_syms)
7531 free (local_syms);
7532 if (elf_section_data (sec)->relocs != relstart)
7533 free (relstart);
7534 return false;
7535 }
7536 sec->contents = loc;
7537 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7538 }
7539
7540 elf_section_data (sec)->relocs = relstart;
7541
7542 new_contents = sec->contents;
7543 if (add_aux_fields)
7544 {
7545 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7546 if (new_contents == NULL)
7547 return false;
7548 need_pad = NULL;
7549 }
7550 wptr = new_contents;
7551 rptr = sec->contents;
7552 write_rel = relstart;
7553 for (rel = relstart; rel < relend; )
7554 {
7555 unsigned long r_symndx;
7556 asection *sym_sec;
7557 struct elf_link_hash_entry *h;
7558 struct ppc_link_hash_entry *fdh = NULL;
7559 Elf_Internal_Sym *sym;
7560 long opd_ent_size;
7561 Elf_Internal_Rela *next_rel;
7562 bool skip;
7563
7564 r_symndx = ELF64_R_SYM (rel->r_info);
7565 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7566 r_symndx, ibfd))
7567 goto error_ret;
7568
7569 next_rel = rel + 2;
7570 if (next_rel + 1 == relend
7571 || (next_rel + 2 < relend
7572 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7573 ++next_rel;
7574
7575 /* See if the .opd entry is full 24 byte or
7576 16 byte (with fd_aux entry overlapped with next
7577 fd_func). */
7578 opd_ent_size = 24;
7579 if (next_rel == relend)
7580 {
7581 if (sec->size == rel->r_offset + 16)
7582 opd_ent_size = 16;
7583 }
7584 else if (next_rel->r_offset == rel->r_offset + 16)
7585 opd_ent_size = 16;
7586
7587 if (h != NULL
7588 && h->root.root.string[0] == '.')
7589 {
7590 fdh = ppc_elf_hash_entry (h)->oh;
7591 if (fdh != NULL)
7592 {
7593 fdh = ppc_follow_link (fdh);
7594 if (fdh->elf.root.type != bfd_link_hash_defined
7595 && fdh->elf.root.type != bfd_link_hash_defweak)
7596 fdh = NULL;
7597 }
7598 }
7599
7600 skip = (sym_sec->owner != ibfd
7601 || sym_sec->output_section == bfd_abs_section_ptr);
7602 if (skip)
7603 {
7604 if (fdh != NULL && sym_sec->owner == ibfd)
7605 {
7606 /* Arrange for the function descriptor sym
7607 to be dropped. */
7608 fdh->elf.root.u.def.value = 0;
7609 fdh->elf.root.u.def.section = sym_sec;
7610 }
7611 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7612
7613 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7614 rel = next_rel;
7615 else
7616 while (1)
7617 {
7618 if (!dec_dynrel_count (rel, sec, info,
7619 NULL, h, sym))
7620 goto error_ret;
7621
7622 if (++rel == next_rel)
7623 break;
7624
7625 r_symndx = ELF64_R_SYM (rel->r_info);
7626 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7627 r_symndx, ibfd))
7628 goto error_ret;
7629 }
7630 }
7631 else
7632 {
7633 /* We'll be keeping this opd entry. */
7634 long adjust;
7635
7636 if (fdh != NULL)
7637 {
7638 /* Redefine the function descriptor symbol to
7639 this location in the opd section. It is
7640 necessary to update the value here rather
7641 than using an array of adjustments as we do
7642 for local symbols, because various places
7643 in the generic ELF code use the value
7644 stored in u.def.value. */
7645 fdh->elf.root.u.def.value = wptr - new_contents;
7646 fdh->adjust_done = 1;
7647 }
7648
7649 /* Local syms are a bit tricky. We could
7650 tweak them as they can be cached, but
7651 we'd need to look through the local syms
7652 for the function descriptor sym which we
7653 don't have at the moment. So keep an
7654 array of adjustments. */
7655 adjust = (wptr - new_contents) - (rptr - sec->contents);
7656 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7657
7658 if (wptr != rptr)
7659 memcpy (wptr, rptr, opd_ent_size);
7660 wptr += opd_ent_size;
7661 if (add_aux_fields && opd_ent_size == 16)
7662 {
7663 memset (wptr, '\0', 8);
7664 wptr += 8;
7665 }
7666
7667 /* We need to adjust any reloc offsets to point to the
7668 new opd entries. */
7669 for ( ; rel != next_rel; ++rel)
7670 {
7671 rel->r_offset += adjust;
7672 if (write_rel != rel)
7673 memcpy (write_rel, rel, sizeof (*rel));
7674 ++write_rel;
7675 }
7676 }
7677
7678 rptr += opd_ent_size;
7679 }
7680
7681 sec->size = wptr - new_contents;
7682 sec->reloc_count = write_rel - relstart;
7683 if (add_aux_fields)
7684 {
7685 free (sec->contents);
7686 sec->contents = new_contents;
7687 }
7688
7689 /* Fudge the header size too, as this is used later in
7690 elf_bfd_final_link if we are emitting relocs. */
7691 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7692 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7693 some_edited = true;
7694 }
7695 else if (elf_section_data (sec)->relocs != relstart)
7696 free (relstart);
7697
7698 if (local_syms != NULL
7699 && symtab_hdr->contents != (unsigned char *) local_syms)
7700 {
7701 if (!info->keep_memory)
7702 free (local_syms);
7703 else
7704 symtab_hdr->contents = (unsigned char *) local_syms;
7705 }
7706 }
7707
7708 if (some_edited)
7709 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7710
7711 /* If we are doing a final link and the last .opd entry is just 16 byte
7712 long, add a 8 byte padding after it. */
7713 if (need_pad != NULL && !bfd_link_relocatable (info))
7714 {
7715 bfd_byte *p;
7716
7717 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7718 {
7719 BFD_ASSERT (need_pad->size > 0);
7720
7721 p = bfd_malloc (need_pad->size + 8);
7722 if (p == NULL)
7723 return false;
7724
7725 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7726 p, 0, need_pad->size))
7727 return false;
7728
7729 need_pad->contents = p;
7730 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7731 }
7732 else
7733 {
7734 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7735 if (p == NULL)
7736 return false;
7737
7738 need_pad->contents = p;
7739 }
7740
7741 memset (need_pad->contents + need_pad->size, 0, 8);
7742 need_pad->size += 8;
7743 }
7744
7745 return true;
7746 }
7747
7748 /* Analyze inline PLT call relocations to see whether calls to locally
7749 defined functions can be converted to direct calls. */
7750
7751 bool
7752 ppc64_elf_inline_plt (struct bfd_link_info *info)
7753 {
7754 struct ppc_link_hash_table *htab;
7755 bfd *ibfd;
7756 asection *sec;
7757 bfd_vma low_vma, high_vma, limit;
7758
7759 htab = ppc_hash_table (info);
7760 if (htab == NULL)
7761 return false;
7762
7763 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7764 reduced somewhat to cater for possible stubs that might be added
7765 between the call and its destination. */
7766 if (htab->params->group_size < 0)
7767 {
7768 limit = -htab->params->group_size;
7769 if (limit == 1)
7770 limit = 0x1e00000;
7771 }
7772 else
7773 {
7774 limit = htab->params->group_size;
7775 if (limit == 1)
7776 limit = 0x1c00000;
7777 }
7778
7779 low_vma = -1;
7780 high_vma = 0;
7781 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7782 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7783 {
7784 if (low_vma > sec->vma)
7785 low_vma = sec->vma;
7786 if (high_vma < sec->vma + sec->size)
7787 high_vma = sec->vma + sec->size;
7788 }
7789
7790 /* If a "bl" can reach anywhere in local code sections, then we can
7791 convert all inline PLT sequences to direct calls when the symbol
7792 is local. */
7793 if (high_vma - low_vma < limit)
7794 {
7795 htab->can_convert_all_inline_plt = 1;
7796 return true;
7797 }
7798
7799 /* Otherwise, go looking through relocs for cases where a direct
7800 call won't reach. Mark the symbol on any such reloc to disable
7801 the optimization and keep the PLT entry as it seems likely that
7802 this will be better than creating trampolines. Note that this
7803 will disable the optimization for all inline PLT calls to a
7804 particular symbol, not just those that won't reach. The
7805 difficulty in doing a more precise optimization is that the
7806 linker needs to make a decision depending on whether a
7807 particular R_PPC64_PLTCALL insn can be turned into a direct
7808 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7809 the sequence, and there is nothing that ties those relocs
7810 together except their symbol. */
7811
7812 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7813 {
7814 Elf_Internal_Shdr *symtab_hdr;
7815 Elf_Internal_Sym *local_syms;
7816
7817 if (!is_ppc64_elf (ibfd))
7818 continue;
7819
7820 local_syms = NULL;
7821 symtab_hdr = &elf_symtab_hdr (ibfd);
7822
7823 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7824 if (ppc64_elf_section_data (sec)->has_pltcall
7825 && !bfd_is_abs_section (sec->output_section))
7826 {
7827 Elf_Internal_Rela *relstart, *rel, *relend;
7828
7829 /* Read the relocations. */
7830 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7831 info->keep_memory);
7832 if (relstart == NULL)
7833 return false;
7834
7835 relend = relstart + sec->reloc_count;
7836 for (rel = relstart; rel < relend; rel++)
7837 {
7838 enum elf_ppc64_reloc_type r_type;
7839 unsigned long r_symndx;
7840 asection *sym_sec;
7841 struct elf_link_hash_entry *h;
7842 Elf_Internal_Sym *sym;
7843 unsigned char *tls_maskp;
7844
7845 r_type = ELF64_R_TYPE (rel->r_info);
7846 if (r_type != R_PPC64_PLTCALL
7847 && r_type != R_PPC64_PLTCALL_NOTOC)
7848 continue;
7849
7850 r_symndx = ELF64_R_SYM (rel->r_info);
7851 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7852 r_symndx, ibfd))
7853 {
7854 if (elf_section_data (sec)->relocs != relstart)
7855 free (relstart);
7856 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7857 free (local_syms);
7858 return false;
7859 }
7860
7861 if (sym_sec != NULL && sym_sec->output_section != NULL)
7862 {
7863 bfd_vma from, to;
7864 if (h != NULL)
7865 to = h->root.u.def.value;
7866 else
7867 to = sym->st_value;
7868 to += (rel->r_addend
7869 + sym_sec->output_offset
7870 + sym_sec->output_section->vma);
7871 from = (rel->r_offset
7872 + sec->output_offset
7873 + sec->output_section->vma);
7874 if (to - from + limit < 2 * limit
7875 && !(r_type == R_PPC64_PLTCALL_NOTOC
7876 && (((h ? h->other : sym->st_other)
7877 & STO_PPC64_LOCAL_MASK)
7878 > 1 << STO_PPC64_LOCAL_BIT)))
7879 *tls_maskp &= ~PLT_KEEP;
7880 }
7881 }
7882 if (elf_section_data (sec)->relocs != relstart)
7883 free (relstart);
7884 }
7885
7886 if (local_syms != NULL
7887 && symtab_hdr->contents != (unsigned char *) local_syms)
7888 {
7889 if (!info->keep_memory)
7890 free (local_syms);
7891 else
7892 symtab_hdr->contents = (unsigned char *) local_syms;
7893 }
7894 }
7895
7896 return true;
7897 }
7898
7899 /* Set htab->tls_get_addr and various other info specific to TLS.
7900 This needs to run before dynamic symbols are processed in
7901 bfd_elf_size_dynamic_sections. */
7902
7903 bool
7904 ppc64_elf_tls_setup (struct bfd_link_info *info)
7905 {
7906 struct ppc_link_hash_table *htab;
7907 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7908
7909 htab = ppc_hash_table (info);
7910 if (htab == NULL)
7911 return false;
7912
7913 /* Move dynamic linking info to the function descriptor sym. */
7914 if (htab->need_func_desc_adj)
7915 {
7916 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7917 htab->need_func_desc_adj = 0;
7918 }
7919
7920 if (abiversion (info->output_bfd) == 1)
7921 htab->opd_abi = 1;
7922
7923 if (htab->params->no_multi_toc)
7924 htab->do_multi_toc = 0;
7925 else if (!htab->do_multi_toc)
7926 htab->params->no_multi_toc = 1;
7927
7928 /* Default to --no-plt-localentry, as this option can cause problems
7929 with symbol interposition. For example, glibc libpthread.so and
7930 libc.so duplicate many pthread symbols, with a fallback
7931 implementation in libc.so. In some cases the fallback does more
7932 work than the pthread implementation. __pthread_condattr_destroy
7933 is one such symbol: the libpthread.so implementation is
7934 localentry:0 while the libc.so implementation is localentry:8.
7935 An app that "cleverly" uses dlopen to only load necessary
7936 libraries at runtime may omit loading libpthread.so when not
7937 running multi-threaded, which then results in the libc.so
7938 fallback symbols being used and ld.so complaining. Now there
7939 are workarounds in ld (see non_zero_localentry) to detect the
7940 pthread situation, but that may not be the only case where
7941 --plt-localentry can cause trouble. */
7942 if (htab->params->plt_localentry0 < 0)
7943 htab->params->plt_localentry0 = 0;
7944 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7945 {
7946 /* The issue is that __glink_PLTresolve saves r2, which is done
7947 because glibc ld.so _dl_runtime_resolve restores r2 to support
7948 a glibc plt call optimisation where global entry code is
7949 skipped on calls that resolve to the same binary. The
7950 __glink_PLTresolve save of r2 is incompatible with code
7951 making tail calls, because the tail call might go via the
7952 resolver and thus overwrite the proper saved r2. */
7953 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7954 "power10 pc-relative code"));
7955 htab->params->plt_localentry0 = 0;
7956 }
7957 if (htab->params->plt_localentry0
7958 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7959 false, false, false) == NULL)
7960 _bfd_error_handler
7961 (_("warning: --plt-localentry is especially dangerous without "
7962 "ld.so support to detect ABI violations"));
7963
7964 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7965 false, false, true);
7966 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7967 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7968 false, false, true);
7969 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7970
7971 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7972 false, false, true);
7973 htab->tga_desc = ppc_elf_hash_entry (desc);
7974 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7975 false, false, true);
7976 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7977
7978 if (htab->params->tls_get_addr_opt)
7979 {
7980 struct elf_link_hash_entry *opt, *opt_fd;
7981
7982 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7983 false, false, true);
7984 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7985 false, false, true);
7986 if (opt_fd != NULL
7987 && (opt_fd->root.type == bfd_link_hash_defined
7988 || opt_fd->root.type == bfd_link_hash_defweak))
7989 {
7990 /* If glibc supports an optimized __tls_get_addr call stub,
7991 signalled by the presence of __tls_get_addr_opt, and we'll
7992 be calling __tls_get_addr via a plt call stub, then
7993 make __tls_get_addr point to __tls_get_addr_opt. */
7994 if (!(htab->elf.dynamic_sections_created
7995 && tga_fd != NULL
7996 && (tga_fd->type == STT_FUNC
7997 || tga_fd->needs_plt)
7998 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7999 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8000 tga_fd = NULL;
8001 if (!(htab->elf.dynamic_sections_created
8002 && desc_fd != NULL
8003 && (desc_fd->type == STT_FUNC
8004 || desc_fd->needs_plt)
8005 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8006 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8007 desc_fd = NULL;
8008
8009 if (tga_fd != NULL || desc_fd != NULL)
8010 {
8011 struct plt_entry *ent = NULL;
8012
8013 if (tga_fd != NULL)
8014 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8015 if (ent->plt.refcount > 0)
8016 break;
8017 if (ent == NULL && desc_fd != NULL)
8018 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8019 if (ent->plt.refcount > 0)
8020 break;
8021 if (ent != NULL)
8022 {
8023 if (tga_fd != NULL)
8024 {
8025 tga_fd->root.type = bfd_link_hash_indirect;
8026 tga_fd->root.u.i.link = &opt_fd->root;
8027 tga_fd->root.u.i.warning = NULL;
8028 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8029 }
8030 if (desc_fd != NULL)
8031 {
8032 desc_fd->root.type = bfd_link_hash_indirect;
8033 desc_fd->root.u.i.link = &opt_fd->root;
8034 desc_fd->root.u.i.warning = NULL;
8035 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8036 }
8037 opt_fd->mark = 1;
8038 if (opt_fd->dynindx != -1)
8039 {
8040 /* Use __tls_get_addr_opt in dynamic relocations. */
8041 opt_fd->dynindx = -1;
8042 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8043 opt_fd->dynstr_index);
8044 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8045 return false;
8046 }
8047 if (tga_fd != NULL)
8048 {
8049 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8050 tga = elf_hash_entry (htab->tls_get_addr);
8051 if (opt != NULL && tga != NULL)
8052 {
8053 tga->root.type = bfd_link_hash_indirect;
8054 tga->root.u.i.link = &opt->root;
8055 tga->root.u.i.warning = NULL;
8056 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8057 opt->mark = 1;
8058 _bfd_elf_link_hash_hide_symbol (info, opt,
8059 tga->forced_local);
8060 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8061 }
8062 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8063 htab->tls_get_addr_fd->is_func_descriptor = 1;
8064 if (htab->tls_get_addr != NULL)
8065 {
8066 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8067 htab->tls_get_addr->is_func = 1;
8068 }
8069 }
8070 if (desc_fd != NULL)
8071 {
8072 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8073 if (opt != NULL && desc != NULL)
8074 {
8075 desc->root.type = bfd_link_hash_indirect;
8076 desc->root.u.i.link = &opt->root;
8077 desc->root.u.i.warning = NULL;
8078 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8079 opt->mark = 1;
8080 _bfd_elf_link_hash_hide_symbol (info, opt,
8081 desc->forced_local);
8082 htab->tga_desc = ppc_elf_hash_entry (opt);
8083 }
8084 htab->tga_desc_fd->oh = htab->tga_desc;
8085 htab->tga_desc_fd->is_func_descriptor = 1;
8086 if (htab->tga_desc != NULL)
8087 {
8088 htab->tga_desc->oh = htab->tga_desc_fd;
8089 htab->tga_desc->is_func = 1;
8090 }
8091 }
8092 }
8093 }
8094 }
8095 else if (htab->params->tls_get_addr_opt < 0)
8096 htab->params->tls_get_addr_opt = 0;
8097 }
8098
8099 if (htab->tga_desc_fd != NULL
8100 && htab->params->tls_get_addr_opt
8101 && htab->params->no_tls_get_addr_regsave == -1)
8102 htab->params->no_tls_get_addr_regsave = 0;
8103
8104 return true;
8105 }
8106
8107 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8108 any of HASH1, HASH2, HASH3, or HASH4. */
8109
8110 static bool
8111 branch_reloc_hash_match (bfd *ibfd,
8112 Elf_Internal_Rela *rel,
8113 struct ppc_link_hash_entry *hash1,
8114 struct ppc_link_hash_entry *hash2,
8115 struct ppc_link_hash_entry *hash3,
8116 struct ppc_link_hash_entry *hash4)
8117 {
8118 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8119 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8120 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8121
8122 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8123 {
8124 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8125 struct elf_link_hash_entry *h;
8126
8127 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8128 h = elf_follow_link (h);
8129 if (h == elf_hash_entry (hash1)
8130 || h == elf_hash_entry (hash2)
8131 || h == elf_hash_entry (hash3)
8132 || h == elf_hash_entry (hash4))
8133 return true;
8134 }
8135 return false;
8136 }
8137
8138 /* Run through all the TLS relocs looking for optimization
8139 opportunities. The linker has been hacked (see ppc64elf.em) to do
8140 a preliminary section layout so that we know the TLS segment
8141 offsets. We can't optimize earlier because some optimizations need
8142 to know the tp offset, and we need to optimize before allocating
8143 dynamic relocations. */
8144
8145 bool
8146 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8147 {
8148 bfd *ibfd;
8149 asection *sec;
8150 struct ppc_link_hash_table *htab;
8151 unsigned char *toc_ref;
8152 int pass;
8153
8154 if (!bfd_link_executable (info))
8155 return true;
8156
8157 htab = ppc_hash_table (info);
8158 if (htab == NULL)
8159 return false;
8160
8161 htab->do_tls_opt = 1;
8162
8163 /* Make two passes over the relocs. On the first pass, mark toc
8164 entries involved with tls relocs, and check that tls relocs
8165 involved in setting up a tls_get_addr call are indeed followed by
8166 such a call. If they are not, we can't do any tls optimization.
8167 On the second pass twiddle tls_mask flags to notify
8168 relocate_section that optimization can be done, and adjust got
8169 and plt refcounts. */
8170 toc_ref = NULL;
8171 for (pass = 0; pass < 2; ++pass)
8172 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8173 {
8174 Elf_Internal_Sym *locsyms = NULL;
8175 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8176
8177 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8178 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8179 {
8180 Elf_Internal_Rela *relstart, *rel, *relend;
8181 bool found_tls_get_addr_arg = 0;
8182
8183 /* Read the relocations. */
8184 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8185 info->keep_memory);
8186 if (relstart == NULL)
8187 {
8188 free (toc_ref);
8189 return false;
8190 }
8191
8192 relend = relstart + sec->reloc_count;
8193 for (rel = relstart; rel < relend; rel++)
8194 {
8195 enum elf_ppc64_reloc_type r_type;
8196 unsigned long r_symndx;
8197 struct elf_link_hash_entry *h;
8198 Elf_Internal_Sym *sym;
8199 asection *sym_sec;
8200 unsigned char *tls_mask;
8201 unsigned int tls_set, tls_clear, tls_type = 0;
8202 bfd_vma value;
8203 bool ok_tprel, is_local;
8204 long toc_ref_index = 0;
8205 int expecting_tls_get_addr = 0;
8206 bool ret = false;
8207
8208 r_symndx = ELF64_R_SYM (rel->r_info);
8209 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8210 r_symndx, ibfd))
8211 {
8212 err_free_rel:
8213 if (elf_section_data (sec)->relocs != relstart)
8214 free (relstart);
8215 free (toc_ref);
8216 if (elf_symtab_hdr (ibfd).contents
8217 != (unsigned char *) locsyms)
8218 free (locsyms);
8219 return ret;
8220 }
8221
8222 if (h != NULL)
8223 {
8224 if (h->root.type == bfd_link_hash_defined
8225 || h->root.type == bfd_link_hash_defweak)
8226 value = h->root.u.def.value;
8227 else if (h->root.type == bfd_link_hash_undefweak)
8228 value = 0;
8229 else
8230 {
8231 found_tls_get_addr_arg = 0;
8232 continue;
8233 }
8234 }
8235 else
8236 /* Symbols referenced by TLS relocs must be of type
8237 STT_TLS. So no need for .opd local sym adjust. */
8238 value = sym->st_value;
8239
8240 ok_tprel = false;
8241 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8242 if (is_local)
8243 {
8244 if (h != NULL
8245 && h->root.type == bfd_link_hash_undefweak)
8246 ok_tprel = true;
8247 else if (sym_sec != NULL
8248 && sym_sec->output_section != NULL)
8249 {
8250 value += sym_sec->output_offset;
8251 value += sym_sec->output_section->vma;
8252 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8253 /* Note that even though the prefix insns
8254 allow a 1<<33 offset we use the same test
8255 as for addis;addi. There may be a mix of
8256 pcrel and non-pcrel code and the decision
8257 to optimise is per symbol, not per TLS
8258 sequence. */
8259 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8260 }
8261 }
8262
8263 r_type = ELF64_R_TYPE (rel->r_info);
8264 /* If this section has old-style __tls_get_addr calls
8265 without marker relocs, then check that each
8266 __tls_get_addr call reloc is preceded by a reloc
8267 that conceivably belongs to the __tls_get_addr arg
8268 setup insn. If we don't find matching arg setup
8269 relocs, don't do any tls optimization. */
8270 if (pass == 0
8271 && sec->nomark_tls_get_addr
8272 && h != NULL
8273 && is_tls_get_addr (h, htab)
8274 && !found_tls_get_addr_arg
8275 && is_branch_reloc (r_type))
8276 {
8277 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8278 "TLS optimization disabled\n"),
8279 ibfd, sec, rel->r_offset);
8280 ret = true;
8281 goto err_free_rel;
8282 }
8283
8284 found_tls_get_addr_arg = 0;
8285 switch (r_type)
8286 {
8287 case R_PPC64_GOT_TLSLD16:
8288 case R_PPC64_GOT_TLSLD16_LO:
8289 case R_PPC64_GOT_TLSLD_PCREL34:
8290 expecting_tls_get_addr = 1;
8291 found_tls_get_addr_arg = 1;
8292 /* Fall through. */
8293
8294 case R_PPC64_GOT_TLSLD16_HI:
8295 case R_PPC64_GOT_TLSLD16_HA:
8296 /* These relocs should never be against a symbol
8297 defined in a shared lib. Leave them alone if
8298 that turns out to be the case. */
8299 if (!is_local)
8300 continue;
8301
8302 /* LD -> LE */
8303 tls_set = 0;
8304 tls_clear = TLS_LD;
8305 tls_type = TLS_TLS | TLS_LD;
8306 break;
8307
8308 case R_PPC64_GOT_TLSGD16:
8309 case R_PPC64_GOT_TLSGD16_LO:
8310 case R_PPC64_GOT_TLSGD_PCREL34:
8311 expecting_tls_get_addr = 1;
8312 found_tls_get_addr_arg = 1;
8313 /* Fall through. */
8314
8315 case R_PPC64_GOT_TLSGD16_HI:
8316 case R_PPC64_GOT_TLSGD16_HA:
8317 if (ok_tprel)
8318 /* GD -> LE */
8319 tls_set = 0;
8320 else
8321 /* GD -> IE */
8322 tls_set = TLS_TLS | TLS_GDIE;
8323 tls_clear = TLS_GD;
8324 tls_type = TLS_TLS | TLS_GD;
8325 break;
8326
8327 case R_PPC64_GOT_TPREL_PCREL34:
8328 case R_PPC64_GOT_TPREL16_DS:
8329 case R_PPC64_GOT_TPREL16_LO_DS:
8330 case R_PPC64_GOT_TPREL16_HI:
8331 case R_PPC64_GOT_TPREL16_HA:
8332 if (ok_tprel)
8333 {
8334 /* IE -> LE */
8335 tls_set = 0;
8336 tls_clear = TLS_TPREL;
8337 tls_type = TLS_TLS | TLS_TPREL;
8338 break;
8339 }
8340 continue;
8341
8342 case R_PPC64_TLSLD:
8343 if (!is_local)
8344 continue;
8345 /* Fall through. */
8346 case R_PPC64_TLSGD:
8347 if (rel + 1 < relend
8348 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8349 {
8350 if (pass != 0
8351 && (ELF64_R_TYPE (rel[1].r_info)
8352 != R_PPC64_PLTSEQ)
8353 && (ELF64_R_TYPE (rel[1].r_info)
8354 != R_PPC64_PLTSEQ_NOTOC))
8355 {
8356 r_symndx = ELF64_R_SYM (rel[1].r_info);
8357 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8358 r_symndx, ibfd))
8359 goto err_free_rel;
8360 if (h != NULL)
8361 {
8362 struct plt_entry *ent = NULL;
8363
8364 for (ent = h->plt.plist;
8365 ent != NULL;
8366 ent = ent->next)
8367 if (ent->addend == rel[1].r_addend)
8368 break;
8369
8370 if (ent != NULL
8371 && ent->plt.refcount > 0)
8372 ent->plt.refcount -= 1;
8373 }
8374 }
8375 continue;
8376 }
8377 found_tls_get_addr_arg = 1;
8378 /* Fall through. */
8379
8380 case R_PPC64_TLS:
8381 case R_PPC64_TOC16:
8382 case R_PPC64_TOC16_LO:
8383 if (sym_sec == NULL || sym_sec != toc)
8384 continue;
8385
8386 /* Mark this toc entry as referenced by a TLS
8387 code sequence. We can do that now in the
8388 case of R_PPC64_TLS, and after checking for
8389 tls_get_addr for the TOC16 relocs. */
8390 if (toc_ref == NULL)
8391 toc_ref
8392 = bfd_zmalloc (toc->output_section->rawsize / 8);
8393 if (toc_ref == NULL)
8394 goto err_free_rel;
8395
8396 if (h != NULL)
8397 value = h->root.u.def.value;
8398 else
8399 value = sym->st_value;
8400 value += rel->r_addend;
8401 if (value % 8 != 0)
8402 continue;
8403 BFD_ASSERT (value < toc->size
8404 && toc->output_offset % 8 == 0);
8405 toc_ref_index = (value + toc->output_offset) / 8;
8406 if (r_type == R_PPC64_TLS
8407 || r_type == R_PPC64_TLSGD
8408 || r_type == R_PPC64_TLSLD)
8409 {
8410 toc_ref[toc_ref_index] = 1;
8411 continue;
8412 }
8413
8414 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8415 continue;
8416
8417 tls_set = 0;
8418 tls_clear = 0;
8419 expecting_tls_get_addr = 2;
8420 break;
8421
8422 case R_PPC64_TPREL64:
8423 if (pass == 0
8424 || sec != toc
8425 || toc_ref == NULL
8426 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8427 continue;
8428 if (ok_tprel)
8429 {
8430 /* IE -> LE */
8431 tls_set = TLS_EXPLICIT;
8432 tls_clear = TLS_TPREL;
8433 break;
8434 }
8435 continue;
8436
8437 case R_PPC64_DTPMOD64:
8438 if (pass == 0
8439 || sec != toc
8440 || toc_ref == NULL
8441 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8442 continue;
8443 if (rel + 1 < relend
8444 && (rel[1].r_info
8445 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8446 && rel[1].r_offset == rel->r_offset + 8)
8447 {
8448 if (ok_tprel)
8449 /* GD -> LE */
8450 tls_set = TLS_EXPLICIT | TLS_GD;
8451 else
8452 /* GD -> IE */
8453 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8454 tls_clear = TLS_GD;
8455 }
8456 else
8457 {
8458 if (!is_local)
8459 continue;
8460
8461 /* LD -> LE */
8462 tls_set = TLS_EXPLICIT;
8463 tls_clear = TLS_LD;
8464 }
8465 break;
8466
8467 case R_PPC64_TPREL16_HA:
8468 if (pass == 0)
8469 {
8470 unsigned char buf[4];
8471 unsigned int insn;
8472 bfd_vma off = rel->r_offset & ~3;
8473 if (!bfd_get_section_contents (ibfd, sec, buf,
8474 off, 4))
8475 goto err_free_rel;
8476 insn = bfd_get_32 (ibfd, buf);
8477 /* addis rt,13,imm */
8478 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8479 != ((15u << 26) | (13 << 16)))
8480 {
8481 /* xgettext:c-format */
8482 info->callbacks->minfo
8483 (_("%H: warning: %s unexpected insn %#x.\n"),
8484 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8485 htab->do_tls_opt = 0;
8486 }
8487 }
8488 continue;
8489
8490 case R_PPC64_TPREL16_HI:
8491 case R_PPC64_TPREL16_HIGH:
8492 case R_PPC64_TPREL16_HIGHA:
8493 case R_PPC64_TPREL16_HIGHER:
8494 case R_PPC64_TPREL16_HIGHERA:
8495 case R_PPC64_TPREL16_HIGHEST:
8496 case R_PPC64_TPREL16_HIGHESTA:
8497 /* These can all be used in sequences along with
8498 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8499 able to verify easily. */
8500 htab->do_tls_opt = 0;
8501 continue;
8502
8503 default:
8504 continue;
8505 }
8506
8507 if (pass == 0)
8508 {
8509 if (!expecting_tls_get_addr
8510 || !sec->nomark_tls_get_addr)
8511 continue;
8512
8513 if (rel + 1 < relend
8514 && branch_reloc_hash_match (ibfd, rel + 1,
8515 htab->tls_get_addr_fd,
8516 htab->tga_desc_fd,
8517 htab->tls_get_addr,
8518 htab->tga_desc))
8519 {
8520 if (expecting_tls_get_addr == 2)
8521 {
8522 /* Check for toc tls entries. */
8523 unsigned char *toc_tls;
8524 int retval;
8525
8526 retval = get_tls_mask (&toc_tls, NULL, NULL,
8527 &locsyms,
8528 rel, ibfd);
8529 if (retval == 0)
8530 goto err_free_rel;
8531 if (toc_tls != NULL)
8532 {
8533 if ((*toc_tls & TLS_TLS) != 0
8534 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8535 found_tls_get_addr_arg = 1;
8536 if (retval > 1)
8537 toc_ref[toc_ref_index] = 1;
8538 }
8539 }
8540 continue;
8541 }
8542
8543 /* Uh oh, we didn't find the expected call. We
8544 could just mark this symbol to exclude it
8545 from tls optimization but it's safer to skip
8546 the entire optimization. */
8547 /* xgettext:c-format */
8548 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8549 "TLS optimization disabled\n"),
8550 ibfd, sec, rel->r_offset);
8551 ret = true;
8552 goto err_free_rel;
8553 }
8554
8555 /* If we don't have old-style __tls_get_addr calls
8556 without TLSGD/TLSLD marker relocs, and we haven't
8557 found a new-style __tls_get_addr call with a
8558 marker for this symbol, then we either have a
8559 broken object file or an -mlongcall style
8560 indirect call to __tls_get_addr without a marker.
8561 Disable optimization in this case. */
8562 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8563 && (tls_set & TLS_EXPLICIT) == 0
8564 && !sec->nomark_tls_get_addr
8565 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8566 != (TLS_TLS | TLS_MARK)))
8567 continue;
8568
8569 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8570 {
8571 struct plt_entry *ent = NULL;
8572
8573 if (htab->tls_get_addr_fd != NULL)
8574 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8575 ent != NULL;
8576 ent = ent->next)
8577 if (ent->addend == 0)
8578 break;
8579
8580 if (ent == NULL && htab->tga_desc_fd != NULL)
8581 for (ent = htab->tga_desc_fd->elf.plt.plist;
8582 ent != NULL;
8583 ent = ent->next)
8584 if (ent->addend == 0)
8585 break;
8586
8587 if (ent == NULL && htab->tls_get_addr != NULL)
8588 for (ent = htab->tls_get_addr->elf.plt.plist;
8589 ent != NULL;
8590 ent = ent->next)
8591 if (ent->addend == 0)
8592 break;
8593
8594 if (ent == NULL && htab->tga_desc != NULL)
8595 for (ent = htab->tga_desc->elf.plt.plist;
8596 ent != NULL;
8597 ent = ent->next)
8598 if (ent->addend == 0)
8599 break;
8600
8601 if (ent != NULL
8602 && ent->plt.refcount > 0)
8603 ent->plt.refcount -= 1;
8604 }
8605
8606 if (tls_clear == 0)
8607 continue;
8608
8609 if ((tls_set & TLS_EXPLICIT) == 0)
8610 {
8611 struct got_entry *ent;
8612
8613 /* Adjust got entry for this reloc. */
8614 if (h != NULL)
8615 ent = h->got.glist;
8616 else
8617 ent = elf_local_got_ents (ibfd)[r_symndx];
8618
8619 for (; ent != NULL; ent = ent->next)
8620 if (ent->addend == rel->r_addend
8621 && ent->owner == ibfd
8622 && ent->tls_type == tls_type)
8623 break;
8624 if (ent == NULL)
8625 abort ();
8626
8627 if (tls_set == 0)
8628 {
8629 /* We managed to get rid of a got entry. */
8630 if (ent->got.refcount > 0)
8631 ent->got.refcount -= 1;
8632 }
8633 }
8634 else
8635 {
8636 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8637 we'll lose one or two dyn relocs. */
8638 if (!dec_dynrel_count (rel, sec, info,
8639 NULL, h, sym))
8640 return false;
8641
8642 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8643 {
8644 if (!dec_dynrel_count (rel + 1, sec, info,
8645 NULL, h, sym))
8646 return false;
8647 }
8648 }
8649
8650 *tls_mask |= tls_set & 0xff;
8651 *tls_mask &= ~tls_clear;
8652 }
8653
8654 if (elf_section_data (sec)->relocs != relstart)
8655 free (relstart);
8656 }
8657
8658 if (locsyms != NULL
8659 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8660 {
8661 if (!info->keep_memory)
8662 free (locsyms);
8663 else
8664 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8665 }
8666 }
8667
8668 free (toc_ref);
8669 return true;
8670 }
8671
8672 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8673 the values of any global symbols in a toc section that has been
8674 edited. Globals in toc sections should be a rarity, so this function
8675 sets a flag if any are found in toc sections other than the one just
8676 edited, so that further hash table traversals can be avoided. */
8677
8678 struct adjust_toc_info
8679 {
8680 asection *toc;
8681 unsigned long *skip;
8682 bool global_toc_syms;
8683 };
8684
8685 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8686
8687 static bool
8688 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8689 {
8690 struct ppc_link_hash_entry *eh;
8691 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8692 unsigned long i;
8693
8694 if (h->root.type != bfd_link_hash_defined
8695 && h->root.type != bfd_link_hash_defweak)
8696 return true;
8697
8698 eh = ppc_elf_hash_entry (h);
8699 if (eh->adjust_done)
8700 return true;
8701
8702 if (eh->elf.root.u.def.section == toc_inf->toc)
8703 {
8704 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8705 i = toc_inf->toc->rawsize >> 3;
8706 else
8707 i = eh->elf.root.u.def.value >> 3;
8708
8709 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8710 {
8711 _bfd_error_handler
8712 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8713 do
8714 ++i;
8715 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8716 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8717 }
8718
8719 eh->elf.root.u.def.value -= toc_inf->skip[i];
8720 eh->adjust_done = 1;
8721 }
8722 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8723 toc_inf->global_toc_syms = true;
8724
8725 return true;
8726 }
8727
8728 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8729 on a _LO variety toc/got reloc. */
8730
8731 static bool
8732 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8733 {
8734 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8735 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8736 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8737 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8738 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8739 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8740 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8741 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8742 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8743 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8744 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8745 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8746 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8747 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8748 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8749 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8750 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8751 /* Exclude lfqu by testing reloc. If relocs are ever
8752 defined for the reduced D field in psq_lu then those
8753 will need testing too. */
8754 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8755 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8756 && (insn & 1) == 0)
8757 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8758 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8759 /* Exclude stfqu. psq_stu as above for psq_lu. */
8760 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8761 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8762 && (insn & 1) == 0));
8763 }
8764
8765 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8766 pld ra,symbol@got@pcrel
8767 load/store rt,off(ra)
8768 or
8769 pla ra,symbol@pcrel
8770 load/store rt,off(ra)
8771 may be translated to
8772 pload/pstore rt,symbol+off@pcrel
8773 nop.
8774 This function returns true if the optimization is possible, placing
8775 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8776
8777 On entry to this function, the linker has already determined that
8778 the pld can be replaced with pla: *PINSN1 is that pla insn,
8779 while *PINSN2 is the second instruction. */
8780
8781 static bool
8782 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8783 {
8784 uint64_t insn1 = *pinsn1;
8785 uint64_t insn2 = *pinsn2;
8786 bfd_signed_vma off;
8787
8788 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8789 {
8790 /* Check that regs match. */
8791 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8792 return false;
8793
8794 /* P8LS or PMLS form, non-pcrel. */
8795 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8796 return false;
8797
8798 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8799 *pinsn2 = PNOP;
8800 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8801 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8802 return true;
8803 }
8804
8805 insn2 >>= 32;
8806
8807 /* Check that regs match. */
8808 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8809 return false;
8810
8811 switch ((insn2 >> 26) & 63)
8812 {
8813 default:
8814 return false;
8815
8816 case 32: /* lwz */
8817 case 34: /* lbz */
8818 case 36: /* stw */
8819 case 38: /* stb */
8820 case 40: /* lhz */
8821 case 42: /* lha */
8822 case 44: /* sth */
8823 case 48: /* lfs */
8824 case 50: /* lfd */
8825 case 52: /* stfs */
8826 case 54: /* stfd */
8827 /* These are the PMLS cases, where we just need to tack a prefix
8828 on the insn. */
8829 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8830 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8831 off = insn2 & 0xffff;
8832 break;
8833
8834 case 58: /* lwa, ld */
8835 if ((insn2 & 1) != 0)
8836 return false;
8837 insn1 = ((1ULL << 58) | (1ULL << 52)
8838 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8839 | (insn2 & (31ULL << 21)));
8840 off = insn2 & 0xfffc;
8841 break;
8842
8843 case 57: /* lxsd, lxssp */
8844 if ((insn2 & 3) < 2)
8845 return false;
8846 insn1 = ((1ULL << 58) | (1ULL << 52)
8847 | ((40ULL | (insn2 & 3)) << 26)
8848 | (insn2 & (31ULL << 21)));
8849 off = insn2 & 0xfffc;
8850 break;
8851
8852 case 61: /* stxsd, stxssp, lxv, stxv */
8853 if ((insn2 & 3) == 0)
8854 return false;
8855 else if ((insn2 & 3) >= 2)
8856 {
8857 insn1 = ((1ULL << 58) | (1ULL << 52)
8858 | ((44ULL | (insn2 & 3)) << 26)
8859 | (insn2 & (31ULL << 21)));
8860 off = insn2 & 0xfffc;
8861 }
8862 else
8863 {
8864 insn1 = ((1ULL << 58) | (1ULL << 52)
8865 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8866 | (insn2 & (31ULL << 21)));
8867 off = insn2 & 0xfff0;
8868 }
8869 break;
8870
8871 case 56: /* lq */
8872 insn1 = ((1ULL << 58) | (1ULL << 52)
8873 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8874 off = insn2 & 0xffff;
8875 break;
8876
8877 case 6: /* lxvp, stxvp */
8878 if ((insn2 & 0xe) != 0)
8879 return false;
8880 insn1 = ((1ULL << 58) | (1ULL << 52)
8881 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8882 | (insn2 & (31ULL << 21)));
8883 off = insn2 & 0xfff0;
8884 break;
8885
8886 case 62: /* std, stq */
8887 if ((insn2 & 1) != 0)
8888 return false;
8889 insn1 = ((1ULL << 58) | (1ULL << 52)
8890 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8891 | (insn2 & (31ULL << 21)));
8892 off = insn2 & 0xfffc;
8893 break;
8894 }
8895
8896 *pinsn1 = insn1;
8897 *pinsn2 = (uint64_t) NOP << 32;
8898 *poff = (off ^ 0x8000) - 0x8000;
8899 return true;
8900 }
8901
8902 /* Examine all relocs referencing .toc sections in order to remove
8903 unused .toc entries. */
8904
8905 bool
8906 ppc64_elf_edit_toc (struct bfd_link_info *info)
8907 {
8908 bfd *ibfd;
8909 struct adjust_toc_info toc_inf;
8910 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8911
8912 htab->do_toc_opt = 1;
8913 toc_inf.global_toc_syms = true;
8914 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8915 {
8916 asection *toc, *sec;
8917 Elf_Internal_Shdr *symtab_hdr;
8918 Elf_Internal_Sym *local_syms;
8919 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8920 unsigned long *skip, *drop;
8921 unsigned char *used;
8922 unsigned char *keep, last, some_unused;
8923
8924 if (!is_ppc64_elf (ibfd))
8925 continue;
8926
8927 toc = bfd_get_section_by_name (ibfd, ".toc");
8928 if (toc == NULL
8929 || toc->size == 0
8930 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8931 || discarded_section (toc))
8932 continue;
8933
8934 toc_relocs = NULL;
8935 local_syms = NULL;
8936 symtab_hdr = &elf_symtab_hdr (ibfd);
8937
8938 /* Look at sections dropped from the final link. */
8939 skip = NULL;
8940 relstart = NULL;
8941 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8942 {
8943 if (sec->reloc_count == 0
8944 || !discarded_section (sec)
8945 || get_opd_info (sec)
8946 || (sec->flags & SEC_ALLOC) == 0
8947 || (sec->flags & SEC_DEBUGGING) != 0)
8948 continue;
8949
8950 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8951 if (relstart == NULL)
8952 goto error_ret;
8953
8954 /* Run through the relocs to see which toc entries might be
8955 unused. */
8956 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8957 {
8958 enum elf_ppc64_reloc_type r_type;
8959 unsigned long r_symndx;
8960 asection *sym_sec;
8961 struct elf_link_hash_entry *h;
8962 Elf_Internal_Sym *sym;
8963 bfd_vma val;
8964
8965 r_type = ELF64_R_TYPE (rel->r_info);
8966 switch (r_type)
8967 {
8968 default:
8969 continue;
8970
8971 case R_PPC64_TOC16:
8972 case R_PPC64_TOC16_LO:
8973 case R_PPC64_TOC16_HI:
8974 case R_PPC64_TOC16_HA:
8975 case R_PPC64_TOC16_DS:
8976 case R_PPC64_TOC16_LO_DS:
8977 break;
8978 }
8979
8980 r_symndx = ELF64_R_SYM (rel->r_info);
8981 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8982 r_symndx, ibfd))
8983 goto error_ret;
8984
8985 if (sym_sec != toc)
8986 continue;
8987
8988 if (h != NULL)
8989 val = h->root.u.def.value;
8990 else
8991 val = sym->st_value;
8992 val += rel->r_addend;
8993
8994 if (val >= toc->size)
8995 continue;
8996
8997 /* Anything in the toc ought to be aligned to 8 bytes.
8998 If not, don't mark as unused. */
8999 if (val & 7)
9000 continue;
9001
9002 if (skip == NULL)
9003 {
9004 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9005 if (skip == NULL)
9006 goto error_ret;
9007 }
9008
9009 skip[val >> 3] = ref_from_discarded;
9010 }
9011
9012 if (elf_section_data (sec)->relocs != relstart)
9013 free (relstart);
9014 }
9015
9016 /* For largetoc loads of address constants, we can convert
9017 . addis rx,2,addr@got@ha
9018 . ld ry,addr@got@l(rx)
9019 to
9020 . addis rx,2,addr@toc@ha
9021 . addi ry,rx,addr@toc@l
9022 when addr is within 2G of the toc pointer. This then means
9023 that the word storing "addr" in the toc is no longer needed. */
9024
9025 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9026 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9027 && toc->reloc_count != 0)
9028 {
9029 /* Read toc relocs. */
9030 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9031 info->keep_memory);
9032 if (toc_relocs == NULL)
9033 goto error_ret;
9034
9035 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9036 {
9037 enum elf_ppc64_reloc_type r_type;
9038 unsigned long r_symndx;
9039 asection *sym_sec;
9040 struct elf_link_hash_entry *h;
9041 Elf_Internal_Sym *sym;
9042 bfd_vma val, addr;
9043
9044 r_type = ELF64_R_TYPE (rel->r_info);
9045 if (r_type != R_PPC64_ADDR64)
9046 continue;
9047
9048 r_symndx = ELF64_R_SYM (rel->r_info);
9049 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9050 r_symndx, ibfd))
9051 goto error_ret;
9052
9053 if (sym_sec == NULL
9054 || sym_sec->output_section == NULL
9055 || discarded_section (sym_sec))
9056 continue;
9057
9058 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9059 continue;
9060
9061 if (h != NULL)
9062 {
9063 if (h->type == STT_GNU_IFUNC)
9064 continue;
9065 val = h->root.u.def.value;
9066 }
9067 else
9068 {
9069 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9070 continue;
9071 val = sym->st_value;
9072 }
9073 val += rel->r_addend;
9074 val += sym_sec->output_section->vma + sym_sec->output_offset;
9075
9076 /* We don't yet know the exact toc pointer value, but we
9077 know it will be somewhere in the toc section. Don't
9078 optimize if the difference from any possible toc
9079 pointer is outside [ff..f80008000, 7fff7fff]. */
9080 addr = toc->output_section->vma + TOC_BASE_OFF;
9081 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9082 continue;
9083
9084 addr = toc->output_section->vma + toc->output_section->rawsize;
9085 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9086 continue;
9087
9088 if (skip == NULL)
9089 {
9090 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9091 if (skip == NULL)
9092 goto error_ret;
9093 }
9094
9095 skip[rel->r_offset >> 3]
9096 |= can_optimize | ((rel - toc_relocs) << 2);
9097 }
9098 }
9099
9100 if (skip == NULL)
9101 continue;
9102
9103 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9104 if (used == NULL)
9105 {
9106 error_ret:
9107 if (symtab_hdr->contents != (unsigned char *) local_syms)
9108 free (local_syms);
9109 if (sec != NULL
9110 && elf_section_data (sec)->relocs != relstart)
9111 free (relstart);
9112 if (elf_section_data (toc)->relocs != toc_relocs)
9113 free (toc_relocs);
9114 free (skip);
9115 return false;
9116 }
9117
9118 /* Now check all kept sections that might reference the toc.
9119 Check the toc itself last. */
9120 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9121 : ibfd->sections);
9122 sec != NULL;
9123 sec = (sec == toc ? NULL
9124 : sec->next == NULL ? toc
9125 : sec->next == toc && toc->next ? toc->next
9126 : sec->next))
9127 {
9128 int repeat;
9129
9130 if (sec->reloc_count == 0
9131 || discarded_section (sec)
9132 || get_opd_info (sec)
9133 || (sec->flags & SEC_ALLOC) == 0
9134 || (sec->flags & SEC_DEBUGGING) != 0)
9135 continue;
9136
9137 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9138 info->keep_memory);
9139 if (relstart == NULL)
9140 {
9141 free (used);
9142 goto error_ret;
9143 }
9144
9145 /* Mark toc entries referenced as used. */
9146 do
9147 {
9148 repeat = 0;
9149 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9150 {
9151 enum elf_ppc64_reloc_type r_type;
9152 unsigned long r_symndx;
9153 asection *sym_sec;
9154 struct elf_link_hash_entry *h;
9155 Elf_Internal_Sym *sym;
9156 bfd_vma val;
9157
9158 r_type = ELF64_R_TYPE (rel->r_info);
9159 switch (r_type)
9160 {
9161 case R_PPC64_TOC16:
9162 case R_PPC64_TOC16_LO:
9163 case R_PPC64_TOC16_HI:
9164 case R_PPC64_TOC16_HA:
9165 case R_PPC64_TOC16_DS:
9166 case R_PPC64_TOC16_LO_DS:
9167 /* In case we're taking addresses of toc entries. */
9168 case R_PPC64_ADDR64:
9169 break;
9170
9171 default:
9172 continue;
9173 }
9174
9175 r_symndx = ELF64_R_SYM (rel->r_info);
9176 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9177 r_symndx, ibfd))
9178 {
9179 free (used);
9180 goto error_ret;
9181 }
9182
9183 if (sym_sec != toc)
9184 continue;
9185
9186 if (h != NULL)
9187 val = h->root.u.def.value;
9188 else
9189 val = sym->st_value;
9190 val += rel->r_addend;
9191
9192 if (val >= toc->size)
9193 continue;
9194
9195 if ((skip[val >> 3] & can_optimize) != 0)
9196 {
9197 bfd_vma off;
9198 unsigned char opc;
9199
9200 switch (r_type)
9201 {
9202 case R_PPC64_TOC16_HA:
9203 break;
9204
9205 case R_PPC64_TOC16_LO_DS:
9206 off = rel->r_offset;
9207 off += (bfd_big_endian (ibfd) ? -2 : 3);
9208 if (!bfd_get_section_contents (ibfd, sec, &opc,
9209 off, 1))
9210 {
9211 free (used);
9212 goto error_ret;
9213 }
9214 if ((opc & (0x3f << 2)) == (58u << 2))
9215 break;
9216 /* Fall through. */
9217
9218 default:
9219 /* Wrong sort of reloc, or not a ld. We may
9220 as well clear ref_from_discarded too. */
9221 skip[val >> 3] = 0;
9222 }
9223 }
9224
9225 if (sec != toc)
9226 used[val >> 3] = 1;
9227 /* For the toc section, we only mark as used if this
9228 entry itself isn't unused. */
9229 else if ((used[rel->r_offset >> 3]
9230 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9231 && !used[val >> 3])
9232 {
9233 /* Do all the relocs again, to catch reference
9234 chains. */
9235 repeat = 1;
9236 used[val >> 3] = 1;
9237 }
9238 }
9239 }
9240 while (repeat);
9241
9242 if (elf_section_data (sec)->relocs != relstart)
9243 free (relstart);
9244 }
9245
9246 /* Merge the used and skip arrays. Assume that TOC
9247 doublewords not appearing as either used or unused belong
9248 to an entry more than one doubleword in size. */
9249 for (drop = skip, keep = used, last = 0, some_unused = 0;
9250 drop < skip + (toc->size + 7) / 8;
9251 ++drop, ++keep)
9252 {
9253 if (*keep)
9254 {
9255 *drop &= ~ref_from_discarded;
9256 if ((*drop & can_optimize) != 0)
9257 some_unused = 1;
9258 last = 0;
9259 }
9260 else if ((*drop & ref_from_discarded) != 0)
9261 {
9262 some_unused = 1;
9263 last = ref_from_discarded;
9264 }
9265 else
9266 *drop = last;
9267 }
9268
9269 free (used);
9270
9271 if (some_unused)
9272 {
9273 bfd_byte *contents, *src;
9274 unsigned long off;
9275 Elf_Internal_Sym *sym;
9276 bool local_toc_syms = false;
9277
9278 /* Shuffle the toc contents, and at the same time convert the
9279 skip array from booleans into offsets. */
9280 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9281 goto error_ret;
9282
9283 elf_section_data (toc)->this_hdr.contents = contents;
9284
9285 for (src = contents, off = 0, drop = skip;
9286 src < contents + toc->size;
9287 src += 8, ++drop)
9288 {
9289 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9290 off += 8;
9291 else if (off != 0)
9292 {
9293 *drop = off;
9294 memcpy (src - off, src, 8);
9295 }
9296 }
9297 *drop = off;
9298 toc->rawsize = toc->size;
9299 toc->size = src - contents - off;
9300
9301 /* Adjust addends for relocs against the toc section sym,
9302 and optimize any accesses we can. */
9303 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9304 {
9305 if (sec->reloc_count == 0
9306 || discarded_section (sec))
9307 continue;
9308
9309 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9310 info->keep_memory);
9311 if (relstart == NULL)
9312 goto error_ret;
9313
9314 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9315 {
9316 enum elf_ppc64_reloc_type r_type;
9317 unsigned long r_symndx;
9318 asection *sym_sec;
9319 struct elf_link_hash_entry *h;
9320 bfd_vma val;
9321
9322 r_type = ELF64_R_TYPE (rel->r_info);
9323 switch (r_type)
9324 {
9325 default:
9326 continue;
9327
9328 case R_PPC64_TOC16:
9329 case R_PPC64_TOC16_LO:
9330 case R_PPC64_TOC16_HI:
9331 case R_PPC64_TOC16_HA:
9332 case R_PPC64_TOC16_DS:
9333 case R_PPC64_TOC16_LO_DS:
9334 case R_PPC64_ADDR64:
9335 break;
9336 }
9337
9338 r_symndx = ELF64_R_SYM (rel->r_info);
9339 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9340 r_symndx, ibfd))
9341 goto error_ret;
9342
9343 if (sym_sec != toc)
9344 continue;
9345
9346 if (h != NULL)
9347 val = h->root.u.def.value;
9348 else
9349 {
9350 val = sym->st_value;
9351 if (val != 0)
9352 local_toc_syms = true;
9353 }
9354
9355 val += rel->r_addend;
9356
9357 if (val > toc->rawsize)
9358 val = toc->rawsize;
9359 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9360 continue;
9361 else if ((skip[val >> 3] & can_optimize) != 0)
9362 {
9363 Elf_Internal_Rela *tocrel
9364 = toc_relocs + (skip[val >> 3] >> 2);
9365 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9366
9367 switch (r_type)
9368 {
9369 case R_PPC64_TOC16_HA:
9370 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9371 break;
9372
9373 case R_PPC64_TOC16_LO_DS:
9374 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9375 break;
9376
9377 default:
9378 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9379 ppc_howto_init ();
9380 info->callbacks->einfo
9381 /* xgettext:c-format */
9382 (_("%H: %s references "
9383 "optimized away TOC entry\n"),
9384 ibfd, sec, rel->r_offset,
9385 ppc64_elf_howto_table[r_type]->name);
9386 bfd_set_error (bfd_error_bad_value);
9387 goto error_ret;
9388 }
9389 rel->r_addend = tocrel->r_addend;
9390 elf_section_data (sec)->relocs = relstart;
9391 continue;
9392 }
9393
9394 if (h != NULL || sym->st_value != 0)
9395 continue;
9396
9397 rel->r_addend -= skip[val >> 3];
9398 elf_section_data (sec)->relocs = relstart;
9399 }
9400
9401 if (elf_section_data (sec)->relocs != relstart)
9402 free (relstart);
9403 }
9404
9405 /* We shouldn't have local or global symbols defined in the TOC,
9406 but handle them anyway. */
9407 if (local_syms != NULL)
9408 for (sym = local_syms;
9409 sym < local_syms + symtab_hdr->sh_info;
9410 ++sym)
9411 if (sym->st_value != 0
9412 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9413 {
9414 unsigned long i;
9415
9416 if (sym->st_value > toc->rawsize)
9417 i = toc->rawsize >> 3;
9418 else
9419 i = sym->st_value >> 3;
9420
9421 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9422 {
9423 if (local_toc_syms)
9424 _bfd_error_handler
9425 (_("%s defined on removed toc entry"),
9426 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9427 do
9428 ++i;
9429 while ((skip[i] & (ref_from_discarded | can_optimize)));
9430 sym->st_value = (bfd_vma) i << 3;
9431 }
9432
9433 sym->st_value -= skip[i];
9434 symtab_hdr->contents = (unsigned char *) local_syms;
9435 }
9436
9437 /* Adjust any global syms defined in this toc input section. */
9438 if (toc_inf.global_toc_syms)
9439 {
9440 toc_inf.toc = toc;
9441 toc_inf.skip = skip;
9442 toc_inf.global_toc_syms = false;
9443 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9444 &toc_inf);
9445 }
9446
9447 if (toc->reloc_count != 0)
9448 {
9449 Elf_Internal_Shdr *rel_hdr;
9450 Elf_Internal_Rela *wrel;
9451 bfd_size_type sz;
9452
9453 /* Remove unused toc relocs, and adjust those we keep. */
9454 if (toc_relocs == NULL)
9455 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9456 info->keep_memory);
9457 if (toc_relocs == NULL)
9458 goto error_ret;
9459
9460 wrel = toc_relocs;
9461 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9462 if ((skip[rel->r_offset >> 3]
9463 & (ref_from_discarded | can_optimize)) == 0)
9464 {
9465 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9466 wrel->r_info = rel->r_info;
9467 wrel->r_addend = rel->r_addend;
9468 ++wrel;
9469 }
9470 else if (!dec_dynrel_count (rel, toc, info,
9471 &local_syms, NULL, NULL))
9472 goto error_ret;
9473
9474 elf_section_data (toc)->relocs = toc_relocs;
9475 toc->reloc_count = wrel - toc_relocs;
9476 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9477 sz = rel_hdr->sh_entsize;
9478 rel_hdr->sh_size = toc->reloc_count * sz;
9479 }
9480 }
9481 else if (elf_section_data (toc)->relocs != toc_relocs)
9482 free (toc_relocs);
9483
9484 if (local_syms != NULL
9485 && symtab_hdr->contents != (unsigned char *) local_syms)
9486 {
9487 if (!info->keep_memory)
9488 free (local_syms);
9489 else
9490 symtab_hdr->contents = (unsigned char *) local_syms;
9491 }
9492 free (skip);
9493 }
9494
9495 /* Look for cases where we can change an indirect GOT access to
9496 a GOT relative or PC relative access, possibly reducing the
9497 number of GOT entries. */
9498 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9499 {
9500 asection *sec;
9501 Elf_Internal_Shdr *symtab_hdr;
9502 Elf_Internal_Sym *local_syms;
9503 Elf_Internal_Rela *relstart, *rel;
9504 bfd_vma got;
9505
9506 if (!is_ppc64_elf (ibfd))
9507 continue;
9508
9509 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9510 continue;
9511
9512 sec = ppc64_elf_tdata (ibfd)->got;
9513 got = 0;
9514 if (sec != NULL)
9515 got = sec->output_section->vma + sec->output_offset + 0x8000;
9516
9517 local_syms = NULL;
9518 symtab_hdr = &elf_symtab_hdr (ibfd);
9519
9520 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9521 {
9522 if (sec->reloc_count == 0
9523 || !ppc64_elf_section_data (sec)->has_optrel
9524 || discarded_section (sec))
9525 continue;
9526
9527 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9528 info->keep_memory);
9529 if (relstart == NULL)
9530 {
9531 got_error_ret:
9532 if (symtab_hdr->contents != (unsigned char *) local_syms)
9533 free (local_syms);
9534 if (sec != NULL
9535 && elf_section_data (sec)->relocs != relstart)
9536 free (relstart);
9537 return false;
9538 }
9539
9540 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9541 {
9542 enum elf_ppc64_reloc_type r_type;
9543 unsigned long r_symndx;
9544 Elf_Internal_Sym *sym;
9545 asection *sym_sec;
9546 struct elf_link_hash_entry *h;
9547 struct got_entry *ent;
9548 bfd_vma val, pc;
9549 unsigned char buf[8];
9550 unsigned int insn;
9551 enum {no_check, check_lo, check_ha} insn_check;
9552
9553 r_type = ELF64_R_TYPE (rel->r_info);
9554 switch (r_type)
9555 {
9556 default:
9557 insn_check = no_check;
9558 break;
9559
9560 case R_PPC64_PLT16_HA:
9561 case R_PPC64_GOT_TLSLD16_HA:
9562 case R_PPC64_GOT_TLSGD16_HA:
9563 case R_PPC64_GOT_TPREL16_HA:
9564 case R_PPC64_GOT_DTPREL16_HA:
9565 case R_PPC64_GOT16_HA:
9566 case R_PPC64_TOC16_HA:
9567 insn_check = check_ha;
9568 break;
9569
9570 case R_PPC64_PLT16_LO:
9571 case R_PPC64_PLT16_LO_DS:
9572 case R_PPC64_GOT_TLSLD16_LO:
9573 case R_PPC64_GOT_TLSGD16_LO:
9574 case R_PPC64_GOT_TPREL16_LO_DS:
9575 case R_PPC64_GOT_DTPREL16_LO_DS:
9576 case R_PPC64_GOT16_LO:
9577 case R_PPC64_GOT16_LO_DS:
9578 case R_PPC64_TOC16_LO:
9579 case R_PPC64_TOC16_LO_DS:
9580 insn_check = check_lo;
9581 break;
9582 }
9583
9584 if (insn_check != no_check)
9585 {
9586 bfd_vma off = rel->r_offset & ~3;
9587
9588 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9589 goto got_error_ret;
9590
9591 insn = bfd_get_32 (ibfd, buf);
9592 if (insn_check == check_lo
9593 ? !ok_lo_toc_insn (insn, r_type)
9594 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9595 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9596 {
9597 char str[12];
9598
9599 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9600 sprintf (str, "%#08x", insn);
9601 info->callbacks->einfo
9602 /* xgettext:c-format */
9603 (_("%H: got/toc optimization is not supported for"
9604 " %s instruction\n"),
9605 ibfd, sec, rel->r_offset & ~3, str);
9606 continue;
9607 }
9608 }
9609
9610 switch (r_type)
9611 {
9612 /* Note that we don't delete GOT entries for
9613 R_PPC64_GOT16_DS since we'd need a lot more
9614 analysis. For starters, the preliminary layout is
9615 before the GOT, PLT, dynamic sections and stubs are
9616 laid out. Then we'd need to allow for changes in
9617 distance between sections caused by alignment. */
9618 default:
9619 continue;
9620
9621 case R_PPC64_GOT16_HA:
9622 case R_PPC64_GOT16_LO_DS:
9623 case R_PPC64_GOT_PCREL34:
9624 break;
9625 }
9626
9627 r_symndx = ELF64_R_SYM (rel->r_info);
9628 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9629 r_symndx, ibfd))
9630 goto got_error_ret;
9631
9632 if (sym_sec == NULL
9633 || sym_sec->output_section == NULL
9634 || discarded_section (sym_sec))
9635 continue;
9636
9637 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9638 continue;
9639
9640 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9641 continue;
9642
9643 if (h != NULL)
9644 val = h->root.u.def.value;
9645 else
9646 val = sym->st_value;
9647 val += rel->r_addend;
9648 val += sym_sec->output_section->vma + sym_sec->output_offset;
9649
9650 /* Fudge factor to allow for the fact that the preliminary layout
9651 isn't exact. Reduce limits by this factor. */
9652 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9653
9654 switch (r_type)
9655 {
9656 default:
9657 continue;
9658
9659 case R_PPC64_GOT16_HA:
9660 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9661 >= LIMIT_ADJUST (0x100000000ULL))
9662 continue;
9663
9664 if (!bfd_get_section_contents (ibfd, sec, buf,
9665 rel->r_offset & ~3, 4))
9666 goto got_error_ret;
9667 insn = bfd_get_32 (ibfd, buf);
9668 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9669 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9670 continue;
9671 break;
9672
9673 case R_PPC64_GOT16_LO_DS:
9674 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9675 >= LIMIT_ADJUST (0x100000000ULL))
9676 continue;
9677 if (!bfd_get_section_contents (ibfd, sec, buf,
9678 rel->r_offset & ~3, 4))
9679 goto got_error_ret;
9680 insn = bfd_get_32 (ibfd, buf);
9681 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9682 continue;
9683 break;
9684
9685 case R_PPC64_GOT_PCREL34:
9686 pc = rel->r_offset;
9687 pc += sec->output_section->vma + sec->output_offset;
9688 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9689 >= LIMIT_ADJUST (1ULL << 34))
9690 continue;
9691 if (!bfd_get_section_contents (ibfd, sec, buf,
9692 rel->r_offset & ~3, 8))
9693 goto got_error_ret;
9694 insn = bfd_get_32 (ibfd, buf);
9695 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9696 continue;
9697 insn = bfd_get_32 (ibfd, buf + 4);
9698 if ((insn & (0x3fu << 26)) != 57u << 26)
9699 continue;
9700 break;
9701 }
9702 #undef LIMIT_ADJUST
9703
9704 if (h != NULL)
9705 ent = h->got.glist;
9706 else
9707 {
9708 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9709 ent = local_got_ents[r_symndx];
9710 }
9711 for (; ent != NULL; ent = ent->next)
9712 if (ent->addend == rel->r_addend
9713 && ent->owner == ibfd
9714 && ent->tls_type == 0)
9715 break;
9716 BFD_ASSERT (ent && ent->got.refcount > 0);
9717 ent->got.refcount -= 1;
9718 }
9719
9720 if (elf_section_data (sec)->relocs != relstart)
9721 free (relstart);
9722 }
9723
9724 if (local_syms != NULL
9725 && symtab_hdr->contents != (unsigned char *) local_syms)
9726 {
9727 if (!info->keep_memory)
9728 free (local_syms);
9729 else
9730 symtab_hdr->contents = (unsigned char *) local_syms;
9731 }
9732 }
9733
9734 return true;
9735 }
9736
9737 /* Return true iff input section I references the TOC using
9738 instructions limited to +/-32k offsets. */
9739
9740 bool
9741 ppc64_elf_has_small_toc_reloc (asection *i)
9742 {
9743 return (is_ppc64_elf (i->owner)
9744 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9745 }
9746
9747 /* Allocate space for one GOT entry. */
9748
9749 static void
9750 allocate_got (struct elf_link_hash_entry *h,
9751 struct bfd_link_info *info,
9752 struct got_entry *gent)
9753 {
9754 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9755 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9756 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9757 ? 16 : 8);
9758 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9759 ? 2 : 1) * sizeof (Elf64_External_Rela);
9760 asection *got = ppc64_elf_tdata (gent->owner)->got;
9761
9762 gent->got.offset = got->size;
9763 got->size += entsize;
9764
9765 if (h->type == STT_GNU_IFUNC)
9766 {
9767 htab->elf.irelplt->size += rentsize;
9768 htab->got_reli_size += rentsize;
9769 }
9770 else if (((bfd_link_pic (info)
9771 && (gent->tls_type == 0
9772 ? !info->enable_dt_relr
9773 : !(bfd_link_executable (info)
9774 && SYMBOL_REFERENCES_LOCAL (info, h))))
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 {
10004 unsigned int count;
10005 asection *sreloc = elf_section_data (p->sec)->sreloc;
10006 if (eh->elf.type == STT_GNU_IFUNC)
10007 sreloc = htab->elf.irelplt;
10008 count = p->count;
10009 if (info->enable_dt_relr
10010 && ((!NO_OPD_RELOCS
10011 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10012 || (eh->elf.type != STT_GNU_IFUNC
10013 && SYMBOL_REFERENCES_LOCAL (info, h))))
10014 count -= p->rel_count;
10015 sreloc->size += count * sizeof (Elf64_External_Rela);
10016 }
10017 }
10018
10019 /* We might need a PLT entry when the symbol
10020 a) is dynamic, or
10021 b) is an ifunc, or
10022 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10023 d) has plt16 relocs and we are linking statically. */
10024 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10025 || h->type == STT_GNU_IFUNC
10026 || (h->needs_plt && h->dynamic_adjusted)
10027 || (h->needs_plt
10028 && h->def_regular
10029 && !htab->elf.dynamic_sections_created
10030 && !htab->can_convert_all_inline_plt
10031 && (ppc_elf_hash_entry (h)->tls_mask
10032 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10033 {
10034 struct plt_entry *pent;
10035 bool doneone = false;
10036 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10037 if (pent->plt.refcount > 0)
10038 {
10039 if (!ensure_undef_dynamic (info, h))
10040 return false;
10041
10042 if (use_local_plt (info, h))
10043 {
10044 if (h->type == STT_GNU_IFUNC)
10045 {
10046 s = htab->elf.iplt;
10047 pent->plt.offset = s->size;
10048 s->size += PLT_ENTRY_SIZE (htab);
10049 s = htab->elf.irelplt;
10050 }
10051 else
10052 {
10053 s = htab->pltlocal;
10054 pent->plt.offset = s->size;
10055 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10056 s = NULL;
10057 if (bfd_link_pic (info)
10058 && !(info->enable_dt_relr && !htab->opd_abi))
10059 s = htab->relpltlocal;
10060 }
10061 }
10062 else
10063 {
10064 /* If this is the first .plt entry, make room for the special
10065 first entry. */
10066 s = htab->elf.splt;
10067 if (s->size == 0)
10068 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10069
10070 pent->plt.offset = s->size;
10071
10072 /* Make room for this entry. */
10073 s->size += PLT_ENTRY_SIZE (htab);
10074
10075 /* Make room for the .glink code. */
10076 s = htab->glink;
10077 if (s->size == 0)
10078 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10079 if (htab->opd_abi)
10080 {
10081 /* We need bigger stubs past index 32767. */
10082 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10083 s->size += 4;
10084 s->size += 2*4;
10085 }
10086 else
10087 s->size += 4;
10088
10089 /* We also need to make an entry in the .rela.plt section. */
10090 s = htab->elf.srelplt;
10091 }
10092 if (s != NULL)
10093 s->size += sizeof (Elf64_External_Rela);
10094 doneone = true;
10095 }
10096 else
10097 pent->plt.offset = (bfd_vma) -1;
10098 if (!doneone)
10099 {
10100 h->plt.plist = NULL;
10101 h->needs_plt = 0;
10102 }
10103 }
10104 else
10105 {
10106 h->plt.plist = NULL;
10107 h->needs_plt = 0;
10108 }
10109
10110 return true;
10111 }
10112
10113 #define PPC_LO(v) ((v) & 0xffff)
10114 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10115 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10116 #define D34(v) \
10117 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10118 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10119
10120 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10121 to set up space for global entry stubs. These are put in glink,
10122 after the branch table. */
10123
10124 static bool
10125 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10126 {
10127 struct bfd_link_info *info;
10128 struct ppc_link_hash_table *htab;
10129 struct plt_entry *pent;
10130 asection *s, *plt;
10131
10132 if (h->root.type == bfd_link_hash_indirect)
10133 return true;
10134
10135 if (!h->pointer_equality_needed)
10136 return true;
10137
10138 if (h->def_regular)
10139 return true;
10140
10141 info = inf;
10142 htab = ppc_hash_table (info);
10143 if (htab == NULL)
10144 return false;
10145
10146 s = htab->global_entry;
10147 plt = htab->elf.splt;
10148 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10149 if (pent->plt.offset != (bfd_vma) -1
10150 && pent->addend == 0)
10151 {
10152 /* For ELFv2, if this symbol is not defined in a regular file
10153 and we are not generating a shared library or pie, then we
10154 need to define the symbol in the executable on a call stub.
10155 This is to avoid text relocations. */
10156 bfd_vma off, stub_align, stub_off, stub_size;
10157 unsigned int align_power;
10158
10159 stub_size = 16;
10160 stub_off = s->size;
10161 if (htab->params->plt_stub_align >= 0)
10162 align_power = htab->params->plt_stub_align;
10163 else
10164 align_power = -htab->params->plt_stub_align;
10165 /* Setting section alignment is delayed until we know it is
10166 non-empty. Otherwise the .text output section will be
10167 aligned at least to plt_stub_align even when no global
10168 entry stubs are needed. */
10169 if (s->alignment_power < align_power)
10170 s->alignment_power = align_power;
10171 stub_align = (bfd_vma) 1 << align_power;
10172 if (htab->params->plt_stub_align >= 0
10173 || ((((stub_off + stub_size - 1) & -stub_align)
10174 - (stub_off & -stub_align))
10175 > ((stub_size - 1) & -stub_align)))
10176 stub_off = (stub_off + stub_align - 1) & -stub_align;
10177 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10178 off -= stub_off + s->output_offset + s->output_section->vma;
10179 /* Note that for --plt-stub-align negative we have a possible
10180 dependency between stub offset and size. Break that
10181 dependency by assuming the max stub size when calculating
10182 the stub offset. */
10183 if (PPC_HA (off) == 0)
10184 stub_size -= 4;
10185 h->root.type = bfd_link_hash_defined;
10186 h->root.u.def.section = s;
10187 h->root.u.def.value = stub_off;
10188 s->size = stub_off + stub_size;
10189 break;
10190 }
10191 return true;
10192 }
10193
10194 /* Set the sizes of the dynamic sections. */
10195
10196 static bool
10197 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10198 struct bfd_link_info *info)
10199 {
10200 struct ppc_link_hash_table *htab;
10201 bfd *dynobj;
10202 asection *s;
10203 bool relocs;
10204 bfd *ibfd;
10205 struct got_entry *first_tlsld;
10206
10207 htab = ppc_hash_table (info);
10208 if (htab == NULL)
10209 return false;
10210
10211 dynobj = htab->elf.dynobj;
10212 if (dynobj == NULL)
10213 abort ();
10214
10215 if (htab->elf.dynamic_sections_created)
10216 {
10217 /* Set the contents of the .interp section to the interpreter. */
10218 if (bfd_link_executable (info) && !info->nointerp)
10219 {
10220 s = bfd_get_linker_section (dynobj, ".interp");
10221 if (s == NULL)
10222 abort ();
10223 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10224 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10225 }
10226 }
10227
10228 /* Set up .got offsets for local syms, and space for local dynamic
10229 relocs. */
10230 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10231 {
10232 struct got_entry **lgot_ents;
10233 struct got_entry **end_lgot_ents;
10234 struct plt_entry **local_plt;
10235 struct plt_entry **end_local_plt;
10236 unsigned char *lgot_masks;
10237 bfd_size_type locsymcount;
10238 Elf_Internal_Shdr *symtab_hdr;
10239
10240 if (!is_ppc64_elf (ibfd))
10241 continue;
10242
10243 for (s = ibfd->sections; s != NULL; s = s->next)
10244 {
10245 struct ppc_local_dyn_relocs *p;
10246
10247 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10248 {
10249 if (!bfd_is_abs_section (p->sec)
10250 && bfd_is_abs_section (p->sec->output_section))
10251 {
10252 /* Input section has been discarded, either because
10253 it is a copy of a linkonce section or due to
10254 linker script /DISCARD/, so we'll be discarding
10255 the relocs too. */
10256 }
10257 else if (p->count != 0)
10258 {
10259 unsigned int count;
10260 asection *srel;
10261
10262 count = p->count;
10263 if (info->enable_dt_relr
10264 && ((!NO_OPD_RELOCS
10265 && (ppc64_elf_section_data (p->sec)->sec_type
10266 == sec_opd))
10267 || !p->ifunc))
10268 count -= p->rel_count;
10269 srel = elf_section_data (p->sec)->sreloc;
10270 if (p->ifunc)
10271 srel = htab->elf.irelplt;
10272 srel->size += count * sizeof (Elf64_External_Rela);
10273 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10274 info->flags |= DF_TEXTREL;
10275 }
10276 }
10277 }
10278
10279 lgot_ents = elf_local_got_ents (ibfd);
10280 if (!lgot_ents)
10281 continue;
10282
10283 symtab_hdr = &elf_symtab_hdr (ibfd);
10284 locsymcount = symtab_hdr->sh_info;
10285 end_lgot_ents = lgot_ents + locsymcount;
10286 local_plt = (struct plt_entry **) end_lgot_ents;
10287 end_local_plt = local_plt + locsymcount;
10288 lgot_masks = (unsigned char *) end_local_plt;
10289 s = ppc64_elf_tdata (ibfd)->got;
10290 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10291 {
10292 struct got_entry **pent, *ent;
10293
10294 pent = lgot_ents;
10295 while ((ent = *pent) != NULL)
10296 if (ent->got.refcount > 0)
10297 {
10298 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10299 {
10300 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10301 *pent = ent->next;
10302 }
10303 else
10304 {
10305 unsigned int ent_size = 8;
10306 unsigned int rel_size = sizeof (Elf64_External_Rela);
10307
10308 ent->got.offset = s->size;
10309 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10310 {
10311 ent_size *= 2;
10312 rel_size *= 2;
10313 }
10314 s->size += ent_size;
10315 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10316 {
10317 htab->elf.irelplt->size += rel_size;
10318 htab->got_reli_size += rel_size;
10319 }
10320 else if (bfd_link_pic (info)
10321 && (ent->tls_type == 0
10322 ? !info->enable_dt_relr
10323 : !bfd_link_executable (info)))
10324 {
10325 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10326 srel->size += rel_size;
10327 }
10328 pent = &ent->next;
10329 }
10330 }
10331 else
10332 *pent = ent->next;
10333 }
10334
10335 /* Allocate space for plt calls to local syms. */
10336 lgot_masks = (unsigned char *) end_local_plt;
10337 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10338 {
10339 struct plt_entry *ent;
10340
10341 for (ent = *local_plt; ent != NULL; ent = ent->next)
10342 if (ent->plt.refcount > 0)
10343 {
10344 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10345 {
10346 s = htab->elf.iplt;
10347 ent->plt.offset = s->size;
10348 s->size += PLT_ENTRY_SIZE (htab);
10349 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10350 }
10351 else if (htab->can_convert_all_inline_plt
10352 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10353 ent->plt.offset = (bfd_vma) -1;
10354 else
10355 {
10356 s = htab->pltlocal;
10357 ent->plt.offset = s->size;
10358 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10359 if (bfd_link_pic (info)
10360 && !(info->enable_dt_relr && !htab->opd_abi))
10361 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10362 }
10363 }
10364 else
10365 ent->plt.offset = (bfd_vma) -1;
10366 }
10367 }
10368
10369 /* Allocate global sym .plt and .got entries, and space for global
10370 sym dynamic relocs. */
10371 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10372
10373 if (!htab->opd_abi && !bfd_link_pic (info))
10374 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10375
10376 first_tlsld = NULL;
10377 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10378 {
10379 struct got_entry *ent;
10380
10381 if (!is_ppc64_elf (ibfd))
10382 continue;
10383
10384 ent = ppc64_tlsld_got (ibfd);
10385 if (ent->got.refcount > 0)
10386 {
10387 if (!htab->do_multi_toc && first_tlsld != NULL)
10388 {
10389 ent->is_indirect = true;
10390 ent->got.ent = first_tlsld;
10391 }
10392 else
10393 {
10394 if (first_tlsld == NULL)
10395 first_tlsld = ent;
10396 s = ppc64_elf_tdata (ibfd)->got;
10397 ent->got.offset = s->size;
10398 ent->owner = ibfd;
10399 s->size += 16;
10400 if (bfd_link_dll (info))
10401 {
10402 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10403 srel->size += sizeof (Elf64_External_Rela);
10404 }
10405 }
10406 }
10407 else
10408 ent->got.offset = (bfd_vma) -1;
10409 }
10410
10411 /* We now have determined the sizes of the various dynamic sections.
10412 Allocate memory for them. */
10413 relocs = false;
10414 for (s = dynobj->sections; s != NULL; s = s->next)
10415 {
10416 if ((s->flags & SEC_LINKER_CREATED) == 0)
10417 continue;
10418
10419 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10420 /* These haven't been allocated yet; don't strip. */
10421 continue;
10422 else if (s == htab->elf.sgot
10423 || s == htab->elf.splt
10424 || s == htab->elf.iplt
10425 || s == htab->pltlocal
10426 || s == htab->glink
10427 || s == htab->global_entry
10428 || s == htab->elf.sdynbss
10429 || s == htab->elf.sdynrelro)
10430 {
10431 /* Strip this section if we don't need it; see the
10432 comment below. */
10433 }
10434 else if (s == htab->glink_eh_frame)
10435 {
10436 if (!bfd_is_abs_section (s->output_section))
10437 /* Not sized yet. */
10438 continue;
10439 }
10440 else if (startswith (s->name, ".rela"))
10441 {
10442 if (s->size != 0)
10443 {
10444 if (s != htab->elf.srelplt)
10445 relocs = true;
10446
10447 /* We use the reloc_count field as a counter if we need
10448 to copy relocs into the output file. */
10449 s->reloc_count = 0;
10450 }
10451 }
10452 else
10453 {
10454 /* It's not one of our sections, so don't allocate space. */
10455 continue;
10456 }
10457
10458 if (s->size == 0)
10459 {
10460 /* If we don't need this section, strip it from the
10461 output file. This is mostly to handle .rela.bss and
10462 .rela.plt. We must create both sections in
10463 create_dynamic_sections, because they must be created
10464 before the linker maps input sections to output
10465 sections. The linker does that before
10466 adjust_dynamic_symbol is called, and it is that
10467 function which decides whether anything needs to go
10468 into these sections. */
10469 s->flags |= SEC_EXCLUDE;
10470 continue;
10471 }
10472
10473 if (bfd_is_abs_section (s->output_section))
10474 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10475 s->name);
10476
10477 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10478 continue;
10479
10480 /* Allocate memory for the section contents. We use bfd_zalloc
10481 here in case unused entries are not reclaimed before the
10482 section's contents are written out. This should not happen,
10483 but this way if it does we get a R_PPC64_NONE reloc in .rela
10484 sections instead of garbage.
10485 We also rely on the section contents being zero when writing
10486 the GOT and .dynrelro. */
10487 s->contents = bfd_zalloc (dynobj, s->size);
10488 if (s->contents == NULL)
10489 return false;
10490 }
10491
10492 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10493 {
10494 if (!is_ppc64_elf (ibfd))
10495 continue;
10496
10497 s = ppc64_elf_tdata (ibfd)->got;
10498 if (s != NULL && s != htab->elf.sgot)
10499 {
10500 if (s->size == 0)
10501 s->flags |= SEC_EXCLUDE;
10502 else
10503 {
10504 s->contents = bfd_zalloc (ibfd, s->size);
10505 if (s->contents == NULL)
10506 return false;
10507 }
10508 }
10509 s = ppc64_elf_tdata (ibfd)->relgot;
10510 if (s != NULL)
10511 {
10512 if (s->size == 0)
10513 s->flags |= SEC_EXCLUDE;
10514 else
10515 {
10516 s->contents = bfd_zalloc (ibfd, s->size);
10517 if (s->contents == NULL)
10518 return false;
10519 relocs = true;
10520 s->reloc_count = 0;
10521 }
10522 }
10523 }
10524
10525 if (htab->elf.dynamic_sections_created)
10526 {
10527 bool tls_opt;
10528
10529 /* Add some entries to the .dynamic section. We fill in the
10530 values later, in ppc64_elf_finish_dynamic_sections, but we
10531 must add the entries now so that we get the correct size for
10532 the .dynamic section. The DT_DEBUG entry is filled in by the
10533 dynamic linker and used by the debugger. */
10534 #define add_dynamic_entry(TAG, VAL) \
10535 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10536
10537 if (bfd_link_executable (info))
10538 {
10539 if (!add_dynamic_entry (DT_DEBUG, 0))
10540 return false;
10541 }
10542
10543 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10544 {
10545 if (!add_dynamic_entry (DT_PLTGOT, 0)
10546 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10547 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10548 || !add_dynamic_entry (DT_JMPREL, 0)
10549 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10550 return false;
10551 }
10552
10553 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10554 {
10555 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10556 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10557 return false;
10558 }
10559
10560 tls_opt = (htab->params->tls_get_addr_opt
10561 && ((htab->tls_get_addr_fd != NULL
10562 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10563 || (htab->tga_desc_fd != NULL
10564 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10565 if (tls_opt || !htab->opd_abi)
10566 {
10567 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10568 return false;
10569 }
10570
10571 if (relocs)
10572 {
10573 if (!add_dynamic_entry (DT_RELA, 0)
10574 || !add_dynamic_entry (DT_RELASZ, 0)
10575 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10576 return false;
10577
10578 /* If any dynamic relocs apply to a read-only section,
10579 then we need a DT_TEXTREL entry. */
10580 if ((info->flags & DF_TEXTREL) == 0)
10581 elf_link_hash_traverse (&htab->elf,
10582 _bfd_elf_maybe_set_textrel, info);
10583
10584 if ((info->flags & DF_TEXTREL) != 0)
10585 {
10586 if (!add_dynamic_entry (DT_TEXTREL, 0))
10587 return false;
10588 }
10589 }
10590 }
10591 #undef add_dynamic_entry
10592
10593 return true;
10594 }
10595
10596 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10597
10598 static bool
10599 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10600 {
10601 if (h->plt.plist != NULL
10602 && !h->def_regular
10603 && !h->pointer_equality_needed)
10604 return false;
10605
10606 return _bfd_elf_hash_symbol (h);
10607 }
10608
10609 /* Determine the type of stub needed, if any, for a call. */
10610
10611 static inline enum ppc_stub_main_type
10612 ppc_type_of_stub (asection *input_sec,
10613 const Elf_Internal_Rela *rel,
10614 struct ppc_link_hash_entry **hash,
10615 struct plt_entry **plt_ent,
10616 bfd_vma destination,
10617 unsigned long local_off)
10618 {
10619 struct ppc_link_hash_entry *h = *hash;
10620 bfd_vma location;
10621 bfd_vma branch_offset;
10622 bfd_vma max_branch_offset;
10623 enum elf_ppc64_reloc_type r_type;
10624
10625 if (h != NULL)
10626 {
10627 struct plt_entry *ent;
10628 struct ppc_link_hash_entry *fdh = h;
10629 if (h->oh != NULL
10630 && h->oh->is_func_descriptor)
10631 {
10632 fdh = ppc_follow_link (h->oh);
10633 *hash = fdh;
10634 }
10635
10636 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10637 if (ent->addend == rel->r_addend
10638 && ent->plt.offset != (bfd_vma) -1)
10639 {
10640 *plt_ent = ent;
10641 return ppc_stub_plt_call;
10642 }
10643
10644 /* Here, we know we don't have a plt entry. If we don't have a
10645 either a defined function descriptor or a defined entry symbol
10646 in a regular object file, then it is pointless trying to make
10647 any other type of stub. */
10648 if (!is_static_defined (&fdh->elf)
10649 && !is_static_defined (&h->elf))
10650 return ppc_stub_none;
10651 }
10652 else if (elf_local_got_ents (input_sec->owner) != NULL)
10653 {
10654 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10655 struct plt_entry **local_plt = (struct plt_entry **)
10656 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10657 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10658
10659 if (local_plt[r_symndx] != NULL)
10660 {
10661 struct plt_entry *ent;
10662
10663 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10664 if (ent->addend == rel->r_addend
10665 && ent->plt.offset != (bfd_vma) -1)
10666 {
10667 *plt_ent = ent;
10668 return ppc_stub_plt_call;
10669 }
10670 }
10671 }
10672
10673 /* Determine where the call point is. */
10674 location = (input_sec->output_offset
10675 + input_sec->output_section->vma
10676 + rel->r_offset);
10677
10678 branch_offset = destination - location;
10679 r_type = ELF64_R_TYPE (rel->r_info);
10680
10681 /* Determine if a long branch stub is needed. */
10682 max_branch_offset = 1 << 25;
10683 if (r_type == R_PPC64_REL14
10684 || r_type == R_PPC64_REL14_BRTAKEN
10685 || r_type == R_PPC64_REL14_BRNTAKEN)
10686 max_branch_offset = 1 << 15;
10687
10688 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10689 /* We need a stub. Figure out whether a long_branch or plt_branch
10690 is needed later. */
10691 return ppc_stub_long_branch;
10692
10693 return ppc_stub_none;
10694 }
10695
10696 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10697 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10698 . mflr %r12
10699 . bcl 20,31,1f
10700 .1: mflr %r11
10701 . mtlr %r12
10702 . lis %r12,xxx-1b@highest
10703 . ori %r12,%r12,xxx-1b@higher
10704 . sldi %r12,%r12,32
10705 . oris %r12,%r12,xxx-1b@high
10706 . ori %r12,%r12,xxx-1b@l
10707 . add/ldx %r12,%r11,%r12 */
10708
10709 static bfd_byte *
10710 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10711 {
10712 bfd_put_32 (abfd, MFLR_R12, p);
10713 p += 4;
10714 bfd_put_32 (abfd, BCL_20_31, p);
10715 p += 4;
10716 bfd_put_32 (abfd, MFLR_R11, p);
10717 p += 4;
10718 bfd_put_32 (abfd, MTLR_R12, p);
10719 p += 4;
10720 if (off + 0x8000 < 0x10000)
10721 {
10722 if (load)
10723 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10724 else
10725 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10726 p += 4;
10727 }
10728 else if (off + 0x80008000ULL < 0x100000000ULL)
10729 {
10730 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10731 p += 4;
10732 if (load)
10733 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10734 else
10735 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10736 p += 4;
10737 }
10738 else
10739 {
10740 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10741 {
10742 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10743 p += 4;
10744 }
10745 else
10746 {
10747 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10748 p += 4;
10749 if (((off >> 32) & 0xffff) != 0)
10750 {
10751 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10752 p += 4;
10753 }
10754 }
10755 if (((off >> 32) & 0xffffffffULL) != 0)
10756 {
10757 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10758 p += 4;
10759 }
10760 if (PPC_HI (off) != 0)
10761 {
10762 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10763 p += 4;
10764 }
10765 if (PPC_LO (off) != 0)
10766 {
10767 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10768 p += 4;
10769 }
10770 if (load)
10771 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10772 else
10773 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10774 p += 4;
10775 }
10776 return p;
10777 }
10778
10779 static unsigned int
10780 size_offset (bfd_vma off)
10781 {
10782 unsigned int size;
10783 if (off + 0x8000 < 0x10000)
10784 size = 4;
10785 else if (off + 0x80008000ULL < 0x100000000ULL)
10786 size = 8;
10787 else
10788 {
10789 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10790 size = 4;
10791 else
10792 {
10793 size = 4;
10794 if (((off >> 32) & 0xffff) != 0)
10795 size += 4;
10796 }
10797 if (((off >> 32) & 0xffffffffULL) != 0)
10798 size += 4;
10799 if (PPC_HI (off) != 0)
10800 size += 4;
10801 if (PPC_LO (off) != 0)
10802 size += 4;
10803 size += 4;
10804 }
10805 return size + 16;
10806 }
10807
10808 static unsigned int
10809 num_relocs_for_offset (bfd_vma off)
10810 {
10811 unsigned int num_rel;
10812 if (off + 0x8000 < 0x10000)
10813 num_rel = 1;
10814 else if (off + 0x80008000ULL < 0x100000000ULL)
10815 num_rel = 2;
10816 else
10817 {
10818 num_rel = 1;
10819 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10820 && ((off >> 32) & 0xffff) != 0)
10821 num_rel += 1;
10822 if (PPC_HI (off) != 0)
10823 num_rel += 1;
10824 if (PPC_LO (off) != 0)
10825 num_rel += 1;
10826 }
10827 return num_rel;
10828 }
10829
10830 static Elf_Internal_Rela *
10831 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10832 bfd_vma roff, bfd_vma targ, bfd_vma off)
10833 {
10834 bfd_vma relative_targ = targ - (roff - 8);
10835 if (bfd_big_endian (info->output_bfd))
10836 roff += 2;
10837 r->r_offset = roff;
10838 r->r_addend = relative_targ + roff;
10839 if (off + 0x8000 < 0x10000)
10840 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10841 else if (off + 0x80008000ULL < 0x100000000ULL)
10842 {
10843 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10844 ++r;
10845 roff += 4;
10846 r->r_offset = roff;
10847 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10848 r->r_addend = relative_targ + roff;
10849 }
10850 else
10851 {
10852 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10853 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10854 else
10855 {
10856 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10857 if (((off >> 32) & 0xffff) != 0)
10858 {
10859 ++r;
10860 roff += 4;
10861 r->r_offset = roff;
10862 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10863 r->r_addend = relative_targ + roff;
10864 }
10865 }
10866 if (((off >> 32) & 0xffffffffULL) != 0)
10867 roff += 4;
10868 if (PPC_HI (off) != 0)
10869 {
10870 ++r;
10871 roff += 4;
10872 r->r_offset = roff;
10873 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10874 r->r_addend = relative_targ + roff;
10875 }
10876 if (PPC_LO (off) != 0)
10877 {
10878 ++r;
10879 roff += 4;
10880 r->r_offset = roff;
10881 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10882 r->r_addend = relative_targ + roff;
10883 }
10884 }
10885 return r;
10886 }
10887
10888 static bfd_byte *
10889 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10890 bool load)
10891 {
10892 uint64_t insn;
10893 if (off - odd + (1ULL << 33) < 1ULL << 34)
10894 {
10895 off -= odd;
10896 if (odd)
10897 {
10898 bfd_put_32 (abfd, NOP, p);
10899 p += 4;
10900 }
10901 if (load)
10902 insn = PLD_R12_PC;
10903 else
10904 insn = PADDI_R12_PC;
10905 insn |= D34 (off);
10906 bfd_put_32 (abfd, insn >> 32, p);
10907 p += 4;
10908 bfd_put_32 (abfd, insn, p);
10909 }
10910 /* The minimum value for paddi is -0x200000000. The minimum value
10911 for li is -0x8000, which when shifted by 34 and added gives a
10912 minimum value of -0x2000200000000. The maximum value is
10913 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10914 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10915 {
10916 off -= 8 - odd;
10917 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10918 p += 4;
10919 if (!odd)
10920 {
10921 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10922 p += 4;
10923 }
10924 insn = PADDI_R12_PC | D34 (off);
10925 bfd_put_32 (abfd, insn >> 32, p);
10926 p += 4;
10927 bfd_put_32 (abfd, insn, p);
10928 p += 4;
10929 if (odd)
10930 {
10931 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10932 p += 4;
10933 }
10934 if (load)
10935 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10936 else
10937 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10938 }
10939 else
10940 {
10941 off -= odd + 8;
10942 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10943 p += 4;
10944 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10945 p += 4;
10946 if (odd)
10947 {
10948 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10949 p += 4;
10950 }
10951 insn = PADDI_R12_PC | D34 (off);
10952 bfd_put_32 (abfd, insn >> 32, p);
10953 p += 4;
10954 bfd_put_32 (abfd, insn, p);
10955 p += 4;
10956 if (!odd)
10957 {
10958 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10959 p += 4;
10960 }
10961 if (load)
10962 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10963 else
10964 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10965 }
10966 p += 4;
10967 return p;
10968 }
10969
10970 static unsigned int
10971 size_power10_offset (bfd_vma off, int odd)
10972 {
10973 if (off - odd + (1ULL << 33) < 1ULL << 34)
10974 return odd + 8;
10975 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10976 return 20;
10977 else
10978 return 24;
10979 }
10980
10981 static unsigned int
10982 num_relocs_for_power10_offset (bfd_vma off, int odd)
10983 {
10984 if (off - odd + (1ULL << 33) < 1ULL << 34)
10985 return 1;
10986 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10987 return 2;
10988 else
10989 return 3;
10990 }
10991
10992 static Elf_Internal_Rela *
10993 emit_relocs_for_power10_offset (struct bfd_link_info *info,
10994 Elf_Internal_Rela *r, bfd_vma roff,
10995 bfd_vma targ, bfd_vma off, int odd)
10996 {
10997 if (off - odd + (1ULL << 33) < 1ULL << 34)
10998 roff += odd;
10999 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11000 {
11001 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11002 r->r_offset = roff + d_offset;
11003 r->r_addend = targ + 8 - odd - d_offset;
11004 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11005 ++r;
11006 roff += 8 - odd;
11007 }
11008 else
11009 {
11010 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11011 r->r_offset = roff + d_offset;
11012 r->r_addend = targ + 8 + odd - d_offset;
11013 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11014 ++r;
11015 roff += 4;
11016 r->r_offset = roff + d_offset;
11017 r->r_addend = targ + 4 + odd - d_offset;
11018 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11019 ++r;
11020 roff += 4 + odd;
11021 }
11022 r->r_offset = roff;
11023 r->r_addend = targ;
11024 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11025 return r;
11026 }
11027
11028 /* Emit .eh_frame opcode to advance pc by DELTA. */
11029
11030 static bfd_byte *
11031 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11032 {
11033 delta /= 4;
11034 if (delta < 64)
11035 *eh++ = DW_CFA_advance_loc + delta;
11036 else if (delta < 256)
11037 {
11038 *eh++ = DW_CFA_advance_loc1;
11039 *eh++ = delta;
11040 }
11041 else if (delta < 65536)
11042 {
11043 *eh++ = DW_CFA_advance_loc2;
11044 bfd_put_16 (abfd, delta, eh);
11045 eh += 2;
11046 }
11047 else
11048 {
11049 *eh++ = DW_CFA_advance_loc4;
11050 bfd_put_32 (abfd, delta, eh);
11051 eh += 4;
11052 }
11053 return eh;
11054 }
11055
11056 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11057
11058 static unsigned int
11059 eh_advance_size (unsigned int delta)
11060 {
11061 if (delta < 64 * 4)
11062 /* DW_CFA_advance_loc+[1..63]. */
11063 return 1;
11064 if (delta < 256 * 4)
11065 /* DW_CFA_advance_loc1, byte. */
11066 return 2;
11067 if (delta < 65536 * 4)
11068 /* DW_CFA_advance_loc2, 2 bytes. */
11069 return 3;
11070 /* DW_CFA_advance_loc4, 4 bytes. */
11071 return 5;
11072 }
11073
11074 /* With power7 weakly ordered memory model, it is possible for ld.so
11075 to update a plt entry in one thread and have another thread see a
11076 stale zero toc entry. To avoid this we need some sort of acquire
11077 barrier in the call stub. One solution is to make the load of the
11078 toc word seem to appear to depend on the load of the function entry
11079 word. Another solution is to test for r2 being zero, and branch to
11080 the appropriate glink entry if so.
11081
11082 . fake dep barrier compare
11083 . ld 12,xxx(2) ld 12,xxx(2)
11084 . mtctr 12 mtctr 12
11085 . xor 11,12,12 ld 2,xxx+8(2)
11086 . add 2,2,11 cmpldi 2,0
11087 . ld 2,xxx+8(2) bnectr+
11088 . bctr b <glink_entry>
11089
11090 The solution involving the compare turns out to be faster, so
11091 that's what we use unless the branch won't reach. */
11092
11093 #define ALWAYS_USE_FAKE_DEP 0
11094 #define ALWAYS_EMIT_R2SAVE 0
11095
11096 static inline unsigned int
11097 plt_stub_size (struct ppc_link_hash_table *htab,
11098 struct ppc_stub_hash_entry *stub_entry,
11099 bfd_vma off,
11100 unsigned int odd)
11101 {
11102 unsigned size;
11103
11104 if (stub_entry->type.sub == ppc_stub_notoc)
11105 {
11106 size = 8 + size_power10_offset (off, odd);
11107 if (stub_entry->type.r2save)
11108 size += 4;
11109 }
11110 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11111 {
11112 size = 8 + size_offset (off - 8);
11113 if (stub_entry->type.r2save)
11114 size += 4;
11115 }
11116 else
11117 {
11118 size = 12;
11119 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11120 size += 4;
11121 if (PPC_HA (off) != 0)
11122 size += 4;
11123 if (htab->opd_abi)
11124 {
11125 size += 4;
11126 if (htab->params->plt_static_chain)
11127 size += 4;
11128 if (htab->params->plt_thread_safe
11129 && htab->elf.dynamic_sections_created
11130 && stub_entry->h != NULL
11131 && stub_entry->h->elf.dynindx != -1)
11132 size += 8;
11133 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11134 != PPC_HA (off))
11135 size += 4;
11136 }
11137 }
11138 if (stub_entry->h != NULL
11139 && is_tls_get_addr (&stub_entry->h->elf, htab)
11140 && htab->params->tls_get_addr_opt)
11141 {
11142 if (!htab->params->no_tls_get_addr_regsave)
11143 {
11144 size += 30 * 4;
11145 if (stub_entry->type.r2save)
11146 size += 4;
11147 }
11148 else
11149 {
11150 size += 7 * 4;
11151 if (stub_entry->type.r2save)
11152 size += 6 * 4;
11153 }
11154 }
11155 return size;
11156 }
11157
11158 /* Depending on the sign of plt_stub_align:
11159 If positive, return the padding to align to a 2**plt_stub_align
11160 boundary.
11161 If negative, if this stub would cross fewer 2**plt_stub_align
11162 boundaries if we align, then return the padding needed to do so. */
11163
11164 static inline unsigned int
11165 plt_stub_pad (struct ppc_link_hash_table *htab,
11166 struct ppc_stub_hash_entry *stub_entry,
11167 bfd_vma plt_off,
11168 unsigned int odd)
11169 {
11170 int stub_align;
11171 unsigned stub_size;
11172 bfd_vma stub_off = stub_entry->group->stub_sec->size;
11173
11174 if (htab->params->plt_stub_align >= 0)
11175 {
11176 stub_align = 1 << htab->params->plt_stub_align;
11177 if ((stub_off & (stub_align - 1)) != 0)
11178 return stub_align - (stub_off & (stub_align - 1));
11179 return 0;
11180 }
11181
11182 stub_align = 1 << -htab->params->plt_stub_align;
11183 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
11184 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11185 > ((stub_size - 1) & -stub_align))
11186 return stub_align - (stub_off & (stub_align - 1));
11187 return 0;
11188 }
11189
11190 /* Build a toc using .plt call stub. */
11191
11192 static inline bfd_byte *
11193 build_plt_stub (struct ppc_link_hash_table *htab,
11194 struct ppc_stub_hash_entry *stub_entry,
11195 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11196 {
11197 bfd *obfd = htab->params->stub_bfd;
11198 bool plt_load_toc = htab->opd_abi;
11199 bool plt_static_chain = htab->params->plt_static_chain;
11200 bool plt_thread_safe = (htab->params->plt_thread_safe
11201 && htab->elf.dynamic_sections_created
11202 && stub_entry->h != NULL
11203 && stub_entry->h->elf.dynindx != -1);
11204 bool use_fake_dep = plt_thread_safe;
11205 bfd_vma cmp_branch_off = 0;
11206
11207 if (!ALWAYS_USE_FAKE_DEP
11208 && plt_load_toc
11209 && plt_thread_safe
11210 && !(stub_entry->h != NULL
11211 && is_tls_get_addr (&stub_entry->h->elf, htab)
11212 && htab->params->tls_get_addr_opt))
11213 {
11214 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11215 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11216 / PLT_ENTRY_SIZE (htab));
11217 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11218 bfd_vma to, from;
11219
11220 if (pltindex > 32768)
11221 glinkoff += (pltindex - 32768) * 4;
11222 to = (glinkoff
11223 + htab->glink->output_offset
11224 + htab->glink->output_section->vma);
11225 from = (p - stub_entry->group->stub_sec->contents
11226 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11227 + 4 * (PPC_HA (offset) != 0)
11228 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11229 != PPC_HA (offset))
11230 + 4 * (plt_static_chain != 0)
11231 + 20
11232 + stub_entry->group->stub_sec->output_offset
11233 + stub_entry->group->stub_sec->output_section->vma);
11234 cmp_branch_off = to - from;
11235 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11236 }
11237
11238 if (PPC_HA (offset) != 0)
11239 {
11240 if (r != NULL)
11241 {
11242 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11243 r[0].r_offset += 4;
11244 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11245 r[1].r_offset = r[0].r_offset + 4;
11246 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11247 r[1].r_addend = r[0].r_addend;
11248 if (plt_load_toc)
11249 {
11250 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11251 {
11252 r[2].r_offset = r[1].r_offset + 4;
11253 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11254 r[2].r_addend = r[0].r_addend;
11255 }
11256 else
11257 {
11258 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11259 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11260 r[2].r_addend = r[0].r_addend + 8;
11261 if (plt_static_chain)
11262 {
11263 r[3].r_offset = r[2].r_offset + 4;
11264 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11265 r[3].r_addend = r[0].r_addend + 16;
11266 }
11267 }
11268 }
11269 }
11270 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11271 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11272 if (plt_load_toc)
11273 {
11274 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11275 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11276 }
11277 else
11278 {
11279 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11280 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11281 }
11282 if (plt_load_toc
11283 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11284 {
11285 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11286 offset = 0;
11287 }
11288 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11289 if (plt_load_toc)
11290 {
11291 if (use_fake_dep)
11292 {
11293 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11294 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11295 }
11296 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11297 if (plt_static_chain)
11298 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11299 }
11300 }
11301 else
11302 {
11303 if (r != NULL)
11304 {
11305 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11306 r[0].r_offset += 4;
11307 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11308 if (plt_load_toc)
11309 {
11310 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11311 {
11312 r[1].r_offset = r[0].r_offset + 4;
11313 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11314 r[1].r_addend = r[0].r_addend;
11315 }
11316 else
11317 {
11318 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11319 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11320 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11321 if (plt_static_chain)
11322 {
11323 r[2].r_offset = r[1].r_offset + 4;
11324 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11325 r[2].r_addend = r[0].r_addend + 8;
11326 }
11327 }
11328 }
11329 }
11330 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11331 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11332 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11333 if (plt_load_toc
11334 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11335 {
11336 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11337 offset = 0;
11338 }
11339 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11340 if (plt_load_toc)
11341 {
11342 if (use_fake_dep)
11343 {
11344 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11345 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11346 }
11347 if (plt_static_chain)
11348 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11349 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11350 }
11351 }
11352 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11353 {
11354 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11355 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11356 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11357 }
11358 else
11359 bfd_put_32 (obfd, BCTR, p), p += 4;
11360 return p;
11361 }
11362
11363 /* Build a special .plt call stub for __tls_get_addr. */
11364
11365 #define LD_R0_0R3 0xe8030000
11366 #define LD_R12_0R3 0xe9830000
11367 #define MR_R0_R3 0x7c601b78
11368 #define CMPDI_R0_0 0x2c200000
11369 #define ADD_R3_R12_R13 0x7c6c6a14
11370 #define BEQLR 0x4d820020
11371 #define MR_R3_R0 0x7c030378
11372 #define BCTRL 0x4e800421
11373
11374 static bfd_byte *
11375 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11376 struct ppc_stub_hash_entry *stub_entry,
11377 bfd_byte *p)
11378 {
11379 bfd *obfd = htab->params->stub_bfd;
11380
11381 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11382 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11383 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11384 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11385 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11386 bfd_put_32 (obfd, BEQLR, p), p += 4;
11387 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11388
11389 if (!htab->params->no_tls_get_addr_regsave)
11390 p = tls_get_addr_prologue (obfd, p, htab);
11391 else if (stub_entry->type.r2save)
11392 {
11393 bfd_put_32 (obfd, MFLR_R0, p);
11394 p += 4;
11395 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11396 p += 4;
11397 }
11398 return p;
11399 }
11400
11401 static bfd_byte *
11402 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11403 struct ppc_stub_hash_entry *stub_entry,
11404 bfd_byte *p,
11405 bfd_byte *loc)
11406 {
11407 bfd *obfd = htab->params->stub_bfd;
11408
11409 if (!htab->params->no_tls_get_addr_regsave)
11410 {
11411 bfd_put_32 (obfd, BCTRL, p - 4);
11412
11413 if (stub_entry->type.r2save)
11414 {
11415 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11416 p += 4;
11417 }
11418 p = tls_get_addr_epilogue (obfd, p, htab);
11419 }
11420 else if (stub_entry->type.r2save)
11421 {
11422 bfd_put_32 (obfd, BCTRL, p - 4);
11423
11424 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11425 p += 4;
11426 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11427 p += 4;
11428 bfd_put_32 (obfd, MTLR_R0, p);
11429 p += 4;
11430 bfd_put_32 (obfd, BLR, p);
11431 p += 4;
11432 }
11433
11434 if (htab->glink_eh_frame != NULL
11435 && htab->glink_eh_frame->size != 0)
11436 {
11437 bfd_byte *base, *eh;
11438
11439 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11440 eh = base + stub_entry->group->eh_size;
11441
11442 if (!htab->params->no_tls_get_addr_regsave)
11443 {
11444 unsigned int cfa_updt, delta, i;
11445
11446 /* After the bctrl, lr has been modified so we need to emit
11447 .eh_frame info saying the return address is on the stack. In
11448 fact we must put the EH info at or before the call rather
11449 than after it, because the EH info for a call needs to be
11450 specified by that point.
11451 See libgcc/unwind-dw2.c execute_cfa_program.
11452 Any stack pointer update must be described immediately after
11453 the instruction making the change, and since the stdu occurs
11454 after saving regs we put all the reg saves and the cfa
11455 change there. */
11456 cfa_updt = stub_entry->stub_offset + 18 * 4;
11457 delta = cfa_updt - stub_entry->group->lr_restore;
11458 stub_entry->group->lr_restore
11459 = stub_entry->stub_offset + (p - loc) - 4;
11460 eh = eh_advance (htab->elf.dynobj, eh, delta);
11461 *eh++ = DW_CFA_def_cfa_offset;
11462 if (htab->opd_abi)
11463 {
11464 *eh++ = 128;
11465 *eh++ = 1;
11466 }
11467 else
11468 *eh++ = 96;
11469 *eh++ = DW_CFA_offset_extended_sf;
11470 *eh++ = 65;
11471 *eh++ = (-16 / 8) & 0x7f;
11472 for (i = 4; i < 12; i++)
11473 {
11474 *eh++ = DW_CFA_offset + i;
11475 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11476 }
11477 *eh++ = (DW_CFA_advance_loc
11478 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11479 *eh++ = DW_CFA_def_cfa_offset;
11480 *eh++ = 0;
11481 for (i = 4; i < 12; i++)
11482 *eh++ = DW_CFA_restore + i;
11483 *eh++ = DW_CFA_advance_loc + 2;
11484 *eh++ = DW_CFA_restore_extended;
11485 *eh++ = 65;
11486 stub_entry->group->eh_size = eh - base;
11487 }
11488 else if (stub_entry->type.r2save)
11489 {
11490 unsigned int lr_used, delta;
11491
11492 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11493 delta = lr_used - stub_entry->group->lr_restore;
11494 stub_entry->group->lr_restore = lr_used + 16;
11495 eh = eh_advance (htab->elf.dynobj, eh, delta);
11496 *eh++ = DW_CFA_offset_extended_sf;
11497 *eh++ = 65;
11498 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11499 *eh++ = DW_CFA_advance_loc + 4;
11500 *eh++ = DW_CFA_restore_extended;
11501 *eh++ = 65;
11502 stub_entry->group->eh_size = eh - base;
11503 }
11504 }
11505 return p;
11506 }
11507
11508 static Elf_Internal_Rela *
11509 get_relocs (asection *sec, int count)
11510 {
11511 Elf_Internal_Rela *relocs;
11512 struct bfd_elf_section_data *elfsec_data;
11513
11514 elfsec_data = elf_section_data (sec);
11515 relocs = elfsec_data->relocs;
11516 if (relocs == NULL)
11517 {
11518 bfd_size_type relsize;
11519 relsize = sec->reloc_count * sizeof (*relocs);
11520 relocs = bfd_alloc (sec->owner, relsize);
11521 if (relocs == NULL)
11522 return NULL;
11523 elfsec_data->relocs = relocs;
11524 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11525 sizeof (Elf_Internal_Shdr));
11526 if (elfsec_data->rela.hdr == NULL)
11527 return NULL;
11528 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11529 * sizeof (Elf64_External_Rela));
11530 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11531 sec->reloc_count = 0;
11532 }
11533 relocs += sec->reloc_count;
11534 sec->reloc_count += count;
11535 return relocs;
11536 }
11537
11538 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11539 forms, to the equivalent relocs against the global symbol given by
11540 STUB_ENTRY->H. */
11541
11542 static bool
11543 use_global_in_relocs (struct ppc_link_hash_table *htab,
11544 struct ppc_stub_hash_entry *stub_entry,
11545 Elf_Internal_Rela *r, unsigned int num_rel)
11546 {
11547 struct elf_link_hash_entry **hashes;
11548 unsigned long symndx;
11549 struct ppc_link_hash_entry *h;
11550 bfd_vma symval;
11551
11552 /* Relocs are always against symbols in their own object file. Fake
11553 up global sym hashes for the stub bfd (which has no symbols). */
11554 hashes = elf_sym_hashes (htab->params->stub_bfd);
11555 if (hashes == NULL)
11556 {
11557 bfd_size_type hsize;
11558
11559 /* When called the first time, stub_globals will contain the
11560 total number of symbols seen during stub sizing. After
11561 allocating, stub_globals is used as an index to fill the
11562 hashes array. */
11563 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11564 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11565 if (hashes == NULL)
11566 return false;
11567 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11568 htab->stub_globals = 1;
11569 }
11570 symndx = htab->stub_globals++;
11571 h = stub_entry->h;
11572 hashes[symndx] = &h->elf;
11573 if (h->oh != NULL && h->oh->is_func)
11574 h = ppc_follow_link (h->oh);
11575 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11576 || h->elf.root.type == bfd_link_hash_defweak);
11577 symval = defined_sym_val (&h->elf);
11578 while (num_rel-- != 0)
11579 {
11580 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11581 if (h->elf.root.u.def.section != stub_entry->target_section)
11582 {
11583 /* H is an opd symbol. The addend must be zero, and the
11584 branch reloc is the only one we can convert. */
11585 r->r_addend = 0;
11586 break;
11587 }
11588 else
11589 r->r_addend -= symval;
11590 --r;
11591 }
11592 return true;
11593 }
11594
11595 static bfd_vma
11596 get_r2off (struct bfd_link_info *info,
11597 struct ppc_stub_hash_entry *stub_entry)
11598 {
11599 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11600 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11601
11602 if (r2off == 0)
11603 {
11604 /* Support linking -R objects. Get the toc pointer from the
11605 opd entry. */
11606 char buf[8];
11607 if (!htab->opd_abi)
11608 return r2off;
11609 asection *opd = stub_entry->h->elf.root.u.def.section;
11610 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11611
11612 if (strcmp (opd->name, ".opd") != 0
11613 || opd->reloc_count != 0)
11614 {
11615 info->callbacks->einfo
11616 (_("%P: cannot find opd entry toc for `%pT'\n"),
11617 stub_entry->h->elf.root.root.string);
11618 bfd_set_error (bfd_error_bad_value);
11619 return (bfd_vma) -1;
11620 }
11621 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11622 return (bfd_vma) -1;
11623 r2off = bfd_get_64 (opd->owner, buf);
11624 r2off -= elf_gp (info->output_bfd);
11625 }
11626 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11627 return r2off;
11628 }
11629
11630 static bool
11631 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11632 {
11633 struct ppc_stub_hash_entry *stub_entry;
11634 struct ppc_branch_hash_entry *br_entry;
11635 struct bfd_link_info *info;
11636 struct ppc_link_hash_table *htab;
11637 bfd *obfd;
11638 bfd_byte *loc;
11639 bfd_byte *p, *relp;
11640 bfd_vma targ, off;
11641 Elf_Internal_Rela *r;
11642 asection *plt;
11643 int num_rel;
11644 int odd;
11645 bool is_tga;
11646
11647 /* Massage our args to the form they really have. */
11648 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11649 info = in_arg;
11650
11651 /* Fail if the target section could not be assigned to an output
11652 section. The user should fix his linker script. */
11653 if (stub_entry->target_section != NULL
11654 && stub_entry->target_section->output_section == NULL
11655 && info->non_contiguous_regions)
11656 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11657 "Retry without --enable-non-contiguous-regions.\n"),
11658 stub_entry->target_section);
11659
11660 /* Same for the group. */
11661 if (stub_entry->group->stub_sec != NULL
11662 && stub_entry->group->stub_sec->output_section == NULL
11663 && info->non_contiguous_regions)
11664 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11665 "output section. Retry without "
11666 "--enable-non-contiguous-regions.\n"),
11667 stub_entry->group->stub_sec,
11668 stub_entry->target_section);
11669
11670 htab = ppc_hash_table (info);
11671 if (htab == NULL)
11672 return false;
11673
11674 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11675 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11676
11677 htab->stub_count[stub_entry->type.main - 1] += 1;
11678 if (stub_entry->type.main == ppc_stub_long_branch
11679 && stub_entry->type.sub == ppc_stub_toc)
11680 {
11681 /* Branches are relative. This is where we are going to. */
11682 targ = (stub_entry->target_value
11683 + stub_entry->target_section->output_offset
11684 + stub_entry->target_section->output_section->vma);
11685 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11686
11687 /* And this is where we are coming from. */
11688 off = (stub_entry->stub_offset
11689 + stub_entry->group->stub_sec->output_offset
11690 + stub_entry->group->stub_sec->output_section->vma);
11691 off = targ - off;
11692
11693 p = loc;
11694 obfd = htab->params->stub_bfd;
11695 if (stub_entry->type.r2save)
11696 {
11697 bfd_vma r2off = get_r2off (info, stub_entry);
11698
11699 if (r2off == (bfd_vma) -1)
11700 {
11701 htab->stub_error = true;
11702 return false;
11703 }
11704 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11705 p += 4;
11706 if (PPC_HA (r2off) != 0)
11707 {
11708 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11709 p += 4;
11710 }
11711 if (PPC_LO (r2off) != 0)
11712 {
11713 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11714 p += 4;
11715 }
11716 off -= p - loc;
11717 }
11718 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11719 p += 4;
11720
11721 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11722 {
11723 _bfd_error_handler
11724 (_("long branch stub `%s' offset overflow"),
11725 stub_entry->root.string);
11726 htab->stub_error = true;
11727 return false;
11728 }
11729
11730 if (info->emitrelocations)
11731 {
11732 r = get_relocs (stub_entry->group->stub_sec, 1);
11733 if (r == NULL)
11734 return false;
11735 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11736 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11737 r->r_addend = targ;
11738 if (stub_entry->h != NULL
11739 && !use_global_in_relocs (htab, stub_entry, r, 1))
11740 return false;
11741 }
11742 }
11743 else if (stub_entry->type.main == ppc_stub_plt_branch
11744 && stub_entry->type.sub == ppc_stub_toc)
11745 {
11746 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11747 stub_entry->root.string + 9,
11748 false, false);
11749 if (br_entry == NULL)
11750 {
11751 _bfd_error_handler (_("can't find branch stub `%s'"),
11752 stub_entry->root.string);
11753 htab->stub_error = true;
11754 return false;
11755 }
11756
11757 targ = (stub_entry->target_value
11758 + stub_entry->target_section->output_offset
11759 + stub_entry->target_section->output_section->vma);
11760 if (!stub_entry->type.r2save)
11761 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11762
11763 bfd_put_64 (htab->brlt->owner, targ,
11764 htab->brlt->contents + br_entry->offset);
11765
11766 if (br_entry->iter == htab->stub_iteration)
11767 {
11768 br_entry->iter = 0;
11769
11770 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11771 {
11772 /* Create a reloc for the branch lookup table entry. */
11773 Elf_Internal_Rela rela;
11774 bfd_byte *rl;
11775
11776 rela.r_offset = (br_entry->offset
11777 + htab->brlt->output_offset
11778 + htab->brlt->output_section->vma);
11779 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11780 rela.r_addend = targ;
11781
11782 rl = htab->relbrlt->contents;
11783 rl += (htab->relbrlt->reloc_count++
11784 * sizeof (Elf64_External_Rela));
11785 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11786 }
11787 else if (info->emitrelocations)
11788 {
11789 r = get_relocs (htab->brlt, 1);
11790 if (r == NULL)
11791 return false;
11792 /* brlt, being SEC_LINKER_CREATED does not go through the
11793 normal reloc processing. Symbols and offsets are not
11794 translated from input file to output file form, so
11795 set up the offset per the output file. */
11796 r->r_offset = (br_entry->offset
11797 + htab->brlt->output_offset
11798 + htab->brlt->output_section->vma);
11799 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11800 r->r_addend = targ;
11801 }
11802 }
11803
11804 targ = (br_entry->offset
11805 + htab->brlt->output_offset
11806 + htab->brlt->output_section->vma);
11807
11808 off = (elf_gp (info->output_bfd)
11809 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11810 off = targ - off;
11811
11812 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11813 {
11814 info->callbacks->einfo
11815 (_("%P: linkage table error against `%pT'\n"),
11816 stub_entry->root.string);
11817 bfd_set_error (bfd_error_bad_value);
11818 htab->stub_error = true;
11819 return false;
11820 }
11821
11822 if (info->emitrelocations)
11823 {
11824 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11825 if (r == NULL)
11826 return false;
11827 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11828 if (bfd_big_endian (info->output_bfd))
11829 r[0].r_offset += 2;
11830 if (stub_entry->type.r2save)
11831 r[0].r_offset += 4;
11832 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11833 r[0].r_addend = targ;
11834 if (PPC_HA (off) != 0)
11835 {
11836 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11837 r[1].r_offset = r[0].r_offset + 4;
11838 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11839 r[1].r_addend = r[0].r_addend;
11840 }
11841 }
11842
11843 p = loc;
11844 obfd = htab->params->stub_bfd;
11845 if (!stub_entry->type.r2save)
11846 {
11847 if (PPC_HA (off) != 0)
11848 {
11849 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11850 p += 4;
11851 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11852 }
11853 else
11854 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11855 }
11856 else
11857 {
11858 bfd_vma r2off = get_r2off (info, stub_entry);
11859
11860 if (r2off == (bfd_vma) -1)
11861 {
11862 htab->stub_error = true;
11863 return false;
11864 }
11865
11866 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11867 p += 4;
11868 if (PPC_HA (off) != 0)
11869 {
11870 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11871 p += 4;
11872 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11873 }
11874 else
11875 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11876
11877 if (PPC_HA (r2off) != 0)
11878 {
11879 p += 4;
11880 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11881 }
11882 if (PPC_LO (r2off) != 0)
11883 {
11884 p += 4;
11885 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11886 }
11887 }
11888 p += 4;
11889 bfd_put_32 (obfd, MTCTR_R12, p);
11890 p += 4;
11891 bfd_put_32 (obfd, BCTR, p);
11892 p += 4;
11893 }
11894 else if (stub_entry->type.sub >= ppc_stub_notoc)
11895 {
11896 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
11897 p = loc;
11898 off = (stub_entry->stub_offset
11899 + stub_entry->group->stub_sec->output_offset
11900 + stub_entry->group->stub_sec->output_section->vma);
11901 obfd = htab->params->stub_bfd;
11902 is_tga = (is_plt
11903 && stub_entry->h != NULL
11904 && is_tls_get_addr (&stub_entry->h->elf, htab)
11905 && htab->params->tls_get_addr_opt);
11906 if (is_tga)
11907 {
11908 p = build_tls_get_addr_head (htab, stub_entry, p);
11909 off += p - loc;
11910 }
11911 if (stub_entry->type.r2save)
11912 {
11913 off += 4;
11914 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11915 p += 4;
11916 }
11917 if (is_plt)
11918 {
11919 targ = stub_entry->plt_ent->plt.offset & ~1;
11920 if (targ >= (bfd_vma) -2)
11921 abort ();
11922
11923 plt = htab->elf.splt;
11924 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11925 {
11926 if (stub_entry->symtype == STT_GNU_IFUNC)
11927 plt = htab->elf.iplt;
11928 else
11929 plt = htab->pltlocal;
11930 }
11931 targ += plt->output_offset + plt->output_section->vma;
11932 }
11933 else
11934 targ = (stub_entry->target_value
11935 + stub_entry->target_section->output_offset
11936 + stub_entry->target_section->output_section->vma);
11937 odd = off & 4;
11938 off = targ - off;
11939
11940 relp = p;
11941 num_rel = 0;
11942 if (stub_entry->type.sub == ppc_stub_notoc)
11943 p = build_power10_offset (obfd, p, off, odd, is_plt);
11944 else
11945 {
11946 if (htab->glink_eh_frame != NULL
11947 && htab->glink_eh_frame->size != 0)
11948 {
11949 bfd_byte *base, *eh;
11950 unsigned int lr_used, delta;
11951
11952 base = (htab->glink_eh_frame->contents
11953 + stub_entry->group->eh_base + 17);
11954 eh = base + stub_entry->group->eh_size;
11955 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11956 delta = lr_used - stub_entry->group->lr_restore;
11957 stub_entry->group->lr_restore = lr_used + 8;
11958 eh = eh_advance (htab->elf.dynobj, eh, delta);
11959 *eh++ = DW_CFA_register;
11960 *eh++ = 65;
11961 *eh++ = 12;
11962 *eh++ = DW_CFA_advance_loc + 2;
11963 *eh++ = DW_CFA_restore_extended;
11964 *eh++ = 65;
11965 stub_entry->group->eh_size = eh - base;
11966 }
11967
11968 /* The notoc stubs calculate their target (either a PLT entry or
11969 the global entry point of a function) relative to the PC
11970 returned by the "bcl" two instructions past the start of the
11971 sequence emitted by build_offset. The offset is therefore 8
11972 less than calculated from the start of the sequence. */
11973 off -= 8;
11974 p = build_offset (obfd, p, off, is_plt);
11975 }
11976
11977 if (stub_entry->type.main == ppc_stub_long_branch)
11978 {
11979 bfd_vma from;
11980 num_rel = 1;
11981 from = (stub_entry->stub_offset
11982 + stub_entry->group->stub_sec->output_offset
11983 + stub_entry->group->stub_sec->output_section->vma
11984 + (p - loc));
11985 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
11986 }
11987 else
11988 {
11989 bfd_put_32 (obfd, MTCTR_R12, p);
11990 p += 4;
11991 bfd_put_32 (obfd, BCTR, p);
11992 }
11993 p += 4;
11994
11995 if (is_tga)
11996 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11997
11998 if (info->emitrelocations)
11999 {
12000 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12001 if (stub_entry->type.sub == ppc_stub_notoc)
12002 num_rel += num_relocs_for_power10_offset (off, odd);
12003 else
12004 {
12005 num_rel += num_relocs_for_offset (off);
12006 roff += 16;
12007 }
12008 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12009 if (r == NULL)
12010 return false;
12011 if (stub_entry->type.sub == ppc_stub_notoc)
12012 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12013 else
12014 r = emit_relocs_for_offset (info, r, roff, targ, off);
12015 if (stub_entry->type.main == ppc_stub_long_branch)
12016 {
12017 ++r;
12018 roff = p - 4 - stub_entry->group->stub_sec->contents;
12019 r->r_offset = roff;
12020 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12021 r->r_addend = targ;
12022 if (stub_entry->h != NULL
12023 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12024 return false;
12025 }
12026 }
12027 }
12028 else if (stub_entry->type.main == ppc_stub_plt_call)
12029 {
12030 if (stub_entry->h != NULL
12031 && stub_entry->h->is_func_descriptor
12032 && stub_entry->h->oh != NULL)
12033 {
12034 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12035
12036 /* If the old-ABI "dot-symbol" is undefined make it weak so
12037 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12038 if (fh->elf.root.type == bfd_link_hash_undefined
12039 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12040 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12041 fh->elf.root.type = bfd_link_hash_undefweak;
12042 }
12043
12044 /* Now build the stub. */
12045 targ = stub_entry->plt_ent->plt.offset & ~1;
12046 if (targ >= (bfd_vma) -2)
12047 abort ();
12048
12049 plt = htab->elf.splt;
12050 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12051 {
12052 if (stub_entry->symtype == STT_GNU_IFUNC)
12053 plt = htab->elf.iplt;
12054 else
12055 plt = htab->pltlocal;
12056 }
12057 targ += plt->output_offset + plt->output_section->vma;
12058
12059 off = (elf_gp (info->output_bfd)
12060 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12061 off = targ - off;
12062
12063 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12064 {
12065 info->callbacks->einfo
12066 /* xgettext:c-format */
12067 (_("%P: linkage table error against `%pT'\n"),
12068 stub_entry->h != NULL
12069 ? stub_entry->h->elf.root.root.string
12070 : "<local sym>");
12071 bfd_set_error (bfd_error_bad_value);
12072 htab->stub_error = true;
12073 return false;
12074 }
12075
12076 r = NULL;
12077 if (info->emitrelocations)
12078 {
12079 r = get_relocs (stub_entry->group->stub_sec,
12080 ((PPC_HA (off) != 0)
12081 + (htab->opd_abi
12082 ? 2 + (htab->params->plt_static_chain
12083 && PPC_HA (off + 16) == PPC_HA (off))
12084 : 1)));
12085 if (r == NULL)
12086 return false;
12087 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12088 if (bfd_big_endian (info->output_bfd))
12089 r[0].r_offset += 2;
12090 r[0].r_addend = targ;
12091 }
12092 p = loc;
12093 obfd = htab->params->stub_bfd;
12094 is_tga = (stub_entry->h != NULL
12095 && is_tls_get_addr (&stub_entry->h->elf, htab)
12096 && htab->params->tls_get_addr_opt);
12097 if (is_tga)
12098 {
12099 p = build_tls_get_addr_head (htab, stub_entry, p);
12100 if (r != NULL)
12101 r[0].r_offset += p - loc;
12102 }
12103 p = build_plt_stub (htab, stub_entry, p, off, r);
12104 if (is_tga)
12105 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12106 }
12107 else if (stub_entry->type.main == ppc_stub_save_res)
12108 return true;
12109 else
12110 {
12111 BFD_FAIL ();
12112 return false;
12113 }
12114
12115 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12116
12117 if (htab->params->emit_stub_syms)
12118 {
12119 struct elf_link_hash_entry *h;
12120 size_t len1, len2;
12121 char *name;
12122 const char *const stub_str[] = { "long_branch",
12123 "plt_branch",
12124 "plt_call" };
12125
12126 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12127 len2 = strlen (stub_entry->root.string);
12128 name = bfd_malloc (len1 + len2 + 2);
12129 if (name == NULL)
12130 return false;
12131 memcpy (name, stub_entry->root.string, 9);
12132 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12133 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12134 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12135 if (h == NULL)
12136 return false;
12137 if (h->root.type == bfd_link_hash_new)
12138 {
12139 h->root.type = bfd_link_hash_defined;
12140 h->root.u.def.section = stub_entry->group->stub_sec;
12141 h->root.u.def.value = stub_entry->stub_offset;
12142 h->ref_regular = 1;
12143 h->def_regular = 1;
12144 h->ref_regular_nonweak = 1;
12145 h->forced_local = 1;
12146 h->non_elf = 0;
12147 h->root.linker_def = 1;
12148 }
12149 }
12150
12151 return true;
12152 }
12153
12154 /* As above, but don't actually build the stub. Just bump offset so
12155 we know stub section sizes, and select plt_branch stubs where
12156 long_branch stubs won't do. */
12157
12158 static bool
12159 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12160 {
12161 struct ppc_stub_hash_entry *stub_entry;
12162 struct bfd_link_info *info;
12163 struct ppc_link_hash_table *htab;
12164 asection *plt;
12165 bfd_vma targ, off, r2off;
12166 unsigned int size, extra, lr_used, delta, odd;
12167
12168 /* Massage our args to the form they really have. */
12169 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12170 info = in_arg;
12171
12172 htab = ppc_hash_table (info);
12173 if (htab == NULL)
12174 return false;
12175
12176 /* Fail if the target section could not be assigned to an output
12177 section. The user should fix his linker script. */
12178 if (stub_entry->target_section != NULL
12179 && stub_entry->target_section->output_section == NULL
12180 && info->non_contiguous_regions)
12181 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12182 "Retry without --enable-non-contiguous-regions.\n"),
12183 stub_entry->target_section);
12184
12185 /* Same for the group. */
12186 if (stub_entry->group->stub_sec != NULL
12187 && stub_entry->group->stub_sec->output_section == NULL
12188 && info->non_contiguous_regions)
12189 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12190 "output section. Retry without "
12191 "--enable-non-contiguous-regions.\n"),
12192 stub_entry->group->stub_sec,
12193 stub_entry->target_section);
12194
12195 /* Make a note of the offset within the stubs for this entry. */
12196 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12197
12198 if (stub_entry->h != NULL
12199 && stub_entry->h->save_res
12200 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12201 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12202 {
12203 /* Don't make stubs to out-of-line register save/restore
12204 functions. Instead, emit copies of the functions. */
12205 stub_entry->group->needs_save_res = 1;
12206 stub_entry->type.main = ppc_stub_save_res;
12207 stub_entry->type.sub = ppc_stub_toc;
12208 stub_entry->type.r2save = 0;
12209 return true;
12210 }
12211
12212 if (stub_entry->type.main == ppc_stub_plt_branch)
12213 {
12214 /* Reset the stub type from the plt branch variant in case we now
12215 can reach with a shorter stub. */
12216 stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
12217 }
12218
12219 if (stub_entry->type.main == ppc_stub_long_branch
12220 && stub_entry->type.sub == ppc_stub_toc)
12221 {
12222 targ = (stub_entry->target_value
12223 + stub_entry->target_section->output_offset
12224 + stub_entry->target_section->output_section->vma);
12225 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12226 off = (stub_entry->stub_offset
12227 + stub_entry->group->stub_sec->output_offset
12228 + stub_entry->group->stub_sec->output_section->vma);
12229
12230 size = 4;
12231 r2off = 0;
12232 if (stub_entry->type.r2save)
12233 {
12234 r2off = get_r2off (info, stub_entry);
12235 if (r2off == (bfd_vma) -1)
12236 {
12237 htab->stub_error = true;
12238 return false;
12239 }
12240 size = 8;
12241 if (PPC_HA (r2off) != 0)
12242 size += 4;
12243 if (PPC_LO (r2off) != 0)
12244 size += 4;
12245 off += size - 4;
12246 }
12247 off = targ - off;
12248
12249 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12250 Do the same for -R objects without function descriptors. */
12251 if ((stub_entry->type.r2save
12252 && r2off == 0
12253 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12254 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12255 {
12256 struct ppc_branch_hash_entry *br_entry;
12257
12258 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12259 stub_entry->root.string + 9,
12260 true, false);
12261 if (br_entry == NULL)
12262 {
12263 _bfd_error_handler (_("can't build branch stub `%s'"),
12264 stub_entry->root.string);
12265 htab->stub_error = true;
12266 return false;
12267 }
12268
12269 if (br_entry->iter != htab->stub_iteration)
12270 {
12271 br_entry->iter = htab->stub_iteration;
12272 br_entry->offset = htab->brlt->size;
12273 htab->brlt->size += 8;
12274
12275 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12276 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12277 else if (info->emitrelocations)
12278 {
12279 htab->brlt->reloc_count += 1;
12280 htab->brlt->flags |= SEC_RELOC;
12281 }
12282 }
12283
12284 targ = (br_entry->offset
12285 + htab->brlt->output_offset
12286 + htab->brlt->output_section->vma);
12287 off = (elf_gp (info->output_bfd)
12288 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12289 off = targ - off;
12290
12291 if (info->emitrelocations)
12292 {
12293 stub_entry->group->stub_sec->reloc_count
12294 += 1 + (PPC_HA (off) != 0);
12295 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12296 }
12297
12298 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12299 if (!stub_entry->type.r2save)
12300 {
12301 size = 12;
12302 if (PPC_HA (off) != 0)
12303 size = 16;
12304 }
12305 else
12306 {
12307 size = 16;
12308 if (PPC_HA (off) != 0)
12309 size += 4;
12310
12311 if (PPC_HA (r2off) != 0)
12312 size += 4;
12313 if (PPC_LO (r2off) != 0)
12314 size += 4;
12315 }
12316 }
12317 else if (info->emitrelocations)
12318 {
12319 stub_entry->group->stub_sec->reloc_count += 1;
12320 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12321 }
12322 }
12323 else if (stub_entry->type.main == ppc_stub_long_branch)
12324 {
12325 off = (stub_entry->stub_offset
12326 + stub_entry->group->stub_sec->output_offset
12327 + stub_entry->group->stub_sec->output_section->vma);
12328 size = 0;
12329 if (stub_entry->type.r2save)
12330 size = 4;
12331 off += size;
12332 targ = (stub_entry->target_value
12333 + stub_entry->target_section->output_offset
12334 + stub_entry->target_section->output_section->vma);
12335 odd = off & 4;
12336 off = targ - off;
12337
12338 if (info->emitrelocations)
12339 {
12340 unsigned int num_rel;
12341 if (stub_entry->type.sub == ppc_stub_notoc)
12342 num_rel = num_relocs_for_power10_offset (off, odd);
12343 else
12344 num_rel = num_relocs_for_offset (off - 8);
12345 stub_entry->group->stub_sec->reloc_count += num_rel;
12346 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12347 }
12348
12349 if (stub_entry->type.sub == ppc_stub_notoc)
12350 extra = size_power10_offset (off, odd);
12351 else
12352 extra = size_offset (off - 8);
12353 /* Include branch insn plus those in the offset sequence. */
12354 size += 4 + extra;
12355 /* The branch insn is at the end, or "extra" bytes along. So
12356 its offset will be "extra" bytes less that that already
12357 calculated. */
12358 off -= extra;
12359
12360 if (stub_entry->type.sub != ppc_stub_notoc)
12361 {
12362 /* After the bcl, lr has been modified so we need to emit
12363 .eh_frame info saying the return address is in r12. */
12364 lr_used = stub_entry->stub_offset + 8;
12365 if (stub_entry->type.r2save)
12366 lr_used += 4;
12367 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12368 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12369 DW_CFA_restore_extended 65. */
12370 delta = lr_used - stub_entry->group->lr_restore;
12371 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12372 stub_entry->group->lr_restore = lr_used + 8;
12373 }
12374
12375 /* If the branch can't reach, use a plt_branch. */
12376 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12377 {
12378 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12379 size += 4;
12380 }
12381 else if (info->emitrelocations)
12382 stub_entry->group->stub_sec->reloc_count +=1;
12383 }
12384 else if (stub_entry->type.sub >= ppc_stub_notoc)
12385 {
12386 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12387 lr_used = 0;
12388 if (stub_entry->h != NULL
12389 && is_tls_get_addr (&stub_entry->h->elf, htab)
12390 && htab->params->tls_get_addr_opt)
12391 {
12392 lr_used += 7 * 4;
12393 if (!htab->params->no_tls_get_addr_regsave)
12394 lr_used += 11 * 4;
12395 else if (stub_entry->type.r2save)
12396 lr_used += 2 * 4;
12397 }
12398 if (stub_entry->type.r2save)
12399 lr_used += 4;
12400 targ = stub_entry->plt_ent->plt.offset & ~1;
12401 if (targ >= (bfd_vma) -2)
12402 abort ();
12403
12404 plt = htab->elf.splt;
12405 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12406 {
12407 if (stub_entry->symtype == STT_GNU_IFUNC)
12408 plt = htab->elf.iplt;
12409 else
12410 plt = htab->pltlocal;
12411 }
12412 targ += plt->output_offset + plt->output_section->vma;
12413 off = (stub_entry->stub_offset
12414 + stub_entry->group->stub_sec->output_offset
12415 + stub_entry->group->stub_sec->output_section->vma
12416 + lr_used);
12417 odd = off & 4;
12418 off = targ - off;
12419
12420 if (htab->params->plt_stub_align != 0)
12421 {
12422 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
12423
12424 stub_entry->group->stub_sec->size += pad;
12425 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12426 off -= pad;
12427 odd ^= pad & 4;
12428 }
12429
12430 if (info->emitrelocations)
12431 {
12432 unsigned int num_rel;
12433 if (stub_entry->type.sub == ppc_stub_notoc)
12434 num_rel = num_relocs_for_power10_offset (off, odd);
12435 else
12436 num_rel = num_relocs_for_offset (off - 8);
12437 stub_entry->group->stub_sec->reloc_count += num_rel;
12438 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12439 }
12440
12441 size = plt_stub_size (htab, stub_entry, off, odd);
12442
12443 if (stub_entry->type.sub != ppc_stub_notoc)
12444 {
12445 /* After the bcl, lr has been modified so we need to emit
12446 .eh_frame info saying the return address is in r12. */
12447 lr_used += stub_entry->stub_offset + 8;
12448 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12449 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12450 DW_CFA_restore_extended 65. */
12451 delta = lr_used - stub_entry->group->lr_restore;
12452 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12453 stub_entry->group->lr_restore = lr_used + 8;
12454 }
12455 if (stub_entry->h != NULL
12456 && is_tls_get_addr (&stub_entry->h->elf, htab)
12457 && htab->params->tls_get_addr_opt)
12458 {
12459 if (!htab->params->no_tls_get_addr_regsave)
12460 {
12461 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12462 delta = cfa_updt - stub_entry->group->lr_restore;
12463 stub_entry->group->eh_size += eh_advance_size (delta);
12464 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12465 stub_entry->group->lr_restore
12466 = stub_entry->stub_offset + size - 4;
12467 }
12468 else if (stub_entry->type.r2save)
12469 {
12470 lr_used = stub_entry->stub_offset + size - 20;
12471 delta = lr_used - stub_entry->group->lr_restore;
12472 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12473 stub_entry->group->lr_restore
12474 = stub_entry->stub_offset + size - 4;
12475 }
12476 }
12477 }
12478 else if (stub_entry->type.main == ppc_stub_plt_call)
12479 {
12480 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12481 if (targ >= (bfd_vma) -2)
12482 abort ();
12483 plt = htab->elf.splt;
12484 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12485 {
12486 if (stub_entry->symtype == STT_GNU_IFUNC)
12487 plt = htab->elf.iplt;
12488 else
12489 plt = htab->pltlocal;
12490 }
12491 targ += plt->output_offset + plt->output_section->vma;
12492
12493 off = (elf_gp (info->output_bfd)
12494 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12495 off = targ - off;
12496
12497 if (htab->params->plt_stub_align != 0)
12498 {
12499 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
12500
12501 stub_entry->group->stub_sec->size += pad;
12502 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12503 }
12504
12505 if (info->emitrelocations)
12506 {
12507 stub_entry->group->stub_sec->reloc_count
12508 += ((PPC_HA (off) != 0)
12509 + (htab->opd_abi
12510 ? 2 + (htab->params->plt_static_chain
12511 && PPC_HA (off + 16) == PPC_HA (off))
12512 : 1));
12513 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12514 }
12515
12516 size = plt_stub_size (htab, stub_entry, off, 0);
12517
12518 if (stub_entry->h != NULL
12519 && is_tls_get_addr (&stub_entry->h->elf, htab)
12520 && htab->params->tls_get_addr_opt
12521 && stub_entry->type.r2save)
12522 {
12523 if (!htab->params->no_tls_get_addr_regsave)
12524 {
12525 /* Adjustments to r1 need to be described. */
12526 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12527 delta = cfa_updt - stub_entry->group->lr_restore;
12528 stub_entry->group->eh_size += eh_advance_size (delta);
12529 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12530 }
12531 else
12532 {
12533 lr_used = stub_entry->stub_offset + size - 20;
12534 /* The eh_frame info will consist of a DW_CFA_advance_loc
12535 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12536 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12537 delta = lr_used - stub_entry->group->lr_restore;
12538 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12539 }
12540 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
12541 }
12542 }
12543 else
12544 {
12545 BFD_FAIL ();
12546 return false;
12547 }
12548
12549 stub_entry->group->stub_sec->size += size;
12550 return true;
12551 }
12552
12553 /* Set up various things so that we can make a list of input sections
12554 for each output section included in the link. Returns -1 on error,
12555 0 when no stubs will be needed, and 1 on success. */
12556
12557 int
12558 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12559 {
12560 unsigned int id;
12561 size_t amt;
12562 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12563
12564 if (htab == NULL)
12565 return -1;
12566
12567 htab->sec_info_arr_size = _bfd_section_id;
12568 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12569 htab->sec_info = bfd_zmalloc (amt);
12570 if (htab->sec_info == NULL)
12571 return -1;
12572
12573 /* Set toc_off for com, und, abs and ind sections. */
12574 for (id = 0; id < 3; id++)
12575 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12576
12577 return 1;
12578 }
12579
12580 /* Set up for first pass at multitoc partitioning. */
12581
12582 void
12583 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12584 {
12585 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12586
12587 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12588 htab->toc_bfd = NULL;
12589 htab->toc_first_sec = NULL;
12590 }
12591
12592 /* The linker repeatedly calls this function for each TOC input section
12593 and linker generated GOT section. Group input bfds such that the toc
12594 within a group is less than 64k in size. */
12595
12596 bool
12597 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12598 {
12599 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12600 bfd_vma addr, off, limit;
12601
12602 if (htab == NULL)
12603 return false;
12604
12605 if (!htab->second_toc_pass)
12606 {
12607 /* Keep track of the first .toc or .got section for this input bfd. */
12608 bool new_bfd = htab->toc_bfd != isec->owner;
12609
12610 if (new_bfd)
12611 {
12612 htab->toc_bfd = isec->owner;
12613 htab->toc_first_sec = isec;
12614 }
12615
12616 addr = isec->output_offset + isec->output_section->vma;
12617 off = addr - htab->toc_curr;
12618 limit = 0x80008000;
12619 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12620 limit = 0x10000;
12621 if (off + isec->size > limit)
12622 {
12623 addr = (htab->toc_first_sec->output_offset
12624 + htab->toc_first_sec->output_section->vma);
12625 htab->toc_curr = addr;
12626 htab->toc_curr &= -TOC_BASE_ALIGN;
12627 }
12628
12629 /* toc_curr is the base address of this toc group. Set elf_gp
12630 for the input section to be the offset relative to the
12631 output toc base plus 0x8000. Making the input elf_gp an
12632 offset allows us to move the toc as a whole without
12633 recalculating input elf_gp. */
12634 off = htab->toc_curr - elf_gp (info->output_bfd);
12635 off += TOC_BASE_OFF;
12636
12637 /* Die if someone uses a linker script that doesn't keep input
12638 file .toc and .got together. */
12639 if (new_bfd
12640 && elf_gp (isec->owner) != 0
12641 && elf_gp (isec->owner) != off)
12642 return false;
12643
12644 elf_gp (isec->owner) = off;
12645 return true;
12646 }
12647
12648 /* During the second pass toc_first_sec points to the start of
12649 a toc group, and toc_curr is used to track the old elf_gp.
12650 We use toc_bfd to ensure we only look at each bfd once. */
12651 if (htab->toc_bfd == isec->owner)
12652 return true;
12653 htab->toc_bfd = isec->owner;
12654
12655 if (htab->toc_first_sec == NULL
12656 || htab->toc_curr != elf_gp (isec->owner))
12657 {
12658 htab->toc_curr = elf_gp (isec->owner);
12659 htab->toc_first_sec = isec;
12660 }
12661 addr = (htab->toc_first_sec->output_offset
12662 + htab->toc_first_sec->output_section->vma);
12663 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12664 elf_gp (isec->owner) = off;
12665
12666 return true;
12667 }
12668
12669 /* Called via elf_link_hash_traverse to merge GOT entries for global
12670 symbol H. */
12671
12672 static bool
12673 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12674 {
12675 if (h->root.type == bfd_link_hash_indirect)
12676 return true;
12677
12678 merge_got_entries (&h->got.glist);
12679
12680 return true;
12681 }
12682
12683 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12684 symbol H. */
12685
12686 static bool
12687 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12688 {
12689 struct got_entry *gent;
12690
12691 if (h->root.type == bfd_link_hash_indirect)
12692 return true;
12693
12694 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12695 if (!gent->is_indirect)
12696 allocate_got (h, (struct bfd_link_info *) inf, gent);
12697 return true;
12698 }
12699
12700 /* Called on the first multitoc pass after the last call to
12701 ppc64_elf_next_toc_section. This function removes duplicate GOT
12702 entries. */
12703
12704 bool
12705 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12706 {
12707 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12708 struct bfd *ibfd, *ibfd2;
12709 bool done_something;
12710
12711 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12712
12713 if (!htab->do_multi_toc)
12714 return false;
12715
12716 /* Merge global sym got entries within a toc group. */
12717 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12718
12719 /* And tlsld_got. */
12720 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12721 {
12722 struct got_entry *ent, *ent2;
12723
12724 if (!is_ppc64_elf (ibfd))
12725 continue;
12726
12727 ent = ppc64_tlsld_got (ibfd);
12728 if (!ent->is_indirect
12729 && ent->got.offset != (bfd_vma) -1)
12730 {
12731 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12732 {
12733 if (!is_ppc64_elf (ibfd2))
12734 continue;
12735
12736 ent2 = ppc64_tlsld_got (ibfd2);
12737 if (!ent2->is_indirect
12738 && ent2->got.offset != (bfd_vma) -1
12739 && elf_gp (ibfd2) == elf_gp (ibfd))
12740 {
12741 ent2->is_indirect = true;
12742 ent2->got.ent = ent;
12743 }
12744 }
12745 }
12746 }
12747
12748 /* Zap sizes of got sections. */
12749 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12750 htab->elf.irelplt->size -= htab->got_reli_size;
12751 htab->got_reli_size = 0;
12752
12753 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12754 {
12755 asection *got, *relgot;
12756
12757 if (!is_ppc64_elf (ibfd))
12758 continue;
12759
12760 got = ppc64_elf_tdata (ibfd)->got;
12761 if (got != NULL)
12762 {
12763 got->rawsize = got->size;
12764 got->size = 0;
12765 relgot = ppc64_elf_tdata (ibfd)->relgot;
12766 relgot->rawsize = relgot->size;
12767 relgot->size = 0;
12768 }
12769 }
12770
12771 /* Now reallocate the got, local syms first. We don't need to
12772 allocate section contents again since we never increase size. */
12773 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12774 {
12775 struct got_entry **lgot_ents;
12776 struct got_entry **end_lgot_ents;
12777 struct plt_entry **local_plt;
12778 struct plt_entry **end_local_plt;
12779 unsigned char *lgot_masks;
12780 bfd_size_type locsymcount;
12781 Elf_Internal_Shdr *symtab_hdr;
12782 asection *s;
12783
12784 if (!is_ppc64_elf (ibfd))
12785 continue;
12786
12787 lgot_ents = elf_local_got_ents (ibfd);
12788 if (!lgot_ents)
12789 continue;
12790
12791 symtab_hdr = &elf_symtab_hdr (ibfd);
12792 locsymcount = symtab_hdr->sh_info;
12793 end_lgot_ents = lgot_ents + locsymcount;
12794 local_plt = (struct plt_entry **) end_lgot_ents;
12795 end_local_plt = local_plt + locsymcount;
12796 lgot_masks = (unsigned char *) end_local_plt;
12797 s = ppc64_elf_tdata (ibfd)->got;
12798 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12799 {
12800 struct got_entry *ent;
12801
12802 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12803 {
12804 unsigned int ent_size = 8;
12805 unsigned int rel_size = sizeof (Elf64_External_Rela);
12806
12807 ent->got.offset = s->size;
12808 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12809 {
12810 ent_size *= 2;
12811 rel_size *= 2;
12812 }
12813 s->size += ent_size;
12814 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12815 {
12816 htab->elf.irelplt->size += rel_size;
12817 htab->got_reli_size += rel_size;
12818 }
12819 else if (bfd_link_pic (info)
12820 && (ent->tls_type == 0
12821 ? !info->enable_dt_relr
12822 : !bfd_link_executable (info)))
12823 {
12824 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12825 srel->size += rel_size;
12826 }
12827 }
12828 }
12829 }
12830
12831 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12832
12833 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12834 {
12835 struct got_entry *ent;
12836
12837 if (!is_ppc64_elf (ibfd))
12838 continue;
12839
12840 ent = ppc64_tlsld_got (ibfd);
12841 if (!ent->is_indirect
12842 && ent->got.offset != (bfd_vma) -1)
12843 {
12844 asection *s = ppc64_elf_tdata (ibfd)->got;
12845 ent->got.offset = s->size;
12846 s->size += 16;
12847 if (bfd_link_dll (info))
12848 {
12849 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12850 srel->size += sizeof (Elf64_External_Rela);
12851 }
12852 }
12853 }
12854
12855 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12856 if (!done_something)
12857 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12858 {
12859 asection *got;
12860
12861 if (!is_ppc64_elf (ibfd))
12862 continue;
12863
12864 got = ppc64_elf_tdata (ibfd)->got;
12865 if (got != NULL)
12866 {
12867 done_something = got->rawsize != got->size;
12868 if (done_something)
12869 break;
12870 }
12871 }
12872
12873 if (done_something)
12874 (*htab->params->layout_sections_again) ();
12875
12876 /* Set up for second pass over toc sections to recalculate elf_gp
12877 on input sections. */
12878 htab->toc_bfd = NULL;
12879 htab->toc_first_sec = NULL;
12880 htab->second_toc_pass = true;
12881 return done_something;
12882 }
12883
12884 /* Called after second pass of multitoc partitioning. */
12885
12886 void
12887 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12888 {
12889 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12890
12891 /* After the second pass, toc_curr tracks the TOC offset used
12892 for code sections below in ppc64_elf_next_input_section. */
12893 htab->toc_curr = TOC_BASE_OFF;
12894 }
12895
12896 /* No toc references were found in ISEC. If the code in ISEC makes no
12897 calls, then there's no need to use toc adjusting stubs when branching
12898 into ISEC. Actually, indirect calls from ISEC are OK as they will
12899 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12900 needed, and 2 if a cyclical call-graph was found but no other reason
12901 for a stub was detected. If called from the top level, a return of
12902 2 means the same as a return of 0. */
12903
12904 static int
12905 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12906 {
12907 int ret;
12908
12909 /* Mark this section as checked. */
12910 isec->call_check_done = 1;
12911
12912 /* We know none of our code bearing sections will need toc stubs. */
12913 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12914 return 0;
12915
12916 if (isec->size == 0)
12917 return 0;
12918
12919 if (isec->output_section == NULL)
12920 return 0;
12921
12922 ret = 0;
12923 if (isec->reloc_count != 0)
12924 {
12925 Elf_Internal_Rela *relstart, *rel;
12926 Elf_Internal_Sym *local_syms;
12927 struct ppc_link_hash_table *htab;
12928
12929 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12930 info->keep_memory);
12931 if (relstart == NULL)
12932 return -1;
12933
12934 /* Look for branches to outside of this section. */
12935 local_syms = NULL;
12936 htab = ppc_hash_table (info);
12937 if (htab == NULL)
12938 return -1;
12939
12940 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12941 {
12942 enum elf_ppc64_reloc_type r_type;
12943 unsigned long r_symndx;
12944 struct elf_link_hash_entry *h;
12945 struct ppc_link_hash_entry *eh;
12946 Elf_Internal_Sym *sym;
12947 asection *sym_sec;
12948 struct _opd_sec_data *opd;
12949 bfd_vma sym_value;
12950 bfd_vma dest;
12951
12952 r_type = ELF64_R_TYPE (rel->r_info);
12953 if (r_type != R_PPC64_REL24
12954 && r_type != R_PPC64_REL24_NOTOC
12955 && r_type != R_PPC64_REL24_P9NOTOC
12956 && r_type != R_PPC64_REL14
12957 && r_type != R_PPC64_REL14_BRTAKEN
12958 && r_type != R_PPC64_REL14_BRNTAKEN
12959 && r_type != R_PPC64_PLTCALL
12960 && r_type != R_PPC64_PLTCALL_NOTOC)
12961 continue;
12962
12963 r_symndx = ELF64_R_SYM (rel->r_info);
12964 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12965 isec->owner))
12966 {
12967 ret = -1;
12968 break;
12969 }
12970
12971 /* Calls to dynamic lib functions go through a plt call stub
12972 that uses r2. */
12973 eh = ppc_elf_hash_entry (h);
12974 if (eh != NULL
12975 && (eh->elf.plt.plist != NULL
12976 || (eh->oh != NULL
12977 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12978 {
12979 ret = 1;
12980 break;
12981 }
12982
12983 if (sym_sec == NULL)
12984 /* Ignore other undefined symbols. */
12985 continue;
12986
12987 /* Assume branches to other sections not included in the
12988 link need stubs too, to cover -R and absolute syms. */
12989 if (sym_sec->output_section == NULL)
12990 {
12991 ret = 1;
12992 break;
12993 }
12994
12995 if (h == NULL)
12996 sym_value = sym->st_value;
12997 else
12998 {
12999 if (h->root.type != bfd_link_hash_defined
13000 && h->root.type != bfd_link_hash_defweak)
13001 abort ();
13002 sym_value = h->root.u.def.value;
13003 }
13004 sym_value += rel->r_addend;
13005
13006 /* If this branch reloc uses an opd sym, find the code section. */
13007 opd = get_opd_info (sym_sec);
13008 if (opd != NULL)
13009 {
13010 if (h == NULL && opd->adjust != NULL)
13011 {
13012 long adjust;
13013
13014 adjust = opd->adjust[OPD_NDX (sym_value)];
13015 if (adjust == -1)
13016 /* Assume deleted functions won't ever be called. */
13017 continue;
13018 sym_value += adjust;
13019 }
13020
13021 dest = opd_entry_value (sym_sec, sym_value,
13022 &sym_sec, NULL, false);
13023 if (dest == (bfd_vma) -1)
13024 continue;
13025 }
13026 else
13027 dest = (sym_value
13028 + sym_sec->output_offset
13029 + sym_sec->output_section->vma);
13030
13031 /* Ignore branch to self. */
13032 if (sym_sec == isec)
13033 continue;
13034
13035 /* If the called function uses the toc, we need a stub. */
13036 if (sym_sec->has_toc_reloc
13037 || sym_sec->makes_toc_func_call)
13038 {
13039 ret = 1;
13040 break;
13041 }
13042
13043 /* Assume any branch that needs a long branch stub might in fact
13044 need a plt_branch stub. A plt_branch stub uses r2. */
13045 else if (dest - (isec->output_offset
13046 + isec->output_section->vma
13047 + rel->r_offset) + (1 << 25)
13048 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13049 ? h->other
13050 : sym->st_other))
13051 {
13052 ret = 1;
13053 break;
13054 }
13055
13056 /* If calling back to a section in the process of being
13057 tested, we can't say for sure that no toc adjusting stubs
13058 are needed, so don't return zero. */
13059 else if (sym_sec->call_check_in_progress)
13060 ret = 2;
13061
13062 /* Branches to another section that itself doesn't have any TOC
13063 references are OK. Recursively call ourselves to check. */
13064 else if (!sym_sec->call_check_done)
13065 {
13066 int recur;
13067
13068 /* Mark current section as indeterminate, so that other
13069 sections that call back to current won't be marked as
13070 known. */
13071 isec->call_check_in_progress = 1;
13072 recur = toc_adjusting_stub_needed (info, sym_sec);
13073 isec->call_check_in_progress = 0;
13074
13075 if (recur != 0)
13076 {
13077 ret = recur;
13078 if (recur != 2)
13079 break;
13080 }
13081 }
13082 }
13083
13084 if (elf_symtab_hdr (isec->owner).contents
13085 != (unsigned char *) local_syms)
13086 free (local_syms);
13087 if (elf_section_data (isec)->relocs != relstart)
13088 free (relstart);
13089 }
13090
13091 if ((ret & 1) == 0
13092 && isec->map_head.s != NULL
13093 && (strcmp (isec->output_section->name, ".init") == 0
13094 || strcmp (isec->output_section->name, ".fini") == 0))
13095 {
13096 if (isec->map_head.s->has_toc_reloc
13097 || isec->map_head.s->makes_toc_func_call)
13098 ret = 1;
13099 else if (!isec->map_head.s->call_check_done)
13100 {
13101 int recur;
13102 isec->call_check_in_progress = 1;
13103 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13104 isec->call_check_in_progress = 0;
13105 if (recur != 0)
13106 ret = recur;
13107 }
13108 }
13109
13110 if (ret == 1)
13111 isec->makes_toc_func_call = 1;
13112
13113 return ret;
13114 }
13115
13116 /* The linker repeatedly calls this function for each input section,
13117 in the order that input sections are linked into output sections.
13118 Build lists of input sections to determine groupings between which
13119 we may insert linker stubs. */
13120
13121 bool
13122 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13123 {
13124 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13125
13126 if (htab == NULL)
13127 return false;
13128
13129 if ((isec->output_section->flags & SEC_CODE) != 0
13130 && isec->output_section->id < htab->sec_info_arr_size)
13131 {
13132 /* This happens to make the list in reverse order,
13133 which is what we want. */
13134 htab->sec_info[isec->id].u.list
13135 = htab->sec_info[isec->output_section->id].u.list;
13136 htab->sec_info[isec->output_section->id].u.list = isec;
13137 }
13138
13139 if (htab->multi_toc_needed)
13140 {
13141 /* Analyse sections that aren't already flagged as needing a
13142 valid toc pointer. Exclude .fixup for the linux kernel.
13143 .fixup contains branches, but only back to the function that
13144 hit an exception. */
13145 if (!(isec->has_toc_reloc
13146 || (isec->flags & SEC_CODE) == 0
13147 || strcmp (isec->name, ".fixup") == 0
13148 || isec->call_check_done))
13149 {
13150 if (toc_adjusting_stub_needed (info, isec) < 0)
13151 return false;
13152 }
13153 /* Make all sections use the TOC assigned for this object file.
13154 This will be wrong for pasted sections; We fix that in
13155 check_pasted_section(). */
13156 if (elf_gp (isec->owner) != 0)
13157 htab->toc_curr = elf_gp (isec->owner);
13158 }
13159
13160 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13161 return true;
13162 }
13163
13164 /* Check that all .init and .fini sections use the same toc, if they
13165 have toc relocs. */
13166
13167 static bool
13168 check_pasted_section (struct bfd_link_info *info, const char *name)
13169 {
13170 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13171
13172 if (o != NULL)
13173 {
13174 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13175 bfd_vma toc_off = 0;
13176 asection *i;
13177
13178 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13179 if (i->has_toc_reloc)
13180 {
13181 if (toc_off == 0)
13182 toc_off = htab->sec_info[i->id].toc_off;
13183 else if (toc_off != htab->sec_info[i->id].toc_off)
13184 return false;
13185 }
13186
13187 if (toc_off == 0)
13188 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13189 if (i->makes_toc_func_call)
13190 {
13191 toc_off = htab->sec_info[i->id].toc_off;
13192 break;
13193 }
13194
13195 /* Make sure the whole pasted function uses the same toc offset. */
13196 if (toc_off != 0)
13197 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13198 htab->sec_info[i->id].toc_off = toc_off;
13199 }
13200 return true;
13201 }
13202
13203 bool
13204 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13205 {
13206 bool ret1 = check_pasted_section (info, ".init");
13207 bool ret2 = check_pasted_section (info, ".fini");
13208
13209 return ret1 && ret2;
13210 }
13211
13212 /* See whether we can group stub sections together. Grouping stub
13213 sections may result in fewer stubs. More importantly, we need to
13214 put all .init* and .fini* stubs at the beginning of the .init or
13215 .fini output sections respectively, because glibc splits the
13216 _init and _fini functions into multiple parts. Putting a stub in
13217 the middle of a function is not a good idea. */
13218
13219 static bool
13220 group_sections (struct bfd_link_info *info,
13221 bfd_size_type stub_group_size,
13222 bool stubs_always_before_branch)
13223 {
13224 struct ppc_link_hash_table *htab;
13225 asection *osec;
13226 bool suppress_size_errors;
13227
13228 htab = ppc_hash_table (info);
13229 if (htab == NULL)
13230 return false;
13231
13232 suppress_size_errors = false;
13233 if (stub_group_size == 1)
13234 {
13235 /* Default values. */
13236 if (stubs_always_before_branch)
13237 stub_group_size = 0x1e00000;
13238 else
13239 stub_group_size = 0x1c00000;
13240 suppress_size_errors = true;
13241 }
13242
13243 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13244 {
13245 asection *tail;
13246
13247 if (osec->id >= htab->sec_info_arr_size)
13248 continue;
13249
13250 tail = htab->sec_info[osec->id].u.list;
13251 while (tail != NULL)
13252 {
13253 asection *curr;
13254 asection *prev;
13255 bfd_size_type total;
13256 bool big_sec;
13257 bfd_vma curr_toc;
13258 struct map_stub *group;
13259 bfd_size_type group_size;
13260
13261 curr = tail;
13262 total = tail->size;
13263 group_size = (ppc64_elf_section_data (tail) != NULL
13264 && ppc64_elf_section_data (tail)->has_14bit_branch
13265 ? stub_group_size >> 10 : stub_group_size);
13266
13267 big_sec = total > group_size;
13268 if (big_sec && !suppress_size_errors)
13269 /* xgettext:c-format */
13270 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13271 tail->owner, tail);
13272 curr_toc = htab->sec_info[tail->id].toc_off;
13273
13274 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13275 && ((total += curr->output_offset - prev->output_offset)
13276 < (ppc64_elf_section_data (prev) != NULL
13277 && ppc64_elf_section_data (prev)->has_14bit_branch
13278 ? (group_size = stub_group_size >> 10) : group_size))
13279 && htab->sec_info[prev->id].toc_off == curr_toc)
13280 curr = prev;
13281
13282 /* OK, the size from the start of CURR to the end is less
13283 than group_size and thus can be handled by one stub
13284 section. (or the tail section is itself larger than
13285 group_size, in which case we may be toast.) We should
13286 really be keeping track of the total size of stubs added
13287 here, as stubs contribute to the final output section
13288 size. That's a little tricky, and this way will only
13289 break if stubs added make the total size more than 2^25,
13290 ie. for the default stub_group_size, if stubs total more
13291 than 2097152 bytes, or nearly 75000 plt call stubs. */
13292 group = bfd_alloc (curr->owner, sizeof (*group));
13293 if (group == NULL)
13294 return false;
13295 group->link_sec = curr;
13296 group->stub_sec = NULL;
13297 group->needs_save_res = 0;
13298 group->lr_restore = 0;
13299 group->eh_size = 0;
13300 group->eh_base = 0;
13301 group->next = htab->group;
13302 htab->group = group;
13303 do
13304 {
13305 prev = htab->sec_info[tail->id].u.list;
13306 /* Set up this stub group. */
13307 htab->sec_info[tail->id].u.group = group;
13308 }
13309 while (tail != curr && (tail = prev) != NULL);
13310
13311 /* But wait, there's more! Input sections up to group_size
13312 bytes before the stub section can be handled by it too.
13313 Don't do this if we have a really large section after the
13314 stubs, as adding more stubs increases the chance that
13315 branches may not reach into the stub section. */
13316 if (!stubs_always_before_branch && !big_sec)
13317 {
13318 total = 0;
13319 while (prev != NULL
13320 && ((total += tail->output_offset - prev->output_offset)
13321 < (ppc64_elf_section_data (prev) != NULL
13322 && ppc64_elf_section_data (prev)->has_14bit_branch
13323 ? (group_size = stub_group_size >> 10)
13324 : group_size))
13325 && htab->sec_info[prev->id].toc_off == curr_toc)
13326 {
13327 tail = prev;
13328 prev = htab->sec_info[tail->id].u.list;
13329 htab->sec_info[tail->id].u.group = group;
13330 }
13331 }
13332 tail = prev;
13333 }
13334 }
13335 return true;
13336 }
13337
13338 static const unsigned char glink_eh_frame_cie[] =
13339 {
13340 0, 0, 0, 16, /* length. */
13341 0, 0, 0, 0, /* id. */
13342 1, /* CIE version. */
13343 'z', 'R', 0, /* Augmentation string. */
13344 4, /* Code alignment. */
13345 0x78, /* Data alignment. */
13346 65, /* RA reg. */
13347 1, /* Augmentation size. */
13348 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13349 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13350 };
13351
13352 /* Stripping output sections is normally done before dynamic section
13353 symbols have been allocated. This function is called later, and
13354 handles cases like htab->brlt which is mapped to its own output
13355 section. */
13356
13357 static void
13358 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13359 {
13360 if (isec->size == 0
13361 && isec->output_section->size == 0
13362 && !(isec->output_section->flags & SEC_KEEP)
13363 && !bfd_section_removed_from_list (info->output_bfd,
13364 isec->output_section)
13365 && elf_section_data (isec->output_section)->dynindx == 0)
13366 {
13367 isec->output_section->flags |= SEC_EXCLUDE;
13368 bfd_section_list_remove (info->output_bfd, isec->output_section);
13369 info->output_bfd->section_count--;
13370 }
13371 }
13372
13373 static int
13374 compare_relr_address (const void *arg1, const void *arg2)
13375 {
13376 bfd_vma a = *(bfd_vma *) arg1;
13377 bfd_vma b = *(bfd_vma *) arg2;
13378 return a < b ? -1 : a > b ? 1 : 0;
13379 }
13380
13381 static bool
13382 append_relr_off (struct ppc_link_hash_table *htab, bfd_vma off)
13383 {
13384 if (htab->relr_count >= htab->relr_alloc)
13385 {
13386 if (htab->relr_alloc == 0)
13387 htab->relr_alloc = 4096;
13388 else
13389 htab->relr_alloc *= 2;
13390 htab->relr_addr
13391 = bfd_realloc (htab->relr_addr,
13392 htab->relr_alloc * sizeof (htab->relr_addr[0]));
13393 if (htab->relr_addr == NULL)
13394 return false;
13395 }
13396 htab->relr_addr[htab->relr_count++] = off;
13397 return true;
13398 }
13399
13400 static bool
13401 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13402 {
13403 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13404 bfd *ibfd;
13405
13406 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13407 {
13408 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13409 struct plt_entry **local_plt, **lplt, **end_local_plt;
13410 Elf_Internal_Shdr *symtab_hdr;
13411 bfd_size_type locsymcount;
13412 Elf_Internal_Sym *local_syms = NULL;
13413 struct plt_entry *pent;
13414 struct got_entry *gent;
13415
13416 if (!is_ppc64_elf (ibfd))
13417 continue;
13418
13419 lgot_ents = elf_local_got_ents (ibfd);
13420 if (!lgot_ents)
13421 continue;
13422
13423 symtab_hdr = &elf_symtab_hdr (ibfd);
13424 locsymcount = symtab_hdr->sh_info;
13425 end_lgot_ents = lgot_ents + locsymcount;
13426 local_plt = (struct plt_entry **) end_lgot_ents;
13427 end_local_plt = local_plt + locsymcount;
13428 for (lgot = lgot_ents; lgot < end_lgot_ents; ++lgot)
13429 for (gent = *lgot; gent != NULL; gent = gent->next)
13430 if (!gent->is_indirect
13431 && gent->tls_type == 0
13432 && gent->got.offset != (bfd_vma) -1)
13433 {
13434 asection *got = ppc64_elf_tdata (gent->owner)->got;
13435 bfd_vma r_offset = (got->output_section->vma
13436 + got->output_offset
13437 + gent->got.offset);
13438 if (!append_relr_off (htab, r_offset))
13439 {
13440 htab->stub_error = true;
13441 return false;
13442 }
13443 }
13444
13445 if (!htab->opd_abi)
13446 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13447 for (pent = *lplt; pent != NULL; pent = pent->next)
13448 if (pent->plt.offset != (bfd_vma) -1)
13449 {
13450 Elf_Internal_Sym *sym;
13451
13452 if (!get_sym_h (NULL, &sym, NULL, NULL, &local_syms,
13453 lplt - local_plt, ibfd))
13454 {
13455 err_exit:
13456 if (symtab_hdr->contents != (unsigned char *) local_syms)
13457 free (local_syms);
13458 return false;
13459 }
13460
13461 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13462 {
13463 bfd_vma r_offset = (pent->plt.offset
13464 + htab->pltlocal->output_offset
13465 + htab->pltlocal->output_section->vma);
13466 if (!append_relr_off (htab, r_offset))
13467 goto err_exit;
13468 }
13469 }
13470
13471 if (local_syms != NULL
13472 && symtab_hdr->contents != (unsigned char *) local_syms)
13473 {
13474 if (!info->keep_memory)
13475 free (local_syms);
13476 else
13477 symtab_hdr->contents = (unsigned char *) local_syms;
13478 }
13479 }
13480 return true;
13481 }
13482
13483 static bool
13484 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13485 {
13486 struct bfd_link_info *info;
13487 struct ppc_link_hash_table *htab;
13488 struct plt_entry *pent;
13489 struct got_entry *gent;
13490
13491 if (h->root.type == bfd_link_hash_indirect)
13492 return true;
13493
13494 info = (struct bfd_link_info *) inf;
13495 htab = ppc_hash_table (info);
13496 if (htab == NULL)
13497 return false;
13498
13499 if (h->type != STT_GNU_IFUNC
13500 && h->def_regular
13501 && (h->root.type == bfd_link_hash_defined
13502 || h->root.type == bfd_link_hash_defweak))
13503 {
13504 if (!htab->elf.dynamic_sections_created
13505 || h->dynindx == -1
13506 || SYMBOL_REFERENCES_LOCAL (info, h))
13507 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13508 if (!gent->is_indirect
13509 && gent->tls_type == 0
13510 && gent->got.offset != (bfd_vma) -1)
13511 {
13512 asection *got = ppc64_elf_tdata (gent->owner)->got;
13513 bfd_vma r_offset = (got->output_section->vma
13514 + got->output_offset
13515 + gent->got.offset);
13516 if (!append_relr_off (htab, r_offset))
13517 {
13518 htab->stub_error = true;
13519 return false;
13520 }
13521 }
13522
13523 if (!htab->opd_abi
13524 && use_local_plt (info, h))
13525 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13526 if (pent->plt.offset != (bfd_vma) -1)
13527 {
13528 bfd_vma r_offset = (htab->pltlocal->output_section->vma
13529 + htab->pltlocal->output_offset
13530 + pent->plt.offset);
13531 if (!append_relr_off (htab, r_offset))
13532 {
13533 htab->stub_error = true;
13534 return false;
13535 }
13536 }
13537 }
13538 return true;
13539 }
13540
13541 /* Determine and set the size of the stub section for a final link.
13542
13543 The basic idea here is to examine all the relocations looking for
13544 PC-relative calls to a target that is unreachable with a "bl"
13545 instruction. */
13546
13547 bool
13548 ppc64_elf_size_stubs (struct bfd_link_info *info)
13549 {
13550 bfd_size_type stub_group_size;
13551 bool stubs_always_before_branch;
13552 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13553
13554 if (htab == NULL)
13555 return false;
13556
13557 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13558 htab->params->power10_stubs = 0;
13559
13560 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13561 htab->params->plt_thread_safe = 1;
13562 if (!htab->opd_abi)
13563 htab->params->plt_thread_safe = 0;
13564 else if (htab->params->plt_thread_safe == -1)
13565 {
13566 static const char *const thread_starter[] =
13567 {
13568 "pthread_create",
13569 /* libstdc++ */
13570 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13571 /* librt */
13572 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13573 "mq_notify", "create_timer",
13574 /* libanl */
13575 "getaddrinfo_a",
13576 /* libgomp */
13577 "GOMP_parallel",
13578 "GOMP_parallel_start",
13579 "GOMP_parallel_loop_static",
13580 "GOMP_parallel_loop_static_start",
13581 "GOMP_parallel_loop_dynamic",
13582 "GOMP_parallel_loop_dynamic_start",
13583 "GOMP_parallel_loop_guided",
13584 "GOMP_parallel_loop_guided_start",
13585 "GOMP_parallel_loop_runtime",
13586 "GOMP_parallel_loop_runtime_start",
13587 "GOMP_parallel_sections",
13588 "GOMP_parallel_sections_start",
13589 /* libgo */
13590 "__go_go",
13591 };
13592 unsigned i;
13593
13594 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13595 {
13596 struct elf_link_hash_entry *h;
13597 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13598 false, false, true);
13599 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13600 if (htab->params->plt_thread_safe)
13601 break;
13602 }
13603 }
13604 stubs_always_before_branch = htab->params->group_size < 0;
13605 if (htab->params->group_size < 0)
13606 stub_group_size = -htab->params->group_size;
13607 else
13608 stub_group_size = htab->params->group_size;
13609
13610 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13611 return false;
13612
13613 htab->tga_group = NULL;
13614 if (!htab->params->no_tls_get_addr_regsave
13615 && htab->tga_desc_fd != NULL
13616 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13617 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13618 && htab->tls_get_addr_fd != NULL
13619 && is_static_defined (&htab->tls_get_addr_fd->elf))
13620 {
13621 asection *sym_sec, *code_sec, *stub_sec;
13622 bfd_vma sym_value;
13623 struct _opd_sec_data *opd;
13624
13625 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13626 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13627 code_sec = sym_sec;
13628 opd = get_opd_info (sym_sec);
13629 if (opd != NULL)
13630 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13631 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13632 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13633 htab->tga_group->link_sec);
13634 if (stub_sec == NULL)
13635 return false;
13636 htab->tga_group->stub_sec = stub_sec;
13637
13638 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13639 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13640 htab->tga_desc_fd->elf.root.u.def.value = 0;
13641 htab->tga_desc_fd->elf.type = STT_FUNC;
13642 htab->tga_desc_fd->elf.def_regular = 1;
13643 htab->tga_desc_fd->elf.non_elf = 0;
13644 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13645 }
13646
13647 #define STUB_SHRINK_ITER 20
13648 /* Loop until no stubs added. After iteration 20 of this loop we may
13649 exit on a stub section shrinking. This is to break out of a
13650 pathological case where adding stubs on one iteration decreases
13651 section gaps (perhaps due to alignment), which then requires
13652 fewer or smaller stubs on the next iteration. */
13653
13654 while (1)
13655 {
13656 bfd *input_bfd;
13657 unsigned int bfd_indx;
13658 struct map_stub *group;
13659
13660 htab->stub_iteration += 1;
13661 htab->relr_count = 0;
13662
13663 for (input_bfd = info->input_bfds, bfd_indx = 0;
13664 input_bfd != NULL;
13665 input_bfd = input_bfd->link.next, bfd_indx++)
13666 {
13667 Elf_Internal_Shdr *symtab_hdr;
13668 asection *section;
13669 Elf_Internal_Sym *local_syms = NULL;
13670
13671 if (!is_ppc64_elf (input_bfd))
13672 continue;
13673
13674 /* We'll need the symbol table in a second. */
13675 symtab_hdr = &elf_symtab_hdr (input_bfd);
13676 if (symtab_hdr->sh_info == 0)
13677 continue;
13678
13679 /* Walk over each section attached to the input bfd. */
13680 for (section = input_bfd->sections;
13681 section != NULL;
13682 section = section->next)
13683 {
13684 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13685 bool is_opd;
13686
13687 /* If there aren't any relocs, then there's nothing more
13688 to do. */
13689 if ((section->flags & SEC_RELOC) == 0
13690 || (section->flags & SEC_ALLOC) == 0
13691 || (section->flags & SEC_LOAD) == 0
13692 || section->reloc_count == 0)
13693 continue;
13694
13695 if (!info->enable_dt_relr
13696 && (section->flags & SEC_CODE) == 0)
13697 continue;
13698
13699 /* If this section is a link-once section that will be
13700 discarded, then don't create any stubs. */
13701 if (section->output_section == NULL
13702 || section->output_section->owner != info->output_bfd)
13703 continue;
13704
13705 /* Get the relocs. */
13706 internal_relocs
13707 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13708 info->keep_memory);
13709 if (internal_relocs == NULL)
13710 goto error_ret_free_local;
13711
13712 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13713
13714 /* Now examine each relocation. */
13715 irela = internal_relocs;
13716 irelaend = irela + section->reloc_count;
13717 for (; irela < irelaend; irela++)
13718 {
13719 enum elf_ppc64_reloc_type r_type;
13720 unsigned int r_indx;
13721 struct ppc_stub_type stub_type;
13722 struct ppc_stub_hash_entry *stub_entry;
13723 asection *sym_sec, *code_sec;
13724 bfd_vma sym_value, code_value;
13725 bfd_vma destination;
13726 unsigned long local_off;
13727 bool ok_dest;
13728 struct ppc_link_hash_entry *hash;
13729 struct ppc_link_hash_entry *fdh;
13730 struct elf_link_hash_entry *h;
13731 Elf_Internal_Sym *sym;
13732 char *stub_name;
13733 const asection *id_sec;
13734 struct _opd_sec_data *opd;
13735 struct plt_entry *plt_ent;
13736
13737 r_type = ELF64_R_TYPE (irela->r_info);
13738 r_indx = ELF64_R_SYM (irela->r_info);
13739
13740 if (r_type >= R_PPC64_max)
13741 {
13742 bfd_set_error (bfd_error_bad_value);
13743 goto error_ret_free_internal;
13744 }
13745
13746 /* Only look for stubs on branch instructions. */
13747 switch (r_type)
13748 {
13749 default:
13750 continue;
13751
13752 case R_PPC64_REL24:
13753 case R_PPC64_REL24_NOTOC:
13754 case R_PPC64_REL24_P9NOTOC:
13755 case R_PPC64_REL14:
13756 case R_PPC64_REL14_BRTAKEN:
13757 case R_PPC64_REL14_BRNTAKEN:
13758 if ((section->flags & SEC_CODE) != 0)
13759 break;
13760 continue;
13761
13762 case R_PPC64_ADDR64:
13763 case R_PPC64_TOC:
13764 if (info->enable_dt_relr
13765 && irela->r_offset % 2 == 0
13766 && section->alignment_power != 0)
13767 break;
13768 continue;
13769 }
13770
13771 /* Now determine the call target, its name, value,
13772 section. */
13773 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13774 r_indx, input_bfd))
13775 goto error_ret_free_internal;
13776
13777 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13778 {
13779 /* Only locally defined symbols can possibly use
13780 relative relocations. */
13781 bfd_vma r_offset;
13782 if ((sym_sec == NULL
13783 || sym_sec->output_section == NULL)
13784 /* No symbol is OK too. */
13785 && !(sym != NULL && sym->st_shndx == 0)
13786 /* Hack for __ehdr_start, which is undefined
13787 at this point. */
13788 && !(h != NULL && h->root.linker_def))
13789 continue;
13790 if (NO_OPD_RELOCS && is_opd)
13791 continue;
13792 if (!is_opd
13793 && r_type == R_PPC64_ADDR64)
13794 {
13795 if (h != NULL
13796 ? h->type == STT_GNU_IFUNC
13797 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13798 continue;
13799 if (h != NULL
13800 && !SYMBOL_REFERENCES_LOCAL (info, h))
13801 continue;
13802 }
13803 r_offset = _bfd_elf_section_offset (info->output_bfd,
13804 info,
13805 section,
13806 irela->r_offset);
13807 if (r_offset >= (bfd_vma) -2)
13808 continue;
13809 r_offset += (section->output_section->vma
13810 + section->output_offset);
13811 if (!append_relr_off (htab, r_offset))
13812 goto error_ret_free_internal;
13813 continue;
13814 }
13815
13816 hash = ppc_elf_hash_entry (h);
13817 ok_dest = false;
13818 fdh = NULL;
13819 sym_value = 0;
13820 if (hash == NULL)
13821 {
13822 sym_value = sym->st_value;
13823 if (sym_sec != NULL
13824 && sym_sec->output_section != NULL)
13825 ok_dest = true;
13826 }
13827 else if (hash->elf.root.type == bfd_link_hash_defined
13828 || hash->elf.root.type == bfd_link_hash_defweak)
13829 {
13830 sym_value = hash->elf.root.u.def.value;
13831 if (sym_sec->output_section != NULL)
13832 ok_dest = true;
13833 }
13834 else if (hash->elf.root.type == bfd_link_hash_undefweak
13835 || hash->elf.root.type == bfd_link_hash_undefined)
13836 {
13837 /* Recognise an old ABI func code entry sym, and
13838 use the func descriptor sym instead if it is
13839 defined. */
13840 if (hash->elf.root.root.string[0] == '.'
13841 && hash->oh != NULL)
13842 {
13843 fdh = ppc_follow_link (hash->oh);
13844 if (fdh->elf.root.type == bfd_link_hash_defined
13845 || fdh->elf.root.type == bfd_link_hash_defweak)
13846 {
13847 sym_sec = fdh->elf.root.u.def.section;
13848 sym_value = fdh->elf.root.u.def.value;
13849 if (sym_sec->output_section != NULL)
13850 ok_dest = true;
13851 }
13852 else
13853 fdh = NULL;
13854 }
13855 }
13856 else
13857 {
13858 bfd_set_error (bfd_error_bad_value);
13859 goto error_ret_free_internal;
13860 }
13861
13862 destination = 0;
13863 local_off = 0;
13864 if (ok_dest)
13865 {
13866 sym_value += irela->r_addend;
13867 destination = (sym_value
13868 + sym_sec->output_offset
13869 + sym_sec->output_section->vma);
13870 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13871 ? hash->elf.other
13872 : sym->st_other);
13873 }
13874
13875 code_sec = sym_sec;
13876 code_value = sym_value;
13877 opd = get_opd_info (sym_sec);
13878 if (opd != NULL)
13879 {
13880 bfd_vma dest;
13881
13882 if (hash == NULL && opd->adjust != NULL)
13883 {
13884 long adjust = opd->adjust[OPD_NDX (sym_value)];
13885 if (adjust == -1)
13886 continue;
13887 code_value += adjust;
13888 sym_value += adjust;
13889 }
13890 dest = opd_entry_value (sym_sec, sym_value,
13891 &code_sec, &code_value, false);
13892 if (dest != (bfd_vma) -1)
13893 {
13894 destination = dest;
13895 if (fdh != NULL)
13896 {
13897 /* Fixup old ABI sym to point at code
13898 entry. */
13899 hash->elf.root.type = bfd_link_hash_defweak;
13900 hash->elf.root.u.def.section = code_sec;
13901 hash->elf.root.u.def.value = code_value;
13902 }
13903 }
13904 }
13905
13906 /* Determine what (if any) linker stub is needed. */
13907 plt_ent = NULL;
13908 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13909 &plt_ent, destination,
13910 local_off);
13911 stub_type.sub = ppc_stub_toc;
13912 stub_type.r2save = 0;
13913
13914 if (r_type == R_PPC64_REL24_NOTOC
13915 || r_type == R_PPC64_REL24_P9NOTOC)
13916 {
13917 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13918 if (htab->params->power10_stubs == 0
13919 || (r_type == R_PPC64_REL24_P9NOTOC
13920 && htab->params->power10_stubs != 1))
13921 notoc = ppc_stub_p9notoc;
13922 if (stub_type.main == ppc_stub_plt_call)
13923 stub_type.sub = notoc;
13924 else if (stub_type.main == ppc_stub_long_branch
13925 || (code_sec != NULL
13926 && code_sec->output_section != NULL
13927 && (((hash ? hash->elf.other : sym->st_other)
13928 & STO_PPC64_LOCAL_MASK)
13929 > 1 << STO_PPC64_LOCAL_BIT)))
13930 {
13931 stub_type.main = ppc_stub_long_branch;
13932 stub_type.sub = notoc;
13933 stub_type.r2save = 0;
13934 }
13935 }
13936 else if (stub_type.main != ppc_stub_plt_call)
13937 {
13938 /* Check whether we need a TOC adjusting stub.
13939 Since the linker pastes together pieces from
13940 different object files when creating the
13941 _init and _fini functions, it may be that a
13942 call to what looks like a local sym is in
13943 fact a call needing a TOC adjustment. */
13944 if ((code_sec != NULL
13945 && code_sec->output_section != NULL
13946 && (code_sec->has_toc_reloc
13947 || code_sec->makes_toc_func_call)
13948 && (htab->sec_info[code_sec->id].toc_off
13949 != htab->sec_info[section->id].toc_off))
13950 || (((hash ? hash->elf.other : sym->st_other)
13951 & STO_PPC64_LOCAL_MASK)
13952 == 1 << STO_PPC64_LOCAL_BIT))
13953 {
13954 stub_type.main = ppc_stub_long_branch;
13955 stub_type.sub = ppc_stub_toc;
13956 stub_type.r2save = 1;
13957 }
13958 }
13959
13960 if (stub_type.main == ppc_stub_none)
13961 continue;
13962
13963 /* __tls_get_addr calls might be eliminated. */
13964 if (stub_type.main != ppc_stub_plt_call
13965 && hash != NULL
13966 && is_tls_get_addr (&hash->elf, htab)
13967 && section->has_tls_reloc
13968 && irela != internal_relocs)
13969 {
13970 /* Get tls info. */
13971 unsigned char *tls_mask;
13972
13973 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13974 irela - 1, input_bfd))
13975 goto error_ret_free_internal;
13976 if ((*tls_mask & TLS_TLS) != 0
13977 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
13978 continue;
13979 }
13980
13981 if (stub_type.main == ppc_stub_plt_call
13982 && stub_type.sub == ppc_stub_toc)
13983 {
13984 if (!htab->opd_abi
13985 && htab->params->plt_localentry0 != 0
13986 && is_elfv2_localentry0 (&hash->elf))
13987 htab->has_plt_localentry0 = 1;
13988 else if (irela + 1 < irelaend
13989 && irela[1].r_offset == irela->r_offset + 4
13990 && (ELF64_R_TYPE (irela[1].r_info)
13991 == R_PPC64_TOCSAVE))
13992 {
13993 if (!tocsave_find (htab, INSERT,
13994 &local_syms, irela + 1, input_bfd))
13995 goto error_ret_free_internal;
13996 }
13997 else
13998 stub_type.r2save = 1;
13999 }
14000
14001 /* Support for grouping stub sections. */
14002 id_sec = htab->sec_info[section->id].u.group->link_sec;
14003
14004 /* Get the name of this stub. */
14005 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14006 if (!stub_name)
14007 goto error_ret_free_internal;
14008
14009 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14010 stub_name, false, false);
14011 if (stub_entry != NULL)
14012 {
14013 free (stub_name);
14014 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14015 {
14016 /* xgettext:c-format */
14017 _bfd_error_handler
14018 (_("%pB: cannot create stub entry %s"),
14019 section->owner, stub_entry->root.string);
14020 goto error_ret_free_internal;
14021 }
14022 continue;
14023 }
14024
14025 stub_entry = ppc_add_stub (stub_name, section, info);
14026 if (stub_entry == NULL)
14027 {
14028 free (stub_name);
14029 error_ret_free_internal:
14030 if (elf_section_data (section)->relocs == NULL)
14031 free (internal_relocs);
14032 error_ret_free_local:
14033 if (symtab_hdr->contents
14034 != (unsigned char *) local_syms)
14035 free (local_syms);
14036 return false;
14037 }
14038
14039 stub_entry->type = stub_type;
14040 if (stub_type.main == ppc_stub_plt_call)
14041 {
14042 stub_entry->target_value = sym_value;
14043 stub_entry->target_section = sym_sec;
14044 }
14045 else
14046 {
14047 stub_entry->target_value = code_value;
14048 stub_entry->target_section = code_sec;
14049 }
14050 stub_entry->h = hash;
14051 stub_entry->plt_ent = plt_ent;
14052 stub_entry->symtype
14053 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14054 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14055
14056 if (hash != NULL
14057 && (hash->elf.root.type == bfd_link_hash_defined
14058 || hash->elf.root.type == bfd_link_hash_defweak))
14059 htab->stub_globals += 1;
14060 }
14061
14062 /* We're done with the internal relocs, free them. */
14063 if (elf_section_data (section)->relocs != internal_relocs)
14064 free (internal_relocs);
14065 }
14066
14067 if (local_syms != NULL
14068 && symtab_hdr->contents != (unsigned char *) local_syms)
14069 {
14070 if (!info->keep_memory)
14071 free (local_syms);
14072 else
14073 symtab_hdr->contents = (unsigned char *) local_syms;
14074 }
14075 }
14076
14077 /* We may have added some stubs. Find out the new size of the
14078 stub sections. */
14079 for (group = htab->group; group != NULL; group = group->next)
14080 {
14081 group->lr_restore = 0;
14082 group->eh_size = 0;
14083 if (group->stub_sec != NULL)
14084 {
14085 asection *stub_sec = group->stub_sec;
14086
14087 if (htab->stub_iteration <= STUB_SHRINK_ITER
14088 || stub_sec->rawsize < stub_sec->size)
14089 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
14090 stub_sec->rawsize = stub_sec->size;
14091 stub_sec->size = 0;
14092 stub_sec->reloc_count = 0;
14093 stub_sec->flags &= ~SEC_RELOC;
14094 }
14095 }
14096 if (htab->tga_group != NULL)
14097 {
14098 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14099 htab->tga_group->eh_size
14100 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14101 htab->tga_group->lr_restore = 23 * 4;
14102 htab->tga_group->stub_sec->size = 24 * 4;
14103 }
14104
14105 if (htab->stub_iteration <= STUB_SHRINK_ITER
14106 || htab->brlt->rawsize < htab->brlt->size)
14107 htab->brlt->rawsize = htab->brlt->size;
14108 htab->brlt->size = 0;
14109 htab->brlt->reloc_count = 0;
14110 htab->brlt->flags &= ~SEC_RELOC;
14111 if (htab->relbrlt != NULL)
14112 htab->relbrlt->size = 0;
14113
14114 if (htab->elf.srelrdyn != NULL)
14115 {
14116 if (htab->stub_iteration <= STUB_SHRINK_ITER
14117 || htab->elf.srelrdyn->rawsize < htab->elf.srelrdyn->size)
14118 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14119 htab->elf.srelrdyn->size = 0;
14120 }
14121
14122 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14123
14124 for (group = htab->group; group != NULL; group = group->next)
14125 if (group->needs_save_res)
14126 group->stub_sec->size += htab->sfpr->size;
14127
14128 if (info->emitrelocations
14129 && htab->glink != NULL && htab->glink->size != 0)
14130 {
14131 htab->glink->reloc_count = 1;
14132 htab->glink->flags |= SEC_RELOC;
14133 }
14134
14135 if (htab->glink_eh_frame != NULL
14136 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14137 && htab->glink_eh_frame->output_section->size > 8)
14138 {
14139 size_t size = 0, align = 4;
14140
14141 for (group = htab->group; group != NULL; group = group->next)
14142 if (group->eh_size != 0)
14143 size += (group->eh_size + 17 + align - 1) & -align;
14144 if (htab->glink != NULL && htab->glink->size != 0)
14145 size += (24 + align - 1) & -align;
14146 if (size != 0)
14147 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14148 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14149 size = (size + align - 1) & -align;
14150 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14151 htab->glink_eh_frame->size = size;
14152 }
14153
14154 if (htab->params->plt_stub_align != 0)
14155 for (group = htab->group; group != NULL; group = group->next)
14156 if (group->stub_sec != NULL)
14157 {
14158 int align = abs (htab->params->plt_stub_align);
14159 group->stub_sec->size
14160 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14161 }
14162
14163 if (htab->elf.srelrdyn != NULL)
14164 {
14165 bfd_vma r_offset;
14166
14167 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14168 if (!append_relr_off (htab, (r_offset
14169 + htab->brlt->output_section->vma
14170 + htab->brlt->output_offset)))
14171 return false;
14172
14173 if (!got_and_plt_relr_for_local_syms (info))
14174 return false;
14175 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14176 if (htab->stub_error)
14177 return false;
14178
14179 if (htab->relr_count > 1)
14180 qsort (htab->relr_addr, htab->relr_count, sizeof (*htab->relr_addr),
14181 compare_relr_address);
14182
14183 size_t i = 0;
14184 while (i < htab->relr_count)
14185 {
14186 bfd_vma base = htab->relr_addr[i];
14187 htab->elf.srelrdyn->size += 8;
14188 i++;
14189 /* Handle possible duplicate address. This can happen
14190 as sections increase in size when adding stubs. */
14191 while (i < htab->relr_count
14192 && htab->relr_addr[i] == base)
14193 i++;
14194 base += 8;
14195 while (1)
14196 {
14197 size_t start_i = i;
14198 while (i < htab->relr_count
14199 && htab->relr_addr[i] - base < 63 * 8
14200 && (htab->relr_addr[i] - base) % 8 == 0)
14201 i++;
14202 if (i == start_i)
14203 break;
14204 htab->elf.srelrdyn->size += 8;
14205 base += 63 * 8;
14206 }
14207 }
14208 }
14209
14210 for (group = htab->group; group != NULL; group = group->next)
14211 if (group->stub_sec != NULL
14212 && group->stub_sec->rawsize != group->stub_sec->size
14213 && (htab->stub_iteration <= STUB_SHRINK_ITER
14214 || group->stub_sec->rawsize < group->stub_sec->size))
14215 break;
14216
14217 if (group == NULL
14218 && (htab->brlt->rawsize == htab->brlt->size
14219 || (htab->stub_iteration > STUB_SHRINK_ITER
14220 && htab->brlt->rawsize > htab->brlt->size))
14221 && (htab->elf.srelrdyn == NULL
14222 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14223 || (htab->stub_iteration > STUB_SHRINK_ITER
14224 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14225 && (htab->glink_eh_frame == NULL
14226 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14227 && (htab->tga_group == NULL
14228 || htab->stub_iteration > 1))
14229 break;
14230
14231 /* Ask the linker to do its stuff. */
14232 (*htab->params->layout_sections_again) ();
14233 }
14234
14235 if (htab->glink_eh_frame != NULL
14236 && htab->glink_eh_frame->size != 0)
14237 {
14238 bfd_vma val;
14239 bfd_byte *p, *last_fde;
14240 size_t last_fde_len, size, align, pad;
14241 struct map_stub *group;
14242
14243 /* It is necessary to at least have a rough outline of the
14244 linker generated CIEs and FDEs written before
14245 bfd_elf_discard_info is run, in order for these FDEs to be
14246 indexed in .eh_frame_hdr. */
14247 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14248 if (p == NULL)
14249 return false;
14250 htab->glink_eh_frame->contents = p;
14251 last_fde = p;
14252 align = 4;
14253
14254 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14255 /* CIE length (rewrite in case little-endian). */
14256 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14257 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14258 p += last_fde_len + 4;
14259
14260 for (group = htab->group; group != NULL; group = group->next)
14261 if (group->eh_size != 0)
14262 {
14263 group->eh_base = p - htab->glink_eh_frame->contents;
14264 last_fde = p;
14265 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14266 /* FDE length. */
14267 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14268 p += 4;
14269 /* CIE pointer. */
14270 val = p - htab->glink_eh_frame->contents;
14271 bfd_put_32 (htab->elf.dynobj, val, p);
14272 p += 4;
14273 /* Offset to stub section, written later. */
14274 p += 4;
14275 /* stub section size. */
14276 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14277 p += 4;
14278 /* Augmentation. */
14279 p += 1;
14280 /* Make sure we don't have all nops. This is enough for
14281 elf-eh-frame.c to detect the last non-nop opcode. */
14282 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14283 p = last_fde + last_fde_len + 4;
14284 }
14285 if (htab->glink != NULL && htab->glink->size != 0)
14286 {
14287 last_fde = p;
14288 last_fde_len = ((24 + align - 1) & -align) - 4;
14289 /* FDE length. */
14290 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14291 p += 4;
14292 /* CIE pointer. */
14293 val = p - htab->glink_eh_frame->contents;
14294 bfd_put_32 (htab->elf.dynobj, val, p);
14295 p += 4;
14296 /* Offset to .glink, written later. */
14297 p += 4;
14298 /* .glink size. */
14299 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14300 p += 4;
14301 /* Augmentation. */
14302 p += 1;
14303
14304 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14305 *p++ = DW_CFA_register;
14306 *p++ = 65;
14307 *p++ = htab->opd_abi ? 12 : 0;
14308 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14309 *p++ = DW_CFA_restore_extended;
14310 *p++ = 65;
14311 p += ((24 + align - 1) & -align) - 24;
14312 }
14313 /* Subsume any padding into the last FDE if user .eh_frame
14314 sections are aligned more than glink_eh_frame. Otherwise any
14315 zero padding will be seen as a terminator. */
14316 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14317 size = p - htab->glink_eh_frame->contents;
14318 pad = ((size + align - 1) & -align) - size;
14319 htab->glink_eh_frame->size = size + pad;
14320 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14321 }
14322
14323 maybe_strip_output (info, htab->brlt);
14324 if (htab->relbrlt != NULL)
14325 maybe_strip_output (info, htab->relbrlt);
14326 if (htab->glink_eh_frame != NULL)
14327 maybe_strip_output (info, htab->glink_eh_frame);
14328 if (htab->elf.srelrdyn != NULL)
14329 maybe_strip_output (info, htab->elf.srelrdyn);
14330
14331 return true;
14332 }
14333
14334 /* Called after we have determined section placement. If sections
14335 move, we'll be called again. Provide a value for TOCstart. */
14336
14337 bfd_vma
14338 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14339 {
14340 asection *s;
14341 bfd_vma TOCstart, adjust;
14342
14343 if (info != NULL)
14344 {
14345 struct elf_link_hash_entry *h;
14346 struct elf_link_hash_table *htab = elf_hash_table (info);
14347
14348 if (is_elf_hash_table (&htab->root)
14349 && htab->hgot != NULL)
14350 h = htab->hgot;
14351 else
14352 {
14353 h = (struct elf_link_hash_entry *)
14354 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14355 if (is_elf_hash_table (&htab->root))
14356 htab->hgot = h;
14357 }
14358 if (h != NULL
14359 && h->root.type == bfd_link_hash_defined
14360 && !h->root.linker_def
14361 && (!is_elf_hash_table (&htab->root)
14362 || h->def_regular))
14363 {
14364 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14365 _bfd_set_gp_value (obfd, TOCstart);
14366 return TOCstart;
14367 }
14368 }
14369
14370 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14371 order. The TOC starts where the first of these sections starts. */
14372 s = bfd_get_section_by_name (obfd, ".got");
14373 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14374 s = bfd_get_section_by_name (obfd, ".toc");
14375 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14376 s = bfd_get_section_by_name (obfd, ".tocbss");
14377 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14378 s = bfd_get_section_by_name (obfd, ".plt");
14379 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14380 {
14381 /* This may happen for
14382 o references to TOC base (SYM@toc / TOC[tc0]) without a
14383 .toc directive
14384 o bad linker script
14385 o --gc-sections and empty TOC sections
14386
14387 FIXME: Warn user? */
14388
14389 /* Look for a likely section. We probably won't even be
14390 using TOCstart. */
14391 for (s = obfd->sections; s != NULL; s = s->next)
14392 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14393 | SEC_EXCLUDE))
14394 == (SEC_ALLOC | SEC_SMALL_DATA))
14395 break;
14396 if (s == NULL)
14397 for (s = obfd->sections; s != NULL; s = s->next)
14398 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14399 == (SEC_ALLOC | SEC_SMALL_DATA))
14400 break;
14401 if (s == NULL)
14402 for (s = obfd->sections; s != NULL; s = s->next)
14403 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14404 == SEC_ALLOC)
14405 break;
14406 if (s == NULL)
14407 for (s = obfd->sections; s != NULL; s = s->next)
14408 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14409 break;
14410 }
14411
14412 TOCstart = 0;
14413 if (s != NULL)
14414 TOCstart = s->output_section->vma + s->output_offset;
14415
14416 /* Force alignment. */
14417 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14418 TOCstart -= adjust;
14419 _bfd_set_gp_value (obfd, TOCstart);
14420
14421 if (info != NULL && s != NULL)
14422 {
14423 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14424
14425 if (htab != NULL)
14426 {
14427 if (htab->elf.hgot != NULL)
14428 {
14429 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14430 htab->elf.hgot->root.u.def.section = s;
14431 }
14432 }
14433 else
14434 {
14435 struct bfd_link_hash_entry *bh = NULL;
14436 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14437 s, TOC_BASE_OFF - adjust,
14438 NULL, false, false, &bh);
14439 }
14440 }
14441 return TOCstart;
14442 }
14443
14444 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14445 write out any global entry stubs, and PLT relocations. */
14446
14447 static bool
14448 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14449 {
14450 struct bfd_link_info *info;
14451 struct ppc_link_hash_table *htab;
14452 struct plt_entry *ent;
14453 asection *s;
14454
14455 if (h->root.type == bfd_link_hash_indirect)
14456 return true;
14457
14458 info = inf;
14459 htab = ppc_hash_table (info);
14460 if (htab == NULL)
14461 return false;
14462
14463 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14464 if (ent->plt.offset != (bfd_vma) -1)
14465 {
14466 /* This symbol has an entry in the procedure linkage
14467 table. Set it up. */
14468 Elf_Internal_Rela rela;
14469 asection *plt, *relplt;
14470 bfd_byte *loc;
14471
14472 if (use_local_plt (info, h))
14473 {
14474 if (!(h->def_regular
14475 && (h->root.type == bfd_link_hash_defined
14476 || h->root.type == bfd_link_hash_defweak)))
14477 continue;
14478 if (h->type == STT_GNU_IFUNC)
14479 {
14480 plt = htab->elf.iplt;
14481 relplt = htab->elf.irelplt;
14482 htab->elf.ifunc_resolvers = true;
14483 if (htab->opd_abi)
14484 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14485 else
14486 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14487 }
14488 else
14489 {
14490 plt = htab->pltlocal;
14491 relplt = NULL;
14492 if (bfd_link_pic (info)
14493 && !(info->enable_dt_relr && !htab->opd_abi))
14494 {
14495 relplt = htab->relpltlocal;
14496 if (htab->opd_abi)
14497 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14498 else
14499 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14500 }
14501 }
14502 rela.r_addend = defined_sym_val (h) + ent->addend;
14503
14504 if (relplt == NULL)
14505 {
14506 loc = plt->contents + ent->plt.offset;
14507 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14508 if (htab->opd_abi)
14509 {
14510 bfd_vma toc = elf_gp (info->output_bfd);
14511 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14512 bfd_put_64 (info->output_bfd, toc, loc + 8);
14513 }
14514 }
14515 else
14516 {
14517 rela.r_offset = (plt->output_section->vma
14518 + plt->output_offset
14519 + ent->plt.offset);
14520 loc = relplt->contents + (relplt->reloc_count++
14521 * sizeof (Elf64_External_Rela));
14522 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14523 }
14524 }
14525 else
14526 {
14527 rela.r_offset = (htab->elf.splt->output_section->vma
14528 + htab->elf.splt->output_offset
14529 + ent->plt.offset);
14530 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14531 rela.r_addend = ent->addend;
14532 loc = (htab->elf.srelplt->contents
14533 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14534 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14535 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14536 htab->elf.ifunc_resolvers = true;
14537 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14538 }
14539 }
14540
14541 if (!h->pointer_equality_needed)
14542 return true;
14543
14544 if (h->def_regular)
14545 return true;
14546
14547 s = htab->global_entry;
14548 if (s == NULL || s->size == 0)
14549 return true;
14550
14551 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14552 if (ent->plt.offset != (bfd_vma) -1
14553 && ent->addend == 0)
14554 {
14555 bfd_byte *p;
14556 asection *plt;
14557 bfd_vma off;
14558
14559 p = s->contents + h->root.u.def.value;
14560 plt = htab->elf.splt;
14561 if (use_local_plt (info, h))
14562 {
14563 if (h->type == STT_GNU_IFUNC)
14564 plt = htab->elf.iplt;
14565 else
14566 plt = htab->pltlocal;
14567 }
14568 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14569 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14570
14571 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14572 {
14573 info->callbacks->einfo
14574 (_("%P: linkage table error against `%pT'\n"),
14575 h->root.root.string);
14576 bfd_set_error (bfd_error_bad_value);
14577 htab->stub_error = true;
14578 }
14579
14580 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14581 if (htab->params->emit_stub_syms)
14582 {
14583 size_t len = strlen (h->root.root.string);
14584 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14585
14586 if (name == NULL)
14587 return false;
14588
14589 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14590 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14591 if (h == NULL)
14592 return false;
14593 if (h->root.type == bfd_link_hash_new)
14594 {
14595 h->root.type = bfd_link_hash_defined;
14596 h->root.u.def.section = s;
14597 h->root.u.def.value = p - s->contents;
14598 h->ref_regular = 1;
14599 h->def_regular = 1;
14600 h->ref_regular_nonweak = 1;
14601 h->forced_local = 1;
14602 h->non_elf = 0;
14603 h->root.linker_def = 1;
14604 }
14605 }
14606
14607 if (PPC_HA (off) != 0)
14608 {
14609 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14610 p += 4;
14611 }
14612 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14613 p += 4;
14614 bfd_put_32 (s->owner, MTCTR_R12, p);
14615 p += 4;
14616 bfd_put_32 (s->owner, BCTR, p);
14617 break;
14618 }
14619 return true;
14620 }
14621
14622 /* Write PLT relocs for locals. */
14623
14624 static bool
14625 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14626 {
14627 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14628 bfd *ibfd;
14629
14630 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14631 {
14632 struct got_entry **lgot_ents, **end_lgot_ents;
14633 struct plt_entry **local_plt, **lplt, **end_local_plt;
14634 Elf_Internal_Shdr *symtab_hdr;
14635 bfd_size_type locsymcount;
14636 Elf_Internal_Sym *local_syms = NULL;
14637 struct plt_entry *ent;
14638
14639 if (!is_ppc64_elf (ibfd))
14640 continue;
14641
14642 lgot_ents = elf_local_got_ents (ibfd);
14643 if (!lgot_ents)
14644 continue;
14645
14646 symtab_hdr = &elf_symtab_hdr (ibfd);
14647 locsymcount = symtab_hdr->sh_info;
14648 end_lgot_ents = lgot_ents + locsymcount;
14649 local_plt = (struct plt_entry **) end_lgot_ents;
14650 end_local_plt = local_plt + locsymcount;
14651 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14652 for (ent = *lplt; ent != NULL; ent = ent->next)
14653 if (ent->plt.offset != (bfd_vma) -1)
14654 {
14655 Elf_Internal_Sym *sym;
14656 asection *sym_sec;
14657 asection *plt, *relplt;
14658 bfd_byte *loc;
14659 bfd_vma val;
14660
14661 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14662 lplt - local_plt, ibfd))
14663 {
14664 if (symtab_hdr->contents != (unsigned char *) local_syms)
14665 free (local_syms);
14666 return false;
14667 }
14668
14669 val = sym->st_value + ent->addend;
14670 if (sym_sec != NULL && sym_sec->output_section != NULL)
14671 val += sym_sec->output_offset + sym_sec->output_section->vma;
14672
14673 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14674 {
14675 htab->elf.ifunc_resolvers = true;
14676 plt = htab->elf.iplt;
14677 relplt = htab->elf.irelplt;
14678 }
14679 else
14680 {
14681 plt = htab->pltlocal;
14682 relplt = NULL;
14683 if (bfd_link_pic (info)
14684 && !(info->enable_dt_relr && !htab->opd_abi))
14685 relplt = htab->relpltlocal;
14686 }
14687
14688 if (relplt == NULL)
14689 {
14690 loc = plt->contents + ent->plt.offset;
14691 bfd_put_64 (info->output_bfd, val, loc);
14692 if (htab->opd_abi)
14693 {
14694 bfd_vma toc = elf_gp (ibfd);
14695 bfd_put_64 (info->output_bfd, toc, loc + 8);
14696 }
14697 }
14698 else
14699 {
14700 Elf_Internal_Rela rela;
14701 rela.r_offset = (ent->plt.offset
14702 + plt->output_offset
14703 + plt->output_section->vma);
14704 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14705 {
14706 if (htab->opd_abi)
14707 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14708 else
14709 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14710 }
14711 else
14712 {
14713 if (htab->opd_abi)
14714 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14715 else
14716 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14717 }
14718 rela.r_addend = val;
14719 loc = relplt->contents + (relplt->reloc_count++
14720 * sizeof (Elf64_External_Rela));
14721 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14722 }
14723 }
14724
14725 if (local_syms != NULL
14726 && symtab_hdr->contents != (unsigned char *) local_syms)
14727 {
14728 if (!info->keep_memory)
14729 free (local_syms);
14730 else
14731 symtab_hdr->contents = (unsigned char *) local_syms;
14732 }
14733 }
14734 return true;
14735 }
14736
14737 /* Emit the static wrapper function preserving registers around a
14738 __tls_get_addr_opt call. */
14739
14740 static bool
14741 emit_tga_desc (struct ppc_link_hash_table *htab)
14742 {
14743 asection *stub_sec = htab->tga_group->stub_sec;
14744 unsigned int cfa_updt = 11 * 4;
14745 bfd_byte *p;
14746 bfd_vma to, from, delta;
14747
14748 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14749 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14750 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14751 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14752 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14753 delta = to - from;
14754 if (delta + (1 << 25) >= 1 << 26)
14755 {
14756 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14757 htab->stub_error = true;
14758 return false;
14759 }
14760
14761 p = stub_sec->contents;
14762 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14763 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14764 p += 4;
14765 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14766 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14767 }
14768
14769 /* Emit eh_frame describing the static wrapper function. */
14770
14771 static bfd_byte *
14772 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14773 {
14774 unsigned int cfa_updt = 11 * 4;
14775 unsigned int i;
14776
14777 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14778 *p++ = DW_CFA_def_cfa_offset;
14779 if (htab->opd_abi)
14780 {
14781 *p++ = 128;
14782 *p++ = 1;
14783 }
14784 else
14785 *p++ = 96;
14786 *p++ = DW_CFA_offset_extended_sf;
14787 *p++ = 65;
14788 *p++ = (-16 / 8) & 0x7f;
14789 for (i = 4; i < 12; i++)
14790 {
14791 *p++ = DW_CFA_offset + i;
14792 *p++ = (htab->opd_abi ? 13 : 12) - i;
14793 }
14794 *p++ = DW_CFA_advance_loc + 10;
14795 *p++ = DW_CFA_def_cfa_offset;
14796 *p++ = 0;
14797 for (i = 4; i < 12; i++)
14798 *p++ = DW_CFA_restore + i;
14799 *p++ = DW_CFA_advance_loc + 2;
14800 *p++ = DW_CFA_restore_extended;
14801 *p++ = 65;
14802 return p;
14803 }
14804
14805 /* Build all the stubs associated with the current output file.
14806 The stubs are kept in a hash table attached to the main linker
14807 hash table. This function is called via gldelf64ppc_finish. */
14808
14809 bool
14810 ppc64_elf_build_stubs (struct bfd_link_info *info,
14811 char **stats)
14812 {
14813 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14814 struct map_stub *group;
14815 asection *stub_sec;
14816 bfd_byte *p;
14817 int stub_sec_count = 0;
14818
14819 if (htab == NULL)
14820 return false;
14821
14822 /* Allocate memory to hold the linker stubs. */
14823 for (group = htab->group; group != NULL; group = group->next)
14824 {
14825 group->eh_size = 0;
14826 group->lr_restore = 0;
14827 if ((stub_sec = group->stub_sec) != NULL
14828 && stub_sec->size != 0)
14829 {
14830 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14831 stub_sec->size);
14832 if (stub_sec->contents == NULL)
14833 return false;
14834 stub_sec->size = 0;
14835 }
14836 }
14837
14838 if (htab->glink != NULL && htab->glink->size != 0)
14839 {
14840 unsigned int indx;
14841 bfd_vma plt0;
14842
14843 /* Build the .glink plt call stub. */
14844 if (htab->params->emit_stub_syms)
14845 {
14846 struct elf_link_hash_entry *h;
14847 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14848 true, false, false);
14849 if (h == NULL)
14850 return false;
14851 if (h->root.type == bfd_link_hash_new)
14852 {
14853 h->root.type = bfd_link_hash_defined;
14854 h->root.u.def.section = htab->glink;
14855 h->root.u.def.value = 8;
14856 h->ref_regular = 1;
14857 h->def_regular = 1;
14858 h->ref_regular_nonweak = 1;
14859 h->forced_local = 1;
14860 h->non_elf = 0;
14861 h->root.linker_def = 1;
14862 }
14863 }
14864 plt0 = (htab->elf.splt->output_section->vma
14865 + htab->elf.splt->output_offset
14866 - 16);
14867 if (info->emitrelocations)
14868 {
14869 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14870 if (r == NULL)
14871 return false;
14872 r->r_offset = (htab->glink->output_offset
14873 + htab->glink->output_section->vma);
14874 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14875 r->r_addend = plt0;
14876 }
14877 p = htab->glink->contents;
14878 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14879 bfd_put_64 (htab->glink->owner, plt0, p);
14880 p += 8;
14881 if (htab->opd_abi)
14882 {
14883 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14884 p += 4;
14885 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14886 p += 4;
14887 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14888 p += 4;
14889 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14890 p += 4;
14891 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14892 p += 4;
14893 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14894 p += 4;
14895 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14896 p += 4;
14897 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14898 p += 4;
14899 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14900 p += 4;
14901 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14902 p += 4;
14903 }
14904 else
14905 {
14906 unsigned int insn;
14907
14908 /* 0:
14909 . .quad plt0-1f # plt0 entry relative to 1:
14910 #
14911 # We get here with r12 initially @ a glink branch
14912 # Load the address of _dl_runtime_resolve from plt0 and
14913 # jump to it, with r0 set to the index of the PLT entry
14914 # to be resolved and r11 the link map.
14915 __glink_PLTresolve:
14916 . std %r2,24(%r1) # optional
14917 . mflr %r0
14918 . bcl 20,31,1f
14919 1:
14920 . mflr %r11
14921 . mtlr %r0
14922 . ld %r0,(0b-1b)(%r11)
14923 . sub %r12,%r12,%r11
14924 . add %r11,%r0,%r11
14925 . addi %r0,%r12,1b-2f
14926 . ld %r12,0(%r11)
14927 . srdi %r0,%r0,2
14928 . mtctr %r12
14929 . ld %r11,8(%r11)
14930 . bctr
14931 2:
14932 . b __glink_PLTresolve
14933 . ...
14934 . b __glink_PLTresolve */
14935
14936 if (htab->has_plt_localentry0)
14937 {
14938 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14939 p += 4;
14940 }
14941 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14942 p += 4;
14943 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14944 p += 4;
14945 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14946 p += 4;
14947 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14948 p += 4;
14949 if (htab->has_plt_localentry0)
14950 insn = LD_R0_0R11 | (-20 & 0xfffc);
14951 else
14952 insn = LD_R0_0R11 | (-16 & 0xfffc);
14953 bfd_put_32 (htab->glink->owner, insn, p);
14954 p += 4;
14955 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14956 p += 4;
14957 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
14958 p += 4;
14959 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
14960 p += 4;
14961 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14962 p += 4;
14963 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14964 p += 4;
14965 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14966 p += 4;
14967 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14968 p += 4;
14969 }
14970 bfd_put_32 (htab->glink->owner, BCTR, p);
14971 p += 4;
14972 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14973
14974 /* Build the .glink lazy link call stubs. */
14975 indx = 0;
14976 while (p < htab->glink->contents + htab->glink->size)
14977 {
14978 if (htab->opd_abi)
14979 {
14980 if (indx < 0x8000)
14981 {
14982 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14983 p += 4;
14984 }
14985 else
14986 {
14987 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14988 p += 4;
14989 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14990 p);
14991 p += 4;
14992 }
14993 }
14994 bfd_put_32 (htab->glink->owner,
14995 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14996 indx++;
14997 p += 4;
14998 }
14999 }
15000
15001 if (htab->tga_group != NULL)
15002 {
15003 htab->tga_group->lr_restore = 23 * 4;
15004 htab->tga_group->stub_sec->size = 24 * 4;
15005 if (!emit_tga_desc (htab))
15006 return false;
15007 if (htab->glink_eh_frame != NULL
15008 && htab->glink_eh_frame->size != 0)
15009 {
15010 size_t align = 4;
15011
15012 p = htab->glink_eh_frame->contents;
15013 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15014 p += 17;
15015 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15016 }
15017 }
15018
15019 /* Build .glink global entry stubs, and PLT relocs for globals. */
15020 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15021
15022 if (!write_plt_relocs_for_local_syms (info))
15023 return false;
15024
15025 if (htab->brlt != NULL && htab->brlt->size != 0)
15026 {
15027 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15028 htab->brlt->size);
15029 if (htab->brlt->contents == NULL)
15030 return false;
15031 }
15032 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15033 {
15034 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15035 htab->relbrlt->size);
15036 if (htab->relbrlt->contents == NULL)
15037 return false;
15038 }
15039
15040 /* Build the stubs as directed by the stub hash table. */
15041 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15042
15043 for (group = htab->group; group != NULL; group = group->next)
15044 if (group->needs_save_res)
15045 group->stub_sec->size += htab->sfpr->size;
15046
15047 if (htab->relbrlt != NULL)
15048 htab->relbrlt->reloc_count = 0;
15049
15050 if (htab->params->plt_stub_align != 0)
15051 for (group = htab->group; group != NULL; group = group->next)
15052 if ((stub_sec = group->stub_sec) != NULL)
15053 {
15054 int align = abs (htab->params->plt_stub_align);
15055 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15056 }
15057
15058 for (group = htab->group; group != NULL; group = group->next)
15059 if (group->needs_save_res)
15060 {
15061 stub_sec = group->stub_sec;
15062 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15063 htab->sfpr->contents, htab->sfpr->size);
15064 if (htab->params->emit_stub_syms)
15065 {
15066 unsigned int i;
15067
15068 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15069 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15070 return false;
15071 }
15072 }
15073
15074 if (htab->glink_eh_frame != NULL
15075 && htab->glink_eh_frame->size != 0)
15076 {
15077 bfd_vma val;
15078 size_t align = 4;
15079
15080 p = htab->glink_eh_frame->contents;
15081 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15082
15083 for (group = htab->group; group != NULL; group = group->next)
15084 if (group->eh_size != 0)
15085 {
15086 /* Offset to stub section. */
15087 val = (group->stub_sec->output_section->vma
15088 + group->stub_sec->output_offset);
15089 val -= (htab->glink_eh_frame->output_section->vma
15090 + htab->glink_eh_frame->output_offset
15091 + (p + 8 - htab->glink_eh_frame->contents));
15092 if (val + 0x80000000 > 0xffffffff)
15093 {
15094 _bfd_error_handler
15095 (_("%s offset too large for .eh_frame sdata4 encoding"),
15096 group->stub_sec->name);
15097 return false;
15098 }
15099 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15100 p += (group->eh_size + 17 + 3) & -4;
15101 }
15102 if (htab->glink != NULL && htab->glink->size != 0)
15103 {
15104 /* Offset to .glink. */
15105 val = (htab->glink->output_section->vma
15106 + htab->glink->output_offset
15107 + 8);
15108 val -= (htab->glink_eh_frame->output_section->vma
15109 + htab->glink_eh_frame->output_offset
15110 + (p + 8 - htab->glink_eh_frame->contents));
15111 if (val + 0x80000000 > 0xffffffff)
15112 {
15113 _bfd_error_handler
15114 (_("%s offset too large for .eh_frame sdata4 encoding"),
15115 htab->glink->name);
15116 return false;
15117 }
15118 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15119 p += (24 + align - 1) & -align;
15120 }
15121 }
15122
15123 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15124 {
15125 htab->elf.srelrdyn->contents
15126 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15127 if (htab->elf.srelrdyn->contents == NULL)
15128 return false;
15129
15130 size_t i = 0;
15131 bfd_byte *loc = htab->elf.srelrdyn->contents;
15132 while (i < htab->relr_count)
15133 {
15134 bfd_vma base = htab->relr_addr[i];
15135 BFD_ASSERT (base % 2 == 0);
15136 bfd_put_64 (htab->elf.dynobj, base, loc);
15137 loc += 8;
15138 i++;
15139 while (i < htab->relr_count
15140 && htab->relr_addr[i] == base)
15141 {
15142 htab->stub_error = true;
15143 i++;
15144 }
15145 base += 8;
15146 while (1)
15147 {
15148 bfd_vma bits = 0;
15149 while (i < htab->relr_count
15150 && htab->relr_addr[i] - base < 63 * 8
15151 && (htab->relr_addr[i] - base) % 8 == 0)
15152 {
15153 bits |= (bfd_vma) 1 << ((htab->relr_addr[i] - base) / 8);
15154 i++;
15155 }
15156 if (bits == 0)
15157 break;
15158 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15159 loc += 8;
15160 base += 63 * 8;
15161 }
15162 }
15163 /* Pad any excess with 1's, a do-nothing encoding. */
15164 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15165 < htab->elf.srelrdyn->size)
15166 {
15167 bfd_put_64 (htab->elf.dynobj, 1, loc);
15168 loc += 8;
15169 }
15170 }
15171
15172 for (group = htab->group; group != NULL; group = group->next)
15173 if ((stub_sec = group->stub_sec) != NULL)
15174 {
15175 stub_sec_count += 1;
15176 if (stub_sec->rawsize != stub_sec->size
15177 && (htab->stub_iteration <= STUB_SHRINK_ITER
15178 || stub_sec->rawsize < stub_sec->size))
15179 break;
15180 }
15181
15182 if (group != NULL)
15183 htab->stub_error = true;
15184
15185 if (htab->stub_error)
15186 {
15187 _bfd_error_handler (_("stubs don't match calculated size"));
15188 return false;
15189 }
15190
15191 if (stats != NULL)
15192 {
15193 char *groupmsg;
15194 if (asprintf (&groupmsg,
15195 ngettext ("linker stubs in %u group\n",
15196 "linker stubs in %u groups\n",
15197 stub_sec_count),
15198 stub_sec_count) < 0)
15199 *stats = NULL;
15200 else
15201 {
15202 if (asprintf (stats, _("%s"
15203 " branch %lu\n"
15204 " long branch %lu\n"
15205 " plt call %lu\n"
15206 " global entry %lu"),
15207 groupmsg,
15208 htab->stub_count[ppc_stub_long_branch - 1],
15209 htab->stub_count[ppc_stub_plt_branch - 1],
15210 htab->stub_count[ppc_stub_plt_call - 1],
15211 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15212 *stats = NULL;
15213 free (groupmsg);
15214 }
15215 }
15216 return true;
15217 }
15218
15219 /* What to do when ld finds relocations against symbols defined in
15220 discarded sections. */
15221
15222 static unsigned int
15223 ppc64_elf_action_discarded (asection *sec)
15224 {
15225 if (strcmp (".opd", sec->name) == 0)
15226 return 0;
15227
15228 if (strcmp (".toc", sec->name) == 0)
15229 return 0;
15230
15231 if (strcmp (".toc1", sec->name) == 0)
15232 return 0;
15233
15234 return _bfd_elf_default_action_discarded (sec);
15235 }
15236
15237 /* These are the dynamic relocations supported by glibc. */
15238
15239 static bool
15240 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15241 {
15242 switch (r_type)
15243 {
15244 case R_PPC64_RELATIVE:
15245 case R_PPC64_NONE:
15246 case R_PPC64_ADDR64:
15247 case R_PPC64_GLOB_DAT:
15248 case R_PPC64_IRELATIVE:
15249 case R_PPC64_JMP_IREL:
15250 case R_PPC64_JMP_SLOT:
15251 case R_PPC64_DTPMOD64:
15252 case R_PPC64_DTPREL64:
15253 case R_PPC64_TPREL64:
15254 case R_PPC64_TPREL16_LO_DS:
15255 case R_PPC64_TPREL16_DS:
15256 case R_PPC64_TPREL16:
15257 case R_PPC64_TPREL16_LO:
15258 case R_PPC64_TPREL16_HI:
15259 case R_PPC64_TPREL16_HIGH:
15260 case R_PPC64_TPREL16_HA:
15261 case R_PPC64_TPREL16_HIGHA:
15262 case R_PPC64_TPREL16_HIGHER:
15263 case R_PPC64_TPREL16_HIGHEST:
15264 case R_PPC64_TPREL16_HIGHERA:
15265 case R_PPC64_TPREL16_HIGHESTA:
15266 case R_PPC64_ADDR16_LO_DS:
15267 case R_PPC64_ADDR16_LO:
15268 case R_PPC64_ADDR16_HI:
15269 case R_PPC64_ADDR16_HIGH:
15270 case R_PPC64_ADDR16_HA:
15271 case R_PPC64_ADDR16_HIGHA:
15272 case R_PPC64_REL30:
15273 case R_PPC64_COPY:
15274 case R_PPC64_UADDR64:
15275 case R_PPC64_UADDR32:
15276 case R_PPC64_ADDR32:
15277 case R_PPC64_ADDR24:
15278 case R_PPC64_ADDR16:
15279 case R_PPC64_UADDR16:
15280 case R_PPC64_ADDR16_DS:
15281 case R_PPC64_ADDR16_HIGHER:
15282 case R_PPC64_ADDR16_HIGHEST:
15283 case R_PPC64_ADDR16_HIGHERA:
15284 case R_PPC64_ADDR16_HIGHESTA:
15285 case R_PPC64_ADDR14:
15286 case R_PPC64_ADDR14_BRTAKEN:
15287 case R_PPC64_ADDR14_BRNTAKEN:
15288 case R_PPC64_REL32:
15289 case R_PPC64_REL64:
15290 return true;
15291
15292 default:
15293 return false;
15294 }
15295 }
15296
15297 /* The RELOCATE_SECTION function is called by the ELF backend linker
15298 to handle the relocations for a section.
15299
15300 The relocs are always passed as Rela structures; if the section
15301 actually uses Rel structures, the r_addend field will always be
15302 zero.
15303
15304 This function is responsible for adjust the section contents as
15305 necessary, and (if using Rela relocs and generating a
15306 relocatable output file) adjusting the reloc addend as
15307 necessary.
15308
15309 This function does not have to worry about setting the reloc
15310 address or the reloc symbol index.
15311
15312 LOCAL_SYMS is a pointer to the swapped in local symbols.
15313
15314 LOCAL_SECTIONS is an array giving the section in the input file
15315 corresponding to the st_shndx field of each local symbol.
15316
15317 The global hash table entry for the global symbols can be found
15318 via elf_sym_hashes (input_bfd).
15319
15320 When generating relocatable output, this function must handle
15321 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15322 going to be the section symbol corresponding to the output
15323 section, which means that the addend must be adjusted
15324 accordingly. */
15325
15326 static int
15327 ppc64_elf_relocate_section (bfd *output_bfd,
15328 struct bfd_link_info *info,
15329 bfd *input_bfd,
15330 asection *input_section,
15331 bfd_byte *contents,
15332 Elf_Internal_Rela *relocs,
15333 Elf_Internal_Sym *local_syms,
15334 asection **local_sections)
15335 {
15336 struct ppc_link_hash_table *htab;
15337 Elf_Internal_Shdr *symtab_hdr;
15338 struct elf_link_hash_entry **sym_hashes;
15339 Elf_Internal_Rela *rel;
15340 Elf_Internal_Rela *wrel;
15341 Elf_Internal_Rela *relend;
15342 Elf_Internal_Rela outrel;
15343 bfd_byte *loc;
15344 struct got_entry **local_got_ents;
15345 bfd_vma TOCstart;
15346 bool ret = true;
15347 bool is_opd;
15348 /* Assume 'at' branch hints. */
15349 bool is_isa_v2 = true;
15350 bool warned_dynamic = false;
15351 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15352
15353 /* Initialize howto table if needed. */
15354 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15355 ppc_howto_init ();
15356
15357 htab = ppc_hash_table (info);
15358 if (htab == NULL)
15359 return false;
15360
15361 /* Don't relocate stub sections. */
15362 if (input_section->owner == htab->params->stub_bfd)
15363 return true;
15364
15365 if (!is_ppc64_elf (input_bfd))
15366 {
15367 bfd_set_error (bfd_error_wrong_format);
15368 return false;
15369 }
15370
15371 local_got_ents = elf_local_got_ents (input_bfd);
15372 TOCstart = elf_gp (output_bfd);
15373 symtab_hdr = &elf_symtab_hdr (input_bfd);
15374 sym_hashes = elf_sym_hashes (input_bfd);
15375 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15376
15377 rel = wrel = relocs;
15378 relend = relocs + input_section->reloc_count;
15379 for (; rel < relend; wrel++, rel++)
15380 {
15381 enum elf_ppc64_reloc_type r_type;
15382 bfd_vma addend;
15383 bfd_reloc_status_type r;
15384 Elf_Internal_Sym *sym;
15385 asection *sec;
15386 struct elf_link_hash_entry *h_elf;
15387 struct ppc_link_hash_entry *h;
15388 struct ppc_link_hash_entry *fdh;
15389 const char *sym_name;
15390 unsigned long r_symndx, toc_symndx;
15391 bfd_vma toc_addend;
15392 unsigned char tls_mask, tls_gd, tls_type;
15393 unsigned char sym_type;
15394 bfd_vma relocation;
15395 bool unresolved_reloc, save_unresolved_reloc;
15396 bool warned;
15397 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15398 unsigned int insn;
15399 unsigned int mask;
15400 struct ppc_stub_hash_entry *stub_entry;
15401 bfd_vma max_br_offset;
15402 bfd_vma from;
15403 Elf_Internal_Rela orig_rel;
15404 reloc_howto_type *howto;
15405 struct reloc_howto_struct alt_howto;
15406 uint64_t pinsn;
15407 bfd_vma offset;
15408
15409 again:
15410 orig_rel = *rel;
15411
15412 r_type = ELF64_R_TYPE (rel->r_info);
15413 r_symndx = ELF64_R_SYM (rel->r_info);
15414
15415 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15416 symbol of the previous ADDR64 reloc. The symbol gives us the
15417 proper TOC base to use. */
15418 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15419 && wrel != relocs
15420 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15421 && is_opd)
15422 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15423
15424 sym = NULL;
15425 sec = NULL;
15426 h_elf = NULL;
15427 sym_name = NULL;
15428 unresolved_reloc = false;
15429 warned = false;
15430
15431 if (r_symndx < symtab_hdr->sh_info)
15432 {
15433 /* It's a local symbol. */
15434 struct _opd_sec_data *opd;
15435
15436 sym = local_syms + r_symndx;
15437 sec = local_sections[r_symndx];
15438 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15439 sym_type = ELF64_ST_TYPE (sym->st_info);
15440 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15441 opd = get_opd_info (sec);
15442 if (opd != NULL && opd->adjust != NULL)
15443 {
15444 long adjust = opd->adjust[OPD_NDX (sym->st_value
15445 + rel->r_addend)];
15446 if (adjust == -1)
15447 relocation = 0;
15448 else
15449 {
15450 /* If this is a relocation against the opd section sym
15451 and we have edited .opd, adjust the reloc addend so
15452 that ld -r and ld --emit-relocs output is correct.
15453 If it is a reloc against some other .opd symbol,
15454 then the symbol value will be adjusted later. */
15455 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15456 rel->r_addend += adjust;
15457 else
15458 relocation += adjust;
15459 }
15460 }
15461 }
15462 else
15463 {
15464 bool ignored;
15465
15466 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15467 r_symndx, symtab_hdr, sym_hashes,
15468 h_elf, sec, relocation,
15469 unresolved_reloc, warned, ignored);
15470 sym_name = h_elf->root.root.string;
15471 sym_type = h_elf->type;
15472 if (sec != NULL
15473 && sec->owner == output_bfd
15474 && strcmp (sec->name, ".opd") == 0)
15475 {
15476 /* This is a symbol defined in a linker script. All
15477 such are defined in output sections, even those
15478 defined by simple assignment from a symbol defined in
15479 an input section. Transfer the symbol to an
15480 appropriate input .opd section, so that a branch to
15481 this symbol will be mapped to the location specified
15482 by the opd entry. */
15483 struct bfd_link_order *lo;
15484 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15485 if (lo->type == bfd_indirect_link_order)
15486 {
15487 asection *isec = lo->u.indirect.section;
15488 if (h_elf->root.u.def.value >= isec->output_offset
15489 && h_elf->root.u.def.value < (isec->output_offset
15490 + isec->size))
15491 {
15492 h_elf->root.u.def.value -= isec->output_offset;
15493 h_elf->root.u.def.section = isec;
15494 sec = isec;
15495 break;
15496 }
15497 }
15498 }
15499 }
15500 h = ppc_elf_hash_entry (h_elf);
15501
15502 if (sec != NULL && discarded_section (sec))
15503 {
15504 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15505 input_bfd, input_section,
15506 contents, rel->r_offset);
15507 wrel->r_offset = rel->r_offset;
15508 wrel->r_info = 0;
15509 wrel->r_addend = 0;
15510
15511 /* For ld -r, remove relocations in debug sections against
15512 symbols defined in discarded sections. Not done for
15513 non-debug to preserve relocs in .eh_frame which the
15514 eh_frame editing code expects to be present. */
15515 if (bfd_link_relocatable (info)
15516 && (input_section->flags & SEC_DEBUGGING))
15517 wrel--;
15518
15519 continue;
15520 }
15521
15522 if (bfd_link_relocatable (info))
15523 goto copy_reloc;
15524
15525 if (h != NULL && &h->elf == htab->elf.hgot)
15526 {
15527 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15528 sec = bfd_abs_section_ptr;
15529 unresolved_reloc = false;
15530 }
15531
15532 /* TLS optimizations. Replace instruction sequences and relocs
15533 based on information we collected in tls_optimize. We edit
15534 RELOCS so that --emit-relocs will output something sensible
15535 for the final instruction stream. */
15536 tls_mask = 0;
15537 tls_gd = 0;
15538 toc_symndx = 0;
15539 if (h != NULL)
15540 tls_mask = h->tls_mask;
15541 else if (local_got_ents != NULL)
15542 {
15543 struct plt_entry **local_plt = (struct plt_entry **)
15544 (local_got_ents + symtab_hdr->sh_info);
15545 unsigned char *lgot_masks = (unsigned char *)
15546 (local_plt + symtab_hdr->sh_info);
15547 tls_mask = lgot_masks[r_symndx];
15548 }
15549 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15550 && (r_type == R_PPC64_TLS
15551 || r_type == R_PPC64_TLSGD
15552 || r_type == R_PPC64_TLSLD))
15553 {
15554 /* Check for toc tls entries. */
15555 unsigned char *toc_tls;
15556
15557 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15558 &local_syms, rel, input_bfd))
15559 return false;
15560
15561 if (toc_tls)
15562 tls_mask = *toc_tls;
15563 }
15564
15565 /* Check that tls relocs are used with tls syms, and non-tls
15566 relocs are used with non-tls syms. */
15567 if (r_symndx != STN_UNDEF
15568 && r_type != R_PPC64_NONE
15569 && (h == NULL
15570 || h->elf.root.type == bfd_link_hash_defined
15571 || h->elf.root.type == bfd_link_hash_defweak)
15572 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15573 {
15574 if ((tls_mask & TLS_TLS) != 0
15575 && (r_type == R_PPC64_TLS
15576 || r_type == R_PPC64_TLSGD
15577 || r_type == R_PPC64_TLSLD))
15578 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15579 ;
15580 else
15581 info->callbacks->einfo
15582 (!IS_PPC64_TLS_RELOC (r_type)
15583 /* xgettext:c-format */
15584 ? _("%H: %s used with TLS symbol `%pT'\n")
15585 /* xgettext:c-format */
15586 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15587 input_bfd, input_section, rel->r_offset,
15588 ppc64_elf_howto_table[r_type]->name,
15589 sym_name);
15590 }
15591
15592 /* Ensure reloc mapping code below stays sane. */
15593 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15594 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15595 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15596 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15597 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15598 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15599 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15600 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15601 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15602 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15603 abort ();
15604
15605 switch (r_type)
15606 {
15607 default:
15608 break;
15609
15610 case R_PPC64_LO_DS_OPT:
15611 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15612 {
15613 insn = bfd_get_32 (input_bfd,
15614 contents + rel->r_offset - d_offset);
15615 if ((insn & (0x3fu << 26)) != 58u << 26)
15616 abort ();
15617 insn += (14u << 26) - (58u << 26);
15618 bfd_put_32 (input_bfd, insn,
15619 contents + rel->r_offset - d_offset);
15620 r_type = R_PPC64_TOC16_LO;
15621 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15622 }
15623 break;
15624
15625 case R_PPC64_TOC16:
15626 case R_PPC64_TOC16_LO:
15627 case R_PPC64_TOC16_DS:
15628 case R_PPC64_TOC16_LO_DS:
15629 {
15630 /* Check for toc tls entries. */
15631 unsigned char *toc_tls;
15632 int retval;
15633
15634 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15635 &local_syms, rel, input_bfd);
15636 if (retval == 0)
15637 return false;
15638
15639 if (toc_tls)
15640 {
15641 tls_mask = *toc_tls;
15642 if (r_type == R_PPC64_TOC16_DS
15643 || r_type == R_PPC64_TOC16_LO_DS)
15644 {
15645 if ((tls_mask & TLS_TLS) != 0
15646 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15647 goto toctprel;
15648 }
15649 else
15650 {
15651 /* If we found a GD reloc pair, then we might be
15652 doing a GD->IE transition. */
15653 if (retval == 2)
15654 {
15655 tls_gd = TLS_GDIE;
15656 if ((tls_mask & TLS_TLS) != 0
15657 && (tls_mask & TLS_GD) == 0)
15658 goto tls_ldgd_opt;
15659 }
15660 else if (retval == 3)
15661 {
15662 if ((tls_mask & TLS_TLS) != 0
15663 && (tls_mask & TLS_LD) == 0)
15664 goto tls_ldgd_opt;
15665 }
15666 }
15667 }
15668 }
15669 break;
15670
15671 case R_PPC64_GOT_TPREL16_HI:
15672 case R_PPC64_GOT_TPREL16_HA:
15673 if ((tls_mask & TLS_TLS) != 0
15674 && (tls_mask & TLS_TPREL) == 0
15675 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15676 {
15677 rel->r_offset -= d_offset;
15678 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15679 r_type = R_PPC64_NONE;
15680 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15681 }
15682 break;
15683
15684 case R_PPC64_GOT_TPREL16_DS:
15685 case R_PPC64_GOT_TPREL16_LO_DS:
15686 if ((tls_mask & TLS_TLS) != 0
15687 && (tls_mask & TLS_TPREL) == 0
15688 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15689 {
15690 toctprel:
15691 insn = bfd_get_32 (input_bfd,
15692 contents + rel->r_offset - d_offset);
15693 insn &= 31 << 21;
15694 insn |= 0x3c0d0000; /* addis 0,13,0 */
15695 bfd_put_32 (input_bfd, insn,
15696 contents + rel->r_offset - d_offset);
15697 r_type = R_PPC64_TPREL16_HA;
15698 if (toc_symndx != 0)
15699 {
15700 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15701 rel->r_addend = toc_addend;
15702 /* We changed the symbol. Start over in order to
15703 get h, sym, sec etc. right. */
15704 goto again;
15705 }
15706 else
15707 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15708 }
15709 break;
15710
15711 case R_PPC64_GOT_TPREL_PCREL34:
15712 if ((tls_mask & TLS_TLS) != 0
15713 && (tls_mask & TLS_TPREL) == 0
15714 && offset_in_range (input_section, rel->r_offset, 8))
15715 {
15716 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15717 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15718 pinsn <<= 32;
15719 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15720 pinsn += ((2ULL << 56) + (-1ULL << 52)
15721 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15722 bfd_put_32 (input_bfd, pinsn >> 32,
15723 contents + rel->r_offset);
15724 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15725 contents + rel->r_offset + 4);
15726 r_type = R_PPC64_TPREL34;
15727 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15728 }
15729 break;
15730
15731 case R_PPC64_TLS:
15732 if ((tls_mask & TLS_TLS) != 0
15733 && (tls_mask & TLS_TPREL) == 0
15734 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15735 {
15736 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15737 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15738 if (insn == 0)
15739 break;
15740 if ((rel->r_offset & 3) == 0)
15741 {
15742 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15743 /* Was PPC64_TLS which sits on insn boundary, now
15744 PPC64_TPREL16_LO which is at low-order half-word. */
15745 rel->r_offset += d_offset;
15746 r_type = R_PPC64_TPREL16_LO;
15747 if (toc_symndx != 0)
15748 {
15749 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15750 rel->r_addend = toc_addend;
15751 /* We changed the symbol. Start over in order to
15752 get h, sym, sec etc. right. */
15753 goto again;
15754 }
15755 else
15756 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15757 }
15758 else if ((rel->r_offset & 3) == 1)
15759 {
15760 /* For pcrel IE to LE we already have the full
15761 offset and thus don't need an addi here. A nop
15762 or mr will do. */
15763 if ((insn & (0x3fu << 26)) == 14 << 26)
15764 {
15765 /* Extract regs from addi rt,ra,si. */
15766 unsigned int rt = (insn >> 21) & 0x1f;
15767 unsigned int ra = (insn >> 16) & 0x1f;
15768 if (rt == ra)
15769 insn = NOP;
15770 else
15771 {
15772 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15773 insn = (rt << 16) | (ra << 21) | (ra << 11);
15774 insn |= (31u << 26) | (444u << 1);
15775 }
15776 }
15777 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15778 }
15779 }
15780 break;
15781
15782 case R_PPC64_GOT_TLSGD16_HI:
15783 case R_PPC64_GOT_TLSGD16_HA:
15784 tls_gd = TLS_GDIE;
15785 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15786 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15787 goto tls_gdld_hi;
15788 break;
15789
15790 case R_PPC64_GOT_TLSLD16_HI:
15791 case R_PPC64_GOT_TLSLD16_HA:
15792 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15793 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15794 {
15795 tls_gdld_hi:
15796 if ((tls_mask & tls_gd) != 0)
15797 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15798 + R_PPC64_GOT_TPREL16_DS);
15799 else
15800 {
15801 rel->r_offset -= d_offset;
15802 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15803 r_type = R_PPC64_NONE;
15804 }
15805 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15806 }
15807 break;
15808
15809 case R_PPC64_GOT_TLSGD16:
15810 case R_PPC64_GOT_TLSGD16_LO:
15811 tls_gd = TLS_GDIE;
15812 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15813 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15814 goto tls_ldgd_opt;
15815 break;
15816
15817 case R_PPC64_GOT_TLSLD16:
15818 case R_PPC64_GOT_TLSLD16_LO:
15819 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15820 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15821 {
15822 unsigned int insn1, insn2;
15823
15824 tls_ldgd_opt:
15825 offset = (bfd_vma) -1;
15826 /* If not using the newer R_PPC64_TLSGD/LD to mark
15827 __tls_get_addr calls, we must trust that the call
15828 stays with its arg setup insns, ie. that the next
15829 reloc is the __tls_get_addr call associated with
15830 the current reloc. Edit both insns. */
15831 if (input_section->nomark_tls_get_addr
15832 && rel + 1 < relend
15833 && branch_reloc_hash_match (input_bfd, rel + 1,
15834 htab->tls_get_addr_fd,
15835 htab->tga_desc_fd,
15836 htab->tls_get_addr,
15837 htab->tga_desc))
15838 offset = rel[1].r_offset;
15839 /* We read the low GOT_TLS (or TOC16) insn because we
15840 need to keep the destination reg. It may be
15841 something other than the usual r3, and moved to r3
15842 before the call by intervening code. */
15843 insn1 = bfd_get_32 (input_bfd,
15844 contents + rel->r_offset - d_offset);
15845 if ((tls_mask & tls_gd) != 0)
15846 {
15847 /* IE */
15848 insn1 &= (0x1f << 21) | (0x1f << 16);
15849 insn1 |= 58u << 26; /* ld */
15850 insn2 = 0x7c636a14; /* add 3,3,13 */
15851 if (offset != (bfd_vma) -1)
15852 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15853 if (r_type == R_PPC64_TOC16
15854 || r_type == R_PPC64_TOC16_LO)
15855 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15856 else
15857 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15858 + R_PPC64_GOT_TPREL16_DS);
15859 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15860 }
15861 else
15862 {
15863 /* LE */
15864 insn1 &= 0x1f << 21;
15865 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15866 insn2 = 0x38630000; /* addi 3,3,0 */
15867 if (tls_gd == 0)
15868 {
15869 /* Was an LD reloc. */
15870 r_symndx = STN_UNDEF;
15871 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15872 }
15873 else if (toc_symndx != 0)
15874 {
15875 r_symndx = toc_symndx;
15876 rel->r_addend = toc_addend;
15877 }
15878 r_type = R_PPC64_TPREL16_HA;
15879 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15880 if (offset != (bfd_vma) -1)
15881 {
15882 rel[1].r_info = ELF64_R_INFO (r_symndx,
15883 R_PPC64_TPREL16_LO);
15884 rel[1].r_offset = offset + d_offset;
15885 rel[1].r_addend = rel->r_addend;
15886 }
15887 }
15888 bfd_put_32 (input_bfd, insn1,
15889 contents + rel->r_offset - d_offset);
15890 if (offset != (bfd_vma) -1
15891 && offset_in_range (input_section, offset, 4))
15892 {
15893 bfd_put_32 (input_bfd, insn2, contents + offset);
15894 if (offset_in_range (input_section, offset + 4, 4))
15895 {
15896 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15897 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15898 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15899 }
15900 }
15901 if ((tls_mask & tls_gd) == 0
15902 && (tls_gd == 0 || toc_symndx != 0))
15903 {
15904 /* We changed the symbol. Start over in order
15905 to get h, sym, sec etc. right. */
15906 goto again;
15907 }
15908 }
15909 break;
15910
15911 case R_PPC64_GOT_TLSGD_PCREL34:
15912 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15913 && offset_in_range (input_section, rel->r_offset, 8))
15914 {
15915 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15916 pinsn <<= 32;
15917 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15918 if ((tls_mask & TLS_GDIE) != 0)
15919 {
15920 /* IE, pla -> pld */
15921 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
15922 r_type = R_PPC64_GOT_TPREL_PCREL34;
15923 }
15924 else
15925 {
15926 /* LE, pla pcrel -> paddi r13 */
15927 pinsn += (-1ULL << 52) + (13ULL << 16);
15928 r_type = R_PPC64_TPREL34;
15929 }
15930 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15931 bfd_put_32 (input_bfd, pinsn >> 32,
15932 contents + rel->r_offset);
15933 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15934 contents + rel->r_offset + 4);
15935 }
15936 break;
15937
15938 case R_PPC64_GOT_TLSLD_PCREL34:
15939 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15940 && offset_in_range (input_section, rel->r_offset, 8))
15941 {
15942 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15943 pinsn <<= 32;
15944 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15945 pinsn += (-1ULL << 52) + (13ULL << 16);
15946 bfd_put_32 (input_bfd, pinsn >> 32,
15947 contents + rel->r_offset);
15948 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15949 contents + rel->r_offset + 4);
15950 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15951 r_symndx = STN_UNDEF;
15952 r_type = R_PPC64_TPREL34;
15953 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15954 goto again;
15955 }
15956 break;
15957
15958 case R_PPC64_TLSGD:
15959 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15960 && rel + 1 < relend
15961 && offset_in_range (input_section, rel->r_offset,
15962 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15963 ? 8 : 4))
15964 {
15965 unsigned int insn2;
15966 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15967
15968 offset = rel->r_offset;
15969 if (is_plt_seq_reloc (r_type1))
15970 {
15971 bfd_put_32 (output_bfd, NOP, contents + offset);
15972 if (r_type1 == R_PPC64_PLT_PCREL34
15973 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15974 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15975 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15976 break;
15977 }
15978
15979 if (r_type1 == R_PPC64_PLTCALL)
15980 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15981
15982 if ((tls_mask & TLS_GDIE) != 0)
15983 {
15984 /* IE */
15985 r_type = R_PPC64_NONE;
15986 insn2 = 0x7c636a14; /* add 3,3,13 */
15987 }
15988 else
15989 {
15990 /* LE */
15991 if (toc_symndx != 0)
15992 {
15993 r_symndx = toc_symndx;
15994 rel->r_addend = toc_addend;
15995 }
15996 if (r_type1 == R_PPC64_REL24_NOTOC
15997 || r_type1 == R_PPC64_REL24_P9NOTOC
15998 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15999 {
16000 r_type = R_PPC64_NONE;
16001 insn2 = NOP;
16002 }
16003 else
16004 {
16005 rel->r_offset = offset + d_offset;
16006 r_type = R_PPC64_TPREL16_LO;
16007 insn2 = 0x38630000; /* addi 3,3,0 */
16008 }
16009 }
16010 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16011 /* Zap the reloc on the _tls_get_addr call too. */
16012 BFD_ASSERT (offset == rel[1].r_offset);
16013 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16014 bfd_put_32 (input_bfd, insn2, contents + offset);
16015 if ((tls_mask & TLS_GDIE) == 0
16016 && toc_symndx != 0
16017 && r_type != R_PPC64_NONE)
16018 goto again;
16019 }
16020 break;
16021
16022 case R_PPC64_TLSLD:
16023 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16024 && rel + 1 < relend
16025 && offset_in_range (input_section, rel->r_offset,
16026 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16027 ? 8 : 4))
16028 {
16029 unsigned int insn2;
16030 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16031
16032 offset = rel->r_offset;
16033 if (is_plt_seq_reloc (r_type1))
16034 {
16035 bfd_put_32 (output_bfd, NOP, contents + offset);
16036 if (r_type1 == R_PPC64_PLT_PCREL34
16037 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16038 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16039 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16040 break;
16041 }
16042
16043 if (r_type1 == R_PPC64_PLTCALL)
16044 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16045
16046 if (r_type1 == R_PPC64_REL24_NOTOC
16047 || r_type1 == R_PPC64_REL24_P9NOTOC
16048 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16049 {
16050 r_type = R_PPC64_NONE;
16051 insn2 = NOP;
16052 }
16053 else
16054 {
16055 rel->r_offset = offset + d_offset;
16056 r_symndx = STN_UNDEF;
16057 r_type = R_PPC64_TPREL16_LO;
16058 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16059 insn2 = 0x38630000; /* addi 3,3,0 */
16060 }
16061 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16062 /* Zap the reloc on the _tls_get_addr call too. */
16063 BFD_ASSERT (offset == rel[1].r_offset);
16064 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16065 bfd_put_32 (input_bfd, insn2, contents + offset);
16066 if (r_type != R_PPC64_NONE)
16067 goto again;
16068 }
16069 break;
16070
16071 case R_PPC64_DTPMOD64:
16072 if (rel + 1 < relend
16073 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16074 && rel[1].r_offset == rel->r_offset + 8)
16075 {
16076 if ((tls_mask & TLS_GD) == 0
16077 && offset_in_range (input_section, rel->r_offset, 8))
16078 {
16079 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16080 if ((tls_mask & TLS_GDIE) != 0)
16081 r_type = R_PPC64_TPREL64;
16082 else
16083 {
16084 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16085 r_type = R_PPC64_NONE;
16086 }
16087 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16088 }
16089 }
16090 else
16091 {
16092 if ((tls_mask & TLS_LD) == 0
16093 && offset_in_range (input_section, rel->r_offset, 8))
16094 {
16095 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16096 r_type = R_PPC64_NONE;
16097 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16098 }
16099 }
16100 break;
16101
16102 case R_PPC64_TPREL64:
16103 if ((tls_mask & TLS_TPREL) == 0)
16104 {
16105 r_type = R_PPC64_NONE;
16106 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16107 }
16108 break;
16109
16110 case R_PPC64_ENTRY:
16111 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16112 if (!bfd_link_pic (info)
16113 && !info->traditional_format
16114 && relocation + 0x80008000 <= 0xffffffff
16115 && offset_in_range (input_section, rel->r_offset, 8))
16116 {
16117 unsigned int insn1, insn2;
16118
16119 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16120 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16121 if ((insn1 & ~0xfffc) == LD_R2_0R12
16122 && insn2 == ADD_R2_R2_R12)
16123 {
16124 bfd_put_32 (input_bfd,
16125 LIS_R2 + PPC_HA (relocation),
16126 contents + rel->r_offset);
16127 bfd_put_32 (input_bfd,
16128 ADDI_R2_R2 + PPC_LO (relocation),
16129 contents + rel->r_offset + 4);
16130 }
16131 }
16132 else
16133 {
16134 relocation -= (rel->r_offset
16135 + input_section->output_offset
16136 + input_section->output_section->vma);
16137 if (relocation + 0x80008000 <= 0xffffffff
16138 && offset_in_range (input_section, rel->r_offset, 8))
16139 {
16140 unsigned int insn1, insn2;
16141
16142 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16143 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16144 if ((insn1 & ~0xfffc) == LD_R2_0R12
16145 && insn2 == ADD_R2_R2_R12)
16146 {
16147 bfd_put_32 (input_bfd,
16148 ADDIS_R2_R12 + PPC_HA (relocation),
16149 contents + rel->r_offset);
16150 bfd_put_32 (input_bfd,
16151 ADDI_R2_R2 + PPC_LO (relocation),
16152 contents + rel->r_offset + 4);
16153 }
16154 }
16155 }
16156 break;
16157
16158 case R_PPC64_REL16_HA:
16159 /* If we are generating a non-PIC executable, edit
16160 . 0: addis 2,12,.TOC.-0b@ha
16161 . addi 2,2,.TOC.-0b@l
16162 used by ELFv2 global entry points to set up r2, to
16163 . lis 2,.TOC.@ha
16164 . addi 2,2,.TOC.@l
16165 if .TOC. is in range. */
16166 if (!bfd_link_pic (info)
16167 && !info->traditional_format
16168 && !htab->opd_abi
16169 && rel->r_addend == d_offset
16170 && h != NULL && &h->elf == htab->elf.hgot
16171 && rel + 1 < relend
16172 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16173 && rel[1].r_offset == rel->r_offset + 4
16174 && rel[1].r_addend == rel->r_addend + 4
16175 && relocation + 0x80008000 <= 0xffffffff
16176 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16177 {
16178 unsigned int insn1, insn2;
16179 offset = rel->r_offset - d_offset;
16180 insn1 = bfd_get_32 (input_bfd, contents + offset);
16181 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16182 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16183 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16184 {
16185 r_type = R_PPC64_ADDR16_HA;
16186 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16187 rel->r_addend -= d_offset;
16188 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16189 rel[1].r_addend -= d_offset + 4;
16190 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16191 }
16192 }
16193 break;
16194 }
16195
16196 /* Handle other relocations that tweak non-addend part of insn. */
16197 insn = 0;
16198 max_br_offset = 1 << 25;
16199 addend = rel->r_addend;
16200 reloc_dest = DEST_NORMAL;
16201 switch (r_type)
16202 {
16203 default:
16204 break;
16205
16206 case R_PPC64_TOCSAVE:
16207 if (relocation + addend == (rel->r_offset
16208 + input_section->output_offset
16209 + input_section->output_section->vma)
16210 && tocsave_find (htab, NO_INSERT,
16211 &local_syms, rel, input_bfd)
16212 && offset_in_range (input_section, rel->r_offset, 4))
16213 {
16214 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16215 if (insn == NOP
16216 || insn == CROR_151515 || insn == CROR_313131)
16217 bfd_put_32 (input_bfd,
16218 STD_R2_0R1 + STK_TOC (htab),
16219 contents + rel->r_offset);
16220 }
16221 break;
16222
16223 /* Branch taken prediction relocations. */
16224 case R_PPC64_ADDR14_BRTAKEN:
16225 case R_PPC64_REL14_BRTAKEN:
16226 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16227 /* Fall through. */
16228
16229 /* Branch not taken prediction relocations. */
16230 case R_PPC64_ADDR14_BRNTAKEN:
16231 case R_PPC64_REL14_BRNTAKEN:
16232 if (!offset_in_range (input_section, rel->r_offset, 4))
16233 break;
16234 insn |= bfd_get_32 (input_bfd,
16235 contents + rel->r_offset) & ~(0x01 << 21);
16236 /* Fall through. */
16237
16238 case R_PPC64_REL14:
16239 max_br_offset = 1 << 15;
16240 /* Fall through. */
16241
16242 case R_PPC64_REL24:
16243 case R_PPC64_REL24_NOTOC:
16244 case R_PPC64_REL24_P9NOTOC:
16245 case R_PPC64_PLTCALL:
16246 case R_PPC64_PLTCALL_NOTOC:
16247 /* Calls to functions with a different TOC, such as calls to
16248 shared objects, need to alter the TOC pointer. This is
16249 done using a linkage stub. A REL24 branching to these
16250 linkage stubs needs to be followed by a nop, as the nop
16251 will be replaced with an instruction to restore the TOC
16252 base pointer. */
16253 fdh = h;
16254 if (h != NULL
16255 && h->oh != NULL
16256 && h->oh->is_func_descriptor)
16257 fdh = ppc_follow_link (h->oh);
16258 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16259 htab);
16260 if ((r_type == R_PPC64_PLTCALL
16261 || r_type == R_PPC64_PLTCALL_NOTOC)
16262 && stub_entry != NULL
16263 && stub_entry->type.main == ppc_stub_plt_call)
16264 stub_entry = NULL;
16265
16266 if (stub_entry != NULL
16267 && (stub_entry->type.main == ppc_stub_plt_call
16268 || stub_entry->type.r2save))
16269 {
16270 bool can_plt_call = false;
16271
16272 if (r_type == R_PPC64_REL24_NOTOC
16273 || r_type == R_PPC64_REL24_P9NOTOC)
16274 {
16275 /* NOTOC calls don't need to restore r2. */
16276 can_plt_call = true;
16277 }
16278 else if (stub_entry->type.main == ppc_stub_plt_call
16279 && !htab->opd_abi
16280 && htab->params->plt_localentry0 != 0
16281 && h != NULL
16282 && is_elfv2_localentry0 (&h->elf))
16283 {
16284 /* The function doesn't use or change r2. */
16285 can_plt_call = true;
16286 }
16287
16288 /* All of these stubs may modify r2, so there must be a
16289 branch and link followed by a nop. The nop is
16290 replaced by an insn to restore r2. */
16291 else if (offset_in_range (input_section, rel->r_offset, 8))
16292 {
16293 unsigned long br;
16294
16295 br = bfd_get_32 (input_bfd,
16296 contents + rel->r_offset);
16297 if ((br & 1) != 0)
16298 {
16299 unsigned long nop;
16300
16301 nop = bfd_get_32 (input_bfd,
16302 contents + rel->r_offset + 4);
16303 if (nop == LD_R2_0R1 + STK_TOC (htab))
16304 can_plt_call = true;
16305 else if (nop == NOP
16306 || nop == CROR_151515
16307 || nop == CROR_313131)
16308 {
16309 if (h != NULL
16310 && is_tls_get_addr (&h->elf, htab)
16311 && htab->params->tls_get_addr_opt)
16312 {
16313 /* Special stub used, leave nop alone. */
16314 }
16315 else
16316 bfd_put_32 (input_bfd,
16317 LD_R2_0R1 + STK_TOC (htab),
16318 contents + rel->r_offset + 4);
16319 can_plt_call = true;
16320 }
16321 }
16322 }
16323
16324 if (!can_plt_call && h != NULL)
16325 {
16326 const char *name = h->elf.root.root.string;
16327
16328 if (*name == '.')
16329 ++name;
16330
16331 if (startswith (name, "__libc_start_main")
16332 && (name[17] == 0 || name[17] == '@'))
16333 {
16334 /* Allow crt1 branch to go via a toc adjusting
16335 stub. Other calls that never return could do
16336 the same, if we could detect such. */
16337 can_plt_call = true;
16338 }
16339 }
16340
16341 if (!can_plt_call)
16342 {
16343 /* g++ as of 20130507 emits self-calls without a
16344 following nop. This is arguably wrong since we
16345 have conflicting information. On the one hand a
16346 global symbol and on the other a local call
16347 sequence, but don't error for this special case.
16348 It isn't possible to cheaply verify we have
16349 exactly such a call. Allow all calls to the same
16350 section. */
16351 asection *code_sec = sec;
16352
16353 if (get_opd_info (sec) != NULL)
16354 {
16355 bfd_vma off = (relocation + addend
16356 - sec->output_section->vma
16357 - sec->output_offset);
16358
16359 opd_entry_value (sec, off, &code_sec, NULL, false);
16360 }
16361 if (code_sec == input_section)
16362 can_plt_call = true;
16363 }
16364
16365 if (!can_plt_call)
16366 {
16367 if (stub_entry->type.main == ppc_stub_plt_call)
16368 info->callbacks->einfo
16369 /* xgettext:c-format */
16370 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16371 "(plt call stub)\n"),
16372 input_bfd, input_section, rel->r_offset, sym_name);
16373 else
16374 info->callbacks->einfo
16375 /* xgettext:c-format */
16376 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16377 "(toc save/adjust stub)\n"),
16378 input_bfd, input_section, rel->r_offset, sym_name);
16379
16380 bfd_set_error (bfd_error_bad_value);
16381 ret = false;
16382 }
16383
16384 if (can_plt_call
16385 && stub_entry->type.main == ppc_stub_plt_call)
16386 unresolved_reloc = false;
16387 }
16388
16389 if ((stub_entry == NULL
16390 || stub_entry->type.main == ppc_stub_long_branch
16391 || stub_entry->type.main == ppc_stub_plt_branch)
16392 && get_opd_info (sec) != NULL)
16393 {
16394 /* The branch destination is the value of the opd entry. */
16395 bfd_vma off = (relocation + addend
16396 - sec->output_section->vma
16397 - sec->output_offset);
16398 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16399 if (dest != (bfd_vma) -1)
16400 {
16401 relocation = dest;
16402 addend = 0;
16403 reloc_dest = DEST_OPD;
16404 }
16405 }
16406
16407 /* If the branch is out of reach we ought to have a long
16408 branch stub. */
16409 from = (rel->r_offset
16410 + input_section->output_offset
16411 + input_section->output_section->vma);
16412
16413 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16414 ? fdh->elf.other
16415 : sym->st_other);
16416
16417 if (stub_entry != NULL
16418 && (stub_entry->type.main == ppc_stub_long_branch
16419 || stub_entry->type.main == ppc_stub_plt_branch))
16420 {
16421 if (stub_entry->type.sub == ppc_stub_toc
16422 && !stub_entry->type.r2save
16423 && (r_type == R_PPC64_ADDR14_BRTAKEN
16424 || r_type == R_PPC64_ADDR14_BRNTAKEN
16425 || (relocation + addend - from + max_br_offset
16426 < 2 * max_br_offset)))
16427 /* Don't use the stub if this branch is in range. */
16428 stub_entry = NULL;
16429
16430 if (stub_entry != NULL
16431 && stub_entry->type.sub >= ppc_stub_notoc
16432 && ((r_type != R_PPC64_REL24_NOTOC
16433 && r_type != R_PPC64_REL24_P9NOTOC)
16434 || ((fdh ? fdh->elf.other : sym->st_other)
16435 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16436 && (relocation + addend - from + max_br_offset
16437 < 2 * max_br_offset))
16438 stub_entry = NULL;
16439
16440 if (stub_entry != NULL
16441 && stub_entry->type.r2save
16442 && (r_type == R_PPC64_REL24_NOTOC
16443 || r_type == R_PPC64_REL24_P9NOTOC)
16444 && (relocation + addend - from + max_br_offset
16445 < 2 * max_br_offset))
16446 stub_entry = NULL;
16447 }
16448
16449 if (stub_entry != NULL)
16450 {
16451 /* Munge up the value and addend so that we call the stub
16452 rather than the procedure directly. */
16453 asection *stub_sec = stub_entry->group->stub_sec;
16454
16455 if (stub_entry->type.main == ppc_stub_save_res)
16456 relocation += (stub_sec->output_offset
16457 + stub_sec->output_section->vma
16458 + stub_sec->size - htab->sfpr->size
16459 - htab->sfpr->output_offset
16460 - htab->sfpr->output_section->vma);
16461 else
16462 relocation = (stub_entry->stub_offset
16463 + stub_sec->output_offset
16464 + stub_sec->output_section->vma);
16465 addend = 0;
16466 reloc_dest = DEST_STUB;
16467
16468 if (((stub_entry->type.r2save
16469 && (r_type == R_PPC64_REL24_NOTOC
16470 || r_type == R_PPC64_REL24_P9NOTOC))
16471 || ((stub_entry->type.main == ppc_stub_plt_call
16472 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16473 && rel + 1 < relend
16474 && rel[1].r_offset == rel->r_offset + 4
16475 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16476 && !(stub_entry->type.main == ppc_stub_plt_call
16477 && htab->params->tls_get_addr_opt
16478 && h != NULL
16479 && is_tls_get_addr (&h->elf, htab)))
16480 {
16481 /* Skip over the r2 store at the start of the stub. */
16482 relocation += 4;
16483 }
16484
16485 if ((r_type == R_PPC64_REL24_NOTOC
16486 || r_type == R_PPC64_REL24_P9NOTOC)
16487 && stub_entry->type.main == ppc_stub_plt_call
16488 && stub_entry->type.sub >= ppc_stub_notoc)
16489 htab->notoc_plt = 1;
16490 }
16491
16492 if (insn != 0)
16493 {
16494 if (is_isa_v2)
16495 {
16496 /* Set 'a' bit. This is 0b00010 in BO field for branch
16497 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16498 for branch on CTR insns (BO == 1a00t or 1a01t). */
16499 if ((insn & (0x14 << 21)) == (0x04 << 21))
16500 insn |= 0x02 << 21;
16501 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16502 insn |= 0x08 << 21;
16503 else
16504 break;
16505 }
16506 else
16507 {
16508 /* Invert 'y' bit if not the default. */
16509 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16510 insn ^= 0x01 << 21;
16511 }
16512
16513 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16514 }
16515
16516 /* NOP out calls to undefined weak functions.
16517 We can thus call a weak function without first
16518 checking whether the function is defined. */
16519 else if (h != NULL
16520 && h->elf.root.type == bfd_link_hash_undefweak
16521 && h->elf.dynindx == -1
16522 && (r_type == R_PPC64_REL24
16523 || r_type == R_PPC64_REL24_NOTOC
16524 || r_type == R_PPC64_REL24_P9NOTOC)
16525 && relocation == 0
16526 && addend == 0
16527 && offset_in_range (input_section, rel->r_offset, 4))
16528 {
16529 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16530 goto copy_reloc;
16531 }
16532 break;
16533
16534 case R_PPC64_GOT16_DS:
16535 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16536 || !htab->do_toc_opt)
16537 break;
16538 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16539 if (relocation + addend - from + 0x8000 < 0x10000
16540 && sec != NULL
16541 && sec->output_section != NULL
16542 && !discarded_section (sec)
16543 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16544 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16545 {
16546 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16547 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16548 {
16549 insn += (14u << 26) - (58u << 26);
16550 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16551 r_type = R_PPC64_TOC16;
16552 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16553 }
16554 }
16555 break;
16556
16557 case R_PPC64_GOT16_LO_DS:
16558 case R_PPC64_GOT16_HA:
16559 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16560 || !htab->do_toc_opt)
16561 break;
16562 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16563 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16564 && sec != NULL
16565 && sec->output_section != NULL
16566 && !discarded_section (sec)
16567 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16568 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16569 {
16570 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16571 if (r_type == R_PPC64_GOT16_LO_DS
16572 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16573 {
16574 insn += (14u << 26) - (58u << 26);
16575 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16576 r_type = R_PPC64_TOC16_LO;
16577 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16578 }
16579 else if (r_type == R_PPC64_GOT16_HA
16580 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16581 {
16582 r_type = R_PPC64_TOC16_HA;
16583 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16584 }
16585 }
16586 break;
16587
16588 case R_PPC64_GOT_PCREL34:
16589 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16590 || !htab->do_toc_opt)
16591 break;
16592 from = (rel->r_offset
16593 + input_section->output_section->vma
16594 + input_section->output_offset);
16595 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16596 && sec != NULL
16597 && sec->output_section != NULL
16598 && !discarded_section (sec)
16599 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16600 && offset_in_range (input_section, rel->r_offset, 8)))
16601 break;
16602
16603 offset = rel->r_offset;
16604 pinsn = bfd_get_32 (input_bfd, contents + offset);
16605 pinsn <<= 32;
16606 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16607 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16608 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16609 break;
16610
16611 /* Replace with paddi. */
16612 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16613 r_type = R_PPC64_PCREL34;
16614 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16615 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16616 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16617 /* Fall through. */
16618
16619 case R_PPC64_PCREL34:
16620 if (!htab->params->no_pcrel_opt
16621 && rel + 1 < relend
16622 && rel[1].r_offset == rel->r_offset
16623 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16624 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16625 && offset_in_range (input_section, rel->r_offset, 8))
16626 {
16627 offset = rel->r_offset;
16628 pinsn = bfd_get_32 (input_bfd, contents + offset);
16629 pinsn <<= 32;
16630 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16631 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16632 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16633 | (14ULL << 26) /* paddi */))
16634 {
16635 bfd_vma off2 = rel[1].r_addend;
16636 if (off2 == 0)
16637 /* zero means next insn. */
16638 off2 = 8;
16639 off2 += offset;
16640 if (offset_in_range (input_section, off2, 4))
16641 {
16642 uint64_t pinsn2;
16643 bfd_signed_vma addend_off;
16644 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16645 pinsn2 <<= 32;
16646 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16647 {
16648 if (!offset_in_range (input_section, off2, 8))
16649 break;
16650 pinsn2 |= bfd_get_32 (input_bfd,
16651 contents + off2 + 4);
16652 }
16653 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16654 {
16655 addend += addend_off;
16656 rel->r_addend = addend;
16657 bfd_put_32 (input_bfd, pinsn >> 32,
16658 contents + offset);
16659 bfd_put_32 (input_bfd, pinsn,
16660 contents + offset + 4);
16661 bfd_put_32 (input_bfd, pinsn2 >> 32,
16662 contents + off2);
16663 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16664 bfd_put_32 (input_bfd, pinsn2,
16665 contents + off2 + 4);
16666 }
16667 }
16668 }
16669 }
16670 break;
16671 }
16672
16673 tls_type = 0;
16674 save_unresolved_reloc = unresolved_reloc;
16675 switch (r_type)
16676 {
16677 default:
16678 /* xgettext:c-format */
16679 _bfd_error_handler (_("%pB: %s unsupported"),
16680 input_bfd, ppc64_elf_howto_table[r_type]->name);
16681
16682 bfd_set_error (bfd_error_bad_value);
16683 ret = false;
16684 goto copy_reloc;
16685
16686 case R_PPC64_NONE:
16687 case R_PPC64_TLS:
16688 case R_PPC64_TLSGD:
16689 case R_PPC64_TLSLD:
16690 case R_PPC64_TOCSAVE:
16691 case R_PPC64_GNU_VTINHERIT:
16692 case R_PPC64_GNU_VTENTRY:
16693 case R_PPC64_ENTRY:
16694 case R_PPC64_PCREL_OPT:
16695 goto copy_reloc;
16696
16697 /* GOT16 relocations. Like an ADDR16 using the symbol's
16698 address in the GOT as relocation value instead of the
16699 symbol's value itself. Also, create a GOT entry for the
16700 symbol and put the symbol value there. */
16701 case R_PPC64_GOT_TLSGD16:
16702 case R_PPC64_GOT_TLSGD16_LO:
16703 case R_PPC64_GOT_TLSGD16_HI:
16704 case R_PPC64_GOT_TLSGD16_HA:
16705 case R_PPC64_GOT_TLSGD_PCREL34:
16706 tls_type = TLS_TLS | TLS_GD;
16707 goto dogot;
16708
16709 case R_PPC64_GOT_TLSLD16:
16710 case R_PPC64_GOT_TLSLD16_LO:
16711 case R_PPC64_GOT_TLSLD16_HI:
16712 case R_PPC64_GOT_TLSLD16_HA:
16713 case R_PPC64_GOT_TLSLD_PCREL34:
16714 tls_type = TLS_TLS | TLS_LD;
16715 goto dogot;
16716
16717 case R_PPC64_GOT_TPREL16_DS:
16718 case R_PPC64_GOT_TPREL16_LO_DS:
16719 case R_PPC64_GOT_TPREL16_HI:
16720 case R_PPC64_GOT_TPREL16_HA:
16721 case R_PPC64_GOT_TPREL_PCREL34:
16722 tls_type = TLS_TLS | TLS_TPREL;
16723 goto dogot;
16724
16725 case R_PPC64_GOT_DTPREL16_DS:
16726 case R_PPC64_GOT_DTPREL16_LO_DS:
16727 case R_PPC64_GOT_DTPREL16_HI:
16728 case R_PPC64_GOT_DTPREL16_HA:
16729 case R_PPC64_GOT_DTPREL_PCREL34:
16730 tls_type = TLS_TLS | TLS_DTPREL;
16731 goto dogot;
16732
16733 case R_PPC64_GOT16:
16734 case R_PPC64_GOT16_LO:
16735 case R_PPC64_GOT16_HI:
16736 case R_PPC64_GOT16_HA:
16737 case R_PPC64_GOT16_DS:
16738 case R_PPC64_GOT16_LO_DS:
16739 case R_PPC64_GOT_PCREL34:
16740 dogot:
16741 {
16742 /* Relocation is to the entry for this symbol in the global
16743 offset table. */
16744 asection *got;
16745 bfd_vma *offp;
16746 bfd_vma off;
16747 unsigned long indx = 0;
16748 struct got_entry *ent;
16749
16750 if (tls_type == (TLS_TLS | TLS_LD)
16751 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16752 ent = ppc64_tlsld_got (input_bfd);
16753 else
16754 {
16755 if (h != NULL)
16756 {
16757 if (!htab->elf.dynamic_sections_created
16758 || h->elf.dynindx == -1
16759 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16760 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16761 /* This is actually a static link, or it is a
16762 -Bsymbolic link and the symbol is defined
16763 locally, or the symbol was forced to be local
16764 because of a version file. */
16765 ;
16766 else
16767 {
16768 indx = h->elf.dynindx;
16769 unresolved_reloc = false;
16770 }
16771 ent = h->elf.got.glist;
16772 }
16773 else
16774 {
16775 if (local_got_ents == NULL)
16776 abort ();
16777 ent = local_got_ents[r_symndx];
16778 }
16779
16780 for (; ent != NULL; ent = ent->next)
16781 if (ent->addend == orig_rel.r_addend
16782 && ent->owner == input_bfd
16783 && ent->tls_type == tls_type)
16784 break;
16785 }
16786
16787 if (ent == NULL)
16788 abort ();
16789 if (ent->is_indirect)
16790 ent = ent->got.ent;
16791 offp = &ent->got.offset;
16792 got = ppc64_elf_tdata (ent->owner)->got;
16793 if (got == NULL)
16794 abort ();
16795
16796 /* The offset must always be a multiple of 8. We use the
16797 least significant bit to record whether we have already
16798 processed this entry. */
16799 off = *offp;
16800 if ((off & 1) != 0)
16801 off &= ~1;
16802 else
16803 {
16804 /* Generate relocs for the dynamic linker, except in
16805 the case of TLSLD where we'll use one entry per
16806 module. */
16807 asection *relgot;
16808 bool ifunc;
16809
16810 *offp = off | 1;
16811 relgot = NULL;
16812 ifunc = (h != NULL
16813 ? h->elf.type == STT_GNU_IFUNC
16814 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16815 if (ifunc)
16816 {
16817 relgot = htab->elf.irelplt;
16818 if (indx == 0 || is_static_defined (&h->elf))
16819 htab->elf.ifunc_resolvers = true;
16820 }
16821 else if (indx != 0
16822 || (bfd_link_pic (info)
16823 && (h == NULL
16824 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16825 && !(tls_type != 0
16826 && bfd_link_executable (info)
16827 && (h == NULL
16828 || SYMBOL_REFERENCES_LOCAL (info,
16829 &h->elf)))))
16830 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16831 if (relgot != NULL)
16832 {
16833 outrel.r_offset = (got->output_section->vma
16834 + got->output_offset
16835 + off);
16836 outrel.r_addend = orig_rel.r_addend;
16837 if (tls_type & (TLS_LD | TLS_GD))
16838 {
16839 outrel.r_addend = 0;
16840 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16841 if (tls_type == (TLS_TLS | TLS_GD))
16842 {
16843 loc = relgot->contents;
16844 loc += (relgot->reloc_count++
16845 * sizeof (Elf64_External_Rela));
16846 bfd_elf64_swap_reloca_out (output_bfd,
16847 &outrel, loc);
16848 outrel.r_offset += 8;
16849 outrel.r_addend = orig_rel.r_addend;
16850 outrel.r_info
16851 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16852 }
16853 }
16854 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16855 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16856 else if (tls_type == (TLS_TLS | TLS_TPREL))
16857 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16858 else if (indx != 0)
16859 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16860 else
16861 {
16862 if (ifunc)
16863 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16864 else
16865 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16866
16867 /* Write the .got section contents for the sake
16868 of prelink. */
16869 loc = got->contents + off;
16870 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16871 loc);
16872 }
16873
16874 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16875 {
16876 outrel.r_addend += relocation;
16877 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16878 {
16879 if (htab->elf.tls_sec == NULL)
16880 outrel.r_addend = 0;
16881 else
16882 outrel.r_addend -= htab->elf.tls_sec->vma;
16883 }
16884 }
16885 if (!(info->enable_dt_relr
16886 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
16887 {
16888 loc = relgot->contents;
16889 loc += (relgot->reloc_count++
16890 * sizeof (Elf64_External_Rela));
16891 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16892 }
16893 }
16894
16895 /* Init the .got section contents here if we're not
16896 emitting a reloc. */
16897 else
16898 {
16899 relocation += orig_rel.r_addend;
16900 if (tls_type != 0)
16901 {
16902 if (htab->elf.tls_sec == NULL)
16903 relocation = 0;
16904 else
16905 {
16906 if (tls_type & TLS_LD)
16907 relocation = 0;
16908 else
16909 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
16910 if (tls_type & TLS_TPREL)
16911 relocation += DTP_OFFSET - TP_OFFSET;
16912 }
16913
16914 if (tls_type & (TLS_GD | TLS_LD))
16915 {
16916 bfd_put_64 (output_bfd, relocation,
16917 got->contents + off + 8);
16918 relocation = 1;
16919 }
16920 }
16921 bfd_put_64 (output_bfd, relocation,
16922 got->contents + off);
16923 }
16924 }
16925
16926 if (off >= (bfd_vma) -2)
16927 abort ();
16928
16929 relocation = got->output_section->vma + got->output_offset + off;
16930 addend = 0;
16931 if (!(r_type == R_PPC64_GOT_PCREL34
16932 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16933 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16934 || r_type == R_PPC64_GOT_TPREL_PCREL34
16935 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
16936 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
16937 }
16938 break;
16939
16940 case R_PPC64_PLT16_HA:
16941 case R_PPC64_PLT16_HI:
16942 case R_PPC64_PLT16_LO:
16943 case R_PPC64_PLT16_LO_DS:
16944 case R_PPC64_PLT_PCREL34:
16945 case R_PPC64_PLT_PCREL34_NOTOC:
16946 case R_PPC64_PLT32:
16947 case R_PPC64_PLT64:
16948 case R_PPC64_PLTSEQ:
16949 case R_PPC64_PLTSEQ_NOTOC:
16950 case R_PPC64_PLTCALL:
16951 case R_PPC64_PLTCALL_NOTOC:
16952 /* Relocation is to the entry for this symbol in the
16953 procedure linkage table. */
16954 unresolved_reloc = true;
16955 {
16956 struct plt_entry **plt_list = NULL;
16957 if (h != NULL)
16958 plt_list = &h->elf.plt.plist;
16959 else if (local_got_ents != NULL)
16960 {
16961 struct plt_entry **local_plt = (struct plt_entry **)
16962 (local_got_ents + symtab_hdr->sh_info);
16963 plt_list = local_plt + r_symndx;
16964 }
16965 if (plt_list)
16966 {
16967 struct plt_entry *ent;
16968
16969 for (ent = *plt_list; ent != NULL; ent = ent->next)
16970 if (ent->plt.offset != (bfd_vma) -1
16971 && ent->addend == orig_rel.r_addend)
16972 {
16973 asection *plt;
16974 bfd_vma got;
16975
16976 plt = htab->elf.splt;
16977 if (use_local_plt (info, elf_hash_entry (h)))
16978 {
16979 if (h != NULL
16980 ? h->elf.type == STT_GNU_IFUNC
16981 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16982 plt = htab->elf.iplt;
16983 else
16984 plt = htab->pltlocal;
16985 }
16986 relocation = (plt->output_section->vma
16987 + plt->output_offset
16988 + ent->plt.offset);
16989 if (r_type == R_PPC64_PLT16_HA
16990 || r_type == R_PPC64_PLT16_HI
16991 || r_type == R_PPC64_PLT16_LO
16992 || r_type == R_PPC64_PLT16_LO_DS)
16993 {
16994 got = (elf_gp (output_bfd)
16995 + htab->sec_info[input_section->id].toc_off);
16996 relocation -= got;
16997 }
16998 addend = 0;
16999 unresolved_reloc = false;
17000 break;
17001 }
17002 }
17003 }
17004 break;
17005
17006 case R_PPC64_TOC:
17007 /* Relocation value is TOC base. */
17008 relocation = TOCstart;
17009 if (r_symndx == STN_UNDEF)
17010 relocation += htab->sec_info[input_section->id].toc_off;
17011 else if (unresolved_reloc)
17012 ;
17013 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17014 relocation += htab->sec_info[sec->id].toc_off;
17015 else
17016 unresolved_reloc = true;
17017 goto dodyn;
17018
17019 /* TOC16 relocs. We want the offset relative to the TOC base,
17020 which is the address of the start of the TOC plus 0x8000.
17021 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17022 in this order. */
17023 case R_PPC64_TOC16:
17024 case R_PPC64_TOC16_LO:
17025 case R_PPC64_TOC16_HI:
17026 case R_PPC64_TOC16_DS:
17027 case R_PPC64_TOC16_LO_DS:
17028 case R_PPC64_TOC16_HA:
17029 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17030 if (h != NULL)
17031 goto dodyn;
17032 break;
17033
17034 /* Relocate against the beginning of the section. */
17035 case R_PPC64_SECTOFF:
17036 case R_PPC64_SECTOFF_LO:
17037 case R_PPC64_SECTOFF_HI:
17038 case R_PPC64_SECTOFF_DS:
17039 case R_PPC64_SECTOFF_LO_DS:
17040 case R_PPC64_SECTOFF_HA:
17041 if (sec != NULL)
17042 addend -= sec->output_section->vma;
17043 break;
17044
17045 case R_PPC64_REL16:
17046 case R_PPC64_REL16_LO:
17047 case R_PPC64_REL16_HI:
17048 case R_PPC64_REL16_HA:
17049 case R_PPC64_REL16_HIGH:
17050 case R_PPC64_REL16_HIGHA:
17051 case R_PPC64_REL16_HIGHER:
17052 case R_PPC64_REL16_HIGHERA:
17053 case R_PPC64_REL16_HIGHEST:
17054 case R_PPC64_REL16_HIGHESTA:
17055 case R_PPC64_REL16_HIGHER34:
17056 case R_PPC64_REL16_HIGHERA34:
17057 case R_PPC64_REL16_HIGHEST34:
17058 case R_PPC64_REL16_HIGHESTA34:
17059 case R_PPC64_REL16DX_HA:
17060 case R_PPC64_REL14:
17061 case R_PPC64_REL14_BRNTAKEN:
17062 case R_PPC64_REL14_BRTAKEN:
17063 case R_PPC64_REL24:
17064 case R_PPC64_REL24_NOTOC:
17065 case R_PPC64_REL24_P9NOTOC:
17066 case R_PPC64_PCREL34:
17067 case R_PPC64_PCREL28:
17068 break;
17069
17070 case R_PPC64_TPREL16:
17071 case R_PPC64_TPREL16_LO:
17072 case R_PPC64_TPREL16_HI:
17073 case R_PPC64_TPREL16_HA:
17074 case R_PPC64_TPREL16_DS:
17075 case R_PPC64_TPREL16_LO_DS:
17076 case R_PPC64_TPREL16_HIGH:
17077 case R_PPC64_TPREL16_HIGHA:
17078 case R_PPC64_TPREL16_HIGHER:
17079 case R_PPC64_TPREL16_HIGHERA:
17080 case R_PPC64_TPREL16_HIGHEST:
17081 case R_PPC64_TPREL16_HIGHESTA:
17082 if (h != NULL
17083 && h->elf.root.type == bfd_link_hash_undefweak
17084 && h->elf.dynindx == -1
17085 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17086 {
17087 /* Make this relocation against an undefined weak symbol
17088 resolve to zero. This is really just a tweak, since
17089 code using weak externs ought to check that they are
17090 defined before using them. */
17091 bfd_byte *p = contents + rel->r_offset - d_offset;
17092
17093 insn = bfd_get_32 (input_bfd, p);
17094 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17095 if (insn != 0)
17096 bfd_put_32 (input_bfd, insn, p);
17097 break;
17098 }
17099 /* Fall through. */
17100
17101 case R_PPC64_TPREL34:
17102 if (htab->elf.tls_sec != NULL)
17103 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17104 /* The TPREL16 relocs shouldn't really be used in shared
17105 libs or with non-local symbols as that will result in
17106 DT_TEXTREL being set, but support them anyway. */
17107 goto dodyn;
17108
17109 case R_PPC64_DTPREL16:
17110 case R_PPC64_DTPREL16_LO:
17111 case R_PPC64_DTPREL16_HI:
17112 case R_PPC64_DTPREL16_HA:
17113 case R_PPC64_DTPREL16_DS:
17114 case R_PPC64_DTPREL16_LO_DS:
17115 case R_PPC64_DTPREL16_HIGH:
17116 case R_PPC64_DTPREL16_HIGHA:
17117 case R_PPC64_DTPREL16_HIGHER:
17118 case R_PPC64_DTPREL16_HIGHERA:
17119 case R_PPC64_DTPREL16_HIGHEST:
17120 case R_PPC64_DTPREL16_HIGHESTA:
17121 case R_PPC64_DTPREL34:
17122 if (htab->elf.tls_sec != NULL)
17123 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17124 break;
17125
17126 case R_PPC64_ADDR64_LOCAL:
17127 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17128 ? h->elf.other
17129 : sym->st_other);
17130 break;
17131
17132 case R_PPC64_DTPMOD64:
17133 relocation = 1;
17134 addend = 0;
17135 goto dodyn;
17136
17137 case R_PPC64_TPREL64:
17138 if (htab->elf.tls_sec != NULL)
17139 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17140 goto dodyn;
17141
17142 case R_PPC64_DTPREL64:
17143 if (htab->elf.tls_sec != NULL)
17144 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17145 /* Fall through. */
17146
17147 /* Relocations that may need to be propagated if this is a
17148 dynamic object. */
17149 case R_PPC64_REL30:
17150 case R_PPC64_REL32:
17151 case R_PPC64_REL64:
17152 case R_PPC64_ADDR14:
17153 case R_PPC64_ADDR14_BRNTAKEN:
17154 case R_PPC64_ADDR14_BRTAKEN:
17155 case R_PPC64_ADDR16:
17156 case R_PPC64_ADDR16_DS:
17157 case R_PPC64_ADDR16_HA:
17158 case R_PPC64_ADDR16_HI:
17159 case R_PPC64_ADDR16_HIGH:
17160 case R_PPC64_ADDR16_HIGHA:
17161 case R_PPC64_ADDR16_HIGHER:
17162 case R_PPC64_ADDR16_HIGHERA:
17163 case R_PPC64_ADDR16_HIGHEST:
17164 case R_PPC64_ADDR16_HIGHESTA:
17165 case R_PPC64_ADDR16_LO:
17166 case R_PPC64_ADDR16_LO_DS:
17167 case R_PPC64_ADDR16_HIGHER34:
17168 case R_PPC64_ADDR16_HIGHERA34:
17169 case R_PPC64_ADDR16_HIGHEST34:
17170 case R_PPC64_ADDR16_HIGHESTA34:
17171 case R_PPC64_ADDR24:
17172 case R_PPC64_ADDR32:
17173 case R_PPC64_ADDR64:
17174 case R_PPC64_UADDR16:
17175 case R_PPC64_UADDR32:
17176 case R_PPC64_UADDR64:
17177 case R_PPC64_D34:
17178 case R_PPC64_D34_LO:
17179 case R_PPC64_D34_HI30:
17180 case R_PPC64_D34_HA30:
17181 case R_PPC64_D28:
17182 dodyn:
17183 if ((input_section->flags & SEC_ALLOC) == 0)
17184 break;
17185
17186 if (NO_OPD_RELOCS && is_opd)
17187 break;
17188
17189 if (bfd_link_pic (info)
17190 ? ((h == NULL
17191 || h->elf.dyn_relocs != NULL)
17192 && ((h != NULL && pc_dynrelocs (h))
17193 || must_be_dyn_reloc (info, r_type)))
17194 : (h != NULL
17195 ? h->elf.dyn_relocs != NULL
17196 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17197 {
17198 bool skip, relocate;
17199 asection *sreloc;
17200 bfd_vma out_off;
17201 long indx = 0;
17202
17203 /* When generating a dynamic object, these relocations
17204 are copied into the output file to be resolved at run
17205 time. */
17206
17207 skip = false;
17208 relocate = false;
17209
17210 out_off = _bfd_elf_section_offset (output_bfd, info,
17211 input_section, rel->r_offset);
17212 if (out_off == (bfd_vma) -1)
17213 skip = true;
17214 else if (out_off == (bfd_vma) -2)
17215 skip = true, relocate = true;
17216 out_off += (input_section->output_section->vma
17217 + input_section->output_offset);
17218 outrel.r_offset = out_off;
17219 outrel.r_addend = rel->r_addend;
17220
17221 /* Optimize unaligned reloc use. */
17222 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17223 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17224 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17225 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17226 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17227 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17228 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17229 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17230 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17231
17232 if (skip)
17233 memset (&outrel, 0, sizeof outrel);
17234 else if (h != NULL
17235 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17236 && !is_opd
17237 && r_type != R_PPC64_TOC)
17238 {
17239 indx = h->elf.dynindx;
17240 BFD_ASSERT (indx != -1);
17241 outrel.r_info = ELF64_R_INFO (indx, r_type);
17242 }
17243 else
17244 {
17245 /* This symbol is local, or marked to become local,
17246 or this is an opd section reloc which must point
17247 at a local function. */
17248 outrel.r_addend += relocation;
17249 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17250 {
17251 if (is_opd && h != NULL)
17252 {
17253 /* Lie about opd entries. This case occurs
17254 when building shared libraries and we
17255 reference a function in another shared
17256 lib. The same thing happens for a weak
17257 definition in an application that's
17258 overridden by a strong definition in a
17259 shared lib. (I believe this is a generic
17260 bug in binutils handling of weak syms.)
17261 In these cases we won't use the opd
17262 entry in this lib. */
17263 unresolved_reloc = false;
17264 }
17265 if (!is_opd
17266 && r_type == R_PPC64_ADDR64
17267 && (h != NULL
17268 ? h->elf.type == STT_GNU_IFUNC
17269 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17270 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17271 else
17272 {
17273 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17274
17275 /* We need to relocate .opd contents for ld.so.
17276 Prelink also wants simple and consistent rules
17277 for relocs. This make all RELATIVE relocs have
17278 *r_offset equal to r_addend. */
17279 relocate = true;
17280 }
17281 }
17282 else
17283 {
17284 if (h != NULL
17285 ? h->elf.type == STT_GNU_IFUNC
17286 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17287 {
17288 info->callbacks->einfo
17289 /* xgettext:c-format */
17290 (_("%H: %s for indirect "
17291 "function `%pT' unsupported\n"),
17292 input_bfd, input_section, rel->r_offset,
17293 ppc64_elf_howto_table[r_type]->name,
17294 sym_name);
17295 ret = false;
17296 }
17297 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17298 ;
17299 else if (sec == NULL || sec->owner == NULL)
17300 {
17301 bfd_set_error (bfd_error_bad_value);
17302 return false;
17303 }
17304 else
17305 {
17306 asection *osec = sec->output_section;
17307
17308 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17309 {
17310 /* TLS symbol values are relative to the
17311 TLS segment. Dynamic relocations for
17312 local TLS symbols therefore can't be
17313 reduced to a relocation against their
17314 section symbol because it holds the
17315 address of the section, not a value
17316 relative to the TLS segment. We could
17317 change the .tdata dynamic section symbol
17318 to be zero value but STN_UNDEF works
17319 and is used elsewhere, eg. for TPREL64
17320 GOT relocs against local TLS symbols. */
17321 osec = htab->elf.tls_sec;
17322 indx = 0;
17323 }
17324 else
17325 {
17326 indx = elf_section_data (osec)->dynindx;
17327 if (indx == 0)
17328 {
17329 if ((osec->flags & SEC_READONLY) == 0
17330 && htab->elf.data_index_section != NULL)
17331 osec = htab->elf.data_index_section;
17332 else
17333 osec = htab->elf.text_index_section;
17334 indx = elf_section_data (osec)->dynindx;
17335 }
17336 BFD_ASSERT (indx != 0);
17337 }
17338
17339 /* We are turning this relocation into one
17340 against a section symbol, so subtract out
17341 the output section's address but not the
17342 offset of the input section in the output
17343 section. */
17344 outrel.r_addend -= osec->vma;
17345 }
17346
17347 outrel.r_info = ELF64_R_INFO (indx, r_type);
17348 }
17349 }
17350
17351 if (!(info->enable_dt_relr
17352 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17353 && rel->r_offset % 2 == 0
17354 && input_section->alignment_power != 0
17355 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
17356 {
17357 sreloc = elf_section_data (input_section)->sreloc;
17358 if (h != NULL
17359 ? h->elf.type == STT_GNU_IFUNC
17360 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17361 {
17362 sreloc = htab->elf.irelplt;
17363 if (indx == 0 || is_static_defined (&h->elf))
17364 htab->elf.ifunc_resolvers = true;
17365 }
17366 if (sreloc == NULL)
17367 abort ();
17368
17369 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17370 >= sreloc->size)
17371 abort ();
17372 loc = sreloc->contents;
17373 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17374 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17375 }
17376
17377 if (!warned_dynamic
17378 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17379 {
17380 info->callbacks->einfo
17381 /* xgettext:c-format */
17382 (_("%X%P: %pB: %s against %pT "
17383 "is not supported by glibc as a dynamic relocation\n"),
17384 input_bfd,
17385 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17386 sym_name);
17387 warned_dynamic = true;
17388 }
17389
17390 /* If this reloc is against an external symbol, it will
17391 be computed at runtime, so there's no need to do
17392 anything now. However, for the sake of prelink ensure
17393 that the section contents are a known value. */
17394 if (!relocate)
17395 {
17396 unresolved_reloc = false;
17397 /* The value chosen here is quite arbitrary as ld.so
17398 ignores section contents except for the special
17399 case of .opd where the contents might be accessed
17400 before relocation. Choose zero, as that won't
17401 cause reloc overflow. */
17402 relocation = 0;
17403 addend = 0;
17404 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17405 to improve backward compatibility with older
17406 versions of ld. */
17407 if (r_type == R_PPC64_ADDR64)
17408 addend = outrel.r_addend;
17409 /* Adjust pc_relative relocs to have zero in *r_offset. */
17410 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17411 addend = outrel.r_offset;
17412 }
17413 }
17414 break;
17415
17416 case R_PPC64_COPY:
17417 case R_PPC64_GLOB_DAT:
17418 case R_PPC64_JMP_SLOT:
17419 case R_PPC64_JMP_IREL:
17420 case R_PPC64_RELATIVE:
17421 /* We shouldn't ever see these dynamic relocs in relocatable
17422 files. */
17423 /* Fall through. */
17424
17425 case R_PPC64_PLTGOT16:
17426 case R_PPC64_PLTGOT16_DS:
17427 case R_PPC64_PLTGOT16_HA:
17428 case R_PPC64_PLTGOT16_HI:
17429 case R_PPC64_PLTGOT16_LO:
17430 case R_PPC64_PLTGOT16_LO_DS:
17431 case R_PPC64_PLTREL32:
17432 case R_PPC64_PLTREL64:
17433 /* These ones haven't been implemented yet. */
17434
17435 info->callbacks->einfo
17436 /* xgettext:c-format */
17437 (_("%P: %pB: %s is not supported for `%pT'\n"),
17438 input_bfd,
17439 ppc64_elf_howto_table[r_type]->name, sym_name);
17440
17441 bfd_set_error (bfd_error_invalid_operation);
17442 ret = false;
17443 goto copy_reloc;
17444 }
17445
17446 /* Multi-instruction sequences that access the TOC can be
17447 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17448 to nop; addi rb,r2,x; */
17449 switch (r_type)
17450 {
17451 default:
17452 break;
17453
17454 case R_PPC64_GOT_TLSLD16_HI:
17455 case R_PPC64_GOT_TLSGD16_HI:
17456 case R_PPC64_GOT_TPREL16_HI:
17457 case R_PPC64_GOT_DTPREL16_HI:
17458 case R_PPC64_GOT16_HI:
17459 case R_PPC64_TOC16_HI:
17460 /* These relocs would only be useful if building up an
17461 offset to later add to r2, perhaps in an indexed
17462 addressing mode instruction. Don't try to optimize.
17463 Unfortunately, the possibility of someone building up an
17464 offset like this or even with the HA relocs, means that
17465 we need to check the high insn when optimizing the low
17466 insn. */
17467 break;
17468
17469 case R_PPC64_PLTCALL_NOTOC:
17470 if (!unresolved_reloc)
17471 htab->notoc_plt = 1;
17472 /* Fall through. */
17473 case R_PPC64_PLTCALL:
17474 if (unresolved_reloc
17475 && offset_in_range (input_section, rel->r_offset,
17476 r_type == R_PPC64_PLTCALL ? 8 : 4))
17477 {
17478 /* No plt entry. Make this into a direct call. */
17479 bfd_byte *p = contents + rel->r_offset;
17480 insn = bfd_get_32 (input_bfd, p);
17481 insn &= 1;
17482 bfd_put_32 (input_bfd, B_DOT | insn, p);
17483 if (r_type == R_PPC64_PLTCALL)
17484 bfd_put_32 (input_bfd, NOP, p + 4);
17485 unresolved_reloc = save_unresolved_reloc;
17486 r_type = R_PPC64_REL24;
17487 }
17488 break;
17489
17490 case R_PPC64_PLTSEQ_NOTOC:
17491 case R_PPC64_PLTSEQ:
17492 if (unresolved_reloc)
17493 {
17494 unresolved_reloc = false;
17495 goto nop_it;
17496 }
17497 break;
17498
17499 case R_PPC64_PLT_PCREL34_NOTOC:
17500 if (!unresolved_reloc)
17501 htab->notoc_plt = 1;
17502 /* Fall through. */
17503 case R_PPC64_PLT_PCREL34:
17504 if (unresolved_reloc
17505 && offset_in_range (input_section, rel->r_offset, 8))
17506 {
17507 bfd_byte *p = contents + rel->r_offset;
17508 bfd_put_32 (input_bfd, PNOP >> 32, p);
17509 bfd_put_32 (input_bfd, PNOP, p + 4);
17510 unresolved_reloc = false;
17511 goto copy_reloc;
17512 }
17513 break;
17514
17515 case R_PPC64_PLT16_HA:
17516 if (unresolved_reloc)
17517 {
17518 unresolved_reloc = false;
17519 goto nop_it;
17520 }
17521 /* Fall through. */
17522 case R_PPC64_GOT_TLSLD16_HA:
17523 case R_PPC64_GOT_TLSGD16_HA:
17524 case R_PPC64_GOT_TPREL16_HA:
17525 case R_PPC64_GOT_DTPREL16_HA:
17526 case R_PPC64_GOT16_HA:
17527 case R_PPC64_TOC16_HA:
17528 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17529 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
17530 {
17531 bfd_byte *p;
17532 nop_it:
17533 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17534 {
17535 p = contents + (rel->r_offset & ~3);
17536 bfd_put_32 (input_bfd, NOP, p);
17537 goto copy_reloc;
17538 }
17539 }
17540 break;
17541
17542 case R_PPC64_PLT16_LO:
17543 case R_PPC64_PLT16_LO_DS:
17544 if (unresolved_reloc)
17545 {
17546 unresolved_reloc = false;
17547 goto nop_it;
17548 }
17549 /* Fall through. */
17550 case R_PPC64_GOT_TLSLD16_LO:
17551 case R_PPC64_GOT_TLSGD16_LO:
17552 case R_PPC64_GOT_TPREL16_LO_DS:
17553 case R_PPC64_GOT_DTPREL16_LO_DS:
17554 case R_PPC64_GOT16_LO:
17555 case R_PPC64_GOT16_LO_DS:
17556 case R_PPC64_TOC16_LO:
17557 case R_PPC64_TOC16_LO_DS:
17558 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17559 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17560 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17561 {
17562 bfd_byte *p = contents + (rel->r_offset & ~3);
17563 insn = bfd_get_32 (input_bfd, p);
17564 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17565 {
17566 /* Transform addic to addi when we change reg. */
17567 insn &= ~((0x3fu << 26) | (0x1f << 16));
17568 insn |= (14u << 26) | (2 << 16);
17569 }
17570 else
17571 {
17572 insn &= ~(0x1f << 16);
17573 insn |= 2 << 16;
17574 }
17575 bfd_put_32 (input_bfd, insn, p);
17576 }
17577 break;
17578
17579 case R_PPC64_TPREL16_HA:
17580 if (htab->do_tls_opt
17581 && relocation + addend + 0x8000 < 0x10000
17582 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17583 {
17584 bfd_byte *p = contents + (rel->r_offset & ~3);
17585 bfd_put_32 (input_bfd, NOP, p);
17586 goto copy_reloc;
17587 }
17588 break;
17589
17590 case R_PPC64_TPREL16_LO:
17591 case R_PPC64_TPREL16_LO_DS:
17592 if (htab->do_tls_opt
17593 && relocation + addend + 0x8000 < 0x10000
17594 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17595 {
17596 bfd_byte *p = contents + (rel->r_offset & ~3);
17597 insn = bfd_get_32 (input_bfd, p);
17598 insn &= ~(0x1f << 16);
17599 insn |= 13 << 16;
17600 bfd_put_32 (input_bfd, insn, p);
17601 }
17602 break;
17603 }
17604
17605 /* Do any further special processing. */
17606 switch (r_type)
17607 {
17608 default:
17609 break;
17610
17611 case R_PPC64_REL16_HA:
17612 case R_PPC64_REL16_HIGHA:
17613 case R_PPC64_REL16_HIGHERA:
17614 case R_PPC64_REL16_HIGHESTA:
17615 case R_PPC64_REL16DX_HA:
17616 case R_PPC64_ADDR16_HA:
17617 case R_PPC64_ADDR16_HIGHA:
17618 case R_PPC64_ADDR16_HIGHERA:
17619 case R_PPC64_ADDR16_HIGHESTA:
17620 case R_PPC64_TOC16_HA:
17621 case R_PPC64_SECTOFF_HA:
17622 case R_PPC64_TPREL16_HA:
17623 case R_PPC64_TPREL16_HIGHA:
17624 case R_PPC64_TPREL16_HIGHERA:
17625 case R_PPC64_TPREL16_HIGHESTA:
17626 case R_PPC64_DTPREL16_HA:
17627 case R_PPC64_DTPREL16_HIGHA:
17628 case R_PPC64_DTPREL16_HIGHERA:
17629 case R_PPC64_DTPREL16_HIGHESTA:
17630 /* It's just possible that this symbol is a weak symbol
17631 that's not actually defined anywhere. In that case,
17632 'sec' would be NULL, and we should leave the symbol
17633 alone (it will be set to zero elsewhere in the link). */
17634 if (sec == NULL)
17635 break;
17636 /* Fall through. */
17637
17638 case R_PPC64_GOT16_HA:
17639 case R_PPC64_PLTGOT16_HA:
17640 case R_PPC64_PLT16_HA:
17641 case R_PPC64_GOT_TLSGD16_HA:
17642 case R_PPC64_GOT_TLSLD16_HA:
17643 case R_PPC64_GOT_TPREL16_HA:
17644 case R_PPC64_GOT_DTPREL16_HA:
17645 /* Add 0x10000 if sign bit in 0:15 is set.
17646 Bits 0:15 are not used. */
17647 addend += 0x8000;
17648 break;
17649
17650 case R_PPC64_D34_HA30:
17651 case R_PPC64_ADDR16_HIGHERA34:
17652 case R_PPC64_ADDR16_HIGHESTA34:
17653 case R_PPC64_REL16_HIGHERA34:
17654 case R_PPC64_REL16_HIGHESTA34:
17655 if (sec != NULL)
17656 addend += 1ULL << 33;
17657 break;
17658
17659 case R_PPC64_ADDR16_DS:
17660 case R_PPC64_ADDR16_LO_DS:
17661 case R_PPC64_GOT16_DS:
17662 case R_PPC64_GOT16_LO_DS:
17663 case R_PPC64_PLT16_LO_DS:
17664 case R_PPC64_SECTOFF_DS:
17665 case R_PPC64_SECTOFF_LO_DS:
17666 case R_PPC64_TOC16_DS:
17667 case R_PPC64_TOC16_LO_DS:
17668 case R_PPC64_PLTGOT16_DS:
17669 case R_PPC64_PLTGOT16_LO_DS:
17670 case R_PPC64_GOT_TPREL16_DS:
17671 case R_PPC64_GOT_TPREL16_LO_DS:
17672 case R_PPC64_GOT_DTPREL16_DS:
17673 case R_PPC64_GOT_DTPREL16_LO_DS:
17674 case R_PPC64_TPREL16_DS:
17675 case R_PPC64_TPREL16_LO_DS:
17676 case R_PPC64_DTPREL16_DS:
17677 case R_PPC64_DTPREL16_LO_DS:
17678 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17679 break;
17680 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17681 mask = 3;
17682 /* If this reloc is against an lq, lxv, or stxv insn, then
17683 the value must be a multiple of 16. This is somewhat of
17684 a hack, but the "correct" way to do this by defining _DQ
17685 forms of all the _DS relocs bloats all reloc switches in
17686 this file. It doesn't make much sense to use these
17687 relocs in data, so testing the insn should be safe. */
17688 if ((insn & (0x3fu << 26)) == (56u << 26)
17689 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17690 mask = 15;
17691 relocation += addend;
17692 addend = insn & (mask ^ 3);
17693 if ((relocation & mask) != 0)
17694 {
17695 relocation ^= relocation & mask;
17696 info->callbacks->einfo
17697 /* xgettext:c-format */
17698 (_("%H: error: %s not a multiple of %u\n"),
17699 input_bfd, input_section, rel->r_offset,
17700 ppc64_elf_howto_table[r_type]->name,
17701 mask + 1);
17702 bfd_set_error (bfd_error_bad_value);
17703 ret = false;
17704 goto copy_reloc;
17705 }
17706 break;
17707 }
17708
17709 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17710 because such sections are not SEC_ALLOC and thus ld.so will
17711 not process them. */
17712 howto = ppc64_elf_howto_table[(int) r_type];
17713 if (unresolved_reloc
17714 && !((input_section->flags & SEC_DEBUGGING) != 0
17715 && h->elf.def_dynamic)
17716 && _bfd_elf_section_offset (output_bfd, info, input_section,
17717 rel->r_offset) != (bfd_vma) -1)
17718 {
17719 info->callbacks->einfo
17720 /* xgettext:c-format */
17721 (_("%H: unresolvable %s against `%pT'\n"),
17722 input_bfd, input_section, rel->r_offset,
17723 howto->name,
17724 h->elf.root.root.string);
17725 ret = false;
17726 }
17727
17728 /* 16-bit fields in insns mostly have signed values, but a
17729 few insns have 16-bit unsigned values. Really, we should
17730 have different reloc types. */
17731 if (howto->complain_on_overflow != complain_overflow_dont
17732 && howto->dst_mask == 0xffff
17733 && (input_section->flags & SEC_CODE) != 0
17734 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17735 {
17736 enum complain_overflow complain = complain_overflow_signed;
17737
17738 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17739 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17740 complain = complain_overflow_bitfield;
17741 else if (howto->rightshift == 0
17742 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17743 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17744 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17745 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17746 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17747 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17748 complain = complain_overflow_unsigned;
17749 if (howto->complain_on_overflow != complain)
17750 {
17751 alt_howto = *howto;
17752 alt_howto.complain_on_overflow = complain;
17753 howto = &alt_howto;
17754 }
17755 }
17756
17757 switch (r_type)
17758 {
17759 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17760 case R_PPC64_D34:
17761 case R_PPC64_D34_LO:
17762 case R_PPC64_D34_HI30:
17763 case R_PPC64_D34_HA30:
17764 case R_PPC64_PCREL34:
17765 case R_PPC64_GOT_PCREL34:
17766 case R_PPC64_TPREL34:
17767 case R_PPC64_DTPREL34:
17768 case R_PPC64_GOT_TLSGD_PCREL34:
17769 case R_PPC64_GOT_TLSLD_PCREL34:
17770 case R_PPC64_GOT_TPREL_PCREL34:
17771 case R_PPC64_GOT_DTPREL_PCREL34:
17772 case R_PPC64_PLT_PCREL34:
17773 case R_PPC64_PLT_PCREL34_NOTOC:
17774 case R_PPC64_D28:
17775 case R_PPC64_PCREL28:
17776 if (!offset_in_range (input_section, rel->r_offset, 8))
17777 r = bfd_reloc_outofrange;
17778 else
17779 {
17780 relocation += addend;
17781 if (howto->pc_relative)
17782 relocation -= (rel->r_offset
17783 + input_section->output_offset
17784 + input_section->output_section->vma);
17785 relocation >>= howto->rightshift;
17786
17787 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17788 pinsn <<= 32;
17789 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17790
17791 pinsn &= ~howto->dst_mask;
17792 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17793 & howto->dst_mask);
17794 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17795 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17796 r = bfd_reloc_ok;
17797 if (howto->complain_on_overflow == complain_overflow_signed
17798 && (relocation + (1ULL << (howto->bitsize - 1))
17799 >= 1ULL << howto->bitsize))
17800 r = bfd_reloc_overflow;
17801 }
17802 break;
17803
17804 case R_PPC64_REL16DX_HA:
17805 if (!offset_in_range (input_section, rel->r_offset, 4))
17806 r = bfd_reloc_outofrange;
17807 else
17808 {
17809 relocation += addend;
17810 relocation -= (rel->r_offset
17811 + input_section->output_offset
17812 + input_section->output_section->vma);
17813 relocation = (bfd_signed_vma) relocation >> 16;
17814 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17815 insn &= ~0x1fffc1;
17816 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17817 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17818 r = bfd_reloc_ok;
17819 if (relocation + 0x8000 > 0xffff)
17820 r = bfd_reloc_overflow;
17821 }
17822 break;
17823
17824 default:
17825 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17826 contents, rel->r_offset,
17827 relocation, addend);
17828 }
17829
17830 if (r != bfd_reloc_ok)
17831 {
17832 char *more_info = NULL;
17833 const char *reloc_name = howto->name;
17834
17835 if (reloc_dest != DEST_NORMAL)
17836 {
17837 more_info = bfd_malloc (strlen (reloc_name) + 8);
17838 if (more_info != NULL)
17839 {
17840 strcpy (more_info, reloc_name);
17841 strcat (more_info, (reloc_dest == DEST_OPD
17842 ? " (OPD)" : " (stub)"));
17843 reloc_name = more_info;
17844 }
17845 }
17846
17847 if (r == bfd_reloc_overflow)
17848 {
17849 /* On code like "if (foo) foo();" don't report overflow
17850 on a branch to zero when foo is undefined. */
17851 if (!warned
17852 && (reloc_dest == DEST_STUB
17853 || !(h != NULL
17854 && (h->elf.root.type == bfd_link_hash_undefweak
17855 || h->elf.root.type == bfd_link_hash_undefined)
17856 && is_branch_reloc (r_type))))
17857 info->callbacks->reloc_overflow
17858 (info, (struct bfd_link_hash_entry *) h, sym_name,
17859 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17860 rel->r_offset);
17861 }
17862 else
17863 {
17864 info->callbacks->einfo
17865 /* xgettext:c-format */
17866 (_("%H: %s against `%pT': error %d\n"),
17867 input_bfd, input_section, rel->r_offset,
17868 reloc_name, sym_name, (int) r);
17869 ret = false;
17870 }
17871 free (more_info);
17872 }
17873 copy_reloc:
17874 if (wrel != rel)
17875 *wrel = *rel;
17876 }
17877
17878 if (wrel != rel)
17879 {
17880 Elf_Internal_Shdr *rel_hdr;
17881 size_t deleted = rel - wrel;
17882
17883 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17884 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17885 if (rel_hdr->sh_size == 0)
17886 {
17887 /* It is too late to remove an empty reloc section. Leave
17888 one NONE reloc.
17889 ??? What is wrong with an empty section??? */
17890 rel_hdr->sh_size = rel_hdr->sh_entsize;
17891 deleted -= 1;
17892 }
17893 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17894 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17895 input_section->reloc_count -= deleted;
17896 }
17897
17898 /* If we're emitting relocations, then shortly after this function
17899 returns, reloc offsets and addends for this section will be
17900 adjusted. Worse, reloc symbol indices will be for the output
17901 file rather than the input. Save a copy of the relocs for
17902 opd_entry_value. */
17903 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
17904 {
17905 bfd_size_type amt;
17906 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17907 rel = bfd_alloc (input_bfd, amt);
17908 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17909 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
17910 if (rel == NULL)
17911 return false;
17912 memcpy (rel, relocs, amt);
17913 }
17914 return ret;
17915 }
17916
17917 /* Adjust the value of any local symbols in opd sections. */
17918
17919 static int
17920 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17921 const char *name ATTRIBUTE_UNUSED,
17922 Elf_Internal_Sym *elfsym,
17923 asection *input_sec,
17924 struct elf_link_hash_entry *h)
17925 {
17926 struct _opd_sec_data *opd;
17927 long adjust;
17928 bfd_vma value;
17929
17930 if (h != NULL)
17931 return 1;
17932
17933 opd = get_opd_info (input_sec);
17934 if (opd == NULL || opd->adjust == NULL)
17935 return 1;
17936
17937 value = elfsym->st_value - input_sec->output_offset;
17938 if (!bfd_link_relocatable (info))
17939 value -= input_sec->output_section->vma;
17940
17941 adjust = opd->adjust[OPD_NDX (value)];
17942 if (adjust == -1)
17943 return 2;
17944
17945 elfsym->st_value += adjust;
17946 return 1;
17947 }
17948
17949 /* Finish up dynamic symbol handling. We set the contents of various
17950 dynamic sections here. */
17951
17952 static bool
17953 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17954 struct bfd_link_info *info,
17955 struct elf_link_hash_entry *h,
17956 Elf_Internal_Sym *sym)
17957 {
17958 struct ppc_link_hash_table *htab;
17959 struct plt_entry *ent;
17960
17961 htab = ppc_hash_table (info);
17962 if (htab == NULL)
17963 return false;
17964
17965 if (!htab->opd_abi && !h->def_regular)
17966 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17967 if (ent->plt.offset != (bfd_vma) -1)
17968 {
17969 /* Mark the symbol as undefined, rather than as
17970 defined in glink. Leave the value if there were
17971 any relocations where pointer equality matters
17972 (this is a clue for the dynamic linker, to make
17973 function pointer comparisons work between an
17974 application and shared library), otherwise set it
17975 to zero. */
17976 sym->st_shndx = SHN_UNDEF;
17977 if (!h->pointer_equality_needed)
17978 sym->st_value = 0;
17979 else if (!h->ref_regular_nonweak)
17980 {
17981 /* This breaks function pointer comparisons, but
17982 that is better than breaking tests for a NULL
17983 function pointer. */
17984 sym->st_value = 0;
17985 }
17986 break;
17987 }
17988
17989 if (h->needs_copy
17990 && (h->root.type == bfd_link_hash_defined
17991 || h->root.type == bfd_link_hash_defweak)
17992 && (h->root.u.def.section == htab->elf.sdynbss
17993 || h->root.u.def.section == htab->elf.sdynrelro))
17994 {
17995 /* This symbol needs a copy reloc. Set it up. */
17996 Elf_Internal_Rela rela;
17997 asection *srel;
17998 bfd_byte *loc;
17999
18000 if (h->dynindx == -1)
18001 abort ();
18002
18003 rela.r_offset = defined_sym_val (h);
18004 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18005 rela.r_addend = 0;
18006 if (h->root.u.def.section == htab->elf.sdynrelro)
18007 srel = htab->elf.sreldynrelro;
18008 else
18009 srel = htab->elf.srelbss;
18010 loc = srel->contents;
18011 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
18012 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
18013 }
18014
18015 return true;
18016 }
18017
18018 /* Used to decide how to sort relocs in an optimal manner for the
18019 dynamic linker, before writing them out. */
18020
18021 static enum elf_reloc_type_class
18022 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18023 const asection *rel_sec,
18024 const Elf_Internal_Rela *rela)
18025 {
18026 enum elf_ppc64_reloc_type r_type;
18027 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18028
18029 if (rel_sec == htab->elf.irelplt)
18030 return reloc_class_ifunc;
18031
18032 r_type = ELF64_R_TYPE (rela->r_info);
18033 switch (r_type)
18034 {
18035 case R_PPC64_RELATIVE:
18036 return reloc_class_relative;
18037 case R_PPC64_JMP_SLOT:
18038 return reloc_class_plt;
18039 case R_PPC64_COPY:
18040 return reloc_class_copy;
18041 default:
18042 return reloc_class_normal;
18043 }
18044 }
18045
18046 /* Finish up the dynamic sections. */
18047
18048 static bool
18049 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18050 struct bfd_link_info *info)
18051 {
18052 struct ppc_link_hash_table *htab;
18053 bfd *dynobj;
18054 asection *sdyn;
18055
18056 htab = ppc_hash_table (info);
18057 if (htab == NULL)
18058 return false;
18059
18060 dynobj = htab->elf.dynobj;
18061 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18062
18063 if (htab->elf.dynamic_sections_created)
18064 {
18065 Elf64_External_Dyn *dyncon, *dynconend;
18066
18067 if (sdyn == NULL || htab->elf.sgot == NULL)
18068 abort ();
18069
18070 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18071 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18072 for (; dyncon < dynconend; dyncon++)
18073 {
18074 Elf_Internal_Dyn dyn;
18075 asection *s;
18076
18077 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18078
18079 switch (dyn.d_tag)
18080 {
18081 default:
18082 continue;
18083
18084 case DT_PPC64_GLINK:
18085 s = htab->glink;
18086 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18087 /* We stupidly defined DT_PPC64_GLINK to be the start
18088 of glink rather than the first entry point, which is
18089 what ld.so needs, and now have a bigger stub to
18090 support automatic multiple TOCs. */
18091 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18092 break;
18093
18094 case DT_PPC64_OPD:
18095 s = bfd_get_section_by_name (output_bfd, ".opd");
18096 if (s == NULL)
18097 continue;
18098 dyn.d_un.d_ptr = s->vma;
18099 break;
18100
18101 case DT_PPC64_OPT:
18102 if ((htab->do_multi_toc && htab->multi_toc_needed)
18103 || htab->notoc_plt)
18104 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18105 if (htab->has_plt_localentry0)
18106 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18107 break;
18108
18109 case DT_PPC64_OPDSZ:
18110 s = bfd_get_section_by_name (output_bfd, ".opd");
18111 if (s == NULL)
18112 continue;
18113 dyn.d_un.d_val = s->size;
18114 break;
18115
18116 case DT_PLTGOT:
18117 s = htab->elf.splt;
18118 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18119 break;
18120
18121 case DT_JMPREL:
18122 s = htab->elf.srelplt;
18123 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18124 break;
18125
18126 case DT_PLTRELSZ:
18127 dyn.d_un.d_val = htab->elf.srelplt->size;
18128 break;
18129
18130 case DT_TEXTREL:
18131 if (htab->elf.ifunc_resolvers)
18132 info->callbacks->einfo
18133 (_("%P: warning: text relocations and GNU indirect "
18134 "functions may result in a segfault at runtime\n"));
18135 continue;
18136 }
18137
18138 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18139 }
18140 }
18141
18142 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18143 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18144 {
18145 /* Fill in the first entry in the global offset table.
18146 We use it to hold the link-time TOCbase. */
18147 bfd_put_64 (output_bfd,
18148 elf_gp (output_bfd) + TOC_BASE_OFF,
18149 htab->elf.sgot->contents);
18150
18151 /* Set .got entry size. */
18152 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18153 = 8;
18154 }
18155
18156 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18157 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18158 {
18159 /* Set .plt entry size. */
18160 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18161 = PLT_ENTRY_SIZE (htab);
18162 }
18163
18164 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18165 brlt ourselves if emitrelocations. */
18166 if (htab->brlt != NULL
18167 && htab->brlt->reloc_count != 0
18168 && !_bfd_elf_link_output_relocs (output_bfd,
18169 htab->brlt,
18170 elf_section_data (htab->brlt)->rela.hdr,
18171 elf_section_data (htab->brlt)->relocs,
18172 NULL))
18173 return false;
18174
18175 if (htab->glink != NULL
18176 && htab->glink->reloc_count != 0
18177 && !_bfd_elf_link_output_relocs (output_bfd,
18178 htab->glink,
18179 elf_section_data (htab->glink)->rela.hdr,
18180 elf_section_data (htab->glink)->relocs,
18181 NULL))
18182 return false;
18183
18184
18185 if (htab->glink_eh_frame != NULL
18186 && htab->glink_eh_frame->size != 0
18187 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18188 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18189 htab->glink_eh_frame,
18190 htab->glink_eh_frame->contents))
18191 return false;
18192
18193 /* We need to handle writing out multiple GOT sections ourselves,
18194 since we didn't add them to DYNOBJ. We know dynobj is the first
18195 bfd. */
18196 while ((dynobj = dynobj->link.next) != NULL)
18197 {
18198 asection *s;
18199
18200 if (!is_ppc64_elf (dynobj))
18201 continue;
18202
18203 s = ppc64_elf_tdata (dynobj)->got;
18204 if (s != NULL
18205 && s->size != 0
18206 && s->output_section != bfd_abs_section_ptr
18207 && !bfd_set_section_contents (output_bfd, s->output_section,
18208 s->contents, s->output_offset,
18209 s->size))
18210 return false;
18211 s = ppc64_elf_tdata (dynobj)->relgot;
18212 if (s != NULL
18213 && s->size != 0
18214 && s->output_section != bfd_abs_section_ptr
18215 && !bfd_set_section_contents (output_bfd, s->output_section,
18216 s->contents, s->output_offset,
18217 s->size))
18218 return false;
18219 }
18220
18221 return true;
18222 }
18223
18224 #include "elf64-target.h"
18225
18226 /* FreeBSD support */
18227
18228 #undef TARGET_LITTLE_SYM
18229 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18230 #undef TARGET_LITTLE_NAME
18231 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18232
18233 #undef TARGET_BIG_SYM
18234 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18235 #undef TARGET_BIG_NAME
18236 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18237
18238 #undef ELF_OSABI
18239 #define ELF_OSABI ELFOSABI_FREEBSD
18240
18241 #undef elf64_bed
18242 #define elf64_bed elf64_powerpc_fbsd_bed
18243
18244 #include "elf64-target.h"