]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
e8e7cf2a 2 Copyright (C) 1999-2025 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
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
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
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.
5bd4f169 18
4ce794b7
AM
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.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
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 */
5bd4f169 27
c09c8b42
AM
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
4bd7c902 32
3db64b00 33#include "sysdep.h"
183e98be 34#include <stdarg.h>
5bd4f169 35#include "bfd.h"
5bd4f169
AM
36#include "bfdlink.h"
37#include "libbfd.h"
38#include "elf-bfd.h"
04c9666a 39#include "elf/ppc64.h"
5d1634d7 40#include "elf64-ppc.h"
58d180e8 41#include "dwarf2.h"
5bd4f169 42
bb294208
AM
43/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44#define OCTETS_PER_BYTE(ABFD, SEC) 1
45
805fc799 46static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
48static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 58static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 60static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
62static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 64static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 66static bfd_vma opd_entry_value
0a1b45a2 67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
5bd4f169 68
6d00b590 69#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 70#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 71#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
72#define TARGET_BIG_NAME "elf64-powerpc"
73#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 74#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
75#define ELF_MACHINE_CODE EM_PPC64
76#define ELF_MAXPAGESIZE 0x10000
702d1671 77#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
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
ad8e1ba5 84#define elf_backend_got_header_size 8
5474d94f 85#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
86#define elf_backend_can_gc_sections 1
87#define elf_backend_can_refcount 1
88#define elf_backend_rela_normal 1
64f52338 89#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 90#define elf_backend_default_execstack 0
ad8e1ba5 91
e717da7e 92#define bfd_elf64_mkobject ppc64_elf_mkobject
6313825c 93#define bfd_elf64_bfd_free_cached_info ppc64_elf_free_cached_info
ad8e1ba5 94#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 95#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
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
ad8e1ba5
AM
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
90e3cdf2 100#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 101#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 102#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
103
104#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
105#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
106#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 107#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 108#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 109#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 110#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 111#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 112#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 113#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 114#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 115#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 116#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 117#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 118#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
119#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
120#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 121#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
af969b14
AM
122#define elf_backend_early_size_sections ppc64_elf_edit
123#define elf_backend_late_size_sections ppc64_elf_late_size_sections
a345bc8d 124#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 125#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 126#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
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
754021d0 131#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 132#define elf_backend_special_sections ppc64_elf_special_sections
bf577467 133#define elf_backend_section_flags ppc64_elf_section_flags
6911b7dc 134#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 135#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 136#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 137
5bd4f169
AM
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. */
b9e5796b 143#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 144#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
145
146/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 147#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 148
a078d95a
AM
149/* Offsets to some stack save slots. */
150#define STK_LR 16
151#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 152/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
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
5bd4f169 157/* TOC base pointers offset from start of TOC. */
411e1bfb 158#define TOC_BASE_OFF 0x8000
a27e685f
AM
159/* TOC base alignment. */
160#define TOC_BASE_ALIGN 256
411e1bfb
AM
161
162/* Offset of tp and dtp pointers from start of TLS block. */
163#define TP_OFFSET 0x7000
164#define DTP_OFFSET 0x8000
5bd4f169 165
ad8e1ba5
AM
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
71a39c98 168 insert an addi to adjust r11. */
a078d95a 169#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
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) */
5d1634d7
AM
175#define BCTR 0x4e800420 /* bctr */
176
07d6d2b8 177#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
178#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
179#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
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+ */
794e51c0 190
71a39c98 191#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 192#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 193#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 194
07d6d2b8
AM
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 */
ad8e1ba5 198
04bdff6a 199#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 200#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 201#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 202#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
203#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
204#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 205#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 206#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 207#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 208#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 209#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 210#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 211#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
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 */
04bdff6a
AM
215#define PADDI_R12_PC 0x0610000039800000ULL
216#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 217#define PNOP 0x0700000000000000ULL
a345bc8d 218
3cd7c7d7
AM
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. */
9e390558 221#define GLINK_PLTRESOLVE_SIZE(htab) \
3cd7c7d7 222 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
ee4bf8d2
AM
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 */
71a39c98 230 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 231#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
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) */
ee4bf8d2 237 /* bctr */
3cd7c7d7
AM
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 */
5d1634d7
AM
246
247/* Pad with this. */
248#define NOP 0x60000000
249
721956f4
AM
250/* Some other nops. */
251#define CROR_151515 0x4def7b82
252#define CROR_313131 0x4ffffb82
253
cedb70c5 254/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
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 */
10ed1bba 261#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 262
deb0e272
AM
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) */
82bd7b59
AM
268#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
269#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
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 */
82bd7b59
AM
274#define BLR 0x4e800020 /* blr */
275
41bd81ab
AM
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
1e2f5b6e 279 relocate .opd without reloc entries. */
41bd81ab
AM
280#ifndef NO_OPD_RELOCS
281#define NO_OPD_RELOCS 0
282#endif
810d4e75 283
a4b6fadd
AM
284#ifndef ARRAY_SIZE
285#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
286#endif
287
810d4e75
AM
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}
6313825c
AM
300
301#define is_ppc64_elf(bfd) \
302 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
303 && elf_object_id (bfd) == PPC64_ELF_DATA)
5bd4f169
AM
304\f
305/* Relocation HOWTO's. */
46807bf4
AM
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, \
0a1b45a2 317 #type, false, 0, mask, pc_relative)
46807bf4 318
04c9666a 319static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 320
f3185997
NC
321static reloc_howto_type ppc64_elf_howto_raw[] =
322{
5bd4f169 323 /* This reloc does nothing. */
c94cb026 324 HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
46807bf4 325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* A standard 32 bit relocation. */
c94cb026 328 HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
46807bf4 329 bfd_elf_generic_reloc),
5bd4f169
AM
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. */
c94cb026 333 HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
46807bf4 334 bfd_elf_generic_reloc),
5bd4f169
AM
335
336 /* A standard 16 bit relocation. */
c94cb026 337 HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
46807bf4 338 bfd_elf_generic_reloc),
5bd4f169
AM
339
340 /* A 16 bit relocation without overflow. */
c94cb026 341 HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 342 bfd_elf_generic_reloc),
5bd4f169
AM
343
344 /* Bits 16-31 of an address. */
c94cb026 345 HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 346 bfd_elf_generic_reloc),
5bd4f169
AM
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. */
c94cb026 350 HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 351 ppc64_elf_ha_reloc),
5bd4f169
AM
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. */
c94cb026 355 HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
46807bf4 356 ppc64_elf_branch_reloc),
5bd4f169
AM
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. */
c94cb026 361 HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
46807bf4 362 ppc64_elf_brtaken_reloc),
5bd4f169
AM
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. */
c94cb026 367 HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
46807bf4 368 ppc64_elf_brtaken_reloc),
5bd4f169
AM
369
370 /* A relative 26 bit branch; the lower two bits must be zero. */
c94cb026 371 HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
46807bf4 372 ppc64_elf_branch_reloc),
5bd4f169 373
05d0e962 374 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
c94cb026 375 HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
46807bf4 376 ppc64_elf_branch_reloc),
05d0e962 377
7aba54da 378 /* Another variant, when p10 insns can't be used on stubs. */
c94cb026 379 HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
7aba54da
AM
380 ppc64_elf_branch_reloc),
381
5bd4f169 382 /* A relative 16 bit branch; the lower two bits must be zero. */
c94cb026 383 HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
46807bf4 384 ppc64_elf_branch_reloc),
5bd4f169
AM
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. */
c94cb026 389 HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
46807bf4 390 ppc64_elf_brtaken_reloc),
5bd4f169
AM
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. */
c94cb026 395 HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
46807bf4 396 ppc64_elf_brtaken_reloc),
5bd4f169
AM
397
398 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
399 symbol. */
c94cb026 400 HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
46807bf4 401 ppc64_elf_unhandled_reloc),
5bd4f169
AM
402
403 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
404 the symbol. */
c94cb026 405 HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 406 ppc64_elf_unhandled_reloc),
5bd4f169
AM
407
408 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
409 the symbol. */
c94cb026 410 HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 411 ppc64_elf_unhandled_reloc),
5bd4f169
AM
412
413 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
414 the symbol. */
c94cb026 415 HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 416 ppc64_elf_unhandled_reloc),
5bd4f169
AM
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. */
5d0feb98 423 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
46807bf4 424 ppc64_elf_unhandled_reloc),
5bd4f169
AM
425
426 /* Like R_PPC64_ADDR64, but used when setting global offset table
427 entries. */
c94cb026 428 HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 429 ppc64_elf_unhandled_reloc),
5bd4f169
AM
430
431 /* Created by the link editor. Marks a procedure linkage table
432 entry for a symbol. */
5d0feb98 433 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
46807bf4 434 ppc64_elf_unhandled_reloc),
5bd4f169
AM
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. */
c94cb026 439 HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 440 bfd_elf_generic_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR32, but may be unaligned. */
c94cb026 443 HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
46807bf4 444 bfd_elf_generic_reloc),
5bd4f169
AM
445
446 /* Like R_PPC64_ADDR16, but may be unaligned. */
c94cb026 447 HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
46807bf4 448 bfd_elf_generic_reloc),
5bd4f169
AM
449
450 /* 32-bit PC relative. */
c94cb026 451 HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
46807bf4 452 bfd_elf_generic_reloc),
5bd4f169 453
10ed1bba 454 /* 32-bit relocation to the symbol's procedure linkage table. */
c94cb026 455 HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
46807bf4 456 ppc64_elf_unhandled_reloc),
5bd4f169
AM
457
458 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
459 FIXME: R_PPC64_PLTREL32 not supported. */
c94cb026 460 HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
46807bf4 461 ppc64_elf_unhandled_reloc),
5bd4f169
AM
462
463 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
464 the symbol. */
c94cb026 465 HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 466 ppc64_elf_unhandled_reloc),
5bd4f169
AM
467
468 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
469 the symbol. */
c94cb026 470 HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 471 ppc64_elf_unhandled_reloc),
5bd4f169
AM
472
473 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
474 the symbol. */
c94cb026 475 HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 476 ppc64_elf_unhandled_reloc),
5bd4f169 477
c061c2d8 478 /* 16-bit section relative relocation. */
c94cb026 479 HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
46807bf4 480 ppc64_elf_sectoff_reloc),
5bd4f169 481
c061c2d8 482 /* Like R_PPC64_SECTOFF, but no overflow warning. */
c94cb026 483 HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 484 ppc64_elf_sectoff_reloc),
5bd4f169
AM
485
486 /* 16-bit upper half section relative relocation. */
c94cb026 487 HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 488 ppc64_elf_sectoff_reloc),
5bd4f169
AM
489
490 /* 16-bit upper half adjusted section relative relocation. */
c94cb026 491 HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 492 ppc64_elf_sectoff_ha_reloc),
5bd4f169 493
04c9666a 494 /* Like R_PPC64_REL24 without touching the two least significant bits. */
c94cb026 495 HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
46807bf4 496 bfd_elf_generic_reloc),
5bd4f169
AM
497
498 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
499
500 /* A standard 64-bit relocation. */
c94cb026 501 HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 502 bfd_elf_generic_reloc),
5bd4f169
AM
503
504 /* The bits 32-47 of an address. */
c94cb026 505 HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
46807bf4 506 bfd_elf_generic_reloc),
5bd4f169
AM
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. */
c94cb026 510 HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
46807bf4 511 ppc64_elf_ha_reloc),
5bd4f169
AM
512
513 /* The bits 48-63 of an address. */
c94cb026 514 HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
46807bf4 515 bfd_elf_generic_reloc),
5bd4f169
AM
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. */
c94cb026 519 HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
46807bf4 520 ppc64_elf_ha_reloc),
5bd4f169
AM
521
522 /* Like ADDR64, but may be unaligned. */
c94cb026 523 HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 524 bfd_elf_generic_reloc),
5bd4f169
AM
525
526 /* 64-bit relative relocation. */
c94cb026 527 HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 528 bfd_elf_generic_reloc),
5bd4f169 529
cedb70c5 530 /* 64-bit relocation to the symbol's procedure linkage table. */
c94cb026 531 HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 532 ppc64_elf_unhandled_reloc),
5bd4f169
AM
533
534 /* 64-bit PC relative relocation to the symbol's procedure linkage
535 table. */
536 /* FIXME: R_PPC64_PLTREL64 not supported. */
c94cb026 537 HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 538 ppc64_elf_unhandled_reloc),
5bd4f169
AM
539
540 /* 16 bit TOC-relative relocation. */
5bd4f169 541 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
c94cb026 542 HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
46807bf4 543 ppc64_elf_toc_reloc),
5bd4f169
AM
544
545 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 546 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
c94cb026 547 HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 548 ppc64_elf_toc_reloc),
5bd4f169
AM
549
550 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 551 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
c94cb026 552 HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 553 ppc64_elf_toc_reloc),
5bd4f169
AM
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. */
5bd4f169 558 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
c94cb026 559 HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 560 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
561
562 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 563 /* R_PPC64_TOC 51 doubleword64 .TOC. */
c94cb026 564 HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 565 ppc64_elf_toc64_reloc),
5bd4f169
AM
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. */
c94cb026 576 HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
46807bf4 577 ppc64_elf_unhandled_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_PLTGOT16, but without overflow. */
580 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
c94cb026 581 HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 582 ppc64_elf_unhandled_reloc),
411e1bfb
AM
583
584 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
585 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
c94cb026 586 HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 587 ppc64_elf_unhandled_reloc),
411e1bfb
AM
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. */
c94cb026 593 HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 594 ppc64_elf_unhandled_reloc),
411e1bfb
AM
595
596 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
c94cb026 597 HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 598 bfd_elf_generic_reloc),
411e1bfb
AM
599
600 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
c94cb026 601 HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 602 bfd_elf_generic_reloc),
411e1bfb
AM
603
604 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
c94cb026 605 HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 606 ppc64_elf_unhandled_reloc),
411e1bfb
AM
607
608 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
c94cb026 609 HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 610 ppc64_elf_unhandled_reloc),
411e1bfb
AM
611
612 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
c94cb026 613 HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 614 ppc64_elf_unhandled_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
c94cb026 617 HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 618 ppc64_elf_sectoff_reloc),
411e1bfb
AM
619
620 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
c94cb026 621 HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 622 ppc64_elf_sectoff_reloc),
411e1bfb
AM
623
624 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
c94cb026 625 HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 626 ppc64_elf_toc_reloc),
411e1bfb
AM
627
628 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
c94cb026 629 HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 630 ppc64_elf_toc_reloc),
411e1bfb
AM
631
632 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
633 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
c94cb026 634 HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 635 ppc64_elf_unhandled_reloc),
411e1bfb
AM
636
637 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
638 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
c94cb026 639 HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 640 ppc64_elf_unhandled_reloc),
411e1bfb 641
727fc41e 642 /* Marker relocs for TLS. */
c94cb026 643 HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
46807bf4
AM
644 bfd_elf_generic_reloc),
645
c94cb026 646 HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
46807bf4
AM
647 bfd_elf_generic_reloc),
648
c94cb026 649 HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
46807bf4 650 bfd_elf_generic_reloc),
727fc41e 651
23cedd1d
AM
652 /* Marker reloc for optimizing r2 save in prologue rather than on
653 each plt call stub. */
c94cb026 654 HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
46807bf4 655 bfd_elf_generic_reloc),
3b421ab3 656
23cedd1d 657 /* Marker relocs on inline plt call instructions. */
c94cb026 658 HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
46807bf4
AM
659 bfd_elf_generic_reloc),
660
c94cb026 661 HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
46807bf4 662 bfd_elf_generic_reloc),
23cedd1d 663
411e1bfb
AM
664 /* Computes the load module index of the load module that contains the
665 definition of its TLS sym. */
c94cb026 666 HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 667 ppc64_elf_unhandled_reloc),
411e1bfb
AM
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. */
c94cb026 672 HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 673 ppc64_elf_unhandled_reloc),
411e1bfb
AM
674
675 /* A 16 bit dtprel reloc. */
c94cb026 676 HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
46807bf4 677 ppc64_elf_unhandled_reloc),
411e1bfb
AM
678
679 /* Like DTPREL16, but no overflow. */
c94cb026 680 HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 681 ppc64_elf_unhandled_reloc),
411e1bfb
AM
682
683 /* Like DTPREL16_LO, but next higher group of 16 bits. */
c94cb026 684 HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 685 ppc64_elf_unhandled_reloc),
411e1bfb
AM
686
687 /* Like DTPREL16_HI, but adjust for low 16 bits. */
c94cb026 688 HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 689 ppc64_elf_unhandled_reloc),
411e1bfb
AM
690
691 /* Like DTPREL16_HI, but next higher group of 16 bits. */
c94cb026 692 HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
46807bf4 693 ppc64_elf_unhandled_reloc),
411e1bfb
AM
694
695 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
c94cb026 696 HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
46807bf4 697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
c94cb026 700 HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
46807bf4 701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
c94cb026 704 HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
46807bf4 705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like DTPREL16, but for insns with a DS field. */
c94cb026 708 HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Like DTPREL16_DS, but no overflow. */
c94cb026 712 HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* Computes a tp-relative displacement, the difference between the value of
716 sym+add and the value of the thread pointer (r13). */
c94cb026 717 HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 718 ppc64_elf_unhandled_reloc),
411e1bfb
AM
719
720 /* A 16 bit tprel reloc. */
c94cb026 721 HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
46807bf4 722 ppc64_elf_unhandled_reloc),
411e1bfb
AM
723
724 /* Like TPREL16, but no overflow. */
c94cb026 725 HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 726 ppc64_elf_unhandled_reloc),
411e1bfb
AM
727
728 /* Like TPREL16_LO, but next higher group of 16 bits. */
c94cb026 729 HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 730 ppc64_elf_unhandled_reloc),
411e1bfb
AM
731
732 /* Like TPREL16_HI, but adjust for low 16 bits. */
c94cb026 733 HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 734 ppc64_elf_unhandled_reloc),
411e1bfb
AM
735
736 /* Like TPREL16_HI, but next higher group of 16 bits. */
c94cb026 737 HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
46807bf4 738 ppc64_elf_unhandled_reloc),
411e1bfb
AM
739
740 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
c94cb026 741 HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
46807bf4 742 ppc64_elf_unhandled_reloc),
411e1bfb
AM
743
744 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
c94cb026 745 HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
46807bf4 746 ppc64_elf_unhandled_reloc),
411e1bfb
AM
747
748 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
c94cb026 749 HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
46807bf4 750 ppc64_elf_unhandled_reloc),
411e1bfb
AM
751
752 /* Like TPREL16, but for insns with a DS field. */
c94cb026 753 HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 754 ppc64_elf_unhandled_reloc),
411e1bfb
AM
755
756 /* Like TPREL16_DS, but no overflow. */
c94cb026 757 HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 758 ppc64_elf_unhandled_reloc),
411e1bfb
AM
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). */
c94cb026 763 HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
46807bf4 764 ppc64_elf_unhandled_reloc),
5bd4f169 765
411e1bfb 766 /* Like GOT_TLSGD16, but no overflow. */
c94cb026 767 HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 768 ppc64_elf_unhandled_reloc),
5bd4f169 769
411e1bfb 770 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
c94cb026 771 HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 772 ppc64_elf_unhandled_reloc),
5bd4f169 773
411e1bfb 774 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
c94cb026 775 HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 776 ppc64_elf_unhandled_reloc),
5bd4f169 777
411e1bfb
AM
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). */
c94cb026 781 HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
46807bf4 782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_TLSLD16, but no overflow. */
c94cb026 785 HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
c94cb026 789 HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 790 ppc64_elf_unhandled_reloc),
5bd4f169 791
411e1bfb 792 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
c94cb026 793 HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 794 ppc64_elf_unhandled_reloc),
5bd4f169 795
411e1bfb
AM
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). */
c94cb026 798 HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_DTPREL16_DS, but no overflow. */
c94cb026 802 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
c94cb026 806 HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 807 ppc64_elf_unhandled_reloc),
5bd4f169 808
411e1bfb 809 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
c94cb026 810 HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 811 ppc64_elf_unhandled_reloc),
411e1bfb
AM
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). */
c94cb026 815 HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 816 ppc64_elf_unhandled_reloc),
5bd4f169 817
411e1bfb 818 /* Like GOT_TPREL16_DS, but no overflow. */
c94cb026 819 HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 820 ppc64_elf_unhandled_reloc),
5bd4f169 821
411e1bfb 822 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
c94cb026 823 HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 824 ppc64_elf_unhandled_reloc),
5bd4f169 825
411e1bfb 826 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
c94cb026 827 HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4
AM
828 ppc64_elf_unhandled_reloc),
829
5d0feb98 830 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
46807bf4
AM
831 ppc64_elf_unhandled_reloc),
832
c94cb026 833 HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 834 bfd_elf_generic_reloc),
e054468f 835
25f23106 836 /* A 16 bit relative relocation. */
c94cb026 837 HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
46807bf4 838 bfd_elf_generic_reloc),
25f23106
AM
839
840 /* A 16 bit relative relocation without overflow. */
c94cb026 841 HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
46807bf4 842 bfd_elf_generic_reloc),
25f23106
AM
843
844 /* The high order 16 bits of a relative address. */
c94cb026 845 HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
46807bf4 846 bfd_elf_generic_reloc),
25f23106
AM
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. */
c94cb026 850 HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
46807bf4 851 ppc64_elf_ha_reloc),
25f23106 852
c94cb026 853 HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
4a969973
AM
854 bfd_elf_generic_reloc),
855
c94cb026 856 HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
4a969973
AM
857 ppc64_elf_ha_reloc),
858
c94cb026 859 HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
4a969973
AM
860 bfd_elf_generic_reloc),
861
c94cb026 862 HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
4a969973
AM
863 ppc64_elf_ha_reloc),
864
c94cb026 865 HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
4a969973
AM
866 bfd_elf_generic_reloc),
867
c94cb026 868 HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
4a969973
AM
869 ppc64_elf_ha_reloc),
870
a680de9a 871 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
c94cb026 872 HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
46807bf4 873 ppc64_elf_ha_reloc),
a680de9a 874
7ba71655 875 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
c94cb026 876 HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
46807bf4 877 ppc64_elf_ha_reloc),
7ba71655 878
f9c6b907 879 /* Like R_PPC64_ADDR16_HI, but no overflow. */
c94cb026 880 HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
46807bf4 881 bfd_elf_generic_reloc),
f9c6b907
AM
882
883 /* Like R_PPC64_ADDR16_HA, but no overflow. */
c94cb026 884 HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
46807bf4 885 ppc64_elf_ha_reloc),
f9c6b907
AM
886
887 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
c94cb026 888 HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
46807bf4 889 ppc64_elf_unhandled_reloc),
f9c6b907
AM
890
891 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
c94cb026 892 HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
46807bf4 893 ppc64_elf_unhandled_reloc),
f9c6b907
AM
894
895 /* Like R_PPC64_TPREL16_HI, but no overflow. */
c94cb026 896 HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
46807bf4 897 ppc64_elf_unhandled_reloc),
f9c6b907
AM
898
899 /* Like R_PPC64_TPREL16_HA, but no overflow. */
c94cb026 900 HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
46807bf4 901 ppc64_elf_unhandled_reloc),
f9c6b907 902
006589cf 903 /* Marker reloc on ELFv2 large-model function entry. */
c94cb026 904 HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
46807bf4 905 bfd_elf_generic_reloc),
006589cf 906
45965137 907 /* Like ADDR64, but use local entry point of function. */
c94cb026 908 HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 909 bfd_elf_generic_reloc),
45965137 910
c94cb026 911 HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
5663e321
AM
912 bfd_elf_generic_reloc),
913
c94cb026 914 HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
5663e321
AM
915 bfd_elf_generic_reloc),
916
c94cb026 917 HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
5663e321
AM
918 bfd_elf_generic_reloc),
919
c94cb026 920 HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
5663e321
AM
921 ppc64_elf_prefix_reloc),
922
c94cb026 923 HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
5663e321
AM
924 ppc64_elf_prefix_reloc),
925
c94cb026 926 HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
927 ppc64_elf_prefix_reloc),
928
c94cb026 929 HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
930 ppc64_elf_prefix_reloc),
931
c94cb026 932 HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
933 ppc64_elf_prefix_reloc),
934
c94cb026 935 HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
936 ppc64_elf_unhandled_reloc),
937
c94cb026 938 HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
939 ppc64_elf_unhandled_reloc),
940
c94cb026 941 HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
942 ppc64_elf_unhandled_reloc),
943
c94cb026 944 HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
945 ppc64_elf_unhandled_reloc),
946
c94cb026 947 HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
948 ppc64_elf_unhandled_reloc),
949
c94cb026 950 HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
951 ppc64_elf_unhandled_reloc),
952
c94cb026 953 HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
954 ppc64_elf_unhandled_reloc),
955
c94cb026 956 HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
957 ppc64_elf_unhandled_reloc),
958
c94cb026 959 HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
960 ppc64_elf_unhandled_reloc),
961
c94cb026 962 HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
5663e321
AM
963 bfd_elf_generic_reloc),
964
c94cb026 965 HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
5663e321
AM
966 ppc64_elf_ha_reloc),
967
c94cb026 968 HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
5663e321
AM
969 bfd_elf_generic_reloc),
970
c94cb026 971 HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
5663e321
AM
972 ppc64_elf_ha_reloc),
973
c94cb026 974 HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
5663e321
AM
975 bfd_elf_generic_reloc),
976
c94cb026 977 HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
5663e321
AM
978 ppc64_elf_ha_reloc),
979
c94cb026 980 HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
5663e321
AM
981 bfd_elf_generic_reloc),
982
c94cb026 983 HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
5663e321
AM
984 ppc64_elf_ha_reloc),
985
c94cb026 986 HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
5663e321
AM
987 ppc64_elf_prefix_reloc),
988
c94cb026 989 HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
5663e321
AM
990 ppc64_elf_prefix_reloc),
991
5bd4f169 992 /* GNU extension to record C++ vtable hierarchy. */
5d0feb98 993 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
46807bf4 994 NULL),
5bd4f169
AM
995
996 /* GNU extension to record C++ vtable member usage. */
5d0feb98 997 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
46807bf4 998 NULL),
5bd4f169
AM
999};
1000
1001\f
1002/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1003 be done. */
1004
1005static void
4ce794b7 1006ppc_howto_init (void)
5bd4f169
AM
1007{
1008 unsigned int i, type;
1009
a4b6fadd 1010 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
1011 {
1012 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 1013 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
1014 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1015 }
1016}
1017
1018static reloc_howto_type *
87c69f97 1019ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
5bd4f169 1020{
411e1bfb 1021 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1022
1023 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1024 /* Initialize howto table if needed. */
1025 ppc_howto_init ();
1026
4ce794b7 1027 switch (code)
5bd4f169
AM
1028 {
1029 default:
f3185997 1030 /* xgettext:c-format */
2cdcc330
AM
1031 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1032 (int) code);
f3185997 1033 bfd_set_error (bfd_error_bad_value);
4ce794b7 1034 return NULL;
5bd4f169 1035
411e1bfb
AM
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;
f9c6b907
AM
1048 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1049 break;
411e1bfb 1050 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1051 break;
f9c6b907
AM
1052 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1053 break;
411e1bfb 1054 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1061 break;
05d0e962
AM
1062 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1063 break;
7aba54da
AM
1064 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1065 break;
411e1bfb 1066 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1139 break;
411e1bfb 1140 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1141 break;
411e1bfb 1142 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1143 break;
411e1bfb 1144 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1145 break;
411e1bfb 1146 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1147 break;
411e1bfb 1148 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1149 break;
411e1bfb 1150 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1151 break;
411e1bfb 1152 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1153 break;
411e1bfb 1154 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1155 break;
411e1bfb 1156 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1157 break;
411e1bfb 1158 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1159 break;
411e1bfb 1160 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1161 break;
c213164a 1162 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1163 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1164 break;
727fc41e
AM
1165 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1166 break;
1167 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1168 break;
411e1bfb 1169 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1170 break;
411e1bfb 1171 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1172 break;
411e1bfb 1173 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1174 break;
411e1bfb 1175 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1176 break;
f9c6b907
AM
1177 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1178 break;
411e1bfb 1179 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1180 break;
f9c6b907
AM
1181 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1182 break;
411e1bfb 1183 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1184 break;
411e1bfb
AM
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;
f9c6b907
AM
1191 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1192 break;
411e1bfb
AM
1193 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1194 break;
f9c6b907
AM
1195 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1196 break;
411e1bfb
AM
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;
25f23106
AM
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;
4a969973
AM
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;
7ba71655
AM
1275 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1276 break;
a680de9a
PB
1277 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1278 break;
006589cf
AM
1279 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1280 break;
45965137
AM
1281 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1282 break;
5663e321
AM
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;
c213164a
AM
1297 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1298 break;
1299 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1300 break;
87c69f97 1301 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
c213164a 1302 break;
87c69f97 1303 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
c213164a 1304 break;
87c69f97 1305 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
c213164a 1306 break;
87c69f97 1307 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
c213164a 1308 break;
5663e321
AM
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;
411e1bfb
AM
1329 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1330 break;
1331 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1332 break;
1333 }
1334
4ce794b7 1335 return ppc64_elf_howto_table[r];
5bd4f169
AM
1336};
1337
157090f7 1338static reloc_howto_type *
87c69f97 1339ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
157090f7
AM
1340{
1341 unsigned int i;
87c69f97
AM
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 };
157090f7 1348
a4b6fadd 1349 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
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
87c69f97
AM
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
157090f7
AM
1366 return NULL;
1367}
1368
5bd4f169
AM
1369/* Set the howto pointer for a PowerPC ELF reloc. */
1370
0a1b45a2 1371static bool
4aef7643 1372ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1373 Elf_Internal_Rela *dst)
5bd4f169 1374{
65f38f15
AM
1375 unsigned int type;
1376
ef60b7ff 1377 /* Initialize howto table if needed. */
5bd4f169 1378 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1379 ppc_howto_init ();
1380
65f38f15 1381 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1382 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1383 {
695344c0 1384 /* xgettext:c-format */
0aa13fee 1385 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1386 abfd, type);
f3185997 1387 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1388 return false;
d0fb9a8d 1389 }
65f38f15 1390 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1391 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1392 {
1393 /* xgettext:c-format */
1394 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1395 abfd, type);
1396 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1397 return false;
f3185997 1398 }
2cdcc330 1399
0a1b45a2 1400 return true;
5bd4f169
AM
1401}
1402
04c9666a 1403/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1404
1405static bfd_reloc_status_type
4ce794b7
AM
1406ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1407 void *data, asection *input_section,
1408 bfd *output_bfd, char **error_message)
5bd4f169 1409{
a680de9a
PB
1410 enum elf_ppc64_reloc_type r_type;
1411 long insn;
1412 bfd_size_type octets;
3de43e7b 1413 bfd_vma value;
a680de9a 1414
805fc799
AM
1415 /* If this is a relocatable link (output_bfd test tells us), just
1416 call the generic function. Any adjustment will be done at final
1417 link time. */
1418 if (output_bfd != NULL)
cedb70c5 1419 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1420 input_section, output_bfd, error_message);
1421
5663e321
AM
1422 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1423 We won't actually be using the low bits, so trashing them
805fc799 1424 doesn't matter. */
a680de9a 1425 r_type = reloc_entry->howto->type;
5663e321
AM
1426 if (r_type == R_PPC64_ADDR16_HIGHERA34
1427 || r_type == R_PPC64_ADDR16_HIGHESTA34
1428 || r_type == R_PPC64_REL16_HIGHERA34
1429 || r_type == R_PPC64_REL16_HIGHESTA34)
1430 reloc_entry->addend += 1ULL << 33;
1431 else
1432 reloc_entry->addend += 1U << 15;
a680de9a
PB
1433 if (r_type != R_PPC64_REL16DX_HA)
1434 return bfd_reloc_continue;
1435
1436 value = 0;
1437 if (!bfd_is_com_section (symbol->section))
1438 value = symbol->value;
1439 value += (reloc_entry->addend
1440 + symbol->section->output_offset
1441 + symbol->section->output_section->vma);
1442 value -= (reloc_entry->address
1443 + input_section->output_offset
1444 + input_section->output_section->vma);
3de43e7b 1445 value = (bfd_signed_vma) value >> 16;
a680de9a 1446
bb294208 1447 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1448 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1449 input_section, octets))
1450 return bfd_reloc_outofrange;
1451
a680de9a
PB
1452 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1453 insn &= ~0x1fffc1;
3de43e7b 1454 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1455 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1456 if (value + 0x8000 > 0xffff)
a680de9a
PB
1457 return bfd_reloc_overflow;
1458 return bfd_reloc_ok;
805fc799 1459}
5bd4f169 1460
2441e016
AM
1461static bfd_reloc_status_type
1462ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1463 void *data, asection *input_section,
1464 bfd *output_bfd, char **error_message)
1465{
1466 if (output_bfd != NULL)
1467 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1468 input_section, output_bfd, error_message);
1469
6313825c
AM
1470 if (symbol->section->owner == NULL
1471 || !is_ppc64_elf (symbol->section->owner))
1472 return bfd_reloc_continue;
1473
699733f6
AM
1474 if (strcmp (symbol->section->name, ".opd") == 0
1475 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1476 {
1477 bfd_vma dest = opd_entry_value (symbol->section,
1478 symbol->value + reloc_entry->addend,
0a1b45a2 1479 NULL, NULL, false);
2441e016
AM
1480 if (dest != (bfd_vma) -1)
1481 reloc_entry->addend = dest - (symbol->value
1482 + symbol->section->output_section->vma
1483 + symbol->section->output_offset);
1484 }
810d4e75
AM
1485 else
1486 {
1487 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1488
1489 if (symbol->section->owner != abfd
1490 && abiversion (symbol->section->owner) >= 2)
1491 {
1492 unsigned int i;
1493
1494 for (i = 0; i < symbol->section->owner->symcount; ++i)
1495 {
1496 asymbol *symdef = symbol->section->owner->outsymbols[i];
1497
1498 if (strcmp (symdef->name, symbol->name) == 0)
1499 {
1500 elfsym = (elf_symbol_type *) symdef;
1501 break;
1502 }
1503 }
1504 }
1505 reloc_entry->addend
1506 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1507 }
2441e016
AM
1508 return bfd_reloc_continue;
1509}
1510
805fc799 1511static bfd_reloc_status_type
4ce794b7
AM
1512ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1513 void *data, asection *input_section,
1514 bfd *output_bfd, char **error_message)
805fc799
AM
1515{
1516 long insn;
04c9666a 1517 enum elf_ppc64_reloc_type r_type;
805fc799 1518 bfd_size_type octets;
794e51c0 1519 /* Assume 'at' branch hints. */
0a1b45a2 1520 bool is_isa_v2 = true;
805fc799
AM
1521
1522 /* If this is a relocatable link (output_bfd test tells us), just
1523 call the generic function. Any adjustment will be done at final
1524 link time. */
5bd4f169 1525 if (output_bfd != NULL)
cedb70c5 1526 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1527 input_section, output_bfd, error_message);
1528
bb294208 1529 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1530 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1531 input_section, octets))
1532 return bfd_reloc_outofrange;
1533
805fc799
AM
1534 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1535 insn &= ~(0x01 << 21);
4ce794b7 1536 r_type = reloc_entry->howto->type;
805fc799
AM
1537 if (r_type == R_PPC64_ADDR14_BRTAKEN
1538 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1539 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1540
794e51c0 1541 if (is_isa_v2)
5bd4f169 1542 {
805fc799
AM
1543 /* Set 'a' bit. This is 0b00010 in BO field for branch
1544 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1545 for branch on CTR insns (BO == 1a00t or 1a01t). */
1546 if ((insn & (0x14 << 21)) == (0x04 << 21))
1547 insn |= 0x02 << 21;
1548 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1549 insn |= 0x08 << 21;
1550 else
2441e016 1551 goto out;
5bd4f169 1552 }
805fc799
AM
1553 else
1554 {
1555 bfd_vma target = 0;
1556 bfd_vma from;
5bd4f169 1557
805fc799
AM
1558 if (!bfd_is_com_section (symbol->section))
1559 target = symbol->value;
1560 target += symbol->section->output_section->vma;
1561 target += symbol->section->output_offset;
1562 target += reloc_entry->addend;
5bd4f169 1563
805fc799
AM
1564 from = (reloc_entry->address
1565 + input_section->output_offset
1566 + input_section->output_section->vma);
5bd4f169 1567
805fc799
AM
1568 /* Invert 'y' bit if not the default. */
1569 if ((bfd_signed_vma) (target - from) < 0)
1570 insn ^= 0x01 << 21;
1571 }
4ce794b7 1572 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1573 out:
1574 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1575 input_section, output_bfd, error_message);
805fc799 1576}
5bd4f169 1577
805fc799 1578static bfd_reloc_status_type
4ce794b7
AM
1579ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1580 void *data, asection *input_section,
1581 bfd *output_bfd, char **error_message)
805fc799
AM
1582{
1583 /* If this is a relocatable link (output_bfd test tells us), just
1584 call the generic function. Any adjustment will be done at final
1585 link time. */
1586 if (output_bfd != NULL)
cedb70c5 1587 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1588 input_section, output_bfd, error_message);
5bd4f169 1589
805fc799
AM
1590 /* Subtract the symbol section base address. */
1591 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1592 return bfd_reloc_continue;
1593}
1594
805fc799 1595static bfd_reloc_status_type
4ce794b7
AM
1596ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1597 void *data, asection *input_section,
1598 bfd *output_bfd, char **error_message)
805fc799
AM
1599{
1600 /* If this is a relocatable link (output_bfd test tells us), just
1601 call the generic function. Any adjustment will be done at final
1602 link time. */
1603 if (output_bfd != NULL)
cedb70c5 1604 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1605 input_section, output_bfd, error_message);
1606
1607 /* Subtract the symbol section base address. */
1608 reloc_entry->addend -= symbol->section->output_section->vma;
1609
1610 /* Adjust the addend for sign extension of the low 16 bits. */
1611 reloc_entry->addend += 0x8000;
1612 return bfd_reloc_continue;
1613}
1614
1615static bfd_reloc_status_type
4ce794b7
AM
1616ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1617 void *data, asection *input_section,
1618 bfd *output_bfd, char **error_message)
805fc799
AM
1619{
1620 bfd_vma TOCstart;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
cedb70c5 1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1c865ab2 1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1632
1633 /* Subtract the TOC base address. */
1634 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1635 return bfd_reloc_continue;
1636}
1637
1638static bfd_reloc_status_type
4ce794b7
AM
1639ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
805fc799
AM
1642{
1643 bfd_vma TOCstart;
1644
1645 /* If this is a relocatable link (output_bfd test tells us), just
1646 call the generic function. Any adjustment will be done at final
1647 link time. */
1648 if (output_bfd != NULL)
cedb70c5 1649 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1650 input_section, output_bfd, error_message);
1651
1652 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1653 if (TOCstart == 0)
1c865ab2 1654 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1655
1656 /* Subtract the TOC base address. */
1657 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1658
1659 /* Adjust the addend for sign extension of the low 16 bits. */
1660 reloc_entry->addend += 0x8000;
1661 return bfd_reloc_continue;
1662}
1663
1664static bfd_reloc_status_type
4ce794b7
AM
1665ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1666 void *data, asection *input_section,
1667 bfd *output_bfd, char **error_message)
805fc799
AM
1668{
1669 bfd_vma TOCstart;
1670 bfd_size_type octets;
1671
1672 /* If this is a relocatable link (output_bfd test tells us), just
1673 call the generic function. Any adjustment will be done at final
1674 link time. */
1675 if (output_bfd != NULL)
cedb70c5 1676 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1677 input_section, output_bfd, error_message);
1678
b54509b8
AM
1679 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1680 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1681 input_section, octets))
1682 return bfd_reloc_outofrange;
1683
805fc799
AM
1684 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1685 if (TOCstart == 0)
1c865ab2 1686 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799 1687
805fc799
AM
1688 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1689 return bfd_reloc_ok;
1690}
1691
5663e321
AM
1692static bfd_reloc_status_type
1693ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1694 void *data, asection *input_section,
1695 bfd *output_bfd, char **error_message)
1696{
1697 uint64_t insn;
1698 bfd_vma targ;
b54509b8 1699 bfd_size_type octets;
5663e321
AM
1700
1701 if (output_bfd != NULL)
1702 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1703 input_section, output_bfd, error_message);
1704
b54509b8
AM
1705 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1706 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1707 input_section, octets))
1708 return bfd_reloc_outofrange;
1709
1710 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
5663e321 1711 insn <<= 32;
b54509b8 1712 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
5663e321
AM
1713
1714 targ = (symbol->section->output_section->vma
1715 + symbol->section->output_offset
1716 + reloc_entry->addend);
1717 if (!bfd_is_com_section (symbol->section))
1718 targ += symbol->value;
1719 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1720 targ += 1ULL << 33;
1721 if (reloc_entry->howto->pc_relative)
1722 {
1723 bfd_vma from = (reloc_entry->address
1724 + input_section->output_offset
1725 + input_section->output_section->vma);
1726 targ -=from;
1727 }
1728 targ >>= reloc_entry->howto->rightshift;
1729 insn &= ~reloc_entry->howto->dst_mask;
1730 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
b54509b8
AM
1731 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1732 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
5663e321
AM
1733 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1734 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1735 >= 1ULL << reloc_entry->howto->bitsize))
1736 return bfd_reloc_overflow;
1737 return bfd_reloc_ok;
1738}
1739
805fc799 1740static bfd_reloc_status_type
4ce794b7
AM
1741ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1742 void *data, asection *input_section,
1743 bfd *output_bfd, char **error_message)
805fc799
AM
1744{
1745 /* If this is a relocatable link (output_bfd test tells us), just
1746 call the generic function. Any adjustment will be done at final
1747 link time. */
1748 if (output_bfd != NULL)
cedb70c5 1749 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1750 input_section, output_bfd, error_message);
1751
1752 if (error_message != NULL)
6f860418
AM
1753 *error_message = bfd_asprintf (_("generic linker can't handle %s"),
1754 reloc_entry->howto->name);
805fc799
AM
1755 return bfd_reloc_dangerous;
1756}
1757
927be08e
AM
1758/* Track GOT entries needed for a given symbol. We might need more
1759 than one got entry per symbol. */
1760struct got_entry
1761{
1762 struct got_entry *next;
1763
1764 /* The symbol addend that we'll be placing in the GOT. */
1765 bfd_vma addend;
1766
1767 /* Unlike other ELF targets, we use separate GOT entries for the same
1768 symbol referenced from different input files. This is to support
1769 automatic multiple TOC/GOT sections, where the TOC base can vary
1770 from one input file to another. After partitioning into TOC groups
1771 we merge entries within the group.
1772
1773 Point to the BFD owning this GOT entry. */
1774 bfd *owner;
1775
1776 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1777 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1778 unsigned char tls_type;
927be08e
AM
1779
1780 /* Non-zero if got.ent points to real entry. */
f961d9dd 1781 unsigned char is_indirect;
927be08e
AM
1782
1783 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1784 union
2cdcc330
AM
1785 {
1786 bfd_signed_vma refcount;
1787 bfd_vma offset;
1788 struct got_entry *ent;
1789 } got;
927be08e
AM
1790};
1791
1792/* The same for PLT. */
1793struct plt_entry
1794{
1795 struct plt_entry *next;
1796
1797 bfd_vma addend;
1798
1799 union
2cdcc330
AM
1800 {
1801 bfd_signed_vma refcount;
1802 bfd_vma offset;
1803 } plt;
927be08e
AM
1804};
1805
e717da7e
AM
1806struct ppc64_elf_obj_tdata
1807{
1808 struct elf_obj_tdata elf;
1809
1810 /* Shortcuts to dynamic linker sections. */
1811 asection *got;
1812 asection *relgot;
1813
b3fac117
AM
1814 /* Used during garbage collection. We attach global symbols defined
1815 on removed .opd entries to this section so that the sym is removed. */
1816 asection *deleted_section;
81688140 1817
927be08e 1818 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1819 sections means we potentially need one of these for each input bfd. */
927be08e 1820 struct got_entry tlsld_got;
8860955f 1821
d77c8a4b
AM
1822 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1823 the reloc to be in the range -32768 to 32767. */
98528052
AM
1824 unsigned int has_small_toc_reloc : 1;
1825
560c8763
AM
1826 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1827 instruction not one we handle. */
1828 unsigned int unexpected_toc_insn : 1;
066f4018 1829
903b777d
AM
1830 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1831 this file. */
1832 unsigned int has_optrel : 1;
e717da7e
AM
1833};
1834
1835#define ppc64_elf_tdata(bfd) \
1836 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1837
1838#define ppc64_tlsld_got(bfd) \
1839 (&ppc64_elf_tdata (bfd)->tlsld_got)
1840
1841/* Override the generic function because we store some extras. */
1842
0a1b45a2 1843static bool
e717da7e
AM
1844ppc64_elf_mkobject (bfd *abfd)
1845{
aa7a348b 1846 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata));
e717da7e
AM
1847}
1848
feee612b 1849/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1850 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1851
0a1b45a2 1852static bool
4ce794b7 1853ppc64_elf_object_p (bfd *abfd)
feee612b 1854{
14b57c7c 1855 if (!abfd->arch_info->the_default)
0a1b45a2 1856 return true;
14b57c7c
AM
1857
1858 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1859 {
1860 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1861
1862 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1863 {
1864 /* Relies on arch after 32 bit default being 64 bit default. */
1865 abfd->arch_info = abfd->arch_info->next;
1866 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1867 }
1868 }
14b57c7c 1869 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1870}
1871
d37c89e5
AM
1872/* Support for core dump NOTE sections. */
1873
0a1b45a2 1874static bool
d37c89e5
AM
1875ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1876{
eea6121a 1877 size_t offset, size;
d37c89e5
AM
1878
1879 if (note->descsz != 504)
0a1b45a2 1880 return false;
d37c89e5
AM
1881
1882 /* pr_cursig */
228e534f 1883 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1884
1885 /* pr_pid */
228e534f 1886 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1887
1888 /* pr_reg */
1889 offset = 112;
eea6121a 1890 size = 384;
d37c89e5
AM
1891
1892 /* Make a ".reg/999" section. */
1893 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1894 size, note->descpos + offset);
d37c89e5
AM
1895}
1896
0a1b45a2 1897static bool
d37c89e5
AM
1898ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1899{
1900 if (note->descsz != 136)
0a1b45a2 1901 return false;
d37c89e5 1902
228e534f 1903 elf_tdata (abfd)->core->pid
bc989cdc 1904 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1905 elf_tdata (abfd)->core->program
d37c89e5 1906 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1907 elf_tdata (abfd)->core->command
d37c89e5
AM
1908 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1909
0a1b45a2 1910 return true;
d37c89e5
AM
1911}
1912
183e98be
AM
1913static char *
1914ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1915 ...)
1916{
1917 switch (note_type)
1918 {
1919 default:
1920 return NULL;
1921
1922 case NT_PRPSINFO:
1923 {
9ef6d1e3 1924 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1925 va_list ap;
1926
1927 va_start (ap, note_type);
75cd47ed 1928 memset (data, 0, sizeof (data));
183e98be 1929 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1930#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1931 DIAGNOSTIC_PUSH;
be3e27bb 1932 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1933 -Wstringop-truncation:
1934 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1935 */
95da9854
L
1936 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1937#endif
183e98be 1938 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1939#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1940 DIAGNOSTIC_POP;
fe75810f 1941#endif
183e98be
AM
1942 va_end (ap);
1943 return elfcore_write_note (abfd, buf, bufsiz,
1944 "CORE", note_type, data, sizeof (data));
1945 }
1946
1947 case NT_PRSTATUS:
1948 {
1949 char data[504];
1950 va_list ap;
1951 long pid;
1952 int cursig;
1953 const void *greg;
1954
1955 va_start (ap, note_type);
1956 memset (data, 0, 112);
1957 pid = va_arg (ap, long);
1958 bfd_put_32 (abfd, pid, data + 32);
1959 cursig = va_arg (ap, int);
1960 bfd_put_16 (abfd, cursig, data + 12);
1961 greg = va_arg (ap, const void *);
1962 memcpy (data + 112, greg, 384);
1963 memset (data + 496, 0, 8);
1964 va_end (ap);
1965 return elfcore_write_note (abfd, buf, bufsiz,
1966 "CORE", note_type, data, sizeof (data));
1967 }
1968 }
1969}
1970
5d35169e
AM
1971/* Add extra PPC sections. */
1972
2cdcc330 1973static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1974{
07d6d2b8
AM
1975 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1976 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1977 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1978 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1979 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1980 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1981 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1982};
1983
7c8fe5c4
AM
1984enum _ppc64_sec_type {
1985 sec_normal = 0,
1986 sec_opd = 1,
46439c90
AM
1987 sec_toc = 2,
1988 sec_stub = 3
7c8fe5c4
AM
1989};
1990
f0abc2a1
AM
1991struct _ppc64_elf_section_data
1992{
1993 struct bfd_elf_section_data elf;
411e1bfb 1994
f0abc2a1
AM
1995 union
1996 {
51aecdc5
AM
1997 /* An array with one entry for each opd function descriptor,
1998 and some spares since opd entries may be either 16 or 24 bytes. */
1999#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
2000 struct _opd_sec_data
2001 {
2002 /* Points to the function code section for local opd entries. */
2003 asection **func_sec;
2004
2005 /* After editing .opd, adjust references to opd local syms. */
2006 long *adjust;
6313825c
AM
2007
2008 union
2009 {
2010 /* A copy of relocs before they are modified for --emit-relocs. */
2011 Elf_Internal_Rela *relocs;
2012
2013 /* Section contents. */
2014 bfd_byte *contents;
2015 } u;
74f0fb50 2016 } opd;
7c8fe5c4 2017
3a71aa26
AM
2018 /* An array for toc sections, indexed by offset/8. */
2019 struct _toc_sec_data
2020 {
2021 /* Specifies the relocation symbol index used at a given toc offset. */
2022 unsigned *symndx;
2023
2024 /* And the relocation addend. */
2025 bfd_vma *add;
2026 } toc;
46439c90
AM
2027
2028 /* Stub debugging. */
2029 struct ppc_stub_hash_entry *last_ent;
7c8fe5c4
AM
2030 } u;
2031
2032 enum _ppc64_sec_type sec_type:2;
411e1bfb 2033
7c8fe5c4
AM
2034 /* Flag set when small branches are detected. Used to
2035 select suitable defaults for the stub group size. */
2036 unsigned int has_14bit_branch:1;
3e04d765
AM
2037
2038 /* Flag set when PLTCALL relocs are detected. */
2039 unsigned int has_pltcall:1;
066f4018 2040
903b777d
AM
2041 /* Flag set when section has PLT/GOT/TOC relocations that can be
2042 optimised. */
2043 unsigned int has_optrel:1;
f0abc2a1
AM
2044};
2045
2046#define ppc64_elf_section_data(sec) \
411e1bfb 2047 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1 2048
0a1b45a2 2049static bool
4ce794b7 2050ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2051{
f5a8cdea 2052 struct _ppc64_elf_section_data *sdata;
f0abc2a1 2053
f5a8cdea
AM
2054 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2055 if (sdata == NULL)
2056 return false;
2057 sec->used_by_bfd = sdata;
f0abc2a1
AM
2058
2059 return _bfd_elf_new_section_hook (abfd, sec);
2060}
4025353c 2061
0a1b45a2 2062static bool
bf577467
AM
2063ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2064{
2065 const char *name = hdr->bfd_section->name;
2066
3f3328b8
ML
2067 if (startswith (name, ".sbss")
2068 || startswith (name, ".sdata"))
bf577467
AM
2069 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2070
0a1b45a2 2071 return true;
bf577467
AM
2072}
2073
74f0fb50 2074static struct _opd_sec_data *
4025353c
AM
2075get_opd_info (asection * sec)
2076{
2077 if (sec != NULL
2078 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2079 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2080 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2081 return NULL;
2082}
90e3cdf2
JJ
2083\f
2084/* Parameters for the qsort hook. */
0a1b45a2 2085static bool synthetic_relocatable;
8cb1c2c8 2086static const asection *synthetic_opd;
90e3cdf2 2087
699733f6 2088/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2089
2090static int
2091compare_symbols (const void *ap, const void *bp)
2092{
2cdcc330
AM
2093 const asymbol *a = *(const asymbol **) ap;
2094 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2095
699733f6
AM
2096 /* Section symbols first. */
2097 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2098 return -1;
699733f6 2099 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2100 return 1;
2101
699733f6 2102 /* then .opd symbols. */
cd285db5
AM
2103 if (synthetic_opd != NULL)
2104 {
2105 if (strcmp (a->section->name, ".opd") == 0
2106 && strcmp (b->section->name, ".opd") != 0)
2107 return -1;
2108 if (strcmp (a->section->name, ".opd") != 0
2109 && strcmp (b->section->name, ".opd") == 0)
2110 return 1;
2111 }
90e3cdf2 2112
699733f6 2113 /* then other code symbols. */
2cdcc330
AM
2114 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2115 == (SEC_CODE | SEC_ALLOC))
2116 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2117 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2118 return -1;
2119
2cdcc330
AM
2120 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2121 != (SEC_CODE | SEC_ALLOC))
2122 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2123 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2124 return 1;
2125
2126 if (synthetic_relocatable)
2127 {
2128 if (a->section->id < b->section->id)
2129 return -1;
2130
2131 if (a->section->id > b->section->id)
2132 return 1;
2133 }
2134
2135 if (a->value + a->section->vma < b->value + b->section->vma)
2136 return -1;
2137
2138 if (a->value + a->section->vma > b->value + b->section->vma)
2139 return 1;
2140
4d35a0aa
AM
2141 /* For syms with the same value, prefer strong dynamic global function
2142 syms over other syms. */
2143 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2144 return -1;
2145
2146 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2147 return 1;
2148
2149 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2150 return -1;
2151
2152 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2153 return 1;
2154
2155 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2156 return -1;
2157
2158 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2159 return 1;
2160
2161 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2162 return -1;
2163
2164 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2165 return 1;
2166
dcea6a95
AM
2167 /* Finally, sort on where the symbol is in memory. The symbols will
2168 be in at most two malloc'd blocks, one for static syms, one for
2169 dynamic syms, and we distinguish the two blocks above by testing
2170 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2171 originally in the same order as the symbols (and we're not
2172 sorting the symbols themselves), this ensures a stable sort. */
2173 if (a < b)
2174 return -1;
2175 if (a > b)
2176 return 1;
2177 return 0;
90e3cdf2
JJ
2178}
2179
699733f6 2180/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2181
699733f6 2182static asymbol *
9ad9b810
AM
2183sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2184 bfd_vma value)
90e3cdf2 2185{
9ad9b810 2186 size_t mid;
90e3cdf2 2187
7292b3ac 2188 if (id == (unsigned) -1)
699733f6
AM
2189 {
2190 while (lo < hi)
2191 {
2192 mid = (lo + hi) >> 1;
2193 if (syms[mid]->value + syms[mid]->section->vma < value)
2194 lo = mid + 1;
2195 else if (syms[mid]->value + syms[mid]->section->vma > value)
2196 hi = mid;
2197 else
2198 return syms[mid];
2199 }
2200 }
2201 else
2202 {
2203 while (lo < hi)
2204 {
2205 mid = (lo + hi) >> 1;
2206 if (syms[mid]->section->id < id)
2207 lo = mid + 1;
2208 else if (syms[mid]->section->id > id)
2209 hi = mid;
2210 else if (syms[mid]->value < value)
2211 lo = mid + 1;
2212 else if (syms[mid]->value > value)
2213 hi = mid;
2214 else
2215 return syms[mid];
2216 }
2217 }
2218 return NULL;
90e3cdf2
JJ
2219}
2220
0a1b45a2 2221static bool
468392fb
AM
2222section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2223{
2224 bfd_vma vma = *(bfd_vma *) ptr;
2225 return ((section->flags & SEC_ALLOC) != 0
2226 && section->vma <= vma
2227 && vma < section->vma + section->size);
2228}
2229
699733f6 2230/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2231 entry syms. Also generate @plt symbols for the glink branch table.
2232 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2233
2234static long
a7535cf3
AM
2235ppc64_elf_get_synthetic_symtab (bfd *abfd,
2236 long static_count, asymbol **static_syms,
2237 long dyn_count, asymbol **dyn_syms,
c9727e01 2238 asymbol **ret)
90e3cdf2
JJ
2239{
2240 asymbol *s;
0ccf57bd 2241 size_t i, j, count;
90e3cdf2 2242 char *names;
0ccf57bd 2243 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2244 asection *opd = NULL;
0a1b45a2 2245 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2246 asymbol **syms;
ee67d69a 2247 int abi = abiversion (abfd);
90e3cdf2
JJ
2248
2249 *ret = NULL;
2250
ee67d69a
AM
2251 if (abi < 2)
2252 {
2253 opd = bfd_get_section_by_name (abfd, ".opd");
2254 if (opd == NULL && abi == 1)
2255 return 0;
2256 }
90e3cdf2 2257
a5259595
AM
2258 syms = NULL;
2259 codesecsym = 0;
2260 codesecsymend = 0;
2261 secsymend = 0;
2262 opdsymend = 0;
2263 symcount = 0;
2264 if (opd != NULL)
c9727e01 2265 {
a5259595
AM
2266 symcount = static_count;
2267 if (!relocatable)
2268 symcount += dyn_count;
2269 if (symcount == 0)
2270 return 0;
c9727e01 2271
a5259595
AM
2272 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2273 if (syms == NULL)
2274 return -1;
90e3cdf2 2275
a5259595
AM
2276 if (!relocatable && static_count != 0 && dyn_count != 0)
2277 {
2278 /* Use both symbol tables. */
2279 memcpy (syms, static_syms, static_count * sizeof (*syms));
2280 memcpy (syms + static_count, dyn_syms,
2281 (dyn_count + 1) * sizeof (*syms));
2282 }
2283 else if (!relocatable && static_count == 0)
2284 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2285 else
2286 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2287
0ccf57bd
AM
2288 /* Trim uninteresting symbols. Interesting symbols are section,
2289 function, and notype symbols. */
2290 for (i = 0, j = 0; i < symcount; ++i)
2291 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2292 | BSF_RELC | BSF_SRELC)) == 0)
2293 syms[j++] = syms[i];
2294 symcount = j;
2295
a5259595
AM
2296 synthetic_relocatable = relocatable;
2297 synthetic_opd = opd;
2298 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2299
a5259595
AM
2300 if (!relocatable && symcount > 1)
2301 {
bfa5bd2a
PA
2302 /* Trim duplicate syms, since we may have merged the normal
2303 and dynamic symbols. Actually, we only care about syms
2304 that have different values, so trim any with the same
2305 value. Don't consider ifunc and ifunc resolver symbols
2306 duplicates however, because GDB wants to know whether a
2307 text symbol is an ifunc resolver. */
a5259595 2308 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2309 {
2310 const asymbol *s0 = syms[i - 1];
2311 const asymbol *s1 = syms[i];
2312
2313 if ((s0->value + s0->section->vma
2314 != s1->value + s1->section->vma)
2315 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2316 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2317 syms[j++] = syms[i];
2318 }
a5259595
AM
2319 symcount = j;
2320 }
699733f6 2321
a5259595
AM
2322 i = 0;
2323 /* Note that here and in compare_symbols we can't compare opd and
2324 sym->section directly. With separate debug info files, the
2325 symbols will be extracted from the debug file while abfd passed
2326 to this function is the real binary. */
8bd10d6b
AM
2327 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2328 && strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2329 ++i;
2330 codesecsym = i;
2331
2332 for (; i < symcount; ++i)
2333 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2334 | SEC_THREAD_LOCAL))
2335 != (SEC_CODE | SEC_ALLOC))
2336 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2337 break;
2338 codesecsymend = i;
2339
2340 for (; i < symcount; ++i)
2341 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2342 break;
2343 secsymend = i;
2344
2345 for (; i < symcount; ++i)
2346 if (strcmp (syms[i]->section->name, ".opd") != 0)
2347 break;
2348 opdsymend = i;
2349
2350 for (; i < symcount; ++i)
2cdcc330
AM
2351 if (((syms[i]->section->flags
2352 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2353 != (SEC_CODE | SEC_ALLOC))
2354 break;
2355 symcount = i;
2356 }
c9727e01 2357 count = 0;
90e3cdf2 2358
699733f6 2359 if (relocatable)
90e3cdf2 2360 {
0a1b45a2 2361 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
699733f6
AM
2362 arelent *r;
2363 size_t size;
0ccf57bd 2364 size_t relcount;
90e3cdf2 2365
468392fb
AM
2366 if (opdsymend == secsymend)
2367 goto done;
2368
699733f6 2369 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2370 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2371 if (relcount == 0)
c9727e01 2372 goto done;
90e3cdf2 2373
0a1b45a2 2374 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
7356fed5
AM
2375 {
2376 count = -1;
2377 goto done;
2378 }
2379
699733f6 2380 size = 0;
595da8c5 2381 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2382 {
2383 asymbol *sym;
90e3cdf2 2384
595da8c5 2385 while (r < opd->relocation + relcount
699733f6
AM
2386 && r->address < syms[i]->value + opd->vma)
2387 ++r;
90e3cdf2 2388
595da8c5 2389 if (r == opd->relocation + relcount)
699733f6 2390 break;
90e3cdf2 2391
699733f6
AM
2392 if (r->address != syms[i]->value + opd->vma)
2393 continue;
90e3cdf2 2394
699733f6
AM
2395 if (r->howto->type != R_PPC64_ADDR64)
2396 continue;
90e3cdf2 2397
699733f6
AM
2398 sym = *r->sym_ptr_ptr;
2399 if (!sym_exists_at (syms, opdsymend, symcount,
9b09ceea 2400 sym->section->id, sym->value + r->addend))
699733f6
AM
2401 {
2402 ++count;
2403 size += sizeof (asymbol);
2404 size += strlen (syms[i]->name) + 2;
2405 }
2406 }
90e3cdf2 2407
c4b0b099
AM
2408 if (size == 0)
2409 goto done;
699733f6
AM
2410 s = *ret = bfd_malloc (size);
2411 if (s == NULL)
2412 {
7356fed5 2413 count = -1;
c9727e01 2414 goto done;
699733f6 2415 }
90e3cdf2 2416
699733f6 2417 names = (char *) (s + count);
90e3cdf2 2418
595da8c5 2419 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2420 {
699733f6 2421 asymbol *sym;
90e3cdf2 2422
595da8c5 2423 while (r < opd->relocation + relcount
699733f6
AM
2424 && r->address < syms[i]->value + opd->vma)
2425 ++r;
90e3cdf2 2426
595da8c5 2427 if (r == opd->relocation + relcount)
699733f6
AM
2428 break;
2429
2430 if (r->address != syms[i]->value + opd->vma)
2431 continue;
2432
2433 if (r->howto->type != R_PPC64_ADDR64)
2434 continue;
90e3cdf2 2435
699733f6
AM
2436 sym = *r->sym_ptr_ptr;
2437 if (!sym_exists_at (syms, opdsymend, symcount,
9b09ceea 2438 sym->section->id, sym->value + r->addend))
699733f6
AM
2439 {
2440 size_t len;
2441
2442 *s = *syms[i];
6ba2a415 2443 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2444 s->section = sym->section;
2445 s->value = sym->value + r->addend;
2446 s->name = names;
2447 *names++ = '.';
2448 len = strlen (syms[i]->name);
2449 memcpy (names, syms[i]->name, len + 1);
2450 names += len + 1;
6f610d07
UW
2451 /* Have udata.p point back to the original symbol this
2452 synthetic symbol was derived from. */
2453 s->udata.p = syms[i];
699733f6
AM
2454 s++;
2455 }
2456 }
2457 }
2458 else
90e3cdf2 2459 {
0a1b45a2 2460 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
ee67d69a 2461 bfd_byte *contents = NULL;
699733f6 2462 size_t size;
0ccf57bd 2463 size_t plt_count = 0;
468392fb
AM
2464 bfd_vma glink_vma = 0, resolv_vma = 0;
2465 asection *dynamic, *glink = NULL, *relplt = NULL;
2466 arelent *p;
90e3cdf2 2467
81ff113f
AM
2468 if (opd != NULL
2469 && ((opd->flags & SEC_HAS_CONTENTS) == 0
2470 || !bfd_malloc_and_get_section (abfd, opd, &contents)))
699733f6 2471 {
c4b0b099
AM
2472 free_contents_and_exit_err:
2473 count = -1;
ee67d69a 2474 free_contents_and_exit:
c9594989 2475 free (contents);
c9727e01 2476 goto done;
699733f6 2477 }
90e3cdf2 2478
699733f6
AM
2479 size = 0;
2480 for (i = secsymend; i < opdsymend; ++i)
2481 {
2482 bfd_vma ent;
90e3cdf2 2483
5ef11c02
AM
2484 /* Ignore bogus symbols. */
2485 if (syms[i]->value > opd->size - 8)
2486 continue;
2487
699733f6 2488 ent = bfd_get_64 (abfd, contents + syms[i]->value);
9b09ceea 2489 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
699733f6
AM
2490 {
2491 ++count;
2492 size += sizeof (asymbol);
2493 size += strlen (syms[i]->name) + 2;
2494 }
2495 }
90e3cdf2 2496
468392fb 2497 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2498 if (dyn_count != 0
2499 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2500 {
2501 bfd_byte *dynbuf, *extdyn, *extdynend;
2502 size_t extdynsize;
2503 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2504
81ff113f
AM
2505 if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2506 || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2507 goto free_contents_and_exit_err;
468392fb
AM
2508
2509 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2510 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2511
37c59664
AM
2512 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2513 (size_t) (extdynend - extdyn) >= extdynsize;
2514 extdyn += extdynsize)
468392fb
AM
2515 {
2516 Elf_Internal_Dyn dyn;
2517 (*swap_dyn_in) (abfd, extdyn, &dyn);
2518
2519 if (dyn.d_tag == DT_NULL)
2520 break;
2521
2522 if (dyn.d_tag == DT_PPC64_GLINK)
2523 {
9e390558
AM
2524 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2525 See comment in ppc64_elf_finish_dynamic_sections. */
2526 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2527 /* The .glink section usually does not survive the final
2528 link; search for the section (usually .text) where the
2529 glink stubs now reside. */
2530 glink = bfd_sections_find_if (abfd, section_covers_vma,
2531 &glink_vma);
2532 break;
2533 }
2534 }
2535
2536 free (dynbuf);
2537 }
2538
2539 if (glink != NULL)
2540 {
2541 /* Determine __glink trampoline by reading the relative branch
2542 from the first glink stub. */
2543 bfd_byte buf[4];
b9e5796b
AM
2544 unsigned int off = 0;
2545
2546 while (bfd_get_section_contents (abfd, glink, buf,
2547 glink_vma + off - glink->vma, 4))
468392fb
AM
2548 {
2549 unsigned int insn = bfd_get_32 (abfd, buf);
2550 insn ^= B_DOT;
2551 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2552 {
2cdcc330
AM
2553 resolv_vma
2554 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2555 break;
2556 }
2557 off += 4;
2558 if (off > 4)
2559 break;
468392fb
AM
2560 }
2561
2562 if (resolv_vma)
2563 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2564
066ee829
AM
2565 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2566 if (relplt != NULL)
2567 {
2568 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 2569 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
c4b0b099 2570 goto free_contents_and_exit_err;
68ffbac6 2571
93c6e8c3 2572 plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
066ee829 2573 size += plt_count * sizeof (asymbol);
468392fb 2574
066ee829
AM
2575 p = relplt->relocation;
2576 for (i = 0; i < plt_count; i++, p++)
9b09ceea
AM
2577 {
2578 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2579 if (p->addend != 0)
2580 size += sizeof ("+0x") - 1 + 16;
2581 }
066ee829 2582 }
468392fb
AM
2583 }
2584
c4b0b099
AM
2585 if (size == 0)
2586 goto free_contents_and_exit;
699733f6
AM
2587 s = *ret = bfd_malloc (size);
2588 if (s == NULL)
c4b0b099 2589 goto free_contents_and_exit_err;
90e3cdf2 2590
468392fb 2591 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2592
699733f6 2593 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2594 {
699733f6 2595 bfd_vma ent;
90e3cdf2 2596
5ef11c02
AM
2597 if (syms[i]->value > opd->size - 8)
2598 continue;
2599
699733f6 2600 ent = bfd_get_64 (abfd, contents + syms[i]->value);
9b09ceea 2601 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2602 {
0ccf57bd 2603 size_t lo, hi;
699733f6 2604 size_t len;
c9727e01 2605 asection *sec = abfd->sections;
90e3cdf2 2606
699733f6
AM
2607 *s = *syms[i];
2608 lo = codesecsym;
2609 hi = codesecsymend;
2610 while (lo < hi)
2611 {
0ccf57bd 2612 size_t mid = (lo + hi) >> 1;
699733f6
AM
2613 if (syms[mid]->section->vma < ent)
2614 lo = mid + 1;
2615 else if (syms[mid]->section->vma > ent)
2616 hi = mid;
2617 else
c9727e01
AM
2618 {
2619 sec = syms[mid]->section;
2620 break;
2621 }
699733f6
AM
2622 }
2623
c9727e01 2624 if (lo >= hi && lo > codesecsym)
699733f6 2625 sec = syms[lo - 1]->section;
699733f6
AM
2626
2627 for (; sec != NULL; sec = sec->next)
2628 {
2629 if (sec->vma > ent)
2630 break;
63524580
JK
2631 /* SEC_LOAD may not be set if SEC is from a separate debug
2632 info file. */
2633 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2634 break;
2635 if ((sec->flags & SEC_CODE) != 0)
2636 s->section = sec;
2637 }
6ba2a415 2638 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2639 s->value = ent - s->section->vma;
2640 s->name = names;
2641 *names++ = '.';
2642 len = strlen (syms[i]->name);
2643 memcpy (names, syms[i]->name, len + 1);
2644 names += len + 1;
6f610d07
UW
2645 /* Have udata.p point back to the original symbol this
2646 synthetic symbol was derived from. */
2647 s->udata.p = syms[i];
699733f6 2648 s++;
90e3cdf2 2649 }
90e3cdf2 2650 }
699733f6 2651 free (contents);
468392fb
AM
2652
2653 if (glink != NULL && relplt != NULL)
2654 {
2655 if (resolv_vma)
2656 {
2657 /* Add a symbol for the main glink trampoline. */
86a4952b 2658 memset (s, 0, sizeof *s);
468392fb 2659 s->the_bfd = abfd;
6ba2a415 2660 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2661 s->section = glink;
2662 s->value = resolv_vma - glink->vma;
2663 s->name = names;
2cdcc330
AM
2664 memcpy (names, "__glink_PLTresolve",
2665 sizeof ("__glink_PLTresolve"));
468392fb
AM
2666 names += sizeof ("__glink_PLTresolve");
2667 s++;
2668 count++;
2669 }
2670
2671 /* FIXME: It would be very much nicer to put sym@plt on the
2672 stub rather than on the glink branch table entry. The
2673 objdump disassembler would then use a sensible symbol
2674 name on plt calls. The difficulty in doing so is
2675 a) finding the stubs, and,
2676 b) matching stubs against plt entries, and,
2677 c) there can be multiple stubs for a given plt entry.
2678
2679 Solving (a) could be done by code scanning, but older
2680 ppc64 binaries used different stubs to current code.
2681 (b) is the tricky one since you need to known the toc
2682 pointer for at least one function that uses a pic stub to
2683 be able to calculate the plt address referenced.
2684 (c) means gdb would need to set multiple breakpoints (or
2685 find the glink branch itself) when setting breakpoints
2686 for pending shared library loads. */
2687 p = relplt->relocation;
2688 for (i = 0; i < plt_count; i++, p++)
9b09ceea
AM
2689 {
2690 size_t len;
2691
2692 *s = **p->sym_ptr_ptr;
2693 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2694 we are defining a symbol, ensure one of them is set. */
2695 if ((s->flags & BSF_LOCAL) == 0)
2696 s->flags |= BSF_GLOBAL;
2697 s->flags |= BSF_SYNTHETIC;
2698 s->section = glink;
2699 s->value = glink_vma - glink->vma;
2700 s->name = names;
2701 s->udata.p = NULL;
2702 len = strlen ((*p->sym_ptr_ptr)->name);
2703 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2704 names += len;
2705 if (p->addend != 0)
2706 {
2707 memcpy (names, "+0x", sizeof ("+0x") - 1);
2708 names += sizeof ("+0x") - 1;
2709 bfd_sprintf_vma (abfd, names, p->addend);
2710 names += strlen (names);
2711 }
2712 memcpy (names, "@plt", sizeof ("@plt"));
2713 names += sizeof ("@plt");
2714 s++;
2715 if (abi < 2)
2716 {
2717 glink_vma += 8;
2718 if (i >= 0x8000)
2719 glink_vma += 4;
2720 }
2721 else
2722 glink_vma += 4;
2723 }
2724 count += plt_count;
468392fb 2725 }
90e3cdf2
JJ
2726 }
2727
c9727e01 2728 done:
a7535cf3 2729 free (syms);
90e3cdf2
JJ
2730 return count;
2731}
5bd4f169 2732\f
65f38f15
AM
2733/* The following functions are specific to the ELF linker, while
2734 functions above are used generally. Those named ppc64_elf_* are
2735 called by the main ELF linker code. They appear in this file more
2736 or less in the order in which they are called. eg.
2737 ppc64_elf_check_relocs is called early in the link process,
2738 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2739 called.
2740
2741 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2742 functions have both a function code symbol and a function descriptor
2743 symbol. A call to foo in a relocatable object file looks like:
2744
2745 . .text
2746 . x:
2747 . bl .foo
2748 . nop
2749
2750 The function definition in another object file might be:
2751
2752 . .section .opd
2753 . foo: .quad .foo
2754 . .quad .TOC.@tocbase
2755 . .quad 0
2756 .
2757 . .text
2758 . .foo: blr
2759
2760 When the linker resolves the call during a static link, the branch
2761 unsurprisingly just goes to .foo and the .opd information is unused.
2762 If the function definition is in a shared library, things are a little
2763 different: The call goes via a plt call stub, the opd information gets
2764 copied to the plt, and the linker patches the nop.
2765
2766 . x:
2767 . bl .foo_stub
2768 . ld 2,40(1)
2769 .
2770 .
2771 . .foo_stub:
71a39c98
AM
2772 . std 2,40(1) # in practice, the call stub
2773 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2774 . addi 11,11,Lfoo@toc@l # this is the general idea
2775 . ld 12,0(11)
2776 . ld 2,8(11)
2777 . mtctr 12
2778 . ld 11,16(11)
e86ce104
AM
2779 . bctr
2780 .
2781 . .section .plt
2782 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2783
2784 The "reloc ()" notation is supposed to indicate that the linker emits
2785 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2786 copying.
2787
2788 What are the difficulties here? Well, firstly, the relocations
2789 examined by the linker in check_relocs are against the function code
2790 sym .foo, while the dynamic relocation in the plt is emitted against
2791 the function descriptor symbol, foo. Somewhere along the line, we need
2792 to carefully copy dynamic link information from one symbol to the other.
2793 Secondly, the generic part of the elf linker will make .foo a dynamic
2794 symbol as is normal for most other backends. We need foo dynamic
2795 instead, at least for an application final link. However, when
2796 creating a shared library containing foo, we need to have both symbols
2797 dynamic so that references to .foo are satisfied during the early
2798 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2799 definition from some other object, eg. a static library.
2800
2801 Update: As of August 2004, we support a new convention. Function
2802 calls may use the function descriptor symbol, ie. "bl foo". This
2803 behaves exactly as "bl .foo". */
65f38f15 2804
7c8bbca5
AM
2805/* Of those relocs that might be copied as dynamic relocs, this
2806 function selects those that must be copied when linking a shared
2807 library or PIE, even when the symbol is local. */
65f38f15 2808
1d483afe
AM
2809static int
2810must_be_dyn_reloc (struct bfd_link_info *info,
2811 enum elf_ppc64_reloc_type r_type)
2812{
2813 switch (r_type)
2814 {
2815 default:
7c8bbca5
AM
2816 /* Only relative relocs can be resolved when the object load
2817 address isn't fixed. DTPREL64 is excluded because the
2818 dynamic linker needs to differentiate global dynamic from
2819 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2820 return 1;
2821
2822 case R_PPC64_REL32:
2823 case R_PPC64_REL64:
2824 case R_PPC64_REL30:
1bdd8fac
AM
2825 case R_PPC64_TOC16:
2826 case R_PPC64_TOC16_DS:
2827 case R_PPC64_TOC16_LO:
2828 case R_PPC64_TOC16_HI:
2829 case R_PPC64_TOC16_HA:
2830 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2831 return 0;
2832
2833 case R_PPC64_TPREL16:
2834 case R_PPC64_TPREL16_LO:
2835 case R_PPC64_TPREL16_HI:
2836 case R_PPC64_TPREL16_HA:
2837 case R_PPC64_TPREL16_DS:
2838 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2839 case R_PPC64_TPREL16_HIGH:
2840 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2841 case R_PPC64_TPREL16_HIGHER:
2842 case R_PPC64_TPREL16_HIGHERA:
2843 case R_PPC64_TPREL16_HIGHEST:
2844 case R_PPC64_TPREL16_HIGHESTA:
2845 case R_PPC64_TPREL64:
c213164a 2846 case R_PPC64_TPREL34:
7c8bbca5
AM
2847 /* These relocations are relative but in a shared library the
2848 linker doesn't know the thread pointer base. */
2849 return bfd_link_dll (info);
1d483afe
AM
2850 }
2851}
65f38f15 2852
f4656909 2853/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2854 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2855 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2856 shared lib. With code that gcc generates it is vital that this be
2857 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2858 actually the address of a function descriptor which resides in the
2859 .opd section. gcc uses the descriptor directly rather than going
2860 via the GOT as some other ABIs do, which means that initialized
2861 function pointers reference the descriptor. Thus, a function
2862 pointer initialized to the address of a function in a shared
2863 library will either require a .dynbss copy and a copy reloc, or a
2864 dynamic reloc. Using a .dynbss copy redefines the function
2865 descriptor symbol to point to the copy. This presents a problem as
2866 a PLT entry for that function is also initialized from the function
2867 descriptor symbol and the copy may not be initialized first. */
a23b6845 2868#define ELIMINATE_COPY_RELOCS 1
f4656909 2869
721956f4 2870/* Section name for stubs is the associated section name plus this
29942be8
NC
2871 string. */
2872#define STUB_SUFFIX ".stub"
721956f4
AM
2873
2874/* Linker stubs.
2875 ppc_stub_long_branch:
2876 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2877 destination, but a 24 bit branch in a stub section will reach.
2878 . b dest
2879
2880 ppc_stub_plt_branch:
2881 Similar to the above, but a 24 bit branch in the stub section won't
2882 reach its destination.
6df4c9c2
AM
2883 . addis %r12,%r2,xxx@toc@ha
2884 . ld %r12,xxx@toc@l(%r12)
71a39c98 2885 . mtctr %r12
721956f4
AM
2886 . bctr
2887
2888 ppc_stub_plt_call:
2c66dc6c
AM
2889 Used to call a function in a shared library. If it so happens that
2890 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2891 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
7aba54da 2892 An r2save variant starts with "std %r2,40(%r1)".
71a39c98
AM
2893 . addis %r11,%r2,xxx@toc@ha
2894 . ld %r12,xxx+0@toc@l(%r11)
2895 . mtctr %r12
2896 . ld %r2,xxx+8@toc@l(%r11)
2897 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2898 . bctr
ad8e1ba5
AM
2899
2900 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2901 code to adjust the value and save r2 to support multiple toc sections.
2902 A ppc_stub_long_branch with an r2 offset looks like:
2903 . std %r2,40(%r1)
2904 . addis %r2,%r2,off@ha
2905 . addi %r2,%r2,off@l
2906 . b dest
2907
2908 A ppc_stub_plt_branch with an r2 offset looks like:
2909 . std %r2,40(%r1)
6df4c9c2
AM
2910 . addis %r12,%r2,xxx@toc@ha
2911 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2912 . addis %r2,%r2,off@ha
2913 . addi %r2,%r2,off@l
71a39c98 2914 . mtctr %r12
ad8e1ba5 2915 . bctr
ac2df442 2916
05d0e962
AM
2917 All of the above stubs are shown as their ELFv1 variants. ELFv2
2918 variants exist too, simpler for plt calls since a new toc pointer
2919 and static chain are not loaded by the stub. In addition, ELFv2
2920 has some more complex stubs to handle calls marked with NOTOC
7aba54da
AM
2921 relocs from functions where r2 is not a valid toc pointer.
2922 ppc_stub_long_branch_p9notoc:
05d0e962
AM
2923 . mflr %r12
2924 . bcl 20,31,1f
2925 . 1:
2926 . mflr %r11
2927 . mtlr %r12
2cdcc330
AM
2928 . addis %r12,%r11,dest-1b@ha
2929 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2930 . b dest
2931
7aba54da 2932 ppc_stub_plt_branch_p9notoc:
05d0e962
AM
2933 . mflr %r12
2934 . bcl 20,31,1f
2935 . 1:
2936 . mflr %r11
2937 . mtlr %r12
2938 . lis %r12,xxx-1b@highest
f891966f 2939 . ori %r12,%r12,xxx-1b@higher
05d0e962 2940 . sldi %r12,%r12,32
f891966f 2941 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2942 . ori %r12,%r12,xxx-1b@l
2943 . add %r12,%r11,%r12
2944 . mtctr %r12
2945 . bctr
2946
7aba54da 2947 ppc_stub_plt_call_p9notoc:
05d0e962
AM
2948 . mflr %r12
2949 . bcl 20,31,1f
2950 . 1:
2951 . mflr %r11
2952 . mtlr %r12
2953 . lis %r12,xxx-1b@highest
f891966f 2954 . ori %r12,%r12,xxx-1b@higher
05d0e962 2955 . sldi %r12,%r12,32
f891966f 2956 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2957 . ori %r12,%r12,xxx-1b@l
2958 . ldx %r12,%r11,%r12
2959 . mtctr %r12
2960 . bctr
2961
7c1f4227 2962 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2963 ppc_stub_long_branch_notoc:
d4b87b1e 2964 . pla %r12,dest@pcrel
04bdff6a
AM
2965 . b dest
2966 ppc_stub_plt_branch_notoc:
2967 . lis %r11,(dest-1f)@highesta34
2968 . ori %r11,%r11,(dest-1f)@highera34
2969 . sldi %r11,%r11,34
d4b87b1e 2970 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2971 . add %r12,%r11,%r12
2972 . mtctr %r12
2973 . bctr
2974 ppc_stub_plt_call_notoc:
2975 . lis %r11,(xxx-1f)@highesta34
2976 . ori %r11,%r11,(xxx-1f)@highera34
2977 . sldi %r11,%r11,34
d4b87b1e 2978 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2979 . ldx %r12,%r11,%r12
2980 . mtctr %r12
2981 . bctr
2982
05d0e962
AM
2983 In cases where the high instructions would add zero, they are
2984 omitted and following instructions modified in some cases.
7c1f4227 2985 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2986 to
2987 . pld %r12,xxx@pcrel
2988 . mtctr %r12
2989 . bctr
05d0e962 2990
7aba54da 2991 Stub variants may be merged. For example, if printf is called from
05d0e962
AM
2992 code with the tocsave optimization (ie. r2 saved in function
2993 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2994 and from other code without the tocsave optimization requiring a
2995 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2996 type will be created. Calls with the tocsave optimization will
2997 enter this stub after the instruction saving r2. A similar
2998 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2999 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3000 to call an external function like printf. If other calls to printf
3001 require a ppc_stub_plt_call linkage stub then a single
7aba54da
AM
3002 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3003 call. */
3004
3005enum ppc_stub_main_type
2cdcc330 3006{
721956f4
AM
3007 ppc_stub_none,
3008 ppc_stub_long_branch,
3009 ppc_stub_plt_branch,
794e51c0 3010 ppc_stub_plt_call,
a4b6fadd
AM
3011 ppc_stub_global_entry,
3012 ppc_stub_save_res
721956f4
AM
3013};
3014
7aba54da
AM
3015/* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3016 these variations. */
3017
3018enum ppc_stub_sub_type
3019{
3020 ppc_stub_toc,
3021 ppc_stub_notoc,
3022 ppc_stub_p9notoc
3023};
3024
3025struct ppc_stub_type
3026{
3027 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3028 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3029 unsigned int r2save : 1;
3030};
3031
6f20ed8a
AM
3032/* Information on stub grouping. */
3033struct map_stub
3034{
3035 /* The stub section. */
3036 asection *stub_sec;
3037 /* This is the section to which stubs in the group will be attached. */
3038 asection *link_sec;
a4b6fadd
AM
3039 /* Next group. */
3040 struct map_stub *next;
3041 /* Whether to emit a copy of register save/restore functions in this
3042 group. */
3043 int needs_save_res;
df136d64
AM
3044 /* Current offset within stubs after the insn restoring lr in a
3045 _notoc or _both stub using bcl for pc-relative addressing, or
3046 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3047 unsigned int lr_restore;
3048 /* Accumulated size of EH info emitted to describe return address
3049 if stubs modify lr. Does not include 17 byte FDE header. */
3050 unsigned int eh_size;
3051 /* Offset in glink_eh_frame to the start of EH info for this group. */
3052 unsigned int eh_base;
6f20ed8a
AM
3053};
3054
2cdcc330
AM
3055struct ppc_stub_hash_entry
3056{
721956f4
AM
3057 /* Base hash table entry structure. */
3058 struct bfd_hash_entry root;
3059
7aba54da 3060 struct ppc_stub_type type;
ad8e1ba5 3061
6f20ed8a
AM
3062 /* Group information. */
3063 struct map_stub *group;
721956f4
AM
3064
3065 /* Offset within stub_sec of the beginning of this stub. */
3066 bfd_vma stub_offset;
3067
3068 /* Given the symbol's value and its section we can determine its final
3069 value when building the stubs (so the stub knows where to jump. */
3070 bfd_vma target_value;
3071 asection *target_section;
3072
721956f4
AM
3073 /* The symbol table entry, if any, that this was derived from. */
3074 struct ppc_link_hash_entry *h;
e054468f 3075 struct plt_entry *plt_ent;
721956f4 3076
2d7ad24e
AM
3077 /* Symbol type. */
3078 unsigned char symtype;
3079
6911b7dc
AM
3080 /* Symbol st_other. */
3081 unsigned char other;
ecc741e6
AM
3082
3083 /* Debug: Track hash table traversal. */
3084 unsigned int id;
721956f4
AM
3085};
3086
2cdcc330
AM
3087struct ppc_branch_hash_entry
3088{
721956f4
AM
3089 /* Base hash table entry structure. */
3090 struct bfd_hash_entry root;
3091
c456f082 3092 /* Offset within branch lookup table. */
721956f4
AM
3093 unsigned int offset;
3094
3095 /* Generation marker. */
3096 unsigned int iter;
3097};
65f38f15 3098
1657026c 3099/* Used to track dynamic relocations. */
19e08130
AM
3100struct ppc_dyn_relocs
3101{
3102 struct ppc_dyn_relocs *next;
3103
3104 /* The input section of the reloc. */
3105 asection *sec;
3106
3107 /* Total number of relocs copied for the input section. */
1657026c
AM
3108 unsigned int count;
3109
3110 /* Number of pc-relative relocs copied for the input section. */
3111 unsigned int pc_count;
3112
3113 /* Number of relocs that might become R_PPC64_RELATIVE. */
3114 unsigned int rel_count;
3115};
3116
3117struct ppc_local_dyn_relocs
3118{
3119 struct ppc_local_dyn_relocs *next;
3120
3121 /* The input section of the reloc. */
3122 asection *sec;
3123
3124 /* Total number of relocs copied for the input section. */
3125 unsigned int count;
3126
3127 /* Number of relocs that might become R_PPC64_RELATIVE. */
3128 unsigned int rel_count : 31;
19e08130
AM
3129
3130 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3131 unsigned int ifunc : 1;
3132};
3133
65f38f15
AM
3134struct ppc_link_hash_entry
3135{
3136 struct elf_link_hash_entry elf;
3137
2cdcc330
AM
3138 union
3139 {
b3fac117
AM
3140 /* A pointer to the most recently used stub hash entry against this
3141 symbol. */
3142 struct ppc_stub_hash_entry *stub_cache;
3143
3144 /* A pointer to the next symbol starting with a '.' */
3145 struct ppc_link_hash_entry *next_dot_sym;
3146 } u;
721956f4 3147
721956f4 3148 /* Link between function code and descriptor symbols. */
34814b9f 3149 struct ppc_link_hash_entry *oh;
721956f4 3150
e86ce104
AM
3151 /* Flag function code and descriptor symbols. */
3152 unsigned int is_func:1;
3153 unsigned int is_func_descriptor:1;
908b32fc 3154 unsigned int fake:1;
411e1bfb 3155
c5614fa4
AM
3156 /* Whether global opd/toc sym has been adjusted or not.
3157 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3158 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3159 unsigned int adjust_done:1;
3160
a4b6fadd
AM
3161 /* Set if this is an out-of-line register save/restore function,
3162 with non-standard calling convention. */
3163 unsigned int save_res:1;
3164
8b5f1ed8
AM
3165 /* Set if a duplicate symbol with non-zero localentry is detected,
3166 even when the duplicate symbol does not provide a definition. */
3167 unsigned int non_zero_localentry:1;
3168
411e1bfb 3169 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3170 Bits are or'd into the mask as the corresponding relocs are
3171 encountered during check_relocs, with TLS_TLS being set when any
3172 of the other TLS bits are set. tls_optimize clears bits when
3173 optimizing to indicate the corresponding GOT entry type is not
3174 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3175 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3176 These flags are also kept for local symbols. */
3177#define TLS_TLS 1 /* Any TLS reloc. */
3178#define TLS_GD 2 /* GD reloc. */
3179#define TLS_LD 4 /* LD reloc. */
3180#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3181#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3182#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3183#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3184#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3185 unsigned char tls_mask;
37da22e5
AM
3186
3187 /* The above field is also used to mark function symbols. In which
3188 case TLS_TLS will be 0. */
3189#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3190#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3191#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3192};
3193
ed7007c1
AM
3194static inline struct ppc_link_hash_entry *
3195ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3196{
3197 return (struct ppc_link_hash_entry *) ent;
3198}
3199
30845f11
AM
3200static inline struct elf_link_hash_entry *
3201elf_hash_entry (struct ppc_link_hash_entry *ent)
3202{
3203 return (struct elf_link_hash_entry *) ent;
3204}
3205
65f38f15
AM
3206/* ppc64 ELF linker hash table. */
3207
3208struct ppc_link_hash_table
3209{
3210 struct elf_link_hash_table elf;
3211
721956f4
AM
3212 /* The stub hash table. */
3213 struct bfd_hash_table stub_hash_table;
3214
3215 /* Another hash table for plt_branch stubs. */
3216 struct bfd_hash_table branch_hash_table;
3217
3b421ab3
AM
3218 /* Hash table for function prologue tocsave. */
3219 htab_t tocsave_htab;
3220
e7d1c40c
AM
3221 /* Various options and other info passed from the linker. */
3222 struct ppc64_elf_params *params;
721956f4 3223
6f20ed8a
AM
3224 /* The size of sec_info below. */
3225 unsigned int sec_info_arr_size;
3226
3227 /* Per-section array of extra section info. Done this way rather
3228 than as part of ppc64_elf_section_data so we have the info for
3229 non-ppc64 sections. */
3230 struct
3231 {
3232 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3233 bfd_vma toc_off;
6f20ed8a
AM
3234
3235 union
3236 {
3237 /* The section group that this section belongs to. */
3238 struct map_stub *group;
3239 /* A temp section list pointer. */
3240 asection *list;
3241 } u;
3242 } *sec_info;
721956f4 3243
a4b6fadd
AM
3244 /* Linked list of groups. */
3245 struct map_stub *group;
3246
ad8e1ba5
AM
3247 /* Temp used when calculating TOC pointers. */
3248 bfd_vma toc_curr;
bf102f86
AM
3249 bfd *toc_bfd;
3250 asection *toc_first_sec;
ad8e1ba5 3251
b3fac117
AM
3252 /* Used when adding symbols. */
3253 struct ppc_link_hash_entry *dot_syms;
3254
33e44f2e 3255 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3256 asection *glink;
9e390558 3257 asection *global_entry;
82bd7b59 3258 asection *sfpr;
2d7ad24e
AM
3259 asection *pltlocal;
3260 asection *relpltlocal;
4ce794b7
AM
3261 asection *brlt;
3262 asection *relbrlt;
58d180e8 3263 asection *glink_eh_frame;
ec338859 3264
8387904d
AM
3265 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3266 struct ppc_link_hash_entry *tls_get_addr;
3267 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3268 struct ppc_link_hash_entry *tga_desc;
3269 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3270 struct map_stub *tga_group;
411e1bfb 3271
927be08e
AM
3272 /* The size of reliplt used by got entry relocs. */
3273 bfd_size_type got_reli_size;
3274
0aac2413 3275 /* DT_RELR array of section/r_offset. */
1657026c
AM
3276 size_t relr_alloc;
3277 size_t relr_count;
0aac2413
AM
3278 struct
3279 {
3280 asection *sec;
3281 bfd_vma off;
3282 } *relr;
1657026c 3283
9b5ecbd0 3284 /* Statistics. */
7aba54da 3285 unsigned long stub_count[ppc_stub_save_res];
9b5ecbd0 3286
ee75fd95
AM
3287 /* Number of stubs against global syms. */
3288 unsigned long stub_globals;
3289
ee67d69a
AM
3290 /* Set if we're linking code with function descriptors. */
3291 unsigned int opd_abi:1;
3292
4c52953f 3293 /* Support for multiple toc sections. */
33c0ec9d 3294 unsigned int do_multi_toc:1;
4c52953f 3295 unsigned int multi_toc_needed:1;
927be08e 3296 unsigned int second_toc_pass:1;
67f0cbdb 3297 unsigned int do_toc_opt:1;
4c52953f 3298
9a23f96e
AM
3299 /* Set if tls optimization is enabled. */
3300 unsigned int do_tls_opt:1;
3301
3e04d765
AM
3302 /* Set if inline plt calls should be converted to direct calls. */
3303 unsigned int can_convert_all_inline_plt:1;
3304
0441f94f
AM
3305 /* Set if a stub_offset changed. */
3306 unsigned int stub_changed:1;
3307
5d1634d7 3308 /* Set on error. */
99877b66 3309 unsigned int stub_error:1;
721956f4 3310
8c5b4e52
AM
3311 /* Whether func_desc_adjust needs to be run over symbols. */
3312 unsigned int need_func_desc_adj:1;
721956f4 3313
f378ab09
AM
3314 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3315 unsigned int has_plt_localentry0:1;
3316
5663e321
AM
3317 /* Whether calls are made via the PLT from NOTOC functions. */
3318 unsigned int notoc_plt:1;
3319
e10a07b3
AM
3320 /* Whether any code linked seems to be Power10. */
3321 unsigned int has_power10_relocs:1;
3322
ecc741e6
AM
3323 /* Incremented once for each stub sized. */
3324 unsigned int stub_id;
3325
721956f4
AM
3326 /* Incremented every time we size stubs. */
3327 unsigned int stub_iteration;
0441f94f
AM
3328
3329/* After 20 iterations of stub sizing we no longer allow stubs to
3330 shrink. This is to break out of a pathological case where adding
3331 stubs or increasing their size on one iteration decreases section
3332 gaps (perhaps due to alignment), which then results in smaller
3333 stubs on the next iteration. */
3334#define STUB_SHRINK_ITER 20
65f38f15
AM
3335};
3336
4c52953f
AM
3337/* Rename some of the generic section flags to better document how they
3338 are used here. */
b0dddeec
AM
3339
3340/* Nonzero if this section has TLS related relocations. */
3341#define has_tls_reloc sec_flg0
3342
9737e8af
AM
3343/* Nonzero if this section has a call to __tls_get_addr lacking marker
3344 relocations. */
3345#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3346
3347/* Nonzero if this section has any toc or got relocs. */
3348#define has_toc_reloc sec_flg2
3349
3350/* Nonzero if this section has a call to another section that uses
3351 the toc or got. */
d77c8a4b 3352#define makes_toc_func_call sec_flg3
b0dddeec
AM
3353
3354/* Recursion protection when determining above flag. */
d77c8a4b 3355#define call_check_in_progress sec_flg4
70cc837d 3356#define call_check_done sec_flg5
4c52953f 3357
65f38f15
AM
3358/* Get the ppc64 ELF linker hash table from a link_info structure. */
3359
3360#define ppc_hash_table(p) \
573eb292
AM
3361 ((is_elf_hash_table ((p)->hash) \
3362 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3363 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3364
721956f4
AM
3365#define ppc_stub_hash_lookup(table, string, create, copy) \
3366 ((struct ppc_stub_hash_entry *) \
3367 bfd_hash_lookup ((table), (string), (create), (copy)))
3368
3369#define ppc_branch_hash_lookup(table, string, create, copy) \
3370 ((struct ppc_branch_hash_entry *) \
3371 bfd_hash_lookup ((table), (string), (create), (copy)))
3372
3373/* Create an entry in the stub hash table. */
3374
3375static struct bfd_hash_entry *
4ce794b7
AM
3376stub_hash_newfunc (struct bfd_hash_entry *entry,
3377 struct bfd_hash_table *table,
3378 const char *string)
721956f4
AM
3379{
3380 /* Allocate the structure if it has not already been allocated by a
3381 subclass. */
3382 if (entry == NULL)
3383 {
3384 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3385 if (entry == NULL)
3386 return entry;
3387 }
3388
3389 /* Call the allocation method of the superclass. */
3390 entry = bfd_hash_newfunc (entry, table, string);
3391 if (entry != NULL)
3392 {
3393 struct ppc_stub_hash_entry *eh;
3394
3395 /* Initialize the local fields. */
3396 eh = (struct ppc_stub_hash_entry *) entry;
7aba54da
AM
3397 eh->type.main = ppc_stub_none;
3398 eh->type.sub = ppc_stub_toc;
3399 eh->type.r2save = 0;
6f20ed8a 3400 eh->group = NULL;
721956f4
AM
3401 eh->stub_offset = 0;
3402 eh->target_value = 0;
3403 eh->target_section = NULL;
721956f4 3404 eh->h = NULL;
6911b7dc 3405 eh->plt_ent = NULL;
ecc741e6 3406 eh->symtype = 0;
6911b7dc 3407 eh->other = 0;
ecc741e6 3408 eh->id = 0;
721956f4
AM
3409 }
3410
3411 return entry;
3412}
3413
3414/* Create an entry in the branch hash table. */
3415
3416static struct bfd_hash_entry *
4ce794b7
AM
3417branch_hash_newfunc (struct bfd_hash_entry *entry,
3418 struct bfd_hash_table *table,
3419 const char *string)
721956f4
AM
3420{
3421 /* Allocate the structure if it has not already been allocated by a
3422 subclass. */
3423 if (entry == NULL)
3424 {
3425 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3426 if (entry == NULL)
3427 return entry;
3428 }
3429
3430 /* Call the allocation method of the superclass. */
3431 entry = bfd_hash_newfunc (entry, table, string);
3432 if (entry != NULL)
3433 {
3434 struct ppc_branch_hash_entry *eh;
3435
3436 /* Initialize the local fields. */
3437 eh = (struct ppc_branch_hash_entry *) entry;
3438 eh->offset = 0;
3439 eh->iter = 0;
3440 }
3441
3442 return entry;
3443}
3444
65f38f15
AM
3445/* Create an entry in a ppc64 ELF linker hash table. */
3446
3447static struct bfd_hash_entry *
4ce794b7
AM
3448link_hash_newfunc (struct bfd_hash_entry *entry,
3449 struct bfd_hash_table *table,
3450 const char *string)
65f38f15
AM
3451{
3452 /* Allocate the structure if it has not already been allocated by a
3453 subclass. */
3454 if (entry == NULL)
3455 {
3456 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3457 if (entry == NULL)
3458 return entry;
3459 }
3460
3461 /* Call the allocation method of the superclass. */
3462 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3463 if (entry != NULL)
3464 {
3465 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3466
b3fac117 3467 memset (&eh->u.stub_cache, 0,
908b32fc 3468 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3469 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3470
3471 /* When making function calls, old ABI code references function entry
3472 points (dot symbols), while new ABI code references the function
3473 descriptor symbol. We need to make any combination of reference and
3474 definition work together, without breaking archive linking.
3475
3476 For a defined function "foo" and an undefined call to "bar":
3477 An old object defines "foo" and ".foo", references ".bar" (possibly
3478 "bar" too).
3479 A new object defines "foo" and references "bar".
3480
3481 A new object thus has no problem with its undefined symbols being
3482 satisfied by definitions in an old object. On the other hand, the
3483 old object won't have ".bar" satisfied by a new object.
3484
3485 Keep a list of newly added dot-symbols. */
3486
3487 if (string[0] == '.')
3488 {
3489 struct ppc_link_hash_table *htab;
3490
3491 htab = (struct ppc_link_hash_table *) table;
3492 eh->u.next_dot_sym = htab->dot_syms;
3493 htab->dot_syms = eh;
3494 }
65f38f15
AM
3495 }
3496
3497 return entry;
3498}
3499
2cdcc330
AM
3500struct tocsave_entry
3501{
3b421ab3
AM
3502 asection *sec;
3503 bfd_vma offset;
3504};
3505
3506static hashval_t
3507tocsave_htab_hash (const void *p)
3508{
3509 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3510 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3511}
3512
3513static int
3514tocsave_htab_eq (const void *p1, const void *p2)
3515{
3516 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3517 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3518 return e1->sec == e2->sec && e1->offset == e2->offset;
3519}
3520
68faa637
AM
3521/* Destroy a ppc64 ELF linker hash table. */
3522
3523static void
d495ab0d 3524ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3525{
d495ab0d 3526 struct ppc_link_hash_table *htab;
68faa637 3527
d495ab0d 3528 htab = (struct ppc_link_hash_table *) obfd->link.hash;
aefcec16 3529 free (htab->relr);
68faa637
AM
3530 if (htab->tocsave_htab)
3531 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3532 bfd_hash_table_free (&htab->branch_hash_table);
3533 bfd_hash_table_free (&htab->stub_hash_table);
3534 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3535}
3536
65f38f15
AM
3537/* Create a ppc64 ELF linker hash table. */
3538
3539static struct bfd_link_hash_table *
4ce794b7 3540ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3541{
3542 struct ppc_link_hash_table *htab;
986f0783 3543 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3544
4ce794b7 3545 htab = bfd_zmalloc (amt);
65f38f15
AM
3546 if (htab == NULL)
3547 return NULL;
3548
66eb6687 3549 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
f92b077e 3550 sizeof (struct ppc_link_hash_entry)))
65f38f15 3551 {
e2d34d7d 3552 free (htab);
65f38f15
AM
3553 return NULL;
3554 }
3555
721956f4 3556 /* Init the stub hash table too. */
66eb6687
AM
3557 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3558 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3559 {
d495ab0d 3560 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3561 return NULL;
3562 }
721956f4
AM
3563
3564 /* And the branch hash table. */
66eb6687
AM
3565 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3566 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3567 {
3568 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3569 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3570 return NULL;
3571 }
721956f4 3572
3b421ab3
AM
3573 htab->tocsave_htab = htab_try_create (1024,
3574 tocsave_htab_hash,
3575 tocsave_htab_eq,
3576 NULL);
3577 if (htab->tocsave_htab == NULL)
2915c55b 3578 {
d495ab0d 3579 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3580 return NULL;
3581 }
d495ab0d 3582 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3583
3254fd24
AM
3584 /* Initializing two fields of the union is just cosmetic. We really
3585 only care about glist, but when compiled on a 32-bit host the
3586 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3587 debugger inspection of these fields look nicer. */
a6aa5195
AM
3588 htab->elf.init_got_refcount.refcount = 0;
3589 htab->elf.init_got_refcount.glist = NULL;
3590 htab->elf.init_plt_refcount.refcount = 0;
3591 htab->elf.init_plt_refcount.glist = NULL;
3592 htab->elf.init_got_offset.offset = 0;
3593 htab->elf.init_got_offset.glist = NULL;
3594 htab->elf.init_plt_offset.offset = 0;
3595 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3596
65f38f15
AM
3597 return &htab->elf.root;
3598}
3599
bfeb4a28
AM
3600/* Create sections for linker generated code. */
3601
0a1b45a2 3602static bool
bfeb4a28
AM
3603create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3604{
3605 struct ppc_link_hash_table *htab;
3606 flagword flags;
3607
3608 htab = ppc_hash_table (info);
3609
bfeb4a28
AM
3610 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3611 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3612 if (htab->params->save_restore_funcs)
3613 {
3614 /* Create .sfpr for code to save and restore fp regs. */
3615 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3616 flags);
3617 if (htab->sfpr == NULL
fd361982 3618 || !bfd_set_section_alignment (htab->sfpr, 2))
0a1b45a2 3619 return false;
b32547cd
AM
3620 }
3621
3622 if (bfd_link_relocatable (info))
0a1b45a2 3623 return true;
bfeb4a28
AM
3624
3625 /* Create .glink for lazy dynamic linking support. */
3626 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3627 flags);
3628 if (htab->glink == NULL
fd361982 3629 || !bfd_set_section_alignment (htab->glink, 3))
0a1b45a2 3630 return false;
bfeb4a28 3631
9e390558
AM
3632 /* The part of .glink used by global entry stubs, separate so that
3633 it can be aligned appropriately without affecting htab->glink. */
3634 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3635 flags);
3636 if (htab->global_entry == NULL
fd361982 3637 || !bfd_set_section_alignment (htab->global_entry, 2))
0a1b45a2 3638 return false;
9e390558 3639
bfeb4a28
AM
3640 if (!info->no_ld_generated_unwind_info)
3641 {
3642 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3643 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3644 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3645 ".eh_frame",
3646 flags);
3647 if (htab->glink_eh_frame == NULL
fd361982 3648 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
0a1b45a2 3649 return false;
bfeb4a28
AM
3650 }
3651
3652 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3653 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3654 if (htab->elf.iplt == NULL
fd361982 3655 || !bfd_set_section_alignment (htab->elf.iplt, 3))
0a1b45a2 3656 return false;
bfeb4a28
AM
3657
3658 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3659 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3660 htab->elf.irelplt
3661 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3662 if (htab->elf.irelplt == NULL
fd361982 3663 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
0a1b45a2 3664 return false;
bfeb4a28
AM
3665
3666 /* Create branch lookup table for plt_branch stubs. */
3667 flags = (SEC_ALLOC | SEC_LOAD
3668 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3669 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3670 flags);
3671 if (htab->brlt == NULL
fd361982 3672 || !bfd_set_section_alignment (htab->brlt, 3))
0a1b45a2 3673 return false;
bfeb4a28 3674
2d7ad24e
AM
3675 /* Local plt entries, put in .branch_lt but a separate section for
3676 convenience. */
3677 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3678 flags);
3679 if (htab->pltlocal == NULL
fd361982 3680 || !bfd_set_section_alignment (htab->pltlocal, 3))
0a1b45a2 3681 return false;
2d7ad24e 3682
0e1862bb 3683 if (!bfd_link_pic (info))
0a1b45a2 3684 return true;
bfeb4a28
AM
3685
3686 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3687 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3688 htab->relbrlt
3689 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3690 if (htab->relbrlt == NULL
fd361982 3691 || !bfd_set_section_alignment (htab->relbrlt, 3))
0a1b45a2 3692 return false;
bfeb4a28 3693
2d7ad24e
AM
3694 htab->relpltlocal
3695 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3696 if (htab->relpltlocal == NULL
fd361982 3697 || !bfd_set_section_alignment (htab->relpltlocal, 3))
0a1b45a2 3698 return false;
2d7ad24e 3699
0a1b45a2 3700 return true;
bfeb4a28
AM
3701}
3702
e717da7e
AM
3703/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3704
0a1b45a2 3705bool
e7d1c40c
AM
3706ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3707 struct ppc64_elf_params *params)
e717da7e
AM
3708{
3709 struct ppc_link_hash_table *htab;
3710
e7d1c40c 3711 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3712
3713/* Always hook our dynamic sections into the first bfd, which is the
3714 linker created stub bfd. This ensures that the GOT header is at
3715 the start of the output TOC section. */
3716 htab = ppc_hash_table (info);
e7d1c40c
AM
3717 htab->elf.dynobj = params->stub_bfd;
3718 htab->params = params;
bfeb4a28 3719
bfeb4a28 3720 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3721}
3722
721956f4
AM
3723/* Build a name for an entry in the stub hash table. */
3724
3725static char *
4ce794b7
AM
3726ppc_stub_name (const asection *input_section,
3727 const asection *sym_sec,
3728 const struct ppc_link_hash_entry *h,
3729 const Elf_Internal_Rela *rel)
721956f4
AM
3730{
3731 char *stub_name;
bcaa2f82 3732 ssize_t len;
721956f4
AM
3733
3734 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3735 offsets from a sym as a branch target? In fact, we could
3736 probably assume the addend is always zero. */
3737 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3738
3739 if (h)
3740 {
3741 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3742 stub_name = bfd_malloc (len);
46de2a7c
AM
3743 if (stub_name == NULL)
3744 return stub_name;
3745
bcaa2f82
AM
3746 len = sprintf (stub_name, "%08x.%s+%x",
3747 input_section->id & 0xffffffff,
3748 h->elf.root.root.string,
3749 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3750 }
3751 else
3752 {
ad8e1ba5 3753 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3754 stub_name = bfd_malloc (len);
46de2a7c
AM
3755 if (stub_name == NULL)
3756 return stub_name;
3757
bcaa2f82
AM
3758 len = sprintf (stub_name, "%08x.%x:%x+%x",
3759 input_section->id & 0xffffffff,
3760 sym_sec->id & 0xffffffff,
3761 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3762 (int) rel->r_addend & 0xffffffff);
721956f4 3763 }
bcaa2f82 3764 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3765 stub_name[len - 2] = 0;
721956f4
AM
3766 return stub_name;
3767}
3768
e10a07b3 3769/* If mixing power10 with non-power10 code and --power10-stubs is not
7aba54da
AM
3770 specified (or is auto) then there may be multiple stub types for any
3771 given symbol. Up to three classes of stubs are stored in separate
3772 stub_hash_table entries having the same key string. The entries
3773 will always be adjacent on entry->root.next chain, even if hash
3774 table resizing occurs. This function selects the correct entry to
3775 use. */
e10a07b3
AM
3776
3777static struct ppc_stub_hash_entry *
7aba54da
AM
3778select_alt_stub (struct ppc_stub_hash_entry *entry,
3779 enum elf_ppc64_reloc_type r_type)
e10a07b3 3780{
7aba54da 3781 enum ppc_stub_sub_type subt;
e10a07b3 3782
7aba54da
AM
3783 switch (r_type)
3784 {
3785 case R_PPC64_REL24_NOTOC:
3786 subt = ppc_stub_notoc;
3787 break;
3788 case R_PPC64_REL24_P9NOTOC:
3789 subt = ppc_stub_p9notoc;
3790 break;
3791 default:
3792 subt = ppc_stub_toc;
3793 break;
3794 }
e10a07b3 3795
7aba54da 3796 while (entry != NULL && entry->type.sub != subt)
e10a07b3
AM
3797 {
3798 const char *stub_name = entry->root.string;
3799
3800 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3801 if (entry != NULL
3802 && entry->root.string != stub_name)
3803 entry = NULL;
3804 }
3805
3806 return entry;
3807}
3808
721956f4
AM
3809/* Look up an entry in the stub hash. Stub entries are cached because
3810 creating the stub name takes a bit of time. */
3811
3812static struct ppc_stub_hash_entry *
4ce794b7
AM
3813ppc_get_stub_entry (const asection *input_section,
3814 const asection *sym_sec,
039b3fef 3815 struct ppc_link_hash_entry *h,
4ce794b7
AM
3816 const Elf_Internal_Rela *rel,
3817 struct ppc_link_hash_table *htab)
721956f4
AM
3818{
3819 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3820 struct map_stub *group;
721956f4
AM
3821
3822 /* If this input section is part of a group of sections sharing one
3823 stub section, then use the id of the first section in the group.
3824 Stub names need to include a section id, as there may well be
3825 more than one stub used to reach say, printf, and we need to
3826 distinguish between them. */
6f20ed8a 3827 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3828 if (group == NULL)
3829 return NULL;
721956f4 3830
b3fac117
AM
3831 if (h != NULL && h->u.stub_cache != NULL
3832 && h->u.stub_cache->h == h
6f20ed8a 3833 && h->u.stub_cache->group == group)
721956f4 3834 {
b3fac117 3835 stub_entry = h->u.stub_cache;
721956f4
AM
3836 }
3837 else
3838 {
3839 char *stub_name;
3840
6f20ed8a 3841 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3842 if (stub_name == NULL)
3843 return NULL;
3844
3845 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 3846 stub_name, false, false);
721956f4 3847 if (h != NULL)
b3fac117 3848 h->u.stub_cache = stub_entry;
721956f4
AM
3849
3850 free (stub_name);
3851 }
3852
e10a07b3 3853 if (stub_entry != NULL && htab->params->power10_stubs == -1)
7aba54da 3854 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
e10a07b3 3855
721956f4
AM
3856 return stub_entry;
3857}
3858
3859/* Add a new stub entry to the stub hash. Not all fields of the new
3860 stub entry are initialised. */
3861
3862static struct ppc_stub_hash_entry *
4ce794b7
AM
3863ppc_add_stub (const char *stub_name,
3864 asection *section,
25f53a85 3865 struct bfd_link_info *info)
721956f4 3866{
25f53a85 3867 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3868 struct map_stub *group;
721956f4
AM
3869 asection *link_sec;
3870 asection *stub_sec;
3871 struct ppc_stub_hash_entry *stub_entry;
3872
6f20ed8a
AM
3873 group = htab->sec_info[section->id].u.group;
3874 link_sec = group->link_sec;
3875 stub_sec = group->stub_sec;
721956f4
AM
3876 if (stub_sec == NULL)
3877 {
6f20ed8a
AM
3878 size_t namelen;
3879 bfd_size_type len;
3880 char *s_name;
721956f4 3881
6f20ed8a
AM
3882 namelen = strlen (link_sec->name);
3883 len = namelen + sizeof (STUB_SUFFIX);
3884 s_name = bfd_alloc (htab->params->stub_bfd, len);
3885 if (s_name == NULL)
3886 return NULL;
721956f4 3887
6f20ed8a
AM
3888 memcpy (s_name, link_sec->name, namelen);
3889 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3890 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3891 if (stub_sec == NULL)
3892 return NULL;
3893 group->stub_sec = stub_sec;
721956f4
AM
3894 }
3895
3896 /* Enter this entry into the linker stub hash table. */
3897 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
aefcec16 3898 true, true);
721956f4
AM
3899 if (stub_entry == NULL)
3900 {
695344c0 3901 /* xgettext:c-format */
cf97bcb0
AM
3902 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3903 section->owner, stub_name);
721956f4
AM
3904 return NULL;
3905 }
3906
6f20ed8a 3907 stub_entry->group = group;
721956f4 3908 stub_entry->stub_offset = 0;
721956f4
AM
3909 return stub_entry;
3910}
3911
7aba54da
AM
3912/* A stub has already been created, but it may not be the required
3913 type. We shouldn't be transitioning from plt_call to long_branch
3914 stubs or vice versa, but we might be upgrading from plt_call to
3915 plt_call with r2save for example. */
3916
3917static bool
3918ppc_merge_stub (struct ppc_link_hash_table *htab,
3919 struct ppc_stub_hash_entry *stub_entry,
3920 struct ppc_stub_type stub_type,
3921 enum elf_ppc64_reloc_type r_type)
3922{
3923 struct ppc_stub_type old_type = stub_entry->type;
3924
3925 if (old_type.main == ppc_stub_save_res)
3926 return true;
3927
3928 if (htab->params->power10_stubs == -1)
3929 {
3930 /* For --power10-stubs=auto, don't merge _notoc and other
3931 varieties of stubs. */
3932 struct ppc_stub_hash_entry *alt_stub;
3933
3934 alt_stub = select_alt_stub (stub_entry, r_type);
3935 if (alt_stub == NULL)
3936 {
3937 alt_stub = ((struct ppc_stub_hash_entry *)
3938 stub_hash_newfunc (NULL,
3939 &htab->stub_hash_table,
3940 stub_entry->root.string));
3941 if (alt_stub == NULL)
3942 return false;
3943
3944 *alt_stub = *stub_entry;
3945 stub_entry->root.next = &alt_stub->root;
3946
3947 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3948 Not that it matters, this just puts smaller stubs first. */
3949 if (stub_type.sub == ppc_stub_notoc)
3950 alt_stub = stub_entry;
3951 else if (stub_type.sub == ppc_stub_p9notoc
3952 && alt_stub->root.next
3953 && alt_stub->root.next->string == alt_stub->root.string)
3954 {
3955 struct ppc_stub_hash_entry *next
3956 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3957 alt_stub->type = next->type;
3958 alt_stub = next;
3959 }
3960 alt_stub->type = stub_type;
3961 return true;
3962 }
3963 stub_entry = alt_stub;
3964 }
3965
3966 old_type = stub_entry->type;
3967 if (old_type.main == ppc_stub_plt_branch)
04cb922a 3968 old_type.main = ppc_stub_long_branch;
7aba54da
AM
3969
3970 if (old_type.main != stub_type.main
3971 || (old_type.sub != stub_type.sub
3972 && old_type.sub != ppc_stub_toc
3973 && stub_type.sub != ppc_stub_toc))
3974 abort ();
3975
3976 stub_entry->type.sub |= stub_type.sub;
3977 stub_entry->type.r2save |= stub_type.r2save;
3978 return true;
3979}
3980
e717da7e
AM
3981/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3982 not already done. */
65f38f15 3983
0a1b45a2 3984static bool
e717da7e 3985create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3986{
e717da7e
AM
3987 asection *got, *relgot;
3988 flagword flags;
3989 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3990
0c8d6e5c 3991 if (!is_ppc64_elf (abfd))
0a1b45a2 3992 return false;
4dfe6ac6 3993 if (htab == NULL)
0a1b45a2 3994 return false;
0ffa91dd 3995
33e44f2e
AM
3996 if (!htab->elf.sgot
3997 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
0a1b45a2 3998 return false;
e717da7e
AM
3999
4000 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4001 | SEC_LINKER_CREATED);
4002
c456f082 4003 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4004 if (!got
fd361982 4005 || !bfd_set_section_alignment (got, 3))
0a1b45a2 4006 return false;
65f38f15 4007
c456f082
AM
4008 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4009 flags | SEC_READONLY);
e717da7e 4010 if (!relgot
fd361982 4011 || !bfd_set_section_alignment (relgot, 3))
0a1b45a2 4012 return false;
e717da7e
AM
4013
4014 ppc64_elf_tdata (abfd)->got = got;
4015 ppc64_elf_tdata (abfd)->relgot = relgot;
0a1b45a2 4016 return true;
65f38f15 4017}
5bd4f169 4018
b31867b6
AM
4019/* Follow indirect and warning symbol links. */
4020
4021static inline struct bfd_link_hash_entry *
4022follow_link (struct bfd_link_hash_entry *h)
4023{
4024 while (h->type == bfd_link_hash_indirect
4025 || h->type == bfd_link_hash_warning)
4026 h = h->u.i.link;
4027 return h;
4028}
4029
4030static inline struct elf_link_hash_entry *
4031elf_follow_link (struct elf_link_hash_entry *h)
4032{
4033 return (struct elf_link_hash_entry *) follow_link (&h->root);
4034}
4035
4036static inline struct ppc_link_hash_entry *
4037ppc_follow_link (struct ppc_link_hash_entry *h)
4038{
ed7007c1 4039 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
4040}
4041
40d16e0b
AM
4042/* Merge PLT info on FROM with that on TO. */
4043
4044static void
4045move_plt_plist (struct ppc_link_hash_entry *from,
4046 struct ppc_link_hash_entry *to)
4047{
4048 if (from->elf.plt.plist != NULL)
4049 {
4050 if (to->elf.plt.plist != NULL)
4051 {
4052 struct plt_entry **entp;
4053 struct plt_entry *ent;
4054
4055 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4056 {
4057 struct plt_entry *dent;
4058
4059 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4060 if (dent->addend == ent->addend)
4061 {
4062 dent->plt.refcount += ent->plt.refcount;
4063 *entp = ent->next;
4064 break;
4065 }
4066 if (dent == NULL)
4067 entp = &ent->next;
4068 }
4069 *entp = to->elf.plt.plist;
4070 }
4071
4072 to->elf.plt.plist = from->elf.plt.plist;
4073 from->elf.plt.plist = NULL;
4074 }
4075}
4076
65f38f15
AM
4077/* Copy the extra info we tack onto an elf_link_hash_entry. */
4078
4079static void
fcfa13d2
AM
4080ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4081 struct elf_link_hash_entry *dir,
4082 struct elf_link_hash_entry *ind)
65f38f15
AM
4083{
4084 struct ppc_link_hash_entry *edir, *eind;
4085
ed7007c1
AM
4086 edir = ppc_elf_hash_entry (dir);
4087 eind = ppc_elf_hash_entry (ind);
65f38f15 4088
c79d6685
AM
4089 edir->is_func |= eind->is_func;
4090 edir->is_func_descriptor |= eind->is_func_descriptor;
4091 edir->tls_mask |= eind->tls_mask;
4092 if (eind->oh != NULL)
4093 edir->oh = ppc_follow_link (eind->oh);
4094
474436e6 4095 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4096 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4097 edir->elf.ref_regular |= eind->elf.ref_regular;
4098 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4099 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4100 edir->elf.needs_plt |= eind->elf.needs_plt;
4101 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4102
d311bc8b
AM
4103 /* If we were called to copy over info for a weak sym, don't copy
4104 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4105 in order to simplify readonly_dynrelocs and save a field in the
4106 symbol hash entry, but that means dyn_relocs can't be used in any
4107 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4108 are then tested. */
d311bc8b 4109 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4110 return;
d311bc8b 4111
411e1bfb 4112 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 4113 if (ind->dyn_relocs != NULL)
65f38f15 4114 {
190eb1dd 4115 if (dir->dyn_relocs != NULL)
bbd7ec4a 4116 {
1657026c
AM
4117 struct ppc_dyn_relocs **pp;
4118 struct ppc_dyn_relocs *p;
bbd7ec4a 4119
fcfa13d2 4120 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 4121 list. Merge any entries against the same section. */
1657026c
AM
4122 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4123 (p = *pp) != NULL;
4124 )
bbd7ec4a 4125 {
1657026c 4126 struct ppc_dyn_relocs *q;
bbd7ec4a 4127
1657026c
AM
4128 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4129 q != NULL;
4130 q = q->next)
bbd7ec4a
AM
4131 if (q->sec == p->sec)
4132 {
bbd7ec4a 4133 q->count += p->count;
1657026c
AM
4134 q->pc_count += p->pc_count;
4135 q->rel_count += p->rel_count;
bbd7ec4a
AM
4136 *pp = p->next;
4137 break;
4138 }
4139 if (q == NULL)
4140 pp = &p->next;
4141 }
1657026c 4142 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
bbd7ec4a
AM
4143 }
4144
190eb1dd
L
4145 dir->dyn_relocs = ind->dyn_relocs;
4146 ind->dyn_relocs = NULL;
65f38f15 4147 }
65f38f15 4148
81848ca0
AM
4149 /* Copy over got entries that we may have already seen to the
4150 symbol which just became indirect. */
411e1bfb
AM
4151 if (eind->elf.got.glist != NULL)
4152 {
4153 if (edir->elf.got.glist != NULL)
4154 {
4155 struct got_entry **entp;
4156 struct got_entry *ent;
4157
4158 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4159 {
4160 struct got_entry *dent;
4161
4162 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4163 if (dent->addend == ent->addend
e717da7e 4164 && dent->owner == ent->owner
411e1bfb
AM
4165 && dent->tls_type == ent->tls_type)
4166 {
4167 dent->got.refcount += ent->got.refcount;
4168 *entp = ent->next;
4169 break;
4170 }
4171 if (dent == NULL)
4172 entp = &ent->next;
4173 }
4174 *entp = edir->elf.got.glist;
4175 }
4176
4177 edir->elf.got.glist = eind->elf.got.glist;
4178 eind->elf.got.glist = NULL;
4179 }
4180
4181 /* And plt entries. */
40d16e0b 4182 move_plt_plist (eind, edir);
411e1bfb 4183
fcfa13d2 4184 if (eind->elf.dynindx != -1)
411e1bfb 4185 {
fcfa13d2
AM
4186 if (edir->elf.dynindx != -1)
4187 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4188 edir->elf.dynstr_index);
411e1bfb
AM
4189 edir->elf.dynindx = eind->elf.dynindx;
4190 edir->elf.dynstr_index = eind->elf.dynstr_index;
4191 eind->elf.dynindx = -1;
4192 eind->elf.dynstr_index = 0;
4193 }
411e1bfb
AM
4194}
4195
8387904d
AM
4196/* Find the function descriptor hash entry from the given function code
4197 hash entry FH. Link the entries via their OH fields. */
4198
4199static struct ppc_link_hash_entry *
b31867b6 4200lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4201{
4202 struct ppc_link_hash_entry *fdh = fh->oh;
4203
4204 if (fdh == NULL)
4205 {
4206 const char *fd_name = fh->elf.root.root.string + 1;
4207
ed7007c1 4208 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
0a1b45a2 4209 false, false, false));
b31867b6
AM
4210 if (fdh == NULL)
4211 return fdh;
4212
4213 fdh->is_func_descriptor = 1;
4214 fdh->oh = fh;
4215 fh->is_func = 1;
4216 fh->oh = fdh;
8387904d
AM
4217 }
4218
8c5b4e52
AM
4219 fdh = ppc_follow_link (fdh);
4220 fdh->is_func_descriptor = 1;
4221 fdh->oh = fh;
4222 return fdh;
8387904d
AM
4223}
4224
8c5b4e52 4225/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4226
4227static struct ppc_link_hash_entry *
4228make_fdh (struct bfd_link_info *info,
908b32fc 4229 struct ppc_link_hash_entry *fh)
bb700d78 4230{
8c5b4e52
AM
4231 bfd *abfd = fh->elf.root.u.undef.abfd;
4232 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4233 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4234 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4235 ? BSF_WEAK
4236 : BSF_GLOBAL);
4237
4238 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4239 fh->elf.root.root.string + 1,
4240 flags, bfd_und_section_ptr, 0,
0a1b45a2 4241 NULL, false, false, &bh))
bb700d78
AM
4242 return NULL;
4243
4244 fdh = (struct ppc_link_hash_entry *) bh;
4245 fdh->elf.non_elf = 0;
908b32fc
AM
4246 fdh->fake = 1;
4247 fdh->is_func_descriptor = 1;
4248 fdh->oh = fh;
4249 fh->is_func = 1;
4250 fh->oh = fdh;
bb700d78
AM
4251 return fdh;
4252}
4253
8387904d
AM
4254/* Fix function descriptor symbols defined in .opd sections to be
4255 function type. */
555cd476 4256
0a1b45a2 4257static bool
c16153ae 4258ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4259 struct bfd_link_info *info,
555cd476 4260 Elf_Internal_Sym *isym,
6911b7dc 4261 const char **name,
555cd476
AM
4262 flagword *flags ATTRIBUTE_UNUSED,
4263 asection **sec,
b53dfeb2 4264 bfd_vma *value)
555cd476 4265{
b53dfeb2 4266 if (*sec != NULL
f1885d1e 4267 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4268 {
4269 asection *code_sec;
4270
4271 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4272 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4273 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4274
4275 /* If the symbol is a function defined in .opd, and the function
4276 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4277 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4278 && (*sec)->reloc_count != 0
4279 && opd_entry_value (*sec, *value, &code_sec, NULL,
0a1b45a2 4280 false) != (bfd_vma) -1
b53dfeb2
AM
4281 && discarded_section (code_sec))
4282 {
4283 *sec = bfd_und_section_ptr;
4284 isym->st_shndx = SHN_UNDEF;
4285 }
4286 }
dbd1e97e
AM
4287 else if (*sec != NULL
4288 && strcmp ((*sec)->name, ".toc") == 0
4289 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4290 {
4291 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4292 if (htab != NULL)
4293 htab->params->object_in_toc = 1;
4294 }
433817dd 4295
6911b7dc
AM
4296 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4297 {
4298 if (abiversion (ibfd) == 0)
4299 set_abiversion (ibfd, 2);
4300 else if (abiversion (ibfd) == 1)
4301 {
cf97bcb0
AM
4302 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4303 " for ABI version 1"), *name);
6911b7dc 4304 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4305 return false;
6911b7dc
AM
4306 }
4307 }
4308
0a1b45a2 4309 return true;
555cd476
AM
4310}
4311
6911b7dc
AM
4312/* Merge non-visibility st_other attributes: local entry point. */
4313
4314static void
4315ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4316 unsigned int st_other,
0a1b45a2
AM
4317 bool definition,
4318 bool dynamic)
6911b7dc 4319{
f378ab09 4320 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4321 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4322 | ELF_ST_VISIBILITY (h->other));
4323}
4324
8c5b4e52
AM
4325/* Hook called on merging a symbol. We use this to clear "fake" since
4326 we now have a real symbol. */
4327
0a1b45a2 4328static bool
8c5b4e52 4329ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4330 const Elf_Internal_Sym *isym,
8c5b4e52 4331 asection **psec ATTRIBUTE_UNUSED,
0a1b45a2
AM
4332 bool newdef ATTRIBUTE_UNUSED,
4333 bool olddef ATTRIBUTE_UNUSED,
8c5b4e52
AM
4334 bfd *oldbfd ATTRIBUTE_UNUSED,
4335 const asection *oldsec ATTRIBUTE_UNUSED)
4336{
ed7007c1 4337 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4338 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4339 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
0a1b45a2 4340 return true;
8c5b4e52
AM
4341}
4342
8387904d 4343/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4344 inclusion of a new ABI object archive that defines "bar".
4345 NAME is a symbol defined in an archive. Return a symbol in the hash
4346 table that might be satisfied by the archive symbols. */
8387904d 4347
b585e899 4348static struct bfd_link_hash_entry *
8387904d
AM
4349ppc64_elf_archive_symbol_lookup (bfd *abfd,
4350 struct bfd_link_info *info,
4351 const char *name)
4352{
b585e899 4353 struct bfd_link_hash_entry *h;
8387904d
AM
4354 char *dot_name;
4355 size_t len;
4356
4357 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc 4358 if (h != NULL
b585e899 4359 && ppc_hash_table (info) != NULL
908b32fc
AM
4360 /* Don't return this sym if it is a fake function descriptor
4361 created by add_symbol_adjust. */
b585e899 4362 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4363 return h;
4364
4365 if (name[0] == '.')
4366 return h;
4367
4368 len = strlen (name);
4369 dot_name = bfd_alloc (abfd, len + 2);
4370 if (dot_name == NULL)
b585e899 4371 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
4372 dot_name[0] = '.';
4373 memcpy (dot_name + 1, name, len + 1);
4374 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4375 bfd_release (abfd, dot_name);
a804e476
AM
4376 if (h != NULL)
4377 return h;
4378
4379 if (strcmp (name, "__tls_get_addr_opt") == 0)
4380 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4381 return h;
4382}
4383
4384/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4385 new ABI object defines "bar". Well, at least, undefined dot symbols
4386 are made weak. This stops later archive searches from including an
4387 object if we already have a function descriptor definition. It also
35b0ce59
AM
4388 prevents the linker complaining about undefined symbols.
4389 We also check and correct mismatched symbol visibility here. The
4390 most restrictive visibility of the function descriptor and the
4391 function entry symbol is used. */
8387904d 4392
0a1b45a2 4393static bool
b3fac117 4394add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4395{
8387904d
AM
4396 struct ppc_link_hash_table *htab;
4397 struct ppc_link_hash_entry *fdh;
4398
b3fac117
AM
4399 if (eh->elf.root.type == bfd_link_hash_warning)
4400 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4401
8400d40d 4402 if (eh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 4403 return true;
8400d40d 4404
b3fac117
AM
4405 if (eh->elf.root.root.string[0] != '.')
4406 abort ();
8387904d 4407
b3fac117 4408 htab = ppc_hash_table (info);
4dfe6ac6 4409 if (htab == NULL)
0a1b45a2 4410 return false;
4dfe6ac6 4411
b31867b6 4412 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4413 if (fdh == NULL
4414 && !bfd_link_relocatable (info)
4415 && (eh->elf.root.type == bfd_link_hash_undefined
4416 || eh->elf.root.type == bfd_link_hash_undefweak)
4417 && eh->elf.ref_regular)
4418 {
4419 /* Make an undefined function descriptor sym, in order to
4420 pull in an --as-needed shared lib. Archives are handled
4421 elsewhere. */
4422 fdh = make_fdh (info, eh);
4423 if (fdh == NULL)
0a1b45a2 4424 return false;
bb700d78 4425 }
8c5b4e52
AM
4426
4427 if (fdh != NULL)
8387904d 4428 {
35b0ce59
AM
4429 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4430 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4431
4432 /* Make both descriptor and entry symbol have the most
4433 constraining visibility of either symbol. */
35b0ce59
AM
4434 if (entry_vis < descr_vis)
4435 fdh->elf.other += entry_vis - descr_vis;
4436 else if (entry_vis > descr_vis)
4437 eh->elf.other += descr_vis - entry_vis;
4438
8c5b4e52
AM
4439 /* Propagate reference flags from entry symbol to function
4440 descriptor symbol. */
bc4e12de 4441 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4442 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4443 fdh->elf.ref_regular |= eh->elf.ref_regular;
4444 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4445
4446 if (!fdh->elf.forced_local
4447 && fdh->elf.dynindx == -1
4448 && fdh->elf.versioned != versioned_hidden
4449 && (bfd_link_dll (info)
4450 || fdh->elf.def_dynamic
4451 || fdh->elf.ref_dynamic)
4452 && (eh->elf.ref_regular
4453 || eh->elf.def_regular))
4454 {
2cdcc330 4455 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 4456 return false;
35b0ce59 4457 }
8387904d 4458 }
99877b66 4459
0a1b45a2 4460 return true;
8387904d
AM
4461}
4462
f6c7c3e8
AM
4463/* Set up opd section info and abiversion for IBFD, and process list
4464 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4465
0a1b45a2 4466static bool
f6c7c3e8 4467ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4468{
99877b66 4469 struct ppc_link_hash_table *htab;
b3fac117 4470 struct ppc_link_hash_entry **p, *eh;
459609d6 4471 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4472
459609d6 4473 if (opd != NULL && opd->size != 0)
b3fac117 4474 {
6313825c
AM
4475 if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4476 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4477 else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4478 BFD_FAIL ();
b9399fcf 4479
459609d6
AM
4480 if (abiversion (ibfd) == 0)
4481 set_abiversion (ibfd, 1);
8a2058b5 4482 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4483 {
695344c0 4484 /* xgettext:c-format */
cf97bcb0
AM
4485 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4486 ibfd, abiversion (ibfd));
459609d6 4487 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4488 return false;
f6c7c3e8 4489 }
b9399fcf 4490 }
f6c7c3e8 4491
b9399fcf
AM
4492 if (is_ppc64_elf (info->output_bfd))
4493 {
4494 /* For input files without an explicit abiversion in e_flags
4495 we should have flagged any with symbol st_other bits set
ac1b12b5 4496 as ELFv2 and above flagged those with .opd as ELFv1.
b9399fcf
AM
4497 Set the output abiversion if not yet set, and for any input
4498 still ambiguous, take its abiversion from the output.
4499 Differences in ABI are reported later. */
4500 if (abiversion (info->output_bfd) == 0)
4501 set_abiversion (info->output_bfd, abiversion (ibfd));
4502 else if (abiversion (ibfd) == 0)
4503 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4504 }
4505
459609d6
AM
4506 htab = ppc_hash_table (info);
4507 if (htab == NULL)
0a1b45a2 4508 return true;
459609d6 4509
b9399fcf
AM
4510 if (opd != NULL && opd->size != 0
4511 && (ibfd->flags & DYNAMIC) == 0
4512 && (opd->flags & SEC_RELOC) != 0
4513 && opd->reloc_count != 0
4514 && !bfd_is_abs_section (opd->output_section)
4515 && info->gc_sections)
4516 {
4517 /* Garbage collection needs some extra help with .opd sections.
4518 We don't want to necessarily keep everything referenced by
4519 relocs in .opd, as that would keep all functions. Instead,
4520 if we reference an .opd symbol (a function descriptor), we
4521 want to keep the function code symbol's section. This is
4522 easy for global symbols, but for local syms we need to keep
4523 information about the associated function section. */
4524 bfd_size_type amt;
4525 asection **opd_sym_map;
4526 Elf_Internal_Shdr *symtab_hdr;
4527 Elf_Internal_Rela *relocs, *rel_end, *rel;
4528
4529 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4530 opd_sym_map = bfd_zalloc (ibfd, amt);
4531 if (opd_sym_map == NULL)
0a1b45a2 4532 return false;
b9399fcf
AM
4533 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4534 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4535 info->keep_memory);
4536 if (relocs == NULL)
0a1b45a2 4537 return false;
b9399fcf
AM
4538 symtab_hdr = &elf_symtab_hdr (ibfd);
4539 rel_end = relocs + opd->reloc_count - 1;
4540 for (rel = relocs; rel < rel_end; rel++)
4541 {
4542 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4543 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4544
4545 if (r_type == R_PPC64_ADDR64
4546 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4547 && r_symndx < symtab_hdr->sh_info)
4548 {
4549 Elf_Internal_Sym *isym;
4550 asection *s;
4551
f1dfbfdb
L
4552 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4553 r_symndx);
b9399fcf
AM
4554 if (isym == NULL)
4555 {
4556 if (elf_section_data (opd)->relocs != relocs)
4557 free (relocs);
0a1b45a2 4558 return false;
b9399fcf
AM
4559 }
4560
4561 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4562 if (s != NULL && s != opd)
4563 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4564 }
4565 }
4566 if (elf_section_data (opd)->relocs != relocs)
4567 free (relocs);
4568 }
459609d6
AM
4569
4570 p = &htab->dot_syms;
4571 while ((eh = *p) != NULL)
4572 {
4573 *p = NULL;
4574 if (&eh->elf == htab->elf.hgot)
4575 ;
4576 else if (htab->elf.hgot == NULL
4577 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4578 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4579 else if (abiversion (ibfd) <= 1)
4580 {
4581 htab->need_func_desc_adj = 1;
4582 if (!add_symbol_adjust (eh, info))
0a1b45a2 4583 return false;
8c5b4e52 4584 }
459609d6
AM
4585 p = &eh->u.next_dot_sym;
4586 }
0a1b45a2 4587 return true;
8387904d
AM
4588}
4589
97fed1c9
JJ
4590/* Undo hash table changes when an --as-needed input file is determined
4591 not to be needed. */
4592
0a1b45a2 4593static bool
e5034e59
AM
4594ppc64_elf_notice_as_needed (bfd *ibfd,
4595 struct bfd_link_info *info,
4596 enum notice_asneeded_action act)
97fed1c9 4597{
e5034e59
AM
4598 if (act == notice_not_needed)
4599 {
4600 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4601
e5034e59 4602 if (htab == NULL)
0a1b45a2 4603 return false;
4dfe6ac6 4604
e5034e59
AM
4605 htab->dot_syms = NULL;
4606 }
4607 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4608}
4609
aa374f67
AM
4610/* If --just-symbols against a final linked binary, then assume we need
4611 toc adjusting stubs when calling functions defined there. */
4612
4613static void
4614ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4615{
4616 if ((sec->flags & SEC_CODE) != 0
4617 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4618 && is_ppc64_elf (sec->owner))
4619 {
2c3f079f
AM
4620 if (abiversion (sec->owner) >= 2
4621 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4622 sec->has_toc_reloc = 1;
4623 }
4624 _bfd_elf_link_just_syms (sec, info);
4625}
4626
e054468f 4627static struct plt_entry **
4ce794b7
AM
4628update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4629 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4630{
4631 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4632 struct plt_entry **local_plt;
f961d9dd 4633 unsigned char *local_got_tls_masks;
411e1bfb
AM
4634
4635 if (local_got_ents == NULL)
4636 {
4637 bfd_size_type size = symtab_hdr->sh_info;
4638
e054468f
AM
4639 size *= (sizeof (*local_got_ents)
4640 + sizeof (*local_plt)
4641 + sizeof (*local_got_tls_masks));
4ce794b7 4642 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4643 if (local_got_ents == NULL)
e054468f 4644 return NULL;
411e1bfb
AM
4645 elf_local_got_ents (abfd) = local_got_ents;
4646 }
4647
37da22e5 4648 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4649 {
4650 struct got_entry *ent;
4651
4652 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4653 if (ent->addend == r_addend
4654 && ent->owner == abfd
4655 && ent->tls_type == tls_type)
411e1bfb
AM
4656 break;
4657 if (ent == NULL)
4658 {
986f0783 4659 size_t amt = sizeof (*ent);
4ce794b7 4660 ent = bfd_alloc (abfd, amt);
411e1bfb 4661 if (ent == NULL)
931e4f9b 4662 return NULL;
411e1bfb
AM
4663 ent->next = local_got_ents[r_symndx];
4664 ent->addend = r_addend;
e717da7e 4665 ent->owner = abfd;
411e1bfb 4666 ent->tls_type = tls_type;
0a1b45a2 4667 ent->is_indirect = false;
411e1bfb
AM
4668 ent->got.refcount = 0;
4669 local_got_ents[r_symndx] = ent;
4670 }
4671 ent->got.refcount += 1;
4672 }
4673
e054468f 4674 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4675 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4676 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4677
4678 return local_plt + r_symndx;
65f38f15
AM
4679}
4680
0a1b45a2 4681static bool
e054468f 4682update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4683{
411e1bfb 4684 struct plt_entry *ent;
1e2f5b6e 4685
e054468f 4686 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4687 if (ent->addend == addend)
4688 break;
4689 if (ent == NULL)
1e2f5b6e 4690 {
986f0783 4691 size_t amt = sizeof (*ent);
4ce794b7 4692 ent = bfd_alloc (abfd, amt);
411e1bfb 4693 if (ent == NULL)
0a1b45a2 4694 return false;
e054468f 4695 ent->next = *plist;
411e1bfb
AM
4696 ent->addend = addend;
4697 ent->plt.refcount = 0;
e054468f 4698 *plist = ent;
1e2f5b6e 4699 }
411e1bfb 4700 ent->plt.refcount += 1;
0a1b45a2 4701 return true;
1e2f5b6e
AM
4702}
4703
0a1b45a2 4704static bool
e054468f
AM
4705is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4706{
4707 return (r_type == R_PPC64_REL24
05d0e962 4708 || r_type == R_PPC64_REL24_NOTOC
7aba54da 4709 || r_type == R_PPC64_REL24_P9NOTOC
e054468f
AM
4710 || r_type == R_PPC64_REL14
4711 || r_type == R_PPC64_REL14_BRTAKEN
4712 || r_type == R_PPC64_REL14_BRNTAKEN
4713 || r_type == R_PPC64_ADDR24
4714 || r_type == R_PPC64_ADDR14
4715 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4716 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4717 || r_type == R_PPC64_PLTCALL
4718 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4719}
4720
4721/* Relocs on inline plt call sequence insns prior to the call. */
4722
0a1b45a2 4723static bool
23cedd1d
AM
4724is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4725{
4726 return (r_type == R_PPC64_PLT16_HA
4727 || r_type == R_PPC64_PLT16_HI
4728 || r_type == R_PPC64_PLT16_LO
4729 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4730 || r_type == R_PPC64_PLT_PCREL34
4731 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4732 || r_type == R_PPC64_PLTSEQ
4733 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4734}
4735
733ae98c
AM
4736/* Of relocs which might appear paired with TLSGD and TLSLD marker
4737 relocs, return true for those that operate on a dword. */
4738
4739static bool
4740is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4741{
4742 return (r_type == R_PPC64_PLT_PCREL34
4743 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4744 || r_type == R_PPC64_PLTCALL);
4745}
4746
f91074eb
AM
4747/* The RELR encoding doesn't allow odd addresses, so RELR_ALIGN must
4748 be at least 1. R_PPC64_RELATIVE relocs require alignment of 2**3.
4749 We use 3 here to avoid complexity in relocate_section, where for a
4750 value of 1 we'd need to test for not just an output RELATIVE reloc
4751 near the call to maybe_relr but also UADDR64 and some conditions on
4752 the symbol. See PR30824. */
4753#define RELR_ALIGN 3
4754
4755static bool
4756maybe_relr (enum elf_ppc64_reloc_type r_type,
4757 const Elf_Internal_Rela *rel,
4758 const asection *sec)
4759{
4760 return ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
4761 && (rel->r_offset & ((1 << RELR_ALIGN) - 1)) == 0
4762 && sec->alignment_power >= RELR_ALIGN);
4763}
4764
733ae98c
AM
4765/* Like bfd_reloc_offset_in_range but without a howto. Return true
4766 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4767
4768static bool
4769offset_in_range (asection *sec, bfd_vma offset, size_t size)
4770{
4771 return offset <= sec->size && size <= sec->size - offset;
4772}
4773
5bd4f169 4774/* Look through the relocs for a section during the first phase, and
65f38f15 4775 calculate needed space in the global offset table, procedure
5d1634d7 4776 linkage table, and dynamic reloc sections. */
5bd4f169 4777
0a1b45a2 4778static bool
4ce794b7
AM
4779ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4780 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4781{
65f38f15 4782 struct ppc_link_hash_table *htab;
5bd4f169 4783 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4784 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4785 const Elf_Internal_Rela *rel;
4786 const Elf_Internal_Rela *rel_end;
5bd4f169 4787 asection *sreloc;
3a71aa26 4788 struct elf_link_hash_entry *tga, *dottga;
0a1b45a2 4789 bool is_opd;
5bd4f169 4790
0e1862bb 4791 if (bfd_link_relocatable (info))
0a1b45a2 4792 return true;
5bd4f169 4793
0c8d6e5c 4794 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4795
65f38f15 4796 htab = ppc_hash_table (info);
4dfe6ac6 4797 if (htab == NULL)
0a1b45a2 4798 return false;
4dfe6ac6 4799
3a71aa26 4800 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 4801 false, false, true);
3a71aa26 4802 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 4803 false, false, true);
0ffa91dd 4804 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4805 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4806 sreloc = NULL;
b9399fcf 4807 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4808 rel_end = relocs + sec->reloc_count;
4809 for (rel = relocs; rel < rel_end; rel++)
4810 {
4811 unsigned long r_symndx;
4812 struct elf_link_hash_entry *h;
3a3a4c1f 4813 Elf_Internal_Sym *isym;
04c9666a 4814 enum elf_ppc64_reloc_type r_type;
727fc41e 4815 int tls_type;
7c8fe5c4 4816 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4817 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4818
4819 r_symndx = ELF64_R_SYM (rel->r_info);
4820 if (r_symndx < symtab_hdr->sh_info)
3a3a4c1f
AM
4821 {
4822 h = NULL;
4823 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4824 if (isym == NULL)
4825 return false;
4826 }
5bd4f169 4827 else
973a3492 4828 {
3a3a4c1f 4829 isym = NULL;
973a3492 4830 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4831 h = elf_follow_link (h);
1c865ab2
AM
4832
4833 if (h == htab->elf.hgot)
4834 sec->has_toc_reloc = 1;
973a3492 4835 }
5bd4f169 4836
5663e321
AM
4837 r_type = ELF64_R_TYPE (rel->r_info);
4838 switch (r_type)
4839 {
04bdff6a
AM
4840 case R_PPC64_D34:
4841 case R_PPC64_D34_LO:
4842 case R_PPC64_D34_HI30:
4843 case R_PPC64_D34_HA30:
4844 case R_PPC64_D28:
c213164a
AM
4845 case R_PPC64_TPREL34:
4846 case R_PPC64_DTPREL34:
04bdff6a 4847 case R_PPC64_PCREL34:
5663e321 4848 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4849 case R_PPC64_GOT_TLSGD_PCREL34:
4850 case R_PPC64_GOT_TLSLD_PCREL34:
4851 case R_PPC64_GOT_TPREL_PCREL34:
4852 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4853 case R_PPC64_PLT_PCREL34:
4854 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4855 case R_PPC64_PCREL28:
e10a07b3 4856 htab->has_power10_relocs = 1;
133a1f60
AM
4857 break;
4858 default:
5663e321
AM
4859 break;
4860 }
903b777d
AM
4861
4862 switch (r_type)
4863 {
4864 case R_PPC64_PLT16_HA:
4865 case R_PPC64_GOT_TLSLD16_HA:
4866 case R_PPC64_GOT_TLSGD16_HA:
4867 case R_PPC64_GOT_TPREL16_HA:
4868 case R_PPC64_GOT_DTPREL16_HA:
4869 case R_PPC64_GOT16_HA:
4870 case R_PPC64_TOC16_HA:
4871 case R_PPC64_PLT16_LO:
4872 case R_PPC64_PLT16_LO_DS:
4873 case R_PPC64_GOT_TLSLD16_LO:
4874 case R_PPC64_GOT_TLSGD16_LO:
4875 case R_PPC64_GOT_TPREL16_LO_DS:
4876 case R_PPC64_GOT_DTPREL16_LO_DS:
4877 case R_PPC64_GOT16_LO:
4878 case R_PPC64_GOT16_LO_DS:
4879 case R_PPC64_TOC16_LO:
4880 case R_PPC64_TOC16_LO_DS:
4881 case R_PPC64_GOT_PCREL34:
4882 ppc64_elf_tdata (abfd)->has_optrel = 1;
4883 ppc64_elf_section_data (sec)->has_optrel = 1;
4884 break;
4885 default:
4886 break;
4887 }
4888
f749f26e 4889 ifunc = NULL;
25f23106
AM
4890 if (h != NULL)
4891 {
4892 if (h->type == STT_GNU_IFUNC)
4893 {
4894 h->needs_plt = 1;
4895 ifunc = &h->plt.plist;
4896 }
4897 }
4898 else
4899 {
25f23106
AM
4900 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4901 {
4902 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4903 rel->r_addend,
37da22e5 4904 NON_GOT | PLT_IFUNC);
25f23106 4905 if (ifunc == NULL)
0a1b45a2 4906 return false;
25f23106
AM
4907 }
4908 }
727fc41e 4909
f749f26e 4910 tls_type = 0;
a33d1f77 4911 switch (r_type)
5bd4f169 4912 {
1686dc70
AM
4913 case R_PPC64_PLTSEQ:
4914 case R_PPC64_PLTSEQ_NOTOC:
4915 /* Inline plt call code emitted by gcc doesn't support
4916 modifying the tls_index words to short-circuit
4917 __tls_get_addr calls. See PR32387. */
4918 if (h != NULL && (h == tga || h == dottga))
4919 htab->params->tls_get_addr_opt = 0;
4920 break;
4921
727fc41e
AM
4922 case R_PPC64_TLSGD:
4923 case R_PPC64_TLSLD:
4924 /* These special tls relocs tie a call to __tls_get_addr with
4925 its parameter symbol. */
37da22e5 4926 if (h != NULL)
ed7007c1 4927 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4928 else
4929 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4930 rel->r_addend,
37da22e5 4931 NON_GOT | TLS_TLS | TLS_MARK))
0a1b45a2 4932 return false;
37da22e5 4933 sec->has_tls_reloc = 1;
727fc41e
AM
4934 break;
4935
411e1bfb
AM
4936 case R_PPC64_GOT_TLSLD16:
4937 case R_PPC64_GOT_TLSLD16_LO:
4938 case R_PPC64_GOT_TLSLD16_HI:
4939 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4940 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4941 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4942 goto dogottls;
4943
4944 case R_PPC64_GOT_TLSGD16:
4945 case R_PPC64_GOT_TLSGD16_LO:
4946 case R_PPC64_GOT_TLSGD16_HI:
4947 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4948 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4949 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4950 goto dogottls;
4951
4952 case R_PPC64_GOT_TPREL16_DS:
4953 case R_PPC64_GOT_TPREL16_LO_DS:
4954 case R_PPC64_GOT_TPREL16_HI:
4955 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4956 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4957 if (bfd_link_dll (info))
411e1bfb
AM
4958 info->flags |= DF_STATIC_TLS;
4959 tls_type = TLS_TLS | TLS_TPREL;
4960 goto dogottls;
4961
4962 case R_PPC64_GOT_DTPREL16_DS:
4963 case R_PPC64_GOT_DTPREL16_LO_DS:
4964 case R_PPC64_GOT_DTPREL16_HI:
4965 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4966 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4967 tls_type = TLS_TLS | TLS_DTPREL;
4968 dogottls:
4969 sec->has_tls_reloc = 1;
066f4018 4970 goto dogot;
411e1bfb 4971
903b777d
AM
4972 case R_PPC64_GOT16:
4973 case R_PPC64_GOT16_LO:
4974 case R_PPC64_GOT16_HI:
65f38f15 4975 case R_PPC64_GOT16_HA:
903b777d 4976 case R_PPC64_GOT16_DS:
066f4018 4977 case R_PPC64_GOT16_LO_DS:
4a421c53 4978 case R_PPC64_GOT_PCREL34:
066f4018 4979 dogot:
65f38f15 4980 /* This symbol requires a global offset table entry. */
4c52953f 4981 sec->has_toc_reloc = 1;
33c0ec9d
AM
4982 if (r_type == R_PPC64_GOT_TLSLD16
4983 || r_type == R_PPC64_GOT_TLSGD16
4984 || r_type == R_PPC64_GOT_TPREL16_DS
4985 || r_type == R_PPC64_GOT_DTPREL16_DS
4986 || r_type == R_PPC64_GOT16
4987 || r_type == R_PPC64_GOT16_DS)
4988 {
4989 htab->do_multi_toc = 1;
d77c8a4b 4990 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4991 }
4992
e717da7e
AM
4993 if (ppc64_elf_tdata (abfd)->got == NULL
4994 && !create_got_section (abfd, info))
0a1b45a2 4995 return false;
5bd4f169
AM
4996
4997 if (h != NULL)
4998 {
411e1bfb
AM
4999 struct ppc_link_hash_entry *eh;
5000 struct got_entry *ent;
65f38f15 5001
ed7007c1 5002 eh = ppc_elf_hash_entry (h);
411e1bfb 5003 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 5004 if (ent->addend == rel->r_addend
e717da7e 5005 && ent->owner == abfd
411e1bfb
AM
5006 && ent->tls_type == tls_type)
5007 break;
5008 if (ent == NULL)
5bd4f169 5009 {
986f0783 5010 size_t amt = sizeof (*ent);
4ce794b7 5011 ent = bfd_alloc (abfd, amt);
411e1bfb 5012 if (ent == NULL)
0a1b45a2 5013 return false;
411e1bfb 5014 ent->next = eh->elf.got.glist;
133a1f60 5015 ent->addend = rel->r_addend;
e717da7e 5016 ent->owner = abfd;
411e1bfb 5017 ent->tls_type = tls_type;
0a1b45a2 5018 ent->is_indirect = false;
411e1bfb
AM
5019 ent->got.refcount = 0;
5020 eh->elf.got.glist = ent;
5bd4f169 5021 }
411e1bfb 5022 ent->got.refcount += 1;
e7b938ca 5023 eh->tls_mask |= tls_type;
5bd4f169 5024 }
411e1bfb
AM
5025 else
5026 /* This is a global offset table entry for a local symbol. */
5027 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5028 rel->r_addend, tls_type))
0a1b45a2 5029 return false;
5bd4f169
AM
5030 break;
5031
5bd4f169 5032 case R_PPC64_PLT16_HA:
65f38f15
AM
5033 case R_PPC64_PLT16_HI:
5034 case R_PPC64_PLT16_LO:
08be3224 5035 case R_PPC64_PLT16_LO_DS:
5663e321
AM
5036 case R_PPC64_PLT_PCREL34:
5037 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
5038 case R_PPC64_PLT32:
5039 case R_PPC64_PLT64:
cbf95972
AM
5040 /* This symbol requires a procedure linkage table entry. */
5041 plt_list = ifunc;
5042 if (h != NULL)
e054468f 5043 {
e054468f
AM
5044 h->needs_plt = 1;
5045 if (h->root.root.string[0] == '.'
5046 && h->root.root.string[1] != '\0')
ed7007c1
AM
5047 ppc_elf_hash_entry (h)->is_func = 1;
5048 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5049 plt_list = &h->plt.plist;
5050 }
5051 if (plt_list == NULL)
2d7ad24e 5052 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5053 rel->r_addend,
2d7ad24e 5054 NON_GOT | PLT_KEEP);
133a1f60 5055 if (!update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5056 return false;
5bd4f169
AM
5057 break;
5058
5059 /* The following relocations don't need to propagate the
5060 relocation if linking a shared object since they are
5061 section relative. */
5062 case R_PPC64_SECTOFF:
5063 case R_PPC64_SECTOFF_LO:
5064 case R_PPC64_SECTOFF_HI:
5065 case R_PPC64_SECTOFF_HA:
5066 case R_PPC64_SECTOFF_DS:
5067 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5068 case R_PPC64_DTPREL16:
5069 case R_PPC64_DTPREL16_LO:
5070 case R_PPC64_DTPREL16_HI:
5071 case R_PPC64_DTPREL16_HA:
5072 case R_PPC64_DTPREL16_DS:
5073 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5074 case R_PPC64_DTPREL16_HIGH:
5075 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5076 case R_PPC64_DTPREL16_HIGHER:
5077 case R_PPC64_DTPREL16_HIGHERA:
5078 case R_PPC64_DTPREL16_HIGHEST:
5079 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5080 break;
5081
ad8e1ba5 5082 /* Nor do these. */
25f23106
AM
5083 case R_PPC64_REL16:
5084 case R_PPC64_REL16_LO:
5085 case R_PPC64_REL16_HI:
5086 case R_PPC64_REL16_HA:
4a969973
AM
5087 case R_PPC64_REL16_HIGH:
5088 case R_PPC64_REL16_HIGHA:
5089 case R_PPC64_REL16_HIGHER:
5090 case R_PPC64_REL16_HIGHERA:
5091 case R_PPC64_REL16_HIGHEST:
5092 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
5093 case R_PPC64_REL16_HIGHER34:
5094 case R_PPC64_REL16_HIGHERA34:
5095 case R_PPC64_REL16_HIGHEST34:
5096 case R_PPC64_REL16_HIGHESTA34:
a680de9a 5097 case R_PPC64_REL16DX_HA:
25f23106
AM
5098 break;
5099
45965137
AM
5100 /* Not supported as a dynamic relocation. */
5101 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5102 if (bfd_link_pic (info))
45965137
AM
5103 {
5104 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5105 ppc_howto_init ();
695344c0 5106 /* xgettext:c-format */
174d0a74 5107 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5108 "in shared libraries and PIEs\n"),
45965137
AM
5109 abfd, sec, rel->r_offset,
5110 ppc64_elf_howto_table[r_type]->name);
5111 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5112 return false;
45965137
AM
5113 }
5114 break;
5115
ad8e1ba5 5116 case R_PPC64_TOC16:
33c0ec9d
AM
5117 case R_PPC64_TOC16_DS:
5118 htab->do_multi_toc = 1;
d77c8a4b 5119 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5120 /* Fall through. */
ad8e1ba5
AM
5121 case R_PPC64_TOC16_LO:
5122 case R_PPC64_TOC16_HI:
5123 case R_PPC64_TOC16_HA:
ad8e1ba5 5124 case R_PPC64_TOC16_LO_DS:
4c52953f 5125 sec->has_toc_reloc = 1;
ec73ddcd 5126 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
5127 {
5128 /* We may need a copy reloc. */
5129 h->non_got_ref = 1;
5130 /* Strongly prefer a copy reloc over a dynamic reloc.
5131 glibc ld.so as of 2019-08 will error out if one of
5132 these relocations is emitted. */
5133 h->needs_copy = 1;
5134 goto dodyn;
5135 }
ad8e1ba5
AM
5136 break;
5137
006589cf
AM
5138 /* Marker reloc. */
5139 case R_PPC64_ENTRY:
5140 break;
5141
5bd4f169
AM
5142 /* This relocation describes the C++ object vtable hierarchy.
5143 Reconstruct it for later use during GC. */
5144 case R_PPC64_GNU_VTINHERIT:
c152c796 5145 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
0a1b45a2 5146 return false;
5bd4f169
AM
5147 break;
5148
5149 /* This relocation describes which C++ vtable entries are actually
5150 used. Record for later use during GC. */
5151 case R_PPC64_GNU_VTENTRY:
a0ea3a14 5152 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
0a1b45a2 5153 return false;
5bd4f169
AM
5154 break;
5155
721956f4
AM
5156 case R_PPC64_REL14:
5157 case R_PPC64_REL14_BRTAKEN:
5158 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5159 {
5160 asection *dest = NULL;
5161
5162 /* Heuristic: If jumping outside our section, chances are
5163 we are going to need a stub. */
5164 if (h != NULL)
5165 {
5166 /* If the sym is weak it may be overridden later, so
5167 don't assume we know where a weak sym lives. */
5168 if (h->root.type == bfd_link_hash_defined)
5169 dest = h->root.u.def.section;
5170 }
5171 else
3a3a4c1f 5172 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
87d72d41 5173
220c76dd 5174 if (dest != sec)
7c8fe5c4 5175 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5176 }
3e04d765
AM
5177 goto rel24;
5178
5179 case R_PPC64_PLTCALL:
5663e321 5180 case R_PPC64_PLTCALL_NOTOC:
3e04d765 5181 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5182 /* Fall through. */
5183
5d1634d7 5184 case R_PPC64_REL24:
05d0e962 5185 case R_PPC64_REL24_NOTOC:
7aba54da 5186 case R_PPC64_REL24_P9NOTOC:
3e04d765 5187 rel24:
cbf95972
AM
5188 plt_list = ifunc;
5189 if (h != NULL)
5d1634d7 5190 {
e054468f
AM
5191 h->needs_plt = 1;
5192 if (h->root.root.string[0] == '.'
5193 && h->root.root.string[1] != '\0')
ed7007c1 5194 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 5195
3a71aa26 5196 if (h == tga || h == dottga)
cbf95972
AM
5197 {
5198 sec->has_tls_reloc = 1;
5199 if (rel != relocs
5200 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5201 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5202 /* We have a new-style __tls_get_addr call with
5203 a marker reloc. */
5204 ;
5205 else
5206 /* Mark this section as having an old-style call. */
9737e8af 5207 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5208 }
5209 plt_list = &h->plt.plist;
411e1bfb 5210 }
cbf95972
AM
5211
5212 /* We may need a .plt entry if the function this reloc
5213 refers to is in a shared lib. */
5214 if (plt_list
133a1f60 5215 && !update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5216 return false;
411e1bfb
AM
5217 break;
5218
cbf95972
AM
5219 case R_PPC64_ADDR14:
5220 case R_PPC64_ADDR14_BRNTAKEN:
5221 case R_PPC64_ADDR14_BRTAKEN:
5222 case R_PPC64_ADDR24:
5223 goto dodyn;
5224
411e1bfb
AM
5225 case R_PPC64_TPREL64:
5226 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5227 if (bfd_link_dll (info))
411e1bfb
AM
5228 info->flags |= DF_STATIC_TLS;
5229 goto dotlstoc;
5230
5231 case R_PPC64_DTPMOD64:
5232 if (rel + 1 < rel_end
5233 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5234 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5235 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5236 else
951fd09b 5237 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5238 goto dotlstoc;
5239
5240 case R_PPC64_DTPREL64:
5241 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5242 if (rel != relocs
5243 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5244 && rel[-1].r_offset == rel->r_offset - 8)
5245 /* This is the second reloc of a dtpmod, dtprel pair.
5246 Don't mark with TLS_DTPREL. */
5247 goto dodyn;
5248
5249 dotlstoc:
5250 sec->has_tls_reloc = 1;
5251 if (h != NULL)
ed7007c1 5252 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5253 else
5254 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5255 rel->r_addend, tls_type))
0a1b45a2 5256 return false;
411e1bfb 5257
7c8fe5c4 5258 ppc64_sec = ppc64_elf_section_data (sec);
6313825c 5259 if (ppc64_sec->sec_type == sec_normal)
411e1bfb 5260 {
3a71aa26
AM
5261 bfd_size_type amt;
5262
e7b938ca 5263 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5264 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5265 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5266 if (ppc64_sec->u.toc.symndx == NULL)
0a1b45a2 5267 return false;
3a71aa26
AM
5268 amt = sec->size * sizeof (bfd_vma) / 8;
5269 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5270 if (ppc64_sec->u.toc.add == NULL)
0a1b45a2 5271 return false;
7c8fe5c4 5272 ppc64_sec->sec_type = sec_toc;
411e1bfb 5273 }
6313825c
AM
5274 if (ppc64_sec->sec_type != sec_toc
5275 || rel->r_offset % 8 != 0)
5276 {
5277 info->callbacks->einfo (_("%H: %s reloc unsupported here\n"),
5278 abfd, sec, rel->r_offset,
5279 ppc64_elf_howto_table[r_type]->name);
5280 bfd_set_error (bfd_error_bad_value);
5281 return false;
5282 }
3a71aa26 5283 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5284 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5285
5286 /* Mark the second slot of a GD or LD entry.
5287 -1 to indicate GD and -2 to indicate LD. */
5288 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5289 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5290 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5291 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5292 goto dodyn;
5293
411e1bfb
AM
5294 case R_PPC64_TPREL16_HI:
5295 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5296 case R_PPC64_TPREL16_HIGH:
5297 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5298 case R_PPC64_TPREL16_HIGHER:
5299 case R_PPC64_TPREL16_HIGHERA:
5300 case R_PPC64_TPREL16_HIGHEST:
5301 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5302 sec->has_tls_reloc = 1;
5303 /* Fall through. */
c213164a 5304 case R_PPC64_TPREL34:
252dcdf4
AM
5305 case R_PPC64_TPREL16:
5306 case R_PPC64_TPREL16_DS:
5307 case R_PPC64_TPREL16_LO:
5308 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5309 if (bfd_link_dll (info))
5310 info->flags |= DF_STATIC_TLS;
5311 goto dodyn;
5d1634d7 5312
e86ce104 5313 case R_PPC64_ADDR64:
b9399fcf 5314 if (is_opd
1e2f5b6e 5315 && rel + 1 < rel_end
4ce794b7 5316 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5317 {
8387904d 5318 if (h != NULL)
ed7007c1 5319 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5320 }
e86ce104
AM
5321 /* Fall through. */
5322
65f38f15
AM
5323 case R_PPC64_ADDR16:
5324 case R_PPC64_ADDR16_DS:
5325 case R_PPC64_ADDR16_HA:
5326 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5327 case R_PPC64_ADDR16_HIGH:
5328 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5329 case R_PPC64_ADDR16_HIGHER:
5330 case R_PPC64_ADDR16_HIGHERA:
5331 case R_PPC64_ADDR16_HIGHEST:
5332 case R_PPC64_ADDR16_HIGHESTA:
5333 case R_PPC64_ADDR16_LO:
5334 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5335 case R_PPC64_D34:
5336 case R_PPC64_D34_LO:
5337 case R_PPC64_D34_HI30:
5338 case R_PPC64_D34_HA30:
5339 case R_PPC64_ADDR16_HIGHER34:
5340 case R_PPC64_ADDR16_HIGHERA34:
5341 case R_PPC64_ADDR16_HIGHEST34:
5342 case R_PPC64_ADDR16_HIGHESTA34:
5343 case R_PPC64_D28:
0e1862bb 5344 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5345 && rel->r_addend == 0)
5346 {
5347 /* We may need a .plt entry if this reloc refers to a
5348 function in a shared lib. */
5663e321 5349 if (!update_plt_info (abfd, &h->plt.plist, 0))
0a1b45a2 5350 return false;
a345bc8d
AM
5351 h->pointer_equality_needed = 1;
5352 }
5353 /* Fall through. */
5354
5355 case R_PPC64_REL30:
5356 case R_PPC64_REL32:
5357 case R_PPC64_REL64:
65f38f15 5358 case R_PPC64_ADDR32:
65f38f15
AM
5359 case R_PPC64_UADDR16:
5360 case R_PPC64_UADDR32:
5361 case R_PPC64_UADDR64:
5bd4f169 5362 case R_PPC64_TOC:
ec73ddcd 5363 if (h != NULL && bfd_link_executable (info))
81848ca0 5364 /* We may need a copy reloc. */
f5385ebf 5365 h->non_got_ref = 1;
81848ca0 5366
41bd81ab 5367 /* Don't propagate .opd relocs. */
b9399fcf 5368 if (NO_OPD_RELOCS && is_opd)
e86ce104 5369 break;
e86ce104 5370
c99d782d
AM
5371 /* Set up information for symbols that might need dynamic
5372 relocations. At this point in linking we have read all
5373 the input files and resolved most symbols, but have not
5374 yet decided whether symbols are dynamic or finalized
5375 symbol flags. In some cases we might be setting dynamic
5376 reloc info for symbols that do not end up needing such.
5377 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5378 work together with this code. */
411e1bfb 5379 dodyn:
ec73ddcd 5380 if ((h != NULL
c99d782d 5381 && !SYMBOL_REFERENCES_LOCAL (info, h))
ec73ddcd 5382 || (bfd_link_pic (info)
3a3a4c1f
AM
5383 && (h != NULL
5384 ? !bfd_is_abs_symbol (&h->root)
5385 : isym->st_shndx != SHN_ABS)
ec73ddcd 5386 && must_be_dyn_reloc (info, r_type))
0e1862bb 5387 || (!bfd_link_pic (info)
25f23106 5388 && ifunc != NULL))
5bd4f169 5389 {
65f38f15
AM
5390 /* We must copy these reloc types into the output file.
5391 Create a reloc section in dynobj and make room for
5392 this reloc. */
5bd4f169
AM
5393 if (sreloc == NULL)
5394 {
83bac4b0 5395 sreloc = _bfd_elf_make_dynamic_reloc_section
0a1b45a2 5396 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
65f38f15 5397
5bd4f169 5398 if (sreloc == NULL)
0a1b45a2 5399 return false;
5bd4f169
AM
5400 }
5401
65f38f15
AM
5402 /* If this is a global symbol, we count the number of
5403 relocations we need for this symbol. */
5404 if (h != NULL)
5405 {
1657026c
AM
5406 struct ppc_dyn_relocs *p;
5407 struct ppc_dyn_relocs **head;
19e08130 5408
1657026c 5409 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
5410 p = *head;
5411 if (p == NULL || p->sec != sec)
5412 {
5413 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5414 if (p == NULL)
0a1b45a2 5415 return false;
19e08130
AM
5416 p->next = *head;
5417 *head = p;
5418 p->sec = sec;
5419 p->count = 0;
5420 p->pc_count = 0;
1657026c 5421 p->rel_count = 0;
19e08130
AM
5422 }
5423 p->count += 1;
5424 if (!must_be_dyn_reloc (info, r_type))
5425 p->pc_count += 1;
f91074eb 5426 if (maybe_relr (r_type, rel, sec))
1657026c 5427 p->rel_count += 1;
65f38f15
AM
5428 }
5429 else
5430 {
3a3a4c1f 5431 /* Track dynamic relocs needed for local syms too. */
1657026c
AM
5432 struct ppc_local_dyn_relocs *p;
5433 struct ppc_local_dyn_relocs **head;
0a1b45a2 5434 bool is_ifunc;
ec338859 5435 asection *s;
6edfbbad 5436 void *vpp;
ec338859 5437
87d72d41
AM
5438 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5439 if (s == NULL)
5440 s = sec;
5441
6edfbbad 5442 vpp = &elf_section_data (s)->local_dynrel;
1657026c 5443 head = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
5444 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5445 p = *head;
5446 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5447 p = p->next;
5448 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5449 {
5450 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5451 if (p == NULL)
0a1b45a2 5452 return false;
19e08130
AM
5453 p->next = *head;
5454 *head = p;
5455 p->sec = sec;
19e08130 5456 p->count = 0;
1657026c
AM
5457 p->rel_count = 0;
5458 p->ifunc = is_ifunc;
19e08130
AM
5459 }
5460 p->count += 1;
f91074eb 5461 if (maybe_relr (r_type, rel, sec))
1657026c 5462 p->rel_count += 1;
ec338859 5463 }
65f38f15 5464 }
5bd4f169 5465 break;
65f38f15
AM
5466
5467 default:
96e0dda4 5468 break;
5bd4f169
AM
5469 }
5470 }
5471
0a1b45a2 5472 return true;
5bd4f169
AM
5473}
5474
ee67d69a
AM
5475/* Merge backend specific data from an object file to the output
5476 object file when linking. */
5477
0a1b45a2 5478static bool
50e03d47 5479ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5480{
50e03d47 5481 bfd *obfd = info->output_bfd;
ee67d69a
AM
5482 unsigned long iflags, oflags;
5483
5484 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
0a1b45a2 5485 return true;
ee67d69a
AM
5486
5487 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
0a1b45a2 5488 return true;
ee67d69a 5489
50e03d47 5490 if (!_bfd_generic_verify_endian_match (ibfd, info))
0a1b45a2 5491 return false;
ee67d69a
AM
5492
5493 iflags = elf_elfheader (ibfd)->e_flags;
5494 oflags = elf_elfheader (obfd)->e_flags;
5495
f6c7c3e8 5496 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5497 {
4eca0228 5498 _bfd_error_handler
695344c0 5499 /* xgettext:c-format */
871b3ab2 5500 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a 5501 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5502 return false;
ee67d69a 5503 }
f6c7c3e8 5504 else if (iflags != oflags && iflags != 0)
ee67d69a 5505 {
4eca0228 5506 _bfd_error_handler
695344c0 5507 /* xgettext:c-format */
871b3ab2 5508 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5509 ibfd, iflags, oflags);
5510 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5511 return false;
ee67d69a
AM
5512 }
5513
4a91d0ba 5514 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
0a1b45a2 5515 return false;
005d79fd 5516
ee67d69a 5517 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5518 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5519}
5520
0a1b45a2 5521static bool
ee67d69a
AM
5522ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5523{
5524 /* Print normal ELF private data. */
5525 _bfd_elf_print_private_bfd_data (abfd, ptr);
5526
5527 if (elf_elfheader (abfd)->e_flags != 0)
5528 {
5529 FILE *file = ptr;
5530
ee67d69a
AM
5531 fprintf (file, _("private flags = 0x%lx:"),
5532 elf_elfheader (abfd)->e_flags);
5533
5534 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5535 fprintf (file, _(" [abiv%ld]"),
5536 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5537 fputc ('\n', file);
5538 }
5539
0a1b45a2 5540 return true;
ee67d69a
AM
5541}
5542
8387904d 5543/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5544 of the code entry point, and its section, which must be in the same
5545 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5546
5547static bfd_vma
5548opd_entry_value (asection *opd_sec,
5549 bfd_vma offset,
5550 asection **code_sec,
aef36ac1 5551 bfd_vma *code_off,
0a1b45a2 5552 bool in_code_sec)
8387904d
AM
5553{
5554 bfd *opd_bfd = opd_sec->owner;
8860955f 5555 Elf_Internal_Rela *relocs;
8387904d 5556 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5557 bfd_vma val;
8387904d 5558
6313825c
AM
5559 if (!is_ppc64_elf (opd_bfd))
5560 return (bfd_vma) -1;
5561
5562 if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5563 ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5564 else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5565 return (bfd_vma) -1;
5566
9f296da3
AM
5567 /* No relocs implies we are linking a --just-symbols object, or looking
5568 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5569 if (opd_sec->reloc_count == 0)
5570 {
6313825c 5571 bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
3b36f7e6 5572
729eabd5
AM
5573 if (contents == NULL)
5574 {
81ff113f
AM
5575 if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5576 || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
729eabd5 5577 return (bfd_vma) -1;
6313825c 5578 ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
729eabd5 5579 }
ee1e4ede 5580
dbb3fbbb 5581 /* PR 17512: file: 64b9dfbb. */
451dfd38 5582 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5583 return (bfd_vma) -1;
5584
729eabd5 5585 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5586 if (code_sec != NULL)
5587 {
5588 asection *sec, *likely = NULL;
ee1e4ede 5589
aef36ac1 5590 if (in_code_sec)
4b85d634 5591 {
aef36ac1
AM
5592 sec = *code_sec;
5593 if (sec->vma <= val
5594 && val < sec->vma + sec->size)
5595 likely = sec;
5596 else
5597 val = -1;
5598 }
5599 else
5600 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5601 if (sec->vma <= val
5602 && (sec->flags & SEC_LOAD) != 0
5603 && (sec->flags & SEC_ALLOC) != 0)
5604 likely = sec;
5605 if (likely != NULL)
5606 {
5607 *code_sec = likely;
5608 if (code_off != NULL)
5609 *code_off = val - likely->vma;
4b85d634
AM
5610 }
5611 }
aef36ac1 5612 return val;
4b85d634
AM
5613 }
5614
6313825c 5615 relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
8860955f 5616 if (relocs == NULL)
0a1b45a2 5617 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
877a8638
NC
5618 /* PR 17512: file: df8e1fd6. */
5619 if (relocs == NULL)
5620 return (bfd_vma) -1;
645ea6a9 5621
8387904d 5622 /* Go find the opd reloc at the sym address. */
8860955f 5623 lo = relocs;
8387904d 5624 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5625 val = (bfd_vma) -1;
8387904d
AM
5626 while (lo < hi)
5627 {
5628 look = lo + (hi - lo) / 2;
5629 if (look->r_offset < offset)
5630 lo = look + 1;
5631 else if (look->r_offset > offset)
5632 hi = look;
5633 else
5634 {
0ffa91dd
NC
5635 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5636
8387904d
AM
5637 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5638 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5639 {
5640 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5641 asection *sec = NULL;
8387904d 5642
b53dfeb2
AM
5643 if (symndx >= symtab_hdr->sh_info
5644 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5645 {
5646 struct elf_link_hash_entry **sym_hashes;
5647 struct elf_link_hash_entry *rh;
5648
5649 sym_hashes = elf_sym_hashes (opd_bfd);
5650 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5651 if (rh != NULL)
5652 {
5653 rh = elf_follow_link (rh);
bb854a36
AM
5654 if (rh->root.type != bfd_link_hash_defined
5655 && rh->root.type != bfd_link_hash_defweak)
5656 break;
5657 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5658 {
bb854a36
AM
5659 val = rh->root.u.def.value;
5660 sec = rh->root.u.def.section;
b53dfeb2
AM
5661 }
5662 }
5663 }
5664
5665 if (sec == NULL)
5666 {
5667 Elf_Internal_Sym *sym;
5668
5669 if (symndx < symtab_hdr->sh_info)
5670 {
5671 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5672 if (sym == NULL)
5673 {
5674 size_t symcnt = symtab_hdr->sh_info;
5675 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5676 symcnt, 0,
5677 NULL, NULL, NULL);
5678 if (sym == NULL)
5679 break;
5680 symtab_hdr->contents = (bfd_byte *) sym;
5681 }
5682 sym += symndx;
128205bb
AM
5683 }
5684 else
5685 {
b53dfeb2
AM
5686 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5687 1, symndx,
5688 NULL, NULL, NULL);
128205bb
AM
5689 if (sym == NULL)
5690 break;
128205bb 5691 }
b53dfeb2 5692 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
aefcec16
AM
5693 if (sec != NULL)
5694 {
5695 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5696 val = sym->st_value;
5697 }
5698 if (symndx >= symtab_hdr->sh_info)
5699 free (sym);
b53dfeb2
AM
5700 if (sec == NULL)
5701 break;
8387904d 5702 }
b53dfeb2 5703
8387904d
AM
5704 val += look->r_addend;
5705 if (code_off != NULL)
5706 *code_off = val;
5707 if (code_sec != NULL)
aef36ac1
AM
5708 {
5709 if (in_code_sec && *code_sec != sec)
5710 return -1;
5711 else
5712 *code_sec = sec;
5713 }
b53dfeb2 5714 if (sec->output_section != NULL)
8387904d 5715 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5716 }
5717 break;
5718 }
5719 }
645ea6a9 5720
645ea6a9 5721 return val;
8387904d
AM
5722}
5723
aef36ac1
AM
5724/* If the ELF symbol SYM might be a function in SEC, return the
5725 function size and set *CODE_OFF to the function's entry point,
5726 otherwise return zero. */
9f296da3 5727
aef36ac1
AM
5728static bfd_size_type
5729ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5730 bfd_vma *code_off)
9f296da3 5731{
aef36ac1 5732 bfd_size_type size;
24aebc79 5733 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1
AM
5734
5735 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5736 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5737 return 0;
5738
24aebc79
NC
5739 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5740
5741 /* In theory we should check that the symbol's type satisfies
5742 _bfd_elf_is_function_type(), but there are some function-like
5743 symbols which would fail this test. (eg _start). Instead
5744 we check for hidden, local, notype symbols with zero size.
5745 This type of symbol is generated by the annobin plugin for gcc
5746 and clang, and should not be considered to be a function symbol. */
5747 if (size == 0
5748 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5749 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5750 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5751 return 0;
aef36ac1
AM
5752
5753 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5754 {
b07bca4e
AM
5755 struct _opd_sec_data *opd = get_opd_info (sym->section);
5756 bfd_vma symval = sym->value;
5757
5758 if (opd != NULL
5759 && opd->adjust != NULL
5760 && elf_section_data (sym->section)->relocs != NULL)
5761 {
5762 /* opd_entry_value will use cached relocs that have been
5763 adjusted, but with raw symbols. That means both local
5764 and global symbols need adjusting. */
5765 long adjust = opd->adjust[OPD_NDX (symval)];
5766 if (adjust == -1)
5767 return 0;
5768 symval += adjust;
5769 }
5770
5771 if (opd_entry_value (sym->section, symval,
0a1b45a2 5772 &sec, code_off, true) == (bfd_vma) -1)
aef36ac1
AM
5773 return 0;
5774 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5775 symbol. This size has nothing to do with the code size of the
5776 function, which is what we're supposed to return, but the
5777 code size isn't available without looking up the dot-sym.
5778 However, doing that would be a waste of time particularly
5779 since elf_find_function will look at the dot-sym anyway.
5780 Now, elf_find_function will keep the largest size of any
5781 function sym found at the code address of interest, so return
5782 1 here to avoid it incorrectly caching a larger function size
5783 for a small function. This does mean we return the wrong
5784 size for a new-ABI function of size 24, but all that does is
5785 disable caching for such functions. */
5786 if (size == 24)
5787 size = 1;
9f296da3 5788 }
aef36ac1
AM
5789 else
5790 {
5791 if (sym->section != sec)
5792 return 0;
5793 *code_off = sym->value;
5794 }
24aebc79
NC
5795
5796 /* Do not return 0 for the function's size. */
5797 return size ? size : 1;
9f296da3
AM
5798}
5799
f378ab09
AM
5800/* Return true if symbol is a strong function defined in an ELFv2
5801 object with st_other localentry bits of zero, ie. its local entry
5802 point coincides with its global entry point. */
5803
0a1b45a2 5804static bool
f378ab09
AM
5805is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5806{
5807 return (h != NULL
5808 && h->type == STT_FUNC
5809 && h->root.type == bfd_link_hash_defined
5810 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5811 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5812 && is_ppc64_elf (h->root.u.def.section->owner)
5813 && abiversion (h->root.u.def.section->owner) >= 2);
5814}
5815
854b41e7
AM
5816/* Return true if symbol is defined in a regular object file. */
5817
0a1b45a2 5818static bool
854b41e7
AM
5819is_static_defined (struct elf_link_hash_entry *h)
5820{
5821 return ((h->root.type == bfd_link_hash_defined
5822 || h->root.type == bfd_link_hash_defweak)
5823 && h->root.u.def.section != NULL
5824 && h->root.u.def.section->output_section != NULL);
5825}
5826
b31867b6
AM
5827/* If FDH is a function descriptor symbol, return the associated code
5828 entry symbol if it is defined. Return NULL otherwise. */
5829
5830static struct ppc_link_hash_entry *
5831defined_code_entry (struct ppc_link_hash_entry *fdh)
5832{
5833 if (fdh->is_func_descriptor)
5834 {
5835 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5836 if (fh->elf.root.type == bfd_link_hash_defined
5837 || fh->elf.root.type == bfd_link_hash_defweak)
5838 return fh;
5839 }
5840 return NULL;
5841}
5842
5843/* If FH is a function code entry symbol, return the associated
5844 function descriptor symbol if it is defined. Return NULL otherwise. */
5845
5846static struct ppc_link_hash_entry *
5847defined_func_desc (struct ppc_link_hash_entry *fh)
5848{
5849 if (fh->oh != NULL
5850 && fh->oh->is_func_descriptor)
5851 {
5852 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5853 if (fdh->elf.root.type == bfd_link_hash_defined
5854 || fdh->elf.root.type == bfd_link_hash_defweak)
5855 return fdh;
5856 }
5857 return NULL;
5858}
5859
ed7007c1
AM
5860/* Given H is a symbol that satisfies is_static_defined, return the
5861 value in the output file. */
5862
5863static bfd_vma
5864defined_sym_val (struct elf_link_hash_entry *h)
5865{
5866 return (h->root.u.def.section->output_section->vma
5867 + h->root.u.def.section->output_offset
5868 + h->root.u.def.value);
5869}
5870
5871/* Return true if H matches __tls_get_addr or one of its variants. */
5872
0a1b45a2 5873static bool
ed7007c1
AM
5874is_tls_get_addr (struct elf_link_hash_entry *h,
5875 struct ppc_link_hash_table *htab)
5876{
30845f11
AM
5877 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5878 || h == elf_hash_entry (htab->tga_desc_fd)
5879 || h == elf_hash_entry (htab->tls_get_addr)
5880 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5881}
5882
0a1b45a2 5883static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
8c5b4e52
AM
5884
5885/* Garbage collect sections, after first dealing with dot-symbols. */
5886
0a1b45a2 5887static bool
8c5b4e52
AM
5888ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5889{
5890 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5891
5892 if (htab != NULL && htab->need_func_desc_adj)
5893 {
5894 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5895 htab->need_func_desc_adj = 0;
5896 }
5897 return bfd_elf_gc_sections (abfd, info);
5898}
5899
74f0fb50
AM
5900/* Mark all our entry sym sections, both opd and code section. */
5901
5902static void
5903ppc64_elf_gc_keep (struct bfd_link_info *info)
5904{
5905 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5906 struct bfd_sym_chain *sym;
5907
4dfe6ac6
NC
5908 if (htab == NULL)
5909 return;
5910
74f0fb50
AM
5911 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5912 {
b31867b6 5913 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5914 asection *sec;
5915
ed7007c1 5916 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
0a1b45a2 5917 false, false, true));
74f0fb50
AM
5918 if (eh == NULL)
5919 continue;
5920 if (eh->elf.root.type != bfd_link_hash_defined
5921 && eh->elf.root.type != bfd_link_hash_defweak)
5922 continue;
5923
b31867b6
AM
5924 fh = defined_code_entry (eh);
5925 if (fh != NULL)
74f0fb50 5926 {
b31867b6 5927 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5928 sec->flags |= SEC_KEEP;
5929 }
5930 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5931 && opd_entry_value (eh->elf.root.u.def.section,
5932 eh->elf.root.u.def.value,
0a1b45a2 5933 &sec, NULL, false) != (bfd_vma) -1)
74f0fb50
AM
5934 sec->flags |= SEC_KEEP;
5935
5936 sec = eh->elf.root.u.def.section;
5937 sec->flags |= SEC_KEEP;
5938 }
5939}
5940
64d03ab5
AM
5941/* Mark sections containing dynamically referenced symbols. When
5942 building shared libraries, we must assume that any visible symbol is
5943 referenced. */
5944
0a1b45a2 5945static bool
64d03ab5
AM
5946ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5947{
5948 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5949 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5950 struct ppc_link_hash_entry *fdh;
b407645f 5951 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5952
64d03ab5 5953 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5954 fdh = defined_func_desc (eh);
5955 if (fdh != NULL)
5956 eh = fdh;
64d03ab5
AM
5957
5958 if ((eh->elf.root.type == bfd_link_hash_defined
5959 || eh->elf.root.type == bfd_link_hash_defweak)
b80e421f
AM
5960 && (!eh->elf.start_stop
5961 || eh->elf.root.ldscript_def
5962 || !info->start_stop_gc)
87e79a65 5963 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5964 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5965 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5966 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5967 && (!bfd_link_executable (info)
e278ae05 5968 || info->gc_keep_exported
b407645f
AM
5969 || info->export_dynamic
5970 || (eh->elf.dynamic
5971 && d != NULL
2cdcc330
AM
5972 && (*d->match) (&d->head, NULL,
5973 eh->elf.root.root.string)))
e278ae05 5974 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5975 || !bfd_hide_sym_by_version (info->version_info,
5976 eh->elf.root.root.string)))))
64d03ab5
AM
5977 {
5978 asection *code_sec;
b31867b6 5979 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5980
5981 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5982
5983 /* Function descriptor syms cause the associated
5984 function code sym section to be marked. */
b31867b6
AM
5985 fh = defined_code_entry (eh);
5986 if (fh != NULL)
5987 {
5988 code_sec = fh->elf.root.u.def.section;
5989 code_sec->flags |= SEC_KEEP;
5990 }
64d03ab5
AM
5991 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5992 && opd_entry_value (eh->elf.root.u.def.section,
5993 eh->elf.root.u.def.value,
0a1b45a2 5994 &code_sec, NULL, false) != (bfd_vma) -1)
64d03ab5
AM
5995 code_sec->flags |= SEC_KEEP;
5996 }
5997
0a1b45a2 5998 return true;
64d03ab5
AM
5999}
6000
5bd4f169
AM
6001/* Return the section that should be marked against GC for a given
6002 relocation. */
6003
6004static asection *
4ce794b7 6005ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6006 struct bfd_link_info *info,
4ce794b7
AM
6007 Elf_Internal_Rela *rel,
6008 struct elf_link_hash_entry *h,
6009 Elf_Internal_Sym *sym)
5bd4f169 6010{
ccfa59ea
AM
6011 asection *rsec;
6012
ccfa59ea
AM
6013 /* Syms return NULL if we're marking .opd, so we avoid marking all
6014 function sections, as all functions are referenced in .opd. */
6015 rsec = NULL;
6016 if (get_opd_info (sec) != NULL)
6017 return rsec;
1e2f5b6e 6018
5bd4f169
AM
6019 if (h != NULL)
6020 {
04c9666a 6021 enum elf_ppc64_reloc_type r_type;
b31867b6 6022 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6023
4ce794b7 6024 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6025 switch (r_type)
5bd4f169
AM
6026 {
6027 case R_PPC64_GNU_VTINHERIT:
6028 case R_PPC64_GNU_VTENTRY:
6029 break;
6030
6031 default:
6032 switch (h->root.type)
6033 {
6034 case bfd_link_hash_defined:
6035 case bfd_link_hash_defweak:
ed7007c1 6036 eh = ppc_elf_hash_entry (h);
b31867b6
AM
6037 fdh = defined_func_desc (eh);
6038 if (fdh != NULL)
8c5b4e52
AM
6039 {
6040 /* -mcall-aixdesc code references the dot-symbol on
6041 a call reloc. Mark the function descriptor too
6042 against garbage collection. */
6043 fdh->elf.mark = 1;
60d67dc8
AM
6044 if (fdh->elf.is_weakalias)
6045 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6046 eh = fdh;
6047 }
1e2f5b6e
AM
6048
6049 /* Function descriptor syms cause the associated
6050 function code sym section to be marked. */
b31867b6
AM
6051 fh = defined_code_entry (eh);
6052 if (fh != NULL)
ccfa59ea
AM
6053 {
6054 /* They also mark their opd section. */
74f0fb50 6055 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6056
b31867b6 6057 rsec = fh->elf.root.u.def.section;
ccfa59ea 6058 }
8387904d
AM
6059 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6060 && opd_entry_value (eh->elf.root.u.def.section,
6061 eh->elf.root.u.def.value,
0a1b45a2 6062 &rsec, NULL, false) != (bfd_vma) -1)
74f0fb50 6063 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6064 else
1e2f5b6e
AM
6065 rsec = h->root.u.def.section;
6066 break;
5bd4f169
AM
6067
6068 case bfd_link_hash_common:
1e2f5b6e
AM
6069 rsec = h->root.u.c.p->section;
6070 break;
5bd4f169
AM
6071
6072 default:
fb34365b 6073 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6074 }
6075 }
6076 }
6077 else
6078 {
74f0fb50 6079 struct _opd_sec_data *opd;
1e2f5b6e
AM
6080
6081 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6082 opd = get_opd_info (rsec);
6083 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6084 {
74f0fb50 6085 rsec->gc_mark = 1;
ccfa59ea 6086
51aecdc5 6087 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6088 }
5bd4f169
AM
6089 }
6090
1e2f5b6e 6091 return rsec;
5bd4f169
AM
6092}
6093
deb0e272
AM
6094/* The maximum size of .sfpr. */
6095#define SFPR_MAX (218*4)
6096
6097struct sfpr_def_parms
6098{
699733f6
AM
6099 const char name[12];
6100 unsigned char lo, hi;
2cdcc330
AM
6101 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6102 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
6103};
6104
a4b6fadd
AM
6105/* Auto-generate _save*, _rest* functions in .sfpr.
6106 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6107 instead. */
deb0e272 6108
0a1b45a2 6109static bool
a4b6fadd
AM
6110sfpr_define (struct bfd_link_info *info,
6111 const struct sfpr_def_parms *parm,
6112 asection *stub_sec)
deb0e272
AM
6113{
6114 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6115 unsigned int i;
6116 size_t len = strlen (parm->name);
0a1b45a2 6117 bool writing = false;
699733f6 6118 char sym[16];
deb0e272 6119
4dfe6ac6 6120 if (htab == NULL)
0a1b45a2 6121 return false;
4dfe6ac6 6122
deb0e272
AM
6123 memcpy (sym, parm->name, len);
6124 sym[len + 2] = 0;
6125
6126 for (i = parm->lo; i <= parm->hi; i++)
6127 {
a4b6fadd 6128 struct ppc_link_hash_entry *h;
deb0e272
AM
6129
6130 sym[len + 0] = i / 10 + '0';
6131 sym[len + 1] = i % 10 + '0';
ed7007c1 6132 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
0a1b45a2 6133 writing, true, true));
a4b6fadd 6134 if (stub_sec != NULL)
deb0e272 6135 {
a4b6fadd
AM
6136 if (h != NULL
6137 && h->elf.root.type == bfd_link_hash_defined
6138 && h->elf.root.u.def.section == htab->sfpr)
6139 {
6140 struct elf_link_hash_entry *s;
6141 char buf[32];
6142 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
0a1b45a2 6143 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
a4b6fadd 6144 if (s == NULL)
0a1b45a2 6145 return false;
779f2ae7 6146 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
6147 {
6148 s->root.type = bfd_link_hash_defined;
6149 s->root.u.def.section = stub_sec;
7dda8d3c 6150 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6151 + h->elf.root.u.def.value);
6152 s->ref_regular = 1;
6153 s->def_regular = 1;
6154 s->ref_regular_nonweak = 1;
6155 s->forced_local = 1;
6156 s->non_elf = 0;
6157 s->root.linker_def = 1;
6158 }
6159 }
6160 continue;
6161 }
6162 if (h != NULL)
6163 {
6164 h->save_res = 1;
6165 if (!h->elf.def_regular)
deb0e272 6166 {
a4b6fadd
AM
6167 h->elf.root.type = bfd_link_hash_defined;
6168 h->elf.root.u.def.section = htab->sfpr;
6169 h->elf.root.u.def.value = htab->sfpr->size;
6170 h->elf.type = STT_FUNC;
6171 h->elf.def_regular = 1;
b32547cd 6172 h->elf.non_elf = 0;
0a1b45a2
AM
6173 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6174 writing = true;
deb0e272 6175 if (htab->sfpr->contents == NULL)
a4b6fadd 6176 {
2cdcc330
AM
6177 htab->sfpr->contents
6178 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd 6179 if (htab->sfpr->contents == NULL)
0a1b45a2 6180 return false;
cb6326b5 6181 htab->sfpr->alloced = 1;
a4b6fadd 6182 }
deb0e272
AM
6183 }
6184 }
6185 if (writing)
6186 {
6187 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6188 if (i != parm->hi)
6189 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6190 else
6191 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6192 htab->sfpr->size = p - htab->sfpr->contents;
6193 }
6194 }
6195
0a1b45a2 6196 return true;
deb0e272
AM
6197}
6198
6199static bfd_byte *
6200savegpr0 (bfd *abfd, bfd_byte *p, int r)
6201{
6202 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6203 return p + 4;
6204}
6205
6206static bfd_byte *
6207savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6208{
6209 p = savegpr0 (abfd, p, r);
a078d95a 6210 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6211 p = p + 4;
6212 bfd_put_32 (abfd, BLR, p);
6213 return p + 4;
6214}
6215
6216static bfd_byte *
6217restgpr0 (bfd *abfd, bfd_byte *p, int r)
6218{
6219 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6220 return p + 4;
6221}
6222
6223static bfd_byte *
6224restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6225{
a078d95a 6226 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6227 p = p + 4;
6228 p = restgpr0 (abfd, p, r);
6229 bfd_put_32 (abfd, MTLR_R0, p);
6230 p = p + 4;
6231 if (r == 29)
6232 {
6233 p = restgpr0 (abfd, p, 30);
6234 p = restgpr0 (abfd, p, 31);
6235 }
6236 bfd_put_32 (abfd, BLR, p);
6237 return p + 4;
6238}
6239
6240static bfd_byte *
6241savegpr1 (bfd *abfd, bfd_byte *p, int r)
6242{
6243 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6244 return p + 4;
6245}
6246
6247static bfd_byte *
6248savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6249{
6250 p = savegpr1 (abfd, p, r);
6251 bfd_put_32 (abfd, BLR, p);
6252 return p + 4;
6253}
6254
6255static bfd_byte *
6256restgpr1 (bfd *abfd, bfd_byte *p, int r)
6257{
6258 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6259 return p + 4;
6260}
6261
6262static bfd_byte *
6263restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6264{
6265 p = restgpr1 (abfd, p, r);
6266 bfd_put_32 (abfd, BLR, p);
6267 return p + 4;
6268}
6269
6270static bfd_byte *
6271savefpr (bfd *abfd, bfd_byte *p, int r)
6272{
6273 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6274 return p + 4;
6275}
6276
6277static bfd_byte *
6278savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6279{
6280 p = savefpr (abfd, p, r);
a078d95a 6281 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6282 p = p + 4;
6283 bfd_put_32 (abfd, BLR, p);
6284 return p + 4;
6285}
6286
6287static bfd_byte *
6288restfpr (bfd *abfd, bfd_byte *p, int r)
6289{
6290 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6291 return p + 4;
6292}
6293
6294static bfd_byte *
6295restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6296{
a078d95a 6297 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6298 p = p + 4;
6299 p = restfpr (abfd, p, r);
6300 bfd_put_32 (abfd, MTLR_R0, p);
6301 p = p + 4;
6302 if (r == 29)
6303 {
6304 p = restfpr (abfd, p, 30);
6305 p = restfpr (abfd, p, 31);
6306 }
6307 bfd_put_32 (abfd, BLR, p);
6308 return p + 4;
6309}
6310
6311static bfd_byte *
6312savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6313{
6314 p = savefpr (abfd, p, r);
6315 bfd_put_32 (abfd, BLR, p);
6316 return p + 4;
6317}
6318
6319static bfd_byte *
6320restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6321{
6322 p = restfpr (abfd, p, r);
6323 bfd_put_32 (abfd, BLR, p);
6324 return p + 4;
6325}
6326
6327static bfd_byte *
6328savevr (bfd *abfd, bfd_byte *p, int r)
6329{
6330 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6331 p = p + 4;
6332 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6333 return p + 4;
6334}
6335
6336static bfd_byte *
6337savevr_tail (bfd *abfd, bfd_byte *p, int r)
6338{
6339 p = savevr (abfd, p, r);
6340 bfd_put_32 (abfd, BLR, p);
6341 return p + 4;
6342}
6343
6344static bfd_byte *
6345restvr (bfd *abfd, bfd_byte *p, int r)
6346{
6347 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6348 p = p + 4;
6349 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6350 return p + 4;
6351}
6352
6353static bfd_byte *
6354restvr_tail (bfd *abfd, bfd_byte *p, int r)
6355{
6356 p = restvr (abfd, p, r);
6357 bfd_put_32 (abfd, BLR, p);
6358 return p + 4;
6359}
6360
9e7028aa
AM
6361#define STDU_R1_0R1 0xf8210001
6362#define ADDI_R1_R1 0x38210000
6363
6364/* Emit prologue of wrapper preserving regs around a call to
6365 __tls_get_addr_opt. */
6366
6367static bfd_byte *
6368tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6369{
6370 unsigned int i;
6371
6372 bfd_put_32 (obfd, MFLR_R0, p);
6373 p += 4;
6374 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6375 p += 4;
6376
6377 if (htab->opd_abi)
6378 {
6379 for (i = 4; i < 12; i++)
6380 {
6381 bfd_put_32 (obfd,
6382 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6383 p += 4;
6384 }
6385 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6386 p += 4;
6387 }
6388 else
6389 {
6390 for (i = 4; i < 12; i++)
6391 {
6392 bfd_put_32 (obfd,
6393 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6394 p += 4;
6395 }
6396 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6397 p += 4;
6398 }
6399 return p;
6400}
6401
6402/* Emit epilogue of wrapper preserving regs around a call to
6403 __tls_get_addr_opt. */
6404
6405static bfd_byte *
6406tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6407{
6408 unsigned int i;
6409
6410 if (htab->opd_abi)
6411 {
6412 for (i = 4; i < 12; i++)
6413 {
6414 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6415 p += 4;
6416 }
6417 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6418 p += 4;
6419 }
6420 else
6421 {
6422 for (i = 4; i < 12; i++)
6423 {
6424 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6425 p += 4;
6426 }
6427 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6428 p += 4;
6429 }
6430 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6431 p += 4;
6432 bfd_put_32 (obfd, MTLR_R0, p);
6433 p += 4;
6434 bfd_put_32 (obfd, BLR, p);
6435 p += 4;
6436 return p;
6437}
6438
e86ce104
AM
6439/* Called via elf_link_hash_traverse to transfer dynamic linking
6440 information on function code symbol entries to their corresponding
a442059f
AM
6441 function descriptor symbol entries. Must not be called twice for
6442 any given code symbol. */
deb0e272 6443
0a1b45a2 6444static bool
4ce794b7 6445func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6446{
e86ce104 6447 struct bfd_link_info *info;
65f38f15 6448 struct ppc_link_hash_table *htab;
50bc7936
AM
6449 struct ppc_link_hash_entry *fh;
6450 struct ppc_link_hash_entry *fdh;
0a1b45a2 6451 bool force_local;
5bd4f169 6452
ed7007c1 6453 fh = ppc_elf_hash_entry (h);
50bc7936 6454 if (fh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 6455 return true;
e86ce104 6456
8c5b4e52 6457 if (!fh->is_func)
0a1b45a2 6458 return true;
8c5b4e52
AM
6459
6460 if (fh->elf.root.root.string[0] != '.'
6461 || fh->elf.root.root.string[1] == '\0')
0a1b45a2 6462 return true;
8c5b4e52 6463
4ce794b7 6464 info = inf;
65f38f15 6465 htab = ppc_hash_table (info);
4dfe6ac6 6466 if (htab == NULL)
0a1b45a2 6467 return false;
5bd4f169 6468
8c5b4e52
AM
6469 /* Find the corresponding function descriptor symbol. */
6470 fdh = lookup_fdh (fh, htab);
6471
c09bdfe5
AM
6472 /* Resolve undefined references to dot-symbols as the value
6473 in the function descriptor, if we have one in a regular object.
6474 This is to satisfy cases like ".quad .foo". Calls to functions
6475 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6476 if ((fh->elf.root.type == bfd_link_hash_undefined
6477 || fh->elf.root.type == bfd_link_hash_undefweak)
6478 && (fdh->elf.root.type == bfd_link_hash_defined
6479 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6480 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6481 && opd_entry_value (fdh->elf.root.u.def.section,
6482 fdh->elf.root.u.def.value,
c09bdfe5 6483 &fh->elf.root.u.def.section,
0a1b45a2 6484 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
c09bdfe5 6485 {
b31867b6 6486 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6487 fh->elf.forced_local = 1;
b31867b6
AM
6488 fh->elf.def_regular = fdh->elf.def_regular;
6489 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6490 }
6491
8c5b4e52
AM
6492 if (!fh->elf.dynamic)
6493 {
6494 struct plt_entry *ent;
5bd4f169 6495
8c5b4e52
AM
6496 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6497 if (ent->plt.refcount > 0)
6498 break;
6499 if (ent == NULL)
a442059f
AM
6500 {
6501 if (fdh != NULL && fdh->fake)
6502 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6503 return true;
6504 }
8c5b4e52 6505 }
5bd4f169 6506
8c5b4e52 6507 /* Create a descriptor as undefined if necessary. */
50bc7936 6508 if (fdh == NULL
0e1862bb 6509 && !bfd_link_executable (info)
50bc7936
AM
6510 && (fh->elf.root.type == bfd_link_hash_undefined
6511 || fh->elf.root.type == bfd_link_hash_undefweak))
6512 {
908b32fc 6513 fdh = make_fdh (info, fh);
bb700d78 6514 if (fdh == NULL)
0a1b45a2 6515 return false;
50bc7936 6516 }
648cca2c 6517
8c5b4e52 6518 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6519 if (fdh != NULL
6520 && fdh->fake
8c5b4e52
AM
6521 && (fh->elf.root.type == bfd_link_hash_defined
6522 || fh->elf.root.type == bfd_link_hash_defweak))
0a1b45a2 6523 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
908b32fc 6524
8c5b4e52
AM
6525 /* Transfer dynamic linking information to the function descriptor. */
6526 if (fdh != NULL)
6527 {
f5385ebf
AM
6528 fdh->elf.ref_regular |= fh->elf.ref_regular;
6529 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6530 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6531 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6532 fdh->elf.dynamic |= fh->elf.dynamic;
6533 fdh->elf.needs_plt |= (fh->elf.needs_plt
6534 || fh->elf.type == STT_FUNC
6535 || fh->elf.type == STT_GNU_IFUNC);
6536 move_plt_plist (fh, fdh);
6537
6538 if (!fdh->elf.forced_local
6539 && fh->elf.dynindx != -1)
6540 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 6541 return false;
e86ce104
AM
6542 }
6543
50bc7936
AM
6544 /* Now that the info is on the function descriptor, clear the
6545 function code sym info. Any function code syms for which we
6546 don't have a definition in a regular file, we force local.
6547 This prevents a shared library from exporting syms that have
6548 been imported from another library. Function code syms that
6549 are really in the library we must leave global to prevent the
6550 linker dragging in a definition from a static library. */
93f3fa99
AM
6551 force_local = (!fh->elf.def_regular
6552 || fdh == NULL
6553 || !fdh->elf.def_regular
6554 || fdh->elf.forced_local);
50bc7936
AM
6555 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6556
0a1b45a2 6557 return true;
e86ce104 6558}
40b8271b 6559
a4b6fadd
AM
6560static const struct sfpr_def_parms save_res_funcs[] =
6561 {
6562 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6563 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6564 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6565 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6566 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6567 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6568 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6569 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6570 { "._savef", 14, 31, savefpr, savefpr1_tail },
6571 { "._restf", 14, 31, restfpr, restfpr1_tail },
6572 { "_savevr_", 20, 31, savevr, savevr_tail },
6573 { "_restvr_", 20, 31, restvr, restvr_tail }
6574 };
6575
e86ce104 6576/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6577 this hook to a) run the edit functions in this file, b) provide
6578 some gcc support functions, and c) transfer dynamic linking
6579 information gathered so far on function code symbol entries, to
6580 their corresponding function descriptor symbol entries. */
deb0e272 6581
0a1b45a2 6582static bool
c9405344 6583ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6584{
6585 struct ppc_link_hash_table *htab;
6586
6587 htab = ppc_hash_table (info);
4dfe6ac6 6588 if (htab == NULL)
0a1b45a2 6589 return false;
4dfe6ac6 6590
c9405344
AM
6591 /* Call back into the linker, which then runs the edit functions. */
6592 htab->params->edit ();
6593
b32547cd
AM
6594 /* Provide any missing _save* and _rest* functions. */
6595 if (htab->sfpr != NULL)
6596 {
6597 unsigned int i;
6598
6599 htab->sfpr->size = 0;
6600 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6601 if (!sfpr_define (info, &save_res_funcs[i], NULL))
0a1b45a2 6602 return false;
b32547cd
AM
6603 if (htab->sfpr->size == 0)
6604 htab->sfpr->flags |= SEC_EXCLUDE;
6605 }
6606
6607 if (bfd_link_relocatable (info))
0a1b45a2 6608 return true;
b32547cd
AM
6609
6610 if (htab->elf.hgot != NULL)
dba6fa9b 6611 {
0a1b45a2 6612 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
dba6fa9b
AM
6613 /* Make .TOC. defined so as to prevent it being made dynamic.
6614 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6615 if (!htab->elf.hgot->def_regular
6616 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6617 {
6618 htab->elf.hgot->root.type = bfd_link_hash_defined;
6619 htab->elf.hgot->root.u.def.value = 0;
6620 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6621 htab->elf.hgot->def_regular = 1;
6622 htab->elf.hgot->root.linker_def = 1;
6623 }
dba6fa9b 6624 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6625 htab->elf.hgot->other
6626 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6627 }
c66bb0ee 6628
0a1b45a2 6629 return true;
e86ce104
AM
6630}
6631
d311bc8b 6632/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6633 aliases, that apply to read-only sections. Cannot be used after
6634 size_dynamic_sections. */
d311bc8b 6635
0a1b45a2 6636static bool
d311bc8b
AM
6637alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6638{
ed7007c1 6639 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6640 do
6641 {
5dbc8b37 6642 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
0a1b45a2 6643 return true;
ed7007c1 6644 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6645 }
6646 while (eh != NULL && &eh->elf != h);
d311bc8b 6647
0a1b45a2 6648 return false;
d311bc8b 6649}
8a2058b5 6650
8a9e8e72
AM
6651/* Return whether EH has pc-relative dynamic relocs. */
6652
0a1b45a2 6653static bool
8a9e8e72
AM
6654pc_dynrelocs (struct ppc_link_hash_entry *eh)
6655{
1657026c 6656 struct ppc_dyn_relocs *p;
8a9e8e72 6657
1657026c 6658 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72 6659 if (p->pc_count != 0)
0a1b45a2
AM
6660 return true;
6661 return false;
8a9e8e72
AM
6662}
6663
8a2058b5
AM
6664/* Return true if a global entry stub will be created for H. Valid
6665 for ELFv2 before plt entries have been allocated. */
6666
0a1b45a2 6667static bool
8a2058b5
AM
6668global_entry_stub (struct elf_link_hash_entry *h)
6669{
6670 struct plt_entry *pent;
6671
6672 if (!h->pointer_equality_needed
6673 || h->def_regular)
0a1b45a2 6674 return false;
8a2058b5
AM
6675
6676 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6677 if (pent->plt.refcount > 0
6678 && pent->addend == 0)
0a1b45a2 6679 return true;
8a2058b5 6680
0a1b45a2 6681 return false;
8a2058b5
AM
6682}
6683
e86ce104
AM
6684/* Adjust a symbol defined by a dynamic object and referenced by a
6685 regular object. The current definition is in some section of the
6686 dynamic object, but we're not including those sections. We have to
6687 change the definition to something the rest of the link can
6688 understand. */
6689
0a1b45a2 6690static bool
4ce794b7
AM
6691ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6692 struct elf_link_hash_entry *h)
e86ce104
AM
6693{
6694 struct ppc_link_hash_table *htab;
5474d94f 6695 asection *s, *srel;
e86ce104
AM
6696
6697 htab = ppc_hash_table (info);
4dfe6ac6 6698 if (htab == NULL)
0a1b45a2 6699 return false;
e86ce104
AM
6700
6701 /* Deal with function syms. */
6702 if (h->type == STT_FUNC
e054468f 6703 || h->type == STT_GNU_IFUNC
f5385ebf 6704 || h->needs_plt)
e86ce104 6705 {
0a1b45a2
AM
6706 bool local = (ppc_elf_hash_entry (h)->save_res
6707 || SYMBOL_CALLS_LOCAL (info, h)
6708 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
529fe20e
AM
6709 /* Discard dyn_relocs when non-pic if we've decided that a
6710 function symbol is local and not an ifunc. We keep dynamic
6711 relocs for ifuncs when local rather than always emitting a
6712 plt call stub for them and defining the symbol on the call
6713 stub. We can't do that for ELFv1 anyway (a function symbol
6714 is defined on a descriptor, not code) and it can be faster at
6715 run-time due to not needing to bounce through a stub. The
6716 dyn_relocs for ifuncs will be applied even in a static
6717 executable. */
6718 if (!bfd_link_pic (info)
6719 && h->type != STT_GNU_IFUNC
6720 && local)
190eb1dd 6721 h->dyn_relocs = NULL;
529fe20e 6722
e86ce104
AM
6723 /* Clear procedure linkage table information for any symbol that
6724 won't need a .plt entry. */
411e1bfb
AM
6725 struct plt_entry *ent;
6726 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6727 if (ent->plt.refcount > 0)
6728 break;
8387904d 6729 if (ent == NULL
2d7ad24e
AM
6730 || (h->type != STT_GNU_IFUNC
6731 && local
3e04d765 6732 && (htab->can_convert_all_inline_plt
ed7007c1 6733 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6734 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6735 {
411e1bfb 6736 h->plt.plist = NULL;
f5385ebf 6737 h->needs_plt = 0;
d1eca1e4 6738 h->pointer_equality_needed = 0;
40b8271b 6739 }
8a2058b5 6740 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6741 {
d1eca1e4
AM
6742 /* Taking a function's address in a read/write section
6743 doesn't require us to define the function symbol in the
6744 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6745 be used instead. The reason we prefer a few more dynamic
6746 relocs is that calling via a global entry stub costs a
6747 few more instructions, and pointer_equality_needed causes
6748 extra work in ld.so when resolving these symbols. */
529fe20e 6749 if (global_entry_stub (h))
d1eca1e4 6750 {
5dbc8b37 6751 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6752 {
6753 h->pointer_equality_needed = 0;
04383fd1
AM
6754 /* If we haven't seen a branch reloc and the symbol
6755 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6756 if (!h->needs_plt)
6757 h->plt.plist = NULL;
6758 }
6759 else if (!bfd_link_pic (info))
6760 /* We are going to be defining the function symbol on the
6761 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6762 h->dyn_relocs = NULL;
d1eca1e4
AM
6763 }
6764
3988aed5 6765 /* ELFv2 function symbols can't have copy relocs. */
0a1b45a2 6766 return true;
3988aed5
AM
6767 }
6768 else if (!h->needs_plt
5dbc8b37 6769 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6770 {
04383fd1
AM
6771 /* If we haven't seen a branch reloc and the symbol isn't an
6772 ifunc then we don't need a plt entry. */
3988aed5
AM
6773 h->plt.plist = NULL;
6774 h->pointer_equality_needed = 0;
0a1b45a2 6775 return true;
a345bc8d 6776 }
5bd4f169 6777 }
bbd7ec4a 6778 else
411e1bfb 6779 h->plt.plist = NULL;
5bd4f169
AM
6780
6781 /* If this is a weak symbol, and there is a real definition, the
6782 processor independent code will have arranged for us to see the
6783 real definition first, and we can just use the same value. */
60d67dc8 6784 if (h->is_weakalias)
5bd4f169 6785 {
60d67dc8
AM
6786 struct elf_link_hash_entry *def = weakdef (h);
6787 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6788 h->root.u.def.section = def->root.u.def.section;
6789 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6790 if (def->root.u.def.section == htab->elf.sdynbss
6791 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6792 h->dyn_relocs = NULL;
0a1b45a2 6793 return true;
5bd4f169
AM
6794 }
6795
5bd4f169
AM
6796 /* If we are creating a shared library, we must presume that the
6797 only references to the symbol are via the global offset table.
6798 For such cases we need not do anything here; the relocations will
6799 be handled correctly by relocate_section. */
ec73ddcd 6800 if (!bfd_link_executable (info))
0a1b45a2 6801 return true;
5bd4f169 6802
65f38f15
AM
6803 /* If there are no references to this symbol that do not use the
6804 GOT, we don't need to generate a copy reloc. */
f5385ebf 6805 if (!h->non_got_ref)
0a1b45a2 6806 return true;
65f38f15 6807
b186458a 6808 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6809 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6810
d93d1c80
AM
6811 /* If -z nocopyreloc was given, don't generate them either. */
6812 || info->nocopyreloc
a127494f 6813
dce2246a 6814 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6815 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6816 || (ELIMINATE_COPY_RELOCS
6817 && !h->needs_copy
6818 && !alias_readonly_dynrelocs (h))
65f38f15 6819
d93d1c80
AM
6820 /* Protected variables do not work with .dynbss. The copy in
6821 .dynbss won't be used by the shared library with the protected
6822 definition for the variable. Text relocations are preferable
6823 to an incorrect program. */
6824 || h->protected_def)
0a1b45a2 6825 return true;
a127494f 6826
e1c6cf61
AM
6827 if (h->type == STT_FUNC
6828 || h->type == STT_GNU_IFUNC)
97b639ba 6829 {
e1c6cf61
AM
6830 /* .dynbss copies of function symbols only work if we have
6831 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6832 use dot-symbols and set the function symbol size to the text
6833 size of the function rather than the size of the descriptor.
6834 That's wrong for copying a descriptor. */
ed7007c1 6835 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61 6836 || !(h->size == 24 || h->size == 16))
0a1b45a2 6837 return true;
e1c6cf61
AM
6838
6839 /* We should never get here, but unfortunately there are old
6840 versions of gcc (circa gcc-3.2) that improperly for the
6841 ELFv1 ABI put initialized function pointers, vtable refs and
6842 suchlike in read-only sections. Allow them to proceed, but
6843 warn that this might break at runtime. */
25f53a85 6844 info->callbacks->einfo
c1c8c1ef 6845 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6846 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6847 h->root.root.string);
6848 }
5d35169e
AM
6849
6850 /* This is a reference to a symbol defined by a dynamic object which
6851 is not a function. */
6852
5bd4f169
AM
6853 /* We must allocate the symbol in our .dynbss section, which will
6854 become part of the .bss section of the executable. There will be
6855 an entry for this symbol in the .dynsym section. The dynamic
6856 object will contain position independent code, so all references
6857 from the dynamic object to this symbol will go through the global
6858 offset table. The dynamic linker will use the .dynsym entry to
6859 determine the address it must put in the global offset table, so
6860 both the dynamic object and the regular object will refer to the
6861 same memory location for the variable. */
5474d94f
AM
6862 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6863 {
6864 s = htab->elf.sdynrelro;
6865 srel = htab->elf.sreldynrelro;
6866 }
6867 else
6868 {
6869 s = htab->elf.sdynbss;
6870 srel = htab->elf.srelbss;
6871 }
1d7e9d18 6872 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6873 {
4a7e5234
AM
6874 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6875 linker to copy the initial value out of the dynamic object
6876 and into the runtime process image. */
5474d94f 6877 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6878 h->needs_copy = 1;
5bd4f169
AM
6879 }
6880
529fe20e 6881 /* We no longer want dyn_relocs. */
190eb1dd 6882 h->dyn_relocs = NULL;
6cabe1ea 6883 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6884}
6885
e86ce104
AM
6886/* If given a function descriptor symbol, hide both the function code
6887 sym and the descriptor. */
6888static void
4ce794b7
AM
6889ppc64_elf_hide_symbol (struct bfd_link_info *info,
6890 struct elf_link_hash_entry *h,
0a1b45a2 6891 bool force_local)
e86ce104 6892{
34814b9f 6893 struct ppc_link_hash_entry *eh;
e86ce104
AM
6894 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6895
87469ba2
AM
6896 if (ppc_hash_table (info) == NULL)
6897 return;
6898
ed7007c1 6899 eh = ppc_elf_hash_entry (h);
34814b9f 6900 if (eh->is_func_descriptor)
e86ce104 6901 {
34814b9f 6902 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6903
721956f4 6904 if (fh == NULL)
d1329ca3
AM
6905 {
6906 const char *p, *q;
b8ac2841 6907 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6908 char save;
6909
6910 /* We aren't supposed to use alloca in BFD because on
6911 systems which do not have alloca the version in libiberty
6912 calls xmalloc, which might cause the program to crash
6913 when it runs out of memory. This function doesn't have a
6914 return status, so there's no way to gracefully return an
6915 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6916 accessed; It's either a string in an ELF string table,
6917 or allocated in an objalloc structure. */
d1329ca3 6918
34814b9f 6919 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6920 save = *p;
6921 *(char *) p = '.';
0a1b45a2
AM
6922 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6923 false, false));
d1329ca3
AM
6924 *(char *) p = save;
6925
6926 /* Unfortunately, if it so happens that the string we were
6927 looking for was allocated immediately before this string,
6928 then we overwrote the string terminator. That's the only
6929 reason the lookup should fail. */
6930 if (fh == NULL)
6931 {
34814b9f
AM
6932 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6933 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6934 --q, --p;
34814b9f 6935 if (q < eh->elf.root.root.string && *p == '.')
0a1b45a2
AM
6936 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6937 false, false));
d1329ca3
AM
6938 }
6939 if (fh != NULL)
6940 {
34814b9f
AM
6941 eh->oh = fh;
6942 fh->oh = eh;
d1329ca3
AM
6943 }
6944 }
e86ce104 6945 if (fh != NULL)
34814b9f 6946 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6947 }
6948}
6949
0a1b45a2 6950static bool
8843416a
AM
6951get_sym_h (struct elf_link_hash_entry **hp,
6952 Elf_Internal_Sym **symp,
6953 asection **symsecp,
f961d9dd 6954 unsigned char **tls_maskp,
8843416a
AM
6955 Elf_Internal_Sym **locsymsp,
6956 unsigned long r_symndx,
6957 bfd *ibfd)
411e1bfb 6958{
0ffa91dd 6959 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6960
6961 if (r_symndx >= symtab_hdr->sh_info)
6962 {
6963 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6964 struct elf_link_hash_entry *h;
6965
6966 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6967 h = elf_follow_link (h);
411e1bfb
AM
6968
6969 if (hp != NULL)
6970 *hp = h;
6971
6972 if (symp != NULL)
6973 *symp = NULL;
6974
6975 if (symsecp != NULL)
6976 {
6977 asection *symsec = NULL;
6978 if (h->root.type == bfd_link_hash_defined
6979 || h->root.type == bfd_link_hash_defweak)
6980 symsec = h->root.u.def.section;
6981 *symsecp = symsec;
6982 }
6983
e7b938ca 6984 if (tls_maskp != NULL)
ed7007c1 6985 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6986 }
6987 else
6988 {
6989 Elf_Internal_Sym *sym;
6990 Elf_Internal_Sym *locsyms = *locsymsp;
6991
6992 if (locsyms == NULL)
6993 {
6994 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6995 if (locsyms == NULL)
6996 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6997 symtab_hdr->sh_info,
6998 0, NULL, NULL, NULL);
6999 if (locsyms == NULL)
0a1b45a2 7000 return false;
411e1bfb
AM
7001 *locsymsp = locsyms;
7002 }
7003 sym = locsyms + r_symndx;
7004
7005 if (hp != NULL)
7006 *hp = NULL;
7007
7008 if (symp != NULL)
7009 *symp = sym;
7010
7011 if (symsecp != NULL)
cb33740c 7012 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7013
e7b938ca 7014 if (tls_maskp != NULL)
411e1bfb
AM
7015 {
7016 struct got_entry **lgot_ents;
f961d9dd 7017 unsigned char *tls_mask;
411e1bfb 7018
e7b938ca 7019 tls_mask = NULL;
411e1bfb
AM
7020 lgot_ents = elf_local_got_ents (ibfd);
7021 if (lgot_ents != NULL)
7022 {
e054468f
AM
7023 struct plt_entry **local_plt = (struct plt_entry **)
7024 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7025 unsigned char *lgot_masks = (unsigned char *)
e054468f 7026 (local_plt + symtab_hdr->sh_info);
e7b938ca 7027 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7028 }
e7b938ca 7029 *tls_maskp = tls_mask;
411e1bfb
AM
7030 }
7031 }
0a1b45a2 7032 return true;
411e1bfb
AM
7033}
7034
e7b938ca 7035/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7036 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7037 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7038
7039static int
f961d9dd 7040get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7041 unsigned long *toc_symndx,
7042 bfd_vma *toc_addend,
0d4792f7 7043 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7044 const Elf_Internal_Rela *rel,
7045 bfd *ibfd)
411e1bfb
AM
7046{
7047 unsigned long r_symndx;
0d4792f7 7048 int next_r;
411e1bfb
AM
7049 struct elf_link_hash_entry *h;
7050 Elf_Internal_Sym *sym;
7051 asection *sec;
7052 bfd_vma off;
7053
7054 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7055 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7056 return 0;
411e1bfb 7057
37da22e5
AM
7058 if ((*tls_maskp != NULL
7059 && (**tls_maskp & TLS_TLS) != 0
7060 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7061 || sec == NULL
6bee8834 7062 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7063 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7064 return 1;
411e1bfb
AM
7065
7066 /* Look inside a TOC section too. */
7067 if (h != NULL)
7068 {
7069 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7070 off = h->root.u.def.value;
7071 }
7072 else
7073 off = sym->st_value;
7074 off += rel->r_addend;
7075 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7076 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7077 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7078 if (toc_symndx != NULL)
7079 *toc_symndx = r_symndx;
3a71aa26
AM
7080 if (toc_addend != NULL)
7081 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7082 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7083 return 0;
854b41e7 7084 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7085 && (next_r == -1 || next_r == -2))
7086 return 1 - next_r;
951fd09b 7087 return 1;
411e1bfb
AM
7088}
7089
3b421ab3
AM
7090/* Find (or create) an entry in the tocsave hash table. */
7091
7092static struct tocsave_entry *
7093tocsave_find (struct ppc_link_hash_table *htab,
7094 enum insert_option insert,
7095 Elf_Internal_Sym **local_syms,
7096 const Elf_Internal_Rela *irela,
7097 bfd *ibfd)
7098{
7099 unsigned long r_indx;
7100 struct elf_link_hash_entry *h;
7101 Elf_Internal_Sym *sym;
7102 struct tocsave_entry ent, *p;
7103 hashval_t hash;
7104 struct tocsave_entry **slot;
7105
7106 r_indx = ELF64_R_SYM (irela->r_info);
7107 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7108 return NULL;
7109 if (ent.sec == NULL || ent.sec->output_section == NULL)
7110 {
4eca0228 7111 _bfd_error_handler
871b3ab2 7112 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7113 return NULL;
7114 }
7115
7116 if (h != NULL)
7117 ent.offset = h->root.u.def.value;
7118 else
7119 ent.offset = sym->st_value;
7120 ent.offset += irela->r_addend;
7121
7122 hash = tocsave_htab_hash (&ent);
7123 slot = ((struct tocsave_entry **)
7124 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7125 if (slot == NULL)
7126 return NULL;
7127
7128 if (*slot == NULL)
7129 {
7130 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7131 if (p == NULL)
7132 return NULL;
7133 *p = ent;
7134 *slot = p;
7135 }
7136 return *slot;
7137}
7138
754021d0 7139/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7140 code for the old ABI, these will already have been done. */
754021d0 7141
0a1b45a2 7142static bool
754021d0
AM
7143adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7144{
7145 struct ppc_link_hash_entry *eh;
7146 asection *sym_sec;
74f0fb50 7147 struct _opd_sec_data *opd;
754021d0
AM
7148
7149 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 7150 return true;
754021d0 7151
754021d0
AM
7152 if (h->root.type != bfd_link_hash_defined
7153 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 7154 return true;
754021d0 7155
ed7007c1 7156 eh = ppc_elf_hash_entry (h);
754021d0 7157 if (eh->adjust_done)
0a1b45a2 7158 return true;
754021d0
AM
7159
7160 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7161 opd = get_opd_info (sym_sec);
7162 if (opd != NULL && opd->adjust != NULL)
754021d0 7163 {
51aecdc5 7164 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7165 if (adjust == -1)
7166 {
7167 /* This entry has been deleted. */
b3fac117 7168 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7169 if (dsec == NULL)
7170 {
7171 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7172 if (discarded_section (dsec))
81688140 7173 {
b3fac117 7174 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7175 break;
7176 }
7177 }
4025353c 7178 eh->elf.root.u.def.value = 0;
81688140 7179 eh->elf.root.u.def.section = dsec;
4025353c
AM
7180 }
7181 else
7182 eh->elf.root.u.def.value += adjust;
754021d0
AM
7183 eh->adjust_done = 1;
7184 }
0a1b45a2 7185 return true;
754021d0
AM
7186}
7187
8c1d1bb8 7188/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7189 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7190 have already been determined. */
7191
0a1b45a2 7192static bool
1657026c 7193dec_dynrel_count (const Elf_Internal_Rela *rel,
8c1d1bb8
AM
7194 asection *sec,
7195 struct bfd_link_info *info,
7196 Elf_Internal_Sym **local_syms,
7197 struct elf_link_hash_entry *h,
19e08130 7198 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7199{
7200 enum elf_ppc64_reloc_type r_type;
19e08130 7201 asection *sym_sec = NULL;
8c1d1bb8
AM
7202
7203 /* Can this reloc be dynamic? This switch, and later tests here
7204 should be kept in sync with the code in check_relocs. */
1657026c 7205 r_type = ELF64_R_TYPE (rel->r_info);
8c1d1bb8
AM
7206 switch (r_type)
7207 {
7208 default:
0a1b45a2 7209 return true;
8c1d1bb8 7210
1bdd8fac
AM
7211 case R_PPC64_TOC16:
7212 case R_PPC64_TOC16_DS:
7213 case R_PPC64_TOC16_LO:
7214 case R_PPC64_TOC16_HI:
7215 case R_PPC64_TOC16_HA:
7216 case R_PPC64_TOC16_LO_DS:
7217 if (h == NULL)
0a1b45a2 7218 return true;
1bdd8fac
AM
7219 break;
7220
8c1d1bb8
AM
7221 case R_PPC64_TPREL16:
7222 case R_PPC64_TPREL16_LO:
7223 case R_PPC64_TPREL16_HI:
7224 case R_PPC64_TPREL16_HA:
7225 case R_PPC64_TPREL16_DS:
7226 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7227 case R_PPC64_TPREL16_HIGH:
7228 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7229 case R_PPC64_TPREL16_HIGHER:
7230 case R_PPC64_TPREL16_HIGHERA:
7231 case R_PPC64_TPREL16_HIGHEST:
7232 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7233 case R_PPC64_TPREL64:
c213164a 7234 case R_PPC64_TPREL34:
8c1d1bb8
AM
7235 case R_PPC64_DTPMOD64:
7236 case R_PPC64_DTPREL64:
7237 case R_PPC64_ADDR64:
7238 case R_PPC64_REL30:
7239 case R_PPC64_REL32:
7240 case R_PPC64_REL64:
7241 case R_PPC64_ADDR14:
7242 case R_PPC64_ADDR14_BRNTAKEN:
7243 case R_PPC64_ADDR14_BRTAKEN:
7244 case R_PPC64_ADDR16:
7245 case R_PPC64_ADDR16_DS:
7246 case R_PPC64_ADDR16_HA:
7247 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7248 case R_PPC64_ADDR16_HIGH:
7249 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7250 case R_PPC64_ADDR16_HIGHER:
7251 case R_PPC64_ADDR16_HIGHERA:
7252 case R_PPC64_ADDR16_HIGHEST:
7253 case R_PPC64_ADDR16_HIGHESTA:
7254 case R_PPC64_ADDR16_LO:
7255 case R_PPC64_ADDR16_LO_DS:
7256 case R_PPC64_ADDR24:
7257 case R_PPC64_ADDR32:
7258 case R_PPC64_UADDR16:
7259 case R_PPC64_UADDR32:
7260 case R_PPC64_UADDR64:
7261 case R_PPC64_TOC:
5663e321
AM
7262 case R_PPC64_D34:
7263 case R_PPC64_D34_LO:
7264 case R_PPC64_D34_HI30:
7265 case R_PPC64_D34_HA30:
7266 case R_PPC64_ADDR16_HIGHER34:
7267 case R_PPC64_ADDR16_HIGHERA34:
7268 case R_PPC64_ADDR16_HIGHEST34:
7269 case R_PPC64_ADDR16_HIGHESTA34:
7270 case R_PPC64_D28:
8c1d1bb8
AM
7271 break;
7272 }
7273
7274 if (local_syms != NULL)
7275 {
7276 unsigned long r_symndx;
8c1d1bb8
AM
7277 bfd *ibfd = sec->owner;
7278
1657026c 7279 r_symndx = ELF64_R_SYM (rel->r_info);
8c1d1bb8 7280 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
0a1b45a2 7281 return false;
8c1d1bb8
AM
7282 }
7283
ec73ddcd 7284 if ((h != NULL
c99d782d 7285 && !SYMBOL_REFERENCES_LOCAL (info, h))
ec73ddcd 7286 || (bfd_link_pic (info)
3a3a4c1f
AM
7287 && (h != NULL
7288 ? !bfd_is_abs_symbol (&h->root)
7289 : sym_sec != bfd_abs_section_ptr)
ec73ddcd
AM
7290 && must_be_dyn_reloc (info, r_type))
7291 || (!bfd_link_pic (info)
7292 && (h != NULL
7293 ? h->type == STT_GNU_IFUNC
7294 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7295 ;
7296 else
0a1b45a2 7297 return true;
8c1d1bb8
AM
7298
7299 if (h != NULL)
6edfbbad 7300 {
1657026c
AM
7301 struct ppc_dyn_relocs *p;
7302 struct ppc_dyn_relocs **pp;
7303 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
7304
7305 /* elf_gc_sweep may have already removed all dyn relocs associated
7306 with local syms for a given section. Also, symbol flags are
7307 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7308 report a dynreloc miscount. */
7309 if (*pp == NULL && info->gc_sections)
0a1b45a2 7310 return true;
19e08130
AM
7311
7312 while ((p = *pp) != NULL)
60124e18 7313 {
19e08130
AM
7314 if (p->sec == sec)
7315 {
7316 if (!must_be_dyn_reloc (info, r_type))
7317 p->pc_count -= 1;
f91074eb 7318 if (maybe_relr (r_type, rel, sec))
1657026c 7319 p->rel_count -= 1;
19e08130
AM
7320 p->count -= 1;
7321 if (p->count == 0)
7322 *pp = p->next;
0a1b45a2 7323 return true;
19e08130
AM
7324 }
7325 pp = &p->next;
60124e18 7326 }
6edfbbad 7327 }
19e08130
AM
7328 else
7329 {
1657026c
AM
7330 struct ppc_local_dyn_relocs *p;
7331 struct ppc_local_dyn_relocs **pp;
19e08130 7332 void *vpp;
0a1b45a2 7333 bool is_ifunc;
8c1d1bb8 7334
19e08130
AM
7335 if (local_syms == NULL)
7336 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7337 if (sym_sec == NULL)
7338 sym_sec = sec;
c57da1a7 7339
19e08130 7340 vpp = &elf_section_data (sym_sec)->local_dynrel;
1657026c 7341 pp = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
7342
7343 if (*pp == NULL && info->gc_sections)
0a1b45a2 7344 return true;
19e08130
AM
7345
7346 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7347 while ((p = *pp) != NULL)
8c1d1bb8 7348 {
19e08130
AM
7349 if (p->sec == sec && p->ifunc == is_ifunc)
7350 {
f91074eb 7351 if (maybe_relr (r_type, rel, sec))
1657026c 7352 p->rel_count -= 1;
19e08130
AM
7353 p->count -= 1;
7354 if (p->count == 0)
7355 *pp = p->next;
0a1b45a2 7356 return true;
19e08130
AM
7357 }
7358 pp = &p->next;
8c1d1bb8 7359 }
8c1d1bb8
AM
7360 }
7361
695344c0 7362 /* xgettext:c-format */
cf97bcb0
AM
7363 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7364 sec->owner, sec);
8c1d1bb8 7365 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7366 return false;
8c1d1bb8
AM
7367}
7368
754021d0
AM
7369/* Remove unused Official Procedure Descriptor entries. Currently we
7370 only remove those associated with functions in discarded link-once
7371 sections, or weakly defined functions that have been overridden. It
7372 would be possible to remove many more entries for statically linked
7373 applications. */
7374
0a1b45a2 7375bool
e7d1c40c 7376ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7377{
7378 bfd *ibfd;
0a1b45a2 7379 bool some_edited = false;
3f764659 7380 asection *need_pad = NULL;
e7d1c40c
AM
7381 struct ppc_link_hash_table *htab;
7382
7383 htab = ppc_hash_table (info);
7384 if (htab == NULL)
0a1b45a2 7385 return false;
1e2f5b6e 7386
c72f2fb2 7387 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7388 {
7389 asection *sec;
7390 Elf_Internal_Rela *relstart, *rel, *relend;
7391 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7392 Elf_Internal_Sym *local_syms;
74f0fb50 7393 struct _opd_sec_data *opd;
0a1b45a2 7394 bool need_edit, add_aux_fields, broken;
3f764659 7395 bfd_size_type cnt_16b = 0;
1e2f5b6e 7396
854b41e7
AM
7397 if (!is_ppc64_elf (ibfd))
7398 continue;
7399
1e2f5b6e 7400 sec = bfd_get_section_by_name (ibfd, ".opd");
81ff113f
AM
7401 if (sec == NULL
7402 || sec->size == 0
7403 || (sec->flags & SEC_HAS_CONTENTS) == 0)
1e2f5b6e
AM
7404 continue;
7405
dbaa2011 7406 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7407 continue;
7408
1e2f5b6e
AM
7409 if (sec->output_section == bfd_abs_section_ptr)
7410 continue;
7411
7412 /* Look through the section relocs. */
7413 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7414 continue;
7415
6cdc0ccc 7416 local_syms = NULL;
0ffa91dd 7417 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7418
7419 /* Read the relocations. */
4ce794b7 7420 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7421 info->keep_memory);
1e2f5b6e 7422 if (relstart == NULL)
0a1b45a2 7423 return false;
1e2f5b6e
AM
7424
7425 /* First run through the relocs to check they are sane, and to
7426 determine whether we need to edit this opd section. */
0a1b45a2
AM
7427 need_edit = false;
7428 broken = false;
3f764659 7429 need_pad = sec;
1e2f5b6e 7430 relend = relstart + sec->reloc_count;
50bc7936 7431 for (rel = relstart; rel < relend; )
1e2f5b6e 7432 {
04c9666a 7433 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7434 unsigned long r_symndx;
7435 asection *sym_sec;
7436 struct elf_link_hash_entry *h;
7437 Elf_Internal_Sym *sym;
51aecdc5 7438 bfd_vma offset;
1e2f5b6e 7439
51aecdc5 7440 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7441 only interested in the reloc pointing to a function entry
7442 point. */
51aecdc5
AM
7443 offset = rel->r_offset;
7444 if (rel + 1 == relend
7445 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7446 {
7447 /* If someone messes with .opd alignment then after a
7448 "ld -r" we might have padding in the middle of .opd.
7449 Also, there's nothing to prevent someone putting
7450 something silly in .opd with the assembler. No .opd
b34976b6 7451 optimization for them! */
3f764659 7452 broken_opd:
4eca0228 7453 _bfd_error_handler
871b3ab2 7454 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
0a1b45a2 7455 broken = true;
1e2f5b6e
AM
7456 break;
7457 }
7458
50bc7936
AM
7459 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7460 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7461 {
4eca0228 7462 _bfd_error_handler
695344c0 7463 /* xgettext:c-format */
871b3ab2 7464 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7465 ibfd, r_type);
0a1b45a2 7466 broken = true;
50bc7936
AM
7467 break;
7468 }
7469
1e2f5b6e 7470 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7471 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7472 r_symndx, ibfd))
50bc7936 7473 goto error_ret;
1e2f5b6e
AM
7474
7475 if (sym_sec == NULL || sym_sec->owner == NULL)
7476 {
411e1bfb
AM
7477 const char *sym_name;
7478 if (h != NULL)
7479 sym_name = h->root.root.string;
7480 else
26c61ae5
L
7481 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7482 sym_sec);
411e1bfb 7483
4eca0228 7484 _bfd_error_handler
695344c0 7485 /* xgettext:c-format */
871b3ab2 7486 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7487 ibfd, sym_name);
0a1b45a2 7488 broken = true;
1e2f5b6e
AM
7489 break;
7490 }
7491
51020317
AM
7492 /* opd entries are always for functions defined in the
7493 current input bfd. If the symbol isn't defined in the
7494 input bfd, then we won't be using the function in this
7495 bfd; It must be defined in a linkonce section in another
7496 bfd, or is weak. It's also possible that we are
7497 discarding the function due to a linker script /DISCARD/,
7498 which we test for via the output_section. */
7499 if (sym_sec->owner != ibfd
7500 || sym_sec->output_section == bfd_abs_section_ptr)
0a1b45a2 7501 need_edit = true;
1e2f5b6e 7502
50bc7936 7503 rel += 2;
51aecdc5
AM
7504 if (rel + 1 == relend
7505 || (rel + 2 < relend
7506 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7507 ++rel;
7508
7509 if (rel == relend)
3f764659
JJ
7510 {
7511 if (sec->size == offset + 24)
7512 {
7513 need_pad = NULL;
7514 break;
7515 }
51aecdc5 7516 if (sec->size == offset + 16)
3f764659
JJ
7517 {
7518 cnt_16b++;
7519 break;
7520 }
7521 goto broken_opd;
7522 }
3f764659
JJ
7523 else if (rel + 1 < relend
7524 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7525 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7526 {
51aecdc5
AM
7527 if (rel[0].r_offset == offset + 16)
7528 cnt_16b++;
7529 else if (rel[0].r_offset != offset + 24)
7530 goto broken_opd;
3f764659
JJ
7531 }
7532 else
7533 goto broken_opd;
1e2f5b6e
AM
7534 }
7535
e7d1c40c 7536 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7537
51aecdc5 7538 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7539 {
7540 Elf_Internal_Rela *write_rel;
d4730f92 7541 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7542 bfd_byte *rptr, *wptr;
983bddc8 7543 bfd_byte *new_contents;
74f0fb50
AM
7544 bfd_size_type amt;
7545
983bddc8 7546 new_contents = NULL;
51aecdc5 7547 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7548 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7549 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50 7550 if (opd->adjust == NULL)
0a1b45a2 7551 return false;
1e2f5b6e
AM
7552
7553 /* This seems a waste of time as input .opd sections are all
7554 zeros as generated by gcc, but I suppose there's no reason
7555 this will always be so. We might start putting something in
7556 the third word of .opd entries. */
7557 if ((sec->flags & SEC_IN_MEMORY) == 0)
7558 {
eea6121a
AM
7559 bfd_byte *loc;
7560 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7561 {
c9594989 7562 free (loc);
50bc7936 7563 error_ret:
c9594989 7564 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7565 free (local_syms);
6cdc0ccc
AM
7566 if (elf_section_data (sec)->relocs != relstart)
7567 free (relstart);
0a1b45a2 7568 return false;
6cdc0ccc 7569 }
1e2f5b6e
AM
7570 sec->contents = loc;
7571 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7572 }
7573
7574 elf_section_data (sec)->relocs = relstart;
7575
3f764659 7576 new_contents = sec->contents;
3f764659
JJ
7577 if (add_aux_fields)
7578 {
7579 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7580 if (new_contents == NULL)
0a1b45a2 7581 return false;
51aecdc5 7582 need_pad = NULL;
3f764659 7583 }
b4f4e59f
AM
7584 wptr = new_contents;
7585 rptr = sec->contents;
1e2f5b6e 7586 write_rel = relstart;
51aecdc5 7587 for (rel = relstart; rel < relend; )
1e2f5b6e 7588 {
50bc7936
AM
7589 unsigned long r_symndx;
7590 asection *sym_sec;
7591 struct elf_link_hash_entry *h;
51aecdc5 7592 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7593 Elf_Internal_Sym *sym;
51aecdc5
AM
7594 long opd_ent_size;
7595 Elf_Internal_Rela *next_rel;
0a1b45a2 7596 bool skip;
50bc7936
AM
7597
7598 r_symndx = ELF64_R_SYM (rel->r_info);
7599 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7600 r_symndx, ibfd))
50bc7936
AM
7601 goto error_ret;
7602
51aecdc5
AM
7603 next_rel = rel + 2;
7604 if (next_rel + 1 == relend
7605 || (next_rel + 2 < relend
7606 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7607 ++next_rel;
7608
7609 /* See if the .opd entry is full 24 byte or
7610 16 byte (with fd_aux entry overlapped with next
7611 fd_func). */
7612 opd_ent_size = 24;
7613 if (next_rel == relend)
1e2f5b6e 7614 {
51aecdc5 7615 if (sec->size == rel->r_offset + 16)
3f764659 7616 opd_ent_size = 16;
51aecdc5
AM
7617 }
7618 else if (next_rel->r_offset == rel->r_offset + 16)
7619 opd_ent_size = 16;
3f764659 7620
51aecdc5
AM
7621 if (h != NULL
7622 && h->root.root.string[0] == '.')
7623 {
ed7007c1 7624 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7625 if (fdh != NULL)
7626 {
7627 fdh = ppc_follow_link (fdh);
7628 if (fdh->elf.root.type != bfd_link_hash_defined
7629 && fdh->elf.root.type != bfd_link_hash_defweak)
7630 fdh = NULL;
7631 }
51aecdc5 7632 }
1e2f5b6e 7633
51aecdc5
AM
7634 skip = (sym_sec->owner != ibfd
7635 || sym_sec->output_section == bfd_abs_section_ptr);
7636 if (skip)
7637 {
7638 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7639 {
51aecdc5
AM
7640 /* Arrange for the function descriptor sym
7641 to be dropped. */
7642 fdh->elf.root.u.def.value = 0;
7643 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7644 }
51aecdc5 7645 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7646
0e1862bb 7647 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7648 rel = next_rel;
7649 else
7650 while (1)
7651 {
1657026c 7652 if (!dec_dynrel_count (rel, sec, info,
51aecdc5
AM
7653 NULL, h, sym))
7654 goto error_ret;
754021d0 7655
51aecdc5
AM
7656 if (++rel == next_rel)
7657 break;
1e2f5b6e 7658
51aecdc5
AM
7659 r_symndx = ELF64_R_SYM (rel->r_info);
7660 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7661 r_symndx, ibfd))
7662 goto error_ret;
7663 }
50bc7936
AM
7664 }
7665 else
1e2f5b6e 7666 {
51aecdc5
AM
7667 /* We'll be keeping this opd entry. */
7668 long adjust;
7669
7670 if (fdh != NULL)
7671 {
7672 /* Redefine the function descriptor symbol to
7673 this location in the opd section. It is
7674 necessary to update the value here rather
7675 than using an array of adjustments as we do
7676 for local symbols, because various places
7677 in the generic ELF code use the value
7678 stored in u.def.value. */
7679 fdh->elf.root.u.def.value = wptr - new_contents;
7680 fdh->adjust_done = 1;
7681 }
7682
7683 /* Local syms are a bit tricky. We could
7684 tweak them as they can be cached, but
7685 we'd need to look through the local syms
7686 for the function descriptor sym which we
7687 don't have at the moment. So keep an
7688 array of adjustments. */
7689 adjust = (wptr - new_contents) - (rptr - sec->contents);
7690 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7691
7692 if (wptr != rptr)
7693 memcpy (wptr, rptr, opd_ent_size);
7694 wptr += opd_ent_size;
7695 if (add_aux_fields && opd_ent_size == 16)
7696 {
7697 memset (wptr, '\0', 8);
7698 wptr += 8;
7699 }
7700
50bc7936 7701 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7702 new opd entries. */
7703 for ( ; rel != next_rel; ++rel)
7704 {
7705 rel->r_offset += adjust;
7706 if (write_rel != rel)
7707 memcpy (write_rel, rel, sizeof (*rel));
7708 ++write_rel;
7709 }
1e2f5b6e 7710 }
51aecdc5
AM
7711
7712 rptr += opd_ent_size;
1e2f5b6e
AM
7713 }
7714
3f764659 7715 sec->size = wptr - new_contents;
1e2f5b6e 7716 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7717 if (add_aux_fields)
7718 {
7719 free (sec->contents);
7720 sec->contents = new_contents;
7721 }
7722
05bf9422 7723 /* Fudge the header size too, as this is used later in
cdcf6e38 7724 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7725 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7726 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
0a1b45a2 7727 some_edited = true;
1e2f5b6e 7728 }
6cdc0ccc 7729 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7730 free (relstart);
6cdc0ccc 7731
411e1bfb
AM
7732 if (local_syms != NULL
7733 && symtab_hdr->contents != (unsigned char *) local_syms)
7734 {
7735 if (!info->keep_memory)
7736 free (local_syms);
7737 else
7738 symtab_hdr->contents = (unsigned char *) local_syms;
7739 }
7740 }
7741
754021d0
AM
7742 if (some_edited)
7743 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7744
3f764659
JJ
7745 /* If we are doing a final link and the last .opd entry is just 16 byte
7746 long, add a 8 byte padding after it. */
0e1862bb 7747 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7748 {
7749 bfd_byte *p;
7750
7751 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7752 {
7753 BFD_ASSERT (need_pad->size > 0);
7754
7755 p = bfd_malloc (need_pad->size + 8);
7756 if (p == NULL)
0a1b45a2 7757 return false;
699733f6 7758
2cdcc330
AM
7759 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7760 p, 0, need_pad->size))
0a1b45a2 7761 return false;
3f764659
JJ
7762
7763 need_pad->contents = p;
7764 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7765 }
7766 else
7767 {
7768 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7769 if (p == NULL)
0a1b45a2 7770 return false;
3f764659
JJ
7771
7772 need_pad->contents = p;
7773 }
7774
7775 memset (need_pad->contents + need_pad->size, 0, 8);
7776 need_pad->size += 8;
7777 }
7778
0a1b45a2 7779 return true;
411e1bfb
AM
7780}
7781
3e04d765
AM
7782/* Analyze inline PLT call relocations to see whether calls to locally
7783 defined functions can be converted to direct calls. */
7784
0a1b45a2 7785bool
3e04d765
AM
7786ppc64_elf_inline_plt (struct bfd_link_info *info)
7787{
7788 struct ppc_link_hash_table *htab;
7789 bfd *ibfd;
7790 asection *sec;
7791 bfd_vma low_vma, high_vma, limit;
7792
7793 htab = ppc_hash_table (info);
7794 if (htab == NULL)
0a1b45a2 7795 return false;
3e04d765
AM
7796
7797 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7798 reduced somewhat to cater for possible stubs that might be added
7799 between the call and its destination. */
7800 if (htab->params->group_size < 0)
7801 {
7802 limit = -htab->params->group_size;
7803 if (limit == 1)
7804 limit = 0x1e00000;
7805 }
7806 else
7807 {
7808 limit = htab->params->group_size;
7809 if (limit == 1)
7810 limit = 0x1c00000;
7811 }
7812
7813 low_vma = -1;
7814 high_vma = 0;
7815 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7816 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7817 {
7818 if (low_vma > sec->vma)
7819 low_vma = sec->vma;
7820 if (high_vma < sec->vma + sec->size)
7821 high_vma = sec->vma + sec->size;
7822 }
7823
7824 /* If a "bl" can reach anywhere in local code sections, then we can
7825 convert all inline PLT sequences to direct calls when the symbol
7826 is local. */
7827 if (high_vma - low_vma < limit)
7828 {
7829 htab->can_convert_all_inline_plt = 1;
0a1b45a2 7830 return true;
3e04d765
AM
7831 }
7832
7833 /* Otherwise, go looking through relocs for cases where a direct
7834 call won't reach. Mark the symbol on any such reloc to disable
7835 the optimization and keep the PLT entry as it seems likely that
7836 this will be better than creating trampolines. Note that this
7837 will disable the optimization for all inline PLT calls to a
7838 particular symbol, not just those that won't reach. The
7839 difficulty in doing a more precise optimization is that the
7840 linker needs to make a decision depending on whether a
7841 particular R_PPC64_PLTCALL insn can be turned into a direct
7842 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7843 the sequence, and there is nothing that ties those relocs
7844 together except their symbol. */
7845
7846 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7847 {
7848 Elf_Internal_Shdr *symtab_hdr;
7849 Elf_Internal_Sym *local_syms;
7850
7851 if (!is_ppc64_elf (ibfd))
7852 continue;
7853
7854 local_syms = NULL;
7855 symtab_hdr = &elf_symtab_hdr (ibfd);
7856
7857 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7858 if (ppc64_elf_section_data (sec)->has_pltcall
7859 && !bfd_is_abs_section (sec->output_section))
7860 {
7861 Elf_Internal_Rela *relstart, *rel, *relend;
7862
7863 /* Read the relocations. */
7864 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7865 info->keep_memory);
7866 if (relstart == NULL)
0a1b45a2 7867 return false;
3e04d765
AM
7868
7869 relend = relstart + sec->reloc_count;
435edf0b 7870 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7871 {
7872 enum elf_ppc64_reloc_type r_type;
7873 unsigned long r_symndx;
7874 asection *sym_sec;
7875 struct elf_link_hash_entry *h;
7876 Elf_Internal_Sym *sym;
7877 unsigned char *tls_maskp;
7878
7879 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7880 if (r_type != R_PPC64_PLTCALL
7881 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7882 continue;
7883
7884 r_symndx = ELF64_R_SYM (rel->r_info);
7885 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7886 r_symndx, ibfd))
7887 {
7888 if (elf_section_data (sec)->relocs != relstart)
7889 free (relstart);
c9594989 7890 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765 7891 free (local_syms);
0a1b45a2 7892 return false;
3e04d765
AM
7893 }
7894
7895 if (sym_sec != NULL && sym_sec->output_section != NULL)
7896 {
7897 bfd_vma from, to;
7898 if (h != NULL)
7899 to = h->root.u.def.value;
7900 else
7901 to = sym->st_value;
7902 to += (rel->r_addend
7903 + sym_sec->output_offset
7904 + sym_sec->output_section->vma);
7905 from = (rel->r_offset
7906 + sec->output_offset
7907 + sec->output_section->vma);
5663e321
AM
7908 if (to - from + limit < 2 * limit
7909 && !(r_type == R_PPC64_PLTCALL_NOTOC
7910 && (((h ? h->other : sym->st_other)
7911 & STO_PPC64_LOCAL_MASK)
4a4e7361 7912 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7913 *tls_maskp &= ~PLT_KEEP;
7914 }
7915 }
7916 if (elf_section_data (sec)->relocs != relstart)
7917 free (relstart);
7918 }
7919
7920 if (local_syms != NULL
7921 && symtab_hdr->contents != (unsigned char *) local_syms)
7922 {
7923 if (!info->keep_memory)
7924 free (local_syms);
7925 else
7926 symtab_hdr->contents = (unsigned char *) local_syms;
7927 }
7928 }
7929
0a1b45a2 7930 return true;
3e04d765
AM
7931}
7932
c9405344
AM
7933/* Set htab->tls_get_addr and various other info specific to TLS.
7934 This needs to run before dynamic symbols are processed in
7935 bfd_elf_size_dynamic_sections. */
411e1bfb 7936
0a1b45a2 7937bool
e7d1c40c 7938ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7939{
411e1bfb 7940 struct ppc_link_hash_table *htab;
9e7028aa 7941 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7942
411e1bfb 7943 htab = ppc_hash_table (info);
4dfe6ac6 7944 if (htab == NULL)
0a1b45a2 7945 return false;
4dfe6ac6 7946
a442059f
AM
7947 /* Move dynamic linking info to the function descriptor sym. */
7948 if (htab->need_func_desc_adj)
7949 {
7950 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7951 htab->need_func_desc_adj = 0;
7952 }
7953
ee67d69a
AM
7954 if (abiversion (info->output_bfd) == 1)
7955 htab->opd_abi = 1;
7956
e7d1c40c 7957 if (htab->params->no_multi_toc)
33c0ec9d
AM
7958 htab->do_multi_toc = 0;
7959 else if (!htab->do_multi_toc)
e7d1c40c 7960 htab->params->no_multi_toc = 1;
33c0ec9d 7961
8b5f1ed8
AM
7962 /* Default to --no-plt-localentry, as this option can cause problems
7963 with symbol interposition. For example, glibc libpthread.so and
7964 libc.so duplicate many pthread symbols, with a fallback
7965 implementation in libc.so. In some cases the fallback does more
7966 work than the pthread implementation. __pthread_condattr_destroy
7967 is one such symbol: the libpthread.so implementation is
7968 localentry:0 while the libc.so implementation is localentry:8.
7969 An app that "cleverly" uses dlopen to only load necessary
7970 libraries at runtime may omit loading libpthread.so when not
7971 running multi-threaded, which then results in the libc.so
7972 fallback symbols being used and ld.so complaining. Now there
7973 are workarounds in ld (see non_zero_localentry) to detect the
7974 pthread situation, but that may not be the only case where
7975 --plt-localentry can cause trouble. */
f378ab09 7976 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7977 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7978 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7979 {
7980 /* The issue is that __glink_PLTresolve saves r2, which is done
7981 because glibc ld.so _dl_runtime_resolve restores r2 to support
7982 a glibc plt call optimisation where global entry code is
7983 skipped on calls that resolve to the same binary. The
7984 __glink_PLTresolve save of r2 is incompatible with code
7985 making tail calls, because the tail call might go via the
7986 resolver and thus overwrite the proper saved r2. */
7987 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7988 "power10 pc-relative code"));
7989 htab->params->plt_localentry0 = 0;
7990 }
d44c746a
AM
7991 if (htab->params->plt_localentry0
7992 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
0a1b45a2 7993 false, false, false) == NULL)
cf97bcb0
AM
7994 _bfd_error_handler
7995 (_("warning: --plt-localentry is especially dangerous without "
7996 "ld.so support to detect ABI violations"));
f378ab09 7997
9e7028aa 7998 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 7999 false, false, true);
9e7028aa 8000 htab->tls_get_addr = ppc_elf_hash_entry (tga);
9e7028aa 8001 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 8002 false, false, true);
9e7028aa
AM
8003 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
8004
8005 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
0a1b45a2 8006 false, false, true);
9e7028aa 8007 htab->tga_desc = ppc_elf_hash_entry (desc);
9e7028aa 8008 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
0a1b45a2 8009 false, false, true);
9e7028aa
AM
8010 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
8011
7c9cf415 8012 if (htab->params->tls_get_addr_opt)
a7f2871e 8013 {
9e7028aa 8014 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
8015
8016 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
0a1b45a2 8017 false, false, true);
a7f2871e 8018 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
0a1b45a2 8019 false, false, true);
a7f2871e
AM
8020 if (opt_fd != NULL
8021 && (opt_fd->root.type == bfd_link_hash_defined
8022 || opt_fd->root.type == bfd_link_hash_defweak))
8023 {
8024 /* If glibc supports an optimized __tls_get_addr call stub,
8025 signalled by the presence of __tls_get_addr_opt, and we'll
8026 be calling __tls_get_addr via a plt call stub, then
8027 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
8028 if (!(htab->elf.dynamic_sections_created
8029 && tga_fd != NULL
8030 && (tga_fd->type == STT_FUNC
8031 || tga_fd->needs_plt)
8032 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8033 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8034 tga_fd = NULL;
8035 if (!(htab->elf.dynamic_sections_created
8036 && desc_fd != NULL
8037 && (desc_fd->type == STT_FUNC
8038 || desc_fd->needs_plt)
8039 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8040 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8041 desc_fd = NULL;
8042
8043 if (tga_fd != NULL || desc_fd != NULL)
8044 {
8045 struct plt_entry *ent = NULL;
8046
8047 if (tga_fd != NULL)
8048 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8049 if (ent->plt.refcount > 0)
8050 break;
8051 if (ent == NULL && desc_fd != NULL)
8052 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8053 if (ent->plt.refcount > 0)
8054 break;
a7f2871e
AM
8055 if (ent != NULL)
8056 {
9e7028aa
AM
8057 if (tga_fd != NULL)
8058 {
8059 tga_fd->root.type = bfd_link_hash_indirect;
8060 tga_fd->root.u.i.link = &opt_fd->root;
8061 tga_fd->root.u.i.warning = NULL;
8062 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8063 }
8064 if (desc_fd != NULL)
8065 {
8066 desc_fd->root.type = bfd_link_hash_indirect;
8067 desc_fd->root.u.i.link = &opt_fd->root;
8068 desc_fd->root.u.i.warning = NULL;
8069 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8070 }
b531344c 8071 opt_fd->mark = 1;
a7f2871e
AM
8072 if (opt_fd->dynindx != -1)
8073 {
8074 /* Use __tls_get_addr_opt in dynamic relocations. */
8075 opt_fd->dynindx = -1;
8076 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8077 opt_fd->dynstr_index);
8078 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
0a1b45a2 8079 return false;
a7f2871e 8080 }
9e7028aa 8081 if (tga_fd != NULL)
a7f2871e 8082 {
9e7028aa 8083 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 8084 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
8085 if (opt != NULL && tga != NULL)
8086 {
8087 tga->root.type = bfd_link_hash_indirect;
8088 tga->root.u.i.link = &opt->root;
8089 tga->root.u.i.warning = NULL;
8090 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8091 opt->mark = 1;
8092 _bfd_elf_link_hash_hide_symbol (info, opt,
8093 tga->forced_local);
8094 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8095 }
8096 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8097 htab->tls_get_addr_fd->is_func_descriptor = 1;
8098 if (htab->tls_get_addr != NULL)
8099 {
8100 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8101 htab->tls_get_addr->is_func = 1;
8102 }
a7f2871e 8103 }
9e7028aa 8104 if (desc_fd != NULL)
a7f2871e 8105 {
9e7028aa
AM
8106 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8107 if (opt != NULL && desc != NULL)
8108 {
8109 desc->root.type = bfd_link_hash_indirect;
8110 desc->root.u.i.link = &opt->root;
8111 desc->root.u.i.warning = NULL;
8112 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8113 opt->mark = 1;
8114 _bfd_elf_link_hash_hide_symbol (info, opt,
8115 desc->forced_local);
8116 htab->tga_desc = ppc_elf_hash_entry (opt);
8117 }
8118 htab->tga_desc_fd->oh = htab->tga_desc;
8119 htab->tga_desc_fd->is_func_descriptor = 1;
8120 if (htab->tga_desc != NULL)
8121 {
8122 htab->tga_desc->oh = htab->tga_desc_fd;
8123 htab->tga_desc->is_func = 1;
8124 }
a7f2871e
AM
8125 }
8126 }
8127 }
8128 }
7c9cf415
AM
8129 else if (htab->params->tls_get_addr_opt < 0)
8130 htab->params->tls_get_addr_opt = 0;
a7f2871e 8131 }
9e7028aa
AM
8132
8133 if (htab->tga_desc_fd != NULL
8134 && htab->params->tls_get_addr_opt
8135 && htab->params->no_tls_get_addr_regsave == -1)
8136 htab->params->no_tls_get_addr_regsave = 0;
8137
0a1b45a2 8138 return true;
3a71aa26 8139}
8387904d 8140
3a71aa26 8141/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 8142 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 8143
0a1b45a2 8144static bool
30845f11
AM
8145branch_reloc_hash_match (bfd *ibfd,
8146 Elf_Internal_Rela *rel,
8147 struct ppc_link_hash_entry *hash1,
8148 struct ppc_link_hash_entry *hash2,
8149 struct ppc_link_hash_entry *hash3,
8150 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
8151{
8152 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8153 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8154 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8155
e054468f 8156 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8157 {
3a71aa26
AM
8158 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8159 struct elf_link_hash_entry *h;
8387904d 8160
3a71aa26 8161 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8162 h = elf_follow_link (h);
30845f11
AM
8163 if (h == elf_hash_entry (hash1)
8164 || h == elf_hash_entry (hash2)
8165 || h == elf_hash_entry (hash3)
8166 || h == elf_hash_entry (hash4))
0a1b45a2 8167 return true;
a48ebf4d 8168 }
0a1b45a2 8169 return false;
951fd09b 8170}
411e1bfb 8171
951fd09b
AM
8172/* Run through all the TLS relocs looking for optimization
8173 opportunities. The linker has been hacked (see ppc64elf.em) to do
8174 a preliminary section layout so that we know the TLS segment
8175 offsets. We can't optimize earlier because some optimizations need
8176 to know the tp offset, and we need to optimize before allocating
8177 dynamic relocations. */
8178
0a1b45a2 8179bool
33c0ec9d 8180ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8181{
8182 bfd *ibfd;
8183 asection *sec;
8184 struct ppc_link_hash_table *htab;
663a1470 8185 unsigned char *toc_ref;
102890f0 8186 int pass;
951fd09b 8187
3cbc1e5e 8188 if (!bfd_link_executable (info))
0a1b45a2 8189 return true;
411e1bfb 8190
951fd09b 8191 htab = ppc_hash_table (info);
4dfe6ac6 8192 if (htab == NULL)
0a1b45a2 8193 return false;
4dfe6ac6 8194
252dcdf4
AM
8195 htab->do_tls_opt = 1;
8196
663a1470
AM
8197 /* Make two passes over the relocs. On the first pass, mark toc
8198 entries involved with tls relocs, and check that tls relocs
8199 involved in setting up a tls_get_addr call are indeed followed by
8200 such a call. If they are not, we can't do any tls optimization.
8201 On the second pass twiddle tls_mask flags to notify
8202 relocate_section that optimization can be done, and adjust got
8203 and plt refcounts. */
8204 toc_ref = NULL;
8205 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8206 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8207 {
8208 Elf_Internal_Sym *locsyms = NULL;
8209 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8210
102890f0
AM
8211 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8212 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8213 {
8214 Elf_Internal_Rela *relstart, *rel, *relend;
0a1b45a2 8215 bool found_tls_get_addr_arg = 0;
411e1bfb 8216
102890f0
AM
8217 /* Read the relocations. */
8218 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8219 info->keep_memory);
8220 if (relstart == NULL)
2915c55b
JK
8221 {
8222 free (toc_ref);
0a1b45a2 8223 return false;
2915c55b 8224 }
411e1bfb 8225
102890f0
AM
8226 relend = relstart + sec->reloc_count;
8227 for (rel = relstart; rel < relend; rel++)
8228 {
8229 enum elf_ppc64_reloc_type r_type;
8230 unsigned long r_symndx;
8231 struct elf_link_hash_entry *h;
8232 Elf_Internal_Sym *sym;
8233 asection *sym_sec;
f961d9dd 8234 unsigned char *tls_mask;
46e9995a 8235 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0 8236 bfd_vma value;
0a1b45a2 8237 bool ok_tprel, is_local;
102890f0
AM
8238 long toc_ref_index = 0;
8239 int expecting_tls_get_addr = 0;
0a1b45a2 8240 bool ret = false;
411e1bfb 8241
102890f0
AM
8242 r_symndx = ELF64_R_SYM (rel->r_info);
8243 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8244 r_symndx, ibfd))
8245 {
8246 err_free_rel:
8247 if (elf_section_data (sec)->relocs != relstart)
8248 free (relstart);
c9594989
AM
8249 free (toc_ref);
8250 if (elf_symtab_hdr (ibfd).contents
8251 != (unsigned char *) locsyms)
102890f0 8252 free (locsyms);
663a1470 8253 return ret;
102890f0 8254 }
411e1bfb 8255
102890f0
AM
8256 if (h != NULL)
8257 {
766bc656
AM
8258 if (h->root.type == bfd_link_hash_defined
8259 || h->root.type == bfd_link_hash_defweak)
8260 value = h->root.u.def.value;
8261 else if (h->root.type == bfd_link_hash_undefweak)
8262 value = 0;
8263 else
663a1470
AM
8264 {
8265 found_tls_get_addr_arg = 0;
8266 continue;
8267 }
102890f0
AM
8268 }
8269 else
8270 /* Symbols referenced by TLS relocs must be of type
8271 STT_TLS. So no need for .opd local sym adjust. */
8272 value = sym->st_value;
8273
0a1b45a2 8274 ok_tprel = false;
f749f26e
AM
8275 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8276 if (is_local)
102890f0 8277 {
766bc656
AM
8278 if (h != NULL
8279 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 8280 ok_tprel = true;
c27b8c2a
AM
8281 else if (sym_sec != NULL
8282 && sym_sec->output_section != NULL)
766bc656
AM
8283 {
8284 value += sym_sec->output_offset;
8285 value += sym_sec->output_section->vma;
0b147428 8286 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8287 /* Note that even though the prefix insns
8288 allow a 1<<33 offset we use the same test
8289 as for addis;addi. There may be a mix of
8290 pcrel and non-pcrel code and the decision
8291 to optimise is per symbol, not per TLS
8292 sequence. */
0b147428 8293 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8294 }
102890f0 8295 }
951fd09b 8296
102890f0 8297 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8298 /* If this section has old-style __tls_get_addr calls
8299 without marker relocs, then check that each
8300 __tls_get_addr call reloc is preceded by a reloc
8301 that conceivably belongs to the __tls_get_addr arg
8302 setup insn. If we don't find matching arg setup
8303 relocs, don't do any tls optimization. */
8304 if (pass == 0
9737e8af 8305 && sec->nomark_tls_get_addr
663a1470 8306 && h != NULL
ed7007c1 8307 && is_tls_get_addr (h, htab)
663a1470
AM
8308 && !found_tls_get_addr_arg
8309 && is_branch_reloc (r_type))
8310 {
25f53a85 8311 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8312 "TLS optimization disabled\n"),
8313 ibfd, sec, rel->r_offset);
0a1b45a2 8314 ret = true;
663a1470
AM
8315 goto err_free_rel;
8316 }
8317
8318 found_tls_get_addr_arg = 0;
102890f0
AM
8319 switch (r_type)
8320 {
8321 case R_PPC64_GOT_TLSLD16:
8322 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8323 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8324 expecting_tls_get_addr = 1;
663a1470 8325 found_tls_get_addr_arg = 1;
1a0670f3 8326 /* Fall through. */
102890f0
AM
8327
8328 case R_PPC64_GOT_TLSLD16_HI:
8329 case R_PPC64_GOT_TLSLD16_HA:
8330 /* These relocs should never be against a symbol
8331 defined in a shared lib. Leave them alone if
8332 that turns out to be the case. */
8333 if (!is_local)
8334 continue;
411e1bfb 8335
102890f0 8336 /* LD -> LE */
411e1bfb 8337 tls_set = 0;
102890f0
AM
8338 tls_clear = TLS_LD;
8339 tls_type = TLS_TLS | TLS_LD;
8340 break;
411e1bfb 8341
102890f0
AM
8342 case R_PPC64_GOT_TLSGD16:
8343 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8344 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8345 expecting_tls_get_addr = 1;
663a1470 8346 found_tls_get_addr_arg = 1;
1a0670f3 8347 /* Fall through. */
102890f0
AM
8348
8349 case R_PPC64_GOT_TLSGD16_HI:
8350 case R_PPC64_GOT_TLSGD16_HA:
8351 if (ok_tprel)
8352 /* GD -> LE */
411e1bfb 8353 tls_set = 0;
102890f0
AM
8354 else
8355 /* GD -> IE */
b00a0a86 8356 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8357 tls_clear = TLS_GD;
8358 tls_type = TLS_TLS | TLS_GD;
8359 break;
8360
87c69f97 8361 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8362 case R_PPC64_GOT_TPREL16_DS:
8363 case R_PPC64_GOT_TPREL16_LO_DS:
8364 case R_PPC64_GOT_TPREL16_HI:
8365 case R_PPC64_GOT_TPREL16_HA:
8366 if (ok_tprel)
8367 {
8368 /* IE -> LE */
8369 tls_set = 0;
8370 tls_clear = TLS_TPREL;
8371 tls_type = TLS_TLS | TLS_TPREL;
8372 break;
8373 }
411e1bfb
AM
8374 continue;
8375
727fc41e 8376 case R_PPC64_TLSLD:
7d04a20a
AM
8377 if (!is_local)
8378 continue;
8379 /* Fall through. */
8380 case R_PPC64_TLSGD:
23cedd1d
AM
8381 if (rel + 1 < relend
8382 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8383 {
8384 if (pass != 0
2cdcc330 8385 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8386 != R_PPC64_PLTSEQ)
8387 && (ELF64_R_TYPE (rel[1].r_info)
8388 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8389 {
8390 r_symndx = ELF64_R_SYM (rel[1].r_info);
8391 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8392 r_symndx, ibfd))
23cedd1d
AM
8393 goto err_free_rel;
8394 if (h != NULL)
8395 {
8396 struct plt_entry *ent = NULL;
8397
8398 for (ent = h->plt.plist;
8399 ent != NULL;
8400 ent = ent->next)
8401 if (ent->addend == rel[1].r_addend)
8402 break;
8403
8404 if (ent != NULL
8405 && ent->plt.refcount > 0)
8406 ent->plt.refcount -= 1;
8407 }
8408 }
8409 continue;
8410 }
663a1470 8411 found_tls_get_addr_arg = 1;
1a0670f3 8412 /* Fall through. */
663a1470
AM
8413
8414 case R_PPC64_TLS:
8415 case R_PPC64_TOC16:
8416 case R_PPC64_TOC16_LO:
102890f0
AM
8417 if (sym_sec == NULL || sym_sec != toc)
8418 continue;
8419
8420 /* Mark this toc entry as referenced by a TLS
8421 code sequence. We can do that now in the
8422 case of R_PPC64_TLS, and after checking for
8423 tls_get_addr for the TOC16 relocs. */
8424 if (toc_ref == NULL)
2cdcc330
AM
8425 toc_ref
8426 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8427 if (toc_ref == NULL)
8428 goto err_free_rel;
8429
102890f0
AM
8430 if (h != NULL)
8431 value = h->root.u.def.value;
8432 else
8433 value = sym->st_value;
8434 value += rel->r_addend;
73242275
AM
8435 if (value % 8 != 0)
8436 continue;
8437 BFD_ASSERT (value < toc->size
8438 && toc->output_offset % 8 == 0);
663a1470 8439 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8440 if (r_type == R_PPC64_TLS
8441 || r_type == R_PPC64_TLSGD
8442 || r_type == R_PPC64_TLSLD)
102890f0
AM
8443 {
8444 toc_ref[toc_ref_index] = 1;
8445 continue;
8446 }
8447
8448 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8449 continue;
8450
8451 tls_set = 0;
8452 tls_clear = 0;
8453 expecting_tls_get_addr = 2;
8454 break;
8455
8456 case R_PPC64_TPREL64:
8457 if (pass == 0
8458 || sec != toc
8459 || toc_ref == NULL
663a1470 8460 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8461 continue;
8462 if (ok_tprel)
8463 {
8464 /* IE -> LE */
8465 tls_set = TLS_EXPLICIT;
8466 tls_clear = TLS_TPREL;
8467 break;
8468 }
8469 continue;
8470
8471 case R_PPC64_DTPMOD64:
8472 if (pass == 0
8473 || sec != toc
8474 || toc_ref == NULL
663a1470 8475 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8476 continue;
8477 if (rel + 1 < relend
8478 && (rel[1].r_info
8479 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8480 && rel[1].r_offset == rel->r_offset + 8)
8481 {
8482 if (ok_tprel)
8483 /* GD -> LE */
8484 tls_set = TLS_EXPLICIT | TLS_GD;
8485 else
8486 /* GD -> IE */
b00a0a86 8487 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8488 tls_clear = TLS_GD;
8489 }
8490 else
8491 {
8492 if (!is_local)
8493 continue;
8494
8495 /* LD -> LE */
8496 tls_set = TLS_EXPLICIT;
8497 tls_clear = TLS_LD;
8498 }
8499 break;
8500
252dcdf4
AM
8501 case R_PPC64_TPREL16_HA:
8502 if (pass == 0)
8503 {
8504 unsigned char buf[4];
8505 unsigned int insn;
8506 bfd_vma off = rel->r_offset & ~3;
8507 if (!bfd_get_section_contents (ibfd, sec, buf,
8508 off, 4))
8509 goto err_free_rel;
8510 insn = bfd_get_32 (ibfd, buf);
8511 /* addis rt,13,imm */
8512 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8513 != ((15u << 26) | (13 << 16)))
8514 {
8515 /* xgettext:c-format */
8516 info->callbacks->minfo
8517 (_("%H: warning: %s unexpected insn %#x.\n"),
8518 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8519 htab->do_tls_opt = 0;
8520 }
8521 }
8522 continue;
8523
8524 case R_PPC64_TPREL16_HI:
8525 case R_PPC64_TPREL16_HIGH:
8526 case R_PPC64_TPREL16_HIGHA:
8527 case R_PPC64_TPREL16_HIGHER:
8528 case R_PPC64_TPREL16_HIGHERA:
8529 case R_PPC64_TPREL16_HIGHEST:
8530 case R_PPC64_TPREL16_HIGHESTA:
8531 /* These can all be used in sequences along with
8532 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8533 able to verify easily. */
8534 htab->do_tls_opt = 0;
8535 continue;
8536
102890f0
AM
8537 default:
8538 continue;
8539 }
8540
8541 if (pass == 0)
8542 {
727fc41e 8543 if (!expecting_tls_get_addr
9737e8af 8544 || !sec->nomark_tls_get_addr)
102890f0
AM
8545 continue;
8546
3a71aa26
AM
8547 if (rel + 1 < relend
8548 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8549 htab->tls_get_addr_fd,
8550 htab->tga_desc_fd,
3a71aa26 8551 htab->tls_get_addr,
9e7028aa 8552 htab->tga_desc))
102890f0 8553 {
3a71aa26 8554 if (expecting_tls_get_addr == 2)
102890f0 8555 {
3a71aa26 8556 /* Check for toc tls entries. */
f961d9dd 8557 unsigned char *toc_tls;
3a71aa26
AM
8558 int retval;
8559
8560 retval = get_tls_mask (&toc_tls, NULL, NULL,
8561 &locsyms,
8562 rel, ibfd);
8563 if (retval == 0)
8564 goto err_free_rel;
663a1470
AM
8565 if (toc_tls != NULL)
8566 {
37da22e5
AM
8567 if ((*toc_tls & TLS_TLS) != 0
8568 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8569 found_tls_get_addr_arg = 1;
8570 if (retval > 1)
8571 toc_ref[toc_ref_index] = 1;
8572 }
102890f0 8573 }
3a71aa26 8574 continue;
102890f0
AM
8575 }
8576
102890f0
AM
8577 /* Uh oh, we didn't find the expected call. We
8578 could just mark this symbol to exclude it
8579 from tls optimization but it's safer to skip
663a1470 8580 the entire optimization. */
695344c0 8581 /* xgettext:c-format */
25f53a85 8582 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8583 "TLS optimization disabled\n"),
8584 ibfd, sec, rel->r_offset);
0a1b45a2 8585 ret = true;
663a1470 8586 goto err_free_rel;
102890f0
AM
8587 }
8588
37da22e5
AM
8589 /* If we don't have old-style __tls_get_addr calls
8590 without TLSGD/TLSLD marker relocs, and we haven't
8591 found a new-style __tls_get_addr call with a
8592 marker for this symbol, then we either have a
8593 broken object file or an -mlongcall style
8594 indirect call to __tls_get_addr without a marker.
8595 Disable optimization in this case. */
8596 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8597 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8598 && !sec->nomark_tls_get_addr
37da22e5
AM
8599 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8600 != (TLS_TLS | TLS_MARK)))
8601 continue;
8602
7d04a20a 8603 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8604 {
23cedd1d
AM
8605 struct plt_entry *ent = NULL;
8606
9e7028aa
AM
8607 if (htab->tls_get_addr_fd != NULL)
8608 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8609 ent != NULL;
8610 ent = ent->next)
8611 if (ent->addend == 0)
8612 break;
8613
8614 if (ent == NULL && htab->tga_desc_fd != NULL)
8615 for (ent = htab->tga_desc_fd->elf.plt.plist;
8616 ent != NULL;
8617 ent = ent->next)
8618 if (ent->addend == 0)
8619 break;
8620
8621 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8622 for (ent = htab->tls_get_addr->elf.plt.plist;
8623 ent != NULL;
8624 ent = ent->next)
8625 if (ent->addend == 0)
102890f0 8626 break;
411e1bfb 8627
9e7028aa
AM
8628 if (ent == NULL && htab->tga_desc != NULL)
8629 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8630 ent != NULL;
8631 ent = ent->next)
8632 if (ent->addend == 0)
102890f0 8633 break;
23cedd1d
AM
8634
8635 if (ent != NULL
8636 && ent->plt.refcount > 0)
8637 ent->plt.refcount -= 1;
102890f0 8638 }
411e1bfb 8639
102890f0 8640 if (tls_clear == 0)
30038c59
AM
8641 continue;
8642
102890f0
AM
8643 if ((tls_set & TLS_EXPLICIT) == 0)
8644 {
8645 struct got_entry *ent;
411e1bfb 8646
102890f0
AM
8647 /* Adjust got entry for this reloc. */
8648 if (h != NULL)
8649 ent = h->got.glist;
8650 else
8651 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8652
102890f0
AM
8653 for (; ent != NULL; ent = ent->next)
8654 if (ent->addend == rel->r_addend
8655 && ent->owner == ibfd
8656 && ent->tls_type == tls_type)
8657 break;
8658 if (ent == NULL)
8659 abort ();
411e1bfb 8660
102890f0
AM
8661 if (tls_set == 0)
8662 {
8663 /* We managed to get rid of a got entry. */
8664 if (ent->got.refcount > 0)
8665 ent->got.refcount -= 1;
8666 }
8667 }
8668 else
8669 {
8670 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8671 we'll lose one or two dyn relocs. */
1657026c 8672 if (!dec_dynrel_count (rel, sec, info,
19e08130 8673 NULL, h, sym))
0a1b45a2 8674 return false;
411e1bfb 8675
102890f0
AM
8676 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8677 {
1657026c 8678 if (!dec_dynrel_count (rel + 1, sec, info,
19e08130 8679 NULL, h, sym))
0a1b45a2 8680 return false;
102890f0
AM
8681 }
8682 }
411e1bfb 8683
46e9995a 8684 *tls_mask |= tls_set & 0xff;
102890f0
AM
8685 *tls_mask &= ~tls_clear;
8686 }
8c1d1bb8 8687
102890f0
AM
8688 if (elf_section_data (sec)->relocs != relstart)
8689 free (relstart);
8690 }
411e1bfb 8691
663a1470
AM
8692 if (locsyms != NULL
8693 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8694 {
8695 if (!info->keep_memory)
8696 free (locsyms);
8697 else
8698 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8699 }
8700 }
411e1bfb 8701
c9594989 8702 free (toc_ref);
0a1b45a2 8703 return true;
1e2f5b6e 8704}
b34976b6 8705
c5614fa4
AM
8706/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8707 the values of any global symbols in a toc section that has been
8708 edited. Globals in toc sections should be a rarity, so this function
8709 sets a flag if any are found in toc sections other than the one just
de194d85 8710 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8711
8712struct adjust_toc_info
8713{
8714 asection *toc;
8715 unsigned long *skip;
0a1b45a2 8716 bool global_toc_syms;
c5614fa4
AM
8717};
8718
ba761f19
AM
8719enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8720
0a1b45a2 8721static bool
c5614fa4
AM
8722adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8723{
8724 struct ppc_link_hash_entry *eh;
8725 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8726 unsigned long i;
c5614fa4 8727
c5614fa4
AM
8728 if (h->root.type != bfd_link_hash_defined
8729 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 8730 return true;
c5614fa4 8731
ed7007c1 8732 eh = ppc_elf_hash_entry (h);
c5614fa4 8733 if (eh->adjust_done)
0a1b45a2 8734 return true;
c5614fa4
AM
8735
8736 if (eh->elf.root.u.def.section == toc_inf->toc)
8737 {
854b41e7
AM
8738 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8739 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8740 else
854b41e7
AM
8741 i = eh->elf.root.u.def.value >> 3;
8742
ba761f19 8743 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8744 {
4eca0228 8745 _bfd_error_handler
854b41e7
AM
8746 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8747 do
8748 ++i;
ba761f19 8749 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8750 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8751 }
854b41e7
AM
8752
8753 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8754 eh->adjust_done = 1;
8755 }
8756 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
0a1b45a2 8757 toc_inf->global_toc_syms = true;
c5614fa4 8758
0a1b45a2 8759 return true;
c5614fa4
AM
8760}
8761
39eeab25
AM
8762/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8763 on a _LO variety toc/got reloc. */
560c8763 8764
0a1b45a2 8765static bool
39eeab25 8766ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8767{
2365f8d7
AM
8768 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8769 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8770 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8771 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8772 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8773 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8774 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8775 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8776 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8777 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8778 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8779 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8780 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8781 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8782 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8783 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8784 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8785 /* Exclude lfqu by testing reloc. If relocs are ever
8786 defined for the reduced D field in psq_lu then those
8787 will need testing too. */
8788 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8789 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8790 && (insn & 1) == 0)
2365f8d7
AM
8791 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8792 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8793 /* Exclude stfqu. psq_stu as above for psq_lu. */
8794 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8795 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8796 && (insn & 1) == 0));
560c8763
AM
8797}
8798
4a421c53
AM
8799/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8800 pld ra,symbol@got@pcrel
dd9b12c2 8801 load/store rt,off(ra)
4a421c53 8802 or
d4b87b1e 8803 pla ra,symbol@pcrel
dd9b12c2 8804 load/store rt,off(ra)
4a421c53 8805 may be translated to
dd9b12c2 8806 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8807 nop.
8808 This function returns true if the optimization is possible, placing
dd9b12c2 8809 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8810
8811 On entry to this function, the linker has already determined that
d4b87b1e 8812 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8813 while *PINSN2 is the second instruction. */
8814
0a1b45a2 8815static bool
dd9b12c2 8816xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8817{
77486630
AM
8818 uint64_t insn1 = *pinsn1;
8819 uint64_t insn2 = *pinsn2;
dd9b12c2 8820 bfd_signed_vma off;
4a421c53 8821
77486630
AM
8822 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8823 {
8824 /* Check that regs match. */
8825 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8826 return false;
77486630
AM
8827
8828 /* P8LS or PMLS form, non-pcrel. */
8829 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
0a1b45a2 8830 return false;
77486630
AM
8831
8832 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8833 *pinsn2 = PNOP;
8834 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8835 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
0a1b45a2 8836 return true;
77486630
AM
8837 }
8838
8839 insn2 >>= 32;
8840
4a421c53 8841 /* Check that regs match. */
77486630 8842 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8843 return false;
4a421c53
AM
8844
8845 switch ((insn2 >> 26) & 63)
8846 {
8847 default:
0a1b45a2 8848 return false;
4a421c53
AM
8849
8850 case 32: /* lwz */
8851 case 34: /* lbz */
8852 case 36: /* stw */
8853 case 38: /* stb */
8854 case 40: /* lhz */
8855 case 42: /* lha */
8856 case 44: /* sth */
8857 case 48: /* lfs */
8858 case 50: /* lfd */
8859 case 52: /* stfs */
8860 case 54: /* stfd */
8861 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8862 on the insn. */
77486630 8863 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8864 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8865 off = insn2 & 0xffff;
4a421c53
AM
8866 break;
8867
8868 case 58: /* lwa, ld */
dd9b12c2 8869 if ((insn2 & 1) != 0)
0a1b45a2 8870 return false;
77486630 8871 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8872 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8873 | (insn2 & (31ULL << 21)));
dd9b12c2 8874 off = insn2 & 0xfffc;
4a421c53
AM
8875 break;
8876
8877 case 57: /* lxsd, lxssp */
dd9b12c2 8878 if ((insn2 & 3) < 2)
0a1b45a2 8879 return false;
77486630 8880 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8881 | ((40ULL | (insn2 & 3)) << 26)
8882 | (insn2 & (31ULL << 21)));
dd9b12c2 8883 off = insn2 & 0xfffc;
4a421c53
AM
8884 break;
8885
8886 case 61: /* stxsd, stxssp, lxv, stxv */
8887 if ((insn2 & 3) == 0)
0a1b45a2 8888 return false;
4a421c53
AM
8889 else if ((insn2 & 3) >= 2)
8890 {
77486630 8891 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8892 | ((44ULL | (insn2 & 3)) << 26)
8893 | (insn2 & (31ULL << 21)));
dd9b12c2 8894 off = insn2 & 0xfffc;
4a421c53
AM
8895 }
8896 else
8897 {
77486630 8898 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8899 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8900 | (insn2 & (31ULL << 21)));
dd9b12c2 8901 off = insn2 & 0xfff0;
4a421c53
AM
8902 }
8903 break;
8904
8905 case 56: /* lq */
77486630 8906 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8907 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8908 off = insn2 & 0xffff;
4a421c53
AM
8909 break;
8910
94ba9882
AM
8911 case 6: /* lxvp, stxvp */
8912 if ((insn2 & 0xe) != 0)
0a1b45a2 8913 return false;
94ba9882
AM
8914 insn1 = ((1ULL << 58) | (1ULL << 52)
8915 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8916 | (insn2 & (31ULL << 21)));
8917 off = insn2 & 0xfff0;
8918 break;
8919
4a421c53 8920 case 62: /* std, stq */
dd9b12c2 8921 if ((insn2 & 1) != 0)
0a1b45a2 8922 return false;
77486630 8923 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8924 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8925 | (insn2 & (31ULL << 21)));
dd9b12c2 8926 off = insn2 & 0xfffc;
4a421c53
AM
8927 break;
8928 }
8929
77486630 8930 *pinsn1 = insn1;
4a421c53 8931 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8932 *poff = (off ^ 0x8000) - 0x8000;
0a1b45a2 8933 return true;
4a421c53
AM
8934}
8935
c5614fa4
AM
8936/* Examine all relocs referencing .toc sections in order to remove
8937 unused .toc entries. */
8938
0a1b45a2 8939bool
33c0ec9d 8940ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8941{
8942 bfd *ibfd;
8943 struct adjust_toc_info toc_inf;
67f0cbdb 8944 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8945
67f0cbdb 8946 htab->do_toc_opt = 1;
0a1b45a2 8947 toc_inf.global_toc_syms = true;
c72f2fb2 8948 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8949 {
8950 asection *toc, *sec;
8951 Elf_Internal_Shdr *symtab_hdr;
8952 Elf_Internal_Sym *local_syms;
425b145b 8953 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8954 unsigned long *skip, *drop;
8955 unsigned char *used;
8956 unsigned char *keep, last, some_unused;
8957
854b41e7
AM
8958 if (!is_ppc64_elf (ibfd))
8959 continue;
8960
c5614fa4
AM
8961 toc = bfd_get_section_by_name (ibfd, ".toc");
8962 if (toc == NULL
92b7a70f 8963 || toc->size == 0
81ff113f 8964 || (toc->flags & SEC_HAS_CONTENTS) == 0
dbaa2011
AM
8965 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8966 || discarded_section (toc))
c5614fa4
AM
8967 continue;
8968
425b145b 8969 toc_relocs = NULL;
c5614fa4 8970 local_syms = NULL;
0ffa91dd 8971 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8972
8973 /* Look at sections dropped from the final link. */
8974 skip = NULL;
8975 relstart = NULL;
8976 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8977 {
8978 if (sec->reloc_count == 0
dbaa2011 8979 || !discarded_section (sec)
c5614fa4
AM
8980 || get_opd_info (sec)
8981 || (sec->flags & SEC_ALLOC) == 0
8982 || (sec->flags & SEC_DEBUGGING) != 0)
8983 continue;
8984
0a1b45a2 8985 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
c5614fa4
AM
8986 if (relstart == NULL)
8987 goto error_ret;
8988
8989 /* Run through the relocs to see which toc entries might be
8990 unused. */
8991 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8992 {
8993 enum elf_ppc64_reloc_type r_type;
8994 unsigned long r_symndx;
8995 asection *sym_sec;
8996 struct elf_link_hash_entry *h;
8997 Elf_Internal_Sym *sym;
8998 bfd_vma val;
8999
9000 r_type = ELF64_R_TYPE (rel->r_info);
9001 switch (r_type)
9002 {
9003 default:
9004 continue;
9005
9006 case R_PPC64_TOC16:
9007 case R_PPC64_TOC16_LO:
9008 case R_PPC64_TOC16_HI:
9009 case R_PPC64_TOC16_HA:
9010 case R_PPC64_TOC16_DS:
9011 case R_PPC64_TOC16_LO_DS:
9012 break;
9013 }
9014
9015 r_symndx = ELF64_R_SYM (rel->r_info);
9016 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9017 r_symndx, ibfd))
9018 goto error_ret;
9019
9020 if (sym_sec != toc)
9021 continue;
9022
9023 if (h != NULL)
9024 val = h->root.u.def.value;
9025 else
9026 val = sym->st_value;
9027 val += rel->r_addend;
9028
9029 if (val >= toc->size)
9030 continue;
9031
9032 /* Anything in the toc ought to be aligned to 8 bytes.
9033 If not, don't mark as unused. */
9034 if (val & 7)
9035 continue;
9036
9037 if (skip == NULL)
9038 {
854b41e7 9039 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9040 if (skip == NULL)
9041 goto error_ret;
9042 }
9043
ba761f19 9044 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9045 }
9046
9047 if (elf_section_data (sec)->relocs != relstart)
9048 free (relstart);
9049 }
9050
ba761f19
AM
9051 /* For largetoc loads of address constants, we can convert
9052 . addis rx,2,addr@got@ha
9053 . ld ry,addr@got@l(rx)
9054 to
9055 . addis rx,2,addr@toc@ha
9056 . addi ry,rx,addr@toc@l
9057 when addr is within 2G of the toc pointer. This then means
9058 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9059
ba761f19
AM
9060 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9061 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9062 && toc->reloc_count != 0)
9063 {
9064 /* Read toc relocs. */
425b145b
AM
9065 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9066 info->keep_memory);
9067 if (toc_relocs == NULL)
ba761f19
AM
9068 goto error_ret;
9069
425b145b 9070 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9071 {
9072 enum elf_ppc64_reloc_type r_type;
9073 unsigned long r_symndx;
9074 asection *sym_sec;
9075 struct elf_link_hash_entry *h;
9076 Elf_Internal_Sym *sym;
9077 bfd_vma val, addr;
9078
9079 r_type = ELF64_R_TYPE (rel->r_info);
9080 if (r_type != R_PPC64_ADDR64)
9081 continue;
9082
9083 r_symndx = ELF64_R_SYM (rel->r_info);
9084 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9085 r_symndx, ibfd))
9086 goto error_ret;
9087
425b145b 9088 if (sym_sec == NULL
c27b8c2a 9089 || sym_sec->output_section == NULL
dbaa2011 9090 || discarded_section (sym_sec))
425b145b
AM
9091 continue;
9092
3a3a4c1f
AM
9093 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9094 || (bfd_link_pic (info)
9095 && sym_sec == bfd_abs_section_ptr))
ba761f19
AM
9096 continue;
9097
9098 if (h != NULL)
bddc25c9
AM
9099 {
9100 if (h->type == STT_GNU_IFUNC)
9101 continue;
9102 val = h->root.u.def.value;
9103 }
ba761f19 9104 else
bddc25c9
AM
9105 {
9106 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9107 continue;
9108 val = sym->st_value;
9109 }
ba761f19
AM
9110 val += rel->r_addend;
9111 val += sym_sec->output_section->vma + sym_sec->output_offset;
9112
9113 /* We don't yet know the exact toc pointer value, but we
9114 know it will be somewhere in the toc section. Don't
9115 optimize if the difference from any possible toc
9116 pointer is outside [ff..f80008000, 7fff7fff]. */
9117 addr = toc->output_section->vma + TOC_BASE_OFF;
9118 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9119 continue;
9120
9121 addr = toc->output_section->vma + toc->output_section->rawsize;
9122 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9123 continue;
9124
9125 if (skip == NULL)
9126 {
9127 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9128 if (skip == NULL)
9129 goto error_ret;
9130 }
9131
9132 skip[rel->r_offset >> 3]
425b145b 9133 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9134 }
ba761f19
AM
9135 }
9136
c5614fa4
AM
9137 if (skip == NULL)
9138 continue;
9139
9140 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9141 if (used == NULL)
9142 {
9143 error_ret:
c9594989 9144 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
9145 free (local_syms);
9146 if (sec != NULL
c5614fa4
AM
9147 && elf_section_data (sec)->relocs != relstart)
9148 free (relstart);
c9594989 9149 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9150 free (toc_relocs);
c9594989 9151 free (skip);
0a1b45a2 9152 return false;
c5614fa4
AM
9153 }
9154
30038c59
AM
9155 /* Now check all kept sections that might reference the toc.
9156 Check the toc itself last. */
9157 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9158 : ibfd->sections);
c5614fa4 9159 sec != NULL;
c5614fa4 9160 sec = (sec == toc ? NULL
c5614fa4 9161 : sec->next == NULL ? toc
30038c59 9162 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9163 : sec->next))
9164 {
9165 int repeat;
9166
9167 if (sec->reloc_count == 0
dbaa2011 9168 || discarded_section (sec)
c5614fa4
AM
9169 || get_opd_info (sec)
9170 || (sec->flags & SEC_ALLOC) == 0
9171 || (sec->flags & SEC_DEBUGGING) != 0)
9172 continue;
9173
854b41e7
AM
9174 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9175 info->keep_memory);
c5614fa4 9176 if (relstart == NULL)
2915c55b
JK
9177 {
9178 free (used);
9179 goto error_ret;
9180 }
c5614fa4
AM
9181
9182 /* Mark toc entries referenced as used. */
c5614fa4 9183 do
d4f1ee75
AM
9184 {
9185 repeat = 0;
9186 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9187 {
9188 enum elf_ppc64_reloc_type r_type;
9189 unsigned long r_symndx;
9190 asection *sym_sec;
9191 struct elf_link_hash_entry *h;
9192 Elf_Internal_Sym *sym;
9193 bfd_vma val;
98528052 9194
d4f1ee75 9195 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
9196 switch (r_type)
9197 {
9198 case R_PPC64_TOC16:
9199 case R_PPC64_TOC16_LO:
9200 case R_PPC64_TOC16_HI:
9201 case R_PPC64_TOC16_HA:
9202 case R_PPC64_TOC16_DS:
9203 case R_PPC64_TOC16_LO_DS:
9204 /* In case we're taking addresses of toc entries. */
9205 case R_PPC64_ADDR64:
9206 break;
c5614fa4 9207
d4f1ee75
AM
9208 default:
9209 continue;
9210 }
c5614fa4 9211
d4f1ee75
AM
9212 r_symndx = ELF64_R_SYM (rel->r_info);
9213 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9214 r_symndx, ibfd))
9215 {
9216 free (used);
9217 goto error_ret;
9218 }
c5614fa4 9219
d4f1ee75
AM
9220 if (sym_sec != toc)
9221 continue;
c5614fa4 9222
d4f1ee75
AM
9223 if (h != NULL)
9224 val = h->root.u.def.value;
9225 else
9226 val = sym->st_value;
9227 val += rel->r_addend;
ba761f19 9228
d4f1ee75
AM
9229 if (val >= toc->size)
9230 continue;
ba761f19 9231
d4f1ee75
AM
9232 if ((skip[val >> 3] & can_optimize) != 0)
9233 {
9234 bfd_vma off;
9235 unsigned char opc;
9236
9237 switch (r_type)
9238 {
9239 case R_PPC64_TOC16_HA:
ba761f19 9240 break;
ba761f19 9241
d4f1ee75
AM
9242 case R_PPC64_TOC16_LO_DS:
9243 off = rel->r_offset;
9244 off += (bfd_big_endian (ibfd) ? -2 : 3);
9245 if (!bfd_get_section_contents (ibfd, sec, &opc,
9246 off, 1))
9247 {
9248 free (used);
9249 goto error_ret;
9250 }
9251 if ((opc & (0x3f << 2)) == (58u << 2))
9252 break;
1a0670f3 9253 /* Fall through. */
ba761f19 9254
d4f1ee75
AM
9255 default:
9256 /* Wrong sort of reloc, or not a ld. We may
9257 as well clear ref_from_discarded too. */
9258 skip[val >> 3] = 0;
9259 }
9260 }
9261
9262 if (sec != toc)
9263 used[val >> 3] = 1;
9264 /* For the toc section, we only mark as used if this
9265 entry itself isn't unused. */
9266 else if ((used[rel->r_offset >> 3]
9267 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9268 && !used[val >> 3])
9269 {
9270 /* Do all the relocs again, to catch reference
9271 chains. */
9272 repeat = 1;
9273 used[val >> 3] = 1;
9274 }
9275 }
9276 }
c5614fa4 9277 while (repeat);
854b41e7
AM
9278
9279 if (elf_section_data (sec)->relocs != relstart)
9280 free (relstart);
c5614fa4
AM
9281 }
9282
9283 /* Merge the used and skip arrays. Assume that TOC
9284 doublewords not appearing as either used or unused belong
de194d85 9285 to an entry more than one doubleword in size. */
c5614fa4
AM
9286 for (drop = skip, keep = used, last = 0, some_unused = 0;
9287 drop < skip + (toc->size + 7) / 8;
9288 ++drop, ++keep)
9289 {
9290 if (*keep)
9291 {
ba761f19
AM
9292 *drop &= ~ref_from_discarded;
9293 if ((*drop & can_optimize) != 0)
9294 some_unused = 1;
c5614fa4
AM
9295 last = 0;
9296 }
b140b010 9297 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9298 {
9299 some_unused = 1;
ba761f19 9300 last = ref_from_discarded;
c5614fa4
AM
9301 }
9302 else
9303 *drop = last;
9304 }
9305
9306 free (used);
9307
9308 if (some_unused)
9309 {
9310 bfd_byte *contents, *src;
9311 unsigned long off;
d62b3684 9312 Elf_Internal_Sym *sym;
0a1b45a2 9313 bool local_toc_syms = false;
c5614fa4
AM
9314
9315 /* Shuffle the toc contents, and at the same time convert the
9316 skip array from booleans into offsets. */
9317 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9318 goto error_ret;
9319
9320 elf_section_data (toc)->this_hdr.contents = contents;
9321
9322 for (src = contents, off = 0, drop = skip;
9323 src < contents + toc->size;
9324 src += 8, ++drop)
9325 {
ba761f19
AM
9326 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9327 off += 8;
c5614fa4
AM
9328 else if (off != 0)
9329 {
9330 *drop = off;
9331 memcpy (src - off, src, 8);
9332 }
9333 }
854b41e7 9334 *drop = off;
c5614fa4
AM
9335 toc->rawsize = toc->size;
9336 toc->size = src - contents - off;
9337
ba761f19
AM
9338 /* Adjust addends for relocs against the toc section sym,
9339 and optimize any accesses we can. */
c5614fa4
AM
9340 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9341 {
9342 if (sec->reloc_count == 0
dbaa2011 9343 || discarded_section (sec))
c5614fa4
AM
9344 continue;
9345
9346 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9347 info->keep_memory);
c5614fa4
AM
9348 if (relstart == NULL)
9349 goto error_ret;
9350
9351 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9352 {
9353 enum elf_ppc64_reloc_type r_type;
9354 unsigned long r_symndx;
9355 asection *sym_sec;
9356 struct elf_link_hash_entry *h;
854b41e7 9357 bfd_vma val;
c5614fa4
AM
9358
9359 r_type = ELF64_R_TYPE (rel->r_info);
9360 switch (r_type)
9361 {
9362 default:
9363 continue;
9364
9365 case R_PPC64_TOC16:
9366 case R_PPC64_TOC16_LO:
9367 case R_PPC64_TOC16_HI:
9368 case R_PPC64_TOC16_HA:
9369 case R_PPC64_TOC16_DS:
9370 case R_PPC64_TOC16_LO_DS:
9371 case R_PPC64_ADDR64:
9372 break;
9373 }
9374
9375 r_symndx = ELF64_R_SYM (rel->r_info);
9376 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9377 r_symndx, ibfd))
9378 goto error_ret;
9379
ba761f19 9380 if (sym_sec != toc)
c5614fa4
AM
9381 continue;
9382
ba761f19
AM
9383 if (h != NULL)
9384 val = h->root.u.def.value;
9385 else
9386 {
9387 val = sym->st_value;
9388 if (val != 0)
0a1b45a2 9389 local_toc_syms = true;
ba761f19
AM
9390 }
9391
9392 val += rel->r_addend;
854b41e7
AM
9393
9394 if (val > toc->rawsize)
9395 val = toc->rawsize;
ba761f19
AM
9396 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9397 continue;
9398 else if ((skip[val >> 3] & can_optimize) != 0)
9399 {
9400 Elf_Internal_Rela *tocrel
425b145b 9401 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9402 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9403
9404 switch (r_type)
9405 {
9406 case R_PPC64_TOC16_HA:
9407 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9408 break;
9409
9410 case R_PPC64_TOC16_LO_DS:
9411 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9412 break;
9413
9414 default:
28942f62
AM
9415 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9416 ppc_howto_init ();
b140b010 9417 info->callbacks->einfo
695344c0 9418 /* xgettext:c-format */
174d0a74 9419 (_("%H: %s references "
b140b010
AM
9420 "optimized away TOC entry\n"),
9421 ibfd, sec, rel->r_offset,
9422 ppc64_elf_howto_table[r_type]->name);
9423 bfd_set_error (bfd_error_bad_value);
9424 goto error_ret;
ba761f19
AM
9425 }
9426 rel->r_addend = tocrel->r_addend;
9427 elf_section_data (sec)->relocs = relstart;
9428 continue;
9429 }
9430
9431 if (h != NULL || sym->st_value != 0)
9432 continue;
854b41e7
AM
9433
9434 rel->r_addend -= skip[val >> 3];
9435 elf_section_data (sec)->relocs = relstart;
c5614fa4 9436 }
854b41e7
AM
9437
9438 if (elf_section_data (sec)->relocs != relstart)
9439 free (relstart);
c5614fa4
AM
9440 }
9441
9442 /* We shouldn't have local or global symbols defined in the TOC,
9443 but handle them anyway. */
df22d223
AM
9444 if (local_syms != NULL)
9445 for (sym = local_syms;
9446 sym < local_syms + symtab_hdr->sh_info;
9447 ++sym)
9448 if (sym->st_value != 0
9449 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9450 {
9451 unsigned long i;
854b41e7 9452
df22d223
AM
9453 if (sym->st_value > toc->rawsize)
9454 i = toc->rawsize >> 3;
9455 else
9456 i = sym->st_value >> 3;
854b41e7 9457
df22d223
AM
9458 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9459 {
9460 if (local_toc_syms)
4eca0228 9461 _bfd_error_handler
df22d223
AM
9462 (_("%s defined on removed toc entry"),
9463 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9464 do
9465 ++i;
9466 while ((skip[i] & (ref_from_discarded | can_optimize)));
9467 sym->st_value = (bfd_vma) i << 3;
9468 }
d62b3684 9469
df22d223
AM
9470 sym->st_value -= skip[i];
9471 symtab_hdr->contents = (unsigned char *) local_syms;
9472 }
c5614fa4 9473
854b41e7 9474 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9475 if (toc_inf.global_toc_syms)
9476 {
9477 toc_inf.toc = toc;
9478 toc_inf.skip = skip;
0a1b45a2 9479 toc_inf.global_toc_syms = false;
c5614fa4
AM
9480 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9481 &toc_inf);
9482 }
854b41e7
AM
9483
9484 if (toc->reloc_count != 0)
9485 {
d4730f92 9486 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9487 Elf_Internal_Rela *wrel;
9488 bfd_size_type sz;
9489
854b41e7 9490 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9491 if (toc_relocs == NULL)
9492 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9493 info->keep_memory);
9494 if (toc_relocs == NULL)
9495 goto error_ret;
9496
425b145b
AM
9497 wrel = toc_relocs;
9498 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9499 if ((skip[rel->r_offset >> 3]
9500 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9501 {
9502 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9503 wrel->r_info = rel->r_info;
9504 wrel->r_addend = rel->r_addend;
9505 ++wrel;
9506 }
1657026c 9507 else if (!dec_dynrel_count (rel, toc, info,
854b41e7
AM
9508 &local_syms, NULL, NULL))
9509 goto error_ret;
9510
425b145b
AM
9511 elf_section_data (toc)->relocs = toc_relocs;
9512 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9513 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9514 sz = rel_hdr->sh_entsize;
9515 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9516 }
c5614fa4 9517 }
c9594989 9518 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9519 free (toc_relocs);
c5614fa4
AM
9520
9521 if (local_syms != NULL
9522 && symtab_hdr->contents != (unsigned char *) local_syms)
9523 {
9524 if (!info->keep_memory)
9525 free (local_syms);
9526 else
9527 symtab_hdr->contents = (unsigned char *) local_syms;
9528 }
9529 free (skip);
9530 }
9531
066f4018 9532 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9533 a GOT relative or PC relative access, possibly reducing the
9534 number of GOT entries. */
066f4018
AM
9535 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9536 {
9537 asection *sec;
9538 Elf_Internal_Shdr *symtab_hdr;
9539 Elf_Internal_Sym *local_syms;
9540 Elf_Internal_Rela *relstart, *rel;
9541 bfd_vma got;
9542
9543 if (!is_ppc64_elf (ibfd))
9544 continue;
9545
903b777d 9546 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9547 continue;
9548
9549 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9550 got = 0;
9551 if (sec != NULL)
9552 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9553
9554 local_syms = NULL;
9555 symtab_hdr = &elf_symtab_hdr (ibfd);
9556
9557 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9558 {
9559 if (sec->reloc_count == 0
903b777d 9560 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9561 || discarded_section (sec))
9562 continue;
9563
9564 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9565 info->keep_memory);
9566 if (relstart == NULL)
9567 {
9568 got_error_ret:
c9594989 9569 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9570 free (local_syms);
9571 if (sec != NULL
066f4018
AM
9572 && elf_section_data (sec)->relocs != relstart)
9573 free (relstart);
0a1b45a2 9574 return false;
066f4018
AM
9575 }
9576
9577 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9578 {
9579 enum elf_ppc64_reloc_type r_type;
9580 unsigned long r_symndx;
9581 Elf_Internal_Sym *sym;
9582 asection *sym_sec;
9583 struct elf_link_hash_entry *h;
9584 struct got_entry *ent;
133a1f60 9585 bfd_vma val, pc;
4a421c53 9586 unsigned char buf[8];
066f4018 9587 unsigned int insn;
903b777d 9588 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9589
9590 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9591 switch (r_type)
9592 {
9593 default:
9594 insn_check = no_check;
9595 break;
9596
9597 case R_PPC64_PLT16_HA:
9598 case R_PPC64_GOT_TLSLD16_HA:
9599 case R_PPC64_GOT_TLSGD16_HA:
9600 case R_PPC64_GOT_TPREL16_HA:
9601 case R_PPC64_GOT_DTPREL16_HA:
9602 case R_PPC64_GOT16_HA:
9603 case R_PPC64_TOC16_HA:
9604 insn_check = check_ha;
9605 break;
9606
9607 case R_PPC64_PLT16_LO:
9608 case R_PPC64_PLT16_LO_DS:
9609 case R_PPC64_GOT_TLSLD16_LO:
9610 case R_PPC64_GOT_TLSGD16_LO:
9611 case R_PPC64_GOT_TPREL16_LO_DS:
9612 case R_PPC64_GOT_DTPREL16_LO_DS:
9613 case R_PPC64_GOT16_LO:
9614 case R_PPC64_GOT16_LO_DS:
9615 case R_PPC64_TOC16_LO:
9616 case R_PPC64_TOC16_LO_DS:
9617 insn_check = check_lo;
9618 break;
9619 }
9620
9621 if (insn_check != no_check)
9622 {
9623 bfd_vma off = rel->r_offset & ~3;
9624
9625 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9626 goto got_error_ret;
9627
9628 insn = bfd_get_32 (ibfd, buf);
9629 if (insn_check == check_lo
9630 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9631 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9632 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9633 {
9634 char str[12];
9635
9636 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9637 sprintf (str, "%#08x", insn);
9638 info->callbacks->einfo
9639 /* xgettext:c-format */
9640 (_("%H: got/toc optimization is not supported for"
9641 " %s instruction\n"),
9642 ibfd, sec, rel->r_offset & ~3, str);
9643 continue;
9644 }
9645 }
9646
066f4018
AM
9647 switch (r_type)
9648 {
bb22a418
AM
9649 /* Note that we don't delete GOT entries for
9650 R_PPC64_GOT16_DS since we'd need a lot more
9651 analysis. For starters, the preliminary layout is
9652 before the GOT, PLT, dynamic sections and stubs are
9653 laid out. Then we'd need to allow for changes in
9654 distance between sections caused by alignment. */
066f4018
AM
9655 default:
9656 continue;
9657
066f4018
AM
9658 case R_PPC64_GOT16_HA:
9659 case R_PPC64_GOT16_LO_DS:
4a421c53 9660 case R_PPC64_GOT_PCREL34:
066f4018
AM
9661 break;
9662 }
9663
9664 r_symndx = ELF64_R_SYM (rel->r_info);
9665 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9666 r_symndx, ibfd))
9667 goto got_error_ret;
9668
6d5554a6
AM
9669 if (sym_sec == NULL
9670 || sym_sec->output_section == NULL
9671 || discarded_section (sym_sec))
9672 continue;
9673
06507dab
AM
9674 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9675 continue;
9676
3a3a4c1f
AM
9677 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9678 || (bfd_link_pic (info)
9679 && sym_sec == bfd_abs_section_ptr))
066f4018
AM
9680 continue;
9681
9682 if (h != NULL)
9683 val = h->root.u.def.value;
9684 else
9685 val = sym->st_value;
133a1f60 9686 val += rel->r_addend;
066f4018
AM
9687 val += sym_sec->output_section->vma + sym_sec->output_offset;
9688
bb22a418
AM
9689/* Fudge factor to allow for the fact that the preliminary layout
9690 isn't exact. Reduce limits by this factor. */
9691#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9692
066f4018
AM
9693 switch (r_type)
9694 {
9695 default:
9696 continue;
9697
066f4018 9698 case R_PPC64_GOT16_HA:
bb22a418
AM
9699 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9700 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9701 continue;
9702
9703 if (!bfd_get_section_contents (ibfd, sec, buf,
9704 rel->r_offset & ~3, 4))
9705 goto got_error_ret;
9706 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9707 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9708 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9709 continue;
9710 break;
9711
9712 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9713 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9714 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9715 continue;
9716 if (!bfd_get_section_contents (ibfd, sec, buf,
9717 rel->r_offset & ~3, 4))
9718 goto got_error_ret;
9719 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9720 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9721 continue;
9722 break;
4a421c53
AM
9723
9724 case R_PPC64_GOT_PCREL34:
9725 pc = rel->r_offset;
9726 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9727 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9728 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9729 continue;
9730 if (!bfd_get_section_contents (ibfd, sec, buf,
9731 rel->r_offset & ~3, 8))
9732 goto got_error_ret;
9733 insn = bfd_get_32 (ibfd, buf);
9734 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9735 continue;
9736 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9737 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9738 continue;
9739 break;
066f4018 9740 }
bb22a418 9741#undef LIMIT_ADJUST
066f4018
AM
9742
9743 if (h != NULL)
9744 ent = h->got.glist;
9745 else
9746 {
9747 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9748 ent = local_got_ents[r_symndx];
9749 }
9750 for (; ent != NULL; ent = ent->next)
133a1f60 9751 if (ent->addend == rel->r_addend
066f4018
AM
9752 && ent->owner == ibfd
9753 && ent->tls_type == 0)
9754 break;
9755 BFD_ASSERT (ent && ent->got.refcount > 0);
9756 ent->got.refcount -= 1;
9757 }
9758
9759 if (elf_section_data (sec)->relocs != relstart)
9760 free (relstart);
9761 }
9762
9763 if (local_syms != NULL
9764 && symtab_hdr->contents != (unsigned char *) local_syms)
9765 {
9766 if (!info->keep_memory)
9767 free (local_syms);
9768 else
9769 symtab_hdr->contents = (unsigned char *) local_syms;
9770 }
9771 }
9772
0a1b45a2 9773 return true;
c5614fa4
AM
9774}
9775
1bbe0902
AM
9776/* Return true iff input section I references the TOC using
9777 instructions limited to +/-32k offsets. */
9778
0a1b45a2 9779bool
1bbe0902
AM
9780ppc64_elf_has_small_toc_reloc (asection *i)
9781{
9782 return (is_ppc64_elf (i->owner)
9783 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9784}
9785
927be08e
AM
9786/* Allocate space for one GOT entry. */
9787
9788static void
9789allocate_got (struct elf_link_hash_entry *h,
9790 struct bfd_link_info *info,
9791 struct got_entry *gent)
9792{
9793 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9794 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9795 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9796 ? 16 : 8);
9797 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9798 ? 2 : 1) * sizeof (Elf64_External_Rela);
9799 asection *got = ppc64_elf_tdata (gent->owner)->got;
9800
9801 gent->got.offset = got->size;
9802 got->size += entsize;
9803
19e08130 9804 if (h->type == STT_GNU_IFUNC)
927be08e 9805 {
33e44f2e 9806 htab->elf.irelplt->size += rentsize;
19e08130 9807 htab->got_reli_size += rentsize;
927be08e 9808 }
f15d0b54 9809 else if (((bfd_link_pic (info)
1657026c
AM
9810 && (gent->tls_type == 0
9811 ? !info->enable_dt_relr
9812 : !(bfd_link_executable (info)
3a3a4c1f
AM
9813 && SYMBOL_REFERENCES_LOCAL (info, h)))
9814 && !bfd_is_abs_symbol (&h->root))
f0158f44
AM
9815 || (htab->elf.dynamic_sections_created
9816 && h->dynindx != -1
9817 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9818 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9819 {
19e08130 9820 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9821 relgot->size += rentsize;
927be08e
AM
9822 }
9823}
9824
7865406b
AM
9825/* This function merges got entries in the same toc group. */
9826
9827static void
9828merge_got_entries (struct got_entry **pent)
9829{
9830 struct got_entry *ent, *ent2;
9831
9832 for (ent = *pent; ent != NULL; ent = ent->next)
9833 if (!ent->is_indirect)
9834 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9835 if (!ent2->is_indirect
9836 && ent2->addend == ent->addend
9837 && ent2->tls_type == ent->tls_type
9838 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9839 {
0a1b45a2 9840 ent2->is_indirect = true;
7865406b
AM
9841 ent2->got.ent = ent;
9842 }
9843}
9844
46434633 9845/* If H is undefined, make it dynamic if that makes sense. */
f0158f44 9846
0a1b45a2 9847static bool
46434633
AM
9848ensure_undef_dynamic (struct bfd_link_info *info,
9849 struct elf_link_hash_entry *h)
f0158f44
AM
9850{
9851 struct elf_link_hash_table *htab = elf_hash_table (info);
9852
9853 if (htab->dynamic_sections_created
46434633
AM
9854 && ((info->dynamic_undefined_weak != 0
9855 && h->root.type == bfd_link_hash_undefweak)
9856 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9857 && h->dynindx == -1
9858 && !h->forced_local
9859 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9860 return bfd_elf_link_record_dynamic_symbol (info, h);
0a1b45a2 9861 return true;
f0158f44
AM
9862}
9863
30845f11
AM
9864/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9865 usual htab->elf.splt section for a PLT entry. */
9866
9867static inline
0a1b45a2 9868bool use_local_plt (struct bfd_link_info *info,
30845f11
AM
9869 struct elf_link_hash_entry *h)
9870{
9871 return (h == NULL
9872 || h->dynindx == -1
9873 || !elf_hash_table (info)->dynamic_sections_created);
9874}
9875
65f38f15
AM
9876/* Allocate space in .plt, .got and associated reloc sections for
9877 dynamic relocs. */
5bd4f169 9878
0a1b45a2 9879static bool
4ce794b7 9880allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9881{
65f38f15
AM
9882 struct bfd_link_info *info;
9883 struct ppc_link_hash_table *htab;
5bd4f169 9884 asection *s;
65f38f15 9885 struct ppc_link_hash_entry *eh;
0b8bcf0d 9886 struct got_entry **pgent, *gent;
5bd4f169 9887
e92d460e 9888 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9889 return true;
5bd4f169 9890
65f38f15
AM
9891 info = (struct bfd_link_info *) inf;
9892 htab = ppc_hash_table (info);
4dfe6ac6 9893 if (htab == NULL)
0a1b45a2 9894 return false;
5bd4f169 9895
ed7007c1 9896 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9897 /* Run through the TLS GD got entries first if we're changing them
9898 to TPREL. */
b00a0a86 9899 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9900 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9901 if (gent->got.refcount > 0
9902 && (gent->tls_type & TLS_GD) != 0)
9903 {
9904 /* This was a GD entry that has been converted to TPREL. If
9905 there happens to be a TPREL entry we can use that one. */
9906 struct got_entry *ent;
9907 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9908 if (ent->got.refcount > 0
9909 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9910 && ent->addend == gent->addend
9911 && ent->owner == gent->owner)
951fd09b
AM
9912 {
9913 gent->got.refcount = 0;
9914 break;
9915 }
9916
9917 /* If not, then we'll be using our own TPREL entry. */
9918 if (gent->got.refcount != 0)
9919 gent->tls_type = TLS_TLS | TLS_TPREL;
9920 }
9921
7865406b
AM
9922 /* Remove any list entry that won't generate a word in the GOT before
9923 we call merge_got_entries. Otherwise we risk merging to empty
9924 entries. */
0b8bcf0d
AM
9925 pgent = &h->got.glist;
9926 while ((gent = *pgent) != NULL)
411e1bfb 9927 if (gent->got.refcount > 0)
7865406b
AM
9928 {
9929 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9930 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9931 {
9932 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9933 *pgent = gent->next;
9934 }
9935 else
9936 pgent = &gent->next;
9937 }
9938 else
9939 *pgent = gent->next;
9940
9941 if (!htab->do_multi_toc)
9942 merge_got_entries (&h->got.glist);
9943
9944 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9945 if (!gent->is_indirect)
411e1bfb 9946 {
ec73ddcd
AM
9947 /* Ensure we catch all the cases where this symbol should
9948 be made dynamic. */
46434633 9949 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9950 return false;
65f38f15 9951
0c8d6e5c 9952 if (!is_ppc64_elf (gent->owner))
927be08e 9953 abort ();
0ffa91dd 9954
927be08e 9955 allocate_got (h, info, gent);
411e1bfb 9956 }
65f38f15 9957
954b63d4
AM
9958 /* If no dynamic sections we can't have dynamic relocs, except for
9959 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9960 if (!htab->elf.dynamic_sections_created
9961 && h->type != STT_GNU_IFUNC)
190eb1dd 9962 h->dyn_relocs = NULL;
8a2058b5 9963
529fe20e
AM
9964 /* Discard relocs on undefined symbols that must be local. */
9965 else if (h->root.type == bfd_link_hash_undefined
9966 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9967 h->dyn_relocs = NULL;
529fe20e 9968
954b63d4
AM
9969 /* Also discard relocs on undefined weak syms with non-default
9970 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9971 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9972 h->dyn_relocs = NULL;
954b63d4 9973
190eb1dd 9974 if (h->dyn_relocs != NULL)
65f38f15 9975 {
1657026c 9976 struct ppc_dyn_relocs *p, **pp;
8a2058b5 9977
57e7d118
AM
9978 /* In the shared -Bsymbolic case, discard space allocated for
9979 dynamic pc-relative relocs against symbols which turn out to
9980 be defined in regular objects. For the normal shared case,
9981 discard space for relocs that have become local due to symbol
9982 visibility changes. */
57e7d118 9983 if (bfd_link_pic (info))
65f38f15 9984 {
57e7d118
AM
9985 /* Relocs that use pc_count are those that appear on a call
9986 insn, or certain REL relocs (see must_be_dyn_reloc) that
9987 can be generated via assembly. We want calls to
9988 protected symbols to resolve directly to the function
9989 rather than going via the plt. If people want function
9990 pointer comparisons to work as expected then they should
9991 avoid writing weird assembly. */
9992 if (SYMBOL_CALLS_LOCAL (info, h))
9993 {
1657026c
AM
9994 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9995 (p = *pp) != NULL;
9996 )
57e7d118
AM
9997 {
9998 p->count -= p->pc_count;
9999 p->pc_count = 0;
10000 if (p->count == 0)
10001 *pp = p->next;
10002 else
10003 pp = &p->next;
10004 }
10005 }
65f38f15 10006
190eb1dd 10007 if (h->dyn_relocs != NULL)
5bd4f169 10008 {
ec73ddcd
AM
10009 /* Ensure we catch all the cases where this symbol
10010 should be made dynamic. */
46434633 10011 if (!ensure_undef_dynamic (info, h))
0a1b45a2 10012 return false;
5bd4f169 10013 }
65f38f15 10014 }
ec73ddcd
AM
10015
10016 /* For a fixed position executable, discard space for
10017 relocs against symbols which are not dynamic. */
10018 else if (h->type != STT_GNU_IFUNC)
57e7d118 10019 {
49160308
AM
10020 if ((h->dynamic_adjusted
10021 || (h->ref_regular
10022 && h->root.type == bfd_link_hash_undefweak
10023 && (info->dynamic_undefined_weak > 0
10024 || !_bfd_elf_readonly_dynrelocs (h))))
529fe20e
AM
10025 && !h->def_regular
10026 && !ELF_COMMON_DEF_P (h))
f0158f44 10027 {
ec73ddcd
AM
10028 /* Ensure we catch all the cases where this symbol
10029 should be made dynamic. */
46434633 10030 if (!ensure_undef_dynamic (info, h))
0a1b45a2 10031 return false;
dfbb6ac9 10032
ec73ddcd 10033 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 10034 if (h->dynindx == -1)
190eb1dd 10035 h->dyn_relocs = NULL;
f0158f44
AM
10036 }
10037 else
190eb1dd 10038 h->dyn_relocs = NULL;
57e7d118
AM
10039 }
10040
10041 /* Finally, allocate space. */
1657026c 10042 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
365bf300
AM
10043 if (!discarded_section (p->sec))
10044 {
10045 unsigned int count;
10046 asection *sreloc = elf_section_data (p->sec)->sreloc;
10047 if (eh->elf.type == STT_GNU_IFUNC)
10048 sreloc = htab->elf.irelplt;
10049 count = p->count;
10050 if (info->enable_dt_relr
10051 && ((!NO_OPD_RELOCS
10052 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10053 || (eh->elf.type != STT_GNU_IFUNC
10054 && SYMBOL_REFERENCES_LOCAL (info, h))))
10055 count -= p->rel_count;
10056 sreloc->size += count * sizeof (Elf64_External_Rela);
10057 }
65f38f15 10058 }
57e7d118 10059
2d7ad24e
AM
10060 /* We might need a PLT entry when the symbol
10061 a) is dynamic, or
10062 b) is an ifunc, or
10063 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10064 d) has plt16 relocs and we are linking statically. */
10065 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10066 || h->type == STT_GNU_IFUNC
10067 || (h->needs_plt && h->dynamic_adjusted)
10068 || (h->needs_plt
10069 && h->def_regular
10070 && !htab->elf.dynamic_sections_created
3e04d765 10071 && !htab->can_convert_all_inline_plt
ed7007c1 10072 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 10073 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10074 {
57e7d118 10075 struct plt_entry *pent;
0a1b45a2 10076 bool doneone = false;
57e7d118
AM
10077 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10078 if (pent->plt.refcount > 0)
10079 {
b2936612
AM
10080 if (!ensure_undef_dynamic (info, h))
10081 return false;
10082
30845f11 10083 if (use_local_plt (info, h))
57e7d118 10084 {
2d7ad24e
AM
10085 if (h->type == STT_GNU_IFUNC)
10086 {
10087 s = htab->elf.iplt;
10088 pent->plt.offset = s->size;
10089 s->size += PLT_ENTRY_SIZE (htab);
10090 s = htab->elf.irelplt;
10091 }
10092 else
10093 {
10094 s = htab->pltlocal;
10095 pent->plt.offset = s->size;
10096 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
1657026c
AM
10097 s = NULL;
10098 if (bfd_link_pic (info)
10099 && !(info->enable_dt_relr && !htab->opd_abi))
10100 s = htab->relpltlocal;
2d7ad24e 10101 }
57e7d118
AM
10102 }
10103 else
10104 {
10105 /* If this is the first .plt entry, make room for the special
10106 first entry. */
10107 s = htab->elf.splt;
10108 if (s->size == 0)
10109 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10110
57e7d118 10111 pent->plt.offset = s->size;
65f38f15 10112
57e7d118
AM
10113 /* Make room for this entry. */
10114 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10115
57e7d118
AM
10116 /* Make room for the .glink code. */
10117 s = htab->glink;
10118 if (s->size == 0)
9e390558 10119 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10120 if (htab->opd_abi)
10121 {
10122 /* We need bigger stubs past index 32767. */
9e390558 10123 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10124 s->size += 4;
10125 s->size += 2*4;
10126 }
10127 else
10128 s->size += 4;
65f38f15 10129
57e7d118
AM
10130 /* We also need to make an entry in the .rela.plt section. */
10131 s = htab->elf.srelplt;
10132 }
2d7ad24e
AM
10133 if (s != NULL)
10134 s->size += sizeof (Elf64_External_Rela);
0a1b45a2 10135 doneone = true;
57e7d118
AM
10136 }
10137 else
10138 pent->plt.offset = (bfd_vma) -1;
10139 if (!doneone)
10140 {
10141 h->plt.plist = NULL;
10142 h->needs_plt = 0;
10143 }
65f38f15 10144 }
57e7d118 10145 else
65f38f15 10146 {
57e7d118
AM
10147 h->plt.plist = NULL;
10148 h->needs_plt = 0;
65f38f15
AM
10149 }
10150
0a1b45a2 10151 return true;
65f38f15
AM
10152}
10153
9e390558
AM
10154#define PPC_LO(v) ((v) & 0xffff)
10155#define PPC_HI(v) (((v) >> 16) & 0xffff)
10156#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
10157#define D34(v) \
10158 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10159#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 10160
af969b14 10161/* Called via elf_link_hash_traverse from ppc64_elf_late_size_sections
a345bc8d
AM
10162 to set up space for global entry stubs. These are put in glink,
10163 after the branch table. */
65f38f15 10164
0a1b45a2 10165static bool
a345bc8d 10166size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10167{
a345bc8d
AM
10168 struct bfd_link_info *info;
10169 struct ppc_link_hash_table *htab;
10170 struct plt_entry *pent;
9e390558 10171 asection *s, *plt;
65f38f15 10172
a345bc8d 10173 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 10174 return true;
65f38f15 10175
a345bc8d 10176 if (!h->pointer_equality_needed)
0a1b45a2 10177 return true;
65f38f15 10178
a345bc8d 10179 if (h->def_regular)
0a1b45a2 10180 return true;
65f38f15 10181
a345bc8d
AM
10182 info = inf;
10183 htab = ppc_hash_table (info);
10184 if (htab == NULL)
0a1b45a2 10185 return false;
a345bc8d 10186
9e390558
AM
10187 s = htab->global_entry;
10188 plt = htab->elf.splt;
a345bc8d
AM
10189 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10190 if (pent->plt.offset != (bfd_vma) -1
10191 && pent->addend == 0)
10192 {
afe397ea
AM
10193 /* For ELFv2, if this symbol is not defined in a regular file
10194 and we are not generating a shared library or pie, then we
10195 need to define the symbol in the executable on a call stub.
10196 This is to avoid text relocations. */
9e390558
AM
10197 bfd_vma off, stub_align, stub_off, stub_size;
10198 unsigned int align_power;
10199
10200 stub_size = 16;
10201 stub_off = s->size;
10202 if (htab->params->plt_stub_align >= 0)
10203 align_power = htab->params->plt_stub_align;
10204 else
10205 align_power = -htab->params->plt_stub_align;
10206 /* Setting section alignment is delayed until we know it is
10207 non-empty. Otherwise the .text output section will be
10208 aligned at least to plt_stub_align even when no global
10209 entry stubs are needed. */
10444351
AM
10210 if (!bfd_link_align_section (s, align_power))
10211 return false;
9e390558
AM
10212 stub_align = (bfd_vma) 1 << align_power;
10213 if (htab->params->plt_stub_align >= 0
10214 || ((((stub_off + stub_size - 1) & -stub_align)
10215 - (stub_off & -stub_align))
10216 > ((stub_size - 1) & -stub_align)))
10217 stub_off = (stub_off + stub_align - 1) & -stub_align;
10218 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10219 off -= stub_off + s->output_offset + s->output_section->vma;
10220 /* Note that for --plt-stub-align negative we have a possible
10221 dependency between stub offset and size. Break that
10222 dependency by assuming the max stub size when calculating
10223 the stub offset. */
10224 if (PPC_HA (off) == 0)
10225 stub_size -= 4;
8a2058b5 10226 h->root.type = bfd_link_hash_defined;
afe397ea 10227 h->root.u.def.section = s;
9e390558
AM
10228 h->root.u.def.value = stub_off;
10229 s->size = stub_off + stub_size;
a345bc8d
AM
10230 break;
10231 }
0a1b45a2 10232 return true;
a345bc8d
AM
10233}
10234
65f38f15
AM
10235/* Set the sizes of the dynamic sections. */
10236
0a1b45a2 10237static bool
af969b14
AM
10238ppc64_elf_late_size_sections (bfd *output_bfd,
10239 struct bfd_link_info *info)
65f38f15
AM
10240{
10241 struct ppc_link_hash_table *htab;
10242 bfd *dynobj;
10243 asection *s;
0a1b45a2 10244 bool relocs;
65f38f15 10245 bfd *ibfd;
7865406b 10246 struct got_entry *first_tlsld;
65f38f15
AM
10247
10248 htab = ppc_hash_table (info);
4dfe6ac6 10249 if (htab == NULL)
0a1b45a2 10250 return false;
4dfe6ac6 10251
65f38f15
AM
10252 dynobj = htab->elf.dynobj;
10253 if (dynobj == NULL)
af969b14 10254 return true;
65f38f15
AM
10255
10256 if (htab->elf.dynamic_sections_created)
10257 {
10258 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10259 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10260 {
3d4d4302 10261 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10262 if (s == NULL)
10263 abort ();
eea6121a 10264 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15 10265 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
cb6326b5 10266 s->alloced = 1;
65f38f15
AM
10267 }
10268 }
10269
10270 /* Set up .got offsets for local syms, and space for local dynamic
10271 relocs. */
c72f2fb2 10272 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10273 {
411e1bfb
AM
10274 struct got_entry **lgot_ents;
10275 struct got_entry **end_lgot_ents;
e054468f
AM
10276 struct plt_entry **local_plt;
10277 struct plt_entry **end_local_plt;
f961d9dd 10278 unsigned char *lgot_masks;
65f38f15
AM
10279 bfd_size_type locsymcount;
10280 Elf_Internal_Shdr *symtab_hdr;
3a3a4c1f
AM
10281 Elf_Internal_Sym *local_syms;
10282 Elf_Internal_Sym *isym;
65f38f15 10283
0c8d6e5c 10284 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10285 continue;
10286
10287 for (s = ibfd->sections; s != NULL; s = s->next)
10288 {
1657026c 10289 struct ppc_local_dyn_relocs *p;
65f38f15 10290
6edfbbad 10291 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10292 {
365bf300 10293 if (discarded_section (p->sec))
ec338859
AM
10294 {
10295 /* Input section has been discarded, either because
10296 it is a copy of a linkonce section or due to
10297 linker script /DISCARD/, so we'll be discarding
10298 the relocs too. */
10299 }
248866a8 10300 else if (p->count != 0)
ec338859 10301 {
1657026c
AM
10302 unsigned int count;
10303 asection *srel;
10304
10305 count = p->count;
8ffb6df2
AM
10306 if (info->enable_dt_relr
10307 && ((!NO_OPD_RELOCS
10308 && (ppc64_elf_section_data (p->sec)->sec_type
10309 == sec_opd))
10310 || !p->ifunc))
1657026c
AM
10311 count -= p->rel_count;
10312 srel = elf_section_data (p->sec)->sreloc;
19e08130 10313 if (p->ifunc)
33e44f2e 10314 srel = htab->elf.irelplt;
1657026c 10315 srel->size += count * sizeof (Elf64_External_Rela);
248866a8
AM
10316 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10317 info->flags |= DF_TEXTREL;
ec338859 10318 }
65f38f15
AM
10319 }
10320 }
10321
411e1bfb
AM
10322 lgot_ents = elf_local_got_ents (ibfd);
10323 if (!lgot_ents)
65f38f15
AM
10324 continue;
10325
0ffa91dd 10326 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10327 locsymcount = symtab_hdr->sh_info;
411e1bfb 10328 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10329 local_plt = (struct plt_entry **) end_lgot_ents;
10330 end_local_plt = local_plt + locsymcount;
f961d9dd 10331 lgot_masks = (unsigned char *) end_local_plt;
c9fecd62
AM
10332 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10333 if (local_syms == NULL && locsymcount != 0)
3a3a4c1f 10334 {
c9fecd62
AM
10335 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10336 0, NULL, NULL, NULL);
10337 if (local_syms == NULL)
10338 return false;
3a3a4c1f 10339 }
e717da7e 10340 s = ppc64_elf_tdata (ibfd)->got;
3a3a4c1f
AM
10341 for (isym = local_syms;
10342 lgot_ents < end_lgot_ents;
c9fecd62 10343 ++lgot_ents, ++lgot_masks, isym++)
65f38f15 10344 {
0b8bcf0d 10345 struct got_entry **pent, *ent;
411e1bfb 10346
0b8bcf0d
AM
10347 pent = lgot_ents;
10348 while ((ent = *pent) != NULL)
411e1bfb
AM
10349 if (ent->got.refcount > 0)
10350 {
e7b938ca 10351 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10352 {
927be08e 10353 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10354 *pent = ent->next;
411e1bfb
AM
10355 }
10356 else
10357 {
19e08130
AM
10358 unsigned int ent_size = 8;
10359 unsigned int rel_size = sizeof (Elf64_External_Rela);
10360
eea6121a 10361 ent->got.offset = s->size;
e7b938ca 10362 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10363 {
19e08130
AM
10364 ent_size *= 2;
10365 rel_size *= 2;
10366 }
10367 s->size += ent_size;
37da22e5 10368 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10369 {
33e44f2e 10370 htab->elf.irelplt->size += rel_size;
19e08130
AM
10371 htab->got_reli_size += rel_size;
10372 }
93370e8e 10373 else if (bfd_link_pic (info)
8f22c953
AM
10374 && (ent->tls_type == 0
10375 ? !info->enable_dt_relr
3a3a4c1f
AM
10376 : !bfd_link_executable (info))
10377 && isym->st_shndx != SHN_ABS)
19e08130
AM
10378 {
10379 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10380 srel->size += rel_size;
927be08e 10381 }
0b8bcf0d 10382 pent = &ent->next;
411e1bfb
AM
10383 }
10384 }
10385 else
0b8bcf0d 10386 *pent = ent->next;
65f38f15 10387 }
3a3a4c1f
AM
10388 if (local_syms != NULL
10389 && symtab_hdr->contents != (unsigned char *) local_syms)
10390 {
10391 if (!info->keep_memory)
10392 free (local_syms);
10393 else
10394 symtab_hdr->contents = (unsigned char *) local_syms;
10395 }
e054468f 10396
2d7ad24e
AM
10397 /* Allocate space for plt calls to local syms. */
10398 lgot_masks = (unsigned char *) end_local_plt;
10399 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10400 {
10401 struct plt_entry *ent;
10402
10403 for (ent = *local_plt; ent != NULL; ent = ent->next)
10404 if (ent->plt.refcount > 0)
10405 {
2d7ad24e
AM
10406 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10407 {
10408 s = htab->elf.iplt;
10409 ent->plt.offset = s->size;
10410 s->size += PLT_ENTRY_SIZE (htab);
10411 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10412 }
3e04d765
AM
10413 else if (htab->can_convert_all_inline_plt
10414 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10415 ent->plt.offset = (bfd_vma) -1;
10416 else
10417 {
10418 s = htab->pltlocal;
10419 ent->plt.offset = s->size;
10420 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
edca615a
AM
10421 if (bfd_link_pic (info)
10422 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
10423 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10424 }
e054468f
AM
10425 }
10426 else
10427 ent->plt.offset = (bfd_vma) -1;
10428 }
65f38f15
AM
10429 }
10430
10431 /* Allocate global sym .plt and .got entries, and space for global
10432 sym dynamic relocs. */
4ce794b7 10433 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10434
0e1862bb 10435 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10436 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10437
7865406b 10438 first_tlsld = NULL;
c72f2fb2 10439 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10440 {
7865406b
AM
10441 struct got_entry *ent;
10442
0c8d6e5c 10443 if (!is_ppc64_elf (ibfd))
102890f0
AM
10444 continue;
10445
7865406b
AM
10446 ent = ppc64_tlsld_got (ibfd);
10447 if (ent->got.refcount > 0)
102890f0 10448 {
7865406b 10449 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10450 {
0a1b45a2 10451 ent->is_indirect = true;
7865406b
AM
10452 ent->got.ent = first_tlsld;
10453 }
10454 else
10455 {
10456 if (first_tlsld == NULL)
10457 first_tlsld = ent;
10458 s = ppc64_elf_tdata (ibfd)->got;
10459 ent->got.offset = s->size;
10460 ent->owner = ibfd;
10461 s->size += 16;
f749f26e 10462 if (bfd_link_dll (info))
7865406b
AM
10463 {
10464 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10465 srel->size += sizeof (Elf64_External_Rela);
10466 }
102890f0
AM
10467 }
10468 }
10469 else
7865406b 10470 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10471 }
10472
65f38f15
AM
10473 /* We now have determined the sizes of the various dynamic sections.
10474 Allocate memory for them. */
0a1b45a2 10475 relocs = false;
65f38f15
AM
10476 for (s = dynobj->sections; s != NULL; s = s->next)
10477 {
10478 if ((s->flags & SEC_LINKER_CREATED) == 0)
10479 continue;
10480
1657026c 10481 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
721956f4
AM
10482 /* These haven't been allocated yet; don't strip. */
10483 continue;
33e44f2e
AM
10484 else if (s == htab->elf.sgot
10485 || s == htab->elf.splt
10486 || s == htab->elf.iplt
2d7ad24e 10487 || s == htab->pltlocal
c456f082 10488 || s == htab->glink
9e390558 10489 || s == htab->global_entry
5474d94f
AM
10490 || s == htab->elf.sdynbss
10491 || s == htab->elf.sdynrelro)
65f38f15
AM
10492 {
10493 /* Strip this section if we don't need it; see the
10494 comment below. */
5bd4f169 10495 }
58d180e8
AM
10496 else if (s == htab->glink_eh_frame)
10497 {
10498 if (!bfd_is_abs_section (s->output_section))
10499 /* Not sized yet. */
10500 continue;
10501 }
08dedd66 10502 else if (startswith (s->name, ".rela"))
5bd4f169 10503 {
c456f082 10504 if (s->size != 0)
5bd4f169 10505 {
33e44f2e 10506 if (s != htab->elf.srelplt)
0a1b45a2 10507 relocs = true;
5bd4f169
AM
10508
10509 /* We use the reloc_count field as a counter if we need
10510 to copy relocs into the output file. */
10511 s->reloc_count = 0;
10512 }
10513 }
65f38f15 10514 else
5bd4f169
AM
10515 {
10516 /* It's not one of our sections, so don't allocate space. */
10517 continue;
10518 }
10519
eea6121a 10520 if (s->size == 0)
5bd4f169 10521 {
c456f082
AM
10522 /* If we don't need this section, strip it from the
10523 output file. This is mostly to handle .rela.bss and
10524 .rela.plt. We must create both sections in
10525 create_dynamic_sections, because they must be created
10526 before the linker maps input sections to output
10527 sections. The linker does that before
10528 adjust_dynamic_symbol is called, and it is that
10529 function which decides whether anything needs to go
10530 into these sections. */
8423293d 10531 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10532 continue;
10533 }
10534
06bcf541
AM
10535 if (bfd_is_abs_section (s->output_section))
10536 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10537 s->name);
10538
c456f082 10539 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10540 continue;
10541
65f38f15
AM
10542 /* Allocate memory for the section contents. We use bfd_zalloc
10543 here in case unused entries are not reclaimed before the
10544 section's contents are written out. This should not happen,
411e1bfb
AM
10545 but this way if it does we get a R_PPC64_NONE reloc in .rela
10546 sections instead of garbage.
10547 We also rely on the section contents being zero when writing
5474d94f 10548 the GOT and .dynrelro. */
eea6121a 10549 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10550 if (s->contents == NULL)
0a1b45a2 10551 return false;
cb6326b5 10552 s->alloced = 1;
5bd4f169
AM
10553 }
10554
c72f2fb2 10555 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10556 {
0c8d6e5c 10557 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10558 continue;
10559
e717da7e 10560 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10561 if (s != NULL && s != htab->elf.sgot)
e717da7e 10562 {
eea6121a 10563 if (s->size == 0)
8423293d 10564 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10565 else
10566 {
eea6121a 10567 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10568 if (s->contents == NULL)
0a1b45a2 10569 return false;
cb6326b5 10570 s->alloced = 1;
e717da7e
AM
10571 }
10572 }
10573 s = ppc64_elf_tdata (ibfd)->relgot;
10574 if (s != NULL)
10575 {
eea6121a 10576 if (s->size == 0)
8423293d 10577 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10578 else
10579 {
eea6121a 10580 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10581 if (s->contents == NULL)
0a1b45a2 10582 return false;
cb6326b5 10583 s->alloced = 1;
0a1b45a2 10584 relocs = true;
e717da7e
AM
10585 s->reloc_count = 0;
10586 }
10587 }
10588 }
10589
e86ce104 10590 if (htab->elf.dynamic_sections_created)
5bd4f169 10591 {
0a1b45a2 10592 bool tls_opt;
e8910a83 10593
5bd4f169
AM
10594 /* Add some entries to the .dynamic section. We fill in the
10595 values later, in ppc64_elf_finish_dynamic_sections, but we
10596 must add the entries now so that we get the correct size for
10597 the .dynamic section. The DT_DEBUG entry is filled in by the
10598 dynamic linker and used by the debugger. */
dc810e39 10599#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10600 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10601
0e1862bb 10602 if (bfd_link_executable (info))
5bd4f169 10603 {
dc810e39 10604 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 10605 return false;
5bd4f169
AM
10606 }
10607
33e44f2e 10608 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10609 {
dc810e39
AM
10610 if (!add_dynamic_entry (DT_PLTGOT, 0)
10611 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10612 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10613 || !add_dynamic_entry (DT_JMPREL, 0)
10614 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
0a1b45a2 10615 return false;
5bd4f169
AM
10616 }
10617
ee67d69a 10618 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10619 {
10620 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10621 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
0a1b45a2 10622 return false;
19397422
AM
10623 }
10624
7c9cf415 10625 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10626 && ((htab->tls_get_addr_fd != NULL
10627 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10628 || (htab->tga_desc_fd != NULL
10629 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10630 if (tls_opt || !htab->opd_abi)
10631 {
10632 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
0a1b45a2 10633 return false;
e8910a83 10634 }
a7f2871e 10635
5bd4f169
AM
10636 if (relocs)
10637 {
dc810e39
AM
10638 if (!add_dynamic_entry (DT_RELA, 0)
10639 || !add_dynamic_entry (DT_RELASZ, 0)
10640 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
0a1b45a2 10641 return false;
5bd4f169 10642
65f38f15
AM
10643 /* If any dynamic relocs apply to a read-only section,
10644 then we need a DT_TEXTREL entry. */
248866a8 10645 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10646 elf_link_hash_traverse (&htab->elf,
10647 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10648
65f38f15 10649 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10650 {
65f38f15 10651 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 10652 return false;
5bd4f169 10653 }
5bd4f169 10654 }
5bd4f169 10655 }
65f38f15 10656#undef add_dynamic_entry
5bd4f169 10657
0a1b45a2 10658 return true;
5bd4f169
AM
10659}
10660
a345bc8d
AM
10661/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10662
0a1b45a2 10663static bool
a345bc8d
AM
10664ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10665{
10666 if (h->plt.plist != NULL
10667 && !h->def_regular
10668 && !h->pointer_equality_needed)
0a1b45a2 10669 return false;
a345bc8d
AM
10670
10671 return _bfd_elf_hash_symbol (h);
10672}
10673
721956f4 10674/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10675
7aba54da 10676static inline enum ppc_stub_main_type
4ce794b7
AM
10677ppc_type_of_stub (asection *input_sec,
10678 const Elf_Internal_Rela *rel,
10679 struct ppc_link_hash_entry **hash,
e054468f 10680 struct plt_entry **plt_ent,
6911b7dc
AM
10681 bfd_vma destination,
10682 unsigned long local_off)
5bd4f169 10683{
721956f4
AM
10684 struct ppc_link_hash_entry *h = *hash;
10685 bfd_vma location;
10686 bfd_vma branch_offset;
10687 bfd_vma max_branch_offset;
4ce794b7 10688 enum elf_ppc64_reloc_type r_type;
5bd4f169 10689
721956f4
AM
10690 if (h != NULL)
10691 {
e054468f 10692 struct plt_entry *ent;
7fe2b9a6 10693 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10694 if (h->oh != NULL
10695 && h->oh->is_func_descriptor)
7b8f6675
AM
10696 {
10697 fdh = ppc_follow_link (h->oh);
10698 *hash = fdh;
10699 }
8387904d 10700
e054468f
AM
10701 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10702 if (ent->addend == rel->r_addend
10703 && ent->plt.offset != (bfd_vma) -1)
10704 {
e054468f
AM
10705 *plt_ent = ent;
10706 return ppc_stub_plt_call;
10707 }
5bd4f169 10708
7fe2b9a6
AM
10709 /* Here, we know we don't have a plt entry. If we don't have a
10710 either a defined function descriptor or a defined entry symbol
10711 in a regular object file, then it is pointless trying to make
10712 any other type of stub. */
854b41e7
AM
10713 if (!is_static_defined (&fdh->elf)
10714 && !is_static_defined (&h->elf))
721956f4 10715 return ppc_stub_none;
5d1634d7 10716 }
e054468f
AM
10717 else if (elf_local_got_ents (input_sec->owner) != NULL)
10718 {
10719 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10720 struct plt_entry **local_plt = (struct plt_entry **)
10721 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10722 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10723
10724 if (local_plt[r_symndx] != NULL)
10725 {
10726 struct plt_entry *ent;
10727
10728 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10729 if (ent->addend == rel->r_addend
10730 && ent->plt.offset != (bfd_vma) -1)
10731 {
10732 *plt_ent = ent;
10733 return ppc_stub_plt_call;
10734 }
10735 }
10736 }
5d1634d7 10737
721956f4
AM
10738 /* Determine where the call point is. */
10739 location = (input_sec->output_offset
10740 + input_sec->output_section->vma
10741 + rel->r_offset);
5d1634d7 10742
721956f4
AM
10743 branch_offset = destination - location;
10744 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10745
721956f4
AM
10746 /* Determine if a long branch stub is needed. */
10747 max_branch_offset = 1 << 25;
23cedd1d
AM
10748 if (r_type == R_PPC64_REL14
10749 || r_type == R_PPC64_REL14_BRTAKEN
10750 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10751 max_branch_offset = 1 << 15;
5d1634d7 10752
6911b7dc 10753 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10754 /* We need a stub. Figure out whether a long_branch or plt_branch
10755 is needed later. */
10756 return ppc_stub_long_branch;
5d1634d7 10757
721956f4 10758 return ppc_stub_none;
5d1634d7
AM
10759}
10760
f891966f
AM
10761/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10762 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10763 . mflr %r12
10764 . bcl 20,31,1f
10765 .1: mflr %r11
10766 . mtlr %r12
05d0e962 10767 . lis %r12,xxx-1b@highest
f891966f 10768 . ori %r12,%r12,xxx-1b@higher
05d0e962 10769 . sldi %r12,%r12,32
f891966f 10770 . oris %r12,%r12,xxx-1b@high
05d0e962 10771 . ori %r12,%r12,xxx-1b@l
f891966f 10772 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10773
10774static bfd_byte *
0a1b45a2 10775build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
05d0e962 10776{
f891966f
AM
10777 bfd_put_32 (abfd, MFLR_R12, p);
10778 p += 4;
10779 bfd_put_32 (abfd, BCL_20_31, p);
10780 p += 4;
10781 bfd_put_32 (abfd, MFLR_R11, p);
10782 p += 4;
10783 bfd_put_32 (abfd, MTLR_R12, p);
10784 p += 4;
05d0e962
AM
10785 if (off + 0x8000 < 0x10000)
10786 {
10787 if (load)
10788 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10789 else
10790 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10791 p += 4;
10792 }
10793 else if (off + 0x80008000ULL < 0x100000000ULL)
10794 {
10795 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10796 p += 4;
10797 if (load)
10798 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10799 else
10800 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10801 p += 4;
10802 }
10803 else
10804 {
10805 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10806 {
10807 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10808 p += 4;
10809 }
10810 else
10811 {
10812 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10813 p += 4;
10814 if (((off >> 32) & 0xffff) != 0)
10815 {
10816 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10817 p += 4;
10818 }
10819 }
10820 if (((off >> 32) & 0xffffffffULL) != 0)
10821 {
10822 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10823 p += 4;
10824 }
10825 if (PPC_HI (off) != 0)
10826 {
10827 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10828 p += 4;
10829 }
10830 if (PPC_LO (off) != 0)
10831 {
10832 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10833 p += 4;
10834 }
10835 if (load)
10836 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10837 else
10838 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10839 p += 4;
10840 }
10841 return p;
10842}
10843
10844static unsigned int
10845size_offset (bfd_vma off)
10846{
10847 unsigned int size;
10848 if (off + 0x8000 < 0x10000)
10849 size = 4;
10850 else if (off + 0x80008000ULL < 0x100000000ULL)
10851 size = 8;
10852 else
10853 {
10854 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10855 size = 4;
10856 else
10857 {
10858 size = 4;
10859 if (((off >> 32) & 0xffff) != 0)
10860 size += 4;
10861 }
10862 if (((off >> 32) & 0xffffffffULL) != 0)
10863 size += 4;
10864 if (PPC_HI (off) != 0)
10865 size += 4;
10866 if (PPC_LO (off) != 0)
10867 size += 4;
10868 size += 4;
10869 }
f891966f 10870 return size + 16;
05d0e962
AM
10871}
10872
3d58e1fc
AM
10873static unsigned int
10874num_relocs_for_offset (bfd_vma off)
10875{
10876 unsigned int num_rel;
10877 if (off + 0x8000 < 0x10000)
10878 num_rel = 1;
10879 else if (off + 0x80008000ULL < 0x100000000ULL)
10880 num_rel = 2;
10881 else
10882 {
10883 num_rel = 1;
10884 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10885 && ((off >> 32) & 0xffff) != 0)
10886 num_rel += 1;
10887 if (PPC_HI (off) != 0)
10888 num_rel += 1;
10889 if (PPC_LO (off) != 0)
10890 num_rel += 1;
10891 }
10892 return num_rel;
10893}
10894
10895static Elf_Internal_Rela *
10896emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10897 bfd_vma roff, bfd_vma targ, bfd_vma off)
10898{
10899 bfd_vma relative_targ = targ - (roff - 8);
10900 if (bfd_big_endian (info->output_bfd))
10901 roff += 2;
10902 r->r_offset = roff;
10903 r->r_addend = relative_targ + roff;
10904 if (off + 0x8000 < 0x10000)
10905 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10906 else if (off + 0x80008000ULL < 0x100000000ULL)
10907 {
10908 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10909 ++r;
10910 roff += 4;
10911 r->r_offset = roff;
10912 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10913 r->r_addend = relative_targ + roff;
10914 }
10915 else
10916 {
10917 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10918 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10919 else
10920 {
10921 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10922 if (((off >> 32) & 0xffff) != 0)
10923 {
10924 ++r;
10925 roff += 4;
10926 r->r_offset = roff;
10927 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10928 r->r_addend = relative_targ + roff;
10929 }
10930 }
10931 if (((off >> 32) & 0xffffffffULL) != 0)
10932 roff += 4;
10933 if (PPC_HI (off) != 0)
10934 {
10935 ++r;
10936 roff += 4;
10937 r->r_offset = roff;
10938 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10939 r->r_addend = relative_targ + roff;
10940 }
10941 if (PPC_LO (off) != 0)
10942 {
10943 ++r;
10944 roff += 4;
10945 r->r_offset = roff;
10946 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10947 r->r_addend = relative_targ + roff;
10948 }
10949 }
10950 return r;
10951}
10952
04bdff6a 10953static bfd_byte *
7c1f4227 10954build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
0a1b45a2 10955 bool load)
04bdff6a
AM
10956{
10957 uint64_t insn;
10958 if (off - odd + (1ULL << 33) < 1ULL << 34)
10959 {
10960 off -= odd;
10961 if (odd)
10962 {
10963 bfd_put_32 (abfd, NOP, p);
10964 p += 4;
10965 }
10966 if (load)
10967 insn = PLD_R12_PC;
10968 else
10969 insn = PADDI_R12_PC;
10970 insn |= D34 (off);
10971 bfd_put_32 (abfd, insn >> 32, p);
10972 p += 4;
10973 bfd_put_32 (abfd, insn, p);
10974 }
10975 /* The minimum value for paddi is -0x200000000. The minimum value
10976 for li is -0x8000, which when shifted by 34 and added gives a
10977 minimum value of -0x2000200000000. The maximum value is
10978 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10979 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10980 {
10981 off -= 8 - odd;
10982 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10983 p += 4;
10984 if (!odd)
10985 {
10986 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10987 p += 4;
10988 }
10989 insn = PADDI_R12_PC | D34 (off);
10990 bfd_put_32 (abfd, insn >> 32, p);
10991 p += 4;
10992 bfd_put_32 (abfd, insn, p);
10993 p += 4;
10994 if (odd)
10995 {
10996 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10997 p += 4;
10998 }
10999 if (load)
11000 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11001 else
11002 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11003 }
11004 else
11005 {
11006 off -= odd + 8;
11007 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
11008 p += 4;
11009 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
11010 p += 4;
11011 if (odd)
11012 {
11013 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11014 p += 4;
11015 }
11016 insn = PADDI_R12_PC | D34 (off);
11017 bfd_put_32 (abfd, insn >> 32, p);
11018 p += 4;
11019 bfd_put_32 (abfd, insn, p);
11020 p += 4;
11021 if (!odd)
11022 {
11023 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11024 p += 4;
11025 }
11026 if (load)
11027 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11028 else
11029 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11030 }
11031 p += 4;
11032 return p;
11033}
11034
11035static unsigned int
7c1f4227 11036size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
11037{
11038 if (off - odd + (1ULL << 33) < 1ULL << 34)
11039 return odd + 8;
11040 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11041 return 20;
11042 else
11043 return 24;
11044}
11045
11046static unsigned int
7c1f4227 11047num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
11048{
11049 if (off - odd + (1ULL << 33) < 1ULL << 34)
11050 return 1;
11051 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11052 return 2;
11053 else
11054 return 3;
11055}
11056
11057static Elf_Internal_Rela *
7c1f4227 11058emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
11059 Elf_Internal_Rela *r, bfd_vma roff,
11060 bfd_vma targ, bfd_vma off, int odd)
11061{
11062 if (off - odd + (1ULL << 33) < 1ULL << 34)
11063 roff += odd;
11064 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11065 {
11066 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11067 r->r_offset = roff + d_offset;
11068 r->r_addend = targ + 8 - odd - d_offset;
11069 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11070 ++r;
11071 roff += 8 - odd;
11072 }
11073 else
11074 {
11075 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11076 r->r_offset = roff + d_offset;
11077 r->r_addend = targ + 8 + odd - d_offset;
11078 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11079 ++r;
11080 roff += 4;
11081 r->r_offset = roff + d_offset;
11082 r->r_addend = targ + 4 + odd - d_offset;
11083 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11084 ++r;
11085 roff += 4 + odd;
11086 }
11087 r->r_offset = roff;
11088 r->r_addend = targ;
11089 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11090 return r;
11091}
11092
df136d64
AM
11093/* Emit .eh_frame opcode to advance pc by DELTA. */
11094
11095static bfd_byte *
11096eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11097{
11098 delta /= 4;
11099 if (delta < 64)
11100 *eh++ = DW_CFA_advance_loc + delta;
11101 else if (delta < 256)
11102 {
11103 *eh++ = DW_CFA_advance_loc1;
11104 *eh++ = delta;
11105 }
11106 else if (delta < 65536)
11107 {
11108 *eh++ = DW_CFA_advance_loc2;
11109 bfd_put_16 (abfd, delta, eh);
11110 eh += 2;
11111 }
11112 else
11113 {
11114 *eh++ = DW_CFA_advance_loc4;
11115 bfd_put_32 (abfd, delta, eh);
11116 eh += 4;
11117 }
11118 return eh;
11119}
11120
11121/* Size of required .eh_frame opcode to advance pc by DELTA. */
11122
11123static unsigned int
11124eh_advance_size (unsigned int delta)
11125{
11126 if (delta < 64 * 4)
11127 /* DW_CFA_advance_loc+[1..63]. */
11128 return 1;
11129 if (delta < 256 * 4)
11130 /* DW_CFA_advance_loc1, byte. */
11131 return 2;
11132 if (delta < 65536 * 4)
11133 /* DW_CFA_advance_loc2, 2 bytes. */
11134 return 3;
11135 /* DW_CFA_advance_loc4, 4 bytes. */
11136 return 5;
11137}
11138
794e51c0
AM
11139/* With power7 weakly ordered memory model, it is possible for ld.so
11140 to update a plt entry in one thread and have another thread see a
11141 stale zero toc entry. To avoid this we need some sort of acquire
11142 barrier in the call stub. One solution is to make the load of the
11143 toc word seem to appear to depend on the load of the function entry
11144 word. Another solution is to test for r2 being zero, and branch to
11145 the appropriate glink entry if so.
11146
11147 . fake dep barrier compare
71a39c98
AM
11148 . ld 12,xxx(2) ld 12,xxx(2)
11149 . mtctr 12 mtctr 12
11150 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
11151 . add 2,2,11 cmpldi 2,0
11152 . ld 2,xxx+8(2) bnectr+
11153 . bctr b <glink_entry>
11154
11155 The solution involving the compare turns out to be faster, so
11156 that's what we use unless the branch won't reach. */
11157
11158#define ALWAYS_USE_FAKE_DEP 0
11159#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 11160
794e51c0
AM
11161static inline unsigned int
11162plt_stub_size (struct ppc_link_hash_table *htab,
11163 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
11164 bfd_vma off,
11165 unsigned int odd)
794e51c0 11166{
05d0e962 11167 unsigned size;
b9e5796b 11168
7aba54da 11169 if (stub_entry->type.sub == ppc_stub_notoc)
05d0e962 11170 {
7aba54da
AM
11171 size = 8 + size_power10_offset (off, odd);
11172 if (stub_entry->type.r2save)
11173 size += 4;
11174 }
11175 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11176 {
11177 size = 8 + size_offset (off - 8);
11178 if (stub_entry->type.r2save)
05d0e962 11179 size += 4;
05d0e962 11180 }
29433886 11181 else
b9e5796b 11182 {
29433886 11183 size = 12;
7aba54da 11184 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
b9e5796b 11185 size += 4;
29433886 11186 if (PPC_HA (off) != 0)
b9e5796b 11187 size += 4;
29433886
AM
11188 if (htab->opd_abi)
11189 {
11190 size += 4;
11191 if (htab->params->plt_static_chain)
11192 size += 4;
11193 if (htab->params->plt_thread_safe
11194 && htab->elf.dynamic_sections_created
11195 && stub_entry->h != NULL
11196 && stub_entry->h->elf.dynindx != -1)
11197 size += 8;
11198 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11199 != PPC_HA (off))
11200 size += 4;
11201 }
b9e5796b 11202 }
794e51c0 11203 if (stub_entry->h != NULL
ed7007c1 11204 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11205 && htab->params->tls_get_addr_opt)
f378ab09 11206 {
29433886 11207 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11208 {
29433886 11209 size += 30 * 4;
7aba54da 11210 if (stub_entry->type.r2save)
29433886 11211 size += 4;
9e7028aa
AM
11212 }
11213 else
11214 {
29433886 11215 size += 7 * 4;
7aba54da 11216 if (stub_entry->type.r2save)
29433886 11217 size += 6 * 4;
9e7028aa 11218 }
f378ab09 11219 }
794e51c0
AM
11220 return size;
11221}
11222
2420fff6
AM
11223/* Depending on the sign of plt_stub_align:
11224 If positive, return the padding to align to a 2**plt_stub_align
11225 boundary.
11226 If negative, if this stub would cross fewer 2**plt_stub_align
11227 boundaries if we align, then return the padding needed to do so. */
11228
794e51c0 11229static inline unsigned int
0f0d9373 11230plt_stub_pad (int plt_stub_align,
2405fc40 11231 bfd_vma stub_off,
0f0d9373 11232 unsigned int stub_size)
794e51c0 11233{
0f0d9373 11234 unsigned int stub_align;
794e51c0 11235
0f0d9373
AM
11236 if (plt_stub_align >= 0)
11237 stub_align = 1u << plt_stub_align;
11238 else
2420fff6 11239 {
0f0d9373
AM
11240 stub_align = 1u << -plt_stub_align;
11241 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11242 <= ((stub_size - 1) & -stub_align))
11243 return 0;
2420fff6 11244 }
0f0d9373 11245 return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
794e51c0
AM
11246}
11247
7aba54da 11248/* Build a toc using .plt call stub. */
794e51c0
AM
11249
11250static inline bfd_byte *
11251build_plt_stub (struct ppc_link_hash_table *htab,
11252 struct ppc_stub_hash_entry *stub_entry,
11253 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11254{
e7d1c40c 11255 bfd *obfd = htab->params->stub_bfd;
0a1b45a2
AM
11256 bool plt_load_toc = htab->opd_abi;
11257 bool plt_static_chain = htab->params->plt_static_chain;
11258 bool plt_thread_safe = (htab->params->plt_thread_safe
11259 && htab->elf.dynamic_sections_created
11260 && stub_entry->h != NULL
11261 && stub_entry->h->elf.dynindx != -1);
11262 bool use_fake_dep = plt_thread_safe;
794e51c0
AM
11263 bfd_vma cmp_branch_off = 0;
11264
11265 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11266 && plt_load_toc
794e51c0 11267 && plt_thread_safe
9e51d549
AM
11268 && !(stub_entry->h != NULL
11269 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11270 && htab->params->tls_get_addr_opt))
794e51c0
AM
11271 {
11272 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11273 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11274 / PLT_ENTRY_SIZE (htab));
9e390558 11275 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11276 bfd_vma to, from;
11277
68d62958
AM
11278 if (pltindex > 32768)
11279 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11280 to = (glinkoff
11281 + htab->glink->output_offset
11282 + htab->glink->output_section->vma);
6f20ed8a 11283 from = (p - stub_entry->group->stub_sec->contents
7aba54da 11284 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0
AM
11285 + 4 * (PPC_HA (offset) != 0)
11286 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11287 != PPC_HA (offset))
11288 + 4 * (plt_static_chain != 0)
11289 + 20
6f20ed8a
AM
11290 + stub_entry->group->stub_sec->output_offset
11291 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11292 cmp_branch_off = to - from;
11293 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11294 }
11295
ac2df442
AM
11296 if (PPC_HA (offset) != 0)
11297 {
176a0d42
AM
11298 if (r != NULL)
11299 {
7aba54da 11300 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11301 r[0].r_offset += 4;
176a0d42 11302 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11303 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11304 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11305 r[1].r_addend = r[0].r_addend;
b9e5796b 11306 if (plt_load_toc)
176a0d42 11307 {
b9e5796b 11308 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11309 {
b9e5796b
AM
11310 r[2].r_offset = r[1].r_offset + 4;
11311 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11312 r[2].r_addend = r[0].r_addend;
11313 }
11314 else
11315 {
11316 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11317 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11318 r[2].r_addend = r[0].r_addend + 8;
11319 if (plt_static_chain)
11320 {
11321 r[3].r_offset = r[2].r_offset + 4;
11322 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11323 r[3].r_addend = r[0].r_addend + 16;
11324 }
c7131b65 11325 }
176a0d42
AM
11326 }
11327 }
7aba54da 11328 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11329 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11330 if (plt_load_toc)
11331 {
11332 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11333 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11334 }
11335 else
11336 {
11337 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11338 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11339 }
b9e5796b
AM
11340 if (plt_load_toc
11341 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11342 {
71a39c98 11343 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11344 offset = 0;
11345 }
71a39c98 11346 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11347 if (plt_load_toc)
794e51c0 11348 {
b9e5796b
AM
11349 if (use_fake_dep)
11350 {
11351 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11352 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11353 }
11354 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11355 if (plt_static_chain)
11356 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11357 }
ac2df442
AM
11358 }
11359 else
11360 {
176a0d42
AM
11361 if (r != NULL)
11362 {
7aba54da 11363 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11364 r[0].r_offset += 4;
176a0d42 11365 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11366 if (plt_load_toc)
176a0d42 11367 {
b9e5796b 11368 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11369 {
b9e5796b
AM
11370 r[1].r_offset = r[0].r_offset + 4;
11371 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11372 r[1].r_addend = r[0].r_addend;
11373 }
11374 else
11375 {
11376 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11377 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11378 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11379 if (plt_static_chain)
11380 {
11381 r[2].r_offset = r[1].r_offset + 4;
11382 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11383 r[2].r_addend = r[0].r_addend + 8;
11384 }
c7131b65 11385 }
176a0d42
AM
11386 }
11387 }
7aba54da 11388 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11389 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11390 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11391 if (plt_load_toc
11392 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11393 {
11394 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11395 offset = 0;
11396 }
71a39c98 11397 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11398 if (plt_load_toc)
794e51c0 11399 {
b9e5796b
AM
11400 if (use_fake_dep)
11401 {
11402 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11403 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11404 }
11405 if (plt_static_chain)
11406 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11407 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11408 }
ac2df442 11409 }
b9e5796b 11410 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11411 {
11412 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11413 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11414 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11415 }
11416 else
407aa07c 11417 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11418 return p;
11419}
11420
a7f2871e
AM
11421/* Build a special .plt call stub for __tls_get_addr. */
11422
b9ca1af6 11423#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11424#define LD_R12_0R3 0xe9830000
11425#define MR_R0_R3 0x7c601b78
b9ca1af6 11426#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11427#define ADD_R3_R12_R13 0x7c6c6a14
11428#define BEQLR 0x4d820020
11429#define MR_R3_R0 0x7c030378
a7f2871e 11430#define BCTRL 0x4e800421
a7f2871e 11431
29433886
AM
11432static bfd_byte *
11433build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11434 struct ppc_stub_hash_entry *stub_entry,
29433886 11435 bfd_byte *p)
a7f2871e 11436{
e7d1c40c 11437 bfd *obfd = htab->params->stub_bfd;
794e51c0 11438
b9ca1af6 11439 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11440 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11441 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11442 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11443 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11444 bfd_put_32 (obfd, BEQLR, p), p += 4;
11445 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11446
29433886
AM
11447 if (!htab->params->no_tls_get_addr_regsave)
11448 p = tls_get_addr_prologue (obfd, p, htab);
7aba54da 11449 else if (stub_entry->type.r2save)
29433886 11450 {
9e7028aa
AM
11451 bfd_put_32 (obfd, MFLR_R0, p);
11452 p += 4;
11453 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11454 p += 4;
29433886
AM
11455 }
11456 return p;
11457}
9e7028aa 11458
29433886
AM
11459static bfd_byte *
11460build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11461 struct ppc_stub_hash_entry *stub_entry,
11462 bfd_byte *p,
11463 bfd_byte *loc)
11464{
11465 bfd *obfd = htab->params->stub_bfd;
11466
11467 if (!htab->params->no_tls_get_addr_regsave)
11468 {
11469 bfd_put_32 (obfd, BCTRL, p - 4);
11470
7aba54da 11471 if (stub_entry->type.r2save)
29433886
AM
11472 {
11473 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11474 p += 4;
11475 }
11476 p = tls_get_addr_epilogue (obfd, p, htab);
11477 }
7aba54da 11478 else if (stub_entry->type.r2save)
29433886 11479 {
9e7028aa
AM
11480 bfd_put_32 (obfd, BCTRL, p - 4);
11481
11482 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11483 p += 4;
11484 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11485 p += 4;
11486 bfd_put_32 (obfd, MTLR_R0, p);
11487 p += 4;
11488 bfd_put_32 (obfd, BLR, p);
11489 p += 4;
11490 }
a7f2871e 11491
df136d64
AM
11492 if (htab->glink_eh_frame != NULL
11493 && htab->glink_eh_frame->size != 0)
11494 {
11495 bfd_byte *base, *eh;
df136d64
AM
11496
11497 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11498 eh = base + stub_entry->group->eh_size;
29433886
AM
11499
11500 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11501 {
29433886
AM
11502 unsigned int cfa_updt, delta, i;
11503
9e7028aa
AM
11504 /* After the bctrl, lr has been modified so we need to emit
11505 .eh_frame info saying the return address is on the stack. In
11506 fact we must put the EH info at or before the call rather
11507 than after it, because the EH info for a call needs to be
11508 specified by that point.
11509 See libgcc/unwind-dw2.c execute_cfa_program.
11510 Any stack pointer update must be described immediately after
11511 the instruction making the change, and since the stdu occurs
11512 after saving regs we put all the reg saves and the cfa
11513 change there. */
11514 cfa_updt = stub_entry->stub_offset + 18 * 4;
11515 delta = cfa_updt - stub_entry->group->lr_restore;
11516 stub_entry->group->lr_restore
11517 = stub_entry->stub_offset + (p - loc) - 4;
11518 eh = eh_advance (htab->elf.dynobj, eh, delta);
11519 *eh++ = DW_CFA_def_cfa_offset;
11520 if (htab->opd_abi)
11521 {
11522 *eh++ = 128;
11523 *eh++ = 1;
11524 }
11525 else
11526 *eh++ = 96;
11527 *eh++ = DW_CFA_offset_extended_sf;
11528 *eh++ = 65;
11529 *eh++ = (-16 / 8) & 0x7f;
11530 for (i = 4; i < 12; i++)
11531 {
11532 *eh++ = DW_CFA_offset + i;
11533 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11534 }
11535 *eh++ = (DW_CFA_advance_loc
11536 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11537 *eh++ = DW_CFA_def_cfa_offset;
11538 *eh++ = 0;
11539 for (i = 4; i < 12; i++)
11540 *eh++ = DW_CFA_restore + i;
11541 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11542 *eh++ = DW_CFA_restore_extended;
11543 *eh++ = 65;
11544 stub_entry->group->eh_size = eh - base;
11545 }
7aba54da 11546 else if (stub_entry->type.r2save)
29433886
AM
11547 {
11548 unsigned int lr_used, delta;
11549
11550 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11551 delta = lr_used - stub_entry->group->lr_restore;
11552 stub_entry->group->lr_restore = lr_used + 16;
11553 eh = eh_advance (htab->elf.dynobj, eh, delta);
11554 *eh++ = DW_CFA_offset_extended_sf;
11555 *eh++ = 65;
11556 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11557 *eh++ = DW_CFA_advance_loc + 4;
11558 *eh++ = DW_CFA_restore_extended;
11559 *eh++ = 65;
11560 stub_entry->group->eh_size = eh - base;
9e7028aa 11561 }
df136d64 11562 }
a7f2871e
AM
11563 return p;
11564}
11565
176a0d42
AM
11566static Elf_Internal_Rela *
11567get_relocs (asection *sec, int count)
11568{
11569 Elf_Internal_Rela *relocs;
11570 struct bfd_elf_section_data *elfsec_data;
11571
11572 elfsec_data = elf_section_data (sec);
11573 relocs = elfsec_data->relocs;
11574 if (relocs == NULL)
11575 {
11576 bfd_size_type relsize;
11577 relsize = sec->reloc_count * sizeof (*relocs);
3a8864b3 11578 relocs = bfd_malloc (relsize);
176a0d42
AM
11579 if (relocs == NULL)
11580 return NULL;
11581 elfsec_data->relocs = relocs;
d4730f92
BS
11582 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11583 sizeof (Elf_Internal_Shdr));
11584 if (elfsec_data->rela.hdr == NULL)
11585 return NULL;
11586 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11587 * sizeof (Elf64_External_Rela));
11588 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11589 sec->reloc_count = 0;
11590 }
11591 relocs += sec->reloc_count;
11592 sec->reloc_count += count;
11593 return relocs;
11594}
11595
066c738e
AM
11596static bool
11597swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, bfd_byte *loc, asection *s)
11598{
11599 if ((size_t) (loc - s->contents) >= s->size)
11600 return false;
11601 bfd_elf64_swap_reloca_out (obfd, rel, loc);
11602 return true;
11603}
11604
11605static bool
11606count_and_swap_reloc_out (bfd *obfd, Elf_Internal_Rela *rel, asection *s)
11607{
11608 bfd_byte *loc = s->contents;
11609 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
11610 return swap_reloc_out (obfd, rel, loc, s);
11611}
11612
11613
3d58e1fc
AM
11614/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11615 forms, to the equivalent relocs against the global symbol given by
11616 STUB_ENTRY->H. */
11617
0a1b45a2 11618static bool
3d58e1fc
AM
11619use_global_in_relocs (struct ppc_link_hash_table *htab,
11620 struct ppc_stub_hash_entry *stub_entry,
11621 Elf_Internal_Rela *r, unsigned int num_rel)
11622{
11623 struct elf_link_hash_entry **hashes;
11624 unsigned long symndx;
11625 struct ppc_link_hash_entry *h;
11626 bfd_vma symval;
11627
11628 /* Relocs are always against symbols in their own object file. Fake
11629 up global sym hashes for the stub bfd (which has no symbols). */
11630 hashes = elf_sym_hashes (htab->params->stub_bfd);
11631 if (hashes == NULL)
11632 {
11633 bfd_size_type hsize;
11634
11635 /* When called the first time, stub_globals will contain the
11636 total number of symbols seen during stub sizing. After
11637 allocating, stub_globals is used as an index to fill the
11638 hashes array. */
11639 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11640 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11641 if (hashes == NULL)
0a1b45a2 11642 return false;
3d58e1fc
AM
11643 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11644 htab->stub_globals = 1;
11645 }
11646 symndx = htab->stub_globals++;
11647 h = stub_entry->h;
11648 hashes[symndx] = &h->elf;
11649 if (h->oh != NULL && h->oh->is_func)
11650 h = ppc_follow_link (h->oh);
11651 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11652 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11653 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11654 while (num_rel-- != 0)
11655 {
11656 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11657 if (h->elf.root.u.def.section != stub_entry->target_section)
11658 {
11659 /* H is an opd symbol. The addend must be zero, and the
11660 branch reloc is the only one we can convert. */
11661 r->r_addend = 0;
11662 break;
11663 }
11664 else
11665 r->r_addend -= symval;
11666 --r;
11667 }
0a1b45a2 11668 return true;
3d58e1fc
AM
11669}
11670
aa374f67 11671static bfd_vma
25f53a85 11672get_r2off (struct bfd_link_info *info,
aa374f67
AM
11673 struct ppc_stub_hash_entry *stub_entry)
11674{
25f53a85 11675 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11676 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11677
11678 if (r2off == 0)
11679 {
11680 /* Support linking -R objects. Get the toc pointer from the
11681 opd entry. */
11682 char buf[8];
b9e5796b
AM
11683 if (!htab->opd_abi)
11684 return r2off;
aa374f67
AM
11685 asection *opd = stub_entry->h->elf.root.u.def.section;
11686 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11687
11688 if (strcmp (opd->name, ".opd") != 0
11689 || opd->reloc_count != 0)
11690 {
2cdcc330
AM
11691 info->callbacks->einfo
11692 (_("%P: cannot find opd entry toc for `%pT'\n"),
11693 stub_entry->h->elf.root.root.string);
aa374f67 11694 bfd_set_error (bfd_error_bad_value);
a7c49797 11695 return (bfd_vma) -1;
aa374f67
AM
11696 }
11697 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11698 return (bfd_vma) -1;
aa374f67 11699 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11700 r2off -= elf_gp (info->output_bfd);
aa374f67 11701 }
6f20ed8a 11702 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11703 return r2off;
11704}
11705
46439c90
AM
11706/* Debug dump. */
11707
11708static void
ecc741e6
AM
11709dump_stub (const char *header,
11710 struct ppc_stub_hash_entry *stub_entry,
11711 size_t end_offset)
46439c90 11712{
ecc741e6
AM
11713 const char *t1, *t2, *t3;
11714 switch (stub_entry->type.main)
46439c90 11715 {
ecc741e6
AM
11716 case ppc_stub_none: t1 = "none"; break;
11717 case ppc_stub_long_branch: t1 = "long_branch"; break;
11718 case ppc_stub_plt_branch: t1 = "plt_branch"; break;
11719 case ppc_stub_plt_call: t1 = "plt_call"; break;
11720 case ppc_stub_global_entry: t1 = "global_entry"; break;
11721 case ppc_stub_save_res: t1 = "save_res"; break;
11722 default: t1 = "???"; break;
11723 }
11724 switch (stub_entry->type.sub)
11725 {
11726 case ppc_stub_toc: t2 = "toc"; break;
11727 case ppc_stub_notoc: t2 = "notoc"; break;
11728 case ppc_stub_p9notoc: t2 = "p9notoc"; break;
11729 default: t2 = "???"; break;
11730 }
11731 t3 = stub_entry->type.r2save ? "r2save" : "";
11732 fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11733 header, stub_entry->id, t1, t2, t3);
11734 fprintf (stderr, "name = %s\n", stub_entry->root.string);
b8281767 11735 fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
ecc741e6
AM
11736 for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11737 {
11738 asection *stub_sec = stub_entry->group->stub_sec;
11739 uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11740 fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
46439c90 11741 }
ecc741e6 11742 fprintf (stderr, "\n");
46439c90
AM
11743}
11744
0a1b45a2 11745static bool
4ce794b7 11746ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11747{
721956f4
AM
11748 struct ppc_stub_hash_entry *stub_entry;
11749 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11750 struct bfd_link_info *info;
11751 struct ppc_link_hash_table *htab;
29433886 11752 bfd *obfd;
721956f4 11753 bfd_byte *loc;
3d58e1fc 11754 bfd_byte *p, *relp;
1aa42141 11755 bfd_vma targ, off;
176a0d42 11756 Elf_Internal_Rela *r;
e054468f 11757 asection *plt;
3d58e1fc 11758 int num_rel;
04bdff6a 11759 int odd;
0a1b45a2 11760 bool is_tga;
5d1634d7 11761
721956f4
AM
11762 /* Massage our args to the form they really have. */
11763 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11764 info = in_arg;
5d1634d7 11765
5d1634d7 11766 htab = ppc_hash_table (info);
4dfe6ac6 11767 if (htab == NULL)
0a1b45a2 11768 return false;
5d1634d7 11769
ecc741e6
AM
11770 struct _ppc64_elf_section_data *esd
11771 = ppc64_elf_section_data (stub_entry->group->stub_sec);
11772 ++htab->stub_id;
11773 if (stub_entry->id != htab->stub_id
570e911f
AM
11774 || (stub_entry->type.main != ppc_stub_save_res
11775 && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
46439c90
AM
11776 {
11777 BFD_ASSERT (0);
ecc741e6
AM
11778 if (stub_entry->id != htab->stub_id)
11779 fprintf (stderr, "Expected id %u, got %u\n",
11780 htab->stub_id, stub_entry->id);
11781 if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
b8281767
AM
11782 fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11783 PRIx64 "\n", stub_entry->group->stub_sec->size,
ecc741e6
AM
11784 stub_entry->stub_offset);
11785 if (esd->sec_type == sec_stub)
11786 dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11787 dump_stub ("Current:", stub_entry, 0);
46439c90 11788 }
ecc741e6
AM
11789 if (esd->sec_type == sec_normal)
11790 esd->sec_type = sec_stub;
11791 if (esd->sec_type == sec_stub)
11792 esd->u.last_ent = stub_entry;
6f20ed8a 11793 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11794
7aba54da
AM
11795 htab->stub_count[stub_entry->type.main - 1] += 1;
11796 if (stub_entry->type.main == ppc_stub_long_branch
11797 && stub_entry->type.sub == ppc_stub_toc)
5d1634d7 11798 {
721956f4 11799 /* Branches are relative. This is where we are going to. */
1aa42141 11800 targ = (stub_entry->target_value
6911b7dc
AM
11801 + stub_entry->target_section->output_offset
11802 + stub_entry->target_section->output_section->vma);
1aa42141 11803 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11804
721956f4 11805 /* And this is where we are coming from. */
1aa42141
AM
11806 off = (stub_entry->stub_offset
11807 + stub_entry->group->stub_sec->output_offset
11808 + stub_entry->group->stub_sec->output_section->vma);
11809 off = targ - off;
e86ce104 11810
9e390558 11811 p = loc;
29433886 11812 obfd = htab->params->stub_bfd;
7aba54da 11813 if (stub_entry->type.r2save)
ad8e1ba5 11814 {
25f53a85 11815 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11816
a7c49797 11817 if (r2off == (bfd_vma) -1)
aa374f67 11818 {
0a1b45a2
AM
11819 htab->stub_error = true;
11820 return false;
aa374f67 11821 }
29433886 11822 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11823 p += 4;
ac2df442
AM
11824 if (PPC_HA (r2off) != 0)
11825 {
29433886 11826 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11827 p += 4;
a7c49797
AM
11828 }
11829 if (PPC_LO (r2off) != 0)
11830 {
29433886 11831 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11832 p += 4;
ac2df442 11833 }
9e390558 11834 off -= p - loc;
ad8e1ba5 11835 }
29433886 11836 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11837 p += 4;
ad8e1ba5 11838
5c3dead3
AM
11839 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11840 {
cf97bcb0
AM
11841 _bfd_error_handler
11842 (_("long branch stub `%s' offset overflow"),
bc30df16 11843 stub_entry->root.string);
0a1b45a2
AM
11844 htab->stub_error = true;
11845 return false;
5c3dead3 11846 }
ee75fd95
AM
11847
11848 if (info->emitrelocations)
11849 {
6f20ed8a 11850 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42 11851 if (r == NULL)
0a1b45a2 11852 return false;
9e390558 11853 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11854 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11855 r->r_addend = targ;
3d58e1fc
AM
11856 if (stub_entry->h != NULL
11857 && !use_global_in_relocs (htab, stub_entry, r, 1))
0a1b45a2 11858 return false;
ee75fd95 11859 }
7aba54da
AM
11860 }
11861 else if (stub_entry->type.main == ppc_stub_plt_branch
11862 && stub_entry->type.sub == ppc_stub_toc)
11863 {
721956f4
AM
11864 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11865 stub_entry->root.string + 9,
0a1b45a2 11866 false, false);
721956f4
AM
11867 if (br_entry == NULL)
11868 {
cf97bcb0
AM
11869 _bfd_error_handler (_("can't find branch stub `%s'"),
11870 stub_entry->root.string);
0a1b45a2
AM
11871 htab->stub_error = true;
11872 return false;
721956f4
AM
11873 }
11874
1aa42141 11875 targ = (stub_entry->target_value
176a0d42
AM
11876 + stub_entry->target_section->output_offset
11877 + stub_entry->target_section->output_section->vma);
7aba54da 11878 if (!stub_entry->type.r2save)
1aa42141 11879 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11880
1aa42141 11881 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11882 htab->brlt->contents + br_entry->offset);
721956f4 11883
f94498ff 11884 if (br_entry->iter == htab->stub_iteration)
721956f4 11885 {
f94498ff 11886 br_entry->iter = 0;
84f5d08e 11887
1657026c 11888 if (htab->relbrlt != NULL && !info->enable_dt_relr)
84f5d08e 11889 {
f94498ff
AM
11890 /* Create a reloc for the branch lookup table entry. */
11891 Elf_Internal_Rela rela;
f94498ff
AM
11892
11893 rela.r_offset = (br_entry->offset
11894 + htab->brlt->output_offset
11895 + htab->brlt->output_section->vma);
11896 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11897 rela.r_addend = targ;
f94498ff 11898
066c738e
AM
11899 BFD_ASSERT (count_and_swap_reloc_out (htab->relbrlt->owner, &rela,
11900 htab->relbrlt));
f94498ff
AM
11901 }
11902 else if (info->emitrelocations)
11903 {
176a0d42
AM
11904 r = get_relocs (htab->brlt, 1);
11905 if (r == NULL)
0a1b45a2 11906 return false;
176a0d42
AM
11907 /* brlt, being SEC_LINKER_CREATED does not go through the
11908 normal reloc processing. Symbols and offsets are not
11909 translated from input file to output file form, so
11910 set up the offset per the output file. */
f94498ff
AM
11911 r->r_offset = (br_entry->offset
11912 + htab->brlt->output_offset
11913 + htab->brlt->output_section->vma);
11914 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11915 r->r_addend = targ;
84f5d08e 11916 }
84f5d08e 11917 }
721956f4 11918
1aa42141 11919 targ = (br_entry->offset
176a0d42
AM
11920 + htab->brlt->output_offset
11921 + htab->brlt->output_section->vma);
11922
1aa42141
AM
11923 off = (elf_gp (info->output_bfd)
11924 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11925 off = targ - off;
721956f4 11926
ad8e1ba5 11927 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11928 {
25f53a85 11929 info->callbacks->einfo
c1c8c1ef 11930 (_("%P: linkage table error against `%pT'\n"),
721956f4 11931 stub_entry->root.string);
5d1634d7 11932 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11933 htab->stub_error = true;
11934 return false;
5d1634d7 11935 }
41bd81ab 11936
176a0d42
AM
11937 if (info->emitrelocations)
11938 {
6f20ed8a 11939 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42 11940 if (r == NULL)
0a1b45a2 11941 return false;
6f20ed8a 11942 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11943 if (bfd_big_endian (info->output_bfd))
11944 r[0].r_offset += 2;
7aba54da 11945 if (stub_entry->type.r2save)
176a0d42
AM
11946 r[0].r_offset += 4;
11947 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11948 r[0].r_addend = targ;
176a0d42
AM
11949 if (PPC_HA (off) != 0)
11950 {
11951 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11952 r[1].r_offset = r[0].r_offset + 4;
11953 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11954 r[1].r_addend = r[0].r_addend;
11955 }
11956 }
11957
9e390558 11958 p = loc;
29433886 11959 obfd = htab->params->stub_bfd;
7aba54da 11960 if (!stub_entry->type.r2save)
ad8e1ba5 11961 {
176a0d42 11962 if (PPC_HA (off) != 0)
ac2df442 11963 {
29433886 11964 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11965 p += 4;
29433886 11966 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11967 }
11968 else
29433886 11969 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11970 }
11971 else
11972 {
25f53a85 11973 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11974
a7c49797 11975 if (r2off == (bfd_vma) -1)
aa374f67 11976 {
0a1b45a2
AM
11977 htab->stub_error = true;
11978 return false;
aa374f67 11979 }
ad8e1ba5 11980
29433886 11981 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11982 p += 4;
176a0d42 11983 if (PPC_HA (off) != 0)
ac2df442 11984 {
29433886 11985 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11986 p += 4;
29433886 11987 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11988 }
11989 else
29433886 11990 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11991
11992 if (PPC_HA (r2off) != 0)
11993 {
9e390558 11994 p += 4;
29433886 11995 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11996 }
11997 if (PPC_LO (r2off) != 0)
11998 {
9e390558 11999 p += 4;
29433886 12000 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 12001 }
ad8e1ba5 12002 }
9e390558 12003 p += 4;
29433886 12004 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 12005 p += 4;
29433886 12006 bfd_put_32 (obfd, BCTR, p);
407aa07c 12007 p += 4;
7aba54da
AM
12008 }
12009 else if (stub_entry->type.sub >= ppc_stub_notoc)
12010 {
12011 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
05d0e962 12012 p = loc;
f891966f 12013 off = (stub_entry->stub_offset
05d0e962
AM
12014 + stub_entry->group->stub_sec->output_offset
12015 + stub_entry->group->stub_sec->output_section->vma);
29433886 12016 obfd = htab->params->stub_bfd;
7aba54da 12017 is_tga = (is_plt
12cf8b93 12018 && stub_entry->h != NULL
29433886
AM
12019 && is_tls_get_addr (&stub_entry->h->elf, htab)
12020 && htab->params->tls_get_addr_opt);
12021 if (is_tga)
12022 {
12023 p = build_tls_get_addr_head (htab, stub_entry, p);
12024 off += p - loc;
12025 }
7aba54da 12026 if (stub_entry->type.r2save)
05d0e962
AM
12027 {
12028 off += 4;
29433886 12029 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
12030 p += 4;
12031 }
7aba54da 12032 if (is_plt)
05d0e962
AM
12033 {
12034 targ = stub_entry->plt_ent->plt.offset & ~1;
12035 if (targ >= (bfd_vma) -2)
12036 abort ();
12037
12038 plt = htab->elf.splt;
30845f11 12039 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
12040 {
12041 if (stub_entry->symtype == STT_GNU_IFUNC)
12042 plt = htab->elf.iplt;
12043 else
12044 plt = htab->pltlocal;
12045 }
12046 targ += plt->output_offset + plt->output_section->vma;
12047 }
12048 else
12049 targ = (stub_entry->target_value
12050 + stub_entry->target_section->output_offset
12051 + stub_entry->target_section->output_section->vma);
04bdff6a 12052 odd = off & 4;
05d0e962 12053 off = targ - off;
f891966f 12054
3d58e1fc
AM
12055 relp = p;
12056 num_rel = 0;
7aba54da
AM
12057 if (stub_entry->type.sub == ppc_stub_notoc)
12058 p = build_power10_offset (obfd, p, off, odd, is_plt);
04bdff6a
AM
12059 else
12060 {
29433886
AM
12061 if (htab->glink_eh_frame != NULL
12062 && htab->glink_eh_frame->size != 0)
12063 {
12064 bfd_byte *base, *eh;
12065 unsigned int lr_used, delta;
12066
12067 base = (htab->glink_eh_frame->contents
12068 + stub_entry->group->eh_base + 17);
12069 eh = base + stub_entry->group->eh_size;
12070 lr_used = stub_entry->stub_offset + (p - loc) + 8;
12071 delta = lr_used - stub_entry->group->lr_restore;
12072 stub_entry->group->lr_restore = lr_used + 8;
12073 eh = eh_advance (htab->elf.dynobj, eh, delta);
12074 *eh++ = DW_CFA_register;
12075 *eh++ = 65;
12076 *eh++ = 12;
12077 *eh++ = DW_CFA_advance_loc + 2;
12078 *eh++ = DW_CFA_restore_extended;
12079 *eh++ = 65;
12080 stub_entry->group->eh_size = eh - base;
12081 }
12082
04bdff6a
AM
12083 /* The notoc stubs calculate their target (either a PLT entry or
12084 the global entry point of a function) relative to the PC
12085 returned by the "bcl" two instructions past the start of the
12086 sequence emitted by build_offset. The offset is therefore 8
12087 less than calculated from the start of the sequence. */
12088 off -= 8;
7aba54da 12089 p = build_offset (obfd, p, off, is_plt);
04bdff6a
AM
12090 }
12091
7aba54da 12092 if (stub_entry->type.main == ppc_stub_long_branch)
05d0e962 12093 {
f891966f 12094 bfd_vma from;
3d58e1fc 12095 num_rel = 1;
f891966f
AM
12096 from = (stub_entry->stub_offset
12097 + stub_entry->group->stub_sec->output_offset
12098 + stub_entry->group->stub_sec->output_section->vma
12099 + (p - loc));
29433886 12100 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
12101 }
12102 else
12103 {
29433886 12104 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 12105 p += 4;
29433886 12106 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
12107 }
12108 p += 4;
df136d64 12109
29433886
AM
12110 if (is_tga)
12111 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12112
3d58e1fc
AM
12113 if (info->emitrelocations)
12114 {
04bdff6a 12115 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
7aba54da 12116 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12117 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12118 else
12119 {
12120 num_rel += num_relocs_for_offset (off);
12121 roff += 16;
12122 }
3d58e1fc
AM
12123 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12124 if (r == NULL)
0a1b45a2 12125 return false;
7aba54da 12126 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12127 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
12128 else
12129 r = emit_relocs_for_offset (info, r, roff, targ, off);
7aba54da 12130 if (stub_entry->type.main == ppc_stub_long_branch)
3d58e1fc
AM
12131 {
12132 ++r;
12133 roff = p - 4 - stub_entry->group->stub_sec->contents;
12134 r->r_offset = roff;
12135 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12136 r->r_addend = targ;
12137 if (stub_entry->h != NULL
12138 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
0a1b45a2 12139 return false;
3d58e1fc
AM
12140 }
12141 }
7aba54da
AM
12142 }
12143 else if (stub_entry->type.main == ppc_stub_plt_call)
12144 {
e054468f 12145 if (stub_entry->h != NULL
b31867b6
AM
12146 && stub_entry->h->is_func_descriptor
12147 && stub_entry->h->oh != NULL)
c862ae31 12148 {
b31867b6
AM
12149 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12150
12151 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 12152 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
12153 if (fh->elf.root.type == bfd_link_hash_undefined
12154 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12155 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 12156 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
12157 }
12158
721956f4 12159 /* Now build the stub. */
1aa42141
AM
12160 targ = stub_entry->plt_ent->plt.offset & ~1;
12161 if (targ >= (bfd_vma) -2)
721956f4
AM
12162 abort ();
12163
33e44f2e 12164 plt = htab->elf.splt;
30845f11 12165 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
12166 {
12167 if (stub_entry->symtype == STT_GNU_IFUNC)
12168 plt = htab->elf.iplt;
12169 else
12170 plt = htab->pltlocal;
12171 }
1aa42141 12172 targ += plt->output_offset + plt->output_section->vma;
e054468f 12173
1aa42141
AM
12174 off = (elf_gp (info->output_bfd)
12175 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12176 off = targ - off;
721956f4 12177
ad8e1ba5 12178 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 12179 {
25f53a85 12180 info->callbacks->einfo
695344c0 12181 /* xgettext:c-format */
c1c8c1ef 12182 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
12183 stub_entry->h != NULL
12184 ? stub_entry->h->elf.root.root.string
12185 : "<local sym>");
721956f4 12186 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
12187 htab->stub_error = true;
12188 return false;
721956f4
AM
12189 }
12190
176a0d42
AM
12191 r = NULL;
12192 if (info->emitrelocations)
12193 {
6f20ed8a 12194 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
12195 ((PPC_HA (off) != 0)
12196 + (htab->opd_abi
e7d1c40c 12197 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
12198 && PPC_HA (off + 16) == PPC_HA (off))
12199 : 1)));
176a0d42 12200 if (r == NULL)
0a1b45a2 12201 return false;
6f20ed8a 12202 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
12203 if (bfd_big_endian (info->output_bfd))
12204 r[0].r_offset += 2;
1aa42141 12205 r[0].r_addend = targ;
176a0d42 12206 }
29433886
AM
12207 p = loc;
12208 obfd = htab->params->stub_bfd;
12209 is_tga = (stub_entry->h != NULL
12210 && is_tls_get_addr (&stub_entry->h->elf, htab)
12211 && htab->params->tls_get_addr_opt);
12212 if (is_tga)
12213 {
12214 p = build_tls_get_addr_head (htab, stub_entry, p);
12215 if (r != NULL)
12216 r[0].r_offset += p - loc;
12217 }
12218 p = build_plt_stub (htab, stub_entry, p, off, r);
12219 if (is_tga)
12220 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
7aba54da
AM
12221 }
12222 else if (stub_entry->type.main == ppc_stub_save_res)
12223 return true;
12224 else
12225 {
721956f4 12226 BFD_FAIL ();
0a1b45a2 12227 return false;
721956f4
AM
12228 }
12229
1aa42141 12230 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 12231
e7d1c40c 12232 if (htab->params->emit_stub_syms)
97b639ba
AM
12233 {
12234 struct elf_link_hash_entry *h;
ee75fd95
AM
12235 size_t len1, len2;
12236 char *name;
12237 const char *const stub_str[] = { "long_branch",
05d0e962 12238 "plt_branch",
ee75fd95
AM
12239 "plt_call" };
12240
7aba54da 12241 len1 = strlen (stub_str[stub_entry->type.main - 1]);
ee75fd95 12242 len2 = strlen (stub_entry->root.string);
aefcec16 12243 name = bfd_alloc (info->output_bfd, len1 + len2 + 2);
ee75fd95 12244 if (name == NULL)
0a1b45a2 12245 return false;
ee75fd95 12246 memcpy (name, stub_entry->root.string, 9);
7aba54da 12247 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
ee75fd95 12248 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
0a1b45a2 12249 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
97b639ba 12250 if (h == NULL)
0a1b45a2 12251 return false;
97b639ba
AM
12252 if (h->root.type == bfd_link_hash_new)
12253 {
12254 h->root.type = bfd_link_hash_defined;
6f20ed8a 12255 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 12256 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
12257 h->ref_regular = 1;
12258 h->def_regular = 1;
12259 h->ref_regular_nonweak = 1;
12260 h->forced_local = 1;
12261 h->non_elf = 0;
2ec55de3 12262 h->root.linker_def = 1;
97b639ba
AM
12263 }
12264 }
12265
0a1b45a2 12266 return true;
721956f4
AM
12267}
12268
12269/* As above, but don't actually build the stub. Just bump offset so
12270 we know stub section sizes, and select plt_branch stubs where
12271 long_branch stubs won't do. */
12272
0a1b45a2 12273static bool
4ce794b7 12274ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
12275{
12276 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 12277 struct bfd_link_info *info;
721956f4 12278 struct ppc_link_hash_table *htab;
f891966f
AM
12279 asection *plt;
12280 bfd_vma targ, off, r2off;
0f0d9373 12281 unsigned int size, pad, extra, lr_used, delta, odd;
0441f94f 12282 bfd_vma stub_offset;
721956f4
AM
12283
12284 /* Massage our args to the form they really have. */
12285 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
12286 info = in_arg;
12287
12288 htab = ppc_hash_table (info);
4dfe6ac6 12289 if (htab == NULL)
0a1b45a2 12290 return false;
721956f4 12291
abf874aa
CL
12292 /* Fail if the target section could not be assigned to an output
12293 section. The user should fix his linker script. */
12294 if (stub_entry->target_section != NULL
12295 && stub_entry->target_section->output_section == NULL
12296 && info->non_contiguous_regions)
d2616191 12297 info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
53215f21
CL
12298 "Retry without --enable-non-contiguous-regions.\n"),
12299 stub_entry->target_section);
abf874aa
CL
12300
12301 /* Same for the group. */
12302 if (stub_entry->group->stub_sec != NULL
12303 && stub_entry->group->stub_sec->output_section == NULL
12304 && info->non_contiguous_regions)
d2616191 12305 info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
c63d4862
AM
12306 "Retry without --enable-non-contiguous-regions.\n"),
12307 stub_entry->group->stub_sec);
abf874aa 12308
1aa42141 12309 /* Make a note of the offset within the stubs for this entry. */
0441f94f 12310 stub_offset = stub_entry->group->stub_sec->size;
2405fc40
AM
12311 if (htab->stub_iteration > STUB_SHRINK_ITER
12312 && stub_entry->stub_offset > stub_offset)
12313 stub_offset = stub_entry->stub_offset;
ecc741e6 12314 stub_entry->id = ++htab->stub_id;
1aa42141 12315
a4b6fadd
AM
12316 if (stub_entry->h != NULL
12317 && stub_entry->h->save_res
12318 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12319 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12320 {
12321 /* Don't make stubs to out-of-line register save/restore
12322 functions. Instead, emit copies of the functions. */
12323 stub_entry->group->needs_save_res = 1;
7aba54da
AM
12324 stub_entry->type.main = ppc_stub_save_res;
12325 stub_entry->type.sub = ppc_stub_toc;
12326 stub_entry->type.r2save = 0;
0a1b45a2 12327 return true;
a4b6fadd
AM
12328 }
12329
7aba54da 12330 if (stub_entry->type.main == ppc_stub_plt_branch)
721956f4 12331 {
f891966f
AM
12332 /* Reset the stub type from the plt branch variant in case we now
12333 can reach with a shorter stub. */
04cb922a 12334 stub_entry->type.main = ppc_stub_long_branch;
7aba54da
AM
12335 }
12336
12337 if (stub_entry->type.main == ppc_stub_long_branch
12338 && stub_entry->type.sub == ppc_stub_toc)
12339 {
1aa42141
AM
12340 targ = (stub_entry->target_value
12341 + stub_entry->target_section->output_offset
12342 + stub_entry->target_section->output_section->vma);
f891966f 12343 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
0441f94f 12344 off = (stub_offset
1aa42141
AM
12345 + stub_entry->group->stub_sec->output_offset
12346 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12347
ad8e1ba5 12348 size = 4;
f891966f 12349 r2off = 0;
7aba54da 12350 if (stub_entry->type.r2save)
ad8e1ba5 12351 {
25f53a85 12352 r2off = get_r2off (info, stub_entry);
a7c49797 12353 if (r2off == (bfd_vma) -1)
aa374f67 12354 {
0a1b45a2
AM
12355 htab->stub_error = true;
12356 return false;
aa374f67 12357 }
a7c49797 12358 size = 8;
ac2df442 12359 if (PPC_HA (r2off) != 0)
a7c49797
AM
12360 size += 4;
12361 if (PPC_LO (r2off) != 0)
12362 size += 4;
1aa42141 12363 off += size - 4;
ad8e1ba5 12364 }
1aa42141 12365 off = targ - off;
ad8e1ba5 12366
f891966f
AM
12367 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12368 Do the same for -R objects without function descriptors. */
7aba54da 12369 if ((stub_entry->type.r2save
f891966f
AM
12370 && r2off == 0
12371 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12372 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12373 {
f891966f 12374 struct ppc_branch_hash_entry *br_entry;
df136d64 12375
f891966f
AM
12376 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12377 stub_entry->root.string + 9,
0a1b45a2 12378 true, false);
f891966f 12379 if (br_entry == NULL)
721956f4 12380 {
f891966f
AM
12381 _bfd_error_handler (_("can't build branch stub `%s'"),
12382 stub_entry->root.string);
0a1b45a2
AM
12383 htab->stub_error = true;
12384 return false;
721956f4
AM
12385 }
12386
f891966f 12387 if (br_entry->iter != htab->stub_iteration)
721956f4 12388 {
f891966f
AM
12389 br_entry->iter = htab->stub_iteration;
12390 br_entry->offset = htab->brlt->size;
12391 htab->brlt->size += 8;
63bc6f6c 12392
1657026c 12393 if (htab->relbrlt != NULL && !info->enable_dt_relr)
f891966f
AM
12394 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12395 else if (info->emitrelocations)
84f5d08e 12396 {
f891966f
AM
12397 htab->brlt->reloc_count += 1;
12398 htab->brlt->flags |= SEC_RELOC;
05d0e962 12399 }
f891966f 12400 }
ac2df442 12401
f891966f
AM
12402 targ = (br_entry->offset
12403 + htab->brlt->output_offset
12404 + htab->brlt->output_section->vma);
12405 off = (elf_gp (info->output_bfd)
12406 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12407 off = targ - off;
176a0d42 12408
f891966f
AM
12409 if (info->emitrelocations)
12410 {
12411 stub_entry->group->stub_sec->reloc_count
12412 += 1 + (PPC_HA (off) != 0);
12413 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12414 }
05d0e962 12415
04cb922a 12416 stub_entry->type.main = ppc_stub_plt_branch;
7aba54da 12417 if (!stub_entry->type.r2save)
f891966f
AM
12418 {
12419 size = 12;
12420 if (PPC_HA (off) != 0)
12421 size = 16;
ac2df442 12422 }
f891966f 12423 else
ac2df442 12424 {
f891966f
AM
12425 size = 16;
12426 if (PPC_HA (off) != 0)
12427 size += 4;
12428
12429 if (PPC_HA (r2off) != 0)
12430 size += 4;
12431 if (PPC_LO (r2off) != 0)
12432 size += 4;
ac2df442 12433 }
f40bb390
AM
12434 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12435 stub_offset += pad;
721956f4 12436 }
f891966f
AM
12437 else if (info->emitrelocations)
12438 {
12439 stub_entry->group->stub_sec->reloc_count += 1;
12440 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12441 }
7aba54da
AM
12442 }
12443 else if (stub_entry->type.main == ppc_stub_long_branch)
12444 {
0441f94f 12445 off = (stub_offset
f891966f
AM
12446 + stub_entry->group->stub_sec->output_offset
12447 + stub_entry->group->stub_sec->output_section->vma);
12448 size = 0;
7aba54da 12449 if (stub_entry->type.r2save)
f891966f
AM
12450 size = 4;
12451 off += size;
12452 targ = (stub_entry->target_value
12453 + stub_entry->target_section->output_offset
12454 + stub_entry->target_section->output_section->vma);
04bdff6a 12455 odd = off & 4;
f891966f
AM
12456 off = targ - off;
12457
f40bb390
AM
12458 if (stub_entry->type.sub == ppc_stub_notoc)
12459 extra = size_power10_offset (off, odd);
12460 else
12461 extra = size_offset (off - 8);
12462 /* Include branch insn plus those in the offset sequence. */
12463 size += 4 + extra;
12464
12465 /* If the branch can't reach, use a plt_branch.
12466 The branch insn is at the end, or "extra" bytes along. So
12467 its offset will be "extra" bytes less that that already
12468 calculated. */
12469 if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12470 {
12471 stub_entry->type.main = ppc_stub_plt_branch;
12472 size += 4;
12473 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12474 if (pad != 0)
12475 {
12476 stub_offset += pad;
12477 off -= pad;
12478 odd ^= pad & 4;
12479 size -= extra;
12480 if (stub_entry->type.sub == ppc_stub_notoc)
12481 extra = size_power10_offset (off, odd);
12482 else
12483 extra = size_offset (off - 8);
12484 size += extra;
12485 }
12486 }
12487 else if (info->emitrelocations)
12488 stub_entry->group->stub_sec->reloc_count +=1;
12489
3d58e1fc
AM
12490 if (info->emitrelocations)
12491 {
04bdff6a 12492 unsigned int num_rel;
7aba54da 12493 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12494 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12495 else
12496 num_rel = num_relocs_for_offset (off - 8);
12497 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12498 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12499 }
12500
7aba54da 12501 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12502 {
12503 /* After the bcl, lr has been modified so we need to emit
12504 .eh_frame info saying the return address is in r12. */
0441f94f 12505 lr_used = stub_offset + 8;
7aba54da 12506 if (stub_entry->type.r2save)
04bdff6a
AM
12507 lr_used += 4;
12508 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12509 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12510 DW_CFA_restore_extended 65. */
12511 delta = lr_used - stub_entry->group->lr_restore;
12512 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12513 stub_entry->group->lr_restore = lr_used + 8;
12514 }
7aba54da
AM
12515 }
12516 else if (stub_entry->type.sub >= ppc_stub_notoc)
12517 {
12518 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
29433886 12519 lr_used = 0;
12cf8b93
AM
12520 if (stub_entry->h != NULL
12521 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12522 && htab->params->tls_get_addr_opt)
12523 {
12524 lr_used += 7 * 4;
12525 if (!htab->params->no_tls_get_addr_regsave)
12526 lr_used += 11 * 4;
7aba54da 12527 else if (stub_entry->type.r2save)
29433886
AM
12528 lr_used += 2 * 4;
12529 }
7aba54da 12530 if (stub_entry->type.r2save)
29433886 12531 lr_used += 4;
f891966f
AM
12532 targ = stub_entry->plt_ent->plt.offset & ~1;
12533 if (targ >= (bfd_vma) -2)
12534 abort ();
12535
12536 plt = htab->elf.splt;
30845f11 12537 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12538 {
12539 if (stub_entry->symtype == STT_GNU_IFUNC)
12540 plt = htab->elf.iplt;
12541 else
12542 plt = htab->pltlocal;
12543 }
12544 targ += plt->output_offset + plt->output_section->vma;
0441f94f 12545 off = (stub_offset
29433886
AM
12546 + stub_entry->group->stub_sec->output_offset
12547 + stub_entry->group->stub_sec->output_section->vma
12548 + lr_used);
04bdff6a 12549 odd = off & 4;
f891966f
AM
12550 off = targ - off;
12551
0f0d9373
AM
12552 size = plt_stub_size (htab, stub_entry, off, odd);
12553 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12554 if (pad != 0)
f891966f 12555 {
2405fc40 12556 stub_offset += pad;
f891966f 12557 off -= pad;
29433886 12558 odd ^= pad & 4;
0f0d9373 12559 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f
AM
12560 }
12561
3d58e1fc
AM
12562 if (info->emitrelocations)
12563 {
04bdff6a 12564 unsigned int num_rel;
7aba54da 12565 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12566 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12567 else
12568 num_rel = num_relocs_for_offset (off - 8);
12569 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12570 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12571 }
12572
7aba54da 12573 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12574 {
12575 /* After the bcl, lr has been modified so we need to emit
12576 .eh_frame info saying the return address is in r12. */
0441f94f 12577 lr_used += stub_offset + 8;
04bdff6a
AM
12578 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12579 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12580 DW_CFA_restore_extended 65. */
12581 delta = lr_used - stub_entry->group->lr_restore;
12582 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12583 stub_entry->group->lr_restore = lr_used + 8;
12584 }
7aba54da 12585 if (stub_entry->h != NULL
29433886
AM
12586 && is_tls_get_addr (&stub_entry->h->elf, htab)
12587 && htab->params->tls_get_addr_opt)
12588 {
12589 if (!htab->params->no_tls_get_addr_regsave)
12590 {
0441f94f 12591 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12592 delta = cfa_updt - stub_entry->group->lr_restore;
12593 stub_entry->group->eh_size += eh_advance_size (delta);
12594 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
0441f94f 12595 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886 12596 }
7aba54da 12597 else if (stub_entry->type.r2save)
29433886 12598 {
0441f94f 12599 lr_used = stub_offset + size - 20;
29433886
AM
12600 delta = lr_used - stub_entry->group->lr_restore;
12601 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
0441f94f 12602 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886
AM
12603 }
12604 }
7aba54da
AM
12605 }
12606 else if (stub_entry->type.main == ppc_stub_plt_call)
12607 {
f891966f
AM
12608 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12609 if (targ >= (bfd_vma) -2)
12610 abort ();
12611 plt = htab->elf.splt;
30845f11 12612 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12613 {
12614 if (stub_entry->symtype == STT_GNU_IFUNC)
12615 plt = htab->elf.iplt;
12616 else
12617 plt = htab->pltlocal;
12618 }
12619 targ += plt->output_offset + plt->output_section->vma;
12620
12621 off = (elf_gp (info->output_bfd)
12622 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12623 off = targ - off;
12624
0f0d9373
AM
12625 size = plt_stub_size (htab, stub_entry, off, 0);
12626 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12627 stub_offset += pad;
f891966f
AM
12628
12629 if (info->emitrelocations)
12630 {
12631 stub_entry->group->stub_sec->reloc_count
12632 += ((PPC_HA (off) != 0)
12633 + (htab->opd_abi
12634 ? 2 + (htab->params->plt_static_chain
12635 && PPC_HA (off + 16) == PPC_HA (off))
12636 : 1));
12637 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12638 }
12639
f891966f 12640 if (stub_entry->h != NULL
ed7007c1 12641 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f 12642 && htab->params->tls_get_addr_opt
7aba54da 12643 && stub_entry->type.r2save)
f891966f 12644 {
29433886
AM
12645 if (!htab->params->no_tls_get_addr_regsave)
12646 {
12647 /* Adjustments to r1 need to be described. */
0441f94f 12648 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12649 delta = cfa_updt - stub_entry->group->lr_restore;
12650 stub_entry->group->eh_size += eh_advance_size (delta);
12651 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12652 }
12653 else
9e7028aa 12654 {
0441f94f 12655 lr_used = stub_offset + size - 20;
9e7028aa
AM
12656 /* The eh_frame info will consist of a DW_CFA_advance_loc
12657 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12658 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12659 delta = lr_used - stub_entry->group->lr_restore;
12660 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12661 }
0441f94f 12662 stub_entry->group->lr_restore = stub_offset + size - 4;
f891966f 12663 }
7aba54da
AM
12664 }
12665 else
12666 {
f891966f 12667 BFD_FAIL ();
0a1b45a2 12668 return false;
721956f4
AM
12669 }
12670
0441f94f
AM
12671 if (stub_entry->stub_offset != stub_offset)
12672 htab->stub_changed = true;
2405fc40
AM
12673 stub_entry->stub_offset = stub_offset;
12674 stub_entry->group->stub_sec->size = stub_offset + size;
0a1b45a2 12675 return true;
721956f4
AM
12676}
12677
12678/* Set up various things so that we can make a list of input sections
12679 for each output section included in the link. Returns -1 on error,
cedb70c5 12680 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12681
12682int
e7d1c40c 12683ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12684{
6f20ed8a 12685 unsigned int id;
986f0783 12686 size_t amt;
721956f4
AM
12687 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12688
4dfe6ac6
NC
12689 if (htab == NULL)
12690 return -1;
4c52953f 12691
e1093de6
TT
12692 /* The access to _bfd_section_id here is unlocked, so for the time
12693 being this function cannot be called in multi-threaded mode. */
12694 BFD_ASSERT (!_bfd_threading_enabled ());
12695
7cf7fcc8 12696 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a 12697 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
aefcec16 12698 htab->sec_info = bfd_zalloc (info->output_bfd, amt);
6f20ed8a 12699 if (htab->sec_info == NULL)
721956f4
AM
12700 return -1;
12701
3d6f9012
AM
12702 /* Set toc_off for com, und, abs and ind sections. */
12703 for (id = 0; id < 3; id++)
6f20ed8a 12704 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12705
721956f4
AM
12706 return 1;
12707}
12708
927be08e
AM
12709/* Set up for first pass at multitoc partitioning. */
12710
12711void
12712ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12713{
12714 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12715
1c865ab2 12716 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12717 htab->toc_bfd = NULL;
12718 htab->toc_first_sec = NULL;
12719}
12720
e717da7e
AM
12721/* The linker repeatedly calls this function for each TOC input section
12722 and linker generated GOT section. Group input bfds such that the toc
927be08e 12723 within a group is less than 64k in size. */
ad8e1ba5 12724
0a1b45a2 12725bool
4ce794b7 12726ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12727{
12728 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12729 bfd_vma addr, off, limit;
ad8e1ba5 12730
4dfe6ac6 12731 if (htab == NULL)
0a1b45a2 12732 return false;
4dfe6ac6 12733
927be08e 12734 if (!htab->second_toc_pass)
4c52953f 12735 {
927be08e 12736 /* Keep track of the first .toc or .got section for this input bfd. */
0a1b45a2 12737 bool new_bfd = htab->toc_bfd != isec->owner;
a4fd3de5
AM
12738
12739 if (new_bfd)
bf102f86
AM
12740 {
12741 htab->toc_bfd = isec->owner;
12742 htab->toc_first_sec = isec;
12743 }
927be08e 12744
bf102f86
AM
12745 addr = isec->output_offset + isec->output_section->vma;
12746 off = addr - htab->toc_curr;
d77c8a4b
AM
12747 limit = 0x80008000;
12748 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12749 limit = 0x10000;
12750 if (off + isec->size > limit)
bf102f86
AM
12751 {
12752 addr = (htab->toc_first_sec->output_offset
12753 + htab->toc_first_sec->output_section->vma);
12754 htab->toc_curr = addr;
a27e685f 12755 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12756 }
99877b66 12757
927be08e
AM
12758 /* toc_curr is the base address of this toc group. Set elf_gp
12759 for the input section to be the offset relative to the
12760 output toc base plus 0x8000. Making the input elf_gp an
12761 offset allows us to move the toc as a whole without
12762 recalculating input elf_gp. */
06bcf541 12763 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12764 off += TOC_BASE_OFF;
12765
12766 /* Die if someone uses a linker script that doesn't keep input
12767 file .toc and .got together. */
a4fd3de5
AM
12768 if (new_bfd
12769 && elf_gp (isec->owner) != 0
927be08e 12770 && elf_gp (isec->owner) != off)
0a1b45a2 12771 return false;
927be08e
AM
12772
12773 elf_gp (isec->owner) = off;
0a1b45a2 12774 return true;
4c52953f 12775 }
927be08e
AM
12776
12777 /* During the second pass toc_first_sec points to the start of
12778 a toc group, and toc_curr is used to track the old elf_gp.
12779 We use toc_bfd to ensure we only look at each bfd once. */
12780 if (htab->toc_bfd == isec->owner)
0a1b45a2 12781 return true;
927be08e
AM
12782 htab->toc_bfd = isec->owner;
12783
12784 if (htab->toc_first_sec == NULL
12785 || htab->toc_curr != elf_gp (isec->owner))
12786 {
12787 htab->toc_curr = elf_gp (isec->owner);
12788 htab->toc_first_sec = isec;
12789 }
12790 addr = (htab->toc_first_sec->output_offset
12791 + htab->toc_first_sec->output_section->vma);
06bcf541 12792 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12793 elf_gp (isec->owner) = off;
12794
0a1b45a2 12795 return true;
ad8e1ba5
AM
12796}
12797
927be08e
AM
12798/* Called via elf_link_hash_traverse to merge GOT entries for global
12799 symbol H. */
12800
0a1b45a2 12801static bool
927be08e
AM
12802merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12803{
12804 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12805 return true;
927be08e 12806
927be08e
AM
12807 merge_got_entries (&h->got.glist);
12808
0a1b45a2 12809 return true;
927be08e
AM
12810}
12811
12812/* Called via elf_link_hash_traverse to allocate GOT entries for global
12813 symbol H. */
12814
0a1b45a2 12815static bool
927be08e
AM
12816reallocate_got (struct elf_link_hash_entry *h, void *inf)
12817{
12818 struct got_entry *gent;
12819
12820 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12821 return true;
927be08e 12822
927be08e
AM
12823 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12824 if (!gent->is_indirect)
12825 allocate_got (h, (struct bfd_link_info *) inf, gent);
0a1b45a2 12826 return true;
927be08e
AM
12827}
12828
12829/* Called on the first multitoc pass after the last call to
12830 ppc64_elf_next_toc_section. This function removes duplicate GOT
12831 entries. */
12832
0a1b45a2 12833bool
927be08e 12834ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12835{
12836 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e 12837 struct bfd *ibfd, *ibfd2;
0a1b45a2 12838 bool done_something;
927be08e
AM
12839
12840 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12841
7865406b 12842 if (!htab->do_multi_toc)
0a1b45a2 12843 return false;
7865406b 12844
d0fae19d 12845 /* Merge global sym got entries within a toc group. */
927be08e
AM
12846 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12847
12848 /* And tlsld_got. */
c72f2fb2 12849 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12850 {
12851 struct got_entry *ent, *ent2;
12852
12853 if (!is_ppc64_elf (ibfd))
12854 continue;
12855
12856 ent = ppc64_tlsld_got (ibfd);
12857 if (!ent->is_indirect
12858 && ent->got.offset != (bfd_vma) -1)
12859 {
c72f2fb2 12860 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12861 {
12862 if (!is_ppc64_elf (ibfd2))
12863 continue;
12864
12865 ent2 = ppc64_tlsld_got (ibfd2);
12866 if (!ent2->is_indirect
12867 && ent2->got.offset != (bfd_vma) -1
12868 && elf_gp (ibfd2) == elf_gp (ibfd))
12869 {
0a1b45a2 12870 ent2->is_indirect = true;
927be08e
AM
12871 ent2->got.ent = ent;
12872 }
12873 }
12874 }
12875 }
12876
12877 /* Zap sizes of got sections. */
33e44f2e
AM
12878 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12879 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12880 htab->got_reli_size = 0;
12881
c72f2fb2 12882 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12883 {
12884 asection *got, *relgot;
12885
12886 if (!is_ppc64_elf (ibfd))
12887 continue;
12888
12889 got = ppc64_elf_tdata (ibfd)->got;
12890 if (got != NULL)
12891 {
12892 got->rawsize = got->size;
12893 got->size = 0;
12894 relgot = ppc64_elf_tdata (ibfd)->relgot;
12895 relgot->rawsize = relgot->size;
12896 relgot->size = 0;
12897 }
12898 }
12899
12900 /* Now reallocate the got, local syms first. We don't need to
12901 allocate section contents again since we never increase size. */
c72f2fb2 12902 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12903 {
12904 struct got_entry **lgot_ents;
12905 struct got_entry **end_lgot_ents;
12906 struct plt_entry **local_plt;
12907 struct plt_entry **end_local_plt;
f961d9dd 12908 unsigned char *lgot_masks;
927be08e
AM
12909 bfd_size_type locsymcount;
12910 Elf_Internal_Shdr *symtab_hdr;
19e08130 12911 asection *s;
3a3a4c1f
AM
12912 Elf_Internal_Sym *local_syms;
12913 Elf_Internal_Sym *isym;
927be08e
AM
12914
12915 if (!is_ppc64_elf (ibfd))
12916 continue;
12917
12918 lgot_ents = elf_local_got_ents (ibfd);
12919 if (!lgot_ents)
12920 continue;
12921
12922 symtab_hdr = &elf_symtab_hdr (ibfd);
12923 locsymcount = symtab_hdr->sh_info;
12924 end_lgot_ents = lgot_ents + locsymcount;
12925 local_plt = (struct plt_entry **) end_lgot_ents;
12926 end_local_plt = local_plt + locsymcount;
f961d9dd 12927 lgot_masks = (unsigned char *) end_local_plt;
c9fecd62
AM
12928 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12929 if (local_syms == NULL && locsymcount != 0)
3a3a4c1f 12930 {
c9fecd62
AM
12931 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12932 0, NULL, NULL, NULL);
12933 if (local_syms == NULL)
12934 return false;
3a3a4c1f 12935 }
927be08e 12936 s = ppc64_elf_tdata (ibfd)->got;
3a3a4c1f
AM
12937 for (isym = local_syms;
12938 lgot_ents < end_lgot_ents;
c9fecd62 12939 ++lgot_ents, ++lgot_masks, isym++)
927be08e
AM
12940 {
12941 struct got_entry *ent;
12942
12943 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12944 {
19e08130
AM
12945 unsigned int ent_size = 8;
12946 unsigned int rel_size = sizeof (Elf64_External_Rela);
12947
d0fae19d
AM
12948 ent->got.offset = s->size;
12949 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12950 {
19e08130
AM
12951 ent_size *= 2;
12952 rel_size *= 2;
12953 }
12954 s->size += ent_size;
37da22e5 12955 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12956 {
33e44f2e 12957 htab->elf.irelplt->size += rel_size;
19e08130
AM
12958 htab->got_reli_size += rel_size;
12959 }
6a3858a6 12960 else if (bfd_link_pic (info)
8f22c953
AM
12961 && (ent->tls_type == 0
12962 ? !info->enable_dt_relr
3a3a4c1f
AM
12963 : !bfd_link_executable (info))
12964 && isym->st_shndx != SHN_ABS)
19e08130
AM
12965 {
12966 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12967 srel->size += rel_size;
d0fae19d
AM
12968 }
12969 }
927be08e
AM
12970 }
12971 }
12972
12973 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12974
c72f2fb2 12975 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12976 {
12977 struct got_entry *ent;
12978
12979 if (!is_ppc64_elf (ibfd))
12980 continue;
12981
12982 ent = ppc64_tlsld_got (ibfd);
12983 if (!ent->is_indirect
12984 && ent->got.offset != (bfd_vma) -1)
12985 {
12986 asection *s = ppc64_elf_tdata (ibfd)->got;
12987 ent->got.offset = s->size;
12988 s->size += 16;
f749f26e 12989 if (bfd_link_dll (info))
927be08e
AM
12990 {
12991 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12992 srel->size += sizeof (Elf64_External_Rela);
12993 }
12994 }
12995 }
12996
33e44f2e 12997 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12998 if (!done_something)
c72f2fb2 12999 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
13000 {
13001 asection *got;
13002
13003 if (!is_ppc64_elf (ibfd))
13004 continue;
13005
13006 got = ppc64_elf_tdata (ibfd)->got;
13007 if (got != NULL)
13008 {
13009 done_something = got->rawsize != got->size;
13010 if (done_something)
13011 break;
13012 }
13013 }
13014
13015 if (done_something)
e7d1c40c 13016 (*htab->params->layout_sections_again) ();
927be08e
AM
13017
13018 /* Set up for second pass over toc sections to recalculate elf_gp
13019 on input sections. */
13020 htab->toc_bfd = NULL;
13021 htab->toc_first_sec = NULL;
0a1b45a2 13022 htab->second_toc_pass = true;
927be08e
AM
13023 return done_something;
13024}
13025
13026/* Called after second pass of multitoc partitioning. */
13027
13028void
13029ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
13030{
13031 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13032
13033 /* After the second pass, toc_curr tracks the TOC offset used
13034 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 13035 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
13036}
13037
9b5ecbd0
AM
13038/* No toc references were found in ISEC. If the code in ISEC makes no
13039 calls, then there's no need to use toc adjusting stubs when branching
13040 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
13041 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
13042 needed, and 2 if a cyclical call-graph was found but no other reason
13043 for a stub was detected. If called from the top level, a return of
13044 2 means the same as a return of 0. */
9b5ecbd0
AM
13045
13046static int
4ce794b7 13047toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 13048{
9b5ecbd0 13049 int ret;
70cc837d
AM
13050
13051 /* Mark this section as checked. */
13052 isec->call_check_done = 1;
9b5ecbd0 13053
772119ce
AM
13054 /* We know none of our code bearing sections will need toc stubs. */
13055 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13056 return 0;
13057
eea6121a 13058 if (isec->size == 0)
082c50f8
AM
13059 return 0;
13060
4c52953f
AM
13061 if (isec->output_section == NULL)
13062 return 0;
13063
4c52953f 13064 ret = 0;
70cc837d 13065 if (isec->reloc_count != 0)
9b5ecbd0 13066 {
70cc837d
AM
13067 Elf_Internal_Rela *relstart, *rel;
13068 Elf_Internal_Sym *local_syms;
13069 struct ppc_link_hash_table *htab;
2917689a 13070
70cc837d
AM
13071 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13072 info->keep_memory);
13073 if (relstart == NULL)
13074 return -1;
90aecf7a 13075
70cc837d
AM
13076 /* Look for branches to outside of this section. */
13077 local_syms = NULL;
13078 htab = ppc_hash_table (info);
13079 if (htab == NULL)
13080 return -1;
4c52953f 13081
70cc837d 13082 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 13083 {
70cc837d
AM
13084 enum elf_ppc64_reloc_type r_type;
13085 unsigned long r_symndx;
13086 struct elf_link_hash_entry *h;
13087 struct ppc_link_hash_entry *eh;
13088 Elf_Internal_Sym *sym;
13089 asection *sym_sec;
13090 struct _opd_sec_data *opd;
13091 bfd_vma sym_value;
13092 bfd_vma dest;
13093
13094 r_type = ELF64_R_TYPE (rel->r_info);
13095 if (r_type != R_PPC64_REL24
05d0e962 13096 && r_type != R_PPC64_REL24_NOTOC
7aba54da 13097 && r_type != R_PPC64_REL24_P9NOTOC
70cc837d
AM
13098 && r_type != R_PPC64_REL14
13099 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 13100 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
13101 && r_type != R_PPC64_PLTCALL
13102 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 13103 continue;
4c52953f 13104
70cc837d
AM
13105 r_symndx = ELF64_R_SYM (rel->r_info);
13106 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13107 isec->owner))
4c52953f 13108 {
70cc837d
AM
13109 ret = -1;
13110 break;
13111 }
4c52953f 13112
70cc837d
AM
13113 /* Calls to dynamic lib functions go through a plt call stub
13114 that uses r2. */
ed7007c1 13115 eh = ppc_elf_hash_entry (h);
70cc837d
AM
13116 if (eh != NULL
13117 && (eh->elf.plt.plist != NULL
13118 || (eh->oh != NULL
13119 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13120 {
13121 ret = 1;
13122 break;
4c52953f
AM
13123 }
13124
70cc837d
AM
13125 if (sym_sec == NULL)
13126 /* Ignore other undefined symbols. */
4c52953f 13127 continue;
4c52953f 13128
70cc837d
AM
13129 /* Assume branches to other sections not included in the
13130 link need stubs too, to cover -R and absolute syms. */
13131 if (sym_sec->output_section == NULL)
13132 {
13133 ret = 1;
13134 break;
13135 }
4c52953f 13136
70cc837d
AM
13137 if (h == NULL)
13138 sym_value = sym->st_value;
13139 else
13140 {
13141 if (h->root.type != bfd_link_hash_defined
13142 && h->root.type != bfd_link_hash_defweak)
13143 abort ();
13144 sym_value = h->root.u.def.value;
13145 }
13146 sym_value += rel->r_addend;
4c52953f 13147
70cc837d
AM
13148 /* If this branch reloc uses an opd sym, find the code section. */
13149 opd = get_opd_info (sym_sec);
13150 if (opd != NULL)
13151 {
13152 if (h == NULL && opd->adjust != NULL)
13153 {
13154 long adjust;
4c52953f 13155
92a9c616 13156 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
13157 if (adjust == -1)
13158 /* Assume deleted functions won't ever be called. */
13159 continue;
13160 sym_value += adjust;
13161 }
4c52953f 13162
aef36ac1 13163 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13164 &sym_sec, NULL, false);
70cc837d
AM
13165 if (dest == (bfd_vma) -1)
13166 continue;
13167 }
13168 else
13169 dest = (sym_value
13170 + sym_sec->output_offset
13171 + sym_sec->output_section->vma);
4c52953f 13172
70cc837d
AM
13173 /* Ignore branch to self. */
13174 if (sym_sec == isec)
13175 continue;
4c52953f 13176
70cc837d
AM
13177 /* If the called function uses the toc, we need a stub. */
13178 if (sym_sec->has_toc_reloc
13179 || sym_sec->makes_toc_func_call)
4c52953f 13180 {
70cc837d 13181 ret = 1;
4c52953f
AM
13182 break;
13183 }
70cc837d
AM
13184
13185 /* Assume any branch that needs a long branch stub might in fact
13186 need a plt_branch stub. A plt_branch stub uses r2. */
13187 else if (dest - (isec->output_offset
13188 + isec->output_section->vma
6911b7dc
AM
13189 + rel->r_offset) + (1 << 25)
13190 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13191 ? h->other
13192 : sym->st_other))
4c52953f 13193 {
70cc837d
AM
13194 ret = 1;
13195 break;
13196 }
13197
13198 /* If calling back to a section in the process of being
13199 tested, we can't say for sure that no toc adjusting stubs
13200 are needed, so don't return zero. */
13201 else if (sym_sec->call_check_in_progress)
13202 ret = 2;
13203
13204 /* Branches to another section that itself doesn't have any TOC
13205 references are OK. Recursively call ourselves to check. */
13206 else if (!sym_sec->call_check_done)
13207 {
13208 int recur;
13209
13210 /* Mark current section as indeterminate, so that other
13211 sections that call back to current won't be marked as
13212 known. */
13213 isec->call_check_in_progress = 1;
13214 recur = toc_adjusting_stub_needed (info, sym_sec);
13215 isec->call_check_in_progress = 0;
13216
4c52953f
AM
13217 if (recur != 0)
13218 {
70cc837d
AM
13219 ret = recur;
13220 if (recur != 2)
13221 break;
4c52953f
AM
13222 }
13223 }
4c52953f 13224 }
70cc837d 13225
c9594989
AM
13226 if (elf_symtab_hdr (isec->owner).contents
13227 != (unsigned char *) local_syms)
70cc837d
AM
13228 free (local_syms);
13229 if (elf_section_data (isec)->relocs != relstart)
13230 free (relstart);
9b5ecbd0
AM
13231 }
13232
70cc837d
AM
13233 if ((ret & 1) == 0
13234 && isec->map_head.s != NULL
13235 && (strcmp (isec->output_section->name, ".init") == 0
13236 || strcmp (isec->output_section->name, ".fini") == 0))
13237 {
13238 if (isec->map_head.s->has_toc_reloc
13239 || isec->map_head.s->makes_toc_func_call)
13240 ret = 1;
13241 else if (!isec->map_head.s->call_check_done)
13242 {
13243 int recur;
13244 isec->call_check_in_progress = 1;
13245 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13246 isec->call_check_in_progress = 0;
13247 if (recur != 0)
13248 ret = recur;
13249 }
13250 }
13251
13252 if (ret == 1)
13253 isec->makes_toc_func_call = 1;
4c52953f 13254
9b5ecbd0
AM
13255 return ret;
13256}
13257
721956f4
AM
13258/* The linker repeatedly calls this function for each input section,
13259 in the order that input sections are linked into output sections.
13260 Build lists of input sections to determine groupings between which
13261 we may insert linker stubs. */
13262
0a1b45a2 13263bool
4ce794b7 13264ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
13265{
13266 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13267
4dfe6ac6 13268 if (htab == NULL)
0a1b45a2 13269 return false;
4dfe6ac6 13270
734b6cf9 13271 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 13272 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 13273 {
3d6f9012
AM
13274 /* This happens to make the list in reverse order,
13275 which is what we want. */
6f20ed8a
AM
13276 htab->sec_info[isec->id].u.list
13277 = htab->sec_info[isec->output_section->id].u.list;
13278 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 13279 }
ad8e1ba5 13280
4c52953f 13281 if (htab->multi_toc_needed)
9b5ecbd0 13282 {
8b974ba3
AM
13283 /* Analyse sections that aren't already flagged as needing a
13284 valid toc pointer. Exclude .fixup for the linux kernel.
13285 .fixup contains branches, but only back to the function that
13286 hit an exception. */
13287 if (!(isec->has_toc_reloc
13288 || (isec->flags & SEC_CODE) == 0
13289 || strcmp (isec->name, ".fixup") == 0
13290 || isec->call_check_done))
13291 {
13292 if (toc_adjusting_stub_needed (info, isec) < 0)
0a1b45a2 13293 return false;
8b974ba3
AM
13294 }
13295 /* Make all sections use the TOC assigned for this object file.
13296 This will be wrong for pasted sections; We fix that in
13297 check_pasted_section(). */
13298 if (elf_gp (isec->owner) != 0)
13299 htab->toc_curr = elf_gp (isec->owner);
13300 }
13301
6f20ed8a 13302 htab->sec_info[isec->id].toc_off = htab->toc_curr;
0a1b45a2 13303 return true;
721956f4
AM
13304}
13305
70cc837d
AM
13306/* Check that all .init and .fini sections use the same toc, if they
13307 have toc relocs. */
13308
0a1b45a2 13309static bool
70cc837d
AM
13310check_pasted_section (struct bfd_link_info *info, const char *name)
13311{
13312 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13313
13314 if (o != NULL)
13315 {
13316 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13317 bfd_vma toc_off = 0;
13318 asection *i;
13319
13320 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13321 if (i->has_toc_reloc)
13322 {
13323 if (toc_off == 0)
6f20ed8a
AM
13324 toc_off = htab->sec_info[i->id].toc_off;
13325 else if (toc_off != htab->sec_info[i->id].toc_off)
0a1b45a2 13326 return false;
70cc837d 13327 }
6683a28d
AM
13328
13329 if (toc_off == 0)
13330 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13331 if (i->makes_toc_func_call)
13332 {
6f20ed8a 13333 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13334 break;
13335 }
13336
70cc837d
AM
13337 /* Make sure the whole pasted function uses the same toc offset. */
13338 if (toc_off != 0)
13339 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13340 htab->sec_info[i->id].toc_off = toc_off;
70cc837d 13341 }
0a1b45a2 13342 return true;
70cc837d
AM
13343}
13344
0a1b45a2 13345bool
70cc837d
AM
13346ppc64_elf_check_init_fini (struct bfd_link_info *info)
13347{
0c80c644
SM
13348 bool ret1 = check_pasted_section (info, ".init");
13349 bool ret2 = check_pasted_section (info, ".fini");
13350
13351 return ret1 && ret2;
70cc837d
AM
13352}
13353
721956f4
AM
13354/* See whether we can group stub sections together. Grouping stub
13355 sections may result in fewer stubs. More importantly, we need to
13356 put all .init* and .fini* stubs at the beginning of the .init or
13357 .fini output sections respectively, because glibc splits the
13358 _init and _fini functions into multiple parts. Putting a stub in
13359 the middle of a function is not a good idea. */
13360
0a1b45a2 13361static bool
6f20ed8a 13362group_sections (struct bfd_link_info *info,
4ce794b7 13363 bfd_size_type stub_group_size,
0a1b45a2 13364 bool stubs_always_before_branch)
721956f4 13365{
6f20ed8a
AM
13366 struct ppc_link_hash_table *htab;
13367 asection *osec;
0a1b45a2 13368 bool suppress_size_errors;
7c8fe5c4 13369
6f20ed8a
AM
13370 htab = ppc_hash_table (info);
13371 if (htab == NULL)
0a1b45a2 13372 return false;
6f20ed8a 13373
0a1b45a2 13374 suppress_size_errors = false;
7c8fe5c4
AM
13375 if (stub_group_size == 1)
13376 {
13377 /* Default values. */
13378 if (stubs_always_before_branch)
09f92717 13379 stub_group_size = 0x1e00000;
7c8fe5c4 13380 else
09f92717 13381 stub_group_size = 0x1c00000;
0a1b45a2 13382 suppress_size_errors = true;
7c8fe5c4
AM
13383 }
13384
6f20ed8a 13385 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13386 {
6f20ed8a
AM
13387 asection *tail;
13388
13389 if (osec->id >= htab->sec_info_arr_size)
13390 continue;
13391
13392 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13393 while (tail != NULL)
721956f4 13394 {
734b6cf9
AM
13395 asection *curr;
13396 asection *prev;
13397 bfd_size_type total;
0a1b45a2 13398 bool big_sec;
734b6cf9 13399 bfd_vma curr_toc;
6f20ed8a 13400 struct map_stub *group;
09f92717 13401 bfd_size_type group_size;
734b6cf9
AM
13402
13403 curr = tail;
eea6121a 13404 total = tail->size;
09f92717
AM
13405 group_size = (ppc64_elf_section_data (tail) != NULL
13406 && ppc64_elf_section_data (tail)->has_14bit_branch
13407 ? stub_group_size >> 10 : stub_group_size);
13408
13409 big_sec = total > group_size;
7c8fe5c4 13410 if (big_sec && !suppress_size_errors)
695344c0 13411 /* xgettext:c-format */
871b3ab2 13412 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13413 tail->owner, tail);
6f20ed8a 13414 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13415
6f20ed8a 13416 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13417 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13418 < (ppc64_elf_section_data (prev) != NULL
13419 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13420 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13421 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13422 curr = prev;
13423
13424 /* OK, the size from the start of CURR to the end is less
09f92717 13425 than group_size and thus can be handled by one stub
734b6cf9 13426 section. (or the tail section is itself larger than
09f92717
AM
13427 group_size, in which case we may be toast.) We should
13428 really be keeping track of the total size of stubs added
13429 here, as stubs contribute to the final output section
13430 size. That's a little tricky, and this way will only
13431 break if stubs added make the total size more than 2^25,
13432 ie. for the default stub_group_size, if stubs total more
13433 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13434 group = bfd_alloc (curr->owner, sizeof (*group));
13435 if (group == NULL)
0a1b45a2 13436 return false;
6f20ed8a
AM
13437 group->link_sec = curr;
13438 group->stub_sec = NULL;
a4b6fadd 13439 group->needs_save_res = 0;
df136d64
AM
13440 group->lr_restore = 0;
13441 group->eh_size = 0;
13442 group->eh_base = 0;
a4b6fadd
AM
13443 group->next = htab->group;
13444 htab->group = group;
734b6cf9 13445 do
721956f4 13446 {
6f20ed8a 13447 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13448 /* Set up this stub group. */
6f20ed8a 13449 htab->sec_info[tail->id].u.group = group;
721956f4 13450 }
734b6cf9
AM
13451 while (tail != curr && (tail = prev) != NULL);
13452
09f92717 13453 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13454 bytes before the stub section can be handled by it too.
13455 Don't do this if we have a really large section after the
13456 stubs, as adding more stubs increases the chance that
13457 branches may not reach into the stub section. */
13458 if (!stubs_always_before_branch && !big_sec)
13459 {
13460 total = 0;
13461 while (prev != NULL
13462 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13463 < (ppc64_elf_section_data (prev) != NULL
13464 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13465 ? (group_size = stub_group_size >> 10)
13466 : group_size))
6f20ed8a 13467 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13468 {
13469 tail = prev;
6f20ed8a
AM
13470 prev = htab->sec_info[tail->id].u.list;
13471 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13472 }
13473 }
13474 tail = prev;
721956f4
AM
13475 }
13476 }
0a1b45a2 13477 return true;
721956f4
AM
13478}
13479
58d180e8
AM
13480static const unsigned char glink_eh_frame_cie[] =
13481{
13482 0, 0, 0, 16, /* length. */
13483 0, 0, 0, 0, /* id. */
13484 1, /* CIE version. */
13485 'z', 'R', 0, /* Augmentation string. */
13486 4, /* Code alignment. */
13487 0x78, /* Data alignment. */
13488 65, /* RA reg. */
13489 1, /* Augmentation size. */
13490 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13491 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13492};
13493
d969d15f
AM
13494/* Stripping output sections is normally done before dynamic section
13495 symbols have been allocated. This function is called later, and
13496 handles cases like htab->brlt which is mapped to its own output
13497 section. */
13498
13499static void
13500maybe_strip_output (struct bfd_link_info *info, asection *isec)
13501{
13502 if (isec->size == 0
13503 && isec->output_section->size == 0
53d8967a 13504 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13505 && !bfd_section_removed_from_list (info->output_bfd,
13506 isec->output_section)
13507 && elf_section_data (isec->output_section)->dynindx == 0)
13508 {
13509 isec->output_section->flags |= SEC_EXCLUDE;
13510 bfd_section_list_remove (info->output_bfd, isec->output_section);
13511 info->output_bfd->section_count--;
13512 }
13513}
13514
0aac2413
AM
13515/* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13516 the array of such relocs. */
1657026c
AM
13517
13518static bool
0aac2413 13519append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
1657026c
AM
13520{
13521 if (htab->relr_count >= htab->relr_alloc)
13522 {
13523 if (htab->relr_alloc == 0)
13524 htab->relr_alloc = 4096;
13525 else
13526 htab->relr_alloc *= 2;
0aac2413
AM
13527 htab->relr = bfd_realloc (htab->relr,
13528 htab->relr_alloc * sizeof (*htab->relr));
13529 if (htab->relr == NULL)
1657026c
AM
13530 return false;
13531 }
0aac2413
AM
13532 htab->relr[htab->relr_count].sec = sec;
13533 htab->relr[htab->relr_count].off = off;
13534 htab->relr_count++;
1657026c
AM
13535 return true;
13536}
13537
0aac2413
AM
13538/* qsort comparator for bfd_vma args. */
13539
13540static int
13541compare_relr_address (const void *arg1, const void *arg2)
13542{
13543 bfd_vma a = *(bfd_vma *) arg1;
13544 bfd_vma b = *(bfd_vma *) arg2;
13545 return a < b ? -1 : a > b ? 1 : 0;
13546}
13547
13548/* Produce a malloc'd sorted array of reloc addresses from the info
13549 stored by append_relr_off. */
13550
13551static bfd_vma *
13552sort_relr (struct ppc_link_hash_table *htab)
13553{
13554 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13555 if (addr == NULL)
13556 return NULL;
13557
13558 for (size_t i = 0; i < htab->relr_count; i++)
13559 addr[i] = (htab->relr[i].sec->output_section->vma
13560 + htab->relr[i].sec->output_offset
13561 + htab->relr[i].off);
13562
13563 if (htab->relr_count > 1)
13564 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13565
13566 return addr;
13567}
13568
13569/* Look over GOT and PLT entries saved on elf_local_got_ents for all
e26ff4b5 13570 input files, stashing info about needed relative relocs. */
0aac2413 13571
1657026c
AM
13572static bool
13573got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13574{
13575 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13576 bfd *ibfd;
13577
13578 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13579 {
13580 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13581 struct plt_entry **local_plt, **lplt, **end_local_plt;
13582 Elf_Internal_Shdr *symtab_hdr;
13583 bfd_size_type locsymcount;
3a3a4c1f
AM
13584 Elf_Internal_Sym *local_syms;
13585 Elf_Internal_Sym *isym;
1657026c
AM
13586 struct plt_entry *pent;
13587 struct got_entry *gent;
13588
13589 if (!is_ppc64_elf (ibfd))
13590 continue;
13591
13592 lgot_ents = elf_local_got_ents (ibfd);
13593 if (!lgot_ents)
13594 continue;
13595
13596 symtab_hdr = &elf_symtab_hdr (ibfd);
13597 locsymcount = symtab_hdr->sh_info;
3a3a4c1f
AM
13598 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13599 if (local_syms == NULL && locsymcount != 0)
13600 {
13601 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13602 0, NULL, NULL, NULL);
13603 if (local_syms == NULL)
13604 return false;
13605 }
1657026c
AM
13606 end_lgot_ents = lgot_ents + locsymcount;
13607 local_plt = (struct plt_entry **) end_lgot_ents;
13608 end_local_plt = local_plt + locsymcount;
3a3a4c1f
AM
13609 for (lgot = lgot_ents, isym = local_syms;
13610 lgot < end_lgot_ents;
13611 ++lgot, ++isym)
1657026c
AM
13612 for (gent = *lgot; gent != NULL; gent = gent->next)
13613 if (!gent->is_indirect
13614 && gent->tls_type == 0
3a3a4c1f
AM
13615 && gent->got.offset != (bfd_vma) -1
13616 && isym->st_shndx != SHN_ABS)
1657026c
AM
13617 {
13618 asection *got = ppc64_elf_tdata (gent->owner)->got;
0aac2413 13619 if (!append_relr_off (htab, got, gent->got.offset))
1657026c
AM
13620 {
13621 htab->stub_error = true;
13622 return false;
13623 }
13624 }
13625
13626 if (!htab->opd_abi)
3a3a4c1f
AM
13627 for (lplt = local_plt, isym = local_syms;
13628 lplt < end_local_plt;
13629 ++lplt, ++isym)
1657026c 13630 for (pent = *lplt; pent != NULL; pent = pent->next)
3a3a4c1f
AM
13631 if (pent->plt.offset != (bfd_vma) -1
13632 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
1657026c 13633 {
0aac2413 13634 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
1657026c 13635 {
1657026c
AM
13636 if (symtab_hdr->contents != (unsigned char *) local_syms)
13637 free (local_syms);
13638 return false;
13639 }
1657026c
AM
13640 }
13641
13642 if (local_syms != NULL
13643 && symtab_hdr->contents != (unsigned char *) local_syms)
13644 {
13645 if (!info->keep_memory)
13646 free (local_syms);
13647 else
13648 symtab_hdr->contents = (unsigned char *) local_syms;
13649 }
13650 }
13651 return true;
13652}
13653
0aac2413
AM
13654/* Stash info about needed GOT and PLT entry relative relocs for
13655 global symbol H. */
13656
1657026c
AM
13657static bool
13658got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13659{
13660 struct bfd_link_info *info;
13661 struct ppc_link_hash_table *htab;
13662 struct plt_entry *pent;
13663 struct got_entry *gent;
13664
13665 if (h->root.type == bfd_link_hash_indirect)
13666 return true;
13667
13668 info = (struct bfd_link_info *) inf;
13669 htab = ppc_hash_table (info);
13670 if (htab == NULL)
13671 return false;
13672
13673 if (h->type != STT_GNU_IFUNC
13674 && h->def_regular
13675 && (h->root.type == bfd_link_hash_defined
13676 || h->root.type == bfd_link_hash_defweak))
13677 {
3a3a4c1f
AM
13678 if ((!htab->elf.dynamic_sections_created
13679 || h->dynindx == -1
13680 || SYMBOL_REFERENCES_LOCAL (info, h))
13681 && !bfd_is_abs_symbol (&h->root))
1657026c
AM
13682 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13683 if (!gent->is_indirect
13684 && gent->tls_type == 0
13685 && gent->got.offset != (bfd_vma) -1)
13686 {
13687 asection *got = ppc64_elf_tdata (gent->owner)->got;
0aac2413 13688 if (!append_relr_off (htab, got, gent->got.offset))
1657026c
AM
13689 {
13690 htab->stub_error = true;
13691 return false;
13692 }
13693 }
13694
13695 if (!htab->opd_abi
13696 && use_local_plt (info, h))
13697 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13698 if (pent->plt.offset != (bfd_vma) -1)
13699 {
0aac2413 13700 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
1657026c
AM
13701 {
13702 htab->stub_error = true;
13703 return false;
13704 }
13705 }
13706 }
13707 return true;
13708}
13709
721956f4
AM
13710/* Determine and set the size of the stub section for a final link.
13711
13712 The basic idea here is to examine all the relocations looking for
13713 PC-relative calls to a target that is unreachable with a "bl"
13714 instruction. */
13715
0a1b45a2 13716bool
e7d1c40c 13717ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13718{
13719 bfd_size_type stub_group_size;
0a1b45a2 13720 bool stubs_always_before_branch;
721956f4
AM
13721 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13722
4dfe6ac6 13723 if (htab == NULL)
0a1b45a2 13724 return false;
4dfe6ac6 13725
e10a07b3
AM
13726 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13727 htab->params->power10_stubs = 0;
13728
0e1862bb 13729 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13730 htab->params->plt_thread_safe = 1;
b9e5796b 13731 if (!htab->opd_abi)
e7d1c40c
AM
13732 htab->params->plt_thread_safe = 0;
13733 else if (htab->params->plt_thread_safe == -1)
794e51c0 13734 {
e2458743 13735 static const char *const thread_starter[] =
794e51c0
AM
13736 {
13737 "pthread_create",
13738 /* libstdc++ */
13739 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13740 /* librt */
13741 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13742 "mq_notify", "create_timer",
13743 /* libanl */
13744 "getaddrinfo_a",
13745 /* libgomp */
2300b5a1 13746 "GOMP_parallel",
794e51c0 13747 "GOMP_parallel_start",
2300b5a1 13748 "GOMP_parallel_loop_static",
794e51c0 13749 "GOMP_parallel_loop_static_start",
2300b5a1 13750 "GOMP_parallel_loop_dynamic",
794e51c0 13751 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13752 "GOMP_parallel_loop_guided",
794e51c0 13753 "GOMP_parallel_loop_guided_start",
2300b5a1 13754 "GOMP_parallel_loop_runtime",
794e51c0 13755 "GOMP_parallel_loop_runtime_start",
2300b5a1 13756 "GOMP_parallel_sections",
68ffbac6 13757 "GOMP_parallel_sections_start",
f9dffbf0
AM
13758 /* libgo */
13759 "__go_go",
794e51c0
AM
13760 };
13761 unsigned i;
13762
a4b6fadd 13763 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13764 {
13765 struct elf_link_hash_entry *h;
13766 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
0a1b45a2 13767 false, false, true);
e7d1c40c
AM
13768 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13769 if (htab->params->plt_thread_safe)
794e51c0
AM
13770 break;
13771 }
13772 }
e7d1c40c
AM
13773 stubs_always_before_branch = htab->params->group_size < 0;
13774 if (htab->params->group_size < 0)
13775 stub_group_size = -htab->params->group_size;
721956f4 13776 else
e7d1c40c 13777 stub_group_size = htab->params->group_size;
721956f4 13778
6f20ed8a 13779 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
0a1b45a2 13780 return false;
721956f4 13781
a804e476
AM
13782 htab->tga_group = NULL;
13783 if (!htab->params->no_tls_get_addr_regsave
13784 && htab->tga_desc_fd != NULL
13785 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13786 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13787 && htab->tls_get_addr_fd != NULL
13788 && is_static_defined (&htab->tls_get_addr_fd->elf))
13789 {
13790 asection *sym_sec, *code_sec, *stub_sec;
13791 bfd_vma sym_value;
13792 struct _opd_sec_data *opd;
13793
13794 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13795 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13796 code_sec = sym_sec;
13797 opd = get_opd_info (sym_sec);
13798 if (opd != NULL)
0a1b45a2 13799 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
a804e476
AM
13800 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13801 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13802 htab->tga_group->link_sec);
13803 if (stub_sec == NULL)
0a1b45a2 13804 return false;
a804e476
AM
13805 htab->tga_group->stub_sec = stub_sec;
13806
13807 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13808 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13809 htab->tga_desc_fd->elf.root.u.def.value = 0;
13810 htab->tga_desc_fd->elf.type = STT_FUNC;
13811 htab->tga_desc_fd->elf.def_regular = 1;
13812 htab->tga_desc_fd->elf.non_elf = 0;
0a1b45a2 13813 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
a804e476
AM
13814 }
13815
c9301e31 13816 /* Loop until no stubs added. After iteration 20 of this loop we may
0441f94f 13817 exit on a stub section shrinking. */
c9301e31 13818
721956f4
AM
13819 while (1)
13820 {
13821 bfd *input_bfd;
13822 unsigned int bfd_indx;
a4b6fadd 13823 struct map_stub *group;
721956f4
AM
13824
13825 htab->stub_iteration += 1;
1657026c 13826 htab->relr_count = 0;
721956f4
AM
13827
13828 for (input_bfd = info->input_bfds, bfd_indx = 0;
13829 input_bfd != NULL;
c72f2fb2 13830 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13831 {
13832 Elf_Internal_Shdr *symtab_hdr;
13833 asection *section;
6cdc0ccc 13834 Elf_Internal_Sym *local_syms = NULL;
721956f4 13835
0c8d6e5c 13836 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13837 continue;
13838
721956f4 13839 /* We'll need the symbol table in a second. */
0ffa91dd 13840 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13841 if (symtab_hdr->sh_info == 0)
13842 continue;
13843
721956f4
AM
13844 /* Walk over each section attached to the input bfd. */
13845 for (section = input_bfd->sections;
13846 section != NULL;
13847 section = section->next)
13848 {
721956f4 13849 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1657026c 13850 bool is_opd;
721956f4
AM
13851
13852 /* If there aren't any relocs, then there's nothing more
13853 to do. */
13854 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13855 || (section->flags & SEC_ALLOC) == 0
13856 || (section->flags & SEC_LOAD) == 0
721956f4
AM
13857 || section->reloc_count == 0)
13858 continue;
13859
1657026c
AM
13860 if (!info->enable_dt_relr
13861 && (section->flags & SEC_CODE) == 0)
13862 continue;
13863
721956f4
AM
13864 /* If this section is a link-once section that will be
13865 discarded, then don't create any stubs. */
13866 if (section->output_section == NULL
927be08e 13867 || section->output_section->owner != info->output_bfd)
721956f4
AM
13868 continue;
13869
1e2f5b6e
AM
13870 /* Get the relocs. */
13871 internal_relocs
4ce794b7 13872 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13873 info->keep_memory);
721956f4 13874 if (internal_relocs == NULL)
1e2f5b6e 13875 goto error_ret_free_local;
721956f4 13876
1657026c
AM
13877 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13878
721956f4
AM
13879 /* Now examine each relocation. */
13880 irela = internal_relocs;
13881 irelaend = irela + section->reloc_count;
13882 for (; irela < irelaend; irela++)
13883 {
4ce794b7
AM
13884 enum elf_ppc64_reloc_type r_type;
13885 unsigned int r_indx;
7aba54da 13886 struct ppc_stub_type stub_type;
721956f4 13887 struct ppc_stub_hash_entry *stub_entry;
8387904d 13888 asection *sym_sec, *code_sec;
e054468f 13889 bfd_vma sym_value, code_value;
721956f4 13890 bfd_vma destination;
6911b7dc 13891 unsigned long local_off;
0a1b45a2 13892 bool ok_dest;
721956f4 13893 struct ppc_link_hash_entry *hash;
8387904d 13894 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13895 struct elf_link_hash_entry *h;
13896 Elf_Internal_Sym *sym;
721956f4
AM
13897 char *stub_name;
13898 const asection *id_sec;
74f0fb50 13899 struct _opd_sec_data *opd;
e054468f 13900 struct plt_entry *plt_ent;
721956f4
AM
13901
13902 r_type = ELF64_R_TYPE (irela->r_info);
13903 r_indx = ELF64_R_SYM (irela->r_info);
13904
4ce794b7 13905 if (r_type >= R_PPC64_max)
721956f4
AM
13906 {
13907 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13908 goto error_ret_free_internal;
721956f4
AM
13909 }
13910
13911 /* Only look for stubs on branch instructions. */
1657026c
AM
13912 switch (r_type)
13913 {
13914 default:
f91074eb
AM
13915 if (info->enable_dt_relr
13916 && maybe_relr (r_type, irela, section))
13917 break;
1657026c
AM
13918 continue;
13919
13920 case R_PPC64_REL24:
13921 case R_PPC64_REL24_NOTOC:
13922 case R_PPC64_REL24_P9NOTOC:
13923 case R_PPC64_REL14:
13924 case R_PPC64_REL14_BRTAKEN:
13925 case R_PPC64_REL14_BRNTAKEN:
13926 if ((section->flags & SEC_CODE) != 0)
13927 break;
13928 continue;
1657026c 13929 }
721956f4
AM
13930
13931 /* Now determine the call target, its name, value,
13932 section. */
411e1bfb
AM
13933 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13934 r_indx, input_bfd))
13935 goto error_ret_free_internal;
411e1bfb 13936
1657026c
AM
13937 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13938 {
13939 /* Only locally defined symbols can possibly use
13940 relative relocations. */
13941 bfd_vma r_offset;
13942 if ((sym_sec == NULL
13943 || sym_sec->output_section == NULL)
13944 /* No symbol is OK too. */
13945 && !(sym != NULL && sym->st_shndx == 0)
13946 /* Hack for __ehdr_start, which is undefined
13947 at this point. */
13948 && !(h != NULL && h->root.linker_def))
13949 continue;
13950 if (NO_OPD_RELOCS && is_opd)
13951 continue;
13952 if (!is_opd
13953 && r_type == R_PPC64_ADDR64)
13954 {
13955 if (h != NULL
13956 ? h->type == STT_GNU_IFUNC
13957 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13958 continue;
3a3a4c1f
AM
13959 if (h != NULL
13960 ? bfd_is_abs_symbol (&h->root)
13961 : sym->st_shndx == SHN_ABS)
13962 continue;
1657026c
AM
13963 if (h != NULL
13964 && !SYMBOL_REFERENCES_LOCAL (info, h))
13965 continue;
13966 }
13967 r_offset = _bfd_elf_section_offset (info->output_bfd,
13968 info,
13969 section,
13970 irela->r_offset);
13971 if (r_offset >= (bfd_vma) -2)
13972 continue;
0aac2413 13973 if (!append_relr_off (htab, section, r_offset))
1657026c
AM
13974 goto error_ret_free_internal;
13975 continue;
13976 }
13977
13978 hash = ppc_elf_hash_entry (h);
0a1b45a2 13979 ok_dest = false;
8387904d 13980 fdh = NULL;
7fe2b9a6 13981 sym_value = 0;
411e1bfb 13982 if (hash == NULL)
721956f4 13983 {
411e1bfb 13984 sym_value = sym->st_value;
c27b8c2a
AM
13985 if (sym_sec != NULL
13986 && sym_sec->output_section != NULL)
0a1b45a2 13987 ok_dest = true;
721956f4 13988 }
7fe2b9a6
AM
13989 else if (hash->elf.root.type == bfd_link_hash_defined
13990 || hash->elf.root.type == bfd_link_hash_defweak)
13991 {
13992 sym_value = hash->elf.root.u.def.value;
13993 if (sym_sec->output_section != NULL)
0a1b45a2 13994 ok_dest = true;
7fe2b9a6
AM
13995 }
13996 else if (hash->elf.root.type == bfd_link_hash_undefweak
13997 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13998 {
99877b66 13999 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
14000 use the func descriptor sym instead if it is
14001 defined. */
ceb1f1ef 14002 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 14003 && hash->oh != NULL)
8387904d 14004 {
8c5b4e52 14005 fdh = ppc_follow_link (hash->oh);
8387904d
AM
14006 if (fdh->elf.root.type == bfd_link_hash_defined
14007 || fdh->elf.root.type == bfd_link_hash_defweak)
14008 {
14009 sym_sec = fdh->elf.root.u.def.section;
14010 sym_value = fdh->elf.root.u.def.value;
14011 if (sym_sec->output_section != NULL)
0a1b45a2 14012 ok_dest = true;
8387904d 14013 }
99877b66
AM
14014 else
14015 fdh = NULL;
8387904d 14016 }
7fe2b9a6
AM
14017 }
14018 else
14019 {
14020 bfd_set_error (bfd_error_bad_value);
14021 goto error_ret_free_internal;
721956f4
AM
14022 }
14023
8843416a 14024 destination = 0;
6911b7dc 14025 local_off = 0;
8843416a
AM
14026 if (ok_dest)
14027 {
14028 sym_value += irela->r_addend;
14029 destination = (sym_value
14030 + sym_sec->output_offset
14031 + sym_sec->output_section->vma);
6911b7dc
AM
14032 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
14033 ? hash->elf.other
14034 : sym->st_other);
8843416a
AM
14035 }
14036
8387904d 14037 code_sec = sym_sec;
e054468f 14038 code_value = sym_value;
74f0fb50
AM
14039 opd = get_opd_info (sym_sec);
14040 if (opd != NULL)
8387904d
AM
14041 {
14042 bfd_vma dest;
14043
74f0fb50 14044 if (hash == NULL && opd->adjust != NULL)
8387904d 14045 {
51aecdc5 14046 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
14047 if (adjust == -1)
14048 continue;
e054468f 14049 code_value += adjust;
8387904d
AM
14050 sym_value += adjust;
14051 }
14052 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 14053 &code_sec, &code_value, false);
8387904d
AM
14054 if (dest != (bfd_vma) -1)
14055 {
14056 destination = dest;
14057 if (fdh != NULL)
14058 {
14059 /* Fixup old ABI sym to point at code
14060 entry. */
99877b66 14061 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 14062 hash->elf.root.u.def.section = code_sec;
e054468f 14063 hash->elf.root.u.def.value = code_value;
8387904d
AM
14064 }
14065 }
14066 }
14067
721956f4 14068 /* Determine what (if any) linker stub is needed. */
e054468f 14069 plt_ent = NULL;
7aba54da
AM
14070 stub_type.main = ppc_type_of_stub (section, irela, &hash,
14071 &plt_ent, destination,
14072 local_off);
14073 stub_type.sub = ppc_stub_toc;
14074 stub_type.r2save = 0;
14075
14076 if (r_type == R_PPC64_REL24_NOTOC
14077 || r_type == R_PPC64_REL24_P9NOTOC)
05d0e962 14078 {
7aba54da
AM
14079 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14080 if (htab->params->power10_stubs == 0
14081 || (r_type == R_PPC64_REL24_P9NOTOC
14082 && htab->params->power10_stubs != 1))
14083 notoc = ppc_stub_p9notoc;
14084 if (stub_type.main == ppc_stub_plt_call)
14085 stub_type.sub = notoc;
14086 else if (stub_type.main == ppc_stub_long_branch
05d0e962
AM
14087 || (code_sec != NULL
14088 && code_sec->output_section != NULL
14089 && (((hash ? hash->elf.other : sym->st_other)
14090 & STO_PPC64_LOCAL_MASK)
4a4e7361 14091 > 1 << STO_PPC64_LOCAL_BIT)))
7aba54da
AM
14092 {
14093 stub_type.main = ppc_stub_long_branch;
14094 stub_type.sub = notoc;
14095 stub_type.r2save = 0;
14096 }
05d0e962 14097 }
7aba54da 14098 else if (stub_type.main != ppc_stub_plt_call)
ad8e1ba5
AM
14099 {
14100 /* Check whether we need a TOC adjusting stub.
14101 Since the linker pastes together pieces from
14102 different object files when creating the
14103 _init and _fini functions, it may be that a
14104 call to what looks like a local sym is in
14105 fact a call needing a TOC adjustment. */
33cb30a1
AM
14106 if ((code_sec != NULL
14107 && code_sec->output_section != NULL
33cb30a1 14108 && (code_sec->has_toc_reloc
918dc783
AM
14109 || code_sec->makes_toc_func_call)
14110 && (htab->sec_info[code_sec->id].toc_off
14111 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
14112 || (((hash ? hash->elf.other : sym->st_other)
14113 & STO_PPC64_LOCAL_MASK)
14114 == 1 << STO_PPC64_LOCAL_BIT))
7aba54da
AM
14115 {
14116 stub_type.main = ppc_stub_long_branch;
14117 stub_type.sub = ppc_stub_toc;
14118 stub_type.r2save = 1;
14119 }
ad8e1ba5
AM
14120 }
14121
7aba54da 14122 if (stub_type.main == ppc_stub_none)
721956f4
AM
14123 continue;
14124
411e1bfb 14125 /* __tls_get_addr calls might be eliminated. */
7aba54da 14126 if (stub_type.main != ppc_stub_plt_call
411e1bfb 14127 && hash != NULL
ed7007c1 14128 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
14129 && section->has_tls_reloc
14130 && irela != internal_relocs)
14131 {
14132 /* Get tls info. */
f961d9dd 14133 unsigned char *tls_mask;
411e1bfb 14134
3a71aa26 14135 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
14136 irela - 1, input_bfd))
14137 goto error_ret_free_internal;
abc489c6
AM
14138 if ((*tls_mask & TLS_TLS) != 0
14139 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
14140 continue;
14141 }
14142
7aba54da
AM
14143 if (stub_type.main == ppc_stub_plt_call
14144 && stub_type.sub == ppc_stub_toc)
794e51c0 14145 {
6e1816be
AM
14146 if (!htab->opd_abi
14147 && htab->params->plt_localentry0 != 0
14148 && is_elfv2_localentry0 (&hash->elf))
14149 htab->has_plt_localentry0 = 1;
14150 else if (irela + 1 < irelaend
14151 && irela[1].r_offset == irela->r_offset + 4
14152 && (ELF64_R_TYPE (irela[1].r_info)
14153 == R_PPC64_TOCSAVE))
f378ab09
AM
14154 {
14155 if (!tocsave_find (htab, INSERT,
14156 &local_syms, irela + 1, input_bfd))
14157 goto error_ret_free_internal;
14158 }
f378ab09 14159 else
7aba54da 14160 stub_type.r2save = 1;
794e51c0 14161 }
3b421ab3 14162
721956f4 14163 /* Support for grouping stub sections. */
6f20ed8a 14164 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
14165
14166 /* Get the name of this stub. */
14167 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14168 if (!stub_name)
14169 goto error_ret_free_internal;
14170
14171 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 14172 stub_name, false, false);
721956f4
AM
14173 if (stub_entry != NULL)
14174 {
721956f4 14175 free (stub_name);
7aba54da 14176 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
e10a07b3 14177 {
7aba54da
AM
14178 /* xgettext:c-format */
14179 _bfd_error_handler
14180 (_("%pB: cannot create stub entry %s"),
14181 section->owner, stub_entry->root.string);
14182 goto error_ret_free_internal;
e10a07b3 14183 }
721956f4
AM
14184 continue;
14185 }
14186
25f53a85 14187 stub_entry = ppc_add_stub (stub_name, section, info);
aefcec16 14188 free (stub_name);
721956f4
AM
14189 if (stub_entry == NULL)
14190 {
6cdc0ccc
AM
14191 error_ret_free_internal:
14192 if (elf_section_data (section)->relocs == NULL)
14193 free (internal_relocs);
14194 error_ret_free_local:
c9594989
AM
14195 if (symtab_hdr->contents
14196 != (unsigned char *) local_syms)
6cdc0ccc 14197 free (local_syms);
0a1b45a2 14198 return false;
721956f4
AM
14199 }
14200
7aba54da
AM
14201 stub_entry->type = stub_type;
14202 if (stub_type.main == ppc_stub_plt_call)
e054468f 14203 {
05d0e962
AM
14204 stub_entry->target_value = sym_value;
14205 stub_entry->target_section = sym_sec;
e054468f
AM
14206 }
14207 else
14208 {
05d0e962
AM
14209 stub_entry->target_value = code_value;
14210 stub_entry->target_section = code_sec;
e054468f 14211 }
721956f4 14212 stub_entry->h = hash;
e054468f 14213 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
14214 stub_entry->symtype
14215 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 14216 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 14217
3d58e1fc
AM
14218 if (hash != NULL
14219 && (hash->elf.root.type == bfd_link_hash_defined
14220 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 14221 htab->stub_globals += 1;
721956f4
AM
14222 }
14223
14224 /* We're done with the internal relocs, free them. */
6cdc0ccc 14225 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 14226 free (internal_relocs);
721956f4 14227 }
6cdc0ccc
AM
14228
14229 if (local_syms != NULL
14230 && symtab_hdr->contents != (unsigned char *) local_syms)
14231 {
14232 if (!info->keep_memory)
14233 free (local_syms);
14234 else
14235 symtab_hdr->contents = (unsigned char *) local_syms;
14236 }
721956f4
AM
14237 }
14238
5c3dead3 14239 /* We may have added some stubs. Find out the new size of the
721956f4 14240 stub sections. */
d4aaa2a0 14241 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14242 {
14243 group->lr_restore = 0;
14244 group->eh_size = 0;
14245 if (group->stub_sec != NULL)
14246 {
14247 asection *stub_sec = group->stub_sec;
14248
9ff8aa7d 14249 stub_sec->rawsize = stub_sec->size;
df136d64
AM
14250 stub_sec->size = 0;
14251 stub_sec->reloc_count = 0;
14252 stub_sec->flags &= ~SEC_RELOC;
14253 }
14254 }
a804e476
AM
14255 if (htab->tga_group != NULL)
14256 {
14257 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14258 htab->tga_group->eh_size
14259 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14260 htab->tga_group->lr_restore = 23 * 4;
14261 htab->tga_group->stub_sec->size = 24 * 4;
14262 }
eea6121a 14263
9ff8aa7d 14264 htab->brlt->rawsize = htab->brlt->size;
eea6121a 14265 htab->brlt->size = 0;
84f5d08e
AM
14266 htab->brlt->reloc_count = 0;
14267 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 14268 if (htab->relbrlt != NULL)
eea6121a 14269 htab->relbrlt->size = 0;
721956f4 14270
1657026c
AM
14271 if (htab->elf.srelrdyn != NULL)
14272 {
9ff8aa7d 14273 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
1657026c
AM
14274 htab->elf.srelrdyn->size = 0;
14275 }
14276
0441f94f 14277 htab->stub_changed = false;
ecc741e6 14278 htab->stub_id = 0;
63bc6f6c 14279 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 14280
a4b6fadd
AM
14281 for (group = htab->group; group != NULL; group = group->next)
14282 if (group->needs_save_res)
14283 group->stub_sec->size += htab->sfpr->size;
14284
176a0d42
AM
14285 if (info->emitrelocations
14286 && htab->glink != NULL && htab->glink->size != 0)
14287 {
14288 htab->glink->reloc_count = 1;
14289 htab->glink->flags |= SEC_RELOC;
14290 }
14291
58d180e8
AM
14292 if (htab->glink_eh_frame != NULL
14293 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 14294 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 14295 {
2e0ce1c8 14296 size_t size = 0, align = 4;
58d180e8 14297
d4aaa2a0 14298 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14299 if (group->eh_size != 0)
14300 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 14301 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 14302 size += (24 + align - 1) & -align;
58d180e8 14303 if (size != 0)
2e0ce1c8
AM
14304 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14305 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14306 size = (size + align - 1) & -align;
58d180e8
AM
14307 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14308 htab->glink_eh_frame->size = size;
14309 }
14310
e7d1c40c 14311 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14312 for (group = htab->group; group != NULL; group = group->next)
14313 if (group->stub_sec != NULL)
691d2e9a
AM
14314 {
14315 int align = abs (htab->params->plt_stub_align);
14316 group->stub_sec->size
14317 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14318 }
d4aaa2a0 14319
1657026c
AM
14320 if (htab->elf.srelrdyn != NULL)
14321 {
14322 bfd_vma r_offset;
14323
14324 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
0aac2413 14325 if (!append_relr_off (htab, htab->brlt, r_offset))
1657026c
AM
14326 return false;
14327
14328 if (!got_and_plt_relr_for_local_syms (info))
14329 return false;
14330 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14331 if (htab->stub_error)
14332 return false;
14333
0aac2413
AM
14334 bfd_vma *relr_addr = sort_relr (htab);
14335 if (htab->relr_count != 0 && relr_addr == NULL)
14336 return false;
1657026c
AM
14337
14338 size_t i = 0;
14339 while (i < htab->relr_count)
14340 {
0aac2413 14341 bfd_vma base = relr_addr[i];
1657026c
AM
14342 htab->elf.srelrdyn->size += 8;
14343 i++;
14344 /* Handle possible duplicate address. This can happen
14345 as sections increase in size when adding stubs. */
14346 while (i < htab->relr_count
0aac2413 14347 && relr_addr[i] == base)
1657026c
AM
14348 i++;
14349 base += 8;
14350 while (1)
14351 {
14352 size_t start_i = i;
14353 while (i < htab->relr_count
0aac2413
AM
14354 && relr_addr[i] - base < 63 * 8
14355 && (relr_addr[i] - base) % 8 == 0)
1657026c
AM
14356 i++;
14357 if (i == start_i)
14358 break;
14359 htab->elf.srelrdyn->size += 8;
14360 base += 63 * 8;
14361 }
14362 }
0aac2413 14363 free (relr_addr);
1657026c
AM
14364 }
14365
d4aaa2a0
AM
14366 for (group = htab->group; group != NULL; group = group->next)
14367 if (group->stub_sec != NULL
14368 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 14369 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 14370 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
14371 break;
14372
d4aaa2a0 14373 if (group == NULL
0441f94f
AM
14374 && (!htab->stub_changed
14375 || htab->stub_iteration > STUB_SHRINK_ITER)
ba21f564
AM
14376 && (htab->brlt->rawsize == htab->brlt->size
14377 || (htab->stub_iteration > STUB_SHRINK_ITER
14378 && htab->brlt->rawsize > htab->brlt->size))
1657026c
AM
14379 && (htab->elf.srelrdyn == NULL
14380 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14381 || (htab->stub_iteration > STUB_SHRINK_ITER
14382 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
58d180e8 14383 && (htab->glink_eh_frame == NULL
a804e476
AM
14384 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14385 && (htab->tga_group == NULL
14386 || htab->stub_iteration > 1))
5c3dead3
AM
14387 break;
14388
9ff8aa7d
AM
14389 if (htab->stub_iteration > STUB_SHRINK_ITER)
14390 {
14391 for (group = htab->group; group != NULL; group = group->next)
14392 if (group->stub_sec != NULL
14393 && group->stub_sec->size < group->stub_sec->rawsize)
14394 group->stub_sec->size = group->stub_sec->rawsize;
14395
14396 if (htab->brlt->size < htab->brlt->rawsize)
14397 htab->brlt->size = htab->brlt->rawsize;
14398
14399 if (htab->elf.srelrdyn != NULL
14400 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14401 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14402 }
14403
721956f4 14404 /* Ask the linker to do its stuff. */
e7d1c40c 14405 (*htab->params->layout_sections_again) ();
721956f4
AM
14406 }
14407
da44f4e5
AM
14408 if (htab->glink_eh_frame != NULL
14409 && htab->glink_eh_frame->size != 0)
14410 {
14411 bfd_vma val;
14412 bfd_byte *p, *last_fde;
14413 size_t last_fde_len, size, align, pad;
d4aaa2a0 14414 struct map_stub *group;
da44f4e5 14415
df136d64
AM
14416 /* It is necessary to at least have a rough outline of the
14417 linker generated CIEs and FDEs written before
14418 bfd_elf_discard_info is run, in order for these FDEs to be
14419 indexed in .eh_frame_hdr. */
da44f4e5
AM
14420 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14421 if (p == NULL)
0a1b45a2 14422 return false;
da44f4e5 14423 htab->glink_eh_frame->contents = p;
cb6326b5 14424 htab->glink_eh_frame->alloced = 1;
da44f4e5 14425 last_fde = p;
2e0ce1c8 14426 align = 4;
da44f4e5
AM
14427
14428 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14429 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 14430 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 14431 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 14432 p += last_fde_len + 4;
da44f4e5 14433
d4aaa2a0 14434 for (group = htab->group; group != NULL; group = group->next)
df136d64 14435 if (group->eh_size != 0)
da44f4e5 14436 {
df136d64 14437 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 14438 last_fde = p;
df136d64 14439 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 14440 /* FDE length. */
2e0ce1c8 14441 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14442 p += 4;
14443 /* CIE pointer. */
14444 val = p - htab->glink_eh_frame->contents;
14445 bfd_put_32 (htab->elf.dynobj, val, p);
14446 p += 4;
14447 /* Offset to stub section, written later. */
14448 p += 4;
14449 /* stub section size. */
d4aaa2a0 14450 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
14451 p += 4;
14452 /* Augmentation. */
14453 p += 1;
df136d64
AM
14454 /* Make sure we don't have all nops. This is enough for
14455 elf-eh-frame.c to detect the last non-nop opcode. */
14456 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 14457 p = last_fde + last_fde_len + 4;
da44f4e5
AM
14458 }
14459 if (htab->glink != NULL && htab->glink->size != 0)
14460 {
14461 last_fde = p;
2e0ce1c8 14462 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 14463 /* FDE length. */
2e0ce1c8 14464 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14465 p += 4;
14466 /* CIE pointer. */
14467 val = p - htab->glink_eh_frame->contents;
14468 bfd_put_32 (htab->elf.dynobj, val, p);
14469 p += 4;
14470 /* Offset to .glink, written later. */
14471 p += 4;
14472 /* .glink size. */
14473 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14474 p += 4;
14475 /* Augmentation. */
14476 p += 1;
14477
3cd7c7d7 14478 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
14479 *p++ = DW_CFA_register;
14480 *p++ = 65;
9f08fa5c 14481 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 14482 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
14483 *p++ = DW_CFA_restore_extended;
14484 *p++ = 65;
2e0ce1c8 14485 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
14486 }
14487 /* Subsume any padding into the last FDE if user .eh_frame
14488 sections are aligned more than glink_eh_frame. Otherwise any
14489 zero padding will be seen as a terminator. */
2e0ce1c8 14490 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 14491 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 14492 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
14493 htab->glink_eh_frame->size = size + pad;
14494 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14495 }
14496
d969d15f 14497 maybe_strip_output (info, htab->brlt);
2efec98b
AM
14498 if (htab->relbrlt != NULL)
14499 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
14500 if (htab->glink_eh_frame != NULL)
14501 maybe_strip_output (info, htab->glink_eh_frame);
1657026c
AM
14502 if (htab->elf.srelrdyn != NULL)
14503 maybe_strip_output (info, htab->elf.srelrdyn);
721956f4 14504
0a1b45a2 14505 return true;
721956f4
AM
14506}
14507
14508/* Called after we have determined section placement. If sections
805fc799 14509 move, we'll be called again. Provide a value for TOCstart. */
721956f4 14510
805fc799 14511bfd_vma
1c865ab2 14512ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 14513{
805fc799 14514 asection *s;
a27e685f 14515 bfd_vma TOCstart, adjust;
721956f4 14516
43417696
AM
14517 if (info != NULL)
14518 {
14519 struct elf_link_hash_entry *h;
14520 struct elf_link_hash_table *htab = elf_hash_table (info);
14521
2cc15b10 14522 if (is_elf_hash_table (&htab->root)
43417696
AM
14523 && htab->hgot != NULL)
14524 h = htab->hgot;
14525 else
14526 {
2cc15b10
AM
14527 h = (struct elf_link_hash_entry *)
14528 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14529 if (is_elf_hash_table (&htab->root))
43417696
AM
14530 htab->hgot = h;
14531 }
14532 if (h != NULL
14533 && h->root.type == bfd_link_hash_defined
14534 && !h->root.linker_def
2cc15b10 14535 && (!is_elf_hash_table (&htab->root)
43417696
AM
14536 || h->def_regular))
14537 {
ed7007c1 14538 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
14539 _bfd_set_gp_value (obfd, TOCstart);
14540 return TOCstart;
14541 }
14542 }
14543
805fc799
AM
14544 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14545 order. The TOC starts where the first of these sections starts. */
14546 s = bfd_get_section_by_name (obfd, ".got");
e054468f 14547 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14548 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 14549 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14550 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 14551 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14552 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 14553 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
14554 {
14555 /* This may happen for
14556 o references to TOC base (SYM@toc / TOC[tc0]) without a
14557 .toc directive
14558 o bad linker script
14559 o --gc-sections and empty TOC sections
14560
14561 FIXME: Warn user? */
14562
14563 /* Look for a likely section. We probably won't even be
14564 using TOCstart. */
14565 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14566 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14567 | SEC_EXCLUDE))
805fc799
AM
14568 == (SEC_ALLOC | SEC_SMALL_DATA))
14569 break;
721956f4 14570 if (s == NULL)
805fc799 14571 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14572 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14573 == (SEC_ALLOC | SEC_SMALL_DATA))
14574 break;
721956f4 14575 if (s == NULL)
805fc799 14576 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14577 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14578 == SEC_ALLOC)
805fc799 14579 break;
721956f4 14580 if (s == NULL)
805fc799 14581 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14582 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14583 break;
14584 }
721956f4 14585
805fc799
AM
14586 TOCstart = 0;
14587 if (s != NULL)
14588 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14589
a27e685f
AM
14590 /* Force alignment. */
14591 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14592 TOCstart -= adjust;
1c865ab2
AM
14593 _bfd_set_gp_value (obfd, TOCstart);
14594
810d4e75 14595 if (info != NULL && s != NULL)
1c865ab2
AM
14596 {
14597 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14598
810d4e75
AM
14599 if (htab != NULL)
14600 {
14601 if (htab->elf.hgot != NULL)
14602 {
a27e685f 14603 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14604 htab->elf.hgot->root.u.def.section = s;
14605 }
14606 }
14607 else
1c865ab2 14608 {
810d4e75
AM
14609 struct bfd_link_hash_entry *bh = NULL;
14610 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f 14611 s, TOC_BASE_OFF - adjust,
0a1b45a2 14612 NULL, false, false, &bh);
1c865ab2
AM
14613 }
14614 }
805fc799 14615 return TOCstart;
721956f4
AM
14616}
14617
a345bc8d 14618/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14619 write out any global entry stubs, and PLT relocations. */
a345bc8d 14620
0a1b45a2 14621static bool
49c09209 14622build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14623{
14624 struct bfd_link_info *info;
14625 struct ppc_link_hash_table *htab;
49c09209 14626 struct plt_entry *ent;
a345bc8d
AM
14627 asection *s;
14628
14629 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 14630 return true;
a345bc8d 14631
49c09209
AM
14632 info = inf;
14633 htab = ppc_hash_table (info);
14634 if (htab == NULL)
0a1b45a2 14635 return false;
49c09209
AM
14636
14637 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14638 if (ent->plt.offset != (bfd_vma) -1)
14639 {
14640 /* This symbol has an entry in the procedure linkage
14641 table. Set it up. */
14642 Elf_Internal_Rela rela;
2d7ad24e 14643 asection *plt, *relplt;
49c09209
AM
14644 bfd_byte *loc;
14645
30845f11 14646 if (use_local_plt (info, h))
49c09209
AM
14647 {
14648 if (!(h->def_regular
14649 && (h->root.type == bfd_link_hash_defined
14650 || h->root.type == bfd_link_hash_defweak)))
14651 continue;
2d7ad24e
AM
14652 if (h->type == STT_GNU_IFUNC)
14653 {
14654 plt = htab->elf.iplt;
14655 relplt = htab->elf.irelplt;
0a1b45a2 14656 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14657 if (htab->opd_abi)
14658 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14659 else
14660 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14661 }
49c09209 14662 else
2d7ad24e
AM
14663 {
14664 plt = htab->pltlocal;
1657026c
AM
14665 relplt = NULL;
14666 if (bfd_link_pic (info)
14667 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
14668 {
14669 relplt = htab->relpltlocal;
14670 if (htab->opd_abi)
14671 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14672 else
14673 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14674 }
2d7ad24e 14675 }
ed7007c1 14676 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14677
14678 if (relplt == NULL)
14679 {
14680 loc = plt->contents + ent->plt.offset;
14681 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14682 if (htab->opd_abi)
14683 {
14684 bfd_vma toc = elf_gp (info->output_bfd);
14685 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14686 bfd_put_64 (info->output_bfd, toc, loc + 8);
14687 }
14688 }
14689 else
14690 {
14691 rela.r_offset = (plt->output_section->vma
14692 + plt->output_offset
14693 + ent->plt.offset);
066c738e
AM
14694 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd, &rela,
14695 relplt));
2d7ad24e 14696 }
49c09209
AM
14697 }
14698 else
14699 {
14700 rela.r_offset = (htab->elf.splt->output_section->vma
14701 + htab->elf.splt->output_offset
14702 + ent->plt.offset);
14703 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14704 rela.r_addend = ent->addend;
14705 loc = (htab->elf.srelplt->contents
14706 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14707 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14708 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
0a1b45a2 14709 htab->elf.ifunc_resolvers = true;
066c738e
AM
14710 BFD_ASSERT (swap_reloc_out (info->output_bfd, &rela,
14711 loc, htab->elf.srelplt));
49c09209 14712 }
49c09209
AM
14713 }
14714
a345bc8d 14715 if (!h->pointer_equality_needed)
0a1b45a2 14716 return true;
a345bc8d
AM
14717
14718 if (h->def_regular)
0a1b45a2 14719 return true;
a345bc8d 14720
9e390558 14721 s = htab->global_entry;
49c09209 14722 if (s == NULL || s->size == 0)
0a1b45a2 14723 return true;
49c09209
AM
14724
14725 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14726 if (ent->plt.offset != (bfd_vma) -1
14727 && ent->addend == 0)
a345bc8d
AM
14728 {
14729 bfd_byte *p;
14730 asection *plt;
14731 bfd_vma off;
14732
a345bc8d 14733 p = s->contents + h->root.u.def.value;
33e44f2e 14734 plt = htab->elf.splt;
30845f11 14735 if (use_local_plt (info, h))
2d7ad24e
AM
14736 {
14737 if (h->type == STT_GNU_IFUNC)
14738 plt = htab->elf.iplt;
14739 else
14740 plt = htab->pltlocal;
14741 }
49c09209 14742 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14743 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14744
14745 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14746 {
14747 info->callbacks->einfo
c1c8c1ef 14748 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14749 h->root.root.string);
14750 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14751 htab->stub_error = true;
a345bc8d
AM
14752 }
14753
7341d5e2
AM
14754 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14755 if (htab->params->emit_stub_syms)
14756 {
14757 size_t len = strlen (h->root.root.string);
aefcec16
AM
14758 char *name = bfd_alloc (info->output_bfd,
14759 sizeof "12345678.global_entry." + len);
7341d5e2
AM
14760
14761 if (name == NULL)
0a1b45a2 14762 return false;
7341d5e2
AM
14763
14764 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
0a1b45a2 14765 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
7341d5e2 14766 if (h == NULL)
0a1b45a2 14767 return false;
7341d5e2
AM
14768 if (h->root.type == bfd_link_hash_new)
14769 {
14770 h->root.type = bfd_link_hash_defined;
14771 h->root.u.def.section = s;
14772 h->root.u.def.value = p - s->contents;
14773 h->ref_regular = 1;
14774 h->def_regular = 1;
14775 h->ref_regular_nonweak = 1;
14776 h->forced_local = 1;
14777 h->non_elf = 0;
2ec55de3 14778 h->root.linker_def = 1;
7341d5e2
AM
14779 }
14780 }
14781
a345bc8d
AM
14782 if (PPC_HA (off) != 0)
14783 {
14784 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14785 p += 4;
14786 }
14787 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14788 p += 4;
14789 bfd_put_32 (s->owner, MTCTR_R12, p);
14790 p += 4;
407aa07c 14791 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14792 break;
14793 }
0a1b45a2 14794 return true;
a345bc8d
AM
14795}
14796
49c09209
AM
14797/* Write PLT relocs for locals. */
14798
0a1b45a2 14799static bool
49c09209
AM
14800write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14801{
14802 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14803 bfd *ibfd;
14804
14805 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14806 {
14807 struct got_entry **lgot_ents, **end_lgot_ents;
14808 struct plt_entry **local_plt, **lplt, **end_local_plt;
14809 Elf_Internal_Shdr *symtab_hdr;
14810 bfd_size_type locsymcount;
14811 Elf_Internal_Sym *local_syms = NULL;
14812 struct plt_entry *ent;
14813
14814 if (!is_ppc64_elf (ibfd))
14815 continue;
14816
14817 lgot_ents = elf_local_got_ents (ibfd);
14818 if (!lgot_ents)
14819 continue;
14820
14821 symtab_hdr = &elf_symtab_hdr (ibfd);
14822 locsymcount = symtab_hdr->sh_info;
14823 end_lgot_ents = lgot_ents + locsymcount;
14824 local_plt = (struct plt_entry **) end_lgot_ents;
14825 end_local_plt = local_plt + locsymcount;
14826 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14827 for (ent = *lplt; ent != NULL; ent = ent->next)
14828 if (ent->plt.offset != (bfd_vma) -1)
14829 {
14830 Elf_Internal_Sym *sym;
14831 asection *sym_sec;
14832 asection *plt, *relplt;
49c09209 14833 bfd_vma val;
49c09209
AM
14834
14835 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14836 lplt - local_plt, ibfd))
14837 {
c9594989 14838 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209 14839 free (local_syms);
0a1b45a2 14840 return false;
49c09209
AM
14841 }
14842
14843 val = sym->st_value + ent->addend;
49c09209
AM
14844 if (sym_sec != NULL && sym_sec->output_section != NULL)
14845 val += sym_sec->output_offset + sym_sec->output_section->vma;
14846
2d7ad24e
AM
14847 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14848 {
0a1b45a2 14849 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14850 plt = htab->elf.iplt;
14851 relplt = htab->elf.irelplt;
14852 }
14853 else
14854 {
14855 plt = htab->pltlocal;
1657026c
AM
14856 relplt = NULL;
14857 if (bfd_link_pic (info)
14858 && !(info->enable_dt_relr && !htab->opd_abi))
14859 relplt = htab->relpltlocal;
2d7ad24e 14860 }
49c09209 14861
2d7ad24e
AM
14862 if (relplt == NULL)
14863 {
066c738e 14864 bfd_byte *loc = plt->contents + ent->plt.offset;
2d7ad24e
AM
14865 bfd_put_64 (info->output_bfd, val, loc);
14866 if (htab->opd_abi)
14867 {
14868 bfd_vma toc = elf_gp (ibfd);
14869 bfd_put_64 (info->output_bfd, toc, loc + 8);
14870 }
14871 }
49c09209 14872 else
2d7ad24e
AM
14873 {
14874 Elf_Internal_Rela rela;
14875 rela.r_offset = (ent->plt.offset
14876 + plt->output_offset
14877 + plt->output_section->vma);
14878 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14879 {
14880 if (htab->opd_abi)
14881 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14882 else
14883 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14884 }
14885 else
14886 {
14887 if (htab->opd_abi)
14888 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14889 else
14890 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14891 }
14892 rela.r_addend = val;
066c738e
AM
14893 BFD_ASSERT (count_and_swap_reloc_out (info->output_bfd,
14894 &rela, relplt));
2d7ad24e 14895 }
49c09209
AM
14896 }
14897
14898 if (local_syms != NULL
14899 && symtab_hdr->contents != (unsigned char *) local_syms)
14900 {
14901 if (!info->keep_memory)
14902 free (local_syms);
14903 else
14904 symtab_hdr->contents = (unsigned char *) local_syms;
14905 }
14906 }
0a1b45a2 14907 return true;
49c09209
AM
14908}
14909
a804e476
AM
14910/* Emit the static wrapper function preserving registers around a
14911 __tls_get_addr_opt call. */
14912
0a1b45a2 14913static bool
a804e476
AM
14914emit_tga_desc (struct ppc_link_hash_table *htab)
14915{
14916 asection *stub_sec = htab->tga_group->stub_sec;
14917 unsigned int cfa_updt = 11 * 4;
14918 bfd_byte *p;
14919 bfd_vma to, from, delta;
14920
14921 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14922 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14923 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14924 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14925 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14926 delta = to - from;
14927 if (delta + (1 << 25) >= 1 << 26)
14928 {
14929 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
0a1b45a2
AM
14930 htab->stub_error = true;
14931 return false;
a804e476
AM
14932 }
14933
14934 p = stub_sec->contents;
14935 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14936 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14937 p += 4;
14938 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14939 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14940}
14941
14942/* Emit eh_frame describing the static wrapper function. */
14943
14944static bfd_byte *
14945emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14946{
14947 unsigned int cfa_updt = 11 * 4;
14948 unsigned int i;
14949
14950 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14951 *p++ = DW_CFA_def_cfa_offset;
14952 if (htab->opd_abi)
14953 {
14954 *p++ = 128;
14955 *p++ = 1;
14956 }
14957 else
14958 *p++ = 96;
14959 *p++ = DW_CFA_offset_extended_sf;
14960 *p++ = 65;
14961 *p++ = (-16 / 8) & 0x7f;
14962 for (i = 4; i < 12; i++)
14963 {
14964 *p++ = DW_CFA_offset + i;
14965 *p++ = (htab->opd_abi ? 13 : 12) - i;
14966 }
14967 *p++ = DW_CFA_advance_loc + 10;
14968 *p++ = DW_CFA_def_cfa_offset;
14969 *p++ = 0;
14970 for (i = 4; i < 12; i++)
14971 *p++ = DW_CFA_restore + i;
14972 *p++ = DW_CFA_advance_loc + 2;
14973 *p++ = DW_CFA_restore_extended;
14974 *p++ = 65;
14975 return p;
14976}
14977
721956f4
AM
14978/* Build all the stubs associated with the current output file.
14979 The stubs are kept in a hash table attached to the main linker
14980 hash table. This function is called via gldelf64ppc_finish. */
14981
0a1b45a2 14982bool
e7d1c40c 14983ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14984 char **stats)
5d1634d7
AM
14985{
14986 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14987 struct map_stub *group;
721956f4 14988 asection *stub_sec;
5d1634d7 14989 bfd_byte *p;
e717da7e 14990 int stub_sec_count = 0;
5d1634d7 14991
4dfe6ac6 14992 if (htab == NULL)
0a1b45a2 14993 return false;
4dfe6ac6 14994
eea6121a 14995 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14996 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14997 {
14998 group->eh_size = 0;
14999 group->lr_restore = 0;
15000 if ((stub_sec = group->stub_sec) != NULL
15001 && stub_sec->size != 0)
15002 {
15003 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
15004 stub_sec->size);
15005 if (stub_sec->contents == NULL)
0a1b45a2 15006 return false;
cb6326b5 15007 stub_sec->alloced = 1;
df136d64
AM
15008 stub_sec->size = 0;
15009 }
15010 }
5d1634d7 15011
23eb7e01 15012 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 15013 {
9f951329 15014 unsigned int indx;
ad8e1ba5 15015 bfd_vma plt0;
9f951329 15016
721956f4 15017 /* Build the .glink plt call stub. */
e7d1c40c 15018 if (htab->params->emit_stub_syms)
97b639ba
AM
15019 {
15020 struct elf_link_hash_entry *h;
468392fb 15021 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
0a1b45a2 15022 true, false, false);
97b639ba 15023 if (h == NULL)
0a1b45a2 15024 return false;
97b639ba
AM
15025 if (h->root.type == bfd_link_hash_new)
15026 {
15027 h->root.type = bfd_link_hash_defined;
15028 h->root.u.def.section = htab->glink;
ee4bf8d2 15029 h->root.u.def.value = 8;
f5385ebf
AM
15030 h->ref_regular = 1;
15031 h->def_regular = 1;
15032 h->ref_regular_nonweak = 1;
15033 h->forced_local = 1;
15034 h->non_elf = 0;
2ec55de3 15035 h->root.linker_def = 1;
97b639ba
AM
15036 }
15037 }
33e44f2e
AM
15038 plt0 = (htab->elf.splt->output_section->vma
15039 + htab->elf.splt->output_offset
15040 - 16);
176a0d42
AM
15041 if (info->emitrelocations)
15042 {
15043 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15044 if (r == NULL)
0a1b45a2 15045 return false;
176a0d42
AM
15046 r->r_offset = (htab->glink->output_offset
15047 + htab->glink->output_section->vma);
15048 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15049 r->r_addend = plt0;
15050 }
4ce794b7 15051 p = htab->glink->contents;
176a0d42 15052 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
15053 bfd_put_64 (htab->glink->owner, plt0, p);
15054 p += 8;
b9e5796b
AM
15055 if (htab->opd_abi)
15056 {
15057 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15058 p += 4;
15059 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15060 p += 4;
15061 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15062 p += 4;
15063 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15064 p += 4;
15065 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15066 p += 4;
15067 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15068 p += 4;
15069 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15070 p += 4;
15071 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15072 p += 4;
15073 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15074 p += 4;
15075 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15076 p += 4;
15077 }
15078 else
15079 {
3cd7c7d7
AM
15080 unsigned int insn;
15081
15082 /* 0:
15083 . .quad plt0-1f # plt0 entry relative to 1:
15084 #
15085 # We get here with r12 initially @ a glink branch
15086 # Load the address of _dl_runtime_resolve from plt0 and
15087 # jump to it, with r0 set to the index of the PLT entry
15088 # to be resolved and r11 the link map.
15089 __glink_PLTresolve:
15090 . std %r2,24(%r1) # optional
15091 . mflr %r0
15092 . bcl 20,31,1f
15093 1:
15094 . mflr %r11
15095 . mtlr %r0
15096 . ld %r0,(0b-1b)(%r11)
15097 . sub %r12,%r12,%r11
15098 . add %r11,%r0,%r11
15099 . addi %r0,%r12,1b-2f
15100 . ld %r12,0(%r11)
15101 . srdi %r0,%r0,2
15102 . mtctr %r12
15103 . ld %r11,8(%r11)
15104 . bctr
15105 2:
15106 . b __glink_PLTresolve
15107 . ...
15108 . b __glink_PLTresolve */
15109
15110 if (htab->has_plt_localentry0)
15111 {
15112 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15113 p += 4;
15114 }
b9e5796b
AM
15115 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15116 p += 4;
15117 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15118 p += 4;
15119 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15120 p += 4;
b9e5796b
AM
15121 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15122 p += 4;
3cd7c7d7
AM
15123 if (htab->has_plt_localentry0)
15124 insn = LD_R0_0R11 | (-20 & 0xfffc);
15125 else
15126 insn = LD_R0_0R11 | (-16 & 0xfffc);
15127 bfd_put_32 (htab->glink->owner, insn, p);
15128 p += 4;
b9e5796b
AM
15129 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15130 p += 4;
3cd7c7d7 15131 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 15132 p += 4;
3cd7c7d7 15133 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
15134 p += 4;
15135 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15136 p += 4;
15137 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15138 p += 4;
15139 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15140 p += 4;
15141 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15142 p += 4;
15143 }
407aa07c
AM
15144 bfd_put_32 (htab->glink->owner, BCTR, p);
15145 p += 4;
c75bc4f7 15146 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 15147
9f951329
AM
15148 /* Build the .glink lazy link call stubs. */
15149 indx = 0;
9e390558 15150 while (p < htab->glink->contents + htab->glink->size)
9f951329 15151 {
b9e5796b 15152 if (htab->opd_abi)
9f951329 15153 {
b9e5796b
AM
15154 if (indx < 0x8000)
15155 {
15156 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15157 p += 4;
15158 }
15159 else
15160 {
15161 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15162 p += 4;
15163 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15164 p);
15165 p += 4;
15166 }
9f951329 15167 }
4ce794b7 15168 bfd_put_32 (htab->glink->owner,
ee4bf8d2 15169 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 15170 indx++;
9f951329
AM
15171 p += 4;
15172 }
5d1634d7 15173 }
5d1634d7 15174
a804e476
AM
15175 if (htab->tga_group != NULL)
15176 {
15177 htab->tga_group->lr_restore = 23 * 4;
15178 htab->tga_group->stub_sec->size = 24 * 4;
15179 if (!emit_tga_desc (htab))
0a1b45a2 15180 return false;
a804e476
AM
15181 if (htab->glink_eh_frame != NULL
15182 && htab->glink_eh_frame->size != 0)
15183 {
15184 size_t align = 4;
15185
15186 p = htab->glink_eh_frame->contents;
15187 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15188 p += 17;
15189 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15190 }
15191 }
15192
49c09209
AM
15193 /* Build .glink global entry stubs, and PLT relocs for globals. */
15194 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15195
15196 if (!write_plt_relocs_for_local_syms (info))
0a1b45a2 15197 return false;
9e390558 15198
7341d5e2 15199 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 15200 {
4ce794b7 15201 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 15202 htab->brlt->size);
4ce794b7 15203 if (htab->brlt->contents == NULL)
0a1b45a2 15204 return false;
cb6326b5 15205 htab->brlt->alloced = 1;
721956f4 15206 }
ee75fd95 15207 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
15208 {
15209 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 15210 htab->relbrlt->size);
63bc6f6c 15211 if (htab->relbrlt->contents == NULL)
0a1b45a2 15212 return false;
cb6326b5 15213 htab->relbrlt->alloced = 1;
63bc6f6c 15214 }
5d1634d7 15215
721956f4 15216 /* Build the stubs as directed by the stub hash table. */
ecc741e6 15217 htab->stub_id = 0;
721956f4 15218 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 15219
a4b6fadd
AM
15220 for (group = htab->group; group != NULL; group = group->next)
15221 if (group->needs_save_res)
7dda8d3c 15222 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 15223
aa8a7074
AM
15224 if (htab->relbrlt != NULL)
15225 htab->relbrlt->reloc_count = 0;
15226
e7d1c40c 15227 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
15228 for (group = htab->group; group != NULL; group = group->next)
15229 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
15230 {
15231 int align = abs (htab->params->plt_stub_align);
15232 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15233 }
794e51c0 15234
7dda8d3c
AM
15235 for (group = htab->group; group != NULL; group = group->next)
15236 if (group->needs_save_res)
15237 {
15238 stub_sec = group->stub_sec;
15239 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15240 htab->sfpr->contents, htab->sfpr->size);
15241 if (htab->params->emit_stub_syms)
15242 {
15243 unsigned int i;
15244
15245 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15246 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
0a1b45a2 15247 return false;
7dda8d3c
AM
15248 }
15249 }
15250
df136d64
AM
15251 if (htab->glink_eh_frame != NULL
15252 && htab->glink_eh_frame->size != 0)
15253 {
15254 bfd_vma val;
15255 size_t align = 4;
15256
15257 p = htab->glink_eh_frame->contents;
15258 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15259
15260 for (group = htab->group; group != NULL; group = group->next)
15261 if (group->eh_size != 0)
15262 {
15263 /* Offset to stub section. */
15264 val = (group->stub_sec->output_section->vma
15265 + group->stub_sec->output_offset);
15266 val -= (htab->glink_eh_frame->output_section->vma
15267 + htab->glink_eh_frame->output_offset
15268 + (p + 8 - htab->glink_eh_frame->contents));
15269 if (val + 0x80000000 > 0xffffffff)
15270 {
15271 _bfd_error_handler
15272 (_("%s offset too large for .eh_frame sdata4 encoding"),
15273 group->stub_sec->name);
0a1b45a2 15274 return false;
df136d64
AM
15275 }
15276 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15277 p += (group->eh_size + 17 + 3) & -4;
15278 }
15279 if (htab->glink != NULL && htab->glink->size != 0)
15280 {
15281 /* Offset to .glink. */
15282 val = (htab->glink->output_section->vma
15283 + htab->glink->output_offset
15284 + 8);
15285 val -= (htab->glink_eh_frame->output_section->vma
15286 + htab->glink_eh_frame->output_offset
15287 + (p + 8 - htab->glink_eh_frame->contents));
15288 if (val + 0x80000000 > 0xffffffff)
15289 {
15290 _bfd_error_handler
15291 (_("%s offset too large for .eh_frame sdata4 encoding"),
15292 htab->glink->name);
0a1b45a2 15293 return false;
df136d64
AM
15294 }
15295 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15296 p += (24 + align - 1) & -align;
15297 }
15298 }
15299
1657026c
AM
15300 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15301 {
15302 htab->elf.srelrdyn->contents
15303 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15304 if (htab->elf.srelrdyn->contents == NULL)
15305 return false;
cb6326b5 15306 htab->elf.srelrdyn->alloced = 1;
1657026c 15307
0aac2413
AM
15308 bfd_vma *relr_addr = sort_relr (htab);
15309 if (htab->relr_count != 0 && relr_addr == NULL)
15310 return false;
15311
1657026c
AM
15312 size_t i = 0;
15313 bfd_byte *loc = htab->elf.srelrdyn->contents;
15314 while (i < htab->relr_count)
15315 {
0aac2413 15316 bfd_vma base = relr_addr[i];
f91074eb 15317 BFD_ASSERT ((base & ((1 << RELR_ALIGN) - 1)) == 0);
1657026c
AM
15318 bfd_put_64 (htab->elf.dynobj, base, loc);
15319 loc += 8;
15320 i++;
15321 while (i < htab->relr_count
0aac2413 15322 && relr_addr[i] == base)
1657026c
AM
15323 {
15324 htab->stub_error = true;
15325 i++;
15326 }
15327 base += 8;
15328 while (1)
15329 {
15330 bfd_vma bits = 0;
15331 while (i < htab->relr_count
0aac2413
AM
15332 && relr_addr[i] - base < 63 * 8
15333 && (relr_addr[i] - base) % 8 == 0)
1657026c 15334 {
0aac2413 15335 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
1657026c
AM
15336 i++;
15337 }
15338 if (bits == 0)
15339 break;
15340 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15341 loc += 8;
15342 base += 63 * 8;
15343 }
15344 }
0aac2413 15345 free (relr_addr);
1657026c
AM
15346 /* Pad any excess with 1's, a do-nothing encoding. */
15347 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15348 < htab->elf.srelrdyn->size)
15349 {
15350 bfd_put_64 (htab->elf.dynobj, 1, loc);
15351 loc += 8;
15352 }
15353 }
aefcec16
AM
15354 free (htab->relr);
15355 htab->relr = NULL;
1657026c 15356
d4aaa2a0
AM
15357 for (group = htab->group; group != NULL; group = group->next)
15358 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
15359 {
15360 stub_sec_count += 1;
c9301e31
AM
15361 if (stub_sec->rawsize != stub_sec->size
15362 && (htab->stub_iteration <= STUB_SHRINK_ITER
15363 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
15364 break;
15365 }
5d1634d7 15366
25516cc5 15367 if (group != NULL)
1657026c
AM
15368 htab->stub_error = true;
15369
15370 if (htab->stub_error)
5d1634d7 15371 {
cf97bcb0 15372 _bfd_error_handler (_("stubs don't match calculated size"));
1657026c 15373 return false;
5d1634d7 15374 }
721956f4 15375
d2a300cf
AM
15376 if (stats != NULL)
15377 {
988b7300
AM
15378 char *groupmsg;
15379 if (asprintf (&groupmsg,
17a6f5f2
AM
15380 ngettext ("linker stubs in %u group",
15381 "linker stubs in %u groups",
988b7300
AM
15382 stub_sec_count),
15383 stub_sec_count) < 0)
15384 *stats = NULL;
15385 else
15386 {
17a6f5f2 15387 if (asprintf (stats, _("%s, iter %u\n"
988b7300 15388 " branch %lu\n"
988b7300 15389 " long branch %lu\n"
988b7300 15390 " plt call %lu\n"
988b7300 15391 " global entry %lu"),
17a6f5f2 15392 groupmsg, htab->stub_iteration,
988b7300 15393 htab->stub_count[ppc_stub_long_branch - 1],
988b7300 15394 htab->stub_count[ppc_stub_plt_branch - 1],
988b7300 15395 htab->stub_count[ppc_stub_plt_call - 1],
988b7300
AM
15396 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15397 *stats = NULL;
15398 free (groupmsg);
15399 }
d2a300cf 15400 }
0a1b45a2 15401 return true;
5bd4f169
AM
15402}
15403
60124e18
AM
15404/* What to do when ld finds relocations against symbols defined in
15405 discarded sections. */
15406
15407static unsigned int
15408ppc64_elf_action_discarded (asection *sec)
15409{
15410 if (strcmp (".opd", sec->name) == 0)
15411 return 0;
15412
15413 if (strcmp (".toc", sec->name) == 0)
15414 return 0;
15415
bce50a28
JJ
15416 if (strcmp (".toc1", sec->name) == 0)
15417 return 0;
15418
60124e18
AM
15419 return _bfd_elf_default_action_discarded (sec);
15420}
15421
e59a1001
AM
15422/* These are the dynamic relocations supported by glibc. */
15423
0a1b45a2 15424static bool
e59a1001
AM
15425ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15426{
15427 switch (r_type)
15428 {
15429 case R_PPC64_RELATIVE:
15430 case R_PPC64_NONE:
15431 case R_PPC64_ADDR64:
15432 case R_PPC64_GLOB_DAT:
15433 case R_PPC64_IRELATIVE:
15434 case R_PPC64_JMP_IREL:
15435 case R_PPC64_JMP_SLOT:
15436 case R_PPC64_DTPMOD64:
15437 case R_PPC64_DTPREL64:
15438 case R_PPC64_TPREL64:
15439 case R_PPC64_TPREL16_LO_DS:
15440 case R_PPC64_TPREL16_DS:
15441 case R_PPC64_TPREL16:
15442 case R_PPC64_TPREL16_LO:
15443 case R_PPC64_TPREL16_HI:
15444 case R_PPC64_TPREL16_HIGH:
15445 case R_PPC64_TPREL16_HA:
15446 case R_PPC64_TPREL16_HIGHA:
15447 case R_PPC64_TPREL16_HIGHER:
15448 case R_PPC64_TPREL16_HIGHEST:
15449 case R_PPC64_TPREL16_HIGHERA:
15450 case R_PPC64_TPREL16_HIGHESTA:
15451 case R_PPC64_ADDR16_LO_DS:
15452 case R_PPC64_ADDR16_LO:
15453 case R_PPC64_ADDR16_HI:
15454 case R_PPC64_ADDR16_HIGH:
15455 case R_PPC64_ADDR16_HA:
15456 case R_PPC64_ADDR16_HIGHA:
15457 case R_PPC64_REL30:
15458 case R_PPC64_COPY:
15459 case R_PPC64_UADDR64:
15460 case R_PPC64_UADDR32:
15461 case R_PPC64_ADDR32:
15462 case R_PPC64_ADDR24:
15463 case R_PPC64_ADDR16:
15464 case R_PPC64_UADDR16:
15465 case R_PPC64_ADDR16_DS:
15466 case R_PPC64_ADDR16_HIGHER:
15467 case R_PPC64_ADDR16_HIGHEST:
15468 case R_PPC64_ADDR16_HIGHERA:
15469 case R_PPC64_ADDR16_HIGHESTA:
15470 case R_PPC64_ADDR14:
15471 case R_PPC64_ADDR14_BRTAKEN:
15472 case R_PPC64_ADDR14_BRNTAKEN:
15473 case R_PPC64_REL32:
15474 case R_PPC64_REL64:
0a1b45a2 15475 return true;
e59a1001
AM
15476
15477 default:
0a1b45a2 15478 return false;
e59a1001
AM
15479 }
15480}
15481
5bd4f169
AM
15482/* The RELOCATE_SECTION function is called by the ELF backend linker
15483 to handle the relocations for a section.
15484
15485 The relocs are always passed as Rela structures; if the section
15486 actually uses Rel structures, the r_addend field will always be
15487 zero.
15488
15489 This function is responsible for adjust the section contents as
15490 necessary, and (if using Rela relocs and generating a
1049f94e 15491 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
15492 necessary.
15493
15494 This function does not have to worry about setting the reloc
15495 address or the reloc symbol index.
15496
15497 LOCAL_SYMS is a pointer to the swapped in local symbols.
15498
15499 LOCAL_SECTIONS is an array giving the section in the input file
15500 corresponding to the st_shndx field of each local symbol.
15501
15502 The global hash table entry for the global symbols can be found
15503 via elf_sym_hashes (input_bfd).
15504
1049f94e 15505 When generating relocatable output, this function must handle
5bd4f169
AM
15506 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15507 going to be the section symbol corresponding to the output
15508 section, which means that the addend must be adjusted
15509 accordingly. */
15510
0f684201 15511static int
4ce794b7
AM
15512ppc64_elf_relocate_section (bfd *output_bfd,
15513 struct bfd_link_info *info,
15514 bfd *input_bfd,
15515 asection *input_section,
15516 bfd_byte *contents,
15517 Elf_Internal_Rela *relocs,
15518 Elf_Internal_Sym *local_syms,
15519 asection **local_sections)
5bd4f169 15520{
65f38f15 15521 struct ppc_link_hash_table *htab;
5bd4f169
AM
15522 Elf_Internal_Shdr *symtab_hdr;
15523 struct elf_link_hash_entry **sym_hashes;
5bd4f169 15524 Elf_Internal_Rela *rel;
c316a17c 15525 Elf_Internal_Rela *wrel;
5bd4f169 15526 Elf_Internal_Rela *relend;
411e1bfb
AM
15527 Elf_Internal_Rela outrel;
15528 bfd_byte *loc;
411e1bfb 15529 struct got_entry **local_got_ents;
5bd4f169 15530 bfd_vma TOCstart;
0a1b45a2
AM
15531 bool ret = true;
15532 bool is_opd;
794e51c0 15533 /* Assume 'at' branch hints. */
0a1b45a2
AM
15534 bool is_isa_v2 = true;
15535 bool warned_dynamic = false;
95f0d0d2 15536 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 15537
65f38f15 15538 /* Initialize howto table if needed. */
5bd4f169 15539 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
15540 ppc_howto_init ();
15541
65f38f15 15542 htab = ppc_hash_table (info);
4dfe6ac6 15543 if (htab == NULL)
0a1b45a2 15544 return false;
ee75fd95
AM
15545
15546 /* Don't relocate stub sections. */
e7d1c40c 15547 if (input_section->owner == htab->params->stub_bfd)
0a1b45a2 15548 return true;
ee75fd95 15549
7af5d5c4
AM
15550 if (!is_ppc64_elf (input_bfd))
15551 {
15552 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 15553 return false;
7af5d5c4 15554 }
0ffa91dd 15555
411e1bfb 15556 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 15557 TOCstart = elf_gp (output_bfd);
0ffa91dd 15558 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 15559 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 15560 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 15561
c316a17c 15562 rel = wrel = relocs;
5bd4f169 15563 relend = relocs + input_section->reloc_count;
c316a17c 15564 for (; rel < relend; wrel++, rel++)
5bd4f169 15565 {
04c9666a 15566 enum elf_ppc64_reloc_type r_type;
31c76678 15567 bfd_vma addend;
5bd4f169
AM
15568 bfd_reloc_status_type r;
15569 Elf_Internal_Sym *sym;
15570 asection *sec;
039b3fef
AM
15571 struct elf_link_hash_entry *h_elf;
15572 struct ppc_link_hash_entry *h;
15573 struct ppc_link_hash_entry *fdh;
5bd4f169 15574 const char *sym_name;
0d4792f7 15575 unsigned long r_symndx, toc_symndx;
3a71aa26 15576 bfd_vma toc_addend;
f961d9dd
AM
15577 unsigned char tls_mask, tls_gd, tls_type;
15578 unsigned char sym_type;
5bd4f169 15579 bfd_vma relocation;
0a1b45a2
AM
15580 bool unresolved_reloc, save_unresolved_reloc;
15581 bool warned;
bc30df16 15582 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 15583 unsigned int insn;
e11840f9 15584 unsigned int mask;
721956f4
AM
15585 struct ppc_stub_hash_entry *stub_entry;
15586 bfd_vma max_br_offset;
15587 bfd_vma from;
c316a17c 15588 Elf_Internal_Rela orig_rel;
b80eed39
AM
15589 reloc_howto_type *howto;
15590 struct reloc_howto_struct alt_howto;
4a421c53
AM
15591 uint64_t pinsn;
15592 bfd_vma offset;
5bd4f169 15593
c316a17c
AM
15594 again:
15595 orig_rel = *rel;
15596
4ce794b7 15597 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 15598 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
15599
15600 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15601 symbol of the previous ADDR64 reloc. The symbol gives us the
15602 proper TOC base to use. */
15603 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
15604 && wrel != relocs
15605 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 15606 && is_opd)
c316a17c 15607 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 15608
4ce794b7
AM
15609 sym = NULL;
15610 sec = NULL;
039b3fef 15611 h_elf = NULL;
4ce794b7 15612 sym_name = NULL;
0a1b45a2
AM
15613 unresolved_reloc = false;
15614 warned = false;
65f38f15 15615
0b13192e 15616 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15617 {
15618 /* It's a local symbol. */
74f0fb50 15619 struct _opd_sec_data *opd;
4025353c 15620
5bd4f169
AM
15621 sym = local_syms + r_symndx;
15622 sec = local_sections[r_symndx];
26c61ae5 15623 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15624 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15625 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15626 opd = get_opd_info (sec);
15627 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15628 {
51aecdc5
AM
15629 long adjust = opd->adjust[OPD_NDX (sym->st_value
15630 + rel->r_addend)];
4025353c
AM
15631 if (adjust == -1)
15632 relocation = 0;
15633 else
4cc603a5
AM
15634 {
15635 /* If this is a relocation against the opd section sym
15636 and we have edited .opd, adjust the reloc addend so
15637 that ld -r and ld --emit-relocs output is correct.
15638 If it is a reloc against some other .opd symbol,
15639 then the symbol value will be adjusted later. */
15640 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15641 rel->r_addend += adjust;
15642 else
15643 relocation += adjust;
15644 }
1e2f5b6e 15645 }
5bd4f169
AM
15646 }
15647 else
15648 {
0a1b45a2 15649 bool ignored;
62d887d4 15650
b2a8e766
AM
15651 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15652 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15653 h_elf, sec, relocation,
62d887d4 15654 unresolved_reloc, warned, ignored);
039b3fef
AM
15655 sym_name = h_elf->root.root.string;
15656 sym_type = h_elf->type;
b69fdb4e
AM
15657 if (sec != NULL
15658 && sec->owner == output_bfd
15659 && strcmp (sec->name, ".opd") == 0)
15660 {
15661 /* This is a symbol defined in a linker script. All
15662 such are defined in output sections, even those
15663 defined by simple assignment from a symbol defined in
15664 an input section. Transfer the symbol to an
15665 appropriate input .opd section, so that a branch to
15666 this symbol will be mapped to the location specified
15667 by the opd entry. */
15668 struct bfd_link_order *lo;
15669 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15670 if (lo->type == bfd_indirect_link_order)
15671 {
15672 asection *isec = lo->u.indirect.section;
15673 if (h_elf->root.u.def.value >= isec->output_offset
15674 && h_elf->root.u.def.value < (isec->output_offset
15675 + isec->size))
15676 {
15677 h_elf->root.u.def.value -= isec->output_offset;
15678 h_elf->root.u.def.section = isec;
15679 sec = isec;
15680 break;
15681 }
15682 }
15683 }
5bd4f169 15684 }
ed7007c1 15685 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15686
dbaa2011 15687 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15688 {
15689 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15690 input_bfd, input_section,
0930cb30 15691 contents, rel->r_offset);
c316a17c
AM
15692 wrel->r_offset = rel->r_offset;
15693 wrel->r_info = 0;
15694 wrel->r_addend = 0;
15695
15696 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15697 symbols defined in discarded sections. Not done for
c316a17c
AM
15698 non-debug to preserve relocs in .eh_frame which the
15699 eh_frame editing code expects to be present. */
15700 if (bfd_link_relocatable (info)
15701 && (input_section->flags & SEC_DEBUGGING))
15702 wrel--;
15703
15704 continue;
15705 }
ab96bf03 15706
0e1862bb 15707 if (bfd_link_relocatable (info))
c316a17c 15708 goto copy_reloc;
ab96bf03 15709
f40da81b
AM
15710 if (h != NULL && &h->elf == htab->elf.hgot)
15711 {
6f20ed8a 15712 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b 15713 sec = bfd_abs_section_ptr;
0a1b45a2 15714 unresolved_reloc = false;
f40da81b
AM
15715 }
15716
951fd09b
AM
15717 /* TLS optimizations. Replace instruction sequences and relocs
15718 based on information we collected in tls_optimize. We edit
15719 RELOCS so that --emit-relocs will output something sensible
15720 for the final instruction stream. */
15721 tls_mask = 0;
15722 tls_gd = 0;
0d4792f7 15723 toc_symndx = 0;
727fc41e
AM
15724 if (h != NULL)
15725 tls_mask = h->tls_mask;
15726 else if (local_got_ents != NULL)
411e1bfb 15727 {
e054468f
AM
15728 struct plt_entry **local_plt = (struct plt_entry **)
15729 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15730 unsigned char *lgot_masks = (unsigned char *)
e054468f 15731 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15732 tls_mask = lgot_masks[r_symndx];
15733 }
37da22e5 15734 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15735 && (r_type == R_PPC64_TLS
15736 || r_type == R_PPC64_TLSGD
15737 || r_type == R_PPC64_TLSLD))
15738 {
15739 /* Check for toc tls entries. */
f961d9dd 15740 unsigned char *toc_tls;
0d4792f7 15741
727fc41e
AM
15742 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15743 &local_syms, rel, input_bfd))
0a1b45a2 15744 return false;
0d4792f7 15745
727fc41e
AM
15746 if (toc_tls)
15747 tls_mask = *toc_tls;
0d4792f7
AM
15748 }
15749
15750 /* Check that tls relocs are used with tls syms, and non-tls
15751 relocs are used with non-tls syms. */
cf35638d 15752 if (r_symndx != STN_UNDEF
0d4792f7
AM
15753 && r_type != R_PPC64_NONE
15754 && (h == NULL
039b3fef
AM
15755 || h->elf.root.type == bfd_link_hash_defined
15756 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15757 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15758 {
37da22e5 15759 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15760 && (r_type == R_PPC64_TLS
15761 || r_type == R_PPC64_TLSGD
15762 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15763 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15764 ;
15765 else
25f53a85 15766 info->callbacks->einfo
1d483afe 15767 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15768 /* xgettext:c-format */
c1c8c1ef 15769 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15770 /* xgettext:c-format */
c1c8c1ef 15771 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15772 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15773 ppc64_elf_howto_table[r_type]->name,
15774 sym_name);
411e1bfb
AM
15775 }
15776
15777 /* Ensure reloc mapping code below stays sane. */
15778 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15779 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15780 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15781 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15782 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15783 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15784 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15785 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15786 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15787 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15788 abort ();
0d4792f7 15789
411e1bfb
AM
15790 switch (r_type)
15791 {
15792 default:
411e1bfb
AM
15793 break;
15794
ba761f19 15795 case R_PPC64_LO_DS_OPT:
733ae98c
AM
15796 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15797 {
15798 insn = bfd_get_32 (input_bfd,
15799 contents + rel->r_offset - d_offset);
15800 if ((insn & (0x3fu << 26)) != 58u << 26)
15801 abort ();
15802 insn += (14u << 26) - (58u << 26);
15803 bfd_put_32 (input_bfd, insn,
15804 contents + rel->r_offset - d_offset);
15805 r_type = R_PPC64_TOC16_LO;
15806 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15807 }
ba761f19
AM
15808 break;
15809
411e1bfb
AM
15810 case R_PPC64_TOC16:
15811 case R_PPC64_TOC16_LO:
15812 case R_PPC64_TOC16_DS:
15813 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15814 {
15815 /* Check for toc tls entries. */
f961d9dd 15816 unsigned char *toc_tls;
951fd09b 15817 int retval;
411e1bfb 15818
3a71aa26
AM
15819 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15820 &local_syms, rel, input_bfd);
951fd09b 15821 if (retval == 0)
0a1b45a2 15822 return false;
411e1bfb
AM
15823
15824 if (toc_tls)
15825 {
951fd09b 15826 tls_mask = *toc_tls;
411e1bfb
AM
15827 if (r_type == R_PPC64_TOC16_DS
15828 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15829 {
37da22e5 15830 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15831 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15832 goto toctprel;
15833 }
411e1bfb 15834 else
951fd09b
AM
15835 {
15836 /* If we found a GD reloc pair, then we might be
15837 doing a GD->IE transition. */
15838 if (retval == 2)
15839 {
b00a0a86 15840 tls_gd = TLS_GDIE;
37da22e5
AM
15841 if ((tls_mask & TLS_TLS) != 0
15842 && (tls_mask & TLS_GD) == 0)
102890f0 15843 goto tls_ldgd_opt;
951fd09b
AM
15844 }
15845 else if (retval == 3)
15846 {
37da22e5
AM
15847 if ((tls_mask & TLS_TLS) != 0
15848 && (tls_mask & TLS_LD) == 0)
102890f0 15849 goto tls_ldgd_opt;
951fd09b
AM
15850 }
15851 }
411e1bfb
AM
15852 }
15853 }
15854 break;
15855
9d6ded02
AM
15856 case R_PPC64_GOT_TPREL16_HI:
15857 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15858 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15859 && (tls_mask & TLS_TPREL) == 0
15860 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
9d6ded02
AM
15861 {
15862 rel->r_offset -= d_offset;
95f0d0d2 15863 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15864 r_type = R_PPC64_NONE;
15865 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15866 }
15867 break;
15868
411e1bfb
AM
15869 case R_PPC64_GOT_TPREL16_DS:
15870 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15871 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15872 && (tls_mask & TLS_TPREL) == 0
15873 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
411e1bfb 15874 {
81407a69 15875 toctprel:
95f0d0d2 15876 insn = bfd_get_32 (input_bfd,
c316a17c 15877 contents + rel->r_offset - d_offset);
411e1bfb
AM
15878 insn &= 31 << 21;
15879 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15880 bfd_put_32 (input_bfd, insn,
c316a17c 15881 contents + rel->r_offset - d_offset);
411e1bfb 15882 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15883 if (toc_symndx != 0)
15884 {
15885 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15886 rel->r_addend = toc_addend;
0d4792f7
AM
15887 /* We changed the symbol. Start over in order to
15888 get h, sym, sec etc. right. */
c316a17c 15889 goto again;
0d4792f7
AM
15890 }
15891 else
15892 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15893 }
15894 break;
15895
87c69f97 15896 case R_PPC64_GOT_TPREL_PCREL34:
c213164a 15897 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15898 && (tls_mask & TLS_TPREL) == 0
15899 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15900 {
15901 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15902 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15903 pinsn <<= 32;
15904 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15905 pinsn += ((2ULL << 56) + (-1ULL << 52)
15906 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15907 bfd_put_32 (input_bfd, pinsn >> 32,
15908 contents + rel->r_offset);
15909 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15910 contents + rel->r_offset + 4);
15911 r_type = R_PPC64_TPREL34;
15912 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15913 }
15914 break;
15915
411e1bfb 15916 case R_PPC64_TLS:
37da22e5 15917 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15918 && (tls_mask & TLS_TPREL) == 0
15919 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15920 {
c213164a 15921 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15922 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15923 if (insn == 0)
c213164a
AM
15924 break;
15925 if ((rel->r_offset & 3) == 0)
0d4792f7 15926 {
c213164a
AM
15927 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15928 /* Was PPC64_TLS which sits on insn boundary, now
15929 PPC64_TPREL16_LO which is at low-order half-word. */
15930 rel->r_offset += d_offset;
15931 r_type = R_PPC64_TPREL16_LO;
15932 if (toc_symndx != 0)
15933 {
15934 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15935 rel->r_addend = toc_addend;
15936 /* We changed the symbol. Start over in order to
15937 get h, sym, sec etc. right. */
15938 goto again;
15939 }
15940 else
15941 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15942 }
15943 else if ((rel->r_offset & 3) == 1)
15944 {
15945 /* For pcrel IE to LE we already have the full
15946 offset and thus don't need an addi here. A nop
15947 or mr will do. */
2365f8d7 15948 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15949 {
15950 /* Extract regs from addi rt,ra,si. */
15951 unsigned int rt = (insn >> 21) & 0x1f;
15952 unsigned int ra = (insn >> 16) & 0x1f;
15953 if (rt == ra)
15954 insn = NOP;
15955 else
15956 {
15957 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15958 insn = (rt << 16) | (ra << 21) | (ra << 11);
15959 insn |= (31u << 26) | (444u << 1);
15960 }
15961 }
15962 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15963 }
411e1bfb
AM
15964 }
15965 break;
15966
411e1bfb
AM
15967 case R_PPC64_GOT_TLSGD16_HI:
15968 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15969 tls_gd = TLS_GDIE;
733ae98c
AM
15970 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15971 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b
AM
15972 goto tls_gdld_hi;
15973 break;
15974
411e1bfb
AM
15975 case R_PPC64_GOT_TLSLD16_HI:
15976 case R_PPC64_GOT_TLSLD16_HA:
733ae98c
AM
15977 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15978 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15979 {
951fd09b
AM
15980 tls_gdld_hi:
15981 if ((tls_mask & tls_gd) != 0)
15982 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15983 + R_PPC64_GOT_TPREL16_DS);
15984 else
411e1bfb 15985 {
4fe5ca5b 15986 rel->r_offset -= d_offset;
95f0d0d2 15987 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15988 r_type = R_PPC64_NONE;
411e1bfb 15989 }
951fd09b 15990 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15991 }
15992 break;
15993
951fd09b
AM
15994 case R_PPC64_GOT_TLSGD16:
15995 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15996 tls_gd = TLS_GDIE;
733ae98c
AM
15997 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15998 && offset_in_range (input_section, rel->r_offset & ~3, 4))
102890f0 15999 goto tls_ldgd_opt;
951fd09b 16000 break;
411e1bfb 16001
951fd09b
AM
16002 case R_PPC64_GOT_TLSLD16:
16003 case R_PPC64_GOT_TLSLD16_LO:
733ae98c
AM
16004 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16005 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b 16006 {
b9f04fe0 16007 unsigned int insn1, insn2;
102890f0
AM
16008
16009 tls_ldgd_opt:
727fc41e
AM
16010 offset = (bfd_vma) -1;
16011 /* If not using the newer R_PPC64_TLSGD/LD to mark
16012 __tls_get_addr calls, we must trust that the call
16013 stays with its arg setup insns, ie. that the next
16014 reloc is the __tls_get_addr call associated with
16015 the current reloc. Edit both insns. */
9737e8af 16016 if (input_section->nomark_tls_get_addr
727fc41e
AM
16017 && rel + 1 < relend
16018 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
16019 htab->tls_get_addr_fd,
16020 htab->tga_desc_fd,
727fc41e 16021 htab->tls_get_addr,
9e7028aa 16022 htab->tga_desc))
727fc41e 16023 offset = rel[1].r_offset;
b86ac8e3
AM
16024 /* We read the low GOT_TLS (or TOC16) insn because we
16025 need to keep the destination reg. It may be
16026 something other than the usual r3, and moved to r3
16027 before the call by intervening code. */
95f0d0d2 16028 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 16029 contents + rel->r_offset - d_offset);
102890f0 16030 if ((tls_mask & tls_gd) != 0)
411e1bfb 16031 {
102890f0 16032 /* IE */
b86ac8e3 16033 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 16034 insn1 |= 58u << 26; /* ld */
102890f0 16035 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 16036 if (offset != (bfd_vma) -1)
f58d5a2d 16037 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
16038 if (r_type == R_PPC64_TOC16
16039 || r_type == R_PPC64_TOC16_LO)
102890f0 16040 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
16041 else
16042 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
16043 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
16044 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16045 }
16046 else
16047 {
16048 /* LE */
b86ac8e3
AM
16049 insn1 &= 0x1f << 21;
16050 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
16051 insn2 = 0x38630000; /* addi 3,3,0 */
16052 if (tls_gd == 0)
951fd09b 16053 {
102890f0 16054 /* Was an LD reloc. */
71c4e95a 16055 r_symndx = STN_UNDEF;
102890f0 16056 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 16057 }
102890f0 16058 else if (toc_symndx != 0)
3a71aa26
AM
16059 {
16060 r_symndx = toc_symndx;
16061 rel->r_addend = toc_addend;
16062 }
102890f0
AM
16063 r_type = R_PPC64_TPREL16_HA;
16064 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
16065 if (offset != (bfd_vma) -1)
16066 {
16067 rel[1].r_info = ELF64_R_INFO (r_symndx,
16068 R_PPC64_TPREL16_LO);
16069 rel[1].r_offset = offset + d_offset;
16070 rel[1].r_addend = rel->r_addend;
16071 }
102890f0 16072 }
95f0d0d2 16073 bfd_put_32 (input_bfd, insn1,
3a71aa26 16074 contents + rel->r_offset - d_offset);
733ae98c
AM
16075 if (offset != (bfd_vma) -1
16076 && offset_in_range (input_section, offset, 4))
c96e0573
AM
16077 {
16078 bfd_put_32 (input_bfd, insn2, contents + offset);
733ae98c 16079 if (offset_in_range (input_section, offset + 4, 4))
c96e0573
AM
16080 {
16081 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16082 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16083 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16084 }
16085 }
727fc41e
AM
16086 if ((tls_mask & tls_gd) == 0
16087 && (tls_gd == 0 || toc_symndx != 0))
16088 {
16089 /* We changed the symbol. Start over in order
16090 to get h, sym, sec etc. right. */
c316a17c 16091 goto again;
727fc41e
AM
16092 }
16093 }
16094 break;
16095
87c69f97 16096 case R_PPC64_GOT_TLSGD_PCREL34:
733ae98c
AM
16097 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16098 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
16099 {
16100 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16101 pinsn <<= 32;
16102 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16103 if ((tls_mask & TLS_GDIE) != 0)
16104 {
16105 /* IE, pla -> pld */
16106 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 16107 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
16108 }
16109 else
16110 {
16111 /* LE, pla pcrel -> paddi r13 */
16112 pinsn += (-1ULL << 52) + (13ULL << 16);
16113 r_type = R_PPC64_TPREL34;
16114 }
16115 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16116 bfd_put_32 (input_bfd, pinsn >> 32,
16117 contents + rel->r_offset);
16118 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16119 contents + rel->r_offset + 4);
16120 }
16121 break;
16122
87c69f97 16123 case R_PPC64_GOT_TLSLD_PCREL34:
733ae98c
AM
16124 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16125 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
16126 {
16127 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16128 pinsn <<= 32;
16129 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16130 pinsn += (-1ULL << 52) + (13ULL << 16);
16131 bfd_put_32 (input_bfd, pinsn >> 32,
16132 contents + rel->r_offset);
16133 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16134 contents + rel->r_offset + 4);
16135 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16136 r_symndx = STN_UNDEF;
16137 r_type = R_PPC64_TPREL34;
16138 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16139 goto again;
16140 }
16141 break;
16142
727fc41e 16143 case R_PPC64_TLSGD:
37da22e5 16144 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
733ae98c
AM
16145 && rel + 1 < relend
16146 && offset_in_range (input_section, rel->r_offset,
16147 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16148 ? 8 : 4))
727fc41e 16149 {
b9f04fe0 16150 unsigned int insn2;
5663e321 16151 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16152
4a421c53 16153 offset = rel->r_offset;
5663e321 16154 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16155 {
16156 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16157 if (r_type1 == R_PPC64_PLT_PCREL34
16158 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16159 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16160 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16161 break;
16162 }
16163
733ae98c 16164 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16165 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16166
b00a0a86 16167 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
16168 {
16169 /* IE */
16170 r_type = R_PPC64_NONE;
16171 insn2 = 0x7c636a14; /* add 3,3,13 */
16172 }
16173 else
16174 {
16175 /* LE */
16176 if (toc_symndx != 0)
16177 {
16178 r_symndx = toc_symndx;
16179 rel->r_addend = toc_addend;
16180 }
c213164a 16181 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16182 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16183 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16184 {
16185 r_type = R_PPC64_NONE;
16186 insn2 = NOP;
16187 }
16188 else
16189 {
16190 rel->r_offset = offset + d_offset;
16191 r_type = R_PPC64_TPREL16_LO;
16192 insn2 = 0x38630000; /* addi 3,3,0 */
16193 }
727fc41e
AM
16194 }
16195 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16196 /* Zap the reloc on the _tls_get_addr call too. */
16197 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16198 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16199 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16200 if ((tls_mask & TLS_GDIE) == 0
16201 && toc_symndx != 0
16202 && r_type != R_PPC64_NONE)
c316a17c 16203 goto again;
411e1bfb 16204 }
411e1bfb
AM
16205 break;
16206
727fc41e 16207 case R_PPC64_TLSLD:
37da22e5 16208 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
733ae98c
AM
16209 && rel + 1 < relend
16210 && offset_in_range (input_section, rel->r_offset,
16211 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16212 ? 8 : 4))
727fc41e 16213 {
b9f04fe0 16214 unsigned int insn2;
5663e321 16215 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16216
4a421c53 16217 offset = rel->r_offset;
5663e321 16218 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16219 {
16220 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16221 if (r_type1 == R_PPC64_PLT_PCREL34
16222 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16223 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16224 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16225 break;
16226 }
16227
733ae98c 16228 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16229 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16230
c213164a 16231 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16232 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16233 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16234 {
16235 r_type = R_PPC64_NONE;
16236 insn2 = NOP;
16237 }
16238 else
16239 {
16240 rel->r_offset = offset + d_offset;
16241 r_symndx = STN_UNDEF;
16242 r_type = R_PPC64_TPREL16_LO;
16243 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16244 insn2 = 0x38630000; /* addi 3,3,0 */
16245 }
727fc41e 16246 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
16247 /* Zap the reloc on the _tls_get_addr call too. */
16248 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16249 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16250 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16251 if (r_type != R_PPC64_NONE)
16252 goto again;
727fc41e
AM
16253 }
16254 break;
16255
411e1bfb 16256 case R_PPC64_DTPMOD64:
951fd09b
AM
16257 if (rel + 1 < relend
16258 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16259 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 16260 {
733ae98c
AM
16261 if ((tls_mask & TLS_GD) == 0
16262 && offset_in_range (input_section, rel->r_offset, 8))
951fd09b
AM
16263 {
16264 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 16265 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
16266 r_type = R_PPC64_TPREL64;
16267 else
16268 {
4ce794b7 16269 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
16270 r_type = R_PPC64_NONE;
16271 }
16272 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16273 }
16274 }
16275 else
16276 {
733ae98c
AM
16277 if ((tls_mask & TLS_LD) == 0
16278 && offset_in_range (input_section, rel->r_offset, 8))
411e1bfb 16279 {
4ce794b7 16280 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 16281 r_type = R_PPC64_NONE;
951fd09b 16282 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 16283 }
411e1bfb
AM
16284 }
16285 break;
16286
16287 case R_PPC64_TPREL64:
951fd09b 16288 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
16289 {
16290 r_type = R_PPC64_NONE;
16291 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16292 }
16293 break;
52a82034 16294
006589cf
AM
16295 case R_PPC64_ENTRY:
16296 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16297 if (!bfd_link_pic (info)
16298 && !info->traditional_format
733ae98c
AM
16299 && relocation + 0x80008000 <= 0xffffffff
16300 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16301 {
16302 unsigned int insn1, insn2;
16303
16304 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16305 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16306 if ((insn1 & ~0xfffc) == LD_R2_0R12
16307 && insn2 == ADD_R2_R2_R12)
16308 {
95f0d0d2 16309 bfd_put_32 (input_bfd,
006589cf
AM
16310 LIS_R2 + PPC_HA (relocation),
16311 contents + rel->r_offset);
95f0d0d2 16312 bfd_put_32 (input_bfd,
006589cf
AM
16313 ADDI_R2_R2 + PPC_LO (relocation),
16314 contents + rel->r_offset + 4);
16315 }
16316 }
16317 else
16318 {
16319 relocation -= (rel->r_offset
16320 + input_section->output_offset
16321 + input_section->output_section->vma);
733ae98c
AM
16322 if (relocation + 0x80008000 <= 0xffffffff
16323 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16324 {
16325 unsigned int insn1, insn2;
16326
16327 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16328 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16329 if ((insn1 & ~0xfffc) == LD_R2_0R12
16330 && insn2 == ADD_R2_R2_R12)
16331 {
95f0d0d2 16332 bfd_put_32 (input_bfd,
006589cf
AM
16333 ADDIS_R2_R12 + PPC_HA (relocation),
16334 contents + rel->r_offset);
95f0d0d2 16335 bfd_put_32 (input_bfd,
006589cf
AM
16336 ADDI_R2_R2 + PPC_LO (relocation),
16337 contents + rel->r_offset + 4);
16338 }
16339 }
16340 }
16341 break;
16342
52a82034
AM
16343 case R_PPC64_REL16_HA:
16344 /* If we are generating a non-PIC executable, edit
16345 . 0: addis 2,12,.TOC.-0b@ha
16346 . addi 2,2,.TOC.-0b@l
16347 used by ELFv2 global entry points to set up r2, to
16348 . lis 2,.TOC.@ha
16349 . addi 2,2,.TOC.@l
16350 if .TOC. is in range. */
0e1862bb 16351 if (!bfd_link_pic (info)
810d4e75 16352 && !info->traditional_format
006589cf 16353 && !htab->opd_abi
4f038ee5 16354 && rel->r_addend == d_offset
52a82034
AM
16355 && h != NULL && &h->elf == htab->elf.hgot
16356 && rel + 1 < relend
16357 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16358 && rel[1].r_offset == rel->r_offset + 4
16359 && rel[1].r_addend == rel->r_addend + 4
733ae98c
AM
16360 && relocation + 0x80008000 <= 0xffffffff
16361 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
52a82034
AM
16362 {
16363 unsigned int insn1, insn2;
4a421c53 16364 offset = rel->r_offset - d_offset;
95f0d0d2
AM
16365 insn1 = bfd_get_32 (input_bfd, contents + offset);
16366 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
16367 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16368 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
16369 {
16370 r_type = R_PPC64_ADDR16_HA;
16371 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16372 rel->r_addend -= d_offset;
16373 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16374 rel[1].r_addend -= d_offset + 4;
95f0d0d2 16375 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
16376 }
16377 }
16378 break;
411e1bfb
AM
16379 }
16380
16381 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 16382 insn = 0;
b25116a9
AM
16383 max_br_offset = 1 << 25;
16384 addend = rel->r_addend;
bc30df16 16385 reloc_dest = DEST_NORMAL;
65f38f15 16386 switch (r_type)
5bd4f169
AM
16387 {
16388 default:
65f38f15 16389 break;
5bd4f169 16390
3b421ab3
AM
16391 case R_PPC64_TOCSAVE:
16392 if (relocation + addend == (rel->r_offset
16393 + input_section->output_offset
16394 + input_section->output_section->vma)
16395 && tocsave_find (htab, NO_INSERT,
733ae98c
AM
16396 &local_syms, rel, input_bfd)
16397 && offset_in_range (input_section, rel->r_offset, 4))
3b421ab3
AM
16398 {
16399 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16400 if (insn == NOP
16401 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
16402 bfd_put_32 (input_bfd,
16403 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
16404 contents + rel->r_offset);
16405 }
16406 break;
16407
65f38f15
AM
16408 /* Branch taken prediction relocations. */
16409 case R_PPC64_ADDR14_BRTAKEN:
16410 case R_PPC64_REL14_BRTAKEN:
cedb70c5 16411 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 16412 /* Fall through. */
65f38f15 16413
86c76c7b 16414 /* Branch not taken prediction relocations. */
65f38f15
AM
16415 case R_PPC64_ADDR14_BRNTAKEN:
16416 case R_PPC64_REL14_BRNTAKEN:
733ae98c
AM
16417 if (!offset_in_range (input_section, rel->r_offset, 4))
16418 break;
95f0d0d2 16419 insn |= bfd_get_32 (input_bfd,
411e1bfb 16420 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 16421 /* Fall through. */
86c76c7b 16422
b25116a9
AM
16423 case R_PPC64_REL14:
16424 max_br_offset = 1 << 15;
1a0670f3 16425 /* Fall through. */
5bd4f169 16426
65f38f15 16427 case R_PPC64_REL24:
05d0e962 16428 case R_PPC64_REL24_NOTOC:
7aba54da 16429 case R_PPC64_REL24_P9NOTOC:
23cedd1d 16430 case R_PPC64_PLTCALL:
5663e321 16431 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
16432 /* Calls to functions with a different TOC, such as calls to
16433 shared objects, need to alter the TOC pointer. This is
16434 done using a linkage stub. A REL24 branching to these
16435 linkage stubs needs to be followed by a nop, as the nop
16436 will be replaced with an instruction to restore the TOC
16437 base pointer. */
8387904d 16438 fdh = h;
b31867b6
AM
16439 if (h != NULL
16440 && h->oh != NULL
16441 && h->oh->is_func_descriptor)
16442 fdh = ppc_follow_link (h->oh);
31c76678
DK
16443 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16444 htab);
5663e321
AM
16445 if ((r_type == R_PPC64_PLTCALL
16446 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 16447 && stub_entry != NULL
7aba54da 16448 && stub_entry->type.main == ppc_stub_plt_call)
23cedd1d
AM
16449 stub_entry = NULL;
16450
6abec6bc 16451 if (stub_entry != NULL
7aba54da
AM
16452 && (stub_entry->type.main == ppc_stub_plt_call
16453 || stub_entry->type.r2save))
41bd81ab 16454 {
0a1b45a2 16455 bool can_plt_call = false;
721956f4 16456
7aba54da
AM
16457 if (r_type == R_PPC64_REL24_NOTOC
16458 || r_type == R_PPC64_REL24_P9NOTOC)
6e1816be 16459 {
7aba54da 16460 /* NOTOC calls don't need to restore r2. */
0a1b45a2 16461 can_plt_call = true;
6e1816be 16462 }
7aba54da
AM
16463 else if (stub_entry->type.main == ppc_stub_plt_call
16464 && !htab->opd_abi
16465 && htab->params->plt_localentry0 != 0
16466 && h != NULL
16467 && is_elfv2_localentry0 (&h->elf))
05d0e962 16468 {
7aba54da 16469 /* The function doesn't use or change r2. */
0a1b45a2 16470 can_plt_call = true;
05d0e962 16471 }
6e1816be 16472
f378ab09 16473 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
16474 branch and link followed by a nop. The nop is
16475 replaced by an insn to restore r2. */
733ae98c 16476 else if (offset_in_range (input_section, rel->r_offset, 8))
41bd81ab 16477 {
ba8ca3e7
AM
16478 unsigned long br;
16479
16480 br = bfd_get_32 (input_bfd,
16481 contents + rel->r_offset);
16482 if ((br & 1) != 0)
41bd81ab 16483 {
ba8ca3e7
AM
16484 unsigned long nop;
16485
16486 nop = bfd_get_32 (input_bfd,
16487 contents + rel->r_offset + 4);
23cedd1d 16488 if (nop == LD_R2_0R1 + STK_TOC (htab))
0a1b45a2 16489 can_plt_call = true;
23cedd1d
AM
16490 else if (nop == NOP
16491 || nop == CROR_151515
16492 || nop == CROR_313131)
a7f2871e 16493 {
ba8ca3e7 16494 if (h != NULL
ed7007c1 16495 && is_tls_get_addr (&h->elf, htab)
7c9cf415 16496 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
16497 {
16498 /* Special stub used, leave nop alone. */
16499 }
16500 else
a078d95a
AM
16501 bfd_put_32 (input_bfd,
16502 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7 16503 contents + rel->r_offset + 4);
0a1b45a2 16504 can_plt_call = true;
a7f2871e 16505 }
41bd81ab 16506 }
5bd4f169 16507 }
721956f4 16508
ba8ca3e7 16509 if (!can_plt_call && h != NULL)
721956f4 16510 {
ba8ca3e7
AM
16511 const char *name = h->elf.root.root.string;
16512
16513 if (*name == '.')
16514 ++name;
16515
3f3328b8 16516 if (startswith (name, "__libc_start_main")
ba8ca3e7 16517 && (name[17] == 0 || name[17] == '@'))
6ab189d5 16518 {
ba8ca3e7
AM
16519 /* Allow crt1 branch to go via a toc adjusting
16520 stub. Other calls that never return could do
16521 the same, if we could detect such. */
0a1b45a2 16522 can_plt_call = true;
6ab189d5 16523 }
ba8ca3e7
AM
16524 }
16525
16526 if (!can_plt_call)
16527 {
16528 /* g++ as of 20130507 emits self-calls without a
16529 following nop. This is arguably wrong since we
16530 have conflicting information. On the one hand a
16531 global symbol and on the other a local call
16532 sequence, but don't error for this special case.
16533 It isn't possible to cheaply verify we have
16534 exactly such a call. Allow all calls to the same
16535 section. */
16536 asection *code_sec = sec;
16537
16538 if (get_opd_info (sec) != NULL)
ad8e1ba5 16539 {
ba8ca3e7
AM
16540 bfd_vma off = (relocation + addend
16541 - sec->output_section->vma
16542 - sec->output_offset);
bc30df16 16543
0a1b45a2 16544 opd_entry_value (sec, off, &code_sec, NULL, false);
ad8e1ba5 16545 }
ba8ca3e7 16546 if (code_sec == input_section)
0a1b45a2 16547 can_plt_call = true;
ba8ca3e7
AM
16548 }
16549
16550 if (!can_plt_call)
16551 {
7aba54da 16552 if (stub_entry->type.main == ppc_stub_plt_call)
4805fc55 16553 info->callbacks->einfo
695344c0 16554 /* xgettext:c-format */
c1c8c1ef 16555 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16556 "(plt call stub)\n"),
4805fc55
AM
16557 input_bfd, input_section, rel->r_offset, sym_name);
16558 else
16559 info->callbacks->einfo
695344c0 16560 /* xgettext:c-format */
c1c8c1ef 16561 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16562 "(toc save/adjust stub)\n"),
4805fc55 16563 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
16564
16565 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16566 ret = false;
721956f4
AM
16567 }
16568
b25116a9 16569 if (can_plt_call
7aba54da 16570 && stub_entry->type.main == ppc_stub_plt_call)
0a1b45a2 16571 unresolved_reloc = false;
b25116a9
AM
16572 }
16573
6abec6bc 16574 if ((stub_entry == NULL
7aba54da
AM
16575 || stub_entry->type.main == ppc_stub_long_branch
16576 || stub_entry->type.main == ppc_stub_plt_branch)
8387904d
AM
16577 && get_opd_info (sec) != NULL)
16578 {
16579 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
16580 bfd_vma off = (relocation + addend
16581 - sec->output_section->vma
16582 - sec->output_offset);
0a1b45a2 16583 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
8387904d
AM
16584 if (dest != (bfd_vma) -1)
16585 {
16586 relocation = dest;
16587 addend = 0;
bc30df16 16588 reloc_dest = DEST_OPD;
8387904d
AM
16589 }
16590 }
16591
b25116a9
AM
16592 /* If the branch is out of reach we ought to have a long
16593 branch stub. */
16594 from = (rel->r_offset
16595 + input_section->output_offset
16596 + input_section->output_section->vma);
16597
6911b7dc
AM
16598 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16599 ? fdh->elf.other
16600 : sym->st_other);
16601
6abec6bc 16602 if (stub_entry != NULL
7aba54da
AM
16603 && (stub_entry->type.main == ppc_stub_long_branch
16604 || stub_entry->type.main == ppc_stub_plt_branch))
16605 {
16606 if (stub_entry->type.sub == ppc_stub_toc
16607 && !stub_entry->type.r2save
16608 && (r_type == R_PPC64_ADDR14_BRTAKEN
16609 || r_type == R_PPC64_ADDR14_BRNTAKEN
16610 || (relocation + addend - from + max_br_offset
16611 < 2 * max_br_offset)))
16612 /* Don't use the stub if this branch is in range. */
16613 stub_entry = NULL;
16614
16615 if (stub_entry != NULL
16616 && stub_entry->type.sub >= ppc_stub_notoc
16617 && ((r_type != R_PPC64_REL24_NOTOC
16618 && r_type != R_PPC64_REL24_P9NOTOC)
16619 || ((fdh ? fdh->elf.other : sym->st_other)
16620 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16621 && (relocation + addend - from + max_br_offset
16622 < 2 * max_br_offset))
16623 stub_entry = NULL;
16624
16625 if (stub_entry != NULL
16626 && stub_entry->type.r2save
16627 && (r_type == R_PPC64_REL24_NOTOC
16628 || r_type == R_PPC64_REL24_P9NOTOC)
16629 && (relocation + addend - from + max_br_offset
16630 < 2 * max_br_offset))
16631 stub_entry = NULL;
16632 }
05d0e962 16633
b25116a9
AM
16634 if (stub_entry != NULL)
16635 {
16636 /* Munge up the value and addend so that we call the stub
16637 rather than the procedure directly. */
a4b6fadd
AM
16638 asection *stub_sec = stub_entry->group->stub_sec;
16639
7aba54da 16640 if (stub_entry->type.main == ppc_stub_save_res)
a4b6fadd
AM
16641 relocation += (stub_sec->output_offset
16642 + stub_sec->output_section->vma
16643 + stub_sec->size - htab->sfpr->size
16644 - htab->sfpr->output_offset
16645 - htab->sfpr->output_section->vma);
16646 else
16647 relocation = (stub_entry->stub_offset
16648 + stub_sec->output_offset
16649 + stub_sec->output_section->vma);
b25116a9 16650 addend = 0;
bc30df16 16651 reloc_dest = DEST_STUB;
3b421ab3 16652
7aba54da
AM
16653 if (((stub_entry->type.r2save
16654 && (r_type == R_PPC64_REL24_NOTOC
16655 || r_type == R_PPC64_REL24_P9NOTOC))
16656 || ((stub_entry->type.main == ppc_stub_plt_call
16657 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16658 && rel + 1 < relend
16659 && rel[1].r_offset == rel->r_offset + 4
16660 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16661 && !(stub_entry->type.main == ppc_stub_plt_call
16662 && htab->params->tls_get_addr_opt
16663 && h != NULL
16664 && is_tls_get_addr (&h->elf, htab)))
29433886
AM
16665 {
16666 /* Skip over the r2 store at the start of the stub. */
7aba54da 16667 relocation += 4;
29433886 16668 }
5663e321 16669
7aba54da
AM
16670 if ((r_type == R_PPC64_REL24_NOTOC
16671 || r_type == R_PPC64_REL24_P9NOTOC)
16672 && stub_entry->type.main == ppc_stub_plt_call
16673 && stub_entry->type.sub >= ppc_stub_notoc)
5663e321 16674 htab->notoc_plt = 1;
b25116a9
AM
16675 }
16676
16677 if (insn != 0)
16678 {
794e51c0 16679 if (is_isa_v2)
721956f4 16680 {
b25116a9
AM
16681 /* Set 'a' bit. This is 0b00010 in BO field for branch
16682 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16683 for branch on CTR insns (BO == 1a00t or 1a01t). */
16684 if ((insn & (0x14 << 21)) == (0x04 << 21))
16685 insn |= 0x02 << 21;
16686 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16687 insn |= 0x08 << 21;
16688 else
16689 break;
16690 }
16691 else
16692 {
16693 /* Invert 'y' bit if not the default. */
4cc603a5 16694 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16695 insn ^= 0x01 << 21;
721956f4 16696 }
b25116a9 16697
95f0d0d2 16698 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16699 }
e86ce104 16700
06da1e8e
AM
16701 /* NOP out calls to undefined weak functions.
16702 We can thus call a weak function without first
16703 checking whether the function is defined. */
b25116a9 16704 else if (h != NULL
039b3fef 16705 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16706 && h->elf.dynindx == -1
05d0e962 16707 && (r_type == R_PPC64_REL24
7aba54da
AM
16708 || r_type == R_PPC64_REL24_NOTOC
16709 || r_type == R_PPC64_REL24_P9NOTOC)
b25116a9 16710 && relocation == 0
733ae98c
AM
16711 && addend == 0
16712 && offset_in_range (input_section, rel->r_offset, 4))
e86ce104 16713 {
95f0d0d2 16714 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16715 goto copy_reloc;
e86ce104 16716 }
65f38f15 16717 break;
066f4018
AM
16718
16719 case R_PPC64_GOT16_DS:
6738c8a7 16720 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16721 || (bfd_link_pic (info)
16722 && sec == bfd_abs_section_ptr)
6738c8a7 16723 || !htab->do_toc_opt)
06507dab 16724 break;
066f4018
AM
16725 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16726 if (relocation + addend - from + 0x8000 < 0x10000
f5b9c288
AM
16727 && sec != NULL
16728 && sec->output_section != NULL
16729 && !discarded_section (sec)
733ae98c
AM
16730 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16731 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16732 {
16733 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16734 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16735 {
16736 insn += (14u << 26) - (58u << 26);
16737 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16738 r_type = R_PPC64_TOC16;
16739 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16740 }
16741 }
16742 break;
16743
16744 case R_PPC64_GOT16_LO_DS:
16745 case R_PPC64_GOT16_HA:
6738c8a7 16746 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16747 || (bfd_link_pic (info)
16748 && sec == bfd_abs_section_ptr)
6738c8a7 16749 || !htab->do_toc_opt)
06507dab 16750 break;
066f4018
AM
16751 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16752 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
f5b9c288
AM
16753 && sec != NULL
16754 && sec->output_section != NULL
16755 && !discarded_section (sec)
733ae98c
AM
16756 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16757 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16758 {
16759 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16760 if (r_type == R_PPC64_GOT16_LO_DS
16761 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16762 {
16763 insn += (14u << 26) - (58u << 26);
16764 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16765 r_type = R_PPC64_TOC16_LO;
16766 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16767 }
fba8689a
AM
16768 else if (r_type == R_PPC64_GOT16_HA
16769 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16770 {
16771 r_type = R_PPC64_TOC16_HA;
16772 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16773 }
16774 }
16775 break;
4a421c53
AM
16776
16777 case R_PPC64_GOT_PCREL34:
6738c8a7 16778 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16779 || (bfd_link_pic (info)
16780 && sec == bfd_abs_section_ptr)
6738c8a7 16781 || !htab->do_toc_opt)
06507dab 16782 break;
4a421c53
AM
16783 from = (rel->r_offset
16784 + input_section->output_section->vma
16785 + input_section->output_offset);
6738c8a7 16786 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
f5b9c288
AM
16787 && sec != NULL
16788 && sec->output_section != NULL
16789 && !discarded_section (sec)
733ae98c
AM
16790 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16791 && offset_in_range (input_section, rel->r_offset, 8)))
6738c8a7
AM
16792 break;
16793
16794 offset = rel->r_offset;
16795 pinsn = bfd_get_32 (input_bfd, contents + offset);
16796 pinsn <<= 32;
16797 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16798 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16799 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16800 break;
16801
16802 /* Replace with paddi. */
16803 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16804 r_type = R_PPC64_PCREL34;
16805 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16806 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16807 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16808 /* Fall through. */
4a421c53
AM
16809
16810 case R_PPC64_PCREL34:
6738c8a7
AM
16811 if (!htab->params->no_pcrel_opt
16812 && rel + 1 < relend
16813 && rel[1].r_offset == rel->r_offset
16814 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
733ae98c
AM
16815 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16816 && offset_in_range (input_section, rel->r_offset, 8))
4a421c53
AM
16817 {
16818 offset = rel->r_offset;
16819 pinsn = bfd_get_32 (input_bfd, contents + offset);
16820 pinsn <<= 32;
16821 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16822 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16823 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16824 | (14ULL << 26) /* paddi */))
16825 {
6738c8a7
AM
16826 bfd_vma off2 = rel[1].r_addend;
16827 if (off2 == 0)
16828 /* zero means next insn. */
16829 off2 = 8;
16830 off2 += offset;
733ae98c 16831 if (offset_in_range (input_section, off2, 4))
4a421c53 16832 {
6738c8a7
AM
16833 uint64_t pinsn2;
16834 bfd_signed_vma addend_off;
16835 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16836 pinsn2 <<= 32;
16837 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16838 {
733ae98c 16839 if (!offset_in_range (input_section, off2, 8))
6738c8a7
AM
16840 break;
16841 pinsn2 |= bfd_get_32 (input_bfd,
16842 contents + off2 + 4);
16843 }
16844 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16845 {
16846 addend += addend_off;
16847 rel->r_addend = addend;
16848 bfd_put_32 (input_bfd, pinsn >> 32,
16849 contents + offset);
16850 bfd_put_32 (input_bfd, pinsn,
16851 contents + offset + 4);
16852 bfd_put_32 (input_bfd, pinsn2 >> 32,
16853 contents + off2);
4a421c53 16854 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16855 bfd_put_32 (input_bfd, pinsn2,
16856 contents + off2 + 4);
4a421c53
AM
16857 }
16858 }
16859 }
16860 }
16861 break;
65f38f15 16862 }
5bd4f169 16863
411e1bfb 16864 tls_type = 0;
23cedd1d 16865 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16866 switch (r_type)
16867 {
16868 default:
cf97bcb0
AM
16869 /* xgettext:c-format */
16870 _bfd_error_handler (_("%pB: %s unsupported"),
16871 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16872
65f38f15 16873 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16874 ret = false;
c316a17c 16875 goto copy_reloc;
5bd4f169 16876
65f38f15 16877 case R_PPC64_NONE:
411e1bfb 16878 case R_PPC64_TLS:
727fc41e
AM
16879 case R_PPC64_TLSGD:
16880 case R_PPC64_TLSLD:
3b421ab3 16881 case R_PPC64_TOCSAVE:
04c9666a
AM
16882 case R_PPC64_GNU_VTINHERIT:
16883 case R_PPC64_GNU_VTENTRY:
006589cf 16884 case R_PPC64_ENTRY:
4a421c53 16885 case R_PPC64_PCREL_OPT:
c316a17c 16886 goto copy_reloc;
5bd4f169
AM
16887
16888 /* GOT16 relocations. Like an ADDR16 using the symbol's
16889 address in the GOT as relocation value instead of the
411e1bfb 16890 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16891 symbol and put the symbol value there. */
411e1bfb
AM
16892 case R_PPC64_GOT_TLSGD16:
16893 case R_PPC64_GOT_TLSGD16_LO:
16894 case R_PPC64_GOT_TLSGD16_HI:
16895 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16896 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16897 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16898 goto dogot;
16899
16900 case R_PPC64_GOT_TLSLD16:
16901 case R_PPC64_GOT_TLSLD16_LO:
16902 case R_PPC64_GOT_TLSLD16_HI:
16903 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16904 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16905 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16906 goto dogot;
16907
16908 case R_PPC64_GOT_TPREL16_DS:
16909 case R_PPC64_GOT_TPREL16_LO_DS:
16910 case R_PPC64_GOT_TPREL16_HI:
16911 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16912 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16913 tls_type = TLS_TLS | TLS_TPREL;
16914 goto dogot;
16915
16916 case R_PPC64_GOT_DTPREL16_DS:
16917 case R_PPC64_GOT_DTPREL16_LO_DS:
16918 case R_PPC64_GOT_DTPREL16_HI:
16919 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16920 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16921 tls_type = TLS_TLS | TLS_DTPREL;
16922 goto dogot;
16923
65f38f15
AM
16924 case R_PPC64_GOT16:
16925 case R_PPC64_GOT16_LO:
16926 case R_PPC64_GOT16_HI:
16927 case R_PPC64_GOT16_HA:
16928 case R_PPC64_GOT16_DS:
16929 case R_PPC64_GOT16_LO_DS:
5663e321 16930 case R_PPC64_GOT_PCREL34:
411e1bfb 16931 dogot:
5bd4f169
AM
16932 {
16933 /* Relocation is to the entry for this symbol in the global
16934 offset table. */
e717da7e 16935 asection *got;
d881513a 16936 bfd_vma *offp;
5bd4f169 16937 bfd_vma off;
d881513a 16938 unsigned long indx = 0;
927be08e 16939 struct got_entry *ent;
65f38f15 16940
d881513a 16941 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16942 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16943 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16944 else
5bd4f169 16945 {
d881513a
AM
16946 if (h != NULL)
16947 {
f0158f44
AM
16948 if (!htab->elf.dynamic_sections_created
16949 || h->elf.dynindx == -1
16950 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16951 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16952 /* This is actually a static link, or it is a
16953 -Bsymbolic link and the symbol is defined
16954 locally, or the symbol was forced to be local
16955 because of a version file. */
16956 ;
16957 else
16958 {
039b3fef 16959 indx = h->elf.dynindx;
0a1b45a2 16960 unresolved_reloc = false;
d881513a 16961 }
039b3fef 16962 ent = h->elf.got.glist;
d881513a 16963 }
411e1bfb 16964 else
5bd4f169 16965 {
d881513a
AM
16966 if (local_got_ents == NULL)
16967 abort ();
16968 ent = local_got_ents[r_symndx];
5bd4f169 16969 }
d881513a
AM
16970
16971 for (; ent != NULL; ent = ent->next)
133a1f60 16972 if (ent->addend == orig_rel.r_addend
e717da7e 16973 && ent->owner == input_bfd
d881513a
AM
16974 && ent->tls_type == tls_type)
16975 break;
5bd4f169 16976 }
411e1bfb 16977
927be08e
AM
16978 if (ent == NULL)
16979 abort ();
16980 if (ent->is_indirect)
16981 ent = ent->got.ent;
16982 offp = &ent->got.offset;
16983 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16984 if (got == NULL)
16985 abort ();
16986
411e1bfb
AM
16987 /* The offset must always be a multiple of 8. We use the
16988 least significant bit to record whether we have already
16989 processed this entry. */
d881513a 16990 off = *offp;
411e1bfb
AM
16991 if ((off & 1) != 0)
16992 off &= ~1;
5bd4f169
AM
16993 else
16994 {
411e1bfb
AM
16995 /* Generate relocs for the dynamic linker, except in
16996 the case of TLSLD where we'll use one entry per
16997 module. */
25f23106 16998 asection *relgot;
0a1b45a2 16999 bool ifunc;
e717da7e 17000
d881513a 17001 *offp = off | 1;
25f23106
AM
17002 relgot = NULL;
17003 ifunc = (h != NULL
17004 ? h->elf.type == STT_GNU_IFUNC
17005 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 17006 if (ifunc)
82e66161
AM
17007 {
17008 relgot = htab->elf.irelplt;
efb2a7b4 17009 if (indx == 0 || is_static_defined (&h->elf))
0a1b45a2 17010 htab->elf.ifunc_resolvers = true;
82e66161 17011 }
f0158f44
AM
17012 else if (indx != 0
17013 || (bfd_link_pic (info)
17014 && (h == NULL
f749f26e
AM
17015 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
17016 && !(tls_type != 0
f15d0b54 17017 && bfd_link_executable (info)
9e51d549
AM
17018 && (h == NULL
17019 || SYMBOL_REFERENCES_LOCAL (info,
3a3a4c1f
AM
17020 &h->elf)))
17021 && (h != NULL
17022 ? !bfd_is_abs_symbol (&h->elf.root)
17023 : sym->st_shndx != SHN_ABS)))
17024
19e08130 17025 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 17026 if (relgot != NULL)
5bd4f169 17027 {
e717da7e
AM
17028 outrel.r_offset = (got->output_section->vma
17029 + got->output_offset
411e1bfb 17030 + off);
133a1f60 17031 outrel.r_addend = orig_rel.r_addend;
d881513a 17032 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 17033 {
411e1bfb 17034 outrel.r_addend = 0;
e515b051 17035 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
17036 if (tls_type == (TLS_TLS | TLS_GD))
17037 {
066c738e
AM
17038 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17039 &outrel,
17040 relgot));
e515b051 17041 outrel.r_offset += 8;
133a1f60 17042 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
17043 outrel.r_info
17044 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 17045 }
411e1bfb 17046 }
951fd09b 17047 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 17048 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 17049 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 17050 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
17051 else if (indx != 0)
17052 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17053 else
81407a69 17054 {
25f23106
AM
17055 if (ifunc)
17056 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17057 else
17058 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
17059
17060 /* Write the .got section contents for the sake
17061 of prelink. */
e717da7e 17062 loc = got->contents + off;
23fbd6fa
JJ
17063 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17064 loc);
81407a69 17065 }
81407a69
AM
17066
17067 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
17068 {
17069 outrel.r_addend += relocation;
17070 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
17071 {
17072 if (htab->elf.tls_sec == NULL)
17073 outrel.r_addend = 0;
17074 else
17075 outrel.r_addend -= htab->elf.tls_sec->vma;
17076 }
e515b051 17077 }
1657026c
AM
17078 if (!(info->enable_dt_relr
17079 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
066c738e
AM
17080 BFD_ASSERT (count_and_swap_reloc_out (output_bfd,
17081 &outrel, relgot));
411e1bfb
AM
17082 }
17083
ad8e1ba5 17084 /* Init the .got section contents here if we're not
81407a69 17085 emitting a reloc. */
d881513a 17086 else
411e1bfb 17087 {
133a1f60 17088 relocation += orig_rel.r_addend;
f0158f44 17089 if (tls_type != 0)
411e1bfb 17090 {
989f9879
AM
17091 if (htab->elf.tls_sec == NULL)
17092 relocation = 0;
17093 else
17094 {
f0158f44
AM
17095 if (tls_type & TLS_LD)
17096 relocation = 0;
17097 else
17098 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 17099 if (tls_type & TLS_TPREL)
989f9879
AM
17100 relocation += DTP_OFFSET - TP_OFFSET;
17101 }
5bd4f169 17102
f0158f44 17103 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
17104 {
17105 bfd_put_64 (output_bfd, relocation,
e717da7e 17106 got->contents + off + 8);
676ee2b5 17107 relocation = 1;
7b609f53 17108 }
411e1bfb
AM
17109 }
17110 bfd_put_64 (output_bfd, relocation,
e717da7e 17111 got->contents + off);
5bd4f169
AM
17112 }
17113 }
17114
65f38f15
AM
17115 if (off >= (bfd_vma) -2)
17116 abort ();
17117
bf102f86 17118 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 17119 addend = 0;
c213164a 17120 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
17121 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17122 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17123 || r_type == R_PPC64_GOT_TPREL_PCREL34
17124 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 17125 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 17126 }
65f38f15
AM
17127 break;
17128
17129 case R_PPC64_PLT16_HA:
17130 case R_PPC64_PLT16_HI:
17131 case R_PPC64_PLT16_LO:
08be3224 17132 case R_PPC64_PLT16_LO_DS:
5663e321
AM
17133 case R_PPC64_PLT_PCREL34:
17134 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
17135 case R_PPC64_PLT32:
17136 case R_PPC64_PLT64:
23cedd1d 17137 case R_PPC64_PLTSEQ:
5663e321 17138 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 17139 case R_PPC64_PLTCALL:
5663e321 17140 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
17141 /* Relocation is to the entry for this symbol in the
17142 procedure linkage table. */
0a1b45a2 17143 unresolved_reloc = true;
cbf95972
AM
17144 {
17145 struct plt_entry **plt_list = NULL;
17146 if (h != NULL)
17147 plt_list = &h->elf.plt.plist;
17148 else if (local_got_ents != NULL)
17149 {
17150 struct plt_entry **local_plt = (struct plt_entry **)
17151 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 17152 plt_list = local_plt + r_symndx;
cbf95972
AM
17153 }
17154 if (plt_list)
17155 {
17156 struct plt_entry *ent;
65f38f15 17157
cbf95972
AM
17158 for (ent = *plt_list; ent != NULL; ent = ent->next)
17159 if (ent->plt.offset != (bfd_vma) -1
133a1f60 17160 && ent->addend == orig_rel.r_addend)
cbf95972
AM
17161 {
17162 asection *plt;
08be3224 17163 bfd_vma got;
cbf95972
AM
17164
17165 plt = htab->elf.splt;
30845f11 17166 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
17167 {
17168 if (h != NULL
17169 ? h->elf.type == STT_GNU_IFUNC
17170 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17171 plt = htab->elf.iplt;
17172 else
17173 plt = htab->pltlocal;
17174 }
17175 relocation = (plt->output_section->vma
17176 + plt->output_offset
17177 + ent->plt.offset);
08be3224 17178 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
17179 || r_type == R_PPC64_PLT16_HI
17180 || r_type == R_PPC64_PLT16_LO
17181 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
17182 {
17183 got = (elf_gp (output_bfd)
17184 + htab->sec_info[input_section->id].toc_off);
17185 relocation -= got;
17186 }
133a1f60 17187 addend = 0;
0a1b45a2 17188 unresolved_reloc = false;
cbf95972
AM
17189 break;
17190 }
17191 }
17192 }
65f38f15 17193 break;
5bd4f169 17194
0b13192e
AM
17195 case R_PPC64_TOC:
17196 /* Relocation value is TOC base. */
17197 relocation = TOCstart;
cf35638d 17198 if (r_symndx == STN_UNDEF)
6f20ed8a 17199 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
17200 else if (unresolved_reloc)
17201 ;
6f20ed8a
AM
17202 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17203 relocation += htab->sec_info[sec->id].toc_off;
0b13192e 17204 else
0a1b45a2 17205 unresolved_reloc = true;
10040916
AM
17206 if (unresolved_reloc
17207 || (!is_opd
17208 && h != NULL
17209 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17210 info->callbacks->einfo
17211 /* xgettext:c-format */
17212 (_("%H: %s against %pT is not supported\n"),
17213 input_bfd, input_section, rel->r_offset,
17214 ppc64_elf_howto_table[r_type]->name, sym_name);
ab96bf03 17215 goto dodyn;
0b13192e 17216
5bd4f169
AM
17217 /* TOC16 relocs. We want the offset relative to the TOC base,
17218 which is the address of the start of the TOC plus 0x8000.
17219 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17220 in this order. */
65f38f15
AM
17221 case R_PPC64_TOC16:
17222 case R_PPC64_TOC16_LO:
17223 case R_PPC64_TOC16_HI:
17224 case R_PPC64_TOC16_DS:
17225 case R_PPC64_TOC16_LO_DS:
17226 case R_PPC64_TOC16_HA:
6f20ed8a 17227 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
17228 if (h != NULL)
17229 goto dodyn;
5bd4f169
AM
17230 break;
17231
17232 /* Relocate against the beginning of the section. */
65f38f15
AM
17233 case R_PPC64_SECTOFF:
17234 case R_PPC64_SECTOFF_LO:
17235 case R_PPC64_SECTOFF_HI:
17236 case R_PPC64_SECTOFF_DS:
17237 case R_PPC64_SECTOFF_LO_DS:
17238 case R_PPC64_SECTOFF_HA:
4ce794b7 17239 if (sec != NULL)
65f38f15 17240 addend -= sec->output_section->vma;
5bd4f169
AM
17241 break;
17242
25f23106
AM
17243 case R_PPC64_REL16:
17244 case R_PPC64_REL16_LO:
17245 case R_PPC64_REL16_HI:
17246 case R_PPC64_REL16_HA:
4a969973
AM
17247 case R_PPC64_REL16_HIGH:
17248 case R_PPC64_REL16_HIGHA:
17249 case R_PPC64_REL16_HIGHER:
17250 case R_PPC64_REL16_HIGHERA:
17251 case R_PPC64_REL16_HIGHEST:
17252 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
17253 case R_PPC64_REL16_HIGHER34:
17254 case R_PPC64_REL16_HIGHERA34:
17255 case R_PPC64_REL16_HIGHEST34:
17256 case R_PPC64_REL16_HIGHESTA34:
a680de9a 17257 case R_PPC64_REL16DX_HA:
721956f4
AM
17258 case R_PPC64_REL14:
17259 case R_PPC64_REL14_BRNTAKEN:
17260 case R_PPC64_REL14_BRTAKEN:
5d1634d7 17261 case R_PPC64_REL24:
05d0e962 17262 case R_PPC64_REL24_NOTOC:
7aba54da 17263 case R_PPC64_REL24_P9NOTOC:
5663e321
AM
17264 case R_PPC64_PCREL34:
17265 case R_PPC64_PCREL28:
5d1634d7
AM
17266 break;
17267
411e1bfb
AM
17268 case R_PPC64_TPREL16:
17269 case R_PPC64_TPREL16_LO:
17270 case R_PPC64_TPREL16_HI:
17271 case R_PPC64_TPREL16_HA:
17272 case R_PPC64_TPREL16_DS:
17273 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
17274 case R_PPC64_TPREL16_HIGH:
17275 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
17276 case R_PPC64_TPREL16_HIGHER:
17277 case R_PPC64_TPREL16_HIGHERA:
17278 case R_PPC64_TPREL16_HIGHEST:
17279 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
17280 if (h != NULL
17281 && h->elf.root.type == bfd_link_hash_undefweak
733ae98c
AM
17282 && h->elf.dynindx == -1
17283 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
766bc656
AM
17284 {
17285 /* Make this relocation against an undefined weak symbol
17286 resolve to zero. This is really just a tweak, since
17287 code using weak externs ought to check that they are
17288 defined before using them. */
17289 bfd_byte *p = contents + rel->r_offset - d_offset;
17290
95f0d0d2 17291 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
17292 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17293 if (insn != 0)
95f0d0d2 17294 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
17295 break;
17296 }
beaddc1a
AM
17297 /* Fall through. */
17298
17299 case R_PPC64_TPREL34:
989f9879
AM
17300 if (htab->elf.tls_sec != NULL)
17301 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
17302 /* The TPREL16 relocs shouldn't really be used in shared
17303 libs or with non-local symbols as that will result in
17304 DT_TEXTREL being set, but support them anyway. */
17305 goto dodyn;
411e1bfb
AM
17306
17307 case R_PPC64_DTPREL16:
17308 case R_PPC64_DTPREL16_LO:
17309 case R_PPC64_DTPREL16_HI:
17310 case R_PPC64_DTPREL16_HA:
17311 case R_PPC64_DTPREL16_DS:
17312 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
17313 case R_PPC64_DTPREL16_HIGH:
17314 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
17315 case R_PPC64_DTPREL16_HIGHER:
17316 case R_PPC64_DTPREL16_HIGHERA:
17317 case R_PPC64_DTPREL16_HIGHEST:
17318 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 17319 case R_PPC64_DTPREL34:
989f9879
AM
17320 if (htab->elf.tls_sec != NULL)
17321 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
17322 break;
17323
45965137
AM
17324 case R_PPC64_ADDR64_LOCAL:
17325 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17326 ? h->elf.other
17327 : sym->st_other);
17328 break;
17329
e515b051
AM
17330 case R_PPC64_DTPMOD64:
17331 relocation = 1;
17332 addend = 0;
17333 goto dodyn;
17334
411e1bfb 17335 case R_PPC64_TPREL64:
989f9879
AM
17336 if (htab->elf.tls_sec != NULL)
17337 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
17338 goto dodyn;
17339
17340 case R_PPC64_DTPREL64:
989f9879
AM
17341 if (htab->elf.tls_sec != NULL)
17342 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 17343 /* Fall through. */
411e1bfb 17344
65f38f15
AM
17345 /* Relocations that may need to be propagated if this is a
17346 dynamic object. */
04c9666a 17347 case R_PPC64_REL30:
65f38f15
AM
17348 case R_PPC64_REL32:
17349 case R_PPC64_REL64:
17350 case R_PPC64_ADDR14:
17351 case R_PPC64_ADDR14_BRNTAKEN:
17352 case R_PPC64_ADDR14_BRTAKEN:
17353 case R_PPC64_ADDR16:
17354 case R_PPC64_ADDR16_DS:
17355 case R_PPC64_ADDR16_HA:
17356 case R_PPC64_ADDR16_HI:
f9c6b907
AM
17357 case R_PPC64_ADDR16_HIGH:
17358 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17359 case R_PPC64_ADDR16_HIGHER:
17360 case R_PPC64_ADDR16_HIGHERA:
17361 case R_PPC64_ADDR16_HIGHEST:
17362 case R_PPC64_ADDR16_HIGHESTA:
17363 case R_PPC64_ADDR16_LO:
17364 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
17365 case R_PPC64_ADDR16_HIGHER34:
17366 case R_PPC64_ADDR16_HIGHERA34:
17367 case R_PPC64_ADDR16_HIGHEST34:
17368 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 17369 case R_PPC64_ADDR24:
65f38f15
AM
17370 case R_PPC64_ADDR32:
17371 case R_PPC64_ADDR64:
17372 case R_PPC64_UADDR16:
17373 case R_PPC64_UADDR32:
17374 case R_PPC64_UADDR64:
5663e321
AM
17375 case R_PPC64_D34:
17376 case R_PPC64_D34_LO:
17377 case R_PPC64_D34_HI30:
17378 case R_PPC64_D34_HA30:
17379 case R_PPC64_D28:
411e1bfb 17380 dodyn:
5d1634d7 17381 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
17382 break;
17383
41bd81ab
AM
17384 if (NO_OPD_RELOCS && is_opd)
17385 break;
17386
8a9e8e72 17387 if (bfd_link_pic (info)
b1b07054 17388 ? ((h == NULL
190eb1dd 17389 || h->elf.dyn_relocs != NULL)
b1b07054
AM
17390 && ((h != NULL && pc_dynrelocs (h))
17391 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 17392 : (h != NULL
190eb1dd 17393 ? h->elf.dyn_relocs != NULL
d311bc8b 17394 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 17395 {
0a1b45a2 17396 bool skip, relocate;
65f38f15 17397 asection *sreloc;
1cf1f670 17398 bfd_vma out_off;
82e66161 17399 long indx = 0;
65f38f15
AM
17400
17401 /* When generating a dynamic object, these relocations
17402 are copied into the output file to be resolved at run
17403 time. */
17404
0a1b45a2
AM
17405 skip = false;
17406 relocate = false;
65f38f15 17407
1cf1f670
AM
17408 out_off = _bfd_elf_section_offset (output_bfd, info,
17409 input_section, rel->r_offset);
17410 if (out_off == (bfd_vma) -1)
0a1b45a2 17411 skip = true;
1cf1f670 17412 else if (out_off == (bfd_vma) -2)
0a1b45a2 17413 skip = true, relocate = true;
1cf1f670
AM
17414 out_off += (input_section->output_section->vma
17415 + input_section->output_offset);
17416 outrel.r_offset = out_off;
411e1bfb 17417 outrel.r_addend = rel->r_addend;
65f38f15 17418
1cf1f670
AM
17419 /* Optimize unaligned reloc use. */
17420 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17421 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17422 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17423 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17424 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17425 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17426 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17427 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17428 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17429
65f38f15 17430 if (skip)
0bb2d96a 17431 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
17432 else if (h != NULL
17433 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
17434 && !is_opd
17435 && r_type != R_PPC64_TOC)
14acf4dc 17436 {
82e66161
AM
17437 indx = h->elf.dynindx;
17438 BFD_ASSERT (indx != -1);
17439 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 17440 }
65f38f15
AM
17441 else
17442 {
41bd81ab
AM
17443 /* This symbol is local, or marked to become local,
17444 or this is an opd section reloc which must point
17445 at a local function. */
65f38f15 17446 outrel.r_addend += relocation;
e86ce104 17447 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 17448 {
3fad3c7c 17449 if (is_opd && h != NULL)
afbe61cf
AM
17450 {
17451 /* Lie about opd entries. This case occurs
17452 when building shared libraries and we
17453 reference a function in another shared
3fad3c7c
AM
17454 lib. The same thing happens for a weak
17455 definition in an application that's
17456 overridden by a strong definition in a
17457 shared lib. (I believe this is a generic
17458 bug in binutils handling of weak syms.)
17459 In these cases we won't use the opd
1e2f5b6e 17460 entry in this lib. */
0a1b45a2 17461 unresolved_reloc = false;
afbe61cf 17462 }
25f23106
AM
17463 if (!is_opd
17464 && r_type == R_PPC64_ADDR64
17465 && (h != NULL
17466 ? h->elf.type == STT_GNU_IFUNC
17467 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17468 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17469 else
17470 {
17471 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 17472
25f23106
AM
17473 /* We need to relocate .opd contents for ld.so.
17474 Prelink also wants simple and consistent rules
17475 for relocs. This make all RELATIVE relocs have
17476 *r_offset equal to r_addend. */
0a1b45a2 17477 relocate = true;
25f23106 17478 }
65f38f15
AM
17479 }
17480 else
17481 {
25f23106
AM
17482 if (h != NULL
17483 ? h->elf.type == STT_GNU_IFUNC
17484 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17485 {
25f53a85 17486 info->callbacks->einfo
695344c0 17487 /* xgettext:c-format */
174d0a74 17488 (_("%H: %s for indirect "
c1c8c1ef 17489 "function `%pT' unsupported\n"),
25f53a85 17490 input_bfd, input_section, rel->r_offset,
25f23106
AM
17491 ppc64_elf_howto_table[r_type]->name,
17492 sym_name);
0a1b45a2 17493 ret = false;
25f23106 17494 }
cf35638d 17495 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
17496 ;
17497 else if (sec == NULL || sec->owner == NULL)
17498 {
17499 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17500 return false;
65f38f15
AM
17501 }
17502 else
17503 {
f26a3287 17504 asection *osec = sec->output_section;
65f38f15 17505
f26a3287
AM
17506 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17507 {
17508 /* TLS symbol values are relative to the
17509 TLS segment. Dynamic relocations for
17510 local TLS symbols therefore can't be
17511 reduced to a relocation against their
17512 section symbol because it holds the
17513 address of the section, not a value
17514 relative to the TLS segment. We could
17515 change the .tdata dynamic section symbol
17516 to be zero value but STN_UNDEF works
17517 and is used elsewhere, eg. for TPREL64
17518 GOT relocs against local TLS symbols. */
17519 osec = htab->elf.tls_sec;
17520 indx = 0;
17521 }
17522 else
74541ad4 17523 {
74541ad4 17524 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
17525 if (indx == 0)
17526 {
17527 if ((osec->flags & SEC_READONLY) == 0
17528 && htab->elf.data_index_section != NULL)
17529 osec = htab->elf.data_index_section;
17530 else
17531 osec = htab->elf.text_index_section;
17532 indx = elf_section_data (osec)->dynindx;
17533 }
17534 BFD_ASSERT (indx != 0);
74541ad4 17535 }
74541ad4 17536
65f38f15
AM
17537 /* We are turning this relocation into one
17538 against a section symbol, so subtract out
17539 the output section's address but not the
17540 offset of the input section in the output
17541 section. */
17542 outrel.r_addend -= osec->vma;
17543 }
17544
17545 outrel.r_info = ELF64_R_INFO (indx, r_type);
17546 }
17547 }
17548
1657026c
AM
17549 if (!(info->enable_dt_relr
17550 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
f91074eb
AM
17551 && maybe_relr (ELF64_R_TYPE (orig_rel.r_info),
17552 rel, input_section)))
82e66161 17553 {
1657026c
AM
17554 sreloc = elf_section_data (input_section)->sreloc;
17555 if (h != NULL
17556 ? h->elf.type == STT_GNU_IFUNC
17557 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17558 {
17559 sreloc = htab->elf.irelplt;
17560 if (indx == 0 || is_static_defined (&h->elf))
17561 htab->elf.ifunc_resolvers = true;
17562 }
17563 if (sreloc == NULL)
17564 abort ();
17565
066c738e
AM
17566 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &outrel,
17567 sreloc));
82e66161 17568 }
65f38f15 17569
e59a1001
AM
17570 if (!warned_dynamic
17571 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17572 {
17573 info->callbacks->einfo
17574 /* xgettext:c-format */
17575 (_("%X%P: %pB: %s against %pT "
17576 "is not supported by glibc as a dynamic relocation\n"),
17577 input_bfd,
17578 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17579 sym_name);
0a1b45a2 17580 warned_dynamic = true;
e59a1001
AM
17581 }
17582
65f38f15
AM
17583 /* If this reloc is against an external symbol, it will
17584 be computed at runtime, so there's no need to do
81407a69
AM
17585 anything now. However, for the sake of prelink ensure
17586 that the section contents are a known value. */
2cdcc330 17587 if (!relocate)
81407a69 17588 {
0a1b45a2 17589 unresolved_reloc = false;
81407a69
AM
17590 /* The value chosen here is quite arbitrary as ld.so
17591 ignores section contents except for the special
17592 case of .opd where the contents might be accessed
17593 before relocation. Choose zero, as that won't
17594 cause reloc overflow. */
17595 relocation = 0;
17596 addend = 0;
17597 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17598 to improve backward compatibility with older
17599 versions of ld. */
17600 if (r_type == R_PPC64_ADDR64)
17601 addend = outrel.r_addend;
17602 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 17603 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 17604 addend = outrel.r_offset;
81407a69 17605 }
65f38f15 17606 }
5bd4f169
AM
17607 break;
17608
65f38f15
AM
17609 case R_PPC64_COPY:
17610 case R_PPC64_GLOB_DAT:
17611 case R_PPC64_JMP_SLOT:
25f23106 17612 case R_PPC64_JMP_IREL:
65f38f15
AM
17613 case R_PPC64_RELATIVE:
17614 /* We shouldn't ever see these dynamic relocs in relocatable
17615 files. */
ae9a127f 17616 /* Fall through. */
65f38f15
AM
17617
17618 case R_PPC64_PLTGOT16:
17619 case R_PPC64_PLTGOT16_DS:
17620 case R_PPC64_PLTGOT16_HA:
17621 case R_PPC64_PLTGOT16_HI:
17622 case R_PPC64_PLTGOT16_LO:
17623 case R_PPC64_PLTGOT16_LO_DS:
17624 case R_PPC64_PLTREL32:
17625 case R_PPC64_PLTREL64:
17626 /* These ones haven't been implemented yet. */
17627
25f53a85 17628 info->callbacks->einfo
695344c0 17629 /* xgettext:c-format */
c1c8c1ef 17630 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 17631 input_bfd,
4ce794b7 17632 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
17633
17634 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 17635 ret = false;
c316a17c 17636 goto copy_reloc;
65f38f15 17637 }
5bd4f169 17638
67f0cbdb
AM
17639 /* Multi-instruction sequences that access the TOC can be
17640 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 17641 to nop; addi rb,r2,x; */
67f0cbdb
AM
17642 switch (r_type)
17643 {
17644 default:
17645 break;
17646
17647 case R_PPC64_GOT_TLSLD16_HI:
17648 case R_PPC64_GOT_TLSGD16_HI:
17649 case R_PPC64_GOT_TPREL16_HI:
17650 case R_PPC64_GOT_DTPREL16_HI:
17651 case R_PPC64_GOT16_HI:
17652 case R_PPC64_TOC16_HI:
17653 /* These relocs would only be useful if building up an
17654 offset to later add to r2, perhaps in an indexed
17655 addressing mode instruction. Don't try to optimize.
17656 Unfortunately, the possibility of someone building up an
17657 offset like this or even with the HA relocs, means that
17658 we need to check the high insn when optimizing the low
17659 insn. */
17660 break;
17661
5663e321
AM
17662 case R_PPC64_PLTCALL_NOTOC:
17663 if (!unresolved_reloc)
17664 htab->notoc_plt = 1;
17665 /* Fall through. */
23cedd1d 17666 case R_PPC64_PLTCALL:
733ae98c
AM
17667 if (unresolved_reloc
17668 && offset_in_range (input_section, rel->r_offset,
17669 r_type == R_PPC64_PLTCALL ? 8 : 4))
23cedd1d
AM
17670 {
17671 /* No plt entry. Make this into a direct call. */
17672 bfd_byte *p = contents + rel->r_offset;
17673 insn = bfd_get_32 (input_bfd, p);
17674 insn &= 1;
17675 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
17676 if (r_type == R_PPC64_PLTCALL)
17677 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
17678 unresolved_reloc = save_unresolved_reloc;
17679 r_type = R_PPC64_REL24;
17680 }
17681 break;
17682
5663e321 17683 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17684 case R_PPC64_PLTSEQ:
17685 if (unresolved_reloc)
17686 {
0a1b45a2 17687 unresolved_reloc = false;
23cedd1d
AM
17688 goto nop_it;
17689 }
17690 break;
17691
5663e321
AM
17692 case R_PPC64_PLT_PCREL34_NOTOC:
17693 if (!unresolved_reloc)
17694 htab->notoc_plt = 1;
17695 /* Fall through. */
17696 case R_PPC64_PLT_PCREL34:
733ae98c
AM
17697 if (unresolved_reloc
17698 && offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17699 {
17700 bfd_byte *p = contents + rel->r_offset;
17701 bfd_put_32 (input_bfd, PNOP >> 32, p);
17702 bfd_put_32 (input_bfd, PNOP, p + 4);
0a1b45a2 17703 unresolved_reloc = false;
5663e321
AM
17704 goto copy_reloc;
17705 }
17706 break;
17707
23cedd1d
AM
17708 case R_PPC64_PLT16_HA:
17709 if (unresolved_reloc)
17710 {
0a1b45a2 17711 unresolved_reloc = false;
23cedd1d
AM
17712 goto nop_it;
17713 }
17714 /* Fall through. */
67f0cbdb
AM
17715 case R_PPC64_GOT_TLSLD16_HA:
17716 case R_PPC64_GOT_TLSGD16_HA:
17717 case R_PPC64_GOT_TPREL16_HA:
17718 case R_PPC64_GOT_DTPREL16_HA:
17719 case R_PPC64_GOT16_HA:
17720 case R_PPC64_TOC16_HA:
98528052 17721 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
3a3a4c1f
AM
17722 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17723 && !(bfd_link_pic (info)
17724 && (h != NULL
17725 ? bfd_is_abs_symbol (&h->elf.root)
17726 : sec == bfd_abs_section_ptr)))
98528052 17727 {
23cedd1d
AM
17728 bfd_byte *p;
17729 nop_it:
733ae98c
AM
17730 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17731 {
17732 p = contents + (rel->r_offset & ~3);
17733 bfd_put_32 (input_bfd, NOP, p);
17734 goto copy_reloc;
17735 }
98528052 17736 }
67f0cbdb
AM
17737 break;
17738
23cedd1d
AM
17739 case R_PPC64_PLT16_LO:
17740 case R_PPC64_PLT16_LO_DS:
17741 if (unresolved_reloc)
17742 {
0a1b45a2 17743 unresolved_reloc = false;
23cedd1d
AM
17744 goto nop_it;
17745 }
17746 /* Fall through. */
67f0cbdb
AM
17747 case R_PPC64_GOT_TLSLD16_LO:
17748 case R_PPC64_GOT_TLSGD16_LO:
17749 case R_PPC64_GOT_TPREL16_LO_DS:
17750 case R_PPC64_GOT_DTPREL16_LO_DS:
17751 case R_PPC64_GOT16_LO:
17752 case R_PPC64_GOT16_LO_DS:
17753 case R_PPC64_TOC16_LO:
17754 case R_PPC64_TOC16_LO_DS:
98528052 17755 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
733ae98c 17756 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
3a3a4c1f
AM
17757 && !(bfd_link_pic (info)
17758 && (h != NULL
17759 ? bfd_is_abs_symbol (&h->elf.root)
17760 : sec == bfd_abs_section_ptr))
733ae98c 17761 && offset_in_range (input_section, rel->r_offset & ~3, 4))
67f0cbdb
AM
17762 {
17763 bfd_byte *p = contents + (rel->r_offset & ~3);
17764 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17765 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17766 {
17767 /* Transform addic to addi when we change reg. */
2365f8d7 17768 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17769 insn |= (14u << 26) | (2 << 16);
17770 }
17771 else
67f0cbdb 17772 {
98528052
AM
17773 insn &= ~(0x1f << 16);
17774 insn |= 2 << 16;
67f0cbdb 17775 }
560c8763 17776 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17777 }
17778 break;
9a23f96e
AM
17779
17780 case R_PPC64_TPREL16_HA:
733ae98c
AM
17781 if (htab->do_tls_opt
17782 && relocation + addend + 0x8000 < 0x10000
17783 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17784 {
17785 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17786 bfd_put_32 (input_bfd, NOP, p);
17787 goto copy_reloc;
9a23f96e
AM
17788 }
17789 break;
17790
17791 case R_PPC64_TPREL16_LO:
17792 case R_PPC64_TPREL16_LO_DS:
733ae98c
AM
17793 if (htab->do_tls_opt
17794 && relocation + addend + 0x8000 < 0x10000
17795 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17796 {
17797 bfd_byte *p = contents + (rel->r_offset & ~3);
17798 insn = bfd_get_32 (input_bfd, p);
17799 insn &= ~(0x1f << 16);
17800 insn |= 13 << 16;
17801 bfd_put_32 (input_bfd, insn, p);
17802 }
17803 break;
67f0cbdb
AM
17804 }
17805
65f38f15
AM
17806 /* Do any further special processing. */
17807 switch (r_type)
17808 {
17809 default:
17810 break;
17811
25f23106 17812 case R_PPC64_REL16_HA:
4a969973
AM
17813 case R_PPC64_REL16_HIGHA:
17814 case R_PPC64_REL16_HIGHERA:
17815 case R_PPC64_REL16_HIGHESTA:
a680de9a 17816 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17817 case R_PPC64_ADDR16_HA:
17818 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17819 case R_PPC64_ADDR16_HIGHERA:
17820 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17821 case R_PPC64_TOC16_HA:
17822 case R_PPC64_SECTOFF_HA:
411e1bfb 17823 case R_PPC64_TPREL16_HA:
f9c6b907 17824 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17825 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17826 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17827 case R_PPC64_DTPREL16_HA:
17828 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17829 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17830 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17831 /* It's just possible that this symbol is a weak symbol
17832 that's not actually defined anywhere. In that case,
17833 'sec' would be NULL, and we should leave the symbol
17834 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17835 if (sec == NULL)
17836 break;
1a0670f3 17837 /* Fall through. */
5c5f6e17
AM
17838
17839 case R_PPC64_GOT16_HA:
17840 case R_PPC64_PLTGOT16_HA:
17841 case R_PPC64_PLT16_HA:
17842 case R_PPC64_GOT_TLSGD16_HA:
17843 case R_PPC64_GOT_TLSLD16_HA:
17844 case R_PPC64_GOT_TPREL16_HA:
17845 case R_PPC64_GOT_DTPREL16_HA:
17846 /* Add 0x10000 if sign bit in 0:15 is set.
17847 Bits 0:15 are not used. */
17848 addend += 0x8000;
65f38f15
AM
17849 break;
17850
5663e321
AM
17851 case R_PPC64_D34_HA30:
17852 case R_PPC64_ADDR16_HIGHERA34:
17853 case R_PPC64_ADDR16_HIGHESTA34:
17854 case R_PPC64_REL16_HIGHERA34:
17855 case R_PPC64_REL16_HIGHESTA34:
17856 if (sec != NULL)
17857 addend += 1ULL << 33;
17858 break;
17859
65f38f15
AM
17860 case R_PPC64_ADDR16_DS:
17861 case R_PPC64_ADDR16_LO_DS:
17862 case R_PPC64_GOT16_DS:
17863 case R_PPC64_GOT16_LO_DS:
17864 case R_PPC64_PLT16_LO_DS:
17865 case R_PPC64_SECTOFF_DS:
17866 case R_PPC64_SECTOFF_LO_DS:
17867 case R_PPC64_TOC16_DS:
17868 case R_PPC64_TOC16_LO_DS:
17869 case R_PPC64_PLTGOT16_DS:
17870 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17871 case R_PPC64_GOT_TPREL16_DS:
17872 case R_PPC64_GOT_TPREL16_LO_DS:
17873 case R_PPC64_GOT_DTPREL16_DS:
17874 case R_PPC64_GOT_DTPREL16_LO_DS:
17875 case R_PPC64_TPREL16_DS:
17876 case R_PPC64_TPREL16_LO_DS:
17877 case R_PPC64_DTPREL16_DS:
17878 case R_PPC64_DTPREL16_LO_DS:
733ae98c
AM
17879 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17880 break;
adadcc0c
AM
17881 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17882 mask = 3;
a680de9a
PB
17883 /* If this reloc is against an lq, lxv, or stxv insn, then
17884 the value must be a multiple of 16. This is somewhat of
17885 a hack, but the "correct" way to do this by defining _DQ
17886 forms of all the _DS relocs bloats all reloc switches in
17887 this file. It doesn't make much sense to use these
17888 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17889 if ((insn & (0x3fu << 26)) == (56u << 26)
17890 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17891 mask = 15;
a680de9a
PB
17892 relocation += addend;
17893 addend = insn & (mask ^ 3);
17894 if ((relocation & mask) != 0)
65f38f15 17895 {
a680de9a 17896 relocation ^= relocation & mask;
25f53a85 17897 info->callbacks->einfo
695344c0 17898 /* xgettext:c-format */
174d0a74 17899 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17900 input_bfd, input_section, rel->r_offset,
d830549d 17901 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17902 mask + 1);
65f38f15 17903 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17904 ret = false;
c316a17c 17905 goto copy_reloc;
65f38f15
AM
17906 }
17907 break;
5bd4f169
AM
17908 }
17909
239e1f3a
AM
17910 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17911 because such sections are not SEC_ALLOC and thus ld.so will
17912 not process them. */
d830549d 17913 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17914 if (unresolved_reloc
239e1f3a 17915 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17916 && h->elf.def_dynamic)
17917 && _bfd_elf_section_offset (output_bfd, info, input_section,
17918 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17919 {
25f53a85 17920 info->callbacks->einfo
695344c0 17921 /* xgettext:c-format */
c1c8c1ef 17922 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17923 input_bfd, input_section, rel->r_offset,
b80eed39 17924 howto->name,
039b3fef 17925 h->elf.root.root.string);
0a1b45a2 17926 ret = false;
9c07fe7c 17927 }
5bd4f169 17928
b80eed39
AM
17929 /* 16-bit fields in insns mostly have signed values, but a
17930 few insns have 16-bit unsigned values. Really, we should
17931 have different reloc types. */
17932 if (howto->complain_on_overflow != complain_overflow_dont
17933 && howto->dst_mask == 0xffff
733ae98c
AM
17934 && (input_section->flags & SEC_CODE) != 0
17935 && offset_in_range (input_section, rel->r_offset & ~3, 4))
b80eed39
AM
17936 {
17937 enum complain_overflow complain = complain_overflow_signed;
17938
17939 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17940 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17941 complain = complain_overflow_bitfield;
17942 else if (howto->rightshift == 0
2365f8d7
AM
17943 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17944 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17945 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17946 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17947 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17948 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17949 complain = complain_overflow_unsigned;
17950 if (howto->complain_on_overflow != complain)
17951 {
17952 alt_howto = *howto;
17953 alt_howto.complain_on_overflow = complain;
17954 howto = &alt_howto;
17955 }
17956 }
17957
5663e321 17958 switch (r_type)
a680de9a 17959 {
5663e321
AM
17960 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17961 case R_PPC64_D34:
17962 case R_PPC64_D34_LO:
17963 case R_PPC64_D34_HI30:
17964 case R_PPC64_D34_HA30:
17965 case R_PPC64_PCREL34:
17966 case R_PPC64_GOT_PCREL34:
c213164a
AM
17967 case R_PPC64_TPREL34:
17968 case R_PPC64_DTPREL34:
87c69f97
AM
17969 case R_PPC64_GOT_TLSGD_PCREL34:
17970 case R_PPC64_GOT_TLSLD_PCREL34:
17971 case R_PPC64_GOT_TPREL_PCREL34:
17972 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17973 case R_PPC64_PLT_PCREL34:
17974 case R_PPC64_PLT_PCREL34_NOTOC:
17975 case R_PPC64_D28:
17976 case R_PPC64_PCREL28:
733ae98c 17977 if (!offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17978 r = bfd_reloc_outofrange;
17979 else
17980 {
5663e321
AM
17981 relocation += addend;
17982 if (howto->pc_relative)
17983 relocation -= (rel->r_offset
17984 + input_section->output_offset
17985 + input_section->output_section->vma);
17986 relocation >>= howto->rightshift;
17987
17988 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17989 pinsn <<= 32;
17990 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17991
17992 pinsn &= ~howto->dst_mask;
17993 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17994 & howto->dst_mask);
17995 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17996 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17997 r = bfd_reloc_ok;
17998 if (howto->complain_on_overflow == complain_overflow_signed
17999 && (relocation + (1ULL << (howto->bitsize - 1))
18000 >= 1ULL << howto->bitsize))
18001 r = bfd_reloc_overflow;
18002 }
18003 break;
18004
18005 case R_PPC64_REL16DX_HA:
733ae98c 18006 if (!offset_in_range (input_section, rel->r_offset, 4))
a680de9a
PB
18007 r = bfd_reloc_outofrange;
18008 else
18009 {
18010 relocation += addend;
18011 relocation -= (rel->r_offset
18012 + input_section->output_offset
18013 + input_section->output_section->vma);
3de43e7b 18014 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
18015 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
18016 insn &= ~0x1fffc1;
3de43e7b 18017 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
18018 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
18019 r = bfd_reloc_ok;
3de43e7b 18020 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
18021 r = bfd_reloc_overflow;
18022 }
5663e321
AM
18023 break;
18024
18025 default:
18026 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
18027 contents, rel->r_offset,
18028 relocation, addend);
a680de9a 18029 }
5bd4f169 18030
ef60b7ff 18031 if (r != bfd_reloc_ok)
5bd4f169 18032 {
bc30df16 18033 char *more_info = NULL;
b80eed39 18034 const char *reloc_name = howto->name;
bc30df16
AM
18035
18036 if (reloc_dest != DEST_NORMAL)
18037 {
18038 more_info = bfd_malloc (strlen (reloc_name) + 8);
18039 if (more_info != NULL)
18040 {
18041 strcpy (more_info, reloc_name);
18042 strcat (more_info, (reloc_dest == DEST_OPD
18043 ? " (OPD)" : " (stub)"));
18044 reloc_name = more_info;
18045 }
18046 }
18047
cd27b276 18048 if (r == bfd_reloc_overflow)
5bd4f169 18049 {
8131c122
AM
18050 /* On code like "if (foo) foo();" don't report overflow
18051 on a branch to zero when foo is undefined. */
18052 if (!warned
18053 && (reloc_dest == DEST_STUB
18054 || !(h != NULL
18055 && (h->elf.root.type == bfd_link_hash_undefweak
18056 || h->elf.root.type == bfd_link_hash_undefined)
18057 && is_branch_reloc (r_type))))
9e51d549
AM
18058 info->callbacks->reloc_overflow
18059 (info, (struct bfd_link_hash_entry *) h, sym_name,
18060 reloc_name, orig_rel.r_addend, input_bfd, input_section,
18061 rel->r_offset);
ef60b7ff
AM
18062 }
18063 else
18064 {
25f53a85 18065 info->callbacks->einfo
695344c0 18066 /* xgettext:c-format */
c1c8c1ef 18067 (_("%H: %s against `%pT': error %d\n"),
25f53a85 18068 input_bfd, input_section, rel->r_offset,
bc30df16 18069 reloc_name, sym_name, (int) r);
0a1b45a2 18070 ret = false;
ef60b7ff 18071 }
c9594989 18072 free (more_info);
5bd4f169 18073 }
c316a17c
AM
18074 copy_reloc:
18075 if (wrel != rel)
18076 *wrel = *rel;
18077 }
18078
18079 if (wrel != rel)
18080 {
18081 Elf_Internal_Shdr *rel_hdr;
18082 size_t deleted = rel - wrel;
18083
18084 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18085 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18086 if (rel_hdr->sh_size == 0)
18087 {
18088 /* It is too late to remove an empty reloc section. Leave
18089 one NONE reloc.
18090 ??? What is wrong with an empty section??? */
18091 rel_hdr->sh_size = rel_hdr->sh_entsize;
18092 deleted -= 1;
18093 }
18094 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18095 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18096 input_section->reloc_count -= deleted;
5bd4f169
AM
18097 }
18098
645ea6a9
AM
18099 /* If we're emitting relocations, then shortly after this function
18100 returns, reloc offsets and addends for this section will be
18101 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
18102 file rather than the input. Save a copy of the relocs for
18103 opd_entry_value. */
6313825c
AM
18104 if (is_opd
18105 && (info->emitrelocations || bfd_link_relocatable (info))
18106 && input_section->reloc_count != 0)
8860955f
AM
18107 {
18108 bfd_size_type amt;
18109 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18110 rel = bfd_alloc (input_bfd, amt);
6313825c 18111 ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
8860955f 18112 if (rel == NULL)
0a1b45a2 18113 return false;
8860955f
AM
18114 memcpy (rel, relocs, amt);
18115 }
5bd4f169
AM
18116 return ret;
18117}
18118
754021d0
AM
18119/* Adjust the value of any local symbols in opd sections. */
18120
6e0b88f1 18121static int
754021d0
AM
18122ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18123 const char *name ATTRIBUTE_UNUSED,
18124 Elf_Internal_Sym *elfsym,
18125 asection *input_sec,
18126 struct elf_link_hash_entry *h)
18127{
74f0fb50
AM
18128 struct _opd_sec_data *opd;
18129 long adjust;
754021d0
AM
18130 bfd_vma value;
18131
4025353c 18132 if (h != NULL)
6e0b88f1 18133 return 1;
4025353c 18134
74f0fb50
AM
18135 opd = get_opd_info (input_sec);
18136 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 18137 return 1;
754021d0
AM
18138
18139 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 18140 if (!bfd_link_relocatable (info))
754021d0
AM
18141 value -= input_sec->output_section->vma;
18142
51aecdc5 18143 adjust = opd->adjust[OPD_NDX (value)];
4025353c 18144 if (adjust == -1)
6e0b88f1
AM
18145 return 2;
18146
18147 elfsym->st_value += adjust;
18148 return 1;
754021d0
AM
18149}
18150
5bd4f169
AM
18151/* Finish up dynamic symbol handling. We set the contents of various
18152 dynamic sections here. */
18153
0a1b45a2 18154static bool
4ce794b7
AM
18155ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18156 struct bfd_link_info *info,
18157 struct elf_link_hash_entry *h,
4aef7643 18158 Elf_Internal_Sym *sym)
5bd4f169 18159{
65f38f15 18160 struct ppc_link_hash_table *htab;
8387904d 18161 struct plt_entry *ent;
5bd4f169 18162
65f38f15 18163 htab = ppc_hash_table (info);
5bd4f169 18164
49c09209
AM
18165 if (!htab->opd_abi && !h->def_regular)
18166 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18167 if (ent->plt.offset != (bfd_vma) -1)
18168 {
18169 /* Mark the symbol as undefined, rather than as
18170 defined in glink. Leave the value if there were
18171 any relocations where pointer equality matters
18172 (this is a clue for the dynamic linker, to make
18173 function pointer comparisons work between an
18174 application and shared library), otherwise set it
18175 to zero. */
18176 sym->st_shndx = SHN_UNDEF;
18177 if (!h->pointer_equality_needed)
18178 sym->st_value = 0;
18179 else if (!h->ref_regular_nonweak)
18180 {
18181 /* This breaks function pointer comparisons, but
18182 that is better than breaking tests for a NULL
18183 function pointer. */
18184 sym->st_value = 0;
18185 }
18186 break;
18187 }
5bd4f169 18188
1bdd8fac
AM
18189 if (h->needs_copy
18190 && (h->root.type == bfd_link_hash_defined
18191 || h->root.type == bfd_link_hash_defweak)
18192 && (h->root.u.def.section == htab->elf.sdynbss
18193 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 18194 {
65f38f15 18195 /* This symbol needs a copy reloc. Set it up. */
49c09209 18196 Elf_Internal_Rela rela;
5474d94f 18197 asection *srel;
5bd4f169 18198
1bdd8fac 18199 if (h->dynindx == -1)
65f38f15 18200 abort ();
5bd4f169 18201
ed7007c1 18202 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
18203 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18204 rela.r_addend = 0;
afbf7e8e 18205 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
18206 srel = htab->elf.sreldynrelro;
18207 else
18208 srel = htab->elf.srelbss;
066c738e 18209 BFD_ASSERT (count_and_swap_reloc_out (output_bfd, &rela, srel));
5bd4f169
AM
18210 }
18211
0a1b45a2 18212 return true;
5bd4f169
AM
18213}
18214
65f38f15
AM
18215/* Used to decide how to sort relocs in an optimal manner for the
18216 dynamic linker, before writing them out. */
18217
18218static enum elf_reloc_type_class
7e612e98
AM
18219ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18220 const asection *rel_sec,
18221 const Elf_Internal_Rela *rela)
65f38f15 18222{
04c9666a 18223 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
18224 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18225
33e44f2e 18226 if (rel_sec == htab->elf.irelplt)
7e612e98 18227 return reloc_class_ifunc;
a33d1f77 18228
4ce794b7 18229 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 18230 switch (r_type)
65f38f15
AM
18231 {
18232 case R_PPC64_RELATIVE:
18233 return reloc_class_relative;
18234 case R_PPC64_JMP_SLOT:
18235 return reloc_class_plt;
18236 case R_PPC64_COPY:
18237 return reloc_class_copy;
18238 default:
18239 return reloc_class_normal;
18240 }
18241}
18242
5bd4f169
AM
18243/* Finish up the dynamic sections. */
18244
0a1b45a2 18245static bool
4ce794b7
AM
18246ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18247 struct bfd_link_info *info)
5bd4f169 18248{
65f38f15
AM
18249 struct ppc_link_hash_table *htab;
18250 bfd *dynobj;
5bd4f169 18251 asection *sdyn;
5bd4f169 18252
65f38f15 18253 htab = ppc_hash_table (info);
4dfe6ac6 18254 if (htab == NULL)
0a1b45a2 18255 return false;
4dfe6ac6 18256
65f38f15 18257 dynobj = htab->elf.dynobj;
3d4d4302 18258 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 18259
65f38f15 18260 if (htab->elf.dynamic_sections_created)
5bd4f169 18261 {
5bd4f169
AM
18262 Elf64_External_Dyn *dyncon, *dynconend;
18263
33e44f2e 18264 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 18265 abort ();
5bd4f169
AM
18266
18267 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 18268 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
18269 for (; dyncon < dynconend; dyncon++)
18270 {
18271 Elf_Internal_Dyn dyn;
19397422 18272 asection *s;
5bd4f169
AM
18273
18274 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18275
18276 switch (dyn.d_tag)
18277 {
65f38f15
AM
18278 default:
18279 continue;
5bd4f169 18280
5d1634d7 18281 case DT_PPC64_GLINK:
4ce794b7 18282 s = htab->glink;
6348e046 18283 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
18284 /* We stupidly defined DT_PPC64_GLINK to be the start
18285 of glink rather than the first entry point, which is
18286 what ld.so needs, and now have a bigger stub to
18287 support automatic multiple TOCs. */
9e390558 18288 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
18289 break;
18290
19397422
AM
18291 case DT_PPC64_OPD:
18292 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18293 if (s == NULL)
18294 continue;
18295 dyn.d_un.d_ptr = s->vma;
19397422
AM
18296 break;
18297
e8910a83 18298 case DT_PPC64_OPT:
5663e321
AM
18299 if ((htab->do_multi_toc && htab->multi_toc_needed)
18300 || htab->notoc_plt)
e8910a83 18301 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
18302 if (htab->has_plt_localentry0)
18303 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
18304 break;
18305
19397422
AM
18306 case DT_PPC64_OPDSZ:
18307 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18308 if (s == NULL)
18309 continue;
eea6121a 18310 dyn.d_un.d_val = s->size;
19397422
AM
18311 break;
18312
65f38f15 18313 case DT_PLTGOT:
33e44f2e 18314 s = htab->elf.splt;
6348e046 18315 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
18316 break;
18317
18318 case DT_JMPREL:
33e44f2e 18319 s = htab->elf.srelplt;
6348e046 18320 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 18321 break;
5bd4f169 18322
65f38f15 18323 case DT_PLTRELSZ:
33e44f2e 18324 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 18325 break;
82e66161
AM
18326
18327 case DT_TEXTREL:
cebd6b8a 18328 if (htab->elf.ifunc_resolvers)
82e66161
AM
18329 info->callbacks->einfo
18330 (_("%P: warning: text relocations and GNU indirect "
18331 "functions may result in a segfault at runtime\n"));
18332 continue;
5bd4f169 18333 }
5bd4f169 18334
65f38f15 18335 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 18336 }
5bd4f169
AM
18337 }
18338
6528b6eb
AM
18339 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18340 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18341 {
18342 /* Fill in the first entry in the global offset table.
18343 We use it to hold the link-time TOCbase. */
18344 bfd_put_64 (output_bfd,
60ee0d4a 18345 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 18346 htab->elf.sgot->contents);
5d1634d7
AM
18347
18348 /* Set .got entry size. */
2cdcc330
AM
18349 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18350 = 8;
5d1634d7
AM
18351 }
18352
6528b6eb
AM
18353 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18354 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18355 {
18356 /* Set .plt entry size. */
33e44f2e 18357 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 18358 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
18359 }
18360
84f5d08e
AM
18361 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18362 brlt ourselves if emitrelocations. */
18363 if (htab->brlt != NULL
18364 && htab->brlt->reloc_count != 0
18365 && !_bfd_elf_link_output_relocs (output_bfd,
18366 htab->brlt,
d4730f92 18367 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
18368 elf_section_data (htab->brlt)->relocs,
18369 NULL))
0a1b45a2 18370 return false;
84f5d08e 18371
176a0d42
AM
18372 if (htab->glink != NULL
18373 && htab->glink->reloc_count != 0
18374 && !_bfd_elf_link_output_relocs (output_bfd,
18375 htab->glink,
d4730f92 18376 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
18377 elf_section_data (htab->glink)->relocs,
18378 NULL))
0a1b45a2 18379 return false;
176a0d42 18380
da44f4e5 18381
df136d64
AM
18382 if (htab->glink_eh_frame != NULL
18383 && htab->glink_eh_frame->size != 0
18384 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18385 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18386 htab->glink_eh_frame,
18387 htab->glink_eh_frame->contents))
0a1b45a2 18388 return false;
58d180e8 18389
e717da7e 18390 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
18391 since we didn't add them to DYNOBJ. We know dynobj is the first
18392 bfd. */
c72f2fb2 18393 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
18394 {
18395 asection *s;
7b53ace3 18396
0c8d6e5c 18397 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
18398 continue;
18399
e717da7e
AM
18400 s = ppc64_elf_tdata (dynobj)->got;
18401 if (s != NULL
eea6121a 18402 && s->size != 0
e717da7e
AM
18403 && s->output_section != bfd_abs_section_ptr
18404 && !bfd_set_section_contents (output_bfd, s->output_section,
18405 s->contents, s->output_offset,
eea6121a 18406 s->size))
0a1b45a2 18407 return false;
e717da7e
AM
18408 s = ppc64_elf_tdata (dynobj)->relgot;
18409 if (s != NULL
eea6121a 18410 && s->size != 0
e717da7e
AM
18411 && s->output_section != bfd_abs_section_ptr
18412 && !bfd_set_section_contents (output_bfd, s->output_section,
18413 s->contents, s->output_offset,
eea6121a 18414 s->size))
0a1b45a2 18415 return false;
e717da7e 18416 }
f6c52c13 18417
0a1b45a2 18418 return true;
5bd4f169
AM
18419}
18420
6313825c
AM
18421static bool
18422ppc64_elf_free_cached_info (bfd *abfd)
18423{
18424 if (abfd->sections)
18425 for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18426 opd != NULL;
18427 opd = bfd_get_next_section_by_name (NULL, opd))
18428 if (opd->reloc_count == 0)
18429 free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18430
09d757a5 18431 return _bfd_elf_free_cached_info (abfd);
6313825c
AM
18432}
18433
5bd4f169 18434#include "elf64-target.h"
7b8e7dad
AM
18435
18436/* FreeBSD support */
18437
18438#undef TARGET_LITTLE_SYM
6afcdeb3 18439#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 18440#undef TARGET_LITTLE_NAME
6afcdeb3 18441#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
18442
18443#undef TARGET_BIG_SYM
6d00b590 18444#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
18445#undef TARGET_BIG_NAME
18446#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18447
18448#undef ELF_OSABI
18449#define ELF_OSABI ELFOSABI_FREEBSD
18450
18451#undef elf64_bed
18452#define elf64_bed elf64_powerpc_fbsd_bed
18453
18454#include "elf64-target.h"