]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - bfd/elf64-ppc.c
strip: Don't check target_defaulted in input BFD
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
... / ...
CommitLineData
1/* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2025 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28/* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31#define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
32
33#include "sysdep.h"
34#include <stdarg.h>
35#include "bfd.h"
36#include "bfdlink.h"
37#include "libbfd.h"
38#include "elf-bfd.h"
39#include "elf/ppc64.h"
40#include "elf64-ppc.h"
41#include "dwarf2.h"
42
43/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44#define OCTETS_PER_BYTE(ABFD, SEC) 1
45
46static bfd_reloc_status_type ppc64_elf_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50static bfd_reloc_status_type ppc64_elf_brtaken_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52static bfd_reloc_status_type ppc64_elf_sectoff_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56static bfd_reloc_status_type ppc64_elf_toc_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60static bfd_reloc_status_type ppc64_elf_toc64_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64static bfd_reloc_status_type ppc64_elf_unhandled_reloc
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66static 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
288static inline int
289abiversion (bfd *abfd)
290{
291 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
292}
293
294static inline void
295set_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\f
305/* Relocation HOWTO's. */
306/* Like other ELF RELA targets that don't apply multiple
307 field-altering relocations to the same localation, src_mask is
308 always zero and pcrel_offset is the same as pc_relative.
309 PowerPC can always use a zero bitpos, even when the field is not at
310 the LSB. For example, a REL24 could use rightshift=2, bisize=24
311 and bitpos=2 which matches the ABI description, or as we do here,
312 rightshift=0, bitsize=26 and bitpos=0. */
313#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
314 complain, special_func) \
315 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
316 complain_overflow_ ## complain, special_func, \
317 #type, false, 0, mask, pc_relative)
318
319static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
320
321static reloc_howto_type ppc64_elf_howto_raw[] =
322{
323 /* This reloc does nothing. */
324 HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
325 bfd_elf_generic_reloc),
326
327 /* A standard 32 bit relocation. */
328 HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
329 bfd_elf_generic_reloc),
330
331 /* An absolute 26 bit branch; the lower two bits must be zero.
332 FIXME: we don't check that, we just clear them. */
333 HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
334 bfd_elf_generic_reloc),
335
336 /* A standard 16 bit relocation. */
337 HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
338 bfd_elf_generic_reloc),
339
340 /* A 16 bit relocation without overflow. */
341 HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
342 bfd_elf_generic_reloc),
343
344 /* Bits 16-31 of an address. */
345 HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
346 bfd_elf_generic_reloc),
347
348 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
349 bits, treated as a signed number, is negative. */
350 HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
351 ppc64_elf_ha_reloc),
352
353 /* An absolute 16 bit branch; the lower two bits must be zero.
354 FIXME: we don't check that, we just clear them. */
355 HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
356 ppc64_elf_branch_reloc),
357
358 /* An absolute 16 bit branch, for which bit 10 should be set to
359 indicate that the branch is expected to be taken. The lower two
360 bits must be zero. */
361 HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
362 ppc64_elf_brtaken_reloc),
363
364 /* An absolute 16 bit branch, for which bit 10 should be set to
365 indicate that the branch is not expected to be taken. The lower
366 two bits must be zero. */
367 HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
368 ppc64_elf_brtaken_reloc),
369
370 /* A relative 26 bit branch; the lower two bits must be zero. */
371 HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
372 ppc64_elf_branch_reloc),
373
374 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
375 HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
376 ppc64_elf_branch_reloc),
377
378 /* Another variant, when p10 insns can't be used on stubs. */
379 HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
380 ppc64_elf_branch_reloc),
381
382 /* A relative 16 bit branch; the lower two bits must be zero. */
383 HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
384 ppc64_elf_branch_reloc),
385
386 /* A relative 16 bit branch. Bit 10 should be set to indicate that
387 the branch is expected to be taken. The lower two bits must be
388 zero. */
389 HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
390 ppc64_elf_brtaken_reloc),
391
392 /* A relative 16 bit branch. Bit 10 should be set to indicate that
393 the branch is not expected to be taken. The lower two bits must
394 be zero. */
395 HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
396 ppc64_elf_brtaken_reloc),
397
398 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
399 symbol. */
400 HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
401 ppc64_elf_unhandled_reloc),
402
403 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
404 the symbol. */
405 HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
406 ppc64_elf_unhandled_reloc),
407
408 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
409 the symbol. */
410 HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
411 ppc64_elf_unhandled_reloc),
412
413 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
414 the symbol. */
415 HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
416 ppc64_elf_unhandled_reloc),
417
418 /* This is used only by the dynamic linker. The symbol should exist
419 both in the object being run and in some shared library. The
420 dynamic linker copies the data addressed by the symbol from the
421 shared library into the object, because the object being
422 run has to have the data at some particular address. */
423 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
424 ppc64_elf_unhandled_reloc),
425
426 /* Like R_PPC64_ADDR64, but used when setting global offset table
427 entries. */
428 HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
429 ppc64_elf_unhandled_reloc),
430
431 /* Created by the link editor. Marks a procedure linkage table
432 entry for a symbol. */
433 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
434 ppc64_elf_unhandled_reloc),
435
436 /* Used only by the dynamic linker. When the object is run, this
437 doubleword64 is set to the load address of the object, plus the
438 addend. */
439 HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
440 bfd_elf_generic_reloc),
441
442 /* Like R_PPC64_ADDR32, but may be unaligned. */
443 HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
444 bfd_elf_generic_reloc),
445
446 /* Like R_PPC64_ADDR16, but may be unaligned. */
447 HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
448 bfd_elf_generic_reloc),
449
450 /* 32-bit PC relative. */
451 HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
452 bfd_elf_generic_reloc),
453
454 /* 32-bit relocation to the symbol's procedure linkage table. */
455 HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
456 ppc64_elf_unhandled_reloc),
457
458 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
459 FIXME: R_PPC64_PLTREL32 not supported. */
460 HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
461 ppc64_elf_unhandled_reloc),
462
463 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
464 the symbol. */
465 HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
466 ppc64_elf_unhandled_reloc),
467
468 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
469 the symbol. */
470 HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
471 ppc64_elf_unhandled_reloc),
472
473 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
474 the symbol. */
475 HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
476 ppc64_elf_unhandled_reloc),
477
478 /* 16-bit section relative relocation. */
479 HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
480 ppc64_elf_sectoff_reloc),
481
482 /* Like R_PPC64_SECTOFF, but no overflow warning. */
483 HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
484 ppc64_elf_sectoff_reloc),
485
486 /* 16-bit upper half section relative relocation. */
487 HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
488 ppc64_elf_sectoff_reloc),
489
490 /* 16-bit upper half adjusted section relative relocation. */
491 HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
492 ppc64_elf_sectoff_ha_reloc),
493
494 /* Like R_PPC64_REL24 without touching the two least significant bits. */
495 HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
496 bfd_elf_generic_reloc),
497
498 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
499
500 /* A standard 64-bit relocation. */
501 HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
502 bfd_elf_generic_reloc),
503
504 /* The bits 32-47 of an address. */
505 HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
506 bfd_elf_generic_reloc),
507
508 /* The bits 32-47 of an address, plus 1 if the contents of the low
509 16 bits, treated as a signed number, is negative. */
510 HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
511 ppc64_elf_ha_reloc),
512
513 /* The bits 48-63 of an address. */
514 HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
515 bfd_elf_generic_reloc),
516
517 /* The bits 48-63 of an address, plus 1 if the contents of the low
518 16 bits, treated as a signed number, is negative. */
519 HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
520 ppc64_elf_ha_reloc),
521
522 /* Like ADDR64, but may be unaligned. */
523 HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
524 bfd_elf_generic_reloc),
525
526 /* 64-bit relative relocation. */
527 HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
528 bfd_elf_generic_reloc),
529
530 /* 64-bit relocation to the symbol's procedure linkage table. */
531 HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
532 ppc64_elf_unhandled_reloc),
533
534 /* 64-bit PC relative relocation to the symbol's procedure linkage
535 table. */
536 /* FIXME: R_PPC64_PLTREL64 not supported. */
537 HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
538 ppc64_elf_unhandled_reloc),
539
540 /* 16 bit TOC-relative relocation. */
541 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
542 HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
543 ppc64_elf_toc_reloc),
544
545 /* 16 bit TOC-relative relocation without overflow. */
546 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
547 HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
548 ppc64_elf_toc_reloc),
549
550 /* 16 bit TOC-relative relocation, high 16 bits. */
551 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
552 HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
553 ppc64_elf_toc_reloc),
554
555 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
556 contents of the low 16 bits, treated as a signed number, is
557 negative. */
558 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
559 HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
560 ppc64_elf_toc_ha_reloc),
561
562 /* 64-bit relocation; insert value of TOC base (.TOC.). */
563 /* R_PPC64_TOC 51 doubleword64 .TOC. */
564 HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
565 ppc64_elf_toc64_reloc),
566
567 /* Like R_PPC64_GOT16, but also informs the link editor that the
568 value to relocate may (!) refer to a PLT entry which the link
569 editor (a) may replace with the symbol value. If the link editor
570 is unable to fully resolve the symbol, it may (b) create a PLT
571 entry and store the address to the new PLT entry in the GOT.
572 This permits lazy resolution of function symbols at run time.
573 The link editor may also skip all of this and just (c) emit a
574 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
575 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
576 HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
577 ppc64_elf_unhandled_reloc),
578
579 /* Like R_PPC64_PLTGOT16, but without overflow. */
580 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
581 HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
582 ppc64_elf_unhandled_reloc),
583
584 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
585 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
586 HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
587 ppc64_elf_unhandled_reloc),
588
589 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
590 1 if the contents of the low 16 bits, treated as a signed number,
591 is negative. */
592 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
593 HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
594 ppc64_elf_unhandled_reloc),
595
596 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
597 HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
598 bfd_elf_generic_reloc),
599
600 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
601 HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
602 bfd_elf_generic_reloc),
603
604 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
605 HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
606 ppc64_elf_unhandled_reloc),
607
608 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
609 HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
610 ppc64_elf_unhandled_reloc),
611
612 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
613 HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
614 ppc64_elf_unhandled_reloc),
615
616 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
617 HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
618 ppc64_elf_sectoff_reloc),
619
620 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
621 HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
622 ppc64_elf_sectoff_reloc),
623
624 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
625 HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
626 ppc64_elf_toc_reloc),
627
628 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
629 HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
630 ppc64_elf_toc_reloc),
631
632 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
633 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
634 HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
635 ppc64_elf_unhandled_reloc),
636
637 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
638 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
639 HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
640 ppc64_elf_unhandled_reloc),
641
642 /* Marker relocs for TLS. */
643 HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
644 bfd_elf_generic_reloc),
645
646 HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
647 bfd_elf_generic_reloc),
648
649 HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
650 bfd_elf_generic_reloc),
651
652 /* Marker reloc for optimizing r2 save in prologue rather than on
653 each plt call stub. */
654 HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
655 bfd_elf_generic_reloc),
656
657 /* Marker relocs on inline plt call instructions. */
658 HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
659 bfd_elf_generic_reloc),
660
661 HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
662 bfd_elf_generic_reloc),
663
664 /* Computes the load module index of the load module that contains the
665 definition of its TLS sym. */
666 HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
667 ppc64_elf_unhandled_reloc),
668
669 /* Computes a dtv-relative displacement, the difference between the value
670 of sym+add and the base address of the thread-local storage block that
671 contains the definition of sym, minus 0x8000. */
672 HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
673 ppc64_elf_unhandled_reloc),
674
675 /* A 16 bit dtprel reloc. */
676 HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
677 ppc64_elf_unhandled_reloc),
678
679 /* Like DTPREL16, but no overflow. */
680 HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
681 ppc64_elf_unhandled_reloc),
682
683 /* Like DTPREL16_LO, but next higher group of 16 bits. */
684 HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
685 ppc64_elf_unhandled_reloc),
686
687 /* Like DTPREL16_HI, but adjust for low 16 bits. */
688 HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
689 ppc64_elf_unhandled_reloc),
690
691 /* Like DTPREL16_HI, but next higher group of 16 bits. */
692 HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
693 ppc64_elf_unhandled_reloc),
694
695 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
696 HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
697 ppc64_elf_unhandled_reloc),
698
699 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
700 HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
701 ppc64_elf_unhandled_reloc),
702
703 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
704 HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
705 ppc64_elf_unhandled_reloc),
706
707 /* Like DTPREL16, but for insns with a DS field. */
708 HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
709 ppc64_elf_unhandled_reloc),
710
711 /* Like DTPREL16_DS, but no overflow. */
712 HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
713 ppc64_elf_unhandled_reloc),
714
715 /* Computes a tp-relative displacement, the difference between the value of
716 sym+add and the value of the thread pointer (r13). */
717 HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
718 ppc64_elf_unhandled_reloc),
719
720 /* A 16 bit tprel reloc. */
721 HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
722 ppc64_elf_unhandled_reloc),
723
724 /* Like TPREL16, but no overflow. */
725 HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
726 ppc64_elf_unhandled_reloc),
727
728 /* Like TPREL16_LO, but next higher group of 16 bits. */
729 HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
730 ppc64_elf_unhandled_reloc),
731
732 /* Like TPREL16_HI, but adjust for low 16 bits. */
733 HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
734 ppc64_elf_unhandled_reloc),
735
736 /* Like TPREL16_HI, but next higher group of 16 bits. */
737 HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
738 ppc64_elf_unhandled_reloc),
739
740 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
741 HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
742 ppc64_elf_unhandled_reloc),
743
744 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
745 HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
746 ppc64_elf_unhandled_reloc),
747
748 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
749 HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
750 ppc64_elf_unhandled_reloc),
751
752 /* Like TPREL16, but for insns with a DS field. */
753 HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
754 ppc64_elf_unhandled_reloc),
755
756 /* Like TPREL16_DS, but no overflow. */
757 HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
758 ppc64_elf_unhandled_reloc),
759
760 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
761 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
762 to the first entry relative to the TOC base (r2). */
763 HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
764 ppc64_elf_unhandled_reloc),
765
766 /* Like GOT_TLSGD16, but no overflow. */
767 HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
768 ppc64_elf_unhandled_reloc),
769
770 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
771 HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
772 ppc64_elf_unhandled_reloc),
773
774 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
775 HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
776 ppc64_elf_unhandled_reloc),
777
778 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
779 with values (sym+add)@dtpmod and zero, and computes the offset to the
780 first entry relative to the TOC base (r2). */
781 HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
782 ppc64_elf_unhandled_reloc),
783
784 /* Like GOT_TLSLD16, but no overflow. */
785 HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
786 ppc64_elf_unhandled_reloc),
787
788 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
789 HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
790 ppc64_elf_unhandled_reloc),
791
792 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
793 HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
794 ppc64_elf_unhandled_reloc),
795
796 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
797 the offset to the entry relative to the TOC base (r2). */
798 HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
799 ppc64_elf_unhandled_reloc),
800
801 /* Like GOT_DTPREL16_DS, but no overflow. */
802 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
803 ppc64_elf_unhandled_reloc),
804
805 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
806 HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
807 ppc64_elf_unhandled_reloc),
808
809 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
810 HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
811 ppc64_elf_unhandled_reloc),
812
813 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
814 offset to the entry relative to the TOC base (r2). */
815 HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
816 ppc64_elf_unhandled_reloc),
817
818 /* Like GOT_TPREL16_DS, but no overflow. */
819 HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
820 ppc64_elf_unhandled_reloc),
821
822 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
823 HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
824 ppc64_elf_unhandled_reloc),
825
826 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
827 HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
828 ppc64_elf_unhandled_reloc),
829
830 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
831 ppc64_elf_unhandled_reloc),
832
833 HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
834 bfd_elf_generic_reloc),
835
836 /* A 16 bit relative relocation. */
837 HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
838 bfd_elf_generic_reloc),
839
840 /* A 16 bit relative relocation without overflow. */
841 HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
842 bfd_elf_generic_reloc),
843
844 /* The high order 16 bits of a relative address. */
845 HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
846 bfd_elf_generic_reloc),
847
848 /* The high order 16 bits of a relative address, plus 1 if the contents of
849 the low 16 bits, treated as a signed number, is negative. */
850 HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
851 ppc64_elf_ha_reloc),
852
853 HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
854 bfd_elf_generic_reloc),
855
856 HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
857 ppc64_elf_ha_reloc),
858
859 HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
860 bfd_elf_generic_reloc),
861
862 HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
863 ppc64_elf_ha_reloc),
864
865 HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
866 bfd_elf_generic_reloc),
867
868 HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
869 ppc64_elf_ha_reloc),
870
871 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
872 HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
873 ppc64_elf_ha_reloc),
874
875 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
876 HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
877 ppc64_elf_ha_reloc),
878
879 /* Like R_PPC64_ADDR16_HI, but no overflow. */
880 HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
881 bfd_elf_generic_reloc),
882
883 /* Like R_PPC64_ADDR16_HA, but no overflow. */
884 HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
885 ppc64_elf_ha_reloc),
886
887 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
888 HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
889 ppc64_elf_unhandled_reloc),
890
891 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
892 HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
893 ppc64_elf_unhandled_reloc),
894
895 /* Like R_PPC64_TPREL16_HI, but no overflow. */
896 HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
897 ppc64_elf_unhandled_reloc),
898
899 /* Like R_PPC64_TPREL16_HA, but no overflow. */
900 HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
901 ppc64_elf_unhandled_reloc),
902
903 /* Marker reloc on ELFv2 large-model function entry. */
904 HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
905 bfd_elf_generic_reloc),
906
907 /* Like ADDR64, but use local entry point of function. */
908 HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
909 bfd_elf_generic_reloc),
910
911 HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
912 bfd_elf_generic_reloc),
913
914 HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
915 bfd_elf_generic_reloc),
916
917 HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
918 bfd_elf_generic_reloc),
919
920 HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
921 ppc64_elf_prefix_reloc),
922
923 HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
924 ppc64_elf_prefix_reloc),
925
926 HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
927 ppc64_elf_prefix_reloc),
928
929 HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
930 ppc64_elf_prefix_reloc),
931
932 HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
933 ppc64_elf_prefix_reloc),
934
935 HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
936 ppc64_elf_unhandled_reloc),
937
938 HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
939 ppc64_elf_unhandled_reloc),
940
941 HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
942 ppc64_elf_unhandled_reloc),
943
944 HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
945 ppc64_elf_unhandled_reloc),
946
947 HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
948 ppc64_elf_unhandled_reloc),
949
950 HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
951 ppc64_elf_unhandled_reloc),
952
953 HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
954 ppc64_elf_unhandled_reloc),
955
956 HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
957 ppc64_elf_unhandled_reloc),
958
959 HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
960 ppc64_elf_unhandled_reloc),
961
962 HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
963 bfd_elf_generic_reloc),
964
965 HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
966 ppc64_elf_ha_reloc),
967
968 HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
969 bfd_elf_generic_reloc),
970
971 HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
972 ppc64_elf_ha_reloc),
973
974 HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
975 bfd_elf_generic_reloc),
976
977 HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
978 ppc64_elf_ha_reloc),
979
980 HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
981 bfd_elf_generic_reloc),
982
983 HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
984 ppc64_elf_ha_reloc),
985
986 HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
987 ppc64_elf_prefix_reloc),
988
989 HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
990 ppc64_elf_prefix_reloc),
991
992 /* GNU extension to record C++ vtable hierarchy. */
993 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
994 NULL),
995
996 /* GNU extension to record C++ vtable member usage. */
997 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
998 NULL),
999};
1000
1001\f
1002/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1003 be done. */
1004
1005static void
1006ppc_howto_init (void)
1007{
1008 unsigned int i, type;
1009
1010 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1011 {
1012 type = ppc64_elf_howto_raw[i].type;
1013 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1014 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1015 }
1016}
1017
1018static reloc_howto_type *
1019ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1020{
1021 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1022
1023 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1024 /* Initialize howto table if needed. */
1025 ppc_howto_init ();
1026
1027 switch (code)
1028 {
1029 default:
1030 /* xgettext:c-format */
1031 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1032 (int) code);
1033 bfd_set_error (bfd_error_bad_value);
1034 return NULL;
1035
1036 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1037 break;
1038 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1039 break;
1040 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1041 break;
1042 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1043 break;
1044 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1045 break;
1046 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1047 break;
1048 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1049 break;
1050 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1051 break;
1052 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1053 break;
1054 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1055 break;
1056 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1057 break;
1058 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1059 break;
1060 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1061 break;
1062 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1063 break;
1064 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1065 break;
1066 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1067 break;
1068 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1069 break;
1070 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1071 break;
1072 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1073 break;
1074 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1075 break;
1076 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1077 break;
1078 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1079 break;
1080 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1081 break;
1082 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1083 break;
1084 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1085 break;
1086 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1087 break;
1088 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1089 break;
1090 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1091 break;
1092 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1093 break;
1094 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1095 break;
1096 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1097 break;
1098 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1099 break;
1100 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1101 break;
1102 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1103 break;
1104 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1105 break;
1106 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1107 break;
1108 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1109 break;
1110 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1111 break;
1112 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1113 break;
1114 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1115 break;
1116 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1117 break;
1118 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1119 break;
1120 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1121 break;
1122 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1123 break;
1124 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1125 break;
1126 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1127 break;
1128 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1129 break;
1130 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1131 break;
1132 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1133 break;
1134 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1135 break;
1136 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1137 break;
1138 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1139 break;
1140 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1141 break;
1142 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1143 break;
1144 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1145 break;
1146 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1147 break;
1148 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1149 break;
1150 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1151 break;
1152 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1153 break;
1154 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1155 break;
1156 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1157 break;
1158 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1159 break;
1160 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1161 break;
1162 case BFD_RELOC_PPC64_TLS_PCREL:
1163 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1164 break;
1165 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1166 break;
1167 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1168 break;
1169 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1170 break;
1171 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1172 break;
1173 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1174 break;
1175 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1176 break;
1177 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1178 break;
1179 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1180 break;
1181 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1182 break;
1183 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1184 break;
1185 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1186 break;
1187 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1188 break;
1189 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1190 break;
1191 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1192 break;
1193 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1194 break;
1195 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1196 break;
1197 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1218 break;
1219 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1220 break;
1221 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1222 break;
1223 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1226 break;
1227 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1228 break;
1229 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1238 break;
1239 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1240 break;
1241 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1250 break;
1251 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1252 break;
1253 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1254 break;
1255 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1256 break;
1257 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1258 break;
1259 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1260 break;
1261 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1262 break;
1263 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1270 break;
1271 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1272 break;
1273 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1274 break;
1275 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1276 break;
1277 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1278 break;
1279 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1280 break;
1281 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1282 break;
1283 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1284 break;
1285 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1286 break;
1287 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1288 break;
1289 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1290 break;
1291 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1292 break;
1293 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1294 break;
1295 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1296 break;
1297 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1298 break;
1299 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1300 break;
1301 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1302 break;
1303 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1304 break;
1305 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1306 break;
1307 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1308 break;
1309 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1312 break;
1313 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1314 break;
1315 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1316 break;
1317 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1320 break;
1321 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1322 break;
1323 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1324 break;
1325 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1326 break;
1327 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1328 break;
1329 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1330 break;
1331 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1332 break;
1333 }
1334
1335 return ppc64_elf_howto_table[r];
1336};
1337
1338static reloc_howto_type *
1339ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1340{
1341 unsigned int i;
1342 static char *compat_map[][2] = {
1343 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1344 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1345 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1346 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1347 };
1348
1349 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1350 if (ppc64_elf_howto_raw[i].name != NULL
1351 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1352 return &ppc64_elf_howto_raw[i];
1353
1354 /* Handle old names of relocations in case they were used by
1355 .reloc directives.
1356 FIXME: Remove this soon. Mapping the reloc names is very likely
1357 completely unnecessary. */
1358 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1359 if (strcasecmp (compat_map[i][0], r_name) == 0)
1360 {
1361 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1362 compat_map[i][1], compat_map[i][0]);
1363 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1364 }
1365
1366 return NULL;
1367}
1368
1369/* Set the howto pointer for a PowerPC ELF reloc. */
1370
1371static bool
1372ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1373 Elf_Internal_Rela *dst)
1374{
1375 unsigned int type;
1376
1377 /* Initialize howto table if needed. */
1378 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1379 ppc_howto_init ();
1380
1381 type = ELF64_R_TYPE (dst->r_info);
1382 if (type >= ARRAY_SIZE (ppc64_elf_howto_table)
1383 || ppc64_elf_howto_table[type] == NULL)
1384 {
1385 /* xgettext:c-format */
1386 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1387 abfd, type);
1388 bfd_set_error (bfd_error_bad_value);
1389 return false;
1390 }
1391 cache_ptr->howto = ppc64_elf_howto_table[type];
1392 return true;
1393}
1394
1395/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1396
1397static bfd_reloc_status_type
1398ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1399 void *data, asection *input_section,
1400 bfd *output_bfd, char **error_message)
1401{
1402 enum elf_ppc64_reloc_type r_type;
1403 long insn;
1404 bfd_size_type octets;
1405 bfd_vma value;
1406
1407 /* If this is a relocatable link (output_bfd test tells us), just
1408 call the generic function. Any adjustment will be done at final
1409 link time. */
1410 if (output_bfd != NULL)
1411 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1412 input_section, output_bfd, error_message);
1413
1414 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1415 We won't actually be using the low bits, so trashing them
1416 doesn't matter. */
1417 r_type = reloc_entry->howto->type;
1418 if (r_type == R_PPC64_ADDR16_HIGHERA34
1419 || r_type == R_PPC64_ADDR16_HIGHESTA34
1420 || r_type == R_PPC64_REL16_HIGHERA34
1421 || r_type == R_PPC64_REL16_HIGHESTA34)
1422 reloc_entry->addend += 1ULL << 33;
1423 else
1424 reloc_entry->addend += 1U << 15;
1425 if (r_type != R_PPC64_REL16DX_HA)
1426 return bfd_reloc_continue;
1427
1428 value = 0;
1429 if (!bfd_is_com_section (symbol->section))
1430 value = symbol->value;
1431 value += (reloc_entry->addend
1432 + symbol->section->output_offset
1433 + symbol->section->output_section->vma);
1434 value -= (reloc_entry->address
1435 + input_section->output_offset
1436 + input_section->output_section->vma);
1437 value = (bfd_signed_vma) value >> 16;
1438
1439 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1440 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1441 input_section, octets))
1442 return bfd_reloc_outofrange;
1443
1444 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1445 insn &= ~0x1fffc1;
1446 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1447 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1448 if (value + 0x8000 > 0xffff)
1449 return bfd_reloc_overflow;
1450 return bfd_reloc_ok;
1451}
1452
1453static bfd_reloc_status_type
1454ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1455 void *data, asection *input_section,
1456 bfd *output_bfd, char **error_message)
1457{
1458 if (output_bfd != NULL)
1459 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1460 input_section, output_bfd, error_message);
1461
1462 if (symbol->section->owner == NULL
1463 || !is_ppc64_elf (symbol->section->owner))
1464 return bfd_reloc_continue;
1465
1466 if (strcmp (symbol->section->name, ".opd") == 0
1467 && (symbol->section->owner->flags & DYNAMIC) == 0)
1468 {
1469 bfd_vma dest = opd_entry_value (symbol->section,
1470 symbol->value + reloc_entry->addend,
1471 NULL, NULL, false);
1472 if (dest != (bfd_vma) -1)
1473 reloc_entry->addend = dest - (symbol->value
1474 + symbol->section->output_section->vma
1475 + symbol->section->output_offset);
1476 }
1477 else
1478 {
1479 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1480
1481 if (symbol->section->owner != abfd
1482 && abiversion (symbol->section->owner) >= 2)
1483 {
1484 unsigned int i;
1485
1486 for (i = 0; i < symbol->section->owner->symcount; ++i)
1487 {
1488 asymbol *symdef = symbol->section->owner->outsymbols[i];
1489
1490 if (strcmp (symdef->name, symbol->name) == 0)
1491 {
1492 elfsym = (elf_symbol_type *) symdef;
1493 break;
1494 }
1495 }
1496 }
1497 reloc_entry->addend
1498 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1499 }
1500 return bfd_reloc_continue;
1501}
1502
1503static bfd_reloc_status_type
1504ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1505 void *data, asection *input_section,
1506 bfd *output_bfd, char **error_message)
1507{
1508 long insn;
1509 enum elf_ppc64_reloc_type r_type;
1510 bfd_size_type octets;
1511 /* Assume 'at' branch hints. */
1512 bool is_isa_v2 = true;
1513
1514 /* If this is a relocatable link (output_bfd test tells us), just
1515 call the generic function. Any adjustment will be done at final
1516 link time. */
1517 if (output_bfd != NULL)
1518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1519 input_section, output_bfd, error_message);
1520
1521 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1522 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1523 input_section, octets))
1524 return bfd_reloc_outofrange;
1525
1526 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1527 insn &= ~(0x01 << 21);
1528 r_type = reloc_entry->howto->type;
1529 if (r_type == R_PPC64_ADDR14_BRTAKEN
1530 || r_type == R_PPC64_REL14_BRTAKEN)
1531 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1532
1533 if (is_isa_v2)
1534 {
1535 /* Set 'a' bit. This is 0b00010 in BO field for branch
1536 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1537 for branch on CTR insns (BO == 1a00t or 1a01t). */
1538 if ((insn & (0x14 << 21)) == (0x04 << 21))
1539 insn |= 0x02 << 21;
1540 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1541 insn |= 0x08 << 21;
1542 else
1543 goto out;
1544 }
1545 else
1546 {
1547 bfd_vma target = 0;
1548 bfd_vma from;
1549
1550 if (!bfd_is_com_section (symbol->section))
1551 target = symbol->value;
1552 target += symbol->section->output_section->vma;
1553 target += symbol->section->output_offset;
1554 target += reloc_entry->addend;
1555
1556 from = (reloc_entry->address
1557 + input_section->output_offset
1558 + input_section->output_section->vma);
1559
1560 /* Invert 'y' bit if not the default. */
1561 if ((bfd_signed_vma) (target - from) < 0)
1562 insn ^= 0x01 << 21;
1563 }
1564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1565 out:
1566 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1567 input_section, output_bfd, error_message);
1568}
1569
1570static bfd_reloc_status_type
1571ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1572 void *data, asection *input_section,
1573 bfd *output_bfd, char **error_message)
1574{
1575 /* If this is a relocatable link (output_bfd test tells us), just
1576 call the generic function. Any adjustment will be done at final
1577 link time. */
1578 if (output_bfd != NULL)
1579 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1580 input_section, output_bfd, error_message);
1581
1582 /* Subtract the symbol section base address. */
1583 reloc_entry->addend -= symbol->section->output_section->vma;
1584 return bfd_reloc_continue;
1585}
1586
1587static bfd_reloc_status_type
1588ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1589 void *data, asection *input_section,
1590 bfd *output_bfd, char **error_message)
1591{
1592 /* If this is a relocatable link (output_bfd test tells us), just
1593 call the generic function. Any adjustment will be done at final
1594 link time. */
1595 if (output_bfd != NULL)
1596 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1597 input_section, output_bfd, error_message);
1598
1599 /* Subtract the symbol section base address. */
1600 reloc_entry->addend -= symbol->section->output_section->vma;
1601
1602 /* Adjust the addend for sign extension of the low 16 bits. */
1603 reloc_entry->addend += 0x8000;
1604 return bfd_reloc_continue;
1605}
1606
1607static bfd_reloc_status_type
1608ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609 void *data, asection *input_section,
1610 bfd *output_bfd, char **error_message)
1611{
1612 bfd_vma TOCstart;
1613
1614 /* If this is a relocatable link (output_bfd test tells us), just
1615 call the generic function. Any adjustment will be done at final
1616 link time. */
1617 if (output_bfd != NULL)
1618 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1619 input_section, output_bfd, error_message);
1620
1621 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1622 if (TOCstart == 0)
1623 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1624
1625 /* Subtract the TOC base address. */
1626 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1627 return bfd_reloc_continue;
1628}
1629
1630static bfd_reloc_status_type
1631ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1632 void *data, asection *input_section,
1633 bfd *output_bfd, char **error_message)
1634{
1635 bfd_vma TOCstart;
1636
1637 /* If this is a relocatable link (output_bfd test tells us), just
1638 call the generic function. Any adjustment will be done at final
1639 link time. */
1640 if (output_bfd != NULL)
1641 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1642 input_section, output_bfd, error_message);
1643
1644 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1645 if (TOCstart == 0)
1646 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1647
1648 /* Subtract the TOC base address. */
1649 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1650
1651 /* Adjust the addend for sign extension of the low 16 bits. */
1652 reloc_entry->addend += 0x8000;
1653 return bfd_reloc_continue;
1654}
1655
1656static bfd_reloc_status_type
1657ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1658 void *data, asection *input_section,
1659 bfd *output_bfd, char **error_message)
1660{
1661 bfd_vma TOCstart;
1662 bfd_size_type octets;
1663
1664 /* If this is a relocatable link (output_bfd test tells us), just
1665 call the generic function. Any adjustment will be done at final
1666 link time. */
1667 if (output_bfd != NULL)
1668 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1669 input_section, output_bfd, error_message);
1670
1671 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1672 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1673 input_section, octets))
1674 return bfd_reloc_outofrange;
1675
1676 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1677 if (TOCstart == 0)
1678 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1679
1680 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1681 return bfd_reloc_ok;
1682}
1683
1684static bfd_reloc_status_type
1685ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1686 void *data, asection *input_section,
1687 bfd *output_bfd, char **error_message)
1688{
1689 uint64_t insn;
1690 bfd_vma targ;
1691 bfd_size_type octets;
1692
1693 if (output_bfd != NULL)
1694 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1695 input_section, output_bfd, error_message);
1696
1697 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1698 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1699 input_section, octets))
1700 return bfd_reloc_outofrange;
1701
1702 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1703 insn <<= 32;
1704 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1705
1706 targ = (symbol->section->output_section->vma
1707 + symbol->section->output_offset
1708 + reloc_entry->addend);
1709 if (!bfd_is_com_section (symbol->section))
1710 targ += symbol->value;
1711 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1712 targ += 1ULL << 33;
1713 if (reloc_entry->howto->pc_relative)
1714 {
1715 bfd_vma from = (reloc_entry->address
1716 + input_section->output_offset
1717 + input_section->output_section->vma);
1718 targ -=from;
1719 }
1720 targ >>= reloc_entry->howto->rightshift;
1721 insn &= ~reloc_entry->howto->dst_mask;
1722 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1723 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1724 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1725 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1726 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1727 >= 1ULL << reloc_entry->howto->bitsize))
1728 return bfd_reloc_overflow;
1729 return bfd_reloc_ok;
1730}
1731
1732static bfd_reloc_status_type
1733ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1734 void *data, asection *input_section,
1735 bfd *output_bfd, char **error_message)
1736{
1737 /* If this is a relocatable link (output_bfd test tells us), just
1738 call the generic function. Any adjustment will be done at final
1739 link time. */
1740 if (output_bfd != NULL)
1741 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1742 input_section, output_bfd, error_message);
1743
1744 if (error_message != NULL)
1745 *error_message = bfd_asprintf (_("generic linker can't handle %s"),
1746 reloc_entry->howto->name);
1747 return bfd_reloc_dangerous;
1748}
1749
1750/* Track GOT entries needed for a given symbol. We might need more
1751 than one got entry per symbol. */
1752struct got_entry
1753{
1754 struct got_entry *next;
1755
1756 /* The symbol addend that we'll be placing in the GOT. */
1757 bfd_vma addend;
1758
1759 /* Unlike other ELF targets, we use separate GOT entries for the same
1760 symbol referenced from different input files. This is to support
1761 automatic multiple TOC/GOT sections, where the TOC base can vary
1762 from one input file to another. After partitioning into TOC groups
1763 we merge entries within the group.
1764
1765 Point to the BFD owning this GOT entry. */
1766 bfd *owner;
1767
1768 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1769 TLS_TPREL or TLS_DTPREL for tls entries. */
1770 unsigned char tls_type;
1771
1772 /* Non-zero if got.ent points to real entry. */
1773 unsigned char is_indirect;
1774
1775 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1776 union
1777 {
1778 bfd_signed_vma refcount;
1779 bfd_vma offset;
1780 struct got_entry *ent;
1781 } got;
1782};
1783
1784/* The same for PLT. */
1785struct plt_entry
1786{
1787 struct plt_entry *next;
1788
1789 bfd_vma addend;
1790
1791 union
1792 {
1793 bfd_signed_vma refcount;
1794 bfd_vma offset;
1795 } plt;
1796};
1797
1798struct ppc64_elf_obj_tdata
1799{
1800 struct elf_obj_tdata elf;
1801
1802 /* Shortcuts to dynamic linker sections. */
1803 asection *got;
1804 asection *relgot;
1805
1806 /* Used during garbage collection. We attach global symbols defined
1807 on removed .opd entries to this section so that the sym is removed. */
1808 asection *deleted_section;
1809
1810 /* TLS local dynamic got entry handling. Support for multiple GOT
1811 sections means we potentially need one of these for each input bfd. */
1812 struct got_entry tlsld_got;
1813
1814 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1815 the reloc to be in the range -32768 to 32767. */
1816 unsigned int has_small_toc_reloc : 1;
1817
1818 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1819 instruction not one we handle. */
1820 unsigned int unexpected_toc_insn : 1;
1821
1822 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1823 this file. */
1824 unsigned int has_optrel : 1;
1825};
1826
1827#define ppc64_elf_tdata(bfd) \
1828 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1829
1830#define ppc64_tlsld_got(bfd) \
1831 (&ppc64_elf_tdata (bfd)->tlsld_got)
1832
1833/* Override the generic function because we store some extras. */
1834
1835static bool
1836ppc64_elf_mkobject (bfd *abfd)
1837{
1838 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata));
1839}
1840
1841/* Fix bad default arch selected for a 64 bit input bfd when the
1842 default is 32 bit. Also select arch based on apuinfo. */
1843
1844static bool
1845ppc64_elf_object_p (bfd *abfd)
1846{
1847 if (!abfd->arch_info->the_default)
1848 return true;
1849
1850 if (abfd->arch_info->bits_per_word == 32)
1851 {
1852 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1853
1854 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1855 {
1856 /* Relies on arch after 32 bit default being 64 bit default. */
1857 abfd->arch_info = abfd->arch_info->next;
1858 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1859 }
1860 }
1861 return _bfd_elf_ppc_set_arch (abfd);
1862}
1863
1864/* Support for core dump NOTE sections. */
1865
1866static bool
1867ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1868{
1869 size_t offset, size;
1870
1871 if (note->descsz != 504)
1872 return false;
1873
1874 /* pr_cursig */
1875 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1876
1877 /* pr_pid */
1878 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1879
1880 /* pr_reg */
1881 offset = 112;
1882 size = 384;
1883
1884 /* Make a ".reg/999" section. */
1885 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1886 size, note->descpos + offset);
1887}
1888
1889static bool
1890ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1891{
1892 if (note->descsz != 136)
1893 return false;
1894
1895 elf_tdata (abfd)->core->pid
1896 = bfd_get_32 (abfd, note->descdata + 24);
1897 elf_tdata (abfd)->core->program
1898 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1899 elf_tdata (abfd)->core->command
1900 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1901
1902 return true;
1903}
1904
1905static char *
1906ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1907 ...)
1908{
1909 switch (note_type)
1910 {
1911 default:
1912 return NULL;
1913
1914 case NT_PRPSINFO:
1915 {
1916 char data[136] ATTRIBUTE_NONSTRING;
1917 va_list ap;
1918
1919 va_start (ap, note_type);
1920 memset (data, 0, sizeof (data));
1921 strncpy (data + 40, va_arg (ap, const char *), 16);
1922#if GCC_VERSION == 8000 || GCC_VERSION == 8001
1923 DIAGNOSTIC_PUSH;
1924 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1925 -Wstringop-truncation:
1926 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1927 */
1928 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1929#endif
1930 strncpy (data + 56, va_arg (ap, const char *), 80);
1931#if GCC_VERSION == 8000 || GCC_VERSION == 8001
1932 DIAGNOSTIC_POP;
1933#endif
1934 va_end (ap);
1935 return elfcore_write_note (abfd, buf, bufsiz,
1936 "CORE", note_type, data, sizeof (data));
1937 }
1938
1939 case NT_PRSTATUS:
1940 {
1941 char data[504];
1942 va_list ap;
1943 long pid;
1944 int cursig;
1945 const void *greg;
1946
1947 va_start (ap, note_type);
1948 memset (data, 0, 112);
1949 pid = va_arg (ap, long);
1950 bfd_put_32 (abfd, pid, data + 32);
1951 cursig = va_arg (ap, int);
1952 bfd_put_16 (abfd, cursig, data + 12);
1953 greg = va_arg (ap, const void *);
1954 memcpy (data + 112, greg, 384);
1955 memset (data + 496, 0, 8);
1956 va_end (ap);
1957 return elfcore_write_note (abfd, buf, bufsiz,
1958 "CORE", note_type, data, sizeof (data));
1959 }
1960 }
1961}
1962
1963/* Add extra PPC sections. */
1964
1965static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1966{
1967 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1968 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1969 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1970 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1971 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1972 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1973 { NULL, 0, 0, 0, 0 }
1974};
1975
1976enum _ppc64_sec_type {
1977 sec_normal = 0,
1978 sec_opd = 1,
1979 sec_toc = 2,
1980 sec_stub = 3
1981};
1982
1983struct _ppc64_elf_section_data
1984{
1985 struct bfd_elf_section_data elf;
1986
1987 union
1988 {
1989 /* An array with one entry for each opd function descriptor,
1990 and some spares since opd entries may be either 16 or 24 bytes. */
1991#define OPD_NDX(OFF) ((OFF) >> 4)
1992 struct _opd_sec_data
1993 {
1994 /* Points to the function code section for local opd entries. */
1995 asection **func_sec;
1996
1997 /* After editing .opd, adjust references to opd local syms. */
1998 long *adjust;
1999
2000 union
2001 {
2002 /* A copy of relocs before they are modified for --emit-relocs. */
2003 Elf_Internal_Rela *relocs;
2004
2005 /* Section contents. */
2006 bfd_byte *contents;
2007 } u;
2008 } opd;
2009
2010 /* An array for toc sections, indexed by offset/8. */
2011 struct _toc_sec_data
2012 {
2013 /* Specifies the relocation symbol index used at a given toc offset. */
2014 unsigned *symndx;
2015
2016 /* And the relocation addend. */
2017 bfd_vma *add;
2018 } toc;
2019
2020 /* Stub debugging. */
2021 struct ppc_stub_hash_entry *last_ent;
2022 } u;
2023
2024 enum _ppc64_sec_type sec_type:2;
2025
2026 /* Flag set when small branches are detected. Used to
2027 select suitable defaults for the stub group size. */
2028 unsigned int has_14bit_branch:1;
2029
2030 /* Flag set when PLTCALL relocs are detected. */
2031 unsigned int has_pltcall:1;
2032
2033 /* Flag set when section has PLT/GOT/TOC relocations that can be
2034 optimised. */
2035 unsigned int has_optrel:1;
2036};
2037
2038#define ppc64_elf_section_data(sec) \
2039 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2040
2041static bool
2042ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2043{
2044 struct _ppc64_elf_section_data *sdata;
2045
2046 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2047 if (sdata == NULL)
2048 return false;
2049 sec->used_by_bfd = sdata;
2050
2051 return _bfd_elf_new_section_hook (abfd, sec);
2052}
2053
2054static bool
2055ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2056{
2057 const char *name = hdr->bfd_section->name;
2058
2059 if (startswith (name, ".sbss")
2060 || startswith (name, ".sdata"))
2061 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2062
2063 return true;
2064}
2065
2066static struct _opd_sec_data *
2067get_opd_info (asection * sec)
2068{
2069 if (sec != NULL
2070 && ppc64_elf_section_data (sec) != NULL
2071 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2072 return &ppc64_elf_section_data (sec)->u.opd;
2073 return NULL;
2074}
2075\f
2076/* Parameters for the qsort hook. */
2077static bool synthetic_relocatable;
2078static const asection *synthetic_opd;
2079
2080/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2081
2082static int
2083compare_symbols (const void *ap, const void *bp)
2084{
2085 const asymbol *a = *(const asymbol **) ap;
2086 const asymbol *b = *(const asymbol **) bp;
2087
2088 /* Section symbols first. */
2089 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2090 return -1;
2091 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2092 return 1;
2093
2094 /* then .opd symbols. */
2095 if (synthetic_opd != NULL)
2096 {
2097 if (strcmp (a->section->name, ".opd") == 0
2098 && strcmp (b->section->name, ".opd") != 0)
2099 return -1;
2100 if (strcmp (a->section->name, ".opd") != 0
2101 && strcmp (b->section->name, ".opd") == 0)
2102 return 1;
2103 }
2104
2105 /* then other code symbols. */
2106 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2107 == (SEC_CODE | SEC_ALLOC))
2108 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2109 != (SEC_CODE | SEC_ALLOC)))
2110 return -1;
2111
2112 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2113 != (SEC_CODE | SEC_ALLOC))
2114 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2115 == (SEC_CODE | SEC_ALLOC)))
2116 return 1;
2117
2118 if (synthetic_relocatable)
2119 {
2120 if (a->section->id < b->section->id)
2121 return -1;
2122
2123 if (a->section->id > b->section->id)
2124 return 1;
2125 }
2126
2127 if (a->value + a->section->vma < b->value + b->section->vma)
2128 return -1;
2129
2130 if (a->value + a->section->vma > b->value + b->section->vma)
2131 return 1;
2132
2133 /* For syms with the same value, prefer strong dynamic global function
2134 syms over other syms. */
2135 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2136 return -1;
2137
2138 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2139 return 1;
2140
2141 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2142 return -1;
2143
2144 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2145 return 1;
2146
2147 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2148 return -1;
2149
2150 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2151 return 1;
2152
2153 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2154 return -1;
2155
2156 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2157 return 1;
2158
2159 /* Finally, sort on where the symbol is in memory. The symbols will
2160 be in at most two malloc'd blocks, one for static syms, one for
2161 dynamic syms, and we distinguish the two blocks above by testing
2162 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2163 originally in the same order as the symbols (and we're not
2164 sorting the symbols themselves), this ensures a stable sort. */
2165 if (a < b)
2166 return -1;
2167 if (a > b)
2168 return 1;
2169 return 0;
2170}
2171
2172/* Search SYMS for a symbol of the given VALUE. */
2173
2174static asymbol *
2175sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2176 bfd_vma value)
2177{
2178 size_t mid;
2179
2180 if (id == (unsigned) -1)
2181 {
2182 while (lo < hi)
2183 {
2184 mid = (lo + hi) >> 1;
2185 if (syms[mid]->value + syms[mid]->section->vma < value)
2186 lo = mid + 1;
2187 else if (syms[mid]->value + syms[mid]->section->vma > value)
2188 hi = mid;
2189 else
2190 return syms[mid];
2191 }
2192 }
2193 else
2194 {
2195 while (lo < hi)
2196 {
2197 mid = (lo + hi) >> 1;
2198 if (syms[mid]->section->id < id)
2199 lo = mid + 1;
2200 else if (syms[mid]->section->id > id)
2201 hi = mid;
2202 else if (syms[mid]->value < value)
2203 lo = mid + 1;
2204 else if (syms[mid]->value > value)
2205 hi = mid;
2206 else
2207 return syms[mid];
2208 }
2209 }
2210 return NULL;
2211}
2212
2213static bool
2214section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2215{
2216 bfd_vma vma = *(bfd_vma *) ptr;
2217 return ((section->flags & SEC_ALLOC) != 0
2218 && section->vma <= vma
2219 && vma < section->vma + section->size);
2220}
2221
2222/* Create synthetic symbols, effectively restoring "dot-symbol" function
2223 entry syms. Also generate @plt symbols for the glink branch table.
2224 Returns count of synthetic symbols in RET or -1 on error. */
2225
2226static long
2227ppc64_elf_get_synthetic_symtab (bfd *abfd,
2228 long static_count, asymbol **static_syms,
2229 long dyn_count, asymbol **dyn_syms,
2230 asymbol **ret)
2231{
2232 asymbol *s;
2233 size_t i, j, count;
2234 char *names;
2235 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2236 asection *opd = NULL;
2237 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2238 asymbol **syms;
2239 int abi = abiversion (abfd);
2240
2241 *ret = NULL;
2242
2243 if (abi < 2)
2244 {
2245 opd = bfd_get_section_by_name (abfd, ".opd");
2246 if (opd == NULL && abi == 1)
2247 return 0;
2248 }
2249
2250 syms = NULL;
2251 codesecsym = 0;
2252 codesecsymend = 0;
2253 secsymend = 0;
2254 opdsymend = 0;
2255 symcount = 0;
2256 if (opd != NULL)
2257 {
2258 symcount = static_count;
2259 if (!relocatable)
2260 symcount += dyn_count;
2261 if (symcount == 0)
2262 return 0;
2263
2264 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2265 if (syms == NULL)
2266 return -1;
2267
2268 if (!relocatable && static_count != 0 && dyn_count != 0)
2269 {
2270 /* Use both symbol tables. */
2271 memcpy (syms, static_syms, static_count * sizeof (*syms));
2272 memcpy (syms + static_count, dyn_syms,
2273 (dyn_count + 1) * sizeof (*syms));
2274 }
2275 else if (!relocatable && static_count == 0)
2276 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2277 else
2278 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2279
2280 /* Trim uninteresting symbols. Interesting symbols are section,
2281 function, and notype symbols. */
2282 for (i = 0, j = 0; i < symcount; ++i)
2283 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2284 | BSF_RELC | BSF_SRELC)) == 0)
2285 syms[j++] = syms[i];
2286 symcount = j;
2287
2288 synthetic_relocatable = relocatable;
2289 synthetic_opd = opd;
2290 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2291
2292 if (!relocatable && symcount > 1)
2293 {
2294 /* Trim duplicate syms, since we may have merged the normal
2295 and dynamic symbols. Actually, we only care about syms
2296 that have different values, so trim any with the same
2297 value. Don't consider ifunc and ifunc resolver symbols
2298 duplicates however, because GDB wants to know whether a
2299 text symbol is an ifunc resolver. */
2300 for (i = 1, j = 1; i < symcount; ++i)
2301 {
2302 const asymbol *s0 = syms[i - 1];
2303 const asymbol *s1 = syms[i];
2304
2305 if ((s0->value + s0->section->vma
2306 != s1->value + s1->section->vma)
2307 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2308 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2309 syms[j++] = syms[i];
2310 }
2311 symcount = j;
2312 }
2313
2314 i = 0;
2315 /* Note that here and in compare_symbols we can't compare opd and
2316 sym->section directly. With separate debug info files, the
2317 symbols will be extracted from the debug file while abfd passed
2318 to this function is the real binary. */
2319 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2320 && strcmp (syms[i]->section->name, ".opd") == 0)
2321 ++i;
2322 codesecsym = i;
2323
2324 for (; i < symcount; ++i)
2325 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2326 | SEC_THREAD_LOCAL))
2327 != (SEC_CODE | SEC_ALLOC))
2328 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2329 break;
2330 codesecsymend = i;
2331
2332 for (; i < symcount; ++i)
2333 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2334 break;
2335 secsymend = i;
2336
2337 for (; i < symcount; ++i)
2338 if (strcmp (syms[i]->section->name, ".opd") != 0)
2339 break;
2340 opdsymend = i;
2341
2342 for (; i < symcount; ++i)
2343 if (((syms[i]->section->flags
2344 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2345 != (SEC_CODE | SEC_ALLOC))
2346 break;
2347 symcount = i;
2348 }
2349 count = 0;
2350
2351 if (relocatable)
2352 {
2353 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2354 arelent *r;
2355 size_t size;
2356 size_t relcount;
2357
2358 if (opdsymend == secsymend)
2359 goto done;
2360
2361 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2362 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2363 if (relcount == 0)
2364 goto done;
2365
2366 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2367 {
2368 count = -1;
2369 goto done;
2370 }
2371
2372 size = 0;
2373 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2374 {
2375 asymbol *sym;
2376
2377 while (r < opd->relocation + relcount
2378 && r->address < syms[i]->value + opd->vma)
2379 ++r;
2380
2381 if (r == opd->relocation + relcount)
2382 break;
2383
2384 if (r->address != syms[i]->value + opd->vma)
2385 continue;
2386
2387 if (r->howto->type != R_PPC64_ADDR64)
2388 continue;
2389
2390 sym = *r->sym_ptr_ptr;
2391 if (!sym_exists_at (syms, opdsymend, symcount,
2392 sym->section->id, sym->value + r->addend))
2393 {
2394 ++count;
2395 size += sizeof (asymbol);
2396 size += strlen (syms[i]->name) + 2;
2397 }
2398 }
2399
2400 if (size == 0)
2401 goto done;
2402 s = *ret = bfd_malloc (size);
2403 if (s == NULL)
2404 {
2405 count = -1;
2406 goto done;
2407 }
2408
2409 names = (char *) (s + count);
2410
2411 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2412 {
2413 asymbol *sym;
2414
2415 while (r < opd->relocation + relcount
2416 && r->address < syms[i]->value + opd->vma)
2417 ++r;
2418
2419 if (r == opd->relocation + relcount)
2420 break;
2421
2422 if (r->address != syms[i]->value + opd->vma)
2423 continue;
2424
2425 if (r->howto->type != R_PPC64_ADDR64)
2426 continue;
2427
2428 sym = *r->sym_ptr_ptr;
2429 if (!sym_exists_at (syms, opdsymend, symcount,
2430 sym->section->id, sym->value + r->addend))
2431 {
2432 size_t len;
2433
2434 *s = *syms[i];
2435 s->flags |= BSF_SYNTHETIC;
2436 s->section = sym->section;
2437 s->value = sym->value + r->addend;
2438 s->name = names;
2439 *names++ = '.';
2440 len = strlen (syms[i]->name);
2441 memcpy (names, syms[i]->name, len + 1);
2442 names += len + 1;
2443 /* Have udata.p point back to the original symbol this
2444 synthetic symbol was derived from. */
2445 s->udata.p = syms[i];
2446 s++;
2447 }
2448 }
2449 }
2450 else
2451 {
2452 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2453 bfd_byte *contents = NULL;
2454 size_t size;
2455 size_t plt_count = 0;
2456 bfd_vma glink_vma = 0, resolv_vma = 0;
2457 asection *dynamic, *glink = NULL, *relplt = NULL;
2458 arelent *p;
2459
2460 if (opd != NULL
2461 && ((opd->flags & SEC_HAS_CONTENTS) == 0
2462 || !bfd_malloc_and_get_section (abfd, opd, &contents)))
2463 {
2464 free_contents_and_exit_err:
2465 count = -1;
2466 free_contents_and_exit:
2467 free (contents);
2468 goto done;
2469 }
2470
2471 size = 0;
2472 for (i = secsymend; i < opdsymend; ++i)
2473 {
2474 bfd_vma ent;
2475
2476 /* Ignore bogus symbols. */
2477 if (syms[i]->value > opd->size - 8)
2478 continue;
2479
2480 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2481 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2482 {
2483 ++count;
2484 size += sizeof (asymbol);
2485 size += strlen (syms[i]->name) + 2;
2486 }
2487 }
2488
2489 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2490 if (dyn_count != 0
2491 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2492 {
2493 bfd_byte *dynbuf, *extdyn, *extdynend;
2494 size_t extdynsize;
2495 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2496
2497 if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2498 || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2499 goto free_contents_and_exit_err;
2500
2501 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2502 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2503
2504 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2505 (size_t) (extdynend - extdyn) >= extdynsize;
2506 extdyn += extdynsize)
2507 {
2508 Elf_Internal_Dyn dyn;
2509 (*swap_dyn_in) (abfd, extdyn, &dyn);
2510
2511 if (dyn.d_tag == DT_NULL)
2512 break;
2513
2514 if (dyn.d_tag == DT_PPC64_GLINK)
2515 {
2516 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2517 See comment in ppc64_elf_finish_dynamic_sections. */
2518 glink_vma = dyn.d_un.d_val + 8 * 4;
2519 /* The .glink section usually does not survive the final
2520 link; search for the section (usually .text) where the
2521 glink stubs now reside. */
2522 glink = bfd_sections_find_if (abfd, section_covers_vma,
2523 &glink_vma);
2524 break;
2525 }
2526 }
2527
2528 free (dynbuf);
2529 }
2530
2531 if (glink != NULL)
2532 {
2533 /* Determine __glink trampoline by reading the relative branch
2534 from the first glink stub. */
2535 bfd_byte buf[4];
2536 unsigned int off = 0;
2537
2538 while (bfd_get_section_contents (abfd, glink, buf,
2539 glink_vma + off - glink->vma, 4))
2540 {
2541 unsigned int insn = bfd_get_32 (abfd, buf);
2542 insn ^= B_DOT;
2543 if ((insn & ~0x3fffffc) == 0)
2544 {
2545 resolv_vma
2546 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2547 break;
2548 }
2549 off += 4;
2550 if (off > 4)
2551 break;
2552 }
2553
2554 if (resolv_vma)
2555 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2556
2557 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2558 if (relplt != NULL)
2559 {
2560 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2561 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2562 goto free_contents_and_exit_err;
2563
2564 plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
2565 size += plt_count * sizeof (asymbol);
2566
2567 p = relplt->relocation;
2568 for (i = 0; i < plt_count; i++, p++)
2569 {
2570 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2571 if (p->addend != 0)
2572 size += sizeof ("+0x") - 1 + 16;
2573 }
2574 }
2575 }
2576
2577 if (size == 0)
2578 goto free_contents_and_exit;
2579 s = *ret = bfd_malloc (size);
2580 if (s == NULL)
2581 goto free_contents_and_exit_err;
2582
2583 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2584
2585 for (i = secsymend; i < opdsymend; ++i)
2586 {
2587 bfd_vma ent;
2588
2589 if (syms[i]->value > opd->size - 8)
2590 continue;
2591
2592 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2593 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2594 {
2595 size_t lo, hi;
2596 size_t len;
2597 asection *sec = abfd->sections;
2598
2599 *s = *syms[i];
2600 lo = codesecsym;
2601 hi = codesecsymend;
2602 while (lo < hi)
2603 {
2604 size_t mid = (lo + hi) >> 1;
2605 if (syms[mid]->section->vma < ent)
2606 lo = mid + 1;
2607 else if (syms[mid]->section->vma > ent)
2608 hi = mid;
2609 else
2610 {
2611 sec = syms[mid]->section;
2612 break;
2613 }
2614 }
2615
2616 if (lo >= hi && lo > codesecsym)
2617 sec = syms[lo - 1]->section;
2618
2619 for (; sec != NULL; sec = sec->next)
2620 {
2621 if (sec->vma > ent)
2622 break;
2623 /* SEC_LOAD may not be set if SEC is from a separate debug
2624 info file. */
2625 if ((sec->flags & SEC_ALLOC) == 0)
2626 break;
2627 if ((sec->flags & SEC_CODE) != 0)
2628 s->section = sec;
2629 }
2630 s->flags |= BSF_SYNTHETIC;
2631 s->value = ent - s->section->vma;
2632 s->name = names;
2633 *names++ = '.';
2634 len = strlen (syms[i]->name);
2635 memcpy (names, syms[i]->name, len + 1);
2636 names += len + 1;
2637 /* Have udata.p point back to the original symbol this
2638 synthetic symbol was derived from. */
2639 s->udata.p = syms[i];
2640 s++;
2641 }
2642 }
2643 free (contents);
2644
2645 if (glink != NULL && relplt != NULL)
2646 {
2647 if (resolv_vma)
2648 {
2649 /* Add a symbol for the main glink trampoline. */
2650 memset (s, 0, sizeof *s);
2651 s->the_bfd = abfd;
2652 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2653 s->section = glink;
2654 s->value = resolv_vma - glink->vma;
2655 s->name = names;
2656 memcpy (names, "__glink_PLTresolve",
2657 sizeof ("__glink_PLTresolve"));
2658 names += sizeof ("__glink_PLTresolve");
2659 s++;
2660 count++;
2661 }
2662
2663 /* FIXME: It would be very much nicer to put sym@plt on the
2664 stub rather than on the glink branch table entry. The
2665 objdump disassembler would then use a sensible symbol
2666 name on plt calls. The difficulty in doing so is
2667 a) finding the stubs, and,
2668 b) matching stubs against plt entries, and,
2669 c) there can be multiple stubs for a given plt entry.
2670
2671 Solving (a) could be done by code scanning, but older
2672 ppc64 binaries used different stubs to current code.
2673 (b) is the tricky one since you need to known the toc
2674 pointer for at least one function that uses a pic stub to
2675 be able to calculate the plt address referenced.
2676 (c) means gdb would need to set multiple breakpoints (or
2677 find the glink branch itself) when setting breakpoints
2678 for pending shared library loads. */
2679 p = relplt->relocation;
2680 for (i = 0; i < plt_count; i++, p++)
2681 {
2682 size_t len;
2683
2684 *s = **p->sym_ptr_ptr;
2685 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2686 we are defining a symbol, ensure one of them is set. */
2687 if ((s->flags & BSF_LOCAL) == 0)
2688 s->flags |= BSF_GLOBAL;
2689 s->flags |= BSF_SYNTHETIC;
2690 s->section = glink;
2691 s->value = glink_vma - glink->vma;
2692 s->name = names;
2693 s->udata.p = NULL;
2694 len = strlen ((*p->sym_ptr_ptr)->name);
2695 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2696 names += len;
2697 if (p->addend != 0)
2698 {
2699 memcpy (names, "+0x", sizeof ("+0x") - 1);
2700 names += sizeof ("+0x") - 1;
2701 bfd_sprintf_vma (abfd, names, p->addend);
2702 names += strlen (names);
2703 }
2704 memcpy (names, "@plt", sizeof ("@plt"));
2705 names += sizeof ("@plt");
2706 s++;
2707 if (abi < 2)
2708 {
2709 glink_vma += 8;
2710 if (i >= 0x8000)
2711 glink_vma += 4;
2712 }
2713 else
2714 glink_vma += 4;
2715 }
2716 count += plt_count;
2717 }
2718 }
2719
2720 done:
2721 free (syms);
2722 return count;
2723}
2724\f
2725/* The following functions are specific to the ELF linker, while
2726 functions above are used generally. Those named ppc64_elf_* are
2727 called by the main ELF linker code. They appear in this file more
2728 or less in the order in which they are called. eg.
2729 ppc64_elf_check_relocs is called early in the link process,
2730 ppc64_elf_finish_dynamic_sections is one of the last functions
2731 called.
2732
2733 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2734 functions have both a function code symbol and a function descriptor
2735 symbol. A call to foo in a relocatable object file looks like:
2736
2737 . .text
2738 . x:
2739 . bl .foo
2740 . nop
2741
2742 The function definition in another object file might be:
2743
2744 . .section .opd
2745 . foo: .quad .foo
2746 . .quad .TOC.@tocbase
2747 . .quad 0
2748 .
2749 . .text
2750 . .foo: blr
2751
2752 When the linker resolves the call during a static link, the branch
2753 unsurprisingly just goes to .foo and the .opd information is unused.
2754 If the function definition is in a shared library, things are a little
2755 different: The call goes via a plt call stub, the opd information gets
2756 copied to the plt, and the linker patches the nop.
2757
2758 . x:
2759 . bl .foo_stub
2760 . ld 2,40(1)
2761 .
2762 .
2763 . .foo_stub:
2764 . std 2,40(1) # in practice, the call stub
2765 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2766 . addi 11,11,Lfoo@toc@l # this is the general idea
2767 . ld 12,0(11)
2768 . ld 2,8(11)
2769 . mtctr 12
2770 . ld 11,16(11)
2771 . bctr
2772 .
2773 . .section .plt
2774 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2775
2776 The "reloc ()" notation is supposed to indicate that the linker emits
2777 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2778 copying.
2779
2780 What are the difficulties here? Well, firstly, the relocations
2781 examined by the linker in check_relocs are against the function code
2782 sym .foo, while the dynamic relocation in the plt is emitted against
2783 the function descriptor symbol, foo. Somewhere along the line, we need
2784 to carefully copy dynamic link information from one symbol to the other.
2785 Secondly, the generic part of the elf linker will make .foo a dynamic
2786 symbol as is normal for most other backends. We need foo dynamic
2787 instead, at least for an application final link. However, when
2788 creating a shared library containing foo, we need to have both symbols
2789 dynamic so that references to .foo are satisfied during the early
2790 stages of linking. Otherwise the linker might decide to pull in a
2791 definition from some other object, eg. a static library.
2792
2793 Update: As of August 2004, we support a new convention. Function
2794 calls may use the function descriptor symbol, ie. "bl foo". This
2795 behaves exactly as "bl .foo". */
2796
2797/* Of those relocs that might be copied as dynamic relocs, this
2798 function selects those that must be copied when linking a shared
2799 library or PIE, even when the symbol is local. */
2800
2801static int
2802must_be_dyn_reloc (struct bfd_link_info *info,
2803 enum elf_ppc64_reloc_type r_type)
2804{
2805 switch (r_type)
2806 {
2807 default:
2808 /* Only relative relocs can be resolved when the object load
2809 address isn't fixed. DTPREL64 is excluded because the
2810 dynamic linker needs to differentiate global dynamic from
2811 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2812 return 1;
2813
2814 case R_PPC64_REL32:
2815 case R_PPC64_REL64:
2816 case R_PPC64_REL30:
2817 case R_PPC64_TOC16:
2818 case R_PPC64_TOC16_DS:
2819 case R_PPC64_TOC16_LO:
2820 case R_PPC64_TOC16_HI:
2821 case R_PPC64_TOC16_HA:
2822 case R_PPC64_TOC16_LO_DS:
2823 return 0;
2824
2825 case R_PPC64_TPREL16:
2826 case R_PPC64_TPREL16_LO:
2827 case R_PPC64_TPREL16_HI:
2828 case R_PPC64_TPREL16_HA:
2829 case R_PPC64_TPREL16_DS:
2830 case R_PPC64_TPREL16_LO_DS:
2831 case R_PPC64_TPREL16_HIGH:
2832 case R_PPC64_TPREL16_HIGHA:
2833 case R_PPC64_TPREL16_HIGHER:
2834 case R_PPC64_TPREL16_HIGHERA:
2835 case R_PPC64_TPREL16_HIGHEST:
2836 case R_PPC64_TPREL16_HIGHESTA:
2837 case R_PPC64_TPREL64:
2838 case R_PPC64_TPREL34:
2839 /* These relocations are relative but in a shared library the
2840 linker doesn't know the thread pointer base. */
2841 return bfd_link_dll (info);
2842 }
2843}
2844
2845/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2846 copying dynamic variables from a shared lib into an app's .dynbss
2847 section, and instead use a dynamic relocation to point into the
2848 shared lib. With code that gcc generates it is vital that this be
2849 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2850 actually the address of a function descriptor which resides in the
2851 .opd section. gcc uses the descriptor directly rather than going
2852 via the GOT as some other ABIs do, which means that initialized
2853 function pointers reference the descriptor. Thus, a function
2854 pointer initialized to the address of a function in a shared
2855 library will either require a .dynbss copy and a copy reloc, or a
2856 dynamic reloc. Using a .dynbss copy redefines the function
2857 descriptor symbol to point to the copy. This presents a problem as
2858 a PLT entry for that function is also initialized from the function
2859 descriptor symbol and the copy may not be initialized first. */
2860#define ELIMINATE_COPY_RELOCS 1
2861
2862/* Section name for stubs is the associated section name plus this
2863 string. */
2864#define STUB_SUFFIX ".stub"
2865
2866/* Linker stubs.
2867 ppc_stub_long_branch:
2868 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2869 destination, but a 24 bit branch in a stub section will reach.
2870 . b dest
2871
2872 ppc_stub_plt_branch:
2873 Similar to the above, but a 24 bit branch in the stub section won't
2874 reach its destination.
2875 . addis %r12,%r2,xxx@toc@ha
2876 . ld %r12,xxx@toc@l(%r12)
2877 . mtctr %r12
2878 . bctr
2879
2880 ppc_stub_plt_call:
2881 Used to call a function in a shared library. If it so happens that
2882 the plt entry referenced crosses a 64k boundary, then an extra
2883 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2884 An r2save variant starts with "std %r2,40(%r1)".
2885 . addis %r11,%r2,xxx@toc@ha
2886 . ld %r12,xxx+0@toc@l(%r11)
2887 . mtctr %r12
2888 . ld %r2,xxx+8@toc@l(%r11)
2889 . ld %r11,xxx+16@toc@l(%r11)
2890 . bctr
2891
2892 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2893 code to adjust the value and save r2 to support multiple toc sections.
2894 A ppc_stub_long_branch with an r2 offset looks like:
2895 . std %r2,40(%r1)
2896 . addis %r2,%r2,off@ha
2897 . addi %r2,%r2,off@l
2898 . b dest
2899
2900 A ppc_stub_plt_branch with an r2 offset looks like:
2901 . std %r2,40(%r1)
2902 . addis %r12,%r2,xxx@toc@ha
2903 . ld %r12,xxx@toc@l(%r12)
2904 . addis %r2,%r2,off@ha
2905 . addi %r2,%r2,off@l
2906 . mtctr %r12
2907 . bctr
2908
2909 All of the above stubs are shown as their ELFv1 variants. ELFv2
2910 variants exist too, simpler for plt calls since a new toc pointer
2911 and static chain are not loaded by the stub. In addition, ELFv2
2912 has some more complex stubs to handle calls marked with NOTOC
2913 relocs from functions where r2 is not a valid toc pointer.
2914 ppc_stub_long_branch_p9notoc:
2915 . mflr %r12
2916 . bcl 20,31,1f
2917 . 1:
2918 . mflr %r11
2919 . mtlr %r12
2920 . addis %r12,%r11,dest-1b@ha
2921 . addi %r12,%r12,dest-1b@l
2922 . b dest
2923
2924 ppc_stub_plt_branch_p9notoc:
2925 . mflr %r12
2926 . bcl 20,31,1f
2927 . 1:
2928 . mflr %r11
2929 . mtlr %r12
2930 . lis %r12,xxx-1b@highest
2931 . ori %r12,%r12,xxx-1b@higher
2932 . sldi %r12,%r12,32
2933 . oris %r12,%r12,xxx-1b@high
2934 . ori %r12,%r12,xxx-1b@l
2935 . add %r12,%r11,%r12
2936 . mtctr %r12
2937 . bctr
2938
2939 ppc_stub_plt_call_p9notoc:
2940 . mflr %r12
2941 . bcl 20,31,1f
2942 . 1:
2943 . mflr %r11
2944 . mtlr %r12
2945 . lis %r12,xxx-1b@highest
2946 . ori %r12,%r12,xxx-1b@higher
2947 . sldi %r12,%r12,32
2948 . oris %r12,%r12,xxx-1b@high
2949 . ori %r12,%r12,xxx-1b@l
2950 . ldx %r12,%r11,%r12
2951 . mtctr %r12
2952 . bctr
2953
2954 There are also ELFv1 power10 variants of these stubs.
2955 ppc_stub_long_branch_notoc:
2956 . pla %r12,dest@pcrel
2957 . b dest
2958 ppc_stub_plt_branch_notoc:
2959 . lis %r11,(dest-1f)@highesta34
2960 . ori %r11,%r11,(dest-1f)@highera34
2961 . sldi %r11,%r11,34
2962 . 1: pla %r12,dest@pcrel
2963 . add %r12,%r11,%r12
2964 . mtctr %r12
2965 . bctr
2966 ppc_stub_plt_call_notoc:
2967 . lis %r11,(xxx-1f)@highesta34
2968 . ori %r11,%r11,(xxx-1f)@highera34
2969 . sldi %r11,%r11,34
2970 . 1: pla %r12,xxx@pcrel
2971 . ldx %r12,%r11,%r12
2972 . mtctr %r12
2973 . bctr
2974
2975 In cases where the high instructions would add zero, they are
2976 omitted and following instructions modified in some cases.
2977 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2978 to
2979 . pld %r12,xxx@pcrel
2980 . mtctr %r12
2981 . bctr
2982
2983 Stub variants may be merged. For example, if printf is called from
2984 code with the tocsave optimization (ie. r2 saved in function
2985 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2986 and from other code without the tocsave optimization requiring a
2987 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2988 type will be created. Calls with the tocsave optimization will
2989 enter this stub after the instruction saving r2. A similar
2990 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2991 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2992 to call an external function like printf. If other calls to printf
2993 require a ppc_stub_plt_call linkage stub then a single
2994 ppc_stub_plt_call_notoc linkage stub may be used for both types of
2995 call. */
2996
2997enum ppc_stub_main_type
2998{
2999 ppc_stub_none,
3000 ppc_stub_long_branch,
3001 ppc_stub_plt_branch,
3002 ppc_stub_plt_call,
3003 ppc_stub_global_entry,
3004 ppc_stub_save_res
3005};
3006
3007/* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3008 these variations. */
3009
3010enum ppc_stub_sub_type
3011{
3012 ppc_stub_toc,
3013 ppc_stub_notoc,
3014 ppc_stub_p9notoc
3015};
3016
3017struct ppc_stub_type
3018{
3019 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3020 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3021 unsigned int r2save : 1;
3022};
3023
3024/* Information on stub grouping. */
3025struct map_stub
3026{
3027 /* The stub section. */
3028 asection *stub_sec;
3029 /* This is the section to which stubs in the group will be attached. */
3030 asection *link_sec;
3031 /* Next group. */
3032 struct map_stub *next;
3033 /* Whether to emit a copy of register save/restore functions in this
3034 group. */
3035 int needs_save_res;
3036 /* Current offset within stubs after the insn restoring lr in a
3037 _notoc or _both stub using bcl for pc-relative addressing, or
3038 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3039 unsigned int lr_restore;
3040 /* Accumulated size of EH info emitted to describe return address
3041 if stubs modify lr. Does not include 17 byte FDE header. */
3042 unsigned int eh_size;
3043 /* Offset in glink_eh_frame to the start of EH info for this group. */
3044 unsigned int eh_base;
3045};
3046
3047struct ppc_stub_hash_entry
3048{
3049 /* Base hash table entry structure. */
3050 struct bfd_hash_entry root;
3051
3052 struct ppc_stub_type type;
3053
3054 /* Group information. */
3055 struct map_stub *group;
3056
3057 /* Offset within stub_sec of the beginning of this stub. */
3058 bfd_vma stub_offset;
3059
3060 /* Given the symbol's value and its section we can determine its final
3061 value when building the stubs (so the stub knows where to jump. */
3062 bfd_vma target_value;
3063 asection *target_section;
3064
3065 /* The symbol table entry, if any, that this was derived from. */
3066 struct ppc_link_hash_entry *h;
3067 struct plt_entry *plt_ent;
3068
3069 /* Symbol type. */
3070 unsigned char symtype;
3071
3072 /* Symbol st_other. */
3073 unsigned char other;
3074
3075 /* Debug: Track hash table traversal. */
3076 unsigned int id;
3077};
3078
3079struct ppc_branch_hash_entry
3080{
3081 /* Base hash table entry structure. */
3082 struct bfd_hash_entry root;
3083
3084 /* Offset within branch lookup table. */
3085 unsigned int offset;
3086
3087 /* Generation marker. */
3088 unsigned int iter;
3089};
3090
3091/* Used to track dynamic relocations. */
3092struct ppc_dyn_relocs
3093{
3094 struct ppc_dyn_relocs *next;
3095
3096 /* The input section of the reloc. */
3097 asection *sec;
3098
3099 /* Total number of relocs copied for the input section. */
3100 unsigned int count;
3101
3102 /* Number of pc-relative relocs copied for the input section. */
3103 unsigned int pc_count;
3104
3105 /* Number of relocs that might become R_PPC64_RELATIVE. */
3106 unsigned int rel_count;
3107};
3108
3109struct ppc_local_dyn_relocs
3110{
3111 struct ppc_local_dyn_relocs *next;
3112
3113 /* The input section of the reloc. */
3114 asection *sec;
3115
3116 /* Total number of relocs copied for the input section. */
3117 unsigned int count;
3118
3119 /* Number of relocs that might become R_PPC64_RELATIVE. */
3120 unsigned int rel_count : 31;
3121
3122 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3123 unsigned int ifunc : 1;
3124};
3125
3126struct ppc_link_hash_entry
3127{
3128 struct elf_link_hash_entry elf;
3129
3130 union
3131 {
3132 /* A pointer to the most recently used stub hash entry against this
3133 symbol. */
3134 struct ppc_stub_hash_entry *stub_cache;
3135
3136 /* A pointer to the next symbol starting with a '.' */
3137 struct ppc_link_hash_entry *next_dot_sym;
3138 } u;
3139
3140 /* Link between function code and descriptor symbols. */
3141 struct ppc_link_hash_entry *oh;
3142
3143 /* Flag function code and descriptor symbols. */
3144 unsigned int is_func:1;
3145 unsigned int is_func_descriptor:1;
3146 unsigned int fake:1;
3147
3148 /* Whether global opd/toc sym has been adjusted or not.
3149 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3150 should be set for all globals defined in any opd/toc section. */
3151 unsigned int adjust_done:1;
3152
3153 /* Set if this is an out-of-line register save/restore function,
3154 with non-standard calling convention. */
3155 unsigned int save_res:1;
3156
3157 /* Set if a duplicate symbol with non-zero localentry is detected,
3158 even when the duplicate symbol does not provide a definition. */
3159 unsigned int non_zero_localentry:1;
3160
3161 /* Contexts in which symbol is used in the GOT (or TOC).
3162 Bits are or'd into the mask as the corresponding relocs are
3163 encountered during check_relocs, with TLS_TLS being set when any
3164 of the other TLS bits are set. tls_optimize clears bits when
3165 optimizing to indicate the corresponding GOT entry type is not
3166 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3167 set TLS_GDIE when a GD reloc turns into an IE one.
3168 These flags are also kept for local symbols. */
3169#define TLS_TLS 1 /* Any TLS reloc. */
3170#define TLS_GD 2 /* GD reloc. */
3171#define TLS_LD 4 /* LD reloc. */
3172#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3173#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3174#define TLS_MARK 32 /* __tls_get_addr call marked. */
3175#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3176#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3177 unsigned char tls_mask;
3178
3179 /* The above field is also used to mark function symbols. In which
3180 case TLS_TLS will be 0. */
3181#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3182#define PLT_KEEP 4 /* inline plt call requires plt entry. */
3183#define NON_GOT 256 /* local symbol plt, not stored. */
3184};
3185
3186static inline struct ppc_link_hash_entry *
3187ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3188{
3189 return (struct ppc_link_hash_entry *) ent;
3190}
3191
3192static inline struct elf_link_hash_entry *
3193elf_hash_entry (struct ppc_link_hash_entry *ent)
3194{
3195 return (struct elf_link_hash_entry *) ent;
3196}
3197
3198/* ppc64 ELF linker hash table. */
3199
3200struct ppc_link_hash_table
3201{
3202 struct elf_link_hash_table elf;
3203
3204 /* The stub hash table. */
3205 struct bfd_hash_table stub_hash_table;
3206
3207 /* Another hash table for plt_branch stubs. */
3208 struct bfd_hash_table branch_hash_table;
3209
3210 /* Hash table for function prologue tocsave. */
3211 htab_t tocsave_htab;
3212
3213 /* Various options and other info passed from the linker. */
3214 struct ppc64_elf_params *params;
3215
3216 /* The size of sec_info below. */
3217 unsigned int sec_info_arr_size;
3218
3219 /* Per-section array of extra section info. Done this way rather
3220 than as part of ppc64_elf_section_data so we have the info for
3221 non-ppc64 sections. */
3222 struct
3223 {
3224 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3225 bfd_vma toc_off;
3226
3227 union
3228 {
3229 /* The section group that this section belongs to. */
3230 struct map_stub *group;
3231 /* A temp section list pointer. */
3232 asection *list;
3233 } u;
3234 } *sec_info;
3235
3236 /* Linked list of groups. */
3237 struct map_stub *group;
3238
3239 /* Temp used when calculating TOC pointers. */
3240 bfd_vma toc_curr;
3241 bfd *toc_bfd;
3242 asection *toc_first_sec;
3243
3244 /* Used when adding symbols. */
3245 struct ppc_link_hash_entry *dot_syms;
3246
3247 /* Shortcuts to get to dynamic linker sections. */
3248 asection *glink;
3249 asection *global_entry;
3250 asection *sfpr;
3251 asection *pltlocal;
3252 asection *relpltlocal;
3253 asection *brlt;
3254 asection *relbrlt;
3255 asection *glink_eh_frame;
3256
3257 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3258 struct ppc_link_hash_entry *tls_get_addr;
3259 struct ppc_link_hash_entry *tls_get_addr_fd;
3260 struct ppc_link_hash_entry *tga_desc;
3261 struct ppc_link_hash_entry *tga_desc_fd;
3262 struct map_stub *tga_group;
3263
3264 /* The size of reliplt used by got entry relocs. */
3265 bfd_size_type got_reli_size;
3266
3267 /* DT_RELR array of section/r_offset. */
3268 size_t relr_alloc;
3269 size_t relr_count;
3270 struct
3271 {
3272 asection *sec;
3273 bfd_vma off;
3274 } *relr;
3275
3276 /* Statistics. */
3277 unsigned long stub_count[ppc_stub_save_res];
3278
3279 /* Number of stubs against global syms. */
3280 unsigned long stub_globals;
3281
3282 /* Set if we're linking code with function descriptors. */
3283 unsigned int opd_abi:1;
3284
3285 /* Support for multiple toc sections. */
3286 unsigned int do_multi_toc:1;
3287 unsigned int multi_toc_needed:1;
3288 unsigned int second_toc_pass:1;
3289 unsigned int do_toc_opt:1;
3290
3291 /* Set if tls optimization is enabled. */
3292 unsigned int do_tls_opt:1;
3293
3294 /* Set if inline plt calls should be converted to direct calls. */
3295 unsigned int can_convert_all_inline_plt:1;
3296
3297 /* Set if a stub_offset changed. */
3298 unsigned int stub_changed:1;
3299
3300 /* Set on error. */
3301 unsigned int stub_error:1;
3302
3303 /* Whether func_desc_adjust needs to be run over symbols. */
3304 unsigned int need_func_desc_adj:1;
3305
3306 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3307 unsigned int has_plt_localentry0:1;
3308
3309 /* Whether calls are made via the PLT from NOTOC functions. */
3310 unsigned int notoc_plt:1;
3311
3312 /* Whether any code linked seems to be Power10. */
3313 unsigned int has_power10_relocs:1;
3314
3315 /* Incremented once for each stub sized. */
3316 unsigned int stub_id;
3317
3318 /* Incremented every time we size stubs. */
3319 unsigned int stub_iteration;
3320
3321/* After 20 iterations of stub sizing we no longer allow stubs to
3322 shrink. This is to break out of a pathological case where adding
3323 stubs or increasing their size on one iteration decreases section
3324 gaps (perhaps due to alignment), which then results in smaller
3325 stubs on the next iteration. */
3326#define STUB_SHRINK_ITER 20
3327};
3328
3329/* Rename some of the generic section flags to better document how they
3330 are used here. */
3331
3332/* Nonzero if this section has TLS related relocations. */
3333#define has_tls_reloc sec_flg0
3334
3335/* Nonzero if this section has a call to __tls_get_addr lacking marker
3336 relocations. */
3337#define nomark_tls_get_addr sec_flg1
3338
3339/* Nonzero if this section has any toc or got relocs. */
3340#define has_toc_reloc sec_flg2
3341
3342/* Nonzero if this section has a call to another section that uses
3343 the toc or got. */
3344#define makes_toc_func_call sec_flg3
3345
3346/* Recursion protection when determining above flag. */
3347#define call_check_in_progress sec_flg4
3348#define call_check_done sec_flg5
3349
3350/* Get the ppc64 ELF linker hash table from a link_info structure. */
3351
3352#define ppc_hash_table(p) \
3353 ((is_elf_hash_table ((p)->hash) \
3354 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3355 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3356
3357#define ppc_stub_hash_lookup(table, string, create, copy) \
3358 ((struct ppc_stub_hash_entry *) \
3359 bfd_hash_lookup ((table), (string), (create), (copy)))
3360
3361#define ppc_branch_hash_lookup(table, string, create, copy) \
3362 ((struct ppc_branch_hash_entry *) \
3363 bfd_hash_lookup ((table), (string), (create), (copy)))
3364
3365/* Create an entry in the stub hash table. */
3366
3367static struct bfd_hash_entry *
3368stub_hash_newfunc (struct bfd_hash_entry *entry,
3369 struct bfd_hash_table *table,
3370 const char *string)
3371{
3372 /* Allocate the structure if it has not already been allocated by a
3373 subclass. */
3374 if (entry == NULL)
3375 {
3376 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3377 if (entry == NULL)
3378 return entry;
3379 }
3380
3381 /* Call the allocation method of the superclass. */
3382 entry = bfd_hash_newfunc (entry, table, string);
3383 if (entry != NULL)
3384 {
3385 struct ppc_stub_hash_entry *eh;
3386
3387 /* Initialize the local fields. */
3388 eh = (struct ppc_stub_hash_entry *) entry;
3389 eh->type.main = ppc_stub_none;
3390 eh->type.sub = ppc_stub_toc;
3391 eh->type.r2save = 0;
3392 eh->group = NULL;
3393 eh->stub_offset = 0;
3394 eh->target_value = 0;
3395 eh->target_section = NULL;
3396 eh->h = NULL;
3397 eh->plt_ent = NULL;
3398 eh->symtype = 0;
3399 eh->other = 0;
3400 eh->id = 0;
3401 }
3402
3403 return entry;
3404}
3405
3406/* Create an entry in the branch hash table. */
3407
3408static struct bfd_hash_entry *
3409branch_hash_newfunc (struct bfd_hash_entry *entry,
3410 struct bfd_hash_table *table,
3411 const char *string)
3412{
3413 /* Allocate the structure if it has not already been allocated by a
3414 subclass. */
3415 if (entry == NULL)
3416 {
3417 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3418 if (entry == NULL)
3419 return entry;
3420 }
3421
3422 /* Call the allocation method of the superclass. */
3423 entry = bfd_hash_newfunc (entry, table, string);
3424 if (entry != NULL)
3425 {
3426 struct ppc_branch_hash_entry *eh;
3427
3428 /* Initialize the local fields. */
3429 eh = (struct ppc_branch_hash_entry *) entry;
3430 eh->offset = 0;
3431 eh->iter = 0;
3432 }
3433
3434 return entry;
3435}
3436
3437/* Create an entry in a ppc64 ELF linker hash table. */
3438
3439static struct bfd_hash_entry *
3440link_hash_newfunc (struct bfd_hash_entry *entry,
3441 struct bfd_hash_table *table,
3442 const char *string)
3443{
3444 /* Allocate the structure if it has not already been allocated by a
3445 subclass. */
3446 if (entry == NULL)
3447 {
3448 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3449 if (entry == NULL)
3450 return entry;
3451 }
3452
3453 /* Call the allocation method of the superclass. */
3454 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3455 if (entry != NULL)
3456 {
3457 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3458
3459 memset (&eh->u.stub_cache, 0,
3460 (sizeof (struct ppc_link_hash_entry)
3461 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3462
3463 /* When making function calls, old ABI code references function entry
3464 points (dot symbols), while new ABI code references the function
3465 descriptor symbol. We need to make any combination of reference and
3466 definition work together, without breaking archive linking.
3467
3468 For a defined function "foo" and an undefined call to "bar":
3469 An old object defines "foo" and ".foo", references ".bar" (possibly
3470 "bar" too).
3471 A new object defines "foo" and references "bar".
3472
3473 A new object thus has no problem with its undefined symbols being
3474 satisfied by definitions in an old object. On the other hand, the
3475 old object won't have ".bar" satisfied by a new object.
3476
3477 Keep a list of newly added dot-symbols. */
3478
3479 if (string[0] == '.')
3480 {
3481 struct ppc_link_hash_table *htab;
3482
3483 htab = (struct ppc_link_hash_table *) table;
3484 eh->u.next_dot_sym = htab->dot_syms;
3485 htab->dot_syms = eh;
3486 }
3487 }
3488
3489 return entry;
3490}
3491
3492struct tocsave_entry
3493{
3494 asection *sec;
3495 bfd_vma offset;
3496};
3497
3498static hashval_t
3499tocsave_htab_hash (const void *p)
3500{
3501 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3502 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3503}
3504
3505static int
3506tocsave_htab_eq (const void *p1, const void *p2)
3507{
3508 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3509 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3510 return e1->sec == e2->sec && e1->offset == e2->offset;
3511}
3512
3513/* Destroy a ppc64 ELF linker hash table. */
3514
3515static void
3516ppc64_elf_link_hash_table_free (bfd *obfd)
3517{
3518 struct ppc_link_hash_table *htab;
3519
3520 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3521 free (htab->relr);
3522 if (htab->tocsave_htab)
3523 htab_delete (htab->tocsave_htab);
3524 bfd_hash_table_free (&htab->branch_hash_table);
3525 bfd_hash_table_free (&htab->stub_hash_table);
3526 _bfd_elf_link_hash_table_free (obfd);
3527}
3528
3529/* Create a ppc64 ELF linker hash table. */
3530
3531static struct bfd_link_hash_table *
3532ppc64_elf_link_hash_table_create (bfd *abfd)
3533{
3534 struct ppc_link_hash_table *htab;
3535 size_t amt = sizeof (struct ppc_link_hash_table);
3536
3537 htab = bfd_zmalloc (amt);
3538 if (htab == NULL)
3539 return NULL;
3540
3541 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3542 sizeof (struct ppc_link_hash_entry)))
3543 {
3544 free (htab);
3545 return NULL;
3546 }
3547
3548 /* Init the stub hash table too. */
3549 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3550 sizeof (struct ppc_stub_hash_entry)))
3551 {
3552 _bfd_elf_link_hash_table_free (abfd);
3553 return NULL;
3554 }
3555
3556 /* And the branch hash table. */
3557 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3558 sizeof (struct ppc_branch_hash_entry)))
3559 {
3560 bfd_hash_table_free (&htab->stub_hash_table);
3561 _bfd_elf_link_hash_table_free (abfd);
3562 return NULL;
3563 }
3564
3565 htab->tocsave_htab = htab_try_create (1024,
3566 tocsave_htab_hash,
3567 tocsave_htab_eq,
3568 NULL);
3569 if (htab->tocsave_htab == NULL)
3570 {
3571 ppc64_elf_link_hash_table_free (abfd);
3572 return NULL;
3573 }
3574 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3575
3576 /* Initializing two fields of the union is just cosmetic. We really
3577 only care about glist, but when compiled on a 32-bit host the
3578 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3579 debugger inspection of these fields look nicer. */
3580 htab->elf.init_got_refcount.refcount = 0;
3581 htab->elf.init_got_refcount.glist = NULL;
3582 htab->elf.init_plt_refcount.refcount = 0;
3583 htab->elf.init_plt_refcount.glist = NULL;
3584 htab->elf.init_got_offset.offset = 0;
3585 htab->elf.init_got_offset.glist = NULL;
3586 htab->elf.init_plt_offset.offset = 0;
3587 htab->elf.init_plt_offset.glist = NULL;
3588
3589 return &htab->elf.root;
3590}
3591
3592/* Create sections for linker generated code. */
3593
3594static bool
3595create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3596{
3597 struct ppc_link_hash_table *htab;
3598 flagword flags;
3599
3600 htab = ppc_hash_table (info);
3601
3602 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3603 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3604 if (htab->params->save_restore_funcs)
3605 {
3606 /* Create .sfpr for code to save and restore fp regs. */
3607 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3608 flags);
3609 if (htab->sfpr == NULL
3610 || !bfd_set_section_alignment (htab->sfpr, 2))
3611 return false;
3612 }
3613
3614 if (bfd_link_relocatable (info))
3615 return true;
3616
3617 /* Create .glink for lazy dynamic linking support. */
3618 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3619 flags);
3620 if (htab->glink == NULL
3621 || !bfd_set_section_alignment (htab->glink, 3))
3622 return false;
3623
3624 /* The part of .glink used by global entry stubs, separate so that
3625 it can be aligned appropriately without affecting htab->glink. */
3626 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3627 flags);
3628 if (htab->global_entry == NULL
3629 || !bfd_set_section_alignment (htab->global_entry, 2))
3630 return false;
3631
3632 if (!info->no_ld_generated_unwind_info)
3633 {
3634 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3635 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3636 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3637 ".eh_frame",
3638 flags);
3639 if (htab->glink_eh_frame == NULL
3640 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3641 return false;
3642 }
3643
3644 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3645 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3646 if (htab->elf.iplt == NULL
3647 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3648 return false;
3649
3650 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3651 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3652 htab->elf.irelplt
3653 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3654 if (htab->elf.irelplt == NULL
3655 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3656 return false;
3657
3658 /* Create branch lookup table for plt_branch stubs. */
3659 flags = (SEC_ALLOC | SEC_LOAD
3660 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3661 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3662 flags);
3663 if (htab->brlt == NULL
3664 || !bfd_set_section_alignment (htab->brlt, 3))
3665 return false;
3666
3667 /* Local plt entries, put in .branch_lt but a separate section for
3668 convenience. */
3669 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3670 flags);
3671 if (htab->pltlocal == NULL
3672 || !bfd_set_section_alignment (htab->pltlocal, 3))
3673 return false;
3674
3675 if (!bfd_link_pic (info))
3676 return true;
3677
3678 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3679 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3680 htab->relbrlt
3681 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3682 if (htab->relbrlt == NULL
3683 || !bfd_set_section_alignment (htab->relbrlt, 3))
3684 return false;
3685
3686 htab->relpltlocal
3687 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3688 if (htab->relpltlocal == NULL
3689 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3690 return false;
3691
3692 return true;
3693}
3694
3695/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3696
3697bool
3698ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3699 struct ppc64_elf_params *params)
3700{
3701 struct ppc_link_hash_table *htab;
3702
3703 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3704
3705/* Always hook our dynamic sections into the first bfd, which is the
3706 linker created stub bfd. This ensures that the GOT header is at
3707 the start of the output TOC section. */
3708 htab = ppc_hash_table (info);
3709 htab->elf.dynobj = params->stub_bfd;
3710 htab->params = params;
3711
3712 return create_linkage_sections (htab->elf.dynobj, info);
3713}
3714
3715/* Build a name for an entry in the stub hash table. */
3716
3717static char *
3718ppc_stub_name (const asection *input_section,
3719 const asection *sym_sec,
3720 const struct ppc_link_hash_entry *h,
3721 const Elf_Internal_Rela *rel)
3722{
3723 char *stub_name;
3724 ssize_t len;
3725
3726 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3727 offsets from a sym as a branch target? In fact, we could
3728 probably assume the addend is always zero. */
3729 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3730
3731 if (h)
3732 {
3733 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3734 stub_name = bfd_malloc (len);
3735 if (stub_name == NULL)
3736 return stub_name;
3737
3738 len = sprintf (stub_name, "%08x.%s+%x",
3739 input_section->id & 0xffffffff,
3740 h->elf.root.root.string,
3741 (int) rel->r_addend & 0xffffffff);
3742 }
3743 else
3744 {
3745 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3746 stub_name = bfd_malloc (len);
3747 if (stub_name == NULL)
3748 return stub_name;
3749
3750 len = sprintf (stub_name, "%08x.%x:%x+%x",
3751 input_section->id & 0xffffffff,
3752 sym_sec->id & 0xffffffff,
3753 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3754 (int) rel->r_addend & 0xffffffff);
3755 }
3756 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3757 stub_name[len - 2] = 0;
3758 return stub_name;
3759}
3760
3761/* If mixing power10 with non-power10 code and --power10-stubs is not
3762 specified (or is auto) then there may be multiple stub types for any
3763 given symbol. Up to three classes of stubs are stored in separate
3764 stub_hash_table entries having the same key string. The entries
3765 will always be adjacent on entry->root.next chain, even if hash
3766 table resizing occurs. This function selects the correct entry to
3767 use. */
3768
3769static struct ppc_stub_hash_entry *
3770select_alt_stub (struct ppc_stub_hash_entry *entry,
3771 enum elf_ppc64_reloc_type r_type)
3772{
3773 enum ppc_stub_sub_type subt;
3774
3775 switch (r_type)
3776 {
3777 case R_PPC64_REL24_NOTOC:
3778 subt = ppc_stub_notoc;
3779 break;
3780 case R_PPC64_REL24_P9NOTOC:
3781 subt = ppc_stub_p9notoc;
3782 break;
3783 default:
3784 subt = ppc_stub_toc;
3785 break;
3786 }
3787
3788 while (entry != NULL && entry->type.sub != subt)
3789 {
3790 const char *stub_name = entry->root.string;
3791
3792 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3793 if (entry != NULL
3794 && entry->root.string != stub_name)
3795 entry = NULL;
3796 }
3797
3798 return entry;
3799}
3800
3801/* Look up an entry in the stub hash. Stub entries are cached because
3802 creating the stub name takes a bit of time. */
3803
3804static struct ppc_stub_hash_entry *
3805ppc_get_stub_entry (const asection *input_section,
3806 const asection *sym_sec,
3807 struct ppc_link_hash_entry *h,
3808 const Elf_Internal_Rela *rel,
3809 struct ppc_link_hash_table *htab)
3810{
3811 struct ppc_stub_hash_entry *stub_entry;
3812 struct map_stub *group;
3813
3814 /* If this input section is part of a group of sections sharing one
3815 stub section, then use the id of the first section in the group.
3816 Stub names need to include a section id, as there may well be
3817 more than one stub used to reach say, printf, and we need to
3818 distinguish between them. */
3819 group = htab->sec_info[input_section->id].u.group;
3820 if (group == NULL)
3821 return NULL;
3822
3823 if (h != NULL && h->u.stub_cache != NULL
3824 && h->u.stub_cache->h == h
3825 && h->u.stub_cache->group == group)
3826 {
3827 stub_entry = h->u.stub_cache;
3828 }
3829 else
3830 {
3831 char *stub_name;
3832
3833 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3834 if (stub_name == NULL)
3835 return NULL;
3836
3837 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3838 stub_name, false, false);
3839 if (h != NULL)
3840 h->u.stub_cache = stub_entry;
3841
3842 free (stub_name);
3843 }
3844
3845 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3846 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3847
3848 return stub_entry;
3849}
3850
3851/* Add a new stub entry to the stub hash. Not all fields of the new
3852 stub entry are initialised. */
3853
3854static struct ppc_stub_hash_entry *
3855ppc_add_stub (const char *stub_name,
3856 asection *section,
3857 struct bfd_link_info *info)
3858{
3859 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3860 struct map_stub *group;
3861 asection *link_sec;
3862 asection *stub_sec;
3863 struct ppc_stub_hash_entry *stub_entry;
3864
3865 group = htab->sec_info[section->id].u.group;
3866 link_sec = group->link_sec;
3867 stub_sec = group->stub_sec;
3868 if (stub_sec == NULL)
3869 {
3870 size_t namelen;
3871 bfd_size_type len;
3872 char *s_name;
3873
3874 namelen = strlen (link_sec->name);
3875 len = namelen + sizeof (STUB_SUFFIX);
3876 s_name = bfd_alloc (htab->params->stub_bfd, len);
3877 if (s_name == NULL)
3878 return NULL;
3879
3880 memcpy (s_name, link_sec->name, namelen);
3881 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3882 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3883 if (stub_sec == NULL)
3884 return NULL;
3885 group->stub_sec = stub_sec;
3886 }
3887
3888 /* Enter this entry into the linker stub hash table. */
3889 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3890 true, true);
3891 if (stub_entry == NULL)
3892 {
3893 /* xgettext:c-format */
3894 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3895 section->owner, stub_name);
3896 return NULL;
3897 }
3898
3899 stub_entry->group = group;
3900 stub_entry->stub_offset = 0;
3901 return stub_entry;
3902}
3903
3904/* A stub has already been created, but it may not be the required
3905 type. We shouldn't be transitioning from plt_call to long_branch
3906 stubs or vice versa, but we might be upgrading from plt_call to
3907 plt_call with r2save for example. */
3908
3909static bool
3910ppc_merge_stub (struct ppc_link_hash_table *htab,
3911 struct ppc_stub_hash_entry *stub_entry,
3912 struct ppc_stub_type stub_type,
3913 enum elf_ppc64_reloc_type r_type)
3914{
3915 struct ppc_stub_type old_type = stub_entry->type;
3916
3917 if (old_type.main == ppc_stub_save_res)
3918 return true;
3919
3920 if (htab->params->power10_stubs == -1)
3921 {
3922 /* For --power10-stubs=auto, don't merge _notoc and other
3923 varieties of stubs. */
3924 struct ppc_stub_hash_entry *alt_stub;
3925
3926 alt_stub = select_alt_stub (stub_entry, r_type);
3927 if (alt_stub == NULL)
3928 {
3929 alt_stub = ((struct ppc_stub_hash_entry *)
3930 stub_hash_newfunc (NULL,
3931 &htab->stub_hash_table,
3932 stub_entry->root.string));
3933 if (alt_stub == NULL)
3934 return false;
3935
3936 *alt_stub = *stub_entry;
3937 stub_entry->root.next = &alt_stub->root;
3938
3939 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3940 Not that it matters, this just puts smaller stubs first. */
3941 if (stub_type.sub == ppc_stub_notoc)
3942 alt_stub = stub_entry;
3943 else if (stub_type.sub == ppc_stub_p9notoc
3944 && alt_stub->root.next
3945 && alt_stub->root.next->string == alt_stub->root.string)
3946 {
3947 struct ppc_stub_hash_entry *next
3948 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3949 alt_stub->type = next->type;
3950 alt_stub = next;
3951 }
3952 alt_stub->type = stub_type;
3953 return true;
3954 }
3955 stub_entry = alt_stub;
3956 }
3957
3958 old_type = stub_entry->type;
3959 if (old_type.main == ppc_stub_plt_branch)
3960 old_type.main = ppc_stub_long_branch;
3961
3962 if (old_type.main != stub_type.main
3963 || (old_type.sub != stub_type.sub
3964 && old_type.sub != ppc_stub_toc
3965 && stub_type.sub != ppc_stub_toc))
3966 abort ();
3967
3968 stub_entry->type.sub |= stub_type.sub;
3969 stub_entry->type.r2save |= stub_type.r2save;
3970 return true;
3971}
3972
3973/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3974 not already done. */
3975
3976static bool
3977create_got_section (bfd *abfd, struct bfd_link_info *info)
3978{
3979 asection *got, *relgot;
3980 flagword flags;
3981 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3982
3983 if (!is_ppc64_elf (abfd))
3984 return false;
3985 if (htab == NULL)
3986 return false;
3987
3988 if (!htab->elf.sgot
3989 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3990 return false;
3991
3992 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3993 | SEC_LINKER_CREATED);
3994
3995 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3996 if (!got
3997 || !bfd_set_section_alignment (got, 3))
3998 return false;
3999
4000 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4001 flags | SEC_READONLY);
4002 if (!relgot
4003 || !bfd_set_section_alignment (relgot, 3))
4004 return false;
4005
4006 ppc64_elf_tdata (abfd)->got = got;
4007 ppc64_elf_tdata (abfd)->relgot = relgot;
4008 return true;
4009}
4010
4011/* Follow indirect and warning symbol links. */
4012
4013static inline struct bfd_link_hash_entry *
4014follow_link (struct bfd_link_hash_entry *h)
4015{
4016 while (h->type == bfd_link_hash_indirect
4017 || h->type == bfd_link_hash_warning)
4018 h = h->u.i.link;
4019 return h;
4020}
4021
4022static inline struct elf_link_hash_entry *
4023elf_follow_link (struct elf_link_hash_entry *h)
4024{
4025 return (struct elf_link_hash_entry *) follow_link (&h->root);
4026}
4027
4028static inline struct ppc_link_hash_entry *
4029ppc_follow_link (struct ppc_link_hash_entry *h)
4030{
4031 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4032}
4033
4034/* Merge PLT info on FROM with that on TO. */
4035
4036static void
4037move_plt_plist (struct ppc_link_hash_entry *from,
4038 struct ppc_link_hash_entry *to)
4039{
4040 if (from->elf.plt.plist != NULL)
4041 {
4042 if (to->elf.plt.plist != NULL)
4043 {
4044 struct plt_entry **entp;
4045 struct plt_entry *ent;
4046
4047 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4048 {
4049 struct plt_entry *dent;
4050
4051 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4052 if (dent->addend == ent->addend)
4053 {
4054 dent->plt.refcount += ent->plt.refcount;
4055 *entp = ent->next;
4056 break;
4057 }
4058 if (dent == NULL)
4059 entp = &ent->next;
4060 }
4061 *entp = to->elf.plt.plist;
4062 }
4063
4064 to->elf.plt.plist = from->elf.plt.plist;
4065 from->elf.plt.plist = NULL;
4066 }
4067}
4068
4069/* Copy the extra info we tack onto an elf_link_hash_entry. */
4070
4071static void
4072ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4073 struct elf_link_hash_entry *dir,
4074 struct elf_link_hash_entry *ind)
4075{
4076 struct ppc_link_hash_entry *edir, *eind;
4077
4078 edir = ppc_elf_hash_entry (dir);
4079 eind = ppc_elf_hash_entry (ind);
4080
4081 edir->is_func |= eind->is_func;
4082 edir->is_func_descriptor |= eind->is_func_descriptor;
4083 edir->tls_mask |= eind->tls_mask;
4084 if (eind->oh != NULL)
4085 edir->oh = ppc_follow_link (eind->oh);
4086
4087 if (edir->elf.versioned != versioned_hidden)
4088 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4089 edir->elf.ref_regular |= eind->elf.ref_regular;
4090 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4091 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4092 edir->elf.needs_plt |= eind->elf.needs_plt;
4093 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4094
4095 /* If we were called to copy over info for a weak sym, don't copy
4096 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4097 in order to simplify readonly_dynrelocs and save a field in the
4098 symbol hash entry, but that means dyn_relocs can't be used in any
4099 tests about a specific symbol, or affect other symbol flags which
4100 are then tested. */
4101 if (eind->elf.root.type != bfd_link_hash_indirect)
4102 return;
4103
4104 /* Copy over any dynamic relocs we may have on the indirect sym. */
4105 if (ind->dyn_relocs != NULL)
4106 {
4107 if (dir->dyn_relocs != NULL)
4108 {
4109 struct ppc_dyn_relocs **pp;
4110 struct ppc_dyn_relocs *p;
4111
4112 /* Add reloc counts against the indirect sym to the direct sym
4113 list. Merge any entries against the same section. */
4114 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4115 (p = *pp) != NULL;
4116 )
4117 {
4118 struct ppc_dyn_relocs *q;
4119
4120 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4121 q != NULL;
4122 q = q->next)
4123 if (q->sec == p->sec)
4124 {
4125 q->count += p->count;
4126 q->pc_count += p->pc_count;
4127 q->rel_count += p->rel_count;
4128 *pp = p->next;
4129 break;
4130 }
4131 if (q == NULL)
4132 pp = &p->next;
4133 }
4134 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4135 }
4136
4137 dir->dyn_relocs = ind->dyn_relocs;
4138 ind->dyn_relocs = NULL;
4139 }
4140
4141 /* Copy over got entries that we may have already seen to the
4142 symbol which just became indirect. */
4143 if (eind->elf.got.glist != NULL)
4144 {
4145 if (edir->elf.got.glist != NULL)
4146 {
4147 struct got_entry **entp;
4148 struct got_entry *ent;
4149
4150 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4151 {
4152 struct got_entry *dent;
4153
4154 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4155 if (dent->addend == ent->addend
4156 && dent->owner == ent->owner
4157 && dent->tls_type == ent->tls_type)
4158 {
4159 dent->got.refcount += ent->got.refcount;
4160 *entp = ent->next;
4161 break;
4162 }
4163 if (dent == NULL)
4164 entp = &ent->next;
4165 }
4166 *entp = edir->elf.got.glist;
4167 }
4168
4169 edir->elf.got.glist = eind->elf.got.glist;
4170 eind->elf.got.glist = NULL;
4171 }
4172
4173 /* And plt entries. */
4174 move_plt_plist (eind, edir);
4175
4176 if (eind->elf.dynindx != -1)
4177 {
4178 if (edir->elf.dynindx != -1)
4179 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4180 edir->elf.dynstr_index);
4181 edir->elf.dynindx = eind->elf.dynindx;
4182 edir->elf.dynstr_index = eind->elf.dynstr_index;
4183 eind->elf.dynindx = -1;
4184 eind->elf.dynstr_index = 0;
4185 }
4186}
4187
4188/* Find the function descriptor hash entry from the given function code
4189 hash entry FH. Link the entries via their OH fields. */
4190
4191static struct ppc_link_hash_entry *
4192lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4193{
4194 struct ppc_link_hash_entry *fdh = fh->oh;
4195
4196 if (fdh == NULL)
4197 {
4198 const char *fd_name = fh->elf.root.root.string + 1;
4199
4200 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4201 false, false, false));
4202 if (fdh == NULL)
4203 return fdh;
4204
4205 fdh->is_func_descriptor = 1;
4206 fdh->oh = fh;
4207 fh->is_func = 1;
4208 fh->oh = fdh;
4209 }
4210
4211 fdh = ppc_follow_link (fdh);
4212 fdh->is_func_descriptor = 1;
4213 fdh->oh = fh;
4214 return fdh;
4215}
4216
4217/* Make a fake function descriptor sym for the undefined code sym FH. */
4218
4219static struct ppc_link_hash_entry *
4220make_fdh (struct bfd_link_info *info,
4221 struct ppc_link_hash_entry *fh)
4222{
4223 bfd *abfd = fh->elf.root.u.undef.abfd;
4224 struct bfd_link_hash_entry *bh = NULL;
4225 struct ppc_link_hash_entry *fdh;
4226 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4227 ? BSF_WEAK
4228 : BSF_GLOBAL);
4229
4230 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4231 fh->elf.root.root.string + 1,
4232 flags, bfd_und_section_ptr, 0,
4233 NULL, false, false, &bh))
4234 return NULL;
4235
4236 fdh = (struct ppc_link_hash_entry *) bh;
4237 fdh->elf.non_elf = 0;
4238 fdh->fake = 1;
4239 fdh->is_func_descriptor = 1;
4240 fdh->oh = fh;
4241 fh->is_func = 1;
4242 fh->oh = fdh;
4243 return fdh;
4244}
4245
4246/* Fix function descriptor symbols defined in .opd sections to be
4247 function type. */
4248
4249static bool
4250ppc64_elf_add_symbol_hook (bfd *ibfd,
4251 struct bfd_link_info *info,
4252 Elf_Internal_Sym *isym,
4253 const char **name,
4254 flagword *flags ATTRIBUTE_UNUSED,
4255 asection **sec,
4256 bfd_vma *value)
4257{
4258 if (*sec != NULL
4259 && strcmp ((*sec)->name, ".opd") == 0)
4260 {
4261 asection *code_sec;
4262
4263 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4264 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4265 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4266
4267 /* If the symbol is a function defined in .opd, and the function
4268 code is in a discarded group, let it appear to be undefined. */
4269 if (!bfd_link_relocatable (info)
4270 && (*sec)->reloc_count != 0
4271 && opd_entry_value (*sec, *value, &code_sec, NULL,
4272 false) != (bfd_vma) -1
4273 && discarded_section (code_sec))
4274 {
4275 *sec = bfd_und_section_ptr;
4276 isym->st_shndx = SHN_UNDEF;
4277 }
4278 }
4279 else if (*sec != NULL
4280 && strcmp ((*sec)->name, ".toc") == 0
4281 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4282 {
4283 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4284 if (htab != NULL)
4285 htab->params->object_in_toc = 1;
4286 }
4287
4288 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4289 {
4290 if (abiversion (ibfd) == 0)
4291 set_abiversion (ibfd, 2);
4292 else if (abiversion (ibfd) == 1)
4293 {
4294 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4295 " for ABI version 1"), *name);
4296 bfd_set_error (bfd_error_bad_value);
4297 return false;
4298 }
4299 }
4300
4301 return true;
4302}
4303
4304/* Merge non-visibility st_other attributes: local entry point. */
4305
4306static void
4307ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4308 unsigned int st_other,
4309 bool definition,
4310 bool dynamic)
4311{
4312 if (definition && (!dynamic || !h->def_regular))
4313 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4314 | ELF_ST_VISIBILITY (h->other));
4315}
4316
4317/* Hook called on merging a symbol. We use this to clear "fake" since
4318 we now have a real symbol. */
4319
4320static bool
4321ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4322 const Elf_Internal_Sym *isym,
4323 asection **psec ATTRIBUTE_UNUSED,
4324 bool newdef ATTRIBUTE_UNUSED,
4325 bool olddef ATTRIBUTE_UNUSED,
4326 bfd *oldbfd ATTRIBUTE_UNUSED,
4327 const asection *oldsec ATTRIBUTE_UNUSED)
4328{
4329 ppc_elf_hash_entry (h)->fake = 0;
4330 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4331 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4332 return true;
4333}
4334
4335/* This function makes an old ABI object reference to ".bar" cause the
4336 inclusion of a new ABI object archive that defines "bar".
4337 NAME is a symbol defined in an archive. Return a symbol in the hash
4338 table that might be satisfied by the archive symbols. */
4339
4340static struct bfd_link_hash_entry *
4341ppc64_elf_archive_symbol_lookup (bfd *abfd,
4342 struct bfd_link_info *info,
4343 const char *name)
4344{
4345 struct bfd_link_hash_entry *h;
4346 char *dot_name;
4347 size_t len;
4348
4349 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4350 if (h != NULL
4351 && ppc_hash_table (info) != NULL
4352 /* Don't return this sym if it is a fake function descriptor
4353 created by add_symbol_adjust. */
4354 && !((struct ppc_link_hash_entry *) h)->fake)
4355 return h;
4356
4357 if (name[0] == '.')
4358 return h;
4359
4360 len = strlen (name);
4361 dot_name = bfd_alloc (abfd, len + 2);
4362 if (dot_name == NULL)
4363 return (struct bfd_link_hash_entry *) -1;
4364 dot_name[0] = '.';
4365 memcpy (dot_name + 1, name, len + 1);
4366 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4367 bfd_release (abfd, dot_name);
4368 if (h != NULL)
4369 return h;
4370
4371 if (strcmp (name, "__tls_get_addr_opt") == 0)
4372 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4373 return h;
4374}
4375
4376/* This function satisfies all old ABI object references to ".bar" if a
4377 new ABI object defines "bar". Well, at least, undefined dot symbols
4378 are made weak. This stops later archive searches from including an
4379 object if we already have a function descriptor definition. It also
4380 prevents the linker complaining about undefined symbols.
4381 We also check and correct mismatched symbol visibility here. The
4382 most restrictive visibility of the function descriptor and the
4383 function entry symbol is used. */
4384
4385static bool
4386add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4387{
4388 struct ppc_link_hash_table *htab;
4389 struct ppc_link_hash_entry *fdh;
4390
4391 if (eh->elf.root.type == bfd_link_hash_warning)
4392 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4393
4394 if (eh->elf.root.type == bfd_link_hash_indirect)
4395 return true;
4396
4397 if (eh->elf.root.root.string[0] != '.')
4398 abort ();
4399
4400 htab = ppc_hash_table (info);
4401 if (htab == NULL)
4402 return false;
4403
4404 fdh = lookup_fdh (eh, htab);
4405 if (fdh == NULL
4406 && !bfd_link_relocatable (info)
4407 && (eh->elf.root.type == bfd_link_hash_undefined
4408 || eh->elf.root.type == bfd_link_hash_undefweak)
4409 && eh->elf.ref_regular)
4410 {
4411 /* Make an undefined function descriptor sym, in order to
4412 pull in an --as-needed shared lib. Archives are handled
4413 elsewhere. */
4414 fdh = make_fdh (info, eh);
4415 if (fdh == NULL)
4416 return false;
4417 }
4418
4419 if (fdh != NULL)
4420 {
4421 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4422 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4423
4424 /* Make both descriptor and entry symbol have the most
4425 constraining visibility of either symbol. */
4426 if (entry_vis < descr_vis)
4427 fdh->elf.other += entry_vis - descr_vis;
4428 else if (entry_vis > descr_vis)
4429 eh->elf.other += descr_vis - entry_vis;
4430
4431 /* Propagate reference flags from entry symbol to function
4432 descriptor symbol. */
4433 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4434 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4435 fdh->elf.ref_regular |= eh->elf.ref_regular;
4436 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4437
4438 if (!fdh->elf.forced_local
4439 && fdh->elf.dynindx == -1
4440 && fdh->elf.versioned != versioned_hidden
4441 && (bfd_link_dll (info)
4442 || fdh->elf.def_dynamic
4443 || fdh->elf.ref_dynamic)
4444 && (eh->elf.ref_regular
4445 || eh->elf.def_regular))
4446 {
4447 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4448 return false;
4449 }
4450 }
4451
4452 return true;
4453}
4454
4455/* Set up opd section info and abiversion for IBFD, and process list
4456 of dot-symbols we made in link_hash_newfunc. */
4457
4458static bool
4459ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4460{
4461 struct ppc_link_hash_table *htab;
4462 struct ppc_link_hash_entry **p, *eh;
4463 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4464
4465 if (opd != NULL && opd->size != 0)
4466 {
4467 if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4468 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4469 else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4470 BFD_FAIL ();
4471
4472 if (abiversion (ibfd) == 0)
4473 set_abiversion (ibfd, 1);
4474 else if (abiversion (ibfd) >= 2)
4475 {
4476 /* xgettext:c-format */
4477 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4478 ibfd, abiversion (ibfd));
4479 bfd_set_error (bfd_error_bad_value);
4480 return false;
4481 }
4482 }
4483
4484 if (is_ppc64_elf (info->output_bfd))
4485 {
4486 /* For input files without an explicit abiversion in e_flags
4487 we should have flagged any with symbol st_other bits set
4488 as ELFv2 and above flagged those with .opd as ELFv1.
4489 Set the output abiversion if not yet set, and for any input
4490 still ambiguous, take its abiversion from the output.
4491 Differences in ABI are reported later. */
4492 if (abiversion (info->output_bfd) == 0)
4493 set_abiversion (info->output_bfd, abiversion (ibfd));
4494 else if (abiversion (ibfd) == 0)
4495 set_abiversion (ibfd, abiversion (info->output_bfd));
4496 }
4497
4498 htab = ppc_hash_table (info);
4499 if (htab == NULL)
4500 return true;
4501
4502 if (opd != NULL && opd->size != 0
4503 && (ibfd->flags & DYNAMIC) == 0
4504 && (opd->flags & SEC_RELOC) != 0
4505 && opd->reloc_count != 0
4506 && !bfd_is_abs_section (opd->output_section)
4507 && info->gc_sections)
4508 {
4509 /* Garbage collection needs some extra help with .opd sections.
4510 We don't want to necessarily keep everything referenced by
4511 relocs in .opd, as that would keep all functions. Instead,
4512 if we reference an .opd symbol (a function descriptor), we
4513 want to keep the function code symbol's section. This is
4514 easy for global symbols, but for local syms we need to keep
4515 information about the associated function section. */
4516 bfd_size_type amt;
4517 asection **opd_sym_map;
4518 Elf_Internal_Shdr *symtab_hdr;
4519 Elf_Internal_Rela *relocs, *rel_end, *rel;
4520
4521 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4522 opd_sym_map = bfd_zalloc (ibfd, amt);
4523 if (opd_sym_map == NULL)
4524 return false;
4525 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4526 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4527 info->keep_memory);
4528 if (relocs == NULL)
4529 return false;
4530 symtab_hdr = &elf_symtab_hdr (ibfd);
4531 rel_end = relocs + opd->reloc_count - 1;
4532 for (rel = relocs; rel < rel_end; rel++)
4533 {
4534 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4535 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4536
4537 if (r_type == R_PPC64_ADDR64
4538 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4539 && r_symndx < symtab_hdr->sh_info)
4540 {
4541 Elf_Internal_Sym *isym;
4542 asection *s;
4543
4544 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4545 r_symndx);
4546 if (isym == NULL)
4547 {
4548 if (elf_section_data (opd)->relocs != relocs)
4549 free (relocs);
4550 return false;
4551 }
4552
4553 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4554 if (s != NULL && s != opd)
4555 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4556 }
4557 }
4558 if (elf_section_data (opd)->relocs != relocs)
4559 free (relocs);
4560 }
4561
4562 p = &htab->dot_syms;
4563 while ((eh = *p) != NULL)
4564 {
4565 *p = NULL;
4566 if (&eh->elf == htab->elf.hgot)
4567 ;
4568 else if (htab->elf.hgot == NULL
4569 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4570 htab->elf.hgot = &eh->elf;
4571 else if (abiversion (ibfd) <= 1)
4572 {
4573 htab->need_func_desc_adj = 1;
4574 if (!add_symbol_adjust (eh, info))
4575 return false;
4576 }
4577 p = &eh->u.next_dot_sym;
4578 }
4579 return true;
4580}
4581
4582/* Undo hash table changes when an --as-needed input file is determined
4583 not to be needed. */
4584
4585static bool
4586ppc64_elf_notice_as_needed (bfd *ibfd,
4587 struct bfd_link_info *info,
4588 enum notice_asneeded_action act)
4589{
4590 if (act == notice_not_needed)
4591 {
4592 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4593
4594 if (htab == NULL)
4595 return false;
4596
4597 htab->dot_syms = NULL;
4598 }
4599 return _bfd_elf_notice_as_needed (ibfd, info, act);
4600}
4601
4602/* If --just-symbols against a final linked binary, then assume we need
4603 toc adjusting stubs when calling functions defined there. */
4604
4605static void
4606ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4607{
4608 if ((sec->flags & SEC_CODE) != 0
4609 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4610 && is_ppc64_elf (sec->owner))
4611 {
4612 if (abiversion (sec->owner) >= 2
4613 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4614 sec->has_toc_reloc = 1;
4615 }
4616 _bfd_elf_link_just_syms (sec, info);
4617}
4618
4619static struct plt_entry **
4620update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4621 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4622{
4623 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4624 struct plt_entry **local_plt;
4625 unsigned char *local_got_tls_masks;
4626
4627 if (local_got_ents == NULL)
4628 {
4629 bfd_size_type size = symtab_hdr->sh_info;
4630
4631 size *= (sizeof (*local_got_ents)
4632 + sizeof (*local_plt)
4633 + sizeof (*local_got_tls_masks));
4634 local_got_ents = bfd_zalloc (abfd, size);
4635 if (local_got_ents == NULL)
4636 return NULL;
4637 elf_local_got_ents (abfd) = local_got_ents;
4638 }
4639
4640 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4641 {
4642 struct got_entry *ent;
4643
4644 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4645 if (ent->addend == r_addend
4646 && ent->owner == abfd
4647 && ent->tls_type == tls_type)
4648 break;
4649 if (ent == NULL)
4650 {
4651 size_t amt = sizeof (*ent);
4652 ent = bfd_alloc (abfd, amt);
4653 if (ent == NULL)
4654 return NULL;
4655 ent->next = local_got_ents[r_symndx];
4656 ent->addend = r_addend;
4657 ent->owner = abfd;
4658 ent->tls_type = tls_type;
4659 ent->is_indirect = false;
4660 ent->got.refcount = 0;
4661 local_got_ents[r_symndx] = ent;
4662 }
4663 ent->got.refcount += 1;
4664 }
4665
4666 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4667 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4668 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4669
4670 return local_plt + r_symndx;
4671}
4672
4673static bool
4674update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4675{
4676 struct plt_entry *ent;
4677
4678 for (ent = *plist; ent != NULL; ent = ent->next)
4679 if (ent->addend == addend)
4680 break;
4681 if (ent == NULL)
4682 {
4683 size_t amt = sizeof (*ent);
4684 ent = bfd_alloc (abfd, amt);
4685 if (ent == NULL)
4686 return false;
4687 ent->next = *plist;
4688 ent->addend = addend;
4689 ent->plt.refcount = 0;
4690 *plist = ent;
4691 }
4692 ent->plt.refcount += 1;
4693 return true;
4694}
4695
4696static bool
4697is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4698{
4699 return (r_type == R_PPC64_REL24
4700 || r_type == R_PPC64_REL24_NOTOC
4701 || r_type == R_PPC64_REL24_P9NOTOC
4702 || r_type == R_PPC64_REL14
4703 || r_type == R_PPC64_REL14_BRTAKEN
4704 || r_type == R_PPC64_REL14_BRNTAKEN
4705 || r_type == R_PPC64_ADDR24
4706 || r_type == R_PPC64_ADDR14
4707 || r_type == R_PPC64_ADDR14_BRTAKEN
4708 || r_type == R_PPC64_ADDR14_BRNTAKEN
4709 || r_type == R_PPC64_PLTCALL
4710 || r_type == R_PPC64_PLTCALL_NOTOC);
4711}
4712
4713/* Relocs on inline plt call sequence insns prior to the call. */
4714
4715static bool
4716is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4717{
4718 return (r_type == R_PPC64_PLT16_HA
4719 || r_type == R_PPC64_PLT16_HI
4720 || r_type == R_PPC64_PLT16_LO
4721 || r_type == R_PPC64_PLT16_LO_DS
4722 || r_type == R_PPC64_PLT_PCREL34
4723 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4724 || r_type == R_PPC64_PLTSEQ
4725 || r_type == R_PPC64_PLTSEQ_NOTOC);
4726}
4727
4728/* Of relocs which might appear paired with TLSGD and TLSLD marker
4729 relocs, return true for those that operate on a dword. */
4730
4731static bool
4732is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4733{
4734 return (r_type == R_PPC64_PLT_PCREL34
4735 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4736 || r_type == R_PPC64_PLTCALL);
4737}
4738
4739/* The RELR encoding doesn't allow odd addresses, so RELR_ALIGN must
4740 be at least 1. R_PPC64_RELATIVE relocs require alignment of 2**3.
4741 We use 3 here to avoid complexity in relocate_section, where for a
4742 value of 1 we'd need to test for not just an output RELATIVE reloc
4743 near the call to maybe_relr but also UADDR64 and some conditions on
4744 the symbol. See PR30824. */
4745#define RELR_ALIGN 3
4746
4747static bool
4748maybe_relr (enum elf_ppc64_reloc_type r_type,
4749 const Elf_Internal_Rela *rel,
4750 const asection *sec)
4751{
4752 return ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
4753 && (rel->r_offset & ((1 << RELR_ALIGN) - 1)) == 0
4754 && sec->alignment_power >= RELR_ALIGN);
4755}
4756
4757/* Like bfd_reloc_offset_in_range but without a howto. Return true
4758 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4759
4760static bool
4761offset_in_range (asection *sec, bfd_vma offset, size_t size)
4762{
4763 return offset <= sec->size && size <= sec->size - offset;
4764}
4765
4766/* Look through the relocs for a section during the first phase, and
4767 calculate needed space in the global offset table, procedure
4768 linkage table, and dynamic reloc sections. */
4769
4770static bool
4771ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4772 asection *sec, const Elf_Internal_Rela *relocs)
4773{
4774 struct ppc_link_hash_table *htab;
4775 Elf_Internal_Shdr *symtab_hdr;
4776 struct elf_link_hash_entry **sym_hashes;
4777 const Elf_Internal_Rela *rel;
4778 const Elf_Internal_Rela *rel_end;
4779 asection *sreloc;
4780 struct elf_link_hash_entry *tga, *dottga;
4781 bool is_opd;
4782
4783 if (bfd_link_relocatable (info))
4784 return true;
4785
4786 BFD_ASSERT (is_ppc64_elf (abfd));
4787
4788 htab = ppc_hash_table (info);
4789 if (htab == NULL)
4790 return false;
4791
4792 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4793 false, false, true);
4794 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4795 false, false, true);
4796 symtab_hdr = &elf_symtab_hdr (abfd);
4797 sym_hashes = elf_sym_hashes (abfd);
4798 sreloc = NULL;
4799 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4800 rel_end = relocs + sec->reloc_count;
4801 for (rel = relocs; rel < rel_end; rel++)
4802 {
4803 unsigned long r_symndx;
4804 struct elf_link_hash_entry *h;
4805 Elf_Internal_Sym *isym;
4806 enum elf_ppc64_reloc_type r_type;
4807 int tls_type;
4808 struct _ppc64_elf_section_data *ppc64_sec;
4809 struct plt_entry **ifunc, **plt_list;
4810
4811 r_symndx = ELF64_R_SYM (rel->r_info);
4812 if (r_symndx < symtab_hdr->sh_info)
4813 {
4814 h = NULL;
4815 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4816 if (isym == NULL)
4817 return false;
4818 }
4819 else
4820 {
4821 isym = NULL;
4822 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4823 h = elf_follow_link (h);
4824
4825 if (h == htab->elf.hgot)
4826 sec->has_toc_reloc = 1;
4827 }
4828
4829 r_type = ELF64_R_TYPE (rel->r_info);
4830 switch (r_type)
4831 {
4832 case R_PPC64_D34:
4833 case R_PPC64_D34_LO:
4834 case R_PPC64_D34_HI30:
4835 case R_PPC64_D34_HA30:
4836 case R_PPC64_D28:
4837 case R_PPC64_TPREL34:
4838 case R_PPC64_DTPREL34:
4839 case R_PPC64_PCREL34:
4840 case R_PPC64_GOT_PCREL34:
4841 case R_PPC64_GOT_TLSGD_PCREL34:
4842 case R_PPC64_GOT_TLSLD_PCREL34:
4843 case R_PPC64_GOT_TPREL_PCREL34:
4844 case R_PPC64_GOT_DTPREL_PCREL34:
4845 case R_PPC64_PLT_PCREL34:
4846 case R_PPC64_PLT_PCREL34_NOTOC:
4847 case R_PPC64_PCREL28:
4848 htab->has_power10_relocs = 1;
4849 break;
4850 default:
4851 break;
4852 }
4853
4854 switch (r_type)
4855 {
4856 case R_PPC64_PLT16_HA:
4857 case R_PPC64_GOT_TLSLD16_HA:
4858 case R_PPC64_GOT_TLSGD16_HA:
4859 case R_PPC64_GOT_TPREL16_HA:
4860 case R_PPC64_GOT_DTPREL16_HA:
4861 case R_PPC64_GOT16_HA:
4862 case R_PPC64_TOC16_HA:
4863 case R_PPC64_PLT16_LO:
4864 case R_PPC64_PLT16_LO_DS:
4865 case R_PPC64_GOT_TLSLD16_LO:
4866 case R_PPC64_GOT_TLSGD16_LO:
4867 case R_PPC64_GOT_TPREL16_LO_DS:
4868 case R_PPC64_GOT_DTPREL16_LO_DS:
4869 case R_PPC64_GOT16_LO:
4870 case R_PPC64_GOT16_LO_DS:
4871 case R_PPC64_TOC16_LO:
4872 case R_PPC64_TOC16_LO_DS:
4873 case R_PPC64_GOT_PCREL34:
4874 ppc64_elf_tdata (abfd)->has_optrel = 1;
4875 ppc64_elf_section_data (sec)->has_optrel = 1;
4876 break;
4877 default:
4878 break;
4879 }
4880
4881 ifunc = NULL;
4882 if (h != NULL)
4883 {
4884 if (h->type == STT_GNU_IFUNC)
4885 {
4886 h->needs_plt = 1;
4887 ifunc = &h->plt.plist;
4888 }
4889 }
4890 else
4891 {
4892 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4893 {
4894 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4895 rel->r_addend,
4896 NON_GOT | PLT_IFUNC);
4897 if (ifunc == NULL)
4898 return false;
4899 }
4900 }
4901
4902 tls_type = 0;
4903 switch (r_type)
4904 {
4905 case R_PPC64_PLTSEQ:
4906 case R_PPC64_PLTSEQ_NOTOC:
4907 /* Inline plt call code emitted by gcc doesn't support
4908 modifying the tls_index words to short-circuit
4909 __tls_get_addr calls. See PR32387. */
4910 if (h != NULL && (h == tga || h == dottga))
4911 htab->params->tls_get_addr_opt = 0;
4912 break;
4913
4914 case R_PPC64_TLSGD:
4915 case R_PPC64_TLSLD:
4916 /* These special tls relocs tie a call to __tls_get_addr with
4917 its parameter symbol. */
4918 if (h != NULL)
4919 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4920 else
4921 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4922 rel->r_addend,
4923 NON_GOT | TLS_TLS | TLS_MARK))
4924 return false;
4925 sec->has_tls_reloc = 1;
4926 break;
4927
4928 case R_PPC64_GOT_TLSLD16:
4929 case R_PPC64_GOT_TLSLD16_LO:
4930 case R_PPC64_GOT_TLSLD16_HI:
4931 case R_PPC64_GOT_TLSLD16_HA:
4932 case R_PPC64_GOT_TLSLD_PCREL34:
4933 tls_type = TLS_TLS | TLS_LD;
4934 goto dogottls;
4935
4936 case R_PPC64_GOT_TLSGD16:
4937 case R_PPC64_GOT_TLSGD16_LO:
4938 case R_PPC64_GOT_TLSGD16_HI:
4939 case R_PPC64_GOT_TLSGD16_HA:
4940 case R_PPC64_GOT_TLSGD_PCREL34:
4941 tls_type = TLS_TLS | TLS_GD;
4942 goto dogottls;
4943
4944 case R_PPC64_GOT_TPREL16_DS:
4945 case R_PPC64_GOT_TPREL16_LO_DS:
4946 case R_PPC64_GOT_TPREL16_HI:
4947 case R_PPC64_GOT_TPREL16_HA:
4948 case R_PPC64_GOT_TPREL_PCREL34:
4949 if (bfd_link_dll (info))
4950 info->flags |= DF_STATIC_TLS;
4951 tls_type = TLS_TLS | TLS_TPREL;
4952 goto dogottls;
4953
4954 case R_PPC64_GOT_DTPREL16_DS:
4955 case R_PPC64_GOT_DTPREL16_LO_DS:
4956 case R_PPC64_GOT_DTPREL16_HI:
4957 case R_PPC64_GOT_DTPREL16_HA:
4958 case R_PPC64_GOT_DTPREL_PCREL34:
4959 tls_type = TLS_TLS | TLS_DTPREL;
4960 dogottls:
4961 sec->has_tls_reloc = 1;
4962 goto dogot;
4963
4964 case R_PPC64_GOT16:
4965 case R_PPC64_GOT16_LO:
4966 case R_PPC64_GOT16_HI:
4967 case R_PPC64_GOT16_HA:
4968 case R_PPC64_GOT16_DS:
4969 case R_PPC64_GOT16_LO_DS:
4970 case R_PPC64_GOT_PCREL34:
4971 dogot:
4972 /* This symbol requires a global offset table entry. */
4973 sec->has_toc_reloc = 1;
4974 if (r_type == R_PPC64_GOT_TLSLD16
4975 || r_type == R_PPC64_GOT_TLSGD16
4976 || r_type == R_PPC64_GOT_TPREL16_DS
4977 || r_type == R_PPC64_GOT_DTPREL16_DS
4978 || r_type == R_PPC64_GOT16
4979 || r_type == R_PPC64_GOT16_DS)
4980 {
4981 htab->do_multi_toc = 1;
4982 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4983 }
4984
4985 if (ppc64_elf_tdata (abfd)->got == NULL
4986 && !create_got_section (abfd, info))
4987 return false;
4988
4989 if (h != NULL)
4990 {
4991 struct ppc_link_hash_entry *eh;
4992 struct got_entry *ent;
4993
4994 eh = ppc_elf_hash_entry (h);
4995 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4996 if (ent->addend == rel->r_addend
4997 && ent->owner == abfd
4998 && ent->tls_type == tls_type)
4999 break;
5000 if (ent == NULL)
5001 {
5002 size_t amt = sizeof (*ent);
5003 ent = bfd_alloc (abfd, amt);
5004 if (ent == NULL)
5005 return false;
5006 ent->next = eh->elf.got.glist;
5007 ent->addend = rel->r_addend;
5008 ent->owner = abfd;
5009 ent->tls_type = tls_type;
5010 ent->is_indirect = false;
5011 ent->got.refcount = 0;
5012 eh->elf.got.glist = ent;
5013 }
5014 ent->got.refcount += 1;
5015 eh->tls_mask |= tls_type;
5016 }
5017 else
5018 /* This is a global offset table entry for a local symbol. */
5019 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5020 rel->r_addend, tls_type))
5021 return false;
5022 break;
5023
5024 case R_PPC64_PLT16_HA:
5025 case R_PPC64_PLT16_HI:
5026 case R_PPC64_PLT16_LO:
5027 case R_PPC64_PLT16_LO_DS:
5028 case R_PPC64_PLT_PCREL34:
5029 case R_PPC64_PLT_PCREL34_NOTOC:
5030 case R_PPC64_PLT32:
5031 case R_PPC64_PLT64:
5032 /* This symbol requires a procedure linkage table entry. */
5033 plt_list = ifunc;
5034 if (h != NULL)
5035 {
5036 h->needs_plt = 1;
5037 if (h->root.root.string[0] == '.'
5038 && h->root.root.string[1] != '\0')
5039 ppc_elf_hash_entry (h)->is_func = 1;
5040 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5041 plt_list = &h->plt.plist;
5042 }
5043 if (plt_list == NULL)
5044 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5045 rel->r_addend,
5046 NON_GOT | PLT_KEEP);
5047 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5048 return false;
5049 break;
5050
5051 /* The following relocations don't need to propagate the
5052 relocation if linking a shared object since they are
5053 section relative. */
5054 case R_PPC64_SECTOFF:
5055 case R_PPC64_SECTOFF_LO:
5056 case R_PPC64_SECTOFF_HI:
5057 case R_PPC64_SECTOFF_HA:
5058 case R_PPC64_SECTOFF_DS:
5059 case R_PPC64_SECTOFF_LO_DS:
5060 case R_PPC64_DTPREL16:
5061 case R_PPC64_DTPREL16_LO:
5062 case R_PPC64_DTPREL16_HI:
5063 case R_PPC64_DTPREL16_HA:
5064 case R_PPC64_DTPREL16_DS:
5065 case R_PPC64_DTPREL16_LO_DS:
5066 case R_PPC64_DTPREL16_HIGH:
5067 case R_PPC64_DTPREL16_HIGHA:
5068 case R_PPC64_DTPREL16_HIGHER:
5069 case R_PPC64_DTPREL16_HIGHERA:
5070 case R_PPC64_DTPREL16_HIGHEST:
5071 case R_PPC64_DTPREL16_HIGHESTA:
5072 break;
5073
5074 /* Nor do these. */
5075 case R_PPC64_REL16:
5076 case R_PPC64_REL16_LO:
5077 case R_PPC64_REL16_HI:
5078 case R_PPC64_REL16_HA:
5079 case R_PPC64_REL16_HIGH:
5080 case R_PPC64_REL16_HIGHA:
5081 case R_PPC64_REL16_HIGHER:
5082 case R_PPC64_REL16_HIGHERA:
5083 case R_PPC64_REL16_HIGHEST:
5084 case R_PPC64_REL16_HIGHESTA:
5085 case R_PPC64_REL16_HIGHER34:
5086 case R_PPC64_REL16_HIGHERA34:
5087 case R_PPC64_REL16_HIGHEST34:
5088 case R_PPC64_REL16_HIGHESTA34:
5089 case R_PPC64_REL16DX_HA:
5090 break;
5091
5092 /* Not supported as a dynamic relocation. */
5093 case R_PPC64_ADDR64_LOCAL:
5094 if (bfd_link_pic (info))
5095 {
5096 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5097 ppc_howto_init ();
5098 /* xgettext:c-format */
5099 info->callbacks->einfo (_("%H: %s unsupported "
5100 "in shared libraries and PIEs\n"),
5101 abfd, sec, rel->r_offset,
5102 ppc64_elf_howto_table[r_type]->name);
5103 bfd_set_error (bfd_error_bad_value);
5104 return false;
5105 }
5106 break;
5107
5108 case R_PPC64_TOC16:
5109 case R_PPC64_TOC16_DS:
5110 htab->do_multi_toc = 1;
5111 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5112 /* Fall through. */
5113 case R_PPC64_TOC16_LO:
5114 case R_PPC64_TOC16_HI:
5115 case R_PPC64_TOC16_HA:
5116 case R_PPC64_TOC16_LO_DS:
5117 sec->has_toc_reloc = 1;
5118 if (h != NULL && bfd_link_executable (info))
5119 {
5120 /* We may need a copy reloc. */
5121 h->non_got_ref = 1;
5122 /* Strongly prefer a copy reloc over a dynamic reloc.
5123 glibc ld.so as of 2019-08 will error out if one of
5124 these relocations is emitted. */
5125 h->needs_copy = 1;
5126 goto dodyn;
5127 }
5128 break;
5129
5130 /* Marker reloc. */
5131 case R_PPC64_ENTRY:
5132 break;
5133
5134 /* This relocation describes the C++ object vtable hierarchy.
5135 Reconstruct it for later use during GC. */
5136 case R_PPC64_GNU_VTINHERIT:
5137 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5138 return false;
5139 break;
5140
5141 /* This relocation describes which C++ vtable entries are actually
5142 used. Record for later use during GC. */
5143 case R_PPC64_GNU_VTENTRY:
5144 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5145 return false;
5146 break;
5147
5148 case R_PPC64_REL14:
5149 case R_PPC64_REL14_BRTAKEN:
5150 case R_PPC64_REL14_BRNTAKEN:
5151 {
5152 asection *dest = NULL;
5153
5154 /* Heuristic: If jumping outside our section, chances are
5155 we are going to need a stub. */
5156 if (h != NULL)
5157 {
5158 /* If the sym is weak it may be overridden later, so
5159 don't assume we know where a weak sym lives. */
5160 if (h->root.type == bfd_link_hash_defined)
5161 dest = h->root.u.def.section;
5162 }
5163 else
5164 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5165
5166 if (dest != sec)
5167 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5168 }
5169 goto rel24;
5170
5171 case R_PPC64_PLTCALL:
5172 case R_PPC64_PLTCALL_NOTOC:
5173 ppc64_elf_section_data (sec)->has_pltcall = 1;
5174 /* Fall through. */
5175
5176 case R_PPC64_REL24:
5177 case R_PPC64_REL24_NOTOC:
5178 case R_PPC64_REL24_P9NOTOC:
5179 rel24:
5180 plt_list = ifunc;
5181 if (h != NULL)
5182 {
5183 h->needs_plt = 1;
5184 if (h->root.root.string[0] == '.'
5185 && h->root.root.string[1] != '\0')
5186 ppc_elf_hash_entry (h)->is_func = 1;
5187
5188 if (h == tga || h == dottga)
5189 {
5190 sec->has_tls_reloc = 1;
5191 if (rel != relocs
5192 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5193 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5194 /* We have a new-style __tls_get_addr call with
5195 a marker reloc. */
5196 ;
5197 else
5198 /* Mark this section as having an old-style call. */
5199 sec->nomark_tls_get_addr = 1;
5200 }
5201 plt_list = &h->plt.plist;
5202 }
5203
5204 /* We may need a .plt entry if the function this reloc
5205 refers to is in a shared lib. */
5206 if (plt_list
5207 && !update_plt_info (abfd, plt_list, rel->r_addend))
5208 return false;
5209 break;
5210
5211 case R_PPC64_ADDR14:
5212 case R_PPC64_ADDR14_BRNTAKEN:
5213 case R_PPC64_ADDR14_BRTAKEN:
5214 case R_PPC64_ADDR24:
5215 goto dodyn;
5216
5217 case R_PPC64_TPREL64:
5218 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5219 if (bfd_link_dll (info))
5220 info->flags |= DF_STATIC_TLS;
5221 goto dotlstoc;
5222
5223 case R_PPC64_DTPMOD64:
5224 if (rel + 1 < rel_end
5225 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5226 && rel[1].r_offset == rel->r_offset + 8)
5227 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5228 else
5229 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5230 goto dotlstoc;
5231
5232 case R_PPC64_DTPREL64:
5233 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5234 if (rel != relocs
5235 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5236 && rel[-1].r_offset == rel->r_offset - 8)
5237 /* This is the second reloc of a dtpmod, dtprel pair.
5238 Don't mark with TLS_DTPREL. */
5239 goto dodyn;
5240
5241 dotlstoc:
5242 sec->has_tls_reloc = 1;
5243 if (h != NULL)
5244 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5245 else
5246 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5247 rel->r_addend, tls_type))
5248 return false;
5249
5250 ppc64_sec = ppc64_elf_section_data (sec);
5251 if (ppc64_sec->sec_type == sec_normal)
5252 {
5253 bfd_size_type amt;
5254
5255 /* One extra to simplify get_tls_mask. */
5256 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5257 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5258 if (ppc64_sec->u.toc.symndx == NULL)
5259 return false;
5260 amt = sec->size * sizeof (bfd_vma) / 8;
5261 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5262 if (ppc64_sec->u.toc.add == NULL)
5263 return false;
5264 ppc64_sec->sec_type = sec_toc;
5265 }
5266 if (ppc64_sec->sec_type != sec_toc
5267 || rel->r_offset % 8 != 0)
5268 {
5269 info->callbacks->einfo (_("%H: %s unsupported here\n"),
5270 abfd, sec, rel->r_offset,
5271 ppc64_elf_howto_table[r_type]->name);
5272 bfd_set_error (bfd_error_bad_value);
5273 return false;
5274 }
5275 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5276 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5277
5278 /* Mark the second slot of a GD or LD entry.
5279 -1 to indicate GD and -2 to indicate LD. */
5280 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5281 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5282 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5283 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5284 goto dodyn;
5285
5286 case R_PPC64_TPREL16_HI:
5287 case R_PPC64_TPREL16_HA:
5288 case R_PPC64_TPREL16_HIGH:
5289 case R_PPC64_TPREL16_HIGHA:
5290 case R_PPC64_TPREL16_HIGHER:
5291 case R_PPC64_TPREL16_HIGHERA:
5292 case R_PPC64_TPREL16_HIGHEST:
5293 case R_PPC64_TPREL16_HIGHESTA:
5294 sec->has_tls_reloc = 1;
5295 /* Fall through. */
5296 case R_PPC64_TPREL34:
5297 case R_PPC64_TPREL16:
5298 case R_PPC64_TPREL16_DS:
5299 case R_PPC64_TPREL16_LO:
5300 case R_PPC64_TPREL16_LO_DS:
5301 if (bfd_link_dll (info))
5302 info->flags |= DF_STATIC_TLS;
5303 goto dodyn;
5304
5305 case R_PPC64_ADDR64:
5306 if (is_opd
5307 && rel + 1 < rel_end
5308 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5309 {
5310 if (h != NULL)
5311 ppc_elf_hash_entry (h)->is_func = 1;
5312 }
5313 /* Fall through. */
5314
5315 case R_PPC64_ADDR16:
5316 case R_PPC64_ADDR16_DS:
5317 case R_PPC64_ADDR16_HA:
5318 case R_PPC64_ADDR16_HI:
5319 case R_PPC64_ADDR16_HIGH:
5320 case R_PPC64_ADDR16_HIGHA:
5321 case R_PPC64_ADDR16_HIGHER:
5322 case R_PPC64_ADDR16_HIGHERA:
5323 case R_PPC64_ADDR16_HIGHEST:
5324 case R_PPC64_ADDR16_HIGHESTA:
5325 case R_PPC64_ADDR16_LO:
5326 case R_PPC64_ADDR16_LO_DS:
5327 case R_PPC64_D34:
5328 case R_PPC64_D34_LO:
5329 case R_PPC64_D34_HI30:
5330 case R_PPC64_D34_HA30:
5331 case R_PPC64_ADDR16_HIGHER34:
5332 case R_PPC64_ADDR16_HIGHERA34:
5333 case R_PPC64_ADDR16_HIGHEST34:
5334 case R_PPC64_ADDR16_HIGHESTA34:
5335 case R_PPC64_D28:
5336 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5337 && rel->r_addend == 0)
5338 {
5339 /* We may need a .plt entry if this reloc refers to a
5340 function in a shared lib. */
5341 if (!update_plt_info (abfd, &h->plt.plist, 0))
5342 return false;
5343 h->pointer_equality_needed = 1;
5344 }
5345 /* Fall through. */
5346
5347 case R_PPC64_REL30:
5348 case R_PPC64_REL32:
5349 case R_PPC64_REL64:
5350 case R_PPC64_ADDR32:
5351 case R_PPC64_UADDR16:
5352 case R_PPC64_UADDR32:
5353 case R_PPC64_UADDR64:
5354 case R_PPC64_TOC:
5355 if (h != NULL && bfd_link_executable (info))
5356 /* We may need a copy reloc. */
5357 h->non_got_ref = 1;
5358
5359 /* Don't propagate .opd relocs. */
5360 if (NO_OPD_RELOCS && is_opd)
5361 break;
5362
5363 /* Set up information for symbols that might need dynamic
5364 relocations. At this point in linking we have read all
5365 the input files and resolved most symbols, but have not
5366 yet decided whether symbols are dynamic or finalized
5367 symbol flags. In some cases we might be setting dynamic
5368 reloc info for symbols that do not end up needing such.
5369 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5370 work together with this code. */
5371 dodyn:
5372 if ((h != NULL
5373 && !SYMBOL_REFERENCES_LOCAL (info, h))
5374 || (bfd_link_pic (info)
5375 && (h != NULL
5376 ? !bfd_is_abs_symbol (&h->root)
5377 : isym->st_shndx != SHN_ABS)
5378 && must_be_dyn_reloc (info, r_type))
5379 || (!bfd_link_pic (info)
5380 && ifunc != NULL))
5381 {
5382 /* We must copy these reloc types into the output file.
5383 Create a reloc section in dynobj and make room for
5384 this reloc. */
5385 if (sreloc == NULL)
5386 {
5387 sreloc = _bfd_elf_make_dynamic_reloc_section
5388 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5389
5390 if (sreloc == NULL)
5391 return false;
5392 }
5393
5394 /* If this is a global symbol, we count the number of
5395 relocations we need for this symbol. */
5396 if (h != NULL)
5397 {
5398 struct ppc_dyn_relocs *p;
5399 struct ppc_dyn_relocs **head;
5400
5401 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5402 p = *head;
5403 if (p == NULL || p->sec != sec)
5404 {
5405 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5406 if (p == NULL)
5407 return false;
5408 p->next = *head;
5409 *head = p;
5410 p->sec = sec;
5411 p->count = 0;
5412 p->pc_count = 0;
5413 p->rel_count = 0;
5414 }
5415 p->count += 1;
5416 if (!must_be_dyn_reloc (info, r_type))
5417 p->pc_count += 1;
5418 if (maybe_relr (r_type, rel, sec))
5419 p->rel_count += 1;
5420 }
5421 else
5422 {
5423 /* Track dynamic relocs needed for local syms too. */
5424 struct ppc_local_dyn_relocs *p;
5425 struct ppc_local_dyn_relocs **head;
5426 bool is_ifunc;
5427 asection *s;
5428 void *vpp;
5429
5430 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5431 if (s == NULL)
5432 s = sec;
5433
5434 vpp = &elf_section_data (s)->local_dynrel;
5435 head = (struct ppc_local_dyn_relocs **) vpp;
5436 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5437 p = *head;
5438 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5439 p = p->next;
5440 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5441 {
5442 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5443 if (p == NULL)
5444 return false;
5445 p->next = *head;
5446 *head = p;
5447 p->sec = sec;
5448 p->count = 0;
5449 p->rel_count = 0;
5450 p->ifunc = is_ifunc;
5451 }
5452 p->count += 1;
5453 if (maybe_relr (r_type, rel, sec))
5454 p->rel_count += 1;
5455 }
5456 }
5457 break;
5458
5459 default:
5460 break;
5461 }
5462 }
5463
5464 return true;
5465}
5466
5467/* Merge backend specific data from an object file to the output
5468 object file when linking. */
5469
5470static bool
5471ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5472{
5473 bfd *obfd = info->output_bfd;
5474 unsigned long iflags, oflags;
5475
5476 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5477 return true;
5478
5479 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5480 return true;
5481
5482 if (!_bfd_generic_verify_endian_match (ibfd, info))
5483 return false;
5484
5485 iflags = elf_elfheader (ibfd)->e_flags;
5486 oflags = elf_elfheader (obfd)->e_flags;
5487
5488 if (iflags & ~EF_PPC64_ABI)
5489 {
5490 _bfd_error_handler
5491 /* xgettext:c-format */
5492 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5493 bfd_set_error (bfd_error_bad_value);
5494 return false;
5495 }
5496 else if (iflags != oflags && iflags != 0)
5497 {
5498 _bfd_error_handler
5499 /* xgettext:c-format */
5500 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5501 ibfd, iflags, oflags);
5502 bfd_set_error (bfd_error_bad_value);
5503 return false;
5504 }
5505
5506 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5507 return false;
5508
5509 /* Merge Tag_compatibility attributes and any common GNU ones. */
5510 return _bfd_elf_merge_object_attributes (ibfd, info);
5511}
5512
5513static bool
5514ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5515{
5516 /* Print normal ELF private data. */
5517 _bfd_elf_print_private_bfd_data (abfd, ptr);
5518
5519 if (elf_elfheader (abfd)->e_flags != 0)
5520 {
5521 FILE *file = ptr;
5522
5523 fprintf (file, _("private flags = 0x%lx:"),
5524 elf_elfheader (abfd)->e_flags);
5525
5526 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5527 fprintf (file, _(" [abiv%ld]"),
5528 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5529 fputc ('\n', file);
5530 }
5531
5532 return true;
5533}
5534
5535/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5536 of the code entry point, and its section, which must be in the same
5537 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5538
5539static bfd_vma
5540opd_entry_value (asection *opd_sec,
5541 bfd_vma offset,
5542 asection **code_sec,
5543 bfd_vma *code_off,
5544 bool in_code_sec)
5545{
5546 bfd *opd_bfd = opd_sec->owner;
5547 Elf_Internal_Rela *relocs;
5548 Elf_Internal_Rela *lo, *hi, *look;
5549 bfd_vma val;
5550
5551 if (!is_ppc64_elf (opd_bfd))
5552 return (bfd_vma) -1;
5553
5554 if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5555 ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5556 else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5557 return (bfd_vma) -1;
5558
5559 /* No relocs implies we are linking a --just-symbols object, or looking
5560 at a final linked executable with addr2line or somesuch. */
5561 if (opd_sec->reloc_count == 0)
5562 {
5563 bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
5564
5565 if (contents == NULL)
5566 {
5567 if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5568 || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5569 return (bfd_vma) -1;
5570 ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
5571 }
5572
5573 /* PR 17512: file: 64b9dfbb. */
5574 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5575 return (bfd_vma) -1;
5576
5577 val = bfd_get_64 (opd_bfd, contents + offset);
5578 if (code_sec != NULL)
5579 {
5580 asection *sec, *likely = NULL;
5581
5582 if (in_code_sec)
5583 {
5584 sec = *code_sec;
5585 if (sec->vma <= val
5586 && val < sec->vma + sec->size)
5587 likely = sec;
5588 else
5589 val = -1;
5590 }
5591 else
5592 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5593 if (sec->vma <= val
5594 && (sec->flags & SEC_LOAD) != 0
5595 && (sec->flags & SEC_ALLOC) != 0)
5596 likely = sec;
5597 if (likely != NULL)
5598 {
5599 *code_sec = likely;
5600 if (code_off != NULL)
5601 *code_off = val - likely->vma;
5602 }
5603 }
5604 return val;
5605 }
5606
5607 relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
5608 if (relocs == NULL)
5609 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5610 /* PR 17512: file: df8e1fd6. */
5611 if (relocs == NULL)
5612 return (bfd_vma) -1;
5613
5614 /* Go find the opd reloc at the sym address. */
5615 lo = relocs;
5616 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5617 val = (bfd_vma) -1;
5618 while (lo < hi)
5619 {
5620 look = lo + (hi - lo) / 2;
5621 if (look->r_offset < offset)
5622 lo = look + 1;
5623 else if (look->r_offset > offset)
5624 hi = look;
5625 else
5626 {
5627 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5628
5629 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5630 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5631 {
5632 unsigned long symndx = ELF64_R_SYM (look->r_info);
5633 asection *sec = NULL;
5634
5635 if (symndx >= symtab_hdr->sh_info
5636 && elf_sym_hashes (opd_bfd) != NULL)
5637 {
5638 struct elf_link_hash_entry **sym_hashes;
5639 struct elf_link_hash_entry *rh;
5640
5641 sym_hashes = elf_sym_hashes (opd_bfd);
5642 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5643 if (rh != NULL)
5644 {
5645 rh = elf_follow_link (rh);
5646 if (rh->root.type != bfd_link_hash_defined
5647 && rh->root.type != bfd_link_hash_defweak)
5648 break;
5649 if (rh->root.u.def.section->owner == opd_bfd)
5650 {
5651 val = rh->root.u.def.value;
5652 sec = rh->root.u.def.section;
5653 }
5654 }
5655 }
5656
5657 if (sec == NULL)
5658 {
5659 Elf_Internal_Sym *sym;
5660
5661 if (symndx < symtab_hdr->sh_info)
5662 {
5663 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5664 if (sym == NULL)
5665 {
5666 size_t symcnt = symtab_hdr->sh_info;
5667 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5668 symcnt, 0,
5669 NULL, NULL, NULL);
5670 if (sym == NULL)
5671 break;
5672 symtab_hdr->contents = (bfd_byte *) sym;
5673 }
5674 sym += symndx;
5675 }
5676 else
5677 {
5678 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5679 1, symndx,
5680 NULL, NULL, NULL);
5681 if (sym == NULL)
5682 break;
5683 }
5684 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5685 if (sec != NULL)
5686 {
5687 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5688 val = sym->st_value;
5689 }
5690 if (symndx >= symtab_hdr->sh_info)
5691 free (sym);
5692 if (sec == NULL)
5693 break;
5694 }
5695
5696 val += look->r_addend;
5697 if (code_off != NULL)
5698 *code_off = val;
5699 if (code_sec != NULL)
5700 {
5701 if (in_code_sec && *code_sec != sec)
5702 return -1;
5703 else
5704 *code_sec = sec;
5705 }
5706 if (sec->output_section != NULL)
5707 val += sec->output_section->vma + sec->output_offset;
5708 }
5709 break;
5710 }
5711 }
5712
5713 return val;
5714}
5715
5716/* If the ELF symbol SYM might be a function in SEC, return the
5717 function size and set *CODE_OFF to the function's entry point,
5718 otherwise return zero. */
5719
5720static bfd_size_type
5721ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5722 bfd_vma *code_off)
5723{
5724 bfd_size_type size;
5725 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5726
5727 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5728 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5729 return 0;
5730
5731 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5732
5733 /* In theory we should check that the symbol's type satisfies
5734 _bfd_elf_is_function_type(), but there are some function-like
5735 symbols which would fail this test. (eg _start). Instead
5736 we check for hidden, local, notype symbols with zero size.
5737 This type of symbol is generated by the annobin plugin for gcc
5738 and clang, and should not be considered to be a function symbol. */
5739 if (size == 0
5740 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5741 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5742 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5743 return 0;
5744
5745 if (strcmp (sym->section->name, ".opd") == 0)
5746 {
5747 struct _opd_sec_data *opd = get_opd_info (sym->section);
5748 bfd_vma symval = sym->value;
5749
5750 if (opd != NULL
5751 && opd->adjust != NULL
5752 && elf_section_data (sym->section)->relocs != NULL)
5753 {
5754 /* opd_entry_value will use cached relocs that have been
5755 adjusted, but with raw symbols. That means both local
5756 and global symbols need adjusting. */
5757 long adjust = opd->adjust[OPD_NDX (symval)];
5758 if (adjust == -1)
5759 return 0;
5760 symval += adjust;
5761 }
5762
5763 if (opd_entry_value (sym->section, symval,
5764 &sec, code_off, true) == (bfd_vma) -1)
5765 return 0;
5766 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5767 symbol. This size has nothing to do with the code size of the
5768 function, which is what we're supposed to return, but the
5769 code size isn't available without looking up the dot-sym.
5770 However, doing that would be a waste of time particularly
5771 since elf_find_function will look at the dot-sym anyway.
5772 Now, elf_find_function will keep the largest size of any
5773 function sym found at the code address of interest, so return
5774 1 here to avoid it incorrectly caching a larger function size
5775 for a small function. This does mean we return the wrong
5776 size for a new-ABI function of size 24, but all that does is
5777 disable caching for such functions. */
5778 if (size == 24)
5779 size = 1;
5780 }
5781 else
5782 {
5783 if (sym->section != sec)
5784 return 0;
5785 *code_off = sym->value;
5786 }
5787
5788 /* Do not return 0 for the function's size. */
5789 return size ? size : 1;
5790}
5791
5792/* Return true if symbol is a strong function defined in an ELFv2
5793 object with st_other localentry bits of zero, ie. its local entry
5794 point coincides with its global entry point. */
5795
5796static bool
5797is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5798{
5799 return (h != NULL
5800 && h->type == STT_FUNC
5801 && h->root.type == bfd_link_hash_defined
5802 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5803 && !ppc_elf_hash_entry (h)->non_zero_localentry
5804 && is_ppc64_elf (h->root.u.def.section->owner)
5805 && abiversion (h->root.u.def.section->owner) >= 2);
5806}
5807
5808/* Return true if symbol is defined in a regular object file. */
5809
5810static bool
5811is_static_defined (struct elf_link_hash_entry *h)
5812{
5813 return ((h->root.type == bfd_link_hash_defined
5814 || h->root.type == bfd_link_hash_defweak)
5815 && h->root.u.def.section != NULL
5816 && h->root.u.def.section->output_section != NULL);
5817}
5818
5819/* If FDH is a function descriptor symbol, return the associated code
5820 entry symbol if it is defined. Return NULL otherwise. */
5821
5822static struct ppc_link_hash_entry *
5823defined_code_entry (struct ppc_link_hash_entry *fdh)
5824{
5825 if (fdh->is_func_descriptor)
5826 {
5827 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5828 if (fh->elf.root.type == bfd_link_hash_defined
5829 || fh->elf.root.type == bfd_link_hash_defweak)
5830 return fh;
5831 }
5832 return NULL;
5833}
5834
5835/* If FH is a function code entry symbol, return the associated
5836 function descriptor symbol if it is defined. Return NULL otherwise. */
5837
5838static struct ppc_link_hash_entry *
5839defined_func_desc (struct ppc_link_hash_entry *fh)
5840{
5841 if (fh->oh != NULL
5842 && fh->oh->is_func_descriptor)
5843 {
5844 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5845 if (fdh->elf.root.type == bfd_link_hash_defined
5846 || fdh->elf.root.type == bfd_link_hash_defweak)
5847 return fdh;
5848 }
5849 return NULL;
5850}
5851
5852/* Given H is a symbol that satisfies is_static_defined, return the
5853 value in the output file. */
5854
5855static bfd_vma
5856defined_sym_val (struct elf_link_hash_entry *h)
5857{
5858 return (h->root.u.def.section->output_section->vma
5859 + h->root.u.def.section->output_offset
5860 + h->root.u.def.value);
5861}
5862
5863/* Return true if H matches __tls_get_addr or one of its variants. */
5864
5865static bool
5866is_tls_get_addr (struct elf_link_hash_entry *h,
5867 struct ppc_link_hash_table *htab)
5868{
5869 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5870 || h == elf_hash_entry (htab->tga_desc_fd)
5871 || h == elf_hash_entry (htab->tls_get_addr)
5872 || h == elf_hash_entry (htab->tga_desc));
5873}
5874
5875static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5876
5877/* Garbage collect sections, after first dealing with dot-symbols. */
5878
5879static bool
5880ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5881{
5882 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5883
5884 if (htab != NULL && htab->need_func_desc_adj)
5885 {
5886 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5887 htab->need_func_desc_adj = 0;
5888 }
5889 return bfd_elf_gc_sections (abfd, info);
5890}
5891
5892/* Mark all our entry sym sections, both opd and code section. */
5893
5894static void
5895ppc64_elf_gc_keep (struct bfd_link_info *info)
5896{
5897 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5898 struct bfd_sym_chain *sym;
5899
5900 if (htab == NULL)
5901 return;
5902
5903 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5904 {
5905 struct ppc_link_hash_entry *eh, *fh;
5906 asection *sec;
5907
5908 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5909 false, false, true));
5910 if (eh == NULL)
5911 continue;
5912 if (eh->elf.root.type != bfd_link_hash_defined
5913 && eh->elf.root.type != bfd_link_hash_defweak)
5914 continue;
5915
5916 fh = defined_code_entry (eh);
5917 if (fh != NULL)
5918 {
5919 sec = fh->elf.root.u.def.section;
5920 sec->flags |= SEC_KEEP;
5921 }
5922 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5923 && opd_entry_value (eh->elf.root.u.def.section,
5924 eh->elf.root.u.def.value,
5925 &sec, NULL, false) != (bfd_vma) -1)
5926 sec->flags |= SEC_KEEP;
5927
5928 sec = eh->elf.root.u.def.section;
5929 sec->flags |= SEC_KEEP;
5930 }
5931}
5932
5933/* Mark sections containing dynamically referenced symbols. When
5934 building shared libraries, we must assume that any visible symbol is
5935 referenced. */
5936
5937static bool
5938ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5939{
5940 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5941 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5942 struct ppc_link_hash_entry *fdh;
5943 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5944
5945 /* Dynamic linking info is on the func descriptor sym. */
5946 fdh = defined_func_desc (eh);
5947 if (fdh != NULL)
5948 eh = fdh;
5949
5950 if ((eh->elf.root.type == bfd_link_hash_defined
5951 || eh->elf.root.type == bfd_link_hash_defweak)
5952 && (!eh->elf.start_stop
5953 || eh->elf.root.ldscript_def
5954 || !info->start_stop_gc)
5955 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5956 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5957 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5958 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5959 && (!bfd_link_executable (info)
5960 || info->gc_keep_exported
5961 || info->export_dynamic
5962 || (eh->elf.dynamic
5963 && d != NULL
5964 && (*d->match) (&d->head, NULL,
5965 eh->elf.root.root.string)))
5966 && (eh->elf.versioned >= versioned
5967 || !bfd_hide_sym_by_version (info->version_info,
5968 eh->elf.root.root.string)))))
5969 {
5970 asection *code_sec;
5971 struct ppc_link_hash_entry *fh;
5972
5973 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5974
5975 /* Function descriptor syms cause the associated
5976 function code sym section to be marked. */
5977 fh = defined_code_entry (eh);
5978 if (fh != NULL)
5979 {
5980 code_sec = fh->elf.root.u.def.section;
5981 code_sec->flags |= SEC_KEEP;
5982 }
5983 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5984 && opd_entry_value (eh->elf.root.u.def.section,
5985 eh->elf.root.u.def.value,
5986 &code_sec, NULL, false) != (bfd_vma) -1)
5987 code_sec->flags |= SEC_KEEP;
5988 }
5989
5990 return true;
5991}
5992
5993/* Return the section that should be marked against GC for a given
5994 relocation. */
5995
5996static asection *
5997ppc64_elf_gc_mark_hook (asection *sec,
5998 struct bfd_link_info *info,
5999 Elf_Internal_Rela *rel,
6000 struct elf_link_hash_entry *h,
6001 Elf_Internal_Sym *sym)
6002{
6003 asection *rsec;
6004
6005 /* Syms return NULL if we're marking .opd, so we avoid marking all
6006 function sections, as all functions are referenced in .opd. */
6007 rsec = NULL;
6008 if (get_opd_info (sec) != NULL)
6009 return rsec;
6010
6011 if (h != NULL)
6012 {
6013 enum elf_ppc64_reloc_type r_type;
6014 struct ppc_link_hash_entry *eh, *fh, *fdh;
6015
6016 r_type = ELF64_R_TYPE (rel->r_info);
6017 switch (r_type)
6018 {
6019 case R_PPC64_GNU_VTINHERIT:
6020 case R_PPC64_GNU_VTENTRY:
6021 break;
6022
6023 default:
6024 switch (h->root.type)
6025 {
6026 case bfd_link_hash_defined:
6027 case bfd_link_hash_defweak:
6028 eh = ppc_elf_hash_entry (h);
6029 fdh = defined_func_desc (eh);
6030 if (fdh != NULL)
6031 {
6032 /* -mcall-aixdesc code references the dot-symbol on
6033 a call reloc. Mark the function descriptor too
6034 against garbage collection. */
6035 fdh->elf.mark = 1;
6036 if (fdh->elf.is_weakalias)
6037 weakdef (&fdh->elf)->mark = 1;
6038 eh = fdh;
6039 }
6040
6041 /* Function descriptor syms cause the associated
6042 function code sym section to be marked. */
6043 fh = defined_code_entry (eh);
6044 if (fh != NULL)
6045 {
6046 /* They also mark their opd section. */
6047 eh->elf.root.u.def.section->gc_mark = 1;
6048
6049 rsec = fh->elf.root.u.def.section;
6050 }
6051 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6052 && opd_entry_value (eh->elf.root.u.def.section,
6053 eh->elf.root.u.def.value,
6054 &rsec, NULL, false) != (bfd_vma) -1)
6055 eh->elf.root.u.def.section->gc_mark = 1;
6056 else
6057 rsec = h->root.u.def.section;
6058 break;
6059
6060 case bfd_link_hash_common:
6061 rsec = h->root.u.c.p->section;
6062 break;
6063
6064 default:
6065 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6066 }
6067 }
6068 }
6069 else
6070 {
6071 struct _opd_sec_data *opd;
6072
6073 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6074 opd = get_opd_info (rsec);
6075 if (opd != NULL && opd->func_sec != NULL)
6076 {
6077 rsec->gc_mark = 1;
6078
6079 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6080 }
6081 }
6082
6083 return rsec;
6084}
6085
6086/* The maximum size of .sfpr. */
6087#define SFPR_MAX (218*4)
6088
6089struct sfpr_def_parms
6090{
6091 const char name[12];
6092 unsigned char lo, hi;
6093 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6094 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6095};
6096
6097/* Auto-generate _save*, _rest* functions in .sfpr.
6098 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6099 instead. */
6100
6101static bool
6102sfpr_define (struct bfd_link_info *info,
6103 const struct sfpr_def_parms *parm,
6104 asection *stub_sec)
6105{
6106 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6107 unsigned int i;
6108 size_t len = strlen (parm->name);
6109 bool writing = false;
6110 char sym[16];
6111
6112 if (htab == NULL)
6113 return false;
6114
6115 memcpy (sym, parm->name, len);
6116 sym[len + 2] = 0;
6117
6118 for (i = parm->lo; i <= parm->hi; i++)
6119 {
6120 struct ppc_link_hash_entry *h;
6121
6122 sym[len + 0] = i / 10 + '0';
6123 sym[len + 1] = i % 10 + '0';
6124 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6125 writing, true, true));
6126 if (stub_sec != NULL)
6127 {
6128 if (h != NULL
6129 && h->elf.root.type == bfd_link_hash_defined
6130 && h->elf.root.u.def.section == htab->sfpr)
6131 {
6132 struct elf_link_hash_entry *s;
6133 char buf[32];
6134 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6135 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6136 if (s == NULL)
6137 return false;
6138 if (s->root.type == bfd_link_hash_new)
6139 {
6140 s->root.type = bfd_link_hash_defined;
6141 s->root.u.def.section = stub_sec;
6142 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6143 + h->elf.root.u.def.value);
6144 s->ref_regular = 1;
6145 s->def_regular = 1;
6146 s->ref_regular_nonweak = 1;
6147 s->forced_local = 1;
6148 s->non_elf = 0;
6149 s->root.linker_def = 1;
6150 }
6151 }
6152 continue;
6153 }
6154 if (h != NULL)
6155 {
6156 h->save_res = 1;
6157 if (!h->elf.def_regular)
6158 {
6159 h->elf.root.type = bfd_link_hash_defined;
6160 h->elf.root.u.def.section = htab->sfpr;
6161 h->elf.root.u.def.value = htab->sfpr->size;
6162 h->elf.type = STT_FUNC;
6163 h->elf.def_regular = 1;
6164 h->elf.non_elf = 0;
6165 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6166 writing = true;
6167 if (htab->sfpr->contents == NULL)
6168 {
6169 htab->sfpr->contents
6170 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6171 if (htab->sfpr->contents == NULL)
6172 return false;
6173 htab->sfpr->alloced = 1;
6174 }
6175 }
6176 }
6177 if (writing)
6178 {
6179 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6180 if (i != parm->hi)
6181 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6182 else
6183 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6184 htab->sfpr->size = p - htab->sfpr->contents;
6185 }
6186 }
6187
6188 return true;
6189}
6190
6191static bfd_byte *
6192savegpr0 (bfd *abfd, bfd_byte *p, int r)
6193{
6194 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6195 return p + 4;
6196}
6197
6198static bfd_byte *
6199savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6200{
6201 p = savegpr0 (abfd, p, r);
6202 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6203 p = p + 4;
6204 bfd_put_32 (abfd, BLR, p);
6205 return p + 4;
6206}
6207
6208static bfd_byte *
6209restgpr0 (bfd *abfd, bfd_byte *p, int r)
6210{
6211 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6212 return p + 4;
6213}
6214
6215static bfd_byte *
6216restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6217{
6218 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6219 p = p + 4;
6220 p = restgpr0 (abfd, p, r);
6221 bfd_put_32 (abfd, MTLR_R0, p);
6222 p = p + 4;
6223 if (r == 29)
6224 {
6225 p = restgpr0 (abfd, p, 30);
6226 p = restgpr0 (abfd, p, 31);
6227 }
6228 bfd_put_32 (abfd, BLR, p);
6229 return p + 4;
6230}
6231
6232static bfd_byte *
6233savegpr1 (bfd *abfd, bfd_byte *p, int r)
6234{
6235 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6236 return p + 4;
6237}
6238
6239static bfd_byte *
6240savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6241{
6242 p = savegpr1 (abfd, p, r);
6243 bfd_put_32 (abfd, BLR, p);
6244 return p + 4;
6245}
6246
6247static bfd_byte *
6248restgpr1 (bfd *abfd, bfd_byte *p, int r)
6249{
6250 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6251 return p + 4;
6252}
6253
6254static bfd_byte *
6255restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6256{
6257 p = restgpr1 (abfd, p, r);
6258 bfd_put_32 (abfd, BLR, p);
6259 return p + 4;
6260}
6261
6262static bfd_byte *
6263savefpr (bfd *abfd, bfd_byte *p, int r)
6264{
6265 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6266 return p + 4;
6267}
6268
6269static bfd_byte *
6270savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6271{
6272 p = savefpr (abfd, p, r);
6273 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6274 p = p + 4;
6275 bfd_put_32 (abfd, BLR, p);
6276 return p + 4;
6277}
6278
6279static bfd_byte *
6280restfpr (bfd *abfd, bfd_byte *p, int r)
6281{
6282 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6283 return p + 4;
6284}
6285
6286static bfd_byte *
6287restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6288{
6289 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6290 p = p + 4;
6291 p = restfpr (abfd, p, r);
6292 bfd_put_32 (abfd, MTLR_R0, p);
6293 p = p + 4;
6294 if (r == 29)
6295 {
6296 p = restfpr (abfd, p, 30);
6297 p = restfpr (abfd, p, 31);
6298 }
6299 bfd_put_32 (abfd, BLR, p);
6300 return p + 4;
6301}
6302
6303static bfd_byte *
6304savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6305{
6306 p = savefpr (abfd, p, r);
6307 bfd_put_32 (abfd, BLR, p);
6308 return p + 4;
6309}
6310
6311static bfd_byte *
6312restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6313{
6314 p = restfpr (abfd, p, r);
6315 bfd_put_32 (abfd, BLR, p);
6316 return p + 4;
6317}
6318
6319static bfd_byte *
6320savevr (bfd *abfd, bfd_byte *p, int r)
6321{
6322 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6323 p = p + 4;
6324 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6325 return p + 4;
6326}
6327
6328static bfd_byte *
6329savevr_tail (bfd *abfd, bfd_byte *p, int r)
6330{
6331 p = savevr (abfd, p, r);
6332 bfd_put_32 (abfd, BLR, p);
6333 return p + 4;
6334}
6335
6336static bfd_byte *
6337restvr (bfd *abfd, bfd_byte *p, int r)
6338{
6339 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6340 p = p + 4;
6341 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6342 return p + 4;
6343}
6344
6345static bfd_byte *
6346restvr_tail (bfd *abfd, bfd_byte *p, int r)
6347{
6348 p = restvr (abfd, p, r);
6349 bfd_put_32 (abfd, BLR, p);
6350 return p + 4;
6351}
6352
6353#define STDU_R1_0R1 0xf8210001
6354#define ADDI_R1_R1 0x38210000
6355
6356/* Emit prologue of wrapper preserving regs around a call to
6357 __tls_get_addr_opt. */
6358
6359static bfd_byte *
6360tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6361{
6362 unsigned int i;
6363
6364 bfd_put_32 (obfd, MFLR_R0, p);
6365 p += 4;
6366 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6367 p += 4;
6368
6369 if (htab->opd_abi)
6370 {
6371 for (i = 4; i < 12; i++)
6372 {
6373 bfd_put_32 (obfd,
6374 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6375 p += 4;
6376 }
6377 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6378 p += 4;
6379 }
6380 else
6381 {
6382 for (i = 4; i < 12; i++)
6383 {
6384 bfd_put_32 (obfd,
6385 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6386 p += 4;
6387 }
6388 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6389 p += 4;
6390 }
6391 return p;
6392}
6393
6394/* Emit epilogue of wrapper preserving regs around a call to
6395 __tls_get_addr_opt. */
6396
6397static bfd_byte *
6398tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6399{
6400 unsigned int i;
6401
6402 if (htab->opd_abi)
6403 {
6404 for (i = 4; i < 12; i++)
6405 {
6406 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6407 p += 4;
6408 }
6409 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6410 p += 4;
6411 }
6412 else
6413 {
6414 for (i = 4; i < 12; i++)
6415 {
6416 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6417 p += 4;
6418 }
6419 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6420 p += 4;
6421 }
6422 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6423 p += 4;
6424 bfd_put_32 (obfd, MTLR_R0, p);
6425 p += 4;
6426 bfd_put_32 (obfd, BLR, p);
6427 p += 4;
6428 return p;
6429}
6430
6431/* Called via elf_link_hash_traverse to transfer dynamic linking
6432 information on function code symbol entries to their corresponding
6433 function descriptor symbol entries. Must not be called twice for
6434 any given code symbol. */
6435
6436static bool
6437func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6438{
6439 struct bfd_link_info *info;
6440 struct ppc_link_hash_table *htab;
6441 struct ppc_link_hash_entry *fh;
6442 struct ppc_link_hash_entry *fdh;
6443 bool force_local;
6444
6445 fh = ppc_elf_hash_entry (h);
6446 if (fh->elf.root.type == bfd_link_hash_indirect)
6447 return true;
6448
6449 if (!fh->is_func)
6450 return true;
6451
6452 if (fh->elf.root.root.string[0] != '.'
6453 || fh->elf.root.root.string[1] == '\0')
6454 return true;
6455
6456 info = inf;
6457 htab = ppc_hash_table (info);
6458 if (htab == NULL)
6459 return false;
6460
6461 /* Find the corresponding function descriptor symbol. */
6462 fdh = lookup_fdh (fh, htab);
6463
6464 /* Resolve undefined references to dot-symbols as the value
6465 in the function descriptor, if we have one in a regular object.
6466 This is to satisfy cases like ".quad .foo". Calls to functions
6467 in dynamic objects are handled elsewhere. */
6468 if ((fh->elf.root.type == bfd_link_hash_undefined
6469 || fh->elf.root.type == bfd_link_hash_undefweak)
6470 && (fdh->elf.root.type == bfd_link_hash_defined
6471 || fdh->elf.root.type == bfd_link_hash_defweak)
6472 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6473 && opd_entry_value (fdh->elf.root.u.def.section,
6474 fdh->elf.root.u.def.value,
6475 &fh->elf.root.u.def.section,
6476 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6477 {
6478 fh->elf.root.type = fdh->elf.root.type;
6479 fh->elf.forced_local = 1;
6480 fh->elf.def_regular = fdh->elf.def_regular;
6481 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6482 }
6483
6484 if (!fh->elf.dynamic)
6485 {
6486 struct plt_entry *ent;
6487
6488 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6489 if (ent->plt.refcount > 0)
6490 break;
6491 if (ent == NULL)
6492 {
6493 if (fdh != NULL && fdh->fake)
6494 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6495 return true;
6496 }
6497 }
6498
6499 /* Create a descriptor as undefined if necessary. */
6500 if (fdh == NULL
6501 && !bfd_link_executable (info)
6502 && (fh->elf.root.type == bfd_link_hash_undefined
6503 || fh->elf.root.type == bfd_link_hash_undefweak))
6504 {
6505 fdh = make_fdh (info, fh);
6506 if (fdh == NULL)
6507 return false;
6508 }
6509
6510 /* We can't support overriding of symbols on a fake descriptor. */
6511 if (fdh != NULL
6512 && fdh->fake
6513 && (fh->elf.root.type == bfd_link_hash_defined
6514 || fh->elf.root.type == bfd_link_hash_defweak))
6515 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6516
6517 /* Transfer dynamic linking information to the function descriptor. */
6518 if (fdh != NULL)
6519 {
6520 fdh->elf.ref_regular |= fh->elf.ref_regular;
6521 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6522 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6523 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6524 fdh->elf.dynamic |= fh->elf.dynamic;
6525 fdh->elf.needs_plt |= (fh->elf.needs_plt
6526 || fh->elf.type == STT_FUNC
6527 || fh->elf.type == STT_GNU_IFUNC);
6528 move_plt_plist (fh, fdh);
6529
6530 if (!fdh->elf.forced_local
6531 && fh->elf.dynindx != -1)
6532 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6533 return false;
6534 }
6535
6536 /* Now that the info is on the function descriptor, clear the
6537 function code sym info. Any function code syms for which we
6538 don't have a definition in a regular file, we force local.
6539 This prevents a shared library from exporting syms that have
6540 been imported from another library. Function code syms that
6541 are really in the library we must leave global to prevent the
6542 linker dragging in a definition from a static library. */
6543 force_local = (!fh->elf.def_regular
6544 || fdh == NULL
6545 || !fdh->elf.def_regular
6546 || fdh->elf.forced_local);
6547 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6548
6549 return true;
6550}
6551
6552static const struct sfpr_def_parms save_res_funcs[] =
6553 {
6554 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6555 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6556 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6557 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6558 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6559 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6560 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6561 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6562 { "._savef", 14, 31, savefpr, savefpr1_tail },
6563 { "._restf", 14, 31, restfpr, restfpr1_tail },
6564 { "_savevr_", 20, 31, savevr, savevr_tail },
6565 { "_restvr_", 20, 31, restvr, restvr_tail }
6566 };
6567
6568/* Called near the start of bfd_elf_size_dynamic_sections. We use
6569 this hook to a) run the edit functions in this file, b) provide
6570 some gcc support functions, and c) transfer dynamic linking
6571 information gathered so far on function code symbol entries, to
6572 their corresponding function descriptor symbol entries. */
6573
6574static bool
6575ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6576{
6577 struct ppc_link_hash_table *htab;
6578
6579 htab = ppc_hash_table (info);
6580 if (htab == NULL)
6581 return false;
6582
6583 /* Call back into the linker, which then runs the edit functions. */
6584 htab->params->edit ();
6585
6586 /* Provide any missing _save* and _rest* functions. */
6587 if (htab->sfpr != NULL)
6588 {
6589 unsigned int i;
6590
6591 htab->sfpr->size = 0;
6592 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6593 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6594 return false;
6595 if (htab->sfpr->size == 0)
6596 htab->sfpr->flags |= SEC_EXCLUDE;
6597 }
6598
6599 if (bfd_link_relocatable (info))
6600 return true;
6601
6602 if (htab->elf.hgot != NULL)
6603 {
6604 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6605 /* Make .TOC. defined so as to prevent it being made dynamic.
6606 The wrong value here is fixed later in ppc64_elf_set_toc. */
6607 if (!htab->elf.hgot->def_regular
6608 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6609 {
6610 htab->elf.hgot->root.type = bfd_link_hash_defined;
6611 htab->elf.hgot->root.u.def.value = 0;
6612 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6613 htab->elf.hgot->def_regular = 1;
6614 htab->elf.hgot->root.linker_def = 1;
6615 }
6616 htab->elf.hgot->type = STT_OBJECT;
6617 htab->elf.hgot->other
6618 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6619 }
6620
6621 return true;
6622}
6623
6624/* Return true if we have dynamic relocs against H or any of its weak
6625 aliases, that apply to read-only sections. Cannot be used after
6626 size_dynamic_sections. */
6627
6628static bool
6629alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6630{
6631 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6632 do
6633 {
6634 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6635 return true;
6636 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6637 }
6638 while (eh != NULL && &eh->elf != h);
6639
6640 return false;
6641}
6642
6643/* Return whether EH has pc-relative dynamic relocs. */
6644
6645static bool
6646pc_dynrelocs (struct ppc_link_hash_entry *eh)
6647{
6648 struct ppc_dyn_relocs *p;
6649
6650 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6651 if (p->pc_count != 0)
6652 return true;
6653 return false;
6654}
6655
6656/* Return true if a global entry stub will be created for H. Valid
6657 for ELFv2 before plt entries have been allocated. */
6658
6659static bool
6660global_entry_stub (struct elf_link_hash_entry *h)
6661{
6662 struct plt_entry *pent;
6663
6664 if (!h->pointer_equality_needed
6665 || h->def_regular)
6666 return false;
6667
6668 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6669 if (pent->plt.refcount > 0
6670 && pent->addend == 0)
6671 return true;
6672
6673 return false;
6674}
6675
6676/* Adjust a symbol defined by a dynamic object and referenced by a
6677 regular object. The current definition is in some section of the
6678 dynamic object, but we're not including those sections. We have to
6679 change the definition to something the rest of the link can
6680 understand. */
6681
6682static bool
6683ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6684 struct elf_link_hash_entry *h)
6685{
6686 struct ppc_link_hash_table *htab;
6687 asection *s, *srel;
6688
6689 htab = ppc_hash_table (info);
6690 if (htab == NULL)
6691 return false;
6692
6693 /* Deal with function syms. */
6694 if (h->type == STT_FUNC
6695 || h->type == STT_GNU_IFUNC
6696 || h->needs_plt)
6697 {
6698 bool local = (ppc_elf_hash_entry (h)->save_res
6699 || SYMBOL_CALLS_LOCAL (info, h)
6700 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6701 /* Discard dyn_relocs when non-pic if we've decided that a
6702 function symbol is local and not an ifunc. We keep dynamic
6703 relocs for ifuncs when local rather than always emitting a
6704 plt call stub for them and defining the symbol on the call
6705 stub. We can't do that for ELFv1 anyway (a function symbol
6706 is defined on a descriptor, not code) and it can be faster at
6707 run-time due to not needing to bounce through a stub. The
6708 dyn_relocs for ifuncs will be applied even in a static
6709 executable. */
6710 if (!bfd_link_pic (info)
6711 && h->type != STT_GNU_IFUNC
6712 && local)
6713 h->dyn_relocs = NULL;
6714
6715 /* Clear procedure linkage table information for any symbol that
6716 won't need a .plt entry. */
6717 struct plt_entry *ent;
6718 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6719 if (ent->plt.refcount > 0)
6720 break;
6721 if (ent == NULL
6722 || (h->type != STT_GNU_IFUNC
6723 && local
6724 && (htab->can_convert_all_inline_plt
6725 || (ppc_elf_hash_entry (h)->tls_mask
6726 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6727 {
6728 h->plt.plist = NULL;
6729 h->needs_plt = 0;
6730 h->pointer_equality_needed = 0;
6731 }
6732 else if (abiversion (info->output_bfd) >= 2)
6733 {
6734 /* Taking a function's address in a read/write section
6735 doesn't require us to define the function symbol in the
6736 executable on a global entry stub. A dynamic reloc can
6737 be used instead. The reason we prefer a few more dynamic
6738 relocs is that calling via a global entry stub costs a
6739 few more instructions, and pointer_equality_needed causes
6740 extra work in ld.so when resolving these symbols. */
6741 if (global_entry_stub (h))
6742 {
6743 if (!_bfd_elf_readonly_dynrelocs (h))
6744 {
6745 h->pointer_equality_needed = 0;
6746 /* If we haven't seen a branch reloc and the symbol
6747 isn't an ifunc then we don't need a plt entry. */
6748 if (!h->needs_plt)
6749 h->plt.plist = NULL;
6750 }
6751 else if (!bfd_link_pic (info))
6752 /* We are going to be defining the function symbol on the
6753 plt stub, so no dyn_relocs needed when non-pic. */
6754 h->dyn_relocs = NULL;
6755 }
6756
6757 /* ELFv2 function symbols can't have copy relocs. */
6758 return true;
6759 }
6760 else if (!h->needs_plt
6761 && !_bfd_elf_readonly_dynrelocs (h))
6762 {
6763 /* If we haven't seen a branch reloc and the symbol isn't an
6764 ifunc then we don't need a plt entry. */
6765 h->plt.plist = NULL;
6766 h->pointer_equality_needed = 0;
6767 return true;
6768 }
6769 }
6770 else
6771 h->plt.plist = NULL;
6772
6773 /* If this is a weak symbol, and there is a real definition, the
6774 processor independent code will have arranged for us to see the
6775 real definition first, and we can just use the same value. */
6776 if (h->is_weakalias)
6777 {
6778 struct elf_link_hash_entry *def = weakdef (h);
6779 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6780 h->root.u.def.section = def->root.u.def.section;
6781 h->root.u.def.value = def->root.u.def.value;
6782 if (def->root.u.def.section == htab->elf.sdynbss
6783 || def->root.u.def.section == htab->elf.sdynrelro)
6784 h->dyn_relocs = NULL;
6785 return true;
6786 }
6787
6788 /* If we are creating a shared library, we must presume that the
6789 only references to the symbol are via the global offset table.
6790 For such cases we need not do anything here; the relocations will
6791 be handled correctly by relocate_section. */
6792 if (!bfd_link_executable (info))
6793 return true;
6794
6795 /* If there are no references to this symbol that do not use the
6796 GOT, we don't need to generate a copy reloc. */
6797 if (!h->non_got_ref)
6798 return true;
6799
6800 /* Don't generate a copy reloc for symbols defined in the executable. */
6801 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6802
6803 /* If -z nocopyreloc was given, don't generate them either. */
6804 || info->nocopyreloc
6805
6806 /* If we don't find any dynamic relocs in read-only sections, then
6807 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6808 || (ELIMINATE_COPY_RELOCS
6809 && !h->needs_copy
6810 && !alias_readonly_dynrelocs (h))
6811
6812 /* Protected variables do not work with .dynbss. The copy in
6813 .dynbss won't be used by the shared library with the protected
6814 definition for the variable. Text relocations are preferable
6815 to an incorrect program. */
6816 || h->protected_def)
6817 return true;
6818
6819 if (h->type == STT_FUNC
6820 || h->type == STT_GNU_IFUNC)
6821 {
6822 /* .dynbss copies of function symbols only work if we have
6823 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6824 use dot-symbols and set the function symbol size to the text
6825 size of the function rather than the size of the descriptor.
6826 That's wrong for copying a descriptor. */
6827 if (ppc_elf_hash_entry (h)->oh == NULL
6828 || !(h->size == 24 || h->size == 16))
6829 return true;
6830
6831 /* We should never get here, but unfortunately there are old
6832 versions of gcc (circa gcc-3.2) that improperly for the
6833 ELFv1 ABI put initialized function pointers, vtable refs and
6834 suchlike in read-only sections. Allow them to proceed, but
6835 warn that this might break at runtime. */
6836 info->callbacks->einfo
6837 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6838 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6839 h->root.root.string);
6840 }
6841
6842 /* This is a reference to a symbol defined by a dynamic object which
6843 is not a function. */
6844
6845 /* We must allocate the symbol in our .dynbss section, which will
6846 become part of the .bss section of the executable. There will be
6847 an entry for this symbol in the .dynsym section. The dynamic
6848 object will contain position independent code, so all references
6849 from the dynamic object to this symbol will go through the global
6850 offset table. The dynamic linker will use the .dynsym entry to
6851 determine the address it must put in the global offset table, so
6852 both the dynamic object and the regular object will refer to the
6853 same memory location for the variable. */
6854 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6855 {
6856 s = htab->elf.sdynrelro;
6857 srel = htab->elf.sreldynrelro;
6858 }
6859 else
6860 {
6861 s = htab->elf.sdynbss;
6862 srel = htab->elf.srelbss;
6863 }
6864 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6865 {
6866 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6867 linker to copy the initial value out of the dynamic object
6868 and into the runtime process image. */
6869 srel->size += sizeof (Elf64_External_Rela);
6870 h->needs_copy = 1;
6871 }
6872
6873 /* We no longer want dyn_relocs. */
6874 h->dyn_relocs = NULL;
6875 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6876}
6877
6878/* If given a function descriptor symbol, hide both the function code
6879 sym and the descriptor. */
6880static void
6881ppc64_elf_hide_symbol (struct bfd_link_info *info,
6882 struct elf_link_hash_entry *h,
6883 bool force_local)
6884{
6885 struct ppc_link_hash_entry *eh;
6886 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6887
6888 if (ppc_hash_table (info) == NULL)
6889 return;
6890
6891 eh = ppc_elf_hash_entry (h);
6892 if (eh->is_func_descriptor)
6893 {
6894 struct ppc_link_hash_entry *fh = eh->oh;
6895
6896 if (fh == NULL)
6897 {
6898 const char *p, *q;
6899 struct elf_link_hash_table *htab = elf_hash_table (info);
6900 char save;
6901
6902 /* We aren't supposed to use alloca in BFD because on
6903 systems which do not have alloca the version in libiberty
6904 calls xmalloc, which might cause the program to crash
6905 when it runs out of memory. This function doesn't have a
6906 return status, so there's no way to gracefully return an
6907 error. So cheat. We know that string[-1] can be safely
6908 accessed; It's either a string in an ELF string table,
6909 or allocated in an objalloc structure. */
6910
6911 p = eh->elf.root.root.string - 1;
6912 save = *p;
6913 *(char *) p = '.';
6914 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6915 false, false));
6916 *(char *) p = save;
6917
6918 /* Unfortunately, if it so happens that the string we were
6919 looking for was allocated immediately before this string,
6920 then we overwrote the string terminator. That's the only
6921 reason the lookup should fail. */
6922 if (fh == NULL)
6923 {
6924 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6925 while (q >= eh->elf.root.root.string && *q == *p)
6926 --q, --p;
6927 if (q < eh->elf.root.root.string && *p == '.')
6928 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6929 false, false));
6930 }
6931 if (fh != NULL)
6932 {
6933 eh->oh = fh;
6934 fh->oh = eh;
6935 }
6936 }
6937 if (fh != NULL)
6938 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6939 }
6940}
6941
6942static bool
6943get_sym_h (struct elf_link_hash_entry **hp,
6944 Elf_Internal_Sym **symp,
6945 asection **symsecp,
6946 unsigned char **tls_maskp,
6947 Elf_Internal_Sym **locsymsp,
6948 unsigned long r_symndx,
6949 bfd *ibfd)
6950{
6951 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6952
6953 if (r_symndx >= symtab_hdr->sh_info)
6954 {
6955 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6956 struct elf_link_hash_entry *h;
6957
6958 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6959 h = elf_follow_link (h);
6960
6961 if (hp != NULL)
6962 *hp = h;
6963
6964 if (symp != NULL)
6965 *symp = NULL;
6966
6967 if (symsecp != NULL)
6968 {
6969 asection *symsec = NULL;
6970 if (h->root.type == bfd_link_hash_defined
6971 || h->root.type == bfd_link_hash_defweak)
6972 symsec = h->root.u.def.section;
6973 *symsecp = symsec;
6974 }
6975
6976 if (tls_maskp != NULL)
6977 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6978 }
6979 else
6980 {
6981 Elf_Internal_Sym *sym;
6982 Elf_Internal_Sym *locsyms = *locsymsp;
6983
6984 if (locsyms == NULL)
6985 {
6986 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6987 if (locsyms == NULL)
6988 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6989 symtab_hdr->sh_info,
6990 0, NULL, NULL, NULL);
6991 if (locsyms == NULL)
6992 return false;
6993 *locsymsp = locsyms;
6994 }
6995 sym = locsyms + r_symndx;
6996
6997 if (hp != NULL)
6998 *hp = NULL;
6999
7000 if (symp != NULL)
7001 *symp = sym;
7002
7003 if (symsecp != NULL)
7004 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7005
7006 if (tls_maskp != NULL)
7007 {
7008 struct got_entry **lgot_ents;
7009 unsigned char *tls_mask;
7010
7011 tls_mask = NULL;
7012 lgot_ents = elf_local_got_ents (ibfd);
7013 if (lgot_ents != NULL)
7014 {
7015 struct plt_entry **local_plt = (struct plt_entry **)
7016 (lgot_ents + symtab_hdr->sh_info);
7017 unsigned char *lgot_masks = (unsigned char *)
7018 (local_plt + symtab_hdr->sh_info);
7019 tls_mask = &lgot_masks[r_symndx];
7020 }
7021 *tls_maskp = tls_mask;
7022 }
7023 }
7024 return true;
7025}
7026
7027/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7028 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7029 type suitable for optimization, and 1 otherwise. */
7030
7031static int
7032get_tls_mask (unsigned char **tls_maskp,
7033 unsigned long *toc_symndx,
7034 bfd_vma *toc_addend,
7035 Elf_Internal_Sym **locsymsp,
7036 const Elf_Internal_Rela *rel,
7037 bfd *ibfd)
7038{
7039 unsigned long r_symndx;
7040 int next_r;
7041 struct elf_link_hash_entry *h;
7042 Elf_Internal_Sym *sym;
7043 asection *sec;
7044 bfd_vma off;
7045
7046 r_symndx = ELF64_R_SYM (rel->r_info);
7047 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7048 return 0;
7049
7050 if ((*tls_maskp != NULL
7051 && (**tls_maskp & TLS_TLS) != 0
7052 && **tls_maskp != (TLS_TLS | TLS_MARK))
7053 || sec == NULL
7054 || ppc64_elf_section_data (sec) == NULL
7055 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7056 return 1;
7057
7058 /* Look inside a TOC section too. */
7059 if (h != NULL)
7060 {
7061 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7062 off = h->root.u.def.value;
7063 }
7064 else
7065 off = sym->st_value;
7066 off += rel->r_addend;
7067 BFD_ASSERT (off % 8 == 0);
7068 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7069 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7070 if (toc_symndx != NULL)
7071 *toc_symndx = r_symndx;
7072 if (toc_addend != NULL)
7073 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7074 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7075 return 0;
7076 if ((h == NULL || is_static_defined (h))
7077 && (next_r == -1 || next_r == -2))
7078 return 1 - next_r;
7079 return 1;
7080}
7081
7082/* Find (or create) an entry in the tocsave hash table. */
7083
7084static struct tocsave_entry *
7085tocsave_find (struct ppc_link_hash_table *htab,
7086 enum insert_option insert,
7087 Elf_Internal_Sym **local_syms,
7088 const Elf_Internal_Rela *irela,
7089 bfd *ibfd)
7090{
7091 unsigned long r_indx;
7092 struct elf_link_hash_entry *h;
7093 Elf_Internal_Sym *sym;
7094 struct tocsave_entry ent, *p;
7095 hashval_t hash;
7096 struct tocsave_entry **slot;
7097
7098 r_indx = ELF64_R_SYM (irela->r_info);
7099 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7100 return NULL;
7101 if (ent.sec == NULL || ent.sec->output_section == NULL)
7102 {
7103 _bfd_error_handler
7104 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7105 return NULL;
7106 }
7107
7108 if (h != NULL)
7109 ent.offset = h->root.u.def.value;
7110 else
7111 ent.offset = sym->st_value;
7112 ent.offset += irela->r_addend;
7113
7114 hash = tocsave_htab_hash (&ent);
7115 slot = ((struct tocsave_entry **)
7116 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7117 if (slot == NULL)
7118 return NULL;
7119
7120 if (*slot == NULL)
7121 {
7122 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7123 if (p == NULL)
7124 return NULL;
7125 *p = ent;
7126 *slot = p;
7127 }
7128 return *slot;
7129}
7130
7131/* Adjust all global syms defined in opd sections. In gcc generated
7132 code for the old ABI, these will already have been done. */
7133
7134static bool
7135adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7136{
7137 struct ppc_link_hash_entry *eh;
7138 asection *sym_sec;
7139 struct _opd_sec_data *opd;
7140
7141 if (h->root.type == bfd_link_hash_indirect)
7142 return true;
7143
7144 if (h->root.type != bfd_link_hash_defined
7145 && h->root.type != bfd_link_hash_defweak)
7146 return true;
7147
7148 eh = ppc_elf_hash_entry (h);
7149 if (eh->adjust_done)
7150 return true;
7151
7152 sym_sec = eh->elf.root.u.def.section;
7153 opd = get_opd_info (sym_sec);
7154 if (opd != NULL && opd->adjust != NULL)
7155 {
7156 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7157 if (adjust == -1)
7158 {
7159 /* This entry has been deleted. */
7160 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7161 if (dsec == NULL)
7162 {
7163 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7164 if (discarded_section (dsec))
7165 {
7166 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7167 break;
7168 }
7169 }
7170 eh->elf.root.u.def.value = 0;
7171 eh->elf.root.u.def.section = dsec;
7172 }
7173 else
7174 eh->elf.root.u.def.value += adjust;
7175 eh->adjust_done = 1;
7176 }
7177 return true;
7178}
7179
7180/* Handles decrementing dynamic reloc counts for the reloc specified by
7181 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7182 have already been determined. */
7183
7184static bool
7185dec_dynrel_count (const Elf_Internal_Rela *rel,
7186 asection *sec,
7187 struct bfd_link_info *info,
7188 Elf_Internal_Sym **local_syms,
7189 struct elf_link_hash_entry *h,
7190 Elf_Internal_Sym *sym)
7191{
7192 enum elf_ppc64_reloc_type r_type;
7193 asection *sym_sec = NULL;
7194
7195 /* Can this reloc be dynamic? This switch, and later tests here
7196 should be kept in sync with the code in check_relocs. */
7197 r_type = ELF64_R_TYPE (rel->r_info);
7198 switch (r_type)
7199 {
7200 default:
7201 return true;
7202
7203 case R_PPC64_TOC16:
7204 case R_PPC64_TOC16_DS:
7205 case R_PPC64_TOC16_LO:
7206 case R_PPC64_TOC16_HI:
7207 case R_PPC64_TOC16_HA:
7208 case R_PPC64_TOC16_LO_DS:
7209 if (h == NULL)
7210 return true;
7211 break;
7212
7213 case R_PPC64_TPREL16:
7214 case R_PPC64_TPREL16_LO:
7215 case R_PPC64_TPREL16_HI:
7216 case R_PPC64_TPREL16_HA:
7217 case R_PPC64_TPREL16_DS:
7218 case R_PPC64_TPREL16_LO_DS:
7219 case R_PPC64_TPREL16_HIGH:
7220 case R_PPC64_TPREL16_HIGHA:
7221 case R_PPC64_TPREL16_HIGHER:
7222 case R_PPC64_TPREL16_HIGHERA:
7223 case R_PPC64_TPREL16_HIGHEST:
7224 case R_PPC64_TPREL16_HIGHESTA:
7225 case R_PPC64_TPREL64:
7226 case R_PPC64_TPREL34:
7227 case R_PPC64_DTPMOD64:
7228 case R_PPC64_DTPREL64:
7229 case R_PPC64_ADDR64:
7230 case R_PPC64_REL30:
7231 case R_PPC64_REL32:
7232 case R_PPC64_REL64:
7233 case R_PPC64_ADDR14:
7234 case R_PPC64_ADDR14_BRNTAKEN:
7235 case R_PPC64_ADDR14_BRTAKEN:
7236 case R_PPC64_ADDR16:
7237 case R_PPC64_ADDR16_DS:
7238 case R_PPC64_ADDR16_HA:
7239 case R_PPC64_ADDR16_HI:
7240 case R_PPC64_ADDR16_HIGH:
7241 case R_PPC64_ADDR16_HIGHA:
7242 case R_PPC64_ADDR16_HIGHER:
7243 case R_PPC64_ADDR16_HIGHERA:
7244 case R_PPC64_ADDR16_HIGHEST:
7245 case R_PPC64_ADDR16_HIGHESTA:
7246 case R_PPC64_ADDR16_LO:
7247 case R_PPC64_ADDR16_LO_DS:
7248 case R_PPC64_ADDR24:
7249 case R_PPC64_ADDR32:
7250 case R_PPC64_UADDR16:
7251 case R_PPC64_UADDR32:
7252 case R_PPC64_UADDR64:
7253 case R_PPC64_TOC:
7254 case R_PPC64_D34:
7255 case R_PPC64_D34_LO:
7256 case R_PPC64_D34_HI30:
7257 case R_PPC64_D34_HA30:
7258 case R_PPC64_ADDR16_HIGHER34:
7259 case R_PPC64_ADDR16_HIGHERA34:
7260 case R_PPC64_ADDR16_HIGHEST34:
7261 case R_PPC64_ADDR16_HIGHESTA34:
7262 case R_PPC64_D28:
7263 break;
7264 }
7265
7266 if (local_syms != NULL)
7267 {
7268 unsigned long r_symndx;
7269 bfd *ibfd = sec->owner;
7270
7271 r_symndx = ELF64_R_SYM (rel->r_info);
7272 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7273 return false;
7274 }
7275
7276 if ((h != NULL
7277 && !SYMBOL_REFERENCES_LOCAL (info, h))
7278 || (bfd_link_pic (info)
7279 && (h != NULL
7280 ? !bfd_is_abs_symbol (&h->root)
7281 : sym_sec != bfd_abs_section_ptr)
7282 && must_be_dyn_reloc (info, r_type))
7283 || (!bfd_link_pic (info)
7284 && (h != NULL
7285 ? h->type == STT_GNU_IFUNC
7286 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7287 ;
7288 else
7289 return true;
7290
7291 if (h != NULL)
7292 {
7293 struct ppc_dyn_relocs *p;
7294 struct ppc_dyn_relocs **pp;
7295 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7296
7297 /* elf_gc_sweep may have already removed all dyn relocs associated
7298 with local syms for a given section. Also, symbol flags are
7299 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7300 report a dynreloc miscount. */
7301 if (*pp == NULL && info->gc_sections)
7302 return true;
7303
7304 while ((p = *pp) != NULL)
7305 {
7306 if (p->sec == sec)
7307 {
7308 if (!must_be_dyn_reloc (info, r_type))
7309 p->pc_count -= 1;
7310 if (maybe_relr (r_type, rel, sec))
7311 p->rel_count -= 1;
7312 p->count -= 1;
7313 if (p->count == 0)
7314 *pp = p->next;
7315 return true;
7316 }
7317 pp = &p->next;
7318 }
7319 }
7320 else
7321 {
7322 struct ppc_local_dyn_relocs *p;
7323 struct ppc_local_dyn_relocs **pp;
7324 void *vpp;
7325 bool is_ifunc;
7326
7327 if (local_syms == NULL)
7328 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7329 if (sym_sec == NULL)
7330 sym_sec = sec;
7331
7332 vpp = &elf_section_data (sym_sec)->local_dynrel;
7333 pp = (struct ppc_local_dyn_relocs **) vpp;
7334
7335 if (*pp == NULL && info->gc_sections)
7336 return true;
7337
7338 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7339 while ((p = *pp) != NULL)
7340 {
7341 if (p->sec == sec && p->ifunc == is_ifunc)
7342 {
7343 if (maybe_relr (r_type, rel, sec))
7344 p->rel_count -= 1;
7345 p->count -= 1;
7346 if (p->count == 0)
7347 *pp = p->next;
7348 return true;
7349 }
7350 pp = &p->next;
7351 }
7352 }
7353
7354 /* xgettext:c-format */
7355 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7356 sec->owner, sec);
7357 bfd_set_error (bfd_error_bad_value);
7358 return false;
7359}
7360
7361/* Remove unused Official Procedure Descriptor entries. Currently we
7362 only remove those associated with functions in discarded link-once
7363 sections, or weakly defined functions that have been overridden. It
7364 would be possible to remove many more entries for statically linked
7365 applications. */
7366
7367bool
7368ppc64_elf_edit_opd (struct bfd_link_info *info)
7369{
7370 bfd *ibfd;
7371 bool some_edited = false;
7372 asection *need_pad = NULL;
7373 struct ppc_link_hash_table *htab;
7374
7375 htab = ppc_hash_table (info);
7376 if (htab == NULL)
7377 return false;
7378
7379 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7380 {
7381 asection *sec;
7382 Elf_Internal_Rela *relstart, *rel, *relend;
7383 Elf_Internal_Shdr *symtab_hdr;
7384 Elf_Internal_Sym *local_syms;
7385 struct _opd_sec_data *opd;
7386 bool need_edit, add_aux_fields, broken;
7387 bfd_size_type cnt_16b = 0;
7388
7389 if (!is_ppc64_elf (ibfd))
7390 continue;
7391
7392 sec = bfd_get_section_by_name (ibfd, ".opd");
7393 if (sec == NULL
7394 || sec->size == 0
7395 || (sec->flags & SEC_HAS_CONTENTS) == 0)
7396 continue;
7397
7398 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7399 continue;
7400
7401 if (sec->output_section == bfd_abs_section_ptr)
7402 continue;
7403
7404 /* Look through the section relocs. */
7405 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7406 continue;
7407
7408 local_syms = NULL;
7409 symtab_hdr = &elf_symtab_hdr (ibfd);
7410
7411 /* Read the relocations. */
7412 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7413 info->keep_memory);
7414 if (relstart == NULL)
7415 return false;
7416
7417 /* First run through the relocs to check they are sane, and to
7418 determine whether we need to edit this opd section. */
7419 need_edit = false;
7420 broken = false;
7421 need_pad = sec;
7422 relend = relstart + sec->reloc_count;
7423 for (rel = relstart; rel < relend; )
7424 {
7425 enum elf_ppc64_reloc_type r_type;
7426 unsigned long r_symndx;
7427 asection *sym_sec;
7428 struct elf_link_hash_entry *h;
7429 Elf_Internal_Sym *sym;
7430 bfd_vma offset;
7431
7432 /* .opd contains an array of 16 or 24 byte entries. We're
7433 only interested in the reloc pointing to a function entry
7434 point. */
7435 offset = rel->r_offset;
7436 if (rel + 1 == relend
7437 || rel[1].r_offset != offset + 8)
7438 {
7439 /* If someone messes with .opd alignment then after a
7440 "ld -r" we might have padding in the middle of .opd.
7441 Also, there's nothing to prevent someone putting
7442 something silly in .opd with the assembler. No .opd
7443 optimization for them! */
7444 broken_opd:
7445 _bfd_error_handler
7446 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7447 broken = true;
7448 break;
7449 }
7450
7451 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7452 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7453 {
7454 _bfd_error_handler
7455 /* xgettext:c-format */
7456 (_("%pB: unexpected reloc type %u in .opd section"),
7457 ibfd, r_type);
7458 broken = true;
7459 break;
7460 }
7461
7462 r_symndx = ELF64_R_SYM (rel->r_info);
7463 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7464 r_symndx, ibfd))
7465 goto error_ret;
7466
7467 if (sym_sec == NULL || sym_sec->owner == NULL)
7468 {
7469 const char *sym_name;
7470 if (h != NULL)
7471 sym_name = h->root.root.string;
7472 else
7473 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7474 sym_sec);
7475
7476 _bfd_error_handler
7477 /* xgettext:c-format */
7478 (_("%pB: undefined sym `%s' in .opd section"),
7479 ibfd, sym_name);
7480 broken = true;
7481 break;
7482 }
7483
7484 /* opd entries are always for functions defined in the
7485 current input bfd. If the symbol isn't defined in the
7486 input bfd, then we won't be using the function in this
7487 bfd; It must be defined in a linkonce section in another
7488 bfd, or is weak. It's also possible that we are
7489 discarding the function due to a linker script /DISCARD/,
7490 which we test for via the output_section. */
7491 if (sym_sec->owner != ibfd
7492 || sym_sec->output_section == bfd_abs_section_ptr)
7493 need_edit = true;
7494
7495 rel += 2;
7496 if (rel + 1 == relend
7497 || (rel + 2 < relend
7498 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7499 ++rel;
7500
7501 if (rel == relend)
7502 {
7503 if (sec->size == offset + 24)
7504 {
7505 need_pad = NULL;
7506 break;
7507 }
7508 if (sec->size == offset + 16)
7509 {
7510 cnt_16b++;
7511 break;
7512 }
7513 goto broken_opd;
7514 }
7515 else if (rel + 1 < relend
7516 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7517 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7518 {
7519 if (rel[0].r_offset == offset + 16)
7520 cnt_16b++;
7521 else if (rel[0].r_offset != offset + 24)
7522 goto broken_opd;
7523 }
7524 else
7525 goto broken_opd;
7526 }
7527
7528 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7529
7530 if (!broken && (need_edit || add_aux_fields))
7531 {
7532 Elf_Internal_Rela *write_rel;
7533 Elf_Internal_Shdr *rel_hdr;
7534 bfd_byte *rptr, *wptr;
7535 bfd_byte *new_contents;
7536 bfd_size_type amt;
7537
7538 new_contents = NULL;
7539 amt = OPD_NDX (sec->size) * sizeof (long);
7540 opd = &ppc64_elf_section_data (sec)->u.opd;
7541 opd->adjust = bfd_zalloc (sec->owner, amt);
7542 if (opd->adjust == NULL)
7543 return false;
7544
7545 /* This seems a waste of time as input .opd sections are all
7546 zeros as generated by gcc, but I suppose there's no reason
7547 this will always be so. We might start putting something in
7548 the third word of .opd entries. */
7549 if ((sec->flags & SEC_IN_MEMORY) == 0)
7550 {
7551 bfd_byte *loc;
7552 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7553 {
7554 free (loc);
7555 error_ret:
7556 if (symtab_hdr->contents != (unsigned char *) local_syms)
7557 free (local_syms);
7558 if (elf_section_data (sec)->relocs != relstart)
7559 free (relstart);
7560 return false;
7561 }
7562 sec->contents = loc;
7563 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7564 }
7565
7566 elf_section_data (sec)->relocs = relstart;
7567
7568 new_contents = sec->contents;
7569 if (add_aux_fields)
7570 {
7571 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7572 if (new_contents == NULL)
7573 return false;
7574 need_pad = NULL;
7575 }
7576 wptr = new_contents;
7577 rptr = sec->contents;
7578 write_rel = relstart;
7579 for (rel = relstart; rel < relend; )
7580 {
7581 unsigned long r_symndx;
7582 asection *sym_sec;
7583 struct elf_link_hash_entry *h;
7584 struct ppc_link_hash_entry *fdh = NULL;
7585 Elf_Internal_Sym *sym;
7586 long opd_ent_size;
7587 Elf_Internal_Rela *next_rel;
7588 bool skip;
7589
7590 r_symndx = ELF64_R_SYM (rel->r_info);
7591 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7592 r_symndx, ibfd))
7593 goto error_ret;
7594
7595 next_rel = rel + 2;
7596 if (next_rel + 1 == relend
7597 || (next_rel + 2 < relend
7598 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7599 ++next_rel;
7600
7601 /* See if the .opd entry is full 24 byte or
7602 16 byte (with fd_aux entry overlapped with next
7603 fd_func). */
7604 opd_ent_size = 24;
7605 if (next_rel == relend)
7606 {
7607 if (sec->size == rel->r_offset + 16)
7608 opd_ent_size = 16;
7609 }
7610 else if (next_rel->r_offset == rel->r_offset + 16)
7611 opd_ent_size = 16;
7612
7613 if (h != NULL
7614 && h->root.root.string[0] == '.')
7615 {
7616 fdh = ppc_elf_hash_entry (h)->oh;
7617 if (fdh != NULL)
7618 {
7619 fdh = ppc_follow_link (fdh);
7620 if (fdh->elf.root.type != bfd_link_hash_defined
7621 && fdh->elf.root.type != bfd_link_hash_defweak)
7622 fdh = NULL;
7623 }
7624 }
7625
7626 skip = (sym_sec->owner != ibfd
7627 || sym_sec->output_section == bfd_abs_section_ptr);
7628 if (skip)
7629 {
7630 if (fdh != NULL && sym_sec->owner == ibfd)
7631 {
7632 /* Arrange for the function descriptor sym
7633 to be dropped. */
7634 fdh->elf.root.u.def.value = 0;
7635 fdh->elf.root.u.def.section = sym_sec;
7636 }
7637 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7638
7639 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7640 rel = next_rel;
7641 else
7642 while (1)
7643 {
7644 if (!dec_dynrel_count (rel, sec, info,
7645 NULL, h, sym))
7646 goto error_ret;
7647
7648 if (++rel == next_rel)
7649 break;
7650
7651 r_symndx = ELF64_R_SYM (rel->r_info);
7652 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7653 r_symndx, ibfd))
7654 goto error_ret;
7655 }
7656 }
7657 else
7658 {
7659 /* We'll be keeping this opd entry. */
7660 long adjust;
7661
7662 if (fdh != NULL)
7663 {
7664 /* Redefine the function descriptor symbol to
7665 this location in the opd section. It is
7666 necessary to update the value here rather
7667 than using an array of adjustments as we do
7668 for local symbols, because various places
7669 in the generic ELF code use the value
7670 stored in u.def.value. */
7671 fdh->elf.root.u.def.value = wptr - new_contents;
7672 fdh->adjust_done = 1;
7673 }
7674
7675 /* Local syms are a bit tricky. We could
7676 tweak them as they can be cached, but
7677 we'd need to look through the local syms
7678 for the function descriptor sym which we
7679 don't have at the moment. So keep an
7680 array of adjustments. */
7681 adjust = (wptr - new_contents) - (rptr - sec->contents);
7682 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7683
7684 if (wptr != rptr)
7685 memcpy (wptr, rptr, opd_ent_size);
7686 wptr += opd_ent_size;
7687 if (add_aux_fields && opd_ent_size == 16)
7688 {
7689 memset (wptr, '\0', 8);
7690 wptr += 8;
7691 }
7692
7693 /* We need to adjust any reloc offsets to point to the
7694 new opd entries. */
7695 for ( ; rel != next_rel; ++rel)
7696 {
7697 rel->r_offset += adjust;
7698 if (write_rel != rel)
7699 memcpy (write_rel, rel, sizeof (*rel));
7700 ++write_rel;
7701 }
7702 }
7703
7704 rptr += opd_ent_size;
7705 }
7706
7707 sec->size = wptr - new_contents;
7708 sec->reloc_count = write_rel - relstart;
7709 if (add_aux_fields)
7710 {
7711 free (sec->contents);
7712 sec->contents = new_contents;
7713 }
7714
7715 /* Fudge the header size too, as this is used later in
7716 elf_bfd_final_link if we are emitting relocs. */
7717 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7718 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7719 some_edited = true;
7720 }
7721 else if (elf_section_data (sec)->relocs != relstart)
7722 free (relstart);
7723
7724 if (local_syms != NULL
7725 && symtab_hdr->contents != (unsigned char *) local_syms)
7726 {
7727 if (!info->keep_memory)
7728 free (local_syms);
7729 else
7730 symtab_hdr->contents = (unsigned char *) local_syms;
7731 }
7732 }
7733
7734 if (some_edited)
7735 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7736
7737 /* If we are doing a final link and the last .opd entry is just 16 byte
7738 long, add a 8 byte padding after it. */
7739 if (need_pad != NULL && !bfd_link_relocatable (info))
7740 {
7741 bfd_byte *p;
7742
7743 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7744 {
7745 BFD_ASSERT (need_pad->size > 0);
7746
7747 p = bfd_malloc (need_pad->size + 8);
7748 if (p == NULL)
7749 return false;
7750
7751 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7752 p, 0, need_pad->size))
7753 return false;
7754
7755 need_pad->contents = p;
7756 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7757 }
7758 else
7759 {
7760 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7761 if (p == NULL)
7762 return false;
7763
7764 need_pad->contents = p;
7765 }
7766
7767 memset (need_pad->contents + need_pad->size, 0, 8);
7768 need_pad->size += 8;
7769 }
7770
7771 return true;
7772}
7773
7774/* Analyze inline PLT call relocations to see whether calls to locally
7775 defined functions can be converted to direct calls. */
7776
7777bool
7778ppc64_elf_inline_plt (struct bfd_link_info *info)
7779{
7780 struct ppc_link_hash_table *htab;
7781 bfd *ibfd;
7782 asection *sec;
7783 bfd_vma low_vma, high_vma, limit;
7784
7785 htab = ppc_hash_table (info);
7786 if (htab == NULL)
7787 return false;
7788
7789 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7790 reduced somewhat to cater for possible stubs that might be added
7791 between the call and its destination. */
7792 if (htab->params->group_size < 0)
7793 {
7794 limit = -htab->params->group_size;
7795 if (limit == 1)
7796 limit = 0x1e00000;
7797 }
7798 else
7799 {
7800 limit = htab->params->group_size;
7801 if (limit == 1)
7802 limit = 0x1c00000;
7803 }
7804
7805 low_vma = -1;
7806 high_vma = 0;
7807 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7808 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7809 {
7810 if (low_vma > sec->vma)
7811 low_vma = sec->vma;
7812 if (high_vma < sec->vma + sec->size)
7813 high_vma = sec->vma + sec->size;
7814 }
7815
7816 /* If a "bl" can reach anywhere in local code sections, then we can
7817 convert all inline PLT sequences to direct calls when the symbol
7818 is local. */
7819 if (high_vma - low_vma < limit)
7820 {
7821 htab->can_convert_all_inline_plt = 1;
7822 return true;
7823 }
7824
7825 /* Otherwise, go looking through relocs for cases where a direct
7826 call won't reach. Mark the symbol on any such reloc to disable
7827 the optimization and keep the PLT entry as it seems likely that
7828 this will be better than creating trampolines. Note that this
7829 will disable the optimization for all inline PLT calls to a
7830 particular symbol, not just those that won't reach. The
7831 difficulty in doing a more precise optimization is that the
7832 linker needs to make a decision depending on whether a
7833 particular R_PPC64_PLTCALL insn can be turned into a direct
7834 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7835 the sequence, and there is nothing that ties those relocs
7836 together except their symbol. */
7837
7838 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7839 {
7840 Elf_Internal_Shdr *symtab_hdr;
7841 Elf_Internal_Sym *local_syms;
7842
7843 if (!is_ppc64_elf (ibfd))
7844 continue;
7845
7846 local_syms = NULL;
7847 symtab_hdr = &elf_symtab_hdr (ibfd);
7848
7849 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7850 if (ppc64_elf_section_data (sec)->has_pltcall
7851 && !bfd_is_abs_section (sec->output_section))
7852 {
7853 Elf_Internal_Rela *relstart, *rel, *relend;
7854
7855 /* Read the relocations. */
7856 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7857 info->keep_memory);
7858 if (relstart == NULL)
7859 return false;
7860
7861 relend = relstart + sec->reloc_count;
7862 for (rel = relstart; rel < relend; rel++)
7863 {
7864 enum elf_ppc64_reloc_type r_type;
7865 unsigned long r_symndx;
7866 asection *sym_sec;
7867 struct elf_link_hash_entry *h;
7868 Elf_Internal_Sym *sym;
7869 unsigned char *tls_maskp;
7870
7871 r_type = ELF64_R_TYPE (rel->r_info);
7872 if (r_type != R_PPC64_PLTCALL
7873 && r_type != R_PPC64_PLTCALL_NOTOC)
7874 continue;
7875
7876 r_symndx = ELF64_R_SYM (rel->r_info);
7877 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7878 r_symndx, ibfd))
7879 {
7880 if (elf_section_data (sec)->relocs != relstart)
7881 free (relstart);
7882 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7883 free (local_syms);
7884 return false;
7885 }
7886
7887 if (sym_sec != NULL && sym_sec->output_section != NULL)
7888 {
7889 bfd_vma from, to;
7890 if (h != NULL)
7891 to = h->root.u.def.value;
7892 else
7893 to = sym->st_value;
7894 to += (rel->r_addend
7895 + sym_sec->output_offset
7896 + sym_sec->output_section->vma);
7897 from = (rel->r_offset
7898 + sec->output_offset
7899 + sec->output_section->vma);
7900 if (to - from + limit < 2 * limit
7901 && !(r_type == R_PPC64_PLTCALL_NOTOC
7902 && (((h ? h->other : sym->st_other)
7903 & STO_PPC64_LOCAL_MASK)
7904 > 1 << STO_PPC64_LOCAL_BIT)))
7905 *tls_maskp &= ~PLT_KEEP;
7906 }
7907 }
7908 if (elf_section_data (sec)->relocs != relstart)
7909 free (relstart);
7910 }
7911
7912 if (local_syms != NULL
7913 && symtab_hdr->contents != (unsigned char *) local_syms)
7914 {
7915 if (!info->keep_memory)
7916 free (local_syms);
7917 else
7918 symtab_hdr->contents = (unsigned char *) local_syms;
7919 }
7920 }
7921
7922 return true;
7923}
7924
7925/* Set htab->tls_get_addr and various other info specific to TLS.
7926 This needs to run before dynamic symbols are processed in
7927 bfd_elf_size_dynamic_sections. */
7928
7929bool
7930ppc64_elf_tls_setup (struct bfd_link_info *info)
7931{
7932 struct ppc_link_hash_table *htab;
7933 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7934
7935 htab = ppc_hash_table (info);
7936 if (htab == NULL)
7937 return false;
7938
7939 /* Move dynamic linking info to the function descriptor sym. */
7940 if (htab->need_func_desc_adj)
7941 {
7942 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7943 htab->need_func_desc_adj = 0;
7944 }
7945
7946 if (abiversion (info->output_bfd) == 1)
7947 htab->opd_abi = 1;
7948
7949 if (htab->params->no_multi_toc)
7950 htab->do_multi_toc = 0;
7951 else if (!htab->do_multi_toc)
7952 htab->params->no_multi_toc = 1;
7953
7954 /* Default to --no-plt-localentry, as this option can cause problems
7955 with symbol interposition. For example, glibc libpthread.so and
7956 libc.so duplicate many pthread symbols, with a fallback
7957 implementation in libc.so. In some cases the fallback does more
7958 work than the pthread implementation. __pthread_condattr_destroy
7959 is one such symbol: the libpthread.so implementation is
7960 localentry:0 while the libc.so implementation is localentry:8.
7961 An app that "cleverly" uses dlopen to only load necessary
7962 libraries at runtime may omit loading libpthread.so when not
7963 running multi-threaded, which then results in the libc.so
7964 fallback symbols being used and ld.so complaining. Now there
7965 are workarounds in ld (see non_zero_localentry) to detect the
7966 pthread situation, but that may not be the only case where
7967 --plt-localentry can cause trouble. */
7968 if (htab->params->plt_localentry0 < 0)
7969 htab->params->plt_localentry0 = 0;
7970 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7971 {
7972 /* The issue is that __glink_PLTresolve saves r2, which is done
7973 because glibc ld.so _dl_runtime_resolve restores r2 to support
7974 a glibc plt call optimisation where global entry code is
7975 skipped on calls that resolve to the same binary. The
7976 __glink_PLTresolve save of r2 is incompatible with code
7977 making tail calls, because the tail call might go via the
7978 resolver and thus overwrite the proper saved r2. */
7979 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7980 "power10 pc-relative code"));
7981 htab->params->plt_localentry0 = 0;
7982 }
7983 if (htab->params->plt_localentry0
7984 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7985 false, false, false) == NULL)
7986 _bfd_error_handler
7987 (_("warning: --plt-localentry is especially dangerous without "
7988 "ld.so support to detect ABI violations"));
7989
7990 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7991 false, false, true);
7992 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7993 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7994 false, false, true);
7995 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7996
7997 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7998 false, false, true);
7999 htab->tga_desc = ppc_elf_hash_entry (desc);
8000 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
8001 false, false, true);
8002 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
8003
8004 if (htab->params->tls_get_addr_opt)
8005 {
8006 struct elf_link_hash_entry *opt, *opt_fd;
8007
8008 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8009 false, false, true);
8010 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8011 false, false, true);
8012 if (opt_fd != NULL
8013 && (opt_fd->root.type == bfd_link_hash_defined
8014 || opt_fd->root.type == bfd_link_hash_defweak))
8015 {
8016 /* If glibc supports an optimized __tls_get_addr call stub,
8017 signalled by the presence of __tls_get_addr_opt, and we'll
8018 be calling __tls_get_addr via a plt call stub, then
8019 make __tls_get_addr point to __tls_get_addr_opt. */
8020 if (!(htab->elf.dynamic_sections_created
8021 && tga_fd != NULL
8022 && (tga_fd->type == STT_FUNC
8023 || tga_fd->needs_plt)
8024 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8025 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8026 tga_fd = NULL;
8027 if (!(htab->elf.dynamic_sections_created
8028 && desc_fd != NULL
8029 && (desc_fd->type == STT_FUNC
8030 || desc_fd->needs_plt)
8031 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8032 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8033 desc_fd = NULL;
8034
8035 if (tga_fd != NULL || desc_fd != NULL)
8036 {
8037 struct plt_entry *ent = NULL;
8038
8039 if (tga_fd != NULL)
8040 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8041 if (ent->plt.refcount > 0)
8042 break;
8043 if (ent == NULL && desc_fd != NULL)
8044 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8045 if (ent->plt.refcount > 0)
8046 break;
8047 if (ent != NULL)
8048 {
8049 if (tga_fd != NULL)
8050 {
8051 tga_fd->root.type = bfd_link_hash_indirect;
8052 tga_fd->root.u.i.link = &opt_fd->root;
8053 tga_fd->root.u.i.warning = NULL;
8054 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8055 }
8056 if (desc_fd != NULL)
8057 {
8058 desc_fd->root.type = bfd_link_hash_indirect;
8059 desc_fd->root.u.i.link = &opt_fd->root;
8060 desc_fd->root.u.i.warning = NULL;
8061 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8062 }
8063 opt_fd->mark = 1;
8064 if (opt_fd->dynindx != -1)
8065 {
8066 /* Use __tls_get_addr_opt in dynamic relocations. */
8067 opt_fd->dynindx = -1;
8068 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8069 opt_fd->dynstr_index);
8070 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8071 return false;
8072 }
8073 if (tga_fd != NULL)
8074 {
8075 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8076 tga = elf_hash_entry (htab->tls_get_addr);
8077 if (opt != NULL && tga != NULL)
8078 {
8079 tga->root.type = bfd_link_hash_indirect;
8080 tga->root.u.i.link = &opt->root;
8081 tga->root.u.i.warning = NULL;
8082 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8083 opt->mark = 1;
8084 _bfd_elf_link_hash_hide_symbol (info, opt,
8085 tga->forced_local);
8086 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8087 }
8088 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8089 htab->tls_get_addr_fd->is_func_descriptor = 1;
8090 if (htab->tls_get_addr != NULL)
8091 {
8092 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8093 htab->tls_get_addr->is_func = 1;
8094 }
8095 }
8096 if (desc_fd != NULL)
8097 {
8098 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8099 if (opt != NULL && desc != NULL)
8100 {
8101 desc->root.type = bfd_link_hash_indirect;
8102 desc->root.u.i.link = &opt->root;
8103 desc->root.u.i.warning = NULL;
8104 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8105 opt->mark = 1;
8106 _bfd_elf_link_hash_hide_symbol (info, opt,
8107 desc->forced_local);
8108 htab->tga_desc = ppc_elf_hash_entry (opt);
8109 }
8110 htab->tga_desc_fd->oh = htab->tga_desc;
8111 htab->tga_desc_fd->is_func_descriptor = 1;
8112 if (htab->tga_desc != NULL)
8113 {
8114 htab->tga_desc->oh = htab->tga_desc_fd;
8115 htab->tga_desc->is_func = 1;
8116 }
8117 }
8118 }
8119 }
8120 }
8121 else if (htab->params->tls_get_addr_opt < 0)
8122 htab->params->tls_get_addr_opt = 0;
8123 }
8124
8125 if (htab->tga_desc_fd != NULL
8126 && htab->params->tls_get_addr_opt
8127 && htab->params->no_tls_get_addr_regsave == -1)
8128 htab->params->no_tls_get_addr_regsave = 0;
8129
8130 return true;
8131}
8132
8133/* Return TRUE iff REL is a branch reloc with a global symbol matching
8134 any of HASH1, HASH2, HASH3, or HASH4. */
8135
8136static bool
8137branch_reloc_hash_match (bfd *ibfd,
8138 Elf_Internal_Rela *rel,
8139 struct ppc_link_hash_entry *hash1,
8140 struct ppc_link_hash_entry *hash2,
8141 struct ppc_link_hash_entry *hash3,
8142 struct ppc_link_hash_entry *hash4)
8143{
8144 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8145 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8146 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8147
8148 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8149 {
8150 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8151 struct elf_link_hash_entry *h;
8152
8153 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8154 h = elf_follow_link (h);
8155 if (h == elf_hash_entry (hash1)
8156 || h == elf_hash_entry (hash2)
8157 || h == elf_hash_entry (hash3)
8158 || h == elf_hash_entry (hash4))
8159 return true;
8160 }
8161 return false;
8162}
8163
8164/* Run through all the TLS relocs looking for optimization
8165 opportunities. The linker has been hacked (see ppc64elf.em) to do
8166 a preliminary section layout so that we know the TLS segment
8167 offsets. We can't optimize earlier because some optimizations need
8168 to know the tp offset, and we need to optimize before allocating
8169 dynamic relocations. */
8170
8171bool
8172ppc64_elf_tls_optimize (struct bfd_link_info *info)
8173{
8174 bfd *ibfd;
8175 asection *sec;
8176 struct ppc_link_hash_table *htab;
8177 unsigned char *toc_ref;
8178 int pass;
8179
8180 if (!bfd_link_executable (info))
8181 return true;
8182
8183 htab = ppc_hash_table (info);
8184 if (htab == NULL)
8185 return false;
8186
8187 htab->do_tls_opt = 1;
8188
8189 /* Make two passes over the relocs. On the first pass, mark toc
8190 entries involved with tls relocs, and check that tls relocs
8191 involved in setting up a tls_get_addr call are indeed followed by
8192 such a call. If they are not, we can't do any tls optimization.
8193 On the second pass twiddle tls_mask flags to notify
8194 relocate_section that optimization can be done, and adjust got
8195 and plt refcounts. */
8196 toc_ref = NULL;
8197 for (pass = 0; pass < 2; ++pass)
8198 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8199 {
8200 Elf_Internal_Sym *locsyms = NULL;
8201 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8202
8203 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8204 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8205 {
8206 Elf_Internal_Rela *relstart, *rel, *relend;
8207 bool found_tls_get_addr_arg = 0;
8208
8209 /* Read the relocations. */
8210 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8211 info->keep_memory);
8212 if (relstart == NULL)
8213 {
8214 free (toc_ref);
8215 return false;
8216 }
8217
8218 relend = relstart + sec->reloc_count;
8219 for (rel = relstart; rel < relend; rel++)
8220 {
8221 enum elf_ppc64_reloc_type r_type;
8222 unsigned long r_symndx;
8223 struct elf_link_hash_entry *h;
8224 Elf_Internal_Sym *sym;
8225 asection *sym_sec;
8226 unsigned char *tls_mask;
8227 unsigned int tls_set, tls_clear, tls_type = 0;
8228 bfd_vma value;
8229 bool ok_tprel, is_local;
8230 long toc_ref_index = 0;
8231 int expecting_tls_get_addr = 0;
8232 bool ret = false;
8233
8234 r_symndx = ELF64_R_SYM (rel->r_info);
8235 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8236 r_symndx, ibfd))
8237 {
8238 err_free_rel:
8239 if (elf_section_data (sec)->relocs != relstart)
8240 free (relstart);
8241 free (toc_ref);
8242 if (elf_symtab_hdr (ibfd).contents
8243 != (unsigned char *) locsyms)
8244 free (locsyms);
8245 return ret;
8246 }
8247
8248 if (h != NULL)
8249 {
8250 if (h->root.type == bfd_link_hash_defined
8251 || h->root.type == bfd_link_hash_defweak)
8252 value = h->root.u.def.value;
8253 else if (h->root.type == bfd_link_hash_undefweak)
8254 value = 0;
8255 else
8256 {
8257 found_tls_get_addr_arg = 0;
8258 continue;
8259 }
8260 }
8261 else
8262 /* Symbols referenced by TLS relocs must be of type
8263 STT_TLS. So no need for .opd local sym adjust. */
8264 value = sym->st_value;
8265
8266 ok_tprel = false;
8267 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8268 if (is_local)
8269 {
8270 if (h != NULL
8271 && h->root.type == bfd_link_hash_undefweak)
8272 ok_tprel = true;
8273 else if (sym_sec != NULL
8274 && sym_sec->output_section != NULL)
8275 {
8276 value += sym_sec->output_offset;
8277 value += sym_sec->output_section->vma;
8278 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8279 /* Note that even though the prefix insns
8280 allow a 1<<33 offset we use the same test
8281 as for addis;addi. There may be a mix of
8282 pcrel and non-pcrel code and the decision
8283 to optimise is per symbol, not per TLS
8284 sequence. */
8285 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8286 }
8287 }
8288
8289 r_type = ELF64_R_TYPE (rel->r_info);
8290 /* If this section has old-style __tls_get_addr calls
8291 without marker relocs, then check that each
8292 __tls_get_addr call reloc is preceded by a reloc
8293 that conceivably belongs to the __tls_get_addr arg
8294 setup insn. If we don't find matching arg setup
8295 relocs, don't do any tls optimization. */
8296 if (pass == 0
8297 && sec->nomark_tls_get_addr
8298 && h != NULL
8299 && is_tls_get_addr (h, htab)
8300 && !found_tls_get_addr_arg
8301 && is_branch_reloc (r_type))
8302 {
8303 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8304 "TLS optimization disabled\n"),
8305 ibfd, sec, rel->r_offset);
8306 ret = true;
8307 goto err_free_rel;
8308 }
8309
8310 found_tls_get_addr_arg = 0;
8311 switch (r_type)
8312 {
8313 case R_PPC64_GOT_TLSLD16:
8314 case R_PPC64_GOT_TLSLD16_LO:
8315 case R_PPC64_GOT_TLSLD_PCREL34:
8316 expecting_tls_get_addr = 1;
8317 found_tls_get_addr_arg = 1;
8318 /* Fall through. */
8319
8320 case R_PPC64_GOT_TLSLD16_HI:
8321 case R_PPC64_GOT_TLSLD16_HA:
8322 /* These relocs should never be against a symbol
8323 defined in a shared lib. Leave them alone if
8324 that turns out to be the case. */
8325 if (!is_local)
8326 continue;
8327
8328 /* LD -> LE */
8329 tls_set = 0;
8330 tls_clear = TLS_LD;
8331 tls_type = TLS_TLS | TLS_LD;
8332 break;
8333
8334 case R_PPC64_GOT_TLSGD16:
8335 case R_PPC64_GOT_TLSGD16_LO:
8336 case R_PPC64_GOT_TLSGD_PCREL34:
8337 expecting_tls_get_addr = 1;
8338 found_tls_get_addr_arg = 1;
8339 /* Fall through. */
8340
8341 case R_PPC64_GOT_TLSGD16_HI:
8342 case R_PPC64_GOT_TLSGD16_HA:
8343 if (ok_tprel)
8344 /* GD -> LE */
8345 tls_set = 0;
8346 else
8347 /* GD -> IE */
8348 tls_set = TLS_TLS | TLS_GDIE;
8349 tls_clear = TLS_GD;
8350 tls_type = TLS_TLS | TLS_GD;
8351 break;
8352
8353 case R_PPC64_GOT_TPREL_PCREL34:
8354 case R_PPC64_GOT_TPREL16_DS:
8355 case R_PPC64_GOT_TPREL16_LO_DS:
8356 case R_PPC64_GOT_TPREL16_HI:
8357 case R_PPC64_GOT_TPREL16_HA:
8358 if (ok_tprel)
8359 {
8360 /* IE -> LE */
8361 tls_set = 0;
8362 tls_clear = TLS_TPREL;
8363 tls_type = TLS_TLS | TLS_TPREL;
8364 break;
8365 }
8366 continue;
8367
8368 case R_PPC64_TLSLD:
8369 if (!is_local)
8370 continue;
8371 /* Fall through. */
8372 case R_PPC64_TLSGD:
8373 if (rel + 1 < relend
8374 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8375 {
8376 if (pass != 0
8377 && (ELF64_R_TYPE (rel[1].r_info)
8378 != R_PPC64_PLTSEQ)
8379 && (ELF64_R_TYPE (rel[1].r_info)
8380 != R_PPC64_PLTSEQ_NOTOC))
8381 {
8382 r_symndx = ELF64_R_SYM (rel[1].r_info);
8383 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8384 r_symndx, ibfd))
8385 goto err_free_rel;
8386 if (h != NULL)
8387 {
8388 struct plt_entry *ent = NULL;
8389
8390 for (ent = h->plt.plist;
8391 ent != NULL;
8392 ent = ent->next)
8393 if (ent->addend == rel[1].r_addend)
8394 break;
8395
8396 if (ent != NULL
8397 && ent->plt.refcount > 0)
8398 ent->plt.refcount -= 1;
8399 }
8400 }
8401 continue;
8402 }
8403 found_tls_get_addr_arg = 1;
8404 /* Fall through. */
8405
8406 case R_PPC64_TLS:
8407 case R_PPC64_TOC16:
8408 case R_PPC64_TOC16_LO:
8409 if (sym_sec == NULL || sym_sec != toc)
8410 continue;
8411
8412 /* Mark this toc entry as referenced by a TLS
8413 code sequence. We can do that now in the
8414 case of R_PPC64_TLS, and after checking for
8415 tls_get_addr for the TOC16 relocs. */
8416 if (toc_ref == NULL)
8417 toc_ref
8418 = bfd_zmalloc (toc->output_section->rawsize / 8);
8419 if (toc_ref == NULL)
8420 goto err_free_rel;
8421
8422 if (h != NULL)
8423 value = h->root.u.def.value;
8424 else
8425 value = sym->st_value;
8426 value += rel->r_addend;
8427 if (value % 8 != 0)
8428 continue;
8429 BFD_ASSERT (value < toc->size
8430 && toc->output_offset % 8 == 0);
8431 toc_ref_index = (value + toc->output_offset) / 8;
8432 if (r_type == R_PPC64_TLS
8433 || r_type == R_PPC64_TLSGD
8434 || r_type == R_PPC64_TLSLD)
8435 {
8436 toc_ref[toc_ref_index] = 1;
8437 continue;
8438 }
8439
8440 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8441 continue;
8442
8443 tls_set = 0;
8444 tls_clear = 0;
8445 expecting_tls_get_addr = 2;
8446 break;
8447
8448 case R_PPC64_TPREL64:
8449 if (pass == 0
8450 || sec != toc
8451 || toc_ref == NULL
8452 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8453 continue;
8454 if (ok_tprel)
8455 {
8456 /* IE -> LE */
8457 tls_set = TLS_EXPLICIT;
8458 tls_clear = TLS_TPREL;
8459 break;
8460 }
8461 continue;
8462
8463 case R_PPC64_DTPMOD64:
8464 if (pass == 0
8465 || sec != toc
8466 || toc_ref == NULL
8467 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8468 continue;
8469 if (rel + 1 < relend
8470 && (rel[1].r_info
8471 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8472 && rel[1].r_offset == rel->r_offset + 8)
8473 {
8474 if (ok_tprel)
8475 /* GD -> LE */
8476 tls_set = TLS_EXPLICIT | TLS_GD;
8477 else
8478 /* GD -> IE */
8479 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8480 tls_clear = TLS_GD;
8481 }
8482 else
8483 {
8484 if (!is_local)
8485 continue;
8486
8487 /* LD -> LE */
8488 tls_set = TLS_EXPLICIT;
8489 tls_clear = TLS_LD;
8490 }
8491 break;
8492
8493 case R_PPC64_TPREL16_HA:
8494 if (pass == 0)
8495 {
8496 unsigned char buf[4];
8497 unsigned int insn;
8498 bfd_vma off = rel->r_offset & ~3;
8499 if (!bfd_get_section_contents (ibfd, sec, buf,
8500 off, 4))
8501 goto err_free_rel;
8502 insn = bfd_get_32 (ibfd, buf);
8503 /* addis rt,13,imm */
8504 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8505 != ((15u << 26) | (13 << 16)))
8506 {
8507 /* xgettext:c-format */
8508 info->callbacks->minfo
8509 (_("%H: warning: %s unexpected insn %#x.\n"),
8510 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8511 htab->do_tls_opt = 0;
8512 }
8513 }
8514 continue;
8515
8516 case R_PPC64_TPREL16_HI:
8517 case R_PPC64_TPREL16_HIGH:
8518 case R_PPC64_TPREL16_HIGHA:
8519 case R_PPC64_TPREL16_HIGHER:
8520 case R_PPC64_TPREL16_HIGHERA:
8521 case R_PPC64_TPREL16_HIGHEST:
8522 case R_PPC64_TPREL16_HIGHESTA:
8523 /* These can all be used in sequences along with
8524 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8525 able to verify easily. */
8526 htab->do_tls_opt = 0;
8527 continue;
8528
8529 default:
8530 continue;
8531 }
8532
8533 if (pass == 0)
8534 {
8535 if (!expecting_tls_get_addr
8536 || !sec->nomark_tls_get_addr)
8537 continue;
8538
8539 if (rel + 1 < relend
8540 && branch_reloc_hash_match (ibfd, rel + 1,
8541 htab->tls_get_addr_fd,
8542 htab->tga_desc_fd,
8543 htab->tls_get_addr,
8544 htab->tga_desc))
8545 {
8546 if (expecting_tls_get_addr == 2)
8547 {
8548 /* Check for toc tls entries. */
8549 unsigned char *toc_tls;
8550 int retval;
8551
8552 retval = get_tls_mask (&toc_tls, NULL, NULL,
8553 &locsyms,
8554 rel, ibfd);
8555 if (retval == 0)
8556 goto err_free_rel;
8557 if (toc_tls != NULL)
8558 {
8559 if ((*toc_tls & TLS_TLS) != 0
8560 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8561 found_tls_get_addr_arg = 1;
8562 if (retval > 1)
8563 toc_ref[toc_ref_index] = 1;
8564 }
8565 }
8566 continue;
8567 }
8568
8569 /* Uh oh, we didn't find the expected call. We
8570 could just mark this symbol to exclude it
8571 from tls optimization but it's safer to skip
8572 the entire optimization. */
8573 /* xgettext:c-format */
8574 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8575 "TLS optimization disabled\n"),
8576 ibfd, sec, rel->r_offset);
8577 ret = true;
8578 goto err_free_rel;
8579 }
8580
8581 /* If we don't have old-style __tls_get_addr calls
8582 without TLSGD/TLSLD marker relocs, and we haven't
8583 found a new-style __tls_get_addr call with a
8584 marker for this symbol, then we either have a
8585 broken object file or an -mlongcall style
8586 indirect call to __tls_get_addr without a marker.
8587 Disable optimization in this case. */
8588 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8589 && (tls_set & TLS_EXPLICIT) == 0
8590 && !sec->nomark_tls_get_addr
8591 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8592 != (TLS_TLS | TLS_MARK)))
8593 continue;
8594
8595 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8596 {
8597 struct plt_entry *ent = NULL;
8598
8599 if (htab->tls_get_addr_fd != NULL)
8600 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8601 ent != NULL;
8602 ent = ent->next)
8603 if (ent->addend == 0)
8604 break;
8605
8606 if (ent == NULL && htab->tga_desc_fd != NULL)
8607 for (ent = htab->tga_desc_fd->elf.plt.plist;
8608 ent != NULL;
8609 ent = ent->next)
8610 if (ent->addend == 0)
8611 break;
8612
8613 if (ent == NULL && htab->tls_get_addr != NULL)
8614 for (ent = htab->tls_get_addr->elf.plt.plist;
8615 ent != NULL;
8616 ent = ent->next)
8617 if (ent->addend == 0)
8618 break;
8619
8620 if (ent == NULL && htab->tga_desc != NULL)
8621 for (ent = htab->tga_desc->elf.plt.plist;
8622 ent != NULL;
8623 ent = ent->next)
8624 if (ent->addend == 0)
8625 break;
8626
8627 if (ent != NULL
8628 && ent->plt.refcount > 0)
8629 ent->plt.refcount -= 1;
8630 }
8631
8632 if (tls_clear == 0)
8633 continue;
8634
8635 if ((tls_set & TLS_EXPLICIT) == 0)
8636 {
8637 struct got_entry *ent;
8638
8639 /* Adjust got entry for this reloc. */
8640 if (h != NULL)
8641 ent = h->got.glist;
8642 else
8643 ent = elf_local_got_ents (ibfd)[r_symndx];
8644
8645 for (; ent != NULL; ent = ent->next)
8646 if (ent->addend == rel->r_addend
8647 && ent->owner == ibfd
8648 && ent->tls_type == tls_type)
8649 break;
8650 if (ent == NULL)
8651 abort ();
8652
8653 if (tls_set == 0)
8654 {
8655 /* We managed to get rid of a got entry. */
8656 if (ent->got.refcount > 0)
8657 ent->got.refcount -= 1;
8658 }
8659 }
8660 else
8661 {
8662 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8663 we'll lose one or two dyn relocs. */
8664 if (!dec_dynrel_count (rel, sec, info,
8665 NULL, h, sym))
8666 return false;
8667
8668 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8669 {
8670 if (!dec_dynrel_count (rel + 1, sec, info,
8671 NULL, h, sym))
8672 return false;
8673 }
8674 }
8675
8676 *tls_mask |= tls_set & 0xff;
8677 *tls_mask &= ~tls_clear;
8678 }
8679
8680 if (elf_section_data (sec)->relocs != relstart)
8681 free (relstart);
8682 }
8683
8684 if (locsyms != NULL
8685 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8686 {
8687 if (!info->keep_memory)
8688 free (locsyms);
8689 else
8690 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8691 }
8692 }
8693
8694 free (toc_ref);
8695 return true;
8696}
8697
8698/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8699 the values of any global symbols in a toc section that has been
8700 edited. Globals in toc sections should be a rarity, so this function
8701 sets a flag if any are found in toc sections other than the one just
8702 edited, so that further hash table traversals can be avoided. */
8703
8704struct adjust_toc_info
8705{
8706 asection *toc;
8707 unsigned long *skip;
8708 bool global_toc_syms;
8709};
8710
8711enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8712
8713static bool
8714adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8715{
8716 struct ppc_link_hash_entry *eh;
8717 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8718 unsigned long i;
8719
8720 if (h->root.type != bfd_link_hash_defined
8721 && h->root.type != bfd_link_hash_defweak)
8722 return true;
8723
8724 eh = ppc_elf_hash_entry (h);
8725 if (eh->adjust_done)
8726 return true;
8727
8728 if (eh->elf.root.u.def.section == toc_inf->toc)
8729 {
8730 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8731 i = toc_inf->toc->rawsize >> 3;
8732 else
8733 i = eh->elf.root.u.def.value >> 3;
8734
8735 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8736 {
8737 _bfd_error_handler
8738 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8739 do
8740 ++i;
8741 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8742 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8743 }
8744
8745 eh->elf.root.u.def.value -= toc_inf->skip[i];
8746 eh->adjust_done = 1;
8747 }
8748 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8749 toc_inf->global_toc_syms = true;
8750
8751 return true;
8752}
8753
8754/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8755 on a _LO variety toc/got reloc. */
8756
8757static bool
8758ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8759{
8760 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8761 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8762 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8763 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8764 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8765 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8766 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8767 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8768 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8769 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8770 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8771 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8772 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8773 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8774 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8775 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8776 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8777 /* Exclude lfqu by testing reloc. If relocs are ever
8778 defined for the reduced D field in psq_lu then those
8779 will need testing too. */
8780 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8781 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8782 && (insn & 1) == 0)
8783 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8784 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8785 /* Exclude stfqu. psq_stu as above for psq_lu. */
8786 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8787 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8788 && (insn & 1) == 0));
8789}
8790
8791/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8792 pld ra,symbol@got@pcrel
8793 load/store rt,off(ra)
8794 or
8795 pla ra,symbol@pcrel
8796 load/store rt,off(ra)
8797 may be translated to
8798 pload/pstore rt,symbol+off@pcrel
8799 nop.
8800 This function returns true if the optimization is possible, placing
8801 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8802
8803 On entry to this function, the linker has already determined that
8804 the pld can be replaced with pla: *PINSN1 is that pla insn,
8805 while *PINSN2 is the second instruction. */
8806
8807static bool
8808xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8809{
8810 uint64_t insn1 = *pinsn1;
8811 uint64_t insn2 = *pinsn2;
8812 bfd_signed_vma off;
8813
8814 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8815 {
8816 /* Check that regs match. */
8817 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8818 return false;
8819
8820 /* P8LS or PMLS form, non-pcrel. */
8821 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8822 return false;
8823
8824 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8825 *pinsn2 = PNOP;
8826 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8827 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8828 return true;
8829 }
8830
8831 insn2 >>= 32;
8832
8833 /* Check that regs match. */
8834 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8835 return false;
8836
8837 switch ((insn2 >> 26) & 63)
8838 {
8839 default:
8840 return false;
8841
8842 case 32: /* lwz */
8843 case 34: /* lbz */
8844 case 36: /* stw */
8845 case 38: /* stb */
8846 case 40: /* lhz */
8847 case 42: /* lha */
8848 case 44: /* sth */
8849 case 48: /* lfs */
8850 case 50: /* lfd */
8851 case 52: /* stfs */
8852 case 54: /* stfd */
8853 /* These are the PMLS cases, where we just need to tack a prefix
8854 on the insn. */
8855 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8856 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8857 off = insn2 & 0xffff;
8858 break;
8859
8860 case 58: /* lwa, ld */
8861 if ((insn2 & 1) != 0)
8862 return false;
8863 insn1 = ((1ULL << 58) | (1ULL << 52)
8864 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8865 | (insn2 & (31ULL << 21)));
8866 off = insn2 & 0xfffc;
8867 break;
8868
8869 case 57: /* lxsd, lxssp */
8870 if ((insn2 & 3) < 2)
8871 return false;
8872 insn1 = ((1ULL << 58) | (1ULL << 52)
8873 | ((40ULL | (insn2 & 3)) << 26)
8874 | (insn2 & (31ULL << 21)));
8875 off = insn2 & 0xfffc;
8876 break;
8877
8878 case 61: /* stxsd, stxssp, lxv, stxv */
8879 if ((insn2 & 3) == 0)
8880 return false;
8881 else if ((insn2 & 3) >= 2)
8882 {
8883 insn1 = ((1ULL << 58) | (1ULL << 52)
8884 | ((44ULL | (insn2 & 3)) << 26)
8885 | (insn2 & (31ULL << 21)));
8886 off = insn2 & 0xfffc;
8887 }
8888 else
8889 {
8890 insn1 = ((1ULL << 58) | (1ULL << 52)
8891 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8892 | (insn2 & (31ULL << 21)));
8893 off = insn2 & 0xfff0;
8894 }
8895 break;
8896
8897 case 56: /* lq */
8898 insn1 = ((1ULL << 58) | (1ULL << 52)
8899 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8900 off = insn2 & 0xffff;
8901 break;
8902
8903 case 6: /* lxvp, stxvp */
8904 if ((insn2 & 0xe) != 0)
8905 return false;
8906 insn1 = ((1ULL << 58) | (1ULL << 52)
8907 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8908 | (insn2 & (31ULL << 21)));
8909 off = insn2 & 0xfff0;
8910 break;
8911
8912 case 62: /* std, stq */
8913 if ((insn2 & 1) != 0)
8914 return false;
8915 insn1 = ((1ULL << 58) | (1ULL << 52)
8916 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8917 | (insn2 & (31ULL << 21)));
8918 off = insn2 & 0xfffc;
8919 break;
8920 }
8921
8922 *pinsn1 = insn1;
8923 *pinsn2 = (uint64_t) NOP << 32;
8924 *poff = (off ^ 0x8000) - 0x8000;
8925 return true;
8926}
8927
8928/* Examine all relocs referencing .toc sections in order to remove
8929 unused .toc entries. */
8930
8931bool
8932ppc64_elf_edit_toc (struct bfd_link_info *info)
8933{
8934 bfd *ibfd;
8935 struct adjust_toc_info toc_inf;
8936 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8937
8938 htab->do_toc_opt = 1;
8939 toc_inf.global_toc_syms = true;
8940 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8941 {
8942 asection *toc, *sec;
8943 Elf_Internal_Shdr *symtab_hdr;
8944 Elf_Internal_Sym *local_syms;
8945 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8946 unsigned long *skip, *drop;
8947 unsigned char *used;
8948 unsigned char *keep, last, some_unused;
8949
8950 if (!is_ppc64_elf (ibfd))
8951 continue;
8952
8953 toc = bfd_get_section_by_name (ibfd, ".toc");
8954 if (toc == NULL
8955 || toc->size == 0
8956 || (toc->flags & SEC_HAS_CONTENTS) == 0
8957 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8958 || discarded_section (toc))
8959 continue;
8960
8961 toc_relocs = NULL;
8962 local_syms = NULL;
8963 symtab_hdr = &elf_symtab_hdr (ibfd);
8964
8965 /* Look at sections dropped from the final link. */
8966 skip = NULL;
8967 relstart = NULL;
8968 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8969 {
8970 if (sec->reloc_count == 0
8971 || !discarded_section (sec)
8972 || get_opd_info (sec)
8973 || (sec->flags & SEC_ALLOC) == 0
8974 || (sec->flags & SEC_DEBUGGING) != 0)
8975 continue;
8976
8977 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8978 if (relstart == NULL)
8979 goto error_ret;
8980
8981 /* Run through the relocs to see which toc entries might be
8982 unused. */
8983 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8984 {
8985 enum elf_ppc64_reloc_type r_type;
8986 unsigned long r_symndx;
8987 asection *sym_sec;
8988 struct elf_link_hash_entry *h;
8989 Elf_Internal_Sym *sym;
8990 bfd_vma val;
8991
8992 r_type = ELF64_R_TYPE (rel->r_info);
8993 switch (r_type)
8994 {
8995 default:
8996 continue;
8997
8998 case R_PPC64_TOC16:
8999 case R_PPC64_TOC16_LO:
9000 case R_PPC64_TOC16_HI:
9001 case R_PPC64_TOC16_HA:
9002 case R_PPC64_TOC16_DS:
9003 case R_PPC64_TOC16_LO_DS:
9004 break;
9005 }
9006
9007 r_symndx = ELF64_R_SYM (rel->r_info);
9008 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9009 r_symndx, ibfd))
9010 goto error_ret;
9011
9012 if (sym_sec != toc)
9013 continue;
9014
9015 if (h != NULL)
9016 val = h->root.u.def.value;
9017 else
9018 val = sym->st_value;
9019 val += rel->r_addend;
9020
9021 if (val >= toc->size)
9022 continue;
9023
9024 /* Anything in the toc ought to be aligned to 8 bytes.
9025 If not, don't mark as unused. */
9026 if (val & 7)
9027 continue;
9028
9029 if (skip == NULL)
9030 {
9031 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9032 if (skip == NULL)
9033 goto error_ret;
9034 }
9035
9036 skip[val >> 3] = ref_from_discarded;
9037 }
9038
9039 if (elf_section_data (sec)->relocs != relstart)
9040 free (relstart);
9041 }
9042
9043 /* For largetoc loads of address constants, we can convert
9044 . addis rx,2,addr@got@ha
9045 . ld ry,addr@got@l(rx)
9046 to
9047 . addis rx,2,addr@toc@ha
9048 . addi ry,rx,addr@toc@l
9049 when addr is within 2G of the toc pointer. This then means
9050 that the word storing "addr" in the toc is no longer needed. */
9051
9052 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9053 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9054 && toc->reloc_count != 0)
9055 {
9056 /* Read toc relocs. */
9057 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9058 info->keep_memory);
9059 if (toc_relocs == NULL)
9060 goto error_ret;
9061
9062 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9063 {
9064 enum elf_ppc64_reloc_type r_type;
9065 unsigned long r_symndx;
9066 asection *sym_sec;
9067 struct elf_link_hash_entry *h;
9068 Elf_Internal_Sym *sym;
9069 bfd_vma val, addr;
9070
9071 r_type = ELF64_R_TYPE (rel->r_info);
9072 if (r_type != R_PPC64_ADDR64)
9073 continue;
9074
9075 r_symndx = ELF64_R_SYM (rel->r_info);
9076 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9077 r_symndx, ibfd))
9078 goto error_ret;
9079
9080 if (sym_sec == NULL
9081 || sym_sec->output_section == NULL
9082 || discarded_section (sym_sec))
9083 continue;
9084
9085 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9086 || (bfd_link_pic (info)
9087 && sym_sec == bfd_abs_section_ptr))
9088 continue;
9089
9090 if (h != NULL)
9091 {
9092 if (h->type == STT_GNU_IFUNC)
9093 continue;
9094 val = h->root.u.def.value;
9095 }
9096 else
9097 {
9098 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9099 continue;
9100 val = sym->st_value;
9101 }
9102 val += rel->r_addend;
9103 val += sym_sec->output_section->vma + sym_sec->output_offset;
9104
9105 /* We don't yet know the exact toc pointer value, but we
9106 know it will be somewhere in the toc section. Don't
9107 optimize if the difference from any possible toc
9108 pointer is outside [ff..f80008000, 7fff7fff]. */
9109 addr = toc->output_section->vma + TOC_BASE_OFF;
9110 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9111 continue;
9112
9113 addr = toc->output_section->vma + toc->output_section->rawsize;
9114 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9115 continue;
9116
9117 if (skip == NULL)
9118 {
9119 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9120 if (skip == NULL)
9121 goto error_ret;
9122 }
9123
9124 skip[rel->r_offset >> 3]
9125 |= can_optimize | ((rel - toc_relocs) << 2);
9126 }
9127 }
9128
9129 if (skip == NULL)
9130 continue;
9131
9132 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9133 if (used == NULL)
9134 {
9135 error_ret:
9136 if (symtab_hdr->contents != (unsigned char *) local_syms)
9137 free (local_syms);
9138 if (sec != NULL
9139 && elf_section_data (sec)->relocs != relstart)
9140 free (relstart);
9141 if (elf_section_data (toc)->relocs != toc_relocs)
9142 free (toc_relocs);
9143 free (skip);
9144 return false;
9145 }
9146
9147 /* Now check all kept sections that might reference the toc.
9148 Check the toc itself last. */
9149 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9150 : ibfd->sections);
9151 sec != NULL;
9152 sec = (sec == toc ? NULL
9153 : sec->next == NULL ? toc
9154 : sec->next == toc && toc->next ? toc->next
9155 : sec->next))
9156 {
9157 int repeat;
9158
9159 if (sec->reloc_count == 0
9160 || discarded_section (sec)
9161 || get_opd_info (sec)
9162 || (sec->flags & SEC_ALLOC) == 0
9163 || (sec->flags & SEC_DEBUGGING) != 0)
9164 continue;
9165
9166 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9167 info->keep_memory);
9168 if (relstart == NULL)
9169 {
9170 free (used);
9171 goto error_ret;
9172 }
9173
9174 /* Mark toc entries referenced as used. */
9175 do
9176 {
9177 repeat = 0;
9178 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9179 {
9180 enum elf_ppc64_reloc_type r_type;
9181 unsigned long r_symndx;
9182 asection *sym_sec;
9183 struct elf_link_hash_entry *h;
9184 Elf_Internal_Sym *sym;
9185 bfd_vma val;
9186
9187 r_type = ELF64_R_TYPE (rel->r_info);
9188 switch (r_type)
9189 {
9190 case R_PPC64_TOC16:
9191 case R_PPC64_TOC16_LO:
9192 case R_PPC64_TOC16_HI:
9193 case R_PPC64_TOC16_HA:
9194 case R_PPC64_TOC16_DS:
9195 case R_PPC64_TOC16_LO_DS:
9196 /* In case we're taking addresses of toc entries. */
9197 case R_PPC64_ADDR64:
9198 break;
9199
9200 default:
9201 continue;
9202 }
9203
9204 r_symndx = ELF64_R_SYM (rel->r_info);
9205 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9206 r_symndx, ibfd))
9207 {
9208 free (used);
9209 goto error_ret;
9210 }
9211
9212 if (sym_sec != toc)
9213 continue;
9214
9215 if (h != NULL)
9216 val = h->root.u.def.value;
9217 else
9218 val = sym->st_value;
9219 val += rel->r_addend;
9220
9221 if (val >= toc->size)
9222 continue;
9223
9224 if ((skip[val >> 3] & can_optimize) != 0)
9225 {
9226 bfd_vma off;
9227 unsigned char opc;
9228
9229 switch (r_type)
9230 {
9231 case R_PPC64_TOC16_HA:
9232 break;
9233
9234 case R_PPC64_TOC16_LO_DS:
9235 off = rel->r_offset;
9236 off += (bfd_big_endian (ibfd) ? -2 : 3);
9237 if (!bfd_get_section_contents (ibfd, sec, &opc,
9238 off, 1))
9239 {
9240 free (used);
9241 goto error_ret;
9242 }
9243 if ((opc & (0x3f << 2)) == (58u << 2))
9244 break;
9245 /* Fall through. */
9246
9247 default:
9248 /* Wrong sort of reloc, or not a ld. We may
9249 as well clear ref_from_discarded too. */
9250 skip[val >> 3] = 0;
9251 }
9252 }
9253
9254 if (sec != toc)
9255 used[val >> 3] = 1;
9256 /* For the toc section, we only mark as used if this
9257 entry itself isn't unused. */
9258 else if ((used[rel->r_offset >> 3]
9259 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9260 && !used[val >> 3])
9261 {
9262 /* Do all the relocs again, to catch reference
9263 chains. */
9264 repeat = 1;
9265 used[val >> 3] = 1;
9266 }
9267 }
9268 }
9269 while (repeat);
9270
9271 if (elf_section_data (sec)->relocs != relstart)
9272 free (relstart);
9273 }
9274
9275 /* Merge the used and skip arrays. Assume that TOC
9276 doublewords not appearing as either used or unused belong
9277 to an entry more than one doubleword in size. */
9278 for (drop = skip, keep = used, last = 0, some_unused = 0;
9279 drop < skip + (toc->size + 7) / 8;
9280 ++drop, ++keep)
9281 {
9282 if (*keep)
9283 {
9284 *drop &= ~ref_from_discarded;
9285 if ((*drop & can_optimize) != 0)
9286 some_unused = 1;
9287 last = 0;
9288 }
9289 else if ((*drop & ref_from_discarded) != 0)
9290 {
9291 some_unused = 1;
9292 last = ref_from_discarded;
9293 }
9294 else
9295 *drop = last;
9296 }
9297
9298 free (used);
9299
9300 if (some_unused)
9301 {
9302 bfd_byte *contents, *src;
9303 unsigned long off;
9304 Elf_Internal_Sym *sym;
9305 bool local_toc_syms = false;
9306
9307 /* Shuffle the toc contents, and at the same time convert the
9308 skip array from booleans into offsets. */
9309 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9310 goto error_ret;
9311
9312 elf_section_data (toc)->this_hdr.contents = contents;
9313
9314 for (src = contents, off = 0, drop = skip;
9315 src < contents + toc->size;
9316 src += 8, ++drop)
9317 {
9318 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9319 off += 8;
9320 else if (off != 0)
9321 {
9322 *drop = off;
9323 memcpy (src - off, src, 8);
9324 }
9325 }
9326 *drop = off;
9327 toc->rawsize = toc->size;
9328 toc->size = src - contents - off;
9329
9330 /* Adjust addends for relocs against the toc section sym,
9331 and optimize any accesses we can. */
9332 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9333 {
9334 if (sec->reloc_count == 0
9335 || discarded_section (sec))
9336 continue;
9337
9338 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9339 info->keep_memory);
9340 if (relstart == NULL)
9341 goto error_ret;
9342
9343 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9344 {
9345 enum elf_ppc64_reloc_type r_type;
9346 unsigned long r_symndx;
9347 asection *sym_sec;
9348 struct elf_link_hash_entry *h;
9349 bfd_vma val;
9350
9351 r_type = ELF64_R_TYPE (rel->r_info);
9352 switch (r_type)
9353 {
9354 default:
9355 continue;
9356
9357 case R_PPC64_TOC16:
9358 case R_PPC64_TOC16_LO:
9359 case R_PPC64_TOC16_HI:
9360 case R_PPC64_TOC16_HA:
9361 case R_PPC64_TOC16_DS:
9362 case R_PPC64_TOC16_LO_DS:
9363 case R_PPC64_ADDR64:
9364 break;
9365 }
9366
9367 r_symndx = ELF64_R_SYM (rel->r_info);
9368 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9369 r_symndx, ibfd))
9370 goto error_ret;
9371
9372 if (sym_sec != toc)
9373 continue;
9374
9375 if (h != NULL)
9376 val = h->root.u.def.value;
9377 else
9378 {
9379 val = sym->st_value;
9380 if (val != 0)
9381 local_toc_syms = true;
9382 }
9383
9384 val += rel->r_addend;
9385
9386 if (val > toc->rawsize)
9387 val = toc->rawsize;
9388 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9389 continue;
9390 else if ((skip[val >> 3] & can_optimize) != 0)
9391 {
9392 Elf_Internal_Rela *tocrel
9393 = toc_relocs + (skip[val >> 3] >> 2);
9394 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9395
9396 switch (r_type)
9397 {
9398 case R_PPC64_TOC16_HA:
9399 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9400 break;
9401
9402 case R_PPC64_TOC16_LO_DS:
9403 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9404 break;
9405
9406 default:
9407 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9408 ppc_howto_init ();
9409 info->callbacks->einfo
9410 /* xgettext:c-format */
9411 (_("%H: %s references "
9412 "optimized away TOC entry\n"),
9413 ibfd, sec, rel->r_offset,
9414 ppc64_elf_howto_table[r_type]->name);
9415 bfd_set_error (bfd_error_bad_value);
9416 goto error_ret;
9417 }
9418 rel->r_addend = tocrel->r_addend;
9419 elf_section_data (sec)->relocs = relstart;
9420 continue;
9421 }
9422
9423 if (h != NULL || sym->st_value != 0)
9424 continue;
9425
9426 rel->r_addend -= skip[val >> 3];
9427 elf_section_data (sec)->relocs = relstart;
9428 }
9429
9430 if (elf_section_data (sec)->relocs != relstart)
9431 free (relstart);
9432 }
9433
9434 /* We shouldn't have local or global symbols defined in the TOC,
9435 but handle them anyway. */
9436 if (local_syms != NULL)
9437 for (sym = local_syms;
9438 sym < local_syms + symtab_hdr->sh_info;
9439 ++sym)
9440 if (sym->st_value != 0
9441 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9442 {
9443 unsigned long i;
9444
9445 if (sym->st_value > toc->rawsize)
9446 i = toc->rawsize >> 3;
9447 else
9448 i = sym->st_value >> 3;
9449
9450 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9451 {
9452 if (local_toc_syms)
9453 _bfd_error_handler
9454 (_("%s defined on removed toc entry"),
9455 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9456 do
9457 ++i;
9458 while ((skip[i] & (ref_from_discarded | can_optimize)));
9459 sym->st_value = (bfd_vma) i << 3;
9460 }
9461
9462 sym->st_value -= skip[i];
9463 symtab_hdr->contents = (unsigned char *) local_syms;
9464 }
9465
9466 /* Adjust any global syms defined in this toc input section. */
9467 if (toc_inf.global_toc_syms)
9468 {
9469 toc_inf.toc = toc;
9470 toc_inf.skip = skip;
9471 toc_inf.global_toc_syms = false;
9472 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9473 &toc_inf);
9474 }
9475
9476 if (toc->reloc_count != 0)
9477 {
9478 Elf_Internal_Shdr *rel_hdr;
9479 Elf_Internal_Rela *wrel;
9480 bfd_size_type sz;
9481
9482 /* Remove unused toc relocs, and adjust those we keep. */
9483 if (toc_relocs == NULL)
9484 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9485 info->keep_memory);
9486 if (toc_relocs == NULL)
9487 goto error_ret;
9488
9489 wrel = toc_relocs;
9490 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9491 if ((skip[rel->r_offset >> 3]
9492 & (ref_from_discarded | can_optimize)) == 0)
9493 {
9494 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9495 wrel->r_info = rel->r_info;
9496 wrel->r_addend = rel->r_addend;
9497 ++wrel;
9498 }
9499 else if (!dec_dynrel_count (rel, toc, info,
9500 &local_syms, NULL, NULL))
9501 goto error_ret;
9502
9503 elf_section_data (toc)->relocs = toc_relocs;
9504 toc->reloc_count = wrel - toc_relocs;
9505 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9506 sz = rel_hdr->sh_entsize;
9507 rel_hdr->sh_size = toc->reloc_count * sz;
9508 }
9509 }
9510 else if (elf_section_data (toc)->relocs != toc_relocs)
9511 free (toc_relocs);
9512
9513 if (local_syms != NULL
9514 && symtab_hdr->contents != (unsigned char *) local_syms)
9515 {
9516 if (!info->keep_memory)
9517 free (local_syms);
9518 else
9519 symtab_hdr->contents = (unsigned char *) local_syms;
9520 }
9521 free (skip);
9522 }
9523
9524 /* Look for cases where we can change an indirect GOT access to
9525 a GOT relative or PC relative access, possibly reducing the
9526 number of GOT entries. */
9527 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9528 {
9529 asection *sec;
9530 Elf_Internal_Shdr *symtab_hdr;
9531 Elf_Internal_Sym *local_syms;
9532 Elf_Internal_Rela *relstart, *rel;
9533 bfd_vma got;
9534
9535 if (!is_ppc64_elf (ibfd))
9536 continue;
9537
9538 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9539 continue;
9540
9541 sec = ppc64_elf_tdata (ibfd)->got;
9542 got = 0;
9543 if (sec != NULL)
9544 got = sec->output_section->vma + sec->output_offset + 0x8000;
9545
9546 local_syms = NULL;
9547 symtab_hdr = &elf_symtab_hdr (ibfd);
9548
9549 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9550 {
9551 if (sec->reloc_count == 0
9552 || !ppc64_elf_section_data (sec)->has_optrel
9553 || discarded_section (sec))
9554 continue;
9555
9556 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9557 info->keep_memory);
9558 if (relstart == NULL)
9559 {
9560 got_error_ret:
9561 if (symtab_hdr->contents != (unsigned char *) local_syms)
9562 free (local_syms);
9563 if (sec != NULL
9564 && elf_section_data (sec)->relocs != relstart)
9565 free (relstart);
9566 return false;
9567 }
9568
9569 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9570 {
9571 enum elf_ppc64_reloc_type r_type;
9572 unsigned long r_symndx;
9573 Elf_Internal_Sym *sym;
9574 asection *sym_sec;
9575 struct elf_link_hash_entry *h;
9576 struct got_entry *ent;
9577 bfd_vma val, pc;
9578 unsigned char buf[8];
9579 unsigned int insn;
9580 enum {no_check, check_lo, check_ha} insn_check;
9581
9582 r_type = ELF64_R_TYPE (rel->r_info);
9583 switch (r_type)
9584 {
9585 default:
9586 insn_check = no_check;
9587 break;
9588
9589 case R_PPC64_PLT16_HA:
9590 case R_PPC64_GOT_TLSLD16_HA:
9591 case R_PPC64_GOT_TLSGD16_HA:
9592 case R_PPC64_GOT_TPREL16_HA:
9593 case R_PPC64_GOT_DTPREL16_HA:
9594 case R_PPC64_GOT16_HA:
9595 case R_PPC64_TOC16_HA:
9596 insn_check = check_ha;
9597 break;
9598
9599 case R_PPC64_PLT16_LO:
9600 case R_PPC64_PLT16_LO_DS:
9601 case R_PPC64_GOT_TLSLD16_LO:
9602 case R_PPC64_GOT_TLSGD16_LO:
9603 case R_PPC64_GOT_TPREL16_LO_DS:
9604 case R_PPC64_GOT_DTPREL16_LO_DS:
9605 case R_PPC64_GOT16_LO:
9606 case R_PPC64_GOT16_LO_DS:
9607 case R_PPC64_TOC16_LO:
9608 case R_PPC64_TOC16_LO_DS:
9609 insn_check = check_lo;
9610 break;
9611 }
9612
9613 if (insn_check != no_check)
9614 {
9615 bfd_vma off = rel->r_offset & ~3;
9616
9617 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9618 goto got_error_ret;
9619
9620 insn = bfd_get_32 (ibfd, buf);
9621 if (insn_check == check_lo
9622 ? !ok_lo_toc_insn (insn, r_type)
9623 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9624 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9625 {
9626 char str[12];
9627
9628 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9629 sprintf (str, "%#08x", insn);
9630 info->callbacks->einfo
9631 /* xgettext:c-format */
9632 (_("%H: got/toc optimization is not supported for"
9633 " %s instruction\n"),
9634 ibfd, sec, rel->r_offset & ~3, str);
9635 continue;
9636 }
9637 }
9638
9639 switch (r_type)
9640 {
9641 /* Note that we don't delete GOT entries for
9642 R_PPC64_GOT16_DS since we'd need a lot more
9643 analysis. For starters, the preliminary layout is
9644 before the GOT, PLT, dynamic sections and stubs are
9645 laid out. Then we'd need to allow for changes in
9646 distance between sections caused by alignment. */
9647 default:
9648 continue;
9649
9650 case R_PPC64_GOT16_HA:
9651 case R_PPC64_GOT16_LO_DS:
9652 case R_PPC64_GOT_PCREL34:
9653 break;
9654 }
9655
9656 r_symndx = ELF64_R_SYM (rel->r_info);
9657 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9658 r_symndx, ibfd))
9659 goto got_error_ret;
9660
9661 if (sym_sec == NULL
9662 || sym_sec->output_section == NULL
9663 || discarded_section (sym_sec))
9664 continue;
9665
9666 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9667 continue;
9668
9669 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9670 || (bfd_link_pic (info)
9671 && sym_sec == bfd_abs_section_ptr))
9672 continue;
9673
9674 if (h != NULL)
9675 val = h->root.u.def.value;
9676 else
9677 val = sym->st_value;
9678 val += rel->r_addend;
9679 val += sym_sec->output_section->vma + sym_sec->output_offset;
9680
9681/* Fudge factor to allow for the fact that the preliminary layout
9682 isn't exact. Reduce limits by this factor. */
9683#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9684
9685 switch (r_type)
9686 {
9687 default:
9688 continue;
9689
9690 case R_PPC64_GOT16_HA:
9691 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9692 >= LIMIT_ADJUST (0x100000000ULL))
9693 continue;
9694
9695 if (!bfd_get_section_contents (ibfd, sec, buf,
9696 rel->r_offset & ~3, 4))
9697 goto got_error_ret;
9698 insn = bfd_get_32 (ibfd, buf);
9699 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9700 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9701 continue;
9702 break;
9703
9704 case R_PPC64_GOT16_LO_DS:
9705 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9706 >= LIMIT_ADJUST (0x100000000ULL))
9707 continue;
9708 if (!bfd_get_section_contents (ibfd, sec, buf,
9709 rel->r_offset & ~3, 4))
9710 goto got_error_ret;
9711 insn = bfd_get_32 (ibfd, buf);
9712 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9713 continue;
9714 break;
9715
9716 case R_PPC64_GOT_PCREL34:
9717 pc = rel->r_offset;
9718 pc += sec->output_section->vma + sec->output_offset;
9719 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9720 >= LIMIT_ADJUST (1ULL << 34))
9721 continue;
9722 if (!bfd_get_section_contents (ibfd, sec, buf,
9723 rel->r_offset & ~3, 8))
9724 goto got_error_ret;
9725 insn = bfd_get_32 (ibfd, buf);
9726 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9727 continue;
9728 insn = bfd_get_32 (ibfd, buf + 4);
9729 if ((insn & (0x3fu << 26)) != 57u << 26)
9730 continue;
9731 break;
9732 }
9733#undef LIMIT_ADJUST
9734
9735 if (h != NULL)
9736 ent = h->got.glist;
9737 else
9738 {
9739 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9740 ent = local_got_ents[r_symndx];
9741 }
9742 for (; ent != NULL; ent = ent->next)
9743 if (ent->addend == rel->r_addend
9744 && ent->owner == ibfd
9745 && ent->tls_type == 0)
9746 break;
9747 BFD_ASSERT (ent && ent->got.refcount > 0);
9748 ent->got.refcount -= 1;
9749 }
9750
9751 if (elf_section_data (sec)->relocs != relstart)
9752 free (relstart);
9753 }
9754
9755 if (local_syms != NULL
9756 && symtab_hdr->contents != (unsigned char *) local_syms)
9757 {
9758 if (!info->keep_memory)
9759 free (local_syms);
9760 else
9761 symtab_hdr->contents = (unsigned char *) local_syms;
9762 }
9763 }
9764
9765 return true;
9766}
9767
9768/* Return true iff input section I references the TOC using
9769 instructions limited to +/-32k offsets. */
9770
9771bool
9772ppc64_elf_has_small_toc_reloc (asection *i)
9773{
9774 return (is_ppc64_elf (i->owner)
9775 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9776}
9777
9778/* Allocate space for one GOT entry. */
9779
9780static void
9781allocate_got (struct elf_link_hash_entry *h,
9782 struct bfd_link_info *info,
9783 struct got_entry *gent)
9784{
9785 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9786 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9787 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9788 ? 16 : 8);
9789 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9790 ? 2 : 1) * sizeof (Elf64_External_Rela);
9791 asection *got = ppc64_elf_tdata (gent->owner)->got;
9792
9793 gent->got.offset = got->size;
9794 got->size += entsize;
9795
9796 if (h->type == STT_GNU_IFUNC)
9797 {
9798 htab->elf.irelplt->size += rentsize;
9799 htab->got_reli_size += rentsize;
9800 }
9801 else if (((bfd_link_pic (info)
9802 && (gent->tls_type == 0
9803 ? !info->enable_dt_relr
9804 : !(bfd_link_executable (info)
9805 && SYMBOL_REFERENCES_LOCAL (info, h)))
9806 && !bfd_is_abs_symbol (&h->root))
9807 || (htab->elf.dynamic_sections_created
9808 && h->dynindx != -1
9809 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9810 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9811 {
9812 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9813 relgot->size += rentsize;
9814 }
9815}
9816
9817/* This function merges got entries in the same toc group. */
9818
9819static void
9820merge_got_entries (struct got_entry **pent)
9821{
9822 struct got_entry *ent, *ent2;
9823
9824 for (ent = *pent; ent != NULL; ent = ent->next)
9825 if (!ent->is_indirect)
9826 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9827 if (!ent2->is_indirect
9828 && ent2->addend == ent->addend
9829 && ent2->tls_type == ent->tls_type
9830 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9831 {
9832 ent2->is_indirect = true;
9833 ent2->got.ent = ent;
9834 }
9835}
9836
9837/* If H is undefined, make it dynamic if that makes sense. */
9838
9839static bool
9840ensure_undef_dynamic (struct bfd_link_info *info,
9841 struct elf_link_hash_entry *h)
9842{
9843 struct elf_link_hash_table *htab = elf_hash_table (info);
9844
9845 if (htab->dynamic_sections_created
9846 && ((info->dynamic_undefined_weak != 0
9847 && h->root.type == bfd_link_hash_undefweak)
9848 || h->root.type == bfd_link_hash_undefined)
9849 && h->dynindx == -1
9850 && !h->forced_local
9851 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9852 return bfd_elf_link_record_dynamic_symbol (info, h);
9853 return true;
9854}
9855
9856/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9857 usual htab->elf.splt section for a PLT entry. */
9858
9859static inline
9860bool use_local_plt (struct bfd_link_info *info,
9861 struct elf_link_hash_entry *h)
9862{
9863 return (h == NULL
9864 || h->dynindx == -1
9865 || !elf_hash_table (info)->dynamic_sections_created);
9866}
9867
9868/* Allocate space in .plt, .got and associated reloc sections for
9869 dynamic relocs. */
9870
9871static bool
9872allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9873{
9874 struct bfd_link_info *info;
9875 struct ppc_link_hash_table *htab;
9876 asection *s;
9877 struct ppc_link_hash_entry *eh;
9878 struct got_entry **pgent, *gent;
9879
9880 if (h->root.type == bfd_link_hash_indirect)
9881 return true;
9882
9883 info = (struct bfd_link_info *) inf;
9884 htab = ppc_hash_table (info);
9885 if (htab == NULL)
9886 return false;
9887
9888 eh = ppc_elf_hash_entry (h);
9889 /* Run through the TLS GD got entries first if we're changing them
9890 to TPREL. */
9891 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9892 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9893 if (gent->got.refcount > 0
9894 && (gent->tls_type & TLS_GD) != 0)
9895 {
9896 /* This was a GD entry that has been converted to TPREL. If
9897 there happens to be a TPREL entry we can use that one. */
9898 struct got_entry *ent;
9899 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9900 if (ent->got.refcount > 0
9901 && (ent->tls_type & TLS_TPREL) != 0
9902 && ent->addend == gent->addend
9903 && ent->owner == gent->owner)
9904 {
9905 gent->got.refcount = 0;
9906 break;
9907 }
9908
9909 /* If not, then we'll be using our own TPREL entry. */
9910 if (gent->got.refcount != 0)
9911 gent->tls_type = TLS_TLS | TLS_TPREL;
9912 }
9913
9914 /* Remove any list entry that won't generate a word in the GOT before
9915 we call merge_got_entries. Otherwise we risk merging to empty
9916 entries. */
9917 pgent = &h->got.glist;
9918 while ((gent = *pgent) != NULL)
9919 if (gent->got.refcount > 0)
9920 {
9921 if ((gent->tls_type & TLS_LD) != 0
9922 && SYMBOL_REFERENCES_LOCAL (info, h))
9923 {
9924 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9925 *pgent = gent->next;
9926 }
9927 else
9928 pgent = &gent->next;
9929 }
9930 else
9931 *pgent = gent->next;
9932
9933 if (!htab->do_multi_toc)
9934 merge_got_entries (&h->got.glist);
9935
9936 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9937 if (!gent->is_indirect)
9938 {
9939 /* Ensure we catch all the cases where this symbol should
9940 be made dynamic. */
9941 if (!ensure_undef_dynamic (info, h))
9942 return false;
9943
9944 if (!is_ppc64_elf (gent->owner))
9945 abort ();
9946
9947 allocate_got (h, info, gent);
9948 }
9949
9950 /* If no dynamic sections we can't have dynamic relocs, except for
9951 IFUNCs which are handled even in static executables. */
9952 if (!htab->elf.dynamic_sections_created
9953 && h->type != STT_GNU_IFUNC)
9954 h->dyn_relocs = NULL;
9955
9956 /* Discard relocs on undefined symbols that must be local. */
9957 else if (h->root.type == bfd_link_hash_undefined
9958 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9959 h->dyn_relocs = NULL;
9960
9961 /* Also discard relocs on undefined weak syms with non-default
9962 visibility, or when dynamic_undefined_weak says so. */
9963 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9964 h->dyn_relocs = NULL;
9965
9966 if (h->dyn_relocs != NULL)
9967 {
9968 struct ppc_dyn_relocs *p, **pp;
9969
9970 /* In the shared -Bsymbolic case, discard space allocated for
9971 dynamic pc-relative relocs against symbols which turn out to
9972 be defined in regular objects. For the normal shared case,
9973 discard space for relocs that have become local due to symbol
9974 visibility changes. */
9975 if (bfd_link_pic (info))
9976 {
9977 /* Relocs that use pc_count are those that appear on a call
9978 insn, or certain REL relocs (see must_be_dyn_reloc) that
9979 can be generated via assembly. We want calls to
9980 protected symbols to resolve directly to the function
9981 rather than going via the plt. If people want function
9982 pointer comparisons to work as expected then they should
9983 avoid writing weird assembly. */
9984 if (SYMBOL_CALLS_LOCAL (info, h))
9985 {
9986 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9987 (p = *pp) != NULL;
9988 )
9989 {
9990 p->count -= p->pc_count;
9991 p->pc_count = 0;
9992 if (p->count == 0)
9993 *pp = p->next;
9994 else
9995 pp = &p->next;
9996 }
9997 }
9998
9999 if (h->dyn_relocs != NULL)
10000 {
10001 /* Ensure we catch all the cases where this symbol
10002 should be made dynamic. */
10003 if (!ensure_undef_dynamic (info, h))
10004 return false;
10005 }
10006 }
10007
10008 /* For a fixed position executable, discard space for
10009 relocs against symbols which are not dynamic. */
10010 else if (h->type != STT_GNU_IFUNC)
10011 {
10012 if ((h->dynamic_adjusted
10013 || (h->ref_regular
10014 && h->root.type == bfd_link_hash_undefweak
10015 && (info->dynamic_undefined_weak > 0
10016 || !_bfd_elf_readonly_dynrelocs (h))))
10017 && !h->def_regular
10018 && !ELF_COMMON_DEF_P (h))
10019 {
10020 /* Ensure we catch all the cases where this symbol
10021 should be made dynamic. */
10022 if (!ensure_undef_dynamic (info, h))
10023 return false;
10024
10025 /* But if that didn't work out, discard dynamic relocs. */
10026 if (h->dynindx == -1)
10027 h->dyn_relocs = NULL;
10028 }
10029 else
10030 h->dyn_relocs = NULL;
10031 }
10032
10033 /* Finally, allocate space. */
10034 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10035 if (!discarded_section (p->sec))
10036 {
10037 unsigned int count;
10038 asection *sreloc = elf_section_data (p->sec)->sreloc;
10039 if (eh->elf.type == STT_GNU_IFUNC)
10040 sreloc = htab->elf.irelplt;
10041 count = p->count;
10042 if (info->enable_dt_relr
10043 && ((!NO_OPD_RELOCS
10044 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10045 || (eh->elf.type != STT_GNU_IFUNC
10046 && SYMBOL_REFERENCES_LOCAL (info, h))))
10047 count -= p->rel_count;
10048 sreloc->size += count * sizeof (Elf64_External_Rela);
10049 }
10050 }
10051
10052 /* We might need a PLT entry when the symbol
10053 a) is dynamic, or
10054 b) is an ifunc, or
10055 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10056 d) has plt16 relocs and we are linking statically. */
10057 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10058 || h->type == STT_GNU_IFUNC
10059 || (h->needs_plt && h->dynamic_adjusted)
10060 || (h->needs_plt
10061 && h->def_regular
10062 && !htab->elf.dynamic_sections_created
10063 && !htab->can_convert_all_inline_plt
10064 && (ppc_elf_hash_entry (h)->tls_mask
10065 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10066 {
10067 struct plt_entry *pent;
10068 bool doneone = false;
10069 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10070 if (pent->plt.refcount > 0)
10071 {
10072 if (!ensure_undef_dynamic (info, h))
10073 return false;
10074
10075 if (use_local_plt (info, h))
10076 {
10077 if (h->type == STT_GNU_IFUNC)
10078 {
10079 s = htab->elf.iplt;
10080 pent->plt.offset = s->size;
10081 s->size += PLT_ENTRY_SIZE (htab);
10082 s = htab->elf.irelplt;
10083 }
10084 else
10085 {
10086 s = htab->pltlocal;
10087 pent->plt.offset = s->size;
10088 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10089 s = NULL;
10090 if (bfd_link_pic (info)
10091 && !(info->enable_dt_relr && !htab->opd_abi))
10092 s = htab->relpltlocal;
10093 }
10094 }
10095 else
10096 {
10097 /* If this is the first .plt entry, make room for the special
10098 first entry. */
10099 s = htab->elf.splt;
10100 if (s->size == 0)
10101 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10102
10103 pent->plt.offset = s->size;
10104
10105 /* Make room for this entry. */
10106 s->size += PLT_ENTRY_SIZE (htab);
10107
10108 /* Make room for the .glink code. */
10109 s = htab->glink;
10110 if (s->size == 0)
10111 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10112 if (htab->opd_abi)
10113 {
10114 /* We need bigger stubs past index 32767. */
10115 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10116 s->size += 4;
10117 s->size += 2*4;
10118 }
10119 else
10120 s->size += 4;
10121
10122 /* We also need to make an entry in the .rela.plt section. */
10123 s = htab->elf.srelplt;
10124 }
10125 if (s != NULL)
10126 s->size += sizeof (Elf64_External_Rela);
10127 doneone = true;
10128 }
10129 else
10130 pent->plt.offset = (bfd_vma) -1;
10131 if (!doneone)
10132 {
10133 h->plt.plist = NULL;
10134 h->needs_plt = 0;
10135 }
10136 }
10137 else
10138 {
10139 h->plt.plist = NULL;
10140 h->needs_plt = 0;
10141 }
10142
10143 return true;
10144}
10145
10146#define PPC_LO(v) ((v) & 0xffff)
10147#define PPC_HI(v) (((v) >> 16) & 0xffff)
10148#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10149#define D34(v) \
10150 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10151#define HA34(v) ((v + (1ULL << 33)) >> 34)
10152
10153/* Called via elf_link_hash_traverse from ppc64_elf_late_size_sections
10154 to set up space for global entry stubs. These are put in glink,
10155 after the branch table. */
10156
10157static bool
10158size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10159{
10160 struct bfd_link_info *info;
10161 struct ppc_link_hash_table *htab;
10162 struct plt_entry *pent;
10163 asection *s, *plt;
10164
10165 if (h->root.type == bfd_link_hash_indirect)
10166 return true;
10167
10168 if (!h->pointer_equality_needed)
10169 return true;
10170
10171 if (h->def_regular)
10172 return true;
10173
10174 info = inf;
10175 htab = ppc_hash_table (info);
10176 if (htab == NULL)
10177 return false;
10178
10179 s = htab->global_entry;
10180 plt = htab->elf.splt;
10181 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10182 if (pent->plt.offset != (bfd_vma) -1
10183 && pent->addend == 0)
10184 {
10185 /* For ELFv2, if this symbol is not defined in a regular file
10186 and we are not generating a shared library or pie, then we
10187 need to define the symbol in the executable on a call stub.
10188 This is to avoid text relocations. */
10189 bfd_vma off, stub_align, stub_off, stub_size;
10190 unsigned int align_power;
10191
10192 stub_size = 16;
10193 stub_off = s->size;
10194 if (htab->params->plt_stub_align >= 0)
10195 align_power = htab->params->plt_stub_align;
10196 else
10197 align_power = -htab->params->plt_stub_align;
10198 /* Setting section alignment is delayed until we know it is
10199 non-empty. Otherwise the .text output section will be
10200 aligned at least to plt_stub_align even when no global
10201 entry stubs are needed. */
10202 if (!bfd_link_align_section (s, align_power))
10203 return false;
10204 stub_align = (bfd_vma) 1 << align_power;
10205 if (htab->params->plt_stub_align >= 0
10206 || ((((stub_off + stub_size - 1) & -stub_align)
10207 - (stub_off & -stub_align))
10208 > ((stub_size - 1) & -stub_align)))
10209 stub_off = (stub_off + stub_align - 1) & -stub_align;
10210 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10211 off -= stub_off + s->output_offset + s->output_section->vma;
10212 /* Note that for --plt-stub-align negative we have a possible
10213 dependency between stub offset and size. Break that
10214 dependency by assuming the max stub size when calculating
10215 the stub offset. */
10216 if (PPC_HA (off) == 0)
10217 stub_size -= 4;
10218 h->root.type = bfd_link_hash_defined;
10219 h->root.u.def.section = s;
10220 h->root.u.def.value = stub_off;
10221 s->size = stub_off + stub_size;
10222 break;
10223 }
10224 return true;
10225}
10226
10227/* Set the sizes of the dynamic sections. */
10228
10229static bool
10230ppc64_elf_late_size_sections (bfd *output_bfd,
10231 struct bfd_link_info *info)
10232{
10233 struct ppc_link_hash_table *htab;
10234 bfd *dynobj;
10235 asection *s;
10236 bool relocs;
10237 bfd *ibfd;
10238 struct got_entry *first_tlsld;
10239
10240 htab = ppc_hash_table (info);
10241 if (htab == NULL)
10242 return false;
10243
10244 dynobj = htab->elf.dynobj;
10245 if (dynobj == NULL)
10246 return true;
10247
10248 if (htab->elf.dynamic_sections_created)
10249 {
10250 /* Set the contents of the .interp section to the interpreter. */
10251 if (bfd_link_executable (info) && !info->nointerp)
10252 {
10253 s = bfd_get_linker_section (dynobj, ".interp");
10254 if (s == NULL)
10255 abort ();
10256 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10257 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10258 s->alloced = 1;
10259 }
10260 }
10261
10262 /* Set up .got offsets for local syms, and space for local dynamic
10263 relocs. */
10264 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10265 {
10266 struct got_entry **lgot_ents;
10267 struct got_entry **end_lgot_ents;
10268 struct plt_entry **local_plt;
10269 struct plt_entry **end_local_plt;
10270 unsigned char *lgot_masks;
10271 bfd_size_type locsymcount;
10272 Elf_Internal_Shdr *symtab_hdr;
10273 Elf_Internal_Sym *local_syms;
10274 Elf_Internal_Sym *isym;
10275
10276 if (!is_ppc64_elf (ibfd))
10277 continue;
10278
10279 for (s = ibfd->sections; s != NULL; s = s->next)
10280 {
10281 struct ppc_local_dyn_relocs *p;
10282
10283 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10284 {
10285 if (discarded_section (p->sec))
10286 {
10287 /* Input section has been discarded, either because
10288 it is a copy of a linkonce section or due to
10289 linker script /DISCARD/, so we'll be discarding
10290 the relocs too. */
10291 }
10292 else if (p->count != 0)
10293 {
10294 unsigned int count;
10295 asection *srel;
10296
10297 count = p->count;
10298 if (info->enable_dt_relr
10299 && ((!NO_OPD_RELOCS
10300 && (ppc64_elf_section_data (p->sec)->sec_type
10301 == sec_opd))
10302 || !p->ifunc))
10303 count -= p->rel_count;
10304 srel = elf_section_data (p->sec)->sreloc;
10305 if (p->ifunc)
10306 srel = htab->elf.irelplt;
10307 srel->size += count * sizeof (Elf64_External_Rela);
10308 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10309 info->flags |= DF_TEXTREL;
10310 }
10311 }
10312 }
10313
10314 lgot_ents = elf_local_got_ents (ibfd);
10315 if (!lgot_ents)
10316 continue;
10317
10318 symtab_hdr = &elf_symtab_hdr (ibfd);
10319 locsymcount = symtab_hdr->sh_info;
10320 end_lgot_ents = lgot_ents + locsymcount;
10321 local_plt = (struct plt_entry **) end_lgot_ents;
10322 end_local_plt = local_plt + locsymcount;
10323 lgot_masks = (unsigned char *) end_local_plt;
10324 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10325 if (local_syms == NULL && locsymcount != 0)
10326 {
10327 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10328 0, NULL, NULL, NULL);
10329 if (local_syms == NULL)
10330 return false;
10331 }
10332 s = ppc64_elf_tdata (ibfd)->got;
10333 for (isym = local_syms;
10334 lgot_ents < end_lgot_ents;
10335 ++lgot_ents, ++lgot_masks, isym++)
10336 {
10337 struct got_entry **pent, *ent;
10338
10339 pent = lgot_ents;
10340 while ((ent = *pent) != NULL)
10341 if (ent->got.refcount > 0)
10342 {
10343 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10344 {
10345 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10346 *pent = ent->next;
10347 }
10348 else
10349 {
10350 unsigned int ent_size = 8;
10351 unsigned int rel_size = sizeof (Elf64_External_Rela);
10352
10353 ent->got.offset = s->size;
10354 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10355 {
10356 ent_size *= 2;
10357 rel_size *= 2;
10358 }
10359 s->size += ent_size;
10360 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10361 {
10362 htab->elf.irelplt->size += rel_size;
10363 htab->got_reli_size += rel_size;
10364 }
10365 else if (bfd_link_pic (info)
10366 && (ent->tls_type == 0
10367 ? !info->enable_dt_relr
10368 : !bfd_link_executable (info))
10369 && isym->st_shndx != SHN_ABS)
10370 {
10371 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10372 srel->size += rel_size;
10373 }
10374 pent = &ent->next;
10375 }
10376 }
10377 else
10378 *pent = ent->next;
10379 }
10380 if (local_syms != NULL
10381 && symtab_hdr->contents != (unsigned char *) local_syms)
10382 {
10383 if (!info->keep_memory)
10384 free (local_syms);
10385 else
10386 symtab_hdr->contents = (unsigned char *) local_syms;
10387 }
10388
10389 /* Allocate space for plt calls to local syms. */
10390 lgot_masks = (unsigned char *) end_local_plt;
10391 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10392 {
10393 struct plt_entry *ent;
10394
10395 for (ent = *local_plt; ent != NULL; ent = ent->next)
10396 if (ent->plt.refcount > 0)
10397 {
10398 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10399 {
10400 s = htab->elf.iplt;
10401 ent->plt.offset = s->size;
10402 s->size += PLT_ENTRY_SIZE (htab);
10403 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10404 }
10405 else if (htab->can_convert_all_inline_plt
10406 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10407 ent->plt.offset = (bfd_vma) -1;
10408 else
10409 {
10410 s = htab->pltlocal;
10411 ent->plt.offset = s->size;
10412 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10413 if (bfd_link_pic (info)
10414 && !(info->enable_dt_relr && !htab->opd_abi))
10415 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10416 }
10417 }
10418 else
10419 ent->plt.offset = (bfd_vma) -1;
10420 }
10421 }
10422
10423 /* Allocate global sym .plt and .got entries, and space for global
10424 sym dynamic relocs. */
10425 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10426
10427 if (!htab->opd_abi && !bfd_link_pic (info))
10428 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10429
10430 first_tlsld = NULL;
10431 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10432 {
10433 struct got_entry *ent;
10434
10435 if (!is_ppc64_elf (ibfd))
10436 continue;
10437
10438 ent = ppc64_tlsld_got (ibfd);
10439 if (ent->got.refcount > 0)
10440 {
10441 if (!htab->do_multi_toc && first_tlsld != NULL)
10442 {
10443 ent->is_indirect = true;
10444 ent->got.ent = first_tlsld;
10445 }
10446 else
10447 {
10448 if (first_tlsld == NULL)
10449 first_tlsld = ent;
10450 s = ppc64_elf_tdata (ibfd)->got;
10451 ent->got.offset = s->size;
10452 ent->owner = ibfd;
10453 s->size += 16;
10454 if (bfd_link_dll (info))
10455 {
10456 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10457 srel->size += sizeof (Elf64_External_Rela);
10458 }
10459 }
10460 }
10461 else
10462 ent->got.offset = (bfd_vma) -1;
10463 }
10464
10465 /* We now have determined the sizes of the various dynamic sections.
10466 Allocate memory for them. */
10467 relocs = false;
10468 for (s = dynobj->sections; s != NULL; s = s->next)
10469 {
10470 if ((s->flags & SEC_LINKER_CREATED) == 0)
10471 continue;
10472
10473 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10474 /* These haven't been allocated yet; don't strip. */
10475 continue;
10476 else if (s == htab->elf.sgot
10477 || s == htab->elf.splt
10478 || s == htab->elf.iplt
10479 || s == htab->pltlocal
10480 || s == htab->glink
10481 || s == htab->global_entry
10482 || s == htab->elf.sdynbss
10483 || s == htab->elf.sdynrelro)
10484 {
10485 /* Strip this section if we don't need it; see the
10486 comment below. */
10487 }
10488 else if (s == htab->glink_eh_frame)
10489 {
10490 if (!bfd_is_abs_section (s->output_section))
10491 /* Not sized yet. */
10492 continue;
10493 }
10494 else if (startswith (s->name, ".rela"))
10495 {
10496 if (s->size != 0)
10497 {
10498 if (s != htab->elf.srelplt)
10499 relocs = true;
10500
10501 /* We use the reloc_count field as a counter if we need
10502 to copy relocs into the output file. */
10503 s->reloc_count = 0;
10504 }
10505 }
10506 else
10507 {
10508 /* It's not one of our sections, so don't allocate space. */
10509 continue;
10510 }
10511
10512 if (s->size == 0)
10513 {
10514 /* If we don't need this section, strip it from the
10515 output file. This is mostly to handle .rela.bss and
10516 .rela.plt. We must create both sections in
10517 create_dynamic_sections, because they must be created
10518 before the linker maps input sections to output
10519 sections. The linker does that before
10520 adjust_dynamic_symbol is called, and it is that
10521 function which decides whether anything needs to go
10522 into these sections. */
10523 s->flags |= SEC_EXCLUDE;
10524 continue;
10525 }
10526
10527 if (bfd_is_abs_section (s->output_section))
10528 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10529 s->name);
10530
10531 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10532 continue;
10533
10534 /* Allocate memory for the section contents. We use bfd_zalloc
10535 here in case unused entries are not reclaimed before the
10536 section's contents are written out. This should not happen,
10537 but this way if it does we get a R_PPC64_NONE reloc in .rela
10538 sections instead of garbage.
10539 We also rely on the section contents being zero when writing
10540 the GOT and .dynrelro. */
10541 s->contents = bfd_zalloc (dynobj, s->size);
10542 if (s->contents == NULL)
10543 return false;
10544 s->alloced = 1;
10545 }
10546
10547 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10548 {
10549 if (!is_ppc64_elf (ibfd))
10550 continue;
10551
10552 s = ppc64_elf_tdata (ibfd)->got;
10553 if (s != NULL && s != htab->elf.sgot)
10554 {
10555 if (s->size == 0)
10556 s->flags |= SEC_EXCLUDE;
10557 else
10558 {
10559 s->contents = bfd_zalloc (ibfd, s->size);
10560 if (s->contents == NULL)
10561 return false;
10562 s->alloced = 1;
10563 }
10564 }
10565 s = ppc64_elf_tdata (ibfd)->relgot;
10566 if (s != NULL)
10567 {
10568 if (s->size == 0)
10569 s->flags |= SEC_EXCLUDE;
10570 else
10571 {
10572 s->contents = bfd_zalloc (ibfd, s->size);
10573 if (s->contents == NULL)
10574 return false;
10575 s->alloced = 1;
10576 relocs = true;
10577 s->reloc_count = 0;
10578 }
10579 }
10580 }
10581
10582 if (htab->elf.dynamic_sections_created)
10583 {
10584 bool tls_opt;
10585
10586 /* Add some entries to the .dynamic section. We fill in the
10587 values later, in ppc64_elf_finish_dynamic_sections, but we
10588 must add the entries now so that we get the correct size for
10589 the .dynamic section. The DT_DEBUG entry is filled in by the
10590 dynamic linker and used by the debugger. */
10591#define add_dynamic_entry(TAG, VAL) \
10592 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10593
10594 if (bfd_link_executable (info))
10595 {
10596 if (!add_dynamic_entry (DT_DEBUG, 0))
10597 return false;
10598 }
10599
10600 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10601 {
10602 if (!add_dynamic_entry (DT_PLTGOT, 0)
10603 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10604 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10605 || !add_dynamic_entry (DT_JMPREL, 0)
10606 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10607 return false;
10608 }
10609
10610 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10611 {
10612 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10613 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10614 return false;
10615 }
10616
10617 tls_opt = (htab->params->tls_get_addr_opt
10618 && ((htab->tls_get_addr_fd != NULL
10619 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10620 || (htab->tga_desc_fd != NULL
10621 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10622 if (tls_opt || !htab->opd_abi)
10623 {
10624 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10625 return false;
10626 }
10627
10628 if (relocs)
10629 {
10630 if (!add_dynamic_entry (DT_RELA, 0)
10631 || !add_dynamic_entry (DT_RELASZ, 0)
10632 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10633 return false;
10634
10635 /* If any dynamic relocs apply to a read-only section,
10636 then we need a DT_TEXTREL entry. */
10637 if ((info->flags & DF_TEXTREL) == 0)
10638 elf_link_hash_traverse (&htab->elf,
10639 _bfd_elf_maybe_set_textrel, info);
10640
10641 if ((info->flags & DF_TEXTREL) != 0)
10642 {
10643 if (!add_dynamic_entry (DT_TEXTREL, 0))
10644 return false;
10645 }
10646 }
10647 }
10648#undef add_dynamic_entry
10649
10650 return true;
10651}
10652
10653/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10654
10655static bool
10656ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10657{
10658 if (h->plt.plist != NULL
10659 && !h->def_regular
10660 && !h->pointer_equality_needed)
10661 return false;
10662
10663 return _bfd_elf_hash_symbol (h);
10664}
10665
10666/* Determine the type of stub needed, if any, for a call. */
10667
10668static inline enum ppc_stub_main_type
10669ppc_type_of_stub (asection *input_sec,
10670 const Elf_Internal_Rela *rel,
10671 struct ppc_link_hash_entry **hash,
10672 struct plt_entry **plt_ent,
10673 bfd_vma destination,
10674 unsigned long local_off)
10675{
10676 struct ppc_link_hash_entry *h = *hash;
10677 bfd_vma location;
10678 bfd_vma branch_offset;
10679 bfd_vma max_branch_offset;
10680 enum elf_ppc64_reloc_type r_type;
10681
10682 if (h != NULL)
10683 {
10684 struct plt_entry *ent;
10685 struct ppc_link_hash_entry *fdh = h;
10686 if (h->oh != NULL
10687 && h->oh->is_func_descriptor)
10688 {
10689 fdh = ppc_follow_link (h->oh);
10690 *hash = fdh;
10691 }
10692
10693 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10694 if (ent->addend == rel->r_addend
10695 && ent->plt.offset != (bfd_vma) -1)
10696 {
10697 *plt_ent = ent;
10698 return ppc_stub_plt_call;
10699 }
10700
10701 /* Here, we know we don't have a plt entry. If we don't have a
10702 either a defined function descriptor or a defined entry symbol
10703 in a regular object file, then it is pointless trying to make
10704 any other type of stub. */
10705 if (!is_static_defined (&fdh->elf)
10706 && !is_static_defined (&h->elf))
10707 return ppc_stub_none;
10708 }
10709 else if (elf_local_got_ents (input_sec->owner) != NULL)
10710 {
10711 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10712 struct plt_entry **local_plt = (struct plt_entry **)
10713 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10714 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10715
10716 if (local_plt[r_symndx] != NULL)
10717 {
10718 struct plt_entry *ent;
10719
10720 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10721 if (ent->addend == rel->r_addend
10722 && ent->plt.offset != (bfd_vma) -1)
10723 {
10724 *plt_ent = ent;
10725 return ppc_stub_plt_call;
10726 }
10727 }
10728 }
10729
10730 /* Determine where the call point is. */
10731 location = (input_sec->output_offset
10732 + input_sec->output_section->vma
10733 + rel->r_offset);
10734
10735 branch_offset = destination - location;
10736 r_type = ELF64_R_TYPE (rel->r_info);
10737
10738 /* Determine if a long branch stub is needed. */
10739 max_branch_offset = 1 << 25;
10740 if (r_type == R_PPC64_REL14
10741 || r_type == R_PPC64_REL14_BRTAKEN
10742 || r_type == R_PPC64_REL14_BRNTAKEN)
10743 max_branch_offset = 1 << 15;
10744
10745 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10746 /* We need a stub. Figure out whether a long_branch or plt_branch
10747 is needed later. */
10748 return ppc_stub_long_branch;
10749
10750 return ppc_stub_none;
10751}
10752
10753/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10754 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10755 . mflr %r12
10756 . bcl 20,31,1f
10757 .1: mflr %r11
10758 . mtlr %r12
10759 . lis %r12,xxx-1b@highest
10760 . ori %r12,%r12,xxx-1b@higher
10761 . sldi %r12,%r12,32
10762 . oris %r12,%r12,xxx-1b@high
10763 . ori %r12,%r12,xxx-1b@l
10764 . add/ldx %r12,%r11,%r12 */
10765
10766static bfd_byte *
10767build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10768{
10769 bfd_put_32 (abfd, MFLR_R12, p);
10770 p += 4;
10771 bfd_put_32 (abfd, BCL_20_31, p);
10772 p += 4;
10773 bfd_put_32 (abfd, MFLR_R11, p);
10774 p += 4;
10775 bfd_put_32 (abfd, MTLR_R12, p);
10776 p += 4;
10777 if (off + 0x8000 < 0x10000)
10778 {
10779 if (load)
10780 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10781 else
10782 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10783 p += 4;
10784 }
10785 else if (off + 0x80008000ULL < 0x100000000ULL)
10786 {
10787 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10788 p += 4;
10789 if (load)
10790 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10791 else
10792 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10793 p += 4;
10794 }
10795 else
10796 {
10797 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10798 {
10799 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10800 p += 4;
10801 }
10802 else
10803 {
10804 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10805 p += 4;
10806 if (((off >> 32) & 0xffff) != 0)
10807 {
10808 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10809 p += 4;
10810 }
10811 }
10812 if (((off >> 32) & 0xffffffffULL) != 0)
10813 {
10814 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10815 p += 4;
10816 }
10817 if (PPC_HI (off) != 0)
10818 {
10819 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10820 p += 4;
10821 }
10822 if (PPC_LO (off) != 0)
10823 {
10824 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10825 p += 4;
10826 }
10827 if (load)
10828 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10829 else
10830 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10831 p += 4;
10832 }
10833 return p;
10834}
10835
10836static unsigned int
10837size_offset (bfd_vma off)
10838{
10839 unsigned int size;
10840 if (off + 0x8000 < 0x10000)
10841 size = 4;
10842 else if (off + 0x80008000ULL < 0x100000000ULL)
10843 size = 8;
10844 else
10845 {
10846 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10847 size = 4;
10848 else
10849 {
10850 size = 4;
10851 if (((off >> 32) & 0xffff) != 0)
10852 size += 4;
10853 }
10854 if (((off >> 32) & 0xffffffffULL) != 0)
10855 size += 4;
10856 if (PPC_HI (off) != 0)
10857 size += 4;
10858 if (PPC_LO (off) != 0)
10859 size += 4;
10860 size += 4;
10861 }
10862 return size + 16;
10863}
10864
10865static unsigned int
10866num_relocs_for_offset (bfd_vma off)
10867{
10868 unsigned int num_rel;
10869 if (off + 0x8000 < 0x10000)
10870 num_rel = 1;
10871 else if (off + 0x80008000ULL < 0x100000000ULL)
10872 num_rel = 2;
10873 else
10874 {
10875 num_rel = 1;
10876 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10877 && ((off >> 32) & 0xffff) != 0)
10878 num_rel += 1;
10879 if (PPC_HI (off) != 0)
10880 num_rel += 1;
10881 if (PPC_LO (off) != 0)
10882 num_rel += 1;
10883 }
10884 return num_rel;
10885}
10886
10887static Elf_Internal_Rela *
10888emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10889 bfd_vma roff, bfd_vma targ, bfd_vma off)
10890{
10891 bfd_vma relative_targ = targ - (roff - 8);
10892 if (bfd_big_endian (info->output_bfd))
10893 roff += 2;
10894 r->r_offset = roff;
10895 r->r_addend = relative_targ + roff;
10896 if (off + 0x8000 < 0x10000)
10897 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10898 else if (off + 0x80008000ULL < 0x100000000ULL)
10899 {
10900 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10901 ++r;
10902 roff += 4;
10903 r->r_offset = roff;
10904 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10905 r->r_addend = relative_targ + roff;
10906 }
10907 else
10908 {
10909 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10910 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10911 else
10912 {
10913 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10914 if (((off >> 32) & 0xffff) != 0)
10915 {
10916 ++r;
10917 roff += 4;
10918 r->r_offset = roff;
10919 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10920 r->r_addend = relative_targ + roff;
10921 }
10922 }
10923 if (((off >> 32) & 0xffffffffULL) != 0)
10924 roff += 4;
10925 if (PPC_HI (off) != 0)
10926 {
10927 ++r;
10928 roff += 4;
10929 r->r_offset = roff;
10930 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10931 r->r_addend = relative_targ + roff;
10932 }
10933 if (PPC_LO (off) != 0)
10934 {
10935 ++r;
10936 roff += 4;
10937 r->r_offset = roff;
10938 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10939 r->r_addend = relative_targ + roff;
10940 }
10941 }
10942 return r;
10943}
10944
10945static bfd_byte *
10946build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10947 bool load)
10948{
10949 uint64_t insn;
10950 if (off - odd + (1ULL << 33) < 1ULL << 34)
10951 {
10952 off -= odd;
10953 if (odd)
10954 {
10955 bfd_put_32 (abfd, NOP, p);
10956 p += 4;
10957 }
10958 if (load)
10959 insn = PLD_R12_PC;
10960 else
10961 insn = PADDI_R12_PC;
10962 insn |= D34 (off);
10963 bfd_put_32 (abfd, insn >> 32, p);
10964 p += 4;
10965 bfd_put_32 (abfd, insn, p);
10966 }
10967 /* The minimum value for paddi is -0x200000000. The minimum value
10968 for li is -0x8000, which when shifted by 34 and added gives a
10969 minimum value of -0x2000200000000. The maximum value is
10970 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10971 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10972 {
10973 off -= 8 - odd;
10974 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10975 p += 4;
10976 if (!odd)
10977 {
10978 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10979 p += 4;
10980 }
10981 insn = PADDI_R12_PC | D34 (off);
10982 bfd_put_32 (abfd, insn >> 32, p);
10983 p += 4;
10984 bfd_put_32 (abfd, insn, p);
10985 p += 4;
10986 if (odd)
10987 {
10988 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10989 p += 4;
10990 }
10991 if (load)
10992 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10993 else
10994 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10995 }
10996 else
10997 {
10998 off -= odd + 8;
10999 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
11000 p += 4;
11001 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
11002 p += 4;
11003 if (odd)
11004 {
11005 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11006 p += 4;
11007 }
11008 insn = PADDI_R12_PC | D34 (off);
11009 bfd_put_32 (abfd, insn >> 32, p);
11010 p += 4;
11011 bfd_put_32 (abfd, insn, p);
11012 p += 4;
11013 if (!odd)
11014 {
11015 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11016 p += 4;
11017 }
11018 if (load)
11019 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11020 else
11021 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11022 }
11023 p += 4;
11024 return p;
11025}
11026
11027static unsigned int
11028size_power10_offset (bfd_vma off, int odd)
11029{
11030 if (off - odd + (1ULL << 33) < 1ULL << 34)
11031 return odd + 8;
11032 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11033 return 20;
11034 else
11035 return 24;
11036}
11037
11038static unsigned int
11039num_relocs_for_power10_offset (bfd_vma off, int odd)
11040{
11041 if (off - odd + (1ULL << 33) < 1ULL << 34)
11042 return 1;
11043 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11044 return 2;
11045 else
11046 return 3;
11047}
11048
11049static Elf_Internal_Rela *
11050emit_relocs_for_power10_offset (struct bfd_link_info *info,
11051 Elf_Internal_Rela *r, bfd_vma roff,
11052 bfd_vma targ, bfd_vma off, int odd)
11053{
11054 if (off - odd + (1ULL << 33) < 1ULL << 34)
11055 roff += odd;
11056 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11057 {
11058 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11059 r->r_offset = roff + d_offset;
11060 r->r_addend = targ + 8 - odd - d_offset;
11061 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11062 ++r;
11063 roff += 8 - odd;
11064 }
11065 else
11066 {
11067 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11068 r->r_offset = roff + d_offset;
11069 r->r_addend = targ + 8 + odd - d_offset;
11070 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11071 ++r;
11072 roff += 4;
11073 r->r_offset = roff + d_offset;
11074 r->r_addend = targ + 4 + odd - d_offset;
11075 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11076 ++r;
11077 roff += 4 + odd;
11078 }
11079 r->r_offset = roff;
11080 r->r_addend = targ;
11081 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11082 return r;
11083}
11084
11085/* Emit .eh_frame opcode to advance pc by DELTA. */
11086
11087static bfd_byte *
11088eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11089{
11090 delta /= 4;
11091 if (delta < 64)
11092 *eh++ = DW_CFA_advance_loc + delta;
11093 else if (delta < 256)
11094 {
11095 *eh++ = DW_CFA_advance_loc1;
11096 *eh++ = delta;
11097 }
11098 else if (delta < 65536)
11099 {
11100 *eh++ = DW_CFA_advance_loc2;
11101 bfd_put_16 (abfd, delta, eh);
11102 eh += 2;
11103 }
11104 else
11105 {
11106 *eh++ = DW_CFA_advance_loc4;
11107 bfd_put_32 (abfd, delta, eh);
11108 eh += 4;
11109 }
11110 return eh;
11111}
11112
11113/* Size of required .eh_frame opcode to advance pc by DELTA. */
11114
11115static unsigned int
11116eh_advance_size (unsigned int delta)
11117{
11118 if (delta < 64 * 4)
11119 /* DW_CFA_advance_loc+[1..63]. */
11120 return 1;
11121 if (delta < 256 * 4)
11122 /* DW_CFA_advance_loc1, byte. */
11123 return 2;
11124 if (delta < 65536 * 4)
11125 /* DW_CFA_advance_loc2, 2 bytes. */
11126 return 3;
11127 /* DW_CFA_advance_loc4, 4 bytes. */
11128 return 5;
11129}
11130
11131/* With power7 weakly ordered memory model, it is possible for ld.so
11132 to update a plt entry in one thread and have another thread see a
11133 stale zero toc entry. To avoid this we need some sort of acquire
11134 barrier in the call stub. One solution is to make the load of the
11135 toc word seem to appear to depend on the load of the function entry
11136 word. Another solution is to test for r2 being zero, and branch to
11137 the appropriate glink entry if so.
11138
11139 . fake dep barrier compare
11140 . ld 12,xxx(2) ld 12,xxx(2)
11141 . mtctr 12 mtctr 12
11142 . xor 11,12,12 ld 2,xxx+8(2)
11143 . add 2,2,11 cmpldi 2,0
11144 . ld 2,xxx+8(2) bnectr+
11145 . bctr b <glink_entry>
11146
11147 The solution involving the compare turns out to be faster, so
11148 that's what we use unless the branch won't reach. */
11149
11150#define ALWAYS_USE_FAKE_DEP 0
11151#define ALWAYS_EMIT_R2SAVE 0
11152
11153static inline unsigned int
11154plt_stub_size (struct ppc_link_hash_table *htab,
11155 struct ppc_stub_hash_entry *stub_entry,
11156 bfd_vma off,
11157 unsigned int odd)
11158{
11159 unsigned size;
11160
11161 if (stub_entry->type.sub == ppc_stub_notoc)
11162 {
11163 size = 8 + size_power10_offset (off, odd);
11164 if (stub_entry->type.r2save)
11165 size += 4;
11166 }
11167 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11168 {
11169 size = 8 + size_offset (off - 8);
11170 if (stub_entry->type.r2save)
11171 size += 4;
11172 }
11173 else
11174 {
11175 size = 12;
11176 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11177 size += 4;
11178 if (PPC_HA (off) != 0)
11179 size += 4;
11180 if (htab->opd_abi)
11181 {
11182 size += 4;
11183 if (htab->params->plt_static_chain)
11184 size += 4;
11185 if (htab->params->plt_thread_safe
11186 && htab->elf.dynamic_sections_created
11187 && stub_entry->h != NULL
11188 && stub_entry->h->elf.dynindx != -1)
11189 size += 8;
11190 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11191 != PPC_HA (off))
11192 size += 4;
11193 }
11194 }
11195 if (stub_entry->h != NULL
11196 && is_tls_get_addr (&stub_entry->h->elf, htab)
11197 && htab->params->tls_get_addr_opt)
11198 {
11199 if (!htab->params->no_tls_get_addr_regsave)
11200 {
11201 size += 30 * 4;
11202 if (stub_entry->type.r2save)
11203 size += 4;
11204 }
11205 else
11206 {
11207 size += 7 * 4;
11208 if (stub_entry->type.r2save)
11209 size += 6 * 4;
11210 }
11211 }
11212 return size;
11213}
11214
11215/* Depending on the sign of plt_stub_align:
11216 If positive, return the padding to align to a 2**plt_stub_align
11217 boundary.
11218 If negative, if this stub would cross fewer 2**plt_stub_align
11219 boundaries if we align, then return the padding needed to do so. */
11220
11221static inline unsigned int
11222plt_stub_pad (int plt_stub_align,
11223 bfd_vma stub_off,
11224 unsigned int stub_size)
11225{
11226 unsigned int stub_align;
11227
11228 if (plt_stub_align >= 0)
11229 stub_align = 1u << plt_stub_align;
11230 else
11231 {
11232 stub_align = 1u << -plt_stub_align;
11233 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11234 <= ((stub_size - 1) & -stub_align))
11235 return 0;
11236 }
11237 return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
11238}
11239
11240/* Build a toc using .plt call stub. */
11241
11242static inline bfd_byte *
11243build_plt_stub (struct ppc_link_hash_table *htab,
11244 struct ppc_stub_hash_entry *stub_entry,
11245 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11246{
11247 bfd *obfd = htab->params->stub_bfd;
11248 bool plt_load_toc = htab->opd_abi;
11249 bool plt_static_chain = htab->params->plt_static_chain;
11250 bool plt_thread_safe = (htab->params->plt_thread_safe
11251 && htab->elf.dynamic_sections_created
11252 && stub_entry->h != NULL
11253 && stub_entry->h->elf.dynindx != -1);
11254 bool use_fake_dep = plt_thread_safe;
11255 bfd_vma cmp_branch_off = 0;
11256
11257 if (!ALWAYS_USE_FAKE_DEP
11258 && plt_load_toc
11259 && plt_thread_safe
11260 && !(stub_entry->h != NULL
11261 && is_tls_get_addr (&stub_entry->h->elf, htab)
11262 && htab->params->tls_get_addr_opt))
11263 {
11264 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11265 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11266 / PLT_ENTRY_SIZE (htab));
11267 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11268 bfd_vma to, from;
11269
11270 if (pltindex > 32768)
11271 glinkoff += (pltindex - 32768) * 4;
11272 to = (glinkoff
11273 + htab->glink->output_offset
11274 + htab->glink->output_section->vma);
11275 from = (p - stub_entry->group->stub_sec->contents
11276 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11277 + 4 * (PPC_HA (offset) != 0)
11278 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11279 != PPC_HA (offset))
11280 + 4 * (plt_static_chain != 0)
11281 + 20
11282 + stub_entry->group->stub_sec->output_offset
11283 + stub_entry->group->stub_sec->output_section->vma);
11284 cmp_branch_off = to - from;
11285 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11286 }
11287
11288 if (PPC_HA (offset) != 0)
11289 {
11290 if (r != NULL)
11291 {
11292 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11293 r[0].r_offset += 4;
11294 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11295 r[1].r_offset = r[0].r_offset + 4;
11296 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11297 r[1].r_addend = r[0].r_addend;
11298 if (plt_load_toc)
11299 {
11300 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11301 {
11302 r[2].r_offset = r[1].r_offset + 4;
11303 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11304 r[2].r_addend = r[0].r_addend;
11305 }
11306 else
11307 {
11308 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11309 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11310 r[2].r_addend = r[0].r_addend + 8;
11311 if (plt_static_chain)
11312 {
11313 r[3].r_offset = r[2].r_offset + 4;
11314 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11315 r[3].r_addend = r[0].r_addend + 16;
11316 }
11317 }
11318 }
11319 }
11320 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11321 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11322 if (plt_load_toc)
11323 {
11324 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11325 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11326 }
11327 else
11328 {
11329 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11330 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11331 }
11332 if (plt_load_toc
11333 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11334 {
11335 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11336 offset = 0;
11337 }
11338 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11339 if (plt_load_toc)
11340 {
11341 if (use_fake_dep)
11342 {
11343 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11344 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11345 }
11346 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11347 if (plt_static_chain)
11348 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11349 }
11350 }
11351 else
11352 {
11353 if (r != NULL)
11354 {
11355 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11356 r[0].r_offset += 4;
11357 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11358 if (plt_load_toc)
11359 {
11360 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11361 {
11362 r[1].r_offset = r[0].r_offset + 4;
11363 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11364 r[1].r_addend = r[0].r_addend;
11365 }
11366 else
11367 {
11368 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11369 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11370 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11371 if (plt_static_chain)
11372 {
11373 r[2].r_offset = r[1].r_offset + 4;
11374 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11375 r[2].r_addend = r[0].r_addend + 8;
11376 }
11377 }
11378 }
11379 }
11380 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11381 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11382 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11383 if (plt_load_toc
11384 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11385 {
11386 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11387 offset = 0;
11388 }
11389 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11390 if (plt_load_toc)
11391 {
11392 if (use_fake_dep)
11393 {
11394 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11395 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11396 }
11397 if (plt_static_chain)
11398 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11399 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11400 }
11401 }
11402 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11403 {
11404 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11405 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11406 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11407 }
11408 else
11409 bfd_put_32 (obfd, BCTR, p), p += 4;
11410 return p;
11411}
11412
11413/* Build a special .plt call stub for __tls_get_addr. */
11414
11415#define LD_R0_0R3 0xe8030000
11416#define LD_R12_0R3 0xe9830000
11417#define MR_R0_R3 0x7c601b78
11418#define CMPDI_R0_0 0x2c200000
11419#define ADD_R3_R12_R13 0x7c6c6a14
11420#define BEQLR 0x4d820020
11421#define MR_R3_R0 0x7c030378
11422#define BCTRL 0x4e800421
11423
11424static bfd_byte *
11425build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11426 struct ppc_stub_hash_entry *stub_entry,
11427 bfd_byte *p)
11428{
11429 bfd *obfd = htab->params->stub_bfd;
11430
11431 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11432 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11433 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11434 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11435 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11436 bfd_put_32 (obfd, BEQLR, p), p += 4;
11437 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11438
11439 if (!htab->params->no_tls_get_addr_regsave)
11440 p = tls_get_addr_prologue (obfd, p, htab);
11441 else if (stub_entry->type.r2save)
11442 {
11443 bfd_put_32 (obfd, MFLR_R0, p);
11444 p += 4;
11445 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11446 p += 4;
11447 }
11448 return p;
11449}
11450
11451static bfd_byte *
11452build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11453 struct ppc_stub_hash_entry *stub_entry,
11454 bfd_byte *p,
11455 bfd_byte *loc)
11456{
11457 bfd *obfd = htab->params->stub_bfd;
11458
11459 if (!htab->params->no_tls_get_addr_regsave)
11460 {
11461 bfd_put_32 (obfd, BCTRL, p - 4);
11462
11463 if (stub_entry->type.r2save)
11464 {
11465 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11466 p += 4;
11467 }
11468 p = tls_get_addr_epilogue (obfd, p, htab);
11469 }
11470 else if (stub_entry->type.r2save)
11471 {
11472 bfd_put_32 (obfd, BCTRL, p - 4);
11473
11474 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11475 p += 4;
11476 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11477 p += 4;
11478 bfd_put_32 (obfd, MTLR_R0, p);
11479 p += 4;
11480 bfd_put_32 (obfd, BLR, p);
11481 p += 4;
11482 }
11483
11484 if (htab->glink_eh_frame != NULL
11485 && htab->glink_eh_frame->size != 0)
11486 {
11487 bfd_byte *base, *eh;
11488
11489 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11490 eh = base + stub_entry->group->eh_size;
11491
11492 if (!htab->params->no_tls_get_addr_regsave)
11493 {
11494 unsigned int cfa_updt, delta, i;
11495
11496 /* After the bctrl, lr has been modified so we need to emit
11497 .eh_frame info saying the return address is on the stack. In
11498 fact we must put the EH info at or before the call rather
11499 than after it, because the EH info for a call needs to be
11500 specified by that point.
11501 See libgcc/unwind-dw2.c execute_cfa_program.
11502 Any stack pointer update must be described immediately after
11503 the instruction making the change, and since the stdu occurs
11504 after saving regs we put all the reg saves and the cfa
11505 change there. */
11506 cfa_updt = stub_entry->stub_offset + 18 * 4;
11507 delta = cfa_updt - stub_entry->group->lr_restore;
11508 stub_entry->group->lr_restore
11509 = stub_entry->stub_offset + (p - loc) - 4;
11510 eh = eh_advance (htab->elf.dynobj, eh, delta);
11511 *eh++ = DW_CFA_def_cfa_offset;
11512 if (htab->opd_abi)
11513 {
11514 *eh++ = 128;
11515 *eh++ = 1;
11516 }
11517 else
11518 *eh++ = 96;
11519 *eh++ = DW_CFA_offset_extended_sf;
11520 *eh++ = 65;
11521 *eh++ = (-16 / 8) & 0x7f;
11522 for (i = 4; i < 12; i++)
11523 {
11524 *eh++ = DW_CFA_offset + i;
11525 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11526 }
11527 *eh++ = (DW_CFA_advance_loc
11528 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11529 *eh++ = DW_CFA_def_cfa_offset;
11530 *eh++ = 0;
11531 for (i = 4; i < 12; i++)
11532 *eh++ = DW_CFA_restore + i;
11533 *eh++ = DW_CFA_advance_loc + 2;
11534 *eh++ = DW_CFA_restore_extended;
11535 *eh++ = 65;
11536 stub_entry->group->eh_size = eh - base;
11537 }
11538 else if (stub_entry->type.r2save)
11539 {
11540 unsigned int lr_used, delta;
11541
11542 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11543 delta = lr_used - stub_entry->group->lr_restore;
11544 stub_entry->group->lr_restore = lr_used + 16;
11545 eh = eh_advance (htab->elf.dynobj, eh, delta);
11546 *eh++ = DW_CFA_offset_extended_sf;
11547 *eh++ = 65;
11548 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11549 *eh++ = DW_CFA_advance_loc + 4;
11550 *eh++ = DW_CFA_restore_extended;
11551 *eh++ = 65;
11552 stub_entry->group->eh_size = eh - base;
11553 }
11554 }
11555 return p;
11556}
11557
11558static Elf_Internal_Rela *
11559get_relocs (asection *sec, int count)
11560{
11561 Elf_Internal_Rela *relocs;
11562 struct bfd_elf_section_data *elfsec_data;
11563
11564 elfsec_data = elf_section_data (sec);
11565 relocs = elfsec_data->relocs;
11566 if (relocs == NULL)
11567 {
11568 bfd_size_type relsize;
11569 relsize = sec->reloc_count * sizeof (*relocs);
11570 relocs = bfd_malloc (relsize);
11571 if (relocs == NULL)
11572 return NULL;
11573 elfsec_data->relocs = relocs;
11574 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11575 sizeof (Elf_Internal_Shdr));
11576 if (elfsec_data->rela.hdr == NULL)
11577 return NULL;
11578 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11579 * sizeof (Elf64_External_Rela));
11580 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11581 sec->reloc_count = 0;
11582 }
11583 relocs += sec->reloc_count;
11584 sec->reloc_count += count;
11585 return relocs;
11586}
11587
11588static bool
11589swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
11590{
11591 if ((size_t) (loc - s->contents) >= s->size)
11592 return false;
11593 bfd_elf64_swap_reloca_out (obfd, rel, loc);
11594 return true;
11595}
11596
11597static bool
11598count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
11599{
11600 bfd_byte *loc = s->contents;
11601 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
11602 return swap_reloc_out (obfd, rel, loc, s);
11603}
11604
11605
11606/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11607 forms, to the equivalent relocs against the global symbol given by
11608 STUB_ENTRY->H. */
11609
11610static bool
11611use_global_in_relocs (struct ppc_link_hash_table *htab,
11612 struct ppc_stub_hash_entry *stub_entry,
11613 Elf_Internal_Rela *r, unsigned int num_rel)
11614{
11615 struct elf_link_hash_entry **hashes;
11616 unsigned long symndx;
11617 struct ppc_link_hash_entry *h;
11618 bfd_vma symval;
11619
11620 /* Relocs are always against symbols in their own object file. Fake
11621 up global sym hashes for the stub bfd (which has no symbols). */
11622 hashes = elf_sym_hashes (htab->params->stub_bfd);
11623 if (hashes == NULL)
11624 {
11625 bfd_size_type hsize;
11626
11627 /* When called the first time, stub_globals will contain the
11628 total number of symbols seen during stub sizing. After
11629 allocating, stub_globals is used as an index to fill the
11630 hashes array. */
11631 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11632 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11633 if (hashes == NULL)
11634 return false;
11635 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11636 htab->stub_globals = 1;
11637 }
11638 symndx = htab->stub_globals++;
11639 h = stub_entry->h;
11640 hashes[symndx] = &h->elf;
11641 if (h->oh != NULL && h->oh->is_func)
11642 h = ppc_follow_link (h->oh);
11643 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11644 || h->elf.root.type == bfd_link_hash_defweak);
11645 symval = defined_sym_val (&h->elf);
11646 while (num_rel-- != 0)
11647 {
11648 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11649 if (h->elf.root.u.def.section != stub_entry->target_section)
11650 {
11651 /* H is an opd symbol. The addend must be zero, and the
11652 branch reloc is the only one we can convert. */
11653 r->r_addend = 0;
11654 break;
11655 }
11656 else
11657 r->r_addend -= symval;
11658 --r;
11659 }
11660 return true;
11661}
11662
11663static bfd_vma
11664get_r2off (struct bfd_link_info *info,
11665 struct ppc_stub_hash_entry *stub_entry)
11666{
11667 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11668 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11669
11670 if (r2off == 0)
11671 {
11672 /* Support linking -R objects. Get the toc pointer from the
11673 opd entry. */
11674 char buf[8];
11675 if (!htab->opd_abi)
11676 return r2off;
11677 asection *opd = stub_entry->h->elf.root.u.def.section;
11678 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11679
11680 if (strcmp (opd->name, ".opd") != 0
11681 || opd->reloc_count != 0)
11682 {
11683 info->callbacks->einfo
11684 (_("%P: cannot find opd entry toc for `%pT'\n"),
11685 stub_entry->h->elf.root.root.string);
11686 bfd_set_error (bfd_error_bad_value);
11687 return (bfd_vma) -1;
11688 }
11689 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11690 return (bfd_vma) -1;
11691 r2off = bfd_get_64 (opd->owner, buf);
11692 r2off -= elf_gp (info->output_bfd);
11693 }
11694 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11695 return r2off;
11696}
11697
11698/* Debug dump. */
11699
11700static void
11701dump_stub (const char *header,
11702 struct ppc_stub_hash_entry *stub_entry,
11703 size_t end_offset)
11704{
11705 const char *t1, *t2, *t3;
11706 switch (stub_entry->type.main)
11707 {
11708 case ppc_stub_none: t1 = "none"; break;
11709 case ppc_stub_long_branch: t1 = "long_branch"; break;
11710 case ppc_stub_plt_branch: t1 = "plt_branch"; break;
11711 case ppc_stub_plt_call: t1 = "plt_call"; break;
11712 case ppc_stub_global_entry: t1 = "global_entry"; break;
11713 case ppc_stub_save_res: t1 = "save_res"; break;
11714 default: t1 = "???"; break;
11715 }
11716 switch (stub_entry->type.sub)
11717 {
11718 case ppc_stub_toc: t2 = "toc"; break;
11719 case ppc_stub_notoc: t2 = "notoc"; break;
11720 case ppc_stub_p9notoc: t2 = "p9notoc"; break;
11721 default: t2 = "???"; break;
11722 }
11723 t3 = stub_entry->type.r2save ? "r2save" : "";
11724 fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11725 header, stub_entry->id, t1, t2, t3);
11726 fprintf (stderr, "name = %s\n", stub_entry->root.string);
11727 fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
11728 for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11729 {
11730 asection *stub_sec = stub_entry->group->stub_sec;
11731 uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11732 fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
11733 }
11734 fprintf (stderr, "\n");
11735}
11736
11737static bool
11738ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11739{
11740 struct ppc_stub_hash_entry *stub_entry;
11741 struct ppc_branch_hash_entry *br_entry;
11742 struct bfd_link_info *info;
11743 struct ppc_link_hash_table *htab;
11744 bfd *obfd;
11745 bfd_byte *loc;
11746 bfd_byte *p, *relp;
11747 bfd_vma targ, off;
11748 Elf_Internal_Rela *r;
11749 asection *plt;
11750 int num_rel;
11751 int odd;
11752 bool is_tga;
11753
11754 /* Massage our args to the form they really have. */
11755 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11756 info = in_arg;
11757
11758 htab = ppc_hash_table (info);
11759 if (htab == NULL)
11760 return false;
11761
11762 struct _ppc64_elf_section_data *esd
11763 = ppc64_elf_section_data (stub_entry->group->stub_sec);
11764 ++htab->stub_id;
11765 if (stub_entry->id != htab->stub_id
11766 || (stub_entry->type.main != ppc_stub_save_res
11767 && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
11768 {
11769 BFD_ASSERT (0);
11770 if (stub_entry->id != htab->stub_id)
11771 fprintf (stderr, "Expected id %u, got %u\n",
11772 htab->stub_id, stub_entry->id);
11773 if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
11774 fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11775 PRIx64 "\n", stub_entry->group->stub_sec->size,
11776 stub_entry->stub_offset);
11777 if (esd->sec_type == sec_stub)
11778 dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11779 dump_stub ("Current:", stub_entry, 0);
11780 }
11781 if (esd->sec_type == sec_normal)
11782 esd->sec_type = sec_stub;
11783 if (esd->sec_type == sec_stub)
11784 esd->u.last_ent = stub_entry;
11785 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11786
11787 htab->stub_count[stub_entry->type.main - 1] += 1;
11788 if (stub_entry->type.main == ppc_stub_long_branch
11789 && stub_entry->type.sub == ppc_stub_toc)
11790 {
11791 /* Branches are relative. This is where we are going to. */
11792 targ = (stub_entry->target_value
11793 + stub_entry->target_section->output_offset
11794 + stub_entry->target_section->output_section->vma);
11795 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11796
11797 /* And this is where we are coming from. */
11798 off = (stub_entry->stub_offset
11799 + stub_entry->group->stub_sec->output_offset
11800 + stub_entry->group->stub_sec->output_section->vma);
11801 off = targ - off;
11802
11803 p = loc;
11804 obfd = htab->params->stub_bfd;
11805 if (stub_entry->type.r2save)
11806 {
11807 bfd_vma r2off = get_r2off (info, stub_entry);
11808
11809 if (r2off == (bfd_vma) -1)
11810 {
11811 htab->stub_error = true;
11812 return false;
11813 }
11814 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11815 p += 4;
11816 if (PPC_HA (r2off) != 0)
11817 {
11818 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11819 p += 4;
11820 }
11821 if (PPC_LO (r2off) != 0)
11822 {
11823 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11824 p += 4;
11825 }
11826 off -= p - loc;
11827 }
11828 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11829 p += 4;
11830
11831 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11832 {
11833 _bfd_error_handler
11834 (_("long branch stub `%s' offset overflow"),
11835 stub_entry->root.string);
11836 htab->stub_error = true;
11837 return false;
11838 }
11839
11840 if (info->emitrelocations)
11841 {
11842 r = get_relocs (stub_entry->group->stub_sec, 1);
11843 if (r == NULL)
11844 return false;
11845 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11846 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11847 r->r_addend = targ;
11848 if (stub_entry->h != NULL
11849 && !use_global_in_relocs (htab, stub_entry, r, 1))
11850 return false;
11851 }
11852 }
11853 else if (stub_entry->type.main == ppc_stub_plt_branch
11854 && stub_entry->type.sub == ppc_stub_toc)
11855 {
11856 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11857 stub_entry->root.string + 9,
11858 false, false);
11859 if (br_entry == NULL)
11860 {
11861 _bfd_error_handler (_("can't find branch stub `%s'"),
11862 stub_entry->root.string);
11863 htab->stub_error = true;
11864 return false;
11865 }
11866
11867 targ = (stub_entry->target_value
11868 + stub_entry->target_section->output_offset
11869 + stub_entry->target_section->output_section->vma);
11870 if (!stub_entry->type.r2save)
11871 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11872
11873 bfd_put_64 (htab->brlt->owner, targ,
11874 htab->brlt->contents + br_entry->offset);
11875
11876 if (br_entry->iter == htab->stub_iteration)
11877 {
11878 br_entry->iter = 0;
11879
11880 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11881 {
11882 /* Create a reloc for the branch lookup table entry. */
11883 Elf_Internal_Rela rela;
11884
11885 rela.r_offset = (br_entry->offset
11886 + htab->brlt->output_offset
11887 + htab->brlt->output_section->vma);
11888 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11889 rela.r_addend = targ;
11890
11891 BFD_ASSERT (count_and_swap_reloc_out (htab->relbrlt->owner, &rela,
11892 htab->relbrlt));
11893 }
11894 else if (info->emitrelocations)
11895 {
11896 r = get_relocs (htab->brlt, 1);
11897 if (r == NULL)
11898 return false;
11899 /* brlt, being SEC_LINKER_CREATED does not go through the
11900 normal reloc processing. Symbols and offsets are not
11901 translated from input file to output file form, so
11902 set up the offset per the output file. */
11903 r->r_offset = (br_entry->offset
11904 + htab->brlt->output_offset
11905 + htab->brlt->output_section->vma);
11906 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11907 r->r_addend = targ;
11908 }
11909 }
11910
11911 targ = (br_entry->offset
11912 + htab->brlt->output_offset
11913 + htab->brlt->output_section->vma);
11914
11915 off = (elf_gp (info->output_bfd)
11916 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11917 off = targ - off;
11918
11919 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11920 {
11921 info->callbacks->einfo
11922 (_("%P: linkage table error against `%pT'\n"),
11923 stub_entry->root.string);
11924 bfd_set_error (bfd_error_bad_value);
11925 htab->stub_error = true;
11926 return false;
11927 }
11928
11929 if (info->emitrelocations)
11930 {
11931 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11932 if (r == NULL)
11933 return false;
11934 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11935 if (bfd_big_endian (info->output_bfd))
11936 r[0].r_offset += 2;
11937 if (stub_entry->type.r2save)
11938 r[0].r_offset += 4;
11939 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11940 r[0].r_addend = targ;
11941 if (PPC_HA (off) != 0)
11942 {
11943 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11944 r[1].r_offset = r[0].r_offset + 4;
11945 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11946 r[1].r_addend = r[0].r_addend;
11947 }
11948 }
11949
11950 p = loc;
11951 obfd = htab->params->stub_bfd;
11952 if (!stub_entry->type.r2save)
11953 {
11954 if (PPC_HA (off) != 0)
11955 {
11956 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11957 p += 4;
11958 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11959 }
11960 else
11961 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11962 }
11963 else
11964 {
11965 bfd_vma r2off = get_r2off (info, stub_entry);
11966
11967 if (r2off == (bfd_vma) -1)
11968 {
11969 htab->stub_error = true;
11970 return false;
11971 }
11972
11973 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11974 p += 4;
11975 if (PPC_HA (off) != 0)
11976 {
11977 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11978 p += 4;
11979 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11980 }
11981 else
11982 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11983
11984 if (PPC_HA (r2off) != 0)
11985 {
11986 p += 4;
11987 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11988 }
11989 if (PPC_LO (r2off) != 0)
11990 {
11991 p += 4;
11992 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11993 }
11994 }
11995 p += 4;
11996 bfd_put_32 (obfd, MTCTR_R12, p);
11997 p += 4;
11998 bfd_put_32 (obfd, BCTR, p);
11999 p += 4;
12000 }
12001 else if (stub_entry->type.sub >= ppc_stub_notoc)
12002 {
12003 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
12004 p = loc;
12005 off = (stub_entry->stub_offset
12006 + stub_entry->group->stub_sec->output_offset
12007 + stub_entry->group->stub_sec->output_section->vma);
12008 obfd = htab->params->stub_bfd;
12009 is_tga = (is_plt
12010 && stub_entry->h != NULL
12011 && is_tls_get_addr (&stub_entry->h->elf, htab)
12012 && htab->params->tls_get_addr_opt);
12013 if (is_tga)
12014 {
12015 p = build_tls_get_addr_head (htab, stub_entry, p);
12016 off += p - loc;
12017 }
12018 if (stub_entry->type.r2save)
12019 {
12020 off += 4;
12021 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
12022 p += 4;
12023 }
12024 if (is_plt)
12025 {
12026 targ = stub_entry->plt_ent->plt.offset & ~1;
12027 if (targ >= (bfd_vma) -2)
12028 abort ();
12029
12030 plt = htab->elf.splt;
12031 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12032 {
12033 if (stub_entry->symtype == STT_GNU_IFUNC)
12034 plt = htab->elf.iplt;
12035 else
12036 plt = htab->pltlocal;
12037 }
12038 targ += plt->output_offset + plt->output_section->vma;
12039 }
12040 else
12041 targ = (stub_entry->target_value
12042 + stub_entry->target_section->output_offset
12043 + stub_entry->target_section->output_section->vma);
12044 odd = off & 4;
12045 off = targ - off;
12046
12047 relp = p;
12048 num_rel = 0;
12049 if (stub_entry->type.sub == ppc_stub_notoc)
12050 p = build_power10_offset (obfd, p, off, odd, is_plt);
12051 else
12052 {
12053 if (htab->glink_eh_frame != NULL
12054 && htab->glink_eh_frame->size != 0)
12055 {
12056 bfd_byte *base, *eh;
12057 unsigned int lr_used, delta;
12058
12059 base = (htab->glink_eh_frame->contents
12060 + stub_entry->group->eh_base + 17);
12061 eh = base + stub_entry->group->eh_size;
12062 lr_used = stub_entry->stub_offset + (p - loc) + 8;
12063 delta = lr_used - stub_entry->group->lr_restore;
12064 stub_entry->group->lr_restore = lr_used + 8;
12065 eh = eh_advance (htab->elf.dynobj, eh, delta);
12066 *eh++ = DW_CFA_register;
12067 *eh++ = 65;
12068 *eh++ = 12;
12069 *eh++ = DW_CFA_advance_loc + 2;
12070 *eh++ = DW_CFA_restore_extended;
12071 *eh++ = 65;
12072 stub_entry->group->eh_size = eh - base;
12073 }
12074
12075 /* The notoc stubs calculate their target (either a PLT entry or
12076 the global entry point of a function) relative to the PC
12077 returned by the "bcl" two instructions past the start of the
12078 sequence emitted by build_offset. The offset is therefore 8
12079 less than calculated from the start of the sequence. */
12080 off -= 8;
12081 p = build_offset (obfd, p, off, is_plt);
12082 }
12083
12084 if (stub_entry->type.main == ppc_stub_long_branch)
12085 {
12086 bfd_vma from;
12087 num_rel = 1;
12088 from = (stub_entry->stub_offset
12089 + stub_entry->group->stub_sec->output_offset
12090 + stub_entry->group->stub_sec->output_section->vma
12091 + (p - loc));
12092 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12093 }
12094 else
12095 {
12096 bfd_put_32 (obfd, MTCTR_R12, p);
12097 p += 4;
12098 bfd_put_32 (obfd, BCTR, p);
12099 }
12100 p += 4;
12101
12102 if (is_tga)
12103 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12104
12105 if (info->emitrelocations)
12106 {
12107 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12108 if (stub_entry->type.sub == ppc_stub_notoc)
12109 num_rel += num_relocs_for_power10_offset (off, odd);
12110 else
12111 {
12112 num_rel += num_relocs_for_offset (off);
12113 roff += 16;
12114 }
12115 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12116 if (r == NULL)
12117 return false;
12118 if (stub_entry->type.sub == ppc_stub_notoc)
12119 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12120 else
12121 r = emit_relocs_for_offset (info, r, roff, targ, off);
12122 if (stub_entry->type.main == ppc_stub_long_branch)
12123 {
12124 ++r;
12125 roff = p - 4 - stub_entry->group->stub_sec->contents;
12126 r->r_offset = roff;
12127 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12128 r->r_addend = targ;
12129 if (stub_entry->h != NULL
12130 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12131 return false;
12132 }
12133 }
12134 }
12135 else if (stub_entry->type.main == ppc_stub_plt_call)
12136 {
12137 if (stub_entry->h != NULL
12138 && stub_entry->h->is_func_descriptor
12139 && stub_entry->h->oh != NULL)
12140 {
12141 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12142
12143 /* If the old-ABI "dot-symbol" is undefined make it weak so
12144 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12145 if (fh->elf.root.type == bfd_link_hash_undefined
12146 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12147 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12148 fh->elf.root.type = bfd_link_hash_undefweak;
12149 }
12150
12151 /* Now build the stub. */
12152 targ = stub_entry->plt_ent->plt.offset & ~1;
12153 if (targ >= (bfd_vma) -2)
12154 abort ();
12155
12156 plt = htab->elf.splt;
12157 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12158 {
12159 if (stub_entry->symtype == STT_GNU_IFUNC)
12160 plt = htab->elf.iplt;
12161 else
12162 plt = htab->pltlocal;
12163 }
12164 targ += plt->output_offset + plt->output_section->vma;
12165
12166 off = (elf_gp (info->output_bfd)
12167 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12168 off = targ - off;
12169
12170 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12171 {
12172 info->callbacks->einfo
12173 /* xgettext:c-format */
12174 (_("%P: linkage table error against `%pT'\n"),
12175 stub_entry->h != NULL
12176 ? stub_entry->h->elf.root.root.string
12177 : "<local sym>");
12178 bfd_set_error (bfd_error_bad_value);
12179 htab->stub_error = true;
12180 return false;
12181 }
12182
12183 r = NULL;
12184 if (info->emitrelocations)
12185 {
12186 r = get_relocs (stub_entry->group->stub_sec,
12187 ((PPC_HA (off) != 0)
12188 + (htab->opd_abi
12189 ? 2 + (htab->params->plt_static_chain
12190 && PPC_HA (off + 16) == PPC_HA (off))
12191 : 1)));
12192 if (r == NULL)
12193 return false;
12194 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12195 if (bfd_big_endian (info->output_bfd))
12196 r[0].r_offset += 2;
12197 r[0].r_addend = targ;
12198 }
12199 p = loc;
12200 obfd = htab->params->stub_bfd;
12201 is_tga = (stub_entry->h != NULL
12202 && is_tls_get_addr (&stub_entry->h->elf, htab)
12203 && htab->params->tls_get_addr_opt);
12204 if (is_tga)
12205 {
12206 p = build_tls_get_addr_head (htab, stub_entry, p);
12207 if (r != NULL)
12208 r[0].r_offset += p - loc;
12209 }
12210 p = build_plt_stub (htab, stub_entry, p, off, r);
12211 if (is_tga)
12212 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12213 }
12214 else if (stub_entry->type.main == ppc_stub_save_res)
12215 return true;
12216 else
12217 {
12218 BFD_FAIL ();
12219 return false;
12220 }
12221
12222 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12223
12224 if (htab->params->emit_stub_syms)
12225 {
12226 struct elf_link_hash_entry *h;
12227 size_t len1, len2;
12228 char *name;
12229 const char *const stub_str[] = { "long_branch",
12230 "plt_branch",
12231 "plt_call" };
12232
12233 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12234 len2 = strlen (stub_entry->root.string);
12235 name = bfd_alloc (info->output_bfd, len1 + len2 + 2);
12236 if (name == NULL)
12237 return false;
12238 memcpy (name, stub_entry->root.string, 9);
12239 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12240 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12241 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12242 if (h == NULL)
12243 return false;
12244 if (h->root.type == bfd_link_hash_new)
12245 {
12246 h->root.type = bfd_link_hash_defined;
12247 h->root.u.def.section = stub_entry->group->stub_sec;
12248 h->root.u.def.value = stub_entry->stub_offset;
12249 h->ref_regular = 1;
12250 h->def_regular = 1;
12251 h->ref_regular_nonweak = 1;
12252 h->forced_local = 1;
12253 h->non_elf = 0;
12254 h->root.linker_def = 1;
12255 }
12256 }
12257
12258 return true;
12259}
12260
12261/* As above, but don't actually build the stub. Just bump offset so
12262 we know stub section sizes, and select plt_branch stubs where
12263 long_branch stubs won't do. */
12264
12265static bool
12266ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12267{
12268 struct ppc_stub_hash_entry *stub_entry;
12269 struct bfd_link_info *info;
12270 struct ppc_link_hash_table *htab;
12271 asection *plt;
12272 bfd_vma targ, off, r2off;
12273 unsigned int size, pad, extra, lr_used, delta, odd;
12274 bfd_vma stub_offset;
12275
12276 /* Massage our args to the form they really have. */
12277 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12278 info = in_arg;
12279
12280 htab = ppc_hash_table (info);
12281 if (htab == NULL)
12282 return false;
12283
12284 /* Fail if the target section could not be assigned to an output
12285 section. The user should fix his linker script. */
12286 if (stub_entry->target_section != NULL
12287 && stub_entry->target_section->output_section == NULL
12288 && info->non_contiguous_regions)
12289 info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
12290 "Retry without --enable-non-contiguous-regions.\n"),
12291 stub_entry->target_section);
12292
12293 /* Same for the group. */
12294 if (stub_entry->group->stub_sec != NULL
12295 && stub_entry->group->stub_sec->output_section == NULL
12296 && info->non_contiguous_regions)
12297 info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
12298 "Retry without --enable-non-contiguous-regions.\n"),
12299 stub_entry->group->stub_sec);
12300
12301 /* Make a note of the offset within the stubs for this entry. */
12302 stub_offset = stub_entry->group->stub_sec->size;
12303 if (htab->stub_iteration > STUB_SHRINK_ITER
12304 && stub_entry->stub_offset > stub_offset)
12305 stub_offset = stub_entry->stub_offset;
12306 stub_entry->id = ++htab->stub_id;
12307
12308 if (stub_entry->h != NULL
12309 && stub_entry->h->save_res
12310 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12311 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12312 {
12313 /* Don't make stubs to out-of-line register save/restore
12314 functions. Instead, emit copies of the functions. */
12315 stub_entry->group->needs_save_res = 1;
12316 stub_entry->type.main = ppc_stub_save_res;
12317 stub_entry->type.sub = ppc_stub_toc;
12318 stub_entry->type.r2save = 0;
12319 return true;
12320 }
12321
12322 if (stub_entry->type.main == ppc_stub_plt_branch)
12323 {
12324 /* Reset the stub type from the plt branch variant in case we now
12325 can reach with a shorter stub. */
12326 stub_entry->type.main = ppc_stub_long_branch;
12327 }
12328
12329 if (stub_entry->type.main == ppc_stub_long_branch
12330 && stub_entry->type.sub == ppc_stub_toc)
12331 {
12332 targ = (stub_entry->target_value
12333 + stub_entry->target_section->output_offset
12334 + stub_entry->target_section->output_section->vma);
12335 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12336 off = (stub_offset
12337 + stub_entry->group->stub_sec->output_offset
12338 + stub_entry->group->stub_sec->output_section->vma);
12339
12340 size = 4;
12341 r2off = 0;
12342 if (stub_entry->type.r2save)
12343 {
12344 r2off = get_r2off (info, stub_entry);
12345 if (r2off == (bfd_vma) -1)
12346 {
12347 htab->stub_error = true;
12348 return false;
12349 }
12350 size = 8;
12351 if (PPC_HA (r2off) != 0)
12352 size += 4;
12353 if (PPC_LO (r2off) != 0)
12354 size += 4;
12355 off += size - 4;
12356 }
12357 off = targ - off;
12358
12359 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12360 Do the same for -R objects without function descriptors. */
12361 if ((stub_entry->type.r2save
12362 && r2off == 0
12363 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12364 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12365 {
12366 struct ppc_branch_hash_entry *br_entry;
12367
12368 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12369 stub_entry->root.string + 9,
12370 true, false);
12371 if (br_entry == NULL)
12372 {
12373 _bfd_error_handler (_("can't build branch stub `%s'"),
12374 stub_entry->root.string);
12375 htab->stub_error = true;
12376 return false;
12377 }
12378
12379 if (br_entry->iter != htab->stub_iteration)
12380 {
12381 br_entry->iter = htab->stub_iteration;
12382 br_entry->offset = htab->brlt->size;
12383 htab->brlt->size += 8;
12384
12385 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12386 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12387 else if (info->emitrelocations)
12388 {
12389 htab->brlt->reloc_count += 1;
12390 htab->brlt->flags |= SEC_RELOC;
12391 }
12392 }
12393
12394 targ = (br_entry->offset
12395 + htab->brlt->output_offset
12396 + htab->brlt->output_section->vma);
12397 off = (elf_gp (info->output_bfd)
12398 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12399 off = targ - off;
12400
12401 if (info->emitrelocations)
12402 {
12403 stub_entry->group->stub_sec->reloc_count
12404 += 1 + (PPC_HA (off) != 0);
12405 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12406 }
12407
12408 stub_entry->type.main = ppc_stub_plt_branch;
12409 if (!stub_entry->type.r2save)
12410 {
12411 size = 12;
12412 if (PPC_HA (off) != 0)
12413 size = 16;
12414 }
12415 else
12416 {
12417 size = 16;
12418 if (PPC_HA (off) != 0)
12419 size += 4;
12420
12421 if (PPC_HA (r2off) != 0)
12422 size += 4;
12423 if (PPC_LO (r2off) != 0)
12424 size += 4;
12425 }
12426 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12427 stub_offset += pad;
12428 }
12429 else if (info->emitrelocations)
12430 {
12431 stub_entry->group->stub_sec->reloc_count += 1;
12432 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12433 }
12434 }
12435 else if (stub_entry->type.main == ppc_stub_long_branch)
12436 {
12437 off = (stub_offset
12438 + stub_entry->group->stub_sec->output_offset
12439 + stub_entry->group->stub_sec->output_section->vma);
12440 size = 0;
12441 if (stub_entry->type.r2save)
12442 size = 4;
12443 off += size;
12444 targ = (stub_entry->target_value
12445 + stub_entry->target_section->output_offset
12446 + stub_entry->target_section->output_section->vma);
12447 odd = off & 4;
12448 off = targ - off;
12449
12450 if (stub_entry->type.sub == ppc_stub_notoc)
12451 extra = size_power10_offset (off, odd);
12452 else
12453 extra = size_offset (off - 8);
12454 /* Include branch insn plus those in the offset sequence. */
12455 size += 4 + extra;
12456
12457 /* If the branch can't reach, use a plt_branch.
12458 The branch insn is at the end, or "extra" bytes along. So
12459 its offset will be "extra" bytes less that that already
12460 calculated. */
12461 if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12462 {
12463 stub_entry->type.main = ppc_stub_plt_branch;
12464 size += 4;
12465 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12466 if (pad != 0)
12467 {
12468 stub_offset += pad;
12469 off -= pad;
12470 odd ^= pad & 4;
12471 size -= extra;
12472 if (stub_entry->type.sub == ppc_stub_notoc)
12473 extra = size_power10_offset (off, odd);
12474 else
12475 extra = size_offset (off - 8);
12476 size += extra;
12477 }
12478 }
12479 else if (info->emitrelocations)
12480 stub_entry->group->stub_sec->reloc_count +=1;
12481
12482 if (info->emitrelocations)
12483 {
12484 unsigned int num_rel;
12485 if (stub_entry->type.sub == ppc_stub_notoc)
12486 num_rel = num_relocs_for_power10_offset (off, odd);
12487 else
12488 num_rel = num_relocs_for_offset (off - 8);
12489 stub_entry->group->stub_sec->reloc_count += num_rel;
12490 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12491 }
12492
12493 if (stub_entry->type.sub != ppc_stub_notoc)
12494 {
12495 /* After the bcl, lr has been modified so we need to emit
12496 .eh_frame info saying the return address is in r12. */
12497 lr_used = stub_offset + 8;
12498 if (stub_entry->type.r2save)
12499 lr_used += 4;
12500 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12501 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12502 DW_CFA_restore_extended 65. */
12503 delta = lr_used - stub_entry->group->lr_restore;
12504 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12505 stub_entry->group->lr_restore = lr_used + 8;
12506 }
12507 }
12508 else if (stub_entry->type.sub >= ppc_stub_notoc)
12509 {
12510 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12511 lr_used = 0;
12512 if (stub_entry->h != NULL
12513 && is_tls_get_addr (&stub_entry->h->elf, htab)
12514 && htab->params->tls_get_addr_opt)
12515 {
12516 lr_used += 7 * 4;
12517 if (!htab->params->no_tls_get_addr_regsave)
12518 lr_used += 11 * 4;
12519 else if (stub_entry->type.r2save)
12520 lr_used += 2 * 4;
12521 }
12522 if (stub_entry->type.r2save)
12523 lr_used += 4;
12524 targ = stub_entry->plt_ent->plt.offset & ~1;
12525 if (targ >= (bfd_vma) -2)
12526 abort ();
12527
12528 plt = htab->elf.splt;
12529 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12530 {
12531 if (stub_entry->symtype == STT_GNU_IFUNC)
12532 plt = htab->elf.iplt;
12533 else
12534 plt = htab->pltlocal;
12535 }
12536 targ += plt->output_offset + plt->output_section->vma;
12537 off = (stub_offset
12538 + stub_entry->group->stub_sec->output_offset
12539 + stub_entry->group->stub_sec->output_section->vma
12540 + lr_used);
12541 odd = off & 4;
12542 off = targ - off;
12543
12544 size = plt_stub_size (htab, stub_entry, off, odd);
12545 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12546 if (pad != 0)
12547 {
12548 stub_offset += pad;
12549 off -= pad;
12550 odd ^= pad & 4;
12551 size = plt_stub_size (htab, stub_entry, off, odd);
12552 }
12553
12554 if (info->emitrelocations)
12555 {
12556 unsigned int num_rel;
12557 if (stub_entry->type.sub == ppc_stub_notoc)
12558 num_rel = num_relocs_for_power10_offset (off, odd);
12559 else
12560 num_rel = num_relocs_for_offset (off - 8);
12561 stub_entry->group->stub_sec->reloc_count += num_rel;
12562 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12563 }
12564
12565 if (stub_entry->type.sub != ppc_stub_notoc)
12566 {
12567 /* After the bcl, lr has been modified so we need to emit
12568 .eh_frame info saying the return address is in r12. */
12569 lr_used += stub_offset + 8;
12570 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12571 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12572 DW_CFA_restore_extended 65. */
12573 delta = lr_used - stub_entry->group->lr_restore;
12574 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12575 stub_entry->group->lr_restore = lr_used + 8;
12576 }
12577 if (stub_entry->h != NULL
12578 && is_tls_get_addr (&stub_entry->h->elf, htab)
12579 && htab->params->tls_get_addr_opt)
12580 {
12581 if (!htab->params->no_tls_get_addr_regsave)
12582 {
12583 unsigned int cfa_updt = stub_offset + 18 * 4;
12584 delta = cfa_updt - stub_entry->group->lr_restore;
12585 stub_entry->group->eh_size += eh_advance_size (delta);
12586 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12587 stub_entry->group->lr_restore = stub_offset + size - 4;
12588 }
12589 else if (stub_entry->type.r2save)
12590 {
12591 lr_used = stub_offset + size - 20;
12592 delta = lr_used - stub_entry->group->lr_restore;
12593 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12594 stub_entry->group->lr_restore = stub_offset + size - 4;
12595 }
12596 }
12597 }
12598 else if (stub_entry->type.main == ppc_stub_plt_call)
12599 {
12600 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12601 if (targ >= (bfd_vma) -2)
12602 abort ();
12603 plt = htab->elf.splt;
12604 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12605 {
12606 if (stub_entry->symtype == STT_GNU_IFUNC)
12607 plt = htab->elf.iplt;
12608 else
12609 plt = htab->pltlocal;
12610 }
12611 targ += plt->output_offset + plt->output_section->vma;
12612
12613 off = (elf_gp (info->output_bfd)
12614 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12615 off = targ - off;
12616
12617 size = plt_stub_size (htab, stub_entry, off, 0);
12618 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12619 stub_offset += pad;
12620
12621 if (info->emitrelocations)
12622 {
12623 stub_entry->group->stub_sec->reloc_count
12624 += ((PPC_HA (off) != 0)
12625 + (htab->opd_abi
12626 ? 2 + (htab->params->plt_static_chain
12627 && PPC_HA (off + 16) == PPC_HA (off))
12628 : 1));
12629 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12630 }
12631
12632 if (stub_entry->h != NULL
12633 && is_tls_get_addr (&stub_entry->h->elf, htab)
12634 && htab->params->tls_get_addr_opt
12635 && stub_entry->type.r2save)
12636 {
12637 if (!htab->params->no_tls_get_addr_regsave)
12638 {
12639 /* Adjustments to r1 need to be described. */
12640 unsigned int cfa_updt = stub_offset + 18 * 4;
12641 delta = cfa_updt - stub_entry->group->lr_restore;
12642 stub_entry->group->eh_size += eh_advance_size (delta);
12643 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12644 }
12645 else
12646 {
12647 lr_used = stub_offset + size - 20;
12648 /* The eh_frame info will consist of a DW_CFA_advance_loc
12649 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12650 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12651 delta = lr_used - stub_entry->group->lr_restore;
12652 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12653 }
12654 stub_entry->group->lr_restore = stub_offset + size - 4;
12655 }
12656 }
12657 else
12658 {
12659 BFD_FAIL ();
12660 return false;
12661 }
12662
12663 if (stub_entry->stub_offset != stub_offset)
12664 htab->stub_changed = true;
12665 stub_entry->stub_offset = stub_offset;
12666 stub_entry->group->stub_sec->size = stub_offset + size;
12667 return true;
12668}
12669
12670/* Set up various things so that we can make a list of input sections
12671 for each output section included in the link. Returns -1 on error,
12672 0 when no stubs will be needed, and 1 on success. */
12673
12674int
12675ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12676{
12677 unsigned int id;
12678 size_t amt;
12679 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12680
12681 if (htab == NULL)
12682 return -1;
12683
12684 /* The access to _bfd_section_id here is unlocked, so for the time
12685 being this function cannot be called in multi-threaded mode. */
12686 BFD_ASSERT (!_bfd_threading_enabled ());
12687
12688 htab->sec_info_arr_size = _bfd_section_id;
12689 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12690 htab->sec_info = bfd_zalloc (info->output_bfd, amt);
12691 if (htab->sec_info == NULL)
12692 return -1;
12693
12694 /* Set toc_off for com, und, abs and ind sections. */
12695 for (id = 0; id < 3; id++)
12696 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12697
12698 return 1;
12699}
12700
12701/* Set up for first pass at multitoc partitioning. */
12702
12703void
12704ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12705{
12706 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12707
12708 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12709 htab->toc_bfd = NULL;
12710 htab->toc_first_sec = NULL;
12711}
12712
12713/* The linker repeatedly calls this function for each TOC input section
12714 and linker generated GOT section. Group input bfds such that the toc
12715 within a group is less than 64k in size. */
12716
12717bool
12718ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12719{
12720 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12721 bfd_vma addr, off, limit;
12722
12723 if (htab == NULL)
12724 return false;
12725
12726 if (!htab->second_toc_pass)
12727 {
12728 /* Keep track of the first .toc or .got section for this input bfd. */
12729 bool new_bfd = htab->toc_bfd != isec->owner;
12730
12731 if (new_bfd)
12732 {
12733 htab->toc_bfd = isec->owner;
12734 htab->toc_first_sec = isec;
12735 }
12736
12737 addr = isec->output_offset + isec->output_section->vma;
12738 off = addr - htab->toc_curr;
12739 limit = 0x80008000;
12740 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12741 limit = 0x10000;
12742 if (off + isec->size > limit)
12743 {
12744 addr = (htab->toc_first_sec->output_offset
12745 + htab->toc_first_sec->output_section->vma);
12746 htab->toc_curr = addr;
12747 htab->toc_curr &= -TOC_BASE_ALIGN;
12748 }
12749
12750 /* toc_curr is the base address of this toc group. Set elf_gp
12751 for the input section to be the offset relative to the
12752 output toc base plus 0x8000. Making the input elf_gp an
12753 offset allows us to move the toc as a whole without
12754 recalculating input elf_gp. */
12755 off = htab->toc_curr - elf_gp (info->output_bfd);
12756 off += TOC_BASE_OFF;
12757
12758 /* Die if someone uses a linker script that doesn't keep input
12759 file .toc and .got together. */
12760 if (new_bfd
12761 && elf_gp (isec->owner) != 0
12762 && elf_gp (isec->owner) != off)
12763 return false;
12764
12765 elf_gp (isec->owner) = off;
12766 return true;
12767 }
12768
12769 /* During the second pass toc_first_sec points to the start of
12770 a toc group, and toc_curr is used to track the old elf_gp.
12771 We use toc_bfd to ensure we only look at each bfd once. */
12772 if (htab->toc_bfd == isec->owner)
12773 return true;
12774 htab->toc_bfd = isec->owner;
12775
12776 if (htab->toc_first_sec == NULL
12777 || htab->toc_curr != elf_gp (isec->owner))
12778 {
12779 htab->toc_curr = elf_gp (isec->owner);
12780 htab->toc_first_sec = isec;
12781 }
12782 addr = (htab->toc_first_sec->output_offset
12783 + htab->toc_first_sec->output_section->vma);
12784 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12785 elf_gp (isec->owner) = off;
12786
12787 return true;
12788}
12789
12790/* Called via elf_link_hash_traverse to merge GOT entries for global
12791 symbol H. */
12792
12793static bool
12794merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12795{
12796 if (h->root.type == bfd_link_hash_indirect)
12797 return true;
12798
12799 merge_got_entries (&h->got.glist);
12800
12801 return true;
12802}
12803
12804/* Called via elf_link_hash_traverse to allocate GOT entries for global
12805 symbol H. */
12806
12807static bool
12808reallocate_got (struct elf_link_hash_entry *h, void *inf)
12809{
12810 struct got_entry *gent;
12811
12812 if (h->root.type == bfd_link_hash_indirect)
12813 return true;
12814
12815 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12816 if (!gent->is_indirect)
12817 allocate_got (h, (struct bfd_link_info *) inf, gent);
12818 return true;
12819}
12820
12821/* Called on the first multitoc pass after the last call to
12822 ppc64_elf_next_toc_section. This function removes duplicate GOT
12823 entries. */
12824
12825bool
12826ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12827{
12828 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12829 struct bfd *ibfd, *ibfd2;
12830 bool done_something;
12831
12832 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12833
12834 if (!htab->do_multi_toc)
12835 return false;
12836
12837 /* Merge global sym got entries within a toc group. */
12838 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12839
12840 /* And tlsld_got. */
12841 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12842 {
12843 struct got_entry *ent, *ent2;
12844
12845 if (!is_ppc64_elf (ibfd))
12846 continue;
12847
12848 ent = ppc64_tlsld_got (ibfd);
12849 if (!ent->is_indirect
12850 && ent->got.offset != (bfd_vma) -1)
12851 {
12852 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12853 {
12854 if (!is_ppc64_elf (ibfd2))
12855 continue;
12856
12857 ent2 = ppc64_tlsld_got (ibfd2);
12858 if (!ent2->is_indirect
12859 && ent2->got.offset != (bfd_vma) -1
12860 && elf_gp (ibfd2) == elf_gp (ibfd))
12861 {
12862 ent2->is_indirect = true;
12863 ent2->got.ent = ent;
12864 }
12865 }
12866 }
12867 }
12868
12869 /* Zap sizes of got sections. */
12870 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12871 htab->elf.irelplt->size -= htab->got_reli_size;
12872 htab->got_reli_size = 0;
12873
12874 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12875 {
12876 asection *got, *relgot;
12877
12878 if (!is_ppc64_elf (ibfd))
12879 continue;
12880
12881 got = ppc64_elf_tdata (ibfd)->got;
12882 if (got != NULL)
12883 {
12884 got->rawsize = got->size;
12885 got->size = 0;
12886 relgot = ppc64_elf_tdata (ibfd)->relgot;
12887 relgot->rawsize = relgot->size;
12888 relgot->size = 0;
12889 }
12890 }
12891
12892 /* Now reallocate the got, local syms first. We don't need to
12893 allocate section contents again since we never increase size. */
12894 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12895 {
12896 struct got_entry **lgot_ents;
12897 struct got_entry **end_lgot_ents;
12898 struct plt_entry **local_plt;
12899 struct plt_entry **end_local_plt;
12900 unsigned char *lgot_masks;
12901 bfd_size_type locsymcount;
12902 Elf_Internal_Shdr *symtab_hdr;
12903 asection *s;
12904 Elf_Internal_Sym *local_syms;
12905 Elf_Internal_Sym *isym;
12906
12907 if (!is_ppc64_elf (ibfd))
12908 continue;
12909
12910 lgot_ents = elf_local_got_ents (ibfd);
12911 if (!lgot_ents)
12912 continue;
12913
12914 symtab_hdr = &elf_symtab_hdr (ibfd);
12915 locsymcount = symtab_hdr->sh_info;
12916 end_lgot_ents = lgot_ents + locsymcount;
12917 local_plt = (struct plt_entry **) end_lgot_ents;
12918 end_local_plt = local_plt + locsymcount;
12919 lgot_masks = (unsigned char *) end_local_plt;
12920 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12921 if (local_syms == NULL && locsymcount != 0)
12922 {
12923 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12924 0, NULL, NULL, NULL);
12925 if (local_syms == NULL)
12926 return false;
12927 }
12928 s = ppc64_elf_tdata (ibfd)->got;
12929 for (isym = local_syms;
12930 lgot_ents < end_lgot_ents;
12931 ++lgot_ents, ++lgot_masks, isym++)
12932 {
12933 struct got_entry *ent;
12934
12935 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12936 {
12937 unsigned int ent_size = 8;
12938 unsigned int rel_size = sizeof (Elf64_External_Rela);
12939
12940 ent->got.offset = s->size;
12941 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12942 {
12943 ent_size *= 2;
12944 rel_size *= 2;
12945 }
12946 s->size += ent_size;
12947 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12948 {
12949 htab->elf.irelplt->size += rel_size;
12950 htab->got_reli_size += rel_size;
12951 }
12952 else if (bfd_link_pic (info)
12953 && (ent->tls_type == 0
12954 ? !info->enable_dt_relr
12955 : !bfd_link_executable (info))
12956 && isym->st_shndx != SHN_ABS)
12957 {
12958 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12959 srel->size += rel_size;
12960 }
12961 }
12962 }
12963 }
12964
12965 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12966
12967 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12968 {
12969 struct got_entry *ent;
12970
12971 if (!is_ppc64_elf (ibfd))
12972 continue;
12973
12974 ent = ppc64_tlsld_got (ibfd);
12975 if (!ent->is_indirect
12976 && ent->got.offset != (bfd_vma) -1)
12977 {
12978 asection *s = ppc64_elf_tdata (ibfd)->got;
12979 ent->got.offset = s->size;
12980 s->size += 16;
12981 if (bfd_link_dll (info))
12982 {
12983 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12984 srel->size += sizeof (Elf64_External_Rela);
12985 }
12986 }
12987 }
12988
12989 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12990 if (!done_something)
12991 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12992 {
12993 asection *got;
12994
12995 if (!is_ppc64_elf (ibfd))
12996 continue;
12997
12998 got = ppc64_elf_tdata (ibfd)->got;
12999 if (got != NULL)
13000 {
13001 done_something = got->rawsize != got->size;
13002 if (done_something)
13003 break;
13004 }
13005 }
13006
13007 if (done_something)
13008 (*htab->params->layout_sections_again) ();
13009
13010 /* Set up for second pass over toc sections to recalculate elf_gp
13011 on input sections. */
13012 htab->toc_bfd = NULL;
13013 htab->toc_first_sec = NULL;
13014 htab->second_toc_pass = true;
13015 return done_something;
13016}
13017
13018/* Called after second pass of multitoc partitioning. */
13019
13020void
13021ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
13022{
13023 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13024
13025 /* After the second pass, toc_curr tracks the TOC offset used
13026 for code sections below in ppc64_elf_next_input_section. */
13027 htab->toc_curr = TOC_BASE_OFF;
13028}
13029
13030/* No toc references were found in ISEC. If the code in ISEC makes no
13031 calls, then there's no need to use toc adjusting stubs when branching
13032 into ISEC. Actually, indirect calls from ISEC are OK as they will
13033 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
13034 needed, and 2 if a cyclical call-graph was found but no other reason
13035 for a stub was detected. If called from the top level, a return of
13036 2 means the same as a return of 0. */
13037
13038static int
13039toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
13040{
13041 int ret;
13042
13043 /* Mark this section as checked. */
13044 isec->call_check_done = 1;
13045
13046 /* We know none of our code bearing sections will need toc stubs. */
13047 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13048 return 0;
13049
13050 if (isec->size == 0)
13051 return 0;
13052
13053 if (isec->output_section == NULL)
13054 return 0;
13055
13056 ret = 0;
13057 if (isec->reloc_count != 0)
13058 {
13059 Elf_Internal_Rela *relstart, *rel;
13060 Elf_Internal_Sym *local_syms;
13061 struct ppc_link_hash_table *htab;
13062
13063 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13064 info->keep_memory);
13065 if (relstart == NULL)
13066 return -1;
13067
13068 /* Look for branches to outside of this section. */
13069 local_syms = NULL;
13070 htab = ppc_hash_table (info);
13071 if (htab == NULL)
13072 return -1;
13073
13074 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
13075 {
13076 enum elf_ppc64_reloc_type r_type;
13077 unsigned long r_symndx;
13078 struct elf_link_hash_entry *h;
13079 struct ppc_link_hash_entry *eh;
13080 Elf_Internal_Sym *sym;
13081 asection *sym_sec;
13082 struct _opd_sec_data *opd;
13083 bfd_vma sym_value;
13084 bfd_vma dest;
13085
13086 r_type = ELF64_R_TYPE (rel->r_info);
13087 if (r_type != R_PPC64_REL24
13088 && r_type != R_PPC64_REL24_NOTOC
13089 && r_type != R_PPC64_REL24_P9NOTOC
13090 && r_type != R_PPC64_REL14
13091 && r_type != R_PPC64_REL14_BRTAKEN
13092 && r_type != R_PPC64_REL14_BRNTAKEN
13093 && r_type != R_PPC64_PLTCALL
13094 && r_type != R_PPC64_PLTCALL_NOTOC)
13095 continue;
13096
13097 r_symndx = ELF64_R_SYM (rel->r_info);
13098 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13099 isec->owner))
13100 {
13101 ret = -1;
13102 break;
13103 }
13104
13105 /* Calls to dynamic lib functions go through a plt call stub
13106 that uses r2. */
13107 eh = ppc_elf_hash_entry (h);
13108 if (eh != NULL
13109 && (eh->elf.plt.plist != NULL
13110 || (eh->oh != NULL
13111 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13112 {
13113 ret = 1;
13114 break;
13115 }
13116
13117 if (sym_sec == NULL)
13118 /* Ignore other undefined symbols. */
13119 continue;
13120
13121 /* Assume branches to other sections not included in the
13122 link need stubs too, to cover -R and absolute syms. */
13123 if (sym_sec->output_section == NULL)
13124 {
13125 ret = 1;
13126 break;
13127 }
13128
13129 if (h == NULL)
13130 sym_value = sym->st_value;
13131 else
13132 {
13133 if (h->root.type != bfd_link_hash_defined
13134 && h->root.type != bfd_link_hash_defweak)
13135 abort ();
13136 sym_value = h->root.u.def.value;
13137 }
13138 sym_value += rel->r_addend;
13139
13140 /* If this branch reloc uses an opd sym, find the code section. */
13141 opd = get_opd_info (sym_sec);
13142 if (opd != NULL)
13143 {
13144 if (h == NULL && opd->adjust != NULL)
13145 {
13146 long adjust;
13147
13148 adjust = opd->adjust[OPD_NDX (sym_value)];
13149 if (adjust == -1)
13150 /* Assume deleted functions won't ever be called. */
13151 continue;
13152 sym_value += adjust;
13153 }
13154
13155 dest = opd_entry_value (sym_sec, sym_value,
13156 &sym_sec, NULL, false);
13157 if (dest == (bfd_vma) -1)
13158 continue;
13159 }
13160 else
13161 dest = (sym_value
13162 + sym_sec->output_offset
13163 + sym_sec->output_section->vma);
13164
13165 /* Ignore branch to self. */
13166 if (sym_sec == isec)
13167 continue;
13168
13169 /* If the called function uses the toc, we need a stub. */
13170 if (sym_sec->has_toc_reloc
13171 || sym_sec->makes_toc_func_call)
13172 {
13173 ret = 1;
13174 break;
13175 }
13176
13177 /* Assume any branch that needs a long branch stub might in fact
13178 need a plt_branch stub. A plt_branch stub uses r2. */
13179 else if (dest - (isec->output_offset
13180 + isec->output_section->vma
13181 + rel->r_offset) + (1 << 25)
13182 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13183 ? h->other
13184 : sym->st_other))
13185 {
13186 ret = 1;
13187 break;
13188 }
13189
13190 /* If calling back to a section in the process of being
13191 tested, we can't say for sure that no toc adjusting stubs
13192 are needed, so don't return zero. */
13193 else if (sym_sec->call_check_in_progress)
13194 ret = 2;
13195
13196 /* Branches to another section that itself doesn't have any TOC
13197 references are OK. Recursively call ourselves to check. */
13198 else if (!sym_sec->call_check_done)
13199 {
13200 int recur;
13201
13202 /* Mark current section as indeterminate, so that other
13203 sections that call back to current won't be marked as
13204 known. */
13205 isec->call_check_in_progress = 1;
13206 recur = toc_adjusting_stub_needed (info, sym_sec);
13207 isec->call_check_in_progress = 0;
13208
13209 if (recur != 0)
13210 {
13211 ret = recur;
13212 if (recur != 2)
13213 break;
13214 }
13215 }
13216 }
13217
13218 if (elf_symtab_hdr (isec->owner).contents
13219 != (unsigned char *) local_syms)
13220 free (local_syms);
13221 if (elf_section_data (isec)->relocs != relstart)
13222 free (relstart);
13223 }
13224
13225 if ((ret & 1) == 0
13226 && isec->map_head.s != NULL
13227 && (strcmp (isec->output_section->name, ".init") == 0
13228 || strcmp (isec->output_section->name, ".fini") == 0))
13229 {
13230 if (isec->map_head.s->has_toc_reloc
13231 || isec->map_head.s->makes_toc_func_call)
13232 ret = 1;
13233 else if (!isec->map_head.s->call_check_done)
13234 {
13235 int recur;
13236 isec->call_check_in_progress = 1;
13237 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13238 isec->call_check_in_progress = 0;
13239 if (recur != 0)
13240 ret = recur;
13241 }
13242 }
13243
13244 if (ret == 1)
13245 isec->makes_toc_func_call = 1;
13246
13247 return ret;
13248}
13249
13250/* The linker repeatedly calls this function for each input section,
13251 in the order that input sections are linked into output sections.
13252 Build lists of input sections to determine groupings between which
13253 we may insert linker stubs. */
13254
13255bool
13256ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13257{
13258 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13259
13260 if (htab == NULL)
13261 return false;
13262
13263 if ((isec->output_section->flags & SEC_CODE) != 0
13264 && isec->output_section->id < htab->sec_info_arr_size)
13265 {
13266 /* This happens to make the list in reverse order,
13267 which is what we want. */
13268 htab->sec_info[isec->id].u.list
13269 = htab->sec_info[isec->output_section->id].u.list;
13270 htab->sec_info[isec->output_section->id].u.list = isec;
13271 }
13272
13273 if (htab->multi_toc_needed)
13274 {
13275 /* Analyse sections that aren't already flagged as needing a
13276 valid toc pointer. Exclude .fixup for the linux kernel.
13277 .fixup contains branches, but only back to the function that
13278 hit an exception. */
13279 if (!(isec->has_toc_reloc
13280 || (isec->flags & SEC_CODE) == 0
13281 || strcmp (isec->name, ".fixup") == 0
13282 || isec->call_check_done))
13283 {
13284 if (toc_adjusting_stub_needed (info, isec) < 0)
13285 return false;
13286 }
13287 /* Make all sections use the TOC assigned for this object file.
13288 This will be wrong for pasted sections; We fix that in
13289 check_pasted_section(). */
13290 if (elf_gp (isec->owner) != 0)
13291 htab->toc_curr = elf_gp (isec->owner);
13292 }
13293
13294 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13295 return true;
13296}
13297
13298/* Check that all .init and .fini sections use the same toc, if they
13299 have toc relocs. */
13300
13301static bool
13302check_pasted_section (struct bfd_link_info *info, const char *name)
13303{
13304 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13305
13306 if (o != NULL)
13307 {
13308 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13309 bfd_vma toc_off = 0;
13310 asection *i;
13311
13312 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13313 if (i->has_toc_reloc)
13314 {
13315 if (toc_off == 0)
13316 toc_off = htab->sec_info[i->id].toc_off;
13317 else if (toc_off != htab->sec_info[i->id].toc_off)
13318 return false;
13319 }
13320
13321 if (toc_off == 0)
13322 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13323 if (i->makes_toc_func_call)
13324 {
13325 toc_off = htab->sec_info[i->id].toc_off;
13326 break;
13327 }
13328
13329 /* Make sure the whole pasted function uses the same toc offset. */
13330 if (toc_off != 0)
13331 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13332 htab->sec_info[i->id].toc_off = toc_off;
13333 }
13334 return true;
13335}
13336
13337bool
13338ppc64_elf_check_init_fini (struct bfd_link_info *info)
13339{
13340 bool ret1 = check_pasted_section (info, ".init");
13341 bool ret2 = check_pasted_section (info, ".fini");
13342
13343 return ret1 && ret2;
13344}
13345
13346/* See whether we can group stub sections together. Grouping stub
13347 sections may result in fewer stubs. More importantly, we need to
13348 put all .init* and .fini* stubs at the beginning of the .init or
13349 .fini output sections respectively, because glibc splits the
13350 _init and _fini functions into multiple parts. Putting a stub in
13351 the middle of a function is not a good idea. */
13352
13353static bool
13354group_sections (struct bfd_link_info *info,
13355 bfd_size_type stub_group_size,
13356 bool stubs_always_before_branch)
13357{
13358 struct ppc_link_hash_table *htab;
13359 asection *osec;
13360 bool suppress_size_errors;
13361
13362 htab = ppc_hash_table (info);
13363 if (htab == NULL)
13364 return false;
13365
13366 suppress_size_errors = false;
13367 if (stub_group_size == 1)
13368 {
13369 /* Default values. */
13370 if (stubs_always_before_branch)
13371 stub_group_size = 0x1e00000;
13372 else
13373 stub_group_size = 0x1c00000;
13374 suppress_size_errors = true;
13375 }
13376
13377 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13378 {
13379 asection *tail;
13380
13381 if (osec->id >= htab->sec_info_arr_size)
13382 continue;
13383
13384 tail = htab->sec_info[osec->id].u.list;
13385 while (tail != NULL)
13386 {
13387 asection *curr;
13388 asection *prev;
13389 bfd_size_type total;
13390 bool big_sec;
13391 bfd_vma curr_toc;
13392 struct map_stub *group;
13393 bfd_size_type group_size;
13394
13395 curr = tail;
13396 total = tail->size;
13397 group_size = (ppc64_elf_section_data (tail) != NULL
13398 && ppc64_elf_section_data (tail)->has_14bit_branch
13399 ? stub_group_size >> 10 : stub_group_size);
13400
13401 big_sec = total > group_size;
13402 if (big_sec && !suppress_size_errors)
13403 /* xgettext:c-format */
13404 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13405 tail->owner, tail);
13406 curr_toc = htab->sec_info[tail->id].toc_off;
13407
13408 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13409 && ((total += curr->output_offset - prev->output_offset)
13410 < (ppc64_elf_section_data (prev) != NULL
13411 && ppc64_elf_section_data (prev)->has_14bit_branch
13412 ? (group_size = stub_group_size >> 10) : group_size))
13413 && htab->sec_info[prev->id].toc_off == curr_toc)
13414 curr = prev;
13415
13416 /* OK, the size from the start of CURR to the end is less
13417 than group_size and thus can be handled by one stub
13418 section. (or the tail section is itself larger than
13419 group_size, in which case we may be toast.) We should
13420 really be keeping track of the total size of stubs added
13421 here, as stubs contribute to the final output section
13422 size. That's a little tricky, and this way will only
13423 break if stubs added make the total size more than 2^25,
13424 ie. for the default stub_group_size, if stubs total more
13425 than 2097152 bytes, or nearly 75000 plt call stubs. */
13426 group = bfd_alloc (curr->owner, sizeof (*group));
13427 if (group == NULL)
13428 return false;
13429 group->link_sec = curr;
13430 group->stub_sec = NULL;
13431 group->needs_save_res = 0;
13432 group->lr_restore = 0;
13433 group->eh_size = 0;
13434 group->eh_base = 0;
13435 group->next = htab->group;
13436 htab->group = group;
13437 do
13438 {
13439 prev = htab->sec_info[tail->id].u.list;
13440 /* Set up this stub group. */
13441 htab->sec_info[tail->id].u.group = group;
13442 }
13443 while (tail != curr && (tail = prev) != NULL);
13444
13445 /* But wait, there's more! Input sections up to group_size
13446 bytes before the stub section can be handled by it too.
13447 Don't do this if we have a really large section after the
13448 stubs, as adding more stubs increases the chance that
13449 branches may not reach into the stub section. */
13450 if (!stubs_always_before_branch && !big_sec)
13451 {
13452 total = 0;
13453 while (prev != NULL
13454 && ((total += tail->output_offset - prev->output_offset)
13455 < (ppc64_elf_section_data (prev) != NULL
13456 && ppc64_elf_section_data (prev)->has_14bit_branch
13457 ? (group_size = stub_group_size >> 10)
13458 : group_size))
13459 && htab->sec_info[prev->id].toc_off == curr_toc)
13460 {
13461 tail = prev;
13462 prev = htab->sec_info[tail->id].u.list;
13463 htab->sec_info[tail->id].u.group = group;
13464 }
13465 }
13466 tail = prev;
13467 }
13468 }
13469 return true;
13470}
13471
13472static const unsigned char glink_eh_frame_cie[] =
13473{
13474 0, 0, 0, 16, /* length. */
13475 0, 0, 0, 0, /* id. */
13476 1, /* CIE version. */
13477 'z', 'R', 0, /* Augmentation string. */
13478 4, /* Code alignment. */
13479 0x78, /* Data alignment. */
13480 65, /* RA reg. */
13481 1, /* Augmentation size. */
13482 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13483 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13484};
13485
13486/* Stripping output sections is normally done before dynamic section
13487 symbols have been allocated. This function is called later, and
13488 handles cases like htab->brlt which is mapped to its own output
13489 section. */
13490
13491static void
13492maybe_strip_output (struct bfd_link_info *info, asection *isec)
13493{
13494 if (isec->size == 0
13495 && isec->output_section->size == 0
13496 && !(isec->output_section->flags & SEC_KEEP)
13497 && !bfd_section_removed_from_list (info->output_bfd,
13498 isec->output_section)
13499 && elf_section_data (isec->output_section)->dynindx == 0)
13500 {
13501 isec->output_section->flags |= SEC_EXCLUDE;
13502 bfd_section_list_remove (info->output_bfd, isec->output_section);
13503 info->output_bfd->section_count--;
13504 }
13505}
13506
13507/* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13508 the array of such relocs. */
13509
13510static bool
13511append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13512{
13513 if (htab->relr_count >= htab->relr_alloc)
13514 {
13515 if (htab->relr_alloc == 0)
13516 htab->relr_alloc = 4096;
13517 else
13518 htab->relr_alloc *= 2;
13519 htab->relr = bfd_realloc (htab->relr,
13520 htab->relr_alloc * sizeof (*htab->relr));
13521 if (htab->relr == NULL)
13522 return false;
13523 }
13524 htab->relr[htab->relr_count].sec = sec;
13525 htab->relr[htab->relr_count].off = off;
13526 htab->relr_count++;
13527 return true;
13528}
13529
13530/* qsort comparator for bfd_vma args. */
13531
13532static int
13533compare_relr_address (const void *arg1, const void *arg2)
13534{
13535 bfd_vma a = *(bfd_vma *) arg1;
13536 bfd_vma b = *(bfd_vma *) arg2;
13537 return a < b ? -1 : a > b ? 1 : 0;
13538}
13539
13540/* Produce a malloc'd sorted array of reloc addresses from the info
13541 stored by append_relr_off. */
13542
13543static bfd_vma *
13544sort_relr (struct ppc_link_hash_table *htab)
13545{
13546 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13547 if (addr == NULL)
13548 return NULL;
13549
13550 for (size_t i = 0; i < htab->relr_count; i++)
13551 addr[i] = (htab->relr[i].sec->output_section->vma
13552 + htab->relr[i].sec->output_offset
13553 + htab->relr[i].off);
13554
13555 if (htab->relr_count > 1)
13556 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13557
13558 return addr;
13559}
13560
13561/* Look over GOT and PLT entries saved on elf_local_got_ents for all
13562 input files, stashing info about needed relative relocs. */
13563
13564static bool
13565got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13566{
13567 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13568 bfd *ibfd;
13569
13570 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13571 {
13572 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13573 struct plt_entry **local_plt, **lplt, **end_local_plt;
13574 Elf_Internal_Shdr *symtab_hdr;
13575 bfd_size_type locsymcount;
13576 Elf_Internal_Sym *local_syms;
13577 Elf_Internal_Sym *isym;
13578 struct plt_entry *pent;
13579 struct got_entry *gent;
13580
13581 if (!is_ppc64_elf (ibfd))
13582 continue;
13583
13584 lgot_ents = elf_local_got_ents (ibfd);
13585 if (!lgot_ents)
13586 continue;
13587
13588 symtab_hdr = &elf_symtab_hdr (ibfd);
13589 locsymcount = symtab_hdr->sh_info;
13590 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13591 if (local_syms == NULL && locsymcount != 0)
13592 {
13593 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13594 0, NULL, NULL, NULL);
13595 if (local_syms == NULL)
13596 return false;
13597 }
13598 end_lgot_ents = lgot_ents + locsymcount;
13599 local_plt = (struct plt_entry **) end_lgot_ents;
13600 end_local_plt = local_plt + locsymcount;
13601 for (lgot = lgot_ents, isym = local_syms;
13602 lgot < end_lgot_ents;
13603 ++lgot, ++isym)
13604 for (gent = *lgot; gent != NULL; gent = gent->next)
13605 if (!gent->is_indirect
13606 && gent->tls_type == 0
13607 && gent->got.offset != (bfd_vma) -1
13608 && isym->st_shndx != SHN_ABS)
13609 {
13610 asection *got = ppc64_elf_tdata (gent->owner)->got;
13611 if (!append_relr_off (htab, got, gent->got.offset))
13612 {
13613 htab->stub_error = true;
13614 return false;
13615 }
13616 }
13617
13618 if (!htab->opd_abi)
13619 for (lplt = local_plt, isym = local_syms;
13620 lplt < end_local_plt;
13621 ++lplt, ++isym)
13622 for (pent = *lplt; pent != NULL; pent = pent->next)
13623 if (pent->plt.offset != (bfd_vma) -1
13624 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13625 {
13626 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13627 {
13628 if (symtab_hdr->contents != (unsigned char *) local_syms)
13629 free (local_syms);
13630 return false;
13631 }
13632 }
13633
13634 if (local_syms != NULL
13635 && symtab_hdr->contents != (unsigned char *) local_syms)
13636 {
13637 if (!info->keep_memory)
13638 free (local_syms);
13639 else
13640 symtab_hdr->contents = (unsigned char *) local_syms;
13641 }
13642 }
13643 return true;
13644}
13645
13646/* Stash info about needed GOT and PLT entry relative relocs for
13647 global symbol H. */
13648
13649static bool
13650got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13651{
13652 struct bfd_link_info *info;
13653 struct ppc_link_hash_table *htab;
13654 struct plt_entry *pent;
13655 struct got_entry *gent;
13656
13657 if (h->root.type == bfd_link_hash_indirect)
13658 return true;
13659
13660 info = (struct bfd_link_info *) inf;
13661 htab = ppc_hash_table (info);
13662 if (htab == NULL)
13663 return false;
13664
13665 if (h->type != STT_GNU_IFUNC
13666 && h->def_regular
13667 && (h->root.type == bfd_link_hash_defined
13668 || h->root.type == bfd_link_hash_defweak))
13669 {
13670 if ((!htab->elf.dynamic_sections_created
13671 || h->dynindx == -1
13672 || SYMBOL_REFERENCES_LOCAL (info, h))
13673 && !bfd_is_abs_symbol (&h->root))
13674 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13675 if (!gent->is_indirect
13676 && gent->tls_type == 0
13677 && gent->got.offset != (bfd_vma) -1)
13678 {
13679 asection *got = ppc64_elf_tdata (gent->owner)->got;
13680 if (!append_relr_off (htab, got, gent->got.offset))
13681 {
13682 htab->stub_error = true;
13683 return false;
13684 }
13685 }
13686
13687 if (!htab->opd_abi
13688 && use_local_plt (info, h))
13689 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13690 if (pent->plt.offset != (bfd_vma) -1)
13691 {
13692 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13693 {
13694 htab->stub_error = true;
13695 return false;
13696 }
13697 }
13698 }
13699 return true;
13700}
13701
13702/* Determine and set the size of the stub section for a final link.
13703
13704 The basic idea here is to examine all the relocations looking for
13705 PC-relative calls to a target that is unreachable with a "bl"
13706 instruction. */
13707
13708bool
13709ppc64_elf_size_stubs (struct bfd_link_info *info)
13710{
13711 bfd_size_type stub_group_size;
13712 bool stubs_always_before_branch;
13713 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13714
13715 if (htab == NULL)
13716 return false;
13717
13718 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13719 htab->params->power10_stubs = 0;
13720
13721 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13722 htab->params->plt_thread_safe = 1;
13723 if (!htab->opd_abi)
13724 htab->params->plt_thread_safe = 0;
13725 else if (htab->params->plt_thread_safe == -1)
13726 {
13727 static const char *const thread_starter[] =
13728 {
13729 "pthread_create",
13730 /* libstdc++ */
13731 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13732 /* librt */
13733 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13734 "mq_notify", "create_timer",
13735 /* libanl */
13736 "getaddrinfo_a",
13737 /* libgomp */
13738 "GOMP_parallel",
13739 "GOMP_parallel_start",
13740 "GOMP_parallel_loop_static",
13741 "GOMP_parallel_loop_static_start",
13742 "GOMP_parallel_loop_dynamic",
13743 "GOMP_parallel_loop_dynamic_start",
13744 "GOMP_parallel_loop_guided",
13745 "GOMP_parallel_loop_guided_start",
13746 "GOMP_parallel_loop_runtime",
13747 "GOMP_parallel_loop_runtime_start",
13748 "GOMP_parallel_sections",
13749 "GOMP_parallel_sections_start",
13750 /* libgo */
13751 "__go_go",
13752 };
13753 unsigned i;
13754
13755 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13756 {
13757 struct elf_link_hash_entry *h;
13758 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13759 false, false, true);
13760 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13761 if (htab->params->plt_thread_safe)
13762 break;
13763 }
13764 }
13765 stubs_always_before_branch = htab->params->group_size < 0;
13766 if (htab->params->group_size < 0)
13767 stub_group_size = -htab->params->group_size;
13768 else
13769 stub_group_size = htab->params->group_size;
13770
13771 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13772 return false;
13773
13774 htab->tga_group = NULL;
13775 if (!htab->params->no_tls_get_addr_regsave
13776 && htab->tga_desc_fd != NULL
13777 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13778 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13779 && htab->tls_get_addr_fd != NULL
13780 && is_static_defined (&htab->tls_get_addr_fd->elf))
13781 {
13782 asection *sym_sec, *code_sec, *stub_sec;
13783 bfd_vma sym_value;
13784 struct _opd_sec_data *opd;
13785
13786 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13787 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13788 code_sec = sym_sec;
13789 opd = get_opd_info (sym_sec);
13790 if (opd != NULL)
13791 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13792 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13793 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13794 htab->tga_group->link_sec);
13795 if (stub_sec == NULL)
13796 return false;
13797 htab->tga_group->stub_sec = stub_sec;
13798
13799 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13800 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13801 htab->tga_desc_fd->elf.root.u.def.value = 0;
13802 htab->tga_desc_fd->elf.type = STT_FUNC;
13803 htab->tga_desc_fd->elf.def_regular = 1;
13804 htab->tga_desc_fd->elf.non_elf = 0;
13805 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13806 }
13807
13808 /* Loop until no stubs added. After iteration 20 of this loop we may
13809 exit on a stub section shrinking. */
13810
13811 while (1)
13812 {
13813 bfd *input_bfd;
13814 struct map_stub *group;
13815
13816 htab->stub_iteration += 1;
13817 htab->relr_count = 0;
13818
13819 for (input_bfd = info->input_bfds;
13820 input_bfd != NULL;
13821 input_bfd = input_bfd->link.next)
13822 {
13823 Elf_Internal_Shdr *symtab_hdr;
13824 asection *section;
13825 Elf_Internal_Sym *local_syms = NULL;
13826
13827 if (!is_ppc64_elf (input_bfd))
13828 continue;
13829
13830 /* We'll need the symbol table in a second. */
13831 symtab_hdr = &elf_symtab_hdr (input_bfd);
13832 if (symtab_hdr->sh_info == 0)
13833 continue;
13834
13835 /* Walk over each section attached to the input bfd. */
13836 for (section = input_bfd->sections;
13837 section != NULL;
13838 section = section->next)
13839 {
13840 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13841 bool is_opd;
13842
13843 /* If there aren't any relocs, then there's nothing more
13844 to do. */
13845 if ((section->flags & SEC_RELOC) == 0
13846 || (section->flags & SEC_ALLOC) == 0
13847 || (section->flags & SEC_LOAD) == 0
13848 || section->reloc_count == 0)
13849 continue;
13850
13851 if (!info->enable_dt_relr
13852 && (section->flags & SEC_CODE) == 0)
13853 continue;
13854
13855 /* If this section is a link-once section that will be
13856 discarded, then don't create any stubs. */
13857 if (section->output_section == NULL
13858 || section->output_section->owner != info->output_bfd)
13859 continue;
13860
13861 /* Get the relocs. */
13862 internal_relocs
13863 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13864 info->keep_memory);
13865 if (internal_relocs == NULL)
13866 goto error_ret_free_local;
13867
13868 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13869
13870 /* Now examine each relocation. */
13871 irela = internal_relocs;
13872 irelaend = irela + section->reloc_count;
13873 for (; irela < irelaend; irela++)
13874 {
13875 enum elf_ppc64_reloc_type r_type;
13876 unsigned int r_indx;
13877 struct ppc_stub_type stub_type;
13878 struct ppc_stub_hash_entry *stub_entry;
13879 asection *sym_sec, *code_sec;
13880 bfd_vma sym_value, code_value;
13881 bfd_vma destination;
13882 unsigned long local_off;
13883 bool ok_dest;
13884 struct ppc_link_hash_entry *hash;
13885 struct ppc_link_hash_entry *fdh;
13886 struct elf_link_hash_entry *h;
13887 Elf_Internal_Sym *sym;
13888 char *stub_name;
13889 const asection *id_sec;
13890 struct _opd_sec_data *opd;
13891 struct plt_entry *plt_ent;
13892
13893 r_type = ELF64_R_TYPE (irela->r_info);
13894 r_indx = ELF64_R_SYM (irela->r_info);
13895
13896 if (r_type >= R_PPC64_max)
13897 {
13898 bfd_set_error (bfd_error_bad_value);
13899 goto error_ret_free_internal;
13900 }
13901
13902 /* Only look for stubs on branch instructions. */
13903 switch (r_type)
13904 {
13905 default:
13906 if (info->enable_dt_relr
13907 && maybe_relr (r_type, irela, section))
13908 break;
13909 continue;
13910
13911 case R_PPC64_REL24:
13912 case R_PPC64_REL24_NOTOC:
13913 case R_PPC64_REL24_P9NOTOC:
13914 case R_PPC64_REL14:
13915 case R_PPC64_REL14_BRTAKEN:
13916 case R_PPC64_REL14_BRNTAKEN:
13917 if ((section->flags & SEC_CODE) != 0)
13918 break;
13919 continue;
13920 }
13921
13922 /* Now determine the call target, its name, value,
13923 section. */
13924 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13925 r_indx, input_bfd))
13926 goto error_ret_free_internal;
13927
13928 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13929 {
13930 /* Only locally defined symbols can possibly use
13931 relative relocations. */
13932 bfd_vma r_offset;
13933 if ((sym_sec == NULL
13934 || sym_sec->output_section == NULL)
13935 /* No symbol is OK too. */
13936 && !(sym != NULL && sym->st_shndx == 0)
13937 /* Hack for __ehdr_start, which is undefined
13938 at this point. */
13939 && !(h != NULL && h->root.linker_def))
13940 continue;
13941 if (NO_OPD_RELOCS && is_opd)
13942 continue;
13943 if (!is_opd
13944 && r_type == R_PPC64_ADDR64)
13945 {
13946 if (h != NULL
13947 ? h->type == STT_GNU_IFUNC
13948 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13949 continue;
13950 if (h != NULL
13951 ? bfd_is_abs_symbol (&h->root)
13952 : sym->st_shndx == SHN_ABS)
13953 continue;
13954 if (h != NULL
13955 && !SYMBOL_REFERENCES_LOCAL (info, h))
13956 continue;
13957 }
13958 r_offset = _bfd_elf_section_offset (info->output_bfd,
13959 info,
13960 section,
13961 irela->r_offset);
13962 if (r_offset >= (bfd_vma) -2)
13963 continue;
13964 if (!append_relr_off (htab, section, r_offset))
13965 goto error_ret_free_internal;
13966 continue;
13967 }
13968
13969 hash = ppc_elf_hash_entry (h);
13970 ok_dest = false;
13971 fdh = NULL;
13972 sym_value = 0;
13973 if (hash == NULL)
13974 {
13975 sym_value = sym->st_value;
13976 if (sym_sec != NULL
13977 && sym_sec->output_section != NULL)
13978 ok_dest = true;
13979 }
13980 else if (hash->elf.root.type == bfd_link_hash_defined
13981 || hash->elf.root.type == bfd_link_hash_defweak)
13982 {
13983 sym_value = hash->elf.root.u.def.value;
13984 if (sym_sec->output_section != NULL)
13985 ok_dest = true;
13986 }
13987 else if (hash->elf.root.type == bfd_link_hash_undefweak
13988 || hash->elf.root.type == bfd_link_hash_undefined)
13989 {
13990 /* Recognise an old ABI func code entry sym, and
13991 use the func descriptor sym instead if it is
13992 defined. */
13993 if (hash->elf.root.root.string[0] == '.'
13994 && hash->oh != NULL)
13995 {
13996 fdh = ppc_follow_link (hash->oh);
13997 if (fdh->elf.root.type == bfd_link_hash_defined
13998 || fdh->elf.root.type == bfd_link_hash_defweak)
13999 {
14000 sym_sec = fdh->elf.root.u.def.section;
14001 sym_value = fdh->elf.root.u.def.value;
14002 if (sym_sec->output_section != NULL)
14003 ok_dest = true;
14004 }
14005 else
14006 fdh = NULL;
14007 }
14008 }
14009 else
14010 {
14011 bfd_set_error (bfd_error_bad_value);
14012 goto error_ret_free_internal;
14013 }
14014
14015 destination = 0;
14016 local_off = 0;
14017 if (ok_dest)
14018 {
14019 sym_value += irela->r_addend;
14020 destination = (sym_value
14021 + sym_sec->output_offset
14022 + sym_sec->output_section->vma);
14023 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
14024 ? hash->elf.other
14025 : sym->st_other);
14026 }
14027
14028 code_sec = sym_sec;
14029 code_value = sym_value;
14030 opd = get_opd_info (sym_sec);
14031 if (opd != NULL)
14032 {
14033 bfd_vma dest;
14034
14035 if (hash == NULL && opd->adjust != NULL)
14036 {
14037 long adjust = opd->adjust[OPD_NDX (sym_value)];
14038 if (adjust == -1)
14039 continue;
14040 code_value += adjust;
14041 sym_value += adjust;
14042 }
14043 dest = opd_entry_value (sym_sec, sym_value,
14044 &code_sec, &code_value, false);
14045 if (dest != (bfd_vma) -1)
14046 {
14047 destination = dest;
14048 if (fdh != NULL)
14049 {
14050 /* Fixup old ABI sym to point at code
14051 entry. */
14052 hash->elf.root.type = bfd_link_hash_defweak;
14053 hash->elf.root.u.def.section = code_sec;
14054 hash->elf.root.u.def.value = code_value;
14055 }
14056 }
14057 }
14058
14059 /* Determine what (if any) linker stub is needed. */
14060 plt_ent = NULL;
14061 stub_type.main = ppc_type_of_stub (section, irela, &hash,
14062 &plt_ent, destination,
14063 local_off);
14064 stub_type.sub = ppc_stub_toc;
14065 stub_type.r2save = 0;
14066
14067 if (r_type == R_PPC64_REL24_NOTOC
14068 || r_type == R_PPC64_REL24_P9NOTOC)
14069 {
14070 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14071 if (htab->params->power10_stubs == 0
14072 || (r_type == R_PPC64_REL24_P9NOTOC
14073 && htab->params->power10_stubs != 1))
14074 notoc = ppc_stub_p9notoc;
14075 if (stub_type.main == ppc_stub_plt_call)
14076 stub_type.sub = notoc;
14077 else if (stub_type.main == ppc_stub_long_branch
14078 || (code_sec != NULL
14079 && code_sec->output_section != NULL
14080 && (((hash ? hash->elf.other : sym->st_other)
14081 & STO_PPC64_LOCAL_MASK)
14082 > 1 << STO_PPC64_LOCAL_BIT)))
14083 {
14084 stub_type.main = ppc_stub_long_branch;
14085 stub_type.sub = notoc;
14086 stub_type.r2save = 0;
14087 }
14088 }
14089 else if (stub_type.main != ppc_stub_plt_call)
14090 {
14091 /* Check whether we need a TOC adjusting stub.
14092 Since the linker pastes together pieces from
14093 different object files when creating the
14094 _init and _fini functions, it may be that a
14095 call to what looks like a local sym is in
14096 fact a call needing a TOC adjustment. */
14097 if ((code_sec != NULL
14098 && code_sec->output_section != NULL
14099 && (code_sec->has_toc_reloc
14100 || code_sec->makes_toc_func_call)
14101 && (htab->sec_info[code_sec->id].toc_off
14102 != htab->sec_info[section->id].toc_off))
14103 || (((hash ? hash->elf.other : sym->st_other)
14104 & STO_PPC64_LOCAL_MASK)
14105 == 1 << STO_PPC64_LOCAL_BIT))
14106 {
14107 stub_type.main = ppc_stub_long_branch;
14108 stub_type.sub = ppc_stub_toc;
14109 stub_type.r2save = 1;
14110 }
14111 }
14112
14113 if (stub_type.main == ppc_stub_none)
14114 continue;
14115
14116 /* __tls_get_addr calls might be eliminated. */
14117 if (stub_type.main != ppc_stub_plt_call
14118 && hash != NULL
14119 && is_tls_get_addr (&hash->elf, htab)
14120 && section->has_tls_reloc
14121 && irela != internal_relocs)
14122 {
14123 /* Get tls info. */
14124 unsigned char *tls_mask;
14125
14126 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14127 irela - 1, input_bfd))
14128 goto error_ret_free_internal;
14129 if ((*tls_mask & TLS_TLS) != 0
14130 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14131 continue;
14132 }
14133
14134 if (stub_type.main == ppc_stub_plt_call
14135 && stub_type.sub == ppc_stub_toc)
14136 {
14137 if (!htab->opd_abi
14138 && htab->params->plt_localentry0 != 0
14139 && is_elfv2_localentry0 (&hash->elf))
14140 htab->has_plt_localentry0 = 1;
14141 else if (irela + 1 < irelaend
14142 && irela[1].r_offset == irela->r_offset + 4
14143 && (ELF64_R_TYPE (irela[1].r_info)
14144 == R_PPC64_TOCSAVE))
14145 {
14146 if (!tocsave_find (htab, INSERT,
14147 &local_syms, irela + 1, input_bfd))
14148 goto error_ret_free_internal;
14149 }
14150 else
14151 stub_type.r2save = 1;
14152 }
14153
14154 /* Support for grouping stub sections. */
14155 id_sec = htab->sec_info[section->id].u.group->link_sec;
14156
14157 /* Get the name of this stub. */
14158 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14159 if (!stub_name)
14160 goto error_ret_free_internal;
14161
14162 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14163 stub_name, false, false);
14164 if (stub_entry != NULL)
14165 {
14166 free (stub_name);
14167 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14168 {
14169 /* xgettext:c-format */
14170 _bfd_error_handler
14171 (_("%pB: cannot create stub entry %s"),
14172 section->owner, stub_entry->root.string);
14173 goto error_ret_free_internal;
14174 }
14175 continue;
14176 }
14177
14178 stub_entry = ppc_add_stub (stub_name, section, info);
14179 free (stub_name);
14180 if (stub_entry == NULL)
14181 {
14182 error_ret_free_internal:
14183 if (elf_section_data (section)->relocs == NULL)
14184 free (internal_relocs);
14185 error_ret_free_local:
14186 if (symtab_hdr->contents
14187 != (unsigned char *) local_syms)
14188 free (local_syms);
14189 return false;
14190 }
14191
14192 stub_entry->type = stub_type;
14193 if (stub_type.main == ppc_stub_plt_call)
14194 {
14195 stub_entry->target_value = sym_value;
14196 stub_entry->target_section = sym_sec;
14197 }
14198 else
14199 {
14200 stub_entry->target_value = code_value;
14201 stub_entry->target_section = code_sec;
14202 }
14203 stub_entry->h = hash;
14204 stub_entry->plt_ent = plt_ent;
14205 stub_entry->symtype
14206 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14207 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14208
14209 if (hash != NULL
14210 && (hash->elf.root.type == bfd_link_hash_defined
14211 || hash->elf.root.type == bfd_link_hash_defweak))
14212 htab->stub_globals += 1;
14213 }
14214
14215 /* We're done with the internal relocs, free them. */
14216 if (elf_section_data (section)->relocs != internal_relocs)
14217 free (internal_relocs);
14218 }
14219
14220 if (local_syms != NULL
14221 && symtab_hdr->contents != (unsigned char *) local_syms)
14222 {
14223 if (!info->keep_memory)
14224 free (local_syms);
14225 else
14226 symtab_hdr->contents = (unsigned char *) local_syms;
14227 }
14228 }
14229
14230 /* We may have added some stubs. Find out the new size of the
14231 stub sections. */
14232 for (group = htab->group; group != NULL; group = group->next)
14233 {
14234 group->lr_restore = 0;
14235 group->eh_size = 0;
14236 if (group->stub_sec != NULL)
14237 {
14238 asection *stub_sec = group->stub_sec;
14239
14240 stub_sec->rawsize = stub_sec->size;
14241 stub_sec->size = 0;
14242 stub_sec->reloc_count = 0;
14243 stub_sec->flags &= ~SEC_RELOC;
14244 }
14245 }
14246 if (htab->tga_group != NULL)
14247 {
14248 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14249 htab->tga_group->eh_size
14250 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14251 htab->tga_group->lr_restore = 23 * 4;
14252 htab->tga_group->stub_sec->size = 24 * 4;
14253 }
14254
14255 htab->brlt->rawsize = htab->brlt->size;
14256 htab->brlt->size = 0;
14257 htab->brlt->reloc_count = 0;
14258 htab->brlt->flags &= ~SEC_RELOC;
14259 if (htab->relbrlt != NULL)
14260 htab->relbrlt->size = 0;
14261
14262 if (htab->elf.srelrdyn != NULL)
14263 {
14264 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14265 htab->elf.srelrdyn->size = 0;
14266 }
14267
14268 htab->stub_changed = false;
14269 htab->stub_id = 0;
14270 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14271
14272 for (group = htab->group; group != NULL; group = group->next)
14273 if (group->needs_save_res)
14274 group->stub_sec->size += htab->sfpr->size;
14275
14276 if (info->emitrelocations
14277 && htab->glink != NULL && htab->glink->size != 0)
14278 {
14279 htab->glink->reloc_count = 1;
14280 htab->glink->flags |= SEC_RELOC;
14281 }
14282
14283 if (htab->glink_eh_frame != NULL
14284 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14285 && htab->glink_eh_frame->output_section->size > 8)
14286 {
14287 size_t size = 0, align = 4;
14288
14289 for (group = htab->group; group != NULL; group = group->next)
14290 if (group->eh_size != 0)
14291 size += (group->eh_size + 17 + align - 1) & -align;
14292 if (htab->glink != NULL && htab->glink->size != 0)
14293 size += (24 + align - 1) & -align;
14294 if (size != 0)
14295 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14296 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14297 size = (size + align - 1) & -align;
14298 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14299 htab->glink_eh_frame->size = size;
14300 }
14301
14302 if (htab->params->plt_stub_align != 0)
14303 for (group = htab->group; group != NULL; group = group->next)
14304 if (group->stub_sec != NULL)
14305 {
14306 int align = abs (htab->params->plt_stub_align);
14307 group->stub_sec->size
14308 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14309 }
14310
14311 if (htab->elf.srelrdyn != NULL)
14312 {
14313 bfd_vma r_offset;
14314
14315 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14316 if (!append_relr_off (htab, htab->brlt, r_offset))
14317 return false;
14318
14319 if (!got_and_plt_relr_for_local_syms (info))
14320 return false;
14321 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14322 if (htab->stub_error)
14323 return false;
14324
14325 bfd_vma *relr_addr = sort_relr (htab);
14326 if (htab->relr_count != 0 && relr_addr == NULL)
14327 return false;
14328
14329 size_t i = 0;
14330 while (i < htab->relr_count)
14331 {
14332 bfd_vma base = relr_addr[i];
14333 htab->elf.srelrdyn->size += 8;
14334 i++;
14335 /* Handle possible duplicate address. This can happen
14336 as sections increase in size when adding stubs. */
14337 while (i < htab->relr_count
14338 && relr_addr[i] == base)
14339 i++;
14340 base += 8;
14341 while (1)
14342 {
14343 size_t start_i = i;
14344 while (i < htab->relr_count
14345 && relr_addr[i] - base < 63 * 8
14346 && (relr_addr[i] - base) % 8 == 0)
14347 i++;
14348 if (i == start_i)
14349 break;
14350 htab->elf.srelrdyn->size += 8;
14351 base += 63 * 8;
14352 }
14353 }
14354 free (relr_addr);
14355 }
14356
14357 for (group = htab->group; group != NULL; group = group->next)
14358 if (group->stub_sec != NULL
14359 && group->stub_sec->rawsize != group->stub_sec->size
14360 && (htab->stub_iteration <= STUB_SHRINK_ITER
14361 || group->stub_sec->rawsize < group->stub_sec->size))
14362 break;
14363
14364 if (group == NULL
14365 && (!htab->stub_changed
14366 || htab->stub_iteration > STUB_SHRINK_ITER)
14367 && (htab->brlt->rawsize == htab->brlt->size
14368 || (htab->stub_iteration > STUB_SHRINK_ITER
14369 && htab->brlt->rawsize > htab->brlt->size))
14370 && (htab->elf.srelrdyn == NULL
14371 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14372 || (htab->stub_iteration > STUB_SHRINK_ITER
14373 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14374 && (htab->glink_eh_frame == NULL
14375 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14376 && (htab->tga_group == NULL
14377 || htab->stub_iteration > 1))
14378 break;
14379
14380 if (htab->stub_iteration > STUB_SHRINK_ITER)
14381 {
14382 for (group = htab->group; group != NULL; group = group->next)
14383 if (group->stub_sec != NULL
14384 && group->stub_sec->size < group->stub_sec->rawsize)
14385 group->stub_sec->size = group->stub_sec->rawsize;
14386
14387 if (htab->brlt->size < htab->brlt->rawsize)
14388 htab->brlt->size = htab->brlt->rawsize;
14389
14390 if (htab->elf.srelrdyn != NULL
14391 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14392 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14393 }
14394
14395 /* Ask the linker to do its stuff. */
14396 (*htab->params->layout_sections_again) ();
14397 }
14398
14399 if (htab->glink_eh_frame != NULL
14400 && htab->glink_eh_frame->size != 0)
14401 {
14402 bfd_vma val;
14403 bfd_byte *p, *last_fde;
14404 size_t last_fde_len, size, align, pad;
14405 struct map_stub *group;
14406
14407 /* It is necessary to at least have a rough outline of the
14408 linker generated CIEs and FDEs written before
14409 bfd_elf_discard_info is run, in order for these FDEs to be
14410 indexed in .eh_frame_hdr. */
14411 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14412 if (p == NULL)
14413 return false;
14414 htab->glink_eh_frame->contents = p;
14415 htab->glink_eh_frame->alloced = 1;
14416 last_fde = p;
14417 align = 4;
14418
14419 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14420 /* CIE length (rewrite in case little-endian). */
14421 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14422 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14423 p += last_fde_len + 4;
14424
14425 for (group = htab->group; group != NULL; group = group->next)
14426 if (group->eh_size != 0)
14427 {
14428 group->eh_base = p - htab->glink_eh_frame->contents;
14429 last_fde = p;
14430 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14431 /* FDE length. */
14432 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14433 p += 4;
14434 /* CIE pointer. */
14435 val = p - htab->glink_eh_frame->contents;
14436 bfd_put_32 (htab->elf.dynobj, val, p);
14437 p += 4;
14438 /* Offset to stub section, written later. */
14439 p += 4;
14440 /* stub section size. */
14441 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14442 p += 4;
14443 /* Augmentation. */
14444 p += 1;
14445 /* Make sure we don't have all nops. This is enough for
14446 elf-eh-frame.c to detect the last non-nop opcode. */
14447 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14448 p = last_fde + last_fde_len + 4;
14449 }
14450 if (htab->glink != NULL && htab->glink->size != 0)
14451 {
14452 last_fde = p;
14453 last_fde_len = ((24 + align - 1) & -align) - 4;
14454 /* FDE length. */
14455 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14456 p += 4;
14457 /* CIE pointer. */
14458 val = p - htab->glink_eh_frame->contents;
14459 bfd_put_32 (htab->elf.dynobj, val, p);
14460 p += 4;
14461 /* Offset to .glink, written later. */
14462 p += 4;
14463 /* .glink size. */
14464 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14465 p += 4;
14466 /* Augmentation. */
14467 p += 1;
14468
14469 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14470 *p++ = DW_CFA_register;
14471 *p++ = 65;
14472 *p++ = htab->opd_abi ? 12 : 0;
14473 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14474 *p++ = DW_CFA_restore_extended;
14475 *p++ = 65;
14476 p += ((24 + align - 1) & -align) - 24;
14477 }
14478 /* Subsume any padding into the last FDE if user .eh_frame
14479 sections are aligned more than glink_eh_frame. Otherwise any
14480 zero padding will be seen as a terminator. */
14481 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14482 size = p - htab->glink_eh_frame->contents;
14483 pad = ((size + align - 1) & -align) - size;
14484 htab->glink_eh_frame->size = size + pad;
14485 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14486 }
14487
14488 maybe_strip_output (info, htab->brlt);
14489 if (htab->relbrlt != NULL)
14490 maybe_strip_output (info, htab->relbrlt);
14491 if (htab->glink_eh_frame != NULL)
14492 maybe_strip_output (info, htab->glink_eh_frame);
14493 if (htab->elf.srelrdyn != NULL)
14494 maybe_strip_output (info, htab->elf.srelrdyn);
14495
14496 return true;
14497}
14498
14499/* Called after we have determined section placement. If sections
14500 move, we'll be called again. Provide a value for TOCstart. */
14501
14502bfd_vma
14503ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14504{
14505 asection *s;
14506 bfd_vma TOCstart, adjust;
14507
14508 if (info != NULL)
14509 {
14510 struct elf_link_hash_entry *h;
14511 struct elf_link_hash_table *htab = elf_hash_table (info);
14512
14513 if (is_elf_hash_table (&htab->root)
14514 && htab->hgot != NULL)
14515 h = htab->hgot;
14516 else
14517 {
14518 h = (struct elf_link_hash_entry *)
14519 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14520 if (is_elf_hash_table (&htab->root))
14521 htab->hgot = h;
14522 }
14523 if (h != NULL
14524 && h->root.type == bfd_link_hash_defined
14525 && !h->root.linker_def
14526 && (!is_elf_hash_table (&htab->root)
14527 || h->def_regular))
14528 {
14529 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14530 _bfd_set_gp_value (obfd, TOCstart);
14531 return TOCstart;
14532 }
14533 }
14534
14535 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14536 order. The TOC starts where the first of these sections starts. */
14537 s = bfd_get_section_by_name (obfd, ".got");
14538 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14539 s = bfd_get_section_by_name (obfd, ".toc");
14540 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14541 s = bfd_get_section_by_name (obfd, ".tocbss");
14542 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14543 s = bfd_get_section_by_name (obfd, ".plt");
14544 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14545 {
14546 /* This may happen for
14547 o references to TOC base (SYM@toc / TOC[tc0]) without a
14548 .toc directive
14549 o bad linker script
14550 o --gc-sections and empty TOC sections
14551
14552 FIXME: Warn user? */
14553
14554 /* Look for a likely section. We probably won't even be
14555 using TOCstart. */
14556 for (s = obfd->sections; s != NULL; s = s->next)
14557 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14558 | SEC_EXCLUDE))
14559 == (SEC_ALLOC | SEC_SMALL_DATA))
14560 break;
14561 if (s == NULL)
14562 for (s = obfd->sections; s != NULL; s = s->next)
14563 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14564 == (SEC_ALLOC | SEC_SMALL_DATA))
14565 break;
14566 if (s == NULL)
14567 for (s = obfd->sections; s != NULL; s = s->next)
14568 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14569 == SEC_ALLOC)
14570 break;
14571 if (s == NULL)
14572 for (s = obfd->sections; s != NULL; s = s->next)
14573 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14574 break;
14575 }
14576
14577 TOCstart = 0;
14578 if (s != NULL)
14579 TOCstart = s->output_section->vma + s->output_offset;
14580
14581 /* Force alignment. */
14582 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14583 TOCstart -= adjust;
14584 _bfd_set_gp_value (obfd, TOCstart);
14585
14586 if (info != NULL && s != NULL)
14587 {
14588 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14589
14590 if (htab != NULL)
14591 {
14592 if (htab->elf.hgot != NULL)
14593 {
14594 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14595 htab->elf.hgot->root.u.def.section = s;
14596 }
14597 }
14598 else
14599 {
14600 struct bfd_link_hash_entry *bh = NULL;
14601 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14602 s, TOC_BASE_OFF - adjust,
14603 NULL, false, false, &bh);
14604 }
14605 }
14606 return TOCstart;
14607}
14608
14609/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14610 write out any global entry stubs, and PLT relocations. */
14611
14612static bool
14613build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14614{
14615 struct bfd_link_info *info;
14616 struct ppc_link_hash_table *htab;
14617 struct plt_entry *ent;
14618 asection *s;
14619
14620 if (h->root.type == bfd_link_hash_indirect)
14621 return true;
14622
14623 info = inf;
14624 htab = ppc_hash_table (info);
14625 if (htab == NULL)
14626 return false;
14627
14628 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14629 if (ent->plt.offset != (bfd_vma) -1)
14630 {
14631 /* This symbol has an entry in the procedure linkage
14632 table. Set it up. */
14633 Elf_Internal_Rela rela;
14634 asection *plt, *relplt;
14635 bfd_byte *loc;
14636
14637 if (use_local_plt (info, h))
14638 {
14639 if (!(h->def_regular
14640 && (h->root.type == bfd_link_hash_defined
14641 || h->root.type == bfd_link_hash_defweak)))
14642 continue;
14643 if (h->type == STT_GNU_IFUNC)
14644 {
14645 plt = htab->elf.iplt;
14646 relplt = htab->elf.irelplt;
14647 htab->elf.ifunc_resolvers = true;
14648 if (htab->opd_abi)
14649 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14650 else
14651 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14652 }
14653 else
14654 {
14655 plt = htab->pltlocal;
14656 relplt = NULL;
14657 if (bfd_link_pic (info)
14658 && !(info->enable_dt_relr && !htab->opd_abi))
14659 {
14660 relplt = htab->relpltlocal;
14661 if (htab->opd_abi)
14662 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14663 else
14664 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14665 }
14666 }
14667 rela.r_addend = defined_sym_val (h) + ent->addend;
14668
14669 if (relplt == NULL)
14670 {
14671 loc = plt->contents + ent->plt.offset;
14672 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14673 if (htab->opd_abi)
14674 {
14675 bfd_vma toc = elf_gp (info->output_bfd);
14676 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14677 bfd_put_64 (info->output_bfd, toc, loc + 8);
14678 }
14679 }
14680 else
14681 {
14682 rela.r_offset = (plt->output_section->vma
14683 + plt->output_offset
14684 + ent->plt.offset);
14685 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
14686 relplt));
14687 }
14688 }
14689 else
14690 {
14691 rela.r_offset = (htab->elf.splt->output_section->vma
14692 + htab->elf.splt->output_offset
14693 + ent->plt.offset);
14694 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14695 rela.r_addend = ent->addend;
14696 loc = (htab->elf.srelplt->contents
14697 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14698 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14699 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14700 htab->elf.ifunc_resolvers = true;
14701 BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
14702 loc, htab->elf.srelplt));
14703 }
14704 }
14705
14706 if (!h->pointer_equality_needed)
14707 return true;
14708
14709 if (h->def_regular)
14710 return true;
14711
14712 s = htab->global_entry;
14713 if (s == NULL || s->size == 0)
14714 return true;
14715
14716 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14717 if (ent->plt.offset != (bfd_vma) -1
14718 && ent->addend == 0)
14719 {
14720 bfd_byte *p;
14721 asection *plt;
14722 bfd_vma off;
14723
14724 p = s->contents + h->root.u.def.value;
14725 plt = htab->elf.splt;
14726 if (use_local_plt (info, h))
14727 {
14728 if (h->type == STT_GNU_IFUNC)
14729 plt = htab->elf.iplt;
14730 else
14731 plt = htab->pltlocal;
14732 }
14733 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14734 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14735
14736 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14737 {
14738 info->callbacks->einfo
14739 (_("%P: linkage table error against `%pT'\n"),
14740 h->root.root.string);
14741 bfd_set_error (bfd_error_bad_value);
14742 htab->stub_error = true;
14743 }
14744
14745 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14746 if (htab->params->emit_stub_syms)
14747 {
14748 size_t len = strlen (h->root.root.string);
14749 char *name = bfd_alloc (info->output_bfd,
14750 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
14790static bool
14791write_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
14904static bool
14905emit_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
14935static bfd_byte *
14936emit_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
14973bool
14974ppc64_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->alloced = 1;
14999 stub_sec->size = 0;
15000 }
15001 }
15002
15003 if (htab->glink != NULL && htab->glink->size != 0)
15004 {
15005 unsigned int indx;
15006 bfd_vma plt0;
15007
15008 /* Build the .glink plt call stub. */
15009 if (htab->params->emit_stub_syms)
15010 {
15011 struct elf_link_hash_entry *h;
15012 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
15013 true, false, false);
15014 if (h == NULL)
15015 return false;
15016 if (h->root.type == bfd_link_hash_new)
15017 {
15018 h->root.type = bfd_link_hash_defined;
15019 h->root.u.def.section = htab->glink;
15020 h->root.u.def.value = 8;
15021 h->ref_regular = 1;
15022 h->def_regular = 1;
15023 h->ref_regular_nonweak = 1;
15024 h->forced_local = 1;
15025 h->non_elf = 0;
15026 h->root.linker_def = 1;
15027 }
15028 }
15029 plt0 = (htab->elf.splt->output_section->vma
15030 + htab->elf.splt->output_offset
15031 - 16);
15032 if (info->emitrelocations)
15033 {
15034 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15035 if (r == NULL)
15036 return false;
15037 r->r_offset = (htab->glink->output_offset
15038 + htab->glink->output_section->vma);
15039 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15040 r->r_addend = plt0;
15041 }
15042 p = htab->glink->contents;
15043 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
15044 bfd_put_64 (htab->glink->owner, plt0, p);
15045 p += 8;
15046 if (htab->opd_abi)
15047 {
15048 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15049 p += 4;
15050 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15051 p += 4;
15052 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15053 p += 4;
15054 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15055 p += 4;
15056 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15057 p += 4;
15058 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15059 p += 4;
15060 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15061 p += 4;
15062 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15063 p += 4;
15064 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15065 p += 4;
15066 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15067 p += 4;
15068 }
15069 else
15070 {
15071 unsigned int insn;
15072
15073 /* 0:
15074 . .quad plt0-1f # plt0 entry relative to 1:
15075 #
15076 # We get here with r12 initially @ a glink branch
15077 # Load the address of _dl_runtime_resolve from plt0 and
15078 # jump to it, with r0 set to the index of the PLT entry
15079 # to be resolved and r11 the link map.
15080 __glink_PLTresolve:
15081 . std %r2,24(%r1) # optional
15082 . mflr %r0
15083 . bcl 20,31,1f
15084 1:
15085 . mflr %r11
15086 . mtlr %r0
15087 . ld %r0,(0b-1b)(%r11)
15088 . sub %r12,%r12,%r11
15089 . add %r11,%r0,%r11
15090 . addi %r0,%r12,1b-2f
15091 . ld %r12,0(%r11)
15092 . srdi %r0,%r0,2
15093 . mtctr %r12
15094 . ld %r11,8(%r11)
15095 . bctr
15096 2:
15097 . b __glink_PLTresolve
15098 . ...
15099 . b __glink_PLTresolve */
15100
15101 if (htab->has_plt_localentry0)
15102 {
15103 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15104 p += 4;
15105 }
15106 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15107 p += 4;
15108 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15109 p += 4;
15110 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15111 p += 4;
15112 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15113 p += 4;
15114 if (htab->has_plt_localentry0)
15115 insn = LD_R0_0R11 | (-20 & 0xfffc);
15116 else
15117 insn = LD_R0_0R11 | (-16 & 0xfffc);
15118 bfd_put_32 (htab->glink->owner, insn, p);
15119 p += 4;
15120 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15121 p += 4;
15122 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15123 p += 4;
15124 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15125 p += 4;
15126 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15127 p += 4;
15128 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15129 p += 4;
15130 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15131 p += 4;
15132 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15133 p += 4;
15134 }
15135 bfd_put_32 (htab->glink->owner, BCTR, p);
15136 p += 4;
15137 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15138
15139 /* Build the .glink lazy link call stubs. */
15140 indx = 0;
15141 while (p < htab->glink->contents + htab->glink->size)
15142 {
15143 if (htab->opd_abi)
15144 {
15145 if (indx < 0x8000)
15146 {
15147 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15148 p += 4;
15149 }
15150 else
15151 {
15152 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15153 p += 4;
15154 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15155 p);
15156 p += 4;
15157 }
15158 }
15159 bfd_put_32 (htab->glink->owner,
15160 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15161 indx++;
15162 p += 4;
15163 }
15164 }
15165
15166 if (htab->tga_group != NULL)
15167 {
15168 htab->tga_group->lr_restore = 23 * 4;
15169 htab->tga_group->stub_sec->size = 24 * 4;
15170 if (!emit_tga_desc (htab))
15171 return false;
15172 if (htab->glink_eh_frame != NULL
15173 && htab->glink_eh_frame->size != 0)
15174 {
15175 size_t align = 4;
15176
15177 p = htab->glink_eh_frame->contents;
15178 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15179 p += 17;
15180 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15181 }
15182 }
15183
15184 /* Build .glink global entry stubs, and PLT relocs for globals. */
15185 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15186
15187 if (!write_plt_relocs_for_local_syms (info))
15188 return false;
15189
15190 if (htab->brlt != NULL && htab->brlt->size != 0)
15191 {
15192 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15193 htab->brlt->size);
15194 if (htab->brlt->contents == NULL)
15195 return false;
15196 htab->brlt->alloced = 1;
15197 }
15198 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15199 {
15200 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15201 htab->relbrlt->size);
15202 if (htab->relbrlt->contents == NULL)
15203 return false;
15204 htab->relbrlt->alloced = 1;
15205 }
15206
15207 /* Build the stubs as directed by the stub hash table. */
15208 htab->stub_id = 0;
15209 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15210
15211 for (group = htab->group; group != NULL; group = group->next)
15212 if (group->needs_save_res)
15213 group->stub_sec->size += htab->sfpr->size;
15214
15215 if (htab->relbrlt != NULL)
15216 htab->relbrlt->reloc_count = 0;
15217
15218 if (htab->params->plt_stub_align != 0)
15219 for (group = htab->group; group != NULL; group = group->next)
15220 if ((stub_sec = group->stub_sec) != NULL)
15221 {
15222 int align = abs (htab->params->plt_stub_align);
15223 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15224 }
15225
15226 for (group = htab->group; group != NULL; group = group->next)
15227 if (group->needs_save_res)
15228 {
15229 stub_sec = group->stub_sec;
15230 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15231 htab->sfpr->contents, htab->sfpr->size);
15232 if (htab->params->emit_stub_syms)
15233 {
15234 unsigned int i;
15235
15236 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15237 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15238 return false;
15239 }
15240 }
15241
15242 if (htab->glink_eh_frame != NULL
15243 && htab->glink_eh_frame->size != 0)
15244 {
15245 bfd_vma val;
15246 size_t align = 4;
15247
15248 p = htab->glink_eh_frame->contents;
15249 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15250
15251 for (group = htab->group; group != NULL; group = group->next)
15252 if (group->eh_size != 0)
15253 {
15254 /* Offset to stub section. */
15255 val = (group->stub_sec->output_section->vma
15256 + group->stub_sec->output_offset);
15257 val -= (htab->glink_eh_frame->output_section->vma
15258 + htab->glink_eh_frame->output_offset
15259 + (p + 8 - htab->glink_eh_frame->contents));
15260 if (val + 0x80000000 > 0xffffffff)
15261 {
15262 _bfd_error_handler
15263 (_("%s offset too large for .eh_frame sdata4 encoding"),
15264 group->stub_sec->name);
15265 return false;
15266 }
15267 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15268 p += (group->eh_size + 17 + 3) & -4;
15269 }
15270 if (htab->glink != NULL && htab->glink->size != 0)
15271 {
15272 /* Offset to .glink. */
15273 val = (htab->glink->output_section->vma
15274 + htab->glink->output_offset
15275 + 8);
15276 val -= (htab->glink_eh_frame->output_section->vma
15277 + htab->glink_eh_frame->output_offset
15278 + (p + 8 - htab->glink_eh_frame->contents));
15279 if (val + 0x80000000 > 0xffffffff)
15280 {
15281 _bfd_error_handler
15282 (_("%s offset too large for .eh_frame sdata4 encoding"),
15283 htab->glink->name);
15284 return false;
15285 }
15286 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15287 p += (24 + align - 1) & -align;
15288 }
15289 }
15290
15291 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15292 {
15293 htab->elf.srelrdyn->contents
15294 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15295 if (htab->elf.srelrdyn->contents == NULL)
15296 return false;
15297 htab->elf.srelrdyn->alloced = 1;
15298
15299 bfd_vma *relr_addr = sort_relr (htab);
15300 if (htab->relr_count != 0 && relr_addr == NULL)
15301 return false;
15302
15303 size_t i = 0;
15304 bfd_byte *loc = htab->elf.srelrdyn->contents;
15305 while (i < htab->relr_count)
15306 {
15307 bfd_vma base = relr_addr[i];
15308 BFD_ASSERT ((base & ((1 << RELR_ALIGN) - 1)) == 0);
15309 bfd_put_64 (htab->elf.dynobj, base, loc);
15310 loc += 8;
15311 i++;
15312 while (i < htab->relr_count
15313 && relr_addr[i] == base)
15314 {
15315 htab->stub_error = true;
15316 i++;
15317 }
15318 base += 8;
15319 while (1)
15320 {
15321 bfd_vma bits = 0;
15322 while (i < htab->relr_count
15323 && relr_addr[i] - base < 63 * 8
15324 && (relr_addr[i] - base) % 8 == 0)
15325 {
15326 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15327 i++;
15328 }
15329 if (bits == 0)
15330 break;
15331 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15332 loc += 8;
15333 base += 63 * 8;
15334 }
15335 }
15336 free (relr_addr);
15337 /* Pad any excess with 1's, a do-nothing encoding. */
15338 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15339 < htab->elf.srelrdyn->size)
15340 {
15341 bfd_put_64 (htab->elf.dynobj, 1, loc);
15342 loc += 8;
15343 }
15344 }
15345 free (htab->relr);
15346 htab->relr = NULL;
15347
15348 for (group = htab->group; group != NULL; group = group->next)
15349 if ((stub_sec = group->stub_sec) != NULL)
15350 {
15351 stub_sec_count += 1;
15352 if (stub_sec->rawsize != stub_sec->size
15353 && (htab->stub_iteration <= STUB_SHRINK_ITER
15354 || stub_sec->rawsize < stub_sec->size))
15355 break;
15356 }
15357
15358 if (group != NULL)
15359 htab->stub_error = true;
15360
15361 if (htab->stub_error)
15362 {
15363 _bfd_error_handler (_("stubs don't match calculated size"));
15364 return false;
15365 }
15366
15367 if (stats != NULL)
15368 {
15369 char *groupmsg;
15370 if (asprintf (&groupmsg,
15371 ngettext ("linker stubs in %u group",
15372 "linker stubs in %u groups",
15373 stub_sec_count),
15374 stub_sec_count) < 0)
15375 *stats = NULL;
15376 else
15377 {
15378 if (asprintf (stats, _("%s, iter %u\n"
15379 " branch %lu\n"
15380 " long branch %lu\n"
15381 " plt call %lu\n"
15382 " global entry %lu"),
15383 groupmsg, htab->stub_iteration,
15384 htab->stub_count[ppc_stub_long_branch - 1],
15385 htab->stub_count[ppc_stub_plt_branch - 1],
15386 htab->stub_count[ppc_stub_plt_call - 1],
15387 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15388 *stats = NULL;
15389 free (groupmsg);
15390 }
15391 }
15392 return true;
15393}
15394
15395/* What to do when ld finds relocations against symbols defined in
15396 discarded sections. */
15397
15398static unsigned int
15399ppc64_elf_action_discarded (asection *sec)
15400{
15401 if (strcmp (".opd", sec->name) == 0)
15402 return 0;
15403
15404 if (strcmp (".toc", sec->name) == 0)
15405 return 0;
15406
15407 if (strcmp (".toc1", sec->name) == 0)
15408 return 0;
15409
15410 return _bfd_elf_default_action_discarded (sec);
15411}
15412
15413/* These are the dynamic relocations supported by glibc. */
15414
15415static bool
15416ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15417{
15418 switch (r_type)
15419 {
15420 case R_PPC64_RELATIVE:
15421 case R_PPC64_NONE:
15422 case R_PPC64_ADDR64:
15423 case R_PPC64_GLOB_DAT:
15424 case R_PPC64_IRELATIVE:
15425 case R_PPC64_JMP_IREL:
15426 case R_PPC64_JMP_SLOT:
15427 case R_PPC64_DTPMOD64:
15428 case R_PPC64_DTPREL64:
15429 case R_PPC64_TPREL64:
15430 case R_PPC64_TPREL16_LO_DS:
15431 case R_PPC64_TPREL16_DS:
15432 case R_PPC64_TPREL16:
15433 case R_PPC64_TPREL16_LO:
15434 case R_PPC64_TPREL16_HI:
15435 case R_PPC64_TPREL16_HIGH:
15436 case R_PPC64_TPREL16_HA:
15437 case R_PPC64_TPREL16_HIGHA:
15438 case R_PPC64_TPREL16_HIGHER:
15439 case R_PPC64_TPREL16_HIGHEST:
15440 case R_PPC64_TPREL16_HIGHERA:
15441 case R_PPC64_TPREL16_HIGHESTA:
15442 case R_PPC64_ADDR16_LO_DS:
15443 case R_PPC64_ADDR16_LO:
15444 case R_PPC64_ADDR16_HI:
15445 case R_PPC64_ADDR16_HIGH:
15446 case R_PPC64_ADDR16_HA:
15447 case R_PPC64_ADDR16_HIGHA:
15448 case R_PPC64_REL30:
15449 case R_PPC64_COPY:
15450 case R_PPC64_UADDR64:
15451 case R_PPC64_UADDR32:
15452 case R_PPC64_ADDR32:
15453 case R_PPC64_ADDR24:
15454 case R_PPC64_ADDR16:
15455 case R_PPC64_UADDR16:
15456 case R_PPC64_ADDR16_DS:
15457 case R_PPC64_ADDR16_HIGHER:
15458 case R_PPC64_ADDR16_HIGHEST:
15459 case R_PPC64_ADDR16_HIGHERA:
15460 case R_PPC64_ADDR16_HIGHESTA:
15461 case R_PPC64_ADDR14:
15462 case R_PPC64_ADDR14_BRTAKEN:
15463 case R_PPC64_ADDR14_BRNTAKEN:
15464 case R_PPC64_REL32:
15465 case R_PPC64_REL64:
15466 return true;
15467
15468 default:
15469 return false;
15470 }
15471}
15472
15473/* The RELOCATE_SECTION function is called by the ELF backend linker
15474 to handle the relocations for a section.
15475
15476 The relocs are always passed as Rela structures; if the section
15477 actually uses Rel structures, the r_addend field will always be
15478 zero.
15479
15480 This function is responsible for adjust the section contents as
15481 necessary, and (if using Rela relocs and generating a
15482 relocatable output file) adjusting the reloc addend as
15483 necessary.
15484
15485 This function does not have to worry about setting the reloc
15486 address or the reloc symbol index.
15487
15488 LOCAL_SYMS is a pointer to the swapped in local symbols.
15489
15490 LOCAL_SECTIONS is an array giving the section in the input file
15491 corresponding to the st_shndx field of each local symbol.
15492
15493 The global hash table entry for the global symbols can be found
15494 via elf_sym_hashes (input_bfd).
15495
15496 When generating relocatable output, this function must handle
15497 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15498 going to be the section symbol corresponding to the output
15499 section, which means that the addend must be adjusted
15500 accordingly. */
15501
15502static int
15503ppc64_elf_relocate_section (bfd *output_bfd,
15504 struct bfd_link_info *info,
15505 bfd *input_bfd,
15506 asection *input_section,
15507 bfd_byte *contents,
15508 Elf_Internal_Rela *relocs,
15509 Elf_Internal_Sym *local_syms,
15510 asection **local_sections)
15511{
15512 struct ppc_link_hash_table *htab;
15513 Elf_Internal_Shdr *symtab_hdr;
15514 struct elf_link_hash_entry **sym_hashes;
15515 Elf_Internal_Rela *rel;
15516 Elf_Internal_Rela *wrel;
15517 Elf_Internal_Rela *relend;
15518 Elf_Internal_Rela outrel;
15519 bfd_byte *loc;
15520 struct got_entry **local_got_ents;
15521 bfd_vma TOCstart;
15522 bool ret = true;
15523 bool is_opd;
15524 /* Assume 'at' branch hints. */
15525 bool is_isa_v2 = true;
15526 bool warned_dynamic = false;
15527 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15528
15529 /* Initialize howto table if needed. */
15530 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15531 ppc_howto_init ();
15532
15533 htab = ppc_hash_table (info);
15534 if (htab == NULL)
15535 return false;
15536
15537 /* Don't relocate stub sections. */
15538 if (input_section->owner == htab->params->stub_bfd)
15539 return true;
15540
15541 if (!is_ppc64_elf (input_bfd))
15542 {
15543 bfd_set_error (bfd_error_wrong_format);
15544 return false;
15545 }
15546
15547 local_got_ents = elf_local_got_ents (input_bfd);
15548 TOCstart = elf_gp (output_bfd);
15549 symtab_hdr = &elf_symtab_hdr (input_bfd);
15550 sym_hashes = elf_sym_hashes (input_bfd);
15551 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15552
15553 rel = wrel = relocs;
15554 relend = relocs + input_section->reloc_count;
15555 for (; rel < relend; wrel++, rel++)
15556 {
15557 enum elf_ppc64_reloc_type r_type;
15558 bfd_vma addend;
15559 bfd_reloc_status_type r;
15560 Elf_Internal_Sym *sym;
15561 asection *sec;
15562 struct elf_link_hash_entry *h_elf;
15563 struct ppc_link_hash_entry *h;
15564 struct ppc_link_hash_entry *fdh;
15565 const char *sym_name;
15566 unsigned long r_symndx, toc_symndx;
15567 bfd_vma toc_addend;
15568 unsigned char tls_mask, tls_gd, tls_type;
15569 unsigned char sym_type;
15570 bfd_vma relocation;
15571 bool unresolved_reloc, save_unresolved_reloc;
15572 bool warned;
15573 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15574 unsigned int insn;
15575 unsigned int mask;
15576 struct ppc_stub_hash_entry *stub_entry;
15577 bfd_vma max_br_offset;
15578 bfd_vma from;
15579 Elf_Internal_Rela orig_rel;
15580 reloc_howto_type *howto;
15581 struct reloc_howto_struct alt_howto;
15582 uint64_t pinsn;
15583 bfd_vma offset;
15584
15585 again:
15586 orig_rel = *rel;
15587
15588 r_type = ELF64_R_TYPE (rel->r_info);
15589 r_symndx = ELF64_R_SYM (rel->r_info);
15590
15591 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15592 symbol of the previous ADDR64 reloc. The symbol gives us the
15593 proper TOC base to use. */
15594 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15595 && wrel != relocs
15596 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15597 && is_opd)
15598 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15599
15600 sym = NULL;
15601 sec = NULL;
15602 h_elf = NULL;
15603 sym_name = NULL;
15604 unresolved_reloc = false;
15605 warned = false;
15606
15607 if (r_symndx < symtab_hdr->sh_info)
15608 {
15609 /* It's a local symbol. */
15610 struct _opd_sec_data *opd;
15611
15612 sym = local_syms + r_symndx;
15613 sec = local_sections[r_symndx];
15614 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15615 sym_type = ELF64_ST_TYPE (sym->st_info);
15616 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15617 opd = get_opd_info (sec);
15618 if (opd != NULL && opd->adjust != NULL)
15619 {
15620 long adjust = opd->adjust[OPD_NDX (sym->st_value
15621 + rel->r_addend)];
15622 if (adjust == -1)
15623 relocation = 0;
15624 else
15625 {
15626 /* If this is a relocation against the opd section sym
15627 and we have edited .opd, adjust the reloc addend so
15628 that ld -r and ld --emit-relocs output is correct.
15629 If it is a reloc against some other .opd symbol,
15630 then the symbol value will be adjusted later. */
15631 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15632 rel->r_addend += adjust;
15633 else
15634 relocation += adjust;
15635 }
15636 }
15637 }
15638 else
15639 {
15640 bool ignored;
15641
15642 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15643 r_symndx, symtab_hdr, sym_hashes,
15644 h_elf, sec, relocation,
15645 unresolved_reloc, warned, ignored);
15646 sym_name = h_elf->root.root.string;
15647 sym_type = h_elf->type;
15648 if (sec != NULL
15649 && sec->owner == output_bfd
15650 && strcmp (sec->name, ".opd") == 0)
15651 {
15652 /* This is a symbol defined in a linker script. All
15653 such are defined in output sections, even those
15654 defined by simple assignment from a symbol defined in
15655 an input section. Transfer the symbol to an
15656 appropriate input .opd section, so that a branch to
15657 this symbol will be mapped to the location specified
15658 by the opd entry. */
15659 struct bfd_link_order *lo;
15660 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15661 if (lo->type == bfd_indirect_link_order)
15662 {
15663 asection *isec = lo->u.indirect.section;
15664 if (h_elf->root.u.def.value >= isec->output_offset
15665 && h_elf->root.u.def.value < (isec->output_offset
15666 + isec->size))
15667 {
15668 h_elf->root.u.def.value -= isec->output_offset;
15669 h_elf->root.u.def.section = isec;
15670 sec = isec;
15671 break;
15672 }
15673 }
15674 }
15675 }
15676 h = ppc_elf_hash_entry (h_elf);
15677
15678 if (sec != NULL && discarded_section (sec))
15679 {
15680 if (r_type < ARRAY_SIZE (ppc64_elf_howto_table)
15681 && ppc64_elf_howto_table[r_type] != NULL)
15682 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15683 input_bfd, input_section,
15684 contents, rel->r_offset);
15685 wrel->r_offset = rel->r_offset;
15686 wrel->r_info = 0;
15687 wrel->r_addend = 0;
15688
15689 /* For ld -r, remove relocations in debug sections against
15690 symbols defined in discarded sections. Not done for
15691 non-debug to preserve relocs in .eh_frame which the
15692 eh_frame editing code expects to be present. */
15693 if (bfd_link_relocatable (info)
15694 && (input_section->flags & SEC_DEBUGGING))
15695 wrel--;
15696
15697 continue;
15698 }
15699
15700 if (bfd_link_relocatable (info))
15701 goto copy_reloc;
15702
15703 if (h != NULL && &h->elf == htab->elf.hgot)
15704 {
15705 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15706 sec = bfd_abs_section_ptr;
15707 unresolved_reloc = false;
15708 }
15709
15710 /* TLS optimizations. Replace instruction sequences and relocs
15711 based on information we collected in tls_optimize. We edit
15712 RELOCS so that --emit-relocs will output something sensible
15713 for the final instruction stream. */
15714 tls_mask = 0;
15715 tls_gd = 0;
15716 toc_symndx = 0;
15717 if (h != NULL)
15718 tls_mask = h->tls_mask;
15719 else if (local_got_ents != NULL)
15720 {
15721 struct plt_entry **local_plt = (struct plt_entry **)
15722 (local_got_ents + symtab_hdr->sh_info);
15723 unsigned char *lgot_masks = (unsigned char *)
15724 (local_plt + symtab_hdr->sh_info);
15725 tls_mask = lgot_masks[r_symndx];
15726 }
15727 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15728 && (r_type == R_PPC64_TLS
15729 || r_type == R_PPC64_TLSGD
15730 || r_type == R_PPC64_TLSLD))
15731 {
15732 /* Check for toc tls entries. */
15733 unsigned char *toc_tls;
15734
15735 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15736 &local_syms, rel, input_bfd))
15737 return false;
15738
15739 if (toc_tls)
15740 tls_mask = *toc_tls;
15741 }
15742
15743 /* Check that tls relocs are used with tls syms, and non-tls
15744 relocs are used with non-tls syms. */
15745 if (r_symndx != STN_UNDEF
15746 && r_type != R_PPC64_NONE
15747 && r_type < ARRAY_SIZE (ppc64_elf_howto_table)
15748 && ppc64_elf_howto_table[r_type] != NULL
15749 && (h == NULL
15750 || h->elf.root.type == bfd_link_hash_defined
15751 || h->elf.root.type == bfd_link_hash_defweak)
15752 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15753 {
15754 if ((tls_mask & TLS_TLS) != 0
15755 && (r_type == R_PPC64_TLS
15756 || r_type == R_PPC64_TLSGD
15757 || r_type == R_PPC64_TLSLD))
15758 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15759 ;
15760 else
15761 info->callbacks->einfo
15762 (!IS_PPC64_TLS_RELOC (r_type)
15763 /* xgettext:c-format */
15764 ? _("%H: %s used with TLS symbol `%pT'\n")
15765 /* xgettext:c-format */
15766 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15767 input_bfd, input_section, rel->r_offset,
15768 ppc64_elf_howto_table[r_type]->name,
15769 sym_name);
15770 }
15771
15772 /* Ensure reloc mapping code below stays sane. */
15773 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15774 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15775 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15776 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15777 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15778 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15779 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15780 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15781 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15782 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15783 abort ();
15784
15785 switch (r_type)
15786 {
15787 default:
15788 break;
15789
15790 case R_PPC64_LO_DS_OPT:
15791 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15792 {
15793 insn = bfd_get_32 (input_bfd,
15794 contents + rel->r_offset - d_offset);
15795 if ((insn & (0x3fu << 26)) != 58u << 26)
15796 abort ();
15797 insn += (14u << 26) - (58u << 26);
15798 bfd_put_32 (input_bfd, insn,
15799 contents + rel->r_offset - d_offset);
15800 r_type = R_PPC64_TOC16_LO;
15801 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15802 }
15803 break;
15804
15805 case R_PPC64_TOC16:
15806 case R_PPC64_TOC16_LO:
15807 case R_PPC64_TOC16_DS:
15808 case R_PPC64_TOC16_LO_DS:
15809 {
15810 /* Check for toc tls entries. */
15811 unsigned char *toc_tls;
15812 int retval;
15813
15814 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15815 &local_syms, rel, input_bfd);
15816 if (retval == 0)
15817 return false;
15818
15819 if (toc_tls)
15820 {
15821 tls_mask = *toc_tls;
15822 if (r_type == R_PPC64_TOC16_DS
15823 || r_type == R_PPC64_TOC16_LO_DS)
15824 {
15825 if ((tls_mask & TLS_TLS) != 0
15826 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15827 goto toctprel;
15828 }
15829 else
15830 {
15831 /* If we found a GD reloc pair, then we might be
15832 doing a GD->IE transition. */
15833 if (retval == 2)
15834 {
15835 tls_gd = TLS_GDIE;
15836 if ((tls_mask & TLS_TLS) != 0
15837 && (tls_mask & TLS_GD) == 0)
15838 goto tls_ldgd_opt;
15839 }
15840 else if (retval == 3)
15841 {
15842 if ((tls_mask & TLS_TLS) != 0
15843 && (tls_mask & TLS_LD) == 0)
15844 goto tls_ldgd_opt;
15845 }
15846 }
15847 }
15848 }
15849 break;
15850
15851 case R_PPC64_GOT_TPREL16_HI:
15852 case R_PPC64_GOT_TPREL16_HA:
15853 if ((tls_mask & TLS_TLS) != 0
15854 && (tls_mask & TLS_TPREL) == 0
15855 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15856 {
15857 rel->r_offset -= d_offset;
15858 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15859 r_type = R_PPC64_NONE;
15860 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15861 }
15862 break;
15863
15864 case R_PPC64_GOT_TPREL16_DS:
15865 case R_PPC64_GOT_TPREL16_LO_DS:
15866 if ((tls_mask & TLS_TLS) != 0
15867 && (tls_mask & TLS_TPREL) == 0
15868 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15869 {
15870 toctprel:
15871 insn = bfd_get_32 (input_bfd,
15872 contents + rel->r_offset - d_offset);
15873 insn &= 31 << 21;
15874 insn |= 0x3c0d0000; /* addis 0,13,0 */
15875 bfd_put_32 (input_bfd, insn,
15876 contents + rel->r_offset - d_offset);
15877 r_type = R_PPC64_TPREL16_HA;
15878 if (toc_symndx != 0)
15879 {
15880 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15881 rel->r_addend = toc_addend;
15882 /* We changed the symbol. Start over in order to
15883 get h, sym, sec etc. right. */
15884 goto again;
15885 }
15886 else
15887 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15888 }
15889 break;
15890
15891 case R_PPC64_GOT_TPREL_PCREL34:
15892 if ((tls_mask & TLS_TLS) != 0
15893 && (tls_mask & TLS_TPREL) == 0
15894 && offset_in_range (input_section, rel->r_offset, 8))
15895 {
15896 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15897 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15898 pinsn <<= 32;
15899 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15900 pinsn += ((2ULL << 56) + (-1ULL << 52)
15901 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15902 bfd_put_32 (input_bfd, pinsn >> 32,
15903 contents + rel->r_offset);
15904 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15905 contents + rel->r_offset + 4);
15906 r_type = R_PPC64_TPREL34;
15907 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15908 }
15909 break;
15910
15911 case R_PPC64_TLS:
15912 if ((tls_mask & TLS_TLS) != 0
15913 && (tls_mask & TLS_TPREL) == 0
15914 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15915 {
15916 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15917 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15918 if (insn == 0)
15919 break;
15920 if ((rel->r_offset & 3) == 0)
15921 {
15922 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15923 /* Was PPC64_TLS which sits on insn boundary, now
15924 PPC64_TPREL16_LO which is at low-order half-word. */
15925 rel->r_offset += d_offset;
15926 r_type = R_PPC64_TPREL16_LO;
15927 if (toc_symndx != 0)
15928 {
15929 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15930 rel->r_addend = toc_addend;
15931 /* We changed the symbol. Start over in order to
15932 get h, sym, sec etc. right. */
15933 goto again;
15934 }
15935 else
15936 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15937 }
15938 else if ((rel->r_offset & 3) == 1)
15939 {
15940 /* For pcrel IE to LE we already have the full
15941 offset and thus don't need an addi here. A nop
15942 or mr will do. */
15943 if ((insn & (0x3fu << 26)) == 14 << 26)
15944 {
15945 /* Extract regs from addi rt,ra,si. */
15946 unsigned int rt = (insn >> 21) & 0x1f;
15947 unsigned int ra = (insn >> 16) & 0x1f;
15948 if (rt == ra)
15949 insn = NOP;
15950 else
15951 {
15952 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15953 insn = (rt << 16) | (ra << 21) | (ra << 11);
15954 insn |= (31u << 26) | (444u << 1);
15955 }
15956 }
15957 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15958 }
15959 }
15960 break;
15961
15962 case R_PPC64_GOT_TLSGD16_HI:
15963 case R_PPC64_GOT_TLSGD16_HA:
15964 tls_gd = TLS_GDIE;
15965 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15966 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15967 goto tls_gdld_hi;
15968 break;
15969
15970 case R_PPC64_GOT_TLSLD16_HI:
15971 case R_PPC64_GOT_TLSLD16_HA:
15972 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15973 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15974 {
15975 tls_gdld_hi:
15976 if ((tls_mask & tls_gd) != 0)
15977 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15978 + R_PPC64_GOT_TPREL16_DS);
15979 else
15980 {
15981 rel->r_offset -= d_offset;
15982 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15983 r_type = R_PPC64_NONE;
15984 }
15985 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15986 }
15987 break;
15988
15989 case R_PPC64_GOT_TLSGD16:
15990 case R_PPC64_GOT_TLSGD16_LO:
15991 tls_gd = TLS_GDIE;
15992 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15993 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15994 goto tls_ldgd_opt;
15995 break;
15996
15997 case R_PPC64_GOT_TLSLD16:
15998 case R_PPC64_GOT_TLSLD16_LO:
15999 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16000 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16001 {
16002 unsigned int insn1, insn2;
16003
16004 tls_ldgd_opt:
16005 offset = (bfd_vma) -1;
16006 /* If not using the newer R_PPC64_TLSGD/LD to mark
16007 __tls_get_addr calls, we must trust that the call
16008 stays with its arg setup insns, ie. that the next
16009 reloc is the __tls_get_addr call associated with
16010 the current reloc. Edit both insns. */
16011 if (input_section->nomark_tls_get_addr
16012 && rel + 1 < relend
16013 && branch_reloc_hash_match (input_bfd, rel + 1,
16014 htab->tls_get_addr_fd,
16015 htab->tga_desc_fd,
16016 htab->tls_get_addr,
16017 htab->tga_desc))
16018 offset = rel[1].r_offset;
16019 /* We read the low GOT_TLS (or TOC16) insn because we
16020 need to keep the destination reg. It may be
16021 something other than the usual r3, and moved to r3
16022 before the call by intervening code. */
16023 insn1 = bfd_get_32 (input_bfd,
16024 contents + rel->r_offset - d_offset);
16025 if ((tls_mask & tls_gd) != 0)
16026 {
16027 /* IE */
16028 insn1 &= (0x1f << 21) | (0x1f << 16);
16029 insn1 |= 58u << 26; /* ld */
16030 insn2 = 0x7c636a14; /* add 3,3,13 */
16031 if (offset != (bfd_vma) -1)
16032 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16033 if (r_type == R_PPC64_TOC16
16034 || r_type == R_PPC64_TOC16_LO)
16035 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
16036 else
16037 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
16038 + R_PPC64_GOT_TPREL16_DS);
16039 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16040 }
16041 else
16042 {
16043 /* LE */
16044 insn1 &= 0x1f << 21;
16045 insn1 |= 0x3c0d0000; /* addis r,13,0 */
16046 insn2 = 0x38630000; /* addi 3,3,0 */
16047 if (tls_gd == 0)
16048 {
16049 /* Was an LD reloc. */
16050 r_symndx = STN_UNDEF;
16051 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16052 }
16053 else if (toc_symndx != 0)
16054 {
16055 r_symndx = toc_symndx;
16056 rel->r_addend = toc_addend;
16057 }
16058 r_type = R_PPC64_TPREL16_HA;
16059 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16060 if (offset != (bfd_vma) -1)
16061 {
16062 rel[1].r_info = ELF64_R_INFO (r_symndx,
16063 R_PPC64_TPREL16_LO);
16064 rel[1].r_offset = offset + d_offset;
16065 rel[1].r_addend = rel->r_addend;
16066 }
16067 }
16068 bfd_put_32 (input_bfd, insn1,
16069 contents + rel->r_offset - d_offset);
16070 if (offset != (bfd_vma) -1
16071 && offset_in_range (input_section, offset, 4))
16072 {
16073 bfd_put_32 (input_bfd, insn2, contents + offset);
16074 if (offset_in_range (input_section, offset + 4, 4))
16075 {
16076 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16077 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16078 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16079 }
16080 }
16081 if ((tls_mask & tls_gd) == 0
16082 && (tls_gd == 0 || toc_symndx != 0))
16083 {
16084 /* We changed the symbol. Start over in order
16085 to get h, sym, sec etc. right. */
16086 goto again;
16087 }
16088 }
16089 break;
16090
16091 case R_PPC64_GOT_TLSGD_PCREL34:
16092 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16093 && offset_in_range (input_section, rel->r_offset, 8))
16094 {
16095 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16096 pinsn <<= 32;
16097 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16098 if ((tls_mask & TLS_GDIE) != 0)
16099 {
16100 /* IE, pla -> pld */
16101 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16102 r_type = R_PPC64_GOT_TPREL_PCREL34;
16103 }
16104 else
16105 {
16106 /* LE, pla pcrel -> paddi r13 */
16107 pinsn += (-1ULL << 52) + (13ULL << 16);
16108 r_type = R_PPC64_TPREL34;
16109 }
16110 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16111 bfd_put_32 (input_bfd, pinsn >> 32,
16112 contents + rel->r_offset);
16113 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16114 contents + rel->r_offset + 4);
16115 }
16116 break;
16117
16118 case R_PPC64_GOT_TLSLD_PCREL34:
16119 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16120 && offset_in_range (input_section, rel->r_offset, 8))
16121 {
16122 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16123 pinsn <<= 32;
16124 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16125 pinsn += (-1ULL << 52) + (13ULL << 16);
16126 bfd_put_32 (input_bfd, pinsn >> 32,
16127 contents + rel->r_offset);
16128 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16129 contents + rel->r_offset + 4);
16130 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16131 r_symndx = STN_UNDEF;
16132 r_type = R_PPC64_TPREL34;
16133 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16134 goto again;
16135 }
16136 break;
16137
16138 case R_PPC64_TLSGD:
16139 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16140 && rel + 1 < relend
16141 && offset_in_range (input_section, rel->r_offset,
16142 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16143 ? 8 : 4))
16144 {
16145 unsigned int insn2;
16146 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16147
16148 offset = rel->r_offset;
16149 if (is_plt_seq_reloc (r_type1))
16150 {
16151 bfd_put_32 (output_bfd, NOP, contents + offset);
16152 if (r_type1 == R_PPC64_PLT_PCREL34
16153 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16154 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16155 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16156 break;
16157 }
16158
16159 if (r_type1 == R_PPC64_PLTCALL)
16160 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16161
16162 if ((tls_mask & TLS_GDIE) != 0)
16163 {
16164 /* IE */
16165 r_type = R_PPC64_NONE;
16166 insn2 = 0x7c636a14; /* add 3,3,13 */
16167 }
16168 else
16169 {
16170 /* LE */
16171 if (toc_symndx != 0)
16172 {
16173 r_symndx = toc_symndx;
16174 rel->r_addend = toc_addend;
16175 }
16176 if (r_type1 == R_PPC64_REL24_NOTOC
16177 || r_type1 == R_PPC64_REL24_P9NOTOC
16178 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16179 {
16180 r_type = R_PPC64_NONE;
16181 insn2 = NOP;
16182 }
16183 else
16184 {
16185 rel->r_offset = offset + d_offset;
16186 r_type = R_PPC64_TPREL16_LO;
16187 insn2 = 0x38630000; /* addi 3,3,0 */
16188 }
16189 }
16190 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16191 /* Zap the reloc on the _tls_get_addr call too. */
16192 BFD_ASSERT (offset == rel[1].r_offset);
16193 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16194 bfd_put_32 (input_bfd, insn2, contents + offset);
16195 if ((tls_mask & TLS_GDIE) == 0
16196 && toc_symndx != 0
16197 && r_type != R_PPC64_NONE)
16198 goto again;
16199 }
16200 break;
16201
16202 case R_PPC64_TLSLD:
16203 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16204 && rel + 1 < relend
16205 && offset_in_range (input_section, rel->r_offset,
16206 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16207 ? 8 : 4))
16208 {
16209 unsigned int insn2;
16210 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16211
16212 offset = rel->r_offset;
16213 if (is_plt_seq_reloc (r_type1))
16214 {
16215 bfd_put_32 (output_bfd, NOP, contents + offset);
16216 if (r_type1 == R_PPC64_PLT_PCREL34
16217 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16218 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16219 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16220 break;
16221 }
16222
16223 if (r_type1 == R_PPC64_PLTCALL)
16224 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16225
16226 if (r_type1 == R_PPC64_REL24_NOTOC
16227 || r_type1 == R_PPC64_REL24_P9NOTOC
16228 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16229 {
16230 r_type = R_PPC64_NONE;
16231 insn2 = NOP;
16232 }
16233 else
16234 {
16235 rel->r_offset = offset + d_offset;
16236 r_symndx = STN_UNDEF;
16237 r_type = R_PPC64_TPREL16_LO;
16238 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16239 insn2 = 0x38630000; /* addi 3,3,0 */
16240 }
16241 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16242 /* Zap the reloc on the _tls_get_addr call too. */
16243 BFD_ASSERT (offset == rel[1].r_offset);
16244 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16245 bfd_put_32 (input_bfd, insn2, contents + offset);
16246 if (r_type != R_PPC64_NONE)
16247 goto again;
16248 }
16249 break;
16250
16251 case R_PPC64_DTPMOD64:
16252 if (rel + 1 < relend
16253 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16254 && rel[1].r_offset == rel->r_offset + 8)
16255 {
16256 if ((tls_mask & TLS_GD) == 0
16257 && offset_in_range (input_section, rel->r_offset, 8))
16258 {
16259 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16260 if ((tls_mask & TLS_GDIE) != 0)
16261 r_type = R_PPC64_TPREL64;
16262 else
16263 {
16264 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16265 r_type = R_PPC64_NONE;
16266 }
16267 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16268 }
16269 }
16270 else
16271 {
16272 if ((tls_mask & TLS_LD) == 0
16273 && offset_in_range (input_section, rel->r_offset, 8))
16274 {
16275 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16276 r_type = R_PPC64_NONE;
16277 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16278 }
16279 }
16280 break;
16281
16282 case R_PPC64_TPREL64:
16283 if ((tls_mask & TLS_TPREL) == 0)
16284 {
16285 r_type = R_PPC64_NONE;
16286 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16287 }
16288 break;
16289
16290 case R_PPC64_ENTRY:
16291 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16292 if (!bfd_link_pic (info)
16293 && !info->traditional_format
16294 && relocation + 0x80008000 <= 0xffffffff
16295 && offset_in_range (input_section, rel->r_offset, 8))
16296 {
16297 unsigned int insn1, insn2;
16298
16299 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16300 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16301 if ((insn1 & ~0xfffc) == LD_R2_0R12
16302 && insn2 == ADD_R2_R2_R12)
16303 {
16304 bfd_put_32 (input_bfd,
16305 LIS_R2 + PPC_HA (relocation),
16306 contents + rel->r_offset);
16307 bfd_put_32 (input_bfd,
16308 ADDI_R2_R2 + PPC_LO (relocation),
16309 contents + rel->r_offset + 4);
16310 }
16311 }
16312 else
16313 {
16314 relocation -= (rel->r_offset
16315 + input_section->output_offset
16316 + input_section->output_section->vma);
16317 if (relocation + 0x80008000 <= 0xffffffff
16318 && offset_in_range (input_section, rel->r_offset, 8))
16319 {
16320 unsigned int insn1, insn2;
16321
16322 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16323 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16324 if ((insn1 & ~0xfffc) == LD_R2_0R12
16325 && insn2 == ADD_R2_R2_R12)
16326 {
16327 bfd_put_32 (input_bfd,
16328 ADDIS_R2_R12 + PPC_HA (relocation),
16329 contents + rel->r_offset);
16330 bfd_put_32 (input_bfd,
16331 ADDI_R2_R2 + PPC_LO (relocation),
16332 contents + rel->r_offset + 4);
16333 }
16334 }
16335 }
16336 break;
16337
16338 case R_PPC64_REL16_HA:
16339 /* If we are generating a non-PIC executable, edit
16340 . 0: addis 2,12,.TOC.-0b@ha
16341 . addi 2,2,.TOC.-0b@l
16342 used by ELFv2 global entry points to set up r2, to
16343 . lis 2,.TOC.@ha
16344 . addi 2,2,.TOC.@l
16345 if .TOC. is in range. */
16346 if (!bfd_link_pic (info)
16347 && !info->traditional_format
16348 && !htab->opd_abi
16349 && rel->r_addend == d_offset
16350 && h != NULL && &h->elf == htab->elf.hgot
16351 && rel + 1 < relend
16352 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16353 && rel[1].r_offset == rel->r_offset + 4
16354 && rel[1].r_addend == rel->r_addend + 4
16355 && relocation + 0x80008000 <= 0xffffffff
16356 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16357 {
16358 unsigned int insn1, insn2;
16359 offset = rel->r_offset - d_offset;
16360 insn1 = bfd_get_32 (input_bfd, contents + offset);
16361 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16362 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16363 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16364 {
16365 r_type = R_PPC64_ADDR16_HA;
16366 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16367 rel->r_addend -= d_offset;
16368 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16369 rel[1].r_addend -= d_offset + 4;
16370 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16371 }
16372 }
16373 break;
16374 }
16375
16376 /* Handle other relocations that tweak non-addend part of insn. */
16377 insn = 0;
16378 max_br_offset = 1 << 25;
16379 addend = rel->r_addend;
16380 reloc_dest = DEST_NORMAL;
16381 switch (r_type)
16382 {
16383 default:
16384 break;
16385
16386 case R_PPC64_TOCSAVE:
16387 if (relocation + addend == (rel->r_offset
16388 + input_section->output_offset
16389 + input_section->output_section->vma)
16390 && tocsave_find (htab, NO_INSERT,
16391 &local_syms, rel, input_bfd)
16392 && offset_in_range (input_section, rel->r_offset, 4))
16393 {
16394 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16395 if (insn == NOP
16396 || insn == CROR_151515 || insn == CROR_313131)
16397 bfd_put_32 (input_bfd,
16398 STD_R2_0R1 + STK_TOC (htab),
16399 contents + rel->r_offset);
16400 }
16401 break;
16402
16403 /* Branch taken prediction relocations. */
16404 case R_PPC64_ADDR14_BRTAKEN:
16405 case R_PPC64_REL14_BRTAKEN:
16406 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16407 /* Fall through. */
16408
16409 /* Branch not taken prediction relocations. */
16410 case R_PPC64_ADDR14_BRNTAKEN:
16411 case R_PPC64_REL14_BRNTAKEN:
16412 if (!offset_in_range (input_section, rel->r_offset, 4))
16413 break;
16414 insn |= bfd_get_32 (input_bfd,
16415 contents + rel->r_offset) & ~(0x01 << 21);
16416 /* Fall through. */
16417
16418 case R_PPC64_REL14:
16419 max_br_offset = 1 << 15;
16420 /* Fall through. */
16421
16422 case R_PPC64_REL24:
16423 case R_PPC64_REL24_NOTOC:
16424 case R_PPC64_REL24_P9NOTOC:
16425 case R_PPC64_PLTCALL:
16426 case R_PPC64_PLTCALL_NOTOC:
16427 /* Calls to functions with a different TOC, such as calls to
16428 shared objects, need to alter the TOC pointer. This is
16429 done using a linkage stub. A REL24 branching to these
16430 linkage stubs needs to be followed by a nop, as the nop
16431 will be replaced with an instruction to restore the TOC
16432 base pointer. */
16433 fdh = h;
16434 if (h != NULL
16435 && h->oh != NULL
16436 && h->oh->is_func_descriptor)
16437 fdh = ppc_follow_link (h->oh);
16438 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16439 htab);
16440 if ((r_type == R_PPC64_PLTCALL
16441 || r_type == R_PPC64_PLTCALL_NOTOC)
16442 && stub_entry != NULL
16443 && stub_entry->type.main == ppc_stub_plt_call)
16444 stub_entry = NULL;
16445
16446 if (stub_entry != NULL
16447 && (stub_entry->type.main == ppc_stub_plt_call
16448 || stub_entry->type.r2save))
16449 {
16450 bool can_plt_call = false;
16451
16452 if (r_type == R_PPC64_REL24_NOTOC
16453 || r_type == R_PPC64_REL24_P9NOTOC)
16454 {
16455 /* NOTOC calls don't need to restore r2. */
16456 can_plt_call = true;
16457 }
16458 else if (stub_entry->type.main == ppc_stub_plt_call
16459 && !htab->opd_abi
16460 && htab->params->plt_localentry0 != 0
16461 && h != NULL
16462 && is_elfv2_localentry0 (&h->elf))
16463 {
16464 /* The function doesn't use or change r2. */
16465 can_plt_call = true;
16466 }
16467
16468 /* All of these stubs may modify r2, so there must be a
16469 branch and link followed by a nop. The nop is
16470 replaced by an insn to restore r2. */
16471 else if (offset_in_range (input_section, rel->r_offset, 8))
16472 {
16473 unsigned long br;
16474
16475 br = bfd_get_32 (input_bfd,
16476 contents + rel->r_offset);
16477 if ((br & 1) != 0)
16478 {
16479 unsigned long nop;
16480
16481 nop = bfd_get_32 (input_bfd,
16482 contents + rel->r_offset + 4);
16483 if (nop == LD_R2_0R1 + STK_TOC (htab))
16484 can_plt_call = true;
16485 else if (nop == NOP
16486 || nop == CROR_151515
16487 || nop == CROR_313131)
16488 {
16489 if (h != NULL
16490 && is_tls_get_addr (&h->elf, htab)
16491 && htab->params->tls_get_addr_opt)
16492 {
16493 /* Special stub used, leave nop alone. */
16494 }
16495 else
16496 bfd_put_32 (input_bfd,
16497 LD_R2_0R1 + STK_TOC (htab),
16498 contents + rel->r_offset + 4);
16499 can_plt_call = true;
16500 }
16501 }
16502 }
16503
16504 if (!can_plt_call && h != NULL)
16505 {
16506 const char *name = h->elf.root.root.string;
16507
16508 if (*name == '.')
16509 ++name;
16510
16511 if (startswith (name, "__libc_start_main")
16512 && (name[17] == 0 || name[17] == '@'))
16513 {
16514 /* Allow crt1 branch to go via a toc adjusting
16515 stub. Other calls that never return could do
16516 the same, if we could detect such. */
16517 can_plt_call = true;
16518 }
16519 }
16520
16521 if (!can_plt_call)
16522 {
16523 /* g++ as of 20130507 emits self-calls without a
16524 following nop. This is arguably wrong since we
16525 have conflicting information. On the one hand a
16526 global symbol and on the other a local call
16527 sequence, but don't error for this special case.
16528 It isn't possible to cheaply verify we have
16529 exactly such a call. Allow all calls to the same
16530 section. */
16531 asection *code_sec = sec;
16532
16533 if (get_opd_info (sec) != NULL)
16534 {
16535 bfd_vma off = (relocation + addend
16536 - sec->output_section->vma
16537 - sec->output_offset);
16538
16539 opd_entry_value (sec, off, &code_sec, NULL, false);
16540 }
16541 if (code_sec == input_section)
16542 can_plt_call = true;
16543 }
16544
16545 if (!can_plt_call)
16546 {
16547 if (stub_entry->type.main == ppc_stub_plt_call)
16548 info->callbacks->einfo
16549 /* xgettext:c-format */
16550 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16551 "(plt call stub)\n"),
16552 input_bfd, input_section, rel->r_offset, sym_name);
16553 else
16554 info->callbacks->einfo
16555 /* xgettext:c-format */
16556 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16557 "(toc save/adjust stub)\n"),
16558 input_bfd, input_section, rel->r_offset, sym_name);
16559
16560 bfd_set_error (bfd_error_bad_value);
16561 ret = false;
16562 }
16563
16564 if (can_plt_call
16565 && stub_entry->type.main == ppc_stub_plt_call)
16566 unresolved_reloc = false;
16567 }
16568
16569 if ((stub_entry == NULL
16570 || stub_entry->type.main == ppc_stub_long_branch
16571 || stub_entry->type.main == ppc_stub_plt_branch)
16572 && get_opd_info (sec) != NULL)
16573 {
16574 /* The branch destination is the value of the opd entry. */
16575 bfd_vma off = (relocation + addend
16576 - sec->output_section->vma
16577 - sec->output_offset);
16578 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16579 if (dest != (bfd_vma) -1)
16580 {
16581 relocation = dest;
16582 addend = 0;
16583 reloc_dest = DEST_OPD;
16584 }
16585 }
16586
16587 /* If the branch is out of reach we ought to have a long
16588 branch stub. */
16589 from = (rel->r_offset
16590 + input_section->output_offset
16591 + input_section->output_section->vma);
16592
16593 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16594 ? fdh->elf.other
16595 : sym->st_other);
16596
16597 if (stub_entry != NULL
16598 && (stub_entry->type.main == ppc_stub_long_branch
16599 || stub_entry->type.main == ppc_stub_plt_branch))
16600 {
16601 if (stub_entry->type.sub == ppc_stub_toc
16602 && !stub_entry->type.r2save
16603 && (r_type == R_PPC64_ADDR14_BRTAKEN
16604 || r_type == R_PPC64_ADDR14_BRNTAKEN
16605 || (relocation + addend - from + max_br_offset
16606 < 2 * max_br_offset)))
16607 /* Don't use the stub if this branch is in range. */
16608 stub_entry = NULL;
16609
16610 if (stub_entry != NULL
16611 && stub_entry->type.sub >= ppc_stub_notoc
16612 && ((r_type != R_PPC64_REL24_NOTOC
16613 && r_type != R_PPC64_REL24_P9NOTOC)
16614 || ((fdh ? fdh->elf.other : sym->st_other)
16615 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16616 && (relocation + addend - from + max_br_offset
16617 < 2 * max_br_offset))
16618 stub_entry = NULL;
16619
16620 if (stub_entry != NULL
16621 && stub_entry->type.r2save
16622 && (r_type == R_PPC64_REL24_NOTOC
16623 || r_type == R_PPC64_REL24_P9NOTOC)
16624 && (relocation + addend - from + max_br_offset
16625 < 2 * max_br_offset))
16626 stub_entry = NULL;
16627 }
16628
16629 if (stub_entry != NULL)
16630 {
16631 /* Munge up the value and addend so that we call the stub
16632 rather than the procedure directly. */
16633 asection *stub_sec = stub_entry->group->stub_sec;
16634
16635 if (stub_entry->type.main == ppc_stub_save_res)
16636 relocation += (stub_sec->output_offset
16637 + stub_sec->output_section->vma
16638 + stub_sec->size - htab->sfpr->size
16639 - htab->sfpr->output_offset
16640 - htab->sfpr->output_section->vma);
16641 else
16642 relocation = (stub_entry->stub_offset
16643 + stub_sec->output_offset
16644 + stub_sec->output_section->vma);
16645 addend = 0;
16646 reloc_dest = DEST_STUB;
16647
16648 if (((stub_entry->type.r2save
16649 && (r_type == R_PPC64_REL24_NOTOC
16650 || r_type == R_PPC64_REL24_P9NOTOC))
16651 || ((stub_entry->type.main == ppc_stub_plt_call
16652 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16653 && rel + 1 < relend
16654 && rel[1].r_offset == rel->r_offset + 4
16655 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16656 && !(stub_entry->type.main == ppc_stub_plt_call
16657 && htab->params->tls_get_addr_opt
16658 && h != NULL
16659 && is_tls_get_addr (&h->elf, htab)))
16660 {
16661 /* Skip over the r2 store at the start of the stub. */
16662 relocation += 4;
16663 }
16664
16665 if ((r_type == R_PPC64_REL24_NOTOC
16666 || r_type == R_PPC64_REL24_P9NOTOC)
16667 && stub_entry->type.main == ppc_stub_plt_call
16668 && stub_entry->type.sub >= ppc_stub_notoc)
16669 htab->notoc_plt = 1;
16670 }
16671
16672 if (insn != 0)
16673 {
16674 if (is_isa_v2)
16675 {
16676 /* Set 'a' bit. This is 0b00010 in BO field for branch
16677 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16678 for branch on CTR insns (BO == 1a00t or 1a01t). */
16679 if ((insn & (0x14 << 21)) == (0x04 << 21))
16680 insn |= 0x02 << 21;
16681 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16682 insn |= 0x08 << 21;
16683 else
16684 break;
16685 }
16686 else
16687 {
16688 /* Invert 'y' bit if not the default. */
16689 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16690 insn ^= 0x01 << 21;
16691 }
16692
16693 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16694 }
16695
16696 /* NOP out calls to undefined weak functions.
16697 We can thus call a weak function without first
16698 checking whether the function is defined. */
16699 else if (h != NULL
16700 && h->elf.root.type == bfd_link_hash_undefweak
16701 && h->elf.dynindx == -1
16702 && (r_type == R_PPC64_REL24
16703 || r_type == R_PPC64_REL24_NOTOC
16704 || r_type == R_PPC64_REL24_P9NOTOC)
16705 && relocation == 0
16706 && addend == 0
16707 && offset_in_range (input_section, rel->r_offset, 4))
16708 {
16709 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16710 goto copy_reloc;
16711 }
16712 break;
16713
16714 case R_PPC64_GOT16_DS:
16715 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16716 || (bfd_link_pic (info)
16717 && sec == bfd_abs_section_ptr)
16718 || !htab->do_toc_opt)
16719 break;
16720 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16721 if (relocation + addend - from + 0x8000 < 0x10000
16722 && sec != NULL
16723 && sec->output_section != NULL
16724 && !discarded_section (sec)
16725 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16726 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16727 {
16728 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16729 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16730 {
16731 insn += (14u << 26) - (58u << 26);
16732 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16733 r_type = R_PPC64_TOC16;
16734 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16735 }
16736 }
16737 break;
16738
16739 case R_PPC64_GOT16_LO_DS:
16740 case R_PPC64_GOT16_HA:
16741 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16742 || (bfd_link_pic (info)
16743 && sec == bfd_abs_section_ptr)
16744 || !htab->do_toc_opt)
16745 break;
16746 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16747 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16748 && sec != NULL
16749 && sec->output_section != NULL
16750 && !discarded_section (sec)
16751 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16752 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16753 {
16754 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16755 if (r_type == R_PPC64_GOT16_LO_DS
16756 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16757 {
16758 insn += (14u << 26) - (58u << 26);
16759 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16760 r_type = R_PPC64_TOC16_LO;
16761 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16762 }
16763 else if (r_type == R_PPC64_GOT16_HA
16764 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16765 {
16766 r_type = R_PPC64_TOC16_HA;
16767 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16768 }
16769 }
16770 break;
16771
16772 case R_PPC64_GOT_PCREL34:
16773 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16774 || (bfd_link_pic (info)
16775 && sec == bfd_abs_section_ptr)
16776 || !htab->do_toc_opt)
16777 break;
16778 from = (rel->r_offset
16779 + input_section->output_section->vma
16780 + input_section->output_offset);
16781 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16782 && sec != NULL
16783 && sec->output_section != NULL
16784 && !discarded_section (sec)
16785 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16786 && offset_in_range (input_section, rel->r_offset, 8)))
16787 break;
16788
16789 offset = rel->r_offset;
16790 pinsn = bfd_get_32 (input_bfd, contents + offset);
16791 pinsn <<= 32;
16792 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16793 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16794 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16795 break;
16796
16797 /* Replace with paddi. */
16798 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16799 r_type = R_PPC64_PCREL34;
16800 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16801 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16802 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16803 /* Fall through. */
16804
16805 case R_PPC64_PCREL34:
16806 if (!htab->params->no_pcrel_opt
16807 && rel + 1 < relend
16808 && rel[1].r_offset == rel->r_offset
16809 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16810 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16811 && offset_in_range (input_section, rel->r_offset, 8))
16812 {
16813 offset = rel->r_offset;
16814 pinsn = bfd_get_32 (input_bfd, contents + offset);
16815 pinsn <<= 32;
16816 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16817 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16818 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16819 | (14ULL << 26) /* paddi */))
16820 {
16821 bfd_vma off2 = rel[1].r_addend;
16822 if (off2 == 0)
16823 /* zero means next insn. */
16824 off2 = 8;
16825 off2 += offset;
16826 if (offset_in_range (input_section, off2, 4))
16827 {
16828 uint64_t pinsn2;
16829 bfd_signed_vma addend_off;
16830 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16831 pinsn2 <<= 32;
16832 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16833 {
16834 if (!offset_in_range (input_section, off2, 8))
16835 break;
16836 pinsn2 |= bfd_get_32 (input_bfd,
16837 contents + off2 + 4);
16838 }
16839 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16840 {
16841 addend += addend_off;
16842 rel->r_addend = addend;
16843 bfd_put_32 (input_bfd, pinsn >> 32,
16844 contents + offset);
16845 bfd_put_32 (input_bfd, pinsn,
16846 contents + offset + 4);
16847 bfd_put_32 (input_bfd, pinsn2 >> 32,
16848 contents + off2);
16849 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16850 bfd_put_32 (input_bfd, pinsn2,
16851 contents + off2 + 4);
16852 }
16853 }
16854 }
16855 }
16856 break;
16857 }
16858
16859 tls_type = 0;
16860 save_unresolved_reloc = unresolved_reloc;
16861 switch (r_type)
16862 {
16863 default:
16864 if (r_type < ARRAY_SIZE (ppc64_elf_howto_table)
16865 && ppc64_elf_howto_table[r_type] != NULL)
16866 /* xgettext:c-format */
16867 _bfd_error_handler (_("%pB: %s unsupported"),
16868 input_bfd, ppc64_elf_howto_table[r_type]->name);
16869 else
16870 /* xgettext:c-format */
16871 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
16872 input_bfd, r_type);
16873
16874 bfd_set_error (bfd_error_bad_value);
16875 ret = false;
16876 goto copy_reloc;
16877
16878 case R_PPC64_NONE:
16879 case R_PPC64_TLS:
16880 case R_PPC64_TLSGD:
16881 case R_PPC64_TLSLD:
16882 case R_PPC64_TOCSAVE:
16883 case R_PPC64_GNU_VTINHERIT:
16884 case R_PPC64_GNU_VTENTRY:
16885 case R_PPC64_ENTRY:
16886 case R_PPC64_PCREL_OPT:
16887 goto copy_reloc;
16888
16889 /* GOT16 relocations. Like an ADDR16 using the symbol's
16890 address in the GOT as relocation value instead of the
16891 symbol's value itself. Also, create a GOT entry for the
16892 symbol and put the symbol value there. */
16893 case R_PPC64_GOT_TLSGD16:
16894 case R_PPC64_GOT_TLSGD16_LO:
16895 case R_PPC64_GOT_TLSGD16_HI:
16896 case R_PPC64_GOT_TLSGD16_HA:
16897 case R_PPC64_GOT_TLSGD_PCREL34:
16898 tls_type = TLS_TLS | TLS_GD;
16899 goto dogot;
16900
16901 case R_PPC64_GOT_TLSLD16:
16902 case R_PPC64_GOT_TLSLD16_LO:
16903 case R_PPC64_GOT_TLSLD16_HI:
16904 case R_PPC64_GOT_TLSLD16_HA:
16905 case R_PPC64_GOT_TLSLD_PCREL34:
16906 tls_type = TLS_TLS | TLS_LD;
16907 goto dogot;
16908
16909 case R_PPC64_GOT_TPREL16_DS:
16910 case R_PPC64_GOT_TPREL16_LO_DS:
16911 case R_PPC64_GOT_TPREL16_HI:
16912 case R_PPC64_GOT_TPREL16_HA:
16913 case R_PPC64_GOT_TPREL_PCREL34:
16914 tls_type = TLS_TLS | TLS_TPREL;
16915 goto dogot;
16916
16917 case R_PPC64_GOT_DTPREL16_DS:
16918 case R_PPC64_GOT_DTPREL16_LO_DS:
16919 case R_PPC64_GOT_DTPREL16_HI:
16920 case R_PPC64_GOT_DTPREL16_HA:
16921 case R_PPC64_GOT_DTPREL_PCREL34:
16922 tls_type = TLS_TLS | TLS_DTPREL;
16923 goto dogot;
16924
16925 case R_PPC64_GOT16:
16926 case R_PPC64_GOT16_LO:
16927 case R_PPC64_GOT16_HI:
16928 case R_PPC64_GOT16_HA:
16929 case R_PPC64_GOT16_DS:
16930 case R_PPC64_GOT16_LO_DS:
16931 case R_PPC64_GOT_PCREL34:
16932 dogot:
16933 {
16934 /* Relocation is to the entry for this symbol in the global
16935 offset table. */
16936 asection *got;
16937 bfd_vma *offp;
16938 bfd_vma off;
16939 unsigned long indx = 0;
16940 struct got_entry *ent;
16941
16942 if (tls_type == (TLS_TLS | TLS_LD)
16943 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16944 ent = ppc64_tlsld_got (input_bfd);
16945 else
16946 {
16947 if (h != NULL)
16948 {
16949 if (!htab->elf.dynamic_sections_created
16950 || h->elf.dynindx == -1
16951 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16952 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16953 /* This is actually a static link, or it is a
16954 -Bsymbolic link and the symbol is defined
16955 locally, or the symbol was forced to be local
16956 because of a version file. */
16957 ;
16958 else
16959 {
16960 indx = h->elf.dynindx;
16961 unresolved_reloc = false;
16962 }
16963 ent = h->elf.got.glist;
16964 }
16965 else
16966 {
16967 if (local_got_ents == NULL)
16968 abort ();
16969 ent = local_got_ents[r_symndx];
16970 }
16971
16972 for (; ent != NULL; ent = ent->next)
16973 if (ent->addend == orig_rel.r_addend
16974 && ent->owner == input_bfd
16975 && ent->tls_type == tls_type)
16976 break;
16977 }
16978
16979 if (ent == NULL)
16980 abort ();
16981 if (ent->is_indirect)
16982 ent = ent->got.ent;
16983 offp = &ent->got.offset;
16984 got = ppc64_elf_tdata (ent->owner)->got;
16985 if (got == NULL)
16986 abort ();
16987
16988 /* The offset must always be a multiple of 8. We use the
16989 least significant bit to record whether we have already
16990 processed this entry. */
16991 off = *offp;
16992 if ((off & 1) != 0)
16993 off &= ~1;
16994 else
16995 {
16996 /* Generate relocs for the dynamic linker, except in
16997 the case of TLSLD where we'll use one entry per
16998 module. */
16999 asection *relgot;
17000 bool ifunc;
17001
17002 *offp = off | 1;
17003 relgot = NULL;
17004 ifunc = (h != NULL
17005 ? h->elf.type == STT_GNU_IFUNC
17006 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
17007 if (ifunc)
17008 {
17009 relgot = htab->elf.irelplt;
17010 if (indx == 0 || is_static_defined (&h->elf))
17011 htab->elf.ifunc_resolvers = true;
17012 }
17013 else if (indx != 0
17014 || (bfd_link_pic (info)
17015 && (h == NULL
17016 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
17017 && !(tls_type != 0
17018 && bfd_link_executable (info)
17019 && (h == NULL
17020 || SYMBOL_REFERENCES_LOCAL (info,
17021 &h->elf)))
17022 && (h != NULL
17023 ? !bfd_is_abs_symbol (&h->elf.root)
17024 : sym->st_shndx != SHN_ABS)))
17025
17026 relgot = ppc64_elf_tdata (ent->owner)->relgot;
17027 if (relgot != NULL)
17028 {
17029 outrel.r_offset = (got->output_section->vma
17030 + got->output_offset
17031 + off);
17032 outrel.r_addend = orig_rel.r_addend;
17033 if (tls_type & (TLS_LD | TLS_GD))
17034 {
17035 outrel.r_addend = 0;
17036 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
17037 if (tls_type == (TLS_TLS | TLS_GD))
17038 {
17039 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17040 &outrel,
17041 relgot));
17042 outrel.r_offset += 8;
17043 outrel.r_addend = orig_rel.r_addend;
17044 outrel.r_info
17045 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17046 }
17047 }
17048 else if (tls_type == (TLS_TLS | TLS_DTPREL))
17049 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
17050 else if (tls_type == (TLS_TLS | TLS_TPREL))
17051 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
17052 else if (indx != 0)
17053 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17054 else
17055 {
17056 if (ifunc)
17057 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17058 else
17059 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17060
17061 /* Write the .got section contents for the sake
17062 of prelink. */
17063 loc = got->contents + off;
17064 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17065 loc);
17066 }
17067
17068 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
17069 {
17070 outrel.r_addend += relocation;
17071 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
17072 {
17073 if (htab->elf.tls_sec == NULL)
17074 outrel.r_addend = 0;
17075 else
17076 outrel.r_addend -= htab->elf.tls_sec->vma;
17077 }
17078 }
17079 if (!(info->enable_dt_relr
17080 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17081 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17082 &outrel, relgot));
17083 }
17084
17085 /* Init the .got section contents here if we're not
17086 emitting a reloc. */
17087 else
17088 {
17089 relocation += orig_rel.r_addend;
17090 if (tls_type != 0)
17091 {
17092 if (htab->elf.tls_sec == NULL)
17093 relocation = 0;
17094 else
17095 {
17096 if (tls_type & TLS_LD)
17097 relocation = 0;
17098 else
17099 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17100 if (tls_type & TLS_TPREL)
17101 relocation += DTP_OFFSET - TP_OFFSET;
17102 }
17103
17104 if (tls_type & (TLS_GD | TLS_LD))
17105 {
17106 bfd_put_64 (output_bfd, relocation,
17107 got->contents + off + 8);
17108 relocation = 1;
17109 }
17110 }
17111 bfd_put_64 (output_bfd, relocation,
17112 got->contents + off);
17113 }
17114 }
17115
17116 if (off >= (bfd_vma) -2)
17117 abort ();
17118
17119 relocation = got->output_section->vma + got->output_offset + off;
17120 addend = 0;
17121 if (!(r_type == R_PPC64_GOT_PCREL34
17122 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17123 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17124 || r_type == R_PPC64_GOT_TPREL_PCREL34
17125 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17126 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17127 }
17128 break;
17129
17130 case R_PPC64_PLT16_HA:
17131 case R_PPC64_PLT16_HI:
17132 case R_PPC64_PLT16_LO:
17133 case R_PPC64_PLT16_LO_DS:
17134 case R_PPC64_PLT_PCREL34:
17135 case R_PPC64_PLT_PCREL34_NOTOC:
17136 case R_PPC64_PLT32:
17137 case R_PPC64_PLT64:
17138 case R_PPC64_PLTSEQ:
17139 case R_PPC64_PLTSEQ_NOTOC:
17140 case R_PPC64_PLTCALL:
17141 case R_PPC64_PLTCALL_NOTOC:
17142 /* Relocation is to the entry for this symbol in the
17143 procedure linkage table. */
17144 unresolved_reloc = true;
17145 {
17146 struct plt_entry **plt_list = NULL;
17147 if (h != NULL)
17148 plt_list = &h->elf.plt.plist;
17149 else if (local_got_ents != NULL)
17150 {
17151 struct plt_entry **local_plt = (struct plt_entry **)
17152 (local_got_ents + symtab_hdr->sh_info);
17153 plt_list = local_plt + r_symndx;
17154 }
17155 if (plt_list)
17156 {
17157 struct plt_entry *ent;
17158
17159 for (ent = *plt_list; ent != NULL; ent = ent->next)
17160 if (ent->plt.offset != (bfd_vma) -1
17161 && ent->addend == orig_rel.r_addend)
17162 {
17163 asection *plt;
17164 bfd_vma got;
17165
17166 plt = htab->elf.splt;
17167 if (use_local_plt (info, elf_hash_entry (h)))
17168 {
17169 if (h != NULL
17170 ? h->elf.type == STT_GNU_IFUNC
17171 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17172 plt = htab->elf.iplt;
17173 else
17174 plt = htab->pltlocal;
17175 }
17176 relocation = (plt->output_section->vma
17177 + plt->output_offset
17178 + ent->plt.offset);
17179 if (r_type == R_PPC64_PLT16_HA
17180 || r_type == R_PPC64_PLT16_HI
17181 || r_type == R_PPC64_PLT16_LO
17182 || r_type == R_PPC64_PLT16_LO_DS)
17183 {
17184 got = (elf_gp (output_bfd)
17185 + htab->sec_info[input_section->id].toc_off);
17186 relocation -= got;
17187 }
17188 addend = 0;
17189 unresolved_reloc = false;
17190 break;
17191 }
17192 }
17193 }
17194 break;
17195
17196 case R_PPC64_TOC:
17197 /* Relocation value is TOC base. */
17198 relocation = TOCstart;
17199 if (r_symndx == STN_UNDEF)
17200 relocation += htab->sec_info[input_section->id].toc_off;
17201 else if (unresolved_reloc)
17202 ;
17203 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17204 relocation += htab->sec_info[sec->id].toc_off;
17205 else
17206 unresolved_reloc = true;
17207 if (unresolved_reloc
17208 || (!is_opd
17209 && h != NULL
17210 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17211 info->callbacks->einfo
17212 /* xgettext:c-format */
17213 (_("%H: %s against %pT is not supported\n"),
17214 input_bfd, input_section, rel->r_offset,
17215 ppc64_elf_howto_table[r_type]->name, sym_name);
17216 goto dodyn;
17217
17218 /* TOC16 relocs. We want the offset relative to the TOC base,
17219 which is the address of the start of the TOC plus 0x8000.
17220 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17221 in this order. */
17222 case R_PPC64_TOC16:
17223 case R_PPC64_TOC16_LO:
17224 case R_PPC64_TOC16_HI:
17225 case R_PPC64_TOC16_DS:
17226 case R_PPC64_TOC16_LO_DS:
17227 case R_PPC64_TOC16_HA:
17228 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17229 if (h != NULL)
17230 goto dodyn;
17231 break;
17232
17233 /* Relocate against the beginning of the section. */
17234 case R_PPC64_SECTOFF:
17235 case R_PPC64_SECTOFF_LO:
17236 case R_PPC64_SECTOFF_HI:
17237 case R_PPC64_SECTOFF_DS:
17238 case R_PPC64_SECTOFF_LO_DS:
17239 case R_PPC64_SECTOFF_HA:
17240 if (sec != NULL)
17241 addend -= sec->output_section->vma;
17242 break;
17243
17244 case R_PPC64_REL16:
17245 case R_PPC64_REL16_LO:
17246 case R_PPC64_REL16_HI:
17247 case R_PPC64_REL16_HA:
17248 case R_PPC64_REL16_HIGH:
17249 case R_PPC64_REL16_HIGHA:
17250 case R_PPC64_REL16_HIGHER:
17251 case R_PPC64_REL16_HIGHERA:
17252 case R_PPC64_REL16_HIGHEST:
17253 case R_PPC64_REL16_HIGHESTA:
17254 case R_PPC64_REL16_HIGHER34:
17255 case R_PPC64_REL16_HIGHERA34:
17256 case R_PPC64_REL16_HIGHEST34:
17257 case R_PPC64_REL16_HIGHESTA34:
17258 case R_PPC64_REL16DX_HA:
17259 case R_PPC64_REL14:
17260 case R_PPC64_REL14_BRNTAKEN:
17261 case R_PPC64_REL14_BRTAKEN:
17262 case R_PPC64_REL24:
17263 case R_PPC64_REL24_NOTOC:
17264 case R_PPC64_REL24_P9NOTOC:
17265 case R_PPC64_PCREL34:
17266 case R_PPC64_PCREL28:
17267 break;
17268
17269 case R_PPC64_TPREL16:
17270 case R_PPC64_TPREL16_LO:
17271 case R_PPC64_TPREL16_HI:
17272 case R_PPC64_TPREL16_HA:
17273 case R_PPC64_TPREL16_DS:
17274 case R_PPC64_TPREL16_LO_DS:
17275 case R_PPC64_TPREL16_HIGH:
17276 case R_PPC64_TPREL16_HIGHA:
17277 case R_PPC64_TPREL16_HIGHER:
17278 case R_PPC64_TPREL16_HIGHERA:
17279 case R_PPC64_TPREL16_HIGHEST:
17280 case R_PPC64_TPREL16_HIGHESTA:
17281 if (h != NULL
17282 && h->elf.root.type == bfd_link_hash_undefweak
17283 && h->elf.dynindx == -1
17284 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17285 {
17286 /* Make this relocation against an undefined weak symbol
17287 resolve to zero. This is really just a tweak, since
17288 code using weak externs ought to check that they are
17289 defined before using them. */
17290 bfd_byte *p = contents + rel->r_offset - d_offset;
17291
17292 insn = bfd_get_32 (input_bfd, p);
17293 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17294 if (insn != 0)
17295 bfd_put_32 (input_bfd, insn, p);
17296 break;
17297 }
17298 /* Fall through. */
17299
17300 case R_PPC64_TPREL34:
17301 if (htab->elf.tls_sec != NULL)
17302 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17303 /* The TPREL16 relocs shouldn't really be used in shared
17304 libs or with non-local symbols as that will result in
17305 DT_TEXTREL being set, but support them anyway. */
17306 goto dodyn;
17307
17308 case R_PPC64_DTPREL16:
17309 case R_PPC64_DTPREL16_LO:
17310 case R_PPC64_DTPREL16_HI:
17311 case R_PPC64_DTPREL16_HA:
17312 case R_PPC64_DTPREL16_DS:
17313 case R_PPC64_DTPREL16_LO_DS:
17314 case R_PPC64_DTPREL16_HIGH:
17315 case R_PPC64_DTPREL16_HIGHA:
17316 case R_PPC64_DTPREL16_HIGHER:
17317 case R_PPC64_DTPREL16_HIGHERA:
17318 case R_PPC64_DTPREL16_HIGHEST:
17319 case R_PPC64_DTPREL16_HIGHESTA:
17320 case R_PPC64_DTPREL34:
17321 if (htab->elf.tls_sec != NULL)
17322 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17323 break;
17324
17325 case R_PPC64_ADDR64_LOCAL:
17326 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17327 ? h->elf.other
17328 : sym->st_other);
17329 break;
17330
17331 case R_PPC64_DTPMOD64:
17332 relocation = 1;
17333 addend = 0;
17334 goto dodyn;
17335
17336 case R_PPC64_TPREL64:
17337 if (htab->elf.tls_sec != NULL)
17338 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17339 goto dodyn;
17340
17341 case R_PPC64_DTPREL64:
17342 if (htab->elf.tls_sec != NULL)
17343 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17344 /* Fall through. */
17345
17346 /* Relocations that may need to be propagated if this is a
17347 dynamic object. */
17348 case R_PPC64_REL30:
17349 case R_PPC64_REL32:
17350 case R_PPC64_REL64:
17351 case R_PPC64_ADDR14:
17352 case R_PPC64_ADDR14_BRNTAKEN:
17353 case R_PPC64_ADDR14_BRTAKEN:
17354 case R_PPC64_ADDR16:
17355 case R_PPC64_ADDR16_DS:
17356 case R_PPC64_ADDR16_HA:
17357 case R_PPC64_ADDR16_HI:
17358 case R_PPC64_ADDR16_HIGH:
17359 case R_PPC64_ADDR16_HIGHA:
17360 case R_PPC64_ADDR16_HIGHER:
17361 case R_PPC64_ADDR16_HIGHERA:
17362 case R_PPC64_ADDR16_HIGHEST:
17363 case R_PPC64_ADDR16_HIGHESTA:
17364 case R_PPC64_ADDR16_LO:
17365 case R_PPC64_ADDR16_LO_DS:
17366 case R_PPC64_ADDR16_HIGHER34:
17367 case R_PPC64_ADDR16_HIGHERA34:
17368 case R_PPC64_ADDR16_HIGHEST34:
17369 case R_PPC64_ADDR16_HIGHESTA34:
17370 case R_PPC64_ADDR24:
17371 case R_PPC64_ADDR32:
17372 case R_PPC64_ADDR64:
17373 case R_PPC64_UADDR16:
17374 case R_PPC64_UADDR32:
17375 case R_PPC64_UADDR64:
17376 case R_PPC64_D34:
17377 case R_PPC64_D34_LO:
17378 case R_PPC64_D34_HI30:
17379 case R_PPC64_D34_HA30:
17380 case R_PPC64_D28:
17381 dodyn:
17382 if ((input_section->flags & SEC_ALLOC) == 0)
17383 break;
17384
17385 if (NO_OPD_RELOCS && is_opd)
17386 break;
17387
17388 if (bfd_link_pic (info)
17389 ? ((h == NULL
17390 || h->elf.dyn_relocs != NULL)
17391 && ((h != NULL && pc_dynrelocs (h))
17392 || must_be_dyn_reloc (info, r_type)))
17393 : (h != NULL
17394 ? h->elf.dyn_relocs != NULL
17395 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17396 {
17397 bool skip, relocate;
17398 asection *sreloc;
17399 bfd_vma out_off;
17400 long indx = 0;
17401
17402 /* When generating a dynamic object, these relocations
17403 are copied into the output file to be resolved at run
17404 time. */
17405
17406 skip = false;
17407 relocate = false;
17408
17409 out_off = _bfd_elf_section_offset (output_bfd, info,
17410 input_section, rel->r_offset);
17411 if (out_off == (bfd_vma) -1)
17412 skip = true;
17413 else if (out_off == (bfd_vma) -2)
17414 skip = true, relocate = true;
17415 out_off += (input_section->output_section->vma
17416 + input_section->output_offset);
17417 outrel.r_offset = out_off;
17418 outrel.r_addend = rel->r_addend;
17419
17420 /* Optimize unaligned reloc use. */
17421 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17422 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17423 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17424 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17425 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17426 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17427 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17428 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17429 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17430
17431 if (skip)
17432 memset (&outrel, 0, sizeof outrel);
17433 else if (h != NULL
17434 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17435 && !is_opd
17436 && r_type != R_PPC64_TOC)
17437 {
17438 indx = h->elf.dynindx;
17439 BFD_ASSERT (indx != -1);
17440 outrel.r_info = ELF64_R_INFO (indx, r_type);
17441 }
17442 else
17443 {
17444 /* This symbol is local, or marked to become local,
17445 or this is an opd section reloc which must point
17446 at a local function. */
17447 outrel.r_addend += relocation;
17448 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17449 {
17450 if (is_opd && h != NULL)
17451 {
17452 /* Lie about opd entries. This case occurs
17453 when building shared libraries and we
17454 reference a function in another shared
17455 lib. The same thing happens for a weak
17456 definition in an application that's
17457 overridden by a strong definition in a
17458 shared lib. (I believe this is a generic
17459 bug in binutils handling of weak syms.)
17460 In these cases we won't use the opd
17461 entry in this lib. */
17462 unresolved_reloc = false;
17463 }
17464 if (!is_opd
17465 && r_type == R_PPC64_ADDR64
17466 && (h != NULL
17467 ? h->elf.type == STT_GNU_IFUNC
17468 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17469 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17470 else
17471 {
17472 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17473
17474 /* We need to relocate .opd contents for ld.so.
17475 Prelink also wants simple and consistent rules
17476 for relocs. This make all RELATIVE relocs have
17477 *r_offset equal to r_addend. */
17478 relocate = true;
17479 }
17480 }
17481 else
17482 {
17483 if (h != NULL
17484 ? h->elf.type == STT_GNU_IFUNC
17485 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17486 {
17487 info->callbacks->einfo
17488 /* xgettext:c-format */
17489 (_("%H: %s for indirect "
17490 "function `%pT' unsupported\n"),
17491 input_bfd, input_section, rel->r_offset,
17492 ppc64_elf_howto_table[r_type]->name,
17493 sym_name);
17494 ret = false;
17495 }
17496 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17497 ;
17498 else if (sec == NULL || sec->owner == NULL)
17499 {
17500 bfd_set_error (bfd_error_bad_value);
17501 return false;
17502 }
17503 else
17504 {
17505 asection *osec = sec->output_section;
17506
17507 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17508 {
17509 /* TLS symbol values are relative to the
17510 TLS segment. Dynamic relocations for
17511 local TLS symbols therefore can't be
17512 reduced to a relocation against their
17513 section symbol because it holds the
17514 address of the section, not a value
17515 relative to the TLS segment. We could
17516 change the .tdata dynamic section symbol
17517 to be zero value but STN_UNDEF works
17518 and is used elsewhere, eg. for TPREL64
17519 GOT relocs against local TLS symbols. */
17520 osec = htab->elf.tls_sec;
17521 indx = 0;
17522 }
17523 else
17524 {
17525 indx = elf_section_data (osec)->dynindx;
17526 if (indx == 0)
17527 {
17528 if ((osec->flags & SEC_READONLY) == 0
17529 && htab->elf.data_index_section != NULL)
17530 osec = htab->elf.data_index_section;
17531 else
17532 osec = htab->elf.text_index_section;
17533 indx = elf_section_data (osec)->dynindx;
17534 }
17535 BFD_ASSERT (indx != 0);
17536 }
17537
17538 /* We are turning this relocation into one
17539 against a section symbol, so subtract out
17540 the output section's address but not the
17541 offset of the input section in the output
17542 section. */
17543 outrel.r_addend -= osec->vma;
17544 }
17545
17546 outrel.r_info = ELF64_R_INFO (indx, r_type);
17547 }
17548 }
17549
17550 if (!(info->enable_dt_relr
17551 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17552 && maybe_relr (ELF64_R_TYPE (orig_rel.r_info),
17553 rel, input_section)))
17554 {
17555 sreloc = elf_section_data (input_section)->sreloc;
17556 if (h != NULL
17557 ? h->elf.type == STT_GNU_IFUNC
17558 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17559 {
17560 sreloc = htab->elf.irelplt;
17561 if (indx == 0 || is_static_defined (&h->elf))
17562 htab->elf.ifunc_resolvers = true;
17563 }
17564 if (sreloc == NULL)
17565 abort ();
17566
17567 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &outrel,
17568 sreloc));
17569 }
17570
17571 if (!warned_dynamic
17572 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17573 {
17574 info->callbacks->einfo
17575 /* xgettext:c-format */
17576 (_("%X%P: %pB: %s against %pT "
17577 "is not supported by glibc as a dynamic relocation\n"),
17578 input_bfd,
17579 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17580 sym_name);
17581 warned_dynamic = true;
17582 }
17583
17584 /* If this reloc is against an external symbol, it will
17585 be computed at runtime, so there's no need to do
17586 anything now. However, for the sake of prelink ensure
17587 that the section contents are a known value. */
17588 if (!relocate)
17589 {
17590 unresolved_reloc = false;
17591 /* The value chosen here is quite arbitrary as ld.so
17592 ignores section contents except for the special
17593 case of .opd where the contents might be accessed
17594 before relocation. Choose zero, as that won't
17595 cause reloc overflow. */
17596 relocation = 0;
17597 addend = 0;
17598 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17599 to improve backward compatibility with older
17600 versions of ld. */
17601 if (r_type == R_PPC64_ADDR64)
17602 addend = outrel.r_addend;
17603 /* Adjust pc_relative relocs to have zero in *r_offset. */
17604 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17605 addend = outrel.r_offset;
17606 }
17607 }
17608 break;
17609
17610 case R_PPC64_COPY:
17611 case R_PPC64_GLOB_DAT:
17612 case R_PPC64_JMP_SLOT:
17613 case R_PPC64_JMP_IREL:
17614 case R_PPC64_RELATIVE:
17615 /* We shouldn't ever see these dynamic relocs in relocatable
17616 files. */
17617 /* Fall through. */
17618
17619 case R_PPC64_PLTGOT16:
17620 case R_PPC64_PLTGOT16_DS:
17621 case R_PPC64_PLTGOT16_HA:
17622 case R_PPC64_PLTGOT16_HI:
17623 case R_PPC64_PLTGOT16_LO:
17624 case R_PPC64_PLTGOT16_LO_DS:
17625 case R_PPC64_PLTREL32:
17626 case R_PPC64_PLTREL64:
17627 /* These ones haven't been implemented yet. */
17628
17629 info->callbacks->einfo
17630 /* xgettext:c-format */
17631 (_("%P: %pB: %s is not supported for `%pT'\n"),
17632 input_bfd,
17633 ppc64_elf_howto_table[r_type]->name, sym_name);
17634
17635 bfd_set_error (bfd_error_invalid_operation);
17636 ret = false;
17637 goto copy_reloc;
17638 }
17639
17640 /* Multi-instruction sequences that access the TOC can be
17641 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17642 to nop; addi rb,r2,x; */
17643 switch (r_type)
17644 {
17645 default:
17646 break;
17647
17648 case R_PPC64_GOT_TLSLD16_HI:
17649 case R_PPC64_GOT_TLSGD16_HI:
17650 case R_PPC64_GOT_TPREL16_HI:
17651 case R_PPC64_GOT_DTPREL16_HI:
17652 case R_PPC64_GOT16_HI:
17653 case R_PPC64_TOC16_HI:
17654 /* These relocs would only be useful if building up an
17655 offset to later add to r2, perhaps in an indexed
17656 addressing mode instruction. Don't try to optimize.
17657 Unfortunately, the possibility of someone building up an
17658 offset like this or even with the HA relocs, means that
17659 we need to check the high insn when optimizing the low
17660 insn. */
17661 break;
17662
17663 case R_PPC64_PLTCALL_NOTOC:
17664 if (!unresolved_reloc)
17665 htab->notoc_plt = 1;
17666 /* Fall through. */
17667 case R_PPC64_PLTCALL:
17668 if (unresolved_reloc
17669 && offset_in_range (input_section, rel->r_offset,
17670 r_type == R_PPC64_PLTCALL ? 8 : 4))
17671 {
17672 /* No plt entry. Make this into a direct call. */
17673 bfd_byte *p = contents + rel->r_offset;
17674 insn = bfd_get_32 (input_bfd, p);
17675 insn &= 1;
17676 bfd_put_32 (input_bfd, B_DOT | insn, p);
17677 if (r_type == R_PPC64_PLTCALL)
17678 bfd_put_32 (input_bfd, NOP, p + 4);
17679 unresolved_reloc = save_unresolved_reloc;
17680 r_type = R_PPC64_REL24;
17681 }
17682 break;
17683
17684 case R_PPC64_PLTSEQ_NOTOC:
17685 case R_PPC64_PLTSEQ:
17686 if (unresolved_reloc)
17687 {
17688 unresolved_reloc = false;
17689 goto nop_it;
17690 }
17691 break;
17692
17693 case R_PPC64_PLT_PCREL34_NOTOC:
17694 if (!unresolved_reloc)
17695 htab->notoc_plt = 1;
17696 /* Fall through. */
17697 case R_PPC64_PLT_PCREL34:
17698 if (unresolved_reloc
17699 && offset_in_range (input_section, rel->r_offset, 8))
17700 {
17701 bfd_byte *p = contents + rel->r_offset;
17702 bfd_put_32 (input_bfd, PNOP >> 32, p);
17703 bfd_put_32 (input_bfd, PNOP, p + 4);
17704 unresolved_reloc = false;
17705 goto copy_reloc;
17706 }
17707 break;
17708
17709 case R_PPC64_PLT16_HA:
17710 if (unresolved_reloc)
17711 {
17712 unresolved_reloc = false;
17713 goto nop_it;
17714 }
17715 /* Fall through. */
17716 case R_PPC64_GOT_TLSLD16_HA:
17717 case R_PPC64_GOT_TLSGD16_HA:
17718 case R_PPC64_GOT_TPREL16_HA:
17719 case R_PPC64_GOT_DTPREL16_HA:
17720 case R_PPC64_GOT16_HA:
17721 case R_PPC64_TOC16_HA:
17722 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17723 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17724 && !(bfd_link_pic (info)
17725 && (h != NULL
17726 ? bfd_is_abs_symbol (&h->elf.root)
17727 : sec == bfd_abs_section_ptr)))
17728 {
17729 bfd_byte *p;
17730 nop_it:
17731 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17732 {
17733 p = contents + (rel->r_offset & ~3);
17734 bfd_put_32 (input_bfd, NOP, p);
17735 goto copy_reloc;
17736 }
17737 }
17738 break;
17739
17740 case R_PPC64_PLT16_LO:
17741 case R_PPC64_PLT16_LO_DS:
17742 if (unresolved_reloc)
17743 {
17744 unresolved_reloc = false;
17745 goto nop_it;
17746 }
17747 /* Fall through. */
17748 case R_PPC64_GOT_TLSLD16_LO:
17749 case R_PPC64_GOT_TLSGD16_LO:
17750 case R_PPC64_GOT_TPREL16_LO_DS:
17751 case R_PPC64_GOT_DTPREL16_LO_DS:
17752 case R_PPC64_GOT16_LO:
17753 case R_PPC64_GOT16_LO_DS:
17754 case R_PPC64_TOC16_LO:
17755 case R_PPC64_TOC16_LO_DS:
17756 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17757 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17758 && !(bfd_link_pic (info)
17759 && (h != NULL
17760 ? bfd_is_abs_symbol (&h->elf.root)
17761 : sec == bfd_abs_section_ptr))
17762 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17763 {
17764 bfd_byte *p = contents + (rel->r_offset & ~3);
17765 insn = bfd_get_32 (input_bfd, p);
17766 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17767 {
17768 /* Transform addic to addi when we change reg. */
17769 insn &= ~((0x3fu << 26) | (0x1f << 16));
17770 insn |= (14u << 26) | (2 << 16);
17771 }
17772 else
17773 {
17774 insn &= ~(0x1f << 16);
17775 insn |= 2 << 16;
17776 }
17777 bfd_put_32 (input_bfd, insn, p);
17778 }
17779 break;
17780
17781 case R_PPC64_TPREL16_HA:
17782 if (htab->do_tls_opt
17783 && relocation + addend + 0x8000 < 0x10000
17784 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17785 {
17786 bfd_byte *p = contents + (rel->r_offset & ~3);
17787 bfd_put_32 (input_bfd, NOP, p);
17788 goto copy_reloc;
17789 }
17790 break;
17791
17792 case R_PPC64_TPREL16_LO:
17793 case R_PPC64_TPREL16_LO_DS:
17794 if (htab->do_tls_opt
17795 && relocation + addend + 0x8000 < 0x10000
17796 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17797 {
17798 bfd_byte *p = contents + (rel->r_offset & ~3);
17799 insn = bfd_get_32 (input_bfd, p);
17800 insn &= ~(0x1f << 16);
17801 insn |= 13 << 16;
17802 bfd_put_32 (input_bfd, insn, p);
17803 }
17804 break;
17805 }
17806
17807 /* Do any further special processing. */
17808 switch (r_type)
17809 {
17810 default:
17811 break;
17812
17813 case R_PPC64_REL16_HA:
17814 case R_PPC64_REL16_HIGHA:
17815 case R_PPC64_REL16_HIGHERA:
17816 case R_PPC64_REL16_HIGHESTA:
17817 case R_PPC64_REL16DX_HA:
17818 case R_PPC64_ADDR16_HA:
17819 case R_PPC64_ADDR16_HIGHA:
17820 case R_PPC64_ADDR16_HIGHERA:
17821 case R_PPC64_ADDR16_HIGHESTA:
17822 case R_PPC64_TOC16_HA:
17823 case R_PPC64_SECTOFF_HA:
17824 case R_PPC64_TPREL16_HA:
17825 case R_PPC64_TPREL16_HIGHA:
17826 case R_PPC64_TPREL16_HIGHERA:
17827 case R_PPC64_TPREL16_HIGHESTA:
17828 case R_PPC64_DTPREL16_HA:
17829 case R_PPC64_DTPREL16_HIGHA:
17830 case R_PPC64_DTPREL16_HIGHERA:
17831 case R_PPC64_DTPREL16_HIGHESTA:
17832 /* It's just possible that this symbol is a weak symbol
17833 that's not actually defined anywhere. In that case,
17834 'sec' would be NULL, and we should leave the symbol
17835 alone (it will be set to zero elsewhere in the link). */
17836 if (sec == NULL)
17837 break;
17838 /* Fall through. */
17839
17840 case R_PPC64_GOT16_HA:
17841 case R_PPC64_PLTGOT16_HA:
17842 case R_PPC64_PLT16_HA:
17843 case R_PPC64_GOT_TLSGD16_HA:
17844 case R_PPC64_GOT_TLSLD16_HA:
17845 case R_PPC64_GOT_TPREL16_HA:
17846 case R_PPC64_GOT_DTPREL16_HA:
17847 /* Add 0x10000 if sign bit in 0:15 is set.
17848 Bits 0:15 are not used. */
17849 addend += 0x8000;
17850 break;
17851
17852 case R_PPC64_D34_HA30:
17853 case R_PPC64_ADDR16_HIGHERA34:
17854 case R_PPC64_ADDR16_HIGHESTA34:
17855 case R_PPC64_REL16_HIGHERA34:
17856 case R_PPC64_REL16_HIGHESTA34:
17857 if (sec != NULL)
17858 addend += 1ULL << 33;
17859 break;
17860
17861 case R_PPC64_ADDR16_DS:
17862 case R_PPC64_ADDR16_LO_DS:
17863 case R_PPC64_GOT16_DS:
17864 case R_PPC64_GOT16_LO_DS:
17865 case R_PPC64_PLT16_LO_DS:
17866 case R_PPC64_SECTOFF_DS:
17867 case R_PPC64_SECTOFF_LO_DS:
17868 case R_PPC64_TOC16_DS:
17869 case R_PPC64_TOC16_LO_DS:
17870 case R_PPC64_PLTGOT16_DS:
17871 case R_PPC64_PLTGOT16_LO_DS:
17872 case R_PPC64_GOT_TPREL16_DS:
17873 case R_PPC64_GOT_TPREL16_LO_DS:
17874 case R_PPC64_GOT_DTPREL16_DS:
17875 case R_PPC64_GOT_DTPREL16_LO_DS:
17876 case R_PPC64_TPREL16_DS:
17877 case R_PPC64_TPREL16_LO_DS:
17878 case R_PPC64_DTPREL16_DS:
17879 case R_PPC64_DTPREL16_LO_DS:
17880 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17881 break;
17882 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17883 mask = 3;
17884 /* If this reloc is against an lq, lxv, or stxv insn, then
17885 the value must be a multiple of 16. This is somewhat of
17886 a hack, but the "correct" way to do this by defining _DQ
17887 forms of all the _DS relocs bloats all reloc switches in
17888 this file. It doesn't make much sense to use these
17889 relocs in data, so testing the insn should be safe. */
17890 if ((insn & (0x3fu << 26)) == (56u << 26)
17891 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17892 mask = 15;
17893 relocation += addend;
17894 addend = insn & (mask ^ 3);
17895 if ((relocation & mask) != 0)
17896 {
17897 relocation ^= relocation & mask;
17898 info->callbacks->einfo
17899 /* xgettext:c-format */
17900 (_("%H: error: %s not a multiple of %u\n"),
17901 input_bfd, input_section, rel->r_offset,
17902 ppc64_elf_howto_table[r_type]->name,
17903 mask + 1);
17904 bfd_set_error (bfd_error_bad_value);
17905 ret = false;
17906 goto copy_reloc;
17907 }
17908 break;
17909 }
17910
17911 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17912 because such sections are not SEC_ALLOC and thus ld.so will
17913 not process them. */
17914 howto = ppc64_elf_howto_table[r_type];
17915 if (unresolved_reloc
17916 && !((input_section->flags & SEC_DEBUGGING) != 0
17917 && h->elf.def_dynamic)
17918 && _bfd_elf_section_offset (output_bfd, info, input_section,
17919 rel->r_offset) != (bfd_vma) -1)
17920 {
17921 info->callbacks->einfo
17922 /* xgettext:c-format */
17923 (_("%H: unresolvable %s against `%pT'\n"),
17924 input_bfd, input_section, rel->r_offset,
17925 howto->name,
17926 h->elf.root.root.string);
17927 ret = false;
17928 }
17929
17930 /* 16-bit fields in insns mostly have signed values, but a
17931 few insns have 16-bit unsigned values. Really, we should
17932 have different reloc types. */
17933 if (howto->complain_on_overflow != complain_overflow_dont
17934 && howto->dst_mask == 0xffff
17935 && (input_section->flags & SEC_CODE) != 0
17936 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17937 {
17938 enum complain_overflow complain = complain_overflow_signed;
17939
17940 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17941 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17942 complain = complain_overflow_bitfield;
17943 else if (howto->rightshift == 0
17944 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17945 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17946 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17947 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17948 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17949 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17950 complain = complain_overflow_unsigned;
17951 if (howto->complain_on_overflow != complain)
17952 {
17953 alt_howto = *howto;
17954 alt_howto.complain_on_overflow = complain;
17955 howto = &alt_howto;
17956 }
17957 }
17958
17959 switch (r_type)
17960 {
17961 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17962 case R_PPC64_D34:
17963 case R_PPC64_D34_LO:
17964 case R_PPC64_D34_HI30:
17965 case R_PPC64_D34_HA30:
17966 case R_PPC64_PCREL34:
17967 case R_PPC64_GOT_PCREL34:
17968 case R_PPC64_TPREL34:
17969 case R_PPC64_DTPREL34:
17970 case R_PPC64_GOT_TLSGD_PCREL34:
17971 case R_PPC64_GOT_TLSLD_PCREL34:
17972 case R_PPC64_GOT_TPREL_PCREL34:
17973 case R_PPC64_GOT_DTPREL_PCREL34:
17974 case R_PPC64_PLT_PCREL34:
17975 case R_PPC64_PLT_PCREL34_NOTOC:
17976 case R_PPC64_D28:
17977 case R_PPC64_PCREL28:
17978 if (!offset_in_range (input_section, rel->r_offset, 8))
17979 r = bfd_reloc_outofrange;
17980 else
17981 {
17982 relocation += addend;
17983 if (howto->pc_relative)
17984 relocation -= (rel->r_offset
17985 + input_section->output_offset
17986 + input_section->output_section->vma);
17987 relocation >>= howto->rightshift;
17988
17989 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17990 pinsn <<= 32;
17991 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17992
17993 pinsn &= ~howto->dst_mask;
17994 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17995 & howto->dst_mask);
17996 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17997 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17998 r = bfd_reloc_ok;
17999 if (howto->complain_on_overflow == complain_overflow_signed
18000 && (relocation + (1ULL << (howto->bitsize - 1))
18001 >= 1ULL << howto->bitsize))
18002 r = bfd_reloc_overflow;
18003 }
18004 break;
18005
18006 case R_PPC64_REL16DX_HA:
18007 if (!offset_in_range (input_section, rel->r_offset, 4))
18008 r = bfd_reloc_outofrange;
18009 else
18010 {
18011 relocation += addend;
18012 relocation -= (rel->r_offset
18013 + input_section->output_offset
18014 + input_section->output_section->vma);
18015 relocation = (bfd_signed_vma) relocation >> 16;
18016 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
18017 insn &= ~0x1fffc1;
18018 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
18019 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
18020 r = bfd_reloc_ok;
18021 if (relocation + 0x8000 > 0xffff)
18022 r = bfd_reloc_overflow;
18023 }
18024 break;
18025
18026 default:
18027 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
18028 contents, rel->r_offset,
18029 relocation, addend);
18030 }
18031
18032 if (r != bfd_reloc_ok)
18033 {
18034 char *more_info = NULL;
18035 const char *reloc_name = howto->name;
18036
18037 if (reloc_dest != DEST_NORMAL)
18038 {
18039 more_info = bfd_malloc (strlen (reloc_name) + 8);
18040 if (more_info != NULL)
18041 {
18042 strcpy (more_info, reloc_name);
18043 strcat (more_info, (reloc_dest == DEST_OPD
18044 ? " (OPD)" : " (stub)"));
18045 reloc_name = more_info;
18046 }
18047 }
18048
18049 if (r == bfd_reloc_overflow)
18050 {
18051 /* On code like "if (foo) foo();" don't report overflow
18052 on a branch to zero when foo is undefined. */
18053 if (!warned
18054 && (reloc_dest == DEST_STUB
18055 || !(h != NULL
18056 && (h->elf.root.type == bfd_link_hash_undefweak
18057 || h->elf.root.type == bfd_link_hash_undefined)
18058 && is_branch_reloc (r_type))))
18059 info->callbacks->reloc_overflow
18060 (info, (struct bfd_link_hash_entry *) h, sym_name,
18061 reloc_name, orig_rel.r_addend, input_bfd, input_section,
18062 rel->r_offset);
18063 }
18064 else
18065 {
18066 info->callbacks->einfo
18067 /* xgettext:c-format */
18068 (_("%H: %s against `%pT': error %d\n"),
18069 input_bfd, input_section, rel->r_offset,
18070 reloc_name, sym_name, (int) r);
18071 ret = false;
18072 }
18073 free (more_info);
18074 }
18075 copy_reloc:
18076 if (wrel != rel)
18077 *wrel = *rel;
18078 }
18079
18080 if (wrel != rel)
18081 {
18082 Elf_Internal_Shdr *rel_hdr;
18083 size_t deleted = rel - wrel;
18084
18085 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18086 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18087 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18088 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18089 input_section->reloc_count -= deleted;
18090 }
18091
18092 /* If we're emitting relocations, then shortly after this function
18093 returns, reloc offsets and addends for this section will be
18094 adjusted. Worse, reloc symbol indices will be for the output
18095 file rather than the input. Save a copy of the relocs for
18096 opd_entry_value. */
18097 if (is_opd
18098 && (info->emitrelocations || bfd_link_relocatable (info))
18099 && input_section->reloc_count != 0)
18100 {
18101 bfd_size_type amt;
18102 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18103 rel = bfd_alloc (input_bfd, amt);
18104 ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
18105 if (rel == NULL)
18106 return false;
18107 memcpy (rel, relocs, amt);
18108 }
18109 return ret;
18110}
18111
18112/* Adjust the value of any local symbols in opd sections. */
18113
18114static int
18115ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18116 const char *name ATTRIBUTE_UNUSED,
18117 Elf_Internal_Sym *elfsym,
18118 asection *input_sec,
18119 struct elf_link_hash_entry *h)
18120{
18121 struct _opd_sec_data *opd;
18122 long adjust;
18123 bfd_vma value;
18124
18125 if (h != NULL)
18126 return 1;
18127
18128 opd = get_opd_info (input_sec);
18129 if (opd == NULL || opd->adjust == NULL)
18130 return 1;
18131
18132 value = elfsym->st_value - input_sec->output_offset;
18133 if (!bfd_link_relocatable (info))
18134 value -= input_sec->output_section->vma;
18135
18136 adjust = opd->adjust[OPD_NDX (value)];
18137 if (adjust == -1)
18138 return 2;
18139
18140 elfsym->st_value += adjust;
18141 return 1;
18142}
18143
18144/* Finish up dynamic symbol handling. We set the contents of various
18145 dynamic sections here. */
18146
18147static bool
18148ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18149 struct bfd_link_info *info,
18150 struct elf_link_hash_entry *h,
18151 Elf_Internal_Sym *sym)
18152{
18153 struct ppc_link_hash_table *htab;
18154 struct plt_entry *ent;
18155
18156 htab = ppc_hash_table (info);
18157
18158 if (!htab->opd_abi && !h->def_regular)
18159 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18160 if (ent->plt.offset != (bfd_vma) -1)
18161 {
18162 /* Mark the symbol as undefined, rather than as
18163 defined in glink. Leave the value if there were
18164 any relocations where pointer equality matters
18165 (this is a clue for the dynamic linker, to make
18166 function pointer comparisons work between an
18167 application and shared library), otherwise set it
18168 to zero. */
18169 sym->st_shndx = SHN_UNDEF;
18170 if (!h->pointer_equality_needed)
18171 sym->st_value = 0;
18172 else if (!h->ref_regular_nonweak)
18173 {
18174 /* This breaks function pointer comparisons, but
18175 that is better than breaking tests for a NULL
18176 function pointer. */
18177 sym->st_value = 0;
18178 }
18179 break;
18180 }
18181
18182 if (h->needs_copy
18183 && (h->root.type == bfd_link_hash_defined
18184 || h->root.type == bfd_link_hash_defweak)
18185 && (h->root.u.def.section == htab->elf.sdynbss
18186 || h->root.u.def.section == htab->elf.sdynrelro))
18187 {
18188 /* This symbol needs a copy reloc. Set it up. */
18189 Elf_Internal_Rela rela;
18190 asection *srel;
18191
18192 if (h->dynindx == -1)
18193 abort ();
18194
18195 rela.r_offset = defined_sym_val (h);
18196 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18197 rela.r_addend = 0;
18198 if (h->root.u.def.section == htab->elf.sdynrelro)
18199 srel = htab->elf.sreldynrelro;
18200 else
18201 srel = htab->elf.srelbss;
18202 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &rela, srel));
18203 }
18204
18205 return true;
18206}
18207
18208/* Used to decide how to sort relocs in an optimal manner for the
18209 dynamic linker, before writing them out. */
18210
18211static enum elf_reloc_type_class
18212ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18213 const asection *rel_sec,
18214 const Elf_Internal_Rela *rela)
18215{
18216 enum elf_ppc64_reloc_type r_type;
18217 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18218
18219 if (rel_sec == htab->elf.irelplt)
18220 return reloc_class_ifunc;
18221
18222 r_type = ELF64_R_TYPE (rela->r_info);
18223 switch (r_type)
18224 {
18225 case R_PPC64_RELATIVE:
18226 return reloc_class_relative;
18227 case R_PPC64_JMP_SLOT:
18228 return reloc_class_plt;
18229 case R_PPC64_COPY:
18230 return reloc_class_copy;
18231 default:
18232 return reloc_class_normal;
18233 }
18234}
18235
18236/* Finish up the dynamic sections. */
18237
18238static bool
18239ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18240 struct bfd_link_info *info)
18241{
18242 struct ppc_link_hash_table *htab;
18243 bfd *dynobj;
18244 asection *sdyn;
18245
18246 htab = ppc_hash_table (info);
18247 if (htab == NULL)
18248 return false;
18249
18250 dynobj = htab->elf.dynobj;
18251 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18252
18253 if (htab->elf.dynamic_sections_created)
18254 {
18255 Elf64_External_Dyn *dyncon, *dynconend;
18256
18257 if (sdyn == NULL || htab->elf.sgot == NULL)
18258 abort ();
18259
18260 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18261 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18262 for (; dyncon < dynconend; dyncon++)
18263 {
18264 Elf_Internal_Dyn dyn;
18265 asection *s;
18266
18267 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18268
18269 switch (dyn.d_tag)
18270 {
18271 default:
18272 continue;
18273
18274 case DT_PPC64_GLINK:
18275 s = htab->glink;
18276 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18277 /* We stupidly defined DT_PPC64_GLINK to be the start
18278 of glink rather than the first entry point, which is
18279 what ld.so needs, and now have a bigger stub to
18280 support automatic multiple TOCs. */
18281 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18282 break;
18283
18284 case DT_PPC64_OPD:
18285 s = bfd_get_section_by_name (output_bfd, ".opd");
18286 if (s == NULL)
18287 continue;
18288 dyn.d_un.d_ptr = s->vma;
18289 break;
18290
18291 case DT_PPC64_OPT:
18292 if ((htab->do_multi_toc && htab->multi_toc_needed)
18293 || htab->notoc_plt)
18294 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18295 if (htab->has_plt_localentry0)
18296 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18297 break;
18298
18299 case DT_PPC64_OPDSZ:
18300 s = bfd_get_section_by_name (output_bfd, ".opd");
18301 if (s == NULL)
18302 continue;
18303 dyn.d_un.d_val = s->size;
18304 break;
18305
18306 case DT_PLTGOT:
18307 s = htab->elf.splt;
18308 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18309 break;
18310
18311 case DT_JMPREL:
18312 s = htab->elf.srelplt;
18313 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18314 break;
18315
18316 case DT_PLTRELSZ:
18317 dyn.d_un.d_val = htab->elf.srelplt->size;
18318 break;
18319
18320 case DT_TEXTREL:
18321 if (htab->elf.ifunc_resolvers)
18322 info->callbacks->einfo
18323 (_("%P: warning: text relocations and GNU indirect "
18324 "functions may result in a segfault at runtime\n"));
18325 continue;
18326 }
18327
18328 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18329 }
18330 }
18331
18332 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18333 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18334 {
18335 /* Fill in the first entry in the global offset table.
18336 We use it to hold the link-time TOCbase. */
18337 bfd_put_64 (output_bfd,
18338 elf_gp (output_bfd) + TOC_BASE_OFF,
18339 htab->elf.sgot->contents);
18340
18341 /* Set .got entry size. */
18342 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18343 = 8;
18344 }
18345
18346 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18347 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18348 {
18349 /* Set .plt entry size. */
18350 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18351 = PLT_ENTRY_SIZE (htab);
18352 }
18353
18354 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18355 brlt ourselves if emitrelocations. */
18356 if (htab->brlt != NULL
18357 && htab->brlt->reloc_count != 0
18358 && !_bfd_elf_link_output_relocs (output_bfd,
18359 htab->brlt,
18360 elf_section_data (htab->brlt)->rela.hdr,
18361 elf_section_data (htab->brlt)->relocs,
18362 NULL))
18363 return false;
18364
18365 if (htab->glink != NULL
18366 && htab->glink->reloc_count != 0
18367 && !_bfd_elf_link_output_relocs (output_bfd,
18368 htab->glink,
18369 elf_section_data (htab->glink)->rela.hdr,
18370 elf_section_data (htab->glink)->relocs,
18371 NULL))
18372 return false;
18373
18374
18375 if (htab->glink_eh_frame != NULL
18376 && htab->glink_eh_frame->size != 0
18377 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18378 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18379 htab->glink_eh_frame,
18380 htab->glink_eh_frame->contents))
18381 return false;
18382
18383 /* We need to handle writing out multiple GOT sections ourselves,
18384 since we didn't add them to DYNOBJ. We know dynobj is the first
18385 bfd. */
18386 while ((dynobj = dynobj->link.next) != NULL)
18387 {
18388 asection *s;
18389
18390 if (!is_ppc64_elf (dynobj))
18391 continue;
18392
18393 s = ppc64_elf_tdata (dynobj)->got;
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 s = ppc64_elf_tdata (dynobj)->relgot;
18402 if (s != NULL
18403 && s->size != 0
18404 && s->output_section != bfd_abs_section_ptr
18405 && !bfd_set_section_contents (output_bfd, s->output_section,
18406 s->contents, s->output_offset,
18407 s->size))
18408 return false;
18409 }
18410
18411 return true;
18412}
18413
18414static bool
18415ppc64_elf_free_cached_info (bfd *abfd)
18416{
18417 if (abfd->sections)
18418 for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18419 opd != NULL;
18420 opd = bfd_get_next_section_by_name (NULL, opd))
18421 if (opd->reloc_count == 0)
18422 free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18423
18424 return _bfd_elf_free_cached_info (abfd);
18425}
18426
18427#include "elf64-target.h"
18428
18429/* FreeBSD support */
18430
18431#undef TARGET_LITTLE_SYM
18432#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18433#undef TARGET_LITTLE_NAME
18434#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18435
18436#undef TARGET_BIG_SYM
18437#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18438#undef TARGET_BIG_NAME
18439#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18440
18441#undef ELF_OSABI
18442#define ELF_OSABI ELFOSABI_FREEBSD
18443
18444#undef elf64_bed
18445#define elf64_bed elf64_powerpc_fbsd_bed
18446
18447#include "elf64-target.h"