]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Remove bfd ELF_RELROPAGESIZE
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
a2c58332 2 Copyright (C) 1999-2022 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
ad8e1ba5 93#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 94#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
95#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
96#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
97#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
98#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 99#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 100#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 101#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
102
103#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
104#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
105#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 106#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 107#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 108#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 109#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 110#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 111#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 112#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 113#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 114#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 115#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 116#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 117#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
118#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
119#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 120#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
c9405344 121#define elf_backend_always_size_sections ppc64_elf_edit
ad8e1ba5 122#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 123#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 124#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 125#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
126#define elf_backend_relocate_section ppc64_elf_relocate_section
127#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
128#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
129#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 130#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 131#define elf_backend_special_sections ppc64_elf_special_sections
bf577467 132#define elf_backend_section_flags ppc64_elf_section_flags
6911b7dc 133#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 134#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 135#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 136
5bd4f169
AM
137/* The name of the dynamic interpreter. This is put in the .interp
138 section. */
139#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
140
141/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 142#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 143#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
144
145/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 146#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 147
a078d95a
AM
148/* Offsets to some stack save slots. */
149#define STK_LR 16
150#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 151/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
152 CR save slot. Used only by optimised __tls_get_addr call stub,
153 relying on __tls_get_addr_opt not saving CR.. */
154#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
155
5bd4f169 156/* TOC base pointers offset from start of TOC. */
411e1bfb 157#define TOC_BASE_OFF 0x8000
a27e685f
AM
158/* TOC base alignment. */
159#define TOC_BASE_ALIGN 256
411e1bfb
AM
160
161/* Offset of tp and dtp pointers from start of TLS block. */
162#define TP_OFFSET 0x7000
163#define DTP_OFFSET 0x8000
5bd4f169 164
ad8e1ba5
AM
165/* .plt call stub instructions. The normal stub is like this, but
166 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 167 insert an addi to adjust r11. */
a078d95a 168#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
169#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
170#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
171#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
172#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
173#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
174#define BCTR 0x4e800420 /* bctr */
175
07d6d2b8 176#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
177#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
178#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
179#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
180#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
181
182#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
183#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
184#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
185#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
186#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
187#define BNECTR 0x4ca20420 /* bnectr+ */
188#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 189
71a39c98 190#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 191#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 192#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 193
07d6d2b8
AM
194#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
195#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
196#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 197
04bdff6a 198#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 199#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 200#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 201#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
202#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
203#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 204#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 205#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 206#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 207#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 208#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 209#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 210#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
211#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
212#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
213#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
214#define PADDI_R12_PC 0x0610000039800000ULL
215#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 216#define PNOP 0x0700000000000000ULL
a345bc8d 217
3cd7c7d7
AM
218/* __glink_PLTresolve stub instructions. We enter with the index in
219 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
9e390558 220#define GLINK_PLTRESOLVE_SIZE(htab) \
3cd7c7d7 221 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
ee4bf8d2
AM
222 /* 0: */
223 /* .quad plt0-1f */
224 /* __glink: */
225#define MFLR_R12 0x7d8802a6 /* mflr %12 */
226#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
227 /* 1: */
228#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 229 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 230#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
231#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
232 /* ld %12,0(%11) */
233 /* ld %2,8(%11) */
234 /* mtctr %12 */
235 /* ld %11,16(%11) */
ee4bf8d2 236 /* bctr */
3cd7c7d7
AM
237
238#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
239#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
240#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
241#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
242#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
243#define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
244#define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
5d1634d7
AM
245
246/* Pad with this. */
247#define NOP 0x60000000
248
721956f4
AM
249/* Some other nops. */
250#define CROR_151515 0x4def7b82
251#define CROR_313131 0x4ffffb82
252
cedb70c5 253/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
254#define LI_R0_0 0x38000000 /* li %r0,0 */
255#define B_DOT 0x48000000 /* b . */
256
257/* After that, we need two instructions to load the index, followed by
258 a branch. */
259#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 260#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 261
deb0e272
AM
262/* Instructions used by the save and restore reg functions. */
263#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
264#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
265#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
266#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
267#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
268#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
269#define LI_R12_0 0x39800000 /* li %r12,0 */
270#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
271#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
272#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
273#define BLR 0x4e800020 /* blr */
274
41bd81ab
AM
275/* Since .opd is an array of descriptors and each entry will end up
276 with identical R_PPC64_RELATIVE relocs, there is really no need to
277 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 278 relocate .opd without reloc entries. */
41bd81ab
AM
279#ifndef NO_OPD_RELOCS
280#define NO_OPD_RELOCS 0
281#endif
810d4e75 282
a4b6fadd
AM
283#ifndef ARRAY_SIZE
284#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
285#endif
286
810d4e75
AM
287static inline int
288abiversion (bfd *abfd)
289{
290 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
291}
292
293static inline void
294set_abiversion (bfd *abfd, int ver)
295{
296 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
297 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
298}
5bd4f169
AM
299\f
300/* Relocation HOWTO's. */
46807bf4
AM
301/* Like other ELF RELA targets that don't apply multiple
302 field-altering relocations to the same localation, src_mask is
303 always zero and pcrel_offset is the same as pc_relative.
304 PowerPC can always use a zero bitpos, even when the field is not at
305 the LSB. For example, a REL24 could use rightshift=2, bisize=24
306 and bitpos=2 which matches the ABI description, or as we do here,
307 rightshift=0, bitsize=26 and bitpos=0. */
308#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
309 complain, special_func) \
310 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
311 complain_overflow_ ## complain, special_func, \
0a1b45a2 312 #type, false, 0, mask, pc_relative)
46807bf4 313
04c9666a 314static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 315
f3185997
NC
316static reloc_howto_type ppc64_elf_howto_raw[] =
317{
5bd4f169 318 /* This reloc does nothing. */
0a1b45a2 319 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
46807bf4 320 bfd_elf_generic_reloc),
5bd4f169
AM
321
322 /* A standard 32 bit relocation. */
0a1b45a2 323 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 324 bfd_elf_generic_reloc),
5bd4f169
AM
325
326 /* An absolute 26 bit branch; the lower two bits must be zero.
327 FIXME: we don't check that, we just clear them. */
0a1b45a2 328 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
46807bf4 329 bfd_elf_generic_reloc),
5bd4f169
AM
330
331 /* A standard 16 bit relocation. */
0a1b45a2 332 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 333 bfd_elf_generic_reloc),
5bd4f169
AM
334
335 /* A 16 bit relocation without overflow. */
0a1b45a2 336 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 337 bfd_elf_generic_reloc),
5bd4f169
AM
338
339 /* Bits 16-31 of an address. */
0a1b45a2 340 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 341 bfd_elf_generic_reloc),
5bd4f169
AM
342
343 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
344 bits, treated as a signed number, is negative. */
0a1b45a2 345 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 346 ppc64_elf_ha_reloc),
5bd4f169
AM
347
348 /* An absolute 16 bit branch; the lower two bits must be zero.
349 FIXME: we don't check that, we just clear them. */
0a1b45a2 350 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 351 ppc64_elf_branch_reloc),
5bd4f169
AM
352
353 /* An absolute 16 bit branch, for which bit 10 should be set to
354 indicate that the branch is expected to be taken. The lower two
355 bits must be zero. */
0a1b45a2 356 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 357 ppc64_elf_brtaken_reloc),
5bd4f169
AM
358
359 /* An absolute 16 bit branch, for which bit 10 should be set to
360 indicate that the branch is not expected to be taken. The lower
361 two bits must be zero. */
0a1b45a2 362 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 363 ppc64_elf_brtaken_reloc),
5bd4f169
AM
364
365 /* A relative 26 bit branch; the lower two bits must be zero. */
0a1b45a2 366 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 367 ppc64_elf_branch_reloc),
5bd4f169 368
05d0e962 369 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
0a1b45a2 370 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 371 ppc64_elf_branch_reloc),
05d0e962 372
7aba54da
AM
373 /* Another variant, when p10 insns can't be used on stubs. */
374 HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
375 ppc64_elf_branch_reloc),
376
5bd4f169 377 /* A relative 16 bit branch; the lower two bits must be zero. */
0a1b45a2 378 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 379 ppc64_elf_branch_reloc),
5bd4f169
AM
380
381 /* A relative 16 bit branch. Bit 10 should be set to indicate that
382 the branch is expected to be taken. The lower two bits must be
383 zero. */
0a1b45a2 384 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 385 ppc64_elf_brtaken_reloc),
5bd4f169
AM
386
387 /* A relative 16 bit branch. Bit 10 should be set to indicate that
388 the branch is not expected to be taken. The lower two bits must
389 be zero. */
0a1b45a2 390 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 391 ppc64_elf_brtaken_reloc),
5bd4f169
AM
392
393 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
394 symbol. */
0a1b45a2 395 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
46807bf4 396 ppc64_elf_unhandled_reloc),
5bd4f169
AM
397
398 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
399 the symbol. */
0a1b45a2 400 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 401 ppc64_elf_unhandled_reloc),
5bd4f169
AM
402
403 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
404 the symbol. */
0a1b45a2 405 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 406 ppc64_elf_unhandled_reloc),
5bd4f169
AM
407
408 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
409 the symbol. */
0a1b45a2 410 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 411 ppc64_elf_unhandled_reloc),
5bd4f169
AM
412
413 /* This is used only by the dynamic linker. The symbol should exist
414 both in the object being run and in some shared library. The
415 dynamic linker copies the data addressed by the symbol from the
416 shared library into the object, because the object being
417 run has to have the data at some particular address. */
0a1b45a2 418 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
46807bf4 419 ppc64_elf_unhandled_reloc),
5bd4f169
AM
420
421 /* Like R_PPC64_ADDR64, but used when setting global offset table
422 entries. */
0a1b45a2 423 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 424 ppc64_elf_unhandled_reloc),
5bd4f169
AM
425
426 /* Created by the link editor. Marks a procedure linkage table
427 entry for a symbol. */
0a1b45a2 428 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
46807bf4 429 ppc64_elf_unhandled_reloc),
5bd4f169
AM
430
431 /* Used only by the dynamic linker. When the object is run, this
432 doubleword64 is set to the load address of the object, plus the
433 addend. */
0a1b45a2 434 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 435 bfd_elf_generic_reloc),
5bd4f169
AM
436
437 /* Like R_PPC64_ADDR32, but may be unaligned. */
0a1b45a2 438 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 439 bfd_elf_generic_reloc),
5bd4f169
AM
440
441 /* Like R_PPC64_ADDR16, but may be unaligned. */
0a1b45a2 442 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 443 bfd_elf_generic_reloc),
5bd4f169
AM
444
445 /* 32-bit PC relative. */
0a1b45a2 446 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 447 bfd_elf_generic_reloc),
5bd4f169 448
10ed1bba 449 /* 32-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 450 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 451 ppc64_elf_unhandled_reloc),
5bd4f169
AM
452
453 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
454 FIXME: R_PPC64_PLTREL32 not supported. */
0a1b45a2 455 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 456 ppc64_elf_unhandled_reloc),
5bd4f169
AM
457
458 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
459 the symbol. */
0a1b45a2 460 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 461 ppc64_elf_unhandled_reloc),
5bd4f169
AM
462
463 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
464 the symbol. */
0a1b45a2 465 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 466 ppc64_elf_unhandled_reloc),
5bd4f169
AM
467
468 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
469 the symbol. */
0a1b45a2 470 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 471 ppc64_elf_unhandled_reloc),
5bd4f169 472
c061c2d8 473 /* 16-bit section relative relocation. */
0a1b45a2 474 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
46807bf4 475 ppc64_elf_sectoff_reloc),
5bd4f169 476
c061c2d8 477 /* Like R_PPC64_SECTOFF, but no overflow warning. */
0a1b45a2 478 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 479 ppc64_elf_sectoff_reloc),
5bd4f169
AM
480
481 /* 16-bit upper half section relative relocation. */
0a1b45a2 482 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 483 ppc64_elf_sectoff_reloc),
5bd4f169
AM
484
485 /* 16-bit upper half adjusted section relative relocation. */
0a1b45a2 486 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 487 ppc64_elf_sectoff_ha_reloc),
5bd4f169 488
04c9666a 489 /* Like R_PPC64_REL24 without touching the two least significant bits. */
0a1b45a2 490 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
46807bf4 491 bfd_elf_generic_reloc),
5bd4f169
AM
492
493 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
494
495 /* A standard 64-bit relocation. */
0a1b45a2 496 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 497 bfd_elf_generic_reloc),
5bd4f169
AM
498
499 /* The bits 32-47 of an address. */
0a1b45a2 500 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 501 bfd_elf_generic_reloc),
5bd4f169
AM
502
503 /* The bits 32-47 of an address, plus 1 if the contents of the low
504 16 bits, treated as a signed number, is negative. */
0a1b45a2 505 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 506 ppc64_elf_ha_reloc),
5bd4f169
AM
507
508 /* The bits 48-63 of an address. */
0a1b45a2 509 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 510 bfd_elf_generic_reloc),
5bd4f169
AM
511
512 /* The bits 48-63 of an address, plus 1 if the contents of the low
513 16 bits, treated as a signed number, is negative. */
0a1b45a2 514 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 515 ppc64_elf_ha_reloc),
5bd4f169
AM
516
517 /* Like ADDR64, but may be unaligned. */
0a1b45a2 518 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 519 bfd_elf_generic_reloc),
5bd4f169
AM
520
521 /* 64-bit relative relocation. */
0a1b45a2 522 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 523 bfd_elf_generic_reloc),
5bd4f169 524
cedb70c5 525 /* 64-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 526 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 527 ppc64_elf_unhandled_reloc),
5bd4f169
AM
528
529 /* 64-bit PC relative relocation to the symbol's procedure linkage
530 table. */
531 /* FIXME: R_PPC64_PLTREL64 not supported. */
0a1b45a2 532 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 533 ppc64_elf_unhandled_reloc),
5bd4f169
AM
534
535 /* 16 bit TOC-relative relocation. */
5bd4f169 536 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
0a1b45a2 537 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
46807bf4 538 ppc64_elf_toc_reloc),
5bd4f169
AM
539
540 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 541 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
0a1b45a2 542 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 543 ppc64_elf_toc_reloc),
5bd4f169
AM
544
545 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 546 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
0a1b45a2 547 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 548 ppc64_elf_toc_reloc),
5bd4f169
AM
549
550 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
551 contents of the low 16 bits, treated as a signed number, is
552 negative. */
5bd4f169 553 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
0a1b45a2 554 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 555 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
556
557 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 558 /* R_PPC64_TOC 51 doubleword64 .TOC. */
0a1b45a2 559 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 560 ppc64_elf_toc64_reloc),
5bd4f169
AM
561
562 /* Like R_PPC64_GOT16, but also informs the link editor that the
563 value to relocate may (!) refer to a PLT entry which the link
564 editor (a) may replace with the symbol value. If the link editor
565 is unable to fully resolve the symbol, it may (b) create a PLT
566 entry and store the address to the new PLT entry in the GOT.
567 This permits lazy resolution of function symbols at run time.
568 The link editor may also skip all of this and just (c) emit a
569 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
570 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
0a1b45a2 571 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
46807bf4 572 ppc64_elf_unhandled_reloc),
411e1bfb
AM
573
574 /* Like R_PPC64_PLTGOT16, but without overflow. */
575 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 576 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 577 ppc64_elf_unhandled_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
580 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
0a1b45a2 581 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 582 ppc64_elf_unhandled_reloc),
411e1bfb
AM
583
584 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
585 1 if the contents of the low 16 bits, treated as a signed number,
586 is negative. */
587 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
0a1b45a2 588 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 589 ppc64_elf_unhandled_reloc),
411e1bfb
AM
590
591 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
0a1b45a2 592 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 593 bfd_elf_generic_reloc),
411e1bfb
AM
594
595 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
0a1b45a2 596 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 597 bfd_elf_generic_reloc),
411e1bfb
AM
598
599 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
0a1b45a2 600 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 601 ppc64_elf_unhandled_reloc),
411e1bfb
AM
602
603 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
0a1b45a2 604 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 605 ppc64_elf_unhandled_reloc),
411e1bfb
AM
606
607 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
0a1b45a2 608 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 609 ppc64_elf_unhandled_reloc),
411e1bfb
AM
610
611 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
0a1b45a2 612 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 613 ppc64_elf_sectoff_reloc),
411e1bfb
AM
614
615 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
0a1b45a2 616 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 617 ppc64_elf_sectoff_reloc),
411e1bfb
AM
618
619 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
0a1b45a2 620 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 621 ppc64_elf_toc_reloc),
411e1bfb
AM
622
623 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
0a1b45a2 624 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 625 ppc64_elf_toc_reloc),
411e1bfb
AM
626
627 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
628 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
0a1b45a2 629 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 630 ppc64_elf_unhandled_reloc),
411e1bfb
AM
631
632 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
633 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 634 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 635 ppc64_elf_unhandled_reloc),
411e1bfb 636
727fc41e 637 /* Marker relocs for TLS. */
0a1b45a2 638 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
46807bf4
AM
639 bfd_elf_generic_reloc),
640
0a1b45a2 641 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
46807bf4
AM
642 bfd_elf_generic_reloc),
643
0a1b45a2 644 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
46807bf4 645 bfd_elf_generic_reloc),
727fc41e 646
23cedd1d
AM
647 /* Marker reloc for optimizing r2 save in prologue rather than on
648 each plt call stub. */
0a1b45a2 649 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
46807bf4 650 bfd_elf_generic_reloc),
3b421ab3 651
23cedd1d 652 /* Marker relocs on inline plt call instructions. */
0a1b45a2 653 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
46807bf4
AM
654 bfd_elf_generic_reloc),
655
0a1b45a2 656 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
46807bf4 657 bfd_elf_generic_reloc),
23cedd1d 658
411e1bfb
AM
659 /* Computes the load module index of the load module that contains the
660 definition of its TLS sym. */
0a1b45a2 661 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 662 ppc64_elf_unhandled_reloc),
411e1bfb
AM
663
664 /* Computes a dtv-relative displacement, the difference between the value
665 of sym+add and the base address of the thread-local storage block that
666 contains the definition of sym, minus 0x8000. */
0a1b45a2 667 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 668 ppc64_elf_unhandled_reloc),
411e1bfb
AM
669
670 /* A 16 bit dtprel reloc. */
0a1b45a2 671 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 672 ppc64_elf_unhandled_reloc),
411e1bfb
AM
673
674 /* Like DTPREL16, but no overflow. */
0a1b45a2 675 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 676 ppc64_elf_unhandled_reloc),
411e1bfb
AM
677
678 /* Like DTPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 679 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 680 ppc64_elf_unhandled_reloc),
411e1bfb
AM
681
682 /* Like DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 683 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 684 ppc64_elf_unhandled_reloc),
411e1bfb
AM
685
686 /* Like DTPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 687 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 688 ppc64_elf_unhandled_reloc),
411e1bfb
AM
689
690 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 691 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 692 ppc64_elf_unhandled_reloc),
411e1bfb
AM
693
694 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 695 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 696 ppc64_elf_unhandled_reloc),
411e1bfb
AM
697
698 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 699 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 700 ppc64_elf_unhandled_reloc),
411e1bfb
AM
701
702 /* Like DTPREL16, but for insns with a DS field. */
0a1b45a2 703 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 704 ppc64_elf_unhandled_reloc),
411e1bfb
AM
705
706 /* Like DTPREL16_DS, but no overflow. */
0a1b45a2 707 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 708 ppc64_elf_unhandled_reloc),
411e1bfb
AM
709
710 /* Computes a tp-relative displacement, the difference between the value of
711 sym+add and the value of the thread pointer (r13). */
0a1b45a2 712 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* A 16 bit tprel reloc. */
0a1b45a2 716 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 717 ppc64_elf_unhandled_reloc),
411e1bfb
AM
718
719 /* Like TPREL16, but no overflow. */
0a1b45a2 720 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 721 ppc64_elf_unhandled_reloc),
411e1bfb
AM
722
723 /* Like TPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 724 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 725 ppc64_elf_unhandled_reloc),
411e1bfb
AM
726
727 /* Like TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 728 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 729 ppc64_elf_unhandled_reloc),
411e1bfb
AM
730
731 /* Like TPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 732 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 733 ppc64_elf_unhandled_reloc),
411e1bfb
AM
734
735 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 736 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 737 ppc64_elf_unhandled_reloc),
411e1bfb
AM
738
739 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 740 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 741 ppc64_elf_unhandled_reloc),
411e1bfb
AM
742
743 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 744 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 745 ppc64_elf_unhandled_reloc),
411e1bfb
AM
746
747 /* Like TPREL16, but for insns with a DS field. */
0a1b45a2 748 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 749 ppc64_elf_unhandled_reloc),
411e1bfb
AM
750
751 /* Like TPREL16_DS, but no overflow. */
0a1b45a2 752 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 753 ppc64_elf_unhandled_reloc),
411e1bfb
AM
754
755 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
756 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
757 to the first entry relative to the TOC base (r2). */
0a1b45a2 758 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 759 ppc64_elf_unhandled_reloc),
5bd4f169 760
411e1bfb 761 /* Like GOT_TLSGD16, but no overflow. */
0a1b45a2 762 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 763 ppc64_elf_unhandled_reloc),
5bd4f169 764
411e1bfb 765 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
0a1b45a2 766 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 767 ppc64_elf_unhandled_reloc),
5bd4f169 768
411e1bfb 769 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
0a1b45a2 770 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 771 ppc64_elf_unhandled_reloc),
5bd4f169 772
411e1bfb
AM
773 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
774 with values (sym+add)@dtpmod and zero, and computes the offset to the
775 first entry relative to the TOC base (r2). */
0a1b45a2 776 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 777 ppc64_elf_unhandled_reloc),
5bd4f169 778
411e1bfb 779 /* Like GOT_TLSLD16, but no overflow. */
0a1b45a2 780 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 781 ppc64_elf_unhandled_reloc),
5bd4f169 782
411e1bfb 783 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
0a1b45a2 784 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 785 ppc64_elf_unhandled_reloc),
5bd4f169 786
411e1bfb 787 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
0a1b45a2 788 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 789 ppc64_elf_unhandled_reloc),
5bd4f169 790
411e1bfb
AM
791 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
792 the offset to the entry relative to the TOC base (r2). */
0a1b45a2 793 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 794 ppc64_elf_unhandled_reloc),
5bd4f169 795
411e1bfb 796 /* Like GOT_DTPREL16_DS, but no overflow. */
0a1b45a2 797 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 798 ppc64_elf_unhandled_reloc),
5bd4f169 799
411e1bfb 800 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 801 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 802 ppc64_elf_unhandled_reloc),
5bd4f169 803
411e1bfb 804 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 805 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 806 ppc64_elf_unhandled_reloc),
411e1bfb
AM
807
808 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
809 offset to the entry relative to the TOC base (r2). */
0a1b45a2 810 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 811 ppc64_elf_unhandled_reloc),
5bd4f169 812
411e1bfb 813 /* Like GOT_TPREL16_DS, but no overflow. */
0a1b45a2 814 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 815 ppc64_elf_unhandled_reloc),
5bd4f169 816
411e1bfb 817 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 818 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 819 ppc64_elf_unhandled_reloc),
5bd4f169 820
411e1bfb 821 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 822 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4
AM
823 ppc64_elf_unhandled_reloc),
824
0a1b45a2 825 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
46807bf4
AM
826 ppc64_elf_unhandled_reloc),
827
0a1b45a2 828 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 829 bfd_elf_generic_reloc),
e054468f 830
25f23106 831 /* A 16 bit relative relocation. */
0a1b45a2 832 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
46807bf4 833 bfd_elf_generic_reloc),
25f23106
AM
834
835 /* A 16 bit relative relocation without overflow. */
0a1b45a2 836 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
46807bf4 837 bfd_elf_generic_reloc),
25f23106
AM
838
839 /* The high order 16 bits of a relative address. */
0a1b45a2 840 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
46807bf4 841 bfd_elf_generic_reloc),
25f23106
AM
842
843 /* The high order 16 bits of a relative address, plus 1 if the contents of
844 the low 16 bits, treated as a signed number, is negative. */
0a1b45a2 845 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
46807bf4 846 ppc64_elf_ha_reloc),
25f23106 847
0a1b45a2 848 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
849 bfd_elf_generic_reloc),
850
0a1b45a2 851 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
852 ppc64_elf_ha_reloc),
853
0a1b45a2 854 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
855 bfd_elf_generic_reloc),
856
0a1b45a2 857 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
858 ppc64_elf_ha_reloc),
859
0a1b45a2 860 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
861 bfd_elf_generic_reloc),
862
0a1b45a2 863 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
864 ppc64_elf_ha_reloc),
865
a680de9a 866 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
0a1b45a2 867 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
46807bf4 868 ppc64_elf_ha_reloc),
a680de9a 869
7ba71655 870 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
0a1b45a2 871 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
46807bf4 872 ppc64_elf_ha_reloc),
7ba71655 873
f9c6b907 874 /* Like R_PPC64_ADDR16_HI, but no overflow. */
0a1b45a2 875 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 876 bfd_elf_generic_reloc),
f9c6b907
AM
877
878 /* Like R_PPC64_ADDR16_HA, but no overflow. */
0a1b45a2 879 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 880 ppc64_elf_ha_reloc),
f9c6b907
AM
881
882 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
0a1b45a2 883 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 884 ppc64_elf_unhandled_reloc),
f9c6b907
AM
885
886 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
0a1b45a2 887 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 888 ppc64_elf_unhandled_reloc),
f9c6b907
AM
889
890 /* Like R_PPC64_TPREL16_HI, but no overflow. */
0a1b45a2 891 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 892 ppc64_elf_unhandled_reloc),
f9c6b907
AM
893
894 /* Like R_PPC64_TPREL16_HA, but no overflow. */
0a1b45a2 895 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 896 ppc64_elf_unhandled_reloc),
f9c6b907 897
006589cf 898 /* Marker reloc on ELFv2 large-model function entry. */
0a1b45a2 899 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
46807bf4 900 bfd_elf_generic_reloc),
006589cf 901
45965137 902 /* Like ADDR64, but use local entry point of function. */
0a1b45a2 903 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 904 bfd_elf_generic_reloc),
45965137 905
0a1b45a2 906 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
907 bfd_elf_generic_reloc),
908
0a1b45a2 909 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
910 bfd_elf_generic_reloc),
911
0a1b45a2 912 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
5663e321
AM
913 bfd_elf_generic_reloc),
914
0a1b45a2 915 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
5663e321
AM
916 ppc64_elf_prefix_reloc),
917
0a1b45a2 918 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
5663e321
AM
919 ppc64_elf_prefix_reloc),
920
0a1b45a2 921 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
922 ppc64_elf_prefix_reloc),
923
0a1b45a2 924 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
925 ppc64_elf_prefix_reloc),
926
0a1b45a2 927 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
928 ppc64_elf_prefix_reloc),
929
0a1b45a2 930 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
931 ppc64_elf_unhandled_reloc),
932
0a1b45a2 933 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
934 ppc64_elf_unhandled_reloc),
935
0a1b45a2 936 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
937 ppc64_elf_unhandled_reloc),
938
0a1b45a2 939 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
940 ppc64_elf_unhandled_reloc),
941
0a1b45a2 942 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
943 ppc64_elf_unhandled_reloc),
944
0a1b45a2 945 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
946 ppc64_elf_unhandled_reloc),
947
0a1b45a2 948 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
949 ppc64_elf_unhandled_reloc),
950
0a1b45a2 951 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
952 ppc64_elf_unhandled_reloc),
953
0a1b45a2 954 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
955 ppc64_elf_unhandled_reloc),
956
0a1b45a2 957 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
958 bfd_elf_generic_reloc),
959
0a1b45a2 960 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
961 ppc64_elf_ha_reloc),
962
0a1b45a2 963 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
964 bfd_elf_generic_reloc),
965
0a1b45a2 966 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
967 ppc64_elf_ha_reloc),
968
0a1b45a2 969 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
970 bfd_elf_generic_reloc),
971
0a1b45a2 972 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
973 ppc64_elf_ha_reloc),
974
0a1b45a2 975 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
976 bfd_elf_generic_reloc),
977
0a1b45a2 978 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
979 ppc64_elf_ha_reloc),
980
0a1b45a2 981 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
5663e321
AM
982 ppc64_elf_prefix_reloc),
983
0a1b45a2 984 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
5663e321
AM
985 ppc64_elf_prefix_reloc),
986
5bd4f169 987 /* GNU extension to record C++ vtable hierarchy. */
0a1b45a2 988 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
46807bf4 989 NULL),
5bd4f169
AM
990
991 /* GNU extension to record C++ vtable member usage. */
0a1b45a2 992 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
46807bf4 993 NULL),
5bd4f169
AM
994};
995
996\f
997/* Initialize the ppc64_elf_howto_table, so that linear accesses can
998 be done. */
999
1000static void
4ce794b7 1001ppc_howto_init (void)
5bd4f169
AM
1002{
1003 unsigned int i, type;
1004
a4b6fadd 1005 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
1006 {
1007 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 1008 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
1009 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1010 }
1011}
1012
1013static reloc_howto_type *
87c69f97 1014ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
5bd4f169 1015{
411e1bfb 1016 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1017
1018 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1019 /* Initialize howto table if needed. */
1020 ppc_howto_init ();
1021
4ce794b7 1022 switch (code)
5bd4f169
AM
1023 {
1024 default:
f3185997 1025 /* xgettext:c-format */
2cdcc330
AM
1026 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1027 (int) code);
f3185997 1028 bfd_set_error (bfd_error_bad_value);
4ce794b7 1029 return NULL;
5bd4f169 1030
411e1bfb
AM
1031 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1032 break;
1033 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1034 break;
1035 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1036 break;
1037 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1038 break;
1039 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1040 break;
1041 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1042 break;
f9c6b907
AM
1043 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1044 break;
411e1bfb 1045 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1046 break;
f9c6b907
AM
1047 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1048 break;
411e1bfb 1049 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1050 break;
411e1bfb 1051 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1052 break;
411e1bfb 1053 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1054 break;
411e1bfb 1055 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1056 break;
05d0e962
AM
1057 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1058 break;
7aba54da
AM
1059 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1060 break;
411e1bfb 1061 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1062 break;
411e1bfb 1063 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1064 break;
411e1bfb 1065 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1066 break;
411e1bfb 1067 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1068 break;
411e1bfb 1069 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1070 break;
411e1bfb 1071 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1072 break;
411e1bfb 1073 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1074 break;
411e1bfb 1075 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1076 break;
411e1bfb 1077 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1078 break;
411e1bfb 1079 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1080 break;
411e1bfb 1081 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1082 break;
411e1bfb 1083 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1084 break;
411e1bfb 1085 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1086 break;
411e1bfb 1087 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1088 break;
411e1bfb 1089 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1090 break;
411e1bfb 1091 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1092 break;
411e1bfb 1093 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1094 break;
411e1bfb 1095 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1096 break;
411e1bfb 1097 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1098 break;
411e1bfb 1099 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1100 break;
411e1bfb 1101 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1102 break;
411e1bfb 1103 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1104 break;
411e1bfb 1105 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1106 break;
411e1bfb 1107 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1108 break;
411e1bfb 1109 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1110 break;
411e1bfb 1111 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1112 break;
411e1bfb 1113 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1114 break;
411e1bfb 1115 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1116 break;
411e1bfb 1117 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1118 break;
411e1bfb 1119 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1120 break;
411e1bfb 1121 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1122 break;
411e1bfb 1123 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1124 break;
411e1bfb 1125 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1126 break;
411e1bfb 1127 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1128 break;
411e1bfb 1129 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1130 break;
411e1bfb 1131 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1132 break;
411e1bfb 1133 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1134 break;
411e1bfb 1135 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1136 break;
411e1bfb 1137 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1138 break;
411e1bfb 1139 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1140 break;
411e1bfb 1141 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1142 break;
411e1bfb 1143 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1144 break;
411e1bfb 1145 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1146 break;
411e1bfb 1147 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1148 break;
411e1bfb 1149 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1150 break;
411e1bfb 1151 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1152 break;
411e1bfb 1153 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1154 break;
411e1bfb 1155 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1156 break;
c213164a 1157 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1158 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1159 break;
727fc41e
AM
1160 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1161 break;
1162 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1163 break;
411e1bfb 1164 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1165 break;
411e1bfb 1166 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1167 break;
411e1bfb 1168 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1169 break;
411e1bfb 1170 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1171 break;
f9c6b907
AM
1172 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1173 break;
411e1bfb 1174 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1175 break;
f9c6b907
AM
1176 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1177 break;
411e1bfb 1178 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1179 break;
411e1bfb
AM
1180 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1181 break;
1182 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1183 break;
1184 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1185 break;
f9c6b907
AM
1186 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1187 break;
411e1bfb
AM
1188 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1189 break;
f9c6b907
AM
1190 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1191 break;
411e1bfb
AM
1192 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1193 break;
1194 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1195 break;
1196 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1197 break;
1198 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1199 break;
1200 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1201 break;
1202 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1203 break;
1204 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1205 break;
1206 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1207 break;
1208 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1209 break;
1210 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1211 break;
1212 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1213 break;
1214 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1215 break;
1216 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1217 break;
1218 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1219 break;
1220 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1221 break;
1222 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1223 break;
1224 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1225 break;
1226 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1227 break;
1228 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1229 break;
1230 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1231 break;
1232 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1233 break;
1234 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1235 break;
1236 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1237 break;
1238 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1239 break;
1240 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1241 break;
1242 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1243 break;
1244 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1245 break;
1246 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1247 break;
1248 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1249 break;
25f23106
AM
1250 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1251 break;
1252 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1253 break;
1254 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1255 break;
1256 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1257 break;
4a969973
AM
1258 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1259 break;
1260 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1261 break;
1262 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1263 break;
1264 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1265 break;
1266 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1267 break;
1268 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1269 break;
7ba71655
AM
1270 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1271 break;
a680de9a
PB
1272 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1273 break;
006589cf
AM
1274 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1275 break;
45965137
AM
1276 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1277 break;
5663e321
AM
1278 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1279 break;
1280 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1281 break;
1282 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1283 break;
1284 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1285 break;
1286 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1287 break;
1288 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1289 break;
1290 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1291 break;
c213164a
AM
1292 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1293 break;
1294 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1295 break;
87c69f97 1296 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
c213164a 1297 break;
87c69f97 1298 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
c213164a 1299 break;
87c69f97 1300 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
c213164a 1301 break;
87c69f97 1302 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
c213164a 1303 break;
5663e321
AM
1304 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1305 break;
1306 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1307 break;
1308 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1309 break;
1310 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1311 break;
1312 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1313 break;
1314 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1315 break;
1316 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1317 break;
1318 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1319 break;
1320 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1321 break;
1322 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1323 break;
411e1bfb
AM
1324 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1325 break;
1326 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1327 break;
1328 }
1329
4ce794b7 1330 return ppc64_elf_howto_table[r];
5bd4f169
AM
1331};
1332
157090f7 1333static reloc_howto_type *
87c69f97 1334ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
157090f7
AM
1335{
1336 unsigned int i;
87c69f97
AM
1337 static char *compat_map[][2] = {
1338 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1339 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1340 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1341 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1342 };
157090f7 1343
a4b6fadd 1344 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1345 if (ppc64_elf_howto_raw[i].name != NULL
1346 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1347 return &ppc64_elf_howto_raw[i];
1348
87c69f97
AM
1349 /* Handle old names of relocations in case they were used by
1350 .reloc directives.
1351 FIXME: Remove this soon. Mapping the reloc names is very likely
1352 completely unnecessary. */
1353 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1354 if (strcasecmp (compat_map[i][0], r_name) == 0)
1355 {
1356 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1357 compat_map[i][1], compat_map[i][0]);
1358 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1359 }
1360
157090f7
AM
1361 return NULL;
1362}
1363
5bd4f169
AM
1364/* Set the howto pointer for a PowerPC ELF reloc. */
1365
0a1b45a2 1366static bool
4aef7643 1367ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1368 Elf_Internal_Rela *dst)
5bd4f169 1369{
65f38f15
AM
1370 unsigned int type;
1371
ef60b7ff 1372 /* Initialize howto table if needed. */
5bd4f169 1373 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1374 ppc_howto_init ();
1375
65f38f15 1376 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1377 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1378 {
695344c0 1379 /* xgettext:c-format */
0aa13fee 1380 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1381 abfd, type);
f3185997 1382 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1383 return false;
d0fb9a8d 1384 }
65f38f15 1385 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1386 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1387 {
1388 /* xgettext:c-format */
1389 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1390 abfd, type);
1391 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1392 return false;
f3185997 1393 }
2cdcc330 1394
0a1b45a2 1395 return true;
5bd4f169
AM
1396}
1397
04c9666a 1398/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1399
1400static bfd_reloc_status_type
4ce794b7
AM
1401ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1402 void *data, asection *input_section,
1403 bfd *output_bfd, char **error_message)
5bd4f169 1404{
a680de9a
PB
1405 enum elf_ppc64_reloc_type r_type;
1406 long insn;
1407 bfd_size_type octets;
3de43e7b 1408 bfd_vma value;
a680de9a 1409
805fc799
AM
1410 /* If this is a relocatable link (output_bfd test tells us), just
1411 call the generic function. Any adjustment will be done at final
1412 link time. */
1413 if (output_bfd != NULL)
cedb70c5 1414 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1415 input_section, output_bfd, error_message);
1416
5663e321
AM
1417 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1418 We won't actually be using the low bits, so trashing them
805fc799 1419 doesn't matter. */
a680de9a 1420 r_type = reloc_entry->howto->type;
5663e321
AM
1421 if (r_type == R_PPC64_ADDR16_HIGHERA34
1422 || r_type == R_PPC64_ADDR16_HIGHESTA34
1423 || r_type == R_PPC64_REL16_HIGHERA34
1424 || r_type == R_PPC64_REL16_HIGHESTA34)
1425 reloc_entry->addend += 1ULL << 33;
1426 else
1427 reloc_entry->addend += 1U << 15;
a680de9a
PB
1428 if (r_type != R_PPC64_REL16DX_HA)
1429 return bfd_reloc_continue;
1430
1431 value = 0;
1432 if (!bfd_is_com_section (symbol->section))
1433 value = symbol->value;
1434 value += (reloc_entry->addend
1435 + symbol->section->output_offset
1436 + symbol->section->output_section->vma);
1437 value -= (reloc_entry->address
1438 + input_section->output_offset
1439 + input_section->output_section->vma);
3de43e7b 1440 value = (bfd_signed_vma) value >> 16;
a680de9a 1441
bb294208 1442 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1443 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1444 input_section, octets))
1445 return bfd_reloc_outofrange;
1446
a680de9a
PB
1447 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1448 insn &= ~0x1fffc1;
3de43e7b 1449 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1450 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1451 if (value + 0x8000 > 0xffff)
a680de9a
PB
1452 return bfd_reloc_overflow;
1453 return bfd_reloc_ok;
805fc799 1454}
5bd4f169 1455
2441e016
AM
1456static bfd_reloc_status_type
1457ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1458 void *data, asection *input_section,
1459 bfd *output_bfd, char **error_message)
1460{
1461 if (output_bfd != NULL)
1462 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1463 input_section, output_bfd, error_message);
1464
699733f6
AM
1465 if (strcmp (symbol->section->name, ".opd") == 0
1466 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1467 {
1468 bfd_vma dest = opd_entry_value (symbol->section,
1469 symbol->value + reloc_entry->addend,
0a1b45a2 1470 NULL, NULL, false);
2441e016
AM
1471 if (dest != (bfd_vma) -1)
1472 reloc_entry->addend = dest - (symbol->value
1473 + symbol->section->output_section->vma
1474 + symbol->section->output_offset);
1475 }
810d4e75
AM
1476 else
1477 {
1478 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1479
1480 if (symbol->section->owner != abfd
9f284bf9 1481 && symbol->section->owner != NULL
810d4e75
AM
1482 && abiversion (symbol->section->owner) >= 2)
1483 {
1484 unsigned int i;
1485
1486 for (i = 0; i < symbol->section->owner->symcount; ++i)
1487 {
1488 asymbol *symdef = symbol->section->owner->outsymbols[i];
1489
1490 if (strcmp (symdef->name, symbol->name) == 0)
1491 {
1492 elfsym = (elf_symbol_type *) symdef;
1493 break;
1494 }
1495 }
1496 }
1497 reloc_entry->addend
1498 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1499 }
2441e016
AM
1500 return bfd_reloc_continue;
1501}
1502
805fc799 1503static bfd_reloc_status_type
4ce794b7
AM
1504ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1505 void *data, asection *input_section,
1506 bfd *output_bfd, char **error_message)
805fc799
AM
1507{
1508 long insn;
04c9666a 1509 enum elf_ppc64_reloc_type r_type;
805fc799 1510 bfd_size_type octets;
794e51c0 1511 /* Assume 'at' branch hints. */
0a1b45a2 1512 bool is_isa_v2 = true;
805fc799
AM
1513
1514 /* If this is a relocatable link (output_bfd test tells us), just
1515 call the generic function. Any adjustment will be done at final
1516 link time. */
5bd4f169 1517 if (output_bfd != NULL)
cedb70c5 1518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1519 input_section, output_bfd, error_message);
1520
bb294208 1521 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1522 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1523 input_section, octets))
1524 return bfd_reloc_outofrange;
1525
805fc799
AM
1526 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1527 insn &= ~(0x01 << 21);
4ce794b7 1528 r_type = reloc_entry->howto->type;
805fc799
AM
1529 if (r_type == R_PPC64_ADDR14_BRTAKEN
1530 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1531 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1532
794e51c0 1533 if (is_isa_v2)
5bd4f169 1534 {
805fc799
AM
1535 /* Set 'a' bit. This is 0b00010 in BO field for branch
1536 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1537 for branch on CTR insns (BO == 1a00t or 1a01t). */
1538 if ((insn & (0x14 << 21)) == (0x04 << 21))
1539 insn |= 0x02 << 21;
1540 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1541 insn |= 0x08 << 21;
1542 else
2441e016 1543 goto out;
5bd4f169 1544 }
805fc799
AM
1545 else
1546 {
1547 bfd_vma target = 0;
1548 bfd_vma from;
5bd4f169 1549
805fc799
AM
1550 if (!bfd_is_com_section (symbol->section))
1551 target = symbol->value;
1552 target += symbol->section->output_section->vma;
1553 target += symbol->section->output_offset;
1554 target += reloc_entry->addend;
5bd4f169 1555
805fc799
AM
1556 from = (reloc_entry->address
1557 + input_section->output_offset
1558 + input_section->output_section->vma);
5bd4f169 1559
805fc799
AM
1560 /* Invert 'y' bit if not the default. */
1561 if ((bfd_signed_vma) (target - from) < 0)
1562 insn ^= 0x01 << 21;
1563 }
4ce794b7 1564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1565 out:
1566 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1567 input_section, output_bfd, error_message);
805fc799 1568}
5bd4f169 1569
805fc799 1570static bfd_reloc_status_type
4ce794b7
AM
1571ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1572 void *data, asection *input_section,
1573 bfd *output_bfd, char **error_message)
805fc799
AM
1574{
1575 /* If this is a relocatable link (output_bfd test tells us), just
1576 call the generic function. Any adjustment will be done at final
1577 link time. */
1578 if (output_bfd != NULL)
cedb70c5 1579 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1580 input_section, output_bfd, error_message);
5bd4f169 1581
805fc799
AM
1582 /* Subtract the symbol section base address. */
1583 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1584 return bfd_reloc_continue;
1585}
1586
805fc799 1587static bfd_reloc_status_type
4ce794b7
AM
1588ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1589 void *data, asection *input_section,
1590 bfd *output_bfd, char **error_message)
805fc799
AM
1591{
1592 /* If this is a relocatable link (output_bfd test tells us), just
1593 call the generic function. Any adjustment will be done at final
1594 link time. */
1595 if (output_bfd != NULL)
cedb70c5 1596 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1597 input_section, output_bfd, error_message);
1598
1599 /* Subtract the symbol section base address. */
1600 reloc_entry->addend -= symbol->section->output_section->vma;
1601
1602 /* Adjust the addend for sign extension of the low 16 bits. */
1603 reloc_entry->addend += 0x8000;
1604 return bfd_reloc_continue;
1605}
1606
1607static bfd_reloc_status_type
4ce794b7
AM
1608ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609 void *data, asection *input_section,
1610 bfd *output_bfd, char **error_message)
805fc799
AM
1611{
1612 bfd_vma TOCstart;
1613
1614 /* If this is a relocatable link (output_bfd test tells us), just
1615 call the generic function. Any adjustment will be done at final
1616 link time. */
1617 if (output_bfd != NULL)
cedb70c5 1618 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1619 input_section, output_bfd, error_message);
1620
1621 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1622 if (TOCstart == 0)
1c865ab2 1623 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1624
1625 /* Subtract the TOC base address. */
1626 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1627 return bfd_reloc_continue;
1628}
1629
1630static bfd_reloc_status_type
4ce794b7
AM
1631ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1632 void *data, asection *input_section,
1633 bfd *output_bfd, char **error_message)
805fc799
AM
1634{
1635 bfd_vma TOCstart;
1636
1637 /* If this is a relocatable link (output_bfd test tells us), just
1638 call the generic function. Any adjustment will be done at final
1639 link time. */
1640 if (output_bfd != NULL)
cedb70c5 1641 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1642 input_section, output_bfd, error_message);
1643
1644 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1645 if (TOCstart == 0)
1c865ab2 1646 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1647
1648 /* Subtract the TOC base address. */
1649 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1650
1651 /* Adjust the addend for sign extension of the low 16 bits. */
1652 reloc_entry->addend += 0x8000;
1653 return bfd_reloc_continue;
1654}
1655
1656static bfd_reloc_status_type
4ce794b7
AM
1657ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1658 void *data, asection *input_section,
1659 bfd *output_bfd, char **error_message)
805fc799
AM
1660{
1661 bfd_vma TOCstart;
1662 bfd_size_type octets;
1663
1664 /* If this is a relocatable link (output_bfd test tells us), just
1665 call the generic function. Any adjustment will be done at final
1666 link time. */
1667 if (output_bfd != NULL)
cedb70c5 1668 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1669 input_section, output_bfd, error_message);
1670
b54509b8
AM
1671 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1672 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1673 input_section, octets))
1674 return bfd_reloc_outofrange;
1675
805fc799
AM
1676 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1677 if (TOCstart == 0)
1c865ab2 1678 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799 1679
805fc799
AM
1680 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1681 return bfd_reloc_ok;
1682}
1683
5663e321
AM
1684static bfd_reloc_status_type
1685ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1686 void *data, asection *input_section,
1687 bfd *output_bfd, char **error_message)
1688{
1689 uint64_t insn;
1690 bfd_vma targ;
b54509b8 1691 bfd_size_type octets;
5663e321
AM
1692
1693 if (output_bfd != NULL)
1694 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1695 input_section, output_bfd, error_message);
1696
b54509b8
AM
1697 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1698 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1699 input_section, octets))
1700 return bfd_reloc_outofrange;
1701
1702 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
5663e321 1703 insn <<= 32;
b54509b8 1704 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
5663e321
AM
1705
1706 targ = (symbol->section->output_section->vma
1707 + symbol->section->output_offset
1708 + reloc_entry->addend);
1709 if (!bfd_is_com_section (symbol->section))
1710 targ += symbol->value;
1711 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1712 targ += 1ULL << 33;
1713 if (reloc_entry->howto->pc_relative)
1714 {
1715 bfd_vma from = (reloc_entry->address
1716 + input_section->output_offset
1717 + input_section->output_section->vma);
1718 targ -=from;
1719 }
1720 targ >>= reloc_entry->howto->rightshift;
1721 insn &= ~reloc_entry->howto->dst_mask;
1722 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
b54509b8
AM
1723 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1724 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
5663e321
AM
1725 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1726 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1727 >= 1ULL << reloc_entry->howto->bitsize))
1728 return bfd_reloc_overflow;
1729 return bfd_reloc_ok;
1730}
1731
805fc799 1732static bfd_reloc_status_type
4ce794b7
AM
1733ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1734 void *data, asection *input_section,
1735 bfd *output_bfd, char **error_message)
805fc799
AM
1736{
1737 /* If this is a relocatable link (output_bfd test tells us), just
1738 call the generic function. Any adjustment will be done at final
1739 link time. */
1740 if (output_bfd != NULL)
cedb70c5 1741 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1742 input_section, output_bfd, error_message);
1743
1744 if (error_message != NULL)
1745 {
7fbd5f4e
AM
1746 static char *message;
1747 free (message);
1748 if (asprintf (&message, _("generic linker can't handle %s"),
1749 reloc_entry->howto->name) < 0)
1750 message = NULL;
1751 *error_message = message;
805fc799
AM
1752 }
1753 return bfd_reloc_dangerous;
1754}
1755
927be08e
AM
1756/* Track GOT entries needed for a given symbol. We might need more
1757 than one got entry per symbol. */
1758struct got_entry
1759{
1760 struct got_entry *next;
1761
1762 /* The symbol addend that we'll be placing in the GOT. */
1763 bfd_vma addend;
1764
1765 /* Unlike other ELF targets, we use separate GOT entries for the same
1766 symbol referenced from different input files. This is to support
1767 automatic multiple TOC/GOT sections, where the TOC base can vary
1768 from one input file to another. After partitioning into TOC groups
1769 we merge entries within the group.
1770
1771 Point to the BFD owning this GOT entry. */
1772 bfd *owner;
1773
1774 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1775 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1776 unsigned char tls_type;
927be08e
AM
1777
1778 /* Non-zero if got.ent points to real entry. */
f961d9dd 1779 unsigned char is_indirect;
927be08e
AM
1780
1781 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1782 union
2cdcc330
AM
1783 {
1784 bfd_signed_vma refcount;
1785 bfd_vma offset;
1786 struct got_entry *ent;
1787 } got;
927be08e
AM
1788};
1789
1790/* The same for PLT. */
1791struct plt_entry
1792{
1793 struct plt_entry *next;
1794
1795 bfd_vma addend;
1796
1797 union
2cdcc330
AM
1798 {
1799 bfd_signed_vma refcount;
1800 bfd_vma offset;
1801 } plt;
927be08e
AM
1802};
1803
e717da7e
AM
1804struct ppc64_elf_obj_tdata
1805{
1806 struct elf_obj_tdata elf;
1807
1808 /* Shortcuts to dynamic linker sections. */
1809 asection *got;
1810 asection *relgot;
1811
b3fac117
AM
1812 /* Used during garbage collection. We attach global symbols defined
1813 on removed .opd entries to this section so that the sym is removed. */
1814 asection *deleted_section;
81688140 1815
927be08e 1816 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1817 sections means we potentially need one of these for each input bfd. */
927be08e 1818 struct got_entry tlsld_got;
8860955f 1819
2cdcc330
AM
1820 union
1821 {
729eabd5
AM
1822 /* A copy of relocs before they are modified for --emit-relocs. */
1823 Elf_Internal_Rela *relocs;
1824
1825 /* Section contents. */
1826 bfd_byte *contents;
1827 } opd;
d77c8a4b
AM
1828
1829 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1830 the reloc to be in the range -32768 to 32767. */
98528052
AM
1831 unsigned int has_small_toc_reloc : 1;
1832
560c8763
AM
1833 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1834 instruction not one we handle. */
1835 unsigned int unexpected_toc_insn : 1;
066f4018 1836
903b777d
AM
1837 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1838 this file. */
1839 unsigned int has_optrel : 1;
e717da7e
AM
1840};
1841
1842#define ppc64_elf_tdata(bfd) \
1843 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1844
1845#define ppc64_tlsld_got(bfd) \
1846 (&ppc64_elf_tdata (bfd)->tlsld_got)
1847
0c8d6e5c
AM
1848#define is_ppc64_elf(bfd) \
1849 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1850 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1851
e717da7e
AM
1852/* Override the generic function because we store some extras. */
1853
0a1b45a2 1854static bool
e717da7e
AM
1855ppc64_elf_mkobject (bfd *abfd)
1856{
0ffa91dd 1857 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1858 PPC64_ELF_DATA);
e717da7e
AM
1859}
1860
feee612b 1861/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1862 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1863
0a1b45a2 1864static bool
4ce794b7 1865ppc64_elf_object_p (bfd *abfd)
feee612b 1866{
14b57c7c 1867 if (!abfd->arch_info->the_default)
0a1b45a2 1868 return true;
14b57c7c
AM
1869
1870 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1871 {
1872 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1873
1874 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1875 {
1876 /* Relies on arch after 32 bit default being 64 bit default. */
1877 abfd->arch_info = abfd->arch_info->next;
1878 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1879 }
1880 }
14b57c7c 1881 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1882}
1883
d37c89e5
AM
1884/* Support for core dump NOTE sections. */
1885
0a1b45a2 1886static bool
d37c89e5
AM
1887ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1888{
eea6121a 1889 size_t offset, size;
d37c89e5
AM
1890
1891 if (note->descsz != 504)
0a1b45a2 1892 return false;
d37c89e5
AM
1893
1894 /* pr_cursig */
228e534f 1895 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1896
1897 /* pr_pid */
228e534f 1898 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1899
1900 /* pr_reg */
1901 offset = 112;
eea6121a 1902 size = 384;
d37c89e5
AM
1903
1904 /* Make a ".reg/999" section. */
1905 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1906 size, note->descpos + offset);
d37c89e5
AM
1907}
1908
0a1b45a2 1909static bool
d37c89e5
AM
1910ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1911{
1912 if (note->descsz != 136)
0a1b45a2 1913 return false;
d37c89e5 1914
228e534f 1915 elf_tdata (abfd)->core->pid
bc989cdc 1916 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1917 elf_tdata (abfd)->core->program
d37c89e5 1918 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1919 elf_tdata (abfd)->core->command
d37c89e5
AM
1920 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1921
0a1b45a2 1922 return true;
d37c89e5
AM
1923}
1924
183e98be
AM
1925static char *
1926ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1927 ...)
1928{
1929 switch (note_type)
1930 {
1931 default:
1932 return NULL;
1933
1934 case NT_PRPSINFO:
1935 {
9ef6d1e3 1936 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1937 va_list ap;
1938
1939 va_start (ap, note_type);
75cd47ed 1940 memset (data, 0, sizeof (data));
183e98be 1941 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1942#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1943 DIAGNOSTIC_PUSH;
be3e27bb 1944 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1945 -Wstringop-truncation:
1946 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1947 */
95da9854
L
1948 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1949#endif
183e98be 1950 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1951#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1952 DIAGNOSTIC_POP;
fe75810f 1953#endif
183e98be
AM
1954 va_end (ap);
1955 return elfcore_write_note (abfd, buf, bufsiz,
1956 "CORE", note_type, data, sizeof (data));
1957 }
1958
1959 case NT_PRSTATUS:
1960 {
1961 char data[504];
1962 va_list ap;
1963 long pid;
1964 int cursig;
1965 const void *greg;
1966
1967 va_start (ap, note_type);
1968 memset (data, 0, 112);
1969 pid = va_arg (ap, long);
1970 bfd_put_32 (abfd, pid, data + 32);
1971 cursig = va_arg (ap, int);
1972 bfd_put_16 (abfd, cursig, data + 12);
1973 greg = va_arg (ap, const void *);
1974 memcpy (data + 112, greg, 384);
1975 memset (data + 496, 0, 8);
1976 va_end (ap);
1977 return elfcore_write_note (abfd, buf, bufsiz,
1978 "CORE", note_type, data, sizeof (data));
1979 }
1980 }
1981}
1982
5d35169e
AM
1983/* Add extra PPC sections. */
1984
2cdcc330 1985static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1986{
07d6d2b8
AM
1987 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1988 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1989 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1990 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1991 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1992 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1993 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1994};
1995
7c8fe5c4
AM
1996enum _ppc64_sec_type {
1997 sec_normal = 0,
1998 sec_opd = 1,
1999 sec_toc = 2
2000};
2001
f0abc2a1
AM
2002struct _ppc64_elf_section_data
2003{
2004 struct bfd_elf_section_data elf;
411e1bfb 2005
f0abc2a1
AM
2006 union
2007 {
51aecdc5
AM
2008 /* An array with one entry for each opd function descriptor,
2009 and some spares since opd entries may be either 16 or 24 bytes. */
2010#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
2011 struct _opd_sec_data
2012 {
2013 /* Points to the function code section for local opd entries. */
2014 asection **func_sec;
2015
2016 /* After editing .opd, adjust references to opd local syms. */
2017 long *adjust;
2018 } opd;
7c8fe5c4 2019
3a71aa26
AM
2020 /* An array for toc sections, indexed by offset/8. */
2021 struct _toc_sec_data
2022 {
2023 /* Specifies the relocation symbol index used at a given toc offset. */
2024 unsigned *symndx;
2025
2026 /* And the relocation addend. */
2027 bfd_vma *add;
2028 } toc;
7c8fe5c4
AM
2029 } u;
2030
2031 enum _ppc64_sec_type sec_type:2;
411e1bfb 2032
7c8fe5c4
AM
2033 /* Flag set when small branches are detected. Used to
2034 select suitable defaults for the stub group size. */
2035 unsigned int has_14bit_branch:1;
3e04d765
AM
2036
2037 /* Flag set when PLTCALL relocs are detected. */
2038 unsigned int has_pltcall:1;
066f4018 2039
903b777d
AM
2040 /* Flag set when section has PLT/GOT/TOC relocations that can be
2041 optimised. */
2042 unsigned int has_optrel:1;
f0abc2a1
AM
2043};
2044
2045#define ppc64_elf_section_data(sec) \
411e1bfb 2046 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1 2047
0a1b45a2 2048static bool
4ce794b7 2049ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2050{
f592407e
AM
2051 if (!sec->used_by_bfd)
2052 {
2053 struct _ppc64_elf_section_data *sdata;
986f0783 2054 size_t amt = sizeof (*sdata);
f0abc2a1 2055
f592407e
AM
2056 sdata = bfd_zalloc (abfd, amt);
2057 if (sdata == NULL)
0a1b45a2 2058 return false;
f592407e
AM
2059 sec->used_by_bfd = sdata;
2060 }
f0abc2a1
AM
2061
2062 return _bfd_elf_new_section_hook (abfd, sec);
2063}
4025353c 2064
0a1b45a2 2065static bool
bf577467
AM
2066ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2067{
2068 const char *name = hdr->bfd_section->name;
2069
3f3328b8
ML
2070 if (startswith (name, ".sbss")
2071 || startswith (name, ".sdata"))
bf577467
AM
2072 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2073
0a1b45a2 2074 return true;
bf577467
AM
2075}
2076
74f0fb50 2077static struct _opd_sec_data *
4025353c
AM
2078get_opd_info (asection * sec)
2079{
2080 if (sec != NULL
2081 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2082 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2083 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2084 return NULL;
2085}
90e3cdf2
JJ
2086\f
2087/* Parameters for the qsort hook. */
0a1b45a2 2088static bool synthetic_relocatable;
8cb1c2c8 2089static const asection *synthetic_opd;
90e3cdf2 2090
699733f6 2091/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2092
2093static int
2094compare_symbols (const void *ap, const void *bp)
2095{
2cdcc330
AM
2096 const asymbol *a = *(const asymbol **) ap;
2097 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2098
699733f6
AM
2099 /* Section symbols first. */
2100 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2101 return -1;
699733f6 2102 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2103 return 1;
2104
699733f6 2105 /* then .opd symbols. */
cd285db5
AM
2106 if (synthetic_opd != NULL)
2107 {
2108 if (strcmp (a->section->name, ".opd") == 0
2109 && strcmp (b->section->name, ".opd") != 0)
2110 return -1;
2111 if (strcmp (a->section->name, ".opd") != 0
2112 && strcmp (b->section->name, ".opd") == 0)
2113 return 1;
2114 }
90e3cdf2 2115
699733f6 2116 /* then other code symbols. */
2cdcc330
AM
2117 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2118 == (SEC_CODE | SEC_ALLOC))
2119 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2120 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2121 return -1;
2122
2cdcc330
AM
2123 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2124 != (SEC_CODE | SEC_ALLOC))
2125 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2126 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2127 return 1;
2128
2129 if (synthetic_relocatable)
2130 {
2131 if (a->section->id < b->section->id)
2132 return -1;
2133
2134 if (a->section->id > b->section->id)
2135 return 1;
2136 }
2137
2138 if (a->value + a->section->vma < b->value + b->section->vma)
2139 return -1;
2140
2141 if (a->value + a->section->vma > b->value + b->section->vma)
2142 return 1;
2143
4d35a0aa
AM
2144 /* For syms with the same value, prefer strong dynamic global function
2145 syms over other syms. */
2146 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2147 return -1;
2148
2149 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2150 return 1;
2151
2152 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2153 return -1;
2154
2155 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2156 return 1;
2157
2158 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2159 return -1;
2160
2161 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2162 return 1;
2163
2164 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2165 return -1;
2166
2167 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2168 return 1;
2169
dcea6a95
AM
2170 /* Finally, sort on where the symbol is in memory. The symbols will
2171 be in at most two malloc'd blocks, one for static syms, one for
2172 dynamic syms, and we distinguish the two blocks above by testing
2173 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2174 originally in the same order as the symbols (and we're not
2175 sorting the symbols themselves), this ensures a stable sort. */
2176 if (a < b)
2177 return -1;
2178 if (a > b)
2179 return 1;
2180 return 0;
90e3cdf2
JJ
2181}
2182
699733f6 2183/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2184
699733f6 2185static asymbol *
9ad9b810
AM
2186sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2187 bfd_vma value)
90e3cdf2 2188{
9ad9b810 2189 size_t mid;
90e3cdf2 2190
7292b3ac 2191 if (id == (unsigned) -1)
699733f6
AM
2192 {
2193 while (lo < hi)
2194 {
2195 mid = (lo + hi) >> 1;
2196 if (syms[mid]->value + syms[mid]->section->vma < value)
2197 lo = mid + 1;
2198 else if (syms[mid]->value + syms[mid]->section->vma > value)
2199 hi = mid;
2200 else
2201 return syms[mid];
2202 }
2203 }
2204 else
2205 {
2206 while (lo < hi)
2207 {
2208 mid = (lo + hi) >> 1;
2209 if (syms[mid]->section->id < id)
2210 lo = mid + 1;
2211 else if (syms[mid]->section->id > id)
2212 hi = mid;
2213 else if (syms[mid]->value < value)
2214 lo = mid + 1;
2215 else if (syms[mid]->value > value)
2216 hi = mid;
2217 else
2218 return syms[mid];
2219 }
2220 }
2221 return NULL;
90e3cdf2
JJ
2222}
2223
0a1b45a2 2224static bool
468392fb
AM
2225section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2226{
2227 bfd_vma vma = *(bfd_vma *) ptr;
2228 return ((section->flags & SEC_ALLOC) != 0
2229 && section->vma <= vma
2230 && vma < section->vma + section->size);
2231}
2232
699733f6 2233/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2234 entry syms. Also generate @plt symbols for the glink branch table.
2235 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2236
2237static long
a7535cf3
AM
2238ppc64_elf_get_synthetic_symtab (bfd *abfd,
2239 long static_count, asymbol **static_syms,
2240 long dyn_count, asymbol **dyn_syms,
c9727e01 2241 asymbol **ret)
90e3cdf2
JJ
2242{
2243 asymbol *s;
0ccf57bd 2244 size_t i, j, count;
90e3cdf2 2245 char *names;
0ccf57bd 2246 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2247 asection *opd = NULL;
0a1b45a2 2248 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2249 asymbol **syms;
ee67d69a 2250 int abi = abiversion (abfd);
90e3cdf2
JJ
2251
2252 *ret = NULL;
2253
ee67d69a
AM
2254 if (abi < 2)
2255 {
2256 opd = bfd_get_section_by_name (abfd, ".opd");
2257 if (opd == NULL && abi == 1)
2258 return 0;
2259 }
90e3cdf2 2260
a5259595
AM
2261 syms = NULL;
2262 codesecsym = 0;
2263 codesecsymend = 0;
2264 secsymend = 0;
2265 opdsymend = 0;
2266 symcount = 0;
2267 if (opd != NULL)
c9727e01 2268 {
a5259595
AM
2269 symcount = static_count;
2270 if (!relocatable)
2271 symcount += dyn_count;
2272 if (symcount == 0)
2273 return 0;
c9727e01 2274
a5259595
AM
2275 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2276 if (syms == NULL)
2277 return -1;
90e3cdf2 2278
a5259595
AM
2279 if (!relocatable && static_count != 0 && dyn_count != 0)
2280 {
2281 /* Use both symbol tables. */
2282 memcpy (syms, static_syms, static_count * sizeof (*syms));
2283 memcpy (syms + static_count, dyn_syms,
2284 (dyn_count + 1) * sizeof (*syms));
2285 }
2286 else if (!relocatable && static_count == 0)
2287 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2288 else
2289 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2290
0ccf57bd
AM
2291 /* Trim uninteresting symbols. Interesting symbols are section,
2292 function, and notype symbols. */
2293 for (i = 0, j = 0; i < symcount; ++i)
2294 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2295 | BSF_RELC | BSF_SRELC)) == 0)
2296 syms[j++] = syms[i];
2297 symcount = j;
2298
a5259595
AM
2299 synthetic_relocatable = relocatable;
2300 synthetic_opd = opd;
2301 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2302
a5259595
AM
2303 if (!relocatable && symcount > 1)
2304 {
bfa5bd2a
PA
2305 /* Trim duplicate syms, since we may have merged the normal
2306 and dynamic symbols. Actually, we only care about syms
2307 that have different values, so trim any with the same
2308 value. Don't consider ifunc and ifunc resolver symbols
2309 duplicates however, because GDB wants to know whether a
2310 text symbol is an ifunc resolver. */
a5259595 2311 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2312 {
2313 const asymbol *s0 = syms[i - 1];
2314 const asymbol *s1 = syms[i];
2315
2316 if ((s0->value + s0->section->vma
2317 != s1->value + s1->section->vma)
2318 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2319 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2320 syms[j++] = syms[i];
2321 }
a5259595
AM
2322 symcount = j;
2323 }
699733f6 2324
a5259595
AM
2325 i = 0;
2326 /* Note that here and in compare_symbols we can't compare opd and
2327 sym->section directly. With separate debug info files, the
2328 symbols will be extracted from the debug file while abfd passed
2329 to this function is the real binary. */
8bd10d6b
AM
2330 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2331 && strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2332 ++i;
2333 codesecsym = i;
2334
2335 for (; i < symcount; ++i)
2336 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2337 | SEC_THREAD_LOCAL))
2338 != (SEC_CODE | SEC_ALLOC))
2339 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2340 break;
2341 codesecsymend = i;
2342
2343 for (; i < symcount; ++i)
2344 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2345 break;
2346 secsymend = i;
2347
2348 for (; i < symcount; ++i)
2349 if (strcmp (syms[i]->section->name, ".opd") != 0)
2350 break;
2351 opdsymend = i;
2352
2353 for (; i < symcount; ++i)
2cdcc330
AM
2354 if (((syms[i]->section->flags
2355 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2356 != (SEC_CODE | SEC_ALLOC))
2357 break;
2358 symcount = i;
2359 }
c9727e01 2360 count = 0;
90e3cdf2 2361
699733f6 2362 if (relocatable)
90e3cdf2 2363 {
0a1b45a2 2364 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
699733f6
AM
2365 arelent *r;
2366 size_t size;
0ccf57bd 2367 size_t relcount;
90e3cdf2 2368
468392fb
AM
2369 if (opdsymend == secsymend)
2370 goto done;
2371
699733f6 2372 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2373 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2374 if (relcount == 0)
c9727e01 2375 goto done;
90e3cdf2 2376
0a1b45a2 2377 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
7356fed5
AM
2378 {
2379 count = -1;
2380 goto done;
2381 }
2382
699733f6 2383 size = 0;
595da8c5 2384 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2385 {
2386 asymbol *sym;
90e3cdf2 2387
595da8c5 2388 while (r < opd->relocation + relcount
699733f6
AM
2389 && r->address < syms[i]->value + opd->vma)
2390 ++r;
90e3cdf2 2391
595da8c5 2392 if (r == opd->relocation + relcount)
699733f6 2393 break;
90e3cdf2 2394
699733f6
AM
2395 if (r->address != syms[i]->value + opd->vma)
2396 continue;
90e3cdf2 2397
699733f6
AM
2398 if (r->howto->type != R_PPC64_ADDR64)
2399 continue;
90e3cdf2 2400
699733f6
AM
2401 sym = *r->sym_ptr_ptr;
2402 if (!sym_exists_at (syms, opdsymend, symcount,
2403 sym->section->id, sym->value + r->addend))
2404 {
2405 ++count;
2406 size += sizeof (asymbol);
2407 size += strlen (syms[i]->name) + 2;
2408 }
2409 }
90e3cdf2 2410
c4b0b099
AM
2411 if (size == 0)
2412 goto done;
699733f6
AM
2413 s = *ret = bfd_malloc (size);
2414 if (s == NULL)
2415 {
7356fed5 2416 count = -1;
c9727e01 2417 goto done;
699733f6 2418 }
90e3cdf2 2419
699733f6 2420 names = (char *) (s + count);
90e3cdf2 2421
595da8c5 2422 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2423 {
699733f6 2424 asymbol *sym;
90e3cdf2 2425
595da8c5 2426 while (r < opd->relocation + relcount
699733f6
AM
2427 && r->address < syms[i]->value + opd->vma)
2428 ++r;
90e3cdf2 2429
595da8c5 2430 if (r == opd->relocation + relcount)
699733f6
AM
2431 break;
2432
2433 if (r->address != syms[i]->value + opd->vma)
2434 continue;
2435
2436 if (r->howto->type != R_PPC64_ADDR64)
2437 continue;
90e3cdf2 2438
699733f6
AM
2439 sym = *r->sym_ptr_ptr;
2440 if (!sym_exists_at (syms, opdsymend, symcount,
2441 sym->section->id, sym->value + r->addend))
2442 {
2443 size_t len;
2444
2445 *s = *syms[i];
6ba2a415 2446 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2447 s->section = sym->section;
2448 s->value = sym->value + r->addend;
2449 s->name = names;
2450 *names++ = '.';
2451 len = strlen (syms[i]->name);
2452 memcpy (names, syms[i]->name, len + 1);
2453 names += len + 1;
6f610d07
UW
2454 /* Have udata.p point back to the original symbol this
2455 synthetic symbol was derived from. */
2456 s->udata.p = syms[i];
699733f6
AM
2457 s++;
2458 }
2459 }
2460 }
2461 else
90e3cdf2 2462 {
0a1b45a2 2463 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
ee67d69a 2464 bfd_byte *contents = NULL;
699733f6 2465 size_t size;
0ccf57bd 2466 size_t plt_count = 0;
468392fb
AM
2467 bfd_vma glink_vma = 0, resolv_vma = 0;
2468 asection *dynamic, *glink = NULL, *relplt = NULL;
2469 arelent *p;
90e3cdf2 2470
ee67d69a 2471 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2472 {
c4b0b099
AM
2473 free_contents_and_exit_err:
2474 count = -1;
ee67d69a 2475 free_contents_and_exit:
c9594989 2476 free (contents);
c9727e01 2477 goto done;
699733f6 2478 }
90e3cdf2 2479
699733f6
AM
2480 size = 0;
2481 for (i = secsymend; i < opdsymend; ++i)
2482 {
2483 bfd_vma ent;
90e3cdf2 2484
5ef11c02
AM
2485 /* Ignore bogus symbols. */
2486 if (syms[i]->value > opd->size - 8)
2487 continue;
2488
699733f6
AM
2489 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2490 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2491 {
2492 ++count;
2493 size += sizeof (asymbol);
2494 size += strlen (syms[i]->name) + 2;
2495 }
2496 }
90e3cdf2 2497
468392fb 2498 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2499 if (dyn_count != 0
2500 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2501 {
2502 bfd_byte *dynbuf, *extdyn, *extdynend;
2503 size_t extdynsize;
2504 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2505
2506 if (!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
2512 extdyn = dynbuf;
2513 extdynend = extdyn + dynamic->size;
2514 for (; extdyn < extdynend; extdyn += extdynsize)
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
066ee829
AM
2572 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2573 size += plt_count * sizeof (asymbol);
468392fb 2574
066ee829
AM
2575 p = relplt->relocation;
2576 for (i = 0; i < plt_count; i++, p++)
e054468f
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
AM
2600 ent = bfd_get_64 (abfd, contents + syms[i]->value);
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++)
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;
6ba2a415 2697 s->flags |= BSF_SYNTHETIC;
468392fb
AM
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;
e054468f
AM
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 }
468392fb
AM
2712 memcpy (names, "@plt", sizeof ("@plt"));
2713 names += sizeof ("@plt");
2714 s++;
b9e5796b
AM
2715 if (abi < 2)
2716 {
2717 glink_vma += 8;
2718 if (i >= 0x8000)
2719 glink_vma += 4;
2720 }
2721 else
468392fb
AM
2722 glink_vma += 4;
2723 }
2724 count += plt_count;
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;
721956f4
AM
3082};
3083
2cdcc330
AM
3084struct ppc_branch_hash_entry
3085{
721956f4
AM
3086 /* Base hash table entry structure. */
3087 struct bfd_hash_entry root;
3088
c456f082 3089 /* Offset within branch lookup table. */
721956f4
AM
3090 unsigned int offset;
3091
3092 /* Generation marker. */
3093 unsigned int iter;
3094};
65f38f15 3095
1657026c 3096/* Used to track dynamic relocations. */
19e08130
AM
3097struct ppc_dyn_relocs
3098{
3099 struct ppc_dyn_relocs *next;
3100
3101 /* The input section of the reloc. */
3102 asection *sec;
3103
3104 /* Total number of relocs copied for the input section. */
1657026c
AM
3105 unsigned int count;
3106
3107 /* Number of pc-relative relocs copied for the input section. */
3108 unsigned int pc_count;
3109
3110 /* Number of relocs that might become R_PPC64_RELATIVE. */
3111 unsigned int rel_count;
3112};
3113
3114struct ppc_local_dyn_relocs
3115{
3116 struct ppc_local_dyn_relocs *next;
3117
3118 /* The input section of the reloc. */
3119 asection *sec;
3120
3121 /* Total number of relocs copied for the input section. */
3122 unsigned int count;
3123
3124 /* Number of relocs that might become R_PPC64_RELATIVE. */
3125 unsigned int rel_count : 31;
19e08130
AM
3126
3127 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3128 unsigned int ifunc : 1;
3129};
3130
65f38f15
AM
3131struct ppc_link_hash_entry
3132{
3133 struct elf_link_hash_entry elf;
3134
2cdcc330
AM
3135 union
3136 {
b3fac117
AM
3137 /* A pointer to the most recently used stub hash entry against this
3138 symbol. */
3139 struct ppc_stub_hash_entry *stub_cache;
3140
3141 /* A pointer to the next symbol starting with a '.' */
3142 struct ppc_link_hash_entry *next_dot_sym;
3143 } u;
721956f4 3144
721956f4 3145 /* Link between function code and descriptor symbols. */
34814b9f 3146 struct ppc_link_hash_entry *oh;
721956f4 3147
e86ce104
AM
3148 /* Flag function code and descriptor symbols. */
3149 unsigned int is_func:1;
3150 unsigned int is_func_descriptor:1;
908b32fc 3151 unsigned int fake:1;
411e1bfb 3152
c5614fa4
AM
3153 /* Whether global opd/toc sym has been adjusted or not.
3154 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3155 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3156 unsigned int adjust_done:1;
3157
a4b6fadd
AM
3158 /* Set if this is an out-of-line register save/restore function,
3159 with non-standard calling convention. */
3160 unsigned int save_res:1;
3161
8b5f1ed8
AM
3162 /* Set if a duplicate symbol with non-zero localentry is detected,
3163 even when the duplicate symbol does not provide a definition. */
3164 unsigned int non_zero_localentry:1;
3165
411e1bfb 3166 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3167 Bits are or'd into the mask as the corresponding relocs are
3168 encountered during check_relocs, with TLS_TLS being set when any
3169 of the other TLS bits are set. tls_optimize clears bits when
3170 optimizing to indicate the corresponding GOT entry type is not
3171 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3172 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3173 These flags are also kept for local symbols. */
3174#define TLS_TLS 1 /* Any TLS reloc. */
3175#define TLS_GD 2 /* GD reloc. */
3176#define TLS_LD 4 /* LD reloc. */
3177#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3178#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3179#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3180#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3181#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3182 unsigned char tls_mask;
37da22e5
AM
3183
3184 /* The above field is also used to mark function symbols. In which
3185 case TLS_TLS will be 0. */
3186#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3187#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3188#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3189};
3190
ed7007c1
AM
3191static inline struct ppc_link_hash_entry *
3192ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3193{
3194 return (struct ppc_link_hash_entry *) ent;
3195}
3196
30845f11
AM
3197static inline struct elf_link_hash_entry *
3198elf_hash_entry (struct ppc_link_hash_entry *ent)
3199{
3200 return (struct elf_link_hash_entry *) ent;
3201}
3202
65f38f15
AM
3203/* ppc64 ELF linker hash table. */
3204
3205struct ppc_link_hash_table
3206{
3207 struct elf_link_hash_table elf;
3208
721956f4
AM
3209 /* The stub hash table. */
3210 struct bfd_hash_table stub_hash_table;
3211
3212 /* Another hash table for plt_branch stubs. */
3213 struct bfd_hash_table branch_hash_table;
3214
3b421ab3
AM
3215 /* Hash table for function prologue tocsave. */
3216 htab_t tocsave_htab;
3217
e7d1c40c
AM
3218 /* Various options and other info passed from the linker. */
3219 struct ppc64_elf_params *params;
721956f4 3220
6f20ed8a
AM
3221 /* The size of sec_info below. */
3222 unsigned int sec_info_arr_size;
3223
3224 /* Per-section array of extra section info. Done this way rather
3225 than as part of ppc64_elf_section_data so we have the info for
3226 non-ppc64 sections. */
3227 struct
3228 {
3229 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3230 bfd_vma toc_off;
6f20ed8a
AM
3231
3232 union
3233 {
3234 /* The section group that this section belongs to. */
3235 struct map_stub *group;
3236 /* A temp section list pointer. */
3237 asection *list;
3238 } u;
3239 } *sec_info;
721956f4 3240
a4b6fadd
AM
3241 /* Linked list of groups. */
3242 struct map_stub *group;
3243
ad8e1ba5
AM
3244 /* Temp used when calculating TOC pointers. */
3245 bfd_vma toc_curr;
bf102f86
AM
3246 bfd *toc_bfd;
3247 asection *toc_first_sec;
ad8e1ba5 3248
b3fac117
AM
3249 /* Used when adding symbols. */
3250 struct ppc_link_hash_entry *dot_syms;
3251
33e44f2e 3252 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3253 asection *glink;
9e390558 3254 asection *global_entry;
82bd7b59 3255 asection *sfpr;
2d7ad24e
AM
3256 asection *pltlocal;
3257 asection *relpltlocal;
4ce794b7
AM
3258 asection *brlt;
3259 asection *relbrlt;
58d180e8 3260 asection *glink_eh_frame;
ec338859 3261
8387904d
AM
3262 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3263 struct ppc_link_hash_entry *tls_get_addr;
3264 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3265 struct ppc_link_hash_entry *tga_desc;
3266 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3267 struct map_stub *tga_group;
411e1bfb 3268
927be08e
AM
3269 /* The size of reliplt used by got entry relocs. */
3270 bfd_size_type got_reli_size;
3271
1657026c
AM
3272 /* DT_RELR array of r_offset. */
3273 size_t relr_alloc;
3274 size_t relr_count;
3275 bfd_vma *relr_addr;
3276
9b5ecbd0 3277 /* Statistics. */
7aba54da 3278 unsigned long stub_count[ppc_stub_save_res];
9b5ecbd0 3279
ee75fd95
AM
3280 /* Number of stubs against global syms. */
3281 unsigned long stub_globals;
3282
ee67d69a
AM
3283 /* Set if we're linking code with function descriptors. */
3284 unsigned int opd_abi:1;
3285
4c52953f 3286 /* Support for multiple toc sections. */
33c0ec9d 3287 unsigned int do_multi_toc:1;
4c52953f 3288 unsigned int multi_toc_needed:1;
927be08e 3289 unsigned int second_toc_pass:1;
67f0cbdb 3290 unsigned int do_toc_opt:1;
4c52953f 3291
9a23f96e
AM
3292 /* Set if tls optimization is enabled. */
3293 unsigned int do_tls_opt:1;
3294
3e04d765
AM
3295 /* Set if inline plt calls should be converted to direct calls. */
3296 unsigned int can_convert_all_inline_plt:1;
3297
0441f94f
AM
3298 /* Set if a stub_offset changed. */
3299 unsigned int stub_changed:1;
3300
5d1634d7 3301 /* Set on error. */
99877b66 3302 unsigned int stub_error:1;
721956f4 3303
8c5b4e52
AM
3304 /* Whether func_desc_adjust needs to be run over symbols. */
3305 unsigned int need_func_desc_adj:1;
721956f4 3306
f378ab09
AM
3307 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3308 unsigned int has_plt_localentry0:1;
3309
5663e321
AM
3310 /* Whether calls are made via the PLT from NOTOC functions. */
3311 unsigned int notoc_plt:1;
3312
e10a07b3
AM
3313 /* Whether any code linked seems to be Power10. */
3314 unsigned int has_power10_relocs:1;
3315
721956f4
AM
3316 /* Incremented every time we size stubs. */
3317 unsigned int stub_iteration;
0441f94f
AM
3318
3319/* After 20 iterations of stub sizing we no longer allow stubs to
3320 shrink. This is to break out of a pathological case where adding
3321 stubs or increasing their size on one iteration decreases section
3322 gaps (perhaps due to alignment), which then results in smaller
3323 stubs on the next iteration. */
3324#define STUB_SHRINK_ITER 20
65f38f15
AM
3325};
3326
4c52953f
AM
3327/* Rename some of the generic section flags to better document how they
3328 are used here. */
b0dddeec
AM
3329
3330/* Nonzero if this section has TLS related relocations. */
3331#define has_tls_reloc sec_flg0
3332
9737e8af
AM
3333/* Nonzero if this section has a call to __tls_get_addr lacking marker
3334 relocations. */
3335#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3336
3337/* Nonzero if this section has any toc or got relocs. */
3338#define has_toc_reloc sec_flg2
3339
3340/* Nonzero if this section has a call to another section that uses
3341 the toc or got. */
d77c8a4b 3342#define makes_toc_func_call sec_flg3
b0dddeec
AM
3343
3344/* Recursion protection when determining above flag. */
d77c8a4b 3345#define call_check_in_progress sec_flg4
70cc837d 3346#define call_check_done sec_flg5
4c52953f 3347
65f38f15
AM
3348/* Get the ppc64 ELF linker hash table from a link_info structure. */
3349
3350#define ppc_hash_table(p) \
573eb292
AM
3351 ((is_elf_hash_table ((p)->hash) \
3352 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3353 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3354
721956f4
AM
3355#define ppc_stub_hash_lookup(table, string, create, copy) \
3356 ((struct ppc_stub_hash_entry *) \
3357 bfd_hash_lookup ((table), (string), (create), (copy)))
3358
3359#define ppc_branch_hash_lookup(table, string, create, copy) \
3360 ((struct ppc_branch_hash_entry *) \
3361 bfd_hash_lookup ((table), (string), (create), (copy)))
3362
3363/* Create an entry in the stub hash table. */
3364
3365static struct bfd_hash_entry *
4ce794b7
AM
3366stub_hash_newfunc (struct bfd_hash_entry *entry,
3367 struct bfd_hash_table *table,
3368 const char *string)
721956f4
AM
3369{
3370 /* Allocate the structure if it has not already been allocated by a
3371 subclass. */
3372 if (entry == NULL)
3373 {
3374 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3375 if (entry == NULL)
3376 return entry;
3377 }
3378
3379 /* Call the allocation method of the superclass. */
3380 entry = bfd_hash_newfunc (entry, table, string);
3381 if (entry != NULL)
3382 {
3383 struct ppc_stub_hash_entry *eh;
3384
3385 /* Initialize the local fields. */
3386 eh = (struct ppc_stub_hash_entry *) entry;
7aba54da
AM
3387 eh->type.main = ppc_stub_none;
3388 eh->type.sub = ppc_stub_toc;
3389 eh->type.r2save = 0;
6f20ed8a 3390 eh->group = NULL;
721956f4
AM
3391 eh->stub_offset = 0;
3392 eh->target_value = 0;
3393 eh->target_section = NULL;
721956f4 3394 eh->h = NULL;
6911b7dc 3395 eh->plt_ent = NULL;
6911b7dc 3396 eh->other = 0;
721956f4
AM
3397 }
3398
3399 return entry;
3400}
3401
3402/* Create an entry in the branch hash table. */
3403
3404static struct bfd_hash_entry *
4ce794b7
AM
3405branch_hash_newfunc (struct bfd_hash_entry *entry,
3406 struct bfd_hash_table *table,
3407 const char *string)
721956f4
AM
3408{
3409 /* Allocate the structure if it has not already been allocated by a
3410 subclass. */
3411 if (entry == NULL)
3412 {
3413 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3414 if (entry == NULL)
3415 return entry;
3416 }
3417
3418 /* Call the allocation method of the superclass. */
3419 entry = bfd_hash_newfunc (entry, table, string);
3420 if (entry != NULL)
3421 {
3422 struct ppc_branch_hash_entry *eh;
3423
3424 /* Initialize the local fields. */
3425 eh = (struct ppc_branch_hash_entry *) entry;
3426 eh->offset = 0;
3427 eh->iter = 0;
3428 }
3429
3430 return entry;
3431}
3432
65f38f15
AM
3433/* Create an entry in a ppc64 ELF linker hash table. */
3434
3435static struct bfd_hash_entry *
4ce794b7
AM
3436link_hash_newfunc (struct bfd_hash_entry *entry,
3437 struct bfd_hash_table *table,
3438 const char *string)
65f38f15
AM
3439{
3440 /* Allocate the structure if it has not already been allocated by a
3441 subclass. */
3442 if (entry == NULL)
3443 {
3444 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3445 if (entry == NULL)
3446 return entry;
3447 }
3448
3449 /* Call the allocation method of the superclass. */
3450 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3451 if (entry != NULL)
3452 {
3453 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3454
b3fac117 3455 memset (&eh->u.stub_cache, 0,
908b32fc 3456 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3457 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3458
3459 /* When making function calls, old ABI code references function entry
3460 points (dot symbols), while new ABI code references the function
3461 descriptor symbol. We need to make any combination of reference and
3462 definition work together, without breaking archive linking.
3463
3464 For a defined function "foo" and an undefined call to "bar":
3465 An old object defines "foo" and ".foo", references ".bar" (possibly
3466 "bar" too).
3467 A new object defines "foo" and references "bar".
3468
3469 A new object thus has no problem with its undefined symbols being
3470 satisfied by definitions in an old object. On the other hand, the
3471 old object won't have ".bar" satisfied by a new object.
3472
3473 Keep a list of newly added dot-symbols. */
3474
3475 if (string[0] == '.')
3476 {
3477 struct ppc_link_hash_table *htab;
3478
3479 htab = (struct ppc_link_hash_table *) table;
3480 eh->u.next_dot_sym = htab->dot_syms;
3481 htab->dot_syms = eh;
3482 }
65f38f15
AM
3483 }
3484
3485 return entry;
3486}
3487
2cdcc330
AM
3488struct tocsave_entry
3489{
3b421ab3
AM
3490 asection *sec;
3491 bfd_vma offset;
3492};
3493
3494static hashval_t
3495tocsave_htab_hash (const void *p)
3496{
3497 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3498 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3499}
3500
3501static int
3502tocsave_htab_eq (const void *p1, const void *p2)
3503{
3504 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3505 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3506 return e1->sec == e2->sec && e1->offset == e2->offset;
3507}
3508
68faa637
AM
3509/* Destroy a ppc64 ELF linker hash table. */
3510
3511static void
d495ab0d 3512ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3513{
d495ab0d 3514 struct ppc_link_hash_table *htab;
68faa637 3515
d495ab0d 3516 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3517 if (htab->tocsave_htab)
3518 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3519 bfd_hash_table_free (&htab->branch_hash_table);
3520 bfd_hash_table_free (&htab->stub_hash_table);
3521 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3522}
3523
65f38f15
AM
3524/* Create a ppc64 ELF linker hash table. */
3525
3526static struct bfd_link_hash_table *
4ce794b7 3527ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3528{
3529 struct ppc_link_hash_table *htab;
986f0783 3530 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3531
4ce794b7 3532 htab = bfd_zmalloc (amt);
65f38f15
AM
3533 if (htab == NULL)
3534 return NULL;
3535
66eb6687 3536 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3537 sizeof (struct ppc_link_hash_entry),
3538 PPC64_ELF_DATA))
65f38f15 3539 {
e2d34d7d 3540 free (htab);
65f38f15
AM
3541 return NULL;
3542 }
3543
721956f4 3544 /* Init the stub hash table too. */
66eb6687
AM
3545 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3546 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3547 {
d495ab0d 3548 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3549 return NULL;
3550 }
721956f4
AM
3551
3552 /* And the branch hash table. */
66eb6687
AM
3553 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3554 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3555 {
3556 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3557 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3558 return NULL;
3559 }
721956f4 3560
3b421ab3
AM
3561 htab->tocsave_htab = htab_try_create (1024,
3562 tocsave_htab_hash,
3563 tocsave_htab_eq,
3564 NULL);
3565 if (htab->tocsave_htab == NULL)
2915c55b 3566 {
d495ab0d 3567 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3568 return NULL;
3569 }
d495ab0d 3570 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3571
3254fd24
AM
3572 /* Initializing two fields of the union is just cosmetic. We really
3573 only care about glist, but when compiled on a 32-bit host the
3574 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3575 debugger inspection of these fields look nicer. */
a6aa5195
AM
3576 htab->elf.init_got_refcount.refcount = 0;
3577 htab->elf.init_got_refcount.glist = NULL;
3578 htab->elf.init_plt_refcount.refcount = 0;
3579 htab->elf.init_plt_refcount.glist = NULL;
3580 htab->elf.init_got_offset.offset = 0;
3581 htab->elf.init_got_offset.glist = NULL;
3582 htab->elf.init_plt_offset.offset = 0;
3583 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3584
65f38f15
AM
3585 return &htab->elf.root;
3586}
3587
bfeb4a28
AM
3588/* Create sections for linker generated code. */
3589
0a1b45a2 3590static bool
bfeb4a28
AM
3591create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3592{
3593 struct ppc_link_hash_table *htab;
3594 flagword flags;
3595
3596 htab = ppc_hash_table (info);
3597
bfeb4a28
AM
3598 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3599 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3600 if (htab->params->save_restore_funcs)
3601 {
3602 /* Create .sfpr for code to save and restore fp regs. */
3603 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3604 flags);
3605 if (htab->sfpr == NULL
fd361982 3606 || !bfd_set_section_alignment (htab->sfpr, 2))
0a1b45a2 3607 return false;
b32547cd
AM
3608 }
3609
3610 if (bfd_link_relocatable (info))
0a1b45a2 3611 return true;
bfeb4a28
AM
3612
3613 /* Create .glink for lazy dynamic linking support. */
3614 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3615 flags);
3616 if (htab->glink == NULL
fd361982 3617 || !bfd_set_section_alignment (htab->glink, 3))
0a1b45a2 3618 return false;
bfeb4a28 3619
9e390558
AM
3620 /* The part of .glink used by global entry stubs, separate so that
3621 it can be aligned appropriately without affecting htab->glink. */
3622 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3623 flags);
3624 if (htab->global_entry == NULL
fd361982 3625 || !bfd_set_section_alignment (htab->global_entry, 2))
0a1b45a2 3626 return false;
9e390558 3627
bfeb4a28
AM
3628 if (!info->no_ld_generated_unwind_info)
3629 {
3630 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3631 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3632 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3633 ".eh_frame",
3634 flags);
3635 if (htab->glink_eh_frame == NULL
fd361982 3636 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
0a1b45a2 3637 return false;
bfeb4a28
AM
3638 }
3639
3640 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3641 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3642 if (htab->elf.iplt == NULL
fd361982 3643 || !bfd_set_section_alignment (htab->elf.iplt, 3))
0a1b45a2 3644 return false;
bfeb4a28
AM
3645
3646 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3647 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3648 htab->elf.irelplt
3649 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3650 if (htab->elf.irelplt == NULL
fd361982 3651 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
0a1b45a2 3652 return false;
bfeb4a28
AM
3653
3654 /* Create branch lookup table for plt_branch stubs. */
3655 flags = (SEC_ALLOC | SEC_LOAD
3656 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3657 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3658 flags);
3659 if (htab->brlt == NULL
fd361982 3660 || !bfd_set_section_alignment (htab->brlt, 3))
0a1b45a2 3661 return false;
bfeb4a28 3662
2d7ad24e
AM
3663 /* Local plt entries, put in .branch_lt but a separate section for
3664 convenience. */
3665 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3666 flags);
3667 if (htab->pltlocal == NULL
fd361982 3668 || !bfd_set_section_alignment (htab->pltlocal, 3))
0a1b45a2 3669 return false;
2d7ad24e 3670
0e1862bb 3671 if (!bfd_link_pic (info))
0a1b45a2 3672 return true;
bfeb4a28
AM
3673
3674 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3675 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3676 htab->relbrlt
3677 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3678 if (htab->relbrlt == NULL
fd361982 3679 || !bfd_set_section_alignment (htab->relbrlt, 3))
0a1b45a2 3680 return false;
bfeb4a28 3681
2d7ad24e
AM
3682 htab->relpltlocal
3683 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3684 if (htab->relpltlocal == NULL
fd361982 3685 || !bfd_set_section_alignment (htab->relpltlocal, 3))
0a1b45a2 3686 return false;
2d7ad24e 3687
0a1b45a2 3688 return true;
bfeb4a28
AM
3689}
3690
e717da7e
AM
3691/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3692
0a1b45a2 3693bool
e7d1c40c
AM
3694ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3695 struct ppc64_elf_params *params)
e717da7e
AM
3696{
3697 struct ppc_link_hash_table *htab;
3698
e7d1c40c 3699 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3700
3701/* Always hook our dynamic sections into the first bfd, which is the
3702 linker created stub bfd. This ensures that the GOT header is at
3703 the start of the output TOC section. */
3704 htab = ppc_hash_table (info);
e7d1c40c
AM
3705 htab->elf.dynobj = params->stub_bfd;
3706 htab->params = params;
bfeb4a28 3707
bfeb4a28 3708 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3709}
3710
721956f4
AM
3711/* Build a name for an entry in the stub hash table. */
3712
3713static char *
4ce794b7
AM
3714ppc_stub_name (const asection *input_section,
3715 const asection *sym_sec,
3716 const struct ppc_link_hash_entry *h,
3717 const Elf_Internal_Rela *rel)
721956f4
AM
3718{
3719 char *stub_name;
bcaa2f82 3720 ssize_t len;
721956f4
AM
3721
3722 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3723 offsets from a sym as a branch target? In fact, we could
3724 probably assume the addend is always zero. */
3725 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3726
3727 if (h)
3728 {
3729 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3730 stub_name = bfd_malloc (len);
46de2a7c
AM
3731 if (stub_name == NULL)
3732 return stub_name;
3733
bcaa2f82
AM
3734 len = sprintf (stub_name, "%08x.%s+%x",
3735 input_section->id & 0xffffffff,
3736 h->elf.root.root.string,
3737 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3738 }
3739 else
3740 {
ad8e1ba5 3741 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 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.%x:%x+%x",
3747 input_section->id & 0xffffffff,
3748 sym_sec->id & 0xffffffff,
3749 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3750 (int) rel->r_addend & 0xffffffff);
721956f4 3751 }
bcaa2f82 3752 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3753 stub_name[len - 2] = 0;
721956f4
AM
3754 return stub_name;
3755}
3756
e10a07b3 3757/* If mixing power10 with non-power10 code and --power10-stubs is not
7aba54da
AM
3758 specified (or is auto) then there may be multiple stub types for any
3759 given symbol. Up to three classes of stubs are stored in separate
3760 stub_hash_table entries having the same key string. The entries
3761 will always be adjacent on entry->root.next chain, even if hash
3762 table resizing occurs. This function selects the correct entry to
3763 use. */
e10a07b3
AM
3764
3765static struct ppc_stub_hash_entry *
7aba54da
AM
3766select_alt_stub (struct ppc_stub_hash_entry *entry,
3767 enum elf_ppc64_reloc_type r_type)
e10a07b3 3768{
7aba54da 3769 enum ppc_stub_sub_type subt;
e10a07b3 3770
7aba54da
AM
3771 switch (r_type)
3772 {
3773 case R_PPC64_REL24_NOTOC:
3774 subt = ppc_stub_notoc;
3775 break;
3776 case R_PPC64_REL24_P9NOTOC:
3777 subt = ppc_stub_p9notoc;
3778 break;
3779 default:
3780 subt = ppc_stub_toc;
3781 break;
3782 }
e10a07b3 3783
7aba54da 3784 while (entry != NULL && entry->type.sub != subt)
e10a07b3
AM
3785 {
3786 const char *stub_name = entry->root.string;
3787
3788 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3789 if (entry != NULL
3790 && entry->root.string != stub_name)
3791 entry = NULL;
3792 }
3793
3794 return entry;
3795}
3796
721956f4
AM
3797/* Look up an entry in the stub hash. Stub entries are cached because
3798 creating the stub name takes a bit of time. */
3799
3800static struct ppc_stub_hash_entry *
4ce794b7
AM
3801ppc_get_stub_entry (const asection *input_section,
3802 const asection *sym_sec,
039b3fef 3803 struct ppc_link_hash_entry *h,
4ce794b7
AM
3804 const Elf_Internal_Rela *rel,
3805 struct ppc_link_hash_table *htab)
721956f4
AM
3806{
3807 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3808 struct map_stub *group;
721956f4
AM
3809
3810 /* If this input section is part of a group of sections sharing one
3811 stub section, then use the id of the first section in the group.
3812 Stub names need to include a section id, as there may well be
3813 more than one stub used to reach say, printf, and we need to
3814 distinguish between them. */
6f20ed8a 3815 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3816 if (group == NULL)
3817 return NULL;
721956f4 3818
b3fac117
AM
3819 if (h != NULL && h->u.stub_cache != NULL
3820 && h->u.stub_cache->h == h
6f20ed8a 3821 && h->u.stub_cache->group == group)
721956f4 3822 {
b3fac117 3823 stub_entry = h->u.stub_cache;
721956f4
AM
3824 }
3825 else
3826 {
3827 char *stub_name;
3828
6f20ed8a 3829 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3830 if (stub_name == NULL)
3831 return NULL;
3832
3833 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 3834 stub_name, false, false);
721956f4 3835 if (h != NULL)
b3fac117 3836 h->u.stub_cache = stub_entry;
721956f4
AM
3837
3838 free (stub_name);
3839 }
3840
e10a07b3 3841 if (stub_entry != NULL && htab->params->power10_stubs == -1)
7aba54da 3842 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
e10a07b3 3843
721956f4
AM
3844 return stub_entry;
3845}
3846
3847/* Add a new stub entry to the stub hash. Not all fields of the new
3848 stub entry are initialised. */
3849
3850static struct ppc_stub_hash_entry *
4ce794b7
AM
3851ppc_add_stub (const char *stub_name,
3852 asection *section,
25f53a85 3853 struct bfd_link_info *info)
721956f4 3854{
25f53a85 3855 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3856 struct map_stub *group;
721956f4
AM
3857 asection *link_sec;
3858 asection *stub_sec;
3859 struct ppc_stub_hash_entry *stub_entry;
3860
6f20ed8a
AM
3861 group = htab->sec_info[section->id].u.group;
3862 link_sec = group->link_sec;
3863 stub_sec = group->stub_sec;
721956f4
AM
3864 if (stub_sec == NULL)
3865 {
6f20ed8a
AM
3866 size_t namelen;
3867 bfd_size_type len;
3868 char *s_name;
721956f4 3869
6f20ed8a
AM
3870 namelen = strlen (link_sec->name);
3871 len = namelen + sizeof (STUB_SUFFIX);
3872 s_name = bfd_alloc (htab->params->stub_bfd, len);
3873 if (s_name == NULL)
3874 return NULL;
721956f4 3875
6f20ed8a
AM
3876 memcpy (s_name, link_sec->name, namelen);
3877 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3878 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3879 if (stub_sec == NULL)
3880 return NULL;
3881 group->stub_sec = stub_sec;
721956f4
AM
3882 }
3883
3884 /* Enter this entry into the linker stub hash table. */
3885 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
0a1b45a2 3886 true, false);
721956f4
AM
3887 if (stub_entry == NULL)
3888 {
695344c0 3889 /* xgettext:c-format */
cf97bcb0
AM
3890 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3891 section->owner, stub_name);
721956f4
AM
3892 return NULL;
3893 }
3894
6f20ed8a 3895 stub_entry->group = group;
721956f4 3896 stub_entry->stub_offset = 0;
721956f4
AM
3897 return stub_entry;
3898}
3899
7aba54da
AM
3900/* A stub has already been created, but it may not be the required
3901 type. We shouldn't be transitioning from plt_call to long_branch
3902 stubs or vice versa, but we might be upgrading from plt_call to
3903 plt_call with r2save for example. */
3904
3905static bool
3906ppc_merge_stub (struct ppc_link_hash_table *htab,
3907 struct ppc_stub_hash_entry *stub_entry,
3908 struct ppc_stub_type stub_type,
3909 enum elf_ppc64_reloc_type r_type)
3910{
3911 struct ppc_stub_type old_type = stub_entry->type;
3912
3913 if (old_type.main == ppc_stub_save_res)
3914 return true;
3915
3916 if (htab->params->power10_stubs == -1)
3917 {
3918 /* For --power10-stubs=auto, don't merge _notoc and other
3919 varieties of stubs. */
3920 struct ppc_stub_hash_entry *alt_stub;
3921
3922 alt_stub = select_alt_stub (stub_entry, r_type);
3923 if (alt_stub == NULL)
3924 {
3925 alt_stub = ((struct ppc_stub_hash_entry *)
3926 stub_hash_newfunc (NULL,
3927 &htab->stub_hash_table,
3928 stub_entry->root.string));
3929 if (alt_stub == NULL)
3930 return false;
3931
3932 *alt_stub = *stub_entry;
3933 stub_entry->root.next = &alt_stub->root;
3934
3935 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3936 Not that it matters, this just puts smaller stubs first. */
3937 if (stub_type.sub == ppc_stub_notoc)
3938 alt_stub = stub_entry;
3939 else if (stub_type.sub == ppc_stub_p9notoc
3940 && alt_stub->root.next
3941 && alt_stub->root.next->string == alt_stub->root.string)
3942 {
3943 struct ppc_stub_hash_entry *next
3944 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3945 alt_stub->type = next->type;
3946 alt_stub = next;
3947 }
3948 alt_stub->type = stub_type;
3949 return true;
3950 }
3951 stub_entry = alt_stub;
3952 }
3953
3954 old_type = stub_entry->type;
3955 if (old_type.main == ppc_stub_plt_branch)
3956 old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
3957
3958 if (old_type.main != stub_type.main
3959 || (old_type.sub != stub_type.sub
3960 && old_type.sub != ppc_stub_toc
3961 && stub_type.sub != ppc_stub_toc))
3962 abort ();
3963
3964 stub_entry->type.sub |= stub_type.sub;
3965 stub_entry->type.r2save |= stub_type.r2save;
3966 return true;
3967}
3968
e717da7e
AM
3969/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3970 not already done. */
65f38f15 3971
0a1b45a2 3972static bool
e717da7e 3973create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3974{
e717da7e
AM
3975 asection *got, *relgot;
3976 flagword flags;
3977 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3978
0c8d6e5c 3979 if (!is_ppc64_elf (abfd))
0a1b45a2 3980 return false;
4dfe6ac6 3981 if (htab == NULL)
0a1b45a2 3982 return false;
0ffa91dd 3983
33e44f2e
AM
3984 if (!htab->elf.sgot
3985 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
0a1b45a2 3986 return false;
e717da7e
AM
3987
3988 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3989 | SEC_LINKER_CREATED);
3990
c456f082 3991 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3992 if (!got
fd361982 3993 || !bfd_set_section_alignment (got, 3))
0a1b45a2 3994 return false;
65f38f15 3995
c456f082
AM
3996 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3997 flags | SEC_READONLY);
e717da7e 3998 if (!relgot
fd361982 3999 || !bfd_set_section_alignment (relgot, 3))
0a1b45a2 4000 return false;
e717da7e
AM
4001
4002 ppc64_elf_tdata (abfd)->got = got;
4003 ppc64_elf_tdata (abfd)->relgot = relgot;
0a1b45a2 4004 return true;
65f38f15 4005}
5bd4f169 4006
b31867b6
AM
4007/* Follow indirect and warning symbol links. */
4008
4009static inline struct bfd_link_hash_entry *
4010follow_link (struct bfd_link_hash_entry *h)
4011{
4012 while (h->type == bfd_link_hash_indirect
4013 || h->type == bfd_link_hash_warning)
4014 h = h->u.i.link;
4015 return h;
4016}
4017
4018static inline struct elf_link_hash_entry *
4019elf_follow_link (struct elf_link_hash_entry *h)
4020{
4021 return (struct elf_link_hash_entry *) follow_link (&h->root);
4022}
4023
4024static inline struct ppc_link_hash_entry *
4025ppc_follow_link (struct ppc_link_hash_entry *h)
4026{
ed7007c1 4027 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
4028}
4029
40d16e0b
AM
4030/* Merge PLT info on FROM with that on TO. */
4031
4032static void
4033move_plt_plist (struct ppc_link_hash_entry *from,
4034 struct ppc_link_hash_entry *to)
4035{
4036 if (from->elf.plt.plist != NULL)
4037 {
4038 if (to->elf.plt.plist != NULL)
4039 {
4040 struct plt_entry **entp;
4041 struct plt_entry *ent;
4042
4043 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4044 {
4045 struct plt_entry *dent;
4046
4047 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4048 if (dent->addend == ent->addend)
4049 {
4050 dent->plt.refcount += ent->plt.refcount;
4051 *entp = ent->next;
4052 break;
4053 }
4054 if (dent == NULL)
4055 entp = &ent->next;
4056 }
4057 *entp = to->elf.plt.plist;
4058 }
4059
4060 to->elf.plt.plist = from->elf.plt.plist;
4061 from->elf.plt.plist = NULL;
4062 }
4063}
4064
65f38f15
AM
4065/* Copy the extra info we tack onto an elf_link_hash_entry. */
4066
4067static void
fcfa13d2
AM
4068ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4069 struct elf_link_hash_entry *dir,
4070 struct elf_link_hash_entry *ind)
65f38f15
AM
4071{
4072 struct ppc_link_hash_entry *edir, *eind;
4073
ed7007c1
AM
4074 edir = ppc_elf_hash_entry (dir);
4075 eind = ppc_elf_hash_entry (ind);
65f38f15 4076
c79d6685
AM
4077 edir->is_func |= eind->is_func;
4078 edir->is_func_descriptor |= eind->is_func_descriptor;
4079 edir->tls_mask |= eind->tls_mask;
4080 if (eind->oh != NULL)
4081 edir->oh = ppc_follow_link (eind->oh);
4082
474436e6 4083 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4084 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4085 edir->elf.ref_regular |= eind->elf.ref_regular;
4086 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4087 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4088 edir->elf.needs_plt |= eind->elf.needs_plt;
4089 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4090
d311bc8b
AM
4091 /* If we were called to copy over info for a weak sym, don't copy
4092 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4093 in order to simplify readonly_dynrelocs and save a field in the
4094 symbol hash entry, but that means dyn_relocs can't be used in any
4095 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4096 are then tested. */
d311bc8b 4097 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4098 return;
d311bc8b 4099
411e1bfb 4100 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 4101 if (ind->dyn_relocs != NULL)
65f38f15 4102 {
190eb1dd 4103 if (dir->dyn_relocs != NULL)
bbd7ec4a 4104 {
1657026c
AM
4105 struct ppc_dyn_relocs **pp;
4106 struct ppc_dyn_relocs *p;
bbd7ec4a 4107
fcfa13d2 4108 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 4109 list. Merge any entries against the same section. */
1657026c
AM
4110 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4111 (p = *pp) != NULL;
4112 )
bbd7ec4a 4113 {
1657026c 4114 struct ppc_dyn_relocs *q;
bbd7ec4a 4115
1657026c
AM
4116 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4117 q != NULL;
4118 q = q->next)
bbd7ec4a
AM
4119 if (q->sec == p->sec)
4120 {
bbd7ec4a 4121 q->count += p->count;
1657026c
AM
4122 q->pc_count += p->pc_count;
4123 q->rel_count += p->rel_count;
bbd7ec4a
AM
4124 *pp = p->next;
4125 break;
4126 }
4127 if (q == NULL)
4128 pp = &p->next;
4129 }
1657026c 4130 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
bbd7ec4a
AM
4131 }
4132
190eb1dd
L
4133 dir->dyn_relocs = ind->dyn_relocs;
4134 ind->dyn_relocs = NULL;
65f38f15 4135 }
65f38f15 4136
81848ca0
AM
4137 /* Copy over got entries that we may have already seen to the
4138 symbol which just became indirect. */
411e1bfb
AM
4139 if (eind->elf.got.glist != NULL)
4140 {
4141 if (edir->elf.got.glist != NULL)
4142 {
4143 struct got_entry **entp;
4144 struct got_entry *ent;
4145
4146 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4147 {
4148 struct got_entry *dent;
4149
4150 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4151 if (dent->addend == ent->addend
e717da7e 4152 && dent->owner == ent->owner
411e1bfb
AM
4153 && dent->tls_type == ent->tls_type)
4154 {
4155 dent->got.refcount += ent->got.refcount;
4156 *entp = ent->next;
4157 break;
4158 }
4159 if (dent == NULL)
4160 entp = &ent->next;
4161 }
4162 *entp = edir->elf.got.glist;
4163 }
4164
4165 edir->elf.got.glist = eind->elf.got.glist;
4166 eind->elf.got.glist = NULL;
4167 }
4168
4169 /* And plt entries. */
40d16e0b 4170 move_plt_plist (eind, edir);
411e1bfb 4171
fcfa13d2 4172 if (eind->elf.dynindx != -1)
411e1bfb 4173 {
fcfa13d2
AM
4174 if (edir->elf.dynindx != -1)
4175 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4176 edir->elf.dynstr_index);
411e1bfb
AM
4177 edir->elf.dynindx = eind->elf.dynindx;
4178 edir->elf.dynstr_index = eind->elf.dynstr_index;
4179 eind->elf.dynindx = -1;
4180 eind->elf.dynstr_index = 0;
4181 }
411e1bfb
AM
4182}
4183
8387904d
AM
4184/* Find the function descriptor hash entry from the given function code
4185 hash entry FH. Link the entries via their OH fields. */
4186
4187static struct ppc_link_hash_entry *
b31867b6 4188lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4189{
4190 struct ppc_link_hash_entry *fdh = fh->oh;
4191
4192 if (fdh == NULL)
4193 {
4194 const char *fd_name = fh->elf.root.root.string + 1;
4195
ed7007c1 4196 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
0a1b45a2 4197 false, false, false));
b31867b6
AM
4198 if (fdh == NULL)
4199 return fdh;
4200
4201 fdh->is_func_descriptor = 1;
4202 fdh->oh = fh;
4203 fh->is_func = 1;
4204 fh->oh = fdh;
8387904d
AM
4205 }
4206
8c5b4e52
AM
4207 fdh = ppc_follow_link (fdh);
4208 fdh->is_func_descriptor = 1;
4209 fdh->oh = fh;
4210 return fdh;
8387904d
AM
4211}
4212
8c5b4e52 4213/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4214
4215static struct ppc_link_hash_entry *
4216make_fdh (struct bfd_link_info *info,
908b32fc 4217 struct ppc_link_hash_entry *fh)
bb700d78 4218{
8c5b4e52
AM
4219 bfd *abfd = fh->elf.root.u.undef.abfd;
4220 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4221 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4222 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4223 ? BSF_WEAK
4224 : BSF_GLOBAL);
4225
4226 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4227 fh->elf.root.root.string + 1,
4228 flags, bfd_und_section_ptr, 0,
0a1b45a2 4229 NULL, false, false, &bh))
bb700d78
AM
4230 return NULL;
4231
4232 fdh = (struct ppc_link_hash_entry *) bh;
4233 fdh->elf.non_elf = 0;
908b32fc
AM
4234 fdh->fake = 1;
4235 fdh->is_func_descriptor = 1;
4236 fdh->oh = fh;
4237 fh->is_func = 1;
4238 fh->oh = fdh;
bb700d78
AM
4239 return fdh;
4240}
4241
8387904d
AM
4242/* Fix function descriptor symbols defined in .opd sections to be
4243 function type. */
555cd476 4244
0a1b45a2 4245static bool
c16153ae 4246ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4247 struct bfd_link_info *info,
555cd476 4248 Elf_Internal_Sym *isym,
6911b7dc 4249 const char **name,
555cd476
AM
4250 flagword *flags ATTRIBUTE_UNUSED,
4251 asection **sec,
b53dfeb2 4252 bfd_vma *value)
555cd476 4253{
b53dfeb2 4254 if (*sec != NULL
f1885d1e 4255 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4256 {
4257 asection *code_sec;
4258
4259 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4260 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4261 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4262
4263 /* If the symbol is a function defined in .opd, and the function
4264 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4265 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4266 && (*sec)->reloc_count != 0
4267 && opd_entry_value (*sec, *value, &code_sec, NULL,
0a1b45a2 4268 false) != (bfd_vma) -1
b53dfeb2
AM
4269 && discarded_section (code_sec))
4270 {
4271 *sec = bfd_und_section_ptr;
4272 isym->st_shndx = SHN_UNDEF;
4273 }
4274 }
dbd1e97e
AM
4275 else if (*sec != NULL
4276 && strcmp ((*sec)->name, ".toc") == 0
4277 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4278 {
4279 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4280 if (htab != NULL)
4281 htab->params->object_in_toc = 1;
4282 }
433817dd 4283
6911b7dc
AM
4284 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4285 {
4286 if (abiversion (ibfd) == 0)
4287 set_abiversion (ibfd, 2);
4288 else if (abiversion (ibfd) == 1)
4289 {
cf97bcb0
AM
4290 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4291 " for ABI version 1"), *name);
6911b7dc 4292 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4293 return false;
6911b7dc
AM
4294 }
4295 }
4296
0a1b45a2 4297 return true;
555cd476
AM
4298}
4299
6911b7dc
AM
4300/* Merge non-visibility st_other attributes: local entry point. */
4301
4302static void
4303ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4304 unsigned int st_other,
0a1b45a2
AM
4305 bool definition,
4306 bool dynamic)
6911b7dc 4307{
f378ab09 4308 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4309 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4310 | ELF_ST_VISIBILITY (h->other));
4311}
4312
8c5b4e52
AM
4313/* Hook called on merging a symbol. We use this to clear "fake" since
4314 we now have a real symbol. */
4315
0a1b45a2 4316static bool
8c5b4e52 4317ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4318 const Elf_Internal_Sym *isym,
8c5b4e52 4319 asection **psec ATTRIBUTE_UNUSED,
0a1b45a2
AM
4320 bool newdef ATTRIBUTE_UNUSED,
4321 bool olddef ATTRIBUTE_UNUSED,
8c5b4e52
AM
4322 bfd *oldbfd ATTRIBUTE_UNUSED,
4323 const asection *oldsec ATTRIBUTE_UNUSED)
4324{
ed7007c1 4325 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4326 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4327 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
0a1b45a2 4328 return true;
8c5b4e52
AM
4329}
4330
8387904d 4331/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4332 inclusion of a new ABI object archive that defines "bar".
4333 NAME is a symbol defined in an archive. Return a symbol in the hash
4334 table that might be satisfied by the archive symbols. */
8387904d 4335
b585e899 4336static struct bfd_link_hash_entry *
8387904d
AM
4337ppc64_elf_archive_symbol_lookup (bfd *abfd,
4338 struct bfd_link_info *info,
4339 const char *name)
4340{
b585e899 4341 struct bfd_link_hash_entry *h;
8387904d
AM
4342 char *dot_name;
4343 size_t len;
4344
4345 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc 4346 if (h != NULL
b585e899 4347 && ppc_hash_table (info) != NULL
908b32fc
AM
4348 /* Don't return this sym if it is a fake function descriptor
4349 created by add_symbol_adjust. */
b585e899 4350 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4351 return h;
4352
4353 if (name[0] == '.')
4354 return h;
4355
4356 len = strlen (name);
4357 dot_name = bfd_alloc (abfd, len + 2);
4358 if (dot_name == NULL)
b585e899 4359 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
4360 dot_name[0] = '.';
4361 memcpy (dot_name + 1, name, len + 1);
4362 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4363 bfd_release (abfd, dot_name);
a804e476
AM
4364 if (h != NULL)
4365 return h;
4366
4367 if (strcmp (name, "__tls_get_addr_opt") == 0)
4368 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4369 return h;
4370}
4371
4372/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4373 new ABI object defines "bar". Well, at least, undefined dot symbols
4374 are made weak. This stops later archive searches from including an
4375 object if we already have a function descriptor definition. It also
35b0ce59
AM
4376 prevents the linker complaining about undefined symbols.
4377 We also check and correct mismatched symbol visibility here. The
4378 most restrictive visibility of the function descriptor and the
4379 function entry symbol is used. */
8387904d 4380
0a1b45a2 4381static bool
b3fac117 4382add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4383{
8387904d
AM
4384 struct ppc_link_hash_table *htab;
4385 struct ppc_link_hash_entry *fdh;
4386
b3fac117
AM
4387 if (eh->elf.root.type == bfd_link_hash_warning)
4388 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4389
8400d40d 4390 if (eh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 4391 return true;
8400d40d 4392
b3fac117
AM
4393 if (eh->elf.root.root.string[0] != '.')
4394 abort ();
8387904d 4395
b3fac117 4396 htab = ppc_hash_table (info);
4dfe6ac6 4397 if (htab == NULL)
0a1b45a2 4398 return false;
4dfe6ac6 4399
b31867b6 4400 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4401 if (fdh == NULL
4402 && !bfd_link_relocatable (info)
4403 && (eh->elf.root.type == bfd_link_hash_undefined
4404 || eh->elf.root.type == bfd_link_hash_undefweak)
4405 && eh->elf.ref_regular)
4406 {
4407 /* Make an undefined function descriptor sym, in order to
4408 pull in an --as-needed shared lib. Archives are handled
4409 elsewhere. */
4410 fdh = make_fdh (info, eh);
4411 if (fdh == NULL)
0a1b45a2 4412 return false;
bb700d78 4413 }
8c5b4e52
AM
4414
4415 if (fdh != NULL)
8387904d 4416 {
35b0ce59
AM
4417 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4418 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4419
4420 /* Make both descriptor and entry symbol have the most
4421 constraining visibility of either symbol. */
35b0ce59
AM
4422 if (entry_vis < descr_vis)
4423 fdh->elf.other += entry_vis - descr_vis;
4424 else if (entry_vis > descr_vis)
4425 eh->elf.other += descr_vis - entry_vis;
4426
8c5b4e52
AM
4427 /* Propagate reference flags from entry symbol to function
4428 descriptor symbol. */
bc4e12de 4429 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4430 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4431 fdh->elf.ref_regular |= eh->elf.ref_regular;
4432 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4433
4434 if (!fdh->elf.forced_local
4435 && fdh->elf.dynindx == -1
4436 && fdh->elf.versioned != versioned_hidden
4437 && (bfd_link_dll (info)
4438 || fdh->elf.def_dynamic
4439 || fdh->elf.ref_dynamic)
4440 && (eh->elf.ref_regular
4441 || eh->elf.def_regular))
4442 {
2cdcc330 4443 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 4444 return false;
35b0ce59 4445 }
8387904d 4446 }
99877b66 4447
0a1b45a2 4448 return true;
8387904d
AM
4449}
4450
f6c7c3e8
AM
4451/* Set up opd section info and abiversion for IBFD, and process list
4452 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4453
0a1b45a2 4454static bool
f6c7c3e8 4455ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4456{
99877b66 4457 struct ppc_link_hash_table *htab;
b3fac117 4458 struct ppc_link_hash_entry **p, *eh;
459609d6 4459 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4460
459609d6 4461 if (opd != NULL && opd->size != 0)
b3fac117 4462 {
b9399fcf
AM
4463 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4464 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4465
459609d6
AM
4466 if (abiversion (ibfd) == 0)
4467 set_abiversion (ibfd, 1);
8a2058b5 4468 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4469 {
695344c0 4470 /* xgettext:c-format */
cf97bcb0
AM
4471 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4472 ibfd, abiversion (ibfd));
459609d6 4473 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4474 return false;
f6c7c3e8 4475 }
b9399fcf 4476 }
f6c7c3e8 4477
b9399fcf
AM
4478 if (is_ppc64_elf (info->output_bfd))
4479 {
4480 /* For input files without an explicit abiversion in e_flags
4481 we should have flagged any with symbol st_other bits set
4482 as ELFv1 and above flagged those with .opd as ELFv2.
4483 Set the output abiversion if not yet set, and for any input
4484 still ambiguous, take its abiversion from the output.
4485 Differences in ABI are reported later. */
4486 if (abiversion (info->output_bfd) == 0)
4487 set_abiversion (info->output_bfd, abiversion (ibfd));
4488 else if (abiversion (ibfd) == 0)
4489 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4490 }
4491
459609d6
AM
4492 htab = ppc_hash_table (info);
4493 if (htab == NULL)
0a1b45a2 4494 return true;
459609d6 4495
b9399fcf
AM
4496 if (opd != NULL && opd->size != 0
4497 && (ibfd->flags & DYNAMIC) == 0
4498 && (opd->flags & SEC_RELOC) != 0
4499 && opd->reloc_count != 0
4500 && !bfd_is_abs_section (opd->output_section)
4501 && info->gc_sections)
4502 {
4503 /* Garbage collection needs some extra help with .opd sections.
4504 We don't want to necessarily keep everything referenced by
4505 relocs in .opd, as that would keep all functions. Instead,
4506 if we reference an .opd symbol (a function descriptor), we
4507 want to keep the function code symbol's section. This is
4508 easy for global symbols, but for local syms we need to keep
4509 information about the associated function section. */
4510 bfd_size_type amt;
4511 asection **opd_sym_map;
4512 Elf_Internal_Shdr *symtab_hdr;
4513 Elf_Internal_Rela *relocs, *rel_end, *rel;
4514
4515 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4516 opd_sym_map = bfd_zalloc (ibfd, amt);
4517 if (opd_sym_map == NULL)
0a1b45a2 4518 return false;
b9399fcf
AM
4519 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4520 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4521 info->keep_memory);
4522 if (relocs == NULL)
0a1b45a2 4523 return false;
b9399fcf
AM
4524 symtab_hdr = &elf_symtab_hdr (ibfd);
4525 rel_end = relocs + opd->reloc_count - 1;
4526 for (rel = relocs; rel < rel_end; rel++)
4527 {
4528 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4529 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4530
4531 if (r_type == R_PPC64_ADDR64
4532 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4533 && r_symndx < symtab_hdr->sh_info)
4534 {
4535 Elf_Internal_Sym *isym;
4536 asection *s;
4537
f1dfbfdb
L
4538 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4539 r_symndx);
b9399fcf
AM
4540 if (isym == NULL)
4541 {
4542 if (elf_section_data (opd)->relocs != relocs)
4543 free (relocs);
0a1b45a2 4544 return false;
b9399fcf
AM
4545 }
4546
4547 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4548 if (s != NULL && s != opd)
4549 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4550 }
4551 }
4552 if (elf_section_data (opd)->relocs != relocs)
4553 free (relocs);
4554 }
459609d6
AM
4555
4556 p = &htab->dot_syms;
4557 while ((eh = *p) != NULL)
4558 {
4559 *p = NULL;
4560 if (&eh->elf == htab->elf.hgot)
4561 ;
4562 else if (htab->elf.hgot == NULL
4563 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4564 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4565 else if (abiversion (ibfd) <= 1)
4566 {
4567 htab->need_func_desc_adj = 1;
4568 if (!add_symbol_adjust (eh, info))
0a1b45a2 4569 return false;
8c5b4e52 4570 }
459609d6
AM
4571 p = &eh->u.next_dot_sym;
4572 }
0a1b45a2 4573 return true;
8387904d
AM
4574}
4575
97fed1c9
JJ
4576/* Undo hash table changes when an --as-needed input file is determined
4577 not to be needed. */
4578
0a1b45a2 4579static bool
e5034e59
AM
4580ppc64_elf_notice_as_needed (bfd *ibfd,
4581 struct bfd_link_info *info,
4582 enum notice_asneeded_action act)
97fed1c9 4583{
e5034e59
AM
4584 if (act == notice_not_needed)
4585 {
4586 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4587
e5034e59 4588 if (htab == NULL)
0a1b45a2 4589 return false;
4dfe6ac6 4590
e5034e59
AM
4591 htab->dot_syms = NULL;
4592 }
4593 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4594}
4595
aa374f67
AM
4596/* If --just-symbols against a final linked binary, then assume we need
4597 toc adjusting stubs when calling functions defined there. */
4598
4599static void
4600ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4601{
4602 if ((sec->flags & SEC_CODE) != 0
4603 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4604 && is_ppc64_elf (sec->owner))
4605 {
2c3f079f
AM
4606 if (abiversion (sec->owner) >= 2
4607 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4608 sec->has_toc_reloc = 1;
4609 }
4610 _bfd_elf_link_just_syms (sec, info);
4611}
4612
e054468f 4613static struct plt_entry **
4ce794b7
AM
4614update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4615 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4616{
4617 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4618 struct plt_entry **local_plt;
f961d9dd 4619 unsigned char *local_got_tls_masks;
411e1bfb
AM
4620
4621 if (local_got_ents == NULL)
4622 {
4623 bfd_size_type size = symtab_hdr->sh_info;
4624
e054468f
AM
4625 size *= (sizeof (*local_got_ents)
4626 + sizeof (*local_plt)
4627 + sizeof (*local_got_tls_masks));
4ce794b7 4628 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4629 if (local_got_ents == NULL)
e054468f 4630 return NULL;
411e1bfb
AM
4631 elf_local_got_ents (abfd) = local_got_ents;
4632 }
4633
37da22e5 4634 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4635 {
4636 struct got_entry *ent;
4637
4638 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4639 if (ent->addend == r_addend
4640 && ent->owner == abfd
4641 && ent->tls_type == tls_type)
411e1bfb
AM
4642 break;
4643 if (ent == NULL)
4644 {
986f0783 4645 size_t amt = sizeof (*ent);
4ce794b7 4646 ent = bfd_alloc (abfd, amt);
411e1bfb 4647 if (ent == NULL)
0a1b45a2 4648 return false;
411e1bfb
AM
4649 ent->next = local_got_ents[r_symndx];
4650 ent->addend = r_addend;
e717da7e 4651 ent->owner = abfd;
411e1bfb 4652 ent->tls_type = tls_type;
0a1b45a2 4653 ent->is_indirect = false;
411e1bfb
AM
4654 ent->got.refcount = 0;
4655 local_got_ents[r_symndx] = ent;
4656 }
4657 ent->got.refcount += 1;
4658 }
4659
e054468f 4660 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4661 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4662 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4663
4664 return local_plt + r_symndx;
65f38f15
AM
4665}
4666
0a1b45a2 4667static bool
e054468f 4668update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4669{
411e1bfb 4670 struct plt_entry *ent;
1e2f5b6e 4671
e054468f 4672 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4673 if (ent->addend == addend)
4674 break;
4675 if (ent == NULL)
1e2f5b6e 4676 {
986f0783 4677 size_t amt = sizeof (*ent);
4ce794b7 4678 ent = bfd_alloc (abfd, amt);
411e1bfb 4679 if (ent == NULL)
0a1b45a2 4680 return false;
e054468f 4681 ent->next = *plist;
411e1bfb
AM
4682 ent->addend = addend;
4683 ent->plt.refcount = 0;
e054468f 4684 *plist = ent;
1e2f5b6e 4685 }
411e1bfb 4686 ent->plt.refcount += 1;
0a1b45a2 4687 return true;
1e2f5b6e
AM
4688}
4689
0a1b45a2 4690static bool
e054468f
AM
4691is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4692{
4693 return (r_type == R_PPC64_REL24
05d0e962 4694 || r_type == R_PPC64_REL24_NOTOC
7aba54da 4695 || r_type == R_PPC64_REL24_P9NOTOC
e054468f
AM
4696 || r_type == R_PPC64_REL14
4697 || r_type == R_PPC64_REL14_BRTAKEN
4698 || r_type == R_PPC64_REL14_BRNTAKEN
4699 || r_type == R_PPC64_ADDR24
4700 || r_type == R_PPC64_ADDR14
4701 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4702 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4703 || r_type == R_PPC64_PLTCALL
4704 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4705}
4706
4707/* Relocs on inline plt call sequence insns prior to the call. */
4708
0a1b45a2 4709static bool
23cedd1d
AM
4710is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4711{
4712 return (r_type == R_PPC64_PLT16_HA
4713 || r_type == R_PPC64_PLT16_HI
4714 || r_type == R_PPC64_PLT16_LO
4715 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4716 || r_type == R_PPC64_PLT_PCREL34
4717 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4718 || r_type == R_PPC64_PLTSEQ
4719 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4720}
4721
733ae98c
AM
4722/* Of relocs which might appear paired with TLSGD and TLSLD marker
4723 relocs, return true for those that operate on a dword. */
4724
4725static bool
4726is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4727{
4728 return (r_type == R_PPC64_PLT_PCREL34
4729 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4730 || r_type == R_PPC64_PLTCALL);
4731}
4732
4733/* Like bfd_reloc_offset_in_range but without a howto. Return true
4734 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4735
4736static bool
4737offset_in_range (asection *sec, bfd_vma offset, size_t size)
4738{
4739 return offset <= sec->size && size <= sec->size - offset;
4740}
4741
5bd4f169 4742/* Look through the relocs for a section during the first phase, and
65f38f15 4743 calculate needed space in the global offset table, procedure
5d1634d7 4744 linkage table, and dynamic reloc sections. */
5bd4f169 4745
0a1b45a2 4746static bool
4ce794b7
AM
4747ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4748 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4749{
65f38f15 4750 struct ppc_link_hash_table *htab;
5bd4f169 4751 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4752 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4753 const Elf_Internal_Rela *rel;
4754 const Elf_Internal_Rela *rel_end;
5bd4f169 4755 asection *sreloc;
3a71aa26 4756 struct elf_link_hash_entry *tga, *dottga;
0a1b45a2 4757 bool is_opd;
5bd4f169 4758
0e1862bb 4759 if (bfd_link_relocatable (info))
0a1b45a2 4760 return true;
5bd4f169 4761
0c8d6e5c 4762 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4763
65f38f15 4764 htab = ppc_hash_table (info);
4dfe6ac6 4765 if (htab == NULL)
0a1b45a2 4766 return false;
4dfe6ac6 4767
3a71aa26 4768 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 4769 false, false, true);
3a71aa26 4770 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 4771 false, false, true);
0ffa91dd 4772 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4773 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4774 sreloc = NULL;
b9399fcf 4775 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4776 rel_end = relocs + sec->reloc_count;
4777 for (rel = relocs; rel < rel_end; rel++)
4778 {
4779 unsigned long r_symndx;
4780 struct elf_link_hash_entry *h;
3a3a4c1f 4781 Elf_Internal_Sym *isym;
04c9666a 4782 enum elf_ppc64_reloc_type r_type;
727fc41e 4783 int tls_type;
7c8fe5c4 4784 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4785 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4786
4787 r_symndx = ELF64_R_SYM (rel->r_info);
4788 if (r_symndx < symtab_hdr->sh_info)
3a3a4c1f
AM
4789 {
4790 h = NULL;
4791 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4792 if (isym == NULL)
4793 return false;
4794 }
5bd4f169 4795 else
973a3492 4796 {
3a3a4c1f 4797 isym = NULL;
973a3492 4798 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4799 h = elf_follow_link (h);
1c865ab2
AM
4800
4801 if (h == htab->elf.hgot)
4802 sec->has_toc_reloc = 1;
973a3492 4803 }
5bd4f169 4804
5663e321
AM
4805 r_type = ELF64_R_TYPE (rel->r_info);
4806 switch (r_type)
4807 {
04bdff6a
AM
4808 case R_PPC64_D34:
4809 case R_PPC64_D34_LO:
4810 case R_PPC64_D34_HI30:
4811 case R_PPC64_D34_HA30:
4812 case R_PPC64_D28:
c213164a
AM
4813 case R_PPC64_TPREL34:
4814 case R_PPC64_DTPREL34:
04bdff6a 4815 case R_PPC64_PCREL34:
5663e321 4816 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4817 case R_PPC64_GOT_TLSGD_PCREL34:
4818 case R_PPC64_GOT_TLSLD_PCREL34:
4819 case R_PPC64_GOT_TPREL_PCREL34:
4820 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4821 case R_PPC64_PLT_PCREL34:
4822 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4823 case R_PPC64_PCREL28:
e10a07b3 4824 htab->has_power10_relocs = 1;
133a1f60
AM
4825 break;
4826 default:
5663e321
AM
4827 break;
4828 }
903b777d
AM
4829
4830 switch (r_type)
4831 {
4832 case R_PPC64_PLT16_HA:
4833 case R_PPC64_GOT_TLSLD16_HA:
4834 case R_PPC64_GOT_TLSGD16_HA:
4835 case R_PPC64_GOT_TPREL16_HA:
4836 case R_PPC64_GOT_DTPREL16_HA:
4837 case R_PPC64_GOT16_HA:
4838 case R_PPC64_TOC16_HA:
4839 case R_PPC64_PLT16_LO:
4840 case R_PPC64_PLT16_LO_DS:
4841 case R_PPC64_GOT_TLSLD16_LO:
4842 case R_PPC64_GOT_TLSGD16_LO:
4843 case R_PPC64_GOT_TPREL16_LO_DS:
4844 case R_PPC64_GOT_DTPREL16_LO_DS:
4845 case R_PPC64_GOT16_LO:
4846 case R_PPC64_GOT16_LO_DS:
4847 case R_PPC64_TOC16_LO:
4848 case R_PPC64_TOC16_LO_DS:
4849 case R_PPC64_GOT_PCREL34:
4850 ppc64_elf_tdata (abfd)->has_optrel = 1;
4851 ppc64_elf_section_data (sec)->has_optrel = 1;
4852 break;
4853 default:
4854 break;
4855 }
4856
f749f26e 4857 ifunc = NULL;
25f23106
AM
4858 if (h != NULL)
4859 {
4860 if (h->type == STT_GNU_IFUNC)
4861 {
4862 h->needs_plt = 1;
4863 ifunc = &h->plt.plist;
4864 }
4865 }
4866 else
4867 {
25f23106
AM
4868 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4869 {
4870 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4871 rel->r_addend,
37da22e5 4872 NON_GOT | PLT_IFUNC);
25f23106 4873 if (ifunc == NULL)
0a1b45a2 4874 return false;
25f23106
AM
4875 }
4876 }
727fc41e 4877
f749f26e 4878 tls_type = 0;
a33d1f77 4879 switch (r_type)
5bd4f169 4880 {
727fc41e
AM
4881 case R_PPC64_TLSGD:
4882 case R_PPC64_TLSLD:
4883 /* These special tls relocs tie a call to __tls_get_addr with
4884 its parameter symbol. */
37da22e5 4885 if (h != NULL)
ed7007c1 4886 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4887 else
4888 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4889 rel->r_addend,
37da22e5 4890 NON_GOT | TLS_TLS | TLS_MARK))
0a1b45a2 4891 return false;
37da22e5 4892 sec->has_tls_reloc = 1;
727fc41e
AM
4893 break;
4894
411e1bfb
AM
4895 case R_PPC64_GOT_TLSLD16:
4896 case R_PPC64_GOT_TLSLD16_LO:
4897 case R_PPC64_GOT_TLSLD16_HI:
4898 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4899 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4900 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4901 goto dogottls;
4902
4903 case R_PPC64_GOT_TLSGD16:
4904 case R_PPC64_GOT_TLSGD16_LO:
4905 case R_PPC64_GOT_TLSGD16_HI:
4906 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4907 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4908 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4909 goto dogottls;
4910
4911 case R_PPC64_GOT_TPREL16_DS:
4912 case R_PPC64_GOT_TPREL16_LO_DS:
4913 case R_PPC64_GOT_TPREL16_HI:
4914 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4915 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4916 if (bfd_link_dll (info))
411e1bfb
AM
4917 info->flags |= DF_STATIC_TLS;
4918 tls_type = TLS_TLS | TLS_TPREL;
4919 goto dogottls;
4920
4921 case R_PPC64_GOT_DTPREL16_DS:
4922 case R_PPC64_GOT_DTPREL16_LO_DS:
4923 case R_PPC64_GOT_DTPREL16_HI:
4924 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4925 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4926 tls_type = TLS_TLS | TLS_DTPREL;
4927 dogottls:
4928 sec->has_tls_reloc = 1;
066f4018 4929 goto dogot;
411e1bfb 4930
903b777d
AM
4931 case R_PPC64_GOT16:
4932 case R_PPC64_GOT16_LO:
4933 case R_PPC64_GOT16_HI:
65f38f15 4934 case R_PPC64_GOT16_HA:
903b777d 4935 case R_PPC64_GOT16_DS:
066f4018 4936 case R_PPC64_GOT16_LO_DS:
4a421c53 4937 case R_PPC64_GOT_PCREL34:
066f4018 4938 dogot:
65f38f15 4939 /* This symbol requires a global offset table entry. */
4c52953f 4940 sec->has_toc_reloc = 1;
33c0ec9d
AM
4941 if (r_type == R_PPC64_GOT_TLSLD16
4942 || r_type == R_PPC64_GOT_TLSGD16
4943 || r_type == R_PPC64_GOT_TPREL16_DS
4944 || r_type == R_PPC64_GOT_DTPREL16_DS
4945 || r_type == R_PPC64_GOT16
4946 || r_type == R_PPC64_GOT16_DS)
4947 {
4948 htab->do_multi_toc = 1;
d77c8a4b 4949 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4950 }
4951
e717da7e
AM
4952 if (ppc64_elf_tdata (abfd)->got == NULL
4953 && !create_got_section (abfd, info))
0a1b45a2 4954 return false;
5bd4f169
AM
4955
4956 if (h != NULL)
4957 {
411e1bfb
AM
4958 struct ppc_link_hash_entry *eh;
4959 struct got_entry *ent;
65f38f15 4960
ed7007c1 4961 eh = ppc_elf_hash_entry (h);
411e1bfb 4962 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4963 if (ent->addend == rel->r_addend
e717da7e 4964 && ent->owner == abfd
411e1bfb
AM
4965 && ent->tls_type == tls_type)
4966 break;
4967 if (ent == NULL)
5bd4f169 4968 {
986f0783 4969 size_t amt = sizeof (*ent);
4ce794b7 4970 ent = bfd_alloc (abfd, amt);
411e1bfb 4971 if (ent == NULL)
0a1b45a2 4972 return false;
411e1bfb 4973 ent->next = eh->elf.got.glist;
133a1f60 4974 ent->addend = rel->r_addend;
e717da7e 4975 ent->owner = abfd;
411e1bfb 4976 ent->tls_type = tls_type;
0a1b45a2 4977 ent->is_indirect = false;
411e1bfb
AM
4978 ent->got.refcount = 0;
4979 eh->elf.got.glist = ent;
5bd4f169 4980 }
411e1bfb 4981 ent->got.refcount += 1;
e7b938ca 4982 eh->tls_mask |= tls_type;
5bd4f169 4983 }
411e1bfb
AM
4984 else
4985 /* This is a global offset table entry for a local symbol. */
4986 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4987 rel->r_addend, tls_type))
0a1b45a2 4988 return false;
5bd4f169
AM
4989 break;
4990
5bd4f169 4991 case R_PPC64_PLT16_HA:
65f38f15
AM
4992 case R_PPC64_PLT16_HI:
4993 case R_PPC64_PLT16_LO:
08be3224 4994 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4995 case R_PPC64_PLT_PCREL34:
4996 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4997 case R_PPC64_PLT32:
4998 case R_PPC64_PLT64:
cbf95972
AM
4999 /* This symbol requires a procedure linkage table entry. */
5000 plt_list = ifunc;
5001 if (h != NULL)
e054468f 5002 {
e054468f
AM
5003 h->needs_plt = 1;
5004 if (h->root.root.string[0] == '.'
5005 && h->root.root.string[1] != '\0')
ed7007c1
AM
5006 ppc_elf_hash_entry (h)->is_func = 1;
5007 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5008 plt_list = &h->plt.plist;
5009 }
5010 if (plt_list == NULL)
2d7ad24e 5011 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5012 rel->r_addend,
2d7ad24e 5013 NON_GOT | PLT_KEEP);
133a1f60 5014 if (!update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5015 return false;
5bd4f169
AM
5016 break;
5017
5018 /* The following relocations don't need to propagate the
5019 relocation if linking a shared object since they are
5020 section relative. */
5021 case R_PPC64_SECTOFF:
5022 case R_PPC64_SECTOFF_LO:
5023 case R_PPC64_SECTOFF_HI:
5024 case R_PPC64_SECTOFF_HA:
5025 case R_PPC64_SECTOFF_DS:
5026 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5027 case R_PPC64_DTPREL16:
5028 case R_PPC64_DTPREL16_LO:
5029 case R_PPC64_DTPREL16_HI:
5030 case R_PPC64_DTPREL16_HA:
5031 case R_PPC64_DTPREL16_DS:
5032 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5033 case R_PPC64_DTPREL16_HIGH:
5034 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5035 case R_PPC64_DTPREL16_HIGHER:
5036 case R_PPC64_DTPREL16_HIGHERA:
5037 case R_PPC64_DTPREL16_HIGHEST:
5038 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5039 break;
5040
ad8e1ba5 5041 /* Nor do these. */
25f23106
AM
5042 case R_PPC64_REL16:
5043 case R_PPC64_REL16_LO:
5044 case R_PPC64_REL16_HI:
5045 case R_PPC64_REL16_HA:
4a969973
AM
5046 case R_PPC64_REL16_HIGH:
5047 case R_PPC64_REL16_HIGHA:
5048 case R_PPC64_REL16_HIGHER:
5049 case R_PPC64_REL16_HIGHERA:
5050 case R_PPC64_REL16_HIGHEST:
5051 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
5052 case R_PPC64_REL16_HIGHER34:
5053 case R_PPC64_REL16_HIGHERA34:
5054 case R_PPC64_REL16_HIGHEST34:
5055 case R_PPC64_REL16_HIGHESTA34:
a680de9a 5056 case R_PPC64_REL16DX_HA:
25f23106
AM
5057 break;
5058
45965137
AM
5059 /* Not supported as a dynamic relocation. */
5060 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5061 if (bfd_link_pic (info))
45965137
AM
5062 {
5063 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5064 ppc_howto_init ();
695344c0 5065 /* xgettext:c-format */
174d0a74 5066 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5067 "in shared libraries and PIEs\n"),
45965137
AM
5068 abfd, sec, rel->r_offset,
5069 ppc64_elf_howto_table[r_type]->name);
5070 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5071 return false;
45965137
AM
5072 }
5073 break;
5074
ad8e1ba5 5075 case R_PPC64_TOC16:
33c0ec9d
AM
5076 case R_PPC64_TOC16_DS:
5077 htab->do_multi_toc = 1;
d77c8a4b 5078 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5079 /* Fall through. */
ad8e1ba5
AM
5080 case R_PPC64_TOC16_LO:
5081 case R_PPC64_TOC16_HI:
5082 case R_PPC64_TOC16_HA:
ad8e1ba5 5083 case R_PPC64_TOC16_LO_DS:
4c52953f 5084 sec->has_toc_reloc = 1;
ec73ddcd 5085 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
5086 {
5087 /* We may need a copy reloc. */
5088 h->non_got_ref = 1;
5089 /* Strongly prefer a copy reloc over a dynamic reloc.
5090 glibc ld.so as of 2019-08 will error out if one of
5091 these relocations is emitted. */
5092 h->needs_copy = 1;
5093 goto dodyn;
5094 }
ad8e1ba5
AM
5095 break;
5096
006589cf
AM
5097 /* Marker reloc. */
5098 case R_PPC64_ENTRY:
5099 break;
5100
5bd4f169
AM
5101 /* This relocation describes the C++ object vtable hierarchy.
5102 Reconstruct it for later use during GC. */
5103 case R_PPC64_GNU_VTINHERIT:
c152c796 5104 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
0a1b45a2 5105 return false;
5bd4f169
AM
5106 break;
5107
5108 /* This relocation describes which C++ vtable entries are actually
5109 used. Record for later use during GC. */
5110 case R_PPC64_GNU_VTENTRY:
a0ea3a14 5111 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
0a1b45a2 5112 return false;
5bd4f169
AM
5113 break;
5114
721956f4
AM
5115 case R_PPC64_REL14:
5116 case R_PPC64_REL14_BRTAKEN:
5117 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5118 {
5119 asection *dest = NULL;
5120
5121 /* Heuristic: If jumping outside our section, chances are
5122 we are going to need a stub. */
5123 if (h != NULL)
5124 {
5125 /* If the sym is weak it may be overridden later, so
5126 don't assume we know where a weak sym lives. */
5127 if (h->root.type == bfd_link_hash_defined)
5128 dest = h->root.u.def.section;
5129 }
5130 else
3a3a4c1f 5131 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
87d72d41 5132
220c76dd 5133 if (dest != sec)
7c8fe5c4 5134 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5135 }
3e04d765
AM
5136 goto rel24;
5137
5138 case R_PPC64_PLTCALL:
5663e321 5139 case R_PPC64_PLTCALL_NOTOC:
3e04d765 5140 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5141 /* Fall through. */
5142
5d1634d7 5143 case R_PPC64_REL24:
05d0e962 5144 case R_PPC64_REL24_NOTOC:
7aba54da 5145 case R_PPC64_REL24_P9NOTOC:
3e04d765 5146 rel24:
cbf95972
AM
5147 plt_list = ifunc;
5148 if (h != NULL)
5d1634d7 5149 {
e054468f
AM
5150 h->needs_plt = 1;
5151 if (h->root.root.string[0] == '.'
5152 && h->root.root.string[1] != '\0')
ed7007c1 5153 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 5154
3a71aa26 5155 if (h == tga || h == dottga)
cbf95972
AM
5156 {
5157 sec->has_tls_reloc = 1;
5158 if (rel != relocs
5159 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5160 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5161 /* We have a new-style __tls_get_addr call with
5162 a marker reloc. */
5163 ;
5164 else
5165 /* Mark this section as having an old-style call. */
9737e8af 5166 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5167 }
5168 plt_list = &h->plt.plist;
411e1bfb 5169 }
cbf95972
AM
5170
5171 /* We may need a .plt entry if the function this reloc
5172 refers to is in a shared lib. */
5173 if (plt_list
133a1f60 5174 && !update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5175 return false;
411e1bfb
AM
5176 break;
5177
cbf95972
AM
5178 case R_PPC64_ADDR14:
5179 case R_PPC64_ADDR14_BRNTAKEN:
5180 case R_PPC64_ADDR14_BRTAKEN:
5181 case R_PPC64_ADDR24:
5182 goto dodyn;
5183
411e1bfb
AM
5184 case R_PPC64_TPREL64:
5185 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5186 if (bfd_link_dll (info))
411e1bfb
AM
5187 info->flags |= DF_STATIC_TLS;
5188 goto dotlstoc;
5189
5190 case R_PPC64_DTPMOD64:
5191 if (rel + 1 < rel_end
5192 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5193 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5194 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5195 else
951fd09b 5196 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5197 goto dotlstoc;
5198
5199 case R_PPC64_DTPREL64:
5200 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5201 if (rel != relocs
5202 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5203 && rel[-1].r_offset == rel->r_offset - 8)
5204 /* This is the second reloc of a dtpmod, dtprel pair.
5205 Don't mark with TLS_DTPREL. */
5206 goto dodyn;
5207
5208 dotlstoc:
5209 sec->has_tls_reloc = 1;
5210 if (h != NULL)
ed7007c1 5211 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5212 else
5213 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5214 rel->r_addend, tls_type))
0a1b45a2 5215 return false;
411e1bfb 5216
7c8fe5c4
AM
5217 ppc64_sec = ppc64_elf_section_data (sec);
5218 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5219 {
3a71aa26
AM
5220 bfd_size_type amt;
5221
e7b938ca 5222 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5223 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5224 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5225 if (ppc64_sec->u.toc.symndx == NULL)
0a1b45a2 5226 return false;
3a71aa26
AM
5227 amt = sec->size * sizeof (bfd_vma) / 8;
5228 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5229 if (ppc64_sec->u.toc.add == NULL)
0a1b45a2 5230 return false;
7c8fe5c4
AM
5231 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5232 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5233 }
5234 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5235 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5236 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5237
5238 /* Mark the second slot of a GD or LD entry.
5239 -1 to indicate GD and -2 to indicate LD. */
5240 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5241 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5242 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5243 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5244 goto dodyn;
5245
411e1bfb
AM
5246 case R_PPC64_TPREL16_HI:
5247 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5248 case R_PPC64_TPREL16_HIGH:
5249 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5250 case R_PPC64_TPREL16_HIGHER:
5251 case R_PPC64_TPREL16_HIGHERA:
5252 case R_PPC64_TPREL16_HIGHEST:
5253 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5254 sec->has_tls_reloc = 1;
5255 /* Fall through. */
c213164a 5256 case R_PPC64_TPREL34:
252dcdf4
AM
5257 case R_PPC64_TPREL16:
5258 case R_PPC64_TPREL16_DS:
5259 case R_PPC64_TPREL16_LO:
5260 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5261 if (bfd_link_dll (info))
5262 info->flags |= DF_STATIC_TLS;
5263 goto dodyn;
5d1634d7 5264
e86ce104 5265 case R_PPC64_ADDR64:
b9399fcf 5266 if (is_opd
1e2f5b6e 5267 && rel + 1 < rel_end
4ce794b7 5268 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5269 {
8387904d 5270 if (h != NULL)
ed7007c1 5271 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5272 }
e86ce104
AM
5273 /* Fall through. */
5274
65f38f15
AM
5275 case R_PPC64_ADDR16:
5276 case R_PPC64_ADDR16_DS:
5277 case R_PPC64_ADDR16_HA:
5278 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5279 case R_PPC64_ADDR16_HIGH:
5280 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5281 case R_PPC64_ADDR16_HIGHER:
5282 case R_PPC64_ADDR16_HIGHERA:
5283 case R_PPC64_ADDR16_HIGHEST:
5284 case R_PPC64_ADDR16_HIGHESTA:
5285 case R_PPC64_ADDR16_LO:
5286 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5287 case R_PPC64_D34:
5288 case R_PPC64_D34_LO:
5289 case R_PPC64_D34_HI30:
5290 case R_PPC64_D34_HA30:
5291 case R_PPC64_ADDR16_HIGHER34:
5292 case R_PPC64_ADDR16_HIGHERA34:
5293 case R_PPC64_ADDR16_HIGHEST34:
5294 case R_PPC64_ADDR16_HIGHESTA34:
5295 case R_PPC64_D28:
0e1862bb 5296 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5297 && rel->r_addend == 0)
5298 {
5299 /* We may need a .plt entry if this reloc refers to a
5300 function in a shared lib. */
5663e321 5301 if (!update_plt_info (abfd, &h->plt.plist, 0))
0a1b45a2 5302 return false;
a345bc8d
AM
5303 h->pointer_equality_needed = 1;
5304 }
5305 /* Fall through. */
5306
5307 case R_PPC64_REL30:
5308 case R_PPC64_REL32:
5309 case R_PPC64_REL64:
65f38f15 5310 case R_PPC64_ADDR32:
65f38f15
AM
5311 case R_PPC64_UADDR16:
5312 case R_PPC64_UADDR32:
5313 case R_PPC64_UADDR64:
5bd4f169 5314 case R_PPC64_TOC:
ec73ddcd 5315 if (h != NULL && bfd_link_executable (info))
81848ca0 5316 /* We may need a copy reloc. */
f5385ebf 5317 h->non_got_ref = 1;
81848ca0 5318
41bd81ab 5319 /* Don't propagate .opd relocs. */
b9399fcf 5320 if (NO_OPD_RELOCS && is_opd)
e86ce104 5321 break;
e86ce104 5322
65f38f15
AM
5323 /* If we are creating a shared library, and this is a reloc
5324 against a global symbol, or a non PC relative reloc
5325 against a local symbol, then we need to copy the reloc
5326 into the shared library. However, if we are linking with
5327 -Bsymbolic, we do not need to copy a reloc against a
5328 global symbol which is defined in an object we are
5329 including in the link (i.e., DEF_REGULAR is set). At
5330 this point we have not seen all the input files, so it is
5331 possible that DEF_REGULAR is not set now but will be set
5332 later (it is never cleared). In case of a weak definition,
5333 DEF_REGULAR may be cleared later by a strong definition in
5334 a shared library. We account for that possibility below by
f4656909 5335 storing information in the dyn_relocs field of the hash
65f38f15
AM
5336 table entry. A similar situation occurs when creating
5337 shared libraries and symbol visibility changes render the
5338 symbol local.
5339
5340 If on the other hand, we are creating an executable, we
5341 may need to keep relocations for symbols satisfied by a
5342 dynamic library if we manage to avoid copy relocs for the
5343 symbol. */
411e1bfb 5344 dodyn:
ec73ddcd
AM
5345 if ((h != NULL
5346 && (h->root.type == bfd_link_hash_defweak
3a3a4c1f 5347 || (!h->def_regular && !h->root.ldscript_def)))
ec73ddcd 5348 || (h != NULL
3a3a4c1f 5349 && !SYMBOL_REFERENCES_LOCAL (info, h))
ec73ddcd 5350 || (bfd_link_pic (info)
3a3a4c1f
AM
5351 && (h != NULL
5352 ? !bfd_is_abs_symbol (&h->root)
5353 : isym->st_shndx != SHN_ABS)
ec73ddcd 5354 && must_be_dyn_reloc (info, r_type))
0e1862bb 5355 || (!bfd_link_pic (info)
25f23106 5356 && ifunc != NULL))
5bd4f169 5357 {
65f38f15
AM
5358 /* We must copy these reloc types into the output file.
5359 Create a reloc section in dynobj and make room for
5360 this reloc. */
5bd4f169
AM
5361 if (sreloc == NULL)
5362 {
83bac4b0 5363 sreloc = _bfd_elf_make_dynamic_reloc_section
0a1b45a2 5364 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
65f38f15 5365
5bd4f169 5366 if (sreloc == NULL)
0a1b45a2 5367 return false;
5bd4f169
AM
5368 }
5369
65f38f15
AM
5370 /* If this is a global symbol, we count the number of
5371 relocations we need for this symbol. */
5372 if (h != NULL)
5373 {
1657026c
AM
5374 struct ppc_dyn_relocs *p;
5375 struct ppc_dyn_relocs **head;
19e08130 5376
1657026c 5377 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
5378 p = *head;
5379 if (p == NULL || p->sec != sec)
5380 {
5381 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5382 if (p == NULL)
0a1b45a2 5383 return false;
19e08130
AM
5384 p->next = *head;
5385 *head = p;
5386 p->sec = sec;
5387 p->count = 0;
5388 p->pc_count = 0;
1657026c 5389 p->rel_count = 0;
19e08130
AM
5390 }
5391 p->count += 1;
5392 if (!must_be_dyn_reloc (info, r_type))
5393 p->pc_count += 1;
1657026c
AM
5394 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5395 && rel->r_offset % 2 == 0
8ffb6df2 5396 && sec->alignment_power != 0)
1657026c 5397 p->rel_count += 1;
65f38f15
AM
5398 }
5399 else
5400 {
3a3a4c1f 5401 /* Track dynamic relocs needed for local syms too. */
1657026c
AM
5402 struct ppc_local_dyn_relocs *p;
5403 struct ppc_local_dyn_relocs **head;
0a1b45a2 5404 bool is_ifunc;
ec338859 5405 asection *s;
6edfbbad 5406 void *vpp;
ec338859 5407
87d72d41
AM
5408 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5409 if (s == NULL)
5410 s = sec;
5411
6edfbbad 5412 vpp = &elf_section_data (s)->local_dynrel;
1657026c 5413 head = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
5414 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5415 p = *head;
5416 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5417 p = p->next;
5418 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5419 {
5420 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5421 if (p == NULL)
0a1b45a2 5422 return false;
19e08130
AM
5423 p->next = *head;
5424 *head = p;
5425 p->sec = sec;
19e08130 5426 p->count = 0;
1657026c
AM
5427 p->rel_count = 0;
5428 p->ifunc = is_ifunc;
19e08130
AM
5429 }
5430 p->count += 1;
1657026c
AM
5431 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5432 && rel->r_offset % 2 == 0
8ffb6df2 5433 && sec->alignment_power != 0)
1657026c 5434 p->rel_count += 1;
ec338859 5435 }
65f38f15 5436 }
5bd4f169 5437 break;
65f38f15
AM
5438
5439 default:
96e0dda4 5440 break;
5bd4f169
AM
5441 }
5442 }
5443
0a1b45a2 5444 return true;
5bd4f169
AM
5445}
5446
ee67d69a
AM
5447/* Merge backend specific data from an object file to the output
5448 object file when linking. */
5449
0a1b45a2 5450static bool
50e03d47 5451ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5452{
50e03d47 5453 bfd *obfd = info->output_bfd;
ee67d69a
AM
5454 unsigned long iflags, oflags;
5455
5456 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
0a1b45a2 5457 return true;
ee67d69a
AM
5458
5459 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
0a1b45a2 5460 return true;
ee67d69a 5461
50e03d47 5462 if (!_bfd_generic_verify_endian_match (ibfd, info))
0a1b45a2 5463 return false;
ee67d69a
AM
5464
5465 iflags = elf_elfheader (ibfd)->e_flags;
5466 oflags = elf_elfheader (obfd)->e_flags;
5467
f6c7c3e8 5468 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5469 {
4eca0228 5470 _bfd_error_handler
695344c0 5471 /* xgettext:c-format */
871b3ab2 5472 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a 5473 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5474 return false;
ee67d69a 5475 }
f6c7c3e8 5476 else if (iflags != oflags && iflags != 0)
ee67d69a 5477 {
4eca0228 5478 _bfd_error_handler
695344c0 5479 /* xgettext:c-format */
871b3ab2 5480 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5481 ibfd, iflags, oflags);
5482 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5483 return false;
ee67d69a
AM
5484 }
5485
4a91d0ba 5486 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
0a1b45a2 5487 return false;
005d79fd 5488
ee67d69a 5489 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5490 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5491}
5492
0a1b45a2 5493static bool
ee67d69a
AM
5494ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5495{
5496 /* Print normal ELF private data. */
5497 _bfd_elf_print_private_bfd_data (abfd, ptr);
5498
5499 if (elf_elfheader (abfd)->e_flags != 0)
5500 {
5501 FILE *file = ptr;
5502
ee67d69a
AM
5503 fprintf (file, _("private flags = 0x%lx:"),
5504 elf_elfheader (abfd)->e_flags);
5505
5506 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5507 fprintf (file, _(" [abiv%ld]"),
5508 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5509 fputc ('\n', file);
5510 }
5511
0a1b45a2 5512 return true;
ee67d69a
AM
5513}
5514
8387904d 5515/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5516 of the code entry point, and its section, which must be in the same
5517 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5518
5519static bfd_vma
5520opd_entry_value (asection *opd_sec,
5521 bfd_vma offset,
5522 asection **code_sec,
aef36ac1 5523 bfd_vma *code_off,
0a1b45a2 5524 bool in_code_sec)
8387904d
AM
5525{
5526 bfd *opd_bfd = opd_sec->owner;
8860955f 5527 Elf_Internal_Rela *relocs;
8387904d 5528 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5529 bfd_vma val;
8387904d 5530
9f296da3
AM
5531 /* No relocs implies we are linking a --just-symbols object, or looking
5532 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5533 if (opd_sec->reloc_count == 0)
5534 {
729eabd5 5535 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5536
729eabd5
AM
5537 if (contents == NULL)
5538 {
5539 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5540 return (bfd_vma) -1;
5541 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5542 }
ee1e4ede 5543
dbb3fbbb 5544 /* PR 17512: file: 64b9dfbb. */
451dfd38 5545 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5546 return (bfd_vma) -1;
5547
729eabd5 5548 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5549 if (code_sec != NULL)
5550 {
5551 asection *sec, *likely = NULL;
ee1e4ede 5552
aef36ac1 5553 if (in_code_sec)
4b85d634 5554 {
aef36ac1
AM
5555 sec = *code_sec;
5556 if (sec->vma <= val
5557 && val < sec->vma + sec->size)
5558 likely = sec;
5559 else
5560 val = -1;
5561 }
5562 else
5563 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5564 if (sec->vma <= val
5565 && (sec->flags & SEC_LOAD) != 0
5566 && (sec->flags & SEC_ALLOC) != 0)
5567 likely = sec;
5568 if (likely != NULL)
5569 {
5570 *code_sec = likely;
5571 if (code_off != NULL)
5572 *code_off = val - likely->vma;
4b85d634
AM
5573 }
5574 }
aef36ac1 5575 return val;
4b85d634
AM
5576 }
5577
0c8d6e5c 5578 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5579
729eabd5 5580 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f 5581 if (relocs == NULL)
0a1b45a2 5582 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
877a8638
NC
5583 /* PR 17512: file: df8e1fd6. */
5584 if (relocs == NULL)
5585 return (bfd_vma) -1;
645ea6a9 5586
8387904d 5587 /* Go find the opd reloc at the sym address. */
8860955f 5588 lo = relocs;
8387904d 5589 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5590 val = (bfd_vma) -1;
8387904d
AM
5591 while (lo < hi)
5592 {
5593 look = lo + (hi - lo) / 2;
5594 if (look->r_offset < offset)
5595 lo = look + 1;
5596 else if (look->r_offset > offset)
5597 hi = look;
5598 else
5599 {
0ffa91dd
NC
5600 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5601
8387904d
AM
5602 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5603 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5604 {
5605 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5606 asection *sec = NULL;
8387904d 5607
b53dfeb2
AM
5608 if (symndx >= symtab_hdr->sh_info
5609 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5610 {
5611 struct elf_link_hash_entry **sym_hashes;
5612 struct elf_link_hash_entry *rh;
5613
5614 sym_hashes = elf_sym_hashes (opd_bfd);
5615 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5616 if (rh != NULL)
5617 {
5618 rh = elf_follow_link (rh);
bb854a36
AM
5619 if (rh->root.type != bfd_link_hash_defined
5620 && rh->root.type != bfd_link_hash_defweak)
5621 break;
5622 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5623 {
bb854a36
AM
5624 val = rh->root.u.def.value;
5625 sec = rh->root.u.def.section;
b53dfeb2
AM
5626 }
5627 }
5628 }
5629
5630 if (sec == NULL)
5631 {
5632 Elf_Internal_Sym *sym;
5633
5634 if (symndx < symtab_hdr->sh_info)
5635 {
5636 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5637 if (sym == NULL)
5638 {
5639 size_t symcnt = symtab_hdr->sh_info;
5640 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5641 symcnt, 0,
5642 NULL, NULL, NULL);
5643 if (sym == NULL)
5644 break;
5645 symtab_hdr->contents = (bfd_byte *) sym;
5646 }
5647 sym += symndx;
128205bb
AM
5648 }
5649 else
5650 {
b53dfeb2
AM
5651 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5652 1, symndx,
5653 NULL, NULL, NULL);
128205bb
AM
5654 if (sym == NULL)
5655 break;
128205bb 5656 }
b53dfeb2
AM
5657 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5658 if (sec == NULL)
5659 break;
5660 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5661 val = sym->st_value;
8387904d 5662 }
b53dfeb2 5663
8387904d
AM
5664 val += look->r_addend;
5665 if (code_off != NULL)
5666 *code_off = val;
5667 if (code_sec != NULL)
aef36ac1
AM
5668 {
5669 if (in_code_sec && *code_sec != sec)
5670 return -1;
5671 else
5672 *code_sec = sec;
5673 }
b53dfeb2 5674 if (sec->output_section != NULL)
8387904d 5675 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5676 }
5677 break;
5678 }
5679 }
645ea6a9 5680
645ea6a9 5681 return val;
8387904d
AM
5682}
5683
aef36ac1
AM
5684/* If the ELF symbol SYM might be a function in SEC, return the
5685 function size and set *CODE_OFF to the function's entry point,
5686 otherwise return zero. */
9f296da3 5687
aef36ac1
AM
5688static bfd_size_type
5689ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5690 bfd_vma *code_off)
9f296da3 5691{
aef36ac1 5692 bfd_size_type size;
24aebc79 5693 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1
AM
5694
5695 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5696 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5697 return 0;
5698
24aebc79
NC
5699 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5700
5701 /* In theory we should check that the symbol's type satisfies
5702 _bfd_elf_is_function_type(), but there are some function-like
5703 symbols which would fail this test. (eg _start). Instead
5704 we check for hidden, local, notype symbols with zero size.
5705 This type of symbol is generated by the annobin plugin for gcc
5706 and clang, and should not be considered to be a function symbol. */
5707 if (size == 0
5708 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5709 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5710 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5711 return 0;
aef36ac1
AM
5712
5713 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5714 {
b07bca4e
AM
5715 struct _opd_sec_data *opd = get_opd_info (sym->section);
5716 bfd_vma symval = sym->value;
5717
5718 if (opd != NULL
5719 && opd->adjust != NULL
5720 && elf_section_data (sym->section)->relocs != NULL)
5721 {
5722 /* opd_entry_value will use cached relocs that have been
5723 adjusted, but with raw symbols. That means both local
5724 and global symbols need adjusting. */
5725 long adjust = opd->adjust[OPD_NDX (symval)];
5726 if (adjust == -1)
5727 return 0;
5728 symval += adjust;
5729 }
5730
5731 if (opd_entry_value (sym->section, symval,
0a1b45a2 5732 &sec, code_off, true) == (bfd_vma) -1)
aef36ac1
AM
5733 return 0;
5734 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5735 symbol. This size has nothing to do with the code size of the
5736 function, which is what we're supposed to return, but the
5737 code size isn't available without looking up the dot-sym.
5738 However, doing that would be a waste of time particularly
5739 since elf_find_function will look at the dot-sym anyway.
5740 Now, elf_find_function will keep the largest size of any
5741 function sym found at the code address of interest, so return
5742 1 here to avoid it incorrectly caching a larger function size
5743 for a small function. This does mean we return the wrong
5744 size for a new-ABI function of size 24, but all that does is
5745 disable caching for such functions. */
5746 if (size == 24)
5747 size = 1;
9f296da3 5748 }
aef36ac1
AM
5749 else
5750 {
5751 if (sym->section != sec)
5752 return 0;
5753 *code_off = sym->value;
5754 }
24aebc79
NC
5755
5756 /* Do not return 0 for the function's size. */
5757 return size ? size : 1;
9f296da3
AM
5758}
5759
f378ab09
AM
5760/* Return true if symbol is a strong function defined in an ELFv2
5761 object with st_other localentry bits of zero, ie. its local entry
5762 point coincides with its global entry point. */
5763
0a1b45a2 5764static bool
f378ab09
AM
5765is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5766{
5767 return (h != NULL
5768 && h->type == STT_FUNC
5769 && h->root.type == bfd_link_hash_defined
5770 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5771 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5772 && is_ppc64_elf (h->root.u.def.section->owner)
5773 && abiversion (h->root.u.def.section->owner) >= 2);
5774}
5775
854b41e7
AM
5776/* Return true if symbol is defined in a regular object file. */
5777
0a1b45a2 5778static bool
854b41e7
AM
5779is_static_defined (struct elf_link_hash_entry *h)
5780{
5781 return ((h->root.type == bfd_link_hash_defined
5782 || h->root.type == bfd_link_hash_defweak)
5783 && h->root.u.def.section != NULL
5784 && h->root.u.def.section->output_section != NULL);
5785}
5786
b31867b6
AM
5787/* If FDH is a function descriptor symbol, return the associated code
5788 entry symbol if it is defined. Return NULL otherwise. */
5789
5790static struct ppc_link_hash_entry *
5791defined_code_entry (struct ppc_link_hash_entry *fdh)
5792{
5793 if (fdh->is_func_descriptor)
5794 {
5795 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5796 if (fh->elf.root.type == bfd_link_hash_defined
5797 || fh->elf.root.type == bfd_link_hash_defweak)
5798 return fh;
5799 }
5800 return NULL;
5801}
5802
5803/* If FH is a function code entry symbol, return the associated
5804 function descriptor symbol if it is defined. Return NULL otherwise. */
5805
5806static struct ppc_link_hash_entry *
5807defined_func_desc (struct ppc_link_hash_entry *fh)
5808{
5809 if (fh->oh != NULL
5810 && fh->oh->is_func_descriptor)
5811 {
5812 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5813 if (fdh->elf.root.type == bfd_link_hash_defined
5814 || fdh->elf.root.type == bfd_link_hash_defweak)
5815 return fdh;
5816 }
5817 return NULL;
5818}
5819
ed7007c1
AM
5820/* Given H is a symbol that satisfies is_static_defined, return the
5821 value in the output file. */
5822
5823static bfd_vma
5824defined_sym_val (struct elf_link_hash_entry *h)
5825{
5826 return (h->root.u.def.section->output_section->vma
5827 + h->root.u.def.section->output_offset
5828 + h->root.u.def.value);
5829}
5830
5831/* Return true if H matches __tls_get_addr or one of its variants. */
5832
0a1b45a2 5833static bool
ed7007c1
AM
5834is_tls_get_addr (struct elf_link_hash_entry *h,
5835 struct ppc_link_hash_table *htab)
5836{
30845f11
AM
5837 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5838 || h == elf_hash_entry (htab->tga_desc_fd)
5839 || h == elf_hash_entry (htab->tls_get_addr)
5840 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5841}
5842
0a1b45a2 5843static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
8c5b4e52
AM
5844
5845/* Garbage collect sections, after first dealing with dot-symbols. */
5846
0a1b45a2 5847static bool
8c5b4e52
AM
5848ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5849{
5850 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5851
5852 if (htab != NULL && htab->need_func_desc_adj)
5853 {
5854 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5855 htab->need_func_desc_adj = 0;
5856 }
5857 return bfd_elf_gc_sections (abfd, info);
5858}
5859
74f0fb50
AM
5860/* Mark all our entry sym sections, both opd and code section. */
5861
5862static void
5863ppc64_elf_gc_keep (struct bfd_link_info *info)
5864{
5865 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5866 struct bfd_sym_chain *sym;
5867
4dfe6ac6
NC
5868 if (htab == NULL)
5869 return;
5870
74f0fb50
AM
5871 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5872 {
b31867b6 5873 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5874 asection *sec;
5875
ed7007c1 5876 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
0a1b45a2 5877 false, false, true));
74f0fb50
AM
5878 if (eh == NULL)
5879 continue;
5880 if (eh->elf.root.type != bfd_link_hash_defined
5881 && eh->elf.root.type != bfd_link_hash_defweak)
5882 continue;
5883
b31867b6
AM
5884 fh = defined_code_entry (eh);
5885 if (fh != NULL)
74f0fb50 5886 {
b31867b6 5887 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5888 sec->flags |= SEC_KEEP;
5889 }
5890 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5891 && opd_entry_value (eh->elf.root.u.def.section,
5892 eh->elf.root.u.def.value,
0a1b45a2 5893 &sec, NULL, false) != (bfd_vma) -1)
74f0fb50
AM
5894 sec->flags |= SEC_KEEP;
5895
5896 sec = eh->elf.root.u.def.section;
5897 sec->flags |= SEC_KEEP;
5898 }
5899}
5900
64d03ab5
AM
5901/* Mark sections containing dynamically referenced symbols. When
5902 building shared libraries, we must assume that any visible symbol is
5903 referenced. */
5904
0a1b45a2 5905static bool
64d03ab5
AM
5906ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5907{
5908 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5909 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5910 struct ppc_link_hash_entry *fdh;
b407645f 5911 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5912
64d03ab5 5913 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5914 fdh = defined_func_desc (eh);
5915 if (fdh != NULL)
5916 eh = fdh;
64d03ab5
AM
5917
5918 if ((eh->elf.root.type == bfd_link_hash_defined
5919 || eh->elf.root.type == bfd_link_hash_defweak)
b80e421f
AM
5920 && (!eh->elf.start_stop
5921 || eh->elf.root.ldscript_def
5922 || !info->start_stop_gc)
87e79a65 5923 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5924 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5925 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5926 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5927 && (!bfd_link_executable (info)
e278ae05 5928 || info->gc_keep_exported
b407645f
AM
5929 || info->export_dynamic
5930 || (eh->elf.dynamic
5931 && d != NULL
2cdcc330
AM
5932 && (*d->match) (&d->head, NULL,
5933 eh->elf.root.root.string)))
e278ae05 5934 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5935 || !bfd_hide_sym_by_version (info->version_info,
5936 eh->elf.root.root.string)))))
64d03ab5
AM
5937 {
5938 asection *code_sec;
b31867b6 5939 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5940
5941 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5942
5943 /* Function descriptor syms cause the associated
5944 function code sym section to be marked. */
b31867b6
AM
5945 fh = defined_code_entry (eh);
5946 if (fh != NULL)
5947 {
5948 code_sec = fh->elf.root.u.def.section;
5949 code_sec->flags |= SEC_KEEP;
5950 }
64d03ab5
AM
5951 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5952 && opd_entry_value (eh->elf.root.u.def.section,
5953 eh->elf.root.u.def.value,
0a1b45a2 5954 &code_sec, NULL, false) != (bfd_vma) -1)
64d03ab5
AM
5955 code_sec->flags |= SEC_KEEP;
5956 }
5957
0a1b45a2 5958 return true;
64d03ab5
AM
5959}
5960
5bd4f169
AM
5961/* Return the section that should be marked against GC for a given
5962 relocation. */
5963
5964static asection *
4ce794b7 5965ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5966 struct bfd_link_info *info,
4ce794b7
AM
5967 Elf_Internal_Rela *rel,
5968 struct elf_link_hash_entry *h,
5969 Elf_Internal_Sym *sym)
5bd4f169 5970{
ccfa59ea
AM
5971 asection *rsec;
5972
ccfa59ea
AM
5973 /* Syms return NULL if we're marking .opd, so we avoid marking all
5974 function sections, as all functions are referenced in .opd. */
5975 rsec = NULL;
5976 if (get_opd_info (sec) != NULL)
5977 return rsec;
1e2f5b6e 5978
5bd4f169
AM
5979 if (h != NULL)
5980 {
04c9666a 5981 enum elf_ppc64_reloc_type r_type;
b31867b6 5982 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5983
4ce794b7 5984 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5985 switch (r_type)
5bd4f169
AM
5986 {
5987 case R_PPC64_GNU_VTINHERIT:
5988 case R_PPC64_GNU_VTENTRY:
5989 break;
5990
5991 default:
5992 switch (h->root.type)
5993 {
5994 case bfd_link_hash_defined:
5995 case bfd_link_hash_defweak:
ed7007c1 5996 eh = ppc_elf_hash_entry (h);
b31867b6
AM
5997 fdh = defined_func_desc (eh);
5998 if (fdh != NULL)
8c5b4e52
AM
5999 {
6000 /* -mcall-aixdesc code references the dot-symbol on
6001 a call reloc. Mark the function descriptor too
6002 against garbage collection. */
6003 fdh->elf.mark = 1;
60d67dc8
AM
6004 if (fdh->elf.is_weakalias)
6005 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6006 eh = fdh;
6007 }
1e2f5b6e
AM
6008
6009 /* Function descriptor syms cause the associated
6010 function code sym section to be marked. */
b31867b6
AM
6011 fh = defined_code_entry (eh);
6012 if (fh != NULL)
ccfa59ea
AM
6013 {
6014 /* They also mark their opd section. */
74f0fb50 6015 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6016
b31867b6 6017 rsec = fh->elf.root.u.def.section;
ccfa59ea 6018 }
8387904d
AM
6019 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6020 && opd_entry_value (eh->elf.root.u.def.section,
6021 eh->elf.root.u.def.value,
0a1b45a2 6022 &rsec, NULL, false) != (bfd_vma) -1)
74f0fb50 6023 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6024 else
1e2f5b6e
AM
6025 rsec = h->root.u.def.section;
6026 break;
5bd4f169
AM
6027
6028 case bfd_link_hash_common:
1e2f5b6e
AM
6029 rsec = h->root.u.c.p->section;
6030 break;
5bd4f169
AM
6031
6032 default:
fb34365b 6033 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6034 }
6035 }
6036 }
6037 else
6038 {
74f0fb50 6039 struct _opd_sec_data *opd;
1e2f5b6e
AM
6040
6041 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6042 opd = get_opd_info (rsec);
6043 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6044 {
74f0fb50 6045 rsec->gc_mark = 1;
ccfa59ea 6046
51aecdc5 6047 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6048 }
5bd4f169
AM
6049 }
6050
1e2f5b6e 6051 return rsec;
5bd4f169
AM
6052}
6053
deb0e272
AM
6054/* The maximum size of .sfpr. */
6055#define SFPR_MAX (218*4)
6056
6057struct sfpr_def_parms
6058{
699733f6
AM
6059 const char name[12];
6060 unsigned char lo, hi;
2cdcc330
AM
6061 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6062 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
6063};
6064
a4b6fadd
AM
6065/* Auto-generate _save*, _rest* functions in .sfpr.
6066 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6067 instead. */
deb0e272 6068
0a1b45a2 6069static bool
a4b6fadd
AM
6070sfpr_define (struct bfd_link_info *info,
6071 const struct sfpr_def_parms *parm,
6072 asection *stub_sec)
deb0e272
AM
6073{
6074 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6075 unsigned int i;
6076 size_t len = strlen (parm->name);
0a1b45a2 6077 bool writing = false;
699733f6 6078 char sym[16];
deb0e272 6079
4dfe6ac6 6080 if (htab == NULL)
0a1b45a2 6081 return false;
4dfe6ac6 6082
deb0e272
AM
6083 memcpy (sym, parm->name, len);
6084 sym[len + 2] = 0;
6085
6086 for (i = parm->lo; i <= parm->hi; i++)
6087 {
a4b6fadd 6088 struct ppc_link_hash_entry *h;
deb0e272
AM
6089
6090 sym[len + 0] = i / 10 + '0';
6091 sym[len + 1] = i % 10 + '0';
ed7007c1 6092 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
0a1b45a2 6093 writing, true, true));
a4b6fadd 6094 if (stub_sec != NULL)
deb0e272 6095 {
a4b6fadd
AM
6096 if (h != NULL
6097 && h->elf.root.type == bfd_link_hash_defined
6098 && h->elf.root.u.def.section == htab->sfpr)
6099 {
6100 struct elf_link_hash_entry *s;
6101 char buf[32];
6102 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
0a1b45a2 6103 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
a4b6fadd 6104 if (s == NULL)
0a1b45a2 6105 return false;
779f2ae7 6106 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
6107 {
6108 s->root.type = bfd_link_hash_defined;
6109 s->root.u.def.section = stub_sec;
7dda8d3c 6110 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6111 + h->elf.root.u.def.value);
6112 s->ref_regular = 1;
6113 s->def_regular = 1;
6114 s->ref_regular_nonweak = 1;
6115 s->forced_local = 1;
6116 s->non_elf = 0;
6117 s->root.linker_def = 1;
6118 }
6119 }
6120 continue;
6121 }
6122 if (h != NULL)
6123 {
6124 h->save_res = 1;
6125 if (!h->elf.def_regular)
deb0e272 6126 {
a4b6fadd
AM
6127 h->elf.root.type = bfd_link_hash_defined;
6128 h->elf.root.u.def.section = htab->sfpr;
6129 h->elf.root.u.def.value = htab->sfpr->size;
6130 h->elf.type = STT_FUNC;
6131 h->elf.def_regular = 1;
b32547cd 6132 h->elf.non_elf = 0;
0a1b45a2
AM
6133 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6134 writing = true;
deb0e272 6135 if (htab->sfpr->contents == NULL)
a4b6fadd 6136 {
2cdcc330
AM
6137 htab->sfpr->contents
6138 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd 6139 if (htab->sfpr->contents == NULL)
0a1b45a2 6140 return false;
a4b6fadd 6141 }
deb0e272
AM
6142 }
6143 }
6144 if (writing)
6145 {
6146 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6147 if (i != parm->hi)
6148 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6149 else
6150 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6151 htab->sfpr->size = p - htab->sfpr->contents;
6152 }
6153 }
6154
0a1b45a2 6155 return true;
deb0e272
AM
6156}
6157
6158static bfd_byte *
6159savegpr0 (bfd *abfd, bfd_byte *p, int r)
6160{
6161 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6162 return p + 4;
6163}
6164
6165static bfd_byte *
6166savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6167{
6168 p = savegpr0 (abfd, p, r);
a078d95a 6169 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6170 p = p + 4;
6171 bfd_put_32 (abfd, BLR, p);
6172 return p + 4;
6173}
6174
6175static bfd_byte *
6176restgpr0 (bfd *abfd, bfd_byte *p, int r)
6177{
6178 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6179 return p + 4;
6180}
6181
6182static bfd_byte *
6183restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6184{
a078d95a 6185 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6186 p = p + 4;
6187 p = restgpr0 (abfd, p, r);
6188 bfd_put_32 (abfd, MTLR_R0, p);
6189 p = p + 4;
6190 if (r == 29)
6191 {
6192 p = restgpr0 (abfd, p, 30);
6193 p = restgpr0 (abfd, p, 31);
6194 }
6195 bfd_put_32 (abfd, BLR, p);
6196 return p + 4;
6197}
6198
6199static bfd_byte *
6200savegpr1 (bfd *abfd, bfd_byte *p, int r)
6201{
6202 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6203 return p + 4;
6204}
6205
6206static bfd_byte *
6207savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6208{
6209 p = savegpr1 (abfd, p, r);
6210 bfd_put_32 (abfd, BLR, p);
6211 return p + 4;
6212}
6213
6214static bfd_byte *
6215restgpr1 (bfd *abfd, bfd_byte *p, int r)
6216{
6217 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6218 return p + 4;
6219}
6220
6221static bfd_byte *
6222restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6223{
6224 p = restgpr1 (abfd, p, r);
6225 bfd_put_32 (abfd, BLR, p);
6226 return p + 4;
6227}
6228
6229static bfd_byte *
6230savefpr (bfd *abfd, bfd_byte *p, int r)
6231{
6232 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6233 return p + 4;
6234}
6235
6236static bfd_byte *
6237savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6238{
6239 p = savefpr (abfd, p, r);
a078d95a 6240 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6241 p = p + 4;
6242 bfd_put_32 (abfd, BLR, p);
6243 return p + 4;
6244}
6245
6246static bfd_byte *
6247restfpr (bfd *abfd, bfd_byte *p, int r)
6248{
6249 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6250 return p + 4;
6251}
6252
6253static bfd_byte *
6254restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6255{
a078d95a 6256 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6257 p = p + 4;
6258 p = restfpr (abfd, p, r);
6259 bfd_put_32 (abfd, MTLR_R0, p);
6260 p = p + 4;
6261 if (r == 29)
6262 {
6263 p = restfpr (abfd, p, 30);
6264 p = restfpr (abfd, p, 31);
6265 }
6266 bfd_put_32 (abfd, BLR, p);
6267 return p + 4;
6268}
6269
6270static bfd_byte *
6271savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6272{
6273 p = savefpr (abfd, p, r);
6274 bfd_put_32 (abfd, BLR, p);
6275 return p + 4;
6276}
6277
6278static bfd_byte *
6279restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6280{
6281 p = restfpr (abfd, p, r);
6282 bfd_put_32 (abfd, BLR, p);
6283 return p + 4;
6284}
6285
6286static bfd_byte *
6287savevr (bfd *abfd, bfd_byte *p, int r)
6288{
6289 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6290 p = p + 4;
6291 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6292 return p + 4;
6293}
6294
6295static bfd_byte *
6296savevr_tail (bfd *abfd, bfd_byte *p, int r)
6297{
6298 p = savevr (abfd, p, r);
6299 bfd_put_32 (abfd, BLR, p);
6300 return p + 4;
6301}
6302
6303static bfd_byte *
6304restvr (bfd *abfd, bfd_byte *p, int r)
6305{
6306 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6307 p = p + 4;
6308 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6309 return p + 4;
6310}
6311
6312static bfd_byte *
6313restvr_tail (bfd *abfd, bfd_byte *p, int r)
6314{
6315 p = restvr (abfd, p, r);
6316 bfd_put_32 (abfd, BLR, p);
6317 return p + 4;
6318}
6319
9e7028aa
AM
6320#define STDU_R1_0R1 0xf8210001
6321#define ADDI_R1_R1 0x38210000
6322
6323/* Emit prologue of wrapper preserving regs around a call to
6324 __tls_get_addr_opt. */
6325
6326static bfd_byte *
6327tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6328{
6329 unsigned int i;
6330
6331 bfd_put_32 (obfd, MFLR_R0, p);
6332 p += 4;
6333 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6334 p += 4;
6335
6336 if (htab->opd_abi)
6337 {
6338 for (i = 4; i < 12; i++)
6339 {
6340 bfd_put_32 (obfd,
6341 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6342 p += 4;
6343 }
6344 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6345 p += 4;
6346 }
6347 else
6348 {
6349 for (i = 4; i < 12; i++)
6350 {
6351 bfd_put_32 (obfd,
6352 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6353 p += 4;
6354 }
6355 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6356 p += 4;
6357 }
6358 return p;
6359}
6360
6361/* Emit epilogue of wrapper preserving regs around a call to
6362 __tls_get_addr_opt. */
6363
6364static bfd_byte *
6365tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6366{
6367 unsigned int i;
6368
6369 if (htab->opd_abi)
6370 {
6371 for (i = 4; i < 12; i++)
6372 {
6373 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6374 p += 4;
6375 }
6376 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6377 p += 4;
6378 }
6379 else
6380 {
6381 for (i = 4; i < 12; i++)
6382 {
6383 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6384 p += 4;
6385 }
6386 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6387 p += 4;
6388 }
6389 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6390 p += 4;
6391 bfd_put_32 (obfd, MTLR_R0, p);
6392 p += 4;
6393 bfd_put_32 (obfd, BLR, p);
6394 p += 4;
6395 return p;
6396}
6397
e86ce104
AM
6398/* Called via elf_link_hash_traverse to transfer dynamic linking
6399 information on function code symbol entries to their corresponding
a442059f
AM
6400 function descriptor symbol entries. Must not be called twice for
6401 any given code symbol. */
deb0e272 6402
0a1b45a2 6403static bool
4ce794b7 6404func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6405{
e86ce104 6406 struct bfd_link_info *info;
65f38f15 6407 struct ppc_link_hash_table *htab;
50bc7936
AM
6408 struct ppc_link_hash_entry *fh;
6409 struct ppc_link_hash_entry *fdh;
0a1b45a2 6410 bool force_local;
5bd4f169 6411
ed7007c1 6412 fh = ppc_elf_hash_entry (h);
50bc7936 6413 if (fh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 6414 return true;
e86ce104 6415
8c5b4e52 6416 if (!fh->is_func)
0a1b45a2 6417 return true;
8c5b4e52
AM
6418
6419 if (fh->elf.root.root.string[0] != '.'
6420 || fh->elf.root.root.string[1] == '\0')
0a1b45a2 6421 return true;
8c5b4e52 6422
4ce794b7 6423 info = inf;
65f38f15 6424 htab = ppc_hash_table (info);
4dfe6ac6 6425 if (htab == NULL)
0a1b45a2 6426 return false;
5bd4f169 6427
8c5b4e52
AM
6428 /* Find the corresponding function descriptor symbol. */
6429 fdh = lookup_fdh (fh, htab);
6430
c09bdfe5
AM
6431 /* Resolve undefined references to dot-symbols as the value
6432 in the function descriptor, if we have one in a regular object.
6433 This is to satisfy cases like ".quad .foo". Calls to functions
6434 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6435 if ((fh->elf.root.type == bfd_link_hash_undefined
6436 || fh->elf.root.type == bfd_link_hash_undefweak)
6437 && (fdh->elf.root.type == bfd_link_hash_defined
6438 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6439 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6440 && opd_entry_value (fdh->elf.root.u.def.section,
6441 fdh->elf.root.u.def.value,
c09bdfe5 6442 &fh->elf.root.u.def.section,
0a1b45a2 6443 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
c09bdfe5 6444 {
b31867b6 6445 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6446 fh->elf.forced_local = 1;
b31867b6
AM
6447 fh->elf.def_regular = fdh->elf.def_regular;
6448 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6449 }
6450
8c5b4e52
AM
6451 if (!fh->elf.dynamic)
6452 {
6453 struct plt_entry *ent;
5bd4f169 6454
8c5b4e52
AM
6455 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6456 if (ent->plt.refcount > 0)
6457 break;
6458 if (ent == NULL)
a442059f
AM
6459 {
6460 if (fdh != NULL && fdh->fake)
6461 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6462 return true;
6463 }
8c5b4e52 6464 }
5bd4f169 6465
8c5b4e52 6466 /* Create a descriptor as undefined if necessary. */
50bc7936 6467 if (fdh == NULL
0e1862bb 6468 && !bfd_link_executable (info)
50bc7936
AM
6469 && (fh->elf.root.type == bfd_link_hash_undefined
6470 || fh->elf.root.type == bfd_link_hash_undefweak))
6471 {
908b32fc 6472 fdh = make_fdh (info, fh);
bb700d78 6473 if (fdh == NULL)
0a1b45a2 6474 return false;
50bc7936 6475 }
648cca2c 6476
8c5b4e52 6477 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6478 if (fdh != NULL
6479 && fdh->fake
8c5b4e52
AM
6480 && (fh->elf.root.type == bfd_link_hash_defined
6481 || fh->elf.root.type == bfd_link_hash_defweak))
0a1b45a2 6482 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
908b32fc 6483
8c5b4e52
AM
6484 /* Transfer dynamic linking information to the function descriptor. */
6485 if (fdh != NULL)
6486 {
f5385ebf
AM
6487 fdh->elf.ref_regular |= fh->elf.ref_regular;
6488 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6489 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6490 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6491 fdh->elf.dynamic |= fh->elf.dynamic;
6492 fdh->elf.needs_plt |= (fh->elf.needs_plt
6493 || fh->elf.type == STT_FUNC
6494 || fh->elf.type == STT_GNU_IFUNC);
6495 move_plt_plist (fh, fdh);
6496
6497 if (!fdh->elf.forced_local
6498 && fh->elf.dynindx != -1)
6499 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 6500 return false;
e86ce104
AM
6501 }
6502
50bc7936
AM
6503 /* Now that the info is on the function descriptor, clear the
6504 function code sym info. Any function code syms for which we
6505 don't have a definition in a regular file, we force local.
6506 This prevents a shared library from exporting syms that have
6507 been imported from another library. Function code syms that
6508 are really in the library we must leave global to prevent the
6509 linker dragging in a definition from a static library. */
93f3fa99
AM
6510 force_local = (!fh->elf.def_regular
6511 || fdh == NULL
6512 || !fdh->elf.def_regular
6513 || fdh->elf.forced_local);
50bc7936
AM
6514 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6515
0a1b45a2 6516 return true;
e86ce104 6517}
40b8271b 6518
a4b6fadd
AM
6519static const struct sfpr_def_parms save_res_funcs[] =
6520 {
6521 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6522 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6523 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6524 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6525 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6526 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6527 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6528 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6529 { "._savef", 14, 31, savefpr, savefpr1_tail },
6530 { "._restf", 14, 31, restfpr, restfpr1_tail },
6531 { "_savevr_", 20, 31, savevr, savevr_tail },
6532 { "_restvr_", 20, 31, restvr, restvr_tail }
6533 };
6534
e86ce104 6535/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6536 this hook to a) run the edit functions in this file, b) provide
6537 some gcc support functions, and c) transfer dynamic linking
6538 information gathered so far on function code symbol entries, to
6539 their corresponding function descriptor symbol entries. */
deb0e272 6540
0a1b45a2 6541static bool
c9405344 6542ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6543{
6544 struct ppc_link_hash_table *htab;
6545
6546 htab = ppc_hash_table (info);
4dfe6ac6 6547 if (htab == NULL)
0a1b45a2 6548 return false;
4dfe6ac6 6549
c9405344
AM
6550 /* Call back into the linker, which then runs the edit functions. */
6551 htab->params->edit ();
6552
b32547cd
AM
6553 /* Provide any missing _save* and _rest* functions. */
6554 if (htab->sfpr != NULL)
6555 {
6556 unsigned int i;
6557
6558 htab->sfpr->size = 0;
6559 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6560 if (!sfpr_define (info, &save_res_funcs[i], NULL))
0a1b45a2 6561 return false;
b32547cd
AM
6562 if (htab->sfpr->size == 0)
6563 htab->sfpr->flags |= SEC_EXCLUDE;
6564 }
6565
6566 if (bfd_link_relocatable (info))
0a1b45a2 6567 return true;
b32547cd
AM
6568
6569 if (htab->elf.hgot != NULL)
dba6fa9b 6570 {
0a1b45a2 6571 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
dba6fa9b
AM
6572 /* Make .TOC. defined so as to prevent it being made dynamic.
6573 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6574 if (!htab->elf.hgot->def_regular
6575 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6576 {
6577 htab->elf.hgot->root.type = bfd_link_hash_defined;
6578 htab->elf.hgot->root.u.def.value = 0;
6579 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6580 htab->elf.hgot->def_regular = 1;
6581 htab->elf.hgot->root.linker_def = 1;
6582 }
dba6fa9b 6583 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6584 htab->elf.hgot->other
6585 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6586 }
c66bb0ee 6587
0a1b45a2 6588 return true;
e86ce104
AM
6589}
6590
d311bc8b 6591/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6592 aliases, that apply to read-only sections. Cannot be used after
6593 size_dynamic_sections. */
d311bc8b 6594
0a1b45a2 6595static bool
d311bc8b
AM
6596alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6597{
ed7007c1 6598 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6599 do
6600 {
5dbc8b37 6601 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
0a1b45a2 6602 return true;
ed7007c1 6603 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6604 }
6605 while (eh != NULL && &eh->elf != h);
d311bc8b 6606
0a1b45a2 6607 return false;
d311bc8b 6608}
8a2058b5 6609
8a9e8e72
AM
6610/* Return whether EH has pc-relative dynamic relocs. */
6611
0a1b45a2 6612static bool
8a9e8e72
AM
6613pc_dynrelocs (struct ppc_link_hash_entry *eh)
6614{
1657026c 6615 struct ppc_dyn_relocs *p;
8a9e8e72 6616
1657026c 6617 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72 6618 if (p->pc_count != 0)
0a1b45a2
AM
6619 return true;
6620 return false;
8a9e8e72
AM
6621}
6622
8a2058b5
AM
6623/* Return true if a global entry stub will be created for H. Valid
6624 for ELFv2 before plt entries have been allocated. */
6625
0a1b45a2 6626static bool
8a2058b5
AM
6627global_entry_stub (struct elf_link_hash_entry *h)
6628{
6629 struct plt_entry *pent;
6630
6631 if (!h->pointer_equality_needed
6632 || h->def_regular)
0a1b45a2 6633 return false;
8a2058b5
AM
6634
6635 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6636 if (pent->plt.refcount > 0
6637 && pent->addend == 0)
0a1b45a2 6638 return true;
8a2058b5 6639
0a1b45a2 6640 return false;
8a2058b5
AM
6641}
6642
e86ce104
AM
6643/* Adjust a symbol defined by a dynamic object and referenced by a
6644 regular object. The current definition is in some section of the
6645 dynamic object, but we're not including those sections. We have to
6646 change the definition to something the rest of the link can
6647 understand. */
6648
0a1b45a2 6649static bool
4ce794b7
AM
6650ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6651 struct elf_link_hash_entry *h)
e86ce104
AM
6652{
6653 struct ppc_link_hash_table *htab;
5474d94f 6654 asection *s, *srel;
e86ce104
AM
6655
6656 htab = ppc_hash_table (info);
4dfe6ac6 6657 if (htab == NULL)
0a1b45a2 6658 return false;
e86ce104
AM
6659
6660 /* Deal with function syms. */
6661 if (h->type == STT_FUNC
e054468f 6662 || h->type == STT_GNU_IFUNC
f5385ebf 6663 || h->needs_plt)
e86ce104 6664 {
0a1b45a2
AM
6665 bool local = (ppc_elf_hash_entry (h)->save_res
6666 || SYMBOL_CALLS_LOCAL (info, h)
6667 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
529fe20e
AM
6668 /* Discard dyn_relocs when non-pic if we've decided that a
6669 function symbol is local and not an ifunc. We keep dynamic
6670 relocs for ifuncs when local rather than always emitting a
6671 plt call stub for them and defining the symbol on the call
6672 stub. We can't do that for ELFv1 anyway (a function symbol
6673 is defined on a descriptor, not code) and it can be faster at
6674 run-time due to not needing to bounce through a stub. The
6675 dyn_relocs for ifuncs will be applied even in a static
6676 executable. */
6677 if (!bfd_link_pic (info)
6678 && h->type != STT_GNU_IFUNC
6679 && local)
190eb1dd 6680 h->dyn_relocs = NULL;
529fe20e 6681
e86ce104
AM
6682 /* Clear procedure linkage table information for any symbol that
6683 won't need a .plt entry. */
411e1bfb
AM
6684 struct plt_entry *ent;
6685 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6686 if (ent->plt.refcount > 0)
6687 break;
8387904d 6688 if (ent == NULL
2d7ad24e
AM
6689 || (h->type != STT_GNU_IFUNC
6690 && local
3e04d765 6691 && (htab->can_convert_all_inline_plt
ed7007c1 6692 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6693 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6694 {
411e1bfb 6695 h->plt.plist = NULL;
f5385ebf 6696 h->needs_plt = 0;
d1eca1e4 6697 h->pointer_equality_needed = 0;
40b8271b 6698 }
8a2058b5 6699 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6700 {
d1eca1e4
AM
6701 /* Taking a function's address in a read/write section
6702 doesn't require us to define the function symbol in the
6703 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6704 be used instead. The reason we prefer a few more dynamic
6705 relocs is that calling via a global entry stub costs a
6706 few more instructions, and pointer_equality_needed causes
6707 extra work in ld.so when resolving these symbols. */
529fe20e 6708 if (global_entry_stub (h))
d1eca1e4 6709 {
5dbc8b37 6710 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6711 {
6712 h->pointer_equality_needed = 0;
04383fd1
AM
6713 /* If we haven't seen a branch reloc and the symbol
6714 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6715 if (!h->needs_plt)
6716 h->plt.plist = NULL;
6717 }
6718 else if (!bfd_link_pic (info))
6719 /* We are going to be defining the function symbol on the
6720 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6721 h->dyn_relocs = NULL;
d1eca1e4
AM
6722 }
6723
3988aed5 6724 /* ELFv2 function symbols can't have copy relocs. */
0a1b45a2 6725 return true;
3988aed5
AM
6726 }
6727 else if (!h->needs_plt
5dbc8b37 6728 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6729 {
04383fd1
AM
6730 /* If we haven't seen a branch reloc and the symbol isn't an
6731 ifunc then we don't need a plt entry. */
3988aed5
AM
6732 h->plt.plist = NULL;
6733 h->pointer_equality_needed = 0;
0a1b45a2 6734 return true;
a345bc8d 6735 }
5bd4f169 6736 }
bbd7ec4a 6737 else
411e1bfb 6738 h->plt.plist = NULL;
5bd4f169
AM
6739
6740 /* If this is a weak symbol, and there is a real definition, the
6741 processor independent code will have arranged for us to see the
6742 real definition first, and we can just use the same value. */
60d67dc8 6743 if (h->is_weakalias)
5bd4f169 6744 {
60d67dc8
AM
6745 struct elf_link_hash_entry *def = weakdef (h);
6746 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6747 h->root.u.def.section = def->root.u.def.section;
6748 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6749 if (def->root.u.def.section == htab->elf.sdynbss
6750 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6751 h->dyn_relocs = NULL;
0a1b45a2 6752 return true;
5bd4f169
AM
6753 }
6754
5bd4f169
AM
6755 /* If we are creating a shared library, we must presume that the
6756 only references to the symbol are via the global offset table.
6757 For such cases we need not do anything here; the relocations will
6758 be handled correctly by relocate_section. */
ec73ddcd 6759 if (!bfd_link_executable (info))
0a1b45a2 6760 return true;
5bd4f169 6761
65f38f15
AM
6762 /* If there are no references to this symbol that do not use the
6763 GOT, we don't need to generate a copy reloc. */
f5385ebf 6764 if (!h->non_got_ref)
0a1b45a2 6765 return true;
65f38f15 6766
b186458a 6767 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6768 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6769
d93d1c80
AM
6770 /* If -z nocopyreloc was given, don't generate them either. */
6771 || info->nocopyreloc
a127494f 6772
dce2246a 6773 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6774 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6775 || (ELIMINATE_COPY_RELOCS
6776 && !h->needs_copy
6777 && !alias_readonly_dynrelocs (h))
65f38f15 6778
d93d1c80
AM
6779 /* Protected variables do not work with .dynbss. The copy in
6780 .dynbss won't be used by the shared library with the protected
6781 definition for the variable. Text relocations are preferable
6782 to an incorrect program. */
6783 || h->protected_def)
0a1b45a2 6784 return true;
a127494f 6785
e1c6cf61
AM
6786 if (h->type == STT_FUNC
6787 || h->type == STT_GNU_IFUNC)
97b639ba 6788 {
e1c6cf61
AM
6789 /* .dynbss copies of function symbols only work if we have
6790 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6791 use dot-symbols and set the function symbol size to the text
6792 size of the function rather than the size of the descriptor.
6793 That's wrong for copying a descriptor. */
ed7007c1 6794 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61 6795 || !(h->size == 24 || h->size == 16))
0a1b45a2 6796 return true;
e1c6cf61
AM
6797
6798 /* We should never get here, but unfortunately there are old
6799 versions of gcc (circa gcc-3.2) that improperly for the
6800 ELFv1 ABI put initialized function pointers, vtable refs and
6801 suchlike in read-only sections. Allow them to proceed, but
6802 warn that this might break at runtime. */
25f53a85 6803 info->callbacks->einfo
c1c8c1ef 6804 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6805 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6806 h->root.root.string);
6807 }
5d35169e
AM
6808
6809 /* This is a reference to a symbol defined by a dynamic object which
6810 is not a function. */
6811
5bd4f169
AM
6812 /* We must allocate the symbol in our .dynbss section, which will
6813 become part of the .bss section of the executable. There will be
6814 an entry for this symbol in the .dynsym section. The dynamic
6815 object will contain position independent code, so all references
6816 from the dynamic object to this symbol will go through the global
6817 offset table. The dynamic linker will use the .dynsym entry to
6818 determine the address it must put in the global offset table, so
6819 both the dynamic object and the regular object will refer to the
6820 same memory location for the variable. */
5474d94f
AM
6821 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6822 {
6823 s = htab->elf.sdynrelro;
6824 srel = htab->elf.sreldynrelro;
6825 }
6826 else
6827 {
6828 s = htab->elf.sdynbss;
6829 srel = htab->elf.srelbss;
6830 }
1d7e9d18 6831 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6832 {
4a7e5234
AM
6833 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6834 linker to copy the initial value out of the dynamic object
6835 and into the runtime process image. */
5474d94f 6836 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6837 h->needs_copy = 1;
5bd4f169
AM
6838 }
6839
529fe20e 6840 /* We no longer want dyn_relocs. */
190eb1dd 6841 h->dyn_relocs = NULL;
6cabe1ea 6842 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6843}
6844
e86ce104
AM
6845/* If given a function descriptor symbol, hide both the function code
6846 sym and the descriptor. */
6847static void
4ce794b7
AM
6848ppc64_elf_hide_symbol (struct bfd_link_info *info,
6849 struct elf_link_hash_entry *h,
0a1b45a2 6850 bool force_local)
e86ce104 6851{
34814b9f 6852 struct ppc_link_hash_entry *eh;
e86ce104
AM
6853 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6854
87469ba2
AM
6855 if (ppc_hash_table (info) == NULL)
6856 return;
6857
ed7007c1 6858 eh = ppc_elf_hash_entry (h);
34814b9f 6859 if (eh->is_func_descriptor)
e86ce104 6860 {
34814b9f 6861 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6862
721956f4 6863 if (fh == NULL)
d1329ca3
AM
6864 {
6865 const char *p, *q;
b8ac2841 6866 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6867 char save;
6868
6869 /* We aren't supposed to use alloca in BFD because on
6870 systems which do not have alloca the version in libiberty
6871 calls xmalloc, which might cause the program to crash
6872 when it runs out of memory. This function doesn't have a
6873 return status, so there's no way to gracefully return an
6874 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6875 accessed; It's either a string in an ELF string table,
6876 or allocated in an objalloc structure. */
d1329ca3 6877
34814b9f 6878 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6879 save = *p;
6880 *(char *) p = '.';
0a1b45a2
AM
6881 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6882 false, false));
d1329ca3
AM
6883 *(char *) p = save;
6884
6885 /* Unfortunately, if it so happens that the string we were
6886 looking for was allocated immediately before this string,
6887 then we overwrote the string terminator. That's the only
6888 reason the lookup should fail. */
6889 if (fh == NULL)
6890 {
34814b9f
AM
6891 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6892 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6893 --q, --p;
34814b9f 6894 if (q < eh->elf.root.root.string && *p == '.')
0a1b45a2
AM
6895 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6896 false, false));
d1329ca3
AM
6897 }
6898 if (fh != NULL)
6899 {
34814b9f
AM
6900 eh->oh = fh;
6901 fh->oh = eh;
d1329ca3
AM
6902 }
6903 }
e86ce104 6904 if (fh != NULL)
34814b9f 6905 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6906 }
6907}
6908
0a1b45a2 6909static bool
8843416a
AM
6910get_sym_h (struct elf_link_hash_entry **hp,
6911 Elf_Internal_Sym **symp,
6912 asection **symsecp,
f961d9dd 6913 unsigned char **tls_maskp,
8843416a
AM
6914 Elf_Internal_Sym **locsymsp,
6915 unsigned long r_symndx,
6916 bfd *ibfd)
411e1bfb 6917{
0ffa91dd 6918 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6919
6920 if (r_symndx >= symtab_hdr->sh_info)
6921 {
6922 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6923 struct elf_link_hash_entry *h;
6924
6925 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6926 h = elf_follow_link (h);
411e1bfb
AM
6927
6928 if (hp != NULL)
6929 *hp = h;
6930
6931 if (symp != NULL)
6932 *symp = NULL;
6933
6934 if (symsecp != NULL)
6935 {
6936 asection *symsec = NULL;
6937 if (h->root.type == bfd_link_hash_defined
6938 || h->root.type == bfd_link_hash_defweak)
6939 symsec = h->root.u.def.section;
6940 *symsecp = symsec;
6941 }
6942
e7b938ca 6943 if (tls_maskp != NULL)
ed7007c1 6944 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6945 }
6946 else
6947 {
6948 Elf_Internal_Sym *sym;
6949 Elf_Internal_Sym *locsyms = *locsymsp;
6950
6951 if (locsyms == NULL)
6952 {
6953 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6954 if (locsyms == NULL)
6955 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6956 symtab_hdr->sh_info,
6957 0, NULL, NULL, NULL);
6958 if (locsyms == NULL)
0a1b45a2 6959 return false;
411e1bfb
AM
6960 *locsymsp = locsyms;
6961 }
6962 sym = locsyms + r_symndx;
6963
6964 if (hp != NULL)
6965 *hp = NULL;
6966
6967 if (symp != NULL)
6968 *symp = sym;
6969
6970 if (symsecp != NULL)
cb33740c 6971 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6972
e7b938ca 6973 if (tls_maskp != NULL)
411e1bfb
AM
6974 {
6975 struct got_entry **lgot_ents;
f961d9dd 6976 unsigned char *tls_mask;
411e1bfb 6977
e7b938ca 6978 tls_mask = NULL;
411e1bfb
AM
6979 lgot_ents = elf_local_got_ents (ibfd);
6980 if (lgot_ents != NULL)
6981 {
e054468f
AM
6982 struct plt_entry **local_plt = (struct plt_entry **)
6983 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6984 unsigned char *lgot_masks = (unsigned char *)
e054468f 6985 (local_plt + symtab_hdr->sh_info);
e7b938ca 6986 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6987 }
e7b938ca 6988 *tls_maskp = tls_mask;
411e1bfb
AM
6989 }
6990 }
0a1b45a2 6991 return true;
411e1bfb
AM
6992}
6993
e7b938ca 6994/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6995 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6996 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6997
6998static int
f961d9dd 6999get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7000 unsigned long *toc_symndx,
7001 bfd_vma *toc_addend,
0d4792f7 7002 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7003 const Elf_Internal_Rela *rel,
7004 bfd *ibfd)
411e1bfb
AM
7005{
7006 unsigned long r_symndx;
0d4792f7 7007 int next_r;
411e1bfb
AM
7008 struct elf_link_hash_entry *h;
7009 Elf_Internal_Sym *sym;
7010 asection *sec;
7011 bfd_vma off;
7012
7013 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7014 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7015 return 0;
411e1bfb 7016
37da22e5
AM
7017 if ((*tls_maskp != NULL
7018 && (**tls_maskp & TLS_TLS) != 0
7019 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7020 || sec == NULL
6bee8834 7021 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7022 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7023 return 1;
411e1bfb
AM
7024
7025 /* Look inside a TOC section too. */
7026 if (h != NULL)
7027 {
7028 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7029 off = h->root.u.def.value;
7030 }
7031 else
7032 off = sym->st_value;
7033 off += rel->r_addend;
7034 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7035 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7036 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7037 if (toc_symndx != NULL)
7038 *toc_symndx = r_symndx;
3a71aa26
AM
7039 if (toc_addend != NULL)
7040 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7041 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7042 return 0;
854b41e7 7043 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7044 && (next_r == -1 || next_r == -2))
7045 return 1 - next_r;
951fd09b 7046 return 1;
411e1bfb
AM
7047}
7048
3b421ab3
AM
7049/* Find (or create) an entry in the tocsave hash table. */
7050
7051static struct tocsave_entry *
7052tocsave_find (struct ppc_link_hash_table *htab,
7053 enum insert_option insert,
7054 Elf_Internal_Sym **local_syms,
7055 const Elf_Internal_Rela *irela,
7056 bfd *ibfd)
7057{
7058 unsigned long r_indx;
7059 struct elf_link_hash_entry *h;
7060 Elf_Internal_Sym *sym;
7061 struct tocsave_entry ent, *p;
7062 hashval_t hash;
7063 struct tocsave_entry **slot;
7064
7065 r_indx = ELF64_R_SYM (irela->r_info);
7066 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7067 return NULL;
7068 if (ent.sec == NULL || ent.sec->output_section == NULL)
7069 {
4eca0228 7070 _bfd_error_handler
871b3ab2 7071 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7072 return NULL;
7073 }
7074
7075 if (h != NULL)
7076 ent.offset = h->root.u.def.value;
7077 else
7078 ent.offset = sym->st_value;
7079 ent.offset += irela->r_addend;
7080
7081 hash = tocsave_htab_hash (&ent);
7082 slot = ((struct tocsave_entry **)
7083 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7084 if (slot == NULL)
7085 return NULL;
7086
7087 if (*slot == NULL)
7088 {
7089 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7090 if (p == NULL)
7091 return NULL;
7092 *p = ent;
7093 *slot = p;
7094 }
7095 return *slot;
7096}
7097
754021d0 7098/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7099 code for the old ABI, these will already have been done. */
754021d0 7100
0a1b45a2 7101static bool
754021d0
AM
7102adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7103{
7104 struct ppc_link_hash_entry *eh;
7105 asection *sym_sec;
74f0fb50 7106 struct _opd_sec_data *opd;
754021d0
AM
7107
7108 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 7109 return true;
754021d0 7110
754021d0
AM
7111 if (h->root.type != bfd_link_hash_defined
7112 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 7113 return true;
754021d0 7114
ed7007c1 7115 eh = ppc_elf_hash_entry (h);
754021d0 7116 if (eh->adjust_done)
0a1b45a2 7117 return true;
754021d0
AM
7118
7119 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7120 opd = get_opd_info (sym_sec);
7121 if (opd != NULL && opd->adjust != NULL)
754021d0 7122 {
51aecdc5 7123 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7124 if (adjust == -1)
7125 {
7126 /* This entry has been deleted. */
b3fac117 7127 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7128 if (dsec == NULL)
7129 {
7130 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7131 if (discarded_section (dsec))
81688140 7132 {
b3fac117 7133 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7134 break;
7135 }
7136 }
4025353c 7137 eh->elf.root.u.def.value = 0;
81688140 7138 eh->elf.root.u.def.section = dsec;
4025353c
AM
7139 }
7140 else
7141 eh->elf.root.u.def.value += adjust;
754021d0
AM
7142 eh->adjust_done = 1;
7143 }
0a1b45a2 7144 return true;
754021d0
AM
7145}
7146
8c1d1bb8 7147/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7148 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7149 have already been determined. */
7150
0a1b45a2 7151static bool
1657026c 7152dec_dynrel_count (const Elf_Internal_Rela *rel,
8c1d1bb8
AM
7153 asection *sec,
7154 struct bfd_link_info *info,
7155 Elf_Internal_Sym **local_syms,
7156 struct elf_link_hash_entry *h,
19e08130 7157 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7158{
7159 enum elf_ppc64_reloc_type r_type;
19e08130 7160 asection *sym_sec = NULL;
8c1d1bb8
AM
7161
7162 /* Can this reloc be dynamic? This switch, and later tests here
7163 should be kept in sync with the code in check_relocs. */
1657026c 7164 r_type = ELF64_R_TYPE (rel->r_info);
8c1d1bb8
AM
7165 switch (r_type)
7166 {
7167 default:
0a1b45a2 7168 return true;
8c1d1bb8 7169
1bdd8fac
AM
7170 case R_PPC64_TOC16:
7171 case R_PPC64_TOC16_DS:
7172 case R_PPC64_TOC16_LO:
7173 case R_PPC64_TOC16_HI:
7174 case R_PPC64_TOC16_HA:
7175 case R_PPC64_TOC16_LO_DS:
7176 if (h == NULL)
0a1b45a2 7177 return true;
1bdd8fac
AM
7178 break;
7179
8c1d1bb8
AM
7180 case R_PPC64_TPREL16:
7181 case R_PPC64_TPREL16_LO:
7182 case R_PPC64_TPREL16_HI:
7183 case R_PPC64_TPREL16_HA:
7184 case R_PPC64_TPREL16_DS:
7185 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7186 case R_PPC64_TPREL16_HIGH:
7187 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7188 case R_PPC64_TPREL16_HIGHER:
7189 case R_PPC64_TPREL16_HIGHERA:
7190 case R_PPC64_TPREL16_HIGHEST:
7191 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7192 case R_PPC64_TPREL64:
c213164a 7193 case R_PPC64_TPREL34:
8c1d1bb8
AM
7194 case R_PPC64_DTPMOD64:
7195 case R_PPC64_DTPREL64:
7196 case R_PPC64_ADDR64:
7197 case R_PPC64_REL30:
7198 case R_PPC64_REL32:
7199 case R_PPC64_REL64:
7200 case R_PPC64_ADDR14:
7201 case R_PPC64_ADDR14_BRNTAKEN:
7202 case R_PPC64_ADDR14_BRTAKEN:
7203 case R_PPC64_ADDR16:
7204 case R_PPC64_ADDR16_DS:
7205 case R_PPC64_ADDR16_HA:
7206 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7207 case R_PPC64_ADDR16_HIGH:
7208 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7209 case R_PPC64_ADDR16_HIGHER:
7210 case R_PPC64_ADDR16_HIGHERA:
7211 case R_PPC64_ADDR16_HIGHEST:
7212 case R_PPC64_ADDR16_HIGHESTA:
7213 case R_PPC64_ADDR16_LO:
7214 case R_PPC64_ADDR16_LO_DS:
7215 case R_PPC64_ADDR24:
7216 case R_PPC64_ADDR32:
7217 case R_PPC64_UADDR16:
7218 case R_PPC64_UADDR32:
7219 case R_PPC64_UADDR64:
7220 case R_PPC64_TOC:
5663e321
AM
7221 case R_PPC64_D34:
7222 case R_PPC64_D34_LO:
7223 case R_PPC64_D34_HI30:
7224 case R_PPC64_D34_HA30:
7225 case R_PPC64_ADDR16_HIGHER34:
7226 case R_PPC64_ADDR16_HIGHERA34:
7227 case R_PPC64_ADDR16_HIGHEST34:
7228 case R_PPC64_ADDR16_HIGHESTA34:
7229 case R_PPC64_D28:
8c1d1bb8
AM
7230 break;
7231 }
7232
7233 if (local_syms != NULL)
7234 {
7235 unsigned long r_symndx;
8c1d1bb8
AM
7236 bfd *ibfd = sec->owner;
7237
1657026c 7238 r_symndx = ELF64_R_SYM (rel->r_info);
8c1d1bb8 7239 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
0a1b45a2 7240 return false;
8c1d1bb8
AM
7241 }
7242
ec73ddcd
AM
7243 if ((h != NULL
7244 && (h->root.type == bfd_link_hash_defweak
3a3a4c1f 7245 || (!h->def_regular && !h->root.ldscript_def)))
ec73ddcd 7246 || (h != NULL
3a3a4c1f 7247 && !SYMBOL_REFERENCES_LOCAL (info, h))
ec73ddcd 7248 || (bfd_link_pic (info)
3a3a4c1f
AM
7249 && (h != NULL
7250 ? !bfd_is_abs_symbol (&h->root)
7251 : sym_sec != bfd_abs_section_ptr)
ec73ddcd
AM
7252 && must_be_dyn_reloc (info, r_type))
7253 || (!bfd_link_pic (info)
7254 && (h != NULL
7255 ? h->type == STT_GNU_IFUNC
7256 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7257 ;
7258 else
0a1b45a2 7259 return true;
8c1d1bb8
AM
7260
7261 if (h != NULL)
6edfbbad 7262 {
1657026c
AM
7263 struct ppc_dyn_relocs *p;
7264 struct ppc_dyn_relocs **pp;
7265 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
7266
7267 /* elf_gc_sweep may have already removed all dyn relocs associated
7268 with local syms for a given section. Also, symbol flags are
7269 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7270 report a dynreloc miscount. */
7271 if (*pp == NULL && info->gc_sections)
0a1b45a2 7272 return true;
19e08130
AM
7273
7274 while ((p = *pp) != NULL)
60124e18 7275 {
19e08130
AM
7276 if (p->sec == sec)
7277 {
7278 if (!must_be_dyn_reloc (info, r_type))
7279 p->pc_count -= 1;
1657026c
AM
7280 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7281 && rel->r_offset % 2 == 0
8ffb6df2 7282 && sec->alignment_power != 0)
1657026c 7283 p->rel_count -= 1;
19e08130
AM
7284 p->count -= 1;
7285 if (p->count == 0)
7286 *pp = p->next;
0a1b45a2 7287 return true;
19e08130
AM
7288 }
7289 pp = &p->next;
60124e18 7290 }
6edfbbad 7291 }
19e08130
AM
7292 else
7293 {
1657026c
AM
7294 struct ppc_local_dyn_relocs *p;
7295 struct ppc_local_dyn_relocs **pp;
19e08130 7296 void *vpp;
0a1b45a2 7297 bool is_ifunc;
8c1d1bb8 7298
19e08130
AM
7299 if (local_syms == NULL)
7300 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7301 if (sym_sec == NULL)
7302 sym_sec = sec;
c57da1a7 7303
19e08130 7304 vpp = &elf_section_data (sym_sec)->local_dynrel;
1657026c 7305 pp = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
7306
7307 if (*pp == NULL && info->gc_sections)
0a1b45a2 7308 return true;
19e08130
AM
7309
7310 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7311 while ((p = *pp) != NULL)
8c1d1bb8 7312 {
19e08130
AM
7313 if (p->sec == sec && p->ifunc == is_ifunc)
7314 {
1657026c
AM
7315 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7316 && rel->r_offset % 2 == 0
8ffb6df2 7317 && sec->alignment_power != 0)
1657026c 7318 p->rel_count -= 1;
19e08130
AM
7319 p->count -= 1;
7320 if (p->count == 0)
7321 *pp = p->next;
0a1b45a2 7322 return true;
19e08130
AM
7323 }
7324 pp = &p->next;
8c1d1bb8 7325 }
8c1d1bb8
AM
7326 }
7327
695344c0 7328 /* xgettext:c-format */
cf97bcb0
AM
7329 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7330 sec->owner, sec);
8c1d1bb8 7331 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7332 return false;
8c1d1bb8
AM
7333}
7334
754021d0
AM
7335/* Remove unused Official Procedure Descriptor entries. Currently we
7336 only remove those associated with functions in discarded link-once
7337 sections, or weakly defined functions that have been overridden. It
7338 would be possible to remove many more entries for statically linked
7339 applications. */
7340
0a1b45a2 7341bool
e7d1c40c 7342ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7343{
7344 bfd *ibfd;
0a1b45a2 7345 bool some_edited = false;
3f764659 7346 asection *need_pad = NULL;
e7d1c40c
AM
7347 struct ppc_link_hash_table *htab;
7348
7349 htab = ppc_hash_table (info);
7350 if (htab == NULL)
0a1b45a2 7351 return false;
1e2f5b6e 7352
c72f2fb2 7353 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7354 {
7355 asection *sec;
7356 Elf_Internal_Rela *relstart, *rel, *relend;
7357 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7358 Elf_Internal_Sym *local_syms;
74f0fb50 7359 struct _opd_sec_data *opd;
0a1b45a2 7360 bool need_edit, add_aux_fields, broken;
3f764659 7361 bfd_size_type cnt_16b = 0;
1e2f5b6e 7362
854b41e7
AM
7363 if (!is_ppc64_elf (ibfd))
7364 continue;
7365
1e2f5b6e 7366 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7367 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7368 continue;
7369
dbaa2011 7370 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7371 continue;
7372
1e2f5b6e
AM
7373 if (sec->output_section == bfd_abs_section_ptr)
7374 continue;
7375
7376 /* Look through the section relocs. */
7377 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7378 continue;
7379
6cdc0ccc 7380 local_syms = NULL;
0ffa91dd 7381 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7382
7383 /* Read the relocations. */
4ce794b7 7384 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7385 info->keep_memory);
1e2f5b6e 7386 if (relstart == NULL)
0a1b45a2 7387 return false;
1e2f5b6e
AM
7388
7389 /* First run through the relocs to check they are sane, and to
7390 determine whether we need to edit this opd section. */
0a1b45a2
AM
7391 need_edit = false;
7392 broken = false;
3f764659 7393 need_pad = sec;
1e2f5b6e 7394 relend = relstart + sec->reloc_count;
50bc7936 7395 for (rel = relstart; rel < relend; )
1e2f5b6e 7396 {
04c9666a 7397 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7398 unsigned long r_symndx;
7399 asection *sym_sec;
7400 struct elf_link_hash_entry *h;
7401 Elf_Internal_Sym *sym;
51aecdc5 7402 bfd_vma offset;
1e2f5b6e 7403
51aecdc5 7404 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7405 only interested in the reloc pointing to a function entry
7406 point. */
51aecdc5
AM
7407 offset = rel->r_offset;
7408 if (rel + 1 == relend
7409 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7410 {
7411 /* If someone messes with .opd alignment then after a
7412 "ld -r" we might have padding in the middle of .opd.
7413 Also, there's nothing to prevent someone putting
7414 something silly in .opd with the assembler. No .opd
b34976b6 7415 optimization for them! */
3f764659 7416 broken_opd:
4eca0228 7417 _bfd_error_handler
871b3ab2 7418 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
0a1b45a2 7419 broken = true;
1e2f5b6e
AM
7420 break;
7421 }
7422
50bc7936
AM
7423 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7424 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7425 {
4eca0228 7426 _bfd_error_handler
695344c0 7427 /* xgettext:c-format */
871b3ab2 7428 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7429 ibfd, r_type);
0a1b45a2 7430 broken = true;
50bc7936
AM
7431 break;
7432 }
7433
1e2f5b6e 7434 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7435 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7436 r_symndx, ibfd))
50bc7936 7437 goto error_ret;
1e2f5b6e
AM
7438
7439 if (sym_sec == NULL || sym_sec->owner == NULL)
7440 {
411e1bfb
AM
7441 const char *sym_name;
7442 if (h != NULL)
7443 sym_name = h->root.root.string;
7444 else
26c61ae5
L
7445 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7446 sym_sec);
411e1bfb 7447
4eca0228 7448 _bfd_error_handler
695344c0 7449 /* xgettext:c-format */
871b3ab2 7450 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7451 ibfd, sym_name);
0a1b45a2 7452 broken = true;
1e2f5b6e
AM
7453 break;
7454 }
7455
51020317
AM
7456 /* opd entries are always for functions defined in the
7457 current input bfd. If the symbol isn't defined in the
7458 input bfd, then we won't be using the function in this
7459 bfd; It must be defined in a linkonce section in another
7460 bfd, or is weak. It's also possible that we are
7461 discarding the function due to a linker script /DISCARD/,
7462 which we test for via the output_section. */
7463 if (sym_sec->owner != ibfd
7464 || sym_sec->output_section == bfd_abs_section_ptr)
0a1b45a2 7465 need_edit = true;
1e2f5b6e 7466
50bc7936 7467 rel += 2;
51aecdc5
AM
7468 if (rel + 1 == relend
7469 || (rel + 2 < relend
7470 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7471 ++rel;
7472
7473 if (rel == relend)
3f764659
JJ
7474 {
7475 if (sec->size == offset + 24)
7476 {
7477 need_pad = NULL;
7478 break;
7479 }
51aecdc5 7480 if (sec->size == offset + 16)
3f764659
JJ
7481 {
7482 cnt_16b++;
7483 break;
7484 }
7485 goto broken_opd;
7486 }
3f764659
JJ
7487 else if (rel + 1 < relend
7488 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7489 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7490 {
51aecdc5
AM
7491 if (rel[0].r_offset == offset + 16)
7492 cnt_16b++;
7493 else if (rel[0].r_offset != offset + 24)
7494 goto broken_opd;
3f764659
JJ
7495 }
7496 else
7497 goto broken_opd;
1e2f5b6e
AM
7498 }
7499
e7d1c40c 7500 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7501
51aecdc5 7502 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7503 {
7504 Elf_Internal_Rela *write_rel;
d4730f92 7505 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7506 bfd_byte *rptr, *wptr;
983bddc8 7507 bfd_byte *new_contents;
74f0fb50
AM
7508 bfd_size_type amt;
7509
983bddc8 7510 new_contents = NULL;
51aecdc5 7511 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7512 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7513 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50 7514 if (opd->adjust == NULL)
0a1b45a2 7515 return false;
1e2f5b6e
AM
7516
7517 /* This seems a waste of time as input .opd sections are all
7518 zeros as generated by gcc, but I suppose there's no reason
7519 this will always be so. We might start putting something in
7520 the third word of .opd entries. */
7521 if ((sec->flags & SEC_IN_MEMORY) == 0)
7522 {
eea6121a
AM
7523 bfd_byte *loc;
7524 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7525 {
c9594989 7526 free (loc);
50bc7936 7527 error_ret:
c9594989 7528 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7529 free (local_syms);
6cdc0ccc
AM
7530 if (elf_section_data (sec)->relocs != relstart)
7531 free (relstart);
0a1b45a2 7532 return false;
6cdc0ccc 7533 }
1e2f5b6e
AM
7534 sec->contents = loc;
7535 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7536 }
7537
7538 elf_section_data (sec)->relocs = relstart;
7539
3f764659 7540 new_contents = sec->contents;
3f764659
JJ
7541 if (add_aux_fields)
7542 {
7543 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7544 if (new_contents == NULL)
0a1b45a2 7545 return false;
51aecdc5 7546 need_pad = NULL;
3f764659 7547 }
b4f4e59f
AM
7548 wptr = new_contents;
7549 rptr = sec->contents;
1e2f5b6e 7550 write_rel = relstart;
51aecdc5 7551 for (rel = relstart; rel < relend; )
1e2f5b6e 7552 {
50bc7936
AM
7553 unsigned long r_symndx;
7554 asection *sym_sec;
7555 struct elf_link_hash_entry *h;
51aecdc5 7556 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7557 Elf_Internal_Sym *sym;
51aecdc5
AM
7558 long opd_ent_size;
7559 Elf_Internal_Rela *next_rel;
0a1b45a2 7560 bool skip;
50bc7936
AM
7561
7562 r_symndx = ELF64_R_SYM (rel->r_info);
7563 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7564 r_symndx, ibfd))
50bc7936
AM
7565 goto error_ret;
7566
51aecdc5
AM
7567 next_rel = rel + 2;
7568 if (next_rel + 1 == relend
7569 || (next_rel + 2 < relend
7570 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7571 ++next_rel;
7572
7573 /* See if the .opd entry is full 24 byte or
7574 16 byte (with fd_aux entry overlapped with next
7575 fd_func). */
7576 opd_ent_size = 24;
7577 if (next_rel == relend)
1e2f5b6e 7578 {
51aecdc5 7579 if (sec->size == rel->r_offset + 16)
3f764659 7580 opd_ent_size = 16;
51aecdc5
AM
7581 }
7582 else if (next_rel->r_offset == rel->r_offset + 16)
7583 opd_ent_size = 16;
3f764659 7584
51aecdc5
AM
7585 if (h != NULL
7586 && h->root.root.string[0] == '.')
7587 {
ed7007c1 7588 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7589 if (fdh != NULL)
7590 {
7591 fdh = ppc_follow_link (fdh);
7592 if (fdh->elf.root.type != bfd_link_hash_defined
7593 && fdh->elf.root.type != bfd_link_hash_defweak)
7594 fdh = NULL;
7595 }
51aecdc5 7596 }
1e2f5b6e 7597
51aecdc5
AM
7598 skip = (sym_sec->owner != ibfd
7599 || sym_sec->output_section == bfd_abs_section_ptr);
7600 if (skip)
7601 {
7602 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7603 {
51aecdc5
AM
7604 /* Arrange for the function descriptor sym
7605 to be dropped. */
7606 fdh->elf.root.u.def.value = 0;
7607 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7608 }
51aecdc5 7609 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7610
0e1862bb 7611 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7612 rel = next_rel;
7613 else
7614 while (1)
7615 {
1657026c 7616 if (!dec_dynrel_count (rel, sec, info,
51aecdc5
AM
7617 NULL, h, sym))
7618 goto error_ret;
754021d0 7619
51aecdc5
AM
7620 if (++rel == next_rel)
7621 break;
1e2f5b6e 7622
51aecdc5
AM
7623 r_symndx = ELF64_R_SYM (rel->r_info);
7624 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7625 r_symndx, ibfd))
7626 goto error_ret;
7627 }
50bc7936
AM
7628 }
7629 else
1e2f5b6e 7630 {
51aecdc5
AM
7631 /* We'll be keeping this opd entry. */
7632 long adjust;
7633
7634 if (fdh != NULL)
7635 {
7636 /* Redefine the function descriptor symbol to
7637 this location in the opd section. It is
7638 necessary to update the value here rather
7639 than using an array of adjustments as we do
7640 for local symbols, because various places
7641 in the generic ELF code use the value
7642 stored in u.def.value. */
7643 fdh->elf.root.u.def.value = wptr - new_contents;
7644 fdh->adjust_done = 1;
7645 }
7646
7647 /* Local syms are a bit tricky. We could
7648 tweak them as they can be cached, but
7649 we'd need to look through the local syms
7650 for the function descriptor sym which we
7651 don't have at the moment. So keep an
7652 array of adjustments. */
7653 adjust = (wptr - new_contents) - (rptr - sec->contents);
7654 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7655
7656 if (wptr != rptr)
7657 memcpy (wptr, rptr, opd_ent_size);
7658 wptr += opd_ent_size;
7659 if (add_aux_fields && opd_ent_size == 16)
7660 {
7661 memset (wptr, '\0', 8);
7662 wptr += 8;
7663 }
7664
50bc7936 7665 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7666 new opd entries. */
7667 for ( ; rel != next_rel; ++rel)
7668 {
7669 rel->r_offset += adjust;
7670 if (write_rel != rel)
7671 memcpy (write_rel, rel, sizeof (*rel));
7672 ++write_rel;
7673 }
1e2f5b6e 7674 }
51aecdc5
AM
7675
7676 rptr += opd_ent_size;
1e2f5b6e
AM
7677 }
7678
3f764659 7679 sec->size = wptr - new_contents;
1e2f5b6e 7680 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7681 if (add_aux_fields)
7682 {
7683 free (sec->contents);
7684 sec->contents = new_contents;
7685 }
7686
05bf9422 7687 /* Fudge the header size too, as this is used later in
cdcf6e38 7688 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7689 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7690 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
0a1b45a2 7691 some_edited = true;
1e2f5b6e 7692 }
6cdc0ccc 7693 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7694 free (relstart);
6cdc0ccc 7695
411e1bfb
AM
7696 if (local_syms != NULL
7697 && symtab_hdr->contents != (unsigned char *) local_syms)
7698 {
7699 if (!info->keep_memory)
7700 free (local_syms);
7701 else
7702 symtab_hdr->contents = (unsigned char *) local_syms;
7703 }
7704 }
7705
754021d0
AM
7706 if (some_edited)
7707 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7708
3f764659
JJ
7709 /* If we are doing a final link and the last .opd entry is just 16 byte
7710 long, add a 8 byte padding after it. */
0e1862bb 7711 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7712 {
7713 bfd_byte *p;
7714
7715 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7716 {
7717 BFD_ASSERT (need_pad->size > 0);
7718
7719 p = bfd_malloc (need_pad->size + 8);
7720 if (p == NULL)
0a1b45a2 7721 return false;
699733f6 7722
2cdcc330
AM
7723 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7724 p, 0, need_pad->size))
0a1b45a2 7725 return false;
3f764659
JJ
7726
7727 need_pad->contents = p;
7728 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7729 }
7730 else
7731 {
7732 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7733 if (p == NULL)
0a1b45a2 7734 return false;
3f764659
JJ
7735
7736 need_pad->contents = p;
7737 }
7738
7739 memset (need_pad->contents + need_pad->size, 0, 8);
7740 need_pad->size += 8;
7741 }
7742
0a1b45a2 7743 return true;
411e1bfb
AM
7744}
7745
3e04d765
AM
7746/* Analyze inline PLT call relocations to see whether calls to locally
7747 defined functions can be converted to direct calls. */
7748
0a1b45a2 7749bool
3e04d765
AM
7750ppc64_elf_inline_plt (struct bfd_link_info *info)
7751{
7752 struct ppc_link_hash_table *htab;
7753 bfd *ibfd;
7754 asection *sec;
7755 bfd_vma low_vma, high_vma, limit;
7756
7757 htab = ppc_hash_table (info);
7758 if (htab == NULL)
0a1b45a2 7759 return false;
3e04d765
AM
7760
7761 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7762 reduced somewhat to cater for possible stubs that might be added
7763 between the call and its destination. */
7764 if (htab->params->group_size < 0)
7765 {
7766 limit = -htab->params->group_size;
7767 if (limit == 1)
7768 limit = 0x1e00000;
7769 }
7770 else
7771 {
7772 limit = htab->params->group_size;
7773 if (limit == 1)
7774 limit = 0x1c00000;
7775 }
7776
7777 low_vma = -1;
7778 high_vma = 0;
7779 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7780 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7781 {
7782 if (low_vma > sec->vma)
7783 low_vma = sec->vma;
7784 if (high_vma < sec->vma + sec->size)
7785 high_vma = sec->vma + sec->size;
7786 }
7787
7788 /* If a "bl" can reach anywhere in local code sections, then we can
7789 convert all inline PLT sequences to direct calls when the symbol
7790 is local. */
7791 if (high_vma - low_vma < limit)
7792 {
7793 htab->can_convert_all_inline_plt = 1;
0a1b45a2 7794 return true;
3e04d765
AM
7795 }
7796
7797 /* Otherwise, go looking through relocs for cases where a direct
7798 call won't reach. Mark the symbol on any such reloc to disable
7799 the optimization and keep the PLT entry as it seems likely that
7800 this will be better than creating trampolines. Note that this
7801 will disable the optimization for all inline PLT calls to a
7802 particular symbol, not just those that won't reach. The
7803 difficulty in doing a more precise optimization is that the
7804 linker needs to make a decision depending on whether a
7805 particular R_PPC64_PLTCALL insn can be turned into a direct
7806 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7807 the sequence, and there is nothing that ties those relocs
7808 together except their symbol. */
7809
7810 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7811 {
7812 Elf_Internal_Shdr *symtab_hdr;
7813 Elf_Internal_Sym *local_syms;
7814
7815 if (!is_ppc64_elf (ibfd))
7816 continue;
7817
7818 local_syms = NULL;
7819 symtab_hdr = &elf_symtab_hdr (ibfd);
7820
7821 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7822 if (ppc64_elf_section_data (sec)->has_pltcall
7823 && !bfd_is_abs_section (sec->output_section))
7824 {
7825 Elf_Internal_Rela *relstart, *rel, *relend;
7826
7827 /* Read the relocations. */
7828 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7829 info->keep_memory);
7830 if (relstart == NULL)
0a1b45a2 7831 return false;
3e04d765
AM
7832
7833 relend = relstart + sec->reloc_count;
435edf0b 7834 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7835 {
7836 enum elf_ppc64_reloc_type r_type;
7837 unsigned long r_symndx;
7838 asection *sym_sec;
7839 struct elf_link_hash_entry *h;
7840 Elf_Internal_Sym *sym;
7841 unsigned char *tls_maskp;
7842
7843 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7844 if (r_type != R_PPC64_PLTCALL
7845 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7846 continue;
7847
7848 r_symndx = ELF64_R_SYM (rel->r_info);
7849 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7850 r_symndx, ibfd))
7851 {
7852 if (elf_section_data (sec)->relocs != relstart)
7853 free (relstart);
c9594989 7854 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765 7855 free (local_syms);
0a1b45a2 7856 return false;
3e04d765
AM
7857 }
7858
7859 if (sym_sec != NULL && sym_sec->output_section != NULL)
7860 {
7861 bfd_vma from, to;
7862 if (h != NULL)
7863 to = h->root.u.def.value;
7864 else
7865 to = sym->st_value;
7866 to += (rel->r_addend
7867 + sym_sec->output_offset
7868 + sym_sec->output_section->vma);
7869 from = (rel->r_offset
7870 + sec->output_offset
7871 + sec->output_section->vma);
5663e321
AM
7872 if (to - from + limit < 2 * limit
7873 && !(r_type == R_PPC64_PLTCALL_NOTOC
7874 && (((h ? h->other : sym->st_other)
7875 & STO_PPC64_LOCAL_MASK)
4a4e7361 7876 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7877 *tls_maskp &= ~PLT_KEEP;
7878 }
7879 }
7880 if (elf_section_data (sec)->relocs != relstart)
7881 free (relstart);
7882 }
7883
7884 if (local_syms != NULL
7885 && symtab_hdr->contents != (unsigned char *) local_syms)
7886 {
7887 if (!info->keep_memory)
7888 free (local_syms);
7889 else
7890 symtab_hdr->contents = (unsigned char *) local_syms;
7891 }
7892 }
7893
0a1b45a2 7894 return true;
3e04d765
AM
7895}
7896
c9405344
AM
7897/* Set htab->tls_get_addr and various other info specific to TLS.
7898 This needs to run before dynamic symbols are processed in
7899 bfd_elf_size_dynamic_sections. */
411e1bfb 7900
0a1b45a2 7901bool
e7d1c40c 7902ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7903{
411e1bfb 7904 struct ppc_link_hash_table *htab;
9e7028aa 7905 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7906
411e1bfb 7907 htab = ppc_hash_table (info);
4dfe6ac6 7908 if (htab == NULL)
0a1b45a2 7909 return false;
4dfe6ac6 7910
a442059f
AM
7911 /* Move dynamic linking info to the function descriptor sym. */
7912 if (htab->need_func_desc_adj)
7913 {
7914 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7915 htab->need_func_desc_adj = 0;
7916 }
7917
ee67d69a
AM
7918 if (abiversion (info->output_bfd) == 1)
7919 htab->opd_abi = 1;
7920
e7d1c40c 7921 if (htab->params->no_multi_toc)
33c0ec9d
AM
7922 htab->do_multi_toc = 0;
7923 else if (!htab->do_multi_toc)
e7d1c40c 7924 htab->params->no_multi_toc = 1;
33c0ec9d 7925
8b5f1ed8
AM
7926 /* Default to --no-plt-localentry, as this option can cause problems
7927 with symbol interposition. For example, glibc libpthread.so and
7928 libc.so duplicate many pthread symbols, with a fallback
7929 implementation in libc.so. In some cases the fallback does more
7930 work than the pthread implementation. __pthread_condattr_destroy
7931 is one such symbol: the libpthread.so implementation is
7932 localentry:0 while the libc.so implementation is localentry:8.
7933 An app that "cleverly" uses dlopen to only load necessary
7934 libraries at runtime may omit loading libpthread.so when not
7935 running multi-threaded, which then results in the libc.so
7936 fallback symbols being used and ld.so complaining. Now there
7937 are workarounds in ld (see non_zero_localentry) to detect the
7938 pthread situation, but that may not be the only case where
7939 --plt-localentry can cause trouble. */
f378ab09 7940 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7941 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7942 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7943 {
7944 /* The issue is that __glink_PLTresolve saves r2, which is done
7945 because glibc ld.so _dl_runtime_resolve restores r2 to support
7946 a glibc plt call optimisation where global entry code is
7947 skipped on calls that resolve to the same binary. The
7948 __glink_PLTresolve save of r2 is incompatible with code
7949 making tail calls, because the tail call might go via the
7950 resolver and thus overwrite the proper saved r2. */
7951 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7952 "power10 pc-relative code"));
7953 htab->params->plt_localentry0 = 0;
7954 }
d44c746a
AM
7955 if (htab->params->plt_localentry0
7956 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
0a1b45a2 7957 false, false, false) == NULL)
cf97bcb0
AM
7958 _bfd_error_handler
7959 (_("warning: --plt-localentry is especially dangerous without "
7960 "ld.so support to detect ABI violations"));
f378ab09 7961
9e7028aa 7962 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 7963 false, false, true);
9e7028aa 7964 htab->tls_get_addr = ppc_elf_hash_entry (tga);
9e7028aa 7965 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 7966 false, false, true);
9e7028aa
AM
7967 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7968
7969 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
0a1b45a2 7970 false, false, true);
9e7028aa 7971 htab->tga_desc = ppc_elf_hash_entry (desc);
9e7028aa 7972 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
0a1b45a2 7973 false, false, true);
9e7028aa
AM
7974 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7975
7c9cf415 7976 if (htab->params->tls_get_addr_opt)
a7f2871e 7977 {
9e7028aa 7978 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7979
7980 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
0a1b45a2 7981 false, false, true);
a7f2871e 7982 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
0a1b45a2 7983 false, false, true);
a7f2871e
AM
7984 if (opt_fd != NULL
7985 && (opt_fd->root.type == bfd_link_hash_defined
7986 || opt_fd->root.type == bfd_link_hash_defweak))
7987 {
7988 /* If glibc supports an optimized __tls_get_addr call stub,
7989 signalled by the presence of __tls_get_addr_opt, and we'll
7990 be calling __tls_get_addr via a plt call stub, then
7991 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
7992 if (!(htab->elf.dynamic_sections_created
7993 && tga_fd != NULL
7994 && (tga_fd->type == STT_FUNC
7995 || tga_fd->needs_plt)
7996 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7997 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7998 tga_fd = NULL;
7999 if (!(htab->elf.dynamic_sections_created
8000 && desc_fd != NULL
8001 && (desc_fd->type == STT_FUNC
8002 || desc_fd->needs_plt)
8003 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8004 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8005 desc_fd = NULL;
8006
8007 if (tga_fd != NULL || desc_fd != NULL)
8008 {
8009 struct plt_entry *ent = NULL;
8010
8011 if (tga_fd != NULL)
8012 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8013 if (ent->plt.refcount > 0)
8014 break;
8015 if (ent == NULL && desc_fd != NULL)
8016 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8017 if (ent->plt.refcount > 0)
8018 break;
a7f2871e
AM
8019 if (ent != NULL)
8020 {
9e7028aa
AM
8021 if (tga_fd != NULL)
8022 {
8023 tga_fd->root.type = bfd_link_hash_indirect;
8024 tga_fd->root.u.i.link = &opt_fd->root;
8025 tga_fd->root.u.i.warning = NULL;
8026 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8027 }
8028 if (desc_fd != NULL)
8029 {
8030 desc_fd->root.type = bfd_link_hash_indirect;
8031 desc_fd->root.u.i.link = &opt_fd->root;
8032 desc_fd->root.u.i.warning = NULL;
8033 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8034 }
b531344c 8035 opt_fd->mark = 1;
a7f2871e
AM
8036 if (opt_fd->dynindx != -1)
8037 {
8038 /* Use __tls_get_addr_opt in dynamic relocations. */
8039 opt_fd->dynindx = -1;
8040 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8041 opt_fd->dynstr_index);
8042 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
0a1b45a2 8043 return false;
a7f2871e 8044 }
9e7028aa 8045 if (tga_fd != NULL)
a7f2871e 8046 {
9e7028aa 8047 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 8048 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
8049 if (opt != NULL && tga != NULL)
8050 {
8051 tga->root.type = bfd_link_hash_indirect;
8052 tga->root.u.i.link = &opt->root;
8053 tga->root.u.i.warning = NULL;
8054 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8055 opt->mark = 1;
8056 _bfd_elf_link_hash_hide_symbol (info, opt,
8057 tga->forced_local);
8058 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8059 }
8060 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8061 htab->tls_get_addr_fd->is_func_descriptor = 1;
8062 if (htab->tls_get_addr != NULL)
8063 {
8064 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8065 htab->tls_get_addr->is_func = 1;
8066 }
a7f2871e 8067 }
9e7028aa 8068 if (desc_fd != NULL)
a7f2871e 8069 {
9e7028aa
AM
8070 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8071 if (opt != NULL && desc != NULL)
8072 {
8073 desc->root.type = bfd_link_hash_indirect;
8074 desc->root.u.i.link = &opt->root;
8075 desc->root.u.i.warning = NULL;
8076 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8077 opt->mark = 1;
8078 _bfd_elf_link_hash_hide_symbol (info, opt,
8079 desc->forced_local);
8080 htab->tga_desc = ppc_elf_hash_entry (opt);
8081 }
8082 htab->tga_desc_fd->oh = htab->tga_desc;
8083 htab->tga_desc_fd->is_func_descriptor = 1;
8084 if (htab->tga_desc != NULL)
8085 {
8086 htab->tga_desc->oh = htab->tga_desc_fd;
8087 htab->tga_desc->is_func = 1;
8088 }
a7f2871e
AM
8089 }
8090 }
8091 }
8092 }
7c9cf415
AM
8093 else if (htab->params->tls_get_addr_opt < 0)
8094 htab->params->tls_get_addr_opt = 0;
a7f2871e 8095 }
9e7028aa
AM
8096
8097 if (htab->tga_desc_fd != NULL
8098 && htab->params->tls_get_addr_opt
8099 && htab->params->no_tls_get_addr_regsave == -1)
8100 htab->params->no_tls_get_addr_regsave = 0;
8101
0a1b45a2 8102 return true;
3a71aa26 8103}
8387904d 8104
3a71aa26 8105/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 8106 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 8107
0a1b45a2 8108static bool
30845f11
AM
8109branch_reloc_hash_match (bfd *ibfd,
8110 Elf_Internal_Rela *rel,
8111 struct ppc_link_hash_entry *hash1,
8112 struct ppc_link_hash_entry *hash2,
8113 struct ppc_link_hash_entry *hash3,
8114 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
8115{
8116 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8117 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8118 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8119
e054468f 8120 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8121 {
3a71aa26
AM
8122 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8123 struct elf_link_hash_entry *h;
8387904d 8124
3a71aa26 8125 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8126 h = elf_follow_link (h);
30845f11
AM
8127 if (h == elf_hash_entry (hash1)
8128 || h == elf_hash_entry (hash2)
8129 || h == elf_hash_entry (hash3)
8130 || h == elf_hash_entry (hash4))
0a1b45a2 8131 return true;
a48ebf4d 8132 }
0a1b45a2 8133 return false;
951fd09b 8134}
411e1bfb 8135
951fd09b
AM
8136/* Run through all the TLS relocs looking for optimization
8137 opportunities. The linker has been hacked (see ppc64elf.em) to do
8138 a preliminary section layout so that we know the TLS segment
8139 offsets. We can't optimize earlier because some optimizations need
8140 to know the tp offset, and we need to optimize before allocating
8141 dynamic relocations. */
8142
0a1b45a2 8143bool
33c0ec9d 8144ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8145{
8146 bfd *ibfd;
8147 asection *sec;
8148 struct ppc_link_hash_table *htab;
663a1470 8149 unsigned char *toc_ref;
102890f0 8150 int pass;
951fd09b 8151
3cbc1e5e 8152 if (!bfd_link_executable (info))
0a1b45a2 8153 return true;
411e1bfb 8154
951fd09b 8155 htab = ppc_hash_table (info);
4dfe6ac6 8156 if (htab == NULL)
0a1b45a2 8157 return false;
4dfe6ac6 8158
252dcdf4
AM
8159 htab->do_tls_opt = 1;
8160
663a1470
AM
8161 /* Make two passes over the relocs. On the first pass, mark toc
8162 entries involved with tls relocs, and check that tls relocs
8163 involved in setting up a tls_get_addr call are indeed followed by
8164 such a call. If they are not, we can't do any tls optimization.
8165 On the second pass twiddle tls_mask flags to notify
8166 relocate_section that optimization can be done, and adjust got
8167 and plt refcounts. */
8168 toc_ref = NULL;
8169 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8170 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8171 {
8172 Elf_Internal_Sym *locsyms = NULL;
8173 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8174
102890f0
AM
8175 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8176 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8177 {
8178 Elf_Internal_Rela *relstart, *rel, *relend;
0a1b45a2 8179 bool found_tls_get_addr_arg = 0;
411e1bfb 8180
102890f0
AM
8181 /* Read the relocations. */
8182 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8183 info->keep_memory);
8184 if (relstart == NULL)
2915c55b
JK
8185 {
8186 free (toc_ref);
0a1b45a2 8187 return false;
2915c55b 8188 }
411e1bfb 8189
102890f0
AM
8190 relend = relstart + sec->reloc_count;
8191 for (rel = relstart; rel < relend; rel++)
8192 {
8193 enum elf_ppc64_reloc_type r_type;
8194 unsigned long r_symndx;
8195 struct elf_link_hash_entry *h;
8196 Elf_Internal_Sym *sym;
8197 asection *sym_sec;
f961d9dd 8198 unsigned char *tls_mask;
46e9995a 8199 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0 8200 bfd_vma value;
0a1b45a2 8201 bool ok_tprel, is_local;
102890f0
AM
8202 long toc_ref_index = 0;
8203 int expecting_tls_get_addr = 0;
0a1b45a2 8204 bool ret = false;
411e1bfb 8205
102890f0
AM
8206 r_symndx = ELF64_R_SYM (rel->r_info);
8207 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8208 r_symndx, ibfd))
8209 {
8210 err_free_rel:
8211 if (elf_section_data (sec)->relocs != relstart)
8212 free (relstart);
c9594989
AM
8213 free (toc_ref);
8214 if (elf_symtab_hdr (ibfd).contents
8215 != (unsigned char *) locsyms)
102890f0 8216 free (locsyms);
663a1470 8217 return ret;
102890f0 8218 }
411e1bfb 8219
102890f0
AM
8220 if (h != NULL)
8221 {
766bc656
AM
8222 if (h->root.type == bfd_link_hash_defined
8223 || h->root.type == bfd_link_hash_defweak)
8224 value = h->root.u.def.value;
8225 else if (h->root.type == bfd_link_hash_undefweak)
8226 value = 0;
8227 else
663a1470
AM
8228 {
8229 found_tls_get_addr_arg = 0;
8230 continue;
8231 }
102890f0
AM
8232 }
8233 else
8234 /* Symbols referenced by TLS relocs must be of type
8235 STT_TLS. So no need for .opd local sym adjust. */
8236 value = sym->st_value;
8237
0a1b45a2 8238 ok_tprel = false;
f749f26e
AM
8239 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8240 if (is_local)
102890f0 8241 {
766bc656
AM
8242 if (h != NULL
8243 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 8244 ok_tprel = true;
c27b8c2a
AM
8245 else if (sym_sec != NULL
8246 && sym_sec->output_section != NULL)
766bc656
AM
8247 {
8248 value += sym_sec->output_offset;
8249 value += sym_sec->output_section->vma;
0b147428 8250 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8251 /* Note that even though the prefix insns
8252 allow a 1<<33 offset we use the same test
8253 as for addis;addi. There may be a mix of
8254 pcrel and non-pcrel code and the decision
8255 to optimise is per symbol, not per TLS
8256 sequence. */
0b147428 8257 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8258 }
102890f0 8259 }
951fd09b 8260
102890f0 8261 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8262 /* If this section has old-style __tls_get_addr calls
8263 without marker relocs, then check that each
8264 __tls_get_addr call reloc is preceded by a reloc
8265 that conceivably belongs to the __tls_get_addr arg
8266 setup insn. If we don't find matching arg setup
8267 relocs, don't do any tls optimization. */
8268 if (pass == 0
9737e8af 8269 && sec->nomark_tls_get_addr
663a1470 8270 && h != NULL
ed7007c1 8271 && is_tls_get_addr (h, htab)
663a1470
AM
8272 && !found_tls_get_addr_arg
8273 && is_branch_reloc (r_type))
8274 {
25f53a85 8275 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8276 "TLS optimization disabled\n"),
8277 ibfd, sec, rel->r_offset);
0a1b45a2 8278 ret = true;
663a1470
AM
8279 goto err_free_rel;
8280 }
8281
8282 found_tls_get_addr_arg = 0;
102890f0
AM
8283 switch (r_type)
8284 {
8285 case R_PPC64_GOT_TLSLD16:
8286 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8287 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8288 expecting_tls_get_addr = 1;
663a1470 8289 found_tls_get_addr_arg = 1;
1a0670f3 8290 /* Fall through. */
102890f0
AM
8291
8292 case R_PPC64_GOT_TLSLD16_HI:
8293 case R_PPC64_GOT_TLSLD16_HA:
8294 /* These relocs should never be against a symbol
8295 defined in a shared lib. Leave them alone if
8296 that turns out to be the case. */
8297 if (!is_local)
8298 continue;
411e1bfb 8299
102890f0 8300 /* LD -> LE */
411e1bfb 8301 tls_set = 0;
102890f0
AM
8302 tls_clear = TLS_LD;
8303 tls_type = TLS_TLS | TLS_LD;
8304 break;
411e1bfb 8305
102890f0
AM
8306 case R_PPC64_GOT_TLSGD16:
8307 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8308 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8309 expecting_tls_get_addr = 1;
663a1470 8310 found_tls_get_addr_arg = 1;
1a0670f3 8311 /* Fall through. */
102890f0
AM
8312
8313 case R_PPC64_GOT_TLSGD16_HI:
8314 case R_PPC64_GOT_TLSGD16_HA:
8315 if (ok_tprel)
8316 /* GD -> LE */
411e1bfb 8317 tls_set = 0;
102890f0
AM
8318 else
8319 /* GD -> IE */
b00a0a86 8320 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8321 tls_clear = TLS_GD;
8322 tls_type = TLS_TLS | TLS_GD;
8323 break;
8324
87c69f97 8325 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8326 case R_PPC64_GOT_TPREL16_DS:
8327 case R_PPC64_GOT_TPREL16_LO_DS:
8328 case R_PPC64_GOT_TPREL16_HI:
8329 case R_PPC64_GOT_TPREL16_HA:
8330 if (ok_tprel)
8331 {
8332 /* IE -> LE */
8333 tls_set = 0;
8334 tls_clear = TLS_TPREL;
8335 tls_type = TLS_TLS | TLS_TPREL;
8336 break;
8337 }
411e1bfb
AM
8338 continue;
8339
727fc41e 8340 case R_PPC64_TLSLD:
7d04a20a
AM
8341 if (!is_local)
8342 continue;
8343 /* Fall through. */
8344 case R_PPC64_TLSGD:
23cedd1d
AM
8345 if (rel + 1 < relend
8346 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8347 {
8348 if (pass != 0
2cdcc330 8349 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8350 != R_PPC64_PLTSEQ)
8351 && (ELF64_R_TYPE (rel[1].r_info)
8352 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8353 {
8354 r_symndx = ELF64_R_SYM (rel[1].r_info);
8355 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8356 r_symndx, ibfd))
23cedd1d
AM
8357 goto err_free_rel;
8358 if (h != NULL)
8359 {
8360 struct plt_entry *ent = NULL;
8361
8362 for (ent = h->plt.plist;
8363 ent != NULL;
8364 ent = ent->next)
8365 if (ent->addend == rel[1].r_addend)
8366 break;
8367
8368 if (ent != NULL
8369 && ent->plt.refcount > 0)
8370 ent->plt.refcount -= 1;
8371 }
8372 }
8373 continue;
8374 }
663a1470 8375 found_tls_get_addr_arg = 1;
1a0670f3 8376 /* Fall through. */
663a1470
AM
8377
8378 case R_PPC64_TLS:
8379 case R_PPC64_TOC16:
8380 case R_PPC64_TOC16_LO:
102890f0
AM
8381 if (sym_sec == NULL || sym_sec != toc)
8382 continue;
8383
8384 /* Mark this toc entry as referenced by a TLS
8385 code sequence. We can do that now in the
8386 case of R_PPC64_TLS, and after checking for
8387 tls_get_addr for the TOC16 relocs. */
8388 if (toc_ref == NULL)
2cdcc330
AM
8389 toc_ref
8390 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8391 if (toc_ref == NULL)
8392 goto err_free_rel;
8393
102890f0
AM
8394 if (h != NULL)
8395 value = h->root.u.def.value;
8396 else
8397 value = sym->st_value;
8398 value += rel->r_addend;
73242275
AM
8399 if (value % 8 != 0)
8400 continue;
8401 BFD_ASSERT (value < toc->size
8402 && toc->output_offset % 8 == 0);
663a1470 8403 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8404 if (r_type == R_PPC64_TLS
8405 || r_type == R_PPC64_TLSGD
8406 || r_type == R_PPC64_TLSLD)
102890f0
AM
8407 {
8408 toc_ref[toc_ref_index] = 1;
8409 continue;
8410 }
8411
8412 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8413 continue;
8414
8415 tls_set = 0;
8416 tls_clear = 0;
8417 expecting_tls_get_addr = 2;
8418 break;
8419
8420 case R_PPC64_TPREL64:
8421 if (pass == 0
8422 || sec != toc
8423 || toc_ref == NULL
663a1470 8424 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8425 continue;
8426 if (ok_tprel)
8427 {
8428 /* IE -> LE */
8429 tls_set = TLS_EXPLICIT;
8430 tls_clear = TLS_TPREL;
8431 break;
8432 }
8433 continue;
8434
8435 case R_PPC64_DTPMOD64:
8436 if (pass == 0
8437 || sec != toc
8438 || toc_ref == NULL
663a1470 8439 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8440 continue;
8441 if (rel + 1 < relend
8442 && (rel[1].r_info
8443 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8444 && rel[1].r_offset == rel->r_offset + 8)
8445 {
8446 if (ok_tprel)
8447 /* GD -> LE */
8448 tls_set = TLS_EXPLICIT | TLS_GD;
8449 else
8450 /* GD -> IE */
b00a0a86 8451 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8452 tls_clear = TLS_GD;
8453 }
8454 else
8455 {
8456 if (!is_local)
8457 continue;
8458
8459 /* LD -> LE */
8460 tls_set = TLS_EXPLICIT;
8461 tls_clear = TLS_LD;
8462 }
8463 break;
8464
252dcdf4
AM
8465 case R_PPC64_TPREL16_HA:
8466 if (pass == 0)
8467 {
8468 unsigned char buf[4];
8469 unsigned int insn;
8470 bfd_vma off = rel->r_offset & ~3;
8471 if (!bfd_get_section_contents (ibfd, sec, buf,
8472 off, 4))
8473 goto err_free_rel;
8474 insn = bfd_get_32 (ibfd, buf);
8475 /* addis rt,13,imm */
8476 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8477 != ((15u << 26) | (13 << 16)))
8478 {
8479 /* xgettext:c-format */
8480 info->callbacks->minfo
8481 (_("%H: warning: %s unexpected insn %#x.\n"),
8482 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8483 htab->do_tls_opt = 0;
8484 }
8485 }
8486 continue;
8487
8488 case R_PPC64_TPREL16_HI:
8489 case R_PPC64_TPREL16_HIGH:
8490 case R_PPC64_TPREL16_HIGHA:
8491 case R_PPC64_TPREL16_HIGHER:
8492 case R_PPC64_TPREL16_HIGHERA:
8493 case R_PPC64_TPREL16_HIGHEST:
8494 case R_PPC64_TPREL16_HIGHESTA:
8495 /* These can all be used in sequences along with
8496 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8497 able to verify easily. */
8498 htab->do_tls_opt = 0;
8499 continue;
8500
102890f0
AM
8501 default:
8502 continue;
8503 }
8504
8505 if (pass == 0)
8506 {
727fc41e 8507 if (!expecting_tls_get_addr
9737e8af 8508 || !sec->nomark_tls_get_addr)
102890f0
AM
8509 continue;
8510
3a71aa26
AM
8511 if (rel + 1 < relend
8512 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8513 htab->tls_get_addr_fd,
8514 htab->tga_desc_fd,
3a71aa26 8515 htab->tls_get_addr,
9e7028aa 8516 htab->tga_desc))
102890f0 8517 {
3a71aa26 8518 if (expecting_tls_get_addr == 2)
102890f0 8519 {
3a71aa26 8520 /* Check for toc tls entries. */
f961d9dd 8521 unsigned char *toc_tls;
3a71aa26
AM
8522 int retval;
8523
8524 retval = get_tls_mask (&toc_tls, NULL, NULL,
8525 &locsyms,
8526 rel, ibfd);
8527 if (retval == 0)
8528 goto err_free_rel;
663a1470
AM
8529 if (toc_tls != NULL)
8530 {
37da22e5
AM
8531 if ((*toc_tls & TLS_TLS) != 0
8532 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8533 found_tls_get_addr_arg = 1;
8534 if (retval > 1)
8535 toc_ref[toc_ref_index] = 1;
8536 }
102890f0 8537 }
3a71aa26 8538 continue;
102890f0
AM
8539 }
8540
102890f0
AM
8541 /* Uh oh, we didn't find the expected call. We
8542 could just mark this symbol to exclude it
8543 from tls optimization but it's safer to skip
663a1470 8544 the entire optimization. */
695344c0 8545 /* xgettext:c-format */
25f53a85 8546 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8547 "TLS optimization disabled\n"),
8548 ibfd, sec, rel->r_offset);
0a1b45a2 8549 ret = true;
663a1470 8550 goto err_free_rel;
102890f0
AM
8551 }
8552
37da22e5
AM
8553 /* If we don't have old-style __tls_get_addr calls
8554 without TLSGD/TLSLD marker relocs, and we haven't
8555 found a new-style __tls_get_addr call with a
8556 marker for this symbol, then we either have a
8557 broken object file or an -mlongcall style
8558 indirect call to __tls_get_addr without a marker.
8559 Disable optimization in this case. */
8560 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8561 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8562 && !sec->nomark_tls_get_addr
37da22e5
AM
8563 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8564 != (TLS_TLS | TLS_MARK)))
8565 continue;
8566
7d04a20a 8567 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8568 {
23cedd1d
AM
8569 struct plt_entry *ent = NULL;
8570
9e7028aa
AM
8571 if (htab->tls_get_addr_fd != NULL)
8572 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8573 ent != NULL;
8574 ent = ent->next)
8575 if (ent->addend == 0)
8576 break;
8577
8578 if (ent == NULL && htab->tga_desc_fd != NULL)
8579 for (ent = htab->tga_desc_fd->elf.plt.plist;
8580 ent != NULL;
8581 ent = ent->next)
8582 if (ent->addend == 0)
8583 break;
8584
8585 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8586 for (ent = htab->tls_get_addr->elf.plt.plist;
8587 ent != NULL;
8588 ent = ent->next)
8589 if (ent->addend == 0)
102890f0 8590 break;
411e1bfb 8591
9e7028aa
AM
8592 if (ent == NULL && htab->tga_desc != NULL)
8593 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8594 ent != NULL;
8595 ent = ent->next)
8596 if (ent->addend == 0)
102890f0 8597 break;
23cedd1d
AM
8598
8599 if (ent != NULL
8600 && ent->plt.refcount > 0)
8601 ent->plt.refcount -= 1;
102890f0 8602 }
411e1bfb 8603
102890f0 8604 if (tls_clear == 0)
30038c59
AM
8605 continue;
8606
102890f0
AM
8607 if ((tls_set & TLS_EXPLICIT) == 0)
8608 {
8609 struct got_entry *ent;
411e1bfb 8610
102890f0
AM
8611 /* Adjust got entry for this reloc. */
8612 if (h != NULL)
8613 ent = h->got.glist;
8614 else
8615 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8616
102890f0
AM
8617 for (; ent != NULL; ent = ent->next)
8618 if (ent->addend == rel->r_addend
8619 && ent->owner == ibfd
8620 && ent->tls_type == tls_type)
8621 break;
8622 if (ent == NULL)
8623 abort ();
411e1bfb 8624
102890f0
AM
8625 if (tls_set == 0)
8626 {
8627 /* We managed to get rid of a got entry. */
8628 if (ent->got.refcount > 0)
8629 ent->got.refcount -= 1;
8630 }
8631 }
8632 else
8633 {
8634 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8635 we'll lose one or two dyn relocs. */
1657026c 8636 if (!dec_dynrel_count (rel, sec, info,
19e08130 8637 NULL, h, sym))
0a1b45a2 8638 return false;
411e1bfb 8639
102890f0
AM
8640 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8641 {
1657026c 8642 if (!dec_dynrel_count (rel + 1, sec, info,
19e08130 8643 NULL, h, sym))
0a1b45a2 8644 return false;
102890f0
AM
8645 }
8646 }
411e1bfb 8647
46e9995a 8648 *tls_mask |= tls_set & 0xff;
102890f0
AM
8649 *tls_mask &= ~tls_clear;
8650 }
8c1d1bb8 8651
102890f0
AM
8652 if (elf_section_data (sec)->relocs != relstart)
8653 free (relstart);
8654 }
411e1bfb 8655
663a1470
AM
8656 if (locsyms != NULL
8657 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8658 {
8659 if (!info->keep_memory)
8660 free (locsyms);
8661 else
8662 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8663 }
8664 }
411e1bfb 8665
c9594989 8666 free (toc_ref);
0a1b45a2 8667 return true;
1e2f5b6e 8668}
b34976b6 8669
c5614fa4
AM
8670/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8671 the values of any global symbols in a toc section that has been
8672 edited. Globals in toc sections should be a rarity, so this function
8673 sets a flag if any are found in toc sections other than the one just
de194d85 8674 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8675
8676struct adjust_toc_info
8677{
8678 asection *toc;
8679 unsigned long *skip;
0a1b45a2 8680 bool global_toc_syms;
c5614fa4
AM
8681};
8682
ba761f19
AM
8683enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8684
0a1b45a2 8685static bool
c5614fa4
AM
8686adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8687{
8688 struct ppc_link_hash_entry *eh;
8689 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8690 unsigned long i;
c5614fa4 8691
c5614fa4
AM
8692 if (h->root.type != bfd_link_hash_defined
8693 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 8694 return true;
c5614fa4 8695
ed7007c1 8696 eh = ppc_elf_hash_entry (h);
c5614fa4 8697 if (eh->adjust_done)
0a1b45a2 8698 return true;
c5614fa4
AM
8699
8700 if (eh->elf.root.u.def.section == toc_inf->toc)
8701 {
854b41e7
AM
8702 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8703 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8704 else
854b41e7
AM
8705 i = eh->elf.root.u.def.value >> 3;
8706
ba761f19 8707 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8708 {
4eca0228 8709 _bfd_error_handler
854b41e7
AM
8710 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8711 do
8712 ++i;
ba761f19 8713 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8714 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8715 }
854b41e7
AM
8716
8717 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8718 eh->adjust_done = 1;
8719 }
8720 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
0a1b45a2 8721 toc_inf->global_toc_syms = true;
c5614fa4 8722
0a1b45a2 8723 return true;
c5614fa4
AM
8724}
8725
39eeab25
AM
8726/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8727 on a _LO variety toc/got reloc. */
560c8763 8728
0a1b45a2 8729static bool
39eeab25 8730ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8731{
2365f8d7
AM
8732 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8733 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8734 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8735 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8736 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8737 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8738 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8739 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8740 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8741 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8742 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8743 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8744 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8745 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8746 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8747 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8748 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8749 /* Exclude lfqu by testing reloc. If relocs are ever
8750 defined for the reduced D field in psq_lu then those
8751 will need testing too. */
8752 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8753 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8754 && (insn & 1) == 0)
2365f8d7
AM
8755 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8756 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8757 /* Exclude stfqu. psq_stu as above for psq_lu. */
8758 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8759 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8760 && (insn & 1) == 0));
560c8763
AM
8761}
8762
4a421c53
AM
8763/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8764 pld ra,symbol@got@pcrel
dd9b12c2 8765 load/store rt,off(ra)
4a421c53 8766 or
d4b87b1e 8767 pla ra,symbol@pcrel
dd9b12c2 8768 load/store rt,off(ra)
4a421c53 8769 may be translated to
dd9b12c2 8770 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8771 nop.
8772 This function returns true if the optimization is possible, placing
dd9b12c2 8773 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8774
8775 On entry to this function, the linker has already determined that
d4b87b1e 8776 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8777 while *PINSN2 is the second instruction. */
8778
0a1b45a2 8779static bool
dd9b12c2 8780xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8781{
77486630
AM
8782 uint64_t insn1 = *pinsn1;
8783 uint64_t insn2 = *pinsn2;
dd9b12c2 8784 bfd_signed_vma off;
4a421c53 8785
77486630
AM
8786 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8787 {
8788 /* Check that regs match. */
8789 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8790 return false;
77486630
AM
8791
8792 /* P8LS or PMLS form, non-pcrel. */
8793 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
0a1b45a2 8794 return false;
77486630
AM
8795
8796 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8797 *pinsn2 = PNOP;
8798 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8799 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
0a1b45a2 8800 return true;
77486630
AM
8801 }
8802
8803 insn2 >>= 32;
8804
4a421c53 8805 /* Check that regs match. */
77486630 8806 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8807 return false;
4a421c53
AM
8808
8809 switch ((insn2 >> 26) & 63)
8810 {
8811 default:
0a1b45a2 8812 return false;
4a421c53
AM
8813
8814 case 32: /* lwz */
8815 case 34: /* lbz */
8816 case 36: /* stw */
8817 case 38: /* stb */
8818 case 40: /* lhz */
8819 case 42: /* lha */
8820 case 44: /* sth */
8821 case 48: /* lfs */
8822 case 50: /* lfd */
8823 case 52: /* stfs */
8824 case 54: /* stfd */
8825 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8826 on the insn. */
77486630 8827 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8828 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8829 off = insn2 & 0xffff;
4a421c53
AM
8830 break;
8831
8832 case 58: /* lwa, ld */
dd9b12c2 8833 if ((insn2 & 1) != 0)
0a1b45a2 8834 return false;
77486630 8835 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8836 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8837 | (insn2 & (31ULL << 21)));
dd9b12c2 8838 off = insn2 & 0xfffc;
4a421c53
AM
8839 break;
8840
8841 case 57: /* lxsd, lxssp */
dd9b12c2 8842 if ((insn2 & 3) < 2)
0a1b45a2 8843 return false;
77486630 8844 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8845 | ((40ULL | (insn2 & 3)) << 26)
8846 | (insn2 & (31ULL << 21)));
dd9b12c2 8847 off = insn2 & 0xfffc;
4a421c53
AM
8848 break;
8849
8850 case 61: /* stxsd, stxssp, lxv, stxv */
8851 if ((insn2 & 3) == 0)
0a1b45a2 8852 return false;
4a421c53
AM
8853 else if ((insn2 & 3) >= 2)
8854 {
77486630 8855 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8856 | ((44ULL | (insn2 & 3)) << 26)
8857 | (insn2 & (31ULL << 21)));
dd9b12c2 8858 off = insn2 & 0xfffc;
4a421c53
AM
8859 }
8860 else
8861 {
77486630 8862 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8863 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8864 | (insn2 & (31ULL << 21)));
dd9b12c2 8865 off = insn2 & 0xfff0;
4a421c53
AM
8866 }
8867 break;
8868
8869 case 56: /* lq */
77486630 8870 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8871 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8872 off = insn2 & 0xffff;
4a421c53
AM
8873 break;
8874
94ba9882
AM
8875 case 6: /* lxvp, stxvp */
8876 if ((insn2 & 0xe) != 0)
0a1b45a2 8877 return false;
94ba9882
AM
8878 insn1 = ((1ULL << 58) | (1ULL << 52)
8879 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8880 | (insn2 & (31ULL << 21)));
8881 off = insn2 & 0xfff0;
8882 break;
8883
4a421c53 8884 case 62: /* std, stq */
dd9b12c2 8885 if ((insn2 & 1) != 0)
0a1b45a2 8886 return false;
77486630 8887 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8888 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8889 | (insn2 & (31ULL << 21)));
dd9b12c2 8890 off = insn2 & 0xfffc;
4a421c53
AM
8891 break;
8892 }
8893
77486630 8894 *pinsn1 = insn1;
4a421c53 8895 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8896 *poff = (off ^ 0x8000) - 0x8000;
0a1b45a2 8897 return true;
4a421c53
AM
8898}
8899
c5614fa4
AM
8900/* Examine all relocs referencing .toc sections in order to remove
8901 unused .toc entries. */
8902
0a1b45a2 8903bool
33c0ec9d 8904ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8905{
8906 bfd *ibfd;
8907 struct adjust_toc_info toc_inf;
67f0cbdb 8908 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8909
67f0cbdb 8910 htab->do_toc_opt = 1;
0a1b45a2 8911 toc_inf.global_toc_syms = true;
c72f2fb2 8912 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8913 {
8914 asection *toc, *sec;
8915 Elf_Internal_Shdr *symtab_hdr;
8916 Elf_Internal_Sym *local_syms;
425b145b 8917 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8918 unsigned long *skip, *drop;
8919 unsigned char *used;
8920 unsigned char *keep, last, some_unused;
8921
854b41e7
AM
8922 if (!is_ppc64_elf (ibfd))
8923 continue;
8924
c5614fa4
AM
8925 toc = bfd_get_section_by_name (ibfd, ".toc");
8926 if (toc == NULL
92b7a70f 8927 || toc->size == 0
dbaa2011
AM
8928 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8929 || discarded_section (toc))
c5614fa4
AM
8930 continue;
8931
425b145b 8932 toc_relocs = NULL;
c5614fa4 8933 local_syms = NULL;
0ffa91dd 8934 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8935
8936 /* Look at sections dropped from the final link. */
8937 skip = NULL;
8938 relstart = NULL;
8939 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8940 {
8941 if (sec->reloc_count == 0
dbaa2011 8942 || !discarded_section (sec)
c5614fa4
AM
8943 || get_opd_info (sec)
8944 || (sec->flags & SEC_ALLOC) == 0
8945 || (sec->flags & SEC_DEBUGGING) != 0)
8946 continue;
8947
0a1b45a2 8948 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
c5614fa4
AM
8949 if (relstart == NULL)
8950 goto error_ret;
8951
8952 /* Run through the relocs to see which toc entries might be
8953 unused. */
8954 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8955 {
8956 enum elf_ppc64_reloc_type r_type;
8957 unsigned long r_symndx;
8958 asection *sym_sec;
8959 struct elf_link_hash_entry *h;
8960 Elf_Internal_Sym *sym;
8961 bfd_vma val;
8962
8963 r_type = ELF64_R_TYPE (rel->r_info);
8964 switch (r_type)
8965 {
8966 default:
8967 continue;
8968
8969 case R_PPC64_TOC16:
8970 case R_PPC64_TOC16_LO:
8971 case R_PPC64_TOC16_HI:
8972 case R_PPC64_TOC16_HA:
8973 case R_PPC64_TOC16_DS:
8974 case R_PPC64_TOC16_LO_DS:
8975 break;
8976 }
8977
8978 r_symndx = ELF64_R_SYM (rel->r_info);
8979 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8980 r_symndx, ibfd))
8981 goto error_ret;
8982
8983 if (sym_sec != toc)
8984 continue;
8985
8986 if (h != NULL)
8987 val = h->root.u.def.value;
8988 else
8989 val = sym->st_value;
8990 val += rel->r_addend;
8991
8992 if (val >= toc->size)
8993 continue;
8994
8995 /* Anything in the toc ought to be aligned to 8 bytes.
8996 If not, don't mark as unused. */
8997 if (val & 7)
8998 continue;
8999
9000 if (skip == NULL)
9001 {
854b41e7 9002 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9003 if (skip == NULL)
9004 goto error_ret;
9005 }
9006
ba761f19 9007 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9008 }
9009
9010 if (elf_section_data (sec)->relocs != relstart)
9011 free (relstart);
9012 }
9013
ba761f19
AM
9014 /* For largetoc loads of address constants, we can convert
9015 . addis rx,2,addr@got@ha
9016 . ld ry,addr@got@l(rx)
9017 to
9018 . addis rx,2,addr@toc@ha
9019 . addi ry,rx,addr@toc@l
9020 when addr is within 2G of the toc pointer. This then means
9021 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9022
ba761f19
AM
9023 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9024 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9025 && toc->reloc_count != 0)
9026 {
9027 /* Read toc relocs. */
425b145b
AM
9028 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9029 info->keep_memory);
9030 if (toc_relocs == NULL)
ba761f19
AM
9031 goto error_ret;
9032
425b145b 9033 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9034 {
9035 enum elf_ppc64_reloc_type r_type;
9036 unsigned long r_symndx;
9037 asection *sym_sec;
9038 struct elf_link_hash_entry *h;
9039 Elf_Internal_Sym *sym;
9040 bfd_vma val, addr;
9041
9042 r_type = ELF64_R_TYPE (rel->r_info);
9043 if (r_type != R_PPC64_ADDR64)
9044 continue;
9045
9046 r_symndx = ELF64_R_SYM (rel->r_info);
9047 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9048 r_symndx, ibfd))
9049 goto error_ret;
9050
425b145b 9051 if (sym_sec == NULL
c27b8c2a 9052 || sym_sec->output_section == NULL
dbaa2011 9053 || discarded_section (sym_sec))
425b145b
AM
9054 continue;
9055
3a3a4c1f
AM
9056 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9057 || (bfd_link_pic (info)
9058 && sym_sec == bfd_abs_section_ptr))
ba761f19
AM
9059 continue;
9060
9061 if (h != NULL)
bddc25c9
AM
9062 {
9063 if (h->type == STT_GNU_IFUNC)
9064 continue;
9065 val = h->root.u.def.value;
9066 }
ba761f19 9067 else
bddc25c9
AM
9068 {
9069 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9070 continue;
9071 val = sym->st_value;
9072 }
ba761f19
AM
9073 val += rel->r_addend;
9074 val += sym_sec->output_section->vma + sym_sec->output_offset;
9075
9076 /* We don't yet know the exact toc pointer value, but we
9077 know it will be somewhere in the toc section. Don't
9078 optimize if the difference from any possible toc
9079 pointer is outside [ff..f80008000, 7fff7fff]. */
9080 addr = toc->output_section->vma + TOC_BASE_OFF;
9081 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9082 continue;
9083
9084 addr = toc->output_section->vma + toc->output_section->rawsize;
9085 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9086 continue;
9087
9088 if (skip == NULL)
9089 {
9090 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9091 if (skip == NULL)
9092 goto error_ret;
9093 }
9094
9095 skip[rel->r_offset >> 3]
425b145b 9096 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9097 }
ba761f19
AM
9098 }
9099
c5614fa4
AM
9100 if (skip == NULL)
9101 continue;
9102
9103 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9104 if (used == NULL)
9105 {
9106 error_ret:
c9594989 9107 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
9108 free (local_syms);
9109 if (sec != NULL
c5614fa4
AM
9110 && elf_section_data (sec)->relocs != relstart)
9111 free (relstart);
c9594989 9112 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9113 free (toc_relocs);
c9594989 9114 free (skip);
0a1b45a2 9115 return false;
c5614fa4
AM
9116 }
9117
30038c59
AM
9118 /* Now check all kept sections that might reference the toc.
9119 Check the toc itself last. */
9120 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9121 : ibfd->sections);
c5614fa4 9122 sec != NULL;
c5614fa4 9123 sec = (sec == toc ? NULL
c5614fa4 9124 : sec->next == NULL ? toc
30038c59 9125 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9126 : sec->next))
9127 {
9128 int repeat;
9129
9130 if (sec->reloc_count == 0
dbaa2011 9131 || discarded_section (sec)
c5614fa4
AM
9132 || get_opd_info (sec)
9133 || (sec->flags & SEC_ALLOC) == 0
9134 || (sec->flags & SEC_DEBUGGING) != 0)
9135 continue;
9136
854b41e7
AM
9137 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9138 info->keep_memory);
c5614fa4 9139 if (relstart == NULL)
2915c55b
JK
9140 {
9141 free (used);
9142 goto error_ret;
9143 }
c5614fa4
AM
9144
9145 /* Mark toc entries referenced as used. */
c5614fa4 9146 do
d4f1ee75
AM
9147 {
9148 repeat = 0;
9149 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9150 {
9151 enum elf_ppc64_reloc_type r_type;
9152 unsigned long r_symndx;
9153 asection *sym_sec;
9154 struct elf_link_hash_entry *h;
9155 Elf_Internal_Sym *sym;
9156 bfd_vma val;
98528052 9157
d4f1ee75 9158 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
9159 switch (r_type)
9160 {
9161 case R_PPC64_TOC16:
9162 case R_PPC64_TOC16_LO:
9163 case R_PPC64_TOC16_HI:
9164 case R_PPC64_TOC16_HA:
9165 case R_PPC64_TOC16_DS:
9166 case R_PPC64_TOC16_LO_DS:
9167 /* In case we're taking addresses of toc entries. */
9168 case R_PPC64_ADDR64:
9169 break;
c5614fa4 9170
d4f1ee75
AM
9171 default:
9172 continue;
9173 }
c5614fa4 9174
d4f1ee75
AM
9175 r_symndx = ELF64_R_SYM (rel->r_info);
9176 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9177 r_symndx, ibfd))
9178 {
9179 free (used);
9180 goto error_ret;
9181 }
c5614fa4 9182
d4f1ee75
AM
9183 if (sym_sec != toc)
9184 continue;
c5614fa4 9185
d4f1ee75
AM
9186 if (h != NULL)
9187 val = h->root.u.def.value;
9188 else
9189 val = sym->st_value;
9190 val += rel->r_addend;
ba761f19 9191
d4f1ee75
AM
9192 if (val >= toc->size)
9193 continue;
ba761f19 9194
d4f1ee75
AM
9195 if ((skip[val >> 3] & can_optimize) != 0)
9196 {
9197 bfd_vma off;
9198 unsigned char opc;
9199
9200 switch (r_type)
9201 {
9202 case R_PPC64_TOC16_HA:
ba761f19 9203 break;
ba761f19 9204
d4f1ee75
AM
9205 case R_PPC64_TOC16_LO_DS:
9206 off = rel->r_offset;
9207 off += (bfd_big_endian (ibfd) ? -2 : 3);
9208 if (!bfd_get_section_contents (ibfd, sec, &opc,
9209 off, 1))
9210 {
9211 free (used);
9212 goto error_ret;
9213 }
9214 if ((opc & (0x3f << 2)) == (58u << 2))
9215 break;
1a0670f3 9216 /* Fall through. */
ba761f19 9217
d4f1ee75
AM
9218 default:
9219 /* Wrong sort of reloc, or not a ld. We may
9220 as well clear ref_from_discarded too. */
9221 skip[val >> 3] = 0;
9222 }
9223 }
9224
9225 if (sec != toc)
9226 used[val >> 3] = 1;
9227 /* For the toc section, we only mark as used if this
9228 entry itself isn't unused. */
9229 else if ((used[rel->r_offset >> 3]
9230 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9231 && !used[val >> 3])
9232 {
9233 /* Do all the relocs again, to catch reference
9234 chains. */
9235 repeat = 1;
9236 used[val >> 3] = 1;
9237 }
9238 }
9239 }
c5614fa4 9240 while (repeat);
854b41e7
AM
9241
9242 if (elf_section_data (sec)->relocs != relstart)
9243 free (relstart);
c5614fa4
AM
9244 }
9245
9246 /* Merge the used and skip arrays. Assume that TOC
9247 doublewords not appearing as either used or unused belong
de194d85 9248 to an entry more than one doubleword in size. */
c5614fa4
AM
9249 for (drop = skip, keep = used, last = 0, some_unused = 0;
9250 drop < skip + (toc->size + 7) / 8;
9251 ++drop, ++keep)
9252 {
9253 if (*keep)
9254 {
ba761f19
AM
9255 *drop &= ~ref_from_discarded;
9256 if ((*drop & can_optimize) != 0)
9257 some_unused = 1;
c5614fa4
AM
9258 last = 0;
9259 }
b140b010 9260 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9261 {
9262 some_unused = 1;
ba761f19 9263 last = ref_from_discarded;
c5614fa4
AM
9264 }
9265 else
9266 *drop = last;
9267 }
9268
9269 free (used);
9270
9271 if (some_unused)
9272 {
9273 bfd_byte *contents, *src;
9274 unsigned long off;
d62b3684 9275 Elf_Internal_Sym *sym;
0a1b45a2 9276 bool local_toc_syms = false;
c5614fa4
AM
9277
9278 /* Shuffle the toc contents, and at the same time convert the
9279 skip array from booleans into offsets. */
9280 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9281 goto error_ret;
9282
9283 elf_section_data (toc)->this_hdr.contents = contents;
9284
9285 for (src = contents, off = 0, drop = skip;
9286 src < contents + toc->size;
9287 src += 8, ++drop)
9288 {
ba761f19
AM
9289 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9290 off += 8;
c5614fa4
AM
9291 else if (off != 0)
9292 {
9293 *drop = off;
9294 memcpy (src - off, src, 8);
9295 }
9296 }
854b41e7 9297 *drop = off;
c5614fa4
AM
9298 toc->rawsize = toc->size;
9299 toc->size = src - contents - off;
9300
ba761f19
AM
9301 /* Adjust addends for relocs against the toc section sym,
9302 and optimize any accesses we can. */
c5614fa4
AM
9303 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9304 {
9305 if (sec->reloc_count == 0
dbaa2011 9306 || discarded_section (sec))
c5614fa4
AM
9307 continue;
9308
9309 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9310 info->keep_memory);
c5614fa4
AM
9311 if (relstart == NULL)
9312 goto error_ret;
9313
9314 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9315 {
9316 enum elf_ppc64_reloc_type r_type;
9317 unsigned long r_symndx;
9318 asection *sym_sec;
9319 struct elf_link_hash_entry *h;
854b41e7 9320 bfd_vma val;
c5614fa4
AM
9321
9322 r_type = ELF64_R_TYPE (rel->r_info);
9323 switch (r_type)
9324 {
9325 default:
9326 continue;
9327
9328 case R_PPC64_TOC16:
9329 case R_PPC64_TOC16_LO:
9330 case R_PPC64_TOC16_HI:
9331 case R_PPC64_TOC16_HA:
9332 case R_PPC64_TOC16_DS:
9333 case R_PPC64_TOC16_LO_DS:
9334 case R_PPC64_ADDR64:
9335 break;
9336 }
9337
9338 r_symndx = ELF64_R_SYM (rel->r_info);
9339 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9340 r_symndx, ibfd))
9341 goto error_ret;
9342
ba761f19 9343 if (sym_sec != toc)
c5614fa4
AM
9344 continue;
9345
ba761f19
AM
9346 if (h != NULL)
9347 val = h->root.u.def.value;
9348 else
9349 {
9350 val = sym->st_value;
9351 if (val != 0)
0a1b45a2 9352 local_toc_syms = true;
ba761f19
AM
9353 }
9354
9355 val += rel->r_addend;
854b41e7
AM
9356
9357 if (val > toc->rawsize)
9358 val = toc->rawsize;
ba761f19
AM
9359 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9360 continue;
9361 else if ((skip[val >> 3] & can_optimize) != 0)
9362 {
9363 Elf_Internal_Rela *tocrel
425b145b 9364 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9365 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9366
9367 switch (r_type)
9368 {
9369 case R_PPC64_TOC16_HA:
9370 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9371 break;
9372
9373 case R_PPC64_TOC16_LO_DS:
9374 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9375 break;
9376
9377 default:
28942f62
AM
9378 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9379 ppc_howto_init ();
b140b010 9380 info->callbacks->einfo
695344c0 9381 /* xgettext:c-format */
174d0a74 9382 (_("%H: %s references "
b140b010
AM
9383 "optimized away TOC entry\n"),
9384 ibfd, sec, rel->r_offset,
9385 ppc64_elf_howto_table[r_type]->name);
9386 bfd_set_error (bfd_error_bad_value);
9387 goto error_ret;
ba761f19
AM
9388 }
9389 rel->r_addend = tocrel->r_addend;
9390 elf_section_data (sec)->relocs = relstart;
9391 continue;
9392 }
9393
9394 if (h != NULL || sym->st_value != 0)
9395 continue;
854b41e7
AM
9396
9397 rel->r_addend -= skip[val >> 3];
9398 elf_section_data (sec)->relocs = relstart;
c5614fa4 9399 }
854b41e7
AM
9400
9401 if (elf_section_data (sec)->relocs != relstart)
9402 free (relstart);
c5614fa4
AM
9403 }
9404
9405 /* We shouldn't have local or global symbols defined in the TOC,
9406 but handle them anyway. */
df22d223
AM
9407 if (local_syms != NULL)
9408 for (sym = local_syms;
9409 sym < local_syms + symtab_hdr->sh_info;
9410 ++sym)
9411 if (sym->st_value != 0
9412 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9413 {
9414 unsigned long i;
854b41e7 9415
df22d223
AM
9416 if (sym->st_value > toc->rawsize)
9417 i = toc->rawsize >> 3;
9418 else
9419 i = sym->st_value >> 3;
854b41e7 9420
df22d223
AM
9421 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9422 {
9423 if (local_toc_syms)
4eca0228 9424 _bfd_error_handler
df22d223
AM
9425 (_("%s defined on removed toc entry"),
9426 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9427 do
9428 ++i;
9429 while ((skip[i] & (ref_from_discarded | can_optimize)));
9430 sym->st_value = (bfd_vma) i << 3;
9431 }
d62b3684 9432
df22d223
AM
9433 sym->st_value -= skip[i];
9434 symtab_hdr->contents = (unsigned char *) local_syms;
9435 }
c5614fa4 9436
854b41e7 9437 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9438 if (toc_inf.global_toc_syms)
9439 {
9440 toc_inf.toc = toc;
9441 toc_inf.skip = skip;
0a1b45a2 9442 toc_inf.global_toc_syms = false;
c5614fa4
AM
9443 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9444 &toc_inf);
9445 }
854b41e7
AM
9446
9447 if (toc->reloc_count != 0)
9448 {
d4730f92 9449 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9450 Elf_Internal_Rela *wrel;
9451 bfd_size_type sz;
9452
854b41e7 9453 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9454 if (toc_relocs == NULL)
9455 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9456 info->keep_memory);
9457 if (toc_relocs == NULL)
9458 goto error_ret;
9459
425b145b
AM
9460 wrel = toc_relocs;
9461 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9462 if ((skip[rel->r_offset >> 3]
9463 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9464 {
9465 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9466 wrel->r_info = rel->r_info;
9467 wrel->r_addend = rel->r_addend;
9468 ++wrel;
9469 }
1657026c 9470 else if (!dec_dynrel_count (rel, toc, info,
854b41e7
AM
9471 &local_syms, NULL, NULL))
9472 goto error_ret;
9473
425b145b
AM
9474 elf_section_data (toc)->relocs = toc_relocs;
9475 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9476 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9477 sz = rel_hdr->sh_entsize;
9478 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9479 }
c5614fa4 9480 }
c9594989 9481 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9482 free (toc_relocs);
c5614fa4
AM
9483
9484 if (local_syms != NULL
9485 && symtab_hdr->contents != (unsigned char *) local_syms)
9486 {
9487 if (!info->keep_memory)
9488 free (local_syms);
9489 else
9490 symtab_hdr->contents = (unsigned char *) local_syms;
9491 }
9492 free (skip);
9493 }
9494
066f4018 9495 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9496 a GOT relative or PC relative access, possibly reducing the
9497 number of GOT entries. */
066f4018
AM
9498 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9499 {
9500 asection *sec;
9501 Elf_Internal_Shdr *symtab_hdr;
9502 Elf_Internal_Sym *local_syms;
9503 Elf_Internal_Rela *relstart, *rel;
9504 bfd_vma got;
9505
9506 if (!is_ppc64_elf (ibfd))
9507 continue;
9508
903b777d 9509 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9510 continue;
9511
9512 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9513 got = 0;
9514 if (sec != NULL)
9515 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9516
9517 local_syms = NULL;
9518 symtab_hdr = &elf_symtab_hdr (ibfd);
9519
9520 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9521 {
9522 if (sec->reloc_count == 0
903b777d 9523 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9524 || discarded_section (sec))
9525 continue;
9526
9527 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9528 info->keep_memory);
9529 if (relstart == NULL)
9530 {
9531 got_error_ret:
c9594989 9532 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9533 free (local_syms);
9534 if (sec != NULL
066f4018
AM
9535 && elf_section_data (sec)->relocs != relstart)
9536 free (relstart);
0a1b45a2 9537 return false;
066f4018
AM
9538 }
9539
9540 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9541 {
9542 enum elf_ppc64_reloc_type r_type;
9543 unsigned long r_symndx;
9544 Elf_Internal_Sym *sym;
9545 asection *sym_sec;
9546 struct elf_link_hash_entry *h;
9547 struct got_entry *ent;
133a1f60 9548 bfd_vma val, pc;
4a421c53 9549 unsigned char buf[8];
066f4018 9550 unsigned int insn;
903b777d 9551 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9552
9553 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9554 switch (r_type)
9555 {
9556 default:
9557 insn_check = no_check;
9558 break;
9559
9560 case R_PPC64_PLT16_HA:
9561 case R_PPC64_GOT_TLSLD16_HA:
9562 case R_PPC64_GOT_TLSGD16_HA:
9563 case R_PPC64_GOT_TPREL16_HA:
9564 case R_PPC64_GOT_DTPREL16_HA:
9565 case R_PPC64_GOT16_HA:
9566 case R_PPC64_TOC16_HA:
9567 insn_check = check_ha;
9568 break;
9569
9570 case R_PPC64_PLT16_LO:
9571 case R_PPC64_PLT16_LO_DS:
9572 case R_PPC64_GOT_TLSLD16_LO:
9573 case R_PPC64_GOT_TLSGD16_LO:
9574 case R_PPC64_GOT_TPREL16_LO_DS:
9575 case R_PPC64_GOT_DTPREL16_LO_DS:
9576 case R_PPC64_GOT16_LO:
9577 case R_PPC64_GOT16_LO_DS:
9578 case R_PPC64_TOC16_LO:
9579 case R_PPC64_TOC16_LO_DS:
9580 insn_check = check_lo;
9581 break;
9582 }
9583
9584 if (insn_check != no_check)
9585 {
9586 bfd_vma off = rel->r_offset & ~3;
9587
9588 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9589 goto got_error_ret;
9590
9591 insn = bfd_get_32 (ibfd, buf);
9592 if (insn_check == check_lo
9593 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9594 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9595 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9596 {
9597 char str[12];
9598
9599 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9600 sprintf (str, "%#08x", insn);
9601 info->callbacks->einfo
9602 /* xgettext:c-format */
9603 (_("%H: got/toc optimization is not supported for"
9604 " %s instruction\n"),
9605 ibfd, sec, rel->r_offset & ~3, str);
9606 continue;
9607 }
9608 }
9609
066f4018
AM
9610 switch (r_type)
9611 {
bb22a418
AM
9612 /* Note that we don't delete GOT entries for
9613 R_PPC64_GOT16_DS since we'd need a lot more
9614 analysis. For starters, the preliminary layout is
9615 before the GOT, PLT, dynamic sections and stubs are
9616 laid out. Then we'd need to allow for changes in
9617 distance between sections caused by alignment. */
066f4018
AM
9618 default:
9619 continue;
9620
066f4018
AM
9621 case R_PPC64_GOT16_HA:
9622 case R_PPC64_GOT16_LO_DS:
4a421c53 9623 case R_PPC64_GOT_PCREL34:
066f4018
AM
9624 break;
9625 }
9626
9627 r_symndx = ELF64_R_SYM (rel->r_info);
9628 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9629 r_symndx, ibfd))
9630 goto got_error_ret;
9631
6d5554a6
AM
9632 if (sym_sec == NULL
9633 || sym_sec->output_section == NULL
9634 || discarded_section (sym_sec))
9635 continue;
9636
06507dab
AM
9637 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9638 continue;
9639
3a3a4c1f
AM
9640 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9641 || (bfd_link_pic (info)
9642 && sym_sec == bfd_abs_section_ptr))
066f4018
AM
9643 continue;
9644
9645 if (h != NULL)
9646 val = h->root.u.def.value;
9647 else
9648 val = sym->st_value;
133a1f60 9649 val += rel->r_addend;
066f4018
AM
9650 val += sym_sec->output_section->vma + sym_sec->output_offset;
9651
bb22a418
AM
9652/* Fudge factor to allow for the fact that the preliminary layout
9653 isn't exact. Reduce limits by this factor. */
9654#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9655
066f4018
AM
9656 switch (r_type)
9657 {
9658 default:
9659 continue;
9660
066f4018 9661 case R_PPC64_GOT16_HA:
bb22a418
AM
9662 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9663 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9664 continue;
9665
9666 if (!bfd_get_section_contents (ibfd, sec, buf,
9667 rel->r_offset & ~3, 4))
9668 goto got_error_ret;
9669 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9670 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9671 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9672 continue;
9673 break;
9674
9675 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9676 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9677 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9678 continue;
9679 if (!bfd_get_section_contents (ibfd, sec, buf,
9680 rel->r_offset & ~3, 4))
9681 goto got_error_ret;
9682 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9683 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9684 continue;
9685 break;
4a421c53
AM
9686
9687 case R_PPC64_GOT_PCREL34:
9688 pc = rel->r_offset;
9689 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9690 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9691 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9692 continue;
9693 if (!bfd_get_section_contents (ibfd, sec, buf,
9694 rel->r_offset & ~3, 8))
9695 goto got_error_ret;
9696 insn = bfd_get_32 (ibfd, buf);
9697 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9698 continue;
9699 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9700 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9701 continue;
9702 break;
066f4018 9703 }
bb22a418 9704#undef LIMIT_ADJUST
066f4018
AM
9705
9706 if (h != NULL)
9707 ent = h->got.glist;
9708 else
9709 {
9710 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9711 ent = local_got_ents[r_symndx];
9712 }
9713 for (; ent != NULL; ent = ent->next)
133a1f60 9714 if (ent->addend == rel->r_addend
066f4018
AM
9715 && ent->owner == ibfd
9716 && ent->tls_type == 0)
9717 break;
9718 BFD_ASSERT (ent && ent->got.refcount > 0);
9719 ent->got.refcount -= 1;
9720 }
9721
9722 if (elf_section_data (sec)->relocs != relstart)
9723 free (relstart);
9724 }
9725
9726 if (local_syms != NULL
9727 && symtab_hdr->contents != (unsigned char *) local_syms)
9728 {
9729 if (!info->keep_memory)
9730 free (local_syms);
9731 else
9732 symtab_hdr->contents = (unsigned char *) local_syms;
9733 }
9734 }
9735
0a1b45a2 9736 return true;
c5614fa4
AM
9737}
9738
1bbe0902
AM
9739/* Return true iff input section I references the TOC using
9740 instructions limited to +/-32k offsets. */
9741
0a1b45a2 9742bool
1bbe0902
AM
9743ppc64_elf_has_small_toc_reloc (asection *i)
9744{
9745 return (is_ppc64_elf (i->owner)
9746 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9747}
9748
927be08e
AM
9749/* Allocate space for one GOT entry. */
9750
9751static void
9752allocate_got (struct elf_link_hash_entry *h,
9753 struct bfd_link_info *info,
9754 struct got_entry *gent)
9755{
9756 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9757 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9758 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9759 ? 16 : 8);
9760 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9761 ? 2 : 1) * sizeof (Elf64_External_Rela);
9762 asection *got = ppc64_elf_tdata (gent->owner)->got;
9763
9764 gent->got.offset = got->size;
9765 got->size += entsize;
9766
19e08130 9767 if (h->type == STT_GNU_IFUNC)
927be08e 9768 {
33e44f2e 9769 htab->elf.irelplt->size += rentsize;
19e08130 9770 htab->got_reli_size += rentsize;
927be08e 9771 }
f15d0b54 9772 else if (((bfd_link_pic (info)
1657026c
AM
9773 && (gent->tls_type == 0
9774 ? !info->enable_dt_relr
9775 : !(bfd_link_executable (info)
3a3a4c1f
AM
9776 && SYMBOL_REFERENCES_LOCAL (info, h)))
9777 && !bfd_is_abs_symbol (&h->root))
f0158f44
AM
9778 || (htab->elf.dynamic_sections_created
9779 && h->dynindx != -1
9780 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9781 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9782 {
19e08130 9783 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9784 relgot->size += rentsize;
927be08e
AM
9785 }
9786}
9787
7865406b
AM
9788/* This function merges got entries in the same toc group. */
9789
9790static void
9791merge_got_entries (struct got_entry **pent)
9792{
9793 struct got_entry *ent, *ent2;
9794
9795 for (ent = *pent; ent != NULL; ent = ent->next)
9796 if (!ent->is_indirect)
9797 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9798 if (!ent2->is_indirect
9799 && ent2->addend == ent->addend
9800 && ent2->tls_type == ent->tls_type
9801 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9802 {
0a1b45a2 9803 ent2->is_indirect = true;
7865406b
AM
9804 ent2->got.ent = ent;
9805 }
9806}
9807
46434633 9808/* If H is undefined, make it dynamic if that makes sense. */
f0158f44 9809
0a1b45a2 9810static bool
46434633
AM
9811ensure_undef_dynamic (struct bfd_link_info *info,
9812 struct elf_link_hash_entry *h)
f0158f44
AM
9813{
9814 struct elf_link_hash_table *htab = elf_hash_table (info);
9815
9816 if (htab->dynamic_sections_created
46434633
AM
9817 && ((info->dynamic_undefined_weak != 0
9818 && h->root.type == bfd_link_hash_undefweak)
9819 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9820 && h->dynindx == -1
9821 && !h->forced_local
9822 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9823 return bfd_elf_link_record_dynamic_symbol (info, h);
0a1b45a2 9824 return true;
f0158f44
AM
9825}
9826
30845f11
AM
9827/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9828 usual htab->elf.splt section for a PLT entry. */
9829
9830static inline
0a1b45a2 9831bool use_local_plt (struct bfd_link_info *info,
30845f11
AM
9832 struct elf_link_hash_entry *h)
9833{
9834 return (h == NULL
9835 || h->dynindx == -1
9836 || !elf_hash_table (info)->dynamic_sections_created);
9837}
9838
65f38f15
AM
9839/* Allocate space in .plt, .got and associated reloc sections for
9840 dynamic relocs. */
5bd4f169 9841
0a1b45a2 9842static bool
4ce794b7 9843allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9844{
65f38f15
AM
9845 struct bfd_link_info *info;
9846 struct ppc_link_hash_table *htab;
5bd4f169 9847 asection *s;
65f38f15 9848 struct ppc_link_hash_entry *eh;
0b8bcf0d 9849 struct got_entry **pgent, *gent;
5bd4f169 9850
e92d460e 9851 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9852 return true;
5bd4f169 9853
65f38f15
AM
9854 info = (struct bfd_link_info *) inf;
9855 htab = ppc_hash_table (info);
4dfe6ac6 9856 if (htab == NULL)
0a1b45a2 9857 return false;
5bd4f169 9858
ed7007c1 9859 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9860 /* Run through the TLS GD got entries first if we're changing them
9861 to TPREL. */
b00a0a86 9862 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9863 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9864 if (gent->got.refcount > 0
9865 && (gent->tls_type & TLS_GD) != 0)
9866 {
9867 /* This was a GD entry that has been converted to TPREL. If
9868 there happens to be a TPREL entry we can use that one. */
9869 struct got_entry *ent;
9870 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9871 if (ent->got.refcount > 0
9872 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9873 && ent->addend == gent->addend
9874 && ent->owner == gent->owner)
951fd09b
AM
9875 {
9876 gent->got.refcount = 0;
9877 break;
9878 }
9879
9880 /* If not, then we'll be using our own TPREL entry. */
9881 if (gent->got.refcount != 0)
9882 gent->tls_type = TLS_TLS | TLS_TPREL;
9883 }
9884
7865406b
AM
9885 /* Remove any list entry that won't generate a word in the GOT before
9886 we call merge_got_entries. Otherwise we risk merging to empty
9887 entries. */
0b8bcf0d
AM
9888 pgent = &h->got.glist;
9889 while ((gent = *pgent) != NULL)
411e1bfb 9890 if (gent->got.refcount > 0)
7865406b
AM
9891 {
9892 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9893 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9894 {
9895 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9896 *pgent = gent->next;
9897 }
9898 else
9899 pgent = &gent->next;
9900 }
9901 else
9902 *pgent = gent->next;
9903
9904 if (!htab->do_multi_toc)
9905 merge_got_entries (&h->got.glist);
9906
9907 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9908 if (!gent->is_indirect)
411e1bfb 9909 {
ec73ddcd
AM
9910 /* Ensure we catch all the cases where this symbol should
9911 be made dynamic. */
46434633 9912 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9913 return false;
65f38f15 9914
0c8d6e5c 9915 if (!is_ppc64_elf (gent->owner))
927be08e 9916 abort ();
0ffa91dd 9917
927be08e 9918 allocate_got (h, info, gent);
411e1bfb 9919 }
65f38f15 9920
954b63d4
AM
9921 /* If no dynamic sections we can't have dynamic relocs, except for
9922 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9923 if (!htab->elf.dynamic_sections_created
9924 && h->type != STT_GNU_IFUNC)
190eb1dd 9925 h->dyn_relocs = NULL;
8a2058b5 9926
529fe20e
AM
9927 /* Discard relocs on undefined symbols that must be local. */
9928 else if (h->root.type == bfd_link_hash_undefined
9929 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9930 h->dyn_relocs = NULL;
529fe20e 9931
954b63d4
AM
9932 /* Also discard relocs on undefined weak syms with non-default
9933 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9934 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9935 h->dyn_relocs = NULL;
954b63d4 9936
190eb1dd 9937 if (h->dyn_relocs != NULL)
65f38f15 9938 {
1657026c 9939 struct ppc_dyn_relocs *p, **pp;
8a2058b5 9940
57e7d118
AM
9941 /* In the shared -Bsymbolic case, discard space allocated for
9942 dynamic pc-relative relocs against symbols which turn out to
9943 be defined in regular objects. For the normal shared case,
9944 discard space for relocs that have become local due to symbol
9945 visibility changes. */
57e7d118 9946 if (bfd_link_pic (info))
65f38f15 9947 {
57e7d118
AM
9948 /* Relocs that use pc_count are those that appear on a call
9949 insn, or certain REL relocs (see must_be_dyn_reloc) that
9950 can be generated via assembly. We want calls to
9951 protected symbols to resolve directly to the function
9952 rather than going via the plt. If people want function
9953 pointer comparisons to work as expected then they should
9954 avoid writing weird assembly. */
9955 if (SYMBOL_CALLS_LOCAL (info, h))
9956 {
1657026c
AM
9957 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9958 (p = *pp) != NULL;
9959 )
57e7d118
AM
9960 {
9961 p->count -= p->pc_count;
9962 p->pc_count = 0;
9963 if (p->count == 0)
9964 *pp = p->next;
9965 else
9966 pp = &p->next;
9967 }
9968 }
65f38f15 9969
190eb1dd 9970 if (h->dyn_relocs != NULL)
5bd4f169 9971 {
ec73ddcd
AM
9972 /* Ensure we catch all the cases where this symbol
9973 should be made dynamic. */
46434633 9974 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9975 return false;
5bd4f169 9976 }
65f38f15 9977 }
ec73ddcd
AM
9978
9979 /* For a fixed position executable, discard space for
9980 relocs against symbols which are not dynamic. */
9981 else if (h->type != STT_GNU_IFUNC)
57e7d118 9982 {
49160308
AM
9983 if ((h->dynamic_adjusted
9984 || (h->ref_regular
9985 && h->root.type == bfd_link_hash_undefweak
9986 && (info->dynamic_undefined_weak > 0
9987 || !_bfd_elf_readonly_dynrelocs (h))))
529fe20e
AM
9988 && !h->def_regular
9989 && !ELF_COMMON_DEF_P (h))
f0158f44 9990 {
ec73ddcd
AM
9991 /* Ensure we catch all the cases where this symbol
9992 should be made dynamic. */
46434633 9993 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9994 return false;
dfbb6ac9 9995
ec73ddcd 9996 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 9997 if (h->dynindx == -1)
190eb1dd 9998 h->dyn_relocs = NULL;
f0158f44
AM
9999 }
10000 else
190eb1dd 10001 h->dyn_relocs = NULL;
57e7d118
AM
10002 }
10003
10004 /* Finally, allocate space. */
1657026c 10005 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
57e7d118 10006 {
1657026c 10007 unsigned int count;
57e7d118
AM
10008 asection *sreloc = elf_section_data (p->sec)->sreloc;
10009 if (eh->elf.type == STT_GNU_IFUNC)
10010 sreloc = htab->elf.irelplt;
1657026c 10011 count = p->count;
8ffb6df2
AM
10012 if (info->enable_dt_relr
10013 && ((!NO_OPD_RELOCS
10014 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10015 || (eh->elf.type != STT_GNU_IFUNC
10016 && SYMBOL_REFERENCES_LOCAL (info, h))))
1657026c
AM
10017 count -= p->rel_count;
10018 sreloc->size += count * sizeof (Elf64_External_Rela);
dfbb6ac9 10019 }
65f38f15 10020 }
57e7d118 10021
2d7ad24e
AM
10022 /* We might need a PLT entry when the symbol
10023 a) is dynamic, or
10024 b) is an ifunc, or
10025 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10026 d) has plt16 relocs and we are linking statically. */
10027 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10028 || h->type == STT_GNU_IFUNC
10029 || (h->needs_plt && h->dynamic_adjusted)
10030 || (h->needs_plt
10031 && h->def_regular
10032 && !htab->elf.dynamic_sections_created
3e04d765 10033 && !htab->can_convert_all_inline_plt
ed7007c1 10034 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 10035 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10036 {
57e7d118 10037 struct plt_entry *pent;
0a1b45a2 10038 bool doneone = false;
57e7d118
AM
10039 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10040 if (pent->plt.refcount > 0)
10041 {
b2936612
AM
10042 if (!ensure_undef_dynamic (info, h))
10043 return false;
10044
30845f11 10045 if (use_local_plt (info, h))
57e7d118 10046 {
2d7ad24e
AM
10047 if (h->type == STT_GNU_IFUNC)
10048 {
10049 s = htab->elf.iplt;
10050 pent->plt.offset = s->size;
10051 s->size += PLT_ENTRY_SIZE (htab);
10052 s = htab->elf.irelplt;
10053 }
10054 else
10055 {
10056 s = htab->pltlocal;
10057 pent->plt.offset = s->size;
10058 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
1657026c
AM
10059 s = NULL;
10060 if (bfd_link_pic (info)
10061 && !(info->enable_dt_relr && !htab->opd_abi))
10062 s = htab->relpltlocal;
2d7ad24e 10063 }
57e7d118
AM
10064 }
10065 else
10066 {
10067 /* If this is the first .plt entry, make room for the special
10068 first entry. */
10069 s = htab->elf.splt;
10070 if (s->size == 0)
10071 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10072
57e7d118 10073 pent->plt.offset = s->size;
65f38f15 10074
57e7d118
AM
10075 /* Make room for this entry. */
10076 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10077
57e7d118
AM
10078 /* Make room for the .glink code. */
10079 s = htab->glink;
10080 if (s->size == 0)
9e390558 10081 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10082 if (htab->opd_abi)
10083 {
10084 /* We need bigger stubs past index 32767. */
9e390558 10085 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10086 s->size += 4;
10087 s->size += 2*4;
10088 }
10089 else
10090 s->size += 4;
65f38f15 10091
57e7d118
AM
10092 /* We also need to make an entry in the .rela.plt section. */
10093 s = htab->elf.srelplt;
10094 }
2d7ad24e
AM
10095 if (s != NULL)
10096 s->size += sizeof (Elf64_External_Rela);
0a1b45a2 10097 doneone = true;
57e7d118
AM
10098 }
10099 else
10100 pent->plt.offset = (bfd_vma) -1;
10101 if (!doneone)
10102 {
10103 h->plt.plist = NULL;
10104 h->needs_plt = 0;
10105 }
65f38f15 10106 }
57e7d118 10107 else
65f38f15 10108 {
57e7d118
AM
10109 h->plt.plist = NULL;
10110 h->needs_plt = 0;
65f38f15
AM
10111 }
10112
0a1b45a2 10113 return true;
65f38f15
AM
10114}
10115
9e390558
AM
10116#define PPC_LO(v) ((v) & 0xffff)
10117#define PPC_HI(v) (((v) >> 16) & 0xffff)
10118#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
10119#define D34(v) \
10120 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10121#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 10122
a345bc8d
AM
10123/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10124 to set up space for global entry stubs. These are put in glink,
10125 after the branch table. */
65f38f15 10126
0a1b45a2 10127static bool
a345bc8d 10128size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10129{
a345bc8d
AM
10130 struct bfd_link_info *info;
10131 struct ppc_link_hash_table *htab;
10132 struct plt_entry *pent;
9e390558 10133 asection *s, *plt;
65f38f15 10134
a345bc8d 10135 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 10136 return true;
65f38f15 10137
a345bc8d 10138 if (!h->pointer_equality_needed)
0a1b45a2 10139 return true;
65f38f15 10140
a345bc8d 10141 if (h->def_regular)
0a1b45a2 10142 return true;
65f38f15 10143
a345bc8d
AM
10144 info = inf;
10145 htab = ppc_hash_table (info);
10146 if (htab == NULL)
0a1b45a2 10147 return false;
a345bc8d 10148
9e390558
AM
10149 s = htab->global_entry;
10150 plt = htab->elf.splt;
a345bc8d
AM
10151 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10152 if (pent->plt.offset != (bfd_vma) -1
10153 && pent->addend == 0)
10154 {
afe397ea
AM
10155 /* For ELFv2, if this symbol is not defined in a regular file
10156 and we are not generating a shared library or pie, then we
10157 need to define the symbol in the executable on a call stub.
10158 This is to avoid text relocations. */
9e390558
AM
10159 bfd_vma off, stub_align, stub_off, stub_size;
10160 unsigned int align_power;
10161
10162 stub_size = 16;
10163 stub_off = s->size;
10164 if (htab->params->plt_stub_align >= 0)
10165 align_power = htab->params->plt_stub_align;
10166 else
10167 align_power = -htab->params->plt_stub_align;
10168 /* Setting section alignment is delayed until we know it is
10169 non-empty. Otherwise the .text output section will be
10170 aligned at least to plt_stub_align even when no global
10171 entry stubs are needed. */
10172 if (s->alignment_power < align_power)
10173 s->alignment_power = align_power;
10174 stub_align = (bfd_vma) 1 << align_power;
10175 if (htab->params->plt_stub_align >= 0
10176 || ((((stub_off + stub_size - 1) & -stub_align)
10177 - (stub_off & -stub_align))
10178 > ((stub_size - 1) & -stub_align)))
10179 stub_off = (stub_off + stub_align - 1) & -stub_align;
10180 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10181 off -= stub_off + s->output_offset + s->output_section->vma;
10182 /* Note that for --plt-stub-align negative we have a possible
10183 dependency between stub offset and size. Break that
10184 dependency by assuming the max stub size when calculating
10185 the stub offset. */
10186 if (PPC_HA (off) == 0)
10187 stub_size -= 4;
8a2058b5 10188 h->root.type = bfd_link_hash_defined;
afe397ea 10189 h->root.u.def.section = s;
9e390558
AM
10190 h->root.u.def.value = stub_off;
10191 s->size = stub_off + stub_size;
a345bc8d
AM
10192 break;
10193 }
0a1b45a2 10194 return true;
a345bc8d
AM
10195}
10196
65f38f15
AM
10197/* Set the sizes of the dynamic sections. */
10198
0a1b45a2 10199static bool
ee67d69a 10200ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10201 struct bfd_link_info *info)
65f38f15
AM
10202{
10203 struct ppc_link_hash_table *htab;
10204 bfd *dynobj;
10205 asection *s;
0a1b45a2 10206 bool relocs;
65f38f15 10207 bfd *ibfd;
7865406b 10208 struct got_entry *first_tlsld;
65f38f15
AM
10209
10210 htab = ppc_hash_table (info);
4dfe6ac6 10211 if (htab == NULL)
0a1b45a2 10212 return false;
4dfe6ac6 10213
65f38f15
AM
10214 dynobj = htab->elf.dynobj;
10215 if (dynobj == NULL)
10216 abort ();
10217
10218 if (htab->elf.dynamic_sections_created)
10219 {
10220 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10221 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10222 {
3d4d4302 10223 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10224 if (s == NULL)
10225 abort ();
eea6121a 10226 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10227 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10228 }
10229 }
10230
10231 /* Set up .got offsets for local syms, and space for local dynamic
10232 relocs. */
c72f2fb2 10233 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10234 {
411e1bfb
AM
10235 struct got_entry **lgot_ents;
10236 struct got_entry **end_lgot_ents;
e054468f
AM
10237 struct plt_entry **local_plt;
10238 struct plt_entry **end_local_plt;
f961d9dd 10239 unsigned char *lgot_masks;
65f38f15
AM
10240 bfd_size_type locsymcount;
10241 Elf_Internal_Shdr *symtab_hdr;
3a3a4c1f
AM
10242 Elf_Internal_Sym *local_syms;
10243 Elf_Internal_Sym *isym;
65f38f15 10244
0c8d6e5c 10245 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10246 continue;
10247
10248 for (s = ibfd->sections; s != NULL; s = s->next)
10249 {
1657026c 10250 struct ppc_local_dyn_relocs *p;
65f38f15 10251
6edfbbad 10252 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10253 {
ec338859
AM
10254 if (!bfd_is_abs_section (p->sec)
10255 && bfd_is_abs_section (p->sec->output_section))
10256 {
10257 /* Input section has been discarded, either because
10258 it is a copy of a linkonce section or due to
10259 linker script /DISCARD/, so we'll be discarding
10260 the relocs too. */
10261 }
248866a8 10262 else if (p->count != 0)
ec338859 10263 {
1657026c
AM
10264 unsigned int count;
10265 asection *srel;
10266
10267 count = p->count;
8ffb6df2
AM
10268 if (info->enable_dt_relr
10269 && ((!NO_OPD_RELOCS
10270 && (ppc64_elf_section_data (p->sec)->sec_type
10271 == sec_opd))
10272 || !p->ifunc))
1657026c
AM
10273 count -= p->rel_count;
10274 srel = elf_section_data (p->sec)->sreloc;
19e08130 10275 if (p->ifunc)
33e44f2e 10276 srel = htab->elf.irelplt;
1657026c 10277 srel->size += count * sizeof (Elf64_External_Rela);
248866a8
AM
10278 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10279 info->flags |= DF_TEXTREL;
ec338859 10280 }
65f38f15
AM
10281 }
10282 }
10283
411e1bfb
AM
10284 lgot_ents = elf_local_got_ents (ibfd);
10285 if (!lgot_ents)
65f38f15
AM
10286 continue;
10287
0ffa91dd 10288 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10289 locsymcount = symtab_hdr->sh_info;
411e1bfb 10290 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10291 local_plt = (struct plt_entry **) end_lgot_ents;
10292 end_local_plt = local_plt + locsymcount;
f961d9dd 10293 lgot_masks = (unsigned char *) end_local_plt;
c9fecd62
AM
10294 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10295 if (local_syms == NULL && locsymcount != 0)
3a3a4c1f 10296 {
c9fecd62
AM
10297 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10298 0, NULL, NULL, NULL);
10299 if (local_syms == NULL)
10300 return false;
3a3a4c1f 10301 }
e717da7e 10302 s = ppc64_elf_tdata (ibfd)->got;
3a3a4c1f
AM
10303 for (isym = local_syms;
10304 lgot_ents < end_lgot_ents;
c9fecd62 10305 ++lgot_ents, ++lgot_masks, isym++)
65f38f15 10306 {
0b8bcf0d 10307 struct got_entry **pent, *ent;
411e1bfb 10308
0b8bcf0d
AM
10309 pent = lgot_ents;
10310 while ((ent = *pent) != NULL)
411e1bfb
AM
10311 if (ent->got.refcount > 0)
10312 {
e7b938ca 10313 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10314 {
927be08e 10315 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10316 *pent = ent->next;
411e1bfb
AM
10317 }
10318 else
10319 {
19e08130
AM
10320 unsigned int ent_size = 8;
10321 unsigned int rel_size = sizeof (Elf64_External_Rela);
10322
eea6121a 10323 ent->got.offset = s->size;
e7b938ca 10324 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10325 {
19e08130
AM
10326 ent_size *= 2;
10327 rel_size *= 2;
10328 }
10329 s->size += ent_size;
37da22e5 10330 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10331 {
33e44f2e 10332 htab->elf.irelplt->size += rel_size;
19e08130
AM
10333 htab->got_reli_size += rel_size;
10334 }
93370e8e 10335 else if (bfd_link_pic (info)
8f22c953
AM
10336 && (ent->tls_type == 0
10337 ? !info->enable_dt_relr
3a3a4c1f
AM
10338 : !bfd_link_executable (info))
10339 && isym->st_shndx != SHN_ABS)
19e08130
AM
10340 {
10341 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10342 srel->size += rel_size;
927be08e 10343 }
0b8bcf0d 10344 pent = &ent->next;
411e1bfb
AM
10345 }
10346 }
10347 else
0b8bcf0d 10348 *pent = ent->next;
65f38f15 10349 }
3a3a4c1f
AM
10350 if (local_syms != NULL
10351 && symtab_hdr->contents != (unsigned char *) local_syms)
10352 {
10353 if (!info->keep_memory)
10354 free (local_syms);
10355 else
10356 symtab_hdr->contents = (unsigned char *) local_syms;
10357 }
e054468f 10358
2d7ad24e
AM
10359 /* Allocate space for plt calls to local syms. */
10360 lgot_masks = (unsigned char *) end_local_plt;
10361 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10362 {
10363 struct plt_entry *ent;
10364
10365 for (ent = *local_plt; ent != NULL; ent = ent->next)
10366 if (ent->plt.refcount > 0)
10367 {
2d7ad24e
AM
10368 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10369 {
10370 s = htab->elf.iplt;
10371 ent->plt.offset = s->size;
10372 s->size += PLT_ENTRY_SIZE (htab);
10373 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10374 }
3e04d765
AM
10375 else if (htab->can_convert_all_inline_plt
10376 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10377 ent->plt.offset = (bfd_vma) -1;
10378 else
10379 {
10380 s = htab->pltlocal;
10381 ent->plt.offset = s->size;
10382 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
edca615a
AM
10383 if (bfd_link_pic (info)
10384 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
10385 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10386 }
e054468f
AM
10387 }
10388 else
10389 ent->plt.offset = (bfd_vma) -1;
10390 }
65f38f15
AM
10391 }
10392
10393 /* Allocate global sym .plt and .got entries, and space for global
10394 sym dynamic relocs. */
4ce794b7 10395 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10396
0e1862bb 10397 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10398 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10399
7865406b 10400 first_tlsld = NULL;
c72f2fb2 10401 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10402 {
7865406b
AM
10403 struct got_entry *ent;
10404
0c8d6e5c 10405 if (!is_ppc64_elf (ibfd))
102890f0
AM
10406 continue;
10407
7865406b
AM
10408 ent = ppc64_tlsld_got (ibfd);
10409 if (ent->got.refcount > 0)
102890f0 10410 {
7865406b 10411 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10412 {
0a1b45a2 10413 ent->is_indirect = true;
7865406b
AM
10414 ent->got.ent = first_tlsld;
10415 }
10416 else
10417 {
10418 if (first_tlsld == NULL)
10419 first_tlsld = ent;
10420 s = ppc64_elf_tdata (ibfd)->got;
10421 ent->got.offset = s->size;
10422 ent->owner = ibfd;
10423 s->size += 16;
f749f26e 10424 if (bfd_link_dll (info))
7865406b
AM
10425 {
10426 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10427 srel->size += sizeof (Elf64_External_Rela);
10428 }
102890f0
AM
10429 }
10430 }
10431 else
7865406b 10432 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10433 }
10434
65f38f15
AM
10435 /* We now have determined the sizes of the various dynamic sections.
10436 Allocate memory for them. */
0a1b45a2 10437 relocs = false;
65f38f15
AM
10438 for (s = dynobj->sections; s != NULL; s = s->next)
10439 {
10440 if ((s->flags & SEC_LINKER_CREATED) == 0)
10441 continue;
10442
1657026c 10443 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
721956f4
AM
10444 /* These haven't been allocated yet; don't strip. */
10445 continue;
33e44f2e
AM
10446 else if (s == htab->elf.sgot
10447 || s == htab->elf.splt
10448 || s == htab->elf.iplt
2d7ad24e 10449 || s == htab->pltlocal
c456f082 10450 || s == htab->glink
9e390558 10451 || s == htab->global_entry
5474d94f
AM
10452 || s == htab->elf.sdynbss
10453 || s == htab->elf.sdynrelro)
65f38f15
AM
10454 {
10455 /* Strip this section if we don't need it; see the
10456 comment below. */
5bd4f169 10457 }
58d180e8
AM
10458 else if (s == htab->glink_eh_frame)
10459 {
10460 if (!bfd_is_abs_section (s->output_section))
10461 /* Not sized yet. */
10462 continue;
10463 }
08dedd66 10464 else if (startswith (s->name, ".rela"))
5bd4f169 10465 {
c456f082 10466 if (s->size != 0)
5bd4f169 10467 {
33e44f2e 10468 if (s != htab->elf.srelplt)
0a1b45a2 10469 relocs = true;
5bd4f169
AM
10470
10471 /* We use the reloc_count field as a counter if we need
10472 to copy relocs into the output file. */
10473 s->reloc_count = 0;
10474 }
10475 }
65f38f15 10476 else
5bd4f169
AM
10477 {
10478 /* It's not one of our sections, so don't allocate space. */
10479 continue;
10480 }
10481
eea6121a 10482 if (s->size == 0)
5bd4f169 10483 {
c456f082
AM
10484 /* If we don't need this section, strip it from the
10485 output file. This is mostly to handle .rela.bss and
10486 .rela.plt. We must create both sections in
10487 create_dynamic_sections, because they must be created
10488 before the linker maps input sections to output
10489 sections. The linker does that before
10490 adjust_dynamic_symbol is called, and it is that
10491 function which decides whether anything needs to go
10492 into these sections. */
8423293d 10493 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10494 continue;
10495 }
10496
06bcf541
AM
10497 if (bfd_is_abs_section (s->output_section))
10498 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10499 s->name);
10500
c456f082 10501 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10502 continue;
10503
65f38f15
AM
10504 /* Allocate memory for the section contents. We use bfd_zalloc
10505 here in case unused entries are not reclaimed before the
10506 section's contents are written out. This should not happen,
411e1bfb
AM
10507 but this way if it does we get a R_PPC64_NONE reloc in .rela
10508 sections instead of garbage.
10509 We also rely on the section contents being zero when writing
5474d94f 10510 the GOT and .dynrelro. */
eea6121a 10511 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10512 if (s->contents == NULL)
0a1b45a2 10513 return false;
5bd4f169
AM
10514 }
10515
c72f2fb2 10516 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10517 {
0c8d6e5c 10518 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10519 continue;
10520
e717da7e 10521 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10522 if (s != NULL && s != htab->elf.sgot)
e717da7e 10523 {
eea6121a 10524 if (s->size == 0)
8423293d 10525 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10526 else
10527 {
eea6121a 10528 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10529 if (s->contents == NULL)
0a1b45a2 10530 return false;
e717da7e
AM
10531 }
10532 }
10533 s = ppc64_elf_tdata (ibfd)->relgot;
10534 if (s != NULL)
10535 {
eea6121a 10536 if (s->size == 0)
8423293d 10537 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10538 else
10539 {
eea6121a 10540 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10541 if (s->contents == NULL)
0a1b45a2
AM
10542 return false;
10543 relocs = true;
e717da7e
AM
10544 s->reloc_count = 0;
10545 }
10546 }
10547 }
10548
e86ce104 10549 if (htab->elf.dynamic_sections_created)
5bd4f169 10550 {
0a1b45a2 10551 bool tls_opt;
e8910a83 10552
5bd4f169
AM
10553 /* Add some entries to the .dynamic section. We fill in the
10554 values later, in ppc64_elf_finish_dynamic_sections, but we
10555 must add the entries now so that we get the correct size for
10556 the .dynamic section. The DT_DEBUG entry is filled in by the
10557 dynamic linker and used by the debugger. */
dc810e39 10558#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10559 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10560
0e1862bb 10561 if (bfd_link_executable (info))
5bd4f169 10562 {
dc810e39 10563 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 10564 return false;
5bd4f169
AM
10565 }
10566
33e44f2e 10567 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10568 {
dc810e39
AM
10569 if (!add_dynamic_entry (DT_PLTGOT, 0)
10570 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10571 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10572 || !add_dynamic_entry (DT_JMPREL, 0)
10573 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
0a1b45a2 10574 return false;
5bd4f169
AM
10575 }
10576
ee67d69a 10577 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10578 {
10579 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10580 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
0a1b45a2 10581 return false;
19397422
AM
10582 }
10583
7c9cf415 10584 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10585 && ((htab->tls_get_addr_fd != NULL
10586 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10587 || (htab->tga_desc_fd != NULL
10588 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10589 if (tls_opt || !htab->opd_abi)
10590 {
10591 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
0a1b45a2 10592 return false;
e8910a83 10593 }
a7f2871e 10594
5bd4f169
AM
10595 if (relocs)
10596 {
dc810e39
AM
10597 if (!add_dynamic_entry (DT_RELA, 0)
10598 || !add_dynamic_entry (DT_RELASZ, 0)
10599 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
0a1b45a2 10600 return false;
5bd4f169 10601
65f38f15
AM
10602 /* If any dynamic relocs apply to a read-only section,
10603 then we need a DT_TEXTREL entry. */
248866a8 10604 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10605 elf_link_hash_traverse (&htab->elf,
10606 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10607
65f38f15 10608 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10609 {
65f38f15 10610 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 10611 return false;
5bd4f169 10612 }
5bd4f169 10613 }
5bd4f169 10614 }
65f38f15 10615#undef add_dynamic_entry
5bd4f169 10616
0a1b45a2 10617 return true;
5bd4f169
AM
10618}
10619
a345bc8d
AM
10620/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10621
0a1b45a2 10622static bool
a345bc8d
AM
10623ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10624{
10625 if (h->plt.plist != NULL
10626 && !h->def_regular
10627 && !h->pointer_equality_needed)
0a1b45a2 10628 return false;
a345bc8d
AM
10629
10630 return _bfd_elf_hash_symbol (h);
10631}
10632
721956f4 10633/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10634
7aba54da 10635static inline enum ppc_stub_main_type
4ce794b7
AM
10636ppc_type_of_stub (asection *input_sec,
10637 const Elf_Internal_Rela *rel,
10638 struct ppc_link_hash_entry **hash,
e054468f 10639 struct plt_entry **plt_ent,
6911b7dc
AM
10640 bfd_vma destination,
10641 unsigned long local_off)
5bd4f169 10642{
721956f4
AM
10643 struct ppc_link_hash_entry *h = *hash;
10644 bfd_vma location;
10645 bfd_vma branch_offset;
10646 bfd_vma max_branch_offset;
4ce794b7 10647 enum elf_ppc64_reloc_type r_type;
5bd4f169 10648
721956f4
AM
10649 if (h != NULL)
10650 {
e054468f 10651 struct plt_entry *ent;
7fe2b9a6 10652 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10653 if (h->oh != NULL
10654 && h->oh->is_func_descriptor)
7b8f6675
AM
10655 {
10656 fdh = ppc_follow_link (h->oh);
10657 *hash = fdh;
10658 }
8387904d 10659
e054468f
AM
10660 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10661 if (ent->addend == rel->r_addend
10662 && ent->plt.offset != (bfd_vma) -1)
10663 {
e054468f
AM
10664 *plt_ent = ent;
10665 return ppc_stub_plt_call;
10666 }
5bd4f169 10667
7fe2b9a6
AM
10668 /* Here, we know we don't have a plt entry. If we don't have a
10669 either a defined function descriptor or a defined entry symbol
10670 in a regular object file, then it is pointless trying to make
10671 any other type of stub. */
854b41e7
AM
10672 if (!is_static_defined (&fdh->elf)
10673 && !is_static_defined (&h->elf))
721956f4 10674 return ppc_stub_none;
5d1634d7 10675 }
e054468f
AM
10676 else if (elf_local_got_ents (input_sec->owner) != NULL)
10677 {
10678 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10679 struct plt_entry **local_plt = (struct plt_entry **)
10680 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10681 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10682
10683 if (local_plt[r_symndx] != NULL)
10684 {
10685 struct plt_entry *ent;
10686
10687 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10688 if (ent->addend == rel->r_addend
10689 && ent->plt.offset != (bfd_vma) -1)
10690 {
10691 *plt_ent = ent;
10692 return ppc_stub_plt_call;
10693 }
10694 }
10695 }
5d1634d7 10696
721956f4
AM
10697 /* Determine where the call point is. */
10698 location = (input_sec->output_offset
10699 + input_sec->output_section->vma
10700 + rel->r_offset);
5d1634d7 10701
721956f4
AM
10702 branch_offset = destination - location;
10703 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10704
721956f4
AM
10705 /* Determine if a long branch stub is needed. */
10706 max_branch_offset = 1 << 25;
23cedd1d
AM
10707 if (r_type == R_PPC64_REL14
10708 || r_type == R_PPC64_REL14_BRTAKEN
10709 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10710 max_branch_offset = 1 << 15;
5d1634d7 10711
6911b7dc 10712 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10713 /* We need a stub. Figure out whether a long_branch or plt_branch
10714 is needed later. */
10715 return ppc_stub_long_branch;
5d1634d7 10716
721956f4 10717 return ppc_stub_none;
5d1634d7
AM
10718}
10719
f891966f
AM
10720/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10721 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10722 . mflr %r12
10723 . bcl 20,31,1f
10724 .1: mflr %r11
10725 . mtlr %r12
05d0e962 10726 . lis %r12,xxx-1b@highest
f891966f 10727 . ori %r12,%r12,xxx-1b@higher
05d0e962 10728 . sldi %r12,%r12,32
f891966f 10729 . oris %r12,%r12,xxx-1b@high
05d0e962 10730 . ori %r12,%r12,xxx-1b@l
f891966f 10731 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10732
10733static bfd_byte *
0a1b45a2 10734build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
05d0e962 10735{
f891966f
AM
10736 bfd_put_32 (abfd, MFLR_R12, p);
10737 p += 4;
10738 bfd_put_32 (abfd, BCL_20_31, p);
10739 p += 4;
10740 bfd_put_32 (abfd, MFLR_R11, p);
10741 p += 4;
10742 bfd_put_32 (abfd, MTLR_R12, p);
10743 p += 4;
05d0e962
AM
10744 if (off + 0x8000 < 0x10000)
10745 {
10746 if (load)
10747 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10748 else
10749 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10750 p += 4;
10751 }
10752 else if (off + 0x80008000ULL < 0x100000000ULL)
10753 {
10754 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10755 p += 4;
10756 if (load)
10757 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10758 else
10759 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10760 p += 4;
10761 }
10762 else
10763 {
10764 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10765 {
10766 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10767 p += 4;
10768 }
10769 else
10770 {
10771 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10772 p += 4;
10773 if (((off >> 32) & 0xffff) != 0)
10774 {
10775 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10776 p += 4;
10777 }
10778 }
10779 if (((off >> 32) & 0xffffffffULL) != 0)
10780 {
10781 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10782 p += 4;
10783 }
10784 if (PPC_HI (off) != 0)
10785 {
10786 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10787 p += 4;
10788 }
10789 if (PPC_LO (off) != 0)
10790 {
10791 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10792 p += 4;
10793 }
10794 if (load)
10795 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10796 else
10797 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10798 p += 4;
10799 }
10800 return p;
10801}
10802
10803static unsigned int
10804size_offset (bfd_vma off)
10805{
10806 unsigned int size;
10807 if (off + 0x8000 < 0x10000)
10808 size = 4;
10809 else if (off + 0x80008000ULL < 0x100000000ULL)
10810 size = 8;
10811 else
10812 {
10813 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10814 size = 4;
10815 else
10816 {
10817 size = 4;
10818 if (((off >> 32) & 0xffff) != 0)
10819 size += 4;
10820 }
10821 if (((off >> 32) & 0xffffffffULL) != 0)
10822 size += 4;
10823 if (PPC_HI (off) != 0)
10824 size += 4;
10825 if (PPC_LO (off) != 0)
10826 size += 4;
10827 size += 4;
10828 }
f891966f 10829 return size + 16;
05d0e962
AM
10830}
10831
3d58e1fc
AM
10832static unsigned int
10833num_relocs_for_offset (bfd_vma off)
10834{
10835 unsigned int num_rel;
10836 if (off + 0x8000 < 0x10000)
10837 num_rel = 1;
10838 else if (off + 0x80008000ULL < 0x100000000ULL)
10839 num_rel = 2;
10840 else
10841 {
10842 num_rel = 1;
10843 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10844 && ((off >> 32) & 0xffff) != 0)
10845 num_rel += 1;
10846 if (PPC_HI (off) != 0)
10847 num_rel += 1;
10848 if (PPC_LO (off) != 0)
10849 num_rel += 1;
10850 }
10851 return num_rel;
10852}
10853
10854static Elf_Internal_Rela *
10855emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10856 bfd_vma roff, bfd_vma targ, bfd_vma off)
10857{
10858 bfd_vma relative_targ = targ - (roff - 8);
10859 if (bfd_big_endian (info->output_bfd))
10860 roff += 2;
10861 r->r_offset = roff;
10862 r->r_addend = relative_targ + roff;
10863 if (off + 0x8000 < 0x10000)
10864 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10865 else if (off + 0x80008000ULL < 0x100000000ULL)
10866 {
10867 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10868 ++r;
10869 roff += 4;
10870 r->r_offset = roff;
10871 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10872 r->r_addend = relative_targ + roff;
10873 }
10874 else
10875 {
10876 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10877 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10878 else
10879 {
10880 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10881 if (((off >> 32) & 0xffff) != 0)
10882 {
10883 ++r;
10884 roff += 4;
10885 r->r_offset = roff;
10886 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10887 r->r_addend = relative_targ + roff;
10888 }
10889 }
10890 if (((off >> 32) & 0xffffffffULL) != 0)
10891 roff += 4;
10892 if (PPC_HI (off) != 0)
10893 {
10894 ++r;
10895 roff += 4;
10896 r->r_offset = roff;
10897 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10898 r->r_addend = relative_targ + roff;
10899 }
10900 if (PPC_LO (off) != 0)
10901 {
10902 ++r;
10903 roff += 4;
10904 r->r_offset = roff;
10905 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10906 r->r_addend = relative_targ + roff;
10907 }
10908 }
10909 return r;
10910}
10911
04bdff6a 10912static bfd_byte *
7c1f4227 10913build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
0a1b45a2 10914 bool load)
04bdff6a
AM
10915{
10916 uint64_t insn;
10917 if (off - odd + (1ULL << 33) < 1ULL << 34)
10918 {
10919 off -= odd;
10920 if (odd)
10921 {
10922 bfd_put_32 (abfd, NOP, p);
10923 p += 4;
10924 }
10925 if (load)
10926 insn = PLD_R12_PC;
10927 else
10928 insn = PADDI_R12_PC;
10929 insn |= D34 (off);
10930 bfd_put_32 (abfd, insn >> 32, p);
10931 p += 4;
10932 bfd_put_32 (abfd, insn, p);
10933 }
10934 /* The minimum value for paddi is -0x200000000. The minimum value
10935 for li is -0x8000, which when shifted by 34 and added gives a
10936 minimum value of -0x2000200000000. The maximum value is
10937 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10938 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10939 {
10940 off -= 8 - odd;
10941 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10942 p += 4;
10943 if (!odd)
10944 {
10945 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10946 p += 4;
10947 }
10948 insn = PADDI_R12_PC | D34 (off);
10949 bfd_put_32 (abfd, insn >> 32, p);
10950 p += 4;
10951 bfd_put_32 (abfd, insn, p);
10952 p += 4;
10953 if (odd)
10954 {
10955 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10956 p += 4;
10957 }
10958 if (load)
10959 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10960 else
10961 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10962 }
10963 else
10964 {
10965 off -= odd + 8;
10966 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10967 p += 4;
10968 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10969 p += 4;
10970 if (odd)
10971 {
10972 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10973 p += 4;
10974 }
10975 insn = PADDI_R12_PC | D34 (off);
10976 bfd_put_32 (abfd, insn >> 32, p);
10977 p += 4;
10978 bfd_put_32 (abfd, insn, p);
10979 p += 4;
10980 if (!odd)
10981 {
10982 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10983 p += 4;
10984 }
10985 if (load)
10986 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10987 else
10988 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10989 }
10990 p += 4;
10991 return p;
10992}
10993
10994static unsigned int
7c1f4227 10995size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10996{
10997 if (off - odd + (1ULL << 33) < 1ULL << 34)
10998 return odd + 8;
10999 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11000 return 20;
11001 else
11002 return 24;
11003}
11004
11005static unsigned int
7c1f4227 11006num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
11007{
11008 if (off - odd + (1ULL << 33) < 1ULL << 34)
11009 return 1;
11010 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11011 return 2;
11012 else
11013 return 3;
11014}
11015
11016static Elf_Internal_Rela *
7c1f4227 11017emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
11018 Elf_Internal_Rela *r, bfd_vma roff,
11019 bfd_vma targ, bfd_vma off, int odd)
11020{
11021 if (off - odd + (1ULL << 33) < 1ULL << 34)
11022 roff += odd;
11023 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11024 {
11025 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11026 r->r_offset = roff + d_offset;
11027 r->r_addend = targ + 8 - odd - d_offset;
11028 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11029 ++r;
11030 roff += 8 - odd;
11031 }
11032 else
11033 {
11034 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11035 r->r_offset = roff + d_offset;
11036 r->r_addend = targ + 8 + odd - d_offset;
11037 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11038 ++r;
11039 roff += 4;
11040 r->r_offset = roff + d_offset;
11041 r->r_addend = targ + 4 + odd - d_offset;
11042 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11043 ++r;
11044 roff += 4 + odd;
11045 }
11046 r->r_offset = roff;
11047 r->r_addend = targ;
11048 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11049 return r;
11050}
11051
df136d64
AM
11052/* Emit .eh_frame opcode to advance pc by DELTA. */
11053
11054static bfd_byte *
11055eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11056{
11057 delta /= 4;
11058 if (delta < 64)
11059 *eh++ = DW_CFA_advance_loc + delta;
11060 else if (delta < 256)
11061 {
11062 *eh++ = DW_CFA_advance_loc1;
11063 *eh++ = delta;
11064 }
11065 else if (delta < 65536)
11066 {
11067 *eh++ = DW_CFA_advance_loc2;
11068 bfd_put_16 (abfd, delta, eh);
11069 eh += 2;
11070 }
11071 else
11072 {
11073 *eh++ = DW_CFA_advance_loc4;
11074 bfd_put_32 (abfd, delta, eh);
11075 eh += 4;
11076 }
11077 return eh;
11078}
11079
11080/* Size of required .eh_frame opcode to advance pc by DELTA. */
11081
11082static unsigned int
11083eh_advance_size (unsigned int delta)
11084{
11085 if (delta < 64 * 4)
11086 /* DW_CFA_advance_loc+[1..63]. */
11087 return 1;
11088 if (delta < 256 * 4)
11089 /* DW_CFA_advance_loc1, byte. */
11090 return 2;
11091 if (delta < 65536 * 4)
11092 /* DW_CFA_advance_loc2, 2 bytes. */
11093 return 3;
11094 /* DW_CFA_advance_loc4, 4 bytes. */
11095 return 5;
11096}
11097
794e51c0
AM
11098/* With power7 weakly ordered memory model, it is possible for ld.so
11099 to update a plt entry in one thread and have another thread see a
11100 stale zero toc entry. To avoid this we need some sort of acquire
11101 barrier in the call stub. One solution is to make the load of the
11102 toc word seem to appear to depend on the load of the function entry
11103 word. Another solution is to test for r2 being zero, and branch to
11104 the appropriate glink entry if so.
11105
11106 . fake dep barrier compare
71a39c98
AM
11107 . ld 12,xxx(2) ld 12,xxx(2)
11108 . mtctr 12 mtctr 12
11109 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
11110 . add 2,2,11 cmpldi 2,0
11111 . ld 2,xxx+8(2) bnectr+
11112 . bctr b <glink_entry>
11113
11114 The solution involving the compare turns out to be faster, so
11115 that's what we use unless the branch won't reach. */
11116
11117#define ALWAYS_USE_FAKE_DEP 0
11118#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 11119
794e51c0
AM
11120static inline unsigned int
11121plt_stub_size (struct ppc_link_hash_table *htab,
11122 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
11123 bfd_vma off,
11124 unsigned int odd)
794e51c0 11125{
05d0e962 11126 unsigned size;
b9e5796b 11127
7aba54da 11128 if (stub_entry->type.sub == ppc_stub_notoc)
05d0e962 11129 {
7aba54da
AM
11130 size = 8 + size_power10_offset (off, odd);
11131 if (stub_entry->type.r2save)
11132 size += 4;
11133 }
11134 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11135 {
11136 size = 8 + size_offset (off - 8);
11137 if (stub_entry->type.r2save)
05d0e962 11138 size += 4;
05d0e962 11139 }
29433886 11140 else
b9e5796b 11141 {
29433886 11142 size = 12;
7aba54da 11143 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
b9e5796b 11144 size += 4;
29433886 11145 if (PPC_HA (off) != 0)
b9e5796b 11146 size += 4;
29433886
AM
11147 if (htab->opd_abi)
11148 {
11149 size += 4;
11150 if (htab->params->plt_static_chain)
11151 size += 4;
11152 if (htab->params->plt_thread_safe
11153 && htab->elf.dynamic_sections_created
11154 && stub_entry->h != NULL
11155 && stub_entry->h->elf.dynindx != -1)
11156 size += 8;
11157 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11158 != PPC_HA (off))
11159 size += 4;
11160 }
b9e5796b 11161 }
794e51c0 11162 if (stub_entry->h != NULL
ed7007c1 11163 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11164 && htab->params->tls_get_addr_opt)
f378ab09 11165 {
29433886 11166 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11167 {
29433886 11168 size += 30 * 4;
7aba54da 11169 if (stub_entry->type.r2save)
29433886 11170 size += 4;
9e7028aa
AM
11171 }
11172 else
11173 {
29433886 11174 size += 7 * 4;
7aba54da 11175 if (stub_entry->type.r2save)
29433886 11176 size += 6 * 4;
9e7028aa 11177 }
f378ab09 11178 }
794e51c0
AM
11179 return size;
11180}
11181
2420fff6
AM
11182/* Depending on the sign of plt_stub_align:
11183 If positive, return the padding to align to a 2**plt_stub_align
11184 boundary.
11185 If negative, if this stub would cross fewer 2**plt_stub_align
11186 boundaries if we align, then return the padding needed to do so. */
11187
794e51c0
AM
11188static inline unsigned int
11189plt_stub_pad (struct ppc_link_hash_table *htab,
11190 struct ppc_stub_hash_entry *stub_entry,
2405fc40 11191 bfd_vma stub_off,
29433886
AM
11192 bfd_vma plt_off,
11193 unsigned int odd)
794e51c0 11194{
2420fff6 11195 int stub_align;
1aa42141 11196 unsigned stub_size;
794e51c0 11197
2420fff6
AM
11198 if (htab->params->plt_stub_align >= 0)
11199 {
11200 stub_align = 1 << htab->params->plt_stub_align;
11201 if ((stub_off & (stub_align - 1)) != 0)
11202 return stub_align - (stub_off & (stub_align - 1));
11203 return 0;
11204 }
11205
11206 stub_align = 1 << -htab->params->plt_stub_align;
29433886 11207 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 11208 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 11209 > ((stub_size - 1) & -stub_align))
794e51c0
AM
11210 return stub_align - (stub_off & (stub_align - 1));
11211 return 0;
11212}
11213
7aba54da 11214/* Build a toc using .plt call stub. */
794e51c0
AM
11215
11216static inline bfd_byte *
11217build_plt_stub (struct ppc_link_hash_table *htab,
11218 struct ppc_stub_hash_entry *stub_entry,
11219 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11220{
e7d1c40c 11221 bfd *obfd = htab->params->stub_bfd;
0a1b45a2
AM
11222 bool plt_load_toc = htab->opd_abi;
11223 bool plt_static_chain = htab->params->plt_static_chain;
11224 bool plt_thread_safe = (htab->params->plt_thread_safe
11225 && htab->elf.dynamic_sections_created
11226 && stub_entry->h != NULL
11227 && stub_entry->h->elf.dynindx != -1);
11228 bool use_fake_dep = plt_thread_safe;
794e51c0
AM
11229 bfd_vma cmp_branch_off = 0;
11230
11231 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11232 && plt_load_toc
794e51c0 11233 && plt_thread_safe
9e51d549
AM
11234 && !(stub_entry->h != NULL
11235 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11236 && htab->params->tls_get_addr_opt))
794e51c0
AM
11237 {
11238 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11239 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11240 / PLT_ENTRY_SIZE (htab));
9e390558 11241 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11242 bfd_vma to, from;
11243
68d62958
AM
11244 if (pltindex > 32768)
11245 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11246 to = (glinkoff
11247 + htab->glink->output_offset
11248 + htab->glink->output_section->vma);
6f20ed8a 11249 from = (p - stub_entry->group->stub_sec->contents
7aba54da 11250 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0
AM
11251 + 4 * (PPC_HA (offset) != 0)
11252 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11253 != PPC_HA (offset))
11254 + 4 * (plt_static_chain != 0)
11255 + 20
6f20ed8a
AM
11256 + stub_entry->group->stub_sec->output_offset
11257 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11258 cmp_branch_off = to - from;
11259 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11260 }
11261
ac2df442
AM
11262 if (PPC_HA (offset) != 0)
11263 {
176a0d42
AM
11264 if (r != NULL)
11265 {
7aba54da 11266 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11267 r[0].r_offset += 4;
176a0d42 11268 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11269 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11270 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11271 r[1].r_addend = r[0].r_addend;
b9e5796b 11272 if (plt_load_toc)
176a0d42 11273 {
b9e5796b 11274 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11275 {
b9e5796b
AM
11276 r[2].r_offset = r[1].r_offset + 4;
11277 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11278 r[2].r_addend = r[0].r_addend;
11279 }
11280 else
11281 {
11282 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11283 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11284 r[2].r_addend = r[0].r_addend + 8;
11285 if (plt_static_chain)
11286 {
11287 r[3].r_offset = r[2].r_offset + 4;
11288 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11289 r[3].r_addend = r[0].r_addend + 16;
11290 }
c7131b65 11291 }
176a0d42
AM
11292 }
11293 }
7aba54da 11294 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11295 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11296 if (plt_load_toc)
11297 {
11298 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11299 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11300 }
11301 else
11302 {
11303 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11304 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11305 }
b9e5796b
AM
11306 if (plt_load_toc
11307 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11308 {
71a39c98 11309 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11310 offset = 0;
11311 }
71a39c98 11312 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11313 if (plt_load_toc)
794e51c0 11314 {
b9e5796b
AM
11315 if (use_fake_dep)
11316 {
11317 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11318 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11319 }
11320 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11321 if (plt_static_chain)
11322 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11323 }
ac2df442
AM
11324 }
11325 else
11326 {
176a0d42
AM
11327 if (r != NULL)
11328 {
7aba54da 11329 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11330 r[0].r_offset += 4;
176a0d42 11331 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11332 if (plt_load_toc)
176a0d42 11333 {
b9e5796b 11334 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11335 {
b9e5796b
AM
11336 r[1].r_offset = r[0].r_offset + 4;
11337 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11338 r[1].r_addend = r[0].r_addend;
11339 }
11340 else
11341 {
11342 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11343 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11344 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11345 if (plt_static_chain)
11346 {
11347 r[2].r_offset = r[1].r_offset + 4;
11348 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11349 r[2].r_addend = r[0].r_addend + 8;
11350 }
c7131b65 11351 }
176a0d42
AM
11352 }
11353 }
7aba54da 11354 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11355 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11356 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11357 if (plt_load_toc
11358 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11359 {
11360 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11361 offset = 0;
11362 }
71a39c98 11363 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11364 if (plt_load_toc)
794e51c0 11365 {
b9e5796b
AM
11366 if (use_fake_dep)
11367 {
11368 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11369 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11370 }
11371 if (plt_static_chain)
11372 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11373 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11374 }
ac2df442 11375 }
b9e5796b 11376 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11377 {
11378 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11379 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11380 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11381 }
11382 else
407aa07c 11383 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11384 return p;
11385}
11386
a7f2871e
AM
11387/* Build a special .plt call stub for __tls_get_addr. */
11388
b9ca1af6 11389#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11390#define LD_R12_0R3 0xe9830000
11391#define MR_R0_R3 0x7c601b78
b9ca1af6 11392#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11393#define ADD_R3_R12_R13 0x7c6c6a14
11394#define BEQLR 0x4d820020
11395#define MR_R3_R0 0x7c030378
a7f2871e 11396#define BCTRL 0x4e800421
a7f2871e 11397
29433886
AM
11398static bfd_byte *
11399build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11400 struct ppc_stub_hash_entry *stub_entry,
29433886 11401 bfd_byte *p)
a7f2871e 11402{
e7d1c40c 11403 bfd *obfd = htab->params->stub_bfd;
794e51c0 11404
b9ca1af6 11405 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11406 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11407 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11408 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11409 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11410 bfd_put_32 (obfd, BEQLR, p), p += 4;
11411 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11412
29433886
AM
11413 if (!htab->params->no_tls_get_addr_regsave)
11414 p = tls_get_addr_prologue (obfd, p, htab);
7aba54da 11415 else if (stub_entry->type.r2save)
29433886 11416 {
9e7028aa
AM
11417 bfd_put_32 (obfd, MFLR_R0, p);
11418 p += 4;
11419 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11420 p += 4;
29433886
AM
11421 }
11422 return p;
11423}
9e7028aa 11424
29433886
AM
11425static bfd_byte *
11426build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11427 struct ppc_stub_hash_entry *stub_entry,
11428 bfd_byte *p,
11429 bfd_byte *loc)
11430{
11431 bfd *obfd = htab->params->stub_bfd;
11432
11433 if (!htab->params->no_tls_get_addr_regsave)
11434 {
11435 bfd_put_32 (obfd, BCTRL, p - 4);
11436
7aba54da 11437 if (stub_entry->type.r2save)
29433886
AM
11438 {
11439 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11440 p += 4;
11441 }
11442 p = tls_get_addr_epilogue (obfd, p, htab);
11443 }
7aba54da 11444 else if (stub_entry->type.r2save)
29433886 11445 {
9e7028aa
AM
11446 bfd_put_32 (obfd, BCTRL, p - 4);
11447
11448 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11449 p += 4;
11450 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11451 p += 4;
11452 bfd_put_32 (obfd, MTLR_R0, p);
11453 p += 4;
11454 bfd_put_32 (obfd, BLR, p);
11455 p += 4;
11456 }
a7f2871e 11457
df136d64
AM
11458 if (htab->glink_eh_frame != NULL
11459 && htab->glink_eh_frame->size != 0)
11460 {
11461 bfd_byte *base, *eh;
df136d64
AM
11462
11463 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11464 eh = base + stub_entry->group->eh_size;
29433886
AM
11465
11466 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11467 {
29433886
AM
11468 unsigned int cfa_updt, delta, i;
11469
9e7028aa
AM
11470 /* After the bctrl, lr has been modified so we need to emit
11471 .eh_frame info saying the return address is on the stack. In
11472 fact we must put the EH info at or before the call rather
11473 than after it, because the EH info for a call needs to be
11474 specified by that point.
11475 See libgcc/unwind-dw2.c execute_cfa_program.
11476 Any stack pointer update must be described immediately after
11477 the instruction making the change, and since the stdu occurs
11478 after saving regs we put all the reg saves and the cfa
11479 change there. */
11480 cfa_updt = stub_entry->stub_offset + 18 * 4;
11481 delta = cfa_updt - stub_entry->group->lr_restore;
11482 stub_entry->group->lr_restore
11483 = stub_entry->stub_offset + (p - loc) - 4;
11484 eh = eh_advance (htab->elf.dynobj, eh, delta);
11485 *eh++ = DW_CFA_def_cfa_offset;
11486 if (htab->opd_abi)
11487 {
11488 *eh++ = 128;
11489 *eh++ = 1;
11490 }
11491 else
11492 *eh++ = 96;
11493 *eh++ = DW_CFA_offset_extended_sf;
11494 *eh++ = 65;
11495 *eh++ = (-16 / 8) & 0x7f;
11496 for (i = 4; i < 12; i++)
11497 {
11498 *eh++ = DW_CFA_offset + i;
11499 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11500 }
11501 *eh++ = (DW_CFA_advance_loc
11502 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11503 *eh++ = DW_CFA_def_cfa_offset;
11504 *eh++ = 0;
11505 for (i = 4; i < 12; i++)
11506 *eh++ = DW_CFA_restore + i;
11507 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11508 *eh++ = DW_CFA_restore_extended;
11509 *eh++ = 65;
11510 stub_entry->group->eh_size = eh - base;
11511 }
7aba54da 11512 else if (stub_entry->type.r2save)
29433886
AM
11513 {
11514 unsigned int lr_used, delta;
11515
11516 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11517 delta = lr_used - stub_entry->group->lr_restore;
11518 stub_entry->group->lr_restore = lr_used + 16;
11519 eh = eh_advance (htab->elf.dynobj, eh, delta);
11520 *eh++ = DW_CFA_offset_extended_sf;
11521 *eh++ = 65;
11522 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11523 *eh++ = DW_CFA_advance_loc + 4;
11524 *eh++ = DW_CFA_restore_extended;
11525 *eh++ = 65;
11526 stub_entry->group->eh_size = eh - base;
9e7028aa 11527 }
df136d64 11528 }
a7f2871e
AM
11529 return p;
11530}
11531
176a0d42
AM
11532static Elf_Internal_Rela *
11533get_relocs (asection *sec, int count)
11534{
11535 Elf_Internal_Rela *relocs;
11536 struct bfd_elf_section_data *elfsec_data;
11537
11538 elfsec_data = elf_section_data (sec);
11539 relocs = elfsec_data->relocs;
11540 if (relocs == NULL)
11541 {
11542 bfd_size_type relsize;
11543 relsize = sec->reloc_count * sizeof (*relocs);
11544 relocs = bfd_alloc (sec->owner, relsize);
11545 if (relocs == NULL)
11546 return NULL;
11547 elfsec_data->relocs = relocs;
d4730f92
BS
11548 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11549 sizeof (Elf_Internal_Shdr));
11550 if (elfsec_data->rela.hdr == NULL)
11551 return NULL;
11552 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11553 * sizeof (Elf64_External_Rela));
11554 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11555 sec->reloc_count = 0;
11556 }
11557 relocs += sec->reloc_count;
11558 sec->reloc_count += count;
11559 return relocs;
11560}
11561
3d58e1fc
AM
11562/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11563 forms, to the equivalent relocs against the global symbol given by
11564 STUB_ENTRY->H. */
11565
0a1b45a2 11566static bool
3d58e1fc
AM
11567use_global_in_relocs (struct ppc_link_hash_table *htab,
11568 struct ppc_stub_hash_entry *stub_entry,
11569 Elf_Internal_Rela *r, unsigned int num_rel)
11570{
11571 struct elf_link_hash_entry **hashes;
11572 unsigned long symndx;
11573 struct ppc_link_hash_entry *h;
11574 bfd_vma symval;
11575
11576 /* Relocs are always against symbols in their own object file. Fake
11577 up global sym hashes for the stub bfd (which has no symbols). */
11578 hashes = elf_sym_hashes (htab->params->stub_bfd);
11579 if (hashes == NULL)
11580 {
11581 bfd_size_type hsize;
11582
11583 /* When called the first time, stub_globals will contain the
11584 total number of symbols seen during stub sizing. After
11585 allocating, stub_globals is used as an index to fill the
11586 hashes array. */
11587 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11588 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11589 if (hashes == NULL)
0a1b45a2 11590 return false;
3d58e1fc
AM
11591 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11592 htab->stub_globals = 1;
11593 }
11594 symndx = htab->stub_globals++;
11595 h = stub_entry->h;
11596 hashes[symndx] = &h->elf;
11597 if (h->oh != NULL && h->oh->is_func)
11598 h = ppc_follow_link (h->oh);
11599 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11600 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11601 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11602 while (num_rel-- != 0)
11603 {
11604 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11605 if (h->elf.root.u.def.section != stub_entry->target_section)
11606 {
11607 /* H is an opd symbol. The addend must be zero, and the
11608 branch reloc is the only one we can convert. */
11609 r->r_addend = 0;
11610 break;
11611 }
11612 else
11613 r->r_addend -= symval;
11614 --r;
11615 }
0a1b45a2 11616 return true;
3d58e1fc
AM
11617}
11618
aa374f67 11619static bfd_vma
25f53a85 11620get_r2off (struct bfd_link_info *info,
aa374f67
AM
11621 struct ppc_stub_hash_entry *stub_entry)
11622{
25f53a85 11623 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11624 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11625
11626 if (r2off == 0)
11627 {
11628 /* Support linking -R objects. Get the toc pointer from the
11629 opd entry. */
11630 char buf[8];
b9e5796b
AM
11631 if (!htab->opd_abi)
11632 return r2off;
aa374f67
AM
11633 asection *opd = stub_entry->h->elf.root.u.def.section;
11634 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11635
11636 if (strcmp (opd->name, ".opd") != 0
11637 || opd->reloc_count != 0)
11638 {
2cdcc330
AM
11639 info->callbacks->einfo
11640 (_("%P: cannot find opd entry toc for `%pT'\n"),
11641 stub_entry->h->elf.root.root.string);
aa374f67 11642 bfd_set_error (bfd_error_bad_value);
a7c49797 11643 return (bfd_vma) -1;
aa374f67
AM
11644 }
11645 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11646 return (bfd_vma) -1;
aa374f67 11647 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11648 r2off -= elf_gp (info->output_bfd);
aa374f67 11649 }
6f20ed8a 11650 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11651 return r2off;
11652}
11653
0a1b45a2 11654static bool
4ce794b7 11655ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11656{
721956f4
AM
11657 struct ppc_stub_hash_entry *stub_entry;
11658 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11659 struct bfd_link_info *info;
11660 struct ppc_link_hash_table *htab;
29433886 11661 bfd *obfd;
721956f4 11662 bfd_byte *loc;
3d58e1fc 11663 bfd_byte *p, *relp;
1aa42141 11664 bfd_vma targ, off;
176a0d42 11665 Elf_Internal_Rela *r;
e054468f 11666 asection *plt;
3d58e1fc 11667 int num_rel;
04bdff6a 11668 int odd;
0a1b45a2 11669 bool is_tga;
5d1634d7 11670
721956f4
AM
11671 /* Massage our args to the form they really have. */
11672 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11673 info = in_arg;
5d1634d7 11674
abf874aa
CL
11675 /* Fail if the target section could not be assigned to an output
11676 section. The user should fix his linker script. */
11677 if (stub_entry->target_section != NULL
11678 && stub_entry->target_section->output_section == NULL
11679 && info->non_contiguous_regions)
53215f21
CL
11680 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11681 "Retry without --enable-non-contiguous-regions.\n"),
11682 stub_entry->target_section);
abf874aa
CL
11683
11684 /* Same for the group. */
11685 if (stub_entry->group->stub_sec != NULL
11686 && stub_entry->group->stub_sec->output_section == NULL
11687 && info->non_contiguous_regions)
53215f21
CL
11688 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11689 "output section. Retry without "
11690 "--enable-non-contiguous-regions.\n"),
11691 stub_entry->group->stub_sec,
11692 stub_entry->target_section);
abf874aa 11693
5d1634d7 11694 htab = ppc_hash_table (info);
4dfe6ac6 11695 if (htab == NULL)
0a1b45a2 11696 return false;
5d1634d7 11697
1aa42141 11698 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11699 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11700
7aba54da
AM
11701 htab->stub_count[stub_entry->type.main - 1] += 1;
11702 if (stub_entry->type.main == ppc_stub_long_branch
11703 && stub_entry->type.sub == ppc_stub_toc)
5d1634d7 11704 {
721956f4 11705 /* Branches are relative. This is where we are going to. */
1aa42141 11706 targ = (stub_entry->target_value
6911b7dc
AM
11707 + stub_entry->target_section->output_offset
11708 + stub_entry->target_section->output_section->vma);
1aa42141 11709 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11710
721956f4 11711 /* And this is where we are coming from. */
1aa42141
AM
11712 off = (stub_entry->stub_offset
11713 + stub_entry->group->stub_sec->output_offset
11714 + stub_entry->group->stub_sec->output_section->vma);
11715 off = targ - off;
e86ce104 11716
9e390558 11717 p = loc;
29433886 11718 obfd = htab->params->stub_bfd;
7aba54da 11719 if (stub_entry->type.r2save)
ad8e1ba5 11720 {
25f53a85 11721 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11722
a7c49797 11723 if (r2off == (bfd_vma) -1)
aa374f67 11724 {
0a1b45a2
AM
11725 htab->stub_error = true;
11726 return false;
aa374f67 11727 }
29433886 11728 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11729 p += 4;
ac2df442
AM
11730 if (PPC_HA (r2off) != 0)
11731 {
29433886 11732 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11733 p += 4;
a7c49797
AM
11734 }
11735 if (PPC_LO (r2off) != 0)
11736 {
29433886 11737 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11738 p += 4;
ac2df442 11739 }
9e390558 11740 off -= p - loc;
ad8e1ba5 11741 }
29433886 11742 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11743 p += 4;
ad8e1ba5 11744
5c3dead3
AM
11745 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11746 {
cf97bcb0
AM
11747 _bfd_error_handler
11748 (_("long branch stub `%s' offset overflow"),
bc30df16 11749 stub_entry->root.string);
0a1b45a2
AM
11750 htab->stub_error = true;
11751 return false;
5c3dead3 11752 }
ee75fd95
AM
11753
11754 if (info->emitrelocations)
11755 {
6f20ed8a 11756 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42 11757 if (r == NULL)
0a1b45a2 11758 return false;
9e390558 11759 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11760 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11761 r->r_addend = targ;
3d58e1fc
AM
11762 if (stub_entry->h != NULL
11763 && !use_global_in_relocs (htab, stub_entry, r, 1))
0a1b45a2 11764 return false;
ee75fd95 11765 }
7aba54da
AM
11766 }
11767 else if (stub_entry->type.main == ppc_stub_plt_branch
11768 && stub_entry->type.sub == ppc_stub_toc)
11769 {
721956f4
AM
11770 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11771 stub_entry->root.string + 9,
0a1b45a2 11772 false, false);
721956f4
AM
11773 if (br_entry == NULL)
11774 {
cf97bcb0
AM
11775 _bfd_error_handler (_("can't find branch stub `%s'"),
11776 stub_entry->root.string);
0a1b45a2
AM
11777 htab->stub_error = true;
11778 return false;
721956f4
AM
11779 }
11780
1aa42141 11781 targ = (stub_entry->target_value
176a0d42
AM
11782 + stub_entry->target_section->output_offset
11783 + stub_entry->target_section->output_section->vma);
7aba54da 11784 if (!stub_entry->type.r2save)
1aa42141 11785 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11786
1aa42141 11787 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11788 htab->brlt->contents + br_entry->offset);
721956f4 11789
f94498ff 11790 if (br_entry->iter == htab->stub_iteration)
721956f4 11791 {
f94498ff 11792 br_entry->iter = 0;
84f5d08e 11793
1657026c 11794 if (htab->relbrlt != NULL && !info->enable_dt_relr)
84f5d08e 11795 {
f94498ff
AM
11796 /* Create a reloc for the branch lookup table entry. */
11797 Elf_Internal_Rela rela;
11798 bfd_byte *rl;
11799
11800 rela.r_offset = (br_entry->offset
11801 + htab->brlt->output_offset
11802 + htab->brlt->output_section->vma);
11803 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11804 rela.r_addend = targ;
f94498ff
AM
11805
11806 rl = htab->relbrlt->contents;
11807 rl += (htab->relbrlt->reloc_count++
11808 * sizeof (Elf64_External_Rela));
11809 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11810 }
11811 else if (info->emitrelocations)
11812 {
176a0d42
AM
11813 r = get_relocs (htab->brlt, 1);
11814 if (r == NULL)
0a1b45a2 11815 return false;
176a0d42
AM
11816 /* brlt, being SEC_LINKER_CREATED does not go through the
11817 normal reloc processing. Symbols and offsets are not
11818 translated from input file to output file form, so
11819 set up the offset per the output file. */
f94498ff
AM
11820 r->r_offset = (br_entry->offset
11821 + htab->brlt->output_offset
11822 + htab->brlt->output_section->vma);
11823 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11824 r->r_addend = targ;
84f5d08e 11825 }
84f5d08e 11826 }
721956f4 11827
1aa42141 11828 targ = (br_entry->offset
176a0d42
AM
11829 + htab->brlt->output_offset
11830 + htab->brlt->output_section->vma);
11831
1aa42141
AM
11832 off = (elf_gp (info->output_bfd)
11833 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11834 off = targ - off;
721956f4 11835
ad8e1ba5 11836 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11837 {
25f53a85 11838 info->callbacks->einfo
c1c8c1ef 11839 (_("%P: linkage table error against `%pT'\n"),
721956f4 11840 stub_entry->root.string);
5d1634d7 11841 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11842 htab->stub_error = true;
11843 return false;
5d1634d7 11844 }
41bd81ab 11845
176a0d42
AM
11846 if (info->emitrelocations)
11847 {
6f20ed8a 11848 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42 11849 if (r == NULL)
0a1b45a2 11850 return false;
6f20ed8a 11851 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11852 if (bfd_big_endian (info->output_bfd))
11853 r[0].r_offset += 2;
7aba54da 11854 if (stub_entry->type.r2save)
176a0d42
AM
11855 r[0].r_offset += 4;
11856 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11857 r[0].r_addend = targ;
176a0d42
AM
11858 if (PPC_HA (off) != 0)
11859 {
11860 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11861 r[1].r_offset = r[0].r_offset + 4;
11862 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11863 r[1].r_addend = r[0].r_addend;
11864 }
11865 }
11866
9e390558 11867 p = loc;
29433886 11868 obfd = htab->params->stub_bfd;
7aba54da 11869 if (!stub_entry->type.r2save)
ad8e1ba5 11870 {
176a0d42 11871 if (PPC_HA (off) != 0)
ac2df442 11872 {
29433886 11873 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11874 p += 4;
29433886 11875 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11876 }
11877 else
29433886 11878 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11879 }
11880 else
11881 {
25f53a85 11882 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11883
a7c49797 11884 if (r2off == (bfd_vma) -1)
aa374f67 11885 {
0a1b45a2
AM
11886 htab->stub_error = true;
11887 return false;
aa374f67 11888 }
ad8e1ba5 11889
29433886 11890 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11891 p += 4;
176a0d42 11892 if (PPC_HA (off) != 0)
ac2df442 11893 {
29433886 11894 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11895 p += 4;
29433886 11896 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11897 }
11898 else
29433886 11899 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11900
11901 if (PPC_HA (r2off) != 0)
11902 {
9e390558 11903 p += 4;
29433886 11904 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11905 }
11906 if (PPC_LO (r2off) != 0)
11907 {
9e390558 11908 p += 4;
29433886 11909 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11910 }
ad8e1ba5 11911 }
9e390558 11912 p += 4;
29433886 11913 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11914 p += 4;
29433886 11915 bfd_put_32 (obfd, BCTR, p);
407aa07c 11916 p += 4;
7aba54da
AM
11917 }
11918 else if (stub_entry->type.sub >= ppc_stub_notoc)
11919 {
11920 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
05d0e962 11921 p = loc;
f891966f 11922 off = (stub_entry->stub_offset
05d0e962
AM
11923 + stub_entry->group->stub_sec->output_offset
11924 + stub_entry->group->stub_sec->output_section->vma);
29433886 11925 obfd = htab->params->stub_bfd;
7aba54da 11926 is_tga = (is_plt
12cf8b93 11927 && stub_entry->h != NULL
29433886
AM
11928 && is_tls_get_addr (&stub_entry->h->elf, htab)
11929 && htab->params->tls_get_addr_opt);
11930 if (is_tga)
11931 {
11932 p = build_tls_get_addr_head (htab, stub_entry, p);
11933 off += p - loc;
11934 }
7aba54da 11935 if (stub_entry->type.r2save)
05d0e962
AM
11936 {
11937 off += 4;
29433886 11938 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11939 p += 4;
11940 }
7aba54da 11941 if (is_plt)
05d0e962
AM
11942 {
11943 targ = stub_entry->plt_ent->plt.offset & ~1;
11944 if (targ >= (bfd_vma) -2)
11945 abort ();
11946
11947 plt = htab->elf.splt;
30845f11 11948 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
11949 {
11950 if (stub_entry->symtype == STT_GNU_IFUNC)
11951 plt = htab->elf.iplt;
11952 else
11953 plt = htab->pltlocal;
11954 }
11955 targ += plt->output_offset + plt->output_section->vma;
11956 }
11957 else
11958 targ = (stub_entry->target_value
11959 + stub_entry->target_section->output_offset
11960 + stub_entry->target_section->output_section->vma);
04bdff6a 11961 odd = off & 4;
05d0e962 11962 off = targ - off;
f891966f 11963
3d58e1fc
AM
11964 relp = p;
11965 num_rel = 0;
7aba54da
AM
11966 if (stub_entry->type.sub == ppc_stub_notoc)
11967 p = build_power10_offset (obfd, p, off, odd, is_plt);
04bdff6a
AM
11968 else
11969 {
29433886
AM
11970 if (htab->glink_eh_frame != NULL
11971 && htab->glink_eh_frame->size != 0)
11972 {
11973 bfd_byte *base, *eh;
11974 unsigned int lr_used, delta;
11975
11976 base = (htab->glink_eh_frame->contents
11977 + stub_entry->group->eh_base + 17);
11978 eh = base + stub_entry->group->eh_size;
11979 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11980 delta = lr_used - stub_entry->group->lr_restore;
11981 stub_entry->group->lr_restore = lr_used + 8;
11982 eh = eh_advance (htab->elf.dynobj, eh, delta);
11983 *eh++ = DW_CFA_register;
11984 *eh++ = 65;
11985 *eh++ = 12;
11986 *eh++ = DW_CFA_advance_loc + 2;
11987 *eh++ = DW_CFA_restore_extended;
11988 *eh++ = 65;
11989 stub_entry->group->eh_size = eh - base;
11990 }
11991
04bdff6a
AM
11992 /* The notoc stubs calculate their target (either a PLT entry or
11993 the global entry point of a function) relative to the PC
11994 returned by the "bcl" two instructions past the start of the
11995 sequence emitted by build_offset. The offset is therefore 8
11996 less than calculated from the start of the sequence. */
11997 off -= 8;
7aba54da 11998 p = build_offset (obfd, p, off, is_plt);
04bdff6a
AM
11999 }
12000
7aba54da 12001 if (stub_entry->type.main == ppc_stub_long_branch)
05d0e962 12002 {
f891966f 12003 bfd_vma from;
3d58e1fc 12004 num_rel = 1;
f891966f
AM
12005 from = (stub_entry->stub_offset
12006 + stub_entry->group->stub_sec->output_offset
12007 + stub_entry->group->stub_sec->output_section->vma
12008 + (p - loc));
29433886 12009 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
12010 }
12011 else
12012 {
29433886 12013 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 12014 p += 4;
29433886 12015 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
12016 }
12017 p += 4;
df136d64 12018
29433886
AM
12019 if (is_tga)
12020 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12021
3d58e1fc
AM
12022 if (info->emitrelocations)
12023 {
04bdff6a 12024 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
7aba54da 12025 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12026 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12027 else
12028 {
12029 num_rel += num_relocs_for_offset (off);
12030 roff += 16;
12031 }
3d58e1fc
AM
12032 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12033 if (r == NULL)
0a1b45a2 12034 return false;
7aba54da 12035 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12036 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
12037 else
12038 r = emit_relocs_for_offset (info, r, roff, targ, off);
7aba54da 12039 if (stub_entry->type.main == ppc_stub_long_branch)
3d58e1fc
AM
12040 {
12041 ++r;
12042 roff = p - 4 - stub_entry->group->stub_sec->contents;
12043 r->r_offset = roff;
12044 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12045 r->r_addend = targ;
12046 if (stub_entry->h != NULL
12047 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
0a1b45a2 12048 return false;
3d58e1fc
AM
12049 }
12050 }
7aba54da
AM
12051 }
12052 else if (stub_entry->type.main == ppc_stub_plt_call)
12053 {
e054468f 12054 if (stub_entry->h != NULL
b31867b6
AM
12055 && stub_entry->h->is_func_descriptor
12056 && stub_entry->h->oh != NULL)
c862ae31 12057 {
b31867b6
AM
12058 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12059
12060 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 12061 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
12062 if (fh->elf.root.type == bfd_link_hash_undefined
12063 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12064 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 12065 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
12066 }
12067
721956f4 12068 /* Now build the stub. */
1aa42141
AM
12069 targ = stub_entry->plt_ent->plt.offset & ~1;
12070 if (targ >= (bfd_vma) -2)
721956f4
AM
12071 abort ();
12072
33e44f2e 12073 plt = htab->elf.splt;
30845f11 12074 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
12075 {
12076 if (stub_entry->symtype == STT_GNU_IFUNC)
12077 plt = htab->elf.iplt;
12078 else
12079 plt = htab->pltlocal;
12080 }
1aa42141 12081 targ += plt->output_offset + plt->output_section->vma;
e054468f 12082
1aa42141
AM
12083 off = (elf_gp (info->output_bfd)
12084 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12085 off = targ - off;
721956f4 12086
ad8e1ba5 12087 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 12088 {
25f53a85 12089 info->callbacks->einfo
695344c0 12090 /* xgettext:c-format */
c1c8c1ef 12091 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
12092 stub_entry->h != NULL
12093 ? stub_entry->h->elf.root.root.string
12094 : "<local sym>");
721956f4 12095 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
12096 htab->stub_error = true;
12097 return false;
721956f4
AM
12098 }
12099
176a0d42
AM
12100 r = NULL;
12101 if (info->emitrelocations)
12102 {
6f20ed8a 12103 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
12104 ((PPC_HA (off) != 0)
12105 + (htab->opd_abi
e7d1c40c 12106 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
12107 && PPC_HA (off + 16) == PPC_HA (off))
12108 : 1)));
176a0d42 12109 if (r == NULL)
0a1b45a2 12110 return false;
6f20ed8a 12111 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
12112 if (bfd_big_endian (info->output_bfd))
12113 r[0].r_offset += 2;
1aa42141 12114 r[0].r_addend = targ;
176a0d42 12115 }
29433886
AM
12116 p = loc;
12117 obfd = htab->params->stub_bfd;
12118 is_tga = (stub_entry->h != NULL
12119 && is_tls_get_addr (&stub_entry->h->elf, htab)
12120 && htab->params->tls_get_addr_opt);
12121 if (is_tga)
12122 {
12123 p = build_tls_get_addr_head (htab, stub_entry, p);
12124 if (r != NULL)
12125 r[0].r_offset += p - loc;
12126 }
12127 p = build_plt_stub (htab, stub_entry, p, off, r);
12128 if (is_tga)
12129 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
7aba54da
AM
12130 }
12131 else if (stub_entry->type.main == ppc_stub_save_res)
12132 return true;
12133 else
12134 {
721956f4 12135 BFD_FAIL ();
0a1b45a2 12136 return false;
721956f4
AM
12137 }
12138
1aa42141 12139 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 12140
e7d1c40c 12141 if (htab->params->emit_stub_syms)
97b639ba
AM
12142 {
12143 struct elf_link_hash_entry *h;
ee75fd95
AM
12144 size_t len1, len2;
12145 char *name;
12146 const char *const stub_str[] = { "long_branch",
05d0e962 12147 "plt_branch",
ee75fd95
AM
12148 "plt_call" };
12149
7aba54da 12150 len1 = strlen (stub_str[stub_entry->type.main - 1]);
ee75fd95
AM
12151 len2 = strlen (stub_entry->root.string);
12152 name = bfd_malloc (len1 + len2 + 2);
12153 if (name == NULL)
0a1b45a2 12154 return false;
ee75fd95 12155 memcpy (name, stub_entry->root.string, 9);
7aba54da 12156 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
ee75fd95 12157 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
0a1b45a2 12158 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
97b639ba 12159 if (h == NULL)
0a1b45a2 12160 return false;
97b639ba
AM
12161 if (h->root.type == bfd_link_hash_new)
12162 {
12163 h->root.type = bfd_link_hash_defined;
6f20ed8a 12164 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 12165 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
12166 h->ref_regular = 1;
12167 h->def_regular = 1;
12168 h->ref_regular_nonweak = 1;
12169 h->forced_local = 1;
12170 h->non_elf = 0;
2ec55de3 12171 h->root.linker_def = 1;
97b639ba
AM
12172 }
12173 }
12174
0a1b45a2 12175 return true;
721956f4
AM
12176}
12177
12178/* As above, but don't actually build the stub. Just bump offset so
12179 we know stub section sizes, and select plt_branch stubs where
12180 long_branch stubs won't do. */
12181
0a1b45a2 12182static bool
4ce794b7 12183ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
12184{
12185 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 12186 struct bfd_link_info *info;
721956f4 12187 struct ppc_link_hash_table *htab;
f891966f
AM
12188 asection *plt;
12189 bfd_vma targ, off, r2off;
04bdff6a 12190 unsigned int size, extra, lr_used, delta, odd;
0441f94f 12191 bfd_vma stub_offset;
721956f4
AM
12192
12193 /* Massage our args to the form they really have. */
12194 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
12195 info = in_arg;
12196
12197 htab = ppc_hash_table (info);
4dfe6ac6 12198 if (htab == NULL)
0a1b45a2 12199 return false;
721956f4 12200
abf874aa
CL
12201 /* Fail if the target section could not be assigned to an output
12202 section. The user should fix his linker script. */
12203 if (stub_entry->target_section != NULL
12204 && stub_entry->target_section->output_section == NULL
12205 && info->non_contiguous_regions)
53215f21
CL
12206 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12207 "Retry without --enable-non-contiguous-regions.\n"),
12208 stub_entry->target_section);
abf874aa
CL
12209
12210 /* Same for the group. */
12211 if (stub_entry->group->stub_sec != NULL
12212 && stub_entry->group->stub_sec->output_section == NULL
12213 && info->non_contiguous_regions)
53215f21
CL
12214 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12215 "output section. Retry without "
12216 "--enable-non-contiguous-regions.\n"),
12217 stub_entry->group->stub_sec,
12218 stub_entry->target_section);
abf874aa 12219
1aa42141 12220 /* Make a note of the offset within the stubs for this entry. */
0441f94f 12221 stub_offset = stub_entry->group->stub_sec->size;
2405fc40
AM
12222 if (htab->stub_iteration > STUB_SHRINK_ITER
12223 && stub_entry->stub_offset > stub_offset)
12224 stub_offset = stub_entry->stub_offset;
1aa42141 12225
a4b6fadd
AM
12226 if (stub_entry->h != NULL
12227 && stub_entry->h->save_res
12228 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12229 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12230 {
12231 /* Don't make stubs to out-of-line register save/restore
12232 functions. Instead, emit copies of the functions. */
12233 stub_entry->group->needs_save_res = 1;
7aba54da
AM
12234 stub_entry->type.main = ppc_stub_save_res;
12235 stub_entry->type.sub = ppc_stub_toc;
12236 stub_entry->type.r2save = 0;
0a1b45a2 12237 return true;
a4b6fadd
AM
12238 }
12239
7aba54da 12240 if (stub_entry->type.main == ppc_stub_plt_branch)
721956f4 12241 {
f891966f
AM
12242 /* Reset the stub type from the plt branch variant in case we now
12243 can reach with a shorter stub. */
7aba54da
AM
12244 stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
12245 }
12246
12247 if (stub_entry->type.main == ppc_stub_long_branch
12248 && stub_entry->type.sub == ppc_stub_toc)
12249 {
1aa42141
AM
12250 targ = (stub_entry->target_value
12251 + stub_entry->target_section->output_offset
12252 + stub_entry->target_section->output_section->vma);
f891966f 12253 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
0441f94f 12254 off = (stub_offset
1aa42141
AM
12255 + stub_entry->group->stub_sec->output_offset
12256 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12257
ad8e1ba5 12258 size = 4;
f891966f 12259 r2off = 0;
7aba54da 12260 if (stub_entry->type.r2save)
ad8e1ba5 12261 {
25f53a85 12262 r2off = get_r2off (info, stub_entry);
a7c49797 12263 if (r2off == (bfd_vma) -1)
aa374f67 12264 {
0a1b45a2
AM
12265 htab->stub_error = true;
12266 return false;
aa374f67 12267 }
a7c49797 12268 size = 8;
ac2df442 12269 if (PPC_HA (r2off) != 0)
a7c49797
AM
12270 size += 4;
12271 if (PPC_LO (r2off) != 0)
12272 size += 4;
1aa42141 12273 off += size - 4;
ad8e1ba5 12274 }
1aa42141 12275 off = targ - off;
ad8e1ba5 12276
f891966f
AM
12277 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12278 Do the same for -R objects without function descriptors. */
7aba54da 12279 if ((stub_entry->type.r2save
f891966f
AM
12280 && r2off == 0
12281 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12282 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12283 {
f891966f 12284 struct ppc_branch_hash_entry *br_entry;
df136d64 12285
f891966f
AM
12286 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12287 stub_entry->root.string + 9,
0a1b45a2 12288 true, false);
f891966f 12289 if (br_entry == NULL)
721956f4 12290 {
f891966f
AM
12291 _bfd_error_handler (_("can't build branch stub `%s'"),
12292 stub_entry->root.string);
0a1b45a2
AM
12293 htab->stub_error = true;
12294 return false;
721956f4
AM
12295 }
12296
f891966f 12297 if (br_entry->iter != htab->stub_iteration)
721956f4 12298 {
f891966f
AM
12299 br_entry->iter = htab->stub_iteration;
12300 br_entry->offset = htab->brlt->size;
12301 htab->brlt->size += 8;
63bc6f6c 12302
1657026c 12303 if (htab->relbrlt != NULL && !info->enable_dt_relr)
f891966f
AM
12304 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12305 else if (info->emitrelocations)
84f5d08e 12306 {
f891966f
AM
12307 htab->brlt->reloc_count += 1;
12308 htab->brlt->flags |= SEC_RELOC;
05d0e962 12309 }
f891966f 12310 }
ac2df442 12311
f891966f
AM
12312 targ = (br_entry->offset
12313 + htab->brlt->output_offset
12314 + htab->brlt->output_section->vma);
12315 off = (elf_gp (info->output_bfd)
12316 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12317 off = targ - off;
176a0d42 12318
f891966f
AM
12319 if (info->emitrelocations)
12320 {
12321 stub_entry->group->stub_sec->reloc_count
12322 += 1 + (PPC_HA (off) != 0);
12323 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12324 }
05d0e962 12325
7aba54da
AM
12326 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12327 if (!stub_entry->type.r2save)
f891966f
AM
12328 {
12329 size = 12;
12330 if (PPC_HA (off) != 0)
12331 size = 16;
ac2df442 12332 }
f891966f 12333 else
ac2df442 12334 {
f891966f
AM
12335 size = 16;
12336 if (PPC_HA (off) != 0)
12337 size += 4;
12338
12339 if (PPC_HA (r2off) != 0)
12340 size += 4;
12341 if (PPC_LO (r2off) != 0)
12342 size += 4;
ac2df442 12343 }
721956f4 12344 }
f891966f
AM
12345 else if (info->emitrelocations)
12346 {
12347 stub_entry->group->stub_sec->reloc_count += 1;
12348 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12349 }
7aba54da
AM
12350 }
12351 else if (stub_entry->type.main == ppc_stub_long_branch)
12352 {
0441f94f 12353 off = (stub_offset
f891966f
AM
12354 + stub_entry->group->stub_sec->output_offset
12355 + stub_entry->group->stub_sec->output_section->vma);
12356 size = 0;
7aba54da 12357 if (stub_entry->type.r2save)
f891966f
AM
12358 size = 4;
12359 off += size;
12360 targ = (stub_entry->target_value
12361 + stub_entry->target_section->output_offset
12362 + stub_entry->target_section->output_section->vma);
04bdff6a 12363 odd = off & 4;
f891966f
AM
12364 off = targ - off;
12365
3d58e1fc
AM
12366 if (info->emitrelocations)
12367 {
04bdff6a 12368 unsigned int num_rel;
7aba54da 12369 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12370 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12371 else
12372 num_rel = num_relocs_for_offset (off - 8);
12373 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12374 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12375 }
12376
7aba54da 12377 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12378 extra = size_power10_offset (off, odd);
04bdff6a
AM
12379 else
12380 extra = size_offset (off - 8);
f891966f
AM
12381 /* Include branch insn plus those in the offset sequence. */
12382 size += 4 + extra;
12383 /* The branch insn is at the end, or "extra" bytes along. So
12384 its offset will be "extra" bytes less that that already
12385 calculated. */
12386 off -= extra;
12387
7aba54da 12388 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12389 {
12390 /* After the bcl, lr has been modified so we need to emit
12391 .eh_frame info saying the return address is in r12. */
0441f94f 12392 lr_used = stub_offset + 8;
7aba54da 12393 if (stub_entry->type.r2save)
04bdff6a
AM
12394 lr_used += 4;
12395 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12396 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12397 DW_CFA_restore_extended 65. */
12398 delta = lr_used - stub_entry->group->lr_restore;
12399 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12400 stub_entry->group->lr_restore = lr_used + 8;
12401 }
f891966f
AM
12402
12403 /* If the branch can't reach, use a plt_branch. */
12404 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12405 {
7aba54da 12406 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
f891966f
AM
12407 size += 4;
12408 }
3d58e1fc
AM
12409 else if (info->emitrelocations)
12410 stub_entry->group->stub_sec->reloc_count +=1;
7aba54da
AM
12411 }
12412 else if (stub_entry->type.sub >= ppc_stub_notoc)
12413 {
12414 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
29433886 12415 lr_used = 0;
12cf8b93
AM
12416 if (stub_entry->h != NULL
12417 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12418 && htab->params->tls_get_addr_opt)
12419 {
12420 lr_used += 7 * 4;
12421 if (!htab->params->no_tls_get_addr_regsave)
12422 lr_used += 11 * 4;
7aba54da 12423 else if (stub_entry->type.r2save)
29433886
AM
12424 lr_used += 2 * 4;
12425 }
7aba54da 12426 if (stub_entry->type.r2save)
29433886 12427 lr_used += 4;
f891966f
AM
12428 targ = stub_entry->plt_ent->plt.offset & ~1;
12429 if (targ >= (bfd_vma) -2)
12430 abort ();
12431
12432 plt = htab->elf.splt;
30845f11 12433 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12434 {
12435 if (stub_entry->symtype == STT_GNU_IFUNC)
12436 plt = htab->elf.iplt;
12437 else
12438 plt = htab->pltlocal;
12439 }
12440 targ += plt->output_offset + plt->output_section->vma;
0441f94f 12441 off = (stub_offset
29433886
AM
12442 + stub_entry->group->stub_sec->output_offset
12443 + stub_entry->group->stub_sec->output_section->vma
12444 + lr_used);
04bdff6a 12445 odd = off & 4;
f891966f
AM
12446 off = targ - off;
12447
12448 if (htab->params->plt_stub_align != 0)
12449 {
2405fc40 12450 unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, odd);
f891966f 12451
2405fc40 12452 stub_offset += pad;
f891966f 12453 off -= pad;
29433886 12454 odd ^= pad & 4;
f891966f
AM
12455 }
12456
3d58e1fc
AM
12457 if (info->emitrelocations)
12458 {
04bdff6a 12459 unsigned int num_rel;
7aba54da 12460 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12461 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12462 else
12463 num_rel = num_relocs_for_offset (off - 8);
12464 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12465 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12466 }
12467
29433886 12468 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12469
7aba54da 12470 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12471 {
12472 /* After the bcl, lr has been modified so we need to emit
12473 .eh_frame info saying the return address is in r12. */
0441f94f 12474 lr_used += stub_offset + 8;
04bdff6a
AM
12475 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12476 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12477 DW_CFA_restore_extended 65. */
12478 delta = lr_used - stub_entry->group->lr_restore;
12479 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12480 stub_entry->group->lr_restore = lr_used + 8;
12481 }
7aba54da 12482 if (stub_entry->h != NULL
29433886
AM
12483 && is_tls_get_addr (&stub_entry->h->elf, htab)
12484 && htab->params->tls_get_addr_opt)
12485 {
12486 if (!htab->params->no_tls_get_addr_regsave)
12487 {
0441f94f 12488 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12489 delta = cfa_updt - stub_entry->group->lr_restore;
12490 stub_entry->group->eh_size += eh_advance_size (delta);
12491 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
0441f94f 12492 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886 12493 }
7aba54da 12494 else if (stub_entry->type.r2save)
29433886 12495 {
0441f94f 12496 lr_used = stub_offset + size - 20;
29433886
AM
12497 delta = lr_used - stub_entry->group->lr_restore;
12498 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
0441f94f 12499 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886
AM
12500 }
12501 }
7aba54da
AM
12502 }
12503 else if (stub_entry->type.main == ppc_stub_plt_call)
12504 {
f891966f
AM
12505 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12506 if (targ >= (bfd_vma) -2)
12507 abort ();
12508 plt = htab->elf.splt;
30845f11 12509 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12510 {
12511 if (stub_entry->symtype == STT_GNU_IFUNC)
12512 plt = htab->elf.iplt;
12513 else
12514 plt = htab->pltlocal;
12515 }
12516 targ += plt->output_offset + plt->output_section->vma;
12517
12518 off = (elf_gp (info->output_bfd)
12519 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12520 off = targ - off;
12521
12522 if (htab->params->plt_stub_align != 0)
12523 {
2405fc40 12524 unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, 0);
f891966f 12525
2405fc40 12526 stub_offset += pad;
f891966f
AM
12527 }
12528
12529 if (info->emitrelocations)
12530 {
12531 stub_entry->group->stub_sec->reloc_count
12532 += ((PPC_HA (off) != 0)
12533 + (htab->opd_abi
12534 ? 2 + (htab->params->plt_static_chain
12535 && PPC_HA (off + 16) == PPC_HA (off))
12536 : 1));
12537 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12538 }
12539
29433886 12540 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12541
12542 if (stub_entry->h != NULL
ed7007c1 12543 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f 12544 && htab->params->tls_get_addr_opt
7aba54da 12545 && stub_entry->type.r2save)
f891966f 12546 {
29433886
AM
12547 if (!htab->params->no_tls_get_addr_regsave)
12548 {
12549 /* Adjustments to r1 need to be described. */
0441f94f 12550 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12551 delta = cfa_updt - stub_entry->group->lr_restore;
12552 stub_entry->group->eh_size += eh_advance_size (delta);
12553 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12554 }
12555 else
9e7028aa 12556 {
0441f94f 12557 lr_used = stub_offset + size - 20;
9e7028aa
AM
12558 /* The eh_frame info will consist of a DW_CFA_advance_loc
12559 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12560 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12561 delta = lr_used - stub_entry->group->lr_restore;
12562 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12563 }
0441f94f 12564 stub_entry->group->lr_restore = stub_offset + size - 4;
f891966f 12565 }
7aba54da
AM
12566 }
12567 else
12568 {
f891966f 12569 BFD_FAIL ();
0a1b45a2 12570 return false;
721956f4
AM
12571 }
12572
0441f94f
AM
12573 if (stub_entry->stub_offset != stub_offset)
12574 htab->stub_changed = true;
2405fc40
AM
12575 stub_entry->stub_offset = stub_offset;
12576 stub_entry->group->stub_sec->size = stub_offset + size;
0a1b45a2 12577 return true;
721956f4
AM
12578}
12579
12580/* Set up various things so that we can make a list of input sections
12581 for each output section included in the link. Returns -1 on error,
cedb70c5 12582 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12583
12584int
e7d1c40c 12585ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12586{
6f20ed8a 12587 unsigned int id;
986f0783 12588 size_t amt;
721956f4
AM
12589 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12590
4dfe6ac6
NC
12591 if (htab == NULL)
12592 return -1;
4c52953f 12593
7cf7fcc8 12594 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12595 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12596 htab->sec_info = bfd_zmalloc (amt);
12597 if (htab->sec_info == NULL)
721956f4
AM
12598 return -1;
12599
3d6f9012
AM
12600 /* Set toc_off for com, und, abs and ind sections. */
12601 for (id = 0; id < 3; id++)
6f20ed8a 12602 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12603
721956f4
AM
12604 return 1;
12605}
12606
927be08e
AM
12607/* Set up for first pass at multitoc partitioning. */
12608
12609void
12610ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12611{
12612 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12613
1c865ab2 12614 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12615 htab->toc_bfd = NULL;
12616 htab->toc_first_sec = NULL;
12617}
12618
e717da7e
AM
12619/* The linker repeatedly calls this function for each TOC input section
12620 and linker generated GOT section. Group input bfds such that the toc
927be08e 12621 within a group is less than 64k in size. */
ad8e1ba5 12622
0a1b45a2 12623bool
4ce794b7 12624ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12625{
12626 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12627 bfd_vma addr, off, limit;
ad8e1ba5 12628
4dfe6ac6 12629 if (htab == NULL)
0a1b45a2 12630 return false;
4dfe6ac6 12631
927be08e 12632 if (!htab->second_toc_pass)
4c52953f 12633 {
927be08e 12634 /* Keep track of the first .toc or .got section for this input bfd. */
0a1b45a2 12635 bool new_bfd = htab->toc_bfd != isec->owner;
a4fd3de5
AM
12636
12637 if (new_bfd)
bf102f86
AM
12638 {
12639 htab->toc_bfd = isec->owner;
12640 htab->toc_first_sec = isec;
12641 }
927be08e 12642
bf102f86
AM
12643 addr = isec->output_offset + isec->output_section->vma;
12644 off = addr - htab->toc_curr;
d77c8a4b
AM
12645 limit = 0x80008000;
12646 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12647 limit = 0x10000;
12648 if (off + isec->size > limit)
bf102f86
AM
12649 {
12650 addr = (htab->toc_first_sec->output_offset
12651 + htab->toc_first_sec->output_section->vma);
12652 htab->toc_curr = addr;
a27e685f 12653 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12654 }
99877b66 12655
927be08e
AM
12656 /* toc_curr is the base address of this toc group. Set elf_gp
12657 for the input section to be the offset relative to the
12658 output toc base plus 0x8000. Making the input elf_gp an
12659 offset allows us to move the toc as a whole without
12660 recalculating input elf_gp. */
06bcf541 12661 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12662 off += TOC_BASE_OFF;
12663
12664 /* Die if someone uses a linker script that doesn't keep input
12665 file .toc and .got together. */
a4fd3de5
AM
12666 if (new_bfd
12667 && elf_gp (isec->owner) != 0
927be08e 12668 && elf_gp (isec->owner) != off)
0a1b45a2 12669 return false;
927be08e
AM
12670
12671 elf_gp (isec->owner) = off;
0a1b45a2 12672 return true;
4c52953f 12673 }
927be08e
AM
12674
12675 /* During the second pass toc_first_sec points to the start of
12676 a toc group, and toc_curr is used to track the old elf_gp.
12677 We use toc_bfd to ensure we only look at each bfd once. */
12678 if (htab->toc_bfd == isec->owner)
0a1b45a2 12679 return true;
927be08e
AM
12680 htab->toc_bfd = isec->owner;
12681
12682 if (htab->toc_first_sec == NULL
12683 || htab->toc_curr != elf_gp (isec->owner))
12684 {
12685 htab->toc_curr = elf_gp (isec->owner);
12686 htab->toc_first_sec = isec;
12687 }
12688 addr = (htab->toc_first_sec->output_offset
12689 + htab->toc_first_sec->output_section->vma);
06bcf541 12690 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12691 elf_gp (isec->owner) = off;
12692
0a1b45a2 12693 return true;
ad8e1ba5
AM
12694}
12695
927be08e
AM
12696/* Called via elf_link_hash_traverse to merge GOT entries for global
12697 symbol H. */
12698
0a1b45a2 12699static bool
927be08e
AM
12700merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12701{
12702 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12703 return true;
927be08e 12704
927be08e
AM
12705 merge_got_entries (&h->got.glist);
12706
0a1b45a2 12707 return true;
927be08e
AM
12708}
12709
12710/* Called via elf_link_hash_traverse to allocate GOT entries for global
12711 symbol H. */
12712
0a1b45a2 12713static bool
927be08e
AM
12714reallocate_got (struct elf_link_hash_entry *h, void *inf)
12715{
12716 struct got_entry *gent;
12717
12718 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12719 return true;
927be08e 12720
927be08e
AM
12721 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12722 if (!gent->is_indirect)
12723 allocate_got (h, (struct bfd_link_info *) inf, gent);
0a1b45a2 12724 return true;
927be08e
AM
12725}
12726
12727/* Called on the first multitoc pass after the last call to
12728 ppc64_elf_next_toc_section. This function removes duplicate GOT
12729 entries. */
12730
0a1b45a2 12731bool
927be08e 12732ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12733{
12734 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e 12735 struct bfd *ibfd, *ibfd2;
0a1b45a2 12736 bool done_something;
927be08e
AM
12737
12738 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12739
7865406b 12740 if (!htab->do_multi_toc)
0a1b45a2 12741 return false;
7865406b 12742
d0fae19d 12743 /* Merge global sym got entries within a toc group. */
927be08e
AM
12744 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12745
12746 /* And tlsld_got. */
c72f2fb2 12747 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12748 {
12749 struct got_entry *ent, *ent2;
12750
12751 if (!is_ppc64_elf (ibfd))
12752 continue;
12753
12754 ent = ppc64_tlsld_got (ibfd);
12755 if (!ent->is_indirect
12756 && ent->got.offset != (bfd_vma) -1)
12757 {
c72f2fb2 12758 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12759 {
12760 if (!is_ppc64_elf (ibfd2))
12761 continue;
12762
12763 ent2 = ppc64_tlsld_got (ibfd2);
12764 if (!ent2->is_indirect
12765 && ent2->got.offset != (bfd_vma) -1
12766 && elf_gp (ibfd2) == elf_gp (ibfd))
12767 {
0a1b45a2 12768 ent2->is_indirect = true;
927be08e
AM
12769 ent2->got.ent = ent;
12770 }
12771 }
12772 }
12773 }
12774
12775 /* Zap sizes of got sections. */
33e44f2e
AM
12776 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12777 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12778 htab->got_reli_size = 0;
12779
c72f2fb2 12780 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12781 {
12782 asection *got, *relgot;
12783
12784 if (!is_ppc64_elf (ibfd))
12785 continue;
12786
12787 got = ppc64_elf_tdata (ibfd)->got;
12788 if (got != NULL)
12789 {
12790 got->rawsize = got->size;
12791 got->size = 0;
12792 relgot = ppc64_elf_tdata (ibfd)->relgot;
12793 relgot->rawsize = relgot->size;
12794 relgot->size = 0;
12795 }
12796 }
12797
12798 /* Now reallocate the got, local syms first. We don't need to
12799 allocate section contents again since we never increase size. */
c72f2fb2 12800 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12801 {
12802 struct got_entry **lgot_ents;
12803 struct got_entry **end_lgot_ents;
12804 struct plt_entry **local_plt;
12805 struct plt_entry **end_local_plt;
f961d9dd 12806 unsigned char *lgot_masks;
927be08e
AM
12807 bfd_size_type locsymcount;
12808 Elf_Internal_Shdr *symtab_hdr;
19e08130 12809 asection *s;
3a3a4c1f
AM
12810 Elf_Internal_Sym *local_syms;
12811 Elf_Internal_Sym *isym;
927be08e
AM
12812
12813 if (!is_ppc64_elf (ibfd))
12814 continue;
12815
12816 lgot_ents = elf_local_got_ents (ibfd);
12817 if (!lgot_ents)
12818 continue;
12819
12820 symtab_hdr = &elf_symtab_hdr (ibfd);
12821 locsymcount = symtab_hdr->sh_info;
12822 end_lgot_ents = lgot_ents + locsymcount;
12823 local_plt = (struct plt_entry **) end_lgot_ents;
12824 end_local_plt = local_plt + locsymcount;
f961d9dd 12825 lgot_masks = (unsigned char *) end_local_plt;
c9fecd62
AM
12826 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12827 if (local_syms == NULL && locsymcount != 0)
3a3a4c1f 12828 {
c9fecd62
AM
12829 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12830 0, NULL, NULL, NULL);
12831 if (local_syms == NULL)
12832 return false;
3a3a4c1f 12833 }
927be08e 12834 s = ppc64_elf_tdata (ibfd)->got;
3a3a4c1f
AM
12835 for (isym = local_syms;
12836 lgot_ents < end_lgot_ents;
c9fecd62 12837 ++lgot_ents, ++lgot_masks, isym++)
927be08e
AM
12838 {
12839 struct got_entry *ent;
12840
12841 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12842 {
19e08130
AM
12843 unsigned int ent_size = 8;
12844 unsigned int rel_size = sizeof (Elf64_External_Rela);
12845
d0fae19d
AM
12846 ent->got.offset = s->size;
12847 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12848 {
19e08130
AM
12849 ent_size *= 2;
12850 rel_size *= 2;
12851 }
12852 s->size += ent_size;
37da22e5 12853 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12854 {
33e44f2e 12855 htab->elf.irelplt->size += rel_size;
19e08130
AM
12856 htab->got_reli_size += rel_size;
12857 }
6a3858a6 12858 else if (bfd_link_pic (info)
8f22c953
AM
12859 && (ent->tls_type == 0
12860 ? !info->enable_dt_relr
3a3a4c1f
AM
12861 : !bfd_link_executable (info))
12862 && isym->st_shndx != SHN_ABS)
19e08130
AM
12863 {
12864 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12865 srel->size += rel_size;
d0fae19d
AM
12866 }
12867 }
927be08e
AM
12868 }
12869 }
12870
12871 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12872
c72f2fb2 12873 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12874 {
12875 struct got_entry *ent;
12876
12877 if (!is_ppc64_elf (ibfd))
12878 continue;
12879
12880 ent = ppc64_tlsld_got (ibfd);
12881 if (!ent->is_indirect
12882 && ent->got.offset != (bfd_vma) -1)
12883 {
12884 asection *s = ppc64_elf_tdata (ibfd)->got;
12885 ent->got.offset = s->size;
12886 s->size += 16;
f749f26e 12887 if (bfd_link_dll (info))
927be08e
AM
12888 {
12889 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12890 srel->size += sizeof (Elf64_External_Rela);
12891 }
12892 }
12893 }
12894
33e44f2e 12895 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12896 if (!done_something)
c72f2fb2 12897 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12898 {
12899 asection *got;
12900
12901 if (!is_ppc64_elf (ibfd))
12902 continue;
12903
12904 got = ppc64_elf_tdata (ibfd)->got;
12905 if (got != NULL)
12906 {
12907 done_something = got->rawsize != got->size;
12908 if (done_something)
12909 break;
12910 }
12911 }
12912
12913 if (done_something)
e7d1c40c 12914 (*htab->params->layout_sections_again) ();
927be08e
AM
12915
12916 /* Set up for second pass over toc sections to recalculate elf_gp
12917 on input sections. */
12918 htab->toc_bfd = NULL;
12919 htab->toc_first_sec = NULL;
0a1b45a2 12920 htab->second_toc_pass = true;
927be08e
AM
12921 return done_something;
12922}
12923
12924/* Called after second pass of multitoc partitioning. */
12925
12926void
12927ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12928{
12929 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12930
12931 /* After the second pass, toc_curr tracks the TOC offset used
12932 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12933 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12934}
12935
9b5ecbd0
AM
12936/* No toc references were found in ISEC. If the code in ISEC makes no
12937 calls, then there's no need to use toc adjusting stubs when branching
12938 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12939 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12940 needed, and 2 if a cyclical call-graph was found but no other reason
12941 for a stub was detected. If called from the top level, a return of
12942 2 means the same as a return of 0. */
9b5ecbd0
AM
12943
12944static int
4ce794b7 12945toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12946{
9b5ecbd0 12947 int ret;
70cc837d
AM
12948
12949 /* Mark this section as checked. */
12950 isec->call_check_done = 1;
9b5ecbd0 12951
772119ce
AM
12952 /* We know none of our code bearing sections will need toc stubs. */
12953 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12954 return 0;
12955
eea6121a 12956 if (isec->size == 0)
082c50f8
AM
12957 return 0;
12958
4c52953f
AM
12959 if (isec->output_section == NULL)
12960 return 0;
12961
4c52953f 12962 ret = 0;
70cc837d 12963 if (isec->reloc_count != 0)
9b5ecbd0 12964 {
70cc837d
AM
12965 Elf_Internal_Rela *relstart, *rel;
12966 Elf_Internal_Sym *local_syms;
12967 struct ppc_link_hash_table *htab;
2917689a 12968
70cc837d
AM
12969 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12970 info->keep_memory);
12971 if (relstart == NULL)
12972 return -1;
90aecf7a 12973
70cc837d
AM
12974 /* Look for branches to outside of this section. */
12975 local_syms = NULL;
12976 htab = ppc_hash_table (info);
12977 if (htab == NULL)
12978 return -1;
4c52953f 12979
70cc837d 12980 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12981 {
70cc837d
AM
12982 enum elf_ppc64_reloc_type r_type;
12983 unsigned long r_symndx;
12984 struct elf_link_hash_entry *h;
12985 struct ppc_link_hash_entry *eh;
12986 Elf_Internal_Sym *sym;
12987 asection *sym_sec;
12988 struct _opd_sec_data *opd;
12989 bfd_vma sym_value;
12990 bfd_vma dest;
12991
12992 r_type = ELF64_R_TYPE (rel->r_info);
12993 if (r_type != R_PPC64_REL24
05d0e962 12994 && r_type != R_PPC64_REL24_NOTOC
7aba54da 12995 && r_type != R_PPC64_REL24_P9NOTOC
70cc837d
AM
12996 && r_type != R_PPC64_REL14
12997 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12998 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12999 && r_type != R_PPC64_PLTCALL
13000 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 13001 continue;
4c52953f 13002
70cc837d
AM
13003 r_symndx = ELF64_R_SYM (rel->r_info);
13004 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13005 isec->owner))
4c52953f 13006 {
70cc837d
AM
13007 ret = -1;
13008 break;
13009 }
4c52953f 13010
70cc837d
AM
13011 /* Calls to dynamic lib functions go through a plt call stub
13012 that uses r2. */
ed7007c1 13013 eh = ppc_elf_hash_entry (h);
70cc837d
AM
13014 if (eh != NULL
13015 && (eh->elf.plt.plist != NULL
13016 || (eh->oh != NULL
13017 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13018 {
13019 ret = 1;
13020 break;
4c52953f
AM
13021 }
13022
70cc837d
AM
13023 if (sym_sec == NULL)
13024 /* Ignore other undefined symbols. */
4c52953f 13025 continue;
4c52953f 13026
70cc837d
AM
13027 /* Assume branches to other sections not included in the
13028 link need stubs too, to cover -R and absolute syms. */
13029 if (sym_sec->output_section == NULL)
13030 {
13031 ret = 1;
13032 break;
13033 }
4c52953f 13034
70cc837d
AM
13035 if (h == NULL)
13036 sym_value = sym->st_value;
13037 else
13038 {
13039 if (h->root.type != bfd_link_hash_defined
13040 && h->root.type != bfd_link_hash_defweak)
13041 abort ();
13042 sym_value = h->root.u.def.value;
13043 }
13044 sym_value += rel->r_addend;
4c52953f 13045
70cc837d
AM
13046 /* If this branch reloc uses an opd sym, find the code section. */
13047 opd = get_opd_info (sym_sec);
13048 if (opd != NULL)
13049 {
13050 if (h == NULL && opd->adjust != NULL)
13051 {
13052 long adjust;
4c52953f 13053
92a9c616 13054 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
13055 if (adjust == -1)
13056 /* Assume deleted functions won't ever be called. */
13057 continue;
13058 sym_value += adjust;
13059 }
4c52953f 13060
aef36ac1 13061 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13062 &sym_sec, NULL, false);
70cc837d
AM
13063 if (dest == (bfd_vma) -1)
13064 continue;
13065 }
13066 else
13067 dest = (sym_value
13068 + sym_sec->output_offset
13069 + sym_sec->output_section->vma);
4c52953f 13070
70cc837d
AM
13071 /* Ignore branch to self. */
13072 if (sym_sec == isec)
13073 continue;
4c52953f 13074
70cc837d
AM
13075 /* If the called function uses the toc, we need a stub. */
13076 if (sym_sec->has_toc_reloc
13077 || sym_sec->makes_toc_func_call)
4c52953f 13078 {
70cc837d 13079 ret = 1;
4c52953f
AM
13080 break;
13081 }
70cc837d
AM
13082
13083 /* Assume any branch that needs a long branch stub might in fact
13084 need a plt_branch stub. A plt_branch stub uses r2. */
13085 else if (dest - (isec->output_offset
13086 + isec->output_section->vma
6911b7dc
AM
13087 + rel->r_offset) + (1 << 25)
13088 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13089 ? h->other
13090 : sym->st_other))
4c52953f 13091 {
70cc837d
AM
13092 ret = 1;
13093 break;
13094 }
13095
13096 /* If calling back to a section in the process of being
13097 tested, we can't say for sure that no toc adjusting stubs
13098 are needed, so don't return zero. */
13099 else if (sym_sec->call_check_in_progress)
13100 ret = 2;
13101
13102 /* Branches to another section that itself doesn't have any TOC
13103 references are OK. Recursively call ourselves to check. */
13104 else if (!sym_sec->call_check_done)
13105 {
13106 int recur;
13107
13108 /* Mark current section as indeterminate, so that other
13109 sections that call back to current won't be marked as
13110 known. */
13111 isec->call_check_in_progress = 1;
13112 recur = toc_adjusting_stub_needed (info, sym_sec);
13113 isec->call_check_in_progress = 0;
13114
4c52953f
AM
13115 if (recur != 0)
13116 {
70cc837d
AM
13117 ret = recur;
13118 if (recur != 2)
13119 break;
4c52953f
AM
13120 }
13121 }
4c52953f 13122 }
70cc837d 13123
c9594989
AM
13124 if (elf_symtab_hdr (isec->owner).contents
13125 != (unsigned char *) local_syms)
70cc837d
AM
13126 free (local_syms);
13127 if (elf_section_data (isec)->relocs != relstart)
13128 free (relstart);
9b5ecbd0
AM
13129 }
13130
70cc837d
AM
13131 if ((ret & 1) == 0
13132 && isec->map_head.s != NULL
13133 && (strcmp (isec->output_section->name, ".init") == 0
13134 || strcmp (isec->output_section->name, ".fini") == 0))
13135 {
13136 if (isec->map_head.s->has_toc_reloc
13137 || isec->map_head.s->makes_toc_func_call)
13138 ret = 1;
13139 else if (!isec->map_head.s->call_check_done)
13140 {
13141 int recur;
13142 isec->call_check_in_progress = 1;
13143 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13144 isec->call_check_in_progress = 0;
13145 if (recur != 0)
13146 ret = recur;
13147 }
13148 }
13149
13150 if (ret == 1)
13151 isec->makes_toc_func_call = 1;
4c52953f 13152
9b5ecbd0
AM
13153 return ret;
13154}
13155
721956f4
AM
13156/* The linker repeatedly calls this function for each input section,
13157 in the order that input sections are linked into output sections.
13158 Build lists of input sections to determine groupings between which
13159 we may insert linker stubs. */
13160
0a1b45a2 13161bool
4ce794b7 13162ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
13163{
13164 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13165
4dfe6ac6 13166 if (htab == NULL)
0a1b45a2 13167 return false;
4dfe6ac6 13168
734b6cf9 13169 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 13170 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 13171 {
3d6f9012
AM
13172 /* This happens to make the list in reverse order,
13173 which is what we want. */
6f20ed8a
AM
13174 htab->sec_info[isec->id].u.list
13175 = htab->sec_info[isec->output_section->id].u.list;
13176 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 13177 }
ad8e1ba5 13178
4c52953f 13179 if (htab->multi_toc_needed)
9b5ecbd0 13180 {
8b974ba3
AM
13181 /* Analyse sections that aren't already flagged as needing a
13182 valid toc pointer. Exclude .fixup for the linux kernel.
13183 .fixup contains branches, but only back to the function that
13184 hit an exception. */
13185 if (!(isec->has_toc_reloc
13186 || (isec->flags & SEC_CODE) == 0
13187 || strcmp (isec->name, ".fixup") == 0
13188 || isec->call_check_done))
13189 {
13190 if (toc_adjusting_stub_needed (info, isec) < 0)
0a1b45a2 13191 return false;
8b974ba3
AM
13192 }
13193 /* Make all sections use the TOC assigned for this object file.
13194 This will be wrong for pasted sections; We fix that in
13195 check_pasted_section(). */
13196 if (elf_gp (isec->owner) != 0)
13197 htab->toc_curr = elf_gp (isec->owner);
13198 }
13199
6f20ed8a 13200 htab->sec_info[isec->id].toc_off = htab->toc_curr;
0a1b45a2 13201 return true;
721956f4
AM
13202}
13203
70cc837d
AM
13204/* Check that all .init and .fini sections use the same toc, if they
13205 have toc relocs. */
13206
0a1b45a2 13207static bool
70cc837d
AM
13208check_pasted_section (struct bfd_link_info *info, const char *name)
13209{
13210 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13211
13212 if (o != NULL)
13213 {
13214 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13215 bfd_vma toc_off = 0;
13216 asection *i;
13217
13218 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13219 if (i->has_toc_reloc)
13220 {
13221 if (toc_off == 0)
6f20ed8a
AM
13222 toc_off = htab->sec_info[i->id].toc_off;
13223 else if (toc_off != htab->sec_info[i->id].toc_off)
0a1b45a2 13224 return false;
70cc837d 13225 }
6683a28d
AM
13226
13227 if (toc_off == 0)
13228 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13229 if (i->makes_toc_func_call)
13230 {
6f20ed8a 13231 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13232 break;
13233 }
13234
70cc837d
AM
13235 /* Make sure the whole pasted function uses the same toc offset. */
13236 if (toc_off != 0)
13237 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13238 htab->sec_info[i->id].toc_off = toc_off;
70cc837d 13239 }
0a1b45a2 13240 return true;
70cc837d
AM
13241}
13242
0a1b45a2 13243bool
70cc837d
AM
13244ppc64_elf_check_init_fini (struct bfd_link_info *info)
13245{
0c80c644
SM
13246 bool ret1 = check_pasted_section (info, ".init");
13247 bool ret2 = check_pasted_section (info, ".fini");
13248
13249 return ret1 && ret2;
70cc837d
AM
13250}
13251
721956f4
AM
13252/* See whether we can group stub sections together. Grouping stub
13253 sections may result in fewer stubs. More importantly, we need to
13254 put all .init* and .fini* stubs at the beginning of the .init or
13255 .fini output sections respectively, because glibc splits the
13256 _init and _fini functions into multiple parts. Putting a stub in
13257 the middle of a function is not a good idea. */
13258
0a1b45a2 13259static bool
6f20ed8a 13260group_sections (struct bfd_link_info *info,
4ce794b7 13261 bfd_size_type stub_group_size,
0a1b45a2 13262 bool stubs_always_before_branch)
721956f4 13263{
6f20ed8a
AM
13264 struct ppc_link_hash_table *htab;
13265 asection *osec;
0a1b45a2 13266 bool suppress_size_errors;
7c8fe5c4 13267
6f20ed8a
AM
13268 htab = ppc_hash_table (info);
13269 if (htab == NULL)
0a1b45a2 13270 return false;
6f20ed8a 13271
0a1b45a2 13272 suppress_size_errors = false;
7c8fe5c4
AM
13273 if (stub_group_size == 1)
13274 {
13275 /* Default values. */
13276 if (stubs_always_before_branch)
09f92717 13277 stub_group_size = 0x1e00000;
7c8fe5c4 13278 else
09f92717 13279 stub_group_size = 0x1c00000;
0a1b45a2 13280 suppress_size_errors = true;
7c8fe5c4
AM
13281 }
13282
6f20ed8a 13283 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13284 {
6f20ed8a
AM
13285 asection *tail;
13286
13287 if (osec->id >= htab->sec_info_arr_size)
13288 continue;
13289
13290 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13291 while (tail != NULL)
721956f4 13292 {
734b6cf9
AM
13293 asection *curr;
13294 asection *prev;
13295 bfd_size_type total;
0a1b45a2 13296 bool big_sec;
734b6cf9 13297 bfd_vma curr_toc;
6f20ed8a 13298 struct map_stub *group;
09f92717 13299 bfd_size_type group_size;
734b6cf9
AM
13300
13301 curr = tail;
eea6121a 13302 total = tail->size;
09f92717
AM
13303 group_size = (ppc64_elf_section_data (tail) != NULL
13304 && ppc64_elf_section_data (tail)->has_14bit_branch
13305 ? stub_group_size >> 10 : stub_group_size);
13306
13307 big_sec = total > group_size;
7c8fe5c4 13308 if (big_sec && !suppress_size_errors)
695344c0 13309 /* xgettext:c-format */
871b3ab2 13310 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13311 tail->owner, tail);
6f20ed8a 13312 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13313
6f20ed8a 13314 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13315 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13316 < (ppc64_elf_section_data (prev) != NULL
13317 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13318 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13319 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13320 curr = prev;
13321
13322 /* OK, the size from the start of CURR to the end is less
09f92717 13323 than group_size and thus can be handled by one stub
734b6cf9 13324 section. (or the tail section is itself larger than
09f92717
AM
13325 group_size, in which case we may be toast.) We should
13326 really be keeping track of the total size of stubs added
13327 here, as stubs contribute to the final output section
13328 size. That's a little tricky, and this way will only
13329 break if stubs added make the total size more than 2^25,
13330 ie. for the default stub_group_size, if stubs total more
13331 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13332 group = bfd_alloc (curr->owner, sizeof (*group));
13333 if (group == NULL)
0a1b45a2 13334 return false;
6f20ed8a
AM
13335 group->link_sec = curr;
13336 group->stub_sec = NULL;
a4b6fadd 13337 group->needs_save_res = 0;
df136d64
AM
13338 group->lr_restore = 0;
13339 group->eh_size = 0;
13340 group->eh_base = 0;
a4b6fadd
AM
13341 group->next = htab->group;
13342 htab->group = group;
734b6cf9 13343 do
721956f4 13344 {
6f20ed8a 13345 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13346 /* Set up this stub group. */
6f20ed8a 13347 htab->sec_info[tail->id].u.group = group;
721956f4 13348 }
734b6cf9
AM
13349 while (tail != curr && (tail = prev) != NULL);
13350
09f92717 13351 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13352 bytes before the stub section can be handled by it too.
13353 Don't do this if we have a really large section after the
13354 stubs, as adding more stubs increases the chance that
13355 branches may not reach into the stub section. */
13356 if (!stubs_always_before_branch && !big_sec)
13357 {
13358 total = 0;
13359 while (prev != NULL
13360 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13361 < (ppc64_elf_section_data (prev) != NULL
13362 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13363 ? (group_size = stub_group_size >> 10)
13364 : group_size))
6f20ed8a 13365 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13366 {
13367 tail = prev;
6f20ed8a
AM
13368 prev = htab->sec_info[tail->id].u.list;
13369 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13370 }
13371 }
13372 tail = prev;
721956f4
AM
13373 }
13374 }
0a1b45a2 13375 return true;
721956f4
AM
13376}
13377
58d180e8
AM
13378static const unsigned char glink_eh_frame_cie[] =
13379{
13380 0, 0, 0, 16, /* length. */
13381 0, 0, 0, 0, /* id. */
13382 1, /* CIE version. */
13383 'z', 'R', 0, /* Augmentation string. */
13384 4, /* Code alignment. */
13385 0x78, /* Data alignment. */
13386 65, /* RA reg. */
13387 1, /* Augmentation size. */
13388 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13389 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13390};
13391
d969d15f
AM
13392/* Stripping output sections is normally done before dynamic section
13393 symbols have been allocated. This function is called later, and
13394 handles cases like htab->brlt which is mapped to its own output
13395 section. */
13396
13397static void
13398maybe_strip_output (struct bfd_link_info *info, asection *isec)
13399{
13400 if (isec->size == 0
13401 && isec->output_section->size == 0
53d8967a 13402 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13403 && !bfd_section_removed_from_list (info->output_bfd,
13404 isec->output_section)
13405 && elf_section_data (isec->output_section)->dynindx == 0)
13406 {
13407 isec->output_section->flags |= SEC_EXCLUDE;
13408 bfd_section_list_remove (info->output_bfd, isec->output_section);
13409 info->output_bfd->section_count--;
13410 }
13411}
13412
1657026c
AM
13413static int
13414compare_relr_address (const void *arg1, const void *arg2)
13415{
13416 bfd_vma a = *(bfd_vma *) arg1;
13417 bfd_vma b = *(bfd_vma *) arg2;
13418 return a < b ? -1 : a > b ? 1 : 0;
13419}
13420
13421static bool
13422append_relr_off (struct ppc_link_hash_table *htab, bfd_vma off)
13423{
13424 if (htab->relr_count >= htab->relr_alloc)
13425 {
13426 if (htab->relr_alloc == 0)
13427 htab->relr_alloc = 4096;
13428 else
13429 htab->relr_alloc *= 2;
13430 htab->relr_addr
13431 = bfd_realloc (htab->relr_addr,
13432 htab->relr_alloc * sizeof (htab->relr_addr[0]));
13433 if (htab->relr_addr == NULL)
13434 return false;
13435 }
13436 htab->relr_addr[htab->relr_count++] = off;
13437 return true;
13438}
13439
13440static bool
13441got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13442{
13443 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13444 bfd *ibfd;
13445
13446 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13447 {
13448 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13449 struct plt_entry **local_plt, **lplt, **end_local_plt;
13450 Elf_Internal_Shdr *symtab_hdr;
13451 bfd_size_type locsymcount;
3a3a4c1f
AM
13452 Elf_Internal_Sym *local_syms;
13453 Elf_Internal_Sym *isym;
1657026c
AM
13454 struct plt_entry *pent;
13455 struct got_entry *gent;
13456
13457 if (!is_ppc64_elf (ibfd))
13458 continue;
13459
13460 lgot_ents = elf_local_got_ents (ibfd);
13461 if (!lgot_ents)
13462 continue;
13463
13464 symtab_hdr = &elf_symtab_hdr (ibfd);
13465 locsymcount = symtab_hdr->sh_info;
3a3a4c1f
AM
13466 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13467 if (local_syms == NULL && locsymcount != 0)
13468 {
13469 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13470 0, NULL, NULL, NULL);
13471 if (local_syms == NULL)
13472 return false;
13473 }
1657026c
AM
13474 end_lgot_ents = lgot_ents + locsymcount;
13475 local_plt = (struct plt_entry **) end_lgot_ents;
13476 end_local_plt = local_plt + locsymcount;
3a3a4c1f
AM
13477 for (lgot = lgot_ents, isym = local_syms;
13478 lgot < end_lgot_ents;
13479 ++lgot, ++isym)
1657026c
AM
13480 for (gent = *lgot; gent != NULL; gent = gent->next)
13481 if (!gent->is_indirect
13482 && gent->tls_type == 0
3a3a4c1f
AM
13483 && gent->got.offset != (bfd_vma) -1
13484 && isym->st_shndx != SHN_ABS)
1657026c
AM
13485 {
13486 asection *got = ppc64_elf_tdata (gent->owner)->got;
13487 bfd_vma r_offset = (got->output_section->vma
13488 + got->output_offset
13489 + gent->got.offset);
13490 if (!append_relr_off (htab, r_offset))
13491 {
13492 htab->stub_error = true;
13493 return false;
13494 }
13495 }
13496
13497 if (!htab->opd_abi)
3a3a4c1f
AM
13498 for (lplt = local_plt, isym = local_syms;
13499 lplt < end_local_plt;
13500 ++lplt, ++isym)
1657026c 13501 for (pent = *lplt; pent != NULL; pent = pent->next)
3a3a4c1f
AM
13502 if (pent->plt.offset != (bfd_vma) -1
13503 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
1657026c 13504 {
3a3a4c1f
AM
13505 bfd_vma r_offset = (pent->plt.offset
13506 + htab->pltlocal->output_offset
13507 + htab->pltlocal->output_section->vma);
13508 if (!append_relr_off (htab, r_offset))
1657026c 13509 {
1657026c
AM
13510 if (symtab_hdr->contents != (unsigned char *) local_syms)
13511 free (local_syms);
13512 return false;
13513 }
1657026c
AM
13514 }
13515
13516 if (local_syms != NULL
13517 && symtab_hdr->contents != (unsigned char *) local_syms)
13518 {
13519 if (!info->keep_memory)
13520 free (local_syms);
13521 else
13522 symtab_hdr->contents = (unsigned char *) local_syms;
13523 }
13524 }
13525 return true;
13526}
13527
13528static bool
13529got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13530{
13531 struct bfd_link_info *info;
13532 struct ppc_link_hash_table *htab;
13533 struct plt_entry *pent;
13534 struct got_entry *gent;
13535
13536 if (h->root.type == bfd_link_hash_indirect)
13537 return true;
13538
13539 info = (struct bfd_link_info *) inf;
13540 htab = ppc_hash_table (info);
13541 if (htab == NULL)
13542 return false;
13543
13544 if (h->type != STT_GNU_IFUNC
13545 && h->def_regular
13546 && (h->root.type == bfd_link_hash_defined
13547 || h->root.type == bfd_link_hash_defweak))
13548 {
3a3a4c1f
AM
13549 if ((!htab->elf.dynamic_sections_created
13550 || h->dynindx == -1
13551 || SYMBOL_REFERENCES_LOCAL (info, h))
13552 && !bfd_is_abs_symbol (&h->root))
1657026c
AM
13553 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13554 if (!gent->is_indirect
13555 && gent->tls_type == 0
13556 && gent->got.offset != (bfd_vma) -1)
13557 {
13558 asection *got = ppc64_elf_tdata (gent->owner)->got;
13559 bfd_vma r_offset = (got->output_section->vma
13560 + got->output_offset
13561 + gent->got.offset);
13562 if (!append_relr_off (htab, r_offset))
13563 {
13564 htab->stub_error = true;
13565 return false;
13566 }
13567 }
13568
13569 if (!htab->opd_abi
13570 && use_local_plt (info, h))
13571 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13572 if (pent->plt.offset != (bfd_vma) -1)
13573 {
13574 bfd_vma r_offset = (htab->pltlocal->output_section->vma
13575 + htab->pltlocal->output_offset
13576 + pent->plt.offset);
13577 if (!append_relr_off (htab, r_offset))
13578 {
13579 htab->stub_error = true;
13580 return false;
13581 }
13582 }
13583 }
13584 return true;
13585}
13586
721956f4
AM
13587/* Determine and set the size of the stub section for a final link.
13588
13589 The basic idea here is to examine all the relocations looking for
13590 PC-relative calls to a target that is unreachable with a "bl"
13591 instruction. */
13592
0a1b45a2 13593bool
e7d1c40c 13594ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13595{
13596 bfd_size_type stub_group_size;
0a1b45a2 13597 bool stubs_always_before_branch;
721956f4
AM
13598 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13599
4dfe6ac6 13600 if (htab == NULL)
0a1b45a2 13601 return false;
4dfe6ac6 13602
e10a07b3
AM
13603 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13604 htab->params->power10_stubs = 0;
13605
0e1862bb 13606 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13607 htab->params->plt_thread_safe = 1;
b9e5796b 13608 if (!htab->opd_abi)
e7d1c40c
AM
13609 htab->params->plt_thread_safe = 0;
13610 else if (htab->params->plt_thread_safe == -1)
794e51c0 13611 {
e2458743 13612 static const char *const thread_starter[] =
794e51c0
AM
13613 {
13614 "pthread_create",
13615 /* libstdc++ */
13616 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13617 /* librt */
13618 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13619 "mq_notify", "create_timer",
13620 /* libanl */
13621 "getaddrinfo_a",
13622 /* libgomp */
2300b5a1 13623 "GOMP_parallel",
794e51c0 13624 "GOMP_parallel_start",
2300b5a1 13625 "GOMP_parallel_loop_static",
794e51c0 13626 "GOMP_parallel_loop_static_start",
2300b5a1 13627 "GOMP_parallel_loop_dynamic",
794e51c0 13628 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13629 "GOMP_parallel_loop_guided",
794e51c0 13630 "GOMP_parallel_loop_guided_start",
2300b5a1 13631 "GOMP_parallel_loop_runtime",
794e51c0 13632 "GOMP_parallel_loop_runtime_start",
2300b5a1 13633 "GOMP_parallel_sections",
68ffbac6 13634 "GOMP_parallel_sections_start",
f9dffbf0
AM
13635 /* libgo */
13636 "__go_go",
794e51c0
AM
13637 };
13638 unsigned i;
13639
a4b6fadd 13640 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13641 {
13642 struct elf_link_hash_entry *h;
13643 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
0a1b45a2 13644 false, false, true);
e7d1c40c
AM
13645 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13646 if (htab->params->plt_thread_safe)
794e51c0
AM
13647 break;
13648 }
13649 }
e7d1c40c
AM
13650 stubs_always_before_branch = htab->params->group_size < 0;
13651 if (htab->params->group_size < 0)
13652 stub_group_size = -htab->params->group_size;
721956f4 13653 else
e7d1c40c 13654 stub_group_size = htab->params->group_size;
721956f4 13655
6f20ed8a 13656 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
0a1b45a2 13657 return false;
721956f4 13658
a804e476
AM
13659 htab->tga_group = NULL;
13660 if (!htab->params->no_tls_get_addr_regsave
13661 && htab->tga_desc_fd != NULL
13662 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13663 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13664 && htab->tls_get_addr_fd != NULL
13665 && is_static_defined (&htab->tls_get_addr_fd->elf))
13666 {
13667 asection *sym_sec, *code_sec, *stub_sec;
13668 bfd_vma sym_value;
13669 struct _opd_sec_data *opd;
13670
13671 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13672 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13673 code_sec = sym_sec;
13674 opd = get_opd_info (sym_sec);
13675 if (opd != NULL)
0a1b45a2 13676 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
a804e476
AM
13677 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13678 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13679 htab->tga_group->link_sec);
13680 if (stub_sec == NULL)
0a1b45a2 13681 return false;
a804e476
AM
13682 htab->tga_group->stub_sec = stub_sec;
13683
13684 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13685 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13686 htab->tga_desc_fd->elf.root.u.def.value = 0;
13687 htab->tga_desc_fd->elf.type = STT_FUNC;
13688 htab->tga_desc_fd->elf.def_regular = 1;
13689 htab->tga_desc_fd->elf.non_elf = 0;
0a1b45a2 13690 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
a804e476
AM
13691 }
13692
c9301e31 13693 /* Loop until no stubs added. After iteration 20 of this loop we may
0441f94f 13694 exit on a stub section shrinking. */
c9301e31 13695
721956f4
AM
13696 while (1)
13697 {
13698 bfd *input_bfd;
13699 unsigned int bfd_indx;
a4b6fadd 13700 struct map_stub *group;
721956f4
AM
13701
13702 htab->stub_iteration += 1;
1657026c 13703 htab->relr_count = 0;
721956f4
AM
13704
13705 for (input_bfd = info->input_bfds, bfd_indx = 0;
13706 input_bfd != NULL;
c72f2fb2 13707 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13708 {
13709 Elf_Internal_Shdr *symtab_hdr;
13710 asection *section;
6cdc0ccc 13711 Elf_Internal_Sym *local_syms = NULL;
721956f4 13712
0c8d6e5c 13713 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13714 continue;
13715
721956f4 13716 /* We'll need the symbol table in a second. */
0ffa91dd 13717 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13718 if (symtab_hdr->sh_info == 0)
13719 continue;
13720
721956f4
AM
13721 /* Walk over each section attached to the input bfd. */
13722 for (section = input_bfd->sections;
13723 section != NULL;
13724 section = section->next)
13725 {
721956f4 13726 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1657026c 13727 bool is_opd;
721956f4
AM
13728
13729 /* If there aren't any relocs, then there's nothing more
13730 to do. */
13731 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13732 || (section->flags & SEC_ALLOC) == 0
13733 || (section->flags & SEC_LOAD) == 0
721956f4
AM
13734 || section->reloc_count == 0)
13735 continue;
13736
1657026c
AM
13737 if (!info->enable_dt_relr
13738 && (section->flags & SEC_CODE) == 0)
13739 continue;
13740
721956f4
AM
13741 /* If this section is a link-once section that will be
13742 discarded, then don't create any stubs. */
13743 if (section->output_section == NULL
927be08e 13744 || section->output_section->owner != info->output_bfd)
721956f4
AM
13745 continue;
13746
1e2f5b6e
AM
13747 /* Get the relocs. */
13748 internal_relocs
4ce794b7 13749 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13750 info->keep_memory);
721956f4 13751 if (internal_relocs == NULL)
1e2f5b6e 13752 goto error_ret_free_local;
721956f4 13753
1657026c
AM
13754 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13755
721956f4
AM
13756 /* Now examine each relocation. */
13757 irela = internal_relocs;
13758 irelaend = irela + section->reloc_count;
13759 for (; irela < irelaend; irela++)
13760 {
4ce794b7
AM
13761 enum elf_ppc64_reloc_type r_type;
13762 unsigned int r_indx;
7aba54da 13763 struct ppc_stub_type stub_type;
721956f4 13764 struct ppc_stub_hash_entry *stub_entry;
8387904d 13765 asection *sym_sec, *code_sec;
e054468f 13766 bfd_vma sym_value, code_value;
721956f4 13767 bfd_vma destination;
6911b7dc 13768 unsigned long local_off;
0a1b45a2 13769 bool ok_dest;
721956f4 13770 struct ppc_link_hash_entry *hash;
8387904d 13771 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13772 struct elf_link_hash_entry *h;
13773 Elf_Internal_Sym *sym;
721956f4
AM
13774 char *stub_name;
13775 const asection *id_sec;
74f0fb50 13776 struct _opd_sec_data *opd;
e054468f 13777 struct plt_entry *plt_ent;
721956f4
AM
13778
13779 r_type = ELF64_R_TYPE (irela->r_info);
13780 r_indx = ELF64_R_SYM (irela->r_info);
13781
4ce794b7 13782 if (r_type >= R_PPC64_max)
721956f4
AM
13783 {
13784 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13785 goto error_ret_free_internal;
721956f4
AM
13786 }
13787
13788 /* Only look for stubs on branch instructions. */
1657026c
AM
13789 switch (r_type)
13790 {
13791 default:
13792 continue;
13793
13794 case R_PPC64_REL24:
13795 case R_PPC64_REL24_NOTOC:
13796 case R_PPC64_REL24_P9NOTOC:
13797 case R_PPC64_REL14:
13798 case R_PPC64_REL14_BRTAKEN:
13799 case R_PPC64_REL14_BRNTAKEN:
13800 if ((section->flags & SEC_CODE) != 0)
13801 break;
13802 continue;
13803
13804 case R_PPC64_ADDR64:
13805 case R_PPC64_TOC:
13806 if (info->enable_dt_relr
13807 && irela->r_offset % 2 == 0
13808 && section->alignment_power != 0)
13809 break;
13810 continue;
13811 }
721956f4
AM
13812
13813 /* Now determine the call target, its name, value,
13814 section. */
411e1bfb
AM
13815 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13816 r_indx, input_bfd))
13817 goto error_ret_free_internal;
411e1bfb 13818
1657026c
AM
13819 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13820 {
13821 /* Only locally defined symbols can possibly use
13822 relative relocations. */
13823 bfd_vma r_offset;
13824 if ((sym_sec == NULL
13825 || sym_sec->output_section == NULL)
13826 /* No symbol is OK too. */
13827 && !(sym != NULL && sym->st_shndx == 0)
13828 /* Hack for __ehdr_start, which is undefined
13829 at this point. */
13830 && !(h != NULL && h->root.linker_def))
13831 continue;
13832 if (NO_OPD_RELOCS && is_opd)
13833 continue;
13834 if (!is_opd
13835 && r_type == R_PPC64_ADDR64)
13836 {
13837 if (h != NULL
13838 ? h->type == STT_GNU_IFUNC
13839 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13840 continue;
3a3a4c1f
AM
13841 if (h != NULL
13842 ? bfd_is_abs_symbol (&h->root)
13843 : sym->st_shndx == SHN_ABS)
13844 continue;
1657026c
AM
13845 if (h != NULL
13846 && !SYMBOL_REFERENCES_LOCAL (info, h))
13847 continue;
13848 }
13849 r_offset = _bfd_elf_section_offset (info->output_bfd,
13850 info,
13851 section,
13852 irela->r_offset);
13853 if (r_offset >= (bfd_vma) -2)
13854 continue;
13855 r_offset += (section->output_section->vma
13856 + section->output_offset);
13857 if (!append_relr_off (htab, r_offset))
13858 goto error_ret_free_internal;
13859 continue;
13860 }
13861
13862 hash = ppc_elf_hash_entry (h);
0a1b45a2 13863 ok_dest = false;
8387904d 13864 fdh = NULL;
7fe2b9a6 13865 sym_value = 0;
411e1bfb 13866 if (hash == NULL)
721956f4 13867 {
411e1bfb 13868 sym_value = sym->st_value;
c27b8c2a
AM
13869 if (sym_sec != NULL
13870 && sym_sec->output_section != NULL)
0a1b45a2 13871 ok_dest = true;
721956f4 13872 }
7fe2b9a6
AM
13873 else if (hash->elf.root.type == bfd_link_hash_defined
13874 || hash->elf.root.type == bfd_link_hash_defweak)
13875 {
13876 sym_value = hash->elf.root.u.def.value;
13877 if (sym_sec->output_section != NULL)
0a1b45a2 13878 ok_dest = true;
7fe2b9a6
AM
13879 }
13880 else if (hash->elf.root.type == bfd_link_hash_undefweak
13881 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13882 {
99877b66 13883 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13884 use the func descriptor sym instead if it is
13885 defined. */
ceb1f1ef 13886 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13887 && hash->oh != NULL)
8387904d 13888 {
8c5b4e52 13889 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13890 if (fdh->elf.root.type == bfd_link_hash_defined
13891 || fdh->elf.root.type == bfd_link_hash_defweak)
13892 {
13893 sym_sec = fdh->elf.root.u.def.section;
13894 sym_value = fdh->elf.root.u.def.value;
13895 if (sym_sec->output_section != NULL)
0a1b45a2 13896 ok_dest = true;
8387904d 13897 }
99877b66
AM
13898 else
13899 fdh = NULL;
8387904d 13900 }
7fe2b9a6
AM
13901 }
13902 else
13903 {
13904 bfd_set_error (bfd_error_bad_value);
13905 goto error_ret_free_internal;
721956f4
AM
13906 }
13907
8843416a 13908 destination = 0;
6911b7dc 13909 local_off = 0;
8843416a
AM
13910 if (ok_dest)
13911 {
13912 sym_value += irela->r_addend;
13913 destination = (sym_value
13914 + sym_sec->output_offset
13915 + sym_sec->output_section->vma);
6911b7dc
AM
13916 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13917 ? hash->elf.other
13918 : sym->st_other);
8843416a
AM
13919 }
13920
8387904d 13921 code_sec = sym_sec;
e054468f 13922 code_value = sym_value;
74f0fb50
AM
13923 opd = get_opd_info (sym_sec);
13924 if (opd != NULL)
8387904d
AM
13925 {
13926 bfd_vma dest;
13927
74f0fb50 13928 if (hash == NULL && opd->adjust != NULL)
8387904d 13929 {
51aecdc5 13930 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13931 if (adjust == -1)
13932 continue;
e054468f 13933 code_value += adjust;
8387904d
AM
13934 sym_value += adjust;
13935 }
13936 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13937 &code_sec, &code_value, false);
8387904d
AM
13938 if (dest != (bfd_vma) -1)
13939 {
13940 destination = dest;
13941 if (fdh != NULL)
13942 {
13943 /* Fixup old ABI sym to point at code
13944 entry. */
99877b66 13945 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13946 hash->elf.root.u.def.section = code_sec;
e054468f 13947 hash->elf.root.u.def.value = code_value;
8387904d
AM
13948 }
13949 }
13950 }
13951
721956f4 13952 /* Determine what (if any) linker stub is needed. */
e054468f 13953 plt_ent = NULL;
7aba54da
AM
13954 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13955 &plt_ent, destination,
13956 local_off);
13957 stub_type.sub = ppc_stub_toc;
13958 stub_type.r2save = 0;
13959
13960 if (r_type == R_PPC64_REL24_NOTOC
13961 || r_type == R_PPC64_REL24_P9NOTOC)
05d0e962 13962 {
7aba54da
AM
13963 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13964 if (htab->params->power10_stubs == 0
13965 || (r_type == R_PPC64_REL24_P9NOTOC
13966 && htab->params->power10_stubs != 1))
13967 notoc = ppc_stub_p9notoc;
13968 if (stub_type.main == ppc_stub_plt_call)
13969 stub_type.sub = notoc;
13970 else if (stub_type.main == ppc_stub_long_branch
05d0e962
AM
13971 || (code_sec != NULL
13972 && code_sec->output_section != NULL
13973 && (((hash ? hash->elf.other : sym->st_other)
13974 & STO_PPC64_LOCAL_MASK)
4a4e7361 13975 > 1 << STO_PPC64_LOCAL_BIT)))
7aba54da
AM
13976 {
13977 stub_type.main = ppc_stub_long_branch;
13978 stub_type.sub = notoc;
13979 stub_type.r2save = 0;
13980 }
05d0e962 13981 }
7aba54da 13982 else if (stub_type.main != ppc_stub_plt_call)
ad8e1ba5
AM
13983 {
13984 /* Check whether we need a TOC adjusting stub.
13985 Since the linker pastes together pieces from
13986 different object files when creating the
13987 _init and _fini functions, it may be that a
13988 call to what looks like a local sym is in
13989 fact a call needing a TOC adjustment. */
33cb30a1
AM
13990 if ((code_sec != NULL
13991 && code_sec->output_section != NULL
33cb30a1 13992 && (code_sec->has_toc_reloc
918dc783
AM
13993 || code_sec->makes_toc_func_call)
13994 && (htab->sec_info[code_sec->id].toc_off
13995 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13996 || (((hash ? hash->elf.other : sym->st_other)
13997 & STO_PPC64_LOCAL_MASK)
13998 == 1 << STO_PPC64_LOCAL_BIT))
7aba54da
AM
13999 {
14000 stub_type.main = ppc_stub_long_branch;
14001 stub_type.sub = ppc_stub_toc;
14002 stub_type.r2save = 1;
14003 }
ad8e1ba5
AM
14004 }
14005
7aba54da 14006 if (stub_type.main == ppc_stub_none)
721956f4
AM
14007 continue;
14008
411e1bfb 14009 /* __tls_get_addr calls might be eliminated. */
7aba54da 14010 if (stub_type.main != ppc_stub_plt_call
411e1bfb 14011 && hash != NULL
ed7007c1 14012 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
14013 && section->has_tls_reloc
14014 && irela != internal_relocs)
14015 {
14016 /* Get tls info. */
f961d9dd 14017 unsigned char *tls_mask;
411e1bfb 14018
3a71aa26 14019 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
14020 irela - 1, input_bfd))
14021 goto error_ret_free_internal;
abc489c6
AM
14022 if ((*tls_mask & TLS_TLS) != 0
14023 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
14024 continue;
14025 }
14026
7aba54da
AM
14027 if (stub_type.main == ppc_stub_plt_call
14028 && stub_type.sub == ppc_stub_toc)
794e51c0 14029 {
6e1816be
AM
14030 if (!htab->opd_abi
14031 && htab->params->plt_localentry0 != 0
14032 && is_elfv2_localentry0 (&hash->elf))
14033 htab->has_plt_localentry0 = 1;
14034 else if (irela + 1 < irelaend
14035 && irela[1].r_offset == irela->r_offset + 4
14036 && (ELF64_R_TYPE (irela[1].r_info)
14037 == R_PPC64_TOCSAVE))
f378ab09
AM
14038 {
14039 if (!tocsave_find (htab, INSERT,
14040 &local_syms, irela + 1, input_bfd))
14041 goto error_ret_free_internal;
14042 }
f378ab09 14043 else
7aba54da 14044 stub_type.r2save = 1;
794e51c0 14045 }
3b421ab3 14046
721956f4 14047 /* Support for grouping stub sections. */
6f20ed8a 14048 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
14049
14050 /* Get the name of this stub. */
14051 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14052 if (!stub_name)
14053 goto error_ret_free_internal;
14054
14055 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 14056 stub_name, false, false);
721956f4
AM
14057 if (stub_entry != NULL)
14058 {
721956f4 14059 free (stub_name);
7aba54da 14060 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
e10a07b3 14061 {
7aba54da
AM
14062 /* xgettext:c-format */
14063 _bfd_error_handler
14064 (_("%pB: cannot create stub entry %s"),
14065 section->owner, stub_entry->root.string);
14066 goto error_ret_free_internal;
e10a07b3 14067 }
721956f4
AM
14068 continue;
14069 }
14070
25f53a85 14071 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
14072 if (stub_entry == NULL)
14073 {
14074 free (stub_name);
6cdc0ccc
AM
14075 error_ret_free_internal:
14076 if (elf_section_data (section)->relocs == NULL)
14077 free (internal_relocs);
14078 error_ret_free_local:
c9594989
AM
14079 if (symtab_hdr->contents
14080 != (unsigned char *) local_syms)
6cdc0ccc 14081 free (local_syms);
0a1b45a2 14082 return false;
721956f4
AM
14083 }
14084
7aba54da
AM
14085 stub_entry->type = stub_type;
14086 if (stub_type.main == ppc_stub_plt_call)
e054468f 14087 {
05d0e962
AM
14088 stub_entry->target_value = sym_value;
14089 stub_entry->target_section = sym_sec;
e054468f
AM
14090 }
14091 else
14092 {
05d0e962
AM
14093 stub_entry->target_value = code_value;
14094 stub_entry->target_section = code_sec;
e054468f 14095 }
721956f4 14096 stub_entry->h = hash;
e054468f 14097 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
14098 stub_entry->symtype
14099 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 14100 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 14101
3d58e1fc
AM
14102 if (hash != NULL
14103 && (hash->elf.root.type == bfd_link_hash_defined
14104 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 14105 htab->stub_globals += 1;
721956f4
AM
14106 }
14107
14108 /* We're done with the internal relocs, free them. */
6cdc0ccc 14109 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 14110 free (internal_relocs);
721956f4 14111 }
6cdc0ccc
AM
14112
14113 if (local_syms != NULL
14114 && symtab_hdr->contents != (unsigned char *) local_syms)
14115 {
14116 if (!info->keep_memory)
14117 free (local_syms);
14118 else
14119 symtab_hdr->contents = (unsigned char *) local_syms;
14120 }
721956f4
AM
14121 }
14122
5c3dead3 14123 /* We may have added some stubs. Find out the new size of the
721956f4 14124 stub sections. */
d4aaa2a0 14125 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14126 {
14127 group->lr_restore = 0;
14128 group->eh_size = 0;
14129 if (group->stub_sec != NULL)
14130 {
14131 asection *stub_sec = group->stub_sec;
14132
9ff8aa7d 14133 stub_sec->rawsize = stub_sec->size;
df136d64
AM
14134 stub_sec->size = 0;
14135 stub_sec->reloc_count = 0;
14136 stub_sec->flags &= ~SEC_RELOC;
14137 }
14138 }
a804e476
AM
14139 if (htab->tga_group != NULL)
14140 {
14141 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14142 htab->tga_group->eh_size
14143 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14144 htab->tga_group->lr_restore = 23 * 4;
14145 htab->tga_group->stub_sec->size = 24 * 4;
14146 }
eea6121a 14147
9ff8aa7d 14148 htab->brlt->rawsize = htab->brlt->size;
eea6121a 14149 htab->brlt->size = 0;
84f5d08e
AM
14150 htab->brlt->reloc_count = 0;
14151 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 14152 if (htab->relbrlt != NULL)
eea6121a 14153 htab->relbrlt->size = 0;
721956f4 14154
1657026c
AM
14155 if (htab->elf.srelrdyn != NULL)
14156 {
9ff8aa7d 14157 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
1657026c
AM
14158 htab->elf.srelrdyn->size = 0;
14159 }
14160
0441f94f 14161 htab->stub_changed = false;
63bc6f6c 14162 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 14163
a4b6fadd
AM
14164 for (group = htab->group; group != NULL; group = group->next)
14165 if (group->needs_save_res)
14166 group->stub_sec->size += htab->sfpr->size;
14167
176a0d42
AM
14168 if (info->emitrelocations
14169 && htab->glink != NULL && htab->glink->size != 0)
14170 {
14171 htab->glink->reloc_count = 1;
14172 htab->glink->flags |= SEC_RELOC;
14173 }
14174
58d180e8
AM
14175 if (htab->glink_eh_frame != NULL
14176 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 14177 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 14178 {
2e0ce1c8 14179 size_t size = 0, align = 4;
58d180e8 14180
d4aaa2a0 14181 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14182 if (group->eh_size != 0)
14183 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 14184 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 14185 size += (24 + align - 1) & -align;
58d180e8 14186 if (size != 0)
2e0ce1c8
AM
14187 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14188 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14189 size = (size + align - 1) & -align;
58d180e8
AM
14190 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14191 htab->glink_eh_frame->size = size;
14192 }
14193
e7d1c40c 14194 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14195 for (group = htab->group; group != NULL; group = group->next)
14196 if (group->stub_sec != NULL)
691d2e9a
AM
14197 {
14198 int align = abs (htab->params->plt_stub_align);
14199 group->stub_sec->size
14200 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14201 }
d4aaa2a0 14202
1657026c
AM
14203 if (htab->elf.srelrdyn != NULL)
14204 {
14205 bfd_vma r_offset;
14206
14207 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14208 if (!append_relr_off (htab, (r_offset
14209 + htab->brlt->output_section->vma
14210 + htab->brlt->output_offset)))
14211 return false;
14212
14213 if (!got_and_plt_relr_for_local_syms (info))
14214 return false;
14215 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14216 if (htab->stub_error)
14217 return false;
14218
14219 if (htab->relr_count > 1)
14220 qsort (htab->relr_addr, htab->relr_count, sizeof (*htab->relr_addr),
14221 compare_relr_address);
14222
14223 size_t i = 0;
14224 while (i < htab->relr_count)
14225 {
14226 bfd_vma base = htab->relr_addr[i];
14227 htab->elf.srelrdyn->size += 8;
14228 i++;
14229 /* Handle possible duplicate address. This can happen
14230 as sections increase in size when adding stubs. */
14231 while (i < htab->relr_count
14232 && htab->relr_addr[i] == base)
14233 i++;
14234 base += 8;
14235 while (1)
14236 {
14237 size_t start_i = i;
14238 while (i < htab->relr_count
14239 && htab->relr_addr[i] - base < 63 * 8
14240 && (htab->relr_addr[i] - base) % 8 == 0)
14241 i++;
14242 if (i == start_i)
14243 break;
14244 htab->elf.srelrdyn->size += 8;
14245 base += 63 * 8;
14246 }
14247 }
14248 }
14249
d4aaa2a0
AM
14250 for (group = htab->group; group != NULL; group = group->next)
14251 if (group->stub_sec != NULL
14252 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 14253 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 14254 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
14255 break;
14256
d4aaa2a0 14257 if (group == NULL
0441f94f
AM
14258 && (!htab->stub_changed
14259 || htab->stub_iteration > STUB_SHRINK_ITER)
ba21f564
AM
14260 && (htab->brlt->rawsize == htab->brlt->size
14261 || (htab->stub_iteration > STUB_SHRINK_ITER
14262 && htab->brlt->rawsize > htab->brlt->size))
1657026c
AM
14263 && (htab->elf.srelrdyn == NULL
14264 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14265 || (htab->stub_iteration > STUB_SHRINK_ITER
14266 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
58d180e8 14267 && (htab->glink_eh_frame == NULL
a804e476
AM
14268 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14269 && (htab->tga_group == NULL
14270 || htab->stub_iteration > 1))
5c3dead3
AM
14271 break;
14272
9ff8aa7d
AM
14273 if (htab->stub_iteration > STUB_SHRINK_ITER)
14274 {
14275 for (group = htab->group; group != NULL; group = group->next)
14276 if (group->stub_sec != NULL
14277 && group->stub_sec->size < group->stub_sec->rawsize)
14278 group->stub_sec->size = group->stub_sec->rawsize;
14279
14280 if (htab->brlt->size < htab->brlt->rawsize)
14281 htab->brlt->size = htab->brlt->rawsize;
14282
14283 if (htab->elf.srelrdyn != NULL
14284 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14285 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14286 }
14287
721956f4 14288 /* Ask the linker to do its stuff. */
e7d1c40c 14289 (*htab->params->layout_sections_again) ();
721956f4
AM
14290 }
14291
da44f4e5
AM
14292 if (htab->glink_eh_frame != NULL
14293 && htab->glink_eh_frame->size != 0)
14294 {
14295 bfd_vma val;
14296 bfd_byte *p, *last_fde;
14297 size_t last_fde_len, size, align, pad;
d4aaa2a0 14298 struct map_stub *group;
da44f4e5 14299
df136d64
AM
14300 /* It is necessary to at least have a rough outline of the
14301 linker generated CIEs and FDEs written before
14302 bfd_elf_discard_info is run, in order for these FDEs to be
14303 indexed in .eh_frame_hdr. */
da44f4e5
AM
14304 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14305 if (p == NULL)
0a1b45a2 14306 return false;
da44f4e5
AM
14307 htab->glink_eh_frame->contents = p;
14308 last_fde = p;
2e0ce1c8 14309 align = 4;
da44f4e5
AM
14310
14311 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14312 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 14313 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 14314 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 14315 p += last_fde_len + 4;
da44f4e5 14316
d4aaa2a0 14317 for (group = htab->group; group != NULL; group = group->next)
df136d64 14318 if (group->eh_size != 0)
da44f4e5 14319 {
df136d64 14320 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 14321 last_fde = p;
df136d64 14322 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 14323 /* FDE length. */
2e0ce1c8 14324 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14325 p += 4;
14326 /* CIE pointer. */
14327 val = p - htab->glink_eh_frame->contents;
14328 bfd_put_32 (htab->elf.dynobj, val, p);
14329 p += 4;
14330 /* Offset to stub section, written later. */
14331 p += 4;
14332 /* stub section size. */
d4aaa2a0 14333 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
14334 p += 4;
14335 /* Augmentation. */
14336 p += 1;
df136d64
AM
14337 /* Make sure we don't have all nops. This is enough for
14338 elf-eh-frame.c to detect the last non-nop opcode. */
14339 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 14340 p = last_fde + last_fde_len + 4;
da44f4e5
AM
14341 }
14342 if (htab->glink != NULL && htab->glink->size != 0)
14343 {
14344 last_fde = p;
2e0ce1c8 14345 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 14346 /* FDE length. */
2e0ce1c8 14347 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14348 p += 4;
14349 /* CIE pointer. */
14350 val = p - htab->glink_eh_frame->contents;
14351 bfd_put_32 (htab->elf.dynobj, val, p);
14352 p += 4;
14353 /* Offset to .glink, written later. */
14354 p += 4;
14355 /* .glink size. */
14356 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14357 p += 4;
14358 /* Augmentation. */
14359 p += 1;
14360
3cd7c7d7 14361 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
14362 *p++ = DW_CFA_register;
14363 *p++ = 65;
9f08fa5c 14364 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 14365 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
14366 *p++ = DW_CFA_restore_extended;
14367 *p++ = 65;
2e0ce1c8 14368 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
14369 }
14370 /* Subsume any padding into the last FDE if user .eh_frame
14371 sections are aligned more than glink_eh_frame. Otherwise any
14372 zero padding will be seen as a terminator. */
2e0ce1c8 14373 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 14374 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 14375 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
14376 htab->glink_eh_frame->size = size + pad;
14377 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14378 }
14379
d969d15f 14380 maybe_strip_output (info, htab->brlt);
2efec98b
AM
14381 if (htab->relbrlt != NULL)
14382 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
14383 if (htab->glink_eh_frame != NULL)
14384 maybe_strip_output (info, htab->glink_eh_frame);
1657026c
AM
14385 if (htab->elf.srelrdyn != NULL)
14386 maybe_strip_output (info, htab->elf.srelrdyn);
721956f4 14387
0a1b45a2 14388 return true;
721956f4
AM
14389}
14390
14391/* Called after we have determined section placement. If sections
805fc799 14392 move, we'll be called again. Provide a value for TOCstart. */
721956f4 14393
805fc799 14394bfd_vma
1c865ab2 14395ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 14396{
805fc799 14397 asection *s;
a27e685f 14398 bfd_vma TOCstart, adjust;
721956f4 14399
43417696
AM
14400 if (info != NULL)
14401 {
14402 struct elf_link_hash_entry *h;
14403 struct elf_link_hash_table *htab = elf_hash_table (info);
14404
2cc15b10 14405 if (is_elf_hash_table (&htab->root)
43417696
AM
14406 && htab->hgot != NULL)
14407 h = htab->hgot;
14408 else
14409 {
2cc15b10
AM
14410 h = (struct elf_link_hash_entry *)
14411 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14412 if (is_elf_hash_table (&htab->root))
43417696
AM
14413 htab->hgot = h;
14414 }
14415 if (h != NULL
14416 && h->root.type == bfd_link_hash_defined
14417 && !h->root.linker_def
2cc15b10 14418 && (!is_elf_hash_table (&htab->root)
43417696
AM
14419 || h->def_regular))
14420 {
ed7007c1 14421 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
14422 _bfd_set_gp_value (obfd, TOCstart);
14423 return TOCstart;
14424 }
14425 }
14426
805fc799
AM
14427 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14428 order. The TOC starts where the first of these sections starts. */
14429 s = bfd_get_section_by_name (obfd, ".got");
e054468f 14430 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14431 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 14432 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14433 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 14434 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14435 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 14436 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
14437 {
14438 /* This may happen for
14439 o references to TOC base (SYM@toc / TOC[tc0]) without a
14440 .toc directive
14441 o bad linker script
14442 o --gc-sections and empty TOC sections
14443
14444 FIXME: Warn user? */
14445
14446 /* Look for a likely section. We probably won't even be
14447 using TOCstart. */
14448 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14449 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14450 | SEC_EXCLUDE))
805fc799
AM
14451 == (SEC_ALLOC | SEC_SMALL_DATA))
14452 break;
721956f4 14453 if (s == NULL)
805fc799 14454 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14455 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14456 == (SEC_ALLOC | SEC_SMALL_DATA))
14457 break;
721956f4 14458 if (s == NULL)
805fc799 14459 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14460 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14461 == SEC_ALLOC)
805fc799 14462 break;
721956f4 14463 if (s == NULL)
805fc799 14464 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14465 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14466 break;
14467 }
721956f4 14468
805fc799
AM
14469 TOCstart = 0;
14470 if (s != NULL)
14471 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14472
a27e685f
AM
14473 /* Force alignment. */
14474 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14475 TOCstart -= adjust;
1c865ab2
AM
14476 _bfd_set_gp_value (obfd, TOCstart);
14477
810d4e75 14478 if (info != NULL && s != NULL)
1c865ab2
AM
14479 {
14480 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14481
810d4e75
AM
14482 if (htab != NULL)
14483 {
14484 if (htab->elf.hgot != NULL)
14485 {
a27e685f 14486 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14487 htab->elf.hgot->root.u.def.section = s;
14488 }
14489 }
14490 else
1c865ab2 14491 {
810d4e75
AM
14492 struct bfd_link_hash_entry *bh = NULL;
14493 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f 14494 s, TOC_BASE_OFF - adjust,
0a1b45a2 14495 NULL, false, false, &bh);
1c865ab2
AM
14496 }
14497 }
805fc799 14498 return TOCstart;
721956f4
AM
14499}
14500
a345bc8d 14501/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14502 write out any global entry stubs, and PLT relocations. */
a345bc8d 14503
0a1b45a2 14504static bool
49c09209 14505build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14506{
14507 struct bfd_link_info *info;
14508 struct ppc_link_hash_table *htab;
49c09209 14509 struct plt_entry *ent;
a345bc8d
AM
14510 asection *s;
14511
14512 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 14513 return true;
a345bc8d 14514
49c09209
AM
14515 info = inf;
14516 htab = ppc_hash_table (info);
14517 if (htab == NULL)
0a1b45a2 14518 return false;
49c09209
AM
14519
14520 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14521 if (ent->plt.offset != (bfd_vma) -1)
14522 {
14523 /* This symbol has an entry in the procedure linkage
14524 table. Set it up. */
14525 Elf_Internal_Rela rela;
2d7ad24e 14526 asection *plt, *relplt;
49c09209
AM
14527 bfd_byte *loc;
14528
30845f11 14529 if (use_local_plt (info, h))
49c09209
AM
14530 {
14531 if (!(h->def_regular
14532 && (h->root.type == bfd_link_hash_defined
14533 || h->root.type == bfd_link_hash_defweak)))
14534 continue;
2d7ad24e
AM
14535 if (h->type == STT_GNU_IFUNC)
14536 {
14537 plt = htab->elf.iplt;
14538 relplt = htab->elf.irelplt;
0a1b45a2 14539 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14540 if (htab->opd_abi)
14541 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14542 else
14543 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14544 }
49c09209 14545 else
2d7ad24e
AM
14546 {
14547 plt = htab->pltlocal;
1657026c
AM
14548 relplt = NULL;
14549 if (bfd_link_pic (info)
14550 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
14551 {
14552 relplt = htab->relpltlocal;
14553 if (htab->opd_abi)
14554 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14555 else
14556 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14557 }
2d7ad24e 14558 }
ed7007c1 14559 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14560
14561 if (relplt == NULL)
14562 {
14563 loc = plt->contents + ent->plt.offset;
14564 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14565 if (htab->opd_abi)
14566 {
14567 bfd_vma toc = elf_gp (info->output_bfd);
14568 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14569 bfd_put_64 (info->output_bfd, toc, loc + 8);
14570 }
14571 }
14572 else
14573 {
14574 rela.r_offset = (plt->output_section->vma
14575 + plt->output_offset
14576 + ent->plt.offset);
14577 loc = relplt->contents + (relplt->reloc_count++
14578 * sizeof (Elf64_External_Rela));
14579 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14580 }
49c09209
AM
14581 }
14582 else
14583 {
14584 rela.r_offset = (htab->elf.splt->output_section->vma
14585 + htab->elf.splt->output_offset
14586 + ent->plt.offset);
14587 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14588 rela.r_addend = ent->addend;
14589 loc = (htab->elf.srelplt->contents
14590 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14591 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14592 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
0a1b45a2 14593 htab->elf.ifunc_resolvers = true;
2d7ad24e 14594 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14595 }
49c09209
AM
14596 }
14597
a345bc8d 14598 if (!h->pointer_equality_needed)
0a1b45a2 14599 return true;
a345bc8d
AM
14600
14601 if (h->def_regular)
0a1b45a2 14602 return true;
a345bc8d 14603
9e390558 14604 s = htab->global_entry;
49c09209 14605 if (s == NULL || s->size == 0)
0a1b45a2 14606 return true;
49c09209
AM
14607
14608 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14609 if (ent->plt.offset != (bfd_vma) -1
14610 && ent->addend == 0)
a345bc8d
AM
14611 {
14612 bfd_byte *p;
14613 asection *plt;
14614 bfd_vma off;
14615
a345bc8d 14616 p = s->contents + h->root.u.def.value;
33e44f2e 14617 plt = htab->elf.splt;
30845f11 14618 if (use_local_plt (info, h))
2d7ad24e
AM
14619 {
14620 if (h->type == STT_GNU_IFUNC)
14621 plt = htab->elf.iplt;
14622 else
14623 plt = htab->pltlocal;
14624 }
49c09209 14625 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14626 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14627
14628 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14629 {
14630 info->callbacks->einfo
c1c8c1ef 14631 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14632 h->root.root.string);
14633 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14634 htab->stub_error = true;
a345bc8d
AM
14635 }
14636
7341d5e2
AM
14637 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14638 if (htab->params->emit_stub_syms)
14639 {
14640 size_t len = strlen (h->root.root.string);
14641 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14642
14643 if (name == NULL)
0a1b45a2 14644 return false;
7341d5e2
AM
14645
14646 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
0a1b45a2 14647 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
7341d5e2 14648 if (h == NULL)
0a1b45a2 14649 return false;
7341d5e2
AM
14650 if (h->root.type == bfd_link_hash_new)
14651 {
14652 h->root.type = bfd_link_hash_defined;
14653 h->root.u.def.section = s;
14654 h->root.u.def.value = p - s->contents;
14655 h->ref_regular = 1;
14656 h->def_regular = 1;
14657 h->ref_regular_nonweak = 1;
14658 h->forced_local = 1;
14659 h->non_elf = 0;
2ec55de3 14660 h->root.linker_def = 1;
7341d5e2
AM
14661 }
14662 }
14663
a345bc8d
AM
14664 if (PPC_HA (off) != 0)
14665 {
14666 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14667 p += 4;
14668 }
14669 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14670 p += 4;
14671 bfd_put_32 (s->owner, MTCTR_R12, p);
14672 p += 4;
407aa07c 14673 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14674 break;
14675 }
0a1b45a2 14676 return true;
a345bc8d
AM
14677}
14678
49c09209
AM
14679/* Write PLT relocs for locals. */
14680
0a1b45a2 14681static bool
49c09209
AM
14682write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14683{
14684 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14685 bfd *ibfd;
14686
14687 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14688 {
14689 struct got_entry **lgot_ents, **end_lgot_ents;
14690 struct plt_entry **local_plt, **lplt, **end_local_plt;
14691 Elf_Internal_Shdr *symtab_hdr;
14692 bfd_size_type locsymcount;
14693 Elf_Internal_Sym *local_syms = NULL;
14694 struct plt_entry *ent;
14695
14696 if (!is_ppc64_elf (ibfd))
14697 continue;
14698
14699 lgot_ents = elf_local_got_ents (ibfd);
14700 if (!lgot_ents)
14701 continue;
14702
14703 symtab_hdr = &elf_symtab_hdr (ibfd);
14704 locsymcount = symtab_hdr->sh_info;
14705 end_lgot_ents = lgot_ents + locsymcount;
14706 local_plt = (struct plt_entry **) end_lgot_ents;
14707 end_local_plt = local_plt + locsymcount;
14708 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14709 for (ent = *lplt; ent != NULL; ent = ent->next)
14710 if (ent->plt.offset != (bfd_vma) -1)
14711 {
14712 Elf_Internal_Sym *sym;
14713 asection *sym_sec;
14714 asection *plt, *relplt;
14715 bfd_byte *loc;
14716 bfd_vma val;
49c09209
AM
14717
14718 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14719 lplt - local_plt, ibfd))
14720 {
c9594989 14721 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209 14722 free (local_syms);
0a1b45a2 14723 return false;
49c09209
AM
14724 }
14725
14726 val = sym->st_value + ent->addend;
49c09209
AM
14727 if (sym_sec != NULL && sym_sec->output_section != NULL)
14728 val += sym_sec->output_offset + sym_sec->output_section->vma;
14729
2d7ad24e
AM
14730 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14731 {
0a1b45a2 14732 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14733 plt = htab->elf.iplt;
14734 relplt = htab->elf.irelplt;
14735 }
14736 else
14737 {
14738 plt = htab->pltlocal;
1657026c
AM
14739 relplt = NULL;
14740 if (bfd_link_pic (info)
14741 && !(info->enable_dt_relr && !htab->opd_abi))
14742 relplt = htab->relpltlocal;
2d7ad24e 14743 }
49c09209 14744
2d7ad24e
AM
14745 if (relplt == NULL)
14746 {
14747 loc = plt->contents + ent->plt.offset;
14748 bfd_put_64 (info->output_bfd, val, loc);
14749 if (htab->opd_abi)
14750 {
14751 bfd_vma toc = elf_gp (ibfd);
14752 bfd_put_64 (info->output_bfd, toc, loc + 8);
14753 }
14754 }
49c09209 14755 else
2d7ad24e
AM
14756 {
14757 Elf_Internal_Rela rela;
14758 rela.r_offset = (ent->plt.offset
14759 + plt->output_offset
14760 + plt->output_section->vma);
14761 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14762 {
14763 if (htab->opd_abi)
14764 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14765 else
14766 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14767 }
14768 else
14769 {
14770 if (htab->opd_abi)
14771 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14772 else
14773 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14774 }
14775 rela.r_addend = val;
14776 loc = relplt->contents + (relplt->reloc_count++
14777 * sizeof (Elf64_External_Rela));
14778 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14779 }
49c09209
AM
14780 }
14781
14782 if (local_syms != NULL
14783 && symtab_hdr->contents != (unsigned char *) local_syms)
14784 {
14785 if (!info->keep_memory)
14786 free (local_syms);
14787 else
14788 symtab_hdr->contents = (unsigned char *) local_syms;
14789 }
14790 }
0a1b45a2 14791 return true;
49c09209
AM
14792}
14793
a804e476
AM
14794/* Emit the static wrapper function preserving registers around a
14795 __tls_get_addr_opt call. */
14796
0a1b45a2 14797static bool
a804e476
AM
14798emit_tga_desc (struct ppc_link_hash_table *htab)
14799{
14800 asection *stub_sec = htab->tga_group->stub_sec;
14801 unsigned int cfa_updt = 11 * 4;
14802 bfd_byte *p;
14803 bfd_vma to, from, delta;
14804
14805 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14806 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14807 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14808 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14809 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14810 delta = to - from;
14811 if (delta + (1 << 25) >= 1 << 26)
14812 {
14813 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
0a1b45a2
AM
14814 htab->stub_error = true;
14815 return false;
a804e476
AM
14816 }
14817
14818 p = stub_sec->contents;
14819 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14820 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14821 p += 4;
14822 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14823 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14824}
14825
14826/* Emit eh_frame describing the static wrapper function. */
14827
14828static bfd_byte *
14829emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14830{
14831 unsigned int cfa_updt = 11 * 4;
14832 unsigned int i;
14833
14834 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14835 *p++ = DW_CFA_def_cfa_offset;
14836 if (htab->opd_abi)
14837 {
14838 *p++ = 128;
14839 *p++ = 1;
14840 }
14841 else
14842 *p++ = 96;
14843 *p++ = DW_CFA_offset_extended_sf;
14844 *p++ = 65;
14845 *p++ = (-16 / 8) & 0x7f;
14846 for (i = 4; i < 12; i++)
14847 {
14848 *p++ = DW_CFA_offset + i;
14849 *p++ = (htab->opd_abi ? 13 : 12) - i;
14850 }
14851 *p++ = DW_CFA_advance_loc + 10;
14852 *p++ = DW_CFA_def_cfa_offset;
14853 *p++ = 0;
14854 for (i = 4; i < 12; i++)
14855 *p++ = DW_CFA_restore + i;
14856 *p++ = DW_CFA_advance_loc + 2;
14857 *p++ = DW_CFA_restore_extended;
14858 *p++ = 65;
14859 return p;
14860}
14861
721956f4
AM
14862/* Build all the stubs associated with the current output file.
14863 The stubs are kept in a hash table attached to the main linker
14864 hash table. This function is called via gldelf64ppc_finish. */
14865
0a1b45a2 14866bool
e7d1c40c 14867ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14868 char **stats)
5d1634d7
AM
14869{
14870 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14871 struct map_stub *group;
721956f4 14872 asection *stub_sec;
5d1634d7 14873 bfd_byte *p;
e717da7e 14874 int stub_sec_count = 0;
5d1634d7 14875
4dfe6ac6 14876 if (htab == NULL)
0a1b45a2 14877 return false;
4dfe6ac6 14878
eea6121a 14879 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14880 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14881 {
14882 group->eh_size = 0;
14883 group->lr_restore = 0;
14884 if ((stub_sec = group->stub_sec) != NULL
14885 && stub_sec->size != 0)
14886 {
14887 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14888 stub_sec->size);
14889 if (stub_sec->contents == NULL)
0a1b45a2 14890 return false;
df136d64
AM
14891 stub_sec->size = 0;
14892 }
14893 }
5d1634d7 14894
23eb7e01 14895 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14896 {
9f951329 14897 unsigned int indx;
ad8e1ba5 14898 bfd_vma plt0;
9f951329 14899
721956f4 14900 /* Build the .glink plt call stub. */
e7d1c40c 14901 if (htab->params->emit_stub_syms)
97b639ba
AM
14902 {
14903 struct elf_link_hash_entry *h;
468392fb 14904 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
0a1b45a2 14905 true, false, false);
97b639ba 14906 if (h == NULL)
0a1b45a2 14907 return false;
97b639ba
AM
14908 if (h->root.type == bfd_link_hash_new)
14909 {
14910 h->root.type = bfd_link_hash_defined;
14911 h->root.u.def.section = htab->glink;
ee4bf8d2 14912 h->root.u.def.value = 8;
f5385ebf
AM
14913 h->ref_regular = 1;
14914 h->def_regular = 1;
14915 h->ref_regular_nonweak = 1;
14916 h->forced_local = 1;
14917 h->non_elf = 0;
2ec55de3 14918 h->root.linker_def = 1;
97b639ba
AM
14919 }
14920 }
33e44f2e
AM
14921 plt0 = (htab->elf.splt->output_section->vma
14922 + htab->elf.splt->output_offset
14923 - 16);
176a0d42
AM
14924 if (info->emitrelocations)
14925 {
14926 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14927 if (r == NULL)
0a1b45a2 14928 return false;
176a0d42
AM
14929 r->r_offset = (htab->glink->output_offset
14930 + htab->glink->output_section->vma);
14931 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14932 r->r_addend = plt0;
14933 }
4ce794b7 14934 p = htab->glink->contents;
176a0d42 14935 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14936 bfd_put_64 (htab->glink->owner, plt0, p);
14937 p += 8;
b9e5796b
AM
14938 if (htab->opd_abi)
14939 {
14940 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14941 p += 4;
14942 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14943 p += 4;
14944 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14945 p += 4;
14946 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14947 p += 4;
14948 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14949 p += 4;
14950 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14951 p += 4;
14952 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14953 p += 4;
14954 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14955 p += 4;
14956 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14957 p += 4;
14958 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14959 p += 4;
14960 }
14961 else
14962 {
3cd7c7d7
AM
14963 unsigned int insn;
14964
14965 /* 0:
14966 . .quad plt0-1f # plt0 entry relative to 1:
14967 #
14968 # We get here with r12 initially @ a glink branch
14969 # Load the address of _dl_runtime_resolve from plt0 and
14970 # jump to it, with r0 set to the index of the PLT entry
14971 # to be resolved and r11 the link map.
14972 __glink_PLTresolve:
14973 . std %r2,24(%r1) # optional
14974 . mflr %r0
14975 . bcl 20,31,1f
14976 1:
14977 . mflr %r11
14978 . mtlr %r0
14979 . ld %r0,(0b-1b)(%r11)
14980 . sub %r12,%r12,%r11
14981 . add %r11,%r0,%r11
14982 . addi %r0,%r12,1b-2f
14983 . ld %r12,0(%r11)
14984 . srdi %r0,%r0,2
14985 . mtctr %r12
14986 . ld %r11,8(%r11)
14987 . bctr
14988 2:
14989 . b __glink_PLTresolve
14990 . ...
14991 . b __glink_PLTresolve */
14992
14993 if (htab->has_plt_localentry0)
14994 {
14995 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14996 p += 4;
14997 }
b9e5796b
AM
14998 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14999 p += 4;
15000 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15001 p += 4;
15002 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15003 p += 4;
b9e5796b
AM
15004 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15005 p += 4;
3cd7c7d7
AM
15006 if (htab->has_plt_localentry0)
15007 insn = LD_R0_0R11 | (-20 & 0xfffc);
15008 else
15009 insn = LD_R0_0R11 | (-16 & 0xfffc);
15010 bfd_put_32 (htab->glink->owner, insn, p);
15011 p += 4;
b9e5796b
AM
15012 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15013 p += 4;
3cd7c7d7 15014 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 15015 p += 4;
3cd7c7d7 15016 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
15017 p += 4;
15018 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15019 p += 4;
15020 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15021 p += 4;
15022 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15023 p += 4;
15024 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15025 p += 4;
15026 }
407aa07c
AM
15027 bfd_put_32 (htab->glink->owner, BCTR, p);
15028 p += 4;
c75bc4f7 15029 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 15030
9f951329
AM
15031 /* Build the .glink lazy link call stubs. */
15032 indx = 0;
9e390558 15033 while (p < htab->glink->contents + htab->glink->size)
9f951329 15034 {
b9e5796b 15035 if (htab->opd_abi)
9f951329 15036 {
b9e5796b
AM
15037 if (indx < 0x8000)
15038 {
15039 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15040 p += 4;
15041 }
15042 else
15043 {
15044 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15045 p += 4;
15046 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15047 p);
15048 p += 4;
15049 }
9f951329 15050 }
4ce794b7 15051 bfd_put_32 (htab->glink->owner,
ee4bf8d2 15052 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 15053 indx++;
9f951329
AM
15054 p += 4;
15055 }
5d1634d7 15056 }
5d1634d7 15057
a804e476
AM
15058 if (htab->tga_group != NULL)
15059 {
15060 htab->tga_group->lr_restore = 23 * 4;
15061 htab->tga_group->stub_sec->size = 24 * 4;
15062 if (!emit_tga_desc (htab))
0a1b45a2 15063 return false;
a804e476
AM
15064 if (htab->glink_eh_frame != NULL
15065 && htab->glink_eh_frame->size != 0)
15066 {
15067 size_t align = 4;
15068
15069 p = htab->glink_eh_frame->contents;
15070 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15071 p += 17;
15072 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15073 }
15074 }
15075
49c09209
AM
15076 /* Build .glink global entry stubs, and PLT relocs for globals. */
15077 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15078
15079 if (!write_plt_relocs_for_local_syms (info))
0a1b45a2 15080 return false;
9e390558 15081
7341d5e2 15082 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 15083 {
4ce794b7 15084 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 15085 htab->brlt->size);
4ce794b7 15086 if (htab->brlt->contents == NULL)
0a1b45a2 15087 return false;
721956f4 15088 }
ee75fd95 15089 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
15090 {
15091 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 15092 htab->relbrlt->size);
63bc6f6c 15093 if (htab->relbrlt->contents == NULL)
0a1b45a2 15094 return false;
63bc6f6c 15095 }
5d1634d7 15096
721956f4
AM
15097 /* Build the stubs as directed by the stub hash table. */
15098 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 15099
a4b6fadd
AM
15100 for (group = htab->group; group != NULL; group = group->next)
15101 if (group->needs_save_res)
7dda8d3c 15102 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 15103
aa8a7074
AM
15104 if (htab->relbrlt != NULL)
15105 htab->relbrlt->reloc_count = 0;
15106
e7d1c40c 15107 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
15108 for (group = htab->group; group != NULL; group = group->next)
15109 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
15110 {
15111 int align = abs (htab->params->plt_stub_align);
15112 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15113 }
794e51c0 15114
7dda8d3c
AM
15115 for (group = htab->group; group != NULL; group = group->next)
15116 if (group->needs_save_res)
15117 {
15118 stub_sec = group->stub_sec;
15119 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15120 htab->sfpr->contents, htab->sfpr->size);
15121 if (htab->params->emit_stub_syms)
15122 {
15123 unsigned int i;
15124
15125 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15126 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
0a1b45a2 15127 return false;
7dda8d3c
AM
15128 }
15129 }
15130
df136d64
AM
15131 if (htab->glink_eh_frame != NULL
15132 && htab->glink_eh_frame->size != 0)
15133 {
15134 bfd_vma val;
15135 size_t align = 4;
15136
15137 p = htab->glink_eh_frame->contents;
15138 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15139
15140 for (group = htab->group; group != NULL; group = group->next)
15141 if (group->eh_size != 0)
15142 {
15143 /* Offset to stub section. */
15144 val = (group->stub_sec->output_section->vma
15145 + group->stub_sec->output_offset);
15146 val -= (htab->glink_eh_frame->output_section->vma
15147 + htab->glink_eh_frame->output_offset
15148 + (p + 8 - htab->glink_eh_frame->contents));
15149 if (val + 0x80000000 > 0xffffffff)
15150 {
15151 _bfd_error_handler
15152 (_("%s offset too large for .eh_frame sdata4 encoding"),
15153 group->stub_sec->name);
0a1b45a2 15154 return false;
df136d64
AM
15155 }
15156 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15157 p += (group->eh_size + 17 + 3) & -4;
15158 }
15159 if (htab->glink != NULL && htab->glink->size != 0)
15160 {
15161 /* Offset to .glink. */
15162 val = (htab->glink->output_section->vma
15163 + htab->glink->output_offset
15164 + 8);
15165 val -= (htab->glink_eh_frame->output_section->vma
15166 + htab->glink_eh_frame->output_offset
15167 + (p + 8 - htab->glink_eh_frame->contents));
15168 if (val + 0x80000000 > 0xffffffff)
15169 {
15170 _bfd_error_handler
15171 (_("%s offset too large for .eh_frame sdata4 encoding"),
15172 htab->glink->name);
0a1b45a2 15173 return false;
df136d64
AM
15174 }
15175 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15176 p += (24 + align - 1) & -align;
15177 }
15178 }
15179
1657026c
AM
15180 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15181 {
15182 htab->elf.srelrdyn->contents
15183 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15184 if (htab->elf.srelrdyn->contents == NULL)
15185 return false;
15186
15187 size_t i = 0;
15188 bfd_byte *loc = htab->elf.srelrdyn->contents;
15189 while (i < htab->relr_count)
15190 {
15191 bfd_vma base = htab->relr_addr[i];
15192 BFD_ASSERT (base % 2 == 0);
15193 bfd_put_64 (htab->elf.dynobj, base, loc);
15194 loc += 8;
15195 i++;
15196 while (i < htab->relr_count
15197 && htab->relr_addr[i] == base)
15198 {
15199 htab->stub_error = true;
15200 i++;
15201 }
15202 base += 8;
15203 while (1)
15204 {
15205 bfd_vma bits = 0;
15206 while (i < htab->relr_count
15207 && htab->relr_addr[i] - base < 63 * 8
15208 && (htab->relr_addr[i] - base) % 8 == 0)
15209 {
15210 bits |= (bfd_vma) 1 << ((htab->relr_addr[i] - base) / 8);
15211 i++;
15212 }
15213 if (bits == 0)
15214 break;
15215 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15216 loc += 8;
15217 base += 63 * 8;
15218 }
15219 }
15220 /* Pad any excess with 1's, a do-nothing encoding. */
15221 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15222 < htab->elf.srelrdyn->size)
15223 {
15224 bfd_put_64 (htab->elf.dynobj, 1, loc);
15225 loc += 8;
15226 }
15227 }
15228
d4aaa2a0
AM
15229 for (group = htab->group; group != NULL; group = group->next)
15230 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
15231 {
15232 stub_sec_count += 1;
c9301e31
AM
15233 if (stub_sec->rawsize != stub_sec->size
15234 && (htab->stub_iteration <= STUB_SHRINK_ITER
15235 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
15236 break;
15237 }
5d1634d7 15238
25516cc5 15239 if (group != NULL)
1657026c
AM
15240 htab->stub_error = true;
15241
15242 if (htab->stub_error)
5d1634d7 15243 {
cf97bcb0 15244 _bfd_error_handler (_("stubs don't match calculated size"));
1657026c 15245 return false;
5d1634d7 15246 }
721956f4 15247
d2a300cf
AM
15248 if (stats != NULL)
15249 {
988b7300
AM
15250 char *groupmsg;
15251 if (asprintf (&groupmsg,
15252 ngettext ("linker stubs in %u group\n",
15253 "linker stubs in %u groups\n",
15254 stub_sec_count),
15255 stub_sec_count) < 0)
15256 *stats = NULL;
15257 else
15258 {
15259 if (asprintf (stats, _("%s"
15260 " branch %lu\n"
988b7300 15261 " long branch %lu\n"
988b7300 15262 " plt call %lu\n"
988b7300
AM
15263 " global entry %lu"),
15264 groupmsg,
15265 htab->stub_count[ppc_stub_long_branch - 1],
988b7300 15266 htab->stub_count[ppc_stub_plt_branch - 1],
988b7300 15267 htab->stub_count[ppc_stub_plt_call - 1],
988b7300
AM
15268 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15269 *stats = NULL;
15270 free (groupmsg);
15271 }
d2a300cf 15272 }
0a1b45a2 15273 return true;
5bd4f169
AM
15274}
15275
60124e18
AM
15276/* What to do when ld finds relocations against symbols defined in
15277 discarded sections. */
15278
15279static unsigned int
15280ppc64_elf_action_discarded (asection *sec)
15281{
15282 if (strcmp (".opd", sec->name) == 0)
15283 return 0;
15284
15285 if (strcmp (".toc", sec->name) == 0)
15286 return 0;
15287
bce50a28
JJ
15288 if (strcmp (".toc1", sec->name) == 0)
15289 return 0;
15290
60124e18
AM
15291 return _bfd_elf_default_action_discarded (sec);
15292}
15293
e59a1001
AM
15294/* These are the dynamic relocations supported by glibc. */
15295
0a1b45a2 15296static bool
e59a1001
AM
15297ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15298{
15299 switch (r_type)
15300 {
15301 case R_PPC64_RELATIVE:
15302 case R_PPC64_NONE:
15303 case R_PPC64_ADDR64:
15304 case R_PPC64_GLOB_DAT:
15305 case R_PPC64_IRELATIVE:
15306 case R_PPC64_JMP_IREL:
15307 case R_PPC64_JMP_SLOT:
15308 case R_PPC64_DTPMOD64:
15309 case R_PPC64_DTPREL64:
15310 case R_PPC64_TPREL64:
15311 case R_PPC64_TPREL16_LO_DS:
15312 case R_PPC64_TPREL16_DS:
15313 case R_PPC64_TPREL16:
15314 case R_PPC64_TPREL16_LO:
15315 case R_PPC64_TPREL16_HI:
15316 case R_PPC64_TPREL16_HIGH:
15317 case R_PPC64_TPREL16_HA:
15318 case R_PPC64_TPREL16_HIGHA:
15319 case R_PPC64_TPREL16_HIGHER:
15320 case R_PPC64_TPREL16_HIGHEST:
15321 case R_PPC64_TPREL16_HIGHERA:
15322 case R_PPC64_TPREL16_HIGHESTA:
15323 case R_PPC64_ADDR16_LO_DS:
15324 case R_PPC64_ADDR16_LO:
15325 case R_PPC64_ADDR16_HI:
15326 case R_PPC64_ADDR16_HIGH:
15327 case R_PPC64_ADDR16_HA:
15328 case R_PPC64_ADDR16_HIGHA:
15329 case R_PPC64_REL30:
15330 case R_PPC64_COPY:
15331 case R_PPC64_UADDR64:
15332 case R_PPC64_UADDR32:
15333 case R_PPC64_ADDR32:
15334 case R_PPC64_ADDR24:
15335 case R_PPC64_ADDR16:
15336 case R_PPC64_UADDR16:
15337 case R_PPC64_ADDR16_DS:
15338 case R_PPC64_ADDR16_HIGHER:
15339 case R_PPC64_ADDR16_HIGHEST:
15340 case R_PPC64_ADDR16_HIGHERA:
15341 case R_PPC64_ADDR16_HIGHESTA:
15342 case R_PPC64_ADDR14:
15343 case R_PPC64_ADDR14_BRTAKEN:
15344 case R_PPC64_ADDR14_BRNTAKEN:
15345 case R_PPC64_REL32:
15346 case R_PPC64_REL64:
0a1b45a2 15347 return true;
e59a1001
AM
15348
15349 default:
0a1b45a2 15350 return false;
e59a1001
AM
15351 }
15352}
15353
5bd4f169
AM
15354/* The RELOCATE_SECTION function is called by the ELF backend linker
15355 to handle the relocations for a section.
15356
15357 The relocs are always passed as Rela structures; if the section
15358 actually uses Rel structures, the r_addend field will always be
15359 zero.
15360
15361 This function is responsible for adjust the section contents as
15362 necessary, and (if using Rela relocs and generating a
1049f94e 15363 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
15364 necessary.
15365
15366 This function does not have to worry about setting the reloc
15367 address or the reloc symbol index.
15368
15369 LOCAL_SYMS is a pointer to the swapped in local symbols.
15370
15371 LOCAL_SECTIONS is an array giving the section in the input file
15372 corresponding to the st_shndx field of each local symbol.
15373
15374 The global hash table entry for the global symbols can be found
15375 via elf_sym_hashes (input_bfd).
15376
1049f94e 15377 When generating relocatable output, this function must handle
5bd4f169
AM
15378 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15379 going to be the section symbol corresponding to the output
15380 section, which means that the addend must be adjusted
15381 accordingly. */
15382
0f684201 15383static int
4ce794b7
AM
15384ppc64_elf_relocate_section (bfd *output_bfd,
15385 struct bfd_link_info *info,
15386 bfd *input_bfd,
15387 asection *input_section,
15388 bfd_byte *contents,
15389 Elf_Internal_Rela *relocs,
15390 Elf_Internal_Sym *local_syms,
15391 asection **local_sections)
5bd4f169 15392{
65f38f15 15393 struct ppc_link_hash_table *htab;
5bd4f169
AM
15394 Elf_Internal_Shdr *symtab_hdr;
15395 struct elf_link_hash_entry **sym_hashes;
5bd4f169 15396 Elf_Internal_Rela *rel;
c316a17c 15397 Elf_Internal_Rela *wrel;
5bd4f169 15398 Elf_Internal_Rela *relend;
411e1bfb
AM
15399 Elf_Internal_Rela outrel;
15400 bfd_byte *loc;
411e1bfb 15401 struct got_entry **local_got_ents;
5bd4f169 15402 bfd_vma TOCstart;
0a1b45a2
AM
15403 bool ret = true;
15404 bool is_opd;
794e51c0 15405 /* Assume 'at' branch hints. */
0a1b45a2
AM
15406 bool is_isa_v2 = true;
15407 bool warned_dynamic = false;
95f0d0d2 15408 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 15409
65f38f15 15410 /* Initialize howto table if needed. */
5bd4f169 15411 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
15412 ppc_howto_init ();
15413
65f38f15 15414 htab = ppc_hash_table (info);
4dfe6ac6 15415 if (htab == NULL)
0a1b45a2 15416 return false;
ee75fd95
AM
15417
15418 /* Don't relocate stub sections. */
e7d1c40c 15419 if (input_section->owner == htab->params->stub_bfd)
0a1b45a2 15420 return true;
ee75fd95 15421
7af5d5c4
AM
15422 if (!is_ppc64_elf (input_bfd))
15423 {
15424 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 15425 return false;
7af5d5c4 15426 }
0ffa91dd 15427
411e1bfb 15428 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 15429 TOCstart = elf_gp (output_bfd);
0ffa91dd 15430 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 15431 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 15432 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 15433
c316a17c 15434 rel = wrel = relocs;
5bd4f169 15435 relend = relocs + input_section->reloc_count;
c316a17c 15436 for (; rel < relend; wrel++, rel++)
5bd4f169 15437 {
04c9666a 15438 enum elf_ppc64_reloc_type r_type;
31c76678 15439 bfd_vma addend;
5bd4f169
AM
15440 bfd_reloc_status_type r;
15441 Elf_Internal_Sym *sym;
15442 asection *sec;
039b3fef
AM
15443 struct elf_link_hash_entry *h_elf;
15444 struct ppc_link_hash_entry *h;
15445 struct ppc_link_hash_entry *fdh;
5bd4f169 15446 const char *sym_name;
0d4792f7 15447 unsigned long r_symndx, toc_symndx;
3a71aa26 15448 bfd_vma toc_addend;
f961d9dd
AM
15449 unsigned char tls_mask, tls_gd, tls_type;
15450 unsigned char sym_type;
5bd4f169 15451 bfd_vma relocation;
0a1b45a2
AM
15452 bool unresolved_reloc, save_unresolved_reloc;
15453 bool warned;
bc30df16 15454 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 15455 unsigned int insn;
e11840f9 15456 unsigned int mask;
721956f4
AM
15457 struct ppc_stub_hash_entry *stub_entry;
15458 bfd_vma max_br_offset;
15459 bfd_vma from;
c316a17c 15460 Elf_Internal_Rela orig_rel;
b80eed39
AM
15461 reloc_howto_type *howto;
15462 struct reloc_howto_struct alt_howto;
4a421c53
AM
15463 uint64_t pinsn;
15464 bfd_vma offset;
5bd4f169 15465
c316a17c
AM
15466 again:
15467 orig_rel = *rel;
15468
4ce794b7 15469 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 15470 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
15471
15472 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15473 symbol of the previous ADDR64 reloc. The symbol gives us the
15474 proper TOC base to use. */
15475 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
15476 && wrel != relocs
15477 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 15478 && is_opd)
c316a17c 15479 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 15480
4ce794b7
AM
15481 sym = NULL;
15482 sec = NULL;
039b3fef 15483 h_elf = NULL;
4ce794b7 15484 sym_name = NULL;
0a1b45a2
AM
15485 unresolved_reloc = false;
15486 warned = false;
65f38f15 15487
0b13192e 15488 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15489 {
15490 /* It's a local symbol. */
74f0fb50 15491 struct _opd_sec_data *opd;
4025353c 15492
5bd4f169
AM
15493 sym = local_syms + r_symndx;
15494 sec = local_sections[r_symndx];
26c61ae5 15495 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15496 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15497 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15498 opd = get_opd_info (sec);
15499 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15500 {
51aecdc5
AM
15501 long adjust = opd->adjust[OPD_NDX (sym->st_value
15502 + rel->r_addend)];
4025353c
AM
15503 if (adjust == -1)
15504 relocation = 0;
15505 else
4cc603a5
AM
15506 {
15507 /* If this is a relocation against the opd section sym
15508 and we have edited .opd, adjust the reloc addend so
15509 that ld -r and ld --emit-relocs output is correct.
15510 If it is a reloc against some other .opd symbol,
15511 then the symbol value will be adjusted later. */
15512 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15513 rel->r_addend += adjust;
15514 else
15515 relocation += adjust;
15516 }
1e2f5b6e 15517 }
5bd4f169
AM
15518 }
15519 else
15520 {
0a1b45a2 15521 bool ignored;
62d887d4 15522
b2a8e766
AM
15523 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15524 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15525 h_elf, sec, relocation,
62d887d4 15526 unresolved_reloc, warned, ignored);
039b3fef
AM
15527 sym_name = h_elf->root.root.string;
15528 sym_type = h_elf->type;
b69fdb4e
AM
15529 if (sec != NULL
15530 && sec->owner == output_bfd
15531 && strcmp (sec->name, ".opd") == 0)
15532 {
15533 /* This is a symbol defined in a linker script. All
15534 such are defined in output sections, even those
15535 defined by simple assignment from a symbol defined in
15536 an input section. Transfer the symbol to an
15537 appropriate input .opd section, so that a branch to
15538 this symbol will be mapped to the location specified
15539 by the opd entry. */
15540 struct bfd_link_order *lo;
15541 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15542 if (lo->type == bfd_indirect_link_order)
15543 {
15544 asection *isec = lo->u.indirect.section;
15545 if (h_elf->root.u.def.value >= isec->output_offset
15546 && h_elf->root.u.def.value < (isec->output_offset
15547 + isec->size))
15548 {
15549 h_elf->root.u.def.value -= isec->output_offset;
15550 h_elf->root.u.def.section = isec;
15551 sec = isec;
15552 break;
15553 }
15554 }
15555 }
5bd4f169 15556 }
ed7007c1 15557 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15558
dbaa2011 15559 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15560 {
15561 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15562 input_bfd, input_section,
0930cb30 15563 contents, rel->r_offset);
c316a17c
AM
15564 wrel->r_offset = rel->r_offset;
15565 wrel->r_info = 0;
15566 wrel->r_addend = 0;
15567
15568 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15569 symbols defined in discarded sections. Not done for
c316a17c
AM
15570 non-debug to preserve relocs in .eh_frame which the
15571 eh_frame editing code expects to be present. */
15572 if (bfd_link_relocatable (info)
15573 && (input_section->flags & SEC_DEBUGGING))
15574 wrel--;
15575
15576 continue;
15577 }
ab96bf03 15578
0e1862bb 15579 if (bfd_link_relocatable (info))
c316a17c 15580 goto copy_reloc;
ab96bf03 15581
f40da81b
AM
15582 if (h != NULL && &h->elf == htab->elf.hgot)
15583 {
6f20ed8a 15584 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b 15585 sec = bfd_abs_section_ptr;
0a1b45a2 15586 unresolved_reloc = false;
f40da81b
AM
15587 }
15588
951fd09b
AM
15589 /* TLS optimizations. Replace instruction sequences and relocs
15590 based on information we collected in tls_optimize. We edit
15591 RELOCS so that --emit-relocs will output something sensible
15592 for the final instruction stream. */
15593 tls_mask = 0;
15594 tls_gd = 0;
0d4792f7 15595 toc_symndx = 0;
727fc41e
AM
15596 if (h != NULL)
15597 tls_mask = h->tls_mask;
15598 else if (local_got_ents != NULL)
411e1bfb 15599 {
e054468f
AM
15600 struct plt_entry **local_plt = (struct plt_entry **)
15601 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15602 unsigned char *lgot_masks = (unsigned char *)
e054468f 15603 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15604 tls_mask = lgot_masks[r_symndx];
15605 }
37da22e5 15606 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15607 && (r_type == R_PPC64_TLS
15608 || r_type == R_PPC64_TLSGD
15609 || r_type == R_PPC64_TLSLD))
15610 {
15611 /* Check for toc tls entries. */
f961d9dd 15612 unsigned char *toc_tls;
0d4792f7 15613
727fc41e
AM
15614 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15615 &local_syms, rel, input_bfd))
0a1b45a2 15616 return false;
0d4792f7 15617
727fc41e
AM
15618 if (toc_tls)
15619 tls_mask = *toc_tls;
0d4792f7
AM
15620 }
15621
15622 /* Check that tls relocs are used with tls syms, and non-tls
15623 relocs are used with non-tls syms. */
cf35638d 15624 if (r_symndx != STN_UNDEF
0d4792f7
AM
15625 && r_type != R_PPC64_NONE
15626 && (h == NULL
039b3fef
AM
15627 || h->elf.root.type == bfd_link_hash_defined
15628 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15629 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15630 {
37da22e5 15631 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15632 && (r_type == R_PPC64_TLS
15633 || r_type == R_PPC64_TLSGD
15634 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15635 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15636 ;
15637 else
25f53a85 15638 info->callbacks->einfo
1d483afe 15639 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15640 /* xgettext:c-format */
c1c8c1ef 15641 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15642 /* xgettext:c-format */
c1c8c1ef 15643 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15644 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15645 ppc64_elf_howto_table[r_type]->name,
15646 sym_name);
411e1bfb
AM
15647 }
15648
15649 /* Ensure reloc mapping code below stays sane. */
15650 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15651 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15652 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15653 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15654 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15655 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15656 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15657 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15658 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15659 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15660 abort ();
0d4792f7 15661
411e1bfb
AM
15662 switch (r_type)
15663 {
15664 default:
411e1bfb
AM
15665 break;
15666
ba761f19 15667 case R_PPC64_LO_DS_OPT:
733ae98c
AM
15668 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15669 {
15670 insn = bfd_get_32 (input_bfd,
15671 contents + rel->r_offset - d_offset);
15672 if ((insn & (0x3fu << 26)) != 58u << 26)
15673 abort ();
15674 insn += (14u << 26) - (58u << 26);
15675 bfd_put_32 (input_bfd, insn,
15676 contents + rel->r_offset - d_offset);
15677 r_type = R_PPC64_TOC16_LO;
15678 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15679 }
ba761f19
AM
15680 break;
15681
411e1bfb
AM
15682 case R_PPC64_TOC16:
15683 case R_PPC64_TOC16_LO:
15684 case R_PPC64_TOC16_DS:
15685 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15686 {
15687 /* Check for toc tls entries. */
f961d9dd 15688 unsigned char *toc_tls;
951fd09b 15689 int retval;
411e1bfb 15690
3a71aa26
AM
15691 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15692 &local_syms, rel, input_bfd);
951fd09b 15693 if (retval == 0)
0a1b45a2 15694 return false;
411e1bfb
AM
15695
15696 if (toc_tls)
15697 {
951fd09b 15698 tls_mask = *toc_tls;
411e1bfb
AM
15699 if (r_type == R_PPC64_TOC16_DS
15700 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15701 {
37da22e5 15702 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15703 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15704 goto toctprel;
15705 }
411e1bfb 15706 else
951fd09b
AM
15707 {
15708 /* If we found a GD reloc pair, then we might be
15709 doing a GD->IE transition. */
15710 if (retval == 2)
15711 {
b00a0a86 15712 tls_gd = TLS_GDIE;
37da22e5
AM
15713 if ((tls_mask & TLS_TLS) != 0
15714 && (tls_mask & TLS_GD) == 0)
102890f0 15715 goto tls_ldgd_opt;
951fd09b
AM
15716 }
15717 else if (retval == 3)
15718 {
37da22e5
AM
15719 if ((tls_mask & TLS_TLS) != 0
15720 && (tls_mask & TLS_LD) == 0)
102890f0 15721 goto tls_ldgd_opt;
951fd09b
AM
15722 }
15723 }
411e1bfb
AM
15724 }
15725 }
15726 break;
15727
9d6ded02
AM
15728 case R_PPC64_GOT_TPREL16_HI:
15729 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15730 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15731 && (tls_mask & TLS_TPREL) == 0
15732 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
9d6ded02
AM
15733 {
15734 rel->r_offset -= d_offset;
95f0d0d2 15735 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15736 r_type = R_PPC64_NONE;
15737 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15738 }
15739 break;
15740
411e1bfb
AM
15741 case R_PPC64_GOT_TPREL16_DS:
15742 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15743 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15744 && (tls_mask & TLS_TPREL) == 0
15745 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
411e1bfb 15746 {
81407a69 15747 toctprel:
95f0d0d2 15748 insn = bfd_get_32 (input_bfd,
c316a17c 15749 contents + rel->r_offset - d_offset);
411e1bfb
AM
15750 insn &= 31 << 21;
15751 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15752 bfd_put_32 (input_bfd, insn,
c316a17c 15753 contents + rel->r_offset - d_offset);
411e1bfb 15754 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15755 if (toc_symndx != 0)
15756 {
15757 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15758 rel->r_addend = toc_addend;
0d4792f7
AM
15759 /* We changed the symbol. Start over in order to
15760 get h, sym, sec etc. right. */
c316a17c 15761 goto again;
0d4792f7
AM
15762 }
15763 else
15764 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15765 }
15766 break;
15767
87c69f97 15768 case R_PPC64_GOT_TPREL_PCREL34:
c213164a 15769 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15770 && (tls_mask & TLS_TPREL) == 0
15771 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15772 {
15773 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15774 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15775 pinsn <<= 32;
15776 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15777 pinsn += ((2ULL << 56) + (-1ULL << 52)
15778 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15779 bfd_put_32 (input_bfd, pinsn >> 32,
15780 contents + rel->r_offset);
15781 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15782 contents + rel->r_offset + 4);
15783 r_type = R_PPC64_TPREL34;
15784 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15785 }
15786 break;
15787
411e1bfb 15788 case R_PPC64_TLS:
37da22e5 15789 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15790 && (tls_mask & TLS_TPREL) == 0
15791 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15792 {
c213164a 15793 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15794 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15795 if (insn == 0)
c213164a
AM
15796 break;
15797 if ((rel->r_offset & 3) == 0)
0d4792f7 15798 {
c213164a
AM
15799 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15800 /* Was PPC64_TLS which sits on insn boundary, now
15801 PPC64_TPREL16_LO which is at low-order half-word. */
15802 rel->r_offset += d_offset;
15803 r_type = R_PPC64_TPREL16_LO;
15804 if (toc_symndx != 0)
15805 {
15806 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15807 rel->r_addend = toc_addend;
15808 /* We changed the symbol. Start over in order to
15809 get h, sym, sec etc. right. */
15810 goto again;
15811 }
15812 else
15813 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15814 }
15815 else if ((rel->r_offset & 3) == 1)
15816 {
15817 /* For pcrel IE to LE we already have the full
15818 offset and thus don't need an addi here. A nop
15819 or mr will do. */
2365f8d7 15820 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15821 {
15822 /* Extract regs from addi rt,ra,si. */
15823 unsigned int rt = (insn >> 21) & 0x1f;
15824 unsigned int ra = (insn >> 16) & 0x1f;
15825 if (rt == ra)
15826 insn = NOP;
15827 else
15828 {
15829 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15830 insn = (rt << 16) | (ra << 21) | (ra << 11);
15831 insn |= (31u << 26) | (444u << 1);
15832 }
15833 }
15834 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15835 }
411e1bfb
AM
15836 }
15837 break;
15838
411e1bfb
AM
15839 case R_PPC64_GOT_TLSGD16_HI:
15840 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15841 tls_gd = TLS_GDIE;
733ae98c
AM
15842 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15843 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b
AM
15844 goto tls_gdld_hi;
15845 break;
15846
411e1bfb
AM
15847 case R_PPC64_GOT_TLSLD16_HI:
15848 case R_PPC64_GOT_TLSLD16_HA:
733ae98c
AM
15849 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15850 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15851 {
951fd09b
AM
15852 tls_gdld_hi:
15853 if ((tls_mask & tls_gd) != 0)
15854 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15855 + R_PPC64_GOT_TPREL16_DS);
15856 else
411e1bfb 15857 {
4fe5ca5b 15858 rel->r_offset -= d_offset;
95f0d0d2 15859 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15860 r_type = R_PPC64_NONE;
411e1bfb 15861 }
951fd09b 15862 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15863 }
15864 break;
15865
951fd09b
AM
15866 case R_PPC64_GOT_TLSGD16:
15867 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15868 tls_gd = TLS_GDIE;
733ae98c
AM
15869 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15870 && offset_in_range (input_section, rel->r_offset & ~3, 4))
102890f0 15871 goto tls_ldgd_opt;
951fd09b 15872 break;
411e1bfb 15873
951fd09b
AM
15874 case R_PPC64_GOT_TLSLD16:
15875 case R_PPC64_GOT_TLSLD16_LO:
733ae98c
AM
15876 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15877 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b 15878 {
b9f04fe0 15879 unsigned int insn1, insn2;
102890f0
AM
15880
15881 tls_ldgd_opt:
727fc41e
AM
15882 offset = (bfd_vma) -1;
15883 /* If not using the newer R_PPC64_TLSGD/LD to mark
15884 __tls_get_addr calls, we must trust that the call
15885 stays with its arg setup insns, ie. that the next
15886 reloc is the __tls_get_addr call associated with
15887 the current reloc. Edit both insns. */
9737e8af 15888 if (input_section->nomark_tls_get_addr
727fc41e
AM
15889 && rel + 1 < relend
15890 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15891 htab->tls_get_addr_fd,
15892 htab->tga_desc_fd,
727fc41e 15893 htab->tls_get_addr,
9e7028aa 15894 htab->tga_desc))
727fc41e 15895 offset = rel[1].r_offset;
b86ac8e3
AM
15896 /* We read the low GOT_TLS (or TOC16) insn because we
15897 need to keep the destination reg. It may be
15898 something other than the usual r3, and moved to r3
15899 before the call by intervening code. */
95f0d0d2 15900 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15901 contents + rel->r_offset - d_offset);
102890f0 15902 if ((tls_mask & tls_gd) != 0)
411e1bfb 15903 {
102890f0 15904 /* IE */
b86ac8e3 15905 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15906 insn1 |= 58u << 26; /* ld */
102890f0 15907 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15908 if (offset != (bfd_vma) -1)
f58d5a2d 15909 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15910 if (r_type == R_PPC64_TOC16
15911 || r_type == R_PPC64_TOC16_LO)
102890f0 15912 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15913 else
15914 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15915 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15916 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15917 }
15918 else
15919 {
15920 /* LE */
b86ac8e3
AM
15921 insn1 &= 0x1f << 21;
15922 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15923 insn2 = 0x38630000; /* addi 3,3,0 */
15924 if (tls_gd == 0)
951fd09b 15925 {
102890f0 15926 /* Was an LD reloc. */
71c4e95a 15927 r_symndx = STN_UNDEF;
102890f0 15928 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15929 }
102890f0 15930 else if (toc_symndx != 0)
3a71aa26
AM
15931 {
15932 r_symndx = toc_symndx;
15933 rel->r_addend = toc_addend;
15934 }
102890f0
AM
15935 r_type = R_PPC64_TPREL16_HA;
15936 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15937 if (offset != (bfd_vma) -1)
15938 {
15939 rel[1].r_info = ELF64_R_INFO (r_symndx,
15940 R_PPC64_TPREL16_LO);
15941 rel[1].r_offset = offset + d_offset;
15942 rel[1].r_addend = rel->r_addend;
15943 }
102890f0 15944 }
95f0d0d2 15945 bfd_put_32 (input_bfd, insn1,
3a71aa26 15946 contents + rel->r_offset - d_offset);
733ae98c
AM
15947 if (offset != (bfd_vma) -1
15948 && offset_in_range (input_section, offset, 4))
c96e0573
AM
15949 {
15950 bfd_put_32 (input_bfd, insn2, contents + offset);
733ae98c 15951 if (offset_in_range (input_section, offset + 4, 4))
c96e0573
AM
15952 {
15953 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15954 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15955 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15956 }
15957 }
727fc41e
AM
15958 if ((tls_mask & tls_gd) == 0
15959 && (tls_gd == 0 || toc_symndx != 0))
15960 {
15961 /* We changed the symbol. Start over in order
15962 to get h, sym, sec etc. right. */
c316a17c 15963 goto again;
727fc41e
AM
15964 }
15965 }
15966 break;
15967
87c69f97 15968 case R_PPC64_GOT_TLSGD_PCREL34:
733ae98c
AM
15969 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15970 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15971 {
15972 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15973 pinsn <<= 32;
15974 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15975 if ((tls_mask & TLS_GDIE) != 0)
15976 {
15977 /* IE, pla -> pld */
15978 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15979 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15980 }
15981 else
15982 {
15983 /* LE, pla pcrel -> paddi r13 */
15984 pinsn += (-1ULL << 52) + (13ULL << 16);
15985 r_type = R_PPC64_TPREL34;
15986 }
15987 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15988 bfd_put_32 (input_bfd, pinsn >> 32,
15989 contents + rel->r_offset);
15990 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15991 contents + rel->r_offset + 4);
15992 }
15993 break;
15994
87c69f97 15995 case R_PPC64_GOT_TLSLD_PCREL34:
733ae98c
AM
15996 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15997 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15998 {
15999 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16000 pinsn <<= 32;
16001 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16002 pinsn += (-1ULL << 52) + (13ULL << 16);
16003 bfd_put_32 (input_bfd, pinsn >> 32,
16004 contents + rel->r_offset);
16005 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16006 contents + rel->r_offset + 4);
16007 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16008 r_symndx = STN_UNDEF;
16009 r_type = R_PPC64_TPREL34;
16010 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16011 goto again;
16012 }
16013 break;
16014
727fc41e 16015 case R_PPC64_TLSGD:
37da22e5 16016 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
733ae98c
AM
16017 && rel + 1 < relend
16018 && offset_in_range (input_section, rel->r_offset,
16019 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16020 ? 8 : 4))
727fc41e 16021 {
b9f04fe0 16022 unsigned int insn2;
5663e321 16023 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16024
4a421c53 16025 offset = rel->r_offset;
5663e321 16026 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16027 {
16028 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16029 if (r_type1 == R_PPC64_PLT_PCREL34
16030 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16031 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16032 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16033 break;
16034 }
16035
733ae98c 16036 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16037 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16038
b00a0a86 16039 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
16040 {
16041 /* IE */
16042 r_type = R_PPC64_NONE;
16043 insn2 = 0x7c636a14; /* add 3,3,13 */
16044 }
16045 else
16046 {
16047 /* LE */
16048 if (toc_symndx != 0)
16049 {
16050 r_symndx = toc_symndx;
16051 rel->r_addend = toc_addend;
16052 }
c213164a 16053 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16054 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16055 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16056 {
16057 r_type = R_PPC64_NONE;
16058 insn2 = NOP;
16059 }
16060 else
16061 {
16062 rel->r_offset = offset + d_offset;
16063 r_type = R_PPC64_TPREL16_LO;
16064 insn2 = 0x38630000; /* addi 3,3,0 */
16065 }
727fc41e
AM
16066 }
16067 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16068 /* Zap the reloc on the _tls_get_addr call too. */
16069 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16070 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16071 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16072 if ((tls_mask & TLS_GDIE) == 0
16073 && toc_symndx != 0
16074 && r_type != R_PPC64_NONE)
c316a17c 16075 goto again;
411e1bfb 16076 }
411e1bfb
AM
16077 break;
16078
727fc41e 16079 case R_PPC64_TLSLD:
37da22e5 16080 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
733ae98c
AM
16081 && rel + 1 < relend
16082 && offset_in_range (input_section, rel->r_offset,
16083 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16084 ? 8 : 4))
727fc41e 16085 {
b9f04fe0 16086 unsigned int insn2;
5663e321 16087 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16088
4a421c53 16089 offset = rel->r_offset;
5663e321 16090 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16091 {
16092 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16093 if (r_type1 == R_PPC64_PLT_PCREL34
16094 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16095 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16096 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16097 break;
16098 }
16099
733ae98c 16100 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16101 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16102
c213164a 16103 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16104 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16105 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16106 {
16107 r_type = R_PPC64_NONE;
16108 insn2 = NOP;
16109 }
16110 else
16111 {
16112 rel->r_offset = offset + d_offset;
16113 r_symndx = STN_UNDEF;
16114 r_type = R_PPC64_TPREL16_LO;
16115 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16116 insn2 = 0x38630000; /* addi 3,3,0 */
16117 }
727fc41e 16118 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
16119 /* Zap the reloc on the _tls_get_addr call too. */
16120 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16121 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16122 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16123 if (r_type != R_PPC64_NONE)
16124 goto again;
727fc41e
AM
16125 }
16126 break;
16127
411e1bfb 16128 case R_PPC64_DTPMOD64:
951fd09b
AM
16129 if (rel + 1 < relend
16130 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16131 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 16132 {
733ae98c
AM
16133 if ((tls_mask & TLS_GD) == 0
16134 && offset_in_range (input_section, rel->r_offset, 8))
951fd09b
AM
16135 {
16136 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 16137 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
16138 r_type = R_PPC64_TPREL64;
16139 else
16140 {
4ce794b7 16141 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
16142 r_type = R_PPC64_NONE;
16143 }
16144 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16145 }
16146 }
16147 else
16148 {
733ae98c
AM
16149 if ((tls_mask & TLS_LD) == 0
16150 && offset_in_range (input_section, rel->r_offset, 8))
411e1bfb 16151 {
4ce794b7 16152 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 16153 r_type = R_PPC64_NONE;
951fd09b 16154 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 16155 }
411e1bfb
AM
16156 }
16157 break;
16158
16159 case R_PPC64_TPREL64:
951fd09b 16160 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
16161 {
16162 r_type = R_PPC64_NONE;
16163 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16164 }
16165 break;
52a82034 16166
006589cf
AM
16167 case R_PPC64_ENTRY:
16168 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16169 if (!bfd_link_pic (info)
16170 && !info->traditional_format
733ae98c
AM
16171 && relocation + 0x80008000 <= 0xffffffff
16172 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16173 {
16174 unsigned int insn1, insn2;
16175
16176 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16177 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16178 if ((insn1 & ~0xfffc) == LD_R2_0R12
16179 && insn2 == ADD_R2_R2_R12)
16180 {
95f0d0d2 16181 bfd_put_32 (input_bfd,
006589cf
AM
16182 LIS_R2 + PPC_HA (relocation),
16183 contents + rel->r_offset);
95f0d0d2 16184 bfd_put_32 (input_bfd,
006589cf
AM
16185 ADDI_R2_R2 + PPC_LO (relocation),
16186 contents + rel->r_offset + 4);
16187 }
16188 }
16189 else
16190 {
16191 relocation -= (rel->r_offset
16192 + input_section->output_offset
16193 + input_section->output_section->vma);
733ae98c
AM
16194 if (relocation + 0x80008000 <= 0xffffffff
16195 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16196 {
16197 unsigned int insn1, insn2;
16198
16199 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16200 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16201 if ((insn1 & ~0xfffc) == LD_R2_0R12
16202 && insn2 == ADD_R2_R2_R12)
16203 {
95f0d0d2 16204 bfd_put_32 (input_bfd,
006589cf
AM
16205 ADDIS_R2_R12 + PPC_HA (relocation),
16206 contents + rel->r_offset);
95f0d0d2 16207 bfd_put_32 (input_bfd,
006589cf
AM
16208 ADDI_R2_R2 + PPC_LO (relocation),
16209 contents + rel->r_offset + 4);
16210 }
16211 }
16212 }
16213 break;
16214
52a82034
AM
16215 case R_PPC64_REL16_HA:
16216 /* If we are generating a non-PIC executable, edit
16217 . 0: addis 2,12,.TOC.-0b@ha
16218 . addi 2,2,.TOC.-0b@l
16219 used by ELFv2 global entry points to set up r2, to
16220 . lis 2,.TOC.@ha
16221 . addi 2,2,.TOC.@l
16222 if .TOC. is in range. */
0e1862bb 16223 if (!bfd_link_pic (info)
810d4e75 16224 && !info->traditional_format
006589cf 16225 && !htab->opd_abi
4f038ee5 16226 && rel->r_addend == d_offset
52a82034
AM
16227 && h != NULL && &h->elf == htab->elf.hgot
16228 && rel + 1 < relend
16229 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16230 && rel[1].r_offset == rel->r_offset + 4
16231 && rel[1].r_addend == rel->r_addend + 4
733ae98c
AM
16232 && relocation + 0x80008000 <= 0xffffffff
16233 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
52a82034
AM
16234 {
16235 unsigned int insn1, insn2;
4a421c53 16236 offset = rel->r_offset - d_offset;
95f0d0d2
AM
16237 insn1 = bfd_get_32 (input_bfd, contents + offset);
16238 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
16239 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16240 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
16241 {
16242 r_type = R_PPC64_ADDR16_HA;
16243 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16244 rel->r_addend -= d_offset;
16245 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16246 rel[1].r_addend -= d_offset + 4;
95f0d0d2 16247 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
16248 }
16249 }
16250 break;
411e1bfb
AM
16251 }
16252
16253 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 16254 insn = 0;
b25116a9
AM
16255 max_br_offset = 1 << 25;
16256 addend = rel->r_addend;
bc30df16 16257 reloc_dest = DEST_NORMAL;
65f38f15 16258 switch (r_type)
5bd4f169
AM
16259 {
16260 default:
65f38f15 16261 break;
5bd4f169 16262
3b421ab3
AM
16263 case R_PPC64_TOCSAVE:
16264 if (relocation + addend == (rel->r_offset
16265 + input_section->output_offset
16266 + input_section->output_section->vma)
16267 && tocsave_find (htab, NO_INSERT,
733ae98c
AM
16268 &local_syms, rel, input_bfd)
16269 && offset_in_range (input_section, rel->r_offset, 4))
3b421ab3
AM
16270 {
16271 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16272 if (insn == NOP
16273 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
16274 bfd_put_32 (input_bfd,
16275 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
16276 contents + rel->r_offset);
16277 }
16278 break;
16279
65f38f15
AM
16280 /* Branch taken prediction relocations. */
16281 case R_PPC64_ADDR14_BRTAKEN:
16282 case R_PPC64_REL14_BRTAKEN:
cedb70c5 16283 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 16284 /* Fall through. */
65f38f15 16285
86c76c7b 16286 /* Branch not taken prediction relocations. */
65f38f15
AM
16287 case R_PPC64_ADDR14_BRNTAKEN:
16288 case R_PPC64_REL14_BRNTAKEN:
733ae98c
AM
16289 if (!offset_in_range (input_section, rel->r_offset, 4))
16290 break;
95f0d0d2 16291 insn |= bfd_get_32 (input_bfd,
411e1bfb 16292 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 16293 /* Fall through. */
86c76c7b 16294
b25116a9
AM
16295 case R_PPC64_REL14:
16296 max_br_offset = 1 << 15;
1a0670f3 16297 /* Fall through. */
5bd4f169 16298
65f38f15 16299 case R_PPC64_REL24:
05d0e962 16300 case R_PPC64_REL24_NOTOC:
7aba54da 16301 case R_PPC64_REL24_P9NOTOC:
23cedd1d 16302 case R_PPC64_PLTCALL:
5663e321 16303 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
16304 /* Calls to functions with a different TOC, such as calls to
16305 shared objects, need to alter the TOC pointer. This is
16306 done using a linkage stub. A REL24 branching to these
16307 linkage stubs needs to be followed by a nop, as the nop
16308 will be replaced with an instruction to restore the TOC
16309 base pointer. */
8387904d 16310 fdh = h;
b31867b6
AM
16311 if (h != NULL
16312 && h->oh != NULL
16313 && h->oh->is_func_descriptor)
16314 fdh = ppc_follow_link (h->oh);
31c76678
DK
16315 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16316 htab);
5663e321
AM
16317 if ((r_type == R_PPC64_PLTCALL
16318 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 16319 && stub_entry != NULL
7aba54da 16320 && stub_entry->type.main == ppc_stub_plt_call)
23cedd1d
AM
16321 stub_entry = NULL;
16322
6abec6bc 16323 if (stub_entry != NULL
7aba54da
AM
16324 && (stub_entry->type.main == ppc_stub_plt_call
16325 || stub_entry->type.r2save))
41bd81ab 16326 {
0a1b45a2 16327 bool can_plt_call = false;
721956f4 16328
7aba54da
AM
16329 if (r_type == R_PPC64_REL24_NOTOC
16330 || r_type == R_PPC64_REL24_P9NOTOC)
6e1816be 16331 {
7aba54da 16332 /* NOTOC calls don't need to restore r2. */
0a1b45a2 16333 can_plt_call = true;
6e1816be 16334 }
7aba54da
AM
16335 else if (stub_entry->type.main == ppc_stub_plt_call
16336 && !htab->opd_abi
16337 && htab->params->plt_localentry0 != 0
16338 && h != NULL
16339 && is_elfv2_localentry0 (&h->elf))
05d0e962 16340 {
7aba54da 16341 /* The function doesn't use or change r2. */
0a1b45a2 16342 can_plt_call = true;
05d0e962 16343 }
6e1816be 16344
f378ab09 16345 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
16346 branch and link followed by a nop. The nop is
16347 replaced by an insn to restore r2. */
733ae98c 16348 else if (offset_in_range (input_section, rel->r_offset, 8))
41bd81ab 16349 {
ba8ca3e7
AM
16350 unsigned long br;
16351
16352 br = bfd_get_32 (input_bfd,
16353 contents + rel->r_offset);
16354 if ((br & 1) != 0)
41bd81ab 16355 {
ba8ca3e7
AM
16356 unsigned long nop;
16357
16358 nop = bfd_get_32 (input_bfd,
16359 contents + rel->r_offset + 4);
23cedd1d 16360 if (nop == LD_R2_0R1 + STK_TOC (htab))
0a1b45a2 16361 can_plt_call = true;
23cedd1d
AM
16362 else if (nop == NOP
16363 || nop == CROR_151515
16364 || nop == CROR_313131)
a7f2871e 16365 {
ba8ca3e7 16366 if (h != NULL
ed7007c1 16367 && is_tls_get_addr (&h->elf, htab)
7c9cf415 16368 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
16369 {
16370 /* Special stub used, leave nop alone. */
16371 }
16372 else
a078d95a
AM
16373 bfd_put_32 (input_bfd,
16374 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7 16375 contents + rel->r_offset + 4);
0a1b45a2 16376 can_plt_call = true;
a7f2871e 16377 }
41bd81ab 16378 }
5bd4f169 16379 }
721956f4 16380
ba8ca3e7 16381 if (!can_plt_call && h != NULL)
721956f4 16382 {
ba8ca3e7
AM
16383 const char *name = h->elf.root.root.string;
16384
16385 if (*name == '.')
16386 ++name;
16387
3f3328b8 16388 if (startswith (name, "__libc_start_main")
ba8ca3e7 16389 && (name[17] == 0 || name[17] == '@'))
6ab189d5 16390 {
ba8ca3e7
AM
16391 /* Allow crt1 branch to go via a toc adjusting
16392 stub. Other calls that never return could do
16393 the same, if we could detect such. */
0a1b45a2 16394 can_plt_call = true;
6ab189d5 16395 }
ba8ca3e7
AM
16396 }
16397
16398 if (!can_plt_call)
16399 {
16400 /* g++ as of 20130507 emits self-calls without a
16401 following nop. This is arguably wrong since we
16402 have conflicting information. On the one hand a
16403 global symbol and on the other a local call
16404 sequence, but don't error for this special case.
16405 It isn't possible to cheaply verify we have
16406 exactly such a call. Allow all calls to the same
16407 section. */
16408 asection *code_sec = sec;
16409
16410 if (get_opd_info (sec) != NULL)
ad8e1ba5 16411 {
ba8ca3e7
AM
16412 bfd_vma off = (relocation + addend
16413 - sec->output_section->vma
16414 - sec->output_offset);
bc30df16 16415
0a1b45a2 16416 opd_entry_value (sec, off, &code_sec, NULL, false);
ad8e1ba5 16417 }
ba8ca3e7 16418 if (code_sec == input_section)
0a1b45a2 16419 can_plt_call = true;
ba8ca3e7
AM
16420 }
16421
16422 if (!can_plt_call)
16423 {
7aba54da 16424 if (stub_entry->type.main == ppc_stub_plt_call)
4805fc55 16425 info->callbacks->einfo
695344c0 16426 /* xgettext:c-format */
c1c8c1ef 16427 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16428 "(plt call stub)\n"),
4805fc55
AM
16429 input_bfd, input_section, rel->r_offset, sym_name);
16430 else
16431 info->callbacks->einfo
695344c0 16432 /* xgettext:c-format */
c1c8c1ef 16433 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16434 "(toc save/adjust stub)\n"),
4805fc55 16435 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
16436
16437 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16438 ret = false;
721956f4
AM
16439 }
16440
b25116a9 16441 if (can_plt_call
7aba54da 16442 && stub_entry->type.main == ppc_stub_plt_call)
0a1b45a2 16443 unresolved_reloc = false;
b25116a9
AM
16444 }
16445
6abec6bc 16446 if ((stub_entry == NULL
7aba54da
AM
16447 || stub_entry->type.main == ppc_stub_long_branch
16448 || stub_entry->type.main == ppc_stub_plt_branch)
8387904d
AM
16449 && get_opd_info (sec) != NULL)
16450 {
16451 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
16452 bfd_vma off = (relocation + addend
16453 - sec->output_section->vma
16454 - sec->output_offset);
0a1b45a2 16455 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
8387904d
AM
16456 if (dest != (bfd_vma) -1)
16457 {
16458 relocation = dest;
16459 addend = 0;
bc30df16 16460 reloc_dest = DEST_OPD;
8387904d
AM
16461 }
16462 }
16463
b25116a9
AM
16464 /* If the branch is out of reach we ought to have a long
16465 branch stub. */
16466 from = (rel->r_offset
16467 + input_section->output_offset
16468 + input_section->output_section->vma);
16469
6911b7dc
AM
16470 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16471 ? fdh->elf.other
16472 : sym->st_other);
16473
6abec6bc 16474 if (stub_entry != NULL
7aba54da
AM
16475 && (stub_entry->type.main == ppc_stub_long_branch
16476 || stub_entry->type.main == ppc_stub_plt_branch))
16477 {
16478 if (stub_entry->type.sub == ppc_stub_toc
16479 && !stub_entry->type.r2save
16480 && (r_type == R_PPC64_ADDR14_BRTAKEN
16481 || r_type == R_PPC64_ADDR14_BRNTAKEN
16482 || (relocation + addend - from + max_br_offset
16483 < 2 * max_br_offset)))
16484 /* Don't use the stub if this branch is in range. */
16485 stub_entry = NULL;
16486
16487 if (stub_entry != NULL
16488 && stub_entry->type.sub >= ppc_stub_notoc
16489 && ((r_type != R_PPC64_REL24_NOTOC
16490 && r_type != R_PPC64_REL24_P9NOTOC)
16491 || ((fdh ? fdh->elf.other : sym->st_other)
16492 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16493 && (relocation + addend - from + max_br_offset
16494 < 2 * max_br_offset))
16495 stub_entry = NULL;
16496
16497 if (stub_entry != NULL
16498 && stub_entry->type.r2save
16499 && (r_type == R_PPC64_REL24_NOTOC
16500 || r_type == R_PPC64_REL24_P9NOTOC)
16501 && (relocation + addend - from + max_br_offset
16502 < 2 * max_br_offset))
16503 stub_entry = NULL;
16504 }
05d0e962 16505
b25116a9
AM
16506 if (stub_entry != NULL)
16507 {
16508 /* Munge up the value and addend so that we call the stub
16509 rather than the procedure directly. */
a4b6fadd
AM
16510 asection *stub_sec = stub_entry->group->stub_sec;
16511
7aba54da 16512 if (stub_entry->type.main == ppc_stub_save_res)
a4b6fadd
AM
16513 relocation += (stub_sec->output_offset
16514 + stub_sec->output_section->vma
16515 + stub_sec->size - htab->sfpr->size
16516 - htab->sfpr->output_offset
16517 - htab->sfpr->output_section->vma);
16518 else
16519 relocation = (stub_entry->stub_offset
16520 + stub_sec->output_offset
16521 + stub_sec->output_section->vma);
b25116a9 16522 addend = 0;
bc30df16 16523 reloc_dest = DEST_STUB;
3b421ab3 16524
7aba54da
AM
16525 if (((stub_entry->type.r2save
16526 && (r_type == R_PPC64_REL24_NOTOC
16527 || r_type == R_PPC64_REL24_P9NOTOC))
16528 || ((stub_entry->type.main == ppc_stub_plt_call
16529 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16530 && rel + 1 < relend
16531 && rel[1].r_offset == rel->r_offset + 4
16532 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16533 && !(stub_entry->type.main == ppc_stub_plt_call
16534 && htab->params->tls_get_addr_opt
16535 && h != NULL
16536 && is_tls_get_addr (&h->elf, htab)))
29433886
AM
16537 {
16538 /* Skip over the r2 store at the start of the stub. */
7aba54da 16539 relocation += 4;
29433886 16540 }
5663e321 16541
7aba54da
AM
16542 if ((r_type == R_PPC64_REL24_NOTOC
16543 || r_type == R_PPC64_REL24_P9NOTOC)
16544 && stub_entry->type.main == ppc_stub_plt_call
16545 && stub_entry->type.sub >= ppc_stub_notoc)
5663e321 16546 htab->notoc_plt = 1;
b25116a9
AM
16547 }
16548
16549 if (insn != 0)
16550 {
794e51c0 16551 if (is_isa_v2)
721956f4 16552 {
b25116a9
AM
16553 /* Set 'a' bit. This is 0b00010 in BO field for branch
16554 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16555 for branch on CTR insns (BO == 1a00t or 1a01t). */
16556 if ((insn & (0x14 << 21)) == (0x04 << 21))
16557 insn |= 0x02 << 21;
16558 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16559 insn |= 0x08 << 21;
16560 else
16561 break;
16562 }
16563 else
16564 {
16565 /* Invert 'y' bit if not the default. */
4cc603a5 16566 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16567 insn ^= 0x01 << 21;
721956f4 16568 }
b25116a9 16569
95f0d0d2 16570 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16571 }
e86ce104 16572
06da1e8e
AM
16573 /* NOP out calls to undefined weak functions.
16574 We can thus call a weak function without first
16575 checking whether the function is defined. */
b25116a9 16576 else if (h != NULL
039b3fef 16577 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16578 && h->elf.dynindx == -1
05d0e962 16579 && (r_type == R_PPC64_REL24
7aba54da
AM
16580 || r_type == R_PPC64_REL24_NOTOC
16581 || r_type == R_PPC64_REL24_P9NOTOC)
b25116a9 16582 && relocation == 0
733ae98c
AM
16583 && addend == 0
16584 && offset_in_range (input_section, rel->r_offset, 4))
e86ce104 16585 {
95f0d0d2 16586 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16587 goto copy_reloc;
e86ce104 16588 }
65f38f15 16589 break;
066f4018
AM
16590
16591 case R_PPC64_GOT16_DS:
6738c8a7 16592 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16593 || (bfd_link_pic (info)
16594 && sec == bfd_abs_section_ptr)
6738c8a7 16595 || !htab->do_toc_opt)
06507dab 16596 break;
066f4018
AM
16597 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16598 if (relocation + addend - from + 0x8000 < 0x10000
f5b9c288
AM
16599 && sec != NULL
16600 && sec->output_section != NULL
16601 && !discarded_section (sec)
733ae98c
AM
16602 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16603 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16604 {
16605 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16606 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16607 {
16608 insn += (14u << 26) - (58u << 26);
16609 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16610 r_type = R_PPC64_TOC16;
16611 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16612 }
16613 }
16614 break;
16615
16616 case R_PPC64_GOT16_LO_DS:
16617 case R_PPC64_GOT16_HA:
6738c8a7 16618 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16619 || (bfd_link_pic (info)
16620 && sec == bfd_abs_section_ptr)
6738c8a7 16621 || !htab->do_toc_opt)
06507dab 16622 break;
066f4018
AM
16623 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16624 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
f5b9c288
AM
16625 && sec != NULL
16626 && sec->output_section != NULL
16627 && !discarded_section (sec)
733ae98c
AM
16628 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16629 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16630 {
16631 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16632 if (r_type == R_PPC64_GOT16_LO_DS
16633 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16634 {
16635 insn += (14u << 26) - (58u << 26);
16636 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16637 r_type = R_PPC64_TOC16_LO;
16638 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16639 }
fba8689a
AM
16640 else if (r_type == R_PPC64_GOT16_HA
16641 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16642 {
16643 r_type = R_PPC64_TOC16_HA;
16644 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16645 }
16646 }
16647 break;
4a421c53
AM
16648
16649 case R_PPC64_GOT_PCREL34:
6738c8a7 16650 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16651 || (bfd_link_pic (info)
16652 && sec == bfd_abs_section_ptr)
6738c8a7 16653 || !htab->do_toc_opt)
06507dab 16654 break;
4a421c53
AM
16655 from = (rel->r_offset
16656 + input_section->output_section->vma
16657 + input_section->output_offset);
6738c8a7 16658 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
f5b9c288
AM
16659 && sec != NULL
16660 && sec->output_section != NULL
16661 && !discarded_section (sec)
733ae98c
AM
16662 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16663 && offset_in_range (input_section, rel->r_offset, 8)))
6738c8a7
AM
16664 break;
16665
16666 offset = rel->r_offset;
16667 pinsn = bfd_get_32 (input_bfd, contents + offset);
16668 pinsn <<= 32;
16669 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16670 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16671 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16672 break;
16673
16674 /* Replace with paddi. */
16675 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16676 r_type = R_PPC64_PCREL34;
16677 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16678 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16679 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16680 /* Fall through. */
4a421c53
AM
16681
16682 case R_PPC64_PCREL34:
6738c8a7
AM
16683 if (!htab->params->no_pcrel_opt
16684 && rel + 1 < relend
16685 && rel[1].r_offset == rel->r_offset
16686 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
733ae98c
AM
16687 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16688 && offset_in_range (input_section, rel->r_offset, 8))
4a421c53
AM
16689 {
16690 offset = rel->r_offset;
16691 pinsn = bfd_get_32 (input_bfd, contents + offset);
16692 pinsn <<= 32;
16693 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16694 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16695 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16696 | (14ULL << 26) /* paddi */))
16697 {
6738c8a7
AM
16698 bfd_vma off2 = rel[1].r_addend;
16699 if (off2 == 0)
16700 /* zero means next insn. */
16701 off2 = 8;
16702 off2 += offset;
733ae98c 16703 if (offset_in_range (input_section, off2, 4))
4a421c53 16704 {
6738c8a7
AM
16705 uint64_t pinsn2;
16706 bfd_signed_vma addend_off;
16707 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16708 pinsn2 <<= 32;
16709 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16710 {
733ae98c 16711 if (!offset_in_range (input_section, off2, 8))
6738c8a7
AM
16712 break;
16713 pinsn2 |= bfd_get_32 (input_bfd,
16714 contents + off2 + 4);
16715 }
16716 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16717 {
16718 addend += addend_off;
16719 rel->r_addend = addend;
16720 bfd_put_32 (input_bfd, pinsn >> 32,
16721 contents + offset);
16722 bfd_put_32 (input_bfd, pinsn,
16723 contents + offset + 4);
16724 bfd_put_32 (input_bfd, pinsn2 >> 32,
16725 contents + off2);
4a421c53 16726 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16727 bfd_put_32 (input_bfd, pinsn2,
16728 contents + off2 + 4);
4a421c53
AM
16729 }
16730 }
16731 }
16732 }
16733 break;
65f38f15 16734 }
5bd4f169 16735
411e1bfb 16736 tls_type = 0;
23cedd1d 16737 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16738 switch (r_type)
16739 {
16740 default:
cf97bcb0
AM
16741 /* xgettext:c-format */
16742 _bfd_error_handler (_("%pB: %s unsupported"),
16743 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16744
65f38f15 16745 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16746 ret = false;
c316a17c 16747 goto copy_reloc;
5bd4f169 16748
65f38f15 16749 case R_PPC64_NONE:
411e1bfb 16750 case R_PPC64_TLS:
727fc41e
AM
16751 case R_PPC64_TLSGD:
16752 case R_PPC64_TLSLD:
3b421ab3 16753 case R_PPC64_TOCSAVE:
04c9666a
AM
16754 case R_PPC64_GNU_VTINHERIT:
16755 case R_PPC64_GNU_VTENTRY:
006589cf 16756 case R_PPC64_ENTRY:
4a421c53 16757 case R_PPC64_PCREL_OPT:
c316a17c 16758 goto copy_reloc;
5bd4f169
AM
16759
16760 /* GOT16 relocations. Like an ADDR16 using the symbol's
16761 address in the GOT as relocation value instead of the
411e1bfb 16762 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16763 symbol and put the symbol value there. */
411e1bfb
AM
16764 case R_PPC64_GOT_TLSGD16:
16765 case R_PPC64_GOT_TLSGD16_LO:
16766 case R_PPC64_GOT_TLSGD16_HI:
16767 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16768 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16769 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16770 goto dogot;
16771
16772 case R_PPC64_GOT_TLSLD16:
16773 case R_PPC64_GOT_TLSLD16_LO:
16774 case R_PPC64_GOT_TLSLD16_HI:
16775 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16776 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16777 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16778 goto dogot;
16779
16780 case R_PPC64_GOT_TPREL16_DS:
16781 case R_PPC64_GOT_TPREL16_LO_DS:
16782 case R_PPC64_GOT_TPREL16_HI:
16783 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16784 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16785 tls_type = TLS_TLS | TLS_TPREL;
16786 goto dogot;
16787
16788 case R_PPC64_GOT_DTPREL16_DS:
16789 case R_PPC64_GOT_DTPREL16_LO_DS:
16790 case R_PPC64_GOT_DTPREL16_HI:
16791 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16792 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16793 tls_type = TLS_TLS | TLS_DTPREL;
16794 goto dogot;
16795
65f38f15
AM
16796 case R_PPC64_GOT16:
16797 case R_PPC64_GOT16_LO:
16798 case R_PPC64_GOT16_HI:
16799 case R_PPC64_GOT16_HA:
16800 case R_PPC64_GOT16_DS:
16801 case R_PPC64_GOT16_LO_DS:
5663e321 16802 case R_PPC64_GOT_PCREL34:
411e1bfb 16803 dogot:
5bd4f169
AM
16804 {
16805 /* Relocation is to the entry for this symbol in the global
16806 offset table. */
e717da7e 16807 asection *got;
d881513a 16808 bfd_vma *offp;
5bd4f169 16809 bfd_vma off;
d881513a 16810 unsigned long indx = 0;
927be08e 16811 struct got_entry *ent;
65f38f15 16812
d881513a 16813 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16814 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16815 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16816 else
5bd4f169 16817 {
d881513a
AM
16818 if (h != NULL)
16819 {
f0158f44
AM
16820 if (!htab->elf.dynamic_sections_created
16821 || h->elf.dynindx == -1
16822 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16823 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16824 /* This is actually a static link, or it is a
16825 -Bsymbolic link and the symbol is defined
16826 locally, or the symbol was forced to be local
16827 because of a version file. */
16828 ;
16829 else
16830 {
039b3fef 16831 indx = h->elf.dynindx;
0a1b45a2 16832 unresolved_reloc = false;
d881513a 16833 }
039b3fef 16834 ent = h->elf.got.glist;
d881513a 16835 }
411e1bfb 16836 else
5bd4f169 16837 {
d881513a
AM
16838 if (local_got_ents == NULL)
16839 abort ();
16840 ent = local_got_ents[r_symndx];
5bd4f169 16841 }
d881513a
AM
16842
16843 for (; ent != NULL; ent = ent->next)
133a1f60 16844 if (ent->addend == orig_rel.r_addend
e717da7e 16845 && ent->owner == input_bfd
d881513a
AM
16846 && ent->tls_type == tls_type)
16847 break;
5bd4f169 16848 }
411e1bfb 16849
927be08e
AM
16850 if (ent == NULL)
16851 abort ();
16852 if (ent->is_indirect)
16853 ent = ent->got.ent;
16854 offp = &ent->got.offset;
16855 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16856 if (got == NULL)
16857 abort ();
16858
411e1bfb
AM
16859 /* The offset must always be a multiple of 8. We use the
16860 least significant bit to record whether we have already
16861 processed this entry. */
d881513a 16862 off = *offp;
411e1bfb
AM
16863 if ((off & 1) != 0)
16864 off &= ~1;
5bd4f169
AM
16865 else
16866 {
411e1bfb
AM
16867 /* Generate relocs for the dynamic linker, except in
16868 the case of TLSLD where we'll use one entry per
16869 module. */
25f23106 16870 asection *relgot;
0a1b45a2 16871 bool ifunc;
e717da7e 16872
d881513a 16873 *offp = off | 1;
25f23106
AM
16874 relgot = NULL;
16875 ifunc = (h != NULL
16876 ? h->elf.type == STT_GNU_IFUNC
16877 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16878 if (ifunc)
82e66161
AM
16879 {
16880 relgot = htab->elf.irelplt;
efb2a7b4 16881 if (indx == 0 || is_static_defined (&h->elf))
0a1b45a2 16882 htab->elf.ifunc_resolvers = true;
82e66161 16883 }
f0158f44
AM
16884 else if (indx != 0
16885 || (bfd_link_pic (info)
16886 && (h == NULL
f749f26e
AM
16887 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16888 && !(tls_type != 0
f15d0b54 16889 && bfd_link_executable (info)
9e51d549
AM
16890 && (h == NULL
16891 || SYMBOL_REFERENCES_LOCAL (info,
3a3a4c1f
AM
16892 &h->elf)))
16893 && (h != NULL
16894 ? !bfd_is_abs_symbol (&h->elf.root)
16895 : sym->st_shndx != SHN_ABS)))
16896
19e08130 16897 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16898 if (relgot != NULL)
5bd4f169 16899 {
e717da7e
AM
16900 outrel.r_offset = (got->output_section->vma
16901 + got->output_offset
411e1bfb 16902 + off);
133a1f60 16903 outrel.r_addend = orig_rel.r_addend;
d881513a 16904 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16905 {
411e1bfb 16906 outrel.r_addend = 0;
e515b051 16907 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16908 if (tls_type == (TLS_TLS | TLS_GD))
16909 {
e717da7e
AM
16910 loc = relgot->contents;
16911 loc += (relgot->reloc_count++
d881513a
AM
16912 * sizeof (Elf64_External_Rela));
16913 bfd_elf64_swap_reloca_out (output_bfd,
16914 &outrel, loc);
e515b051 16915 outrel.r_offset += 8;
133a1f60 16916 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16917 outrel.r_info
16918 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16919 }
411e1bfb 16920 }
951fd09b 16921 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16922 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16923 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16924 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16925 else if (indx != 0)
16926 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16927 else
81407a69 16928 {
25f23106
AM
16929 if (ifunc)
16930 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16931 else
16932 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16933
16934 /* Write the .got section contents for the sake
16935 of prelink. */
e717da7e 16936 loc = got->contents + off;
23fbd6fa
JJ
16937 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16938 loc);
81407a69 16939 }
81407a69
AM
16940
16941 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16942 {
16943 outrel.r_addend += relocation;
16944 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16945 {
16946 if (htab->elf.tls_sec == NULL)
16947 outrel.r_addend = 0;
16948 else
16949 outrel.r_addend -= htab->elf.tls_sec->vma;
16950 }
e515b051 16951 }
1657026c
AM
16952 if (!(info->enable_dt_relr
16953 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
16954 {
16955 loc = relgot->contents;
16956 loc += (relgot->reloc_count++
16957 * sizeof (Elf64_External_Rela));
16958 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16959 }
411e1bfb
AM
16960 }
16961
ad8e1ba5 16962 /* Init the .got section contents here if we're not
81407a69 16963 emitting a reloc. */
d881513a 16964 else
411e1bfb 16965 {
133a1f60 16966 relocation += orig_rel.r_addend;
f0158f44 16967 if (tls_type != 0)
411e1bfb 16968 {
989f9879
AM
16969 if (htab->elf.tls_sec == NULL)
16970 relocation = 0;
16971 else
16972 {
f0158f44
AM
16973 if (tls_type & TLS_LD)
16974 relocation = 0;
16975 else
16976 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16977 if (tls_type & TLS_TPREL)
989f9879
AM
16978 relocation += DTP_OFFSET - TP_OFFSET;
16979 }
5bd4f169 16980
f0158f44 16981 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16982 {
16983 bfd_put_64 (output_bfd, relocation,
e717da7e 16984 got->contents + off + 8);
676ee2b5 16985 relocation = 1;
7b609f53 16986 }
411e1bfb
AM
16987 }
16988 bfd_put_64 (output_bfd, relocation,
e717da7e 16989 got->contents + off);
5bd4f169
AM
16990 }
16991 }
16992
65f38f15
AM
16993 if (off >= (bfd_vma) -2)
16994 abort ();
16995
bf102f86 16996 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16997 addend = 0;
c213164a 16998 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16999 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17000 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17001 || r_type == R_PPC64_GOT_TPREL_PCREL34
17002 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 17003 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 17004 }
65f38f15
AM
17005 break;
17006
17007 case R_PPC64_PLT16_HA:
17008 case R_PPC64_PLT16_HI:
17009 case R_PPC64_PLT16_LO:
08be3224 17010 case R_PPC64_PLT16_LO_DS:
5663e321
AM
17011 case R_PPC64_PLT_PCREL34:
17012 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
17013 case R_PPC64_PLT32:
17014 case R_PPC64_PLT64:
23cedd1d 17015 case R_PPC64_PLTSEQ:
5663e321 17016 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 17017 case R_PPC64_PLTCALL:
5663e321 17018 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
17019 /* Relocation is to the entry for this symbol in the
17020 procedure linkage table. */
0a1b45a2 17021 unresolved_reloc = true;
cbf95972
AM
17022 {
17023 struct plt_entry **plt_list = NULL;
17024 if (h != NULL)
17025 plt_list = &h->elf.plt.plist;
17026 else if (local_got_ents != NULL)
17027 {
17028 struct plt_entry **local_plt = (struct plt_entry **)
17029 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 17030 plt_list = local_plt + r_symndx;
cbf95972
AM
17031 }
17032 if (plt_list)
17033 {
17034 struct plt_entry *ent;
65f38f15 17035
cbf95972
AM
17036 for (ent = *plt_list; ent != NULL; ent = ent->next)
17037 if (ent->plt.offset != (bfd_vma) -1
133a1f60 17038 && ent->addend == orig_rel.r_addend)
cbf95972
AM
17039 {
17040 asection *plt;
08be3224 17041 bfd_vma got;
cbf95972
AM
17042
17043 plt = htab->elf.splt;
30845f11 17044 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
17045 {
17046 if (h != NULL
17047 ? h->elf.type == STT_GNU_IFUNC
17048 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17049 plt = htab->elf.iplt;
17050 else
17051 plt = htab->pltlocal;
17052 }
17053 relocation = (plt->output_section->vma
17054 + plt->output_offset
17055 + ent->plt.offset);
08be3224 17056 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
17057 || r_type == R_PPC64_PLT16_HI
17058 || r_type == R_PPC64_PLT16_LO
17059 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
17060 {
17061 got = (elf_gp (output_bfd)
17062 + htab->sec_info[input_section->id].toc_off);
17063 relocation -= got;
17064 }
133a1f60 17065 addend = 0;
0a1b45a2 17066 unresolved_reloc = false;
cbf95972
AM
17067 break;
17068 }
17069 }
17070 }
65f38f15 17071 break;
5bd4f169 17072
0b13192e
AM
17073 case R_PPC64_TOC:
17074 /* Relocation value is TOC base. */
17075 relocation = TOCstart;
cf35638d 17076 if (r_symndx == STN_UNDEF)
6f20ed8a 17077 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
17078 else if (unresolved_reloc)
17079 ;
6f20ed8a
AM
17080 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17081 relocation += htab->sec_info[sec->id].toc_off;
0b13192e 17082 else
0a1b45a2 17083 unresolved_reloc = true;
10040916
AM
17084 if (unresolved_reloc
17085 || (!is_opd
17086 && h != NULL
17087 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17088 info->callbacks->einfo
17089 /* xgettext:c-format */
17090 (_("%H: %s against %pT is not supported\n"),
17091 input_bfd, input_section, rel->r_offset,
17092 ppc64_elf_howto_table[r_type]->name, sym_name);
ab96bf03 17093 goto dodyn;
0b13192e 17094
5bd4f169
AM
17095 /* TOC16 relocs. We want the offset relative to the TOC base,
17096 which is the address of the start of the TOC plus 0x8000.
17097 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17098 in this order. */
65f38f15
AM
17099 case R_PPC64_TOC16:
17100 case R_PPC64_TOC16_LO:
17101 case R_PPC64_TOC16_HI:
17102 case R_PPC64_TOC16_DS:
17103 case R_PPC64_TOC16_LO_DS:
17104 case R_PPC64_TOC16_HA:
6f20ed8a 17105 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
17106 if (h != NULL)
17107 goto dodyn;
5bd4f169
AM
17108 break;
17109
17110 /* Relocate against the beginning of the section. */
65f38f15
AM
17111 case R_PPC64_SECTOFF:
17112 case R_PPC64_SECTOFF_LO:
17113 case R_PPC64_SECTOFF_HI:
17114 case R_PPC64_SECTOFF_DS:
17115 case R_PPC64_SECTOFF_LO_DS:
17116 case R_PPC64_SECTOFF_HA:
4ce794b7 17117 if (sec != NULL)
65f38f15 17118 addend -= sec->output_section->vma;
5bd4f169
AM
17119 break;
17120
25f23106
AM
17121 case R_PPC64_REL16:
17122 case R_PPC64_REL16_LO:
17123 case R_PPC64_REL16_HI:
17124 case R_PPC64_REL16_HA:
4a969973
AM
17125 case R_PPC64_REL16_HIGH:
17126 case R_PPC64_REL16_HIGHA:
17127 case R_PPC64_REL16_HIGHER:
17128 case R_PPC64_REL16_HIGHERA:
17129 case R_PPC64_REL16_HIGHEST:
17130 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
17131 case R_PPC64_REL16_HIGHER34:
17132 case R_PPC64_REL16_HIGHERA34:
17133 case R_PPC64_REL16_HIGHEST34:
17134 case R_PPC64_REL16_HIGHESTA34:
a680de9a 17135 case R_PPC64_REL16DX_HA:
721956f4
AM
17136 case R_PPC64_REL14:
17137 case R_PPC64_REL14_BRNTAKEN:
17138 case R_PPC64_REL14_BRTAKEN:
5d1634d7 17139 case R_PPC64_REL24:
05d0e962 17140 case R_PPC64_REL24_NOTOC:
7aba54da 17141 case R_PPC64_REL24_P9NOTOC:
5663e321
AM
17142 case R_PPC64_PCREL34:
17143 case R_PPC64_PCREL28:
5d1634d7
AM
17144 break;
17145
411e1bfb
AM
17146 case R_PPC64_TPREL16:
17147 case R_PPC64_TPREL16_LO:
17148 case R_PPC64_TPREL16_HI:
17149 case R_PPC64_TPREL16_HA:
17150 case R_PPC64_TPREL16_DS:
17151 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
17152 case R_PPC64_TPREL16_HIGH:
17153 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
17154 case R_PPC64_TPREL16_HIGHER:
17155 case R_PPC64_TPREL16_HIGHERA:
17156 case R_PPC64_TPREL16_HIGHEST:
17157 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
17158 if (h != NULL
17159 && h->elf.root.type == bfd_link_hash_undefweak
733ae98c
AM
17160 && h->elf.dynindx == -1
17161 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
766bc656
AM
17162 {
17163 /* Make this relocation against an undefined weak symbol
17164 resolve to zero. This is really just a tweak, since
17165 code using weak externs ought to check that they are
17166 defined before using them. */
17167 bfd_byte *p = contents + rel->r_offset - d_offset;
17168
95f0d0d2 17169 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
17170 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17171 if (insn != 0)
95f0d0d2 17172 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
17173 break;
17174 }
beaddc1a
AM
17175 /* Fall through. */
17176
17177 case R_PPC64_TPREL34:
989f9879
AM
17178 if (htab->elf.tls_sec != NULL)
17179 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
17180 /* The TPREL16 relocs shouldn't really be used in shared
17181 libs or with non-local symbols as that will result in
17182 DT_TEXTREL being set, but support them anyway. */
17183 goto dodyn;
411e1bfb
AM
17184
17185 case R_PPC64_DTPREL16:
17186 case R_PPC64_DTPREL16_LO:
17187 case R_PPC64_DTPREL16_HI:
17188 case R_PPC64_DTPREL16_HA:
17189 case R_PPC64_DTPREL16_DS:
17190 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
17191 case R_PPC64_DTPREL16_HIGH:
17192 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
17193 case R_PPC64_DTPREL16_HIGHER:
17194 case R_PPC64_DTPREL16_HIGHERA:
17195 case R_PPC64_DTPREL16_HIGHEST:
17196 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 17197 case R_PPC64_DTPREL34:
989f9879
AM
17198 if (htab->elf.tls_sec != NULL)
17199 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
17200 break;
17201
45965137
AM
17202 case R_PPC64_ADDR64_LOCAL:
17203 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17204 ? h->elf.other
17205 : sym->st_other);
17206 break;
17207
e515b051
AM
17208 case R_PPC64_DTPMOD64:
17209 relocation = 1;
17210 addend = 0;
17211 goto dodyn;
17212
411e1bfb 17213 case R_PPC64_TPREL64:
989f9879
AM
17214 if (htab->elf.tls_sec != NULL)
17215 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
17216 goto dodyn;
17217
17218 case R_PPC64_DTPREL64:
989f9879
AM
17219 if (htab->elf.tls_sec != NULL)
17220 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 17221 /* Fall through. */
411e1bfb 17222
65f38f15
AM
17223 /* Relocations that may need to be propagated if this is a
17224 dynamic object. */
04c9666a 17225 case R_PPC64_REL30:
65f38f15
AM
17226 case R_PPC64_REL32:
17227 case R_PPC64_REL64:
17228 case R_PPC64_ADDR14:
17229 case R_PPC64_ADDR14_BRNTAKEN:
17230 case R_PPC64_ADDR14_BRTAKEN:
17231 case R_PPC64_ADDR16:
17232 case R_PPC64_ADDR16_DS:
17233 case R_PPC64_ADDR16_HA:
17234 case R_PPC64_ADDR16_HI:
f9c6b907
AM
17235 case R_PPC64_ADDR16_HIGH:
17236 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17237 case R_PPC64_ADDR16_HIGHER:
17238 case R_PPC64_ADDR16_HIGHERA:
17239 case R_PPC64_ADDR16_HIGHEST:
17240 case R_PPC64_ADDR16_HIGHESTA:
17241 case R_PPC64_ADDR16_LO:
17242 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
17243 case R_PPC64_ADDR16_HIGHER34:
17244 case R_PPC64_ADDR16_HIGHERA34:
17245 case R_PPC64_ADDR16_HIGHEST34:
17246 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 17247 case R_PPC64_ADDR24:
65f38f15
AM
17248 case R_PPC64_ADDR32:
17249 case R_PPC64_ADDR64:
17250 case R_PPC64_UADDR16:
17251 case R_PPC64_UADDR32:
17252 case R_PPC64_UADDR64:
5663e321
AM
17253 case R_PPC64_D34:
17254 case R_PPC64_D34_LO:
17255 case R_PPC64_D34_HI30:
17256 case R_PPC64_D34_HA30:
17257 case R_PPC64_D28:
411e1bfb 17258 dodyn:
5d1634d7 17259 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
17260 break;
17261
41bd81ab
AM
17262 if (NO_OPD_RELOCS && is_opd)
17263 break;
17264
8a9e8e72 17265 if (bfd_link_pic (info)
b1b07054 17266 ? ((h == NULL
190eb1dd 17267 || h->elf.dyn_relocs != NULL)
b1b07054
AM
17268 && ((h != NULL && pc_dynrelocs (h))
17269 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 17270 : (h != NULL
190eb1dd 17271 ? h->elf.dyn_relocs != NULL
d311bc8b 17272 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 17273 {
0a1b45a2 17274 bool skip, relocate;
65f38f15 17275 asection *sreloc;
1cf1f670 17276 bfd_vma out_off;
82e66161 17277 long indx = 0;
65f38f15
AM
17278
17279 /* When generating a dynamic object, these relocations
17280 are copied into the output file to be resolved at run
17281 time. */
17282
0a1b45a2
AM
17283 skip = false;
17284 relocate = false;
65f38f15 17285
1cf1f670
AM
17286 out_off = _bfd_elf_section_offset (output_bfd, info,
17287 input_section, rel->r_offset);
17288 if (out_off == (bfd_vma) -1)
0a1b45a2 17289 skip = true;
1cf1f670 17290 else if (out_off == (bfd_vma) -2)
0a1b45a2 17291 skip = true, relocate = true;
1cf1f670
AM
17292 out_off += (input_section->output_section->vma
17293 + input_section->output_offset);
17294 outrel.r_offset = out_off;
411e1bfb 17295 outrel.r_addend = rel->r_addend;
65f38f15 17296
1cf1f670
AM
17297 /* Optimize unaligned reloc use. */
17298 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17299 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17300 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17301 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17302 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17303 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17304 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17305 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17306 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17307
65f38f15 17308 if (skip)
0bb2d96a 17309 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
17310 else if (h != NULL
17311 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
17312 && !is_opd
17313 && r_type != R_PPC64_TOC)
14acf4dc 17314 {
82e66161
AM
17315 indx = h->elf.dynindx;
17316 BFD_ASSERT (indx != -1);
17317 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 17318 }
65f38f15
AM
17319 else
17320 {
41bd81ab
AM
17321 /* This symbol is local, or marked to become local,
17322 or this is an opd section reloc which must point
17323 at a local function. */
65f38f15 17324 outrel.r_addend += relocation;
e86ce104 17325 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 17326 {
3fad3c7c 17327 if (is_opd && h != NULL)
afbe61cf
AM
17328 {
17329 /* Lie about opd entries. This case occurs
17330 when building shared libraries and we
17331 reference a function in another shared
3fad3c7c
AM
17332 lib. The same thing happens for a weak
17333 definition in an application that's
17334 overridden by a strong definition in a
17335 shared lib. (I believe this is a generic
17336 bug in binutils handling of weak syms.)
17337 In these cases we won't use the opd
1e2f5b6e 17338 entry in this lib. */
0a1b45a2 17339 unresolved_reloc = false;
afbe61cf 17340 }
25f23106
AM
17341 if (!is_opd
17342 && r_type == R_PPC64_ADDR64
17343 && (h != NULL
17344 ? h->elf.type == STT_GNU_IFUNC
17345 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17346 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17347 else
17348 {
17349 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 17350
25f23106
AM
17351 /* We need to relocate .opd contents for ld.so.
17352 Prelink also wants simple and consistent rules
17353 for relocs. This make all RELATIVE relocs have
17354 *r_offset equal to r_addend. */
0a1b45a2 17355 relocate = true;
25f23106 17356 }
65f38f15
AM
17357 }
17358 else
17359 {
25f23106
AM
17360 if (h != NULL
17361 ? h->elf.type == STT_GNU_IFUNC
17362 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17363 {
25f53a85 17364 info->callbacks->einfo
695344c0 17365 /* xgettext:c-format */
174d0a74 17366 (_("%H: %s for indirect "
c1c8c1ef 17367 "function `%pT' unsupported\n"),
25f53a85 17368 input_bfd, input_section, rel->r_offset,
25f23106
AM
17369 ppc64_elf_howto_table[r_type]->name,
17370 sym_name);
0a1b45a2 17371 ret = false;
25f23106 17372 }
cf35638d 17373 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
17374 ;
17375 else if (sec == NULL || sec->owner == NULL)
17376 {
17377 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17378 return false;
65f38f15
AM
17379 }
17380 else
17381 {
f26a3287 17382 asection *osec = sec->output_section;
65f38f15 17383
f26a3287
AM
17384 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17385 {
17386 /* TLS symbol values are relative to the
17387 TLS segment. Dynamic relocations for
17388 local TLS symbols therefore can't be
17389 reduced to a relocation against their
17390 section symbol because it holds the
17391 address of the section, not a value
17392 relative to the TLS segment. We could
17393 change the .tdata dynamic section symbol
17394 to be zero value but STN_UNDEF works
17395 and is used elsewhere, eg. for TPREL64
17396 GOT relocs against local TLS symbols. */
17397 osec = htab->elf.tls_sec;
17398 indx = 0;
17399 }
17400 else
74541ad4 17401 {
74541ad4 17402 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
17403 if (indx == 0)
17404 {
17405 if ((osec->flags & SEC_READONLY) == 0
17406 && htab->elf.data_index_section != NULL)
17407 osec = htab->elf.data_index_section;
17408 else
17409 osec = htab->elf.text_index_section;
17410 indx = elf_section_data (osec)->dynindx;
17411 }
17412 BFD_ASSERT (indx != 0);
74541ad4 17413 }
74541ad4 17414
65f38f15
AM
17415 /* We are turning this relocation into one
17416 against a section symbol, so subtract out
17417 the output section's address but not the
17418 offset of the input section in the output
17419 section. */
17420 outrel.r_addend -= osec->vma;
17421 }
17422
17423 outrel.r_info = ELF64_R_INFO (indx, r_type);
17424 }
17425 }
17426
1657026c
AM
17427 if (!(info->enable_dt_relr
17428 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17429 && rel->r_offset % 2 == 0
17430 && input_section->alignment_power != 0
17431 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
82e66161 17432 {
1657026c
AM
17433 sreloc = elf_section_data (input_section)->sreloc;
17434 if (h != NULL
17435 ? h->elf.type == STT_GNU_IFUNC
17436 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17437 {
17438 sreloc = htab->elf.irelplt;
17439 if (indx == 0 || is_static_defined (&h->elf))
17440 htab->elf.ifunc_resolvers = true;
17441 }
17442 if (sreloc == NULL)
17443 abort ();
17444
17445 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17446 >= sreloc->size)
17447 abort ();
17448 loc = sreloc->contents;
17449 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17450 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
82e66161 17451 }
65f38f15 17452
e59a1001
AM
17453 if (!warned_dynamic
17454 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17455 {
17456 info->callbacks->einfo
17457 /* xgettext:c-format */
17458 (_("%X%P: %pB: %s against %pT "
17459 "is not supported by glibc as a dynamic relocation\n"),
17460 input_bfd,
17461 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17462 sym_name);
0a1b45a2 17463 warned_dynamic = true;
e59a1001
AM
17464 }
17465
65f38f15
AM
17466 /* If this reloc is against an external symbol, it will
17467 be computed at runtime, so there's no need to do
81407a69
AM
17468 anything now. However, for the sake of prelink ensure
17469 that the section contents are a known value. */
2cdcc330 17470 if (!relocate)
81407a69 17471 {
0a1b45a2 17472 unresolved_reloc = false;
81407a69
AM
17473 /* The value chosen here is quite arbitrary as ld.so
17474 ignores section contents except for the special
17475 case of .opd where the contents might be accessed
17476 before relocation. Choose zero, as that won't
17477 cause reloc overflow. */
17478 relocation = 0;
17479 addend = 0;
17480 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17481 to improve backward compatibility with older
17482 versions of ld. */
17483 if (r_type == R_PPC64_ADDR64)
17484 addend = outrel.r_addend;
17485 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 17486 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 17487 addend = outrel.r_offset;
81407a69 17488 }
65f38f15 17489 }
5bd4f169
AM
17490 break;
17491
65f38f15
AM
17492 case R_PPC64_COPY:
17493 case R_PPC64_GLOB_DAT:
17494 case R_PPC64_JMP_SLOT:
25f23106 17495 case R_PPC64_JMP_IREL:
65f38f15
AM
17496 case R_PPC64_RELATIVE:
17497 /* We shouldn't ever see these dynamic relocs in relocatable
17498 files. */
ae9a127f 17499 /* Fall through. */
65f38f15
AM
17500
17501 case R_PPC64_PLTGOT16:
17502 case R_PPC64_PLTGOT16_DS:
17503 case R_PPC64_PLTGOT16_HA:
17504 case R_PPC64_PLTGOT16_HI:
17505 case R_PPC64_PLTGOT16_LO:
17506 case R_PPC64_PLTGOT16_LO_DS:
17507 case R_PPC64_PLTREL32:
17508 case R_PPC64_PLTREL64:
17509 /* These ones haven't been implemented yet. */
17510
25f53a85 17511 info->callbacks->einfo
695344c0 17512 /* xgettext:c-format */
c1c8c1ef 17513 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 17514 input_bfd,
4ce794b7 17515 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
17516
17517 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 17518 ret = false;
c316a17c 17519 goto copy_reloc;
65f38f15 17520 }
5bd4f169 17521
67f0cbdb
AM
17522 /* Multi-instruction sequences that access the TOC can be
17523 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 17524 to nop; addi rb,r2,x; */
67f0cbdb
AM
17525 switch (r_type)
17526 {
17527 default:
17528 break;
17529
17530 case R_PPC64_GOT_TLSLD16_HI:
17531 case R_PPC64_GOT_TLSGD16_HI:
17532 case R_PPC64_GOT_TPREL16_HI:
17533 case R_PPC64_GOT_DTPREL16_HI:
17534 case R_PPC64_GOT16_HI:
17535 case R_PPC64_TOC16_HI:
17536 /* These relocs would only be useful if building up an
17537 offset to later add to r2, perhaps in an indexed
17538 addressing mode instruction. Don't try to optimize.
17539 Unfortunately, the possibility of someone building up an
17540 offset like this or even with the HA relocs, means that
17541 we need to check the high insn when optimizing the low
17542 insn. */
17543 break;
17544
5663e321
AM
17545 case R_PPC64_PLTCALL_NOTOC:
17546 if (!unresolved_reloc)
17547 htab->notoc_plt = 1;
17548 /* Fall through. */
23cedd1d 17549 case R_PPC64_PLTCALL:
733ae98c
AM
17550 if (unresolved_reloc
17551 && offset_in_range (input_section, rel->r_offset,
17552 r_type == R_PPC64_PLTCALL ? 8 : 4))
23cedd1d
AM
17553 {
17554 /* No plt entry. Make this into a direct call. */
17555 bfd_byte *p = contents + rel->r_offset;
17556 insn = bfd_get_32 (input_bfd, p);
17557 insn &= 1;
17558 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
17559 if (r_type == R_PPC64_PLTCALL)
17560 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
17561 unresolved_reloc = save_unresolved_reloc;
17562 r_type = R_PPC64_REL24;
17563 }
17564 break;
17565
5663e321 17566 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17567 case R_PPC64_PLTSEQ:
17568 if (unresolved_reloc)
17569 {
0a1b45a2 17570 unresolved_reloc = false;
23cedd1d
AM
17571 goto nop_it;
17572 }
17573 break;
17574
5663e321
AM
17575 case R_PPC64_PLT_PCREL34_NOTOC:
17576 if (!unresolved_reloc)
17577 htab->notoc_plt = 1;
17578 /* Fall through. */
17579 case R_PPC64_PLT_PCREL34:
733ae98c
AM
17580 if (unresolved_reloc
17581 && offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17582 {
17583 bfd_byte *p = contents + rel->r_offset;
17584 bfd_put_32 (input_bfd, PNOP >> 32, p);
17585 bfd_put_32 (input_bfd, PNOP, p + 4);
0a1b45a2 17586 unresolved_reloc = false;
5663e321
AM
17587 goto copy_reloc;
17588 }
17589 break;
17590
23cedd1d
AM
17591 case R_PPC64_PLT16_HA:
17592 if (unresolved_reloc)
17593 {
0a1b45a2 17594 unresolved_reloc = false;
23cedd1d
AM
17595 goto nop_it;
17596 }
17597 /* Fall through. */
67f0cbdb
AM
17598 case R_PPC64_GOT_TLSLD16_HA:
17599 case R_PPC64_GOT_TLSGD16_HA:
17600 case R_PPC64_GOT_TPREL16_HA:
17601 case R_PPC64_GOT_DTPREL16_HA:
17602 case R_PPC64_GOT16_HA:
17603 case R_PPC64_TOC16_HA:
98528052 17604 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
3a3a4c1f
AM
17605 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17606 && !(bfd_link_pic (info)
17607 && (h != NULL
17608 ? bfd_is_abs_symbol (&h->elf.root)
17609 : sec == bfd_abs_section_ptr)))
98528052 17610 {
23cedd1d
AM
17611 bfd_byte *p;
17612 nop_it:
733ae98c
AM
17613 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17614 {
17615 p = contents + (rel->r_offset & ~3);
17616 bfd_put_32 (input_bfd, NOP, p);
17617 goto copy_reloc;
17618 }
98528052 17619 }
67f0cbdb
AM
17620 break;
17621
23cedd1d
AM
17622 case R_PPC64_PLT16_LO:
17623 case R_PPC64_PLT16_LO_DS:
17624 if (unresolved_reloc)
17625 {
0a1b45a2 17626 unresolved_reloc = false;
23cedd1d
AM
17627 goto nop_it;
17628 }
17629 /* Fall through. */
67f0cbdb
AM
17630 case R_PPC64_GOT_TLSLD16_LO:
17631 case R_PPC64_GOT_TLSGD16_LO:
17632 case R_PPC64_GOT_TPREL16_LO_DS:
17633 case R_PPC64_GOT_DTPREL16_LO_DS:
17634 case R_PPC64_GOT16_LO:
17635 case R_PPC64_GOT16_LO_DS:
17636 case R_PPC64_TOC16_LO:
17637 case R_PPC64_TOC16_LO_DS:
98528052 17638 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
733ae98c 17639 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
3a3a4c1f
AM
17640 && !(bfd_link_pic (info)
17641 && (h != NULL
17642 ? bfd_is_abs_symbol (&h->elf.root)
17643 : sec == bfd_abs_section_ptr))
733ae98c 17644 && offset_in_range (input_section, rel->r_offset & ~3, 4))
67f0cbdb
AM
17645 {
17646 bfd_byte *p = contents + (rel->r_offset & ~3);
17647 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17648 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17649 {
17650 /* Transform addic to addi when we change reg. */
2365f8d7 17651 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17652 insn |= (14u << 26) | (2 << 16);
17653 }
17654 else
67f0cbdb 17655 {
98528052
AM
17656 insn &= ~(0x1f << 16);
17657 insn |= 2 << 16;
67f0cbdb 17658 }
560c8763 17659 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17660 }
17661 break;
9a23f96e
AM
17662
17663 case R_PPC64_TPREL16_HA:
733ae98c
AM
17664 if (htab->do_tls_opt
17665 && relocation + addend + 0x8000 < 0x10000
17666 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17667 {
17668 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17669 bfd_put_32 (input_bfd, NOP, p);
17670 goto copy_reloc;
9a23f96e
AM
17671 }
17672 break;
17673
17674 case R_PPC64_TPREL16_LO:
17675 case R_PPC64_TPREL16_LO_DS:
733ae98c
AM
17676 if (htab->do_tls_opt
17677 && relocation + addend + 0x8000 < 0x10000
17678 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17679 {
17680 bfd_byte *p = contents + (rel->r_offset & ~3);
17681 insn = bfd_get_32 (input_bfd, p);
17682 insn &= ~(0x1f << 16);
17683 insn |= 13 << 16;
17684 bfd_put_32 (input_bfd, insn, p);
17685 }
17686 break;
67f0cbdb
AM
17687 }
17688
65f38f15
AM
17689 /* Do any further special processing. */
17690 switch (r_type)
17691 {
17692 default:
17693 break;
17694
25f23106 17695 case R_PPC64_REL16_HA:
4a969973
AM
17696 case R_PPC64_REL16_HIGHA:
17697 case R_PPC64_REL16_HIGHERA:
17698 case R_PPC64_REL16_HIGHESTA:
a680de9a 17699 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17700 case R_PPC64_ADDR16_HA:
17701 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17702 case R_PPC64_ADDR16_HIGHERA:
17703 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17704 case R_PPC64_TOC16_HA:
17705 case R_PPC64_SECTOFF_HA:
411e1bfb 17706 case R_PPC64_TPREL16_HA:
f9c6b907 17707 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17708 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17709 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17710 case R_PPC64_DTPREL16_HA:
17711 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17712 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17713 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17714 /* It's just possible that this symbol is a weak symbol
17715 that's not actually defined anywhere. In that case,
17716 'sec' would be NULL, and we should leave the symbol
17717 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17718 if (sec == NULL)
17719 break;
1a0670f3 17720 /* Fall through. */
5c5f6e17
AM
17721
17722 case R_PPC64_GOT16_HA:
17723 case R_PPC64_PLTGOT16_HA:
17724 case R_PPC64_PLT16_HA:
17725 case R_PPC64_GOT_TLSGD16_HA:
17726 case R_PPC64_GOT_TLSLD16_HA:
17727 case R_PPC64_GOT_TPREL16_HA:
17728 case R_PPC64_GOT_DTPREL16_HA:
17729 /* Add 0x10000 if sign bit in 0:15 is set.
17730 Bits 0:15 are not used. */
17731 addend += 0x8000;
65f38f15
AM
17732 break;
17733
5663e321
AM
17734 case R_PPC64_D34_HA30:
17735 case R_PPC64_ADDR16_HIGHERA34:
17736 case R_PPC64_ADDR16_HIGHESTA34:
17737 case R_PPC64_REL16_HIGHERA34:
17738 case R_PPC64_REL16_HIGHESTA34:
17739 if (sec != NULL)
17740 addend += 1ULL << 33;
17741 break;
17742
65f38f15
AM
17743 case R_PPC64_ADDR16_DS:
17744 case R_PPC64_ADDR16_LO_DS:
17745 case R_PPC64_GOT16_DS:
17746 case R_PPC64_GOT16_LO_DS:
17747 case R_PPC64_PLT16_LO_DS:
17748 case R_PPC64_SECTOFF_DS:
17749 case R_PPC64_SECTOFF_LO_DS:
17750 case R_PPC64_TOC16_DS:
17751 case R_PPC64_TOC16_LO_DS:
17752 case R_PPC64_PLTGOT16_DS:
17753 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17754 case R_PPC64_GOT_TPREL16_DS:
17755 case R_PPC64_GOT_TPREL16_LO_DS:
17756 case R_PPC64_GOT_DTPREL16_DS:
17757 case R_PPC64_GOT_DTPREL16_LO_DS:
17758 case R_PPC64_TPREL16_DS:
17759 case R_PPC64_TPREL16_LO_DS:
17760 case R_PPC64_DTPREL16_DS:
17761 case R_PPC64_DTPREL16_LO_DS:
733ae98c
AM
17762 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17763 break;
adadcc0c
AM
17764 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17765 mask = 3;
a680de9a
PB
17766 /* If this reloc is against an lq, lxv, or stxv insn, then
17767 the value must be a multiple of 16. This is somewhat of
17768 a hack, but the "correct" way to do this by defining _DQ
17769 forms of all the _DS relocs bloats all reloc switches in
17770 this file. It doesn't make much sense to use these
17771 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17772 if ((insn & (0x3fu << 26)) == (56u << 26)
17773 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17774 mask = 15;
a680de9a
PB
17775 relocation += addend;
17776 addend = insn & (mask ^ 3);
17777 if ((relocation & mask) != 0)
65f38f15 17778 {
a680de9a 17779 relocation ^= relocation & mask;
25f53a85 17780 info->callbacks->einfo
695344c0 17781 /* xgettext:c-format */
174d0a74 17782 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17783 input_bfd, input_section, rel->r_offset,
d830549d 17784 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17785 mask + 1);
65f38f15 17786 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17787 ret = false;
c316a17c 17788 goto copy_reloc;
65f38f15
AM
17789 }
17790 break;
5bd4f169
AM
17791 }
17792
239e1f3a
AM
17793 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17794 because such sections are not SEC_ALLOC and thus ld.so will
17795 not process them. */
d830549d 17796 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17797 if (unresolved_reloc
239e1f3a 17798 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17799 && h->elf.def_dynamic)
17800 && _bfd_elf_section_offset (output_bfd, info, input_section,
17801 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17802 {
25f53a85 17803 info->callbacks->einfo
695344c0 17804 /* xgettext:c-format */
c1c8c1ef 17805 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17806 input_bfd, input_section, rel->r_offset,
b80eed39 17807 howto->name,
039b3fef 17808 h->elf.root.root.string);
0a1b45a2 17809 ret = false;
9c07fe7c 17810 }
5bd4f169 17811
b80eed39
AM
17812 /* 16-bit fields in insns mostly have signed values, but a
17813 few insns have 16-bit unsigned values. Really, we should
17814 have different reloc types. */
17815 if (howto->complain_on_overflow != complain_overflow_dont
17816 && howto->dst_mask == 0xffff
733ae98c
AM
17817 && (input_section->flags & SEC_CODE) != 0
17818 && offset_in_range (input_section, rel->r_offset & ~3, 4))
b80eed39
AM
17819 {
17820 enum complain_overflow complain = complain_overflow_signed;
17821
17822 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17823 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17824 complain = complain_overflow_bitfield;
17825 else if (howto->rightshift == 0
2365f8d7
AM
17826 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17827 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17828 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17829 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17830 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17831 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17832 complain = complain_overflow_unsigned;
17833 if (howto->complain_on_overflow != complain)
17834 {
17835 alt_howto = *howto;
17836 alt_howto.complain_on_overflow = complain;
17837 howto = &alt_howto;
17838 }
17839 }
17840
5663e321 17841 switch (r_type)
a680de9a 17842 {
5663e321
AM
17843 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17844 case R_PPC64_D34:
17845 case R_PPC64_D34_LO:
17846 case R_PPC64_D34_HI30:
17847 case R_PPC64_D34_HA30:
17848 case R_PPC64_PCREL34:
17849 case R_PPC64_GOT_PCREL34:
c213164a
AM
17850 case R_PPC64_TPREL34:
17851 case R_PPC64_DTPREL34:
87c69f97
AM
17852 case R_PPC64_GOT_TLSGD_PCREL34:
17853 case R_PPC64_GOT_TLSLD_PCREL34:
17854 case R_PPC64_GOT_TPREL_PCREL34:
17855 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17856 case R_PPC64_PLT_PCREL34:
17857 case R_PPC64_PLT_PCREL34_NOTOC:
17858 case R_PPC64_D28:
17859 case R_PPC64_PCREL28:
733ae98c 17860 if (!offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17861 r = bfd_reloc_outofrange;
17862 else
17863 {
5663e321
AM
17864 relocation += addend;
17865 if (howto->pc_relative)
17866 relocation -= (rel->r_offset
17867 + input_section->output_offset
17868 + input_section->output_section->vma);
17869 relocation >>= howto->rightshift;
17870
17871 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17872 pinsn <<= 32;
17873 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17874
17875 pinsn &= ~howto->dst_mask;
17876 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17877 & howto->dst_mask);
17878 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17879 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17880 r = bfd_reloc_ok;
17881 if (howto->complain_on_overflow == complain_overflow_signed
17882 && (relocation + (1ULL << (howto->bitsize - 1))
17883 >= 1ULL << howto->bitsize))
17884 r = bfd_reloc_overflow;
17885 }
17886 break;
17887
17888 case R_PPC64_REL16DX_HA:
733ae98c 17889 if (!offset_in_range (input_section, rel->r_offset, 4))
a680de9a
PB
17890 r = bfd_reloc_outofrange;
17891 else
17892 {
17893 relocation += addend;
17894 relocation -= (rel->r_offset
17895 + input_section->output_offset
17896 + input_section->output_section->vma);
3de43e7b 17897 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17898 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17899 insn &= ~0x1fffc1;
3de43e7b 17900 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17901 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17902 r = bfd_reloc_ok;
3de43e7b 17903 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17904 r = bfd_reloc_overflow;
17905 }
5663e321
AM
17906 break;
17907
17908 default:
17909 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17910 contents, rel->r_offset,
17911 relocation, addend);
a680de9a 17912 }
5bd4f169 17913
ef60b7ff 17914 if (r != bfd_reloc_ok)
5bd4f169 17915 {
bc30df16 17916 char *more_info = NULL;
b80eed39 17917 const char *reloc_name = howto->name;
bc30df16
AM
17918
17919 if (reloc_dest != DEST_NORMAL)
17920 {
17921 more_info = bfd_malloc (strlen (reloc_name) + 8);
17922 if (more_info != NULL)
17923 {
17924 strcpy (more_info, reloc_name);
17925 strcat (more_info, (reloc_dest == DEST_OPD
17926 ? " (OPD)" : " (stub)"));
17927 reloc_name = more_info;
17928 }
17929 }
17930
cd27b276 17931 if (r == bfd_reloc_overflow)
5bd4f169 17932 {
8131c122
AM
17933 /* On code like "if (foo) foo();" don't report overflow
17934 on a branch to zero when foo is undefined. */
17935 if (!warned
17936 && (reloc_dest == DEST_STUB
17937 || !(h != NULL
17938 && (h->elf.root.type == bfd_link_hash_undefweak
17939 || h->elf.root.type == bfd_link_hash_undefined)
17940 && is_branch_reloc (r_type))))
9e51d549
AM
17941 info->callbacks->reloc_overflow
17942 (info, (struct bfd_link_hash_entry *) h, sym_name,
17943 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17944 rel->r_offset);
ef60b7ff
AM
17945 }
17946 else
17947 {
25f53a85 17948 info->callbacks->einfo
695344c0 17949 /* xgettext:c-format */
c1c8c1ef 17950 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17951 input_bfd, input_section, rel->r_offset,
bc30df16 17952 reloc_name, sym_name, (int) r);
0a1b45a2 17953 ret = false;
ef60b7ff 17954 }
c9594989 17955 free (more_info);
5bd4f169 17956 }
c316a17c
AM
17957 copy_reloc:
17958 if (wrel != rel)
17959 *wrel = *rel;
17960 }
17961
17962 if (wrel != rel)
17963 {
17964 Elf_Internal_Shdr *rel_hdr;
17965 size_t deleted = rel - wrel;
17966
17967 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17968 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17969 if (rel_hdr->sh_size == 0)
17970 {
17971 /* It is too late to remove an empty reloc section. Leave
17972 one NONE reloc.
17973 ??? What is wrong with an empty section??? */
17974 rel_hdr->sh_size = rel_hdr->sh_entsize;
17975 deleted -= 1;
17976 }
17977 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17978 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17979 input_section->reloc_count -= deleted;
5bd4f169
AM
17980 }
17981
645ea6a9
AM
17982 /* If we're emitting relocations, then shortly after this function
17983 returns, reloc offsets and addends for this section will be
17984 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17985 file rather than the input. Save a copy of the relocs for
17986 opd_entry_value. */
0e1862bb 17987 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17988 {
17989 bfd_size_type amt;
17990 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17991 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17992 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17993 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f 17994 if (rel == NULL)
0a1b45a2 17995 return false;
8860955f
AM
17996 memcpy (rel, relocs, amt);
17997 }
5bd4f169
AM
17998 return ret;
17999}
18000
754021d0
AM
18001/* Adjust the value of any local symbols in opd sections. */
18002
6e0b88f1 18003static int
754021d0
AM
18004ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18005 const char *name ATTRIBUTE_UNUSED,
18006 Elf_Internal_Sym *elfsym,
18007 asection *input_sec,
18008 struct elf_link_hash_entry *h)
18009{
74f0fb50
AM
18010 struct _opd_sec_data *opd;
18011 long adjust;
754021d0
AM
18012 bfd_vma value;
18013
4025353c 18014 if (h != NULL)
6e0b88f1 18015 return 1;
4025353c 18016
74f0fb50
AM
18017 opd = get_opd_info (input_sec);
18018 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 18019 return 1;
754021d0
AM
18020
18021 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 18022 if (!bfd_link_relocatable (info))
754021d0
AM
18023 value -= input_sec->output_section->vma;
18024
51aecdc5 18025 adjust = opd->adjust[OPD_NDX (value)];
4025353c 18026 if (adjust == -1)
6e0b88f1
AM
18027 return 2;
18028
18029 elfsym->st_value += adjust;
18030 return 1;
754021d0
AM
18031}
18032
5bd4f169
AM
18033/* Finish up dynamic symbol handling. We set the contents of various
18034 dynamic sections here. */
18035
0a1b45a2 18036static bool
4ce794b7
AM
18037ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18038 struct bfd_link_info *info,
18039 struct elf_link_hash_entry *h,
4aef7643 18040 Elf_Internal_Sym *sym)
5bd4f169 18041{
65f38f15 18042 struct ppc_link_hash_table *htab;
8387904d 18043 struct plt_entry *ent;
5bd4f169 18044
65f38f15 18045 htab = ppc_hash_table (info);
4dfe6ac6 18046 if (htab == NULL)
0a1b45a2 18047 return false;
5bd4f169 18048
49c09209
AM
18049 if (!htab->opd_abi && !h->def_regular)
18050 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18051 if (ent->plt.offset != (bfd_vma) -1)
18052 {
18053 /* Mark the symbol as undefined, rather than as
18054 defined in glink. Leave the value if there were
18055 any relocations where pointer equality matters
18056 (this is a clue for the dynamic linker, to make
18057 function pointer comparisons work between an
18058 application and shared library), otherwise set it
18059 to zero. */
18060 sym->st_shndx = SHN_UNDEF;
18061 if (!h->pointer_equality_needed)
18062 sym->st_value = 0;
18063 else if (!h->ref_regular_nonweak)
18064 {
18065 /* This breaks function pointer comparisons, but
18066 that is better than breaking tests for a NULL
18067 function pointer. */
18068 sym->st_value = 0;
18069 }
18070 break;
18071 }
5bd4f169 18072
1bdd8fac
AM
18073 if (h->needs_copy
18074 && (h->root.type == bfd_link_hash_defined
18075 || h->root.type == bfd_link_hash_defweak)
18076 && (h->root.u.def.section == htab->elf.sdynbss
18077 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 18078 {
65f38f15 18079 /* This symbol needs a copy reloc. Set it up. */
49c09209 18080 Elf_Internal_Rela rela;
5474d94f 18081 asection *srel;
49c09209 18082 bfd_byte *loc;
5bd4f169 18083
1bdd8fac 18084 if (h->dynindx == -1)
65f38f15 18085 abort ();
5bd4f169 18086
ed7007c1 18087 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
18088 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18089 rela.r_addend = 0;
afbf7e8e 18090 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
18091 srel = htab->elf.sreldynrelro;
18092 else
18093 srel = htab->elf.srelbss;
18094 loc = srel->contents;
18095 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 18096 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
18097 }
18098
0a1b45a2 18099 return true;
5bd4f169
AM
18100}
18101
65f38f15
AM
18102/* Used to decide how to sort relocs in an optimal manner for the
18103 dynamic linker, before writing them out. */
18104
18105static enum elf_reloc_type_class
7e612e98
AM
18106ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18107 const asection *rel_sec,
18108 const Elf_Internal_Rela *rela)
65f38f15 18109{
04c9666a 18110 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
18111 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18112
33e44f2e 18113 if (rel_sec == htab->elf.irelplt)
7e612e98 18114 return reloc_class_ifunc;
a33d1f77 18115
4ce794b7 18116 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 18117 switch (r_type)
65f38f15
AM
18118 {
18119 case R_PPC64_RELATIVE:
18120 return reloc_class_relative;
18121 case R_PPC64_JMP_SLOT:
18122 return reloc_class_plt;
18123 case R_PPC64_COPY:
18124 return reloc_class_copy;
18125 default:
18126 return reloc_class_normal;
18127 }
18128}
18129
5bd4f169
AM
18130/* Finish up the dynamic sections. */
18131
0a1b45a2 18132static bool
4ce794b7
AM
18133ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18134 struct bfd_link_info *info)
5bd4f169 18135{
65f38f15
AM
18136 struct ppc_link_hash_table *htab;
18137 bfd *dynobj;
5bd4f169 18138 asection *sdyn;
5bd4f169 18139
65f38f15 18140 htab = ppc_hash_table (info);
4dfe6ac6 18141 if (htab == NULL)
0a1b45a2 18142 return false;
4dfe6ac6 18143
65f38f15 18144 dynobj = htab->elf.dynobj;
3d4d4302 18145 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 18146
65f38f15 18147 if (htab->elf.dynamic_sections_created)
5bd4f169 18148 {
5bd4f169
AM
18149 Elf64_External_Dyn *dyncon, *dynconend;
18150
33e44f2e 18151 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 18152 abort ();
5bd4f169
AM
18153
18154 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 18155 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
18156 for (; dyncon < dynconend; dyncon++)
18157 {
18158 Elf_Internal_Dyn dyn;
19397422 18159 asection *s;
5bd4f169
AM
18160
18161 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18162
18163 switch (dyn.d_tag)
18164 {
65f38f15
AM
18165 default:
18166 continue;
5bd4f169 18167
5d1634d7 18168 case DT_PPC64_GLINK:
4ce794b7 18169 s = htab->glink;
6348e046 18170 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
18171 /* We stupidly defined DT_PPC64_GLINK to be the start
18172 of glink rather than the first entry point, which is
18173 what ld.so needs, and now have a bigger stub to
18174 support automatic multiple TOCs. */
9e390558 18175 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
18176 break;
18177
19397422
AM
18178 case DT_PPC64_OPD:
18179 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18180 if (s == NULL)
18181 continue;
18182 dyn.d_un.d_ptr = s->vma;
19397422
AM
18183 break;
18184
e8910a83 18185 case DT_PPC64_OPT:
5663e321
AM
18186 if ((htab->do_multi_toc && htab->multi_toc_needed)
18187 || htab->notoc_plt)
e8910a83 18188 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
18189 if (htab->has_plt_localentry0)
18190 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
18191 break;
18192
19397422
AM
18193 case DT_PPC64_OPDSZ:
18194 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18195 if (s == NULL)
18196 continue;
eea6121a 18197 dyn.d_un.d_val = s->size;
19397422
AM
18198 break;
18199
65f38f15 18200 case DT_PLTGOT:
33e44f2e 18201 s = htab->elf.splt;
6348e046 18202 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
18203 break;
18204
18205 case DT_JMPREL:
33e44f2e 18206 s = htab->elf.srelplt;
6348e046 18207 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 18208 break;
5bd4f169 18209
65f38f15 18210 case DT_PLTRELSZ:
33e44f2e 18211 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 18212 break;
82e66161
AM
18213
18214 case DT_TEXTREL:
cebd6b8a 18215 if (htab->elf.ifunc_resolvers)
82e66161
AM
18216 info->callbacks->einfo
18217 (_("%P: warning: text relocations and GNU indirect "
18218 "functions may result in a segfault at runtime\n"));
18219 continue;
5bd4f169 18220 }
5bd4f169 18221
65f38f15 18222 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 18223 }
5bd4f169
AM
18224 }
18225
6528b6eb
AM
18226 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18227 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18228 {
18229 /* Fill in the first entry in the global offset table.
18230 We use it to hold the link-time TOCbase. */
18231 bfd_put_64 (output_bfd,
60ee0d4a 18232 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 18233 htab->elf.sgot->contents);
5d1634d7
AM
18234
18235 /* Set .got entry size. */
2cdcc330
AM
18236 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18237 = 8;
5d1634d7
AM
18238 }
18239
6528b6eb
AM
18240 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18241 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18242 {
18243 /* Set .plt entry size. */
33e44f2e 18244 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 18245 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
18246 }
18247
84f5d08e
AM
18248 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18249 brlt ourselves if emitrelocations. */
18250 if (htab->brlt != NULL
18251 && htab->brlt->reloc_count != 0
18252 && !_bfd_elf_link_output_relocs (output_bfd,
18253 htab->brlt,
d4730f92 18254 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
18255 elf_section_data (htab->brlt)->relocs,
18256 NULL))
0a1b45a2 18257 return false;
84f5d08e 18258
176a0d42
AM
18259 if (htab->glink != NULL
18260 && htab->glink->reloc_count != 0
18261 && !_bfd_elf_link_output_relocs (output_bfd,
18262 htab->glink,
d4730f92 18263 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
18264 elf_section_data (htab->glink)->relocs,
18265 NULL))
0a1b45a2 18266 return false;
176a0d42 18267
da44f4e5 18268
df136d64
AM
18269 if (htab->glink_eh_frame != NULL
18270 && htab->glink_eh_frame->size != 0
18271 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18272 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18273 htab->glink_eh_frame,
18274 htab->glink_eh_frame->contents))
0a1b45a2 18275 return false;
58d180e8 18276
e717da7e 18277 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
18278 since we didn't add them to DYNOBJ. We know dynobj is the first
18279 bfd. */
c72f2fb2 18280 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
18281 {
18282 asection *s;
7b53ace3 18283
0c8d6e5c 18284 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
18285 continue;
18286
e717da7e
AM
18287 s = ppc64_elf_tdata (dynobj)->got;
18288 if (s != NULL
eea6121a 18289 && s->size != 0
e717da7e
AM
18290 && s->output_section != bfd_abs_section_ptr
18291 && !bfd_set_section_contents (output_bfd, s->output_section,
18292 s->contents, s->output_offset,
eea6121a 18293 s->size))
0a1b45a2 18294 return false;
e717da7e
AM
18295 s = ppc64_elf_tdata (dynobj)->relgot;
18296 if (s != NULL
eea6121a 18297 && s->size != 0
e717da7e
AM
18298 && s->output_section != bfd_abs_section_ptr
18299 && !bfd_set_section_contents (output_bfd, s->output_section,
18300 s->contents, s->output_offset,
eea6121a 18301 s->size))
0a1b45a2 18302 return false;
e717da7e 18303 }
f6c52c13 18304
0a1b45a2 18305 return true;
5bd4f169
AM
18306}
18307
5bd4f169 18308#include "elf64-target.h"
7b8e7dad
AM
18309
18310/* FreeBSD support */
18311
18312#undef TARGET_LITTLE_SYM
6afcdeb3 18313#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 18314#undef TARGET_LITTLE_NAME
6afcdeb3 18315#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
18316
18317#undef TARGET_BIG_SYM
6d00b590 18318#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
18319#undef TARGET_BIG_NAME
18320#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18321
18322#undef ELF_OSABI
18323#define ELF_OSABI ELFOSABI_FREEBSD
18324
18325#undef elf64_bed
18326#define elf64_bed elf64_powerpc_fbsd_bed
18327
18328#include "elf64-target.h"