Simplify Ada catchpoints
[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 && ((!NO_OPD_RELOCS && is_opd)
5394 || (!ifunc && SYMBOL_REFERENCES_LOCAL (info, h))))
5395 p->rel_count += 1;
5396 }
5397 else
5398 {
5399 /* Track dynamic relocs needed for local syms too.
5400 We really need local syms available to do this
5401 easily. Oh well. */
5402 struct ppc_local_dyn_relocs *p;
5403 struct ppc_local_dyn_relocs **head;
5404 bool is_ifunc;
5405 asection *s;
5406 void *vpp;
5407 Elf_Internal_Sym *isym;
5408
5409 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5410 abfd, r_symndx);
5411 if (isym == NULL)
5412 return false;
5413
5414 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5415 if (s == NULL)
5416 s = sec;
5417
5418 vpp = &elf_section_data (s)->local_dynrel;
5419 head = (struct ppc_local_dyn_relocs **) vpp;
5420 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5421 p = *head;
5422 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5423 p = p->next;
5424 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5425 {
5426 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5427 if (p == NULL)
5428 return false;
5429 p->next = *head;
5430 *head = p;
5431 p->sec = sec;
5432 p->count = 0;
5433 p->rel_count = 0;
5434 p->ifunc = is_ifunc;
5435 }
5436 p->count += 1;
5437 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5438 && rel->r_offset % 2 == 0
5439 && sec->alignment_power != 0
5440 && ((!NO_OPD_RELOCS && is_opd) || !is_ifunc))
5441 p->rel_count += 1;
5442 }
5443 }
5444 break;
5445
5446 default:
5447 break;
5448 }
5449 }
5450
5451 return true;
5452 }
5453
5454 /* Merge backend specific data from an object file to the output
5455 object file when linking. */
5456
5457 static bool
5458 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5459 {
5460 bfd *obfd = info->output_bfd;
5461 unsigned long iflags, oflags;
5462
5463 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5464 return true;
5465
5466 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5467 return true;
5468
5469 if (!_bfd_generic_verify_endian_match (ibfd, info))
5470 return false;
5471
5472 iflags = elf_elfheader (ibfd)->e_flags;
5473 oflags = elf_elfheader (obfd)->e_flags;
5474
5475 if (iflags & ~EF_PPC64_ABI)
5476 {
5477 _bfd_error_handler
5478 /* xgettext:c-format */
5479 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5480 bfd_set_error (bfd_error_bad_value);
5481 return false;
5482 }
5483 else if (iflags != oflags && iflags != 0)
5484 {
5485 _bfd_error_handler
5486 /* xgettext:c-format */
5487 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5488 ibfd, iflags, oflags);
5489 bfd_set_error (bfd_error_bad_value);
5490 return false;
5491 }
5492
5493 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5494 return false;
5495
5496 /* Merge Tag_compatibility attributes and any common GNU ones. */
5497 return _bfd_elf_merge_object_attributes (ibfd, info);
5498 }
5499
5500 static bool
5501 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5502 {
5503 /* Print normal ELF private data. */
5504 _bfd_elf_print_private_bfd_data (abfd, ptr);
5505
5506 if (elf_elfheader (abfd)->e_flags != 0)
5507 {
5508 FILE *file = ptr;
5509
5510 fprintf (file, _("private flags = 0x%lx:"),
5511 elf_elfheader (abfd)->e_flags);
5512
5513 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5514 fprintf (file, _(" [abiv%ld]"),
5515 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5516 fputc ('\n', file);
5517 }
5518
5519 return true;
5520 }
5521
5522 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5523 of the code entry point, and its section, which must be in the same
5524 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5525
5526 static bfd_vma
5527 opd_entry_value (asection *opd_sec,
5528 bfd_vma offset,
5529 asection **code_sec,
5530 bfd_vma *code_off,
5531 bool in_code_sec)
5532 {
5533 bfd *opd_bfd = opd_sec->owner;
5534 Elf_Internal_Rela *relocs;
5535 Elf_Internal_Rela *lo, *hi, *look;
5536 bfd_vma val;
5537
5538 /* No relocs implies we are linking a --just-symbols object, or looking
5539 at a final linked executable with addr2line or somesuch. */
5540 if (opd_sec->reloc_count == 0)
5541 {
5542 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5543
5544 if (contents == NULL)
5545 {
5546 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5547 return (bfd_vma) -1;
5548 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5549 }
5550
5551 /* PR 17512: file: 64b9dfbb. */
5552 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5553 return (bfd_vma) -1;
5554
5555 val = bfd_get_64 (opd_bfd, contents + offset);
5556 if (code_sec != NULL)
5557 {
5558 asection *sec, *likely = NULL;
5559
5560 if (in_code_sec)
5561 {
5562 sec = *code_sec;
5563 if (sec->vma <= val
5564 && val < sec->vma + sec->size)
5565 likely = sec;
5566 else
5567 val = -1;
5568 }
5569 else
5570 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5571 if (sec->vma <= val
5572 && (sec->flags & SEC_LOAD) != 0
5573 && (sec->flags & SEC_ALLOC) != 0)
5574 likely = sec;
5575 if (likely != NULL)
5576 {
5577 *code_sec = likely;
5578 if (code_off != NULL)
5579 *code_off = val - likely->vma;
5580 }
5581 }
5582 return val;
5583 }
5584
5585 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5586
5587 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5588 if (relocs == NULL)
5589 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5590 /* PR 17512: file: df8e1fd6. */
5591 if (relocs == NULL)
5592 return (bfd_vma) -1;
5593
5594 /* Go find the opd reloc at the sym address. */
5595 lo = relocs;
5596 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5597 val = (bfd_vma) -1;
5598 while (lo < hi)
5599 {
5600 look = lo + (hi - lo) / 2;
5601 if (look->r_offset < offset)
5602 lo = look + 1;
5603 else if (look->r_offset > offset)
5604 hi = look;
5605 else
5606 {
5607 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5608
5609 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5610 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5611 {
5612 unsigned long symndx = ELF64_R_SYM (look->r_info);
5613 asection *sec = NULL;
5614
5615 if (symndx >= symtab_hdr->sh_info
5616 && elf_sym_hashes (opd_bfd) != NULL)
5617 {
5618 struct elf_link_hash_entry **sym_hashes;
5619 struct elf_link_hash_entry *rh;
5620
5621 sym_hashes = elf_sym_hashes (opd_bfd);
5622 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5623 if (rh != NULL)
5624 {
5625 rh = elf_follow_link (rh);
5626 if (rh->root.type != bfd_link_hash_defined
5627 && rh->root.type != bfd_link_hash_defweak)
5628 break;
5629 if (rh->root.u.def.section->owner == opd_bfd)
5630 {
5631 val = rh->root.u.def.value;
5632 sec = rh->root.u.def.section;
5633 }
5634 }
5635 }
5636
5637 if (sec == NULL)
5638 {
5639 Elf_Internal_Sym *sym;
5640
5641 if (symndx < symtab_hdr->sh_info)
5642 {
5643 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5644 if (sym == NULL)
5645 {
5646 size_t symcnt = symtab_hdr->sh_info;
5647 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5648 symcnt, 0,
5649 NULL, NULL, NULL);
5650 if (sym == NULL)
5651 break;
5652 symtab_hdr->contents = (bfd_byte *) sym;
5653 }
5654 sym += symndx;
5655 }
5656 else
5657 {
5658 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5659 1, symndx,
5660 NULL, NULL, NULL);
5661 if (sym == NULL)
5662 break;
5663 }
5664 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5665 if (sec == NULL)
5666 break;
5667 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5668 val = sym->st_value;
5669 }
5670
5671 val += look->r_addend;
5672 if (code_off != NULL)
5673 *code_off = val;
5674 if (code_sec != NULL)
5675 {
5676 if (in_code_sec && *code_sec != sec)
5677 return -1;
5678 else
5679 *code_sec = sec;
5680 }
5681 if (sec->output_section != NULL)
5682 val += sec->output_section->vma + sec->output_offset;
5683 }
5684 break;
5685 }
5686 }
5687
5688 return val;
5689 }
5690
5691 /* If the ELF symbol SYM might be a function in SEC, return the
5692 function size and set *CODE_OFF to the function's entry point,
5693 otherwise return zero. */
5694
5695 static bfd_size_type
5696 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5697 bfd_vma *code_off)
5698 {
5699 bfd_size_type size;
5700 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5701
5702 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5703 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5704 return 0;
5705
5706 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5707
5708 /* In theory we should check that the symbol's type satisfies
5709 _bfd_elf_is_function_type(), but there are some function-like
5710 symbols which would fail this test. (eg _start). Instead
5711 we check for hidden, local, notype symbols with zero size.
5712 This type of symbol is generated by the annobin plugin for gcc
5713 and clang, and should not be considered to be a function symbol. */
5714 if (size == 0
5715 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5716 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5717 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5718 return 0;
5719
5720 if (strcmp (sym->section->name, ".opd") == 0)
5721 {
5722 struct _opd_sec_data *opd = get_opd_info (sym->section);
5723 bfd_vma symval = sym->value;
5724
5725 if (opd != NULL
5726 && opd->adjust != NULL
5727 && elf_section_data (sym->section)->relocs != NULL)
5728 {
5729 /* opd_entry_value will use cached relocs that have been
5730 adjusted, but with raw symbols. That means both local
5731 and global symbols need adjusting. */
5732 long adjust = opd->adjust[OPD_NDX (symval)];
5733 if (adjust == -1)
5734 return 0;
5735 symval += adjust;
5736 }
5737
5738 if (opd_entry_value (sym->section, symval,
5739 &sec, code_off, true) == (bfd_vma) -1)
5740 return 0;
5741 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5742 symbol. This size has nothing to do with the code size of the
5743 function, which is what we're supposed to return, but the
5744 code size isn't available without looking up the dot-sym.
5745 However, doing that would be a waste of time particularly
5746 since elf_find_function will look at the dot-sym anyway.
5747 Now, elf_find_function will keep the largest size of any
5748 function sym found at the code address of interest, so return
5749 1 here to avoid it incorrectly caching a larger function size
5750 for a small function. This does mean we return the wrong
5751 size for a new-ABI function of size 24, but all that does is
5752 disable caching for such functions. */
5753 if (size == 24)
5754 size = 1;
5755 }
5756 else
5757 {
5758 if (sym->section != sec)
5759 return 0;
5760 *code_off = sym->value;
5761 }
5762
5763 /* Do not return 0 for the function's size. */
5764 return size ? size : 1;
5765 }
5766
5767 /* Return true if symbol is a strong function defined in an ELFv2
5768 object with st_other localentry bits of zero, ie. its local entry
5769 point coincides with its global entry point. */
5770
5771 static bool
5772 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5773 {
5774 return (h != NULL
5775 && h->type == STT_FUNC
5776 && h->root.type == bfd_link_hash_defined
5777 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5778 && !ppc_elf_hash_entry (h)->non_zero_localentry
5779 && is_ppc64_elf (h->root.u.def.section->owner)
5780 && abiversion (h->root.u.def.section->owner) >= 2);
5781 }
5782
5783 /* Return true if symbol is defined in a regular object file. */
5784
5785 static bool
5786 is_static_defined (struct elf_link_hash_entry *h)
5787 {
5788 return ((h->root.type == bfd_link_hash_defined
5789 || h->root.type == bfd_link_hash_defweak)
5790 && h->root.u.def.section != NULL
5791 && h->root.u.def.section->output_section != NULL);
5792 }
5793
5794 /* If FDH is a function descriptor symbol, return the associated code
5795 entry symbol if it is defined. Return NULL otherwise. */
5796
5797 static struct ppc_link_hash_entry *
5798 defined_code_entry (struct ppc_link_hash_entry *fdh)
5799 {
5800 if (fdh->is_func_descriptor)
5801 {
5802 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5803 if (fh->elf.root.type == bfd_link_hash_defined
5804 || fh->elf.root.type == bfd_link_hash_defweak)
5805 return fh;
5806 }
5807 return NULL;
5808 }
5809
5810 /* If FH is a function code entry symbol, return the associated
5811 function descriptor symbol if it is defined. Return NULL otherwise. */
5812
5813 static struct ppc_link_hash_entry *
5814 defined_func_desc (struct ppc_link_hash_entry *fh)
5815 {
5816 if (fh->oh != NULL
5817 && fh->oh->is_func_descriptor)
5818 {
5819 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5820 if (fdh->elf.root.type == bfd_link_hash_defined
5821 || fdh->elf.root.type == bfd_link_hash_defweak)
5822 return fdh;
5823 }
5824 return NULL;
5825 }
5826
5827 /* Given H is a symbol that satisfies is_static_defined, return the
5828 value in the output file. */
5829
5830 static bfd_vma
5831 defined_sym_val (struct elf_link_hash_entry *h)
5832 {
5833 return (h->root.u.def.section->output_section->vma
5834 + h->root.u.def.section->output_offset
5835 + h->root.u.def.value);
5836 }
5837
5838 /* Return true if H matches __tls_get_addr or one of its variants. */
5839
5840 static bool
5841 is_tls_get_addr (struct elf_link_hash_entry *h,
5842 struct ppc_link_hash_table *htab)
5843 {
5844 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5845 || h == elf_hash_entry (htab->tga_desc_fd)
5846 || h == elf_hash_entry (htab->tls_get_addr)
5847 || h == elf_hash_entry (htab->tga_desc));
5848 }
5849
5850 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5851
5852 /* Garbage collect sections, after first dealing with dot-symbols. */
5853
5854 static bool
5855 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5856 {
5857 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5858
5859 if (htab != NULL && htab->need_func_desc_adj)
5860 {
5861 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5862 htab->need_func_desc_adj = 0;
5863 }
5864 return bfd_elf_gc_sections (abfd, info);
5865 }
5866
5867 /* Mark all our entry sym sections, both opd and code section. */
5868
5869 static void
5870 ppc64_elf_gc_keep (struct bfd_link_info *info)
5871 {
5872 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5873 struct bfd_sym_chain *sym;
5874
5875 if (htab == NULL)
5876 return;
5877
5878 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5879 {
5880 struct ppc_link_hash_entry *eh, *fh;
5881 asection *sec;
5882
5883 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5884 false, false, true));
5885 if (eh == NULL)
5886 continue;
5887 if (eh->elf.root.type != bfd_link_hash_defined
5888 && eh->elf.root.type != bfd_link_hash_defweak)
5889 continue;
5890
5891 fh = defined_code_entry (eh);
5892 if (fh != NULL)
5893 {
5894 sec = fh->elf.root.u.def.section;
5895 sec->flags |= SEC_KEEP;
5896 }
5897 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5898 && opd_entry_value (eh->elf.root.u.def.section,
5899 eh->elf.root.u.def.value,
5900 &sec, NULL, false) != (bfd_vma) -1)
5901 sec->flags |= SEC_KEEP;
5902
5903 sec = eh->elf.root.u.def.section;
5904 sec->flags |= SEC_KEEP;
5905 }
5906 }
5907
5908 /* Mark sections containing dynamically referenced symbols. When
5909 building shared libraries, we must assume that any visible symbol is
5910 referenced. */
5911
5912 static bool
5913 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5914 {
5915 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5916 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5917 struct ppc_link_hash_entry *fdh;
5918 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5919
5920 /* Dynamic linking info is on the func descriptor sym. */
5921 fdh = defined_func_desc (eh);
5922 if (fdh != NULL)
5923 eh = fdh;
5924
5925 if ((eh->elf.root.type == bfd_link_hash_defined
5926 || eh->elf.root.type == bfd_link_hash_defweak)
5927 && (!eh->elf.start_stop
5928 || eh->elf.root.ldscript_def
5929 || !info->start_stop_gc)
5930 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5931 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5932 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5933 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5934 && (!bfd_link_executable (info)
5935 || info->gc_keep_exported
5936 || info->export_dynamic
5937 || (eh->elf.dynamic
5938 && d != NULL
5939 && (*d->match) (&d->head, NULL,
5940 eh->elf.root.root.string)))
5941 && (eh->elf.versioned >= versioned
5942 || !bfd_hide_sym_by_version (info->version_info,
5943 eh->elf.root.root.string)))))
5944 {
5945 asection *code_sec;
5946 struct ppc_link_hash_entry *fh;
5947
5948 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5949
5950 /* Function descriptor syms cause the associated
5951 function code sym section to be marked. */
5952 fh = defined_code_entry (eh);
5953 if (fh != NULL)
5954 {
5955 code_sec = fh->elf.root.u.def.section;
5956 code_sec->flags |= SEC_KEEP;
5957 }
5958 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5959 && opd_entry_value (eh->elf.root.u.def.section,
5960 eh->elf.root.u.def.value,
5961 &code_sec, NULL, false) != (bfd_vma) -1)
5962 code_sec->flags |= SEC_KEEP;
5963 }
5964
5965 return true;
5966 }
5967
5968 /* Return the section that should be marked against GC for a given
5969 relocation. */
5970
5971 static asection *
5972 ppc64_elf_gc_mark_hook (asection *sec,
5973 struct bfd_link_info *info,
5974 Elf_Internal_Rela *rel,
5975 struct elf_link_hash_entry *h,
5976 Elf_Internal_Sym *sym)
5977 {
5978 asection *rsec;
5979
5980 /* Syms return NULL if we're marking .opd, so we avoid marking all
5981 function sections, as all functions are referenced in .opd. */
5982 rsec = NULL;
5983 if (get_opd_info (sec) != NULL)
5984 return rsec;
5985
5986 if (h != NULL)
5987 {
5988 enum elf_ppc64_reloc_type r_type;
5989 struct ppc_link_hash_entry *eh, *fh, *fdh;
5990
5991 r_type = ELF64_R_TYPE (rel->r_info);
5992 switch (r_type)
5993 {
5994 case R_PPC64_GNU_VTINHERIT:
5995 case R_PPC64_GNU_VTENTRY:
5996 break;
5997
5998 default:
5999 switch (h->root.type)
6000 {
6001 case bfd_link_hash_defined:
6002 case bfd_link_hash_defweak:
6003 eh = ppc_elf_hash_entry (h);
6004 fdh = defined_func_desc (eh);
6005 if (fdh != NULL)
6006 {
6007 /* -mcall-aixdesc code references the dot-symbol on
6008 a call reloc. Mark the function descriptor too
6009 against garbage collection. */
6010 fdh->elf.mark = 1;
6011 if (fdh->elf.is_weakalias)
6012 weakdef (&fdh->elf)->mark = 1;
6013 eh = fdh;
6014 }
6015
6016 /* Function descriptor syms cause the associated
6017 function code sym section to be marked. */
6018 fh = defined_code_entry (eh);
6019 if (fh != NULL)
6020 {
6021 /* They also mark their opd section. */
6022 eh->elf.root.u.def.section->gc_mark = 1;
6023
6024 rsec = fh->elf.root.u.def.section;
6025 }
6026 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6027 && opd_entry_value (eh->elf.root.u.def.section,
6028 eh->elf.root.u.def.value,
6029 &rsec, NULL, false) != (bfd_vma) -1)
6030 eh->elf.root.u.def.section->gc_mark = 1;
6031 else
6032 rsec = h->root.u.def.section;
6033 break;
6034
6035 case bfd_link_hash_common:
6036 rsec = h->root.u.c.p->section;
6037 break;
6038
6039 default:
6040 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6041 }
6042 }
6043 }
6044 else
6045 {
6046 struct _opd_sec_data *opd;
6047
6048 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6049 opd = get_opd_info (rsec);
6050 if (opd != NULL && opd->func_sec != NULL)
6051 {
6052 rsec->gc_mark = 1;
6053
6054 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6055 }
6056 }
6057
6058 return rsec;
6059 }
6060
6061 /* The maximum size of .sfpr. */
6062 #define SFPR_MAX (218*4)
6063
6064 struct sfpr_def_parms
6065 {
6066 const char name[12];
6067 unsigned char lo, hi;
6068 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6069 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6070 };
6071
6072 /* Auto-generate _save*, _rest* functions in .sfpr.
6073 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6074 instead. */
6075
6076 static bool
6077 sfpr_define (struct bfd_link_info *info,
6078 const struct sfpr_def_parms *parm,
6079 asection *stub_sec)
6080 {
6081 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6082 unsigned int i;
6083 size_t len = strlen (parm->name);
6084 bool writing = false;
6085 char sym[16];
6086
6087 if (htab == NULL)
6088 return false;
6089
6090 memcpy (sym, parm->name, len);
6091 sym[len + 2] = 0;
6092
6093 for (i = parm->lo; i <= parm->hi; i++)
6094 {
6095 struct ppc_link_hash_entry *h;
6096
6097 sym[len + 0] = i / 10 + '0';
6098 sym[len + 1] = i % 10 + '0';
6099 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6100 writing, true, true));
6101 if (stub_sec != NULL)
6102 {
6103 if (h != NULL
6104 && h->elf.root.type == bfd_link_hash_defined
6105 && h->elf.root.u.def.section == htab->sfpr)
6106 {
6107 struct elf_link_hash_entry *s;
6108 char buf[32];
6109 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6110 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6111 if (s == NULL)
6112 return false;
6113 if (s->root.type == bfd_link_hash_new)
6114 {
6115 s->root.type = bfd_link_hash_defined;
6116 s->root.u.def.section = stub_sec;
6117 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6118 + h->elf.root.u.def.value);
6119 s->ref_regular = 1;
6120 s->def_regular = 1;
6121 s->ref_regular_nonweak = 1;
6122 s->forced_local = 1;
6123 s->non_elf = 0;
6124 s->root.linker_def = 1;
6125 }
6126 }
6127 continue;
6128 }
6129 if (h != NULL)
6130 {
6131 h->save_res = 1;
6132 if (!h->elf.def_regular)
6133 {
6134 h->elf.root.type = bfd_link_hash_defined;
6135 h->elf.root.u.def.section = htab->sfpr;
6136 h->elf.root.u.def.value = htab->sfpr->size;
6137 h->elf.type = STT_FUNC;
6138 h->elf.def_regular = 1;
6139 h->elf.non_elf = 0;
6140 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6141 writing = true;
6142 if (htab->sfpr->contents == NULL)
6143 {
6144 htab->sfpr->contents
6145 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6146 if (htab->sfpr->contents == NULL)
6147 return false;
6148 }
6149 }
6150 }
6151 if (writing)
6152 {
6153 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6154 if (i != parm->hi)
6155 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6156 else
6157 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6158 htab->sfpr->size = p - htab->sfpr->contents;
6159 }
6160 }
6161
6162 return true;
6163 }
6164
6165 static bfd_byte *
6166 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6167 {
6168 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6169 return p + 4;
6170 }
6171
6172 static bfd_byte *
6173 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6174 {
6175 p = savegpr0 (abfd, p, r);
6176 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6177 p = p + 4;
6178 bfd_put_32 (abfd, BLR, p);
6179 return p + 4;
6180 }
6181
6182 static bfd_byte *
6183 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6184 {
6185 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6186 return p + 4;
6187 }
6188
6189 static bfd_byte *
6190 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6191 {
6192 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6193 p = p + 4;
6194 p = restgpr0 (abfd, p, r);
6195 bfd_put_32 (abfd, MTLR_R0, p);
6196 p = p + 4;
6197 if (r == 29)
6198 {
6199 p = restgpr0 (abfd, p, 30);
6200 p = restgpr0 (abfd, p, 31);
6201 }
6202 bfd_put_32 (abfd, BLR, p);
6203 return p + 4;
6204 }
6205
6206 static bfd_byte *
6207 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6208 {
6209 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6210 return p + 4;
6211 }
6212
6213 static bfd_byte *
6214 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6215 {
6216 p = savegpr1 (abfd, p, r);
6217 bfd_put_32 (abfd, BLR, p);
6218 return p + 4;
6219 }
6220
6221 static bfd_byte *
6222 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6223 {
6224 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6225 return p + 4;
6226 }
6227
6228 static bfd_byte *
6229 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6230 {
6231 p = restgpr1 (abfd, p, r);
6232 bfd_put_32 (abfd, BLR, p);
6233 return p + 4;
6234 }
6235
6236 static bfd_byte *
6237 savefpr (bfd *abfd, bfd_byte *p, int r)
6238 {
6239 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6240 return p + 4;
6241 }
6242
6243 static bfd_byte *
6244 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6245 {
6246 p = savefpr (abfd, p, r);
6247 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6248 p = p + 4;
6249 bfd_put_32 (abfd, BLR, p);
6250 return p + 4;
6251 }
6252
6253 static bfd_byte *
6254 restfpr (bfd *abfd, bfd_byte *p, int r)
6255 {
6256 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6257 return p + 4;
6258 }
6259
6260 static bfd_byte *
6261 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6262 {
6263 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6264 p = p + 4;
6265 p = restfpr (abfd, p, r);
6266 bfd_put_32 (abfd, MTLR_R0, p);
6267 p = p + 4;
6268 if (r == 29)
6269 {
6270 p = restfpr (abfd, p, 30);
6271 p = restfpr (abfd, p, 31);
6272 }
6273 bfd_put_32 (abfd, BLR, p);
6274 return p + 4;
6275 }
6276
6277 static bfd_byte *
6278 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6279 {
6280 p = savefpr (abfd, p, r);
6281 bfd_put_32 (abfd, BLR, p);
6282 return p + 4;
6283 }
6284
6285 static bfd_byte *
6286 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6287 {
6288 p = restfpr (abfd, p, r);
6289 bfd_put_32 (abfd, BLR, p);
6290 return p + 4;
6291 }
6292
6293 static bfd_byte *
6294 savevr (bfd *abfd, bfd_byte *p, int r)
6295 {
6296 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6297 p = p + 4;
6298 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6299 return p + 4;
6300 }
6301
6302 static bfd_byte *
6303 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6304 {
6305 p = savevr (abfd, p, r);
6306 bfd_put_32 (abfd, BLR, p);
6307 return p + 4;
6308 }
6309
6310 static bfd_byte *
6311 restvr (bfd *abfd, bfd_byte *p, int r)
6312 {
6313 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6314 p = p + 4;
6315 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6316 return p + 4;
6317 }
6318
6319 static bfd_byte *
6320 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6321 {
6322 p = restvr (abfd, p, r);
6323 bfd_put_32 (abfd, BLR, p);
6324 return p + 4;
6325 }
6326
6327 #define STDU_R1_0R1 0xf8210001
6328 #define ADDI_R1_R1 0x38210000
6329
6330 /* Emit prologue of wrapper preserving regs around a call to
6331 __tls_get_addr_opt. */
6332
6333 static bfd_byte *
6334 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6335 {
6336 unsigned int i;
6337
6338 bfd_put_32 (obfd, MFLR_R0, p);
6339 p += 4;
6340 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6341 p += 4;
6342
6343 if (htab->opd_abi)
6344 {
6345 for (i = 4; i < 12; i++)
6346 {
6347 bfd_put_32 (obfd,
6348 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6349 p += 4;
6350 }
6351 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6352 p += 4;
6353 }
6354 else
6355 {
6356 for (i = 4; i < 12; i++)
6357 {
6358 bfd_put_32 (obfd,
6359 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6360 p += 4;
6361 }
6362 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6363 p += 4;
6364 }
6365 return p;
6366 }
6367
6368 /* Emit epilogue of wrapper preserving regs around a call to
6369 __tls_get_addr_opt. */
6370
6371 static bfd_byte *
6372 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6373 {
6374 unsigned int i;
6375
6376 if (htab->opd_abi)
6377 {
6378 for (i = 4; i < 12; i++)
6379 {
6380 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6381 p += 4;
6382 }
6383 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6384 p += 4;
6385 }
6386 else
6387 {
6388 for (i = 4; i < 12; i++)
6389 {
6390 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6391 p += 4;
6392 }
6393 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6394 p += 4;
6395 }
6396 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6397 p += 4;
6398 bfd_put_32 (obfd, MTLR_R0, p);
6399 p += 4;
6400 bfd_put_32 (obfd, BLR, p);
6401 p += 4;
6402 return p;
6403 }
6404
6405 /* Called via elf_link_hash_traverse to transfer dynamic linking
6406 information on function code symbol entries to their corresponding
6407 function descriptor symbol entries. Must not be called twice for
6408 any given code symbol. */
6409
6410 static bool
6411 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6412 {
6413 struct bfd_link_info *info;
6414 struct ppc_link_hash_table *htab;
6415 struct ppc_link_hash_entry *fh;
6416 struct ppc_link_hash_entry *fdh;
6417 bool force_local;
6418
6419 fh = ppc_elf_hash_entry (h);
6420 if (fh->elf.root.type == bfd_link_hash_indirect)
6421 return true;
6422
6423 if (!fh->is_func)
6424 return true;
6425
6426 if (fh->elf.root.root.string[0] != '.'
6427 || fh->elf.root.root.string[1] == '\0')
6428 return true;
6429
6430 info = inf;
6431 htab = ppc_hash_table (info);
6432 if (htab == NULL)
6433 return false;
6434
6435 /* Find the corresponding function descriptor symbol. */
6436 fdh = lookup_fdh (fh, htab);
6437
6438 /* Resolve undefined references to dot-symbols as the value
6439 in the function descriptor, if we have one in a regular object.
6440 This is to satisfy cases like ".quad .foo". Calls to functions
6441 in dynamic objects are handled elsewhere. */
6442 if ((fh->elf.root.type == bfd_link_hash_undefined
6443 || fh->elf.root.type == bfd_link_hash_undefweak)
6444 && (fdh->elf.root.type == bfd_link_hash_defined
6445 || fdh->elf.root.type == bfd_link_hash_defweak)
6446 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6447 && opd_entry_value (fdh->elf.root.u.def.section,
6448 fdh->elf.root.u.def.value,
6449 &fh->elf.root.u.def.section,
6450 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6451 {
6452 fh->elf.root.type = fdh->elf.root.type;
6453 fh->elf.forced_local = 1;
6454 fh->elf.def_regular = fdh->elf.def_regular;
6455 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6456 }
6457
6458 if (!fh->elf.dynamic)
6459 {
6460 struct plt_entry *ent;
6461
6462 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6463 if (ent->plt.refcount > 0)
6464 break;
6465 if (ent == NULL)
6466 {
6467 if (fdh != NULL && fdh->fake)
6468 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6469 return true;
6470 }
6471 }
6472
6473 /* Create a descriptor as undefined if necessary. */
6474 if (fdh == NULL
6475 && !bfd_link_executable (info)
6476 && (fh->elf.root.type == bfd_link_hash_undefined
6477 || fh->elf.root.type == bfd_link_hash_undefweak))
6478 {
6479 fdh = make_fdh (info, fh);
6480 if (fdh == NULL)
6481 return false;
6482 }
6483
6484 /* We can't support overriding of symbols on a fake descriptor. */
6485 if (fdh != NULL
6486 && fdh->fake
6487 && (fh->elf.root.type == bfd_link_hash_defined
6488 || fh->elf.root.type == bfd_link_hash_defweak))
6489 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6490
6491 /* Transfer dynamic linking information to the function descriptor. */
6492 if (fdh != NULL)
6493 {
6494 fdh->elf.ref_regular |= fh->elf.ref_regular;
6495 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6496 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6497 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6498 fdh->elf.dynamic |= fh->elf.dynamic;
6499 fdh->elf.needs_plt |= (fh->elf.needs_plt
6500 || fh->elf.type == STT_FUNC
6501 || fh->elf.type == STT_GNU_IFUNC);
6502 move_plt_plist (fh, fdh);
6503
6504 if (!fdh->elf.forced_local
6505 && fh->elf.dynindx != -1)
6506 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6507 return false;
6508 }
6509
6510 /* Now that the info is on the function descriptor, clear the
6511 function code sym info. Any function code syms for which we
6512 don't have a definition in a regular file, we force local.
6513 This prevents a shared library from exporting syms that have
6514 been imported from another library. Function code syms that
6515 are really in the library we must leave global to prevent the
6516 linker dragging in a definition from a static library. */
6517 force_local = (!fh->elf.def_regular
6518 || fdh == NULL
6519 || !fdh->elf.def_regular
6520 || fdh->elf.forced_local);
6521 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6522
6523 return true;
6524 }
6525
6526 static const struct sfpr_def_parms save_res_funcs[] =
6527 {
6528 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6529 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6530 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6531 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6532 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6533 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6534 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6535 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6536 { "._savef", 14, 31, savefpr, savefpr1_tail },
6537 { "._restf", 14, 31, restfpr, restfpr1_tail },
6538 { "_savevr_", 20, 31, savevr, savevr_tail },
6539 { "_restvr_", 20, 31, restvr, restvr_tail }
6540 };
6541
6542 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6543 this hook to a) run the edit functions in this file, b) provide
6544 some gcc support functions, and c) transfer dynamic linking
6545 information gathered so far on function code symbol entries, to
6546 their corresponding function descriptor symbol entries. */
6547
6548 static bool
6549 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6550 {
6551 struct ppc_link_hash_table *htab;
6552
6553 htab = ppc_hash_table (info);
6554 if (htab == NULL)
6555 return false;
6556
6557 /* Call back into the linker, which then runs the edit functions. */
6558 htab->params->edit ();
6559
6560 /* Provide any missing _save* and _rest* functions. */
6561 if (htab->sfpr != NULL)
6562 {
6563 unsigned int i;
6564
6565 htab->sfpr->size = 0;
6566 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6567 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6568 return false;
6569 if (htab->sfpr->size == 0)
6570 htab->sfpr->flags |= SEC_EXCLUDE;
6571 }
6572
6573 if (bfd_link_relocatable (info))
6574 return true;
6575
6576 if (htab->elf.hgot != NULL)
6577 {
6578 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6579 /* Make .TOC. defined so as to prevent it being made dynamic.
6580 The wrong value here is fixed later in ppc64_elf_set_toc. */
6581 if (!htab->elf.hgot->def_regular
6582 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6583 {
6584 htab->elf.hgot->root.type = bfd_link_hash_defined;
6585 htab->elf.hgot->root.u.def.value = 0;
6586 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6587 htab->elf.hgot->def_regular = 1;
6588 htab->elf.hgot->root.linker_def = 1;
6589 }
6590 htab->elf.hgot->type = STT_OBJECT;
6591 htab->elf.hgot->other
6592 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6593 }
6594
6595 return true;
6596 }
6597
6598 /* Return true if we have dynamic relocs against H or any of its weak
6599 aliases, that apply to read-only sections. Cannot be used after
6600 size_dynamic_sections. */
6601
6602 static bool
6603 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6604 {
6605 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6606 do
6607 {
6608 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6609 return true;
6610 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6611 }
6612 while (eh != NULL && &eh->elf != h);
6613
6614 return false;
6615 }
6616
6617 /* Return whether EH has pc-relative dynamic relocs. */
6618
6619 static bool
6620 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6621 {
6622 struct ppc_dyn_relocs *p;
6623
6624 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6625 if (p->pc_count != 0)
6626 return true;
6627 return false;
6628 }
6629
6630 /* Return true if a global entry stub will be created for H. Valid
6631 for ELFv2 before plt entries have been allocated. */
6632
6633 static bool
6634 global_entry_stub (struct elf_link_hash_entry *h)
6635 {
6636 struct plt_entry *pent;
6637
6638 if (!h->pointer_equality_needed
6639 || h->def_regular)
6640 return false;
6641
6642 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6643 if (pent->plt.refcount > 0
6644 && pent->addend == 0)
6645 return true;
6646
6647 return false;
6648 }
6649
6650 /* Adjust a symbol defined by a dynamic object and referenced by a
6651 regular object. The current definition is in some section of the
6652 dynamic object, but we're not including those sections. We have to
6653 change the definition to something the rest of the link can
6654 understand. */
6655
6656 static bool
6657 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6658 struct elf_link_hash_entry *h)
6659 {
6660 struct ppc_link_hash_table *htab;
6661 asection *s, *srel;
6662
6663 htab = ppc_hash_table (info);
6664 if (htab == NULL)
6665 return false;
6666
6667 /* Deal with function syms. */
6668 if (h->type == STT_FUNC
6669 || h->type == STT_GNU_IFUNC
6670 || h->needs_plt)
6671 {
6672 bool local = (ppc_elf_hash_entry (h)->save_res
6673 || SYMBOL_CALLS_LOCAL (info, h)
6674 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6675 /* Discard dyn_relocs when non-pic if we've decided that a
6676 function symbol is local and not an ifunc. We keep dynamic
6677 relocs for ifuncs when local rather than always emitting a
6678 plt call stub for them and defining the symbol on the call
6679 stub. We can't do that for ELFv1 anyway (a function symbol
6680 is defined on a descriptor, not code) and it can be faster at
6681 run-time due to not needing to bounce through a stub. The
6682 dyn_relocs for ifuncs will be applied even in a static
6683 executable. */
6684 if (!bfd_link_pic (info)
6685 && h->type != STT_GNU_IFUNC
6686 && local)
6687 h->dyn_relocs = NULL;
6688
6689 /* Clear procedure linkage table information for any symbol that
6690 won't need a .plt entry. */
6691 struct plt_entry *ent;
6692 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6693 if (ent->plt.refcount > 0)
6694 break;
6695 if (ent == NULL
6696 || (h->type != STT_GNU_IFUNC
6697 && local
6698 && (htab->can_convert_all_inline_plt
6699 || (ppc_elf_hash_entry (h)->tls_mask
6700 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6701 {
6702 h->plt.plist = NULL;
6703 h->needs_plt = 0;
6704 h->pointer_equality_needed = 0;
6705 }
6706 else if (abiversion (info->output_bfd) >= 2)
6707 {
6708 /* Taking a function's address in a read/write section
6709 doesn't require us to define the function symbol in the
6710 executable on a global entry stub. A dynamic reloc can
6711 be used instead. The reason we prefer a few more dynamic
6712 relocs is that calling via a global entry stub costs a
6713 few more instructions, and pointer_equality_needed causes
6714 extra work in ld.so when resolving these symbols. */
6715 if (global_entry_stub (h))
6716 {
6717 if (!_bfd_elf_readonly_dynrelocs (h))
6718 {
6719 h->pointer_equality_needed = 0;
6720 /* If we haven't seen a branch reloc and the symbol
6721 isn't an ifunc then we don't need a plt entry. */
6722 if (!h->needs_plt)
6723 h->plt.plist = NULL;
6724 }
6725 else if (!bfd_link_pic (info))
6726 /* We are going to be defining the function symbol on the
6727 plt stub, so no dyn_relocs needed when non-pic. */
6728 h->dyn_relocs = NULL;
6729 }
6730
6731 /* ELFv2 function symbols can't have copy relocs. */
6732 return true;
6733 }
6734 else if (!h->needs_plt
6735 && !_bfd_elf_readonly_dynrelocs (h))
6736 {
6737 /* If we haven't seen a branch reloc and the symbol isn't an
6738 ifunc then we don't need a plt entry. */
6739 h->plt.plist = NULL;
6740 h->pointer_equality_needed = 0;
6741 return true;
6742 }
6743 }
6744 else
6745 h->plt.plist = NULL;
6746
6747 /* If this is a weak symbol, and there is a real definition, the
6748 processor independent code will have arranged for us to see the
6749 real definition first, and we can just use the same value. */
6750 if (h->is_weakalias)
6751 {
6752 struct elf_link_hash_entry *def = weakdef (h);
6753 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6754 h->root.u.def.section = def->root.u.def.section;
6755 h->root.u.def.value = def->root.u.def.value;
6756 if (def->root.u.def.section == htab->elf.sdynbss
6757 || def->root.u.def.section == htab->elf.sdynrelro)
6758 h->dyn_relocs = NULL;
6759 return true;
6760 }
6761
6762 /* If we are creating a shared library, we must presume that the
6763 only references to the symbol are via the global offset table.
6764 For such cases we need not do anything here; the relocations will
6765 be handled correctly by relocate_section. */
6766 if (!bfd_link_executable (info))
6767 return true;
6768
6769 /* If there are no references to this symbol that do not use the
6770 GOT, we don't need to generate a copy reloc. */
6771 if (!h->non_got_ref)
6772 return true;
6773
6774 /* Don't generate a copy reloc for symbols defined in the executable. */
6775 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6776
6777 /* If -z nocopyreloc was given, don't generate them either. */
6778 || info->nocopyreloc
6779
6780 /* If we don't find any dynamic relocs in read-only sections, then
6781 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6782 || (ELIMINATE_COPY_RELOCS
6783 && !h->needs_copy
6784 && !alias_readonly_dynrelocs (h))
6785
6786 /* Protected variables do not work with .dynbss. The copy in
6787 .dynbss won't be used by the shared library with the protected
6788 definition for the variable. Text relocations are preferable
6789 to an incorrect program. */
6790 || h->protected_def)
6791 return true;
6792
6793 if (h->type == STT_FUNC
6794 || h->type == STT_GNU_IFUNC)
6795 {
6796 /* .dynbss copies of function symbols only work if we have
6797 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6798 use dot-symbols and set the function symbol size to the text
6799 size of the function rather than the size of the descriptor.
6800 That's wrong for copying a descriptor. */
6801 if (ppc_elf_hash_entry (h)->oh == NULL
6802 || !(h->size == 24 || h->size == 16))
6803 return true;
6804
6805 /* We should never get here, but unfortunately there are old
6806 versions of gcc (circa gcc-3.2) that improperly for the
6807 ELFv1 ABI put initialized function pointers, vtable refs and
6808 suchlike in read-only sections. Allow them to proceed, but
6809 warn that this might break at runtime. */
6810 info->callbacks->einfo
6811 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6812 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6813 h->root.root.string);
6814 }
6815
6816 /* This is a reference to a symbol defined by a dynamic object which
6817 is not a function. */
6818
6819 /* We must allocate the symbol in our .dynbss section, which will
6820 become part of the .bss section of the executable. There will be
6821 an entry for this symbol in the .dynsym section. The dynamic
6822 object will contain position independent code, so all references
6823 from the dynamic object to this symbol will go through the global
6824 offset table. The dynamic linker will use the .dynsym entry to
6825 determine the address it must put in the global offset table, so
6826 both the dynamic object and the regular object will refer to the
6827 same memory location for the variable. */
6828 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6829 {
6830 s = htab->elf.sdynrelro;
6831 srel = htab->elf.sreldynrelro;
6832 }
6833 else
6834 {
6835 s = htab->elf.sdynbss;
6836 srel = htab->elf.srelbss;
6837 }
6838 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6839 {
6840 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6841 linker to copy the initial value out of the dynamic object
6842 and into the runtime process image. */
6843 srel->size += sizeof (Elf64_External_Rela);
6844 h->needs_copy = 1;
6845 }
6846
6847 /* We no longer want dyn_relocs. */
6848 h->dyn_relocs = NULL;
6849 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6850 }
6851
6852 /* If given a function descriptor symbol, hide both the function code
6853 sym and the descriptor. */
6854 static void
6855 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6856 struct elf_link_hash_entry *h,
6857 bool force_local)
6858 {
6859 struct ppc_link_hash_entry *eh;
6860 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6861
6862 if (ppc_hash_table (info) == NULL)
6863 return;
6864
6865 eh = ppc_elf_hash_entry (h);
6866 if (eh->is_func_descriptor)
6867 {
6868 struct ppc_link_hash_entry *fh = eh->oh;
6869
6870 if (fh == NULL)
6871 {
6872 const char *p, *q;
6873 struct elf_link_hash_table *htab = elf_hash_table (info);
6874 char save;
6875
6876 /* We aren't supposed to use alloca in BFD because on
6877 systems which do not have alloca the version in libiberty
6878 calls xmalloc, which might cause the program to crash
6879 when it runs out of memory. This function doesn't have a
6880 return status, so there's no way to gracefully return an
6881 error. So cheat. We know that string[-1] can be safely
6882 accessed; It's either a string in an ELF string table,
6883 or allocated in an objalloc structure. */
6884
6885 p = eh->elf.root.root.string - 1;
6886 save = *p;
6887 *(char *) p = '.';
6888 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6889 false, false));
6890 *(char *) p = save;
6891
6892 /* Unfortunately, if it so happens that the string we were
6893 looking for was allocated immediately before this string,
6894 then we overwrote the string terminator. That's the only
6895 reason the lookup should fail. */
6896 if (fh == NULL)
6897 {
6898 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6899 while (q >= eh->elf.root.root.string && *q == *p)
6900 --q, --p;
6901 if (q < eh->elf.root.root.string && *p == '.')
6902 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6903 false, false));
6904 }
6905 if (fh != NULL)
6906 {
6907 eh->oh = fh;
6908 fh->oh = eh;
6909 }
6910 }
6911 if (fh != NULL)
6912 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6913 }
6914 }
6915
6916 static bool
6917 get_sym_h (struct elf_link_hash_entry **hp,
6918 Elf_Internal_Sym **symp,
6919 asection **symsecp,
6920 unsigned char **tls_maskp,
6921 Elf_Internal_Sym **locsymsp,
6922 unsigned long r_symndx,
6923 bfd *ibfd)
6924 {
6925 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6926
6927 if (r_symndx >= symtab_hdr->sh_info)
6928 {
6929 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6930 struct elf_link_hash_entry *h;
6931
6932 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6933 h = elf_follow_link (h);
6934
6935 if (hp != NULL)
6936 *hp = h;
6937
6938 if (symp != NULL)
6939 *symp = NULL;
6940
6941 if (symsecp != NULL)
6942 {
6943 asection *symsec = NULL;
6944 if (h->root.type == bfd_link_hash_defined
6945 || h->root.type == bfd_link_hash_defweak)
6946 symsec = h->root.u.def.section;
6947 *symsecp = symsec;
6948 }
6949
6950 if (tls_maskp != NULL)
6951 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6952 }
6953 else
6954 {
6955 Elf_Internal_Sym *sym;
6956 Elf_Internal_Sym *locsyms = *locsymsp;
6957
6958 if (locsyms == NULL)
6959 {
6960 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6961 if (locsyms == NULL)
6962 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6963 symtab_hdr->sh_info,
6964 0, NULL, NULL, NULL);
6965 if (locsyms == NULL)
6966 return false;
6967 *locsymsp = locsyms;
6968 }
6969 sym = locsyms + r_symndx;
6970
6971 if (hp != NULL)
6972 *hp = NULL;
6973
6974 if (symp != NULL)
6975 *symp = sym;
6976
6977 if (symsecp != NULL)
6978 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6979
6980 if (tls_maskp != NULL)
6981 {
6982 struct got_entry **lgot_ents;
6983 unsigned char *tls_mask;
6984
6985 tls_mask = NULL;
6986 lgot_ents = elf_local_got_ents (ibfd);
6987 if (lgot_ents != NULL)
6988 {
6989 struct plt_entry **local_plt = (struct plt_entry **)
6990 (lgot_ents + symtab_hdr->sh_info);
6991 unsigned char *lgot_masks = (unsigned char *)
6992 (local_plt + symtab_hdr->sh_info);
6993 tls_mask = &lgot_masks[r_symndx];
6994 }
6995 *tls_maskp = tls_mask;
6996 }
6997 }
6998 return true;
6999 }
7000
7001 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7002 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7003 type suitable for optimization, and 1 otherwise. */
7004
7005 static int
7006 get_tls_mask (unsigned char **tls_maskp,
7007 unsigned long *toc_symndx,
7008 bfd_vma *toc_addend,
7009 Elf_Internal_Sym **locsymsp,
7010 const Elf_Internal_Rela *rel,
7011 bfd *ibfd)
7012 {
7013 unsigned long r_symndx;
7014 int next_r;
7015 struct elf_link_hash_entry *h;
7016 Elf_Internal_Sym *sym;
7017 asection *sec;
7018 bfd_vma off;
7019
7020 r_symndx = ELF64_R_SYM (rel->r_info);
7021 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7022 return 0;
7023
7024 if ((*tls_maskp != NULL
7025 && (**tls_maskp & TLS_TLS) != 0
7026 && **tls_maskp != (TLS_TLS | TLS_MARK))
7027 || sec == NULL
7028 || ppc64_elf_section_data (sec) == NULL
7029 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7030 return 1;
7031
7032 /* Look inside a TOC section too. */
7033 if (h != NULL)
7034 {
7035 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7036 off = h->root.u.def.value;
7037 }
7038 else
7039 off = sym->st_value;
7040 off += rel->r_addend;
7041 BFD_ASSERT (off % 8 == 0);
7042 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7043 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7044 if (toc_symndx != NULL)
7045 *toc_symndx = r_symndx;
7046 if (toc_addend != NULL)
7047 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7048 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7049 return 0;
7050 if ((h == NULL || is_static_defined (h))
7051 && (next_r == -1 || next_r == -2))
7052 return 1 - next_r;
7053 return 1;
7054 }
7055
7056 /* Find (or create) an entry in the tocsave hash table. */
7057
7058 static struct tocsave_entry *
7059 tocsave_find (struct ppc_link_hash_table *htab,
7060 enum insert_option insert,
7061 Elf_Internal_Sym **local_syms,
7062 const Elf_Internal_Rela *irela,
7063 bfd *ibfd)
7064 {
7065 unsigned long r_indx;
7066 struct elf_link_hash_entry *h;
7067 Elf_Internal_Sym *sym;
7068 struct tocsave_entry ent, *p;
7069 hashval_t hash;
7070 struct tocsave_entry **slot;
7071
7072 r_indx = ELF64_R_SYM (irela->r_info);
7073 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7074 return NULL;
7075 if (ent.sec == NULL || ent.sec->output_section == NULL)
7076 {
7077 _bfd_error_handler
7078 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7079 return NULL;
7080 }
7081
7082 if (h != NULL)
7083 ent.offset = h->root.u.def.value;
7084 else
7085 ent.offset = sym->st_value;
7086 ent.offset += irela->r_addend;
7087
7088 hash = tocsave_htab_hash (&ent);
7089 slot = ((struct tocsave_entry **)
7090 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7091 if (slot == NULL)
7092 return NULL;
7093
7094 if (*slot == NULL)
7095 {
7096 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7097 if (p == NULL)
7098 return NULL;
7099 *p = ent;
7100 *slot = p;
7101 }
7102 return *slot;
7103 }
7104
7105 /* Adjust all global syms defined in opd sections. In gcc generated
7106 code for the old ABI, these will already have been done. */
7107
7108 static bool
7109 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7110 {
7111 struct ppc_link_hash_entry *eh;
7112 asection *sym_sec;
7113 struct _opd_sec_data *opd;
7114
7115 if (h->root.type == bfd_link_hash_indirect)
7116 return true;
7117
7118 if (h->root.type != bfd_link_hash_defined
7119 && h->root.type != bfd_link_hash_defweak)
7120 return true;
7121
7122 eh = ppc_elf_hash_entry (h);
7123 if (eh->adjust_done)
7124 return true;
7125
7126 sym_sec = eh->elf.root.u.def.section;
7127 opd = get_opd_info (sym_sec);
7128 if (opd != NULL && opd->adjust != NULL)
7129 {
7130 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7131 if (adjust == -1)
7132 {
7133 /* This entry has been deleted. */
7134 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7135 if (dsec == NULL)
7136 {
7137 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7138 if (discarded_section (dsec))
7139 {
7140 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7141 break;
7142 }
7143 }
7144 eh->elf.root.u.def.value = 0;
7145 eh->elf.root.u.def.section = dsec;
7146 }
7147 else
7148 eh->elf.root.u.def.value += adjust;
7149 eh->adjust_done = 1;
7150 }
7151 return true;
7152 }
7153
7154 /* Handles decrementing dynamic reloc counts for the reloc specified by
7155 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7156 have already been determined. */
7157
7158 static bool
7159 dec_dynrel_count (const Elf_Internal_Rela *rel,
7160 asection *sec,
7161 struct bfd_link_info *info,
7162 Elf_Internal_Sym **local_syms,
7163 struct elf_link_hash_entry *h,
7164 Elf_Internal_Sym *sym)
7165 {
7166 enum elf_ppc64_reloc_type r_type;
7167 asection *sym_sec = NULL;
7168
7169 /* Can this reloc be dynamic? This switch, and later tests here
7170 should be kept in sync with the code in check_relocs. */
7171 r_type = ELF64_R_TYPE (rel->r_info);
7172 switch (r_type)
7173 {
7174 default:
7175 return true;
7176
7177 case R_PPC64_TOC16:
7178 case R_PPC64_TOC16_DS:
7179 case R_PPC64_TOC16_LO:
7180 case R_PPC64_TOC16_HI:
7181 case R_PPC64_TOC16_HA:
7182 case R_PPC64_TOC16_LO_DS:
7183 if (h == NULL)
7184 return true;
7185 break;
7186
7187 case R_PPC64_TPREL16:
7188 case R_PPC64_TPREL16_LO:
7189 case R_PPC64_TPREL16_HI:
7190 case R_PPC64_TPREL16_HA:
7191 case R_PPC64_TPREL16_DS:
7192 case R_PPC64_TPREL16_LO_DS:
7193 case R_PPC64_TPREL16_HIGH:
7194 case R_PPC64_TPREL16_HIGHA:
7195 case R_PPC64_TPREL16_HIGHER:
7196 case R_PPC64_TPREL16_HIGHERA:
7197 case R_PPC64_TPREL16_HIGHEST:
7198 case R_PPC64_TPREL16_HIGHESTA:
7199 case R_PPC64_TPREL64:
7200 case R_PPC64_TPREL34:
7201 case R_PPC64_DTPMOD64:
7202 case R_PPC64_DTPREL64:
7203 case R_PPC64_ADDR64:
7204 case R_PPC64_REL30:
7205 case R_PPC64_REL32:
7206 case R_PPC64_REL64:
7207 case R_PPC64_ADDR14:
7208 case R_PPC64_ADDR14_BRNTAKEN:
7209 case R_PPC64_ADDR14_BRTAKEN:
7210 case R_PPC64_ADDR16:
7211 case R_PPC64_ADDR16_DS:
7212 case R_PPC64_ADDR16_HA:
7213 case R_PPC64_ADDR16_HI:
7214 case R_PPC64_ADDR16_HIGH:
7215 case R_PPC64_ADDR16_HIGHA:
7216 case R_PPC64_ADDR16_HIGHER:
7217 case R_PPC64_ADDR16_HIGHERA:
7218 case R_PPC64_ADDR16_HIGHEST:
7219 case R_PPC64_ADDR16_HIGHESTA:
7220 case R_PPC64_ADDR16_LO:
7221 case R_PPC64_ADDR16_LO_DS:
7222 case R_PPC64_ADDR24:
7223 case R_PPC64_ADDR32:
7224 case R_PPC64_UADDR16:
7225 case R_PPC64_UADDR32:
7226 case R_PPC64_UADDR64:
7227 case R_PPC64_TOC:
7228 case R_PPC64_D34:
7229 case R_PPC64_D34_LO:
7230 case R_PPC64_D34_HI30:
7231 case R_PPC64_D34_HA30:
7232 case R_PPC64_ADDR16_HIGHER34:
7233 case R_PPC64_ADDR16_HIGHERA34:
7234 case R_PPC64_ADDR16_HIGHEST34:
7235 case R_PPC64_ADDR16_HIGHESTA34:
7236 case R_PPC64_D28:
7237 break;
7238 }
7239
7240 if (local_syms != NULL)
7241 {
7242 unsigned long r_symndx;
7243 bfd *ibfd = sec->owner;
7244
7245 r_symndx = ELF64_R_SYM (rel->r_info);
7246 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7247 return false;
7248 }
7249
7250 if ((h != NULL
7251 && (h->root.type == bfd_link_hash_defweak
7252 || !h->def_regular))
7253 || (h != NULL
7254 && !bfd_link_executable (info)
7255 && !SYMBOLIC_BIND (info, h))
7256 || (bfd_link_pic (info)
7257 && must_be_dyn_reloc (info, r_type))
7258 || (!bfd_link_pic (info)
7259 && (h != NULL
7260 ? h->type == STT_GNU_IFUNC
7261 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7262 ;
7263 else
7264 return true;
7265
7266 if (h != NULL)
7267 {
7268 struct ppc_dyn_relocs *p;
7269 struct ppc_dyn_relocs **pp;
7270 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7271
7272 /* elf_gc_sweep may have already removed all dyn relocs associated
7273 with local syms for a given section. Also, symbol flags are
7274 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7275 report a dynreloc miscount. */
7276 if (*pp == NULL && info->gc_sections)
7277 return true;
7278
7279 while ((p = *pp) != NULL)
7280 {
7281 if (p->sec == sec)
7282 {
7283 if (!must_be_dyn_reloc (info, r_type))
7284 p->pc_count -= 1;
7285 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7286 && rel->r_offset % 2 == 0
7287 && sec->alignment_power != 0
7288 && ((!NO_OPD_RELOCS
7289 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
7290 || (h->type != STT_GNU_IFUNC
7291 && SYMBOL_REFERENCES_LOCAL (info, h))))
7292 p->rel_count -= 1;
7293 p->count -= 1;
7294 if (p->count == 0)
7295 *pp = p->next;
7296 return true;
7297 }
7298 pp = &p->next;
7299 }
7300 }
7301 else
7302 {
7303 struct ppc_local_dyn_relocs *p;
7304 struct ppc_local_dyn_relocs **pp;
7305 void *vpp;
7306 bool is_ifunc;
7307
7308 if (local_syms == NULL)
7309 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7310 if (sym_sec == NULL)
7311 sym_sec = sec;
7312
7313 vpp = &elf_section_data (sym_sec)->local_dynrel;
7314 pp = (struct ppc_local_dyn_relocs **) vpp;
7315
7316 if (*pp == NULL && info->gc_sections)
7317 return true;
7318
7319 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7320 while ((p = *pp) != NULL)
7321 {
7322 if (p->sec == sec && p->ifunc == is_ifunc)
7323 {
7324 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7325 && rel->r_offset % 2 == 0
7326 && sec->alignment_power != 0
7327 && ((!NO_OPD_RELOCS
7328 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
7329 || !is_ifunc))
7330 p->rel_count -= 1;
7331 p->count -= 1;
7332 if (p->count == 0)
7333 *pp = p->next;
7334 return true;
7335 }
7336 pp = &p->next;
7337 }
7338 }
7339
7340 /* xgettext:c-format */
7341 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7342 sec->owner, sec);
7343 bfd_set_error (bfd_error_bad_value);
7344 return false;
7345 }
7346
7347 /* Remove unused Official Procedure Descriptor entries. Currently we
7348 only remove those associated with functions in discarded link-once
7349 sections, or weakly defined functions that have been overridden. It
7350 would be possible to remove many more entries for statically linked
7351 applications. */
7352
7353 bool
7354 ppc64_elf_edit_opd (struct bfd_link_info *info)
7355 {
7356 bfd *ibfd;
7357 bool some_edited = false;
7358 asection *need_pad = NULL;
7359 struct ppc_link_hash_table *htab;
7360
7361 htab = ppc_hash_table (info);
7362 if (htab == NULL)
7363 return false;
7364
7365 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7366 {
7367 asection *sec;
7368 Elf_Internal_Rela *relstart, *rel, *relend;
7369 Elf_Internal_Shdr *symtab_hdr;
7370 Elf_Internal_Sym *local_syms;
7371 struct _opd_sec_data *opd;
7372 bool need_edit, add_aux_fields, broken;
7373 bfd_size_type cnt_16b = 0;
7374
7375 if (!is_ppc64_elf (ibfd))
7376 continue;
7377
7378 sec = bfd_get_section_by_name (ibfd, ".opd");
7379 if (sec == NULL || sec->size == 0)
7380 continue;
7381
7382 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7383 continue;
7384
7385 if (sec->output_section == bfd_abs_section_ptr)
7386 continue;
7387
7388 /* Look through the section relocs. */
7389 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7390 continue;
7391
7392 local_syms = NULL;
7393 symtab_hdr = &elf_symtab_hdr (ibfd);
7394
7395 /* Read the relocations. */
7396 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7397 info->keep_memory);
7398 if (relstart == NULL)
7399 return false;
7400
7401 /* First run through the relocs to check they are sane, and to
7402 determine whether we need to edit this opd section. */
7403 need_edit = false;
7404 broken = false;
7405 need_pad = sec;
7406 relend = relstart + sec->reloc_count;
7407 for (rel = relstart; rel < relend; )
7408 {
7409 enum elf_ppc64_reloc_type r_type;
7410 unsigned long r_symndx;
7411 asection *sym_sec;
7412 struct elf_link_hash_entry *h;
7413 Elf_Internal_Sym *sym;
7414 bfd_vma offset;
7415
7416 /* .opd contains an array of 16 or 24 byte entries. We're
7417 only interested in the reloc pointing to a function entry
7418 point. */
7419 offset = rel->r_offset;
7420 if (rel + 1 == relend
7421 || rel[1].r_offset != offset + 8)
7422 {
7423 /* If someone messes with .opd alignment then after a
7424 "ld -r" we might have padding in the middle of .opd.
7425 Also, there's nothing to prevent someone putting
7426 something silly in .opd with the assembler. No .opd
7427 optimization for them! */
7428 broken_opd:
7429 _bfd_error_handler
7430 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7431 broken = true;
7432 break;
7433 }
7434
7435 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7436 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7437 {
7438 _bfd_error_handler
7439 /* xgettext:c-format */
7440 (_("%pB: unexpected reloc type %u in .opd section"),
7441 ibfd, r_type);
7442 broken = true;
7443 break;
7444 }
7445
7446 r_symndx = ELF64_R_SYM (rel->r_info);
7447 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7448 r_symndx, ibfd))
7449 goto error_ret;
7450
7451 if (sym_sec == NULL || sym_sec->owner == NULL)
7452 {
7453 const char *sym_name;
7454 if (h != NULL)
7455 sym_name = h->root.root.string;
7456 else
7457 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7458 sym_sec);
7459
7460 _bfd_error_handler
7461 /* xgettext:c-format */
7462 (_("%pB: undefined sym `%s' in .opd section"),
7463 ibfd, sym_name);
7464 broken = true;
7465 break;
7466 }
7467
7468 /* opd entries are always for functions defined in the
7469 current input bfd. If the symbol isn't defined in the
7470 input bfd, then we won't be using the function in this
7471 bfd; It must be defined in a linkonce section in another
7472 bfd, or is weak. It's also possible that we are
7473 discarding the function due to a linker script /DISCARD/,
7474 which we test for via the output_section. */
7475 if (sym_sec->owner != ibfd
7476 || sym_sec->output_section == bfd_abs_section_ptr)
7477 need_edit = true;
7478
7479 rel += 2;
7480 if (rel + 1 == relend
7481 || (rel + 2 < relend
7482 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7483 ++rel;
7484
7485 if (rel == relend)
7486 {
7487 if (sec->size == offset + 24)
7488 {
7489 need_pad = NULL;
7490 break;
7491 }
7492 if (sec->size == offset + 16)
7493 {
7494 cnt_16b++;
7495 break;
7496 }
7497 goto broken_opd;
7498 }
7499 else if (rel + 1 < relend
7500 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7501 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7502 {
7503 if (rel[0].r_offset == offset + 16)
7504 cnt_16b++;
7505 else if (rel[0].r_offset != offset + 24)
7506 goto broken_opd;
7507 }
7508 else
7509 goto broken_opd;
7510 }
7511
7512 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7513
7514 if (!broken && (need_edit || add_aux_fields))
7515 {
7516 Elf_Internal_Rela *write_rel;
7517 Elf_Internal_Shdr *rel_hdr;
7518 bfd_byte *rptr, *wptr;
7519 bfd_byte *new_contents;
7520 bfd_size_type amt;
7521
7522 new_contents = NULL;
7523 amt = OPD_NDX (sec->size) * sizeof (long);
7524 opd = &ppc64_elf_section_data (sec)->u.opd;
7525 opd->adjust = bfd_zalloc (sec->owner, amt);
7526 if (opd->adjust == NULL)
7527 return false;
7528
7529 /* This seems a waste of time as input .opd sections are all
7530 zeros as generated by gcc, but I suppose there's no reason
7531 this will always be so. We might start putting something in
7532 the third word of .opd entries. */
7533 if ((sec->flags & SEC_IN_MEMORY) == 0)
7534 {
7535 bfd_byte *loc;
7536 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7537 {
7538 free (loc);
7539 error_ret:
7540 if (symtab_hdr->contents != (unsigned char *) local_syms)
7541 free (local_syms);
7542 if (elf_section_data (sec)->relocs != relstart)
7543 free (relstart);
7544 return false;
7545 }
7546 sec->contents = loc;
7547 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7548 }
7549
7550 elf_section_data (sec)->relocs = relstart;
7551
7552 new_contents = sec->contents;
7553 if (add_aux_fields)
7554 {
7555 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7556 if (new_contents == NULL)
7557 return false;
7558 need_pad = NULL;
7559 }
7560 wptr = new_contents;
7561 rptr = sec->contents;
7562 write_rel = relstart;
7563 for (rel = relstart; rel < relend; )
7564 {
7565 unsigned long r_symndx;
7566 asection *sym_sec;
7567 struct elf_link_hash_entry *h;
7568 struct ppc_link_hash_entry *fdh = NULL;
7569 Elf_Internal_Sym *sym;
7570 long opd_ent_size;
7571 Elf_Internal_Rela *next_rel;
7572 bool skip;
7573
7574 r_symndx = ELF64_R_SYM (rel->r_info);
7575 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7576 r_symndx, ibfd))
7577 goto error_ret;
7578
7579 next_rel = rel + 2;
7580 if (next_rel + 1 == relend
7581 || (next_rel + 2 < relend
7582 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7583 ++next_rel;
7584
7585 /* See if the .opd entry is full 24 byte or
7586 16 byte (with fd_aux entry overlapped with next
7587 fd_func). */
7588 opd_ent_size = 24;
7589 if (next_rel == relend)
7590 {
7591 if (sec->size == rel->r_offset + 16)
7592 opd_ent_size = 16;
7593 }
7594 else if (next_rel->r_offset == rel->r_offset + 16)
7595 opd_ent_size = 16;
7596
7597 if (h != NULL
7598 && h->root.root.string[0] == '.')
7599 {
7600 fdh = ppc_elf_hash_entry (h)->oh;
7601 if (fdh != NULL)
7602 {
7603 fdh = ppc_follow_link (fdh);
7604 if (fdh->elf.root.type != bfd_link_hash_defined
7605 && fdh->elf.root.type != bfd_link_hash_defweak)
7606 fdh = NULL;
7607 }
7608 }
7609
7610 skip = (sym_sec->owner != ibfd
7611 || sym_sec->output_section == bfd_abs_section_ptr);
7612 if (skip)
7613 {
7614 if (fdh != NULL && sym_sec->owner == ibfd)
7615 {
7616 /* Arrange for the function descriptor sym
7617 to be dropped. */
7618 fdh->elf.root.u.def.value = 0;
7619 fdh->elf.root.u.def.section = sym_sec;
7620 }
7621 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7622
7623 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7624 rel = next_rel;
7625 else
7626 while (1)
7627 {
7628 if (!dec_dynrel_count (rel, sec, info,
7629 NULL, h, sym))
7630 goto error_ret;
7631
7632 if (++rel == next_rel)
7633 break;
7634
7635 r_symndx = ELF64_R_SYM (rel->r_info);
7636 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7637 r_symndx, ibfd))
7638 goto error_ret;
7639 }
7640 }
7641 else
7642 {
7643 /* We'll be keeping this opd entry. */
7644 long adjust;
7645
7646 if (fdh != NULL)
7647 {
7648 /* Redefine the function descriptor symbol to
7649 this location in the opd section. It is
7650 necessary to update the value here rather
7651 than using an array of adjustments as we do
7652 for local symbols, because various places
7653 in the generic ELF code use the value
7654 stored in u.def.value. */
7655 fdh->elf.root.u.def.value = wptr - new_contents;
7656 fdh->adjust_done = 1;
7657 }
7658
7659 /* Local syms are a bit tricky. We could
7660 tweak them as they can be cached, but
7661 we'd need to look through the local syms
7662 for the function descriptor sym which we
7663 don't have at the moment. So keep an
7664 array of adjustments. */
7665 adjust = (wptr - new_contents) - (rptr - sec->contents);
7666 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7667
7668 if (wptr != rptr)
7669 memcpy (wptr, rptr, opd_ent_size);
7670 wptr += opd_ent_size;
7671 if (add_aux_fields && opd_ent_size == 16)
7672 {
7673 memset (wptr, '\0', 8);
7674 wptr += 8;
7675 }
7676
7677 /* We need to adjust any reloc offsets to point to the
7678 new opd entries. */
7679 for ( ; rel != next_rel; ++rel)
7680 {
7681 rel->r_offset += adjust;
7682 if (write_rel != rel)
7683 memcpy (write_rel, rel, sizeof (*rel));
7684 ++write_rel;
7685 }
7686 }
7687
7688 rptr += opd_ent_size;
7689 }
7690
7691 sec->size = wptr - new_contents;
7692 sec->reloc_count = write_rel - relstart;
7693 if (add_aux_fields)
7694 {
7695 free (sec->contents);
7696 sec->contents = new_contents;
7697 }
7698
7699 /* Fudge the header size too, as this is used later in
7700 elf_bfd_final_link if we are emitting relocs. */
7701 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7702 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7703 some_edited = true;
7704 }
7705 else if (elf_section_data (sec)->relocs != relstart)
7706 free (relstart);
7707
7708 if (local_syms != NULL
7709 && symtab_hdr->contents != (unsigned char *) local_syms)
7710 {
7711 if (!info->keep_memory)
7712 free (local_syms);
7713 else
7714 symtab_hdr->contents = (unsigned char *) local_syms;
7715 }
7716 }
7717
7718 if (some_edited)
7719 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7720
7721 /* If we are doing a final link and the last .opd entry is just 16 byte
7722 long, add a 8 byte padding after it. */
7723 if (need_pad != NULL && !bfd_link_relocatable (info))
7724 {
7725 bfd_byte *p;
7726
7727 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7728 {
7729 BFD_ASSERT (need_pad->size > 0);
7730
7731 p = bfd_malloc (need_pad->size + 8);
7732 if (p == NULL)
7733 return false;
7734
7735 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7736 p, 0, need_pad->size))
7737 return false;
7738
7739 need_pad->contents = p;
7740 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7741 }
7742 else
7743 {
7744 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7745 if (p == NULL)
7746 return false;
7747
7748 need_pad->contents = p;
7749 }
7750
7751 memset (need_pad->contents + need_pad->size, 0, 8);
7752 need_pad->size += 8;
7753 }
7754
7755 return true;
7756 }
7757
7758 /* Analyze inline PLT call relocations to see whether calls to locally
7759 defined functions can be converted to direct calls. */
7760
7761 bool
7762 ppc64_elf_inline_plt (struct bfd_link_info *info)
7763 {
7764 struct ppc_link_hash_table *htab;
7765 bfd *ibfd;
7766 asection *sec;
7767 bfd_vma low_vma, high_vma, limit;
7768
7769 htab = ppc_hash_table (info);
7770 if (htab == NULL)
7771 return false;
7772
7773 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7774 reduced somewhat to cater for possible stubs that might be added
7775 between the call and its destination. */
7776 if (htab->params->group_size < 0)
7777 {
7778 limit = -htab->params->group_size;
7779 if (limit == 1)
7780 limit = 0x1e00000;
7781 }
7782 else
7783 {
7784 limit = htab->params->group_size;
7785 if (limit == 1)
7786 limit = 0x1c00000;
7787 }
7788
7789 low_vma = -1;
7790 high_vma = 0;
7791 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7792 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7793 {
7794 if (low_vma > sec->vma)
7795 low_vma = sec->vma;
7796 if (high_vma < sec->vma + sec->size)
7797 high_vma = sec->vma + sec->size;
7798 }
7799
7800 /* If a "bl" can reach anywhere in local code sections, then we can
7801 convert all inline PLT sequences to direct calls when the symbol
7802 is local. */
7803 if (high_vma - low_vma < limit)
7804 {
7805 htab->can_convert_all_inline_plt = 1;
7806 return true;
7807 }
7808
7809 /* Otherwise, go looking through relocs for cases where a direct
7810 call won't reach. Mark the symbol on any such reloc to disable
7811 the optimization and keep the PLT entry as it seems likely that
7812 this will be better than creating trampolines. Note that this
7813 will disable the optimization for all inline PLT calls to a
7814 particular symbol, not just those that won't reach. The
7815 difficulty in doing a more precise optimization is that the
7816 linker needs to make a decision depending on whether a
7817 particular R_PPC64_PLTCALL insn can be turned into a direct
7818 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7819 the sequence, and there is nothing that ties those relocs
7820 together except their symbol. */
7821
7822 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7823 {
7824 Elf_Internal_Shdr *symtab_hdr;
7825 Elf_Internal_Sym *local_syms;
7826
7827 if (!is_ppc64_elf (ibfd))
7828 continue;
7829
7830 local_syms = NULL;
7831 symtab_hdr = &elf_symtab_hdr (ibfd);
7832
7833 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7834 if (ppc64_elf_section_data (sec)->has_pltcall
7835 && !bfd_is_abs_section (sec->output_section))
7836 {
7837 Elf_Internal_Rela *relstart, *rel, *relend;
7838
7839 /* Read the relocations. */
7840 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7841 info->keep_memory);
7842 if (relstart == NULL)
7843 return false;
7844
7845 relend = relstart + sec->reloc_count;
7846 for (rel = relstart; rel < relend; rel++)
7847 {
7848 enum elf_ppc64_reloc_type r_type;
7849 unsigned long r_symndx;
7850 asection *sym_sec;
7851 struct elf_link_hash_entry *h;
7852 Elf_Internal_Sym *sym;
7853 unsigned char *tls_maskp;
7854
7855 r_type = ELF64_R_TYPE (rel->r_info);
7856 if (r_type != R_PPC64_PLTCALL
7857 && r_type != R_PPC64_PLTCALL_NOTOC)
7858 continue;
7859
7860 r_symndx = ELF64_R_SYM (rel->r_info);
7861 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7862 r_symndx, ibfd))
7863 {
7864 if (elf_section_data (sec)->relocs != relstart)
7865 free (relstart);
7866 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7867 free (local_syms);
7868 return false;
7869 }
7870
7871 if (sym_sec != NULL && sym_sec->output_section != NULL)
7872 {
7873 bfd_vma from, to;
7874 if (h != NULL)
7875 to = h->root.u.def.value;
7876 else
7877 to = sym->st_value;
7878 to += (rel->r_addend
7879 + sym_sec->output_offset
7880 + sym_sec->output_section->vma);
7881 from = (rel->r_offset
7882 + sec->output_offset
7883 + sec->output_section->vma);
7884 if (to - from + limit < 2 * limit
7885 && !(r_type == R_PPC64_PLTCALL_NOTOC
7886 && (((h ? h->other : sym->st_other)
7887 & STO_PPC64_LOCAL_MASK)
7888 > 1 << STO_PPC64_LOCAL_BIT)))
7889 *tls_maskp &= ~PLT_KEEP;
7890 }
7891 }
7892 if (elf_section_data (sec)->relocs != relstart)
7893 free (relstart);
7894 }
7895
7896 if (local_syms != NULL
7897 && symtab_hdr->contents != (unsigned char *) local_syms)
7898 {
7899 if (!info->keep_memory)
7900 free (local_syms);
7901 else
7902 symtab_hdr->contents = (unsigned char *) local_syms;
7903 }
7904 }
7905
7906 return true;
7907 }
7908
7909 /* Set htab->tls_get_addr and various other info specific to TLS.
7910 This needs to run before dynamic symbols are processed in
7911 bfd_elf_size_dynamic_sections. */
7912
7913 bool
7914 ppc64_elf_tls_setup (struct bfd_link_info *info)
7915 {
7916 struct ppc_link_hash_table *htab;
7917 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7918
7919 htab = ppc_hash_table (info);
7920 if (htab == NULL)
7921 return false;
7922
7923 /* Move dynamic linking info to the function descriptor sym. */
7924 if (htab->need_func_desc_adj)
7925 {
7926 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7927 htab->need_func_desc_adj = 0;
7928 }
7929
7930 if (abiversion (info->output_bfd) == 1)
7931 htab->opd_abi = 1;
7932
7933 if (htab->params->no_multi_toc)
7934 htab->do_multi_toc = 0;
7935 else if (!htab->do_multi_toc)
7936 htab->params->no_multi_toc = 1;
7937
7938 /* Default to --no-plt-localentry, as this option can cause problems
7939 with symbol interposition. For example, glibc libpthread.so and
7940 libc.so duplicate many pthread symbols, with a fallback
7941 implementation in libc.so. In some cases the fallback does more
7942 work than the pthread implementation. __pthread_condattr_destroy
7943 is one such symbol: the libpthread.so implementation is
7944 localentry:0 while the libc.so implementation is localentry:8.
7945 An app that "cleverly" uses dlopen to only load necessary
7946 libraries at runtime may omit loading libpthread.so when not
7947 running multi-threaded, which then results in the libc.so
7948 fallback symbols being used and ld.so complaining. Now there
7949 are workarounds in ld (see non_zero_localentry) to detect the
7950 pthread situation, but that may not be the only case where
7951 --plt-localentry can cause trouble. */
7952 if (htab->params->plt_localentry0 < 0)
7953 htab->params->plt_localentry0 = 0;
7954 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7955 {
7956 /* The issue is that __glink_PLTresolve saves r2, which is done
7957 because glibc ld.so _dl_runtime_resolve restores r2 to support
7958 a glibc plt call optimisation where global entry code is
7959 skipped on calls that resolve to the same binary. The
7960 __glink_PLTresolve save of r2 is incompatible with code
7961 making tail calls, because the tail call might go via the
7962 resolver and thus overwrite the proper saved r2. */
7963 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7964 "power10 pc-relative code"));
7965 htab->params->plt_localentry0 = 0;
7966 }
7967 if (htab->params->plt_localentry0
7968 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7969 false, false, false) == NULL)
7970 _bfd_error_handler
7971 (_("warning: --plt-localentry is especially dangerous without "
7972 "ld.so support to detect ABI violations"));
7973
7974 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7975 false, false, true);
7976 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7977 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7978 false, false, true);
7979 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7980
7981 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7982 false, false, true);
7983 htab->tga_desc = ppc_elf_hash_entry (desc);
7984 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7985 false, false, true);
7986 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7987
7988 if (htab->params->tls_get_addr_opt)
7989 {
7990 struct elf_link_hash_entry *opt, *opt_fd;
7991
7992 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7993 false, false, true);
7994 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7995 false, false, true);
7996 if (opt_fd != NULL
7997 && (opt_fd->root.type == bfd_link_hash_defined
7998 || opt_fd->root.type == bfd_link_hash_defweak))
7999 {
8000 /* If glibc supports an optimized __tls_get_addr call stub,
8001 signalled by the presence of __tls_get_addr_opt, and we'll
8002 be calling __tls_get_addr via a plt call stub, then
8003 make __tls_get_addr point to __tls_get_addr_opt. */
8004 if (!(htab->elf.dynamic_sections_created
8005 && tga_fd != NULL
8006 && (tga_fd->type == STT_FUNC
8007 || tga_fd->needs_plt)
8008 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8009 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8010 tga_fd = NULL;
8011 if (!(htab->elf.dynamic_sections_created
8012 && desc_fd != NULL
8013 && (desc_fd->type == STT_FUNC
8014 || desc_fd->needs_plt)
8015 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8016 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8017 desc_fd = NULL;
8018
8019 if (tga_fd != NULL || desc_fd != NULL)
8020 {
8021 struct plt_entry *ent = NULL;
8022
8023 if (tga_fd != NULL)
8024 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8025 if (ent->plt.refcount > 0)
8026 break;
8027 if (ent == NULL && desc_fd != NULL)
8028 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8029 if (ent->plt.refcount > 0)
8030 break;
8031 if (ent != NULL)
8032 {
8033 if (tga_fd != NULL)
8034 {
8035 tga_fd->root.type = bfd_link_hash_indirect;
8036 tga_fd->root.u.i.link = &opt_fd->root;
8037 tga_fd->root.u.i.warning = NULL;
8038 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8039 }
8040 if (desc_fd != NULL)
8041 {
8042 desc_fd->root.type = bfd_link_hash_indirect;
8043 desc_fd->root.u.i.link = &opt_fd->root;
8044 desc_fd->root.u.i.warning = NULL;
8045 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8046 }
8047 opt_fd->mark = 1;
8048 if (opt_fd->dynindx != -1)
8049 {
8050 /* Use __tls_get_addr_opt in dynamic relocations. */
8051 opt_fd->dynindx = -1;
8052 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8053 opt_fd->dynstr_index);
8054 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8055 return false;
8056 }
8057 if (tga_fd != NULL)
8058 {
8059 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8060 tga = elf_hash_entry (htab->tls_get_addr);
8061 if (opt != NULL && tga != NULL)
8062 {
8063 tga->root.type = bfd_link_hash_indirect;
8064 tga->root.u.i.link = &opt->root;
8065 tga->root.u.i.warning = NULL;
8066 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8067 opt->mark = 1;
8068 _bfd_elf_link_hash_hide_symbol (info, opt,
8069 tga->forced_local);
8070 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8071 }
8072 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8073 htab->tls_get_addr_fd->is_func_descriptor = 1;
8074 if (htab->tls_get_addr != NULL)
8075 {
8076 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8077 htab->tls_get_addr->is_func = 1;
8078 }
8079 }
8080 if (desc_fd != NULL)
8081 {
8082 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8083 if (opt != NULL && desc != NULL)
8084 {
8085 desc->root.type = bfd_link_hash_indirect;
8086 desc->root.u.i.link = &opt->root;
8087 desc->root.u.i.warning = NULL;
8088 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8089 opt->mark = 1;
8090 _bfd_elf_link_hash_hide_symbol (info, opt,
8091 desc->forced_local);
8092 htab->tga_desc = ppc_elf_hash_entry (opt);
8093 }
8094 htab->tga_desc_fd->oh = htab->tga_desc;
8095 htab->tga_desc_fd->is_func_descriptor = 1;
8096 if (htab->tga_desc != NULL)
8097 {
8098 htab->tga_desc->oh = htab->tga_desc_fd;
8099 htab->tga_desc->is_func = 1;
8100 }
8101 }
8102 }
8103 }
8104 }
8105 else if (htab->params->tls_get_addr_opt < 0)
8106 htab->params->tls_get_addr_opt = 0;
8107 }
8108
8109 if (htab->tga_desc_fd != NULL
8110 && htab->params->tls_get_addr_opt
8111 && htab->params->no_tls_get_addr_regsave == -1)
8112 htab->params->no_tls_get_addr_regsave = 0;
8113
8114 return true;
8115 }
8116
8117 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8118 any of HASH1, HASH2, HASH3, or HASH4. */
8119
8120 static bool
8121 branch_reloc_hash_match (bfd *ibfd,
8122 Elf_Internal_Rela *rel,
8123 struct ppc_link_hash_entry *hash1,
8124 struct ppc_link_hash_entry *hash2,
8125 struct ppc_link_hash_entry *hash3,
8126 struct ppc_link_hash_entry *hash4)
8127 {
8128 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8129 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8130 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8131
8132 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8133 {
8134 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8135 struct elf_link_hash_entry *h;
8136
8137 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8138 h = elf_follow_link (h);
8139 if (h == elf_hash_entry (hash1)
8140 || h == elf_hash_entry (hash2)
8141 || h == elf_hash_entry (hash3)
8142 || h == elf_hash_entry (hash4))
8143 return true;
8144 }
8145 return false;
8146 }
8147
8148 /* Run through all the TLS relocs looking for optimization
8149 opportunities. The linker has been hacked (see ppc64elf.em) to do
8150 a preliminary section layout so that we know the TLS segment
8151 offsets. We can't optimize earlier because some optimizations need
8152 to know the tp offset, and we need to optimize before allocating
8153 dynamic relocations. */
8154
8155 bool
8156 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8157 {
8158 bfd *ibfd;
8159 asection *sec;
8160 struct ppc_link_hash_table *htab;
8161 unsigned char *toc_ref;
8162 int pass;
8163
8164 if (!bfd_link_executable (info))
8165 return true;
8166
8167 htab = ppc_hash_table (info);
8168 if (htab == NULL)
8169 return false;
8170
8171 htab->do_tls_opt = 1;
8172
8173 /* Make two passes over the relocs. On the first pass, mark toc
8174 entries involved with tls relocs, and check that tls relocs
8175 involved in setting up a tls_get_addr call are indeed followed by
8176 such a call. If they are not, we can't do any tls optimization.
8177 On the second pass twiddle tls_mask flags to notify
8178 relocate_section that optimization can be done, and adjust got
8179 and plt refcounts. */
8180 toc_ref = NULL;
8181 for (pass = 0; pass < 2; ++pass)
8182 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8183 {
8184 Elf_Internal_Sym *locsyms = NULL;
8185 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8186
8187 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8188 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8189 {
8190 Elf_Internal_Rela *relstart, *rel, *relend;
8191 bool found_tls_get_addr_arg = 0;
8192
8193 /* Read the relocations. */
8194 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8195 info->keep_memory);
8196 if (relstart == NULL)
8197 {
8198 free (toc_ref);
8199 return false;
8200 }
8201
8202 relend = relstart + sec->reloc_count;
8203 for (rel = relstart; rel < relend; rel++)
8204 {
8205 enum elf_ppc64_reloc_type r_type;
8206 unsigned long r_symndx;
8207 struct elf_link_hash_entry *h;
8208 Elf_Internal_Sym *sym;
8209 asection *sym_sec;
8210 unsigned char *tls_mask;
8211 unsigned int tls_set, tls_clear, tls_type = 0;
8212 bfd_vma value;
8213 bool ok_tprel, is_local;
8214 long toc_ref_index = 0;
8215 int expecting_tls_get_addr = 0;
8216 bool ret = false;
8217
8218 r_symndx = ELF64_R_SYM (rel->r_info);
8219 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8220 r_symndx, ibfd))
8221 {
8222 err_free_rel:
8223 if (elf_section_data (sec)->relocs != relstart)
8224 free (relstart);
8225 free (toc_ref);
8226 if (elf_symtab_hdr (ibfd).contents
8227 != (unsigned char *) locsyms)
8228 free (locsyms);
8229 return ret;
8230 }
8231
8232 if (h != NULL)
8233 {
8234 if (h->root.type == bfd_link_hash_defined
8235 || h->root.type == bfd_link_hash_defweak)
8236 value = h->root.u.def.value;
8237 else if (h->root.type == bfd_link_hash_undefweak)
8238 value = 0;
8239 else
8240 {
8241 found_tls_get_addr_arg = 0;
8242 continue;
8243 }
8244 }
8245 else
8246 /* Symbols referenced by TLS relocs must be of type
8247 STT_TLS. So no need for .opd local sym adjust. */
8248 value = sym->st_value;
8249
8250 ok_tprel = false;
8251 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8252 if (is_local)
8253 {
8254 if (h != NULL
8255 && h->root.type == bfd_link_hash_undefweak)
8256 ok_tprel = true;
8257 else if (sym_sec != NULL
8258 && sym_sec->output_section != NULL)
8259 {
8260 value += sym_sec->output_offset;
8261 value += sym_sec->output_section->vma;
8262 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8263 /* Note that even though the prefix insns
8264 allow a 1<<33 offset we use the same test
8265 as for addis;addi. There may be a mix of
8266 pcrel and non-pcrel code and the decision
8267 to optimise is per symbol, not per TLS
8268 sequence. */
8269 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8270 }
8271 }
8272
8273 r_type = ELF64_R_TYPE (rel->r_info);
8274 /* If this section has old-style __tls_get_addr calls
8275 without marker relocs, then check that each
8276 __tls_get_addr call reloc is preceded by a reloc
8277 that conceivably belongs to the __tls_get_addr arg
8278 setup insn. If we don't find matching arg setup
8279 relocs, don't do any tls optimization. */
8280 if (pass == 0
8281 && sec->nomark_tls_get_addr
8282 && h != NULL
8283 && is_tls_get_addr (h, htab)
8284 && !found_tls_get_addr_arg
8285 && is_branch_reloc (r_type))
8286 {
8287 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8288 "TLS optimization disabled\n"),
8289 ibfd, sec, rel->r_offset);
8290 ret = true;
8291 goto err_free_rel;
8292 }
8293
8294 found_tls_get_addr_arg = 0;
8295 switch (r_type)
8296 {
8297 case R_PPC64_GOT_TLSLD16:
8298 case R_PPC64_GOT_TLSLD16_LO:
8299 case R_PPC64_GOT_TLSLD_PCREL34:
8300 expecting_tls_get_addr = 1;
8301 found_tls_get_addr_arg = 1;
8302 /* Fall through. */
8303
8304 case R_PPC64_GOT_TLSLD16_HI:
8305 case R_PPC64_GOT_TLSLD16_HA:
8306 /* These relocs should never be against a symbol
8307 defined in a shared lib. Leave them alone if
8308 that turns out to be the case. */
8309 if (!is_local)
8310 continue;
8311
8312 /* LD -> LE */
8313 tls_set = 0;
8314 tls_clear = TLS_LD;
8315 tls_type = TLS_TLS | TLS_LD;
8316 break;
8317
8318 case R_PPC64_GOT_TLSGD16:
8319 case R_PPC64_GOT_TLSGD16_LO:
8320 case R_PPC64_GOT_TLSGD_PCREL34:
8321 expecting_tls_get_addr = 1;
8322 found_tls_get_addr_arg = 1;
8323 /* Fall through. */
8324
8325 case R_PPC64_GOT_TLSGD16_HI:
8326 case R_PPC64_GOT_TLSGD16_HA:
8327 if (ok_tprel)
8328 /* GD -> LE */
8329 tls_set = 0;
8330 else
8331 /* GD -> IE */
8332 tls_set = TLS_TLS | TLS_GDIE;
8333 tls_clear = TLS_GD;
8334 tls_type = TLS_TLS | TLS_GD;
8335 break;
8336
8337 case R_PPC64_GOT_TPREL_PCREL34:
8338 case R_PPC64_GOT_TPREL16_DS:
8339 case R_PPC64_GOT_TPREL16_LO_DS:
8340 case R_PPC64_GOT_TPREL16_HI:
8341 case R_PPC64_GOT_TPREL16_HA:
8342 if (ok_tprel)
8343 {
8344 /* IE -> LE */
8345 tls_set = 0;
8346 tls_clear = TLS_TPREL;
8347 tls_type = TLS_TLS | TLS_TPREL;
8348 break;
8349 }
8350 continue;
8351
8352 case R_PPC64_TLSLD:
8353 if (!is_local)
8354 continue;
8355 /* Fall through. */
8356 case R_PPC64_TLSGD:
8357 if (rel + 1 < relend
8358 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8359 {
8360 if (pass != 0
8361 && (ELF64_R_TYPE (rel[1].r_info)
8362 != R_PPC64_PLTSEQ)
8363 && (ELF64_R_TYPE (rel[1].r_info)
8364 != R_PPC64_PLTSEQ_NOTOC))
8365 {
8366 r_symndx = ELF64_R_SYM (rel[1].r_info);
8367 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8368 r_symndx, ibfd))
8369 goto err_free_rel;
8370 if (h != NULL)
8371 {
8372 struct plt_entry *ent = NULL;
8373
8374 for (ent = h->plt.plist;
8375 ent != NULL;
8376 ent = ent->next)
8377 if (ent->addend == rel[1].r_addend)
8378 break;
8379
8380 if (ent != NULL
8381 && ent->plt.refcount > 0)
8382 ent->plt.refcount -= 1;
8383 }
8384 }
8385 continue;
8386 }
8387 found_tls_get_addr_arg = 1;
8388 /* Fall through. */
8389
8390 case R_PPC64_TLS:
8391 case R_PPC64_TOC16:
8392 case R_PPC64_TOC16_LO:
8393 if (sym_sec == NULL || sym_sec != toc)
8394 continue;
8395
8396 /* Mark this toc entry as referenced by a TLS
8397 code sequence. We can do that now in the
8398 case of R_PPC64_TLS, and after checking for
8399 tls_get_addr for the TOC16 relocs. */
8400 if (toc_ref == NULL)
8401 toc_ref
8402 = bfd_zmalloc (toc->output_section->rawsize / 8);
8403 if (toc_ref == NULL)
8404 goto err_free_rel;
8405
8406 if (h != NULL)
8407 value = h->root.u.def.value;
8408 else
8409 value = sym->st_value;
8410 value += rel->r_addend;
8411 if (value % 8 != 0)
8412 continue;
8413 BFD_ASSERT (value < toc->size
8414 && toc->output_offset % 8 == 0);
8415 toc_ref_index = (value + toc->output_offset) / 8;
8416 if (r_type == R_PPC64_TLS
8417 || r_type == R_PPC64_TLSGD
8418 || r_type == R_PPC64_TLSLD)
8419 {
8420 toc_ref[toc_ref_index] = 1;
8421 continue;
8422 }
8423
8424 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8425 continue;
8426
8427 tls_set = 0;
8428 tls_clear = 0;
8429 expecting_tls_get_addr = 2;
8430 break;
8431
8432 case R_PPC64_TPREL64:
8433 if (pass == 0
8434 || sec != toc
8435 || toc_ref == NULL
8436 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8437 continue;
8438 if (ok_tprel)
8439 {
8440 /* IE -> LE */
8441 tls_set = TLS_EXPLICIT;
8442 tls_clear = TLS_TPREL;
8443 break;
8444 }
8445 continue;
8446
8447 case R_PPC64_DTPMOD64:
8448 if (pass == 0
8449 || sec != toc
8450 || toc_ref == NULL
8451 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8452 continue;
8453 if (rel + 1 < relend
8454 && (rel[1].r_info
8455 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8456 && rel[1].r_offset == rel->r_offset + 8)
8457 {
8458 if (ok_tprel)
8459 /* GD -> LE */
8460 tls_set = TLS_EXPLICIT | TLS_GD;
8461 else
8462 /* GD -> IE */
8463 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8464 tls_clear = TLS_GD;
8465 }
8466 else
8467 {
8468 if (!is_local)
8469 continue;
8470
8471 /* LD -> LE */
8472 tls_set = TLS_EXPLICIT;
8473 tls_clear = TLS_LD;
8474 }
8475 break;
8476
8477 case R_PPC64_TPREL16_HA:
8478 if (pass == 0)
8479 {
8480 unsigned char buf[4];
8481 unsigned int insn;
8482 bfd_vma off = rel->r_offset & ~3;
8483 if (!bfd_get_section_contents (ibfd, sec, buf,
8484 off, 4))
8485 goto err_free_rel;
8486 insn = bfd_get_32 (ibfd, buf);
8487 /* addis rt,13,imm */
8488 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8489 != ((15u << 26) | (13 << 16)))
8490 {
8491 /* xgettext:c-format */
8492 info->callbacks->minfo
8493 (_("%H: warning: %s unexpected insn %#x.\n"),
8494 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8495 htab->do_tls_opt = 0;
8496 }
8497 }
8498 continue;
8499
8500 case R_PPC64_TPREL16_HI:
8501 case R_PPC64_TPREL16_HIGH:
8502 case R_PPC64_TPREL16_HIGHA:
8503 case R_PPC64_TPREL16_HIGHER:
8504 case R_PPC64_TPREL16_HIGHERA:
8505 case R_PPC64_TPREL16_HIGHEST:
8506 case R_PPC64_TPREL16_HIGHESTA:
8507 /* These can all be used in sequences along with
8508 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8509 able to verify easily. */
8510 htab->do_tls_opt = 0;
8511 continue;
8512
8513 default:
8514 continue;
8515 }
8516
8517 if (pass == 0)
8518 {
8519 if (!expecting_tls_get_addr
8520 || !sec->nomark_tls_get_addr)
8521 continue;
8522
8523 if (rel + 1 < relend
8524 && branch_reloc_hash_match (ibfd, rel + 1,
8525 htab->tls_get_addr_fd,
8526 htab->tga_desc_fd,
8527 htab->tls_get_addr,
8528 htab->tga_desc))
8529 {
8530 if (expecting_tls_get_addr == 2)
8531 {
8532 /* Check for toc tls entries. */
8533 unsigned char *toc_tls;
8534 int retval;
8535
8536 retval = get_tls_mask (&toc_tls, NULL, NULL,
8537 &locsyms,
8538 rel, ibfd);
8539 if (retval == 0)
8540 goto err_free_rel;
8541 if (toc_tls != NULL)
8542 {
8543 if ((*toc_tls & TLS_TLS) != 0
8544 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8545 found_tls_get_addr_arg = 1;
8546 if (retval > 1)
8547 toc_ref[toc_ref_index] = 1;
8548 }
8549 }
8550 continue;
8551 }
8552
8553 /* Uh oh, we didn't find the expected call. We
8554 could just mark this symbol to exclude it
8555 from tls optimization but it's safer to skip
8556 the entire optimization. */
8557 /* xgettext:c-format */
8558 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8559 "TLS optimization disabled\n"),
8560 ibfd, sec, rel->r_offset);
8561 ret = true;
8562 goto err_free_rel;
8563 }
8564
8565 /* If we don't have old-style __tls_get_addr calls
8566 without TLSGD/TLSLD marker relocs, and we haven't
8567 found a new-style __tls_get_addr call with a
8568 marker for this symbol, then we either have a
8569 broken object file or an -mlongcall style
8570 indirect call to __tls_get_addr without a marker.
8571 Disable optimization in this case. */
8572 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8573 && (tls_set & TLS_EXPLICIT) == 0
8574 && !sec->nomark_tls_get_addr
8575 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8576 != (TLS_TLS | TLS_MARK)))
8577 continue;
8578
8579 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8580 {
8581 struct plt_entry *ent = NULL;
8582
8583 if (htab->tls_get_addr_fd != NULL)
8584 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8585 ent != NULL;
8586 ent = ent->next)
8587 if (ent->addend == 0)
8588 break;
8589
8590 if (ent == NULL && htab->tga_desc_fd != NULL)
8591 for (ent = htab->tga_desc_fd->elf.plt.plist;
8592 ent != NULL;
8593 ent = ent->next)
8594 if (ent->addend == 0)
8595 break;
8596
8597 if (ent == NULL && htab->tls_get_addr != NULL)
8598 for (ent = htab->tls_get_addr->elf.plt.plist;
8599 ent != NULL;
8600 ent = ent->next)
8601 if (ent->addend == 0)
8602 break;
8603
8604 if (ent == NULL && htab->tga_desc != NULL)
8605 for (ent = htab->tga_desc->elf.plt.plist;
8606 ent != NULL;
8607 ent = ent->next)
8608 if (ent->addend == 0)
8609 break;
8610
8611 if (ent != NULL
8612 && ent->plt.refcount > 0)
8613 ent->plt.refcount -= 1;
8614 }
8615
8616 if (tls_clear == 0)
8617 continue;
8618
8619 if ((tls_set & TLS_EXPLICIT) == 0)
8620 {
8621 struct got_entry *ent;
8622
8623 /* Adjust got entry for this reloc. */
8624 if (h != NULL)
8625 ent = h->got.glist;
8626 else
8627 ent = elf_local_got_ents (ibfd)[r_symndx];
8628
8629 for (; ent != NULL; ent = ent->next)
8630 if (ent->addend == rel->r_addend
8631 && ent->owner == ibfd
8632 && ent->tls_type == tls_type)
8633 break;
8634 if (ent == NULL)
8635 abort ();
8636
8637 if (tls_set == 0)
8638 {
8639 /* We managed to get rid of a got entry. */
8640 if (ent->got.refcount > 0)
8641 ent->got.refcount -= 1;
8642 }
8643 }
8644 else
8645 {
8646 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8647 we'll lose one or two dyn relocs. */
8648 if (!dec_dynrel_count (rel, sec, info,
8649 NULL, h, sym))
8650 return false;
8651
8652 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8653 {
8654 if (!dec_dynrel_count (rel + 1, sec, info,
8655 NULL, h, sym))
8656 return false;
8657 }
8658 }
8659
8660 *tls_mask |= tls_set & 0xff;
8661 *tls_mask &= ~tls_clear;
8662 }
8663
8664 if (elf_section_data (sec)->relocs != relstart)
8665 free (relstart);
8666 }
8667
8668 if (locsyms != NULL
8669 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8670 {
8671 if (!info->keep_memory)
8672 free (locsyms);
8673 else
8674 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8675 }
8676 }
8677
8678 free (toc_ref);
8679 return true;
8680 }
8681
8682 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8683 the values of any global symbols in a toc section that has been
8684 edited. Globals in toc sections should be a rarity, so this function
8685 sets a flag if any are found in toc sections other than the one just
8686 edited, so that further hash table traversals can be avoided. */
8687
8688 struct adjust_toc_info
8689 {
8690 asection *toc;
8691 unsigned long *skip;
8692 bool global_toc_syms;
8693 };
8694
8695 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8696
8697 static bool
8698 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8699 {
8700 struct ppc_link_hash_entry *eh;
8701 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8702 unsigned long i;
8703
8704 if (h->root.type != bfd_link_hash_defined
8705 && h->root.type != bfd_link_hash_defweak)
8706 return true;
8707
8708 eh = ppc_elf_hash_entry (h);
8709 if (eh->adjust_done)
8710 return true;
8711
8712 if (eh->elf.root.u.def.section == toc_inf->toc)
8713 {
8714 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8715 i = toc_inf->toc->rawsize >> 3;
8716 else
8717 i = eh->elf.root.u.def.value >> 3;
8718
8719 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8720 {
8721 _bfd_error_handler
8722 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8723 do
8724 ++i;
8725 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8726 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8727 }
8728
8729 eh->elf.root.u.def.value -= toc_inf->skip[i];
8730 eh->adjust_done = 1;
8731 }
8732 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8733 toc_inf->global_toc_syms = true;
8734
8735 return true;
8736 }
8737
8738 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8739 on a _LO variety toc/got reloc. */
8740
8741 static bool
8742 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8743 {
8744 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8745 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8746 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8747 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8748 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8749 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8750 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8751 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8752 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8753 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8754 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8755 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8756 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8757 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8758 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8759 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8760 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8761 /* Exclude lfqu by testing reloc. If relocs are ever
8762 defined for the reduced D field in psq_lu then those
8763 will need testing too. */
8764 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8765 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8766 && (insn & 1) == 0)
8767 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8768 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8769 /* Exclude stfqu. psq_stu as above for psq_lu. */
8770 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8771 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8772 && (insn & 1) == 0));
8773 }
8774
8775 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8776 pld ra,symbol@got@pcrel
8777 load/store rt,off(ra)
8778 or
8779 pla ra,symbol@pcrel
8780 load/store rt,off(ra)
8781 may be translated to
8782 pload/pstore rt,symbol+off@pcrel
8783 nop.
8784 This function returns true if the optimization is possible, placing
8785 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8786
8787 On entry to this function, the linker has already determined that
8788 the pld can be replaced with pla: *PINSN1 is that pla insn,
8789 while *PINSN2 is the second instruction. */
8790
8791 static bool
8792 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8793 {
8794 uint64_t insn1 = *pinsn1;
8795 uint64_t insn2 = *pinsn2;
8796 bfd_signed_vma off;
8797
8798 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8799 {
8800 /* Check that regs match. */
8801 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8802 return false;
8803
8804 /* P8LS or PMLS form, non-pcrel. */
8805 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8806 return false;
8807
8808 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8809 *pinsn2 = PNOP;
8810 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8811 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8812 return true;
8813 }
8814
8815 insn2 >>= 32;
8816
8817 /* Check that regs match. */
8818 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8819 return false;
8820
8821 switch ((insn2 >> 26) & 63)
8822 {
8823 default:
8824 return false;
8825
8826 case 32: /* lwz */
8827 case 34: /* lbz */
8828 case 36: /* stw */
8829 case 38: /* stb */
8830 case 40: /* lhz */
8831 case 42: /* lha */
8832 case 44: /* sth */
8833 case 48: /* lfs */
8834 case 50: /* lfd */
8835 case 52: /* stfs */
8836 case 54: /* stfd */
8837 /* These are the PMLS cases, where we just need to tack a prefix
8838 on the insn. */
8839 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8840 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8841 off = insn2 & 0xffff;
8842 break;
8843
8844 case 58: /* lwa, ld */
8845 if ((insn2 & 1) != 0)
8846 return false;
8847 insn1 = ((1ULL << 58) | (1ULL << 52)
8848 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8849 | (insn2 & (31ULL << 21)));
8850 off = insn2 & 0xfffc;
8851 break;
8852
8853 case 57: /* lxsd, lxssp */
8854 if ((insn2 & 3) < 2)
8855 return false;
8856 insn1 = ((1ULL << 58) | (1ULL << 52)
8857 | ((40ULL | (insn2 & 3)) << 26)
8858 | (insn2 & (31ULL << 21)));
8859 off = insn2 & 0xfffc;
8860 break;
8861
8862 case 61: /* stxsd, stxssp, lxv, stxv */
8863 if ((insn2 & 3) == 0)
8864 return false;
8865 else if ((insn2 & 3) >= 2)
8866 {
8867 insn1 = ((1ULL << 58) | (1ULL << 52)
8868 | ((44ULL | (insn2 & 3)) << 26)
8869 | (insn2 & (31ULL << 21)));
8870 off = insn2 & 0xfffc;
8871 }
8872 else
8873 {
8874 insn1 = ((1ULL << 58) | (1ULL << 52)
8875 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8876 | (insn2 & (31ULL << 21)));
8877 off = insn2 & 0xfff0;
8878 }
8879 break;
8880
8881 case 56: /* lq */
8882 insn1 = ((1ULL << 58) | (1ULL << 52)
8883 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8884 off = insn2 & 0xffff;
8885 break;
8886
8887 case 6: /* lxvp, stxvp */
8888 if ((insn2 & 0xe) != 0)
8889 return false;
8890 insn1 = ((1ULL << 58) | (1ULL << 52)
8891 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8892 | (insn2 & (31ULL << 21)));
8893 off = insn2 & 0xfff0;
8894 break;
8895
8896 case 62: /* std, stq */
8897 if ((insn2 & 1) != 0)
8898 return false;
8899 insn1 = ((1ULL << 58) | (1ULL << 52)
8900 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8901 | (insn2 & (31ULL << 21)));
8902 off = insn2 & 0xfffc;
8903 break;
8904 }
8905
8906 *pinsn1 = insn1;
8907 *pinsn2 = (uint64_t) NOP << 32;
8908 *poff = (off ^ 0x8000) - 0x8000;
8909 return true;
8910 }
8911
8912 /* Examine all relocs referencing .toc sections in order to remove
8913 unused .toc entries. */
8914
8915 bool
8916 ppc64_elf_edit_toc (struct bfd_link_info *info)
8917 {
8918 bfd *ibfd;
8919 struct adjust_toc_info toc_inf;
8920 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8921
8922 htab->do_toc_opt = 1;
8923 toc_inf.global_toc_syms = true;
8924 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8925 {
8926 asection *toc, *sec;
8927 Elf_Internal_Shdr *symtab_hdr;
8928 Elf_Internal_Sym *local_syms;
8929 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8930 unsigned long *skip, *drop;
8931 unsigned char *used;
8932 unsigned char *keep, last, some_unused;
8933
8934 if (!is_ppc64_elf (ibfd))
8935 continue;
8936
8937 toc = bfd_get_section_by_name (ibfd, ".toc");
8938 if (toc == NULL
8939 || toc->size == 0
8940 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8941 || discarded_section (toc))
8942 continue;
8943
8944 toc_relocs = NULL;
8945 local_syms = NULL;
8946 symtab_hdr = &elf_symtab_hdr (ibfd);
8947
8948 /* Look at sections dropped from the final link. */
8949 skip = NULL;
8950 relstart = NULL;
8951 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8952 {
8953 if (sec->reloc_count == 0
8954 || !discarded_section (sec)
8955 || get_opd_info (sec)
8956 || (sec->flags & SEC_ALLOC) == 0
8957 || (sec->flags & SEC_DEBUGGING) != 0)
8958 continue;
8959
8960 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8961 if (relstart == NULL)
8962 goto error_ret;
8963
8964 /* Run through the relocs to see which toc entries might be
8965 unused. */
8966 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8967 {
8968 enum elf_ppc64_reloc_type r_type;
8969 unsigned long r_symndx;
8970 asection *sym_sec;
8971 struct elf_link_hash_entry *h;
8972 Elf_Internal_Sym *sym;
8973 bfd_vma val;
8974
8975 r_type = ELF64_R_TYPE (rel->r_info);
8976 switch (r_type)
8977 {
8978 default:
8979 continue;
8980
8981 case R_PPC64_TOC16:
8982 case R_PPC64_TOC16_LO:
8983 case R_PPC64_TOC16_HI:
8984 case R_PPC64_TOC16_HA:
8985 case R_PPC64_TOC16_DS:
8986 case R_PPC64_TOC16_LO_DS:
8987 break;
8988 }
8989
8990 r_symndx = ELF64_R_SYM (rel->r_info);
8991 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8992 r_symndx, ibfd))
8993 goto error_ret;
8994
8995 if (sym_sec != toc)
8996 continue;
8997
8998 if (h != NULL)
8999 val = h->root.u.def.value;
9000 else
9001 val = sym->st_value;
9002 val += rel->r_addend;
9003
9004 if (val >= toc->size)
9005 continue;
9006
9007 /* Anything in the toc ought to be aligned to 8 bytes.
9008 If not, don't mark as unused. */
9009 if (val & 7)
9010 continue;
9011
9012 if (skip == NULL)
9013 {
9014 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9015 if (skip == NULL)
9016 goto error_ret;
9017 }
9018
9019 skip[val >> 3] = ref_from_discarded;
9020 }
9021
9022 if (elf_section_data (sec)->relocs != relstart)
9023 free (relstart);
9024 }
9025
9026 /* For largetoc loads of address constants, we can convert
9027 . addis rx,2,addr@got@ha
9028 . ld ry,addr@got@l(rx)
9029 to
9030 . addis rx,2,addr@toc@ha
9031 . addi ry,rx,addr@toc@l
9032 when addr is within 2G of the toc pointer. This then means
9033 that the word storing "addr" in the toc is no longer needed. */
9034
9035 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9036 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9037 && toc->reloc_count != 0)
9038 {
9039 /* Read toc relocs. */
9040 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9041 info->keep_memory);
9042 if (toc_relocs == NULL)
9043 goto error_ret;
9044
9045 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9046 {
9047 enum elf_ppc64_reloc_type r_type;
9048 unsigned long r_symndx;
9049 asection *sym_sec;
9050 struct elf_link_hash_entry *h;
9051 Elf_Internal_Sym *sym;
9052 bfd_vma val, addr;
9053
9054 r_type = ELF64_R_TYPE (rel->r_info);
9055 if (r_type != R_PPC64_ADDR64)
9056 continue;
9057
9058 r_symndx = ELF64_R_SYM (rel->r_info);
9059 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9060 r_symndx, ibfd))
9061 goto error_ret;
9062
9063 if (sym_sec == NULL
9064 || sym_sec->output_section == NULL
9065 || discarded_section (sym_sec))
9066 continue;
9067
9068 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9069 continue;
9070
9071 if (h != NULL)
9072 {
9073 if (h->type == STT_GNU_IFUNC)
9074 continue;
9075 val = h->root.u.def.value;
9076 }
9077 else
9078 {
9079 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9080 continue;
9081 val = sym->st_value;
9082 }
9083 val += rel->r_addend;
9084 val += sym_sec->output_section->vma + sym_sec->output_offset;
9085
9086 /* We don't yet know the exact toc pointer value, but we
9087 know it will be somewhere in the toc section. Don't
9088 optimize if the difference from any possible toc
9089 pointer is outside [ff..f80008000, 7fff7fff]. */
9090 addr = toc->output_section->vma + TOC_BASE_OFF;
9091 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9092 continue;
9093
9094 addr = toc->output_section->vma + toc->output_section->rawsize;
9095 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9096 continue;
9097
9098 if (skip == NULL)
9099 {
9100 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9101 if (skip == NULL)
9102 goto error_ret;
9103 }
9104
9105 skip[rel->r_offset >> 3]
9106 |= can_optimize | ((rel - toc_relocs) << 2);
9107 }
9108 }
9109
9110 if (skip == NULL)
9111 continue;
9112
9113 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9114 if (used == NULL)
9115 {
9116 error_ret:
9117 if (symtab_hdr->contents != (unsigned char *) local_syms)
9118 free (local_syms);
9119 if (sec != NULL
9120 && elf_section_data (sec)->relocs != relstart)
9121 free (relstart);
9122 if (elf_section_data (toc)->relocs != toc_relocs)
9123 free (toc_relocs);
9124 free (skip);
9125 return false;
9126 }
9127
9128 /* Now check all kept sections that might reference the toc.
9129 Check the toc itself last. */
9130 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9131 : ibfd->sections);
9132 sec != NULL;
9133 sec = (sec == toc ? NULL
9134 : sec->next == NULL ? toc
9135 : sec->next == toc && toc->next ? toc->next
9136 : sec->next))
9137 {
9138 int repeat;
9139
9140 if (sec->reloc_count == 0
9141 || discarded_section (sec)
9142 || get_opd_info (sec)
9143 || (sec->flags & SEC_ALLOC) == 0
9144 || (sec->flags & SEC_DEBUGGING) != 0)
9145 continue;
9146
9147 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9148 info->keep_memory);
9149 if (relstart == NULL)
9150 {
9151 free (used);
9152 goto error_ret;
9153 }
9154
9155 /* Mark toc entries referenced as used. */
9156 do
9157 {
9158 repeat = 0;
9159 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9160 {
9161 enum elf_ppc64_reloc_type r_type;
9162 unsigned long r_symndx;
9163 asection *sym_sec;
9164 struct elf_link_hash_entry *h;
9165 Elf_Internal_Sym *sym;
9166 bfd_vma val;
9167
9168 r_type = ELF64_R_TYPE (rel->r_info);
9169 switch (r_type)
9170 {
9171 case R_PPC64_TOC16:
9172 case R_PPC64_TOC16_LO:
9173 case R_PPC64_TOC16_HI:
9174 case R_PPC64_TOC16_HA:
9175 case R_PPC64_TOC16_DS:
9176 case R_PPC64_TOC16_LO_DS:
9177 /* In case we're taking addresses of toc entries. */
9178 case R_PPC64_ADDR64:
9179 break;
9180
9181 default:
9182 continue;
9183 }
9184
9185 r_symndx = ELF64_R_SYM (rel->r_info);
9186 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9187 r_symndx, ibfd))
9188 {
9189 free (used);
9190 goto error_ret;
9191 }
9192
9193 if (sym_sec != toc)
9194 continue;
9195
9196 if (h != NULL)
9197 val = h->root.u.def.value;
9198 else
9199 val = sym->st_value;
9200 val += rel->r_addend;
9201
9202 if (val >= toc->size)
9203 continue;
9204
9205 if ((skip[val >> 3] & can_optimize) != 0)
9206 {
9207 bfd_vma off;
9208 unsigned char opc;
9209
9210 switch (r_type)
9211 {
9212 case R_PPC64_TOC16_HA:
9213 break;
9214
9215 case R_PPC64_TOC16_LO_DS:
9216 off = rel->r_offset;
9217 off += (bfd_big_endian (ibfd) ? -2 : 3);
9218 if (!bfd_get_section_contents (ibfd, sec, &opc,
9219 off, 1))
9220 {
9221 free (used);
9222 goto error_ret;
9223 }
9224 if ((opc & (0x3f << 2)) == (58u << 2))
9225 break;
9226 /* Fall through. */
9227
9228 default:
9229 /* Wrong sort of reloc, or not a ld. We may
9230 as well clear ref_from_discarded too. */
9231 skip[val >> 3] = 0;
9232 }
9233 }
9234
9235 if (sec != toc)
9236 used[val >> 3] = 1;
9237 /* For the toc section, we only mark as used if this
9238 entry itself isn't unused. */
9239 else if ((used[rel->r_offset >> 3]
9240 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9241 && !used[val >> 3])
9242 {
9243 /* Do all the relocs again, to catch reference
9244 chains. */
9245 repeat = 1;
9246 used[val >> 3] = 1;
9247 }
9248 }
9249 }
9250 while (repeat);
9251
9252 if (elf_section_data (sec)->relocs != relstart)
9253 free (relstart);
9254 }
9255
9256 /* Merge the used and skip arrays. Assume that TOC
9257 doublewords not appearing as either used or unused belong
9258 to an entry more than one doubleword in size. */
9259 for (drop = skip, keep = used, last = 0, some_unused = 0;
9260 drop < skip + (toc->size + 7) / 8;
9261 ++drop, ++keep)
9262 {
9263 if (*keep)
9264 {
9265 *drop &= ~ref_from_discarded;
9266 if ((*drop & can_optimize) != 0)
9267 some_unused = 1;
9268 last = 0;
9269 }
9270 else if ((*drop & ref_from_discarded) != 0)
9271 {
9272 some_unused = 1;
9273 last = ref_from_discarded;
9274 }
9275 else
9276 *drop = last;
9277 }
9278
9279 free (used);
9280
9281 if (some_unused)
9282 {
9283 bfd_byte *contents, *src;
9284 unsigned long off;
9285 Elf_Internal_Sym *sym;
9286 bool local_toc_syms = false;
9287
9288 /* Shuffle the toc contents, and at the same time convert the
9289 skip array from booleans into offsets. */
9290 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9291 goto error_ret;
9292
9293 elf_section_data (toc)->this_hdr.contents = contents;
9294
9295 for (src = contents, off = 0, drop = skip;
9296 src < contents + toc->size;
9297 src += 8, ++drop)
9298 {
9299 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9300 off += 8;
9301 else if (off != 0)
9302 {
9303 *drop = off;
9304 memcpy (src - off, src, 8);
9305 }
9306 }
9307 *drop = off;
9308 toc->rawsize = toc->size;
9309 toc->size = src - contents - off;
9310
9311 /* Adjust addends for relocs against the toc section sym,
9312 and optimize any accesses we can. */
9313 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9314 {
9315 if (sec->reloc_count == 0
9316 || discarded_section (sec))
9317 continue;
9318
9319 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9320 info->keep_memory);
9321 if (relstart == NULL)
9322 goto error_ret;
9323
9324 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9325 {
9326 enum elf_ppc64_reloc_type r_type;
9327 unsigned long r_symndx;
9328 asection *sym_sec;
9329 struct elf_link_hash_entry *h;
9330 bfd_vma val;
9331
9332 r_type = ELF64_R_TYPE (rel->r_info);
9333 switch (r_type)
9334 {
9335 default:
9336 continue;
9337
9338 case R_PPC64_TOC16:
9339 case R_PPC64_TOC16_LO:
9340 case R_PPC64_TOC16_HI:
9341 case R_PPC64_TOC16_HA:
9342 case R_PPC64_TOC16_DS:
9343 case R_PPC64_TOC16_LO_DS:
9344 case R_PPC64_ADDR64:
9345 break;
9346 }
9347
9348 r_symndx = ELF64_R_SYM (rel->r_info);
9349 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9350 r_symndx, ibfd))
9351 goto error_ret;
9352
9353 if (sym_sec != toc)
9354 continue;
9355
9356 if (h != NULL)
9357 val = h->root.u.def.value;
9358 else
9359 {
9360 val = sym->st_value;
9361 if (val != 0)
9362 local_toc_syms = true;
9363 }
9364
9365 val += rel->r_addend;
9366
9367 if (val > toc->rawsize)
9368 val = toc->rawsize;
9369 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9370 continue;
9371 else if ((skip[val >> 3] & can_optimize) != 0)
9372 {
9373 Elf_Internal_Rela *tocrel
9374 = toc_relocs + (skip[val >> 3] >> 2);
9375 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9376
9377 switch (r_type)
9378 {
9379 case R_PPC64_TOC16_HA:
9380 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9381 break;
9382
9383 case R_PPC64_TOC16_LO_DS:
9384 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9385 break;
9386
9387 default:
9388 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9389 ppc_howto_init ();
9390 info->callbacks->einfo
9391 /* xgettext:c-format */
9392 (_("%H: %s references "
9393 "optimized away TOC entry\n"),
9394 ibfd, sec, rel->r_offset,
9395 ppc64_elf_howto_table[r_type]->name);
9396 bfd_set_error (bfd_error_bad_value);
9397 goto error_ret;
9398 }
9399 rel->r_addend = tocrel->r_addend;
9400 elf_section_data (sec)->relocs = relstart;
9401 continue;
9402 }
9403
9404 if (h != NULL || sym->st_value != 0)
9405 continue;
9406
9407 rel->r_addend -= skip[val >> 3];
9408 elf_section_data (sec)->relocs = relstart;
9409 }
9410
9411 if (elf_section_data (sec)->relocs != relstart)
9412 free (relstart);
9413 }
9414
9415 /* We shouldn't have local or global symbols defined in the TOC,
9416 but handle them anyway. */
9417 if (local_syms != NULL)
9418 for (sym = local_syms;
9419 sym < local_syms + symtab_hdr->sh_info;
9420 ++sym)
9421 if (sym->st_value != 0
9422 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9423 {
9424 unsigned long i;
9425
9426 if (sym->st_value > toc->rawsize)
9427 i = toc->rawsize >> 3;
9428 else
9429 i = sym->st_value >> 3;
9430
9431 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9432 {
9433 if (local_toc_syms)
9434 _bfd_error_handler
9435 (_("%s defined on removed toc entry"),
9436 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9437 do
9438 ++i;
9439 while ((skip[i] & (ref_from_discarded | can_optimize)));
9440 sym->st_value = (bfd_vma) i << 3;
9441 }
9442
9443 sym->st_value -= skip[i];
9444 symtab_hdr->contents = (unsigned char *) local_syms;
9445 }
9446
9447 /* Adjust any global syms defined in this toc input section. */
9448 if (toc_inf.global_toc_syms)
9449 {
9450 toc_inf.toc = toc;
9451 toc_inf.skip = skip;
9452 toc_inf.global_toc_syms = false;
9453 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9454 &toc_inf);
9455 }
9456
9457 if (toc->reloc_count != 0)
9458 {
9459 Elf_Internal_Shdr *rel_hdr;
9460 Elf_Internal_Rela *wrel;
9461 bfd_size_type sz;
9462
9463 /* Remove unused toc relocs, and adjust those we keep. */
9464 if (toc_relocs == NULL)
9465 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9466 info->keep_memory);
9467 if (toc_relocs == NULL)
9468 goto error_ret;
9469
9470 wrel = toc_relocs;
9471 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9472 if ((skip[rel->r_offset >> 3]
9473 & (ref_from_discarded | can_optimize)) == 0)
9474 {
9475 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9476 wrel->r_info = rel->r_info;
9477 wrel->r_addend = rel->r_addend;
9478 ++wrel;
9479 }
9480 else if (!dec_dynrel_count (rel, toc, info,
9481 &local_syms, NULL, NULL))
9482 goto error_ret;
9483
9484 elf_section_data (toc)->relocs = toc_relocs;
9485 toc->reloc_count = wrel - toc_relocs;
9486 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9487 sz = rel_hdr->sh_entsize;
9488 rel_hdr->sh_size = toc->reloc_count * sz;
9489 }
9490 }
9491 else if (elf_section_data (toc)->relocs != toc_relocs)
9492 free (toc_relocs);
9493
9494 if (local_syms != NULL
9495 && symtab_hdr->contents != (unsigned char *) local_syms)
9496 {
9497 if (!info->keep_memory)
9498 free (local_syms);
9499 else
9500 symtab_hdr->contents = (unsigned char *) local_syms;
9501 }
9502 free (skip);
9503 }
9504
9505 /* Look for cases where we can change an indirect GOT access to
9506 a GOT relative or PC relative access, possibly reducing the
9507 number of GOT entries. */
9508 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9509 {
9510 asection *sec;
9511 Elf_Internal_Shdr *symtab_hdr;
9512 Elf_Internal_Sym *local_syms;
9513 Elf_Internal_Rela *relstart, *rel;
9514 bfd_vma got;
9515
9516 if (!is_ppc64_elf (ibfd))
9517 continue;
9518
9519 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9520 continue;
9521
9522 sec = ppc64_elf_tdata (ibfd)->got;
9523 got = 0;
9524 if (sec != NULL)
9525 got = sec->output_section->vma + sec->output_offset + 0x8000;
9526
9527 local_syms = NULL;
9528 symtab_hdr = &elf_symtab_hdr (ibfd);
9529
9530 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9531 {
9532 if (sec->reloc_count == 0
9533 || !ppc64_elf_section_data (sec)->has_optrel
9534 || discarded_section (sec))
9535 continue;
9536
9537 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9538 info->keep_memory);
9539 if (relstart == NULL)
9540 {
9541 got_error_ret:
9542 if (symtab_hdr->contents != (unsigned char *) local_syms)
9543 free (local_syms);
9544 if (sec != NULL
9545 && elf_section_data (sec)->relocs != relstart)
9546 free (relstart);
9547 return false;
9548 }
9549
9550 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9551 {
9552 enum elf_ppc64_reloc_type r_type;
9553 unsigned long r_symndx;
9554 Elf_Internal_Sym *sym;
9555 asection *sym_sec;
9556 struct elf_link_hash_entry *h;
9557 struct got_entry *ent;
9558 bfd_vma val, pc;
9559 unsigned char buf[8];
9560 unsigned int insn;
9561 enum {no_check, check_lo, check_ha} insn_check;
9562
9563 r_type = ELF64_R_TYPE (rel->r_info);
9564 switch (r_type)
9565 {
9566 default:
9567 insn_check = no_check;
9568 break;
9569
9570 case R_PPC64_PLT16_HA:
9571 case R_PPC64_GOT_TLSLD16_HA:
9572 case R_PPC64_GOT_TLSGD16_HA:
9573 case R_PPC64_GOT_TPREL16_HA:
9574 case R_PPC64_GOT_DTPREL16_HA:
9575 case R_PPC64_GOT16_HA:
9576 case R_PPC64_TOC16_HA:
9577 insn_check = check_ha;
9578 break;
9579
9580 case R_PPC64_PLT16_LO:
9581 case R_PPC64_PLT16_LO_DS:
9582 case R_PPC64_GOT_TLSLD16_LO:
9583 case R_PPC64_GOT_TLSGD16_LO:
9584 case R_PPC64_GOT_TPREL16_LO_DS:
9585 case R_PPC64_GOT_DTPREL16_LO_DS:
9586 case R_PPC64_GOT16_LO:
9587 case R_PPC64_GOT16_LO_DS:
9588 case R_PPC64_TOC16_LO:
9589 case R_PPC64_TOC16_LO_DS:
9590 insn_check = check_lo;
9591 break;
9592 }
9593
9594 if (insn_check != no_check)
9595 {
9596 bfd_vma off = rel->r_offset & ~3;
9597
9598 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9599 goto got_error_ret;
9600
9601 insn = bfd_get_32 (ibfd, buf);
9602 if (insn_check == check_lo
9603 ? !ok_lo_toc_insn (insn, r_type)
9604 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9605 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9606 {
9607 char str[12];
9608
9609 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9610 sprintf (str, "%#08x", insn);
9611 info->callbacks->einfo
9612 /* xgettext:c-format */
9613 (_("%H: got/toc optimization is not supported for"
9614 " %s instruction\n"),
9615 ibfd, sec, rel->r_offset & ~3, str);
9616 continue;
9617 }
9618 }
9619
9620 switch (r_type)
9621 {
9622 /* Note that we don't delete GOT entries for
9623 R_PPC64_GOT16_DS since we'd need a lot more
9624 analysis. For starters, the preliminary layout is
9625 before the GOT, PLT, dynamic sections and stubs are
9626 laid out. Then we'd need to allow for changes in
9627 distance between sections caused by alignment. */
9628 default:
9629 continue;
9630
9631 case R_PPC64_GOT16_HA:
9632 case R_PPC64_GOT16_LO_DS:
9633 case R_PPC64_GOT_PCREL34:
9634 break;
9635 }
9636
9637 r_symndx = ELF64_R_SYM (rel->r_info);
9638 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9639 r_symndx, ibfd))
9640 goto got_error_ret;
9641
9642 if (sym_sec == NULL
9643 || sym_sec->output_section == NULL
9644 || discarded_section (sym_sec))
9645 continue;
9646
9647 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9648 continue;
9649
9650 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9651 continue;
9652
9653 if (h != NULL)
9654 val = h->root.u.def.value;
9655 else
9656 val = sym->st_value;
9657 val += rel->r_addend;
9658 val += sym_sec->output_section->vma + sym_sec->output_offset;
9659
9660 /* Fudge factor to allow for the fact that the preliminary layout
9661 isn't exact. Reduce limits by this factor. */
9662 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9663
9664 switch (r_type)
9665 {
9666 default:
9667 continue;
9668
9669 case R_PPC64_GOT16_HA:
9670 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9671 >= LIMIT_ADJUST (0x100000000ULL))
9672 continue;
9673
9674 if (!bfd_get_section_contents (ibfd, sec, buf,
9675 rel->r_offset & ~3, 4))
9676 goto got_error_ret;
9677 insn = bfd_get_32 (ibfd, buf);
9678 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9679 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9680 continue;
9681 break;
9682
9683 case R_PPC64_GOT16_LO_DS:
9684 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9685 >= LIMIT_ADJUST (0x100000000ULL))
9686 continue;
9687 if (!bfd_get_section_contents (ibfd, sec, buf,
9688 rel->r_offset & ~3, 4))
9689 goto got_error_ret;
9690 insn = bfd_get_32 (ibfd, buf);
9691 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9692 continue;
9693 break;
9694
9695 case R_PPC64_GOT_PCREL34:
9696 pc = rel->r_offset;
9697 pc += sec->output_section->vma + sec->output_offset;
9698 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9699 >= LIMIT_ADJUST (1ULL << 34))
9700 continue;
9701 if (!bfd_get_section_contents (ibfd, sec, buf,
9702 rel->r_offset & ~3, 8))
9703 goto got_error_ret;
9704 insn = bfd_get_32 (ibfd, buf);
9705 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9706 continue;
9707 insn = bfd_get_32 (ibfd, buf + 4);
9708 if ((insn & (0x3fu << 26)) != 57u << 26)
9709 continue;
9710 break;
9711 }
9712 #undef LIMIT_ADJUST
9713
9714 if (h != NULL)
9715 ent = h->got.glist;
9716 else
9717 {
9718 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9719 ent = local_got_ents[r_symndx];
9720 }
9721 for (; ent != NULL; ent = ent->next)
9722 if (ent->addend == rel->r_addend
9723 && ent->owner == ibfd
9724 && ent->tls_type == 0)
9725 break;
9726 BFD_ASSERT (ent && ent->got.refcount > 0);
9727 ent->got.refcount -= 1;
9728 }
9729
9730 if (elf_section_data (sec)->relocs != relstart)
9731 free (relstart);
9732 }
9733
9734 if (local_syms != NULL
9735 && symtab_hdr->contents != (unsigned char *) local_syms)
9736 {
9737 if (!info->keep_memory)
9738 free (local_syms);
9739 else
9740 symtab_hdr->contents = (unsigned char *) local_syms;
9741 }
9742 }
9743
9744 return true;
9745 }
9746
9747 /* Return true iff input section I references the TOC using
9748 instructions limited to +/-32k offsets. */
9749
9750 bool
9751 ppc64_elf_has_small_toc_reloc (asection *i)
9752 {
9753 return (is_ppc64_elf (i->owner)
9754 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9755 }
9756
9757 /* Allocate space for one GOT entry. */
9758
9759 static void
9760 allocate_got (struct elf_link_hash_entry *h,
9761 struct bfd_link_info *info,
9762 struct got_entry *gent)
9763 {
9764 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9765 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9766 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9767 ? 16 : 8);
9768 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9769 ? 2 : 1) * sizeof (Elf64_External_Rela);
9770 asection *got = ppc64_elf_tdata (gent->owner)->got;
9771
9772 gent->got.offset = got->size;
9773 got->size += entsize;
9774
9775 if (h->type == STT_GNU_IFUNC)
9776 {
9777 htab->elf.irelplt->size += rentsize;
9778 htab->got_reli_size += rentsize;
9779 }
9780 else if (((bfd_link_pic (info)
9781 && (gent->tls_type == 0
9782 ? !info->enable_dt_relr
9783 : !(bfd_link_executable (info)
9784 && SYMBOL_REFERENCES_LOCAL (info, h))))
9785 || (htab->elf.dynamic_sections_created
9786 && h->dynindx != -1
9787 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9788 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9789 {
9790 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9791 relgot->size += rentsize;
9792 }
9793 }
9794
9795 /* This function merges got entries in the same toc group. */
9796
9797 static void
9798 merge_got_entries (struct got_entry **pent)
9799 {
9800 struct got_entry *ent, *ent2;
9801
9802 for (ent = *pent; ent != NULL; ent = ent->next)
9803 if (!ent->is_indirect)
9804 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9805 if (!ent2->is_indirect
9806 && ent2->addend == ent->addend
9807 && ent2->tls_type == ent->tls_type
9808 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9809 {
9810 ent2->is_indirect = true;
9811 ent2->got.ent = ent;
9812 }
9813 }
9814
9815 /* If H is undefined, make it dynamic if that makes sense. */
9816
9817 static bool
9818 ensure_undef_dynamic (struct bfd_link_info *info,
9819 struct elf_link_hash_entry *h)
9820 {
9821 struct elf_link_hash_table *htab = elf_hash_table (info);
9822
9823 if (htab->dynamic_sections_created
9824 && ((info->dynamic_undefined_weak != 0
9825 && h->root.type == bfd_link_hash_undefweak)
9826 || h->root.type == bfd_link_hash_undefined)
9827 && h->dynindx == -1
9828 && !h->forced_local
9829 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9830 return bfd_elf_link_record_dynamic_symbol (info, h);
9831 return true;
9832 }
9833
9834 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9835 usual htab->elf.splt section for a PLT entry. */
9836
9837 static inline
9838 bool use_local_plt (struct bfd_link_info *info,
9839 struct elf_link_hash_entry *h)
9840 {
9841 return (h == NULL
9842 || h->dynindx == -1
9843 || !elf_hash_table (info)->dynamic_sections_created);
9844 }
9845
9846 /* Allocate space in .plt, .got and associated reloc sections for
9847 dynamic relocs. */
9848
9849 static bool
9850 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9851 {
9852 struct bfd_link_info *info;
9853 struct ppc_link_hash_table *htab;
9854 asection *s;
9855 struct ppc_link_hash_entry *eh;
9856 struct got_entry **pgent, *gent;
9857
9858 if (h->root.type == bfd_link_hash_indirect)
9859 return true;
9860
9861 info = (struct bfd_link_info *) inf;
9862 htab = ppc_hash_table (info);
9863 if (htab == NULL)
9864 return false;
9865
9866 eh = ppc_elf_hash_entry (h);
9867 /* Run through the TLS GD got entries first if we're changing them
9868 to TPREL. */
9869 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9870 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9871 if (gent->got.refcount > 0
9872 && (gent->tls_type & TLS_GD) != 0)
9873 {
9874 /* This was a GD entry that has been converted to TPREL. If
9875 there happens to be a TPREL entry we can use that one. */
9876 struct got_entry *ent;
9877 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9878 if (ent->got.refcount > 0
9879 && (ent->tls_type & TLS_TPREL) != 0
9880 && ent->addend == gent->addend
9881 && ent->owner == gent->owner)
9882 {
9883 gent->got.refcount = 0;
9884 break;
9885 }
9886
9887 /* If not, then we'll be using our own TPREL entry. */
9888 if (gent->got.refcount != 0)
9889 gent->tls_type = TLS_TLS | TLS_TPREL;
9890 }
9891
9892 /* Remove any list entry that won't generate a word in the GOT before
9893 we call merge_got_entries. Otherwise we risk merging to empty
9894 entries. */
9895 pgent = &h->got.glist;
9896 while ((gent = *pgent) != NULL)
9897 if (gent->got.refcount > 0)
9898 {
9899 if ((gent->tls_type & TLS_LD) != 0
9900 && SYMBOL_REFERENCES_LOCAL (info, h))
9901 {
9902 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9903 *pgent = gent->next;
9904 }
9905 else
9906 pgent = &gent->next;
9907 }
9908 else
9909 *pgent = gent->next;
9910
9911 if (!htab->do_multi_toc)
9912 merge_got_entries (&h->got.glist);
9913
9914 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9915 if (!gent->is_indirect)
9916 {
9917 /* Ensure we catch all the cases where this symbol should
9918 be made dynamic. */
9919 if (!ensure_undef_dynamic (info, h))
9920 return false;
9921
9922 if (!is_ppc64_elf (gent->owner))
9923 abort ();
9924
9925 allocate_got (h, info, gent);
9926 }
9927
9928 /* If no dynamic sections we can't have dynamic relocs, except for
9929 IFUNCs which are handled even in static executables. */
9930 if (!htab->elf.dynamic_sections_created
9931 && h->type != STT_GNU_IFUNC)
9932 h->dyn_relocs = NULL;
9933
9934 /* Discard relocs on undefined symbols that must be local. */
9935 else if (h->root.type == bfd_link_hash_undefined
9936 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9937 h->dyn_relocs = NULL;
9938
9939 /* Also discard relocs on undefined weak syms with non-default
9940 visibility, or when dynamic_undefined_weak says so. */
9941 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9942 h->dyn_relocs = NULL;
9943
9944 if (h->dyn_relocs != NULL)
9945 {
9946 struct ppc_dyn_relocs *p, **pp;
9947
9948 /* In the shared -Bsymbolic case, discard space allocated for
9949 dynamic pc-relative relocs against symbols which turn out to
9950 be defined in regular objects. For the normal shared case,
9951 discard space for relocs that have become local due to symbol
9952 visibility changes. */
9953 if (bfd_link_pic (info))
9954 {
9955 /* Relocs that use pc_count are those that appear on a call
9956 insn, or certain REL relocs (see must_be_dyn_reloc) that
9957 can be generated via assembly. We want calls to
9958 protected symbols to resolve directly to the function
9959 rather than going via the plt. If people want function
9960 pointer comparisons to work as expected then they should
9961 avoid writing weird assembly. */
9962 if (SYMBOL_CALLS_LOCAL (info, h))
9963 {
9964 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9965 (p = *pp) != NULL;
9966 )
9967 {
9968 p->count -= p->pc_count;
9969 p->pc_count = 0;
9970 if (p->count == 0)
9971 *pp = p->next;
9972 else
9973 pp = &p->next;
9974 }
9975 }
9976
9977 if (h->dyn_relocs != NULL)
9978 {
9979 /* Ensure we catch all the cases where this symbol
9980 should be made dynamic. */
9981 if (!ensure_undef_dynamic (info, h))
9982 return false;
9983 }
9984 }
9985
9986 /* For a fixed position executable, discard space for
9987 relocs against symbols which are not dynamic. */
9988 else if (h->type != STT_GNU_IFUNC)
9989 {
9990 if ((h->dynamic_adjusted
9991 || (h->ref_regular
9992 && h->root.type == bfd_link_hash_undefweak
9993 && (info->dynamic_undefined_weak > 0
9994 || !_bfd_elf_readonly_dynrelocs (h))))
9995 && !h->def_regular
9996 && !ELF_COMMON_DEF_P (h))
9997 {
9998 /* Ensure we catch all the cases where this symbol
9999 should be made dynamic. */
10000 if (!ensure_undef_dynamic (info, h))
10001 return false;
10002
10003 /* But if that didn't work out, discard dynamic relocs. */
10004 if (h->dynindx == -1)
10005 h->dyn_relocs = NULL;
10006 }
10007 else
10008 h->dyn_relocs = NULL;
10009 }
10010
10011 /* Finally, allocate space. */
10012 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10013 {
10014 unsigned int count;
10015 asection *sreloc = elf_section_data (p->sec)->sreloc;
10016 if (eh->elf.type == STT_GNU_IFUNC)
10017 sreloc = htab->elf.irelplt;
10018 count = p->count;
10019 if (info->enable_dt_relr)
10020 count -= p->rel_count;
10021 sreloc->size += count * sizeof (Elf64_External_Rela);
10022 }
10023 }
10024
10025 /* We might need a PLT entry when the symbol
10026 a) is dynamic, or
10027 b) is an ifunc, or
10028 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10029 d) has plt16 relocs and we are linking statically. */
10030 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10031 || h->type == STT_GNU_IFUNC
10032 || (h->needs_plt && h->dynamic_adjusted)
10033 || (h->needs_plt
10034 && h->def_regular
10035 && !htab->elf.dynamic_sections_created
10036 && !htab->can_convert_all_inline_plt
10037 && (ppc_elf_hash_entry (h)->tls_mask
10038 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10039 {
10040 struct plt_entry *pent;
10041 bool doneone = false;
10042 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10043 if (pent->plt.refcount > 0)
10044 {
10045 if (!ensure_undef_dynamic (info, h))
10046 return false;
10047
10048 if (use_local_plt (info, h))
10049 {
10050 if (h->type == STT_GNU_IFUNC)
10051 {
10052 s = htab->elf.iplt;
10053 pent->plt.offset = s->size;
10054 s->size += PLT_ENTRY_SIZE (htab);
10055 s = htab->elf.irelplt;
10056 }
10057 else
10058 {
10059 s = htab->pltlocal;
10060 pent->plt.offset = s->size;
10061 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10062 s = NULL;
10063 if (bfd_link_pic (info)
10064 && !(info->enable_dt_relr && !htab->opd_abi))
10065 s = htab->relpltlocal;
10066 }
10067 }
10068 else
10069 {
10070 /* If this is the first .plt entry, make room for the special
10071 first entry. */
10072 s = htab->elf.splt;
10073 if (s->size == 0)
10074 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10075
10076 pent->plt.offset = s->size;
10077
10078 /* Make room for this entry. */
10079 s->size += PLT_ENTRY_SIZE (htab);
10080
10081 /* Make room for the .glink code. */
10082 s = htab->glink;
10083 if (s->size == 0)
10084 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10085 if (htab->opd_abi)
10086 {
10087 /* We need bigger stubs past index 32767. */
10088 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10089 s->size += 4;
10090 s->size += 2*4;
10091 }
10092 else
10093 s->size += 4;
10094
10095 /* We also need to make an entry in the .rela.plt section. */
10096 s = htab->elf.srelplt;
10097 }
10098 if (s != NULL)
10099 s->size += sizeof (Elf64_External_Rela);
10100 doneone = true;
10101 }
10102 else
10103 pent->plt.offset = (bfd_vma) -1;
10104 if (!doneone)
10105 {
10106 h->plt.plist = NULL;
10107 h->needs_plt = 0;
10108 }
10109 }
10110 else
10111 {
10112 h->plt.plist = NULL;
10113 h->needs_plt = 0;
10114 }
10115
10116 return true;
10117 }
10118
10119 #define PPC_LO(v) ((v) & 0xffff)
10120 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10121 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10122 #define D34(v) \
10123 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10124 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10125
10126 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10127 to set up space for global entry stubs. These are put in glink,
10128 after the branch table. */
10129
10130 static bool
10131 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10132 {
10133 struct bfd_link_info *info;
10134 struct ppc_link_hash_table *htab;
10135 struct plt_entry *pent;
10136 asection *s, *plt;
10137
10138 if (h->root.type == bfd_link_hash_indirect)
10139 return true;
10140
10141 if (!h->pointer_equality_needed)
10142 return true;
10143
10144 if (h->def_regular)
10145 return true;
10146
10147 info = inf;
10148 htab = ppc_hash_table (info);
10149 if (htab == NULL)
10150 return false;
10151
10152 s = htab->global_entry;
10153 plt = htab->elf.splt;
10154 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10155 if (pent->plt.offset != (bfd_vma) -1
10156 && pent->addend == 0)
10157 {
10158 /* For ELFv2, if this symbol is not defined in a regular file
10159 and we are not generating a shared library or pie, then we
10160 need to define the symbol in the executable on a call stub.
10161 This is to avoid text relocations. */
10162 bfd_vma off, stub_align, stub_off, stub_size;
10163 unsigned int align_power;
10164
10165 stub_size = 16;
10166 stub_off = s->size;
10167 if (htab->params->plt_stub_align >= 0)
10168 align_power = htab->params->plt_stub_align;
10169 else
10170 align_power = -htab->params->plt_stub_align;
10171 /* Setting section alignment is delayed until we know it is
10172 non-empty. Otherwise the .text output section will be
10173 aligned at least to plt_stub_align even when no global
10174 entry stubs are needed. */
10175 if (s->alignment_power < align_power)
10176 s->alignment_power = align_power;
10177 stub_align = (bfd_vma) 1 << align_power;
10178 if (htab->params->plt_stub_align >= 0
10179 || ((((stub_off + stub_size - 1) & -stub_align)
10180 - (stub_off & -stub_align))
10181 > ((stub_size - 1) & -stub_align)))
10182 stub_off = (stub_off + stub_align - 1) & -stub_align;
10183 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10184 off -= stub_off + s->output_offset + s->output_section->vma;
10185 /* Note that for --plt-stub-align negative we have a possible
10186 dependency between stub offset and size. Break that
10187 dependency by assuming the max stub size when calculating
10188 the stub offset. */
10189 if (PPC_HA (off) == 0)
10190 stub_size -= 4;
10191 h->root.type = bfd_link_hash_defined;
10192 h->root.u.def.section = s;
10193 h->root.u.def.value = stub_off;
10194 s->size = stub_off + stub_size;
10195 break;
10196 }
10197 return true;
10198 }
10199
10200 /* Set the sizes of the dynamic sections. */
10201
10202 static bool
10203 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10204 struct bfd_link_info *info)
10205 {
10206 struct ppc_link_hash_table *htab;
10207 bfd *dynobj;
10208 asection *s;
10209 bool relocs;
10210 bfd *ibfd;
10211 struct got_entry *first_tlsld;
10212
10213 htab = ppc_hash_table (info);
10214 if (htab == NULL)
10215 return false;
10216
10217 dynobj = htab->elf.dynobj;
10218 if (dynobj == NULL)
10219 abort ();
10220
10221 if (htab->elf.dynamic_sections_created)
10222 {
10223 /* Set the contents of the .interp section to the interpreter. */
10224 if (bfd_link_executable (info) && !info->nointerp)
10225 {
10226 s = bfd_get_linker_section (dynobj, ".interp");
10227 if (s == NULL)
10228 abort ();
10229 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10230 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10231 }
10232 }
10233
10234 /* Set up .got offsets for local syms, and space for local dynamic
10235 relocs. */
10236 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10237 {
10238 struct got_entry **lgot_ents;
10239 struct got_entry **end_lgot_ents;
10240 struct plt_entry **local_plt;
10241 struct plt_entry **end_local_plt;
10242 unsigned char *lgot_masks;
10243 bfd_size_type locsymcount;
10244 Elf_Internal_Shdr *symtab_hdr;
10245
10246 if (!is_ppc64_elf (ibfd))
10247 continue;
10248
10249 for (s = ibfd->sections; s != NULL; s = s->next)
10250 {
10251 struct ppc_local_dyn_relocs *p;
10252
10253 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10254 {
10255 if (!bfd_is_abs_section (p->sec)
10256 && bfd_is_abs_section (p->sec->output_section))
10257 {
10258 /* Input section has been discarded, either because
10259 it is a copy of a linkonce section or due to
10260 linker script /DISCARD/, so we'll be discarding
10261 the relocs too. */
10262 }
10263 else if (p->count != 0)
10264 {
10265 unsigned int count;
10266 asection *srel;
10267
10268 count = p->count;
10269 if (info->enable_dt_relr)
10270 count -= p->rel_count;
10271 srel = elf_section_data (p->sec)->sreloc;
10272 if (p->ifunc)
10273 srel = htab->elf.irelplt;
10274 srel->size += count * sizeof (Elf64_External_Rela);
10275 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10276 info->flags |= DF_TEXTREL;
10277 }
10278 }
10279 }
10280
10281 lgot_ents = elf_local_got_ents (ibfd);
10282 if (!lgot_ents)
10283 continue;
10284
10285 symtab_hdr = &elf_symtab_hdr (ibfd);
10286 locsymcount = symtab_hdr->sh_info;
10287 end_lgot_ents = lgot_ents + locsymcount;
10288 local_plt = (struct plt_entry **) end_lgot_ents;
10289 end_local_plt = local_plt + locsymcount;
10290 lgot_masks = (unsigned char *) end_local_plt;
10291 s = ppc64_elf_tdata (ibfd)->got;
10292 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10293 {
10294 struct got_entry **pent, *ent;
10295
10296 pent = lgot_ents;
10297 while ((ent = *pent) != NULL)
10298 if (ent->got.refcount > 0)
10299 {
10300 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10301 {
10302 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10303 *pent = ent->next;
10304 }
10305 else
10306 {
10307 unsigned int ent_size = 8;
10308 unsigned int rel_size = sizeof (Elf64_External_Rela);
10309
10310 ent->got.offset = s->size;
10311 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10312 {
10313 ent_size *= 2;
10314 rel_size *= 2;
10315 }
10316 s->size += ent_size;
10317 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10318 {
10319 htab->elf.irelplt->size += rel_size;
10320 htab->got_reli_size += rel_size;
10321 }
10322 else if (bfd_link_pic (info)
10323 && !(ent->tls_type != 0
10324 && bfd_link_executable (info)))
10325 {
10326 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10327 srel->size += rel_size;
10328 }
10329 pent = &ent->next;
10330 }
10331 }
10332 else
10333 *pent = ent->next;
10334 }
10335
10336 /* Allocate space for plt calls to local syms. */
10337 lgot_masks = (unsigned char *) end_local_plt;
10338 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10339 {
10340 struct plt_entry *ent;
10341
10342 for (ent = *local_plt; ent != NULL; ent = ent->next)
10343 if (ent->plt.refcount > 0)
10344 {
10345 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10346 {
10347 s = htab->elf.iplt;
10348 ent->plt.offset = s->size;
10349 s->size += PLT_ENTRY_SIZE (htab);
10350 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10351 }
10352 else if (htab->can_convert_all_inline_plt
10353 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10354 ent->plt.offset = (bfd_vma) -1;
10355 else
10356 {
10357 s = htab->pltlocal;
10358 ent->plt.offset = s->size;
10359 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10360 if (bfd_link_pic (info))
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 && bfd_link_executable (info)))
12822 {
12823 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12824 srel->size += rel_size;
12825 }
12826 }
12827 }
12828 }
12829
12830 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12831
12832 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12833 {
12834 struct got_entry *ent;
12835
12836 if (!is_ppc64_elf (ibfd))
12837 continue;
12838
12839 ent = ppc64_tlsld_got (ibfd);
12840 if (!ent->is_indirect
12841 && ent->got.offset != (bfd_vma) -1)
12842 {
12843 asection *s = ppc64_elf_tdata (ibfd)->got;
12844 ent->got.offset = s->size;
12845 s->size += 16;
12846 if (bfd_link_dll (info))
12847 {
12848 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12849 srel->size += sizeof (Elf64_External_Rela);
12850 }
12851 }
12852 }
12853
12854 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12855 if (!done_something)
12856 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12857 {
12858 asection *got;
12859
12860 if (!is_ppc64_elf (ibfd))
12861 continue;
12862
12863 got = ppc64_elf_tdata (ibfd)->got;
12864 if (got != NULL)
12865 {
12866 done_something = got->rawsize != got->size;
12867 if (done_something)
12868 break;
12869 }
12870 }
12871
12872 if (done_something)
12873 (*htab->params->layout_sections_again) ();
12874
12875 /* Set up for second pass over toc sections to recalculate elf_gp
12876 on input sections. */
12877 htab->toc_bfd = NULL;
12878 htab->toc_first_sec = NULL;
12879 htab->second_toc_pass = true;
12880 return done_something;
12881 }
12882
12883 /* Called after second pass of multitoc partitioning. */
12884
12885 void
12886 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12887 {
12888 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12889
12890 /* After the second pass, toc_curr tracks the TOC offset used
12891 for code sections below in ppc64_elf_next_input_section. */
12892 htab->toc_curr = TOC_BASE_OFF;
12893 }
12894
12895 /* No toc references were found in ISEC. If the code in ISEC makes no
12896 calls, then there's no need to use toc adjusting stubs when branching
12897 into ISEC. Actually, indirect calls from ISEC are OK as they will
12898 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12899 needed, and 2 if a cyclical call-graph was found but no other reason
12900 for a stub was detected. If called from the top level, a return of
12901 2 means the same as a return of 0. */
12902
12903 static int
12904 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12905 {
12906 int ret;
12907
12908 /* Mark this section as checked. */
12909 isec->call_check_done = 1;
12910
12911 /* We know none of our code bearing sections will need toc stubs. */
12912 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12913 return 0;
12914
12915 if (isec->size == 0)
12916 return 0;
12917
12918 if (isec->output_section == NULL)
12919 return 0;
12920
12921 ret = 0;
12922 if (isec->reloc_count != 0)
12923 {
12924 Elf_Internal_Rela *relstart, *rel;
12925 Elf_Internal_Sym *local_syms;
12926 struct ppc_link_hash_table *htab;
12927
12928 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12929 info->keep_memory);
12930 if (relstart == NULL)
12931 return -1;
12932
12933 /* Look for branches to outside of this section. */
12934 local_syms = NULL;
12935 htab = ppc_hash_table (info);
12936 if (htab == NULL)
12937 return -1;
12938
12939 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12940 {
12941 enum elf_ppc64_reloc_type r_type;
12942 unsigned long r_symndx;
12943 struct elf_link_hash_entry *h;
12944 struct ppc_link_hash_entry *eh;
12945 Elf_Internal_Sym *sym;
12946 asection *sym_sec;
12947 struct _opd_sec_data *opd;
12948 bfd_vma sym_value;
12949 bfd_vma dest;
12950
12951 r_type = ELF64_R_TYPE (rel->r_info);
12952 if (r_type != R_PPC64_REL24
12953 && r_type != R_PPC64_REL24_NOTOC
12954 && r_type != R_PPC64_REL24_P9NOTOC
12955 && r_type != R_PPC64_REL14
12956 && r_type != R_PPC64_REL14_BRTAKEN
12957 && r_type != R_PPC64_REL14_BRNTAKEN
12958 && r_type != R_PPC64_PLTCALL
12959 && r_type != R_PPC64_PLTCALL_NOTOC)
12960 continue;
12961
12962 r_symndx = ELF64_R_SYM (rel->r_info);
12963 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12964 isec->owner))
12965 {
12966 ret = -1;
12967 break;
12968 }
12969
12970 /* Calls to dynamic lib functions go through a plt call stub
12971 that uses r2. */
12972 eh = ppc_elf_hash_entry (h);
12973 if (eh != NULL
12974 && (eh->elf.plt.plist != NULL
12975 || (eh->oh != NULL
12976 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12977 {
12978 ret = 1;
12979 break;
12980 }
12981
12982 if (sym_sec == NULL)
12983 /* Ignore other undefined symbols. */
12984 continue;
12985
12986 /* Assume branches to other sections not included in the
12987 link need stubs too, to cover -R and absolute syms. */
12988 if (sym_sec->output_section == NULL)
12989 {
12990 ret = 1;
12991 break;
12992 }
12993
12994 if (h == NULL)
12995 sym_value = sym->st_value;
12996 else
12997 {
12998 if (h->root.type != bfd_link_hash_defined
12999 && h->root.type != bfd_link_hash_defweak)
13000 abort ();
13001 sym_value = h->root.u.def.value;
13002 }
13003 sym_value += rel->r_addend;
13004
13005 /* If this branch reloc uses an opd sym, find the code section. */
13006 opd = get_opd_info (sym_sec);
13007 if (opd != NULL)
13008 {
13009 if (h == NULL && opd->adjust != NULL)
13010 {
13011 long adjust;
13012
13013 adjust = opd->adjust[OPD_NDX (sym_value)];
13014 if (adjust == -1)
13015 /* Assume deleted functions won't ever be called. */
13016 continue;
13017 sym_value += adjust;
13018 }
13019
13020 dest = opd_entry_value (sym_sec, sym_value,
13021 &sym_sec, NULL, false);
13022 if (dest == (bfd_vma) -1)
13023 continue;
13024 }
13025 else
13026 dest = (sym_value
13027 + sym_sec->output_offset
13028 + sym_sec->output_section->vma);
13029
13030 /* Ignore branch to self. */
13031 if (sym_sec == isec)
13032 continue;
13033
13034 /* If the called function uses the toc, we need a stub. */
13035 if (sym_sec->has_toc_reloc
13036 || sym_sec->makes_toc_func_call)
13037 {
13038 ret = 1;
13039 break;
13040 }
13041
13042 /* Assume any branch that needs a long branch stub might in fact
13043 need a plt_branch stub. A plt_branch stub uses r2. */
13044 else if (dest - (isec->output_offset
13045 + isec->output_section->vma
13046 + rel->r_offset) + (1 << 25)
13047 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13048 ? h->other
13049 : sym->st_other))
13050 {
13051 ret = 1;
13052 break;
13053 }
13054
13055 /* If calling back to a section in the process of being
13056 tested, we can't say for sure that no toc adjusting stubs
13057 are needed, so don't return zero. */
13058 else if (sym_sec->call_check_in_progress)
13059 ret = 2;
13060
13061 /* Branches to another section that itself doesn't have any TOC
13062 references are OK. Recursively call ourselves to check. */
13063 else if (!sym_sec->call_check_done)
13064 {
13065 int recur;
13066
13067 /* Mark current section as indeterminate, so that other
13068 sections that call back to current won't be marked as
13069 known. */
13070 isec->call_check_in_progress = 1;
13071 recur = toc_adjusting_stub_needed (info, sym_sec);
13072 isec->call_check_in_progress = 0;
13073
13074 if (recur != 0)
13075 {
13076 ret = recur;
13077 if (recur != 2)
13078 break;
13079 }
13080 }
13081 }
13082
13083 if (elf_symtab_hdr (isec->owner).contents
13084 != (unsigned char *) local_syms)
13085 free (local_syms);
13086 if (elf_section_data (isec)->relocs != relstart)
13087 free (relstart);
13088 }
13089
13090 if ((ret & 1) == 0
13091 && isec->map_head.s != NULL
13092 && (strcmp (isec->output_section->name, ".init") == 0
13093 || strcmp (isec->output_section->name, ".fini") == 0))
13094 {
13095 if (isec->map_head.s->has_toc_reloc
13096 || isec->map_head.s->makes_toc_func_call)
13097 ret = 1;
13098 else if (!isec->map_head.s->call_check_done)
13099 {
13100 int recur;
13101 isec->call_check_in_progress = 1;
13102 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13103 isec->call_check_in_progress = 0;
13104 if (recur != 0)
13105 ret = recur;
13106 }
13107 }
13108
13109 if (ret == 1)
13110 isec->makes_toc_func_call = 1;
13111
13112 return ret;
13113 }
13114
13115 /* The linker repeatedly calls this function for each input section,
13116 in the order that input sections are linked into output sections.
13117 Build lists of input sections to determine groupings between which
13118 we may insert linker stubs. */
13119
13120 bool
13121 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13122 {
13123 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13124
13125 if (htab == NULL)
13126 return false;
13127
13128 if ((isec->output_section->flags & SEC_CODE) != 0
13129 && isec->output_section->id < htab->sec_info_arr_size)
13130 {
13131 /* This happens to make the list in reverse order,
13132 which is what we want. */
13133 htab->sec_info[isec->id].u.list
13134 = htab->sec_info[isec->output_section->id].u.list;
13135 htab->sec_info[isec->output_section->id].u.list = isec;
13136 }
13137
13138 if (htab->multi_toc_needed)
13139 {
13140 /* Analyse sections that aren't already flagged as needing a
13141 valid toc pointer. Exclude .fixup for the linux kernel.
13142 .fixup contains branches, but only back to the function that
13143 hit an exception. */
13144 if (!(isec->has_toc_reloc
13145 || (isec->flags & SEC_CODE) == 0
13146 || strcmp (isec->name, ".fixup") == 0
13147 || isec->call_check_done))
13148 {
13149 if (toc_adjusting_stub_needed (info, isec) < 0)
13150 return false;
13151 }
13152 /* Make all sections use the TOC assigned for this object file.
13153 This will be wrong for pasted sections; We fix that in
13154 check_pasted_section(). */
13155 if (elf_gp (isec->owner) != 0)
13156 htab->toc_curr = elf_gp (isec->owner);
13157 }
13158
13159 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13160 return true;
13161 }
13162
13163 /* Check that all .init and .fini sections use the same toc, if they
13164 have toc relocs. */
13165
13166 static bool
13167 check_pasted_section (struct bfd_link_info *info, const char *name)
13168 {
13169 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13170
13171 if (o != NULL)
13172 {
13173 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13174 bfd_vma toc_off = 0;
13175 asection *i;
13176
13177 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13178 if (i->has_toc_reloc)
13179 {
13180 if (toc_off == 0)
13181 toc_off = htab->sec_info[i->id].toc_off;
13182 else if (toc_off != htab->sec_info[i->id].toc_off)
13183 return false;
13184 }
13185
13186 if (toc_off == 0)
13187 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13188 if (i->makes_toc_func_call)
13189 {
13190 toc_off = htab->sec_info[i->id].toc_off;
13191 break;
13192 }
13193
13194 /* Make sure the whole pasted function uses the same toc offset. */
13195 if (toc_off != 0)
13196 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13197 htab->sec_info[i->id].toc_off = toc_off;
13198 }
13199 return true;
13200 }
13201
13202 bool
13203 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13204 {
13205 bool ret1 = check_pasted_section (info, ".init");
13206 bool ret2 = check_pasted_section (info, ".fini");
13207
13208 return ret1 && ret2;
13209 }
13210
13211 /* See whether we can group stub sections together. Grouping stub
13212 sections may result in fewer stubs. More importantly, we need to
13213 put all .init* and .fini* stubs at the beginning of the .init or
13214 .fini output sections respectively, because glibc splits the
13215 _init and _fini functions into multiple parts. Putting a stub in
13216 the middle of a function is not a good idea. */
13217
13218 static bool
13219 group_sections (struct bfd_link_info *info,
13220 bfd_size_type stub_group_size,
13221 bool stubs_always_before_branch)
13222 {
13223 struct ppc_link_hash_table *htab;
13224 asection *osec;
13225 bool suppress_size_errors;
13226
13227 htab = ppc_hash_table (info);
13228 if (htab == NULL)
13229 return false;
13230
13231 suppress_size_errors = false;
13232 if (stub_group_size == 1)
13233 {
13234 /* Default values. */
13235 if (stubs_always_before_branch)
13236 stub_group_size = 0x1e00000;
13237 else
13238 stub_group_size = 0x1c00000;
13239 suppress_size_errors = true;
13240 }
13241
13242 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13243 {
13244 asection *tail;
13245
13246 if (osec->id >= htab->sec_info_arr_size)
13247 continue;
13248
13249 tail = htab->sec_info[osec->id].u.list;
13250 while (tail != NULL)
13251 {
13252 asection *curr;
13253 asection *prev;
13254 bfd_size_type total;
13255 bool big_sec;
13256 bfd_vma curr_toc;
13257 struct map_stub *group;
13258 bfd_size_type group_size;
13259
13260 curr = tail;
13261 total = tail->size;
13262 group_size = (ppc64_elf_section_data (tail) != NULL
13263 && ppc64_elf_section_data (tail)->has_14bit_branch
13264 ? stub_group_size >> 10 : stub_group_size);
13265
13266 big_sec = total > group_size;
13267 if (big_sec && !suppress_size_errors)
13268 /* xgettext:c-format */
13269 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13270 tail->owner, tail);
13271 curr_toc = htab->sec_info[tail->id].toc_off;
13272
13273 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13274 && ((total += curr->output_offset - prev->output_offset)
13275 < (ppc64_elf_section_data (prev) != NULL
13276 && ppc64_elf_section_data (prev)->has_14bit_branch
13277 ? (group_size = stub_group_size >> 10) : group_size))
13278 && htab->sec_info[prev->id].toc_off == curr_toc)
13279 curr = prev;
13280
13281 /* OK, the size from the start of CURR to the end is less
13282 than group_size and thus can be handled by one stub
13283 section. (or the tail section is itself larger than
13284 group_size, in which case we may be toast.) We should
13285 really be keeping track of the total size of stubs added
13286 here, as stubs contribute to the final output section
13287 size. That's a little tricky, and this way will only
13288 break if stubs added make the total size more than 2^25,
13289 ie. for the default stub_group_size, if stubs total more
13290 than 2097152 bytes, or nearly 75000 plt call stubs. */
13291 group = bfd_alloc (curr->owner, sizeof (*group));
13292 if (group == NULL)
13293 return false;
13294 group->link_sec = curr;
13295 group->stub_sec = NULL;
13296 group->needs_save_res = 0;
13297 group->lr_restore = 0;
13298 group->eh_size = 0;
13299 group->eh_base = 0;
13300 group->next = htab->group;
13301 htab->group = group;
13302 do
13303 {
13304 prev = htab->sec_info[tail->id].u.list;
13305 /* Set up this stub group. */
13306 htab->sec_info[tail->id].u.group = group;
13307 }
13308 while (tail != curr && (tail = prev) != NULL);
13309
13310 /* But wait, there's more! Input sections up to group_size
13311 bytes before the stub section can be handled by it too.
13312 Don't do this if we have a really large section after the
13313 stubs, as adding more stubs increases the chance that
13314 branches may not reach into the stub section. */
13315 if (!stubs_always_before_branch && !big_sec)
13316 {
13317 total = 0;
13318 while (prev != NULL
13319 && ((total += tail->output_offset - prev->output_offset)
13320 < (ppc64_elf_section_data (prev) != NULL
13321 && ppc64_elf_section_data (prev)->has_14bit_branch
13322 ? (group_size = stub_group_size >> 10)
13323 : group_size))
13324 && htab->sec_info[prev->id].toc_off == curr_toc)
13325 {
13326 tail = prev;
13327 prev = htab->sec_info[tail->id].u.list;
13328 htab->sec_info[tail->id].u.group = group;
13329 }
13330 }
13331 tail = prev;
13332 }
13333 }
13334 return true;
13335 }
13336
13337 static const unsigned char glink_eh_frame_cie[] =
13338 {
13339 0, 0, 0, 16, /* length. */
13340 0, 0, 0, 0, /* id. */
13341 1, /* CIE version. */
13342 'z', 'R', 0, /* Augmentation string. */
13343 4, /* Code alignment. */
13344 0x78, /* Data alignment. */
13345 65, /* RA reg. */
13346 1, /* Augmentation size. */
13347 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13348 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13349 };
13350
13351 /* Stripping output sections is normally done before dynamic section
13352 symbols have been allocated. This function is called later, and
13353 handles cases like htab->brlt which is mapped to its own output
13354 section. */
13355
13356 static void
13357 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13358 {
13359 if (isec->size == 0
13360 && isec->output_section->size == 0
13361 && !(isec->output_section->flags & SEC_KEEP)
13362 && !bfd_section_removed_from_list (info->output_bfd,
13363 isec->output_section)
13364 && elf_section_data (isec->output_section)->dynindx == 0)
13365 {
13366 isec->output_section->flags |= SEC_EXCLUDE;
13367 bfd_section_list_remove (info->output_bfd, isec->output_section);
13368 info->output_bfd->section_count--;
13369 }
13370 }
13371
13372 static int
13373 compare_relr_address (const void *arg1, const void *arg2)
13374 {
13375 bfd_vma a = *(bfd_vma *) arg1;
13376 bfd_vma b = *(bfd_vma *) arg2;
13377 return a < b ? -1 : a > b ? 1 : 0;
13378 }
13379
13380 static bool
13381 append_relr_off (struct ppc_link_hash_table *htab, bfd_vma off)
13382 {
13383 if (htab->relr_count >= htab->relr_alloc)
13384 {
13385 if (htab->relr_alloc == 0)
13386 htab->relr_alloc = 4096;
13387 else
13388 htab->relr_alloc *= 2;
13389 htab->relr_addr
13390 = bfd_realloc (htab->relr_addr,
13391 htab->relr_alloc * sizeof (htab->relr_addr[0]));
13392 if (htab->relr_addr == NULL)
13393 return false;
13394 }
13395 htab->relr_addr[htab->relr_count++] = off;
13396 return true;
13397 }
13398
13399 static bool
13400 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13401 {
13402 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13403 bfd *ibfd;
13404
13405 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13406 {
13407 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13408 struct plt_entry **local_plt, **lplt, **end_local_plt;
13409 Elf_Internal_Shdr *symtab_hdr;
13410 bfd_size_type locsymcount;
13411 Elf_Internal_Sym *local_syms = NULL;
13412 struct plt_entry *pent;
13413 struct got_entry *gent;
13414
13415 if (!is_ppc64_elf (ibfd))
13416 continue;
13417
13418 lgot_ents = elf_local_got_ents (ibfd);
13419 if (!lgot_ents)
13420 continue;
13421
13422 symtab_hdr = &elf_symtab_hdr (ibfd);
13423 locsymcount = symtab_hdr->sh_info;
13424 end_lgot_ents = lgot_ents + locsymcount;
13425 local_plt = (struct plt_entry **) end_lgot_ents;
13426 end_local_plt = local_plt + locsymcount;
13427 for (lgot = lgot_ents; lgot < end_lgot_ents; ++lgot)
13428 for (gent = *lgot; gent != NULL; gent = gent->next)
13429 if (!gent->is_indirect
13430 && gent->tls_type == 0
13431 && gent->got.offset != (bfd_vma) -1)
13432 {
13433 asection *got = ppc64_elf_tdata (gent->owner)->got;
13434 bfd_vma r_offset = (got->output_section->vma
13435 + got->output_offset
13436 + gent->got.offset);
13437 if (!append_relr_off (htab, r_offset))
13438 {
13439 htab->stub_error = true;
13440 return false;
13441 }
13442 }
13443
13444 if (!htab->opd_abi)
13445 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13446 for (pent = *lplt; pent != NULL; pent = pent->next)
13447 if (pent->plt.offset != (bfd_vma) -1)
13448 {
13449 Elf_Internal_Sym *sym;
13450
13451 if (!get_sym_h (NULL, &sym, NULL, NULL, &local_syms,
13452 lplt - local_plt, ibfd))
13453 {
13454 err_exit:
13455 if (symtab_hdr->contents != (unsigned char *) local_syms)
13456 free (local_syms);
13457 return false;
13458 }
13459
13460 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13461 {
13462 bfd_vma r_offset = (pent->plt.offset
13463 + htab->pltlocal->output_offset
13464 + htab->pltlocal->output_section->vma);
13465 if (!append_relr_off (htab, r_offset))
13466 goto err_exit;
13467 }
13468 }
13469
13470 if (local_syms != NULL
13471 && symtab_hdr->contents != (unsigned char *) local_syms)
13472 {
13473 if (!info->keep_memory)
13474 free (local_syms);
13475 else
13476 symtab_hdr->contents = (unsigned char *) local_syms;
13477 }
13478 }
13479 return true;
13480 }
13481
13482 static bool
13483 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13484 {
13485 struct bfd_link_info *info;
13486 struct ppc_link_hash_table *htab;
13487 struct plt_entry *pent;
13488 struct got_entry *gent;
13489
13490 if (h->root.type == bfd_link_hash_indirect)
13491 return true;
13492
13493 info = (struct bfd_link_info *) inf;
13494 htab = ppc_hash_table (info);
13495 if (htab == NULL)
13496 return false;
13497
13498 if (h->type != STT_GNU_IFUNC
13499 && h->def_regular
13500 && (h->root.type == bfd_link_hash_defined
13501 || h->root.type == bfd_link_hash_defweak))
13502 {
13503 if (!htab->elf.dynamic_sections_created
13504 || h->dynindx == -1
13505 || SYMBOL_REFERENCES_LOCAL (info, h))
13506 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13507 if (!gent->is_indirect
13508 && gent->tls_type == 0
13509 && gent->got.offset != (bfd_vma) -1)
13510 {
13511 asection *got = ppc64_elf_tdata (gent->owner)->got;
13512 bfd_vma r_offset = (got->output_section->vma
13513 + got->output_offset
13514 + gent->got.offset);
13515 if (!append_relr_off (htab, r_offset))
13516 {
13517 htab->stub_error = true;
13518 return false;
13519 }
13520 }
13521
13522 if (!htab->opd_abi
13523 && use_local_plt (info, h))
13524 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13525 if (pent->plt.offset != (bfd_vma) -1)
13526 {
13527 bfd_vma r_offset = (htab->pltlocal->output_section->vma
13528 + htab->pltlocal->output_offset
13529 + pent->plt.offset);
13530 if (!append_relr_off (htab, r_offset))
13531 {
13532 htab->stub_error = true;
13533 return false;
13534 }
13535 }
13536 }
13537 return true;
13538 }
13539
13540 /* Determine and set the size of the stub section for a final link.
13541
13542 The basic idea here is to examine all the relocations looking for
13543 PC-relative calls to a target that is unreachable with a "bl"
13544 instruction. */
13545
13546 bool
13547 ppc64_elf_size_stubs (struct bfd_link_info *info)
13548 {
13549 bfd_size_type stub_group_size;
13550 bool stubs_always_before_branch;
13551 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13552
13553 if (htab == NULL)
13554 return false;
13555
13556 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13557 htab->params->power10_stubs = 0;
13558
13559 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13560 htab->params->plt_thread_safe = 1;
13561 if (!htab->opd_abi)
13562 htab->params->plt_thread_safe = 0;
13563 else if (htab->params->plt_thread_safe == -1)
13564 {
13565 static const char *const thread_starter[] =
13566 {
13567 "pthread_create",
13568 /* libstdc++ */
13569 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13570 /* librt */
13571 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13572 "mq_notify", "create_timer",
13573 /* libanl */
13574 "getaddrinfo_a",
13575 /* libgomp */
13576 "GOMP_parallel",
13577 "GOMP_parallel_start",
13578 "GOMP_parallel_loop_static",
13579 "GOMP_parallel_loop_static_start",
13580 "GOMP_parallel_loop_dynamic",
13581 "GOMP_parallel_loop_dynamic_start",
13582 "GOMP_parallel_loop_guided",
13583 "GOMP_parallel_loop_guided_start",
13584 "GOMP_parallel_loop_runtime",
13585 "GOMP_parallel_loop_runtime_start",
13586 "GOMP_parallel_sections",
13587 "GOMP_parallel_sections_start",
13588 /* libgo */
13589 "__go_go",
13590 };
13591 unsigned i;
13592
13593 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13594 {
13595 struct elf_link_hash_entry *h;
13596 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13597 false, false, true);
13598 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13599 if (htab->params->plt_thread_safe)
13600 break;
13601 }
13602 }
13603 stubs_always_before_branch = htab->params->group_size < 0;
13604 if (htab->params->group_size < 0)
13605 stub_group_size = -htab->params->group_size;
13606 else
13607 stub_group_size = htab->params->group_size;
13608
13609 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13610 return false;
13611
13612 htab->tga_group = NULL;
13613 if (!htab->params->no_tls_get_addr_regsave
13614 && htab->tga_desc_fd != NULL
13615 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13616 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13617 && htab->tls_get_addr_fd != NULL
13618 && is_static_defined (&htab->tls_get_addr_fd->elf))
13619 {
13620 asection *sym_sec, *code_sec, *stub_sec;
13621 bfd_vma sym_value;
13622 struct _opd_sec_data *opd;
13623
13624 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13625 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13626 code_sec = sym_sec;
13627 opd = get_opd_info (sym_sec);
13628 if (opd != NULL)
13629 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13630 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13631 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13632 htab->tga_group->link_sec);
13633 if (stub_sec == NULL)
13634 return false;
13635 htab->tga_group->stub_sec = stub_sec;
13636
13637 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13638 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13639 htab->tga_desc_fd->elf.root.u.def.value = 0;
13640 htab->tga_desc_fd->elf.type = STT_FUNC;
13641 htab->tga_desc_fd->elf.def_regular = 1;
13642 htab->tga_desc_fd->elf.non_elf = 0;
13643 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13644 }
13645
13646 #define STUB_SHRINK_ITER 20
13647 /* Loop until no stubs added. After iteration 20 of this loop we may
13648 exit on a stub section shrinking. This is to break out of a
13649 pathological case where adding stubs on one iteration decreases
13650 section gaps (perhaps due to alignment), which then requires
13651 fewer or smaller stubs on the next iteration. */
13652
13653 while (1)
13654 {
13655 bfd *input_bfd;
13656 unsigned int bfd_indx;
13657 struct map_stub *group;
13658
13659 htab->stub_iteration += 1;
13660 htab->relr_count = 0;
13661
13662 for (input_bfd = info->input_bfds, bfd_indx = 0;
13663 input_bfd != NULL;
13664 input_bfd = input_bfd->link.next, bfd_indx++)
13665 {
13666 Elf_Internal_Shdr *symtab_hdr;
13667 asection *section;
13668 Elf_Internal_Sym *local_syms = NULL;
13669
13670 if (!is_ppc64_elf (input_bfd))
13671 continue;
13672
13673 /* We'll need the symbol table in a second. */
13674 symtab_hdr = &elf_symtab_hdr (input_bfd);
13675 if (symtab_hdr->sh_info == 0)
13676 continue;
13677
13678 /* Walk over each section attached to the input bfd. */
13679 for (section = input_bfd->sections;
13680 section != NULL;
13681 section = section->next)
13682 {
13683 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13684 bool is_opd;
13685
13686 /* If there aren't any relocs, then there's nothing more
13687 to do. */
13688 if ((section->flags & SEC_RELOC) == 0
13689 || (section->flags & SEC_ALLOC) == 0
13690 || (section->flags & SEC_LOAD) == 0
13691 || section->reloc_count == 0)
13692 continue;
13693
13694 if (!info->enable_dt_relr
13695 && (section->flags & SEC_CODE) == 0)
13696 continue;
13697
13698 /* If this section is a link-once section that will be
13699 discarded, then don't create any stubs. */
13700 if (section->output_section == NULL
13701 || section->output_section->owner != info->output_bfd)
13702 continue;
13703
13704 /* Get the relocs. */
13705 internal_relocs
13706 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13707 info->keep_memory);
13708 if (internal_relocs == NULL)
13709 goto error_ret_free_local;
13710
13711 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13712
13713 /* Now examine each relocation. */
13714 irela = internal_relocs;
13715 irelaend = irela + section->reloc_count;
13716 for (; irela < irelaend; irela++)
13717 {
13718 enum elf_ppc64_reloc_type r_type;
13719 unsigned int r_indx;
13720 struct ppc_stub_type stub_type;
13721 struct ppc_stub_hash_entry *stub_entry;
13722 asection *sym_sec, *code_sec;
13723 bfd_vma sym_value, code_value;
13724 bfd_vma destination;
13725 unsigned long local_off;
13726 bool ok_dest;
13727 struct ppc_link_hash_entry *hash;
13728 struct ppc_link_hash_entry *fdh;
13729 struct elf_link_hash_entry *h;
13730 Elf_Internal_Sym *sym;
13731 char *stub_name;
13732 const asection *id_sec;
13733 struct _opd_sec_data *opd;
13734 struct plt_entry *plt_ent;
13735
13736 r_type = ELF64_R_TYPE (irela->r_info);
13737 r_indx = ELF64_R_SYM (irela->r_info);
13738
13739 if (r_type >= R_PPC64_max)
13740 {
13741 bfd_set_error (bfd_error_bad_value);
13742 goto error_ret_free_internal;
13743 }
13744
13745 /* Only look for stubs on branch instructions. */
13746 switch (r_type)
13747 {
13748 default:
13749 continue;
13750
13751 case R_PPC64_REL24:
13752 case R_PPC64_REL24_NOTOC:
13753 case R_PPC64_REL24_P9NOTOC:
13754 case R_PPC64_REL14:
13755 case R_PPC64_REL14_BRTAKEN:
13756 case R_PPC64_REL14_BRNTAKEN:
13757 if ((section->flags & SEC_CODE) != 0)
13758 break;
13759 continue;
13760
13761 case R_PPC64_ADDR64:
13762 case R_PPC64_TOC:
13763 if (info->enable_dt_relr
13764 && irela->r_offset % 2 == 0
13765 && section->alignment_power != 0)
13766 break;
13767 continue;
13768 }
13769
13770 /* Now determine the call target, its name, value,
13771 section. */
13772 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13773 r_indx, input_bfd))
13774 goto error_ret_free_internal;
13775
13776 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13777 {
13778 /* Only locally defined symbols can possibly use
13779 relative relocations. */
13780 bfd_vma r_offset;
13781 if ((sym_sec == NULL
13782 || sym_sec->output_section == NULL)
13783 /* No symbol is OK too. */
13784 && !(sym != NULL && sym->st_shndx == 0)
13785 /* Hack for __ehdr_start, which is undefined
13786 at this point. */
13787 && !(h != NULL && h->root.linker_def))
13788 continue;
13789 if (NO_OPD_RELOCS && is_opd)
13790 continue;
13791 if (!is_opd
13792 && r_type == R_PPC64_ADDR64)
13793 {
13794 if (h != NULL
13795 ? h->type == STT_GNU_IFUNC
13796 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13797 continue;
13798 if (h != NULL
13799 && !SYMBOL_REFERENCES_LOCAL (info, h))
13800 continue;
13801 }
13802 r_offset = _bfd_elf_section_offset (info->output_bfd,
13803 info,
13804 section,
13805 irela->r_offset);
13806 if (r_offset >= (bfd_vma) -2)
13807 continue;
13808 r_offset += (section->output_section->vma
13809 + section->output_offset);
13810 if (!append_relr_off (htab, r_offset))
13811 goto error_ret_free_internal;
13812 continue;
13813 }
13814
13815 hash = ppc_elf_hash_entry (h);
13816 ok_dest = false;
13817 fdh = NULL;
13818 sym_value = 0;
13819 if (hash == NULL)
13820 {
13821 sym_value = sym->st_value;
13822 if (sym_sec != NULL
13823 && sym_sec->output_section != NULL)
13824 ok_dest = true;
13825 }
13826 else if (hash->elf.root.type == bfd_link_hash_defined
13827 || hash->elf.root.type == bfd_link_hash_defweak)
13828 {
13829 sym_value = hash->elf.root.u.def.value;
13830 if (sym_sec->output_section != NULL)
13831 ok_dest = true;
13832 }
13833 else if (hash->elf.root.type == bfd_link_hash_undefweak
13834 || hash->elf.root.type == bfd_link_hash_undefined)
13835 {
13836 /* Recognise an old ABI func code entry sym, and
13837 use the func descriptor sym instead if it is
13838 defined. */
13839 if (hash->elf.root.root.string[0] == '.'
13840 && hash->oh != NULL)
13841 {
13842 fdh = ppc_follow_link (hash->oh);
13843 if (fdh->elf.root.type == bfd_link_hash_defined
13844 || fdh->elf.root.type == bfd_link_hash_defweak)
13845 {
13846 sym_sec = fdh->elf.root.u.def.section;
13847 sym_value = fdh->elf.root.u.def.value;
13848 if (sym_sec->output_section != NULL)
13849 ok_dest = true;
13850 }
13851 else
13852 fdh = NULL;
13853 }
13854 }
13855 else
13856 {
13857 bfd_set_error (bfd_error_bad_value);
13858 goto error_ret_free_internal;
13859 }
13860
13861 destination = 0;
13862 local_off = 0;
13863 if (ok_dest)
13864 {
13865 sym_value += irela->r_addend;
13866 destination = (sym_value
13867 + sym_sec->output_offset
13868 + sym_sec->output_section->vma);
13869 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13870 ? hash->elf.other
13871 : sym->st_other);
13872 }
13873
13874 code_sec = sym_sec;
13875 code_value = sym_value;
13876 opd = get_opd_info (sym_sec);
13877 if (opd != NULL)
13878 {
13879 bfd_vma dest;
13880
13881 if (hash == NULL && opd->adjust != NULL)
13882 {
13883 long adjust = opd->adjust[OPD_NDX (sym_value)];
13884 if (adjust == -1)
13885 continue;
13886 code_value += adjust;
13887 sym_value += adjust;
13888 }
13889 dest = opd_entry_value (sym_sec, sym_value,
13890 &code_sec, &code_value, false);
13891 if (dest != (bfd_vma) -1)
13892 {
13893 destination = dest;
13894 if (fdh != NULL)
13895 {
13896 /* Fixup old ABI sym to point at code
13897 entry. */
13898 hash->elf.root.type = bfd_link_hash_defweak;
13899 hash->elf.root.u.def.section = code_sec;
13900 hash->elf.root.u.def.value = code_value;
13901 }
13902 }
13903 }
13904
13905 /* Determine what (if any) linker stub is needed. */
13906 plt_ent = NULL;
13907 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13908 &plt_ent, destination,
13909 local_off);
13910 stub_type.sub = ppc_stub_toc;
13911 stub_type.r2save = 0;
13912
13913 if (r_type == R_PPC64_REL24_NOTOC
13914 || r_type == R_PPC64_REL24_P9NOTOC)
13915 {
13916 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13917 if (htab->params->power10_stubs == 0
13918 || (r_type == R_PPC64_REL24_P9NOTOC
13919 && htab->params->power10_stubs != 1))
13920 notoc = ppc_stub_p9notoc;
13921 if (stub_type.main == ppc_stub_plt_call)
13922 stub_type.sub = notoc;
13923 else if (stub_type.main == ppc_stub_long_branch
13924 || (code_sec != NULL
13925 && code_sec->output_section != NULL
13926 && (((hash ? hash->elf.other : sym->st_other)
13927 & STO_PPC64_LOCAL_MASK)
13928 > 1 << STO_PPC64_LOCAL_BIT)))
13929 {
13930 stub_type.main = ppc_stub_long_branch;
13931 stub_type.sub = notoc;
13932 stub_type.r2save = 0;
13933 }
13934 }
13935 else if (stub_type.main != ppc_stub_plt_call)
13936 {
13937 /* Check whether we need a TOC adjusting stub.
13938 Since the linker pastes together pieces from
13939 different object files when creating the
13940 _init and _fini functions, it may be that a
13941 call to what looks like a local sym is in
13942 fact a call needing a TOC adjustment. */
13943 if ((code_sec != NULL
13944 && code_sec->output_section != NULL
13945 && (code_sec->has_toc_reloc
13946 || code_sec->makes_toc_func_call)
13947 && (htab->sec_info[code_sec->id].toc_off
13948 != htab->sec_info[section->id].toc_off))
13949 || (((hash ? hash->elf.other : sym->st_other)
13950 & STO_PPC64_LOCAL_MASK)
13951 == 1 << STO_PPC64_LOCAL_BIT))
13952 {
13953 stub_type.main = ppc_stub_long_branch;
13954 stub_type.sub = ppc_stub_toc;
13955 stub_type.r2save = 1;
13956 }
13957 }
13958
13959 if (stub_type.main == ppc_stub_none)
13960 continue;
13961
13962 /* __tls_get_addr calls might be eliminated. */
13963 if (stub_type.main != ppc_stub_plt_call
13964 && hash != NULL
13965 && is_tls_get_addr (&hash->elf, htab)
13966 && section->has_tls_reloc
13967 && irela != internal_relocs)
13968 {
13969 /* Get tls info. */
13970 unsigned char *tls_mask;
13971
13972 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13973 irela - 1, input_bfd))
13974 goto error_ret_free_internal;
13975 if ((*tls_mask & TLS_TLS) != 0
13976 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
13977 continue;
13978 }
13979
13980 if (stub_type.main == ppc_stub_plt_call
13981 && stub_type.sub == ppc_stub_toc)
13982 {
13983 if (!htab->opd_abi
13984 && htab->params->plt_localentry0 != 0
13985 && is_elfv2_localentry0 (&hash->elf))
13986 htab->has_plt_localentry0 = 1;
13987 else if (irela + 1 < irelaend
13988 && irela[1].r_offset == irela->r_offset + 4
13989 && (ELF64_R_TYPE (irela[1].r_info)
13990 == R_PPC64_TOCSAVE))
13991 {
13992 if (!tocsave_find (htab, INSERT,
13993 &local_syms, irela + 1, input_bfd))
13994 goto error_ret_free_internal;
13995 }
13996 else
13997 stub_type.r2save = 1;
13998 }
13999
14000 /* Support for grouping stub sections. */
14001 id_sec = htab->sec_info[section->id].u.group->link_sec;
14002
14003 /* Get the name of this stub. */
14004 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14005 if (!stub_name)
14006 goto error_ret_free_internal;
14007
14008 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14009 stub_name, false, false);
14010 if (stub_entry != NULL)
14011 {
14012 free (stub_name);
14013 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14014 {
14015 /* xgettext:c-format */
14016 _bfd_error_handler
14017 (_("%pB: cannot create stub entry %s"),
14018 section->owner, stub_entry->root.string);
14019 goto error_ret_free_internal;
14020 }
14021 continue;
14022 }
14023
14024 stub_entry = ppc_add_stub (stub_name, section, info);
14025 if (stub_entry == NULL)
14026 {
14027 free (stub_name);
14028 error_ret_free_internal:
14029 if (elf_section_data (section)->relocs == NULL)
14030 free (internal_relocs);
14031 error_ret_free_local:
14032 if (symtab_hdr->contents
14033 != (unsigned char *) local_syms)
14034 free (local_syms);
14035 return false;
14036 }
14037
14038 stub_entry->type = stub_type;
14039 if (stub_type.main == ppc_stub_plt_call)
14040 {
14041 stub_entry->target_value = sym_value;
14042 stub_entry->target_section = sym_sec;
14043 }
14044 else
14045 {
14046 stub_entry->target_value = code_value;
14047 stub_entry->target_section = code_sec;
14048 }
14049 stub_entry->h = hash;
14050 stub_entry->plt_ent = plt_ent;
14051 stub_entry->symtype
14052 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14053 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14054
14055 if (hash != NULL
14056 && (hash->elf.root.type == bfd_link_hash_defined
14057 || hash->elf.root.type == bfd_link_hash_defweak))
14058 htab->stub_globals += 1;
14059 }
14060
14061 /* We're done with the internal relocs, free them. */
14062 if (elf_section_data (section)->relocs != internal_relocs)
14063 free (internal_relocs);
14064 }
14065
14066 if (local_syms != NULL
14067 && symtab_hdr->contents != (unsigned char *) local_syms)
14068 {
14069 if (!info->keep_memory)
14070 free (local_syms);
14071 else
14072 symtab_hdr->contents = (unsigned char *) local_syms;
14073 }
14074 }
14075
14076 /* We may have added some stubs. Find out the new size of the
14077 stub sections. */
14078 for (group = htab->group; group != NULL; group = group->next)
14079 {
14080 group->lr_restore = 0;
14081 group->eh_size = 0;
14082 if (group->stub_sec != NULL)
14083 {
14084 asection *stub_sec = group->stub_sec;
14085
14086 if (htab->stub_iteration <= STUB_SHRINK_ITER
14087 || stub_sec->rawsize < stub_sec->size)
14088 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
14089 stub_sec->rawsize = stub_sec->size;
14090 stub_sec->size = 0;
14091 stub_sec->reloc_count = 0;
14092 stub_sec->flags &= ~SEC_RELOC;
14093 }
14094 }
14095 if (htab->tga_group != NULL)
14096 {
14097 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14098 htab->tga_group->eh_size
14099 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14100 htab->tga_group->lr_restore = 23 * 4;
14101 htab->tga_group->stub_sec->size = 24 * 4;
14102 }
14103
14104 if (htab->stub_iteration <= STUB_SHRINK_ITER
14105 || htab->brlt->rawsize < htab->brlt->size)
14106 htab->brlt->rawsize = htab->brlt->size;
14107 htab->brlt->size = 0;
14108 htab->brlt->reloc_count = 0;
14109 htab->brlt->flags &= ~SEC_RELOC;
14110 if (htab->relbrlt != NULL)
14111 htab->relbrlt->size = 0;
14112
14113 if (htab->elf.srelrdyn != NULL)
14114 {
14115 if (htab->stub_iteration <= STUB_SHRINK_ITER
14116 || htab->elf.srelrdyn->rawsize < htab->elf.srelrdyn->size)
14117 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14118 htab->elf.srelrdyn->size = 0;
14119 }
14120
14121 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14122
14123 for (group = htab->group; group != NULL; group = group->next)
14124 if (group->needs_save_res)
14125 group->stub_sec->size += htab->sfpr->size;
14126
14127 if (info->emitrelocations
14128 && htab->glink != NULL && htab->glink->size != 0)
14129 {
14130 htab->glink->reloc_count = 1;
14131 htab->glink->flags |= SEC_RELOC;
14132 }
14133
14134 if (htab->glink_eh_frame != NULL
14135 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14136 && htab->glink_eh_frame->output_section->size > 8)
14137 {
14138 size_t size = 0, align = 4;
14139
14140 for (group = htab->group; group != NULL; group = group->next)
14141 if (group->eh_size != 0)
14142 size += (group->eh_size + 17 + align - 1) & -align;
14143 if (htab->glink != NULL && htab->glink->size != 0)
14144 size += (24 + align - 1) & -align;
14145 if (size != 0)
14146 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14147 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14148 size = (size + align - 1) & -align;
14149 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14150 htab->glink_eh_frame->size = size;
14151 }
14152
14153 if (htab->params->plt_stub_align != 0)
14154 for (group = htab->group; group != NULL; group = group->next)
14155 if (group->stub_sec != NULL)
14156 {
14157 int align = abs (htab->params->plt_stub_align);
14158 group->stub_sec->size
14159 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14160 }
14161
14162 if (htab->elf.srelrdyn != NULL)
14163 {
14164 bfd_vma r_offset;
14165
14166 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14167 if (!append_relr_off (htab, (r_offset
14168 + htab->brlt->output_section->vma
14169 + htab->brlt->output_offset)))
14170 return false;
14171
14172 if (!got_and_plt_relr_for_local_syms (info))
14173 return false;
14174 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14175 if (htab->stub_error)
14176 return false;
14177
14178 if (htab->relr_count > 1)
14179 qsort (htab->relr_addr, htab->relr_count, sizeof (*htab->relr_addr),
14180 compare_relr_address);
14181
14182 size_t i = 0;
14183 while (i < htab->relr_count)
14184 {
14185 bfd_vma base = htab->relr_addr[i];
14186 htab->elf.srelrdyn->size += 8;
14187 i++;
14188 /* Handle possible duplicate address. This can happen
14189 as sections increase in size when adding stubs. */
14190 while (i < htab->relr_count
14191 && htab->relr_addr[i] == base)
14192 i++;
14193 base += 8;
14194 while (1)
14195 {
14196 size_t start_i = i;
14197 while (i < htab->relr_count
14198 && htab->relr_addr[i] - base < 63 * 8
14199 && (htab->relr_addr[i] - base) % 8 == 0)
14200 i++;
14201 if (i == start_i)
14202 break;
14203 htab->elf.srelrdyn->size += 8;
14204 base += 63 * 8;
14205 }
14206 }
14207 }
14208
14209 for (group = htab->group; group != NULL; group = group->next)
14210 if (group->stub_sec != NULL
14211 && group->stub_sec->rawsize != group->stub_sec->size
14212 && (htab->stub_iteration <= STUB_SHRINK_ITER
14213 || group->stub_sec->rawsize < group->stub_sec->size))
14214 break;
14215
14216 if (group == NULL
14217 && (htab->brlt->rawsize == htab->brlt->size
14218 || (htab->stub_iteration > STUB_SHRINK_ITER
14219 && htab->brlt->rawsize > htab->brlt->size))
14220 && (htab->elf.srelrdyn == NULL
14221 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14222 || (htab->stub_iteration > STUB_SHRINK_ITER
14223 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14224 && (htab->glink_eh_frame == NULL
14225 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14226 && (htab->tga_group == NULL
14227 || htab->stub_iteration > 1))
14228 break;
14229
14230 /* Ask the linker to do its stuff. */
14231 (*htab->params->layout_sections_again) ();
14232 }
14233
14234 if (htab->glink_eh_frame != NULL
14235 && htab->glink_eh_frame->size != 0)
14236 {
14237 bfd_vma val;
14238 bfd_byte *p, *last_fde;
14239 size_t last_fde_len, size, align, pad;
14240 struct map_stub *group;
14241
14242 /* It is necessary to at least have a rough outline of the
14243 linker generated CIEs and FDEs written before
14244 bfd_elf_discard_info is run, in order for these FDEs to be
14245 indexed in .eh_frame_hdr. */
14246 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14247 if (p == NULL)
14248 return false;
14249 htab->glink_eh_frame->contents = p;
14250 last_fde = p;
14251 align = 4;
14252
14253 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14254 /* CIE length (rewrite in case little-endian). */
14255 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14256 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14257 p += last_fde_len + 4;
14258
14259 for (group = htab->group; group != NULL; group = group->next)
14260 if (group->eh_size != 0)
14261 {
14262 group->eh_base = p - htab->glink_eh_frame->contents;
14263 last_fde = p;
14264 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14265 /* FDE length. */
14266 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14267 p += 4;
14268 /* CIE pointer. */
14269 val = p - htab->glink_eh_frame->contents;
14270 bfd_put_32 (htab->elf.dynobj, val, p);
14271 p += 4;
14272 /* Offset to stub section, written later. */
14273 p += 4;
14274 /* stub section size. */
14275 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14276 p += 4;
14277 /* Augmentation. */
14278 p += 1;
14279 /* Make sure we don't have all nops. This is enough for
14280 elf-eh-frame.c to detect the last non-nop opcode. */
14281 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14282 p = last_fde + last_fde_len + 4;
14283 }
14284 if (htab->glink != NULL && htab->glink->size != 0)
14285 {
14286 last_fde = p;
14287 last_fde_len = ((24 + align - 1) & -align) - 4;
14288 /* FDE length. */
14289 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14290 p += 4;
14291 /* CIE pointer. */
14292 val = p - htab->glink_eh_frame->contents;
14293 bfd_put_32 (htab->elf.dynobj, val, p);
14294 p += 4;
14295 /* Offset to .glink, written later. */
14296 p += 4;
14297 /* .glink size. */
14298 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14299 p += 4;
14300 /* Augmentation. */
14301 p += 1;
14302
14303 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14304 *p++ = DW_CFA_register;
14305 *p++ = 65;
14306 *p++ = htab->opd_abi ? 12 : 0;
14307 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14308 *p++ = DW_CFA_restore_extended;
14309 *p++ = 65;
14310 p += ((24 + align - 1) & -align) - 24;
14311 }
14312 /* Subsume any padding into the last FDE if user .eh_frame
14313 sections are aligned more than glink_eh_frame. Otherwise any
14314 zero padding will be seen as a terminator. */
14315 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14316 size = p - htab->glink_eh_frame->contents;
14317 pad = ((size + align - 1) & -align) - size;
14318 htab->glink_eh_frame->size = size + pad;
14319 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14320 }
14321
14322 maybe_strip_output (info, htab->brlt);
14323 if (htab->relbrlt != NULL)
14324 maybe_strip_output (info, htab->relbrlt);
14325 if (htab->glink_eh_frame != NULL)
14326 maybe_strip_output (info, htab->glink_eh_frame);
14327 if (htab->elf.srelrdyn != NULL)
14328 maybe_strip_output (info, htab->elf.srelrdyn);
14329
14330 return true;
14331 }
14332
14333 /* Called after we have determined section placement. If sections
14334 move, we'll be called again. Provide a value for TOCstart. */
14335
14336 bfd_vma
14337 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14338 {
14339 asection *s;
14340 bfd_vma TOCstart, adjust;
14341
14342 if (info != NULL)
14343 {
14344 struct elf_link_hash_entry *h;
14345 struct elf_link_hash_table *htab = elf_hash_table (info);
14346
14347 if (is_elf_hash_table (&htab->root)
14348 && htab->hgot != NULL)
14349 h = htab->hgot;
14350 else
14351 {
14352 h = (struct elf_link_hash_entry *)
14353 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14354 if (is_elf_hash_table (&htab->root))
14355 htab->hgot = h;
14356 }
14357 if (h != NULL
14358 && h->root.type == bfd_link_hash_defined
14359 && !h->root.linker_def
14360 && (!is_elf_hash_table (&htab->root)
14361 || h->def_regular))
14362 {
14363 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14364 _bfd_set_gp_value (obfd, TOCstart);
14365 return TOCstart;
14366 }
14367 }
14368
14369 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14370 order. The TOC starts where the first of these sections starts. */
14371 s = bfd_get_section_by_name (obfd, ".got");
14372 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14373 s = bfd_get_section_by_name (obfd, ".toc");
14374 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14375 s = bfd_get_section_by_name (obfd, ".tocbss");
14376 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14377 s = bfd_get_section_by_name (obfd, ".plt");
14378 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14379 {
14380 /* This may happen for
14381 o references to TOC base (SYM@toc / TOC[tc0]) without a
14382 .toc directive
14383 o bad linker script
14384 o --gc-sections and empty TOC sections
14385
14386 FIXME: Warn user? */
14387
14388 /* Look for a likely section. We probably won't even be
14389 using TOCstart. */
14390 for (s = obfd->sections; s != NULL; s = s->next)
14391 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14392 | SEC_EXCLUDE))
14393 == (SEC_ALLOC | SEC_SMALL_DATA))
14394 break;
14395 if (s == NULL)
14396 for (s = obfd->sections; s != NULL; s = s->next)
14397 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14398 == (SEC_ALLOC | SEC_SMALL_DATA))
14399 break;
14400 if (s == NULL)
14401 for (s = obfd->sections; s != NULL; s = s->next)
14402 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14403 == SEC_ALLOC)
14404 break;
14405 if (s == NULL)
14406 for (s = obfd->sections; s != NULL; s = s->next)
14407 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14408 break;
14409 }
14410
14411 TOCstart = 0;
14412 if (s != NULL)
14413 TOCstart = s->output_section->vma + s->output_offset;
14414
14415 /* Force alignment. */
14416 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14417 TOCstart -= adjust;
14418 _bfd_set_gp_value (obfd, TOCstart);
14419
14420 if (info != NULL && s != NULL)
14421 {
14422 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14423
14424 if (htab != NULL)
14425 {
14426 if (htab->elf.hgot != NULL)
14427 {
14428 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14429 htab->elf.hgot->root.u.def.section = s;
14430 }
14431 }
14432 else
14433 {
14434 struct bfd_link_hash_entry *bh = NULL;
14435 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14436 s, TOC_BASE_OFF - adjust,
14437 NULL, false, false, &bh);
14438 }
14439 }
14440 return TOCstart;
14441 }
14442
14443 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14444 write out any global entry stubs, and PLT relocations. */
14445
14446 static bool
14447 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14448 {
14449 struct bfd_link_info *info;
14450 struct ppc_link_hash_table *htab;
14451 struct plt_entry *ent;
14452 asection *s;
14453
14454 if (h->root.type == bfd_link_hash_indirect)
14455 return true;
14456
14457 info = inf;
14458 htab = ppc_hash_table (info);
14459 if (htab == NULL)
14460 return false;
14461
14462 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14463 if (ent->plt.offset != (bfd_vma) -1)
14464 {
14465 /* This symbol has an entry in the procedure linkage
14466 table. Set it up. */
14467 Elf_Internal_Rela rela;
14468 asection *plt, *relplt;
14469 bfd_byte *loc;
14470
14471 if (use_local_plt (info, h))
14472 {
14473 if (!(h->def_regular
14474 && (h->root.type == bfd_link_hash_defined
14475 || h->root.type == bfd_link_hash_defweak)))
14476 continue;
14477 if (h->type == STT_GNU_IFUNC)
14478 {
14479 plt = htab->elf.iplt;
14480 relplt = htab->elf.irelplt;
14481 htab->elf.ifunc_resolvers = true;
14482 if (htab->opd_abi)
14483 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14484 else
14485 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14486 }
14487 else
14488 {
14489 plt = htab->pltlocal;
14490 relplt = NULL;
14491 if (bfd_link_pic (info)
14492 && !(info->enable_dt_relr && !htab->opd_abi))
14493 {
14494 relplt = htab->relpltlocal;
14495 if (htab->opd_abi)
14496 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14497 else
14498 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14499 }
14500 }
14501 rela.r_addend = defined_sym_val (h) + ent->addend;
14502
14503 if (relplt == NULL)
14504 {
14505 loc = plt->contents + ent->plt.offset;
14506 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14507 if (htab->opd_abi)
14508 {
14509 bfd_vma toc = elf_gp (info->output_bfd);
14510 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14511 bfd_put_64 (info->output_bfd, toc, loc + 8);
14512 }
14513 }
14514 else
14515 {
14516 rela.r_offset = (plt->output_section->vma
14517 + plt->output_offset
14518 + ent->plt.offset);
14519 loc = relplt->contents + (relplt->reloc_count++
14520 * sizeof (Elf64_External_Rela));
14521 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14522 }
14523 }
14524 else
14525 {
14526 rela.r_offset = (htab->elf.splt->output_section->vma
14527 + htab->elf.splt->output_offset
14528 + ent->plt.offset);
14529 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14530 rela.r_addend = ent->addend;
14531 loc = (htab->elf.srelplt->contents
14532 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14533 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14534 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14535 htab->elf.ifunc_resolvers = true;
14536 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14537 }
14538 }
14539
14540 if (!h->pointer_equality_needed)
14541 return true;
14542
14543 if (h->def_regular)
14544 return true;
14545
14546 s = htab->global_entry;
14547 if (s == NULL || s->size == 0)
14548 return true;
14549
14550 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14551 if (ent->plt.offset != (bfd_vma) -1
14552 && ent->addend == 0)
14553 {
14554 bfd_byte *p;
14555 asection *plt;
14556 bfd_vma off;
14557
14558 p = s->contents + h->root.u.def.value;
14559 plt = htab->elf.splt;
14560 if (use_local_plt (info, h))
14561 {
14562 if (h->type == STT_GNU_IFUNC)
14563 plt = htab->elf.iplt;
14564 else
14565 plt = htab->pltlocal;
14566 }
14567 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14568 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14569
14570 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14571 {
14572 info->callbacks->einfo
14573 (_("%P: linkage table error against `%pT'\n"),
14574 h->root.root.string);
14575 bfd_set_error (bfd_error_bad_value);
14576 htab->stub_error = true;
14577 }
14578
14579 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14580 if (htab->params->emit_stub_syms)
14581 {
14582 size_t len = strlen (h->root.root.string);
14583 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14584
14585 if (name == NULL)
14586 return false;
14587
14588 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14589 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14590 if (h == NULL)
14591 return false;
14592 if (h->root.type == bfd_link_hash_new)
14593 {
14594 h->root.type = bfd_link_hash_defined;
14595 h->root.u.def.section = s;
14596 h->root.u.def.value = p - s->contents;
14597 h->ref_regular = 1;
14598 h->def_regular = 1;
14599 h->ref_regular_nonweak = 1;
14600 h->forced_local = 1;
14601 h->non_elf = 0;
14602 h->root.linker_def = 1;
14603 }
14604 }
14605
14606 if (PPC_HA (off) != 0)
14607 {
14608 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14609 p += 4;
14610 }
14611 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14612 p += 4;
14613 bfd_put_32 (s->owner, MTCTR_R12, p);
14614 p += 4;
14615 bfd_put_32 (s->owner, BCTR, p);
14616 break;
14617 }
14618 return true;
14619 }
14620
14621 /* Write PLT relocs for locals. */
14622
14623 static bool
14624 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14625 {
14626 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14627 bfd *ibfd;
14628
14629 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14630 {
14631 struct got_entry **lgot_ents, **end_lgot_ents;
14632 struct plt_entry **local_plt, **lplt, **end_local_plt;
14633 Elf_Internal_Shdr *symtab_hdr;
14634 bfd_size_type locsymcount;
14635 Elf_Internal_Sym *local_syms = NULL;
14636 struct plt_entry *ent;
14637
14638 if (!is_ppc64_elf (ibfd))
14639 continue;
14640
14641 lgot_ents = elf_local_got_ents (ibfd);
14642 if (!lgot_ents)
14643 continue;
14644
14645 symtab_hdr = &elf_symtab_hdr (ibfd);
14646 locsymcount = symtab_hdr->sh_info;
14647 end_lgot_ents = lgot_ents + locsymcount;
14648 local_plt = (struct plt_entry **) end_lgot_ents;
14649 end_local_plt = local_plt + locsymcount;
14650 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14651 for (ent = *lplt; ent != NULL; ent = ent->next)
14652 if (ent->plt.offset != (bfd_vma) -1)
14653 {
14654 Elf_Internal_Sym *sym;
14655 asection *sym_sec;
14656 asection *plt, *relplt;
14657 bfd_byte *loc;
14658 bfd_vma val;
14659
14660 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14661 lplt - local_plt, ibfd))
14662 {
14663 if (symtab_hdr->contents != (unsigned char *) local_syms)
14664 free (local_syms);
14665 return false;
14666 }
14667
14668 val = sym->st_value + ent->addend;
14669 if (sym_sec != NULL && sym_sec->output_section != NULL)
14670 val += sym_sec->output_offset + sym_sec->output_section->vma;
14671
14672 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14673 {
14674 htab->elf.ifunc_resolvers = true;
14675 plt = htab->elf.iplt;
14676 relplt = htab->elf.irelplt;
14677 }
14678 else
14679 {
14680 plt = htab->pltlocal;
14681 relplt = NULL;
14682 if (bfd_link_pic (info)
14683 && !(info->enable_dt_relr && !htab->opd_abi))
14684 relplt = htab->relpltlocal;
14685 }
14686
14687 if (relplt == NULL)
14688 {
14689 loc = plt->contents + ent->plt.offset;
14690 bfd_put_64 (info->output_bfd, val, loc);
14691 if (htab->opd_abi)
14692 {
14693 bfd_vma toc = elf_gp (ibfd);
14694 bfd_put_64 (info->output_bfd, toc, loc + 8);
14695 }
14696 }
14697 else
14698 {
14699 Elf_Internal_Rela rela;
14700 rela.r_offset = (ent->plt.offset
14701 + plt->output_offset
14702 + plt->output_section->vma);
14703 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14704 {
14705 if (htab->opd_abi)
14706 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14707 else
14708 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14709 }
14710 else
14711 {
14712 if (htab->opd_abi)
14713 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14714 else
14715 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14716 }
14717 rela.r_addend = val;
14718 loc = relplt->contents + (relplt->reloc_count++
14719 * sizeof (Elf64_External_Rela));
14720 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14721 }
14722 }
14723
14724 if (local_syms != NULL
14725 && symtab_hdr->contents != (unsigned char *) local_syms)
14726 {
14727 if (!info->keep_memory)
14728 free (local_syms);
14729 else
14730 symtab_hdr->contents = (unsigned char *) local_syms;
14731 }
14732 }
14733 return true;
14734 }
14735
14736 /* Emit the static wrapper function preserving registers around a
14737 __tls_get_addr_opt call. */
14738
14739 static bool
14740 emit_tga_desc (struct ppc_link_hash_table *htab)
14741 {
14742 asection *stub_sec = htab->tga_group->stub_sec;
14743 unsigned int cfa_updt = 11 * 4;
14744 bfd_byte *p;
14745 bfd_vma to, from, delta;
14746
14747 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14748 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14749 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14750 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14751 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14752 delta = to - from;
14753 if (delta + (1 << 25) >= 1 << 26)
14754 {
14755 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14756 htab->stub_error = true;
14757 return false;
14758 }
14759
14760 p = stub_sec->contents;
14761 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14762 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14763 p += 4;
14764 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14765 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14766 }
14767
14768 /* Emit eh_frame describing the static wrapper function. */
14769
14770 static bfd_byte *
14771 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14772 {
14773 unsigned int cfa_updt = 11 * 4;
14774 unsigned int i;
14775
14776 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14777 *p++ = DW_CFA_def_cfa_offset;
14778 if (htab->opd_abi)
14779 {
14780 *p++ = 128;
14781 *p++ = 1;
14782 }
14783 else
14784 *p++ = 96;
14785 *p++ = DW_CFA_offset_extended_sf;
14786 *p++ = 65;
14787 *p++ = (-16 / 8) & 0x7f;
14788 for (i = 4; i < 12; i++)
14789 {
14790 *p++ = DW_CFA_offset + i;
14791 *p++ = (htab->opd_abi ? 13 : 12) - i;
14792 }
14793 *p++ = DW_CFA_advance_loc + 10;
14794 *p++ = DW_CFA_def_cfa_offset;
14795 *p++ = 0;
14796 for (i = 4; i < 12; i++)
14797 *p++ = DW_CFA_restore + i;
14798 *p++ = DW_CFA_advance_loc + 2;
14799 *p++ = DW_CFA_restore_extended;
14800 *p++ = 65;
14801 return p;
14802 }
14803
14804 /* Build all the stubs associated with the current output file.
14805 The stubs are kept in a hash table attached to the main linker
14806 hash table. This function is called via gldelf64ppc_finish. */
14807
14808 bool
14809 ppc64_elf_build_stubs (struct bfd_link_info *info,
14810 char **stats)
14811 {
14812 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14813 struct map_stub *group;
14814 asection *stub_sec;
14815 bfd_byte *p;
14816 int stub_sec_count = 0;
14817
14818 if (htab == NULL)
14819 return false;
14820
14821 /* Allocate memory to hold the linker stubs. */
14822 for (group = htab->group; group != NULL; group = group->next)
14823 {
14824 group->eh_size = 0;
14825 group->lr_restore = 0;
14826 if ((stub_sec = group->stub_sec) != NULL
14827 && stub_sec->size != 0)
14828 {
14829 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14830 stub_sec->size);
14831 if (stub_sec->contents == NULL)
14832 return false;
14833 stub_sec->size = 0;
14834 }
14835 }
14836
14837 if (htab->glink != NULL && htab->glink->size != 0)
14838 {
14839 unsigned int indx;
14840 bfd_vma plt0;
14841
14842 /* Build the .glink plt call stub. */
14843 if (htab->params->emit_stub_syms)
14844 {
14845 struct elf_link_hash_entry *h;
14846 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14847 true, false, false);
14848 if (h == NULL)
14849 return false;
14850 if (h->root.type == bfd_link_hash_new)
14851 {
14852 h->root.type = bfd_link_hash_defined;
14853 h->root.u.def.section = htab->glink;
14854 h->root.u.def.value = 8;
14855 h->ref_regular = 1;
14856 h->def_regular = 1;
14857 h->ref_regular_nonweak = 1;
14858 h->forced_local = 1;
14859 h->non_elf = 0;
14860 h->root.linker_def = 1;
14861 }
14862 }
14863 plt0 = (htab->elf.splt->output_section->vma
14864 + htab->elf.splt->output_offset
14865 - 16);
14866 if (info->emitrelocations)
14867 {
14868 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14869 if (r == NULL)
14870 return false;
14871 r->r_offset = (htab->glink->output_offset
14872 + htab->glink->output_section->vma);
14873 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14874 r->r_addend = plt0;
14875 }
14876 p = htab->glink->contents;
14877 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14878 bfd_put_64 (htab->glink->owner, plt0, p);
14879 p += 8;
14880 if (htab->opd_abi)
14881 {
14882 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14883 p += 4;
14884 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14885 p += 4;
14886 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14887 p += 4;
14888 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14889 p += 4;
14890 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14891 p += 4;
14892 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14893 p += 4;
14894 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14895 p += 4;
14896 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14897 p += 4;
14898 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14899 p += 4;
14900 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14901 p += 4;
14902 }
14903 else
14904 {
14905 unsigned int insn;
14906
14907 /* 0:
14908 . .quad plt0-1f # plt0 entry relative to 1:
14909 #
14910 # We get here with r12 initially @ a glink branch
14911 # Load the address of _dl_runtime_resolve from plt0 and
14912 # jump to it, with r0 set to the index of the PLT entry
14913 # to be resolved and r11 the link map.
14914 __glink_PLTresolve:
14915 . std %r2,24(%r1) # optional
14916 . mflr %r0
14917 . bcl 20,31,1f
14918 1:
14919 . mflr %r11
14920 . mtlr %r0
14921 . ld %r0,(0b-1b)(%r11)
14922 . sub %r12,%r12,%r11
14923 . add %r11,%r0,%r11
14924 . addi %r0,%r12,1b-2f
14925 . ld %r12,0(%r11)
14926 . srdi %r0,%r0,2
14927 . mtctr %r12
14928 . ld %r11,8(%r11)
14929 . bctr
14930 2:
14931 . b __glink_PLTresolve
14932 . ...
14933 . b __glink_PLTresolve */
14934
14935 if (htab->has_plt_localentry0)
14936 {
14937 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14938 p += 4;
14939 }
14940 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14941 p += 4;
14942 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14943 p += 4;
14944 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14945 p += 4;
14946 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14947 p += 4;
14948 if (htab->has_plt_localentry0)
14949 insn = LD_R0_0R11 | (-20 & 0xfffc);
14950 else
14951 insn = LD_R0_0R11 | (-16 & 0xfffc);
14952 bfd_put_32 (htab->glink->owner, insn, p);
14953 p += 4;
14954 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14955 p += 4;
14956 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
14957 p += 4;
14958 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
14959 p += 4;
14960 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14961 p += 4;
14962 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14963 p += 4;
14964 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14965 p += 4;
14966 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14967 p += 4;
14968 }
14969 bfd_put_32 (htab->glink->owner, BCTR, p);
14970 p += 4;
14971 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14972
14973 /* Build the .glink lazy link call stubs. */
14974 indx = 0;
14975 while (p < htab->glink->contents + htab->glink->size)
14976 {
14977 if (htab->opd_abi)
14978 {
14979 if (indx < 0x8000)
14980 {
14981 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14982 p += 4;
14983 }
14984 else
14985 {
14986 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14987 p += 4;
14988 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14989 p);
14990 p += 4;
14991 }
14992 }
14993 bfd_put_32 (htab->glink->owner,
14994 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14995 indx++;
14996 p += 4;
14997 }
14998 }
14999
15000 if (htab->tga_group != NULL)
15001 {
15002 htab->tga_group->lr_restore = 23 * 4;
15003 htab->tga_group->stub_sec->size = 24 * 4;
15004 if (!emit_tga_desc (htab))
15005 return false;
15006 if (htab->glink_eh_frame != NULL
15007 && htab->glink_eh_frame->size != 0)
15008 {
15009 size_t align = 4;
15010
15011 p = htab->glink_eh_frame->contents;
15012 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15013 p += 17;
15014 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15015 }
15016 }
15017
15018 /* Build .glink global entry stubs, and PLT relocs for globals. */
15019 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15020
15021 if (!write_plt_relocs_for_local_syms (info))
15022 return false;
15023
15024 if (htab->brlt != NULL && htab->brlt->size != 0)
15025 {
15026 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15027 htab->brlt->size);
15028 if (htab->brlt->contents == NULL)
15029 return false;
15030 }
15031 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15032 {
15033 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15034 htab->relbrlt->size);
15035 if (htab->relbrlt->contents == NULL)
15036 return false;
15037 }
15038
15039 /* Build the stubs as directed by the stub hash table. */
15040 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15041
15042 for (group = htab->group; group != NULL; group = group->next)
15043 if (group->needs_save_res)
15044 group->stub_sec->size += htab->sfpr->size;
15045
15046 if (htab->relbrlt != NULL)
15047 htab->relbrlt->reloc_count = 0;
15048
15049 if (htab->params->plt_stub_align != 0)
15050 for (group = htab->group; group != NULL; group = group->next)
15051 if ((stub_sec = group->stub_sec) != NULL)
15052 {
15053 int align = abs (htab->params->plt_stub_align);
15054 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15055 }
15056
15057 for (group = htab->group; group != NULL; group = group->next)
15058 if (group->needs_save_res)
15059 {
15060 stub_sec = group->stub_sec;
15061 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15062 htab->sfpr->contents, htab->sfpr->size);
15063 if (htab->params->emit_stub_syms)
15064 {
15065 unsigned int i;
15066
15067 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15068 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15069 return false;
15070 }
15071 }
15072
15073 if (htab->glink_eh_frame != NULL
15074 && htab->glink_eh_frame->size != 0)
15075 {
15076 bfd_vma val;
15077 size_t align = 4;
15078
15079 p = htab->glink_eh_frame->contents;
15080 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15081
15082 for (group = htab->group; group != NULL; group = group->next)
15083 if (group->eh_size != 0)
15084 {
15085 /* Offset to stub section. */
15086 val = (group->stub_sec->output_section->vma
15087 + group->stub_sec->output_offset);
15088 val -= (htab->glink_eh_frame->output_section->vma
15089 + htab->glink_eh_frame->output_offset
15090 + (p + 8 - htab->glink_eh_frame->contents));
15091 if (val + 0x80000000 > 0xffffffff)
15092 {
15093 _bfd_error_handler
15094 (_("%s offset too large for .eh_frame sdata4 encoding"),
15095 group->stub_sec->name);
15096 return false;
15097 }
15098 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15099 p += (group->eh_size + 17 + 3) & -4;
15100 }
15101 if (htab->glink != NULL && htab->glink->size != 0)
15102 {
15103 /* Offset to .glink. */
15104 val = (htab->glink->output_section->vma
15105 + htab->glink->output_offset
15106 + 8);
15107 val -= (htab->glink_eh_frame->output_section->vma
15108 + htab->glink_eh_frame->output_offset
15109 + (p + 8 - htab->glink_eh_frame->contents));
15110 if (val + 0x80000000 > 0xffffffff)
15111 {
15112 _bfd_error_handler
15113 (_("%s offset too large for .eh_frame sdata4 encoding"),
15114 htab->glink->name);
15115 return false;
15116 }
15117 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15118 p += (24 + align - 1) & -align;
15119 }
15120 }
15121
15122 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15123 {
15124 htab->elf.srelrdyn->contents
15125 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15126 if (htab->elf.srelrdyn->contents == NULL)
15127 return false;
15128
15129 size_t i = 0;
15130 bfd_byte *loc = htab->elf.srelrdyn->contents;
15131 while (i < htab->relr_count)
15132 {
15133 bfd_vma base = htab->relr_addr[i];
15134 BFD_ASSERT (base % 2 == 0);
15135 bfd_put_64 (htab->elf.dynobj, base, loc);
15136 loc += 8;
15137 i++;
15138 while (i < htab->relr_count
15139 && htab->relr_addr[i] == base)
15140 {
15141 htab->stub_error = true;
15142 i++;
15143 }
15144 base += 8;
15145 while (1)
15146 {
15147 bfd_vma bits = 0;
15148 while (i < htab->relr_count
15149 && htab->relr_addr[i] - base < 63 * 8
15150 && (htab->relr_addr[i] - base) % 8 == 0)
15151 {
15152 bits |= (bfd_vma) 1 << ((htab->relr_addr[i] - base) / 8);
15153 i++;
15154 }
15155 if (bits == 0)
15156 break;
15157 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15158 loc += 8;
15159 base += 63 * 8;
15160 }
15161 }
15162 /* Pad any excess with 1's, a do-nothing encoding. */
15163 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15164 < htab->elf.srelrdyn->size)
15165 {
15166 bfd_put_64 (htab->elf.dynobj, 1, loc);
15167 loc += 8;
15168 }
15169 }
15170
15171 for (group = htab->group; group != NULL; group = group->next)
15172 if ((stub_sec = group->stub_sec) != NULL)
15173 {
15174 stub_sec_count += 1;
15175 if (stub_sec->rawsize != stub_sec->size
15176 && (htab->stub_iteration <= STUB_SHRINK_ITER
15177 || stub_sec->rawsize < stub_sec->size))
15178 break;
15179 }
15180
15181 if (group != NULL)
15182 htab->stub_error = true;
15183
15184 if (htab->stub_error)
15185 {
15186 _bfd_error_handler (_("stubs don't match calculated size"));
15187 return false;
15188 }
15189
15190 if (stats != NULL)
15191 {
15192 char *groupmsg;
15193 if (asprintf (&groupmsg,
15194 ngettext ("linker stubs in %u group\n",
15195 "linker stubs in %u groups\n",
15196 stub_sec_count),
15197 stub_sec_count) < 0)
15198 *stats = NULL;
15199 else
15200 {
15201 if (asprintf (stats, _("%s"
15202 " branch %lu\n"
15203 " long branch %lu\n"
15204 " plt call %lu\n"
15205 " global entry %lu"),
15206 groupmsg,
15207 htab->stub_count[ppc_stub_long_branch - 1],
15208 htab->stub_count[ppc_stub_plt_branch - 1],
15209 htab->stub_count[ppc_stub_plt_call - 1],
15210 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15211 *stats = NULL;
15212 free (groupmsg);
15213 }
15214 }
15215 return true;
15216 }
15217
15218 /* What to do when ld finds relocations against symbols defined in
15219 discarded sections. */
15220
15221 static unsigned int
15222 ppc64_elf_action_discarded (asection *sec)
15223 {
15224 if (strcmp (".opd", sec->name) == 0)
15225 return 0;
15226
15227 if (strcmp (".toc", sec->name) == 0)
15228 return 0;
15229
15230 if (strcmp (".toc1", sec->name) == 0)
15231 return 0;
15232
15233 return _bfd_elf_default_action_discarded (sec);
15234 }
15235
15236 /* These are the dynamic relocations supported by glibc. */
15237
15238 static bool
15239 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15240 {
15241 switch (r_type)
15242 {
15243 case R_PPC64_RELATIVE:
15244 case R_PPC64_NONE:
15245 case R_PPC64_ADDR64:
15246 case R_PPC64_GLOB_DAT:
15247 case R_PPC64_IRELATIVE:
15248 case R_PPC64_JMP_IREL:
15249 case R_PPC64_JMP_SLOT:
15250 case R_PPC64_DTPMOD64:
15251 case R_PPC64_DTPREL64:
15252 case R_PPC64_TPREL64:
15253 case R_PPC64_TPREL16_LO_DS:
15254 case R_PPC64_TPREL16_DS:
15255 case R_PPC64_TPREL16:
15256 case R_PPC64_TPREL16_LO:
15257 case R_PPC64_TPREL16_HI:
15258 case R_PPC64_TPREL16_HIGH:
15259 case R_PPC64_TPREL16_HA:
15260 case R_PPC64_TPREL16_HIGHA:
15261 case R_PPC64_TPREL16_HIGHER:
15262 case R_PPC64_TPREL16_HIGHEST:
15263 case R_PPC64_TPREL16_HIGHERA:
15264 case R_PPC64_TPREL16_HIGHESTA:
15265 case R_PPC64_ADDR16_LO_DS:
15266 case R_PPC64_ADDR16_LO:
15267 case R_PPC64_ADDR16_HI:
15268 case R_PPC64_ADDR16_HIGH:
15269 case R_PPC64_ADDR16_HA:
15270 case R_PPC64_ADDR16_HIGHA:
15271 case R_PPC64_REL30:
15272 case R_PPC64_COPY:
15273 case R_PPC64_UADDR64:
15274 case R_PPC64_UADDR32:
15275 case R_PPC64_ADDR32:
15276 case R_PPC64_ADDR24:
15277 case R_PPC64_ADDR16:
15278 case R_PPC64_UADDR16:
15279 case R_PPC64_ADDR16_DS:
15280 case R_PPC64_ADDR16_HIGHER:
15281 case R_PPC64_ADDR16_HIGHEST:
15282 case R_PPC64_ADDR16_HIGHERA:
15283 case R_PPC64_ADDR16_HIGHESTA:
15284 case R_PPC64_ADDR14:
15285 case R_PPC64_ADDR14_BRTAKEN:
15286 case R_PPC64_ADDR14_BRNTAKEN:
15287 case R_PPC64_REL32:
15288 case R_PPC64_REL64:
15289 return true;
15290
15291 default:
15292 return false;
15293 }
15294 }
15295
15296 /* The RELOCATE_SECTION function is called by the ELF backend linker
15297 to handle the relocations for a section.
15298
15299 The relocs are always passed as Rela structures; if the section
15300 actually uses Rel structures, the r_addend field will always be
15301 zero.
15302
15303 This function is responsible for adjust the section contents as
15304 necessary, and (if using Rela relocs and generating a
15305 relocatable output file) adjusting the reloc addend as
15306 necessary.
15307
15308 This function does not have to worry about setting the reloc
15309 address or the reloc symbol index.
15310
15311 LOCAL_SYMS is a pointer to the swapped in local symbols.
15312
15313 LOCAL_SECTIONS is an array giving the section in the input file
15314 corresponding to the st_shndx field of each local symbol.
15315
15316 The global hash table entry for the global symbols can be found
15317 via elf_sym_hashes (input_bfd).
15318
15319 When generating relocatable output, this function must handle
15320 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15321 going to be the section symbol corresponding to the output
15322 section, which means that the addend must be adjusted
15323 accordingly. */
15324
15325 static int
15326 ppc64_elf_relocate_section (bfd *output_bfd,
15327 struct bfd_link_info *info,
15328 bfd *input_bfd,
15329 asection *input_section,
15330 bfd_byte *contents,
15331 Elf_Internal_Rela *relocs,
15332 Elf_Internal_Sym *local_syms,
15333 asection **local_sections)
15334 {
15335 struct ppc_link_hash_table *htab;
15336 Elf_Internal_Shdr *symtab_hdr;
15337 struct elf_link_hash_entry **sym_hashes;
15338 Elf_Internal_Rela *rel;
15339 Elf_Internal_Rela *wrel;
15340 Elf_Internal_Rela *relend;
15341 Elf_Internal_Rela outrel;
15342 bfd_byte *loc;
15343 struct got_entry **local_got_ents;
15344 bfd_vma TOCstart;
15345 bool ret = true;
15346 bool is_opd;
15347 /* Assume 'at' branch hints. */
15348 bool is_isa_v2 = true;
15349 bool warned_dynamic = false;
15350 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15351
15352 /* Initialize howto table if needed. */
15353 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15354 ppc_howto_init ();
15355
15356 htab = ppc_hash_table (info);
15357 if (htab == NULL)
15358 return false;
15359
15360 /* Don't relocate stub sections. */
15361 if (input_section->owner == htab->params->stub_bfd)
15362 return true;
15363
15364 if (!is_ppc64_elf (input_bfd))
15365 {
15366 bfd_set_error (bfd_error_wrong_format);
15367 return false;
15368 }
15369
15370 local_got_ents = elf_local_got_ents (input_bfd);
15371 TOCstart = elf_gp (output_bfd);
15372 symtab_hdr = &elf_symtab_hdr (input_bfd);
15373 sym_hashes = elf_sym_hashes (input_bfd);
15374 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15375
15376 rel = wrel = relocs;
15377 relend = relocs + input_section->reloc_count;
15378 for (; rel < relend; wrel++, rel++)
15379 {
15380 enum elf_ppc64_reloc_type r_type;
15381 bfd_vma addend;
15382 bfd_reloc_status_type r;
15383 Elf_Internal_Sym *sym;
15384 asection *sec;
15385 struct elf_link_hash_entry *h_elf;
15386 struct ppc_link_hash_entry *h;
15387 struct ppc_link_hash_entry *fdh;
15388 const char *sym_name;
15389 unsigned long r_symndx, toc_symndx;
15390 bfd_vma toc_addend;
15391 unsigned char tls_mask, tls_gd, tls_type;
15392 unsigned char sym_type;
15393 bfd_vma relocation;
15394 bool unresolved_reloc, save_unresolved_reloc;
15395 bool warned;
15396 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15397 unsigned int insn;
15398 unsigned int mask;
15399 struct ppc_stub_hash_entry *stub_entry;
15400 bfd_vma max_br_offset;
15401 bfd_vma from;
15402 Elf_Internal_Rela orig_rel;
15403 reloc_howto_type *howto;
15404 struct reloc_howto_struct alt_howto;
15405 uint64_t pinsn;
15406 bfd_vma offset;
15407
15408 again:
15409 orig_rel = *rel;
15410
15411 r_type = ELF64_R_TYPE (rel->r_info);
15412 r_symndx = ELF64_R_SYM (rel->r_info);
15413
15414 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15415 symbol of the previous ADDR64 reloc. The symbol gives us the
15416 proper TOC base to use. */
15417 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15418 && wrel != relocs
15419 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15420 && is_opd)
15421 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15422
15423 sym = NULL;
15424 sec = NULL;
15425 h_elf = NULL;
15426 sym_name = NULL;
15427 unresolved_reloc = false;
15428 warned = false;
15429
15430 if (r_symndx < symtab_hdr->sh_info)
15431 {
15432 /* It's a local symbol. */
15433 struct _opd_sec_data *opd;
15434
15435 sym = local_syms + r_symndx;
15436 sec = local_sections[r_symndx];
15437 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15438 sym_type = ELF64_ST_TYPE (sym->st_info);
15439 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15440 opd = get_opd_info (sec);
15441 if (opd != NULL && opd->adjust != NULL)
15442 {
15443 long adjust = opd->adjust[OPD_NDX (sym->st_value
15444 + rel->r_addend)];
15445 if (adjust == -1)
15446 relocation = 0;
15447 else
15448 {
15449 /* If this is a relocation against the opd section sym
15450 and we have edited .opd, adjust the reloc addend so
15451 that ld -r and ld --emit-relocs output is correct.
15452 If it is a reloc against some other .opd symbol,
15453 then the symbol value will be adjusted later. */
15454 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15455 rel->r_addend += adjust;
15456 else
15457 relocation += adjust;
15458 }
15459 }
15460 }
15461 else
15462 {
15463 bool ignored;
15464
15465 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15466 r_symndx, symtab_hdr, sym_hashes,
15467 h_elf, sec, relocation,
15468 unresolved_reloc, warned, ignored);
15469 sym_name = h_elf->root.root.string;
15470 sym_type = h_elf->type;
15471 if (sec != NULL
15472 && sec->owner == output_bfd
15473 && strcmp (sec->name, ".opd") == 0)
15474 {
15475 /* This is a symbol defined in a linker script. All
15476 such are defined in output sections, even those
15477 defined by simple assignment from a symbol defined in
15478 an input section. Transfer the symbol to an
15479 appropriate input .opd section, so that a branch to
15480 this symbol will be mapped to the location specified
15481 by the opd entry. */
15482 struct bfd_link_order *lo;
15483 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15484 if (lo->type == bfd_indirect_link_order)
15485 {
15486 asection *isec = lo->u.indirect.section;
15487 if (h_elf->root.u.def.value >= isec->output_offset
15488 && h_elf->root.u.def.value < (isec->output_offset
15489 + isec->size))
15490 {
15491 h_elf->root.u.def.value -= isec->output_offset;
15492 h_elf->root.u.def.section = isec;
15493 sec = isec;
15494 break;
15495 }
15496 }
15497 }
15498 }
15499 h = ppc_elf_hash_entry (h_elf);
15500
15501 if (sec != NULL && discarded_section (sec))
15502 {
15503 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15504 input_bfd, input_section,
15505 contents, rel->r_offset);
15506 wrel->r_offset = rel->r_offset;
15507 wrel->r_info = 0;
15508 wrel->r_addend = 0;
15509
15510 /* For ld -r, remove relocations in debug sections against
15511 symbols defined in discarded sections. Not done for
15512 non-debug to preserve relocs in .eh_frame which the
15513 eh_frame editing code expects to be present. */
15514 if (bfd_link_relocatable (info)
15515 && (input_section->flags & SEC_DEBUGGING))
15516 wrel--;
15517
15518 continue;
15519 }
15520
15521 if (bfd_link_relocatable (info))
15522 goto copy_reloc;
15523
15524 if (h != NULL && &h->elf == htab->elf.hgot)
15525 {
15526 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15527 sec = bfd_abs_section_ptr;
15528 unresolved_reloc = false;
15529 }
15530
15531 /* TLS optimizations. Replace instruction sequences and relocs
15532 based on information we collected in tls_optimize. We edit
15533 RELOCS so that --emit-relocs will output something sensible
15534 for the final instruction stream. */
15535 tls_mask = 0;
15536 tls_gd = 0;
15537 toc_symndx = 0;
15538 if (h != NULL)
15539 tls_mask = h->tls_mask;
15540 else if (local_got_ents != NULL)
15541 {
15542 struct plt_entry **local_plt = (struct plt_entry **)
15543 (local_got_ents + symtab_hdr->sh_info);
15544 unsigned char *lgot_masks = (unsigned char *)
15545 (local_plt + symtab_hdr->sh_info);
15546 tls_mask = lgot_masks[r_symndx];
15547 }
15548 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15549 && (r_type == R_PPC64_TLS
15550 || r_type == R_PPC64_TLSGD
15551 || r_type == R_PPC64_TLSLD))
15552 {
15553 /* Check for toc tls entries. */
15554 unsigned char *toc_tls;
15555
15556 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15557 &local_syms, rel, input_bfd))
15558 return false;
15559
15560 if (toc_tls)
15561 tls_mask = *toc_tls;
15562 }
15563
15564 /* Check that tls relocs are used with tls syms, and non-tls
15565 relocs are used with non-tls syms. */
15566 if (r_symndx != STN_UNDEF
15567 && r_type != R_PPC64_NONE
15568 && (h == NULL
15569 || h->elf.root.type == bfd_link_hash_defined
15570 || h->elf.root.type == bfd_link_hash_defweak)
15571 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15572 {
15573 if ((tls_mask & TLS_TLS) != 0
15574 && (r_type == R_PPC64_TLS
15575 || r_type == R_PPC64_TLSGD
15576 || r_type == R_PPC64_TLSLD))
15577 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15578 ;
15579 else
15580 info->callbacks->einfo
15581 (!IS_PPC64_TLS_RELOC (r_type)
15582 /* xgettext:c-format */
15583 ? _("%H: %s used with TLS symbol `%pT'\n")
15584 /* xgettext:c-format */
15585 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15586 input_bfd, input_section, rel->r_offset,
15587 ppc64_elf_howto_table[r_type]->name,
15588 sym_name);
15589 }
15590
15591 /* Ensure reloc mapping code below stays sane. */
15592 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15593 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15594 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15595 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15596 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15597 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15598 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15599 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15600 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15601 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15602 abort ();
15603
15604 switch (r_type)
15605 {
15606 default:
15607 break;
15608
15609 case R_PPC64_LO_DS_OPT:
15610 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15611 {
15612 insn = bfd_get_32 (input_bfd,
15613 contents + rel->r_offset - d_offset);
15614 if ((insn & (0x3fu << 26)) != 58u << 26)
15615 abort ();
15616 insn += (14u << 26) - (58u << 26);
15617 bfd_put_32 (input_bfd, insn,
15618 contents + rel->r_offset - d_offset);
15619 r_type = R_PPC64_TOC16_LO;
15620 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15621 }
15622 break;
15623
15624 case R_PPC64_TOC16:
15625 case R_PPC64_TOC16_LO:
15626 case R_PPC64_TOC16_DS:
15627 case R_PPC64_TOC16_LO_DS:
15628 {
15629 /* Check for toc tls entries. */
15630 unsigned char *toc_tls;
15631 int retval;
15632
15633 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15634 &local_syms, rel, input_bfd);
15635 if (retval == 0)
15636 return false;
15637
15638 if (toc_tls)
15639 {
15640 tls_mask = *toc_tls;
15641 if (r_type == R_PPC64_TOC16_DS
15642 || r_type == R_PPC64_TOC16_LO_DS)
15643 {
15644 if ((tls_mask & TLS_TLS) != 0
15645 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15646 goto toctprel;
15647 }
15648 else
15649 {
15650 /* If we found a GD reloc pair, then we might be
15651 doing a GD->IE transition. */
15652 if (retval == 2)
15653 {
15654 tls_gd = TLS_GDIE;
15655 if ((tls_mask & TLS_TLS) != 0
15656 && (tls_mask & TLS_GD) == 0)
15657 goto tls_ldgd_opt;
15658 }
15659 else if (retval == 3)
15660 {
15661 if ((tls_mask & TLS_TLS) != 0
15662 && (tls_mask & TLS_LD) == 0)
15663 goto tls_ldgd_opt;
15664 }
15665 }
15666 }
15667 }
15668 break;
15669
15670 case R_PPC64_GOT_TPREL16_HI:
15671 case R_PPC64_GOT_TPREL16_HA:
15672 if ((tls_mask & TLS_TLS) != 0
15673 && (tls_mask & TLS_TPREL) == 0
15674 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15675 {
15676 rel->r_offset -= d_offset;
15677 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15678 r_type = R_PPC64_NONE;
15679 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15680 }
15681 break;
15682
15683 case R_PPC64_GOT_TPREL16_DS:
15684 case R_PPC64_GOT_TPREL16_LO_DS:
15685 if ((tls_mask & TLS_TLS) != 0
15686 && (tls_mask & TLS_TPREL) == 0
15687 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15688 {
15689 toctprel:
15690 insn = bfd_get_32 (input_bfd,
15691 contents + rel->r_offset - d_offset);
15692 insn &= 31 << 21;
15693 insn |= 0x3c0d0000; /* addis 0,13,0 */
15694 bfd_put_32 (input_bfd, insn,
15695 contents + rel->r_offset - d_offset);
15696 r_type = R_PPC64_TPREL16_HA;
15697 if (toc_symndx != 0)
15698 {
15699 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15700 rel->r_addend = toc_addend;
15701 /* We changed the symbol. Start over in order to
15702 get h, sym, sec etc. right. */
15703 goto again;
15704 }
15705 else
15706 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15707 }
15708 break;
15709
15710 case R_PPC64_GOT_TPREL_PCREL34:
15711 if ((tls_mask & TLS_TLS) != 0
15712 && (tls_mask & TLS_TPREL) == 0
15713 && offset_in_range (input_section, rel->r_offset, 8))
15714 {
15715 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15716 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15717 pinsn <<= 32;
15718 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15719 pinsn += ((2ULL << 56) + (-1ULL << 52)
15720 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15721 bfd_put_32 (input_bfd, pinsn >> 32,
15722 contents + rel->r_offset);
15723 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15724 contents + rel->r_offset + 4);
15725 r_type = R_PPC64_TPREL34;
15726 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15727 }
15728 break;
15729
15730 case R_PPC64_TLS:
15731 if ((tls_mask & TLS_TLS) != 0
15732 && (tls_mask & TLS_TPREL) == 0
15733 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15734 {
15735 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15736 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15737 if (insn == 0)
15738 break;
15739 if ((rel->r_offset & 3) == 0)
15740 {
15741 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15742 /* Was PPC64_TLS which sits on insn boundary, now
15743 PPC64_TPREL16_LO which is at low-order half-word. */
15744 rel->r_offset += d_offset;
15745 r_type = R_PPC64_TPREL16_LO;
15746 if (toc_symndx != 0)
15747 {
15748 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15749 rel->r_addend = toc_addend;
15750 /* We changed the symbol. Start over in order to
15751 get h, sym, sec etc. right. */
15752 goto again;
15753 }
15754 else
15755 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15756 }
15757 else if ((rel->r_offset & 3) == 1)
15758 {
15759 /* For pcrel IE to LE we already have the full
15760 offset and thus don't need an addi here. A nop
15761 or mr will do. */
15762 if ((insn & (0x3fu << 26)) == 14 << 26)
15763 {
15764 /* Extract regs from addi rt,ra,si. */
15765 unsigned int rt = (insn >> 21) & 0x1f;
15766 unsigned int ra = (insn >> 16) & 0x1f;
15767 if (rt == ra)
15768 insn = NOP;
15769 else
15770 {
15771 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15772 insn = (rt << 16) | (ra << 21) | (ra << 11);
15773 insn |= (31u << 26) | (444u << 1);
15774 }
15775 }
15776 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15777 }
15778 }
15779 break;
15780
15781 case R_PPC64_GOT_TLSGD16_HI:
15782 case R_PPC64_GOT_TLSGD16_HA:
15783 tls_gd = TLS_GDIE;
15784 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15785 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15786 goto tls_gdld_hi;
15787 break;
15788
15789 case R_PPC64_GOT_TLSLD16_HI:
15790 case R_PPC64_GOT_TLSLD16_HA:
15791 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15792 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15793 {
15794 tls_gdld_hi:
15795 if ((tls_mask & tls_gd) != 0)
15796 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15797 + R_PPC64_GOT_TPREL16_DS);
15798 else
15799 {
15800 rel->r_offset -= d_offset;
15801 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15802 r_type = R_PPC64_NONE;
15803 }
15804 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15805 }
15806 break;
15807
15808 case R_PPC64_GOT_TLSGD16:
15809 case R_PPC64_GOT_TLSGD16_LO:
15810 tls_gd = TLS_GDIE;
15811 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15812 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15813 goto tls_ldgd_opt;
15814 break;
15815
15816 case R_PPC64_GOT_TLSLD16:
15817 case R_PPC64_GOT_TLSLD16_LO:
15818 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15819 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15820 {
15821 unsigned int insn1, insn2;
15822
15823 tls_ldgd_opt:
15824 offset = (bfd_vma) -1;
15825 /* If not using the newer R_PPC64_TLSGD/LD to mark
15826 __tls_get_addr calls, we must trust that the call
15827 stays with its arg setup insns, ie. that the next
15828 reloc is the __tls_get_addr call associated with
15829 the current reloc. Edit both insns. */
15830 if (input_section->nomark_tls_get_addr
15831 && rel + 1 < relend
15832 && branch_reloc_hash_match (input_bfd, rel + 1,
15833 htab->tls_get_addr_fd,
15834 htab->tga_desc_fd,
15835 htab->tls_get_addr,
15836 htab->tga_desc))
15837 offset = rel[1].r_offset;
15838 /* We read the low GOT_TLS (or TOC16) insn because we
15839 need to keep the destination reg. It may be
15840 something other than the usual r3, and moved to r3
15841 before the call by intervening code. */
15842 insn1 = bfd_get_32 (input_bfd,
15843 contents + rel->r_offset - d_offset);
15844 if ((tls_mask & tls_gd) != 0)
15845 {
15846 /* IE */
15847 insn1 &= (0x1f << 21) | (0x1f << 16);
15848 insn1 |= 58u << 26; /* ld */
15849 insn2 = 0x7c636a14; /* add 3,3,13 */
15850 if (offset != (bfd_vma) -1)
15851 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15852 if (r_type == R_PPC64_TOC16
15853 || r_type == R_PPC64_TOC16_LO)
15854 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15855 else
15856 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15857 + R_PPC64_GOT_TPREL16_DS);
15858 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15859 }
15860 else
15861 {
15862 /* LE */
15863 insn1 &= 0x1f << 21;
15864 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15865 insn2 = 0x38630000; /* addi 3,3,0 */
15866 if (tls_gd == 0)
15867 {
15868 /* Was an LD reloc. */
15869 r_symndx = STN_UNDEF;
15870 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15871 }
15872 else if (toc_symndx != 0)
15873 {
15874 r_symndx = toc_symndx;
15875 rel->r_addend = toc_addend;
15876 }
15877 r_type = R_PPC64_TPREL16_HA;
15878 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15879 if (offset != (bfd_vma) -1)
15880 {
15881 rel[1].r_info = ELF64_R_INFO (r_symndx,
15882 R_PPC64_TPREL16_LO);
15883 rel[1].r_offset = offset + d_offset;
15884 rel[1].r_addend = rel->r_addend;
15885 }
15886 }
15887 bfd_put_32 (input_bfd, insn1,
15888 contents + rel->r_offset - d_offset);
15889 if (offset != (bfd_vma) -1
15890 && offset_in_range (input_section, offset, 4))
15891 {
15892 bfd_put_32 (input_bfd, insn2, contents + offset);
15893 if (offset_in_range (input_section, offset + 4, 4))
15894 {
15895 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15896 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15897 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15898 }
15899 }
15900 if ((tls_mask & tls_gd) == 0
15901 && (tls_gd == 0 || toc_symndx != 0))
15902 {
15903 /* We changed the symbol. Start over in order
15904 to get h, sym, sec etc. right. */
15905 goto again;
15906 }
15907 }
15908 break;
15909
15910 case R_PPC64_GOT_TLSGD_PCREL34:
15911 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15912 && offset_in_range (input_section, rel->r_offset, 8))
15913 {
15914 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15915 pinsn <<= 32;
15916 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15917 if ((tls_mask & TLS_GDIE) != 0)
15918 {
15919 /* IE, pla -> pld */
15920 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
15921 r_type = R_PPC64_GOT_TPREL_PCREL34;
15922 }
15923 else
15924 {
15925 /* LE, pla pcrel -> paddi r13 */
15926 pinsn += (-1ULL << 52) + (13ULL << 16);
15927 r_type = R_PPC64_TPREL34;
15928 }
15929 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15930 bfd_put_32 (input_bfd, pinsn >> 32,
15931 contents + rel->r_offset);
15932 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15933 contents + rel->r_offset + 4);
15934 }
15935 break;
15936
15937 case R_PPC64_GOT_TLSLD_PCREL34:
15938 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15939 && offset_in_range (input_section, rel->r_offset, 8))
15940 {
15941 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15942 pinsn <<= 32;
15943 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15944 pinsn += (-1ULL << 52) + (13ULL << 16);
15945 bfd_put_32 (input_bfd, pinsn >> 32,
15946 contents + rel->r_offset);
15947 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15948 contents + rel->r_offset + 4);
15949 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15950 r_symndx = STN_UNDEF;
15951 r_type = R_PPC64_TPREL34;
15952 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15953 goto again;
15954 }
15955 break;
15956
15957 case R_PPC64_TLSGD:
15958 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15959 && rel + 1 < relend
15960 && offset_in_range (input_section, rel->r_offset,
15961 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15962 ? 8 : 4))
15963 {
15964 unsigned int insn2;
15965 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15966
15967 offset = rel->r_offset;
15968 if (is_plt_seq_reloc (r_type1))
15969 {
15970 bfd_put_32 (output_bfd, NOP, contents + offset);
15971 if (r_type1 == R_PPC64_PLT_PCREL34
15972 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15973 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15974 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15975 break;
15976 }
15977
15978 if (r_type1 == R_PPC64_PLTCALL)
15979 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15980
15981 if ((tls_mask & TLS_GDIE) != 0)
15982 {
15983 /* IE */
15984 r_type = R_PPC64_NONE;
15985 insn2 = 0x7c636a14; /* add 3,3,13 */
15986 }
15987 else
15988 {
15989 /* LE */
15990 if (toc_symndx != 0)
15991 {
15992 r_symndx = toc_symndx;
15993 rel->r_addend = toc_addend;
15994 }
15995 if (r_type1 == R_PPC64_REL24_NOTOC
15996 || r_type1 == R_PPC64_REL24_P9NOTOC
15997 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15998 {
15999 r_type = R_PPC64_NONE;
16000 insn2 = NOP;
16001 }
16002 else
16003 {
16004 rel->r_offset = offset + d_offset;
16005 r_type = R_PPC64_TPREL16_LO;
16006 insn2 = 0x38630000; /* addi 3,3,0 */
16007 }
16008 }
16009 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16010 /* Zap the reloc on the _tls_get_addr call too. */
16011 BFD_ASSERT (offset == rel[1].r_offset);
16012 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16013 bfd_put_32 (input_bfd, insn2, contents + offset);
16014 if ((tls_mask & TLS_GDIE) == 0
16015 && toc_symndx != 0
16016 && r_type != R_PPC64_NONE)
16017 goto again;
16018 }
16019 break;
16020
16021 case R_PPC64_TLSLD:
16022 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16023 && rel + 1 < relend
16024 && offset_in_range (input_section, rel->r_offset,
16025 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16026 ? 8 : 4))
16027 {
16028 unsigned int insn2;
16029 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16030
16031 offset = rel->r_offset;
16032 if (is_plt_seq_reloc (r_type1))
16033 {
16034 bfd_put_32 (output_bfd, NOP, contents + offset);
16035 if (r_type1 == R_PPC64_PLT_PCREL34
16036 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16037 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16038 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16039 break;
16040 }
16041
16042 if (r_type1 == R_PPC64_PLTCALL)
16043 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16044
16045 if (r_type1 == R_PPC64_REL24_NOTOC
16046 || r_type1 == R_PPC64_REL24_P9NOTOC
16047 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16048 {
16049 r_type = R_PPC64_NONE;
16050 insn2 = NOP;
16051 }
16052 else
16053 {
16054 rel->r_offset = offset + d_offset;
16055 r_symndx = STN_UNDEF;
16056 r_type = R_PPC64_TPREL16_LO;
16057 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16058 insn2 = 0x38630000; /* addi 3,3,0 */
16059 }
16060 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16061 /* Zap the reloc on the _tls_get_addr call too. */
16062 BFD_ASSERT (offset == rel[1].r_offset);
16063 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16064 bfd_put_32 (input_bfd, insn2, contents + offset);
16065 if (r_type != R_PPC64_NONE)
16066 goto again;
16067 }
16068 break;
16069
16070 case R_PPC64_DTPMOD64:
16071 if (rel + 1 < relend
16072 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16073 && rel[1].r_offset == rel->r_offset + 8)
16074 {
16075 if ((tls_mask & TLS_GD) == 0
16076 && offset_in_range (input_section, rel->r_offset, 8))
16077 {
16078 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16079 if ((tls_mask & TLS_GDIE) != 0)
16080 r_type = R_PPC64_TPREL64;
16081 else
16082 {
16083 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16084 r_type = R_PPC64_NONE;
16085 }
16086 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16087 }
16088 }
16089 else
16090 {
16091 if ((tls_mask & TLS_LD) == 0
16092 && offset_in_range (input_section, rel->r_offset, 8))
16093 {
16094 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16095 r_type = R_PPC64_NONE;
16096 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16097 }
16098 }
16099 break;
16100
16101 case R_PPC64_TPREL64:
16102 if ((tls_mask & TLS_TPREL) == 0)
16103 {
16104 r_type = R_PPC64_NONE;
16105 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16106 }
16107 break;
16108
16109 case R_PPC64_ENTRY:
16110 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16111 if (!bfd_link_pic (info)
16112 && !info->traditional_format
16113 && relocation + 0x80008000 <= 0xffffffff
16114 && offset_in_range (input_section, rel->r_offset, 8))
16115 {
16116 unsigned int insn1, insn2;
16117
16118 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16119 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16120 if ((insn1 & ~0xfffc) == LD_R2_0R12
16121 && insn2 == ADD_R2_R2_R12)
16122 {
16123 bfd_put_32 (input_bfd,
16124 LIS_R2 + PPC_HA (relocation),
16125 contents + rel->r_offset);
16126 bfd_put_32 (input_bfd,
16127 ADDI_R2_R2 + PPC_LO (relocation),
16128 contents + rel->r_offset + 4);
16129 }
16130 }
16131 else
16132 {
16133 relocation -= (rel->r_offset
16134 + input_section->output_offset
16135 + input_section->output_section->vma);
16136 if (relocation + 0x80008000 <= 0xffffffff
16137 && offset_in_range (input_section, rel->r_offset, 8))
16138 {
16139 unsigned int insn1, insn2;
16140
16141 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16142 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16143 if ((insn1 & ~0xfffc) == LD_R2_0R12
16144 && insn2 == ADD_R2_R2_R12)
16145 {
16146 bfd_put_32 (input_bfd,
16147 ADDIS_R2_R12 + PPC_HA (relocation),
16148 contents + rel->r_offset);
16149 bfd_put_32 (input_bfd,
16150 ADDI_R2_R2 + PPC_LO (relocation),
16151 contents + rel->r_offset + 4);
16152 }
16153 }
16154 }
16155 break;
16156
16157 case R_PPC64_REL16_HA:
16158 /* If we are generating a non-PIC executable, edit
16159 . 0: addis 2,12,.TOC.-0b@ha
16160 . addi 2,2,.TOC.-0b@l
16161 used by ELFv2 global entry points to set up r2, to
16162 . lis 2,.TOC.@ha
16163 . addi 2,2,.TOC.@l
16164 if .TOC. is in range. */
16165 if (!bfd_link_pic (info)
16166 && !info->traditional_format
16167 && !htab->opd_abi
16168 && rel->r_addend == d_offset
16169 && h != NULL && &h->elf == htab->elf.hgot
16170 && rel + 1 < relend
16171 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16172 && rel[1].r_offset == rel->r_offset + 4
16173 && rel[1].r_addend == rel->r_addend + 4
16174 && relocation + 0x80008000 <= 0xffffffff
16175 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16176 {
16177 unsigned int insn1, insn2;
16178 offset = rel->r_offset - d_offset;
16179 insn1 = bfd_get_32 (input_bfd, contents + offset);
16180 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16181 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16182 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16183 {
16184 r_type = R_PPC64_ADDR16_HA;
16185 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16186 rel->r_addend -= d_offset;
16187 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16188 rel[1].r_addend -= d_offset + 4;
16189 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16190 }
16191 }
16192 break;
16193 }
16194
16195 /* Handle other relocations that tweak non-addend part of insn. */
16196 insn = 0;
16197 max_br_offset = 1 << 25;
16198 addend = rel->r_addend;
16199 reloc_dest = DEST_NORMAL;
16200 switch (r_type)
16201 {
16202 default:
16203 break;
16204
16205 case R_PPC64_TOCSAVE:
16206 if (relocation + addend == (rel->r_offset
16207 + input_section->output_offset
16208 + input_section->output_section->vma)
16209 && tocsave_find (htab, NO_INSERT,
16210 &local_syms, rel, input_bfd)
16211 && offset_in_range (input_section, rel->r_offset, 4))
16212 {
16213 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16214 if (insn == NOP
16215 || insn == CROR_151515 || insn == CROR_313131)
16216 bfd_put_32 (input_bfd,
16217 STD_R2_0R1 + STK_TOC (htab),
16218 contents + rel->r_offset);
16219 }
16220 break;
16221
16222 /* Branch taken prediction relocations. */
16223 case R_PPC64_ADDR14_BRTAKEN:
16224 case R_PPC64_REL14_BRTAKEN:
16225 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16226 /* Fall through. */
16227
16228 /* Branch not taken prediction relocations. */
16229 case R_PPC64_ADDR14_BRNTAKEN:
16230 case R_PPC64_REL14_BRNTAKEN:
16231 if (!offset_in_range (input_section, rel->r_offset, 4))
16232 break;
16233 insn |= bfd_get_32 (input_bfd,
16234 contents + rel->r_offset) & ~(0x01 << 21);
16235 /* Fall through. */
16236
16237 case R_PPC64_REL14:
16238 max_br_offset = 1 << 15;
16239 /* Fall through. */
16240
16241 case R_PPC64_REL24:
16242 case R_PPC64_REL24_NOTOC:
16243 case R_PPC64_REL24_P9NOTOC:
16244 case R_PPC64_PLTCALL:
16245 case R_PPC64_PLTCALL_NOTOC:
16246 /* Calls to functions with a different TOC, such as calls to
16247 shared objects, need to alter the TOC pointer. This is
16248 done using a linkage stub. A REL24 branching to these
16249 linkage stubs needs to be followed by a nop, as the nop
16250 will be replaced with an instruction to restore the TOC
16251 base pointer. */
16252 fdh = h;
16253 if (h != NULL
16254 && h->oh != NULL
16255 && h->oh->is_func_descriptor)
16256 fdh = ppc_follow_link (h->oh);
16257 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16258 htab);
16259 if ((r_type == R_PPC64_PLTCALL
16260 || r_type == R_PPC64_PLTCALL_NOTOC)
16261 && stub_entry != NULL
16262 && stub_entry->type.main == ppc_stub_plt_call)
16263 stub_entry = NULL;
16264
16265 if (stub_entry != NULL
16266 && (stub_entry->type.main == ppc_stub_plt_call
16267 || stub_entry->type.r2save))
16268 {
16269 bool can_plt_call = false;
16270
16271 if (r_type == R_PPC64_REL24_NOTOC
16272 || r_type == R_PPC64_REL24_P9NOTOC)
16273 {
16274 /* NOTOC calls don't need to restore r2. */
16275 can_plt_call = true;
16276 }
16277 else if (stub_entry->type.main == ppc_stub_plt_call
16278 && !htab->opd_abi
16279 && htab->params->plt_localentry0 != 0
16280 && h != NULL
16281 && is_elfv2_localentry0 (&h->elf))
16282 {
16283 /* The function doesn't use or change r2. */
16284 can_plt_call = true;
16285 }
16286
16287 /* All of these stubs may modify r2, so there must be a
16288 branch and link followed by a nop. The nop is
16289 replaced by an insn to restore r2. */
16290 else if (offset_in_range (input_section, rel->r_offset, 8))
16291 {
16292 unsigned long br;
16293
16294 br = bfd_get_32 (input_bfd,
16295 contents + rel->r_offset);
16296 if ((br & 1) != 0)
16297 {
16298 unsigned long nop;
16299
16300 nop = bfd_get_32 (input_bfd,
16301 contents + rel->r_offset + 4);
16302 if (nop == LD_R2_0R1 + STK_TOC (htab))
16303 can_plt_call = true;
16304 else if (nop == NOP
16305 || nop == CROR_151515
16306 || nop == CROR_313131)
16307 {
16308 if (h != NULL
16309 && is_tls_get_addr (&h->elf, htab)
16310 && htab->params->tls_get_addr_opt)
16311 {
16312 /* Special stub used, leave nop alone. */
16313 }
16314 else
16315 bfd_put_32 (input_bfd,
16316 LD_R2_0R1 + STK_TOC (htab),
16317 contents + rel->r_offset + 4);
16318 can_plt_call = true;
16319 }
16320 }
16321 }
16322
16323 if (!can_plt_call && h != NULL)
16324 {
16325 const char *name = h->elf.root.root.string;
16326
16327 if (*name == '.')
16328 ++name;
16329
16330 if (startswith (name, "__libc_start_main")
16331 && (name[17] == 0 || name[17] == '@'))
16332 {
16333 /* Allow crt1 branch to go via a toc adjusting
16334 stub. Other calls that never return could do
16335 the same, if we could detect such. */
16336 can_plt_call = true;
16337 }
16338 }
16339
16340 if (!can_plt_call)
16341 {
16342 /* g++ as of 20130507 emits self-calls without a
16343 following nop. This is arguably wrong since we
16344 have conflicting information. On the one hand a
16345 global symbol and on the other a local call
16346 sequence, but don't error for this special case.
16347 It isn't possible to cheaply verify we have
16348 exactly such a call. Allow all calls to the same
16349 section. */
16350 asection *code_sec = sec;
16351
16352 if (get_opd_info (sec) != NULL)
16353 {
16354 bfd_vma off = (relocation + addend
16355 - sec->output_section->vma
16356 - sec->output_offset);
16357
16358 opd_entry_value (sec, off, &code_sec, NULL, false);
16359 }
16360 if (code_sec == input_section)
16361 can_plt_call = true;
16362 }
16363
16364 if (!can_plt_call)
16365 {
16366 if (stub_entry->type.main == ppc_stub_plt_call)
16367 info->callbacks->einfo
16368 /* xgettext:c-format */
16369 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16370 "(plt call stub)\n"),
16371 input_bfd, input_section, rel->r_offset, sym_name);
16372 else
16373 info->callbacks->einfo
16374 /* xgettext:c-format */
16375 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16376 "(toc save/adjust stub)\n"),
16377 input_bfd, input_section, rel->r_offset, sym_name);
16378
16379 bfd_set_error (bfd_error_bad_value);
16380 ret = false;
16381 }
16382
16383 if (can_plt_call
16384 && stub_entry->type.main == ppc_stub_plt_call)
16385 unresolved_reloc = false;
16386 }
16387
16388 if ((stub_entry == NULL
16389 || stub_entry->type.main == ppc_stub_long_branch
16390 || stub_entry->type.main == ppc_stub_plt_branch)
16391 && get_opd_info (sec) != NULL)
16392 {
16393 /* The branch destination is the value of the opd entry. */
16394 bfd_vma off = (relocation + addend
16395 - sec->output_section->vma
16396 - sec->output_offset);
16397 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16398 if (dest != (bfd_vma) -1)
16399 {
16400 relocation = dest;
16401 addend = 0;
16402 reloc_dest = DEST_OPD;
16403 }
16404 }
16405
16406 /* If the branch is out of reach we ought to have a long
16407 branch stub. */
16408 from = (rel->r_offset
16409 + input_section->output_offset
16410 + input_section->output_section->vma);
16411
16412 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16413 ? fdh->elf.other
16414 : sym->st_other);
16415
16416 if (stub_entry != NULL
16417 && (stub_entry->type.main == ppc_stub_long_branch
16418 || stub_entry->type.main == ppc_stub_plt_branch))
16419 {
16420 if (stub_entry->type.sub == ppc_stub_toc
16421 && !stub_entry->type.r2save
16422 && (r_type == R_PPC64_ADDR14_BRTAKEN
16423 || r_type == R_PPC64_ADDR14_BRNTAKEN
16424 || (relocation + addend - from + max_br_offset
16425 < 2 * max_br_offset)))
16426 /* Don't use the stub if this branch is in range. */
16427 stub_entry = NULL;
16428
16429 if (stub_entry != NULL
16430 && stub_entry->type.sub >= ppc_stub_notoc
16431 && ((r_type != R_PPC64_REL24_NOTOC
16432 && r_type != R_PPC64_REL24_P9NOTOC)
16433 || ((fdh ? fdh->elf.other : sym->st_other)
16434 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16435 && (relocation + addend - from + max_br_offset
16436 < 2 * max_br_offset))
16437 stub_entry = NULL;
16438
16439 if (stub_entry != NULL
16440 && stub_entry->type.r2save
16441 && (r_type == R_PPC64_REL24_NOTOC
16442 || r_type == R_PPC64_REL24_P9NOTOC)
16443 && (relocation + addend - from + max_br_offset
16444 < 2 * max_br_offset))
16445 stub_entry = NULL;
16446 }
16447
16448 if (stub_entry != NULL)
16449 {
16450 /* Munge up the value and addend so that we call the stub
16451 rather than the procedure directly. */
16452 asection *stub_sec = stub_entry->group->stub_sec;
16453
16454 if (stub_entry->type.main == ppc_stub_save_res)
16455 relocation += (stub_sec->output_offset
16456 + stub_sec->output_section->vma
16457 + stub_sec->size - htab->sfpr->size
16458 - htab->sfpr->output_offset
16459 - htab->sfpr->output_section->vma);
16460 else
16461 relocation = (stub_entry->stub_offset
16462 + stub_sec->output_offset
16463 + stub_sec->output_section->vma);
16464 addend = 0;
16465 reloc_dest = DEST_STUB;
16466
16467 if (((stub_entry->type.r2save
16468 && (r_type == R_PPC64_REL24_NOTOC
16469 || r_type == R_PPC64_REL24_P9NOTOC))
16470 || ((stub_entry->type.main == ppc_stub_plt_call
16471 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16472 && rel + 1 < relend
16473 && rel[1].r_offset == rel->r_offset + 4
16474 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16475 && !(stub_entry->type.main == ppc_stub_plt_call
16476 && htab->params->tls_get_addr_opt
16477 && h != NULL
16478 && is_tls_get_addr (&h->elf, htab)))
16479 {
16480 /* Skip over the r2 store at the start of the stub. */
16481 relocation += 4;
16482 }
16483
16484 if ((r_type == R_PPC64_REL24_NOTOC
16485 || r_type == R_PPC64_REL24_P9NOTOC)
16486 && stub_entry->type.main == ppc_stub_plt_call
16487 && stub_entry->type.sub >= ppc_stub_notoc)
16488 htab->notoc_plt = 1;
16489 }
16490
16491 if (insn != 0)
16492 {
16493 if (is_isa_v2)
16494 {
16495 /* Set 'a' bit. This is 0b00010 in BO field for branch
16496 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16497 for branch on CTR insns (BO == 1a00t or 1a01t). */
16498 if ((insn & (0x14 << 21)) == (0x04 << 21))
16499 insn |= 0x02 << 21;
16500 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16501 insn |= 0x08 << 21;
16502 else
16503 break;
16504 }
16505 else
16506 {
16507 /* Invert 'y' bit if not the default. */
16508 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16509 insn ^= 0x01 << 21;
16510 }
16511
16512 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16513 }
16514
16515 /* NOP out calls to undefined weak functions.
16516 We can thus call a weak function without first
16517 checking whether the function is defined. */
16518 else if (h != NULL
16519 && h->elf.root.type == bfd_link_hash_undefweak
16520 && h->elf.dynindx == -1
16521 && (r_type == R_PPC64_REL24
16522 || r_type == R_PPC64_REL24_NOTOC
16523 || r_type == R_PPC64_REL24_P9NOTOC)
16524 && relocation == 0
16525 && addend == 0
16526 && offset_in_range (input_section, rel->r_offset, 4))
16527 {
16528 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16529 goto copy_reloc;
16530 }
16531 break;
16532
16533 case R_PPC64_GOT16_DS:
16534 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16535 || !htab->do_toc_opt)
16536 break;
16537 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16538 if (relocation + addend - from + 0x8000 < 0x10000
16539 && sec != NULL
16540 && sec->output_section != NULL
16541 && !discarded_section (sec)
16542 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16543 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16544 {
16545 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16546 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16547 {
16548 insn += (14u << 26) - (58u << 26);
16549 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16550 r_type = R_PPC64_TOC16;
16551 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16552 }
16553 }
16554 break;
16555
16556 case R_PPC64_GOT16_LO_DS:
16557 case R_PPC64_GOT16_HA:
16558 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16559 || !htab->do_toc_opt)
16560 break;
16561 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16562 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16563 && sec != NULL
16564 && sec->output_section != NULL
16565 && !discarded_section (sec)
16566 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16567 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16568 {
16569 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16570 if (r_type == R_PPC64_GOT16_LO_DS
16571 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16572 {
16573 insn += (14u << 26) - (58u << 26);
16574 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16575 r_type = R_PPC64_TOC16_LO;
16576 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16577 }
16578 else if (r_type == R_PPC64_GOT16_HA
16579 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16580 {
16581 r_type = R_PPC64_TOC16_HA;
16582 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16583 }
16584 }
16585 break;
16586
16587 case R_PPC64_GOT_PCREL34:
16588 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16589 || !htab->do_toc_opt)
16590 break;
16591 from = (rel->r_offset
16592 + input_section->output_section->vma
16593 + input_section->output_offset);
16594 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16595 && sec != NULL
16596 && sec->output_section != NULL
16597 && !discarded_section (sec)
16598 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16599 && offset_in_range (input_section, rel->r_offset, 8)))
16600 break;
16601
16602 offset = rel->r_offset;
16603 pinsn = bfd_get_32 (input_bfd, contents + offset);
16604 pinsn <<= 32;
16605 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16606 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16607 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16608 break;
16609
16610 /* Replace with paddi. */
16611 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16612 r_type = R_PPC64_PCREL34;
16613 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16614 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16615 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16616 /* Fall through. */
16617
16618 case R_PPC64_PCREL34:
16619 if (!htab->params->no_pcrel_opt
16620 && rel + 1 < relend
16621 && rel[1].r_offset == rel->r_offset
16622 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16623 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16624 && offset_in_range (input_section, rel->r_offset, 8))
16625 {
16626 offset = rel->r_offset;
16627 pinsn = bfd_get_32 (input_bfd, contents + offset);
16628 pinsn <<= 32;
16629 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16630 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16631 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16632 | (14ULL << 26) /* paddi */))
16633 {
16634 bfd_vma off2 = rel[1].r_addend;
16635 if (off2 == 0)
16636 /* zero means next insn. */
16637 off2 = 8;
16638 off2 += offset;
16639 if (offset_in_range (input_section, off2, 4))
16640 {
16641 uint64_t pinsn2;
16642 bfd_signed_vma addend_off;
16643 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16644 pinsn2 <<= 32;
16645 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16646 {
16647 if (!offset_in_range (input_section, off2, 8))
16648 break;
16649 pinsn2 |= bfd_get_32 (input_bfd,
16650 contents + off2 + 4);
16651 }
16652 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16653 {
16654 addend += addend_off;
16655 rel->r_addend = addend;
16656 bfd_put_32 (input_bfd, pinsn >> 32,
16657 contents + offset);
16658 bfd_put_32 (input_bfd, pinsn,
16659 contents + offset + 4);
16660 bfd_put_32 (input_bfd, pinsn2 >> 32,
16661 contents + off2);
16662 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16663 bfd_put_32 (input_bfd, pinsn2,
16664 contents + off2 + 4);
16665 }
16666 }
16667 }
16668 }
16669 break;
16670 }
16671
16672 tls_type = 0;
16673 save_unresolved_reloc = unresolved_reloc;
16674 switch (r_type)
16675 {
16676 default:
16677 /* xgettext:c-format */
16678 _bfd_error_handler (_("%pB: %s unsupported"),
16679 input_bfd, ppc64_elf_howto_table[r_type]->name);
16680
16681 bfd_set_error (bfd_error_bad_value);
16682 ret = false;
16683 goto copy_reloc;
16684
16685 case R_PPC64_NONE:
16686 case R_PPC64_TLS:
16687 case R_PPC64_TLSGD:
16688 case R_PPC64_TLSLD:
16689 case R_PPC64_TOCSAVE:
16690 case R_PPC64_GNU_VTINHERIT:
16691 case R_PPC64_GNU_VTENTRY:
16692 case R_PPC64_ENTRY:
16693 case R_PPC64_PCREL_OPT:
16694 goto copy_reloc;
16695
16696 /* GOT16 relocations. Like an ADDR16 using the symbol's
16697 address in the GOT as relocation value instead of the
16698 symbol's value itself. Also, create a GOT entry for the
16699 symbol and put the symbol value there. */
16700 case R_PPC64_GOT_TLSGD16:
16701 case R_PPC64_GOT_TLSGD16_LO:
16702 case R_PPC64_GOT_TLSGD16_HI:
16703 case R_PPC64_GOT_TLSGD16_HA:
16704 case R_PPC64_GOT_TLSGD_PCREL34:
16705 tls_type = TLS_TLS | TLS_GD;
16706 goto dogot;
16707
16708 case R_PPC64_GOT_TLSLD16:
16709 case R_PPC64_GOT_TLSLD16_LO:
16710 case R_PPC64_GOT_TLSLD16_HI:
16711 case R_PPC64_GOT_TLSLD16_HA:
16712 case R_PPC64_GOT_TLSLD_PCREL34:
16713 tls_type = TLS_TLS | TLS_LD;
16714 goto dogot;
16715
16716 case R_PPC64_GOT_TPREL16_DS:
16717 case R_PPC64_GOT_TPREL16_LO_DS:
16718 case R_PPC64_GOT_TPREL16_HI:
16719 case R_PPC64_GOT_TPREL16_HA:
16720 case R_PPC64_GOT_TPREL_PCREL34:
16721 tls_type = TLS_TLS | TLS_TPREL;
16722 goto dogot;
16723
16724 case R_PPC64_GOT_DTPREL16_DS:
16725 case R_PPC64_GOT_DTPREL16_LO_DS:
16726 case R_PPC64_GOT_DTPREL16_HI:
16727 case R_PPC64_GOT_DTPREL16_HA:
16728 case R_PPC64_GOT_DTPREL_PCREL34:
16729 tls_type = TLS_TLS | TLS_DTPREL;
16730 goto dogot;
16731
16732 case R_PPC64_GOT16:
16733 case R_PPC64_GOT16_LO:
16734 case R_PPC64_GOT16_HI:
16735 case R_PPC64_GOT16_HA:
16736 case R_PPC64_GOT16_DS:
16737 case R_PPC64_GOT16_LO_DS:
16738 case R_PPC64_GOT_PCREL34:
16739 dogot:
16740 {
16741 /* Relocation is to the entry for this symbol in the global
16742 offset table. */
16743 asection *got;
16744 bfd_vma *offp;
16745 bfd_vma off;
16746 unsigned long indx = 0;
16747 struct got_entry *ent;
16748
16749 if (tls_type == (TLS_TLS | TLS_LD)
16750 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16751 ent = ppc64_tlsld_got (input_bfd);
16752 else
16753 {
16754 if (h != NULL)
16755 {
16756 if (!htab->elf.dynamic_sections_created
16757 || h->elf.dynindx == -1
16758 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16759 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16760 /* This is actually a static link, or it is a
16761 -Bsymbolic link and the symbol is defined
16762 locally, or the symbol was forced to be local
16763 because of a version file. */
16764 ;
16765 else
16766 {
16767 indx = h->elf.dynindx;
16768 unresolved_reloc = false;
16769 }
16770 ent = h->elf.got.glist;
16771 }
16772 else
16773 {
16774 if (local_got_ents == NULL)
16775 abort ();
16776 ent = local_got_ents[r_symndx];
16777 }
16778
16779 for (; ent != NULL; ent = ent->next)
16780 if (ent->addend == orig_rel.r_addend
16781 && ent->owner == input_bfd
16782 && ent->tls_type == tls_type)
16783 break;
16784 }
16785
16786 if (ent == NULL)
16787 abort ();
16788 if (ent->is_indirect)
16789 ent = ent->got.ent;
16790 offp = &ent->got.offset;
16791 got = ppc64_elf_tdata (ent->owner)->got;
16792 if (got == NULL)
16793 abort ();
16794
16795 /* The offset must always be a multiple of 8. We use the
16796 least significant bit to record whether we have already
16797 processed this entry. */
16798 off = *offp;
16799 if ((off & 1) != 0)
16800 off &= ~1;
16801 else
16802 {
16803 /* Generate relocs for the dynamic linker, except in
16804 the case of TLSLD where we'll use one entry per
16805 module. */
16806 asection *relgot;
16807 bool ifunc;
16808
16809 *offp = off | 1;
16810 relgot = NULL;
16811 ifunc = (h != NULL
16812 ? h->elf.type == STT_GNU_IFUNC
16813 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16814 if (ifunc)
16815 {
16816 relgot = htab->elf.irelplt;
16817 if (indx == 0 || is_static_defined (&h->elf))
16818 htab->elf.ifunc_resolvers = true;
16819 }
16820 else if (indx != 0
16821 || (bfd_link_pic (info)
16822 && (h == NULL
16823 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16824 && !(tls_type != 0
16825 && bfd_link_executable (info)
16826 && (h == NULL
16827 || SYMBOL_REFERENCES_LOCAL (info,
16828 &h->elf)))))
16829 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16830 if (relgot != NULL)
16831 {
16832 outrel.r_offset = (got->output_section->vma
16833 + got->output_offset
16834 + off);
16835 outrel.r_addend = orig_rel.r_addend;
16836 if (tls_type & (TLS_LD | TLS_GD))
16837 {
16838 outrel.r_addend = 0;
16839 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16840 if (tls_type == (TLS_TLS | TLS_GD))
16841 {
16842 loc = relgot->contents;
16843 loc += (relgot->reloc_count++
16844 * sizeof (Elf64_External_Rela));
16845 bfd_elf64_swap_reloca_out (output_bfd,
16846 &outrel, loc);
16847 outrel.r_offset += 8;
16848 outrel.r_addend = orig_rel.r_addend;
16849 outrel.r_info
16850 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16851 }
16852 }
16853 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16854 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16855 else if (tls_type == (TLS_TLS | TLS_TPREL))
16856 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16857 else if (indx != 0)
16858 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16859 else
16860 {
16861 if (ifunc)
16862 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16863 else
16864 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16865
16866 /* Write the .got section contents for the sake
16867 of prelink. */
16868 loc = got->contents + off;
16869 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16870 loc);
16871 }
16872
16873 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16874 {
16875 outrel.r_addend += relocation;
16876 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16877 {
16878 if (htab->elf.tls_sec == NULL)
16879 outrel.r_addend = 0;
16880 else
16881 outrel.r_addend -= htab->elf.tls_sec->vma;
16882 }
16883 }
16884 if (!(info->enable_dt_relr
16885 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
16886 {
16887 loc = relgot->contents;
16888 loc += (relgot->reloc_count++
16889 * sizeof (Elf64_External_Rela));
16890 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16891 }
16892 }
16893
16894 /* Init the .got section contents here if we're not
16895 emitting a reloc. */
16896 else
16897 {
16898 relocation += orig_rel.r_addend;
16899 if (tls_type != 0)
16900 {
16901 if (htab->elf.tls_sec == NULL)
16902 relocation = 0;
16903 else
16904 {
16905 if (tls_type & TLS_LD)
16906 relocation = 0;
16907 else
16908 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
16909 if (tls_type & TLS_TPREL)
16910 relocation += DTP_OFFSET - TP_OFFSET;
16911 }
16912
16913 if (tls_type & (TLS_GD | TLS_LD))
16914 {
16915 bfd_put_64 (output_bfd, relocation,
16916 got->contents + off + 8);
16917 relocation = 1;
16918 }
16919 }
16920 bfd_put_64 (output_bfd, relocation,
16921 got->contents + off);
16922 }
16923 }
16924
16925 if (off >= (bfd_vma) -2)
16926 abort ();
16927
16928 relocation = got->output_section->vma + got->output_offset + off;
16929 addend = 0;
16930 if (!(r_type == R_PPC64_GOT_PCREL34
16931 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16932 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16933 || r_type == R_PPC64_GOT_TPREL_PCREL34
16934 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
16935 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
16936 }
16937 break;
16938
16939 case R_PPC64_PLT16_HA:
16940 case R_PPC64_PLT16_HI:
16941 case R_PPC64_PLT16_LO:
16942 case R_PPC64_PLT16_LO_DS:
16943 case R_PPC64_PLT_PCREL34:
16944 case R_PPC64_PLT_PCREL34_NOTOC:
16945 case R_PPC64_PLT32:
16946 case R_PPC64_PLT64:
16947 case R_PPC64_PLTSEQ:
16948 case R_PPC64_PLTSEQ_NOTOC:
16949 case R_PPC64_PLTCALL:
16950 case R_PPC64_PLTCALL_NOTOC:
16951 /* Relocation is to the entry for this symbol in the
16952 procedure linkage table. */
16953 unresolved_reloc = true;
16954 {
16955 struct plt_entry **plt_list = NULL;
16956 if (h != NULL)
16957 plt_list = &h->elf.plt.plist;
16958 else if (local_got_ents != NULL)
16959 {
16960 struct plt_entry **local_plt = (struct plt_entry **)
16961 (local_got_ents + symtab_hdr->sh_info);
16962 plt_list = local_plt + r_symndx;
16963 }
16964 if (plt_list)
16965 {
16966 struct plt_entry *ent;
16967
16968 for (ent = *plt_list; ent != NULL; ent = ent->next)
16969 if (ent->plt.offset != (bfd_vma) -1
16970 && ent->addend == orig_rel.r_addend)
16971 {
16972 asection *plt;
16973 bfd_vma got;
16974
16975 plt = htab->elf.splt;
16976 if (use_local_plt (info, elf_hash_entry (h)))
16977 {
16978 if (h != NULL
16979 ? h->elf.type == STT_GNU_IFUNC
16980 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16981 plt = htab->elf.iplt;
16982 else
16983 plt = htab->pltlocal;
16984 }
16985 relocation = (plt->output_section->vma
16986 + plt->output_offset
16987 + ent->plt.offset);
16988 if (r_type == R_PPC64_PLT16_HA
16989 || r_type == R_PPC64_PLT16_HI
16990 || r_type == R_PPC64_PLT16_LO
16991 || r_type == R_PPC64_PLT16_LO_DS)
16992 {
16993 got = (elf_gp (output_bfd)
16994 + htab->sec_info[input_section->id].toc_off);
16995 relocation -= got;
16996 }
16997 addend = 0;
16998 unresolved_reloc = false;
16999 break;
17000 }
17001 }
17002 }
17003 break;
17004
17005 case R_PPC64_TOC:
17006 /* Relocation value is TOC base. */
17007 relocation = TOCstart;
17008 if (r_symndx == STN_UNDEF)
17009 relocation += htab->sec_info[input_section->id].toc_off;
17010 else if (unresolved_reloc)
17011 ;
17012 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17013 relocation += htab->sec_info[sec->id].toc_off;
17014 else
17015 unresolved_reloc = true;
17016 goto dodyn;
17017
17018 /* TOC16 relocs. We want the offset relative to the TOC base,
17019 which is the address of the start of the TOC plus 0x8000.
17020 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17021 in this order. */
17022 case R_PPC64_TOC16:
17023 case R_PPC64_TOC16_LO:
17024 case R_PPC64_TOC16_HI:
17025 case R_PPC64_TOC16_DS:
17026 case R_PPC64_TOC16_LO_DS:
17027 case R_PPC64_TOC16_HA:
17028 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17029 if (h != NULL)
17030 goto dodyn;
17031 break;
17032
17033 /* Relocate against the beginning of the section. */
17034 case R_PPC64_SECTOFF:
17035 case R_PPC64_SECTOFF_LO:
17036 case R_PPC64_SECTOFF_HI:
17037 case R_PPC64_SECTOFF_DS:
17038 case R_PPC64_SECTOFF_LO_DS:
17039 case R_PPC64_SECTOFF_HA:
17040 if (sec != NULL)
17041 addend -= sec->output_section->vma;
17042 break;
17043
17044 case R_PPC64_REL16:
17045 case R_PPC64_REL16_LO:
17046 case R_PPC64_REL16_HI:
17047 case R_PPC64_REL16_HA:
17048 case R_PPC64_REL16_HIGH:
17049 case R_PPC64_REL16_HIGHA:
17050 case R_PPC64_REL16_HIGHER:
17051 case R_PPC64_REL16_HIGHERA:
17052 case R_PPC64_REL16_HIGHEST:
17053 case R_PPC64_REL16_HIGHESTA:
17054 case R_PPC64_REL16_HIGHER34:
17055 case R_PPC64_REL16_HIGHERA34:
17056 case R_PPC64_REL16_HIGHEST34:
17057 case R_PPC64_REL16_HIGHESTA34:
17058 case R_PPC64_REL16DX_HA:
17059 case R_PPC64_REL14:
17060 case R_PPC64_REL14_BRNTAKEN:
17061 case R_PPC64_REL14_BRTAKEN:
17062 case R_PPC64_REL24:
17063 case R_PPC64_REL24_NOTOC:
17064 case R_PPC64_REL24_P9NOTOC:
17065 case R_PPC64_PCREL34:
17066 case R_PPC64_PCREL28:
17067 break;
17068
17069 case R_PPC64_TPREL16:
17070 case R_PPC64_TPREL16_LO:
17071 case R_PPC64_TPREL16_HI:
17072 case R_PPC64_TPREL16_HA:
17073 case R_PPC64_TPREL16_DS:
17074 case R_PPC64_TPREL16_LO_DS:
17075 case R_PPC64_TPREL16_HIGH:
17076 case R_PPC64_TPREL16_HIGHA:
17077 case R_PPC64_TPREL16_HIGHER:
17078 case R_PPC64_TPREL16_HIGHERA:
17079 case R_PPC64_TPREL16_HIGHEST:
17080 case R_PPC64_TPREL16_HIGHESTA:
17081 if (h != NULL
17082 && h->elf.root.type == bfd_link_hash_undefweak
17083 && h->elf.dynindx == -1
17084 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17085 {
17086 /* Make this relocation against an undefined weak symbol
17087 resolve to zero. This is really just a tweak, since
17088 code using weak externs ought to check that they are
17089 defined before using them. */
17090 bfd_byte *p = contents + rel->r_offset - d_offset;
17091
17092 insn = bfd_get_32 (input_bfd, p);
17093 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17094 if (insn != 0)
17095 bfd_put_32 (input_bfd, insn, p);
17096 break;
17097 }
17098 /* Fall through. */
17099
17100 case R_PPC64_TPREL34:
17101 if (htab->elf.tls_sec != NULL)
17102 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17103 /* The TPREL16 relocs shouldn't really be used in shared
17104 libs or with non-local symbols as that will result in
17105 DT_TEXTREL being set, but support them anyway. */
17106 goto dodyn;
17107
17108 case R_PPC64_DTPREL16:
17109 case R_PPC64_DTPREL16_LO:
17110 case R_PPC64_DTPREL16_HI:
17111 case R_PPC64_DTPREL16_HA:
17112 case R_PPC64_DTPREL16_DS:
17113 case R_PPC64_DTPREL16_LO_DS:
17114 case R_PPC64_DTPREL16_HIGH:
17115 case R_PPC64_DTPREL16_HIGHA:
17116 case R_PPC64_DTPREL16_HIGHER:
17117 case R_PPC64_DTPREL16_HIGHERA:
17118 case R_PPC64_DTPREL16_HIGHEST:
17119 case R_PPC64_DTPREL16_HIGHESTA:
17120 case R_PPC64_DTPREL34:
17121 if (htab->elf.tls_sec != NULL)
17122 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17123 break;
17124
17125 case R_PPC64_ADDR64_LOCAL:
17126 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17127 ? h->elf.other
17128 : sym->st_other);
17129 break;
17130
17131 case R_PPC64_DTPMOD64:
17132 relocation = 1;
17133 addend = 0;
17134 goto dodyn;
17135
17136 case R_PPC64_TPREL64:
17137 if (htab->elf.tls_sec != NULL)
17138 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17139 goto dodyn;
17140
17141 case R_PPC64_DTPREL64:
17142 if (htab->elf.tls_sec != NULL)
17143 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17144 /* Fall through. */
17145
17146 /* Relocations that may need to be propagated if this is a
17147 dynamic object. */
17148 case R_PPC64_REL30:
17149 case R_PPC64_REL32:
17150 case R_PPC64_REL64:
17151 case R_PPC64_ADDR14:
17152 case R_PPC64_ADDR14_BRNTAKEN:
17153 case R_PPC64_ADDR14_BRTAKEN:
17154 case R_PPC64_ADDR16:
17155 case R_PPC64_ADDR16_DS:
17156 case R_PPC64_ADDR16_HA:
17157 case R_PPC64_ADDR16_HI:
17158 case R_PPC64_ADDR16_HIGH:
17159 case R_PPC64_ADDR16_HIGHA:
17160 case R_PPC64_ADDR16_HIGHER:
17161 case R_PPC64_ADDR16_HIGHERA:
17162 case R_PPC64_ADDR16_HIGHEST:
17163 case R_PPC64_ADDR16_HIGHESTA:
17164 case R_PPC64_ADDR16_LO:
17165 case R_PPC64_ADDR16_LO_DS:
17166 case R_PPC64_ADDR16_HIGHER34:
17167 case R_PPC64_ADDR16_HIGHERA34:
17168 case R_PPC64_ADDR16_HIGHEST34:
17169 case R_PPC64_ADDR16_HIGHESTA34:
17170 case R_PPC64_ADDR24:
17171 case R_PPC64_ADDR32:
17172 case R_PPC64_ADDR64:
17173 case R_PPC64_UADDR16:
17174 case R_PPC64_UADDR32:
17175 case R_PPC64_UADDR64:
17176 case R_PPC64_D34:
17177 case R_PPC64_D34_LO:
17178 case R_PPC64_D34_HI30:
17179 case R_PPC64_D34_HA30:
17180 case R_PPC64_D28:
17181 dodyn:
17182 if ((input_section->flags & SEC_ALLOC) == 0)
17183 break;
17184
17185 if (NO_OPD_RELOCS && is_opd)
17186 break;
17187
17188 if (bfd_link_pic (info)
17189 ? ((h == NULL
17190 || h->elf.dyn_relocs != NULL)
17191 && ((h != NULL && pc_dynrelocs (h))
17192 || must_be_dyn_reloc (info, r_type)))
17193 : (h != NULL
17194 ? h->elf.dyn_relocs != NULL
17195 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17196 {
17197 bool skip, relocate;
17198 asection *sreloc;
17199 bfd_vma out_off;
17200 long indx = 0;
17201
17202 /* When generating a dynamic object, these relocations
17203 are copied into the output file to be resolved at run
17204 time. */
17205
17206 skip = false;
17207 relocate = false;
17208
17209 out_off = _bfd_elf_section_offset (output_bfd, info,
17210 input_section, rel->r_offset);
17211 if (out_off == (bfd_vma) -1)
17212 skip = true;
17213 else if (out_off == (bfd_vma) -2)
17214 skip = true, relocate = true;
17215 out_off += (input_section->output_section->vma
17216 + input_section->output_offset);
17217 outrel.r_offset = out_off;
17218 outrel.r_addend = rel->r_addend;
17219
17220 /* Optimize unaligned reloc use. */
17221 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17222 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17223 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17224 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17225 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17226 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17227 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17228 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17229 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17230
17231 if (skip)
17232 memset (&outrel, 0, sizeof outrel);
17233 else if (h != NULL
17234 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17235 && !is_opd
17236 && r_type != R_PPC64_TOC)
17237 {
17238 indx = h->elf.dynindx;
17239 BFD_ASSERT (indx != -1);
17240 outrel.r_info = ELF64_R_INFO (indx, r_type);
17241 }
17242 else
17243 {
17244 /* This symbol is local, or marked to become local,
17245 or this is an opd section reloc which must point
17246 at a local function. */
17247 outrel.r_addend += relocation;
17248 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17249 {
17250 if (is_opd && h != NULL)
17251 {
17252 /* Lie about opd entries. This case occurs
17253 when building shared libraries and we
17254 reference a function in another shared
17255 lib. The same thing happens for a weak
17256 definition in an application that's
17257 overridden by a strong definition in a
17258 shared lib. (I believe this is a generic
17259 bug in binutils handling of weak syms.)
17260 In these cases we won't use the opd
17261 entry in this lib. */
17262 unresolved_reloc = false;
17263 }
17264 if (!is_opd
17265 && r_type == R_PPC64_ADDR64
17266 && (h != NULL
17267 ? h->elf.type == STT_GNU_IFUNC
17268 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17269 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17270 else
17271 {
17272 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17273
17274 /* We need to relocate .opd contents for ld.so.
17275 Prelink also wants simple and consistent rules
17276 for relocs. This make all RELATIVE relocs have
17277 *r_offset equal to r_addend. */
17278 relocate = true;
17279 }
17280 }
17281 else
17282 {
17283 if (h != NULL
17284 ? h->elf.type == STT_GNU_IFUNC
17285 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17286 {
17287 info->callbacks->einfo
17288 /* xgettext:c-format */
17289 (_("%H: %s for indirect "
17290 "function `%pT' unsupported\n"),
17291 input_bfd, input_section, rel->r_offset,
17292 ppc64_elf_howto_table[r_type]->name,
17293 sym_name);
17294 ret = false;
17295 }
17296 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17297 ;
17298 else if (sec == NULL || sec->owner == NULL)
17299 {
17300 bfd_set_error (bfd_error_bad_value);
17301 return false;
17302 }
17303 else
17304 {
17305 asection *osec = sec->output_section;
17306
17307 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17308 {
17309 /* TLS symbol values are relative to the
17310 TLS segment. Dynamic relocations for
17311 local TLS symbols therefore can't be
17312 reduced to a relocation against their
17313 section symbol because it holds the
17314 address of the section, not a value
17315 relative to the TLS segment. We could
17316 change the .tdata dynamic section symbol
17317 to be zero value but STN_UNDEF works
17318 and is used elsewhere, eg. for TPREL64
17319 GOT relocs against local TLS symbols. */
17320 osec = htab->elf.tls_sec;
17321 indx = 0;
17322 }
17323 else
17324 {
17325 indx = elf_section_data (osec)->dynindx;
17326 if (indx == 0)
17327 {
17328 if ((osec->flags & SEC_READONLY) == 0
17329 && htab->elf.data_index_section != NULL)
17330 osec = htab->elf.data_index_section;
17331 else
17332 osec = htab->elf.text_index_section;
17333 indx = elf_section_data (osec)->dynindx;
17334 }
17335 BFD_ASSERT (indx != 0);
17336 }
17337
17338 /* We are turning this relocation into one
17339 against a section symbol, so subtract out
17340 the output section's address but not the
17341 offset of the input section in the output
17342 section. */
17343 outrel.r_addend -= osec->vma;
17344 }
17345
17346 outrel.r_info = ELF64_R_INFO (indx, r_type);
17347 }
17348 }
17349
17350 if (!(info->enable_dt_relr
17351 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17352 && rel->r_offset % 2 == 0
17353 && input_section->alignment_power != 0
17354 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
17355 {
17356 sreloc = elf_section_data (input_section)->sreloc;
17357 if (h != NULL
17358 ? h->elf.type == STT_GNU_IFUNC
17359 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17360 {
17361 sreloc = htab->elf.irelplt;
17362 if (indx == 0 || is_static_defined (&h->elf))
17363 htab->elf.ifunc_resolvers = true;
17364 }
17365 if (sreloc == NULL)
17366 abort ();
17367
17368 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17369 >= sreloc->size)
17370 abort ();
17371 loc = sreloc->contents;
17372 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17373 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17374 }
17375
17376 if (!warned_dynamic
17377 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17378 {
17379 info->callbacks->einfo
17380 /* xgettext:c-format */
17381 (_("%X%P: %pB: %s against %pT "
17382 "is not supported by glibc as a dynamic relocation\n"),
17383 input_bfd,
17384 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17385 sym_name);
17386 warned_dynamic = true;
17387 }
17388
17389 /* If this reloc is against an external symbol, it will
17390 be computed at runtime, so there's no need to do
17391 anything now. However, for the sake of prelink ensure
17392 that the section contents are a known value. */
17393 if (!relocate)
17394 {
17395 unresolved_reloc = false;
17396 /* The value chosen here is quite arbitrary as ld.so
17397 ignores section contents except for the special
17398 case of .opd where the contents might be accessed
17399 before relocation. Choose zero, as that won't
17400 cause reloc overflow. */
17401 relocation = 0;
17402 addend = 0;
17403 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17404 to improve backward compatibility with older
17405 versions of ld. */
17406 if (r_type == R_PPC64_ADDR64)
17407 addend = outrel.r_addend;
17408 /* Adjust pc_relative relocs to have zero in *r_offset. */
17409 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17410 addend = outrel.r_offset;
17411 }
17412 }
17413 break;
17414
17415 case R_PPC64_COPY:
17416 case R_PPC64_GLOB_DAT:
17417 case R_PPC64_JMP_SLOT:
17418 case R_PPC64_JMP_IREL:
17419 case R_PPC64_RELATIVE:
17420 /* We shouldn't ever see these dynamic relocs in relocatable
17421 files. */
17422 /* Fall through. */
17423
17424 case R_PPC64_PLTGOT16:
17425 case R_PPC64_PLTGOT16_DS:
17426 case R_PPC64_PLTGOT16_HA:
17427 case R_PPC64_PLTGOT16_HI:
17428 case R_PPC64_PLTGOT16_LO:
17429 case R_PPC64_PLTGOT16_LO_DS:
17430 case R_PPC64_PLTREL32:
17431 case R_PPC64_PLTREL64:
17432 /* These ones haven't been implemented yet. */
17433
17434 info->callbacks->einfo
17435 /* xgettext:c-format */
17436 (_("%P: %pB: %s is not supported for `%pT'\n"),
17437 input_bfd,
17438 ppc64_elf_howto_table[r_type]->name, sym_name);
17439
17440 bfd_set_error (bfd_error_invalid_operation);
17441 ret = false;
17442 goto copy_reloc;
17443 }
17444
17445 /* Multi-instruction sequences that access the TOC can be
17446 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17447 to nop; addi rb,r2,x; */
17448 switch (r_type)
17449 {
17450 default:
17451 break;
17452
17453 case R_PPC64_GOT_TLSLD16_HI:
17454 case R_PPC64_GOT_TLSGD16_HI:
17455 case R_PPC64_GOT_TPREL16_HI:
17456 case R_PPC64_GOT_DTPREL16_HI:
17457 case R_PPC64_GOT16_HI:
17458 case R_PPC64_TOC16_HI:
17459 /* These relocs would only be useful if building up an
17460 offset to later add to r2, perhaps in an indexed
17461 addressing mode instruction. Don't try to optimize.
17462 Unfortunately, the possibility of someone building up an
17463 offset like this or even with the HA relocs, means that
17464 we need to check the high insn when optimizing the low
17465 insn. */
17466 break;
17467
17468 case R_PPC64_PLTCALL_NOTOC:
17469 if (!unresolved_reloc)
17470 htab->notoc_plt = 1;
17471 /* Fall through. */
17472 case R_PPC64_PLTCALL:
17473 if (unresolved_reloc
17474 && offset_in_range (input_section, rel->r_offset,
17475 r_type == R_PPC64_PLTCALL ? 8 : 4))
17476 {
17477 /* No plt entry. Make this into a direct call. */
17478 bfd_byte *p = contents + rel->r_offset;
17479 insn = bfd_get_32 (input_bfd, p);
17480 insn &= 1;
17481 bfd_put_32 (input_bfd, B_DOT | insn, p);
17482 if (r_type == R_PPC64_PLTCALL)
17483 bfd_put_32 (input_bfd, NOP, p + 4);
17484 unresolved_reloc = save_unresolved_reloc;
17485 r_type = R_PPC64_REL24;
17486 }
17487 break;
17488
17489 case R_PPC64_PLTSEQ_NOTOC:
17490 case R_PPC64_PLTSEQ:
17491 if (unresolved_reloc)
17492 {
17493 unresolved_reloc = false;
17494 goto nop_it;
17495 }
17496 break;
17497
17498 case R_PPC64_PLT_PCREL34_NOTOC:
17499 if (!unresolved_reloc)
17500 htab->notoc_plt = 1;
17501 /* Fall through. */
17502 case R_PPC64_PLT_PCREL34:
17503 if (unresolved_reloc
17504 && offset_in_range (input_section, rel->r_offset, 8))
17505 {
17506 bfd_byte *p = contents + rel->r_offset;
17507 bfd_put_32 (input_bfd, PNOP >> 32, p);
17508 bfd_put_32 (input_bfd, PNOP, p + 4);
17509 unresolved_reloc = false;
17510 goto copy_reloc;
17511 }
17512 break;
17513
17514 case R_PPC64_PLT16_HA:
17515 if (unresolved_reloc)
17516 {
17517 unresolved_reloc = false;
17518 goto nop_it;
17519 }
17520 /* Fall through. */
17521 case R_PPC64_GOT_TLSLD16_HA:
17522 case R_PPC64_GOT_TLSGD16_HA:
17523 case R_PPC64_GOT_TPREL16_HA:
17524 case R_PPC64_GOT_DTPREL16_HA:
17525 case R_PPC64_GOT16_HA:
17526 case R_PPC64_TOC16_HA:
17527 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17528 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
17529 {
17530 bfd_byte *p;
17531 nop_it:
17532 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17533 {
17534 p = contents + (rel->r_offset & ~3);
17535 bfd_put_32 (input_bfd, NOP, p);
17536 goto copy_reloc;
17537 }
17538 }
17539 break;
17540
17541 case R_PPC64_PLT16_LO:
17542 case R_PPC64_PLT16_LO_DS:
17543 if (unresolved_reloc)
17544 {
17545 unresolved_reloc = false;
17546 goto nop_it;
17547 }
17548 /* Fall through. */
17549 case R_PPC64_GOT_TLSLD16_LO:
17550 case R_PPC64_GOT_TLSGD16_LO:
17551 case R_PPC64_GOT_TPREL16_LO_DS:
17552 case R_PPC64_GOT_DTPREL16_LO_DS:
17553 case R_PPC64_GOT16_LO:
17554 case R_PPC64_GOT16_LO_DS:
17555 case R_PPC64_TOC16_LO:
17556 case R_PPC64_TOC16_LO_DS:
17557 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17558 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17559 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17560 {
17561 bfd_byte *p = contents + (rel->r_offset & ~3);
17562 insn = bfd_get_32 (input_bfd, p);
17563 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17564 {
17565 /* Transform addic to addi when we change reg. */
17566 insn &= ~((0x3fu << 26) | (0x1f << 16));
17567 insn |= (14u << 26) | (2 << 16);
17568 }
17569 else
17570 {
17571 insn &= ~(0x1f << 16);
17572 insn |= 2 << 16;
17573 }
17574 bfd_put_32 (input_bfd, insn, p);
17575 }
17576 break;
17577
17578 case R_PPC64_TPREL16_HA:
17579 if (htab->do_tls_opt
17580 && relocation + addend + 0x8000 < 0x10000
17581 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17582 {
17583 bfd_byte *p = contents + (rel->r_offset & ~3);
17584 bfd_put_32 (input_bfd, NOP, p);
17585 goto copy_reloc;
17586 }
17587 break;
17588
17589 case R_PPC64_TPREL16_LO:
17590 case R_PPC64_TPREL16_LO_DS:
17591 if (htab->do_tls_opt
17592 && relocation + addend + 0x8000 < 0x10000
17593 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17594 {
17595 bfd_byte *p = contents + (rel->r_offset & ~3);
17596 insn = bfd_get_32 (input_bfd, p);
17597 insn &= ~(0x1f << 16);
17598 insn |= 13 << 16;
17599 bfd_put_32 (input_bfd, insn, p);
17600 }
17601 break;
17602 }
17603
17604 /* Do any further special processing. */
17605 switch (r_type)
17606 {
17607 default:
17608 break;
17609
17610 case R_PPC64_REL16_HA:
17611 case R_PPC64_REL16_HIGHA:
17612 case R_PPC64_REL16_HIGHERA:
17613 case R_PPC64_REL16_HIGHESTA:
17614 case R_PPC64_REL16DX_HA:
17615 case R_PPC64_ADDR16_HA:
17616 case R_PPC64_ADDR16_HIGHA:
17617 case R_PPC64_ADDR16_HIGHERA:
17618 case R_PPC64_ADDR16_HIGHESTA:
17619 case R_PPC64_TOC16_HA:
17620 case R_PPC64_SECTOFF_HA:
17621 case R_PPC64_TPREL16_HA:
17622 case R_PPC64_TPREL16_HIGHA:
17623 case R_PPC64_TPREL16_HIGHERA:
17624 case R_PPC64_TPREL16_HIGHESTA:
17625 case R_PPC64_DTPREL16_HA:
17626 case R_PPC64_DTPREL16_HIGHA:
17627 case R_PPC64_DTPREL16_HIGHERA:
17628 case R_PPC64_DTPREL16_HIGHESTA:
17629 /* It's just possible that this symbol is a weak symbol
17630 that's not actually defined anywhere. In that case,
17631 'sec' would be NULL, and we should leave the symbol
17632 alone (it will be set to zero elsewhere in the link). */
17633 if (sec == NULL)
17634 break;
17635 /* Fall through. */
17636
17637 case R_PPC64_GOT16_HA:
17638 case R_PPC64_PLTGOT16_HA:
17639 case R_PPC64_PLT16_HA:
17640 case R_PPC64_GOT_TLSGD16_HA:
17641 case R_PPC64_GOT_TLSLD16_HA:
17642 case R_PPC64_GOT_TPREL16_HA:
17643 case R_PPC64_GOT_DTPREL16_HA:
17644 /* Add 0x10000 if sign bit in 0:15 is set.
17645 Bits 0:15 are not used. */
17646 addend += 0x8000;
17647 break;
17648
17649 case R_PPC64_D34_HA30:
17650 case R_PPC64_ADDR16_HIGHERA34:
17651 case R_PPC64_ADDR16_HIGHESTA34:
17652 case R_PPC64_REL16_HIGHERA34:
17653 case R_PPC64_REL16_HIGHESTA34:
17654 if (sec != NULL)
17655 addend += 1ULL << 33;
17656 break;
17657
17658 case R_PPC64_ADDR16_DS:
17659 case R_PPC64_ADDR16_LO_DS:
17660 case R_PPC64_GOT16_DS:
17661 case R_PPC64_GOT16_LO_DS:
17662 case R_PPC64_PLT16_LO_DS:
17663 case R_PPC64_SECTOFF_DS:
17664 case R_PPC64_SECTOFF_LO_DS:
17665 case R_PPC64_TOC16_DS:
17666 case R_PPC64_TOC16_LO_DS:
17667 case R_PPC64_PLTGOT16_DS:
17668 case R_PPC64_PLTGOT16_LO_DS:
17669 case R_PPC64_GOT_TPREL16_DS:
17670 case R_PPC64_GOT_TPREL16_LO_DS:
17671 case R_PPC64_GOT_DTPREL16_DS:
17672 case R_PPC64_GOT_DTPREL16_LO_DS:
17673 case R_PPC64_TPREL16_DS:
17674 case R_PPC64_TPREL16_LO_DS:
17675 case R_PPC64_DTPREL16_DS:
17676 case R_PPC64_DTPREL16_LO_DS:
17677 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17678 break;
17679 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17680 mask = 3;
17681 /* If this reloc is against an lq, lxv, or stxv insn, then
17682 the value must be a multiple of 16. This is somewhat of
17683 a hack, but the "correct" way to do this by defining _DQ
17684 forms of all the _DS relocs bloats all reloc switches in
17685 this file. It doesn't make much sense to use these
17686 relocs in data, so testing the insn should be safe. */
17687 if ((insn & (0x3fu << 26)) == (56u << 26)
17688 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17689 mask = 15;
17690 relocation += addend;
17691 addend = insn & (mask ^ 3);
17692 if ((relocation & mask) != 0)
17693 {
17694 relocation ^= relocation & mask;
17695 info->callbacks->einfo
17696 /* xgettext:c-format */
17697 (_("%H: error: %s not a multiple of %u\n"),
17698 input_bfd, input_section, rel->r_offset,
17699 ppc64_elf_howto_table[r_type]->name,
17700 mask + 1);
17701 bfd_set_error (bfd_error_bad_value);
17702 ret = false;
17703 goto copy_reloc;
17704 }
17705 break;
17706 }
17707
17708 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17709 because such sections are not SEC_ALLOC and thus ld.so will
17710 not process them. */
17711 howto = ppc64_elf_howto_table[(int) r_type];
17712 if (unresolved_reloc
17713 && !((input_section->flags & SEC_DEBUGGING) != 0
17714 && h->elf.def_dynamic)
17715 && _bfd_elf_section_offset (output_bfd, info, input_section,
17716 rel->r_offset) != (bfd_vma) -1)
17717 {
17718 info->callbacks->einfo
17719 /* xgettext:c-format */
17720 (_("%H: unresolvable %s against `%pT'\n"),
17721 input_bfd, input_section, rel->r_offset,
17722 howto->name,
17723 h->elf.root.root.string);
17724 ret = false;
17725 }
17726
17727 /* 16-bit fields in insns mostly have signed values, but a
17728 few insns have 16-bit unsigned values. Really, we should
17729 have different reloc types. */
17730 if (howto->complain_on_overflow != complain_overflow_dont
17731 && howto->dst_mask == 0xffff
17732 && (input_section->flags & SEC_CODE) != 0
17733 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17734 {
17735 enum complain_overflow complain = complain_overflow_signed;
17736
17737 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17738 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17739 complain = complain_overflow_bitfield;
17740 else if (howto->rightshift == 0
17741 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17742 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17743 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17744 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17745 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17746 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17747 complain = complain_overflow_unsigned;
17748 if (howto->complain_on_overflow != complain)
17749 {
17750 alt_howto = *howto;
17751 alt_howto.complain_on_overflow = complain;
17752 howto = &alt_howto;
17753 }
17754 }
17755
17756 switch (r_type)
17757 {
17758 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17759 case R_PPC64_D34:
17760 case R_PPC64_D34_LO:
17761 case R_PPC64_D34_HI30:
17762 case R_PPC64_D34_HA30:
17763 case R_PPC64_PCREL34:
17764 case R_PPC64_GOT_PCREL34:
17765 case R_PPC64_TPREL34:
17766 case R_PPC64_DTPREL34:
17767 case R_PPC64_GOT_TLSGD_PCREL34:
17768 case R_PPC64_GOT_TLSLD_PCREL34:
17769 case R_PPC64_GOT_TPREL_PCREL34:
17770 case R_PPC64_GOT_DTPREL_PCREL34:
17771 case R_PPC64_PLT_PCREL34:
17772 case R_PPC64_PLT_PCREL34_NOTOC:
17773 case R_PPC64_D28:
17774 case R_PPC64_PCREL28:
17775 if (!offset_in_range (input_section, rel->r_offset, 8))
17776 r = bfd_reloc_outofrange;
17777 else
17778 {
17779 relocation += addend;
17780 if (howto->pc_relative)
17781 relocation -= (rel->r_offset
17782 + input_section->output_offset
17783 + input_section->output_section->vma);
17784 relocation >>= howto->rightshift;
17785
17786 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17787 pinsn <<= 32;
17788 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17789
17790 pinsn &= ~howto->dst_mask;
17791 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17792 & howto->dst_mask);
17793 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17794 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17795 r = bfd_reloc_ok;
17796 if (howto->complain_on_overflow == complain_overflow_signed
17797 && (relocation + (1ULL << (howto->bitsize - 1))
17798 >= 1ULL << howto->bitsize))
17799 r = bfd_reloc_overflow;
17800 }
17801 break;
17802
17803 case R_PPC64_REL16DX_HA:
17804 if (!offset_in_range (input_section, rel->r_offset, 4))
17805 r = bfd_reloc_outofrange;
17806 else
17807 {
17808 relocation += addend;
17809 relocation -= (rel->r_offset
17810 + input_section->output_offset
17811 + input_section->output_section->vma);
17812 relocation = (bfd_signed_vma) relocation >> 16;
17813 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17814 insn &= ~0x1fffc1;
17815 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17816 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17817 r = bfd_reloc_ok;
17818 if (relocation + 0x8000 > 0xffff)
17819 r = bfd_reloc_overflow;
17820 }
17821 break;
17822
17823 default:
17824 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17825 contents, rel->r_offset,
17826 relocation, addend);
17827 }
17828
17829 if (r != bfd_reloc_ok)
17830 {
17831 char *more_info = NULL;
17832 const char *reloc_name = howto->name;
17833
17834 if (reloc_dest != DEST_NORMAL)
17835 {
17836 more_info = bfd_malloc (strlen (reloc_name) + 8);
17837 if (more_info != NULL)
17838 {
17839 strcpy (more_info, reloc_name);
17840 strcat (more_info, (reloc_dest == DEST_OPD
17841 ? " (OPD)" : " (stub)"));
17842 reloc_name = more_info;
17843 }
17844 }
17845
17846 if (r == bfd_reloc_overflow)
17847 {
17848 /* On code like "if (foo) foo();" don't report overflow
17849 on a branch to zero when foo is undefined. */
17850 if (!warned
17851 && (reloc_dest == DEST_STUB
17852 || !(h != NULL
17853 && (h->elf.root.type == bfd_link_hash_undefweak
17854 || h->elf.root.type == bfd_link_hash_undefined)
17855 && is_branch_reloc (r_type))))
17856 info->callbacks->reloc_overflow
17857 (info, (struct bfd_link_hash_entry *) h, sym_name,
17858 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17859 rel->r_offset);
17860 }
17861 else
17862 {
17863 info->callbacks->einfo
17864 /* xgettext:c-format */
17865 (_("%H: %s against `%pT': error %d\n"),
17866 input_bfd, input_section, rel->r_offset,
17867 reloc_name, sym_name, (int) r);
17868 ret = false;
17869 }
17870 free (more_info);
17871 }
17872 copy_reloc:
17873 if (wrel != rel)
17874 *wrel = *rel;
17875 }
17876
17877 if (wrel != rel)
17878 {
17879 Elf_Internal_Shdr *rel_hdr;
17880 size_t deleted = rel - wrel;
17881
17882 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17883 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17884 if (rel_hdr->sh_size == 0)
17885 {
17886 /* It is too late to remove an empty reloc section. Leave
17887 one NONE reloc.
17888 ??? What is wrong with an empty section??? */
17889 rel_hdr->sh_size = rel_hdr->sh_entsize;
17890 deleted -= 1;
17891 }
17892 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17893 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17894 input_section->reloc_count -= deleted;
17895 }
17896
17897 /* If we're emitting relocations, then shortly after this function
17898 returns, reloc offsets and addends for this section will be
17899 adjusted. Worse, reloc symbol indices will be for the output
17900 file rather than the input. Save a copy of the relocs for
17901 opd_entry_value. */
17902 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
17903 {
17904 bfd_size_type amt;
17905 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17906 rel = bfd_alloc (input_bfd, amt);
17907 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17908 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
17909 if (rel == NULL)
17910 return false;
17911 memcpy (rel, relocs, amt);
17912 }
17913 return ret;
17914 }
17915
17916 /* Adjust the value of any local symbols in opd sections. */
17917
17918 static int
17919 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17920 const char *name ATTRIBUTE_UNUSED,
17921 Elf_Internal_Sym *elfsym,
17922 asection *input_sec,
17923 struct elf_link_hash_entry *h)
17924 {
17925 struct _opd_sec_data *opd;
17926 long adjust;
17927 bfd_vma value;
17928
17929 if (h != NULL)
17930 return 1;
17931
17932 opd = get_opd_info (input_sec);
17933 if (opd == NULL || opd->adjust == NULL)
17934 return 1;
17935
17936 value = elfsym->st_value - input_sec->output_offset;
17937 if (!bfd_link_relocatable (info))
17938 value -= input_sec->output_section->vma;
17939
17940 adjust = opd->adjust[OPD_NDX (value)];
17941 if (adjust == -1)
17942 return 2;
17943
17944 elfsym->st_value += adjust;
17945 return 1;
17946 }
17947
17948 /* Finish up dynamic symbol handling. We set the contents of various
17949 dynamic sections here. */
17950
17951 static bool
17952 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17953 struct bfd_link_info *info,
17954 struct elf_link_hash_entry *h,
17955 Elf_Internal_Sym *sym)
17956 {
17957 struct ppc_link_hash_table *htab;
17958 struct plt_entry *ent;
17959
17960 htab = ppc_hash_table (info);
17961 if (htab == NULL)
17962 return false;
17963
17964 if (!htab->opd_abi && !h->def_regular)
17965 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17966 if (ent->plt.offset != (bfd_vma) -1)
17967 {
17968 /* Mark the symbol as undefined, rather than as
17969 defined in glink. Leave the value if there were
17970 any relocations where pointer equality matters
17971 (this is a clue for the dynamic linker, to make
17972 function pointer comparisons work between an
17973 application and shared library), otherwise set it
17974 to zero. */
17975 sym->st_shndx = SHN_UNDEF;
17976 if (!h->pointer_equality_needed)
17977 sym->st_value = 0;
17978 else if (!h->ref_regular_nonweak)
17979 {
17980 /* This breaks function pointer comparisons, but
17981 that is better than breaking tests for a NULL
17982 function pointer. */
17983 sym->st_value = 0;
17984 }
17985 break;
17986 }
17987
17988 if (h->needs_copy
17989 && (h->root.type == bfd_link_hash_defined
17990 || h->root.type == bfd_link_hash_defweak)
17991 && (h->root.u.def.section == htab->elf.sdynbss
17992 || h->root.u.def.section == htab->elf.sdynrelro))
17993 {
17994 /* This symbol needs a copy reloc. Set it up. */
17995 Elf_Internal_Rela rela;
17996 asection *srel;
17997 bfd_byte *loc;
17998
17999 if (h->dynindx == -1)
18000 abort ();
18001
18002 rela.r_offset = defined_sym_val (h);
18003 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18004 rela.r_addend = 0;
18005 if (h->root.u.def.section == htab->elf.sdynrelro)
18006 srel = htab->elf.sreldynrelro;
18007 else
18008 srel = htab->elf.srelbss;
18009 loc = srel->contents;
18010 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
18011 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
18012 }
18013
18014 return true;
18015 }
18016
18017 /* Used to decide how to sort relocs in an optimal manner for the
18018 dynamic linker, before writing them out. */
18019
18020 static enum elf_reloc_type_class
18021 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18022 const asection *rel_sec,
18023 const Elf_Internal_Rela *rela)
18024 {
18025 enum elf_ppc64_reloc_type r_type;
18026 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18027
18028 if (rel_sec == htab->elf.irelplt)
18029 return reloc_class_ifunc;
18030
18031 r_type = ELF64_R_TYPE (rela->r_info);
18032 switch (r_type)
18033 {
18034 case R_PPC64_RELATIVE:
18035 return reloc_class_relative;
18036 case R_PPC64_JMP_SLOT:
18037 return reloc_class_plt;
18038 case R_PPC64_COPY:
18039 return reloc_class_copy;
18040 default:
18041 return reloc_class_normal;
18042 }
18043 }
18044
18045 /* Finish up the dynamic sections. */
18046
18047 static bool
18048 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18049 struct bfd_link_info *info)
18050 {
18051 struct ppc_link_hash_table *htab;
18052 bfd *dynobj;
18053 asection *sdyn;
18054
18055 htab = ppc_hash_table (info);
18056 if (htab == NULL)
18057 return false;
18058
18059 dynobj = htab->elf.dynobj;
18060 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18061
18062 if (htab->elf.dynamic_sections_created)
18063 {
18064 Elf64_External_Dyn *dyncon, *dynconend;
18065
18066 if (sdyn == NULL || htab->elf.sgot == NULL)
18067 abort ();
18068
18069 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18070 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18071 for (; dyncon < dynconend; dyncon++)
18072 {
18073 Elf_Internal_Dyn dyn;
18074 asection *s;
18075
18076 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18077
18078 switch (dyn.d_tag)
18079 {
18080 default:
18081 continue;
18082
18083 case DT_PPC64_GLINK:
18084 s = htab->glink;
18085 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18086 /* We stupidly defined DT_PPC64_GLINK to be the start
18087 of glink rather than the first entry point, which is
18088 what ld.so needs, and now have a bigger stub to
18089 support automatic multiple TOCs. */
18090 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18091 break;
18092
18093 case DT_PPC64_OPD:
18094 s = bfd_get_section_by_name (output_bfd, ".opd");
18095 if (s == NULL)
18096 continue;
18097 dyn.d_un.d_ptr = s->vma;
18098 break;
18099
18100 case DT_PPC64_OPT:
18101 if ((htab->do_multi_toc && htab->multi_toc_needed)
18102 || htab->notoc_plt)
18103 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18104 if (htab->has_plt_localentry0)
18105 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18106 break;
18107
18108 case DT_PPC64_OPDSZ:
18109 s = bfd_get_section_by_name (output_bfd, ".opd");
18110 if (s == NULL)
18111 continue;
18112 dyn.d_un.d_val = s->size;
18113 break;
18114
18115 case DT_PLTGOT:
18116 s = htab->elf.splt;
18117 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18118 break;
18119
18120 case DT_JMPREL:
18121 s = htab->elf.srelplt;
18122 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18123 break;
18124
18125 case DT_PLTRELSZ:
18126 dyn.d_un.d_val = htab->elf.srelplt->size;
18127 break;
18128
18129 case DT_TEXTREL:
18130 if (htab->elf.ifunc_resolvers)
18131 info->callbacks->einfo
18132 (_("%P: warning: text relocations and GNU indirect "
18133 "functions may result in a segfault at runtime\n"));
18134 continue;
18135 }
18136
18137 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18138 }
18139 }
18140
18141 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18142 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18143 {
18144 /* Fill in the first entry in the global offset table.
18145 We use it to hold the link-time TOCbase. */
18146 bfd_put_64 (output_bfd,
18147 elf_gp (output_bfd) + TOC_BASE_OFF,
18148 htab->elf.sgot->contents);
18149
18150 /* Set .got entry size. */
18151 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18152 = 8;
18153 }
18154
18155 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18156 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18157 {
18158 /* Set .plt entry size. */
18159 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18160 = PLT_ENTRY_SIZE (htab);
18161 }
18162
18163 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18164 brlt ourselves if emitrelocations. */
18165 if (htab->brlt != NULL
18166 && htab->brlt->reloc_count != 0
18167 && !_bfd_elf_link_output_relocs (output_bfd,
18168 htab->brlt,
18169 elf_section_data (htab->brlt)->rela.hdr,
18170 elf_section_data (htab->brlt)->relocs,
18171 NULL))
18172 return false;
18173
18174 if (htab->glink != NULL
18175 && htab->glink->reloc_count != 0
18176 && !_bfd_elf_link_output_relocs (output_bfd,
18177 htab->glink,
18178 elf_section_data (htab->glink)->rela.hdr,
18179 elf_section_data (htab->glink)->relocs,
18180 NULL))
18181 return false;
18182
18183
18184 if (htab->glink_eh_frame != NULL
18185 && htab->glink_eh_frame->size != 0
18186 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18187 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18188 htab->glink_eh_frame,
18189 htab->glink_eh_frame->contents))
18190 return false;
18191
18192 /* We need to handle writing out multiple GOT sections ourselves,
18193 since we didn't add them to DYNOBJ. We know dynobj is the first
18194 bfd. */
18195 while ((dynobj = dynobj->link.next) != NULL)
18196 {
18197 asection *s;
18198
18199 if (!is_ppc64_elf (dynobj))
18200 continue;
18201
18202 s = ppc64_elf_tdata (dynobj)->got;
18203 if (s != NULL
18204 && s->size != 0
18205 && s->output_section != bfd_abs_section_ptr
18206 && !bfd_set_section_contents (output_bfd, s->output_section,
18207 s->contents, s->output_offset,
18208 s->size))
18209 return false;
18210 s = ppc64_elf_tdata (dynobj)->relgot;
18211 if (s != NULL
18212 && s->size != 0
18213 && s->output_section != bfd_abs_section_ptr
18214 && !bfd_set_section_contents (output_bfd, s->output_section,
18215 s->contents, s->output_offset,
18216 s->size))
18217 return false;
18218 }
18219
18220 return true;
18221 }
18222
18223 #include "elf64-target.h"
18224
18225 /* FreeBSD support */
18226
18227 #undef TARGET_LITTLE_SYM
18228 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18229 #undef TARGET_LITTLE_NAME
18230 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18231
18232 #undef TARGET_BIG_SYM
18233 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18234 #undef TARGET_BIG_NAME
18235 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18236
18237 #undef ELF_OSABI
18238 #define ELF_OSABI ELFOSABI_FREEBSD
18239
18240 #undef elf64_bed
18241 #define elf64_bed elf64_powerpc_fbsd_bed
18242
18243 #include "elf64-target.h"