elf64-ppc.c revision 1.1.1.12 1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info (at) swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28 /* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
32
33 #include "sysdep.h"
34 #include <stdarg.h>
35 #include "bfd.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #include "elf-bfd.h"
39 #include "elf/ppc64.h"
40 #include "elf64-ppc.h"
41 #include "dwarf2.h"
42
43 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44 #define OCTETS_PER_BYTE(ABFD, SEC) 1
45
46 static bfd_reloc_status_type ppc64_elf_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_toc_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type ppc64_elf_toc64_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_vma opd_entry_value
67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
68
69 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
70 #define TARGET_LITTLE_NAME "elf64-powerpcle"
71 #define TARGET_BIG_SYM powerpc_elf64_vec
72 #define TARGET_BIG_NAME "elf64-powerpc"
73 #define ELF_ARCH bfd_arch_powerpc
74 #define ELF_TARGET_ID PPC64_ELF_DATA
75 #define ELF_MACHINE_CODE EM_PPC64
76 #define ELF_MAXPAGESIZE 0x10000
77 #define ELF_COMMONPAGESIZE 0x1000
78 #define elf_info_to_howto ppc64_elf_info_to_howto
79
80 #define elf_backend_want_got_sym 0
81 #define elf_backend_want_plt_sym 0
82 #define elf_backend_plt_alignment 3
83 #define elf_backend_plt_not_loaded 1
84 #define elf_backend_got_header_size 8
85 #define elf_backend_want_dynrelro 1
86 #define elf_backend_can_gc_sections 1
87 #define elf_backend_can_refcount 1
88 #define elf_backend_rela_normal 1
89 #define elf_backend_dtrel_excludes_plt 1
90 #define elf_backend_default_execstack 0
91
92 #define bfd_elf64_mkobject ppc64_elf_mkobject
93 #define bfd_elf64_bfd_free_cached_info ppc64_elf_free_cached_info
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_early_size_sections ppc64_elf_edit
123 #define elf_backend_late_size_sections ppc64_elf_late_size_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
301 #define is_ppc64_elf(bfd) \
302 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
303 && elf_object_id (bfd) == PPC64_ELF_DATA)
304
305 /* Relocation HOWTO's. */
307 /* Like other ELF RELA targets that don't apply multiple
308 field-altering relocations to the same localation, src_mask is
309 always zero and pcrel_offset is the same as pc_relative.
310 PowerPC can always use a zero bitpos, even when the field is not at
311 the LSB. For example, a REL24 could use rightshift=2, bisize=24
312 and bitpos=2 which matches the ABI description, or as we do here,
313 rightshift=0, bitsize=26 and bitpos=0. */
314 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
315 complain, special_func) \
316 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
317 complain_overflow_ ## complain, special_func, \
318 #type, false, 0, mask, pc_relative)
319
320 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
321
322 static reloc_howto_type ppc64_elf_howto_raw[] =
323 {
324 /* This reloc does nothing. */
325 HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
326 bfd_elf_generic_reloc),
327
328 /* A standard 32 bit relocation. */
329 HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
330 bfd_elf_generic_reloc),
331
332 /* An absolute 26 bit branch; the lower two bits must be zero.
333 FIXME: we don't check that, we just clear them. */
334 HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
335 bfd_elf_generic_reloc),
336
337 /* A standard 16 bit relocation. */
338 HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
339 bfd_elf_generic_reloc),
340
341 /* A 16 bit relocation without overflow. */
342 HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
343 bfd_elf_generic_reloc),
344
345 /* Bits 16-31 of an address. */
346 HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
347 bfd_elf_generic_reloc),
348
349 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
350 bits, treated as a signed number, is negative. */
351 HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
352 ppc64_elf_ha_reloc),
353
354 /* An absolute 16 bit branch; the lower two bits must be zero.
355 FIXME: we don't check that, we just clear them. */
356 HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
357 ppc64_elf_branch_reloc),
358
359 /* An absolute 16 bit branch, for which bit 10 should be set to
360 indicate that the branch is expected to be taken. The lower two
361 bits must be zero. */
362 HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
363 ppc64_elf_brtaken_reloc),
364
365 /* An absolute 16 bit branch, for which bit 10 should be set to
366 indicate that the branch is not expected to be taken. The lower
367 two bits must be zero. */
368 HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
369 ppc64_elf_brtaken_reloc),
370
371 /* A relative 26 bit branch; the lower two bits must be zero. */
372 HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
373 ppc64_elf_branch_reloc),
374
375 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
376 HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
377 ppc64_elf_branch_reloc),
378
379 /* Another variant, when p10 insns can't be used on stubs. */
380 HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
381 ppc64_elf_branch_reloc),
382
383 /* A relative 16 bit branch; the lower two bits must be zero. */
384 HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
385 ppc64_elf_branch_reloc),
386
387 /* A relative 16 bit branch. Bit 10 should be set to indicate that
388 the branch is expected to be taken. The lower two bits must be
389 zero. */
390 HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
391 ppc64_elf_brtaken_reloc),
392
393 /* A relative 16 bit branch. Bit 10 should be set to indicate that
394 the branch is not expected to be taken. The lower two bits must
395 be zero. */
396 HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
397 ppc64_elf_brtaken_reloc),
398
399 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
400 symbol. */
401 HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
402 ppc64_elf_unhandled_reloc),
403
404 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
405 the symbol. */
406 HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
407 ppc64_elf_unhandled_reloc),
408
409 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
410 the symbol. */
411 HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
412 ppc64_elf_unhandled_reloc),
413
414 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
415 the symbol. */
416 HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
417 ppc64_elf_unhandled_reloc),
418
419 /* This is used only by the dynamic linker. The symbol should exist
420 both in the object being run and in some shared library. The
421 dynamic linker copies the data addressed by the symbol from the
422 shared library into the object, because the object being
423 run has to have the data at some particular address. */
424 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
425 ppc64_elf_unhandled_reloc),
426
427 /* Like R_PPC64_ADDR64, but used when setting global offset table
428 entries. */
429 HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
430 ppc64_elf_unhandled_reloc),
431
432 /* Created by the link editor. Marks a procedure linkage table
433 entry for a symbol. */
434 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
435 ppc64_elf_unhandled_reloc),
436
437 /* Used only by the dynamic linker. When the object is run, this
438 doubleword64 is set to the load address of the object, plus the
439 addend. */
440 HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
441 bfd_elf_generic_reloc),
442
443 /* Like R_PPC64_ADDR32, but may be unaligned. */
444 HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
445 bfd_elf_generic_reloc),
446
447 /* Like R_PPC64_ADDR16, but may be unaligned. */
448 HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
449 bfd_elf_generic_reloc),
450
451 /* 32-bit PC relative. */
452 HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
453 bfd_elf_generic_reloc),
454
455 /* 32-bit relocation to the symbol's procedure linkage table. */
456 HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
457 ppc64_elf_unhandled_reloc),
458
459 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
460 FIXME: R_PPC64_PLTREL32 not supported. */
461 HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
462 ppc64_elf_unhandled_reloc),
463
464 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
465 the symbol. */
466 HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
467 ppc64_elf_unhandled_reloc),
468
469 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
470 the symbol. */
471 HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
472 ppc64_elf_unhandled_reloc),
473
474 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
475 the symbol. */
476 HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
477 ppc64_elf_unhandled_reloc),
478
479 /* 16-bit section relative relocation. */
480 HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
481 ppc64_elf_sectoff_reloc),
482
483 /* Like R_PPC64_SECTOFF, but no overflow warning. */
484 HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
485 ppc64_elf_sectoff_reloc),
486
487 /* 16-bit upper half section relative relocation. */
488 HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
489 ppc64_elf_sectoff_reloc),
490
491 /* 16-bit upper half adjusted section relative relocation. */
492 HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
493 ppc64_elf_sectoff_ha_reloc),
494
495 /* Like R_PPC64_REL24 without touching the two least significant bits. */
496 HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
497 bfd_elf_generic_reloc),
498
499 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
500
501 /* A standard 64-bit relocation. */
502 HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
503 bfd_elf_generic_reloc),
504
505 /* The bits 32-47 of an address. */
506 HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
507 bfd_elf_generic_reloc),
508
509 /* The bits 32-47 of an address, plus 1 if the contents of the low
510 16 bits, treated as a signed number, is negative. */
511 HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
512 ppc64_elf_ha_reloc),
513
514 /* The bits 48-63 of an address. */
515 HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
516 bfd_elf_generic_reloc),
517
518 /* The bits 48-63 of an address, plus 1 if the contents of the low
519 16 bits, treated as a signed number, is negative. */
520 HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
521 ppc64_elf_ha_reloc),
522
523 /* Like ADDR64, but may be unaligned. */
524 HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
525 bfd_elf_generic_reloc),
526
527 /* 64-bit relative relocation. */
528 HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
529 bfd_elf_generic_reloc),
530
531 /* 64-bit relocation to the symbol's procedure linkage table. */
532 HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
533 ppc64_elf_unhandled_reloc),
534
535 /* 64-bit PC relative relocation to the symbol's procedure linkage
536 table. */
537 /* FIXME: R_PPC64_PLTREL64 not supported. */
538 HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
539 ppc64_elf_unhandled_reloc),
540
541 /* 16 bit TOC-relative relocation. */
542 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
543 HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
544 ppc64_elf_toc_reloc),
545
546 /* 16 bit TOC-relative relocation without overflow. */
547 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
548 HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
549 ppc64_elf_toc_reloc),
550
551 /* 16 bit TOC-relative relocation, high 16 bits. */
552 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
553 HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
554 ppc64_elf_toc_reloc),
555
556 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
557 contents of the low 16 bits, treated as a signed number, is
558 negative. */
559 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
560 HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
561 ppc64_elf_toc_ha_reloc),
562
563 /* 64-bit relocation; insert value of TOC base (.TOC.). */
564 /* R_PPC64_TOC 51 doubleword64 .TOC. */
565 HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
566 ppc64_elf_toc64_reloc),
567
568 /* Like R_PPC64_GOT16, but also informs the link editor that the
569 value to relocate may (!) refer to a PLT entry which the link
570 editor (a) may replace with the symbol value. If the link editor
571 is unable to fully resolve the symbol, it may (b) create a PLT
572 entry and store the address to the new PLT entry in the GOT.
573 This permits lazy resolution of function symbols at run time.
574 The link editor may also skip all of this and just (c) emit a
575 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
576 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
577 HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
578 ppc64_elf_unhandled_reloc),
579
580 /* Like R_PPC64_PLTGOT16, but without overflow. */
581 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
582 HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
583 ppc64_elf_unhandled_reloc),
584
585 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
586 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
587 HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
588 ppc64_elf_unhandled_reloc),
589
590 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
591 1 if the contents of the low 16 bits, treated as a signed number,
592 is negative. */
593 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
594 HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
595 ppc64_elf_unhandled_reloc),
596
597 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
598 HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
599 bfd_elf_generic_reloc),
600
601 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
602 HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
603 bfd_elf_generic_reloc),
604
605 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
606 HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
607 ppc64_elf_unhandled_reloc),
608
609 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
610 HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
611 ppc64_elf_unhandled_reloc),
612
613 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
614 HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
615 ppc64_elf_unhandled_reloc),
616
617 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
618 HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
619 ppc64_elf_sectoff_reloc),
620
621 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
622 HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
623 ppc64_elf_sectoff_reloc),
624
625 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
626 HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
627 ppc64_elf_toc_reloc),
628
629 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
630 HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
631 ppc64_elf_toc_reloc),
632
633 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
634 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
635 HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
636 ppc64_elf_unhandled_reloc),
637
638 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
639 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
640 HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
641 ppc64_elf_unhandled_reloc),
642
643 /* Marker relocs for TLS. */
644 HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
645 bfd_elf_generic_reloc),
646
647 HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
648 bfd_elf_generic_reloc),
649
650 HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
651 bfd_elf_generic_reloc),
652
653 /* Marker reloc for optimizing r2 save in prologue rather than on
654 each plt call stub. */
655 HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
656 bfd_elf_generic_reloc),
657
658 /* Marker relocs on inline plt call instructions. */
659 HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
660 bfd_elf_generic_reloc),
661
662 HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
663 bfd_elf_generic_reloc),
664
665 /* Computes the load module index of the load module that contains the
666 definition of its TLS sym. */
667 HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
668 ppc64_elf_unhandled_reloc),
669
670 /* Computes a dtv-relative displacement, the difference between the value
671 of sym+add and the base address of the thread-local storage block that
672 contains the definition of sym, minus 0x8000. */
673 HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
674 ppc64_elf_unhandled_reloc),
675
676 /* A 16 bit dtprel reloc. */
677 HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
678 ppc64_elf_unhandled_reloc),
679
680 /* Like DTPREL16, but no overflow. */
681 HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
682 ppc64_elf_unhandled_reloc),
683
684 /* Like DTPREL16_LO, but next higher group of 16 bits. */
685 HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
686 ppc64_elf_unhandled_reloc),
687
688 /* Like DTPREL16_HI, but adjust for low 16 bits. */
689 HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
690 ppc64_elf_unhandled_reloc),
691
692 /* Like DTPREL16_HI, but next higher group of 16 bits. */
693 HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
694 ppc64_elf_unhandled_reloc),
695
696 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
697 HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
698 ppc64_elf_unhandled_reloc),
699
700 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
701 HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
702 ppc64_elf_unhandled_reloc),
703
704 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
705 HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
706 ppc64_elf_unhandled_reloc),
707
708 /* Like DTPREL16, but for insns with a DS field. */
709 HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
710 ppc64_elf_unhandled_reloc),
711
712 /* Like DTPREL16_DS, but no overflow. */
713 HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
714 ppc64_elf_unhandled_reloc),
715
716 /* Computes a tp-relative displacement, the difference between the value of
717 sym+add and the value of the thread pointer (r13). */
718 HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
719 ppc64_elf_unhandled_reloc),
720
721 /* A 16 bit tprel reloc. */
722 HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
723 ppc64_elf_unhandled_reloc),
724
725 /* Like TPREL16, but no overflow. */
726 HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
727 ppc64_elf_unhandled_reloc),
728
729 /* Like TPREL16_LO, but next higher group of 16 bits. */
730 HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
731 ppc64_elf_unhandled_reloc),
732
733 /* Like TPREL16_HI, but adjust for low 16 bits. */
734 HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
735 ppc64_elf_unhandled_reloc),
736
737 /* Like TPREL16_HI, but next higher group of 16 bits. */
738 HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
739 ppc64_elf_unhandled_reloc),
740
741 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
742 HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
743 ppc64_elf_unhandled_reloc),
744
745 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
746 HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
747 ppc64_elf_unhandled_reloc),
748
749 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
750 HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
751 ppc64_elf_unhandled_reloc),
752
753 /* Like TPREL16, but for insns with a DS field. */
754 HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
755 ppc64_elf_unhandled_reloc),
756
757 /* Like TPREL16_DS, but no overflow. */
758 HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
759 ppc64_elf_unhandled_reloc),
760
761 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
762 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
763 to the first entry relative to the TOC base (r2). */
764 HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
765 ppc64_elf_unhandled_reloc),
766
767 /* Like GOT_TLSGD16, but no overflow. */
768 HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
769 ppc64_elf_unhandled_reloc),
770
771 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
772 HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
773 ppc64_elf_unhandled_reloc),
774
775 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
776 HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
777 ppc64_elf_unhandled_reloc),
778
779 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
780 with values (sym+add)@dtpmod and zero, and computes the offset to the
781 first entry relative to the TOC base (r2). */
782 HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
783 ppc64_elf_unhandled_reloc),
784
785 /* Like GOT_TLSLD16, but no overflow. */
786 HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
787 ppc64_elf_unhandled_reloc),
788
789 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
790 HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
791 ppc64_elf_unhandled_reloc),
792
793 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
794 HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
795 ppc64_elf_unhandled_reloc),
796
797 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
798 the offset to the entry relative to the TOC base (r2). */
799 HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
800 ppc64_elf_unhandled_reloc),
801
802 /* Like GOT_DTPREL16_DS, but no overflow. */
803 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
804 ppc64_elf_unhandled_reloc),
805
806 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
807 HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
808 ppc64_elf_unhandled_reloc),
809
810 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
811 HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
812 ppc64_elf_unhandled_reloc),
813
814 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
815 offset to the entry relative to the TOC base (r2). */
816 HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
817 ppc64_elf_unhandled_reloc),
818
819 /* Like GOT_TPREL16_DS, but no overflow. */
820 HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
821 ppc64_elf_unhandled_reloc),
822
823 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
824 HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
825 ppc64_elf_unhandled_reloc),
826
827 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
828 HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
829 ppc64_elf_unhandled_reloc),
830
831 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
832 ppc64_elf_unhandled_reloc),
833
834 HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
835 bfd_elf_generic_reloc),
836
837 /* A 16 bit relative relocation. */
838 HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
839 bfd_elf_generic_reloc),
840
841 /* A 16 bit relative relocation without overflow. */
842 HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
843 bfd_elf_generic_reloc),
844
845 /* The high order 16 bits of a relative address. */
846 HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
847 bfd_elf_generic_reloc),
848
849 /* The high order 16 bits of a relative address, plus 1 if the contents of
850 the low 16 bits, treated as a signed number, is negative. */
851 HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
852 ppc64_elf_ha_reloc),
853
854 HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
855 bfd_elf_generic_reloc),
856
857 HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
858 ppc64_elf_ha_reloc),
859
860 HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
861 bfd_elf_generic_reloc),
862
863 HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
864 ppc64_elf_ha_reloc),
865
866 HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
867 bfd_elf_generic_reloc),
868
869 HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
870 ppc64_elf_ha_reloc),
871
872 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
873 HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
874 ppc64_elf_ha_reloc),
875
876 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
877 HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
878 ppc64_elf_ha_reloc),
879
880 /* Like R_PPC64_ADDR16_HI, but no overflow. */
881 HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
882 bfd_elf_generic_reloc),
883
884 /* Like R_PPC64_ADDR16_HA, but no overflow. */
885 HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
886 ppc64_elf_ha_reloc),
887
888 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
889 HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
890 ppc64_elf_unhandled_reloc),
891
892 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
893 HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
894 ppc64_elf_unhandled_reloc),
895
896 /* Like R_PPC64_TPREL16_HI, but no overflow. */
897 HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
898 ppc64_elf_unhandled_reloc),
899
900 /* Like R_PPC64_TPREL16_HA, but no overflow. */
901 HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
902 ppc64_elf_unhandled_reloc),
903
904 /* Marker reloc on ELFv2 large-model function entry. */
905 HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
906 bfd_elf_generic_reloc),
907
908 /* Like ADDR64, but use local entry point of function. */
909 HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
910 bfd_elf_generic_reloc),
911
912 HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
913 bfd_elf_generic_reloc),
914
915 HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
916 bfd_elf_generic_reloc),
917
918 HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
919 bfd_elf_generic_reloc),
920
921 HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
922 ppc64_elf_prefix_reloc),
923
924 HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
925 ppc64_elf_prefix_reloc),
926
927 HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
928 ppc64_elf_prefix_reloc),
929
930 HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
931 ppc64_elf_prefix_reloc),
932
933 HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
934 ppc64_elf_prefix_reloc),
935
936 HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
937 ppc64_elf_unhandled_reloc),
938
939 HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
940 ppc64_elf_unhandled_reloc),
941
942 HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
943 ppc64_elf_unhandled_reloc),
944
945 HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
946 ppc64_elf_unhandled_reloc),
947
948 HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
949 ppc64_elf_unhandled_reloc),
950
951 HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
952 ppc64_elf_unhandled_reloc),
953
954 HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
955 ppc64_elf_unhandled_reloc),
956
957 HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
958 ppc64_elf_unhandled_reloc),
959
960 HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
961 ppc64_elf_unhandled_reloc),
962
963 HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
964 bfd_elf_generic_reloc),
965
966 HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
967 ppc64_elf_ha_reloc),
968
969 HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
970 bfd_elf_generic_reloc),
971
972 HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
973 ppc64_elf_ha_reloc),
974
975 HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
976 bfd_elf_generic_reloc),
977
978 HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
979 ppc64_elf_ha_reloc),
980
981 HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
982 bfd_elf_generic_reloc),
983
984 HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
985 ppc64_elf_ha_reloc),
986
987 HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
988 ppc64_elf_prefix_reloc),
989
990 HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
991 ppc64_elf_prefix_reloc),
992
993 /* GNU extension to record C++ vtable hierarchy. */
994 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
995 NULL),
996
997 /* GNU extension to record C++ vtable member usage. */
998 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
999 NULL),
1000 };
1001
1002
1003 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1005 be done. */
1006
1007 static void
1008 ppc_howto_init (void)
1009 {
1010 unsigned int i, type;
1011
1012 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1013 {
1014 type = ppc64_elf_howto_raw[i].type;
1015 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1016 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1017 }
1018 }
1019
1020 static reloc_howto_type *
1021 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1022 {
1023 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1024
1025 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1026 /* Initialize howto table if needed. */
1027 ppc_howto_init ();
1028
1029 switch (code)
1030 {
1031 default:
1032 /* xgettext:c-format */
1033 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1034 (int) code);
1035 bfd_set_error (bfd_error_bad_value);
1036 return NULL;
1037
1038 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1039 break;
1040 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1041 break;
1042 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1043 break;
1044 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1045 break;
1046 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1047 break;
1048 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1049 break;
1050 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1051 break;
1052 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1053 break;
1054 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1055 break;
1056 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1057 break;
1058 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1059 break;
1060 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1061 break;
1062 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1063 break;
1064 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1065 break;
1066 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1067 break;
1068 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1069 break;
1070 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1071 break;
1072 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1073 break;
1074 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1075 break;
1076 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1077 break;
1078 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1079 break;
1080 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1081 break;
1082 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1083 break;
1084 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1085 break;
1086 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1087 break;
1088 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1089 break;
1090 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1091 break;
1092 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1093 break;
1094 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1095 break;
1096 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1097 break;
1098 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1099 break;
1100 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1101 break;
1102 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1103 break;
1104 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1105 break;
1106 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1107 break;
1108 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1109 break;
1110 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1111 break;
1112 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1113 break;
1114 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1115 break;
1116 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1117 break;
1118 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1119 break;
1120 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1121 break;
1122 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1123 break;
1124 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1125 break;
1126 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1127 break;
1128 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1129 break;
1130 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1131 break;
1132 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1133 break;
1134 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1135 break;
1136 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1137 break;
1138 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1139 break;
1140 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1141 break;
1142 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1143 break;
1144 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1145 break;
1146 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1147 break;
1148 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1149 break;
1150 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1151 break;
1152 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1153 break;
1154 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1155 break;
1156 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1157 break;
1158 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1159 break;
1160 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1161 break;
1162 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1163 break;
1164 case BFD_RELOC_PPC64_TLS_PCREL:
1165 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1166 break;
1167 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1168 break;
1169 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1170 break;
1171 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1172 break;
1173 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1174 break;
1175 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1176 break;
1177 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1178 break;
1179 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1180 break;
1181 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1182 break;
1183 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1184 break;
1185 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1186 break;
1187 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1188 break;
1189 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1190 break;
1191 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1192 break;
1193 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1194 break;
1195 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1196 break;
1197 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1198 break;
1199 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1218 break;
1219 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1220 break;
1221 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1222 break;
1223 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1226 break;
1227 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1228 break;
1229 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1230 break;
1231 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1238 break;
1239 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1240 break;
1241 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1242 break;
1243 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1250 break;
1251 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1252 break;
1253 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1254 break;
1255 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1256 break;
1257 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1258 break;
1259 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1260 break;
1261 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1262 break;
1263 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1270 break;
1271 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1272 break;
1273 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1274 break;
1275 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1276 break;
1277 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1278 break;
1279 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1280 break;
1281 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1282 break;
1283 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1284 break;
1285 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1286 break;
1287 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1288 break;
1289 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1290 break;
1291 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1292 break;
1293 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1294 break;
1295 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1296 break;
1297 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1298 break;
1299 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1300 break;
1301 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1302 break;
1303 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1304 break;
1305 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1306 break;
1307 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1308 break;
1309 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1312 break;
1313 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1314 break;
1315 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1316 break;
1317 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1320 break;
1321 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1322 break;
1323 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1324 break;
1325 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1326 break;
1327 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1328 break;
1329 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1330 break;
1331 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1332 break;
1333 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1334 break;
1335 }
1336
1337 return ppc64_elf_howto_table[r];
1338 };
1339
1340 static reloc_howto_type *
1341 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1342 {
1343 unsigned int i;
1344 static char *compat_map[][2] = {
1345 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1346 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1347 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1348 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1349 };
1350
1351 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1352 if (ppc64_elf_howto_raw[i].name != NULL
1353 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1354 return &ppc64_elf_howto_raw[i];
1355
1356 /* Handle old names of relocations in case they were used by
1357 .reloc directives.
1358 FIXME: Remove this soon. Mapping the reloc names is very likely
1359 completely unnecessary. */
1360 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1361 if (strcasecmp (compat_map[i][0], r_name) == 0)
1362 {
1363 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1364 compat_map[i][1], compat_map[i][0]);
1365 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1366 }
1367
1368 return NULL;
1369 }
1370
1371 /* Set the howto pointer for a PowerPC ELF reloc. */
1372
1373 static bool
1374 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1375 Elf_Internal_Rela *dst)
1376 {
1377 unsigned int type;
1378
1379 /* Initialize howto table if needed. */
1380 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1381 ppc_howto_init ();
1382
1383 type = ELF64_R_TYPE (dst->r_info);
1384 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1385 {
1386 /* xgettext:c-format */
1387 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1388 abfd, type);
1389 bfd_set_error (bfd_error_bad_value);
1390 return false;
1391 }
1392 cache_ptr->howto = ppc64_elf_howto_table[type];
1393 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1394 {
1395 /* xgettext:c-format */
1396 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1397 abfd, type);
1398 bfd_set_error (bfd_error_bad_value);
1399 return false;
1400 }
1401
1402 return true;
1403 }
1404
1405 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1406
1407 static bfd_reloc_status_type
1408 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1409 void *data, asection *input_section,
1410 bfd *output_bfd, char **error_message)
1411 {
1412 enum elf_ppc64_reloc_type r_type;
1413 long insn;
1414 bfd_size_type octets;
1415 bfd_vma value;
1416
1417 /* If this is a relocatable link (output_bfd test tells us), just
1418 call the generic function. Any adjustment will be done at final
1419 link time. */
1420 if (output_bfd != NULL)
1421 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1422 input_section, output_bfd, error_message);
1423
1424 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1425 We won't actually be using the low bits, so trashing them
1426 doesn't matter. */
1427 r_type = reloc_entry->howto->type;
1428 if (r_type == R_PPC64_ADDR16_HIGHERA34
1429 || r_type == R_PPC64_ADDR16_HIGHESTA34
1430 || r_type == R_PPC64_REL16_HIGHERA34
1431 || r_type == R_PPC64_REL16_HIGHESTA34)
1432 reloc_entry->addend += 1ULL << 33;
1433 else
1434 reloc_entry->addend += 1U << 15;
1435 if (r_type != R_PPC64_REL16DX_HA)
1436 return bfd_reloc_continue;
1437
1438 value = 0;
1439 if (!bfd_is_com_section (symbol->section))
1440 value = symbol->value;
1441 value += (reloc_entry->addend
1442 + symbol->section->output_offset
1443 + symbol->section->output_section->vma);
1444 value -= (reloc_entry->address
1445 + input_section->output_offset
1446 + input_section->output_section->vma);
1447 value = (bfd_signed_vma) value >> 16;
1448
1449 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1450 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1451 input_section, octets))
1452 return bfd_reloc_outofrange;
1453
1454 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1455 insn &= ~0x1fffc1;
1456 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1457 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1458 if (value + 0x8000 > 0xffff)
1459 return bfd_reloc_overflow;
1460 return bfd_reloc_ok;
1461 }
1462
1463 static bfd_reloc_status_type
1464 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1465 void *data, asection *input_section,
1466 bfd *output_bfd, char **error_message)
1467 {
1468 if (output_bfd != NULL)
1469 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1470 input_section, output_bfd, error_message);
1471
1472 if (symbol->section->owner == NULL
1473 || !is_ppc64_elf (symbol->section->owner))
1474 return bfd_reloc_continue;
1475
1476 if (strcmp (symbol->section->name, ".opd") == 0
1477 && (symbol->section->owner->flags & DYNAMIC) == 0)
1478 {
1479 bfd_vma dest = opd_entry_value (symbol->section,
1480 symbol->value + reloc_entry->addend,
1481 NULL, NULL, false);
1482 if (dest != (bfd_vma) -1)
1483 reloc_entry->addend = dest - (symbol->value
1484 + symbol->section->output_section->vma
1485 + symbol->section->output_offset);
1486 }
1487 else
1488 {
1489 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1490
1491 if (symbol->section->owner != abfd
1492 && abiversion (symbol->section->owner) >= 2)
1493 {
1494 unsigned int i;
1495
1496 for (i = 0; i < symbol->section->owner->symcount; ++i)
1497 {
1498 asymbol *symdef = symbol->section->owner->outsymbols[i];
1499
1500 if (strcmp (symdef->name, symbol->name) == 0)
1501 {
1502 elfsym = (elf_symbol_type *) symdef;
1503 break;
1504 }
1505 }
1506 }
1507 reloc_entry->addend
1508 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1509 }
1510 return bfd_reloc_continue;
1511 }
1512
1513 static bfd_reloc_status_type
1514 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1515 void *data, asection *input_section,
1516 bfd *output_bfd, char **error_message)
1517 {
1518 long insn;
1519 enum elf_ppc64_reloc_type r_type;
1520 bfd_size_type octets;
1521 /* Assume 'at' branch hints. */
1522 bool is_isa_v2 = true;
1523
1524 /* If this is a relocatable link (output_bfd test tells us), just
1525 call the generic function. Any adjustment will be done at final
1526 link time. */
1527 if (output_bfd != NULL)
1528 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1529 input_section, output_bfd, error_message);
1530
1531 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1532 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1533 input_section, octets))
1534 return bfd_reloc_outofrange;
1535
1536 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1537 insn &= ~(0x01 << 21);
1538 r_type = reloc_entry->howto->type;
1539 if (r_type == R_PPC64_ADDR14_BRTAKEN
1540 || r_type == R_PPC64_REL14_BRTAKEN)
1541 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1542
1543 if (is_isa_v2)
1544 {
1545 /* Set 'a' bit. This is 0b00010 in BO field for branch
1546 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1547 for branch on CTR insns (BO == 1a00t or 1a01t). */
1548 if ((insn & (0x14 << 21)) == (0x04 << 21))
1549 insn |= 0x02 << 21;
1550 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1551 insn |= 0x08 << 21;
1552 else
1553 goto out;
1554 }
1555 else
1556 {
1557 bfd_vma target = 0;
1558 bfd_vma from;
1559
1560 if (!bfd_is_com_section (symbol->section))
1561 target = symbol->value;
1562 target += symbol->section->output_section->vma;
1563 target += symbol->section->output_offset;
1564 target += reloc_entry->addend;
1565
1566 from = (reloc_entry->address
1567 + input_section->output_offset
1568 + input_section->output_section->vma);
1569
1570 /* Invert 'y' bit if not the default. */
1571 if ((bfd_signed_vma) (target - from) < 0)
1572 insn ^= 0x01 << 21;
1573 }
1574 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1575 out:
1576 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1577 input_section, output_bfd, error_message);
1578 }
1579
1580 static bfd_reloc_status_type
1581 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1582 void *data, asection *input_section,
1583 bfd *output_bfd, char **error_message)
1584 {
1585 /* If this is a relocatable link (output_bfd test tells us), just
1586 call the generic function. Any adjustment will be done at final
1587 link time. */
1588 if (output_bfd != NULL)
1589 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1590 input_section, output_bfd, error_message);
1591
1592 /* Subtract the symbol section base address. */
1593 reloc_entry->addend -= symbol->section->output_section->vma;
1594 return bfd_reloc_continue;
1595 }
1596
1597 static bfd_reloc_status_type
1598 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1599 void *data, asection *input_section,
1600 bfd *output_bfd, char **error_message)
1601 {
1602 /* If this is a relocatable link (output_bfd test tells us), just
1603 call the generic function. Any adjustment will be done at final
1604 link time. */
1605 if (output_bfd != NULL)
1606 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1607 input_section, output_bfd, error_message);
1608
1609 /* Subtract the symbol section base address. */
1610 reloc_entry->addend -= symbol->section->output_section->vma;
1611
1612 /* Adjust the addend for sign extension of the low 16 bits. */
1613 reloc_entry->addend += 0x8000;
1614 return bfd_reloc_continue;
1615 }
1616
1617 static bfd_reloc_status_type
1618 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1619 void *data, asection *input_section,
1620 bfd *output_bfd, char **error_message)
1621 {
1622 bfd_vma TOCstart;
1623
1624 /* If this is a relocatable link (output_bfd test tells us), just
1625 call the generic function. Any adjustment will be done at final
1626 link time. */
1627 if (output_bfd != NULL)
1628 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1629 input_section, output_bfd, error_message);
1630
1631 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1632 if (TOCstart == 0)
1633 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1634
1635 /* Subtract the TOC base address. */
1636 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1637 return bfd_reloc_continue;
1638 }
1639
1640 static bfd_reloc_status_type
1641 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1642 void *data, asection *input_section,
1643 bfd *output_bfd, char **error_message)
1644 {
1645 bfd_vma TOCstart;
1646
1647 /* If this is a relocatable link (output_bfd test tells us), just
1648 call the generic function. Any adjustment will be done at final
1649 link time. */
1650 if (output_bfd != NULL)
1651 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1652 input_section, output_bfd, error_message);
1653
1654 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1655 if (TOCstart == 0)
1656 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1657
1658 /* Subtract the TOC base address. */
1659 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1660
1661 /* Adjust the addend for sign extension of the low 16 bits. */
1662 reloc_entry->addend += 0x8000;
1663 return bfd_reloc_continue;
1664 }
1665
1666 static bfd_reloc_status_type
1667 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1668 void *data, asection *input_section,
1669 bfd *output_bfd, char **error_message)
1670 {
1671 bfd_vma TOCstart;
1672 bfd_size_type octets;
1673
1674 /* If this is a relocatable link (output_bfd test tells us), just
1675 call the generic function. Any adjustment will be done at final
1676 link time. */
1677 if (output_bfd != NULL)
1678 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1679 input_section, output_bfd, error_message);
1680
1681 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1682 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1683 input_section, octets))
1684 return bfd_reloc_outofrange;
1685
1686 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1687 if (TOCstart == 0)
1688 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1689
1690 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1691 return bfd_reloc_ok;
1692 }
1693
1694 static bfd_reloc_status_type
1695 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1696 void *data, asection *input_section,
1697 bfd *output_bfd, char **error_message)
1698 {
1699 uint64_t insn;
1700 bfd_vma targ;
1701 bfd_size_type octets;
1702
1703 if (output_bfd != NULL)
1704 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1705 input_section, output_bfd, error_message);
1706
1707 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1708 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1709 input_section, octets))
1710 return bfd_reloc_outofrange;
1711
1712 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1713 insn <<= 32;
1714 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1715
1716 targ = (symbol->section->output_section->vma
1717 + symbol->section->output_offset
1718 + reloc_entry->addend);
1719 if (!bfd_is_com_section (symbol->section))
1720 targ += symbol->value;
1721 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1722 targ += 1ULL << 33;
1723 if (reloc_entry->howto->pc_relative)
1724 {
1725 bfd_vma from = (reloc_entry->address
1726 + input_section->output_offset
1727 + input_section->output_section->vma);
1728 targ -=from;
1729 }
1730 targ >>= reloc_entry->howto->rightshift;
1731 insn &= ~reloc_entry->howto->dst_mask;
1732 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1733 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1734 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1735 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1736 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1737 >= 1ULL << reloc_entry->howto->bitsize))
1738 return bfd_reloc_overflow;
1739 return bfd_reloc_ok;
1740 }
1741
1742 static bfd_reloc_status_type
1743 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1744 void *data, asection *input_section,
1745 bfd *output_bfd, char **error_message)
1746 {
1747 /* If this is a relocatable link (output_bfd test tells us), just
1748 call the generic function. Any adjustment will be done at final
1749 link time. */
1750 if (output_bfd != NULL)
1751 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1752 input_section, output_bfd, error_message);
1753
1754 if (error_message != NULL)
1755 *error_message = bfd_asprintf (_("generic linker can't handle %s"),
1756 reloc_entry->howto->name);
1757 return bfd_reloc_dangerous;
1758 }
1759
1760 /* Track GOT entries needed for a given symbol. We might need more
1761 than one got entry per symbol. */
1762 struct got_entry
1763 {
1764 struct got_entry *next;
1765
1766 /* The symbol addend that we'll be placing in the GOT. */
1767 bfd_vma addend;
1768
1769 /* Unlike other ELF targets, we use separate GOT entries for the same
1770 symbol referenced from different input files. This is to support
1771 automatic multiple TOC/GOT sections, where the TOC base can vary
1772 from one input file to another. After partitioning into TOC groups
1773 we merge entries within the group.
1774
1775 Point to the BFD owning this GOT entry. */
1776 bfd *owner;
1777
1778 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1779 TLS_TPREL or TLS_DTPREL for tls entries. */
1780 unsigned char tls_type;
1781
1782 /* Non-zero if got.ent points to real entry. */
1783 unsigned char is_indirect;
1784
1785 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1786 union
1787 {
1788 bfd_signed_vma refcount;
1789 bfd_vma offset;
1790 struct got_entry *ent;
1791 } got;
1792 };
1793
1794 /* The same for PLT. */
1795 struct plt_entry
1796 {
1797 struct plt_entry *next;
1798
1799 bfd_vma addend;
1800
1801 union
1802 {
1803 bfd_signed_vma refcount;
1804 bfd_vma offset;
1805 } plt;
1806 };
1807
1808 struct ppc64_elf_obj_tdata
1809 {
1810 struct elf_obj_tdata elf;
1811
1812 /* Shortcuts to dynamic linker sections. */
1813 asection *got;
1814 asection *relgot;
1815
1816 /* Used during garbage collection. We attach global symbols defined
1817 on removed .opd entries to this section so that the sym is removed. */
1818 asection *deleted_section;
1819
1820 /* TLS local dynamic got entry handling. Support for multiple GOT
1821 sections means we potentially need one of these for each input bfd. */
1822 struct got_entry tlsld_got;
1823
1824 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1825 the reloc to be in the range -32768 to 32767. */
1826 unsigned int has_small_toc_reloc : 1;
1827
1828 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1829 instruction not one we handle. */
1830 unsigned int unexpected_toc_insn : 1;
1831
1832 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1833 this file. */
1834 unsigned int has_optrel : 1;
1835 };
1836
1837 #define ppc64_elf_tdata(bfd) \
1838 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1839
1840 #define ppc64_tlsld_got(bfd) \
1841 (&ppc64_elf_tdata (bfd)->tlsld_got)
1842
1843 /* Override the generic function because we store some extras. */
1844
1845 static bool
1846 ppc64_elf_mkobject (bfd *abfd)
1847 {
1848 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata));
1849 }
1850
1851 /* Fix bad default arch selected for a 64 bit input bfd when the
1852 default is 32 bit. Also select arch based on apuinfo. */
1853
1854 static bool
1855 ppc64_elf_object_p (bfd *abfd)
1856 {
1857 if (!abfd->arch_info->the_default)
1858 return true;
1859
1860 if (abfd->arch_info->bits_per_word == 32)
1861 {
1862 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1863
1864 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1865 {
1866 /* Relies on arch after 32 bit default being 64 bit default. */
1867 abfd->arch_info = abfd->arch_info->next;
1868 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1869 }
1870 }
1871 return _bfd_elf_ppc_set_arch (abfd);
1872 }
1873
1874 /* Support for core dump NOTE sections. */
1875
1876 static bool
1877 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1878 {
1879 size_t offset, size;
1880
1881 if (note->descsz != 504)
1882 return false;
1883
1884 /* pr_cursig */
1885 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1886
1887 /* pr_pid */
1888 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1889
1890 /* pr_reg */
1891 offset = 112;
1892 size = 384;
1893
1894 /* Make a ".reg/999" section. */
1895 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1896 size, note->descpos + offset);
1897 }
1898
1899 static bool
1900 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1901 {
1902 if (note->descsz != 136)
1903 return false;
1904
1905 elf_tdata (abfd)->core->pid
1906 = bfd_get_32 (abfd, note->descdata + 24);
1907 elf_tdata (abfd)->core->program
1908 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1909 elf_tdata (abfd)->core->command
1910 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1911
1912 return true;
1913 }
1914
1915 static char *
1916 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1917 ...)
1918 {
1919 switch (note_type)
1920 {
1921 default:
1922 return NULL;
1923
1924 case NT_PRPSINFO:
1925 {
1926 char data[136] ATTRIBUTE_NONSTRING;
1927 va_list ap;
1928
1929 va_start (ap, note_type);
1930 memset (data, 0, sizeof (data));
1931 strncpy (data + 40, va_arg (ap, const char *), 16);
1932 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1933 DIAGNOSTIC_PUSH;
1934 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1935 -Wstringop-truncation:
1936 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1937 */
1938 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1939 #endif
1940 strncpy (data + 56, va_arg (ap, const char *), 80);
1941 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1942 DIAGNOSTIC_POP;
1943 #endif
1944 va_end (ap);
1945 return elfcore_write_note (abfd, buf, bufsiz,
1946 "CORE", note_type, data, sizeof (data));
1947 }
1948
1949 case NT_PRSTATUS:
1950 {
1951 char data[504];
1952 va_list ap;
1953 long pid;
1954 int cursig;
1955 const void *greg;
1956
1957 va_start (ap, note_type);
1958 memset (data, 0, 112);
1959 pid = va_arg (ap, long);
1960 bfd_put_32 (abfd, pid, data + 32);
1961 cursig = va_arg (ap, int);
1962 bfd_put_16 (abfd, cursig, data + 12);
1963 greg = va_arg (ap, const void *);
1964 memcpy (data + 112, greg, 384);
1965 memset (data + 496, 0, 8);
1966 va_end (ap);
1967 return elfcore_write_note (abfd, buf, bufsiz,
1968 "CORE", note_type, data, sizeof (data));
1969 }
1970 }
1971 }
1972
1973 /* Add extra PPC sections. */
1974
1975 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1976 {
1977 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1978 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1979 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1980 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1981 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1982 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1983 { NULL, 0, 0, 0, 0 }
1984 };
1985
1986 enum _ppc64_sec_type {
1987 sec_normal = 0,
1988 sec_opd = 1,
1989 sec_toc = 2,
1990 sec_stub = 3
1991 };
1992
1993 struct _ppc64_elf_section_data
1994 {
1995 struct bfd_elf_section_data elf;
1996
1997 union
1998 {
1999 /* An array with one entry for each opd function descriptor,
2000 and some spares since opd entries may be either 16 or 24 bytes. */
2001 #define OPD_NDX(OFF) ((OFF) >> 4)
2002 struct _opd_sec_data
2003 {
2004 /* Points to the function code section for local opd entries. */
2005 asection **func_sec;
2006
2007 /* After editing .opd, adjust references to opd local syms. */
2008 long *adjust;
2009
2010 union
2011 {
2012 /* A copy of relocs before they are modified for --emit-relocs. */
2013 Elf_Internal_Rela *relocs;
2014
2015 /* Section contents. */
2016 bfd_byte *contents;
2017 } u;
2018 } opd;
2019
2020 /* An array for toc sections, indexed by offset/8. */
2021 struct _toc_sec_data
2022 {
2023 /* Specifies the relocation symbol index used at a given toc offset. */
2024 unsigned *symndx;
2025
2026 /* And the relocation addend. */
2027 bfd_vma *add;
2028 } toc;
2029
2030 /* Stub debugging. */
2031 struct ppc_stub_hash_entry *last_ent;
2032 } u;
2033
2034 enum _ppc64_sec_type sec_type:2;
2035
2036 /* Flag set when small branches are detected. Used to
2037 select suitable defaults for the stub group size. */
2038 unsigned int has_14bit_branch:1;
2039
2040 /* Flag set when PLTCALL relocs are detected. */
2041 unsigned int has_pltcall:1;
2042
2043 /* Flag set when section has PLT/GOT/TOC relocations that can be
2044 optimised. */
2045 unsigned int has_optrel:1;
2046 };
2047
2048 #define ppc64_elf_section_data(sec) \
2049 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2050
2051 static bool
2052 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2053 {
2054 struct _ppc64_elf_section_data *sdata;
2055
2056 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2057 if (sdata == NULL)
2058 return false;
2059 sec->used_by_bfd = sdata;
2060
2061 return _bfd_elf_new_section_hook (abfd, sec);
2062 }
2063
2064 static bool
2065 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2066 {
2067 const char *name = hdr->bfd_section->name;
2068
2069 if (startswith (name, ".sbss")
2070 || startswith (name, ".sdata"))
2071 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2072
2073 return true;
2074 }
2075
2076 static struct _opd_sec_data *
2077 get_opd_info (asection * sec)
2078 {
2079 if (sec != NULL
2080 && ppc64_elf_section_data (sec) != NULL
2081 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2082 return &ppc64_elf_section_data (sec)->u.opd;
2083 return NULL;
2084 }
2085
2086 /* Parameters for the qsort hook. */
2088 static bool synthetic_relocatable;
2089 static const asection *synthetic_opd;
2090
2091 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2092
2093 static int
2094 compare_symbols (const void *ap, const void *bp)
2095 {
2096 const asymbol *a = *(const asymbol **) ap;
2097 const asymbol *b = *(const asymbol **) bp;
2098
2099 /* Section symbols first. */
2100 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2101 return -1;
2102 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2103 return 1;
2104
2105 /* then .opd symbols. */
2106 if (synthetic_opd != NULL)
2107 {
2108 if (strcmp (a->section->name, ".opd") == 0
2109 && strcmp (b->section->name, ".opd") != 0)
2110 return -1;
2111 if (strcmp (a->section->name, ".opd") != 0
2112 && strcmp (b->section->name, ".opd") == 0)
2113 return 1;
2114 }
2115
2116 /* then other code symbols. */
2117 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2118 == (SEC_CODE | SEC_ALLOC))
2119 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2120 != (SEC_CODE | SEC_ALLOC)))
2121 return -1;
2122
2123 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2124 != (SEC_CODE | SEC_ALLOC))
2125 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2126 == (SEC_CODE | SEC_ALLOC)))
2127 return 1;
2128
2129 if (synthetic_relocatable)
2130 {
2131 if (a->section->id < b->section->id)
2132 return -1;
2133
2134 if (a->section->id > b->section->id)
2135 return 1;
2136 }
2137
2138 if (a->value + a->section->vma < b->value + b->section->vma)
2139 return -1;
2140
2141 if (a->value + a->section->vma > b->value + b->section->vma)
2142 return 1;
2143
2144 /* For syms with the same value, prefer strong dynamic global function
2145 syms over other syms. */
2146 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2147 return -1;
2148
2149 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2150 return 1;
2151
2152 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2153 return -1;
2154
2155 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2156 return 1;
2157
2158 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2159 return -1;
2160
2161 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2162 return 1;
2163
2164 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2165 return -1;
2166
2167 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2168 return 1;
2169
2170 /* Finally, sort on where the symbol is in memory. The symbols will
2171 be in at most two malloc'd blocks, one for static syms, one for
2172 dynamic syms, and we distinguish the two blocks above by testing
2173 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2174 originally in the same order as the symbols (and we're not
2175 sorting the symbols themselves), this ensures a stable sort. */
2176 if (a < b)
2177 return -1;
2178 if (a > b)
2179 return 1;
2180 return 0;
2181 }
2182
2183 /* Search SYMS for a symbol of the given VALUE. */
2184
2185 static asymbol *
2186 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2187 bfd_vma value)
2188 {
2189 size_t mid;
2190
2191 if (id == (unsigned) -1)
2192 {
2193 while (lo < hi)
2194 {
2195 mid = (lo + hi) >> 1;
2196 if (syms[mid]->value + syms[mid]->section->vma < value)
2197 lo = mid + 1;
2198 else if (syms[mid]->value + syms[mid]->section->vma > value)
2199 hi = mid;
2200 else
2201 return syms[mid];
2202 }
2203 }
2204 else
2205 {
2206 while (lo < hi)
2207 {
2208 mid = (lo + hi) >> 1;
2209 if (syms[mid]->section->id < id)
2210 lo = mid + 1;
2211 else if (syms[mid]->section->id > id)
2212 hi = mid;
2213 else if (syms[mid]->value < value)
2214 lo = mid + 1;
2215 else if (syms[mid]->value > value)
2216 hi = mid;
2217 else
2218 return syms[mid];
2219 }
2220 }
2221 return NULL;
2222 }
2223
2224 static bool
2225 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2226 {
2227 bfd_vma vma = *(bfd_vma *) ptr;
2228 return ((section->flags & SEC_ALLOC) != 0
2229 && section->vma <= vma
2230 && vma < section->vma + section->size);
2231 }
2232
2233 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2234 entry syms. Also generate @plt symbols for the glink branch table.
2235 Returns count of synthetic symbols in RET or -1 on error. */
2236
2237 static long
2238 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2239 long static_count, asymbol **static_syms,
2240 long dyn_count, asymbol **dyn_syms,
2241 asymbol **ret)
2242 {
2243 asymbol *s;
2244 size_t i, j, count;
2245 char *names;
2246 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2247 asection *opd = NULL;
2248 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2249 asymbol **syms;
2250 int abi = abiversion (abfd);
2251
2252 *ret = NULL;
2253
2254 if (abi < 2)
2255 {
2256 opd = bfd_get_section_by_name (abfd, ".opd");
2257 if (opd == NULL && abi == 1)
2258 return 0;
2259 }
2260
2261 syms = NULL;
2262 codesecsym = 0;
2263 codesecsymend = 0;
2264 secsymend = 0;
2265 opdsymend = 0;
2266 symcount = 0;
2267 if (opd != NULL)
2268 {
2269 symcount = static_count;
2270 if (!relocatable)
2271 symcount += dyn_count;
2272 if (symcount == 0)
2273 return 0;
2274
2275 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2276 if (syms == NULL)
2277 return -1;
2278
2279 if (!relocatable && static_count != 0 && dyn_count != 0)
2280 {
2281 /* Use both symbol tables. */
2282 memcpy (syms, static_syms, static_count * sizeof (*syms));
2283 memcpy (syms + static_count, dyn_syms,
2284 (dyn_count + 1) * sizeof (*syms));
2285 }
2286 else if (!relocatable && static_count == 0)
2287 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2288 else
2289 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2290
2291 /* Trim uninteresting symbols. Interesting symbols are section,
2292 function, and notype symbols. */
2293 for (i = 0, j = 0; i < symcount; ++i)
2294 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2295 | BSF_RELC | BSF_SRELC)) == 0)
2296 syms[j++] = syms[i];
2297 symcount = j;
2298
2299 synthetic_relocatable = relocatable;
2300 synthetic_opd = opd;
2301 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2302
2303 if (!relocatable && symcount > 1)
2304 {
2305 /* Trim duplicate syms, since we may have merged the normal
2306 and dynamic symbols. Actually, we only care about syms
2307 that have different values, so trim any with the same
2308 value. Don't consider ifunc and ifunc resolver symbols
2309 duplicates however, because GDB wants to know whether a
2310 text symbol is an ifunc resolver. */
2311 for (i = 1, j = 1; i < symcount; ++i)
2312 {
2313 const asymbol *s0 = syms[i - 1];
2314 const asymbol *s1 = syms[i];
2315
2316 if ((s0->value + s0->section->vma
2317 != s1->value + s1->section->vma)
2318 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2319 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2320 syms[j++] = syms[i];
2321 }
2322 symcount = j;
2323 }
2324
2325 i = 0;
2326 /* Note that here and in compare_symbols we can't compare opd and
2327 sym->section directly. With separate debug info files, the
2328 symbols will be extracted from the debug file while abfd passed
2329 to this function is the real binary. */
2330 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2331 && strcmp (syms[i]->section->name, ".opd") == 0)
2332 ++i;
2333 codesecsym = i;
2334
2335 for (; i < symcount; ++i)
2336 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2337 | SEC_THREAD_LOCAL))
2338 != (SEC_CODE | SEC_ALLOC))
2339 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2340 break;
2341 codesecsymend = i;
2342
2343 for (; i < symcount; ++i)
2344 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2345 break;
2346 secsymend = i;
2347
2348 for (; i < symcount; ++i)
2349 if (strcmp (syms[i]->section->name, ".opd") != 0)
2350 break;
2351 opdsymend = i;
2352
2353 for (; i < symcount; ++i)
2354 if (((syms[i]->section->flags
2355 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2356 != (SEC_CODE | SEC_ALLOC))
2357 break;
2358 symcount = i;
2359 }
2360 count = 0;
2361
2362 if (relocatable)
2363 {
2364 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2365 arelent *r;
2366 size_t size;
2367 size_t relcount;
2368
2369 if (opdsymend == secsymend)
2370 goto done;
2371
2372 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2373 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2374 if (relcount == 0)
2375 goto done;
2376
2377 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2378 {
2379 count = -1;
2380 goto done;
2381 }
2382
2383 size = 0;
2384 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2385 {
2386 asymbol *sym;
2387
2388 while (r < opd->relocation + relcount
2389 && r->address < syms[i]->value + opd->vma)
2390 ++r;
2391
2392 if (r == opd->relocation + relcount)
2393 break;
2394
2395 if (r->address != syms[i]->value + opd->vma)
2396 continue;
2397
2398 if (r->howto->type != R_PPC64_ADDR64)
2399 continue;
2400
2401 sym = *r->sym_ptr_ptr;
2402 if (!sym_exists_at (syms, opdsymend, symcount,
2403 sym->section->id, sym->value + r->addend))
2404 {
2405 ++count;
2406 size += sizeof (asymbol);
2407 size += strlen (syms[i]->name) + 2;
2408 }
2409 }
2410
2411 if (size == 0)
2412 goto done;
2413 s = *ret = bfd_malloc (size);
2414 if (s == NULL)
2415 {
2416 count = -1;
2417 goto done;
2418 }
2419
2420 names = (char *) (s + count);
2421
2422 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2423 {
2424 asymbol *sym;
2425
2426 while (r < opd->relocation + relcount
2427 && r->address < syms[i]->value + opd->vma)
2428 ++r;
2429
2430 if (r == opd->relocation + relcount)
2431 break;
2432
2433 if (r->address != syms[i]->value + opd->vma)
2434 continue;
2435
2436 if (r->howto->type != R_PPC64_ADDR64)
2437 continue;
2438
2439 sym = *r->sym_ptr_ptr;
2440 if (!sym_exists_at (syms, opdsymend, symcount,
2441 sym->section->id, sym->value + r->addend))
2442 {
2443 size_t len;
2444
2445 *s = *syms[i];
2446 s->flags |= BSF_SYNTHETIC;
2447 s->section = sym->section;
2448 s->value = sym->value + r->addend;
2449 s->name = names;
2450 *names++ = '.';
2451 len = strlen (syms[i]->name);
2452 memcpy (names, syms[i]->name, len + 1);
2453 names += len + 1;
2454 /* Have udata.p point back to the original symbol this
2455 synthetic symbol was derived from. */
2456 s->udata.p = syms[i];
2457 s++;
2458 }
2459 }
2460 }
2461 else
2462 {
2463 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2464 bfd_byte *contents = NULL;
2465 size_t size;
2466 size_t plt_count = 0;
2467 bfd_vma glink_vma = 0, resolv_vma = 0;
2468 asection *dynamic, *glink = NULL, *relplt = NULL;
2469 arelent *p;
2470
2471 if (opd != NULL
2472 && ((opd->flags & SEC_HAS_CONTENTS) == 0
2473 || !bfd_malloc_and_get_section (abfd, opd, &contents)))
2474 {
2475 free_contents_and_exit_err:
2476 count = -1;
2477 free_contents_and_exit:
2478 free (contents);
2479 goto done;
2480 }
2481
2482 size = 0;
2483 for (i = secsymend; i < opdsymend; ++i)
2484 {
2485 bfd_vma ent;
2486
2487 /* Ignore bogus symbols. */
2488 if (syms[i]->value > opd->size - 8)
2489 continue;
2490
2491 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2492 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2493 {
2494 ++count;
2495 size += sizeof (asymbol);
2496 size += strlen (syms[i]->name) + 2;
2497 }
2498 }
2499
2500 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2501 if (dyn_count != 0
2502 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2503 {
2504 bfd_byte *dynbuf, *extdyn, *extdynend;
2505 size_t extdynsize;
2506 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2507
2508 if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2509 || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2510 goto free_contents_and_exit_err;
2511
2512 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2513 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2514
2515 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2516 (size_t) (extdynend - extdyn) >= extdynsize;
2517 extdyn += extdynsize)
2518 {
2519 Elf_Internal_Dyn dyn;
2520 (*swap_dyn_in) (abfd, extdyn, &dyn);
2521
2522 if (dyn.d_tag == DT_NULL)
2523 break;
2524
2525 if (dyn.d_tag == DT_PPC64_GLINK)
2526 {
2527 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2528 See comment in ppc64_elf_finish_dynamic_sections. */
2529 glink_vma = dyn.d_un.d_val + 8 * 4;
2530 /* The .glink section usually does not survive the final
2531 link; search for the section (usually .text) where the
2532 glink stubs now reside. */
2533 glink = bfd_sections_find_if (abfd, section_covers_vma,
2534 &glink_vma);
2535 break;
2536 }
2537 }
2538
2539 free (dynbuf);
2540 }
2541
2542 if (glink != NULL)
2543 {
2544 /* Determine __glink trampoline by reading the relative branch
2545 from the first glink stub. */
2546 bfd_byte buf[4];
2547 unsigned int off = 0;
2548
2549 while (bfd_get_section_contents (abfd, glink, buf,
2550 glink_vma + off - glink->vma, 4))
2551 {
2552 unsigned int insn = bfd_get_32 (abfd, buf);
2553 insn ^= B_DOT;
2554 if ((insn & ~0x3fffffc) == 0)
2555 {
2556 resolv_vma
2557 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2558 break;
2559 }
2560 off += 4;
2561 if (off > 4)
2562 break;
2563 }
2564
2565 if (resolv_vma)
2566 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2567
2568 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2569 if (relplt != NULL)
2570 {
2571 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2572 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2573 goto free_contents_and_exit_err;
2574
2575 plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
2576 size += plt_count * sizeof (asymbol);
2577
2578 p = relplt->relocation;
2579 for (i = 0; i < plt_count; i++, p++)
2580 {
2581 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2582 if (p->addend != 0)
2583 size += sizeof ("+0x") - 1 + 16;
2584 }
2585 }
2586 }
2587
2588 if (size == 0)
2589 goto free_contents_and_exit;
2590 s = *ret = bfd_malloc (size);
2591 if (s == NULL)
2592 goto free_contents_and_exit_err;
2593
2594 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2595
2596 for (i = secsymend; i < opdsymend; ++i)
2597 {
2598 bfd_vma ent;
2599
2600 if (syms[i]->value > opd->size - 8)
2601 continue;
2602
2603 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2604 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2605 {
2606 size_t lo, hi;
2607 size_t len;
2608 asection *sec = abfd->sections;
2609
2610 *s = *syms[i];
2611 lo = codesecsym;
2612 hi = codesecsymend;
2613 while (lo < hi)
2614 {
2615 size_t mid = (lo + hi) >> 1;
2616 if (syms[mid]->section->vma < ent)
2617 lo = mid + 1;
2618 else if (syms[mid]->section->vma > ent)
2619 hi = mid;
2620 else
2621 {
2622 sec = syms[mid]->section;
2623 break;
2624 }
2625 }
2626
2627 if (lo >= hi && lo > codesecsym)
2628 sec = syms[lo - 1]->section;
2629
2630 for (; sec != NULL; sec = sec->next)
2631 {
2632 if (sec->vma > ent)
2633 break;
2634 /* SEC_LOAD may not be set if SEC is from a separate debug
2635 info file. */
2636 if ((sec->flags & SEC_ALLOC) == 0)
2637 break;
2638 if ((sec->flags & SEC_CODE) != 0)
2639 s->section = sec;
2640 }
2641 s->flags |= BSF_SYNTHETIC;
2642 s->value = ent - s->section->vma;
2643 s->name = names;
2644 *names++ = '.';
2645 len = strlen (syms[i]->name);
2646 memcpy (names, syms[i]->name, len + 1);
2647 names += len + 1;
2648 /* Have udata.p point back to the original symbol this
2649 synthetic symbol was derived from. */
2650 s->udata.p = syms[i];
2651 s++;
2652 }
2653 }
2654 free (contents);
2655
2656 if (glink != NULL && relplt != NULL)
2657 {
2658 if (resolv_vma)
2659 {
2660 /* Add a symbol for the main glink trampoline. */
2661 memset (s, 0, sizeof *s);
2662 s->the_bfd = abfd;
2663 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2664 s->section = glink;
2665 s->value = resolv_vma - glink->vma;
2666 s->name = names;
2667 memcpy (names, "__glink_PLTresolve",
2668 sizeof ("__glink_PLTresolve"));
2669 names += sizeof ("__glink_PLTresolve");
2670 s++;
2671 count++;
2672 }
2673
2674 /* FIXME: It would be very much nicer to put sym@plt on the
2675 stub rather than on the glink branch table entry. The
2676 objdump disassembler would then use a sensible symbol
2677 name on plt calls. The difficulty in doing so is
2678 a) finding the stubs, and,
2679 b) matching stubs against plt entries, and,
2680 c) there can be multiple stubs for a given plt entry.
2681
2682 Solving (a) could be done by code scanning, but older
2683 ppc64 binaries used different stubs to current code.
2684 (b) is the tricky one since you need to known the toc
2685 pointer for at least one function that uses a pic stub to
2686 be able to calculate the plt address referenced.
2687 (c) means gdb would need to set multiple breakpoints (or
2688 find the glink branch itself) when setting breakpoints
2689 for pending shared library loads. */
2690 p = relplt->relocation;
2691 for (i = 0; i < plt_count; i++, p++)
2692 {
2693 size_t len;
2694
2695 *s = **p->sym_ptr_ptr;
2696 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2697 we are defining a symbol, ensure one of them is set. */
2698 if ((s->flags & BSF_LOCAL) == 0)
2699 s->flags |= BSF_GLOBAL;
2700 s->flags |= BSF_SYNTHETIC;
2701 s->section = glink;
2702 s->value = glink_vma - glink->vma;
2703 s->name = names;
2704 s->udata.p = NULL;
2705 len = strlen ((*p->sym_ptr_ptr)->name);
2706 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2707 names += len;
2708 if (p->addend != 0)
2709 {
2710 memcpy (names, "+0x", sizeof ("+0x") - 1);
2711 names += sizeof ("+0x") - 1;
2712 bfd_sprintf_vma (abfd, names, p->addend);
2713 names += strlen (names);
2714 }
2715 memcpy (names, "@plt", sizeof ("@plt"));
2716 names += sizeof ("@plt");
2717 s++;
2718 if (abi < 2)
2719 {
2720 glink_vma += 8;
2721 if (i >= 0x8000)
2722 glink_vma += 4;
2723 }
2724 else
2725 glink_vma += 4;
2726 }
2727 count += plt_count;
2728 }
2729 }
2730
2731 done:
2732 free (syms);
2733 return count;
2734 }
2735
2736 /* The following functions are specific to the ELF linker, while
2738 functions above are used generally. Those named ppc64_elf_* are
2739 called by the main ELF linker code. They appear in this file more
2740 or less in the order in which they are called. eg.
2741 ppc64_elf_check_relocs is called early in the link process,
2742 ppc64_elf_finish_dynamic_sections is one of the last functions
2743 called.
2744
2745 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2746 functions have both a function code symbol and a function descriptor
2747 symbol. A call to foo in a relocatable object file looks like:
2748
2749 . .text
2750 . x:
2751 . bl .foo
2752 . nop
2753
2754 The function definition in another object file might be:
2755
2756 . .section .opd
2757 . foo: .quad .foo
2758 . .quad .TOC.@tocbase
2759 . .quad 0
2760 .
2761 . .text
2762 . .foo: blr
2763
2764 When the linker resolves the call during a static link, the branch
2765 unsurprisingly just goes to .foo and the .opd information is unused.
2766 If the function definition is in a shared library, things are a little
2767 different: The call goes via a plt call stub, the opd information gets
2768 copied to the plt, and the linker patches the nop.
2769
2770 . x:
2771 . bl .foo_stub
2772 . ld 2,40(1)
2773 .
2774 .
2775 . .foo_stub:
2776 . std 2,40(1) # in practice, the call stub
2777 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2778 . addi 11,11,Lfoo@toc@l # this is the general idea
2779 . ld 12,0(11)
2780 . ld 2,8(11)
2781 . mtctr 12
2782 . ld 11,16(11)
2783 . bctr
2784 .
2785 . .section .plt
2786 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2787
2788 The "reloc ()" notation is supposed to indicate that the linker emits
2789 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2790 copying.
2791
2792 What are the difficulties here? Well, firstly, the relocations
2793 examined by the linker in check_relocs are against the function code
2794 sym .foo, while the dynamic relocation in the plt is emitted against
2795 the function descriptor symbol, foo. Somewhere along the line, we need
2796 to carefully copy dynamic link information from one symbol to the other.
2797 Secondly, the generic part of the elf linker will make .foo a dynamic
2798 symbol as is normal for most other backends. We need foo dynamic
2799 instead, at least for an application final link. However, when
2800 creating a shared library containing foo, we need to have both symbols
2801 dynamic so that references to .foo are satisfied during the early
2802 stages of linking. Otherwise the linker might decide to pull in a
2803 definition from some other object, eg. a static library.
2804
2805 Update: As of August 2004, we support a new convention. Function
2806 calls may use the function descriptor symbol, ie. "bl foo". This
2807 behaves exactly as "bl .foo". */
2808
2809 /* Of those relocs that might be copied as dynamic relocs, this
2810 function selects those that must be copied when linking a shared
2811 library or PIE, even when the symbol is local. */
2812
2813 static int
2814 must_be_dyn_reloc (struct bfd_link_info *info,
2815 enum elf_ppc64_reloc_type r_type)
2816 {
2817 switch (r_type)
2818 {
2819 default:
2820 /* Only relative relocs can be resolved when the object load
2821 address isn't fixed. DTPREL64 is excluded because the
2822 dynamic linker needs to differentiate global dynamic from
2823 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2824 return 1;
2825
2826 case R_PPC64_REL32:
2827 case R_PPC64_REL64:
2828 case R_PPC64_REL30:
2829 case R_PPC64_TOC16:
2830 case R_PPC64_TOC16_DS:
2831 case R_PPC64_TOC16_LO:
2832 case R_PPC64_TOC16_HI:
2833 case R_PPC64_TOC16_HA:
2834 case R_PPC64_TOC16_LO_DS:
2835 return 0;
2836
2837 case R_PPC64_TPREL16:
2838 case R_PPC64_TPREL16_LO:
2839 case R_PPC64_TPREL16_HI:
2840 case R_PPC64_TPREL16_HA:
2841 case R_PPC64_TPREL16_DS:
2842 case R_PPC64_TPREL16_LO_DS:
2843 case R_PPC64_TPREL16_HIGH:
2844 case R_PPC64_TPREL16_HIGHA:
2845 case R_PPC64_TPREL16_HIGHER:
2846 case R_PPC64_TPREL16_HIGHERA:
2847 case R_PPC64_TPREL16_HIGHEST:
2848 case R_PPC64_TPREL16_HIGHESTA:
2849 case R_PPC64_TPREL64:
2850 case R_PPC64_TPREL34:
2851 /* These relocations are relative but in a shared library the
2852 linker doesn't know the thread pointer base. */
2853 return bfd_link_dll (info);
2854 }
2855 }
2856
2857 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2858 copying dynamic variables from a shared lib into an app's .dynbss
2859 section, and instead use a dynamic relocation to point into the
2860 shared lib. With code that gcc generates it is vital that this be
2861 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2862 actually the address of a function descriptor which resides in the
2863 .opd section. gcc uses the descriptor directly rather than going
2864 via the GOT as some other ABIs do, which means that initialized
2865 function pointers reference the descriptor. Thus, a function
2866 pointer initialized to the address of a function in a shared
2867 library will either require a .dynbss copy and a copy reloc, or a
2868 dynamic reloc. Using a .dynbss copy redefines the function
2869 descriptor symbol to point to the copy. This presents a problem as
2870 a PLT entry for that function is also initialized from the function
2871 descriptor symbol and the copy may not be initialized first. */
2872 #define ELIMINATE_COPY_RELOCS 1
2873
2874 /* Section name for stubs is the associated section name plus this
2875 string. */
2876 #define STUB_SUFFIX ".stub"
2877
2878 /* Linker stubs.
2879 ppc_stub_long_branch:
2880 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2881 destination, but a 24 bit branch in a stub section will reach.
2882 . b dest
2883
2884 ppc_stub_plt_branch:
2885 Similar to the above, but a 24 bit branch in the stub section won't
2886 reach its destination.
2887 . addis %r12,%r2,xxx@toc@ha
2888 . ld %r12,xxx@toc@l(%r12)
2889 . mtctr %r12
2890 . bctr
2891
2892 ppc_stub_plt_call:
2893 Used to call a function in a shared library. If it so happens that
2894 the plt entry referenced crosses a 64k boundary, then an extra
2895 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2896 An r2save variant starts with "std %r2,40(%r1)".
2897 . addis %r11,%r2,xxx@toc@ha
2898 . ld %r12,xxx+0@toc@l(%r11)
2899 . mtctr %r12
2900 . ld %r2,xxx+8@toc@l(%r11)
2901 . ld %r11,xxx+16@toc@l(%r11)
2902 . bctr
2903
2904 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2905 code to adjust the value and save r2 to support multiple toc sections.
2906 A ppc_stub_long_branch with an r2 offset looks like:
2907 . std %r2,40(%r1)
2908 . addis %r2,%r2,off@ha
2909 . addi %r2,%r2,off@l
2910 . b dest
2911
2912 A ppc_stub_plt_branch with an r2 offset looks like:
2913 . std %r2,40(%r1)
2914 . addis %r12,%r2,xxx@toc@ha
2915 . ld %r12,xxx@toc@l(%r12)
2916 . addis %r2,%r2,off@ha
2917 . addi %r2,%r2,off@l
2918 . mtctr %r12
2919 . bctr
2920
2921 All of the above stubs are shown as their ELFv1 variants. ELFv2
2922 variants exist too, simpler for plt calls since a new toc pointer
2923 and static chain are not loaded by the stub. In addition, ELFv2
2924 has some more complex stubs to handle calls marked with NOTOC
2925 relocs from functions where r2 is not a valid toc pointer.
2926 ppc_stub_long_branch_p9notoc:
2927 . mflr %r12
2928 . bcl 20,31,1f
2929 . 1:
2930 . mflr %r11
2931 . mtlr %r12
2932 . addis %r12,%r11,dest-1b@ha
2933 . addi %r12,%r12,dest-1b@l
2934 . b dest
2935
2936 ppc_stub_plt_branch_p9notoc:
2937 . mflr %r12
2938 . bcl 20,31,1f
2939 . 1:
2940 . mflr %r11
2941 . mtlr %r12
2942 . lis %r12,xxx-1b@highest
2943 . ori %r12,%r12,xxx-1b@higher
2944 . sldi %r12,%r12,32
2945 . oris %r12,%r12,xxx-1b@high
2946 . ori %r12,%r12,xxx-1b@l
2947 . add %r12,%r11,%r12
2948 . mtctr %r12
2949 . bctr
2950
2951 ppc_stub_plt_call_p9notoc:
2952 . mflr %r12
2953 . bcl 20,31,1f
2954 . 1:
2955 . mflr %r11
2956 . mtlr %r12
2957 . lis %r12,xxx-1b@highest
2958 . ori %r12,%r12,xxx-1b@higher
2959 . sldi %r12,%r12,32
2960 . oris %r12,%r12,xxx-1b@high
2961 . ori %r12,%r12,xxx-1b@l
2962 . ldx %r12,%r11,%r12
2963 . mtctr %r12
2964 . bctr
2965
2966 There are also ELFv1 power10 variants of these stubs.
2967 ppc_stub_long_branch_notoc:
2968 . pla %r12,dest@pcrel
2969 . b dest
2970 ppc_stub_plt_branch_notoc:
2971 . lis %r11,(dest-1f)@highesta34
2972 . ori %r11,%r11,(dest-1f)@highera34
2973 . sldi %r11,%r11,34
2974 . 1: pla %r12,dest@pcrel
2975 . add %r12,%r11,%r12
2976 . mtctr %r12
2977 . bctr
2978 ppc_stub_plt_call_notoc:
2979 . lis %r11,(xxx-1f)@highesta34
2980 . ori %r11,%r11,(xxx-1f)@highera34
2981 . sldi %r11,%r11,34
2982 . 1: pla %r12,xxx@pcrel
2983 . ldx %r12,%r11,%r12
2984 . mtctr %r12
2985 . bctr
2986
2987 In cases where the high instructions would add zero, they are
2988 omitted and following instructions modified in some cases.
2989 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2990 to
2991 . pld %r12,xxx@pcrel
2992 . mtctr %r12
2993 . bctr
2994
2995 Stub variants may be merged. For example, if printf is called from
2996 code with the tocsave optimization (ie. r2 saved in function
2997 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2998 and from other code without the tocsave optimization requiring a
2999 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
3000 type will be created. Calls with the tocsave optimization will
3001 enter this stub after the instruction saving r2. A similar
3002 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3003 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3004 to call an external function like printf. If other calls to printf
3005 require a ppc_stub_plt_call linkage stub then a single
3006 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3007 call. */
3008
3009 enum ppc_stub_main_type
3010 {
3011 ppc_stub_none,
3012 ppc_stub_long_branch,
3013 ppc_stub_plt_branch,
3014 ppc_stub_plt_call,
3015 ppc_stub_global_entry,
3016 ppc_stub_save_res
3017 };
3018
3019 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3020 these variations. */
3021
3022 enum ppc_stub_sub_type
3023 {
3024 ppc_stub_toc,
3025 ppc_stub_notoc,
3026 ppc_stub_p9notoc
3027 };
3028
3029 struct ppc_stub_type
3030 {
3031 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3032 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3033 unsigned int r2save : 1;
3034 };
3035
3036 /* Information on stub grouping. */
3037 struct map_stub
3038 {
3039 /* The stub section. */
3040 asection *stub_sec;
3041 /* This is the section to which stubs in the group will be attached. */
3042 asection *link_sec;
3043 /* Next group. */
3044 struct map_stub *next;
3045 /* Whether to emit a copy of register save/restore functions in this
3046 group. */
3047 int needs_save_res;
3048 /* Current offset within stubs after the insn restoring lr in a
3049 _notoc or _both stub using bcl for pc-relative addressing, or
3050 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3051 unsigned int lr_restore;
3052 /* Accumulated size of EH info emitted to describe return address
3053 if stubs modify lr. Does not include 17 byte FDE header. */
3054 unsigned int eh_size;
3055 /* Offset in glink_eh_frame to the start of EH info for this group. */
3056 unsigned int eh_base;
3057 };
3058
3059 struct ppc_stub_hash_entry
3060 {
3061 /* Base hash table entry structure. */
3062 struct bfd_hash_entry root;
3063
3064 struct ppc_stub_type type;
3065
3066 /* Group information. */
3067 struct map_stub *group;
3068
3069 /* Offset within stub_sec of the beginning of this stub. */
3070 bfd_vma stub_offset;
3071
3072 /* Given the symbol's value and its section we can determine its final
3073 value when building the stubs (so the stub knows where to jump. */
3074 bfd_vma target_value;
3075 asection *target_section;
3076
3077 /* The symbol table entry, if any, that this was derived from. */
3078 struct ppc_link_hash_entry *h;
3079 struct plt_entry *plt_ent;
3080
3081 /* Symbol type. */
3082 unsigned char symtype;
3083
3084 /* Symbol st_other. */
3085 unsigned char other;
3086
3087 /* Debug: Track hash table traversal. */
3088 unsigned int id;
3089 };
3090
3091 struct ppc_branch_hash_entry
3092 {
3093 /* Base hash table entry structure. */
3094 struct bfd_hash_entry root;
3095
3096 /* Offset within branch lookup table. */
3097 unsigned int offset;
3098
3099 /* Generation marker. */
3100 unsigned int iter;
3101 };
3102
3103 /* Used to track dynamic relocations. */
3104 struct ppc_dyn_relocs
3105 {
3106 struct ppc_dyn_relocs *next;
3107
3108 /* The input section of the reloc. */
3109 asection *sec;
3110
3111 /* Total number of relocs copied for the input section. */
3112 unsigned int count;
3113
3114 /* Number of pc-relative relocs copied for the input section. */
3115 unsigned int pc_count;
3116
3117 /* Number of relocs that might become R_PPC64_RELATIVE. */
3118 unsigned int rel_count;
3119 };
3120
3121 struct ppc_local_dyn_relocs
3122 {
3123 struct ppc_local_dyn_relocs *next;
3124
3125 /* The input section of the reloc. */
3126 asection *sec;
3127
3128 /* Total number of relocs copied for the input section. */
3129 unsigned int count;
3130
3131 /* Number of relocs that might become R_PPC64_RELATIVE. */
3132 unsigned int rel_count : 31;
3133
3134 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3135 unsigned int ifunc : 1;
3136 };
3137
3138 struct ppc_link_hash_entry
3139 {
3140 struct elf_link_hash_entry elf;
3141
3142 union
3143 {
3144 /* A pointer to the most recently used stub hash entry against this
3145 symbol. */
3146 struct ppc_stub_hash_entry *stub_cache;
3147
3148 /* A pointer to the next symbol starting with a '.' */
3149 struct ppc_link_hash_entry *next_dot_sym;
3150 } u;
3151
3152 /* Link between function code and descriptor symbols. */
3153 struct ppc_link_hash_entry *oh;
3154
3155 /* Flag function code and descriptor symbols. */
3156 unsigned int is_func:1;
3157 unsigned int is_func_descriptor:1;
3158 unsigned int fake:1;
3159
3160 /* Whether global opd/toc sym has been adjusted or not.
3161 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3162 should be set for all globals defined in any opd/toc section. */
3163 unsigned int adjust_done:1;
3164
3165 /* Set if this is an out-of-line register save/restore function,
3166 with non-standard calling convention. */
3167 unsigned int save_res:1;
3168
3169 /* Set if a duplicate symbol with non-zero localentry is detected,
3170 even when the duplicate symbol does not provide a definition. */
3171 unsigned int non_zero_localentry:1;
3172
3173 /* Contexts in which symbol is used in the GOT (or TOC).
3174 Bits are or'd into the mask as the corresponding relocs are
3175 encountered during check_relocs, with TLS_TLS being set when any
3176 of the other TLS bits are set. tls_optimize clears bits when
3177 optimizing to indicate the corresponding GOT entry type is not
3178 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3179 set TLS_GDIE when a GD reloc turns into an IE one.
3180 These flags are also kept for local symbols. */
3181 #define TLS_TLS 1 /* Any TLS reloc. */
3182 #define TLS_GD 2 /* GD reloc. */
3183 #define TLS_LD 4 /* LD reloc. */
3184 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3185 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3186 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3187 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3188 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3189 unsigned char tls_mask;
3190
3191 /* The above field is also used to mark function symbols. In which
3192 case TLS_TLS will be 0. */
3193 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3194 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3195 #define NON_GOT 256 /* local symbol plt, not stored. */
3196 };
3197
3198 static inline struct ppc_link_hash_entry *
3199 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3200 {
3201 return (struct ppc_link_hash_entry *) ent;
3202 }
3203
3204 static inline struct elf_link_hash_entry *
3205 elf_hash_entry (struct ppc_link_hash_entry *ent)
3206 {
3207 return (struct elf_link_hash_entry *) ent;
3208 }
3209
3210 /* ppc64 ELF linker hash table. */
3211
3212 struct ppc_link_hash_table
3213 {
3214 struct elf_link_hash_table elf;
3215
3216 /* The stub hash table. */
3217 struct bfd_hash_table stub_hash_table;
3218
3219 /* Another hash table for plt_branch stubs. */
3220 struct bfd_hash_table branch_hash_table;
3221
3222 /* Hash table for function prologue tocsave. */
3223 htab_t tocsave_htab;
3224
3225 /* Various options and other info passed from the linker. */
3226 struct ppc64_elf_params *params;
3227
3228 /* The size of sec_info below. */
3229 unsigned int sec_info_arr_size;
3230
3231 /* Per-section array of extra section info. Done this way rather
3232 than as part of ppc64_elf_section_data so we have the info for
3233 non-ppc64 sections. */
3234 struct
3235 {
3236 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3237 bfd_vma toc_off;
3238
3239 union
3240 {
3241 /* The section group that this section belongs to. */
3242 struct map_stub *group;
3243 /* A temp section list pointer. */
3244 asection *list;
3245 } u;
3246 } *sec_info;
3247
3248 /* Linked list of groups. */
3249 struct map_stub *group;
3250
3251 /* Temp used when calculating TOC pointers. */
3252 bfd_vma toc_curr;
3253 bfd *toc_bfd;
3254 asection *toc_first_sec;
3255
3256 /* Used when adding symbols. */
3257 struct ppc_link_hash_entry *dot_syms;
3258
3259 /* Shortcuts to get to dynamic linker sections. */
3260 asection *glink;
3261 asection *global_entry;
3262 asection *sfpr;
3263 asection *pltlocal;
3264 asection *relpltlocal;
3265 asection *brlt;
3266 asection *relbrlt;
3267 asection *glink_eh_frame;
3268
3269 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3270 struct ppc_link_hash_entry *tls_get_addr;
3271 struct ppc_link_hash_entry *tls_get_addr_fd;
3272 struct ppc_link_hash_entry *tga_desc;
3273 struct ppc_link_hash_entry *tga_desc_fd;
3274 struct map_stub *tga_group;
3275
3276 /* The size of reliplt used by got entry relocs. */
3277 bfd_size_type got_reli_size;
3278
3279 /* DT_RELR array of section/r_offset. */
3280 size_t relr_alloc;
3281 size_t relr_count;
3282 struct
3283 {
3284 asection *sec;
3285 bfd_vma off;
3286 } *relr;
3287
3288 /* Statistics. */
3289 unsigned long stub_count[ppc_stub_save_res];
3290
3291 /* Number of stubs against global syms. */
3292 unsigned long stub_globals;
3293
3294 /* Set if we're linking code with function descriptors. */
3295 unsigned int opd_abi:1;
3296
3297 /* Support for multiple toc sections. */
3298 unsigned int do_multi_toc:1;
3299 unsigned int multi_toc_needed:1;
3300 unsigned int second_toc_pass:1;
3301 unsigned int do_toc_opt:1;
3302
3303 /* Set if tls optimization is enabled. */
3304 unsigned int do_tls_opt:1;
3305
3306 /* Set if inline plt calls should be converted to direct calls. */
3307 unsigned int can_convert_all_inline_plt:1;
3308
3309 /* Set if a stub_offset changed. */
3310 unsigned int stub_changed:1;
3311
3312 /* Set on error. */
3313 unsigned int stub_error:1;
3314
3315 /* Whether func_desc_adjust needs to be run over symbols. */
3316 unsigned int need_func_desc_adj:1;
3317
3318 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3319 unsigned int has_plt_localentry0:1;
3320
3321 /* Whether calls are made via the PLT from NOTOC functions. */
3322 unsigned int notoc_plt:1;
3323
3324 /* Whether any code linked seems to be Power10. */
3325 unsigned int has_power10_relocs:1;
3326
3327 /* Incremented once for each stub sized. */
3328 unsigned int stub_id;
3329
3330 /* Incremented every time we size stubs. */
3331 unsigned int stub_iteration;
3332
3333 /* After 20 iterations of stub sizing we no longer allow stubs to
3334 shrink. This is to break out of a pathological case where adding
3335 stubs or increasing their size on one iteration decreases section
3336 gaps (perhaps due to alignment), which then results in smaller
3337 stubs on the next iteration. */
3338 #define STUB_SHRINK_ITER 20
3339 };
3340
3341 /* Rename some of the generic section flags to better document how they
3342 are used here. */
3343
3344 /* Nonzero if this section has TLS related relocations. */
3345 #define has_tls_reloc sec_flg0
3346
3347 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3348 relocations. */
3349 #define nomark_tls_get_addr sec_flg1
3350
3351 /* Nonzero if this section has any toc or got relocs. */
3352 #define has_toc_reloc sec_flg2
3353
3354 /* Nonzero if this section has a call to another section that uses
3355 the toc or got. */
3356 #define makes_toc_func_call sec_flg3
3357
3358 /* Recursion protection when determining above flag. */
3359 #define call_check_in_progress sec_flg4
3360 #define call_check_done sec_flg5
3361
3362 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3363
3364 #define ppc_hash_table(p) \
3365 ((is_elf_hash_table ((p)->hash) \
3366 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3367 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3368
3369 #define ppc_stub_hash_lookup(table, string, create, copy) \
3370 ((struct ppc_stub_hash_entry *) \
3371 bfd_hash_lookup ((table), (string), (create), (copy)))
3372
3373 #define ppc_branch_hash_lookup(table, string, create, copy) \
3374 ((struct ppc_branch_hash_entry *) \
3375 bfd_hash_lookup ((table), (string), (create), (copy)))
3376
3377 /* Create an entry in the stub hash table. */
3378
3379 static struct bfd_hash_entry *
3380 stub_hash_newfunc (struct bfd_hash_entry *entry,
3381 struct bfd_hash_table *table,
3382 const char *string)
3383 {
3384 /* Allocate the structure if it has not already been allocated by a
3385 subclass. */
3386 if (entry == NULL)
3387 {
3388 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3389 if (entry == NULL)
3390 return entry;
3391 }
3392
3393 /* Call the allocation method of the superclass. */
3394 entry = bfd_hash_newfunc (entry, table, string);
3395 if (entry != NULL)
3396 {
3397 struct ppc_stub_hash_entry *eh;
3398
3399 /* Initialize the local fields. */
3400 eh = (struct ppc_stub_hash_entry *) entry;
3401 eh->type.main = ppc_stub_none;
3402 eh->type.sub = ppc_stub_toc;
3403 eh->type.r2save = 0;
3404 eh->group = NULL;
3405 eh->stub_offset = 0;
3406 eh->target_value = 0;
3407 eh->target_section = NULL;
3408 eh->h = NULL;
3409 eh->plt_ent = NULL;
3410 eh->symtype = 0;
3411 eh->other = 0;
3412 eh->id = 0;
3413 }
3414
3415 return entry;
3416 }
3417
3418 /* Create an entry in the branch hash table. */
3419
3420 static struct bfd_hash_entry *
3421 branch_hash_newfunc (struct bfd_hash_entry *entry,
3422 struct bfd_hash_table *table,
3423 const char *string)
3424 {
3425 /* Allocate the structure if it has not already been allocated by a
3426 subclass. */
3427 if (entry == NULL)
3428 {
3429 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3430 if (entry == NULL)
3431 return entry;
3432 }
3433
3434 /* Call the allocation method of the superclass. */
3435 entry = bfd_hash_newfunc (entry, table, string);
3436 if (entry != NULL)
3437 {
3438 struct ppc_branch_hash_entry *eh;
3439
3440 /* Initialize the local fields. */
3441 eh = (struct ppc_branch_hash_entry *) entry;
3442 eh->offset = 0;
3443 eh->iter = 0;
3444 }
3445
3446 return entry;
3447 }
3448
3449 /* Create an entry in a ppc64 ELF linker hash table. */
3450
3451 static struct bfd_hash_entry *
3452 link_hash_newfunc (struct bfd_hash_entry *entry,
3453 struct bfd_hash_table *table,
3454 const char *string)
3455 {
3456 /* Allocate the structure if it has not already been allocated by a
3457 subclass. */
3458 if (entry == NULL)
3459 {
3460 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3461 if (entry == NULL)
3462 return entry;
3463 }
3464
3465 /* Call the allocation method of the superclass. */
3466 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3467 if (entry != NULL)
3468 {
3469 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3470
3471 memset (&eh->u.stub_cache, 0,
3472 (sizeof (struct ppc_link_hash_entry)
3473 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3474
3475 /* When making function calls, old ABI code references function entry
3476 points (dot symbols), while new ABI code references the function
3477 descriptor symbol. We need to make any combination of reference and
3478 definition work together, without breaking archive linking.
3479
3480 For a defined function "foo" and an undefined call to "bar":
3481 An old object defines "foo" and ".foo", references ".bar" (possibly
3482 "bar" too).
3483 A new object defines "foo" and references "bar".
3484
3485 A new object thus has no problem with its undefined symbols being
3486 satisfied by definitions in an old object. On the other hand, the
3487 old object won't have ".bar" satisfied by a new object.
3488
3489 Keep a list of newly added dot-symbols. */
3490
3491 if (string[0] == '.')
3492 {
3493 struct ppc_link_hash_table *htab;
3494
3495 htab = (struct ppc_link_hash_table *) table;
3496 eh->u.next_dot_sym = htab->dot_syms;
3497 htab->dot_syms = eh;
3498 }
3499 }
3500
3501 return entry;
3502 }
3503
3504 struct tocsave_entry
3505 {
3506 asection *sec;
3507 bfd_vma offset;
3508 };
3509
3510 static hashval_t
3511 tocsave_htab_hash (const void *p)
3512 {
3513 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3514 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3515 }
3516
3517 static int
3518 tocsave_htab_eq (const void *p1, const void *p2)
3519 {
3520 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3521 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3522 return e1->sec == e2->sec && e1->offset == e2->offset;
3523 }
3524
3525 /* Destroy a ppc64 ELF linker hash table. */
3526
3527 static void
3528 ppc64_elf_link_hash_table_free (bfd *obfd)
3529 {
3530 struct ppc_link_hash_table *htab;
3531
3532 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3533 if (htab->tocsave_htab)
3534 htab_delete (htab->tocsave_htab);
3535 bfd_hash_table_free (&htab->branch_hash_table);
3536 bfd_hash_table_free (&htab->stub_hash_table);
3537 _bfd_elf_link_hash_table_free (obfd);
3538 }
3539
3540 /* Create a ppc64 ELF linker hash table. */
3541
3542 static struct bfd_link_hash_table *
3543 ppc64_elf_link_hash_table_create (bfd *abfd)
3544 {
3545 struct ppc_link_hash_table *htab;
3546 size_t amt = sizeof (struct ppc_link_hash_table);
3547
3548 htab = bfd_zmalloc (amt);
3549 if (htab == NULL)
3550 return NULL;
3551
3552 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3553 sizeof (struct ppc_link_hash_entry)))
3554 {
3555 free (htab);
3556 return NULL;
3557 }
3558
3559 /* Init the stub hash table too. */
3560 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3561 sizeof (struct ppc_stub_hash_entry)))
3562 {
3563 _bfd_elf_link_hash_table_free (abfd);
3564 return NULL;
3565 }
3566
3567 /* And the branch hash table. */
3568 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3569 sizeof (struct ppc_branch_hash_entry)))
3570 {
3571 bfd_hash_table_free (&htab->stub_hash_table);
3572 _bfd_elf_link_hash_table_free (abfd);
3573 return NULL;
3574 }
3575
3576 htab->tocsave_htab = htab_try_create (1024,
3577 tocsave_htab_hash,
3578 tocsave_htab_eq,
3579 NULL);
3580 if (htab->tocsave_htab == NULL)
3581 {
3582 ppc64_elf_link_hash_table_free (abfd);
3583 return NULL;
3584 }
3585 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3586
3587 /* Initializing two fields of the union is just cosmetic. We really
3588 only care about glist, but when compiled on a 32-bit host the
3589 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3590 debugger inspection of these fields look nicer. */
3591 htab->elf.init_got_refcount.refcount = 0;
3592 htab->elf.init_got_refcount.glist = NULL;
3593 htab->elf.init_plt_refcount.refcount = 0;
3594 htab->elf.init_plt_refcount.glist = NULL;
3595 htab->elf.init_got_offset.offset = 0;
3596 htab->elf.init_got_offset.glist = NULL;
3597 htab->elf.init_plt_offset.offset = 0;
3598 htab->elf.init_plt_offset.glist = NULL;
3599
3600 return &htab->elf.root;
3601 }
3602
3603 /* Create sections for linker generated code. */
3604
3605 static bool
3606 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3607 {
3608 struct ppc_link_hash_table *htab;
3609 flagword flags;
3610
3611 htab = ppc_hash_table (info);
3612
3613 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3614 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3615 if (htab->params->save_restore_funcs)
3616 {
3617 /* Create .sfpr for code to save and restore fp regs. */
3618 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3619 flags);
3620 if (htab->sfpr == NULL
3621 || !bfd_set_section_alignment (htab->sfpr, 2))
3622 return false;
3623 }
3624
3625 if (bfd_link_relocatable (info))
3626 return true;
3627
3628 /* Create .glink for lazy dynamic linking support. */
3629 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3630 flags);
3631 if (htab->glink == NULL
3632 || !bfd_set_section_alignment (htab->glink, 3))
3633 return false;
3634
3635 /* The part of .glink used by global entry stubs, separate so that
3636 it can be aligned appropriately without affecting htab->glink. */
3637 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3638 flags);
3639 if (htab->global_entry == NULL
3640 || !bfd_set_section_alignment (htab->global_entry, 2))
3641 return false;
3642
3643 if (!info->no_ld_generated_unwind_info)
3644 {
3645 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3646 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3647 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3648 ".eh_frame",
3649 flags);
3650 if (htab->glink_eh_frame == NULL
3651 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3652 return false;
3653 }
3654
3655 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3656 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3657 if (htab->elf.iplt == NULL
3658 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3659 return false;
3660
3661 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3662 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3663 htab->elf.irelplt
3664 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3665 if (htab->elf.irelplt == NULL
3666 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3667 return false;
3668
3669 /* Create branch lookup table for plt_branch stubs. */
3670 flags = (SEC_ALLOC | SEC_LOAD
3671 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3672 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3673 flags);
3674 if (htab->brlt == NULL
3675 || !bfd_set_section_alignment (htab->brlt, 3))
3676 return false;
3677
3678 /* Local plt entries, put in .branch_lt but a separate section for
3679 convenience. */
3680 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3681 flags);
3682 if (htab->pltlocal == NULL
3683 || !bfd_set_section_alignment (htab->pltlocal, 3))
3684 return false;
3685
3686 if (!bfd_link_pic (info))
3687 return true;
3688
3689 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3690 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3691 htab->relbrlt
3692 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3693 if (htab->relbrlt == NULL
3694 || !bfd_set_section_alignment (htab->relbrlt, 3))
3695 return false;
3696
3697 htab->relpltlocal
3698 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3699 if (htab->relpltlocal == NULL
3700 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3701 return false;
3702
3703 return true;
3704 }
3705
3706 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3707
3708 bool
3709 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3710 struct ppc64_elf_params *params)
3711 {
3712 struct ppc_link_hash_table *htab;
3713
3714 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3715
3716 /* Always hook our dynamic sections into the first bfd, which is the
3717 linker created stub bfd. This ensures that the GOT header is at
3718 the start of the output TOC section. */
3719 htab = ppc_hash_table (info);
3720 htab->elf.dynobj = params->stub_bfd;
3721 htab->params = params;
3722
3723 return create_linkage_sections (htab->elf.dynobj, info);
3724 }
3725
3726 /* Build a name for an entry in the stub hash table. */
3727
3728 static char *
3729 ppc_stub_name (const asection *input_section,
3730 const asection *sym_sec,
3731 const struct ppc_link_hash_entry *h,
3732 const Elf_Internal_Rela *rel)
3733 {
3734 char *stub_name;
3735 ssize_t len;
3736
3737 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3738 offsets from a sym as a branch target? In fact, we could
3739 probably assume the addend is always zero. */
3740 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3741
3742 if (h)
3743 {
3744 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3745 stub_name = bfd_malloc (len);
3746 if (stub_name == NULL)
3747 return stub_name;
3748
3749 len = sprintf (stub_name, "%08x.%s+%x",
3750 input_section->id & 0xffffffff,
3751 h->elf.root.root.string,
3752 (int) rel->r_addend & 0xffffffff);
3753 }
3754 else
3755 {
3756 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3757 stub_name = bfd_malloc (len);
3758 if (stub_name == NULL)
3759 return stub_name;
3760
3761 len = sprintf (stub_name, "%08x.%x:%x+%x",
3762 input_section->id & 0xffffffff,
3763 sym_sec->id & 0xffffffff,
3764 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3765 (int) rel->r_addend & 0xffffffff);
3766 }
3767 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3768 stub_name[len - 2] = 0;
3769 return stub_name;
3770 }
3771
3772 /* If mixing power10 with non-power10 code and --power10-stubs is not
3773 specified (or is auto) then there may be multiple stub types for any
3774 given symbol. Up to three classes of stubs are stored in separate
3775 stub_hash_table entries having the same key string. The entries
3776 will always be adjacent on entry->root.next chain, even if hash
3777 table resizing occurs. This function selects the correct entry to
3778 use. */
3779
3780 static struct ppc_stub_hash_entry *
3781 select_alt_stub (struct ppc_stub_hash_entry *entry,
3782 enum elf_ppc64_reloc_type r_type)
3783 {
3784 enum ppc_stub_sub_type subt;
3785
3786 switch (r_type)
3787 {
3788 case R_PPC64_REL24_NOTOC:
3789 subt = ppc_stub_notoc;
3790 break;
3791 case R_PPC64_REL24_P9NOTOC:
3792 subt = ppc_stub_p9notoc;
3793 break;
3794 default:
3795 subt = ppc_stub_toc;
3796 break;
3797 }
3798
3799 while (entry != NULL && entry->type.sub != subt)
3800 {
3801 const char *stub_name = entry->root.string;
3802
3803 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3804 if (entry != NULL
3805 && entry->root.string != stub_name)
3806 entry = NULL;
3807 }
3808
3809 return entry;
3810 }
3811
3812 /* Look up an entry in the stub hash. Stub entries are cached because
3813 creating the stub name takes a bit of time. */
3814
3815 static struct ppc_stub_hash_entry *
3816 ppc_get_stub_entry (const asection *input_section,
3817 const asection *sym_sec,
3818 struct ppc_link_hash_entry *h,
3819 const Elf_Internal_Rela *rel,
3820 struct ppc_link_hash_table *htab)
3821 {
3822 struct ppc_stub_hash_entry *stub_entry;
3823 struct map_stub *group;
3824
3825 /* If this input section is part of a group of sections sharing one
3826 stub section, then use the id of the first section in the group.
3827 Stub names need to include a section id, as there may well be
3828 more than one stub used to reach say, printf, and we need to
3829 distinguish between them. */
3830 group = htab->sec_info[input_section->id].u.group;
3831 if (group == NULL)
3832 return NULL;
3833
3834 if (h != NULL && h->u.stub_cache != NULL
3835 && h->u.stub_cache->h == h
3836 && h->u.stub_cache->group == group)
3837 {
3838 stub_entry = h->u.stub_cache;
3839 }
3840 else
3841 {
3842 char *stub_name;
3843
3844 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3845 if (stub_name == NULL)
3846 return NULL;
3847
3848 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3849 stub_name, false, false);
3850 if (h != NULL)
3851 h->u.stub_cache = stub_entry;
3852
3853 free (stub_name);
3854 }
3855
3856 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3857 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3858
3859 return stub_entry;
3860 }
3861
3862 /* Add a new stub entry to the stub hash. Not all fields of the new
3863 stub entry are initialised. */
3864
3865 static struct ppc_stub_hash_entry *
3866 ppc_add_stub (const char *stub_name,
3867 asection *section,
3868 struct bfd_link_info *info)
3869 {
3870 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3871 struct map_stub *group;
3872 asection *link_sec;
3873 asection *stub_sec;
3874 struct ppc_stub_hash_entry *stub_entry;
3875
3876 group = htab->sec_info[section->id].u.group;
3877 link_sec = group->link_sec;
3878 stub_sec = group->stub_sec;
3879 if (stub_sec == NULL)
3880 {
3881 size_t namelen;
3882 bfd_size_type len;
3883 char *s_name;
3884
3885 namelen = strlen (link_sec->name);
3886 len = namelen + sizeof (STUB_SUFFIX);
3887 s_name = bfd_alloc (htab->params->stub_bfd, len);
3888 if (s_name == NULL)
3889 return NULL;
3890
3891 memcpy (s_name, link_sec->name, namelen);
3892 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3893 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3894 if (stub_sec == NULL)
3895 return NULL;
3896 group->stub_sec = stub_sec;
3897 }
3898
3899 /* Enter this entry into the linker stub hash table. */
3900 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3901 true, false);
3902 if (stub_entry == NULL)
3903 {
3904 /* xgettext:c-format */
3905 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3906 section->owner, stub_name);
3907 return NULL;
3908 }
3909
3910 stub_entry->group = group;
3911 stub_entry->stub_offset = 0;
3912 return stub_entry;
3913 }
3914
3915 /* A stub has already been created, but it may not be the required
3916 type. We shouldn't be transitioning from plt_call to long_branch
3917 stubs or vice versa, but we might be upgrading from plt_call to
3918 plt_call with r2save for example. */
3919
3920 static bool
3921 ppc_merge_stub (struct ppc_link_hash_table *htab,
3922 struct ppc_stub_hash_entry *stub_entry,
3923 struct ppc_stub_type stub_type,
3924 enum elf_ppc64_reloc_type r_type)
3925 {
3926 struct ppc_stub_type old_type = stub_entry->type;
3927
3928 if (old_type.main == ppc_stub_save_res)
3929 return true;
3930
3931 if (htab->params->power10_stubs == -1)
3932 {
3933 /* For --power10-stubs=auto, don't merge _notoc and other
3934 varieties of stubs. */
3935 struct ppc_stub_hash_entry *alt_stub;
3936
3937 alt_stub = select_alt_stub (stub_entry, r_type);
3938 if (alt_stub == NULL)
3939 {
3940 alt_stub = ((struct ppc_stub_hash_entry *)
3941 stub_hash_newfunc (NULL,
3942 &htab->stub_hash_table,
3943 stub_entry->root.string));
3944 if (alt_stub == NULL)
3945 return false;
3946
3947 *alt_stub = *stub_entry;
3948 stub_entry->root.next = &alt_stub->root;
3949
3950 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3951 Not that it matters, this just puts smaller stubs first. */
3952 if (stub_type.sub == ppc_stub_notoc)
3953 alt_stub = stub_entry;
3954 else if (stub_type.sub == ppc_stub_p9notoc
3955 && alt_stub->root.next
3956 && alt_stub->root.next->string == alt_stub->root.string)
3957 {
3958 struct ppc_stub_hash_entry *next
3959 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3960 alt_stub->type = next->type;
3961 alt_stub = next;
3962 }
3963 alt_stub->type = stub_type;
3964 return true;
3965 }
3966 stub_entry = alt_stub;
3967 }
3968
3969 old_type = stub_entry->type;
3970 if (old_type.main == ppc_stub_plt_branch)
3971 old_type.main = ppc_stub_long_branch;
3972
3973 if (old_type.main != stub_type.main
3974 || (old_type.sub != stub_type.sub
3975 && old_type.sub != ppc_stub_toc
3976 && stub_type.sub != ppc_stub_toc))
3977 abort ();
3978
3979 stub_entry->type.sub |= stub_type.sub;
3980 stub_entry->type.r2save |= stub_type.r2save;
3981 return true;
3982 }
3983
3984 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3985 not already done. */
3986
3987 static bool
3988 create_got_section (bfd *abfd, struct bfd_link_info *info)
3989 {
3990 asection *got, *relgot;
3991 flagword flags;
3992 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3993
3994 if (!is_ppc64_elf (abfd))
3995 return false;
3996 if (htab == NULL)
3997 return false;
3998
3999 if (!htab->elf.sgot
4000 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4001 return false;
4002
4003 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4004 | SEC_LINKER_CREATED);
4005
4006 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4007 if (!got
4008 || !bfd_set_section_alignment (got, 3))
4009 return false;
4010
4011 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4012 flags | SEC_READONLY);
4013 if (!relgot
4014 || !bfd_set_section_alignment (relgot, 3))
4015 return false;
4016
4017 ppc64_elf_tdata (abfd)->got = got;
4018 ppc64_elf_tdata (abfd)->relgot = relgot;
4019 return true;
4020 }
4021
4022 /* Follow indirect and warning symbol links. */
4023
4024 static inline struct bfd_link_hash_entry *
4025 follow_link (struct bfd_link_hash_entry *h)
4026 {
4027 while (h->type == bfd_link_hash_indirect
4028 || h->type == bfd_link_hash_warning)
4029 h = h->u.i.link;
4030 return h;
4031 }
4032
4033 static inline struct elf_link_hash_entry *
4034 elf_follow_link (struct elf_link_hash_entry *h)
4035 {
4036 return (struct elf_link_hash_entry *) follow_link (&h->root);
4037 }
4038
4039 static inline struct ppc_link_hash_entry *
4040 ppc_follow_link (struct ppc_link_hash_entry *h)
4041 {
4042 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4043 }
4044
4045 /* Merge PLT info on FROM with that on TO. */
4046
4047 static void
4048 move_plt_plist (struct ppc_link_hash_entry *from,
4049 struct ppc_link_hash_entry *to)
4050 {
4051 if (from->elf.plt.plist != NULL)
4052 {
4053 if (to->elf.plt.plist != NULL)
4054 {
4055 struct plt_entry **entp;
4056 struct plt_entry *ent;
4057
4058 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4059 {
4060 struct plt_entry *dent;
4061
4062 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4063 if (dent->addend == ent->addend)
4064 {
4065 dent->plt.refcount += ent->plt.refcount;
4066 *entp = ent->next;
4067 break;
4068 }
4069 if (dent == NULL)
4070 entp = &ent->next;
4071 }
4072 *entp = to->elf.plt.plist;
4073 }
4074
4075 to->elf.plt.plist = from->elf.plt.plist;
4076 from->elf.plt.plist = NULL;
4077 }
4078 }
4079
4080 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4081
4082 static void
4083 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4084 struct elf_link_hash_entry *dir,
4085 struct elf_link_hash_entry *ind)
4086 {
4087 struct ppc_link_hash_entry *edir, *eind;
4088
4089 edir = ppc_elf_hash_entry (dir);
4090 eind = ppc_elf_hash_entry (ind);
4091
4092 edir->is_func |= eind->is_func;
4093 edir->is_func_descriptor |= eind->is_func_descriptor;
4094 edir->tls_mask |= eind->tls_mask;
4095 if (eind->oh != NULL)
4096 edir->oh = ppc_follow_link (eind->oh);
4097
4098 if (edir->elf.versioned != versioned_hidden)
4099 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4100 edir->elf.ref_regular |= eind->elf.ref_regular;
4101 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4102 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4103 edir->elf.needs_plt |= eind->elf.needs_plt;
4104 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4105
4106 /* If we were called to copy over info for a weak sym, don't copy
4107 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4108 in order to simplify readonly_dynrelocs and save a field in the
4109 symbol hash entry, but that means dyn_relocs can't be used in any
4110 tests about a specific symbol, or affect other symbol flags which
4111 are then tested. */
4112 if (eind->elf.root.type != bfd_link_hash_indirect)
4113 return;
4114
4115 /* Copy over any dynamic relocs we may have on the indirect sym. */
4116 if (ind->dyn_relocs != NULL)
4117 {
4118 if (dir->dyn_relocs != NULL)
4119 {
4120 struct ppc_dyn_relocs **pp;
4121 struct ppc_dyn_relocs *p;
4122
4123 /* Add reloc counts against the indirect sym to the direct sym
4124 list. Merge any entries against the same section. */
4125 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4126 (p = *pp) != NULL;
4127 )
4128 {
4129 struct ppc_dyn_relocs *q;
4130
4131 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4132 q != NULL;
4133 q = q->next)
4134 if (q->sec == p->sec)
4135 {
4136 q->count += p->count;
4137 q->pc_count += p->pc_count;
4138 q->rel_count += p->rel_count;
4139 *pp = p->next;
4140 break;
4141 }
4142 if (q == NULL)
4143 pp = &p->next;
4144 }
4145 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4146 }
4147
4148 dir->dyn_relocs = ind->dyn_relocs;
4149 ind->dyn_relocs = NULL;
4150 }
4151
4152 /* Copy over got entries that we may have already seen to the
4153 symbol which just became indirect. */
4154 if (eind->elf.got.glist != NULL)
4155 {
4156 if (edir->elf.got.glist != NULL)
4157 {
4158 struct got_entry **entp;
4159 struct got_entry *ent;
4160
4161 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4162 {
4163 struct got_entry *dent;
4164
4165 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4166 if (dent->addend == ent->addend
4167 && dent->owner == ent->owner
4168 && dent->tls_type == ent->tls_type)
4169 {
4170 dent->got.refcount += ent->got.refcount;
4171 *entp = ent->next;
4172 break;
4173 }
4174 if (dent == NULL)
4175 entp = &ent->next;
4176 }
4177 *entp = edir->elf.got.glist;
4178 }
4179
4180 edir->elf.got.glist = eind->elf.got.glist;
4181 eind->elf.got.glist = NULL;
4182 }
4183
4184 /* And plt entries. */
4185 move_plt_plist (eind, edir);
4186
4187 if (eind->elf.dynindx != -1)
4188 {
4189 if (edir->elf.dynindx != -1)
4190 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4191 edir->elf.dynstr_index);
4192 edir->elf.dynindx = eind->elf.dynindx;
4193 edir->elf.dynstr_index = eind->elf.dynstr_index;
4194 eind->elf.dynindx = -1;
4195 eind->elf.dynstr_index = 0;
4196 }
4197 }
4198
4199 /* Find the function descriptor hash entry from the given function code
4200 hash entry FH. Link the entries via their OH fields. */
4201
4202 static struct ppc_link_hash_entry *
4203 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4204 {
4205 struct ppc_link_hash_entry *fdh = fh->oh;
4206
4207 if (fdh == NULL)
4208 {
4209 const char *fd_name = fh->elf.root.root.string + 1;
4210
4211 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4212 false, false, false));
4213 if (fdh == NULL)
4214 return fdh;
4215
4216 fdh->is_func_descriptor = 1;
4217 fdh->oh = fh;
4218 fh->is_func = 1;
4219 fh->oh = fdh;
4220 }
4221
4222 fdh = ppc_follow_link (fdh);
4223 fdh->is_func_descriptor = 1;
4224 fdh->oh = fh;
4225 return fdh;
4226 }
4227
4228 /* Make a fake function descriptor sym for the undefined code sym FH. */
4229
4230 static struct ppc_link_hash_entry *
4231 make_fdh (struct bfd_link_info *info,
4232 struct ppc_link_hash_entry *fh)
4233 {
4234 bfd *abfd = fh->elf.root.u.undef.abfd;
4235 struct bfd_link_hash_entry *bh = NULL;
4236 struct ppc_link_hash_entry *fdh;
4237 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4238 ? BSF_WEAK
4239 : BSF_GLOBAL);
4240
4241 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4242 fh->elf.root.root.string + 1,
4243 flags, bfd_und_section_ptr, 0,
4244 NULL, false, false, &bh))
4245 return NULL;
4246
4247 fdh = (struct ppc_link_hash_entry *) bh;
4248 fdh->elf.non_elf = 0;
4249 fdh->fake = 1;
4250 fdh->is_func_descriptor = 1;
4251 fdh->oh = fh;
4252 fh->is_func = 1;
4253 fh->oh = fdh;
4254 return fdh;
4255 }
4256
4257 /* Fix function descriptor symbols defined in .opd sections to be
4258 function type. */
4259
4260 static bool
4261 ppc64_elf_add_symbol_hook (bfd *ibfd,
4262 struct bfd_link_info *info,
4263 Elf_Internal_Sym *isym,
4264 const char **name,
4265 flagword *flags ATTRIBUTE_UNUSED,
4266 asection **sec,
4267 bfd_vma *value)
4268 {
4269 if (*sec != NULL
4270 && strcmp ((*sec)->name, ".opd") == 0)
4271 {
4272 asection *code_sec;
4273
4274 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4275 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4276 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4277
4278 /* If the symbol is a function defined in .opd, and the function
4279 code is in a discarded group, let it appear to be undefined. */
4280 if (!bfd_link_relocatable (info)
4281 && (*sec)->reloc_count != 0
4282 && opd_entry_value (*sec, *value, &code_sec, NULL,
4283 false) != (bfd_vma) -1
4284 && discarded_section (code_sec))
4285 {
4286 *sec = bfd_und_section_ptr;
4287 isym->st_shndx = SHN_UNDEF;
4288 }
4289 }
4290 else if (*sec != NULL
4291 && strcmp ((*sec)->name, ".toc") == 0
4292 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4293 {
4294 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4295 if (htab != NULL)
4296 htab->params->object_in_toc = 1;
4297 }
4298
4299 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4300 {
4301 if (abiversion (ibfd) == 0)
4302 set_abiversion (ibfd, 2);
4303 else if (abiversion (ibfd) == 1)
4304 {
4305 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4306 " for ABI version 1"), *name);
4307 bfd_set_error (bfd_error_bad_value);
4308 return false;
4309 }
4310 }
4311
4312 return true;
4313 }
4314
4315 /* Merge non-visibility st_other attributes: local entry point. */
4316
4317 static void
4318 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4319 unsigned int st_other,
4320 bool definition,
4321 bool dynamic)
4322 {
4323 if (definition && (!dynamic || !h->def_regular))
4324 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4325 | ELF_ST_VISIBILITY (h->other));
4326 }
4327
4328 /* Hook called on merging a symbol. We use this to clear "fake" since
4329 we now have a real symbol. */
4330
4331 static bool
4332 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4333 const Elf_Internal_Sym *isym,
4334 asection **psec ATTRIBUTE_UNUSED,
4335 bool newdef ATTRIBUTE_UNUSED,
4336 bool olddef ATTRIBUTE_UNUSED,
4337 bfd *oldbfd ATTRIBUTE_UNUSED,
4338 const asection *oldsec ATTRIBUTE_UNUSED)
4339 {
4340 ppc_elf_hash_entry (h)->fake = 0;
4341 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4342 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4343 return true;
4344 }
4345
4346 /* This function makes an old ABI object reference to ".bar" cause the
4347 inclusion of a new ABI object archive that defines "bar".
4348 NAME is a symbol defined in an archive. Return a symbol in the hash
4349 table that might be satisfied by the archive symbols. */
4350
4351 static struct bfd_link_hash_entry *
4352 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4353 struct bfd_link_info *info,
4354 const char *name)
4355 {
4356 struct bfd_link_hash_entry *h;
4357 char *dot_name;
4358 size_t len;
4359
4360 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4361 if (h != NULL
4362 && ppc_hash_table (info) != NULL
4363 /* Don't return this sym if it is a fake function descriptor
4364 created by add_symbol_adjust. */
4365 && !((struct ppc_link_hash_entry *) h)->fake)
4366 return h;
4367
4368 if (name[0] == '.')
4369 return h;
4370
4371 len = strlen (name);
4372 dot_name = bfd_alloc (abfd, len + 2);
4373 if (dot_name == NULL)
4374 return (struct bfd_link_hash_entry *) -1;
4375 dot_name[0] = '.';
4376 memcpy (dot_name + 1, name, len + 1);
4377 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4378 bfd_release (abfd, dot_name);
4379 if (h != NULL)
4380 return h;
4381
4382 if (strcmp (name, "__tls_get_addr_opt") == 0)
4383 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4384 return h;
4385 }
4386
4387 /* This function satisfies all old ABI object references to ".bar" if a
4388 new ABI object defines "bar". Well, at least, undefined dot symbols
4389 are made weak. This stops later archive searches from including an
4390 object if we already have a function descriptor definition. It also
4391 prevents the linker complaining about undefined symbols.
4392 We also check and correct mismatched symbol visibility here. The
4393 most restrictive visibility of the function descriptor and the
4394 function entry symbol is used. */
4395
4396 static bool
4397 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4398 {
4399 struct ppc_link_hash_table *htab;
4400 struct ppc_link_hash_entry *fdh;
4401
4402 if (eh->elf.root.type == bfd_link_hash_warning)
4403 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4404
4405 if (eh->elf.root.type == bfd_link_hash_indirect)
4406 return true;
4407
4408 if (eh->elf.root.root.string[0] != '.')
4409 abort ();
4410
4411 htab = ppc_hash_table (info);
4412 if (htab == NULL)
4413 return false;
4414
4415 fdh = lookup_fdh (eh, htab);
4416 if (fdh == NULL
4417 && !bfd_link_relocatable (info)
4418 && (eh->elf.root.type == bfd_link_hash_undefined
4419 || eh->elf.root.type == bfd_link_hash_undefweak)
4420 && eh->elf.ref_regular)
4421 {
4422 /* Make an undefined function descriptor sym, in order to
4423 pull in an --as-needed shared lib. Archives are handled
4424 elsewhere. */
4425 fdh = make_fdh (info, eh);
4426 if (fdh == NULL)
4427 return false;
4428 }
4429
4430 if (fdh != NULL)
4431 {
4432 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4433 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4434
4435 /* Make both descriptor and entry symbol have the most
4436 constraining visibility of either symbol. */
4437 if (entry_vis < descr_vis)
4438 fdh->elf.other += entry_vis - descr_vis;
4439 else if (entry_vis > descr_vis)
4440 eh->elf.other += descr_vis - entry_vis;
4441
4442 /* Propagate reference flags from entry symbol to function
4443 descriptor symbol. */
4444 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4445 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4446 fdh->elf.ref_regular |= eh->elf.ref_regular;
4447 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4448
4449 if (!fdh->elf.forced_local
4450 && fdh->elf.dynindx == -1
4451 && fdh->elf.versioned != versioned_hidden
4452 && (bfd_link_dll (info)
4453 || fdh->elf.def_dynamic
4454 || fdh->elf.ref_dynamic)
4455 && (eh->elf.ref_regular
4456 || eh->elf.def_regular))
4457 {
4458 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4459 return false;
4460 }
4461 }
4462
4463 return true;
4464 }
4465
4466 /* Set up opd section info and abiversion for IBFD, and process list
4467 of dot-symbols we made in link_hash_newfunc. */
4468
4469 static bool
4470 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4471 {
4472 struct ppc_link_hash_table *htab;
4473 struct ppc_link_hash_entry **p, *eh;
4474 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4475
4476 if (opd != NULL && opd->size != 0)
4477 {
4478 if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4479 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4480 else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4481 BFD_FAIL ();
4482
4483 if (abiversion (ibfd) == 0)
4484 set_abiversion (ibfd, 1);
4485 else if (abiversion (ibfd) >= 2)
4486 {
4487 /* xgettext:c-format */
4488 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4489 ibfd, abiversion (ibfd));
4490 bfd_set_error (bfd_error_bad_value);
4491 return false;
4492 }
4493 }
4494
4495 if (is_ppc64_elf (info->output_bfd))
4496 {
4497 /* For input files without an explicit abiversion in e_flags
4498 we should have flagged any with symbol st_other bits set
4499 as ELFv2 and above flagged those with .opd as ELFv1.
4500 Set the output abiversion if not yet set, and for any input
4501 still ambiguous, take its abiversion from the output.
4502 Differences in ABI are reported later. */
4503 if (abiversion (info->output_bfd) == 0)
4504 set_abiversion (info->output_bfd, abiversion (ibfd));
4505 else if (abiversion (ibfd) == 0)
4506 set_abiversion (ibfd, abiversion (info->output_bfd));
4507 }
4508
4509 htab = ppc_hash_table (info);
4510 if (htab == NULL)
4511 return true;
4512
4513 if (opd != NULL && opd->size != 0
4514 && (ibfd->flags & DYNAMIC) == 0
4515 && (opd->flags & SEC_RELOC) != 0
4516 && opd->reloc_count != 0
4517 && !bfd_is_abs_section (opd->output_section)
4518 && info->gc_sections)
4519 {
4520 /* Garbage collection needs some extra help with .opd sections.
4521 We don't want to necessarily keep everything referenced by
4522 relocs in .opd, as that would keep all functions. Instead,
4523 if we reference an .opd symbol (a function descriptor), we
4524 want to keep the function code symbol's section. This is
4525 easy for global symbols, but for local syms we need to keep
4526 information about the associated function section. */
4527 bfd_size_type amt;
4528 asection **opd_sym_map;
4529 Elf_Internal_Shdr *symtab_hdr;
4530 Elf_Internal_Rela *relocs, *rel_end, *rel;
4531
4532 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4533 opd_sym_map = bfd_zalloc (ibfd, amt);
4534 if (opd_sym_map == NULL)
4535 return false;
4536 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4537 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4538 info->keep_memory);
4539 if (relocs == NULL)
4540 return false;
4541 symtab_hdr = &elf_symtab_hdr (ibfd);
4542 rel_end = relocs + opd->reloc_count - 1;
4543 for (rel = relocs; rel < rel_end; rel++)
4544 {
4545 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4546 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4547
4548 if (r_type == R_PPC64_ADDR64
4549 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4550 && r_symndx < symtab_hdr->sh_info)
4551 {
4552 Elf_Internal_Sym *isym;
4553 asection *s;
4554
4555 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4556 r_symndx);
4557 if (isym == NULL)
4558 {
4559 if (elf_section_data (opd)->relocs != relocs)
4560 free (relocs);
4561 return false;
4562 }
4563
4564 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4565 if (s != NULL && s != opd)
4566 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4567 }
4568 }
4569 if (elf_section_data (opd)->relocs != relocs)
4570 free (relocs);
4571 }
4572
4573 p = &htab->dot_syms;
4574 while ((eh = *p) != NULL)
4575 {
4576 *p = NULL;
4577 if (&eh->elf == htab->elf.hgot)
4578 ;
4579 else if (htab->elf.hgot == NULL
4580 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4581 htab->elf.hgot = &eh->elf;
4582 else if (abiversion (ibfd) <= 1)
4583 {
4584 htab->need_func_desc_adj = 1;
4585 if (!add_symbol_adjust (eh, info))
4586 return false;
4587 }
4588 p = &eh->u.next_dot_sym;
4589 }
4590 return true;
4591 }
4592
4593 /* Undo hash table changes when an --as-needed input file is determined
4594 not to be needed. */
4595
4596 static bool
4597 ppc64_elf_notice_as_needed (bfd *ibfd,
4598 struct bfd_link_info *info,
4599 enum notice_asneeded_action act)
4600 {
4601 if (act == notice_not_needed)
4602 {
4603 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4604
4605 if (htab == NULL)
4606 return false;
4607
4608 htab->dot_syms = NULL;
4609 }
4610 return _bfd_elf_notice_as_needed (ibfd, info, act);
4611 }
4612
4613 /* If --just-symbols against a final linked binary, then assume we need
4614 toc adjusting stubs when calling functions defined there. */
4615
4616 static void
4617 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4618 {
4619 if ((sec->flags & SEC_CODE) != 0
4620 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4621 && is_ppc64_elf (sec->owner))
4622 {
4623 if (abiversion (sec->owner) >= 2
4624 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4625 sec->has_toc_reloc = 1;
4626 }
4627 _bfd_elf_link_just_syms (sec, info);
4628 }
4629
4630 static struct plt_entry **
4631 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4632 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4633 {
4634 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4635 struct plt_entry **local_plt;
4636 unsigned char *local_got_tls_masks;
4637
4638 if (local_got_ents == NULL)
4639 {
4640 bfd_size_type size = symtab_hdr->sh_info;
4641
4642 size *= (sizeof (*local_got_ents)
4643 + sizeof (*local_plt)
4644 + sizeof (*local_got_tls_masks));
4645 local_got_ents = bfd_zalloc (abfd, size);
4646 if (local_got_ents == NULL)
4647 return NULL;
4648 elf_local_got_ents (abfd) = local_got_ents;
4649 }
4650
4651 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4652 {
4653 struct got_entry *ent;
4654
4655 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4656 if (ent->addend == r_addend
4657 && ent->owner == abfd
4658 && ent->tls_type == tls_type)
4659 break;
4660 if (ent == NULL)
4661 {
4662 size_t amt = sizeof (*ent);
4663 ent = bfd_alloc (abfd, amt);
4664 if (ent == NULL)
4665 return NULL;
4666 ent->next = local_got_ents[r_symndx];
4667 ent->addend = r_addend;
4668 ent->owner = abfd;
4669 ent->tls_type = tls_type;
4670 ent->is_indirect = false;
4671 ent->got.refcount = 0;
4672 local_got_ents[r_symndx] = ent;
4673 }
4674 ent->got.refcount += 1;
4675 }
4676
4677 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4678 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4679 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4680
4681 return local_plt + r_symndx;
4682 }
4683
4684 static bool
4685 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4686 {
4687 struct plt_entry *ent;
4688
4689 for (ent = *plist; ent != NULL; ent = ent->next)
4690 if (ent->addend == addend)
4691 break;
4692 if (ent == NULL)
4693 {
4694 size_t amt = sizeof (*ent);
4695 ent = bfd_alloc (abfd, amt);
4696 if (ent == NULL)
4697 return false;
4698 ent->next = *plist;
4699 ent->addend = addend;
4700 ent->plt.refcount = 0;
4701 *plist = ent;
4702 }
4703 ent->plt.refcount += 1;
4704 return true;
4705 }
4706
4707 static bool
4708 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4709 {
4710 return (r_type == R_PPC64_REL24
4711 || r_type == R_PPC64_REL24_NOTOC
4712 || r_type == R_PPC64_REL24_P9NOTOC
4713 || r_type == R_PPC64_REL14
4714 || r_type == R_PPC64_REL14_BRTAKEN
4715 || r_type == R_PPC64_REL14_BRNTAKEN
4716 || r_type == R_PPC64_ADDR24
4717 || r_type == R_PPC64_ADDR14
4718 || r_type == R_PPC64_ADDR14_BRTAKEN
4719 || r_type == R_PPC64_ADDR14_BRNTAKEN
4720 || r_type == R_PPC64_PLTCALL
4721 || r_type == R_PPC64_PLTCALL_NOTOC);
4722 }
4723
4724 /* Relocs on inline plt call sequence insns prior to the call. */
4725
4726 static bool
4727 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4728 {
4729 return (r_type == R_PPC64_PLT16_HA
4730 || r_type == R_PPC64_PLT16_HI
4731 || r_type == R_PPC64_PLT16_LO
4732 || r_type == R_PPC64_PLT16_LO_DS
4733 || r_type == R_PPC64_PLT_PCREL34
4734 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4735 || r_type == R_PPC64_PLTSEQ
4736 || r_type == R_PPC64_PLTSEQ_NOTOC);
4737 }
4738
4739 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4740 relocs, return true for those that operate on a dword. */
4741
4742 static bool
4743 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4744 {
4745 return (r_type == R_PPC64_PLT_PCREL34
4746 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4747 || r_type == R_PPC64_PLTCALL);
4748 }
4749
4750 /* The RELR encoding doesn't allow odd addresses, so RELR_ALIGN must
4751 be at least 1. R_PPC64_RELATIVE relocs require alignment of 2**3.
4752 We use 3 here to avoid complexity in relocate_section, where for a
4753 value of 1 we'd need to test for not just an output RELATIVE reloc
4754 near the call to maybe_relr but also UADDR64 and some conditions on
4755 the symbol. See PR30824. */
4756 #define RELR_ALIGN 3
4757
4758 static bool
4759 maybe_relr (enum elf_ppc64_reloc_type r_type,
4760 const Elf_Internal_Rela *rel,
4761 const asection *sec)
4762 {
4763 return ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
4764 && (rel->r_offset & ((1 << RELR_ALIGN) - 1)) == 0
4765 && sec->alignment_power >= RELR_ALIGN);
4766 }
4767
4768 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4769 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4770
4771 static bool
4772 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4773 {
4774 return offset <= sec->size && size <= sec->size - offset;
4775 }
4776
4777 /* Look through the relocs for a section during the first phase, and
4778 calculate needed space in the global offset table, procedure
4779 linkage table, and dynamic reloc sections. */
4780
4781 static bool
4782 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4783 asection *sec, const Elf_Internal_Rela *relocs)
4784 {
4785 struct ppc_link_hash_table *htab;
4786 Elf_Internal_Shdr *symtab_hdr;
4787 struct elf_link_hash_entry **sym_hashes;
4788 const Elf_Internal_Rela *rel;
4789 const Elf_Internal_Rela *rel_end;
4790 asection *sreloc;
4791 struct elf_link_hash_entry *tga, *dottga;
4792 bool is_opd;
4793
4794 if (bfd_link_relocatable (info))
4795 return true;
4796
4797 BFD_ASSERT (is_ppc64_elf (abfd));
4798
4799 htab = ppc_hash_table (info);
4800 if (htab == NULL)
4801 return false;
4802
4803 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4804 false, false, true);
4805 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4806 false, false, true);
4807 symtab_hdr = &elf_symtab_hdr (abfd);
4808 sym_hashes = elf_sym_hashes (abfd);
4809 sreloc = NULL;
4810 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4811 rel_end = relocs + sec->reloc_count;
4812 for (rel = relocs; rel < rel_end; rel++)
4813 {
4814 unsigned long r_symndx;
4815 struct elf_link_hash_entry *h;
4816 Elf_Internal_Sym *isym;
4817 enum elf_ppc64_reloc_type r_type;
4818 int tls_type;
4819 struct _ppc64_elf_section_data *ppc64_sec;
4820 struct plt_entry **ifunc, **plt_list;
4821
4822 r_symndx = ELF64_R_SYM (rel->r_info);
4823 if (r_symndx < symtab_hdr->sh_info)
4824 {
4825 h = NULL;
4826 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4827 if (isym == NULL)
4828 return false;
4829 }
4830 else
4831 {
4832 isym = NULL;
4833 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4834 h = elf_follow_link (h);
4835
4836 if (h == htab->elf.hgot)
4837 sec->has_toc_reloc = 1;
4838 }
4839
4840 r_type = ELF64_R_TYPE (rel->r_info);
4841 switch (r_type)
4842 {
4843 case R_PPC64_D34:
4844 case R_PPC64_D34_LO:
4845 case R_PPC64_D34_HI30:
4846 case R_PPC64_D34_HA30:
4847 case R_PPC64_D28:
4848 case R_PPC64_TPREL34:
4849 case R_PPC64_DTPREL34:
4850 case R_PPC64_PCREL34:
4851 case R_PPC64_GOT_PCREL34:
4852 case R_PPC64_GOT_TLSGD_PCREL34:
4853 case R_PPC64_GOT_TLSLD_PCREL34:
4854 case R_PPC64_GOT_TPREL_PCREL34:
4855 case R_PPC64_GOT_DTPREL_PCREL34:
4856 case R_PPC64_PLT_PCREL34:
4857 case R_PPC64_PLT_PCREL34_NOTOC:
4858 case R_PPC64_PCREL28:
4859 htab->has_power10_relocs = 1;
4860 break;
4861 default:
4862 break;
4863 }
4864
4865 switch (r_type)
4866 {
4867 case R_PPC64_PLT16_HA:
4868 case R_PPC64_GOT_TLSLD16_HA:
4869 case R_PPC64_GOT_TLSGD16_HA:
4870 case R_PPC64_GOT_TPREL16_HA:
4871 case R_PPC64_GOT_DTPREL16_HA:
4872 case R_PPC64_GOT16_HA:
4873 case R_PPC64_TOC16_HA:
4874 case R_PPC64_PLT16_LO:
4875 case R_PPC64_PLT16_LO_DS:
4876 case R_PPC64_GOT_TLSLD16_LO:
4877 case R_PPC64_GOT_TLSGD16_LO:
4878 case R_PPC64_GOT_TPREL16_LO_DS:
4879 case R_PPC64_GOT_DTPREL16_LO_DS:
4880 case R_PPC64_GOT16_LO:
4881 case R_PPC64_GOT16_LO_DS:
4882 case R_PPC64_TOC16_LO:
4883 case R_PPC64_TOC16_LO_DS:
4884 case R_PPC64_GOT_PCREL34:
4885 ppc64_elf_tdata (abfd)->has_optrel = 1;
4886 ppc64_elf_section_data (sec)->has_optrel = 1;
4887 break;
4888 default:
4889 break;
4890 }
4891
4892 ifunc = NULL;
4893 if (h != NULL)
4894 {
4895 if (h->type == STT_GNU_IFUNC)
4896 {
4897 h->needs_plt = 1;
4898 ifunc = &h->plt.plist;
4899 }
4900 }
4901 else
4902 {
4903 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4904 {
4905 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4906 rel->r_addend,
4907 NON_GOT | PLT_IFUNC);
4908 if (ifunc == NULL)
4909 return false;
4910 }
4911 }
4912
4913 tls_type = 0;
4914 switch (r_type)
4915 {
4916 case R_PPC64_PLTSEQ:
4917 case R_PPC64_PLTSEQ_NOTOC:
4918 /* Inline plt call code emitted by gcc doesn't support
4919 modifying the tls_index words to short-circuit
4920 __tls_get_addr calls. See PR32387. */
4921 if (h != NULL && (h == tga || h == dottga))
4922 htab->params->tls_get_addr_opt = 0;
4923 break;
4924
4925 case R_PPC64_TLSGD:
4926 case R_PPC64_TLSLD:
4927 /* These special tls relocs tie a call to __tls_get_addr with
4928 its parameter symbol. */
4929 if (h != NULL)
4930 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4931 else
4932 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4933 rel->r_addend,
4934 NON_GOT | TLS_TLS | TLS_MARK))
4935 return false;
4936 sec->has_tls_reloc = 1;
4937 break;
4938
4939 case R_PPC64_GOT_TLSLD16:
4940 case R_PPC64_GOT_TLSLD16_LO:
4941 case R_PPC64_GOT_TLSLD16_HI:
4942 case R_PPC64_GOT_TLSLD16_HA:
4943 case R_PPC64_GOT_TLSLD_PCREL34:
4944 tls_type = TLS_TLS | TLS_LD;
4945 goto dogottls;
4946
4947 case R_PPC64_GOT_TLSGD16:
4948 case R_PPC64_GOT_TLSGD16_LO:
4949 case R_PPC64_GOT_TLSGD16_HI:
4950 case R_PPC64_GOT_TLSGD16_HA:
4951 case R_PPC64_GOT_TLSGD_PCREL34:
4952 tls_type = TLS_TLS | TLS_GD;
4953 goto dogottls;
4954
4955 case R_PPC64_GOT_TPREL16_DS:
4956 case R_PPC64_GOT_TPREL16_LO_DS:
4957 case R_PPC64_GOT_TPREL16_HI:
4958 case R_PPC64_GOT_TPREL16_HA:
4959 case R_PPC64_GOT_TPREL_PCREL34:
4960 if (bfd_link_dll (info))
4961 info->flags |= DF_STATIC_TLS;
4962 tls_type = TLS_TLS | TLS_TPREL;
4963 goto dogottls;
4964
4965 case R_PPC64_GOT_DTPREL16_DS:
4966 case R_PPC64_GOT_DTPREL16_LO_DS:
4967 case R_PPC64_GOT_DTPREL16_HI:
4968 case R_PPC64_GOT_DTPREL16_HA:
4969 case R_PPC64_GOT_DTPREL_PCREL34:
4970 tls_type = TLS_TLS | TLS_DTPREL;
4971 dogottls:
4972 sec->has_tls_reloc = 1;
4973 goto dogot;
4974
4975 case R_PPC64_GOT16:
4976 case R_PPC64_GOT16_LO:
4977 case R_PPC64_GOT16_HI:
4978 case R_PPC64_GOT16_HA:
4979 case R_PPC64_GOT16_DS:
4980 case R_PPC64_GOT16_LO_DS:
4981 case R_PPC64_GOT_PCREL34:
4982 dogot:
4983 /* This symbol requires a global offset table entry. */
4984 sec->has_toc_reloc = 1;
4985 if (r_type == R_PPC64_GOT_TLSLD16
4986 || r_type == R_PPC64_GOT_TLSGD16
4987 || r_type == R_PPC64_GOT_TPREL16_DS
4988 || r_type == R_PPC64_GOT_DTPREL16_DS
4989 || r_type == R_PPC64_GOT16
4990 || r_type == R_PPC64_GOT16_DS)
4991 {
4992 htab->do_multi_toc = 1;
4993 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4994 }
4995
4996 if (ppc64_elf_tdata (abfd)->got == NULL
4997 && !create_got_section (abfd, info))
4998 return false;
4999
5000 if (h != NULL)
5001 {
5002 struct ppc_link_hash_entry *eh;
5003 struct got_entry *ent;
5004
5005 eh = ppc_elf_hash_entry (h);
5006 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5007 if (ent->addend == rel->r_addend
5008 && ent->owner == abfd
5009 && ent->tls_type == tls_type)
5010 break;
5011 if (ent == NULL)
5012 {
5013 size_t amt = sizeof (*ent);
5014 ent = bfd_alloc (abfd, amt);
5015 if (ent == NULL)
5016 return false;
5017 ent->next = eh->elf.got.glist;
5018 ent->addend = rel->r_addend;
5019 ent->owner = abfd;
5020 ent->tls_type = tls_type;
5021 ent->is_indirect = false;
5022 ent->got.refcount = 0;
5023 eh->elf.got.glist = ent;
5024 }
5025 ent->got.refcount += 1;
5026 eh->tls_mask |= tls_type;
5027 }
5028 else
5029 /* This is a global offset table entry for a local symbol. */
5030 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5031 rel->r_addend, tls_type))
5032 return false;
5033 break;
5034
5035 case R_PPC64_PLT16_HA:
5036 case R_PPC64_PLT16_HI:
5037 case R_PPC64_PLT16_LO:
5038 case R_PPC64_PLT16_LO_DS:
5039 case R_PPC64_PLT_PCREL34:
5040 case R_PPC64_PLT_PCREL34_NOTOC:
5041 case R_PPC64_PLT32:
5042 case R_PPC64_PLT64:
5043 /* This symbol requires a procedure linkage table entry. */
5044 plt_list = ifunc;
5045 if (h != NULL)
5046 {
5047 h->needs_plt = 1;
5048 if (h->root.root.string[0] == '.'
5049 && h->root.root.string[1] != '\0')
5050 ppc_elf_hash_entry (h)->is_func = 1;
5051 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5052 plt_list = &h->plt.plist;
5053 }
5054 if (plt_list == NULL)
5055 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5056 rel->r_addend,
5057 NON_GOT | PLT_KEEP);
5058 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5059 return false;
5060 break;
5061
5062 /* The following relocations don't need to propagate the
5063 relocation if linking a shared object since they are
5064 section relative. */
5065 case R_PPC64_SECTOFF:
5066 case R_PPC64_SECTOFF_LO:
5067 case R_PPC64_SECTOFF_HI:
5068 case R_PPC64_SECTOFF_HA:
5069 case R_PPC64_SECTOFF_DS:
5070 case R_PPC64_SECTOFF_LO_DS:
5071 case R_PPC64_DTPREL16:
5072 case R_PPC64_DTPREL16_LO:
5073 case R_PPC64_DTPREL16_HI:
5074 case R_PPC64_DTPREL16_HA:
5075 case R_PPC64_DTPREL16_DS:
5076 case R_PPC64_DTPREL16_LO_DS:
5077 case R_PPC64_DTPREL16_HIGH:
5078 case R_PPC64_DTPREL16_HIGHA:
5079 case R_PPC64_DTPREL16_HIGHER:
5080 case R_PPC64_DTPREL16_HIGHERA:
5081 case R_PPC64_DTPREL16_HIGHEST:
5082 case R_PPC64_DTPREL16_HIGHESTA:
5083 break;
5084
5085 /* Nor do these. */
5086 case R_PPC64_REL16:
5087 case R_PPC64_REL16_LO:
5088 case R_PPC64_REL16_HI:
5089 case R_PPC64_REL16_HA:
5090 case R_PPC64_REL16_HIGH:
5091 case R_PPC64_REL16_HIGHA:
5092 case R_PPC64_REL16_HIGHER:
5093 case R_PPC64_REL16_HIGHERA:
5094 case R_PPC64_REL16_HIGHEST:
5095 case R_PPC64_REL16_HIGHESTA:
5096 case R_PPC64_REL16_HIGHER34:
5097 case R_PPC64_REL16_HIGHERA34:
5098 case R_PPC64_REL16_HIGHEST34:
5099 case R_PPC64_REL16_HIGHESTA34:
5100 case R_PPC64_REL16DX_HA:
5101 break;
5102
5103 /* Not supported as a dynamic relocation. */
5104 case R_PPC64_ADDR64_LOCAL:
5105 if (bfd_link_pic (info))
5106 {
5107 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5108 ppc_howto_init ();
5109 /* xgettext:c-format */
5110 info->callbacks->einfo (_("%H: %s reloc unsupported "
5111 "in shared libraries and PIEs\n"),
5112 abfd, sec, rel->r_offset,
5113 ppc64_elf_howto_table[r_type]->name);
5114 bfd_set_error (bfd_error_bad_value);
5115 return false;
5116 }
5117 break;
5118
5119 case R_PPC64_TOC16:
5120 case R_PPC64_TOC16_DS:
5121 htab->do_multi_toc = 1;
5122 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5123 /* Fall through. */
5124 case R_PPC64_TOC16_LO:
5125 case R_PPC64_TOC16_HI:
5126 case R_PPC64_TOC16_HA:
5127 case R_PPC64_TOC16_LO_DS:
5128 sec->has_toc_reloc = 1;
5129 if (h != NULL && bfd_link_executable (info))
5130 {
5131 /* We may need a copy reloc. */
5132 h->non_got_ref = 1;
5133 /* Strongly prefer a copy reloc over a dynamic reloc.
5134 glibc ld.so as of 2019-08 will error out if one of
5135 these relocations is emitted. */
5136 h->needs_copy = 1;
5137 goto dodyn;
5138 }
5139 break;
5140
5141 /* Marker reloc. */
5142 case R_PPC64_ENTRY:
5143 break;
5144
5145 /* This relocation describes the C++ object vtable hierarchy.
5146 Reconstruct it for later use during GC. */
5147 case R_PPC64_GNU_VTINHERIT:
5148 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5149 return false;
5150 break;
5151
5152 /* This relocation describes which C++ vtable entries are actually
5153 used. Record for later use during GC. */
5154 case R_PPC64_GNU_VTENTRY:
5155 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5156 return false;
5157 break;
5158
5159 case R_PPC64_REL14:
5160 case R_PPC64_REL14_BRTAKEN:
5161 case R_PPC64_REL14_BRNTAKEN:
5162 {
5163 asection *dest = NULL;
5164
5165 /* Heuristic: If jumping outside our section, chances are
5166 we are going to need a stub. */
5167 if (h != NULL)
5168 {
5169 /* If the sym is weak it may be overridden later, so
5170 don't assume we know where a weak sym lives. */
5171 if (h->root.type == bfd_link_hash_defined)
5172 dest = h->root.u.def.section;
5173 }
5174 else
5175 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5176
5177 if (dest != sec)
5178 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5179 }
5180 goto rel24;
5181
5182 case R_PPC64_PLTCALL:
5183 case R_PPC64_PLTCALL_NOTOC:
5184 ppc64_elf_section_data (sec)->has_pltcall = 1;
5185 /* Fall through. */
5186
5187 case R_PPC64_REL24:
5188 case R_PPC64_REL24_NOTOC:
5189 case R_PPC64_REL24_P9NOTOC:
5190 rel24:
5191 plt_list = ifunc;
5192 if (h != NULL)
5193 {
5194 h->needs_plt = 1;
5195 if (h->root.root.string[0] == '.'
5196 && h->root.root.string[1] != '\0')
5197 ppc_elf_hash_entry (h)->is_func = 1;
5198
5199 if (h == tga || h == dottga)
5200 {
5201 sec->has_tls_reloc = 1;
5202 if (rel != relocs
5203 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5204 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5205 /* We have a new-style __tls_get_addr call with
5206 a marker reloc. */
5207 ;
5208 else
5209 /* Mark this section as having an old-style call. */
5210 sec->nomark_tls_get_addr = 1;
5211 }
5212 plt_list = &h->plt.plist;
5213 }
5214
5215 /* We may need a .plt entry if the function this reloc
5216 refers to is in a shared lib. */
5217 if (plt_list
5218 && !update_plt_info (abfd, plt_list, rel->r_addend))
5219 return false;
5220 break;
5221
5222 case R_PPC64_ADDR14:
5223 case R_PPC64_ADDR14_BRNTAKEN:
5224 case R_PPC64_ADDR14_BRTAKEN:
5225 case R_PPC64_ADDR24:
5226 goto dodyn;
5227
5228 case R_PPC64_TPREL64:
5229 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5230 if (bfd_link_dll (info))
5231 info->flags |= DF_STATIC_TLS;
5232 goto dotlstoc;
5233
5234 case R_PPC64_DTPMOD64:
5235 if (rel + 1 < rel_end
5236 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5237 && rel[1].r_offset == rel->r_offset + 8)
5238 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5239 else
5240 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5241 goto dotlstoc;
5242
5243 case R_PPC64_DTPREL64:
5244 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5245 if (rel != relocs
5246 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5247 && rel[-1].r_offset == rel->r_offset - 8)
5248 /* This is the second reloc of a dtpmod, dtprel pair.
5249 Don't mark with TLS_DTPREL. */
5250 goto dodyn;
5251
5252 dotlstoc:
5253 sec->has_tls_reloc = 1;
5254 if (h != NULL)
5255 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5256 else
5257 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5258 rel->r_addend, tls_type))
5259 return false;
5260
5261 ppc64_sec = ppc64_elf_section_data (sec);
5262 if (ppc64_sec->sec_type == sec_normal)
5263 {
5264 bfd_size_type amt;
5265
5266 /* One extra to simplify get_tls_mask. */
5267 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5268 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5269 if (ppc64_sec->u.toc.symndx == NULL)
5270 return false;
5271 amt = sec->size * sizeof (bfd_vma) / 8;
5272 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5273 if (ppc64_sec->u.toc.add == NULL)
5274 return false;
5275 ppc64_sec->sec_type = sec_toc;
5276 }
5277 if (ppc64_sec->sec_type != sec_toc
5278 || rel->r_offset % 8 != 0)
5279 {
5280 info->callbacks->einfo (_("%H: %s reloc unsupported here\n"),
5281 abfd, sec, rel->r_offset,
5282 ppc64_elf_howto_table[r_type]->name);
5283 bfd_set_error (bfd_error_bad_value);
5284 return false;
5285 }
5286 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5287 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5288
5289 /* Mark the second slot of a GD or LD entry.
5290 -1 to indicate GD and -2 to indicate LD. */
5291 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5292 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5293 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5294 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5295 goto dodyn;
5296
5297 case R_PPC64_TPREL16_HI:
5298 case R_PPC64_TPREL16_HA:
5299 case R_PPC64_TPREL16_HIGH:
5300 case R_PPC64_TPREL16_HIGHA:
5301 case R_PPC64_TPREL16_HIGHER:
5302 case R_PPC64_TPREL16_HIGHERA:
5303 case R_PPC64_TPREL16_HIGHEST:
5304 case R_PPC64_TPREL16_HIGHESTA:
5305 sec->has_tls_reloc = 1;
5306 /* Fall through. */
5307 case R_PPC64_TPREL34:
5308 case R_PPC64_TPREL16:
5309 case R_PPC64_TPREL16_DS:
5310 case R_PPC64_TPREL16_LO:
5311 case R_PPC64_TPREL16_LO_DS:
5312 if (bfd_link_dll (info))
5313 info->flags |= DF_STATIC_TLS;
5314 goto dodyn;
5315
5316 case R_PPC64_ADDR64:
5317 if (is_opd
5318 && rel + 1 < rel_end
5319 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5320 {
5321 if (h != NULL)
5322 ppc_elf_hash_entry (h)->is_func = 1;
5323 }
5324 /* Fall through. */
5325
5326 case R_PPC64_ADDR16:
5327 case R_PPC64_ADDR16_DS:
5328 case R_PPC64_ADDR16_HA:
5329 case R_PPC64_ADDR16_HI:
5330 case R_PPC64_ADDR16_HIGH:
5331 case R_PPC64_ADDR16_HIGHA:
5332 case R_PPC64_ADDR16_HIGHER:
5333 case R_PPC64_ADDR16_HIGHERA:
5334 case R_PPC64_ADDR16_HIGHEST:
5335 case R_PPC64_ADDR16_HIGHESTA:
5336 case R_PPC64_ADDR16_LO:
5337 case R_PPC64_ADDR16_LO_DS:
5338 case R_PPC64_D34:
5339 case R_PPC64_D34_LO:
5340 case R_PPC64_D34_HI30:
5341 case R_PPC64_D34_HA30:
5342 case R_PPC64_ADDR16_HIGHER34:
5343 case R_PPC64_ADDR16_HIGHERA34:
5344 case R_PPC64_ADDR16_HIGHEST34:
5345 case R_PPC64_ADDR16_HIGHESTA34:
5346 case R_PPC64_D28:
5347 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5348 && rel->r_addend == 0)
5349 {
5350 /* We may need a .plt entry if this reloc refers to a
5351 function in a shared lib. */
5352 if (!update_plt_info (abfd, &h->plt.plist, 0))
5353 return false;
5354 h->pointer_equality_needed = 1;
5355 }
5356 /* Fall through. */
5357
5358 case R_PPC64_REL30:
5359 case R_PPC64_REL32:
5360 case R_PPC64_REL64:
5361 case R_PPC64_ADDR32:
5362 case R_PPC64_UADDR16:
5363 case R_PPC64_UADDR32:
5364 case R_PPC64_UADDR64:
5365 case R_PPC64_TOC:
5366 if (h != NULL && bfd_link_executable (info))
5367 /* We may need a copy reloc. */
5368 h->non_got_ref = 1;
5369
5370 /* Don't propagate .opd relocs. */
5371 if (NO_OPD_RELOCS && is_opd)
5372 break;
5373
5374 /* Set up information for symbols that might need dynamic
5375 relocations. At this point in linking we have read all
5376 the input files and resolved most symbols, but have not
5377 yet decided whether symbols are dynamic or finalized
5378 symbol flags. In some cases we might be setting dynamic
5379 reloc info for symbols that do not end up needing such.
5380 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5381 work together with this code. */
5382 dodyn:
5383 if ((h != NULL
5384 && !SYMBOL_REFERENCES_LOCAL (info, h))
5385 || (bfd_link_pic (info)
5386 && (h != NULL
5387 ? !bfd_is_abs_symbol (&h->root)
5388 : isym->st_shndx != SHN_ABS)
5389 && must_be_dyn_reloc (info, r_type))
5390 || (!bfd_link_pic (info)
5391 && ifunc != NULL))
5392 {
5393 /* We must copy these reloc types into the output file.
5394 Create a reloc section in dynobj and make room for
5395 this reloc. */
5396 if (sreloc == NULL)
5397 {
5398 sreloc = _bfd_elf_make_dynamic_reloc_section
5399 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5400
5401 if (sreloc == NULL)
5402 return false;
5403 }
5404
5405 /* If this is a global symbol, we count the number of
5406 relocations we need for this symbol. */
5407 if (h != NULL)
5408 {
5409 struct ppc_dyn_relocs *p;
5410 struct ppc_dyn_relocs **head;
5411
5412 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5413 p = *head;
5414 if (p == NULL || p->sec != sec)
5415 {
5416 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5417 if (p == NULL)
5418 return false;
5419 p->next = *head;
5420 *head = p;
5421 p->sec = sec;
5422 p->count = 0;
5423 p->pc_count = 0;
5424 p->rel_count = 0;
5425 }
5426 p->count += 1;
5427 if (!must_be_dyn_reloc (info, r_type))
5428 p->pc_count += 1;
5429 if (maybe_relr (r_type, rel, sec))
5430 p->rel_count += 1;
5431 }
5432 else
5433 {
5434 /* Track dynamic relocs needed for local syms too. */
5435 struct ppc_local_dyn_relocs *p;
5436 struct ppc_local_dyn_relocs **head;
5437 bool is_ifunc;
5438 asection *s;
5439 void *vpp;
5440
5441 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5442 if (s == NULL)
5443 s = sec;
5444
5445 vpp = &elf_section_data (s)->local_dynrel;
5446 head = (struct ppc_local_dyn_relocs **) vpp;
5447 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5448 p = *head;
5449 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5450 p = p->next;
5451 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5452 {
5453 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5454 if (p == NULL)
5455 return false;
5456 p->next = *head;
5457 *head = p;
5458 p->sec = sec;
5459 p->count = 0;
5460 p->rel_count = 0;
5461 p->ifunc = is_ifunc;
5462 }
5463 p->count += 1;
5464 if (maybe_relr (r_type, rel, sec))
5465 p->rel_count += 1;
5466 }
5467 }
5468 break;
5469
5470 default:
5471 break;
5472 }
5473 }
5474
5475 return true;
5476 }
5477
5478 /* Merge backend specific data from an object file to the output
5479 object file when linking. */
5480
5481 static bool
5482 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5483 {
5484 bfd *obfd = info->output_bfd;
5485 unsigned long iflags, oflags;
5486
5487 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5488 return true;
5489
5490 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5491 return true;
5492
5493 if (!_bfd_generic_verify_endian_match (ibfd, info))
5494 return false;
5495
5496 iflags = elf_elfheader (ibfd)->e_flags;
5497 oflags = elf_elfheader (obfd)->e_flags;
5498
5499 if (iflags & ~EF_PPC64_ABI)
5500 {
5501 _bfd_error_handler
5502 /* xgettext:c-format */
5503 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5504 bfd_set_error (bfd_error_bad_value);
5505 return false;
5506 }
5507 else if (iflags != oflags && iflags != 0)
5508 {
5509 _bfd_error_handler
5510 /* xgettext:c-format */
5511 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5512 ibfd, iflags, oflags);
5513 bfd_set_error (bfd_error_bad_value);
5514 return false;
5515 }
5516
5517 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5518 return false;
5519
5520 /* Merge Tag_compatibility attributes and any common GNU ones. */
5521 return _bfd_elf_merge_object_attributes (ibfd, info);
5522 }
5523
5524 static bool
5525 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5526 {
5527 /* Print normal ELF private data. */
5528 _bfd_elf_print_private_bfd_data (abfd, ptr);
5529
5530 if (elf_elfheader (abfd)->e_flags != 0)
5531 {
5532 FILE *file = ptr;
5533
5534 fprintf (file, _("private flags = 0x%lx:"),
5535 elf_elfheader (abfd)->e_flags);
5536
5537 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5538 fprintf (file, _(" [abiv%ld]"),
5539 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5540 fputc ('\n', file);
5541 }
5542
5543 return true;
5544 }
5545
5546 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5547 of the code entry point, and its section, which must be in the same
5548 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5549
5550 static bfd_vma
5551 opd_entry_value (asection *opd_sec,
5552 bfd_vma offset,
5553 asection **code_sec,
5554 bfd_vma *code_off,
5555 bool in_code_sec)
5556 {
5557 bfd *opd_bfd = opd_sec->owner;
5558 Elf_Internal_Rela *relocs;
5559 Elf_Internal_Rela *lo, *hi, *look;
5560 bfd_vma val;
5561
5562 if (!is_ppc64_elf (opd_bfd))
5563 return (bfd_vma) -1;
5564
5565 if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5566 ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5567 else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5568 return (bfd_vma) -1;
5569
5570 /* No relocs implies we are linking a --just-symbols object, or looking
5571 at a final linked executable with addr2line or somesuch. */
5572 if (opd_sec->reloc_count == 0)
5573 {
5574 bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
5575
5576 if (contents == NULL)
5577 {
5578 if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5579 || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5580 return (bfd_vma) -1;
5581 ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
5582 }
5583
5584 /* PR 17512: file: 64b9dfbb. */
5585 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5586 return (bfd_vma) -1;
5587
5588 val = bfd_get_64 (opd_bfd, contents + offset);
5589 if (code_sec != NULL)
5590 {
5591 asection *sec, *likely = NULL;
5592
5593 if (in_code_sec)
5594 {
5595 sec = *code_sec;
5596 if (sec->vma <= val
5597 && val < sec->vma + sec->size)
5598 likely = sec;
5599 else
5600 val = -1;
5601 }
5602 else
5603 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5604 if (sec->vma <= val
5605 && (sec->flags & SEC_LOAD) != 0
5606 && (sec->flags & SEC_ALLOC) != 0)
5607 likely = sec;
5608 if (likely != NULL)
5609 {
5610 *code_sec = likely;
5611 if (code_off != NULL)
5612 *code_off = val - likely->vma;
5613 }
5614 }
5615 return val;
5616 }
5617
5618 relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
5619 if (relocs == NULL)
5620 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5621 /* PR 17512: file: df8e1fd6. */
5622 if (relocs == NULL)
5623 return (bfd_vma) -1;
5624
5625 /* Go find the opd reloc at the sym address. */
5626 lo = relocs;
5627 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5628 val = (bfd_vma) -1;
5629 while (lo < hi)
5630 {
5631 look = lo + (hi - lo) / 2;
5632 if (look->r_offset < offset)
5633 lo = look + 1;
5634 else if (look->r_offset > offset)
5635 hi = look;
5636 else
5637 {
5638 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5639
5640 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5641 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5642 {
5643 unsigned long symndx = ELF64_R_SYM (look->r_info);
5644 asection *sec = NULL;
5645
5646 if (symndx >= symtab_hdr->sh_info
5647 && elf_sym_hashes (opd_bfd) != NULL)
5648 {
5649 struct elf_link_hash_entry **sym_hashes;
5650 struct elf_link_hash_entry *rh;
5651
5652 sym_hashes = elf_sym_hashes (opd_bfd);
5653 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5654 if (rh != NULL)
5655 {
5656 rh = elf_follow_link (rh);
5657 if (rh->root.type != bfd_link_hash_defined
5658 && rh->root.type != bfd_link_hash_defweak)
5659 break;
5660 if (rh->root.u.def.section->owner == opd_bfd)
5661 {
5662 val = rh->root.u.def.value;
5663 sec = rh->root.u.def.section;
5664 }
5665 }
5666 }
5667
5668 if (sec == NULL)
5669 {
5670 Elf_Internal_Sym *sym;
5671
5672 if (symndx < symtab_hdr->sh_info)
5673 {
5674 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5675 if (sym == NULL)
5676 {
5677 size_t symcnt = symtab_hdr->sh_info;
5678 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5679 symcnt, 0,
5680 NULL, NULL, NULL);
5681 if (sym == NULL)
5682 break;
5683 symtab_hdr->contents = (bfd_byte *) sym;
5684 }
5685 sym += symndx;
5686 }
5687 else
5688 {
5689 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5690 1, symndx,
5691 NULL, NULL, NULL);
5692 if (sym == NULL)
5693 break;
5694 }
5695 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5696 if (sec == NULL)
5697 break;
5698 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5699 val = sym->st_value;
5700 }
5701
5702 val += look->r_addend;
5703 if (code_off != NULL)
5704 *code_off = val;
5705 if (code_sec != NULL)
5706 {
5707 if (in_code_sec && *code_sec != sec)
5708 return -1;
5709 else
5710 *code_sec = sec;
5711 }
5712 if (sec->output_section != NULL)
5713 val += sec->output_section->vma + sec->output_offset;
5714 }
5715 break;
5716 }
5717 }
5718
5719 return val;
5720 }
5721
5722 /* If the ELF symbol SYM might be a function in SEC, return the
5723 function size and set *CODE_OFF to the function's entry point,
5724 otherwise return zero. */
5725
5726 static bfd_size_type
5727 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5728 bfd_vma *code_off)
5729 {
5730 bfd_size_type size;
5731 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5732
5733 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5734 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5735 return 0;
5736
5737 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5738
5739 /* In theory we should check that the symbol's type satisfies
5740 _bfd_elf_is_function_type(), but there are some function-like
5741 symbols which would fail this test. (eg _start). Instead
5742 we check for hidden, local, notype symbols with zero size.
5743 This type of symbol is generated by the annobin plugin for gcc
5744 and clang, and should not be considered to be a function symbol. */
5745 if (size == 0
5746 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5747 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5748 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5749 return 0;
5750
5751 if (strcmp (sym->section->name, ".opd") == 0)
5752 {
5753 struct _opd_sec_data *opd = get_opd_info (sym->section);
5754 bfd_vma symval = sym->value;
5755
5756 if (opd != NULL
5757 && opd->adjust != NULL
5758 && elf_section_data (sym->section)->relocs != NULL)
5759 {
5760 /* opd_entry_value will use cached relocs that have been
5761 adjusted, but with raw symbols. That means both local
5762 and global symbols need adjusting. */
5763 long adjust = opd->adjust[OPD_NDX (symval)];
5764 if (adjust == -1)
5765 return 0;
5766 symval += adjust;
5767 }
5768
5769 if (opd_entry_value (sym->section, symval,
5770 &sec, code_off, true) == (bfd_vma) -1)
5771 return 0;
5772 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5773 symbol. This size has nothing to do with the code size of the
5774 function, which is what we're supposed to return, but the
5775 code size isn't available without looking up the dot-sym.
5776 However, doing that would be a waste of time particularly
5777 since elf_find_function will look at the dot-sym anyway.
5778 Now, elf_find_function will keep the largest size of any
5779 function sym found at the code address of interest, so return
5780 1 here to avoid it incorrectly caching a larger function size
5781 for a small function. This does mean we return the wrong
5782 size for a new-ABI function of size 24, but all that does is
5783 disable caching for such functions. */
5784 if (size == 24)
5785 size = 1;
5786 }
5787 else
5788 {
5789 if (sym->section != sec)
5790 return 0;
5791 *code_off = sym->value;
5792 }
5793
5794 /* Do not return 0 for the function's size. */
5795 return size ? size : 1;
5796 }
5797
5798 /* Return true if symbol is a strong function defined in an ELFv2
5799 object with st_other localentry bits of zero, ie. its local entry
5800 point coincides with its global entry point. */
5801
5802 static bool
5803 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5804 {
5805 return (h != NULL
5806 && h->type == STT_FUNC
5807 && h->root.type == bfd_link_hash_defined
5808 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5809 && !ppc_elf_hash_entry (h)->non_zero_localentry
5810 && is_ppc64_elf (h->root.u.def.section->owner)
5811 && abiversion (h->root.u.def.section->owner) >= 2);
5812 }
5813
5814 /* Return true if symbol is defined in a regular object file. */
5815
5816 static bool
5817 is_static_defined (struct elf_link_hash_entry *h)
5818 {
5819 return ((h->root.type == bfd_link_hash_defined
5820 || h->root.type == bfd_link_hash_defweak)
5821 && h->root.u.def.section != NULL
5822 && h->root.u.def.section->output_section != NULL);
5823 }
5824
5825 /* If FDH is a function descriptor symbol, return the associated code
5826 entry symbol if it is defined. Return NULL otherwise. */
5827
5828 static struct ppc_link_hash_entry *
5829 defined_code_entry (struct ppc_link_hash_entry *fdh)
5830 {
5831 if (fdh->is_func_descriptor)
5832 {
5833 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5834 if (fh->elf.root.type == bfd_link_hash_defined
5835 || fh->elf.root.type == bfd_link_hash_defweak)
5836 return fh;
5837 }
5838 return NULL;
5839 }
5840
5841 /* If FH is a function code entry symbol, return the associated
5842 function descriptor symbol if it is defined. Return NULL otherwise. */
5843
5844 static struct ppc_link_hash_entry *
5845 defined_func_desc (struct ppc_link_hash_entry *fh)
5846 {
5847 if (fh->oh != NULL
5848 && fh->oh->is_func_descriptor)
5849 {
5850 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5851 if (fdh->elf.root.type == bfd_link_hash_defined
5852 || fdh->elf.root.type == bfd_link_hash_defweak)
5853 return fdh;
5854 }
5855 return NULL;
5856 }
5857
5858 /* Given H is a symbol that satisfies is_static_defined, return the
5859 value in the output file. */
5860
5861 static bfd_vma
5862 defined_sym_val (struct elf_link_hash_entry *h)
5863 {
5864 return (h->root.u.def.section->output_section->vma
5865 + h->root.u.def.section->output_offset
5866 + h->root.u.def.value);
5867 }
5868
5869 /* Return true if H matches __tls_get_addr or one of its variants. */
5870
5871 static bool
5872 is_tls_get_addr (struct elf_link_hash_entry *h,
5873 struct ppc_link_hash_table *htab)
5874 {
5875 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5876 || h == elf_hash_entry (htab->tga_desc_fd)
5877 || h == elf_hash_entry (htab->tls_get_addr)
5878 || h == elf_hash_entry (htab->tga_desc));
5879 }
5880
5881 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5882
5883 /* Garbage collect sections, after first dealing with dot-symbols. */
5884
5885 static bool
5886 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5887 {
5888 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5889
5890 if (htab != NULL && htab->need_func_desc_adj)
5891 {
5892 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5893 htab->need_func_desc_adj = 0;
5894 }
5895 return bfd_elf_gc_sections (abfd, info);
5896 }
5897
5898 /* Mark all our entry sym sections, both opd and code section. */
5899
5900 static void
5901 ppc64_elf_gc_keep (struct bfd_link_info *info)
5902 {
5903 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5904 struct bfd_sym_chain *sym;
5905
5906 if (htab == NULL)
5907 return;
5908
5909 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5910 {
5911 struct ppc_link_hash_entry *eh, *fh;
5912 asection *sec;
5913
5914 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5915 false, false, true));
5916 if (eh == NULL)
5917 continue;
5918 if (eh->elf.root.type != bfd_link_hash_defined
5919 && eh->elf.root.type != bfd_link_hash_defweak)
5920 continue;
5921
5922 fh = defined_code_entry (eh);
5923 if (fh != NULL)
5924 {
5925 sec = fh->elf.root.u.def.section;
5926 sec->flags |= SEC_KEEP;
5927 }
5928 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5929 && opd_entry_value (eh->elf.root.u.def.section,
5930 eh->elf.root.u.def.value,
5931 &sec, NULL, false) != (bfd_vma) -1)
5932 sec->flags |= SEC_KEEP;
5933
5934 sec = eh->elf.root.u.def.section;
5935 sec->flags |= SEC_KEEP;
5936 }
5937 }
5938
5939 /* Mark sections containing dynamically referenced symbols. When
5940 building shared libraries, we must assume that any visible symbol is
5941 referenced. */
5942
5943 static bool
5944 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5945 {
5946 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5947 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5948 struct ppc_link_hash_entry *fdh;
5949 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5950
5951 /* Dynamic linking info is on the func descriptor sym. */
5952 fdh = defined_func_desc (eh);
5953 if (fdh != NULL)
5954 eh = fdh;
5955
5956 if ((eh->elf.root.type == bfd_link_hash_defined
5957 || eh->elf.root.type == bfd_link_hash_defweak)
5958 && (!eh->elf.start_stop
5959 || eh->elf.root.ldscript_def
5960 || !info->start_stop_gc)
5961 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5962 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5963 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5964 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5965 && (!bfd_link_executable (info)
5966 || info->gc_keep_exported
5967 || info->export_dynamic
5968 || (eh->elf.dynamic
5969 && d != NULL
5970 && (*d->match) (&d->head, NULL,
5971 eh->elf.root.root.string)))
5972 && (eh->elf.versioned >= versioned
5973 || !bfd_hide_sym_by_version (info->version_info,
5974 eh->elf.root.root.string)))))
5975 {
5976 asection *code_sec;
5977 struct ppc_link_hash_entry *fh;
5978
5979 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5980
5981 /* Function descriptor syms cause the associated
5982 function code sym section to be marked. */
5983 fh = defined_code_entry (eh);
5984 if (fh != NULL)
5985 {
5986 code_sec = fh->elf.root.u.def.section;
5987 code_sec->flags |= SEC_KEEP;
5988 }
5989 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5990 && opd_entry_value (eh->elf.root.u.def.section,
5991 eh->elf.root.u.def.value,
5992 &code_sec, NULL, false) != (bfd_vma) -1)
5993 code_sec->flags |= SEC_KEEP;
5994 }
5995
5996 return true;
5997 }
5998
5999 /* Return the section that should be marked against GC for a given
6000 relocation. */
6001
6002 static asection *
6003 ppc64_elf_gc_mark_hook (asection *sec,
6004 struct bfd_link_info *info,
6005 Elf_Internal_Rela *rel,
6006 struct elf_link_hash_entry *h,
6007 Elf_Internal_Sym *sym)
6008 {
6009 asection *rsec;
6010
6011 /* Syms return NULL if we're marking .opd, so we avoid marking all
6012 function sections, as all functions are referenced in .opd. */
6013 rsec = NULL;
6014 if (get_opd_info (sec) != NULL)
6015 return rsec;
6016
6017 if (h != NULL)
6018 {
6019 enum elf_ppc64_reloc_type r_type;
6020 struct ppc_link_hash_entry *eh, *fh, *fdh;
6021
6022 r_type = ELF64_R_TYPE (rel->r_info);
6023 switch (r_type)
6024 {
6025 case R_PPC64_GNU_VTINHERIT:
6026 case R_PPC64_GNU_VTENTRY:
6027 break;
6028
6029 default:
6030 switch (h->root.type)
6031 {
6032 case bfd_link_hash_defined:
6033 case bfd_link_hash_defweak:
6034 eh = ppc_elf_hash_entry (h);
6035 fdh = defined_func_desc (eh);
6036 if (fdh != NULL)
6037 {
6038 /* -mcall-aixdesc code references the dot-symbol on
6039 a call reloc. Mark the function descriptor too
6040 against garbage collection. */
6041 fdh->elf.mark = 1;
6042 if (fdh->elf.is_weakalias)
6043 weakdef (&fdh->elf)->mark = 1;
6044 eh = fdh;
6045 }
6046
6047 /* Function descriptor syms cause the associated
6048 function code sym section to be marked. */
6049 fh = defined_code_entry (eh);
6050 if (fh != NULL)
6051 {
6052 /* They also mark their opd section. */
6053 eh->elf.root.u.def.section->gc_mark = 1;
6054
6055 rsec = fh->elf.root.u.def.section;
6056 }
6057 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6058 && opd_entry_value (eh->elf.root.u.def.section,
6059 eh->elf.root.u.def.value,
6060 &rsec, NULL, false) != (bfd_vma) -1)
6061 eh->elf.root.u.def.section->gc_mark = 1;
6062 else
6063 rsec = h->root.u.def.section;
6064 break;
6065
6066 case bfd_link_hash_common:
6067 rsec = h->root.u.c.p->section;
6068 break;
6069
6070 default:
6071 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6072 }
6073 }
6074 }
6075 else
6076 {
6077 struct _opd_sec_data *opd;
6078
6079 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6080 opd = get_opd_info (rsec);
6081 if (opd != NULL && opd->func_sec != NULL)
6082 {
6083 rsec->gc_mark = 1;
6084
6085 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6086 }
6087 }
6088
6089 return rsec;
6090 }
6091
6092 /* The maximum size of .sfpr. */
6093 #define SFPR_MAX (218*4)
6094
6095 struct sfpr_def_parms
6096 {
6097 const char name[12];
6098 unsigned char lo, hi;
6099 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6100 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6101 };
6102
6103 /* Auto-generate _save*, _rest* functions in .sfpr.
6104 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6105 instead. */
6106
6107 static bool
6108 sfpr_define (struct bfd_link_info *info,
6109 const struct sfpr_def_parms *parm,
6110 asection *stub_sec)
6111 {
6112 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6113 unsigned int i;
6114 size_t len = strlen (parm->name);
6115 bool writing = false;
6116 char sym[16];
6117
6118 if (htab == NULL)
6119 return false;
6120
6121 memcpy (sym, parm->name, len);
6122 sym[len + 2] = 0;
6123
6124 for (i = parm->lo; i <= parm->hi; i++)
6125 {
6126 struct ppc_link_hash_entry *h;
6127
6128 sym[len + 0] = i / 10 + '0';
6129 sym[len + 1] = i % 10 + '0';
6130 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6131 writing, true, true));
6132 if (stub_sec != NULL)
6133 {
6134 if (h != NULL
6135 && h->elf.root.type == bfd_link_hash_defined
6136 && h->elf.root.u.def.section == htab->sfpr)
6137 {
6138 struct elf_link_hash_entry *s;
6139 char buf[32];
6140 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6141 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6142 if (s == NULL)
6143 return false;
6144 if (s->root.type == bfd_link_hash_new)
6145 {
6146 s->root.type = bfd_link_hash_defined;
6147 s->root.u.def.section = stub_sec;
6148 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6149 + h->elf.root.u.def.value);
6150 s->ref_regular = 1;
6151 s->def_regular = 1;
6152 s->ref_regular_nonweak = 1;
6153 s->forced_local = 1;
6154 s->non_elf = 0;
6155 s->root.linker_def = 1;
6156 }
6157 }
6158 continue;
6159 }
6160 if (h != NULL)
6161 {
6162 h->save_res = 1;
6163 if (!h->elf.def_regular)
6164 {
6165 h->elf.root.type = bfd_link_hash_defined;
6166 h->elf.root.u.def.section = htab->sfpr;
6167 h->elf.root.u.def.value = htab->sfpr->size;
6168 h->elf.type = STT_FUNC;
6169 h->elf.def_regular = 1;
6170 h->elf.non_elf = 0;
6171 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6172 writing = true;
6173 if (htab->sfpr->contents == NULL)
6174 {
6175 htab->sfpr->contents
6176 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6177 if (htab->sfpr->contents == NULL)
6178 return false;
6179 }
6180 }
6181 }
6182 if (writing)
6183 {
6184 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6185 if (i != parm->hi)
6186 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6187 else
6188 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6189 htab->sfpr->size = p - htab->sfpr->contents;
6190 }
6191 }
6192
6193 return true;
6194 }
6195
6196 static bfd_byte *
6197 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6198 {
6199 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6200 return p + 4;
6201 }
6202
6203 static bfd_byte *
6204 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6205 {
6206 p = savegpr0 (abfd, p, r);
6207 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6208 p = p + 4;
6209 bfd_put_32 (abfd, BLR, p);
6210 return p + 4;
6211 }
6212
6213 static bfd_byte *
6214 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6215 {
6216 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6217 return p + 4;
6218 }
6219
6220 static bfd_byte *
6221 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6222 {
6223 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6224 p = p + 4;
6225 p = restgpr0 (abfd, p, r);
6226 bfd_put_32 (abfd, MTLR_R0, p);
6227 p = p + 4;
6228 if (r == 29)
6229 {
6230 p = restgpr0 (abfd, p, 30);
6231 p = restgpr0 (abfd, p, 31);
6232 }
6233 bfd_put_32 (abfd, BLR, p);
6234 return p + 4;
6235 }
6236
6237 static bfd_byte *
6238 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6239 {
6240 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6241 return p + 4;
6242 }
6243
6244 static bfd_byte *
6245 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6246 {
6247 p = savegpr1 (abfd, p, r);
6248 bfd_put_32 (abfd, BLR, p);
6249 return p + 4;
6250 }
6251
6252 static bfd_byte *
6253 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6254 {
6255 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6256 return p + 4;
6257 }
6258
6259 static bfd_byte *
6260 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6261 {
6262 p = restgpr1 (abfd, p, r);
6263 bfd_put_32 (abfd, BLR, p);
6264 return p + 4;
6265 }
6266
6267 static bfd_byte *
6268 savefpr (bfd *abfd, bfd_byte *p, int r)
6269 {
6270 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6271 return p + 4;
6272 }
6273
6274 static bfd_byte *
6275 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6276 {
6277 p = savefpr (abfd, p, r);
6278 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6279 p = p + 4;
6280 bfd_put_32 (abfd, BLR, p);
6281 return p + 4;
6282 }
6283
6284 static bfd_byte *
6285 restfpr (bfd *abfd, bfd_byte *p, int r)
6286 {
6287 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6288 return p + 4;
6289 }
6290
6291 static bfd_byte *
6292 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6293 {
6294 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6295 p = p + 4;
6296 p = restfpr (abfd, p, r);
6297 bfd_put_32 (abfd, MTLR_R0, p);
6298 p = p + 4;
6299 if (r == 29)
6300 {
6301 p = restfpr (abfd, p, 30);
6302 p = restfpr (abfd, p, 31);
6303 }
6304 bfd_put_32 (abfd, BLR, p);
6305 return p + 4;
6306 }
6307
6308 static bfd_byte *
6309 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6310 {
6311 p = savefpr (abfd, p, r);
6312 bfd_put_32 (abfd, BLR, p);
6313 return p + 4;
6314 }
6315
6316 static bfd_byte *
6317 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6318 {
6319 p = restfpr (abfd, p, r);
6320 bfd_put_32 (abfd, BLR, p);
6321 return p + 4;
6322 }
6323
6324 static bfd_byte *
6325 savevr (bfd *abfd, bfd_byte *p, int r)
6326 {
6327 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6328 p = p + 4;
6329 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6330 return p + 4;
6331 }
6332
6333 static bfd_byte *
6334 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6335 {
6336 p = savevr (abfd, p, r);
6337 bfd_put_32 (abfd, BLR, p);
6338 return p + 4;
6339 }
6340
6341 static bfd_byte *
6342 restvr (bfd *abfd, bfd_byte *p, int r)
6343 {
6344 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6345 p = p + 4;
6346 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6347 return p + 4;
6348 }
6349
6350 static bfd_byte *
6351 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6352 {
6353 p = restvr (abfd, p, r);
6354 bfd_put_32 (abfd, BLR, p);
6355 return p + 4;
6356 }
6357
6358 #define STDU_R1_0R1 0xf8210001
6359 #define ADDI_R1_R1 0x38210000
6360
6361 /* Emit prologue of wrapper preserving regs around a call to
6362 __tls_get_addr_opt. */
6363
6364 static bfd_byte *
6365 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6366 {
6367 unsigned int i;
6368
6369 bfd_put_32 (obfd, MFLR_R0, p);
6370 p += 4;
6371 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6372 p += 4;
6373
6374 if (htab->opd_abi)
6375 {
6376 for (i = 4; i < 12; i++)
6377 {
6378 bfd_put_32 (obfd,
6379 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6380 p += 4;
6381 }
6382 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6383 p += 4;
6384 }
6385 else
6386 {
6387 for (i = 4; i < 12; i++)
6388 {
6389 bfd_put_32 (obfd,
6390 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6391 p += 4;
6392 }
6393 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6394 p += 4;
6395 }
6396 return p;
6397 }
6398
6399 /* Emit epilogue of wrapper preserving regs around a call to
6400 __tls_get_addr_opt. */
6401
6402 static bfd_byte *
6403 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6404 {
6405 unsigned int i;
6406
6407 if (htab->opd_abi)
6408 {
6409 for (i = 4; i < 12; i++)
6410 {
6411 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6412 p += 4;
6413 }
6414 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6415 p += 4;
6416 }
6417 else
6418 {
6419 for (i = 4; i < 12; i++)
6420 {
6421 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6422 p += 4;
6423 }
6424 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6425 p += 4;
6426 }
6427 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6428 p += 4;
6429 bfd_put_32 (obfd, MTLR_R0, p);
6430 p += 4;
6431 bfd_put_32 (obfd, BLR, p);
6432 p += 4;
6433 return p;
6434 }
6435
6436 /* Called via elf_link_hash_traverse to transfer dynamic linking
6437 information on function code symbol entries to their corresponding
6438 function descriptor symbol entries. Must not be called twice for
6439 any given code symbol. */
6440
6441 static bool
6442 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6443 {
6444 struct bfd_link_info *info;
6445 struct ppc_link_hash_table *htab;
6446 struct ppc_link_hash_entry *fh;
6447 struct ppc_link_hash_entry *fdh;
6448 bool force_local;
6449
6450 fh = ppc_elf_hash_entry (h);
6451 if (fh->elf.root.type == bfd_link_hash_indirect)
6452 return true;
6453
6454 if (!fh->is_func)
6455 return true;
6456
6457 if (fh->elf.root.root.string[0] != '.'
6458 || fh->elf.root.root.string[1] == '\0')
6459 return true;
6460
6461 info = inf;
6462 htab = ppc_hash_table (info);
6463 if (htab == NULL)
6464 return false;
6465
6466 /* Find the corresponding function descriptor symbol. */
6467 fdh = lookup_fdh (fh, htab);
6468
6469 /* Resolve undefined references to dot-symbols as the value
6470 in the function descriptor, if we have one in a regular object.
6471 This is to satisfy cases like ".quad .foo". Calls to functions
6472 in dynamic objects are handled elsewhere. */
6473 if ((fh->elf.root.type == bfd_link_hash_undefined
6474 || fh->elf.root.type == bfd_link_hash_undefweak)
6475 && (fdh->elf.root.type == bfd_link_hash_defined
6476 || fdh->elf.root.type == bfd_link_hash_defweak)
6477 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6478 && opd_entry_value (fdh->elf.root.u.def.section,
6479 fdh->elf.root.u.def.value,
6480 &fh->elf.root.u.def.section,
6481 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6482 {
6483 fh->elf.root.type = fdh->elf.root.type;
6484 fh->elf.forced_local = 1;
6485 fh->elf.def_regular = fdh->elf.def_regular;
6486 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6487 }
6488
6489 if (!fh->elf.dynamic)
6490 {
6491 struct plt_entry *ent;
6492
6493 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6494 if (ent->plt.refcount > 0)
6495 break;
6496 if (ent == NULL)
6497 {
6498 if (fdh != NULL && fdh->fake)
6499 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6500 return true;
6501 }
6502 }
6503
6504 /* Create a descriptor as undefined if necessary. */
6505 if (fdh == NULL
6506 && !bfd_link_executable (info)
6507 && (fh->elf.root.type == bfd_link_hash_undefined
6508 || fh->elf.root.type == bfd_link_hash_undefweak))
6509 {
6510 fdh = make_fdh (info, fh);
6511 if (fdh == NULL)
6512 return false;
6513 }
6514
6515 /* We can't support overriding of symbols on a fake descriptor. */
6516 if (fdh != NULL
6517 && fdh->fake
6518 && (fh->elf.root.type == bfd_link_hash_defined
6519 || fh->elf.root.type == bfd_link_hash_defweak))
6520 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6521
6522 /* Transfer dynamic linking information to the function descriptor. */
6523 if (fdh != NULL)
6524 {
6525 fdh->elf.ref_regular |= fh->elf.ref_regular;
6526 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6527 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6528 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6529 fdh->elf.dynamic |= fh->elf.dynamic;
6530 fdh->elf.needs_plt |= (fh->elf.needs_plt
6531 || fh->elf.type == STT_FUNC
6532 || fh->elf.type == STT_GNU_IFUNC);
6533 move_plt_plist (fh, fdh);
6534
6535 if (!fdh->elf.forced_local
6536 && fh->elf.dynindx != -1)
6537 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6538 return false;
6539 }
6540
6541 /* Now that the info is on the function descriptor, clear the
6542 function code sym info. Any function code syms for which we
6543 don't have a definition in a regular file, we force local.
6544 This prevents a shared library from exporting syms that have
6545 been imported from another library. Function code syms that
6546 are really in the library we must leave global to prevent the
6547 linker dragging in a definition from a static library. */
6548 force_local = (!fh->elf.def_regular
6549 || fdh == NULL
6550 || !fdh->elf.def_regular
6551 || fdh->elf.forced_local);
6552 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6553
6554 return true;
6555 }
6556
6557 static const struct sfpr_def_parms save_res_funcs[] =
6558 {
6559 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6560 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6561 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6562 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6563 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6564 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6565 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6566 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6567 { "._savef", 14, 31, savefpr, savefpr1_tail },
6568 { "._restf", 14, 31, restfpr, restfpr1_tail },
6569 { "_savevr_", 20, 31, savevr, savevr_tail },
6570 { "_restvr_", 20, 31, restvr, restvr_tail }
6571 };
6572
6573 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6574 this hook to a) run the edit functions in this file, b) provide
6575 some gcc support functions, and c) transfer dynamic linking
6576 information gathered so far on function code symbol entries, to
6577 their corresponding function descriptor symbol entries. */
6578
6579 static bool
6580 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6581 {
6582 struct ppc_link_hash_table *htab;
6583
6584 htab = ppc_hash_table (info);
6585 if (htab == NULL)
6586 return false;
6587
6588 /* Call back into the linker, which then runs the edit functions. */
6589 htab->params->edit ();
6590
6591 /* Provide any missing _save* and _rest* functions. */
6592 if (htab->sfpr != NULL)
6593 {
6594 unsigned int i;
6595
6596 htab->sfpr->size = 0;
6597 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6598 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6599 return false;
6600 if (htab->sfpr->size == 0)
6601 htab->sfpr->flags |= SEC_EXCLUDE;
6602 }
6603
6604 if (bfd_link_relocatable (info))
6605 return true;
6606
6607 if (htab->elf.hgot != NULL)
6608 {
6609 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6610 /* Make .TOC. defined so as to prevent it being made dynamic.
6611 The wrong value here is fixed later in ppc64_elf_set_toc. */
6612 if (!htab->elf.hgot->def_regular
6613 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6614 {
6615 htab->elf.hgot->root.type = bfd_link_hash_defined;
6616 htab->elf.hgot->root.u.def.value = 0;
6617 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6618 htab->elf.hgot->def_regular = 1;
6619 htab->elf.hgot->root.linker_def = 1;
6620 }
6621 htab->elf.hgot->type = STT_OBJECT;
6622 htab->elf.hgot->other
6623 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6624 }
6625
6626 return true;
6627 }
6628
6629 /* Return true if we have dynamic relocs against H or any of its weak
6630 aliases, that apply to read-only sections. Cannot be used after
6631 size_dynamic_sections. */
6632
6633 static bool
6634 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6635 {
6636 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6637 do
6638 {
6639 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6640 return true;
6641 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6642 }
6643 while (eh != NULL && &eh->elf != h);
6644
6645 return false;
6646 }
6647
6648 /* Return whether EH has pc-relative dynamic relocs. */
6649
6650 static bool
6651 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6652 {
6653 struct ppc_dyn_relocs *p;
6654
6655 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6656 if (p->pc_count != 0)
6657 return true;
6658 return false;
6659 }
6660
6661 /* Return true if a global entry stub will be created for H. Valid
6662 for ELFv2 before plt entries have been allocated. */
6663
6664 static bool
6665 global_entry_stub (struct elf_link_hash_entry *h)
6666 {
6667 struct plt_entry *pent;
6668
6669 if (!h->pointer_equality_needed
6670 || h->def_regular)
6671 return false;
6672
6673 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6674 if (pent->plt.refcount > 0
6675 && pent->addend == 0)
6676 return true;
6677
6678 return false;
6679 }
6680
6681 /* Adjust a symbol defined by a dynamic object and referenced by a
6682 regular object. The current definition is in some section of the
6683 dynamic object, but we're not including those sections. We have to
6684 change the definition to something the rest of the link can
6685 understand. */
6686
6687 static bool
6688 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6689 struct elf_link_hash_entry *h)
6690 {
6691 struct ppc_link_hash_table *htab;
6692 asection *s, *srel;
6693
6694 htab = ppc_hash_table (info);
6695 if (htab == NULL)
6696 return false;
6697
6698 /* Deal with function syms. */
6699 if (h->type == STT_FUNC
6700 || h->type == STT_GNU_IFUNC
6701 || h->needs_plt)
6702 {
6703 bool local = (ppc_elf_hash_entry (h)->save_res
6704 || SYMBOL_CALLS_LOCAL (info, h)
6705 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6706 /* Discard dyn_relocs when non-pic if we've decided that a
6707 function symbol is local and not an ifunc. We keep dynamic
6708 relocs for ifuncs when local rather than always emitting a
6709 plt call stub for them and defining the symbol on the call
6710 stub. We can't do that for ELFv1 anyway (a function symbol
6711 is defined on a descriptor, not code) and it can be faster at
6712 run-time due to not needing to bounce through a stub. The
6713 dyn_relocs for ifuncs will be applied even in a static
6714 executable. */
6715 if (!bfd_link_pic (info)
6716 && h->type != STT_GNU_IFUNC
6717 && local)
6718 h->dyn_relocs = NULL;
6719
6720 /* Clear procedure linkage table information for any symbol that
6721 won't need a .plt entry. */
6722 struct plt_entry *ent;
6723 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6724 if (ent->plt.refcount > 0)
6725 break;
6726 if (ent == NULL
6727 || (h->type != STT_GNU_IFUNC
6728 && local
6729 && (htab->can_convert_all_inline_plt
6730 || (ppc_elf_hash_entry (h)->tls_mask
6731 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6732 {
6733 h->plt.plist = NULL;
6734 h->needs_plt = 0;
6735 h->pointer_equality_needed = 0;
6736 }
6737 else if (abiversion (info->output_bfd) >= 2)
6738 {
6739 /* Taking a function's address in a read/write section
6740 doesn't require us to define the function symbol in the
6741 executable on a global entry stub. A dynamic reloc can
6742 be used instead. The reason we prefer a few more dynamic
6743 relocs is that calling via a global entry stub costs a
6744 few more instructions, and pointer_equality_needed causes
6745 extra work in ld.so when resolving these symbols. */
6746 if (global_entry_stub (h))
6747 {
6748 if (!_bfd_elf_readonly_dynrelocs (h))
6749 {
6750 h->pointer_equality_needed = 0;
6751 /* If we haven't seen a branch reloc and the symbol
6752 isn't an ifunc then we don't need a plt entry. */
6753 if (!h->needs_plt)
6754 h->plt.plist = NULL;
6755 }
6756 else if (!bfd_link_pic (info))
6757 /* We are going to be defining the function symbol on the
6758 plt stub, so no dyn_relocs needed when non-pic. */
6759 h->dyn_relocs = NULL;
6760 }
6761
6762 /* ELFv2 function symbols can't have copy relocs. */
6763 return true;
6764 }
6765 else if (!h->needs_plt
6766 && !_bfd_elf_readonly_dynrelocs (h))
6767 {
6768 /* If we haven't seen a branch reloc and the symbol isn't an
6769 ifunc then we don't need a plt entry. */
6770 h->plt.plist = NULL;
6771 h->pointer_equality_needed = 0;
6772 return true;
6773 }
6774 }
6775 else
6776 h->plt.plist = NULL;
6777
6778 /* If this is a weak symbol, and there is a real definition, the
6779 processor independent code will have arranged for us to see the
6780 real definition first, and we can just use the same value. */
6781 if (h->is_weakalias)
6782 {
6783 struct elf_link_hash_entry *def = weakdef (h);
6784 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6785 h->root.u.def.section = def->root.u.def.section;
6786 h->root.u.def.value = def->root.u.def.value;
6787 if (def->root.u.def.section == htab->elf.sdynbss
6788 || def->root.u.def.section == htab->elf.sdynrelro)
6789 h->dyn_relocs = NULL;
6790 return true;
6791 }
6792
6793 /* If we are creating a shared library, we must presume that the
6794 only references to the symbol are via the global offset table.
6795 For such cases we need not do anything here; the relocations will
6796 be handled correctly by relocate_section. */
6797 if (!bfd_link_executable (info))
6798 return true;
6799
6800 /* If there are no references to this symbol that do not use the
6801 GOT, we don't need to generate a copy reloc. */
6802 if (!h->non_got_ref)
6803 return true;
6804
6805 /* Don't generate a copy reloc for symbols defined in the executable. */
6806 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6807
6808 /* If -z nocopyreloc was given, don't generate them either. */
6809 || info->nocopyreloc
6810
6811 /* If we don't find any dynamic relocs in read-only sections, then
6812 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6813 || (ELIMINATE_COPY_RELOCS
6814 && !h->needs_copy
6815 && !alias_readonly_dynrelocs (h))
6816
6817 /* Protected variables do not work with .dynbss. The copy in
6818 .dynbss won't be used by the shared library with the protected
6819 definition for the variable. Text relocations are preferable
6820 to an incorrect program. */
6821 || h->protected_def)
6822 return true;
6823
6824 if (h->type == STT_FUNC
6825 || h->type == STT_GNU_IFUNC)
6826 {
6827 /* .dynbss copies of function symbols only work if we have
6828 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6829 use dot-symbols and set the function symbol size to the text
6830 size of the function rather than the size of the descriptor.
6831 That's wrong for copying a descriptor. */
6832 if (ppc_elf_hash_entry (h)->oh == NULL
6833 || !(h->size == 24 || h->size == 16))
6834 return true;
6835
6836 /* We should never get here, but unfortunately there are old
6837 versions of gcc (circa gcc-3.2) that improperly for the
6838 ELFv1 ABI put initialized function pointers, vtable refs and
6839 suchlike in read-only sections. Allow them to proceed, but
6840 warn that this might break at runtime. */
6841 info->callbacks->einfo
6842 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6843 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6844 h->root.root.string);
6845 }
6846
6847 /* This is a reference to a symbol defined by a dynamic object which
6848 is not a function. */
6849
6850 /* We must allocate the symbol in our .dynbss section, which will
6851 become part of the .bss section of the executable. There will be
6852 an entry for this symbol in the .dynsym section. The dynamic
6853 object will contain position independent code, so all references
6854 from the dynamic object to this symbol will go through the global
6855 offset table. The dynamic linker will use the .dynsym entry to
6856 determine the address it must put in the global offset table, so
6857 both the dynamic object and the regular object will refer to the
6858 same memory location for the variable. */
6859 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6860 {
6861 s = htab->elf.sdynrelro;
6862 srel = htab->elf.sreldynrelro;
6863 }
6864 else
6865 {
6866 s = htab->elf.sdynbss;
6867 srel = htab->elf.srelbss;
6868 }
6869 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6870 {
6871 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6872 linker to copy the initial value out of the dynamic object
6873 and into the runtime process image. */
6874 srel->size += sizeof (Elf64_External_Rela);
6875 h->needs_copy = 1;
6876 }
6877
6878 /* We no longer want dyn_relocs. */
6879 h->dyn_relocs = NULL;
6880 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6881 }
6882
6883 /* If given a function descriptor symbol, hide both the function code
6884 sym and the descriptor. */
6885 static void
6886 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6887 struct elf_link_hash_entry *h,
6888 bool force_local)
6889 {
6890 struct ppc_link_hash_entry *eh;
6891 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6892
6893 if (ppc_hash_table (info) == NULL)
6894 return;
6895
6896 eh = ppc_elf_hash_entry (h);
6897 if (eh->is_func_descriptor)
6898 {
6899 struct ppc_link_hash_entry *fh = eh->oh;
6900
6901 if (fh == NULL)
6902 {
6903 const char *p, *q;
6904 struct elf_link_hash_table *htab = elf_hash_table (info);
6905 char save;
6906
6907 /* We aren't supposed to use alloca in BFD because on
6908 systems which do not have alloca the version in libiberty
6909 calls xmalloc, which might cause the program to crash
6910 when it runs out of memory. This function doesn't have a
6911 return status, so there's no way to gracefully return an
6912 error. So cheat. We know that string[-1] can be safely
6913 accessed; It's either a string in an ELF string table,
6914 or allocated in an objalloc structure. */
6915
6916 p = eh->elf.root.root.string - 1;
6917 save = *p;
6918 *(char *) p = '.';
6919 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6920 false, false));
6921 *(char *) p = save;
6922
6923 /* Unfortunately, if it so happens that the string we were
6924 looking for was allocated immediately before this string,
6925 then we overwrote the string terminator. That's the only
6926 reason the lookup should fail. */
6927 if (fh == NULL)
6928 {
6929 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6930 while (q >= eh->elf.root.root.string && *q == *p)
6931 --q, --p;
6932 if (q < eh->elf.root.root.string && *p == '.')
6933 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6934 false, false));
6935 }
6936 if (fh != NULL)
6937 {
6938 eh->oh = fh;
6939 fh->oh = eh;
6940 }
6941 }
6942 if (fh != NULL)
6943 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6944 }
6945 }
6946
6947 static bool
6948 get_sym_h (struct elf_link_hash_entry **hp,
6949 Elf_Internal_Sym **symp,
6950 asection **symsecp,
6951 unsigned char **tls_maskp,
6952 Elf_Internal_Sym **locsymsp,
6953 unsigned long r_symndx,
6954 bfd *ibfd)
6955 {
6956 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6957
6958 if (r_symndx >= symtab_hdr->sh_info)
6959 {
6960 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6961 struct elf_link_hash_entry *h;
6962
6963 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6964 h = elf_follow_link (h);
6965
6966 if (hp != NULL)
6967 *hp = h;
6968
6969 if (symp != NULL)
6970 *symp = NULL;
6971
6972 if (symsecp != NULL)
6973 {
6974 asection *symsec = NULL;
6975 if (h->root.type == bfd_link_hash_defined
6976 || h->root.type == bfd_link_hash_defweak)
6977 symsec = h->root.u.def.section;
6978 *symsecp = symsec;
6979 }
6980
6981 if (tls_maskp != NULL)
6982 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6983 }
6984 else
6985 {
6986 Elf_Internal_Sym *sym;
6987 Elf_Internal_Sym *locsyms = *locsymsp;
6988
6989 if (locsyms == NULL)
6990 {
6991 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6992 if (locsyms == NULL)
6993 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6994 symtab_hdr->sh_info,
6995 0, NULL, NULL, NULL);
6996 if (locsyms == NULL)
6997 return false;
6998 *locsymsp = locsyms;
6999 }
7000 sym = locsyms + r_symndx;
7001
7002 if (hp != NULL)
7003 *hp = NULL;
7004
7005 if (symp != NULL)
7006 *symp = sym;
7007
7008 if (symsecp != NULL)
7009 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7010
7011 if (tls_maskp != NULL)
7012 {
7013 struct got_entry **lgot_ents;
7014 unsigned char *tls_mask;
7015
7016 tls_mask = NULL;
7017 lgot_ents = elf_local_got_ents (ibfd);
7018 if (lgot_ents != NULL)
7019 {
7020 struct plt_entry **local_plt = (struct plt_entry **)
7021 (lgot_ents + symtab_hdr->sh_info);
7022 unsigned char *lgot_masks = (unsigned char *)
7023 (local_plt + symtab_hdr->sh_info);
7024 tls_mask = &lgot_masks[r_symndx];
7025 }
7026 *tls_maskp = tls_mask;
7027 }
7028 }
7029 return true;
7030 }
7031
7032 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7033 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7034 type suitable for optimization, and 1 otherwise. */
7035
7036 static int
7037 get_tls_mask (unsigned char **tls_maskp,
7038 unsigned long *toc_symndx,
7039 bfd_vma *toc_addend,
7040 Elf_Internal_Sym **locsymsp,
7041 const Elf_Internal_Rela *rel,
7042 bfd *ibfd)
7043 {
7044 unsigned long r_symndx;
7045 int next_r;
7046 struct elf_link_hash_entry *h;
7047 Elf_Internal_Sym *sym;
7048 asection *sec;
7049 bfd_vma off;
7050
7051 r_symndx = ELF64_R_SYM (rel->r_info);
7052 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7053 return 0;
7054
7055 if ((*tls_maskp != NULL
7056 && (**tls_maskp & TLS_TLS) != 0
7057 && **tls_maskp != (TLS_TLS | TLS_MARK))
7058 || sec == NULL
7059 || ppc64_elf_section_data (sec) == NULL
7060 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7061 return 1;
7062
7063 /* Look inside a TOC section too. */
7064 if (h != NULL)
7065 {
7066 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7067 off = h->root.u.def.value;
7068 }
7069 else
7070 off = sym->st_value;
7071 off += rel->r_addend;
7072 BFD_ASSERT (off % 8 == 0);
7073 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7074 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7075 if (toc_symndx != NULL)
7076 *toc_symndx = r_symndx;
7077 if (toc_addend != NULL)
7078 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7079 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7080 return 0;
7081 if ((h == NULL || is_static_defined (h))
7082 && (next_r == -1 || next_r == -2))
7083 return 1 - next_r;
7084 return 1;
7085 }
7086
7087 /* Find (or create) an entry in the tocsave hash table. */
7088
7089 static struct tocsave_entry *
7090 tocsave_find (struct ppc_link_hash_table *htab,
7091 enum insert_option insert,
7092 Elf_Internal_Sym **local_syms,
7093 const Elf_Internal_Rela *irela,
7094 bfd *ibfd)
7095 {
7096 unsigned long r_indx;
7097 struct elf_link_hash_entry *h;
7098 Elf_Internal_Sym *sym;
7099 struct tocsave_entry ent, *p;
7100 hashval_t hash;
7101 struct tocsave_entry **slot;
7102
7103 r_indx = ELF64_R_SYM (irela->r_info);
7104 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7105 return NULL;
7106 if (ent.sec == NULL || ent.sec->output_section == NULL)
7107 {
7108 _bfd_error_handler
7109 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7110 return NULL;
7111 }
7112
7113 if (h != NULL)
7114 ent.offset = h->root.u.def.value;
7115 else
7116 ent.offset = sym->st_value;
7117 ent.offset += irela->r_addend;
7118
7119 hash = tocsave_htab_hash (&ent);
7120 slot = ((struct tocsave_entry **)
7121 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7122 if (slot == NULL)
7123 return NULL;
7124
7125 if (*slot == NULL)
7126 {
7127 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7128 if (p == NULL)
7129 return NULL;
7130 *p = ent;
7131 *slot = p;
7132 }
7133 return *slot;
7134 }
7135
7136 /* Adjust all global syms defined in opd sections. In gcc generated
7137 code for the old ABI, these will already have been done. */
7138
7139 static bool
7140 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7141 {
7142 struct ppc_link_hash_entry *eh;
7143 asection *sym_sec;
7144 struct _opd_sec_data *opd;
7145
7146 if (h->root.type == bfd_link_hash_indirect)
7147 return true;
7148
7149 if (h->root.type != bfd_link_hash_defined
7150 && h->root.type != bfd_link_hash_defweak)
7151 return true;
7152
7153 eh = ppc_elf_hash_entry (h);
7154 if (eh->adjust_done)
7155 return true;
7156
7157 sym_sec = eh->elf.root.u.def.section;
7158 opd = get_opd_info (sym_sec);
7159 if (opd != NULL && opd->adjust != NULL)
7160 {
7161 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7162 if (adjust == -1)
7163 {
7164 /* This entry has been deleted. */
7165 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7166 if (dsec == NULL)
7167 {
7168 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7169 if (discarded_section (dsec))
7170 {
7171 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7172 break;
7173 }
7174 }
7175 eh->elf.root.u.def.value = 0;
7176 eh->elf.root.u.def.section = dsec;
7177 }
7178 else
7179 eh->elf.root.u.def.value += adjust;
7180 eh->adjust_done = 1;
7181 }
7182 return true;
7183 }
7184
7185 /* Handles decrementing dynamic reloc counts for the reloc specified by
7186 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7187 have already been determined. */
7188
7189 static bool
7190 dec_dynrel_count (const Elf_Internal_Rela *rel,
7191 asection *sec,
7192 struct bfd_link_info *info,
7193 Elf_Internal_Sym **local_syms,
7194 struct elf_link_hash_entry *h,
7195 Elf_Internal_Sym *sym)
7196 {
7197 enum elf_ppc64_reloc_type r_type;
7198 asection *sym_sec = NULL;
7199
7200 /* Can this reloc be dynamic? This switch, and later tests here
7201 should be kept in sync with the code in check_relocs. */
7202 r_type = ELF64_R_TYPE (rel->r_info);
7203 switch (r_type)
7204 {
7205 default:
7206 return true;
7207
7208 case R_PPC64_TOC16:
7209 case R_PPC64_TOC16_DS:
7210 case R_PPC64_TOC16_LO:
7211 case R_PPC64_TOC16_HI:
7212 case R_PPC64_TOC16_HA:
7213 case R_PPC64_TOC16_LO_DS:
7214 if (h == NULL)
7215 return true;
7216 break;
7217
7218 case R_PPC64_TPREL16:
7219 case R_PPC64_TPREL16_LO:
7220 case R_PPC64_TPREL16_HI:
7221 case R_PPC64_TPREL16_HA:
7222 case R_PPC64_TPREL16_DS:
7223 case R_PPC64_TPREL16_LO_DS:
7224 case R_PPC64_TPREL16_HIGH:
7225 case R_PPC64_TPREL16_HIGHA:
7226 case R_PPC64_TPREL16_HIGHER:
7227 case R_PPC64_TPREL16_HIGHERA:
7228 case R_PPC64_TPREL16_HIGHEST:
7229 case R_PPC64_TPREL16_HIGHESTA:
7230 case R_PPC64_TPREL64:
7231 case R_PPC64_TPREL34:
7232 case R_PPC64_DTPMOD64:
7233 case R_PPC64_DTPREL64:
7234 case R_PPC64_ADDR64:
7235 case R_PPC64_REL30:
7236 case R_PPC64_REL32:
7237 case R_PPC64_REL64:
7238 case R_PPC64_ADDR14:
7239 case R_PPC64_ADDR14_BRNTAKEN:
7240 case R_PPC64_ADDR14_BRTAKEN:
7241 case R_PPC64_ADDR16:
7242 case R_PPC64_ADDR16_DS:
7243 case R_PPC64_ADDR16_HA:
7244 case R_PPC64_ADDR16_HI:
7245 case R_PPC64_ADDR16_HIGH:
7246 case R_PPC64_ADDR16_HIGHA:
7247 case R_PPC64_ADDR16_HIGHER:
7248 case R_PPC64_ADDR16_HIGHERA:
7249 case R_PPC64_ADDR16_HIGHEST:
7250 case R_PPC64_ADDR16_HIGHESTA:
7251 case R_PPC64_ADDR16_LO:
7252 case R_PPC64_ADDR16_LO_DS:
7253 case R_PPC64_ADDR24:
7254 case R_PPC64_ADDR32:
7255 case R_PPC64_UADDR16:
7256 case R_PPC64_UADDR32:
7257 case R_PPC64_UADDR64:
7258 case R_PPC64_TOC:
7259 case R_PPC64_D34:
7260 case R_PPC64_D34_LO:
7261 case R_PPC64_D34_HI30:
7262 case R_PPC64_D34_HA30:
7263 case R_PPC64_ADDR16_HIGHER34:
7264 case R_PPC64_ADDR16_HIGHERA34:
7265 case R_PPC64_ADDR16_HIGHEST34:
7266 case R_PPC64_ADDR16_HIGHESTA34:
7267 case R_PPC64_D28:
7268 break;
7269 }
7270
7271 if (local_syms != NULL)
7272 {
7273 unsigned long r_symndx;
7274 bfd *ibfd = sec->owner;
7275
7276 r_symndx = ELF64_R_SYM (rel->r_info);
7277 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7278 return false;
7279 }
7280
7281 if ((h != NULL
7282 && !SYMBOL_REFERENCES_LOCAL (info, h))
7283 || (bfd_link_pic (info)
7284 && (h != NULL
7285 ? !bfd_is_abs_symbol (&h->root)
7286 : sym_sec != bfd_abs_section_ptr)
7287 && must_be_dyn_reloc (info, r_type))
7288 || (!bfd_link_pic (info)
7289 && (h != NULL
7290 ? h->type == STT_GNU_IFUNC
7291 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7292 ;
7293 else
7294 return true;
7295
7296 if (h != NULL)
7297 {
7298 struct ppc_dyn_relocs *p;
7299 struct ppc_dyn_relocs **pp;
7300 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7301
7302 /* elf_gc_sweep may have already removed all dyn relocs associated
7303 with local syms for a given section. Also, symbol flags are
7304 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7305 report a dynreloc miscount. */
7306 if (*pp == NULL && info->gc_sections)
7307 return true;
7308
7309 while ((p = *pp) != NULL)
7310 {
7311 if (p->sec == sec)
7312 {
7313 if (!must_be_dyn_reloc (info, r_type))
7314 p->pc_count -= 1;
7315 if (maybe_relr (r_type, rel, sec))
7316 p->rel_count -= 1;
7317 p->count -= 1;
7318 if (p->count == 0)
7319 *pp = p->next;
7320 return true;
7321 }
7322 pp = &p->next;
7323 }
7324 }
7325 else
7326 {
7327 struct ppc_local_dyn_relocs *p;
7328 struct ppc_local_dyn_relocs **pp;
7329 void *vpp;
7330 bool is_ifunc;
7331
7332 if (local_syms == NULL)
7333 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7334 if (sym_sec == NULL)
7335 sym_sec = sec;
7336
7337 vpp = &elf_section_data (sym_sec)->local_dynrel;
7338 pp = (struct ppc_local_dyn_relocs **) vpp;
7339
7340 if (*pp == NULL && info->gc_sections)
7341 return true;
7342
7343 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7344 while ((p = *pp) != NULL)
7345 {
7346 if (p->sec == sec && p->ifunc == is_ifunc)
7347 {
7348 if (maybe_relr (r_type, rel, sec))
7349 p->rel_count -= 1;
7350 p->count -= 1;
7351 if (p->count == 0)
7352 *pp = p->next;
7353 return true;
7354 }
7355 pp = &p->next;
7356 }
7357 }
7358
7359 /* xgettext:c-format */
7360 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7361 sec->owner, sec);
7362 bfd_set_error (bfd_error_bad_value);
7363 return false;
7364 }
7365
7366 /* Remove unused Official Procedure Descriptor entries. Currently we
7367 only remove those associated with functions in discarded link-once
7368 sections, or weakly defined functions that have been overridden. It
7369 would be possible to remove many more entries for statically linked
7370 applications. */
7371
7372 bool
7373 ppc64_elf_edit_opd (struct bfd_link_info *info)
7374 {
7375 bfd *ibfd;
7376 bool some_edited = false;
7377 asection *need_pad = NULL;
7378 struct ppc_link_hash_table *htab;
7379
7380 htab = ppc_hash_table (info);
7381 if (htab == NULL)
7382 return false;
7383
7384 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7385 {
7386 asection *sec;
7387 Elf_Internal_Rela *relstart, *rel, *relend;
7388 Elf_Internal_Shdr *symtab_hdr;
7389 Elf_Internal_Sym *local_syms;
7390 struct _opd_sec_data *opd;
7391 bool need_edit, add_aux_fields, broken;
7392 bfd_size_type cnt_16b = 0;
7393
7394 if (!is_ppc64_elf (ibfd))
7395 continue;
7396
7397 sec = bfd_get_section_by_name (ibfd, ".opd");
7398 if (sec == NULL
7399 || sec->size == 0
7400 || (sec->flags & SEC_HAS_CONTENTS) == 0)
7401 continue;
7402
7403 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7404 continue;
7405
7406 if (sec->output_section == bfd_abs_section_ptr)
7407 continue;
7408
7409 /* Look through the section relocs. */
7410 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7411 continue;
7412
7413 local_syms = NULL;
7414 symtab_hdr = &elf_symtab_hdr (ibfd);
7415
7416 /* Read the relocations. */
7417 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7418 info->keep_memory);
7419 if (relstart == NULL)
7420 return false;
7421
7422 /* First run through the relocs to check they are sane, and to
7423 determine whether we need to edit this opd section. */
7424 need_edit = false;
7425 broken = false;
7426 need_pad = sec;
7427 relend = relstart + sec->reloc_count;
7428 for (rel = relstart; rel < relend; )
7429 {
7430 enum elf_ppc64_reloc_type r_type;
7431 unsigned long r_symndx;
7432 asection *sym_sec;
7433 struct elf_link_hash_entry *h;
7434 Elf_Internal_Sym *sym;
7435 bfd_vma offset;
7436
7437 /* .opd contains an array of 16 or 24 byte entries. We're
7438 only interested in the reloc pointing to a function entry
7439 point. */
7440 offset = rel->r_offset;
7441 if (rel + 1 == relend
7442 || rel[1].r_offset != offset + 8)
7443 {
7444 /* If someone messes with .opd alignment then after a
7445 "ld -r" we might have padding in the middle of .opd.
7446 Also, there's nothing to prevent someone putting
7447 something silly in .opd with the assembler. No .opd
7448 optimization for them! */
7449 broken_opd:
7450 _bfd_error_handler
7451 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7452 broken = true;
7453 break;
7454 }
7455
7456 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7457 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7458 {
7459 _bfd_error_handler
7460 /* xgettext:c-format */
7461 (_("%pB: unexpected reloc type %u in .opd section"),
7462 ibfd, r_type);
7463 broken = true;
7464 break;
7465 }
7466
7467 r_symndx = ELF64_R_SYM (rel->r_info);
7468 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7469 r_symndx, ibfd))
7470 goto error_ret;
7471
7472 if (sym_sec == NULL || sym_sec->owner == NULL)
7473 {
7474 const char *sym_name;
7475 if (h != NULL)
7476 sym_name = h->root.root.string;
7477 else
7478 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7479 sym_sec);
7480
7481 _bfd_error_handler
7482 /* xgettext:c-format */
7483 (_("%pB: undefined sym `%s' in .opd section"),
7484 ibfd, sym_name);
7485 broken = true;
7486 break;
7487 }
7488
7489 /* opd entries are always for functions defined in the
7490 current input bfd. If the symbol isn't defined in the
7491 input bfd, then we won't be using the function in this
7492 bfd; It must be defined in a linkonce section in another
7493 bfd, or is weak. It's also possible that we are
7494 discarding the function due to a linker script /DISCARD/,
7495 which we test for via the output_section. */
7496 if (sym_sec->owner != ibfd
7497 || sym_sec->output_section == bfd_abs_section_ptr)
7498 need_edit = true;
7499
7500 rel += 2;
7501 if (rel + 1 == relend
7502 || (rel + 2 < relend
7503 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7504 ++rel;
7505
7506 if (rel == relend)
7507 {
7508 if (sec->size == offset + 24)
7509 {
7510 need_pad = NULL;
7511 break;
7512 }
7513 if (sec->size == offset + 16)
7514 {
7515 cnt_16b++;
7516 break;
7517 }
7518 goto broken_opd;
7519 }
7520 else if (rel + 1 < relend
7521 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7522 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7523 {
7524 if (rel[0].r_offset == offset + 16)
7525 cnt_16b++;
7526 else if (rel[0].r_offset != offset + 24)
7527 goto broken_opd;
7528 }
7529 else
7530 goto broken_opd;
7531 }
7532
7533 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7534
7535 if (!broken && (need_edit || add_aux_fields))
7536 {
7537 Elf_Internal_Rela *write_rel;
7538 Elf_Internal_Shdr *rel_hdr;
7539 bfd_byte *rptr, *wptr;
7540 bfd_byte *new_contents;
7541 bfd_size_type amt;
7542
7543 new_contents = NULL;
7544 amt = OPD_NDX (sec->size) * sizeof (long);
7545 opd = &ppc64_elf_section_data (sec)->u.opd;
7546 opd->adjust = bfd_zalloc (sec->owner, amt);
7547 if (opd->adjust == NULL)
7548 return false;
7549
7550 /* This seems a waste of time as input .opd sections are all
7551 zeros as generated by gcc, but I suppose there's no reason
7552 this will always be so. We might start putting something in
7553 the third word of .opd entries. */
7554 if ((sec->flags & SEC_IN_MEMORY) == 0)
7555 {
7556 bfd_byte *loc;
7557 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7558 {
7559 free (loc);
7560 error_ret:
7561 if (symtab_hdr->contents != (unsigned char *) local_syms)
7562 free (local_syms);
7563 if (elf_section_data (sec)->relocs != relstart)
7564 free (relstart);
7565 return false;
7566 }
7567 sec->contents = loc;
7568 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7569 }
7570
7571 elf_section_data (sec)->relocs = relstart;
7572
7573 new_contents = sec->contents;
7574 if (add_aux_fields)
7575 {
7576 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7577 if (new_contents == NULL)
7578 return false;
7579 need_pad = NULL;
7580 }
7581 wptr = new_contents;
7582 rptr = sec->contents;
7583 write_rel = relstart;
7584 for (rel = relstart; rel < relend; )
7585 {
7586 unsigned long r_symndx;
7587 asection *sym_sec;
7588 struct elf_link_hash_entry *h;
7589 struct ppc_link_hash_entry *fdh = NULL;
7590 Elf_Internal_Sym *sym;
7591 long opd_ent_size;
7592 Elf_Internal_Rela *next_rel;
7593 bool skip;
7594
7595 r_symndx = ELF64_R_SYM (rel->r_info);
7596 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7597 r_symndx, ibfd))
7598 goto error_ret;
7599
7600 next_rel = rel + 2;
7601 if (next_rel + 1 == relend
7602 || (next_rel + 2 < relend
7603 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7604 ++next_rel;
7605
7606 /* See if the .opd entry is full 24 byte or
7607 16 byte (with fd_aux entry overlapped with next
7608 fd_func). */
7609 opd_ent_size = 24;
7610 if (next_rel == relend)
7611 {
7612 if (sec->size == rel->r_offset + 16)
7613 opd_ent_size = 16;
7614 }
7615 else if (next_rel->r_offset == rel->r_offset + 16)
7616 opd_ent_size = 16;
7617
7618 if (h != NULL
7619 && h->root.root.string[0] == '.')
7620 {
7621 fdh = ppc_elf_hash_entry (h)->oh;
7622 if (fdh != NULL)
7623 {
7624 fdh = ppc_follow_link (fdh);
7625 if (fdh->elf.root.type != bfd_link_hash_defined
7626 && fdh->elf.root.type != bfd_link_hash_defweak)
7627 fdh = NULL;
7628 }
7629 }
7630
7631 skip = (sym_sec->owner != ibfd
7632 || sym_sec->output_section == bfd_abs_section_ptr);
7633 if (skip)
7634 {
7635 if (fdh != NULL && sym_sec->owner == ibfd)
7636 {
7637 /* Arrange for the function descriptor sym
7638 to be dropped. */
7639 fdh->elf.root.u.def.value = 0;
7640 fdh->elf.root.u.def.section = sym_sec;
7641 }
7642 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7643
7644 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7645 rel = next_rel;
7646 else
7647 while (1)
7648 {
7649 if (!dec_dynrel_count (rel, sec, info,
7650 NULL, h, sym))
7651 goto error_ret;
7652
7653 if (++rel == next_rel)
7654 break;
7655
7656 r_symndx = ELF64_R_SYM (rel->r_info);
7657 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7658 r_symndx, ibfd))
7659 goto error_ret;
7660 }
7661 }
7662 else
7663 {
7664 /* We'll be keeping this opd entry. */
7665 long adjust;
7666
7667 if (fdh != NULL)
7668 {
7669 /* Redefine the function descriptor symbol to
7670 this location in the opd section. It is
7671 necessary to update the value here rather
7672 than using an array of adjustments as we do
7673 for local symbols, because various places
7674 in the generic ELF code use the value
7675 stored in u.def.value. */
7676 fdh->elf.root.u.def.value = wptr - new_contents;
7677 fdh->adjust_done = 1;
7678 }
7679
7680 /* Local syms are a bit tricky. We could
7681 tweak them as they can be cached, but
7682 we'd need to look through the local syms
7683 for the function descriptor sym which we
7684 don't have at the moment. So keep an
7685 array of adjustments. */
7686 adjust = (wptr - new_contents) - (rptr - sec->contents);
7687 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7688
7689 if (wptr != rptr)
7690 memcpy (wptr, rptr, opd_ent_size);
7691 wptr += opd_ent_size;
7692 if (add_aux_fields && opd_ent_size == 16)
7693 {
7694 memset (wptr, '\0', 8);
7695 wptr += 8;
7696 }
7697
7698 /* We need to adjust any reloc offsets to point to the
7699 new opd entries. */
7700 for ( ; rel != next_rel; ++rel)
7701 {
7702 rel->r_offset += adjust;
7703 if (write_rel != rel)
7704 memcpy (write_rel, rel, sizeof (*rel));
7705 ++write_rel;
7706 }
7707 }
7708
7709 rptr += opd_ent_size;
7710 }
7711
7712 sec->size = wptr - new_contents;
7713 sec->reloc_count = write_rel - relstart;
7714 if (add_aux_fields)
7715 {
7716 free (sec->contents);
7717 sec->contents = new_contents;
7718 }
7719
7720 /* Fudge the header size too, as this is used later in
7721 elf_bfd_final_link if we are emitting relocs. */
7722 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7723 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7724 some_edited = true;
7725 }
7726 else if (elf_section_data (sec)->relocs != relstart)
7727 free (relstart);
7728
7729 if (local_syms != NULL
7730 && symtab_hdr->contents != (unsigned char *) local_syms)
7731 {
7732 if (!info->keep_memory)
7733 free (local_syms);
7734 else
7735 symtab_hdr->contents = (unsigned char *) local_syms;
7736 }
7737 }
7738
7739 if (some_edited)
7740 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7741
7742 /* If we are doing a final link and the last .opd entry is just 16 byte
7743 long, add a 8 byte padding after it. */
7744 if (need_pad != NULL && !bfd_link_relocatable (info))
7745 {
7746 bfd_byte *p;
7747
7748 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7749 {
7750 BFD_ASSERT (need_pad->size > 0);
7751
7752 p = bfd_malloc (need_pad->size + 8);
7753 if (p == NULL)
7754 return false;
7755
7756 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7757 p, 0, need_pad->size))
7758 return false;
7759
7760 need_pad->contents = p;
7761 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7762 }
7763 else
7764 {
7765 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7766 if (p == NULL)
7767 return false;
7768
7769 need_pad->contents = p;
7770 }
7771
7772 memset (need_pad->contents + need_pad->size, 0, 8);
7773 need_pad->size += 8;
7774 }
7775
7776 return true;
7777 }
7778
7779 /* Analyze inline PLT call relocations to see whether calls to locally
7780 defined functions can be converted to direct calls. */
7781
7782 bool
7783 ppc64_elf_inline_plt (struct bfd_link_info *info)
7784 {
7785 struct ppc_link_hash_table *htab;
7786 bfd *ibfd;
7787 asection *sec;
7788 bfd_vma low_vma, high_vma, limit;
7789
7790 htab = ppc_hash_table (info);
7791 if (htab == NULL)
7792 return false;
7793
7794 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7795 reduced somewhat to cater for possible stubs that might be added
7796 between the call and its destination. */
7797 if (htab->params->group_size < 0)
7798 {
7799 limit = -htab->params->group_size;
7800 if (limit == 1)
7801 limit = 0x1e00000;
7802 }
7803 else
7804 {
7805 limit = htab->params->group_size;
7806 if (limit == 1)
7807 limit = 0x1c00000;
7808 }
7809
7810 low_vma = -1;
7811 high_vma = 0;
7812 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7813 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7814 {
7815 if (low_vma > sec->vma)
7816 low_vma = sec->vma;
7817 if (high_vma < sec->vma + sec->size)
7818 high_vma = sec->vma + sec->size;
7819 }
7820
7821 /* If a "bl" can reach anywhere in local code sections, then we can
7822 convert all inline PLT sequences to direct calls when the symbol
7823 is local. */
7824 if (high_vma - low_vma < limit)
7825 {
7826 htab->can_convert_all_inline_plt = 1;
7827 return true;
7828 }
7829
7830 /* Otherwise, go looking through relocs for cases where a direct
7831 call won't reach. Mark the symbol on any such reloc to disable
7832 the optimization and keep the PLT entry as it seems likely that
7833 this will be better than creating trampolines. Note that this
7834 will disable the optimization for all inline PLT calls to a
7835 particular symbol, not just those that won't reach. The
7836 difficulty in doing a more precise optimization is that the
7837 linker needs to make a decision depending on whether a
7838 particular R_PPC64_PLTCALL insn can be turned into a direct
7839 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7840 the sequence, and there is nothing that ties those relocs
7841 together except their symbol. */
7842
7843 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7844 {
7845 Elf_Internal_Shdr *symtab_hdr;
7846 Elf_Internal_Sym *local_syms;
7847
7848 if (!is_ppc64_elf (ibfd))
7849 continue;
7850
7851 local_syms = NULL;
7852 symtab_hdr = &elf_symtab_hdr (ibfd);
7853
7854 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7855 if (ppc64_elf_section_data (sec)->has_pltcall
7856 && !bfd_is_abs_section (sec->output_section))
7857 {
7858 Elf_Internal_Rela *relstart, *rel, *relend;
7859
7860 /* Read the relocations. */
7861 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7862 info->keep_memory);
7863 if (relstart == NULL)
7864 return false;
7865
7866 relend = relstart + sec->reloc_count;
7867 for (rel = relstart; rel < relend; rel++)
7868 {
7869 enum elf_ppc64_reloc_type r_type;
7870 unsigned long r_symndx;
7871 asection *sym_sec;
7872 struct elf_link_hash_entry *h;
7873 Elf_Internal_Sym *sym;
7874 unsigned char *tls_maskp;
7875
7876 r_type = ELF64_R_TYPE (rel->r_info);
7877 if (r_type != R_PPC64_PLTCALL
7878 && r_type != R_PPC64_PLTCALL_NOTOC)
7879 continue;
7880
7881 r_symndx = ELF64_R_SYM (rel->r_info);
7882 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7883 r_symndx, ibfd))
7884 {
7885 if (elf_section_data (sec)->relocs != relstart)
7886 free (relstart);
7887 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7888 free (local_syms);
7889 return false;
7890 }
7891
7892 if (sym_sec != NULL && sym_sec->output_section != NULL)
7893 {
7894 bfd_vma from, to;
7895 if (h != NULL)
7896 to = h->root.u.def.value;
7897 else
7898 to = sym->st_value;
7899 to += (rel->r_addend
7900 + sym_sec->output_offset
7901 + sym_sec->output_section->vma);
7902 from = (rel->r_offset
7903 + sec->output_offset
7904 + sec->output_section->vma);
7905 if (to - from + limit < 2 * limit
7906 && !(r_type == R_PPC64_PLTCALL_NOTOC
7907 && (((h ? h->other : sym->st_other)
7908 & STO_PPC64_LOCAL_MASK)
7909 > 1 << STO_PPC64_LOCAL_BIT)))
7910 *tls_maskp &= ~PLT_KEEP;
7911 }
7912 }
7913 if (elf_section_data (sec)->relocs != relstart)
7914 free (relstart);
7915 }
7916
7917 if (local_syms != NULL
7918 && symtab_hdr->contents != (unsigned char *) local_syms)
7919 {
7920 if (!info->keep_memory)
7921 free (local_syms);
7922 else
7923 symtab_hdr->contents = (unsigned char *) local_syms;
7924 }
7925 }
7926
7927 return true;
7928 }
7929
7930 /* Set htab->tls_get_addr and various other info specific to TLS.
7931 This needs to run before dynamic symbols are processed in
7932 bfd_elf_size_dynamic_sections. */
7933
7934 bool
7935 ppc64_elf_tls_setup (struct bfd_link_info *info)
7936 {
7937 struct ppc_link_hash_table *htab;
7938 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7939
7940 htab = ppc_hash_table (info);
7941 if (htab == NULL)
7942 return false;
7943
7944 /* Move dynamic linking info to the function descriptor sym. */
7945 if (htab->need_func_desc_adj)
7946 {
7947 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7948 htab->need_func_desc_adj = 0;
7949 }
7950
7951 if (abiversion (info->output_bfd) == 1)
7952 htab->opd_abi = 1;
7953
7954 if (htab->params->no_multi_toc)
7955 htab->do_multi_toc = 0;
7956 else if (!htab->do_multi_toc)
7957 htab->params->no_multi_toc = 1;
7958
7959 /* Default to --no-plt-localentry, as this option can cause problems
7960 with symbol interposition. For example, glibc libpthread.so and
7961 libc.so duplicate many pthread symbols, with a fallback
7962 implementation in libc.so. In some cases the fallback does more
7963 work than the pthread implementation. __pthread_condattr_destroy
7964 is one such symbol: the libpthread.so implementation is
7965 localentry:0 while the libc.so implementation is localentry:8.
7966 An app that "cleverly" uses dlopen to only load necessary
7967 libraries at runtime may omit loading libpthread.so when not
7968 running multi-threaded, which then results in the libc.so
7969 fallback symbols being used and ld.so complaining. Now there
7970 are workarounds in ld (see non_zero_localentry) to detect the
7971 pthread situation, but that may not be the only case where
7972 --plt-localentry can cause trouble. */
7973 if (htab->params->plt_localentry0 < 0)
7974 htab->params->plt_localentry0 = 0;
7975 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7976 {
7977 /* The issue is that __glink_PLTresolve saves r2, which is done
7978 because glibc ld.so _dl_runtime_resolve restores r2 to support
7979 a glibc plt call optimisation where global entry code is
7980 skipped on calls that resolve to the same binary. The
7981 __glink_PLTresolve save of r2 is incompatible with code
7982 making tail calls, because the tail call might go via the
7983 resolver and thus overwrite the proper saved r2. */
7984 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7985 "power10 pc-relative code"));
7986 htab->params->plt_localentry0 = 0;
7987 }
7988 if (htab->params->plt_localentry0
7989 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7990 false, false, false) == NULL)
7991 _bfd_error_handler
7992 (_("warning: --plt-localentry is especially dangerous without "
7993 "ld.so support to detect ABI violations"));
7994
7995 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7996 false, false, true);
7997 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7998 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7999 false, false, true);
8000 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
8001
8002 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
8003 false, false, true);
8004 htab->tga_desc = ppc_elf_hash_entry (desc);
8005 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
8006 false, false, true);
8007 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
8008
8009 if (htab->params->tls_get_addr_opt)
8010 {
8011 struct elf_link_hash_entry *opt, *opt_fd;
8012
8013 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8014 false, false, true);
8015 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8016 false, false, true);
8017 if (opt_fd != NULL
8018 && (opt_fd->root.type == bfd_link_hash_defined
8019 || opt_fd->root.type == bfd_link_hash_defweak))
8020 {
8021 /* If glibc supports an optimized __tls_get_addr call stub,
8022 signalled by the presence of __tls_get_addr_opt, and we'll
8023 be calling __tls_get_addr via a plt call stub, then
8024 make __tls_get_addr point to __tls_get_addr_opt. */
8025 if (!(htab->elf.dynamic_sections_created
8026 && tga_fd != NULL
8027 && (tga_fd->type == STT_FUNC
8028 || tga_fd->needs_plt)
8029 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8030 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8031 tga_fd = NULL;
8032 if (!(htab->elf.dynamic_sections_created
8033 && desc_fd != NULL
8034 && (desc_fd->type == STT_FUNC
8035 || desc_fd->needs_plt)
8036 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8037 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8038 desc_fd = NULL;
8039
8040 if (tga_fd != NULL || desc_fd != NULL)
8041 {
8042 struct plt_entry *ent = NULL;
8043
8044 if (tga_fd != NULL)
8045 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8046 if (ent->plt.refcount > 0)
8047 break;
8048 if (ent == NULL && desc_fd != NULL)
8049 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8050 if (ent->plt.refcount > 0)
8051 break;
8052 if (ent != NULL)
8053 {
8054 if (tga_fd != NULL)
8055 {
8056 tga_fd->root.type = bfd_link_hash_indirect;
8057 tga_fd->root.u.i.link = &opt_fd->root;
8058 tga_fd->root.u.i.warning = NULL;
8059 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8060 }
8061 if (desc_fd != NULL)
8062 {
8063 desc_fd->root.type = bfd_link_hash_indirect;
8064 desc_fd->root.u.i.link = &opt_fd->root;
8065 desc_fd->root.u.i.warning = NULL;
8066 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8067 }
8068 opt_fd->mark = 1;
8069 if (opt_fd->dynindx != -1)
8070 {
8071 /* Use __tls_get_addr_opt in dynamic relocations. */
8072 opt_fd->dynindx = -1;
8073 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8074 opt_fd->dynstr_index);
8075 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8076 return false;
8077 }
8078 if (tga_fd != NULL)
8079 {
8080 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8081 tga = elf_hash_entry (htab->tls_get_addr);
8082 if (opt != NULL && tga != NULL)
8083 {
8084 tga->root.type = bfd_link_hash_indirect;
8085 tga->root.u.i.link = &opt->root;
8086 tga->root.u.i.warning = NULL;
8087 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8088 opt->mark = 1;
8089 _bfd_elf_link_hash_hide_symbol (info, opt,
8090 tga->forced_local);
8091 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8092 }
8093 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8094 htab->tls_get_addr_fd->is_func_descriptor = 1;
8095 if (htab->tls_get_addr != NULL)
8096 {
8097 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8098 htab->tls_get_addr->is_func = 1;
8099 }
8100 }
8101 if (desc_fd != NULL)
8102 {
8103 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8104 if (opt != NULL && desc != NULL)
8105 {
8106 desc->root.type = bfd_link_hash_indirect;
8107 desc->root.u.i.link = &opt->root;
8108 desc->root.u.i.warning = NULL;
8109 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8110 opt->mark = 1;
8111 _bfd_elf_link_hash_hide_symbol (info, opt,
8112 desc->forced_local);
8113 htab->tga_desc = ppc_elf_hash_entry (opt);
8114 }
8115 htab->tga_desc_fd->oh = htab->tga_desc;
8116 htab->tga_desc_fd->is_func_descriptor = 1;
8117 if (htab->tga_desc != NULL)
8118 {
8119 htab->tga_desc->oh = htab->tga_desc_fd;
8120 htab->tga_desc->is_func = 1;
8121 }
8122 }
8123 }
8124 }
8125 }
8126 else if (htab->params->tls_get_addr_opt < 0)
8127 htab->params->tls_get_addr_opt = 0;
8128 }
8129
8130 if (htab->tga_desc_fd != NULL
8131 && htab->params->tls_get_addr_opt
8132 && htab->params->no_tls_get_addr_regsave == -1)
8133 htab->params->no_tls_get_addr_regsave = 0;
8134
8135 return true;
8136 }
8137
8138 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8139 any of HASH1, HASH2, HASH3, or HASH4. */
8140
8141 static bool
8142 branch_reloc_hash_match (bfd *ibfd,
8143 Elf_Internal_Rela *rel,
8144 struct ppc_link_hash_entry *hash1,
8145 struct ppc_link_hash_entry *hash2,
8146 struct ppc_link_hash_entry *hash3,
8147 struct ppc_link_hash_entry *hash4)
8148 {
8149 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8150 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8151 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8152
8153 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8154 {
8155 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8156 struct elf_link_hash_entry *h;
8157
8158 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8159 h = elf_follow_link (h);
8160 if (h == elf_hash_entry (hash1)
8161 || h == elf_hash_entry (hash2)
8162 || h == elf_hash_entry (hash3)
8163 || h == elf_hash_entry (hash4))
8164 return true;
8165 }
8166 return false;
8167 }
8168
8169 /* Run through all the TLS relocs looking for optimization
8170 opportunities. The linker has been hacked (see ppc64elf.em) to do
8171 a preliminary section layout so that we know the TLS segment
8172 offsets. We can't optimize earlier because some optimizations need
8173 to know the tp offset, and we need to optimize before allocating
8174 dynamic relocations. */
8175
8176 bool
8177 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8178 {
8179 bfd *ibfd;
8180 asection *sec;
8181 struct ppc_link_hash_table *htab;
8182 unsigned char *toc_ref;
8183 int pass;
8184
8185 if (!bfd_link_executable (info))
8186 return true;
8187
8188 htab = ppc_hash_table (info);
8189 if (htab == NULL)
8190 return false;
8191
8192 htab->do_tls_opt = 1;
8193
8194 /* Make two passes over the relocs. On the first pass, mark toc
8195 entries involved with tls relocs, and check that tls relocs
8196 involved in setting up a tls_get_addr call are indeed followed by
8197 such a call. If they are not, we can't do any tls optimization.
8198 On the second pass twiddle tls_mask flags to notify
8199 relocate_section that optimization can be done, and adjust got
8200 and plt refcounts. */
8201 toc_ref = NULL;
8202 for (pass = 0; pass < 2; ++pass)
8203 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8204 {
8205 Elf_Internal_Sym *locsyms = NULL;
8206 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8207
8208 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8209 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8210 {
8211 Elf_Internal_Rela *relstart, *rel, *relend;
8212 bool found_tls_get_addr_arg = 0;
8213
8214 /* Read the relocations. */
8215 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8216 info->keep_memory);
8217 if (relstart == NULL)
8218 {
8219 free (toc_ref);
8220 return false;
8221 }
8222
8223 relend = relstart + sec->reloc_count;
8224 for (rel = relstart; rel < relend; rel++)
8225 {
8226 enum elf_ppc64_reloc_type r_type;
8227 unsigned long r_symndx;
8228 struct elf_link_hash_entry *h;
8229 Elf_Internal_Sym *sym;
8230 asection *sym_sec;
8231 unsigned char *tls_mask;
8232 unsigned int tls_set, tls_clear, tls_type = 0;
8233 bfd_vma value;
8234 bool ok_tprel, is_local;
8235 long toc_ref_index = 0;
8236 int expecting_tls_get_addr = 0;
8237 bool ret = false;
8238
8239 r_symndx = ELF64_R_SYM (rel->r_info);
8240 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8241 r_symndx, ibfd))
8242 {
8243 err_free_rel:
8244 if (elf_section_data (sec)->relocs != relstart)
8245 free (relstart);
8246 free (toc_ref);
8247 if (elf_symtab_hdr (ibfd).contents
8248 != (unsigned char *) locsyms)
8249 free (locsyms);
8250 return ret;
8251 }
8252
8253 if (h != NULL)
8254 {
8255 if (h->root.type == bfd_link_hash_defined
8256 || h->root.type == bfd_link_hash_defweak)
8257 value = h->root.u.def.value;
8258 else if (h->root.type == bfd_link_hash_undefweak)
8259 value = 0;
8260 else
8261 {
8262 found_tls_get_addr_arg = 0;
8263 continue;
8264 }
8265 }
8266 else
8267 /* Symbols referenced by TLS relocs must be of type
8268 STT_TLS. So no need for .opd local sym adjust. */
8269 value = sym->st_value;
8270
8271 ok_tprel = false;
8272 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8273 if (is_local)
8274 {
8275 if (h != NULL
8276 && h->root.type == bfd_link_hash_undefweak)
8277 ok_tprel = true;
8278 else if (sym_sec != NULL
8279 && sym_sec->output_section != NULL)
8280 {
8281 value += sym_sec->output_offset;
8282 value += sym_sec->output_section->vma;
8283 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8284 /* Note that even though the prefix insns
8285 allow a 1<<33 offset we use the same test
8286 as for addis;addi. There may be a mix of
8287 pcrel and non-pcrel code and the decision
8288 to optimise is per symbol, not per TLS
8289 sequence. */
8290 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8291 }
8292 }
8293
8294 r_type = ELF64_R_TYPE (rel->r_info);
8295 /* If this section has old-style __tls_get_addr calls
8296 without marker relocs, then check that each
8297 __tls_get_addr call reloc is preceded by a reloc
8298 that conceivably belongs to the __tls_get_addr arg
8299 setup insn. If we don't find matching arg setup
8300 relocs, don't do any tls optimization. */
8301 if (pass == 0
8302 && sec->nomark_tls_get_addr
8303 && h != NULL
8304 && is_tls_get_addr (h, htab)
8305 && !found_tls_get_addr_arg
8306 && is_branch_reloc (r_type))
8307 {
8308 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8309 "TLS optimization disabled\n"),
8310 ibfd, sec, rel->r_offset);
8311 ret = true;
8312 goto err_free_rel;
8313 }
8314
8315 found_tls_get_addr_arg = 0;
8316 switch (r_type)
8317 {
8318 case R_PPC64_GOT_TLSLD16:
8319 case R_PPC64_GOT_TLSLD16_LO:
8320 case R_PPC64_GOT_TLSLD_PCREL34:
8321 expecting_tls_get_addr = 1;
8322 found_tls_get_addr_arg = 1;
8323 /* Fall through. */
8324
8325 case R_PPC64_GOT_TLSLD16_HI:
8326 case R_PPC64_GOT_TLSLD16_HA:
8327 /* These relocs should never be against a symbol
8328 defined in a shared lib. Leave them alone if
8329 that turns out to be the case. */
8330 if (!is_local)
8331 continue;
8332
8333 /* LD -> LE */
8334 tls_set = 0;
8335 tls_clear = TLS_LD;
8336 tls_type = TLS_TLS | TLS_LD;
8337 break;
8338
8339 case R_PPC64_GOT_TLSGD16:
8340 case R_PPC64_GOT_TLSGD16_LO:
8341 case R_PPC64_GOT_TLSGD_PCREL34:
8342 expecting_tls_get_addr = 1;
8343 found_tls_get_addr_arg = 1;
8344 /* Fall through. */
8345
8346 case R_PPC64_GOT_TLSGD16_HI:
8347 case R_PPC64_GOT_TLSGD16_HA:
8348 if (ok_tprel)
8349 /* GD -> LE */
8350 tls_set = 0;
8351 else
8352 /* GD -> IE */
8353 tls_set = TLS_TLS | TLS_GDIE;
8354 tls_clear = TLS_GD;
8355 tls_type = TLS_TLS | TLS_GD;
8356 break;
8357
8358 case R_PPC64_GOT_TPREL_PCREL34:
8359 case R_PPC64_GOT_TPREL16_DS:
8360 case R_PPC64_GOT_TPREL16_LO_DS:
8361 case R_PPC64_GOT_TPREL16_HI:
8362 case R_PPC64_GOT_TPREL16_HA:
8363 if (ok_tprel)
8364 {
8365 /* IE -> LE */
8366 tls_set = 0;
8367 tls_clear = TLS_TPREL;
8368 tls_type = TLS_TLS | TLS_TPREL;
8369 break;
8370 }
8371 continue;
8372
8373 case R_PPC64_TLSLD:
8374 if (!is_local)
8375 continue;
8376 /* Fall through. */
8377 case R_PPC64_TLSGD:
8378 if (rel + 1 < relend
8379 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8380 {
8381 if (pass != 0
8382 && (ELF64_R_TYPE (rel[1].r_info)
8383 != R_PPC64_PLTSEQ)
8384 && (ELF64_R_TYPE (rel[1].r_info)
8385 != R_PPC64_PLTSEQ_NOTOC))
8386 {
8387 r_symndx = ELF64_R_SYM (rel[1].r_info);
8388 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8389 r_symndx, ibfd))
8390 goto err_free_rel;
8391 if (h != NULL)
8392 {
8393 struct plt_entry *ent = NULL;
8394
8395 for (ent = h->plt.plist;
8396 ent != NULL;
8397 ent = ent->next)
8398 if (ent->addend == rel[1].r_addend)
8399 break;
8400
8401 if (ent != NULL
8402 && ent->plt.refcount > 0)
8403 ent->plt.refcount -= 1;
8404 }
8405 }
8406 continue;
8407 }
8408 found_tls_get_addr_arg = 1;
8409 /* Fall through. */
8410
8411 case R_PPC64_TLS:
8412 case R_PPC64_TOC16:
8413 case R_PPC64_TOC16_LO:
8414 if (sym_sec == NULL || sym_sec != toc)
8415 continue;
8416
8417 /* Mark this toc entry as referenced by a TLS
8418 code sequence. We can do that now in the
8419 case of R_PPC64_TLS, and after checking for
8420 tls_get_addr for the TOC16 relocs. */
8421 if (toc_ref == NULL)
8422 toc_ref
8423 = bfd_zmalloc (toc->output_section->rawsize / 8);
8424 if (toc_ref == NULL)
8425 goto err_free_rel;
8426
8427 if (h != NULL)
8428 value = h->root.u.def.value;
8429 else
8430 value = sym->st_value;
8431 value += rel->r_addend;
8432 if (value % 8 != 0)
8433 continue;
8434 BFD_ASSERT (value < toc->size
8435 && toc->output_offset % 8 == 0);
8436 toc_ref_index = (value + toc->output_offset) / 8;
8437 if (r_type == R_PPC64_TLS
8438 || r_type == R_PPC64_TLSGD
8439 || r_type == R_PPC64_TLSLD)
8440 {
8441 toc_ref[toc_ref_index] = 1;
8442 continue;
8443 }
8444
8445 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8446 continue;
8447
8448 tls_set = 0;
8449 tls_clear = 0;
8450 expecting_tls_get_addr = 2;
8451 break;
8452
8453 case R_PPC64_TPREL64:
8454 if (pass == 0
8455 || sec != toc
8456 || toc_ref == NULL
8457 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8458 continue;
8459 if (ok_tprel)
8460 {
8461 /* IE -> LE */
8462 tls_set = TLS_EXPLICIT;
8463 tls_clear = TLS_TPREL;
8464 break;
8465 }
8466 continue;
8467
8468 case R_PPC64_DTPMOD64:
8469 if (pass == 0
8470 || sec != toc
8471 || toc_ref == NULL
8472 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8473 continue;
8474 if (rel + 1 < relend
8475 && (rel[1].r_info
8476 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8477 && rel[1].r_offset == rel->r_offset + 8)
8478 {
8479 if (ok_tprel)
8480 /* GD -> LE */
8481 tls_set = TLS_EXPLICIT | TLS_GD;
8482 else
8483 /* GD -> IE */
8484 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8485 tls_clear = TLS_GD;
8486 }
8487 else
8488 {
8489 if (!is_local)
8490 continue;
8491
8492 /* LD -> LE */
8493 tls_set = TLS_EXPLICIT;
8494 tls_clear = TLS_LD;
8495 }
8496 break;
8497
8498 case R_PPC64_TPREL16_HA:
8499 if (pass == 0)
8500 {
8501 unsigned char buf[4];
8502 unsigned int insn;
8503 bfd_vma off = rel->r_offset & ~3;
8504 if (!bfd_get_section_contents (ibfd, sec, buf,
8505 off, 4))
8506 goto err_free_rel;
8507 insn = bfd_get_32 (ibfd, buf);
8508 /* addis rt,13,imm */
8509 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8510 != ((15u << 26) | (13 << 16)))
8511 {
8512 /* xgettext:c-format */
8513 info->callbacks->minfo
8514 (_("%H: warning: %s unexpected insn %#x.\n"),
8515 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8516 htab->do_tls_opt = 0;
8517 }
8518 }
8519 continue;
8520
8521 case R_PPC64_TPREL16_HI:
8522 case R_PPC64_TPREL16_HIGH:
8523 case R_PPC64_TPREL16_HIGHA:
8524 case R_PPC64_TPREL16_HIGHER:
8525 case R_PPC64_TPREL16_HIGHERA:
8526 case R_PPC64_TPREL16_HIGHEST:
8527 case R_PPC64_TPREL16_HIGHESTA:
8528 /* These can all be used in sequences along with
8529 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8530 able to verify easily. */
8531 htab->do_tls_opt = 0;
8532 continue;
8533
8534 default:
8535 continue;
8536 }
8537
8538 if (pass == 0)
8539 {
8540 if (!expecting_tls_get_addr
8541 || !sec->nomark_tls_get_addr)
8542 continue;
8543
8544 if (rel + 1 < relend
8545 && branch_reloc_hash_match (ibfd, rel + 1,
8546 htab->tls_get_addr_fd,
8547 htab->tga_desc_fd,
8548 htab->tls_get_addr,
8549 htab->tga_desc))
8550 {
8551 if (expecting_tls_get_addr == 2)
8552 {
8553 /* Check for toc tls entries. */
8554 unsigned char *toc_tls;
8555 int retval;
8556
8557 retval = get_tls_mask (&toc_tls, NULL, NULL,
8558 &locsyms,
8559 rel, ibfd);
8560 if (retval == 0)
8561 goto err_free_rel;
8562 if (toc_tls != NULL)
8563 {
8564 if ((*toc_tls & TLS_TLS) != 0
8565 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8566 found_tls_get_addr_arg = 1;
8567 if (retval > 1)
8568 toc_ref[toc_ref_index] = 1;
8569 }
8570 }
8571 continue;
8572 }
8573
8574 /* Uh oh, we didn't find the expected call. We
8575 could just mark this symbol to exclude it
8576 from tls optimization but it's safer to skip
8577 the entire optimization. */
8578 /* xgettext:c-format */
8579 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8580 "TLS optimization disabled\n"),
8581 ibfd, sec, rel->r_offset);
8582 ret = true;
8583 goto err_free_rel;
8584 }
8585
8586 /* If we don't have old-style __tls_get_addr calls
8587 without TLSGD/TLSLD marker relocs, and we haven't
8588 found a new-style __tls_get_addr call with a
8589 marker for this symbol, then we either have a
8590 broken object file or an -mlongcall style
8591 indirect call to __tls_get_addr without a marker.
8592 Disable optimization in this case. */
8593 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8594 && (tls_set & TLS_EXPLICIT) == 0
8595 && !sec->nomark_tls_get_addr
8596 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8597 != (TLS_TLS | TLS_MARK)))
8598 continue;
8599
8600 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8601 {
8602 struct plt_entry *ent = NULL;
8603
8604 if (htab->tls_get_addr_fd != NULL)
8605 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8606 ent != NULL;
8607 ent = ent->next)
8608 if (ent->addend == 0)
8609 break;
8610
8611 if (ent == NULL && htab->tga_desc_fd != NULL)
8612 for (ent = htab->tga_desc_fd->elf.plt.plist;
8613 ent != NULL;
8614 ent = ent->next)
8615 if (ent->addend == 0)
8616 break;
8617
8618 if (ent == NULL && htab->tls_get_addr != NULL)
8619 for (ent = htab->tls_get_addr->elf.plt.plist;
8620 ent != NULL;
8621 ent = ent->next)
8622 if (ent->addend == 0)
8623 break;
8624
8625 if (ent == NULL && htab->tga_desc != NULL)
8626 for (ent = htab->tga_desc->elf.plt.plist;
8627 ent != NULL;
8628 ent = ent->next)
8629 if (ent->addend == 0)
8630 break;
8631
8632 if (ent != NULL
8633 && ent->plt.refcount > 0)
8634 ent->plt.refcount -= 1;
8635 }
8636
8637 if (tls_clear == 0)
8638 continue;
8639
8640 if ((tls_set & TLS_EXPLICIT) == 0)
8641 {
8642 struct got_entry *ent;
8643
8644 /* Adjust got entry for this reloc. */
8645 if (h != NULL)
8646 ent = h->got.glist;
8647 else
8648 ent = elf_local_got_ents (ibfd)[r_symndx];
8649
8650 for (; ent != NULL; ent = ent->next)
8651 if (ent->addend == rel->r_addend
8652 && ent->owner == ibfd
8653 && ent->tls_type == tls_type)
8654 break;
8655 if (ent == NULL)
8656 abort ();
8657
8658 if (tls_set == 0)
8659 {
8660 /* We managed to get rid of a got entry. */
8661 if (ent->got.refcount > 0)
8662 ent->got.refcount -= 1;
8663 }
8664 }
8665 else
8666 {
8667 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8668 we'll lose one or two dyn relocs. */
8669 if (!dec_dynrel_count (rel, sec, info,
8670 NULL, h, sym))
8671 return false;
8672
8673 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8674 {
8675 if (!dec_dynrel_count (rel + 1, sec, info,
8676 NULL, h, sym))
8677 return false;
8678 }
8679 }
8680
8681 *tls_mask |= tls_set & 0xff;
8682 *tls_mask &= ~tls_clear;
8683 }
8684
8685 if (elf_section_data (sec)->relocs != relstart)
8686 free (relstart);
8687 }
8688
8689 if (locsyms != NULL
8690 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8691 {
8692 if (!info->keep_memory)
8693 free (locsyms);
8694 else
8695 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8696 }
8697 }
8698
8699 free (toc_ref);
8700 return true;
8701 }
8702
8703 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8704 the values of any global symbols in a toc section that has been
8705 edited. Globals in toc sections should be a rarity, so this function
8706 sets a flag if any are found in toc sections other than the one just
8707 edited, so that further hash table traversals can be avoided. */
8708
8709 struct adjust_toc_info
8710 {
8711 asection *toc;
8712 unsigned long *skip;
8713 bool global_toc_syms;
8714 };
8715
8716 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8717
8718 static bool
8719 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8720 {
8721 struct ppc_link_hash_entry *eh;
8722 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8723 unsigned long i;
8724
8725 if (h->root.type != bfd_link_hash_defined
8726 && h->root.type != bfd_link_hash_defweak)
8727 return true;
8728
8729 eh = ppc_elf_hash_entry (h);
8730 if (eh->adjust_done)
8731 return true;
8732
8733 if (eh->elf.root.u.def.section == toc_inf->toc)
8734 {
8735 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8736 i = toc_inf->toc->rawsize >> 3;
8737 else
8738 i = eh->elf.root.u.def.value >> 3;
8739
8740 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8741 {
8742 _bfd_error_handler
8743 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8744 do
8745 ++i;
8746 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8747 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8748 }
8749
8750 eh->elf.root.u.def.value -= toc_inf->skip[i];
8751 eh->adjust_done = 1;
8752 }
8753 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8754 toc_inf->global_toc_syms = true;
8755
8756 return true;
8757 }
8758
8759 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8760 on a _LO variety toc/got reloc. */
8761
8762 static bool
8763 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8764 {
8765 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8766 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8767 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8768 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8769 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8770 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8771 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8772 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8773 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8774 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8775 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8776 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8777 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8778 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8779 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8780 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8781 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8782 /* Exclude lfqu by testing reloc. If relocs are ever
8783 defined for the reduced D field in psq_lu then those
8784 will need testing too. */
8785 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8786 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8787 && (insn & 1) == 0)
8788 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8789 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8790 /* Exclude stfqu. psq_stu as above for psq_lu. */
8791 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8792 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8793 && (insn & 1) == 0));
8794 }
8795
8796 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8797 pld ra,symbol@got@pcrel
8798 load/store rt,off(ra)
8799 or
8800 pla ra,symbol@pcrel
8801 load/store rt,off(ra)
8802 may be translated to
8803 pload/pstore rt,symbol+off@pcrel
8804 nop.
8805 This function returns true if the optimization is possible, placing
8806 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8807
8808 On entry to this function, the linker has already determined that
8809 the pld can be replaced with pla: *PINSN1 is that pla insn,
8810 while *PINSN2 is the second instruction. */
8811
8812 static bool
8813 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8814 {
8815 uint64_t insn1 = *pinsn1;
8816 uint64_t insn2 = *pinsn2;
8817 bfd_signed_vma off;
8818
8819 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8820 {
8821 /* Check that regs match. */
8822 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8823 return false;
8824
8825 /* P8LS or PMLS form, non-pcrel. */
8826 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8827 return false;
8828
8829 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8830 *pinsn2 = PNOP;
8831 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8832 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8833 return true;
8834 }
8835
8836 insn2 >>= 32;
8837
8838 /* Check that regs match. */
8839 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8840 return false;
8841
8842 switch ((insn2 >> 26) & 63)
8843 {
8844 default:
8845 return false;
8846
8847 case 32: /* lwz */
8848 case 34: /* lbz */
8849 case 36: /* stw */
8850 case 38: /* stb */
8851 case 40: /* lhz */
8852 case 42: /* lha */
8853 case 44: /* sth */
8854 case 48: /* lfs */
8855 case 50: /* lfd */
8856 case 52: /* stfs */
8857 case 54: /* stfd */
8858 /* These are the PMLS cases, where we just need to tack a prefix
8859 on the insn. */
8860 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8861 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8862 off = insn2 & 0xffff;
8863 break;
8864
8865 case 58: /* lwa, ld */
8866 if ((insn2 & 1) != 0)
8867 return false;
8868 insn1 = ((1ULL << 58) | (1ULL << 52)
8869 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8870 | (insn2 & (31ULL << 21)));
8871 off = insn2 & 0xfffc;
8872 break;
8873
8874 case 57: /* lxsd, lxssp */
8875 if ((insn2 & 3) < 2)
8876 return false;
8877 insn1 = ((1ULL << 58) | (1ULL << 52)
8878 | ((40ULL | (insn2 & 3)) << 26)
8879 | (insn2 & (31ULL << 21)));
8880 off = insn2 & 0xfffc;
8881 break;
8882
8883 case 61: /* stxsd, stxssp, lxv, stxv */
8884 if ((insn2 & 3) == 0)
8885 return false;
8886 else if ((insn2 & 3) >= 2)
8887 {
8888 insn1 = ((1ULL << 58) | (1ULL << 52)
8889 | ((44ULL | (insn2 & 3)) << 26)
8890 | (insn2 & (31ULL << 21)));
8891 off = insn2 & 0xfffc;
8892 }
8893 else
8894 {
8895 insn1 = ((1ULL << 58) | (1ULL << 52)
8896 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8897 | (insn2 & (31ULL << 21)));
8898 off = insn2 & 0xfff0;
8899 }
8900 break;
8901
8902 case 56: /* lq */
8903 insn1 = ((1ULL << 58) | (1ULL << 52)
8904 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8905 off = insn2 & 0xffff;
8906 break;
8907
8908 case 6: /* lxvp, stxvp */
8909 if ((insn2 & 0xe) != 0)
8910 return false;
8911 insn1 = ((1ULL << 58) | (1ULL << 52)
8912 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8913 | (insn2 & (31ULL << 21)));
8914 off = insn2 & 0xfff0;
8915 break;
8916
8917 case 62: /* std, stq */
8918 if ((insn2 & 1) != 0)
8919 return false;
8920 insn1 = ((1ULL << 58) | (1ULL << 52)
8921 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8922 | (insn2 & (31ULL << 21)));
8923 off = insn2 & 0xfffc;
8924 break;
8925 }
8926
8927 *pinsn1 = insn1;
8928 *pinsn2 = (uint64_t) NOP << 32;
8929 *poff = (off ^ 0x8000) - 0x8000;
8930 return true;
8931 }
8932
8933 /* Examine all relocs referencing .toc sections in order to remove
8934 unused .toc entries. */
8935
8936 bool
8937 ppc64_elf_edit_toc (struct bfd_link_info *info)
8938 {
8939 bfd *ibfd;
8940 struct adjust_toc_info toc_inf;
8941 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8942
8943 htab->do_toc_opt = 1;
8944 toc_inf.global_toc_syms = true;
8945 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8946 {
8947 asection *toc, *sec;
8948 Elf_Internal_Shdr *symtab_hdr;
8949 Elf_Internal_Sym *local_syms;
8950 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8951 unsigned long *skip, *drop;
8952 unsigned char *used;
8953 unsigned char *keep, last, some_unused;
8954
8955 if (!is_ppc64_elf (ibfd))
8956 continue;
8957
8958 toc = bfd_get_section_by_name (ibfd, ".toc");
8959 if (toc == NULL
8960 || toc->size == 0
8961 || (toc->flags & SEC_HAS_CONTENTS) == 0
8962 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8963 || discarded_section (toc))
8964 continue;
8965
8966 toc_relocs = NULL;
8967 local_syms = NULL;
8968 symtab_hdr = &elf_symtab_hdr (ibfd);
8969
8970 /* Look at sections dropped from the final link. */
8971 skip = NULL;
8972 relstart = NULL;
8973 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8974 {
8975 if (sec->reloc_count == 0
8976 || !discarded_section (sec)
8977 || get_opd_info (sec)
8978 || (sec->flags & SEC_ALLOC) == 0
8979 || (sec->flags & SEC_DEBUGGING) != 0)
8980 continue;
8981
8982 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8983 if (relstart == NULL)
8984 goto error_ret;
8985
8986 /* Run through the relocs to see which toc entries might be
8987 unused. */
8988 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8989 {
8990 enum elf_ppc64_reloc_type r_type;
8991 unsigned long r_symndx;
8992 asection *sym_sec;
8993 struct elf_link_hash_entry *h;
8994 Elf_Internal_Sym *sym;
8995 bfd_vma val;
8996
8997 r_type = ELF64_R_TYPE (rel->r_info);
8998 switch (r_type)
8999 {
9000 default:
9001 continue;
9002
9003 case R_PPC64_TOC16:
9004 case R_PPC64_TOC16_LO:
9005 case R_PPC64_TOC16_HI:
9006 case R_PPC64_TOC16_HA:
9007 case R_PPC64_TOC16_DS:
9008 case R_PPC64_TOC16_LO_DS:
9009 break;
9010 }
9011
9012 r_symndx = ELF64_R_SYM (rel->r_info);
9013 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9014 r_symndx, ibfd))
9015 goto error_ret;
9016
9017 if (sym_sec != toc)
9018 continue;
9019
9020 if (h != NULL)
9021 val = h->root.u.def.value;
9022 else
9023 val = sym->st_value;
9024 val += rel->r_addend;
9025
9026 if (val >= toc->size)
9027 continue;
9028
9029 /* Anything in the toc ought to be aligned to 8 bytes.
9030 If not, don't mark as unused. */
9031 if (val & 7)
9032 continue;
9033
9034 if (skip == NULL)
9035 {
9036 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9037 if (skip == NULL)
9038 goto error_ret;
9039 }
9040
9041 skip[val >> 3] = ref_from_discarded;
9042 }
9043
9044 if (elf_section_data (sec)->relocs != relstart)
9045 free (relstart);
9046 }
9047
9048 /* For largetoc loads of address constants, we can convert
9049 . addis rx,2,addr@got@ha
9050 . ld ry,addr@got@l(rx)
9051 to
9052 . addis rx,2,addr@toc@ha
9053 . addi ry,rx,addr@toc@l
9054 when addr is within 2G of the toc pointer. This then means
9055 that the word storing "addr" in the toc is no longer needed. */
9056
9057 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9058 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9059 && toc->reloc_count != 0)
9060 {
9061 /* Read toc relocs. */
9062 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9063 info->keep_memory);
9064 if (toc_relocs == NULL)
9065 goto error_ret;
9066
9067 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9068 {
9069 enum elf_ppc64_reloc_type r_type;
9070 unsigned long r_symndx;
9071 asection *sym_sec;
9072 struct elf_link_hash_entry *h;
9073 Elf_Internal_Sym *sym;
9074 bfd_vma val, addr;
9075
9076 r_type = ELF64_R_TYPE (rel->r_info);
9077 if (r_type != R_PPC64_ADDR64)
9078 continue;
9079
9080 r_symndx = ELF64_R_SYM (rel->r_info);
9081 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9082 r_symndx, ibfd))
9083 goto error_ret;
9084
9085 if (sym_sec == NULL
9086 || sym_sec->output_section == NULL
9087 || discarded_section (sym_sec))
9088 continue;
9089
9090 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9091 || (bfd_link_pic (info)
9092 && sym_sec == bfd_abs_section_ptr))
9093 continue;
9094
9095 if (h != NULL)
9096 {
9097 if (h->type == STT_GNU_IFUNC)
9098 continue;
9099 val = h->root.u.def.value;
9100 }
9101 else
9102 {
9103 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9104 continue;
9105 val = sym->st_value;
9106 }
9107 val += rel->r_addend;
9108 val += sym_sec->output_section->vma + sym_sec->output_offset;
9109
9110 /* We don't yet know the exact toc pointer value, but we
9111 know it will be somewhere in the toc section. Don't
9112 optimize if the difference from any possible toc
9113 pointer is outside [ff..f80008000, 7fff7fff]. */
9114 addr = toc->output_section->vma + TOC_BASE_OFF;
9115 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9116 continue;
9117
9118 addr = toc->output_section->vma + toc->output_section->rawsize;
9119 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9120 continue;
9121
9122 if (skip == NULL)
9123 {
9124 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9125 if (skip == NULL)
9126 goto error_ret;
9127 }
9128
9129 skip[rel->r_offset >> 3]
9130 |= can_optimize | ((rel - toc_relocs) << 2);
9131 }
9132 }
9133
9134 if (skip == NULL)
9135 continue;
9136
9137 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9138 if (used == NULL)
9139 {
9140 error_ret:
9141 if (symtab_hdr->contents != (unsigned char *) local_syms)
9142 free (local_syms);
9143 if (sec != NULL
9144 && elf_section_data (sec)->relocs != relstart)
9145 free (relstart);
9146 if (elf_section_data (toc)->relocs != toc_relocs)
9147 free (toc_relocs);
9148 free (skip);
9149 return false;
9150 }
9151
9152 /* Now check all kept sections that might reference the toc.
9153 Check the toc itself last. */
9154 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9155 : ibfd->sections);
9156 sec != NULL;
9157 sec = (sec == toc ? NULL
9158 : sec->next == NULL ? toc
9159 : sec->next == toc && toc->next ? toc->next
9160 : sec->next))
9161 {
9162 int repeat;
9163
9164 if (sec->reloc_count == 0
9165 || discarded_section (sec)
9166 || get_opd_info (sec)
9167 || (sec->flags & SEC_ALLOC) == 0
9168 || (sec->flags & SEC_DEBUGGING) != 0)
9169 continue;
9170
9171 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9172 info->keep_memory);
9173 if (relstart == NULL)
9174 {
9175 free (used);
9176 goto error_ret;
9177 }
9178
9179 /* Mark toc entries referenced as used. */
9180 do
9181 {
9182 repeat = 0;
9183 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9184 {
9185 enum elf_ppc64_reloc_type r_type;
9186 unsigned long r_symndx;
9187 asection *sym_sec;
9188 struct elf_link_hash_entry *h;
9189 Elf_Internal_Sym *sym;
9190 bfd_vma val;
9191
9192 r_type = ELF64_R_TYPE (rel->r_info);
9193 switch (r_type)
9194 {
9195 case R_PPC64_TOC16:
9196 case R_PPC64_TOC16_LO:
9197 case R_PPC64_TOC16_HI:
9198 case R_PPC64_TOC16_HA:
9199 case R_PPC64_TOC16_DS:
9200 case R_PPC64_TOC16_LO_DS:
9201 /* In case we're taking addresses of toc entries. */
9202 case R_PPC64_ADDR64:
9203 break;
9204
9205 default:
9206 continue;
9207 }
9208
9209 r_symndx = ELF64_R_SYM (rel->r_info);
9210 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9211 r_symndx, ibfd))
9212 {
9213 free (used);
9214 goto error_ret;
9215 }
9216
9217 if (sym_sec != toc)
9218 continue;
9219
9220 if (h != NULL)
9221 val = h->root.u.def.value;
9222 else
9223 val = sym->st_value;
9224 val += rel->r_addend;
9225
9226 if (val >= toc->size)
9227 continue;
9228
9229 if ((skip[val >> 3] & can_optimize) != 0)
9230 {
9231 bfd_vma off;
9232 unsigned char opc;
9233
9234 switch (r_type)
9235 {
9236 case R_PPC64_TOC16_HA:
9237 break;
9238
9239 case R_PPC64_TOC16_LO_DS:
9240 off = rel->r_offset;
9241 off += (bfd_big_endian (ibfd) ? -2 : 3);
9242 if (!bfd_get_section_contents (ibfd, sec, &opc,
9243 off, 1))
9244 {
9245 free (used);
9246 goto error_ret;
9247 }
9248 if ((opc & (0x3f << 2)) == (58u << 2))
9249 break;
9250 /* Fall through. */
9251
9252 default:
9253 /* Wrong sort of reloc, or not a ld. We may
9254 as well clear ref_from_discarded too. */
9255 skip[val >> 3] = 0;
9256 }
9257 }
9258
9259 if (sec != toc)
9260 used[val >> 3] = 1;
9261 /* For the toc section, we only mark as used if this
9262 entry itself isn't unused. */
9263 else if ((used[rel->r_offset >> 3]
9264 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9265 && !used[val >> 3])
9266 {
9267 /* Do all the relocs again, to catch reference
9268 chains. */
9269 repeat = 1;
9270 used[val >> 3] = 1;
9271 }
9272 }
9273 }
9274 while (repeat);
9275
9276 if (elf_section_data (sec)->relocs != relstart)
9277 free (relstart);
9278 }
9279
9280 /* Merge the used and skip arrays. Assume that TOC
9281 doublewords not appearing as either used or unused belong
9282 to an entry more than one doubleword in size. */
9283 for (drop = skip, keep = used, last = 0, some_unused = 0;
9284 drop < skip + (toc->size + 7) / 8;
9285 ++drop, ++keep)
9286 {
9287 if (*keep)
9288 {
9289 *drop &= ~ref_from_discarded;
9290 if ((*drop & can_optimize) != 0)
9291 some_unused = 1;
9292 last = 0;
9293 }
9294 else if ((*drop & ref_from_discarded) != 0)
9295 {
9296 some_unused = 1;
9297 last = ref_from_discarded;
9298 }
9299 else
9300 *drop = last;
9301 }
9302
9303 free (used);
9304
9305 if (some_unused)
9306 {
9307 bfd_byte *contents, *src;
9308 unsigned long off;
9309 Elf_Internal_Sym *sym;
9310 bool local_toc_syms = false;
9311
9312 /* Shuffle the toc contents, and at the same time convert the
9313 skip array from booleans into offsets. */
9314 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9315 goto error_ret;
9316
9317 elf_section_data (toc)->this_hdr.contents = contents;
9318
9319 for (src = contents, off = 0, drop = skip;
9320 src < contents + toc->size;
9321 src += 8, ++drop)
9322 {
9323 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9324 off += 8;
9325 else if (off != 0)
9326 {
9327 *drop = off;
9328 memcpy (src - off, src, 8);
9329 }
9330 }
9331 *drop = off;
9332 toc->rawsize = toc->size;
9333 toc->size = src - contents - off;
9334
9335 /* Adjust addends for relocs against the toc section sym,
9336 and optimize any accesses we can. */
9337 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9338 {
9339 if (sec->reloc_count == 0
9340 || discarded_section (sec))
9341 continue;
9342
9343 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9344 info->keep_memory);
9345 if (relstart == NULL)
9346 goto error_ret;
9347
9348 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9349 {
9350 enum elf_ppc64_reloc_type r_type;
9351 unsigned long r_symndx;
9352 asection *sym_sec;
9353 struct elf_link_hash_entry *h;
9354 bfd_vma val;
9355
9356 r_type = ELF64_R_TYPE (rel->r_info);
9357 switch (r_type)
9358 {
9359 default:
9360 continue;
9361
9362 case R_PPC64_TOC16:
9363 case R_PPC64_TOC16_LO:
9364 case R_PPC64_TOC16_HI:
9365 case R_PPC64_TOC16_HA:
9366 case R_PPC64_TOC16_DS:
9367 case R_PPC64_TOC16_LO_DS:
9368 case R_PPC64_ADDR64:
9369 break;
9370 }
9371
9372 r_symndx = ELF64_R_SYM (rel->r_info);
9373 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9374 r_symndx, ibfd))
9375 goto error_ret;
9376
9377 if (sym_sec != toc)
9378 continue;
9379
9380 if (h != NULL)
9381 val = h->root.u.def.value;
9382 else
9383 {
9384 val = sym->st_value;
9385 if (val != 0)
9386 local_toc_syms = true;
9387 }
9388
9389 val += rel->r_addend;
9390
9391 if (val > toc->rawsize)
9392 val = toc->rawsize;
9393 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9394 continue;
9395 else if ((skip[val >> 3] & can_optimize) != 0)
9396 {
9397 Elf_Internal_Rela *tocrel
9398 = toc_relocs + (skip[val >> 3] >> 2);
9399 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9400
9401 switch (r_type)
9402 {
9403 case R_PPC64_TOC16_HA:
9404 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9405 break;
9406
9407 case R_PPC64_TOC16_LO_DS:
9408 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9409 break;
9410
9411 default:
9412 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9413 ppc_howto_init ();
9414 info->callbacks->einfo
9415 /* xgettext:c-format */
9416 (_("%H: %s references "
9417 "optimized away TOC entry\n"),
9418 ibfd, sec, rel->r_offset,
9419 ppc64_elf_howto_table[r_type]->name);
9420 bfd_set_error (bfd_error_bad_value);
9421 goto error_ret;
9422 }
9423 rel->r_addend = tocrel->r_addend;
9424 elf_section_data (sec)->relocs = relstart;
9425 continue;
9426 }
9427
9428 if (h != NULL || sym->st_value != 0)
9429 continue;
9430
9431 rel->r_addend -= skip[val >> 3];
9432 elf_section_data (sec)->relocs = relstart;
9433 }
9434
9435 if (elf_section_data (sec)->relocs != relstart)
9436 free (relstart);
9437 }
9438
9439 /* We shouldn't have local or global symbols defined in the TOC,
9440 but handle them anyway. */
9441 if (local_syms != NULL)
9442 for (sym = local_syms;
9443 sym < local_syms + symtab_hdr->sh_info;
9444 ++sym)
9445 if (sym->st_value != 0
9446 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9447 {
9448 unsigned long i;
9449
9450 if (sym->st_value > toc->rawsize)
9451 i = toc->rawsize >> 3;
9452 else
9453 i = sym->st_value >> 3;
9454
9455 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9456 {
9457 if (local_toc_syms)
9458 _bfd_error_handler
9459 (_("%s defined on removed toc entry"),
9460 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9461 do
9462 ++i;
9463 while ((skip[i] & (ref_from_discarded | can_optimize)));
9464 sym->st_value = (bfd_vma) i << 3;
9465 }
9466
9467 sym->st_value -= skip[i];
9468 symtab_hdr->contents = (unsigned char *) local_syms;
9469 }
9470
9471 /* Adjust any global syms defined in this toc input section. */
9472 if (toc_inf.global_toc_syms)
9473 {
9474 toc_inf.toc = toc;
9475 toc_inf.skip = skip;
9476 toc_inf.global_toc_syms = false;
9477 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9478 &toc_inf);
9479 }
9480
9481 if (toc->reloc_count != 0)
9482 {
9483 Elf_Internal_Shdr *rel_hdr;
9484 Elf_Internal_Rela *wrel;
9485 bfd_size_type sz;
9486
9487 /* Remove unused toc relocs, and adjust those we keep. */
9488 if (toc_relocs == NULL)
9489 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9490 info->keep_memory);
9491 if (toc_relocs == NULL)
9492 goto error_ret;
9493
9494 wrel = toc_relocs;
9495 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9496 if ((skip[rel->r_offset >> 3]
9497 & (ref_from_discarded | can_optimize)) == 0)
9498 {
9499 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9500 wrel->r_info = rel->r_info;
9501 wrel->r_addend = rel->r_addend;
9502 ++wrel;
9503 }
9504 else if (!dec_dynrel_count (rel, toc, info,
9505 &local_syms, NULL, NULL))
9506 goto error_ret;
9507
9508 elf_section_data (toc)->relocs = toc_relocs;
9509 toc->reloc_count = wrel - toc_relocs;
9510 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9511 sz = rel_hdr->sh_entsize;
9512 rel_hdr->sh_size = toc->reloc_count * sz;
9513 }
9514 }
9515 else if (elf_section_data (toc)->relocs != toc_relocs)
9516 free (toc_relocs);
9517
9518 if (local_syms != NULL
9519 && symtab_hdr->contents != (unsigned char *) local_syms)
9520 {
9521 if (!info->keep_memory)
9522 free (local_syms);
9523 else
9524 symtab_hdr->contents = (unsigned char *) local_syms;
9525 }
9526 free (skip);
9527 }
9528
9529 /* Look for cases where we can change an indirect GOT access to
9530 a GOT relative or PC relative access, possibly reducing the
9531 number of GOT entries. */
9532 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9533 {
9534 asection *sec;
9535 Elf_Internal_Shdr *symtab_hdr;
9536 Elf_Internal_Sym *local_syms;
9537 Elf_Internal_Rela *relstart, *rel;
9538 bfd_vma got;
9539
9540 if (!is_ppc64_elf (ibfd))
9541 continue;
9542
9543 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9544 continue;
9545
9546 sec = ppc64_elf_tdata (ibfd)->got;
9547 got = 0;
9548 if (sec != NULL)
9549 got = sec->output_section->vma + sec->output_offset + 0x8000;
9550
9551 local_syms = NULL;
9552 symtab_hdr = &elf_symtab_hdr (ibfd);
9553
9554 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9555 {
9556 if (sec->reloc_count == 0
9557 || !ppc64_elf_section_data (sec)->has_optrel
9558 || discarded_section (sec))
9559 continue;
9560
9561 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9562 info->keep_memory);
9563 if (relstart == NULL)
9564 {
9565 got_error_ret:
9566 if (symtab_hdr->contents != (unsigned char *) local_syms)
9567 free (local_syms);
9568 if (sec != NULL
9569 && elf_section_data (sec)->relocs != relstart)
9570 free (relstart);
9571 return false;
9572 }
9573
9574 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9575 {
9576 enum elf_ppc64_reloc_type r_type;
9577 unsigned long r_symndx;
9578 Elf_Internal_Sym *sym;
9579 asection *sym_sec;
9580 struct elf_link_hash_entry *h;
9581 struct got_entry *ent;
9582 bfd_vma val, pc;
9583 unsigned char buf[8];
9584 unsigned int insn;
9585 enum {no_check, check_lo, check_ha} insn_check;
9586
9587 r_type = ELF64_R_TYPE (rel->r_info);
9588 switch (r_type)
9589 {
9590 default:
9591 insn_check = no_check;
9592 break;
9593
9594 case R_PPC64_PLT16_HA:
9595 case R_PPC64_GOT_TLSLD16_HA:
9596 case R_PPC64_GOT_TLSGD16_HA:
9597 case R_PPC64_GOT_TPREL16_HA:
9598 case R_PPC64_GOT_DTPREL16_HA:
9599 case R_PPC64_GOT16_HA:
9600 case R_PPC64_TOC16_HA:
9601 insn_check = check_ha;
9602 break;
9603
9604 case R_PPC64_PLT16_LO:
9605 case R_PPC64_PLT16_LO_DS:
9606 case R_PPC64_GOT_TLSLD16_LO:
9607 case R_PPC64_GOT_TLSGD16_LO:
9608 case R_PPC64_GOT_TPREL16_LO_DS:
9609 case R_PPC64_GOT_DTPREL16_LO_DS:
9610 case R_PPC64_GOT16_LO:
9611 case R_PPC64_GOT16_LO_DS:
9612 case R_PPC64_TOC16_LO:
9613 case R_PPC64_TOC16_LO_DS:
9614 insn_check = check_lo;
9615 break;
9616 }
9617
9618 if (insn_check != no_check)
9619 {
9620 bfd_vma off = rel->r_offset & ~3;
9621
9622 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9623 goto got_error_ret;
9624
9625 insn = bfd_get_32 (ibfd, buf);
9626 if (insn_check == check_lo
9627 ? !ok_lo_toc_insn (insn, r_type)
9628 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9629 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9630 {
9631 char str[12];
9632
9633 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9634 sprintf (str, "%#08x", insn);
9635 info->callbacks->einfo
9636 /* xgettext:c-format */
9637 (_("%H: got/toc optimization is not supported for"
9638 " %s instruction\n"),
9639 ibfd, sec, rel->r_offset & ~3, str);
9640 continue;
9641 }
9642 }
9643
9644 switch (r_type)
9645 {
9646 /* Note that we don't delete GOT entries for
9647 R_PPC64_GOT16_DS since we'd need a lot more
9648 analysis. For starters, the preliminary layout is
9649 before the GOT, PLT, dynamic sections and stubs are
9650 laid out. Then we'd need to allow for changes in
9651 distance between sections caused by alignment. */
9652 default:
9653 continue;
9654
9655 case R_PPC64_GOT16_HA:
9656 case R_PPC64_GOT16_LO_DS:
9657 case R_PPC64_GOT_PCREL34:
9658 break;
9659 }
9660
9661 r_symndx = ELF64_R_SYM (rel->r_info);
9662 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9663 r_symndx, ibfd))
9664 goto got_error_ret;
9665
9666 if (sym_sec == NULL
9667 || sym_sec->output_section == NULL
9668 || discarded_section (sym_sec))
9669 continue;
9670
9671 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9672 continue;
9673
9674 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9675 || (bfd_link_pic (info)
9676 && sym_sec == bfd_abs_section_ptr))
9677 continue;
9678
9679 if (h != NULL)
9680 val = h->root.u.def.value;
9681 else
9682 val = sym->st_value;
9683 val += rel->r_addend;
9684 val += sym_sec->output_section->vma + sym_sec->output_offset;
9685
9686 /* Fudge factor to allow for the fact that the preliminary layout
9687 isn't exact. Reduce limits by this factor. */
9688 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9689
9690 switch (r_type)
9691 {
9692 default:
9693 continue;
9694
9695 case R_PPC64_GOT16_HA:
9696 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9697 >= LIMIT_ADJUST (0x100000000ULL))
9698 continue;
9699
9700 if (!bfd_get_section_contents (ibfd, sec, buf,
9701 rel->r_offset & ~3, 4))
9702 goto got_error_ret;
9703 insn = bfd_get_32 (ibfd, buf);
9704 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9705 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9706 continue;
9707 break;
9708
9709 case R_PPC64_GOT16_LO_DS:
9710 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9711 >= LIMIT_ADJUST (0x100000000ULL))
9712 continue;
9713 if (!bfd_get_section_contents (ibfd, sec, buf,
9714 rel->r_offset & ~3, 4))
9715 goto got_error_ret;
9716 insn = bfd_get_32 (ibfd, buf);
9717 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9718 continue;
9719 break;
9720
9721 case R_PPC64_GOT_PCREL34:
9722 pc = rel->r_offset;
9723 pc += sec->output_section->vma + sec->output_offset;
9724 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9725 >= LIMIT_ADJUST (1ULL << 34))
9726 continue;
9727 if (!bfd_get_section_contents (ibfd, sec, buf,
9728 rel->r_offset & ~3, 8))
9729 goto got_error_ret;
9730 insn = bfd_get_32 (ibfd, buf);
9731 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9732 continue;
9733 insn = bfd_get_32 (ibfd, buf + 4);
9734 if ((insn & (0x3fu << 26)) != 57u << 26)
9735 continue;
9736 break;
9737 }
9738 #undef LIMIT_ADJUST
9739
9740 if (h != NULL)
9741 ent = h->got.glist;
9742 else
9743 {
9744 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9745 ent = local_got_ents[r_symndx];
9746 }
9747 for (; ent != NULL; ent = ent->next)
9748 if (ent->addend == rel->r_addend
9749 && ent->owner == ibfd
9750 && ent->tls_type == 0)
9751 break;
9752 BFD_ASSERT (ent && ent->got.refcount > 0);
9753 ent->got.refcount -= 1;
9754 }
9755
9756 if (elf_section_data (sec)->relocs != relstart)
9757 free (relstart);
9758 }
9759
9760 if (local_syms != NULL
9761 && symtab_hdr->contents != (unsigned char *) local_syms)
9762 {
9763 if (!info->keep_memory)
9764 free (local_syms);
9765 else
9766 symtab_hdr->contents = (unsigned char *) local_syms;
9767 }
9768 }
9769
9770 return true;
9771 }
9772
9773 /* Return true iff input section I references the TOC using
9774 instructions limited to +/-32k offsets. */
9775
9776 bool
9777 ppc64_elf_has_small_toc_reloc (asection *i)
9778 {
9779 return (is_ppc64_elf (i->owner)
9780 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9781 }
9782
9783 /* Allocate space for one GOT entry. */
9784
9785 static void
9786 allocate_got (struct elf_link_hash_entry *h,
9787 struct bfd_link_info *info,
9788 struct got_entry *gent)
9789 {
9790 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9791 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9792 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9793 ? 16 : 8);
9794 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9795 ? 2 : 1) * sizeof (Elf64_External_Rela);
9796 asection *got = ppc64_elf_tdata (gent->owner)->got;
9797
9798 gent->got.offset = got->size;
9799 got->size += entsize;
9800
9801 if (h->type == STT_GNU_IFUNC)
9802 {
9803 htab->elf.irelplt->size += rentsize;
9804 htab->got_reli_size += rentsize;
9805 }
9806 else if (((bfd_link_pic (info)
9807 && (gent->tls_type == 0
9808 ? !info->enable_dt_relr
9809 : !(bfd_link_executable (info)
9810 && SYMBOL_REFERENCES_LOCAL (info, h)))
9811 && !bfd_is_abs_symbol (&h->root))
9812 || (htab->elf.dynamic_sections_created
9813 && h->dynindx != -1
9814 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9815 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9816 {
9817 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9818 relgot->size += rentsize;
9819 }
9820 }
9821
9822 /* This function merges got entries in the same toc group. */
9823
9824 static void
9825 merge_got_entries (struct got_entry **pent)
9826 {
9827 struct got_entry *ent, *ent2;
9828
9829 for (ent = *pent; ent != NULL; ent = ent->next)
9830 if (!ent->is_indirect)
9831 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9832 if (!ent2->is_indirect
9833 && ent2->addend == ent->addend
9834 && ent2->tls_type == ent->tls_type
9835 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9836 {
9837 ent2->is_indirect = true;
9838 ent2->got.ent = ent;
9839 }
9840 }
9841
9842 /* If H is undefined, make it dynamic if that makes sense. */
9843
9844 static bool
9845 ensure_undef_dynamic (struct bfd_link_info *info,
9846 struct elf_link_hash_entry *h)
9847 {
9848 struct elf_link_hash_table *htab = elf_hash_table (info);
9849
9850 if (htab->dynamic_sections_created
9851 && ((info->dynamic_undefined_weak != 0
9852 && h->root.type == bfd_link_hash_undefweak)
9853 || h->root.type == bfd_link_hash_undefined)
9854 && h->dynindx == -1
9855 && !h->forced_local
9856 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9857 return bfd_elf_link_record_dynamic_symbol (info, h);
9858 return true;
9859 }
9860
9861 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9862 usual htab->elf.splt section for a PLT entry. */
9863
9864 static inline
9865 bool use_local_plt (struct bfd_link_info *info,
9866 struct elf_link_hash_entry *h)
9867 {
9868 return (h == NULL
9869 || h->dynindx == -1
9870 || !elf_hash_table (info)->dynamic_sections_created);
9871 }
9872
9873 /* Allocate space in .plt, .got and associated reloc sections for
9874 dynamic relocs. */
9875
9876 static bool
9877 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9878 {
9879 struct bfd_link_info *info;
9880 struct ppc_link_hash_table *htab;
9881 asection *s;
9882 struct ppc_link_hash_entry *eh;
9883 struct got_entry **pgent, *gent;
9884
9885 if (h->root.type == bfd_link_hash_indirect)
9886 return true;
9887
9888 info = (struct bfd_link_info *) inf;
9889 htab = ppc_hash_table (info);
9890 if (htab == NULL)
9891 return false;
9892
9893 eh = ppc_elf_hash_entry (h);
9894 /* Run through the TLS GD got entries first if we're changing them
9895 to TPREL. */
9896 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9897 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9898 if (gent->got.refcount > 0
9899 && (gent->tls_type & TLS_GD) != 0)
9900 {
9901 /* This was a GD entry that has been converted to TPREL. If
9902 there happens to be a TPREL entry we can use that one. */
9903 struct got_entry *ent;
9904 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9905 if (ent->got.refcount > 0
9906 && (ent->tls_type & TLS_TPREL) != 0
9907 && ent->addend == gent->addend
9908 && ent->owner == gent->owner)
9909 {
9910 gent->got.refcount = 0;
9911 break;
9912 }
9913
9914 /* If not, then we'll be using our own TPREL entry. */
9915 if (gent->got.refcount != 0)
9916 gent->tls_type = TLS_TLS | TLS_TPREL;
9917 }
9918
9919 /* Remove any list entry that won't generate a word in the GOT before
9920 we call merge_got_entries. Otherwise we risk merging to empty
9921 entries. */
9922 pgent = &h->got.glist;
9923 while ((gent = *pgent) != NULL)
9924 if (gent->got.refcount > 0)
9925 {
9926 if ((gent->tls_type & TLS_LD) != 0
9927 && SYMBOL_REFERENCES_LOCAL (info, h))
9928 {
9929 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9930 *pgent = gent->next;
9931 }
9932 else
9933 pgent = &gent->next;
9934 }
9935 else
9936 *pgent = gent->next;
9937
9938 if (!htab->do_multi_toc)
9939 merge_got_entries (&h->got.glist);
9940
9941 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9942 if (!gent->is_indirect)
9943 {
9944 /* Ensure we catch all the cases where this symbol should
9945 be made dynamic. */
9946 if (!ensure_undef_dynamic (info, h))
9947 return false;
9948
9949 if (!is_ppc64_elf (gent->owner))
9950 abort ();
9951
9952 allocate_got (h, info, gent);
9953 }
9954
9955 /* If no dynamic sections we can't have dynamic relocs, except for
9956 IFUNCs which are handled even in static executables. */
9957 if (!htab->elf.dynamic_sections_created
9958 && h->type != STT_GNU_IFUNC)
9959 h->dyn_relocs = NULL;
9960
9961 /* Discard relocs on undefined symbols that must be local. */
9962 else if (h->root.type == bfd_link_hash_undefined
9963 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9964 h->dyn_relocs = NULL;
9965
9966 /* Also discard relocs on undefined weak syms with non-default
9967 visibility, or when dynamic_undefined_weak says so. */
9968 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9969 h->dyn_relocs = NULL;
9970
9971 if (h->dyn_relocs != NULL)
9972 {
9973 struct ppc_dyn_relocs *p, **pp;
9974
9975 /* In the shared -Bsymbolic case, discard space allocated for
9976 dynamic pc-relative relocs against symbols which turn out to
9977 be defined in regular objects. For the normal shared case,
9978 discard space for relocs that have become local due to symbol
9979 visibility changes. */
9980 if (bfd_link_pic (info))
9981 {
9982 /* Relocs that use pc_count are those that appear on a call
9983 insn, or certain REL relocs (see must_be_dyn_reloc) that
9984 can be generated via assembly. We want calls to
9985 protected symbols to resolve directly to the function
9986 rather than going via the plt. If people want function
9987 pointer comparisons to work as expected then they should
9988 avoid writing weird assembly. */
9989 if (SYMBOL_CALLS_LOCAL (info, h))
9990 {
9991 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9992 (p = *pp) != NULL;
9993 )
9994 {
9995 p->count -= p->pc_count;
9996 p->pc_count = 0;
9997 if (p->count == 0)
9998 *pp = p->next;
9999 else
10000 pp = &p->next;
10001 }
10002 }
10003
10004 if (h->dyn_relocs != NULL)
10005 {
10006 /* Ensure we catch all the cases where this symbol
10007 should be made dynamic. */
10008 if (!ensure_undef_dynamic (info, h))
10009 return false;
10010 }
10011 }
10012
10013 /* For a fixed position executable, discard space for
10014 relocs against symbols which are not dynamic. */
10015 else if (h->type != STT_GNU_IFUNC)
10016 {
10017 if ((h->dynamic_adjusted
10018 || (h->ref_regular
10019 && h->root.type == bfd_link_hash_undefweak
10020 && (info->dynamic_undefined_weak > 0
10021 || !_bfd_elf_readonly_dynrelocs (h))))
10022 && !h->def_regular
10023 && !ELF_COMMON_DEF_P (h))
10024 {
10025 /* Ensure we catch all the cases where this symbol
10026 should be made dynamic. */
10027 if (!ensure_undef_dynamic (info, h))
10028 return false;
10029
10030 /* But if that didn't work out, discard dynamic relocs. */
10031 if (h->dynindx == -1)
10032 h->dyn_relocs = NULL;
10033 }
10034 else
10035 h->dyn_relocs = NULL;
10036 }
10037
10038 /* Finally, allocate space. */
10039 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10040 if (!discarded_section (p->sec))
10041 {
10042 unsigned int count;
10043 asection *sreloc = elf_section_data (p->sec)->sreloc;
10044 if (eh->elf.type == STT_GNU_IFUNC)
10045 sreloc = htab->elf.irelplt;
10046 count = p->count;
10047 if (info->enable_dt_relr
10048 && ((!NO_OPD_RELOCS
10049 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10050 || (eh->elf.type != STT_GNU_IFUNC
10051 && SYMBOL_REFERENCES_LOCAL (info, h))))
10052 count -= p->rel_count;
10053 sreloc->size += count * sizeof (Elf64_External_Rela);
10054 }
10055 }
10056
10057 /* We might need a PLT entry when the symbol
10058 a) is dynamic, or
10059 b) is an ifunc, or
10060 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10061 d) has plt16 relocs and we are linking statically. */
10062 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10063 || h->type == STT_GNU_IFUNC
10064 || (h->needs_plt && h->dynamic_adjusted)
10065 || (h->needs_plt
10066 && h->def_regular
10067 && !htab->elf.dynamic_sections_created
10068 && !htab->can_convert_all_inline_plt
10069 && (ppc_elf_hash_entry (h)->tls_mask
10070 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10071 {
10072 struct plt_entry *pent;
10073 bool doneone = false;
10074 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10075 if (pent->plt.refcount > 0)
10076 {
10077 if (!ensure_undef_dynamic (info, h))
10078 return false;
10079
10080 if (use_local_plt (info, h))
10081 {
10082 if (h->type == STT_GNU_IFUNC)
10083 {
10084 s = htab->elf.iplt;
10085 pent->plt.offset = s->size;
10086 s->size += PLT_ENTRY_SIZE (htab);
10087 s = htab->elf.irelplt;
10088 }
10089 else
10090 {
10091 s = htab->pltlocal;
10092 pent->plt.offset = s->size;
10093 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10094 s = NULL;
10095 if (bfd_link_pic (info)
10096 && !(info->enable_dt_relr && !htab->opd_abi))
10097 s = htab->relpltlocal;
10098 }
10099 }
10100 else
10101 {
10102 /* If this is the first .plt entry, make room for the special
10103 first entry. */
10104 s = htab->elf.splt;
10105 if (s->size == 0)
10106 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10107
10108 pent->plt.offset = s->size;
10109
10110 /* Make room for this entry. */
10111 s->size += PLT_ENTRY_SIZE (htab);
10112
10113 /* Make room for the .glink code. */
10114 s = htab->glink;
10115 if (s->size == 0)
10116 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10117 if (htab->opd_abi)
10118 {
10119 /* We need bigger stubs past index 32767. */
10120 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10121 s->size += 4;
10122 s->size += 2*4;
10123 }
10124 else
10125 s->size += 4;
10126
10127 /* We also need to make an entry in the .rela.plt section. */
10128 s = htab->elf.srelplt;
10129 }
10130 if (s != NULL)
10131 s->size += sizeof (Elf64_External_Rela);
10132 doneone = true;
10133 }
10134 else
10135 pent->plt.offset = (bfd_vma) -1;
10136 if (!doneone)
10137 {
10138 h->plt.plist = NULL;
10139 h->needs_plt = 0;
10140 }
10141 }
10142 else
10143 {
10144 h->plt.plist = NULL;
10145 h->needs_plt = 0;
10146 }
10147
10148 return true;
10149 }
10150
10151 #define PPC_LO(v) ((v) & 0xffff)
10152 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10153 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10154 #define D34(v) \
10155 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10156 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10157
10158 /* Called via elf_link_hash_traverse from ppc64_elf_late_size_sections
10159 to set up space for global entry stubs. These are put in glink,
10160 after the branch table. */
10161
10162 static bool
10163 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10164 {
10165 struct bfd_link_info *info;
10166 struct ppc_link_hash_table *htab;
10167 struct plt_entry *pent;
10168 asection *s, *plt;
10169
10170 if (h->root.type == bfd_link_hash_indirect)
10171 return true;
10172
10173 if (!h->pointer_equality_needed)
10174 return true;
10175
10176 if (h->def_regular)
10177 return true;
10178
10179 info = inf;
10180 htab = ppc_hash_table (info);
10181 if (htab == NULL)
10182 return false;
10183
10184 s = htab->global_entry;
10185 plt = htab->elf.splt;
10186 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10187 if (pent->plt.offset != (bfd_vma) -1
10188 && pent->addend == 0)
10189 {
10190 /* For ELFv2, if this symbol is not defined in a regular file
10191 and we are not generating a shared library or pie, then we
10192 need to define the symbol in the executable on a call stub.
10193 This is to avoid text relocations. */
10194 bfd_vma off, stub_align, stub_off, stub_size;
10195 unsigned int align_power;
10196
10197 stub_size = 16;
10198 stub_off = s->size;
10199 if (htab->params->plt_stub_align >= 0)
10200 align_power = htab->params->plt_stub_align;
10201 else
10202 align_power = -htab->params->plt_stub_align;
10203 /* Setting section alignment is delayed until we know it is
10204 non-empty. Otherwise the .text output section will be
10205 aligned at least to plt_stub_align even when no global
10206 entry stubs are needed. */
10207 if (s->alignment_power < align_power)
10208 s->alignment_power = align_power;
10209 stub_align = (bfd_vma) 1 << align_power;
10210 if (htab->params->plt_stub_align >= 0
10211 || ((((stub_off + stub_size - 1) & -stub_align)
10212 - (stub_off & -stub_align))
10213 > ((stub_size - 1) & -stub_align)))
10214 stub_off = (stub_off + stub_align - 1) & -stub_align;
10215 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10216 off -= stub_off + s->output_offset + s->output_section->vma;
10217 /* Note that for --plt-stub-align negative we have a possible
10218 dependency between stub offset and size. Break that
10219 dependency by assuming the max stub size when calculating
10220 the stub offset. */
10221 if (PPC_HA (off) == 0)
10222 stub_size -= 4;
10223 h->root.type = bfd_link_hash_defined;
10224 h->root.u.def.section = s;
10225 h->root.u.def.value = stub_off;
10226 s->size = stub_off + stub_size;
10227 break;
10228 }
10229 return true;
10230 }
10231
10232 /* Set the sizes of the dynamic sections. */
10233
10234 static bool
10235 ppc64_elf_late_size_sections (bfd *output_bfd,
10236 struct bfd_link_info *info)
10237 {
10238 struct ppc_link_hash_table *htab;
10239 bfd *dynobj;
10240 asection *s;
10241 bool relocs;
10242 bfd *ibfd;
10243 struct got_entry *first_tlsld;
10244
10245 htab = ppc_hash_table (info);
10246 if (htab == NULL)
10247 return false;
10248
10249 dynobj = htab->elf.dynobj;
10250 if (dynobj == NULL)
10251 return true;
10252
10253 if (htab->elf.dynamic_sections_created)
10254 {
10255 /* Set the contents of the .interp section to the interpreter. */
10256 if (bfd_link_executable (info) && !info->nointerp)
10257 {
10258 s = bfd_get_linker_section (dynobj, ".interp");
10259 if (s == NULL)
10260 abort ();
10261 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10262 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10263 }
10264 }
10265
10266 /* Set up .got offsets for local syms, and space for local dynamic
10267 relocs. */
10268 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10269 {
10270 struct got_entry **lgot_ents;
10271 struct got_entry **end_lgot_ents;
10272 struct plt_entry **local_plt;
10273 struct plt_entry **end_local_plt;
10274 unsigned char *lgot_masks;
10275 bfd_size_type locsymcount;
10276 Elf_Internal_Shdr *symtab_hdr;
10277 Elf_Internal_Sym *local_syms;
10278 Elf_Internal_Sym *isym;
10279
10280 if (!is_ppc64_elf (ibfd))
10281 continue;
10282
10283 for (s = ibfd->sections; s != NULL; s = s->next)
10284 {
10285 struct ppc_local_dyn_relocs *p;
10286
10287 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10288 {
10289 if (discarded_section (p->sec))
10290 {
10291 /* Input section has been discarded, either because
10292 it is a copy of a linkonce section or due to
10293 linker script /DISCARD/, so we'll be discarding
10294 the relocs too. */
10295 }
10296 else if (p->count != 0)
10297 {
10298 unsigned int count;
10299 asection *srel;
10300
10301 count = p->count;
10302 if (info->enable_dt_relr
10303 && ((!NO_OPD_RELOCS
10304 && (ppc64_elf_section_data (p->sec)->sec_type
10305 == sec_opd))
10306 || !p->ifunc))
10307 count -= p->rel_count;
10308 srel = elf_section_data (p->sec)->sreloc;
10309 if (p->ifunc)
10310 srel = htab->elf.irelplt;
10311 srel->size += count * sizeof (Elf64_External_Rela);
10312 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10313 info->flags |= DF_TEXTREL;
10314 }
10315 }
10316 }
10317
10318 lgot_ents = elf_local_got_ents (ibfd);
10319 if (!lgot_ents)
10320 continue;
10321
10322 symtab_hdr = &elf_symtab_hdr (ibfd);
10323 locsymcount = symtab_hdr->sh_info;
10324 end_lgot_ents = lgot_ents + locsymcount;
10325 local_plt = (struct plt_entry **) end_lgot_ents;
10326 end_local_plt = local_plt + locsymcount;
10327 lgot_masks = (unsigned char *) end_local_plt;
10328 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10329 if (local_syms == NULL && locsymcount != 0)
10330 {
10331 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10332 0, NULL, NULL, NULL);
10333 if (local_syms == NULL)
10334 return false;
10335 }
10336 s = ppc64_elf_tdata (ibfd)->got;
10337 for (isym = local_syms;
10338 lgot_ents < end_lgot_ents;
10339 ++lgot_ents, ++lgot_masks, isym++)
10340 {
10341 struct got_entry **pent, *ent;
10342
10343 pent = lgot_ents;
10344 while ((ent = *pent) != NULL)
10345 if (ent->got.refcount > 0)
10346 {
10347 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10348 {
10349 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10350 *pent = ent->next;
10351 }
10352 else
10353 {
10354 unsigned int ent_size = 8;
10355 unsigned int rel_size = sizeof (Elf64_External_Rela);
10356
10357 ent->got.offset = s->size;
10358 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10359 {
10360 ent_size *= 2;
10361 rel_size *= 2;
10362 }
10363 s->size += ent_size;
10364 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10365 {
10366 htab->elf.irelplt->size += rel_size;
10367 htab->got_reli_size += rel_size;
10368 }
10369 else if (bfd_link_pic (info)
10370 && (ent->tls_type == 0
10371 ? !info->enable_dt_relr
10372 : !bfd_link_executable (info))
10373 && isym->st_shndx != SHN_ABS)
10374 {
10375 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10376 srel->size += rel_size;
10377 }
10378 pent = &ent->next;
10379 }
10380 }
10381 else
10382 *pent = ent->next;
10383 }
10384 if (local_syms != NULL
10385 && symtab_hdr->contents != (unsigned char *) local_syms)
10386 {
10387 if (!info->keep_memory)
10388 free (local_syms);
10389 else
10390 symtab_hdr->contents = (unsigned char *) local_syms;
10391 }
10392
10393 /* Allocate space for plt calls to local syms. */
10394 lgot_masks = (unsigned char *) end_local_plt;
10395 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10396 {
10397 struct plt_entry *ent;
10398
10399 for (ent = *local_plt; ent != NULL; ent = ent->next)
10400 if (ent->plt.refcount > 0)
10401 {
10402 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10403 {
10404 s = htab->elf.iplt;
10405 ent->plt.offset = s->size;
10406 s->size += PLT_ENTRY_SIZE (htab);
10407 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10408 }
10409 else if (htab->can_convert_all_inline_plt
10410 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10411 ent->plt.offset = (bfd_vma) -1;
10412 else
10413 {
10414 s = htab->pltlocal;
10415 ent->plt.offset = s->size;
10416 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10417 if (bfd_link_pic (info)
10418 && !(info->enable_dt_relr && !htab->opd_abi))
10419 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10420 }
10421 }
10422 else
10423 ent->plt.offset = (bfd_vma) -1;
10424 }
10425 }
10426
10427 /* Allocate global sym .plt and .got entries, and space for global
10428 sym dynamic relocs. */
10429 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10430
10431 if (!htab->opd_abi && !bfd_link_pic (info))
10432 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10433
10434 first_tlsld = NULL;
10435 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10436 {
10437 struct got_entry *ent;
10438
10439 if (!is_ppc64_elf (ibfd))
10440 continue;
10441
10442 ent = ppc64_tlsld_got (ibfd);
10443 if (ent->got.refcount > 0)
10444 {
10445 if (!htab->do_multi_toc && first_tlsld != NULL)
10446 {
10447 ent->is_indirect = true;
10448 ent->got.ent = first_tlsld;
10449 }
10450 else
10451 {
10452 if (first_tlsld == NULL)
10453 first_tlsld = ent;
10454 s = ppc64_elf_tdata (ibfd)->got;
10455 ent->got.offset = s->size;
10456 ent->owner = ibfd;
10457 s->size += 16;
10458 if (bfd_link_dll (info))
10459 {
10460 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10461 srel->size += sizeof (Elf64_External_Rela);
10462 }
10463 }
10464 }
10465 else
10466 ent->got.offset = (bfd_vma) -1;
10467 }
10468
10469 /* We now have determined the sizes of the various dynamic sections.
10470 Allocate memory for them. */
10471 relocs = false;
10472 for (s = dynobj->sections; s != NULL; s = s->next)
10473 {
10474 if ((s->flags & SEC_LINKER_CREATED) == 0)
10475 continue;
10476
10477 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10478 /* These haven't been allocated yet; don't strip. */
10479 continue;
10480 else if (s == htab->elf.sgot
10481 || s == htab->elf.splt
10482 || s == htab->elf.iplt
10483 || s == htab->pltlocal
10484 || s == htab->glink
10485 || s == htab->global_entry
10486 || s == htab->elf.sdynbss
10487 || s == htab->elf.sdynrelro)
10488 {
10489 /* Strip this section if we don't need it; see the
10490 comment below. */
10491 }
10492 else if (s == htab->glink_eh_frame)
10493 {
10494 if (!bfd_is_abs_section (s->output_section))
10495 /* Not sized yet. */
10496 continue;
10497 }
10498 else if (startswith (s->name, ".rela"))
10499 {
10500 if (s->size != 0)
10501 {
10502 if (s != htab->elf.srelplt)
10503 relocs = true;
10504
10505 /* We use the reloc_count field as a counter if we need
10506 to copy relocs into the output file. */
10507 s->reloc_count = 0;
10508 }
10509 }
10510 else
10511 {
10512 /* It's not one of our sections, so don't allocate space. */
10513 continue;
10514 }
10515
10516 if (s->size == 0)
10517 {
10518 /* If we don't need this section, strip it from the
10519 output file. This is mostly to handle .rela.bss and
10520 .rela.plt. We must create both sections in
10521 create_dynamic_sections, because they must be created
10522 before the linker maps input sections to output
10523 sections. The linker does that before
10524 adjust_dynamic_symbol is called, and it is that
10525 function which decides whether anything needs to go
10526 into these sections. */
10527 s->flags |= SEC_EXCLUDE;
10528 continue;
10529 }
10530
10531 if (bfd_is_abs_section (s->output_section))
10532 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10533 s->name);
10534
10535 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10536 continue;
10537
10538 /* Allocate memory for the section contents. We use bfd_zalloc
10539 here in case unused entries are not reclaimed before the
10540 section's contents are written out. This should not happen,
10541 but this way if it does we get a R_PPC64_NONE reloc in .rela
10542 sections instead of garbage.
10543 We also rely on the section contents being zero when writing
10544 the GOT and .dynrelro. */
10545 s->contents = bfd_zalloc (dynobj, s->size);
10546 if (s->contents == NULL)
10547 return false;
10548 }
10549
10550 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10551 {
10552 if (!is_ppc64_elf (ibfd))
10553 continue;
10554
10555 s = ppc64_elf_tdata (ibfd)->got;
10556 if (s != NULL && s != htab->elf.sgot)
10557 {
10558 if (s->size == 0)
10559 s->flags |= SEC_EXCLUDE;
10560 else
10561 {
10562 s->contents = bfd_zalloc (ibfd, s->size);
10563 if (s->contents == NULL)
10564 return false;
10565 }
10566 }
10567 s = ppc64_elf_tdata (ibfd)->relgot;
10568 if (s != NULL)
10569 {
10570 if (s->size == 0)
10571 s->flags |= SEC_EXCLUDE;
10572 else
10573 {
10574 s->contents = bfd_zalloc (ibfd, s->size);
10575 if (s->contents == NULL)
10576 return false;
10577 relocs = true;
10578 s->reloc_count = 0;
10579 }
10580 }
10581 }
10582
10583 if (htab->elf.dynamic_sections_created)
10584 {
10585 bool tls_opt;
10586
10587 /* Add some entries to the .dynamic section. We fill in the
10588 values later, in ppc64_elf_finish_dynamic_sections, but we
10589 must add the entries now so that we get the correct size for
10590 the .dynamic section. The DT_DEBUG entry is filled in by the
10591 dynamic linker and used by the debugger. */
10592 #define add_dynamic_entry(TAG, VAL) \
10593 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10594
10595 if (bfd_link_executable (info))
10596 {
10597 if (!add_dynamic_entry (DT_DEBUG, 0))
10598 return false;
10599 }
10600
10601 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10602 {
10603 if (!add_dynamic_entry (DT_PLTGOT, 0)
10604 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10605 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10606 || !add_dynamic_entry (DT_JMPREL, 0)
10607 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10608 return false;
10609 }
10610
10611 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10612 {
10613 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10614 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10615 return false;
10616 }
10617
10618 tls_opt = (htab->params->tls_get_addr_opt
10619 && ((htab->tls_get_addr_fd != NULL
10620 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10621 || (htab->tga_desc_fd != NULL
10622 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10623 if (tls_opt || !htab->opd_abi)
10624 {
10625 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10626 return false;
10627 }
10628
10629 if (relocs)
10630 {
10631 if (!add_dynamic_entry (DT_RELA, 0)
10632 || !add_dynamic_entry (DT_RELASZ, 0)
10633 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10634 return false;
10635
10636 /* If any dynamic relocs apply to a read-only section,
10637 then we need a DT_TEXTREL entry. */
10638 if ((info->flags & DF_TEXTREL) == 0)
10639 elf_link_hash_traverse (&htab->elf,
10640 _bfd_elf_maybe_set_textrel, info);
10641
10642 if ((info->flags & DF_TEXTREL) != 0)
10643 {
10644 if (!add_dynamic_entry (DT_TEXTREL, 0))
10645 return false;
10646 }
10647 }
10648 }
10649 #undef add_dynamic_entry
10650
10651 return true;
10652 }
10653
10654 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10655
10656 static bool
10657 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10658 {
10659 if (h->plt.plist != NULL
10660 && !h->def_regular
10661 && !h->pointer_equality_needed)
10662 return false;
10663
10664 return _bfd_elf_hash_symbol (h);
10665 }
10666
10667 /* Determine the type of stub needed, if any, for a call. */
10668
10669 static inline enum ppc_stub_main_type
10670 ppc_type_of_stub (asection *input_sec,
10671 const Elf_Internal_Rela *rel,
10672 struct ppc_link_hash_entry **hash,
10673 struct plt_entry **plt_ent,
10674 bfd_vma destination,
10675 unsigned long local_off)
10676 {
10677 struct ppc_link_hash_entry *h = *hash;
10678 bfd_vma location;
10679 bfd_vma branch_offset;
10680 bfd_vma max_branch_offset;
10681 enum elf_ppc64_reloc_type r_type;
10682
10683 if (h != NULL)
10684 {
10685 struct plt_entry *ent;
10686 struct ppc_link_hash_entry *fdh = h;
10687 if (h->oh != NULL
10688 && h->oh->is_func_descriptor)
10689 {
10690 fdh = ppc_follow_link (h->oh);
10691 *hash = fdh;
10692 }
10693
10694 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10695 if (ent->addend == rel->r_addend
10696 && ent->plt.offset != (bfd_vma) -1)
10697 {
10698 *plt_ent = ent;
10699 return ppc_stub_plt_call;
10700 }
10701
10702 /* Here, we know we don't have a plt entry. If we don't have a
10703 either a defined function descriptor or a defined entry symbol
10704 in a regular object file, then it is pointless trying to make
10705 any other type of stub. */
10706 if (!is_static_defined (&fdh->elf)
10707 && !is_static_defined (&h->elf))
10708 return ppc_stub_none;
10709 }
10710 else if (elf_local_got_ents (input_sec->owner) != NULL)
10711 {
10712 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10713 struct plt_entry **local_plt = (struct plt_entry **)
10714 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10715 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10716
10717 if (local_plt[r_symndx] != NULL)
10718 {
10719 struct plt_entry *ent;
10720
10721 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10722 if (ent->addend == rel->r_addend
10723 && ent->plt.offset != (bfd_vma) -1)
10724 {
10725 *plt_ent = ent;
10726 return ppc_stub_plt_call;
10727 }
10728 }
10729 }
10730
10731 /* Determine where the call point is. */
10732 location = (input_sec->output_offset
10733 + input_sec->output_section->vma
10734 + rel->r_offset);
10735
10736 branch_offset = destination - location;
10737 r_type = ELF64_R_TYPE (rel->r_info);
10738
10739 /* Determine if a long branch stub is needed. */
10740 max_branch_offset = 1 << 25;
10741 if (r_type == R_PPC64_REL14
10742 || r_type == R_PPC64_REL14_BRTAKEN
10743 || r_type == R_PPC64_REL14_BRNTAKEN)
10744 max_branch_offset = 1 << 15;
10745
10746 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10747 /* We need a stub. Figure out whether a long_branch or plt_branch
10748 is needed later. */
10749 return ppc_stub_long_branch;
10750
10751 return ppc_stub_none;
10752 }
10753
10754 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10755 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10756 . mflr %r12
10757 . bcl 20,31,1f
10758 .1: mflr %r11
10759 . mtlr %r12
10760 . lis %r12,xxx-1b@highest
10761 . ori %r12,%r12,xxx-1b@higher
10762 . sldi %r12,%r12,32
10763 . oris %r12,%r12,xxx-1b@high
10764 . ori %r12,%r12,xxx-1b@l
10765 . add/ldx %r12,%r11,%r12 */
10766
10767 static bfd_byte *
10768 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10769 {
10770 bfd_put_32 (abfd, MFLR_R12, p);
10771 p += 4;
10772 bfd_put_32 (abfd, BCL_20_31, p);
10773 p += 4;
10774 bfd_put_32 (abfd, MFLR_R11, p);
10775 p += 4;
10776 bfd_put_32 (abfd, MTLR_R12, p);
10777 p += 4;
10778 if (off + 0x8000 < 0x10000)
10779 {
10780 if (load)
10781 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10782 else
10783 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10784 p += 4;
10785 }
10786 else if (off + 0x80008000ULL < 0x100000000ULL)
10787 {
10788 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10789 p += 4;
10790 if (load)
10791 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10792 else
10793 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10794 p += 4;
10795 }
10796 else
10797 {
10798 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10799 {
10800 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10801 p += 4;
10802 }
10803 else
10804 {
10805 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10806 p += 4;
10807 if (((off >> 32) & 0xffff) != 0)
10808 {
10809 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10810 p += 4;
10811 }
10812 }
10813 if (((off >> 32) & 0xffffffffULL) != 0)
10814 {
10815 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10816 p += 4;
10817 }
10818 if (PPC_HI (off) != 0)
10819 {
10820 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10821 p += 4;
10822 }
10823 if (PPC_LO (off) != 0)
10824 {
10825 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10826 p += 4;
10827 }
10828 if (load)
10829 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10830 else
10831 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10832 p += 4;
10833 }
10834 return p;
10835 }
10836
10837 static unsigned int
10838 size_offset (bfd_vma off)
10839 {
10840 unsigned int size;
10841 if (off + 0x8000 < 0x10000)
10842 size = 4;
10843 else if (off + 0x80008000ULL < 0x100000000ULL)
10844 size = 8;
10845 else
10846 {
10847 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10848 size = 4;
10849 else
10850 {
10851 size = 4;
10852 if (((off >> 32) & 0xffff) != 0)
10853 size += 4;
10854 }
10855 if (((off >> 32) & 0xffffffffULL) != 0)
10856 size += 4;
10857 if (PPC_HI (off) != 0)
10858 size += 4;
10859 if (PPC_LO (off) != 0)
10860 size += 4;
10861 size += 4;
10862 }
10863 return size + 16;
10864 }
10865
10866 static unsigned int
10867 num_relocs_for_offset (bfd_vma off)
10868 {
10869 unsigned int num_rel;
10870 if (off + 0x8000 < 0x10000)
10871 num_rel = 1;
10872 else if (off + 0x80008000ULL < 0x100000000ULL)
10873 num_rel = 2;
10874 else
10875 {
10876 num_rel = 1;
10877 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10878 && ((off >> 32) & 0xffff) != 0)
10879 num_rel += 1;
10880 if (PPC_HI (off) != 0)
10881 num_rel += 1;
10882 if (PPC_LO (off) != 0)
10883 num_rel += 1;
10884 }
10885 return num_rel;
10886 }
10887
10888 static Elf_Internal_Rela *
10889 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10890 bfd_vma roff, bfd_vma targ, bfd_vma off)
10891 {
10892 bfd_vma relative_targ = targ - (roff - 8);
10893 if (bfd_big_endian (info->output_bfd))
10894 roff += 2;
10895 r->r_offset = roff;
10896 r->r_addend = relative_targ + roff;
10897 if (off + 0x8000 < 0x10000)
10898 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10899 else if (off + 0x80008000ULL < 0x100000000ULL)
10900 {
10901 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10902 ++r;
10903 roff += 4;
10904 r->r_offset = roff;
10905 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10906 r->r_addend = relative_targ + roff;
10907 }
10908 else
10909 {
10910 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10911 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10912 else
10913 {
10914 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10915 if (((off >> 32) & 0xffff) != 0)
10916 {
10917 ++r;
10918 roff += 4;
10919 r->r_offset = roff;
10920 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10921 r->r_addend = relative_targ + roff;
10922 }
10923 }
10924 if (((off >> 32) & 0xffffffffULL) != 0)
10925 roff += 4;
10926 if (PPC_HI (off) != 0)
10927 {
10928 ++r;
10929 roff += 4;
10930 r->r_offset = roff;
10931 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10932 r->r_addend = relative_targ + roff;
10933 }
10934 if (PPC_LO (off) != 0)
10935 {
10936 ++r;
10937 roff += 4;
10938 r->r_offset = roff;
10939 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10940 r->r_addend = relative_targ + roff;
10941 }
10942 }
10943 return r;
10944 }
10945
10946 static bfd_byte *
10947 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10948 bool load)
10949 {
10950 uint64_t insn;
10951 if (off - odd + (1ULL << 33) < 1ULL << 34)
10952 {
10953 off -= odd;
10954 if (odd)
10955 {
10956 bfd_put_32 (abfd, NOP, p);
10957 p += 4;
10958 }
10959 if (load)
10960 insn = PLD_R12_PC;
10961 else
10962 insn = PADDI_R12_PC;
10963 insn |= D34 (off);
10964 bfd_put_32 (abfd, insn >> 32, p);
10965 p += 4;
10966 bfd_put_32 (abfd, insn, p);
10967 }
10968 /* The minimum value for paddi is -0x200000000. The minimum value
10969 for li is -0x8000, which when shifted by 34 and added gives a
10970 minimum value of -0x2000200000000. The maximum value is
10971 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10972 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10973 {
10974 off -= 8 - odd;
10975 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10976 p += 4;
10977 if (!odd)
10978 {
10979 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10980 p += 4;
10981 }
10982 insn = PADDI_R12_PC | D34 (off);
10983 bfd_put_32 (abfd, insn >> 32, p);
10984 p += 4;
10985 bfd_put_32 (abfd, insn, p);
10986 p += 4;
10987 if (odd)
10988 {
10989 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10990 p += 4;
10991 }
10992 if (load)
10993 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10994 else
10995 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10996 }
10997 else
10998 {
10999 off -= odd + 8;
11000 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
11001 p += 4;
11002 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
11003 p += 4;
11004 if (odd)
11005 {
11006 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11007 p += 4;
11008 }
11009 insn = PADDI_R12_PC | D34 (off);
11010 bfd_put_32 (abfd, insn >> 32, p);
11011 p += 4;
11012 bfd_put_32 (abfd, insn, p);
11013 p += 4;
11014 if (!odd)
11015 {
11016 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11017 p += 4;
11018 }
11019 if (load)
11020 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11021 else
11022 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11023 }
11024 p += 4;
11025 return p;
11026 }
11027
11028 static unsigned int
11029 size_power10_offset (bfd_vma off, int odd)
11030 {
11031 if (off - odd + (1ULL << 33) < 1ULL << 34)
11032 return odd + 8;
11033 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11034 return 20;
11035 else
11036 return 24;
11037 }
11038
11039 static unsigned int
11040 num_relocs_for_power10_offset (bfd_vma off, int odd)
11041 {
11042 if (off - odd + (1ULL << 33) < 1ULL << 34)
11043 return 1;
11044 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11045 return 2;
11046 else
11047 return 3;
11048 }
11049
11050 static Elf_Internal_Rela *
11051 emit_relocs_for_power10_offset (struct bfd_link_info *info,
11052 Elf_Internal_Rela *r, bfd_vma roff,
11053 bfd_vma targ, bfd_vma off, int odd)
11054 {
11055 if (off - odd + (1ULL << 33) < 1ULL << 34)
11056 roff += odd;
11057 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11058 {
11059 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11060 r->r_offset = roff + d_offset;
11061 r->r_addend = targ + 8 - odd - d_offset;
11062 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11063 ++r;
11064 roff += 8 - odd;
11065 }
11066 else
11067 {
11068 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11069 r->r_offset = roff + d_offset;
11070 r->r_addend = targ + 8 + odd - d_offset;
11071 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11072 ++r;
11073 roff += 4;
11074 r->r_offset = roff + d_offset;
11075 r->r_addend = targ + 4 + odd - d_offset;
11076 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11077 ++r;
11078 roff += 4 + odd;
11079 }
11080 r->r_offset = roff;
11081 r->r_addend = targ;
11082 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11083 return r;
11084 }
11085
11086 /* Emit .eh_frame opcode to advance pc by DELTA. */
11087
11088 static bfd_byte *
11089 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11090 {
11091 delta /= 4;
11092 if (delta < 64)
11093 *eh++ = DW_CFA_advance_loc + delta;
11094 else if (delta < 256)
11095 {
11096 *eh++ = DW_CFA_advance_loc1;
11097 *eh++ = delta;
11098 }
11099 else if (delta < 65536)
11100 {
11101 *eh++ = DW_CFA_advance_loc2;
11102 bfd_put_16 (abfd, delta, eh);
11103 eh += 2;
11104 }
11105 else
11106 {
11107 *eh++ = DW_CFA_advance_loc4;
11108 bfd_put_32 (abfd, delta, eh);
11109 eh += 4;
11110 }
11111 return eh;
11112 }
11113
11114 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11115
11116 static unsigned int
11117 eh_advance_size (unsigned int delta)
11118 {
11119 if (delta < 64 * 4)
11120 /* DW_CFA_advance_loc+[1..63]. */
11121 return 1;
11122 if (delta < 256 * 4)
11123 /* DW_CFA_advance_loc1, byte. */
11124 return 2;
11125 if (delta < 65536 * 4)
11126 /* DW_CFA_advance_loc2, 2 bytes. */
11127 return 3;
11128 /* DW_CFA_advance_loc4, 4 bytes. */
11129 return 5;
11130 }
11131
11132 /* With power7 weakly ordered memory model, it is possible for ld.so
11133 to update a plt entry in one thread and have another thread see a
11134 stale zero toc entry. To avoid this we need some sort of acquire
11135 barrier in the call stub. One solution is to make the load of the
11136 toc word seem to appear to depend on the load of the function entry
11137 word. Another solution is to test for r2 being zero, and branch to
11138 the appropriate glink entry if so.
11139
11140 . fake dep barrier compare
11141 . ld 12,xxx(2) ld 12,xxx(2)
11142 . mtctr 12 mtctr 12
11143 . xor 11,12,12 ld 2,xxx+8(2)
11144 . add 2,2,11 cmpldi 2,0
11145 . ld 2,xxx+8(2) bnectr+
11146 . bctr b <glink_entry>
11147
11148 The solution involving the compare turns out to be faster, so
11149 that's what we use unless the branch won't reach. */
11150
11151 #define ALWAYS_USE_FAKE_DEP 0
11152 #define ALWAYS_EMIT_R2SAVE 0
11153
11154 static inline unsigned int
11155 plt_stub_size (struct ppc_link_hash_table *htab,
11156 struct ppc_stub_hash_entry *stub_entry,
11157 bfd_vma off,
11158 unsigned int odd)
11159 {
11160 unsigned size;
11161
11162 if (stub_entry->type.sub == ppc_stub_notoc)
11163 {
11164 size = 8 + size_power10_offset (off, odd);
11165 if (stub_entry->type.r2save)
11166 size += 4;
11167 }
11168 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11169 {
11170 size = 8 + size_offset (off - 8);
11171 if (stub_entry->type.r2save)
11172 size += 4;
11173 }
11174 else
11175 {
11176 size = 12;
11177 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11178 size += 4;
11179 if (PPC_HA (off) != 0)
11180 size += 4;
11181 if (htab->opd_abi)
11182 {
11183 size += 4;
11184 if (htab->params->plt_static_chain)
11185 size += 4;
11186 if (htab->params->plt_thread_safe
11187 && htab->elf.dynamic_sections_created
11188 && stub_entry->h != NULL
11189 && stub_entry->h->elf.dynindx != -1)
11190 size += 8;
11191 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11192 != PPC_HA (off))
11193 size += 4;
11194 }
11195 }
11196 if (stub_entry->h != NULL
11197 && is_tls_get_addr (&stub_entry->h->elf, htab)
11198 && htab->params->tls_get_addr_opt)
11199 {
11200 if (!htab->params->no_tls_get_addr_regsave)
11201 {
11202 size += 30 * 4;
11203 if (stub_entry->type.r2save)
11204 size += 4;
11205 }
11206 else
11207 {
11208 size += 7 * 4;
11209 if (stub_entry->type.r2save)
11210 size += 6 * 4;
11211 }
11212 }
11213 return size;
11214 }
11215
11216 /* Depending on the sign of plt_stub_align:
11217 If positive, return the padding to align to a 2**plt_stub_align
11218 boundary.
11219 If negative, if this stub would cross fewer 2**plt_stub_align
11220 boundaries if we align, then return the padding needed to do so. */
11221
11222 static inline unsigned int
11223 plt_stub_pad (int plt_stub_align,
11224 bfd_vma stub_off,
11225 unsigned int stub_size)
11226 {
11227 unsigned int stub_align;
11228
11229 if (plt_stub_align >= 0)
11230 stub_align = 1u << plt_stub_align;
11231 else
11232 {
11233 stub_align = 1u << -plt_stub_align;
11234 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11235 <= ((stub_size - 1) & -stub_align))
11236 return 0;
11237 }
11238 return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
11239 }
11240
11241 /* Build a toc using .plt call stub. */
11242
11243 static inline bfd_byte *
11244 build_plt_stub (struct ppc_link_hash_table *htab,
11245 struct ppc_stub_hash_entry *stub_entry,
11246 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11247 {
11248 bfd *obfd = htab->params->stub_bfd;
11249 bool plt_load_toc = htab->opd_abi;
11250 bool plt_static_chain = htab->params->plt_static_chain;
11251 bool plt_thread_safe = (htab->params->plt_thread_safe
11252 && htab->elf.dynamic_sections_created
11253 && stub_entry->h != NULL
11254 && stub_entry->h->elf.dynindx != -1);
11255 bool use_fake_dep = plt_thread_safe;
11256 bfd_vma cmp_branch_off = 0;
11257
11258 if (!ALWAYS_USE_FAKE_DEP
11259 && plt_load_toc
11260 && plt_thread_safe
11261 && !(stub_entry->h != NULL
11262 && is_tls_get_addr (&stub_entry->h->elf, htab)
11263 && htab->params->tls_get_addr_opt))
11264 {
11265 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11266 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11267 / PLT_ENTRY_SIZE (htab));
11268 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11269 bfd_vma to, from;
11270
11271 if (pltindex > 32768)
11272 glinkoff += (pltindex - 32768) * 4;
11273 to = (glinkoff
11274 + htab->glink->output_offset
11275 + htab->glink->output_section->vma);
11276 from = (p - stub_entry->group->stub_sec->contents
11277 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11278 + 4 * (PPC_HA (offset) != 0)
11279 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11280 != PPC_HA (offset))
11281 + 4 * (plt_static_chain != 0)
11282 + 20
11283 + stub_entry->group->stub_sec->output_offset
11284 + stub_entry->group->stub_sec->output_section->vma);
11285 cmp_branch_off = to - from;
11286 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11287 }
11288
11289 if (PPC_HA (offset) != 0)
11290 {
11291 if (r != NULL)
11292 {
11293 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11294 r[0].r_offset += 4;
11295 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11296 r[1].r_offset = r[0].r_offset + 4;
11297 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11298 r[1].r_addend = r[0].r_addend;
11299 if (plt_load_toc)
11300 {
11301 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11302 {
11303 r[2].r_offset = r[1].r_offset + 4;
11304 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11305 r[2].r_addend = r[0].r_addend;
11306 }
11307 else
11308 {
11309 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11310 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11311 r[2].r_addend = r[0].r_addend + 8;
11312 if (plt_static_chain)
11313 {
11314 r[3].r_offset = r[2].r_offset + 4;
11315 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11316 r[3].r_addend = r[0].r_addend + 16;
11317 }
11318 }
11319 }
11320 }
11321 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11322 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11323 if (plt_load_toc)
11324 {
11325 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11326 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11327 }
11328 else
11329 {
11330 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11331 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11332 }
11333 if (plt_load_toc
11334 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11335 {
11336 bfd_put_32 (obfd, ADDI_R11_R11 | 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_R2_R12_R12, p), p += 4;
11345 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11346 }
11347 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11348 if (plt_static_chain)
11349 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11350 }
11351 }
11352 else
11353 {
11354 if (r != NULL)
11355 {
11356 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11357 r[0].r_offset += 4;
11358 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11359 if (plt_load_toc)
11360 {
11361 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11362 {
11363 r[1].r_offset = r[0].r_offset + 4;
11364 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11365 r[1].r_addend = r[0].r_addend;
11366 }
11367 else
11368 {
11369 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11370 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11371 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11372 if (plt_static_chain)
11373 {
11374 r[2].r_offset = r[1].r_offset + 4;
11375 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11376 r[2].r_addend = r[0].r_addend + 8;
11377 }
11378 }
11379 }
11380 }
11381 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11382 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11383 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11384 if (plt_load_toc
11385 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11386 {
11387 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11388 offset = 0;
11389 }
11390 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11391 if (plt_load_toc)
11392 {
11393 if (use_fake_dep)
11394 {
11395 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11396 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11397 }
11398 if (plt_static_chain)
11399 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11400 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11401 }
11402 }
11403 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11404 {
11405 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11406 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11407 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11408 }
11409 else
11410 bfd_put_32 (obfd, BCTR, p), p += 4;
11411 return p;
11412 }
11413
11414 /* Build a special .plt call stub for __tls_get_addr. */
11415
11416 #define LD_R0_0R3 0xe8030000
11417 #define LD_R12_0R3 0xe9830000
11418 #define MR_R0_R3 0x7c601b78
11419 #define CMPDI_R0_0 0x2c200000
11420 #define ADD_R3_R12_R13 0x7c6c6a14
11421 #define BEQLR 0x4d820020
11422 #define MR_R3_R0 0x7c030378
11423 #define BCTRL 0x4e800421
11424
11425 static bfd_byte *
11426 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11427 struct ppc_stub_hash_entry *stub_entry,
11428 bfd_byte *p)
11429 {
11430 bfd *obfd = htab->params->stub_bfd;
11431
11432 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11433 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11434 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11435 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11436 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11437 bfd_put_32 (obfd, BEQLR, p), p += 4;
11438 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11439
11440 if (!htab->params->no_tls_get_addr_regsave)
11441 p = tls_get_addr_prologue (obfd, p, htab);
11442 else if (stub_entry->type.r2save)
11443 {
11444 bfd_put_32 (obfd, MFLR_R0, p);
11445 p += 4;
11446 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11447 p += 4;
11448 }
11449 return p;
11450 }
11451
11452 static bfd_byte *
11453 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11454 struct ppc_stub_hash_entry *stub_entry,
11455 bfd_byte *p,
11456 bfd_byte *loc)
11457 {
11458 bfd *obfd = htab->params->stub_bfd;
11459
11460 if (!htab->params->no_tls_get_addr_regsave)
11461 {
11462 bfd_put_32 (obfd, BCTRL, p - 4);
11463
11464 if (stub_entry->type.r2save)
11465 {
11466 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11467 p += 4;
11468 }
11469 p = tls_get_addr_epilogue (obfd, p, htab);
11470 }
11471 else if (stub_entry->type.r2save)
11472 {
11473 bfd_put_32 (obfd, BCTRL, p - 4);
11474
11475 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11476 p += 4;
11477 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11478 p += 4;
11479 bfd_put_32 (obfd, MTLR_R0, p);
11480 p += 4;
11481 bfd_put_32 (obfd, BLR, p);
11482 p += 4;
11483 }
11484
11485 if (htab->glink_eh_frame != NULL
11486 && htab->glink_eh_frame->size != 0)
11487 {
11488 bfd_byte *base, *eh;
11489
11490 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11491 eh = base + stub_entry->group->eh_size;
11492
11493 if (!htab->params->no_tls_get_addr_regsave)
11494 {
11495 unsigned int cfa_updt, delta, i;
11496
11497 /* After the bctrl, lr has been modified so we need to emit
11498 .eh_frame info saying the return address is on the stack. In
11499 fact we must put the EH info at or before the call rather
11500 than after it, because the EH info for a call needs to be
11501 specified by that point.
11502 See libgcc/unwind-dw2.c execute_cfa_program.
11503 Any stack pointer update must be described immediately after
11504 the instruction making the change, and since the stdu occurs
11505 after saving regs we put all the reg saves and the cfa
11506 change there. */
11507 cfa_updt = stub_entry->stub_offset + 18 * 4;
11508 delta = cfa_updt - stub_entry->group->lr_restore;
11509 stub_entry->group->lr_restore
11510 = stub_entry->stub_offset + (p - loc) - 4;
11511 eh = eh_advance (htab->elf.dynobj, eh, delta);
11512 *eh++ = DW_CFA_def_cfa_offset;
11513 if (htab->opd_abi)
11514 {
11515 *eh++ = 128;
11516 *eh++ = 1;
11517 }
11518 else
11519 *eh++ = 96;
11520 *eh++ = DW_CFA_offset_extended_sf;
11521 *eh++ = 65;
11522 *eh++ = (-16 / 8) & 0x7f;
11523 for (i = 4; i < 12; i++)
11524 {
11525 *eh++ = DW_CFA_offset + i;
11526 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11527 }
11528 *eh++ = (DW_CFA_advance_loc
11529 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11530 *eh++ = DW_CFA_def_cfa_offset;
11531 *eh++ = 0;
11532 for (i = 4; i < 12; i++)
11533 *eh++ = DW_CFA_restore + i;
11534 *eh++ = DW_CFA_advance_loc + 2;
11535 *eh++ = DW_CFA_restore_extended;
11536 *eh++ = 65;
11537 stub_entry->group->eh_size = eh - base;
11538 }
11539 else if (stub_entry->type.r2save)
11540 {
11541 unsigned int lr_used, delta;
11542
11543 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11544 delta = lr_used - stub_entry->group->lr_restore;
11545 stub_entry->group->lr_restore = lr_used + 16;
11546 eh = eh_advance (htab->elf.dynobj, eh, delta);
11547 *eh++ = DW_CFA_offset_extended_sf;
11548 *eh++ = 65;
11549 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11550 *eh++ = DW_CFA_advance_loc + 4;
11551 *eh++ = DW_CFA_restore_extended;
11552 *eh++ = 65;
11553 stub_entry->group->eh_size = eh - base;
11554 }
11555 }
11556 return p;
11557 }
11558
11559 static Elf_Internal_Rela *
11560 get_relocs (asection *sec, int count)
11561 {
11562 Elf_Internal_Rela *relocs;
11563 struct bfd_elf_section_data *elfsec_data;
11564
11565 elfsec_data = elf_section_data (sec);
11566 relocs = elfsec_data->relocs;
11567 if (relocs == NULL)
11568 {
11569 bfd_size_type relsize;
11570 relsize = sec->reloc_count * sizeof (*relocs);
11571 relocs = bfd_alloc (sec->owner, relsize);
11572 if (relocs == NULL)
11573 return NULL;
11574 elfsec_data->relocs = relocs;
11575 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11576 sizeof (Elf_Internal_Shdr));
11577 if (elfsec_data->rela.hdr == NULL)
11578 return NULL;
11579 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11580 * sizeof (Elf64_External_Rela));
11581 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11582 sec->reloc_count = 0;
11583 }
11584 relocs += sec->reloc_count;
11585 sec->reloc_count += count;
11586 return relocs;
11587 }
11588
11589 static bool
11590 swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
11591 {
11592 if ((size_t) (loc - s->contents) >= s->size)
11593 return false;
11594 bfd_elf64_swap_reloca_out (obfd, rel, loc);
11595 return true;
11596 }
11597
11598 static bool
11599 count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
11600 {
11601 bfd_byte *loc = s->contents;
11602 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
11603 return swap_reloc_out (obfd, rel, loc, s);
11604 }
11605
11606
11607 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11608 forms, to the equivalent relocs against the global symbol given by
11609 STUB_ENTRY->H. */
11610
11611 static bool
11612 use_global_in_relocs (struct ppc_link_hash_table *htab,
11613 struct ppc_stub_hash_entry *stub_entry,
11614 Elf_Internal_Rela *r, unsigned int num_rel)
11615 {
11616 struct elf_link_hash_entry **hashes;
11617 unsigned long symndx;
11618 struct ppc_link_hash_entry *h;
11619 bfd_vma symval;
11620
11621 /* Relocs are always against symbols in their own object file. Fake
11622 up global sym hashes for the stub bfd (which has no symbols). */
11623 hashes = elf_sym_hashes (htab->params->stub_bfd);
11624 if (hashes == NULL)
11625 {
11626 bfd_size_type hsize;
11627
11628 /* When called the first time, stub_globals will contain the
11629 total number of symbols seen during stub sizing. After
11630 allocating, stub_globals is used as an index to fill the
11631 hashes array. */
11632 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11633 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11634 if (hashes == NULL)
11635 return false;
11636 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11637 htab->stub_globals = 1;
11638 }
11639 symndx = htab->stub_globals++;
11640 h = stub_entry->h;
11641 hashes[symndx] = &h->elf;
11642 if (h->oh != NULL && h->oh->is_func)
11643 h = ppc_follow_link (h->oh);
11644 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11645 || h->elf.root.type == bfd_link_hash_defweak);
11646 symval = defined_sym_val (&h->elf);
11647 while (num_rel-- != 0)
11648 {
11649 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11650 if (h->elf.root.u.def.section != stub_entry->target_section)
11651 {
11652 /* H is an opd symbol. The addend must be zero, and the
11653 branch reloc is the only one we can convert. */
11654 r->r_addend = 0;
11655 break;
11656 }
11657 else
11658 r->r_addend -= symval;
11659 --r;
11660 }
11661 return true;
11662 }
11663
11664 static bfd_vma
11665 get_r2off (struct bfd_link_info *info,
11666 struct ppc_stub_hash_entry *stub_entry)
11667 {
11668 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11669 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11670
11671 if (r2off == 0)
11672 {
11673 /* Support linking -R objects. Get the toc pointer from the
11674 opd entry. */
11675 char buf[8];
11676 if (!htab->opd_abi)
11677 return r2off;
11678 asection *opd = stub_entry->h->elf.root.u.def.section;
11679 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11680
11681 if (strcmp (opd->name, ".opd") != 0
11682 || opd->reloc_count != 0)
11683 {
11684 info->callbacks->einfo
11685 (_("%P: cannot find opd entry toc for `%pT'\n"),
11686 stub_entry->h->elf.root.root.string);
11687 bfd_set_error (bfd_error_bad_value);
11688 return (bfd_vma) -1;
11689 }
11690 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11691 return (bfd_vma) -1;
11692 r2off = bfd_get_64 (opd->owner, buf);
11693 r2off -= elf_gp (info->output_bfd);
11694 }
11695 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11696 return r2off;
11697 }
11698
11699 /* Debug dump. */
11700
11701 static void
11702 dump_stub (const char *header,
11703 struct ppc_stub_hash_entry *stub_entry,
11704 size_t end_offset)
11705 {
11706 const char *t1, *t2, *t3;
11707 switch (stub_entry->type.main)
11708 {
11709 case ppc_stub_none: t1 = "none"; break;
11710 case ppc_stub_long_branch: t1 = "long_branch"; break;
11711 case ppc_stub_plt_branch: t1 = "plt_branch"; break;
11712 case ppc_stub_plt_call: t1 = "plt_call"; break;
11713 case ppc_stub_global_entry: t1 = "global_entry"; break;
11714 case ppc_stub_save_res: t1 = "save_res"; break;
11715 default: t1 = "???"; break;
11716 }
11717 switch (stub_entry->type.sub)
11718 {
11719 case ppc_stub_toc: t2 = "toc"; break;
11720 case ppc_stub_notoc: t2 = "notoc"; break;
11721 case ppc_stub_p9notoc: t2 = "p9notoc"; break;
11722 default: t2 = "???"; break;
11723 }
11724 t3 = stub_entry->type.r2save ? "r2save" : "";
11725 fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11726 header, stub_entry->id, t1, t2, t3);
11727 fprintf (stderr, "name = %s\n", stub_entry->root.string);
11728 fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
11729 for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11730 {
11731 asection *stub_sec = stub_entry->group->stub_sec;
11732 uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11733 fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
11734 }
11735 fprintf (stderr, "\n");
11736 }
11737
11738 static bool
11739 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11740 {
11741 struct ppc_stub_hash_entry *stub_entry;
11742 struct ppc_branch_hash_entry *br_entry;
11743 struct bfd_link_info *info;
11744 struct ppc_link_hash_table *htab;
11745 bfd *obfd;
11746 bfd_byte *loc;
11747 bfd_byte *p, *relp;
11748 bfd_vma targ, off;
11749 Elf_Internal_Rela *r;
11750 asection *plt;
11751 int num_rel;
11752 int odd;
11753 bool is_tga;
11754
11755 /* Massage our args to the form they really have. */
11756 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11757 info = in_arg;
11758
11759 htab = ppc_hash_table (info);
11760 if (htab == NULL)
11761 return false;
11762
11763 struct _ppc64_elf_section_data *esd
11764 = ppc64_elf_section_data (stub_entry->group->stub_sec);
11765 ++htab->stub_id;
11766 if (stub_entry->id != htab->stub_id
11767 || (stub_entry->type.main != ppc_stub_save_res
11768 && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
11769 {
11770 BFD_ASSERT (0);
11771 if (stub_entry->id != htab->stub_id)
11772 fprintf (stderr, "Expected id %u, got %u\n",
11773 htab->stub_id, stub_entry->id);
11774 if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
11775 fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11776 PRIx64 "\n", stub_entry->group->stub_sec->size,
11777 stub_entry->stub_offset);
11778 if (esd->sec_type == sec_stub)
11779 dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11780 dump_stub ("Current:", stub_entry, 0);
11781 }
11782 if (esd->sec_type == sec_normal)
11783 esd->sec_type = sec_stub;
11784 if (esd->sec_type == sec_stub)
11785 esd->u.last_ent = stub_entry;
11786 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11787
11788 htab->stub_count[stub_entry->type.main - 1] += 1;
11789 if (stub_entry->type.main == ppc_stub_long_branch
11790 && stub_entry->type.sub == ppc_stub_toc)
11791 {
11792 /* Branches are relative. This is where we are going to. */
11793 targ = (stub_entry->target_value
11794 + stub_entry->target_section->output_offset
11795 + stub_entry->target_section->output_section->vma);
11796 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11797
11798 /* And this is where we are coming from. */
11799 off = (stub_entry->stub_offset
11800 + stub_entry->group->stub_sec->output_offset
11801 + stub_entry->group->stub_sec->output_section->vma);
11802 off = targ - off;
11803
11804 p = loc;
11805 obfd = htab->params->stub_bfd;
11806 if (stub_entry->type.r2save)
11807 {
11808 bfd_vma r2off = get_r2off (info, stub_entry);
11809
11810 if (r2off == (bfd_vma) -1)
11811 {
11812 htab->stub_error = true;
11813 return false;
11814 }
11815 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11816 p += 4;
11817 if (PPC_HA (r2off) != 0)
11818 {
11819 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11820 p += 4;
11821 }
11822 if (PPC_LO (r2off) != 0)
11823 {
11824 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11825 p += 4;
11826 }
11827 off -= p - loc;
11828 }
11829 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11830 p += 4;
11831
11832 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11833 {
11834 _bfd_error_handler
11835 (_("long branch stub `%s' offset overflow"),
11836 stub_entry->root.string);
11837 htab->stub_error = true;
11838 return false;
11839 }
11840
11841 if (info->emitrelocations)
11842 {
11843 r = get_relocs (stub_entry->group->stub_sec, 1);
11844 if (r == NULL)
11845 return false;
11846 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11847 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11848 r->r_addend = targ;
11849 if (stub_entry->h != NULL
11850 && !use_global_in_relocs (htab, stub_entry, r, 1))
11851 return false;
11852 }
11853 }
11854 else if (stub_entry->type.main == ppc_stub_plt_branch
11855 && stub_entry->type.sub == ppc_stub_toc)
11856 {
11857 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11858 stub_entry->root.string + 9,
11859 false, false);
11860 if (br_entry == NULL)
11861 {
11862 _bfd_error_handler (_("can't find branch stub `%s'"),
11863 stub_entry->root.string);
11864 htab->stub_error = true;
11865 return false;
11866 }
11867
11868 targ = (stub_entry->target_value
11869 + stub_entry->target_section->output_offset
11870 + stub_entry->target_section->output_section->vma);
11871 if (!stub_entry->type.r2save)
11872 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11873
11874 bfd_put_64 (htab->brlt->owner, targ,
11875 htab->brlt->contents + br_entry->offset);
11876
11877 if (br_entry->iter == htab->stub_iteration)
11878 {
11879 br_entry->iter = 0;
11880
11881 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11882 {
11883 /* Create a reloc for the branch lookup table entry. */
11884 Elf_Internal_Rela rela;
11885
11886 rela.r_offset = (br_entry->offset
11887 + htab->brlt->output_offset
11888 + htab->brlt->output_section->vma);
11889 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11890 rela.r_addend = targ;
11891
11892 BFD_ASSERT (count_and_swap_reloc_out (htab->relbrlt->owner, &rela,
11893 htab->relbrlt));
11894 }
11895 else if (info->emitrelocations)
11896 {
11897 r = get_relocs (htab->brlt, 1);
11898 if (r == NULL)
11899 return false;
11900 /* brlt, being SEC_LINKER_CREATED does not go through the
11901 normal reloc processing. Symbols and offsets are not
11902 translated from input file to output file form, so
11903 set up the offset per the output file. */
11904 r->r_offset = (br_entry->offset
11905 + htab->brlt->output_offset
11906 + htab->brlt->output_section->vma);
11907 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11908 r->r_addend = targ;
11909 }
11910 }
11911
11912 targ = (br_entry->offset
11913 + htab->brlt->output_offset
11914 + htab->brlt->output_section->vma);
11915
11916 off = (elf_gp (info->output_bfd)
11917 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11918 off = targ - off;
11919
11920 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11921 {
11922 info->callbacks->einfo
11923 (_("%P: linkage table error against `%pT'\n"),
11924 stub_entry->root.string);
11925 bfd_set_error (bfd_error_bad_value);
11926 htab->stub_error = true;
11927 return false;
11928 }
11929
11930 if (info->emitrelocations)
11931 {
11932 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11933 if (r == NULL)
11934 return false;
11935 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11936 if (bfd_big_endian (info->output_bfd))
11937 r[0].r_offset += 2;
11938 if (stub_entry->type.r2save)
11939 r[0].r_offset += 4;
11940 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11941 r[0].r_addend = targ;
11942 if (PPC_HA (off) != 0)
11943 {
11944 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11945 r[1].r_offset = r[0].r_offset + 4;
11946 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11947 r[1].r_addend = r[0].r_addend;
11948 }
11949 }
11950
11951 p = loc;
11952 obfd = htab->params->stub_bfd;
11953 if (!stub_entry->type.r2save)
11954 {
11955 if (PPC_HA (off) != 0)
11956 {
11957 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11958 p += 4;
11959 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11960 }
11961 else
11962 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11963 }
11964 else
11965 {
11966 bfd_vma r2off = get_r2off (info, stub_entry);
11967
11968 if (r2off == (bfd_vma) -1)
11969 {
11970 htab->stub_error = true;
11971 return false;
11972 }
11973
11974 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11975 p += 4;
11976 if (PPC_HA (off) != 0)
11977 {
11978 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11979 p += 4;
11980 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11981 }
11982 else
11983 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11984
11985 if (PPC_HA (r2off) != 0)
11986 {
11987 p += 4;
11988 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11989 }
11990 if (PPC_LO (r2off) != 0)
11991 {
11992 p += 4;
11993 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11994 }
11995 }
11996 p += 4;
11997 bfd_put_32 (obfd, MTCTR_R12, p);
11998 p += 4;
11999 bfd_put_32 (obfd, BCTR, p);
12000 p += 4;
12001 }
12002 else if (stub_entry->type.sub >= ppc_stub_notoc)
12003 {
12004 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
12005 p = loc;
12006 off = (stub_entry->stub_offset
12007 + stub_entry->group->stub_sec->output_offset
12008 + stub_entry->group->stub_sec->output_section->vma);
12009 obfd = htab->params->stub_bfd;
12010 is_tga = (is_plt
12011 && stub_entry->h != NULL
12012 && is_tls_get_addr (&stub_entry->h->elf, htab)
12013 && htab->params->tls_get_addr_opt);
12014 if (is_tga)
12015 {
12016 p = build_tls_get_addr_head (htab, stub_entry, p);
12017 off += p - loc;
12018 }
12019 if (stub_entry->type.r2save)
12020 {
12021 off += 4;
12022 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
12023 p += 4;
12024 }
12025 if (is_plt)
12026 {
12027 targ = stub_entry->plt_ent->plt.offset & ~1;
12028 if (targ >= (bfd_vma) -2)
12029 abort ();
12030
12031 plt = htab->elf.splt;
12032 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12033 {
12034 if (stub_entry->symtype == STT_GNU_IFUNC)
12035 plt = htab->elf.iplt;
12036 else
12037 plt = htab->pltlocal;
12038 }
12039 targ += plt->output_offset + plt->output_section->vma;
12040 }
12041 else
12042 targ = (stub_entry->target_value
12043 + stub_entry->target_section->output_offset
12044 + stub_entry->target_section->output_section->vma);
12045 odd = off & 4;
12046 off = targ - off;
12047
12048 relp = p;
12049 num_rel = 0;
12050 if (stub_entry->type.sub == ppc_stub_notoc)
12051 p = build_power10_offset (obfd, p, off, odd, is_plt);
12052 else
12053 {
12054 if (htab->glink_eh_frame != NULL
12055 && htab->glink_eh_frame->size != 0)
12056 {
12057 bfd_byte *base, *eh;
12058 unsigned int lr_used, delta;
12059
12060 base = (htab->glink_eh_frame->contents
12061 + stub_entry->group->eh_base + 17);
12062 eh = base + stub_entry->group->eh_size;
12063 lr_used = stub_entry->stub_offset + (p - loc) + 8;
12064 delta = lr_used - stub_entry->group->lr_restore;
12065 stub_entry->group->lr_restore = lr_used + 8;
12066 eh = eh_advance (htab->elf.dynobj, eh, delta);
12067 *eh++ = DW_CFA_register;
12068 *eh++ = 65;
12069 *eh++ = 12;
12070 *eh++ = DW_CFA_advance_loc + 2;
12071 *eh++ = DW_CFA_restore_extended;
12072 *eh++ = 65;
12073 stub_entry->group->eh_size = eh - base;
12074 }
12075
12076 /* The notoc stubs calculate their target (either a PLT entry or
12077 the global entry point of a function) relative to the PC
12078 returned by the "bcl" two instructions past the start of the
12079 sequence emitted by build_offset. The offset is therefore 8
12080 less than calculated from the start of the sequence. */
12081 off -= 8;
12082 p = build_offset (obfd, p, off, is_plt);
12083 }
12084
12085 if (stub_entry->type.main == ppc_stub_long_branch)
12086 {
12087 bfd_vma from;
12088 num_rel = 1;
12089 from = (stub_entry->stub_offset
12090 + stub_entry->group->stub_sec->output_offset
12091 + stub_entry->group->stub_sec->output_section->vma
12092 + (p - loc));
12093 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12094 }
12095 else
12096 {
12097 bfd_put_32 (obfd, MTCTR_R12, p);
12098 p += 4;
12099 bfd_put_32 (obfd, BCTR, p);
12100 }
12101 p += 4;
12102
12103 if (is_tga)
12104 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12105
12106 if (info->emitrelocations)
12107 {
12108 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12109 if (stub_entry->type.sub == ppc_stub_notoc)
12110 num_rel += num_relocs_for_power10_offset (off, odd);
12111 else
12112 {
12113 num_rel += num_relocs_for_offset (off);
12114 roff += 16;
12115 }
12116 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12117 if (r == NULL)
12118 return false;
12119 if (stub_entry->type.sub == ppc_stub_notoc)
12120 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12121 else
12122 r = emit_relocs_for_offset (info, r, roff, targ, off);
12123 if (stub_entry->type.main == ppc_stub_long_branch)
12124 {
12125 ++r;
12126 roff = p - 4 - stub_entry->group->stub_sec->contents;
12127 r->r_offset = roff;
12128 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12129 r->r_addend = targ;
12130 if (stub_entry->h != NULL
12131 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12132 return false;
12133 }
12134 }
12135 }
12136 else if (stub_entry->type.main == ppc_stub_plt_call)
12137 {
12138 if (stub_entry->h != NULL
12139 && stub_entry->h->is_func_descriptor
12140 && stub_entry->h->oh != NULL)
12141 {
12142 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12143
12144 /* If the old-ABI "dot-symbol" is undefined make it weak so
12145 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12146 if (fh->elf.root.type == bfd_link_hash_undefined
12147 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12148 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12149 fh->elf.root.type = bfd_link_hash_undefweak;
12150 }
12151
12152 /* Now build the stub. */
12153 targ = stub_entry->plt_ent->plt.offset & ~1;
12154 if (targ >= (bfd_vma) -2)
12155 abort ();
12156
12157 plt = htab->elf.splt;
12158 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12159 {
12160 if (stub_entry->symtype == STT_GNU_IFUNC)
12161 plt = htab->elf.iplt;
12162 else
12163 plt = htab->pltlocal;
12164 }
12165 targ += plt->output_offset + plt->output_section->vma;
12166
12167 off = (elf_gp (info->output_bfd)
12168 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12169 off = targ - off;
12170
12171 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12172 {
12173 info->callbacks->einfo
12174 /* xgettext:c-format */
12175 (_("%P: linkage table error against `%pT'\n"),
12176 stub_entry->h != NULL
12177 ? stub_entry->h->elf.root.root.string
12178 : "<local sym>");
12179 bfd_set_error (bfd_error_bad_value);
12180 htab->stub_error = true;
12181 return false;
12182 }
12183
12184 r = NULL;
12185 if (info->emitrelocations)
12186 {
12187 r = get_relocs (stub_entry->group->stub_sec,
12188 ((PPC_HA (off) != 0)
12189 + (htab->opd_abi
12190 ? 2 + (htab->params->plt_static_chain
12191 && PPC_HA (off + 16) == PPC_HA (off))
12192 : 1)));
12193 if (r == NULL)
12194 return false;
12195 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12196 if (bfd_big_endian (info->output_bfd))
12197 r[0].r_offset += 2;
12198 r[0].r_addend = targ;
12199 }
12200 p = loc;
12201 obfd = htab->params->stub_bfd;
12202 is_tga = (stub_entry->h != NULL
12203 && is_tls_get_addr (&stub_entry->h->elf, htab)
12204 && htab->params->tls_get_addr_opt);
12205 if (is_tga)
12206 {
12207 p = build_tls_get_addr_head (htab, stub_entry, p);
12208 if (r != NULL)
12209 r[0].r_offset += p - loc;
12210 }
12211 p = build_plt_stub (htab, stub_entry, p, off, r);
12212 if (is_tga)
12213 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12214 }
12215 else if (stub_entry->type.main == ppc_stub_save_res)
12216 return true;
12217 else
12218 {
12219 BFD_FAIL ();
12220 return false;
12221 }
12222
12223 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12224
12225 if (htab->params->emit_stub_syms)
12226 {
12227 struct elf_link_hash_entry *h;
12228 size_t len1, len2;
12229 char *name;
12230 const char *const stub_str[] = { "long_branch",
12231 "plt_branch",
12232 "plt_call" };
12233
12234 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12235 len2 = strlen (stub_entry->root.string);
12236 name = bfd_malloc (len1 + len2 + 2);
12237 if (name == NULL)
12238 return false;
12239 memcpy (name, stub_entry->root.string, 9);
12240 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12241 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12242 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12243 if (h == NULL)
12244 return false;
12245 if (h->root.type == bfd_link_hash_new)
12246 {
12247 h->root.type = bfd_link_hash_defined;
12248 h->root.u.def.section = stub_entry->group->stub_sec;
12249 h->root.u.def.value = stub_entry->stub_offset;
12250 h->ref_regular = 1;
12251 h->def_regular = 1;
12252 h->ref_regular_nonweak = 1;
12253 h->forced_local = 1;
12254 h->non_elf = 0;
12255 h->root.linker_def = 1;
12256 }
12257 }
12258
12259 return true;
12260 }
12261
12262 /* As above, but don't actually build the stub. Just bump offset so
12263 we know stub section sizes, and select plt_branch stubs where
12264 long_branch stubs won't do. */
12265
12266 static bool
12267 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12268 {
12269 struct ppc_stub_hash_entry *stub_entry;
12270 struct bfd_link_info *info;
12271 struct ppc_link_hash_table *htab;
12272 asection *plt;
12273 bfd_vma targ, off, r2off;
12274 unsigned int size, pad, extra, lr_used, delta, odd;
12275 bfd_vma stub_offset;
12276
12277 /* Massage our args to the form they really have. */
12278 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12279 info = in_arg;
12280
12281 htab = ppc_hash_table (info);
12282 if (htab == NULL)
12283 return false;
12284
12285 /* Fail if the target section could not be assigned to an output
12286 section. The user should fix his linker script. */
12287 if (stub_entry->target_section != NULL
12288 && stub_entry->target_section->output_section == NULL
12289 && info->non_contiguous_regions)
12290 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12291 "Retry without --enable-non-contiguous-regions.\n"),
12292 stub_entry->target_section);
12293
12294 /* Same for the group. */
12295 if (stub_entry->group->stub_sec != NULL
12296 && stub_entry->group->stub_sec->output_section == NULL
12297 && info->non_contiguous_regions)
12298 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12299 "Retry without --enable-non-contiguous-regions.\n"),
12300 stub_entry->group->stub_sec);
12301
12302 /* Make a note of the offset within the stubs for this entry. */
12303 stub_offset = stub_entry->group->stub_sec->size;
12304 if (htab->stub_iteration > STUB_SHRINK_ITER
12305 && stub_entry->stub_offset > stub_offset)
12306 stub_offset = stub_entry->stub_offset;
12307 stub_entry->id = ++htab->stub_id;
12308
12309 if (stub_entry->h != NULL
12310 && stub_entry->h->save_res
12311 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12312 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12313 {
12314 /* Don't make stubs to out-of-line register save/restore
12315 functions. Instead, emit copies of the functions. */
12316 stub_entry->group->needs_save_res = 1;
12317 stub_entry->type.main = ppc_stub_save_res;
12318 stub_entry->type.sub = ppc_stub_toc;
12319 stub_entry->type.r2save = 0;
12320 return true;
12321 }
12322
12323 if (stub_entry->type.main == ppc_stub_plt_branch)
12324 {
12325 /* Reset the stub type from the plt branch variant in case we now
12326 can reach with a shorter stub. */
12327 stub_entry->type.main = ppc_stub_long_branch;
12328 }
12329
12330 if (stub_entry->type.main == ppc_stub_long_branch
12331 && stub_entry->type.sub == ppc_stub_toc)
12332 {
12333 targ = (stub_entry->target_value
12334 + stub_entry->target_section->output_offset
12335 + stub_entry->target_section->output_section->vma);
12336 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12337 off = (stub_offset
12338 + stub_entry->group->stub_sec->output_offset
12339 + stub_entry->group->stub_sec->output_section->vma);
12340
12341 size = 4;
12342 r2off = 0;
12343 if (stub_entry->type.r2save)
12344 {
12345 r2off = get_r2off (info, stub_entry);
12346 if (r2off == (bfd_vma) -1)
12347 {
12348 htab->stub_error = true;
12349 return false;
12350 }
12351 size = 8;
12352 if (PPC_HA (r2off) != 0)
12353 size += 4;
12354 if (PPC_LO (r2off) != 0)
12355 size += 4;
12356 off += size - 4;
12357 }
12358 off = targ - off;
12359
12360 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12361 Do the same for -R objects without function descriptors. */
12362 if ((stub_entry->type.r2save
12363 && r2off == 0
12364 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12365 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12366 {
12367 struct ppc_branch_hash_entry *br_entry;
12368
12369 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12370 stub_entry->root.string + 9,
12371 true, false);
12372 if (br_entry == NULL)
12373 {
12374 _bfd_error_handler (_("can't build branch stub `%s'"),
12375 stub_entry->root.string);
12376 htab->stub_error = true;
12377 return false;
12378 }
12379
12380 if (br_entry->iter != htab->stub_iteration)
12381 {
12382 br_entry->iter = htab->stub_iteration;
12383 br_entry->offset = htab->brlt->size;
12384 htab->brlt->size += 8;
12385
12386 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12387 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12388 else if (info->emitrelocations)
12389 {
12390 htab->brlt->reloc_count += 1;
12391 htab->brlt->flags |= SEC_RELOC;
12392 }
12393 }
12394
12395 targ = (br_entry->offset
12396 + htab->brlt->output_offset
12397 + htab->brlt->output_section->vma);
12398 off = (elf_gp (info->output_bfd)
12399 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12400 off = targ - off;
12401
12402 if (info->emitrelocations)
12403 {
12404 stub_entry->group->stub_sec->reloc_count
12405 += 1 + (PPC_HA (off) != 0);
12406 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12407 }
12408
12409 stub_entry->type.main = ppc_stub_plt_branch;
12410 if (!stub_entry->type.r2save)
12411 {
12412 size = 12;
12413 if (PPC_HA (off) != 0)
12414 size = 16;
12415 }
12416 else
12417 {
12418 size = 16;
12419 if (PPC_HA (off) != 0)
12420 size += 4;
12421
12422 if (PPC_HA (r2off) != 0)
12423 size += 4;
12424 if (PPC_LO (r2off) != 0)
12425 size += 4;
12426 }
12427 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12428 stub_offset += pad;
12429 }
12430 else if (info->emitrelocations)
12431 {
12432 stub_entry->group->stub_sec->reloc_count += 1;
12433 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12434 }
12435 }
12436 else if (stub_entry->type.main == ppc_stub_long_branch)
12437 {
12438 off = (stub_offset
12439 + stub_entry->group->stub_sec->output_offset
12440 + stub_entry->group->stub_sec->output_section->vma);
12441 size = 0;
12442 if (stub_entry->type.r2save)
12443 size = 4;
12444 off += size;
12445 targ = (stub_entry->target_value
12446 + stub_entry->target_section->output_offset
12447 + stub_entry->target_section->output_section->vma);
12448 odd = off & 4;
12449 off = targ - off;
12450
12451 if (stub_entry->type.sub == ppc_stub_notoc)
12452 extra = size_power10_offset (off, odd);
12453 else
12454 extra = size_offset (off - 8);
12455 /* Include branch insn plus those in the offset sequence. */
12456 size += 4 + extra;
12457
12458 /* If the branch can't reach, use a plt_branch.
12459 The branch insn is at the end, or "extra" bytes along. So
12460 its offset will be "extra" bytes less that that already
12461 calculated. */
12462 if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12463 {
12464 stub_entry->type.main = ppc_stub_plt_branch;
12465 size += 4;
12466 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12467 if (pad != 0)
12468 {
12469 stub_offset += pad;
12470 off -= pad;
12471 odd ^= pad & 4;
12472 size -= extra;
12473 if (stub_entry->type.sub == ppc_stub_notoc)
12474 extra = size_power10_offset (off, odd);
12475 else
12476 extra = size_offset (off - 8);
12477 size += extra;
12478 }
12479 }
12480 else if (info->emitrelocations)
12481 stub_entry->group->stub_sec->reloc_count +=1;
12482
12483 if (info->emitrelocations)
12484 {
12485 unsigned int num_rel;
12486 if (stub_entry->type.sub == ppc_stub_notoc)
12487 num_rel = num_relocs_for_power10_offset (off, odd);
12488 else
12489 num_rel = num_relocs_for_offset (off - 8);
12490 stub_entry->group->stub_sec->reloc_count += num_rel;
12491 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12492 }
12493
12494 if (stub_entry->type.sub != ppc_stub_notoc)
12495 {
12496 /* After the bcl, lr has been modified so we need to emit
12497 .eh_frame info saying the return address is in r12. */
12498 lr_used = stub_offset + 8;
12499 if (stub_entry->type.r2save)
12500 lr_used += 4;
12501 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12502 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12503 DW_CFA_restore_extended 65. */
12504 delta = lr_used - stub_entry->group->lr_restore;
12505 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12506 stub_entry->group->lr_restore = lr_used + 8;
12507 }
12508 }
12509 else if (stub_entry->type.sub >= ppc_stub_notoc)
12510 {
12511 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12512 lr_used = 0;
12513 if (stub_entry->h != NULL
12514 && is_tls_get_addr (&stub_entry->h->elf, htab)
12515 && htab->params->tls_get_addr_opt)
12516 {
12517 lr_used += 7 * 4;
12518 if (!htab->params->no_tls_get_addr_regsave)
12519 lr_used += 11 * 4;
12520 else if (stub_entry->type.r2save)
12521 lr_used += 2 * 4;
12522 }
12523 if (stub_entry->type.r2save)
12524 lr_used += 4;
12525 targ = stub_entry->plt_ent->plt.offset & ~1;
12526 if (targ >= (bfd_vma) -2)
12527 abort ();
12528
12529 plt = htab->elf.splt;
12530 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12531 {
12532 if (stub_entry->symtype == STT_GNU_IFUNC)
12533 plt = htab->elf.iplt;
12534 else
12535 plt = htab->pltlocal;
12536 }
12537 targ += plt->output_offset + plt->output_section->vma;
12538 off = (stub_offset
12539 + stub_entry->group->stub_sec->output_offset
12540 + stub_entry->group->stub_sec->output_section->vma
12541 + lr_used);
12542 odd = off & 4;
12543 off = targ - off;
12544
12545 size = plt_stub_size (htab, stub_entry, off, odd);
12546 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12547 if (pad != 0)
12548 {
12549 stub_offset += pad;
12550 off -= pad;
12551 odd ^= pad & 4;
12552 size = plt_stub_size (htab, stub_entry, off, odd);
12553 }
12554
12555 if (info->emitrelocations)
12556 {
12557 unsigned int num_rel;
12558 if (stub_entry->type.sub == ppc_stub_notoc)
12559 num_rel = num_relocs_for_power10_offset (off, odd);
12560 else
12561 num_rel = num_relocs_for_offset (off - 8);
12562 stub_entry->group->stub_sec->reloc_count += num_rel;
12563 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12564 }
12565
12566 if (stub_entry->type.sub != ppc_stub_notoc)
12567 {
12568 /* After the bcl, lr has been modified so we need to emit
12569 .eh_frame info saying the return address is in r12. */
12570 lr_used += stub_offset + 8;
12571 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12572 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12573 DW_CFA_restore_extended 65. */
12574 delta = lr_used - stub_entry->group->lr_restore;
12575 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12576 stub_entry->group->lr_restore = lr_used + 8;
12577 }
12578 if (stub_entry->h != NULL
12579 && is_tls_get_addr (&stub_entry->h->elf, htab)
12580 && htab->params->tls_get_addr_opt)
12581 {
12582 if (!htab->params->no_tls_get_addr_regsave)
12583 {
12584 unsigned int cfa_updt = stub_offset + 18 * 4;
12585 delta = cfa_updt - stub_entry->group->lr_restore;
12586 stub_entry->group->eh_size += eh_advance_size (delta);
12587 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12588 stub_entry->group->lr_restore = stub_offset + size - 4;
12589 }
12590 else if (stub_entry->type.r2save)
12591 {
12592 lr_used = stub_offset + size - 20;
12593 delta = lr_used - stub_entry->group->lr_restore;
12594 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12595 stub_entry->group->lr_restore = stub_offset + size - 4;
12596 }
12597 }
12598 }
12599 else if (stub_entry->type.main == ppc_stub_plt_call)
12600 {
12601 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12602 if (targ >= (bfd_vma) -2)
12603 abort ();
12604 plt = htab->elf.splt;
12605 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12606 {
12607 if (stub_entry->symtype == STT_GNU_IFUNC)
12608 plt = htab->elf.iplt;
12609 else
12610 plt = htab->pltlocal;
12611 }
12612 targ += plt->output_offset + plt->output_section->vma;
12613
12614 off = (elf_gp (info->output_bfd)
12615 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12616 off = targ - off;
12617
12618 size = plt_stub_size (htab, stub_entry, off, 0);
12619 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12620 stub_offset += pad;
12621
12622 if (info->emitrelocations)
12623 {
12624 stub_entry->group->stub_sec->reloc_count
12625 += ((PPC_HA (off) != 0)
12626 + (htab->opd_abi
12627 ? 2 + (htab->params->plt_static_chain
12628 && PPC_HA (off + 16) == PPC_HA (off))
12629 : 1));
12630 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12631 }
12632
12633 if (stub_entry->h != NULL
12634 && is_tls_get_addr (&stub_entry->h->elf, htab)
12635 && htab->params->tls_get_addr_opt
12636 && stub_entry->type.r2save)
12637 {
12638 if (!htab->params->no_tls_get_addr_regsave)
12639 {
12640 /* Adjustments to r1 need to be described. */
12641 unsigned int cfa_updt = stub_offset + 18 * 4;
12642 delta = cfa_updt - stub_entry->group->lr_restore;
12643 stub_entry->group->eh_size += eh_advance_size (delta);
12644 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12645 }
12646 else
12647 {
12648 lr_used = stub_offset + size - 20;
12649 /* The eh_frame info will consist of a DW_CFA_advance_loc
12650 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12651 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12652 delta = lr_used - stub_entry->group->lr_restore;
12653 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12654 }
12655 stub_entry->group->lr_restore = stub_offset + size - 4;
12656 }
12657 }
12658 else
12659 {
12660 BFD_FAIL ();
12661 return false;
12662 }
12663
12664 if (stub_entry->stub_offset != stub_offset)
12665 htab->stub_changed = true;
12666 stub_entry->stub_offset = stub_offset;
12667 stub_entry->group->stub_sec->size = stub_offset + size;
12668 return true;
12669 }
12670
12671 /* Set up various things so that we can make a list of input sections
12672 for each output section included in the link. Returns -1 on error,
12673 0 when no stubs will be needed, and 1 on success. */
12674
12675 int
12676 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12677 {
12678 unsigned int id;
12679 size_t amt;
12680 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12681
12682 if (htab == NULL)
12683 return -1;
12684
12685 /* The access to _bfd_section_id here is unlocked, so for the time
12686 being this function cannot be called in multi-threaded mode. */
12687 BFD_ASSERT (!_bfd_threading_enabled ());
12688
12689 htab->sec_info_arr_size = _bfd_section_id;
12690 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12691 htab->sec_info = bfd_zmalloc (amt);
12692 if (htab->sec_info == NULL)
12693 return -1;
12694
12695 /* Set toc_off for com, und, abs and ind sections. */
12696 for (id = 0; id < 3; id++)
12697 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12698
12699 return 1;
12700 }
12701
12702 /* Set up for first pass at multitoc partitioning. */
12703
12704 void
12705 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12706 {
12707 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12708
12709 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12710 htab->toc_bfd = NULL;
12711 htab->toc_first_sec = NULL;
12712 }
12713
12714 /* The linker repeatedly calls this function for each TOC input section
12715 and linker generated GOT section. Group input bfds such that the toc
12716 within a group is less than 64k in size. */
12717
12718 bool
12719 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12720 {
12721 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12722 bfd_vma addr, off, limit;
12723
12724 if (htab == NULL)
12725 return false;
12726
12727 if (!htab->second_toc_pass)
12728 {
12729 /* Keep track of the first .toc or .got section for this input bfd. */
12730 bool new_bfd = htab->toc_bfd != isec->owner;
12731
12732 if (new_bfd)
12733 {
12734 htab->toc_bfd = isec->owner;
12735 htab->toc_first_sec = isec;
12736 }
12737
12738 addr = isec->output_offset + isec->output_section->vma;
12739 off = addr - htab->toc_curr;
12740 limit = 0x80008000;
12741 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12742 limit = 0x10000;
12743 if (off + isec->size > limit)
12744 {
12745 addr = (htab->toc_first_sec->output_offset
12746 + htab->toc_first_sec->output_section->vma);
12747 htab->toc_curr = addr;
12748 htab->toc_curr &= -TOC_BASE_ALIGN;
12749 }
12750
12751 /* toc_curr is the base address of this toc group. Set elf_gp
12752 for the input section to be the offset relative to the
12753 output toc base plus 0x8000. Making the input elf_gp an
12754 offset allows us to move the toc as a whole without
12755 recalculating input elf_gp. */
12756 off = htab->toc_curr - elf_gp (info->output_bfd);
12757 off += TOC_BASE_OFF;
12758
12759 /* Die if someone uses a linker script that doesn't keep input
12760 file .toc and .got together. */
12761 if (new_bfd
12762 && elf_gp (isec->owner) != 0
12763 && elf_gp (isec->owner) != off)
12764 return false;
12765
12766 elf_gp (isec->owner) = off;
12767 return true;
12768 }
12769
12770 /* During the second pass toc_first_sec points to the start of
12771 a toc group, and toc_curr is used to track the old elf_gp.
12772 We use toc_bfd to ensure we only look at each bfd once. */
12773 if (htab->toc_bfd == isec->owner)
12774 return true;
12775 htab->toc_bfd = isec->owner;
12776
12777 if (htab->toc_first_sec == NULL
12778 || htab->toc_curr != elf_gp (isec->owner))
12779 {
12780 htab->toc_curr = elf_gp (isec->owner);
12781 htab->toc_first_sec = isec;
12782 }
12783 addr = (htab->toc_first_sec->output_offset
12784 + htab->toc_first_sec->output_section->vma);
12785 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12786 elf_gp (isec->owner) = off;
12787
12788 return true;
12789 }
12790
12791 /* Called via elf_link_hash_traverse to merge GOT entries for global
12792 symbol H. */
12793
12794 static bool
12795 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12796 {
12797 if (h->root.type == bfd_link_hash_indirect)
12798 return true;
12799
12800 merge_got_entries (&h->got.glist);
12801
12802 return true;
12803 }
12804
12805 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12806 symbol H. */
12807
12808 static bool
12809 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12810 {
12811 struct got_entry *gent;
12812
12813 if (h->root.type == bfd_link_hash_indirect)
12814 return true;
12815
12816 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12817 if (!gent->is_indirect)
12818 allocate_got (h, (struct bfd_link_info *) inf, gent);
12819 return true;
12820 }
12821
12822 /* Called on the first multitoc pass after the last call to
12823 ppc64_elf_next_toc_section. This function removes duplicate GOT
12824 entries. */
12825
12826 bool
12827 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12828 {
12829 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12830 struct bfd *ibfd, *ibfd2;
12831 bool done_something;
12832
12833 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12834
12835 if (!htab->do_multi_toc)
12836 return false;
12837
12838 /* Merge global sym got entries within a toc group. */
12839 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12840
12841 /* And tlsld_got. */
12842 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12843 {
12844 struct got_entry *ent, *ent2;
12845
12846 if (!is_ppc64_elf (ibfd))
12847 continue;
12848
12849 ent = ppc64_tlsld_got (ibfd);
12850 if (!ent->is_indirect
12851 && ent->got.offset != (bfd_vma) -1)
12852 {
12853 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12854 {
12855 if (!is_ppc64_elf (ibfd2))
12856 continue;
12857
12858 ent2 = ppc64_tlsld_got (ibfd2);
12859 if (!ent2->is_indirect
12860 && ent2->got.offset != (bfd_vma) -1
12861 && elf_gp (ibfd2) == elf_gp (ibfd))
12862 {
12863 ent2->is_indirect = true;
12864 ent2->got.ent = ent;
12865 }
12866 }
12867 }
12868 }
12869
12870 /* Zap sizes of got sections. */
12871 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12872 htab->elf.irelplt->size -= htab->got_reli_size;
12873 htab->got_reli_size = 0;
12874
12875 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12876 {
12877 asection *got, *relgot;
12878
12879 if (!is_ppc64_elf (ibfd))
12880 continue;
12881
12882 got = ppc64_elf_tdata (ibfd)->got;
12883 if (got != NULL)
12884 {
12885 got->rawsize = got->size;
12886 got->size = 0;
12887 relgot = ppc64_elf_tdata (ibfd)->relgot;
12888 relgot->rawsize = relgot->size;
12889 relgot->size = 0;
12890 }
12891 }
12892
12893 /* Now reallocate the got, local syms first. We don't need to
12894 allocate section contents again since we never increase size. */
12895 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12896 {
12897 struct got_entry **lgot_ents;
12898 struct got_entry **end_lgot_ents;
12899 struct plt_entry **local_plt;
12900 struct plt_entry **end_local_plt;
12901 unsigned char *lgot_masks;
12902 bfd_size_type locsymcount;
12903 Elf_Internal_Shdr *symtab_hdr;
12904 asection *s;
12905 Elf_Internal_Sym *local_syms;
12906 Elf_Internal_Sym *isym;
12907
12908 if (!is_ppc64_elf (ibfd))
12909 continue;
12910
12911 lgot_ents = elf_local_got_ents (ibfd);
12912 if (!lgot_ents)
12913 continue;
12914
12915 symtab_hdr = &elf_symtab_hdr (ibfd);
12916 locsymcount = symtab_hdr->sh_info;
12917 end_lgot_ents = lgot_ents + locsymcount;
12918 local_plt = (struct plt_entry **) end_lgot_ents;
12919 end_local_plt = local_plt + locsymcount;
12920 lgot_masks = (unsigned char *) end_local_plt;
12921 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12922 if (local_syms == NULL && locsymcount != 0)
12923 {
12924 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12925 0, NULL, NULL, NULL);
12926 if (local_syms == NULL)
12927 return false;
12928 }
12929 s = ppc64_elf_tdata (ibfd)->got;
12930 for (isym = local_syms;
12931 lgot_ents < end_lgot_ents;
12932 ++lgot_ents, ++lgot_masks, isym++)
12933 {
12934 struct got_entry *ent;
12935
12936 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12937 {
12938 unsigned int ent_size = 8;
12939 unsigned int rel_size = sizeof (Elf64_External_Rela);
12940
12941 ent->got.offset = s->size;
12942 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12943 {
12944 ent_size *= 2;
12945 rel_size *= 2;
12946 }
12947 s->size += ent_size;
12948 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12949 {
12950 htab->elf.irelplt->size += rel_size;
12951 htab->got_reli_size += rel_size;
12952 }
12953 else if (bfd_link_pic (info)
12954 && (ent->tls_type == 0
12955 ? !info->enable_dt_relr
12956 : !bfd_link_executable (info))
12957 && isym->st_shndx != SHN_ABS)
12958 {
12959 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12960 srel->size += rel_size;
12961 }
12962 }
12963 }
12964 }
12965
12966 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12967
12968 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12969 {
12970 struct got_entry *ent;
12971
12972 if (!is_ppc64_elf (ibfd))
12973 continue;
12974
12975 ent = ppc64_tlsld_got (ibfd);
12976 if (!ent->is_indirect
12977 && ent->got.offset != (bfd_vma) -1)
12978 {
12979 asection *s = ppc64_elf_tdata (ibfd)->got;
12980 ent->got.offset = s->size;
12981 s->size += 16;
12982 if (bfd_link_dll (info))
12983 {
12984 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12985 srel->size += sizeof (Elf64_External_Rela);
12986 }
12987 }
12988 }
12989
12990 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12991 if (!done_something)
12992 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12993 {
12994 asection *got;
12995
12996 if (!is_ppc64_elf (ibfd))
12997 continue;
12998
12999 got = ppc64_elf_tdata (ibfd)->got;
13000 if (got != NULL)
13001 {
13002 done_something = got->rawsize != got->size;
13003 if (done_something)
13004 break;
13005 }
13006 }
13007
13008 if (done_something)
13009 (*htab->params->layout_sections_again) ();
13010
13011 /* Set up for second pass over toc sections to recalculate elf_gp
13012 on input sections. */
13013 htab->toc_bfd = NULL;
13014 htab->toc_first_sec = NULL;
13015 htab->second_toc_pass = true;
13016 return done_something;
13017 }
13018
13019 /* Called after second pass of multitoc partitioning. */
13020
13021 void
13022 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
13023 {
13024 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13025
13026 /* After the second pass, toc_curr tracks the TOC offset used
13027 for code sections below in ppc64_elf_next_input_section. */
13028 htab->toc_curr = TOC_BASE_OFF;
13029 }
13030
13031 /* No toc references were found in ISEC. If the code in ISEC makes no
13032 calls, then there's no need to use toc adjusting stubs when branching
13033 into ISEC. Actually, indirect calls from ISEC are OK as they will
13034 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
13035 needed, and 2 if a cyclical call-graph was found but no other reason
13036 for a stub was detected. If called from the top level, a return of
13037 2 means the same as a return of 0. */
13038
13039 static int
13040 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
13041 {
13042 int ret;
13043
13044 /* Mark this section as checked. */
13045 isec->call_check_done = 1;
13046
13047 /* We know none of our code bearing sections will need toc stubs. */
13048 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13049 return 0;
13050
13051 if (isec->size == 0)
13052 return 0;
13053
13054 if (isec->output_section == NULL)
13055 return 0;
13056
13057 ret = 0;
13058 if (isec->reloc_count != 0)
13059 {
13060 Elf_Internal_Rela *relstart, *rel;
13061 Elf_Internal_Sym *local_syms;
13062 struct ppc_link_hash_table *htab;
13063
13064 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13065 info->keep_memory);
13066 if (relstart == NULL)
13067 return -1;
13068
13069 /* Look for branches to outside of this section. */
13070 local_syms = NULL;
13071 htab = ppc_hash_table (info);
13072 if (htab == NULL)
13073 return -1;
13074
13075 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
13076 {
13077 enum elf_ppc64_reloc_type r_type;
13078 unsigned long r_symndx;
13079 struct elf_link_hash_entry *h;
13080 struct ppc_link_hash_entry *eh;
13081 Elf_Internal_Sym *sym;
13082 asection *sym_sec;
13083 struct _opd_sec_data *opd;
13084 bfd_vma sym_value;
13085 bfd_vma dest;
13086
13087 r_type = ELF64_R_TYPE (rel->r_info);
13088 if (r_type != R_PPC64_REL24
13089 && r_type != R_PPC64_REL24_NOTOC
13090 && r_type != R_PPC64_REL24_P9NOTOC
13091 && r_type != R_PPC64_REL14
13092 && r_type != R_PPC64_REL14_BRTAKEN
13093 && r_type != R_PPC64_REL14_BRNTAKEN
13094 && r_type != R_PPC64_PLTCALL
13095 && r_type != R_PPC64_PLTCALL_NOTOC)
13096 continue;
13097
13098 r_symndx = ELF64_R_SYM (rel->r_info);
13099 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13100 isec->owner))
13101 {
13102 ret = -1;
13103 break;
13104 }
13105
13106 /* Calls to dynamic lib functions go through a plt call stub
13107 that uses r2. */
13108 eh = ppc_elf_hash_entry (h);
13109 if (eh != NULL
13110 && (eh->elf.plt.plist != NULL
13111 || (eh->oh != NULL
13112 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13113 {
13114 ret = 1;
13115 break;
13116 }
13117
13118 if (sym_sec == NULL)
13119 /* Ignore other undefined symbols. */
13120 continue;
13121
13122 /* Assume branches to other sections not included in the
13123 link need stubs too, to cover -R and absolute syms. */
13124 if (sym_sec->output_section == NULL)
13125 {
13126 ret = 1;
13127 break;
13128 }
13129
13130 if (h == NULL)
13131 sym_value = sym->st_value;
13132 else
13133 {
13134 if (h->root.type != bfd_link_hash_defined
13135 && h->root.type != bfd_link_hash_defweak)
13136 abort ();
13137 sym_value = h->root.u.def.value;
13138 }
13139 sym_value += rel->r_addend;
13140
13141 /* If this branch reloc uses an opd sym, find the code section. */
13142 opd = get_opd_info (sym_sec);
13143 if (opd != NULL)
13144 {
13145 if (h == NULL && opd->adjust != NULL)
13146 {
13147 long adjust;
13148
13149 adjust = opd->adjust[OPD_NDX (sym_value)];
13150 if (adjust == -1)
13151 /* Assume deleted functions won't ever be called. */
13152 continue;
13153 sym_value += adjust;
13154 }
13155
13156 dest = opd_entry_value (sym_sec, sym_value,
13157 &sym_sec, NULL, false);
13158 if (dest == (bfd_vma) -1)
13159 continue;
13160 }
13161 else
13162 dest = (sym_value
13163 + sym_sec->output_offset
13164 + sym_sec->output_section->vma);
13165
13166 /* Ignore branch to self. */
13167 if (sym_sec == isec)
13168 continue;
13169
13170 /* If the called function uses the toc, we need a stub. */
13171 if (sym_sec->has_toc_reloc
13172 || sym_sec->makes_toc_func_call)
13173 {
13174 ret = 1;
13175 break;
13176 }
13177
13178 /* Assume any branch that needs a long branch stub might in fact
13179 need a plt_branch stub. A plt_branch stub uses r2. */
13180 else if (dest - (isec->output_offset
13181 + isec->output_section->vma
13182 + rel->r_offset) + (1 << 25)
13183 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13184 ? h->other
13185 : sym->st_other))
13186 {
13187 ret = 1;
13188 break;
13189 }
13190
13191 /* If calling back to a section in the process of being
13192 tested, we can't say for sure that no toc adjusting stubs
13193 are needed, so don't return zero. */
13194 else if (sym_sec->call_check_in_progress)
13195 ret = 2;
13196
13197 /* Branches to another section that itself doesn't have any TOC
13198 references are OK. Recursively call ourselves to check. */
13199 else if (!sym_sec->call_check_done)
13200 {
13201 int recur;
13202
13203 /* Mark current section as indeterminate, so that other
13204 sections that call back to current won't be marked as
13205 known. */
13206 isec->call_check_in_progress = 1;
13207 recur = toc_adjusting_stub_needed (info, sym_sec);
13208 isec->call_check_in_progress = 0;
13209
13210 if (recur != 0)
13211 {
13212 ret = recur;
13213 if (recur != 2)
13214 break;
13215 }
13216 }
13217 }
13218
13219 if (elf_symtab_hdr (isec->owner).contents
13220 != (unsigned char *) local_syms)
13221 free (local_syms);
13222 if (elf_section_data (isec)->relocs != relstart)
13223 free (relstart);
13224 }
13225
13226 if ((ret & 1) == 0
13227 && isec->map_head.s != NULL
13228 && (strcmp (isec->output_section->name, ".init") == 0
13229 || strcmp (isec->output_section->name, ".fini") == 0))
13230 {
13231 if (isec->map_head.s->has_toc_reloc
13232 || isec->map_head.s->makes_toc_func_call)
13233 ret = 1;
13234 else if (!isec->map_head.s->call_check_done)
13235 {
13236 int recur;
13237 isec->call_check_in_progress = 1;
13238 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13239 isec->call_check_in_progress = 0;
13240 if (recur != 0)
13241 ret = recur;
13242 }
13243 }
13244
13245 if (ret == 1)
13246 isec->makes_toc_func_call = 1;
13247
13248 return ret;
13249 }
13250
13251 /* The linker repeatedly calls this function for each input section,
13252 in the order that input sections are linked into output sections.
13253 Build lists of input sections to determine groupings between which
13254 we may insert linker stubs. */
13255
13256 bool
13257 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13258 {
13259 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13260
13261 if (htab == NULL)
13262 return false;
13263
13264 if ((isec->output_section->flags & SEC_CODE) != 0
13265 && isec->output_section->id < htab->sec_info_arr_size)
13266 {
13267 /* This happens to make the list in reverse order,
13268 which is what we want. */
13269 htab->sec_info[isec->id].u.list
13270 = htab->sec_info[isec->output_section->id].u.list;
13271 htab->sec_info[isec->output_section->id].u.list = isec;
13272 }
13273
13274 if (htab->multi_toc_needed)
13275 {
13276 /* Analyse sections that aren't already flagged as needing a
13277 valid toc pointer. Exclude .fixup for the linux kernel.
13278 .fixup contains branches, but only back to the function that
13279 hit an exception. */
13280 if (!(isec->has_toc_reloc
13281 || (isec->flags & SEC_CODE) == 0
13282 || strcmp (isec->name, ".fixup") == 0
13283 || isec->call_check_done))
13284 {
13285 if (toc_adjusting_stub_needed (info, isec) < 0)
13286 return false;
13287 }
13288 /* Make all sections use the TOC assigned for this object file.
13289 This will be wrong for pasted sections; We fix that in
13290 check_pasted_section(). */
13291 if (elf_gp (isec->owner) != 0)
13292 htab->toc_curr = elf_gp (isec->owner);
13293 }
13294
13295 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13296 return true;
13297 }
13298
13299 /* Check that all .init and .fini sections use the same toc, if they
13300 have toc relocs. */
13301
13302 static bool
13303 check_pasted_section (struct bfd_link_info *info, const char *name)
13304 {
13305 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13306
13307 if (o != NULL)
13308 {
13309 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13310 bfd_vma toc_off = 0;
13311 asection *i;
13312
13313 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13314 if (i->has_toc_reloc)
13315 {
13316 if (toc_off == 0)
13317 toc_off = htab->sec_info[i->id].toc_off;
13318 else if (toc_off != htab->sec_info[i->id].toc_off)
13319 return false;
13320 }
13321
13322 if (toc_off == 0)
13323 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13324 if (i->makes_toc_func_call)
13325 {
13326 toc_off = htab->sec_info[i->id].toc_off;
13327 break;
13328 }
13329
13330 /* Make sure the whole pasted function uses the same toc offset. */
13331 if (toc_off != 0)
13332 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13333 htab->sec_info[i->id].toc_off = toc_off;
13334 }
13335 return true;
13336 }
13337
13338 bool
13339 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13340 {
13341 bool ret1 = check_pasted_section (info, ".init");
13342 bool ret2 = check_pasted_section (info, ".fini");
13343
13344 return ret1 && ret2;
13345 }
13346
13347 /* See whether we can group stub sections together. Grouping stub
13348 sections may result in fewer stubs. More importantly, we need to
13349 put all .init* and .fini* stubs at the beginning of the .init or
13350 .fini output sections respectively, because glibc splits the
13351 _init and _fini functions into multiple parts. Putting a stub in
13352 the middle of a function is not a good idea. */
13353
13354 static bool
13355 group_sections (struct bfd_link_info *info,
13356 bfd_size_type stub_group_size,
13357 bool stubs_always_before_branch)
13358 {
13359 struct ppc_link_hash_table *htab;
13360 asection *osec;
13361 bool suppress_size_errors;
13362
13363 htab = ppc_hash_table (info);
13364 if (htab == NULL)
13365 return false;
13366
13367 suppress_size_errors = false;
13368 if (stub_group_size == 1)
13369 {
13370 /* Default values. */
13371 if (stubs_always_before_branch)
13372 stub_group_size = 0x1e00000;
13373 else
13374 stub_group_size = 0x1c00000;
13375 suppress_size_errors = true;
13376 }
13377
13378 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13379 {
13380 asection *tail;
13381
13382 if (osec->id >= htab->sec_info_arr_size)
13383 continue;
13384
13385 tail = htab->sec_info[osec->id].u.list;
13386 while (tail != NULL)
13387 {
13388 asection *curr;
13389 asection *prev;
13390 bfd_size_type total;
13391 bool big_sec;
13392 bfd_vma curr_toc;
13393 struct map_stub *group;
13394 bfd_size_type group_size;
13395
13396 curr = tail;
13397 total = tail->size;
13398 group_size = (ppc64_elf_section_data (tail) != NULL
13399 && ppc64_elf_section_data (tail)->has_14bit_branch
13400 ? stub_group_size >> 10 : stub_group_size);
13401
13402 big_sec = total > group_size;
13403 if (big_sec && !suppress_size_errors)
13404 /* xgettext:c-format */
13405 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13406 tail->owner, tail);
13407 curr_toc = htab->sec_info[tail->id].toc_off;
13408
13409 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13410 && ((total += curr->output_offset - prev->output_offset)
13411 < (ppc64_elf_section_data (prev) != NULL
13412 && ppc64_elf_section_data (prev)->has_14bit_branch
13413 ? (group_size = stub_group_size >> 10) : group_size))
13414 && htab->sec_info[prev->id].toc_off == curr_toc)
13415 curr = prev;
13416
13417 /* OK, the size from the start of CURR to the end is less
13418 than group_size and thus can be handled by one stub
13419 section. (or the tail section is itself larger than
13420 group_size, in which case we may be toast.) We should
13421 really be keeping track of the total size of stubs added
13422 here, as stubs contribute to the final output section
13423 size. That's a little tricky, and this way will only
13424 break if stubs added make the total size more than 2^25,
13425 ie. for the default stub_group_size, if stubs total more
13426 than 2097152 bytes, or nearly 75000 plt call stubs. */
13427 group = bfd_alloc (curr->owner, sizeof (*group));
13428 if (group == NULL)
13429 return false;
13430 group->link_sec = curr;
13431 group->stub_sec = NULL;
13432 group->needs_save_res = 0;
13433 group->lr_restore = 0;
13434 group->eh_size = 0;
13435 group->eh_base = 0;
13436 group->next = htab->group;
13437 htab->group = group;
13438 do
13439 {
13440 prev = htab->sec_info[tail->id].u.list;
13441 /* Set up this stub group. */
13442 htab->sec_info[tail->id].u.group = group;
13443 }
13444 while (tail != curr && (tail = prev) != NULL);
13445
13446 /* But wait, there's more! Input sections up to group_size
13447 bytes before the stub section can be handled by it too.
13448 Don't do this if we have a really large section after the
13449 stubs, as adding more stubs increases the chance that
13450 branches may not reach into the stub section. */
13451 if (!stubs_always_before_branch && !big_sec)
13452 {
13453 total = 0;
13454 while (prev != NULL
13455 && ((total += tail->output_offset - prev->output_offset)
13456 < (ppc64_elf_section_data (prev) != NULL
13457 && ppc64_elf_section_data (prev)->has_14bit_branch
13458 ? (group_size = stub_group_size >> 10)
13459 : group_size))
13460 && htab->sec_info[prev->id].toc_off == curr_toc)
13461 {
13462 tail = prev;
13463 prev = htab->sec_info[tail->id].u.list;
13464 htab->sec_info[tail->id].u.group = group;
13465 }
13466 }
13467 tail = prev;
13468 }
13469 }
13470 return true;
13471 }
13472
13473 static const unsigned char glink_eh_frame_cie[] =
13474 {
13475 0, 0, 0, 16, /* length. */
13476 0, 0, 0, 0, /* id. */
13477 1, /* CIE version. */
13478 'z', 'R', 0, /* Augmentation string. */
13479 4, /* Code alignment. */
13480 0x78, /* Data alignment. */
13481 65, /* RA reg. */
13482 1, /* Augmentation size. */
13483 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13484 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13485 };
13486
13487 /* Stripping output sections is normally done before dynamic section
13488 symbols have been allocated. This function is called later, and
13489 handles cases like htab->brlt which is mapped to its own output
13490 section. */
13491
13492 static void
13493 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13494 {
13495 if (isec->size == 0
13496 && isec->output_section->size == 0
13497 && !(isec->output_section->flags & SEC_KEEP)
13498 && !bfd_section_removed_from_list (info->output_bfd,
13499 isec->output_section)
13500 && elf_section_data (isec->output_section)->dynindx == 0)
13501 {
13502 isec->output_section->flags |= SEC_EXCLUDE;
13503 bfd_section_list_remove (info->output_bfd, isec->output_section);
13504 info->output_bfd->section_count--;
13505 }
13506 }
13507
13508 /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13509 the array of such relocs. */
13510
13511 static bool
13512 append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13513 {
13514 if (htab->relr_count >= htab->relr_alloc)
13515 {
13516 if (htab->relr_alloc == 0)
13517 htab->relr_alloc = 4096;
13518 else
13519 htab->relr_alloc *= 2;
13520 htab->relr = bfd_realloc (htab->relr,
13521 htab->relr_alloc * sizeof (*htab->relr));
13522 if (htab->relr == NULL)
13523 return false;
13524 }
13525 htab->relr[htab->relr_count].sec = sec;
13526 htab->relr[htab->relr_count].off = off;
13527 htab->relr_count++;
13528 return true;
13529 }
13530
13531 /* qsort comparator for bfd_vma args. */
13532
13533 static int
13534 compare_relr_address (const void *arg1, const void *arg2)
13535 {
13536 bfd_vma a = *(bfd_vma *) arg1;
13537 bfd_vma b = *(bfd_vma *) arg2;
13538 return a < b ? -1 : a > b ? 1 : 0;
13539 }
13540
13541 /* Produce a malloc'd sorted array of reloc addresses from the info
13542 stored by append_relr_off. */
13543
13544 static bfd_vma *
13545 sort_relr (struct ppc_link_hash_table *htab)
13546 {
13547 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13548 if (addr == NULL)
13549 return NULL;
13550
13551 for (size_t i = 0; i < htab->relr_count; i++)
13552 addr[i] = (htab->relr[i].sec->output_section->vma
13553 + htab->relr[i].sec->output_offset
13554 + htab->relr[i].off);
13555
13556 if (htab->relr_count > 1)
13557 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13558
13559 return addr;
13560 }
13561
13562 /* Look over GOT and PLT entries saved on elf_local_got_ents for all
13563 input files, stashing info about needed relative relocs. */
13564
13565 static bool
13566 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13567 {
13568 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13569 bfd *ibfd;
13570
13571 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13572 {
13573 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13574 struct plt_entry **local_plt, **lplt, **end_local_plt;
13575 Elf_Internal_Shdr *symtab_hdr;
13576 bfd_size_type locsymcount;
13577 Elf_Internal_Sym *local_syms;
13578 Elf_Internal_Sym *isym;
13579 struct plt_entry *pent;
13580 struct got_entry *gent;
13581
13582 if (!is_ppc64_elf (ibfd))
13583 continue;
13584
13585 lgot_ents = elf_local_got_ents (ibfd);
13586 if (!lgot_ents)
13587 continue;
13588
13589 symtab_hdr = &elf_symtab_hdr (ibfd);
13590 locsymcount = symtab_hdr->sh_info;
13591 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13592 if (local_syms == NULL && locsymcount != 0)
13593 {
13594 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13595 0, NULL, NULL, NULL);
13596 if (local_syms == NULL)
13597 return false;
13598 }
13599 end_lgot_ents = lgot_ents + locsymcount;
13600 local_plt = (struct plt_entry **) end_lgot_ents;
13601 end_local_plt = local_plt + locsymcount;
13602 for (lgot = lgot_ents, isym = local_syms;
13603 lgot < end_lgot_ents;
13604 ++lgot, ++isym)
13605 for (gent = *lgot; gent != NULL; gent = gent->next)
13606 if (!gent->is_indirect
13607 && gent->tls_type == 0
13608 && gent->got.offset != (bfd_vma) -1
13609 && isym->st_shndx != SHN_ABS)
13610 {
13611 asection *got = ppc64_elf_tdata (gent->owner)->got;
13612 if (!append_relr_off (htab, got, gent->got.offset))
13613 {
13614 htab->stub_error = true;
13615 return false;
13616 }
13617 }
13618
13619 if (!htab->opd_abi)
13620 for (lplt = local_plt, isym = local_syms;
13621 lplt < end_local_plt;
13622 ++lplt, ++isym)
13623 for (pent = *lplt; pent != NULL; pent = pent->next)
13624 if (pent->plt.offset != (bfd_vma) -1
13625 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13626 {
13627 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13628 {
13629 if (symtab_hdr->contents != (unsigned char *) local_syms)
13630 free (local_syms);
13631 return false;
13632 }
13633 }
13634
13635 if (local_syms != NULL
13636 && symtab_hdr->contents != (unsigned char *) local_syms)
13637 {
13638 if (!info->keep_memory)
13639 free (local_syms);
13640 else
13641 symtab_hdr->contents = (unsigned char *) local_syms;
13642 }
13643 }
13644 return true;
13645 }
13646
13647 /* Stash info about needed GOT and PLT entry relative relocs for
13648 global symbol H. */
13649
13650 static bool
13651 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13652 {
13653 struct bfd_link_info *info;
13654 struct ppc_link_hash_table *htab;
13655 struct plt_entry *pent;
13656 struct got_entry *gent;
13657
13658 if (h->root.type == bfd_link_hash_indirect)
13659 return true;
13660
13661 info = (struct bfd_link_info *) inf;
13662 htab = ppc_hash_table (info);
13663 if (htab == NULL)
13664 return false;
13665
13666 if (h->type != STT_GNU_IFUNC
13667 && h->def_regular
13668 && (h->root.type == bfd_link_hash_defined
13669 || h->root.type == bfd_link_hash_defweak))
13670 {
13671 if ((!htab->elf.dynamic_sections_created
13672 || h->dynindx == -1
13673 || SYMBOL_REFERENCES_LOCAL (info, h))
13674 && !bfd_is_abs_symbol (&h->root))
13675 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13676 if (!gent->is_indirect
13677 && gent->tls_type == 0
13678 && gent->got.offset != (bfd_vma) -1)
13679 {
13680 asection *got = ppc64_elf_tdata (gent->owner)->got;
13681 if (!append_relr_off (htab, got, gent->got.offset))
13682 {
13683 htab->stub_error = true;
13684 return false;
13685 }
13686 }
13687
13688 if (!htab->opd_abi
13689 && use_local_plt (info, h))
13690 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13691 if (pent->plt.offset != (bfd_vma) -1)
13692 {
13693 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13694 {
13695 htab->stub_error = true;
13696 return false;
13697 }
13698 }
13699 }
13700 return true;
13701 }
13702
13703 /* Determine and set the size of the stub section for a final link.
13704
13705 The basic idea here is to examine all the relocations looking for
13706 PC-relative calls to a target that is unreachable with a "bl"
13707 instruction. */
13708
13709 bool
13710 ppc64_elf_size_stubs (struct bfd_link_info *info)
13711 {
13712 bfd_size_type stub_group_size;
13713 bool stubs_always_before_branch;
13714 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13715
13716 if (htab == NULL)
13717 return false;
13718
13719 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13720 htab->params->power10_stubs = 0;
13721
13722 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13723 htab->params->plt_thread_safe = 1;
13724 if (!htab->opd_abi)
13725 htab->params->plt_thread_safe = 0;
13726 else if (htab->params->plt_thread_safe == -1)
13727 {
13728 static const char *const thread_starter[] =
13729 {
13730 "pthread_create",
13731 /* libstdc++ */
13732 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13733 /* librt */
13734 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13735 "mq_notify", "create_timer",
13736 /* libanl */
13737 "getaddrinfo_a",
13738 /* libgomp */
13739 "GOMP_parallel",
13740 "GOMP_parallel_start",
13741 "GOMP_parallel_loop_static",
13742 "GOMP_parallel_loop_static_start",
13743 "GOMP_parallel_loop_dynamic",
13744 "GOMP_parallel_loop_dynamic_start",
13745 "GOMP_parallel_loop_guided",
13746 "GOMP_parallel_loop_guided_start",
13747 "GOMP_parallel_loop_runtime",
13748 "GOMP_parallel_loop_runtime_start",
13749 "GOMP_parallel_sections",
13750 "GOMP_parallel_sections_start",
13751 /* libgo */
13752 "__go_go",
13753 };
13754 unsigned i;
13755
13756 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13757 {
13758 struct elf_link_hash_entry *h;
13759 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13760 false, false, true);
13761 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13762 if (htab->params->plt_thread_safe)
13763 break;
13764 }
13765 }
13766 stubs_always_before_branch = htab->params->group_size < 0;
13767 if (htab->params->group_size < 0)
13768 stub_group_size = -htab->params->group_size;
13769 else
13770 stub_group_size = htab->params->group_size;
13771
13772 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13773 return false;
13774
13775 htab->tga_group = NULL;
13776 if (!htab->params->no_tls_get_addr_regsave
13777 && htab->tga_desc_fd != NULL
13778 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13779 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13780 && htab->tls_get_addr_fd != NULL
13781 && is_static_defined (&htab->tls_get_addr_fd->elf))
13782 {
13783 asection *sym_sec, *code_sec, *stub_sec;
13784 bfd_vma sym_value;
13785 struct _opd_sec_data *opd;
13786
13787 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13788 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13789 code_sec = sym_sec;
13790 opd = get_opd_info (sym_sec);
13791 if (opd != NULL)
13792 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13793 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13794 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13795 htab->tga_group->link_sec);
13796 if (stub_sec == NULL)
13797 return false;
13798 htab->tga_group->stub_sec = stub_sec;
13799
13800 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13801 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13802 htab->tga_desc_fd->elf.root.u.def.value = 0;
13803 htab->tga_desc_fd->elf.type = STT_FUNC;
13804 htab->tga_desc_fd->elf.def_regular = 1;
13805 htab->tga_desc_fd->elf.non_elf = 0;
13806 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13807 }
13808
13809 /* Loop until no stubs added. After iteration 20 of this loop we may
13810 exit on a stub section shrinking. */
13811
13812 while (1)
13813 {
13814 bfd *input_bfd;
13815 unsigned int bfd_indx;
13816 struct map_stub *group;
13817
13818 htab->stub_iteration += 1;
13819 htab->relr_count = 0;
13820
13821 for (input_bfd = info->input_bfds, bfd_indx = 0;
13822 input_bfd != NULL;
13823 input_bfd = input_bfd->link.next, bfd_indx++)
13824 {
13825 Elf_Internal_Shdr *symtab_hdr;
13826 asection *section;
13827 Elf_Internal_Sym *local_syms = NULL;
13828
13829 if (!is_ppc64_elf (input_bfd))
13830 continue;
13831
13832 /* We'll need the symbol table in a second. */
13833 symtab_hdr = &elf_symtab_hdr (input_bfd);
13834 if (symtab_hdr->sh_info == 0)
13835 continue;
13836
13837 /* Walk over each section attached to the input bfd. */
13838 for (section = input_bfd->sections;
13839 section != NULL;
13840 section = section->next)
13841 {
13842 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13843 bool is_opd;
13844
13845 /* If there aren't any relocs, then there's nothing more
13846 to do. */
13847 if ((section->flags & SEC_RELOC) == 0
13848 || (section->flags & SEC_ALLOC) == 0
13849 || (section->flags & SEC_LOAD) == 0
13850 || section->reloc_count == 0)
13851 continue;
13852
13853 if (!info->enable_dt_relr
13854 && (section->flags & SEC_CODE) == 0)
13855 continue;
13856
13857 /* If this section is a link-once section that will be
13858 discarded, then don't create any stubs. */
13859 if (section->output_section == NULL
13860 || section->output_section->owner != info->output_bfd)
13861 continue;
13862
13863 /* Get the relocs. */
13864 internal_relocs
13865 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13866 info->keep_memory);
13867 if (internal_relocs == NULL)
13868 goto error_ret_free_local;
13869
13870 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13871
13872 /* Now examine each relocation. */
13873 irela = internal_relocs;
13874 irelaend = irela + section->reloc_count;
13875 for (; irela < irelaend; irela++)
13876 {
13877 enum elf_ppc64_reloc_type r_type;
13878 unsigned int r_indx;
13879 struct ppc_stub_type stub_type;
13880 struct ppc_stub_hash_entry *stub_entry;
13881 asection *sym_sec, *code_sec;
13882 bfd_vma sym_value, code_value;
13883 bfd_vma destination;
13884 unsigned long local_off;
13885 bool ok_dest;
13886 struct ppc_link_hash_entry *hash;
13887 struct ppc_link_hash_entry *fdh;
13888 struct elf_link_hash_entry *h;
13889 Elf_Internal_Sym *sym;
13890 char *stub_name;
13891 const asection *id_sec;
13892 struct _opd_sec_data *opd;
13893 struct plt_entry *plt_ent;
13894
13895 r_type = ELF64_R_TYPE (irela->r_info);
13896 r_indx = ELF64_R_SYM (irela->r_info);
13897
13898 if (r_type >= R_PPC64_max)
13899 {
13900 bfd_set_error (bfd_error_bad_value);
13901 goto error_ret_free_internal;
13902 }
13903
13904 /* Only look for stubs on branch instructions. */
13905 switch (r_type)
13906 {
13907 default:
13908 if (info->enable_dt_relr
13909 && maybe_relr (r_type, irela, section))
13910 break;
13911 continue;
13912
13913 case R_PPC64_REL24:
13914 case R_PPC64_REL24_NOTOC:
13915 case R_PPC64_REL24_P9NOTOC:
13916 case R_PPC64_REL14:
13917 case R_PPC64_REL14_BRTAKEN:
13918 case R_PPC64_REL14_BRNTAKEN:
13919 if ((section->flags & SEC_CODE) != 0)
13920 break;
13921 continue;
13922 }
13923
13924 /* Now determine the call target, its name, value,
13925 section. */
13926 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13927 r_indx, input_bfd))
13928 goto error_ret_free_internal;
13929
13930 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13931 {
13932 /* Only locally defined symbols can possibly use
13933 relative relocations. */
13934 bfd_vma r_offset;
13935 if ((sym_sec == NULL
13936 || sym_sec->output_section == NULL)
13937 /* No symbol is OK too. */
13938 && !(sym != NULL && sym->st_shndx == 0)
13939 /* Hack for __ehdr_start, which is undefined
13940 at this point. */
13941 && !(h != NULL && h->root.linker_def))
13942 continue;
13943 if (NO_OPD_RELOCS && is_opd)
13944 continue;
13945 if (!is_opd
13946 && r_type == R_PPC64_ADDR64)
13947 {
13948 if (h != NULL
13949 ? h->type == STT_GNU_IFUNC
13950 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13951 continue;
13952 if (h != NULL
13953 ? bfd_is_abs_symbol (&h->root)
13954 : sym->st_shndx == SHN_ABS)
13955 continue;
13956 if (h != NULL
13957 && !SYMBOL_REFERENCES_LOCAL (info, h))
13958 continue;
13959 }
13960 r_offset = _bfd_elf_section_offset (info->output_bfd,
13961 info,
13962 section,
13963 irela->r_offset);
13964 if (r_offset >= (bfd_vma) -2)
13965 continue;
13966 if (!append_relr_off (htab, section, r_offset))
13967 goto error_ret_free_internal;
13968 continue;
13969 }
13970
13971 hash = ppc_elf_hash_entry (h);
13972 ok_dest = false;
13973 fdh = NULL;
13974 sym_value = 0;
13975 if (hash == NULL)
13976 {
13977 sym_value = sym->st_value;
13978 if (sym_sec != NULL
13979 && sym_sec->output_section != NULL)
13980 ok_dest = true;
13981 }
13982 else if (hash->elf.root.type == bfd_link_hash_defined
13983 || hash->elf.root.type == bfd_link_hash_defweak)
13984 {
13985 sym_value = hash->elf.root.u.def.value;
13986 if (sym_sec->output_section != NULL)
13987 ok_dest = true;
13988 }
13989 else if (hash->elf.root.type == bfd_link_hash_undefweak
13990 || hash->elf.root.type == bfd_link_hash_undefined)
13991 {
13992 /* Recognise an old ABI func code entry sym, and
13993 use the func descriptor sym instead if it is
13994 defined. */
13995 if (hash->elf.root.root.string[0] == '.'
13996 && hash->oh != NULL)
13997 {
13998 fdh = ppc_follow_link (hash->oh);
13999 if (fdh->elf.root.type == bfd_link_hash_defined
14000 || fdh->elf.root.type == bfd_link_hash_defweak)
14001 {
14002 sym_sec = fdh->elf.root.u.def.section;
14003 sym_value = fdh->elf.root.u.def.value;
14004 if (sym_sec->output_section != NULL)
14005 ok_dest = true;
14006 }
14007 else
14008 fdh = NULL;
14009 }
14010 }
14011 else
14012 {
14013 bfd_set_error (bfd_error_bad_value);
14014 goto error_ret_free_internal;
14015 }
14016
14017 destination = 0;
14018 local_off = 0;
14019 if (ok_dest)
14020 {
14021 sym_value += irela->r_addend;
14022 destination = (sym_value
14023 + sym_sec->output_offset
14024 + sym_sec->output_section->vma);
14025 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
14026 ? hash->elf.other
14027 : sym->st_other);
14028 }
14029
14030 code_sec = sym_sec;
14031 code_value = sym_value;
14032 opd = get_opd_info (sym_sec);
14033 if (opd != NULL)
14034 {
14035 bfd_vma dest;
14036
14037 if (hash == NULL && opd->adjust != NULL)
14038 {
14039 long adjust = opd->adjust[OPD_NDX (sym_value)];
14040 if (adjust == -1)
14041 continue;
14042 code_value += adjust;
14043 sym_value += adjust;
14044 }
14045 dest = opd_entry_value (sym_sec, sym_value,
14046 &code_sec, &code_value, false);
14047 if (dest != (bfd_vma) -1)
14048 {
14049 destination = dest;
14050 if (fdh != NULL)
14051 {
14052 /* Fixup old ABI sym to point at code
14053 entry. */
14054 hash->elf.root.type = bfd_link_hash_defweak;
14055 hash->elf.root.u.def.section = code_sec;
14056 hash->elf.root.u.def.value = code_value;
14057 }
14058 }
14059 }
14060
14061 /* Determine what (if any) linker stub is needed. */
14062 plt_ent = NULL;
14063 stub_type.main = ppc_type_of_stub (section, irela, &hash,
14064 &plt_ent, destination,
14065 local_off);
14066 stub_type.sub = ppc_stub_toc;
14067 stub_type.r2save = 0;
14068
14069 if (r_type == R_PPC64_REL24_NOTOC
14070 || r_type == R_PPC64_REL24_P9NOTOC)
14071 {
14072 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14073 if (htab->params->power10_stubs == 0
14074 || (r_type == R_PPC64_REL24_P9NOTOC
14075 && htab->params->power10_stubs != 1))
14076 notoc = ppc_stub_p9notoc;
14077 if (stub_type.main == ppc_stub_plt_call)
14078 stub_type.sub = notoc;
14079 else if (stub_type.main == ppc_stub_long_branch
14080 || (code_sec != NULL
14081 && code_sec->output_section != NULL
14082 && (((hash ? hash->elf.other : sym->st_other)
14083 & STO_PPC64_LOCAL_MASK)
14084 > 1 << STO_PPC64_LOCAL_BIT)))
14085 {
14086 stub_type.main = ppc_stub_long_branch;
14087 stub_type.sub = notoc;
14088 stub_type.r2save = 0;
14089 }
14090 }
14091 else if (stub_type.main != ppc_stub_plt_call)
14092 {
14093 /* Check whether we need a TOC adjusting stub.
14094 Since the linker pastes together pieces from
14095 different object files when creating the
14096 _init and _fini functions, it may be that a
14097 call to what looks like a local sym is in
14098 fact a call needing a TOC adjustment. */
14099 if ((code_sec != NULL
14100 && code_sec->output_section != NULL
14101 && (code_sec->has_toc_reloc
14102 || code_sec->makes_toc_func_call)
14103 && (htab->sec_info[code_sec->id].toc_off
14104 != htab->sec_info[section->id].toc_off))
14105 || (((hash ? hash->elf.other : sym->st_other)
14106 & STO_PPC64_LOCAL_MASK)
14107 == 1 << STO_PPC64_LOCAL_BIT))
14108 {
14109 stub_type.main = ppc_stub_long_branch;
14110 stub_type.sub = ppc_stub_toc;
14111 stub_type.r2save = 1;
14112 }
14113 }
14114
14115 if (stub_type.main == ppc_stub_none)
14116 continue;
14117
14118 /* __tls_get_addr calls might be eliminated. */
14119 if (stub_type.main != ppc_stub_plt_call
14120 && hash != NULL
14121 && is_tls_get_addr (&hash->elf, htab)
14122 && section->has_tls_reloc
14123 && irela != internal_relocs)
14124 {
14125 /* Get tls info. */
14126 unsigned char *tls_mask;
14127
14128 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14129 irela - 1, input_bfd))
14130 goto error_ret_free_internal;
14131 if ((*tls_mask & TLS_TLS) != 0
14132 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14133 continue;
14134 }
14135
14136 if (stub_type.main == ppc_stub_plt_call
14137 && stub_type.sub == ppc_stub_toc)
14138 {
14139 if (!htab->opd_abi
14140 && htab->params->plt_localentry0 != 0
14141 && is_elfv2_localentry0 (&hash->elf))
14142 htab->has_plt_localentry0 = 1;
14143 else if (irela + 1 < irelaend
14144 && irela[1].r_offset == irela->r_offset + 4
14145 && (ELF64_R_TYPE (irela[1].r_info)
14146 == R_PPC64_TOCSAVE))
14147 {
14148 if (!tocsave_find (htab, INSERT,
14149 &local_syms, irela + 1, input_bfd))
14150 goto error_ret_free_internal;
14151 }
14152 else
14153 stub_type.r2save = 1;
14154 }
14155
14156 /* Support for grouping stub sections. */
14157 id_sec = htab->sec_info[section->id].u.group->link_sec;
14158
14159 /* Get the name of this stub. */
14160 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14161 if (!stub_name)
14162 goto error_ret_free_internal;
14163
14164 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14165 stub_name, false, false);
14166 if (stub_entry != NULL)
14167 {
14168 free (stub_name);
14169 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14170 {
14171 /* xgettext:c-format */
14172 _bfd_error_handler
14173 (_("%pB: cannot create stub entry %s"),
14174 section->owner, stub_entry->root.string);
14175 goto error_ret_free_internal;
14176 }
14177 continue;
14178 }
14179
14180 stub_entry = ppc_add_stub (stub_name, section, info);
14181 if (stub_entry == NULL)
14182 {
14183 free (stub_name);
14184 error_ret_free_internal:
14185 if (elf_section_data (section)->relocs == NULL)
14186 free (internal_relocs);
14187 error_ret_free_local:
14188 if (symtab_hdr->contents
14189 != (unsigned char *) local_syms)
14190 free (local_syms);
14191 return false;
14192 }
14193
14194 stub_entry->type = stub_type;
14195 if (stub_type.main == ppc_stub_plt_call)
14196 {
14197 stub_entry->target_value = sym_value;
14198 stub_entry->target_section = sym_sec;
14199 }
14200 else
14201 {
14202 stub_entry->target_value = code_value;
14203 stub_entry->target_section = code_sec;
14204 }
14205 stub_entry->h = hash;
14206 stub_entry->plt_ent = plt_ent;
14207 stub_entry->symtype
14208 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14209 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14210
14211 if (hash != NULL
14212 && (hash->elf.root.type == bfd_link_hash_defined
14213 || hash->elf.root.type == bfd_link_hash_defweak))
14214 htab->stub_globals += 1;
14215 }
14216
14217 /* We're done with the internal relocs, free them. */
14218 if (elf_section_data (section)->relocs != internal_relocs)
14219 free (internal_relocs);
14220 }
14221
14222 if (local_syms != NULL
14223 && symtab_hdr->contents != (unsigned char *) local_syms)
14224 {
14225 if (!info->keep_memory)
14226 free (local_syms);
14227 else
14228 symtab_hdr->contents = (unsigned char *) local_syms;
14229 }
14230 }
14231
14232 /* We may have added some stubs. Find out the new size of the
14233 stub sections. */
14234 for (group = htab->group; group != NULL; group = group->next)
14235 {
14236 group->lr_restore = 0;
14237 group->eh_size = 0;
14238 if (group->stub_sec != NULL)
14239 {
14240 asection *stub_sec = group->stub_sec;
14241
14242 stub_sec->rawsize = stub_sec->size;
14243 stub_sec->size = 0;
14244 stub_sec->reloc_count = 0;
14245 stub_sec->flags &= ~SEC_RELOC;
14246 }
14247 }
14248 if (htab->tga_group != NULL)
14249 {
14250 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14251 htab->tga_group->eh_size
14252 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14253 htab->tga_group->lr_restore = 23 * 4;
14254 htab->tga_group->stub_sec->size = 24 * 4;
14255 }
14256
14257 htab->brlt->rawsize = htab->brlt->size;
14258 htab->brlt->size = 0;
14259 htab->brlt->reloc_count = 0;
14260 htab->brlt->flags &= ~SEC_RELOC;
14261 if (htab->relbrlt != NULL)
14262 htab->relbrlt->size = 0;
14263
14264 if (htab->elf.srelrdyn != NULL)
14265 {
14266 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14267 htab->elf.srelrdyn->size = 0;
14268 }
14269
14270 htab->stub_changed = false;
14271 htab->stub_id = 0;
14272 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14273
14274 for (group = htab->group; group != NULL; group = group->next)
14275 if (group->needs_save_res)
14276 group->stub_sec->size += htab->sfpr->size;
14277
14278 if (info->emitrelocations
14279 && htab->glink != NULL && htab->glink->size != 0)
14280 {
14281 htab->glink->reloc_count = 1;
14282 htab->glink->flags |= SEC_RELOC;
14283 }
14284
14285 if (htab->glink_eh_frame != NULL
14286 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14287 && htab->glink_eh_frame->output_section->size > 8)
14288 {
14289 size_t size = 0, align = 4;
14290
14291 for (group = htab->group; group != NULL; group = group->next)
14292 if (group->eh_size != 0)
14293 size += (group->eh_size + 17 + align - 1) & -align;
14294 if (htab->glink != NULL && htab->glink->size != 0)
14295 size += (24 + align - 1) & -align;
14296 if (size != 0)
14297 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14298 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14299 size = (size + align - 1) & -align;
14300 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14301 htab->glink_eh_frame->size = size;
14302 }
14303
14304 if (htab->params->plt_stub_align != 0)
14305 for (group = htab->group; group != NULL; group = group->next)
14306 if (group->stub_sec != NULL)
14307 {
14308 int align = abs (htab->params->plt_stub_align);
14309 group->stub_sec->size
14310 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14311 }
14312
14313 if (htab->elf.srelrdyn != NULL)
14314 {
14315 bfd_vma r_offset;
14316
14317 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14318 if (!append_relr_off (htab, htab->brlt, r_offset))
14319 return false;
14320
14321 if (!got_and_plt_relr_for_local_syms (info))
14322 return false;
14323 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14324 if (htab->stub_error)
14325 return false;
14326
14327 bfd_vma *relr_addr = sort_relr (htab);
14328 if (htab->relr_count != 0 && relr_addr == NULL)
14329 return false;
14330
14331 size_t i = 0;
14332 while (i < htab->relr_count)
14333 {
14334 bfd_vma base = relr_addr[i];
14335 htab->elf.srelrdyn->size += 8;
14336 i++;
14337 /* Handle possible duplicate address. This can happen
14338 as sections increase in size when adding stubs. */
14339 while (i < htab->relr_count
14340 && relr_addr[i] == base)
14341 i++;
14342 base += 8;
14343 while (1)
14344 {
14345 size_t start_i = i;
14346 while (i < htab->relr_count
14347 && relr_addr[i] - base < 63 * 8
14348 && (relr_addr[i] - base) % 8 == 0)
14349 i++;
14350 if (i == start_i)
14351 break;
14352 htab->elf.srelrdyn->size += 8;
14353 base += 63 * 8;
14354 }
14355 }
14356 free (relr_addr);
14357 }
14358
14359 for (group = htab->group; group != NULL; group = group->next)
14360 if (group->stub_sec != NULL
14361 && group->stub_sec->rawsize != group->stub_sec->size
14362 && (htab->stub_iteration <= STUB_SHRINK_ITER
14363 || group->stub_sec->rawsize < group->stub_sec->size))
14364 break;
14365
14366 if (group == NULL
14367 && (!htab->stub_changed
14368 || htab->stub_iteration > STUB_SHRINK_ITER)
14369 && (htab->brlt->rawsize == htab->brlt->size
14370 || (htab->stub_iteration > STUB_SHRINK_ITER
14371 && htab->brlt->rawsize > htab->brlt->size))
14372 && (htab->elf.srelrdyn == NULL
14373 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14374 || (htab->stub_iteration > STUB_SHRINK_ITER
14375 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14376 && (htab->glink_eh_frame == NULL
14377 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14378 && (htab->tga_group == NULL
14379 || htab->stub_iteration > 1))
14380 break;
14381
14382 if (htab->stub_iteration > STUB_SHRINK_ITER)
14383 {
14384 for (group = htab->group; group != NULL; group = group->next)
14385 if (group->stub_sec != NULL
14386 && group->stub_sec->size < group->stub_sec->rawsize)
14387 group->stub_sec->size = group->stub_sec->rawsize;
14388
14389 if (htab->brlt->size < htab->brlt->rawsize)
14390 htab->brlt->size = htab->brlt->rawsize;
14391
14392 if (htab->elf.srelrdyn != NULL
14393 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14394 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14395 }
14396
14397 /* Ask the linker to do its stuff. */
14398 (*htab->params->layout_sections_again) ();
14399 }
14400
14401 if (htab->glink_eh_frame != NULL
14402 && htab->glink_eh_frame->size != 0)
14403 {
14404 bfd_vma val;
14405 bfd_byte *p, *last_fde;
14406 size_t last_fde_len, size, align, pad;
14407 struct map_stub *group;
14408
14409 /* It is necessary to at least have a rough outline of the
14410 linker generated CIEs and FDEs written before
14411 bfd_elf_discard_info is run, in order for these FDEs to be
14412 indexed in .eh_frame_hdr. */
14413 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14414 if (p == NULL)
14415 return false;
14416 htab->glink_eh_frame->contents = p;
14417 last_fde = p;
14418 align = 4;
14419
14420 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14421 /* CIE length (rewrite in case little-endian). */
14422 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14423 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14424 p += last_fde_len + 4;
14425
14426 for (group = htab->group; group != NULL; group = group->next)
14427 if (group->eh_size != 0)
14428 {
14429 group->eh_base = p - htab->glink_eh_frame->contents;
14430 last_fde = p;
14431 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14432 /* FDE length. */
14433 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14434 p += 4;
14435 /* CIE pointer. */
14436 val = p - htab->glink_eh_frame->contents;
14437 bfd_put_32 (htab->elf.dynobj, val, p);
14438 p += 4;
14439 /* Offset to stub section, written later. */
14440 p += 4;
14441 /* stub section size. */
14442 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14443 p += 4;
14444 /* Augmentation. */
14445 p += 1;
14446 /* Make sure we don't have all nops. This is enough for
14447 elf-eh-frame.c to detect the last non-nop opcode. */
14448 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14449 p = last_fde + last_fde_len + 4;
14450 }
14451 if (htab->glink != NULL && htab->glink->size != 0)
14452 {
14453 last_fde = p;
14454 last_fde_len = ((24 + align - 1) & -align) - 4;
14455 /* FDE length. */
14456 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14457 p += 4;
14458 /* CIE pointer. */
14459 val = p - htab->glink_eh_frame->contents;
14460 bfd_put_32 (htab->elf.dynobj, val, p);
14461 p += 4;
14462 /* Offset to .glink, written later. */
14463 p += 4;
14464 /* .glink size. */
14465 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14466 p += 4;
14467 /* Augmentation. */
14468 p += 1;
14469
14470 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14471 *p++ = DW_CFA_register;
14472 *p++ = 65;
14473 *p++ = htab->opd_abi ? 12 : 0;
14474 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14475 *p++ = DW_CFA_restore_extended;
14476 *p++ = 65;
14477 p += ((24 + align - 1) & -align) - 24;
14478 }
14479 /* Subsume any padding into the last FDE if user .eh_frame
14480 sections are aligned more than glink_eh_frame. Otherwise any
14481 zero padding will be seen as a terminator. */
14482 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14483 size = p - htab->glink_eh_frame->contents;
14484 pad = ((size + align - 1) & -align) - size;
14485 htab->glink_eh_frame->size = size + pad;
14486 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14487 }
14488
14489 maybe_strip_output (info, htab->brlt);
14490 if (htab->relbrlt != NULL)
14491 maybe_strip_output (info, htab->relbrlt);
14492 if (htab->glink_eh_frame != NULL)
14493 maybe_strip_output (info, htab->glink_eh_frame);
14494 if (htab->elf.srelrdyn != NULL)
14495 maybe_strip_output (info, htab->elf.srelrdyn);
14496
14497 return true;
14498 }
14499
14500 /* Called after we have determined section placement. If sections
14501 move, we'll be called again. Provide a value for TOCstart. */
14502
14503 bfd_vma
14504 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14505 {
14506 asection *s;
14507 bfd_vma TOCstart, adjust;
14508
14509 if (info != NULL)
14510 {
14511 struct elf_link_hash_entry *h;
14512 struct elf_link_hash_table *htab = elf_hash_table (info);
14513
14514 if (is_elf_hash_table (&htab->root)
14515 && htab->hgot != NULL)
14516 h = htab->hgot;
14517 else
14518 {
14519 h = (struct elf_link_hash_entry *)
14520 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14521 if (is_elf_hash_table (&htab->root))
14522 htab->hgot = h;
14523 }
14524 if (h != NULL
14525 && h->root.type == bfd_link_hash_defined
14526 && !h->root.linker_def
14527 && (!is_elf_hash_table (&htab->root)
14528 || h->def_regular))
14529 {
14530 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14531 _bfd_set_gp_value (obfd, TOCstart);
14532 return TOCstart;
14533 }
14534 }
14535
14536 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14537 order. The TOC starts where the first of these sections starts. */
14538 s = bfd_get_section_by_name (obfd, ".got");
14539 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14540 s = bfd_get_section_by_name (obfd, ".toc");
14541 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14542 s = bfd_get_section_by_name (obfd, ".tocbss");
14543 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14544 s = bfd_get_section_by_name (obfd, ".plt");
14545 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14546 {
14547 /* This may happen for
14548 o references to TOC base (SYM@toc / TOC[tc0]) without a
14549 .toc directive
14550 o bad linker script
14551 o --gc-sections and empty TOC sections
14552
14553 FIXME: Warn user? */
14554
14555 /* Look for a likely section. We probably won't even be
14556 using TOCstart. */
14557 for (s = obfd->sections; s != NULL; s = s->next)
14558 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14559 | SEC_EXCLUDE))
14560 == (SEC_ALLOC | SEC_SMALL_DATA))
14561 break;
14562 if (s == NULL)
14563 for (s = obfd->sections; s != NULL; s = s->next)
14564 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14565 == (SEC_ALLOC | SEC_SMALL_DATA))
14566 break;
14567 if (s == NULL)
14568 for (s = obfd->sections; s != NULL; s = s->next)
14569 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14570 == SEC_ALLOC)
14571 break;
14572 if (s == NULL)
14573 for (s = obfd->sections; s != NULL; s = s->next)
14574 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14575 break;
14576 }
14577
14578 TOCstart = 0;
14579 if (s != NULL)
14580 TOCstart = s->output_section->vma + s->output_offset;
14581
14582 /* Force alignment. */
14583 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14584 TOCstart -= adjust;
14585 _bfd_set_gp_value (obfd, TOCstart);
14586
14587 if (info != NULL && s != NULL)
14588 {
14589 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14590
14591 if (htab != NULL)
14592 {
14593 if (htab->elf.hgot != NULL)
14594 {
14595 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14596 htab->elf.hgot->root.u.def.section = s;
14597 }
14598 }
14599 else
14600 {
14601 struct bfd_link_hash_entry *bh = NULL;
14602 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14603 s, TOC_BASE_OFF - adjust,
14604 NULL, false, false, &bh);
14605 }
14606 }
14607 return TOCstart;
14608 }
14609
14610 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14611 write out any global entry stubs, and PLT relocations. */
14612
14613 static bool
14614 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14615 {
14616 struct bfd_link_info *info;
14617 struct ppc_link_hash_table *htab;
14618 struct plt_entry *ent;
14619 asection *s;
14620
14621 if (h->root.type == bfd_link_hash_indirect)
14622 return true;
14623
14624 info = inf;
14625 htab = ppc_hash_table (info);
14626 if (htab == NULL)
14627 return false;
14628
14629 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14630 if (ent->plt.offset != (bfd_vma) -1)
14631 {
14632 /* This symbol has an entry in the procedure linkage
14633 table. Set it up. */
14634 Elf_Internal_Rela rela;
14635 asection *plt, *relplt;
14636 bfd_byte *loc;
14637
14638 if (use_local_plt (info, h))
14639 {
14640 if (!(h->def_regular
14641 && (h->root.type == bfd_link_hash_defined
14642 || h->root.type == bfd_link_hash_defweak)))
14643 continue;
14644 if (h->type == STT_GNU_IFUNC)
14645 {
14646 plt = htab->elf.iplt;
14647 relplt = htab->elf.irelplt;
14648 htab->elf.ifunc_resolvers = true;
14649 if (htab->opd_abi)
14650 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14651 else
14652 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14653 }
14654 else
14655 {
14656 plt = htab->pltlocal;
14657 relplt = NULL;
14658 if (bfd_link_pic (info)
14659 && !(info->enable_dt_relr && !htab->opd_abi))
14660 {
14661 relplt = htab->relpltlocal;
14662 if (htab->opd_abi)
14663 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14664 else
14665 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14666 }
14667 }
14668 rela.r_addend = defined_sym_val (h) + ent->addend;
14669
14670 if (relplt == NULL)
14671 {
14672 loc = plt->contents + ent->plt.offset;
14673 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14674 if (htab->opd_abi)
14675 {
14676 bfd_vma toc = elf_gp (info->output_bfd);
14677 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14678 bfd_put_64 (info->output_bfd, toc, loc + 8);
14679 }
14680 }
14681 else
14682 {
14683 rela.r_offset = (plt->output_section->vma
14684 + plt->output_offset
14685 + ent->plt.offset);
14686 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
14687 relplt));
14688 }
14689 }
14690 else
14691 {
14692 rela.r_offset = (htab->elf.splt->output_section->vma
14693 + htab->elf.splt->output_offset
14694 + ent->plt.offset);
14695 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14696 rela.r_addend = ent->addend;
14697 loc = (htab->elf.srelplt->contents
14698 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14699 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14700 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14701 htab->elf.ifunc_resolvers = true;
14702 BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
14703 loc, htab->elf.srelplt));
14704 }
14705 }
14706
14707 if (!h->pointer_equality_needed)
14708 return true;
14709
14710 if (h->def_regular)
14711 return true;
14712
14713 s = htab->global_entry;
14714 if (s == NULL || s->size == 0)
14715 return true;
14716
14717 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14718 if (ent->plt.offset != (bfd_vma) -1
14719 && ent->addend == 0)
14720 {
14721 bfd_byte *p;
14722 asection *plt;
14723 bfd_vma off;
14724
14725 p = s->contents + h->root.u.def.value;
14726 plt = htab->elf.splt;
14727 if (use_local_plt (info, h))
14728 {
14729 if (h->type == STT_GNU_IFUNC)
14730 plt = htab->elf.iplt;
14731 else
14732 plt = htab->pltlocal;
14733 }
14734 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14735 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14736
14737 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14738 {
14739 info->callbacks->einfo
14740 (_("%P: linkage table error against `%pT'\n"),
14741 h->root.root.string);
14742 bfd_set_error (bfd_error_bad_value);
14743 htab->stub_error = true;
14744 }
14745
14746 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14747 if (htab->params->emit_stub_syms)
14748 {
14749 size_t len = strlen (h->root.root.string);
14750 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14751
14752 if (name == NULL)
14753 return false;
14754
14755 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14756 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14757 if (h == NULL)
14758 return false;
14759 if (h->root.type == bfd_link_hash_new)
14760 {
14761 h->root.type = bfd_link_hash_defined;
14762 h->root.u.def.section = s;
14763 h->root.u.def.value = p - s->contents;
14764 h->ref_regular = 1;
14765 h->def_regular = 1;
14766 h->ref_regular_nonweak = 1;
14767 h->forced_local = 1;
14768 h->non_elf = 0;
14769 h->root.linker_def = 1;
14770 }
14771 }
14772
14773 if (PPC_HA (off) != 0)
14774 {
14775 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14776 p += 4;
14777 }
14778 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14779 p += 4;
14780 bfd_put_32 (s->owner, MTCTR_R12, p);
14781 p += 4;
14782 bfd_put_32 (s->owner, BCTR, p);
14783 break;
14784 }
14785 return true;
14786 }
14787
14788 /* Write PLT relocs for locals. */
14789
14790 static bool
14791 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14792 {
14793 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14794 bfd *ibfd;
14795
14796 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14797 {
14798 struct got_entry **lgot_ents, **end_lgot_ents;
14799 struct plt_entry **local_plt, **lplt, **end_local_plt;
14800 Elf_Internal_Shdr *symtab_hdr;
14801 bfd_size_type locsymcount;
14802 Elf_Internal_Sym *local_syms = NULL;
14803 struct plt_entry *ent;
14804
14805 if (!is_ppc64_elf (ibfd))
14806 continue;
14807
14808 lgot_ents = elf_local_got_ents (ibfd);
14809 if (!lgot_ents)
14810 continue;
14811
14812 symtab_hdr = &elf_symtab_hdr (ibfd);
14813 locsymcount = symtab_hdr->sh_info;
14814 end_lgot_ents = lgot_ents + locsymcount;
14815 local_plt = (struct plt_entry **) end_lgot_ents;
14816 end_local_plt = local_plt + locsymcount;
14817 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14818 for (ent = *lplt; ent != NULL; ent = ent->next)
14819 if (ent->plt.offset != (bfd_vma) -1)
14820 {
14821 Elf_Internal_Sym *sym;
14822 asection *sym_sec;
14823 asection *plt, *relplt;
14824 bfd_vma val;
14825
14826 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14827 lplt - local_plt, ibfd))
14828 {
14829 if (symtab_hdr->contents != (unsigned char *) local_syms)
14830 free (local_syms);
14831 return false;
14832 }
14833
14834 val = sym->st_value + ent->addend;
14835 if (sym_sec != NULL && sym_sec->output_section != NULL)
14836 val += sym_sec->output_offset + sym_sec->output_section->vma;
14837
14838 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14839 {
14840 htab->elf.ifunc_resolvers = true;
14841 plt = htab->elf.iplt;
14842 relplt = htab->elf.irelplt;
14843 }
14844 else
14845 {
14846 plt = htab->pltlocal;
14847 relplt = NULL;
14848 if (bfd_link_pic (info)
14849 && !(info->enable_dt_relr && !htab->opd_abi))
14850 relplt = htab->relpltlocal;
14851 }
14852
14853 if (relplt == NULL)
14854 {
14855 bfd_byte *loc = plt->contents + ent->plt.offset;
14856 bfd_put_64 (info->output_bfd, val, loc);
14857 if (htab->opd_abi)
14858 {
14859 bfd_vma toc = elf_gp (ibfd);
14860 bfd_put_64 (info->output_bfd, toc, loc + 8);
14861 }
14862 }
14863 else
14864 {
14865 Elf_Internal_Rela rela;
14866 rela.r_offset = (ent->plt.offset
14867 + plt->output_offset
14868 + plt->output_section->vma);
14869 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14870 {
14871 if (htab->opd_abi)
14872 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14873 else
14874 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14875 }
14876 else
14877 {
14878 if (htab->opd_abi)
14879 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14880 else
14881 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14882 }
14883 rela.r_addend = val;
14884 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
14885 &rela, relplt));
14886 }
14887 }
14888
14889 if (local_syms != NULL
14890 && symtab_hdr->contents != (unsigned char *) local_syms)
14891 {
14892 if (!info->keep_memory)
14893 free (local_syms);
14894 else
14895 symtab_hdr->contents = (unsigned char *) local_syms;
14896 }
14897 }
14898 return true;
14899 }
14900
14901 /* Emit the static wrapper function preserving registers around a
14902 __tls_get_addr_opt call. */
14903
14904 static bool
14905 emit_tga_desc (struct ppc_link_hash_table *htab)
14906 {
14907 asection *stub_sec = htab->tga_group->stub_sec;
14908 unsigned int cfa_updt = 11 * 4;
14909 bfd_byte *p;
14910 bfd_vma to, from, delta;
14911
14912 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14913 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14914 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14915 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14916 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14917 delta = to - from;
14918 if (delta + (1 << 25) >= 1 << 26)
14919 {
14920 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14921 htab->stub_error = true;
14922 return false;
14923 }
14924
14925 p = stub_sec->contents;
14926 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14927 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14928 p += 4;
14929 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14930 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14931 }
14932
14933 /* Emit eh_frame describing the static wrapper function. */
14934
14935 static bfd_byte *
14936 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14937 {
14938 unsigned int cfa_updt = 11 * 4;
14939 unsigned int i;
14940
14941 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14942 *p++ = DW_CFA_def_cfa_offset;
14943 if (htab->opd_abi)
14944 {
14945 *p++ = 128;
14946 *p++ = 1;
14947 }
14948 else
14949 *p++ = 96;
14950 *p++ = DW_CFA_offset_extended_sf;
14951 *p++ = 65;
14952 *p++ = (-16 / 8) & 0x7f;
14953 for (i = 4; i < 12; i++)
14954 {
14955 *p++ = DW_CFA_offset + i;
14956 *p++ = (htab->opd_abi ? 13 : 12) - i;
14957 }
14958 *p++ = DW_CFA_advance_loc + 10;
14959 *p++ = DW_CFA_def_cfa_offset;
14960 *p++ = 0;
14961 for (i = 4; i < 12; i++)
14962 *p++ = DW_CFA_restore + i;
14963 *p++ = DW_CFA_advance_loc + 2;
14964 *p++ = DW_CFA_restore_extended;
14965 *p++ = 65;
14966 return p;
14967 }
14968
14969 /* Build all the stubs associated with the current output file.
14970 The stubs are kept in a hash table attached to the main linker
14971 hash table. This function is called via gldelf64ppc_finish. */
14972
14973 bool
14974 ppc64_elf_build_stubs (struct bfd_link_info *info,
14975 char **stats)
14976 {
14977 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14978 struct map_stub *group;
14979 asection *stub_sec;
14980 bfd_byte *p;
14981 int stub_sec_count = 0;
14982
14983 if (htab == NULL)
14984 return false;
14985
14986 /* Allocate memory to hold the linker stubs. */
14987 for (group = htab->group; group != NULL; group = group->next)
14988 {
14989 group->eh_size = 0;
14990 group->lr_restore = 0;
14991 if ((stub_sec = group->stub_sec) != NULL
14992 && stub_sec->size != 0)
14993 {
14994 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14995 stub_sec->size);
14996 if (stub_sec->contents == NULL)
14997 return false;
14998 stub_sec->size = 0;
14999 }
15000 }
15001
15002 if (htab->glink != NULL && htab->glink->size != 0)
15003 {
15004 unsigned int indx;
15005 bfd_vma plt0;
15006
15007 /* Build the .glink plt call stub. */
15008 if (htab->params->emit_stub_syms)
15009 {
15010 struct elf_link_hash_entry *h;
15011 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
15012 true, false, false);
15013 if (h == NULL)
15014 return false;
15015 if (h->root.type == bfd_link_hash_new)
15016 {
15017 h->root.type = bfd_link_hash_defined;
15018 h->root.u.def.section = htab->glink;
15019 h->root.u.def.value = 8;
15020 h->ref_regular = 1;
15021 h->def_regular = 1;
15022 h->ref_regular_nonweak = 1;
15023 h->forced_local = 1;
15024 h->non_elf = 0;
15025 h->root.linker_def = 1;
15026 }
15027 }
15028 plt0 = (htab->elf.splt->output_section->vma
15029 + htab->elf.splt->output_offset
15030 - 16);
15031 if (info->emitrelocations)
15032 {
15033 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15034 if (r == NULL)
15035 return false;
15036 r->r_offset = (htab->glink->output_offset
15037 + htab->glink->output_section->vma);
15038 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15039 r->r_addend = plt0;
15040 }
15041 p = htab->glink->contents;
15042 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
15043 bfd_put_64 (htab->glink->owner, plt0, p);
15044 p += 8;
15045 if (htab->opd_abi)
15046 {
15047 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15048 p += 4;
15049 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15050 p += 4;
15051 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15052 p += 4;
15053 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15054 p += 4;
15055 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15056 p += 4;
15057 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15058 p += 4;
15059 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15060 p += 4;
15061 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15062 p += 4;
15063 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15064 p += 4;
15065 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15066 p += 4;
15067 }
15068 else
15069 {
15070 unsigned int insn;
15071
15072 /* 0:
15073 . .quad plt0-1f # plt0 entry relative to 1:
15074 #
15075 # We get here with r12 initially @ a glink branch
15076 # Load the address of _dl_runtime_resolve from plt0 and
15077 # jump to it, with r0 set to the index of the PLT entry
15078 # to be resolved and r11 the link map.
15079 __glink_PLTresolve:
15080 . std %r2,24(%r1) # optional
15081 . mflr %r0
15082 . bcl 20,31,1f
15083 1:
15084 . mflr %r11
15085 . mtlr %r0
15086 . ld %r0,(0b-1b)(%r11)
15087 . sub %r12,%r12,%r11
15088 . add %r11,%r0,%r11
15089 . addi %r0,%r12,1b-2f
15090 . ld %r12,0(%r11)
15091 . srdi %r0,%r0,2
15092 . mtctr %r12
15093 . ld %r11,8(%r11)
15094 . bctr
15095 2:
15096 . b __glink_PLTresolve
15097 . ...
15098 . b __glink_PLTresolve */
15099
15100 if (htab->has_plt_localentry0)
15101 {
15102 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15103 p += 4;
15104 }
15105 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15106 p += 4;
15107 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15108 p += 4;
15109 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15110 p += 4;
15111 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15112 p += 4;
15113 if (htab->has_plt_localentry0)
15114 insn = LD_R0_0R11 | (-20 & 0xfffc);
15115 else
15116 insn = LD_R0_0R11 | (-16 & 0xfffc);
15117 bfd_put_32 (htab->glink->owner, insn, p);
15118 p += 4;
15119 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15120 p += 4;
15121 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15122 p += 4;
15123 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15124 p += 4;
15125 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15126 p += 4;
15127 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15128 p += 4;
15129 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15130 p += 4;
15131 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15132 p += 4;
15133 }
15134 bfd_put_32 (htab->glink->owner, BCTR, p);
15135 p += 4;
15136 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15137
15138 /* Build the .glink lazy link call stubs. */
15139 indx = 0;
15140 while (p < htab->glink->contents + htab->glink->size)
15141 {
15142 if (htab->opd_abi)
15143 {
15144 if (indx < 0x8000)
15145 {
15146 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15147 p += 4;
15148 }
15149 else
15150 {
15151 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15152 p += 4;
15153 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15154 p);
15155 p += 4;
15156 }
15157 }
15158 bfd_put_32 (htab->glink->owner,
15159 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15160 indx++;
15161 p += 4;
15162 }
15163 }
15164
15165 if (htab->tga_group != NULL)
15166 {
15167 htab->tga_group->lr_restore = 23 * 4;
15168 htab->tga_group->stub_sec->size = 24 * 4;
15169 if (!emit_tga_desc (htab))
15170 return false;
15171 if (htab->glink_eh_frame != NULL
15172 && htab->glink_eh_frame->size != 0)
15173 {
15174 size_t align = 4;
15175
15176 p = htab->glink_eh_frame->contents;
15177 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15178 p += 17;
15179 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15180 }
15181 }
15182
15183 /* Build .glink global entry stubs, and PLT relocs for globals. */
15184 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15185
15186 if (!write_plt_relocs_for_local_syms (info))
15187 return false;
15188
15189 if (htab->brlt != NULL && htab->brlt->size != 0)
15190 {
15191 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15192 htab->brlt->size);
15193 if (htab->brlt->contents == NULL)
15194 return false;
15195 }
15196 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15197 {
15198 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15199 htab->relbrlt->size);
15200 if (htab->relbrlt->contents == NULL)
15201 return false;
15202 }
15203
15204 /* Build the stubs as directed by the stub hash table. */
15205 htab->stub_id = 0;
15206 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15207
15208 for (group = htab->group; group != NULL; group = group->next)
15209 if (group->needs_save_res)
15210 group->stub_sec->size += htab->sfpr->size;
15211
15212 if (htab->relbrlt != NULL)
15213 htab->relbrlt->reloc_count = 0;
15214
15215 if (htab->params->plt_stub_align != 0)
15216 for (group = htab->group; group != NULL; group = group->next)
15217 if ((stub_sec = group->stub_sec) != NULL)
15218 {
15219 int align = abs (htab->params->plt_stub_align);
15220 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15221 }
15222
15223 for (group = htab->group; group != NULL; group = group->next)
15224 if (group->needs_save_res)
15225 {
15226 stub_sec = group->stub_sec;
15227 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15228 htab->sfpr->contents, htab->sfpr->size);
15229 if (htab->params->emit_stub_syms)
15230 {
15231 unsigned int i;
15232
15233 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15234 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15235 return false;
15236 }
15237 }
15238
15239 if (htab->glink_eh_frame != NULL
15240 && htab->glink_eh_frame->size != 0)
15241 {
15242 bfd_vma val;
15243 size_t align = 4;
15244
15245 p = htab->glink_eh_frame->contents;
15246 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15247
15248 for (group = htab->group; group != NULL; group = group->next)
15249 if (group->eh_size != 0)
15250 {
15251 /* Offset to stub section. */
15252 val = (group->stub_sec->output_section->vma
15253 + group->stub_sec->output_offset);
15254 val -= (htab->glink_eh_frame->output_section->vma
15255 + htab->glink_eh_frame->output_offset
15256 + (p + 8 - htab->glink_eh_frame->contents));
15257 if (val + 0x80000000 > 0xffffffff)
15258 {
15259 _bfd_error_handler
15260 (_("%s offset too large for .eh_frame sdata4 encoding"),
15261 group->stub_sec->name);
15262 return false;
15263 }
15264 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15265 p += (group->eh_size + 17 + 3) & -4;
15266 }
15267 if (htab->glink != NULL && htab->glink->size != 0)
15268 {
15269 /* Offset to .glink. */
15270 val = (htab->glink->output_section->vma
15271 + htab->glink->output_offset
15272 + 8);
15273 val -= (htab->glink_eh_frame->output_section->vma
15274 + htab->glink_eh_frame->output_offset
15275 + (p + 8 - htab->glink_eh_frame->contents));
15276 if (val + 0x80000000 > 0xffffffff)
15277 {
15278 _bfd_error_handler
15279 (_("%s offset too large for .eh_frame sdata4 encoding"),
15280 htab->glink->name);
15281 return false;
15282 }
15283 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15284 p += (24 + align - 1) & -align;
15285 }
15286 }
15287
15288 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15289 {
15290 htab->elf.srelrdyn->contents
15291 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15292 if (htab->elf.srelrdyn->contents == NULL)
15293 return false;
15294
15295 bfd_vma *relr_addr = sort_relr (htab);
15296 if (htab->relr_count != 0 && relr_addr == NULL)
15297 return false;
15298
15299 size_t i = 0;
15300 bfd_byte *loc = htab->elf.srelrdyn->contents;
15301 while (i < htab->relr_count)
15302 {
15303 bfd_vma base = relr_addr[i];
15304 BFD_ASSERT ((base & ((1 << RELR_ALIGN) - 1)) == 0);
15305 bfd_put_64 (htab->elf.dynobj, base, loc);
15306 loc += 8;
15307 i++;
15308 while (i < htab->relr_count
15309 && relr_addr[i] == base)
15310 {
15311 htab->stub_error = true;
15312 i++;
15313 }
15314 base += 8;
15315 while (1)
15316 {
15317 bfd_vma bits = 0;
15318 while (i < htab->relr_count
15319 && relr_addr[i] - base < 63 * 8
15320 && (relr_addr[i] - base) % 8 == 0)
15321 {
15322 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15323 i++;
15324 }
15325 if (bits == 0)
15326 break;
15327 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15328 loc += 8;
15329 base += 63 * 8;
15330 }
15331 }
15332 free (relr_addr);
15333 /* Pad any excess with 1's, a do-nothing encoding. */
15334 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15335 < htab->elf.srelrdyn->size)
15336 {
15337 bfd_put_64 (htab->elf.dynobj, 1, loc);
15338 loc += 8;
15339 }
15340 }
15341
15342 for (group = htab->group; group != NULL; group = group->next)
15343 if ((stub_sec = group->stub_sec) != NULL)
15344 {
15345 stub_sec_count += 1;
15346 if (stub_sec->rawsize != stub_sec->size
15347 && (htab->stub_iteration <= STUB_SHRINK_ITER
15348 || stub_sec->rawsize < stub_sec->size))
15349 break;
15350 }
15351
15352 if (group != NULL)
15353 htab->stub_error = true;
15354
15355 if (htab->stub_error)
15356 {
15357 _bfd_error_handler (_("stubs don't match calculated size"));
15358 return false;
15359 }
15360
15361 if (stats != NULL)
15362 {
15363 char *groupmsg;
15364 if (asprintf (&groupmsg,
15365 ngettext ("linker stubs in %u group",
15366 "linker stubs in %u groups",
15367 stub_sec_count),
15368 stub_sec_count) < 0)
15369 *stats = NULL;
15370 else
15371 {
15372 if (asprintf (stats, _("%s, iter %u\n"
15373 " branch %lu\n"
15374 " long branch %lu\n"
15375 " plt call %lu\n"
15376 " global entry %lu"),
15377 groupmsg, htab->stub_iteration,
15378 htab->stub_count[ppc_stub_long_branch - 1],
15379 htab->stub_count[ppc_stub_plt_branch - 1],
15380 htab->stub_count[ppc_stub_plt_call - 1],
15381 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15382 *stats = NULL;
15383 free (groupmsg);
15384 }
15385 }
15386 return true;
15387 }
15388
15389 /* What to do when ld finds relocations against symbols defined in
15390 discarded sections. */
15391
15392 static unsigned int
15393 ppc64_elf_action_discarded (asection *sec)
15394 {
15395 if (strcmp (".opd", sec->name) == 0)
15396 return 0;
15397
15398 if (strcmp (".toc", sec->name) == 0)
15399 return 0;
15400
15401 if (strcmp (".toc1", sec->name) == 0)
15402 return 0;
15403
15404 return _bfd_elf_default_action_discarded (sec);
15405 }
15406
15407 /* These are the dynamic relocations supported by glibc. */
15408
15409 static bool
15410 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15411 {
15412 switch (r_type)
15413 {
15414 case R_PPC64_RELATIVE:
15415 case R_PPC64_NONE:
15416 case R_PPC64_ADDR64:
15417 case R_PPC64_GLOB_DAT:
15418 case R_PPC64_IRELATIVE:
15419 case R_PPC64_JMP_IREL:
15420 case R_PPC64_JMP_SLOT:
15421 case R_PPC64_DTPMOD64:
15422 case R_PPC64_DTPREL64:
15423 case R_PPC64_TPREL64:
15424 case R_PPC64_TPREL16_LO_DS:
15425 case R_PPC64_TPREL16_DS:
15426 case R_PPC64_TPREL16:
15427 case R_PPC64_TPREL16_LO:
15428 case R_PPC64_TPREL16_HI:
15429 case R_PPC64_TPREL16_HIGH:
15430 case R_PPC64_TPREL16_HA:
15431 case R_PPC64_TPREL16_HIGHA:
15432 case R_PPC64_TPREL16_HIGHER:
15433 case R_PPC64_TPREL16_HIGHEST:
15434 case R_PPC64_TPREL16_HIGHERA:
15435 case R_PPC64_TPREL16_HIGHESTA:
15436 case R_PPC64_ADDR16_LO_DS:
15437 case R_PPC64_ADDR16_LO:
15438 case R_PPC64_ADDR16_HI:
15439 case R_PPC64_ADDR16_HIGH:
15440 case R_PPC64_ADDR16_HA:
15441 case R_PPC64_ADDR16_HIGHA:
15442 case R_PPC64_REL30:
15443 case R_PPC64_COPY:
15444 case R_PPC64_UADDR64:
15445 case R_PPC64_UADDR32:
15446 case R_PPC64_ADDR32:
15447 case R_PPC64_ADDR24:
15448 case R_PPC64_ADDR16:
15449 case R_PPC64_UADDR16:
15450 case R_PPC64_ADDR16_DS:
15451 case R_PPC64_ADDR16_HIGHER:
15452 case R_PPC64_ADDR16_HIGHEST:
15453 case R_PPC64_ADDR16_HIGHERA:
15454 case R_PPC64_ADDR16_HIGHESTA:
15455 case R_PPC64_ADDR14:
15456 case R_PPC64_ADDR14_BRTAKEN:
15457 case R_PPC64_ADDR14_BRNTAKEN:
15458 case R_PPC64_REL32:
15459 case R_PPC64_REL64:
15460 return true;
15461
15462 default:
15463 return false;
15464 }
15465 }
15466
15467 /* The RELOCATE_SECTION function is called by the ELF backend linker
15468 to handle the relocations for a section.
15469
15470 The relocs are always passed as Rela structures; if the section
15471 actually uses Rel structures, the r_addend field will always be
15472 zero.
15473
15474 This function is responsible for adjust the section contents as
15475 necessary, and (if using Rela relocs and generating a
15476 relocatable output file) adjusting the reloc addend as
15477 necessary.
15478
15479 This function does not have to worry about setting the reloc
15480 address or the reloc symbol index.
15481
15482 LOCAL_SYMS is a pointer to the swapped in local symbols.
15483
15484 LOCAL_SECTIONS is an array giving the section in the input file
15485 corresponding to the st_shndx field of each local symbol.
15486
15487 The global hash table entry for the global symbols can be found
15488 via elf_sym_hashes (input_bfd).
15489
15490 When generating relocatable output, this function must handle
15491 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15492 going to be the section symbol corresponding to the output
15493 section, which means that the addend must be adjusted
15494 accordingly. */
15495
15496 static int
15497 ppc64_elf_relocate_section (bfd *output_bfd,
15498 struct bfd_link_info *info,
15499 bfd *input_bfd,
15500 asection *input_section,
15501 bfd_byte *contents,
15502 Elf_Internal_Rela *relocs,
15503 Elf_Internal_Sym *local_syms,
15504 asection **local_sections)
15505 {
15506 struct ppc_link_hash_table *htab;
15507 Elf_Internal_Shdr *symtab_hdr;
15508 struct elf_link_hash_entry **sym_hashes;
15509 Elf_Internal_Rela *rel;
15510 Elf_Internal_Rela *wrel;
15511 Elf_Internal_Rela *relend;
15512 Elf_Internal_Rela outrel;
15513 bfd_byte *loc;
15514 struct got_entry **local_got_ents;
15515 bfd_vma TOCstart;
15516 bool ret = true;
15517 bool is_opd;
15518 /* Assume 'at' branch hints. */
15519 bool is_isa_v2 = true;
15520 bool warned_dynamic = false;
15521 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15522
15523 /* Initialize howto table if needed. */
15524 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15525 ppc_howto_init ();
15526
15527 htab = ppc_hash_table (info);
15528 if (htab == NULL)
15529 return false;
15530
15531 /* Don't relocate stub sections. */
15532 if (input_section->owner == htab->params->stub_bfd)
15533 return true;
15534
15535 if (!is_ppc64_elf (input_bfd))
15536 {
15537 bfd_set_error (bfd_error_wrong_format);
15538 return false;
15539 }
15540
15541 local_got_ents = elf_local_got_ents (input_bfd);
15542 TOCstart = elf_gp (output_bfd);
15543 symtab_hdr = &elf_symtab_hdr (input_bfd);
15544 sym_hashes = elf_sym_hashes (input_bfd);
15545 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15546
15547 rel = wrel = relocs;
15548 relend = relocs + input_section->reloc_count;
15549 for (; rel < relend; wrel++, rel++)
15550 {
15551 enum elf_ppc64_reloc_type r_type;
15552 bfd_vma addend;
15553 bfd_reloc_status_type r;
15554 Elf_Internal_Sym *sym;
15555 asection *sec;
15556 struct elf_link_hash_entry *h_elf;
15557 struct ppc_link_hash_entry *h;
15558 struct ppc_link_hash_entry *fdh;
15559 const char *sym_name;
15560 unsigned long r_symndx, toc_symndx;
15561 bfd_vma toc_addend;
15562 unsigned char tls_mask, tls_gd, tls_type;
15563 unsigned char sym_type;
15564 bfd_vma relocation;
15565 bool unresolved_reloc, save_unresolved_reloc;
15566 bool warned;
15567 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15568 unsigned int insn;
15569 unsigned int mask;
15570 struct ppc_stub_hash_entry *stub_entry;
15571 bfd_vma max_br_offset;
15572 bfd_vma from;
15573 Elf_Internal_Rela orig_rel;
15574 reloc_howto_type *howto;
15575 struct reloc_howto_struct alt_howto;
15576 uint64_t pinsn;
15577 bfd_vma offset;
15578
15579 again:
15580 orig_rel = *rel;
15581
15582 r_type = ELF64_R_TYPE (rel->r_info);
15583 r_symndx = ELF64_R_SYM (rel->r_info);
15584
15585 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15586 symbol of the previous ADDR64 reloc. The symbol gives us the
15587 proper TOC base to use. */
15588 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15589 && wrel != relocs
15590 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15591 && is_opd)
15592 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15593
15594 sym = NULL;
15595 sec = NULL;
15596 h_elf = NULL;
15597 sym_name = NULL;
15598 unresolved_reloc = false;
15599 warned = false;
15600
15601 if (r_symndx < symtab_hdr->sh_info)
15602 {
15603 /* It's a local symbol. */
15604 struct _opd_sec_data *opd;
15605
15606 sym = local_syms + r_symndx;
15607 sec = local_sections[r_symndx];
15608 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15609 sym_type = ELF64_ST_TYPE (sym->st_info);
15610 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15611 opd = get_opd_info (sec);
15612 if (opd != NULL && opd->adjust != NULL)
15613 {
15614 long adjust = opd->adjust[OPD_NDX (sym->st_value
15615 + rel->r_addend)];
15616 if (adjust == -1)
15617 relocation = 0;
15618 else
15619 {
15620 /* If this is a relocation against the opd section sym
15621 and we have edited .opd, adjust the reloc addend so
15622 that ld -r and ld --emit-relocs output is correct.
15623 If it is a reloc against some other .opd symbol,
15624 then the symbol value will be adjusted later. */
15625 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15626 rel->r_addend += adjust;
15627 else
15628 relocation += adjust;
15629 }
15630 }
15631 }
15632 else
15633 {
15634 bool ignored;
15635
15636 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15637 r_symndx, symtab_hdr, sym_hashes,
15638 h_elf, sec, relocation,
15639 unresolved_reloc, warned, ignored);
15640 sym_name = h_elf->root.root.string;
15641 sym_type = h_elf->type;
15642 if (sec != NULL
15643 && sec->owner == output_bfd
15644 && strcmp (sec->name, ".opd") == 0)
15645 {
15646 /* This is a symbol defined in a linker script. All
15647 such are defined in output sections, even those
15648 defined by simple assignment from a symbol defined in
15649 an input section. Transfer the symbol to an
15650 appropriate input .opd section, so that a branch to
15651 this symbol will be mapped to the location specified
15652 by the opd entry. */
15653 struct bfd_link_order *lo;
15654 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15655 if (lo->type == bfd_indirect_link_order)
15656 {
15657 asection *isec = lo->u.indirect.section;
15658 if (h_elf->root.u.def.value >= isec->output_offset
15659 && h_elf->root.u.def.value < (isec->output_offset
15660 + isec->size))
15661 {
15662 h_elf->root.u.def.value -= isec->output_offset;
15663 h_elf->root.u.def.section = isec;
15664 sec = isec;
15665 break;
15666 }
15667 }
15668 }
15669 }
15670 h = ppc_elf_hash_entry (h_elf);
15671
15672 if (sec != NULL && discarded_section (sec))
15673 {
15674 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15675 input_bfd, input_section,
15676 contents, rel->r_offset);
15677 wrel->r_offset = rel->r_offset;
15678 wrel->r_info = 0;
15679 wrel->r_addend = 0;
15680
15681 /* For ld -r, remove relocations in debug sections against
15682 symbols defined in discarded sections. Not done for
15683 non-debug to preserve relocs in .eh_frame which the
15684 eh_frame editing code expects to be present. */
15685 if (bfd_link_relocatable (info)
15686 && (input_section->flags & SEC_DEBUGGING))
15687 wrel--;
15688
15689 continue;
15690 }
15691
15692 if (bfd_link_relocatable (info))
15693 goto copy_reloc;
15694
15695 if (h != NULL && &h->elf == htab->elf.hgot)
15696 {
15697 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15698 sec = bfd_abs_section_ptr;
15699 unresolved_reloc = false;
15700 }
15701
15702 /* TLS optimizations. Replace instruction sequences and relocs
15703 based on information we collected in tls_optimize. We edit
15704 RELOCS so that --emit-relocs will output something sensible
15705 for the final instruction stream. */
15706 tls_mask = 0;
15707 tls_gd = 0;
15708 toc_symndx = 0;
15709 if (h != NULL)
15710 tls_mask = h->tls_mask;
15711 else if (local_got_ents != NULL)
15712 {
15713 struct plt_entry **local_plt = (struct plt_entry **)
15714 (local_got_ents + symtab_hdr->sh_info);
15715 unsigned char *lgot_masks = (unsigned char *)
15716 (local_plt + symtab_hdr->sh_info);
15717 tls_mask = lgot_masks[r_symndx];
15718 }
15719 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15720 && (r_type == R_PPC64_TLS
15721 || r_type == R_PPC64_TLSGD
15722 || r_type == R_PPC64_TLSLD))
15723 {
15724 /* Check for toc tls entries. */
15725 unsigned char *toc_tls;
15726
15727 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15728 &local_syms, rel, input_bfd))
15729 return false;
15730
15731 if (toc_tls)
15732 tls_mask = *toc_tls;
15733 }
15734
15735 /* Check that tls relocs are used with tls syms, and non-tls
15736 relocs are used with non-tls syms. */
15737 if (r_symndx != STN_UNDEF
15738 && r_type != R_PPC64_NONE
15739 && (h == NULL
15740 || h->elf.root.type == bfd_link_hash_defined
15741 || h->elf.root.type == bfd_link_hash_defweak)
15742 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15743 {
15744 if ((tls_mask & TLS_TLS) != 0
15745 && (r_type == R_PPC64_TLS
15746 || r_type == R_PPC64_TLSGD
15747 || r_type == R_PPC64_TLSLD))
15748 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15749 ;
15750 else
15751 info->callbacks->einfo
15752 (!IS_PPC64_TLS_RELOC (r_type)
15753 /* xgettext:c-format */
15754 ? _("%H: %s used with TLS symbol `%pT'\n")
15755 /* xgettext:c-format */
15756 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15757 input_bfd, input_section, rel->r_offset,
15758 ppc64_elf_howto_table[r_type]->name,
15759 sym_name);
15760 }
15761
15762 /* Ensure reloc mapping code below stays sane. */
15763 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15764 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15765 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15766 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15767 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15768 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15769 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15770 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15771 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15772 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15773 abort ();
15774
15775 switch (r_type)
15776 {
15777 default:
15778 break;
15779
15780 case R_PPC64_LO_DS_OPT:
15781 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15782 {
15783 insn = bfd_get_32 (input_bfd,
15784 contents + rel->r_offset - d_offset);
15785 if ((insn & (0x3fu << 26)) != 58u << 26)
15786 abort ();
15787 insn += (14u << 26) - (58u << 26);
15788 bfd_put_32 (input_bfd, insn,
15789 contents + rel->r_offset - d_offset);
15790 r_type = R_PPC64_TOC16_LO;
15791 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15792 }
15793 break;
15794
15795 case R_PPC64_TOC16:
15796 case R_PPC64_TOC16_LO:
15797 case R_PPC64_TOC16_DS:
15798 case R_PPC64_TOC16_LO_DS:
15799 {
15800 /* Check for toc tls entries. */
15801 unsigned char *toc_tls;
15802 int retval;
15803
15804 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15805 &local_syms, rel, input_bfd);
15806 if (retval == 0)
15807 return false;
15808
15809 if (toc_tls)
15810 {
15811 tls_mask = *toc_tls;
15812 if (r_type == R_PPC64_TOC16_DS
15813 || r_type == R_PPC64_TOC16_LO_DS)
15814 {
15815 if ((tls_mask & TLS_TLS) != 0
15816 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15817 goto toctprel;
15818 }
15819 else
15820 {
15821 /* If we found a GD reloc pair, then we might be
15822 doing a GD->IE transition. */
15823 if (retval == 2)
15824 {
15825 tls_gd = TLS_GDIE;
15826 if ((tls_mask & TLS_TLS) != 0
15827 && (tls_mask & TLS_GD) == 0)
15828 goto tls_ldgd_opt;
15829 }
15830 else if (retval == 3)
15831 {
15832 if ((tls_mask & TLS_TLS) != 0
15833 && (tls_mask & TLS_LD) == 0)
15834 goto tls_ldgd_opt;
15835 }
15836 }
15837 }
15838 }
15839 break;
15840
15841 case R_PPC64_GOT_TPREL16_HI:
15842 case R_PPC64_GOT_TPREL16_HA:
15843 if ((tls_mask & TLS_TLS) != 0
15844 && (tls_mask & TLS_TPREL) == 0
15845 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15846 {
15847 rel->r_offset -= d_offset;
15848 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15849 r_type = R_PPC64_NONE;
15850 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15851 }
15852 break;
15853
15854 case R_PPC64_GOT_TPREL16_DS:
15855 case R_PPC64_GOT_TPREL16_LO_DS:
15856 if ((tls_mask & TLS_TLS) != 0
15857 && (tls_mask & TLS_TPREL) == 0
15858 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15859 {
15860 toctprel:
15861 insn = bfd_get_32 (input_bfd,
15862 contents + rel->r_offset - d_offset);
15863 insn &= 31 << 21;
15864 insn |= 0x3c0d0000; /* addis 0,13,0 */
15865 bfd_put_32 (input_bfd, insn,
15866 contents + rel->r_offset - d_offset);
15867 r_type = R_PPC64_TPREL16_HA;
15868 if (toc_symndx != 0)
15869 {
15870 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15871 rel->r_addend = toc_addend;
15872 /* We changed the symbol. Start over in order to
15873 get h, sym, sec etc. right. */
15874 goto again;
15875 }
15876 else
15877 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15878 }
15879 break;
15880
15881 case R_PPC64_GOT_TPREL_PCREL34:
15882 if ((tls_mask & TLS_TLS) != 0
15883 && (tls_mask & TLS_TPREL) == 0
15884 && offset_in_range (input_section, rel->r_offset, 8))
15885 {
15886 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15887 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15888 pinsn <<= 32;
15889 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15890 pinsn += ((2ULL << 56) + (-1ULL << 52)
15891 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15892 bfd_put_32 (input_bfd, pinsn >> 32,
15893 contents + rel->r_offset);
15894 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15895 contents + rel->r_offset + 4);
15896 r_type = R_PPC64_TPREL34;
15897 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15898 }
15899 break;
15900
15901 case R_PPC64_TLS:
15902 if ((tls_mask & TLS_TLS) != 0
15903 && (tls_mask & TLS_TPREL) == 0
15904 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15905 {
15906 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15907 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15908 if (insn == 0)
15909 break;
15910 if ((rel->r_offset & 3) == 0)
15911 {
15912 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15913 /* Was PPC64_TLS which sits on insn boundary, now
15914 PPC64_TPREL16_LO which is at low-order half-word. */
15915 rel->r_offset += d_offset;
15916 r_type = R_PPC64_TPREL16_LO;
15917 if (toc_symndx != 0)
15918 {
15919 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15920 rel->r_addend = toc_addend;
15921 /* We changed the symbol. Start over in order to
15922 get h, sym, sec etc. right. */
15923 goto again;
15924 }
15925 else
15926 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15927 }
15928 else if ((rel->r_offset & 3) == 1)
15929 {
15930 /* For pcrel IE to LE we already have the full
15931 offset and thus don't need an addi here. A nop
15932 or mr will do. */
15933 if ((insn & (0x3fu << 26)) == 14 << 26)
15934 {
15935 /* Extract regs from addi rt,ra,si. */
15936 unsigned int rt = (insn >> 21) & 0x1f;
15937 unsigned int ra = (insn >> 16) & 0x1f;
15938 if (rt == ra)
15939 insn = NOP;
15940 else
15941 {
15942 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15943 insn = (rt << 16) | (ra << 21) | (ra << 11);
15944 insn |= (31u << 26) | (444u << 1);
15945 }
15946 }
15947 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15948 }
15949 }
15950 break;
15951
15952 case R_PPC64_GOT_TLSGD16_HI:
15953 case R_PPC64_GOT_TLSGD16_HA:
15954 tls_gd = TLS_GDIE;
15955 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15956 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15957 goto tls_gdld_hi;
15958 break;
15959
15960 case R_PPC64_GOT_TLSLD16_HI:
15961 case R_PPC64_GOT_TLSLD16_HA:
15962 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15963 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15964 {
15965 tls_gdld_hi:
15966 if ((tls_mask & tls_gd) != 0)
15967 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15968 + R_PPC64_GOT_TPREL16_DS);
15969 else
15970 {
15971 rel->r_offset -= d_offset;
15972 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15973 r_type = R_PPC64_NONE;
15974 }
15975 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15976 }
15977 break;
15978
15979 case R_PPC64_GOT_TLSGD16:
15980 case R_PPC64_GOT_TLSGD16_LO:
15981 tls_gd = TLS_GDIE;
15982 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15983 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15984 goto tls_ldgd_opt;
15985 break;
15986
15987 case R_PPC64_GOT_TLSLD16:
15988 case R_PPC64_GOT_TLSLD16_LO:
15989 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15990 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15991 {
15992 unsigned int insn1, insn2;
15993
15994 tls_ldgd_opt:
15995 offset = (bfd_vma) -1;
15996 /* If not using the newer R_PPC64_TLSGD/LD to mark
15997 __tls_get_addr calls, we must trust that the call
15998 stays with its arg setup insns, ie. that the next
15999 reloc is the __tls_get_addr call associated with
16000 the current reloc. Edit both insns. */
16001 if (input_section->nomark_tls_get_addr
16002 && rel + 1 < relend
16003 && branch_reloc_hash_match (input_bfd, rel + 1,
16004 htab->tls_get_addr_fd,
16005 htab->tga_desc_fd,
16006 htab->tls_get_addr,
16007 htab->tga_desc))
16008 offset = rel[1].r_offset;
16009 /* We read the low GOT_TLS (or TOC16) insn because we
16010 need to keep the destination reg. It may be
16011 something other than the usual r3, and moved to r3
16012 before the call by intervening code. */
16013 insn1 = bfd_get_32 (input_bfd,
16014 contents + rel->r_offset - d_offset);
16015 if ((tls_mask & tls_gd) != 0)
16016 {
16017 /* IE */
16018 insn1 &= (0x1f << 21) | (0x1f << 16);
16019 insn1 |= 58u << 26; /* ld */
16020 insn2 = 0x7c636a14; /* add 3,3,13 */
16021 if (offset != (bfd_vma) -1)
16022 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16023 if (r_type == R_PPC64_TOC16
16024 || r_type == R_PPC64_TOC16_LO)
16025 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
16026 else
16027 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
16028 + R_PPC64_GOT_TPREL16_DS);
16029 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16030 }
16031 else
16032 {
16033 /* LE */
16034 insn1 &= 0x1f << 21;
16035 insn1 |= 0x3c0d0000; /* addis r,13,0 */
16036 insn2 = 0x38630000; /* addi 3,3,0 */
16037 if (tls_gd == 0)
16038 {
16039 /* Was an LD reloc. */
16040 r_symndx = STN_UNDEF;
16041 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16042 }
16043 else if (toc_symndx != 0)
16044 {
16045 r_symndx = toc_symndx;
16046 rel->r_addend = toc_addend;
16047 }
16048 r_type = R_PPC64_TPREL16_HA;
16049 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16050 if (offset != (bfd_vma) -1)
16051 {
16052 rel[1].r_info = ELF64_R_INFO (r_symndx,
16053 R_PPC64_TPREL16_LO);
16054 rel[1].r_offset = offset + d_offset;
16055 rel[1].r_addend = rel->r_addend;
16056 }
16057 }
16058 bfd_put_32 (input_bfd, insn1,
16059 contents + rel->r_offset - d_offset);
16060 if (offset != (bfd_vma) -1
16061 && offset_in_range (input_section, offset, 4))
16062 {
16063 bfd_put_32 (input_bfd, insn2, contents + offset);
16064 if (offset_in_range (input_section, offset + 4, 4))
16065 {
16066 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16067 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16068 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16069 }
16070 }
16071 if ((tls_mask & tls_gd) == 0
16072 && (tls_gd == 0 || toc_symndx != 0))
16073 {
16074 /* We changed the symbol. Start over in order
16075 to get h, sym, sec etc. right. */
16076 goto again;
16077 }
16078 }
16079 break;
16080
16081 case R_PPC64_GOT_TLSGD_PCREL34:
16082 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16083 && offset_in_range (input_section, rel->r_offset, 8))
16084 {
16085 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16086 pinsn <<= 32;
16087 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16088 if ((tls_mask & TLS_GDIE) != 0)
16089 {
16090 /* IE, pla -> pld */
16091 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16092 r_type = R_PPC64_GOT_TPREL_PCREL34;
16093 }
16094 else
16095 {
16096 /* LE, pla pcrel -> paddi r13 */
16097 pinsn += (-1ULL << 52) + (13ULL << 16);
16098 r_type = R_PPC64_TPREL34;
16099 }
16100 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16101 bfd_put_32 (input_bfd, pinsn >> 32,
16102 contents + rel->r_offset);
16103 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16104 contents + rel->r_offset + 4);
16105 }
16106 break;
16107
16108 case R_PPC64_GOT_TLSLD_PCREL34:
16109 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16110 && offset_in_range (input_section, rel->r_offset, 8))
16111 {
16112 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16113 pinsn <<= 32;
16114 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16115 pinsn += (-1ULL << 52) + (13ULL << 16);
16116 bfd_put_32 (input_bfd, pinsn >> 32,
16117 contents + rel->r_offset);
16118 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16119 contents + rel->r_offset + 4);
16120 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16121 r_symndx = STN_UNDEF;
16122 r_type = R_PPC64_TPREL34;
16123 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16124 goto again;
16125 }
16126 break;
16127
16128 case R_PPC64_TLSGD:
16129 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16130 && rel + 1 < relend
16131 && offset_in_range (input_section, rel->r_offset,
16132 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16133 ? 8 : 4))
16134 {
16135 unsigned int insn2;
16136 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16137
16138 offset = rel->r_offset;
16139 if (is_plt_seq_reloc (r_type1))
16140 {
16141 bfd_put_32 (output_bfd, NOP, contents + offset);
16142 if (r_type1 == R_PPC64_PLT_PCREL34
16143 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16144 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16145 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16146 break;
16147 }
16148
16149 if (r_type1 == R_PPC64_PLTCALL)
16150 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16151
16152 if ((tls_mask & TLS_GDIE) != 0)
16153 {
16154 /* IE */
16155 r_type = R_PPC64_NONE;
16156 insn2 = 0x7c636a14; /* add 3,3,13 */
16157 }
16158 else
16159 {
16160 /* LE */
16161 if (toc_symndx != 0)
16162 {
16163 r_symndx = toc_symndx;
16164 rel->r_addend = toc_addend;
16165 }
16166 if (r_type1 == R_PPC64_REL24_NOTOC
16167 || r_type1 == R_PPC64_REL24_P9NOTOC
16168 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16169 {
16170 r_type = R_PPC64_NONE;
16171 insn2 = NOP;
16172 }
16173 else
16174 {
16175 rel->r_offset = offset + d_offset;
16176 r_type = R_PPC64_TPREL16_LO;
16177 insn2 = 0x38630000; /* addi 3,3,0 */
16178 }
16179 }
16180 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16181 /* Zap the reloc on the _tls_get_addr call too. */
16182 BFD_ASSERT (offset == rel[1].r_offset);
16183 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16184 bfd_put_32 (input_bfd, insn2, contents + offset);
16185 if ((tls_mask & TLS_GDIE) == 0
16186 && toc_symndx != 0
16187 && r_type != R_PPC64_NONE)
16188 goto again;
16189 }
16190 break;
16191
16192 case R_PPC64_TLSLD:
16193 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16194 && rel + 1 < relend
16195 && offset_in_range (input_section, rel->r_offset,
16196 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16197 ? 8 : 4))
16198 {
16199 unsigned int insn2;
16200 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16201
16202 offset = rel->r_offset;
16203 if (is_plt_seq_reloc (r_type1))
16204 {
16205 bfd_put_32 (output_bfd, NOP, contents + offset);
16206 if (r_type1 == R_PPC64_PLT_PCREL34
16207 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16208 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16209 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16210 break;
16211 }
16212
16213 if (r_type1 == R_PPC64_PLTCALL)
16214 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16215
16216 if (r_type1 == R_PPC64_REL24_NOTOC
16217 || r_type1 == R_PPC64_REL24_P9NOTOC
16218 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16219 {
16220 r_type = R_PPC64_NONE;
16221 insn2 = NOP;
16222 }
16223 else
16224 {
16225 rel->r_offset = offset + d_offset;
16226 r_symndx = STN_UNDEF;
16227 r_type = R_PPC64_TPREL16_LO;
16228 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16229 insn2 = 0x38630000; /* addi 3,3,0 */
16230 }
16231 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16232 /* Zap the reloc on the _tls_get_addr call too. */
16233 BFD_ASSERT (offset == rel[1].r_offset);
16234 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16235 bfd_put_32 (input_bfd, insn2, contents + offset);
16236 if (r_type != R_PPC64_NONE)
16237 goto again;
16238 }
16239 break;
16240
16241 case R_PPC64_DTPMOD64:
16242 if (rel + 1 < relend
16243 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16244 && rel[1].r_offset == rel->r_offset + 8)
16245 {
16246 if ((tls_mask & TLS_GD) == 0
16247 && offset_in_range (input_section, rel->r_offset, 8))
16248 {
16249 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16250 if ((tls_mask & TLS_GDIE) != 0)
16251 r_type = R_PPC64_TPREL64;
16252 else
16253 {
16254 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16255 r_type = R_PPC64_NONE;
16256 }
16257 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16258 }
16259 }
16260 else
16261 {
16262 if ((tls_mask & TLS_LD) == 0
16263 && offset_in_range (input_section, rel->r_offset, 8))
16264 {
16265 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16266 r_type = R_PPC64_NONE;
16267 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16268 }
16269 }
16270 break;
16271
16272 case R_PPC64_TPREL64:
16273 if ((tls_mask & TLS_TPREL) == 0)
16274 {
16275 r_type = R_PPC64_NONE;
16276 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16277 }
16278 break;
16279
16280 case R_PPC64_ENTRY:
16281 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16282 if (!bfd_link_pic (info)
16283 && !info->traditional_format
16284 && relocation + 0x80008000 <= 0xffffffff
16285 && offset_in_range (input_section, rel->r_offset, 8))
16286 {
16287 unsigned int insn1, insn2;
16288
16289 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16290 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16291 if ((insn1 & ~0xfffc) == LD_R2_0R12
16292 && insn2 == ADD_R2_R2_R12)
16293 {
16294 bfd_put_32 (input_bfd,
16295 LIS_R2 + PPC_HA (relocation),
16296 contents + rel->r_offset);
16297 bfd_put_32 (input_bfd,
16298 ADDI_R2_R2 + PPC_LO (relocation),
16299 contents + rel->r_offset + 4);
16300 }
16301 }
16302 else
16303 {
16304 relocation -= (rel->r_offset
16305 + input_section->output_offset
16306 + input_section->output_section->vma);
16307 if (relocation + 0x80008000 <= 0xffffffff
16308 && offset_in_range (input_section, rel->r_offset, 8))
16309 {
16310 unsigned int insn1, insn2;
16311
16312 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16313 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16314 if ((insn1 & ~0xfffc) == LD_R2_0R12
16315 && insn2 == ADD_R2_R2_R12)
16316 {
16317 bfd_put_32 (input_bfd,
16318 ADDIS_R2_R12 + PPC_HA (relocation),
16319 contents + rel->r_offset);
16320 bfd_put_32 (input_bfd,
16321 ADDI_R2_R2 + PPC_LO (relocation),
16322 contents + rel->r_offset + 4);
16323 }
16324 }
16325 }
16326 break;
16327
16328 case R_PPC64_REL16_HA:
16329 /* If we are generating a non-PIC executable, edit
16330 . 0: addis 2,12,.TOC.-0b@ha
16331 . addi 2,2,.TOC.-0b@l
16332 used by ELFv2 global entry points to set up r2, to
16333 . lis 2,.TOC.@ha
16334 . addi 2,2,.TOC.@l
16335 if .TOC. is in range. */
16336 if (!bfd_link_pic (info)
16337 && !info->traditional_format
16338 && !htab->opd_abi
16339 && rel->r_addend == d_offset
16340 && h != NULL && &h->elf == htab->elf.hgot
16341 && rel + 1 < relend
16342 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16343 && rel[1].r_offset == rel->r_offset + 4
16344 && rel[1].r_addend == rel->r_addend + 4
16345 && relocation + 0x80008000 <= 0xffffffff
16346 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16347 {
16348 unsigned int insn1, insn2;
16349 offset = rel->r_offset - d_offset;
16350 insn1 = bfd_get_32 (input_bfd, contents + offset);
16351 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16352 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16353 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16354 {
16355 r_type = R_PPC64_ADDR16_HA;
16356 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16357 rel->r_addend -= d_offset;
16358 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16359 rel[1].r_addend -= d_offset + 4;
16360 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16361 }
16362 }
16363 break;
16364 }
16365
16366 /* Handle other relocations that tweak non-addend part of insn. */
16367 insn = 0;
16368 max_br_offset = 1 << 25;
16369 addend = rel->r_addend;
16370 reloc_dest = DEST_NORMAL;
16371 switch (r_type)
16372 {
16373 default:
16374 break;
16375
16376 case R_PPC64_TOCSAVE:
16377 if (relocation + addend == (rel->r_offset
16378 + input_section->output_offset
16379 + input_section->output_section->vma)
16380 && tocsave_find (htab, NO_INSERT,
16381 &local_syms, rel, input_bfd)
16382 && offset_in_range (input_section, rel->r_offset, 4))
16383 {
16384 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16385 if (insn == NOP
16386 || insn == CROR_151515 || insn == CROR_313131)
16387 bfd_put_32 (input_bfd,
16388 STD_R2_0R1 + STK_TOC (htab),
16389 contents + rel->r_offset);
16390 }
16391 break;
16392
16393 /* Branch taken prediction relocations. */
16394 case R_PPC64_ADDR14_BRTAKEN:
16395 case R_PPC64_REL14_BRTAKEN:
16396 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16397 /* Fall through. */
16398
16399 /* Branch not taken prediction relocations. */
16400 case R_PPC64_ADDR14_BRNTAKEN:
16401 case R_PPC64_REL14_BRNTAKEN:
16402 if (!offset_in_range (input_section, rel->r_offset, 4))
16403 break;
16404 insn |= bfd_get_32 (input_bfd,
16405 contents + rel->r_offset) & ~(0x01 << 21);
16406 /* Fall through. */
16407
16408 case R_PPC64_REL14:
16409 max_br_offset = 1 << 15;
16410 /* Fall through. */
16411
16412 case R_PPC64_REL24:
16413 case R_PPC64_REL24_NOTOC:
16414 case R_PPC64_REL24_P9NOTOC:
16415 case R_PPC64_PLTCALL:
16416 case R_PPC64_PLTCALL_NOTOC:
16417 /* Calls to functions with a different TOC, such as calls to
16418 shared objects, need to alter the TOC pointer. This is
16419 done using a linkage stub. A REL24 branching to these
16420 linkage stubs needs to be followed by a nop, as the nop
16421 will be replaced with an instruction to restore the TOC
16422 base pointer. */
16423 fdh = h;
16424 if (h != NULL
16425 && h->oh != NULL
16426 && h->oh->is_func_descriptor)
16427 fdh = ppc_follow_link (h->oh);
16428 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16429 htab);
16430 if ((r_type == R_PPC64_PLTCALL
16431 || r_type == R_PPC64_PLTCALL_NOTOC)
16432 && stub_entry != NULL
16433 && stub_entry->type.main == ppc_stub_plt_call)
16434 stub_entry = NULL;
16435
16436 if (stub_entry != NULL
16437 && (stub_entry->type.main == ppc_stub_plt_call
16438 || stub_entry->type.r2save))
16439 {
16440 bool can_plt_call = false;
16441
16442 if (r_type == R_PPC64_REL24_NOTOC
16443 || r_type == R_PPC64_REL24_P9NOTOC)
16444 {
16445 /* NOTOC calls don't need to restore r2. */
16446 can_plt_call = true;
16447 }
16448 else if (stub_entry->type.main == ppc_stub_plt_call
16449 && !htab->opd_abi
16450 && htab->params->plt_localentry0 != 0
16451 && h != NULL
16452 && is_elfv2_localentry0 (&h->elf))
16453 {
16454 /* The function doesn't use or change r2. */
16455 can_plt_call = true;
16456 }
16457
16458 /* All of these stubs may modify r2, so there must be a
16459 branch and link followed by a nop. The nop is
16460 replaced by an insn to restore r2. */
16461 else if (offset_in_range (input_section, rel->r_offset, 8))
16462 {
16463 unsigned long br;
16464
16465 br = bfd_get_32 (input_bfd,
16466 contents + rel->r_offset);
16467 if ((br & 1) != 0)
16468 {
16469 unsigned long nop;
16470
16471 nop = bfd_get_32 (input_bfd,
16472 contents + rel->r_offset + 4);
16473 if (nop == LD_R2_0R1 + STK_TOC (htab))
16474 can_plt_call = true;
16475 else if (nop == NOP
16476 || nop == CROR_151515
16477 || nop == CROR_313131)
16478 {
16479 if (h != NULL
16480 && is_tls_get_addr (&h->elf, htab)
16481 && htab->params->tls_get_addr_opt)
16482 {
16483 /* Special stub used, leave nop alone. */
16484 }
16485 else
16486 bfd_put_32 (input_bfd,
16487 LD_R2_0R1 + STK_TOC (htab),
16488 contents + rel->r_offset + 4);
16489 can_plt_call = true;
16490 }
16491 }
16492 }
16493
16494 if (!can_plt_call && h != NULL)
16495 {
16496 const char *name = h->elf.root.root.string;
16497
16498 if (*name == '.')
16499 ++name;
16500
16501 if (startswith (name, "__libc_start_main")
16502 && (name[17] == 0 || name[17] == '@'))
16503 {
16504 /* Allow crt1 branch to go via a toc adjusting
16505 stub. Other calls that never return could do
16506 the same, if we could detect such. */
16507 can_plt_call = true;
16508 }
16509 }
16510
16511 if (!can_plt_call)
16512 {
16513 /* g++ as of 20130507 emits self-calls without a
16514 following nop. This is arguably wrong since we
16515 have conflicting information. On the one hand a
16516 global symbol and on the other a local call
16517 sequence, but don't error for this special case.
16518 It isn't possible to cheaply verify we have
16519 exactly such a call. Allow all calls to the same
16520 section. */
16521 asection *code_sec = sec;
16522
16523 if (get_opd_info (sec) != NULL)
16524 {
16525 bfd_vma off = (relocation + addend
16526 - sec->output_section->vma
16527 - sec->output_offset);
16528
16529 opd_entry_value (sec, off, &code_sec, NULL, false);
16530 }
16531 if (code_sec == input_section)
16532 can_plt_call = true;
16533 }
16534
16535 if (!can_plt_call)
16536 {
16537 if (stub_entry->type.main == ppc_stub_plt_call)
16538 info->callbacks->einfo
16539 /* xgettext:c-format */
16540 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16541 "(plt call stub)\n"),
16542 input_bfd, input_section, rel->r_offset, sym_name);
16543 else
16544 info->callbacks->einfo
16545 /* xgettext:c-format */
16546 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16547 "(toc save/adjust stub)\n"),
16548 input_bfd, input_section, rel->r_offset, sym_name);
16549
16550 bfd_set_error (bfd_error_bad_value);
16551 ret = false;
16552 }
16553
16554 if (can_plt_call
16555 && stub_entry->type.main == ppc_stub_plt_call)
16556 unresolved_reloc = false;
16557 }
16558
16559 if ((stub_entry == NULL
16560 || stub_entry->type.main == ppc_stub_long_branch
16561 || stub_entry->type.main == ppc_stub_plt_branch)
16562 && get_opd_info (sec) != NULL)
16563 {
16564 /* The branch destination is the value of the opd entry. */
16565 bfd_vma off = (relocation + addend
16566 - sec->output_section->vma
16567 - sec->output_offset);
16568 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16569 if (dest != (bfd_vma) -1)
16570 {
16571 relocation = dest;
16572 addend = 0;
16573 reloc_dest = DEST_OPD;
16574 }
16575 }
16576
16577 /* If the branch is out of reach we ought to have a long
16578 branch stub. */
16579 from = (rel->r_offset
16580 + input_section->output_offset
16581 + input_section->output_section->vma);
16582
16583 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16584 ? fdh->elf.other
16585 : sym->st_other);
16586
16587 if (stub_entry != NULL
16588 && (stub_entry->type.main == ppc_stub_long_branch
16589 || stub_entry->type.main == ppc_stub_plt_branch))
16590 {
16591 if (stub_entry->type.sub == ppc_stub_toc
16592 && !stub_entry->type.r2save
16593 && (r_type == R_PPC64_ADDR14_BRTAKEN
16594 || r_type == R_PPC64_ADDR14_BRNTAKEN
16595 || (relocation + addend - from + max_br_offset
16596 < 2 * max_br_offset)))
16597 /* Don't use the stub if this branch is in range. */
16598 stub_entry = NULL;
16599
16600 if (stub_entry != NULL
16601 && stub_entry->type.sub >= ppc_stub_notoc
16602 && ((r_type != R_PPC64_REL24_NOTOC
16603 && r_type != R_PPC64_REL24_P9NOTOC)
16604 || ((fdh ? fdh->elf.other : sym->st_other)
16605 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16606 && (relocation + addend - from + max_br_offset
16607 < 2 * max_br_offset))
16608 stub_entry = NULL;
16609
16610 if (stub_entry != NULL
16611 && stub_entry->type.r2save
16612 && (r_type == R_PPC64_REL24_NOTOC
16613 || r_type == R_PPC64_REL24_P9NOTOC)
16614 && (relocation + addend - from + max_br_offset
16615 < 2 * max_br_offset))
16616 stub_entry = NULL;
16617 }
16618
16619 if (stub_entry != NULL)
16620 {
16621 /* Munge up the value and addend so that we call the stub
16622 rather than the procedure directly. */
16623 asection *stub_sec = stub_entry->group->stub_sec;
16624
16625 if (stub_entry->type.main == ppc_stub_save_res)
16626 relocation += (stub_sec->output_offset
16627 + stub_sec->output_section->vma
16628 + stub_sec->size - htab->sfpr->size
16629 - htab->sfpr->output_offset
16630 - htab->sfpr->output_section->vma);
16631 else
16632 relocation = (stub_entry->stub_offset
16633 + stub_sec->output_offset
16634 + stub_sec->output_section->vma);
16635 addend = 0;
16636 reloc_dest = DEST_STUB;
16637
16638 if (((stub_entry->type.r2save
16639 && (r_type == R_PPC64_REL24_NOTOC
16640 || r_type == R_PPC64_REL24_P9NOTOC))
16641 || ((stub_entry->type.main == ppc_stub_plt_call
16642 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16643 && rel + 1 < relend
16644 && rel[1].r_offset == rel->r_offset + 4
16645 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16646 && !(stub_entry->type.main == ppc_stub_plt_call
16647 && htab->params->tls_get_addr_opt
16648 && h != NULL
16649 && is_tls_get_addr (&h->elf, htab)))
16650 {
16651 /* Skip over the r2 store at the start of the stub. */
16652 relocation += 4;
16653 }
16654
16655 if ((r_type == R_PPC64_REL24_NOTOC
16656 || r_type == R_PPC64_REL24_P9NOTOC)
16657 && stub_entry->type.main == ppc_stub_plt_call
16658 && stub_entry->type.sub >= ppc_stub_notoc)
16659 htab->notoc_plt = 1;
16660 }
16661
16662 if (insn != 0)
16663 {
16664 if (is_isa_v2)
16665 {
16666 /* Set 'a' bit. This is 0b00010 in BO field for branch
16667 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16668 for branch on CTR insns (BO == 1a00t or 1a01t). */
16669 if ((insn & (0x14 << 21)) == (0x04 << 21))
16670 insn |= 0x02 << 21;
16671 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16672 insn |= 0x08 << 21;
16673 else
16674 break;
16675 }
16676 else
16677 {
16678 /* Invert 'y' bit if not the default. */
16679 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16680 insn ^= 0x01 << 21;
16681 }
16682
16683 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16684 }
16685
16686 /* NOP out calls to undefined weak functions.
16687 We can thus call a weak function without first
16688 checking whether the function is defined. */
16689 else if (h != NULL
16690 && h->elf.root.type == bfd_link_hash_undefweak
16691 && h->elf.dynindx == -1
16692 && (r_type == R_PPC64_REL24
16693 || r_type == R_PPC64_REL24_NOTOC
16694 || r_type == R_PPC64_REL24_P9NOTOC)
16695 && relocation == 0
16696 && addend == 0
16697 && offset_in_range (input_section, rel->r_offset, 4))
16698 {
16699 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16700 goto copy_reloc;
16701 }
16702 break;
16703
16704 case R_PPC64_GOT16_DS:
16705 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16706 || (bfd_link_pic (info)
16707 && sec == bfd_abs_section_ptr)
16708 || !htab->do_toc_opt)
16709 break;
16710 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16711 if (relocation + addend - from + 0x8000 < 0x10000
16712 && sec != NULL
16713 && sec->output_section != NULL
16714 && !discarded_section (sec)
16715 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16716 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16717 {
16718 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16719 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16720 {
16721 insn += (14u << 26) - (58u << 26);
16722 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16723 r_type = R_PPC64_TOC16;
16724 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16725 }
16726 }
16727 break;
16728
16729 case R_PPC64_GOT16_LO_DS:
16730 case R_PPC64_GOT16_HA:
16731 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16732 || (bfd_link_pic (info)
16733 && sec == bfd_abs_section_ptr)
16734 || !htab->do_toc_opt)
16735 break;
16736 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16737 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16738 && sec != NULL
16739 && sec->output_section != NULL
16740 && !discarded_section (sec)
16741 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16742 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16743 {
16744 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16745 if (r_type == R_PPC64_GOT16_LO_DS
16746 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16747 {
16748 insn += (14u << 26) - (58u << 26);
16749 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16750 r_type = R_PPC64_TOC16_LO;
16751 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16752 }
16753 else if (r_type == R_PPC64_GOT16_HA
16754 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16755 {
16756 r_type = R_PPC64_TOC16_HA;
16757 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16758 }
16759 }
16760 break;
16761
16762 case R_PPC64_GOT_PCREL34:
16763 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16764 || (bfd_link_pic (info)
16765 && sec == bfd_abs_section_ptr)
16766 || !htab->do_toc_opt)
16767 break;
16768 from = (rel->r_offset
16769 + input_section->output_section->vma
16770 + input_section->output_offset);
16771 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16772 && sec != NULL
16773 && sec->output_section != NULL
16774 && !discarded_section (sec)
16775 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16776 && offset_in_range (input_section, rel->r_offset, 8)))
16777 break;
16778
16779 offset = rel->r_offset;
16780 pinsn = bfd_get_32 (input_bfd, contents + offset);
16781 pinsn <<= 32;
16782 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16783 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16784 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16785 break;
16786
16787 /* Replace with paddi. */
16788 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16789 r_type = R_PPC64_PCREL34;
16790 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16791 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16792 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16793 /* Fall through. */
16794
16795 case R_PPC64_PCREL34:
16796 if (!htab->params->no_pcrel_opt
16797 && rel + 1 < relend
16798 && rel[1].r_offset == rel->r_offset
16799 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16800 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16801 && offset_in_range (input_section, rel->r_offset, 8))
16802 {
16803 offset = rel->r_offset;
16804 pinsn = bfd_get_32 (input_bfd, contents + offset);
16805 pinsn <<= 32;
16806 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16807 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16808 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16809 | (14ULL << 26) /* paddi */))
16810 {
16811 bfd_vma off2 = rel[1].r_addend;
16812 if (off2 == 0)
16813 /* zero means next insn. */
16814 off2 = 8;
16815 off2 += offset;
16816 if (offset_in_range (input_section, off2, 4))
16817 {
16818 uint64_t pinsn2;
16819 bfd_signed_vma addend_off;
16820 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16821 pinsn2 <<= 32;
16822 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16823 {
16824 if (!offset_in_range (input_section, off2, 8))
16825 break;
16826 pinsn2 |= bfd_get_32 (input_bfd,
16827 contents + off2 + 4);
16828 }
16829 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16830 {
16831 addend += addend_off;
16832 rel->r_addend = addend;
16833 bfd_put_32 (input_bfd, pinsn >> 32,
16834 contents + offset);
16835 bfd_put_32 (input_bfd, pinsn,
16836 contents + offset + 4);
16837 bfd_put_32 (input_bfd, pinsn2 >> 32,
16838 contents + off2);
16839 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16840 bfd_put_32 (input_bfd, pinsn2,
16841 contents + off2 + 4);
16842 }
16843 }
16844 }
16845 }
16846 break;
16847 }
16848
16849 tls_type = 0;
16850 save_unresolved_reloc = unresolved_reloc;
16851 switch (r_type)
16852 {
16853 default:
16854 /* xgettext:c-format */
16855 _bfd_error_handler (_("%pB: %s unsupported"),
16856 input_bfd, ppc64_elf_howto_table[r_type]->name);
16857
16858 bfd_set_error (bfd_error_bad_value);
16859 ret = false;
16860 goto copy_reloc;
16861
16862 case R_PPC64_NONE:
16863 case R_PPC64_TLS:
16864 case R_PPC64_TLSGD:
16865 case R_PPC64_TLSLD:
16866 case R_PPC64_TOCSAVE:
16867 case R_PPC64_GNU_VTINHERIT:
16868 case R_PPC64_GNU_VTENTRY:
16869 case R_PPC64_ENTRY:
16870 case R_PPC64_PCREL_OPT:
16871 goto copy_reloc;
16872
16873 /* GOT16 relocations. Like an ADDR16 using the symbol's
16874 address in the GOT as relocation value instead of the
16875 symbol's value itself. Also, create a GOT entry for the
16876 symbol and put the symbol value there. */
16877 case R_PPC64_GOT_TLSGD16:
16878 case R_PPC64_GOT_TLSGD16_LO:
16879 case R_PPC64_GOT_TLSGD16_HI:
16880 case R_PPC64_GOT_TLSGD16_HA:
16881 case R_PPC64_GOT_TLSGD_PCREL34:
16882 tls_type = TLS_TLS | TLS_GD;
16883 goto dogot;
16884
16885 case R_PPC64_GOT_TLSLD16:
16886 case R_PPC64_GOT_TLSLD16_LO:
16887 case R_PPC64_GOT_TLSLD16_HI:
16888 case R_PPC64_GOT_TLSLD16_HA:
16889 case R_PPC64_GOT_TLSLD_PCREL34:
16890 tls_type = TLS_TLS | TLS_LD;
16891 goto dogot;
16892
16893 case R_PPC64_GOT_TPREL16_DS:
16894 case R_PPC64_GOT_TPREL16_LO_DS:
16895 case R_PPC64_GOT_TPREL16_HI:
16896 case R_PPC64_GOT_TPREL16_HA:
16897 case R_PPC64_GOT_TPREL_PCREL34:
16898 tls_type = TLS_TLS | TLS_TPREL;
16899 goto dogot;
16900
16901 case R_PPC64_GOT_DTPREL16_DS:
16902 case R_PPC64_GOT_DTPREL16_LO_DS:
16903 case R_PPC64_GOT_DTPREL16_HI:
16904 case R_PPC64_GOT_DTPREL16_HA:
16905 case R_PPC64_GOT_DTPREL_PCREL34:
16906 tls_type = TLS_TLS | TLS_DTPREL;
16907 goto dogot;
16908
16909 case R_PPC64_GOT16:
16910 case R_PPC64_GOT16_LO:
16911 case R_PPC64_GOT16_HI:
16912 case R_PPC64_GOT16_HA:
16913 case R_PPC64_GOT16_DS:
16914 case R_PPC64_GOT16_LO_DS:
16915 case R_PPC64_GOT_PCREL34:
16916 dogot:
16917 {
16918 /* Relocation is to the entry for this symbol in the global
16919 offset table. */
16920 asection *got;
16921 bfd_vma *offp;
16922 bfd_vma off;
16923 unsigned long indx = 0;
16924 struct got_entry *ent;
16925
16926 if (tls_type == (TLS_TLS | TLS_LD)
16927 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16928 ent = ppc64_tlsld_got (input_bfd);
16929 else
16930 {
16931 if (h != NULL)
16932 {
16933 if (!htab->elf.dynamic_sections_created
16934 || h->elf.dynindx == -1
16935 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16936 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16937 /* This is actually a static link, or it is a
16938 -Bsymbolic link and the symbol is defined
16939 locally, or the symbol was forced to be local
16940 because of a version file. */
16941 ;
16942 else
16943 {
16944 indx = h->elf.dynindx;
16945 unresolved_reloc = false;
16946 }
16947 ent = h->elf.got.glist;
16948 }
16949 else
16950 {
16951 if (local_got_ents == NULL)
16952 abort ();
16953 ent = local_got_ents[r_symndx];
16954 }
16955
16956 for (; ent != NULL; ent = ent->next)
16957 if (ent->addend == orig_rel.r_addend
16958 && ent->owner == input_bfd
16959 && ent->tls_type == tls_type)
16960 break;
16961 }
16962
16963 if (ent == NULL)
16964 abort ();
16965 if (ent->is_indirect)
16966 ent = ent->got.ent;
16967 offp = &ent->got.offset;
16968 got = ppc64_elf_tdata (ent->owner)->got;
16969 if (got == NULL)
16970 abort ();
16971
16972 /* The offset must always be a multiple of 8. We use the
16973 least significant bit to record whether we have already
16974 processed this entry. */
16975 off = *offp;
16976 if ((off & 1) != 0)
16977 off &= ~1;
16978 else
16979 {
16980 /* Generate relocs for the dynamic linker, except in
16981 the case of TLSLD where we'll use one entry per
16982 module. */
16983 asection *relgot;
16984 bool ifunc;
16985
16986 *offp = off | 1;
16987 relgot = NULL;
16988 ifunc = (h != NULL
16989 ? h->elf.type == STT_GNU_IFUNC
16990 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16991 if (ifunc)
16992 {
16993 relgot = htab->elf.irelplt;
16994 if (indx == 0 || is_static_defined (&h->elf))
16995 htab->elf.ifunc_resolvers = true;
16996 }
16997 else if (indx != 0
16998 || (bfd_link_pic (info)
16999 && (h == NULL
17000 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
17001 && !(tls_type != 0
17002 && bfd_link_executable (info)
17003 && (h == NULL
17004 || SYMBOL_REFERENCES_LOCAL (info,
17005 &h->elf)))
17006 && (h != NULL
17007 ? !bfd_is_abs_symbol (&h->elf.root)
17008 : sym->st_shndx != SHN_ABS)))
17009
17010 relgot = ppc64_elf_tdata (ent->owner)->relgot;
17011 if (relgot != NULL)
17012 {
17013 outrel.r_offset = (got->output_section->vma
17014 + got->output_offset
17015 + off);
17016 outrel.r_addend = orig_rel.r_addend;
17017 if (tls_type & (TLS_LD | TLS_GD))
17018 {
17019 outrel.r_addend = 0;
17020 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
17021 if (tls_type == (TLS_TLS | TLS_GD))
17022 {
17023 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17024 &outrel,
17025 relgot));
17026 outrel.r_offset += 8;
17027 outrel.r_addend = orig_rel.r_addend;
17028 outrel.r_info
17029 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17030 }
17031 }
17032 else if (tls_type == (TLS_TLS | TLS_DTPREL))
17033 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17034 else if (tls_type == (TLS_TLS | TLS_TPREL))
17035 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
17036 else if (indx != 0)
17037 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17038 else
17039 {
17040 if (ifunc)
17041 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17042 else
17043 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17044
17045 /* Write the .got section contents for the sake
17046 of prelink. */
17047 loc = got->contents + off;
17048 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17049 loc);
17050 }
17051
17052 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
17053 {
17054 outrel.r_addend += relocation;
17055 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
17056 {
17057 if (htab->elf.tls_sec == NULL)
17058 outrel.r_addend = 0;
17059 else
17060 outrel.r_addend -= htab->elf.tls_sec->vma;
17061 }
17062 }
17063 if (!(info->enable_dt_relr
17064 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17065 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17066 &outrel, relgot));
17067 }
17068
17069 /* Init the .got section contents here if we're not
17070 emitting a reloc. */
17071 else
17072 {
17073 relocation += orig_rel.r_addend;
17074 if (tls_type != 0)
17075 {
17076 if (htab->elf.tls_sec == NULL)
17077 relocation = 0;
17078 else
17079 {
17080 if (tls_type & TLS_LD)
17081 relocation = 0;
17082 else
17083 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17084 if (tls_type & TLS_TPREL)
17085 relocation += DTP_OFFSET - TP_OFFSET;
17086 }
17087
17088 if (tls_type & (TLS_GD | TLS_LD))
17089 {
17090 bfd_put_64 (output_bfd, relocation,
17091 got->contents + off + 8);
17092 relocation = 1;
17093 }
17094 }
17095 bfd_put_64 (output_bfd, relocation,
17096 got->contents + off);
17097 }
17098 }
17099
17100 if (off >= (bfd_vma) -2)
17101 abort ();
17102
17103 relocation = got->output_section->vma + got->output_offset + off;
17104 addend = 0;
17105 if (!(r_type == R_PPC64_GOT_PCREL34
17106 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17107 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17108 || r_type == R_PPC64_GOT_TPREL_PCREL34
17109 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17110 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17111 }
17112 break;
17113
17114 case R_PPC64_PLT16_HA:
17115 case R_PPC64_PLT16_HI:
17116 case R_PPC64_PLT16_LO:
17117 case R_PPC64_PLT16_LO_DS:
17118 case R_PPC64_PLT_PCREL34:
17119 case R_PPC64_PLT_PCREL34_NOTOC:
17120 case R_PPC64_PLT32:
17121 case R_PPC64_PLT64:
17122 case R_PPC64_PLTSEQ:
17123 case R_PPC64_PLTSEQ_NOTOC:
17124 case R_PPC64_PLTCALL:
17125 case R_PPC64_PLTCALL_NOTOC:
17126 /* Relocation is to the entry for this symbol in the
17127 procedure linkage table. */
17128 unresolved_reloc = true;
17129 {
17130 struct plt_entry **plt_list = NULL;
17131 if (h != NULL)
17132 plt_list = &h->elf.plt.plist;
17133 else if (local_got_ents != NULL)
17134 {
17135 struct plt_entry **local_plt = (struct plt_entry **)
17136 (local_got_ents + symtab_hdr->sh_info);
17137 plt_list = local_plt + r_symndx;
17138 }
17139 if (plt_list)
17140 {
17141 struct plt_entry *ent;
17142
17143 for (ent = *plt_list; ent != NULL; ent = ent->next)
17144 if (ent->plt.offset != (bfd_vma) -1
17145 && ent->addend == orig_rel.r_addend)
17146 {
17147 asection *plt;
17148 bfd_vma got;
17149
17150 plt = htab->elf.splt;
17151 if (use_local_plt (info, elf_hash_entry (h)))
17152 {
17153 if (h != NULL
17154 ? h->elf.type == STT_GNU_IFUNC
17155 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17156 plt = htab->elf.iplt;
17157 else
17158 plt = htab->pltlocal;
17159 }
17160 relocation = (plt->output_section->vma
17161 + plt->output_offset
17162 + ent->plt.offset);
17163 if (r_type == R_PPC64_PLT16_HA
17164 || r_type == R_PPC64_PLT16_HI
17165 || r_type == R_PPC64_PLT16_LO
17166 || r_type == R_PPC64_PLT16_LO_DS)
17167 {
17168 got = (elf_gp (output_bfd)
17169 + htab->sec_info[input_section->id].toc_off);
17170 relocation -= got;
17171 }
17172 addend = 0;
17173 unresolved_reloc = false;
17174 break;
17175 }
17176 }
17177 }
17178 break;
17179
17180 case R_PPC64_TOC:
17181 /* Relocation value is TOC base. */
17182 relocation = TOCstart;
17183 if (r_symndx == STN_UNDEF)
17184 relocation += htab->sec_info[input_section->id].toc_off;
17185 else if (unresolved_reloc)
17186 ;
17187 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17188 relocation += htab->sec_info[sec->id].toc_off;
17189 else
17190 unresolved_reloc = true;
17191 if (unresolved_reloc
17192 || (!is_opd
17193 && h != NULL
17194 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17195 info->callbacks->einfo
17196 /* xgettext:c-format */
17197 (_("%H: %s against %pT is not supported\n"),
17198 input_bfd, input_section, rel->r_offset,
17199 ppc64_elf_howto_table[r_type]->name, sym_name);
17200 goto dodyn;
17201
17202 /* TOC16 relocs. We want the offset relative to the TOC base,
17203 which is the address of the start of the TOC plus 0x8000.
17204 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17205 in this order. */
17206 case R_PPC64_TOC16:
17207 case R_PPC64_TOC16_LO:
17208 case R_PPC64_TOC16_HI:
17209 case R_PPC64_TOC16_DS:
17210 case R_PPC64_TOC16_LO_DS:
17211 case R_PPC64_TOC16_HA:
17212 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17213 if (h != NULL)
17214 goto dodyn;
17215 break;
17216
17217 /* Relocate against the beginning of the section. */
17218 case R_PPC64_SECTOFF:
17219 case R_PPC64_SECTOFF_LO:
17220 case R_PPC64_SECTOFF_HI:
17221 case R_PPC64_SECTOFF_DS:
17222 case R_PPC64_SECTOFF_LO_DS:
17223 case R_PPC64_SECTOFF_HA:
17224 if (sec != NULL)
17225 addend -= sec->output_section->vma;
17226 break;
17227
17228 case R_PPC64_REL16:
17229 case R_PPC64_REL16_LO:
17230 case R_PPC64_REL16_HI:
17231 case R_PPC64_REL16_HA:
17232 case R_PPC64_REL16_HIGH:
17233 case R_PPC64_REL16_HIGHA:
17234 case R_PPC64_REL16_HIGHER:
17235 case R_PPC64_REL16_HIGHERA:
17236 case R_PPC64_REL16_HIGHEST:
17237 case R_PPC64_REL16_HIGHESTA:
17238 case R_PPC64_REL16_HIGHER34:
17239 case R_PPC64_REL16_HIGHERA34:
17240 case R_PPC64_REL16_HIGHEST34:
17241 case R_PPC64_REL16_HIGHESTA34:
17242 case R_PPC64_REL16DX_HA:
17243 case R_PPC64_REL14:
17244 case R_PPC64_REL14_BRNTAKEN:
17245 case R_PPC64_REL14_BRTAKEN:
17246 case R_PPC64_REL24:
17247 case R_PPC64_REL24_NOTOC:
17248 case R_PPC64_REL24_P9NOTOC:
17249 case R_PPC64_PCREL34:
17250 case R_PPC64_PCREL28:
17251 break;
17252
17253 case R_PPC64_TPREL16:
17254 case R_PPC64_TPREL16_LO:
17255 case R_PPC64_TPREL16_HI:
17256 case R_PPC64_TPREL16_HA:
17257 case R_PPC64_TPREL16_DS:
17258 case R_PPC64_TPREL16_LO_DS:
17259 case R_PPC64_TPREL16_HIGH:
17260 case R_PPC64_TPREL16_HIGHA:
17261 case R_PPC64_TPREL16_HIGHER:
17262 case R_PPC64_TPREL16_HIGHERA:
17263 case R_PPC64_TPREL16_HIGHEST:
17264 case R_PPC64_TPREL16_HIGHESTA:
17265 if (h != NULL
17266 && h->elf.root.type == bfd_link_hash_undefweak
17267 && h->elf.dynindx == -1
17268 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17269 {
17270 /* Make this relocation against an undefined weak symbol
17271 resolve to zero. This is really just a tweak, since
17272 code using weak externs ought to check that they are
17273 defined before using them. */
17274 bfd_byte *p = contents + rel->r_offset - d_offset;
17275
17276 insn = bfd_get_32 (input_bfd, p);
17277 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17278 if (insn != 0)
17279 bfd_put_32 (input_bfd, insn, p);
17280 break;
17281 }
17282 /* Fall through. */
17283
17284 case R_PPC64_TPREL34:
17285 if (htab->elf.tls_sec != NULL)
17286 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17287 /* The TPREL16 relocs shouldn't really be used in shared
17288 libs or with non-local symbols as that will result in
17289 DT_TEXTREL being set, but support them anyway. */
17290 goto dodyn;
17291
17292 case R_PPC64_DTPREL16:
17293 case R_PPC64_DTPREL16_LO:
17294 case R_PPC64_DTPREL16_HI:
17295 case R_PPC64_DTPREL16_HA:
17296 case R_PPC64_DTPREL16_DS:
17297 case R_PPC64_DTPREL16_LO_DS:
17298 case R_PPC64_DTPREL16_HIGH:
17299 case R_PPC64_DTPREL16_HIGHA:
17300 case R_PPC64_DTPREL16_HIGHER:
17301 case R_PPC64_DTPREL16_HIGHERA:
17302 case R_PPC64_DTPREL16_HIGHEST:
17303 case R_PPC64_DTPREL16_HIGHESTA:
17304 case R_PPC64_DTPREL34:
17305 if (htab->elf.tls_sec != NULL)
17306 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17307 break;
17308
17309 case R_PPC64_ADDR64_LOCAL:
17310 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17311 ? h->elf.other
17312 : sym->st_other);
17313 break;
17314
17315 case R_PPC64_DTPMOD64:
17316 relocation = 1;
17317 addend = 0;
17318 goto dodyn;
17319
17320 case R_PPC64_TPREL64:
17321 if (htab->elf.tls_sec != NULL)
17322 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17323 goto dodyn;
17324
17325 case R_PPC64_DTPREL64:
17326 if (htab->elf.tls_sec != NULL)
17327 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17328 /* Fall through. */
17329
17330 /* Relocations that may need to be propagated if this is a
17331 dynamic object. */
17332 case R_PPC64_REL30:
17333 case R_PPC64_REL32:
17334 case R_PPC64_REL64:
17335 case R_PPC64_ADDR14:
17336 case R_PPC64_ADDR14_BRNTAKEN:
17337 case R_PPC64_ADDR14_BRTAKEN:
17338 case R_PPC64_ADDR16:
17339 case R_PPC64_ADDR16_DS:
17340 case R_PPC64_ADDR16_HA:
17341 case R_PPC64_ADDR16_HI:
17342 case R_PPC64_ADDR16_HIGH:
17343 case R_PPC64_ADDR16_HIGHA:
17344 case R_PPC64_ADDR16_HIGHER:
17345 case R_PPC64_ADDR16_HIGHERA:
17346 case R_PPC64_ADDR16_HIGHEST:
17347 case R_PPC64_ADDR16_HIGHESTA:
17348 case R_PPC64_ADDR16_LO:
17349 case R_PPC64_ADDR16_LO_DS:
17350 case R_PPC64_ADDR16_HIGHER34:
17351 case R_PPC64_ADDR16_HIGHERA34:
17352 case R_PPC64_ADDR16_HIGHEST34:
17353 case R_PPC64_ADDR16_HIGHESTA34:
17354 case R_PPC64_ADDR24:
17355 case R_PPC64_ADDR32:
17356 case R_PPC64_ADDR64:
17357 case R_PPC64_UADDR16:
17358 case R_PPC64_UADDR32:
17359 case R_PPC64_UADDR64:
17360 case R_PPC64_D34:
17361 case R_PPC64_D34_LO:
17362 case R_PPC64_D34_HI30:
17363 case R_PPC64_D34_HA30:
17364 case R_PPC64_D28:
17365 dodyn:
17366 if ((input_section->flags & SEC_ALLOC) == 0)
17367 break;
17368
17369 if (NO_OPD_RELOCS && is_opd)
17370 break;
17371
17372 if (bfd_link_pic (info)
17373 ? ((h == NULL
17374 || h->elf.dyn_relocs != NULL)
17375 && ((h != NULL && pc_dynrelocs (h))
17376 || must_be_dyn_reloc (info, r_type)))
17377 : (h != NULL
17378 ? h->elf.dyn_relocs != NULL
17379 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17380 {
17381 bool skip, relocate;
17382 asection *sreloc;
17383 bfd_vma out_off;
17384 long indx = 0;
17385
17386 /* When generating a dynamic object, these relocations
17387 are copied into the output file to be resolved at run
17388 time. */
17389
17390 skip = false;
17391 relocate = false;
17392
17393 out_off = _bfd_elf_section_offset (output_bfd, info,
17394 input_section, rel->r_offset);
17395 if (out_off == (bfd_vma) -1)
17396 skip = true;
17397 else if (out_off == (bfd_vma) -2)
17398 skip = true, relocate = true;
17399 out_off += (input_section->output_section->vma
17400 + input_section->output_offset);
17401 outrel.r_offset = out_off;
17402 outrel.r_addend = rel->r_addend;
17403
17404 /* Optimize unaligned reloc use. */
17405 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17406 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17407 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17408 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17409 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17410 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17411 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17412 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17413 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17414
17415 if (skip)
17416 memset (&outrel, 0, sizeof outrel);
17417 else if (h != NULL
17418 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17419 && !is_opd
17420 && r_type != R_PPC64_TOC)
17421 {
17422 indx = h->elf.dynindx;
17423 BFD_ASSERT (indx != -1);
17424 outrel.r_info = ELF64_R_INFO (indx, r_type);
17425 }
17426 else
17427 {
17428 /* This symbol is local, or marked to become local,
17429 or this is an opd section reloc which must point
17430 at a local function. */
17431 outrel.r_addend += relocation;
17432 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17433 {
17434 if (is_opd && h != NULL)
17435 {
17436 /* Lie about opd entries. This case occurs
17437 when building shared libraries and we
17438 reference a function in another shared
17439 lib. The same thing happens for a weak
17440 definition in an application that's
17441 overridden by a strong definition in a
17442 shared lib. (I believe this is a generic
17443 bug in binutils handling of weak syms.)
17444 In these cases we won't use the opd
17445 entry in this lib. */
17446 unresolved_reloc = false;
17447 }
17448 if (!is_opd
17449 && r_type == R_PPC64_ADDR64
17450 && (h != NULL
17451 ? h->elf.type == STT_GNU_IFUNC
17452 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17453 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17454 else
17455 {
17456 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17457
17458 /* We need to relocate .opd contents for ld.so.
17459 Prelink also wants simple and consistent rules
17460 for relocs. This make all RELATIVE relocs have
17461 *r_offset equal to r_addend. */
17462 relocate = true;
17463 }
17464 }
17465 else
17466 {
17467 if (h != NULL
17468 ? h->elf.type == STT_GNU_IFUNC
17469 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17470 {
17471 info->callbacks->einfo
17472 /* xgettext:c-format */
17473 (_("%H: %s for indirect "
17474 "function `%pT' unsupported\n"),
17475 input_bfd, input_section, rel->r_offset,
17476 ppc64_elf_howto_table[r_type]->name,
17477 sym_name);
17478 ret = false;
17479 }
17480 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17481 ;
17482 else if (sec == NULL || sec->owner == NULL)
17483 {
17484 bfd_set_error (bfd_error_bad_value);
17485 return false;
17486 }
17487 else
17488 {
17489 asection *osec = sec->output_section;
17490
17491 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17492 {
17493 /* TLS symbol values are relative to the
17494 TLS segment. Dynamic relocations for
17495 local TLS symbols therefore can't be
17496 reduced to a relocation against their
17497 section symbol because it holds the
17498 address of the section, not a value
17499 relative to the TLS segment. We could
17500 change the .tdata dynamic section symbol
17501 to be zero value but STN_UNDEF works
17502 and is used elsewhere, eg. for TPREL64
17503 GOT relocs against local TLS symbols. */
17504 osec = htab->elf.tls_sec;
17505 indx = 0;
17506 }
17507 else
17508 {
17509 indx = elf_section_data (osec)->dynindx;
17510 if (indx == 0)
17511 {
17512 if ((osec->flags & SEC_READONLY) == 0
17513 && htab->elf.data_index_section != NULL)
17514 osec = htab->elf.data_index_section;
17515 else
17516 osec = htab->elf.text_index_section;
17517 indx = elf_section_data (osec)->dynindx;
17518 }
17519 BFD_ASSERT (indx != 0);
17520 }
17521
17522 /* We are turning this relocation into one
17523 against a section symbol, so subtract out
17524 the output section's address but not the
17525 offset of the input section in the output
17526 section. */
17527 outrel.r_addend -= osec->vma;
17528 }
17529
17530 outrel.r_info = ELF64_R_INFO (indx, r_type);
17531 }
17532 }
17533
17534 if (!(info->enable_dt_relr
17535 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17536 && maybe_relr (ELF64_R_TYPE (orig_rel.r_info),
17537 rel, input_section)))
17538 {
17539 sreloc = elf_section_data (input_section)->sreloc;
17540 if (h != NULL
17541 ? h->elf.type == STT_GNU_IFUNC
17542 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17543 {
17544 sreloc = htab->elf.irelplt;
17545 if (indx == 0 || is_static_defined (&h->elf))
17546 htab->elf.ifunc_resolvers = true;
17547 }
17548 if (sreloc == NULL)
17549 abort ();
17550
17551 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &outrel,
17552 sreloc));
17553 }
17554
17555 if (!warned_dynamic
17556 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17557 {
17558 info->callbacks->einfo
17559 /* xgettext:c-format */
17560 (_("%X%P: %pB: %s against %pT "
17561 "is not supported by glibc as a dynamic relocation\n"),
17562 input_bfd,
17563 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17564 sym_name);
17565 warned_dynamic = true;
17566 }
17567
17568 /* If this reloc is against an external symbol, it will
17569 be computed at runtime, so there's no need to do
17570 anything now. However, for the sake of prelink ensure
17571 that the section contents are a known value. */
17572 if (!relocate)
17573 {
17574 unresolved_reloc = false;
17575 /* The value chosen here is quite arbitrary as ld.so
17576 ignores section contents except for the special
17577 case of .opd where the contents might be accessed
17578 before relocation. Choose zero, as that won't
17579 cause reloc overflow. */
17580 relocation = 0;
17581 addend = 0;
17582 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17583 to improve backward compatibility with older
17584 versions of ld. */
17585 if (r_type == R_PPC64_ADDR64)
17586 addend = outrel.r_addend;
17587 /* Adjust pc_relative relocs to have zero in *r_offset. */
17588 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17589 addend = outrel.r_offset;
17590 }
17591 }
17592 break;
17593
17594 case R_PPC64_COPY:
17595 case R_PPC64_GLOB_DAT:
17596 case R_PPC64_JMP_SLOT:
17597 case R_PPC64_JMP_IREL:
17598 case R_PPC64_RELATIVE:
17599 /* We shouldn't ever see these dynamic relocs in relocatable
17600 files. */
17601 /* Fall through. */
17602
17603 case R_PPC64_PLTGOT16:
17604 case R_PPC64_PLTGOT16_DS:
17605 case R_PPC64_PLTGOT16_HA:
17606 case R_PPC64_PLTGOT16_HI:
17607 case R_PPC64_PLTGOT16_LO:
17608 case R_PPC64_PLTGOT16_LO_DS:
17609 case R_PPC64_PLTREL32:
17610 case R_PPC64_PLTREL64:
17611 /* These ones haven't been implemented yet. */
17612
17613 info->callbacks->einfo
17614 /* xgettext:c-format */
17615 (_("%P: %pB: %s is not supported for `%pT'\n"),
17616 input_bfd,
17617 ppc64_elf_howto_table[r_type]->name, sym_name);
17618
17619 bfd_set_error (bfd_error_invalid_operation);
17620 ret = false;
17621 goto copy_reloc;
17622 }
17623
17624 /* Multi-instruction sequences that access the TOC can be
17625 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17626 to nop; addi rb,r2,x; */
17627 switch (r_type)
17628 {
17629 default:
17630 break;
17631
17632 case R_PPC64_GOT_TLSLD16_HI:
17633 case R_PPC64_GOT_TLSGD16_HI:
17634 case R_PPC64_GOT_TPREL16_HI:
17635 case R_PPC64_GOT_DTPREL16_HI:
17636 case R_PPC64_GOT16_HI:
17637 case R_PPC64_TOC16_HI:
17638 /* These relocs would only be useful if building up an
17639 offset to later add to r2, perhaps in an indexed
17640 addressing mode instruction. Don't try to optimize.
17641 Unfortunately, the possibility of someone building up an
17642 offset like this or even with the HA relocs, means that
17643 we need to check the high insn when optimizing the low
17644 insn. */
17645 break;
17646
17647 case R_PPC64_PLTCALL_NOTOC:
17648 if (!unresolved_reloc)
17649 htab->notoc_plt = 1;
17650 /* Fall through. */
17651 case R_PPC64_PLTCALL:
17652 if (unresolved_reloc
17653 && offset_in_range (input_section, rel->r_offset,
17654 r_type == R_PPC64_PLTCALL ? 8 : 4))
17655 {
17656 /* No plt entry. Make this into a direct call. */
17657 bfd_byte *p = contents + rel->r_offset;
17658 insn = bfd_get_32 (input_bfd, p);
17659 insn &= 1;
17660 bfd_put_32 (input_bfd, B_DOT | insn, p);
17661 if (r_type == R_PPC64_PLTCALL)
17662 bfd_put_32 (input_bfd, NOP, p + 4);
17663 unresolved_reloc = save_unresolved_reloc;
17664 r_type = R_PPC64_REL24;
17665 }
17666 break;
17667
17668 case R_PPC64_PLTSEQ_NOTOC:
17669 case R_PPC64_PLTSEQ:
17670 if (unresolved_reloc)
17671 {
17672 unresolved_reloc = false;
17673 goto nop_it;
17674 }
17675 break;
17676
17677 case R_PPC64_PLT_PCREL34_NOTOC:
17678 if (!unresolved_reloc)
17679 htab->notoc_plt = 1;
17680 /* Fall through. */
17681 case R_PPC64_PLT_PCREL34:
17682 if (unresolved_reloc
17683 && offset_in_range (input_section, rel->r_offset, 8))
17684 {
17685 bfd_byte *p = contents + rel->r_offset;
17686 bfd_put_32 (input_bfd, PNOP >> 32, p);
17687 bfd_put_32 (input_bfd, PNOP, p + 4);
17688 unresolved_reloc = false;
17689 goto copy_reloc;
17690 }
17691 break;
17692
17693 case R_PPC64_PLT16_HA:
17694 if (unresolved_reloc)
17695 {
17696 unresolved_reloc = false;
17697 goto nop_it;
17698 }
17699 /* Fall through. */
17700 case R_PPC64_GOT_TLSLD16_HA:
17701 case R_PPC64_GOT_TLSGD16_HA:
17702 case R_PPC64_GOT_TPREL16_HA:
17703 case R_PPC64_GOT_DTPREL16_HA:
17704 case R_PPC64_GOT16_HA:
17705 case R_PPC64_TOC16_HA:
17706 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17707 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17708 && !(bfd_link_pic (info)
17709 && (h != NULL
17710 ? bfd_is_abs_symbol (&h->elf.root)
17711 : sec == bfd_abs_section_ptr)))
17712 {
17713 bfd_byte *p;
17714 nop_it:
17715 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17716 {
17717 p = contents + (rel->r_offset & ~3);
17718 bfd_put_32 (input_bfd, NOP, p);
17719 goto copy_reloc;
17720 }
17721 }
17722 break;
17723
17724 case R_PPC64_PLT16_LO:
17725 case R_PPC64_PLT16_LO_DS:
17726 if (unresolved_reloc)
17727 {
17728 unresolved_reloc = false;
17729 goto nop_it;
17730 }
17731 /* Fall through. */
17732 case R_PPC64_GOT_TLSLD16_LO:
17733 case R_PPC64_GOT_TLSGD16_LO:
17734 case R_PPC64_GOT_TPREL16_LO_DS:
17735 case R_PPC64_GOT_DTPREL16_LO_DS:
17736 case R_PPC64_GOT16_LO:
17737 case R_PPC64_GOT16_LO_DS:
17738 case R_PPC64_TOC16_LO:
17739 case R_PPC64_TOC16_LO_DS:
17740 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17741 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17742 && !(bfd_link_pic (info)
17743 && (h != NULL
17744 ? bfd_is_abs_symbol (&h->elf.root)
17745 : sec == bfd_abs_section_ptr))
17746 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17747 {
17748 bfd_byte *p = contents + (rel->r_offset & ~3);
17749 insn = bfd_get_32 (input_bfd, p);
17750 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17751 {
17752 /* Transform addic to addi when we change reg. */
17753 insn &= ~((0x3fu << 26) | (0x1f << 16));
17754 insn |= (14u << 26) | (2 << 16);
17755 }
17756 else
17757 {
17758 insn &= ~(0x1f << 16);
17759 insn |= 2 << 16;
17760 }
17761 bfd_put_32 (input_bfd, insn, p);
17762 }
17763 break;
17764
17765 case R_PPC64_TPREL16_HA:
17766 if (htab->do_tls_opt
17767 && relocation + addend + 0x8000 < 0x10000
17768 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17769 {
17770 bfd_byte *p = contents + (rel->r_offset & ~3);
17771 bfd_put_32 (input_bfd, NOP, p);
17772 goto copy_reloc;
17773 }
17774 break;
17775
17776 case R_PPC64_TPREL16_LO:
17777 case R_PPC64_TPREL16_LO_DS:
17778 if (htab->do_tls_opt
17779 && relocation + addend + 0x8000 < 0x10000
17780 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17781 {
17782 bfd_byte *p = contents + (rel->r_offset & ~3);
17783 insn = bfd_get_32 (input_bfd, p);
17784 insn &= ~(0x1f << 16);
17785 insn |= 13 << 16;
17786 bfd_put_32 (input_bfd, insn, p);
17787 }
17788 break;
17789 }
17790
17791 /* Do any further special processing. */
17792 switch (r_type)
17793 {
17794 default:
17795 break;
17796
17797 case R_PPC64_REL16_HA:
17798 case R_PPC64_REL16_HIGHA:
17799 case R_PPC64_REL16_HIGHERA:
17800 case R_PPC64_REL16_HIGHESTA:
17801 case R_PPC64_REL16DX_HA:
17802 case R_PPC64_ADDR16_HA:
17803 case R_PPC64_ADDR16_HIGHA:
17804 case R_PPC64_ADDR16_HIGHERA:
17805 case R_PPC64_ADDR16_HIGHESTA:
17806 case R_PPC64_TOC16_HA:
17807 case R_PPC64_SECTOFF_HA:
17808 case R_PPC64_TPREL16_HA:
17809 case R_PPC64_TPREL16_HIGHA:
17810 case R_PPC64_TPREL16_HIGHERA:
17811 case R_PPC64_TPREL16_HIGHESTA:
17812 case R_PPC64_DTPREL16_HA:
17813 case R_PPC64_DTPREL16_HIGHA:
17814 case R_PPC64_DTPREL16_HIGHERA:
17815 case R_PPC64_DTPREL16_HIGHESTA:
17816 /* It's just possible that this symbol is a weak symbol
17817 that's not actually defined anywhere. In that case,
17818 'sec' would be NULL, and we should leave the symbol
17819 alone (it will be set to zero elsewhere in the link). */
17820 if (sec == NULL)
17821 break;
17822 /* Fall through. */
17823
17824 case R_PPC64_GOT16_HA:
17825 case R_PPC64_PLTGOT16_HA:
17826 case R_PPC64_PLT16_HA:
17827 case R_PPC64_GOT_TLSGD16_HA:
17828 case R_PPC64_GOT_TLSLD16_HA:
17829 case R_PPC64_GOT_TPREL16_HA:
17830 case R_PPC64_GOT_DTPREL16_HA:
17831 /* Add 0x10000 if sign bit in 0:15 is set.
17832 Bits 0:15 are not used. */
17833 addend += 0x8000;
17834 break;
17835
17836 case R_PPC64_D34_HA30:
17837 case R_PPC64_ADDR16_HIGHERA34:
17838 case R_PPC64_ADDR16_HIGHESTA34:
17839 case R_PPC64_REL16_HIGHERA34:
17840 case R_PPC64_REL16_HIGHESTA34:
17841 if (sec != NULL)
17842 addend += 1ULL << 33;
17843 break;
17844
17845 case R_PPC64_ADDR16_DS:
17846 case R_PPC64_ADDR16_LO_DS:
17847 case R_PPC64_GOT16_DS:
17848 case R_PPC64_GOT16_LO_DS:
17849 case R_PPC64_PLT16_LO_DS:
17850 case R_PPC64_SECTOFF_DS:
17851 case R_PPC64_SECTOFF_LO_DS:
17852 case R_PPC64_TOC16_DS:
17853 case R_PPC64_TOC16_LO_DS:
17854 case R_PPC64_PLTGOT16_DS:
17855 case R_PPC64_PLTGOT16_LO_DS:
17856 case R_PPC64_GOT_TPREL16_DS:
17857 case R_PPC64_GOT_TPREL16_LO_DS:
17858 case R_PPC64_GOT_DTPREL16_DS:
17859 case R_PPC64_GOT_DTPREL16_LO_DS:
17860 case R_PPC64_TPREL16_DS:
17861 case R_PPC64_TPREL16_LO_DS:
17862 case R_PPC64_DTPREL16_DS:
17863 case R_PPC64_DTPREL16_LO_DS:
17864 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17865 break;
17866 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17867 mask = 3;
17868 /* If this reloc is against an lq, lxv, or stxv insn, then
17869 the value must be a multiple of 16. This is somewhat of
17870 a hack, but the "correct" way to do this by defining _DQ
17871 forms of all the _DS relocs bloats all reloc switches in
17872 this file. It doesn't make much sense to use these
17873 relocs in data, so testing the insn should be safe. */
17874 if ((insn & (0x3fu << 26)) == (56u << 26)
17875 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17876 mask = 15;
17877 relocation += addend;
17878 addend = insn & (mask ^ 3);
17879 if ((relocation & mask) != 0)
17880 {
17881 relocation ^= relocation & mask;
17882 info->callbacks->einfo
17883 /* xgettext:c-format */
17884 (_("%H: error: %s not a multiple of %u\n"),
17885 input_bfd, input_section, rel->r_offset,
17886 ppc64_elf_howto_table[r_type]->name,
17887 mask + 1);
17888 bfd_set_error (bfd_error_bad_value);
17889 ret = false;
17890 goto copy_reloc;
17891 }
17892 break;
17893 }
17894
17895 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17896 because such sections are not SEC_ALLOC and thus ld.so will
17897 not process them. */
17898 howto = ppc64_elf_howto_table[(int) r_type];
17899 if (unresolved_reloc
17900 && !((input_section->flags & SEC_DEBUGGING) != 0
17901 && h->elf.def_dynamic)
17902 && _bfd_elf_section_offset (output_bfd, info, input_section,
17903 rel->r_offset) != (bfd_vma) -1)
17904 {
17905 info->callbacks->einfo
17906 /* xgettext:c-format */
17907 (_("%H: unresolvable %s against `%pT'\n"),
17908 input_bfd, input_section, rel->r_offset,
17909 howto->name,
17910 h->elf.root.root.string);
17911 ret = false;
17912 }
17913
17914 /* 16-bit fields in insns mostly have signed values, but a
17915 few insns have 16-bit unsigned values. Really, we should
17916 have different reloc types. */
17917 if (howto->complain_on_overflow != complain_overflow_dont
17918 && howto->dst_mask == 0xffff
17919 && (input_section->flags & SEC_CODE) != 0
17920 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17921 {
17922 enum complain_overflow complain = complain_overflow_signed;
17923
17924 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17925 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17926 complain = complain_overflow_bitfield;
17927 else if (howto->rightshift == 0
17928 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17929 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17930 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17931 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17932 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17933 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17934 complain = complain_overflow_unsigned;
17935 if (howto->complain_on_overflow != complain)
17936 {
17937 alt_howto = *howto;
17938 alt_howto.complain_on_overflow = complain;
17939 howto = &alt_howto;
17940 }
17941 }
17942
17943 switch (r_type)
17944 {
17945 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17946 case R_PPC64_D34:
17947 case R_PPC64_D34_LO:
17948 case R_PPC64_D34_HI30:
17949 case R_PPC64_D34_HA30:
17950 case R_PPC64_PCREL34:
17951 case R_PPC64_GOT_PCREL34:
17952 case R_PPC64_TPREL34:
17953 case R_PPC64_DTPREL34:
17954 case R_PPC64_GOT_TLSGD_PCREL34:
17955 case R_PPC64_GOT_TLSLD_PCREL34:
17956 case R_PPC64_GOT_TPREL_PCREL34:
17957 case R_PPC64_GOT_DTPREL_PCREL34:
17958 case R_PPC64_PLT_PCREL34:
17959 case R_PPC64_PLT_PCREL34_NOTOC:
17960 case R_PPC64_D28:
17961 case R_PPC64_PCREL28:
17962 if (!offset_in_range (input_section, rel->r_offset, 8))
17963 r = bfd_reloc_outofrange;
17964 else
17965 {
17966 relocation += addend;
17967 if (howto->pc_relative)
17968 relocation -= (rel->r_offset
17969 + input_section->output_offset
17970 + input_section->output_section->vma);
17971 relocation >>= howto->rightshift;
17972
17973 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17974 pinsn <<= 32;
17975 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17976
17977 pinsn &= ~howto->dst_mask;
17978 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17979 & howto->dst_mask);
17980 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17981 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17982 r = bfd_reloc_ok;
17983 if (howto->complain_on_overflow == complain_overflow_signed
17984 && (relocation + (1ULL << (howto->bitsize - 1))
17985 >= 1ULL << howto->bitsize))
17986 r = bfd_reloc_overflow;
17987 }
17988 break;
17989
17990 case R_PPC64_REL16DX_HA:
17991 if (!offset_in_range (input_section, rel->r_offset, 4))
17992 r = bfd_reloc_outofrange;
17993 else
17994 {
17995 relocation += addend;
17996 relocation -= (rel->r_offset
17997 + input_section->output_offset
17998 + input_section->output_section->vma);
17999 relocation = (bfd_signed_vma) relocation >> 16;
18000 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
18001 insn &= ~0x1fffc1;
18002 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
18003 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
18004 r = bfd_reloc_ok;
18005 if (relocation + 0x8000 > 0xffff)
18006 r = bfd_reloc_overflow;
18007 }
18008 break;
18009
18010 default:
18011 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
18012 contents, rel->r_offset,
18013 relocation, addend);
18014 }
18015
18016 if (r != bfd_reloc_ok)
18017 {
18018 char *more_info = NULL;
18019 const char *reloc_name = howto->name;
18020
18021 if (reloc_dest != DEST_NORMAL)
18022 {
18023 more_info = bfd_malloc (strlen (reloc_name) + 8);
18024 if (more_info != NULL)
18025 {
18026 strcpy (more_info, reloc_name);
18027 strcat (more_info, (reloc_dest == DEST_OPD
18028 ? " (OPD)" : " (stub)"));
18029 reloc_name = more_info;
18030 }
18031 }
18032
18033 if (r == bfd_reloc_overflow)
18034 {
18035 /* On code like "if (foo) foo();" don't report overflow
18036 on a branch to zero when foo is undefined. */
18037 if (!warned
18038 && (reloc_dest == DEST_STUB
18039 || !(h != NULL
18040 && (h->elf.root.type == bfd_link_hash_undefweak
18041 || h->elf.root.type == bfd_link_hash_undefined)
18042 && is_branch_reloc (r_type))))
18043 info->callbacks->reloc_overflow
18044 (info, (struct bfd_link_hash_entry *) h, sym_name,
18045 reloc_name, orig_rel.r_addend, input_bfd, input_section,
18046 rel->r_offset);
18047 }
18048 else
18049 {
18050 info->callbacks->einfo
18051 /* xgettext:c-format */
18052 (_("%H: %s against `%pT': error %d\n"),
18053 input_bfd, input_section, rel->r_offset,
18054 reloc_name, sym_name, (int) r);
18055 ret = false;
18056 }
18057 free (more_info);
18058 }
18059 copy_reloc:
18060 if (wrel != rel)
18061 *wrel = *rel;
18062 }
18063
18064 if (wrel != rel)
18065 {
18066 Elf_Internal_Shdr *rel_hdr;
18067 size_t deleted = rel - wrel;
18068
18069 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18070 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18071 if (rel_hdr->sh_size == 0)
18072 {
18073 /* It is too late to remove an empty reloc section. Leave
18074 one NONE reloc.
18075 ??? What is wrong with an empty section??? */
18076 rel_hdr->sh_size = rel_hdr->sh_entsize;
18077 deleted -= 1;
18078 }
18079 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18080 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18081 input_section->reloc_count -= deleted;
18082 }
18083
18084 /* If we're emitting relocations, then shortly after this function
18085 returns, reloc offsets and addends for this section will be
18086 adjusted. Worse, reloc symbol indices will be for the output
18087 file rather than the input. Save a copy of the relocs for
18088 opd_entry_value. */
18089 if (is_opd
18090 && (info->emitrelocations || bfd_link_relocatable (info))
18091 && input_section->reloc_count != 0)
18092 {
18093 bfd_size_type amt;
18094 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18095 rel = bfd_alloc (input_bfd, amt);
18096 ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
18097 if (rel == NULL)
18098 return false;
18099 memcpy (rel, relocs, amt);
18100 }
18101 return ret;
18102 }
18103
18104 /* Adjust the value of any local symbols in opd sections. */
18105
18106 static int
18107 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18108 const char *name ATTRIBUTE_UNUSED,
18109 Elf_Internal_Sym *elfsym,
18110 asection *input_sec,
18111 struct elf_link_hash_entry *h)
18112 {
18113 struct _opd_sec_data *opd;
18114 long adjust;
18115 bfd_vma value;
18116
18117 if (h != NULL)
18118 return 1;
18119
18120 opd = get_opd_info (input_sec);
18121 if (opd == NULL || opd->adjust == NULL)
18122 return 1;
18123
18124 value = elfsym->st_value - input_sec->output_offset;
18125 if (!bfd_link_relocatable (info))
18126 value -= input_sec->output_section->vma;
18127
18128 adjust = opd->adjust[OPD_NDX (value)];
18129 if (adjust == -1)
18130 return 2;
18131
18132 elfsym->st_value += adjust;
18133 return 1;
18134 }
18135
18136 /* Finish up dynamic symbol handling. We set the contents of various
18137 dynamic sections here. */
18138
18139 static bool
18140 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18141 struct bfd_link_info *info,
18142 struct elf_link_hash_entry *h,
18143 Elf_Internal_Sym *sym)
18144 {
18145 struct ppc_link_hash_table *htab;
18146 struct plt_entry *ent;
18147
18148 htab = ppc_hash_table (info);
18149
18150 if (!htab->opd_abi && !h->def_regular)
18151 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18152 if (ent->plt.offset != (bfd_vma) -1)
18153 {
18154 /* Mark the symbol as undefined, rather than as
18155 defined in glink. Leave the value if there were
18156 any relocations where pointer equality matters
18157 (this is a clue for the dynamic linker, to make
18158 function pointer comparisons work between an
18159 application and shared library), otherwise set it
18160 to zero. */
18161 sym->st_shndx = SHN_UNDEF;
18162 if (!h->pointer_equality_needed)
18163 sym->st_value = 0;
18164 else if (!h->ref_regular_nonweak)
18165 {
18166 /* This breaks function pointer comparisons, but
18167 that is better than breaking tests for a NULL
18168 function pointer. */
18169 sym->st_value = 0;
18170 }
18171 break;
18172 }
18173
18174 if (h->needs_copy
18175 && (h->root.type == bfd_link_hash_defined
18176 || h->root.type == bfd_link_hash_defweak)
18177 && (h->root.u.def.section == htab->elf.sdynbss
18178 || h->root.u.def.section == htab->elf.sdynrelro))
18179 {
18180 /* This symbol needs a copy reloc. Set it up. */
18181 Elf_Internal_Rela rela;
18182 asection *srel;
18183
18184 if (h->dynindx == -1)
18185 abort ();
18186
18187 rela.r_offset = defined_sym_val (h);
18188 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18189 rela.r_addend = 0;
18190 if (h->root.u.def.section == htab->elf.sdynrelro)
18191 srel = htab->elf.sreldynrelro;
18192 else
18193 srel = htab->elf.srelbss;
18194 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &rela, srel));
18195 }
18196
18197 return true;
18198 }
18199
18200 /* Used to decide how to sort relocs in an optimal manner for the
18201 dynamic linker, before writing them out. */
18202
18203 static enum elf_reloc_type_class
18204 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18205 const asection *rel_sec,
18206 const Elf_Internal_Rela *rela)
18207 {
18208 enum elf_ppc64_reloc_type r_type;
18209 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18210
18211 if (rel_sec == htab->elf.irelplt)
18212 return reloc_class_ifunc;
18213
18214 r_type = ELF64_R_TYPE (rela->r_info);
18215 switch (r_type)
18216 {
18217 case R_PPC64_RELATIVE:
18218 return reloc_class_relative;
18219 case R_PPC64_JMP_SLOT:
18220 return reloc_class_plt;
18221 case R_PPC64_COPY:
18222 return reloc_class_copy;
18223 default:
18224 return reloc_class_normal;
18225 }
18226 }
18227
18228 /* Finish up the dynamic sections. */
18229
18230 static bool
18231 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18232 struct bfd_link_info *info)
18233 {
18234 struct ppc_link_hash_table *htab;
18235 bfd *dynobj;
18236 asection *sdyn;
18237
18238 htab = ppc_hash_table (info);
18239 if (htab == NULL)
18240 return false;
18241
18242 dynobj = htab->elf.dynobj;
18243 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18244
18245 if (htab->elf.dynamic_sections_created)
18246 {
18247 Elf64_External_Dyn *dyncon, *dynconend;
18248
18249 if (sdyn == NULL || htab->elf.sgot == NULL)
18250 abort ();
18251
18252 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18253 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18254 for (; dyncon < dynconend; dyncon++)
18255 {
18256 Elf_Internal_Dyn dyn;
18257 asection *s;
18258
18259 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18260
18261 switch (dyn.d_tag)
18262 {
18263 default:
18264 continue;
18265
18266 case DT_PPC64_GLINK:
18267 s = htab->glink;
18268 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18269 /* We stupidly defined DT_PPC64_GLINK to be the start
18270 of glink rather than the first entry point, which is
18271 what ld.so needs, and now have a bigger stub to
18272 support automatic multiple TOCs. */
18273 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18274 break;
18275
18276 case DT_PPC64_OPD:
18277 s = bfd_get_section_by_name (output_bfd, ".opd");
18278 if (s == NULL)
18279 continue;
18280 dyn.d_un.d_ptr = s->vma;
18281 break;
18282
18283 case DT_PPC64_OPT:
18284 if ((htab->do_multi_toc && htab->multi_toc_needed)
18285 || htab->notoc_plt)
18286 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18287 if (htab->has_plt_localentry0)
18288 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18289 break;
18290
18291 case DT_PPC64_OPDSZ:
18292 s = bfd_get_section_by_name (output_bfd, ".opd");
18293 if (s == NULL)
18294 continue;
18295 dyn.d_un.d_val = s->size;
18296 break;
18297
18298 case DT_PLTGOT:
18299 s = htab->elf.splt;
18300 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18301 break;
18302
18303 case DT_JMPREL:
18304 s = htab->elf.srelplt;
18305 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18306 break;
18307
18308 case DT_PLTRELSZ:
18309 dyn.d_un.d_val = htab->elf.srelplt->size;
18310 break;
18311
18312 case DT_TEXTREL:
18313 if (htab->elf.ifunc_resolvers)
18314 info->callbacks->einfo
18315 (_("%P: warning: text relocations and GNU indirect "
18316 "functions may result in a segfault at runtime\n"));
18317 continue;
18318 }
18319
18320 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18321 }
18322 }
18323
18324 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18325 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18326 {
18327 /* Fill in the first entry in the global offset table.
18328 We use it to hold the link-time TOCbase. */
18329 bfd_put_64 (output_bfd,
18330 elf_gp (output_bfd) + TOC_BASE_OFF,
18331 htab->elf.sgot->contents);
18332
18333 /* Set .got entry size. */
18334 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18335 = 8;
18336 }
18337
18338 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18339 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18340 {
18341 /* Set .plt entry size. */
18342 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18343 = PLT_ENTRY_SIZE (htab);
18344 }
18345
18346 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18347 brlt ourselves if emitrelocations. */
18348 if (htab->brlt != NULL
18349 && htab->brlt->reloc_count != 0
18350 && !_bfd_elf_link_output_relocs (output_bfd,
18351 htab->brlt,
18352 elf_section_data (htab->brlt)->rela.hdr,
18353 elf_section_data (htab->brlt)->relocs,
18354 NULL))
18355 return false;
18356
18357 if (htab->glink != NULL
18358 && htab->glink->reloc_count != 0
18359 && !_bfd_elf_link_output_relocs (output_bfd,
18360 htab->glink,
18361 elf_section_data (htab->glink)->rela.hdr,
18362 elf_section_data (htab->glink)->relocs,
18363 NULL))
18364 return false;
18365
18366
18367 if (htab->glink_eh_frame != NULL
18368 && htab->glink_eh_frame->size != 0
18369 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18370 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18371 htab->glink_eh_frame,
18372 htab->glink_eh_frame->contents))
18373 return false;
18374
18375 /* We need to handle writing out multiple GOT sections ourselves,
18376 since we didn't add them to DYNOBJ. We know dynobj is the first
18377 bfd. */
18378 while ((dynobj = dynobj->link.next) != NULL)
18379 {
18380 asection *s;
18381
18382 if (!is_ppc64_elf (dynobj))
18383 continue;
18384
18385 s = ppc64_elf_tdata (dynobj)->got;
18386 if (s != NULL
18387 && s->size != 0
18388 && s->output_section != bfd_abs_section_ptr
18389 && !bfd_set_section_contents (output_bfd, s->output_section,
18390 s->contents, s->output_offset,
18391 s->size))
18392 return false;
18393 s = ppc64_elf_tdata (dynobj)->relgot;
18394 if (s != NULL
18395 && s->size != 0
18396 && s->output_section != bfd_abs_section_ptr
18397 && !bfd_set_section_contents (output_bfd, s->output_section,
18398 s->contents, s->output_offset,
18399 s->size))
18400 return false;
18401 }
18402
18403 return true;
18404 }
18405
18406 static bool
18407 ppc64_elf_free_cached_info (bfd *abfd)
18408 {
18409 if (abfd->sections)
18410 for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18411 opd != NULL;
18412 opd = bfd_get_next_section_by_name (NULL, opd))
18413 if (opd->reloc_count == 0)
18414 free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18415
18416 return _bfd_elf_free_cached_info (abfd);
18417 }
18418
18419 #include "elf64-target.h"
18420
18421 /* FreeBSD support */
18422
18423 #undef TARGET_LITTLE_SYM
18424 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18425 #undef TARGET_LITTLE_NAME
18426 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18427
18428 #undef TARGET_BIG_SYM
18429 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18430 #undef TARGET_BIG_NAME
18431 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18432
18433 #undef ELF_OSABI
18434 #define ELF_OSABI ELFOSABI_FREEBSD
18435
18436 #undef elf64_bed
18437 #define elf64_bed elf64_powerpc_fbsd_bed
18438
18439 #include "elf64-target.h"
18440