]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
PowerPC64 DT_RELR local GOT
[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
AM
77#define ELF_COMMONPAGESIZE 0x1000
78#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
79#define elf_info_to_howto ppc64_elf_info_to_howto
80
81#define elf_backend_want_got_sym 0
82#define elf_backend_want_plt_sym 0
83#define elf_backend_plt_alignment 3
84#define elf_backend_plt_not_loaded 1
ad8e1ba5 85#define elf_backend_got_header_size 8
5474d94f 86#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
87#define elf_backend_can_gc_sections 1
88#define elf_backend_can_refcount 1
89#define elf_backend_rela_normal 1
64f52338 90#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 91#define elf_backend_default_execstack 0
ad8e1ba5 92
e717da7e 93#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 94#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 95#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
96#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
97#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
98#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
99#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 100#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 101#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 102#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
103
104#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
105#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
106#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 107#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 108#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 109#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 110#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 111#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 112#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 113#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 114#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 115#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 116#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 117#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 118#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
119#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
120#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 121#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
c9405344 122#define elf_backend_always_size_sections ppc64_elf_edit
ad8e1ba5 123#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 124#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 125#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 126#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
127#define elf_backend_relocate_section ppc64_elf_relocate_section
128#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
129#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
130#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 131#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 132#define elf_backend_special_sections ppc64_elf_special_sections
bf577467 133#define elf_backend_section_flags ppc64_elf_section_flags
6911b7dc 134#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 135#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 136#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 137
5bd4f169
AM
138/* The name of the dynamic interpreter. This is put in the .interp
139 section. */
140#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
141
142/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 143#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 144#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
145
146/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 147#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 148
a078d95a
AM
149/* Offsets to some stack save slots. */
150#define STK_LR 16
151#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 152/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
153 CR save slot. Used only by optimised __tls_get_addr call stub,
154 relying on __tls_get_addr_opt not saving CR.. */
155#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
156
5bd4f169 157/* TOC base pointers offset from start of TOC. */
411e1bfb 158#define TOC_BASE_OFF 0x8000
a27e685f
AM
159/* TOC base alignment. */
160#define TOC_BASE_ALIGN 256
411e1bfb
AM
161
162/* Offset of tp and dtp pointers from start of TLS block. */
163#define TP_OFFSET 0x7000
164#define DTP_OFFSET 0x8000
5bd4f169 165
ad8e1ba5
AM
166/* .plt call stub instructions. The normal stub is like this, but
167 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 168 insert an addi to adjust r11. */
a078d95a 169#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
170#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
171#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
172#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
173#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
174#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
175#define BCTR 0x4e800420 /* bctr */
176
07d6d2b8 177#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
178#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
179#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
180#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
181#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
182
183#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
184#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
185#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
186#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
187#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
188#define BNECTR 0x4ca20420 /* bnectr+ */
189#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 190
71a39c98 191#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 192#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 193#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 194
07d6d2b8
AM
195#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
196#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
197#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 198
04bdff6a 199#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 200#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 201#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 202#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
203#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
204#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 205#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 206#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 207#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 208#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 209#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 210#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 211#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
212#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
213#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
214#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
215#define PADDI_R12_PC 0x0610000039800000ULL
216#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 217#define PNOP 0x0700000000000000ULL
a345bc8d 218
3cd7c7d7
AM
219/* __glink_PLTresolve stub instructions. We enter with the index in
220 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
9e390558 221#define GLINK_PLTRESOLVE_SIZE(htab) \
3cd7c7d7 222 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
ee4bf8d2
AM
223 /* 0: */
224 /* .quad plt0-1f */
225 /* __glink: */
226#define MFLR_R12 0x7d8802a6 /* mflr %12 */
227#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
228 /* 1: */
229#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 230 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 231#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
232#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
233 /* ld %12,0(%11) */
234 /* ld %2,8(%11) */
235 /* mtctr %12 */
236 /* ld %11,16(%11) */
ee4bf8d2 237 /* bctr */
3cd7c7d7
AM
238
239#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
240#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
241#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
242#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
243#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
244#define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
245#define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
5d1634d7
AM
246
247/* Pad with this. */
248#define NOP 0x60000000
249
721956f4
AM
250/* Some other nops. */
251#define CROR_151515 0x4def7b82
252#define CROR_313131 0x4ffffb82
253
cedb70c5 254/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
255#define LI_R0_0 0x38000000 /* li %r0,0 */
256#define B_DOT 0x48000000 /* b . */
257
258/* After that, we need two instructions to load the index, followed by
259 a branch. */
260#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 261#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 262
deb0e272
AM
263/* Instructions used by the save and restore reg functions. */
264#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
265#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
266#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
267#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
268#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
269#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
270#define LI_R12_0 0x39800000 /* li %r12,0 */
271#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
272#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
273#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
274#define BLR 0x4e800020 /* blr */
275
41bd81ab
AM
276/* Since .opd is an array of descriptors and each entry will end up
277 with identical R_PPC64_RELATIVE relocs, there is really no need to
278 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 279 relocate .opd without reloc entries. */
41bd81ab
AM
280#ifndef NO_OPD_RELOCS
281#define NO_OPD_RELOCS 0
282#endif
810d4e75 283
a4b6fadd
AM
284#ifndef ARRAY_SIZE
285#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
286#endif
287
810d4e75
AM
288static inline int
289abiversion (bfd *abfd)
290{
291 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
292}
293
294static inline void
295set_abiversion (bfd *abfd, int ver)
296{
297 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
298 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
299}
5bd4f169
AM
300\f
301/* Relocation HOWTO's. */
46807bf4
AM
302/* Like other ELF RELA targets that don't apply multiple
303 field-altering relocations to the same localation, src_mask is
304 always zero and pcrel_offset is the same as pc_relative.
305 PowerPC can always use a zero bitpos, even when the field is not at
306 the LSB. For example, a REL24 could use rightshift=2, bisize=24
307 and bitpos=2 which matches the ABI description, or as we do here,
308 rightshift=0, bitsize=26 and bitpos=0. */
309#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
310 complain, special_func) \
311 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
312 complain_overflow_ ## complain, special_func, \
0a1b45a2 313 #type, false, 0, mask, pc_relative)
46807bf4 314
04c9666a 315static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 316
f3185997
NC
317static reloc_howto_type ppc64_elf_howto_raw[] =
318{
5bd4f169 319 /* This reloc does nothing. */
0a1b45a2 320 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
46807bf4 321 bfd_elf_generic_reloc),
5bd4f169
AM
322
323 /* A standard 32 bit relocation. */
0a1b45a2 324 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* An absolute 26 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
0a1b45a2 329 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
46807bf4 330 bfd_elf_generic_reloc),
5bd4f169
AM
331
332 /* A standard 16 bit relocation. */
0a1b45a2 333 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 334 bfd_elf_generic_reloc),
5bd4f169
AM
335
336 /* A 16 bit relocation without overflow. */
0a1b45a2 337 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 338 bfd_elf_generic_reloc),
5bd4f169
AM
339
340 /* Bits 16-31 of an address. */
0a1b45a2 341 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 342 bfd_elf_generic_reloc),
5bd4f169
AM
343
344 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
345 bits, treated as a signed number, is negative. */
0a1b45a2 346 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 347 ppc64_elf_ha_reloc),
5bd4f169
AM
348
349 /* An absolute 16 bit branch; the lower two bits must be zero.
350 FIXME: we don't check that, we just clear them. */
0a1b45a2 351 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 352 ppc64_elf_branch_reloc),
5bd4f169
AM
353
354 /* An absolute 16 bit branch, for which bit 10 should be set to
355 indicate that the branch is expected to be taken. The lower two
356 bits must be zero. */
0a1b45a2 357 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 358 ppc64_elf_brtaken_reloc),
5bd4f169
AM
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
0a1b45a2 363 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 364 ppc64_elf_brtaken_reloc),
5bd4f169
AM
365
366 /* A relative 26 bit branch; the lower two bits must be zero. */
0a1b45a2 367 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 368 ppc64_elf_branch_reloc),
5bd4f169 369
05d0e962 370 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
0a1b45a2 371 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 372 ppc64_elf_branch_reloc),
05d0e962 373
7aba54da
AM
374 /* Another variant, when p10 insns can't be used on stubs. */
375 HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
376 ppc64_elf_branch_reloc),
377
5bd4f169 378 /* A relative 16 bit branch; the lower two bits must be zero. */
0a1b45a2 379 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 380 ppc64_elf_branch_reloc),
5bd4f169
AM
381
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is expected to be taken. The lower two bits must be
384 zero. */
0a1b45a2 385 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 386 ppc64_elf_brtaken_reloc),
5bd4f169
AM
387
388 /* A relative 16 bit branch. Bit 10 should be set to indicate that
389 the branch is not expected to be taken. The lower two bits must
390 be zero. */
0a1b45a2 391 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 392 ppc64_elf_brtaken_reloc),
5bd4f169
AM
393
394 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
395 symbol. */
0a1b45a2 396 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
46807bf4 397 ppc64_elf_unhandled_reloc),
5bd4f169
AM
398
399 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
400 the symbol. */
0a1b45a2 401 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 402 ppc64_elf_unhandled_reloc),
5bd4f169
AM
403
404 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
405 the symbol. */
0a1b45a2 406 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 407 ppc64_elf_unhandled_reloc),
5bd4f169
AM
408
409 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
410 the symbol. */
0a1b45a2 411 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 412 ppc64_elf_unhandled_reloc),
5bd4f169
AM
413
414 /* This is used only by the dynamic linker. The symbol should exist
415 both in the object being run and in some shared library. The
416 dynamic linker copies the data addressed by the symbol from the
417 shared library into the object, because the object being
418 run has to have the data at some particular address. */
0a1b45a2 419 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
46807bf4 420 ppc64_elf_unhandled_reloc),
5bd4f169
AM
421
422 /* Like R_PPC64_ADDR64, but used when setting global offset table
423 entries. */
0a1b45a2 424 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 425 ppc64_elf_unhandled_reloc),
5bd4f169
AM
426
427 /* Created by the link editor. Marks a procedure linkage table
428 entry for a symbol. */
0a1b45a2 429 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
46807bf4 430 ppc64_elf_unhandled_reloc),
5bd4f169
AM
431
432 /* Used only by the dynamic linker. When the object is run, this
433 doubleword64 is set to the load address of the object, plus the
434 addend. */
0a1b45a2 435 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 436 bfd_elf_generic_reloc),
5bd4f169
AM
437
438 /* Like R_PPC64_ADDR32, but may be unaligned. */
0a1b45a2 439 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 440 bfd_elf_generic_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16, but may be unaligned. */
0a1b45a2 443 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 444 bfd_elf_generic_reloc),
5bd4f169
AM
445
446 /* 32-bit PC relative. */
0a1b45a2 447 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 448 bfd_elf_generic_reloc),
5bd4f169 449
10ed1bba 450 /* 32-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 451 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 452 ppc64_elf_unhandled_reloc),
5bd4f169
AM
453
454 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
455 FIXME: R_PPC64_PLTREL32 not supported. */
0a1b45a2 456 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 457 ppc64_elf_unhandled_reloc),
5bd4f169
AM
458
459 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
460 the symbol. */
0a1b45a2 461 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 462 ppc64_elf_unhandled_reloc),
5bd4f169
AM
463
464 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
465 the symbol. */
0a1b45a2 466 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 467 ppc64_elf_unhandled_reloc),
5bd4f169
AM
468
469 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
470 the symbol. */
0a1b45a2 471 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 472 ppc64_elf_unhandled_reloc),
5bd4f169 473
c061c2d8 474 /* 16-bit section relative relocation. */
0a1b45a2 475 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
46807bf4 476 ppc64_elf_sectoff_reloc),
5bd4f169 477
c061c2d8 478 /* Like R_PPC64_SECTOFF, but no overflow warning. */
0a1b45a2 479 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 480 ppc64_elf_sectoff_reloc),
5bd4f169
AM
481
482 /* 16-bit upper half section relative relocation. */
0a1b45a2 483 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 484 ppc64_elf_sectoff_reloc),
5bd4f169
AM
485
486 /* 16-bit upper half adjusted section relative relocation. */
0a1b45a2 487 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 488 ppc64_elf_sectoff_ha_reloc),
5bd4f169 489
04c9666a 490 /* Like R_PPC64_REL24 without touching the two least significant bits. */
0a1b45a2 491 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
46807bf4 492 bfd_elf_generic_reloc),
5bd4f169
AM
493
494 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
495
496 /* A standard 64-bit relocation. */
0a1b45a2 497 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 498 bfd_elf_generic_reloc),
5bd4f169
AM
499
500 /* The bits 32-47 of an address. */
0a1b45a2 501 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 502 bfd_elf_generic_reloc),
5bd4f169
AM
503
504 /* The bits 32-47 of an address, plus 1 if the contents of the low
505 16 bits, treated as a signed number, is negative. */
0a1b45a2 506 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 507 ppc64_elf_ha_reloc),
5bd4f169
AM
508
509 /* The bits 48-63 of an address. */
0a1b45a2 510 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 511 bfd_elf_generic_reloc),
5bd4f169
AM
512
513 /* The bits 48-63 of an address, plus 1 if the contents of the low
514 16 bits, treated as a signed number, is negative. */
0a1b45a2 515 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 516 ppc64_elf_ha_reloc),
5bd4f169
AM
517
518 /* Like ADDR64, but may be unaligned. */
0a1b45a2 519 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 520 bfd_elf_generic_reloc),
5bd4f169
AM
521
522 /* 64-bit relative relocation. */
0a1b45a2 523 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 524 bfd_elf_generic_reloc),
5bd4f169 525
cedb70c5 526 /* 64-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 527 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 528 ppc64_elf_unhandled_reloc),
5bd4f169
AM
529
530 /* 64-bit PC relative relocation to the symbol's procedure linkage
531 table. */
532 /* FIXME: R_PPC64_PLTREL64 not supported. */
0a1b45a2 533 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 534 ppc64_elf_unhandled_reloc),
5bd4f169
AM
535
536 /* 16 bit TOC-relative relocation. */
5bd4f169 537 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
0a1b45a2 538 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
46807bf4 539 ppc64_elf_toc_reloc),
5bd4f169
AM
540
541 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 542 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
0a1b45a2 543 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 544 ppc64_elf_toc_reloc),
5bd4f169
AM
545
546 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 547 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
0a1b45a2 548 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 549 ppc64_elf_toc_reloc),
5bd4f169
AM
550
551 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
552 contents of the low 16 bits, treated as a signed number, is
553 negative. */
5bd4f169 554 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
0a1b45a2 555 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 556 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
557
558 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 559 /* R_PPC64_TOC 51 doubleword64 .TOC. */
0a1b45a2 560 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 561 ppc64_elf_toc64_reloc),
5bd4f169
AM
562
563 /* Like R_PPC64_GOT16, but also informs the link editor that the
564 value to relocate may (!) refer to a PLT entry which the link
565 editor (a) may replace with the symbol value. If the link editor
566 is unable to fully resolve the symbol, it may (b) create a PLT
567 entry and store the address to the new PLT entry in the GOT.
568 This permits lazy resolution of function symbols at run time.
569 The link editor may also skip all of this and just (c) emit a
570 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
571 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
0a1b45a2 572 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
46807bf4 573 ppc64_elf_unhandled_reloc),
411e1bfb
AM
574
575 /* Like R_PPC64_PLTGOT16, but without overflow. */
576 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 577 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 578 ppc64_elf_unhandled_reloc),
411e1bfb
AM
579
580 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
581 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
0a1b45a2 582 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 583 ppc64_elf_unhandled_reloc),
411e1bfb
AM
584
585 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
586 1 if the contents of the low 16 bits, treated as a signed number,
587 is negative. */
588 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
0a1b45a2 589 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 590 ppc64_elf_unhandled_reloc),
411e1bfb
AM
591
592 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
0a1b45a2 593 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 594 bfd_elf_generic_reloc),
411e1bfb
AM
595
596 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
0a1b45a2 597 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 598 bfd_elf_generic_reloc),
411e1bfb
AM
599
600 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
0a1b45a2 601 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 602 ppc64_elf_unhandled_reloc),
411e1bfb
AM
603
604 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
0a1b45a2 605 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 606 ppc64_elf_unhandled_reloc),
411e1bfb
AM
607
608 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
0a1b45a2 609 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 610 ppc64_elf_unhandled_reloc),
411e1bfb
AM
611
612 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
0a1b45a2 613 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 614 ppc64_elf_sectoff_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
0a1b45a2 617 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 618 ppc64_elf_sectoff_reloc),
411e1bfb
AM
619
620 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
0a1b45a2 621 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 622 ppc64_elf_toc_reloc),
411e1bfb
AM
623
624 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
0a1b45a2 625 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 626 ppc64_elf_toc_reloc),
411e1bfb
AM
627
628 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
629 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
0a1b45a2 630 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 631 ppc64_elf_unhandled_reloc),
411e1bfb
AM
632
633 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
634 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 635 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 636 ppc64_elf_unhandled_reloc),
411e1bfb 637
727fc41e 638 /* Marker relocs for TLS. */
0a1b45a2 639 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
46807bf4
AM
640 bfd_elf_generic_reloc),
641
0a1b45a2 642 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
46807bf4
AM
643 bfd_elf_generic_reloc),
644
0a1b45a2 645 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
46807bf4 646 bfd_elf_generic_reloc),
727fc41e 647
23cedd1d
AM
648 /* Marker reloc for optimizing r2 save in prologue rather than on
649 each plt call stub. */
0a1b45a2 650 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
46807bf4 651 bfd_elf_generic_reloc),
3b421ab3 652
23cedd1d 653 /* Marker relocs on inline plt call instructions. */
0a1b45a2 654 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
46807bf4
AM
655 bfd_elf_generic_reloc),
656
0a1b45a2 657 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
46807bf4 658 bfd_elf_generic_reloc),
23cedd1d 659
411e1bfb
AM
660 /* Computes the load module index of the load module that contains the
661 definition of its TLS sym. */
0a1b45a2 662 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 663 ppc64_elf_unhandled_reloc),
411e1bfb
AM
664
665 /* Computes a dtv-relative displacement, the difference between the value
666 of sym+add and the base address of the thread-local storage block that
667 contains the definition of sym, minus 0x8000. */
0a1b45a2 668 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 669 ppc64_elf_unhandled_reloc),
411e1bfb
AM
670
671 /* A 16 bit dtprel reloc. */
0a1b45a2 672 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 673 ppc64_elf_unhandled_reloc),
411e1bfb
AM
674
675 /* Like DTPREL16, but no overflow. */
0a1b45a2 676 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 677 ppc64_elf_unhandled_reloc),
411e1bfb
AM
678
679 /* Like DTPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 680 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 681 ppc64_elf_unhandled_reloc),
411e1bfb
AM
682
683 /* Like DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 684 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 685 ppc64_elf_unhandled_reloc),
411e1bfb
AM
686
687 /* Like DTPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 688 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 689 ppc64_elf_unhandled_reloc),
411e1bfb
AM
690
691 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 692 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 693 ppc64_elf_unhandled_reloc),
411e1bfb
AM
694
695 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 696 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 700 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like DTPREL16, but for insns with a DS field. */
0a1b45a2 704 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like DTPREL16_DS, but no overflow. */
0a1b45a2 708 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Computes a tp-relative displacement, the difference between the value of
712 sym+add and the value of the thread pointer (r13). */
0a1b45a2 713 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 714 ppc64_elf_unhandled_reloc),
411e1bfb
AM
715
716 /* A 16 bit tprel reloc. */
0a1b45a2 717 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 718 ppc64_elf_unhandled_reloc),
411e1bfb
AM
719
720 /* Like TPREL16, but no overflow. */
0a1b45a2 721 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 722 ppc64_elf_unhandled_reloc),
411e1bfb
AM
723
724 /* Like TPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 725 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 726 ppc64_elf_unhandled_reloc),
411e1bfb
AM
727
728 /* Like TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 729 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 730 ppc64_elf_unhandled_reloc),
411e1bfb
AM
731
732 /* Like TPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 733 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 734 ppc64_elf_unhandled_reloc),
411e1bfb
AM
735
736 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 737 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 738 ppc64_elf_unhandled_reloc),
411e1bfb
AM
739
740 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 741 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 742 ppc64_elf_unhandled_reloc),
411e1bfb
AM
743
744 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 745 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 746 ppc64_elf_unhandled_reloc),
411e1bfb
AM
747
748 /* Like TPREL16, but for insns with a DS field. */
0a1b45a2 749 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 750 ppc64_elf_unhandled_reloc),
411e1bfb
AM
751
752 /* Like TPREL16_DS, but no overflow. */
0a1b45a2 753 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 754 ppc64_elf_unhandled_reloc),
411e1bfb
AM
755
756 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
757 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
758 to the first entry relative to the TOC base (r2). */
0a1b45a2 759 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 760 ppc64_elf_unhandled_reloc),
5bd4f169 761
411e1bfb 762 /* Like GOT_TLSGD16, but no overflow. */
0a1b45a2 763 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 764 ppc64_elf_unhandled_reloc),
5bd4f169 765
411e1bfb 766 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
0a1b45a2 767 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 768 ppc64_elf_unhandled_reloc),
5bd4f169 769
411e1bfb 770 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
0a1b45a2 771 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 772 ppc64_elf_unhandled_reloc),
5bd4f169 773
411e1bfb
AM
774 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
775 with values (sym+add)@dtpmod and zero, and computes the offset to the
776 first entry relative to the TOC base (r2). */
0a1b45a2 777 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 778 ppc64_elf_unhandled_reloc),
5bd4f169 779
411e1bfb 780 /* Like GOT_TLSLD16, but no overflow. */
0a1b45a2 781 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
0a1b45a2 785 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
0a1b45a2 789 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 790 ppc64_elf_unhandled_reloc),
5bd4f169 791
411e1bfb
AM
792 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
793 the offset to the entry relative to the TOC base (r2). */
0a1b45a2 794 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 795 ppc64_elf_unhandled_reloc),
5bd4f169 796
411e1bfb 797 /* Like GOT_DTPREL16_DS, but no overflow. */
0a1b45a2 798 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 802 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 806 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 807 ppc64_elf_unhandled_reloc),
411e1bfb
AM
808
809 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
810 offset to the entry relative to the TOC base (r2). */
0a1b45a2 811 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 812 ppc64_elf_unhandled_reloc),
5bd4f169 813
411e1bfb 814 /* Like GOT_TPREL16_DS, but no overflow. */
0a1b45a2 815 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 816 ppc64_elf_unhandled_reloc),
5bd4f169 817
411e1bfb 818 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 819 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 820 ppc64_elf_unhandled_reloc),
5bd4f169 821
411e1bfb 822 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 823 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4
AM
824 ppc64_elf_unhandled_reloc),
825
0a1b45a2 826 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
46807bf4
AM
827 ppc64_elf_unhandled_reloc),
828
0a1b45a2 829 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 830 bfd_elf_generic_reloc),
e054468f 831
25f23106 832 /* A 16 bit relative relocation. */
0a1b45a2 833 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
46807bf4 834 bfd_elf_generic_reloc),
25f23106
AM
835
836 /* A 16 bit relative relocation without overflow. */
0a1b45a2 837 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
46807bf4 838 bfd_elf_generic_reloc),
25f23106
AM
839
840 /* The high order 16 bits of a relative address. */
0a1b45a2 841 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
46807bf4 842 bfd_elf_generic_reloc),
25f23106
AM
843
844 /* The high order 16 bits of a relative address, plus 1 if the contents of
845 the low 16 bits, treated as a signed number, is negative. */
0a1b45a2 846 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
46807bf4 847 ppc64_elf_ha_reloc),
25f23106 848
0a1b45a2 849 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
850 bfd_elf_generic_reloc),
851
0a1b45a2 852 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
853 ppc64_elf_ha_reloc),
854
0a1b45a2 855 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
856 bfd_elf_generic_reloc),
857
0a1b45a2 858 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
859 ppc64_elf_ha_reloc),
860
0a1b45a2 861 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
862 bfd_elf_generic_reloc),
863
0a1b45a2 864 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
865 ppc64_elf_ha_reloc),
866
a680de9a 867 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
0a1b45a2 868 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
46807bf4 869 ppc64_elf_ha_reloc),
a680de9a 870
7ba71655 871 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
0a1b45a2 872 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
46807bf4 873 ppc64_elf_ha_reloc),
7ba71655 874
f9c6b907 875 /* Like R_PPC64_ADDR16_HI, but no overflow. */
0a1b45a2 876 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 877 bfd_elf_generic_reloc),
f9c6b907
AM
878
879 /* Like R_PPC64_ADDR16_HA, but no overflow. */
0a1b45a2 880 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 881 ppc64_elf_ha_reloc),
f9c6b907
AM
882
883 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
0a1b45a2 884 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 885 ppc64_elf_unhandled_reloc),
f9c6b907
AM
886
887 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
0a1b45a2 888 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 889 ppc64_elf_unhandled_reloc),
f9c6b907
AM
890
891 /* Like R_PPC64_TPREL16_HI, but no overflow. */
0a1b45a2 892 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 893 ppc64_elf_unhandled_reloc),
f9c6b907
AM
894
895 /* Like R_PPC64_TPREL16_HA, but no overflow. */
0a1b45a2 896 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 897 ppc64_elf_unhandled_reloc),
f9c6b907 898
006589cf 899 /* Marker reloc on ELFv2 large-model function entry. */
0a1b45a2 900 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
46807bf4 901 bfd_elf_generic_reloc),
006589cf 902
45965137 903 /* Like ADDR64, but use local entry point of function. */
0a1b45a2 904 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 905 bfd_elf_generic_reloc),
45965137 906
0a1b45a2 907 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
908 bfd_elf_generic_reloc),
909
0a1b45a2 910 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
911 bfd_elf_generic_reloc),
912
0a1b45a2 913 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
5663e321
AM
914 bfd_elf_generic_reloc),
915
0a1b45a2 916 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
5663e321
AM
917 ppc64_elf_prefix_reloc),
918
0a1b45a2 919 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
5663e321
AM
920 ppc64_elf_prefix_reloc),
921
0a1b45a2 922 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
923 ppc64_elf_prefix_reloc),
924
0a1b45a2 925 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
926 ppc64_elf_prefix_reloc),
927
0a1b45a2 928 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
929 ppc64_elf_prefix_reloc),
930
0a1b45a2 931 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
932 ppc64_elf_unhandled_reloc),
933
0a1b45a2 934 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
935 ppc64_elf_unhandled_reloc),
936
0a1b45a2 937 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
938 ppc64_elf_unhandled_reloc),
939
0a1b45a2 940 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
941 ppc64_elf_unhandled_reloc),
942
0a1b45a2 943 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
944 ppc64_elf_unhandled_reloc),
945
0a1b45a2 946 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
947 ppc64_elf_unhandled_reloc),
948
0a1b45a2 949 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
950 ppc64_elf_unhandled_reloc),
951
0a1b45a2 952 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
953 ppc64_elf_unhandled_reloc),
954
0a1b45a2 955 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
956 ppc64_elf_unhandled_reloc),
957
0a1b45a2 958 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
959 bfd_elf_generic_reloc),
960
0a1b45a2 961 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
962 ppc64_elf_ha_reloc),
963
0a1b45a2 964 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
965 bfd_elf_generic_reloc),
966
0a1b45a2 967 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
968 ppc64_elf_ha_reloc),
969
0a1b45a2 970 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
971 bfd_elf_generic_reloc),
972
0a1b45a2 973 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
974 ppc64_elf_ha_reloc),
975
0a1b45a2 976 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
977 bfd_elf_generic_reloc),
978
0a1b45a2 979 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
980 ppc64_elf_ha_reloc),
981
0a1b45a2 982 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
5663e321
AM
983 ppc64_elf_prefix_reloc),
984
0a1b45a2 985 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
5663e321
AM
986 ppc64_elf_prefix_reloc),
987
5bd4f169 988 /* GNU extension to record C++ vtable hierarchy. */
0a1b45a2 989 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
46807bf4 990 NULL),
5bd4f169
AM
991
992 /* GNU extension to record C++ vtable member usage. */
0a1b45a2 993 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
46807bf4 994 NULL),
5bd4f169
AM
995};
996
997\f
998/* Initialize the ppc64_elf_howto_table, so that linear accesses can
999 be done. */
1000
1001static void
4ce794b7 1002ppc_howto_init (void)
5bd4f169
AM
1003{
1004 unsigned int i, type;
1005
a4b6fadd 1006 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
1007 {
1008 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 1009 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
1010 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1011 }
1012}
1013
1014static reloc_howto_type *
87c69f97 1015ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
5bd4f169 1016{
411e1bfb 1017 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1018
1019 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1020 /* Initialize howto table if needed. */
1021 ppc_howto_init ();
1022
4ce794b7 1023 switch (code)
5bd4f169
AM
1024 {
1025 default:
f3185997 1026 /* xgettext:c-format */
2cdcc330
AM
1027 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1028 (int) code);
f3185997 1029 bfd_set_error (bfd_error_bad_value);
4ce794b7 1030 return NULL;
5bd4f169 1031
411e1bfb
AM
1032 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1033 break;
1034 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1035 break;
1036 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1037 break;
1038 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1039 break;
1040 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1041 break;
1042 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1043 break;
f9c6b907
AM
1044 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1045 break;
411e1bfb 1046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1047 break;
f9c6b907
AM
1048 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1049 break;
411e1bfb 1050 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1057 break;
05d0e962
AM
1058 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1059 break;
7aba54da
AM
1060 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1061 break;
411e1bfb 1062 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1139 break;
411e1bfb 1140 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1141 break;
411e1bfb 1142 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1143 break;
411e1bfb 1144 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1145 break;
411e1bfb 1146 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1147 break;
411e1bfb 1148 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1149 break;
411e1bfb 1150 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1151 break;
411e1bfb 1152 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1153 break;
411e1bfb 1154 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1155 break;
411e1bfb 1156 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1157 break;
c213164a 1158 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1159 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1160 break;
727fc41e
AM
1161 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1162 break;
1163 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1164 break;
411e1bfb 1165 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1166 break;
411e1bfb 1167 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1168 break;
411e1bfb 1169 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1170 break;
411e1bfb 1171 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1172 break;
f9c6b907
AM
1173 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1174 break;
411e1bfb 1175 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1176 break;
f9c6b907
AM
1177 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1178 break;
411e1bfb 1179 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1180 break;
411e1bfb
AM
1181 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1182 break;
1183 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1184 break;
1185 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1186 break;
f9c6b907
AM
1187 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1188 break;
411e1bfb
AM
1189 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1190 break;
f9c6b907
AM
1191 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1192 break;
411e1bfb
AM
1193 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1218 break;
1219 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1220 break;
1221 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1222 break;
1223 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1226 break;
1227 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1228 break;
1229 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1238 break;
1239 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1240 break;
1241 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1250 break;
25f23106
AM
1251 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1252 break;
1253 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1254 break;
1255 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1256 break;
1257 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1258 break;
4a969973
AM
1259 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1260 break;
1261 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1262 break;
1263 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1270 break;
7ba71655
AM
1271 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1272 break;
a680de9a
PB
1273 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1274 break;
006589cf
AM
1275 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1276 break;
45965137
AM
1277 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1278 break;
5663e321
AM
1279 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1280 break;
1281 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1282 break;
1283 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1284 break;
1285 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1286 break;
1287 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1288 break;
1289 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1290 break;
1291 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1292 break;
c213164a
AM
1293 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1294 break;
1295 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1296 break;
87c69f97 1297 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
c213164a 1298 break;
87c69f97 1299 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
c213164a 1300 break;
87c69f97 1301 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
c213164a 1302 break;
87c69f97 1303 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
c213164a 1304 break;
5663e321
AM
1305 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1306 break;
1307 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1308 break;
1309 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1312 break;
1313 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1314 break;
1315 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1316 break;
1317 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1320 break;
1321 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1322 break;
1323 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1324 break;
411e1bfb
AM
1325 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1326 break;
1327 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1328 break;
1329 }
1330
4ce794b7 1331 return ppc64_elf_howto_table[r];
5bd4f169
AM
1332};
1333
157090f7 1334static reloc_howto_type *
87c69f97 1335ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
157090f7
AM
1336{
1337 unsigned int i;
87c69f97
AM
1338 static char *compat_map[][2] = {
1339 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1340 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1341 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1342 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1343 };
157090f7 1344
a4b6fadd 1345 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1346 if (ppc64_elf_howto_raw[i].name != NULL
1347 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1348 return &ppc64_elf_howto_raw[i];
1349
87c69f97
AM
1350 /* Handle old names of relocations in case they were used by
1351 .reloc directives.
1352 FIXME: Remove this soon. Mapping the reloc names is very likely
1353 completely unnecessary. */
1354 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1355 if (strcasecmp (compat_map[i][0], r_name) == 0)
1356 {
1357 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1358 compat_map[i][1], compat_map[i][0]);
1359 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1360 }
1361
157090f7
AM
1362 return NULL;
1363}
1364
5bd4f169
AM
1365/* Set the howto pointer for a PowerPC ELF reloc. */
1366
0a1b45a2 1367static bool
4aef7643 1368ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1369 Elf_Internal_Rela *dst)
5bd4f169 1370{
65f38f15
AM
1371 unsigned int type;
1372
ef60b7ff 1373 /* Initialize howto table if needed. */
5bd4f169 1374 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1375 ppc_howto_init ();
1376
65f38f15 1377 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1378 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1379 {
695344c0 1380 /* xgettext:c-format */
0aa13fee 1381 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1382 abfd, type);
f3185997 1383 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1384 return false;
d0fb9a8d 1385 }
65f38f15 1386 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1387 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1388 {
1389 /* xgettext:c-format */
1390 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1391 abfd, type);
1392 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1393 return false;
f3185997 1394 }
2cdcc330 1395
0a1b45a2 1396 return true;
5bd4f169
AM
1397}
1398
04c9666a 1399/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1400
1401static bfd_reloc_status_type
4ce794b7
AM
1402ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1403 void *data, asection *input_section,
1404 bfd *output_bfd, char **error_message)
5bd4f169 1405{
a680de9a
PB
1406 enum elf_ppc64_reloc_type r_type;
1407 long insn;
1408 bfd_size_type octets;
3de43e7b 1409 bfd_vma value;
a680de9a 1410
805fc799
AM
1411 /* If this is a relocatable link (output_bfd test tells us), just
1412 call the generic function. Any adjustment will be done at final
1413 link time. */
1414 if (output_bfd != NULL)
cedb70c5 1415 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1416 input_section, output_bfd, error_message);
1417
5663e321
AM
1418 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1419 We won't actually be using the low bits, so trashing them
805fc799 1420 doesn't matter. */
a680de9a 1421 r_type = reloc_entry->howto->type;
5663e321
AM
1422 if (r_type == R_PPC64_ADDR16_HIGHERA34
1423 || r_type == R_PPC64_ADDR16_HIGHESTA34
1424 || r_type == R_PPC64_REL16_HIGHERA34
1425 || r_type == R_PPC64_REL16_HIGHESTA34)
1426 reloc_entry->addend += 1ULL << 33;
1427 else
1428 reloc_entry->addend += 1U << 15;
a680de9a
PB
1429 if (r_type != R_PPC64_REL16DX_HA)
1430 return bfd_reloc_continue;
1431
1432 value = 0;
1433 if (!bfd_is_com_section (symbol->section))
1434 value = symbol->value;
1435 value += (reloc_entry->addend
1436 + symbol->section->output_offset
1437 + symbol->section->output_section->vma);
1438 value -= (reloc_entry->address
1439 + input_section->output_offset
1440 + input_section->output_section->vma);
3de43e7b 1441 value = (bfd_signed_vma) value >> 16;
a680de9a 1442
bb294208 1443 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1444 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1445 input_section, octets))
1446 return bfd_reloc_outofrange;
1447
a680de9a
PB
1448 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1449 insn &= ~0x1fffc1;
3de43e7b 1450 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1451 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1452 if (value + 0x8000 > 0xffff)
a680de9a
PB
1453 return bfd_reloc_overflow;
1454 return bfd_reloc_ok;
805fc799 1455}
5bd4f169 1456
2441e016
AM
1457static bfd_reloc_status_type
1458ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1459 void *data, asection *input_section,
1460 bfd *output_bfd, char **error_message)
1461{
1462 if (output_bfd != NULL)
1463 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1464 input_section, output_bfd, error_message);
1465
699733f6
AM
1466 if (strcmp (symbol->section->name, ".opd") == 0
1467 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1468 {
1469 bfd_vma dest = opd_entry_value (symbol->section,
1470 symbol->value + reloc_entry->addend,
0a1b45a2 1471 NULL, NULL, false);
2441e016
AM
1472 if (dest != (bfd_vma) -1)
1473 reloc_entry->addend = dest - (symbol->value
1474 + symbol->section->output_section->vma
1475 + symbol->section->output_offset);
1476 }
810d4e75
AM
1477 else
1478 {
1479 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1480
1481 if (symbol->section->owner != abfd
9f284bf9 1482 && symbol->section->owner != NULL
810d4e75
AM
1483 && abiversion (symbol->section->owner) >= 2)
1484 {
1485 unsigned int i;
1486
1487 for (i = 0; i < symbol->section->owner->symcount; ++i)
1488 {
1489 asymbol *symdef = symbol->section->owner->outsymbols[i];
1490
1491 if (strcmp (symdef->name, symbol->name) == 0)
1492 {
1493 elfsym = (elf_symbol_type *) symdef;
1494 break;
1495 }
1496 }
1497 }
1498 reloc_entry->addend
1499 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1500 }
2441e016
AM
1501 return bfd_reloc_continue;
1502}
1503
805fc799 1504static bfd_reloc_status_type
4ce794b7
AM
1505ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1506 void *data, asection *input_section,
1507 bfd *output_bfd, char **error_message)
805fc799
AM
1508{
1509 long insn;
04c9666a 1510 enum elf_ppc64_reloc_type r_type;
805fc799 1511 bfd_size_type octets;
794e51c0 1512 /* Assume 'at' branch hints. */
0a1b45a2 1513 bool is_isa_v2 = true;
805fc799
AM
1514
1515 /* If this is a relocatable link (output_bfd test tells us), just
1516 call the generic function. Any adjustment will be done at final
1517 link time. */
5bd4f169 1518 if (output_bfd != NULL)
cedb70c5 1519 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1520 input_section, output_bfd, error_message);
1521
bb294208 1522 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1523 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1524 input_section, octets))
1525 return bfd_reloc_outofrange;
1526
805fc799
AM
1527 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1528 insn &= ~(0x01 << 21);
4ce794b7 1529 r_type = reloc_entry->howto->type;
805fc799
AM
1530 if (r_type == R_PPC64_ADDR14_BRTAKEN
1531 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1532 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1533
794e51c0 1534 if (is_isa_v2)
5bd4f169 1535 {
805fc799
AM
1536 /* Set 'a' bit. This is 0b00010 in BO field for branch
1537 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1538 for branch on CTR insns (BO == 1a00t or 1a01t). */
1539 if ((insn & (0x14 << 21)) == (0x04 << 21))
1540 insn |= 0x02 << 21;
1541 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1542 insn |= 0x08 << 21;
1543 else
2441e016 1544 goto out;
5bd4f169 1545 }
805fc799
AM
1546 else
1547 {
1548 bfd_vma target = 0;
1549 bfd_vma from;
5bd4f169 1550
805fc799
AM
1551 if (!bfd_is_com_section (symbol->section))
1552 target = symbol->value;
1553 target += symbol->section->output_section->vma;
1554 target += symbol->section->output_offset;
1555 target += reloc_entry->addend;
5bd4f169 1556
805fc799
AM
1557 from = (reloc_entry->address
1558 + input_section->output_offset
1559 + input_section->output_section->vma);
5bd4f169 1560
805fc799
AM
1561 /* Invert 'y' bit if not the default. */
1562 if ((bfd_signed_vma) (target - from) < 0)
1563 insn ^= 0x01 << 21;
1564 }
4ce794b7 1565 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1566 out:
1567 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1568 input_section, output_bfd, error_message);
805fc799 1569}
5bd4f169 1570
805fc799 1571static bfd_reloc_status_type
4ce794b7
AM
1572ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1573 void *data, asection *input_section,
1574 bfd *output_bfd, char **error_message)
805fc799
AM
1575{
1576 /* If this is a relocatable link (output_bfd test tells us), just
1577 call the generic function. Any adjustment will be done at final
1578 link time. */
1579 if (output_bfd != NULL)
cedb70c5 1580 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1581 input_section, output_bfd, error_message);
5bd4f169 1582
805fc799
AM
1583 /* Subtract the symbol section base address. */
1584 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1585 return bfd_reloc_continue;
1586}
1587
805fc799 1588static bfd_reloc_status_type
4ce794b7
AM
1589ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
805fc799
AM
1592{
1593 /* If this is a relocatable link (output_bfd test tells us), just
1594 call the generic function. Any adjustment will be done at final
1595 link time. */
1596 if (output_bfd != NULL)
cedb70c5 1597 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1598 input_section, output_bfd, error_message);
1599
1600 /* Subtract the symbol section base address. */
1601 reloc_entry->addend -= symbol->section->output_section->vma;
1602
1603 /* Adjust the addend for sign extension of the low 16 bits. */
1604 reloc_entry->addend += 0x8000;
1605 return bfd_reloc_continue;
1606}
1607
1608static bfd_reloc_status_type
4ce794b7
AM
1609ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1610 void *data, asection *input_section,
1611 bfd *output_bfd, char **error_message)
805fc799
AM
1612{
1613 bfd_vma TOCstart;
1614
1615 /* If this is a relocatable link (output_bfd test tells us), just
1616 call the generic function. Any adjustment will be done at final
1617 link time. */
1618 if (output_bfd != NULL)
cedb70c5 1619 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1620 input_section, output_bfd, error_message);
1621
1622 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1623 if (TOCstart == 0)
1c865ab2 1624 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1625
1626 /* Subtract the TOC base address. */
1627 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1628 return bfd_reloc_continue;
1629}
1630
1631static bfd_reloc_status_type
4ce794b7
AM
1632ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1633 void *data, asection *input_section,
1634 bfd *output_bfd, char **error_message)
805fc799
AM
1635{
1636 bfd_vma TOCstart;
1637
1638 /* If this is a relocatable link (output_bfd test tells us), just
1639 call the generic function. Any adjustment will be done at final
1640 link time. */
1641 if (output_bfd != NULL)
cedb70c5 1642 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1643 input_section, output_bfd, error_message);
1644
1645 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1646 if (TOCstart == 0)
1c865ab2 1647 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1648
1649 /* Subtract the TOC base address. */
1650 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1651
1652 /* Adjust the addend for sign extension of the low 16 bits. */
1653 reloc_entry->addend += 0x8000;
1654 return bfd_reloc_continue;
1655}
1656
1657static bfd_reloc_status_type
4ce794b7
AM
1658ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1659 void *data, asection *input_section,
1660 bfd *output_bfd, char **error_message)
805fc799
AM
1661{
1662 bfd_vma TOCstart;
1663 bfd_size_type octets;
1664
1665 /* If this is a relocatable link (output_bfd test tells us), just
1666 call the generic function. Any adjustment will be done at final
1667 link time. */
1668 if (output_bfd != NULL)
cedb70c5 1669 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1670 input_section, output_bfd, error_message);
1671
b54509b8
AM
1672 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1673 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1674 input_section, octets))
1675 return bfd_reloc_outofrange;
1676
805fc799
AM
1677 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1678 if (TOCstart == 0)
1c865ab2 1679 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799 1680
805fc799
AM
1681 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1682 return bfd_reloc_ok;
1683}
1684
5663e321
AM
1685static bfd_reloc_status_type
1686ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1687 void *data, asection *input_section,
1688 bfd *output_bfd, char **error_message)
1689{
1690 uint64_t insn;
1691 bfd_vma targ;
b54509b8 1692 bfd_size_type octets;
5663e321
AM
1693
1694 if (output_bfd != NULL)
1695 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1696 input_section, output_bfd, error_message);
1697
b54509b8
AM
1698 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1699 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1700 input_section, octets))
1701 return bfd_reloc_outofrange;
1702
1703 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
5663e321 1704 insn <<= 32;
b54509b8 1705 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
5663e321
AM
1706
1707 targ = (symbol->section->output_section->vma
1708 + symbol->section->output_offset
1709 + reloc_entry->addend);
1710 if (!bfd_is_com_section (symbol->section))
1711 targ += symbol->value;
1712 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1713 targ += 1ULL << 33;
1714 if (reloc_entry->howto->pc_relative)
1715 {
1716 bfd_vma from = (reloc_entry->address
1717 + input_section->output_offset
1718 + input_section->output_section->vma);
1719 targ -=from;
1720 }
1721 targ >>= reloc_entry->howto->rightshift;
1722 insn &= ~reloc_entry->howto->dst_mask;
1723 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
b54509b8
AM
1724 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1725 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
5663e321
AM
1726 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1727 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1728 >= 1ULL << reloc_entry->howto->bitsize))
1729 return bfd_reloc_overflow;
1730 return bfd_reloc_ok;
1731}
1732
805fc799 1733static bfd_reloc_status_type
4ce794b7
AM
1734ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1735 void *data, asection *input_section,
1736 bfd *output_bfd, char **error_message)
805fc799
AM
1737{
1738 /* If this is a relocatable link (output_bfd test tells us), just
1739 call the generic function. Any adjustment will be done at final
1740 link time. */
1741 if (output_bfd != NULL)
cedb70c5 1742 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1743 input_section, output_bfd, error_message);
1744
1745 if (error_message != NULL)
1746 {
7fbd5f4e
AM
1747 static char *message;
1748 free (message);
1749 if (asprintf (&message, _("generic linker can't handle %s"),
1750 reloc_entry->howto->name) < 0)
1751 message = NULL;
1752 *error_message = message;
805fc799
AM
1753 }
1754 return bfd_reloc_dangerous;
1755}
1756
927be08e
AM
1757/* Track GOT entries needed for a given symbol. We might need more
1758 than one got entry per symbol. */
1759struct got_entry
1760{
1761 struct got_entry *next;
1762
1763 /* The symbol addend that we'll be placing in the GOT. */
1764 bfd_vma addend;
1765
1766 /* Unlike other ELF targets, we use separate GOT entries for the same
1767 symbol referenced from different input files. This is to support
1768 automatic multiple TOC/GOT sections, where the TOC base can vary
1769 from one input file to another. After partitioning into TOC groups
1770 we merge entries within the group.
1771
1772 Point to the BFD owning this GOT entry. */
1773 bfd *owner;
1774
1775 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1776 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1777 unsigned char tls_type;
927be08e
AM
1778
1779 /* Non-zero if got.ent points to real entry. */
f961d9dd 1780 unsigned char is_indirect;
927be08e
AM
1781
1782 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1783 union
2cdcc330
AM
1784 {
1785 bfd_signed_vma refcount;
1786 bfd_vma offset;
1787 struct got_entry *ent;
1788 } got;
927be08e
AM
1789};
1790
1791/* The same for PLT. */
1792struct plt_entry
1793{
1794 struct plt_entry *next;
1795
1796 bfd_vma addend;
1797
1798 union
2cdcc330
AM
1799 {
1800 bfd_signed_vma refcount;
1801 bfd_vma offset;
1802 } plt;
927be08e
AM
1803};
1804
e717da7e
AM
1805struct ppc64_elf_obj_tdata
1806{
1807 struct elf_obj_tdata elf;
1808
1809 /* Shortcuts to dynamic linker sections. */
1810 asection *got;
1811 asection *relgot;
1812
b3fac117
AM
1813 /* Used during garbage collection. We attach global symbols defined
1814 on removed .opd entries to this section so that the sym is removed. */
1815 asection *deleted_section;
81688140 1816
927be08e 1817 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1818 sections means we potentially need one of these for each input bfd. */
927be08e 1819 struct got_entry tlsld_got;
8860955f 1820
2cdcc330
AM
1821 union
1822 {
729eabd5
AM
1823 /* A copy of relocs before they are modified for --emit-relocs. */
1824 Elf_Internal_Rela *relocs;
1825
1826 /* Section contents. */
1827 bfd_byte *contents;
1828 } opd;
d77c8a4b
AM
1829
1830 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1831 the reloc to be in the range -32768 to 32767. */
98528052
AM
1832 unsigned int has_small_toc_reloc : 1;
1833
560c8763
AM
1834 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1835 instruction not one we handle. */
1836 unsigned int unexpected_toc_insn : 1;
066f4018 1837
903b777d
AM
1838 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1839 this file. */
1840 unsigned int has_optrel : 1;
e717da7e
AM
1841};
1842
1843#define ppc64_elf_tdata(bfd) \
1844 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1845
1846#define ppc64_tlsld_got(bfd) \
1847 (&ppc64_elf_tdata (bfd)->tlsld_got)
1848
0c8d6e5c
AM
1849#define is_ppc64_elf(bfd) \
1850 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1851 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1852
e717da7e
AM
1853/* Override the generic function because we store some extras. */
1854
0a1b45a2 1855static bool
e717da7e
AM
1856ppc64_elf_mkobject (bfd *abfd)
1857{
0ffa91dd 1858 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1859 PPC64_ELF_DATA);
e717da7e
AM
1860}
1861
feee612b 1862/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1863 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1864
0a1b45a2 1865static bool
4ce794b7 1866ppc64_elf_object_p (bfd *abfd)
feee612b 1867{
14b57c7c 1868 if (!abfd->arch_info->the_default)
0a1b45a2 1869 return true;
14b57c7c
AM
1870
1871 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1872 {
1873 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1874
1875 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1876 {
1877 /* Relies on arch after 32 bit default being 64 bit default. */
1878 abfd->arch_info = abfd->arch_info->next;
1879 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1880 }
1881 }
14b57c7c 1882 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1883}
1884
d37c89e5
AM
1885/* Support for core dump NOTE sections. */
1886
0a1b45a2 1887static bool
d37c89e5
AM
1888ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1889{
eea6121a 1890 size_t offset, size;
d37c89e5
AM
1891
1892 if (note->descsz != 504)
0a1b45a2 1893 return false;
d37c89e5
AM
1894
1895 /* pr_cursig */
228e534f 1896 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1897
1898 /* pr_pid */
228e534f 1899 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1900
1901 /* pr_reg */
1902 offset = 112;
eea6121a 1903 size = 384;
d37c89e5
AM
1904
1905 /* Make a ".reg/999" section. */
1906 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1907 size, note->descpos + offset);
d37c89e5
AM
1908}
1909
0a1b45a2 1910static bool
d37c89e5
AM
1911ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1912{
1913 if (note->descsz != 136)
0a1b45a2 1914 return false;
d37c89e5 1915
228e534f 1916 elf_tdata (abfd)->core->pid
bc989cdc 1917 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1918 elf_tdata (abfd)->core->program
d37c89e5 1919 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1920 elf_tdata (abfd)->core->command
d37c89e5
AM
1921 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1922
0a1b45a2 1923 return true;
d37c89e5
AM
1924}
1925
183e98be
AM
1926static char *
1927ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1928 ...)
1929{
1930 switch (note_type)
1931 {
1932 default:
1933 return NULL;
1934
1935 case NT_PRPSINFO:
1936 {
9ef6d1e3 1937 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1938 va_list ap;
1939
1940 va_start (ap, note_type);
75cd47ed 1941 memset (data, 0, sizeof (data));
183e98be 1942 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1943#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1944 DIAGNOSTIC_PUSH;
be3e27bb 1945 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1946 -Wstringop-truncation:
1947 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1948 */
95da9854
L
1949 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1950#endif
183e98be 1951 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1952#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1953 DIAGNOSTIC_POP;
fe75810f 1954#endif
183e98be
AM
1955 va_end (ap);
1956 return elfcore_write_note (abfd, buf, bufsiz,
1957 "CORE", note_type, data, sizeof (data));
1958 }
1959
1960 case NT_PRSTATUS:
1961 {
1962 char data[504];
1963 va_list ap;
1964 long pid;
1965 int cursig;
1966 const void *greg;
1967
1968 va_start (ap, note_type);
1969 memset (data, 0, 112);
1970 pid = va_arg (ap, long);
1971 bfd_put_32 (abfd, pid, data + 32);
1972 cursig = va_arg (ap, int);
1973 bfd_put_16 (abfd, cursig, data + 12);
1974 greg = va_arg (ap, const void *);
1975 memcpy (data + 112, greg, 384);
1976 memset (data + 496, 0, 8);
1977 va_end (ap);
1978 return elfcore_write_note (abfd, buf, bufsiz,
1979 "CORE", note_type, data, sizeof (data));
1980 }
1981 }
1982}
1983
5d35169e
AM
1984/* Add extra PPC sections. */
1985
2cdcc330 1986static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1987{
07d6d2b8
AM
1988 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1989 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1990 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1991 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1993 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1994 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1995};
1996
7c8fe5c4
AM
1997enum _ppc64_sec_type {
1998 sec_normal = 0,
1999 sec_opd = 1,
2000 sec_toc = 2
2001};
2002
f0abc2a1
AM
2003struct _ppc64_elf_section_data
2004{
2005 struct bfd_elf_section_data elf;
411e1bfb 2006
f0abc2a1
AM
2007 union
2008 {
51aecdc5
AM
2009 /* An array with one entry for each opd function descriptor,
2010 and some spares since opd entries may be either 16 or 24 bytes. */
2011#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
2012 struct _opd_sec_data
2013 {
2014 /* Points to the function code section for local opd entries. */
2015 asection **func_sec;
2016
2017 /* After editing .opd, adjust references to opd local syms. */
2018 long *adjust;
2019 } opd;
7c8fe5c4 2020
3a71aa26
AM
2021 /* An array for toc sections, indexed by offset/8. */
2022 struct _toc_sec_data
2023 {
2024 /* Specifies the relocation symbol index used at a given toc offset. */
2025 unsigned *symndx;
2026
2027 /* And the relocation addend. */
2028 bfd_vma *add;
2029 } toc;
7c8fe5c4
AM
2030 } u;
2031
2032 enum _ppc64_sec_type sec_type:2;
411e1bfb 2033
7c8fe5c4
AM
2034 /* Flag set when small branches are detected. Used to
2035 select suitable defaults for the stub group size. */
2036 unsigned int has_14bit_branch:1;
3e04d765
AM
2037
2038 /* Flag set when PLTCALL relocs are detected. */
2039 unsigned int has_pltcall:1;
066f4018 2040
903b777d
AM
2041 /* Flag set when section has PLT/GOT/TOC relocations that can be
2042 optimised. */
2043 unsigned int has_optrel:1;
f0abc2a1
AM
2044};
2045
2046#define ppc64_elf_section_data(sec) \
411e1bfb 2047 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1 2048
0a1b45a2 2049static bool
4ce794b7 2050ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2051{
f592407e
AM
2052 if (!sec->used_by_bfd)
2053 {
2054 struct _ppc64_elf_section_data *sdata;
986f0783 2055 size_t amt = sizeof (*sdata);
f0abc2a1 2056
f592407e
AM
2057 sdata = bfd_zalloc (abfd, amt);
2058 if (sdata == NULL)
0a1b45a2 2059 return false;
f592407e
AM
2060 sec->used_by_bfd = sdata;
2061 }
f0abc2a1
AM
2062
2063 return _bfd_elf_new_section_hook (abfd, sec);
2064}
4025353c 2065
0a1b45a2 2066static bool
bf577467
AM
2067ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2068{
2069 const char *name = hdr->bfd_section->name;
2070
3f3328b8
ML
2071 if (startswith (name, ".sbss")
2072 || startswith (name, ".sdata"))
bf577467
AM
2073 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2074
0a1b45a2 2075 return true;
bf577467
AM
2076}
2077
74f0fb50 2078static struct _opd_sec_data *
4025353c
AM
2079get_opd_info (asection * sec)
2080{
2081 if (sec != NULL
2082 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2083 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2084 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2085 return NULL;
2086}
90e3cdf2
JJ
2087\f
2088/* Parameters for the qsort hook. */
0a1b45a2 2089static bool synthetic_relocatable;
8cb1c2c8 2090static const asection *synthetic_opd;
90e3cdf2 2091
699733f6 2092/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2093
2094static int
2095compare_symbols (const void *ap, const void *bp)
2096{
2cdcc330
AM
2097 const asymbol *a = *(const asymbol **) ap;
2098 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2099
699733f6
AM
2100 /* Section symbols first. */
2101 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2102 return -1;
699733f6 2103 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2104 return 1;
2105
699733f6 2106 /* then .opd symbols. */
cd285db5
AM
2107 if (synthetic_opd != NULL)
2108 {
2109 if (strcmp (a->section->name, ".opd") == 0
2110 && strcmp (b->section->name, ".opd") != 0)
2111 return -1;
2112 if (strcmp (a->section->name, ".opd") != 0
2113 && strcmp (b->section->name, ".opd") == 0)
2114 return 1;
2115 }
90e3cdf2 2116
699733f6 2117 /* then other code symbols. */
2cdcc330
AM
2118 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2119 == (SEC_CODE | SEC_ALLOC))
2120 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2121 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2122 return -1;
2123
2cdcc330
AM
2124 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2125 != (SEC_CODE | SEC_ALLOC))
2126 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2127 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2128 return 1;
2129
2130 if (synthetic_relocatable)
2131 {
2132 if (a->section->id < b->section->id)
2133 return -1;
2134
2135 if (a->section->id > b->section->id)
2136 return 1;
2137 }
2138
2139 if (a->value + a->section->vma < b->value + b->section->vma)
2140 return -1;
2141
2142 if (a->value + a->section->vma > b->value + b->section->vma)
2143 return 1;
2144
4d35a0aa
AM
2145 /* For syms with the same value, prefer strong dynamic global function
2146 syms over other syms. */
2147 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2148 return -1;
2149
2150 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2151 return 1;
2152
2153 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2154 return -1;
2155
2156 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2157 return 1;
2158
2159 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2160 return -1;
2161
2162 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2163 return 1;
2164
2165 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2166 return -1;
2167
2168 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2169 return 1;
2170
dcea6a95
AM
2171 /* Finally, sort on where the symbol is in memory. The symbols will
2172 be in at most two malloc'd blocks, one for static syms, one for
2173 dynamic syms, and we distinguish the two blocks above by testing
2174 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2175 originally in the same order as the symbols (and we're not
2176 sorting the symbols themselves), this ensures a stable sort. */
2177 if (a < b)
2178 return -1;
2179 if (a > b)
2180 return 1;
2181 return 0;
90e3cdf2
JJ
2182}
2183
699733f6 2184/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2185
699733f6 2186static asymbol *
9ad9b810
AM
2187sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2188 bfd_vma value)
90e3cdf2 2189{
9ad9b810 2190 size_t mid;
90e3cdf2 2191
7292b3ac 2192 if (id == (unsigned) -1)
699733f6
AM
2193 {
2194 while (lo < hi)
2195 {
2196 mid = (lo + hi) >> 1;
2197 if (syms[mid]->value + syms[mid]->section->vma < value)
2198 lo = mid + 1;
2199 else if (syms[mid]->value + syms[mid]->section->vma > value)
2200 hi = mid;
2201 else
2202 return syms[mid];
2203 }
2204 }
2205 else
2206 {
2207 while (lo < hi)
2208 {
2209 mid = (lo + hi) >> 1;
2210 if (syms[mid]->section->id < id)
2211 lo = mid + 1;
2212 else if (syms[mid]->section->id > id)
2213 hi = mid;
2214 else if (syms[mid]->value < value)
2215 lo = mid + 1;
2216 else if (syms[mid]->value > value)
2217 hi = mid;
2218 else
2219 return syms[mid];
2220 }
2221 }
2222 return NULL;
90e3cdf2
JJ
2223}
2224
0a1b45a2 2225static bool
468392fb
AM
2226section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2227{
2228 bfd_vma vma = *(bfd_vma *) ptr;
2229 return ((section->flags & SEC_ALLOC) != 0
2230 && section->vma <= vma
2231 && vma < section->vma + section->size);
2232}
2233
699733f6 2234/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2235 entry syms. Also generate @plt symbols for the glink branch table.
2236 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2237
2238static long
a7535cf3
AM
2239ppc64_elf_get_synthetic_symtab (bfd *abfd,
2240 long static_count, asymbol **static_syms,
2241 long dyn_count, asymbol **dyn_syms,
c9727e01 2242 asymbol **ret)
90e3cdf2
JJ
2243{
2244 asymbol *s;
0ccf57bd 2245 size_t i, j, count;
90e3cdf2 2246 char *names;
0ccf57bd 2247 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2248 asection *opd = NULL;
0a1b45a2 2249 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2250 asymbol **syms;
ee67d69a 2251 int abi = abiversion (abfd);
90e3cdf2
JJ
2252
2253 *ret = NULL;
2254
ee67d69a
AM
2255 if (abi < 2)
2256 {
2257 opd = bfd_get_section_by_name (abfd, ".opd");
2258 if (opd == NULL && abi == 1)
2259 return 0;
2260 }
90e3cdf2 2261
a5259595
AM
2262 syms = NULL;
2263 codesecsym = 0;
2264 codesecsymend = 0;
2265 secsymend = 0;
2266 opdsymend = 0;
2267 symcount = 0;
2268 if (opd != NULL)
c9727e01 2269 {
a5259595
AM
2270 symcount = static_count;
2271 if (!relocatable)
2272 symcount += dyn_count;
2273 if (symcount == 0)
2274 return 0;
c9727e01 2275
a5259595
AM
2276 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2277 if (syms == NULL)
2278 return -1;
90e3cdf2 2279
a5259595
AM
2280 if (!relocatable && static_count != 0 && dyn_count != 0)
2281 {
2282 /* Use both symbol tables. */
2283 memcpy (syms, static_syms, static_count * sizeof (*syms));
2284 memcpy (syms + static_count, dyn_syms,
2285 (dyn_count + 1) * sizeof (*syms));
2286 }
2287 else if (!relocatable && static_count == 0)
2288 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2289 else
2290 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2291
0ccf57bd
AM
2292 /* Trim uninteresting symbols. Interesting symbols are section,
2293 function, and notype symbols. */
2294 for (i = 0, j = 0; i < symcount; ++i)
2295 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2296 | BSF_RELC | BSF_SRELC)) == 0)
2297 syms[j++] = syms[i];
2298 symcount = j;
2299
a5259595
AM
2300 synthetic_relocatable = relocatable;
2301 synthetic_opd = opd;
2302 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2303
a5259595
AM
2304 if (!relocatable && symcount > 1)
2305 {
bfa5bd2a
PA
2306 /* Trim duplicate syms, since we may have merged the normal
2307 and dynamic symbols. Actually, we only care about syms
2308 that have different values, so trim any with the same
2309 value. Don't consider ifunc and ifunc resolver symbols
2310 duplicates however, because GDB wants to know whether a
2311 text symbol is an ifunc resolver. */
a5259595 2312 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2313 {
2314 const asymbol *s0 = syms[i - 1];
2315 const asymbol *s1 = syms[i];
2316
2317 if ((s0->value + s0->section->vma
2318 != s1->value + s1->section->vma)
2319 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2320 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2321 syms[j++] = syms[i];
2322 }
a5259595
AM
2323 symcount = j;
2324 }
699733f6 2325
a5259595
AM
2326 i = 0;
2327 /* Note that here and in compare_symbols we can't compare opd and
2328 sym->section directly. With separate debug info files, the
2329 symbols will be extracted from the debug file while abfd passed
2330 to this function is the real binary. */
8bd10d6b
AM
2331 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2332 && strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2333 ++i;
2334 codesecsym = i;
2335
2336 for (; i < symcount; ++i)
2337 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2338 | SEC_THREAD_LOCAL))
2339 != (SEC_CODE | SEC_ALLOC))
2340 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2341 break;
2342 codesecsymend = i;
2343
2344 for (; i < symcount; ++i)
2345 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2346 break;
2347 secsymend = i;
2348
2349 for (; i < symcount; ++i)
2350 if (strcmp (syms[i]->section->name, ".opd") != 0)
2351 break;
2352 opdsymend = i;
2353
2354 for (; i < symcount; ++i)
2cdcc330
AM
2355 if (((syms[i]->section->flags
2356 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2357 != (SEC_CODE | SEC_ALLOC))
2358 break;
2359 symcount = i;
2360 }
c9727e01 2361 count = 0;
90e3cdf2 2362
699733f6 2363 if (relocatable)
90e3cdf2 2364 {
0a1b45a2 2365 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
699733f6
AM
2366 arelent *r;
2367 size_t size;
0ccf57bd 2368 size_t relcount;
90e3cdf2 2369
468392fb
AM
2370 if (opdsymend == secsymend)
2371 goto done;
2372
699733f6 2373 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2374 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2375 if (relcount == 0)
c9727e01 2376 goto done;
90e3cdf2 2377
0a1b45a2 2378 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
7356fed5
AM
2379 {
2380 count = -1;
2381 goto done;
2382 }
2383
699733f6 2384 size = 0;
595da8c5 2385 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2386 {
2387 asymbol *sym;
90e3cdf2 2388
595da8c5 2389 while (r < opd->relocation + relcount
699733f6
AM
2390 && r->address < syms[i]->value + opd->vma)
2391 ++r;
90e3cdf2 2392
595da8c5 2393 if (r == opd->relocation + relcount)
699733f6 2394 break;
90e3cdf2 2395
699733f6
AM
2396 if (r->address != syms[i]->value + opd->vma)
2397 continue;
90e3cdf2 2398
699733f6
AM
2399 if (r->howto->type != R_PPC64_ADDR64)
2400 continue;
90e3cdf2 2401
699733f6
AM
2402 sym = *r->sym_ptr_ptr;
2403 if (!sym_exists_at (syms, opdsymend, symcount,
2404 sym->section->id, sym->value + r->addend))
2405 {
2406 ++count;
2407 size += sizeof (asymbol);
2408 size += strlen (syms[i]->name) + 2;
2409 }
2410 }
90e3cdf2 2411
c4b0b099
AM
2412 if (size == 0)
2413 goto done;
699733f6
AM
2414 s = *ret = bfd_malloc (size);
2415 if (s == NULL)
2416 {
7356fed5 2417 count = -1;
c9727e01 2418 goto done;
699733f6 2419 }
90e3cdf2 2420
699733f6 2421 names = (char *) (s + count);
90e3cdf2 2422
595da8c5 2423 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2424 {
699733f6 2425 asymbol *sym;
90e3cdf2 2426
595da8c5 2427 while (r < opd->relocation + relcount
699733f6
AM
2428 && r->address < syms[i]->value + opd->vma)
2429 ++r;
90e3cdf2 2430
595da8c5 2431 if (r == opd->relocation + relcount)
699733f6
AM
2432 break;
2433
2434 if (r->address != syms[i]->value + opd->vma)
2435 continue;
2436
2437 if (r->howto->type != R_PPC64_ADDR64)
2438 continue;
90e3cdf2 2439
699733f6
AM
2440 sym = *r->sym_ptr_ptr;
2441 if (!sym_exists_at (syms, opdsymend, symcount,
2442 sym->section->id, sym->value + r->addend))
2443 {
2444 size_t len;
2445
2446 *s = *syms[i];
6ba2a415 2447 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2448 s->section = sym->section;
2449 s->value = sym->value + r->addend;
2450 s->name = names;
2451 *names++ = '.';
2452 len = strlen (syms[i]->name);
2453 memcpy (names, syms[i]->name, len + 1);
2454 names += len + 1;
6f610d07
UW
2455 /* Have udata.p point back to the original symbol this
2456 synthetic symbol was derived from. */
2457 s->udata.p = syms[i];
699733f6
AM
2458 s++;
2459 }
2460 }
2461 }
2462 else
90e3cdf2 2463 {
0a1b45a2 2464 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
ee67d69a 2465 bfd_byte *contents = NULL;
699733f6 2466 size_t size;
0ccf57bd 2467 size_t plt_count = 0;
468392fb
AM
2468 bfd_vma glink_vma = 0, resolv_vma = 0;
2469 asection *dynamic, *glink = NULL, *relplt = NULL;
2470 arelent *p;
90e3cdf2 2471
ee67d69a 2472 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2473 {
c4b0b099
AM
2474 free_contents_and_exit_err:
2475 count = -1;
ee67d69a 2476 free_contents_and_exit:
c9594989 2477 free (contents);
c9727e01 2478 goto done;
699733f6 2479 }
90e3cdf2 2480
699733f6
AM
2481 size = 0;
2482 for (i = secsymend; i < opdsymend; ++i)
2483 {
2484 bfd_vma ent;
90e3cdf2 2485
5ef11c02
AM
2486 /* Ignore bogus symbols. */
2487 if (syms[i]->value > opd->size - 8)
2488 continue;
2489
699733f6
AM
2490 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2491 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2492 {
2493 ++count;
2494 size += sizeof (asymbol);
2495 size += strlen (syms[i]->name) + 2;
2496 }
2497 }
90e3cdf2 2498
468392fb 2499 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2500 if (dyn_count != 0
2501 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2502 {
2503 bfd_byte *dynbuf, *extdyn, *extdynend;
2504 size_t extdynsize;
2505 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2506
2507 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2508 goto free_contents_and_exit_err;
468392fb
AM
2509
2510 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2511 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2512
2513 extdyn = dynbuf;
2514 extdynend = extdyn + dynamic->size;
2515 for (; extdyn < extdynend; extdyn += extdynsize)
2516 {
2517 Elf_Internal_Dyn dyn;
2518 (*swap_dyn_in) (abfd, extdyn, &dyn);
2519
2520 if (dyn.d_tag == DT_NULL)
2521 break;
2522
2523 if (dyn.d_tag == DT_PPC64_GLINK)
2524 {
9e390558
AM
2525 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2526 See comment in ppc64_elf_finish_dynamic_sections. */
2527 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2528 /* The .glink section usually does not survive the final
2529 link; search for the section (usually .text) where the
2530 glink stubs now reside. */
2531 glink = bfd_sections_find_if (abfd, section_covers_vma,
2532 &glink_vma);
2533 break;
2534 }
2535 }
2536
2537 free (dynbuf);
2538 }
2539
2540 if (glink != NULL)
2541 {
2542 /* Determine __glink trampoline by reading the relative branch
2543 from the first glink stub. */
2544 bfd_byte buf[4];
b9e5796b
AM
2545 unsigned int off = 0;
2546
2547 while (bfd_get_section_contents (abfd, glink, buf,
2548 glink_vma + off - glink->vma, 4))
468392fb
AM
2549 {
2550 unsigned int insn = bfd_get_32 (abfd, buf);
2551 insn ^= B_DOT;
2552 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2553 {
2cdcc330
AM
2554 resolv_vma
2555 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2556 break;
2557 }
2558 off += 4;
2559 if (off > 4)
2560 break;
468392fb
AM
2561 }
2562
2563 if (resolv_vma)
2564 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2565
066ee829
AM
2566 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2567 if (relplt != NULL)
2568 {
2569 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 2570 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
c4b0b099 2571 goto free_contents_and_exit_err;
68ffbac6 2572
066ee829
AM
2573 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2574 size += plt_count * sizeof (asymbol);
468392fb 2575
066ee829
AM
2576 p = relplt->relocation;
2577 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2578 {
2579 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2580 if (p->addend != 0)
2581 size += sizeof ("+0x") - 1 + 16;
2582 }
066ee829 2583 }
468392fb
AM
2584 }
2585
c4b0b099
AM
2586 if (size == 0)
2587 goto free_contents_and_exit;
699733f6
AM
2588 s = *ret = bfd_malloc (size);
2589 if (s == NULL)
c4b0b099 2590 goto free_contents_and_exit_err;
90e3cdf2 2591
468392fb 2592 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2593
699733f6 2594 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2595 {
699733f6 2596 bfd_vma ent;
90e3cdf2 2597
5ef11c02
AM
2598 if (syms[i]->value > opd->size - 8)
2599 continue;
2600
699733f6
AM
2601 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2602 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2603 {
0ccf57bd 2604 size_t lo, hi;
699733f6 2605 size_t len;
c9727e01 2606 asection *sec = abfd->sections;
90e3cdf2 2607
699733f6
AM
2608 *s = *syms[i];
2609 lo = codesecsym;
2610 hi = codesecsymend;
2611 while (lo < hi)
2612 {
0ccf57bd 2613 size_t mid = (lo + hi) >> 1;
699733f6
AM
2614 if (syms[mid]->section->vma < ent)
2615 lo = mid + 1;
2616 else if (syms[mid]->section->vma > ent)
2617 hi = mid;
2618 else
c9727e01
AM
2619 {
2620 sec = syms[mid]->section;
2621 break;
2622 }
699733f6
AM
2623 }
2624
c9727e01 2625 if (lo >= hi && lo > codesecsym)
699733f6 2626 sec = syms[lo - 1]->section;
699733f6
AM
2627
2628 for (; sec != NULL; sec = sec->next)
2629 {
2630 if (sec->vma > ent)
2631 break;
63524580
JK
2632 /* SEC_LOAD may not be set if SEC is from a separate debug
2633 info file. */
2634 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2635 break;
2636 if ((sec->flags & SEC_CODE) != 0)
2637 s->section = sec;
2638 }
6ba2a415 2639 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2640 s->value = ent - s->section->vma;
2641 s->name = names;
2642 *names++ = '.';
2643 len = strlen (syms[i]->name);
2644 memcpy (names, syms[i]->name, len + 1);
2645 names += len + 1;
6f610d07
UW
2646 /* Have udata.p point back to the original symbol this
2647 synthetic symbol was derived from. */
2648 s->udata.p = syms[i];
699733f6 2649 s++;
90e3cdf2 2650 }
90e3cdf2 2651 }
699733f6 2652 free (contents);
468392fb
AM
2653
2654 if (glink != NULL && relplt != NULL)
2655 {
2656 if (resolv_vma)
2657 {
2658 /* Add a symbol for the main glink trampoline. */
86a4952b 2659 memset (s, 0, sizeof *s);
468392fb 2660 s->the_bfd = abfd;
6ba2a415 2661 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2662 s->section = glink;
2663 s->value = resolv_vma - glink->vma;
2664 s->name = names;
2cdcc330
AM
2665 memcpy (names, "__glink_PLTresolve",
2666 sizeof ("__glink_PLTresolve"));
468392fb
AM
2667 names += sizeof ("__glink_PLTresolve");
2668 s++;
2669 count++;
2670 }
2671
2672 /* FIXME: It would be very much nicer to put sym@plt on the
2673 stub rather than on the glink branch table entry. The
2674 objdump disassembler would then use a sensible symbol
2675 name on plt calls. The difficulty in doing so is
2676 a) finding the stubs, and,
2677 b) matching stubs against plt entries, and,
2678 c) there can be multiple stubs for a given plt entry.
2679
2680 Solving (a) could be done by code scanning, but older
2681 ppc64 binaries used different stubs to current code.
2682 (b) is the tricky one since you need to known the toc
2683 pointer for at least one function that uses a pic stub to
2684 be able to calculate the plt address referenced.
2685 (c) means gdb would need to set multiple breakpoints (or
2686 find the glink branch itself) when setting breakpoints
2687 for pending shared library loads. */
2688 p = relplt->relocation;
2689 for (i = 0; i < plt_count; i++, p++)
2690 {
2691 size_t len;
2692
2693 *s = **p->sym_ptr_ptr;
2694 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2695 we are defining a symbol, ensure one of them is set. */
2696 if ((s->flags & BSF_LOCAL) == 0)
2697 s->flags |= BSF_GLOBAL;
6ba2a415 2698 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2699 s->section = glink;
2700 s->value = glink_vma - glink->vma;
2701 s->name = names;
2702 s->udata.p = NULL;
2703 len = strlen ((*p->sym_ptr_ptr)->name);
2704 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2705 names += len;
e054468f
AM
2706 if (p->addend != 0)
2707 {
2708 memcpy (names, "+0x", sizeof ("+0x") - 1);
2709 names += sizeof ("+0x") - 1;
2710 bfd_sprintf_vma (abfd, names, p->addend);
2711 names += strlen (names);
2712 }
468392fb
AM
2713 memcpy (names, "@plt", sizeof ("@plt"));
2714 names += sizeof ("@plt");
2715 s++;
b9e5796b
AM
2716 if (abi < 2)
2717 {
2718 glink_vma += 8;
2719 if (i >= 0x8000)
2720 glink_vma += 4;
2721 }
2722 else
468392fb
AM
2723 glink_vma += 4;
2724 }
2725 count += plt_count;
2726 }
90e3cdf2
JJ
2727 }
2728
c9727e01 2729 done:
a7535cf3 2730 free (syms);
90e3cdf2
JJ
2731 return count;
2732}
5bd4f169 2733\f
65f38f15
AM
2734/* The following functions are specific to the ELF linker, while
2735 functions above are used generally. Those named ppc64_elf_* are
2736 called by the main ELF linker code. They appear in this file more
2737 or less in the order in which they are called. eg.
2738 ppc64_elf_check_relocs is called early in the link process,
2739 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2740 called.
2741
2742 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2743 functions have both a function code symbol and a function descriptor
2744 symbol. A call to foo in a relocatable object file looks like:
2745
2746 . .text
2747 . x:
2748 . bl .foo
2749 . nop
2750
2751 The function definition in another object file might be:
2752
2753 . .section .opd
2754 . foo: .quad .foo
2755 . .quad .TOC.@tocbase
2756 . .quad 0
2757 .
2758 . .text
2759 . .foo: blr
2760
2761 When the linker resolves the call during a static link, the branch
2762 unsurprisingly just goes to .foo and the .opd information is unused.
2763 If the function definition is in a shared library, things are a little
2764 different: The call goes via a plt call stub, the opd information gets
2765 copied to the plt, and the linker patches the nop.
2766
2767 . x:
2768 . bl .foo_stub
2769 . ld 2,40(1)
2770 .
2771 .
2772 . .foo_stub:
71a39c98
AM
2773 . std 2,40(1) # in practice, the call stub
2774 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2775 . addi 11,11,Lfoo@toc@l # this is the general idea
2776 . ld 12,0(11)
2777 . ld 2,8(11)
2778 . mtctr 12
2779 . ld 11,16(11)
e86ce104
AM
2780 . bctr
2781 .
2782 . .section .plt
2783 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2784
2785 The "reloc ()" notation is supposed to indicate that the linker emits
2786 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2787 copying.
2788
2789 What are the difficulties here? Well, firstly, the relocations
2790 examined by the linker in check_relocs are against the function code
2791 sym .foo, while the dynamic relocation in the plt is emitted against
2792 the function descriptor symbol, foo. Somewhere along the line, we need
2793 to carefully copy dynamic link information from one symbol to the other.
2794 Secondly, the generic part of the elf linker will make .foo a dynamic
2795 symbol as is normal for most other backends. We need foo dynamic
2796 instead, at least for an application final link. However, when
2797 creating a shared library containing foo, we need to have both symbols
2798 dynamic so that references to .foo are satisfied during the early
2799 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2800 definition from some other object, eg. a static library.
2801
2802 Update: As of August 2004, we support a new convention. Function
2803 calls may use the function descriptor symbol, ie. "bl foo". This
2804 behaves exactly as "bl .foo". */
65f38f15 2805
7c8bbca5
AM
2806/* Of those relocs that might be copied as dynamic relocs, this
2807 function selects those that must be copied when linking a shared
2808 library or PIE, even when the symbol is local. */
65f38f15 2809
1d483afe
AM
2810static int
2811must_be_dyn_reloc (struct bfd_link_info *info,
2812 enum elf_ppc64_reloc_type r_type)
2813{
2814 switch (r_type)
2815 {
2816 default:
7c8bbca5
AM
2817 /* Only relative relocs can be resolved when the object load
2818 address isn't fixed. DTPREL64 is excluded because the
2819 dynamic linker needs to differentiate global dynamic from
2820 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2821 return 1;
2822
2823 case R_PPC64_REL32:
2824 case R_PPC64_REL64:
2825 case R_PPC64_REL30:
1bdd8fac
AM
2826 case R_PPC64_TOC16:
2827 case R_PPC64_TOC16_DS:
2828 case R_PPC64_TOC16_LO:
2829 case R_PPC64_TOC16_HI:
2830 case R_PPC64_TOC16_HA:
2831 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2832 return 0;
2833
2834 case R_PPC64_TPREL16:
2835 case R_PPC64_TPREL16_LO:
2836 case R_PPC64_TPREL16_HI:
2837 case R_PPC64_TPREL16_HA:
2838 case R_PPC64_TPREL16_DS:
2839 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2840 case R_PPC64_TPREL16_HIGH:
2841 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2842 case R_PPC64_TPREL16_HIGHER:
2843 case R_PPC64_TPREL16_HIGHERA:
2844 case R_PPC64_TPREL16_HIGHEST:
2845 case R_PPC64_TPREL16_HIGHESTA:
2846 case R_PPC64_TPREL64:
c213164a 2847 case R_PPC64_TPREL34:
7c8bbca5
AM
2848 /* These relocations are relative but in a shared library the
2849 linker doesn't know the thread pointer base. */
2850 return bfd_link_dll (info);
1d483afe
AM
2851 }
2852}
65f38f15 2853
f4656909 2854/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2855 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2856 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2857 shared lib. With code that gcc generates it is vital that this be
2858 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2859 actually the address of a function descriptor which resides in the
2860 .opd section. gcc uses the descriptor directly rather than going
2861 via the GOT as some other ABIs do, which means that initialized
2862 function pointers reference the descriptor. Thus, a function
2863 pointer initialized to the address of a function in a shared
2864 library will either require a .dynbss copy and a copy reloc, or a
2865 dynamic reloc. Using a .dynbss copy redefines the function
2866 descriptor symbol to point to the copy. This presents a problem as
2867 a PLT entry for that function is also initialized from the function
2868 descriptor symbol and the copy may not be initialized first. */
a23b6845 2869#define ELIMINATE_COPY_RELOCS 1
f4656909 2870
721956f4 2871/* Section name for stubs is the associated section name plus this
29942be8
NC
2872 string. */
2873#define STUB_SUFFIX ".stub"
721956f4
AM
2874
2875/* Linker stubs.
2876 ppc_stub_long_branch:
2877 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2878 destination, but a 24 bit branch in a stub section will reach.
2879 . b dest
2880
2881 ppc_stub_plt_branch:
2882 Similar to the above, but a 24 bit branch in the stub section won't
2883 reach its destination.
6df4c9c2
AM
2884 . addis %r12,%r2,xxx@toc@ha
2885 . ld %r12,xxx@toc@l(%r12)
71a39c98 2886 . mtctr %r12
721956f4
AM
2887 . bctr
2888
2889 ppc_stub_plt_call:
2c66dc6c
AM
2890 Used to call a function in a shared library. If it so happens that
2891 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2892 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
7aba54da 2893 An r2save variant starts with "std %r2,40(%r1)".
71a39c98
AM
2894 . addis %r11,%r2,xxx@toc@ha
2895 . ld %r12,xxx+0@toc@l(%r11)
2896 . mtctr %r12
2897 . ld %r2,xxx+8@toc@l(%r11)
2898 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2899 . bctr
ad8e1ba5
AM
2900
2901 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2902 code to adjust the value and save r2 to support multiple toc sections.
2903 A ppc_stub_long_branch with an r2 offset looks like:
2904 . std %r2,40(%r1)
2905 . addis %r2,%r2,off@ha
2906 . addi %r2,%r2,off@l
2907 . b dest
2908
2909 A ppc_stub_plt_branch with an r2 offset looks like:
2910 . std %r2,40(%r1)
6df4c9c2
AM
2911 . addis %r12,%r2,xxx@toc@ha
2912 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2913 . addis %r2,%r2,off@ha
2914 . addi %r2,%r2,off@l
71a39c98 2915 . mtctr %r12
ad8e1ba5 2916 . bctr
ac2df442 2917
05d0e962
AM
2918 All of the above stubs are shown as their ELFv1 variants. ELFv2
2919 variants exist too, simpler for plt calls since a new toc pointer
2920 and static chain are not loaded by the stub. In addition, ELFv2
2921 has some more complex stubs to handle calls marked with NOTOC
7aba54da
AM
2922 relocs from functions where r2 is not a valid toc pointer.
2923 ppc_stub_long_branch_p9notoc:
05d0e962
AM
2924 . mflr %r12
2925 . bcl 20,31,1f
2926 . 1:
2927 . mflr %r11
2928 . mtlr %r12
2cdcc330
AM
2929 . addis %r12,%r11,dest-1b@ha
2930 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2931 . b dest
2932
7aba54da 2933 ppc_stub_plt_branch_p9notoc:
05d0e962
AM
2934 . mflr %r12
2935 . bcl 20,31,1f
2936 . 1:
2937 . mflr %r11
2938 . mtlr %r12
2939 . lis %r12,xxx-1b@highest
f891966f 2940 . ori %r12,%r12,xxx-1b@higher
05d0e962 2941 . sldi %r12,%r12,32
f891966f 2942 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2943 . ori %r12,%r12,xxx-1b@l
2944 . add %r12,%r11,%r12
2945 . mtctr %r12
2946 . bctr
2947
7aba54da 2948 ppc_stub_plt_call_p9notoc:
05d0e962
AM
2949 . mflr %r12
2950 . bcl 20,31,1f
2951 . 1:
2952 . mflr %r11
2953 . mtlr %r12
2954 . lis %r12,xxx-1b@highest
f891966f 2955 . ori %r12,%r12,xxx-1b@higher
05d0e962 2956 . sldi %r12,%r12,32
f891966f 2957 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2958 . ori %r12,%r12,xxx-1b@l
2959 . ldx %r12,%r11,%r12
2960 . mtctr %r12
2961 . bctr
2962
7c1f4227 2963 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2964 ppc_stub_long_branch_notoc:
d4b87b1e 2965 . pla %r12,dest@pcrel
04bdff6a
AM
2966 . b dest
2967 ppc_stub_plt_branch_notoc:
2968 . lis %r11,(dest-1f)@highesta34
2969 . ori %r11,%r11,(dest-1f)@highera34
2970 . sldi %r11,%r11,34
d4b87b1e 2971 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2972 . add %r12,%r11,%r12
2973 . mtctr %r12
2974 . bctr
2975 ppc_stub_plt_call_notoc:
2976 . lis %r11,(xxx-1f)@highesta34
2977 . ori %r11,%r11,(xxx-1f)@highera34
2978 . sldi %r11,%r11,34
d4b87b1e 2979 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2980 . ldx %r12,%r11,%r12
2981 . mtctr %r12
2982 . bctr
2983
05d0e962
AM
2984 In cases where the high instructions would add zero, they are
2985 omitted and following instructions modified in some cases.
7c1f4227 2986 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2987 to
2988 . pld %r12,xxx@pcrel
2989 . mtctr %r12
2990 . bctr
05d0e962 2991
7aba54da 2992 Stub variants may be merged. For example, if printf is called from
05d0e962
AM
2993 code with the tocsave optimization (ie. r2 saved in function
2994 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2995 and from other code without the tocsave optimization requiring a
2996 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2997 type will be created. Calls with the tocsave optimization will
2998 enter this stub after the instruction saving r2. A similar
2999 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3000 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3001 to call an external function like printf. If other calls to printf
3002 require a ppc_stub_plt_call linkage stub then a single
7aba54da
AM
3003 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3004 call. */
3005
3006enum ppc_stub_main_type
2cdcc330 3007{
721956f4
AM
3008 ppc_stub_none,
3009 ppc_stub_long_branch,
3010 ppc_stub_plt_branch,
794e51c0 3011 ppc_stub_plt_call,
a4b6fadd
AM
3012 ppc_stub_global_entry,
3013 ppc_stub_save_res
721956f4
AM
3014};
3015
7aba54da
AM
3016/* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3017 these variations. */
3018
3019enum ppc_stub_sub_type
3020{
3021 ppc_stub_toc,
3022 ppc_stub_notoc,
3023 ppc_stub_p9notoc
3024};
3025
3026struct ppc_stub_type
3027{
3028 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3029 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3030 unsigned int r2save : 1;
3031};
3032
6f20ed8a
AM
3033/* Information on stub grouping. */
3034struct map_stub
3035{
3036 /* The stub section. */
3037 asection *stub_sec;
3038 /* This is the section to which stubs in the group will be attached. */
3039 asection *link_sec;
a4b6fadd
AM
3040 /* Next group. */
3041 struct map_stub *next;
3042 /* Whether to emit a copy of register save/restore functions in this
3043 group. */
3044 int needs_save_res;
df136d64
AM
3045 /* Current offset within stubs after the insn restoring lr in a
3046 _notoc or _both stub using bcl for pc-relative addressing, or
3047 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3048 unsigned int lr_restore;
3049 /* Accumulated size of EH info emitted to describe return address
3050 if stubs modify lr. Does not include 17 byte FDE header. */
3051 unsigned int eh_size;
3052 /* Offset in glink_eh_frame to the start of EH info for this group. */
3053 unsigned int eh_base;
6f20ed8a
AM
3054};
3055
2cdcc330
AM
3056struct ppc_stub_hash_entry
3057{
721956f4
AM
3058 /* Base hash table entry structure. */
3059 struct bfd_hash_entry root;
3060
7aba54da 3061 struct ppc_stub_type type;
ad8e1ba5 3062
6f20ed8a
AM
3063 /* Group information. */
3064 struct map_stub *group;
721956f4
AM
3065
3066 /* Offset within stub_sec of the beginning of this stub. */
3067 bfd_vma stub_offset;
3068
3069 /* Given the symbol's value and its section we can determine its final
3070 value when building the stubs (so the stub knows where to jump. */
3071 bfd_vma target_value;
3072 asection *target_section;
3073
721956f4
AM
3074 /* The symbol table entry, if any, that this was derived from. */
3075 struct ppc_link_hash_entry *h;
e054468f 3076 struct plt_entry *plt_ent;
721956f4 3077
2d7ad24e
AM
3078 /* Symbol type. */
3079 unsigned char symtype;
3080
6911b7dc
AM
3081 /* Symbol st_other. */
3082 unsigned char other;
721956f4
AM
3083};
3084
2cdcc330
AM
3085struct ppc_branch_hash_entry
3086{
721956f4
AM
3087 /* Base hash table entry structure. */
3088 struct bfd_hash_entry root;
3089
c456f082 3090 /* Offset within branch lookup table. */
721956f4
AM
3091 unsigned int offset;
3092
3093 /* Generation marker. */
3094 unsigned int iter;
3095};
65f38f15 3096
1657026c 3097/* Used to track dynamic relocations. */
19e08130
AM
3098struct ppc_dyn_relocs
3099{
3100 struct ppc_dyn_relocs *next;
3101
3102 /* The input section of the reloc. */
3103 asection *sec;
3104
3105 /* Total number of relocs copied for the input section. */
1657026c
AM
3106 unsigned int count;
3107
3108 /* Number of pc-relative relocs copied for the input section. */
3109 unsigned int pc_count;
3110
3111 /* Number of relocs that might become R_PPC64_RELATIVE. */
3112 unsigned int rel_count;
3113};
3114
3115struct ppc_local_dyn_relocs
3116{
3117 struct ppc_local_dyn_relocs *next;
3118
3119 /* The input section of the reloc. */
3120 asection *sec;
3121
3122 /* Total number of relocs copied for the input section. */
3123 unsigned int count;
3124
3125 /* Number of relocs that might become R_PPC64_RELATIVE. */
3126 unsigned int rel_count : 31;
19e08130
AM
3127
3128 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3129 unsigned int ifunc : 1;
3130};
3131
65f38f15
AM
3132struct ppc_link_hash_entry
3133{
3134 struct elf_link_hash_entry elf;
3135
2cdcc330
AM
3136 union
3137 {
b3fac117
AM
3138 /* A pointer to the most recently used stub hash entry against this
3139 symbol. */
3140 struct ppc_stub_hash_entry *stub_cache;
3141
3142 /* A pointer to the next symbol starting with a '.' */
3143 struct ppc_link_hash_entry *next_dot_sym;
3144 } u;
721956f4 3145
721956f4 3146 /* Link between function code and descriptor symbols. */
34814b9f 3147 struct ppc_link_hash_entry *oh;
721956f4 3148
e86ce104
AM
3149 /* Flag function code and descriptor symbols. */
3150 unsigned int is_func:1;
3151 unsigned int is_func_descriptor:1;
908b32fc 3152 unsigned int fake:1;
411e1bfb 3153
c5614fa4
AM
3154 /* Whether global opd/toc sym has been adjusted or not.
3155 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3156 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3157 unsigned int adjust_done:1;
3158
a4b6fadd
AM
3159 /* Set if this is an out-of-line register save/restore function,
3160 with non-standard calling convention. */
3161 unsigned int save_res:1;
3162
8b5f1ed8
AM
3163 /* Set if a duplicate symbol with non-zero localentry is detected,
3164 even when the duplicate symbol does not provide a definition. */
3165 unsigned int non_zero_localentry:1;
3166
411e1bfb 3167 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3168 Bits are or'd into the mask as the corresponding relocs are
3169 encountered during check_relocs, with TLS_TLS being set when any
3170 of the other TLS bits are set. tls_optimize clears bits when
3171 optimizing to indicate the corresponding GOT entry type is not
3172 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3173 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3174 These flags are also kept for local symbols. */
3175#define TLS_TLS 1 /* Any TLS reloc. */
3176#define TLS_GD 2 /* GD reloc. */
3177#define TLS_LD 4 /* LD reloc. */
3178#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3179#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3180#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3181#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3182#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3183 unsigned char tls_mask;
37da22e5
AM
3184
3185 /* The above field is also used to mark function symbols. In which
3186 case TLS_TLS will be 0. */
3187#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3188#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3189#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3190};
3191
ed7007c1
AM
3192static inline struct ppc_link_hash_entry *
3193ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3194{
3195 return (struct ppc_link_hash_entry *) ent;
3196}
3197
30845f11
AM
3198static inline struct elf_link_hash_entry *
3199elf_hash_entry (struct ppc_link_hash_entry *ent)
3200{
3201 return (struct elf_link_hash_entry *) ent;
3202}
3203
65f38f15
AM
3204/* ppc64 ELF linker hash table. */
3205
3206struct ppc_link_hash_table
3207{
3208 struct elf_link_hash_table elf;
3209
721956f4
AM
3210 /* The stub hash table. */
3211 struct bfd_hash_table stub_hash_table;
3212
3213 /* Another hash table for plt_branch stubs. */
3214 struct bfd_hash_table branch_hash_table;
3215
3b421ab3
AM
3216 /* Hash table for function prologue tocsave. */
3217 htab_t tocsave_htab;
3218
e7d1c40c
AM
3219 /* Various options and other info passed from the linker. */
3220 struct ppc64_elf_params *params;
721956f4 3221
6f20ed8a
AM
3222 /* The size of sec_info below. */
3223 unsigned int sec_info_arr_size;
3224
3225 /* Per-section array of extra section info. Done this way rather
3226 than as part of ppc64_elf_section_data so we have the info for
3227 non-ppc64 sections. */
3228 struct
3229 {
3230 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3231 bfd_vma toc_off;
6f20ed8a
AM
3232
3233 union
3234 {
3235 /* The section group that this section belongs to. */
3236 struct map_stub *group;
3237 /* A temp section list pointer. */
3238 asection *list;
3239 } u;
3240 } *sec_info;
721956f4 3241
a4b6fadd
AM
3242 /* Linked list of groups. */
3243 struct map_stub *group;
3244
ad8e1ba5
AM
3245 /* Temp used when calculating TOC pointers. */
3246 bfd_vma toc_curr;
bf102f86
AM
3247 bfd *toc_bfd;
3248 asection *toc_first_sec;
ad8e1ba5 3249
b3fac117
AM
3250 /* Used when adding symbols. */
3251 struct ppc_link_hash_entry *dot_syms;
3252
33e44f2e 3253 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3254 asection *glink;
9e390558 3255 asection *global_entry;
82bd7b59 3256 asection *sfpr;
2d7ad24e
AM
3257 asection *pltlocal;
3258 asection *relpltlocal;
4ce794b7
AM
3259 asection *brlt;
3260 asection *relbrlt;
58d180e8 3261 asection *glink_eh_frame;
ec338859 3262
8387904d
AM
3263 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3264 struct ppc_link_hash_entry *tls_get_addr;
3265 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3266 struct ppc_link_hash_entry *tga_desc;
3267 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3268 struct map_stub *tga_group;
411e1bfb 3269
927be08e
AM
3270 /* The size of reliplt used by got entry relocs. */
3271 bfd_size_type got_reli_size;
3272
1657026c
AM
3273 /* DT_RELR array of r_offset. */
3274 size_t relr_alloc;
3275 size_t relr_count;
3276 bfd_vma *relr_addr;
3277
9b5ecbd0 3278 /* Statistics. */
7aba54da 3279 unsigned long stub_count[ppc_stub_save_res];
9b5ecbd0 3280
ee75fd95
AM
3281 /* Number of stubs against global syms. */
3282 unsigned long stub_globals;
3283
ee67d69a
AM
3284 /* Set if we're linking code with function descriptors. */
3285 unsigned int opd_abi:1;
3286
4c52953f 3287 /* Support for multiple toc sections. */
33c0ec9d 3288 unsigned int do_multi_toc:1;
4c52953f 3289 unsigned int multi_toc_needed:1;
927be08e 3290 unsigned int second_toc_pass:1;
67f0cbdb 3291 unsigned int do_toc_opt:1;
4c52953f 3292
9a23f96e
AM
3293 /* Set if tls optimization is enabled. */
3294 unsigned int do_tls_opt:1;
3295
3e04d765
AM
3296 /* Set if inline plt calls should be converted to direct calls. */
3297 unsigned int can_convert_all_inline_plt:1;
3298
5d1634d7 3299 /* Set on error. */
99877b66 3300 unsigned int stub_error:1;
721956f4 3301
8c5b4e52
AM
3302 /* Whether func_desc_adjust needs to be run over symbols. */
3303 unsigned int need_func_desc_adj:1;
721956f4 3304
f378ab09
AM
3305 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3306 unsigned int has_plt_localentry0:1;
3307
5663e321
AM
3308 /* Whether calls are made via the PLT from NOTOC functions. */
3309 unsigned int notoc_plt:1;
3310
e10a07b3
AM
3311 /* Whether any code linked seems to be Power10. */
3312 unsigned int has_power10_relocs:1;
3313
721956f4
AM
3314 /* Incremented every time we size stubs. */
3315 unsigned int stub_iteration;
65f38f15
AM
3316};
3317
4c52953f
AM
3318/* Rename some of the generic section flags to better document how they
3319 are used here. */
b0dddeec
AM
3320
3321/* Nonzero if this section has TLS related relocations. */
3322#define has_tls_reloc sec_flg0
3323
9737e8af
AM
3324/* Nonzero if this section has a call to __tls_get_addr lacking marker
3325 relocations. */
3326#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3327
3328/* Nonzero if this section has any toc or got relocs. */
3329#define has_toc_reloc sec_flg2
3330
3331/* Nonzero if this section has a call to another section that uses
3332 the toc or got. */
d77c8a4b 3333#define makes_toc_func_call sec_flg3
b0dddeec
AM
3334
3335/* Recursion protection when determining above flag. */
d77c8a4b 3336#define call_check_in_progress sec_flg4
70cc837d 3337#define call_check_done sec_flg5
4c52953f 3338
65f38f15
AM
3339/* Get the ppc64 ELF linker hash table from a link_info structure. */
3340
3341#define ppc_hash_table(p) \
573eb292
AM
3342 ((is_elf_hash_table ((p)->hash) \
3343 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3344 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3345
721956f4
AM
3346#define ppc_stub_hash_lookup(table, string, create, copy) \
3347 ((struct ppc_stub_hash_entry *) \
3348 bfd_hash_lookup ((table), (string), (create), (copy)))
3349
3350#define ppc_branch_hash_lookup(table, string, create, copy) \
3351 ((struct ppc_branch_hash_entry *) \
3352 bfd_hash_lookup ((table), (string), (create), (copy)))
3353
3354/* Create an entry in the stub hash table. */
3355
3356static struct bfd_hash_entry *
4ce794b7
AM
3357stub_hash_newfunc (struct bfd_hash_entry *entry,
3358 struct bfd_hash_table *table,
3359 const char *string)
721956f4
AM
3360{
3361 /* Allocate the structure if it has not already been allocated by a
3362 subclass. */
3363 if (entry == NULL)
3364 {
3365 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3366 if (entry == NULL)
3367 return entry;
3368 }
3369
3370 /* Call the allocation method of the superclass. */
3371 entry = bfd_hash_newfunc (entry, table, string);
3372 if (entry != NULL)
3373 {
3374 struct ppc_stub_hash_entry *eh;
3375
3376 /* Initialize the local fields. */
3377 eh = (struct ppc_stub_hash_entry *) entry;
7aba54da
AM
3378 eh->type.main = ppc_stub_none;
3379 eh->type.sub = ppc_stub_toc;
3380 eh->type.r2save = 0;
6f20ed8a 3381 eh->group = NULL;
721956f4
AM
3382 eh->stub_offset = 0;
3383 eh->target_value = 0;
3384 eh->target_section = NULL;
721956f4 3385 eh->h = NULL;
6911b7dc 3386 eh->plt_ent = NULL;
6911b7dc 3387 eh->other = 0;
721956f4
AM
3388 }
3389
3390 return entry;
3391}
3392
3393/* Create an entry in the branch hash table. */
3394
3395static struct bfd_hash_entry *
4ce794b7
AM
3396branch_hash_newfunc (struct bfd_hash_entry *entry,
3397 struct bfd_hash_table *table,
3398 const char *string)
721956f4
AM
3399{
3400 /* Allocate the structure if it has not already been allocated by a
3401 subclass. */
3402 if (entry == NULL)
3403 {
3404 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3405 if (entry == NULL)
3406 return entry;
3407 }
3408
3409 /* Call the allocation method of the superclass. */
3410 entry = bfd_hash_newfunc (entry, table, string);
3411 if (entry != NULL)
3412 {
3413 struct ppc_branch_hash_entry *eh;
3414
3415 /* Initialize the local fields. */
3416 eh = (struct ppc_branch_hash_entry *) entry;
3417 eh->offset = 0;
3418 eh->iter = 0;
3419 }
3420
3421 return entry;
3422}
3423
65f38f15
AM
3424/* Create an entry in a ppc64 ELF linker hash table. */
3425
3426static struct bfd_hash_entry *
4ce794b7
AM
3427link_hash_newfunc (struct bfd_hash_entry *entry,
3428 struct bfd_hash_table *table,
3429 const char *string)
65f38f15
AM
3430{
3431 /* Allocate the structure if it has not already been allocated by a
3432 subclass. */
3433 if (entry == NULL)
3434 {
3435 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3436 if (entry == NULL)
3437 return entry;
3438 }
3439
3440 /* Call the allocation method of the superclass. */
3441 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3442 if (entry != NULL)
3443 {
3444 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3445
b3fac117 3446 memset (&eh->u.stub_cache, 0,
908b32fc 3447 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3448 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3449
3450 /* When making function calls, old ABI code references function entry
3451 points (dot symbols), while new ABI code references the function
3452 descriptor symbol. We need to make any combination of reference and
3453 definition work together, without breaking archive linking.
3454
3455 For a defined function "foo" and an undefined call to "bar":
3456 An old object defines "foo" and ".foo", references ".bar" (possibly
3457 "bar" too).
3458 A new object defines "foo" and references "bar".
3459
3460 A new object thus has no problem with its undefined symbols being
3461 satisfied by definitions in an old object. On the other hand, the
3462 old object won't have ".bar" satisfied by a new object.
3463
3464 Keep a list of newly added dot-symbols. */
3465
3466 if (string[0] == '.')
3467 {
3468 struct ppc_link_hash_table *htab;
3469
3470 htab = (struct ppc_link_hash_table *) table;
3471 eh->u.next_dot_sym = htab->dot_syms;
3472 htab->dot_syms = eh;
3473 }
65f38f15
AM
3474 }
3475
3476 return entry;
3477}
3478
2cdcc330
AM
3479struct tocsave_entry
3480{
3b421ab3
AM
3481 asection *sec;
3482 bfd_vma offset;
3483};
3484
3485static hashval_t
3486tocsave_htab_hash (const void *p)
3487{
3488 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3489 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3490}
3491
3492static int
3493tocsave_htab_eq (const void *p1, const void *p2)
3494{
3495 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3496 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3497 return e1->sec == e2->sec && e1->offset == e2->offset;
3498}
3499
68faa637
AM
3500/* Destroy a ppc64 ELF linker hash table. */
3501
3502static void
d495ab0d 3503ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3504{
d495ab0d 3505 struct ppc_link_hash_table *htab;
68faa637 3506
d495ab0d 3507 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3508 if (htab->tocsave_htab)
3509 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3510 bfd_hash_table_free (&htab->branch_hash_table);
3511 bfd_hash_table_free (&htab->stub_hash_table);
3512 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3513}
3514
65f38f15
AM
3515/* Create a ppc64 ELF linker hash table. */
3516
3517static struct bfd_link_hash_table *
4ce794b7 3518ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3519{
3520 struct ppc_link_hash_table *htab;
986f0783 3521 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3522
4ce794b7 3523 htab = bfd_zmalloc (amt);
65f38f15
AM
3524 if (htab == NULL)
3525 return NULL;
3526
66eb6687 3527 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3528 sizeof (struct ppc_link_hash_entry),
3529 PPC64_ELF_DATA))
65f38f15 3530 {
e2d34d7d 3531 free (htab);
65f38f15
AM
3532 return NULL;
3533 }
3534
721956f4 3535 /* Init the stub hash table too. */
66eb6687
AM
3536 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3537 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3538 {
d495ab0d 3539 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3540 return NULL;
3541 }
721956f4
AM
3542
3543 /* And the branch hash table. */
66eb6687
AM
3544 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3545 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3546 {
3547 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3548 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3549 return NULL;
3550 }
721956f4 3551
3b421ab3
AM
3552 htab->tocsave_htab = htab_try_create (1024,
3553 tocsave_htab_hash,
3554 tocsave_htab_eq,
3555 NULL);
3556 if (htab->tocsave_htab == NULL)
2915c55b 3557 {
d495ab0d 3558 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3559 return NULL;
3560 }
d495ab0d 3561 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3562
3254fd24
AM
3563 /* Initializing two fields of the union is just cosmetic. We really
3564 only care about glist, but when compiled on a 32-bit host the
3565 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3566 debugger inspection of these fields look nicer. */
a6aa5195
AM
3567 htab->elf.init_got_refcount.refcount = 0;
3568 htab->elf.init_got_refcount.glist = NULL;
3569 htab->elf.init_plt_refcount.refcount = 0;
3570 htab->elf.init_plt_refcount.glist = NULL;
3571 htab->elf.init_got_offset.offset = 0;
3572 htab->elf.init_got_offset.glist = NULL;
3573 htab->elf.init_plt_offset.offset = 0;
3574 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3575
65f38f15
AM
3576 return &htab->elf.root;
3577}
3578
bfeb4a28
AM
3579/* Create sections for linker generated code. */
3580
0a1b45a2 3581static bool
bfeb4a28
AM
3582create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3583{
3584 struct ppc_link_hash_table *htab;
3585 flagword flags;
3586
3587 htab = ppc_hash_table (info);
3588
bfeb4a28
AM
3589 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3590 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3591 if (htab->params->save_restore_funcs)
3592 {
3593 /* Create .sfpr for code to save and restore fp regs. */
3594 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3595 flags);
3596 if (htab->sfpr == NULL
fd361982 3597 || !bfd_set_section_alignment (htab->sfpr, 2))
0a1b45a2 3598 return false;
b32547cd
AM
3599 }
3600
3601 if (bfd_link_relocatable (info))
0a1b45a2 3602 return true;
bfeb4a28
AM
3603
3604 /* Create .glink for lazy dynamic linking support. */
3605 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3606 flags);
3607 if (htab->glink == NULL
fd361982 3608 || !bfd_set_section_alignment (htab->glink, 3))
0a1b45a2 3609 return false;
bfeb4a28 3610
9e390558
AM
3611 /* The part of .glink used by global entry stubs, separate so that
3612 it can be aligned appropriately without affecting htab->glink. */
3613 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3614 flags);
3615 if (htab->global_entry == NULL
fd361982 3616 || !bfd_set_section_alignment (htab->global_entry, 2))
0a1b45a2 3617 return false;
9e390558 3618
bfeb4a28
AM
3619 if (!info->no_ld_generated_unwind_info)
3620 {
3621 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3622 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3623 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3624 ".eh_frame",
3625 flags);
3626 if (htab->glink_eh_frame == NULL
fd361982 3627 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
0a1b45a2 3628 return false;
bfeb4a28
AM
3629 }
3630
3631 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3632 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3633 if (htab->elf.iplt == NULL
fd361982 3634 || !bfd_set_section_alignment (htab->elf.iplt, 3))
0a1b45a2 3635 return false;
bfeb4a28
AM
3636
3637 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3638 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3639 htab->elf.irelplt
3640 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3641 if (htab->elf.irelplt == NULL
fd361982 3642 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
0a1b45a2 3643 return false;
bfeb4a28
AM
3644
3645 /* Create branch lookup table for plt_branch stubs. */
3646 flags = (SEC_ALLOC | SEC_LOAD
3647 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3648 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3649 flags);
3650 if (htab->brlt == NULL
fd361982 3651 || !bfd_set_section_alignment (htab->brlt, 3))
0a1b45a2 3652 return false;
bfeb4a28 3653
2d7ad24e
AM
3654 /* Local plt entries, put in .branch_lt but a separate section for
3655 convenience. */
3656 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3657 flags);
3658 if (htab->pltlocal == NULL
fd361982 3659 || !bfd_set_section_alignment (htab->pltlocal, 3))
0a1b45a2 3660 return false;
2d7ad24e 3661
0e1862bb 3662 if (!bfd_link_pic (info))
0a1b45a2 3663 return true;
bfeb4a28
AM
3664
3665 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3666 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3667 htab->relbrlt
3668 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3669 if (htab->relbrlt == NULL
fd361982 3670 || !bfd_set_section_alignment (htab->relbrlt, 3))
0a1b45a2 3671 return false;
bfeb4a28 3672
2d7ad24e
AM
3673 htab->relpltlocal
3674 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3675 if (htab->relpltlocal == NULL
fd361982 3676 || !bfd_set_section_alignment (htab->relpltlocal, 3))
0a1b45a2 3677 return false;
2d7ad24e 3678
0a1b45a2 3679 return true;
bfeb4a28
AM
3680}
3681
e717da7e
AM
3682/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3683
0a1b45a2 3684bool
e7d1c40c
AM
3685ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3686 struct ppc64_elf_params *params)
e717da7e
AM
3687{
3688 struct ppc_link_hash_table *htab;
3689
e7d1c40c 3690 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3691
3692/* Always hook our dynamic sections into the first bfd, which is the
3693 linker created stub bfd. This ensures that the GOT header is at
3694 the start of the output TOC section. */
3695 htab = ppc_hash_table (info);
e7d1c40c
AM
3696 htab->elf.dynobj = params->stub_bfd;
3697 htab->params = params;
bfeb4a28 3698
bfeb4a28 3699 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3700}
3701
721956f4
AM
3702/* Build a name for an entry in the stub hash table. */
3703
3704static char *
4ce794b7
AM
3705ppc_stub_name (const asection *input_section,
3706 const asection *sym_sec,
3707 const struct ppc_link_hash_entry *h,
3708 const Elf_Internal_Rela *rel)
721956f4
AM
3709{
3710 char *stub_name;
bcaa2f82 3711 ssize_t len;
721956f4
AM
3712
3713 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3714 offsets from a sym as a branch target? In fact, we could
3715 probably assume the addend is always zero. */
3716 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3717
3718 if (h)
3719 {
3720 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3721 stub_name = bfd_malloc (len);
46de2a7c
AM
3722 if (stub_name == NULL)
3723 return stub_name;
3724
bcaa2f82
AM
3725 len = sprintf (stub_name, "%08x.%s+%x",
3726 input_section->id & 0xffffffff,
3727 h->elf.root.root.string,
3728 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3729 }
3730 else
3731 {
ad8e1ba5 3732 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3733 stub_name = bfd_malloc (len);
46de2a7c
AM
3734 if (stub_name == NULL)
3735 return stub_name;
3736
bcaa2f82
AM
3737 len = sprintf (stub_name, "%08x.%x:%x+%x",
3738 input_section->id & 0xffffffff,
3739 sym_sec->id & 0xffffffff,
3740 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3741 (int) rel->r_addend & 0xffffffff);
721956f4 3742 }
bcaa2f82 3743 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3744 stub_name[len - 2] = 0;
721956f4
AM
3745 return stub_name;
3746}
3747
e10a07b3 3748/* If mixing power10 with non-power10 code and --power10-stubs is not
7aba54da
AM
3749 specified (or is auto) then there may be multiple stub types for any
3750 given symbol. Up to three classes of stubs are stored in separate
3751 stub_hash_table entries having the same key string. The entries
3752 will always be adjacent on entry->root.next chain, even if hash
3753 table resizing occurs. This function selects the correct entry to
3754 use. */
e10a07b3
AM
3755
3756static struct ppc_stub_hash_entry *
7aba54da
AM
3757select_alt_stub (struct ppc_stub_hash_entry *entry,
3758 enum elf_ppc64_reloc_type r_type)
e10a07b3 3759{
7aba54da 3760 enum ppc_stub_sub_type subt;
e10a07b3 3761
7aba54da
AM
3762 switch (r_type)
3763 {
3764 case R_PPC64_REL24_NOTOC:
3765 subt = ppc_stub_notoc;
3766 break;
3767 case R_PPC64_REL24_P9NOTOC:
3768 subt = ppc_stub_p9notoc;
3769 break;
3770 default:
3771 subt = ppc_stub_toc;
3772 break;
3773 }
e10a07b3 3774
7aba54da 3775 while (entry != NULL && entry->type.sub != subt)
e10a07b3
AM
3776 {
3777 const char *stub_name = entry->root.string;
3778
3779 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3780 if (entry != NULL
3781 && entry->root.string != stub_name)
3782 entry = NULL;
3783 }
3784
3785 return entry;
3786}
3787
721956f4
AM
3788/* Look up an entry in the stub hash. Stub entries are cached because
3789 creating the stub name takes a bit of time. */
3790
3791static struct ppc_stub_hash_entry *
4ce794b7
AM
3792ppc_get_stub_entry (const asection *input_section,
3793 const asection *sym_sec,
039b3fef 3794 struct ppc_link_hash_entry *h,
4ce794b7
AM
3795 const Elf_Internal_Rela *rel,
3796 struct ppc_link_hash_table *htab)
721956f4
AM
3797{
3798 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3799 struct map_stub *group;
721956f4
AM
3800
3801 /* If this input section is part of a group of sections sharing one
3802 stub section, then use the id of the first section in the group.
3803 Stub names need to include a section id, as there may well be
3804 more than one stub used to reach say, printf, and we need to
3805 distinguish between them. */
6f20ed8a 3806 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3807 if (group == NULL)
3808 return NULL;
721956f4 3809
b3fac117
AM
3810 if (h != NULL && h->u.stub_cache != NULL
3811 && h->u.stub_cache->h == h
6f20ed8a 3812 && h->u.stub_cache->group == group)
721956f4 3813 {
b3fac117 3814 stub_entry = h->u.stub_cache;
721956f4
AM
3815 }
3816 else
3817 {
3818 char *stub_name;
3819
6f20ed8a 3820 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3821 if (stub_name == NULL)
3822 return NULL;
3823
3824 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 3825 stub_name, false, false);
721956f4 3826 if (h != NULL)
b3fac117 3827 h->u.stub_cache = stub_entry;
721956f4
AM
3828
3829 free (stub_name);
3830 }
3831
e10a07b3 3832 if (stub_entry != NULL && htab->params->power10_stubs == -1)
7aba54da 3833 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
e10a07b3 3834
721956f4
AM
3835 return stub_entry;
3836}
3837
3838/* Add a new stub entry to the stub hash. Not all fields of the new
3839 stub entry are initialised. */
3840
3841static struct ppc_stub_hash_entry *
4ce794b7
AM
3842ppc_add_stub (const char *stub_name,
3843 asection *section,
25f53a85 3844 struct bfd_link_info *info)
721956f4 3845{
25f53a85 3846 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3847 struct map_stub *group;
721956f4
AM
3848 asection *link_sec;
3849 asection *stub_sec;
3850 struct ppc_stub_hash_entry *stub_entry;
3851
6f20ed8a
AM
3852 group = htab->sec_info[section->id].u.group;
3853 link_sec = group->link_sec;
3854 stub_sec = group->stub_sec;
721956f4
AM
3855 if (stub_sec == NULL)
3856 {
6f20ed8a
AM
3857 size_t namelen;
3858 bfd_size_type len;
3859 char *s_name;
721956f4 3860
6f20ed8a
AM
3861 namelen = strlen (link_sec->name);
3862 len = namelen + sizeof (STUB_SUFFIX);
3863 s_name = bfd_alloc (htab->params->stub_bfd, len);
3864 if (s_name == NULL)
3865 return NULL;
721956f4 3866
6f20ed8a
AM
3867 memcpy (s_name, link_sec->name, namelen);
3868 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3869 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3870 if (stub_sec == NULL)
3871 return NULL;
3872 group->stub_sec = stub_sec;
721956f4
AM
3873 }
3874
3875 /* Enter this entry into the linker stub hash table. */
3876 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
0a1b45a2 3877 true, false);
721956f4
AM
3878 if (stub_entry == NULL)
3879 {
695344c0 3880 /* xgettext:c-format */
cf97bcb0
AM
3881 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3882 section->owner, stub_name);
721956f4
AM
3883 return NULL;
3884 }
3885
6f20ed8a 3886 stub_entry->group = group;
721956f4 3887 stub_entry->stub_offset = 0;
721956f4
AM
3888 return stub_entry;
3889}
3890
7aba54da
AM
3891/* A stub has already been created, but it may not be the required
3892 type. We shouldn't be transitioning from plt_call to long_branch
3893 stubs or vice versa, but we might be upgrading from plt_call to
3894 plt_call with r2save for example. */
3895
3896static bool
3897ppc_merge_stub (struct ppc_link_hash_table *htab,
3898 struct ppc_stub_hash_entry *stub_entry,
3899 struct ppc_stub_type stub_type,
3900 enum elf_ppc64_reloc_type r_type)
3901{
3902 struct ppc_stub_type old_type = stub_entry->type;
3903
3904 if (old_type.main == ppc_stub_save_res)
3905 return true;
3906
3907 if (htab->params->power10_stubs == -1)
3908 {
3909 /* For --power10-stubs=auto, don't merge _notoc and other
3910 varieties of stubs. */
3911 struct ppc_stub_hash_entry *alt_stub;
3912
3913 alt_stub = select_alt_stub (stub_entry, r_type);
3914 if (alt_stub == NULL)
3915 {
3916 alt_stub = ((struct ppc_stub_hash_entry *)
3917 stub_hash_newfunc (NULL,
3918 &htab->stub_hash_table,
3919 stub_entry->root.string));
3920 if (alt_stub == NULL)
3921 return false;
3922
3923 *alt_stub = *stub_entry;
3924 stub_entry->root.next = &alt_stub->root;
3925
3926 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3927 Not that it matters, this just puts smaller stubs first. */
3928 if (stub_type.sub == ppc_stub_notoc)
3929 alt_stub = stub_entry;
3930 else if (stub_type.sub == ppc_stub_p9notoc
3931 && alt_stub->root.next
3932 && alt_stub->root.next->string == alt_stub->root.string)
3933 {
3934 struct ppc_stub_hash_entry *next
3935 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3936 alt_stub->type = next->type;
3937 alt_stub = next;
3938 }
3939 alt_stub->type = stub_type;
3940 return true;
3941 }
3942 stub_entry = alt_stub;
3943 }
3944
3945 old_type = stub_entry->type;
3946 if (old_type.main == ppc_stub_plt_branch)
3947 old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
3948
3949 if (old_type.main != stub_type.main
3950 || (old_type.sub != stub_type.sub
3951 && old_type.sub != ppc_stub_toc
3952 && stub_type.sub != ppc_stub_toc))
3953 abort ();
3954
3955 stub_entry->type.sub |= stub_type.sub;
3956 stub_entry->type.r2save |= stub_type.r2save;
3957 return true;
3958}
3959
e717da7e
AM
3960/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3961 not already done. */
65f38f15 3962
0a1b45a2 3963static bool
e717da7e 3964create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3965{
e717da7e
AM
3966 asection *got, *relgot;
3967 flagword flags;
3968 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3969
0c8d6e5c 3970 if (!is_ppc64_elf (abfd))
0a1b45a2 3971 return false;
4dfe6ac6 3972 if (htab == NULL)
0a1b45a2 3973 return false;
0ffa91dd 3974
33e44f2e
AM
3975 if (!htab->elf.sgot
3976 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
0a1b45a2 3977 return false;
e717da7e
AM
3978
3979 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3980 | SEC_LINKER_CREATED);
3981
c456f082 3982 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3983 if (!got
fd361982 3984 || !bfd_set_section_alignment (got, 3))
0a1b45a2 3985 return false;
65f38f15 3986
c456f082
AM
3987 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3988 flags | SEC_READONLY);
e717da7e 3989 if (!relgot
fd361982 3990 || !bfd_set_section_alignment (relgot, 3))
0a1b45a2 3991 return false;
e717da7e
AM
3992
3993 ppc64_elf_tdata (abfd)->got = got;
3994 ppc64_elf_tdata (abfd)->relgot = relgot;
0a1b45a2 3995 return true;
65f38f15 3996}
5bd4f169 3997
b31867b6
AM
3998/* Follow indirect and warning symbol links. */
3999
4000static inline struct bfd_link_hash_entry *
4001follow_link (struct bfd_link_hash_entry *h)
4002{
4003 while (h->type == bfd_link_hash_indirect
4004 || h->type == bfd_link_hash_warning)
4005 h = h->u.i.link;
4006 return h;
4007}
4008
4009static inline struct elf_link_hash_entry *
4010elf_follow_link (struct elf_link_hash_entry *h)
4011{
4012 return (struct elf_link_hash_entry *) follow_link (&h->root);
4013}
4014
4015static inline struct ppc_link_hash_entry *
4016ppc_follow_link (struct ppc_link_hash_entry *h)
4017{
ed7007c1 4018 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
4019}
4020
40d16e0b
AM
4021/* Merge PLT info on FROM with that on TO. */
4022
4023static void
4024move_plt_plist (struct ppc_link_hash_entry *from,
4025 struct ppc_link_hash_entry *to)
4026{
4027 if (from->elf.plt.plist != NULL)
4028 {
4029 if (to->elf.plt.plist != NULL)
4030 {
4031 struct plt_entry **entp;
4032 struct plt_entry *ent;
4033
4034 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4035 {
4036 struct plt_entry *dent;
4037
4038 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4039 if (dent->addend == ent->addend)
4040 {
4041 dent->plt.refcount += ent->plt.refcount;
4042 *entp = ent->next;
4043 break;
4044 }
4045 if (dent == NULL)
4046 entp = &ent->next;
4047 }
4048 *entp = to->elf.plt.plist;
4049 }
4050
4051 to->elf.plt.plist = from->elf.plt.plist;
4052 from->elf.plt.plist = NULL;
4053 }
4054}
4055
65f38f15
AM
4056/* Copy the extra info we tack onto an elf_link_hash_entry. */
4057
4058static void
fcfa13d2
AM
4059ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4060 struct elf_link_hash_entry *dir,
4061 struct elf_link_hash_entry *ind)
65f38f15
AM
4062{
4063 struct ppc_link_hash_entry *edir, *eind;
4064
ed7007c1
AM
4065 edir = ppc_elf_hash_entry (dir);
4066 eind = ppc_elf_hash_entry (ind);
65f38f15 4067
c79d6685
AM
4068 edir->is_func |= eind->is_func;
4069 edir->is_func_descriptor |= eind->is_func_descriptor;
4070 edir->tls_mask |= eind->tls_mask;
4071 if (eind->oh != NULL)
4072 edir->oh = ppc_follow_link (eind->oh);
4073
474436e6 4074 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4075 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4076 edir->elf.ref_regular |= eind->elf.ref_regular;
4077 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4078 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4079 edir->elf.needs_plt |= eind->elf.needs_plt;
4080 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4081
d311bc8b
AM
4082 /* If we were called to copy over info for a weak sym, don't copy
4083 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4084 in order to simplify readonly_dynrelocs and save a field in the
4085 symbol hash entry, but that means dyn_relocs can't be used in any
4086 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4087 are then tested. */
d311bc8b 4088 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4089 return;
d311bc8b 4090
411e1bfb 4091 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 4092 if (ind->dyn_relocs != NULL)
65f38f15 4093 {
190eb1dd 4094 if (dir->dyn_relocs != NULL)
bbd7ec4a 4095 {
1657026c
AM
4096 struct ppc_dyn_relocs **pp;
4097 struct ppc_dyn_relocs *p;
bbd7ec4a 4098
fcfa13d2 4099 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 4100 list. Merge any entries against the same section. */
1657026c
AM
4101 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4102 (p = *pp) != NULL;
4103 )
bbd7ec4a 4104 {
1657026c 4105 struct ppc_dyn_relocs *q;
bbd7ec4a 4106
1657026c
AM
4107 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4108 q != NULL;
4109 q = q->next)
bbd7ec4a
AM
4110 if (q->sec == p->sec)
4111 {
bbd7ec4a 4112 q->count += p->count;
1657026c
AM
4113 q->pc_count += p->pc_count;
4114 q->rel_count += p->rel_count;
bbd7ec4a
AM
4115 *pp = p->next;
4116 break;
4117 }
4118 if (q == NULL)
4119 pp = &p->next;
4120 }
1657026c 4121 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
bbd7ec4a
AM
4122 }
4123
190eb1dd
L
4124 dir->dyn_relocs = ind->dyn_relocs;
4125 ind->dyn_relocs = NULL;
65f38f15 4126 }
65f38f15 4127
81848ca0
AM
4128 /* Copy over got entries that we may have already seen to the
4129 symbol which just became indirect. */
411e1bfb
AM
4130 if (eind->elf.got.glist != NULL)
4131 {
4132 if (edir->elf.got.glist != NULL)
4133 {
4134 struct got_entry **entp;
4135 struct got_entry *ent;
4136
4137 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4138 {
4139 struct got_entry *dent;
4140
4141 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4142 if (dent->addend == ent->addend
e717da7e 4143 && dent->owner == ent->owner
411e1bfb
AM
4144 && dent->tls_type == ent->tls_type)
4145 {
4146 dent->got.refcount += ent->got.refcount;
4147 *entp = ent->next;
4148 break;
4149 }
4150 if (dent == NULL)
4151 entp = &ent->next;
4152 }
4153 *entp = edir->elf.got.glist;
4154 }
4155
4156 edir->elf.got.glist = eind->elf.got.glist;
4157 eind->elf.got.glist = NULL;
4158 }
4159
4160 /* And plt entries. */
40d16e0b 4161 move_plt_plist (eind, edir);
411e1bfb 4162
fcfa13d2 4163 if (eind->elf.dynindx != -1)
411e1bfb 4164 {
fcfa13d2
AM
4165 if (edir->elf.dynindx != -1)
4166 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4167 edir->elf.dynstr_index);
411e1bfb
AM
4168 edir->elf.dynindx = eind->elf.dynindx;
4169 edir->elf.dynstr_index = eind->elf.dynstr_index;
4170 eind->elf.dynindx = -1;
4171 eind->elf.dynstr_index = 0;
4172 }
411e1bfb
AM
4173}
4174
8387904d
AM
4175/* Find the function descriptor hash entry from the given function code
4176 hash entry FH. Link the entries via their OH fields. */
4177
4178static struct ppc_link_hash_entry *
b31867b6 4179lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4180{
4181 struct ppc_link_hash_entry *fdh = fh->oh;
4182
4183 if (fdh == NULL)
4184 {
4185 const char *fd_name = fh->elf.root.root.string + 1;
4186
ed7007c1 4187 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
0a1b45a2 4188 false, false, false));
b31867b6
AM
4189 if (fdh == NULL)
4190 return fdh;
4191
4192 fdh->is_func_descriptor = 1;
4193 fdh->oh = fh;
4194 fh->is_func = 1;
4195 fh->oh = fdh;
8387904d
AM
4196 }
4197
8c5b4e52
AM
4198 fdh = ppc_follow_link (fdh);
4199 fdh->is_func_descriptor = 1;
4200 fdh->oh = fh;
4201 return fdh;
8387904d
AM
4202}
4203
8c5b4e52 4204/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4205
4206static struct ppc_link_hash_entry *
4207make_fdh (struct bfd_link_info *info,
908b32fc 4208 struct ppc_link_hash_entry *fh)
bb700d78 4209{
8c5b4e52
AM
4210 bfd *abfd = fh->elf.root.u.undef.abfd;
4211 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4212 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4213 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4214 ? BSF_WEAK
4215 : BSF_GLOBAL);
4216
4217 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4218 fh->elf.root.root.string + 1,
4219 flags, bfd_und_section_ptr, 0,
0a1b45a2 4220 NULL, false, false, &bh))
bb700d78
AM
4221 return NULL;
4222
4223 fdh = (struct ppc_link_hash_entry *) bh;
4224 fdh->elf.non_elf = 0;
908b32fc
AM
4225 fdh->fake = 1;
4226 fdh->is_func_descriptor = 1;
4227 fdh->oh = fh;
4228 fh->is_func = 1;
4229 fh->oh = fdh;
bb700d78
AM
4230 return fdh;
4231}
4232
8387904d
AM
4233/* Fix function descriptor symbols defined in .opd sections to be
4234 function type. */
555cd476 4235
0a1b45a2 4236static bool
c16153ae 4237ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4238 struct bfd_link_info *info,
555cd476 4239 Elf_Internal_Sym *isym,
6911b7dc 4240 const char **name,
555cd476
AM
4241 flagword *flags ATTRIBUTE_UNUSED,
4242 asection **sec,
b53dfeb2 4243 bfd_vma *value)
555cd476 4244{
b53dfeb2 4245 if (*sec != NULL
f1885d1e 4246 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4247 {
4248 asection *code_sec;
4249
4250 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4251 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4252 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4253
4254 /* If the symbol is a function defined in .opd, and the function
4255 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4256 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4257 && (*sec)->reloc_count != 0
4258 && opd_entry_value (*sec, *value, &code_sec, NULL,
0a1b45a2 4259 false) != (bfd_vma) -1
b53dfeb2
AM
4260 && discarded_section (code_sec))
4261 {
4262 *sec = bfd_und_section_ptr;
4263 isym->st_shndx = SHN_UNDEF;
4264 }
4265 }
dbd1e97e
AM
4266 else if (*sec != NULL
4267 && strcmp ((*sec)->name, ".toc") == 0
4268 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4269 {
4270 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4271 if (htab != NULL)
4272 htab->params->object_in_toc = 1;
4273 }
433817dd 4274
6911b7dc
AM
4275 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4276 {
4277 if (abiversion (ibfd) == 0)
4278 set_abiversion (ibfd, 2);
4279 else if (abiversion (ibfd) == 1)
4280 {
cf97bcb0
AM
4281 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4282 " for ABI version 1"), *name);
6911b7dc 4283 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4284 return false;
6911b7dc
AM
4285 }
4286 }
4287
0a1b45a2 4288 return true;
555cd476
AM
4289}
4290
6911b7dc
AM
4291/* Merge non-visibility st_other attributes: local entry point. */
4292
4293static void
4294ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4295 unsigned int st_other,
0a1b45a2
AM
4296 bool definition,
4297 bool dynamic)
6911b7dc 4298{
f378ab09 4299 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4300 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4301 | ELF_ST_VISIBILITY (h->other));
4302}
4303
8c5b4e52
AM
4304/* Hook called on merging a symbol. We use this to clear "fake" since
4305 we now have a real symbol. */
4306
0a1b45a2 4307static bool
8c5b4e52 4308ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4309 const Elf_Internal_Sym *isym,
8c5b4e52 4310 asection **psec ATTRIBUTE_UNUSED,
0a1b45a2
AM
4311 bool newdef ATTRIBUTE_UNUSED,
4312 bool olddef ATTRIBUTE_UNUSED,
8c5b4e52
AM
4313 bfd *oldbfd ATTRIBUTE_UNUSED,
4314 const asection *oldsec ATTRIBUTE_UNUSED)
4315{
ed7007c1 4316 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4317 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4318 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
0a1b45a2 4319 return true;
8c5b4e52
AM
4320}
4321
8387904d 4322/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4323 inclusion of a new ABI object archive that defines "bar".
4324 NAME is a symbol defined in an archive. Return a symbol in the hash
4325 table that might be satisfied by the archive symbols. */
8387904d 4326
b585e899 4327static struct bfd_link_hash_entry *
8387904d
AM
4328ppc64_elf_archive_symbol_lookup (bfd *abfd,
4329 struct bfd_link_info *info,
4330 const char *name)
4331{
b585e899 4332 struct bfd_link_hash_entry *h;
8387904d
AM
4333 char *dot_name;
4334 size_t len;
4335
4336 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc 4337 if (h != NULL
b585e899 4338 && ppc_hash_table (info) != NULL
908b32fc
AM
4339 /* Don't return this sym if it is a fake function descriptor
4340 created by add_symbol_adjust. */
b585e899 4341 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4342 return h;
4343
4344 if (name[0] == '.')
4345 return h;
4346
4347 len = strlen (name);
4348 dot_name = bfd_alloc (abfd, len + 2);
4349 if (dot_name == NULL)
b585e899 4350 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
4351 dot_name[0] = '.';
4352 memcpy (dot_name + 1, name, len + 1);
4353 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4354 bfd_release (abfd, dot_name);
a804e476
AM
4355 if (h != NULL)
4356 return h;
4357
4358 if (strcmp (name, "__tls_get_addr_opt") == 0)
4359 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4360 return h;
4361}
4362
4363/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4364 new ABI object defines "bar". Well, at least, undefined dot symbols
4365 are made weak. This stops later archive searches from including an
4366 object if we already have a function descriptor definition. It also
35b0ce59
AM
4367 prevents the linker complaining about undefined symbols.
4368 We also check and correct mismatched symbol visibility here. The
4369 most restrictive visibility of the function descriptor and the
4370 function entry symbol is used. */
8387904d 4371
0a1b45a2 4372static bool
b3fac117 4373add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4374{
8387904d
AM
4375 struct ppc_link_hash_table *htab;
4376 struct ppc_link_hash_entry *fdh;
4377
b3fac117
AM
4378 if (eh->elf.root.type == bfd_link_hash_warning)
4379 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4380
8400d40d 4381 if (eh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 4382 return true;
8400d40d 4383
b3fac117
AM
4384 if (eh->elf.root.root.string[0] != '.')
4385 abort ();
8387904d 4386
b3fac117 4387 htab = ppc_hash_table (info);
4dfe6ac6 4388 if (htab == NULL)
0a1b45a2 4389 return false;
4dfe6ac6 4390
b31867b6 4391 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4392 if (fdh == NULL
4393 && !bfd_link_relocatable (info)
4394 && (eh->elf.root.type == bfd_link_hash_undefined
4395 || eh->elf.root.type == bfd_link_hash_undefweak)
4396 && eh->elf.ref_regular)
4397 {
4398 /* Make an undefined function descriptor sym, in order to
4399 pull in an --as-needed shared lib. Archives are handled
4400 elsewhere. */
4401 fdh = make_fdh (info, eh);
4402 if (fdh == NULL)
0a1b45a2 4403 return false;
bb700d78 4404 }
8c5b4e52
AM
4405
4406 if (fdh != NULL)
8387904d 4407 {
35b0ce59
AM
4408 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4409 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4410
4411 /* Make both descriptor and entry symbol have the most
4412 constraining visibility of either symbol. */
35b0ce59
AM
4413 if (entry_vis < descr_vis)
4414 fdh->elf.other += entry_vis - descr_vis;
4415 else if (entry_vis > descr_vis)
4416 eh->elf.other += descr_vis - entry_vis;
4417
8c5b4e52
AM
4418 /* Propagate reference flags from entry symbol to function
4419 descriptor symbol. */
bc4e12de 4420 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4421 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4422 fdh->elf.ref_regular |= eh->elf.ref_regular;
4423 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4424
4425 if (!fdh->elf.forced_local
4426 && fdh->elf.dynindx == -1
4427 && fdh->elf.versioned != versioned_hidden
4428 && (bfd_link_dll (info)
4429 || fdh->elf.def_dynamic
4430 || fdh->elf.ref_dynamic)
4431 && (eh->elf.ref_regular
4432 || eh->elf.def_regular))
4433 {
2cdcc330 4434 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 4435 return false;
35b0ce59 4436 }
8387904d 4437 }
99877b66 4438
0a1b45a2 4439 return true;
8387904d
AM
4440}
4441
f6c7c3e8
AM
4442/* Set up opd section info and abiversion for IBFD, and process list
4443 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4444
0a1b45a2 4445static bool
f6c7c3e8 4446ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4447{
99877b66 4448 struct ppc_link_hash_table *htab;
b3fac117 4449 struct ppc_link_hash_entry **p, *eh;
459609d6 4450 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4451
459609d6 4452 if (opd != NULL && opd->size != 0)
b3fac117 4453 {
b9399fcf
AM
4454 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4455 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4456
459609d6
AM
4457 if (abiversion (ibfd) == 0)
4458 set_abiversion (ibfd, 1);
8a2058b5 4459 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4460 {
695344c0 4461 /* xgettext:c-format */
cf97bcb0
AM
4462 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4463 ibfd, abiversion (ibfd));
459609d6 4464 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4465 return false;
f6c7c3e8 4466 }
b9399fcf 4467 }
f6c7c3e8 4468
b9399fcf
AM
4469 if (is_ppc64_elf (info->output_bfd))
4470 {
4471 /* For input files without an explicit abiversion in e_flags
4472 we should have flagged any with symbol st_other bits set
4473 as ELFv1 and above flagged those with .opd as ELFv2.
4474 Set the output abiversion if not yet set, and for any input
4475 still ambiguous, take its abiversion from the output.
4476 Differences in ABI are reported later. */
4477 if (abiversion (info->output_bfd) == 0)
4478 set_abiversion (info->output_bfd, abiversion (ibfd));
4479 else if (abiversion (ibfd) == 0)
4480 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4481 }
4482
459609d6
AM
4483 htab = ppc_hash_table (info);
4484 if (htab == NULL)
0a1b45a2 4485 return true;
459609d6 4486
b9399fcf
AM
4487 if (opd != NULL && opd->size != 0
4488 && (ibfd->flags & DYNAMIC) == 0
4489 && (opd->flags & SEC_RELOC) != 0
4490 && opd->reloc_count != 0
4491 && !bfd_is_abs_section (opd->output_section)
4492 && info->gc_sections)
4493 {
4494 /* Garbage collection needs some extra help with .opd sections.
4495 We don't want to necessarily keep everything referenced by
4496 relocs in .opd, as that would keep all functions. Instead,
4497 if we reference an .opd symbol (a function descriptor), we
4498 want to keep the function code symbol's section. This is
4499 easy for global symbols, but for local syms we need to keep
4500 information about the associated function section. */
4501 bfd_size_type amt;
4502 asection **opd_sym_map;
4503 Elf_Internal_Shdr *symtab_hdr;
4504 Elf_Internal_Rela *relocs, *rel_end, *rel;
4505
4506 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4507 opd_sym_map = bfd_zalloc (ibfd, amt);
4508 if (opd_sym_map == NULL)
0a1b45a2 4509 return false;
b9399fcf
AM
4510 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4511 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4512 info->keep_memory);
4513 if (relocs == NULL)
0a1b45a2 4514 return false;
b9399fcf
AM
4515 symtab_hdr = &elf_symtab_hdr (ibfd);
4516 rel_end = relocs + opd->reloc_count - 1;
4517 for (rel = relocs; rel < rel_end; rel++)
4518 {
4519 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4520 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4521
4522 if (r_type == R_PPC64_ADDR64
4523 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4524 && r_symndx < symtab_hdr->sh_info)
4525 {
4526 Elf_Internal_Sym *isym;
4527 asection *s;
4528
f1dfbfdb
L
4529 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4530 r_symndx);
b9399fcf
AM
4531 if (isym == NULL)
4532 {
4533 if (elf_section_data (opd)->relocs != relocs)
4534 free (relocs);
0a1b45a2 4535 return false;
b9399fcf
AM
4536 }
4537
4538 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4539 if (s != NULL && s != opd)
4540 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4541 }
4542 }
4543 if (elf_section_data (opd)->relocs != relocs)
4544 free (relocs);
4545 }
459609d6
AM
4546
4547 p = &htab->dot_syms;
4548 while ((eh = *p) != NULL)
4549 {
4550 *p = NULL;
4551 if (&eh->elf == htab->elf.hgot)
4552 ;
4553 else if (htab->elf.hgot == NULL
4554 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4555 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4556 else if (abiversion (ibfd) <= 1)
4557 {
4558 htab->need_func_desc_adj = 1;
4559 if (!add_symbol_adjust (eh, info))
0a1b45a2 4560 return false;
8c5b4e52 4561 }
459609d6
AM
4562 p = &eh->u.next_dot_sym;
4563 }
0a1b45a2 4564 return true;
8387904d
AM
4565}
4566
97fed1c9
JJ
4567/* Undo hash table changes when an --as-needed input file is determined
4568 not to be needed. */
4569
0a1b45a2 4570static bool
e5034e59
AM
4571ppc64_elf_notice_as_needed (bfd *ibfd,
4572 struct bfd_link_info *info,
4573 enum notice_asneeded_action act)
97fed1c9 4574{
e5034e59
AM
4575 if (act == notice_not_needed)
4576 {
4577 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4578
e5034e59 4579 if (htab == NULL)
0a1b45a2 4580 return false;
4dfe6ac6 4581
e5034e59
AM
4582 htab->dot_syms = NULL;
4583 }
4584 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4585}
4586
aa374f67
AM
4587/* If --just-symbols against a final linked binary, then assume we need
4588 toc adjusting stubs when calling functions defined there. */
4589
4590static void
4591ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4592{
4593 if ((sec->flags & SEC_CODE) != 0
4594 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4595 && is_ppc64_elf (sec->owner))
4596 {
2c3f079f
AM
4597 if (abiversion (sec->owner) >= 2
4598 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4599 sec->has_toc_reloc = 1;
4600 }
4601 _bfd_elf_link_just_syms (sec, info);
4602}
4603
e054468f 4604static struct plt_entry **
4ce794b7
AM
4605update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4606 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4607{
4608 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4609 struct plt_entry **local_plt;
f961d9dd 4610 unsigned char *local_got_tls_masks;
411e1bfb
AM
4611
4612 if (local_got_ents == NULL)
4613 {
4614 bfd_size_type size = symtab_hdr->sh_info;
4615
e054468f
AM
4616 size *= (sizeof (*local_got_ents)
4617 + sizeof (*local_plt)
4618 + sizeof (*local_got_tls_masks));
4ce794b7 4619 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4620 if (local_got_ents == NULL)
e054468f 4621 return NULL;
411e1bfb
AM
4622 elf_local_got_ents (abfd) = local_got_ents;
4623 }
4624
37da22e5 4625 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4626 {
4627 struct got_entry *ent;
4628
4629 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4630 if (ent->addend == r_addend
4631 && ent->owner == abfd
4632 && ent->tls_type == tls_type)
411e1bfb
AM
4633 break;
4634 if (ent == NULL)
4635 {
986f0783 4636 size_t amt = sizeof (*ent);
4ce794b7 4637 ent = bfd_alloc (abfd, amt);
411e1bfb 4638 if (ent == NULL)
0a1b45a2 4639 return false;
411e1bfb
AM
4640 ent->next = local_got_ents[r_symndx];
4641 ent->addend = r_addend;
e717da7e 4642 ent->owner = abfd;
411e1bfb 4643 ent->tls_type = tls_type;
0a1b45a2 4644 ent->is_indirect = false;
411e1bfb
AM
4645 ent->got.refcount = 0;
4646 local_got_ents[r_symndx] = ent;
4647 }
4648 ent->got.refcount += 1;
4649 }
4650
e054468f 4651 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4652 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4653 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4654
4655 return local_plt + r_symndx;
65f38f15
AM
4656}
4657
0a1b45a2 4658static bool
e054468f 4659update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4660{
411e1bfb 4661 struct plt_entry *ent;
1e2f5b6e 4662
e054468f 4663 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4664 if (ent->addend == addend)
4665 break;
4666 if (ent == NULL)
1e2f5b6e 4667 {
986f0783 4668 size_t amt = sizeof (*ent);
4ce794b7 4669 ent = bfd_alloc (abfd, amt);
411e1bfb 4670 if (ent == NULL)
0a1b45a2 4671 return false;
e054468f 4672 ent->next = *plist;
411e1bfb
AM
4673 ent->addend = addend;
4674 ent->plt.refcount = 0;
e054468f 4675 *plist = ent;
1e2f5b6e 4676 }
411e1bfb 4677 ent->plt.refcount += 1;
0a1b45a2 4678 return true;
1e2f5b6e
AM
4679}
4680
0a1b45a2 4681static bool
e054468f
AM
4682is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4683{
4684 return (r_type == R_PPC64_REL24
05d0e962 4685 || r_type == R_PPC64_REL24_NOTOC
7aba54da 4686 || r_type == R_PPC64_REL24_P9NOTOC
e054468f
AM
4687 || r_type == R_PPC64_REL14
4688 || r_type == R_PPC64_REL14_BRTAKEN
4689 || r_type == R_PPC64_REL14_BRNTAKEN
4690 || r_type == R_PPC64_ADDR24
4691 || r_type == R_PPC64_ADDR14
4692 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4693 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4694 || r_type == R_PPC64_PLTCALL
4695 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4696}
4697
4698/* Relocs on inline plt call sequence insns prior to the call. */
4699
0a1b45a2 4700static bool
23cedd1d
AM
4701is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4702{
4703 return (r_type == R_PPC64_PLT16_HA
4704 || r_type == R_PPC64_PLT16_HI
4705 || r_type == R_PPC64_PLT16_LO
4706 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4707 || r_type == R_PPC64_PLT_PCREL34
4708 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4709 || r_type == R_PPC64_PLTSEQ
4710 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4711}
4712
733ae98c
AM
4713/* Of relocs which might appear paired with TLSGD and TLSLD marker
4714 relocs, return true for those that operate on a dword. */
4715
4716static bool
4717is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4718{
4719 return (r_type == R_PPC64_PLT_PCREL34
4720 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4721 || r_type == R_PPC64_PLTCALL);
4722}
4723
4724/* Like bfd_reloc_offset_in_range but without a howto. Return true
4725 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4726
4727static bool
4728offset_in_range (asection *sec, bfd_vma offset, size_t size)
4729{
4730 return offset <= sec->size && size <= sec->size - offset;
4731}
4732
5bd4f169 4733/* Look through the relocs for a section during the first phase, and
65f38f15 4734 calculate needed space in the global offset table, procedure
5d1634d7 4735 linkage table, and dynamic reloc sections. */
5bd4f169 4736
0a1b45a2 4737static bool
4ce794b7
AM
4738ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4739 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4740{
65f38f15 4741 struct ppc_link_hash_table *htab;
5bd4f169 4742 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4743 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4744 const Elf_Internal_Rela *rel;
4745 const Elf_Internal_Rela *rel_end;
5bd4f169 4746 asection *sreloc;
3a71aa26 4747 struct elf_link_hash_entry *tga, *dottga;
0a1b45a2 4748 bool is_opd;
5bd4f169 4749
0e1862bb 4750 if (bfd_link_relocatable (info))
0a1b45a2 4751 return true;
5bd4f169 4752
0c8d6e5c 4753 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4754
65f38f15 4755 htab = ppc_hash_table (info);
4dfe6ac6 4756 if (htab == NULL)
0a1b45a2 4757 return false;
4dfe6ac6 4758
3a71aa26 4759 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 4760 false, false, true);
3a71aa26 4761 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 4762 false, false, true);
0ffa91dd 4763 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4764 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4765 sreloc = NULL;
b9399fcf 4766 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4767 rel_end = relocs + sec->reloc_count;
4768 for (rel = relocs; rel < rel_end; rel++)
4769 {
4770 unsigned long r_symndx;
4771 struct elf_link_hash_entry *h;
04c9666a 4772 enum elf_ppc64_reloc_type r_type;
727fc41e 4773 int tls_type;
7c8fe5c4 4774 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4775 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4776
4777 r_symndx = ELF64_R_SYM (rel->r_info);
4778 if (r_symndx < symtab_hdr->sh_info)
4779 h = NULL;
4780 else
973a3492
L
4781 {
4782 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4783 h = elf_follow_link (h);
1c865ab2
AM
4784
4785 if (h == htab->elf.hgot)
4786 sec->has_toc_reloc = 1;
973a3492 4787 }
5bd4f169 4788
5663e321
AM
4789 r_type = ELF64_R_TYPE (rel->r_info);
4790 switch (r_type)
4791 {
04bdff6a
AM
4792 case R_PPC64_D34:
4793 case R_PPC64_D34_LO:
4794 case R_PPC64_D34_HI30:
4795 case R_PPC64_D34_HA30:
4796 case R_PPC64_D28:
c213164a
AM
4797 case R_PPC64_TPREL34:
4798 case R_PPC64_DTPREL34:
04bdff6a 4799 case R_PPC64_PCREL34:
5663e321 4800 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4801 case R_PPC64_GOT_TLSGD_PCREL34:
4802 case R_PPC64_GOT_TLSLD_PCREL34:
4803 case R_PPC64_GOT_TPREL_PCREL34:
4804 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4805 case R_PPC64_PLT_PCREL34:
4806 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4807 case R_PPC64_PCREL28:
e10a07b3 4808 htab->has_power10_relocs = 1;
133a1f60
AM
4809 break;
4810 default:
5663e321
AM
4811 break;
4812 }
903b777d
AM
4813
4814 switch (r_type)
4815 {
4816 case R_PPC64_PLT16_HA:
4817 case R_PPC64_GOT_TLSLD16_HA:
4818 case R_PPC64_GOT_TLSGD16_HA:
4819 case R_PPC64_GOT_TPREL16_HA:
4820 case R_PPC64_GOT_DTPREL16_HA:
4821 case R_PPC64_GOT16_HA:
4822 case R_PPC64_TOC16_HA:
4823 case R_PPC64_PLT16_LO:
4824 case R_PPC64_PLT16_LO_DS:
4825 case R_PPC64_GOT_TLSLD16_LO:
4826 case R_PPC64_GOT_TLSGD16_LO:
4827 case R_PPC64_GOT_TPREL16_LO_DS:
4828 case R_PPC64_GOT_DTPREL16_LO_DS:
4829 case R_PPC64_GOT16_LO:
4830 case R_PPC64_GOT16_LO_DS:
4831 case R_PPC64_TOC16_LO:
4832 case R_PPC64_TOC16_LO_DS:
4833 case R_PPC64_GOT_PCREL34:
4834 ppc64_elf_tdata (abfd)->has_optrel = 1;
4835 ppc64_elf_section_data (sec)->has_optrel = 1;
4836 break;
4837 default:
4838 break;
4839 }
4840
f749f26e 4841 ifunc = NULL;
25f23106
AM
4842 if (h != NULL)
4843 {
4844 if (h->type == STT_GNU_IFUNC)
4845 {
4846 h->needs_plt = 1;
4847 ifunc = &h->plt.plist;
4848 }
4849 }
4850 else
4851 {
f1dfbfdb 4852 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
25f23106
AM
4853 abfd, r_symndx);
4854 if (isym == NULL)
0a1b45a2 4855 return false;
25f23106
AM
4856
4857 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4858 {
4859 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4860 rel->r_addend,
37da22e5 4861 NON_GOT | PLT_IFUNC);
25f23106 4862 if (ifunc == NULL)
0a1b45a2 4863 return false;
25f23106
AM
4864 }
4865 }
727fc41e 4866
f749f26e 4867 tls_type = 0;
a33d1f77 4868 switch (r_type)
5bd4f169 4869 {
727fc41e
AM
4870 case R_PPC64_TLSGD:
4871 case R_PPC64_TLSLD:
4872 /* These special tls relocs tie a call to __tls_get_addr with
4873 its parameter symbol. */
37da22e5 4874 if (h != NULL)
ed7007c1 4875 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4876 else
4877 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4878 rel->r_addend,
37da22e5 4879 NON_GOT | TLS_TLS | TLS_MARK))
0a1b45a2 4880 return false;
37da22e5 4881 sec->has_tls_reloc = 1;
727fc41e
AM
4882 break;
4883
411e1bfb
AM
4884 case R_PPC64_GOT_TLSLD16:
4885 case R_PPC64_GOT_TLSLD16_LO:
4886 case R_PPC64_GOT_TLSLD16_HI:
4887 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4888 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4889 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4890 goto dogottls;
4891
4892 case R_PPC64_GOT_TLSGD16:
4893 case R_PPC64_GOT_TLSGD16_LO:
4894 case R_PPC64_GOT_TLSGD16_HI:
4895 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4896 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4897 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4898 goto dogottls;
4899
4900 case R_PPC64_GOT_TPREL16_DS:
4901 case R_PPC64_GOT_TPREL16_LO_DS:
4902 case R_PPC64_GOT_TPREL16_HI:
4903 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4904 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4905 if (bfd_link_dll (info))
411e1bfb
AM
4906 info->flags |= DF_STATIC_TLS;
4907 tls_type = TLS_TLS | TLS_TPREL;
4908 goto dogottls;
4909
4910 case R_PPC64_GOT_DTPREL16_DS:
4911 case R_PPC64_GOT_DTPREL16_LO_DS:
4912 case R_PPC64_GOT_DTPREL16_HI:
4913 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4914 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4915 tls_type = TLS_TLS | TLS_DTPREL;
4916 dogottls:
4917 sec->has_tls_reloc = 1;
066f4018 4918 goto dogot;
411e1bfb 4919
903b777d
AM
4920 case R_PPC64_GOT16:
4921 case R_PPC64_GOT16_LO:
4922 case R_PPC64_GOT16_HI:
65f38f15 4923 case R_PPC64_GOT16_HA:
903b777d 4924 case R_PPC64_GOT16_DS:
066f4018 4925 case R_PPC64_GOT16_LO_DS:
4a421c53 4926 case R_PPC64_GOT_PCREL34:
066f4018 4927 dogot:
65f38f15 4928 /* This symbol requires a global offset table entry. */
4c52953f 4929 sec->has_toc_reloc = 1;
33c0ec9d
AM
4930 if (r_type == R_PPC64_GOT_TLSLD16
4931 || r_type == R_PPC64_GOT_TLSGD16
4932 || r_type == R_PPC64_GOT_TPREL16_DS
4933 || r_type == R_PPC64_GOT_DTPREL16_DS
4934 || r_type == R_PPC64_GOT16
4935 || r_type == R_PPC64_GOT16_DS)
4936 {
4937 htab->do_multi_toc = 1;
d77c8a4b 4938 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4939 }
4940
e717da7e
AM
4941 if (ppc64_elf_tdata (abfd)->got == NULL
4942 && !create_got_section (abfd, info))
0a1b45a2 4943 return false;
5bd4f169
AM
4944
4945 if (h != NULL)
4946 {
411e1bfb
AM
4947 struct ppc_link_hash_entry *eh;
4948 struct got_entry *ent;
65f38f15 4949
ed7007c1 4950 eh = ppc_elf_hash_entry (h);
411e1bfb 4951 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4952 if (ent->addend == rel->r_addend
e717da7e 4953 && ent->owner == abfd
411e1bfb
AM
4954 && ent->tls_type == tls_type)
4955 break;
4956 if (ent == NULL)
5bd4f169 4957 {
986f0783 4958 size_t amt = sizeof (*ent);
4ce794b7 4959 ent = bfd_alloc (abfd, amt);
411e1bfb 4960 if (ent == NULL)
0a1b45a2 4961 return false;
411e1bfb 4962 ent->next = eh->elf.got.glist;
133a1f60 4963 ent->addend = rel->r_addend;
e717da7e 4964 ent->owner = abfd;
411e1bfb 4965 ent->tls_type = tls_type;
0a1b45a2 4966 ent->is_indirect = false;
411e1bfb
AM
4967 ent->got.refcount = 0;
4968 eh->elf.got.glist = ent;
5bd4f169 4969 }
411e1bfb 4970 ent->got.refcount += 1;
e7b938ca 4971 eh->tls_mask |= tls_type;
5bd4f169 4972 }
411e1bfb
AM
4973 else
4974 /* This is a global offset table entry for a local symbol. */
4975 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4976 rel->r_addend, tls_type))
0a1b45a2 4977 return false;
5bd4f169
AM
4978 break;
4979
5bd4f169 4980 case R_PPC64_PLT16_HA:
65f38f15
AM
4981 case R_PPC64_PLT16_HI:
4982 case R_PPC64_PLT16_LO:
08be3224 4983 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4984 case R_PPC64_PLT_PCREL34:
4985 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4986 case R_PPC64_PLT32:
4987 case R_PPC64_PLT64:
cbf95972
AM
4988 /* This symbol requires a procedure linkage table entry. */
4989 plt_list = ifunc;
4990 if (h != NULL)
e054468f 4991 {
e054468f
AM
4992 h->needs_plt = 1;
4993 if (h->root.root.string[0] == '.'
4994 && h->root.root.string[1] != '\0')
ed7007c1
AM
4995 ppc_elf_hash_entry (h)->is_func = 1;
4996 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4997 plt_list = &h->plt.plist;
4998 }
4999 if (plt_list == NULL)
2d7ad24e 5000 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5001 rel->r_addend,
2d7ad24e 5002 NON_GOT | PLT_KEEP);
133a1f60 5003 if (!update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5004 return false;
5bd4f169
AM
5005 break;
5006
5007 /* The following relocations don't need to propagate the
5008 relocation if linking a shared object since they are
5009 section relative. */
5010 case R_PPC64_SECTOFF:
5011 case R_PPC64_SECTOFF_LO:
5012 case R_PPC64_SECTOFF_HI:
5013 case R_PPC64_SECTOFF_HA:
5014 case R_PPC64_SECTOFF_DS:
5015 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5016 case R_PPC64_DTPREL16:
5017 case R_PPC64_DTPREL16_LO:
5018 case R_PPC64_DTPREL16_HI:
5019 case R_PPC64_DTPREL16_HA:
5020 case R_PPC64_DTPREL16_DS:
5021 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5022 case R_PPC64_DTPREL16_HIGH:
5023 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5024 case R_PPC64_DTPREL16_HIGHER:
5025 case R_PPC64_DTPREL16_HIGHERA:
5026 case R_PPC64_DTPREL16_HIGHEST:
5027 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5028 break;
5029
ad8e1ba5 5030 /* Nor do these. */
25f23106
AM
5031 case R_PPC64_REL16:
5032 case R_PPC64_REL16_LO:
5033 case R_PPC64_REL16_HI:
5034 case R_PPC64_REL16_HA:
4a969973
AM
5035 case R_PPC64_REL16_HIGH:
5036 case R_PPC64_REL16_HIGHA:
5037 case R_PPC64_REL16_HIGHER:
5038 case R_PPC64_REL16_HIGHERA:
5039 case R_PPC64_REL16_HIGHEST:
5040 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
5041 case R_PPC64_REL16_HIGHER34:
5042 case R_PPC64_REL16_HIGHERA34:
5043 case R_PPC64_REL16_HIGHEST34:
5044 case R_PPC64_REL16_HIGHESTA34:
a680de9a 5045 case R_PPC64_REL16DX_HA:
25f23106
AM
5046 break;
5047
45965137
AM
5048 /* Not supported as a dynamic relocation. */
5049 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5050 if (bfd_link_pic (info))
45965137
AM
5051 {
5052 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5053 ppc_howto_init ();
695344c0 5054 /* xgettext:c-format */
174d0a74 5055 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5056 "in shared libraries and PIEs\n"),
45965137
AM
5057 abfd, sec, rel->r_offset,
5058 ppc64_elf_howto_table[r_type]->name);
5059 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5060 return false;
45965137
AM
5061 }
5062 break;
5063
ad8e1ba5 5064 case R_PPC64_TOC16:
33c0ec9d
AM
5065 case R_PPC64_TOC16_DS:
5066 htab->do_multi_toc = 1;
d77c8a4b 5067 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5068 /* Fall through. */
ad8e1ba5
AM
5069 case R_PPC64_TOC16_LO:
5070 case R_PPC64_TOC16_HI:
5071 case R_PPC64_TOC16_HA:
ad8e1ba5 5072 case R_PPC64_TOC16_LO_DS:
4c52953f 5073 sec->has_toc_reloc = 1;
ec73ddcd 5074 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
5075 {
5076 /* We may need a copy reloc. */
5077 h->non_got_ref = 1;
5078 /* Strongly prefer a copy reloc over a dynamic reloc.
5079 glibc ld.so as of 2019-08 will error out if one of
5080 these relocations is emitted. */
5081 h->needs_copy = 1;
5082 goto dodyn;
5083 }
ad8e1ba5
AM
5084 break;
5085
006589cf
AM
5086 /* Marker reloc. */
5087 case R_PPC64_ENTRY:
5088 break;
5089
5bd4f169
AM
5090 /* This relocation describes the C++ object vtable hierarchy.
5091 Reconstruct it for later use during GC. */
5092 case R_PPC64_GNU_VTINHERIT:
c152c796 5093 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
0a1b45a2 5094 return false;
5bd4f169
AM
5095 break;
5096
5097 /* This relocation describes which C++ vtable entries are actually
5098 used. Record for later use during GC. */
5099 case R_PPC64_GNU_VTENTRY:
a0ea3a14 5100 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
0a1b45a2 5101 return false;
5bd4f169
AM
5102 break;
5103
721956f4
AM
5104 case R_PPC64_REL14:
5105 case R_PPC64_REL14_BRTAKEN:
5106 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5107 {
5108 asection *dest = NULL;
5109
5110 /* Heuristic: If jumping outside our section, chances are
5111 we are going to need a stub. */
5112 if (h != NULL)
5113 {
5114 /* If the sym is weak it may be overridden later, so
5115 don't assume we know where a weak sym lives. */
5116 if (h->root.type == bfd_link_hash_defined)
5117 dest = h->root.u.def.section;
5118 }
5119 else
87d72d41
AM
5120 {
5121 Elf_Internal_Sym *isym;
5122
f1dfbfdb 5123 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5124 abfd, r_symndx);
5125 if (isym == NULL)
0a1b45a2 5126 return false;
87d72d41
AM
5127
5128 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5129 }
5130
220c76dd 5131 if (dest != sec)
7c8fe5c4 5132 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5133 }
3e04d765
AM
5134 goto rel24;
5135
5136 case R_PPC64_PLTCALL:
5663e321 5137 case R_PPC64_PLTCALL_NOTOC:
3e04d765 5138 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5139 /* Fall through. */
5140
5d1634d7 5141 case R_PPC64_REL24:
05d0e962 5142 case R_PPC64_REL24_NOTOC:
7aba54da 5143 case R_PPC64_REL24_P9NOTOC:
3e04d765 5144 rel24:
cbf95972
AM
5145 plt_list = ifunc;
5146 if (h != NULL)
5d1634d7 5147 {
e054468f
AM
5148 h->needs_plt = 1;
5149 if (h->root.root.string[0] == '.'
5150 && h->root.root.string[1] != '\0')
ed7007c1 5151 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 5152
3a71aa26 5153 if (h == tga || h == dottga)
cbf95972
AM
5154 {
5155 sec->has_tls_reloc = 1;
5156 if (rel != relocs
5157 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5158 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5159 /* We have a new-style __tls_get_addr call with
5160 a marker reloc. */
5161 ;
5162 else
5163 /* Mark this section as having an old-style call. */
9737e8af 5164 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5165 }
5166 plt_list = &h->plt.plist;
411e1bfb 5167 }
cbf95972
AM
5168
5169 /* We may need a .plt entry if the function this reloc
5170 refers to is in a shared lib. */
5171 if (plt_list
133a1f60 5172 && !update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5173 return false;
411e1bfb
AM
5174 break;
5175
cbf95972
AM
5176 case R_PPC64_ADDR14:
5177 case R_PPC64_ADDR14_BRNTAKEN:
5178 case R_PPC64_ADDR14_BRTAKEN:
5179 case R_PPC64_ADDR24:
5180 goto dodyn;
5181
411e1bfb
AM
5182 case R_PPC64_TPREL64:
5183 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5184 if (bfd_link_dll (info))
411e1bfb
AM
5185 info->flags |= DF_STATIC_TLS;
5186 goto dotlstoc;
5187
5188 case R_PPC64_DTPMOD64:
5189 if (rel + 1 < rel_end
5190 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5191 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5192 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5193 else
951fd09b 5194 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5195 goto dotlstoc;
5196
5197 case R_PPC64_DTPREL64:
5198 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5199 if (rel != relocs
5200 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5201 && rel[-1].r_offset == rel->r_offset - 8)
5202 /* This is the second reloc of a dtpmod, dtprel pair.
5203 Don't mark with TLS_DTPREL. */
5204 goto dodyn;
5205
5206 dotlstoc:
5207 sec->has_tls_reloc = 1;
5208 if (h != NULL)
ed7007c1 5209 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5210 else
5211 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5212 rel->r_addend, tls_type))
0a1b45a2 5213 return false;
411e1bfb 5214
7c8fe5c4
AM
5215 ppc64_sec = ppc64_elf_section_data (sec);
5216 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5217 {
3a71aa26
AM
5218 bfd_size_type amt;
5219
e7b938ca 5220 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5221 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5222 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5223 if (ppc64_sec->u.toc.symndx == NULL)
0a1b45a2 5224 return false;
3a71aa26
AM
5225 amt = sec->size * sizeof (bfd_vma) / 8;
5226 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5227 if (ppc64_sec->u.toc.add == NULL)
0a1b45a2 5228 return false;
7c8fe5c4
AM
5229 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5230 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5231 }
5232 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5233 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5234 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5235
5236 /* Mark the second slot of a GD or LD entry.
5237 -1 to indicate GD and -2 to indicate LD. */
5238 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5239 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5240 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5241 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5242 goto dodyn;
5243
411e1bfb
AM
5244 case R_PPC64_TPREL16_HI:
5245 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5246 case R_PPC64_TPREL16_HIGH:
5247 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5248 case R_PPC64_TPREL16_HIGHER:
5249 case R_PPC64_TPREL16_HIGHERA:
5250 case R_PPC64_TPREL16_HIGHEST:
5251 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5252 sec->has_tls_reloc = 1;
5253 /* Fall through. */
c213164a 5254 case R_PPC64_TPREL34:
252dcdf4
AM
5255 case R_PPC64_TPREL16:
5256 case R_PPC64_TPREL16_DS:
5257 case R_PPC64_TPREL16_LO:
5258 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5259 if (bfd_link_dll (info))
5260 info->flags |= DF_STATIC_TLS;
5261 goto dodyn;
5d1634d7 5262
e86ce104 5263 case R_PPC64_ADDR64:
b9399fcf 5264 if (is_opd
1e2f5b6e 5265 && rel + 1 < rel_end
4ce794b7 5266 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5267 {
8387904d 5268 if (h != NULL)
ed7007c1 5269 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5270 }
e86ce104
AM
5271 /* Fall through. */
5272
65f38f15
AM
5273 case R_PPC64_ADDR16:
5274 case R_PPC64_ADDR16_DS:
5275 case R_PPC64_ADDR16_HA:
5276 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5277 case R_PPC64_ADDR16_HIGH:
5278 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5279 case R_PPC64_ADDR16_HIGHER:
5280 case R_PPC64_ADDR16_HIGHERA:
5281 case R_PPC64_ADDR16_HIGHEST:
5282 case R_PPC64_ADDR16_HIGHESTA:
5283 case R_PPC64_ADDR16_LO:
5284 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5285 case R_PPC64_D34:
5286 case R_PPC64_D34_LO:
5287 case R_PPC64_D34_HI30:
5288 case R_PPC64_D34_HA30:
5289 case R_PPC64_ADDR16_HIGHER34:
5290 case R_PPC64_ADDR16_HIGHERA34:
5291 case R_PPC64_ADDR16_HIGHEST34:
5292 case R_PPC64_ADDR16_HIGHESTA34:
5293 case R_PPC64_D28:
0e1862bb 5294 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5295 && rel->r_addend == 0)
5296 {
5297 /* We may need a .plt entry if this reloc refers to a
5298 function in a shared lib. */
5663e321 5299 if (!update_plt_info (abfd, &h->plt.plist, 0))
0a1b45a2 5300 return false;
a345bc8d
AM
5301 h->pointer_equality_needed = 1;
5302 }
5303 /* Fall through. */
5304
5305 case R_PPC64_REL30:
5306 case R_PPC64_REL32:
5307 case R_PPC64_REL64:
65f38f15 5308 case R_PPC64_ADDR32:
65f38f15
AM
5309 case R_PPC64_UADDR16:
5310 case R_PPC64_UADDR32:
5311 case R_PPC64_UADDR64:
5bd4f169 5312 case R_PPC64_TOC:
ec73ddcd 5313 if (h != NULL && bfd_link_executable (info))
81848ca0 5314 /* We may need a copy reloc. */
f5385ebf 5315 h->non_got_ref = 1;
81848ca0 5316
41bd81ab 5317 /* Don't propagate .opd relocs. */
b9399fcf 5318 if (NO_OPD_RELOCS && is_opd)
e86ce104 5319 break;
e86ce104 5320
65f38f15
AM
5321 /* If we are creating a shared library, and this is a reloc
5322 against a global symbol, or a non PC relative reloc
5323 against a local symbol, then we need to copy the reloc
5324 into the shared library. However, if we are linking with
5325 -Bsymbolic, we do not need to copy a reloc against a
5326 global symbol which is defined in an object we are
5327 including in the link (i.e., DEF_REGULAR is set). At
5328 this point we have not seen all the input files, so it is
5329 possible that DEF_REGULAR is not set now but will be set
5330 later (it is never cleared). In case of a weak definition,
5331 DEF_REGULAR may be cleared later by a strong definition in
5332 a shared library. We account for that possibility below by
f4656909 5333 storing information in the dyn_relocs field of the hash
65f38f15
AM
5334 table entry. A similar situation occurs when creating
5335 shared libraries and symbol visibility changes render the
5336 symbol local.
5337
5338 If on the other hand, we are creating an executable, we
5339 may need to keep relocations for symbols satisfied by a
5340 dynamic library if we manage to avoid copy relocs for the
5341 symbol. */
411e1bfb 5342 dodyn:
ec73ddcd
AM
5343 if ((h != NULL
5344 && (h->root.type == bfd_link_hash_defweak
5345 || !h->def_regular))
5346 || (h != NULL
5347 && !bfd_link_executable (info)
5348 && !SYMBOLIC_BIND (info, h))
5349 || (bfd_link_pic (info)
5350 && must_be_dyn_reloc (info, r_type))
0e1862bb 5351 || (!bfd_link_pic (info)
25f23106 5352 && ifunc != NULL))
5bd4f169 5353 {
65f38f15
AM
5354 /* We must copy these reloc types into the output file.
5355 Create a reloc section in dynobj and make room for
5356 this reloc. */
5bd4f169
AM
5357 if (sreloc == NULL)
5358 {
83bac4b0 5359 sreloc = _bfd_elf_make_dynamic_reloc_section
0a1b45a2 5360 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
65f38f15 5361
5bd4f169 5362 if (sreloc == NULL)
0a1b45a2 5363 return false;
5bd4f169
AM
5364 }
5365
65f38f15
AM
5366 /* If this is a global symbol, we count the number of
5367 relocations we need for this symbol. */
5368 if (h != NULL)
5369 {
1657026c
AM
5370 struct ppc_dyn_relocs *p;
5371 struct ppc_dyn_relocs **head;
19e08130 5372
1657026c 5373 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
5374 p = *head;
5375 if (p == NULL || p->sec != sec)
5376 {
5377 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5378 if (p == NULL)
0a1b45a2 5379 return false;
19e08130
AM
5380 p->next = *head;
5381 *head = p;
5382 p->sec = sec;
5383 p->count = 0;
5384 p->pc_count = 0;
1657026c 5385 p->rel_count = 0;
19e08130
AM
5386 }
5387 p->count += 1;
5388 if (!must_be_dyn_reloc (info, r_type))
5389 p->pc_count += 1;
1657026c
AM
5390 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5391 && rel->r_offset % 2 == 0
5392 && sec->alignment_power != 0
858b0eff 5393 && ((!NO_OPD_RELOCS && is_opd) || !ifunc))
1657026c 5394 p->rel_count += 1;
65f38f15
AM
5395 }
5396 else
5397 {
ec338859
AM
5398 /* Track dynamic relocs needed for local syms too.
5399 We really need local syms available to do this
5400 easily. Oh well. */
1657026c
AM
5401 struct ppc_local_dyn_relocs *p;
5402 struct ppc_local_dyn_relocs **head;
0a1b45a2 5403 bool is_ifunc;
ec338859 5404 asection *s;
6edfbbad 5405 void *vpp;
87d72d41 5406 Elf_Internal_Sym *isym;
6edfbbad 5407
f1dfbfdb 5408 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5409 abfd, r_symndx);
5410 if (isym == NULL)
0a1b45a2 5411 return false;
ec338859 5412
87d72d41
AM
5413 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5414 if (s == NULL)
5415 s = sec;
5416
6edfbbad 5417 vpp = &elf_section_data (s)->local_dynrel;
1657026c 5418 head = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
5419 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5420 p = *head;
5421 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5422 p = p->next;
5423 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5424 {
5425 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5426 if (p == NULL)
0a1b45a2 5427 return false;
19e08130
AM
5428 p->next = *head;
5429 *head = p;
5430 p->sec = sec;
19e08130 5431 p->count = 0;
1657026c
AM
5432 p->rel_count = 0;
5433 p->ifunc = is_ifunc;
19e08130
AM
5434 }
5435 p->count += 1;
1657026c
AM
5436 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5437 && rel->r_offset % 2 == 0
5438 && sec->alignment_power != 0
5439 && ((!NO_OPD_RELOCS && is_opd) || !is_ifunc))
5440 p->rel_count += 1;
ec338859 5441 }
65f38f15 5442 }
5bd4f169 5443 break;
65f38f15
AM
5444
5445 default:
96e0dda4 5446 break;
5bd4f169
AM
5447 }
5448 }
5449
0a1b45a2 5450 return true;
5bd4f169
AM
5451}
5452
ee67d69a
AM
5453/* Merge backend specific data from an object file to the output
5454 object file when linking. */
5455
0a1b45a2 5456static bool
50e03d47 5457ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5458{
50e03d47 5459 bfd *obfd = info->output_bfd;
ee67d69a
AM
5460 unsigned long iflags, oflags;
5461
5462 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
0a1b45a2 5463 return true;
ee67d69a
AM
5464
5465 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
0a1b45a2 5466 return true;
ee67d69a 5467
50e03d47 5468 if (!_bfd_generic_verify_endian_match (ibfd, info))
0a1b45a2 5469 return false;
ee67d69a
AM
5470
5471 iflags = elf_elfheader (ibfd)->e_flags;
5472 oflags = elf_elfheader (obfd)->e_flags;
5473
f6c7c3e8 5474 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5475 {
4eca0228 5476 _bfd_error_handler
695344c0 5477 /* xgettext:c-format */
871b3ab2 5478 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a 5479 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5480 return false;
ee67d69a 5481 }
f6c7c3e8 5482 else if (iflags != oflags && iflags != 0)
ee67d69a 5483 {
4eca0228 5484 _bfd_error_handler
695344c0 5485 /* xgettext:c-format */
871b3ab2 5486 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5487 ibfd, iflags, oflags);
5488 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5489 return false;
ee67d69a
AM
5490 }
5491
4a91d0ba 5492 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
0a1b45a2 5493 return false;
005d79fd 5494
ee67d69a 5495 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5496 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5497}
5498
0a1b45a2 5499static bool
ee67d69a
AM
5500ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5501{
5502 /* Print normal ELF private data. */
5503 _bfd_elf_print_private_bfd_data (abfd, ptr);
5504
5505 if (elf_elfheader (abfd)->e_flags != 0)
5506 {
5507 FILE *file = ptr;
5508
ee67d69a
AM
5509 fprintf (file, _("private flags = 0x%lx:"),
5510 elf_elfheader (abfd)->e_flags);
5511
5512 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5513 fprintf (file, _(" [abiv%ld]"),
5514 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5515 fputc ('\n', file);
5516 }
5517
0a1b45a2 5518 return true;
ee67d69a
AM
5519}
5520
8387904d 5521/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5522 of the code entry point, and its section, which must be in the same
5523 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5524
5525static bfd_vma
5526opd_entry_value (asection *opd_sec,
5527 bfd_vma offset,
5528 asection **code_sec,
aef36ac1 5529 bfd_vma *code_off,
0a1b45a2 5530 bool in_code_sec)
8387904d
AM
5531{
5532 bfd *opd_bfd = opd_sec->owner;
8860955f 5533 Elf_Internal_Rela *relocs;
8387904d 5534 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5535 bfd_vma val;
8387904d 5536
9f296da3
AM
5537 /* No relocs implies we are linking a --just-symbols object, or looking
5538 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5539 if (opd_sec->reloc_count == 0)
5540 {
729eabd5 5541 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5542
729eabd5
AM
5543 if (contents == NULL)
5544 {
5545 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5546 return (bfd_vma) -1;
5547 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5548 }
ee1e4ede 5549
dbb3fbbb 5550 /* PR 17512: file: 64b9dfbb. */
451dfd38 5551 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5552 return (bfd_vma) -1;
5553
729eabd5 5554 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5555 if (code_sec != NULL)
5556 {
5557 asection *sec, *likely = NULL;
ee1e4ede 5558
aef36ac1 5559 if (in_code_sec)
4b85d634 5560 {
aef36ac1
AM
5561 sec = *code_sec;
5562 if (sec->vma <= val
5563 && val < sec->vma + sec->size)
5564 likely = sec;
5565 else
5566 val = -1;
5567 }
5568 else
5569 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5570 if (sec->vma <= val
5571 && (sec->flags & SEC_LOAD) != 0
5572 && (sec->flags & SEC_ALLOC) != 0)
5573 likely = sec;
5574 if (likely != NULL)
5575 {
5576 *code_sec = likely;
5577 if (code_off != NULL)
5578 *code_off = val - likely->vma;
4b85d634
AM
5579 }
5580 }
aef36ac1 5581 return val;
4b85d634
AM
5582 }
5583
0c8d6e5c 5584 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5585
729eabd5 5586 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f 5587 if (relocs == NULL)
0a1b45a2 5588 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
877a8638
NC
5589 /* PR 17512: file: df8e1fd6. */
5590 if (relocs == NULL)
5591 return (bfd_vma) -1;
645ea6a9 5592
8387904d 5593 /* Go find the opd reloc at the sym address. */
8860955f 5594 lo = relocs;
8387904d 5595 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5596 val = (bfd_vma) -1;
8387904d
AM
5597 while (lo < hi)
5598 {
5599 look = lo + (hi - lo) / 2;
5600 if (look->r_offset < offset)
5601 lo = look + 1;
5602 else if (look->r_offset > offset)
5603 hi = look;
5604 else
5605 {
0ffa91dd
NC
5606 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5607
8387904d
AM
5608 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5609 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5610 {
5611 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5612 asection *sec = NULL;
8387904d 5613
b53dfeb2
AM
5614 if (symndx >= symtab_hdr->sh_info
5615 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5616 {
5617 struct elf_link_hash_entry **sym_hashes;
5618 struct elf_link_hash_entry *rh;
5619
5620 sym_hashes = elf_sym_hashes (opd_bfd);
5621 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5622 if (rh != NULL)
5623 {
5624 rh = elf_follow_link (rh);
bb854a36
AM
5625 if (rh->root.type != bfd_link_hash_defined
5626 && rh->root.type != bfd_link_hash_defweak)
5627 break;
5628 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5629 {
bb854a36
AM
5630 val = rh->root.u.def.value;
5631 sec = rh->root.u.def.section;
b53dfeb2
AM
5632 }
5633 }
5634 }
5635
5636 if (sec == NULL)
5637 {
5638 Elf_Internal_Sym *sym;
5639
5640 if (symndx < symtab_hdr->sh_info)
5641 {
5642 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5643 if (sym == NULL)
5644 {
5645 size_t symcnt = symtab_hdr->sh_info;
5646 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5647 symcnt, 0,
5648 NULL, NULL, NULL);
5649 if (sym == NULL)
5650 break;
5651 symtab_hdr->contents = (bfd_byte *) sym;
5652 }
5653 sym += symndx;
128205bb
AM
5654 }
5655 else
5656 {
b53dfeb2
AM
5657 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5658 1, symndx,
5659 NULL, NULL, NULL);
128205bb
AM
5660 if (sym == NULL)
5661 break;
128205bb 5662 }
b53dfeb2
AM
5663 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5664 if (sec == NULL)
5665 break;
5666 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5667 val = sym->st_value;
8387904d 5668 }
b53dfeb2 5669
8387904d
AM
5670 val += look->r_addend;
5671 if (code_off != NULL)
5672 *code_off = val;
5673 if (code_sec != NULL)
aef36ac1
AM
5674 {
5675 if (in_code_sec && *code_sec != sec)
5676 return -1;
5677 else
5678 *code_sec = sec;
5679 }
b53dfeb2 5680 if (sec->output_section != NULL)
8387904d 5681 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5682 }
5683 break;
5684 }
5685 }
645ea6a9 5686
645ea6a9 5687 return val;
8387904d
AM
5688}
5689
aef36ac1
AM
5690/* If the ELF symbol SYM might be a function in SEC, return the
5691 function size and set *CODE_OFF to the function's entry point,
5692 otherwise return zero. */
9f296da3 5693
aef36ac1
AM
5694static bfd_size_type
5695ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5696 bfd_vma *code_off)
9f296da3 5697{
aef36ac1 5698 bfd_size_type size;
24aebc79 5699 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1
AM
5700
5701 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5702 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5703 return 0;
5704
24aebc79
NC
5705 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5706
5707 /* In theory we should check that the symbol's type satisfies
5708 _bfd_elf_is_function_type(), but there are some function-like
5709 symbols which would fail this test. (eg _start). Instead
5710 we check for hidden, local, notype symbols with zero size.
5711 This type of symbol is generated by the annobin plugin for gcc
5712 and clang, and should not be considered to be a function symbol. */
5713 if (size == 0
5714 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5715 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5716 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5717 return 0;
aef36ac1
AM
5718
5719 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5720 {
b07bca4e
AM
5721 struct _opd_sec_data *opd = get_opd_info (sym->section);
5722 bfd_vma symval = sym->value;
5723
5724 if (opd != NULL
5725 && opd->adjust != NULL
5726 && elf_section_data (sym->section)->relocs != NULL)
5727 {
5728 /* opd_entry_value will use cached relocs that have been
5729 adjusted, but with raw symbols. That means both local
5730 and global symbols need adjusting. */
5731 long adjust = opd->adjust[OPD_NDX (symval)];
5732 if (adjust == -1)
5733 return 0;
5734 symval += adjust;
5735 }
5736
5737 if (opd_entry_value (sym->section, symval,
0a1b45a2 5738 &sec, code_off, true) == (bfd_vma) -1)
aef36ac1
AM
5739 return 0;
5740 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5741 symbol. This size has nothing to do with the code size of the
5742 function, which is what we're supposed to return, but the
5743 code size isn't available without looking up the dot-sym.
5744 However, doing that would be a waste of time particularly
5745 since elf_find_function will look at the dot-sym anyway.
5746 Now, elf_find_function will keep the largest size of any
5747 function sym found at the code address of interest, so return
5748 1 here to avoid it incorrectly caching a larger function size
5749 for a small function. This does mean we return the wrong
5750 size for a new-ABI function of size 24, but all that does is
5751 disable caching for such functions. */
5752 if (size == 24)
5753 size = 1;
9f296da3 5754 }
aef36ac1
AM
5755 else
5756 {
5757 if (sym->section != sec)
5758 return 0;
5759 *code_off = sym->value;
5760 }
24aebc79
NC
5761
5762 /* Do not return 0 for the function's size. */
5763 return size ? size : 1;
9f296da3
AM
5764}
5765
f378ab09
AM
5766/* Return true if symbol is a strong function defined in an ELFv2
5767 object with st_other localentry bits of zero, ie. its local entry
5768 point coincides with its global entry point. */
5769
0a1b45a2 5770static bool
f378ab09
AM
5771is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5772{
5773 return (h != NULL
5774 && h->type == STT_FUNC
5775 && h->root.type == bfd_link_hash_defined
5776 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5777 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5778 && is_ppc64_elf (h->root.u.def.section->owner)
5779 && abiversion (h->root.u.def.section->owner) >= 2);
5780}
5781
854b41e7
AM
5782/* Return true if symbol is defined in a regular object file. */
5783
0a1b45a2 5784static bool
854b41e7
AM
5785is_static_defined (struct elf_link_hash_entry *h)
5786{
5787 return ((h->root.type == bfd_link_hash_defined
5788 || h->root.type == bfd_link_hash_defweak)
5789 && h->root.u.def.section != NULL
5790 && h->root.u.def.section->output_section != NULL);
5791}
5792
b31867b6
AM
5793/* If FDH is a function descriptor symbol, return the associated code
5794 entry symbol if it is defined. Return NULL otherwise. */
5795
5796static struct ppc_link_hash_entry *
5797defined_code_entry (struct ppc_link_hash_entry *fdh)
5798{
5799 if (fdh->is_func_descriptor)
5800 {
5801 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5802 if (fh->elf.root.type == bfd_link_hash_defined
5803 || fh->elf.root.type == bfd_link_hash_defweak)
5804 return fh;
5805 }
5806 return NULL;
5807}
5808
5809/* If FH is a function code entry symbol, return the associated
5810 function descriptor symbol if it is defined. Return NULL otherwise. */
5811
5812static struct ppc_link_hash_entry *
5813defined_func_desc (struct ppc_link_hash_entry *fh)
5814{
5815 if (fh->oh != NULL
5816 && fh->oh->is_func_descriptor)
5817 {
5818 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5819 if (fdh->elf.root.type == bfd_link_hash_defined
5820 || fdh->elf.root.type == bfd_link_hash_defweak)
5821 return fdh;
5822 }
5823 return NULL;
5824}
5825
ed7007c1
AM
5826/* Given H is a symbol that satisfies is_static_defined, return the
5827 value in the output file. */
5828
5829static bfd_vma
5830defined_sym_val (struct elf_link_hash_entry *h)
5831{
5832 return (h->root.u.def.section->output_section->vma
5833 + h->root.u.def.section->output_offset
5834 + h->root.u.def.value);
5835}
5836
5837/* Return true if H matches __tls_get_addr or one of its variants. */
5838
0a1b45a2 5839static bool
ed7007c1
AM
5840is_tls_get_addr (struct elf_link_hash_entry *h,
5841 struct ppc_link_hash_table *htab)
5842{
30845f11
AM
5843 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5844 || h == elf_hash_entry (htab->tga_desc_fd)
5845 || h == elf_hash_entry (htab->tls_get_addr)
5846 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5847}
5848
0a1b45a2 5849static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
8c5b4e52
AM
5850
5851/* Garbage collect sections, after first dealing with dot-symbols. */
5852
0a1b45a2 5853static bool
8c5b4e52
AM
5854ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5855{
5856 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5857
5858 if (htab != NULL && htab->need_func_desc_adj)
5859 {
5860 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5861 htab->need_func_desc_adj = 0;
5862 }
5863 return bfd_elf_gc_sections (abfd, info);
5864}
5865
74f0fb50
AM
5866/* Mark all our entry sym sections, both opd and code section. */
5867
5868static void
5869ppc64_elf_gc_keep (struct bfd_link_info *info)
5870{
5871 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5872 struct bfd_sym_chain *sym;
5873
4dfe6ac6
NC
5874 if (htab == NULL)
5875 return;
5876
74f0fb50
AM
5877 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5878 {
b31867b6 5879 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5880 asection *sec;
5881
ed7007c1 5882 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
0a1b45a2 5883 false, false, true));
74f0fb50
AM
5884 if (eh == NULL)
5885 continue;
5886 if (eh->elf.root.type != bfd_link_hash_defined
5887 && eh->elf.root.type != bfd_link_hash_defweak)
5888 continue;
5889
b31867b6
AM
5890 fh = defined_code_entry (eh);
5891 if (fh != NULL)
74f0fb50 5892 {
b31867b6 5893 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5894 sec->flags |= SEC_KEEP;
5895 }
5896 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5897 && opd_entry_value (eh->elf.root.u.def.section,
5898 eh->elf.root.u.def.value,
0a1b45a2 5899 &sec, NULL, false) != (bfd_vma) -1)
74f0fb50
AM
5900 sec->flags |= SEC_KEEP;
5901
5902 sec = eh->elf.root.u.def.section;
5903 sec->flags |= SEC_KEEP;
5904 }
5905}
5906
64d03ab5
AM
5907/* Mark sections containing dynamically referenced symbols. When
5908 building shared libraries, we must assume that any visible symbol is
5909 referenced. */
5910
0a1b45a2 5911static bool
64d03ab5
AM
5912ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5913{
5914 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5915 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5916 struct ppc_link_hash_entry *fdh;
b407645f 5917 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5918
64d03ab5 5919 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5920 fdh = defined_func_desc (eh);
5921 if (fdh != NULL)
5922 eh = fdh;
64d03ab5
AM
5923
5924 if ((eh->elf.root.type == bfd_link_hash_defined
5925 || eh->elf.root.type == bfd_link_hash_defweak)
b80e421f
AM
5926 && (!eh->elf.start_stop
5927 || eh->elf.root.ldscript_def
5928 || !info->start_stop_gc)
87e79a65 5929 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5930 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5931 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5932 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5933 && (!bfd_link_executable (info)
e278ae05 5934 || info->gc_keep_exported
b407645f
AM
5935 || info->export_dynamic
5936 || (eh->elf.dynamic
5937 && d != NULL
2cdcc330
AM
5938 && (*d->match) (&d->head, NULL,
5939 eh->elf.root.root.string)))
e278ae05 5940 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5941 || !bfd_hide_sym_by_version (info->version_info,
5942 eh->elf.root.root.string)))))
64d03ab5
AM
5943 {
5944 asection *code_sec;
b31867b6 5945 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5946
5947 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5948
5949 /* Function descriptor syms cause the associated
5950 function code sym section to be marked. */
b31867b6
AM
5951 fh = defined_code_entry (eh);
5952 if (fh != NULL)
5953 {
5954 code_sec = fh->elf.root.u.def.section;
5955 code_sec->flags |= SEC_KEEP;
5956 }
64d03ab5
AM
5957 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5958 && opd_entry_value (eh->elf.root.u.def.section,
5959 eh->elf.root.u.def.value,
0a1b45a2 5960 &code_sec, NULL, false) != (bfd_vma) -1)
64d03ab5
AM
5961 code_sec->flags |= SEC_KEEP;
5962 }
5963
0a1b45a2 5964 return true;
64d03ab5
AM
5965}
5966
5bd4f169
AM
5967/* Return the section that should be marked against GC for a given
5968 relocation. */
5969
5970static asection *
4ce794b7 5971ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5972 struct bfd_link_info *info,
4ce794b7
AM
5973 Elf_Internal_Rela *rel,
5974 struct elf_link_hash_entry *h,
5975 Elf_Internal_Sym *sym)
5bd4f169 5976{
ccfa59ea
AM
5977 asection *rsec;
5978
ccfa59ea
AM
5979 /* Syms return NULL if we're marking .opd, so we avoid marking all
5980 function sections, as all functions are referenced in .opd. */
5981 rsec = NULL;
5982 if (get_opd_info (sec) != NULL)
5983 return rsec;
1e2f5b6e 5984
5bd4f169
AM
5985 if (h != NULL)
5986 {
04c9666a 5987 enum elf_ppc64_reloc_type r_type;
b31867b6 5988 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5989
4ce794b7 5990 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5991 switch (r_type)
5bd4f169
AM
5992 {
5993 case R_PPC64_GNU_VTINHERIT:
5994 case R_PPC64_GNU_VTENTRY:
5995 break;
5996
5997 default:
5998 switch (h->root.type)
5999 {
6000 case bfd_link_hash_defined:
6001 case bfd_link_hash_defweak:
ed7007c1 6002 eh = ppc_elf_hash_entry (h);
b31867b6
AM
6003 fdh = defined_func_desc (eh);
6004 if (fdh != NULL)
8c5b4e52
AM
6005 {
6006 /* -mcall-aixdesc code references the dot-symbol on
6007 a call reloc. Mark the function descriptor too
6008 against garbage collection. */
6009 fdh->elf.mark = 1;
60d67dc8
AM
6010 if (fdh->elf.is_weakalias)
6011 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6012 eh = fdh;
6013 }
1e2f5b6e
AM
6014
6015 /* Function descriptor syms cause the associated
6016 function code sym section to be marked. */
b31867b6
AM
6017 fh = defined_code_entry (eh);
6018 if (fh != NULL)
ccfa59ea
AM
6019 {
6020 /* They also mark their opd section. */
74f0fb50 6021 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6022
b31867b6 6023 rsec = fh->elf.root.u.def.section;
ccfa59ea 6024 }
8387904d
AM
6025 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6026 && opd_entry_value (eh->elf.root.u.def.section,
6027 eh->elf.root.u.def.value,
0a1b45a2 6028 &rsec, NULL, false) != (bfd_vma) -1)
74f0fb50 6029 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6030 else
1e2f5b6e
AM
6031 rsec = h->root.u.def.section;
6032 break;
5bd4f169
AM
6033
6034 case bfd_link_hash_common:
1e2f5b6e
AM
6035 rsec = h->root.u.c.p->section;
6036 break;
5bd4f169
AM
6037
6038 default:
fb34365b 6039 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6040 }
6041 }
6042 }
6043 else
6044 {
74f0fb50 6045 struct _opd_sec_data *opd;
1e2f5b6e
AM
6046
6047 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6048 opd = get_opd_info (rsec);
6049 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6050 {
74f0fb50 6051 rsec->gc_mark = 1;
ccfa59ea 6052
51aecdc5 6053 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6054 }
5bd4f169
AM
6055 }
6056
1e2f5b6e 6057 return rsec;
5bd4f169
AM
6058}
6059
deb0e272
AM
6060/* The maximum size of .sfpr. */
6061#define SFPR_MAX (218*4)
6062
6063struct sfpr_def_parms
6064{
699733f6
AM
6065 const char name[12];
6066 unsigned char lo, hi;
2cdcc330
AM
6067 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6068 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
6069};
6070
a4b6fadd
AM
6071/* Auto-generate _save*, _rest* functions in .sfpr.
6072 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6073 instead. */
deb0e272 6074
0a1b45a2 6075static bool
a4b6fadd
AM
6076sfpr_define (struct bfd_link_info *info,
6077 const struct sfpr_def_parms *parm,
6078 asection *stub_sec)
deb0e272
AM
6079{
6080 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6081 unsigned int i;
6082 size_t len = strlen (parm->name);
0a1b45a2 6083 bool writing = false;
699733f6 6084 char sym[16];
deb0e272 6085
4dfe6ac6 6086 if (htab == NULL)
0a1b45a2 6087 return false;
4dfe6ac6 6088
deb0e272
AM
6089 memcpy (sym, parm->name, len);
6090 sym[len + 2] = 0;
6091
6092 for (i = parm->lo; i <= parm->hi; i++)
6093 {
a4b6fadd 6094 struct ppc_link_hash_entry *h;
deb0e272
AM
6095
6096 sym[len + 0] = i / 10 + '0';
6097 sym[len + 1] = i % 10 + '0';
ed7007c1 6098 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
0a1b45a2 6099 writing, true, true));
a4b6fadd 6100 if (stub_sec != NULL)
deb0e272 6101 {
a4b6fadd
AM
6102 if (h != NULL
6103 && h->elf.root.type == bfd_link_hash_defined
6104 && h->elf.root.u.def.section == htab->sfpr)
6105 {
6106 struct elf_link_hash_entry *s;
6107 char buf[32];
6108 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
0a1b45a2 6109 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
a4b6fadd 6110 if (s == NULL)
0a1b45a2 6111 return false;
779f2ae7 6112 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
6113 {
6114 s->root.type = bfd_link_hash_defined;
6115 s->root.u.def.section = stub_sec;
7dda8d3c 6116 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6117 + h->elf.root.u.def.value);
6118 s->ref_regular = 1;
6119 s->def_regular = 1;
6120 s->ref_regular_nonweak = 1;
6121 s->forced_local = 1;
6122 s->non_elf = 0;
6123 s->root.linker_def = 1;
6124 }
6125 }
6126 continue;
6127 }
6128 if (h != NULL)
6129 {
6130 h->save_res = 1;
6131 if (!h->elf.def_regular)
deb0e272 6132 {
a4b6fadd
AM
6133 h->elf.root.type = bfd_link_hash_defined;
6134 h->elf.root.u.def.section = htab->sfpr;
6135 h->elf.root.u.def.value = htab->sfpr->size;
6136 h->elf.type = STT_FUNC;
6137 h->elf.def_regular = 1;
b32547cd 6138 h->elf.non_elf = 0;
0a1b45a2
AM
6139 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6140 writing = true;
deb0e272 6141 if (htab->sfpr->contents == NULL)
a4b6fadd 6142 {
2cdcc330
AM
6143 htab->sfpr->contents
6144 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd 6145 if (htab->sfpr->contents == NULL)
0a1b45a2 6146 return false;
a4b6fadd 6147 }
deb0e272
AM
6148 }
6149 }
6150 if (writing)
6151 {
6152 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6153 if (i != parm->hi)
6154 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6155 else
6156 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6157 htab->sfpr->size = p - htab->sfpr->contents;
6158 }
6159 }
6160
0a1b45a2 6161 return true;
deb0e272
AM
6162}
6163
6164static bfd_byte *
6165savegpr0 (bfd *abfd, bfd_byte *p, int r)
6166{
6167 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6168 return p + 4;
6169}
6170
6171static bfd_byte *
6172savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6173{
6174 p = savegpr0 (abfd, p, r);
a078d95a 6175 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6176 p = p + 4;
6177 bfd_put_32 (abfd, BLR, p);
6178 return p + 4;
6179}
6180
6181static bfd_byte *
6182restgpr0 (bfd *abfd, bfd_byte *p, int r)
6183{
6184 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6185 return p + 4;
6186}
6187
6188static bfd_byte *
6189restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6190{
a078d95a 6191 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6192 p = p + 4;
6193 p = restgpr0 (abfd, p, r);
6194 bfd_put_32 (abfd, MTLR_R0, p);
6195 p = p + 4;
6196 if (r == 29)
6197 {
6198 p = restgpr0 (abfd, p, 30);
6199 p = restgpr0 (abfd, p, 31);
6200 }
6201 bfd_put_32 (abfd, BLR, p);
6202 return p + 4;
6203}
6204
6205static bfd_byte *
6206savegpr1 (bfd *abfd, bfd_byte *p, int r)
6207{
6208 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6209 return p + 4;
6210}
6211
6212static bfd_byte *
6213savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6214{
6215 p = savegpr1 (abfd, p, r);
6216 bfd_put_32 (abfd, BLR, p);
6217 return p + 4;
6218}
6219
6220static bfd_byte *
6221restgpr1 (bfd *abfd, bfd_byte *p, int r)
6222{
6223 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6224 return p + 4;
6225}
6226
6227static bfd_byte *
6228restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6229{
6230 p = restgpr1 (abfd, p, r);
6231 bfd_put_32 (abfd, BLR, p);
6232 return p + 4;
6233}
6234
6235static bfd_byte *
6236savefpr (bfd *abfd, bfd_byte *p, int r)
6237{
6238 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6239 return p + 4;
6240}
6241
6242static bfd_byte *
6243savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6244{
6245 p = savefpr (abfd, p, r);
a078d95a 6246 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6247 p = p + 4;
6248 bfd_put_32 (abfd, BLR, p);
6249 return p + 4;
6250}
6251
6252static bfd_byte *
6253restfpr (bfd *abfd, bfd_byte *p, int r)
6254{
6255 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6256 return p + 4;
6257}
6258
6259static bfd_byte *
6260restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6261{
a078d95a 6262 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6263 p = p + 4;
6264 p = restfpr (abfd, p, r);
6265 bfd_put_32 (abfd, MTLR_R0, p);
6266 p = p + 4;
6267 if (r == 29)
6268 {
6269 p = restfpr (abfd, p, 30);
6270 p = restfpr (abfd, p, 31);
6271 }
6272 bfd_put_32 (abfd, BLR, p);
6273 return p + 4;
6274}
6275
6276static bfd_byte *
6277savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6278{
6279 p = savefpr (abfd, p, r);
6280 bfd_put_32 (abfd, BLR, p);
6281 return p + 4;
6282}
6283
6284static bfd_byte *
6285restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6286{
6287 p = restfpr (abfd, p, r);
6288 bfd_put_32 (abfd, BLR, p);
6289 return p + 4;
6290}
6291
6292static bfd_byte *
6293savevr (bfd *abfd, bfd_byte *p, int r)
6294{
6295 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6296 p = p + 4;
6297 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6298 return p + 4;
6299}
6300
6301static bfd_byte *
6302savevr_tail (bfd *abfd, bfd_byte *p, int r)
6303{
6304 p = savevr (abfd, p, r);
6305 bfd_put_32 (abfd, BLR, p);
6306 return p + 4;
6307}
6308
6309static bfd_byte *
6310restvr (bfd *abfd, bfd_byte *p, int r)
6311{
6312 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6313 p = p + 4;
6314 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6315 return p + 4;
6316}
6317
6318static bfd_byte *
6319restvr_tail (bfd *abfd, bfd_byte *p, int r)
6320{
6321 p = restvr (abfd, p, r);
6322 bfd_put_32 (abfd, BLR, p);
6323 return p + 4;
6324}
6325
9e7028aa
AM
6326#define STDU_R1_0R1 0xf8210001
6327#define ADDI_R1_R1 0x38210000
6328
6329/* Emit prologue of wrapper preserving regs around a call to
6330 __tls_get_addr_opt. */
6331
6332static bfd_byte *
6333tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6334{
6335 unsigned int i;
6336
6337 bfd_put_32 (obfd, MFLR_R0, p);
6338 p += 4;
6339 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6340 p += 4;
6341
6342 if (htab->opd_abi)
6343 {
6344 for (i = 4; i < 12; i++)
6345 {
6346 bfd_put_32 (obfd,
6347 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6348 p += 4;
6349 }
6350 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6351 p += 4;
6352 }
6353 else
6354 {
6355 for (i = 4; i < 12; i++)
6356 {
6357 bfd_put_32 (obfd,
6358 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6359 p += 4;
6360 }
6361 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6362 p += 4;
6363 }
6364 return p;
6365}
6366
6367/* Emit epilogue of wrapper preserving regs around a call to
6368 __tls_get_addr_opt. */
6369
6370static bfd_byte *
6371tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6372{
6373 unsigned int i;
6374
6375 if (htab->opd_abi)
6376 {
6377 for (i = 4; i < 12; i++)
6378 {
6379 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6380 p += 4;
6381 }
6382 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6383 p += 4;
6384 }
6385 else
6386 {
6387 for (i = 4; i < 12; i++)
6388 {
6389 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6390 p += 4;
6391 }
6392 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6393 p += 4;
6394 }
6395 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6396 p += 4;
6397 bfd_put_32 (obfd, MTLR_R0, p);
6398 p += 4;
6399 bfd_put_32 (obfd, BLR, p);
6400 p += 4;
6401 return p;
6402}
6403
e86ce104
AM
6404/* Called via elf_link_hash_traverse to transfer dynamic linking
6405 information on function code symbol entries to their corresponding
a442059f
AM
6406 function descriptor symbol entries. Must not be called twice for
6407 any given code symbol. */
deb0e272 6408
0a1b45a2 6409static bool
4ce794b7 6410func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6411{
e86ce104 6412 struct bfd_link_info *info;
65f38f15 6413 struct ppc_link_hash_table *htab;
50bc7936
AM
6414 struct ppc_link_hash_entry *fh;
6415 struct ppc_link_hash_entry *fdh;
0a1b45a2 6416 bool force_local;
5bd4f169 6417
ed7007c1 6418 fh = ppc_elf_hash_entry (h);
50bc7936 6419 if (fh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 6420 return true;
e86ce104 6421
8c5b4e52 6422 if (!fh->is_func)
0a1b45a2 6423 return true;
8c5b4e52
AM
6424
6425 if (fh->elf.root.root.string[0] != '.'
6426 || fh->elf.root.root.string[1] == '\0')
0a1b45a2 6427 return true;
8c5b4e52 6428
4ce794b7 6429 info = inf;
65f38f15 6430 htab = ppc_hash_table (info);
4dfe6ac6 6431 if (htab == NULL)
0a1b45a2 6432 return false;
5bd4f169 6433
8c5b4e52
AM
6434 /* Find the corresponding function descriptor symbol. */
6435 fdh = lookup_fdh (fh, htab);
6436
c09bdfe5
AM
6437 /* Resolve undefined references to dot-symbols as the value
6438 in the function descriptor, if we have one in a regular object.
6439 This is to satisfy cases like ".quad .foo". Calls to functions
6440 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6441 if ((fh->elf.root.type == bfd_link_hash_undefined
6442 || fh->elf.root.type == bfd_link_hash_undefweak)
6443 && (fdh->elf.root.type == bfd_link_hash_defined
6444 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6445 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6446 && opd_entry_value (fdh->elf.root.u.def.section,
6447 fdh->elf.root.u.def.value,
c09bdfe5 6448 &fh->elf.root.u.def.section,
0a1b45a2 6449 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
c09bdfe5 6450 {
b31867b6 6451 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6452 fh->elf.forced_local = 1;
b31867b6
AM
6453 fh->elf.def_regular = fdh->elf.def_regular;
6454 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6455 }
6456
8c5b4e52
AM
6457 if (!fh->elf.dynamic)
6458 {
6459 struct plt_entry *ent;
5bd4f169 6460
8c5b4e52
AM
6461 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6462 if (ent->plt.refcount > 0)
6463 break;
6464 if (ent == NULL)
a442059f
AM
6465 {
6466 if (fdh != NULL && fdh->fake)
6467 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6468 return true;
6469 }
8c5b4e52 6470 }
5bd4f169 6471
8c5b4e52 6472 /* Create a descriptor as undefined if necessary. */
50bc7936 6473 if (fdh == NULL
0e1862bb 6474 && !bfd_link_executable (info)
50bc7936
AM
6475 && (fh->elf.root.type == bfd_link_hash_undefined
6476 || fh->elf.root.type == bfd_link_hash_undefweak))
6477 {
908b32fc 6478 fdh = make_fdh (info, fh);
bb700d78 6479 if (fdh == NULL)
0a1b45a2 6480 return false;
50bc7936 6481 }
648cca2c 6482
8c5b4e52 6483 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6484 if (fdh != NULL
6485 && fdh->fake
8c5b4e52
AM
6486 && (fh->elf.root.type == bfd_link_hash_defined
6487 || fh->elf.root.type == bfd_link_hash_defweak))
0a1b45a2 6488 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
908b32fc 6489
8c5b4e52
AM
6490 /* Transfer dynamic linking information to the function descriptor. */
6491 if (fdh != NULL)
6492 {
f5385ebf
AM
6493 fdh->elf.ref_regular |= fh->elf.ref_regular;
6494 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6495 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6496 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6497 fdh->elf.dynamic |= fh->elf.dynamic;
6498 fdh->elf.needs_plt |= (fh->elf.needs_plt
6499 || fh->elf.type == STT_FUNC
6500 || fh->elf.type == STT_GNU_IFUNC);
6501 move_plt_plist (fh, fdh);
6502
6503 if (!fdh->elf.forced_local
6504 && fh->elf.dynindx != -1)
6505 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 6506 return false;
e86ce104
AM
6507 }
6508
50bc7936
AM
6509 /* Now that the info is on the function descriptor, clear the
6510 function code sym info. Any function code syms for which we
6511 don't have a definition in a regular file, we force local.
6512 This prevents a shared library from exporting syms that have
6513 been imported from another library. Function code syms that
6514 are really in the library we must leave global to prevent the
6515 linker dragging in a definition from a static library. */
93f3fa99
AM
6516 force_local = (!fh->elf.def_regular
6517 || fdh == NULL
6518 || !fdh->elf.def_regular
6519 || fdh->elf.forced_local);
50bc7936
AM
6520 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6521
0a1b45a2 6522 return true;
e86ce104 6523}
40b8271b 6524
a4b6fadd
AM
6525static const struct sfpr_def_parms save_res_funcs[] =
6526 {
6527 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6528 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6529 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6530 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6531 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6532 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6533 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6534 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6535 { "._savef", 14, 31, savefpr, savefpr1_tail },
6536 { "._restf", 14, 31, restfpr, restfpr1_tail },
6537 { "_savevr_", 20, 31, savevr, savevr_tail },
6538 { "_restvr_", 20, 31, restvr, restvr_tail }
6539 };
6540
e86ce104 6541/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6542 this hook to a) run the edit functions in this file, b) provide
6543 some gcc support functions, and c) transfer dynamic linking
6544 information gathered so far on function code symbol entries, to
6545 their corresponding function descriptor symbol entries. */
deb0e272 6546
0a1b45a2 6547static bool
c9405344 6548ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6549{
6550 struct ppc_link_hash_table *htab;
6551
6552 htab = ppc_hash_table (info);
4dfe6ac6 6553 if (htab == NULL)
0a1b45a2 6554 return false;
4dfe6ac6 6555
c9405344
AM
6556 /* Call back into the linker, which then runs the edit functions. */
6557 htab->params->edit ();
6558
b32547cd
AM
6559 /* Provide any missing _save* and _rest* functions. */
6560 if (htab->sfpr != NULL)
6561 {
6562 unsigned int i;
6563
6564 htab->sfpr->size = 0;
6565 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6566 if (!sfpr_define (info, &save_res_funcs[i], NULL))
0a1b45a2 6567 return false;
b32547cd
AM
6568 if (htab->sfpr->size == 0)
6569 htab->sfpr->flags |= SEC_EXCLUDE;
6570 }
6571
6572 if (bfd_link_relocatable (info))
0a1b45a2 6573 return true;
b32547cd
AM
6574
6575 if (htab->elf.hgot != NULL)
dba6fa9b 6576 {
0a1b45a2 6577 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
dba6fa9b
AM
6578 /* Make .TOC. defined so as to prevent it being made dynamic.
6579 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6580 if (!htab->elf.hgot->def_regular
6581 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6582 {
6583 htab->elf.hgot->root.type = bfd_link_hash_defined;
6584 htab->elf.hgot->root.u.def.value = 0;
6585 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6586 htab->elf.hgot->def_regular = 1;
6587 htab->elf.hgot->root.linker_def = 1;
6588 }
dba6fa9b 6589 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6590 htab->elf.hgot->other
6591 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6592 }
c66bb0ee 6593
0a1b45a2 6594 return true;
e86ce104
AM
6595}
6596
d311bc8b 6597/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6598 aliases, that apply to read-only sections. Cannot be used after
6599 size_dynamic_sections. */
d311bc8b 6600
0a1b45a2 6601static bool
d311bc8b
AM
6602alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6603{
ed7007c1 6604 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6605 do
6606 {
5dbc8b37 6607 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
0a1b45a2 6608 return true;
ed7007c1 6609 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6610 }
6611 while (eh != NULL && &eh->elf != h);
d311bc8b 6612
0a1b45a2 6613 return false;
d311bc8b 6614}
8a2058b5 6615
8a9e8e72
AM
6616/* Return whether EH has pc-relative dynamic relocs. */
6617
0a1b45a2 6618static bool
8a9e8e72
AM
6619pc_dynrelocs (struct ppc_link_hash_entry *eh)
6620{
1657026c 6621 struct ppc_dyn_relocs *p;
8a9e8e72 6622
1657026c 6623 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72 6624 if (p->pc_count != 0)
0a1b45a2
AM
6625 return true;
6626 return false;
8a9e8e72
AM
6627}
6628
8a2058b5
AM
6629/* Return true if a global entry stub will be created for H. Valid
6630 for ELFv2 before plt entries have been allocated. */
6631
0a1b45a2 6632static bool
8a2058b5
AM
6633global_entry_stub (struct elf_link_hash_entry *h)
6634{
6635 struct plt_entry *pent;
6636
6637 if (!h->pointer_equality_needed
6638 || h->def_regular)
0a1b45a2 6639 return false;
8a2058b5
AM
6640
6641 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6642 if (pent->plt.refcount > 0
6643 && pent->addend == 0)
0a1b45a2 6644 return true;
8a2058b5 6645
0a1b45a2 6646 return false;
8a2058b5
AM
6647}
6648
e86ce104
AM
6649/* Adjust a symbol defined by a dynamic object and referenced by a
6650 regular object. The current definition is in some section of the
6651 dynamic object, but we're not including those sections. We have to
6652 change the definition to something the rest of the link can
6653 understand. */
6654
0a1b45a2 6655static bool
4ce794b7
AM
6656ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6657 struct elf_link_hash_entry *h)
e86ce104
AM
6658{
6659 struct ppc_link_hash_table *htab;
5474d94f 6660 asection *s, *srel;
e86ce104
AM
6661
6662 htab = ppc_hash_table (info);
4dfe6ac6 6663 if (htab == NULL)
0a1b45a2 6664 return false;
e86ce104
AM
6665
6666 /* Deal with function syms. */
6667 if (h->type == STT_FUNC
e054468f 6668 || h->type == STT_GNU_IFUNC
f5385ebf 6669 || h->needs_plt)
e86ce104 6670 {
0a1b45a2
AM
6671 bool local = (ppc_elf_hash_entry (h)->save_res
6672 || SYMBOL_CALLS_LOCAL (info, h)
6673 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
529fe20e
AM
6674 /* Discard dyn_relocs when non-pic if we've decided that a
6675 function symbol is local and not an ifunc. We keep dynamic
6676 relocs for ifuncs when local rather than always emitting a
6677 plt call stub for them and defining the symbol on the call
6678 stub. We can't do that for ELFv1 anyway (a function symbol
6679 is defined on a descriptor, not code) and it can be faster at
6680 run-time due to not needing to bounce through a stub. The
6681 dyn_relocs for ifuncs will be applied even in a static
6682 executable. */
6683 if (!bfd_link_pic (info)
6684 && h->type != STT_GNU_IFUNC
6685 && local)
190eb1dd 6686 h->dyn_relocs = NULL;
529fe20e 6687
e86ce104
AM
6688 /* Clear procedure linkage table information for any symbol that
6689 won't need a .plt entry. */
411e1bfb
AM
6690 struct plt_entry *ent;
6691 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6692 if (ent->plt.refcount > 0)
6693 break;
8387904d 6694 if (ent == NULL
2d7ad24e
AM
6695 || (h->type != STT_GNU_IFUNC
6696 && local
3e04d765 6697 && (htab->can_convert_all_inline_plt
ed7007c1 6698 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6699 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6700 {
411e1bfb 6701 h->plt.plist = NULL;
f5385ebf 6702 h->needs_plt = 0;
d1eca1e4 6703 h->pointer_equality_needed = 0;
40b8271b 6704 }
8a2058b5 6705 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6706 {
d1eca1e4
AM
6707 /* Taking a function's address in a read/write section
6708 doesn't require us to define the function symbol in the
6709 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6710 be used instead. The reason we prefer a few more dynamic
6711 relocs is that calling via a global entry stub costs a
6712 few more instructions, and pointer_equality_needed causes
6713 extra work in ld.so when resolving these symbols. */
529fe20e 6714 if (global_entry_stub (h))
d1eca1e4 6715 {
5dbc8b37 6716 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6717 {
6718 h->pointer_equality_needed = 0;
04383fd1
AM
6719 /* If we haven't seen a branch reloc and the symbol
6720 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6721 if (!h->needs_plt)
6722 h->plt.plist = NULL;
6723 }
6724 else if (!bfd_link_pic (info))
6725 /* We are going to be defining the function symbol on the
6726 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6727 h->dyn_relocs = NULL;
d1eca1e4
AM
6728 }
6729
3988aed5 6730 /* ELFv2 function symbols can't have copy relocs. */
0a1b45a2 6731 return true;
3988aed5
AM
6732 }
6733 else if (!h->needs_plt
5dbc8b37 6734 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6735 {
04383fd1
AM
6736 /* If we haven't seen a branch reloc and the symbol isn't an
6737 ifunc then we don't need a plt entry. */
3988aed5
AM
6738 h->plt.plist = NULL;
6739 h->pointer_equality_needed = 0;
0a1b45a2 6740 return true;
a345bc8d 6741 }
5bd4f169 6742 }
bbd7ec4a 6743 else
411e1bfb 6744 h->plt.plist = NULL;
5bd4f169
AM
6745
6746 /* If this is a weak symbol, and there is a real definition, the
6747 processor independent code will have arranged for us to see the
6748 real definition first, and we can just use the same value. */
60d67dc8 6749 if (h->is_weakalias)
5bd4f169 6750 {
60d67dc8
AM
6751 struct elf_link_hash_entry *def = weakdef (h);
6752 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6753 h->root.u.def.section = def->root.u.def.section;
6754 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6755 if (def->root.u.def.section == htab->elf.sdynbss
6756 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6757 h->dyn_relocs = NULL;
0a1b45a2 6758 return true;
5bd4f169
AM
6759 }
6760
5bd4f169
AM
6761 /* If we are creating a shared library, we must presume that the
6762 only references to the symbol are via the global offset table.
6763 For such cases we need not do anything here; the relocations will
6764 be handled correctly by relocate_section. */
ec73ddcd 6765 if (!bfd_link_executable (info))
0a1b45a2 6766 return true;
5bd4f169 6767
65f38f15
AM
6768 /* If there are no references to this symbol that do not use the
6769 GOT, we don't need to generate a copy reloc. */
f5385ebf 6770 if (!h->non_got_ref)
0a1b45a2 6771 return true;
65f38f15 6772
b186458a 6773 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6774 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6775
d93d1c80
AM
6776 /* If -z nocopyreloc was given, don't generate them either. */
6777 || info->nocopyreloc
a127494f 6778
dce2246a 6779 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6780 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6781 || (ELIMINATE_COPY_RELOCS
6782 && !h->needs_copy
6783 && !alias_readonly_dynrelocs (h))
65f38f15 6784
d93d1c80
AM
6785 /* Protected variables do not work with .dynbss. The copy in
6786 .dynbss won't be used by the shared library with the protected
6787 definition for the variable. Text relocations are preferable
6788 to an incorrect program. */
6789 || h->protected_def)
0a1b45a2 6790 return true;
a127494f 6791
e1c6cf61
AM
6792 if (h->type == STT_FUNC
6793 || h->type == STT_GNU_IFUNC)
97b639ba 6794 {
e1c6cf61
AM
6795 /* .dynbss copies of function symbols only work if we have
6796 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6797 use dot-symbols and set the function symbol size to the text
6798 size of the function rather than the size of the descriptor.
6799 That's wrong for copying a descriptor. */
ed7007c1 6800 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61 6801 || !(h->size == 24 || h->size == 16))
0a1b45a2 6802 return true;
e1c6cf61
AM
6803
6804 /* We should never get here, but unfortunately there are old
6805 versions of gcc (circa gcc-3.2) that improperly for the
6806 ELFv1 ABI put initialized function pointers, vtable refs and
6807 suchlike in read-only sections. Allow them to proceed, but
6808 warn that this might break at runtime. */
25f53a85 6809 info->callbacks->einfo
c1c8c1ef 6810 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6811 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6812 h->root.root.string);
6813 }
5d35169e
AM
6814
6815 /* This is a reference to a symbol defined by a dynamic object which
6816 is not a function. */
6817
5bd4f169
AM
6818 /* We must allocate the symbol in our .dynbss section, which will
6819 become part of the .bss section of the executable. There will be
6820 an entry for this symbol in the .dynsym section. The dynamic
6821 object will contain position independent code, so all references
6822 from the dynamic object to this symbol will go through the global
6823 offset table. The dynamic linker will use the .dynsym entry to
6824 determine the address it must put in the global offset table, so
6825 both the dynamic object and the regular object will refer to the
6826 same memory location for the variable. */
5474d94f
AM
6827 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6828 {
6829 s = htab->elf.sdynrelro;
6830 srel = htab->elf.sreldynrelro;
6831 }
6832 else
6833 {
6834 s = htab->elf.sdynbss;
6835 srel = htab->elf.srelbss;
6836 }
1d7e9d18 6837 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6838 {
4a7e5234
AM
6839 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6840 linker to copy the initial value out of the dynamic object
6841 and into the runtime process image. */
5474d94f 6842 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6843 h->needs_copy = 1;
5bd4f169
AM
6844 }
6845
529fe20e 6846 /* We no longer want dyn_relocs. */
190eb1dd 6847 h->dyn_relocs = NULL;
6cabe1ea 6848 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6849}
6850
e86ce104
AM
6851/* If given a function descriptor symbol, hide both the function code
6852 sym and the descriptor. */
6853static void
4ce794b7
AM
6854ppc64_elf_hide_symbol (struct bfd_link_info *info,
6855 struct elf_link_hash_entry *h,
0a1b45a2 6856 bool force_local)
e86ce104 6857{
34814b9f 6858 struct ppc_link_hash_entry *eh;
e86ce104
AM
6859 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6860
87469ba2
AM
6861 if (ppc_hash_table (info) == NULL)
6862 return;
6863
ed7007c1 6864 eh = ppc_elf_hash_entry (h);
34814b9f 6865 if (eh->is_func_descriptor)
e86ce104 6866 {
34814b9f 6867 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6868
721956f4 6869 if (fh == NULL)
d1329ca3
AM
6870 {
6871 const char *p, *q;
b8ac2841 6872 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6873 char save;
6874
6875 /* We aren't supposed to use alloca in BFD because on
6876 systems which do not have alloca the version in libiberty
6877 calls xmalloc, which might cause the program to crash
6878 when it runs out of memory. This function doesn't have a
6879 return status, so there's no way to gracefully return an
6880 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6881 accessed; It's either a string in an ELF string table,
6882 or allocated in an objalloc structure. */
d1329ca3 6883
34814b9f 6884 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6885 save = *p;
6886 *(char *) p = '.';
0a1b45a2
AM
6887 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6888 false, false));
d1329ca3
AM
6889 *(char *) p = save;
6890
6891 /* Unfortunately, if it so happens that the string we were
6892 looking for was allocated immediately before this string,
6893 then we overwrote the string terminator. That's the only
6894 reason the lookup should fail. */
6895 if (fh == NULL)
6896 {
34814b9f
AM
6897 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6898 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6899 --q, --p;
34814b9f 6900 if (q < eh->elf.root.root.string && *p == '.')
0a1b45a2
AM
6901 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6902 false, false));
d1329ca3
AM
6903 }
6904 if (fh != NULL)
6905 {
34814b9f
AM
6906 eh->oh = fh;
6907 fh->oh = eh;
d1329ca3
AM
6908 }
6909 }
e86ce104 6910 if (fh != NULL)
34814b9f 6911 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6912 }
6913}
6914
0a1b45a2 6915static bool
8843416a
AM
6916get_sym_h (struct elf_link_hash_entry **hp,
6917 Elf_Internal_Sym **symp,
6918 asection **symsecp,
f961d9dd 6919 unsigned char **tls_maskp,
8843416a
AM
6920 Elf_Internal_Sym **locsymsp,
6921 unsigned long r_symndx,
6922 bfd *ibfd)
411e1bfb 6923{
0ffa91dd 6924 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6925
6926 if (r_symndx >= symtab_hdr->sh_info)
6927 {
6928 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6929 struct elf_link_hash_entry *h;
6930
6931 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6932 h = elf_follow_link (h);
411e1bfb
AM
6933
6934 if (hp != NULL)
6935 *hp = h;
6936
6937 if (symp != NULL)
6938 *symp = NULL;
6939
6940 if (symsecp != NULL)
6941 {
6942 asection *symsec = NULL;
6943 if (h->root.type == bfd_link_hash_defined
6944 || h->root.type == bfd_link_hash_defweak)
6945 symsec = h->root.u.def.section;
6946 *symsecp = symsec;
6947 }
6948
e7b938ca 6949 if (tls_maskp != NULL)
ed7007c1 6950 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6951 }
6952 else
6953 {
6954 Elf_Internal_Sym *sym;
6955 Elf_Internal_Sym *locsyms = *locsymsp;
6956
6957 if (locsyms == NULL)
6958 {
6959 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6960 if (locsyms == NULL)
6961 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6962 symtab_hdr->sh_info,
6963 0, NULL, NULL, NULL);
6964 if (locsyms == NULL)
0a1b45a2 6965 return false;
411e1bfb
AM
6966 *locsymsp = locsyms;
6967 }
6968 sym = locsyms + r_symndx;
6969
6970 if (hp != NULL)
6971 *hp = NULL;
6972
6973 if (symp != NULL)
6974 *symp = sym;
6975
6976 if (symsecp != NULL)
cb33740c 6977 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6978
e7b938ca 6979 if (tls_maskp != NULL)
411e1bfb
AM
6980 {
6981 struct got_entry **lgot_ents;
f961d9dd 6982 unsigned char *tls_mask;
411e1bfb 6983
e7b938ca 6984 tls_mask = NULL;
411e1bfb
AM
6985 lgot_ents = elf_local_got_ents (ibfd);
6986 if (lgot_ents != NULL)
6987 {
e054468f
AM
6988 struct plt_entry **local_plt = (struct plt_entry **)
6989 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6990 unsigned char *lgot_masks = (unsigned char *)
e054468f 6991 (local_plt + symtab_hdr->sh_info);
e7b938ca 6992 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6993 }
e7b938ca 6994 *tls_maskp = tls_mask;
411e1bfb
AM
6995 }
6996 }
0a1b45a2 6997 return true;
411e1bfb
AM
6998}
6999
e7b938ca 7000/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7001 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7002 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7003
7004static int
f961d9dd 7005get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7006 unsigned long *toc_symndx,
7007 bfd_vma *toc_addend,
0d4792f7 7008 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7009 const Elf_Internal_Rela *rel,
7010 bfd *ibfd)
411e1bfb
AM
7011{
7012 unsigned long r_symndx;
0d4792f7 7013 int next_r;
411e1bfb
AM
7014 struct elf_link_hash_entry *h;
7015 Elf_Internal_Sym *sym;
7016 asection *sec;
7017 bfd_vma off;
7018
7019 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7020 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7021 return 0;
411e1bfb 7022
37da22e5
AM
7023 if ((*tls_maskp != NULL
7024 && (**tls_maskp & TLS_TLS) != 0
7025 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7026 || sec == NULL
6bee8834 7027 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7028 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7029 return 1;
411e1bfb
AM
7030
7031 /* Look inside a TOC section too. */
7032 if (h != NULL)
7033 {
7034 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7035 off = h->root.u.def.value;
7036 }
7037 else
7038 off = sym->st_value;
7039 off += rel->r_addend;
7040 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7041 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7042 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7043 if (toc_symndx != NULL)
7044 *toc_symndx = r_symndx;
3a71aa26
AM
7045 if (toc_addend != NULL)
7046 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7047 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7048 return 0;
854b41e7 7049 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7050 && (next_r == -1 || next_r == -2))
7051 return 1 - next_r;
951fd09b 7052 return 1;
411e1bfb
AM
7053}
7054
3b421ab3
AM
7055/* Find (or create) an entry in the tocsave hash table. */
7056
7057static struct tocsave_entry *
7058tocsave_find (struct ppc_link_hash_table *htab,
7059 enum insert_option insert,
7060 Elf_Internal_Sym **local_syms,
7061 const Elf_Internal_Rela *irela,
7062 bfd *ibfd)
7063{
7064 unsigned long r_indx;
7065 struct elf_link_hash_entry *h;
7066 Elf_Internal_Sym *sym;
7067 struct tocsave_entry ent, *p;
7068 hashval_t hash;
7069 struct tocsave_entry **slot;
7070
7071 r_indx = ELF64_R_SYM (irela->r_info);
7072 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7073 return NULL;
7074 if (ent.sec == NULL || ent.sec->output_section == NULL)
7075 {
4eca0228 7076 _bfd_error_handler
871b3ab2 7077 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7078 return NULL;
7079 }
7080
7081 if (h != NULL)
7082 ent.offset = h->root.u.def.value;
7083 else
7084 ent.offset = sym->st_value;
7085 ent.offset += irela->r_addend;
7086
7087 hash = tocsave_htab_hash (&ent);
7088 slot = ((struct tocsave_entry **)
7089 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7090 if (slot == NULL)
7091 return NULL;
7092
7093 if (*slot == NULL)
7094 {
7095 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7096 if (p == NULL)
7097 return NULL;
7098 *p = ent;
7099 *slot = p;
7100 }
7101 return *slot;
7102}
7103
754021d0 7104/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7105 code for the old ABI, these will already have been done. */
754021d0 7106
0a1b45a2 7107static bool
754021d0
AM
7108adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7109{
7110 struct ppc_link_hash_entry *eh;
7111 asection *sym_sec;
74f0fb50 7112 struct _opd_sec_data *opd;
754021d0
AM
7113
7114 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 7115 return true;
754021d0 7116
754021d0
AM
7117 if (h->root.type != bfd_link_hash_defined
7118 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 7119 return true;
754021d0 7120
ed7007c1 7121 eh = ppc_elf_hash_entry (h);
754021d0 7122 if (eh->adjust_done)
0a1b45a2 7123 return true;
754021d0
AM
7124
7125 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7126 opd = get_opd_info (sym_sec);
7127 if (opd != NULL && opd->adjust != NULL)
754021d0 7128 {
51aecdc5 7129 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7130 if (adjust == -1)
7131 {
7132 /* This entry has been deleted. */
b3fac117 7133 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7134 if (dsec == NULL)
7135 {
7136 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7137 if (discarded_section (dsec))
81688140 7138 {
b3fac117 7139 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7140 break;
7141 }
7142 }
4025353c 7143 eh->elf.root.u.def.value = 0;
81688140 7144 eh->elf.root.u.def.section = dsec;
4025353c
AM
7145 }
7146 else
7147 eh->elf.root.u.def.value += adjust;
754021d0
AM
7148 eh->adjust_done = 1;
7149 }
0a1b45a2 7150 return true;
754021d0
AM
7151}
7152
8c1d1bb8 7153/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7154 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7155 have already been determined. */
7156
0a1b45a2 7157static bool
1657026c 7158dec_dynrel_count (const Elf_Internal_Rela *rel,
8c1d1bb8
AM
7159 asection *sec,
7160 struct bfd_link_info *info,
7161 Elf_Internal_Sym **local_syms,
7162 struct elf_link_hash_entry *h,
19e08130 7163 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7164{
7165 enum elf_ppc64_reloc_type r_type;
19e08130 7166 asection *sym_sec = NULL;
8c1d1bb8
AM
7167
7168 /* Can this reloc be dynamic? This switch, and later tests here
7169 should be kept in sync with the code in check_relocs. */
1657026c 7170 r_type = ELF64_R_TYPE (rel->r_info);
8c1d1bb8
AM
7171 switch (r_type)
7172 {
7173 default:
0a1b45a2 7174 return true;
8c1d1bb8 7175
1bdd8fac
AM
7176 case R_PPC64_TOC16:
7177 case R_PPC64_TOC16_DS:
7178 case R_PPC64_TOC16_LO:
7179 case R_PPC64_TOC16_HI:
7180 case R_PPC64_TOC16_HA:
7181 case R_PPC64_TOC16_LO_DS:
7182 if (h == NULL)
0a1b45a2 7183 return true;
1bdd8fac
AM
7184 break;
7185
8c1d1bb8
AM
7186 case R_PPC64_TPREL16:
7187 case R_PPC64_TPREL16_LO:
7188 case R_PPC64_TPREL16_HI:
7189 case R_PPC64_TPREL16_HA:
7190 case R_PPC64_TPREL16_DS:
7191 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7192 case R_PPC64_TPREL16_HIGH:
7193 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7194 case R_PPC64_TPREL16_HIGHER:
7195 case R_PPC64_TPREL16_HIGHERA:
7196 case R_PPC64_TPREL16_HIGHEST:
7197 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7198 case R_PPC64_TPREL64:
c213164a 7199 case R_PPC64_TPREL34:
8c1d1bb8
AM
7200 case R_PPC64_DTPMOD64:
7201 case R_PPC64_DTPREL64:
7202 case R_PPC64_ADDR64:
7203 case R_PPC64_REL30:
7204 case R_PPC64_REL32:
7205 case R_PPC64_REL64:
7206 case R_PPC64_ADDR14:
7207 case R_PPC64_ADDR14_BRNTAKEN:
7208 case R_PPC64_ADDR14_BRTAKEN:
7209 case R_PPC64_ADDR16:
7210 case R_PPC64_ADDR16_DS:
7211 case R_PPC64_ADDR16_HA:
7212 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7213 case R_PPC64_ADDR16_HIGH:
7214 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7215 case R_PPC64_ADDR16_HIGHER:
7216 case R_PPC64_ADDR16_HIGHERA:
7217 case R_PPC64_ADDR16_HIGHEST:
7218 case R_PPC64_ADDR16_HIGHESTA:
7219 case R_PPC64_ADDR16_LO:
7220 case R_PPC64_ADDR16_LO_DS:
7221 case R_PPC64_ADDR24:
7222 case R_PPC64_ADDR32:
7223 case R_PPC64_UADDR16:
7224 case R_PPC64_UADDR32:
7225 case R_PPC64_UADDR64:
7226 case R_PPC64_TOC:
5663e321
AM
7227 case R_PPC64_D34:
7228 case R_PPC64_D34_LO:
7229 case R_PPC64_D34_HI30:
7230 case R_PPC64_D34_HA30:
7231 case R_PPC64_ADDR16_HIGHER34:
7232 case R_PPC64_ADDR16_HIGHERA34:
7233 case R_PPC64_ADDR16_HIGHEST34:
7234 case R_PPC64_ADDR16_HIGHESTA34:
7235 case R_PPC64_D28:
8c1d1bb8
AM
7236 break;
7237 }
7238
7239 if (local_syms != NULL)
7240 {
7241 unsigned long r_symndx;
8c1d1bb8
AM
7242 bfd *ibfd = sec->owner;
7243
1657026c 7244 r_symndx = ELF64_R_SYM (rel->r_info);
8c1d1bb8 7245 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
0a1b45a2 7246 return false;
8c1d1bb8
AM
7247 }
7248
ec73ddcd
AM
7249 if ((h != NULL
7250 && (h->root.type == bfd_link_hash_defweak
7251 || !h->def_regular))
7252 || (h != NULL
7253 && !bfd_link_executable (info)
7254 && !SYMBOLIC_BIND (info, h))
7255 || (bfd_link_pic (info)
7256 && must_be_dyn_reloc (info, r_type))
7257 || (!bfd_link_pic (info)
7258 && (h != NULL
7259 ? h->type == STT_GNU_IFUNC
7260 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7261 ;
7262 else
0a1b45a2 7263 return true;
8c1d1bb8
AM
7264
7265 if (h != NULL)
6edfbbad 7266 {
1657026c
AM
7267 struct ppc_dyn_relocs *p;
7268 struct ppc_dyn_relocs **pp;
7269 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
7270
7271 /* elf_gc_sweep may have already removed all dyn relocs associated
7272 with local syms for a given section. Also, symbol flags are
7273 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7274 report a dynreloc miscount. */
7275 if (*pp == NULL && info->gc_sections)
0a1b45a2 7276 return true;
19e08130
AM
7277
7278 while ((p = *pp) != NULL)
60124e18 7279 {
19e08130
AM
7280 if (p->sec == sec)
7281 {
7282 if (!must_be_dyn_reloc (info, r_type))
7283 p->pc_count -= 1;
1657026c
AM
7284 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7285 && rel->r_offset % 2 == 0
7286 && sec->alignment_power != 0
7287 && ((!NO_OPD_RELOCS
7288 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
858b0eff 7289 || h->type != STT_GNU_IFUNC))
1657026c 7290 p->rel_count -= 1;
19e08130
AM
7291 p->count -= 1;
7292 if (p->count == 0)
7293 *pp = p->next;
0a1b45a2 7294 return true;
19e08130
AM
7295 }
7296 pp = &p->next;
60124e18 7297 }
6edfbbad 7298 }
19e08130
AM
7299 else
7300 {
1657026c
AM
7301 struct ppc_local_dyn_relocs *p;
7302 struct ppc_local_dyn_relocs **pp;
19e08130 7303 void *vpp;
0a1b45a2 7304 bool is_ifunc;
8c1d1bb8 7305
19e08130
AM
7306 if (local_syms == NULL)
7307 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7308 if (sym_sec == NULL)
7309 sym_sec = sec;
c57da1a7 7310
19e08130 7311 vpp = &elf_section_data (sym_sec)->local_dynrel;
1657026c 7312 pp = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
7313
7314 if (*pp == NULL && info->gc_sections)
0a1b45a2 7315 return true;
19e08130
AM
7316
7317 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7318 while ((p = *pp) != NULL)
8c1d1bb8 7319 {
19e08130
AM
7320 if (p->sec == sec && p->ifunc == is_ifunc)
7321 {
1657026c
AM
7322 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7323 && rel->r_offset % 2 == 0
7324 && sec->alignment_power != 0
7325 && ((!NO_OPD_RELOCS
7326 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
7327 || !is_ifunc))
7328 p->rel_count -= 1;
19e08130
AM
7329 p->count -= 1;
7330 if (p->count == 0)
7331 *pp = p->next;
0a1b45a2 7332 return true;
19e08130
AM
7333 }
7334 pp = &p->next;
8c1d1bb8 7335 }
8c1d1bb8
AM
7336 }
7337
695344c0 7338 /* xgettext:c-format */
cf97bcb0
AM
7339 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7340 sec->owner, sec);
8c1d1bb8 7341 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7342 return false;
8c1d1bb8
AM
7343}
7344
754021d0
AM
7345/* Remove unused Official Procedure Descriptor entries. Currently we
7346 only remove those associated with functions in discarded link-once
7347 sections, or weakly defined functions that have been overridden. It
7348 would be possible to remove many more entries for statically linked
7349 applications. */
7350
0a1b45a2 7351bool
e7d1c40c 7352ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7353{
7354 bfd *ibfd;
0a1b45a2 7355 bool some_edited = false;
3f764659 7356 asection *need_pad = NULL;
e7d1c40c
AM
7357 struct ppc_link_hash_table *htab;
7358
7359 htab = ppc_hash_table (info);
7360 if (htab == NULL)
0a1b45a2 7361 return false;
1e2f5b6e 7362
c72f2fb2 7363 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7364 {
7365 asection *sec;
7366 Elf_Internal_Rela *relstart, *rel, *relend;
7367 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7368 Elf_Internal_Sym *local_syms;
74f0fb50 7369 struct _opd_sec_data *opd;
0a1b45a2 7370 bool need_edit, add_aux_fields, broken;
3f764659 7371 bfd_size_type cnt_16b = 0;
1e2f5b6e 7372
854b41e7
AM
7373 if (!is_ppc64_elf (ibfd))
7374 continue;
7375
1e2f5b6e 7376 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7377 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7378 continue;
7379
dbaa2011 7380 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7381 continue;
7382
1e2f5b6e
AM
7383 if (sec->output_section == bfd_abs_section_ptr)
7384 continue;
7385
7386 /* Look through the section relocs. */
7387 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7388 continue;
7389
6cdc0ccc 7390 local_syms = NULL;
0ffa91dd 7391 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7392
7393 /* Read the relocations. */
4ce794b7 7394 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7395 info->keep_memory);
1e2f5b6e 7396 if (relstart == NULL)
0a1b45a2 7397 return false;
1e2f5b6e
AM
7398
7399 /* First run through the relocs to check they are sane, and to
7400 determine whether we need to edit this opd section. */
0a1b45a2
AM
7401 need_edit = false;
7402 broken = false;
3f764659 7403 need_pad = sec;
1e2f5b6e 7404 relend = relstart + sec->reloc_count;
50bc7936 7405 for (rel = relstart; rel < relend; )
1e2f5b6e 7406 {
04c9666a 7407 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7408 unsigned long r_symndx;
7409 asection *sym_sec;
7410 struct elf_link_hash_entry *h;
7411 Elf_Internal_Sym *sym;
51aecdc5 7412 bfd_vma offset;
1e2f5b6e 7413
51aecdc5 7414 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7415 only interested in the reloc pointing to a function entry
7416 point. */
51aecdc5
AM
7417 offset = rel->r_offset;
7418 if (rel + 1 == relend
7419 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7420 {
7421 /* If someone messes with .opd alignment then after a
7422 "ld -r" we might have padding in the middle of .opd.
7423 Also, there's nothing to prevent someone putting
7424 something silly in .opd with the assembler. No .opd
b34976b6 7425 optimization for them! */
3f764659 7426 broken_opd:
4eca0228 7427 _bfd_error_handler
871b3ab2 7428 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
0a1b45a2 7429 broken = true;
1e2f5b6e
AM
7430 break;
7431 }
7432
50bc7936
AM
7433 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7434 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7435 {
4eca0228 7436 _bfd_error_handler
695344c0 7437 /* xgettext:c-format */
871b3ab2 7438 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7439 ibfd, r_type);
0a1b45a2 7440 broken = true;
50bc7936
AM
7441 break;
7442 }
7443
1e2f5b6e 7444 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7445 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7446 r_symndx, ibfd))
50bc7936 7447 goto error_ret;
1e2f5b6e
AM
7448
7449 if (sym_sec == NULL || sym_sec->owner == NULL)
7450 {
411e1bfb
AM
7451 const char *sym_name;
7452 if (h != NULL)
7453 sym_name = h->root.root.string;
7454 else
26c61ae5
L
7455 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7456 sym_sec);
411e1bfb 7457
4eca0228 7458 _bfd_error_handler
695344c0 7459 /* xgettext:c-format */
871b3ab2 7460 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7461 ibfd, sym_name);
0a1b45a2 7462 broken = true;
1e2f5b6e
AM
7463 break;
7464 }
7465
51020317
AM
7466 /* opd entries are always for functions defined in the
7467 current input bfd. If the symbol isn't defined in the
7468 input bfd, then we won't be using the function in this
7469 bfd; It must be defined in a linkonce section in another
7470 bfd, or is weak. It's also possible that we are
7471 discarding the function due to a linker script /DISCARD/,
7472 which we test for via the output_section. */
7473 if (sym_sec->owner != ibfd
7474 || sym_sec->output_section == bfd_abs_section_ptr)
0a1b45a2 7475 need_edit = true;
1e2f5b6e 7476
50bc7936 7477 rel += 2;
51aecdc5
AM
7478 if (rel + 1 == relend
7479 || (rel + 2 < relend
7480 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7481 ++rel;
7482
7483 if (rel == relend)
3f764659
JJ
7484 {
7485 if (sec->size == offset + 24)
7486 {
7487 need_pad = NULL;
7488 break;
7489 }
51aecdc5 7490 if (sec->size == offset + 16)
3f764659
JJ
7491 {
7492 cnt_16b++;
7493 break;
7494 }
7495 goto broken_opd;
7496 }
3f764659
JJ
7497 else if (rel + 1 < relend
7498 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7499 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7500 {
51aecdc5
AM
7501 if (rel[0].r_offset == offset + 16)
7502 cnt_16b++;
7503 else if (rel[0].r_offset != offset + 24)
7504 goto broken_opd;
3f764659
JJ
7505 }
7506 else
7507 goto broken_opd;
1e2f5b6e
AM
7508 }
7509
e7d1c40c 7510 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7511
51aecdc5 7512 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7513 {
7514 Elf_Internal_Rela *write_rel;
d4730f92 7515 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7516 bfd_byte *rptr, *wptr;
983bddc8 7517 bfd_byte *new_contents;
74f0fb50
AM
7518 bfd_size_type amt;
7519
983bddc8 7520 new_contents = NULL;
51aecdc5 7521 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7522 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7523 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50 7524 if (opd->adjust == NULL)
0a1b45a2 7525 return false;
1e2f5b6e
AM
7526
7527 /* This seems a waste of time as input .opd sections are all
7528 zeros as generated by gcc, but I suppose there's no reason
7529 this will always be so. We might start putting something in
7530 the third word of .opd entries. */
7531 if ((sec->flags & SEC_IN_MEMORY) == 0)
7532 {
eea6121a
AM
7533 bfd_byte *loc;
7534 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7535 {
c9594989 7536 free (loc);
50bc7936 7537 error_ret:
c9594989 7538 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7539 free (local_syms);
6cdc0ccc
AM
7540 if (elf_section_data (sec)->relocs != relstart)
7541 free (relstart);
0a1b45a2 7542 return false;
6cdc0ccc 7543 }
1e2f5b6e
AM
7544 sec->contents = loc;
7545 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7546 }
7547
7548 elf_section_data (sec)->relocs = relstart;
7549
3f764659 7550 new_contents = sec->contents;
3f764659
JJ
7551 if (add_aux_fields)
7552 {
7553 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7554 if (new_contents == NULL)
0a1b45a2 7555 return false;
51aecdc5 7556 need_pad = NULL;
3f764659 7557 }
b4f4e59f
AM
7558 wptr = new_contents;
7559 rptr = sec->contents;
1e2f5b6e 7560 write_rel = relstart;
51aecdc5 7561 for (rel = relstart; rel < relend; )
1e2f5b6e 7562 {
50bc7936
AM
7563 unsigned long r_symndx;
7564 asection *sym_sec;
7565 struct elf_link_hash_entry *h;
51aecdc5 7566 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7567 Elf_Internal_Sym *sym;
51aecdc5
AM
7568 long opd_ent_size;
7569 Elf_Internal_Rela *next_rel;
0a1b45a2 7570 bool skip;
50bc7936
AM
7571
7572 r_symndx = ELF64_R_SYM (rel->r_info);
7573 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7574 r_symndx, ibfd))
50bc7936
AM
7575 goto error_ret;
7576
51aecdc5
AM
7577 next_rel = rel + 2;
7578 if (next_rel + 1 == relend
7579 || (next_rel + 2 < relend
7580 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7581 ++next_rel;
7582
7583 /* See if the .opd entry is full 24 byte or
7584 16 byte (with fd_aux entry overlapped with next
7585 fd_func). */
7586 opd_ent_size = 24;
7587 if (next_rel == relend)
1e2f5b6e 7588 {
51aecdc5 7589 if (sec->size == rel->r_offset + 16)
3f764659 7590 opd_ent_size = 16;
51aecdc5
AM
7591 }
7592 else if (next_rel->r_offset == rel->r_offset + 16)
7593 opd_ent_size = 16;
3f764659 7594
51aecdc5
AM
7595 if (h != NULL
7596 && h->root.root.string[0] == '.')
7597 {
ed7007c1 7598 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7599 if (fdh != NULL)
7600 {
7601 fdh = ppc_follow_link (fdh);
7602 if (fdh->elf.root.type != bfd_link_hash_defined
7603 && fdh->elf.root.type != bfd_link_hash_defweak)
7604 fdh = NULL;
7605 }
51aecdc5 7606 }
1e2f5b6e 7607
51aecdc5
AM
7608 skip = (sym_sec->owner != ibfd
7609 || sym_sec->output_section == bfd_abs_section_ptr);
7610 if (skip)
7611 {
7612 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7613 {
51aecdc5
AM
7614 /* Arrange for the function descriptor sym
7615 to be dropped. */
7616 fdh->elf.root.u.def.value = 0;
7617 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7618 }
51aecdc5 7619 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7620
0e1862bb 7621 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7622 rel = next_rel;
7623 else
7624 while (1)
7625 {
1657026c 7626 if (!dec_dynrel_count (rel, sec, info,
51aecdc5
AM
7627 NULL, h, sym))
7628 goto error_ret;
754021d0 7629
51aecdc5
AM
7630 if (++rel == next_rel)
7631 break;
1e2f5b6e 7632
51aecdc5
AM
7633 r_symndx = ELF64_R_SYM (rel->r_info);
7634 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7635 r_symndx, ibfd))
7636 goto error_ret;
7637 }
50bc7936
AM
7638 }
7639 else
1e2f5b6e 7640 {
51aecdc5
AM
7641 /* We'll be keeping this opd entry. */
7642 long adjust;
7643
7644 if (fdh != NULL)
7645 {
7646 /* Redefine the function descriptor symbol to
7647 this location in the opd section. It is
7648 necessary to update the value here rather
7649 than using an array of adjustments as we do
7650 for local symbols, because various places
7651 in the generic ELF code use the value
7652 stored in u.def.value. */
7653 fdh->elf.root.u.def.value = wptr - new_contents;
7654 fdh->adjust_done = 1;
7655 }
7656
7657 /* Local syms are a bit tricky. We could
7658 tweak them as they can be cached, but
7659 we'd need to look through the local syms
7660 for the function descriptor sym which we
7661 don't have at the moment. So keep an
7662 array of adjustments. */
7663 adjust = (wptr - new_contents) - (rptr - sec->contents);
7664 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7665
7666 if (wptr != rptr)
7667 memcpy (wptr, rptr, opd_ent_size);
7668 wptr += opd_ent_size;
7669 if (add_aux_fields && opd_ent_size == 16)
7670 {
7671 memset (wptr, '\0', 8);
7672 wptr += 8;
7673 }
7674
50bc7936 7675 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7676 new opd entries. */
7677 for ( ; rel != next_rel; ++rel)
7678 {
7679 rel->r_offset += adjust;
7680 if (write_rel != rel)
7681 memcpy (write_rel, rel, sizeof (*rel));
7682 ++write_rel;
7683 }
1e2f5b6e 7684 }
51aecdc5
AM
7685
7686 rptr += opd_ent_size;
1e2f5b6e
AM
7687 }
7688
3f764659 7689 sec->size = wptr - new_contents;
1e2f5b6e 7690 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7691 if (add_aux_fields)
7692 {
7693 free (sec->contents);
7694 sec->contents = new_contents;
7695 }
7696
05bf9422 7697 /* Fudge the header size too, as this is used later in
cdcf6e38 7698 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7699 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7700 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
0a1b45a2 7701 some_edited = true;
1e2f5b6e 7702 }
6cdc0ccc 7703 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7704 free (relstart);
6cdc0ccc 7705
411e1bfb
AM
7706 if (local_syms != NULL
7707 && symtab_hdr->contents != (unsigned char *) local_syms)
7708 {
7709 if (!info->keep_memory)
7710 free (local_syms);
7711 else
7712 symtab_hdr->contents = (unsigned char *) local_syms;
7713 }
7714 }
7715
754021d0
AM
7716 if (some_edited)
7717 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7718
3f764659
JJ
7719 /* If we are doing a final link and the last .opd entry is just 16 byte
7720 long, add a 8 byte padding after it. */
0e1862bb 7721 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7722 {
7723 bfd_byte *p;
7724
7725 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7726 {
7727 BFD_ASSERT (need_pad->size > 0);
7728
7729 p = bfd_malloc (need_pad->size + 8);
7730 if (p == NULL)
0a1b45a2 7731 return false;
699733f6 7732
2cdcc330
AM
7733 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7734 p, 0, need_pad->size))
0a1b45a2 7735 return false;
3f764659
JJ
7736
7737 need_pad->contents = p;
7738 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7739 }
7740 else
7741 {
7742 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7743 if (p == NULL)
0a1b45a2 7744 return false;
3f764659
JJ
7745
7746 need_pad->contents = p;
7747 }
7748
7749 memset (need_pad->contents + need_pad->size, 0, 8);
7750 need_pad->size += 8;
7751 }
7752
0a1b45a2 7753 return true;
411e1bfb
AM
7754}
7755
3e04d765
AM
7756/* Analyze inline PLT call relocations to see whether calls to locally
7757 defined functions can be converted to direct calls. */
7758
0a1b45a2 7759bool
3e04d765
AM
7760ppc64_elf_inline_plt (struct bfd_link_info *info)
7761{
7762 struct ppc_link_hash_table *htab;
7763 bfd *ibfd;
7764 asection *sec;
7765 bfd_vma low_vma, high_vma, limit;
7766
7767 htab = ppc_hash_table (info);
7768 if (htab == NULL)
0a1b45a2 7769 return false;
3e04d765
AM
7770
7771 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7772 reduced somewhat to cater for possible stubs that might be added
7773 between the call and its destination. */
7774 if (htab->params->group_size < 0)
7775 {
7776 limit = -htab->params->group_size;
7777 if (limit == 1)
7778 limit = 0x1e00000;
7779 }
7780 else
7781 {
7782 limit = htab->params->group_size;
7783 if (limit == 1)
7784 limit = 0x1c00000;
7785 }
7786
7787 low_vma = -1;
7788 high_vma = 0;
7789 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7790 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7791 {
7792 if (low_vma > sec->vma)
7793 low_vma = sec->vma;
7794 if (high_vma < sec->vma + sec->size)
7795 high_vma = sec->vma + sec->size;
7796 }
7797
7798 /* If a "bl" can reach anywhere in local code sections, then we can
7799 convert all inline PLT sequences to direct calls when the symbol
7800 is local. */
7801 if (high_vma - low_vma < limit)
7802 {
7803 htab->can_convert_all_inline_plt = 1;
0a1b45a2 7804 return true;
3e04d765
AM
7805 }
7806
7807 /* Otherwise, go looking through relocs for cases where a direct
7808 call won't reach. Mark the symbol on any such reloc to disable
7809 the optimization and keep the PLT entry as it seems likely that
7810 this will be better than creating trampolines. Note that this
7811 will disable the optimization for all inline PLT calls to a
7812 particular symbol, not just those that won't reach. The
7813 difficulty in doing a more precise optimization is that the
7814 linker needs to make a decision depending on whether a
7815 particular R_PPC64_PLTCALL insn can be turned into a direct
7816 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7817 the sequence, and there is nothing that ties those relocs
7818 together except their symbol. */
7819
7820 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7821 {
7822 Elf_Internal_Shdr *symtab_hdr;
7823 Elf_Internal_Sym *local_syms;
7824
7825 if (!is_ppc64_elf (ibfd))
7826 continue;
7827
7828 local_syms = NULL;
7829 symtab_hdr = &elf_symtab_hdr (ibfd);
7830
7831 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7832 if (ppc64_elf_section_data (sec)->has_pltcall
7833 && !bfd_is_abs_section (sec->output_section))
7834 {
7835 Elf_Internal_Rela *relstart, *rel, *relend;
7836
7837 /* Read the relocations. */
7838 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7839 info->keep_memory);
7840 if (relstart == NULL)
0a1b45a2 7841 return false;
3e04d765
AM
7842
7843 relend = relstart + sec->reloc_count;
435edf0b 7844 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7845 {
7846 enum elf_ppc64_reloc_type r_type;
7847 unsigned long r_symndx;
7848 asection *sym_sec;
7849 struct elf_link_hash_entry *h;
7850 Elf_Internal_Sym *sym;
7851 unsigned char *tls_maskp;
7852
7853 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7854 if (r_type != R_PPC64_PLTCALL
7855 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7856 continue;
7857
7858 r_symndx = ELF64_R_SYM (rel->r_info);
7859 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7860 r_symndx, ibfd))
7861 {
7862 if (elf_section_data (sec)->relocs != relstart)
7863 free (relstart);
c9594989 7864 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765 7865 free (local_syms);
0a1b45a2 7866 return false;
3e04d765
AM
7867 }
7868
7869 if (sym_sec != NULL && sym_sec->output_section != NULL)
7870 {
7871 bfd_vma from, to;
7872 if (h != NULL)
7873 to = h->root.u.def.value;
7874 else
7875 to = sym->st_value;
7876 to += (rel->r_addend
7877 + sym_sec->output_offset
7878 + sym_sec->output_section->vma);
7879 from = (rel->r_offset
7880 + sec->output_offset
7881 + sec->output_section->vma);
5663e321
AM
7882 if (to - from + limit < 2 * limit
7883 && !(r_type == R_PPC64_PLTCALL_NOTOC
7884 && (((h ? h->other : sym->st_other)
7885 & STO_PPC64_LOCAL_MASK)
4a4e7361 7886 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7887 *tls_maskp &= ~PLT_KEEP;
7888 }
7889 }
7890 if (elf_section_data (sec)->relocs != relstart)
7891 free (relstart);
7892 }
7893
7894 if (local_syms != NULL
7895 && symtab_hdr->contents != (unsigned char *) local_syms)
7896 {
7897 if (!info->keep_memory)
7898 free (local_syms);
7899 else
7900 symtab_hdr->contents = (unsigned char *) local_syms;
7901 }
7902 }
7903
0a1b45a2 7904 return true;
3e04d765
AM
7905}
7906
c9405344
AM
7907/* Set htab->tls_get_addr and various other info specific to TLS.
7908 This needs to run before dynamic symbols are processed in
7909 bfd_elf_size_dynamic_sections. */
411e1bfb 7910
0a1b45a2 7911bool
e7d1c40c 7912ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7913{
411e1bfb 7914 struct ppc_link_hash_table *htab;
9e7028aa 7915 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7916
411e1bfb 7917 htab = ppc_hash_table (info);
4dfe6ac6 7918 if (htab == NULL)
0a1b45a2 7919 return false;
4dfe6ac6 7920
a442059f
AM
7921 /* Move dynamic linking info to the function descriptor sym. */
7922 if (htab->need_func_desc_adj)
7923 {
7924 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7925 htab->need_func_desc_adj = 0;
7926 }
7927
ee67d69a
AM
7928 if (abiversion (info->output_bfd) == 1)
7929 htab->opd_abi = 1;
7930
e7d1c40c 7931 if (htab->params->no_multi_toc)
33c0ec9d
AM
7932 htab->do_multi_toc = 0;
7933 else if (!htab->do_multi_toc)
e7d1c40c 7934 htab->params->no_multi_toc = 1;
33c0ec9d 7935
8b5f1ed8
AM
7936 /* Default to --no-plt-localentry, as this option can cause problems
7937 with symbol interposition. For example, glibc libpthread.so and
7938 libc.so duplicate many pthread symbols, with a fallback
7939 implementation in libc.so. In some cases the fallback does more
7940 work than the pthread implementation. __pthread_condattr_destroy
7941 is one such symbol: the libpthread.so implementation is
7942 localentry:0 while the libc.so implementation is localentry:8.
7943 An app that "cleverly" uses dlopen to only load necessary
7944 libraries at runtime may omit loading libpthread.so when not
7945 running multi-threaded, which then results in the libc.so
7946 fallback symbols being used and ld.so complaining. Now there
7947 are workarounds in ld (see non_zero_localentry) to detect the
7948 pthread situation, but that may not be the only case where
7949 --plt-localentry can cause trouble. */
f378ab09 7950 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7951 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7952 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7953 {
7954 /* The issue is that __glink_PLTresolve saves r2, which is done
7955 because glibc ld.so _dl_runtime_resolve restores r2 to support
7956 a glibc plt call optimisation where global entry code is
7957 skipped on calls that resolve to the same binary. The
7958 __glink_PLTresolve save of r2 is incompatible with code
7959 making tail calls, because the tail call might go via the
7960 resolver and thus overwrite the proper saved r2. */
7961 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7962 "power10 pc-relative code"));
7963 htab->params->plt_localentry0 = 0;
7964 }
d44c746a
AM
7965 if (htab->params->plt_localentry0
7966 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
0a1b45a2 7967 false, false, false) == NULL)
cf97bcb0
AM
7968 _bfd_error_handler
7969 (_("warning: --plt-localentry is especially dangerous without "
7970 "ld.so support to detect ABI violations"));
f378ab09 7971
9e7028aa 7972 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 7973 false, false, true);
9e7028aa 7974 htab->tls_get_addr = ppc_elf_hash_entry (tga);
9e7028aa 7975 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 7976 false, false, true);
9e7028aa
AM
7977 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7978
7979 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
0a1b45a2 7980 false, false, true);
9e7028aa 7981 htab->tga_desc = ppc_elf_hash_entry (desc);
9e7028aa 7982 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
0a1b45a2 7983 false, false, true);
9e7028aa
AM
7984 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7985
7c9cf415 7986 if (htab->params->tls_get_addr_opt)
a7f2871e 7987 {
9e7028aa 7988 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7989
7990 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
0a1b45a2 7991 false, false, true);
a7f2871e 7992 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
0a1b45a2 7993 false, false, true);
a7f2871e
AM
7994 if (opt_fd != NULL
7995 && (opt_fd->root.type == bfd_link_hash_defined
7996 || opt_fd->root.type == bfd_link_hash_defweak))
7997 {
7998 /* If glibc supports an optimized __tls_get_addr call stub,
7999 signalled by the presence of __tls_get_addr_opt, and we'll
8000 be calling __tls_get_addr via a plt call stub, then
8001 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
8002 if (!(htab->elf.dynamic_sections_created
8003 && tga_fd != NULL
8004 && (tga_fd->type == STT_FUNC
8005 || tga_fd->needs_plt)
8006 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8007 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8008 tga_fd = NULL;
8009 if (!(htab->elf.dynamic_sections_created
8010 && desc_fd != NULL
8011 && (desc_fd->type == STT_FUNC
8012 || desc_fd->needs_plt)
8013 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8014 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8015 desc_fd = NULL;
8016
8017 if (tga_fd != NULL || desc_fd != NULL)
8018 {
8019 struct plt_entry *ent = NULL;
8020
8021 if (tga_fd != NULL)
8022 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8023 if (ent->plt.refcount > 0)
8024 break;
8025 if (ent == NULL && desc_fd != NULL)
8026 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8027 if (ent->plt.refcount > 0)
8028 break;
a7f2871e
AM
8029 if (ent != NULL)
8030 {
9e7028aa
AM
8031 if (tga_fd != NULL)
8032 {
8033 tga_fd->root.type = bfd_link_hash_indirect;
8034 tga_fd->root.u.i.link = &opt_fd->root;
8035 tga_fd->root.u.i.warning = NULL;
8036 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8037 }
8038 if (desc_fd != NULL)
8039 {
8040 desc_fd->root.type = bfd_link_hash_indirect;
8041 desc_fd->root.u.i.link = &opt_fd->root;
8042 desc_fd->root.u.i.warning = NULL;
8043 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8044 }
b531344c 8045 opt_fd->mark = 1;
a7f2871e
AM
8046 if (opt_fd->dynindx != -1)
8047 {
8048 /* Use __tls_get_addr_opt in dynamic relocations. */
8049 opt_fd->dynindx = -1;
8050 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8051 opt_fd->dynstr_index);
8052 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
0a1b45a2 8053 return false;
a7f2871e 8054 }
9e7028aa 8055 if (tga_fd != NULL)
a7f2871e 8056 {
9e7028aa 8057 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 8058 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
8059 if (opt != NULL && tga != NULL)
8060 {
8061 tga->root.type = bfd_link_hash_indirect;
8062 tga->root.u.i.link = &opt->root;
8063 tga->root.u.i.warning = NULL;
8064 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8065 opt->mark = 1;
8066 _bfd_elf_link_hash_hide_symbol (info, opt,
8067 tga->forced_local);
8068 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8069 }
8070 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8071 htab->tls_get_addr_fd->is_func_descriptor = 1;
8072 if (htab->tls_get_addr != NULL)
8073 {
8074 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8075 htab->tls_get_addr->is_func = 1;
8076 }
a7f2871e 8077 }
9e7028aa 8078 if (desc_fd != NULL)
a7f2871e 8079 {
9e7028aa
AM
8080 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8081 if (opt != NULL && desc != NULL)
8082 {
8083 desc->root.type = bfd_link_hash_indirect;
8084 desc->root.u.i.link = &opt->root;
8085 desc->root.u.i.warning = NULL;
8086 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8087 opt->mark = 1;
8088 _bfd_elf_link_hash_hide_symbol (info, opt,
8089 desc->forced_local);
8090 htab->tga_desc = ppc_elf_hash_entry (opt);
8091 }
8092 htab->tga_desc_fd->oh = htab->tga_desc;
8093 htab->tga_desc_fd->is_func_descriptor = 1;
8094 if (htab->tga_desc != NULL)
8095 {
8096 htab->tga_desc->oh = htab->tga_desc_fd;
8097 htab->tga_desc->is_func = 1;
8098 }
a7f2871e
AM
8099 }
8100 }
8101 }
8102 }
7c9cf415
AM
8103 else if (htab->params->tls_get_addr_opt < 0)
8104 htab->params->tls_get_addr_opt = 0;
a7f2871e 8105 }
9e7028aa
AM
8106
8107 if (htab->tga_desc_fd != NULL
8108 && htab->params->tls_get_addr_opt
8109 && htab->params->no_tls_get_addr_regsave == -1)
8110 htab->params->no_tls_get_addr_regsave = 0;
8111
0a1b45a2 8112 return true;
3a71aa26 8113}
8387904d 8114
3a71aa26 8115/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 8116 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 8117
0a1b45a2 8118static bool
30845f11
AM
8119branch_reloc_hash_match (bfd *ibfd,
8120 Elf_Internal_Rela *rel,
8121 struct ppc_link_hash_entry *hash1,
8122 struct ppc_link_hash_entry *hash2,
8123 struct ppc_link_hash_entry *hash3,
8124 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
8125{
8126 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8127 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8128 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8129
e054468f 8130 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8131 {
3a71aa26
AM
8132 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8133 struct elf_link_hash_entry *h;
8387904d 8134
3a71aa26 8135 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8136 h = elf_follow_link (h);
30845f11
AM
8137 if (h == elf_hash_entry (hash1)
8138 || h == elf_hash_entry (hash2)
8139 || h == elf_hash_entry (hash3)
8140 || h == elf_hash_entry (hash4))
0a1b45a2 8141 return true;
a48ebf4d 8142 }
0a1b45a2 8143 return false;
951fd09b 8144}
411e1bfb 8145
951fd09b
AM
8146/* Run through all the TLS relocs looking for optimization
8147 opportunities. The linker has been hacked (see ppc64elf.em) to do
8148 a preliminary section layout so that we know the TLS segment
8149 offsets. We can't optimize earlier because some optimizations need
8150 to know the tp offset, and we need to optimize before allocating
8151 dynamic relocations. */
8152
0a1b45a2 8153bool
33c0ec9d 8154ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8155{
8156 bfd *ibfd;
8157 asection *sec;
8158 struct ppc_link_hash_table *htab;
663a1470 8159 unsigned char *toc_ref;
102890f0 8160 int pass;
951fd09b 8161
3cbc1e5e 8162 if (!bfd_link_executable (info))
0a1b45a2 8163 return true;
411e1bfb 8164
951fd09b 8165 htab = ppc_hash_table (info);
4dfe6ac6 8166 if (htab == NULL)
0a1b45a2 8167 return false;
4dfe6ac6 8168
252dcdf4
AM
8169 htab->do_tls_opt = 1;
8170
663a1470
AM
8171 /* Make two passes over the relocs. On the first pass, mark toc
8172 entries involved with tls relocs, and check that tls relocs
8173 involved in setting up a tls_get_addr call are indeed followed by
8174 such a call. If they are not, we can't do any tls optimization.
8175 On the second pass twiddle tls_mask flags to notify
8176 relocate_section that optimization can be done, and adjust got
8177 and plt refcounts. */
8178 toc_ref = NULL;
8179 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8180 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8181 {
8182 Elf_Internal_Sym *locsyms = NULL;
8183 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8184
102890f0
AM
8185 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8186 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8187 {
8188 Elf_Internal_Rela *relstart, *rel, *relend;
0a1b45a2 8189 bool found_tls_get_addr_arg = 0;
411e1bfb 8190
102890f0
AM
8191 /* Read the relocations. */
8192 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8193 info->keep_memory);
8194 if (relstart == NULL)
2915c55b
JK
8195 {
8196 free (toc_ref);
0a1b45a2 8197 return false;
2915c55b 8198 }
411e1bfb 8199
102890f0
AM
8200 relend = relstart + sec->reloc_count;
8201 for (rel = relstart; rel < relend; rel++)
8202 {
8203 enum elf_ppc64_reloc_type r_type;
8204 unsigned long r_symndx;
8205 struct elf_link_hash_entry *h;
8206 Elf_Internal_Sym *sym;
8207 asection *sym_sec;
f961d9dd 8208 unsigned char *tls_mask;
46e9995a 8209 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0 8210 bfd_vma value;
0a1b45a2 8211 bool ok_tprel, is_local;
102890f0
AM
8212 long toc_ref_index = 0;
8213 int expecting_tls_get_addr = 0;
0a1b45a2 8214 bool ret = false;
411e1bfb 8215
102890f0
AM
8216 r_symndx = ELF64_R_SYM (rel->r_info);
8217 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8218 r_symndx, ibfd))
8219 {
8220 err_free_rel:
8221 if (elf_section_data (sec)->relocs != relstart)
8222 free (relstart);
c9594989
AM
8223 free (toc_ref);
8224 if (elf_symtab_hdr (ibfd).contents
8225 != (unsigned char *) locsyms)
102890f0 8226 free (locsyms);
663a1470 8227 return ret;
102890f0 8228 }
411e1bfb 8229
102890f0
AM
8230 if (h != NULL)
8231 {
766bc656
AM
8232 if (h->root.type == bfd_link_hash_defined
8233 || h->root.type == bfd_link_hash_defweak)
8234 value = h->root.u.def.value;
8235 else if (h->root.type == bfd_link_hash_undefweak)
8236 value = 0;
8237 else
663a1470
AM
8238 {
8239 found_tls_get_addr_arg = 0;
8240 continue;
8241 }
102890f0
AM
8242 }
8243 else
8244 /* Symbols referenced by TLS relocs must be of type
8245 STT_TLS. So no need for .opd local sym adjust. */
8246 value = sym->st_value;
8247
0a1b45a2 8248 ok_tprel = false;
f749f26e
AM
8249 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8250 if (is_local)
102890f0 8251 {
766bc656
AM
8252 if (h != NULL
8253 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 8254 ok_tprel = true;
c27b8c2a
AM
8255 else if (sym_sec != NULL
8256 && sym_sec->output_section != NULL)
766bc656
AM
8257 {
8258 value += sym_sec->output_offset;
8259 value += sym_sec->output_section->vma;
0b147428 8260 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8261 /* Note that even though the prefix insns
8262 allow a 1<<33 offset we use the same test
8263 as for addis;addi. There may be a mix of
8264 pcrel and non-pcrel code and the decision
8265 to optimise is per symbol, not per TLS
8266 sequence. */
0b147428 8267 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8268 }
102890f0 8269 }
951fd09b 8270
102890f0 8271 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8272 /* If this section has old-style __tls_get_addr calls
8273 without marker relocs, then check that each
8274 __tls_get_addr call reloc is preceded by a reloc
8275 that conceivably belongs to the __tls_get_addr arg
8276 setup insn. If we don't find matching arg setup
8277 relocs, don't do any tls optimization. */
8278 if (pass == 0
9737e8af 8279 && sec->nomark_tls_get_addr
663a1470 8280 && h != NULL
ed7007c1 8281 && is_tls_get_addr (h, htab)
663a1470
AM
8282 && !found_tls_get_addr_arg
8283 && is_branch_reloc (r_type))
8284 {
25f53a85 8285 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8286 "TLS optimization disabled\n"),
8287 ibfd, sec, rel->r_offset);
0a1b45a2 8288 ret = true;
663a1470
AM
8289 goto err_free_rel;
8290 }
8291
8292 found_tls_get_addr_arg = 0;
102890f0
AM
8293 switch (r_type)
8294 {
8295 case R_PPC64_GOT_TLSLD16:
8296 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8297 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8298 expecting_tls_get_addr = 1;
663a1470 8299 found_tls_get_addr_arg = 1;
1a0670f3 8300 /* Fall through. */
102890f0
AM
8301
8302 case R_PPC64_GOT_TLSLD16_HI:
8303 case R_PPC64_GOT_TLSLD16_HA:
8304 /* These relocs should never be against a symbol
8305 defined in a shared lib. Leave them alone if
8306 that turns out to be the case. */
8307 if (!is_local)
8308 continue;
411e1bfb 8309
102890f0 8310 /* LD -> LE */
411e1bfb 8311 tls_set = 0;
102890f0
AM
8312 tls_clear = TLS_LD;
8313 tls_type = TLS_TLS | TLS_LD;
8314 break;
411e1bfb 8315
102890f0
AM
8316 case R_PPC64_GOT_TLSGD16:
8317 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8318 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8319 expecting_tls_get_addr = 1;
663a1470 8320 found_tls_get_addr_arg = 1;
1a0670f3 8321 /* Fall through. */
102890f0
AM
8322
8323 case R_PPC64_GOT_TLSGD16_HI:
8324 case R_PPC64_GOT_TLSGD16_HA:
8325 if (ok_tprel)
8326 /* GD -> LE */
411e1bfb 8327 tls_set = 0;
102890f0
AM
8328 else
8329 /* GD -> IE */
b00a0a86 8330 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8331 tls_clear = TLS_GD;
8332 tls_type = TLS_TLS | TLS_GD;
8333 break;
8334
87c69f97 8335 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8336 case R_PPC64_GOT_TPREL16_DS:
8337 case R_PPC64_GOT_TPREL16_LO_DS:
8338 case R_PPC64_GOT_TPREL16_HI:
8339 case R_PPC64_GOT_TPREL16_HA:
8340 if (ok_tprel)
8341 {
8342 /* IE -> LE */
8343 tls_set = 0;
8344 tls_clear = TLS_TPREL;
8345 tls_type = TLS_TLS | TLS_TPREL;
8346 break;
8347 }
411e1bfb
AM
8348 continue;
8349
727fc41e 8350 case R_PPC64_TLSLD:
7d04a20a
AM
8351 if (!is_local)
8352 continue;
8353 /* Fall through. */
8354 case R_PPC64_TLSGD:
23cedd1d
AM
8355 if (rel + 1 < relend
8356 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8357 {
8358 if (pass != 0
2cdcc330 8359 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8360 != R_PPC64_PLTSEQ)
8361 && (ELF64_R_TYPE (rel[1].r_info)
8362 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8363 {
8364 r_symndx = ELF64_R_SYM (rel[1].r_info);
8365 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8366 r_symndx, ibfd))
23cedd1d
AM
8367 goto err_free_rel;
8368 if (h != NULL)
8369 {
8370 struct plt_entry *ent = NULL;
8371
8372 for (ent = h->plt.plist;
8373 ent != NULL;
8374 ent = ent->next)
8375 if (ent->addend == rel[1].r_addend)
8376 break;
8377
8378 if (ent != NULL
8379 && ent->plt.refcount > 0)
8380 ent->plt.refcount -= 1;
8381 }
8382 }
8383 continue;
8384 }
663a1470 8385 found_tls_get_addr_arg = 1;
1a0670f3 8386 /* Fall through. */
663a1470
AM
8387
8388 case R_PPC64_TLS:
8389 case R_PPC64_TOC16:
8390 case R_PPC64_TOC16_LO:
102890f0
AM
8391 if (sym_sec == NULL || sym_sec != toc)
8392 continue;
8393
8394 /* Mark this toc entry as referenced by a TLS
8395 code sequence. We can do that now in the
8396 case of R_PPC64_TLS, and after checking for
8397 tls_get_addr for the TOC16 relocs. */
8398 if (toc_ref == NULL)
2cdcc330
AM
8399 toc_ref
8400 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8401 if (toc_ref == NULL)
8402 goto err_free_rel;
8403
102890f0
AM
8404 if (h != NULL)
8405 value = h->root.u.def.value;
8406 else
8407 value = sym->st_value;
8408 value += rel->r_addend;
73242275
AM
8409 if (value % 8 != 0)
8410 continue;
8411 BFD_ASSERT (value < toc->size
8412 && toc->output_offset % 8 == 0);
663a1470 8413 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8414 if (r_type == R_PPC64_TLS
8415 || r_type == R_PPC64_TLSGD
8416 || r_type == R_PPC64_TLSLD)
102890f0
AM
8417 {
8418 toc_ref[toc_ref_index] = 1;
8419 continue;
8420 }
8421
8422 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8423 continue;
8424
8425 tls_set = 0;
8426 tls_clear = 0;
8427 expecting_tls_get_addr = 2;
8428 break;
8429
8430 case R_PPC64_TPREL64:
8431 if (pass == 0
8432 || sec != toc
8433 || toc_ref == NULL
663a1470 8434 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8435 continue;
8436 if (ok_tprel)
8437 {
8438 /* IE -> LE */
8439 tls_set = TLS_EXPLICIT;
8440 tls_clear = TLS_TPREL;
8441 break;
8442 }
8443 continue;
8444
8445 case R_PPC64_DTPMOD64:
8446 if (pass == 0
8447 || sec != toc
8448 || toc_ref == NULL
663a1470 8449 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8450 continue;
8451 if (rel + 1 < relend
8452 && (rel[1].r_info
8453 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8454 && rel[1].r_offset == rel->r_offset + 8)
8455 {
8456 if (ok_tprel)
8457 /* GD -> LE */
8458 tls_set = TLS_EXPLICIT | TLS_GD;
8459 else
8460 /* GD -> IE */
b00a0a86 8461 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8462 tls_clear = TLS_GD;
8463 }
8464 else
8465 {
8466 if (!is_local)
8467 continue;
8468
8469 /* LD -> LE */
8470 tls_set = TLS_EXPLICIT;
8471 tls_clear = TLS_LD;
8472 }
8473 break;
8474
252dcdf4
AM
8475 case R_PPC64_TPREL16_HA:
8476 if (pass == 0)
8477 {
8478 unsigned char buf[4];
8479 unsigned int insn;
8480 bfd_vma off = rel->r_offset & ~3;
8481 if (!bfd_get_section_contents (ibfd, sec, buf,
8482 off, 4))
8483 goto err_free_rel;
8484 insn = bfd_get_32 (ibfd, buf);
8485 /* addis rt,13,imm */
8486 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8487 != ((15u << 26) | (13 << 16)))
8488 {
8489 /* xgettext:c-format */
8490 info->callbacks->minfo
8491 (_("%H: warning: %s unexpected insn %#x.\n"),
8492 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8493 htab->do_tls_opt = 0;
8494 }
8495 }
8496 continue;
8497
8498 case R_PPC64_TPREL16_HI:
8499 case R_PPC64_TPREL16_HIGH:
8500 case R_PPC64_TPREL16_HIGHA:
8501 case R_PPC64_TPREL16_HIGHER:
8502 case R_PPC64_TPREL16_HIGHERA:
8503 case R_PPC64_TPREL16_HIGHEST:
8504 case R_PPC64_TPREL16_HIGHESTA:
8505 /* These can all be used in sequences along with
8506 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8507 able to verify easily. */
8508 htab->do_tls_opt = 0;
8509 continue;
8510
102890f0
AM
8511 default:
8512 continue;
8513 }
8514
8515 if (pass == 0)
8516 {
727fc41e 8517 if (!expecting_tls_get_addr
9737e8af 8518 || !sec->nomark_tls_get_addr)
102890f0
AM
8519 continue;
8520
3a71aa26
AM
8521 if (rel + 1 < relend
8522 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8523 htab->tls_get_addr_fd,
8524 htab->tga_desc_fd,
3a71aa26 8525 htab->tls_get_addr,
9e7028aa 8526 htab->tga_desc))
102890f0 8527 {
3a71aa26 8528 if (expecting_tls_get_addr == 2)
102890f0 8529 {
3a71aa26 8530 /* Check for toc tls entries. */
f961d9dd 8531 unsigned char *toc_tls;
3a71aa26
AM
8532 int retval;
8533
8534 retval = get_tls_mask (&toc_tls, NULL, NULL,
8535 &locsyms,
8536 rel, ibfd);
8537 if (retval == 0)
8538 goto err_free_rel;
663a1470
AM
8539 if (toc_tls != NULL)
8540 {
37da22e5
AM
8541 if ((*toc_tls & TLS_TLS) != 0
8542 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8543 found_tls_get_addr_arg = 1;
8544 if (retval > 1)
8545 toc_ref[toc_ref_index] = 1;
8546 }
102890f0 8547 }
3a71aa26 8548 continue;
102890f0
AM
8549 }
8550
102890f0
AM
8551 /* Uh oh, we didn't find the expected call. We
8552 could just mark this symbol to exclude it
8553 from tls optimization but it's safer to skip
663a1470 8554 the entire optimization. */
695344c0 8555 /* xgettext:c-format */
25f53a85 8556 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8557 "TLS optimization disabled\n"),
8558 ibfd, sec, rel->r_offset);
0a1b45a2 8559 ret = true;
663a1470 8560 goto err_free_rel;
102890f0
AM
8561 }
8562
37da22e5
AM
8563 /* If we don't have old-style __tls_get_addr calls
8564 without TLSGD/TLSLD marker relocs, and we haven't
8565 found a new-style __tls_get_addr call with a
8566 marker for this symbol, then we either have a
8567 broken object file or an -mlongcall style
8568 indirect call to __tls_get_addr without a marker.
8569 Disable optimization in this case. */
8570 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8571 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8572 && !sec->nomark_tls_get_addr
37da22e5
AM
8573 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8574 != (TLS_TLS | TLS_MARK)))
8575 continue;
8576
7d04a20a 8577 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8578 {
23cedd1d
AM
8579 struct plt_entry *ent = NULL;
8580
9e7028aa
AM
8581 if (htab->tls_get_addr_fd != NULL)
8582 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8583 ent != NULL;
8584 ent = ent->next)
8585 if (ent->addend == 0)
8586 break;
8587
8588 if (ent == NULL && htab->tga_desc_fd != NULL)
8589 for (ent = htab->tga_desc_fd->elf.plt.plist;
8590 ent != NULL;
8591 ent = ent->next)
8592 if (ent->addend == 0)
8593 break;
8594
8595 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8596 for (ent = htab->tls_get_addr->elf.plt.plist;
8597 ent != NULL;
8598 ent = ent->next)
8599 if (ent->addend == 0)
102890f0 8600 break;
411e1bfb 8601
9e7028aa
AM
8602 if (ent == NULL && htab->tga_desc != NULL)
8603 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8604 ent != NULL;
8605 ent = ent->next)
8606 if (ent->addend == 0)
102890f0 8607 break;
23cedd1d
AM
8608
8609 if (ent != NULL
8610 && ent->plt.refcount > 0)
8611 ent->plt.refcount -= 1;
102890f0 8612 }
411e1bfb 8613
102890f0 8614 if (tls_clear == 0)
30038c59
AM
8615 continue;
8616
102890f0
AM
8617 if ((tls_set & TLS_EXPLICIT) == 0)
8618 {
8619 struct got_entry *ent;
411e1bfb 8620
102890f0
AM
8621 /* Adjust got entry for this reloc. */
8622 if (h != NULL)
8623 ent = h->got.glist;
8624 else
8625 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8626
102890f0
AM
8627 for (; ent != NULL; ent = ent->next)
8628 if (ent->addend == rel->r_addend
8629 && ent->owner == ibfd
8630 && ent->tls_type == tls_type)
8631 break;
8632 if (ent == NULL)
8633 abort ();
411e1bfb 8634
102890f0
AM
8635 if (tls_set == 0)
8636 {
8637 /* We managed to get rid of a got entry. */
8638 if (ent->got.refcount > 0)
8639 ent->got.refcount -= 1;
8640 }
8641 }
8642 else
8643 {
8644 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8645 we'll lose one or two dyn relocs. */
1657026c 8646 if (!dec_dynrel_count (rel, sec, info,
19e08130 8647 NULL, h, sym))
0a1b45a2 8648 return false;
411e1bfb 8649
102890f0
AM
8650 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8651 {
1657026c 8652 if (!dec_dynrel_count (rel + 1, sec, info,
19e08130 8653 NULL, h, sym))
0a1b45a2 8654 return false;
102890f0
AM
8655 }
8656 }
411e1bfb 8657
46e9995a 8658 *tls_mask |= tls_set & 0xff;
102890f0
AM
8659 *tls_mask &= ~tls_clear;
8660 }
8c1d1bb8 8661
102890f0
AM
8662 if (elf_section_data (sec)->relocs != relstart)
8663 free (relstart);
8664 }
411e1bfb 8665
663a1470
AM
8666 if (locsyms != NULL
8667 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8668 {
8669 if (!info->keep_memory)
8670 free (locsyms);
8671 else
8672 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8673 }
8674 }
411e1bfb 8675
c9594989 8676 free (toc_ref);
0a1b45a2 8677 return true;
1e2f5b6e 8678}
b34976b6 8679
c5614fa4
AM
8680/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8681 the values of any global symbols in a toc section that has been
8682 edited. Globals in toc sections should be a rarity, so this function
8683 sets a flag if any are found in toc sections other than the one just
de194d85 8684 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8685
8686struct adjust_toc_info
8687{
8688 asection *toc;
8689 unsigned long *skip;
0a1b45a2 8690 bool global_toc_syms;
c5614fa4
AM
8691};
8692
ba761f19
AM
8693enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8694
0a1b45a2 8695static bool
c5614fa4
AM
8696adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8697{
8698 struct ppc_link_hash_entry *eh;
8699 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8700 unsigned long i;
c5614fa4 8701
c5614fa4
AM
8702 if (h->root.type != bfd_link_hash_defined
8703 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 8704 return true;
c5614fa4 8705
ed7007c1 8706 eh = ppc_elf_hash_entry (h);
c5614fa4 8707 if (eh->adjust_done)
0a1b45a2 8708 return true;
c5614fa4
AM
8709
8710 if (eh->elf.root.u.def.section == toc_inf->toc)
8711 {
854b41e7
AM
8712 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8713 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8714 else
854b41e7
AM
8715 i = eh->elf.root.u.def.value >> 3;
8716
ba761f19 8717 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8718 {
4eca0228 8719 _bfd_error_handler
854b41e7
AM
8720 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8721 do
8722 ++i;
ba761f19 8723 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8724 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8725 }
854b41e7
AM
8726
8727 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8728 eh->adjust_done = 1;
8729 }
8730 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
0a1b45a2 8731 toc_inf->global_toc_syms = true;
c5614fa4 8732
0a1b45a2 8733 return true;
c5614fa4
AM
8734}
8735
39eeab25
AM
8736/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8737 on a _LO variety toc/got reloc. */
560c8763 8738
0a1b45a2 8739static bool
39eeab25 8740ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8741{
2365f8d7
AM
8742 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8743 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8744 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8745 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8746 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8747 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8748 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8749 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8750 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8751 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8752 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8753 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8754 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8755 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8756 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8757 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8758 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8759 /* Exclude lfqu by testing reloc. If relocs are ever
8760 defined for the reduced D field in psq_lu then those
8761 will need testing too. */
8762 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8763 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8764 && (insn & 1) == 0)
2365f8d7
AM
8765 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8766 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8767 /* Exclude stfqu. psq_stu as above for psq_lu. */
8768 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8769 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8770 && (insn & 1) == 0));
560c8763
AM
8771}
8772
4a421c53
AM
8773/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8774 pld ra,symbol@got@pcrel
dd9b12c2 8775 load/store rt,off(ra)
4a421c53 8776 or
d4b87b1e 8777 pla ra,symbol@pcrel
dd9b12c2 8778 load/store rt,off(ra)
4a421c53 8779 may be translated to
dd9b12c2 8780 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8781 nop.
8782 This function returns true if the optimization is possible, placing
dd9b12c2 8783 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8784
8785 On entry to this function, the linker has already determined that
d4b87b1e 8786 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8787 while *PINSN2 is the second instruction. */
8788
0a1b45a2 8789static bool
dd9b12c2 8790xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8791{
77486630
AM
8792 uint64_t insn1 = *pinsn1;
8793 uint64_t insn2 = *pinsn2;
dd9b12c2 8794 bfd_signed_vma off;
4a421c53 8795
77486630
AM
8796 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8797 {
8798 /* Check that regs match. */
8799 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8800 return false;
77486630
AM
8801
8802 /* P8LS or PMLS form, non-pcrel. */
8803 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
0a1b45a2 8804 return false;
77486630
AM
8805
8806 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8807 *pinsn2 = PNOP;
8808 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8809 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
0a1b45a2 8810 return true;
77486630
AM
8811 }
8812
8813 insn2 >>= 32;
8814
4a421c53 8815 /* Check that regs match. */
77486630 8816 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8817 return false;
4a421c53
AM
8818
8819 switch ((insn2 >> 26) & 63)
8820 {
8821 default:
0a1b45a2 8822 return false;
4a421c53
AM
8823
8824 case 32: /* lwz */
8825 case 34: /* lbz */
8826 case 36: /* stw */
8827 case 38: /* stb */
8828 case 40: /* lhz */
8829 case 42: /* lha */
8830 case 44: /* sth */
8831 case 48: /* lfs */
8832 case 50: /* lfd */
8833 case 52: /* stfs */
8834 case 54: /* stfd */
8835 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8836 on the insn. */
77486630 8837 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8838 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8839 off = insn2 & 0xffff;
4a421c53
AM
8840 break;
8841
8842 case 58: /* lwa, ld */
dd9b12c2 8843 if ((insn2 & 1) != 0)
0a1b45a2 8844 return false;
77486630 8845 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8846 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8847 | (insn2 & (31ULL << 21)));
dd9b12c2 8848 off = insn2 & 0xfffc;
4a421c53
AM
8849 break;
8850
8851 case 57: /* lxsd, lxssp */
dd9b12c2 8852 if ((insn2 & 3) < 2)
0a1b45a2 8853 return false;
77486630 8854 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8855 | ((40ULL | (insn2 & 3)) << 26)
8856 | (insn2 & (31ULL << 21)));
dd9b12c2 8857 off = insn2 & 0xfffc;
4a421c53
AM
8858 break;
8859
8860 case 61: /* stxsd, stxssp, lxv, stxv */
8861 if ((insn2 & 3) == 0)
0a1b45a2 8862 return false;
4a421c53
AM
8863 else if ((insn2 & 3) >= 2)
8864 {
77486630 8865 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8866 | ((44ULL | (insn2 & 3)) << 26)
8867 | (insn2 & (31ULL << 21)));
dd9b12c2 8868 off = insn2 & 0xfffc;
4a421c53
AM
8869 }
8870 else
8871 {
77486630 8872 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8873 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8874 | (insn2 & (31ULL << 21)));
dd9b12c2 8875 off = insn2 & 0xfff0;
4a421c53
AM
8876 }
8877 break;
8878
8879 case 56: /* lq */
77486630 8880 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8881 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8882 off = insn2 & 0xffff;
4a421c53
AM
8883 break;
8884
94ba9882
AM
8885 case 6: /* lxvp, stxvp */
8886 if ((insn2 & 0xe) != 0)
0a1b45a2 8887 return false;
94ba9882
AM
8888 insn1 = ((1ULL << 58) | (1ULL << 52)
8889 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8890 | (insn2 & (31ULL << 21)));
8891 off = insn2 & 0xfff0;
8892 break;
8893
4a421c53 8894 case 62: /* std, stq */
dd9b12c2 8895 if ((insn2 & 1) != 0)
0a1b45a2 8896 return false;
77486630 8897 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8898 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8899 | (insn2 & (31ULL << 21)));
dd9b12c2 8900 off = insn2 & 0xfffc;
4a421c53
AM
8901 break;
8902 }
8903
77486630 8904 *pinsn1 = insn1;
4a421c53 8905 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8906 *poff = (off ^ 0x8000) - 0x8000;
0a1b45a2 8907 return true;
4a421c53
AM
8908}
8909
c5614fa4
AM
8910/* Examine all relocs referencing .toc sections in order to remove
8911 unused .toc entries. */
8912
0a1b45a2 8913bool
33c0ec9d 8914ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8915{
8916 bfd *ibfd;
8917 struct adjust_toc_info toc_inf;
67f0cbdb 8918 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8919
67f0cbdb 8920 htab->do_toc_opt = 1;
0a1b45a2 8921 toc_inf.global_toc_syms = true;
c72f2fb2 8922 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8923 {
8924 asection *toc, *sec;
8925 Elf_Internal_Shdr *symtab_hdr;
8926 Elf_Internal_Sym *local_syms;
425b145b 8927 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8928 unsigned long *skip, *drop;
8929 unsigned char *used;
8930 unsigned char *keep, last, some_unused;
8931
854b41e7
AM
8932 if (!is_ppc64_elf (ibfd))
8933 continue;
8934
c5614fa4
AM
8935 toc = bfd_get_section_by_name (ibfd, ".toc");
8936 if (toc == NULL
92b7a70f 8937 || toc->size == 0
dbaa2011
AM
8938 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8939 || discarded_section (toc))
c5614fa4
AM
8940 continue;
8941
425b145b 8942 toc_relocs = NULL;
c5614fa4 8943 local_syms = NULL;
0ffa91dd 8944 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8945
8946 /* Look at sections dropped from the final link. */
8947 skip = NULL;
8948 relstart = NULL;
8949 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8950 {
8951 if (sec->reloc_count == 0
dbaa2011 8952 || !discarded_section (sec)
c5614fa4
AM
8953 || get_opd_info (sec)
8954 || (sec->flags & SEC_ALLOC) == 0
8955 || (sec->flags & SEC_DEBUGGING) != 0)
8956 continue;
8957
0a1b45a2 8958 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
c5614fa4
AM
8959 if (relstart == NULL)
8960 goto error_ret;
8961
8962 /* Run through the relocs to see which toc entries might be
8963 unused. */
8964 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8965 {
8966 enum elf_ppc64_reloc_type r_type;
8967 unsigned long r_symndx;
8968 asection *sym_sec;
8969 struct elf_link_hash_entry *h;
8970 Elf_Internal_Sym *sym;
8971 bfd_vma val;
8972
8973 r_type = ELF64_R_TYPE (rel->r_info);
8974 switch (r_type)
8975 {
8976 default:
8977 continue;
8978
8979 case R_PPC64_TOC16:
8980 case R_PPC64_TOC16_LO:
8981 case R_PPC64_TOC16_HI:
8982 case R_PPC64_TOC16_HA:
8983 case R_PPC64_TOC16_DS:
8984 case R_PPC64_TOC16_LO_DS:
8985 break;
8986 }
8987
8988 r_symndx = ELF64_R_SYM (rel->r_info);
8989 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8990 r_symndx, ibfd))
8991 goto error_ret;
8992
8993 if (sym_sec != toc)
8994 continue;
8995
8996 if (h != NULL)
8997 val = h->root.u.def.value;
8998 else
8999 val = sym->st_value;
9000 val += rel->r_addend;
9001
9002 if (val >= toc->size)
9003 continue;
9004
9005 /* Anything in the toc ought to be aligned to 8 bytes.
9006 If not, don't mark as unused. */
9007 if (val & 7)
9008 continue;
9009
9010 if (skip == NULL)
9011 {
854b41e7 9012 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9013 if (skip == NULL)
9014 goto error_ret;
9015 }
9016
ba761f19 9017 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9018 }
9019
9020 if (elf_section_data (sec)->relocs != relstart)
9021 free (relstart);
9022 }
9023
ba761f19
AM
9024 /* For largetoc loads of address constants, we can convert
9025 . addis rx,2,addr@got@ha
9026 . ld ry,addr@got@l(rx)
9027 to
9028 . addis rx,2,addr@toc@ha
9029 . addi ry,rx,addr@toc@l
9030 when addr is within 2G of the toc pointer. This then means
9031 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9032
ba761f19
AM
9033 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9034 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9035 && toc->reloc_count != 0)
9036 {
9037 /* Read toc relocs. */
425b145b
AM
9038 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9039 info->keep_memory);
9040 if (toc_relocs == NULL)
ba761f19
AM
9041 goto error_ret;
9042
425b145b 9043 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9044 {
9045 enum elf_ppc64_reloc_type r_type;
9046 unsigned long r_symndx;
9047 asection *sym_sec;
9048 struct elf_link_hash_entry *h;
9049 Elf_Internal_Sym *sym;
9050 bfd_vma val, addr;
9051
9052 r_type = ELF64_R_TYPE (rel->r_info);
9053 if (r_type != R_PPC64_ADDR64)
9054 continue;
9055
9056 r_symndx = ELF64_R_SYM (rel->r_info);
9057 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9058 r_symndx, ibfd))
9059 goto error_ret;
9060
425b145b 9061 if (sym_sec == NULL
c27b8c2a 9062 || sym_sec->output_section == NULL
dbaa2011 9063 || discarded_section (sym_sec))
425b145b
AM
9064 continue;
9065
afe397ea 9066 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9067 continue;
9068
9069 if (h != NULL)
bddc25c9
AM
9070 {
9071 if (h->type == STT_GNU_IFUNC)
9072 continue;
9073 val = h->root.u.def.value;
9074 }
ba761f19 9075 else
bddc25c9
AM
9076 {
9077 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9078 continue;
9079 val = sym->st_value;
9080 }
ba761f19
AM
9081 val += rel->r_addend;
9082 val += sym_sec->output_section->vma + sym_sec->output_offset;
9083
9084 /* We don't yet know the exact toc pointer value, but we
9085 know it will be somewhere in the toc section. Don't
9086 optimize if the difference from any possible toc
9087 pointer is outside [ff..f80008000, 7fff7fff]. */
9088 addr = toc->output_section->vma + TOC_BASE_OFF;
9089 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9090 continue;
9091
9092 addr = toc->output_section->vma + toc->output_section->rawsize;
9093 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9094 continue;
9095
9096 if (skip == NULL)
9097 {
9098 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9099 if (skip == NULL)
9100 goto error_ret;
9101 }
9102
9103 skip[rel->r_offset >> 3]
425b145b 9104 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9105 }
ba761f19
AM
9106 }
9107
c5614fa4
AM
9108 if (skip == NULL)
9109 continue;
9110
9111 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9112 if (used == NULL)
9113 {
9114 error_ret:
c9594989 9115 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
9116 free (local_syms);
9117 if (sec != NULL
c5614fa4
AM
9118 && elf_section_data (sec)->relocs != relstart)
9119 free (relstart);
c9594989 9120 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9121 free (toc_relocs);
c9594989 9122 free (skip);
0a1b45a2 9123 return false;
c5614fa4
AM
9124 }
9125
30038c59
AM
9126 /* Now check all kept sections that might reference the toc.
9127 Check the toc itself last. */
9128 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9129 : ibfd->sections);
c5614fa4 9130 sec != NULL;
c5614fa4 9131 sec = (sec == toc ? NULL
c5614fa4 9132 : sec->next == NULL ? toc
30038c59 9133 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9134 : sec->next))
9135 {
9136 int repeat;
9137
9138 if (sec->reloc_count == 0
dbaa2011 9139 || discarded_section (sec)
c5614fa4
AM
9140 || get_opd_info (sec)
9141 || (sec->flags & SEC_ALLOC) == 0
9142 || (sec->flags & SEC_DEBUGGING) != 0)
9143 continue;
9144
854b41e7
AM
9145 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9146 info->keep_memory);
c5614fa4 9147 if (relstart == NULL)
2915c55b
JK
9148 {
9149 free (used);
9150 goto error_ret;
9151 }
c5614fa4
AM
9152
9153 /* Mark toc entries referenced as used. */
c5614fa4 9154 do
d4f1ee75
AM
9155 {
9156 repeat = 0;
9157 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9158 {
9159 enum elf_ppc64_reloc_type r_type;
9160 unsigned long r_symndx;
9161 asection *sym_sec;
9162 struct elf_link_hash_entry *h;
9163 Elf_Internal_Sym *sym;
9164 bfd_vma val;
98528052 9165
d4f1ee75 9166 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
9167 switch (r_type)
9168 {
9169 case R_PPC64_TOC16:
9170 case R_PPC64_TOC16_LO:
9171 case R_PPC64_TOC16_HI:
9172 case R_PPC64_TOC16_HA:
9173 case R_PPC64_TOC16_DS:
9174 case R_PPC64_TOC16_LO_DS:
9175 /* In case we're taking addresses of toc entries. */
9176 case R_PPC64_ADDR64:
9177 break;
c5614fa4 9178
d4f1ee75
AM
9179 default:
9180 continue;
9181 }
c5614fa4 9182
d4f1ee75
AM
9183 r_symndx = ELF64_R_SYM (rel->r_info);
9184 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9185 r_symndx, ibfd))
9186 {
9187 free (used);
9188 goto error_ret;
9189 }
c5614fa4 9190
d4f1ee75
AM
9191 if (sym_sec != toc)
9192 continue;
c5614fa4 9193
d4f1ee75
AM
9194 if (h != NULL)
9195 val = h->root.u.def.value;
9196 else
9197 val = sym->st_value;
9198 val += rel->r_addend;
ba761f19 9199
d4f1ee75
AM
9200 if (val >= toc->size)
9201 continue;
ba761f19 9202
d4f1ee75
AM
9203 if ((skip[val >> 3] & can_optimize) != 0)
9204 {
9205 bfd_vma off;
9206 unsigned char opc;
9207
9208 switch (r_type)
9209 {
9210 case R_PPC64_TOC16_HA:
ba761f19 9211 break;
ba761f19 9212
d4f1ee75
AM
9213 case R_PPC64_TOC16_LO_DS:
9214 off = rel->r_offset;
9215 off += (bfd_big_endian (ibfd) ? -2 : 3);
9216 if (!bfd_get_section_contents (ibfd, sec, &opc,
9217 off, 1))
9218 {
9219 free (used);
9220 goto error_ret;
9221 }
9222 if ((opc & (0x3f << 2)) == (58u << 2))
9223 break;
1a0670f3 9224 /* Fall through. */
ba761f19 9225
d4f1ee75
AM
9226 default:
9227 /* Wrong sort of reloc, or not a ld. We may
9228 as well clear ref_from_discarded too. */
9229 skip[val >> 3] = 0;
9230 }
9231 }
9232
9233 if (sec != toc)
9234 used[val >> 3] = 1;
9235 /* For the toc section, we only mark as used if this
9236 entry itself isn't unused. */
9237 else if ((used[rel->r_offset >> 3]
9238 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9239 && !used[val >> 3])
9240 {
9241 /* Do all the relocs again, to catch reference
9242 chains. */
9243 repeat = 1;
9244 used[val >> 3] = 1;
9245 }
9246 }
9247 }
c5614fa4 9248 while (repeat);
854b41e7
AM
9249
9250 if (elf_section_data (sec)->relocs != relstart)
9251 free (relstart);
c5614fa4
AM
9252 }
9253
9254 /* Merge the used and skip arrays. Assume that TOC
9255 doublewords not appearing as either used or unused belong
de194d85 9256 to an entry more than one doubleword in size. */
c5614fa4
AM
9257 for (drop = skip, keep = used, last = 0, some_unused = 0;
9258 drop < skip + (toc->size + 7) / 8;
9259 ++drop, ++keep)
9260 {
9261 if (*keep)
9262 {
ba761f19
AM
9263 *drop &= ~ref_from_discarded;
9264 if ((*drop & can_optimize) != 0)
9265 some_unused = 1;
c5614fa4
AM
9266 last = 0;
9267 }
b140b010 9268 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9269 {
9270 some_unused = 1;
ba761f19 9271 last = ref_from_discarded;
c5614fa4
AM
9272 }
9273 else
9274 *drop = last;
9275 }
9276
9277 free (used);
9278
9279 if (some_unused)
9280 {
9281 bfd_byte *contents, *src;
9282 unsigned long off;
d62b3684 9283 Elf_Internal_Sym *sym;
0a1b45a2 9284 bool local_toc_syms = false;
c5614fa4
AM
9285
9286 /* Shuffle the toc contents, and at the same time convert the
9287 skip array from booleans into offsets. */
9288 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9289 goto error_ret;
9290
9291 elf_section_data (toc)->this_hdr.contents = contents;
9292
9293 for (src = contents, off = 0, drop = skip;
9294 src < contents + toc->size;
9295 src += 8, ++drop)
9296 {
ba761f19
AM
9297 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9298 off += 8;
c5614fa4
AM
9299 else if (off != 0)
9300 {
9301 *drop = off;
9302 memcpy (src - off, src, 8);
9303 }
9304 }
854b41e7 9305 *drop = off;
c5614fa4
AM
9306 toc->rawsize = toc->size;
9307 toc->size = src - contents - off;
9308
ba761f19
AM
9309 /* Adjust addends for relocs against the toc section sym,
9310 and optimize any accesses we can. */
c5614fa4
AM
9311 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9312 {
9313 if (sec->reloc_count == 0
dbaa2011 9314 || discarded_section (sec))
c5614fa4
AM
9315 continue;
9316
9317 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9318 info->keep_memory);
c5614fa4
AM
9319 if (relstart == NULL)
9320 goto error_ret;
9321
9322 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9323 {
9324 enum elf_ppc64_reloc_type r_type;
9325 unsigned long r_symndx;
9326 asection *sym_sec;
9327 struct elf_link_hash_entry *h;
854b41e7 9328 bfd_vma val;
c5614fa4
AM
9329
9330 r_type = ELF64_R_TYPE (rel->r_info);
9331 switch (r_type)
9332 {
9333 default:
9334 continue;
9335
9336 case R_PPC64_TOC16:
9337 case R_PPC64_TOC16_LO:
9338 case R_PPC64_TOC16_HI:
9339 case R_PPC64_TOC16_HA:
9340 case R_PPC64_TOC16_DS:
9341 case R_PPC64_TOC16_LO_DS:
9342 case R_PPC64_ADDR64:
9343 break;
9344 }
9345
9346 r_symndx = ELF64_R_SYM (rel->r_info);
9347 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9348 r_symndx, ibfd))
9349 goto error_ret;
9350
ba761f19 9351 if (sym_sec != toc)
c5614fa4
AM
9352 continue;
9353
ba761f19
AM
9354 if (h != NULL)
9355 val = h->root.u.def.value;
9356 else
9357 {
9358 val = sym->st_value;
9359 if (val != 0)
0a1b45a2 9360 local_toc_syms = true;
ba761f19
AM
9361 }
9362
9363 val += rel->r_addend;
854b41e7
AM
9364
9365 if (val > toc->rawsize)
9366 val = toc->rawsize;
ba761f19
AM
9367 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9368 continue;
9369 else if ((skip[val >> 3] & can_optimize) != 0)
9370 {
9371 Elf_Internal_Rela *tocrel
425b145b 9372 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9373 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9374
9375 switch (r_type)
9376 {
9377 case R_PPC64_TOC16_HA:
9378 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9379 break;
9380
9381 case R_PPC64_TOC16_LO_DS:
9382 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9383 break;
9384
9385 default:
28942f62
AM
9386 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9387 ppc_howto_init ();
b140b010 9388 info->callbacks->einfo
695344c0 9389 /* xgettext:c-format */
174d0a74 9390 (_("%H: %s references "
b140b010
AM
9391 "optimized away TOC entry\n"),
9392 ibfd, sec, rel->r_offset,
9393 ppc64_elf_howto_table[r_type]->name);
9394 bfd_set_error (bfd_error_bad_value);
9395 goto error_ret;
ba761f19
AM
9396 }
9397 rel->r_addend = tocrel->r_addend;
9398 elf_section_data (sec)->relocs = relstart;
9399 continue;
9400 }
9401
9402 if (h != NULL || sym->st_value != 0)
9403 continue;
854b41e7
AM
9404
9405 rel->r_addend -= skip[val >> 3];
9406 elf_section_data (sec)->relocs = relstart;
c5614fa4 9407 }
854b41e7
AM
9408
9409 if (elf_section_data (sec)->relocs != relstart)
9410 free (relstart);
c5614fa4
AM
9411 }
9412
9413 /* We shouldn't have local or global symbols defined in the TOC,
9414 but handle them anyway. */
df22d223
AM
9415 if (local_syms != NULL)
9416 for (sym = local_syms;
9417 sym < local_syms + symtab_hdr->sh_info;
9418 ++sym)
9419 if (sym->st_value != 0
9420 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9421 {
9422 unsigned long i;
854b41e7 9423
df22d223
AM
9424 if (sym->st_value > toc->rawsize)
9425 i = toc->rawsize >> 3;
9426 else
9427 i = sym->st_value >> 3;
854b41e7 9428
df22d223
AM
9429 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9430 {
9431 if (local_toc_syms)
4eca0228 9432 _bfd_error_handler
df22d223
AM
9433 (_("%s defined on removed toc entry"),
9434 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9435 do
9436 ++i;
9437 while ((skip[i] & (ref_from_discarded | can_optimize)));
9438 sym->st_value = (bfd_vma) i << 3;
9439 }
d62b3684 9440
df22d223
AM
9441 sym->st_value -= skip[i];
9442 symtab_hdr->contents = (unsigned char *) local_syms;
9443 }
c5614fa4 9444
854b41e7 9445 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9446 if (toc_inf.global_toc_syms)
9447 {
9448 toc_inf.toc = toc;
9449 toc_inf.skip = skip;
0a1b45a2 9450 toc_inf.global_toc_syms = false;
c5614fa4
AM
9451 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9452 &toc_inf);
9453 }
854b41e7
AM
9454
9455 if (toc->reloc_count != 0)
9456 {
d4730f92 9457 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9458 Elf_Internal_Rela *wrel;
9459 bfd_size_type sz;
9460
854b41e7 9461 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9462 if (toc_relocs == NULL)
9463 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9464 info->keep_memory);
9465 if (toc_relocs == NULL)
9466 goto error_ret;
9467
425b145b
AM
9468 wrel = toc_relocs;
9469 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9470 if ((skip[rel->r_offset >> 3]
9471 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9472 {
9473 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9474 wrel->r_info = rel->r_info;
9475 wrel->r_addend = rel->r_addend;
9476 ++wrel;
9477 }
1657026c 9478 else if (!dec_dynrel_count (rel, toc, info,
854b41e7
AM
9479 &local_syms, NULL, NULL))
9480 goto error_ret;
9481
425b145b
AM
9482 elf_section_data (toc)->relocs = toc_relocs;
9483 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9484 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9485 sz = rel_hdr->sh_entsize;
9486 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9487 }
c5614fa4 9488 }
c9594989 9489 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9490 free (toc_relocs);
c5614fa4
AM
9491
9492 if (local_syms != NULL
9493 && symtab_hdr->contents != (unsigned char *) local_syms)
9494 {
9495 if (!info->keep_memory)
9496 free (local_syms);
9497 else
9498 symtab_hdr->contents = (unsigned char *) local_syms;
9499 }
9500 free (skip);
9501 }
9502
066f4018 9503 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9504 a GOT relative or PC relative access, possibly reducing the
9505 number of GOT entries. */
066f4018
AM
9506 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9507 {
9508 asection *sec;
9509 Elf_Internal_Shdr *symtab_hdr;
9510 Elf_Internal_Sym *local_syms;
9511 Elf_Internal_Rela *relstart, *rel;
9512 bfd_vma got;
9513
9514 if (!is_ppc64_elf (ibfd))
9515 continue;
9516
903b777d 9517 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9518 continue;
9519
9520 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9521 got = 0;
9522 if (sec != NULL)
9523 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9524
9525 local_syms = NULL;
9526 symtab_hdr = &elf_symtab_hdr (ibfd);
9527
9528 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9529 {
9530 if (sec->reloc_count == 0
903b777d 9531 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9532 || discarded_section (sec))
9533 continue;
9534
9535 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9536 info->keep_memory);
9537 if (relstart == NULL)
9538 {
9539 got_error_ret:
c9594989 9540 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9541 free (local_syms);
9542 if (sec != NULL
066f4018
AM
9543 && elf_section_data (sec)->relocs != relstart)
9544 free (relstart);
0a1b45a2 9545 return false;
066f4018
AM
9546 }
9547
9548 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9549 {
9550 enum elf_ppc64_reloc_type r_type;
9551 unsigned long r_symndx;
9552 Elf_Internal_Sym *sym;
9553 asection *sym_sec;
9554 struct elf_link_hash_entry *h;
9555 struct got_entry *ent;
133a1f60 9556 bfd_vma val, pc;
4a421c53 9557 unsigned char buf[8];
066f4018 9558 unsigned int insn;
903b777d 9559 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9560
9561 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9562 switch (r_type)
9563 {
9564 default:
9565 insn_check = no_check;
9566 break;
9567
9568 case R_PPC64_PLT16_HA:
9569 case R_PPC64_GOT_TLSLD16_HA:
9570 case R_PPC64_GOT_TLSGD16_HA:
9571 case R_PPC64_GOT_TPREL16_HA:
9572 case R_PPC64_GOT_DTPREL16_HA:
9573 case R_PPC64_GOT16_HA:
9574 case R_PPC64_TOC16_HA:
9575 insn_check = check_ha;
9576 break;
9577
9578 case R_PPC64_PLT16_LO:
9579 case R_PPC64_PLT16_LO_DS:
9580 case R_PPC64_GOT_TLSLD16_LO:
9581 case R_PPC64_GOT_TLSGD16_LO:
9582 case R_PPC64_GOT_TPREL16_LO_DS:
9583 case R_PPC64_GOT_DTPREL16_LO_DS:
9584 case R_PPC64_GOT16_LO:
9585 case R_PPC64_GOT16_LO_DS:
9586 case R_PPC64_TOC16_LO:
9587 case R_PPC64_TOC16_LO_DS:
9588 insn_check = check_lo;
9589 break;
9590 }
9591
9592 if (insn_check != no_check)
9593 {
9594 bfd_vma off = rel->r_offset & ~3;
9595
9596 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9597 goto got_error_ret;
9598
9599 insn = bfd_get_32 (ibfd, buf);
9600 if (insn_check == check_lo
9601 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9602 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9603 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9604 {
9605 char str[12];
9606
9607 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9608 sprintf (str, "%#08x", insn);
9609 info->callbacks->einfo
9610 /* xgettext:c-format */
9611 (_("%H: got/toc optimization is not supported for"
9612 " %s instruction\n"),
9613 ibfd, sec, rel->r_offset & ~3, str);
9614 continue;
9615 }
9616 }
9617
066f4018
AM
9618 switch (r_type)
9619 {
bb22a418
AM
9620 /* Note that we don't delete GOT entries for
9621 R_PPC64_GOT16_DS since we'd need a lot more
9622 analysis. For starters, the preliminary layout is
9623 before the GOT, PLT, dynamic sections and stubs are
9624 laid out. Then we'd need to allow for changes in
9625 distance between sections caused by alignment. */
066f4018
AM
9626 default:
9627 continue;
9628
066f4018
AM
9629 case R_PPC64_GOT16_HA:
9630 case R_PPC64_GOT16_LO_DS:
4a421c53 9631 case R_PPC64_GOT_PCREL34:
066f4018
AM
9632 break;
9633 }
9634
9635 r_symndx = ELF64_R_SYM (rel->r_info);
9636 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9637 r_symndx, ibfd))
9638 goto got_error_ret;
9639
6d5554a6
AM
9640 if (sym_sec == NULL
9641 || sym_sec->output_section == NULL
9642 || discarded_section (sym_sec))
9643 continue;
9644
06507dab
AM
9645 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9646 continue;
9647
066f4018
AM
9648 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9649 continue;
9650
9651 if (h != NULL)
9652 val = h->root.u.def.value;
9653 else
9654 val = sym->st_value;
133a1f60 9655 val += rel->r_addend;
066f4018
AM
9656 val += sym_sec->output_section->vma + sym_sec->output_offset;
9657
bb22a418
AM
9658/* Fudge factor to allow for the fact that the preliminary layout
9659 isn't exact. Reduce limits by this factor. */
9660#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9661
066f4018
AM
9662 switch (r_type)
9663 {
9664 default:
9665 continue;
9666
066f4018 9667 case R_PPC64_GOT16_HA:
bb22a418
AM
9668 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9669 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9670 continue;
9671
9672 if (!bfd_get_section_contents (ibfd, sec, buf,
9673 rel->r_offset & ~3, 4))
9674 goto got_error_ret;
9675 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9676 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9677 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9678 continue;
9679 break;
9680
9681 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9682 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9683 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9684 continue;
9685 if (!bfd_get_section_contents (ibfd, sec, buf,
9686 rel->r_offset & ~3, 4))
9687 goto got_error_ret;
9688 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9689 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9690 continue;
9691 break;
4a421c53
AM
9692
9693 case R_PPC64_GOT_PCREL34:
9694 pc = rel->r_offset;
9695 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9696 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9697 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9698 continue;
9699 if (!bfd_get_section_contents (ibfd, sec, buf,
9700 rel->r_offset & ~3, 8))
9701 goto got_error_ret;
9702 insn = bfd_get_32 (ibfd, buf);
9703 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9704 continue;
9705 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9706 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9707 continue;
9708 break;
066f4018 9709 }
bb22a418 9710#undef LIMIT_ADJUST
066f4018
AM
9711
9712 if (h != NULL)
9713 ent = h->got.glist;
9714 else
9715 {
9716 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9717 ent = local_got_ents[r_symndx];
9718 }
9719 for (; ent != NULL; ent = ent->next)
133a1f60 9720 if (ent->addend == rel->r_addend
066f4018
AM
9721 && ent->owner == ibfd
9722 && ent->tls_type == 0)
9723 break;
9724 BFD_ASSERT (ent && ent->got.refcount > 0);
9725 ent->got.refcount -= 1;
9726 }
9727
9728 if (elf_section_data (sec)->relocs != relstart)
9729 free (relstart);
9730 }
9731
9732 if (local_syms != NULL
9733 && symtab_hdr->contents != (unsigned char *) local_syms)
9734 {
9735 if (!info->keep_memory)
9736 free (local_syms);
9737 else
9738 symtab_hdr->contents = (unsigned char *) local_syms;
9739 }
9740 }
9741
0a1b45a2 9742 return true;
c5614fa4
AM
9743}
9744
1bbe0902
AM
9745/* Return true iff input section I references the TOC using
9746 instructions limited to +/-32k offsets. */
9747
0a1b45a2 9748bool
1bbe0902
AM
9749ppc64_elf_has_small_toc_reloc (asection *i)
9750{
9751 return (is_ppc64_elf (i->owner)
9752 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9753}
9754
927be08e
AM
9755/* Allocate space for one GOT entry. */
9756
9757static void
9758allocate_got (struct elf_link_hash_entry *h,
9759 struct bfd_link_info *info,
9760 struct got_entry *gent)
9761{
9762 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9763 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9764 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9765 ? 16 : 8);
9766 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9767 ? 2 : 1) * sizeof (Elf64_External_Rela);
9768 asection *got = ppc64_elf_tdata (gent->owner)->got;
9769
9770 gent->got.offset = got->size;
9771 got->size += entsize;
9772
19e08130 9773 if (h->type == STT_GNU_IFUNC)
927be08e 9774 {
33e44f2e 9775 htab->elf.irelplt->size += rentsize;
19e08130 9776 htab->got_reli_size += rentsize;
927be08e 9777 }
f15d0b54 9778 else if (((bfd_link_pic (info)
1657026c
AM
9779 && (gent->tls_type == 0
9780 ? !info->enable_dt_relr
9781 : !(bfd_link_executable (info)
9782 && SYMBOL_REFERENCES_LOCAL (info, h))))
f0158f44
AM
9783 || (htab->elf.dynamic_sections_created
9784 && h->dynindx != -1
9785 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9786 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9787 {
19e08130 9788 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9789 relgot->size += rentsize;
927be08e
AM
9790 }
9791}
9792
7865406b
AM
9793/* This function merges got entries in the same toc group. */
9794
9795static void
9796merge_got_entries (struct got_entry **pent)
9797{
9798 struct got_entry *ent, *ent2;
9799
9800 for (ent = *pent; ent != NULL; ent = ent->next)
9801 if (!ent->is_indirect)
9802 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9803 if (!ent2->is_indirect
9804 && ent2->addend == ent->addend
9805 && ent2->tls_type == ent->tls_type
9806 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9807 {
0a1b45a2 9808 ent2->is_indirect = true;
7865406b
AM
9809 ent2->got.ent = ent;
9810 }
9811}
9812
46434633 9813/* If H is undefined, make it dynamic if that makes sense. */
f0158f44 9814
0a1b45a2 9815static bool
46434633
AM
9816ensure_undef_dynamic (struct bfd_link_info *info,
9817 struct elf_link_hash_entry *h)
f0158f44
AM
9818{
9819 struct elf_link_hash_table *htab = elf_hash_table (info);
9820
9821 if (htab->dynamic_sections_created
46434633
AM
9822 && ((info->dynamic_undefined_weak != 0
9823 && h->root.type == bfd_link_hash_undefweak)
9824 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9825 && h->dynindx == -1
9826 && !h->forced_local
9827 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9828 return bfd_elf_link_record_dynamic_symbol (info, h);
0a1b45a2 9829 return true;
f0158f44
AM
9830}
9831
30845f11
AM
9832/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9833 usual htab->elf.splt section for a PLT entry. */
9834
9835static inline
0a1b45a2 9836bool use_local_plt (struct bfd_link_info *info,
30845f11
AM
9837 struct elf_link_hash_entry *h)
9838{
9839 return (h == NULL
9840 || h->dynindx == -1
9841 || !elf_hash_table (info)->dynamic_sections_created);
9842}
9843
65f38f15
AM
9844/* Allocate space in .plt, .got and associated reloc sections for
9845 dynamic relocs. */
5bd4f169 9846
0a1b45a2 9847static bool
4ce794b7 9848allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9849{
65f38f15
AM
9850 struct bfd_link_info *info;
9851 struct ppc_link_hash_table *htab;
5bd4f169 9852 asection *s;
65f38f15 9853 struct ppc_link_hash_entry *eh;
0b8bcf0d 9854 struct got_entry **pgent, *gent;
5bd4f169 9855
e92d460e 9856 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9857 return true;
5bd4f169 9858
65f38f15
AM
9859 info = (struct bfd_link_info *) inf;
9860 htab = ppc_hash_table (info);
4dfe6ac6 9861 if (htab == NULL)
0a1b45a2 9862 return false;
5bd4f169 9863
ed7007c1 9864 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9865 /* Run through the TLS GD got entries first if we're changing them
9866 to TPREL. */
b00a0a86 9867 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9868 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9869 if (gent->got.refcount > 0
9870 && (gent->tls_type & TLS_GD) != 0)
9871 {
9872 /* This was a GD entry that has been converted to TPREL. If
9873 there happens to be a TPREL entry we can use that one. */
9874 struct got_entry *ent;
9875 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9876 if (ent->got.refcount > 0
9877 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9878 && ent->addend == gent->addend
9879 && ent->owner == gent->owner)
951fd09b
AM
9880 {
9881 gent->got.refcount = 0;
9882 break;
9883 }
9884
9885 /* If not, then we'll be using our own TPREL entry. */
9886 if (gent->got.refcount != 0)
9887 gent->tls_type = TLS_TLS | TLS_TPREL;
9888 }
9889
7865406b
AM
9890 /* Remove any list entry that won't generate a word in the GOT before
9891 we call merge_got_entries. Otherwise we risk merging to empty
9892 entries. */
0b8bcf0d
AM
9893 pgent = &h->got.glist;
9894 while ((gent = *pgent) != NULL)
411e1bfb 9895 if (gent->got.refcount > 0)
7865406b
AM
9896 {
9897 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9898 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9899 {
9900 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9901 *pgent = gent->next;
9902 }
9903 else
9904 pgent = &gent->next;
9905 }
9906 else
9907 *pgent = gent->next;
9908
9909 if (!htab->do_multi_toc)
9910 merge_got_entries (&h->got.glist);
9911
9912 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9913 if (!gent->is_indirect)
411e1bfb 9914 {
ec73ddcd
AM
9915 /* Ensure we catch all the cases where this symbol should
9916 be made dynamic. */
46434633 9917 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9918 return false;
65f38f15 9919
0c8d6e5c 9920 if (!is_ppc64_elf (gent->owner))
927be08e 9921 abort ();
0ffa91dd 9922
927be08e 9923 allocate_got (h, info, gent);
411e1bfb 9924 }
65f38f15 9925
954b63d4
AM
9926 /* If no dynamic sections we can't have dynamic relocs, except for
9927 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9928 if (!htab->elf.dynamic_sections_created
9929 && h->type != STT_GNU_IFUNC)
190eb1dd 9930 h->dyn_relocs = NULL;
8a2058b5 9931
529fe20e
AM
9932 /* Discard relocs on undefined symbols that must be local. */
9933 else if (h->root.type == bfd_link_hash_undefined
9934 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9935 h->dyn_relocs = NULL;
529fe20e 9936
954b63d4
AM
9937 /* Also discard relocs on undefined weak syms with non-default
9938 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9939 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9940 h->dyn_relocs = NULL;
954b63d4 9941
190eb1dd 9942 if (h->dyn_relocs != NULL)
65f38f15 9943 {
1657026c 9944 struct ppc_dyn_relocs *p, **pp;
8a2058b5 9945
57e7d118
AM
9946 /* In the shared -Bsymbolic case, discard space allocated for
9947 dynamic pc-relative relocs against symbols which turn out to
9948 be defined in regular objects. For the normal shared case,
9949 discard space for relocs that have become local due to symbol
9950 visibility changes. */
57e7d118 9951 if (bfd_link_pic (info))
65f38f15 9952 {
57e7d118
AM
9953 /* Relocs that use pc_count are those that appear on a call
9954 insn, or certain REL relocs (see must_be_dyn_reloc) that
9955 can be generated via assembly. We want calls to
9956 protected symbols to resolve directly to the function
9957 rather than going via the plt. If people want function
9958 pointer comparisons to work as expected then they should
9959 avoid writing weird assembly. */
9960 if (SYMBOL_CALLS_LOCAL (info, h))
9961 {
1657026c
AM
9962 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9963 (p = *pp) != NULL;
9964 )
57e7d118
AM
9965 {
9966 p->count -= p->pc_count;
9967 p->pc_count = 0;
9968 if (p->count == 0)
9969 *pp = p->next;
9970 else
9971 pp = &p->next;
9972 }
9973 }
65f38f15 9974
190eb1dd 9975 if (h->dyn_relocs != NULL)
5bd4f169 9976 {
ec73ddcd
AM
9977 /* Ensure we catch all the cases where this symbol
9978 should be made dynamic. */
46434633 9979 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9980 return false;
5bd4f169 9981 }
65f38f15 9982 }
ec73ddcd
AM
9983
9984 /* For a fixed position executable, discard space for
9985 relocs against symbols which are not dynamic. */
9986 else if (h->type != STT_GNU_IFUNC)
57e7d118 9987 {
49160308
AM
9988 if ((h->dynamic_adjusted
9989 || (h->ref_regular
9990 && h->root.type == bfd_link_hash_undefweak
9991 && (info->dynamic_undefined_weak > 0
9992 || !_bfd_elf_readonly_dynrelocs (h))))
529fe20e
AM
9993 && !h->def_regular
9994 && !ELF_COMMON_DEF_P (h))
f0158f44 9995 {
ec73ddcd
AM
9996 /* Ensure we catch all the cases where this symbol
9997 should be made dynamic. */
46434633 9998 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9999 return false;
dfbb6ac9 10000
ec73ddcd 10001 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 10002 if (h->dynindx == -1)
190eb1dd 10003 h->dyn_relocs = NULL;
f0158f44
AM
10004 }
10005 else
190eb1dd 10006 h->dyn_relocs = NULL;
57e7d118
AM
10007 }
10008
10009 /* Finally, allocate space. */
1657026c 10010 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
57e7d118 10011 {
1657026c 10012 unsigned int count;
57e7d118
AM
10013 asection *sreloc = elf_section_data (p->sec)->sreloc;
10014 if (eh->elf.type == STT_GNU_IFUNC)
10015 sreloc = htab->elf.irelplt;
1657026c 10016 count = p->count;
858b0eff 10017 if (info->enable_dt_relr && SYMBOL_REFERENCES_LOCAL (info, h))
1657026c
AM
10018 count -= p->rel_count;
10019 sreloc->size += count * sizeof (Elf64_External_Rela);
dfbb6ac9 10020 }
65f38f15 10021 }
57e7d118 10022
2d7ad24e
AM
10023 /* We might need a PLT entry when the symbol
10024 a) is dynamic, or
10025 b) is an ifunc, or
10026 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10027 d) has plt16 relocs and we are linking statically. */
10028 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10029 || h->type == STT_GNU_IFUNC
10030 || (h->needs_plt && h->dynamic_adjusted)
10031 || (h->needs_plt
10032 && h->def_regular
10033 && !htab->elf.dynamic_sections_created
3e04d765 10034 && !htab->can_convert_all_inline_plt
ed7007c1 10035 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 10036 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10037 {
57e7d118 10038 struct plt_entry *pent;
0a1b45a2 10039 bool doneone = false;
57e7d118
AM
10040 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10041 if (pent->plt.refcount > 0)
10042 {
b2936612
AM
10043 if (!ensure_undef_dynamic (info, h))
10044 return false;
10045
30845f11 10046 if (use_local_plt (info, h))
57e7d118 10047 {
2d7ad24e
AM
10048 if (h->type == STT_GNU_IFUNC)
10049 {
10050 s = htab->elf.iplt;
10051 pent->plt.offset = s->size;
10052 s->size += PLT_ENTRY_SIZE (htab);
10053 s = htab->elf.irelplt;
10054 }
10055 else
10056 {
10057 s = htab->pltlocal;
10058 pent->plt.offset = s->size;
10059 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
1657026c
AM
10060 s = NULL;
10061 if (bfd_link_pic (info)
10062 && !(info->enable_dt_relr && !htab->opd_abi))
10063 s = htab->relpltlocal;
2d7ad24e 10064 }
57e7d118
AM
10065 }
10066 else
10067 {
10068 /* If this is the first .plt entry, make room for the special
10069 first entry. */
10070 s = htab->elf.splt;
10071 if (s->size == 0)
10072 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10073
57e7d118 10074 pent->plt.offset = s->size;
65f38f15 10075
57e7d118
AM
10076 /* Make room for this entry. */
10077 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10078
57e7d118
AM
10079 /* Make room for the .glink code. */
10080 s = htab->glink;
10081 if (s->size == 0)
9e390558 10082 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10083 if (htab->opd_abi)
10084 {
10085 /* We need bigger stubs past index 32767. */
9e390558 10086 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10087 s->size += 4;
10088 s->size += 2*4;
10089 }
10090 else
10091 s->size += 4;
65f38f15 10092
57e7d118
AM
10093 /* We also need to make an entry in the .rela.plt section. */
10094 s = htab->elf.srelplt;
10095 }
2d7ad24e
AM
10096 if (s != NULL)
10097 s->size += sizeof (Elf64_External_Rela);
0a1b45a2 10098 doneone = true;
57e7d118
AM
10099 }
10100 else
10101 pent->plt.offset = (bfd_vma) -1;
10102 if (!doneone)
10103 {
10104 h->plt.plist = NULL;
10105 h->needs_plt = 0;
10106 }
65f38f15 10107 }
57e7d118 10108 else
65f38f15 10109 {
57e7d118
AM
10110 h->plt.plist = NULL;
10111 h->needs_plt = 0;
65f38f15
AM
10112 }
10113
0a1b45a2 10114 return true;
65f38f15
AM
10115}
10116
9e390558
AM
10117#define PPC_LO(v) ((v) & 0xffff)
10118#define PPC_HI(v) (((v) >> 16) & 0xffff)
10119#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
10120#define D34(v) \
10121 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10122#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 10123
a345bc8d
AM
10124/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10125 to set up space for global entry stubs. These are put in glink,
10126 after the branch table. */
65f38f15 10127
0a1b45a2 10128static bool
a345bc8d 10129size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10130{
a345bc8d
AM
10131 struct bfd_link_info *info;
10132 struct ppc_link_hash_table *htab;
10133 struct plt_entry *pent;
9e390558 10134 asection *s, *plt;
65f38f15 10135
a345bc8d 10136 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 10137 return true;
65f38f15 10138
a345bc8d 10139 if (!h->pointer_equality_needed)
0a1b45a2 10140 return true;
65f38f15 10141
a345bc8d 10142 if (h->def_regular)
0a1b45a2 10143 return true;
65f38f15 10144
a345bc8d
AM
10145 info = inf;
10146 htab = ppc_hash_table (info);
10147 if (htab == NULL)
0a1b45a2 10148 return false;
a345bc8d 10149
9e390558
AM
10150 s = htab->global_entry;
10151 plt = htab->elf.splt;
a345bc8d
AM
10152 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10153 if (pent->plt.offset != (bfd_vma) -1
10154 && pent->addend == 0)
10155 {
afe397ea
AM
10156 /* For ELFv2, if this symbol is not defined in a regular file
10157 and we are not generating a shared library or pie, then we
10158 need to define the symbol in the executable on a call stub.
10159 This is to avoid text relocations. */
9e390558
AM
10160 bfd_vma off, stub_align, stub_off, stub_size;
10161 unsigned int align_power;
10162
10163 stub_size = 16;
10164 stub_off = s->size;
10165 if (htab->params->plt_stub_align >= 0)
10166 align_power = htab->params->plt_stub_align;
10167 else
10168 align_power = -htab->params->plt_stub_align;
10169 /* Setting section alignment is delayed until we know it is
10170 non-empty. Otherwise the .text output section will be
10171 aligned at least to plt_stub_align even when no global
10172 entry stubs are needed. */
10173 if (s->alignment_power < align_power)
10174 s->alignment_power = align_power;
10175 stub_align = (bfd_vma) 1 << align_power;
10176 if (htab->params->plt_stub_align >= 0
10177 || ((((stub_off + stub_size - 1) & -stub_align)
10178 - (stub_off & -stub_align))
10179 > ((stub_size - 1) & -stub_align)))
10180 stub_off = (stub_off + stub_align - 1) & -stub_align;
10181 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10182 off -= stub_off + s->output_offset + s->output_section->vma;
10183 /* Note that for --plt-stub-align negative we have a possible
10184 dependency between stub offset and size. Break that
10185 dependency by assuming the max stub size when calculating
10186 the stub offset. */
10187 if (PPC_HA (off) == 0)
10188 stub_size -= 4;
8a2058b5 10189 h->root.type = bfd_link_hash_defined;
afe397ea 10190 h->root.u.def.section = s;
9e390558
AM
10191 h->root.u.def.value = stub_off;
10192 s->size = stub_off + stub_size;
a345bc8d
AM
10193 break;
10194 }
0a1b45a2 10195 return true;
a345bc8d
AM
10196}
10197
65f38f15
AM
10198/* Set the sizes of the dynamic sections. */
10199
0a1b45a2 10200static bool
ee67d69a 10201ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10202 struct bfd_link_info *info)
65f38f15
AM
10203{
10204 struct ppc_link_hash_table *htab;
10205 bfd *dynobj;
10206 asection *s;
0a1b45a2 10207 bool relocs;
65f38f15 10208 bfd *ibfd;
7865406b 10209 struct got_entry *first_tlsld;
65f38f15
AM
10210
10211 htab = ppc_hash_table (info);
4dfe6ac6 10212 if (htab == NULL)
0a1b45a2 10213 return false;
4dfe6ac6 10214
65f38f15
AM
10215 dynobj = htab->elf.dynobj;
10216 if (dynobj == NULL)
10217 abort ();
10218
10219 if (htab->elf.dynamic_sections_created)
10220 {
10221 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10222 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10223 {
3d4d4302 10224 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10225 if (s == NULL)
10226 abort ();
eea6121a 10227 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10228 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10229 }
10230 }
10231
10232 /* Set up .got offsets for local syms, and space for local dynamic
10233 relocs. */
c72f2fb2 10234 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10235 {
411e1bfb
AM
10236 struct got_entry **lgot_ents;
10237 struct got_entry **end_lgot_ents;
e054468f
AM
10238 struct plt_entry **local_plt;
10239 struct plt_entry **end_local_plt;
f961d9dd 10240 unsigned char *lgot_masks;
65f38f15
AM
10241 bfd_size_type locsymcount;
10242 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10243
0c8d6e5c 10244 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10245 continue;
10246
10247 for (s = ibfd->sections; s != NULL; s = s->next)
10248 {
1657026c 10249 struct ppc_local_dyn_relocs *p;
65f38f15 10250
6edfbbad 10251 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10252 {
ec338859
AM
10253 if (!bfd_is_abs_section (p->sec)
10254 && bfd_is_abs_section (p->sec->output_section))
10255 {
10256 /* Input section has been discarded, either because
10257 it is a copy of a linkonce section or due to
10258 linker script /DISCARD/, so we'll be discarding
10259 the relocs too. */
10260 }
248866a8 10261 else if (p->count != 0)
ec338859 10262 {
1657026c
AM
10263 unsigned int count;
10264 asection *srel;
10265
10266 count = p->count;
10267 if (info->enable_dt_relr)
10268 count -= p->rel_count;
10269 srel = elf_section_data (p->sec)->sreloc;
19e08130 10270 if (p->ifunc)
33e44f2e 10271 srel = htab->elf.irelplt;
1657026c 10272 srel->size += count * sizeof (Elf64_External_Rela);
248866a8
AM
10273 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10274 info->flags |= DF_TEXTREL;
ec338859 10275 }
65f38f15
AM
10276 }
10277 }
10278
411e1bfb
AM
10279 lgot_ents = elf_local_got_ents (ibfd);
10280 if (!lgot_ents)
65f38f15
AM
10281 continue;
10282
0ffa91dd 10283 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10284 locsymcount = symtab_hdr->sh_info;
411e1bfb 10285 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10286 local_plt = (struct plt_entry **) end_lgot_ents;
10287 end_local_plt = local_plt + locsymcount;
f961d9dd 10288 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10289 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10290 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10291 {
0b8bcf0d 10292 struct got_entry **pent, *ent;
411e1bfb 10293
0b8bcf0d
AM
10294 pent = lgot_ents;
10295 while ((ent = *pent) != NULL)
411e1bfb
AM
10296 if (ent->got.refcount > 0)
10297 {
e7b938ca 10298 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10299 {
927be08e 10300 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10301 *pent = ent->next;
411e1bfb
AM
10302 }
10303 else
10304 {
19e08130
AM
10305 unsigned int ent_size = 8;
10306 unsigned int rel_size = sizeof (Elf64_External_Rela);
10307
eea6121a 10308 ent->got.offset = s->size;
e7b938ca 10309 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10310 {
19e08130
AM
10311 ent_size *= 2;
10312 rel_size *= 2;
10313 }
10314 s->size += ent_size;
37da22e5 10315 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10316 {
33e44f2e 10317 htab->elf.irelplt->size += rel_size;
19e08130
AM
10318 htab->got_reli_size += rel_size;
10319 }
93370e8e 10320 else if (bfd_link_pic (info)
8f22c953
AM
10321 && (ent->tls_type == 0
10322 ? !info->enable_dt_relr
10323 : !bfd_link_executable (info)))
19e08130
AM
10324 {
10325 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10326 srel->size += rel_size;
927be08e 10327 }
0b8bcf0d 10328 pent = &ent->next;
411e1bfb
AM
10329 }
10330 }
10331 else
0b8bcf0d 10332 *pent = ent->next;
65f38f15 10333 }
e054468f 10334
2d7ad24e
AM
10335 /* Allocate space for plt calls to local syms. */
10336 lgot_masks = (unsigned char *) end_local_plt;
10337 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10338 {
10339 struct plt_entry *ent;
10340
10341 for (ent = *local_plt; ent != NULL; ent = ent->next)
10342 if (ent->plt.refcount > 0)
10343 {
2d7ad24e
AM
10344 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10345 {
10346 s = htab->elf.iplt;
10347 ent->plt.offset = s->size;
10348 s->size += PLT_ENTRY_SIZE (htab);
10349 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10350 }
3e04d765
AM
10351 else if (htab->can_convert_all_inline_plt
10352 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10353 ent->plt.offset = (bfd_vma) -1;
10354 else
10355 {
10356 s = htab->pltlocal;
10357 ent->plt.offset = s->size;
10358 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10359 if (bfd_link_pic (info))
10360 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10361 }
e054468f
AM
10362 }
10363 else
10364 ent->plt.offset = (bfd_vma) -1;
10365 }
65f38f15
AM
10366 }
10367
10368 /* Allocate global sym .plt and .got entries, and space for global
10369 sym dynamic relocs. */
4ce794b7 10370 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10371
0e1862bb 10372 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10373 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10374
7865406b 10375 first_tlsld = NULL;
c72f2fb2 10376 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10377 {
7865406b
AM
10378 struct got_entry *ent;
10379
0c8d6e5c 10380 if (!is_ppc64_elf (ibfd))
102890f0
AM
10381 continue;
10382
7865406b
AM
10383 ent = ppc64_tlsld_got (ibfd);
10384 if (ent->got.refcount > 0)
102890f0 10385 {
7865406b 10386 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10387 {
0a1b45a2 10388 ent->is_indirect = true;
7865406b
AM
10389 ent->got.ent = first_tlsld;
10390 }
10391 else
10392 {
10393 if (first_tlsld == NULL)
10394 first_tlsld = ent;
10395 s = ppc64_elf_tdata (ibfd)->got;
10396 ent->got.offset = s->size;
10397 ent->owner = ibfd;
10398 s->size += 16;
f749f26e 10399 if (bfd_link_dll (info))
7865406b
AM
10400 {
10401 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10402 srel->size += sizeof (Elf64_External_Rela);
10403 }
102890f0
AM
10404 }
10405 }
10406 else
7865406b 10407 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10408 }
10409
65f38f15
AM
10410 /* We now have determined the sizes of the various dynamic sections.
10411 Allocate memory for them. */
0a1b45a2 10412 relocs = false;
65f38f15
AM
10413 for (s = dynobj->sections; s != NULL; s = s->next)
10414 {
10415 if ((s->flags & SEC_LINKER_CREATED) == 0)
10416 continue;
10417
1657026c 10418 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
721956f4
AM
10419 /* These haven't been allocated yet; don't strip. */
10420 continue;
33e44f2e
AM
10421 else if (s == htab->elf.sgot
10422 || s == htab->elf.splt
10423 || s == htab->elf.iplt
2d7ad24e 10424 || s == htab->pltlocal
c456f082 10425 || s == htab->glink
9e390558 10426 || s == htab->global_entry
5474d94f
AM
10427 || s == htab->elf.sdynbss
10428 || s == htab->elf.sdynrelro)
65f38f15
AM
10429 {
10430 /* Strip this section if we don't need it; see the
10431 comment below. */
5bd4f169 10432 }
58d180e8
AM
10433 else if (s == htab->glink_eh_frame)
10434 {
10435 if (!bfd_is_abs_section (s->output_section))
10436 /* Not sized yet. */
10437 continue;
10438 }
08dedd66 10439 else if (startswith (s->name, ".rela"))
5bd4f169 10440 {
c456f082 10441 if (s->size != 0)
5bd4f169 10442 {
33e44f2e 10443 if (s != htab->elf.srelplt)
0a1b45a2 10444 relocs = true;
5bd4f169
AM
10445
10446 /* We use the reloc_count field as a counter if we need
10447 to copy relocs into the output file. */
10448 s->reloc_count = 0;
10449 }
10450 }
65f38f15 10451 else
5bd4f169
AM
10452 {
10453 /* It's not one of our sections, so don't allocate space. */
10454 continue;
10455 }
10456
eea6121a 10457 if (s->size == 0)
5bd4f169 10458 {
c456f082
AM
10459 /* If we don't need this section, strip it from the
10460 output file. This is mostly to handle .rela.bss and
10461 .rela.plt. We must create both sections in
10462 create_dynamic_sections, because they must be created
10463 before the linker maps input sections to output
10464 sections. The linker does that before
10465 adjust_dynamic_symbol is called, and it is that
10466 function which decides whether anything needs to go
10467 into these sections. */
8423293d 10468 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10469 continue;
10470 }
10471
06bcf541
AM
10472 if (bfd_is_abs_section (s->output_section))
10473 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10474 s->name);
10475
c456f082 10476 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10477 continue;
10478
65f38f15
AM
10479 /* Allocate memory for the section contents. We use bfd_zalloc
10480 here in case unused entries are not reclaimed before the
10481 section's contents are written out. This should not happen,
411e1bfb
AM
10482 but this way if it does we get a R_PPC64_NONE reloc in .rela
10483 sections instead of garbage.
10484 We also rely on the section contents being zero when writing
5474d94f 10485 the GOT and .dynrelro. */
eea6121a 10486 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10487 if (s->contents == NULL)
0a1b45a2 10488 return false;
5bd4f169
AM
10489 }
10490
c72f2fb2 10491 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10492 {
0c8d6e5c 10493 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10494 continue;
10495
e717da7e 10496 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10497 if (s != NULL && s != htab->elf.sgot)
e717da7e 10498 {
eea6121a 10499 if (s->size == 0)
8423293d 10500 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10501 else
10502 {
eea6121a 10503 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10504 if (s->contents == NULL)
0a1b45a2 10505 return false;
e717da7e
AM
10506 }
10507 }
10508 s = ppc64_elf_tdata (ibfd)->relgot;
10509 if (s != NULL)
10510 {
eea6121a 10511 if (s->size == 0)
8423293d 10512 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10513 else
10514 {
eea6121a 10515 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10516 if (s->contents == NULL)
0a1b45a2
AM
10517 return false;
10518 relocs = true;
e717da7e
AM
10519 s->reloc_count = 0;
10520 }
10521 }
10522 }
10523
e86ce104 10524 if (htab->elf.dynamic_sections_created)
5bd4f169 10525 {
0a1b45a2 10526 bool tls_opt;
e8910a83 10527
5bd4f169
AM
10528 /* Add some entries to the .dynamic section. We fill in the
10529 values later, in ppc64_elf_finish_dynamic_sections, but we
10530 must add the entries now so that we get the correct size for
10531 the .dynamic section. The DT_DEBUG entry is filled in by the
10532 dynamic linker and used by the debugger. */
dc810e39 10533#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10534 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10535
0e1862bb 10536 if (bfd_link_executable (info))
5bd4f169 10537 {
dc810e39 10538 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 10539 return false;
5bd4f169
AM
10540 }
10541
33e44f2e 10542 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10543 {
dc810e39
AM
10544 if (!add_dynamic_entry (DT_PLTGOT, 0)
10545 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10546 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10547 || !add_dynamic_entry (DT_JMPREL, 0)
10548 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
0a1b45a2 10549 return false;
5bd4f169
AM
10550 }
10551
ee67d69a 10552 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10553 {
10554 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10555 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
0a1b45a2 10556 return false;
19397422
AM
10557 }
10558
7c9cf415 10559 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10560 && ((htab->tls_get_addr_fd != NULL
10561 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10562 || (htab->tga_desc_fd != NULL
10563 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10564 if (tls_opt || !htab->opd_abi)
10565 {
10566 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
0a1b45a2 10567 return false;
e8910a83 10568 }
a7f2871e 10569
5bd4f169
AM
10570 if (relocs)
10571 {
dc810e39
AM
10572 if (!add_dynamic_entry (DT_RELA, 0)
10573 || !add_dynamic_entry (DT_RELASZ, 0)
10574 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
0a1b45a2 10575 return false;
5bd4f169 10576
65f38f15
AM
10577 /* If any dynamic relocs apply to a read-only section,
10578 then we need a DT_TEXTREL entry. */
248866a8 10579 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10580 elf_link_hash_traverse (&htab->elf,
10581 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10582
65f38f15 10583 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10584 {
65f38f15 10585 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 10586 return false;
5bd4f169 10587 }
5bd4f169 10588 }
5bd4f169 10589 }
65f38f15 10590#undef add_dynamic_entry
5bd4f169 10591
0a1b45a2 10592 return true;
5bd4f169
AM
10593}
10594
a345bc8d
AM
10595/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10596
0a1b45a2 10597static bool
a345bc8d
AM
10598ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10599{
10600 if (h->plt.plist != NULL
10601 && !h->def_regular
10602 && !h->pointer_equality_needed)
0a1b45a2 10603 return false;
a345bc8d
AM
10604
10605 return _bfd_elf_hash_symbol (h);
10606}
10607
721956f4 10608/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10609
7aba54da 10610static inline enum ppc_stub_main_type
4ce794b7
AM
10611ppc_type_of_stub (asection *input_sec,
10612 const Elf_Internal_Rela *rel,
10613 struct ppc_link_hash_entry **hash,
e054468f 10614 struct plt_entry **plt_ent,
6911b7dc
AM
10615 bfd_vma destination,
10616 unsigned long local_off)
5bd4f169 10617{
721956f4
AM
10618 struct ppc_link_hash_entry *h = *hash;
10619 bfd_vma location;
10620 bfd_vma branch_offset;
10621 bfd_vma max_branch_offset;
4ce794b7 10622 enum elf_ppc64_reloc_type r_type;
5bd4f169 10623
721956f4
AM
10624 if (h != NULL)
10625 {
e054468f 10626 struct plt_entry *ent;
7fe2b9a6 10627 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10628 if (h->oh != NULL
10629 && h->oh->is_func_descriptor)
7b8f6675
AM
10630 {
10631 fdh = ppc_follow_link (h->oh);
10632 *hash = fdh;
10633 }
8387904d 10634
e054468f
AM
10635 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10636 if (ent->addend == rel->r_addend
10637 && ent->plt.offset != (bfd_vma) -1)
10638 {
e054468f
AM
10639 *plt_ent = ent;
10640 return ppc_stub_plt_call;
10641 }
5bd4f169 10642
7fe2b9a6
AM
10643 /* Here, we know we don't have a plt entry. If we don't have a
10644 either a defined function descriptor or a defined entry symbol
10645 in a regular object file, then it is pointless trying to make
10646 any other type of stub. */
854b41e7
AM
10647 if (!is_static_defined (&fdh->elf)
10648 && !is_static_defined (&h->elf))
721956f4 10649 return ppc_stub_none;
5d1634d7 10650 }
e054468f
AM
10651 else if (elf_local_got_ents (input_sec->owner) != NULL)
10652 {
10653 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10654 struct plt_entry **local_plt = (struct plt_entry **)
10655 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10656 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10657
10658 if (local_plt[r_symndx] != NULL)
10659 {
10660 struct plt_entry *ent;
10661
10662 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10663 if (ent->addend == rel->r_addend
10664 && ent->plt.offset != (bfd_vma) -1)
10665 {
10666 *plt_ent = ent;
10667 return ppc_stub_plt_call;
10668 }
10669 }
10670 }
5d1634d7 10671
721956f4
AM
10672 /* Determine where the call point is. */
10673 location = (input_sec->output_offset
10674 + input_sec->output_section->vma
10675 + rel->r_offset);
5d1634d7 10676
721956f4
AM
10677 branch_offset = destination - location;
10678 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10679
721956f4
AM
10680 /* Determine if a long branch stub is needed. */
10681 max_branch_offset = 1 << 25;
23cedd1d
AM
10682 if (r_type == R_PPC64_REL14
10683 || r_type == R_PPC64_REL14_BRTAKEN
10684 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10685 max_branch_offset = 1 << 15;
5d1634d7 10686
6911b7dc 10687 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10688 /* We need a stub. Figure out whether a long_branch or plt_branch
10689 is needed later. */
10690 return ppc_stub_long_branch;
5d1634d7 10691
721956f4 10692 return ppc_stub_none;
5d1634d7
AM
10693}
10694
f891966f
AM
10695/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10696 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10697 . mflr %r12
10698 . bcl 20,31,1f
10699 .1: mflr %r11
10700 . mtlr %r12
05d0e962 10701 . lis %r12,xxx-1b@highest
f891966f 10702 . ori %r12,%r12,xxx-1b@higher
05d0e962 10703 . sldi %r12,%r12,32
f891966f 10704 . oris %r12,%r12,xxx-1b@high
05d0e962 10705 . ori %r12,%r12,xxx-1b@l
f891966f 10706 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10707
10708static bfd_byte *
0a1b45a2 10709build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
05d0e962 10710{
f891966f
AM
10711 bfd_put_32 (abfd, MFLR_R12, p);
10712 p += 4;
10713 bfd_put_32 (abfd, BCL_20_31, p);
10714 p += 4;
10715 bfd_put_32 (abfd, MFLR_R11, p);
10716 p += 4;
10717 bfd_put_32 (abfd, MTLR_R12, p);
10718 p += 4;
05d0e962
AM
10719 if (off + 0x8000 < 0x10000)
10720 {
10721 if (load)
10722 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10723 else
10724 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10725 p += 4;
10726 }
10727 else if (off + 0x80008000ULL < 0x100000000ULL)
10728 {
10729 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10730 p += 4;
10731 if (load)
10732 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10733 else
10734 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10735 p += 4;
10736 }
10737 else
10738 {
10739 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10740 {
10741 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10742 p += 4;
10743 }
10744 else
10745 {
10746 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10747 p += 4;
10748 if (((off >> 32) & 0xffff) != 0)
10749 {
10750 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10751 p += 4;
10752 }
10753 }
10754 if (((off >> 32) & 0xffffffffULL) != 0)
10755 {
10756 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10757 p += 4;
10758 }
10759 if (PPC_HI (off) != 0)
10760 {
10761 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10762 p += 4;
10763 }
10764 if (PPC_LO (off) != 0)
10765 {
10766 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10767 p += 4;
10768 }
10769 if (load)
10770 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10771 else
10772 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10773 p += 4;
10774 }
10775 return p;
10776}
10777
10778static unsigned int
10779size_offset (bfd_vma off)
10780{
10781 unsigned int size;
10782 if (off + 0x8000 < 0x10000)
10783 size = 4;
10784 else if (off + 0x80008000ULL < 0x100000000ULL)
10785 size = 8;
10786 else
10787 {
10788 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10789 size = 4;
10790 else
10791 {
10792 size = 4;
10793 if (((off >> 32) & 0xffff) != 0)
10794 size += 4;
10795 }
10796 if (((off >> 32) & 0xffffffffULL) != 0)
10797 size += 4;
10798 if (PPC_HI (off) != 0)
10799 size += 4;
10800 if (PPC_LO (off) != 0)
10801 size += 4;
10802 size += 4;
10803 }
f891966f 10804 return size + 16;
05d0e962
AM
10805}
10806
3d58e1fc
AM
10807static unsigned int
10808num_relocs_for_offset (bfd_vma off)
10809{
10810 unsigned int num_rel;
10811 if (off + 0x8000 < 0x10000)
10812 num_rel = 1;
10813 else if (off + 0x80008000ULL < 0x100000000ULL)
10814 num_rel = 2;
10815 else
10816 {
10817 num_rel = 1;
10818 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10819 && ((off >> 32) & 0xffff) != 0)
10820 num_rel += 1;
10821 if (PPC_HI (off) != 0)
10822 num_rel += 1;
10823 if (PPC_LO (off) != 0)
10824 num_rel += 1;
10825 }
10826 return num_rel;
10827}
10828
10829static Elf_Internal_Rela *
10830emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10831 bfd_vma roff, bfd_vma targ, bfd_vma off)
10832{
10833 bfd_vma relative_targ = targ - (roff - 8);
10834 if (bfd_big_endian (info->output_bfd))
10835 roff += 2;
10836 r->r_offset = roff;
10837 r->r_addend = relative_targ + roff;
10838 if (off + 0x8000 < 0x10000)
10839 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10840 else if (off + 0x80008000ULL < 0x100000000ULL)
10841 {
10842 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10843 ++r;
10844 roff += 4;
10845 r->r_offset = roff;
10846 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10847 r->r_addend = relative_targ + roff;
10848 }
10849 else
10850 {
10851 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10852 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10853 else
10854 {
10855 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10856 if (((off >> 32) & 0xffff) != 0)
10857 {
10858 ++r;
10859 roff += 4;
10860 r->r_offset = roff;
10861 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10862 r->r_addend = relative_targ + roff;
10863 }
10864 }
10865 if (((off >> 32) & 0xffffffffULL) != 0)
10866 roff += 4;
10867 if (PPC_HI (off) != 0)
10868 {
10869 ++r;
10870 roff += 4;
10871 r->r_offset = roff;
10872 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10873 r->r_addend = relative_targ + roff;
10874 }
10875 if (PPC_LO (off) != 0)
10876 {
10877 ++r;
10878 roff += 4;
10879 r->r_offset = roff;
10880 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10881 r->r_addend = relative_targ + roff;
10882 }
10883 }
10884 return r;
10885}
10886
04bdff6a 10887static bfd_byte *
7c1f4227 10888build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
0a1b45a2 10889 bool load)
04bdff6a
AM
10890{
10891 uint64_t insn;
10892 if (off - odd + (1ULL << 33) < 1ULL << 34)
10893 {
10894 off -= odd;
10895 if (odd)
10896 {
10897 bfd_put_32 (abfd, NOP, p);
10898 p += 4;
10899 }
10900 if (load)
10901 insn = PLD_R12_PC;
10902 else
10903 insn = PADDI_R12_PC;
10904 insn |= D34 (off);
10905 bfd_put_32 (abfd, insn >> 32, p);
10906 p += 4;
10907 bfd_put_32 (abfd, insn, p);
10908 }
10909 /* The minimum value for paddi is -0x200000000. The minimum value
10910 for li is -0x8000, which when shifted by 34 and added gives a
10911 minimum value of -0x2000200000000. The maximum value is
10912 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10913 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10914 {
10915 off -= 8 - odd;
10916 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10917 p += 4;
10918 if (!odd)
10919 {
10920 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10921 p += 4;
10922 }
10923 insn = PADDI_R12_PC | D34 (off);
10924 bfd_put_32 (abfd, insn >> 32, p);
10925 p += 4;
10926 bfd_put_32 (abfd, insn, p);
10927 p += 4;
10928 if (odd)
10929 {
10930 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10931 p += 4;
10932 }
10933 if (load)
10934 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10935 else
10936 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10937 }
10938 else
10939 {
10940 off -= odd + 8;
10941 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10942 p += 4;
10943 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10944 p += 4;
10945 if (odd)
10946 {
10947 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10948 p += 4;
10949 }
10950 insn = PADDI_R12_PC | D34 (off);
10951 bfd_put_32 (abfd, insn >> 32, p);
10952 p += 4;
10953 bfd_put_32 (abfd, insn, p);
10954 p += 4;
10955 if (!odd)
10956 {
10957 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10958 p += 4;
10959 }
10960 if (load)
10961 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10962 else
10963 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10964 }
10965 p += 4;
10966 return p;
10967}
10968
10969static unsigned int
7c1f4227 10970size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10971{
10972 if (off - odd + (1ULL << 33) < 1ULL << 34)
10973 return odd + 8;
10974 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10975 return 20;
10976 else
10977 return 24;
10978}
10979
10980static unsigned int
7c1f4227 10981num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10982{
10983 if (off - odd + (1ULL << 33) < 1ULL << 34)
10984 return 1;
10985 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10986 return 2;
10987 else
10988 return 3;
10989}
10990
10991static Elf_Internal_Rela *
7c1f4227 10992emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
10993 Elf_Internal_Rela *r, bfd_vma roff,
10994 bfd_vma targ, bfd_vma off, int odd)
10995{
10996 if (off - odd + (1ULL << 33) < 1ULL << 34)
10997 roff += odd;
10998 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10999 {
11000 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11001 r->r_offset = roff + d_offset;
11002 r->r_addend = targ + 8 - odd - d_offset;
11003 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11004 ++r;
11005 roff += 8 - odd;
11006 }
11007 else
11008 {
11009 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11010 r->r_offset = roff + d_offset;
11011 r->r_addend = targ + 8 + odd - d_offset;
11012 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11013 ++r;
11014 roff += 4;
11015 r->r_offset = roff + d_offset;
11016 r->r_addend = targ + 4 + odd - d_offset;
11017 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11018 ++r;
11019 roff += 4 + odd;
11020 }
11021 r->r_offset = roff;
11022 r->r_addend = targ;
11023 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11024 return r;
11025}
11026
df136d64
AM
11027/* Emit .eh_frame opcode to advance pc by DELTA. */
11028
11029static bfd_byte *
11030eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11031{
11032 delta /= 4;
11033 if (delta < 64)
11034 *eh++ = DW_CFA_advance_loc + delta;
11035 else if (delta < 256)
11036 {
11037 *eh++ = DW_CFA_advance_loc1;
11038 *eh++ = delta;
11039 }
11040 else if (delta < 65536)
11041 {
11042 *eh++ = DW_CFA_advance_loc2;
11043 bfd_put_16 (abfd, delta, eh);
11044 eh += 2;
11045 }
11046 else
11047 {
11048 *eh++ = DW_CFA_advance_loc4;
11049 bfd_put_32 (abfd, delta, eh);
11050 eh += 4;
11051 }
11052 return eh;
11053}
11054
11055/* Size of required .eh_frame opcode to advance pc by DELTA. */
11056
11057static unsigned int
11058eh_advance_size (unsigned int delta)
11059{
11060 if (delta < 64 * 4)
11061 /* DW_CFA_advance_loc+[1..63]. */
11062 return 1;
11063 if (delta < 256 * 4)
11064 /* DW_CFA_advance_loc1, byte. */
11065 return 2;
11066 if (delta < 65536 * 4)
11067 /* DW_CFA_advance_loc2, 2 bytes. */
11068 return 3;
11069 /* DW_CFA_advance_loc4, 4 bytes. */
11070 return 5;
11071}
11072
794e51c0
AM
11073/* With power7 weakly ordered memory model, it is possible for ld.so
11074 to update a plt entry in one thread and have another thread see a
11075 stale zero toc entry. To avoid this we need some sort of acquire
11076 barrier in the call stub. One solution is to make the load of the
11077 toc word seem to appear to depend on the load of the function entry
11078 word. Another solution is to test for r2 being zero, and branch to
11079 the appropriate glink entry if so.
11080
11081 . fake dep barrier compare
71a39c98
AM
11082 . ld 12,xxx(2) ld 12,xxx(2)
11083 . mtctr 12 mtctr 12
11084 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
11085 . add 2,2,11 cmpldi 2,0
11086 . ld 2,xxx+8(2) bnectr+
11087 . bctr b <glink_entry>
11088
11089 The solution involving the compare turns out to be faster, so
11090 that's what we use unless the branch won't reach. */
11091
11092#define ALWAYS_USE_FAKE_DEP 0
11093#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 11094
794e51c0
AM
11095static inline unsigned int
11096plt_stub_size (struct ppc_link_hash_table *htab,
11097 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
11098 bfd_vma off,
11099 unsigned int odd)
794e51c0 11100{
05d0e962 11101 unsigned size;
b9e5796b 11102
7aba54da 11103 if (stub_entry->type.sub == ppc_stub_notoc)
05d0e962 11104 {
7aba54da
AM
11105 size = 8 + size_power10_offset (off, odd);
11106 if (stub_entry->type.r2save)
11107 size += 4;
11108 }
11109 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11110 {
11111 size = 8 + size_offset (off - 8);
11112 if (stub_entry->type.r2save)
05d0e962 11113 size += 4;
05d0e962 11114 }
29433886 11115 else
b9e5796b 11116 {
29433886 11117 size = 12;
7aba54da 11118 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
b9e5796b 11119 size += 4;
29433886 11120 if (PPC_HA (off) != 0)
b9e5796b 11121 size += 4;
29433886
AM
11122 if (htab->opd_abi)
11123 {
11124 size += 4;
11125 if (htab->params->plt_static_chain)
11126 size += 4;
11127 if (htab->params->plt_thread_safe
11128 && htab->elf.dynamic_sections_created
11129 && stub_entry->h != NULL
11130 && stub_entry->h->elf.dynindx != -1)
11131 size += 8;
11132 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11133 != PPC_HA (off))
11134 size += 4;
11135 }
b9e5796b 11136 }
794e51c0 11137 if (stub_entry->h != NULL
ed7007c1 11138 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11139 && htab->params->tls_get_addr_opt)
f378ab09 11140 {
29433886 11141 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11142 {
29433886 11143 size += 30 * 4;
7aba54da 11144 if (stub_entry->type.r2save)
29433886 11145 size += 4;
9e7028aa
AM
11146 }
11147 else
11148 {
29433886 11149 size += 7 * 4;
7aba54da 11150 if (stub_entry->type.r2save)
29433886 11151 size += 6 * 4;
9e7028aa 11152 }
f378ab09 11153 }
794e51c0
AM
11154 return size;
11155}
11156
2420fff6
AM
11157/* Depending on the sign of plt_stub_align:
11158 If positive, return the padding to align to a 2**plt_stub_align
11159 boundary.
11160 If negative, if this stub would cross fewer 2**plt_stub_align
11161 boundaries if we align, then return the padding needed to do so. */
11162
794e51c0
AM
11163static inline unsigned int
11164plt_stub_pad (struct ppc_link_hash_table *htab,
11165 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
11166 bfd_vma plt_off,
11167 unsigned int odd)
794e51c0 11168{
2420fff6 11169 int stub_align;
1aa42141 11170 unsigned stub_size;
6f20ed8a 11171 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 11172
2420fff6
AM
11173 if (htab->params->plt_stub_align >= 0)
11174 {
11175 stub_align = 1 << htab->params->plt_stub_align;
11176 if ((stub_off & (stub_align - 1)) != 0)
11177 return stub_align - (stub_off & (stub_align - 1));
11178 return 0;
11179 }
11180
11181 stub_align = 1 << -htab->params->plt_stub_align;
29433886 11182 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 11183 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 11184 > ((stub_size - 1) & -stub_align))
794e51c0
AM
11185 return stub_align - (stub_off & (stub_align - 1));
11186 return 0;
11187}
11188
7aba54da 11189/* Build a toc using .plt call stub. */
794e51c0
AM
11190
11191static inline bfd_byte *
11192build_plt_stub (struct ppc_link_hash_table *htab,
11193 struct ppc_stub_hash_entry *stub_entry,
11194 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11195{
e7d1c40c 11196 bfd *obfd = htab->params->stub_bfd;
0a1b45a2
AM
11197 bool plt_load_toc = htab->opd_abi;
11198 bool plt_static_chain = htab->params->plt_static_chain;
11199 bool plt_thread_safe = (htab->params->plt_thread_safe
11200 && htab->elf.dynamic_sections_created
11201 && stub_entry->h != NULL
11202 && stub_entry->h->elf.dynindx != -1);
11203 bool use_fake_dep = plt_thread_safe;
794e51c0
AM
11204 bfd_vma cmp_branch_off = 0;
11205
11206 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11207 && plt_load_toc
794e51c0 11208 && plt_thread_safe
9e51d549
AM
11209 && !(stub_entry->h != NULL
11210 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11211 && htab->params->tls_get_addr_opt))
794e51c0
AM
11212 {
11213 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11214 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11215 / PLT_ENTRY_SIZE (htab));
9e390558 11216 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11217 bfd_vma to, from;
11218
68d62958
AM
11219 if (pltindex > 32768)
11220 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11221 to = (glinkoff
11222 + htab->glink->output_offset
11223 + htab->glink->output_section->vma);
6f20ed8a 11224 from = (p - stub_entry->group->stub_sec->contents
7aba54da 11225 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0
AM
11226 + 4 * (PPC_HA (offset) != 0)
11227 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11228 != PPC_HA (offset))
11229 + 4 * (plt_static_chain != 0)
11230 + 20
6f20ed8a
AM
11231 + stub_entry->group->stub_sec->output_offset
11232 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11233 cmp_branch_off = to - from;
11234 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11235 }
11236
ac2df442
AM
11237 if (PPC_HA (offset) != 0)
11238 {
176a0d42
AM
11239 if (r != NULL)
11240 {
7aba54da 11241 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11242 r[0].r_offset += 4;
176a0d42 11243 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11244 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11245 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11246 r[1].r_addend = r[0].r_addend;
b9e5796b 11247 if (plt_load_toc)
176a0d42 11248 {
b9e5796b 11249 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11250 {
b9e5796b
AM
11251 r[2].r_offset = r[1].r_offset + 4;
11252 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11253 r[2].r_addend = r[0].r_addend;
11254 }
11255 else
11256 {
11257 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11258 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11259 r[2].r_addend = r[0].r_addend + 8;
11260 if (plt_static_chain)
11261 {
11262 r[3].r_offset = r[2].r_offset + 4;
11263 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11264 r[3].r_addend = r[0].r_addend + 16;
11265 }
c7131b65 11266 }
176a0d42
AM
11267 }
11268 }
7aba54da 11269 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11270 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11271 if (plt_load_toc)
11272 {
11273 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11274 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11275 }
11276 else
11277 {
11278 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11279 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11280 }
b9e5796b
AM
11281 if (plt_load_toc
11282 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11283 {
71a39c98 11284 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11285 offset = 0;
11286 }
71a39c98 11287 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11288 if (plt_load_toc)
794e51c0 11289 {
b9e5796b
AM
11290 if (use_fake_dep)
11291 {
11292 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11293 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11294 }
11295 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11296 if (plt_static_chain)
11297 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11298 }
ac2df442
AM
11299 }
11300 else
11301 {
176a0d42
AM
11302 if (r != NULL)
11303 {
7aba54da 11304 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11305 r[0].r_offset += 4;
176a0d42 11306 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11307 if (plt_load_toc)
176a0d42 11308 {
b9e5796b 11309 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11310 {
b9e5796b
AM
11311 r[1].r_offset = r[0].r_offset + 4;
11312 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11313 r[1].r_addend = r[0].r_addend;
11314 }
11315 else
11316 {
11317 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11318 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11319 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11320 if (plt_static_chain)
11321 {
11322 r[2].r_offset = r[1].r_offset + 4;
11323 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11324 r[2].r_addend = r[0].r_addend + 8;
11325 }
c7131b65 11326 }
176a0d42
AM
11327 }
11328 }
7aba54da 11329 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11330 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11331 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11332 if (plt_load_toc
11333 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11334 {
11335 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11336 offset = 0;
11337 }
71a39c98 11338 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11339 if (plt_load_toc)
794e51c0 11340 {
b9e5796b
AM
11341 if (use_fake_dep)
11342 {
11343 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11344 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11345 }
11346 if (plt_static_chain)
11347 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11348 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11349 }
ac2df442 11350 }
b9e5796b 11351 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11352 {
11353 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11354 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11355 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11356 }
11357 else
407aa07c 11358 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11359 return p;
11360}
11361
a7f2871e
AM
11362/* Build a special .plt call stub for __tls_get_addr. */
11363
b9ca1af6 11364#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11365#define LD_R12_0R3 0xe9830000
11366#define MR_R0_R3 0x7c601b78
b9ca1af6 11367#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11368#define ADD_R3_R12_R13 0x7c6c6a14
11369#define BEQLR 0x4d820020
11370#define MR_R3_R0 0x7c030378
a7f2871e 11371#define BCTRL 0x4e800421
a7f2871e 11372
29433886
AM
11373static bfd_byte *
11374build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11375 struct ppc_stub_hash_entry *stub_entry,
29433886 11376 bfd_byte *p)
a7f2871e 11377{
e7d1c40c 11378 bfd *obfd = htab->params->stub_bfd;
794e51c0 11379
b9ca1af6 11380 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11381 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11382 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11383 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11384 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11385 bfd_put_32 (obfd, BEQLR, p), p += 4;
11386 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11387
29433886
AM
11388 if (!htab->params->no_tls_get_addr_regsave)
11389 p = tls_get_addr_prologue (obfd, p, htab);
7aba54da 11390 else if (stub_entry->type.r2save)
29433886 11391 {
9e7028aa
AM
11392 bfd_put_32 (obfd, MFLR_R0, p);
11393 p += 4;
11394 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11395 p += 4;
29433886
AM
11396 }
11397 return p;
11398}
9e7028aa 11399
29433886
AM
11400static bfd_byte *
11401build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11402 struct ppc_stub_hash_entry *stub_entry,
11403 bfd_byte *p,
11404 bfd_byte *loc)
11405{
11406 bfd *obfd = htab->params->stub_bfd;
11407
11408 if (!htab->params->no_tls_get_addr_regsave)
11409 {
11410 bfd_put_32 (obfd, BCTRL, p - 4);
11411
7aba54da 11412 if (stub_entry->type.r2save)
29433886
AM
11413 {
11414 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11415 p += 4;
11416 }
11417 p = tls_get_addr_epilogue (obfd, p, htab);
11418 }
7aba54da 11419 else if (stub_entry->type.r2save)
29433886 11420 {
9e7028aa
AM
11421 bfd_put_32 (obfd, BCTRL, p - 4);
11422
11423 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11424 p += 4;
11425 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11426 p += 4;
11427 bfd_put_32 (obfd, MTLR_R0, p);
11428 p += 4;
11429 bfd_put_32 (obfd, BLR, p);
11430 p += 4;
11431 }
a7f2871e 11432
df136d64
AM
11433 if (htab->glink_eh_frame != NULL
11434 && htab->glink_eh_frame->size != 0)
11435 {
11436 bfd_byte *base, *eh;
df136d64
AM
11437
11438 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11439 eh = base + stub_entry->group->eh_size;
29433886
AM
11440
11441 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11442 {
29433886
AM
11443 unsigned int cfa_updt, delta, i;
11444
9e7028aa
AM
11445 /* After the bctrl, lr has been modified so we need to emit
11446 .eh_frame info saying the return address is on the stack. In
11447 fact we must put the EH info at or before the call rather
11448 than after it, because the EH info for a call needs to be
11449 specified by that point.
11450 See libgcc/unwind-dw2.c execute_cfa_program.
11451 Any stack pointer update must be described immediately after
11452 the instruction making the change, and since the stdu occurs
11453 after saving regs we put all the reg saves and the cfa
11454 change there. */
11455 cfa_updt = stub_entry->stub_offset + 18 * 4;
11456 delta = cfa_updt - stub_entry->group->lr_restore;
11457 stub_entry->group->lr_restore
11458 = stub_entry->stub_offset + (p - loc) - 4;
11459 eh = eh_advance (htab->elf.dynobj, eh, delta);
11460 *eh++ = DW_CFA_def_cfa_offset;
11461 if (htab->opd_abi)
11462 {
11463 *eh++ = 128;
11464 *eh++ = 1;
11465 }
11466 else
11467 *eh++ = 96;
11468 *eh++ = DW_CFA_offset_extended_sf;
11469 *eh++ = 65;
11470 *eh++ = (-16 / 8) & 0x7f;
11471 for (i = 4; i < 12; i++)
11472 {
11473 *eh++ = DW_CFA_offset + i;
11474 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11475 }
11476 *eh++ = (DW_CFA_advance_loc
11477 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11478 *eh++ = DW_CFA_def_cfa_offset;
11479 *eh++ = 0;
11480 for (i = 4; i < 12; i++)
11481 *eh++ = DW_CFA_restore + i;
11482 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11483 *eh++ = DW_CFA_restore_extended;
11484 *eh++ = 65;
11485 stub_entry->group->eh_size = eh - base;
11486 }
7aba54da 11487 else if (stub_entry->type.r2save)
29433886
AM
11488 {
11489 unsigned int lr_used, delta;
11490
11491 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11492 delta = lr_used - stub_entry->group->lr_restore;
11493 stub_entry->group->lr_restore = lr_used + 16;
11494 eh = eh_advance (htab->elf.dynobj, eh, delta);
11495 *eh++ = DW_CFA_offset_extended_sf;
11496 *eh++ = 65;
11497 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11498 *eh++ = DW_CFA_advance_loc + 4;
11499 *eh++ = DW_CFA_restore_extended;
11500 *eh++ = 65;
11501 stub_entry->group->eh_size = eh - base;
9e7028aa 11502 }
df136d64 11503 }
a7f2871e
AM
11504 return p;
11505}
11506
176a0d42
AM
11507static Elf_Internal_Rela *
11508get_relocs (asection *sec, int count)
11509{
11510 Elf_Internal_Rela *relocs;
11511 struct bfd_elf_section_data *elfsec_data;
11512
11513 elfsec_data = elf_section_data (sec);
11514 relocs = elfsec_data->relocs;
11515 if (relocs == NULL)
11516 {
11517 bfd_size_type relsize;
11518 relsize = sec->reloc_count * sizeof (*relocs);
11519 relocs = bfd_alloc (sec->owner, relsize);
11520 if (relocs == NULL)
11521 return NULL;
11522 elfsec_data->relocs = relocs;
d4730f92
BS
11523 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11524 sizeof (Elf_Internal_Shdr));
11525 if (elfsec_data->rela.hdr == NULL)
11526 return NULL;
11527 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11528 * sizeof (Elf64_External_Rela));
11529 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11530 sec->reloc_count = 0;
11531 }
11532 relocs += sec->reloc_count;
11533 sec->reloc_count += count;
11534 return relocs;
11535}
11536
3d58e1fc
AM
11537/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11538 forms, to the equivalent relocs against the global symbol given by
11539 STUB_ENTRY->H. */
11540
0a1b45a2 11541static bool
3d58e1fc
AM
11542use_global_in_relocs (struct ppc_link_hash_table *htab,
11543 struct ppc_stub_hash_entry *stub_entry,
11544 Elf_Internal_Rela *r, unsigned int num_rel)
11545{
11546 struct elf_link_hash_entry **hashes;
11547 unsigned long symndx;
11548 struct ppc_link_hash_entry *h;
11549 bfd_vma symval;
11550
11551 /* Relocs are always against symbols in their own object file. Fake
11552 up global sym hashes for the stub bfd (which has no symbols). */
11553 hashes = elf_sym_hashes (htab->params->stub_bfd);
11554 if (hashes == NULL)
11555 {
11556 bfd_size_type hsize;
11557
11558 /* When called the first time, stub_globals will contain the
11559 total number of symbols seen during stub sizing. After
11560 allocating, stub_globals is used as an index to fill the
11561 hashes array. */
11562 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11563 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11564 if (hashes == NULL)
0a1b45a2 11565 return false;
3d58e1fc
AM
11566 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11567 htab->stub_globals = 1;
11568 }
11569 symndx = htab->stub_globals++;
11570 h = stub_entry->h;
11571 hashes[symndx] = &h->elf;
11572 if (h->oh != NULL && h->oh->is_func)
11573 h = ppc_follow_link (h->oh);
11574 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11575 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11576 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11577 while (num_rel-- != 0)
11578 {
11579 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11580 if (h->elf.root.u.def.section != stub_entry->target_section)
11581 {
11582 /* H is an opd symbol. The addend must be zero, and the
11583 branch reloc is the only one we can convert. */
11584 r->r_addend = 0;
11585 break;
11586 }
11587 else
11588 r->r_addend -= symval;
11589 --r;
11590 }
0a1b45a2 11591 return true;
3d58e1fc
AM
11592}
11593
aa374f67 11594static bfd_vma
25f53a85 11595get_r2off (struct bfd_link_info *info,
aa374f67
AM
11596 struct ppc_stub_hash_entry *stub_entry)
11597{
25f53a85 11598 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11599 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11600
11601 if (r2off == 0)
11602 {
11603 /* Support linking -R objects. Get the toc pointer from the
11604 opd entry. */
11605 char buf[8];
b9e5796b
AM
11606 if (!htab->opd_abi)
11607 return r2off;
aa374f67
AM
11608 asection *opd = stub_entry->h->elf.root.u.def.section;
11609 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11610
11611 if (strcmp (opd->name, ".opd") != 0
11612 || opd->reloc_count != 0)
11613 {
2cdcc330
AM
11614 info->callbacks->einfo
11615 (_("%P: cannot find opd entry toc for `%pT'\n"),
11616 stub_entry->h->elf.root.root.string);
aa374f67 11617 bfd_set_error (bfd_error_bad_value);
a7c49797 11618 return (bfd_vma) -1;
aa374f67
AM
11619 }
11620 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11621 return (bfd_vma) -1;
aa374f67 11622 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11623 r2off -= elf_gp (info->output_bfd);
aa374f67 11624 }
6f20ed8a 11625 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11626 return r2off;
11627}
11628
0a1b45a2 11629static bool
4ce794b7 11630ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11631{
721956f4
AM
11632 struct ppc_stub_hash_entry *stub_entry;
11633 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11634 struct bfd_link_info *info;
11635 struct ppc_link_hash_table *htab;
29433886 11636 bfd *obfd;
721956f4 11637 bfd_byte *loc;
3d58e1fc 11638 bfd_byte *p, *relp;
1aa42141 11639 bfd_vma targ, off;
176a0d42 11640 Elf_Internal_Rela *r;
e054468f 11641 asection *plt;
3d58e1fc 11642 int num_rel;
04bdff6a 11643 int odd;
0a1b45a2 11644 bool is_tga;
5d1634d7 11645
721956f4
AM
11646 /* Massage our args to the form they really have. */
11647 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11648 info = in_arg;
5d1634d7 11649
abf874aa
CL
11650 /* Fail if the target section could not be assigned to an output
11651 section. The user should fix his linker script. */
11652 if (stub_entry->target_section != NULL
11653 && stub_entry->target_section->output_section == NULL
11654 && info->non_contiguous_regions)
53215f21
CL
11655 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11656 "Retry without --enable-non-contiguous-regions.\n"),
11657 stub_entry->target_section);
abf874aa
CL
11658
11659 /* Same for the group. */
11660 if (stub_entry->group->stub_sec != NULL
11661 && stub_entry->group->stub_sec->output_section == NULL
11662 && info->non_contiguous_regions)
53215f21
CL
11663 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11664 "output section. Retry without "
11665 "--enable-non-contiguous-regions.\n"),
11666 stub_entry->group->stub_sec,
11667 stub_entry->target_section);
abf874aa 11668
5d1634d7 11669 htab = ppc_hash_table (info);
4dfe6ac6 11670 if (htab == NULL)
0a1b45a2 11671 return false;
5d1634d7 11672
1aa42141 11673 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11674 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11675
7aba54da
AM
11676 htab->stub_count[stub_entry->type.main - 1] += 1;
11677 if (stub_entry->type.main == ppc_stub_long_branch
11678 && stub_entry->type.sub == ppc_stub_toc)
5d1634d7 11679 {
721956f4 11680 /* Branches are relative. This is where we are going to. */
1aa42141 11681 targ = (stub_entry->target_value
6911b7dc
AM
11682 + stub_entry->target_section->output_offset
11683 + stub_entry->target_section->output_section->vma);
1aa42141 11684 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11685
721956f4 11686 /* And this is where we are coming from. */
1aa42141
AM
11687 off = (stub_entry->stub_offset
11688 + stub_entry->group->stub_sec->output_offset
11689 + stub_entry->group->stub_sec->output_section->vma);
11690 off = targ - off;
e86ce104 11691
9e390558 11692 p = loc;
29433886 11693 obfd = htab->params->stub_bfd;
7aba54da 11694 if (stub_entry->type.r2save)
ad8e1ba5 11695 {
25f53a85 11696 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11697
a7c49797 11698 if (r2off == (bfd_vma) -1)
aa374f67 11699 {
0a1b45a2
AM
11700 htab->stub_error = true;
11701 return false;
aa374f67 11702 }
29433886 11703 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11704 p += 4;
ac2df442
AM
11705 if (PPC_HA (r2off) != 0)
11706 {
29433886 11707 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11708 p += 4;
a7c49797
AM
11709 }
11710 if (PPC_LO (r2off) != 0)
11711 {
29433886 11712 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11713 p += 4;
ac2df442 11714 }
9e390558 11715 off -= p - loc;
ad8e1ba5 11716 }
29433886 11717 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11718 p += 4;
ad8e1ba5 11719
5c3dead3
AM
11720 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11721 {
cf97bcb0
AM
11722 _bfd_error_handler
11723 (_("long branch stub `%s' offset overflow"),
bc30df16 11724 stub_entry->root.string);
0a1b45a2
AM
11725 htab->stub_error = true;
11726 return false;
5c3dead3 11727 }
ee75fd95
AM
11728
11729 if (info->emitrelocations)
11730 {
6f20ed8a 11731 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42 11732 if (r == NULL)
0a1b45a2 11733 return false;
9e390558 11734 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11735 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11736 r->r_addend = targ;
3d58e1fc
AM
11737 if (stub_entry->h != NULL
11738 && !use_global_in_relocs (htab, stub_entry, r, 1))
0a1b45a2 11739 return false;
ee75fd95 11740 }
7aba54da
AM
11741 }
11742 else if (stub_entry->type.main == ppc_stub_plt_branch
11743 && stub_entry->type.sub == ppc_stub_toc)
11744 {
721956f4
AM
11745 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11746 stub_entry->root.string + 9,
0a1b45a2 11747 false, false);
721956f4
AM
11748 if (br_entry == NULL)
11749 {
cf97bcb0
AM
11750 _bfd_error_handler (_("can't find branch stub `%s'"),
11751 stub_entry->root.string);
0a1b45a2
AM
11752 htab->stub_error = true;
11753 return false;
721956f4
AM
11754 }
11755
1aa42141 11756 targ = (stub_entry->target_value
176a0d42
AM
11757 + stub_entry->target_section->output_offset
11758 + stub_entry->target_section->output_section->vma);
7aba54da 11759 if (!stub_entry->type.r2save)
1aa42141 11760 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11761
1aa42141 11762 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11763 htab->brlt->contents + br_entry->offset);
721956f4 11764
f94498ff 11765 if (br_entry->iter == htab->stub_iteration)
721956f4 11766 {
f94498ff 11767 br_entry->iter = 0;
84f5d08e 11768
1657026c 11769 if (htab->relbrlt != NULL && !info->enable_dt_relr)
84f5d08e 11770 {
f94498ff
AM
11771 /* Create a reloc for the branch lookup table entry. */
11772 Elf_Internal_Rela rela;
11773 bfd_byte *rl;
11774
11775 rela.r_offset = (br_entry->offset
11776 + htab->brlt->output_offset
11777 + htab->brlt->output_section->vma);
11778 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11779 rela.r_addend = targ;
f94498ff
AM
11780
11781 rl = htab->relbrlt->contents;
11782 rl += (htab->relbrlt->reloc_count++
11783 * sizeof (Elf64_External_Rela));
11784 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11785 }
11786 else if (info->emitrelocations)
11787 {
176a0d42
AM
11788 r = get_relocs (htab->brlt, 1);
11789 if (r == NULL)
0a1b45a2 11790 return false;
176a0d42
AM
11791 /* brlt, being SEC_LINKER_CREATED does not go through the
11792 normal reloc processing. Symbols and offsets are not
11793 translated from input file to output file form, so
11794 set up the offset per the output file. */
f94498ff
AM
11795 r->r_offset = (br_entry->offset
11796 + htab->brlt->output_offset
11797 + htab->brlt->output_section->vma);
11798 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11799 r->r_addend = targ;
84f5d08e 11800 }
84f5d08e 11801 }
721956f4 11802
1aa42141 11803 targ = (br_entry->offset
176a0d42
AM
11804 + htab->brlt->output_offset
11805 + htab->brlt->output_section->vma);
11806
1aa42141
AM
11807 off = (elf_gp (info->output_bfd)
11808 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11809 off = targ - off;
721956f4 11810
ad8e1ba5 11811 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11812 {
25f53a85 11813 info->callbacks->einfo
c1c8c1ef 11814 (_("%P: linkage table error against `%pT'\n"),
721956f4 11815 stub_entry->root.string);
5d1634d7 11816 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11817 htab->stub_error = true;
11818 return false;
5d1634d7 11819 }
41bd81ab 11820
176a0d42
AM
11821 if (info->emitrelocations)
11822 {
6f20ed8a 11823 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42 11824 if (r == NULL)
0a1b45a2 11825 return false;
6f20ed8a 11826 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11827 if (bfd_big_endian (info->output_bfd))
11828 r[0].r_offset += 2;
7aba54da 11829 if (stub_entry->type.r2save)
176a0d42
AM
11830 r[0].r_offset += 4;
11831 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11832 r[0].r_addend = targ;
176a0d42
AM
11833 if (PPC_HA (off) != 0)
11834 {
11835 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11836 r[1].r_offset = r[0].r_offset + 4;
11837 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11838 r[1].r_addend = r[0].r_addend;
11839 }
11840 }
11841
9e390558 11842 p = loc;
29433886 11843 obfd = htab->params->stub_bfd;
7aba54da 11844 if (!stub_entry->type.r2save)
ad8e1ba5 11845 {
176a0d42 11846 if (PPC_HA (off) != 0)
ac2df442 11847 {
29433886 11848 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11849 p += 4;
29433886 11850 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11851 }
11852 else
29433886 11853 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11854 }
11855 else
11856 {
25f53a85 11857 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11858
a7c49797 11859 if (r2off == (bfd_vma) -1)
aa374f67 11860 {
0a1b45a2
AM
11861 htab->stub_error = true;
11862 return false;
aa374f67 11863 }
ad8e1ba5 11864
29433886 11865 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11866 p += 4;
176a0d42 11867 if (PPC_HA (off) != 0)
ac2df442 11868 {
29433886 11869 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11870 p += 4;
29433886 11871 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11872 }
11873 else
29433886 11874 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11875
11876 if (PPC_HA (r2off) != 0)
11877 {
9e390558 11878 p += 4;
29433886 11879 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11880 }
11881 if (PPC_LO (r2off) != 0)
11882 {
9e390558 11883 p += 4;
29433886 11884 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11885 }
ad8e1ba5 11886 }
9e390558 11887 p += 4;
29433886 11888 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11889 p += 4;
29433886 11890 bfd_put_32 (obfd, BCTR, p);
407aa07c 11891 p += 4;
7aba54da
AM
11892 }
11893 else if (stub_entry->type.sub >= ppc_stub_notoc)
11894 {
11895 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
05d0e962 11896 p = loc;
f891966f 11897 off = (stub_entry->stub_offset
05d0e962
AM
11898 + stub_entry->group->stub_sec->output_offset
11899 + stub_entry->group->stub_sec->output_section->vma);
29433886 11900 obfd = htab->params->stub_bfd;
7aba54da 11901 is_tga = (is_plt
12cf8b93 11902 && stub_entry->h != NULL
29433886
AM
11903 && is_tls_get_addr (&stub_entry->h->elf, htab)
11904 && htab->params->tls_get_addr_opt);
11905 if (is_tga)
11906 {
11907 p = build_tls_get_addr_head (htab, stub_entry, p);
11908 off += p - loc;
11909 }
7aba54da 11910 if (stub_entry->type.r2save)
05d0e962
AM
11911 {
11912 off += 4;
29433886 11913 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11914 p += 4;
11915 }
7aba54da 11916 if (is_plt)
05d0e962
AM
11917 {
11918 targ = stub_entry->plt_ent->plt.offset & ~1;
11919 if (targ >= (bfd_vma) -2)
11920 abort ();
11921
11922 plt = htab->elf.splt;
30845f11 11923 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
11924 {
11925 if (stub_entry->symtype == STT_GNU_IFUNC)
11926 plt = htab->elf.iplt;
11927 else
11928 plt = htab->pltlocal;
11929 }
11930 targ += plt->output_offset + plt->output_section->vma;
11931 }
11932 else
11933 targ = (stub_entry->target_value
11934 + stub_entry->target_section->output_offset
11935 + stub_entry->target_section->output_section->vma);
04bdff6a 11936 odd = off & 4;
05d0e962 11937 off = targ - off;
f891966f 11938
3d58e1fc
AM
11939 relp = p;
11940 num_rel = 0;
7aba54da
AM
11941 if (stub_entry->type.sub == ppc_stub_notoc)
11942 p = build_power10_offset (obfd, p, off, odd, is_plt);
04bdff6a
AM
11943 else
11944 {
29433886
AM
11945 if (htab->glink_eh_frame != NULL
11946 && htab->glink_eh_frame->size != 0)
11947 {
11948 bfd_byte *base, *eh;
11949 unsigned int lr_used, delta;
11950
11951 base = (htab->glink_eh_frame->contents
11952 + stub_entry->group->eh_base + 17);
11953 eh = base + stub_entry->group->eh_size;
11954 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11955 delta = lr_used - stub_entry->group->lr_restore;
11956 stub_entry->group->lr_restore = lr_used + 8;
11957 eh = eh_advance (htab->elf.dynobj, eh, delta);
11958 *eh++ = DW_CFA_register;
11959 *eh++ = 65;
11960 *eh++ = 12;
11961 *eh++ = DW_CFA_advance_loc + 2;
11962 *eh++ = DW_CFA_restore_extended;
11963 *eh++ = 65;
11964 stub_entry->group->eh_size = eh - base;
11965 }
11966
04bdff6a
AM
11967 /* The notoc stubs calculate their target (either a PLT entry or
11968 the global entry point of a function) relative to the PC
11969 returned by the "bcl" two instructions past the start of the
11970 sequence emitted by build_offset. The offset is therefore 8
11971 less than calculated from the start of the sequence. */
11972 off -= 8;
7aba54da 11973 p = build_offset (obfd, p, off, is_plt);
04bdff6a
AM
11974 }
11975
7aba54da 11976 if (stub_entry->type.main == ppc_stub_long_branch)
05d0e962 11977 {
f891966f 11978 bfd_vma from;
3d58e1fc 11979 num_rel = 1;
f891966f
AM
11980 from = (stub_entry->stub_offset
11981 + stub_entry->group->stub_sec->output_offset
11982 + stub_entry->group->stub_sec->output_section->vma
11983 + (p - loc));
29433886 11984 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11985 }
11986 else
11987 {
29433886 11988 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 11989 p += 4;
29433886 11990 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
11991 }
11992 p += 4;
df136d64 11993
29433886
AM
11994 if (is_tga)
11995 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11996
3d58e1fc
AM
11997 if (info->emitrelocations)
11998 {
04bdff6a 11999 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
7aba54da 12000 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12001 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12002 else
12003 {
12004 num_rel += num_relocs_for_offset (off);
12005 roff += 16;
12006 }
3d58e1fc
AM
12007 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12008 if (r == NULL)
0a1b45a2 12009 return false;
7aba54da 12010 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12011 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
12012 else
12013 r = emit_relocs_for_offset (info, r, roff, targ, off);
7aba54da 12014 if (stub_entry->type.main == ppc_stub_long_branch)
3d58e1fc
AM
12015 {
12016 ++r;
12017 roff = p - 4 - stub_entry->group->stub_sec->contents;
12018 r->r_offset = roff;
12019 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12020 r->r_addend = targ;
12021 if (stub_entry->h != NULL
12022 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
0a1b45a2 12023 return false;
3d58e1fc
AM
12024 }
12025 }
7aba54da
AM
12026 }
12027 else if (stub_entry->type.main == ppc_stub_plt_call)
12028 {
e054468f 12029 if (stub_entry->h != NULL
b31867b6
AM
12030 && stub_entry->h->is_func_descriptor
12031 && stub_entry->h->oh != NULL)
c862ae31 12032 {
b31867b6
AM
12033 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12034
12035 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 12036 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
12037 if (fh->elf.root.type == bfd_link_hash_undefined
12038 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12039 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 12040 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
12041 }
12042
721956f4 12043 /* Now build the stub. */
1aa42141
AM
12044 targ = stub_entry->plt_ent->plt.offset & ~1;
12045 if (targ >= (bfd_vma) -2)
721956f4
AM
12046 abort ();
12047
33e44f2e 12048 plt = htab->elf.splt;
30845f11 12049 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
12050 {
12051 if (stub_entry->symtype == STT_GNU_IFUNC)
12052 plt = htab->elf.iplt;
12053 else
12054 plt = htab->pltlocal;
12055 }
1aa42141 12056 targ += plt->output_offset + plt->output_section->vma;
e054468f 12057
1aa42141
AM
12058 off = (elf_gp (info->output_bfd)
12059 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12060 off = targ - off;
721956f4 12061
ad8e1ba5 12062 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 12063 {
25f53a85 12064 info->callbacks->einfo
695344c0 12065 /* xgettext:c-format */
c1c8c1ef 12066 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
12067 stub_entry->h != NULL
12068 ? stub_entry->h->elf.root.root.string
12069 : "<local sym>");
721956f4 12070 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
12071 htab->stub_error = true;
12072 return false;
721956f4
AM
12073 }
12074
176a0d42
AM
12075 r = NULL;
12076 if (info->emitrelocations)
12077 {
6f20ed8a 12078 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
12079 ((PPC_HA (off) != 0)
12080 + (htab->opd_abi
e7d1c40c 12081 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
12082 && PPC_HA (off + 16) == PPC_HA (off))
12083 : 1)));
176a0d42 12084 if (r == NULL)
0a1b45a2 12085 return false;
6f20ed8a 12086 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
12087 if (bfd_big_endian (info->output_bfd))
12088 r[0].r_offset += 2;
1aa42141 12089 r[0].r_addend = targ;
176a0d42 12090 }
29433886
AM
12091 p = loc;
12092 obfd = htab->params->stub_bfd;
12093 is_tga = (stub_entry->h != NULL
12094 && is_tls_get_addr (&stub_entry->h->elf, htab)
12095 && htab->params->tls_get_addr_opt);
12096 if (is_tga)
12097 {
12098 p = build_tls_get_addr_head (htab, stub_entry, p);
12099 if (r != NULL)
12100 r[0].r_offset += p - loc;
12101 }
12102 p = build_plt_stub (htab, stub_entry, p, off, r);
12103 if (is_tga)
12104 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
7aba54da
AM
12105 }
12106 else if (stub_entry->type.main == ppc_stub_save_res)
12107 return true;
12108 else
12109 {
721956f4 12110 BFD_FAIL ();
0a1b45a2 12111 return false;
721956f4
AM
12112 }
12113
1aa42141 12114 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 12115
e7d1c40c 12116 if (htab->params->emit_stub_syms)
97b639ba
AM
12117 {
12118 struct elf_link_hash_entry *h;
ee75fd95
AM
12119 size_t len1, len2;
12120 char *name;
12121 const char *const stub_str[] = { "long_branch",
05d0e962 12122 "plt_branch",
ee75fd95
AM
12123 "plt_call" };
12124
7aba54da 12125 len1 = strlen (stub_str[stub_entry->type.main - 1]);
ee75fd95
AM
12126 len2 = strlen (stub_entry->root.string);
12127 name = bfd_malloc (len1 + len2 + 2);
12128 if (name == NULL)
0a1b45a2 12129 return false;
ee75fd95 12130 memcpy (name, stub_entry->root.string, 9);
7aba54da 12131 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
ee75fd95 12132 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
0a1b45a2 12133 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
97b639ba 12134 if (h == NULL)
0a1b45a2 12135 return false;
97b639ba
AM
12136 if (h->root.type == bfd_link_hash_new)
12137 {
12138 h->root.type = bfd_link_hash_defined;
6f20ed8a 12139 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 12140 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
12141 h->ref_regular = 1;
12142 h->def_regular = 1;
12143 h->ref_regular_nonweak = 1;
12144 h->forced_local = 1;
12145 h->non_elf = 0;
2ec55de3 12146 h->root.linker_def = 1;
97b639ba
AM
12147 }
12148 }
12149
0a1b45a2 12150 return true;
721956f4
AM
12151}
12152
12153/* As above, but don't actually build the stub. Just bump offset so
12154 we know stub section sizes, and select plt_branch stubs where
12155 long_branch stubs won't do. */
12156
0a1b45a2 12157static bool
4ce794b7 12158ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
12159{
12160 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 12161 struct bfd_link_info *info;
721956f4 12162 struct ppc_link_hash_table *htab;
f891966f
AM
12163 asection *plt;
12164 bfd_vma targ, off, r2off;
04bdff6a 12165 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
12166
12167 /* Massage our args to the form they really have. */
12168 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
12169 info = in_arg;
12170
12171 htab = ppc_hash_table (info);
4dfe6ac6 12172 if (htab == NULL)
0a1b45a2 12173 return false;
721956f4 12174
abf874aa
CL
12175 /* Fail if the target section could not be assigned to an output
12176 section. The user should fix his linker script. */
12177 if (stub_entry->target_section != NULL
12178 && stub_entry->target_section->output_section == NULL
12179 && info->non_contiguous_regions)
53215f21
CL
12180 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12181 "Retry without --enable-non-contiguous-regions.\n"),
12182 stub_entry->target_section);
abf874aa
CL
12183
12184 /* Same for the group. */
12185 if (stub_entry->group->stub_sec != NULL
12186 && stub_entry->group->stub_sec->output_section == NULL
12187 && info->non_contiguous_regions)
53215f21
CL
12188 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12189 "output section. Retry without "
12190 "--enable-non-contiguous-regions.\n"),
12191 stub_entry->group->stub_sec,
12192 stub_entry->target_section);
abf874aa 12193
1aa42141
AM
12194 /* Make a note of the offset within the stubs for this entry. */
12195 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12196
a4b6fadd
AM
12197 if (stub_entry->h != NULL
12198 && stub_entry->h->save_res
12199 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12200 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12201 {
12202 /* Don't make stubs to out-of-line register save/restore
12203 functions. Instead, emit copies of the functions. */
12204 stub_entry->group->needs_save_res = 1;
7aba54da
AM
12205 stub_entry->type.main = ppc_stub_save_res;
12206 stub_entry->type.sub = ppc_stub_toc;
12207 stub_entry->type.r2save = 0;
0a1b45a2 12208 return true;
a4b6fadd
AM
12209 }
12210
7aba54da 12211 if (stub_entry->type.main == ppc_stub_plt_branch)
721956f4 12212 {
f891966f
AM
12213 /* Reset the stub type from the plt branch variant in case we now
12214 can reach with a shorter stub. */
7aba54da
AM
12215 stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
12216 }
12217
12218 if (stub_entry->type.main == ppc_stub_long_branch
12219 && stub_entry->type.sub == ppc_stub_toc)
12220 {
1aa42141
AM
12221 targ = (stub_entry->target_value
12222 + stub_entry->target_section->output_offset
12223 + stub_entry->target_section->output_section->vma);
f891966f 12224 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
12225 off = (stub_entry->stub_offset
12226 + stub_entry->group->stub_sec->output_offset
12227 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12228
ad8e1ba5 12229 size = 4;
f891966f 12230 r2off = 0;
7aba54da 12231 if (stub_entry->type.r2save)
ad8e1ba5 12232 {
25f53a85 12233 r2off = get_r2off (info, stub_entry);
a7c49797 12234 if (r2off == (bfd_vma) -1)
aa374f67 12235 {
0a1b45a2
AM
12236 htab->stub_error = true;
12237 return false;
aa374f67 12238 }
a7c49797 12239 size = 8;
ac2df442 12240 if (PPC_HA (r2off) != 0)
a7c49797
AM
12241 size += 4;
12242 if (PPC_LO (r2off) != 0)
12243 size += 4;
1aa42141 12244 off += size - 4;
ad8e1ba5 12245 }
1aa42141 12246 off = targ - off;
ad8e1ba5 12247
f891966f
AM
12248 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12249 Do the same for -R objects without function descriptors. */
7aba54da 12250 if ((stub_entry->type.r2save
f891966f
AM
12251 && r2off == 0
12252 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12253 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12254 {
f891966f 12255 struct ppc_branch_hash_entry *br_entry;
df136d64 12256
f891966f
AM
12257 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12258 stub_entry->root.string + 9,
0a1b45a2 12259 true, false);
f891966f 12260 if (br_entry == NULL)
721956f4 12261 {
f891966f
AM
12262 _bfd_error_handler (_("can't build branch stub `%s'"),
12263 stub_entry->root.string);
0a1b45a2
AM
12264 htab->stub_error = true;
12265 return false;
721956f4
AM
12266 }
12267
f891966f 12268 if (br_entry->iter != htab->stub_iteration)
721956f4 12269 {
f891966f
AM
12270 br_entry->iter = htab->stub_iteration;
12271 br_entry->offset = htab->brlt->size;
12272 htab->brlt->size += 8;
63bc6f6c 12273
1657026c 12274 if (htab->relbrlt != NULL && !info->enable_dt_relr)
f891966f
AM
12275 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12276 else if (info->emitrelocations)
84f5d08e 12277 {
f891966f
AM
12278 htab->brlt->reloc_count += 1;
12279 htab->brlt->flags |= SEC_RELOC;
05d0e962 12280 }
f891966f 12281 }
ac2df442 12282
f891966f
AM
12283 targ = (br_entry->offset
12284 + htab->brlt->output_offset
12285 + htab->brlt->output_section->vma);
12286 off = (elf_gp (info->output_bfd)
12287 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12288 off = targ - off;
176a0d42 12289
f891966f
AM
12290 if (info->emitrelocations)
12291 {
12292 stub_entry->group->stub_sec->reloc_count
12293 += 1 + (PPC_HA (off) != 0);
12294 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12295 }
05d0e962 12296
7aba54da
AM
12297 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12298 if (!stub_entry->type.r2save)
f891966f
AM
12299 {
12300 size = 12;
12301 if (PPC_HA (off) != 0)
12302 size = 16;
ac2df442 12303 }
f891966f 12304 else
ac2df442 12305 {
f891966f
AM
12306 size = 16;
12307 if (PPC_HA (off) != 0)
12308 size += 4;
12309
12310 if (PPC_HA (r2off) != 0)
12311 size += 4;
12312 if (PPC_LO (r2off) != 0)
12313 size += 4;
ac2df442 12314 }
721956f4 12315 }
f891966f
AM
12316 else if (info->emitrelocations)
12317 {
12318 stub_entry->group->stub_sec->reloc_count += 1;
12319 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12320 }
7aba54da
AM
12321 }
12322 else if (stub_entry->type.main == ppc_stub_long_branch)
12323 {
f891966f
AM
12324 off = (stub_entry->stub_offset
12325 + stub_entry->group->stub_sec->output_offset
12326 + stub_entry->group->stub_sec->output_section->vma);
12327 size = 0;
7aba54da 12328 if (stub_entry->type.r2save)
f891966f
AM
12329 size = 4;
12330 off += size;
12331 targ = (stub_entry->target_value
12332 + stub_entry->target_section->output_offset
12333 + stub_entry->target_section->output_section->vma);
04bdff6a 12334 odd = off & 4;
f891966f
AM
12335 off = targ - off;
12336
3d58e1fc
AM
12337 if (info->emitrelocations)
12338 {
04bdff6a 12339 unsigned int num_rel;
7aba54da 12340 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12341 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12342 else
12343 num_rel = num_relocs_for_offset (off - 8);
12344 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12345 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12346 }
12347
7aba54da 12348 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12349 extra = size_power10_offset (off, odd);
04bdff6a
AM
12350 else
12351 extra = size_offset (off - 8);
f891966f
AM
12352 /* Include branch insn plus those in the offset sequence. */
12353 size += 4 + extra;
12354 /* The branch insn is at the end, or "extra" bytes along. So
12355 its offset will be "extra" bytes less that that already
12356 calculated. */
12357 off -= extra;
12358
7aba54da 12359 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12360 {
12361 /* After the bcl, lr has been modified so we need to emit
12362 .eh_frame info saying the return address is in r12. */
12363 lr_used = stub_entry->stub_offset + 8;
7aba54da 12364 if (stub_entry->type.r2save)
04bdff6a
AM
12365 lr_used += 4;
12366 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12367 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12368 DW_CFA_restore_extended 65. */
12369 delta = lr_used - stub_entry->group->lr_restore;
12370 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12371 stub_entry->group->lr_restore = lr_used + 8;
12372 }
f891966f
AM
12373
12374 /* If the branch can't reach, use a plt_branch. */
12375 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12376 {
7aba54da 12377 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
f891966f
AM
12378 size += 4;
12379 }
3d58e1fc
AM
12380 else if (info->emitrelocations)
12381 stub_entry->group->stub_sec->reloc_count +=1;
7aba54da
AM
12382 }
12383 else if (stub_entry->type.sub >= ppc_stub_notoc)
12384 {
12385 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
29433886 12386 lr_used = 0;
12cf8b93
AM
12387 if (stub_entry->h != NULL
12388 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12389 && htab->params->tls_get_addr_opt)
12390 {
12391 lr_used += 7 * 4;
12392 if (!htab->params->no_tls_get_addr_regsave)
12393 lr_used += 11 * 4;
7aba54da 12394 else if (stub_entry->type.r2save)
29433886
AM
12395 lr_used += 2 * 4;
12396 }
7aba54da 12397 if (stub_entry->type.r2save)
29433886 12398 lr_used += 4;
f891966f
AM
12399 targ = stub_entry->plt_ent->plt.offset & ~1;
12400 if (targ >= (bfd_vma) -2)
12401 abort ();
12402
12403 plt = htab->elf.splt;
30845f11 12404 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12405 {
12406 if (stub_entry->symtype == STT_GNU_IFUNC)
12407 plt = htab->elf.iplt;
12408 else
12409 plt = htab->pltlocal;
12410 }
12411 targ += plt->output_offset + plt->output_section->vma;
29433886
AM
12412 off = (stub_entry->stub_offset
12413 + stub_entry->group->stub_sec->output_offset
12414 + stub_entry->group->stub_sec->output_section->vma
12415 + lr_used);
04bdff6a 12416 odd = off & 4;
f891966f
AM
12417 off = targ - off;
12418
12419 if (htab->params->plt_stub_align != 0)
12420 {
29433886 12421 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
f891966f
AM
12422
12423 stub_entry->group->stub_sec->size += pad;
12424 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12425 off -= pad;
29433886 12426 odd ^= pad & 4;
f891966f
AM
12427 }
12428
3d58e1fc
AM
12429 if (info->emitrelocations)
12430 {
04bdff6a 12431 unsigned int num_rel;
7aba54da 12432 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12433 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12434 else
12435 num_rel = num_relocs_for_offset (off - 8);
12436 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12437 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12438 }
12439
29433886 12440 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12441
7aba54da 12442 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12443 {
12444 /* After the bcl, lr has been modified so we need to emit
12445 .eh_frame info saying the return address is in r12. */
29433886 12446 lr_used += stub_entry->stub_offset + 8;
04bdff6a
AM
12447 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12448 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12449 DW_CFA_restore_extended 65. */
12450 delta = lr_used - stub_entry->group->lr_restore;
12451 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12452 stub_entry->group->lr_restore = lr_used + 8;
12453 }
7aba54da 12454 if (stub_entry->h != NULL
29433886
AM
12455 && is_tls_get_addr (&stub_entry->h->elf, htab)
12456 && htab->params->tls_get_addr_opt)
12457 {
12458 if (!htab->params->no_tls_get_addr_regsave)
12459 {
12460 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12461 delta = cfa_updt - stub_entry->group->lr_restore;
12462 stub_entry->group->eh_size += eh_advance_size (delta);
12463 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12464 stub_entry->group->lr_restore
12465 = stub_entry->stub_offset + size - 4;
12466 }
7aba54da 12467 else if (stub_entry->type.r2save)
29433886
AM
12468 {
12469 lr_used = stub_entry->stub_offset + size - 20;
12470 delta = lr_used - stub_entry->group->lr_restore;
12471 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12472 stub_entry->group->lr_restore
12473 = stub_entry->stub_offset + size - 4;
12474 }
12475 }
7aba54da
AM
12476 }
12477 else if (stub_entry->type.main == ppc_stub_plt_call)
12478 {
f891966f
AM
12479 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12480 if (targ >= (bfd_vma) -2)
12481 abort ();
12482 plt = htab->elf.splt;
30845f11 12483 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12484 {
12485 if (stub_entry->symtype == STT_GNU_IFUNC)
12486 plt = htab->elf.iplt;
12487 else
12488 plt = htab->pltlocal;
12489 }
12490 targ += plt->output_offset + plt->output_section->vma;
12491
12492 off = (elf_gp (info->output_bfd)
12493 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12494 off = targ - off;
12495
12496 if (htab->params->plt_stub_align != 0)
12497 {
29433886 12498 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
f891966f
AM
12499
12500 stub_entry->group->stub_sec->size += pad;
12501 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12502 }
12503
12504 if (info->emitrelocations)
12505 {
12506 stub_entry->group->stub_sec->reloc_count
12507 += ((PPC_HA (off) != 0)
12508 + (htab->opd_abi
12509 ? 2 + (htab->params->plt_static_chain
12510 && PPC_HA (off + 16) == PPC_HA (off))
12511 : 1));
12512 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12513 }
12514
29433886 12515 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12516
12517 if (stub_entry->h != NULL
ed7007c1 12518 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f 12519 && htab->params->tls_get_addr_opt
7aba54da 12520 && stub_entry->type.r2save)
f891966f 12521 {
29433886
AM
12522 if (!htab->params->no_tls_get_addr_regsave)
12523 {
12524 /* Adjustments to r1 need to be described. */
12525 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12526 delta = cfa_updt - stub_entry->group->lr_restore;
12527 stub_entry->group->eh_size += eh_advance_size (delta);
12528 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12529 }
12530 else
9e7028aa
AM
12531 {
12532 lr_used = stub_entry->stub_offset + size - 20;
12533 /* The eh_frame info will consist of a DW_CFA_advance_loc
12534 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12535 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12536 delta = lr_used - stub_entry->group->lr_restore;
12537 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12538 }
29433886 12539 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
f891966f 12540 }
7aba54da
AM
12541 }
12542 else
12543 {
f891966f 12544 BFD_FAIL ();
0a1b45a2 12545 return false;
721956f4
AM
12546 }
12547
6f20ed8a 12548 stub_entry->group->stub_sec->size += size;
0a1b45a2 12549 return true;
721956f4
AM
12550}
12551
12552/* Set up various things so that we can make a list of input sections
12553 for each output section included in the link. Returns -1 on error,
cedb70c5 12554 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12555
12556int
e7d1c40c 12557ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12558{
6f20ed8a 12559 unsigned int id;
986f0783 12560 size_t amt;
721956f4
AM
12561 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12562
4dfe6ac6
NC
12563 if (htab == NULL)
12564 return -1;
4c52953f 12565
7cf7fcc8 12566 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12567 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12568 htab->sec_info = bfd_zmalloc (amt);
12569 if (htab->sec_info == NULL)
721956f4
AM
12570 return -1;
12571
3d6f9012
AM
12572 /* Set toc_off for com, und, abs and ind sections. */
12573 for (id = 0; id < 3; id++)
6f20ed8a 12574 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12575
721956f4
AM
12576 return 1;
12577}
12578
927be08e
AM
12579/* Set up for first pass at multitoc partitioning. */
12580
12581void
12582ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12583{
12584 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12585
1c865ab2 12586 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12587 htab->toc_bfd = NULL;
12588 htab->toc_first_sec = NULL;
12589}
12590
e717da7e
AM
12591/* The linker repeatedly calls this function for each TOC input section
12592 and linker generated GOT section. Group input bfds such that the toc
927be08e 12593 within a group is less than 64k in size. */
ad8e1ba5 12594
0a1b45a2 12595bool
4ce794b7 12596ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12597{
12598 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12599 bfd_vma addr, off, limit;
ad8e1ba5 12600
4dfe6ac6 12601 if (htab == NULL)
0a1b45a2 12602 return false;
4dfe6ac6 12603
927be08e 12604 if (!htab->second_toc_pass)
4c52953f 12605 {
927be08e 12606 /* Keep track of the first .toc or .got section for this input bfd. */
0a1b45a2 12607 bool new_bfd = htab->toc_bfd != isec->owner;
a4fd3de5
AM
12608
12609 if (new_bfd)
bf102f86
AM
12610 {
12611 htab->toc_bfd = isec->owner;
12612 htab->toc_first_sec = isec;
12613 }
927be08e 12614
bf102f86
AM
12615 addr = isec->output_offset + isec->output_section->vma;
12616 off = addr - htab->toc_curr;
d77c8a4b
AM
12617 limit = 0x80008000;
12618 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12619 limit = 0x10000;
12620 if (off + isec->size > limit)
bf102f86
AM
12621 {
12622 addr = (htab->toc_first_sec->output_offset
12623 + htab->toc_first_sec->output_section->vma);
12624 htab->toc_curr = addr;
a27e685f 12625 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12626 }
99877b66 12627
927be08e
AM
12628 /* toc_curr is the base address of this toc group. Set elf_gp
12629 for the input section to be the offset relative to the
12630 output toc base plus 0x8000. Making the input elf_gp an
12631 offset allows us to move the toc as a whole without
12632 recalculating input elf_gp. */
06bcf541 12633 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12634 off += TOC_BASE_OFF;
12635
12636 /* Die if someone uses a linker script that doesn't keep input
12637 file .toc and .got together. */
a4fd3de5
AM
12638 if (new_bfd
12639 && elf_gp (isec->owner) != 0
927be08e 12640 && elf_gp (isec->owner) != off)
0a1b45a2 12641 return false;
927be08e
AM
12642
12643 elf_gp (isec->owner) = off;
0a1b45a2 12644 return true;
4c52953f 12645 }
927be08e
AM
12646
12647 /* During the second pass toc_first_sec points to the start of
12648 a toc group, and toc_curr is used to track the old elf_gp.
12649 We use toc_bfd to ensure we only look at each bfd once. */
12650 if (htab->toc_bfd == isec->owner)
0a1b45a2 12651 return true;
927be08e
AM
12652 htab->toc_bfd = isec->owner;
12653
12654 if (htab->toc_first_sec == NULL
12655 || htab->toc_curr != elf_gp (isec->owner))
12656 {
12657 htab->toc_curr = elf_gp (isec->owner);
12658 htab->toc_first_sec = isec;
12659 }
12660 addr = (htab->toc_first_sec->output_offset
12661 + htab->toc_first_sec->output_section->vma);
06bcf541 12662 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12663 elf_gp (isec->owner) = off;
12664
0a1b45a2 12665 return true;
ad8e1ba5
AM
12666}
12667
927be08e
AM
12668/* Called via elf_link_hash_traverse to merge GOT entries for global
12669 symbol H. */
12670
0a1b45a2 12671static bool
927be08e
AM
12672merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12673{
12674 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12675 return true;
927be08e 12676
927be08e
AM
12677 merge_got_entries (&h->got.glist);
12678
0a1b45a2 12679 return true;
927be08e
AM
12680}
12681
12682/* Called via elf_link_hash_traverse to allocate GOT entries for global
12683 symbol H. */
12684
0a1b45a2 12685static bool
927be08e
AM
12686reallocate_got (struct elf_link_hash_entry *h, void *inf)
12687{
12688 struct got_entry *gent;
12689
12690 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12691 return true;
927be08e 12692
927be08e
AM
12693 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12694 if (!gent->is_indirect)
12695 allocate_got (h, (struct bfd_link_info *) inf, gent);
0a1b45a2 12696 return true;
927be08e
AM
12697}
12698
12699/* Called on the first multitoc pass after the last call to
12700 ppc64_elf_next_toc_section. This function removes duplicate GOT
12701 entries. */
12702
0a1b45a2 12703bool
927be08e 12704ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12705{
12706 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e 12707 struct bfd *ibfd, *ibfd2;
0a1b45a2 12708 bool done_something;
927be08e
AM
12709
12710 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12711
7865406b 12712 if (!htab->do_multi_toc)
0a1b45a2 12713 return false;
7865406b 12714
d0fae19d 12715 /* Merge global sym got entries within a toc group. */
927be08e
AM
12716 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12717
12718 /* And tlsld_got. */
c72f2fb2 12719 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12720 {
12721 struct got_entry *ent, *ent2;
12722
12723 if (!is_ppc64_elf (ibfd))
12724 continue;
12725
12726 ent = ppc64_tlsld_got (ibfd);
12727 if (!ent->is_indirect
12728 && ent->got.offset != (bfd_vma) -1)
12729 {
c72f2fb2 12730 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12731 {
12732 if (!is_ppc64_elf (ibfd2))
12733 continue;
12734
12735 ent2 = ppc64_tlsld_got (ibfd2);
12736 if (!ent2->is_indirect
12737 && ent2->got.offset != (bfd_vma) -1
12738 && elf_gp (ibfd2) == elf_gp (ibfd))
12739 {
0a1b45a2 12740 ent2->is_indirect = true;
927be08e
AM
12741 ent2->got.ent = ent;
12742 }
12743 }
12744 }
12745 }
12746
12747 /* Zap sizes of got sections. */
33e44f2e
AM
12748 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12749 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12750 htab->got_reli_size = 0;
12751
c72f2fb2 12752 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12753 {
12754 asection *got, *relgot;
12755
12756 if (!is_ppc64_elf (ibfd))
12757 continue;
12758
12759 got = ppc64_elf_tdata (ibfd)->got;
12760 if (got != NULL)
12761 {
12762 got->rawsize = got->size;
12763 got->size = 0;
12764 relgot = ppc64_elf_tdata (ibfd)->relgot;
12765 relgot->rawsize = relgot->size;
12766 relgot->size = 0;
12767 }
12768 }
12769
12770 /* Now reallocate the got, local syms first. We don't need to
12771 allocate section contents again since we never increase size. */
c72f2fb2 12772 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12773 {
12774 struct got_entry **lgot_ents;
12775 struct got_entry **end_lgot_ents;
12776 struct plt_entry **local_plt;
12777 struct plt_entry **end_local_plt;
f961d9dd 12778 unsigned char *lgot_masks;
927be08e
AM
12779 bfd_size_type locsymcount;
12780 Elf_Internal_Shdr *symtab_hdr;
19e08130 12781 asection *s;
927be08e
AM
12782
12783 if (!is_ppc64_elf (ibfd))
12784 continue;
12785
12786 lgot_ents = elf_local_got_ents (ibfd);
12787 if (!lgot_ents)
12788 continue;
12789
12790 symtab_hdr = &elf_symtab_hdr (ibfd);
12791 locsymcount = symtab_hdr->sh_info;
12792 end_lgot_ents = lgot_ents + locsymcount;
12793 local_plt = (struct plt_entry **) end_lgot_ents;
12794 end_local_plt = local_plt + locsymcount;
f961d9dd 12795 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12796 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12797 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12798 {
12799 struct got_entry *ent;
12800
12801 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12802 {
19e08130
AM
12803 unsigned int ent_size = 8;
12804 unsigned int rel_size = sizeof (Elf64_External_Rela);
12805
d0fae19d
AM
12806 ent->got.offset = s->size;
12807 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12808 {
19e08130
AM
12809 ent_size *= 2;
12810 rel_size *= 2;
12811 }
12812 s->size += ent_size;
37da22e5 12813 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12814 {
33e44f2e 12815 htab->elf.irelplt->size += rel_size;
19e08130
AM
12816 htab->got_reli_size += rel_size;
12817 }
6a3858a6 12818 else if (bfd_link_pic (info)
8f22c953
AM
12819 && (ent->tls_type == 0
12820 ? !info->enable_dt_relr
12821 : !bfd_link_executable (info)))
19e08130
AM
12822 {
12823 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12824 srel->size += rel_size;
d0fae19d
AM
12825 }
12826 }
927be08e
AM
12827 }
12828 }
12829
12830 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12831
c72f2fb2 12832 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12833 {
12834 struct got_entry *ent;
12835
12836 if (!is_ppc64_elf (ibfd))
12837 continue;
12838
12839 ent = ppc64_tlsld_got (ibfd);
12840 if (!ent->is_indirect
12841 && ent->got.offset != (bfd_vma) -1)
12842 {
12843 asection *s = ppc64_elf_tdata (ibfd)->got;
12844 ent->got.offset = s->size;
12845 s->size += 16;
f749f26e 12846 if (bfd_link_dll (info))
927be08e
AM
12847 {
12848 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12849 srel->size += sizeof (Elf64_External_Rela);
12850 }
12851 }
12852 }
12853
33e44f2e 12854 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12855 if (!done_something)
c72f2fb2 12856 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12857 {
12858 asection *got;
12859
12860 if (!is_ppc64_elf (ibfd))
12861 continue;
12862
12863 got = ppc64_elf_tdata (ibfd)->got;
12864 if (got != NULL)
12865 {
12866 done_something = got->rawsize != got->size;
12867 if (done_something)
12868 break;
12869 }
12870 }
12871
12872 if (done_something)
e7d1c40c 12873 (*htab->params->layout_sections_again) ();
927be08e
AM
12874
12875 /* Set up for second pass over toc sections to recalculate elf_gp
12876 on input sections. */
12877 htab->toc_bfd = NULL;
12878 htab->toc_first_sec = NULL;
0a1b45a2 12879 htab->second_toc_pass = true;
927be08e
AM
12880 return done_something;
12881}
12882
12883/* Called after second pass of multitoc partitioning. */
12884
12885void
12886ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12887{
12888 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12889
12890 /* After the second pass, toc_curr tracks the TOC offset used
12891 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12892 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12893}
12894
9b5ecbd0
AM
12895/* No toc references were found in ISEC. If the code in ISEC makes no
12896 calls, then there's no need to use toc adjusting stubs when branching
12897 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12898 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12899 needed, and 2 if a cyclical call-graph was found but no other reason
12900 for a stub was detected. If called from the top level, a return of
12901 2 means the same as a return of 0. */
9b5ecbd0
AM
12902
12903static int
4ce794b7 12904toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12905{
9b5ecbd0 12906 int ret;
70cc837d
AM
12907
12908 /* Mark this section as checked. */
12909 isec->call_check_done = 1;
9b5ecbd0 12910
772119ce
AM
12911 /* We know none of our code bearing sections will need toc stubs. */
12912 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12913 return 0;
12914
eea6121a 12915 if (isec->size == 0)
082c50f8
AM
12916 return 0;
12917
4c52953f
AM
12918 if (isec->output_section == NULL)
12919 return 0;
12920
4c52953f 12921 ret = 0;
70cc837d 12922 if (isec->reloc_count != 0)
9b5ecbd0 12923 {
70cc837d
AM
12924 Elf_Internal_Rela *relstart, *rel;
12925 Elf_Internal_Sym *local_syms;
12926 struct ppc_link_hash_table *htab;
2917689a 12927
70cc837d
AM
12928 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12929 info->keep_memory);
12930 if (relstart == NULL)
12931 return -1;
90aecf7a 12932
70cc837d
AM
12933 /* Look for branches to outside of this section. */
12934 local_syms = NULL;
12935 htab = ppc_hash_table (info);
12936 if (htab == NULL)
12937 return -1;
4c52953f 12938
70cc837d 12939 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12940 {
70cc837d
AM
12941 enum elf_ppc64_reloc_type r_type;
12942 unsigned long r_symndx;
12943 struct elf_link_hash_entry *h;
12944 struct ppc_link_hash_entry *eh;
12945 Elf_Internal_Sym *sym;
12946 asection *sym_sec;
12947 struct _opd_sec_data *opd;
12948 bfd_vma sym_value;
12949 bfd_vma dest;
12950
12951 r_type = ELF64_R_TYPE (rel->r_info);
12952 if (r_type != R_PPC64_REL24
05d0e962 12953 && r_type != R_PPC64_REL24_NOTOC
7aba54da 12954 && r_type != R_PPC64_REL24_P9NOTOC
70cc837d
AM
12955 && r_type != R_PPC64_REL14
12956 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12957 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12958 && r_type != R_PPC64_PLTCALL
12959 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12960 continue;
4c52953f 12961
70cc837d
AM
12962 r_symndx = ELF64_R_SYM (rel->r_info);
12963 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12964 isec->owner))
4c52953f 12965 {
70cc837d
AM
12966 ret = -1;
12967 break;
12968 }
4c52953f 12969
70cc837d
AM
12970 /* Calls to dynamic lib functions go through a plt call stub
12971 that uses r2. */
ed7007c1 12972 eh = ppc_elf_hash_entry (h);
70cc837d
AM
12973 if (eh != NULL
12974 && (eh->elf.plt.plist != NULL
12975 || (eh->oh != NULL
12976 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12977 {
12978 ret = 1;
12979 break;
4c52953f
AM
12980 }
12981
70cc837d
AM
12982 if (sym_sec == NULL)
12983 /* Ignore other undefined symbols. */
4c52953f 12984 continue;
4c52953f 12985
70cc837d
AM
12986 /* Assume branches to other sections not included in the
12987 link need stubs too, to cover -R and absolute syms. */
12988 if (sym_sec->output_section == NULL)
12989 {
12990 ret = 1;
12991 break;
12992 }
4c52953f 12993
70cc837d
AM
12994 if (h == NULL)
12995 sym_value = sym->st_value;
12996 else
12997 {
12998 if (h->root.type != bfd_link_hash_defined
12999 && h->root.type != bfd_link_hash_defweak)
13000 abort ();
13001 sym_value = h->root.u.def.value;
13002 }
13003 sym_value += rel->r_addend;
4c52953f 13004
70cc837d
AM
13005 /* If this branch reloc uses an opd sym, find the code section. */
13006 opd = get_opd_info (sym_sec);
13007 if (opd != NULL)
13008 {
13009 if (h == NULL && opd->adjust != NULL)
13010 {
13011 long adjust;
4c52953f 13012
92a9c616 13013 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
13014 if (adjust == -1)
13015 /* Assume deleted functions won't ever be called. */
13016 continue;
13017 sym_value += adjust;
13018 }
4c52953f 13019
aef36ac1 13020 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13021 &sym_sec, NULL, false);
70cc837d
AM
13022 if (dest == (bfd_vma) -1)
13023 continue;
13024 }
13025 else
13026 dest = (sym_value
13027 + sym_sec->output_offset
13028 + sym_sec->output_section->vma);
4c52953f 13029
70cc837d
AM
13030 /* Ignore branch to self. */
13031 if (sym_sec == isec)
13032 continue;
4c52953f 13033
70cc837d
AM
13034 /* If the called function uses the toc, we need a stub. */
13035 if (sym_sec->has_toc_reloc
13036 || sym_sec->makes_toc_func_call)
4c52953f 13037 {
70cc837d 13038 ret = 1;
4c52953f
AM
13039 break;
13040 }
70cc837d
AM
13041
13042 /* Assume any branch that needs a long branch stub might in fact
13043 need a plt_branch stub. A plt_branch stub uses r2. */
13044 else if (dest - (isec->output_offset
13045 + isec->output_section->vma
6911b7dc
AM
13046 + rel->r_offset) + (1 << 25)
13047 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13048 ? h->other
13049 : sym->st_other))
4c52953f 13050 {
70cc837d
AM
13051 ret = 1;
13052 break;
13053 }
13054
13055 /* If calling back to a section in the process of being
13056 tested, we can't say for sure that no toc adjusting stubs
13057 are needed, so don't return zero. */
13058 else if (sym_sec->call_check_in_progress)
13059 ret = 2;
13060
13061 /* Branches to another section that itself doesn't have any TOC
13062 references are OK. Recursively call ourselves to check. */
13063 else if (!sym_sec->call_check_done)
13064 {
13065 int recur;
13066
13067 /* Mark current section as indeterminate, so that other
13068 sections that call back to current won't be marked as
13069 known. */
13070 isec->call_check_in_progress = 1;
13071 recur = toc_adjusting_stub_needed (info, sym_sec);
13072 isec->call_check_in_progress = 0;
13073
4c52953f
AM
13074 if (recur != 0)
13075 {
70cc837d
AM
13076 ret = recur;
13077 if (recur != 2)
13078 break;
4c52953f
AM
13079 }
13080 }
4c52953f 13081 }
70cc837d 13082
c9594989
AM
13083 if (elf_symtab_hdr (isec->owner).contents
13084 != (unsigned char *) local_syms)
70cc837d
AM
13085 free (local_syms);
13086 if (elf_section_data (isec)->relocs != relstart)
13087 free (relstart);
9b5ecbd0
AM
13088 }
13089
70cc837d
AM
13090 if ((ret & 1) == 0
13091 && isec->map_head.s != NULL
13092 && (strcmp (isec->output_section->name, ".init") == 0
13093 || strcmp (isec->output_section->name, ".fini") == 0))
13094 {
13095 if (isec->map_head.s->has_toc_reloc
13096 || isec->map_head.s->makes_toc_func_call)
13097 ret = 1;
13098 else if (!isec->map_head.s->call_check_done)
13099 {
13100 int recur;
13101 isec->call_check_in_progress = 1;
13102 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13103 isec->call_check_in_progress = 0;
13104 if (recur != 0)
13105 ret = recur;
13106 }
13107 }
13108
13109 if (ret == 1)
13110 isec->makes_toc_func_call = 1;
4c52953f 13111
9b5ecbd0
AM
13112 return ret;
13113}
13114
721956f4
AM
13115/* The linker repeatedly calls this function for each input section,
13116 in the order that input sections are linked into output sections.
13117 Build lists of input sections to determine groupings between which
13118 we may insert linker stubs. */
13119
0a1b45a2 13120bool
4ce794b7 13121ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
13122{
13123 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13124
4dfe6ac6 13125 if (htab == NULL)
0a1b45a2 13126 return false;
4dfe6ac6 13127
734b6cf9 13128 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 13129 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 13130 {
3d6f9012
AM
13131 /* This happens to make the list in reverse order,
13132 which is what we want. */
6f20ed8a
AM
13133 htab->sec_info[isec->id].u.list
13134 = htab->sec_info[isec->output_section->id].u.list;
13135 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 13136 }
ad8e1ba5 13137
4c52953f 13138 if (htab->multi_toc_needed)
9b5ecbd0 13139 {
8b974ba3
AM
13140 /* Analyse sections that aren't already flagged as needing a
13141 valid toc pointer. Exclude .fixup for the linux kernel.
13142 .fixup contains branches, but only back to the function that
13143 hit an exception. */
13144 if (!(isec->has_toc_reloc
13145 || (isec->flags & SEC_CODE) == 0
13146 || strcmp (isec->name, ".fixup") == 0
13147 || isec->call_check_done))
13148 {
13149 if (toc_adjusting_stub_needed (info, isec) < 0)
0a1b45a2 13150 return false;
8b974ba3
AM
13151 }
13152 /* Make all sections use the TOC assigned for this object file.
13153 This will be wrong for pasted sections; We fix that in
13154 check_pasted_section(). */
13155 if (elf_gp (isec->owner) != 0)
13156 htab->toc_curr = elf_gp (isec->owner);
13157 }
13158
6f20ed8a 13159 htab->sec_info[isec->id].toc_off = htab->toc_curr;
0a1b45a2 13160 return true;
721956f4
AM
13161}
13162
70cc837d
AM
13163/* Check that all .init and .fini sections use the same toc, if they
13164 have toc relocs. */
13165
0a1b45a2 13166static bool
70cc837d
AM
13167check_pasted_section (struct bfd_link_info *info, const char *name)
13168{
13169 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13170
13171 if (o != NULL)
13172 {
13173 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13174 bfd_vma toc_off = 0;
13175 asection *i;
13176
13177 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13178 if (i->has_toc_reloc)
13179 {
13180 if (toc_off == 0)
6f20ed8a
AM
13181 toc_off = htab->sec_info[i->id].toc_off;
13182 else if (toc_off != htab->sec_info[i->id].toc_off)
0a1b45a2 13183 return false;
70cc837d 13184 }
6683a28d
AM
13185
13186 if (toc_off == 0)
13187 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13188 if (i->makes_toc_func_call)
13189 {
6f20ed8a 13190 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13191 break;
13192 }
13193
70cc837d
AM
13194 /* Make sure the whole pasted function uses the same toc offset. */
13195 if (toc_off != 0)
13196 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13197 htab->sec_info[i->id].toc_off = toc_off;
70cc837d 13198 }
0a1b45a2 13199 return true;
70cc837d
AM
13200}
13201
0a1b45a2 13202bool
70cc837d
AM
13203ppc64_elf_check_init_fini (struct bfd_link_info *info)
13204{
0c80c644
SM
13205 bool ret1 = check_pasted_section (info, ".init");
13206 bool ret2 = check_pasted_section (info, ".fini");
13207
13208 return ret1 && ret2;
70cc837d
AM
13209}
13210
721956f4
AM
13211/* See whether we can group stub sections together. Grouping stub
13212 sections may result in fewer stubs. More importantly, we need to
13213 put all .init* and .fini* stubs at the beginning of the .init or
13214 .fini output sections respectively, because glibc splits the
13215 _init and _fini functions into multiple parts. Putting a stub in
13216 the middle of a function is not a good idea. */
13217
0a1b45a2 13218static bool
6f20ed8a 13219group_sections (struct bfd_link_info *info,
4ce794b7 13220 bfd_size_type stub_group_size,
0a1b45a2 13221 bool stubs_always_before_branch)
721956f4 13222{
6f20ed8a
AM
13223 struct ppc_link_hash_table *htab;
13224 asection *osec;
0a1b45a2 13225 bool suppress_size_errors;
7c8fe5c4 13226
6f20ed8a
AM
13227 htab = ppc_hash_table (info);
13228 if (htab == NULL)
0a1b45a2 13229 return false;
6f20ed8a 13230
0a1b45a2 13231 suppress_size_errors = false;
7c8fe5c4
AM
13232 if (stub_group_size == 1)
13233 {
13234 /* Default values. */
13235 if (stubs_always_before_branch)
09f92717 13236 stub_group_size = 0x1e00000;
7c8fe5c4 13237 else
09f92717 13238 stub_group_size = 0x1c00000;
0a1b45a2 13239 suppress_size_errors = true;
7c8fe5c4
AM
13240 }
13241
6f20ed8a 13242 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13243 {
6f20ed8a
AM
13244 asection *tail;
13245
13246 if (osec->id >= htab->sec_info_arr_size)
13247 continue;
13248
13249 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13250 while (tail != NULL)
721956f4 13251 {
734b6cf9
AM
13252 asection *curr;
13253 asection *prev;
13254 bfd_size_type total;
0a1b45a2 13255 bool big_sec;
734b6cf9 13256 bfd_vma curr_toc;
6f20ed8a 13257 struct map_stub *group;
09f92717 13258 bfd_size_type group_size;
734b6cf9
AM
13259
13260 curr = tail;
eea6121a 13261 total = tail->size;
09f92717
AM
13262 group_size = (ppc64_elf_section_data (tail) != NULL
13263 && ppc64_elf_section_data (tail)->has_14bit_branch
13264 ? stub_group_size >> 10 : stub_group_size);
13265
13266 big_sec = total > group_size;
7c8fe5c4 13267 if (big_sec && !suppress_size_errors)
695344c0 13268 /* xgettext:c-format */
871b3ab2 13269 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13270 tail->owner, tail);
6f20ed8a 13271 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13272
6f20ed8a 13273 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13274 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13275 < (ppc64_elf_section_data (prev) != NULL
13276 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13277 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13278 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13279 curr = prev;
13280
13281 /* OK, the size from the start of CURR to the end is less
09f92717 13282 than group_size and thus can be handled by one stub
734b6cf9 13283 section. (or the tail section is itself larger than
09f92717
AM
13284 group_size, in which case we may be toast.) We should
13285 really be keeping track of the total size of stubs added
13286 here, as stubs contribute to the final output section
13287 size. That's a little tricky, and this way will only
13288 break if stubs added make the total size more than 2^25,
13289 ie. for the default stub_group_size, if stubs total more
13290 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13291 group = bfd_alloc (curr->owner, sizeof (*group));
13292 if (group == NULL)
0a1b45a2 13293 return false;
6f20ed8a
AM
13294 group->link_sec = curr;
13295 group->stub_sec = NULL;
a4b6fadd 13296 group->needs_save_res = 0;
df136d64
AM
13297 group->lr_restore = 0;
13298 group->eh_size = 0;
13299 group->eh_base = 0;
a4b6fadd
AM
13300 group->next = htab->group;
13301 htab->group = group;
734b6cf9 13302 do
721956f4 13303 {
6f20ed8a 13304 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13305 /* Set up this stub group. */
6f20ed8a 13306 htab->sec_info[tail->id].u.group = group;
721956f4 13307 }
734b6cf9
AM
13308 while (tail != curr && (tail = prev) != NULL);
13309
09f92717 13310 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13311 bytes before the stub section can be handled by it too.
13312 Don't do this if we have a really large section after the
13313 stubs, as adding more stubs increases the chance that
13314 branches may not reach into the stub section. */
13315 if (!stubs_always_before_branch && !big_sec)
13316 {
13317 total = 0;
13318 while (prev != NULL
13319 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13320 < (ppc64_elf_section_data (prev) != NULL
13321 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13322 ? (group_size = stub_group_size >> 10)
13323 : group_size))
6f20ed8a 13324 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13325 {
13326 tail = prev;
6f20ed8a
AM
13327 prev = htab->sec_info[tail->id].u.list;
13328 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13329 }
13330 }
13331 tail = prev;
721956f4
AM
13332 }
13333 }
0a1b45a2 13334 return true;
721956f4
AM
13335}
13336
58d180e8
AM
13337static const unsigned char glink_eh_frame_cie[] =
13338{
13339 0, 0, 0, 16, /* length. */
13340 0, 0, 0, 0, /* id. */
13341 1, /* CIE version. */
13342 'z', 'R', 0, /* Augmentation string. */
13343 4, /* Code alignment. */
13344 0x78, /* Data alignment. */
13345 65, /* RA reg. */
13346 1, /* Augmentation size. */
13347 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13348 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13349};
13350
d969d15f
AM
13351/* Stripping output sections is normally done before dynamic section
13352 symbols have been allocated. This function is called later, and
13353 handles cases like htab->brlt which is mapped to its own output
13354 section. */
13355
13356static void
13357maybe_strip_output (struct bfd_link_info *info, asection *isec)
13358{
13359 if (isec->size == 0
13360 && isec->output_section->size == 0
53d8967a 13361 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13362 && !bfd_section_removed_from_list (info->output_bfd,
13363 isec->output_section)
13364 && elf_section_data (isec->output_section)->dynindx == 0)
13365 {
13366 isec->output_section->flags |= SEC_EXCLUDE;
13367 bfd_section_list_remove (info->output_bfd, isec->output_section);
13368 info->output_bfd->section_count--;
13369 }
13370}
13371
1657026c
AM
13372static int
13373compare_relr_address (const void *arg1, const void *arg2)
13374{
13375 bfd_vma a = *(bfd_vma *) arg1;
13376 bfd_vma b = *(bfd_vma *) arg2;
13377 return a < b ? -1 : a > b ? 1 : 0;
13378}
13379
13380static bool
13381append_relr_off (struct ppc_link_hash_table *htab, bfd_vma off)
13382{
13383 if (htab->relr_count >= htab->relr_alloc)
13384 {
13385 if (htab->relr_alloc == 0)
13386 htab->relr_alloc = 4096;
13387 else
13388 htab->relr_alloc *= 2;
13389 htab->relr_addr
13390 = bfd_realloc (htab->relr_addr,
13391 htab->relr_alloc * sizeof (htab->relr_addr[0]));
13392 if (htab->relr_addr == NULL)
13393 return false;
13394 }
13395 htab->relr_addr[htab->relr_count++] = off;
13396 return true;
13397}
13398
13399static bool
13400got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13401{
13402 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13403 bfd *ibfd;
13404
13405 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13406 {
13407 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13408 struct plt_entry **local_plt, **lplt, **end_local_plt;
13409 Elf_Internal_Shdr *symtab_hdr;
13410 bfd_size_type locsymcount;
13411 Elf_Internal_Sym *local_syms = NULL;
13412 struct plt_entry *pent;
13413 struct got_entry *gent;
13414
13415 if (!is_ppc64_elf (ibfd))
13416 continue;
13417
13418 lgot_ents = elf_local_got_ents (ibfd);
13419 if (!lgot_ents)
13420 continue;
13421
13422 symtab_hdr = &elf_symtab_hdr (ibfd);
13423 locsymcount = symtab_hdr->sh_info;
13424 end_lgot_ents = lgot_ents + locsymcount;
13425 local_plt = (struct plt_entry **) end_lgot_ents;
13426 end_local_plt = local_plt + locsymcount;
13427 for (lgot = lgot_ents; lgot < end_lgot_ents; ++lgot)
13428 for (gent = *lgot; gent != NULL; gent = gent->next)
13429 if (!gent->is_indirect
13430 && gent->tls_type == 0
13431 && gent->got.offset != (bfd_vma) -1)
13432 {
13433 asection *got = ppc64_elf_tdata (gent->owner)->got;
13434 bfd_vma r_offset = (got->output_section->vma
13435 + got->output_offset
13436 + gent->got.offset);
13437 if (!append_relr_off (htab, r_offset))
13438 {
13439 htab->stub_error = true;
13440 return false;
13441 }
13442 }
13443
13444 if (!htab->opd_abi)
13445 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13446 for (pent = *lplt; pent != NULL; pent = pent->next)
13447 if (pent->plt.offset != (bfd_vma) -1)
13448 {
13449 Elf_Internal_Sym *sym;
13450
13451 if (!get_sym_h (NULL, &sym, NULL, NULL, &local_syms,
13452 lplt - local_plt, ibfd))
13453 {
13454 err_exit:
13455 if (symtab_hdr->contents != (unsigned char *) local_syms)
13456 free (local_syms);
13457 return false;
13458 }
13459
13460 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13461 {
13462 bfd_vma r_offset = (pent->plt.offset
13463 + htab->pltlocal->output_offset
13464 + htab->pltlocal->output_section->vma);
13465 if (!append_relr_off (htab, r_offset))
13466 goto err_exit;
13467 }
13468 }
13469
13470 if (local_syms != NULL
13471 && symtab_hdr->contents != (unsigned char *) local_syms)
13472 {
13473 if (!info->keep_memory)
13474 free (local_syms);
13475 else
13476 symtab_hdr->contents = (unsigned char *) local_syms;
13477 }
13478 }
13479 return true;
13480}
13481
13482static bool
13483got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13484{
13485 struct bfd_link_info *info;
13486 struct ppc_link_hash_table *htab;
13487 struct plt_entry *pent;
13488 struct got_entry *gent;
13489
13490 if (h->root.type == bfd_link_hash_indirect)
13491 return true;
13492
13493 info = (struct bfd_link_info *) inf;
13494 htab = ppc_hash_table (info);
13495 if (htab == NULL)
13496 return false;
13497
13498 if (h->type != STT_GNU_IFUNC
13499 && h->def_regular
13500 && (h->root.type == bfd_link_hash_defined
13501 || h->root.type == bfd_link_hash_defweak))
13502 {
13503 if (!htab->elf.dynamic_sections_created
13504 || h->dynindx == -1
13505 || SYMBOL_REFERENCES_LOCAL (info, h))
13506 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13507 if (!gent->is_indirect
13508 && gent->tls_type == 0
13509 && gent->got.offset != (bfd_vma) -1)
13510 {
13511 asection *got = ppc64_elf_tdata (gent->owner)->got;
13512 bfd_vma r_offset = (got->output_section->vma
13513 + got->output_offset
13514 + gent->got.offset);
13515 if (!append_relr_off (htab, r_offset))
13516 {
13517 htab->stub_error = true;
13518 return false;
13519 }
13520 }
13521
13522 if (!htab->opd_abi
13523 && use_local_plt (info, h))
13524 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13525 if (pent->plt.offset != (bfd_vma) -1)
13526 {
13527 bfd_vma r_offset = (htab->pltlocal->output_section->vma
13528 + htab->pltlocal->output_offset
13529 + pent->plt.offset);
13530 if (!append_relr_off (htab, r_offset))
13531 {
13532 htab->stub_error = true;
13533 return false;
13534 }
13535 }
13536 }
13537 return true;
13538}
13539
721956f4
AM
13540/* Determine and set the size of the stub section for a final link.
13541
13542 The basic idea here is to examine all the relocations looking for
13543 PC-relative calls to a target that is unreachable with a "bl"
13544 instruction. */
13545
0a1b45a2 13546bool
e7d1c40c 13547ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13548{
13549 bfd_size_type stub_group_size;
0a1b45a2 13550 bool stubs_always_before_branch;
721956f4
AM
13551 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13552
4dfe6ac6 13553 if (htab == NULL)
0a1b45a2 13554 return false;
4dfe6ac6 13555
e10a07b3
AM
13556 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13557 htab->params->power10_stubs = 0;
13558
0e1862bb 13559 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13560 htab->params->plt_thread_safe = 1;
b9e5796b 13561 if (!htab->opd_abi)
e7d1c40c
AM
13562 htab->params->plt_thread_safe = 0;
13563 else if (htab->params->plt_thread_safe == -1)
794e51c0 13564 {
e2458743 13565 static const char *const thread_starter[] =
794e51c0
AM
13566 {
13567 "pthread_create",
13568 /* libstdc++ */
13569 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13570 /* librt */
13571 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13572 "mq_notify", "create_timer",
13573 /* libanl */
13574 "getaddrinfo_a",
13575 /* libgomp */
2300b5a1 13576 "GOMP_parallel",
794e51c0 13577 "GOMP_parallel_start",
2300b5a1 13578 "GOMP_parallel_loop_static",
794e51c0 13579 "GOMP_parallel_loop_static_start",
2300b5a1 13580 "GOMP_parallel_loop_dynamic",
794e51c0 13581 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13582 "GOMP_parallel_loop_guided",
794e51c0 13583 "GOMP_parallel_loop_guided_start",
2300b5a1 13584 "GOMP_parallel_loop_runtime",
794e51c0 13585 "GOMP_parallel_loop_runtime_start",
2300b5a1 13586 "GOMP_parallel_sections",
68ffbac6 13587 "GOMP_parallel_sections_start",
f9dffbf0
AM
13588 /* libgo */
13589 "__go_go",
794e51c0
AM
13590 };
13591 unsigned i;
13592
a4b6fadd 13593 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13594 {
13595 struct elf_link_hash_entry *h;
13596 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
0a1b45a2 13597 false, false, true);
e7d1c40c
AM
13598 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13599 if (htab->params->plt_thread_safe)
794e51c0
AM
13600 break;
13601 }
13602 }
e7d1c40c
AM
13603 stubs_always_before_branch = htab->params->group_size < 0;
13604 if (htab->params->group_size < 0)
13605 stub_group_size = -htab->params->group_size;
721956f4 13606 else
e7d1c40c 13607 stub_group_size = htab->params->group_size;
721956f4 13608
6f20ed8a 13609 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
0a1b45a2 13610 return false;
721956f4 13611
a804e476
AM
13612 htab->tga_group = NULL;
13613 if (!htab->params->no_tls_get_addr_regsave
13614 && htab->tga_desc_fd != NULL
13615 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13616 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13617 && htab->tls_get_addr_fd != NULL
13618 && is_static_defined (&htab->tls_get_addr_fd->elf))
13619 {
13620 asection *sym_sec, *code_sec, *stub_sec;
13621 bfd_vma sym_value;
13622 struct _opd_sec_data *opd;
13623
13624 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13625 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13626 code_sec = sym_sec;
13627 opd = get_opd_info (sym_sec);
13628 if (opd != NULL)
0a1b45a2 13629 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
a804e476
AM
13630 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13631 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13632 htab->tga_group->link_sec);
13633 if (stub_sec == NULL)
0a1b45a2 13634 return false;
a804e476
AM
13635 htab->tga_group->stub_sec = stub_sec;
13636
13637 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13638 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13639 htab->tga_desc_fd->elf.root.u.def.value = 0;
13640 htab->tga_desc_fd->elf.type = STT_FUNC;
13641 htab->tga_desc_fd->elf.def_regular = 1;
13642 htab->tga_desc_fd->elf.non_elf = 0;
0a1b45a2 13643 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
a804e476
AM
13644 }
13645
c9301e31
AM
13646#define STUB_SHRINK_ITER 20
13647 /* Loop until no stubs added. After iteration 20 of this loop we may
13648 exit on a stub section shrinking. This is to break out of a
13649 pathological case where adding stubs on one iteration decreases
13650 section gaps (perhaps due to alignment), which then requires
13651 fewer or smaller stubs on the next iteration. */
13652
721956f4
AM
13653 while (1)
13654 {
13655 bfd *input_bfd;
13656 unsigned int bfd_indx;
a4b6fadd 13657 struct map_stub *group;
721956f4
AM
13658
13659 htab->stub_iteration += 1;
1657026c 13660 htab->relr_count = 0;
721956f4
AM
13661
13662 for (input_bfd = info->input_bfds, bfd_indx = 0;
13663 input_bfd != NULL;
c72f2fb2 13664 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13665 {
13666 Elf_Internal_Shdr *symtab_hdr;
13667 asection *section;
6cdc0ccc 13668 Elf_Internal_Sym *local_syms = NULL;
721956f4 13669
0c8d6e5c 13670 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13671 continue;
13672
721956f4 13673 /* We'll need the symbol table in a second. */
0ffa91dd 13674 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13675 if (symtab_hdr->sh_info == 0)
13676 continue;
13677
721956f4
AM
13678 /* Walk over each section attached to the input bfd. */
13679 for (section = input_bfd->sections;
13680 section != NULL;
13681 section = section->next)
13682 {
721956f4 13683 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1657026c 13684 bool is_opd;
721956f4
AM
13685
13686 /* If there aren't any relocs, then there's nothing more
13687 to do. */
13688 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13689 || (section->flags & SEC_ALLOC) == 0
13690 || (section->flags & SEC_LOAD) == 0
721956f4
AM
13691 || section->reloc_count == 0)
13692 continue;
13693
1657026c
AM
13694 if (!info->enable_dt_relr
13695 && (section->flags & SEC_CODE) == 0)
13696 continue;
13697
721956f4
AM
13698 /* If this section is a link-once section that will be
13699 discarded, then don't create any stubs. */
13700 if (section->output_section == NULL
927be08e 13701 || section->output_section->owner != info->output_bfd)
721956f4
AM
13702 continue;
13703
1e2f5b6e
AM
13704 /* Get the relocs. */
13705 internal_relocs
4ce794b7 13706 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13707 info->keep_memory);
721956f4 13708 if (internal_relocs == NULL)
1e2f5b6e 13709 goto error_ret_free_local;
721956f4 13710
1657026c
AM
13711 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13712
721956f4
AM
13713 /* Now examine each relocation. */
13714 irela = internal_relocs;
13715 irelaend = irela + section->reloc_count;
13716 for (; irela < irelaend; irela++)
13717 {
4ce794b7
AM
13718 enum elf_ppc64_reloc_type r_type;
13719 unsigned int r_indx;
7aba54da 13720 struct ppc_stub_type stub_type;
721956f4 13721 struct ppc_stub_hash_entry *stub_entry;
8387904d 13722 asection *sym_sec, *code_sec;
e054468f 13723 bfd_vma sym_value, code_value;
721956f4 13724 bfd_vma destination;
6911b7dc 13725 unsigned long local_off;
0a1b45a2 13726 bool ok_dest;
721956f4 13727 struct ppc_link_hash_entry *hash;
8387904d 13728 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13729 struct elf_link_hash_entry *h;
13730 Elf_Internal_Sym *sym;
721956f4
AM
13731 char *stub_name;
13732 const asection *id_sec;
74f0fb50 13733 struct _opd_sec_data *opd;
e054468f 13734 struct plt_entry *plt_ent;
721956f4
AM
13735
13736 r_type = ELF64_R_TYPE (irela->r_info);
13737 r_indx = ELF64_R_SYM (irela->r_info);
13738
4ce794b7 13739 if (r_type >= R_PPC64_max)
721956f4
AM
13740 {
13741 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13742 goto error_ret_free_internal;
721956f4
AM
13743 }
13744
13745 /* Only look for stubs on branch instructions. */
1657026c
AM
13746 switch (r_type)
13747 {
13748 default:
13749 continue;
13750
13751 case R_PPC64_REL24:
13752 case R_PPC64_REL24_NOTOC:
13753 case R_PPC64_REL24_P9NOTOC:
13754 case R_PPC64_REL14:
13755 case R_PPC64_REL14_BRTAKEN:
13756 case R_PPC64_REL14_BRNTAKEN:
13757 if ((section->flags & SEC_CODE) != 0)
13758 break;
13759 continue;
13760
13761 case R_PPC64_ADDR64:
13762 case R_PPC64_TOC:
13763 if (info->enable_dt_relr
13764 && irela->r_offset % 2 == 0
13765 && section->alignment_power != 0)
13766 break;
13767 continue;
13768 }
721956f4
AM
13769
13770 /* Now determine the call target, its name, value,
13771 section. */
411e1bfb
AM
13772 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13773 r_indx, input_bfd))
13774 goto error_ret_free_internal;
411e1bfb 13775
1657026c
AM
13776 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13777 {
13778 /* Only locally defined symbols can possibly use
13779 relative relocations. */
13780 bfd_vma r_offset;
13781 if ((sym_sec == NULL
13782 || sym_sec->output_section == NULL)
13783 /* No symbol is OK too. */
13784 && !(sym != NULL && sym->st_shndx == 0)
13785 /* Hack for __ehdr_start, which is undefined
13786 at this point. */
13787 && !(h != NULL && h->root.linker_def))
13788 continue;
13789 if (NO_OPD_RELOCS && is_opd)
13790 continue;
13791 if (!is_opd
13792 && r_type == R_PPC64_ADDR64)
13793 {
13794 if (h != NULL
13795 ? h->type == STT_GNU_IFUNC
13796 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13797 continue;
13798 if (h != NULL
13799 && !SYMBOL_REFERENCES_LOCAL (info, h))
13800 continue;
13801 }
13802 r_offset = _bfd_elf_section_offset (info->output_bfd,
13803 info,
13804 section,
13805 irela->r_offset);
13806 if (r_offset >= (bfd_vma) -2)
13807 continue;
13808 r_offset += (section->output_section->vma
13809 + section->output_offset);
13810 if (!append_relr_off (htab, r_offset))
13811 goto error_ret_free_internal;
13812 continue;
13813 }
13814
13815 hash = ppc_elf_hash_entry (h);
0a1b45a2 13816 ok_dest = false;
8387904d 13817 fdh = NULL;
7fe2b9a6 13818 sym_value = 0;
411e1bfb 13819 if (hash == NULL)
721956f4 13820 {
411e1bfb 13821 sym_value = sym->st_value;
c27b8c2a
AM
13822 if (sym_sec != NULL
13823 && sym_sec->output_section != NULL)
0a1b45a2 13824 ok_dest = true;
721956f4 13825 }
7fe2b9a6
AM
13826 else if (hash->elf.root.type == bfd_link_hash_defined
13827 || hash->elf.root.type == bfd_link_hash_defweak)
13828 {
13829 sym_value = hash->elf.root.u.def.value;
13830 if (sym_sec->output_section != NULL)
0a1b45a2 13831 ok_dest = true;
7fe2b9a6
AM
13832 }
13833 else if (hash->elf.root.type == bfd_link_hash_undefweak
13834 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13835 {
99877b66 13836 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13837 use the func descriptor sym instead if it is
13838 defined. */
ceb1f1ef 13839 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13840 && hash->oh != NULL)
8387904d 13841 {
8c5b4e52 13842 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13843 if (fdh->elf.root.type == bfd_link_hash_defined
13844 || fdh->elf.root.type == bfd_link_hash_defweak)
13845 {
13846 sym_sec = fdh->elf.root.u.def.section;
13847 sym_value = fdh->elf.root.u.def.value;
13848 if (sym_sec->output_section != NULL)
0a1b45a2 13849 ok_dest = true;
8387904d 13850 }
99877b66
AM
13851 else
13852 fdh = NULL;
8387904d 13853 }
7fe2b9a6
AM
13854 }
13855 else
13856 {
13857 bfd_set_error (bfd_error_bad_value);
13858 goto error_ret_free_internal;
721956f4
AM
13859 }
13860
8843416a 13861 destination = 0;
6911b7dc 13862 local_off = 0;
8843416a
AM
13863 if (ok_dest)
13864 {
13865 sym_value += irela->r_addend;
13866 destination = (sym_value
13867 + sym_sec->output_offset
13868 + sym_sec->output_section->vma);
6911b7dc
AM
13869 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13870 ? hash->elf.other
13871 : sym->st_other);
8843416a
AM
13872 }
13873
8387904d 13874 code_sec = sym_sec;
e054468f 13875 code_value = sym_value;
74f0fb50
AM
13876 opd = get_opd_info (sym_sec);
13877 if (opd != NULL)
8387904d
AM
13878 {
13879 bfd_vma dest;
13880
74f0fb50 13881 if (hash == NULL && opd->adjust != NULL)
8387904d 13882 {
51aecdc5 13883 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13884 if (adjust == -1)
13885 continue;
e054468f 13886 code_value += adjust;
8387904d
AM
13887 sym_value += adjust;
13888 }
13889 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13890 &code_sec, &code_value, false);
8387904d
AM
13891 if (dest != (bfd_vma) -1)
13892 {
13893 destination = dest;
13894 if (fdh != NULL)
13895 {
13896 /* Fixup old ABI sym to point at code
13897 entry. */
99877b66 13898 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13899 hash->elf.root.u.def.section = code_sec;
e054468f 13900 hash->elf.root.u.def.value = code_value;
8387904d
AM
13901 }
13902 }
13903 }
13904
721956f4 13905 /* Determine what (if any) linker stub is needed. */
e054468f 13906 plt_ent = NULL;
7aba54da
AM
13907 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13908 &plt_ent, destination,
13909 local_off);
13910 stub_type.sub = ppc_stub_toc;
13911 stub_type.r2save = 0;
13912
13913 if (r_type == R_PPC64_REL24_NOTOC
13914 || r_type == R_PPC64_REL24_P9NOTOC)
05d0e962 13915 {
7aba54da
AM
13916 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13917 if (htab->params->power10_stubs == 0
13918 || (r_type == R_PPC64_REL24_P9NOTOC
13919 && htab->params->power10_stubs != 1))
13920 notoc = ppc_stub_p9notoc;
13921 if (stub_type.main == ppc_stub_plt_call)
13922 stub_type.sub = notoc;
13923 else if (stub_type.main == ppc_stub_long_branch
05d0e962
AM
13924 || (code_sec != NULL
13925 && code_sec->output_section != NULL
13926 && (((hash ? hash->elf.other : sym->st_other)
13927 & STO_PPC64_LOCAL_MASK)
4a4e7361 13928 > 1 << STO_PPC64_LOCAL_BIT)))
7aba54da
AM
13929 {
13930 stub_type.main = ppc_stub_long_branch;
13931 stub_type.sub = notoc;
13932 stub_type.r2save = 0;
13933 }
05d0e962 13934 }
7aba54da 13935 else if (stub_type.main != ppc_stub_plt_call)
ad8e1ba5
AM
13936 {
13937 /* Check whether we need a TOC adjusting stub.
13938 Since the linker pastes together pieces from
13939 different object files when creating the
13940 _init and _fini functions, it may be that a
13941 call to what looks like a local sym is in
13942 fact a call needing a TOC adjustment. */
33cb30a1
AM
13943 if ((code_sec != NULL
13944 && code_sec->output_section != NULL
33cb30a1 13945 && (code_sec->has_toc_reloc
918dc783
AM
13946 || code_sec->makes_toc_func_call)
13947 && (htab->sec_info[code_sec->id].toc_off
13948 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13949 || (((hash ? hash->elf.other : sym->st_other)
13950 & STO_PPC64_LOCAL_MASK)
13951 == 1 << STO_PPC64_LOCAL_BIT))
7aba54da
AM
13952 {
13953 stub_type.main = ppc_stub_long_branch;
13954 stub_type.sub = ppc_stub_toc;
13955 stub_type.r2save = 1;
13956 }
ad8e1ba5
AM
13957 }
13958
7aba54da 13959 if (stub_type.main == ppc_stub_none)
721956f4
AM
13960 continue;
13961
411e1bfb 13962 /* __tls_get_addr calls might be eliminated. */
7aba54da 13963 if (stub_type.main != ppc_stub_plt_call
411e1bfb 13964 && hash != NULL
ed7007c1 13965 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
13966 && section->has_tls_reloc
13967 && irela != internal_relocs)
13968 {
13969 /* Get tls info. */
f961d9dd 13970 unsigned char *tls_mask;
411e1bfb 13971
3a71aa26 13972 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13973 irela - 1, input_bfd))
13974 goto error_ret_free_internal;
abc489c6
AM
13975 if ((*tls_mask & TLS_TLS) != 0
13976 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
13977 continue;
13978 }
13979
7aba54da
AM
13980 if (stub_type.main == ppc_stub_plt_call
13981 && stub_type.sub == ppc_stub_toc)
794e51c0 13982 {
6e1816be
AM
13983 if (!htab->opd_abi
13984 && htab->params->plt_localentry0 != 0
13985 && is_elfv2_localentry0 (&hash->elf))
13986 htab->has_plt_localentry0 = 1;
13987 else if (irela + 1 < irelaend
13988 && irela[1].r_offset == irela->r_offset + 4
13989 && (ELF64_R_TYPE (irela[1].r_info)
13990 == R_PPC64_TOCSAVE))
f378ab09
AM
13991 {
13992 if (!tocsave_find (htab, INSERT,
13993 &local_syms, irela + 1, input_bfd))
13994 goto error_ret_free_internal;
13995 }
f378ab09 13996 else
7aba54da 13997 stub_type.r2save = 1;
794e51c0 13998 }
3b421ab3 13999
721956f4 14000 /* Support for grouping stub sections. */
6f20ed8a 14001 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
14002
14003 /* Get the name of this stub. */
14004 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14005 if (!stub_name)
14006 goto error_ret_free_internal;
14007
14008 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 14009 stub_name, false, false);
721956f4
AM
14010 if (stub_entry != NULL)
14011 {
721956f4 14012 free (stub_name);
7aba54da 14013 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
e10a07b3 14014 {
7aba54da
AM
14015 /* xgettext:c-format */
14016 _bfd_error_handler
14017 (_("%pB: cannot create stub entry %s"),
14018 section->owner, stub_entry->root.string);
14019 goto error_ret_free_internal;
e10a07b3 14020 }
721956f4
AM
14021 continue;
14022 }
14023
25f53a85 14024 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
14025 if (stub_entry == NULL)
14026 {
14027 free (stub_name);
6cdc0ccc
AM
14028 error_ret_free_internal:
14029 if (elf_section_data (section)->relocs == NULL)
14030 free (internal_relocs);
14031 error_ret_free_local:
c9594989
AM
14032 if (symtab_hdr->contents
14033 != (unsigned char *) local_syms)
6cdc0ccc 14034 free (local_syms);
0a1b45a2 14035 return false;
721956f4
AM
14036 }
14037
7aba54da
AM
14038 stub_entry->type = stub_type;
14039 if (stub_type.main == ppc_stub_plt_call)
e054468f 14040 {
05d0e962
AM
14041 stub_entry->target_value = sym_value;
14042 stub_entry->target_section = sym_sec;
e054468f
AM
14043 }
14044 else
14045 {
05d0e962
AM
14046 stub_entry->target_value = code_value;
14047 stub_entry->target_section = code_sec;
e054468f 14048 }
721956f4 14049 stub_entry->h = hash;
e054468f 14050 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
14051 stub_entry->symtype
14052 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 14053 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 14054
3d58e1fc
AM
14055 if (hash != NULL
14056 && (hash->elf.root.type == bfd_link_hash_defined
14057 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 14058 htab->stub_globals += 1;
721956f4
AM
14059 }
14060
14061 /* We're done with the internal relocs, free them. */
6cdc0ccc 14062 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 14063 free (internal_relocs);
721956f4 14064 }
6cdc0ccc
AM
14065
14066 if (local_syms != NULL
14067 && symtab_hdr->contents != (unsigned char *) local_syms)
14068 {
14069 if (!info->keep_memory)
14070 free (local_syms);
14071 else
14072 symtab_hdr->contents = (unsigned char *) local_syms;
14073 }
721956f4
AM
14074 }
14075
5c3dead3 14076 /* We may have added some stubs. Find out the new size of the
721956f4 14077 stub sections. */
d4aaa2a0 14078 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14079 {
14080 group->lr_restore = 0;
14081 group->eh_size = 0;
14082 if (group->stub_sec != NULL)
14083 {
14084 asection *stub_sec = group->stub_sec;
14085
14086 if (htab->stub_iteration <= STUB_SHRINK_ITER
14087 || stub_sec->rawsize < stub_sec->size)
14088 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
14089 stub_sec->rawsize = stub_sec->size;
14090 stub_sec->size = 0;
14091 stub_sec->reloc_count = 0;
14092 stub_sec->flags &= ~SEC_RELOC;
14093 }
14094 }
a804e476
AM
14095 if (htab->tga_group != NULL)
14096 {
14097 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14098 htab->tga_group->eh_size
14099 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14100 htab->tga_group->lr_restore = 23 * 4;
14101 htab->tga_group->stub_sec->size = 24 * 4;
14102 }
eea6121a 14103
ba21f564
AM
14104 if (htab->stub_iteration <= STUB_SHRINK_ITER
14105 || htab->brlt->rawsize < htab->brlt->size)
14106 htab->brlt->rawsize = htab->brlt->size;
eea6121a 14107 htab->brlt->size = 0;
84f5d08e
AM
14108 htab->brlt->reloc_count = 0;
14109 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 14110 if (htab->relbrlt != NULL)
eea6121a 14111 htab->relbrlt->size = 0;
721956f4 14112
1657026c
AM
14113 if (htab->elf.srelrdyn != NULL)
14114 {
14115 if (htab->stub_iteration <= STUB_SHRINK_ITER
14116 || htab->elf.srelrdyn->rawsize < htab->elf.srelrdyn->size)
14117 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14118 htab->elf.srelrdyn->size = 0;
14119 }
14120
63bc6f6c 14121 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 14122
a4b6fadd
AM
14123 for (group = htab->group; group != NULL; group = group->next)
14124 if (group->needs_save_res)
14125 group->stub_sec->size += htab->sfpr->size;
14126
176a0d42
AM
14127 if (info->emitrelocations
14128 && htab->glink != NULL && htab->glink->size != 0)
14129 {
14130 htab->glink->reloc_count = 1;
14131 htab->glink->flags |= SEC_RELOC;
14132 }
14133
58d180e8
AM
14134 if (htab->glink_eh_frame != NULL
14135 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 14136 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 14137 {
2e0ce1c8 14138 size_t size = 0, align = 4;
58d180e8 14139
d4aaa2a0 14140 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14141 if (group->eh_size != 0)
14142 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 14143 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 14144 size += (24 + align - 1) & -align;
58d180e8 14145 if (size != 0)
2e0ce1c8
AM
14146 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14147 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14148 size = (size + align - 1) & -align;
58d180e8
AM
14149 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14150 htab->glink_eh_frame->size = size;
14151 }
14152
e7d1c40c 14153 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14154 for (group = htab->group; group != NULL; group = group->next)
14155 if (group->stub_sec != NULL)
691d2e9a
AM
14156 {
14157 int align = abs (htab->params->plt_stub_align);
14158 group->stub_sec->size
14159 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14160 }
d4aaa2a0 14161
1657026c
AM
14162 if (htab->elf.srelrdyn != NULL)
14163 {
14164 bfd_vma r_offset;
14165
14166 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14167 if (!append_relr_off (htab, (r_offset
14168 + htab->brlt->output_section->vma
14169 + htab->brlt->output_offset)))
14170 return false;
14171
14172 if (!got_and_plt_relr_for_local_syms (info))
14173 return false;
14174 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14175 if (htab->stub_error)
14176 return false;
14177
14178 if (htab->relr_count > 1)
14179 qsort (htab->relr_addr, htab->relr_count, sizeof (*htab->relr_addr),
14180 compare_relr_address);
14181
14182 size_t i = 0;
14183 while (i < htab->relr_count)
14184 {
14185 bfd_vma base = htab->relr_addr[i];
14186 htab->elf.srelrdyn->size += 8;
14187 i++;
14188 /* Handle possible duplicate address. This can happen
14189 as sections increase in size when adding stubs. */
14190 while (i < htab->relr_count
14191 && htab->relr_addr[i] == base)
14192 i++;
14193 base += 8;
14194 while (1)
14195 {
14196 size_t start_i = i;
14197 while (i < htab->relr_count
14198 && htab->relr_addr[i] - base < 63 * 8
14199 && (htab->relr_addr[i] - base) % 8 == 0)
14200 i++;
14201 if (i == start_i)
14202 break;
14203 htab->elf.srelrdyn->size += 8;
14204 base += 63 * 8;
14205 }
14206 }
14207 }
14208
d4aaa2a0
AM
14209 for (group = htab->group; group != NULL; group = group->next)
14210 if (group->stub_sec != NULL
14211 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 14212 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 14213 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
14214 break;
14215
d4aaa2a0 14216 if (group == NULL
ba21f564
AM
14217 && (htab->brlt->rawsize == htab->brlt->size
14218 || (htab->stub_iteration > STUB_SHRINK_ITER
14219 && htab->brlt->rawsize > htab->brlt->size))
1657026c
AM
14220 && (htab->elf.srelrdyn == NULL
14221 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14222 || (htab->stub_iteration > STUB_SHRINK_ITER
14223 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
58d180e8 14224 && (htab->glink_eh_frame == NULL
a804e476
AM
14225 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14226 && (htab->tga_group == NULL
14227 || htab->stub_iteration > 1))
5c3dead3
AM
14228 break;
14229
721956f4 14230 /* Ask the linker to do its stuff. */
e7d1c40c 14231 (*htab->params->layout_sections_again) ();
721956f4
AM
14232 }
14233
da44f4e5
AM
14234 if (htab->glink_eh_frame != NULL
14235 && htab->glink_eh_frame->size != 0)
14236 {
14237 bfd_vma val;
14238 bfd_byte *p, *last_fde;
14239 size_t last_fde_len, size, align, pad;
d4aaa2a0 14240 struct map_stub *group;
da44f4e5 14241
df136d64
AM
14242 /* It is necessary to at least have a rough outline of the
14243 linker generated CIEs and FDEs written before
14244 bfd_elf_discard_info is run, in order for these FDEs to be
14245 indexed in .eh_frame_hdr. */
da44f4e5
AM
14246 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14247 if (p == NULL)
0a1b45a2 14248 return false;
da44f4e5
AM
14249 htab->glink_eh_frame->contents = p;
14250 last_fde = p;
2e0ce1c8 14251 align = 4;
da44f4e5
AM
14252
14253 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14254 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 14255 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 14256 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 14257 p += last_fde_len + 4;
da44f4e5 14258
d4aaa2a0 14259 for (group = htab->group; group != NULL; group = group->next)
df136d64 14260 if (group->eh_size != 0)
da44f4e5 14261 {
df136d64 14262 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 14263 last_fde = p;
df136d64 14264 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 14265 /* FDE length. */
2e0ce1c8 14266 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14267 p += 4;
14268 /* CIE pointer. */
14269 val = p - htab->glink_eh_frame->contents;
14270 bfd_put_32 (htab->elf.dynobj, val, p);
14271 p += 4;
14272 /* Offset to stub section, written later. */
14273 p += 4;
14274 /* stub section size. */
d4aaa2a0 14275 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
14276 p += 4;
14277 /* Augmentation. */
14278 p += 1;
df136d64
AM
14279 /* Make sure we don't have all nops. This is enough for
14280 elf-eh-frame.c to detect the last non-nop opcode. */
14281 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 14282 p = last_fde + last_fde_len + 4;
da44f4e5
AM
14283 }
14284 if (htab->glink != NULL && htab->glink->size != 0)
14285 {
14286 last_fde = p;
2e0ce1c8 14287 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 14288 /* FDE length. */
2e0ce1c8 14289 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14290 p += 4;
14291 /* CIE pointer. */
14292 val = p - htab->glink_eh_frame->contents;
14293 bfd_put_32 (htab->elf.dynobj, val, p);
14294 p += 4;
14295 /* Offset to .glink, written later. */
14296 p += 4;
14297 /* .glink size. */
14298 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14299 p += 4;
14300 /* Augmentation. */
14301 p += 1;
14302
3cd7c7d7 14303 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
14304 *p++ = DW_CFA_register;
14305 *p++ = 65;
9f08fa5c 14306 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 14307 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
14308 *p++ = DW_CFA_restore_extended;
14309 *p++ = 65;
2e0ce1c8 14310 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
14311 }
14312 /* Subsume any padding into the last FDE if user .eh_frame
14313 sections are aligned more than glink_eh_frame. Otherwise any
14314 zero padding will be seen as a terminator. */
2e0ce1c8 14315 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 14316 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 14317 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
14318 htab->glink_eh_frame->size = size + pad;
14319 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14320 }
14321
d969d15f 14322 maybe_strip_output (info, htab->brlt);
2efec98b
AM
14323 if (htab->relbrlt != NULL)
14324 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
14325 if (htab->glink_eh_frame != NULL)
14326 maybe_strip_output (info, htab->glink_eh_frame);
1657026c
AM
14327 if (htab->elf.srelrdyn != NULL)
14328 maybe_strip_output (info, htab->elf.srelrdyn);
721956f4 14329
0a1b45a2 14330 return true;
721956f4
AM
14331}
14332
14333/* Called after we have determined section placement. If sections
805fc799 14334 move, we'll be called again. Provide a value for TOCstart. */
721956f4 14335
805fc799 14336bfd_vma
1c865ab2 14337ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 14338{
805fc799 14339 asection *s;
a27e685f 14340 bfd_vma TOCstart, adjust;
721956f4 14341
43417696
AM
14342 if (info != NULL)
14343 {
14344 struct elf_link_hash_entry *h;
14345 struct elf_link_hash_table *htab = elf_hash_table (info);
14346
2cc15b10 14347 if (is_elf_hash_table (&htab->root)
43417696
AM
14348 && htab->hgot != NULL)
14349 h = htab->hgot;
14350 else
14351 {
2cc15b10
AM
14352 h = (struct elf_link_hash_entry *)
14353 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14354 if (is_elf_hash_table (&htab->root))
43417696
AM
14355 htab->hgot = h;
14356 }
14357 if (h != NULL
14358 && h->root.type == bfd_link_hash_defined
14359 && !h->root.linker_def
2cc15b10 14360 && (!is_elf_hash_table (&htab->root)
43417696
AM
14361 || h->def_regular))
14362 {
ed7007c1 14363 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
14364 _bfd_set_gp_value (obfd, TOCstart);
14365 return TOCstart;
14366 }
14367 }
14368
805fc799
AM
14369 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14370 order. The TOC starts where the first of these sections starts. */
14371 s = bfd_get_section_by_name (obfd, ".got");
e054468f 14372 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14373 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 14374 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14375 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 14376 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14377 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 14378 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
14379 {
14380 /* This may happen for
14381 o references to TOC base (SYM@toc / TOC[tc0]) without a
14382 .toc directive
14383 o bad linker script
14384 o --gc-sections and empty TOC sections
14385
14386 FIXME: Warn user? */
14387
14388 /* Look for a likely section. We probably won't even be
14389 using TOCstart. */
14390 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14391 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14392 | SEC_EXCLUDE))
805fc799
AM
14393 == (SEC_ALLOC | SEC_SMALL_DATA))
14394 break;
721956f4 14395 if (s == NULL)
805fc799 14396 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14397 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14398 == (SEC_ALLOC | SEC_SMALL_DATA))
14399 break;
721956f4 14400 if (s == NULL)
805fc799 14401 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14402 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14403 == SEC_ALLOC)
805fc799 14404 break;
721956f4 14405 if (s == NULL)
805fc799 14406 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14407 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14408 break;
14409 }
721956f4 14410
805fc799
AM
14411 TOCstart = 0;
14412 if (s != NULL)
14413 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14414
a27e685f
AM
14415 /* Force alignment. */
14416 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14417 TOCstart -= adjust;
1c865ab2
AM
14418 _bfd_set_gp_value (obfd, TOCstart);
14419
810d4e75 14420 if (info != NULL && s != NULL)
1c865ab2
AM
14421 {
14422 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14423
810d4e75
AM
14424 if (htab != NULL)
14425 {
14426 if (htab->elf.hgot != NULL)
14427 {
a27e685f 14428 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14429 htab->elf.hgot->root.u.def.section = s;
14430 }
14431 }
14432 else
1c865ab2 14433 {
810d4e75
AM
14434 struct bfd_link_hash_entry *bh = NULL;
14435 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f 14436 s, TOC_BASE_OFF - adjust,
0a1b45a2 14437 NULL, false, false, &bh);
1c865ab2
AM
14438 }
14439 }
805fc799 14440 return TOCstart;
721956f4
AM
14441}
14442
a345bc8d 14443/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14444 write out any global entry stubs, and PLT relocations. */
a345bc8d 14445
0a1b45a2 14446static bool
49c09209 14447build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14448{
14449 struct bfd_link_info *info;
14450 struct ppc_link_hash_table *htab;
49c09209 14451 struct plt_entry *ent;
a345bc8d
AM
14452 asection *s;
14453
14454 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 14455 return true;
a345bc8d 14456
49c09209
AM
14457 info = inf;
14458 htab = ppc_hash_table (info);
14459 if (htab == NULL)
0a1b45a2 14460 return false;
49c09209
AM
14461
14462 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14463 if (ent->plt.offset != (bfd_vma) -1)
14464 {
14465 /* This symbol has an entry in the procedure linkage
14466 table. Set it up. */
14467 Elf_Internal_Rela rela;
2d7ad24e 14468 asection *plt, *relplt;
49c09209
AM
14469 bfd_byte *loc;
14470
30845f11 14471 if (use_local_plt (info, h))
49c09209
AM
14472 {
14473 if (!(h->def_regular
14474 && (h->root.type == bfd_link_hash_defined
14475 || h->root.type == bfd_link_hash_defweak)))
14476 continue;
2d7ad24e
AM
14477 if (h->type == STT_GNU_IFUNC)
14478 {
14479 plt = htab->elf.iplt;
14480 relplt = htab->elf.irelplt;
0a1b45a2 14481 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14482 if (htab->opd_abi)
14483 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14484 else
14485 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14486 }
49c09209 14487 else
2d7ad24e
AM
14488 {
14489 plt = htab->pltlocal;
1657026c
AM
14490 relplt = NULL;
14491 if (bfd_link_pic (info)
14492 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
14493 {
14494 relplt = htab->relpltlocal;
14495 if (htab->opd_abi)
14496 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14497 else
14498 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14499 }
2d7ad24e 14500 }
ed7007c1 14501 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14502
14503 if (relplt == NULL)
14504 {
14505 loc = plt->contents + ent->plt.offset;
14506 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14507 if (htab->opd_abi)
14508 {
14509 bfd_vma toc = elf_gp (info->output_bfd);
14510 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14511 bfd_put_64 (info->output_bfd, toc, loc + 8);
14512 }
14513 }
14514 else
14515 {
14516 rela.r_offset = (plt->output_section->vma
14517 + plt->output_offset
14518 + ent->plt.offset);
14519 loc = relplt->contents + (relplt->reloc_count++
14520 * sizeof (Elf64_External_Rela));
14521 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14522 }
49c09209
AM
14523 }
14524 else
14525 {
14526 rela.r_offset = (htab->elf.splt->output_section->vma
14527 + htab->elf.splt->output_offset
14528 + ent->plt.offset);
14529 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14530 rela.r_addend = ent->addend;
14531 loc = (htab->elf.srelplt->contents
14532 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14533 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14534 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
0a1b45a2 14535 htab->elf.ifunc_resolvers = true;
2d7ad24e 14536 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14537 }
49c09209
AM
14538 }
14539
a345bc8d 14540 if (!h->pointer_equality_needed)
0a1b45a2 14541 return true;
a345bc8d
AM
14542
14543 if (h->def_regular)
0a1b45a2 14544 return true;
a345bc8d 14545
9e390558 14546 s = htab->global_entry;
49c09209 14547 if (s == NULL || s->size == 0)
0a1b45a2 14548 return true;
49c09209
AM
14549
14550 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14551 if (ent->plt.offset != (bfd_vma) -1
14552 && ent->addend == 0)
a345bc8d
AM
14553 {
14554 bfd_byte *p;
14555 asection *plt;
14556 bfd_vma off;
14557
a345bc8d 14558 p = s->contents + h->root.u.def.value;
33e44f2e 14559 plt = htab->elf.splt;
30845f11 14560 if (use_local_plt (info, h))
2d7ad24e
AM
14561 {
14562 if (h->type == STT_GNU_IFUNC)
14563 plt = htab->elf.iplt;
14564 else
14565 plt = htab->pltlocal;
14566 }
49c09209 14567 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14568 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14569
14570 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14571 {
14572 info->callbacks->einfo
c1c8c1ef 14573 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14574 h->root.root.string);
14575 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14576 htab->stub_error = true;
a345bc8d
AM
14577 }
14578
7341d5e2
AM
14579 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14580 if (htab->params->emit_stub_syms)
14581 {
14582 size_t len = strlen (h->root.root.string);
14583 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14584
14585 if (name == NULL)
0a1b45a2 14586 return false;
7341d5e2
AM
14587
14588 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
0a1b45a2 14589 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
7341d5e2 14590 if (h == NULL)
0a1b45a2 14591 return false;
7341d5e2
AM
14592 if (h->root.type == bfd_link_hash_new)
14593 {
14594 h->root.type = bfd_link_hash_defined;
14595 h->root.u.def.section = s;
14596 h->root.u.def.value = p - s->contents;
14597 h->ref_regular = 1;
14598 h->def_regular = 1;
14599 h->ref_regular_nonweak = 1;
14600 h->forced_local = 1;
14601 h->non_elf = 0;
2ec55de3 14602 h->root.linker_def = 1;
7341d5e2
AM
14603 }
14604 }
14605
a345bc8d
AM
14606 if (PPC_HA (off) != 0)
14607 {
14608 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14609 p += 4;
14610 }
14611 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14612 p += 4;
14613 bfd_put_32 (s->owner, MTCTR_R12, p);
14614 p += 4;
407aa07c 14615 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14616 break;
14617 }
0a1b45a2 14618 return true;
a345bc8d
AM
14619}
14620
49c09209
AM
14621/* Write PLT relocs for locals. */
14622
0a1b45a2 14623static bool
49c09209
AM
14624write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14625{
14626 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14627 bfd *ibfd;
14628
14629 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14630 {
14631 struct got_entry **lgot_ents, **end_lgot_ents;
14632 struct plt_entry **local_plt, **lplt, **end_local_plt;
14633 Elf_Internal_Shdr *symtab_hdr;
14634 bfd_size_type locsymcount;
14635 Elf_Internal_Sym *local_syms = NULL;
14636 struct plt_entry *ent;
14637
14638 if (!is_ppc64_elf (ibfd))
14639 continue;
14640
14641 lgot_ents = elf_local_got_ents (ibfd);
14642 if (!lgot_ents)
14643 continue;
14644
14645 symtab_hdr = &elf_symtab_hdr (ibfd);
14646 locsymcount = symtab_hdr->sh_info;
14647 end_lgot_ents = lgot_ents + locsymcount;
14648 local_plt = (struct plt_entry **) end_lgot_ents;
14649 end_local_plt = local_plt + locsymcount;
14650 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14651 for (ent = *lplt; ent != NULL; ent = ent->next)
14652 if (ent->plt.offset != (bfd_vma) -1)
14653 {
14654 Elf_Internal_Sym *sym;
14655 asection *sym_sec;
14656 asection *plt, *relplt;
14657 bfd_byte *loc;
14658 bfd_vma val;
49c09209
AM
14659
14660 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14661 lplt - local_plt, ibfd))
14662 {
c9594989 14663 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209 14664 free (local_syms);
0a1b45a2 14665 return false;
49c09209
AM
14666 }
14667
14668 val = sym->st_value + ent->addend;
49c09209
AM
14669 if (sym_sec != NULL && sym_sec->output_section != NULL)
14670 val += sym_sec->output_offset + sym_sec->output_section->vma;
14671
2d7ad24e
AM
14672 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14673 {
0a1b45a2 14674 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14675 plt = htab->elf.iplt;
14676 relplt = htab->elf.irelplt;
14677 }
14678 else
14679 {
14680 plt = htab->pltlocal;
1657026c
AM
14681 relplt = NULL;
14682 if (bfd_link_pic (info)
14683 && !(info->enable_dt_relr && !htab->opd_abi))
14684 relplt = htab->relpltlocal;
2d7ad24e 14685 }
49c09209 14686
2d7ad24e
AM
14687 if (relplt == NULL)
14688 {
14689 loc = plt->contents + ent->plt.offset;
14690 bfd_put_64 (info->output_bfd, val, loc);
14691 if (htab->opd_abi)
14692 {
14693 bfd_vma toc = elf_gp (ibfd);
14694 bfd_put_64 (info->output_bfd, toc, loc + 8);
14695 }
14696 }
49c09209 14697 else
2d7ad24e
AM
14698 {
14699 Elf_Internal_Rela rela;
14700 rela.r_offset = (ent->plt.offset
14701 + plt->output_offset
14702 + plt->output_section->vma);
14703 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14704 {
14705 if (htab->opd_abi)
14706 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14707 else
14708 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14709 }
14710 else
14711 {
14712 if (htab->opd_abi)
14713 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14714 else
14715 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14716 }
14717 rela.r_addend = val;
14718 loc = relplt->contents + (relplt->reloc_count++
14719 * sizeof (Elf64_External_Rela));
14720 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14721 }
49c09209
AM
14722 }
14723
14724 if (local_syms != NULL
14725 && symtab_hdr->contents != (unsigned char *) local_syms)
14726 {
14727 if (!info->keep_memory)
14728 free (local_syms);
14729 else
14730 symtab_hdr->contents = (unsigned char *) local_syms;
14731 }
14732 }
0a1b45a2 14733 return true;
49c09209
AM
14734}
14735
a804e476
AM
14736/* Emit the static wrapper function preserving registers around a
14737 __tls_get_addr_opt call. */
14738
0a1b45a2 14739static bool
a804e476
AM
14740emit_tga_desc (struct ppc_link_hash_table *htab)
14741{
14742 asection *stub_sec = htab->tga_group->stub_sec;
14743 unsigned int cfa_updt = 11 * 4;
14744 bfd_byte *p;
14745 bfd_vma to, from, delta;
14746
14747 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14748 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14749 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14750 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14751 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14752 delta = to - from;
14753 if (delta + (1 << 25) >= 1 << 26)
14754 {
14755 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
0a1b45a2
AM
14756 htab->stub_error = true;
14757 return false;
a804e476
AM
14758 }
14759
14760 p = stub_sec->contents;
14761 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14762 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14763 p += 4;
14764 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14765 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14766}
14767
14768/* Emit eh_frame describing the static wrapper function. */
14769
14770static bfd_byte *
14771emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14772{
14773 unsigned int cfa_updt = 11 * 4;
14774 unsigned int i;
14775
14776 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14777 *p++ = DW_CFA_def_cfa_offset;
14778 if (htab->opd_abi)
14779 {
14780 *p++ = 128;
14781 *p++ = 1;
14782 }
14783 else
14784 *p++ = 96;
14785 *p++ = DW_CFA_offset_extended_sf;
14786 *p++ = 65;
14787 *p++ = (-16 / 8) & 0x7f;
14788 for (i = 4; i < 12; i++)
14789 {
14790 *p++ = DW_CFA_offset + i;
14791 *p++ = (htab->opd_abi ? 13 : 12) - i;
14792 }
14793 *p++ = DW_CFA_advance_loc + 10;
14794 *p++ = DW_CFA_def_cfa_offset;
14795 *p++ = 0;
14796 for (i = 4; i < 12; i++)
14797 *p++ = DW_CFA_restore + i;
14798 *p++ = DW_CFA_advance_loc + 2;
14799 *p++ = DW_CFA_restore_extended;
14800 *p++ = 65;
14801 return p;
14802}
14803
721956f4
AM
14804/* Build all the stubs associated with the current output file.
14805 The stubs are kept in a hash table attached to the main linker
14806 hash table. This function is called via gldelf64ppc_finish. */
14807
0a1b45a2 14808bool
e7d1c40c 14809ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14810 char **stats)
5d1634d7
AM
14811{
14812 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14813 struct map_stub *group;
721956f4 14814 asection *stub_sec;
5d1634d7 14815 bfd_byte *p;
e717da7e 14816 int stub_sec_count = 0;
5d1634d7 14817
4dfe6ac6 14818 if (htab == NULL)
0a1b45a2 14819 return false;
4dfe6ac6 14820
eea6121a 14821 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14822 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14823 {
14824 group->eh_size = 0;
14825 group->lr_restore = 0;
14826 if ((stub_sec = group->stub_sec) != NULL
14827 && stub_sec->size != 0)
14828 {
14829 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14830 stub_sec->size);
14831 if (stub_sec->contents == NULL)
0a1b45a2 14832 return false;
df136d64
AM
14833 stub_sec->size = 0;
14834 }
14835 }
5d1634d7 14836
23eb7e01 14837 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14838 {
9f951329 14839 unsigned int indx;
ad8e1ba5 14840 bfd_vma plt0;
9f951329 14841
721956f4 14842 /* Build the .glink plt call stub. */
e7d1c40c 14843 if (htab->params->emit_stub_syms)
97b639ba
AM
14844 {
14845 struct elf_link_hash_entry *h;
468392fb 14846 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
0a1b45a2 14847 true, false, false);
97b639ba 14848 if (h == NULL)
0a1b45a2 14849 return false;
97b639ba
AM
14850 if (h->root.type == bfd_link_hash_new)
14851 {
14852 h->root.type = bfd_link_hash_defined;
14853 h->root.u.def.section = htab->glink;
ee4bf8d2 14854 h->root.u.def.value = 8;
f5385ebf
AM
14855 h->ref_regular = 1;
14856 h->def_regular = 1;
14857 h->ref_regular_nonweak = 1;
14858 h->forced_local = 1;
14859 h->non_elf = 0;
2ec55de3 14860 h->root.linker_def = 1;
97b639ba
AM
14861 }
14862 }
33e44f2e
AM
14863 plt0 = (htab->elf.splt->output_section->vma
14864 + htab->elf.splt->output_offset
14865 - 16);
176a0d42
AM
14866 if (info->emitrelocations)
14867 {
14868 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14869 if (r == NULL)
0a1b45a2 14870 return false;
176a0d42
AM
14871 r->r_offset = (htab->glink->output_offset
14872 + htab->glink->output_section->vma);
14873 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14874 r->r_addend = plt0;
14875 }
4ce794b7 14876 p = htab->glink->contents;
176a0d42 14877 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14878 bfd_put_64 (htab->glink->owner, plt0, p);
14879 p += 8;
b9e5796b
AM
14880 if (htab->opd_abi)
14881 {
14882 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14883 p += 4;
14884 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14885 p += 4;
14886 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14887 p += 4;
14888 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14889 p += 4;
14890 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14891 p += 4;
14892 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14893 p += 4;
14894 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14895 p += 4;
14896 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14897 p += 4;
14898 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14899 p += 4;
14900 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14901 p += 4;
14902 }
14903 else
14904 {
3cd7c7d7
AM
14905 unsigned int insn;
14906
14907 /* 0:
14908 . .quad plt0-1f # plt0 entry relative to 1:
14909 #
14910 # We get here with r12 initially @ a glink branch
14911 # Load the address of _dl_runtime_resolve from plt0 and
14912 # jump to it, with r0 set to the index of the PLT entry
14913 # to be resolved and r11 the link map.
14914 __glink_PLTresolve:
14915 . std %r2,24(%r1) # optional
14916 . mflr %r0
14917 . bcl 20,31,1f
14918 1:
14919 . mflr %r11
14920 . mtlr %r0
14921 . ld %r0,(0b-1b)(%r11)
14922 . sub %r12,%r12,%r11
14923 . add %r11,%r0,%r11
14924 . addi %r0,%r12,1b-2f
14925 . ld %r12,0(%r11)
14926 . srdi %r0,%r0,2
14927 . mtctr %r12
14928 . ld %r11,8(%r11)
14929 . bctr
14930 2:
14931 . b __glink_PLTresolve
14932 . ...
14933 . b __glink_PLTresolve */
14934
14935 if (htab->has_plt_localentry0)
14936 {
14937 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14938 p += 4;
14939 }
b9e5796b
AM
14940 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14941 p += 4;
14942 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14943 p += 4;
14944 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14945 p += 4;
b9e5796b
AM
14946 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14947 p += 4;
3cd7c7d7
AM
14948 if (htab->has_plt_localentry0)
14949 insn = LD_R0_0R11 | (-20 & 0xfffc);
14950 else
14951 insn = LD_R0_0R11 | (-16 & 0xfffc);
14952 bfd_put_32 (htab->glink->owner, insn, p);
14953 p += 4;
b9e5796b
AM
14954 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14955 p += 4;
3cd7c7d7 14956 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 14957 p += 4;
3cd7c7d7 14958 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
14959 p += 4;
14960 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14961 p += 4;
14962 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14963 p += 4;
14964 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14965 p += 4;
14966 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14967 p += 4;
14968 }
407aa07c
AM
14969 bfd_put_32 (htab->glink->owner, BCTR, p);
14970 p += 4;
c75bc4f7 14971 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14972
9f951329
AM
14973 /* Build the .glink lazy link call stubs. */
14974 indx = 0;
9e390558 14975 while (p < htab->glink->contents + htab->glink->size)
9f951329 14976 {
b9e5796b 14977 if (htab->opd_abi)
9f951329 14978 {
b9e5796b
AM
14979 if (indx < 0x8000)
14980 {
14981 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14982 p += 4;
14983 }
14984 else
14985 {
14986 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14987 p += 4;
14988 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14989 p);
14990 p += 4;
14991 }
9f951329 14992 }
4ce794b7 14993 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14994 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14995 indx++;
9f951329
AM
14996 p += 4;
14997 }
5d1634d7 14998 }
5d1634d7 14999
a804e476
AM
15000 if (htab->tga_group != NULL)
15001 {
15002 htab->tga_group->lr_restore = 23 * 4;
15003 htab->tga_group->stub_sec->size = 24 * 4;
15004 if (!emit_tga_desc (htab))
0a1b45a2 15005 return false;
a804e476
AM
15006 if (htab->glink_eh_frame != NULL
15007 && htab->glink_eh_frame->size != 0)
15008 {
15009 size_t align = 4;
15010
15011 p = htab->glink_eh_frame->contents;
15012 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15013 p += 17;
15014 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15015 }
15016 }
15017
49c09209
AM
15018 /* Build .glink global entry stubs, and PLT relocs for globals. */
15019 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15020
15021 if (!write_plt_relocs_for_local_syms (info))
0a1b45a2 15022 return false;
9e390558 15023
7341d5e2 15024 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 15025 {
4ce794b7 15026 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 15027 htab->brlt->size);
4ce794b7 15028 if (htab->brlt->contents == NULL)
0a1b45a2 15029 return false;
721956f4 15030 }
ee75fd95 15031 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
15032 {
15033 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 15034 htab->relbrlt->size);
63bc6f6c 15035 if (htab->relbrlt->contents == NULL)
0a1b45a2 15036 return false;
63bc6f6c 15037 }
5d1634d7 15038
721956f4
AM
15039 /* Build the stubs as directed by the stub hash table. */
15040 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 15041
a4b6fadd
AM
15042 for (group = htab->group; group != NULL; group = group->next)
15043 if (group->needs_save_res)
7dda8d3c 15044 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 15045
aa8a7074
AM
15046 if (htab->relbrlt != NULL)
15047 htab->relbrlt->reloc_count = 0;
15048
e7d1c40c 15049 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
15050 for (group = htab->group; group != NULL; group = group->next)
15051 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
15052 {
15053 int align = abs (htab->params->plt_stub_align);
15054 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15055 }
794e51c0 15056
7dda8d3c
AM
15057 for (group = htab->group; group != NULL; group = group->next)
15058 if (group->needs_save_res)
15059 {
15060 stub_sec = group->stub_sec;
15061 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15062 htab->sfpr->contents, htab->sfpr->size);
15063 if (htab->params->emit_stub_syms)
15064 {
15065 unsigned int i;
15066
15067 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15068 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
0a1b45a2 15069 return false;
7dda8d3c
AM
15070 }
15071 }
15072
df136d64
AM
15073 if (htab->glink_eh_frame != NULL
15074 && htab->glink_eh_frame->size != 0)
15075 {
15076 bfd_vma val;
15077 size_t align = 4;
15078
15079 p = htab->glink_eh_frame->contents;
15080 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15081
15082 for (group = htab->group; group != NULL; group = group->next)
15083 if (group->eh_size != 0)
15084 {
15085 /* Offset to stub section. */
15086 val = (group->stub_sec->output_section->vma
15087 + group->stub_sec->output_offset);
15088 val -= (htab->glink_eh_frame->output_section->vma
15089 + htab->glink_eh_frame->output_offset
15090 + (p + 8 - htab->glink_eh_frame->contents));
15091 if (val + 0x80000000 > 0xffffffff)
15092 {
15093 _bfd_error_handler
15094 (_("%s offset too large for .eh_frame sdata4 encoding"),
15095 group->stub_sec->name);
0a1b45a2 15096 return false;
df136d64
AM
15097 }
15098 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15099 p += (group->eh_size + 17 + 3) & -4;
15100 }
15101 if (htab->glink != NULL && htab->glink->size != 0)
15102 {
15103 /* Offset to .glink. */
15104 val = (htab->glink->output_section->vma
15105 + htab->glink->output_offset
15106 + 8);
15107 val -= (htab->glink_eh_frame->output_section->vma
15108 + htab->glink_eh_frame->output_offset
15109 + (p + 8 - htab->glink_eh_frame->contents));
15110 if (val + 0x80000000 > 0xffffffff)
15111 {
15112 _bfd_error_handler
15113 (_("%s offset too large for .eh_frame sdata4 encoding"),
15114 htab->glink->name);
0a1b45a2 15115 return false;
df136d64
AM
15116 }
15117 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15118 p += (24 + align - 1) & -align;
15119 }
15120 }
15121
1657026c
AM
15122 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15123 {
15124 htab->elf.srelrdyn->contents
15125 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15126 if (htab->elf.srelrdyn->contents == NULL)
15127 return false;
15128
15129 size_t i = 0;
15130 bfd_byte *loc = htab->elf.srelrdyn->contents;
15131 while (i < htab->relr_count)
15132 {
15133 bfd_vma base = htab->relr_addr[i];
15134 BFD_ASSERT (base % 2 == 0);
15135 bfd_put_64 (htab->elf.dynobj, base, loc);
15136 loc += 8;
15137 i++;
15138 while (i < htab->relr_count
15139 && htab->relr_addr[i] == base)
15140 {
15141 htab->stub_error = true;
15142 i++;
15143 }
15144 base += 8;
15145 while (1)
15146 {
15147 bfd_vma bits = 0;
15148 while (i < htab->relr_count
15149 && htab->relr_addr[i] - base < 63 * 8
15150 && (htab->relr_addr[i] - base) % 8 == 0)
15151 {
15152 bits |= (bfd_vma) 1 << ((htab->relr_addr[i] - base) / 8);
15153 i++;
15154 }
15155 if (bits == 0)
15156 break;
15157 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15158 loc += 8;
15159 base += 63 * 8;
15160 }
15161 }
15162 /* Pad any excess with 1's, a do-nothing encoding. */
15163 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15164 < htab->elf.srelrdyn->size)
15165 {
15166 bfd_put_64 (htab->elf.dynobj, 1, loc);
15167 loc += 8;
15168 }
15169 }
15170
d4aaa2a0
AM
15171 for (group = htab->group; group != NULL; group = group->next)
15172 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
15173 {
15174 stub_sec_count += 1;
c9301e31
AM
15175 if (stub_sec->rawsize != stub_sec->size
15176 && (htab->stub_iteration <= STUB_SHRINK_ITER
15177 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
15178 break;
15179 }
5d1634d7 15180
25516cc5 15181 if (group != NULL)
1657026c
AM
15182 htab->stub_error = true;
15183
15184 if (htab->stub_error)
5d1634d7 15185 {
cf97bcb0 15186 _bfd_error_handler (_("stubs don't match calculated size"));
1657026c 15187 return false;
5d1634d7 15188 }
721956f4 15189
d2a300cf
AM
15190 if (stats != NULL)
15191 {
988b7300
AM
15192 char *groupmsg;
15193 if (asprintf (&groupmsg,
15194 ngettext ("linker stubs in %u group\n",
15195 "linker stubs in %u groups\n",
15196 stub_sec_count),
15197 stub_sec_count) < 0)
15198 *stats = NULL;
15199 else
15200 {
15201 if (asprintf (stats, _("%s"
15202 " branch %lu\n"
988b7300 15203 " long branch %lu\n"
988b7300 15204 " plt call %lu\n"
988b7300
AM
15205 " global entry %lu"),
15206 groupmsg,
15207 htab->stub_count[ppc_stub_long_branch - 1],
988b7300 15208 htab->stub_count[ppc_stub_plt_branch - 1],
988b7300 15209 htab->stub_count[ppc_stub_plt_call - 1],
988b7300
AM
15210 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15211 *stats = NULL;
15212 free (groupmsg);
15213 }
d2a300cf 15214 }
0a1b45a2 15215 return true;
5bd4f169
AM
15216}
15217
60124e18
AM
15218/* What to do when ld finds relocations against symbols defined in
15219 discarded sections. */
15220
15221static unsigned int
15222ppc64_elf_action_discarded (asection *sec)
15223{
15224 if (strcmp (".opd", sec->name) == 0)
15225 return 0;
15226
15227 if (strcmp (".toc", sec->name) == 0)
15228 return 0;
15229
bce50a28
JJ
15230 if (strcmp (".toc1", sec->name) == 0)
15231 return 0;
15232
60124e18
AM
15233 return _bfd_elf_default_action_discarded (sec);
15234}
15235
e59a1001
AM
15236/* These are the dynamic relocations supported by glibc. */
15237
0a1b45a2 15238static bool
e59a1001
AM
15239ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15240{
15241 switch (r_type)
15242 {
15243 case R_PPC64_RELATIVE:
15244 case R_PPC64_NONE:
15245 case R_PPC64_ADDR64:
15246 case R_PPC64_GLOB_DAT:
15247 case R_PPC64_IRELATIVE:
15248 case R_PPC64_JMP_IREL:
15249 case R_PPC64_JMP_SLOT:
15250 case R_PPC64_DTPMOD64:
15251 case R_PPC64_DTPREL64:
15252 case R_PPC64_TPREL64:
15253 case R_PPC64_TPREL16_LO_DS:
15254 case R_PPC64_TPREL16_DS:
15255 case R_PPC64_TPREL16:
15256 case R_PPC64_TPREL16_LO:
15257 case R_PPC64_TPREL16_HI:
15258 case R_PPC64_TPREL16_HIGH:
15259 case R_PPC64_TPREL16_HA:
15260 case R_PPC64_TPREL16_HIGHA:
15261 case R_PPC64_TPREL16_HIGHER:
15262 case R_PPC64_TPREL16_HIGHEST:
15263 case R_PPC64_TPREL16_HIGHERA:
15264 case R_PPC64_TPREL16_HIGHESTA:
15265 case R_PPC64_ADDR16_LO_DS:
15266 case R_PPC64_ADDR16_LO:
15267 case R_PPC64_ADDR16_HI:
15268 case R_PPC64_ADDR16_HIGH:
15269 case R_PPC64_ADDR16_HA:
15270 case R_PPC64_ADDR16_HIGHA:
15271 case R_PPC64_REL30:
15272 case R_PPC64_COPY:
15273 case R_PPC64_UADDR64:
15274 case R_PPC64_UADDR32:
15275 case R_PPC64_ADDR32:
15276 case R_PPC64_ADDR24:
15277 case R_PPC64_ADDR16:
15278 case R_PPC64_UADDR16:
15279 case R_PPC64_ADDR16_DS:
15280 case R_PPC64_ADDR16_HIGHER:
15281 case R_PPC64_ADDR16_HIGHEST:
15282 case R_PPC64_ADDR16_HIGHERA:
15283 case R_PPC64_ADDR16_HIGHESTA:
15284 case R_PPC64_ADDR14:
15285 case R_PPC64_ADDR14_BRTAKEN:
15286 case R_PPC64_ADDR14_BRNTAKEN:
15287 case R_PPC64_REL32:
15288 case R_PPC64_REL64:
0a1b45a2 15289 return true;
e59a1001
AM
15290
15291 default:
0a1b45a2 15292 return false;
e59a1001
AM
15293 }
15294}
15295
5bd4f169
AM
15296/* The RELOCATE_SECTION function is called by the ELF backend linker
15297 to handle the relocations for a section.
15298
15299 The relocs are always passed as Rela structures; if the section
15300 actually uses Rel structures, the r_addend field will always be
15301 zero.
15302
15303 This function is responsible for adjust the section contents as
15304 necessary, and (if using Rela relocs and generating a
1049f94e 15305 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
15306 necessary.
15307
15308 This function does not have to worry about setting the reloc
15309 address or the reloc symbol index.
15310
15311 LOCAL_SYMS is a pointer to the swapped in local symbols.
15312
15313 LOCAL_SECTIONS is an array giving the section in the input file
15314 corresponding to the st_shndx field of each local symbol.
15315
15316 The global hash table entry for the global symbols can be found
15317 via elf_sym_hashes (input_bfd).
15318
1049f94e 15319 When generating relocatable output, this function must handle
5bd4f169
AM
15320 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15321 going to be the section symbol corresponding to the output
15322 section, which means that the addend must be adjusted
15323 accordingly. */
15324
0f684201 15325static int
4ce794b7
AM
15326ppc64_elf_relocate_section (bfd *output_bfd,
15327 struct bfd_link_info *info,
15328 bfd *input_bfd,
15329 asection *input_section,
15330 bfd_byte *contents,
15331 Elf_Internal_Rela *relocs,
15332 Elf_Internal_Sym *local_syms,
15333 asection **local_sections)
5bd4f169 15334{
65f38f15 15335 struct ppc_link_hash_table *htab;
5bd4f169
AM
15336 Elf_Internal_Shdr *symtab_hdr;
15337 struct elf_link_hash_entry **sym_hashes;
5bd4f169 15338 Elf_Internal_Rela *rel;
c316a17c 15339 Elf_Internal_Rela *wrel;
5bd4f169 15340 Elf_Internal_Rela *relend;
411e1bfb
AM
15341 Elf_Internal_Rela outrel;
15342 bfd_byte *loc;
411e1bfb 15343 struct got_entry **local_got_ents;
5bd4f169 15344 bfd_vma TOCstart;
0a1b45a2
AM
15345 bool ret = true;
15346 bool is_opd;
794e51c0 15347 /* Assume 'at' branch hints. */
0a1b45a2
AM
15348 bool is_isa_v2 = true;
15349 bool warned_dynamic = false;
95f0d0d2 15350 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 15351
65f38f15 15352 /* Initialize howto table if needed. */
5bd4f169 15353 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
15354 ppc_howto_init ();
15355
65f38f15 15356 htab = ppc_hash_table (info);
4dfe6ac6 15357 if (htab == NULL)
0a1b45a2 15358 return false;
ee75fd95
AM
15359
15360 /* Don't relocate stub sections. */
e7d1c40c 15361 if (input_section->owner == htab->params->stub_bfd)
0a1b45a2 15362 return true;
ee75fd95 15363
7af5d5c4
AM
15364 if (!is_ppc64_elf (input_bfd))
15365 {
15366 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 15367 return false;
7af5d5c4 15368 }
0ffa91dd 15369
411e1bfb 15370 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 15371 TOCstart = elf_gp (output_bfd);
0ffa91dd 15372 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 15373 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 15374 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 15375
c316a17c 15376 rel = wrel = relocs;
5bd4f169 15377 relend = relocs + input_section->reloc_count;
c316a17c 15378 for (; rel < relend; wrel++, rel++)
5bd4f169 15379 {
04c9666a 15380 enum elf_ppc64_reloc_type r_type;
31c76678 15381 bfd_vma addend;
5bd4f169
AM
15382 bfd_reloc_status_type r;
15383 Elf_Internal_Sym *sym;
15384 asection *sec;
039b3fef
AM
15385 struct elf_link_hash_entry *h_elf;
15386 struct ppc_link_hash_entry *h;
15387 struct ppc_link_hash_entry *fdh;
5bd4f169 15388 const char *sym_name;
0d4792f7 15389 unsigned long r_symndx, toc_symndx;
3a71aa26 15390 bfd_vma toc_addend;
f961d9dd
AM
15391 unsigned char tls_mask, tls_gd, tls_type;
15392 unsigned char sym_type;
5bd4f169 15393 bfd_vma relocation;
0a1b45a2
AM
15394 bool unresolved_reloc, save_unresolved_reloc;
15395 bool warned;
bc30df16 15396 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 15397 unsigned int insn;
e11840f9 15398 unsigned int mask;
721956f4
AM
15399 struct ppc_stub_hash_entry *stub_entry;
15400 bfd_vma max_br_offset;
15401 bfd_vma from;
c316a17c 15402 Elf_Internal_Rela orig_rel;
b80eed39
AM
15403 reloc_howto_type *howto;
15404 struct reloc_howto_struct alt_howto;
4a421c53
AM
15405 uint64_t pinsn;
15406 bfd_vma offset;
5bd4f169 15407
c316a17c
AM
15408 again:
15409 orig_rel = *rel;
15410
4ce794b7 15411 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 15412 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
15413
15414 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15415 symbol of the previous ADDR64 reloc. The symbol gives us the
15416 proper TOC base to use. */
15417 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
15418 && wrel != relocs
15419 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 15420 && is_opd)
c316a17c 15421 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 15422
4ce794b7
AM
15423 sym = NULL;
15424 sec = NULL;
039b3fef 15425 h_elf = NULL;
4ce794b7 15426 sym_name = NULL;
0a1b45a2
AM
15427 unresolved_reloc = false;
15428 warned = false;
65f38f15 15429
0b13192e 15430 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15431 {
15432 /* It's a local symbol. */
74f0fb50 15433 struct _opd_sec_data *opd;
4025353c 15434
5bd4f169
AM
15435 sym = local_syms + r_symndx;
15436 sec = local_sections[r_symndx];
26c61ae5 15437 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15438 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15439 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15440 opd = get_opd_info (sec);
15441 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15442 {
51aecdc5
AM
15443 long adjust = opd->adjust[OPD_NDX (sym->st_value
15444 + rel->r_addend)];
4025353c
AM
15445 if (adjust == -1)
15446 relocation = 0;
15447 else
4cc603a5
AM
15448 {
15449 /* If this is a relocation against the opd section sym
15450 and we have edited .opd, adjust the reloc addend so
15451 that ld -r and ld --emit-relocs output is correct.
15452 If it is a reloc against some other .opd symbol,
15453 then the symbol value will be adjusted later. */
15454 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15455 rel->r_addend += adjust;
15456 else
15457 relocation += adjust;
15458 }
1e2f5b6e 15459 }
5bd4f169
AM
15460 }
15461 else
15462 {
0a1b45a2 15463 bool ignored;
62d887d4 15464
b2a8e766
AM
15465 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15466 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15467 h_elf, sec, relocation,
62d887d4 15468 unresolved_reloc, warned, ignored);
039b3fef
AM
15469 sym_name = h_elf->root.root.string;
15470 sym_type = h_elf->type;
b69fdb4e
AM
15471 if (sec != NULL
15472 && sec->owner == output_bfd
15473 && strcmp (sec->name, ".opd") == 0)
15474 {
15475 /* This is a symbol defined in a linker script. All
15476 such are defined in output sections, even those
15477 defined by simple assignment from a symbol defined in
15478 an input section. Transfer the symbol to an
15479 appropriate input .opd section, so that a branch to
15480 this symbol will be mapped to the location specified
15481 by the opd entry. */
15482 struct bfd_link_order *lo;
15483 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15484 if (lo->type == bfd_indirect_link_order)
15485 {
15486 asection *isec = lo->u.indirect.section;
15487 if (h_elf->root.u.def.value >= isec->output_offset
15488 && h_elf->root.u.def.value < (isec->output_offset
15489 + isec->size))
15490 {
15491 h_elf->root.u.def.value -= isec->output_offset;
15492 h_elf->root.u.def.section = isec;
15493 sec = isec;
15494 break;
15495 }
15496 }
15497 }
5bd4f169 15498 }
ed7007c1 15499 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15500
dbaa2011 15501 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15502 {
15503 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15504 input_bfd, input_section,
0930cb30 15505 contents, rel->r_offset);
c316a17c
AM
15506 wrel->r_offset = rel->r_offset;
15507 wrel->r_info = 0;
15508 wrel->r_addend = 0;
15509
15510 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15511 symbols defined in discarded sections. Not done for
c316a17c
AM
15512 non-debug to preserve relocs in .eh_frame which the
15513 eh_frame editing code expects to be present. */
15514 if (bfd_link_relocatable (info)
15515 && (input_section->flags & SEC_DEBUGGING))
15516 wrel--;
15517
15518 continue;
15519 }
ab96bf03 15520
0e1862bb 15521 if (bfd_link_relocatable (info))
c316a17c 15522 goto copy_reloc;
ab96bf03 15523
f40da81b
AM
15524 if (h != NULL && &h->elf == htab->elf.hgot)
15525 {
6f20ed8a 15526 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b 15527 sec = bfd_abs_section_ptr;
0a1b45a2 15528 unresolved_reloc = false;
f40da81b
AM
15529 }
15530
951fd09b
AM
15531 /* TLS optimizations. Replace instruction sequences and relocs
15532 based on information we collected in tls_optimize. We edit
15533 RELOCS so that --emit-relocs will output something sensible
15534 for the final instruction stream. */
15535 tls_mask = 0;
15536 tls_gd = 0;
0d4792f7 15537 toc_symndx = 0;
727fc41e
AM
15538 if (h != NULL)
15539 tls_mask = h->tls_mask;
15540 else if (local_got_ents != NULL)
411e1bfb 15541 {
e054468f
AM
15542 struct plt_entry **local_plt = (struct plt_entry **)
15543 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15544 unsigned char *lgot_masks = (unsigned char *)
e054468f 15545 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15546 tls_mask = lgot_masks[r_symndx];
15547 }
37da22e5 15548 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15549 && (r_type == R_PPC64_TLS
15550 || r_type == R_PPC64_TLSGD
15551 || r_type == R_PPC64_TLSLD))
15552 {
15553 /* Check for toc tls entries. */
f961d9dd 15554 unsigned char *toc_tls;
0d4792f7 15555
727fc41e
AM
15556 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15557 &local_syms, rel, input_bfd))
0a1b45a2 15558 return false;
0d4792f7 15559
727fc41e
AM
15560 if (toc_tls)
15561 tls_mask = *toc_tls;
0d4792f7
AM
15562 }
15563
15564 /* Check that tls relocs are used with tls syms, and non-tls
15565 relocs are used with non-tls syms. */
cf35638d 15566 if (r_symndx != STN_UNDEF
0d4792f7
AM
15567 && r_type != R_PPC64_NONE
15568 && (h == NULL
039b3fef
AM
15569 || h->elf.root.type == bfd_link_hash_defined
15570 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15571 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15572 {
37da22e5 15573 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15574 && (r_type == R_PPC64_TLS
15575 || r_type == R_PPC64_TLSGD
15576 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15577 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15578 ;
15579 else
25f53a85 15580 info->callbacks->einfo
1d483afe 15581 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15582 /* xgettext:c-format */
c1c8c1ef 15583 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15584 /* xgettext:c-format */
c1c8c1ef 15585 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15586 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15587 ppc64_elf_howto_table[r_type]->name,
15588 sym_name);
411e1bfb
AM
15589 }
15590
15591 /* Ensure reloc mapping code below stays sane. */
15592 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15593 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15594 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15595 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15596 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15597 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15598 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15599 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15600 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15601 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15602 abort ();
0d4792f7 15603
411e1bfb
AM
15604 switch (r_type)
15605 {
15606 default:
411e1bfb
AM
15607 break;
15608
ba761f19 15609 case R_PPC64_LO_DS_OPT:
733ae98c
AM
15610 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15611 {
15612 insn = bfd_get_32 (input_bfd,
15613 contents + rel->r_offset - d_offset);
15614 if ((insn & (0x3fu << 26)) != 58u << 26)
15615 abort ();
15616 insn += (14u << 26) - (58u << 26);
15617 bfd_put_32 (input_bfd, insn,
15618 contents + rel->r_offset - d_offset);
15619 r_type = R_PPC64_TOC16_LO;
15620 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15621 }
ba761f19
AM
15622 break;
15623
411e1bfb
AM
15624 case R_PPC64_TOC16:
15625 case R_PPC64_TOC16_LO:
15626 case R_PPC64_TOC16_DS:
15627 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15628 {
15629 /* Check for toc tls entries. */
f961d9dd 15630 unsigned char *toc_tls;
951fd09b 15631 int retval;
411e1bfb 15632
3a71aa26
AM
15633 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15634 &local_syms, rel, input_bfd);
951fd09b 15635 if (retval == 0)
0a1b45a2 15636 return false;
411e1bfb
AM
15637
15638 if (toc_tls)
15639 {
951fd09b 15640 tls_mask = *toc_tls;
411e1bfb
AM
15641 if (r_type == R_PPC64_TOC16_DS
15642 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15643 {
37da22e5 15644 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15645 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15646 goto toctprel;
15647 }
411e1bfb 15648 else
951fd09b
AM
15649 {
15650 /* If we found a GD reloc pair, then we might be
15651 doing a GD->IE transition. */
15652 if (retval == 2)
15653 {
b00a0a86 15654 tls_gd = TLS_GDIE;
37da22e5
AM
15655 if ((tls_mask & TLS_TLS) != 0
15656 && (tls_mask & TLS_GD) == 0)
102890f0 15657 goto tls_ldgd_opt;
951fd09b
AM
15658 }
15659 else if (retval == 3)
15660 {
37da22e5
AM
15661 if ((tls_mask & TLS_TLS) != 0
15662 && (tls_mask & TLS_LD) == 0)
102890f0 15663 goto tls_ldgd_opt;
951fd09b
AM
15664 }
15665 }
411e1bfb
AM
15666 }
15667 }
15668 break;
15669
9d6ded02
AM
15670 case R_PPC64_GOT_TPREL16_HI:
15671 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15672 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15673 && (tls_mask & TLS_TPREL) == 0
15674 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
9d6ded02
AM
15675 {
15676 rel->r_offset -= d_offset;
95f0d0d2 15677 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15678 r_type = R_PPC64_NONE;
15679 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15680 }
15681 break;
15682
411e1bfb
AM
15683 case R_PPC64_GOT_TPREL16_DS:
15684 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15685 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15686 && (tls_mask & TLS_TPREL) == 0
15687 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
411e1bfb 15688 {
81407a69 15689 toctprel:
95f0d0d2 15690 insn = bfd_get_32 (input_bfd,
c316a17c 15691 contents + rel->r_offset - d_offset);
411e1bfb
AM
15692 insn &= 31 << 21;
15693 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15694 bfd_put_32 (input_bfd, insn,
c316a17c 15695 contents + rel->r_offset - d_offset);
411e1bfb 15696 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15697 if (toc_symndx != 0)
15698 {
15699 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15700 rel->r_addend = toc_addend;
0d4792f7
AM
15701 /* We changed the symbol. Start over in order to
15702 get h, sym, sec etc. right. */
c316a17c 15703 goto again;
0d4792f7
AM
15704 }
15705 else
15706 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15707 }
15708 break;
15709
87c69f97 15710 case R_PPC64_GOT_TPREL_PCREL34:
c213164a 15711 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15712 && (tls_mask & TLS_TPREL) == 0
15713 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15714 {
15715 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15716 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15717 pinsn <<= 32;
15718 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15719 pinsn += ((2ULL << 56) + (-1ULL << 52)
15720 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15721 bfd_put_32 (input_bfd, pinsn >> 32,
15722 contents + rel->r_offset);
15723 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15724 contents + rel->r_offset + 4);
15725 r_type = R_PPC64_TPREL34;
15726 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15727 }
15728 break;
15729
411e1bfb 15730 case R_PPC64_TLS:
37da22e5 15731 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15732 && (tls_mask & TLS_TPREL) == 0
15733 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15734 {
c213164a 15735 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15736 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15737 if (insn == 0)
c213164a
AM
15738 break;
15739 if ((rel->r_offset & 3) == 0)
0d4792f7 15740 {
c213164a
AM
15741 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15742 /* Was PPC64_TLS which sits on insn boundary, now
15743 PPC64_TPREL16_LO which is at low-order half-word. */
15744 rel->r_offset += d_offset;
15745 r_type = R_PPC64_TPREL16_LO;
15746 if (toc_symndx != 0)
15747 {
15748 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15749 rel->r_addend = toc_addend;
15750 /* We changed the symbol. Start over in order to
15751 get h, sym, sec etc. right. */
15752 goto again;
15753 }
15754 else
15755 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15756 }
15757 else if ((rel->r_offset & 3) == 1)
15758 {
15759 /* For pcrel IE to LE we already have the full
15760 offset and thus don't need an addi here. A nop
15761 or mr will do. */
2365f8d7 15762 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15763 {
15764 /* Extract regs from addi rt,ra,si. */
15765 unsigned int rt = (insn >> 21) & 0x1f;
15766 unsigned int ra = (insn >> 16) & 0x1f;
15767 if (rt == ra)
15768 insn = NOP;
15769 else
15770 {
15771 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15772 insn = (rt << 16) | (ra << 21) | (ra << 11);
15773 insn |= (31u << 26) | (444u << 1);
15774 }
15775 }
15776 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15777 }
411e1bfb
AM
15778 }
15779 break;
15780
411e1bfb
AM
15781 case R_PPC64_GOT_TLSGD16_HI:
15782 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15783 tls_gd = TLS_GDIE;
733ae98c
AM
15784 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15785 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b
AM
15786 goto tls_gdld_hi;
15787 break;
15788
411e1bfb
AM
15789 case R_PPC64_GOT_TLSLD16_HI:
15790 case R_PPC64_GOT_TLSLD16_HA:
733ae98c
AM
15791 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15792 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15793 {
951fd09b
AM
15794 tls_gdld_hi:
15795 if ((tls_mask & tls_gd) != 0)
15796 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15797 + R_PPC64_GOT_TPREL16_DS);
15798 else
411e1bfb 15799 {
4fe5ca5b 15800 rel->r_offset -= d_offset;
95f0d0d2 15801 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15802 r_type = R_PPC64_NONE;
411e1bfb 15803 }
951fd09b 15804 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15805 }
15806 break;
15807
951fd09b
AM
15808 case R_PPC64_GOT_TLSGD16:
15809 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15810 tls_gd = TLS_GDIE;
733ae98c
AM
15811 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15812 && offset_in_range (input_section, rel->r_offset & ~3, 4))
102890f0 15813 goto tls_ldgd_opt;
951fd09b 15814 break;
411e1bfb 15815
951fd09b
AM
15816 case R_PPC64_GOT_TLSLD16:
15817 case R_PPC64_GOT_TLSLD16_LO:
733ae98c
AM
15818 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15819 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b 15820 {
b9f04fe0 15821 unsigned int insn1, insn2;
102890f0
AM
15822
15823 tls_ldgd_opt:
727fc41e
AM
15824 offset = (bfd_vma) -1;
15825 /* If not using the newer R_PPC64_TLSGD/LD to mark
15826 __tls_get_addr calls, we must trust that the call
15827 stays with its arg setup insns, ie. that the next
15828 reloc is the __tls_get_addr call associated with
15829 the current reloc. Edit both insns. */
9737e8af 15830 if (input_section->nomark_tls_get_addr
727fc41e
AM
15831 && rel + 1 < relend
15832 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15833 htab->tls_get_addr_fd,
15834 htab->tga_desc_fd,
727fc41e 15835 htab->tls_get_addr,
9e7028aa 15836 htab->tga_desc))
727fc41e 15837 offset = rel[1].r_offset;
b86ac8e3
AM
15838 /* We read the low GOT_TLS (or TOC16) insn because we
15839 need to keep the destination reg. It may be
15840 something other than the usual r3, and moved to r3
15841 before the call by intervening code. */
95f0d0d2 15842 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15843 contents + rel->r_offset - d_offset);
102890f0 15844 if ((tls_mask & tls_gd) != 0)
411e1bfb 15845 {
102890f0 15846 /* IE */
b86ac8e3 15847 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15848 insn1 |= 58u << 26; /* ld */
102890f0 15849 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15850 if (offset != (bfd_vma) -1)
f58d5a2d 15851 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15852 if (r_type == R_PPC64_TOC16
15853 || r_type == R_PPC64_TOC16_LO)
102890f0 15854 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15855 else
15856 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15857 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15858 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15859 }
15860 else
15861 {
15862 /* LE */
b86ac8e3
AM
15863 insn1 &= 0x1f << 21;
15864 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15865 insn2 = 0x38630000; /* addi 3,3,0 */
15866 if (tls_gd == 0)
951fd09b 15867 {
102890f0 15868 /* Was an LD reloc. */
71c4e95a 15869 r_symndx = STN_UNDEF;
102890f0 15870 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15871 }
102890f0 15872 else if (toc_symndx != 0)
3a71aa26
AM
15873 {
15874 r_symndx = toc_symndx;
15875 rel->r_addend = toc_addend;
15876 }
102890f0
AM
15877 r_type = R_PPC64_TPREL16_HA;
15878 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15879 if (offset != (bfd_vma) -1)
15880 {
15881 rel[1].r_info = ELF64_R_INFO (r_symndx,
15882 R_PPC64_TPREL16_LO);
15883 rel[1].r_offset = offset + d_offset;
15884 rel[1].r_addend = rel->r_addend;
15885 }
102890f0 15886 }
95f0d0d2 15887 bfd_put_32 (input_bfd, insn1,
3a71aa26 15888 contents + rel->r_offset - d_offset);
733ae98c
AM
15889 if (offset != (bfd_vma) -1
15890 && offset_in_range (input_section, offset, 4))
c96e0573
AM
15891 {
15892 bfd_put_32 (input_bfd, insn2, contents + offset);
733ae98c 15893 if (offset_in_range (input_section, offset + 4, 4))
c96e0573
AM
15894 {
15895 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15896 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15897 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15898 }
15899 }
727fc41e
AM
15900 if ((tls_mask & tls_gd) == 0
15901 && (tls_gd == 0 || toc_symndx != 0))
15902 {
15903 /* We changed the symbol. Start over in order
15904 to get h, sym, sec etc. right. */
c316a17c 15905 goto again;
727fc41e
AM
15906 }
15907 }
15908 break;
15909
87c69f97 15910 case R_PPC64_GOT_TLSGD_PCREL34:
733ae98c
AM
15911 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15912 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15913 {
15914 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15915 pinsn <<= 32;
15916 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15917 if ((tls_mask & TLS_GDIE) != 0)
15918 {
15919 /* IE, pla -> pld */
15920 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15921 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15922 }
15923 else
15924 {
15925 /* LE, pla pcrel -> paddi r13 */
15926 pinsn += (-1ULL << 52) + (13ULL << 16);
15927 r_type = R_PPC64_TPREL34;
15928 }
15929 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15930 bfd_put_32 (input_bfd, pinsn >> 32,
15931 contents + rel->r_offset);
15932 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15933 contents + rel->r_offset + 4);
15934 }
15935 break;
15936
87c69f97 15937 case R_PPC64_GOT_TLSLD_PCREL34:
733ae98c
AM
15938 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15939 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15940 {
15941 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15942 pinsn <<= 32;
15943 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15944 pinsn += (-1ULL << 52) + (13ULL << 16);
15945 bfd_put_32 (input_bfd, pinsn >> 32,
15946 contents + rel->r_offset);
15947 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15948 contents + rel->r_offset + 4);
15949 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15950 r_symndx = STN_UNDEF;
15951 r_type = R_PPC64_TPREL34;
15952 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15953 goto again;
15954 }
15955 break;
15956
727fc41e 15957 case R_PPC64_TLSGD:
37da22e5 15958 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
733ae98c
AM
15959 && rel + 1 < relend
15960 && offset_in_range (input_section, rel->r_offset,
15961 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15962 ? 8 : 4))
727fc41e 15963 {
b9f04fe0 15964 unsigned int insn2;
5663e321 15965 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15966
4a421c53 15967 offset = rel->r_offset;
5663e321 15968 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15969 {
15970 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15971 if (r_type1 == R_PPC64_PLT_PCREL34
15972 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15973 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15974 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15975 break;
15976 }
15977
733ae98c 15978 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
15979 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15980
b00a0a86 15981 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
15982 {
15983 /* IE */
15984 r_type = R_PPC64_NONE;
15985 insn2 = 0x7c636a14; /* add 3,3,13 */
15986 }
15987 else
15988 {
15989 /* LE */
15990 if (toc_symndx != 0)
15991 {
15992 r_symndx = toc_symndx;
15993 rel->r_addend = toc_addend;
15994 }
c213164a 15995 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 15996 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
15997 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15998 {
15999 r_type = R_PPC64_NONE;
16000 insn2 = NOP;
16001 }
16002 else
16003 {
16004 rel->r_offset = offset + d_offset;
16005 r_type = R_PPC64_TPREL16_LO;
16006 insn2 = 0x38630000; /* addi 3,3,0 */
16007 }
727fc41e
AM
16008 }
16009 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16010 /* Zap the reloc on the _tls_get_addr call too. */
16011 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16012 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16013 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16014 if ((tls_mask & TLS_GDIE) == 0
16015 && toc_symndx != 0
16016 && r_type != R_PPC64_NONE)
c316a17c 16017 goto again;
411e1bfb 16018 }
411e1bfb
AM
16019 break;
16020
727fc41e 16021 case R_PPC64_TLSLD:
37da22e5 16022 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
733ae98c
AM
16023 && rel + 1 < relend
16024 && offset_in_range (input_section, rel->r_offset,
16025 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16026 ? 8 : 4))
727fc41e 16027 {
b9f04fe0 16028 unsigned int insn2;
5663e321 16029 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16030
4a421c53 16031 offset = rel->r_offset;
5663e321 16032 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16033 {
16034 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16035 if (r_type1 == R_PPC64_PLT_PCREL34
16036 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16037 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16038 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16039 break;
16040 }
16041
733ae98c 16042 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16043 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16044
c213164a 16045 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16046 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16047 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16048 {
16049 r_type = R_PPC64_NONE;
16050 insn2 = NOP;
16051 }
16052 else
16053 {
16054 rel->r_offset = offset + d_offset;
16055 r_symndx = STN_UNDEF;
16056 r_type = R_PPC64_TPREL16_LO;
16057 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16058 insn2 = 0x38630000; /* addi 3,3,0 */
16059 }
727fc41e 16060 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
16061 /* Zap the reloc on the _tls_get_addr call too. */
16062 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16063 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16064 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16065 if (r_type != R_PPC64_NONE)
16066 goto again;
727fc41e
AM
16067 }
16068 break;
16069
411e1bfb 16070 case R_PPC64_DTPMOD64:
951fd09b
AM
16071 if (rel + 1 < relend
16072 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16073 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 16074 {
733ae98c
AM
16075 if ((tls_mask & TLS_GD) == 0
16076 && offset_in_range (input_section, rel->r_offset, 8))
951fd09b
AM
16077 {
16078 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 16079 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
16080 r_type = R_PPC64_TPREL64;
16081 else
16082 {
4ce794b7 16083 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
16084 r_type = R_PPC64_NONE;
16085 }
16086 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16087 }
16088 }
16089 else
16090 {
733ae98c
AM
16091 if ((tls_mask & TLS_LD) == 0
16092 && offset_in_range (input_section, rel->r_offset, 8))
411e1bfb 16093 {
4ce794b7 16094 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 16095 r_type = R_PPC64_NONE;
951fd09b 16096 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 16097 }
411e1bfb
AM
16098 }
16099 break;
16100
16101 case R_PPC64_TPREL64:
951fd09b 16102 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
16103 {
16104 r_type = R_PPC64_NONE;
16105 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16106 }
16107 break;
52a82034 16108
006589cf
AM
16109 case R_PPC64_ENTRY:
16110 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16111 if (!bfd_link_pic (info)
16112 && !info->traditional_format
733ae98c
AM
16113 && relocation + 0x80008000 <= 0xffffffff
16114 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16115 {
16116 unsigned int insn1, insn2;
16117
16118 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16119 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16120 if ((insn1 & ~0xfffc) == LD_R2_0R12
16121 && insn2 == ADD_R2_R2_R12)
16122 {
95f0d0d2 16123 bfd_put_32 (input_bfd,
006589cf
AM
16124 LIS_R2 + PPC_HA (relocation),
16125 contents + rel->r_offset);
95f0d0d2 16126 bfd_put_32 (input_bfd,
006589cf
AM
16127 ADDI_R2_R2 + PPC_LO (relocation),
16128 contents + rel->r_offset + 4);
16129 }
16130 }
16131 else
16132 {
16133 relocation -= (rel->r_offset
16134 + input_section->output_offset
16135 + input_section->output_section->vma);
733ae98c
AM
16136 if (relocation + 0x80008000 <= 0xffffffff
16137 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16138 {
16139 unsigned int insn1, insn2;
16140
16141 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16142 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16143 if ((insn1 & ~0xfffc) == LD_R2_0R12
16144 && insn2 == ADD_R2_R2_R12)
16145 {
95f0d0d2 16146 bfd_put_32 (input_bfd,
006589cf
AM
16147 ADDIS_R2_R12 + PPC_HA (relocation),
16148 contents + rel->r_offset);
95f0d0d2 16149 bfd_put_32 (input_bfd,
006589cf
AM
16150 ADDI_R2_R2 + PPC_LO (relocation),
16151 contents + rel->r_offset + 4);
16152 }
16153 }
16154 }
16155 break;
16156
52a82034
AM
16157 case R_PPC64_REL16_HA:
16158 /* If we are generating a non-PIC executable, edit
16159 . 0: addis 2,12,.TOC.-0b@ha
16160 . addi 2,2,.TOC.-0b@l
16161 used by ELFv2 global entry points to set up r2, to
16162 . lis 2,.TOC.@ha
16163 . addi 2,2,.TOC.@l
16164 if .TOC. is in range. */
0e1862bb 16165 if (!bfd_link_pic (info)
810d4e75 16166 && !info->traditional_format
006589cf 16167 && !htab->opd_abi
4f038ee5 16168 && rel->r_addend == d_offset
52a82034
AM
16169 && h != NULL && &h->elf == htab->elf.hgot
16170 && rel + 1 < relend
16171 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16172 && rel[1].r_offset == rel->r_offset + 4
16173 && rel[1].r_addend == rel->r_addend + 4
733ae98c
AM
16174 && relocation + 0x80008000 <= 0xffffffff
16175 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
52a82034
AM
16176 {
16177 unsigned int insn1, insn2;
4a421c53 16178 offset = rel->r_offset - d_offset;
95f0d0d2
AM
16179 insn1 = bfd_get_32 (input_bfd, contents + offset);
16180 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
16181 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16182 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
16183 {
16184 r_type = R_PPC64_ADDR16_HA;
16185 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16186 rel->r_addend -= d_offset;
16187 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16188 rel[1].r_addend -= d_offset + 4;
95f0d0d2 16189 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
16190 }
16191 }
16192 break;
411e1bfb
AM
16193 }
16194
16195 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 16196 insn = 0;
b25116a9
AM
16197 max_br_offset = 1 << 25;
16198 addend = rel->r_addend;
bc30df16 16199 reloc_dest = DEST_NORMAL;
65f38f15 16200 switch (r_type)
5bd4f169
AM
16201 {
16202 default:
65f38f15 16203 break;
5bd4f169 16204
3b421ab3
AM
16205 case R_PPC64_TOCSAVE:
16206 if (relocation + addend == (rel->r_offset
16207 + input_section->output_offset
16208 + input_section->output_section->vma)
16209 && tocsave_find (htab, NO_INSERT,
733ae98c
AM
16210 &local_syms, rel, input_bfd)
16211 && offset_in_range (input_section, rel->r_offset, 4))
3b421ab3
AM
16212 {
16213 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16214 if (insn == NOP
16215 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
16216 bfd_put_32 (input_bfd,
16217 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
16218 contents + rel->r_offset);
16219 }
16220 break;
16221
65f38f15
AM
16222 /* Branch taken prediction relocations. */
16223 case R_PPC64_ADDR14_BRTAKEN:
16224 case R_PPC64_REL14_BRTAKEN:
cedb70c5 16225 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 16226 /* Fall through. */
65f38f15 16227
86c76c7b 16228 /* Branch not taken prediction relocations. */
65f38f15
AM
16229 case R_PPC64_ADDR14_BRNTAKEN:
16230 case R_PPC64_REL14_BRNTAKEN:
733ae98c
AM
16231 if (!offset_in_range (input_section, rel->r_offset, 4))
16232 break;
95f0d0d2 16233 insn |= bfd_get_32 (input_bfd,
411e1bfb 16234 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 16235 /* Fall through. */
86c76c7b 16236
b25116a9
AM
16237 case R_PPC64_REL14:
16238 max_br_offset = 1 << 15;
1a0670f3 16239 /* Fall through. */
5bd4f169 16240
65f38f15 16241 case R_PPC64_REL24:
05d0e962 16242 case R_PPC64_REL24_NOTOC:
7aba54da 16243 case R_PPC64_REL24_P9NOTOC:
23cedd1d 16244 case R_PPC64_PLTCALL:
5663e321 16245 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
16246 /* Calls to functions with a different TOC, such as calls to
16247 shared objects, need to alter the TOC pointer. This is
16248 done using a linkage stub. A REL24 branching to these
16249 linkage stubs needs to be followed by a nop, as the nop
16250 will be replaced with an instruction to restore the TOC
16251 base pointer. */
8387904d 16252 fdh = h;
b31867b6
AM
16253 if (h != NULL
16254 && h->oh != NULL
16255 && h->oh->is_func_descriptor)
16256 fdh = ppc_follow_link (h->oh);
31c76678
DK
16257 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16258 htab);
5663e321
AM
16259 if ((r_type == R_PPC64_PLTCALL
16260 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 16261 && stub_entry != NULL
7aba54da 16262 && stub_entry->type.main == ppc_stub_plt_call)
23cedd1d
AM
16263 stub_entry = NULL;
16264
6abec6bc 16265 if (stub_entry != NULL
7aba54da
AM
16266 && (stub_entry->type.main == ppc_stub_plt_call
16267 || stub_entry->type.r2save))
41bd81ab 16268 {
0a1b45a2 16269 bool can_plt_call = false;
721956f4 16270
7aba54da
AM
16271 if (r_type == R_PPC64_REL24_NOTOC
16272 || r_type == R_PPC64_REL24_P9NOTOC)
6e1816be 16273 {
7aba54da 16274 /* NOTOC calls don't need to restore r2. */
0a1b45a2 16275 can_plt_call = true;
6e1816be 16276 }
7aba54da
AM
16277 else if (stub_entry->type.main == ppc_stub_plt_call
16278 && !htab->opd_abi
16279 && htab->params->plt_localentry0 != 0
16280 && h != NULL
16281 && is_elfv2_localentry0 (&h->elf))
05d0e962 16282 {
7aba54da 16283 /* The function doesn't use or change r2. */
0a1b45a2 16284 can_plt_call = true;
05d0e962 16285 }
6e1816be 16286
f378ab09 16287 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
16288 branch and link followed by a nop. The nop is
16289 replaced by an insn to restore r2. */
733ae98c 16290 else if (offset_in_range (input_section, rel->r_offset, 8))
41bd81ab 16291 {
ba8ca3e7
AM
16292 unsigned long br;
16293
16294 br = bfd_get_32 (input_bfd,
16295 contents + rel->r_offset);
16296 if ((br & 1) != 0)
41bd81ab 16297 {
ba8ca3e7
AM
16298 unsigned long nop;
16299
16300 nop = bfd_get_32 (input_bfd,
16301 contents + rel->r_offset + 4);
23cedd1d 16302 if (nop == LD_R2_0R1 + STK_TOC (htab))
0a1b45a2 16303 can_plt_call = true;
23cedd1d
AM
16304 else if (nop == NOP
16305 || nop == CROR_151515
16306 || nop == CROR_313131)
a7f2871e 16307 {
ba8ca3e7 16308 if (h != NULL
ed7007c1 16309 && is_tls_get_addr (&h->elf, htab)
7c9cf415 16310 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
16311 {
16312 /* Special stub used, leave nop alone. */
16313 }
16314 else
a078d95a
AM
16315 bfd_put_32 (input_bfd,
16316 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7 16317 contents + rel->r_offset + 4);
0a1b45a2 16318 can_plt_call = true;
a7f2871e 16319 }
41bd81ab 16320 }
5bd4f169 16321 }
721956f4 16322
ba8ca3e7 16323 if (!can_plt_call && h != NULL)
721956f4 16324 {
ba8ca3e7
AM
16325 const char *name = h->elf.root.root.string;
16326
16327 if (*name == '.')
16328 ++name;
16329
3f3328b8 16330 if (startswith (name, "__libc_start_main")
ba8ca3e7 16331 && (name[17] == 0 || name[17] == '@'))
6ab189d5 16332 {
ba8ca3e7
AM
16333 /* Allow crt1 branch to go via a toc adjusting
16334 stub. Other calls that never return could do
16335 the same, if we could detect such. */
0a1b45a2 16336 can_plt_call = true;
6ab189d5 16337 }
ba8ca3e7
AM
16338 }
16339
16340 if (!can_plt_call)
16341 {
16342 /* g++ as of 20130507 emits self-calls without a
16343 following nop. This is arguably wrong since we
16344 have conflicting information. On the one hand a
16345 global symbol and on the other a local call
16346 sequence, but don't error for this special case.
16347 It isn't possible to cheaply verify we have
16348 exactly such a call. Allow all calls to the same
16349 section. */
16350 asection *code_sec = sec;
16351
16352 if (get_opd_info (sec) != NULL)
ad8e1ba5 16353 {
ba8ca3e7
AM
16354 bfd_vma off = (relocation + addend
16355 - sec->output_section->vma
16356 - sec->output_offset);
bc30df16 16357
0a1b45a2 16358 opd_entry_value (sec, off, &code_sec, NULL, false);
ad8e1ba5 16359 }
ba8ca3e7 16360 if (code_sec == input_section)
0a1b45a2 16361 can_plt_call = true;
ba8ca3e7
AM
16362 }
16363
16364 if (!can_plt_call)
16365 {
7aba54da 16366 if (stub_entry->type.main == ppc_stub_plt_call)
4805fc55 16367 info->callbacks->einfo
695344c0 16368 /* xgettext:c-format */
c1c8c1ef 16369 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16370 "(plt call stub)\n"),
4805fc55
AM
16371 input_bfd, input_section, rel->r_offset, sym_name);
16372 else
16373 info->callbacks->einfo
695344c0 16374 /* xgettext:c-format */
c1c8c1ef 16375 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16376 "(toc save/adjust stub)\n"),
4805fc55 16377 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
16378
16379 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16380 ret = false;
721956f4
AM
16381 }
16382
b25116a9 16383 if (can_plt_call
7aba54da 16384 && stub_entry->type.main == ppc_stub_plt_call)
0a1b45a2 16385 unresolved_reloc = false;
b25116a9
AM
16386 }
16387
6abec6bc 16388 if ((stub_entry == NULL
7aba54da
AM
16389 || stub_entry->type.main == ppc_stub_long_branch
16390 || stub_entry->type.main == ppc_stub_plt_branch)
8387904d
AM
16391 && get_opd_info (sec) != NULL)
16392 {
16393 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
16394 bfd_vma off = (relocation + addend
16395 - sec->output_section->vma
16396 - sec->output_offset);
0a1b45a2 16397 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
8387904d
AM
16398 if (dest != (bfd_vma) -1)
16399 {
16400 relocation = dest;
16401 addend = 0;
bc30df16 16402 reloc_dest = DEST_OPD;
8387904d
AM
16403 }
16404 }
16405
b25116a9
AM
16406 /* If the branch is out of reach we ought to have a long
16407 branch stub. */
16408 from = (rel->r_offset
16409 + input_section->output_offset
16410 + input_section->output_section->vma);
16411
6911b7dc
AM
16412 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16413 ? fdh->elf.other
16414 : sym->st_other);
16415
6abec6bc 16416 if (stub_entry != NULL
7aba54da
AM
16417 && (stub_entry->type.main == ppc_stub_long_branch
16418 || stub_entry->type.main == ppc_stub_plt_branch))
16419 {
16420 if (stub_entry->type.sub == ppc_stub_toc
16421 && !stub_entry->type.r2save
16422 && (r_type == R_PPC64_ADDR14_BRTAKEN
16423 || r_type == R_PPC64_ADDR14_BRNTAKEN
16424 || (relocation + addend - from + max_br_offset
16425 < 2 * max_br_offset)))
16426 /* Don't use the stub if this branch is in range. */
16427 stub_entry = NULL;
16428
16429 if (stub_entry != NULL
16430 && stub_entry->type.sub >= ppc_stub_notoc
16431 && ((r_type != R_PPC64_REL24_NOTOC
16432 && r_type != R_PPC64_REL24_P9NOTOC)
16433 || ((fdh ? fdh->elf.other : sym->st_other)
16434 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16435 && (relocation + addend - from + max_br_offset
16436 < 2 * max_br_offset))
16437 stub_entry = NULL;
16438
16439 if (stub_entry != NULL
16440 && stub_entry->type.r2save
16441 && (r_type == R_PPC64_REL24_NOTOC
16442 || r_type == R_PPC64_REL24_P9NOTOC)
16443 && (relocation + addend - from + max_br_offset
16444 < 2 * max_br_offset))
16445 stub_entry = NULL;
16446 }
05d0e962 16447
b25116a9
AM
16448 if (stub_entry != NULL)
16449 {
16450 /* Munge up the value and addend so that we call the stub
16451 rather than the procedure directly. */
a4b6fadd
AM
16452 asection *stub_sec = stub_entry->group->stub_sec;
16453
7aba54da 16454 if (stub_entry->type.main == ppc_stub_save_res)
a4b6fadd
AM
16455 relocation += (stub_sec->output_offset
16456 + stub_sec->output_section->vma
16457 + stub_sec->size - htab->sfpr->size
16458 - htab->sfpr->output_offset
16459 - htab->sfpr->output_section->vma);
16460 else
16461 relocation = (stub_entry->stub_offset
16462 + stub_sec->output_offset
16463 + stub_sec->output_section->vma);
b25116a9 16464 addend = 0;
bc30df16 16465 reloc_dest = DEST_STUB;
3b421ab3 16466
7aba54da
AM
16467 if (((stub_entry->type.r2save
16468 && (r_type == R_PPC64_REL24_NOTOC
16469 || r_type == R_PPC64_REL24_P9NOTOC))
16470 || ((stub_entry->type.main == ppc_stub_plt_call
16471 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16472 && rel + 1 < relend
16473 && rel[1].r_offset == rel->r_offset + 4
16474 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16475 && !(stub_entry->type.main == ppc_stub_plt_call
16476 && htab->params->tls_get_addr_opt
16477 && h != NULL
16478 && is_tls_get_addr (&h->elf, htab)))
29433886
AM
16479 {
16480 /* Skip over the r2 store at the start of the stub. */
7aba54da 16481 relocation += 4;
29433886 16482 }
5663e321 16483
7aba54da
AM
16484 if ((r_type == R_PPC64_REL24_NOTOC
16485 || r_type == R_PPC64_REL24_P9NOTOC)
16486 && stub_entry->type.main == ppc_stub_plt_call
16487 && stub_entry->type.sub >= ppc_stub_notoc)
5663e321 16488 htab->notoc_plt = 1;
b25116a9
AM
16489 }
16490
16491 if (insn != 0)
16492 {
794e51c0 16493 if (is_isa_v2)
721956f4 16494 {
b25116a9
AM
16495 /* Set 'a' bit. This is 0b00010 in BO field for branch
16496 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16497 for branch on CTR insns (BO == 1a00t or 1a01t). */
16498 if ((insn & (0x14 << 21)) == (0x04 << 21))
16499 insn |= 0x02 << 21;
16500 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16501 insn |= 0x08 << 21;
16502 else
16503 break;
16504 }
16505 else
16506 {
16507 /* Invert 'y' bit if not the default. */
4cc603a5 16508 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16509 insn ^= 0x01 << 21;
721956f4 16510 }
b25116a9 16511
95f0d0d2 16512 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16513 }
e86ce104 16514
06da1e8e
AM
16515 /* NOP out calls to undefined weak functions.
16516 We can thus call a weak function without first
16517 checking whether the function is defined. */
b25116a9 16518 else if (h != NULL
039b3fef 16519 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16520 && h->elf.dynindx == -1
05d0e962 16521 && (r_type == R_PPC64_REL24
7aba54da
AM
16522 || r_type == R_PPC64_REL24_NOTOC
16523 || r_type == R_PPC64_REL24_P9NOTOC)
b25116a9 16524 && relocation == 0
733ae98c
AM
16525 && addend == 0
16526 && offset_in_range (input_section, rel->r_offset, 4))
e86ce104 16527 {
95f0d0d2 16528 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16529 goto copy_reloc;
e86ce104 16530 }
65f38f15 16531 break;
066f4018
AM
16532
16533 case R_PPC64_GOT16_DS:
6738c8a7
AM
16534 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16535 || !htab->do_toc_opt)
06507dab 16536 break;
066f4018
AM
16537 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16538 if (relocation + addend - from + 0x8000 < 0x10000
f5b9c288
AM
16539 && sec != NULL
16540 && sec->output_section != NULL
16541 && !discarded_section (sec)
733ae98c
AM
16542 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16543 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16544 {
16545 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16546 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16547 {
16548 insn += (14u << 26) - (58u << 26);
16549 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16550 r_type = R_PPC64_TOC16;
16551 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16552 }
16553 }
16554 break;
16555
16556 case R_PPC64_GOT16_LO_DS:
16557 case R_PPC64_GOT16_HA:
6738c8a7
AM
16558 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16559 || !htab->do_toc_opt)
06507dab 16560 break;
066f4018
AM
16561 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16562 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
f5b9c288
AM
16563 && sec != NULL
16564 && sec->output_section != NULL
16565 && !discarded_section (sec)
733ae98c
AM
16566 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16567 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16568 {
16569 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16570 if (r_type == R_PPC64_GOT16_LO_DS
16571 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16572 {
16573 insn += (14u << 26) - (58u << 26);
16574 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16575 r_type = R_PPC64_TOC16_LO;
16576 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16577 }
fba8689a
AM
16578 else if (r_type == R_PPC64_GOT16_HA
16579 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16580 {
16581 r_type = R_PPC64_TOC16_HA;
16582 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16583 }
16584 }
16585 break;
4a421c53
AM
16586
16587 case R_PPC64_GOT_PCREL34:
6738c8a7
AM
16588 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16589 || !htab->do_toc_opt)
06507dab 16590 break;
4a421c53
AM
16591 from = (rel->r_offset
16592 + input_section->output_section->vma
16593 + input_section->output_offset);
6738c8a7 16594 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
f5b9c288
AM
16595 && sec != NULL
16596 && sec->output_section != NULL
16597 && !discarded_section (sec)
733ae98c
AM
16598 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16599 && offset_in_range (input_section, rel->r_offset, 8)))
6738c8a7
AM
16600 break;
16601
16602 offset = rel->r_offset;
16603 pinsn = bfd_get_32 (input_bfd, contents + offset);
16604 pinsn <<= 32;
16605 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16606 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16607 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16608 break;
16609
16610 /* Replace with paddi. */
16611 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16612 r_type = R_PPC64_PCREL34;
16613 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16614 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16615 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16616 /* Fall through. */
4a421c53
AM
16617
16618 case R_PPC64_PCREL34:
6738c8a7
AM
16619 if (!htab->params->no_pcrel_opt
16620 && rel + 1 < relend
16621 && rel[1].r_offset == rel->r_offset
16622 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
733ae98c
AM
16623 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16624 && offset_in_range (input_section, rel->r_offset, 8))
4a421c53
AM
16625 {
16626 offset = rel->r_offset;
16627 pinsn = bfd_get_32 (input_bfd, contents + offset);
16628 pinsn <<= 32;
16629 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16630 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16631 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16632 | (14ULL << 26) /* paddi */))
16633 {
6738c8a7
AM
16634 bfd_vma off2 = rel[1].r_addend;
16635 if (off2 == 0)
16636 /* zero means next insn. */
16637 off2 = 8;
16638 off2 += offset;
733ae98c 16639 if (offset_in_range (input_section, off2, 4))
4a421c53 16640 {
6738c8a7
AM
16641 uint64_t pinsn2;
16642 bfd_signed_vma addend_off;
16643 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16644 pinsn2 <<= 32;
16645 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16646 {
733ae98c 16647 if (!offset_in_range (input_section, off2, 8))
6738c8a7
AM
16648 break;
16649 pinsn2 |= bfd_get_32 (input_bfd,
16650 contents + off2 + 4);
16651 }
16652 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16653 {
16654 addend += addend_off;
16655 rel->r_addend = addend;
16656 bfd_put_32 (input_bfd, pinsn >> 32,
16657 contents + offset);
16658 bfd_put_32 (input_bfd, pinsn,
16659 contents + offset + 4);
16660 bfd_put_32 (input_bfd, pinsn2 >> 32,
16661 contents + off2);
4a421c53 16662 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16663 bfd_put_32 (input_bfd, pinsn2,
16664 contents + off2 + 4);
4a421c53
AM
16665 }
16666 }
16667 }
16668 }
16669 break;
65f38f15 16670 }
5bd4f169 16671
411e1bfb 16672 tls_type = 0;
23cedd1d 16673 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16674 switch (r_type)
16675 {
16676 default:
cf97bcb0
AM
16677 /* xgettext:c-format */
16678 _bfd_error_handler (_("%pB: %s unsupported"),
16679 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16680
65f38f15 16681 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16682 ret = false;
c316a17c 16683 goto copy_reloc;
5bd4f169 16684
65f38f15 16685 case R_PPC64_NONE:
411e1bfb 16686 case R_PPC64_TLS:
727fc41e
AM
16687 case R_PPC64_TLSGD:
16688 case R_PPC64_TLSLD:
3b421ab3 16689 case R_PPC64_TOCSAVE:
04c9666a
AM
16690 case R_PPC64_GNU_VTINHERIT:
16691 case R_PPC64_GNU_VTENTRY:
006589cf 16692 case R_PPC64_ENTRY:
4a421c53 16693 case R_PPC64_PCREL_OPT:
c316a17c 16694 goto copy_reloc;
5bd4f169
AM
16695
16696 /* GOT16 relocations. Like an ADDR16 using the symbol's
16697 address in the GOT as relocation value instead of the
411e1bfb 16698 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16699 symbol and put the symbol value there. */
411e1bfb
AM
16700 case R_PPC64_GOT_TLSGD16:
16701 case R_PPC64_GOT_TLSGD16_LO:
16702 case R_PPC64_GOT_TLSGD16_HI:
16703 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16704 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16705 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16706 goto dogot;
16707
16708 case R_PPC64_GOT_TLSLD16:
16709 case R_PPC64_GOT_TLSLD16_LO:
16710 case R_PPC64_GOT_TLSLD16_HI:
16711 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16712 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16713 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16714 goto dogot;
16715
16716 case R_PPC64_GOT_TPREL16_DS:
16717 case R_PPC64_GOT_TPREL16_LO_DS:
16718 case R_PPC64_GOT_TPREL16_HI:
16719 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16720 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16721 tls_type = TLS_TLS | TLS_TPREL;
16722 goto dogot;
16723
16724 case R_PPC64_GOT_DTPREL16_DS:
16725 case R_PPC64_GOT_DTPREL16_LO_DS:
16726 case R_PPC64_GOT_DTPREL16_HI:
16727 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16728 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16729 tls_type = TLS_TLS | TLS_DTPREL;
16730 goto dogot;
16731
65f38f15
AM
16732 case R_PPC64_GOT16:
16733 case R_PPC64_GOT16_LO:
16734 case R_PPC64_GOT16_HI:
16735 case R_PPC64_GOT16_HA:
16736 case R_PPC64_GOT16_DS:
16737 case R_PPC64_GOT16_LO_DS:
5663e321 16738 case R_PPC64_GOT_PCREL34:
411e1bfb 16739 dogot:
5bd4f169
AM
16740 {
16741 /* Relocation is to the entry for this symbol in the global
16742 offset table. */
e717da7e 16743 asection *got;
d881513a 16744 bfd_vma *offp;
5bd4f169 16745 bfd_vma off;
d881513a 16746 unsigned long indx = 0;
927be08e 16747 struct got_entry *ent;
65f38f15 16748
d881513a 16749 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16750 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16751 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16752 else
5bd4f169 16753 {
d881513a
AM
16754 if (h != NULL)
16755 {
f0158f44
AM
16756 if (!htab->elf.dynamic_sections_created
16757 || h->elf.dynindx == -1
16758 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16759 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16760 /* This is actually a static link, or it is a
16761 -Bsymbolic link and the symbol is defined
16762 locally, or the symbol was forced to be local
16763 because of a version file. */
16764 ;
16765 else
16766 {
039b3fef 16767 indx = h->elf.dynindx;
0a1b45a2 16768 unresolved_reloc = false;
d881513a 16769 }
039b3fef 16770 ent = h->elf.got.glist;
d881513a 16771 }
411e1bfb 16772 else
5bd4f169 16773 {
d881513a
AM
16774 if (local_got_ents == NULL)
16775 abort ();
16776 ent = local_got_ents[r_symndx];
5bd4f169 16777 }
d881513a
AM
16778
16779 for (; ent != NULL; ent = ent->next)
133a1f60 16780 if (ent->addend == orig_rel.r_addend
e717da7e 16781 && ent->owner == input_bfd
d881513a
AM
16782 && ent->tls_type == tls_type)
16783 break;
5bd4f169 16784 }
411e1bfb 16785
927be08e
AM
16786 if (ent == NULL)
16787 abort ();
16788 if (ent->is_indirect)
16789 ent = ent->got.ent;
16790 offp = &ent->got.offset;
16791 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16792 if (got == NULL)
16793 abort ();
16794
411e1bfb
AM
16795 /* The offset must always be a multiple of 8. We use the
16796 least significant bit to record whether we have already
16797 processed this entry. */
d881513a 16798 off = *offp;
411e1bfb
AM
16799 if ((off & 1) != 0)
16800 off &= ~1;
5bd4f169
AM
16801 else
16802 {
411e1bfb
AM
16803 /* Generate relocs for the dynamic linker, except in
16804 the case of TLSLD where we'll use one entry per
16805 module. */
25f23106 16806 asection *relgot;
0a1b45a2 16807 bool ifunc;
e717da7e 16808
d881513a 16809 *offp = off | 1;
25f23106
AM
16810 relgot = NULL;
16811 ifunc = (h != NULL
16812 ? h->elf.type == STT_GNU_IFUNC
16813 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16814 if (ifunc)
82e66161
AM
16815 {
16816 relgot = htab->elf.irelplt;
efb2a7b4 16817 if (indx == 0 || is_static_defined (&h->elf))
0a1b45a2 16818 htab->elf.ifunc_resolvers = true;
82e66161 16819 }
f0158f44
AM
16820 else if (indx != 0
16821 || (bfd_link_pic (info)
16822 && (h == NULL
f749f26e
AM
16823 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16824 && !(tls_type != 0
f15d0b54 16825 && bfd_link_executable (info)
9e51d549
AM
16826 && (h == NULL
16827 || SYMBOL_REFERENCES_LOCAL (info,
16828 &h->elf)))))
19e08130 16829 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16830 if (relgot != NULL)
5bd4f169 16831 {
e717da7e
AM
16832 outrel.r_offset = (got->output_section->vma
16833 + got->output_offset
411e1bfb 16834 + off);
133a1f60 16835 outrel.r_addend = orig_rel.r_addend;
d881513a 16836 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16837 {
411e1bfb 16838 outrel.r_addend = 0;
e515b051 16839 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16840 if (tls_type == (TLS_TLS | TLS_GD))
16841 {
e717da7e
AM
16842 loc = relgot->contents;
16843 loc += (relgot->reloc_count++
d881513a
AM
16844 * sizeof (Elf64_External_Rela));
16845 bfd_elf64_swap_reloca_out (output_bfd,
16846 &outrel, loc);
e515b051 16847 outrel.r_offset += 8;
133a1f60 16848 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16849 outrel.r_info
16850 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16851 }
411e1bfb 16852 }
951fd09b 16853 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16854 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16855 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16856 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16857 else if (indx != 0)
16858 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16859 else
81407a69 16860 {
25f23106
AM
16861 if (ifunc)
16862 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16863 else
16864 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16865
16866 /* Write the .got section contents for the sake
16867 of prelink. */
e717da7e 16868 loc = got->contents + off;
23fbd6fa
JJ
16869 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16870 loc);
81407a69 16871 }
81407a69
AM
16872
16873 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16874 {
16875 outrel.r_addend += relocation;
16876 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16877 {
16878 if (htab->elf.tls_sec == NULL)
16879 outrel.r_addend = 0;
16880 else
16881 outrel.r_addend -= htab->elf.tls_sec->vma;
16882 }
e515b051 16883 }
1657026c
AM
16884 if (!(info->enable_dt_relr
16885 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
16886 {
16887 loc = relgot->contents;
16888 loc += (relgot->reloc_count++
16889 * sizeof (Elf64_External_Rela));
16890 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16891 }
411e1bfb
AM
16892 }
16893
ad8e1ba5 16894 /* Init the .got section contents here if we're not
81407a69 16895 emitting a reloc. */
d881513a 16896 else
411e1bfb 16897 {
133a1f60 16898 relocation += orig_rel.r_addend;
f0158f44 16899 if (tls_type != 0)
411e1bfb 16900 {
989f9879
AM
16901 if (htab->elf.tls_sec == NULL)
16902 relocation = 0;
16903 else
16904 {
f0158f44
AM
16905 if (tls_type & TLS_LD)
16906 relocation = 0;
16907 else
16908 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16909 if (tls_type & TLS_TPREL)
989f9879
AM
16910 relocation += DTP_OFFSET - TP_OFFSET;
16911 }
5bd4f169 16912
f0158f44 16913 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16914 {
16915 bfd_put_64 (output_bfd, relocation,
e717da7e 16916 got->contents + off + 8);
676ee2b5 16917 relocation = 1;
7b609f53 16918 }
411e1bfb
AM
16919 }
16920 bfd_put_64 (output_bfd, relocation,
e717da7e 16921 got->contents + off);
5bd4f169
AM
16922 }
16923 }
16924
65f38f15
AM
16925 if (off >= (bfd_vma) -2)
16926 abort ();
16927
bf102f86 16928 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16929 addend = 0;
c213164a 16930 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16931 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16932 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16933 || r_type == R_PPC64_GOT_TPREL_PCREL34
16934 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 16935 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 16936 }
65f38f15
AM
16937 break;
16938
16939 case R_PPC64_PLT16_HA:
16940 case R_PPC64_PLT16_HI:
16941 case R_PPC64_PLT16_LO:
08be3224 16942 case R_PPC64_PLT16_LO_DS:
5663e321
AM
16943 case R_PPC64_PLT_PCREL34:
16944 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
16945 case R_PPC64_PLT32:
16946 case R_PPC64_PLT64:
23cedd1d 16947 case R_PPC64_PLTSEQ:
5663e321 16948 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 16949 case R_PPC64_PLTCALL:
5663e321 16950 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
16951 /* Relocation is to the entry for this symbol in the
16952 procedure linkage table. */
0a1b45a2 16953 unresolved_reloc = true;
cbf95972
AM
16954 {
16955 struct plt_entry **plt_list = NULL;
16956 if (h != NULL)
16957 plt_list = &h->elf.plt.plist;
16958 else if (local_got_ents != NULL)
16959 {
16960 struct plt_entry **local_plt = (struct plt_entry **)
16961 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 16962 plt_list = local_plt + r_symndx;
cbf95972
AM
16963 }
16964 if (plt_list)
16965 {
16966 struct plt_entry *ent;
65f38f15 16967
cbf95972
AM
16968 for (ent = *plt_list; ent != NULL; ent = ent->next)
16969 if (ent->plt.offset != (bfd_vma) -1
133a1f60 16970 && ent->addend == orig_rel.r_addend)
cbf95972
AM
16971 {
16972 asection *plt;
08be3224 16973 bfd_vma got;
cbf95972
AM
16974
16975 plt = htab->elf.splt;
30845f11 16976 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
16977 {
16978 if (h != NULL
16979 ? h->elf.type == STT_GNU_IFUNC
16980 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16981 plt = htab->elf.iplt;
16982 else
16983 plt = htab->pltlocal;
16984 }
16985 relocation = (plt->output_section->vma
16986 + plt->output_offset
16987 + ent->plt.offset);
08be3224 16988 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
16989 || r_type == R_PPC64_PLT16_HI
16990 || r_type == R_PPC64_PLT16_LO
16991 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
16992 {
16993 got = (elf_gp (output_bfd)
16994 + htab->sec_info[input_section->id].toc_off);
16995 relocation -= got;
16996 }
133a1f60 16997 addend = 0;
0a1b45a2 16998 unresolved_reloc = false;
cbf95972
AM
16999 break;
17000 }
17001 }
17002 }
65f38f15 17003 break;
5bd4f169 17004
0b13192e
AM
17005 case R_PPC64_TOC:
17006 /* Relocation value is TOC base. */
17007 relocation = TOCstart;
cf35638d 17008 if (r_symndx == STN_UNDEF)
6f20ed8a 17009 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
17010 else if (unresolved_reloc)
17011 ;
6f20ed8a
AM
17012 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17013 relocation += htab->sec_info[sec->id].toc_off;
0b13192e 17014 else
0a1b45a2 17015 unresolved_reloc = true;
ab96bf03 17016 goto dodyn;
0b13192e 17017
5bd4f169
AM
17018 /* TOC16 relocs. We want the offset relative to the TOC base,
17019 which is the address of the start of the TOC plus 0x8000.
17020 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17021 in this order. */
65f38f15
AM
17022 case R_PPC64_TOC16:
17023 case R_PPC64_TOC16_LO:
17024 case R_PPC64_TOC16_HI:
17025 case R_PPC64_TOC16_DS:
17026 case R_PPC64_TOC16_LO_DS:
17027 case R_PPC64_TOC16_HA:
6f20ed8a 17028 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
17029 if (h != NULL)
17030 goto dodyn;
5bd4f169
AM
17031 break;
17032
17033 /* Relocate against the beginning of the section. */
65f38f15
AM
17034 case R_PPC64_SECTOFF:
17035 case R_PPC64_SECTOFF_LO:
17036 case R_PPC64_SECTOFF_HI:
17037 case R_PPC64_SECTOFF_DS:
17038 case R_PPC64_SECTOFF_LO_DS:
17039 case R_PPC64_SECTOFF_HA:
4ce794b7 17040 if (sec != NULL)
65f38f15 17041 addend -= sec->output_section->vma;
5bd4f169
AM
17042 break;
17043
25f23106
AM
17044 case R_PPC64_REL16:
17045 case R_PPC64_REL16_LO:
17046 case R_PPC64_REL16_HI:
17047 case R_PPC64_REL16_HA:
4a969973
AM
17048 case R_PPC64_REL16_HIGH:
17049 case R_PPC64_REL16_HIGHA:
17050 case R_PPC64_REL16_HIGHER:
17051 case R_PPC64_REL16_HIGHERA:
17052 case R_PPC64_REL16_HIGHEST:
17053 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
17054 case R_PPC64_REL16_HIGHER34:
17055 case R_PPC64_REL16_HIGHERA34:
17056 case R_PPC64_REL16_HIGHEST34:
17057 case R_PPC64_REL16_HIGHESTA34:
a680de9a 17058 case R_PPC64_REL16DX_HA:
721956f4
AM
17059 case R_PPC64_REL14:
17060 case R_PPC64_REL14_BRNTAKEN:
17061 case R_PPC64_REL14_BRTAKEN:
5d1634d7 17062 case R_PPC64_REL24:
05d0e962 17063 case R_PPC64_REL24_NOTOC:
7aba54da 17064 case R_PPC64_REL24_P9NOTOC:
5663e321
AM
17065 case R_PPC64_PCREL34:
17066 case R_PPC64_PCREL28:
5d1634d7
AM
17067 break;
17068
411e1bfb
AM
17069 case R_PPC64_TPREL16:
17070 case R_PPC64_TPREL16_LO:
17071 case R_PPC64_TPREL16_HI:
17072 case R_PPC64_TPREL16_HA:
17073 case R_PPC64_TPREL16_DS:
17074 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
17075 case R_PPC64_TPREL16_HIGH:
17076 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
17077 case R_PPC64_TPREL16_HIGHER:
17078 case R_PPC64_TPREL16_HIGHERA:
17079 case R_PPC64_TPREL16_HIGHEST:
17080 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
17081 if (h != NULL
17082 && h->elf.root.type == bfd_link_hash_undefweak
733ae98c
AM
17083 && h->elf.dynindx == -1
17084 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
766bc656
AM
17085 {
17086 /* Make this relocation against an undefined weak symbol
17087 resolve to zero. This is really just a tweak, since
17088 code using weak externs ought to check that they are
17089 defined before using them. */
17090 bfd_byte *p = contents + rel->r_offset - d_offset;
17091
95f0d0d2 17092 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
17093 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17094 if (insn != 0)
95f0d0d2 17095 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
17096 break;
17097 }
beaddc1a
AM
17098 /* Fall through. */
17099
17100 case R_PPC64_TPREL34:
989f9879
AM
17101 if (htab->elf.tls_sec != NULL)
17102 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
17103 /* The TPREL16 relocs shouldn't really be used in shared
17104 libs or with non-local symbols as that will result in
17105 DT_TEXTREL being set, but support them anyway. */
17106 goto dodyn;
411e1bfb
AM
17107
17108 case R_PPC64_DTPREL16:
17109 case R_PPC64_DTPREL16_LO:
17110 case R_PPC64_DTPREL16_HI:
17111 case R_PPC64_DTPREL16_HA:
17112 case R_PPC64_DTPREL16_DS:
17113 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
17114 case R_PPC64_DTPREL16_HIGH:
17115 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
17116 case R_PPC64_DTPREL16_HIGHER:
17117 case R_PPC64_DTPREL16_HIGHERA:
17118 case R_PPC64_DTPREL16_HIGHEST:
17119 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 17120 case R_PPC64_DTPREL34:
989f9879
AM
17121 if (htab->elf.tls_sec != NULL)
17122 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
17123 break;
17124
45965137
AM
17125 case R_PPC64_ADDR64_LOCAL:
17126 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17127 ? h->elf.other
17128 : sym->st_other);
17129 break;
17130
e515b051
AM
17131 case R_PPC64_DTPMOD64:
17132 relocation = 1;
17133 addend = 0;
17134 goto dodyn;
17135
411e1bfb 17136 case R_PPC64_TPREL64:
989f9879
AM
17137 if (htab->elf.tls_sec != NULL)
17138 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
17139 goto dodyn;
17140
17141 case R_PPC64_DTPREL64:
989f9879
AM
17142 if (htab->elf.tls_sec != NULL)
17143 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 17144 /* Fall through. */
411e1bfb 17145
65f38f15
AM
17146 /* Relocations that may need to be propagated if this is a
17147 dynamic object. */
04c9666a 17148 case R_PPC64_REL30:
65f38f15
AM
17149 case R_PPC64_REL32:
17150 case R_PPC64_REL64:
17151 case R_PPC64_ADDR14:
17152 case R_PPC64_ADDR14_BRNTAKEN:
17153 case R_PPC64_ADDR14_BRTAKEN:
17154 case R_PPC64_ADDR16:
17155 case R_PPC64_ADDR16_DS:
17156 case R_PPC64_ADDR16_HA:
17157 case R_PPC64_ADDR16_HI:
f9c6b907
AM
17158 case R_PPC64_ADDR16_HIGH:
17159 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17160 case R_PPC64_ADDR16_HIGHER:
17161 case R_PPC64_ADDR16_HIGHERA:
17162 case R_PPC64_ADDR16_HIGHEST:
17163 case R_PPC64_ADDR16_HIGHESTA:
17164 case R_PPC64_ADDR16_LO:
17165 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
17166 case R_PPC64_ADDR16_HIGHER34:
17167 case R_PPC64_ADDR16_HIGHERA34:
17168 case R_PPC64_ADDR16_HIGHEST34:
17169 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 17170 case R_PPC64_ADDR24:
65f38f15
AM
17171 case R_PPC64_ADDR32:
17172 case R_PPC64_ADDR64:
17173 case R_PPC64_UADDR16:
17174 case R_PPC64_UADDR32:
17175 case R_PPC64_UADDR64:
5663e321
AM
17176 case R_PPC64_D34:
17177 case R_PPC64_D34_LO:
17178 case R_PPC64_D34_HI30:
17179 case R_PPC64_D34_HA30:
17180 case R_PPC64_D28:
411e1bfb 17181 dodyn:
5d1634d7 17182 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
17183 break;
17184
41bd81ab
AM
17185 if (NO_OPD_RELOCS && is_opd)
17186 break;
17187
8a9e8e72 17188 if (bfd_link_pic (info)
b1b07054 17189 ? ((h == NULL
190eb1dd 17190 || h->elf.dyn_relocs != NULL)
b1b07054
AM
17191 && ((h != NULL && pc_dynrelocs (h))
17192 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 17193 : (h != NULL
190eb1dd 17194 ? h->elf.dyn_relocs != NULL
d311bc8b 17195 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 17196 {
0a1b45a2 17197 bool skip, relocate;
65f38f15 17198 asection *sreloc;
1cf1f670 17199 bfd_vma out_off;
82e66161 17200 long indx = 0;
65f38f15
AM
17201
17202 /* When generating a dynamic object, these relocations
17203 are copied into the output file to be resolved at run
17204 time. */
17205
0a1b45a2
AM
17206 skip = false;
17207 relocate = false;
65f38f15 17208
1cf1f670
AM
17209 out_off = _bfd_elf_section_offset (output_bfd, info,
17210 input_section, rel->r_offset);
17211 if (out_off == (bfd_vma) -1)
0a1b45a2 17212 skip = true;
1cf1f670 17213 else if (out_off == (bfd_vma) -2)
0a1b45a2 17214 skip = true, relocate = true;
1cf1f670
AM
17215 out_off += (input_section->output_section->vma
17216 + input_section->output_offset);
17217 outrel.r_offset = out_off;
411e1bfb 17218 outrel.r_addend = rel->r_addend;
65f38f15 17219
1cf1f670
AM
17220 /* Optimize unaligned reloc use. */
17221 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17222 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17223 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17224 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17225 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17226 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17227 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17228 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17229 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17230
65f38f15 17231 if (skip)
0bb2d96a 17232 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
17233 else if (h != NULL
17234 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
17235 && !is_opd
17236 && r_type != R_PPC64_TOC)
14acf4dc 17237 {
82e66161
AM
17238 indx = h->elf.dynindx;
17239 BFD_ASSERT (indx != -1);
17240 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 17241 }
65f38f15
AM
17242 else
17243 {
41bd81ab
AM
17244 /* This symbol is local, or marked to become local,
17245 or this is an opd section reloc which must point
17246 at a local function. */
65f38f15 17247 outrel.r_addend += relocation;
e86ce104 17248 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 17249 {
3fad3c7c 17250 if (is_opd && h != NULL)
afbe61cf
AM
17251 {
17252 /* Lie about opd entries. This case occurs
17253 when building shared libraries and we
17254 reference a function in another shared
3fad3c7c
AM
17255 lib. The same thing happens for a weak
17256 definition in an application that's
17257 overridden by a strong definition in a
17258 shared lib. (I believe this is a generic
17259 bug in binutils handling of weak syms.)
17260 In these cases we won't use the opd
1e2f5b6e 17261 entry in this lib. */
0a1b45a2 17262 unresolved_reloc = false;
afbe61cf 17263 }
25f23106
AM
17264 if (!is_opd
17265 && r_type == R_PPC64_ADDR64
17266 && (h != NULL
17267 ? h->elf.type == STT_GNU_IFUNC
17268 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17269 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17270 else
17271 {
17272 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 17273
25f23106
AM
17274 /* We need to relocate .opd contents for ld.so.
17275 Prelink also wants simple and consistent rules
17276 for relocs. This make all RELATIVE relocs have
17277 *r_offset equal to r_addend. */
0a1b45a2 17278 relocate = true;
25f23106 17279 }
65f38f15
AM
17280 }
17281 else
17282 {
25f23106
AM
17283 if (h != NULL
17284 ? h->elf.type == STT_GNU_IFUNC
17285 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17286 {
25f53a85 17287 info->callbacks->einfo
695344c0 17288 /* xgettext:c-format */
174d0a74 17289 (_("%H: %s for indirect "
c1c8c1ef 17290 "function `%pT' unsupported\n"),
25f53a85 17291 input_bfd, input_section, rel->r_offset,
25f23106
AM
17292 ppc64_elf_howto_table[r_type]->name,
17293 sym_name);
0a1b45a2 17294 ret = false;
25f23106 17295 }
cf35638d 17296 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
17297 ;
17298 else if (sec == NULL || sec->owner == NULL)
17299 {
17300 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17301 return false;
65f38f15
AM
17302 }
17303 else
17304 {
f26a3287 17305 asection *osec = sec->output_section;
65f38f15 17306
f26a3287
AM
17307 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17308 {
17309 /* TLS symbol values are relative to the
17310 TLS segment. Dynamic relocations for
17311 local TLS symbols therefore can't be
17312 reduced to a relocation against their
17313 section symbol because it holds the
17314 address of the section, not a value
17315 relative to the TLS segment. We could
17316 change the .tdata dynamic section symbol
17317 to be zero value but STN_UNDEF works
17318 and is used elsewhere, eg. for TPREL64
17319 GOT relocs against local TLS symbols. */
17320 osec = htab->elf.tls_sec;
17321 indx = 0;
17322 }
17323 else
74541ad4 17324 {
74541ad4 17325 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
17326 if (indx == 0)
17327 {
17328 if ((osec->flags & SEC_READONLY) == 0
17329 && htab->elf.data_index_section != NULL)
17330 osec = htab->elf.data_index_section;
17331 else
17332 osec = htab->elf.text_index_section;
17333 indx = elf_section_data (osec)->dynindx;
17334 }
17335 BFD_ASSERT (indx != 0);
74541ad4 17336 }
74541ad4 17337
65f38f15
AM
17338 /* We are turning this relocation into one
17339 against a section symbol, so subtract out
17340 the output section's address but not the
17341 offset of the input section in the output
17342 section. */
17343 outrel.r_addend -= osec->vma;
17344 }
17345
17346 outrel.r_info = ELF64_R_INFO (indx, r_type);
17347 }
17348 }
17349
1657026c
AM
17350 if (!(info->enable_dt_relr
17351 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17352 && rel->r_offset % 2 == 0
17353 && input_section->alignment_power != 0
17354 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
82e66161 17355 {
1657026c
AM
17356 sreloc = elf_section_data (input_section)->sreloc;
17357 if (h != NULL
17358 ? h->elf.type == STT_GNU_IFUNC
17359 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17360 {
17361 sreloc = htab->elf.irelplt;
17362 if (indx == 0 || is_static_defined (&h->elf))
17363 htab->elf.ifunc_resolvers = true;
17364 }
17365 if (sreloc == NULL)
17366 abort ();
17367
17368 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17369 >= sreloc->size)
17370 abort ();
17371 loc = sreloc->contents;
17372 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17373 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
82e66161 17374 }
65f38f15 17375
e59a1001
AM
17376 if (!warned_dynamic
17377 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17378 {
17379 info->callbacks->einfo
17380 /* xgettext:c-format */
17381 (_("%X%P: %pB: %s against %pT "
17382 "is not supported by glibc as a dynamic relocation\n"),
17383 input_bfd,
17384 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17385 sym_name);
0a1b45a2 17386 warned_dynamic = true;
e59a1001
AM
17387 }
17388
65f38f15
AM
17389 /* If this reloc is against an external symbol, it will
17390 be computed at runtime, so there's no need to do
81407a69
AM
17391 anything now. However, for the sake of prelink ensure
17392 that the section contents are a known value. */
2cdcc330 17393 if (!relocate)
81407a69 17394 {
0a1b45a2 17395 unresolved_reloc = false;
81407a69
AM
17396 /* The value chosen here is quite arbitrary as ld.so
17397 ignores section contents except for the special
17398 case of .opd where the contents might be accessed
17399 before relocation. Choose zero, as that won't
17400 cause reloc overflow. */
17401 relocation = 0;
17402 addend = 0;
17403 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17404 to improve backward compatibility with older
17405 versions of ld. */
17406 if (r_type == R_PPC64_ADDR64)
17407 addend = outrel.r_addend;
17408 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 17409 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 17410 addend = outrel.r_offset;
81407a69 17411 }
65f38f15 17412 }
5bd4f169
AM
17413 break;
17414
65f38f15
AM
17415 case R_PPC64_COPY:
17416 case R_PPC64_GLOB_DAT:
17417 case R_PPC64_JMP_SLOT:
25f23106 17418 case R_PPC64_JMP_IREL:
65f38f15
AM
17419 case R_PPC64_RELATIVE:
17420 /* We shouldn't ever see these dynamic relocs in relocatable
17421 files. */
ae9a127f 17422 /* Fall through. */
65f38f15
AM
17423
17424 case R_PPC64_PLTGOT16:
17425 case R_PPC64_PLTGOT16_DS:
17426 case R_PPC64_PLTGOT16_HA:
17427 case R_PPC64_PLTGOT16_HI:
17428 case R_PPC64_PLTGOT16_LO:
17429 case R_PPC64_PLTGOT16_LO_DS:
17430 case R_PPC64_PLTREL32:
17431 case R_PPC64_PLTREL64:
17432 /* These ones haven't been implemented yet. */
17433
25f53a85 17434 info->callbacks->einfo
695344c0 17435 /* xgettext:c-format */
c1c8c1ef 17436 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 17437 input_bfd,
4ce794b7 17438 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
17439
17440 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 17441 ret = false;
c316a17c 17442 goto copy_reloc;
65f38f15 17443 }
5bd4f169 17444
67f0cbdb
AM
17445 /* Multi-instruction sequences that access the TOC can be
17446 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 17447 to nop; addi rb,r2,x; */
67f0cbdb
AM
17448 switch (r_type)
17449 {
17450 default:
17451 break;
17452
17453 case R_PPC64_GOT_TLSLD16_HI:
17454 case R_PPC64_GOT_TLSGD16_HI:
17455 case R_PPC64_GOT_TPREL16_HI:
17456 case R_PPC64_GOT_DTPREL16_HI:
17457 case R_PPC64_GOT16_HI:
17458 case R_PPC64_TOC16_HI:
17459 /* These relocs would only be useful if building up an
17460 offset to later add to r2, perhaps in an indexed
17461 addressing mode instruction. Don't try to optimize.
17462 Unfortunately, the possibility of someone building up an
17463 offset like this or even with the HA relocs, means that
17464 we need to check the high insn when optimizing the low
17465 insn. */
17466 break;
17467
5663e321
AM
17468 case R_PPC64_PLTCALL_NOTOC:
17469 if (!unresolved_reloc)
17470 htab->notoc_plt = 1;
17471 /* Fall through. */
23cedd1d 17472 case R_PPC64_PLTCALL:
733ae98c
AM
17473 if (unresolved_reloc
17474 && offset_in_range (input_section, rel->r_offset,
17475 r_type == R_PPC64_PLTCALL ? 8 : 4))
23cedd1d
AM
17476 {
17477 /* No plt entry. Make this into a direct call. */
17478 bfd_byte *p = contents + rel->r_offset;
17479 insn = bfd_get_32 (input_bfd, p);
17480 insn &= 1;
17481 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
17482 if (r_type == R_PPC64_PLTCALL)
17483 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
17484 unresolved_reloc = save_unresolved_reloc;
17485 r_type = R_PPC64_REL24;
17486 }
17487 break;
17488
5663e321 17489 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17490 case R_PPC64_PLTSEQ:
17491 if (unresolved_reloc)
17492 {
0a1b45a2 17493 unresolved_reloc = false;
23cedd1d
AM
17494 goto nop_it;
17495 }
17496 break;
17497
5663e321
AM
17498 case R_PPC64_PLT_PCREL34_NOTOC:
17499 if (!unresolved_reloc)
17500 htab->notoc_plt = 1;
17501 /* Fall through. */
17502 case R_PPC64_PLT_PCREL34:
733ae98c
AM
17503 if (unresolved_reloc
17504 && offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17505 {
17506 bfd_byte *p = contents + rel->r_offset;
17507 bfd_put_32 (input_bfd, PNOP >> 32, p);
17508 bfd_put_32 (input_bfd, PNOP, p + 4);
0a1b45a2 17509 unresolved_reloc = false;
5663e321
AM
17510 goto copy_reloc;
17511 }
17512 break;
17513
23cedd1d
AM
17514 case R_PPC64_PLT16_HA:
17515 if (unresolved_reloc)
17516 {
0a1b45a2 17517 unresolved_reloc = false;
23cedd1d
AM
17518 goto nop_it;
17519 }
17520 /* Fall through. */
67f0cbdb
AM
17521 case R_PPC64_GOT_TLSLD16_HA:
17522 case R_PPC64_GOT_TLSGD16_HA:
17523 case R_PPC64_GOT_TPREL16_HA:
17524 case R_PPC64_GOT_DTPREL16_HA:
17525 case R_PPC64_GOT16_HA:
17526 case R_PPC64_TOC16_HA:
98528052 17527 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17528 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 17529 {
23cedd1d
AM
17530 bfd_byte *p;
17531 nop_it:
733ae98c
AM
17532 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17533 {
17534 p = contents + (rel->r_offset & ~3);
17535 bfd_put_32 (input_bfd, NOP, p);
17536 goto copy_reloc;
17537 }
98528052 17538 }
67f0cbdb
AM
17539 break;
17540
23cedd1d
AM
17541 case R_PPC64_PLT16_LO:
17542 case R_PPC64_PLT16_LO_DS:
17543 if (unresolved_reloc)
17544 {
0a1b45a2 17545 unresolved_reloc = false;
23cedd1d
AM
17546 goto nop_it;
17547 }
17548 /* Fall through. */
67f0cbdb
AM
17549 case R_PPC64_GOT_TLSLD16_LO:
17550 case R_PPC64_GOT_TLSGD16_LO:
17551 case R_PPC64_GOT_TPREL16_LO_DS:
17552 case R_PPC64_GOT_DTPREL16_LO_DS:
17553 case R_PPC64_GOT16_LO:
17554 case R_PPC64_GOT16_LO_DS:
17555 case R_PPC64_TOC16_LO:
17556 case R_PPC64_TOC16_LO_DS:
98528052 17557 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
733ae98c
AM
17558 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17559 && offset_in_range (input_section, rel->r_offset & ~3, 4))
67f0cbdb
AM
17560 {
17561 bfd_byte *p = contents + (rel->r_offset & ~3);
17562 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17563 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17564 {
17565 /* Transform addic to addi when we change reg. */
2365f8d7 17566 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17567 insn |= (14u << 26) | (2 << 16);
17568 }
17569 else
67f0cbdb 17570 {
98528052
AM
17571 insn &= ~(0x1f << 16);
17572 insn |= 2 << 16;
67f0cbdb 17573 }
560c8763 17574 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17575 }
17576 break;
9a23f96e
AM
17577
17578 case R_PPC64_TPREL16_HA:
733ae98c
AM
17579 if (htab->do_tls_opt
17580 && relocation + addend + 0x8000 < 0x10000
17581 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17582 {
17583 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17584 bfd_put_32 (input_bfd, NOP, p);
17585 goto copy_reloc;
9a23f96e
AM
17586 }
17587 break;
17588
17589 case R_PPC64_TPREL16_LO:
17590 case R_PPC64_TPREL16_LO_DS:
733ae98c
AM
17591 if (htab->do_tls_opt
17592 && relocation + addend + 0x8000 < 0x10000
17593 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17594 {
17595 bfd_byte *p = contents + (rel->r_offset & ~3);
17596 insn = bfd_get_32 (input_bfd, p);
17597 insn &= ~(0x1f << 16);
17598 insn |= 13 << 16;
17599 bfd_put_32 (input_bfd, insn, p);
17600 }
17601 break;
67f0cbdb
AM
17602 }
17603
65f38f15
AM
17604 /* Do any further special processing. */
17605 switch (r_type)
17606 {
17607 default:
17608 break;
17609
25f23106 17610 case R_PPC64_REL16_HA:
4a969973
AM
17611 case R_PPC64_REL16_HIGHA:
17612 case R_PPC64_REL16_HIGHERA:
17613 case R_PPC64_REL16_HIGHESTA:
a680de9a 17614 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17615 case R_PPC64_ADDR16_HA:
17616 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17617 case R_PPC64_ADDR16_HIGHERA:
17618 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17619 case R_PPC64_TOC16_HA:
17620 case R_PPC64_SECTOFF_HA:
411e1bfb 17621 case R_PPC64_TPREL16_HA:
f9c6b907 17622 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17623 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17624 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17625 case R_PPC64_DTPREL16_HA:
17626 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17627 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17628 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17629 /* It's just possible that this symbol is a weak symbol
17630 that's not actually defined anywhere. In that case,
17631 'sec' would be NULL, and we should leave the symbol
17632 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17633 if (sec == NULL)
17634 break;
1a0670f3 17635 /* Fall through. */
5c5f6e17
AM
17636
17637 case R_PPC64_GOT16_HA:
17638 case R_PPC64_PLTGOT16_HA:
17639 case R_PPC64_PLT16_HA:
17640 case R_PPC64_GOT_TLSGD16_HA:
17641 case R_PPC64_GOT_TLSLD16_HA:
17642 case R_PPC64_GOT_TPREL16_HA:
17643 case R_PPC64_GOT_DTPREL16_HA:
17644 /* Add 0x10000 if sign bit in 0:15 is set.
17645 Bits 0:15 are not used. */
17646 addend += 0x8000;
65f38f15
AM
17647 break;
17648
5663e321
AM
17649 case R_PPC64_D34_HA30:
17650 case R_PPC64_ADDR16_HIGHERA34:
17651 case R_PPC64_ADDR16_HIGHESTA34:
17652 case R_PPC64_REL16_HIGHERA34:
17653 case R_PPC64_REL16_HIGHESTA34:
17654 if (sec != NULL)
17655 addend += 1ULL << 33;
17656 break;
17657
65f38f15
AM
17658 case R_PPC64_ADDR16_DS:
17659 case R_PPC64_ADDR16_LO_DS:
17660 case R_PPC64_GOT16_DS:
17661 case R_PPC64_GOT16_LO_DS:
17662 case R_PPC64_PLT16_LO_DS:
17663 case R_PPC64_SECTOFF_DS:
17664 case R_PPC64_SECTOFF_LO_DS:
17665 case R_PPC64_TOC16_DS:
17666 case R_PPC64_TOC16_LO_DS:
17667 case R_PPC64_PLTGOT16_DS:
17668 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17669 case R_PPC64_GOT_TPREL16_DS:
17670 case R_PPC64_GOT_TPREL16_LO_DS:
17671 case R_PPC64_GOT_DTPREL16_DS:
17672 case R_PPC64_GOT_DTPREL16_LO_DS:
17673 case R_PPC64_TPREL16_DS:
17674 case R_PPC64_TPREL16_LO_DS:
17675 case R_PPC64_DTPREL16_DS:
17676 case R_PPC64_DTPREL16_LO_DS:
733ae98c
AM
17677 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17678 break;
adadcc0c
AM
17679 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17680 mask = 3;
a680de9a
PB
17681 /* If this reloc is against an lq, lxv, or stxv insn, then
17682 the value must be a multiple of 16. This is somewhat of
17683 a hack, but the "correct" way to do this by defining _DQ
17684 forms of all the _DS relocs bloats all reloc switches in
17685 this file. It doesn't make much sense to use these
17686 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17687 if ((insn & (0x3fu << 26)) == (56u << 26)
17688 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17689 mask = 15;
a680de9a
PB
17690 relocation += addend;
17691 addend = insn & (mask ^ 3);
17692 if ((relocation & mask) != 0)
65f38f15 17693 {
a680de9a 17694 relocation ^= relocation & mask;
25f53a85 17695 info->callbacks->einfo
695344c0 17696 /* xgettext:c-format */
174d0a74 17697 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17698 input_bfd, input_section, rel->r_offset,
d830549d 17699 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17700 mask + 1);
65f38f15 17701 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17702 ret = false;
c316a17c 17703 goto copy_reloc;
65f38f15
AM
17704 }
17705 break;
5bd4f169
AM
17706 }
17707
239e1f3a
AM
17708 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17709 because such sections are not SEC_ALLOC and thus ld.so will
17710 not process them. */
d830549d 17711 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17712 if (unresolved_reloc
239e1f3a 17713 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17714 && h->elf.def_dynamic)
17715 && _bfd_elf_section_offset (output_bfd, info, input_section,
17716 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17717 {
25f53a85 17718 info->callbacks->einfo
695344c0 17719 /* xgettext:c-format */
c1c8c1ef 17720 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17721 input_bfd, input_section, rel->r_offset,
b80eed39 17722 howto->name,
039b3fef 17723 h->elf.root.root.string);
0a1b45a2 17724 ret = false;
9c07fe7c 17725 }
5bd4f169 17726
b80eed39
AM
17727 /* 16-bit fields in insns mostly have signed values, but a
17728 few insns have 16-bit unsigned values. Really, we should
17729 have different reloc types. */
17730 if (howto->complain_on_overflow != complain_overflow_dont
17731 && howto->dst_mask == 0xffff
733ae98c
AM
17732 && (input_section->flags & SEC_CODE) != 0
17733 && offset_in_range (input_section, rel->r_offset & ~3, 4))
b80eed39
AM
17734 {
17735 enum complain_overflow complain = complain_overflow_signed;
17736
17737 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17738 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17739 complain = complain_overflow_bitfield;
17740 else if (howto->rightshift == 0
2365f8d7
AM
17741 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17742 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17743 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17744 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17745 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17746 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17747 complain = complain_overflow_unsigned;
17748 if (howto->complain_on_overflow != complain)
17749 {
17750 alt_howto = *howto;
17751 alt_howto.complain_on_overflow = complain;
17752 howto = &alt_howto;
17753 }
17754 }
17755
5663e321 17756 switch (r_type)
a680de9a 17757 {
5663e321
AM
17758 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17759 case R_PPC64_D34:
17760 case R_PPC64_D34_LO:
17761 case R_PPC64_D34_HI30:
17762 case R_PPC64_D34_HA30:
17763 case R_PPC64_PCREL34:
17764 case R_PPC64_GOT_PCREL34:
c213164a
AM
17765 case R_PPC64_TPREL34:
17766 case R_PPC64_DTPREL34:
87c69f97
AM
17767 case R_PPC64_GOT_TLSGD_PCREL34:
17768 case R_PPC64_GOT_TLSLD_PCREL34:
17769 case R_PPC64_GOT_TPREL_PCREL34:
17770 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17771 case R_PPC64_PLT_PCREL34:
17772 case R_PPC64_PLT_PCREL34_NOTOC:
17773 case R_PPC64_D28:
17774 case R_PPC64_PCREL28:
733ae98c 17775 if (!offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17776 r = bfd_reloc_outofrange;
17777 else
17778 {
5663e321
AM
17779 relocation += addend;
17780 if (howto->pc_relative)
17781 relocation -= (rel->r_offset
17782 + input_section->output_offset
17783 + input_section->output_section->vma);
17784 relocation >>= howto->rightshift;
17785
17786 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17787 pinsn <<= 32;
17788 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17789
17790 pinsn &= ~howto->dst_mask;
17791 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17792 & howto->dst_mask);
17793 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17794 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17795 r = bfd_reloc_ok;
17796 if (howto->complain_on_overflow == complain_overflow_signed
17797 && (relocation + (1ULL << (howto->bitsize - 1))
17798 >= 1ULL << howto->bitsize))
17799 r = bfd_reloc_overflow;
17800 }
17801 break;
17802
17803 case R_PPC64_REL16DX_HA:
733ae98c 17804 if (!offset_in_range (input_section, rel->r_offset, 4))
a680de9a
PB
17805 r = bfd_reloc_outofrange;
17806 else
17807 {
17808 relocation += addend;
17809 relocation -= (rel->r_offset
17810 + input_section->output_offset
17811 + input_section->output_section->vma);
3de43e7b 17812 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17813 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17814 insn &= ~0x1fffc1;
3de43e7b 17815 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17816 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17817 r = bfd_reloc_ok;
3de43e7b 17818 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17819 r = bfd_reloc_overflow;
17820 }
5663e321
AM
17821 break;
17822
17823 default:
17824 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17825 contents, rel->r_offset,
17826 relocation, addend);
a680de9a 17827 }
5bd4f169 17828
ef60b7ff 17829 if (r != bfd_reloc_ok)
5bd4f169 17830 {
bc30df16 17831 char *more_info = NULL;
b80eed39 17832 const char *reloc_name = howto->name;
bc30df16
AM
17833
17834 if (reloc_dest != DEST_NORMAL)
17835 {
17836 more_info = bfd_malloc (strlen (reloc_name) + 8);
17837 if (more_info != NULL)
17838 {
17839 strcpy (more_info, reloc_name);
17840 strcat (more_info, (reloc_dest == DEST_OPD
17841 ? " (OPD)" : " (stub)"));
17842 reloc_name = more_info;
17843 }
17844 }
17845
cd27b276 17846 if (r == bfd_reloc_overflow)
5bd4f169 17847 {
8131c122
AM
17848 /* On code like "if (foo) foo();" don't report overflow
17849 on a branch to zero when foo is undefined. */
17850 if (!warned
17851 && (reloc_dest == DEST_STUB
17852 || !(h != NULL
17853 && (h->elf.root.type == bfd_link_hash_undefweak
17854 || h->elf.root.type == bfd_link_hash_undefined)
17855 && is_branch_reloc (r_type))))
9e51d549
AM
17856 info->callbacks->reloc_overflow
17857 (info, (struct bfd_link_hash_entry *) h, sym_name,
17858 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17859 rel->r_offset);
ef60b7ff
AM
17860 }
17861 else
17862 {
25f53a85 17863 info->callbacks->einfo
695344c0 17864 /* xgettext:c-format */
c1c8c1ef 17865 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17866 input_bfd, input_section, rel->r_offset,
bc30df16 17867 reloc_name, sym_name, (int) r);
0a1b45a2 17868 ret = false;
ef60b7ff 17869 }
c9594989 17870 free (more_info);
5bd4f169 17871 }
c316a17c
AM
17872 copy_reloc:
17873 if (wrel != rel)
17874 *wrel = *rel;
17875 }
17876
17877 if (wrel != rel)
17878 {
17879 Elf_Internal_Shdr *rel_hdr;
17880 size_t deleted = rel - wrel;
17881
17882 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17883 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17884 if (rel_hdr->sh_size == 0)
17885 {
17886 /* It is too late to remove an empty reloc section. Leave
17887 one NONE reloc.
17888 ??? What is wrong with an empty section??? */
17889 rel_hdr->sh_size = rel_hdr->sh_entsize;
17890 deleted -= 1;
17891 }
17892 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17893 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17894 input_section->reloc_count -= deleted;
5bd4f169
AM
17895 }
17896
645ea6a9
AM
17897 /* If we're emitting relocations, then shortly after this function
17898 returns, reloc offsets and addends for this section will be
17899 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17900 file rather than the input. Save a copy of the relocs for
17901 opd_entry_value. */
0e1862bb 17902 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17903 {
17904 bfd_size_type amt;
17905 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17906 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17907 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17908 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f 17909 if (rel == NULL)
0a1b45a2 17910 return false;
8860955f
AM
17911 memcpy (rel, relocs, amt);
17912 }
5bd4f169
AM
17913 return ret;
17914}
17915
754021d0
AM
17916/* Adjust the value of any local symbols in opd sections. */
17917
6e0b88f1 17918static int
754021d0
AM
17919ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17920 const char *name ATTRIBUTE_UNUSED,
17921 Elf_Internal_Sym *elfsym,
17922 asection *input_sec,
17923 struct elf_link_hash_entry *h)
17924{
74f0fb50
AM
17925 struct _opd_sec_data *opd;
17926 long adjust;
754021d0
AM
17927 bfd_vma value;
17928
4025353c 17929 if (h != NULL)
6e0b88f1 17930 return 1;
4025353c 17931
74f0fb50
AM
17932 opd = get_opd_info (input_sec);
17933 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 17934 return 1;
754021d0
AM
17935
17936 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 17937 if (!bfd_link_relocatable (info))
754021d0
AM
17938 value -= input_sec->output_section->vma;
17939
51aecdc5 17940 adjust = opd->adjust[OPD_NDX (value)];
4025353c 17941 if (adjust == -1)
6e0b88f1
AM
17942 return 2;
17943
17944 elfsym->st_value += adjust;
17945 return 1;
754021d0
AM
17946}
17947
5bd4f169
AM
17948/* Finish up dynamic symbol handling. We set the contents of various
17949 dynamic sections here. */
17950
0a1b45a2 17951static bool
4ce794b7
AM
17952ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17953 struct bfd_link_info *info,
17954 struct elf_link_hash_entry *h,
4aef7643 17955 Elf_Internal_Sym *sym)
5bd4f169 17956{
65f38f15 17957 struct ppc_link_hash_table *htab;
8387904d 17958 struct plt_entry *ent;
5bd4f169 17959
65f38f15 17960 htab = ppc_hash_table (info);
4dfe6ac6 17961 if (htab == NULL)
0a1b45a2 17962 return false;
5bd4f169 17963
49c09209
AM
17964 if (!htab->opd_abi && !h->def_regular)
17965 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17966 if (ent->plt.offset != (bfd_vma) -1)
17967 {
17968 /* Mark the symbol as undefined, rather than as
17969 defined in glink. Leave the value if there were
17970 any relocations where pointer equality matters
17971 (this is a clue for the dynamic linker, to make
17972 function pointer comparisons work between an
17973 application and shared library), otherwise set it
17974 to zero. */
17975 sym->st_shndx = SHN_UNDEF;
17976 if (!h->pointer_equality_needed)
17977 sym->st_value = 0;
17978 else if (!h->ref_regular_nonweak)
17979 {
17980 /* This breaks function pointer comparisons, but
17981 that is better than breaking tests for a NULL
17982 function pointer. */
17983 sym->st_value = 0;
17984 }
17985 break;
17986 }
5bd4f169 17987
1bdd8fac
AM
17988 if (h->needs_copy
17989 && (h->root.type == bfd_link_hash_defined
17990 || h->root.type == bfd_link_hash_defweak)
17991 && (h->root.u.def.section == htab->elf.sdynbss
17992 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 17993 {
65f38f15 17994 /* This symbol needs a copy reloc. Set it up. */
49c09209 17995 Elf_Internal_Rela rela;
5474d94f 17996 asection *srel;
49c09209 17997 bfd_byte *loc;
5bd4f169 17998
1bdd8fac 17999 if (h->dynindx == -1)
65f38f15 18000 abort ();
5bd4f169 18001
ed7007c1 18002 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
18003 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18004 rela.r_addend = 0;
afbf7e8e 18005 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
18006 srel = htab->elf.sreldynrelro;
18007 else
18008 srel = htab->elf.srelbss;
18009 loc = srel->contents;
18010 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 18011 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
18012 }
18013
0a1b45a2 18014 return true;
5bd4f169
AM
18015}
18016
65f38f15
AM
18017/* Used to decide how to sort relocs in an optimal manner for the
18018 dynamic linker, before writing them out. */
18019
18020static enum elf_reloc_type_class
7e612e98
AM
18021ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18022 const asection *rel_sec,
18023 const Elf_Internal_Rela *rela)
65f38f15 18024{
04c9666a 18025 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
18026 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18027
33e44f2e 18028 if (rel_sec == htab->elf.irelplt)
7e612e98 18029 return reloc_class_ifunc;
a33d1f77 18030
4ce794b7 18031 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 18032 switch (r_type)
65f38f15
AM
18033 {
18034 case R_PPC64_RELATIVE:
18035 return reloc_class_relative;
18036 case R_PPC64_JMP_SLOT:
18037 return reloc_class_plt;
18038 case R_PPC64_COPY:
18039 return reloc_class_copy;
18040 default:
18041 return reloc_class_normal;
18042 }
18043}
18044
5bd4f169
AM
18045/* Finish up the dynamic sections. */
18046
0a1b45a2 18047static bool
4ce794b7
AM
18048ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18049 struct bfd_link_info *info)
5bd4f169 18050{
65f38f15
AM
18051 struct ppc_link_hash_table *htab;
18052 bfd *dynobj;
5bd4f169 18053 asection *sdyn;
5bd4f169 18054
65f38f15 18055 htab = ppc_hash_table (info);
4dfe6ac6 18056 if (htab == NULL)
0a1b45a2 18057 return false;
4dfe6ac6 18058
65f38f15 18059 dynobj = htab->elf.dynobj;
3d4d4302 18060 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 18061
65f38f15 18062 if (htab->elf.dynamic_sections_created)
5bd4f169 18063 {
5bd4f169
AM
18064 Elf64_External_Dyn *dyncon, *dynconend;
18065
33e44f2e 18066 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 18067 abort ();
5bd4f169
AM
18068
18069 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 18070 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
18071 for (; dyncon < dynconend; dyncon++)
18072 {
18073 Elf_Internal_Dyn dyn;
19397422 18074 asection *s;
5bd4f169
AM
18075
18076 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18077
18078 switch (dyn.d_tag)
18079 {
65f38f15
AM
18080 default:
18081 continue;
5bd4f169 18082
5d1634d7 18083 case DT_PPC64_GLINK:
4ce794b7 18084 s = htab->glink;
6348e046 18085 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
18086 /* We stupidly defined DT_PPC64_GLINK to be the start
18087 of glink rather than the first entry point, which is
18088 what ld.so needs, and now have a bigger stub to
18089 support automatic multiple TOCs. */
9e390558 18090 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
18091 break;
18092
19397422
AM
18093 case DT_PPC64_OPD:
18094 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18095 if (s == NULL)
18096 continue;
18097 dyn.d_un.d_ptr = s->vma;
19397422
AM
18098 break;
18099
e8910a83 18100 case DT_PPC64_OPT:
5663e321
AM
18101 if ((htab->do_multi_toc && htab->multi_toc_needed)
18102 || htab->notoc_plt)
e8910a83 18103 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
18104 if (htab->has_plt_localentry0)
18105 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
18106 break;
18107
19397422
AM
18108 case DT_PPC64_OPDSZ:
18109 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18110 if (s == NULL)
18111 continue;
eea6121a 18112 dyn.d_un.d_val = s->size;
19397422
AM
18113 break;
18114
65f38f15 18115 case DT_PLTGOT:
33e44f2e 18116 s = htab->elf.splt;
6348e046 18117 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
18118 break;
18119
18120 case DT_JMPREL:
33e44f2e 18121 s = htab->elf.srelplt;
6348e046 18122 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 18123 break;
5bd4f169 18124
65f38f15 18125 case DT_PLTRELSZ:
33e44f2e 18126 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 18127 break;
82e66161
AM
18128
18129 case DT_TEXTREL:
cebd6b8a 18130 if (htab->elf.ifunc_resolvers)
82e66161
AM
18131 info->callbacks->einfo
18132 (_("%P: warning: text relocations and GNU indirect "
18133 "functions may result in a segfault at runtime\n"));
18134 continue;
5bd4f169 18135 }
5bd4f169 18136
65f38f15 18137 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 18138 }
5bd4f169
AM
18139 }
18140
6528b6eb
AM
18141 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18142 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18143 {
18144 /* Fill in the first entry in the global offset table.
18145 We use it to hold the link-time TOCbase. */
18146 bfd_put_64 (output_bfd,
60ee0d4a 18147 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 18148 htab->elf.sgot->contents);
5d1634d7
AM
18149
18150 /* Set .got entry size. */
2cdcc330
AM
18151 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18152 = 8;
5d1634d7
AM
18153 }
18154
6528b6eb
AM
18155 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18156 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18157 {
18158 /* Set .plt entry size. */
33e44f2e 18159 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 18160 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
18161 }
18162
84f5d08e
AM
18163 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18164 brlt ourselves if emitrelocations. */
18165 if (htab->brlt != NULL
18166 && htab->brlt->reloc_count != 0
18167 && !_bfd_elf_link_output_relocs (output_bfd,
18168 htab->brlt,
d4730f92 18169 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
18170 elf_section_data (htab->brlt)->relocs,
18171 NULL))
0a1b45a2 18172 return false;
84f5d08e 18173
176a0d42
AM
18174 if (htab->glink != NULL
18175 && htab->glink->reloc_count != 0
18176 && !_bfd_elf_link_output_relocs (output_bfd,
18177 htab->glink,
d4730f92 18178 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
18179 elf_section_data (htab->glink)->relocs,
18180 NULL))
0a1b45a2 18181 return false;
176a0d42 18182
da44f4e5 18183
df136d64
AM
18184 if (htab->glink_eh_frame != NULL
18185 && htab->glink_eh_frame->size != 0
18186 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18187 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18188 htab->glink_eh_frame,
18189 htab->glink_eh_frame->contents))
0a1b45a2 18190 return false;
58d180e8 18191
e717da7e 18192 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
18193 since we didn't add them to DYNOBJ. We know dynobj is the first
18194 bfd. */
c72f2fb2 18195 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
18196 {
18197 asection *s;
7b53ace3 18198
0c8d6e5c 18199 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
18200 continue;
18201
e717da7e
AM
18202 s = ppc64_elf_tdata (dynobj)->got;
18203 if (s != NULL
eea6121a 18204 && s->size != 0
e717da7e
AM
18205 && s->output_section != bfd_abs_section_ptr
18206 && !bfd_set_section_contents (output_bfd, s->output_section,
18207 s->contents, s->output_offset,
eea6121a 18208 s->size))
0a1b45a2 18209 return false;
e717da7e
AM
18210 s = ppc64_elf_tdata (dynobj)->relgot;
18211 if (s != NULL
eea6121a 18212 && s->size != 0
e717da7e
AM
18213 && s->output_section != bfd_abs_section_ptr
18214 && !bfd_set_section_contents (output_bfd, s->output_section,
18215 s->contents, s->output_offset,
eea6121a 18216 s->size))
0a1b45a2 18217 return false;
e717da7e 18218 }
f6c52c13 18219
0a1b45a2 18220 return true;
5bd4f169
AM
18221}
18222
5bd4f169 18223#include "elf64-target.h"
7b8e7dad
AM
18224
18225/* FreeBSD support */
18226
18227#undef TARGET_LITTLE_SYM
6afcdeb3 18228#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 18229#undef TARGET_LITTLE_NAME
6afcdeb3 18230#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
18231
18232#undef TARGET_BIG_SYM
6d00b590 18233#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
18234#undef TARGET_BIG_NAME
18235#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18236
18237#undef ELF_OSABI
18238#define ELF_OSABI ELFOSABI_FREEBSD
18239
18240#undef elf64_bed
18241#define elf64_bed elf64_powerpc_fbsd_bed
18242
18243#include "elf64-target.h"