]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
asprintf memory leaks
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
d87bef3a 2 Copyright (C) 1999-2023 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
c09c8b42
AM
28/* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31#define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
4bd7c902 32
3db64b00 33#include "sysdep.h"
183e98be 34#include <stdarg.h>
5bd4f169 35#include "bfd.h"
5bd4f169
AM
36#include "bfdlink.h"
37#include "libbfd.h"
38#include "elf-bfd.h"
04c9666a 39#include "elf/ppc64.h"
5d1634d7 40#include "elf64-ppc.h"
58d180e8 41#include "dwarf2.h"
5bd4f169 42
bb294208
AM
43/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44#define OCTETS_PER_BYTE(ABFD, SEC) 1
45
805fc799 46static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
48static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 58static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 60static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
62static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 64static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 66static bfd_vma opd_entry_value
0a1b45a2 67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
5bd4f169 68
6d00b590 69#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 70#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 71#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
72#define TARGET_BIG_NAME "elf64-powerpc"
73#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 74#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
75#define ELF_MACHINE_CODE EM_PPC64
76#define ELF_MAXPAGESIZE 0x10000
702d1671 77#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
78#define elf_info_to_howto ppc64_elf_info_to_howto
79
80#define elf_backend_want_got_sym 0
81#define elf_backend_want_plt_sym 0
82#define elf_backend_plt_alignment 3
83#define elf_backend_plt_not_loaded 1
ad8e1ba5 84#define elf_backend_got_header_size 8
5474d94f 85#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
86#define elf_backend_can_gc_sections 1
87#define elf_backend_can_refcount 1
88#define elf_backend_rela_normal 1
64f52338 89#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 90#define elf_backend_default_execstack 0
ad8e1ba5 91
e717da7e 92#define bfd_elf64_mkobject ppc64_elf_mkobject
6313825c 93#define bfd_elf64_bfd_free_cached_info ppc64_elf_free_cached_info
ad8e1ba5 94#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 95#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
96#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
97#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
98#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
99#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 100#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 101#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 102#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
103
104#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
105#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
106#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 107#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 108#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 109#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 110#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 111#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 112#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 113#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 114#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 115#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 116#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 117#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 118#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
119#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
120#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 121#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
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}
6313825c
AM
300
301#define is_ppc64_elf(bfd) \
302 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
303 && elf_object_id (bfd) == PPC64_ELF_DATA)
5bd4f169
AM
304\f
305/* Relocation HOWTO's. */
46807bf4
AM
306/* Like other ELF RELA targets that don't apply multiple
307 field-altering relocations to the same localation, src_mask is
308 always zero and pcrel_offset is the same as pc_relative.
309 PowerPC can always use a zero bitpos, even when the field is not at
310 the LSB. For example, a REL24 could use rightshift=2, bisize=24
311 and bitpos=2 which matches the ABI description, or as we do here,
312 rightshift=0, bitsize=26 and bitpos=0. */
313#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
314 complain, special_func) \
315 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
316 complain_overflow_ ## complain, special_func, \
0a1b45a2 317 #type, false, 0, mask, pc_relative)
46807bf4 318
04c9666a 319static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 320
f3185997
NC
321static reloc_howto_type ppc64_elf_howto_raw[] =
322{
5bd4f169 323 /* This reloc does nothing. */
c94cb026 324 HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
46807bf4 325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* A standard 32 bit relocation. */
c94cb026 328 HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
46807bf4 329 bfd_elf_generic_reloc),
5bd4f169
AM
330
331 /* An absolute 26 bit branch; the lower two bits must be zero.
332 FIXME: we don't check that, we just clear them. */
c94cb026 333 HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
46807bf4 334 bfd_elf_generic_reloc),
5bd4f169
AM
335
336 /* A standard 16 bit relocation. */
c94cb026 337 HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
46807bf4 338 bfd_elf_generic_reloc),
5bd4f169
AM
339
340 /* A 16 bit relocation without overflow. */
c94cb026 341 HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 342 bfd_elf_generic_reloc),
5bd4f169
AM
343
344 /* Bits 16-31 of an address. */
c94cb026 345 HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 346 bfd_elf_generic_reloc),
5bd4f169
AM
347
348 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
349 bits, treated as a signed number, is negative. */
c94cb026 350 HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 351 ppc64_elf_ha_reloc),
5bd4f169
AM
352
353 /* An absolute 16 bit branch; the lower two bits must be zero.
354 FIXME: we don't check that, we just clear them. */
c94cb026 355 HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
46807bf4 356 ppc64_elf_branch_reloc),
5bd4f169
AM
357
358 /* An absolute 16 bit branch, for which bit 10 should be set to
359 indicate that the branch is expected to be taken. The lower two
360 bits must be zero. */
c94cb026 361 HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
46807bf4 362 ppc64_elf_brtaken_reloc),
5bd4f169
AM
363
364 /* An absolute 16 bit branch, for which bit 10 should be set to
365 indicate that the branch is not expected to be taken. The lower
366 two bits must be zero. */
c94cb026 367 HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
46807bf4 368 ppc64_elf_brtaken_reloc),
5bd4f169
AM
369
370 /* A relative 26 bit branch; the lower two bits must be zero. */
c94cb026 371 HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
46807bf4 372 ppc64_elf_branch_reloc),
5bd4f169 373
05d0e962 374 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
c94cb026 375 HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
46807bf4 376 ppc64_elf_branch_reloc),
05d0e962 377
7aba54da 378 /* Another variant, when p10 insns can't be used on stubs. */
c94cb026 379 HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
7aba54da
AM
380 ppc64_elf_branch_reloc),
381
5bd4f169 382 /* A relative 16 bit branch; the lower two bits must be zero. */
c94cb026 383 HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
46807bf4 384 ppc64_elf_branch_reloc),
5bd4f169
AM
385
386 /* A relative 16 bit branch. Bit 10 should be set to indicate that
387 the branch is expected to be taken. The lower two bits must be
388 zero. */
c94cb026 389 HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
46807bf4 390 ppc64_elf_brtaken_reloc),
5bd4f169
AM
391
392 /* A relative 16 bit branch. Bit 10 should be set to indicate that
393 the branch is not expected to be taken. The lower two bits must
394 be zero. */
c94cb026 395 HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
46807bf4 396 ppc64_elf_brtaken_reloc),
5bd4f169
AM
397
398 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
399 symbol. */
c94cb026 400 HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
46807bf4 401 ppc64_elf_unhandled_reloc),
5bd4f169
AM
402
403 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
404 the symbol. */
c94cb026 405 HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 406 ppc64_elf_unhandled_reloc),
5bd4f169
AM
407
408 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
409 the symbol. */
c94cb026 410 HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 411 ppc64_elf_unhandled_reloc),
5bd4f169
AM
412
413 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
414 the symbol. */
c94cb026 415 HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 416 ppc64_elf_unhandled_reloc),
5bd4f169
AM
417
418 /* This is used only by the dynamic linker. The symbol should exist
419 both in the object being run and in some shared library. The
420 dynamic linker copies the data addressed by the symbol from the
421 shared library into the object, because the object being
422 run has to have the data at some particular address. */
5d0feb98 423 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
46807bf4 424 ppc64_elf_unhandled_reloc),
5bd4f169
AM
425
426 /* Like R_PPC64_ADDR64, but used when setting global offset table
427 entries. */
c94cb026 428 HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 429 ppc64_elf_unhandled_reloc),
5bd4f169
AM
430
431 /* Created by the link editor. Marks a procedure linkage table
432 entry for a symbol. */
5d0feb98 433 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
46807bf4 434 ppc64_elf_unhandled_reloc),
5bd4f169
AM
435
436 /* Used only by the dynamic linker. When the object is run, this
437 doubleword64 is set to the load address of the object, plus the
438 addend. */
c94cb026 439 HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 440 bfd_elf_generic_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR32, but may be unaligned. */
c94cb026 443 HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
46807bf4 444 bfd_elf_generic_reloc),
5bd4f169
AM
445
446 /* Like R_PPC64_ADDR16, but may be unaligned. */
c94cb026 447 HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
46807bf4 448 bfd_elf_generic_reloc),
5bd4f169
AM
449
450 /* 32-bit PC relative. */
c94cb026 451 HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
46807bf4 452 bfd_elf_generic_reloc),
5bd4f169 453
10ed1bba 454 /* 32-bit relocation to the symbol's procedure linkage table. */
c94cb026 455 HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
46807bf4 456 ppc64_elf_unhandled_reloc),
5bd4f169
AM
457
458 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
459 FIXME: R_PPC64_PLTREL32 not supported. */
c94cb026 460 HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
46807bf4 461 ppc64_elf_unhandled_reloc),
5bd4f169
AM
462
463 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
464 the symbol. */
c94cb026 465 HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 466 ppc64_elf_unhandled_reloc),
5bd4f169
AM
467
468 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
469 the symbol. */
c94cb026 470 HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 471 ppc64_elf_unhandled_reloc),
5bd4f169
AM
472
473 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
474 the symbol. */
c94cb026 475 HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 476 ppc64_elf_unhandled_reloc),
5bd4f169 477
c061c2d8 478 /* 16-bit section relative relocation. */
c94cb026 479 HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
46807bf4 480 ppc64_elf_sectoff_reloc),
5bd4f169 481
c061c2d8 482 /* Like R_PPC64_SECTOFF, but no overflow warning. */
c94cb026 483 HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 484 ppc64_elf_sectoff_reloc),
5bd4f169
AM
485
486 /* 16-bit upper half section relative relocation. */
c94cb026 487 HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 488 ppc64_elf_sectoff_reloc),
5bd4f169
AM
489
490 /* 16-bit upper half adjusted section relative relocation. */
c94cb026 491 HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 492 ppc64_elf_sectoff_ha_reloc),
5bd4f169 493
04c9666a 494 /* Like R_PPC64_REL24 without touching the two least significant bits. */
c94cb026 495 HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
46807bf4 496 bfd_elf_generic_reloc),
5bd4f169
AM
497
498 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
499
500 /* A standard 64-bit relocation. */
c94cb026 501 HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 502 bfd_elf_generic_reloc),
5bd4f169
AM
503
504 /* The bits 32-47 of an address. */
c94cb026 505 HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
46807bf4 506 bfd_elf_generic_reloc),
5bd4f169
AM
507
508 /* The bits 32-47 of an address, plus 1 if the contents of the low
509 16 bits, treated as a signed number, is negative. */
c94cb026 510 HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
46807bf4 511 ppc64_elf_ha_reloc),
5bd4f169
AM
512
513 /* The bits 48-63 of an address. */
c94cb026 514 HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
46807bf4 515 bfd_elf_generic_reloc),
5bd4f169
AM
516
517 /* The bits 48-63 of an address, plus 1 if the contents of the low
518 16 bits, treated as a signed number, is negative. */
c94cb026 519 HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
46807bf4 520 ppc64_elf_ha_reloc),
5bd4f169
AM
521
522 /* Like ADDR64, but may be unaligned. */
c94cb026 523 HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 524 bfd_elf_generic_reloc),
5bd4f169
AM
525
526 /* 64-bit relative relocation. */
c94cb026 527 HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 528 bfd_elf_generic_reloc),
5bd4f169 529
cedb70c5 530 /* 64-bit relocation to the symbol's procedure linkage table. */
c94cb026 531 HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 532 ppc64_elf_unhandled_reloc),
5bd4f169
AM
533
534 /* 64-bit PC relative relocation to the symbol's procedure linkage
535 table. */
536 /* FIXME: R_PPC64_PLTREL64 not supported. */
c94cb026 537 HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 538 ppc64_elf_unhandled_reloc),
5bd4f169
AM
539
540 /* 16 bit TOC-relative relocation. */
5bd4f169 541 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
c94cb026 542 HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
46807bf4 543 ppc64_elf_toc_reloc),
5bd4f169
AM
544
545 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 546 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
c94cb026 547 HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 548 ppc64_elf_toc_reloc),
5bd4f169
AM
549
550 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 551 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
c94cb026 552 HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 553 ppc64_elf_toc_reloc),
5bd4f169
AM
554
555 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
556 contents of the low 16 bits, treated as a signed number, is
557 negative. */
5bd4f169 558 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
c94cb026 559 HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 560 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
561
562 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 563 /* R_PPC64_TOC 51 doubleword64 .TOC. */
c94cb026 564 HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 565 ppc64_elf_toc64_reloc),
5bd4f169
AM
566
567 /* Like R_PPC64_GOT16, but also informs the link editor that the
568 value to relocate may (!) refer to a PLT entry which the link
569 editor (a) may replace with the symbol value. If the link editor
570 is unable to fully resolve the symbol, it may (b) create a PLT
571 entry and store the address to the new PLT entry in the GOT.
572 This permits lazy resolution of function symbols at run time.
573 The link editor may also skip all of this and just (c) emit a
574 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
575 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
c94cb026 576 HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
46807bf4 577 ppc64_elf_unhandled_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_PLTGOT16, but without overflow. */
580 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
c94cb026 581 HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 582 ppc64_elf_unhandled_reloc),
411e1bfb
AM
583
584 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
585 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
c94cb026 586 HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 587 ppc64_elf_unhandled_reloc),
411e1bfb
AM
588
589 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
590 1 if the contents of the low 16 bits, treated as a signed number,
591 is negative. */
592 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
c94cb026 593 HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 594 ppc64_elf_unhandled_reloc),
411e1bfb
AM
595
596 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
c94cb026 597 HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 598 bfd_elf_generic_reloc),
411e1bfb
AM
599
600 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
c94cb026 601 HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 602 bfd_elf_generic_reloc),
411e1bfb
AM
603
604 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
c94cb026 605 HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 606 ppc64_elf_unhandled_reloc),
411e1bfb
AM
607
608 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
c94cb026 609 HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 610 ppc64_elf_unhandled_reloc),
411e1bfb
AM
611
612 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
c94cb026 613 HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 614 ppc64_elf_unhandled_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
c94cb026 617 HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 618 ppc64_elf_sectoff_reloc),
411e1bfb
AM
619
620 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
c94cb026 621 HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 622 ppc64_elf_sectoff_reloc),
411e1bfb
AM
623
624 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
c94cb026 625 HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 626 ppc64_elf_toc_reloc),
411e1bfb
AM
627
628 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
c94cb026 629 HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 630 ppc64_elf_toc_reloc),
411e1bfb
AM
631
632 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
633 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
c94cb026 634 HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 635 ppc64_elf_unhandled_reloc),
411e1bfb
AM
636
637 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
638 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
c94cb026 639 HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 640 ppc64_elf_unhandled_reloc),
411e1bfb 641
727fc41e 642 /* Marker relocs for TLS. */
c94cb026 643 HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
46807bf4
AM
644 bfd_elf_generic_reloc),
645
c94cb026 646 HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
46807bf4
AM
647 bfd_elf_generic_reloc),
648
c94cb026 649 HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
46807bf4 650 bfd_elf_generic_reloc),
727fc41e 651
23cedd1d
AM
652 /* Marker reloc for optimizing r2 save in prologue rather than on
653 each plt call stub. */
c94cb026 654 HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
46807bf4 655 bfd_elf_generic_reloc),
3b421ab3 656
23cedd1d 657 /* Marker relocs on inline plt call instructions. */
c94cb026 658 HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
46807bf4
AM
659 bfd_elf_generic_reloc),
660
c94cb026 661 HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
46807bf4 662 bfd_elf_generic_reloc),
23cedd1d 663
411e1bfb
AM
664 /* Computes the load module index of the load module that contains the
665 definition of its TLS sym. */
c94cb026 666 HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 667 ppc64_elf_unhandled_reloc),
411e1bfb
AM
668
669 /* Computes a dtv-relative displacement, the difference between the value
670 of sym+add and the base address of the thread-local storage block that
671 contains the definition of sym, minus 0x8000. */
c94cb026 672 HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 673 ppc64_elf_unhandled_reloc),
411e1bfb
AM
674
675 /* A 16 bit dtprel reloc. */
c94cb026 676 HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
46807bf4 677 ppc64_elf_unhandled_reloc),
411e1bfb
AM
678
679 /* Like DTPREL16, but no overflow. */
c94cb026 680 HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 681 ppc64_elf_unhandled_reloc),
411e1bfb
AM
682
683 /* Like DTPREL16_LO, but next higher group of 16 bits. */
c94cb026 684 HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 685 ppc64_elf_unhandled_reloc),
411e1bfb
AM
686
687 /* Like DTPREL16_HI, but adjust for low 16 bits. */
c94cb026 688 HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 689 ppc64_elf_unhandled_reloc),
411e1bfb
AM
690
691 /* Like DTPREL16_HI, but next higher group of 16 bits. */
c94cb026 692 HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
46807bf4 693 ppc64_elf_unhandled_reloc),
411e1bfb
AM
694
695 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
c94cb026 696 HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
46807bf4 697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
c94cb026 700 HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
46807bf4 701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
c94cb026 704 HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
46807bf4 705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like DTPREL16, but for insns with a DS field. */
c94cb026 708 HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Like DTPREL16_DS, but no overflow. */
c94cb026 712 HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 713 ppc64_elf_unhandled_reloc),
411e1bfb
AM
714
715 /* Computes a tp-relative displacement, the difference between the value of
716 sym+add and the value of the thread pointer (r13). */
c94cb026 717 HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 718 ppc64_elf_unhandled_reloc),
411e1bfb
AM
719
720 /* A 16 bit tprel reloc. */
c94cb026 721 HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
46807bf4 722 ppc64_elf_unhandled_reloc),
411e1bfb
AM
723
724 /* Like TPREL16, but no overflow. */
c94cb026 725 HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 726 ppc64_elf_unhandled_reloc),
411e1bfb
AM
727
728 /* Like TPREL16_LO, but next higher group of 16 bits. */
c94cb026 729 HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 730 ppc64_elf_unhandled_reloc),
411e1bfb
AM
731
732 /* Like TPREL16_HI, but adjust for low 16 bits. */
c94cb026 733 HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 734 ppc64_elf_unhandled_reloc),
411e1bfb
AM
735
736 /* Like TPREL16_HI, but next higher group of 16 bits. */
c94cb026 737 HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
46807bf4 738 ppc64_elf_unhandled_reloc),
411e1bfb
AM
739
740 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
c94cb026 741 HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
46807bf4 742 ppc64_elf_unhandled_reloc),
411e1bfb
AM
743
744 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
c94cb026 745 HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
46807bf4 746 ppc64_elf_unhandled_reloc),
411e1bfb
AM
747
748 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
c94cb026 749 HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
46807bf4 750 ppc64_elf_unhandled_reloc),
411e1bfb
AM
751
752 /* Like TPREL16, but for insns with a DS field. */
c94cb026 753 HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 754 ppc64_elf_unhandled_reloc),
411e1bfb
AM
755
756 /* Like TPREL16_DS, but no overflow. */
c94cb026 757 HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 758 ppc64_elf_unhandled_reloc),
411e1bfb
AM
759
760 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
761 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
762 to the first entry relative to the TOC base (r2). */
c94cb026 763 HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
46807bf4 764 ppc64_elf_unhandled_reloc),
5bd4f169 765
411e1bfb 766 /* Like GOT_TLSGD16, but no overflow. */
c94cb026 767 HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 768 ppc64_elf_unhandled_reloc),
5bd4f169 769
411e1bfb 770 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
c94cb026 771 HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 772 ppc64_elf_unhandled_reloc),
5bd4f169 773
411e1bfb 774 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
c94cb026 775 HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 776 ppc64_elf_unhandled_reloc),
5bd4f169 777
411e1bfb
AM
778 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
779 with values (sym+add)@dtpmod and zero, and computes the offset to the
780 first entry relative to the TOC base (r2). */
c94cb026 781 HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
46807bf4 782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_TLSLD16, but no overflow. */
c94cb026 785 HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
46807bf4 786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
c94cb026 789 HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 790 ppc64_elf_unhandled_reloc),
5bd4f169 791
411e1bfb 792 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
c94cb026 793 HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 794 ppc64_elf_unhandled_reloc),
5bd4f169 795
411e1bfb
AM
796 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
797 the offset to the entry relative to the TOC base (r2). */
c94cb026 798 HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_DTPREL16_DS, but no overflow. */
c94cb026 802 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
c94cb026 806 HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 807 ppc64_elf_unhandled_reloc),
5bd4f169 808
411e1bfb 809 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
c94cb026 810 HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4 811 ppc64_elf_unhandled_reloc),
411e1bfb
AM
812
813 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
814 offset to the entry relative to the TOC base (r2). */
c94cb026 815 HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
46807bf4 816 ppc64_elf_unhandled_reloc),
5bd4f169 817
411e1bfb 818 /* Like GOT_TPREL16_DS, but no overflow. */
c94cb026 819 HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
46807bf4 820 ppc64_elf_unhandled_reloc),
5bd4f169 821
411e1bfb 822 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
c94cb026 823 HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
46807bf4 824 ppc64_elf_unhandled_reloc),
5bd4f169 825
411e1bfb 826 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
c94cb026 827 HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
46807bf4
AM
828 ppc64_elf_unhandled_reloc),
829
5d0feb98 830 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
46807bf4
AM
831 ppc64_elf_unhandled_reloc),
832
c94cb026 833 HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 834 bfd_elf_generic_reloc),
e054468f 835
25f23106 836 /* A 16 bit relative relocation. */
c94cb026 837 HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
46807bf4 838 bfd_elf_generic_reloc),
25f23106
AM
839
840 /* A 16 bit relative relocation without overflow. */
c94cb026 841 HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
46807bf4 842 bfd_elf_generic_reloc),
25f23106
AM
843
844 /* The high order 16 bits of a relative address. */
c94cb026 845 HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
46807bf4 846 bfd_elf_generic_reloc),
25f23106
AM
847
848 /* The high order 16 bits of a relative address, plus 1 if the contents of
849 the low 16 bits, treated as a signed number, is negative. */
c94cb026 850 HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
46807bf4 851 ppc64_elf_ha_reloc),
25f23106 852
c94cb026 853 HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
4a969973
AM
854 bfd_elf_generic_reloc),
855
c94cb026 856 HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
4a969973
AM
857 ppc64_elf_ha_reloc),
858
c94cb026 859 HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
4a969973
AM
860 bfd_elf_generic_reloc),
861
c94cb026 862 HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
4a969973
AM
863 ppc64_elf_ha_reloc),
864
c94cb026 865 HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
4a969973
AM
866 bfd_elf_generic_reloc),
867
c94cb026 868 HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
4a969973
AM
869 ppc64_elf_ha_reloc),
870
a680de9a 871 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
c94cb026 872 HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
46807bf4 873 ppc64_elf_ha_reloc),
a680de9a 874
7ba71655 875 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
c94cb026 876 HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
46807bf4 877 ppc64_elf_ha_reloc),
7ba71655 878
f9c6b907 879 /* Like R_PPC64_ADDR16_HI, but no overflow. */
c94cb026 880 HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
46807bf4 881 bfd_elf_generic_reloc),
f9c6b907
AM
882
883 /* Like R_PPC64_ADDR16_HA, but no overflow. */
c94cb026 884 HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
46807bf4 885 ppc64_elf_ha_reloc),
f9c6b907
AM
886
887 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
c94cb026 888 HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
46807bf4 889 ppc64_elf_unhandled_reloc),
f9c6b907
AM
890
891 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
c94cb026 892 HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
46807bf4 893 ppc64_elf_unhandled_reloc),
f9c6b907
AM
894
895 /* Like R_PPC64_TPREL16_HI, but no overflow. */
c94cb026 896 HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
46807bf4 897 ppc64_elf_unhandled_reloc),
f9c6b907
AM
898
899 /* Like R_PPC64_TPREL16_HA, but no overflow. */
c94cb026 900 HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
46807bf4 901 ppc64_elf_unhandled_reloc),
f9c6b907 902
006589cf 903 /* Marker reloc on ELFv2 large-model function entry. */
c94cb026 904 HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
46807bf4 905 bfd_elf_generic_reloc),
006589cf 906
45965137 907 /* Like ADDR64, but use local entry point of function. */
c94cb026 908 HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 909 bfd_elf_generic_reloc),
45965137 910
c94cb026 911 HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
5663e321
AM
912 bfd_elf_generic_reloc),
913
c94cb026 914 HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
5663e321
AM
915 bfd_elf_generic_reloc),
916
c94cb026 917 HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
5663e321
AM
918 bfd_elf_generic_reloc),
919
c94cb026 920 HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
5663e321
AM
921 ppc64_elf_prefix_reloc),
922
c94cb026 923 HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
5663e321
AM
924 ppc64_elf_prefix_reloc),
925
c94cb026 926 HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
927 ppc64_elf_prefix_reloc),
928
c94cb026 929 HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
930 ppc64_elf_prefix_reloc),
931
c94cb026 932 HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
933 ppc64_elf_prefix_reloc),
934
c94cb026 935 HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
936 ppc64_elf_unhandled_reloc),
937
c94cb026 938 HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
939 ppc64_elf_unhandled_reloc),
940
c94cb026 941 HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
942 ppc64_elf_unhandled_reloc),
943
c94cb026 944 HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
945 ppc64_elf_unhandled_reloc),
946
c94cb026 947 HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
948 ppc64_elf_unhandled_reloc),
949
c94cb026 950 HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
951 ppc64_elf_unhandled_reloc),
952
c94cb026 953 HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
954 ppc64_elf_unhandled_reloc),
955
c94cb026 956 HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
957 ppc64_elf_unhandled_reloc),
958
c94cb026 959 HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
960 ppc64_elf_unhandled_reloc),
961
c94cb026 962 HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
5663e321
AM
963 bfd_elf_generic_reloc),
964
c94cb026 965 HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
5663e321
AM
966 ppc64_elf_ha_reloc),
967
c94cb026 968 HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
5663e321
AM
969 bfd_elf_generic_reloc),
970
c94cb026 971 HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
5663e321
AM
972 ppc64_elf_ha_reloc),
973
c94cb026 974 HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
5663e321
AM
975 bfd_elf_generic_reloc),
976
c94cb026 977 HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
5663e321
AM
978 ppc64_elf_ha_reloc),
979
c94cb026 980 HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
5663e321
AM
981 bfd_elf_generic_reloc),
982
c94cb026 983 HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
5663e321
AM
984 ppc64_elf_ha_reloc),
985
c94cb026 986 HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
5663e321
AM
987 ppc64_elf_prefix_reloc),
988
c94cb026 989 HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
5663e321
AM
990 ppc64_elf_prefix_reloc),
991
5bd4f169 992 /* GNU extension to record C++ vtable hierarchy. */
5d0feb98 993 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
46807bf4 994 NULL),
5bd4f169
AM
995
996 /* GNU extension to record C++ vtable member usage. */
5d0feb98 997 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
46807bf4 998 NULL),
5bd4f169
AM
999};
1000
1001\f
1002/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1003 be done. */
1004
1005static void
4ce794b7 1006ppc_howto_init (void)
5bd4f169
AM
1007{
1008 unsigned int i, type;
1009
a4b6fadd 1010 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
1011 {
1012 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 1013 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
1014 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1015 }
1016}
1017
1018static reloc_howto_type *
87c69f97 1019ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
5bd4f169 1020{
411e1bfb 1021 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1022
1023 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1024 /* Initialize howto table if needed. */
1025 ppc_howto_init ();
1026
4ce794b7 1027 switch (code)
5bd4f169
AM
1028 {
1029 default:
f3185997 1030 /* xgettext:c-format */
2cdcc330
AM
1031 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1032 (int) code);
f3185997 1033 bfd_set_error (bfd_error_bad_value);
4ce794b7 1034 return NULL;
5bd4f169 1035
411e1bfb
AM
1036 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1037 break;
1038 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1039 break;
1040 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1041 break;
1042 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1043 break;
1044 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1045 break;
1046 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1047 break;
f9c6b907
AM
1048 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1049 break;
411e1bfb 1050 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1051 break;
f9c6b907
AM
1052 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1053 break;
411e1bfb 1054 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1061 break;
05d0e962
AM
1062 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1063 break;
7aba54da
AM
1064 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1065 break;
411e1bfb 1066 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1139 break;
411e1bfb 1140 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1141 break;
411e1bfb 1142 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1143 break;
411e1bfb 1144 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1145 break;
411e1bfb 1146 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1147 break;
411e1bfb 1148 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1149 break;
411e1bfb 1150 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1151 break;
411e1bfb 1152 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1153 break;
411e1bfb 1154 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1155 break;
411e1bfb 1156 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1157 break;
411e1bfb 1158 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1159 break;
411e1bfb 1160 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1161 break;
c213164a 1162 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1163 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1164 break;
727fc41e
AM
1165 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1166 break;
1167 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1168 break;
411e1bfb 1169 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1170 break;
411e1bfb 1171 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1172 break;
411e1bfb 1173 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1174 break;
411e1bfb 1175 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1176 break;
f9c6b907
AM
1177 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1178 break;
411e1bfb 1179 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1180 break;
f9c6b907
AM
1181 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1182 break;
411e1bfb 1183 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1184 break;
411e1bfb
AM
1185 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1186 break;
1187 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1188 break;
1189 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1190 break;
f9c6b907
AM
1191 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1192 break;
411e1bfb
AM
1193 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1194 break;
f9c6b907
AM
1195 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1196 break;
411e1bfb
AM
1197 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1218 break;
1219 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1220 break;
1221 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1222 break;
1223 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1226 break;
1227 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1228 break;
1229 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1238 break;
1239 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1240 break;
1241 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1250 break;
1251 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1252 break;
1253 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1254 break;
25f23106
AM
1255 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1256 break;
1257 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1258 break;
1259 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1260 break;
1261 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1262 break;
4a969973
AM
1263 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1270 break;
1271 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1272 break;
1273 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1274 break;
7ba71655
AM
1275 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1276 break;
a680de9a
PB
1277 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1278 break;
006589cf
AM
1279 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1280 break;
45965137
AM
1281 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1282 break;
5663e321
AM
1283 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1284 break;
1285 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1286 break;
1287 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1288 break;
1289 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1290 break;
1291 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1292 break;
1293 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1294 break;
1295 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1296 break;
c213164a
AM
1297 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1298 break;
1299 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1300 break;
87c69f97 1301 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
c213164a 1302 break;
87c69f97 1303 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
c213164a 1304 break;
87c69f97 1305 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
c213164a 1306 break;
87c69f97 1307 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
c213164a 1308 break;
5663e321
AM
1309 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1312 break;
1313 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1314 break;
1315 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1316 break;
1317 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1320 break;
1321 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1322 break;
1323 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1324 break;
1325 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1326 break;
1327 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1328 break;
411e1bfb
AM
1329 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1330 break;
1331 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1332 break;
1333 }
1334
4ce794b7 1335 return ppc64_elf_howto_table[r];
5bd4f169
AM
1336};
1337
157090f7 1338static reloc_howto_type *
87c69f97 1339ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
157090f7
AM
1340{
1341 unsigned int i;
87c69f97
AM
1342 static char *compat_map[][2] = {
1343 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1344 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1345 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1346 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1347 };
157090f7 1348
a4b6fadd 1349 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1350 if (ppc64_elf_howto_raw[i].name != NULL
1351 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1352 return &ppc64_elf_howto_raw[i];
1353
87c69f97
AM
1354 /* Handle old names of relocations in case they were used by
1355 .reloc directives.
1356 FIXME: Remove this soon. Mapping the reloc names is very likely
1357 completely unnecessary. */
1358 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1359 if (strcasecmp (compat_map[i][0], r_name) == 0)
1360 {
1361 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1362 compat_map[i][1], compat_map[i][0]);
1363 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1364 }
1365
157090f7
AM
1366 return NULL;
1367}
1368
5bd4f169
AM
1369/* Set the howto pointer for a PowerPC ELF reloc. */
1370
0a1b45a2 1371static bool
4aef7643 1372ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1373 Elf_Internal_Rela *dst)
5bd4f169 1374{
65f38f15
AM
1375 unsigned int type;
1376
ef60b7ff 1377 /* Initialize howto table if needed. */
5bd4f169 1378 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1379 ppc_howto_init ();
1380
65f38f15 1381 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1382 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1383 {
695344c0 1384 /* xgettext:c-format */
0aa13fee 1385 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1386 abfd, type);
f3185997 1387 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1388 return false;
d0fb9a8d 1389 }
65f38f15 1390 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1391 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1392 {
1393 /* xgettext:c-format */
1394 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1395 abfd, type);
1396 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1397 return false;
f3185997 1398 }
2cdcc330 1399
0a1b45a2 1400 return true;
5bd4f169
AM
1401}
1402
04c9666a 1403/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1404
1405static bfd_reloc_status_type
4ce794b7
AM
1406ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1407 void *data, asection *input_section,
1408 bfd *output_bfd, char **error_message)
5bd4f169 1409{
a680de9a
PB
1410 enum elf_ppc64_reloc_type r_type;
1411 long insn;
1412 bfd_size_type octets;
3de43e7b 1413 bfd_vma value;
a680de9a 1414
805fc799
AM
1415 /* If this is a relocatable link (output_bfd test tells us), just
1416 call the generic function. Any adjustment will be done at final
1417 link time. */
1418 if (output_bfd != NULL)
cedb70c5 1419 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1420 input_section, output_bfd, error_message);
1421
5663e321
AM
1422 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1423 We won't actually be using the low bits, so trashing them
805fc799 1424 doesn't matter. */
a680de9a 1425 r_type = reloc_entry->howto->type;
5663e321
AM
1426 if (r_type == R_PPC64_ADDR16_HIGHERA34
1427 || r_type == R_PPC64_ADDR16_HIGHESTA34
1428 || r_type == R_PPC64_REL16_HIGHERA34
1429 || r_type == R_PPC64_REL16_HIGHESTA34)
1430 reloc_entry->addend += 1ULL << 33;
1431 else
1432 reloc_entry->addend += 1U << 15;
a680de9a
PB
1433 if (r_type != R_PPC64_REL16DX_HA)
1434 return bfd_reloc_continue;
1435
1436 value = 0;
1437 if (!bfd_is_com_section (symbol->section))
1438 value = symbol->value;
1439 value += (reloc_entry->addend
1440 + symbol->section->output_offset
1441 + symbol->section->output_section->vma);
1442 value -= (reloc_entry->address
1443 + input_section->output_offset
1444 + input_section->output_section->vma);
3de43e7b 1445 value = (bfd_signed_vma) value >> 16;
a680de9a 1446
bb294208 1447 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1448 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1449 input_section, octets))
1450 return bfd_reloc_outofrange;
1451
a680de9a
PB
1452 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1453 insn &= ~0x1fffc1;
3de43e7b 1454 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1455 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1456 if (value + 0x8000 > 0xffff)
a680de9a
PB
1457 return bfd_reloc_overflow;
1458 return bfd_reloc_ok;
805fc799 1459}
5bd4f169 1460
2441e016
AM
1461static bfd_reloc_status_type
1462ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1463 void *data, asection *input_section,
1464 bfd *output_bfd, char **error_message)
1465{
1466 if (output_bfd != NULL)
1467 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1468 input_section, output_bfd, error_message);
1469
6313825c
AM
1470 if (symbol->section->owner == NULL
1471 || !is_ppc64_elf (symbol->section->owner))
1472 return bfd_reloc_continue;
1473
699733f6
AM
1474 if (strcmp (symbol->section->name, ".opd") == 0
1475 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1476 {
1477 bfd_vma dest = opd_entry_value (symbol->section,
1478 symbol->value + reloc_entry->addend,
0a1b45a2 1479 NULL, NULL, false);
2441e016
AM
1480 if (dest != (bfd_vma) -1)
1481 reloc_entry->addend = dest - (symbol->value
1482 + symbol->section->output_section->vma
1483 + symbol->section->output_offset);
1484 }
810d4e75
AM
1485 else
1486 {
1487 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1488
1489 if (symbol->section->owner != abfd
1490 && abiversion (symbol->section->owner) >= 2)
1491 {
1492 unsigned int i;
1493
1494 for (i = 0; i < symbol->section->owner->symcount; ++i)
1495 {
1496 asymbol *symdef = symbol->section->owner->outsymbols[i];
1497
1498 if (strcmp (symdef->name, symbol->name) == 0)
1499 {
1500 elfsym = (elf_symbol_type *) symdef;
1501 break;
1502 }
1503 }
1504 }
1505 reloc_entry->addend
1506 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1507 }
2441e016
AM
1508 return bfd_reloc_continue;
1509}
1510
805fc799 1511static bfd_reloc_status_type
4ce794b7
AM
1512ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1513 void *data, asection *input_section,
1514 bfd *output_bfd, char **error_message)
805fc799
AM
1515{
1516 long insn;
04c9666a 1517 enum elf_ppc64_reloc_type r_type;
805fc799 1518 bfd_size_type octets;
794e51c0 1519 /* Assume 'at' branch hints. */
0a1b45a2 1520 bool is_isa_v2 = true;
805fc799
AM
1521
1522 /* If this is a relocatable link (output_bfd test tells us), just
1523 call the generic function. Any adjustment will be done at final
1524 link time. */
5bd4f169 1525 if (output_bfd != NULL)
cedb70c5 1526 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1527 input_section, output_bfd, error_message);
1528
bb294208 1529 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1530 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1531 input_section, octets))
1532 return bfd_reloc_outofrange;
1533
805fc799
AM
1534 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1535 insn &= ~(0x01 << 21);
4ce794b7 1536 r_type = reloc_entry->howto->type;
805fc799
AM
1537 if (r_type == R_PPC64_ADDR14_BRTAKEN
1538 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1539 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1540
794e51c0 1541 if (is_isa_v2)
5bd4f169 1542 {
805fc799
AM
1543 /* Set 'a' bit. This is 0b00010 in BO field for branch
1544 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1545 for branch on CTR insns (BO == 1a00t or 1a01t). */
1546 if ((insn & (0x14 << 21)) == (0x04 << 21))
1547 insn |= 0x02 << 21;
1548 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1549 insn |= 0x08 << 21;
1550 else
2441e016 1551 goto out;
5bd4f169 1552 }
805fc799
AM
1553 else
1554 {
1555 bfd_vma target = 0;
1556 bfd_vma from;
5bd4f169 1557
805fc799
AM
1558 if (!bfd_is_com_section (symbol->section))
1559 target = symbol->value;
1560 target += symbol->section->output_section->vma;
1561 target += symbol->section->output_offset;
1562 target += reloc_entry->addend;
5bd4f169 1563
805fc799
AM
1564 from = (reloc_entry->address
1565 + input_section->output_offset
1566 + input_section->output_section->vma);
5bd4f169 1567
805fc799
AM
1568 /* Invert 'y' bit if not the default. */
1569 if ((bfd_signed_vma) (target - from) < 0)
1570 insn ^= 0x01 << 21;
1571 }
4ce794b7 1572 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1573 out:
1574 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1575 input_section, output_bfd, error_message);
805fc799 1576}
5bd4f169 1577
805fc799 1578static bfd_reloc_status_type
4ce794b7
AM
1579ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1580 void *data, asection *input_section,
1581 bfd *output_bfd, char **error_message)
805fc799
AM
1582{
1583 /* If this is a relocatable link (output_bfd test tells us), just
1584 call the generic function. Any adjustment will be done at final
1585 link time. */
1586 if (output_bfd != NULL)
cedb70c5 1587 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1588 input_section, output_bfd, error_message);
5bd4f169 1589
805fc799
AM
1590 /* Subtract the symbol section base address. */
1591 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1592 return bfd_reloc_continue;
1593}
1594
805fc799 1595static bfd_reloc_status_type
4ce794b7
AM
1596ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1597 void *data, asection *input_section,
1598 bfd *output_bfd, char **error_message)
805fc799
AM
1599{
1600 /* If this is a relocatable link (output_bfd test tells us), just
1601 call the generic function. Any adjustment will be done at final
1602 link time. */
1603 if (output_bfd != NULL)
cedb70c5 1604 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1605 input_section, output_bfd, error_message);
1606
1607 /* Subtract the symbol section base address. */
1608 reloc_entry->addend -= symbol->section->output_section->vma;
1609
1610 /* Adjust the addend for sign extension of the low 16 bits. */
1611 reloc_entry->addend += 0x8000;
1612 return bfd_reloc_continue;
1613}
1614
1615static bfd_reloc_status_type
4ce794b7
AM
1616ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1617 void *data, asection *input_section,
1618 bfd *output_bfd, char **error_message)
805fc799
AM
1619{
1620 bfd_vma TOCstart;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
cedb70c5 1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1c865ab2 1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1632
1633 /* Subtract the TOC base address. */
1634 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1635 return bfd_reloc_continue;
1636}
1637
1638static bfd_reloc_status_type
4ce794b7
AM
1639ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1640 void *data, asection *input_section,
1641 bfd *output_bfd, char **error_message)
805fc799
AM
1642{
1643 bfd_vma TOCstart;
1644
1645 /* If this is a relocatable link (output_bfd test tells us), just
1646 call the generic function. Any adjustment will be done at final
1647 link time. */
1648 if (output_bfd != NULL)
cedb70c5 1649 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1650 input_section, output_bfd, error_message);
1651
1652 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1653 if (TOCstart == 0)
1c865ab2 1654 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1655
1656 /* Subtract the TOC base address. */
1657 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1658
1659 /* Adjust the addend for sign extension of the low 16 bits. */
1660 reloc_entry->addend += 0x8000;
1661 return bfd_reloc_continue;
1662}
1663
1664static bfd_reloc_status_type
4ce794b7
AM
1665ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1666 void *data, asection *input_section,
1667 bfd *output_bfd, char **error_message)
805fc799
AM
1668{
1669 bfd_vma TOCstart;
1670 bfd_size_type octets;
1671
1672 /* If this is a relocatable link (output_bfd test tells us), just
1673 call the generic function. Any adjustment will be done at final
1674 link time. */
1675 if (output_bfd != NULL)
cedb70c5 1676 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1677 input_section, output_bfd, error_message);
1678
b54509b8
AM
1679 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1680 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1681 input_section, octets))
1682 return bfd_reloc_outofrange;
1683
805fc799
AM
1684 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1685 if (TOCstart == 0)
1c865ab2 1686 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799 1687
805fc799
AM
1688 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1689 return bfd_reloc_ok;
1690}
1691
5663e321
AM
1692static bfd_reloc_status_type
1693ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1694 void *data, asection *input_section,
1695 bfd *output_bfd, char **error_message)
1696{
1697 uint64_t insn;
1698 bfd_vma targ;
b54509b8 1699 bfd_size_type octets;
5663e321
AM
1700
1701 if (output_bfd != NULL)
1702 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1703 input_section, output_bfd, error_message);
1704
b54509b8
AM
1705 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1706 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1707 input_section, octets))
1708 return bfd_reloc_outofrange;
1709
1710 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
5663e321 1711 insn <<= 32;
b54509b8 1712 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
5663e321
AM
1713
1714 targ = (symbol->section->output_section->vma
1715 + symbol->section->output_offset
1716 + reloc_entry->addend);
1717 if (!bfd_is_com_section (symbol->section))
1718 targ += symbol->value;
1719 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1720 targ += 1ULL << 33;
1721 if (reloc_entry->howto->pc_relative)
1722 {
1723 bfd_vma from = (reloc_entry->address
1724 + input_section->output_offset
1725 + input_section->output_section->vma);
1726 targ -=from;
1727 }
1728 targ >>= reloc_entry->howto->rightshift;
1729 insn &= ~reloc_entry->howto->dst_mask;
1730 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
b54509b8
AM
1731 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1732 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
5663e321
AM
1733 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1734 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1735 >= 1ULL << reloc_entry->howto->bitsize))
1736 return bfd_reloc_overflow;
1737 return bfd_reloc_ok;
1738}
1739
805fc799 1740static bfd_reloc_status_type
4ce794b7
AM
1741ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1742 void *data, asection *input_section,
1743 bfd *output_bfd, char **error_message)
805fc799
AM
1744{
1745 /* If this is a relocatable link (output_bfd test tells us), just
1746 call the generic function. Any adjustment will be done at final
1747 link time. */
1748 if (output_bfd != NULL)
cedb70c5 1749 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1750 input_section, output_bfd, error_message);
1751
1752 if (error_message != NULL)
6f860418
AM
1753 *error_message = bfd_asprintf (_("generic linker can't handle %s"),
1754 reloc_entry->howto->name);
805fc799
AM
1755 return bfd_reloc_dangerous;
1756}
1757
927be08e
AM
1758/* Track GOT entries needed for a given symbol. We might need more
1759 than one got entry per symbol. */
1760struct got_entry
1761{
1762 struct got_entry *next;
1763
1764 /* The symbol addend that we'll be placing in the GOT. */
1765 bfd_vma addend;
1766
1767 /* Unlike other ELF targets, we use separate GOT entries for the same
1768 symbol referenced from different input files. This is to support
1769 automatic multiple TOC/GOT sections, where the TOC base can vary
1770 from one input file to another. After partitioning into TOC groups
1771 we merge entries within the group.
1772
1773 Point to the BFD owning this GOT entry. */
1774 bfd *owner;
1775
1776 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1777 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1778 unsigned char tls_type;
927be08e
AM
1779
1780 /* Non-zero if got.ent points to real entry. */
f961d9dd 1781 unsigned char is_indirect;
927be08e
AM
1782
1783 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1784 union
2cdcc330
AM
1785 {
1786 bfd_signed_vma refcount;
1787 bfd_vma offset;
1788 struct got_entry *ent;
1789 } got;
927be08e
AM
1790};
1791
1792/* The same for PLT. */
1793struct plt_entry
1794{
1795 struct plt_entry *next;
1796
1797 bfd_vma addend;
1798
1799 union
2cdcc330
AM
1800 {
1801 bfd_signed_vma refcount;
1802 bfd_vma offset;
1803 } plt;
927be08e
AM
1804};
1805
e717da7e
AM
1806struct ppc64_elf_obj_tdata
1807{
1808 struct elf_obj_tdata elf;
1809
1810 /* Shortcuts to dynamic linker sections. */
1811 asection *got;
1812 asection *relgot;
1813
b3fac117
AM
1814 /* Used during garbage collection. We attach global symbols defined
1815 on removed .opd entries to this section so that the sym is removed. */
1816 asection *deleted_section;
81688140 1817
927be08e 1818 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1819 sections means we potentially need one of these for each input bfd. */
927be08e 1820 struct got_entry tlsld_got;
8860955f 1821
d77c8a4b
AM
1822 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1823 the reloc to be in the range -32768 to 32767. */
98528052
AM
1824 unsigned int has_small_toc_reloc : 1;
1825
560c8763
AM
1826 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1827 instruction not one we handle. */
1828 unsigned int unexpected_toc_insn : 1;
066f4018 1829
903b777d
AM
1830 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1831 this file. */
1832 unsigned int has_optrel : 1;
e717da7e
AM
1833};
1834
1835#define ppc64_elf_tdata(bfd) \
1836 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1837
1838#define ppc64_tlsld_got(bfd) \
1839 (&ppc64_elf_tdata (bfd)->tlsld_got)
1840
1841/* Override the generic function because we store some extras. */
1842
0a1b45a2 1843static bool
e717da7e
AM
1844ppc64_elf_mkobject (bfd *abfd)
1845{
0ffa91dd 1846 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1847 PPC64_ELF_DATA);
e717da7e
AM
1848}
1849
feee612b 1850/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1851 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1852
0a1b45a2 1853static bool
4ce794b7 1854ppc64_elf_object_p (bfd *abfd)
feee612b 1855{
14b57c7c 1856 if (!abfd->arch_info->the_default)
0a1b45a2 1857 return true;
14b57c7c
AM
1858
1859 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1860 {
1861 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1862
1863 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1864 {
1865 /* Relies on arch after 32 bit default being 64 bit default. */
1866 abfd->arch_info = abfd->arch_info->next;
1867 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1868 }
1869 }
14b57c7c 1870 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1871}
1872
d37c89e5
AM
1873/* Support for core dump NOTE sections. */
1874
0a1b45a2 1875static bool
d37c89e5
AM
1876ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1877{
eea6121a 1878 size_t offset, size;
d37c89e5
AM
1879
1880 if (note->descsz != 504)
0a1b45a2 1881 return false;
d37c89e5
AM
1882
1883 /* pr_cursig */
228e534f 1884 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1885
1886 /* pr_pid */
228e534f 1887 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1888
1889 /* pr_reg */
1890 offset = 112;
eea6121a 1891 size = 384;
d37c89e5
AM
1892
1893 /* Make a ".reg/999" section. */
1894 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1895 size, note->descpos + offset);
d37c89e5
AM
1896}
1897
0a1b45a2 1898static bool
d37c89e5
AM
1899ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1900{
1901 if (note->descsz != 136)
0a1b45a2 1902 return false;
d37c89e5 1903
228e534f 1904 elf_tdata (abfd)->core->pid
bc989cdc 1905 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1906 elf_tdata (abfd)->core->program
d37c89e5 1907 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1908 elf_tdata (abfd)->core->command
d37c89e5
AM
1909 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1910
0a1b45a2 1911 return true;
d37c89e5
AM
1912}
1913
183e98be
AM
1914static char *
1915ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1916 ...)
1917{
1918 switch (note_type)
1919 {
1920 default:
1921 return NULL;
1922
1923 case NT_PRPSINFO:
1924 {
9ef6d1e3 1925 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1926 va_list ap;
1927
1928 va_start (ap, note_type);
75cd47ed 1929 memset (data, 0, sizeof (data));
183e98be 1930 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1931#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1932 DIAGNOSTIC_PUSH;
be3e27bb 1933 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1934 -Wstringop-truncation:
1935 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1936 */
95da9854
L
1937 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1938#endif
183e98be 1939 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1940#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1941 DIAGNOSTIC_POP;
fe75810f 1942#endif
183e98be
AM
1943 va_end (ap);
1944 return elfcore_write_note (abfd, buf, bufsiz,
1945 "CORE", note_type, data, sizeof (data));
1946 }
1947
1948 case NT_PRSTATUS:
1949 {
1950 char data[504];
1951 va_list ap;
1952 long pid;
1953 int cursig;
1954 const void *greg;
1955
1956 va_start (ap, note_type);
1957 memset (data, 0, 112);
1958 pid = va_arg (ap, long);
1959 bfd_put_32 (abfd, pid, data + 32);
1960 cursig = va_arg (ap, int);
1961 bfd_put_16 (abfd, cursig, data + 12);
1962 greg = va_arg (ap, const void *);
1963 memcpy (data + 112, greg, 384);
1964 memset (data + 496, 0, 8);
1965 va_end (ap);
1966 return elfcore_write_note (abfd, buf, bufsiz,
1967 "CORE", note_type, data, sizeof (data));
1968 }
1969 }
1970}
1971
5d35169e
AM
1972/* Add extra PPC sections. */
1973
2cdcc330 1974static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1975{
07d6d2b8
AM
1976 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1977 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1978 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1979 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1980 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1981 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1982 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1983};
1984
7c8fe5c4
AM
1985enum _ppc64_sec_type {
1986 sec_normal = 0,
1987 sec_opd = 1,
46439c90
AM
1988 sec_toc = 2,
1989 sec_stub = 3
7c8fe5c4
AM
1990};
1991
f0abc2a1
AM
1992struct _ppc64_elf_section_data
1993{
1994 struct bfd_elf_section_data elf;
411e1bfb 1995
f0abc2a1
AM
1996 union
1997 {
51aecdc5
AM
1998 /* An array with one entry for each opd function descriptor,
1999 and some spares since opd entries may be either 16 or 24 bytes. */
2000#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
2001 struct _opd_sec_data
2002 {
2003 /* Points to the function code section for local opd entries. */
2004 asection **func_sec;
2005
2006 /* After editing .opd, adjust references to opd local syms. */
2007 long *adjust;
6313825c
AM
2008
2009 union
2010 {
2011 /* A copy of relocs before they are modified for --emit-relocs. */
2012 Elf_Internal_Rela *relocs;
2013
2014 /* Section contents. */
2015 bfd_byte *contents;
2016 } u;
74f0fb50 2017 } opd;
7c8fe5c4 2018
3a71aa26
AM
2019 /* An array for toc sections, indexed by offset/8. */
2020 struct _toc_sec_data
2021 {
2022 /* Specifies the relocation symbol index used at a given toc offset. */
2023 unsigned *symndx;
2024
2025 /* And the relocation addend. */
2026 bfd_vma *add;
2027 } toc;
46439c90
AM
2028
2029 /* Stub debugging. */
2030 struct ppc_stub_hash_entry *last_ent;
7c8fe5c4
AM
2031 } u;
2032
2033 enum _ppc64_sec_type sec_type:2;
411e1bfb 2034
7c8fe5c4
AM
2035 /* Flag set when small branches are detected. Used to
2036 select suitable defaults for the stub group size. */
2037 unsigned int has_14bit_branch:1;
3e04d765
AM
2038
2039 /* Flag set when PLTCALL relocs are detected. */
2040 unsigned int has_pltcall:1;
066f4018 2041
903b777d
AM
2042 /* Flag set when section has PLT/GOT/TOC relocations that can be
2043 optimised. */
2044 unsigned int has_optrel:1;
f0abc2a1
AM
2045};
2046
2047#define ppc64_elf_section_data(sec) \
411e1bfb 2048 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1 2049
0a1b45a2 2050static bool
4ce794b7 2051ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2052{
f592407e
AM
2053 if (!sec->used_by_bfd)
2054 {
2055 struct _ppc64_elf_section_data *sdata;
986f0783 2056 size_t amt = sizeof (*sdata);
f0abc2a1 2057
f592407e
AM
2058 sdata = bfd_zalloc (abfd, amt);
2059 if (sdata == NULL)
0a1b45a2 2060 return false;
f592407e
AM
2061 sec->used_by_bfd = sdata;
2062 }
f0abc2a1
AM
2063
2064 return _bfd_elf_new_section_hook (abfd, sec);
2065}
4025353c 2066
0a1b45a2 2067static bool
bf577467
AM
2068ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2069{
2070 const char *name = hdr->bfd_section->name;
2071
3f3328b8
ML
2072 if (startswith (name, ".sbss")
2073 || startswith (name, ".sdata"))
bf577467
AM
2074 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2075
0a1b45a2 2076 return true;
bf577467
AM
2077}
2078
74f0fb50 2079static struct _opd_sec_data *
4025353c
AM
2080get_opd_info (asection * sec)
2081{
2082 if (sec != NULL
2083 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2084 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2085 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2086 return NULL;
2087}
90e3cdf2
JJ
2088\f
2089/* Parameters for the qsort hook. */
0a1b45a2 2090static bool synthetic_relocatable;
8cb1c2c8 2091static const asection *synthetic_opd;
90e3cdf2 2092
699733f6 2093/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2094
2095static int
2096compare_symbols (const void *ap, const void *bp)
2097{
2cdcc330
AM
2098 const asymbol *a = *(const asymbol **) ap;
2099 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2100
699733f6
AM
2101 /* Section symbols first. */
2102 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2103 return -1;
699733f6 2104 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2105 return 1;
2106
699733f6 2107 /* then .opd symbols. */
cd285db5
AM
2108 if (synthetic_opd != NULL)
2109 {
2110 if (strcmp (a->section->name, ".opd") == 0
2111 && strcmp (b->section->name, ".opd") != 0)
2112 return -1;
2113 if (strcmp (a->section->name, ".opd") != 0
2114 && strcmp (b->section->name, ".opd") == 0)
2115 return 1;
2116 }
90e3cdf2 2117
699733f6 2118 /* then other code symbols. */
2cdcc330
AM
2119 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2120 == (SEC_CODE | SEC_ALLOC))
2121 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2122 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2123 return -1;
2124
2cdcc330
AM
2125 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2126 != (SEC_CODE | SEC_ALLOC))
2127 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2128 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2129 return 1;
2130
2131 if (synthetic_relocatable)
2132 {
2133 if (a->section->id < b->section->id)
2134 return -1;
2135
2136 if (a->section->id > b->section->id)
2137 return 1;
2138 }
2139
2140 if (a->value + a->section->vma < b->value + b->section->vma)
2141 return -1;
2142
2143 if (a->value + a->section->vma > b->value + b->section->vma)
2144 return 1;
2145
4d35a0aa
AM
2146 /* For syms with the same value, prefer strong dynamic global function
2147 syms over other syms. */
2148 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2149 return -1;
2150
2151 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2152 return 1;
2153
2154 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2155 return -1;
2156
2157 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2158 return 1;
2159
2160 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2161 return -1;
2162
2163 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2164 return 1;
2165
2166 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2167 return -1;
2168
2169 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2170 return 1;
2171
dcea6a95
AM
2172 /* Finally, sort on where the symbol is in memory. The symbols will
2173 be in at most two malloc'd blocks, one for static syms, one for
2174 dynamic syms, and we distinguish the two blocks above by testing
2175 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2176 originally in the same order as the symbols (and we're not
2177 sorting the symbols themselves), this ensures a stable sort. */
2178 if (a < b)
2179 return -1;
2180 if (a > b)
2181 return 1;
2182 return 0;
90e3cdf2
JJ
2183}
2184
699733f6 2185/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2186
699733f6 2187static asymbol *
9ad9b810
AM
2188sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2189 bfd_vma value)
90e3cdf2 2190{
9ad9b810 2191 size_t mid;
90e3cdf2 2192
7292b3ac 2193 if (id == (unsigned) -1)
699733f6
AM
2194 {
2195 while (lo < hi)
2196 {
2197 mid = (lo + hi) >> 1;
2198 if (syms[mid]->value + syms[mid]->section->vma < value)
2199 lo = mid + 1;
2200 else if (syms[mid]->value + syms[mid]->section->vma > value)
2201 hi = mid;
2202 else
2203 return syms[mid];
2204 }
2205 }
2206 else
2207 {
2208 while (lo < hi)
2209 {
2210 mid = (lo + hi) >> 1;
2211 if (syms[mid]->section->id < id)
2212 lo = mid + 1;
2213 else if (syms[mid]->section->id > id)
2214 hi = mid;
2215 else if (syms[mid]->value < value)
2216 lo = mid + 1;
2217 else if (syms[mid]->value > value)
2218 hi = mid;
2219 else
2220 return syms[mid];
2221 }
2222 }
2223 return NULL;
90e3cdf2
JJ
2224}
2225
0a1b45a2 2226static bool
468392fb
AM
2227section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2228{
2229 bfd_vma vma = *(bfd_vma *) ptr;
2230 return ((section->flags & SEC_ALLOC) != 0
2231 && section->vma <= vma
2232 && vma < section->vma + section->size);
2233}
2234
699733f6 2235/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2236 entry syms. Also generate @plt symbols for the glink branch table.
2237 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2238
2239static long
a7535cf3
AM
2240ppc64_elf_get_synthetic_symtab (bfd *abfd,
2241 long static_count, asymbol **static_syms,
2242 long dyn_count, asymbol **dyn_syms,
c9727e01 2243 asymbol **ret)
90e3cdf2
JJ
2244{
2245 asymbol *s;
0ccf57bd 2246 size_t i, j, count;
90e3cdf2 2247 char *names;
0ccf57bd 2248 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2249 asection *opd = NULL;
0a1b45a2 2250 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2251 asymbol **syms;
ee67d69a 2252 int abi = abiversion (abfd);
90e3cdf2
JJ
2253
2254 *ret = NULL;
2255
ee67d69a
AM
2256 if (abi < 2)
2257 {
2258 opd = bfd_get_section_by_name (abfd, ".opd");
2259 if (opd == NULL && abi == 1)
2260 return 0;
2261 }
90e3cdf2 2262
a5259595
AM
2263 syms = NULL;
2264 codesecsym = 0;
2265 codesecsymend = 0;
2266 secsymend = 0;
2267 opdsymend = 0;
2268 symcount = 0;
2269 if (opd != NULL)
c9727e01 2270 {
a5259595
AM
2271 symcount = static_count;
2272 if (!relocatable)
2273 symcount += dyn_count;
2274 if (symcount == 0)
2275 return 0;
c9727e01 2276
a5259595
AM
2277 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2278 if (syms == NULL)
2279 return -1;
90e3cdf2 2280
a5259595
AM
2281 if (!relocatable && static_count != 0 && dyn_count != 0)
2282 {
2283 /* Use both symbol tables. */
2284 memcpy (syms, static_syms, static_count * sizeof (*syms));
2285 memcpy (syms + static_count, dyn_syms,
2286 (dyn_count + 1) * sizeof (*syms));
2287 }
2288 else if (!relocatable && static_count == 0)
2289 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2290 else
2291 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2292
0ccf57bd
AM
2293 /* Trim uninteresting symbols. Interesting symbols are section,
2294 function, and notype symbols. */
2295 for (i = 0, j = 0; i < symcount; ++i)
2296 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2297 | BSF_RELC | BSF_SRELC)) == 0)
2298 syms[j++] = syms[i];
2299 symcount = j;
2300
a5259595
AM
2301 synthetic_relocatable = relocatable;
2302 synthetic_opd = opd;
2303 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2304
a5259595
AM
2305 if (!relocatable && symcount > 1)
2306 {
bfa5bd2a
PA
2307 /* Trim duplicate syms, since we may have merged the normal
2308 and dynamic symbols. Actually, we only care about syms
2309 that have different values, so trim any with the same
2310 value. Don't consider ifunc and ifunc resolver symbols
2311 duplicates however, because GDB wants to know whether a
2312 text symbol is an ifunc resolver. */
a5259595 2313 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2314 {
2315 const asymbol *s0 = syms[i - 1];
2316 const asymbol *s1 = syms[i];
2317
2318 if ((s0->value + s0->section->vma
2319 != s1->value + s1->section->vma)
2320 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2321 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2322 syms[j++] = syms[i];
2323 }
a5259595
AM
2324 symcount = j;
2325 }
699733f6 2326
a5259595
AM
2327 i = 0;
2328 /* Note that here and in compare_symbols we can't compare opd and
2329 sym->section directly. With separate debug info files, the
2330 symbols will be extracted from the debug file while abfd passed
2331 to this function is the real binary. */
8bd10d6b
AM
2332 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2333 && strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2334 ++i;
2335 codesecsym = i;
2336
2337 for (; i < symcount; ++i)
2338 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2339 | SEC_THREAD_LOCAL))
2340 != (SEC_CODE | SEC_ALLOC))
2341 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2342 break;
2343 codesecsymend = i;
2344
2345 for (; i < symcount; ++i)
2346 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2347 break;
2348 secsymend = i;
2349
2350 for (; i < symcount; ++i)
2351 if (strcmp (syms[i]->section->name, ".opd") != 0)
2352 break;
2353 opdsymend = i;
2354
2355 for (; i < symcount; ++i)
2cdcc330
AM
2356 if (((syms[i]->section->flags
2357 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2358 != (SEC_CODE | SEC_ALLOC))
2359 break;
2360 symcount = i;
2361 }
c9727e01 2362 count = 0;
90e3cdf2 2363
699733f6 2364 if (relocatable)
90e3cdf2 2365 {
0a1b45a2 2366 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
699733f6
AM
2367 arelent *r;
2368 size_t size;
0ccf57bd 2369 size_t relcount;
90e3cdf2 2370
468392fb
AM
2371 if (opdsymend == secsymend)
2372 goto done;
2373
699733f6 2374 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2375 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2376 if (relcount == 0)
c9727e01 2377 goto done;
90e3cdf2 2378
0a1b45a2 2379 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
7356fed5
AM
2380 {
2381 count = -1;
2382 goto done;
2383 }
2384
699733f6 2385 size = 0;
595da8c5 2386 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2387 {
2388 asymbol *sym;
90e3cdf2 2389
595da8c5 2390 while (r < opd->relocation + relcount
699733f6
AM
2391 && r->address < syms[i]->value + opd->vma)
2392 ++r;
90e3cdf2 2393
595da8c5 2394 if (r == opd->relocation + relcount)
699733f6 2395 break;
90e3cdf2 2396
699733f6
AM
2397 if (r->address != syms[i]->value + opd->vma)
2398 continue;
90e3cdf2 2399
699733f6
AM
2400 if (r->howto->type != R_PPC64_ADDR64)
2401 continue;
90e3cdf2 2402
699733f6
AM
2403 sym = *r->sym_ptr_ptr;
2404 if (!sym_exists_at (syms, opdsymend, symcount,
2405 sym->section->id, sym->value + r->addend))
2406 {
2407 ++count;
2408 size += sizeof (asymbol);
2409 size += strlen (syms[i]->name) + 2;
2410 }
2411 }
90e3cdf2 2412
c4b0b099
AM
2413 if (size == 0)
2414 goto done;
699733f6
AM
2415 s = *ret = bfd_malloc (size);
2416 if (s == NULL)
2417 {
7356fed5 2418 count = -1;
c9727e01 2419 goto done;
699733f6 2420 }
90e3cdf2 2421
699733f6 2422 names = (char *) (s + count);
90e3cdf2 2423
595da8c5 2424 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2425 {
699733f6 2426 asymbol *sym;
90e3cdf2 2427
595da8c5 2428 while (r < opd->relocation + relcount
699733f6
AM
2429 && r->address < syms[i]->value + opd->vma)
2430 ++r;
90e3cdf2 2431
595da8c5 2432 if (r == opd->relocation + relcount)
699733f6
AM
2433 break;
2434
2435 if (r->address != syms[i]->value + opd->vma)
2436 continue;
2437
2438 if (r->howto->type != R_PPC64_ADDR64)
2439 continue;
90e3cdf2 2440
699733f6
AM
2441 sym = *r->sym_ptr_ptr;
2442 if (!sym_exists_at (syms, opdsymend, symcount,
2443 sym->section->id, sym->value + r->addend))
2444 {
2445 size_t len;
2446
2447 *s = *syms[i];
6ba2a415 2448 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2449 s->section = sym->section;
2450 s->value = sym->value + r->addend;
2451 s->name = names;
2452 *names++ = '.';
2453 len = strlen (syms[i]->name);
2454 memcpy (names, syms[i]->name, len + 1);
2455 names += len + 1;
6f610d07
UW
2456 /* Have udata.p point back to the original symbol this
2457 synthetic symbol was derived from. */
2458 s->udata.p = syms[i];
699733f6
AM
2459 s++;
2460 }
2461 }
2462 }
2463 else
90e3cdf2 2464 {
0a1b45a2 2465 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
ee67d69a 2466 bfd_byte *contents = NULL;
699733f6 2467 size_t size;
0ccf57bd 2468 size_t plt_count = 0;
468392fb
AM
2469 bfd_vma glink_vma = 0, resolv_vma = 0;
2470 asection *dynamic, *glink = NULL, *relplt = NULL;
2471 arelent *p;
90e3cdf2 2472
81ff113f
AM
2473 if (opd != NULL
2474 && ((opd->flags & SEC_HAS_CONTENTS) == 0
2475 || !bfd_malloc_and_get_section (abfd, opd, &contents)))
699733f6 2476 {
c4b0b099
AM
2477 free_contents_and_exit_err:
2478 count = -1;
ee67d69a 2479 free_contents_and_exit:
c9594989 2480 free (contents);
c9727e01 2481 goto done;
699733f6 2482 }
90e3cdf2 2483
699733f6
AM
2484 size = 0;
2485 for (i = secsymend; i < opdsymend; ++i)
2486 {
2487 bfd_vma ent;
90e3cdf2 2488
5ef11c02
AM
2489 /* Ignore bogus symbols. */
2490 if (syms[i]->value > opd->size - 8)
2491 continue;
2492
699733f6
AM
2493 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2494 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2495 {
2496 ++count;
2497 size += sizeof (asymbol);
2498 size += strlen (syms[i]->name) + 2;
2499 }
2500 }
90e3cdf2 2501
468392fb 2502 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2503 if (dyn_count != 0
2504 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2505 {
2506 bfd_byte *dynbuf, *extdyn, *extdynend;
2507 size_t extdynsize;
2508 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2509
81ff113f
AM
2510 if ((dynamic->flags & SEC_HAS_CONTENTS) == 0
2511 || !bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2512 goto free_contents_and_exit_err;
468392fb
AM
2513
2514 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2515 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2516
37c59664
AM
2517 for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
2518 (size_t) (extdynend - extdyn) >= extdynsize;
2519 extdyn += extdynsize)
468392fb
AM
2520 {
2521 Elf_Internal_Dyn dyn;
2522 (*swap_dyn_in) (abfd, extdyn, &dyn);
2523
2524 if (dyn.d_tag == DT_NULL)
2525 break;
2526
2527 if (dyn.d_tag == DT_PPC64_GLINK)
2528 {
9e390558
AM
2529 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2530 See comment in ppc64_elf_finish_dynamic_sections. */
2531 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2532 /* The .glink section usually does not survive the final
2533 link; search for the section (usually .text) where the
2534 glink stubs now reside. */
2535 glink = bfd_sections_find_if (abfd, section_covers_vma,
2536 &glink_vma);
2537 break;
2538 }
2539 }
2540
2541 free (dynbuf);
2542 }
2543
2544 if (glink != NULL)
2545 {
2546 /* Determine __glink trampoline by reading the relative branch
2547 from the first glink stub. */
2548 bfd_byte buf[4];
b9e5796b
AM
2549 unsigned int off = 0;
2550
2551 while (bfd_get_section_contents (abfd, glink, buf,
2552 glink_vma + off - glink->vma, 4))
468392fb
AM
2553 {
2554 unsigned int insn = bfd_get_32 (abfd, buf);
2555 insn ^= B_DOT;
2556 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2557 {
2cdcc330
AM
2558 resolv_vma
2559 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2560 break;
2561 }
2562 off += 4;
2563 if (off > 4)
2564 break;
468392fb
AM
2565 }
2566
2567 if (resolv_vma)
2568 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2569
066ee829
AM
2570 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2571 if (relplt != NULL)
2572 {
2573 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 2574 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
c4b0b099 2575 goto free_contents_and_exit_err;
68ffbac6 2576
93c6e8c3 2577 plt_count = NUM_SHDR_ENTRIES (&elf_section_data (relplt)->this_hdr);
066ee829 2578 size += plt_count * sizeof (asymbol);
468392fb 2579
066ee829
AM
2580 p = relplt->relocation;
2581 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2582 {
2583 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2584 if (p->addend != 0)
2585 size += sizeof ("+0x") - 1 + 16;
2586 }
066ee829 2587 }
468392fb
AM
2588 }
2589
c4b0b099
AM
2590 if (size == 0)
2591 goto free_contents_and_exit;
699733f6
AM
2592 s = *ret = bfd_malloc (size);
2593 if (s == NULL)
c4b0b099 2594 goto free_contents_and_exit_err;
90e3cdf2 2595
468392fb 2596 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2597
699733f6 2598 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2599 {
699733f6 2600 bfd_vma ent;
90e3cdf2 2601
5ef11c02
AM
2602 if (syms[i]->value > opd->size - 8)
2603 continue;
2604
699733f6
AM
2605 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2606 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2607 {
0ccf57bd 2608 size_t lo, hi;
699733f6 2609 size_t len;
c9727e01 2610 asection *sec = abfd->sections;
90e3cdf2 2611
699733f6
AM
2612 *s = *syms[i];
2613 lo = codesecsym;
2614 hi = codesecsymend;
2615 while (lo < hi)
2616 {
0ccf57bd 2617 size_t mid = (lo + hi) >> 1;
699733f6
AM
2618 if (syms[mid]->section->vma < ent)
2619 lo = mid + 1;
2620 else if (syms[mid]->section->vma > ent)
2621 hi = mid;
2622 else
c9727e01
AM
2623 {
2624 sec = syms[mid]->section;
2625 break;
2626 }
699733f6
AM
2627 }
2628
c9727e01 2629 if (lo >= hi && lo > codesecsym)
699733f6 2630 sec = syms[lo - 1]->section;
699733f6
AM
2631
2632 for (; sec != NULL; sec = sec->next)
2633 {
2634 if (sec->vma > ent)
2635 break;
63524580
JK
2636 /* SEC_LOAD may not be set if SEC is from a separate debug
2637 info file. */
2638 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2639 break;
2640 if ((sec->flags & SEC_CODE) != 0)
2641 s->section = sec;
2642 }
6ba2a415 2643 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2644 s->value = ent - s->section->vma;
2645 s->name = names;
2646 *names++ = '.';
2647 len = strlen (syms[i]->name);
2648 memcpy (names, syms[i]->name, len + 1);
2649 names += len + 1;
6f610d07
UW
2650 /* Have udata.p point back to the original symbol this
2651 synthetic symbol was derived from. */
2652 s->udata.p = syms[i];
699733f6 2653 s++;
90e3cdf2 2654 }
90e3cdf2 2655 }
699733f6 2656 free (contents);
468392fb
AM
2657
2658 if (glink != NULL && relplt != NULL)
2659 {
2660 if (resolv_vma)
2661 {
2662 /* Add a symbol for the main glink trampoline. */
86a4952b 2663 memset (s, 0, sizeof *s);
468392fb 2664 s->the_bfd = abfd;
6ba2a415 2665 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2666 s->section = glink;
2667 s->value = resolv_vma - glink->vma;
2668 s->name = names;
2cdcc330
AM
2669 memcpy (names, "__glink_PLTresolve",
2670 sizeof ("__glink_PLTresolve"));
468392fb
AM
2671 names += sizeof ("__glink_PLTresolve");
2672 s++;
2673 count++;
2674 }
2675
2676 /* FIXME: It would be very much nicer to put sym@plt on the
2677 stub rather than on the glink branch table entry. The
2678 objdump disassembler would then use a sensible symbol
2679 name on plt calls. The difficulty in doing so is
2680 a) finding the stubs, and,
2681 b) matching stubs against plt entries, and,
2682 c) there can be multiple stubs for a given plt entry.
2683
2684 Solving (a) could be done by code scanning, but older
2685 ppc64 binaries used different stubs to current code.
2686 (b) is the tricky one since you need to known the toc
2687 pointer for at least one function that uses a pic stub to
2688 be able to calculate the plt address referenced.
2689 (c) means gdb would need to set multiple breakpoints (or
2690 find the glink branch itself) when setting breakpoints
2691 for pending shared library loads. */
2692 p = relplt->relocation;
2693 for (i = 0; i < plt_count; i++, p++)
2694 {
2695 size_t len;
2696
2697 *s = **p->sym_ptr_ptr;
2698 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2699 we are defining a symbol, ensure one of them is set. */
2700 if ((s->flags & BSF_LOCAL) == 0)
2701 s->flags |= BSF_GLOBAL;
6ba2a415 2702 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2703 s->section = glink;
2704 s->value = glink_vma - glink->vma;
2705 s->name = names;
2706 s->udata.p = NULL;
2707 len = strlen ((*p->sym_ptr_ptr)->name);
2708 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2709 names += len;
e054468f
AM
2710 if (p->addend != 0)
2711 {
2712 memcpy (names, "+0x", sizeof ("+0x") - 1);
2713 names += sizeof ("+0x") - 1;
2714 bfd_sprintf_vma (abfd, names, p->addend);
2715 names += strlen (names);
2716 }
468392fb
AM
2717 memcpy (names, "@plt", sizeof ("@plt"));
2718 names += sizeof ("@plt");
2719 s++;
b9e5796b
AM
2720 if (abi < 2)
2721 {
2722 glink_vma += 8;
2723 if (i >= 0x8000)
2724 glink_vma += 4;
2725 }
2726 else
468392fb
AM
2727 glink_vma += 4;
2728 }
2729 count += plt_count;
2730 }
90e3cdf2
JJ
2731 }
2732
c9727e01 2733 done:
a7535cf3 2734 free (syms);
90e3cdf2
JJ
2735 return count;
2736}
5bd4f169 2737\f
65f38f15
AM
2738/* The following functions are specific to the ELF linker, while
2739 functions above are used generally. Those named ppc64_elf_* are
2740 called by the main ELF linker code. They appear in this file more
2741 or less in the order in which they are called. eg.
2742 ppc64_elf_check_relocs is called early in the link process,
2743 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2744 called.
2745
2746 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2747 functions have both a function code symbol and a function descriptor
2748 symbol. A call to foo in a relocatable object file looks like:
2749
2750 . .text
2751 . x:
2752 . bl .foo
2753 . nop
2754
2755 The function definition in another object file might be:
2756
2757 . .section .opd
2758 . foo: .quad .foo
2759 . .quad .TOC.@tocbase
2760 . .quad 0
2761 .
2762 . .text
2763 . .foo: blr
2764
2765 When the linker resolves the call during a static link, the branch
2766 unsurprisingly just goes to .foo and the .opd information is unused.
2767 If the function definition is in a shared library, things are a little
2768 different: The call goes via a plt call stub, the opd information gets
2769 copied to the plt, and the linker patches the nop.
2770
2771 . x:
2772 . bl .foo_stub
2773 . ld 2,40(1)
2774 .
2775 .
2776 . .foo_stub:
71a39c98
AM
2777 . std 2,40(1) # in practice, the call stub
2778 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2779 . addi 11,11,Lfoo@toc@l # this is the general idea
2780 . ld 12,0(11)
2781 . ld 2,8(11)
2782 . mtctr 12
2783 . ld 11,16(11)
e86ce104
AM
2784 . bctr
2785 .
2786 . .section .plt
2787 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2788
2789 The "reloc ()" notation is supposed to indicate that the linker emits
2790 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2791 copying.
2792
2793 What are the difficulties here? Well, firstly, the relocations
2794 examined by the linker in check_relocs are against the function code
2795 sym .foo, while the dynamic relocation in the plt is emitted against
2796 the function descriptor symbol, foo. Somewhere along the line, we need
2797 to carefully copy dynamic link information from one symbol to the other.
2798 Secondly, the generic part of the elf linker will make .foo a dynamic
2799 symbol as is normal for most other backends. We need foo dynamic
2800 instead, at least for an application final link. However, when
2801 creating a shared library containing foo, we need to have both symbols
2802 dynamic so that references to .foo are satisfied during the early
2803 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2804 definition from some other object, eg. a static library.
2805
2806 Update: As of August 2004, we support a new convention. Function
2807 calls may use the function descriptor symbol, ie. "bl foo". This
2808 behaves exactly as "bl .foo". */
65f38f15 2809
7c8bbca5
AM
2810/* Of those relocs that might be copied as dynamic relocs, this
2811 function selects those that must be copied when linking a shared
2812 library or PIE, even when the symbol is local. */
65f38f15 2813
1d483afe
AM
2814static int
2815must_be_dyn_reloc (struct bfd_link_info *info,
2816 enum elf_ppc64_reloc_type r_type)
2817{
2818 switch (r_type)
2819 {
2820 default:
7c8bbca5
AM
2821 /* Only relative relocs can be resolved when the object load
2822 address isn't fixed. DTPREL64 is excluded because the
2823 dynamic linker needs to differentiate global dynamic from
2824 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2825 return 1;
2826
2827 case R_PPC64_REL32:
2828 case R_PPC64_REL64:
2829 case R_PPC64_REL30:
1bdd8fac
AM
2830 case R_PPC64_TOC16:
2831 case R_PPC64_TOC16_DS:
2832 case R_PPC64_TOC16_LO:
2833 case R_PPC64_TOC16_HI:
2834 case R_PPC64_TOC16_HA:
2835 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2836 return 0;
2837
2838 case R_PPC64_TPREL16:
2839 case R_PPC64_TPREL16_LO:
2840 case R_PPC64_TPREL16_HI:
2841 case R_PPC64_TPREL16_HA:
2842 case R_PPC64_TPREL16_DS:
2843 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2844 case R_PPC64_TPREL16_HIGH:
2845 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2846 case R_PPC64_TPREL16_HIGHER:
2847 case R_PPC64_TPREL16_HIGHERA:
2848 case R_PPC64_TPREL16_HIGHEST:
2849 case R_PPC64_TPREL16_HIGHESTA:
2850 case R_PPC64_TPREL64:
c213164a 2851 case R_PPC64_TPREL34:
7c8bbca5
AM
2852 /* These relocations are relative but in a shared library the
2853 linker doesn't know the thread pointer base. */
2854 return bfd_link_dll (info);
1d483afe
AM
2855 }
2856}
65f38f15 2857
f4656909 2858/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2859 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2860 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2861 shared lib. With code that gcc generates it is vital that this be
2862 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2863 actually the address of a function descriptor which resides in the
2864 .opd section. gcc uses the descriptor directly rather than going
2865 via the GOT as some other ABIs do, which means that initialized
2866 function pointers reference the descriptor. Thus, a function
2867 pointer initialized to the address of a function in a shared
2868 library will either require a .dynbss copy and a copy reloc, or a
2869 dynamic reloc. Using a .dynbss copy redefines the function
2870 descriptor symbol to point to the copy. This presents a problem as
2871 a PLT entry for that function is also initialized from the function
2872 descriptor symbol and the copy may not be initialized first. */
a23b6845 2873#define ELIMINATE_COPY_RELOCS 1
f4656909 2874
721956f4 2875/* Section name for stubs is the associated section name plus this
29942be8
NC
2876 string. */
2877#define STUB_SUFFIX ".stub"
721956f4
AM
2878
2879/* Linker stubs.
2880 ppc_stub_long_branch:
2881 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2882 destination, but a 24 bit branch in a stub section will reach.
2883 . b dest
2884
2885 ppc_stub_plt_branch:
2886 Similar to the above, but a 24 bit branch in the stub section won't
2887 reach its destination.
6df4c9c2
AM
2888 . addis %r12,%r2,xxx@toc@ha
2889 . ld %r12,xxx@toc@l(%r12)
71a39c98 2890 . mtctr %r12
721956f4
AM
2891 . bctr
2892
2893 ppc_stub_plt_call:
2c66dc6c
AM
2894 Used to call a function in a shared library. If it so happens that
2895 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2896 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
7aba54da 2897 An r2save variant starts with "std %r2,40(%r1)".
71a39c98
AM
2898 . addis %r11,%r2,xxx@toc@ha
2899 . ld %r12,xxx+0@toc@l(%r11)
2900 . mtctr %r12
2901 . ld %r2,xxx+8@toc@l(%r11)
2902 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2903 . bctr
ad8e1ba5
AM
2904
2905 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2906 code to adjust the value and save r2 to support multiple toc sections.
2907 A ppc_stub_long_branch with an r2 offset looks like:
2908 . std %r2,40(%r1)
2909 . addis %r2,%r2,off@ha
2910 . addi %r2,%r2,off@l
2911 . b dest
2912
2913 A ppc_stub_plt_branch with an r2 offset looks like:
2914 . std %r2,40(%r1)
6df4c9c2
AM
2915 . addis %r12,%r2,xxx@toc@ha
2916 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2917 . addis %r2,%r2,off@ha
2918 . addi %r2,%r2,off@l
71a39c98 2919 . mtctr %r12
ad8e1ba5 2920 . bctr
ac2df442 2921
05d0e962
AM
2922 All of the above stubs are shown as their ELFv1 variants. ELFv2
2923 variants exist too, simpler for plt calls since a new toc pointer
2924 and static chain are not loaded by the stub. In addition, ELFv2
2925 has some more complex stubs to handle calls marked with NOTOC
7aba54da
AM
2926 relocs from functions where r2 is not a valid toc pointer.
2927 ppc_stub_long_branch_p9notoc:
05d0e962
AM
2928 . mflr %r12
2929 . bcl 20,31,1f
2930 . 1:
2931 . mflr %r11
2932 . mtlr %r12
2cdcc330
AM
2933 . addis %r12,%r11,dest-1b@ha
2934 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2935 . b dest
2936
7aba54da 2937 ppc_stub_plt_branch_p9notoc:
05d0e962
AM
2938 . mflr %r12
2939 . bcl 20,31,1f
2940 . 1:
2941 . mflr %r11
2942 . mtlr %r12
2943 . lis %r12,xxx-1b@highest
f891966f 2944 . ori %r12,%r12,xxx-1b@higher
05d0e962 2945 . sldi %r12,%r12,32
f891966f 2946 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2947 . ori %r12,%r12,xxx-1b@l
2948 . add %r12,%r11,%r12
2949 . mtctr %r12
2950 . bctr
2951
7aba54da 2952 ppc_stub_plt_call_p9notoc:
05d0e962
AM
2953 . mflr %r12
2954 . bcl 20,31,1f
2955 . 1:
2956 . mflr %r11
2957 . mtlr %r12
2958 . lis %r12,xxx-1b@highest
f891966f 2959 . ori %r12,%r12,xxx-1b@higher
05d0e962 2960 . sldi %r12,%r12,32
f891966f 2961 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2962 . ori %r12,%r12,xxx-1b@l
2963 . ldx %r12,%r11,%r12
2964 . mtctr %r12
2965 . bctr
2966
7c1f4227 2967 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2968 ppc_stub_long_branch_notoc:
d4b87b1e 2969 . pla %r12,dest@pcrel
04bdff6a
AM
2970 . b dest
2971 ppc_stub_plt_branch_notoc:
2972 . lis %r11,(dest-1f)@highesta34
2973 . ori %r11,%r11,(dest-1f)@highera34
2974 . sldi %r11,%r11,34
d4b87b1e 2975 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2976 . add %r12,%r11,%r12
2977 . mtctr %r12
2978 . bctr
2979 ppc_stub_plt_call_notoc:
2980 . lis %r11,(xxx-1f)@highesta34
2981 . ori %r11,%r11,(xxx-1f)@highera34
2982 . sldi %r11,%r11,34
d4b87b1e 2983 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2984 . ldx %r12,%r11,%r12
2985 . mtctr %r12
2986 . bctr
2987
05d0e962
AM
2988 In cases where the high instructions would add zero, they are
2989 omitted and following instructions modified in some cases.
7c1f4227 2990 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2991 to
2992 . pld %r12,xxx@pcrel
2993 . mtctr %r12
2994 . bctr
05d0e962 2995
7aba54da 2996 Stub variants may be merged. For example, if printf is called from
05d0e962
AM
2997 code with the tocsave optimization (ie. r2 saved in function
2998 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2999 and from other code without the tocsave optimization requiring a
3000 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
3001 type will be created. Calls with the tocsave optimization will
3002 enter this stub after the instruction saving r2. A similar
3003 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3004 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3005 to call an external function like printf. If other calls to printf
3006 require a ppc_stub_plt_call linkage stub then a single
7aba54da
AM
3007 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3008 call. */
3009
3010enum ppc_stub_main_type
2cdcc330 3011{
721956f4
AM
3012 ppc_stub_none,
3013 ppc_stub_long_branch,
3014 ppc_stub_plt_branch,
794e51c0 3015 ppc_stub_plt_call,
a4b6fadd
AM
3016 ppc_stub_global_entry,
3017 ppc_stub_save_res
721956f4
AM
3018};
3019
7aba54da
AM
3020/* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3021 these variations. */
3022
3023enum ppc_stub_sub_type
3024{
3025 ppc_stub_toc,
3026 ppc_stub_notoc,
3027 ppc_stub_p9notoc
3028};
3029
3030struct ppc_stub_type
3031{
3032 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3033 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3034 unsigned int r2save : 1;
3035};
3036
6f20ed8a
AM
3037/* Information on stub grouping. */
3038struct map_stub
3039{
3040 /* The stub section. */
3041 asection *stub_sec;
3042 /* This is the section to which stubs in the group will be attached. */
3043 asection *link_sec;
a4b6fadd
AM
3044 /* Next group. */
3045 struct map_stub *next;
3046 /* Whether to emit a copy of register save/restore functions in this
3047 group. */
3048 int needs_save_res;
df136d64
AM
3049 /* Current offset within stubs after the insn restoring lr in a
3050 _notoc or _both stub using bcl for pc-relative addressing, or
3051 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3052 unsigned int lr_restore;
3053 /* Accumulated size of EH info emitted to describe return address
3054 if stubs modify lr. Does not include 17 byte FDE header. */
3055 unsigned int eh_size;
3056 /* Offset in glink_eh_frame to the start of EH info for this group. */
3057 unsigned int eh_base;
6f20ed8a
AM
3058};
3059
2cdcc330
AM
3060struct ppc_stub_hash_entry
3061{
721956f4
AM
3062 /* Base hash table entry structure. */
3063 struct bfd_hash_entry root;
3064
7aba54da 3065 struct ppc_stub_type type;
ad8e1ba5 3066
6f20ed8a
AM
3067 /* Group information. */
3068 struct map_stub *group;
721956f4
AM
3069
3070 /* Offset within stub_sec of the beginning of this stub. */
3071 bfd_vma stub_offset;
3072
3073 /* Given the symbol's value and its section we can determine its final
3074 value when building the stubs (so the stub knows where to jump. */
3075 bfd_vma target_value;
3076 asection *target_section;
3077
721956f4
AM
3078 /* The symbol table entry, if any, that this was derived from. */
3079 struct ppc_link_hash_entry *h;
e054468f 3080 struct plt_entry *plt_ent;
721956f4 3081
2d7ad24e
AM
3082 /* Symbol type. */
3083 unsigned char symtype;
3084
6911b7dc
AM
3085 /* Symbol st_other. */
3086 unsigned char other;
ecc741e6
AM
3087
3088 /* Debug: Track hash table traversal. */
3089 unsigned int id;
721956f4
AM
3090};
3091
2cdcc330
AM
3092struct ppc_branch_hash_entry
3093{
721956f4
AM
3094 /* Base hash table entry structure. */
3095 struct bfd_hash_entry root;
3096
c456f082 3097 /* Offset within branch lookup table. */
721956f4
AM
3098 unsigned int offset;
3099
3100 /* Generation marker. */
3101 unsigned int iter;
3102};
65f38f15 3103
1657026c 3104/* Used to track dynamic relocations. */
19e08130
AM
3105struct ppc_dyn_relocs
3106{
3107 struct ppc_dyn_relocs *next;
3108
3109 /* The input section of the reloc. */
3110 asection *sec;
3111
3112 /* Total number of relocs copied for the input section. */
1657026c
AM
3113 unsigned int count;
3114
3115 /* Number of pc-relative relocs copied for the input section. */
3116 unsigned int pc_count;
3117
3118 /* Number of relocs that might become R_PPC64_RELATIVE. */
3119 unsigned int rel_count;
3120};
3121
3122struct ppc_local_dyn_relocs
3123{
3124 struct ppc_local_dyn_relocs *next;
3125
3126 /* The input section of the reloc. */
3127 asection *sec;
3128
3129 /* Total number of relocs copied for the input section. */
3130 unsigned int count;
3131
3132 /* Number of relocs that might become R_PPC64_RELATIVE. */
3133 unsigned int rel_count : 31;
19e08130
AM
3134
3135 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3136 unsigned int ifunc : 1;
3137};
3138
65f38f15
AM
3139struct ppc_link_hash_entry
3140{
3141 struct elf_link_hash_entry elf;
3142
2cdcc330
AM
3143 union
3144 {
b3fac117
AM
3145 /* A pointer to the most recently used stub hash entry against this
3146 symbol. */
3147 struct ppc_stub_hash_entry *stub_cache;
3148
3149 /* A pointer to the next symbol starting with a '.' */
3150 struct ppc_link_hash_entry *next_dot_sym;
3151 } u;
721956f4 3152
721956f4 3153 /* Link between function code and descriptor symbols. */
34814b9f 3154 struct ppc_link_hash_entry *oh;
721956f4 3155
e86ce104
AM
3156 /* Flag function code and descriptor symbols. */
3157 unsigned int is_func:1;
3158 unsigned int is_func_descriptor:1;
908b32fc 3159 unsigned int fake:1;
411e1bfb 3160
c5614fa4
AM
3161 /* Whether global opd/toc sym has been adjusted or not.
3162 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3163 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3164 unsigned int adjust_done:1;
3165
a4b6fadd
AM
3166 /* Set if this is an out-of-line register save/restore function,
3167 with non-standard calling convention. */
3168 unsigned int save_res:1;
3169
8b5f1ed8
AM
3170 /* Set if a duplicate symbol with non-zero localentry is detected,
3171 even when the duplicate symbol does not provide a definition. */
3172 unsigned int non_zero_localentry:1;
3173
411e1bfb 3174 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3175 Bits are or'd into the mask as the corresponding relocs are
3176 encountered during check_relocs, with TLS_TLS being set when any
3177 of the other TLS bits are set. tls_optimize clears bits when
3178 optimizing to indicate the corresponding GOT entry type is not
3179 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3180 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3181 These flags are also kept for local symbols. */
3182#define TLS_TLS 1 /* Any TLS reloc. */
3183#define TLS_GD 2 /* GD reloc. */
3184#define TLS_LD 4 /* LD reloc. */
3185#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3186#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3187#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3188#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3189#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3190 unsigned char tls_mask;
37da22e5
AM
3191
3192 /* The above field is also used to mark function symbols. In which
3193 case TLS_TLS will be 0. */
3194#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3195#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3196#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3197};
3198
ed7007c1
AM
3199static inline struct ppc_link_hash_entry *
3200ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3201{
3202 return (struct ppc_link_hash_entry *) ent;
3203}
3204
30845f11
AM
3205static inline struct elf_link_hash_entry *
3206elf_hash_entry (struct ppc_link_hash_entry *ent)
3207{
3208 return (struct elf_link_hash_entry *) ent;
3209}
3210
65f38f15
AM
3211/* ppc64 ELF linker hash table. */
3212
3213struct ppc_link_hash_table
3214{
3215 struct elf_link_hash_table elf;
3216
721956f4
AM
3217 /* The stub hash table. */
3218 struct bfd_hash_table stub_hash_table;
3219
3220 /* Another hash table for plt_branch stubs. */
3221 struct bfd_hash_table branch_hash_table;
3222
3b421ab3
AM
3223 /* Hash table for function prologue tocsave. */
3224 htab_t tocsave_htab;
3225
e7d1c40c
AM
3226 /* Various options and other info passed from the linker. */
3227 struct ppc64_elf_params *params;
721956f4 3228
6f20ed8a
AM
3229 /* The size of sec_info below. */
3230 unsigned int sec_info_arr_size;
3231
3232 /* Per-section array of extra section info. Done this way rather
3233 than as part of ppc64_elf_section_data so we have the info for
3234 non-ppc64 sections. */
3235 struct
3236 {
3237 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3238 bfd_vma toc_off;
6f20ed8a
AM
3239
3240 union
3241 {
3242 /* The section group that this section belongs to. */
3243 struct map_stub *group;
3244 /* A temp section list pointer. */
3245 asection *list;
3246 } u;
3247 } *sec_info;
721956f4 3248
a4b6fadd
AM
3249 /* Linked list of groups. */
3250 struct map_stub *group;
3251
ad8e1ba5
AM
3252 /* Temp used when calculating TOC pointers. */
3253 bfd_vma toc_curr;
bf102f86
AM
3254 bfd *toc_bfd;
3255 asection *toc_first_sec;
ad8e1ba5 3256
b3fac117
AM
3257 /* Used when adding symbols. */
3258 struct ppc_link_hash_entry *dot_syms;
3259
33e44f2e 3260 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3261 asection *glink;
9e390558 3262 asection *global_entry;
82bd7b59 3263 asection *sfpr;
2d7ad24e
AM
3264 asection *pltlocal;
3265 asection *relpltlocal;
4ce794b7
AM
3266 asection *brlt;
3267 asection *relbrlt;
58d180e8 3268 asection *glink_eh_frame;
ec338859 3269
8387904d
AM
3270 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3271 struct ppc_link_hash_entry *tls_get_addr;
3272 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3273 struct ppc_link_hash_entry *tga_desc;
3274 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3275 struct map_stub *tga_group;
411e1bfb 3276
927be08e
AM
3277 /* The size of reliplt used by got entry relocs. */
3278 bfd_size_type got_reli_size;
3279
0aac2413 3280 /* DT_RELR array of section/r_offset. */
1657026c
AM
3281 size_t relr_alloc;
3282 size_t relr_count;
0aac2413
AM
3283 struct
3284 {
3285 asection *sec;
3286 bfd_vma off;
3287 } *relr;
1657026c 3288
9b5ecbd0 3289 /* Statistics. */
7aba54da 3290 unsigned long stub_count[ppc_stub_save_res];
9b5ecbd0 3291
ee75fd95
AM
3292 /* Number of stubs against global syms. */
3293 unsigned long stub_globals;
3294
ee67d69a
AM
3295 /* Set if we're linking code with function descriptors. */
3296 unsigned int opd_abi:1;
3297
4c52953f 3298 /* Support for multiple toc sections. */
33c0ec9d 3299 unsigned int do_multi_toc:1;
4c52953f 3300 unsigned int multi_toc_needed:1;
927be08e 3301 unsigned int second_toc_pass:1;
67f0cbdb 3302 unsigned int do_toc_opt:1;
4c52953f 3303
9a23f96e
AM
3304 /* Set if tls optimization is enabled. */
3305 unsigned int do_tls_opt:1;
3306
3e04d765
AM
3307 /* Set if inline plt calls should be converted to direct calls. */
3308 unsigned int can_convert_all_inline_plt:1;
3309
0441f94f
AM
3310 /* Set if a stub_offset changed. */
3311 unsigned int stub_changed:1;
3312
5d1634d7 3313 /* Set on error. */
99877b66 3314 unsigned int stub_error:1;
721956f4 3315
8c5b4e52
AM
3316 /* Whether func_desc_adjust needs to be run over symbols. */
3317 unsigned int need_func_desc_adj:1;
721956f4 3318
f378ab09
AM
3319 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3320 unsigned int has_plt_localentry0:1;
3321
5663e321
AM
3322 /* Whether calls are made via the PLT from NOTOC functions. */
3323 unsigned int notoc_plt:1;
3324
e10a07b3
AM
3325 /* Whether any code linked seems to be Power10. */
3326 unsigned int has_power10_relocs:1;
3327
ecc741e6
AM
3328 /* Incremented once for each stub sized. */
3329 unsigned int stub_id;
3330
721956f4
AM
3331 /* Incremented every time we size stubs. */
3332 unsigned int stub_iteration;
0441f94f
AM
3333
3334/* After 20 iterations of stub sizing we no longer allow stubs to
3335 shrink. This is to break out of a pathological case where adding
3336 stubs or increasing their size on one iteration decreases section
3337 gaps (perhaps due to alignment), which then results in smaller
3338 stubs on the next iteration. */
3339#define STUB_SHRINK_ITER 20
65f38f15
AM
3340};
3341
4c52953f
AM
3342/* Rename some of the generic section flags to better document how they
3343 are used here. */
b0dddeec
AM
3344
3345/* Nonzero if this section has TLS related relocations. */
3346#define has_tls_reloc sec_flg0
3347
9737e8af
AM
3348/* Nonzero if this section has a call to __tls_get_addr lacking marker
3349 relocations. */
3350#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3351
3352/* Nonzero if this section has any toc or got relocs. */
3353#define has_toc_reloc sec_flg2
3354
3355/* Nonzero if this section has a call to another section that uses
3356 the toc or got. */
d77c8a4b 3357#define makes_toc_func_call sec_flg3
b0dddeec
AM
3358
3359/* Recursion protection when determining above flag. */
d77c8a4b 3360#define call_check_in_progress sec_flg4
70cc837d 3361#define call_check_done sec_flg5
4c52953f 3362
65f38f15
AM
3363/* Get the ppc64 ELF linker hash table from a link_info structure. */
3364
3365#define ppc_hash_table(p) \
573eb292
AM
3366 ((is_elf_hash_table ((p)->hash) \
3367 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3368 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3369
721956f4
AM
3370#define ppc_stub_hash_lookup(table, string, create, copy) \
3371 ((struct ppc_stub_hash_entry *) \
3372 bfd_hash_lookup ((table), (string), (create), (copy)))
3373
3374#define ppc_branch_hash_lookup(table, string, create, copy) \
3375 ((struct ppc_branch_hash_entry *) \
3376 bfd_hash_lookup ((table), (string), (create), (copy)))
3377
3378/* Create an entry in the stub hash table. */
3379
3380static struct bfd_hash_entry *
4ce794b7
AM
3381stub_hash_newfunc (struct bfd_hash_entry *entry,
3382 struct bfd_hash_table *table,
3383 const char *string)
721956f4
AM
3384{
3385 /* Allocate the structure if it has not already been allocated by a
3386 subclass. */
3387 if (entry == NULL)
3388 {
3389 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3390 if (entry == NULL)
3391 return entry;
3392 }
3393
3394 /* Call the allocation method of the superclass. */
3395 entry = bfd_hash_newfunc (entry, table, string);
3396 if (entry != NULL)
3397 {
3398 struct ppc_stub_hash_entry *eh;
3399
3400 /* Initialize the local fields. */
3401 eh = (struct ppc_stub_hash_entry *) entry;
7aba54da
AM
3402 eh->type.main = ppc_stub_none;
3403 eh->type.sub = ppc_stub_toc;
3404 eh->type.r2save = 0;
6f20ed8a 3405 eh->group = NULL;
721956f4
AM
3406 eh->stub_offset = 0;
3407 eh->target_value = 0;
3408 eh->target_section = NULL;
721956f4 3409 eh->h = NULL;
6911b7dc 3410 eh->plt_ent = NULL;
ecc741e6 3411 eh->symtype = 0;
6911b7dc 3412 eh->other = 0;
ecc741e6 3413 eh->id = 0;
721956f4
AM
3414 }
3415
3416 return entry;
3417}
3418
3419/* Create an entry in the branch hash table. */
3420
3421static struct bfd_hash_entry *
4ce794b7
AM
3422branch_hash_newfunc (struct bfd_hash_entry *entry,
3423 struct bfd_hash_table *table,
3424 const char *string)
721956f4
AM
3425{
3426 /* Allocate the structure if it has not already been allocated by a
3427 subclass. */
3428 if (entry == NULL)
3429 {
3430 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3431 if (entry == NULL)
3432 return entry;
3433 }
3434
3435 /* Call the allocation method of the superclass. */
3436 entry = bfd_hash_newfunc (entry, table, string);
3437 if (entry != NULL)
3438 {
3439 struct ppc_branch_hash_entry *eh;
3440
3441 /* Initialize the local fields. */
3442 eh = (struct ppc_branch_hash_entry *) entry;
3443 eh->offset = 0;
3444 eh->iter = 0;
3445 }
3446
3447 return entry;
3448}
3449
65f38f15
AM
3450/* Create an entry in a ppc64 ELF linker hash table. */
3451
3452static struct bfd_hash_entry *
4ce794b7
AM
3453link_hash_newfunc (struct bfd_hash_entry *entry,
3454 struct bfd_hash_table *table,
3455 const char *string)
65f38f15
AM
3456{
3457 /* Allocate the structure if it has not already been allocated by a
3458 subclass. */
3459 if (entry == NULL)
3460 {
3461 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3462 if (entry == NULL)
3463 return entry;
3464 }
3465
3466 /* Call the allocation method of the superclass. */
3467 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3468 if (entry != NULL)
3469 {
3470 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3471
b3fac117 3472 memset (&eh->u.stub_cache, 0,
908b32fc 3473 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3474 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3475
3476 /* When making function calls, old ABI code references function entry
3477 points (dot symbols), while new ABI code references the function
3478 descriptor symbol. We need to make any combination of reference and
3479 definition work together, without breaking archive linking.
3480
3481 For a defined function "foo" and an undefined call to "bar":
3482 An old object defines "foo" and ".foo", references ".bar" (possibly
3483 "bar" too).
3484 A new object defines "foo" and references "bar".
3485
3486 A new object thus has no problem with its undefined symbols being
3487 satisfied by definitions in an old object. On the other hand, the
3488 old object won't have ".bar" satisfied by a new object.
3489
3490 Keep a list of newly added dot-symbols. */
3491
3492 if (string[0] == '.')
3493 {
3494 struct ppc_link_hash_table *htab;
3495
3496 htab = (struct ppc_link_hash_table *) table;
3497 eh->u.next_dot_sym = htab->dot_syms;
3498 htab->dot_syms = eh;
3499 }
65f38f15
AM
3500 }
3501
3502 return entry;
3503}
3504
2cdcc330
AM
3505struct tocsave_entry
3506{
3b421ab3
AM
3507 asection *sec;
3508 bfd_vma offset;
3509};
3510
3511static hashval_t
3512tocsave_htab_hash (const void *p)
3513{
3514 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3515 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3516}
3517
3518static int
3519tocsave_htab_eq (const void *p1, const void *p2)
3520{
3521 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3522 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3523 return e1->sec == e2->sec && e1->offset == e2->offset;
3524}
3525
68faa637
AM
3526/* Destroy a ppc64 ELF linker hash table. */
3527
3528static void
d495ab0d 3529ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3530{
d495ab0d 3531 struct ppc_link_hash_table *htab;
68faa637 3532
d495ab0d 3533 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3534 if (htab->tocsave_htab)
3535 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3536 bfd_hash_table_free (&htab->branch_hash_table);
3537 bfd_hash_table_free (&htab->stub_hash_table);
3538 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3539}
3540
65f38f15
AM
3541/* Create a ppc64 ELF linker hash table. */
3542
3543static struct bfd_link_hash_table *
4ce794b7 3544ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3545{
3546 struct ppc_link_hash_table *htab;
986f0783 3547 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3548
4ce794b7 3549 htab = bfd_zmalloc (amt);
65f38f15
AM
3550 if (htab == NULL)
3551 return NULL;
3552
66eb6687 3553 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3554 sizeof (struct ppc_link_hash_entry),
3555 PPC64_ELF_DATA))
65f38f15 3556 {
e2d34d7d 3557 free (htab);
65f38f15
AM
3558 return NULL;
3559 }
3560
721956f4 3561 /* Init the stub hash table too. */
66eb6687
AM
3562 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3563 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3564 {
d495ab0d 3565 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3566 return NULL;
3567 }
721956f4
AM
3568
3569 /* And the branch hash table. */
66eb6687
AM
3570 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3571 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3572 {
3573 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3574 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3575 return NULL;
3576 }
721956f4 3577
3b421ab3
AM
3578 htab->tocsave_htab = htab_try_create (1024,
3579 tocsave_htab_hash,
3580 tocsave_htab_eq,
3581 NULL);
3582 if (htab->tocsave_htab == NULL)
2915c55b 3583 {
d495ab0d 3584 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3585 return NULL;
3586 }
d495ab0d 3587 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3588
3254fd24
AM
3589 /* Initializing two fields of the union is just cosmetic. We really
3590 only care about glist, but when compiled on a 32-bit host the
3591 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3592 debugger inspection of these fields look nicer. */
a6aa5195
AM
3593 htab->elf.init_got_refcount.refcount = 0;
3594 htab->elf.init_got_refcount.glist = NULL;
3595 htab->elf.init_plt_refcount.refcount = 0;
3596 htab->elf.init_plt_refcount.glist = NULL;
3597 htab->elf.init_got_offset.offset = 0;
3598 htab->elf.init_got_offset.glist = NULL;
3599 htab->elf.init_plt_offset.offset = 0;
3600 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3601
65f38f15
AM
3602 return &htab->elf.root;
3603}
3604
bfeb4a28
AM
3605/* Create sections for linker generated code. */
3606
0a1b45a2 3607static bool
bfeb4a28
AM
3608create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3609{
3610 struct ppc_link_hash_table *htab;
3611 flagword flags;
3612
3613 htab = ppc_hash_table (info);
3614
bfeb4a28
AM
3615 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3616 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3617 if (htab->params->save_restore_funcs)
3618 {
3619 /* Create .sfpr for code to save and restore fp regs. */
3620 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3621 flags);
3622 if (htab->sfpr == NULL
fd361982 3623 || !bfd_set_section_alignment (htab->sfpr, 2))
0a1b45a2 3624 return false;
b32547cd
AM
3625 }
3626
3627 if (bfd_link_relocatable (info))
0a1b45a2 3628 return true;
bfeb4a28
AM
3629
3630 /* Create .glink for lazy dynamic linking support. */
3631 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3632 flags);
3633 if (htab->glink == NULL
fd361982 3634 || !bfd_set_section_alignment (htab->glink, 3))
0a1b45a2 3635 return false;
bfeb4a28 3636
9e390558
AM
3637 /* The part of .glink used by global entry stubs, separate so that
3638 it can be aligned appropriately without affecting htab->glink. */
3639 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3640 flags);
3641 if (htab->global_entry == NULL
fd361982 3642 || !bfd_set_section_alignment (htab->global_entry, 2))
0a1b45a2 3643 return false;
9e390558 3644
bfeb4a28
AM
3645 if (!info->no_ld_generated_unwind_info)
3646 {
3647 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3648 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3649 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3650 ".eh_frame",
3651 flags);
3652 if (htab->glink_eh_frame == NULL
fd361982 3653 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
0a1b45a2 3654 return false;
bfeb4a28
AM
3655 }
3656
3657 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3658 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3659 if (htab->elf.iplt == NULL
fd361982 3660 || !bfd_set_section_alignment (htab->elf.iplt, 3))
0a1b45a2 3661 return false;
bfeb4a28
AM
3662
3663 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3664 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3665 htab->elf.irelplt
3666 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3667 if (htab->elf.irelplt == NULL
fd361982 3668 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
0a1b45a2 3669 return false;
bfeb4a28
AM
3670
3671 /* Create branch lookup table for plt_branch stubs. */
3672 flags = (SEC_ALLOC | SEC_LOAD
3673 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3674 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3675 flags);
3676 if (htab->brlt == NULL
fd361982 3677 || !bfd_set_section_alignment (htab->brlt, 3))
0a1b45a2 3678 return false;
bfeb4a28 3679
2d7ad24e
AM
3680 /* Local plt entries, put in .branch_lt but a separate section for
3681 convenience. */
3682 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3683 flags);
3684 if (htab->pltlocal == NULL
fd361982 3685 || !bfd_set_section_alignment (htab->pltlocal, 3))
0a1b45a2 3686 return false;
2d7ad24e 3687
0e1862bb 3688 if (!bfd_link_pic (info))
0a1b45a2 3689 return true;
bfeb4a28
AM
3690
3691 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3692 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3693 htab->relbrlt
3694 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3695 if (htab->relbrlt == NULL
fd361982 3696 || !bfd_set_section_alignment (htab->relbrlt, 3))
0a1b45a2 3697 return false;
bfeb4a28 3698
2d7ad24e
AM
3699 htab->relpltlocal
3700 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3701 if (htab->relpltlocal == NULL
fd361982 3702 || !bfd_set_section_alignment (htab->relpltlocal, 3))
0a1b45a2 3703 return false;
2d7ad24e 3704
0a1b45a2 3705 return true;
bfeb4a28
AM
3706}
3707
e717da7e
AM
3708/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3709
0a1b45a2 3710bool
e7d1c40c
AM
3711ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3712 struct ppc64_elf_params *params)
e717da7e
AM
3713{
3714 struct ppc_link_hash_table *htab;
3715
e7d1c40c 3716 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3717
3718/* Always hook our dynamic sections into the first bfd, which is the
3719 linker created stub bfd. This ensures that the GOT header is at
3720 the start of the output TOC section. */
3721 htab = ppc_hash_table (info);
e7d1c40c
AM
3722 htab->elf.dynobj = params->stub_bfd;
3723 htab->params = params;
bfeb4a28 3724
bfeb4a28 3725 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3726}
3727
721956f4
AM
3728/* Build a name for an entry in the stub hash table. */
3729
3730static char *
4ce794b7
AM
3731ppc_stub_name (const asection *input_section,
3732 const asection *sym_sec,
3733 const struct ppc_link_hash_entry *h,
3734 const Elf_Internal_Rela *rel)
721956f4
AM
3735{
3736 char *stub_name;
bcaa2f82 3737 ssize_t len;
721956f4
AM
3738
3739 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3740 offsets from a sym as a branch target? In fact, we could
3741 probably assume the addend is always zero. */
3742 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3743
3744 if (h)
3745 {
3746 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3747 stub_name = bfd_malloc (len);
46de2a7c
AM
3748 if (stub_name == NULL)
3749 return stub_name;
3750
bcaa2f82
AM
3751 len = sprintf (stub_name, "%08x.%s+%x",
3752 input_section->id & 0xffffffff,
3753 h->elf.root.root.string,
3754 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3755 }
3756 else
3757 {
ad8e1ba5 3758 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3759 stub_name = bfd_malloc (len);
46de2a7c
AM
3760 if (stub_name == NULL)
3761 return stub_name;
3762
bcaa2f82
AM
3763 len = sprintf (stub_name, "%08x.%x:%x+%x",
3764 input_section->id & 0xffffffff,
3765 sym_sec->id & 0xffffffff,
3766 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3767 (int) rel->r_addend & 0xffffffff);
721956f4 3768 }
bcaa2f82 3769 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3770 stub_name[len - 2] = 0;
721956f4
AM
3771 return stub_name;
3772}
3773
e10a07b3 3774/* If mixing power10 with non-power10 code and --power10-stubs is not
7aba54da
AM
3775 specified (or is auto) then there may be multiple stub types for any
3776 given symbol. Up to three classes of stubs are stored in separate
3777 stub_hash_table entries having the same key string. The entries
3778 will always be adjacent on entry->root.next chain, even if hash
3779 table resizing occurs. This function selects the correct entry to
3780 use. */
e10a07b3
AM
3781
3782static struct ppc_stub_hash_entry *
7aba54da
AM
3783select_alt_stub (struct ppc_stub_hash_entry *entry,
3784 enum elf_ppc64_reloc_type r_type)
e10a07b3 3785{
7aba54da 3786 enum ppc_stub_sub_type subt;
e10a07b3 3787
7aba54da
AM
3788 switch (r_type)
3789 {
3790 case R_PPC64_REL24_NOTOC:
3791 subt = ppc_stub_notoc;
3792 break;
3793 case R_PPC64_REL24_P9NOTOC:
3794 subt = ppc_stub_p9notoc;
3795 break;
3796 default:
3797 subt = ppc_stub_toc;
3798 break;
3799 }
e10a07b3 3800
7aba54da 3801 while (entry != NULL && entry->type.sub != subt)
e10a07b3
AM
3802 {
3803 const char *stub_name = entry->root.string;
3804
3805 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3806 if (entry != NULL
3807 && entry->root.string != stub_name)
3808 entry = NULL;
3809 }
3810
3811 return entry;
3812}
3813
721956f4
AM
3814/* Look up an entry in the stub hash. Stub entries are cached because
3815 creating the stub name takes a bit of time. */
3816
3817static struct ppc_stub_hash_entry *
4ce794b7
AM
3818ppc_get_stub_entry (const asection *input_section,
3819 const asection *sym_sec,
039b3fef 3820 struct ppc_link_hash_entry *h,
4ce794b7
AM
3821 const Elf_Internal_Rela *rel,
3822 struct ppc_link_hash_table *htab)
721956f4
AM
3823{
3824 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3825 struct map_stub *group;
721956f4
AM
3826
3827 /* If this input section is part of a group of sections sharing one
3828 stub section, then use the id of the first section in the group.
3829 Stub names need to include a section id, as there may well be
3830 more than one stub used to reach say, printf, and we need to
3831 distinguish between them. */
6f20ed8a 3832 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3833 if (group == NULL)
3834 return NULL;
721956f4 3835
b3fac117
AM
3836 if (h != NULL && h->u.stub_cache != NULL
3837 && h->u.stub_cache->h == h
6f20ed8a 3838 && h->u.stub_cache->group == group)
721956f4 3839 {
b3fac117 3840 stub_entry = h->u.stub_cache;
721956f4
AM
3841 }
3842 else
3843 {
3844 char *stub_name;
3845
6f20ed8a 3846 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3847 if (stub_name == NULL)
3848 return NULL;
3849
3850 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 3851 stub_name, false, false);
721956f4 3852 if (h != NULL)
b3fac117 3853 h->u.stub_cache = stub_entry;
721956f4
AM
3854
3855 free (stub_name);
3856 }
3857
e10a07b3 3858 if (stub_entry != NULL && htab->params->power10_stubs == -1)
7aba54da 3859 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
e10a07b3 3860
721956f4
AM
3861 return stub_entry;
3862}
3863
3864/* Add a new stub entry to the stub hash. Not all fields of the new
3865 stub entry are initialised. */
3866
3867static struct ppc_stub_hash_entry *
4ce794b7
AM
3868ppc_add_stub (const char *stub_name,
3869 asection *section,
25f53a85 3870 struct bfd_link_info *info)
721956f4 3871{
25f53a85 3872 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3873 struct map_stub *group;
721956f4
AM
3874 asection *link_sec;
3875 asection *stub_sec;
3876 struct ppc_stub_hash_entry *stub_entry;
3877
6f20ed8a
AM
3878 group = htab->sec_info[section->id].u.group;
3879 link_sec = group->link_sec;
3880 stub_sec = group->stub_sec;
721956f4
AM
3881 if (stub_sec == NULL)
3882 {
6f20ed8a
AM
3883 size_t namelen;
3884 bfd_size_type len;
3885 char *s_name;
721956f4 3886
6f20ed8a
AM
3887 namelen = strlen (link_sec->name);
3888 len = namelen + sizeof (STUB_SUFFIX);
3889 s_name = bfd_alloc (htab->params->stub_bfd, len);
3890 if (s_name == NULL)
3891 return NULL;
721956f4 3892
6f20ed8a
AM
3893 memcpy (s_name, link_sec->name, namelen);
3894 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3895 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3896 if (stub_sec == NULL)
3897 return NULL;
3898 group->stub_sec = stub_sec;
721956f4
AM
3899 }
3900
3901 /* Enter this entry into the linker stub hash table. */
3902 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
0a1b45a2 3903 true, false);
721956f4
AM
3904 if (stub_entry == NULL)
3905 {
695344c0 3906 /* xgettext:c-format */
cf97bcb0
AM
3907 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3908 section->owner, stub_name);
721956f4
AM
3909 return NULL;
3910 }
3911
6f20ed8a 3912 stub_entry->group = group;
721956f4 3913 stub_entry->stub_offset = 0;
721956f4
AM
3914 return stub_entry;
3915}
3916
7aba54da
AM
3917/* A stub has already been created, but it may not be the required
3918 type. We shouldn't be transitioning from plt_call to long_branch
3919 stubs or vice versa, but we might be upgrading from plt_call to
3920 plt_call with r2save for example. */
3921
3922static bool
3923ppc_merge_stub (struct ppc_link_hash_table *htab,
3924 struct ppc_stub_hash_entry *stub_entry,
3925 struct ppc_stub_type stub_type,
3926 enum elf_ppc64_reloc_type r_type)
3927{
3928 struct ppc_stub_type old_type = stub_entry->type;
3929
3930 if (old_type.main == ppc_stub_save_res)
3931 return true;
3932
3933 if (htab->params->power10_stubs == -1)
3934 {
3935 /* For --power10-stubs=auto, don't merge _notoc and other
3936 varieties of stubs. */
3937 struct ppc_stub_hash_entry *alt_stub;
3938
3939 alt_stub = select_alt_stub (stub_entry, r_type);
3940 if (alt_stub == NULL)
3941 {
3942 alt_stub = ((struct ppc_stub_hash_entry *)
3943 stub_hash_newfunc (NULL,
3944 &htab->stub_hash_table,
3945 stub_entry->root.string));
3946 if (alt_stub == NULL)
3947 return false;
3948
3949 *alt_stub = *stub_entry;
3950 stub_entry->root.next = &alt_stub->root;
3951
3952 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3953 Not that it matters, this just puts smaller stubs first. */
3954 if (stub_type.sub == ppc_stub_notoc)
3955 alt_stub = stub_entry;
3956 else if (stub_type.sub == ppc_stub_p9notoc
3957 && alt_stub->root.next
3958 && alt_stub->root.next->string == alt_stub->root.string)
3959 {
3960 struct ppc_stub_hash_entry *next
3961 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3962 alt_stub->type = next->type;
3963 alt_stub = next;
3964 }
3965 alt_stub->type = stub_type;
3966 return true;
3967 }
3968 stub_entry = alt_stub;
3969 }
3970
3971 old_type = stub_entry->type;
3972 if (old_type.main == ppc_stub_plt_branch)
04cb922a 3973 old_type.main = ppc_stub_long_branch;
7aba54da
AM
3974
3975 if (old_type.main != stub_type.main
3976 || (old_type.sub != stub_type.sub
3977 && old_type.sub != ppc_stub_toc
3978 && stub_type.sub != ppc_stub_toc))
3979 abort ();
3980
3981 stub_entry->type.sub |= stub_type.sub;
3982 stub_entry->type.r2save |= stub_type.r2save;
3983 return true;
3984}
3985
e717da7e
AM
3986/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3987 not already done. */
65f38f15 3988
0a1b45a2 3989static bool
e717da7e 3990create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3991{
e717da7e
AM
3992 asection *got, *relgot;
3993 flagword flags;
3994 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3995
0c8d6e5c 3996 if (!is_ppc64_elf (abfd))
0a1b45a2 3997 return false;
4dfe6ac6 3998 if (htab == NULL)
0a1b45a2 3999 return false;
0ffa91dd 4000
33e44f2e
AM
4001 if (!htab->elf.sgot
4002 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
0a1b45a2 4003 return false;
e717da7e
AM
4004
4005 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4006 | SEC_LINKER_CREATED);
4007
c456f082 4008 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4009 if (!got
fd361982 4010 || !bfd_set_section_alignment (got, 3))
0a1b45a2 4011 return false;
65f38f15 4012
c456f082
AM
4013 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4014 flags | SEC_READONLY);
e717da7e 4015 if (!relgot
fd361982 4016 || !bfd_set_section_alignment (relgot, 3))
0a1b45a2 4017 return false;
e717da7e
AM
4018
4019 ppc64_elf_tdata (abfd)->got = got;
4020 ppc64_elf_tdata (abfd)->relgot = relgot;
0a1b45a2 4021 return true;
65f38f15 4022}
5bd4f169 4023
b31867b6
AM
4024/* Follow indirect and warning symbol links. */
4025
4026static inline struct bfd_link_hash_entry *
4027follow_link (struct bfd_link_hash_entry *h)
4028{
4029 while (h->type == bfd_link_hash_indirect
4030 || h->type == bfd_link_hash_warning)
4031 h = h->u.i.link;
4032 return h;
4033}
4034
4035static inline struct elf_link_hash_entry *
4036elf_follow_link (struct elf_link_hash_entry *h)
4037{
4038 return (struct elf_link_hash_entry *) follow_link (&h->root);
4039}
4040
4041static inline struct ppc_link_hash_entry *
4042ppc_follow_link (struct ppc_link_hash_entry *h)
4043{
ed7007c1 4044 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
4045}
4046
40d16e0b
AM
4047/* Merge PLT info on FROM with that on TO. */
4048
4049static void
4050move_plt_plist (struct ppc_link_hash_entry *from,
4051 struct ppc_link_hash_entry *to)
4052{
4053 if (from->elf.plt.plist != NULL)
4054 {
4055 if (to->elf.plt.plist != NULL)
4056 {
4057 struct plt_entry **entp;
4058 struct plt_entry *ent;
4059
4060 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4061 {
4062 struct plt_entry *dent;
4063
4064 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4065 if (dent->addend == ent->addend)
4066 {
4067 dent->plt.refcount += ent->plt.refcount;
4068 *entp = ent->next;
4069 break;
4070 }
4071 if (dent == NULL)
4072 entp = &ent->next;
4073 }
4074 *entp = to->elf.plt.plist;
4075 }
4076
4077 to->elf.plt.plist = from->elf.plt.plist;
4078 from->elf.plt.plist = NULL;
4079 }
4080}
4081
65f38f15
AM
4082/* Copy the extra info we tack onto an elf_link_hash_entry. */
4083
4084static void
fcfa13d2
AM
4085ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4086 struct elf_link_hash_entry *dir,
4087 struct elf_link_hash_entry *ind)
65f38f15
AM
4088{
4089 struct ppc_link_hash_entry *edir, *eind;
4090
ed7007c1
AM
4091 edir = ppc_elf_hash_entry (dir);
4092 eind = ppc_elf_hash_entry (ind);
65f38f15 4093
c79d6685
AM
4094 edir->is_func |= eind->is_func;
4095 edir->is_func_descriptor |= eind->is_func_descriptor;
4096 edir->tls_mask |= eind->tls_mask;
4097 if (eind->oh != NULL)
4098 edir->oh = ppc_follow_link (eind->oh);
4099
474436e6 4100 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4101 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4102 edir->elf.ref_regular |= eind->elf.ref_regular;
4103 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4104 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4105 edir->elf.needs_plt |= eind->elf.needs_plt;
4106 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4107
d311bc8b
AM
4108 /* If we were called to copy over info for a weak sym, don't copy
4109 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4110 in order to simplify readonly_dynrelocs and save a field in the
4111 symbol hash entry, but that means dyn_relocs can't be used in any
4112 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4113 are then tested. */
d311bc8b 4114 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4115 return;
d311bc8b 4116
411e1bfb 4117 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 4118 if (ind->dyn_relocs != NULL)
65f38f15 4119 {
190eb1dd 4120 if (dir->dyn_relocs != NULL)
bbd7ec4a 4121 {
1657026c
AM
4122 struct ppc_dyn_relocs **pp;
4123 struct ppc_dyn_relocs *p;
bbd7ec4a 4124
fcfa13d2 4125 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 4126 list. Merge any entries against the same section. */
1657026c
AM
4127 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4128 (p = *pp) != NULL;
4129 )
bbd7ec4a 4130 {
1657026c 4131 struct ppc_dyn_relocs *q;
bbd7ec4a 4132
1657026c
AM
4133 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4134 q != NULL;
4135 q = q->next)
bbd7ec4a
AM
4136 if (q->sec == p->sec)
4137 {
bbd7ec4a 4138 q->count += p->count;
1657026c
AM
4139 q->pc_count += p->pc_count;
4140 q->rel_count += p->rel_count;
bbd7ec4a
AM
4141 *pp = p->next;
4142 break;
4143 }
4144 if (q == NULL)
4145 pp = &p->next;
4146 }
1657026c 4147 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
bbd7ec4a
AM
4148 }
4149
190eb1dd
L
4150 dir->dyn_relocs = ind->dyn_relocs;
4151 ind->dyn_relocs = NULL;
65f38f15 4152 }
65f38f15 4153
81848ca0
AM
4154 /* Copy over got entries that we may have already seen to the
4155 symbol which just became indirect. */
411e1bfb
AM
4156 if (eind->elf.got.glist != NULL)
4157 {
4158 if (edir->elf.got.glist != NULL)
4159 {
4160 struct got_entry **entp;
4161 struct got_entry *ent;
4162
4163 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4164 {
4165 struct got_entry *dent;
4166
4167 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4168 if (dent->addend == ent->addend
e717da7e 4169 && dent->owner == ent->owner
411e1bfb
AM
4170 && dent->tls_type == ent->tls_type)
4171 {
4172 dent->got.refcount += ent->got.refcount;
4173 *entp = ent->next;
4174 break;
4175 }
4176 if (dent == NULL)
4177 entp = &ent->next;
4178 }
4179 *entp = edir->elf.got.glist;
4180 }
4181
4182 edir->elf.got.glist = eind->elf.got.glist;
4183 eind->elf.got.glist = NULL;
4184 }
4185
4186 /* And plt entries. */
40d16e0b 4187 move_plt_plist (eind, edir);
411e1bfb 4188
fcfa13d2 4189 if (eind->elf.dynindx != -1)
411e1bfb 4190 {
fcfa13d2
AM
4191 if (edir->elf.dynindx != -1)
4192 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4193 edir->elf.dynstr_index);
411e1bfb
AM
4194 edir->elf.dynindx = eind->elf.dynindx;
4195 edir->elf.dynstr_index = eind->elf.dynstr_index;
4196 eind->elf.dynindx = -1;
4197 eind->elf.dynstr_index = 0;
4198 }
411e1bfb
AM
4199}
4200
8387904d
AM
4201/* Find the function descriptor hash entry from the given function code
4202 hash entry FH. Link the entries via their OH fields. */
4203
4204static struct ppc_link_hash_entry *
b31867b6 4205lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4206{
4207 struct ppc_link_hash_entry *fdh = fh->oh;
4208
4209 if (fdh == NULL)
4210 {
4211 const char *fd_name = fh->elf.root.root.string + 1;
4212
ed7007c1 4213 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
0a1b45a2 4214 false, false, false));
b31867b6
AM
4215 if (fdh == NULL)
4216 return fdh;
4217
4218 fdh->is_func_descriptor = 1;
4219 fdh->oh = fh;
4220 fh->is_func = 1;
4221 fh->oh = fdh;
8387904d
AM
4222 }
4223
8c5b4e52
AM
4224 fdh = ppc_follow_link (fdh);
4225 fdh->is_func_descriptor = 1;
4226 fdh->oh = fh;
4227 return fdh;
8387904d
AM
4228}
4229
8c5b4e52 4230/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4231
4232static struct ppc_link_hash_entry *
4233make_fdh (struct bfd_link_info *info,
908b32fc 4234 struct ppc_link_hash_entry *fh)
bb700d78 4235{
8c5b4e52
AM
4236 bfd *abfd = fh->elf.root.u.undef.abfd;
4237 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4238 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4239 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4240 ? BSF_WEAK
4241 : BSF_GLOBAL);
4242
4243 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4244 fh->elf.root.root.string + 1,
4245 flags, bfd_und_section_ptr, 0,
0a1b45a2 4246 NULL, false, false, &bh))
bb700d78
AM
4247 return NULL;
4248
4249 fdh = (struct ppc_link_hash_entry *) bh;
4250 fdh->elf.non_elf = 0;
908b32fc
AM
4251 fdh->fake = 1;
4252 fdh->is_func_descriptor = 1;
4253 fdh->oh = fh;
4254 fh->is_func = 1;
4255 fh->oh = fdh;
bb700d78
AM
4256 return fdh;
4257}
4258
8387904d
AM
4259/* Fix function descriptor symbols defined in .opd sections to be
4260 function type. */
555cd476 4261
0a1b45a2 4262static bool
c16153ae 4263ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4264 struct bfd_link_info *info,
555cd476 4265 Elf_Internal_Sym *isym,
6911b7dc 4266 const char **name,
555cd476
AM
4267 flagword *flags ATTRIBUTE_UNUSED,
4268 asection **sec,
b53dfeb2 4269 bfd_vma *value)
555cd476 4270{
b53dfeb2 4271 if (*sec != NULL
f1885d1e 4272 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4273 {
4274 asection *code_sec;
4275
4276 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4277 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4278 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4279
4280 /* If the symbol is a function defined in .opd, and the function
4281 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4282 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4283 && (*sec)->reloc_count != 0
4284 && opd_entry_value (*sec, *value, &code_sec, NULL,
0a1b45a2 4285 false) != (bfd_vma) -1
b53dfeb2
AM
4286 && discarded_section (code_sec))
4287 {
4288 *sec = bfd_und_section_ptr;
4289 isym->st_shndx = SHN_UNDEF;
4290 }
4291 }
dbd1e97e
AM
4292 else if (*sec != NULL
4293 && strcmp ((*sec)->name, ".toc") == 0
4294 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4295 {
4296 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4297 if (htab != NULL)
4298 htab->params->object_in_toc = 1;
4299 }
433817dd 4300
6911b7dc
AM
4301 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4302 {
4303 if (abiversion (ibfd) == 0)
4304 set_abiversion (ibfd, 2);
4305 else if (abiversion (ibfd) == 1)
4306 {
cf97bcb0
AM
4307 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4308 " for ABI version 1"), *name);
6911b7dc 4309 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4310 return false;
6911b7dc
AM
4311 }
4312 }
4313
0a1b45a2 4314 return true;
555cd476
AM
4315}
4316
6911b7dc
AM
4317/* Merge non-visibility st_other attributes: local entry point. */
4318
4319static void
4320ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4321 unsigned int st_other,
0a1b45a2
AM
4322 bool definition,
4323 bool dynamic)
6911b7dc 4324{
f378ab09 4325 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4326 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4327 | ELF_ST_VISIBILITY (h->other));
4328}
4329
8c5b4e52
AM
4330/* Hook called on merging a symbol. We use this to clear "fake" since
4331 we now have a real symbol. */
4332
0a1b45a2 4333static bool
8c5b4e52 4334ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4335 const Elf_Internal_Sym *isym,
8c5b4e52 4336 asection **psec ATTRIBUTE_UNUSED,
0a1b45a2
AM
4337 bool newdef ATTRIBUTE_UNUSED,
4338 bool olddef ATTRIBUTE_UNUSED,
8c5b4e52
AM
4339 bfd *oldbfd ATTRIBUTE_UNUSED,
4340 const asection *oldsec ATTRIBUTE_UNUSED)
4341{
ed7007c1 4342 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4343 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4344 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
0a1b45a2 4345 return true;
8c5b4e52
AM
4346}
4347
8387904d 4348/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4349 inclusion of a new ABI object archive that defines "bar".
4350 NAME is a symbol defined in an archive. Return a symbol in the hash
4351 table that might be satisfied by the archive symbols. */
8387904d 4352
b585e899 4353static struct bfd_link_hash_entry *
8387904d
AM
4354ppc64_elf_archive_symbol_lookup (bfd *abfd,
4355 struct bfd_link_info *info,
4356 const char *name)
4357{
b585e899 4358 struct bfd_link_hash_entry *h;
8387904d
AM
4359 char *dot_name;
4360 size_t len;
4361
4362 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc 4363 if (h != NULL
b585e899 4364 && ppc_hash_table (info) != NULL
908b32fc
AM
4365 /* Don't return this sym if it is a fake function descriptor
4366 created by add_symbol_adjust. */
b585e899 4367 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4368 return h;
4369
4370 if (name[0] == '.')
4371 return h;
4372
4373 len = strlen (name);
4374 dot_name = bfd_alloc (abfd, len + 2);
4375 if (dot_name == NULL)
b585e899 4376 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
4377 dot_name[0] = '.';
4378 memcpy (dot_name + 1, name, len + 1);
4379 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4380 bfd_release (abfd, dot_name);
a804e476
AM
4381 if (h != NULL)
4382 return h;
4383
4384 if (strcmp (name, "__tls_get_addr_opt") == 0)
4385 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4386 return h;
4387}
4388
4389/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4390 new ABI object defines "bar". Well, at least, undefined dot symbols
4391 are made weak. This stops later archive searches from including an
4392 object if we already have a function descriptor definition. It also
35b0ce59
AM
4393 prevents the linker complaining about undefined symbols.
4394 We also check and correct mismatched symbol visibility here. The
4395 most restrictive visibility of the function descriptor and the
4396 function entry symbol is used. */
8387904d 4397
0a1b45a2 4398static bool
b3fac117 4399add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4400{
8387904d
AM
4401 struct ppc_link_hash_table *htab;
4402 struct ppc_link_hash_entry *fdh;
4403
b3fac117
AM
4404 if (eh->elf.root.type == bfd_link_hash_warning)
4405 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4406
8400d40d 4407 if (eh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 4408 return true;
8400d40d 4409
b3fac117
AM
4410 if (eh->elf.root.root.string[0] != '.')
4411 abort ();
8387904d 4412
b3fac117 4413 htab = ppc_hash_table (info);
4dfe6ac6 4414 if (htab == NULL)
0a1b45a2 4415 return false;
4dfe6ac6 4416
b31867b6 4417 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4418 if (fdh == NULL
4419 && !bfd_link_relocatable (info)
4420 && (eh->elf.root.type == bfd_link_hash_undefined
4421 || eh->elf.root.type == bfd_link_hash_undefweak)
4422 && eh->elf.ref_regular)
4423 {
4424 /* Make an undefined function descriptor sym, in order to
4425 pull in an --as-needed shared lib. Archives are handled
4426 elsewhere. */
4427 fdh = make_fdh (info, eh);
4428 if (fdh == NULL)
0a1b45a2 4429 return false;
bb700d78 4430 }
8c5b4e52
AM
4431
4432 if (fdh != NULL)
8387904d 4433 {
35b0ce59
AM
4434 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4435 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4436
4437 /* Make both descriptor and entry symbol have the most
4438 constraining visibility of either symbol. */
35b0ce59
AM
4439 if (entry_vis < descr_vis)
4440 fdh->elf.other += entry_vis - descr_vis;
4441 else if (entry_vis > descr_vis)
4442 eh->elf.other += descr_vis - entry_vis;
4443
8c5b4e52
AM
4444 /* Propagate reference flags from entry symbol to function
4445 descriptor symbol. */
bc4e12de 4446 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4447 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4448 fdh->elf.ref_regular |= eh->elf.ref_regular;
4449 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4450
4451 if (!fdh->elf.forced_local
4452 && fdh->elf.dynindx == -1
4453 && fdh->elf.versioned != versioned_hidden
4454 && (bfd_link_dll (info)
4455 || fdh->elf.def_dynamic
4456 || fdh->elf.ref_dynamic)
4457 && (eh->elf.ref_regular
4458 || eh->elf.def_regular))
4459 {
2cdcc330 4460 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 4461 return false;
35b0ce59 4462 }
8387904d 4463 }
99877b66 4464
0a1b45a2 4465 return true;
8387904d
AM
4466}
4467
f6c7c3e8
AM
4468/* Set up opd section info and abiversion for IBFD, and process list
4469 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4470
0a1b45a2 4471static bool
f6c7c3e8 4472ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4473{
99877b66 4474 struct ppc_link_hash_table *htab;
b3fac117 4475 struct ppc_link_hash_entry **p, *eh;
459609d6 4476 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4477
459609d6 4478 if (opd != NULL && opd->size != 0)
b3fac117 4479 {
6313825c
AM
4480 if (ppc64_elf_section_data (opd)->sec_type == sec_normal)
4481 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4482 else if (ppc64_elf_section_data (opd)->sec_type != sec_opd)
4483 BFD_FAIL ();
b9399fcf 4484
459609d6
AM
4485 if (abiversion (ibfd) == 0)
4486 set_abiversion (ibfd, 1);
8a2058b5 4487 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4488 {
695344c0 4489 /* xgettext:c-format */
cf97bcb0
AM
4490 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4491 ibfd, abiversion (ibfd));
459609d6 4492 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4493 return false;
f6c7c3e8 4494 }
b9399fcf 4495 }
f6c7c3e8 4496
b9399fcf
AM
4497 if (is_ppc64_elf (info->output_bfd))
4498 {
4499 /* For input files without an explicit abiversion in e_flags
4500 we should have flagged any with symbol st_other bits set
4501 as ELFv1 and above flagged those with .opd as ELFv2.
4502 Set the output abiversion if not yet set, and for any input
4503 still ambiguous, take its abiversion from the output.
4504 Differences in ABI are reported later. */
4505 if (abiversion (info->output_bfd) == 0)
4506 set_abiversion (info->output_bfd, abiversion (ibfd));
4507 else if (abiversion (ibfd) == 0)
4508 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4509 }
4510
459609d6
AM
4511 htab = ppc_hash_table (info);
4512 if (htab == NULL)
0a1b45a2 4513 return true;
459609d6 4514
b9399fcf
AM
4515 if (opd != NULL && opd->size != 0
4516 && (ibfd->flags & DYNAMIC) == 0
4517 && (opd->flags & SEC_RELOC) != 0
4518 && opd->reloc_count != 0
4519 && !bfd_is_abs_section (opd->output_section)
4520 && info->gc_sections)
4521 {
4522 /* Garbage collection needs some extra help with .opd sections.
4523 We don't want to necessarily keep everything referenced by
4524 relocs in .opd, as that would keep all functions. Instead,
4525 if we reference an .opd symbol (a function descriptor), we
4526 want to keep the function code symbol's section. This is
4527 easy for global symbols, but for local syms we need to keep
4528 information about the associated function section. */
4529 bfd_size_type amt;
4530 asection **opd_sym_map;
4531 Elf_Internal_Shdr *symtab_hdr;
4532 Elf_Internal_Rela *relocs, *rel_end, *rel;
4533
4534 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4535 opd_sym_map = bfd_zalloc (ibfd, amt);
4536 if (opd_sym_map == NULL)
0a1b45a2 4537 return false;
b9399fcf
AM
4538 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4539 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4540 info->keep_memory);
4541 if (relocs == NULL)
0a1b45a2 4542 return false;
b9399fcf
AM
4543 symtab_hdr = &elf_symtab_hdr (ibfd);
4544 rel_end = relocs + opd->reloc_count - 1;
4545 for (rel = relocs; rel < rel_end; rel++)
4546 {
4547 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4548 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4549
4550 if (r_type == R_PPC64_ADDR64
4551 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4552 && r_symndx < symtab_hdr->sh_info)
4553 {
4554 Elf_Internal_Sym *isym;
4555 asection *s;
4556
f1dfbfdb
L
4557 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4558 r_symndx);
b9399fcf
AM
4559 if (isym == NULL)
4560 {
4561 if (elf_section_data (opd)->relocs != relocs)
4562 free (relocs);
0a1b45a2 4563 return false;
b9399fcf
AM
4564 }
4565
4566 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4567 if (s != NULL && s != opd)
4568 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4569 }
4570 }
4571 if (elf_section_data (opd)->relocs != relocs)
4572 free (relocs);
4573 }
459609d6
AM
4574
4575 p = &htab->dot_syms;
4576 while ((eh = *p) != NULL)
4577 {
4578 *p = NULL;
4579 if (&eh->elf == htab->elf.hgot)
4580 ;
4581 else if (htab->elf.hgot == NULL
4582 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4583 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4584 else if (abiversion (ibfd) <= 1)
4585 {
4586 htab->need_func_desc_adj = 1;
4587 if (!add_symbol_adjust (eh, info))
0a1b45a2 4588 return false;
8c5b4e52 4589 }
459609d6
AM
4590 p = &eh->u.next_dot_sym;
4591 }
0a1b45a2 4592 return true;
8387904d
AM
4593}
4594
97fed1c9
JJ
4595/* Undo hash table changes when an --as-needed input file is determined
4596 not to be needed. */
4597
0a1b45a2 4598static bool
e5034e59
AM
4599ppc64_elf_notice_as_needed (bfd *ibfd,
4600 struct bfd_link_info *info,
4601 enum notice_asneeded_action act)
97fed1c9 4602{
e5034e59
AM
4603 if (act == notice_not_needed)
4604 {
4605 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4606
e5034e59 4607 if (htab == NULL)
0a1b45a2 4608 return false;
4dfe6ac6 4609
e5034e59
AM
4610 htab->dot_syms = NULL;
4611 }
4612 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4613}
4614
aa374f67
AM
4615/* If --just-symbols against a final linked binary, then assume we need
4616 toc adjusting stubs when calling functions defined there. */
4617
4618static void
4619ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4620{
4621 if ((sec->flags & SEC_CODE) != 0
4622 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4623 && is_ppc64_elf (sec->owner))
4624 {
2c3f079f
AM
4625 if (abiversion (sec->owner) >= 2
4626 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4627 sec->has_toc_reloc = 1;
4628 }
4629 _bfd_elf_link_just_syms (sec, info);
4630}
4631
e054468f 4632static struct plt_entry **
4ce794b7
AM
4633update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4634 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4635{
4636 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4637 struct plt_entry **local_plt;
f961d9dd 4638 unsigned char *local_got_tls_masks;
411e1bfb
AM
4639
4640 if (local_got_ents == NULL)
4641 {
4642 bfd_size_type size = symtab_hdr->sh_info;
4643
e054468f
AM
4644 size *= (sizeof (*local_got_ents)
4645 + sizeof (*local_plt)
4646 + sizeof (*local_got_tls_masks));
4ce794b7 4647 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4648 if (local_got_ents == NULL)
e054468f 4649 return NULL;
411e1bfb
AM
4650 elf_local_got_ents (abfd) = local_got_ents;
4651 }
4652
37da22e5 4653 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4654 {
4655 struct got_entry *ent;
4656
4657 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4658 if (ent->addend == r_addend
4659 && ent->owner == abfd
4660 && ent->tls_type == tls_type)
411e1bfb
AM
4661 break;
4662 if (ent == NULL)
4663 {
986f0783 4664 size_t amt = sizeof (*ent);
4ce794b7 4665 ent = bfd_alloc (abfd, amt);
411e1bfb 4666 if (ent == NULL)
0a1b45a2 4667 return false;
411e1bfb
AM
4668 ent->next = local_got_ents[r_symndx];
4669 ent->addend = r_addend;
e717da7e 4670 ent->owner = abfd;
411e1bfb 4671 ent->tls_type = tls_type;
0a1b45a2 4672 ent->is_indirect = false;
411e1bfb
AM
4673 ent->got.refcount = 0;
4674 local_got_ents[r_symndx] = ent;
4675 }
4676 ent->got.refcount += 1;
4677 }
4678
e054468f 4679 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4680 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4681 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4682
4683 return local_plt + r_symndx;
65f38f15
AM
4684}
4685
0a1b45a2 4686static bool
e054468f 4687update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4688{
411e1bfb 4689 struct plt_entry *ent;
1e2f5b6e 4690
e054468f 4691 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4692 if (ent->addend == addend)
4693 break;
4694 if (ent == NULL)
1e2f5b6e 4695 {
986f0783 4696 size_t amt = sizeof (*ent);
4ce794b7 4697 ent = bfd_alloc (abfd, amt);
411e1bfb 4698 if (ent == NULL)
0a1b45a2 4699 return false;
e054468f 4700 ent->next = *plist;
411e1bfb
AM
4701 ent->addend = addend;
4702 ent->plt.refcount = 0;
e054468f 4703 *plist = ent;
1e2f5b6e 4704 }
411e1bfb 4705 ent->plt.refcount += 1;
0a1b45a2 4706 return true;
1e2f5b6e
AM
4707}
4708
0a1b45a2 4709static bool
e054468f
AM
4710is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4711{
4712 return (r_type == R_PPC64_REL24
05d0e962 4713 || r_type == R_PPC64_REL24_NOTOC
7aba54da 4714 || r_type == R_PPC64_REL24_P9NOTOC
e054468f
AM
4715 || r_type == R_PPC64_REL14
4716 || r_type == R_PPC64_REL14_BRTAKEN
4717 || r_type == R_PPC64_REL14_BRNTAKEN
4718 || r_type == R_PPC64_ADDR24
4719 || r_type == R_PPC64_ADDR14
4720 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4721 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4722 || r_type == R_PPC64_PLTCALL
4723 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4724}
4725
4726/* Relocs on inline plt call sequence insns prior to the call. */
4727
0a1b45a2 4728static bool
23cedd1d
AM
4729is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4730{
4731 return (r_type == R_PPC64_PLT16_HA
4732 || r_type == R_PPC64_PLT16_HI
4733 || r_type == R_PPC64_PLT16_LO
4734 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4735 || r_type == R_PPC64_PLT_PCREL34
4736 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4737 || r_type == R_PPC64_PLTSEQ
4738 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4739}
4740
733ae98c
AM
4741/* Of relocs which might appear paired with TLSGD and TLSLD marker
4742 relocs, return true for those that operate on a dword. */
4743
4744static bool
4745is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4746{
4747 return (r_type == R_PPC64_PLT_PCREL34
4748 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4749 || r_type == R_PPC64_PLTCALL);
4750}
4751
4752/* Like bfd_reloc_offset_in_range but without a howto. Return true
4753 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4754
4755static bool
4756offset_in_range (asection *sec, bfd_vma offset, size_t size)
4757{
4758 return offset <= sec->size && size <= sec->size - offset;
4759}
4760
5bd4f169 4761/* Look through the relocs for a section during the first phase, and
65f38f15 4762 calculate needed space in the global offset table, procedure
5d1634d7 4763 linkage table, and dynamic reloc sections. */
5bd4f169 4764
0a1b45a2 4765static bool
4ce794b7
AM
4766ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4767 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4768{
65f38f15 4769 struct ppc_link_hash_table *htab;
5bd4f169 4770 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4771 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4772 const Elf_Internal_Rela *rel;
4773 const Elf_Internal_Rela *rel_end;
5bd4f169 4774 asection *sreloc;
3a71aa26 4775 struct elf_link_hash_entry *tga, *dottga;
0a1b45a2 4776 bool is_opd;
5bd4f169 4777
0e1862bb 4778 if (bfd_link_relocatable (info))
0a1b45a2 4779 return true;
5bd4f169 4780
0c8d6e5c 4781 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4782
65f38f15 4783 htab = ppc_hash_table (info);
4dfe6ac6 4784 if (htab == NULL)
0a1b45a2 4785 return false;
4dfe6ac6 4786
3a71aa26 4787 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 4788 false, false, true);
3a71aa26 4789 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 4790 false, false, true);
0ffa91dd 4791 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4792 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4793 sreloc = NULL;
b9399fcf 4794 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4795 rel_end = relocs + sec->reloc_count;
4796 for (rel = relocs; rel < rel_end; rel++)
4797 {
4798 unsigned long r_symndx;
4799 struct elf_link_hash_entry *h;
3a3a4c1f 4800 Elf_Internal_Sym *isym;
04c9666a 4801 enum elf_ppc64_reloc_type r_type;
727fc41e 4802 int tls_type;
7c8fe5c4 4803 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4804 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4805
4806 r_symndx = ELF64_R_SYM (rel->r_info);
4807 if (r_symndx < symtab_hdr->sh_info)
3a3a4c1f
AM
4808 {
4809 h = NULL;
4810 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4811 if (isym == NULL)
4812 return false;
4813 }
5bd4f169 4814 else
973a3492 4815 {
3a3a4c1f 4816 isym = NULL;
973a3492 4817 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4818 h = elf_follow_link (h);
1c865ab2
AM
4819
4820 if (h == htab->elf.hgot)
4821 sec->has_toc_reloc = 1;
973a3492 4822 }
5bd4f169 4823
5663e321
AM
4824 r_type = ELF64_R_TYPE (rel->r_info);
4825 switch (r_type)
4826 {
04bdff6a
AM
4827 case R_PPC64_D34:
4828 case R_PPC64_D34_LO:
4829 case R_PPC64_D34_HI30:
4830 case R_PPC64_D34_HA30:
4831 case R_PPC64_D28:
c213164a
AM
4832 case R_PPC64_TPREL34:
4833 case R_PPC64_DTPREL34:
04bdff6a 4834 case R_PPC64_PCREL34:
5663e321 4835 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4836 case R_PPC64_GOT_TLSGD_PCREL34:
4837 case R_PPC64_GOT_TLSLD_PCREL34:
4838 case R_PPC64_GOT_TPREL_PCREL34:
4839 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4840 case R_PPC64_PLT_PCREL34:
4841 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4842 case R_PPC64_PCREL28:
e10a07b3 4843 htab->has_power10_relocs = 1;
133a1f60
AM
4844 break;
4845 default:
5663e321
AM
4846 break;
4847 }
903b777d
AM
4848
4849 switch (r_type)
4850 {
4851 case R_PPC64_PLT16_HA:
4852 case R_PPC64_GOT_TLSLD16_HA:
4853 case R_PPC64_GOT_TLSGD16_HA:
4854 case R_PPC64_GOT_TPREL16_HA:
4855 case R_PPC64_GOT_DTPREL16_HA:
4856 case R_PPC64_GOT16_HA:
4857 case R_PPC64_TOC16_HA:
4858 case R_PPC64_PLT16_LO:
4859 case R_PPC64_PLT16_LO_DS:
4860 case R_PPC64_GOT_TLSLD16_LO:
4861 case R_PPC64_GOT_TLSGD16_LO:
4862 case R_PPC64_GOT_TPREL16_LO_DS:
4863 case R_PPC64_GOT_DTPREL16_LO_DS:
4864 case R_PPC64_GOT16_LO:
4865 case R_PPC64_GOT16_LO_DS:
4866 case R_PPC64_TOC16_LO:
4867 case R_PPC64_TOC16_LO_DS:
4868 case R_PPC64_GOT_PCREL34:
4869 ppc64_elf_tdata (abfd)->has_optrel = 1;
4870 ppc64_elf_section_data (sec)->has_optrel = 1;
4871 break;
4872 default:
4873 break;
4874 }
4875
f749f26e 4876 ifunc = NULL;
25f23106
AM
4877 if (h != NULL)
4878 {
4879 if (h->type == STT_GNU_IFUNC)
4880 {
4881 h->needs_plt = 1;
4882 ifunc = &h->plt.plist;
4883 }
4884 }
4885 else
4886 {
25f23106
AM
4887 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4888 {
4889 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4890 rel->r_addend,
37da22e5 4891 NON_GOT | PLT_IFUNC);
25f23106 4892 if (ifunc == NULL)
0a1b45a2 4893 return false;
25f23106
AM
4894 }
4895 }
727fc41e 4896
f749f26e 4897 tls_type = 0;
a33d1f77 4898 switch (r_type)
5bd4f169 4899 {
727fc41e
AM
4900 case R_PPC64_TLSGD:
4901 case R_PPC64_TLSLD:
4902 /* These special tls relocs tie a call to __tls_get_addr with
4903 its parameter symbol. */
37da22e5 4904 if (h != NULL)
ed7007c1 4905 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4906 else
4907 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4908 rel->r_addend,
37da22e5 4909 NON_GOT | TLS_TLS | TLS_MARK))
0a1b45a2 4910 return false;
37da22e5 4911 sec->has_tls_reloc = 1;
727fc41e
AM
4912 break;
4913
411e1bfb
AM
4914 case R_PPC64_GOT_TLSLD16:
4915 case R_PPC64_GOT_TLSLD16_LO:
4916 case R_PPC64_GOT_TLSLD16_HI:
4917 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4918 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4919 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4920 goto dogottls;
4921
4922 case R_PPC64_GOT_TLSGD16:
4923 case R_PPC64_GOT_TLSGD16_LO:
4924 case R_PPC64_GOT_TLSGD16_HI:
4925 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4926 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4927 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4928 goto dogottls;
4929
4930 case R_PPC64_GOT_TPREL16_DS:
4931 case R_PPC64_GOT_TPREL16_LO_DS:
4932 case R_PPC64_GOT_TPREL16_HI:
4933 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4934 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4935 if (bfd_link_dll (info))
411e1bfb
AM
4936 info->flags |= DF_STATIC_TLS;
4937 tls_type = TLS_TLS | TLS_TPREL;
4938 goto dogottls;
4939
4940 case R_PPC64_GOT_DTPREL16_DS:
4941 case R_PPC64_GOT_DTPREL16_LO_DS:
4942 case R_PPC64_GOT_DTPREL16_HI:
4943 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4944 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4945 tls_type = TLS_TLS | TLS_DTPREL;
4946 dogottls:
4947 sec->has_tls_reloc = 1;
066f4018 4948 goto dogot;
411e1bfb 4949
903b777d
AM
4950 case R_PPC64_GOT16:
4951 case R_PPC64_GOT16_LO:
4952 case R_PPC64_GOT16_HI:
65f38f15 4953 case R_PPC64_GOT16_HA:
903b777d 4954 case R_PPC64_GOT16_DS:
066f4018 4955 case R_PPC64_GOT16_LO_DS:
4a421c53 4956 case R_PPC64_GOT_PCREL34:
066f4018 4957 dogot:
65f38f15 4958 /* This symbol requires a global offset table entry. */
4c52953f 4959 sec->has_toc_reloc = 1;
33c0ec9d
AM
4960 if (r_type == R_PPC64_GOT_TLSLD16
4961 || r_type == R_PPC64_GOT_TLSGD16
4962 || r_type == R_PPC64_GOT_TPREL16_DS
4963 || r_type == R_PPC64_GOT_DTPREL16_DS
4964 || r_type == R_PPC64_GOT16
4965 || r_type == R_PPC64_GOT16_DS)
4966 {
4967 htab->do_multi_toc = 1;
d77c8a4b 4968 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4969 }
4970
e717da7e
AM
4971 if (ppc64_elf_tdata (abfd)->got == NULL
4972 && !create_got_section (abfd, info))
0a1b45a2 4973 return false;
5bd4f169
AM
4974
4975 if (h != NULL)
4976 {
411e1bfb
AM
4977 struct ppc_link_hash_entry *eh;
4978 struct got_entry *ent;
65f38f15 4979
ed7007c1 4980 eh = ppc_elf_hash_entry (h);
411e1bfb 4981 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4982 if (ent->addend == rel->r_addend
e717da7e 4983 && ent->owner == abfd
411e1bfb
AM
4984 && ent->tls_type == tls_type)
4985 break;
4986 if (ent == NULL)
5bd4f169 4987 {
986f0783 4988 size_t amt = sizeof (*ent);
4ce794b7 4989 ent = bfd_alloc (abfd, amt);
411e1bfb 4990 if (ent == NULL)
0a1b45a2 4991 return false;
411e1bfb 4992 ent->next = eh->elf.got.glist;
133a1f60 4993 ent->addend = rel->r_addend;
e717da7e 4994 ent->owner = abfd;
411e1bfb 4995 ent->tls_type = tls_type;
0a1b45a2 4996 ent->is_indirect = false;
411e1bfb
AM
4997 ent->got.refcount = 0;
4998 eh->elf.got.glist = ent;
5bd4f169 4999 }
411e1bfb 5000 ent->got.refcount += 1;
e7b938ca 5001 eh->tls_mask |= tls_type;
5bd4f169 5002 }
411e1bfb
AM
5003 else
5004 /* This is a global offset table entry for a local symbol. */
5005 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5006 rel->r_addend, tls_type))
0a1b45a2 5007 return false;
5bd4f169
AM
5008 break;
5009
5bd4f169 5010 case R_PPC64_PLT16_HA:
65f38f15
AM
5011 case R_PPC64_PLT16_HI:
5012 case R_PPC64_PLT16_LO:
08be3224 5013 case R_PPC64_PLT16_LO_DS:
5663e321
AM
5014 case R_PPC64_PLT_PCREL34:
5015 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
5016 case R_PPC64_PLT32:
5017 case R_PPC64_PLT64:
cbf95972
AM
5018 /* This symbol requires a procedure linkage table entry. */
5019 plt_list = ifunc;
5020 if (h != NULL)
e054468f 5021 {
e054468f
AM
5022 h->needs_plt = 1;
5023 if (h->root.root.string[0] == '.'
5024 && h->root.root.string[1] != '\0')
ed7007c1
AM
5025 ppc_elf_hash_entry (h)->is_func = 1;
5026 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5027 plt_list = &h->plt.plist;
5028 }
5029 if (plt_list == NULL)
2d7ad24e 5030 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5031 rel->r_addend,
2d7ad24e 5032 NON_GOT | PLT_KEEP);
133a1f60 5033 if (!update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5034 return false;
5bd4f169
AM
5035 break;
5036
5037 /* The following relocations don't need to propagate the
5038 relocation if linking a shared object since they are
5039 section relative. */
5040 case R_PPC64_SECTOFF:
5041 case R_PPC64_SECTOFF_LO:
5042 case R_PPC64_SECTOFF_HI:
5043 case R_PPC64_SECTOFF_HA:
5044 case R_PPC64_SECTOFF_DS:
5045 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5046 case R_PPC64_DTPREL16:
5047 case R_PPC64_DTPREL16_LO:
5048 case R_PPC64_DTPREL16_HI:
5049 case R_PPC64_DTPREL16_HA:
5050 case R_PPC64_DTPREL16_DS:
5051 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5052 case R_PPC64_DTPREL16_HIGH:
5053 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5054 case R_PPC64_DTPREL16_HIGHER:
5055 case R_PPC64_DTPREL16_HIGHERA:
5056 case R_PPC64_DTPREL16_HIGHEST:
5057 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5058 break;
5059
ad8e1ba5 5060 /* Nor do these. */
25f23106
AM
5061 case R_PPC64_REL16:
5062 case R_PPC64_REL16_LO:
5063 case R_PPC64_REL16_HI:
5064 case R_PPC64_REL16_HA:
4a969973
AM
5065 case R_PPC64_REL16_HIGH:
5066 case R_PPC64_REL16_HIGHA:
5067 case R_PPC64_REL16_HIGHER:
5068 case R_PPC64_REL16_HIGHERA:
5069 case R_PPC64_REL16_HIGHEST:
5070 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
5071 case R_PPC64_REL16_HIGHER34:
5072 case R_PPC64_REL16_HIGHERA34:
5073 case R_PPC64_REL16_HIGHEST34:
5074 case R_PPC64_REL16_HIGHESTA34:
a680de9a 5075 case R_PPC64_REL16DX_HA:
25f23106
AM
5076 break;
5077
45965137
AM
5078 /* Not supported as a dynamic relocation. */
5079 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5080 if (bfd_link_pic (info))
45965137
AM
5081 {
5082 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5083 ppc_howto_init ();
695344c0 5084 /* xgettext:c-format */
174d0a74 5085 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5086 "in shared libraries and PIEs\n"),
45965137
AM
5087 abfd, sec, rel->r_offset,
5088 ppc64_elf_howto_table[r_type]->name);
5089 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5090 return false;
45965137
AM
5091 }
5092 break;
5093
ad8e1ba5 5094 case R_PPC64_TOC16:
33c0ec9d
AM
5095 case R_PPC64_TOC16_DS:
5096 htab->do_multi_toc = 1;
d77c8a4b 5097 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5098 /* Fall through. */
ad8e1ba5
AM
5099 case R_PPC64_TOC16_LO:
5100 case R_PPC64_TOC16_HI:
5101 case R_PPC64_TOC16_HA:
ad8e1ba5 5102 case R_PPC64_TOC16_LO_DS:
4c52953f 5103 sec->has_toc_reloc = 1;
ec73ddcd 5104 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
5105 {
5106 /* We may need a copy reloc. */
5107 h->non_got_ref = 1;
5108 /* Strongly prefer a copy reloc over a dynamic reloc.
5109 glibc ld.so as of 2019-08 will error out if one of
5110 these relocations is emitted. */
5111 h->needs_copy = 1;
5112 goto dodyn;
5113 }
ad8e1ba5
AM
5114 break;
5115
006589cf
AM
5116 /* Marker reloc. */
5117 case R_PPC64_ENTRY:
5118 break;
5119
5bd4f169
AM
5120 /* This relocation describes the C++ object vtable hierarchy.
5121 Reconstruct it for later use during GC. */
5122 case R_PPC64_GNU_VTINHERIT:
c152c796 5123 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
0a1b45a2 5124 return false;
5bd4f169
AM
5125 break;
5126
5127 /* This relocation describes which C++ vtable entries are actually
5128 used. Record for later use during GC. */
5129 case R_PPC64_GNU_VTENTRY:
a0ea3a14 5130 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
0a1b45a2 5131 return false;
5bd4f169
AM
5132 break;
5133
721956f4
AM
5134 case R_PPC64_REL14:
5135 case R_PPC64_REL14_BRTAKEN:
5136 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5137 {
5138 asection *dest = NULL;
5139
5140 /* Heuristic: If jumping outside our section, chances are
5141 we are going to need a stub. */
5142 if (h != NULL)
5143 {
5144 /* If the sym is weak it may be overridden later, so
5145 don't assume we know where a weak sym lives. */
5146 if (h->root.type == bfd_link_hash_defined)
5147 dest = h->root.u.def.section;
5148 }
5149 else
3a3a4c1f 5150 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
87d72d41 5151
220c76dd 5152 if (dest != sec)
7c8fe5c4 5153 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5154 }
3e04d765
AM
5155 goto rel24;
5156
5157 case R_PPC64_PLTCALL:
5663e321 5158 case R_PPC64_PLTCALL_NOTOC:
3e04d765 5159 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5160 /* Fall through. */
5161
5d1634d7 5162 case R_PPC64_REL24:
05d0e962 5163 case R_PPC64_REL24_NOTOC:
7aba54da 5164 case R_PPC64_REL24_P9NOTOC:
3e04d765 5165 rel24:
cbf95972
AM
5166 plt_list = ifunc;
5167 if (h != NULL)
5d1634d7 5168 {
e054468f
AM
5169 h->needs_plt = 1;
5170 if (h->root.root.string[0] == '.'
5171 && h->root.root.string[1] != '\0')
ed7007c1 5172 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 5173
3a71aa26 5174 if (h == tga || h == dottga)
cbf95972
AM
5175 {
5176 sec->has_tls_reloc = 1;
5177 if (rel != relocs
5178 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5179 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5180 /* We have a new-style __tls_get_addr call with
5181 a marker reloc. */
5182 ;
5183 else
5184 /* Mark this section as having an old-style call. */
9737e8af 5185 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5186 }
5187 plt_list = &h->plt.plist;
411e1bfb 5188 }
cbf95972
AM
5189
5190 /* We may need a .plt entry if the function this reloc
5191 refers to is in a shared lib. */
5192 if (plt_list
133a1f60 5193 && !update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5194 return false;
411e1bfb
AM
5195 break;
5196
cbf95972
AM
5197 case R_PPC64_ADDR14:
5198 case R_PPC64_ADDR14_BRNTAKEN:
5199 case R_PPC64_ADDR14_BRTAKEN:
5200 case R_PPC64_ADDR24:
5201 goto dodyn;
5202
411e1bfb
AM
5203 case R_PPC64_TPREL64:
5204 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5205 if (bfd_link_dll (info))
411e1bfb
AM
5206 info->flags |= DF_STATIC_TLS;
5207 goto dotlstoc;
5208
5209 case R_PPC64_DTPMOD64:
5210 if (rel + 1 < rel_end
5211 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5212 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5213 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5214 else
951fd09b 5215 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5216 goto dotlstoc;
5217
5218 case R_PPC64_DTPREL64:
5219 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5220 if (rel != relocs
5221 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5222 && rel[-1].r_offset == rel->r_offset - 8)
5223 /* This is the second reloc of a dtpmod, dtprel pair.
5224 Don't mark with TLS_DTPREL. */
5225 goto dodyn;
5226
5227 dotlstoc:
5228 sec->has_tls_reloc = 1;
5229 if (h != NULL)
ed7007c1 5230 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5231 else
5232 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5233 rel->r_addend, tls_type))
0a1b45a2 5234 return false;
411e1bfb 5235
7c8fe5c4 5236 ppc64_sec = ppc64_elf_section_data (sec);
6313825c 5237 if (ppc64_sec->sec_type == sec_normal)
411e1bfb 5238 {
3a71aa26
AM
5239 bfd_size_type amt;
5240
e7b938ca 5241 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5242 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5243 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5244 if (ppc64_sec->u.toc.symndx == NULL)
0a1b45a2 5245 return false;
3a71aa26
AM
5246 amt = sec->size * sizeof (bfd_vma) / 8;
5247 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5248 if (ppc64_sec->u.toc.add == NULL)
0a1b45a2 5249 return false;
7c8fe5c4 5250 ppc64_sec->sec_type = sec_toc;
411e1bfb 5251 }
6313825c
AM
5252 if (ppc64_sec->sec_type != sec_toc
5253 || rel->r_offset % 8 != 0)
5254 {
5255 info->callbacks->einfo (_("%H: %s reloc unsupported here\n"),
5256 abfd, sec, rel->r_offset,
5257 ppc64_elf_howto_table[r_type]->name);
5258 bfd_set_error (bfd_error_bad_value);
5259 return false;
5260 }
3a71aa26 5261 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5262 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5263
5264 /* Mark the second slot of a GD or LD entry.
5265 -1 to indicate GD and -2 to indicate LD. */
5266 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5267 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5268 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5269 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5270 goto dodyn;
5271
411e1bfb
AM
5272 case R_PPC64_TPREL16_HI:
5273 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5274 case R_PPC64_TPREL16_HIGH:
5275 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5276 case R_PPC64_TPREL16_HIGHER:
5277 case R_PPC64_TPREL16_HIGHERA:
5278 case R_PPC64_TPREL16_HIGHEST:
5279 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5280 sec->has_tls_reloc = 1;
5281 /* Fall through. */
c213164a 5282 case R_PPC64_TPREL34:
252dcdf4
AM
5283 case R_PPC64_TPREL16:
5284 case R_PPC64_TPREL16_DS:
5285 case R_PPC64_TPREL16_LO:
5286 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5287 if (bfd_link_dll (info))
5288 info->flags |= DF_STATIC_TLS;
5289 goto dodyn;
5d1634d7 5290
e86ce104 5291 case R_PPC64_ADDR64:
b9399fcf 5292 if (is_opd
1e2f5b6e 5293 && rel + 1 < rel_end
4ce794b7 5294 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5295 {
8387904d 5296 if (h != NULL)
ed7007c1 5297 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5298 }
e86ce104
AM
5299 /* Fall through. */
5300
65f38f15
AM
5301 case R_PPC64_ADDR16:
5302 case R_PPC64_ADDR16_DS:
5303 case R_PPC64_ADDR16_HA:
5304 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5305 case R_PPC64_ADDR16_HIGH:
5306 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5307 case R_PPC64_ADDR16_HIGHER:
5308 case R_PPC64_ADDR16_HIGHERA:
5309 case R_PPC64_ADDR16_HIGHEST:
5310 case R_PPC64_ADDR16_HIGHESTA:
5311 case R_PPC64_ADDR16_LO:
5312 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5313 case R_PPC64_D34:
5314 case R_PPC64_D34_LO:
5315 case R_PPC64_D34_HI30:
5316 case R_PPC64_D34_HA30:
5317 case R_PPC64_ADDR16_HIGHER34:
5318 case R_PPC64_ADDR16_HIGHERA34:
5319 case R_PPC64_ADDR16_HIGHEST34:
5320 case R_PPC64_ADDR16_HIGHESTA34:
5321 case R_PPC64_D28:
0e1862bb 5322 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5323 && rel->r_addend == 0)
5324 {
5325 /* We may need a .plt entry if this reloc refers to a
5326 function in a shared lib. */
5663e321 5327 if (!update_plt_info (abfd, &h->plt.plist, 0))
0a1b45a2 5328 return false;
a345bc8d
AM
5329 h->pointer_equality_needed = 1;
5330 }
5331 /* Fall through. */
5332
5333 case R_PPC64_REL30:
5334 case R_PPC64_REL32:
5335 case R_PPC64_REL64:
65f38f15 5336 case R_PPC64_ADDR32:
65f38f15
AM
5337 case R_PPC64_UADDR16:
5338 case R_PPC64_UADDR32:
5339 case R_PPC64_UADDR64:
5bd4f169 5340 case R_PPC64_TOC:
ec73ddcd 5341 if (h != NULL && bfd_link_executable (info))
81848ca0 5342 /* We may need a copy reloc. */
f5385ebf 5343 h->non_got_ref = 1;
81848ca0 5344
41bd81ab 5345 /* Don't propagate .opd relocs. */
b9399fcf 5346 if (NO_OPD_RELOCS && is_opd)
e86ce104 5347 break;
e86ce104 5348
c99d782d
AM
5349 /* Set up information for symbols that might need dynamic
5350 relocations. At this point in linking we have read all
5351 the input files and resolved most symbols, but have not
5352 yet decided whether symbols are dynamic or finalized
5353 symbol flags. In some cases we might be setting dynamic
5354 reloc info for symbols that do not end up needing such.
5355 That's OK, adjust_dynamic_symbol and allocate_dynrelocs
5356 work together with this code. */
411e1bfb 5357 dodyn:
ec73ddcd 5358 if ((h != NULL
c99d782d 5359 && !SYMBOL_REFERENCES_LOCAL (info, h))
ec73ddcd 5360 || (bfd_link_pic (info)
3a3a4c1f
AM
5361 && (h != NULL
5362 ? !bfd_is_abs_symbol (&h->root)
5363 : isym->st_shndx != SHN_ABS)
ec73ddcd 5364 && must_be_dyn_reloc (info, r_type))
0e1862bb 5365 || (!bfd_link_pic (info)
25f23106 5366 && ifunc != NULL))
5bd4f169 5367 {
65f38f15
AM
5368 /* We must copy these reloc types into the output file.
5369 Create a reloc section in dynobj and make room for
5370 this reloc. */
5bd4f169
AM
5371 if (sreloc == NULL)
5372 {
83bac4b0 5373 sreloc = _bfd_elf_make_dynamic_reloc_section
0a1b45a2 5374 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
65f38f15 5375
5bd4f169 5376 if (sreloc == NULL)
0a1b45a2 5377 return false;
5bd4f169
AM
5378 }
5379
65f38f15
AM
5380 /* If this is a global symbol, we count the number of
5381 relocations we need for this symbol. */
5382 if (h != NULL)
5383 {
1657026c
AM
5384 struct ppc_dyn_relocs *p;
5385 struct ppc_dyn_relocs **head;
19e08130 5386
1657026c 5387 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
5388 p = *head;
5389 if (p == NULL || p->sec != sec)
5390 {
5391 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5392 if (p == NULL)
0a1b45a2 5393 return false;
19e08130
AM
5394 p->next = *head;
5395 *head = p;
5396 p->sec = sec;
5397 p->count = 0;
5398 p->pc_count = 0;
1657026c 5399 p->rel_count = 0;
19e08130
AM
5400 }
5401 p->count += 1;
5402 if (!must_be_dyn_reloc (info, r_type))
5403 p->pc_count += 1;
1657026c
AM
5404 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5405 && rel->r_offset % 2 == 0
8ffb6df2 5406 && sec->alignment_power != 0)
1657026c 5407 p->rel_count += 1;
65f38f15
AM
5408 }
5409 else
5410 {
3a3a4c1f 5411 /* Track dynamic relocs needed for local syms too. */
1657026c
AM
5412 struct ppc_local_dyn_relocs *p;
5413 struct ppc_local_dyn_relocs **head;
0a1b45a2 5414 bool is_ifunc;
ec338859 5415 asection *s;
6edfbbad 5416 void *vpp;
ec338859 5417
87d72d41
AM
5418 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5419 if (s == NULL)
5420 s = sec;
5421
6edfbbad 5422 vpp = &elf_section_data (s)->local_dynrel;
1657026c 5423 head = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
5424 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5425 p = *head;
5426 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5427 p = p->next;
5428 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5429 {
5430 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5431 if (p == NULL)
0a1b45a2 5432 return false;
19e08130
AM
5433 p->next = *head;
5434 *head = p;
5435 p->sec = sec;
19e08130 5436 p->count = 0;
1657026c
AM
5437 p->rel_count = 0;
5438 p->ifunc = is_ifunc;
19e08130
AM
5439 }
5440 p->count += 1;
1657026c
AM
5441 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5442 && rel->r_offset % 2 == 0
8ffb6df2 5443 && sec->alignment_power != 0)
1657026c 5444 p->rel_count += 1;
ec338859 5445 }
65f38f15 5446 }
5bd4f169 5447 break;
65f38f15
AM
5448
5449 default:
96e0dda4 5450 break;
5bd4f169
AM
5451 }
5452 }
5453
0a1b45a2 5454 return true;
5bd4f169
AM
5455}
5456
ee67d69a
AM
5457/* Merge backend specific data from an object file to the output
5458 object file when linking. */
5459
0a1b45a2 5460static bool
50e03d47 5461ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5462{
50e03d47 5463 bfd *obfd = info->output_bfd;
ee67d69a
AM
5464 unsigned long iflags, oflags;
5465
5466 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
0a1b45a2 5467 return true;
ee67d69a
AM
5468
5469 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
0a1b45a2 5470 return true;
ee67d69a 5471
50e03d47 5472 if (!_bfd_generic_verify_endian_match (ibfd, info))
0a1b45a2 5473 return false;
ee67d69a
AM
5474
5475 iflags = elf_elfheader (ibfd)->e_flags;
5476 oflags = elf_elfheader (obfd)->e_flags;
5477
f6c7c3e8 5478 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5479 {
4eca0228 5480 _bfd_error_handler
695344c0 5481 /* xgettext:c-format */
871b3ab2 5482 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a 5483 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5484 return false;
ee67d69a 5485 }
f6c7c3e8 5486 else if (iflags != oflags && iflags != 0)
ee67d69a 5487 {
4eca0228 5488 _bfd_error_handler
695344c0 5489 /* xgettext:c-format */
871b3ab2 5490 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5491 ibfd, iflags, oflags);
5492 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5493 return false;
ee67d69a
AM
5494 }
5495
4a91d0ba 5496 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
0a1b45a2 5497 return false;
005d79fd 5498
ee67d69a 5499 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5500 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5501}
5502
0a1b45a2 5503static bool
ee67d69a
AM
5504ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5505{
5506 /* Print normal ELF private data. */
5507 _bfd_elf_print_private_bfd_data (abfd, ptr);
5508
5509 if (elf_elfheader (abfd)->e_flags != 0)
5510 {
5511 FILE *file = ptr;
5512
ee67d69a
AM
5513 fprintf (file, _("private flags = 0x%lx:"),
5514 elf_elfheader (abfd)->e_flags);
5515
5516 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5517 fprintf (file, _(" [abiv%ld]"),
5518 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5519 fputc ('\n', file);
5520 }
5521
0a1b45a2 5522 return true;
ee67d69a
AM
5523}
5524
8387904d 5525/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5526 of the code entry point, and its section, which must be in the same
5527 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5528
5529static bfd_vma
5530opd_entry_value (asection *opd_sec,
5531 bfd_vma offset,
5532 asection **code_sec,
aef36ac1 5533 bfd_vma *code_off,
0a1b45a2 5534 bool in_code_sec)
8387904d
AM
5535{
5536 bfd *opd_bfd = opd_sec->owner;
8860955f 5537 Elf_Internal_Rela *relocs;
8387904d 5538 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5539 bfd_vma val;
8387904d 5540
6313825c
AM
5541 if (!is_ppc64_elf (opd_bfd))
5542 return (bfd_vma) -1;
5543
5544 if (ppc64_elf_section_data (opd_sec)->sec_type == sec_normal)
5545 ppc64_elf_section_data (opd_sec)->sec_type = sec_opd;
5546 else if (ppc64_elf_section_data (opd_sec)->sec_type != sec_opd)
5547 return (bfd_vma) -1;
5548
9f296da3
AM
5549 /* No relocs implies we are linking a --just-symbols object, or looking
5550 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5551 if (opd_sec->reloc_count == 0)
5552 {
6313825c 5553 bfd_byte *contents = ppc64_elf_section_data (opd_sec)->u.opd.u.contents;
3b36f7e6 5554
729eabd5
AM
5555 if (contents == NULL)
5556 {
81ff113f
AM
5557 if ((opd_sec->flags & SEC_HAS_CONTENTS) == 0
5558 || !bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
729eabd5 5559 return (bfd_vma) -1;
6313825c 5560 ppc64_elf_section_data (opd_sec)->u.opd.u.contents = contents;
729eabd5 5561 }
ee1e4ede 5562
dbb3fbbb 5563 /* PR 17512: file: 64b9dfbb. */
451dfd38 5564 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5565 return (bfd_vma) -1;
5566
729eabd5 5567 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5568 if (code_sec != NULL)
5569 {
5570 asection *sec, *likely = NULL;
ee1e4ede 5571
aef36ac1 5572 if (in_code_sec)
4b85d634 5573 {
aef36ac1
AM
5574 sec = *code_sec;
5575 if (sec->vma <= val
5576 && val < sec->vma + sec->size)
5577 likely = sec;
5578 else
5579 val = -1;
5580 }
5581 else
5582 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5583 if (sec->vma <= val
5584 && (sec->flags & SEC_LOAD) != 0
5585 && (sec->flags & SEC_ALLOC) != 0)
5586 likely = sec;
5587 if (likely != NULL)
5588 {
5589 *code_sec = likely;
5590 if (code_off != NULL)
5591 *code_off = val - likely->vma;
4b85d634
AM
5592 }
5593 }
aef36ac1 5594 return val;
4b85d634
AM
5595 }
5596
6313825c 5597 relocs = ppc64_elf_section_data (opd_sec)->u.opd.u.relocs;
8860955f 5598 if (relocs == NULL)
0a1b45a2 5599 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
877a8638
NC
5600 /* PR 17512: file: df8e1fd6. */
5601 if (relocs == NULL)
5602 return (bfd_vma) -1;
645ea6a9 5603
8387904d 5604 /* Go find the opd reloc at the sym address. */
8860955f 5605 lo = relocs;
8387904d 5606 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5607 val = (bfd_vma) -1;
8387904d
AM
5608 while (lo < hi)
5609 {
5610 look = lo + (hi - lo) / 2;
5611 if (look->r_offset < offset)
5612 lo = look + 1;
5613 else if (look->r_offset > offset)
5614 hi = look;
5615 else
5616 {
0ffa91dd
NC
5617 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5618
8387904d
AM
5619 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5620 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5621 {
5622 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5623 asection *sec = NULL;
8387904d 5624
b53dfeb2
AM
5625 if (symndx >= symtab_hdr->sh_info
5626 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5627 {
5628 struct elf_link_hash_entry **sym_hashes;
5629 struct elf_link_hash_entry *rh;
5630
5631 sym_hashes = elf_sym_hashes (opd_bfd);
5632 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5633 if (rh != NULL)
5634 {
5635 rh = elf_follow_link (rh);
bb854a36
AM
5636 if (rh->root.type != bfd_link_hash_defined
5637 && rh->root.type != bfd_link_hash_defweak)
5638 break;
5639 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5640 {
bb854a36
AM
5641 val = rh->root.u.def.value;
5642 sec = rh->root.u.def.section;
b53dfeb2
AM
5643 }
5644 }
5645 }
5646
5647 if (sec == NULL)
5648 {
5649 Elf_Internal_Sym *sym;
5650
5651 if (symndx < symtab_hdr->sh_info)
5652 {
5653 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5654 if (sym == NULL)
5655 {
5656 size_t symcnt = symtab_hdr->sh_info;
5657 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5658 symcnt, 0,
5659 NULL, NULL, NULL);
5660 if (sym == NULL)
5661 break;
5662 symtab_hdr->contents = (bfd_byte *) sym;
5663 }
5664 sym += symndx;
128205bb
AM
5665 }
5666 else
5667 {
b53dfeb2
AM
5668 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5669 1, symndx,
5670 NULL, NULL, NULL);
128205bb
AM
5671 if (sym == NULL)
5672 break;
128205bb 5673 }
b53dfeb2
AM
5674 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5675 if (sec == NULL)
5676 break;
5677 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5678 val = sym->st_value;
8387904d 5679 }
b53dfeb2 5680
8387904d
AM
5681 val += look->r_addend;
5682 if (code_off != NULL)
5683 *code_off = val;
5684 if (code_sec != NULL)
aef36ac1
AM
5685 {
5686 if (in_code_sec && *code_sec != sec)
5687 return -1;
5688 else
5689 *code_sec = sec;
5690 }
b53dfeb2 5691 if (sec->output_section != NULL)
8387904d 5692 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5693 }
5694 break;
5695 }
5696 }
645ea6a9 5697
645ea6a9 5698 return val;
8387904d
AM
5699}
5700
aef36ac1
AM
5701/* If the ELF symbol SYM might be a function in SEC, return the
5702 function size and set *CODE_OFF to the function's entry point,
5703 otherwise return zero. */
9f296da3 5704
aef36ac1
AM
5705static bfd_size_type
5706ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5707 bfd_vma *code_off)
9f296da3 5708{
aef36ac1 5709 bfd_size_type size;
24aebc79 5710 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1
AM
5711
5712 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5713 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5714 return 0;
5715
24aebc79
NC
5716 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5717
5718 /* In theory we should check that the symbol's type satisfies
5719 _bfd_elf_is_function_type(), but there are some function-like
5720 symbols which would fail this test. (eg _start). Instead
5721 we check for hidden, local, notype symbols with zero size.
5722 This type of symbol is generated by the annobin plugin for gcc
5723 and clang, and should not be considered to be a function symbol. */
5724 if (size == 0
5725 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5726 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5727 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5728 return 0;
aef36ac1
AM
5729
5730 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5731 {
b07bca4e
AM
5732 struct _opd_sec_data *opd = get_opd_info (sym->section);
5733 bfd_vma symval = sym->value;
5734
5735 if (opd != NULL
5736 && opd->adjust != NULL
5737 && elf_section_data (sym->section)->relocs != NULL)
5738 {
5739 /* opd_entry_value will use cached relocs that have been
5740 adjusted, but with raw symbols. That means both local
5741 and global symbols need adjusting. */
5742 long adjust = opd->adjust[OPD_NDX (symval)];
5743 if (adjust == -1)
5744 return 0;
5745 symval += adjust;
5746 }
5747
5748 if (opd_entry_value (sym->section, symval,
0a1b45a2 5749 &sec, code_off, true) == (bfd_vma) -1)
aef36ac1
AM
5750 return 0;
5751 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5752 symbol. This size has nothing to do with the code size of the
5753 function, which is what we're supposed to return, but the
5754 code size isn't available without looking up the dot-sym.
5755 However, doing that would be a waste of time particularly
5756 since elf_find_function will look at the dot-sym anyway.
5757 Now, elf_find_function will keep the largest size of any
5758 function sym found at the code address of interest, so return
5759 1 here to avoid it incorrectly caching a larger function size
5760 for a small function. This does mean we return the wrong
5761 size for a new-ABI function of size 24, but all that does is
5762 disable caching for such functions. */
5763 if (size == 24)
5764 size = 1;
9f296da3 5765 }
aef36ac1
AM
5766 else
5767 {
5768 if (sym->section != sec)
5769 return 0;
5770 *code_off = sym->value;
5771 }
24aebc79
NC
5772
5773 /* Do not return 0 for the function's size. */
5774 return size ? size : 1;
9f296da3
AM
5775}
5776
f378ab09
AM
5777/* Return true if symbol is a strong function defined in an ELFv2
5778 object with st_other localentry bits of zero, ie. its local entry
5779 point coincides with its global entry point. */
5780
0a1b45a2 5781static bool
f378ab09
AM
5782is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5783{
5784 return (h != NULL
5785 && h->type == STT_FUNC
5786 && h->root.type == bfd_link_hash_defined
5787 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5788 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5789 && is_ppc64_elf (h->root.u.def.section->owner)
5790 && abiversion (h->root.u.def.section->owner) >= 2);
5791}
5792
854b41e7
AM
5793/* Return true if symbol is defined in a regular object file. */
5794
0a1b45a2 5795static bool
854b41e7
AM
5796is_static_defined (struct elf_link_hash_entry *h)
5797{
5798 return ((h->root.type == bfd_link_hash_defined
5799 || h->root.type == bfd_link_hash_defweak)
5800 && h->root.u.def.section != NULL
5801 && h->root.u.def.section->output_section != NULL);
5802}
5803
b31867b6
AM
5804/* If FDH is a function descriptor symbol, return the associated code
5805 entry symbol if it is defined. Return NULL otherwise. */
5806
5807static struct ppc_link_hash_entry *
5808defined_code_entry (struct ppc_link_hash_entry *fdh)
5809{
5810 if (fdh->is_func_descriptor)
5811 {
5812 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5813 if (fh->elf.root.type == bfd_link_hash_defined
5814 || fh->elf.root.type == bfd_link_hash_defweak)
5815 return fh;
5816 }
5817 return NULL;
5818}
5819
5820/* If FH is a function code entry symbol, return the associated
5821 function descriptor symbol if it is defined. Return NULL otherwise. */
5822
5823static struct ppc_link_hash_entry *
5824defined_func_desc (struct ppc_link_hash_entry *fh)
5825{
5826 if (fh->oh != NULL
5827 && fh->oh->is_func_descriptor)
5828 {
5829 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5830 if (fdh->elf.root.type == bfd_link_hash_defined
5831 || fdh->elf.root.type == bfd_link_hash_defweak)
5832 return fdh;
5833 }
5834 return NULL;
5835}
5836
ed7007c1
AM
5837/* Given H is a symbol that satisfies is_static_defined, return the
5838 value in the output file. */
5839
5840static bfd_vma
5841defined_sym_val (struct elf_link_hash_entry *h)
5842{
5843 return (h->root.u.def.section->output_section->vma
5844 + h->root.u.def.section->output_offset
5845 + h->root.u.def.value);
5846}
5847
5848/* Return true if H matches __tls_get_addr or one of its variants. */
5849
0a1b45a2 5850static bool
ed7007c1
AM
5851is_tls_get_addr (struct elf_link_hash_entry *h,
5852 struct ppc_link_hash_table *htab)
5853{
30845f11
AM
5854 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5855 || h == elf_hash_entry (htab->tga_desc_fd)
5856 || h == elf_hash_entry (htab->tls_get_addr)
5857 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5858}
5859
0a1b45a2 5860static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
8c5b4e52
AM
5861
5862/* Garbage collect sections, after first dealing with dot-symbols. */
5863
0a1b45a2 5864static bool
8c5b4e52
AM
5865ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5866{
5867 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5868
5869 if (htab != NULL && htab->need_func_desc_adj)
5870 {
5871 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5872 htab->need_func_desc_adj = 0;
5873 }
5874 return bfd_elf_gc_sections (abfd, info);
5875}
5876
74f0fb50
AM
5877/* Mark all our entry sym sections, both opd and code section. */
5878
5879static void
5880ppc64_elf_gc_keep (struct bfd_link_info *info)
5881{
5882 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5883 struct bfd_sym_chain *sym;
5884
4dfe6ac6
NC
5885 if (htab == NULL)
5886 return;
5887
74f0fb50
AM
5888 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5889 {
b31867b6 5890 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5891 asection *sec;
5892
ed7007c1 5893 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
0a1b45a2 5894 false, false, true));
74f0fb50
AM
5895 if (eh == NULL)
5896 continue;
5897 if (eh->elf.root.type != bfd_link_hash_defined
5898 && eh->elf.root.type != bfd_link_hash_defweak)
5899 continue;
5900
b31867b6
AM
5901 fh = defined_code_entry (eh);
5902 if (fh != NULL)
74f0fb50 5903 {
b31867b6 5904 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5905 sec->flags |= SEC_KEEP;
5906 }
5907 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5908 && opd_entry_value (eh->elf.root.u.def.section,
5909 eh->elf.root.u.def.value,
0a1b45a2 5910 &sec, NULL, false) != (bfd_vma) -1)
74f0fb50
AM
5911 sec->flags |= SEC_KEEP;
5912
5913 sec = eh->elf.root.u.def.section;
5914 sec->flags |= SEC_KEEP;
5915 }
5916}
5917
64d03ab5
AM
5918/* Mark sections containing dynamically referenced symbols. When
5919 building shared libraries, we must assume that any visible symbol is
5920 referenced. */
5921
0a1b45a2 5922static bool
64d03ab5
AM
5923ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5924{
5925 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5926 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5927 struct ppc_link_hash_entry *fdh;
b407645f 5928 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5929
64d03ab5 5930 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5931 fdh = defined_func_desc (eh);
5932 if (fdh != NULL)
5933 eh = fdh;
64d03ab5
AM
5934
5935 if ((eh->elf.root.type == bfd_link_hash_defined
5936 || eh->elf.root.type == bfd_link_hash_defweak)
b80e421f
AM
5937 && (!eh->elf.start_stop
5938 || eh->elf.root.ldscript_def
5939 || !info->start_stop_gc)
87e79a65 5940 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5941 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5942 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5943 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5944 && (!bfd_link_executable (info)
e278ae05 5945 || info->gc_keep_exported
b407645f
AM
5946 || info->export_dynamic
5947 || (eh->elf.dynamic
5948 && d != NULL
2cdcc330
AM
5949 && (*d->match) (&d->head, NULL,
5950 eh->elf.root.root.string)))
e278ae05 5951 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5952 || !bfd_hide_sym_by_version (info->version_info,
5953 eh->elf.root.root.string)))))
64d03ab5
AM
5954 {
5955 asection *code_sec;
b31867b6 5956 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5957
5958 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5959
5960 /* Function descriptor syms cause the associated
5961 function code sym section to be marked. */
b31867b6
AM
5962 fh = defined_code_entry (eh);
5963 if (fh != NULL)
5964 {
5965 code_sec = fh->elf.root.u.def.section;
5966 code_sec->flags |= SEC_KEEP;
5967 }
64d03ab5
AM
5968 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5969 && opd_entry_value (eh->elf.root.u.def.section,
5970 eh->elf.root.u.def.value,
0a1b45a2 5971 &code_sec, NULL, false) != (bfd_vma) -1)
64d03ab5
AM
5972 code_sec->flags |= SEC_KEEP;
5973 }
5974
0a1b45a2 5975 return true;
64d03ab5
AM
5976}
5977
5bd4f169
AM
5978/* Return the section that should be marked against GC for a given
5979 relocation. */
5980
5981static asection *
4ce794b7 5982ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5983 struct bfd_link_info *info,
4ce794b7
AM
5984 Elf_Internal_Rela *rel,
5985 struct elf_link_hash_entry *h,
5986 Elf_Internal_Sym *sym)
5bd4f169 5987{
ccfa59ea
AM
5988 asection *rsec;
5989
ccfa59ea
AM
5990 /* Syms return NULL if we're marking .opd, so we avoid marking all
5991 function sections, as all functions are referenced in .opd. */
5992 rsec = NULL;
5993 if (get_opd_info (sec) != NULL)
5994 return rsec;
1e2f5b6e 5995
5bd4f169
AM
5996 if (h != NULL)
5997 {
04c9666a 5998 enum elf_ppc64_reloc_type r_type;
b31867b6 5999 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6000
4ce794b7 6001 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6002 switch (r_type)
5bd4f169
AM
6003 {
6004 case R_PPC64_GNU_VTINHERIT:
6005 case R_PPC64_GNU_VTENTRY:
6006 break;
6007
6008 default:
6009 switch (h->root.type)
6010 {
6011 case bfd_link_hash_defined:
6012 case bfd_link_hash_defweak:
ed7007c1 6013 eh = ppc_elf_hash_entry (h);
b31867b6
AM
6014 fdh = defined_func_desc (eh);
6015 if (fdh != NULL)
8c5b4e52
AM
6016 {
6017 /* -mcall-aixdesc code references the dot-symbol on
6018 a call reloc. Mark the function descriptor too
6019 against garbage collection. */
6020 fdh->elf.mark = 1;
60d67dc8
AM
6021 if (fdh->elf.is_weakalias)
6022 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6023 eh = fdh;
6024 }
1e2f5b6e
AM
6025
6026 /* Function descriptor syms cause the associated
6027 function code sym section to be marked. */
b31867b6
AM
6028 fh = defined_code_entry (eh);
6029 if (fh != NULL)
ccfa59ea
AM
6030 {
6031 /* They also mark their opd section. */
74f0fb50 6032 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6033
b31867b6 6034 rsec = fh->elf.root.u.def.section;
ccfa59ea 6035 }
8387904d
AM
6036 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6037 && opd_entry_value (eh->elf.root.u.def.section,
6038 eh->elf.root.u.def.value,
0a1b45a2 6039 &rsec, NULL, false) != (bfd_vma) -1)
74f0fb50 6040 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6041 else
1e2f5b6e
AM
6042 rsec = h->root.u.def.section;
6043 break;
5bd4f169
AM
6044
6045 case bfd_link_hash_common:
1e2f5b6e
AM
6046 rsec = h->root.u.c.p->section;
6047 break;
5bd4f169
AM
6048
6049 default:
fb34365b 6050 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6051 }
6052 }
6053 }
6054 else
6055 {
74f0fb50 6056 struct _opd_sec_data *opd;
1e2f5b6e
AM
6057
6058 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6059 opd = get_opd_info (rsec);
6060 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6061 {
74f0fb50 6062 rsec->gc_mark = 1;
ccfa59ea 6063
51aecdc5 6064 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6065 }
5bd4f169
AM
6066 }
6067
1e2f5b6e 6068 return rsec;
5bd4f169
AM
6069}
6070
deb0e272
AM
6071/* The maximum size of .sfpr. */
6072#define SFPR_MAX (218*4)
6073
6074struct sfpr_def_parms
6075{
699733f6
AM
6076 const char name[12];
6077 unsigned char lo, hi;
2cdcc330
AM
6078 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6079 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
6080};
6081
a4b6fadd
AM
6082/* Auto-generate _save*, _rest* functions in .sfpr.
6083 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6084 instead. */
deb0e272 6085
0a1b45a2 6086static bool
a4b6fadd
AM
6087sfpr_define (struct bfd_link_info *info,
6088 const struct sfpr_def_parms *parm,
6089 asection *stub_sec)
deb0e272
AM
6090{
6091 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6092 unsigned int i;
6093 size_t len = strlen (parm->name);
0a1b45a2 6094 bool writing = false;
699733f6 6095 char sym[16];
deb0e272 6096
4dfe6ac6 6097 if (htab == NULL)
0a1b45a2 6098 return false;
4dfe6ac6 6099
deb0e272
AM
6100 memcpy (sym, parm->name, len);
6101 sym[len + 2] = 0;
6102
6103 for (i = parm->lo; i <= parm->hi; i++)
6104 {
a4b6fadd 6105 struct ppc_link_hash_entry *h;
deb0e272
AM
6106
6107 sym[len + 0] = i / 10 + '0';
6108 sym[len + 1] = i % 10 + '0';
ed7007c1 6109 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
0a1b45a2 6110 writing, true, true));
a4b6fadd 6111 if (stub_sec != NULL)
deb0e272 6112 {
a4b6fadd
AM
6113 if (h != NULL
6114 && h->elf.root.type == bfd_link_hash_defined
6115 && h->elf.root.u.def.section == htab->sfpr)
6116 {
6117 struct elf_link_hash_entry *s;
6118 char buf[32];
6119 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
0a1b45a2 6120 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
a4b6fadd 6121 if (s == NULL)
0a1b45a2 6122 return false;
779f2ae7 6123 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
6124 {
6125 s->root.type = bfd_link_hash_defined;
6126 s->root.u.def.section = stub_sec;
7dda8d3c 6127 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6128 + h->elf.root.u.def.value);
6129 s->ref_regular = 1;
6130 s->def_regular = 1;
6131 s->ref_regular_nonweak = 1;
6132 s->forced_local = 1;
6133 s->non_elf = 0;
6134 s->root.linker_def = 1;
6135 }
6136 }
6137 continue;
6138 }
6139 if (h != NULL)
6140 {
6141 h->save_res = 1;
6142 if (!h->elf.def_regular)
deb0e272 6143 {
a4b6fadd
AM
6144 h->elf.root.type = bfd_link_hash_defined;
6145 h->elf.root.u.def.section = htab->sfpr;
6146 h->elf.root.u.def.value = htab->sfpr->size;
6147 h->elf.type = STT_FUNC;
6148 h->elf.def_regular = 1;
b32547cd 6149 h->elf.non_elf = 0;
0a1b45a2
AM
6150 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6151 writing = true;
deb0e272 6152 if (htab->sfpr->contents == NULL)
a4b6fadd 6153 {
2cdcc330
AM
6154 htab->sfpr->contents
6155 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd 6156 if (htab->sfpr->contents == NULL)
0a1b45a2 6157 return false;
a4b6fadd 6158 }
deb0e272
AM
6159 }
6160 }
6161 if (writing)
6162 {
6163 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6164 if (i != parm->hi)
6165 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6166 else
6167 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6168 htab->sfpr->size = p - htab->sfpr->contents;
6169 }
6170 }
6171
0a1b45a2 6172 return true;
deb0e272
AM
6173}
6174
6175static bfd_byte *
6176savegpr0 (bfd *abfd, bfd_byte *p, int r)
6177{
6178 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6179 return p + 4;
6180}
6181
6182static bfd_byte *
6183savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6184{
6185 p = savegpr0 (abfd, p, r);
a078d95a 6186 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6187 p = p + 4;
6188 bfd_put_32 (abfd, BLR, p);
6189 return p + 4;
6190}
6191
6192static bfd_byte *
6193restgpr0 (bfd *abfd, bfd_byte *p, int r)
6194{
6195 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6196 return p + 4;
6197}
6198
6199static bfd_byte *
6200restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6201{
a078d95a 6202 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6203 p = p + 4;
6204 p = restgpr0 (abfd, p, r);
6205 bfd_put_32 (abfd, MTLR_R0, p);
6206 p = p + 4;
6207 if (r == 29)
6208 {
6209 p = restgpr0 (abfd, p, 30);
6210 p = restgpr0 (abfd, p, 31);
6211 }
6212 bfd_put_32 (abfd, BLR, p);
6213 return p + 4;
6214}
6215
6216static bfd_byte *
6217savegpr1 (bfd *abfd, bfd_byte *p, int r)
6218{
6219 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6220 return p + 4;
6221}
6222
6223static bfd_byte *
6224savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6225{
6226 p = savegpr1 (abfd, p, r);
6227 bfd_put_32 (abfd, BLR, p);
6228 return p + 4;
6229}
6230
6231static bfd_byte *
6232restgpr1 (bfd *abfd, bfd_byte *p, int r)
6233{
6234 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6235 return p + 4;
6236}
6237
6238static bfd_byte *
6239restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6240{
6241 p = restgpr1 (abfd, p, r);
6242 bfd_put_32 (abfd, BLR, p);
6243 return p + 4;
6244}
6245
6246static bfd_byte *
6247savefpr (bfd *abfd, bfd_byte *p, int r)
6248{
6249 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6250 return p + 4;
6251}
6252
6253static bfd_byte *
6254savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6255{
6256 p = savefpr (abfd, p, r);
a078d95a 6257 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6258 p = p + 4;
6259 bfd_put_32 (abfd, BLR, p);
6260 return p + 4;
6261}
6262
6263static bfd_byte *
6264restfpr (bfd *abfd, bfd_byte *p, int r)
6265{
6266 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6267 return p + 4;
6268}
6269
6270static bfd_byte *
6271restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6272{
a078d95a 6273 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6274 p = p + 4;
6275 p = restfpr (abfd, p, r);
6276 bfd_put_32 (abfd, MTLR_R0, p);
6277 p = p + 4;
6278 if (r == 29)
6279 {
6280 p = restfpr (abfd, p, 30);
6281 p = restfpr (abfd, p, 31);
6282 }
6283 bfd_put_32 (abfd, BLR, p);
6284 return p + 4;
6285}
6286
6287static bfd_byte *
6288savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6289{
6290 p = savefpr (abfd, p, r);
6291 bfd_put_32 (abfd, BLR, p);
6292 return p + 4;
6293}
6294
6295static bfd_byte *
6296restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6297{
6298 p = restfpr (abfd, p, r);
6299 bfd_put_32 (abfd, BLR, p);
6300 return p + 4;
6301}
6302
6303static bfd_byte *
6304savevr (bfd *abfd, bfd_byte *p, int r)
6305{
6306 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6307 p = p + 4;
6308 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6309 return p + 4;
6310}
6311
6312static bfd_byte *
6313savevr_tail (bfd *abfd, bfd_byte *p, int r)
6314{
6315 p = savevr (abfd, p, r);
6316 bfd_put_32 (abfd, BLR, p);
6317 return p + 4;
6318}
6319
6320static bfd_byte *
6321restvr (bfd *abfd, bfd_byte *p, int r)
6322{
6323 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6324 p = p + 4;
6325 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6326 return p + 4;
6327}
6328
6329static bfd_byte *
6330restvr_tail (bfd *abfd, bfd_byte *p, int r)
6331{
6332 p = restvr (abfd, p, r);
6333 bfd_put_32 (abfd, BLR, p);
6334 return p + 4;
6335}
6336
9e7028aa
AM
6337#define STDU_R1_0R1 0xf8210001
6338#define ADDI_R1_R1 0x38210000
6339
6340/* Emit prologue of wrapper preserving regs around a call to
6341 __tls_get_addr_opt. */
6342
6343static bfd_byte *
6344tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6345{
6346 unsigned int i;
6347
6348 bfd_put_32 (obfd, MFLR_R0, p);
6349 p += 4;
6350 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6351 p += 4;
6352
6353 if (htab->opd_abi)
6354 {
6355 for (i = 4; i < 12; i++)
6356 {
6357 bfd_put_32 (obfd,
6358 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6359 p += 4;
6360 }
6361 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6362 p += 4;
6363 }
6364 else
6365 {
6366 for (i = 4; i < 12; i++)
6367 {
6368 bfd_put_32 (obfd,
6369 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6370 p += 4;
6371 }
6372 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6373 p += 4;
6374 }
6375 return p;
6376}
6377
6378/* Emit epilogue of wrapper preserving regs around a call to
6379 __tls_get_addr_opt. */
6380
6381static bfd_byte *
6382tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6383{
6384 unsigned int i;
6385
6386 if (htab->opd_abi)
6387 {
6388 for (i = 4; i < 12; i++)
6389 {
6390 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6391 p += 4;
6392 }
6393 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6394 p += 4;
6395 }
6396 else
6397 {
6398 for (i = 4; i < 12; i++)
6399 {
6400 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6401 p += 4;
6402 }
6403 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6404 p += 4;
6405 }
6406 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6407 p += 4;
6408 bfd_put_32 (obfd, MTLR_R0, p);
6409 p += 4;
6410 bfd_put_32 (obfd, BLR, p);
6411 p += 4;
6412 return p;
6413}
6414
e86ce104
AM
6415/* Called via elf_link_hash_traverse to transfer dynamic linking
6416 information on function code symbol entries to their corresponding
a442059f
AM
6417 function descriptor symbol entries. Must not be called twice for
6418 any given code symbol. */
deb0e272 6419
0a1b45a2 6420static bool
4ce794b7 6421func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6422{
e86ce104 6423 struct bfd_link_info *info;
65f38f15 6424 struct ppc_link_hash_table *htab;
50bc7936
AM
6425 struct ppc_link_hash_entry *fh;
6426 struct ppc_link_hash_entry *fdh;
0a1b45a2 6427 bool force_local;
5bd4f169 6428
ed7007c1 6429 fh = ppc_elf_hash_entry (h);
50bc7936 6430 if (fh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 6431 return true;
e86ce104 6432
8c5b4e52 6433 if (!fh->is_func)
0a1b45a2 6434 return true;
8c5b4e52
AM
6435
6436 if (fh->elf.root.root.string[0] != '.'
6437 || fh->elf.root.root.string[1] == '\0')
0a1b45a2 6438 return true;
8c5b4e52 6439
4ce794b7 6440 info = inf;
65f38f15 6441 htab = ppc_hash_table (info);
4dfe6ac6 6442 if (htab == NULL)
0a1b45a2 6443 return false;
5bd4f169 6444
8c5b4e52
AM
6445 /* Find the corresponding function descriptor symbol. */
6446 fdh = lookup_fdh (fh, htab);
6447
c09bdfe5
AM
6448 /* Resolve undefined references to dot-symbols as the value
6449 in the function descriptor, if we have one in a regular object.
6450 This is to satisfy cases like ".quad .foo". Calls to functions
6451 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6452 if ((fh->elf.root.type == bfd_link_hash_undefined
6453 || fh->elf.root.type == bfd_link_hash_undefweak)
6454 && (fdh->elf.root.type == bfd_link_hash_defined
6455 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6456 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6457 && opd_entry_value (fdh->elf.root.u.def.section,
6458 fdh->elf.root.u.def.value,
c09bdfe5 6459 &fh->elf.root.u.def.section,
0a1b45a2 6460 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
c09bdfe5 6461 {
b31867b6 6462 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6463 fh->elf.forced_local = 1;
b31867b6
AM
6464 fh->elf.def_regular = fdh->elf.def_regular;
6465 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6466 }
6467
8c5b4e52
AM
6468 if (!fh->elf.dynamic)
6469 {
6470 struct plt_entry *ent;
5bd4f169 6471
8c5b4e52
AM
6472 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6473 if (ent->plt.refcount > 0)
6474 break;
6475 if (ent == NULL)
a442059f
AM
6476 {
6477 if (fdh != NULL && fdh->fake)
6478 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6479 return true;
6480 }
8c5b4e52 6481 }
5bd4f169 6482
8c5b4e52 6483 /* Create a descriptor as undefined if necessary. */
50bc7936 6484 if (fdh == NULL
0e1862bb 6485 && !bfd_link_executable (info)
50bc7936
AM
6486 && (fh->elf.root.type == bfd_link_hash_undefined
6487 || fh->elf.root.type == bfd_link_hash_undefweak))
6488 {
908b32fc 6489 fdh = make_fdh (info, fh);
bb700d78 6490 if (fdh == NULL)
0a1b45a2 6491 return false;
50bc7936 6492 }
648cca2c 6493
8c5b4e52 6494 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6495 if (fdh != NULL
6496 && fdh->fake
8c5b4e52
AM
6497 && (fh->elf.root.type == bfd_link_hash_defined
6498 || fh->elf.root.type == bfd_link_hash_defweak))
0a1b45a2 6499 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
908b32fc 6500
8c5b4e52
AM
6501 /* Transfer dynamic linking information to the function descriptor. */
6502 if (fdh != NULL)
6503 {
f5385ebf
AM
6504 fdh->elf.ref_regular |= fh->elf.ref_regular;
6505 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6506 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6507 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6508 fdh->elf.dynamic |= fh->elf.dynamic;
6509 fdh->elf.needs_plt |= (fh->elf.needs_plt
6510 || fh->elf.type == STT_FUNC
6511 || fh->elf.type == STT_GNU_IFUNC);
6512 move_plt_plist (fh, fdh);
6513
6514 if (!fdh->elf.forced_local
6515 && fh->elf.dynindx != -1)
6516 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 6517 return false;
e86ce104
AM
6518 }
6519
50bc7936
AM
6520 /* Now that the info is on the function descriptor, clear the
6521 function code sym info. Any function code syms for which we
6522 don't have a definition in a regular file, we force local.
6523 This prevents a shared library from exporting syms that have
6524 been imported from another library. Function code syms that
6525 are really in the library we must leave global to prevent the
6526 linker dragging in a definition from a static library. */
93f3fa99
AM
6527 force_local = (!fh->elf.def_regular
6528 || fdh == NULL
6529 || !fdh->elf.def_regular
6530 || fdh->elf.forced_local);
50bc7936
AM
6531 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6532
0a1b45a2 6533 return true;
e86ce104 6534}
40b8271b 6535
a4b6fadd
AM
6536static const struct sfpr_def_parms save_res_funcs[] =
6537 {
6538 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6539 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6540 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6541 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6542 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6543 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6544 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6545 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6546 { "._savef", 14, 31, savefpr, savefpr1_tail },
6547 { "._restf", 14, 31, restfpr, restfpr1_tail },
6548 { "_savevr_", 20, 31, savevr, savevr_tail },
6549 { "_restvr_", 20, 31, restvr, restvr_tail }
6550 };
6551
e86ce104 6552/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6553 this hook to a) run the edit functions in this file, b) provide
6554 some gcc support functions, and c) transfer dynamic linking
6555 information gathered so far on function code symbol entries, to
6556 their corresponding function descriptor symbol entries. */
deb0e272 6557
0a1b45a2 6558static bool
c9405344 6559ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6560{
6561 struct ppc_link_hash_table *htab;
6562
6563 htab = ppc_hash_table (info);
4dfe6ac6 6564 if (htab == NULL)
0a1b45a2 6565 return false;
4dfe6ac6 6566
c9405344
AM
6567 /* Call back into the linker, which then runs the edit functions. */
6568 htab->params->edit ();
6569
b32547cd
AM
6570 /* Provide any missing _save* and _rest* functions. */
6571 if (htab->sfpr != NULL)
6572 {
6573 unsigned int i;
6574
6575 htab->sfpr->size = 0;
6576 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6577 if (!sfpr_define (info, &save_res_funcs[i], NULL))
0a1b45a2 6578 return false;
b32547cd
AM
6579 if (htab->sfpr->size == 0)
6580 htab->sfpr->flags |= SEC_EXCLUDE;
6581 }
6582
6583 if (bfd_link_relocatable (info))
0a1b45a2 6584 return true;
b32547cd
AM
6585
6586 if (htab->elf.hgot != NULL)
dba6fa9b 6587 {
0a1b45a2 6588 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
dba6fa9b
AM
6589 /* Make .TOC. defined so as to prevent it being made dynamic.
6590 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6591 if (!htab->elf.hgot->def_regular
6592 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6593 {
6594 htab->elf.hgot->root.type = bfd_link_hash_defined;
6595 htab->elf.hgot->root.u.def.value = 0;
6596 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6597 htab->elf.hgot->def_regular = 1;
6598 htab->elf.hgot->root.linker_def = 1;
6599 }
dba6fa9b 6600 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6601 htab->elf.hgot->other
6602 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6603 }
c66bb0ee 6604
0a1b45a2 6605 return true;
e86ce104
AM
6606}
6607
d311bc8b 6608/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6609 aliases, that apply to read-only sections. Cannot be used after
6610 size_dynamic_sections. */
d311bc8b 6611
0a1b45a2 6612static bool
d311bc8b
AM
6613alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6614{
ed7007c1 6615 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6616 do
6617 {
5dbc8b37 6618 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
0a1b45a2 6619 return true;
ed7007c1 6620 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6621 }
6622 while (eh != NULL && &eh->elf != h);
d311bc8b 6623
0a1b45a2 6624 return false;
d311bc8b 6625}
8a2058b5 6626
8a9e8e72
AM
6627/* Return whether EH has pc-relative dynamic relocs. */
6628
0a1b45a2 6629static bool
8a9e8e72
AM
6630pc_dynrelocs (struct ppc_link_hash_entry *eh)
6631{
1657026c 6632 struct ppc_dyn_relocs *p;
8a9e8e72 6633
1657026c 6634 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72 6635 if (p->pc_count != 0)
0a1b45a2
AM
6636 return true;
6637 return false;
8a9e8e72
AM
6638}
6639
8a2058b5
AM
6640/* Return true if a global entry stub will be created for H. Valid
6641 for ELFv2 before plt entries have been allocated. */
6642
0a1b45a2 6643static bool
8a2058b5
AM
6644global_entry_stub (struct elf_link_hash_entry *h)
6645{
6646 struct plt_entry *pent;
6647
6648 if (!h->pointer_equality_needed
6649 || h->def_regular)
0a1b45a2 6650 return false;
8a2058b5
AM
6651
6652 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6653 if (pent->plt.refcount > 0
6654 && pent->addend == 0)
0a1b45a2 6655 return true;
8a2058b5 6656
0a1b45a2 6657 return false;
8a2058b5
AM
6658}
6659
e86ce104
AM
6660/* Adjust a symbol defined by a dynamic object and referenced by a
6661 regular object. The current definition is in some section of the
6662 dynamic object, but we're not including those sections. We have to
6663 change the definition to something the rest of the link can
6664 understand. */
6665
0a1b45a2 6666static bool
4ce794b7
AM
6667ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6668 struct elf_link_hash_entry *h)
e86ce104
AM
6669{
6670 struct ppc_link_hash_table *htab;
5474d94f 6671 asection *s, *srel;
e86ce104
AM
6672
6673 htab = ppc_hash_table (info);
4dfe6ac6 6674 if (htab == NULL)
0a1b45a2 6675 return false;
e86ce104
AM
6676
6677 /* Deal with function syms. */
6678 if (h->type == STT_FUNC
e054468f 6679 || h->type == STT_GNU_IFUNC
f5385ebf 6680 || h->needs_plt)
e86ce104 6681 {
0a1b45a2
AM
6682 bool local = (ppc_elf_hash_entry (h)->save_res
6683 || SYMBOL_CALLS_LOCAL (info, h)
6684 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
529fe20e
AM
6685 /* Discard dyn_relocs when non-pic if we've decided that a
6686 function symbol is local and not an ifunc. We keep dynamic
6687 relocs for ifuncs when local rather than always emitting a
6688 plt call stub for them and defining the symbol on the call
6689 stub. We can't do that for ELFv1 anyway (a function symbol
6690 is defined on a descriptor, not code) and it can be faster at
6691 run-time due to not needing to bounce through a stub. The
6692 dyn_relocs for ifuncs will be applied even in a static
6693 executable. */
6694 if (!bfd_link_pic (info)
6695 && h->type != STT_GNU_IFUNC
6696 && local)
190eb1dd 6697 h->dyn_relocs = NULL;
529fe20e 6698
e86ce104
AM
6699 /* Clear procedure linkage table information for any symbol that
6700 won't need a .plt entry. */
411e1bfb
AM
6701 struct plt_entry *ent;
6702 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6703 if (ent->plt.refcount > 0)
6704 break;
8387904d 6705 if (ent == NULL
2d7ad24e
AM
6706 || (h->type != STT_GNU_IFUNC
6707 && local
3e04d765 6708 && (htab->can_convert_all_inline_plt
ed7007c1 6709 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6710 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6711 {
411e1bfb 6712 h->plt.plist = NULL;
f5385ebf 6713 h->needs_plt = 0;
d1eca1e4 6714 h->pointer_equality_needed = 0;
40b8271b 6715 }
8a2058b5 6716 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6717 {
d1eca1e4
AM
6718 /* Taking a function's address in a read/write section
6719 doesn't require us to define the function symbol in the
6720 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6721 be used instead. The reason we prefer a few more dynamic
6722 relocs is that calling via a global entry stub costs a
6723 few more instructions, and pointer_equality_needed causes
6724 extra work in ld.so when resolving these symbols. */
529fe20e 6725 if (global_entry_stub (h))
d1eca1e4 6726 {
5dbc8b37 6727 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6728 {
6729 h->pointer_equality_needed = 0;
04383fd1
AM
6730 /* If we haven't seen a branch reloc and the symbol
6731 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6732 if (!h->needs_plt)
6733 h->plt.plist = NULL;
6734 }
6735 else if (!bfd_link_pic (info))
6736 /* We are going to be defining the function symbol on the
6737 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6738 h->dyn_relocs = NULL;
d1eca1e4
AM
6739 }
6740
3988aed5 6741 /* ELFv2 function symbols can't have copy relocs. */
0a1b45a2 6742 return true;
3988aed5
AM
6743 }
6744 else if (!h->needs_plt
5dbc8b37 6745 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6746 {
04383fd1
AM
6747 /* If we haven't seen a branch reloc and the symbol isn't an
6748 ifunc then we don't need a plt entry. */
3988aed5
AM
6749 h->plt.plist = NULL;
6750 h->pointer_equality_needed = 0;
0a1b45a2 6751 return true;
a345bc8d 6752 }
5bd4f169 6753 }
bbd7ec4a 6754 else
411e1bfb 6755 h->plt.plist = NULL;
5bd4f169
AM
6756
6757 /* If this is a weak symbol, and there is a real definition, the
6758 processor independent code will have arranged for us to see the
6759 real definition first, and we can just use the same value. */
60d67dc8 6760 if (h->is_weakalias)
5bd4f169 6761 {
60d67dc8
AM
6762 struct elf_link_hash_entry *def = weakdef (h);
6763 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6764 h->root.u.def.section = def->root.u.def.section;
6765 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6766 if (def->root.u.def.section == htab->elf.sdynbss
6767 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6768 h->dyn_relocs = NULL;
0a1b45a2 6769 return true;
5bd4f169
AM
6770 }
6771
5bd4f169
AM
6772 /* If we are creating a shared library, we must presume that the
6773 only references to the symbol are via the global offset table.
6774 For such cases we need not do anything here; the relocations will
6775 be handled correctly by relocate_section. */
ec73ddcd 6776 if (!bfd_link_executable (info))
0a1b45a2 6777 return true;
5bd4f169 6778
65f38f15
AM
6779 /* If there are no references to this symbol that do not use the
6780 GOT, we don't need to generate a copy reloc. */
f5385ebf 6781 if (!h->non_got_ref)
0a1b45a2 6782 return true;
65f38f15 6783
b186458a 6784 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6785 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6786
d93d1c80
AM
6787 /* If -z nocopyreloc was given, don't generate them either. */
6788 || info->nocopyreloc
a127494f 6789
dce2246a 6790 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6791 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6792 || (ELIMINATE_COPY_RELOCS
6793 && !h->needs_copy
6794 && !alias_readonly_dynrelocs (h))
65f38f15 6795
d93d1c80
AM
6796 /* Protected variables do not work with .dynbss. The copy in
6797 .dynbss won't be used by the shared library with the protected
6798 definition for the variable. Text relocations are preferable
6799 to an incorrect program. */
6800 || h->protected_def)
0a1b45a2 6801 return true;
a127494f 6802
e1c6cf61
AM
6803 if (h->type == STT_FUNC
6804 || h->type == STT_GNU_IFUNC)
97b639ba 6805 {
e1c6cf61
AM
6806 /* .dynbss copies of function symbols only work if we have
6807 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6808 use dot-symbols and set the function symbol size to the text
6809 size of the function rather than the size of the descriptor.
6810 That's wrong for copying a descriptor. */
ed7007c1 6811 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61 6812 || !(h->size == 24 || h->size == 16))
0a1b45a2 6813 return true;
e1c6cf61
AM
6814
6815 /* We should never get here, but unfortunately there are old
6816 versions of gcc (circa gcc-3.2) that improperly for the
6817 ELFv1 ABI put initialized function pointers, vtable refs and
6818 suchlike in read-only sections. Allow them to proceed, but
6819 warn that this might break at runtime. */
25f53a85 6820 info->callbacks->einfo
c1c8c1ef 6821 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6822 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6823 h->root.root.string);
6824 }
5d35169e
AM
6825
6826 /* This is a reference to a symbol defined by a dynamic object which
6827 is not a function. */
6828
5bd4f169
AM
6829 /* We must allocate the symbol in our .dynbss section, which will
6830 become part of the .bss section of the executable. There will be
6831 an entry for this symbol in the .dynsym section. The dynamic
6832 object will contain position independent code, so all references
6833 from the dynamic object to this symbol will go through the global
6834 offset table. The dynamic linker will use the .dynsym entry to
6835 determine the address it must put in the global offset table, so
6836 both the dynamic object and the regular object will refer to the
6837 same memory location for the variable. */
5474d94f
AM
6838 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6839 {
6840 s = htab->elf.sdynrelro;
6841 srel = htab->elf.sreldynrelro;
6842 }
6843 else
6844 {
6845 s = htab->elf.sdynbss;
6846 srel = htab->elf.srelbss;
6847 }
1d7e9d18 6848 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6849 {
4a7e5234
AM
6850 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6851 linker to copy the initial value out of the dynamic object
6852 and into the runtime process image. */
5474d94f 6853 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6854 h->needs_copy = 1;
5bd4f169
AM
6855 }
6856
529fe20e 6857 /* We no longer want dyn_relocs. */
190eb1dd 6858 h->dyn_relocs = NULL;
6cabe1ea 6859 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6860}
6861
e86ce104
AM
6862/* If given a function descriptor symbol, hide both the function code
6863 sym and the descriptor. */
6864static void
4ce794b7
AM
6865ppc64_elf_hide_symbol (struct bfd_link_info *info,
6866 struct elf_link_hash_entry *h,
0a1b45a2 6867 bool force_local)
e86ce104 6868{
34814b9f 6869 struct ppc_link_hash_entry *eh;
e86ce104
AM
6870 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6871
87469ba2
AM
6872 if (ppc_hash_table (info) == NULL)
6873 return;
6874
ed7007c1 6875 eh = ppc_elf_hash_entry (h);
34814b9f 6876 if (eh->is_func_descriptor)
e86ce104 6877 {
34814b9f 6878 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6879
721956f4 6880 if (fh == NULL)
d1329ca3
AM
6881 {
6882 const char *p, *q;
b8ac2841 6883 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6884 char save;
6885
6886 /* We aren't supposed to use alloca in BFD because on
6887 systems which do not have alloca the version in libiberty
6888 calls xmalloc, which might cause the program to crash
6889 when it runs out of memory. This function doesn't have a
6890 return status, so there's no way to gracefully return an
6891 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6892 accessed; It's either a string in an ELF string table,
6893 or allocated in an objalloc structure. */
d1329ca3 6894
34814b9f 6895 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6896 save = *p;
6897 *(char *) p = '.';
0a1b45a2
AM
6898 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6899 false, false));
d1329ca3
AM
6900 *(char *) p = save;
6901
6902 /* Unfortunately, if it so happens that the string we were
6903 looking for was allocated immediately before this string,
6904 then we overwrote the string terminator. That's the only
6905 reason the lookup should fail. */
6906 if (fh == NULL)
6907 {
34814b9f
AM
6908 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6909 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6910 --q, --p;
34814b9f 6911 if (q < eh->elf.root.root.string && *p == '.')
0a1b45a2
AM
6912 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6913 false, false));
d1329ca3
AM
6914 }
6915 if (fh != NULL)
6916 {
34814b9f
AM
6917 eh->oh = fh;
6918 fh->oh = eh;
d1329ca3
AM
6919 }
6920 }
e86ce104 6921 if (fh != NULL)
34814b9f 6922 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6923 }
6924}
6925
0a1b45a2 6926static bool
8843416a
AM
6927get_sym_h (struct elf_link_hash_entry **hp,
6928 Elf_Internal_Sym **symp,
6929 asection **symsecp,
f961d9dd 6930 unsigned char **tls_maskp,
8843416a
AM
6931 Elf_Internal_Sym **locsymsp,
6932 unsigned long r_symndx,
6933 bfd *ibfd)
411e1bfb 6934{
0ffa91dd 6935 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6936
6937 if (r_symndx >= symtab_hdr->sh_info)
6938 {
6939 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6940 struct elf_link_hash_entry *h;
6941
6942 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6943 h = elf_follow_link (h);
411e1bfb
AM
6944
6945 if (hp != NULL)
6946 *hp = h;
6947
6948 if (symp != NULL)
6949 *symp = NULL;
6950
6951 if (symsecp != NULL)
6952 {
6953 asection *symsec = NULL;
6954 if (h->root.type == bfd_link_hash_defined
6955 || h->root.type == bfd_link_hash_defweak)
6956 symsec = h->root.u.def.section;
6957 *symsecp = symsec;
6958 }
6959
e7b938ca 6960 if (tls_maskp != NULL)
ed7007c1 6961 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6962 }
6963 else
6964 {
6965 Elf_Internal_Sym *sym;
6966 Elf_Internal_Sym *locsyms = *locsymsp;
6967
6968 if (locsyms == NULL)
6969 {
6970 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6971 if (locsyms == NULL)
6972 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6973 symtab_hdr->sh_info,
6974 0, NULL, NULL, NULL);
6975 if (locsyms == NULL)
0a1b45a2 6976 return false;
411e1bfb
AM
6977 *locsymsp = locsyms;
6978 }
6979 sym = locsyms + r_symndx;
6980
6981 if (hp != NULL)
6982 *hp = NULL;
6983
6984 if (symp != NULL)
6985 *symp = sym;
6986
6987 if (symsecp != NULL)
cb33740c 6988 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6989
e7b938ca 6990 if (tls_maskp != NULL)
411e1bfb
AM
6991 {
6992 struct got_entry **lgot_ents;
f961d9dd 6993 unsigned char *tls_mask;
411e1bfb 6994
e7b938ca 6995 tls_mask = NULL;
411e1bfb
AM
6996 lgot_ents = elf_local_got_ents (ibfd);
6997 if (lgot_ents != NULL)
6998 {
e054468f
AM
6999 struct plt_entry **local_plt = (struct plt_entry **)
7000 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7001 unsigned char *lgot_masks = (unsigned char *)
e054468f 7002 (local_plt + symtab_hdr->sh_info);
e7b938ca 7003 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7004 }
e7b938ca 7005 *tls_maskp = tls_mask;
411e1bfb
AM
7006 }
7007 }
0a1b45a2 7008 return true;
411e1bfb
AM
7009}
7010
e7b938ca 7011/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7012 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7013 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7014
7015static int
f961d9dd 7016get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7017 unsigned long *toc_symndx,
7018 bfd_vma *toc_addend,
0d4792f7 7019 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7020 const Elf_Internal_Rela *rel,
7021 bfd *ibfd)
411e1bfb
AM
7022{
7023 unsigned long r_symndx;
0d4792f7 7024 int next_r;
411e1bfb
AM
7025 struct elf_link_hash_entry *h;
7026 Elf_Internal_Sym *sym;
7027 asection *sec;
7028 bfd_vma off;
7029
7030 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7031 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7032 return 0;
411e1bfb 7033
37da22e5
AM
7034 if ((*tls_maskp != NULL
7035 && (**tls_maskp & TLS_TLS) != 0
7036 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7037 || sec == NULL
6bee8834 7038 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7039 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7040 return 1;
411e1bfb
AM
7041
7042 /* Look inside a TOC section too. */
7043 if (h != NULL)
7044 {
7045 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7046 off = h->root.u.def.value;
7047 }
7048 else
7049 off = sym->st_value;
7050 off += rel->r_addend;
7051 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7052 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7053 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7054 if (toc_symndx != NULL)
7055 *toc_symndx = r_symndx;
3a71aa26
AM
7056 if (toc_addend != NULL)
7057 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7058 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7059 return 0;
854b41e7 7060 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7061 && (next_r == -1 || next_r == -2))
7062 return 1 - next_r;
951fd09b 7063 return 1;
411e1bfb
AM
7064}
7065
3b421ab3
AM
7066/* Find (or create) an entry in the tocsave hash table. */
7067
7068static struct tocsave_entry *
7069tocsave_find (struct ppc_link_hash_table *htab,
7070 enum insert_option insert,
7071 Elf_Internal_Sym **local_syms,
7072 const Elf_Internal_Rela *irela,
7073 bfd *ibfd)
7074{
7075 unsigned long r_indx;
7076 struct elf_link_hash_entry *h;
7077 Elf_Internal_Sym *sym;
7078 struct tocsave_entry ent, *p;
7079 hashval_t hash;
7080 struct tocsave_entry **slot;
7081
7082 r_indx = ELF64_R_SYM (irela->r_info);
7083 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7084 return NULL;
7085 if (ent.sec == NULL || ent.sec->output_section == NULL)
7086 {
4eca0228 7087 _bfd_error_handler
871b3ab2 7088 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7089 return NULL;
7090 }
7091
7092 if (h != NULL)
7093 ent.offset = h->root.u.def.value;
7094 else
7095 ent.offset = sym->st_value;
7096 ent.offset += irela->r_addend;
7097
7098 hash = tocsave_htab_hash (&ent);
7099 slot = ((struct tocsave_entry **)
7100 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7101 if (slot == NULL)
7102 return NULL;
7103
7104 if (*slot == NULL)
7105 {
7106 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7107 if (p == NULL)
7108 return NULL;
7109 *p = ent;
7110 *slot = p;
7111 }
7112 return *slot;
7113}
7114
754021d0 7115/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7116 code for the old ABI, these will already have been done. */
754021d0 7117
0a1b45a2 7118static bool
754021d0
AM
7119adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7120{
7121 struct ppc_link_hash_entry *eh;
7122 asection *sym_sec;
74f0fb50 7123 struct _opd_sec_data *opd;
754021d0
AM
7124
7125 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 7126 return true;
754021d0 7127
754021d0
AM
7128 if (h->root.type != bfd_link_hash_defined
7129 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 7130 return true;
754021d0 7131
ed7007c1 7132 eh = ppc_elf_hash_entry (h);
754021d0 7133 if (eh->adjust_done)
0a1b45a2 7134 return true;
754021d0
AM
7135
7136 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7137 opd = get_opd_info (sym_sec);
7138 if (opd != NULL && opd->adjust != NULL)
754021d0 7139 {
51aecdc5 7140 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7141 if (adjust == -1)
7142 {
7143 /* This entry has been deleted. */
b3fac117 7144 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7145 if (dsec == NULL)
7146 {
7147 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7148 if (discarded_section (dsec))
81688140 7149 {
b3fac117 7150 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7151 break;
7152 }
7153 }
4025353c 7154 eh->elf.root.u.def.value = 0;
81688140 7155 eh->elf.root.u.def.section = dsec;
4025353c
AM
7156 }
7157 else
7158 eh->elf.root.u.def.value += adjust;
754021d0
AM
7159 eh->adjust_done = 1;
7160 }
0a1b45a2 7161 return true;
754021d0
AM
7162}
7163
8c1d1bb8 7164/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7165 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7166 have already been determined. */
7167
0a1b45a2 7168static bool
1657026c 7169dec_dynrel_count (const Elf_Internal_Rela *rel,
8c1d1bb8
AM
7170 asection *sec,
7171 struct bfd_link_info *info,
7172 Elf_Internal_Sym **local_syms,
7173 struct elf_link_hash_entry *h,
19e08130 7174 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7175{
7176 enum elf_ppc64_reloc_type r_type;
19e08130 7177 asection *sym_sec = NULL;
8c1d1bb8
AM
7178
7179 /* Can this reloc be dynamic? This switch, and later tests here
7180 should be kept in sync with the code in check_relocs. */
1657026c 7181 r_type = ELF64_R_TYPE (rel->r_info);
8c1d1bb8
AM
7182 switch (r_type)
7183 {
7184 default:
0a1b45a2 7185 return true;
8c1d1bb8 7186
1bdd8fac
AM
7187 case R_PPC64_TOC16:
7188 case R_PPC64_TOC16_DS:
7189 case R_PPC64_TOC16_LO:
7190 case R_PPC64_TOC16_HI:
7191 case R_PPC64_TOC16_HA:
7192 case R_PPC64_TOC16_LO_DS:
7193 if (h == NULL)
0a1b45a2 7194 return true;
1bdd8fac
AM
7195 break;
7196
8c1d1bb8
AM
7197 case R_PPC64_TPREL16:
7198 case R_PPC64_TPREL16_LO:
7199 case R_PPC64_TPREL16_HI:
7200 case R_PPC64_TPREL16_HA:
7201 case R_PPC64_TPREL16_DS:
7202 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7203 case R_PPC64_TPREL16_HIGH:
7204 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7205 case R_PPC64_TPREL16_HIGHER:
7206 case R_PPC64_TPREL16_HIGHERA:
7207 case R_PPC64_TPREL16_HIGHEST:
7208 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7209 case R_PPC64_TPREL64:
c213164a 7210 case R_PPC64_TPREL34:
8c1d1bb8
AM
7211 case R_PPC64_DTPMOD64:
7212 case R_PPC64_DTPREL64:
7213 case R_PPC64_ADDR64:
7214 case R_PPC64_REL30:
7215 case R_PPC64_REL32:
7216 case R_PPC64_REL64:
7217 case R_PPC64_ADDR14:
7218 case R_PPC64_ADDR14_BRNTAKEN:
7219 case R_PPC64_ADDR14_BRTAKEN:
7220 case R_PPC64_ADDR16:
7221 case R_PPC64_ADDR16_DS:
7222 case R_PPC64_ADDR16_HA:
7223 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7224 case R_PPC64_ADDR16_HIGH:
7225 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7226 case R_PPC64_ADDR16_HIGHER:
7227 case R_PPC64_ADDR16_HIGHERA:
7228 case R_PPC64_ADDR16_HIGHEST:
7229 case R_PPC64_ADDR16_HIGHESTA:
7230 case R_PPC64_ADDR16_LO:
7231 case R_PPC64_ADDR16_LO_DS:
7232 case R_PPC64_ADDR24:
7233 case R_PPC64_ADDR32:
7234 case R_PPC64_UADDR16:
7235 case R_PPC64_UADDR32:
7236 case R_PPC64_UADDR64:
7237 case R_PPC64_TOC:
5663e321
AM
7238 case R_PPC64_D34:
7239 case R_PPC64_D34_LO:
7240 case R_PPC64_D34_HI30:
7241 case R_PPC64_D34_HA30:
7242 case R_PPC64_ADDR16_HIGHER34:
7243 case R_PPC64_ADDR16_HIGHERA34:
7244 case R_PPC64_ADDR16_HIGHEST34:
7245 case R_PPC64_ADDR16_HIGHESTA34:
7246 case R_PPC64_D28:
8c1d1bb8
AM
7247 break;
7248 }
7249
7250 if (local_syms != NULL)
7251 {
7252 unsigned long r_symndx;
8c1d1bb8
AM
7253 bfd *ibfd = sec->owner;
7254
1657026c 7255 r_symndx = ELF64_R_SYM (rel->r_info);
8c1d1bb8 7256 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
0a1b45a2 7257 return false;
8c1d1bb8
AM
7258 }
7259
ec73ddcd 7260 if ((h != NULL
c99d782d 7261 && !SYMBOL_REFERENCES_LOCAL (info, h))
ec73ddcd 7262 || (bfd_link_pic (info)
3a3a4c1f
AM
7263 && (h != NULL
7264 ? !bfd_is_abs_symbol (&h->root)
7265 : sym_sec != bfd_abs_section_ptr)
ec73ddcd
AM
7266 && must_be_dyn_reloc (info, r_type))
7267 || (!bfd_link_pic (info)
7268 && (h != NULL
7269 ? h->type == STT_GNU_IFUNC
7270 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7271 ;
7272 else
0a1b45a2 7273 return true;
8c1d1bb8
AM
7274
7275 if (h != NULL)
6edfbbad 7276 {
1657026c
AM
7277 struct ppc_dyn_relocs *p;
7278 struct ppc_dyn_relocs **pp;
7279 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
7280
7281 /* elf_gc_sweep may have already removed all dyn relocs associated
7282 with local syms for a given section. Also, symbol flags are
7283 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7284 report a dynreloc miscount. */
7285 if (*pp == NULL && info->gc_sections)
0a1b45a2 7286 return true;
19e08130
AM
7287
7288 while ((p = *pp) != NULL)
60124e18 7289 {
19e08130
AM
7290 if (p->sec == sec)
7291 {
7292 if (!must_be_dyn_reloc (info, r_type))
7293 p->pc_count -= 1;
1657026c
AM
7294 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7295 && rel->r_offset % 2 == 0
8ffb6df2 7296 && sec->alignment_power != 0)
1657026c 7297 p->rel_count -= 1;
19e08130
AM
7298 p->count -= 1;
7299 if (p->count == 0)
7300 *pp = p->next;
0a1b45a2 7301 return true;
19e08130
AM
7302 }
7303 pp = &p->next;
60124e18 7304 }
6edfbbad 7305 }
19e08130
AM
7306 else
7307 {
1657026c
AM
7308 struct ppc_local_dyn_relocs *p;
7309 struct ppc_local_dyn_relocs **pp;
19e08130 7310 void *vpp;
0a1b45a2 7311 bool is_ifunc;
8c1d1bb8 7312
19e08130
AM
7313 if (local_syms == NULL)
7314 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7315 if (sym_sec == NULL)
7316 sym_sec = sec;
c57da1a7 7317
19e08130 7318 vpp = &elf_section_data (sym_sec)->local_dynrel;
1657026c 7319 pp = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
7320
7321 if (*pp == NULL && info->gc_sections)
0a1b45a2 7322 return true;
19e08130
AM
7323
7324 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7325 while ((p = *pp) != NULL)
8c1d1bb8 7326 {
19e08130
AM
7327 if (p->sec == sec && p->ifunc == is_ifunc)
7328 {
1657026c
AM
7329 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7330 && rel->r_offset % 2 == 0
8ffb6df2 7331 && sec->alignment_power != 0)
1657026c 7332 p->rel_count -= 1;
19e08130
AM
7333 p->count -= 1;
7334 if (p->count == 0)
7335 *pp = p->next;
0a1b45a2 7336 return true;
19e08130
AM
7337 }
7338 pp = &p->next;
8c1d1bb8 7339 }
8c1d1bb8
AM
7340 }
7341
695344c0 7342 /* xgettext:c-format */
cf97bcb0
AM
7343 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7344 sec->owner, sec);
8c1d1bb8 7345 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7346 return false;
8c1d1bb8
AM
7347}
7348
754021d0
AM
7349/* Remove unused Official Procedure Descriptor entries. Currently we
7350 only remove those associated with functions in discarded link-once
7351 sections, or weakly defined functions that have been overridden. It
7352 would be possible to remove many more entries for statically linked
7353 applications. */
7354
0a1b45a2 7355bool
e7d1c40c 7356ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7357{
7358 bfd *ibfd;
0a1b45a2 7359 bool some_edited = false;
3f764659 7360 asection *need_pad = NULL;
e7d1c40c
AM
7361 struct ppc_link_hash_table *htab;
7362
7363 htab = ppc_hash_table (info);
7364 if (htab == NULL)
0a1b45a2 7365 return false;
1e2f5b6e 7366
c72f2fb2 7367 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7368 {
7369 asection *sec;
7370 Elf_Internal_Rela *relstart, *rel, *relend;
7371 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7372 Elf_Internal_Sym *local_syms;
74f0fb50 7373 struct _opd_sec_data *opd;
0a1b45a2 7374 bool need_edit, add_aux_fields, broken;
3f764659 7375 bfd_size_type cnt_16b = 0;
1e2f5b6e 7376
854b41e7
AM
7377 if (!is_ppc64_elf (ibfd))
7378 continue;
7379
1e2f5b6e 7380 sec = bfd_get_section_by_name (ibfd, ".opd");
81ff113f
AM
7381 if (sec == NULL
7382 || sec->size == 0
7383 || (sec->flags & SEC_HAS_CONTENTS) == 0)
1e2f5b6e
AM
7384 continue;
7385
dbaa2011 7386 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7387 continue;
7388
1e2f5b6e
AM
7389 if (sec->output_section == bfd_abs_section_ptr)
7390 continue;
7391
7392 /* Look through the section relocs. */
7393 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7394 continue;
7395
6cdc0ccc 7396 local_syms = NULL;
0ffa91dd 7397 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7398
7399 /* Read the relocations. */
4ce794b7 7400 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7401 info->keep_memory);
1e2f5b6e 7402 if (relstart == NULL)
0a1b45a2 7403 return false;
1e2f5b6e
AM
7404
7405 /* First run through the relocs to check they are sane, and to
7406 determine whether we need to edit this opd section. */
0a1b45a2
AM
7407 need_edit = false;
7408 broken = false;
3f764659 7409 need_pad = sec;
1e2f5b6e 7410 relend = relstart + sec->reloc_count;
50bc7936 7411 for (rel = relstart; rel < relend; )
1e2f5b6e 7412 {
04c9666a 7413 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7414 unsigned long r_symndx;
7415 asection *sym_sec;
7416 struct elf_link_hash_entry *h;
7417 Elf_Internal_Sym *sym;
51aecdc5 7418 bfd_vma offset;
1e2f5b6e 7419
51aecdc5 7420 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7421 only interested in the reloc pointing to a function entry
7422 point. */
51aecdc5
AM
7423 offset = rel->r_offset;
7424 if (rel + 1 == relend
7425 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7426 {
7427 /* If someone messes with .opd alignment then after a
7428 "ld -r" we might have padding in the middle of .opd.
7429 Also, there's nothing to prevent someone putting
7430 something silly in .opd with the assembler. No .opd
b34976b6 7431 optimization for them! */
3f764659 7432 broken_opd:
4eca0228 7433 _bfd_error_handler
871b3ab2 7434 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
0a1b45a2 7435 broken = true;
1e2f5b6e
AM
7436 break;
7437 }
7438
50bc7936
AM
7439 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7440 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7441 {
4eca0228 7442 _bfd_error_handler
695344c0 7443 /* xgettext:c-format */
871b3ab2 7444 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7445 ibfd, r_type);
0a1b45a2 7446 broken = true;
50bc7936
AM
7447 break;
7448 }
7449
1e2f5b6e 7450 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7451 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7452 r_symndx, ibfd))
50bc7936 7453 goto error_ret;
1e2f5b6e
AM
7454
7455 if (sym_sec == NULL || sym_sec->owner == NULL)
7456 {
411e1bfb
AM
7457 const char *sym_name;
7458 if (h != NULL)
7459 sym_name = h->root.root.string;
7460 else
26c61ae5
L
7461 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7462 sym_sec);
411e1bfb 7463
4eca0228 7464 _bfd_error_handler
695344c0 7465 /* xgettext:c-format */
871b3ab2 7466 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7467 ibfd, sym_name);
0a1b45a2 7468 broken = true;
1e2f5b6e
AM
7469 break;
7470 }
7471
51020317
AM
7472 /* opd entries are always for functions defined in the
7473 current input bfd. If the symbol isn't defined in the
7474 input bfd, then we won't be using the function in this
7475 bfd; It must be defined in a linkonce section in another
7476 bfd, or is weak. It's also possible that we are
7477 discarding the function due to a linker script /DISCARD/,
7478 which we test for via the output_section. */
7479 if (sym_sec->owner != ibfd
7480 || sym_sec->output_section == bfd_abs_section_ptr)
0a1b45a2 7481 need_edit = true;
1e2f5b6e 7482
50bc7936 7483 rel += 2;
51aecdc5
AM
7484 if (rel + 1 == relend
7485 || (rel + 2 < relend
7486 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7487 ++rel;
7488
7489 if (rel == relend)
3f764659
JJ
7490 {
7491 if (sec->size == offset + 24)
7492 {
7493 need_pad = NULL;
7494 break;
7495 }
51aecdc5 7496 if (sec->size == offset + 16)
3f764659
JJ
7497 {
7498 cnt_16b++;
7499 break;
7500 }
7501 goto broken_opd;
7502 }
3f764659
JJ
7503 else if (rel + 1 < relend
7504 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7505 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7506 {
51aecdc5
AM
7507 if (rel[0].r_offset == offset + 16)
7508 cnt_16b++;
7509 else if (rel[0].r_offset != offset + 24)
7510 goto broken_opd;
3f764659
JJ
7511 }
7512 else
7513 goto broken_opd;
1e2f5b6e
AM
7514 }
7515
e7d1c40c 7516 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7517
51aecdc5 7518 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7519 {
7520 Elf_Internal_Rela *write_rel;
d4730f92 7521 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7522 bfd_byte *rptr, *wptr;
983bddc8 7523 bfd_byte *new_contents;
74f0fb50
AM
7524 bfd_size_type amt;
7525
983bddc8 7526 new_contents = NULL;
51aecdc5 7527 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7528 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7529 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50 7530 if (opd->adjust == NULL)
0a1b45a2 7531 return false;
1e2f5b6e
AM
7532
7533 /* This seems a waste of time as input .opd sections are all
7534 zeros as generated by gcc, but I suppose there's no reason
7535 this will always be so. We might start putting something in
7536 the third word of .opd entries. */
7537 if ((sec->flags & SEC_IN_MEMORY) == 0)
7538 {
eea6121a
AM
7539 bfd_byte *loc;
7540 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7541 {
c9594989 7542 free (loc);
50bc7936 7543 error_ret:
c9594989 7544 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7545 free (local_syms);
6cdc0ccc
AM
7546 if (elf_section_data (sec)->relocs != relstart)
7547 free (relstart);
0a1b45a2 7548 return false;
6cdc0ccc 7549 }
1e2f5b6e
AM
7550 sec->contents = loc;
7551 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7552 }
7553
7554 elf_section_data (sec)->relocs = relstart;
7555
3f764659 7556 new_contents = sec->contents;
3f764659
JJ
7557 if (add_aux_fields)
7558 {
7559 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7560 if (new_contents == NULL)
0a1b45a2 7561 return false;
51aecdc5 7562 need_pad = NULL;
3f764659 7563 }
b4f4e59f
AM
7564 wptr = new_contents;
7565 rptr = sec->contents;
1e2f5b6e 7566 write_rel = relstart;
51aecdc5 7567 for (rel = relstart; rel < relend; )
1e2f5b6e 7568 {
50bc7936
AM
7569 unsigned long r_symndx;
7570 asection *sym_sec;
7571 struct elf_link_hash_entry *h;
51aecdc5 7572 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7573 Elf_Internal_Sym *sym;
51aecdc5
AM
7574 long opd_ent_size;
7575 Elf_Internal_Rela *next_rel;
0a1b45a2 7576 bool skip;
50bc7936
AM
7577
7578 r_symndx = ELF64_R_SYM (rel->r_info);
7579 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7580 r_symndx, ibfd))
50bc7936
AM
7581 goto error_ret;
7582
51aecdc5
AM
7583 next_rel = rel + 2;
7584 if (next_rel + 1 == relend
7585 || (next_rel + 2 < relend
7586 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7587 ++next_rel;
7588
7589 /* See if the .opd entry is full 24 byte or
7590 16 byte (with fd_aux entry overlapped with next
7591 fd_func). */
7592 opd_ent_size = 24;
7593 if (next_rel == relend)
1e2f5b6e 7594 {
51aecdc5 7595 if (sec->size == rel->r_offset + 16)
3f764659 7596 opd_ent_size = 16;
51aecdc5
AM
7597 }
7598 else if (next_rel->r_offset == rel->r_offset + 16)
7599 opd_ent_size = 16;
3f764659 7600
51aecdc5
AM
7601 if (h != NULL
7602 && h->root.root.string[0] == '.')
7603 {
ed7007c1 7604 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7605 if (fdh != NULL)
7606 {
7607 fdh = ppc_follow_link (fdh);
7608 if (fdh->elf.root.type != bfd_link_hash_defined
7609 && fdh->elf.root.type != bfd_link_hash_defweak)
7610 fdh = NULL;
7611 }
51aecdc5 7612 }
1e2f5b6e 7613
51aecdc5
AM
7614 skip = (sym_sec->owner != ibfd
7615 || sym_sec->output_section == bfd_abs_section_ptr);
7616 if (skip)
7617 {
7618 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7619 {
51aecdc5
AM
7620 /* Arrange for the function descriptor sym
7621 to be dropped. */
7622 fdh->elf.root.u.def.value = 0;
7623 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7624 }
51aecdc5 7625 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7626
0e1862bb 7627 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7628 rel = next_rel;
7629 else
7630 while (1)
7631 {
1657026c 7632 if (!dec_dynrel_count (rel, sec, info,
51aecdc5
AM
7633 NULL, h, sym))
7634 goto error_ret;
754021d0 7635
51aecdc5
AM
7636 if (++rel == next_rel)
7637 break;
1e2f5b6e 7638
51aecdc5
AM
7639 r_symndx = ELF64_R_SYM (rel->r_info);
7640 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7641 r_symndx, ibfd))
7642 goto error_ret;
7643 }
50bc7936
AM
7644 }
7645 else
1e2f5b6e 7646 {
51aecdc5
AM
7647 /* We'll be keeping this opd entry. */
7648 long adjust;
7649
7650 if (fdh != NULL)
7651 {
7652 /* Redefine the function descriptor symbol to
7653 this location in the opd section. It is
7654 necessary to update the value here rather
7655 than using an array of adjustments as we do
7656 for local symbols, because various places
7657 in the generic ELF code use the value
7658 stored in u.def.value. */
7659 fdh->elf.root.u.def.value = wptr - new_contents;
7660 fdh->adjust_done = 1;
7661 }
7662
7663 /* Local syms are a bit tricky. We could
7664 tweak them as they can be cached, but
7665 we'd need to look through the local syms
7666 for the function descriptor sym which we
7667 don't have at the moment. So keep an
7668 array of adjustments. */
7669 adjust = (wptr - new_contents) - (rptr - sec->contents);
7670 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7671
7672 if (wptr != rptr)
7673 memcpy (wptr, rptr, opd_ent_size);
7674 wptr += opd_ent_size;
7675 if (add_aux_fields && opd_ent_size == 16)
7676 {
7677 memset (wptr, '\0', 8);
7678 wptr += 8;
7679 }
7680
50bc7936 7681 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7682 new opd entries. */
7683 for ( ; rel != next_rel; ++rel)
7684 {
7685 rel->r_offset += adjust;
7686 if (write_rel != rel)
7687 memcpy (write_rel, rel, sizeof (*rel));
7688 ++write_rel;
7689 }
1e2f5b6e 7690 }
51aecdc5
AM
7691
7692 rptr += opd_ent_size;
1e2f5b6e
AM
7693 }
7694
3f764659 7695 sec->size = wptr - new_contents;
1e2f5b6e 7696 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7697 if (add_aux_fields)
7698 {
7699 free (sec->contents);
7700 sec->contents = new_contents;
7701 }
7702
05bf9422 7703 /* Fudge the header size too, as this is used later in
cdcf6e38 7704 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7705 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7706 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
0a1b45a2 7707 some_edited = true;
1e2f5b6e 7708 }
6cdc0ccc 7709 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7710 free (relstart);
6cdc0ccc 7711
411e1bfb
AM
7712 if (local_syms != NULL
7713 && symtab_hdr->contents != (unsigned char *) local_syms)
7714 {
7715 if (!info->keep_memory)
7716 free (local_syms);
7717 else
7718 symtab_hdr->contents = (unsigned char *) local_syms;
7719 }
7720 }
7721
754021d0
AM
7722 if (some_edited)
7723 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7724
3f764659
JJ
7725 /* If we are doing a final link and the last .opd entry is just 16 byte
7726 long, add a 8 byte padding after it. */
0e1862bb 7727 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7728 {
7729 bfd_byte *p;
7730
7731 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7732 {
7733 BFD_ASSERT (need_pad->size > 0);
7734
7735 p = bfd_malloc (need_pad->size + 8);
7736 if (p == NULL)
0a1b45a2 7737 return false;
699733f6 7738
2cdcc330
AM
7739 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7740 p, 0, need_pad->size))
0a1b45a2 7741 return false;
3f764659
JJ
7742
7743 need_pad->contents = p;
7744 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7745 }
7746 else
7747 {
7748 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7749 if (p == NULL)
0a1b45a2 7750 return false;
3f764659
JJ
7751
7752 need_pad->contents = p;
7753 }
7754
7755 memset (need_pad->contents + need_pad->size, 0, 8);
7756 need_pad->size += 8;
7757 }
7758
0a1b45a2 7759 return true;
411e1bfb
AM
7760}
7761
3e04d765
AM
7762/* Analyze inline PLT call relocations to see whether calls to locally
7763 defined functions can be converted to direct calls. */
7764
0a1b45a2 7765bool
3e04d765
AM
7766ppc64_elf_inline_plt (struct bfd_link_info *info)
7767{
7768 struct ppc_link_hash_table *htab;
7769 bfd *ibfd;
7770 asection *sec;
7771 bfd_vma low_vma, high_vma, limit;
7772
7773 htab = ppc_hash_table (info);
7774 if (htab == NULL)
0a1b45a2 7775 return false;
3e04d765
AM
7776
7777 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7778 reduced somewhat to cater for possible stubs that might be added
7779 between the call and its destination. */
7780 if (htab->params->group_size < 0)
7781 {
7782 limit = -htab->params->group_size;
7783 if (limit == 1)
7784 limit = 0x1e00000;
7785 }
7786 else
7787 {
7788 limit = htab->params->group_size;
7789 if (limit == 1)
7790 limit = 0x1c00000;
7791 }
7792
7793 low_vma = -1;
7794 high_vma = 0;
7795 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7796 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7797 {
7798 if (low_vma > sec->vma)
7799 low_vma = sec->vma;
7800 if (high_vma < sec->vma + sec->size)
7801 high_vma = sec->vma + sec->size;
7802 }
7803
7804 /* If a "bl" can reach anywhere in local code sections, then we can
7805 convert all inline PLT sequences to direct calls when the symbol
7806 is local. */
7807 if (high_vma - low_vma < limit)
7808 {
7809 htab->can_convert_all_inline_plt = 1;
0a1b45a2 7810 return true;
3e04d765
AM
7811 }
7812
7813 /* Otherwise, go looking through relocs for cases where a direct
7814 call won't reach. Mark the symbol on any such reloc to disable
7815 the optimization and keep the PLT entry as it seems likely that
7816 this will be better than creating trampolines. Note that this
7817 will disable the optimization for all inline PLT calls to a
7818 particular symbol, not just those that won't reach. The
7819 difficulty in doing a more precise optimization is that the
7820 linker needs to make a decision depending on whether a
7821 particular R_PPC64_PLTCALL insn can be turned into a direct
7822 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7823 the sequence, and there is nothing that ties those relocs
7824 together except their symbol. */
7825
7826 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7827 {
7828 Elf_Internal_Shdr *symtab_hdr;
7829 Elf_Internal_Sym *local_syms;
7830
7831 if (!is_ppc64_elf (ibfd))
7832 continue;
7833
7834 local_syms = NULL;
7835 symtab_hdr = &elf_symtab_hdr (ibfd);
7836
7837 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7838 if (ppc64_elf_section_data (sec)->has_pltcall
7839 && !bfd_is_abs_section (sec->output_section))
7840 {
7841 Elf_Internal_Rela *relstart, *rel, *relend;
7842
7843 /* Read the relocations. */
7844 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7845 info->keep_memory);
7846 if (relstart == NULL)
0a1b45a2 7847 return false;
3e04d765
AM
7848
7849 relend = relstart + sec->reloc_count;
435edf0b 7850 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7851 {
7852 enum elf_ppc64_reloc_type r_type;
7853 unsigned long r_symndx;
7854 asection *sym_sec;
7855 struct elf_link_hash_entry *h;
7856 Elf_Internal_Sym *sym;
7857 unsigned char *tls_maskp;
7858
7859 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7860 if (r_type != R_PPC64_PLTCALL
7861 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7862 continue;
7863
7864 r_symndx = ELF64_R_SYM (rel->r_info);
7865 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7866 r_symndx, ibfd))
7867 {
7868 if (elf_section_data (sec)->relocs != relstart)
7869 free (relstart);
c9594989 7870 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765 7871 free (local_syms);
0a1b45a2 7872 return false;
3e04d765
AM
7873 }
7874
7875 if (sym_sec != NULL && sym_sec->output_section != NULL)
7876 {
7877 bfd_vma from, to;
7878 if (h != NULL)
7879 to = h->root.u.def.value;
7880 else
7881 to = sym->st_value;
7882 to += (rel->r_addend
7883 + sym_sec->output_offset
7884 + sym_sec->output_section->vma);
7885 from = (rel->r_offset
7886 + sec->output_offset
7887 + sec->output_section->vma);
5663e321
AM
7888 if (to - from + limit < 2 * limit
7889 && !(r_type == R_PPC64_PLTCALL_NOTOC
7890 && (((h ? h->other : sym->st_other)
7891 & STO_PPC64_LOCAL_MASK)
4a4e7361 7892 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7893 *tls_maskp &= ~PLT_KEEP;
7894 }
7895 }
7896 if (elf_section_data (sec)->relocs != relstart)
7897 free (relstart);
7898 }
7899
7900 if (local_syms != NULL
7901 && symtab_hdr->contents != (unsigned char *) local_syms)
7902 {
7903 if (!info->keep_memory)
7904 free (local_syms);
7905 else
7906 symtab_hdr->contents = (unsigned char *) local_syms;
7907 }
7908 }
7909
0a1b45a2 7910 return true;
3e04d765
AM
7911}
7912
c9405344
AM
7913/* Set htab->tls_get_addr and various other info specific to TLS.
7914 This needs to run before dynamic symbols are processed in
7915 bfd_elf_size_dynamic_sections. */
411e1bfb 7916
0a1b45a2 7917bool
e7d1c40c 7918ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7919{
411e1bfb 7920 struct ppc_link_hash_table *htab;
9e7028aa 7921 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7922
411e1bfb 7923 htab = ppc_hash_table (info);
4dfe6ac6 7924 if (htab == NULL)
0a1b45a2 7925 return false;
4dfe6ac6 7926
a442059f
AM
7927 /* Move dynamic linking info to the function descriptor sym. */
7928 if (htab->need_func_desc_adj)
7929 {
7930 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7931 htab->need_func_desc_adj = 0;
7932 }
7933
ee67d69a
AM
7934 if (abiversion (info->output_bfd) == 1)
7935 htab->opd_abi = 1;
7936
e7d1c40c 7937 if (htab->params->no_multi_toc)
33c0ec9d
AM
7938 htab->do_multi_toc = 0;
7939 else if (!htab->do_multi_toc)
e7d1c40c 7940 htab->params->no_multi_toc = 1;
33c0ec9d 7941
8b5f1ed8
AM
7942 /* Default to --no-plt-localentry, as this option can cause problems
7943 with symbol interposition. For example, glibc libpthread.so and
7944 libc.so duplicate many pthread symbols, with a fallback
7945 implementation in libc.so. In some cases the fallback does more
7946 work than the pthread implementation. __pthread_condattr_destroy
7947 is one such symbol: the libpthread.so implementation is
7948 localentry:0 while the libc.so implementation is localentry:8.
7949 An app that "cleverly" uses dlopen to only load necessary
7950 libraries at runtime may omit loading libpthread.so when not
7951 running multi-threaded, which then results in the libc.so
7952 fallback symbols being used and ld.so complaining. Now there
7953 are workarounds in ld (see non_zero_localentry) to detect the
7954 pthread situation, but that may not be the only case where
7955 --plt-localentry can cause trouble. */
f378ab09 7956 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7957 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7958 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7959 {
7960 /* The issue is that __glink_PLTresolve saves r2, which is done
7961 because glibc ld.so _dl_runtime_resolve restores r2 to support
7962 a glibc plt call optimisation where global entry code is
7963 skipped on calls that resolve to the same binary. The
7964 __glink_PLTresolve save of r2 is incompatible with code
7965 making tail calls, because the tail call might go via the
7966 resolver and thus overwrite the proper saved r2. */
7967 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7968 "power10 pc-relative code"));
7969 htab->params->plt_localentry0 = 0;
7970 }
d44c746a
AM
7971 if (htab->params->plt_localentry0
7972 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
0a1b45a2 7973 false, false, false) == NULL)
cf97bcb0
AM
7974 _bfd_error_handler
7975 (_("warning: --plt-localentry is especially dangerous without "
7976 "ld.so support to detect ABI violations"));
f378ab09 7977
9e7028aa 7978 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 7979 false, false, true);
9e7028aa 7980 htab->tls_get_addr = ppc_elf_hash_entry (tga);
9e7028aa 7981 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 7982 false, false, true);
9e7028aa
AM
7983 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7984
7985 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
0a1b45a2 7986 false, false, true);
9e7028aa 7987 htab->tga_desc = ppc_elf_hash_entry (desc);
9e7028aa 7988 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
0a1b45a2 7989 false, false, true);
9e7028aa
AM
7990 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7991
7c9cf415 7992 if (htab->params->tls_get_addr_opt)
a7f2871e 7993 {
9e7028aa 7994 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7995
7996 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
0a1b45a2 7997 false, false, true);
a7f2871e 7998 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
0a1b45a2 7999 false, false, true);
a7f2871e
AM
8000 if (opt_fd != NULL
8001 && (opt_fd->root.type == bfd_link_hash_defined
8002 || opt_fd->root.type == bfd_link_hash_defweak))
8003 {
8004 /* If glibc supports an optimized __tls_get_addr call stub,
8005 signalled by the presence of __tls_get_addr_opt, and we'll
8006 be calling __tls_get_addr via a plt call stub, then
8007 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
8008 if (!(htab->elf.dynamic_sections_created
8009 && tga_fd != NULL
8010 && (tga_fd->type == STT_FUNC
8011 || tga_fd->needs_plt)
8012 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8013 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8014 tga_fd = NULL;
8015 if (!(htab->elf.dynamic_sections_created
8016 && desc_fd != NULL
8017 && (desc_fd->type == STT_FUNC
8018 || desc_fd->needs_plt)
8019 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8020 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8021 desc_fd = NULL;
8022
8023 if (tga_fd != NULL || desc_fd != NULL)
8024 {
8025 struct plt_entry *ent = NULL;
8026
8027 if (tga_fd != NULL)
8028 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8029 if (ent->plt.refcount > 0)
8030 break;
8031 if (ent == NULL && desc_fd != NULL)
8032 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8033 if (ent->plt.refcount > 0)
8034 break;
a7f2871e
AM
8035 if (ent != NULL)
8036 {
9e7028aa
AM
8037 if (tga_fd != NULL)
8038 {
8039 tga_fd->root.type = bfd_link_hash_indirect;
8040 tga_fd->root.u.i.link = &opt_fd->root;
8041 tga_fd->root.u.i.warning = NULL;
8042 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8043 }
8044 if (desc_fd != NULL)
8045 {
8046 desc_fd->root.type = bfd_link_hash_indirect;
8047 desc_fd->root.u.i.link = &opt_fd->root;
8048 desc_fd->root.u.i.warning = NULL;
8049 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8050 }
b531344c 8051 opt_fd->mark = 1;
a7f2871e
AM
8052 if (opt_fd->dynindx != -1)
8053 {
8054 /* Use __tls_get_addr_opt in dynamic relocations. */
8055 opt_fd->dynindx = -1;
8056 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8057 opt_fd->dynstr_index);
8058 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
0a1b45a2 8059 return false;
a7f2871e 8060 }
9e7028aa 8061 if (tga_fd != NULL)
a7f2871e 8062 {
9e7028aa 8063 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 8064 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
8065 if (opt != NULL && tga != NULL)
8066 {
8067 tga->root.type = bfd_link_hash_indirect;
8068 tga->root.u.i.link = &opt->root;
8069 tga->root.u.i.warning = NULL;
8070 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8071 opt->mark = 1;
8072 _bfd_elf_link_hash_hide_symbol (info, opt,
8073 tga->forced_local);
8074 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8075 }
8076 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8077 htab->tls_get_addr_fd->is_func_descriptor = 1;
8078 if (htab->tls_get_addr != NULL)
8079 {
8080 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8081 htab->tls_get_addr->is_func = 1;
8082 }
a7f2871e 8083 }
9e7028aa 8084 if (desc_fd != NULL)
a7f2871e 8085 {
9e7028aa
AM
8086 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8087 if (opt != NULL && desc != NULL)
8088 {
8089 desc->root.type = bfd_link_hash_indirect;
8090 desc->root.u.i.link = &opt->root;
8091 desc->root.u.i.warning = NULL;
8092 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8093 opt->mark = 1;
8094 _bfd_elf_link_hash_hide_symbol (info, opt,
8095 desc->forced_local);
8096 htab->tga_desc = ppc_elf_hash_entry (opt);
8097 }
8098 htab->tga_desc_fd->oh = htab->tga_desc;
8099 htab->tga_desc_fd->is_func_descriptor = 1;
8100 if (htab->tga_desc != NULL)
8101 {
8102 htab->tga_desc->oh = htab->tga_desc_fd;
8103 htab->tga_desc->is_func = 1;
8104 }
a7f2871e
AM
8105 }
8106 }
8107 }
8108 }
7c9cf415
AM
8109 else if (htab->params->tls_get_addr_opt < 0)
8110 htab->params->tls_get_addr_opt = 0;
a7f2871e 8111 }
9e7028aa
AM
8112
8113 if (htab->tga_desc_fd != NULL
8114 && htab->params->tls_get_addr_opt
8115 && htab->params->no_tls_get_addr_regsave == -1)
8116 htab->params->no_tls_get_addr_regsave = 0;
8117
0a1b45a2 8118 return true;
3a71aa26 8119}
8387904d 8120
3a71aa26 8121/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 8122 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 8123
0a1b45a2 8124static bool
30845f11
AM
8125branch_reloc_hash_match (bfd *ibfd,
8126 Elf_Internal_Rela *rel,
8127 struct ppc_link_hash_entry *hash1,
8128 struct ppc_link_hash_entry *hash2,
8129 struct ppc_link_hash_entry *hash3,
8130 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
8131{
8132 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8133 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8134 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8135
e054468f 8136 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8137 {
3a71aa26
AM
8138 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8139 struct elf_link_hash_entry *h;
8387904d 8140
3a71aa26 8141 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8142 h = elf_follow_link (h);
30845f11
AM
8143 if (h == elf_hash_entry (hash1)
8144 || h == elf_hash_entry (hash2)
8145 || h == elf_hash_entry (hash3)
8146 || h == elf_hash_entry (hash4))
0a1b45a2 8147 return true;
a48ebf4d 8148 }
0a1b45a2 8149 return false;
951fd09b 8150}
411e1bfb 8151
951fd09b
AM
8152/* Run through all the TLS relocs looking for optimization
8153 opportunities. The linker has been hacked (see ppc64elf.em) to do
8154 a preliminary section layout so that we know the TLS segment
8155 offsets. We can't optimize earlier because some optimizations need
8156 to know the tp offset, and we need to optimize before allocating
8157 dynamic relocations. */
8158
0a1b45a2 8159bool
33c0ec9d 8160ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8161{
8162 bfd *ibfd;
8163 asection *sec;
8164 struct ppc_link_hash_table *htab;
663a1470 8165 unsigned char *toc_ref;
102890f0 8166 int pass;
951fd09b 8167
3cbc1e5e 8168 if (!bfd_link_executable (info))
0a1b45a2 8169 return true;
411e1bfb 8170
951fd09b 8171 htab = ppc_hash_table (info);
4dfe6ac6 8172 if (htab == NULL)
0a1b45a2 8173 return false;
4dfe6ac6 8174
252dcdf4
AM
8175 htab->do_tls_opt = 1;
8176
663a1470
AM
8177 /* Make two passes over the relocs. On the first pass, mark toc
8178 entries involved with tls relocs, and check that tls relocs
8179 involved in setting up a tls_get_addr call are indeed followed by
8180 such a call. If they are not, we can't do any tls optimization.
8181 On the second pass twiddle tls_mask flags to notify
8182 relocate_section that optimization can be done, and adjust got
8183 and plt refcounts. */
8184 toc_ref = NULL;
8185 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8186 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8187 {
8188 Elf_Internal_Sym *locsyms = NULL;
8189 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8190
102890f0
AM
8191 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8192 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8193 {
8194 Elf_Internal_Rela *relstart, *rel, *relend;
0a1b45a2 8195 bool found_tls_get_addr_arg = 0;
411e1bfb 8196
102890f0
AM
8197 /* Read the relocations. */
8198 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8199 info->keep_memory);
8200 if (relstart == NULL)
2915c55b
JK
8201 {
8202 free (toc_ref);
0a1b45a2 8203 return false;
2915c55b 8204 }
411e1bfb 8205
102890f0
AM
8206 relend = relstart + sec->reloc_count;
8207 for (rel = relstart; rel < relend; rel++)
8208 {
8209 enum elf_ppc64_reloc_type r_type;
8210 unsigned long r_symndx;
8211 struct elf_link_hash_entry *h;
8212 Elf_Internal_Sym *sym;
8213 asection *sym_sec;
f961d9dd 8214 unsigned char *tls_mask;
46e9995a 8215 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0 8216 bfd_vma value;
0a1b45a2 8217 bool ok_tprel, is_local;
102890f0
AM
8218 long toc_ref_index = 0;
8219 int expecting_tls_get_addr = 0;
0a1b45a2 8220 bool ret = false;
411e1bfb 8221
102890f0
AM
8222 r_symndx = ELF64_R_SYM (rel->r_info);
8223 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8224 r_symndx, ibfd))
8225 {
8226 err_free_rel:
8227 if (elf_section_data (sec)->relocs != relstart)
8228 free (relstart);
c9594989
AM
8229 free (toc_ref);
8230 if (elf_symtab_hdr (ibfd).contents
8231 != (unsigned char *) locsyms)
102890f0 8232 free (locsyms);
663a1470 8233 return ret;
102890f0 8234 }
411e1bfb 8235
102890f0
AM
8236 if (h != NULL)
8237 {
766bc656
AM
8238 if (h->root.type == bfd_link_hash_defined
8239 || h->root.type == bfd_link_hash_defweak)
8240 value = h->root.u.def.value;
8241 else if (h->root.type == bfd_link_hash_undefweak)
8242 value = 0;
8243 else
663a1470
AM
8244 {
8245 found_tls_get_addr_arg = 0;
8246 continue;
8247 }
102890f0
AM
8248 }
8249 else
8250 /* Symbols referenced by TLS relocs must be of type
8251 STT_TLS. So no need for .opd local sym adjust. */
8252 value = sym->st_value;
8253
0a1b45a2 8254 ok_tprel = false;
f749f26e
AM
8255 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8256 if (is_local)
102890f0 8257 {
766bc656
AM
8258 if (h != NULL
8259 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 8260 ok_tprel = true;
c27b8c2a
AM
8261 else if (sym_sec != NULL
8262 && sym_sec->output_section != NULL)
766bc656
AM
8263 {
8264 value += sym_sec->output_offset;
8265 value += sym_sec->output_section->vma;
0b147428 8266 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8267 /* Note that even though the prefix insns
8268 allow a 1<<33 offset we use the same test
8269 as for addis;addi. There may be a mix of
8270 pcrel and non-pcrel code and the decision
8271 to optimise is per symbol, not per TLS
8272 sequence. */
0b147428 8273 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8274 }
102890f0 8275 }
951fd09b 8276
102890f0 8277 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8278 /* If this section has old-style __tls_get_addr calls
8279 without marker relocs, then check that each
8280 __tls_get_addr call reloc is preceded by a reloc
8281 that conceivably belongs to the __tls_get_addr arg
8282 setup insn. If we don't find matching arg setup
8283 relocs, don't do any tls optimization. */
8284 if (pass == 0
9737e8af 8285 && sec->nomark_tls_get_addr
663a1470 8286 && h != NULL
ed7007c1 8287 && is_tls_get_addr (h, htab)
663a1470
AM
8288 && !found_tls_get_addr_arg
8289 && is_branch_reloc (r_type))
8290 {
25f53a85 8291 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8292 "TLS optimization disabled\n"),
8293 ibfd, sec, rel->r_offset);
0a1b45a2 8294 ret = true;
663a1470
AM
8295 goto err_free_rel;
8296 }
8297
8298 found_tls_get_addr_arg = 0;
102890f0
AM
8299 switch (r_type)
8300 {
8301 case R_PPC64_GOT_TLSLD16:
8302 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8303 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8304 expecting_tls_get_addr = 1;
663a1470 8305 found_tls_get_addr_arg = 1;
1a0670f3 8306 /* Fall through. */
102890f0
AM
8307
8308 case R_PPC64_GOT_TLSLD16_HI:
8309 case R_PPC64_GOT_TLSLD16_HA:
8310 /* These relocs should never be against a symbol
8311 defined in a shared lib. Leave them alone if
8312 that turns out to be the case. */
8313 if (!is_local)
8314 continue;
411e1bfb 8315
102890f0 8316 /* LD -> LE */
411e1bfb 8317 tls_set = 0;
102890f0
AM
8318 tls_clear = TLS_LD;
8319 tls_type = TLS_TLS | TLS_LD;
8320 break;
411e1bfb 8321
102890f0
AM
8322 case R_PPC64_GOT_TLSGD16:
8323 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8324 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8325 expecting_tls_get_addr = 1;
663a1470 8326 found_tls_get_addr_arg = 1;
1a0670f3 8327 /* Fall through. */
102890f0
AM
8328
8329 case R_PPC64_GOT_TLSGD16_HI:
8330 case R_PPC64_GOT_TLSGD16_HA:
8331 if (ok_tprel)
8332 /* GD -> LE */
411e1bfb 8333 tls_set = 0;
102890f0
AM
8334 else
8335 /* GD -> IE */
b00a0a86 8336 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8337 tls_clear = TLS_GD;
8338 tls_type = TLS_TLS | TLS_GD;
8339 break;
8340
87c69f97 8341 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8342 case R_PPC64_GOT_TPREL16_DS:
8343 case R_PPC64_GOT_TPREL16_LO_DS:
8344 case R_PPC64_GOT_TPREL16_HI:
8345 case R_PPC64_GOT_TPREL16_HA:
8346 if (ok_tprel)
8347 {
8348 /* IE -> LE */
8349 tls_set = 0;
8350 tls_clear = TLS_TPREL;
8351 tls_type = TLS_TLS | TLS_TPREL;
8352 break;
8353 }
411e1bfb
AM
8354 continue;
8355
727fc41e 8356 case R_PPC64_TLSLD:
7d04a20a
AM
8357 if (!is_local)
8358 continue;
8359 /* Fall through. */
8360 case R_PPC64_TLSGD:
23cedd1d
AM
8361 if (rel + 1 < relend
8362 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8363 {
8364 if (pass != 0
2cdcc330 8365 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8366 != R_PPC64_PLTSEQ)
8367 && (ELF64_R_TYPE (rel[1].r_info)
8368 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8369 {
8370 r_symndx = ELF64_R_SYM (rel[1].r_info);
8371 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8372 r_symndx, ibfd))
23cedd1d
AM
8373 goto err_free_rel;
8374 if (h != NULL)
8375 {
8376 struct plt_entry *ent = NULL;
8377
8378 for (ent = h->plt.plist;
8379 ent != NULL;
8380 ent = ent->next)
8381 if (ent->addend == rel[1].r_addend)
8382 break;
8383
8384 if (ent != NULL
8385 && ent->plt.refcount > 0)
8386 ent->plt.refcount -= 1;
8387 }
8388 }
8389 continue;
8390 }
663a1470 8391 found_tls_get_addr_arg = 1;
1a0670f3 8392 /* Fall through. */
663a1470
AM
8393
8394 case R_PPC64_TLS:
8395 case R_PPC64_TOC16:
8396 case R_PPC64_TOC16_LO:
102890f0
AM
8397 if (sym_sec == NULL || sym_sec != toc)
8398 continue;
8399
8400 /* Mark this toc entry as referenced by a TLS
8401 code sequence. We can do that now in the
8402 case of R_PPC64_TLS, and after checking for
8403 tls_get_addr for the TOC16 relocs. */
8404 if (toc_ref == NULL)
2cdcc330
AM
8405 toc_ref
8406 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8407 if (toc_ref == NULL)
8408 goto err_free_rel;
8409
102890f0
AM
8410 if (h != NULL)
8411 value = h->root.u.def.value;
8412 else
8413 value = sym->st_value;
8414 value += rel->r_addend;
73242275
AM
8415 if (value % 8 != 0)
8416 continue;
8417 BFD_ASSERT (value < toc->size
8418 && toc->output_offset % 8 == 0);
663a1470 8419 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8420 if (r_type == R_PPC64_TLS
8421 || r_type == R_PPC64_TLSGD
8422 || r_type == R_PPC64_TLSLD)
102890f0
AM
8423 {
8424 toc_ref[toc_ref_index] = 1;
8425 continue;
8426 }
8427
8428 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8429 continue;
8430
8431 tls_set = 0;
8432 tls_clear = 0;
8433 expecting_tls_get_addr = 2;
8434 break;
8435
8436 case R_PPC64_TPREL64:
8437 if (pass == 0
8438 || sec != toc
8439 || toc_ref == NULL
663a1470 8440 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8441 continue;
8442 if (ok_tprel)
8443 {
8444 /* IE -> LE */
8445 tls_set = TLS_EXPLICIT;
8446 tls_clear = TLS_TPREL;
8447 break;
8448 }
8449 continue;
8450
8451 case R_PPC64_DTPMOD64:
8452 if (pass == 0
8453 || sec != toc
8454 || toc_ref == NULL
663a1470 8455 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8456 continue;
8457 if (rel + 1 < relend
8458 && (rel[1].r_info
8459 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8460 && rel[1].r_offset == rel->r_offset + 8)
8461 {
8462 if (ok_tprel)
8463 /* GD -> LE */
8464 tls_set = TLS_EXPLICIT | TLS_GD;
8465 else
8466 /* GD -> IE */
b00a0a86 8467 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8468 tls_clear = TLS_GD;
8469 }
8470 else
8471 {
8472 if (!is_local)
8473 continue;
8474
8475 /* LD -> LE */
8476 tls_set = TLS_EXPLICIT;
8477 tls_clear = TLS_LD;
8478 }
8479 break;
8480
252dcdf4
AM
8481 case R_PPC64_TPREL16_HA:
8482 if (pass == 0)
8483 {
8484 unsigned char buf[4];
8485 unsigned int insn;
8486 bfd_vma off = rel->r_offset & ~3;
8487 if (!bfd_get_section_contents (ibfd, sec, buf,
8488 off, 4))
8489 goto err_free_rel;
8490 insn = bfd_get_32 (ibfd, buf);
8491 /* addis rt,13,imm */
8492 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8493 != ((15u << 26) | (13 << 16)))
8494 {
8495 /* xgettext:c-format */
8496 info->callbacks->minfo
8497 (_("%H: warning: %s unexpected insn %#x.\n"),
8498 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8499 htab->do_tls_opt = 0;
8500 }
8501 }
8502 continue;
8503
8504 case R_PPC64_TPREL16_HI:
8505 case R_PPC64_TPREL16_HIGH:
8506 case R_PPC64_TPREL16_HIGHA:
8507 case R_PPC64_TPREL16_HIGHER:
8508 case R_PPC64_TPREL16_HIGHERA:
8509 case R_PPC64_TPREL16_HIGHEST:
8510 case R_PPC64_TPREL16_HIGHESTA:
8511 /* These can all be used in sequences along with
8512 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8513 able to verify easily. */
8514 htab->do_tls_opt = 0;
8515 continue;
8516
102890f0
AM
8517 default:
8518 continue;
8519 }
8520
8521 if (pass == 0)
8522 {
727fc41e 8523 if (!expecting_tls_get_addr
9737e8af 8524 || !sec->nomark_tls_get_addr)
102890f0
AM
8525 continue;
8526
3a71aa26
AM
8527 if (rel + 1 < relend
8528 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8529 htab->tls_get_addr_fd,
8530 htab->tga_desc_fd,
3a71aa26 8531 htab->tls_get_addr,
9e7028aa 8532 htab->tga_desc))
102890f0 8533 {
3a71aa26 8534 if (expecting_tls_get_addr == 2)
102890f0 8535 {
3a71aa26 8536 /* Check for toc tls entries. */
f961d9dd 8537 unsigned char *toc_tls;
3a71aa26
AM
8538 int retval;
8539
8540 retval = get_tls_mask (&toc_tls, NULL, NULL,
8541 &locsyms,
8542 rel, ibfd);
8543 if (retval == 0)
8544 goto err_free_rel;
663a1470
AM
8545 if (toc_tls != NULL)
8546 {
37da22e5
AM
8547 if ((*toc_tls & TLS_TLS) != 0
8548 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8549 found_tls_get_addr_arg = 1;
8550 if (retval > 1)
8551 toc_ref[toc_ref_index] = 1;
8552 }
102890f0 8553 }
3a71aa26 8554 continue;
102890f0
AM
8555 }
8556
102890f0
AM
8557 /* Uh oh, we didn't find the expected call. We
8558 could just mark this symbol to exclude it
8559 from tls optimization but it's safer to skip
663a1470 8560 the entire optimization. */
695344c0 8561 /* xgettext:c-format */
25f53a85 8562 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8563 "TLS optimization disabled\n"),
8564 ibfd, sec, rel->r_offset);
0a1b45a2 8565 ret = true;
663a1470 8566 goto err_free_rel;
102890f0
AM
8567 }
8568
37da22e5
AM
8569 /* If we don't have old-style __tls_get_addr calls
8570 without TLSGD/TLSLD marker relocs, and we haven't
8571 found a new-style __tls_get_addr call with a
8572 marker for this symbol, then we either have a
8573 broken object file or an -mlongcall style
8574 indirect call to __tls_get_addr without a marker.
8575 Disable optimization in this case. */
8576 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8577 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8578 && !sec->nomark_tls_get_addr
37da22e5
AM
8579 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8580 != (TLS_TLS | TLS_MARK)))
8581 continue;
8582
7d04a20a 8583 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8584 {
23cedd1d
AM
8585 struct plt_entry *ent = NULL;
8586
9e7028aa
AM
8587 if (htab->tls_get_addr_fd != NULL)
8588 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8589 ent != NULL;
8590 ent = ent->next)
8591 if (ent->addend == 0)
8592 break;
8593
8594 if (ent == NULL && htab->tga_desc_fd != NULL)
8595 for (ent = htab->tga_desc_fd->elf.plt.plist;
8596 ent != NULL;
8597 ent = ent->next)
8598 if (ent->addend == 0)
8599 break;
8600
8601 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8602 for (ent = htab->tls_get_addr->elf.plt.plist;
8603 ent != NULL;
8604 ent = ent->next)
8605 if (ent->addend == 0)
102890f0 8606 break;
411e1bfb 8607
9e7028aa
AM
8608 if (ent == NULL && htab->tga_desc != NULL)
8609 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8610 ent != NULL;
8611 ent = ent->next)
8612 if (ent->addend == 0)
102890f0 8613 break;
23cedd1d
AM
8614
8615 if (ent != NULL
8616 && ent->plt.refcount > 0)
8617 ent->plt.refcount -= 1;
102890f0 8618 }
411e1bfb 8619
102890f0 8620 if (tls_clear == 0)
30038c59
AM
8621 continue;
8622
102890f0
AM
8623 if ((tls_set & TLS_EXPLICIT) == 0)
8624 {
8625 struct got_entry *ent;
411e1bfb 8626
102890f0
AM
8627 /* Adjust got entry for this reloc. */
8628 if (h != NULL)
8629 ent = h->got.glist;
8630 else
8631 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8632
102890f0
AM
8633 for (; ent != NULL; ent = ent->next)
8634 if (ent->addend == rel->r_addend
8635 && ent->owner == ibfd
8636 && ent->tls_type == tls_type)
8637 break;
8638 if (ent == NULL)
8639 abort ();
411e1bfb 8640
102890f0
AM
8641 if (tls_set == 0)
8642 {
8643 /* We managed to get rid of a got entry. */
8644 if (ent->got.refcount > 0)
8645 ent->got.refcount -= 1;
8646 }
8647 }
8648 else
8649 {
8650 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8651 we'll lose one or two dyn relocs. */
1657026c 8652 if (!dec_dynrel_count (rel, sec, info,
19e08130 8653 NULL, h, sym))
0a1b45a2 8654 return false;
411e1bfb 8655
102890f0
AM
8656 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8657 {
1657026c 8658 if (!dec_dynrel_count (rel + 1, sec, info,
19e08130 8659 NULL, h, sym))
0a1b45a2 8660 return false;
102890f0
AM
8661 }
8662 }
411e1bfb 8663
46e9995a 8664 *tls_mask |= tls_set & 0xff;
102890f0
AM
8665 *tls_mask &= ~tls_clear;
8666 }
8c1d1bb8 8667
102890f0
AM
8668 if (elf_section_data (sec)->relocs != relstart)
8669 free (relstart);
8670 }
411e1bfb 8671
663a1470
AM
8672 if (locsyms != NULL
8673 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8674 {
8675 if (!info->keep_memory)
8676 free (locsyms);
8677 else
8678 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8679 }
8680 }
411e1bfb 8681
c9594989 8682 free (toc_ref);
0a1b45a2 8683 return true;
1e2f5b6e 8684}
b34976b6 8685
c5614fa4
AM
8686/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8687 the values of any global symbols in a toc section that has been
8688 edited. Globals in toc sections should be a rarity, so this function
8689 sets a flag if any are found in toc sections other than the one just
de194d85 8690 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8691
8692struct adjust_toc_info
8693{
8694 asection *toc;
8695 unsigned long *skip;
0a1b45a2 8696 bool global_toc_syms;
c5614fa4
AM
8697};
8698
ba761f19
AM
8699enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8700
0a1b45a2 8701static bool
c5614fa4
AM
8702adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8703{
8704 struct ppc_link_hash_entry *eh;
8705 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8706 unsigned long i;
c5614fa4 8707
c5614fa4
AM
8708 if (h->root.type != bfd_link_hash_defined
8709 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 8710 return true;
c5614fa4 8711
ed7007c1 8712 eh = ppc_elf_hash_entry (h);
c5614fa4 8713 if (eh->adjust_done)
0a1b45a2 8714 return true;
c5614fa4
AM
8715
8716 if (eh->elf.root.u.def.section == toc_inf->toc)
8717 {
854b41e7
AM
8718 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8719 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8720 else
854b41e7
AM
8721 i = eh->elf.root.u.def.value >> 3;
8722
ba761f19 8723 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8724 {
4eca0228 8725 _bfd_error_handler
854b41e7
AM
8726 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8727 do
8728 ++i;
ba761f19 8729 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8730 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8731 }
854b41e7
AM
8732
8733 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8734 eh->adjust_done = 1;
8735 }
8736 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
0a1b45a2 8737 toc_inf->global_toc_syms = true;
c5614fa4 8738
0a1b45a2 8739 return true;
c5614fa4
AM
8740}
8741
39eeab25
AM
8742/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8743 on a _LO variety toc/got reloc. */
560c8763 8744
0a1b45a2 8745static bool
39eeab25 8746ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8747{
2365f8d7
AM
8748 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8749 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8750 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8751 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8752 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8753 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8754 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8755 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8756 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8757 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8758 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8759 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8760 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8761 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8762 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8763 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8764 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8765 /* Exclude lfqu by testing reloc. If relocs are ever
8766 defined for the reduced D field in psq_lu then those
8767 will need testing too. */
8768 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8769 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8770 && (insn & 1) == 0)
2365f8d7
AM
8771 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8772 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8773 /* Exclude stfqu. psq_stu as above for psq_lu. */
8774 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8775 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8776 && (insn & 1) == 0));
560c8763
AM
8777}
8778
4a421c53
AM
8779/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8780 pld ra,symbol@got@pcrel
dd9b12c2 8781 load/store rt,off(ra)
4a421c53 8782 or
d4b87b1e 8783 pla ra,symbol@pcrel
dd9b12c2 8784 load/store rt,off(ra)
4a421c53 8785 may be translated to
dd9b12c2 8786 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8787 nop.
8788 This function returns true if the optimization is possible, placing
dd9b12c2 8789 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8790
8791 On entry to this function, the linker has already determined that
d4b87b1e 8792 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8793 while *PINSN2 is the second instruction. */
8794
0a1b45a2 8795static bool
dd9b12c2 8796xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8797{
77486630
AM
8798 uint64_t insn1 = *pinsn1;
8799 uint64_t insn2 = *pinsn2;
dd9b12c2 8800 bfd_signed_vma off;
4a421c53 8801
77486630
AM
8802 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8803 {
8804 /* Check that regs match. */
8805 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8806 return false;
77486630
AM
8807
8808 /* P8LS or PMLS form, non-pcrel. */
8809 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
0a1b45a2 8810 return false;
77486630
AM
8811
8812 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8813 *pinsn2 = PNOP;
8814 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8815 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
0a1b45a2 8816 return true;
77486630
AM
8817 }
8818
8819 insn2 >>= 32;
8820
4a421c53 8821 /* Check that regs match. */
77486630 8822 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8823 return false;
4a421c53
AM
8824
8825 switch ((insn2 >> 26) & 63)
8826 {
8827 default:
0a1b45a2 8828 return false;
4a421c53
AM
8829
8830 case 32: /* lwz */
8831 case 34: /* lbz */
8832 case 36: /* stw */
8833 case 38: /* stb */
8834 case 40: /* lhz */
8835 case 42: /* lha */
8836 case 44: /* sth */
8837 case 48: /* lfs */
8838 case 50: /* lfd */
8839 case 52: /* stfs */
8840 case 54: /* stfd */
8841 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8842 on the insn. */
77486630 8843 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8844 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8845 off = insn2 & 0xffff;
4a421c53
AM
8846 break;
8847
8848 case 58: /* lwa, ld */
dd9b12c2 8849 if ((insn2 & 1) != 0)
0a1b45a2 8850 return false;
77486630 8851 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8852 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8853 | (insn2 & (31ULL << 21)));
dd9b12c2 8854 off = insn2 & 0xfffc;
4a421c53
AM
8855 break;
8856
8857 case 57: /* lxsd, lxssp */
dd9b12c2 8858 if ((insn2 & 3) < 2)
0a1b45a2 8859 return false;
77486630 8860 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8861 | ((40ULL | (insn2 & 3)) << 26)
8862 | (insn2 & (31ULL << 21)));
dd9b12c2 8863 off = insn2 & 0xfffc;
4a421c53
AM
8864 break;
8865
8866 case 61: /* stxsd, stxssp, lxv, stxv */
8867 if ((insn2 & 3) == 0)
0a1b45a2 8868 return false;
4a421c53
AM
8869 else if ((insn2 & 3) >= 2)
8870 {
77486630 8871 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8872 | ((44ULL | (insn2 & 3)) << 26)
8873 | (insn2 & (31ULL << 21)));
dd9b12c2 8874 off = insn2 & 0xfffc;
4a421c53
AM
8875 }
8876 else
8877 {
77486630 8878 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8879 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8880 | (insn2 & (31ULL << 21)));
dd9b12c2 8881 off = insn2 & 0xfff0;
4a421c53
AM
8882 }
8883 break;
8884
8885 case 56: /* lq */
77486630 8886 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8887 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8888 off = insn2 & 0xffff;
4a421c53
AM
8889 break;
8890
94ba9882
AM
8891 case 6: /* lxvp, stxvp */
8892 if ((insn2 & 0xe) != 0)
0a1b45a2 8893 return false;
94ba9882
AM
8894 insn1 = ((1ULL << 58) | (1ULL << 52)
8895 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8896 | (insn2 & (31ULL << 21)));
8897 off = insn2 & 0xfff0;
8898 break;
8899
4a421c53 8900 case 62: /* std, stq */
dd9b12c2 8901 if ((insn2 & 1) != 0)
0a1b45a2 8902 return false;
77486630 8903 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8904 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8905 | (insn2 & (31ULL << 21)));
dd9b12c2 8906 off = insn2 & 0xfffc;
4a421c53
AM
8907 break;
8908 }
8909
77486630 8910 *pinsn1 = insn1;
4a421c53 8911 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8912 *poff = (off ^ 0x8000) - 0x8000;
0a1b45a2 8913 return true;
4a421c53
AM
8914}
8915
c5614fa4
AM
8916/* Examine all relocs referencing .toc sections in order to remove
8917 unused .toc entries. */
8918
0a1b45a2 8919bool
33c0ec9d 8920ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8921{
8922 bfd *ibfd;
8923 struct adjust_toc_info toc_inf;
67f0cbdb 8924 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8925
67f0cbdb 8926 htab->do_toc_opt = 1;
0a1b45a2 8927 toc_inf.global_toc_syms = true;
c72f2fb2 8928 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8929 {
8930 asection *toc, *sec;
8931 Elf_Internal_Shdr *symtab_hdr;
8932 Elf_Internal_Sym *local_syms;
425b145b 8933 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8934 unsigned long *skip, *drop;
8935 unsigned char *used;
8936 unsigned char *keep, last, some_unused;
8937
854b41e7
AM
8938 if (!is_ppc64_elf (ibfd))
8939 continue;
8940
c5614fa4
AM
8941 toc = bfd_get_section_by_name (ibfd, ".toc");
8942 if (toc == NULL
92b7a70f 8943 || toc->size == 0
81ff113f 8944 || (toc->flags & SEC_HAS_CONTENTS) == 0
dbaa2011
AM
8945 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8946 || discarded_section (toc))
c5614fa4
AM
8947 continue;
8948
425b145b 8949 toc_relocs = NULL;
c5614fa4 8950 local_syms = NULL;
0ffa91dd 8951 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8952
8953 /* Look at sections dropped from the final link. */
8954 skip = NULL;
8955 relstart = NULL;
8956 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8957 {
8958 if (sec->reloc_count == 0
dbaa2011 8959 || !discarded_section (sec)
c5614fa4
AM
8960 || get_opd_info (sec)
8961 || (sec->flags & SEC_ALLOC) == 0
8962 || (sec->flags & SEC_DEBUGGING) != 0)
8963 continue;
8964
0a1b45a2 8965 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
c5614fa4
AM
8966 if (relstart == NULL)
8967 goto error_ret;
8968
8969 /* Run through the relocs to see which toc entries might be
8970 unused. */
8971 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8972 {
8973 enum elf_ppc64_reloc_type r_type;
8974 unsigned long r_symndx;
8975 asection *sym_sec;
8976 struct elf_link_hash_entry *h;
8977 Elf_Internal_Sym *sym;
8978 bfd_vma val;
8979
8980 r_type = ELF64_R_TYPE (rel->r_info);
8981 switch (r_type)
8982 {
8983 default:
8984 continue;
8985
8986 case R_PPC64_TOC16:
8987 case R_PPC64_TOC16_LO:
8988 case R_PPC64_TOC16_HI:
8989 case R_PPC64_TOC16_HA:
8990 case R_PPC64_TOC16_DS:
8991 case R_PPC64_TOC16_LO_DS:
8992 break;
8993 }
8994
8995 r_symndx = ELF64_R_SYM (rel->r_info);
8996 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8997 r_symndx, ibfd))
8998 goto error_ret;
8999
9000 if (sym_sec != toc)
9001 continue;
9002
9003 if (h != NULL)
9004 val = h->root.u.def.value;
9005 else
9006 val = sym->st_value;
9007 val += rel->r_addend;
9008
9009 if (val >= toc->size)
9010 continue;
9011
9012 /* Anything in the toc ought to be aligned to 8 bytes.
9013 If not, don't mark as unused. */
9014 if (val & 7)
9015 continue;
9016
9017 if (skip == NULL)
9018 {
854b41e7 9019 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9020 if (skip == NULL)
9021 goto error_ret;
9022 }
9023
ba761f19 9024 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9025 }
9026
9027 if (elf_section_data (sec)->relocs != relstart)
9028 free (relstart);
9029 }
9030
ba761f19
AM
9031 /* For largetoc loads of address constants, we can convert
9032 . addis rx,2,addr@got@ha
9033 . ld ry,addr@got@l(rx)
9034 to
9035 . addis rx,2,addr@toc@ha
9036 . addi ry,rx,addr@toc@l
9037 when addr is within 2G of the toc pointer. This then means
9038 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9039
ba761f19
AM
9040 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9041 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9042 && toc->reloc_count != 0)
9043 {
9044 /* Read toc relocs. */
425b145b
AM
9045 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9046 info->keep_memory);
9047 if (toc_relocs == NULL)
ba761f19
AM
9048 goto error_ret;
9049
425b145b 9050 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9051 {
9052 enum elf_ppc64_reloc_type r_type;
9053 unsigned long r_symndx;
9054 asection *sym_sec;
9055 struct elf_link_hash_entry *h;
9056 Elf_Internal_Sym *sym;
9057 bfd_vma val, addr;
9058
9059 r_type = ELF64_R_TYPE (rel->r_info);
9060 if (r_type != R_PPC64_ADDR64)
9061 continue;
9062
9063 r_symndx = ELF64_R_SYM (rel->r_info);
9064 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9065 r_symndx, ibfd))
9066 goto error_ret;
9067
425b145b 9068 if (sym_sec == NULL
c27b8c2a 9069 || sym_sec->output_section == NULL
dbaa2011 9070 || discarded_section (sym_sec))
425b145b
AM
9071 continue;
9072
3a3a4c1f
AM
9073 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9074 || (bfd_link_pic (info)
9075 && sym_sec == bfd_abs_section_ptr))
ba761f19
AM
9076 continue;
9077
9078 if (h != NULL)
bddc25c9
AM
9079 {
9080 if (h->type == STT_GNU_IFUNC)
9081 continue;
9082 val = h->root.u.def.value;
9083 }
ba761f19 9084 else
bddc25c9
AM
9085 {
9086 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9087 continue;
9088 val = sym->st_value;
9089 }
ba761f19
AM
9090 val += rel->r_addend;
9091 val += sym_sec->output_section->vma + sym_sec->output_offset;
9092
9093 /* We don't yet know the exact toc pointer value, but we
9094 know it will be somewhere in the toc section. Don't
9095 optimize if the difference from any possible toc
9096 pointer is outside [ff..f80008000, 7fff7fff]. */
9097 addr = toc->output_section->vma + TOC_BASE_OFF;
9098 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9099 continue;
9100
9101 addr = toc->output_section->vma + toc->output_section->rawsize;
9102 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9103 continue;
9104
9105 if (skip == NULL)
9106 {
9107 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9108 if (skip == NULL)
9109 goto error_ret;
9110 }
9111
9112 skip[rel->r_offset >> 3]
425b145b 9113 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9114 }
ba761f19
AM
9115 }
9116
c5614fa4
AM
9117 if (skip == NULL)
9118 continue;
9119
9120 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9121 if (used == NULL)
9122 {
9123 error_ret:
c9594989 9124 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
9125 free (local_syms);
9126 if (sec != NULL
c5614fa4
AM
9127 && elf_section_data (sec)->relocs != relstart)
9128 free (relstart);
c9594989 9129 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9130 free (toc_relocs);
c9594989 9131 free (skip);
0a1b45a2 9132 return false;
c5614fa4
AM
9133 }
9134
30038c59
AM
9135 /* Now check all kept sections that might reference the toc.
9136 Check the toc itself last. */
9137 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9138 : ibfd->sections);
c5614fa4 9139 sec != NULL;
c5614fa4 9140 sec = (sec == toc ? NULL
c5614fa4 9141 : sec->next == NULL ? toc
30038c59 9142 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9143 : sec->next))
9144 {
9145 int repeat;
9146
9147 if (sec->reloc_count == 0
dbaa2011 9148 || discarded_section (sec)
c5614fa4
AM
9149 || get_opd_info (sec)
9150 || (sec->flags & SEC_ALLOC) == 0
9151 || (sec->flags & SEC_DEBUGGING) != 0)
9152 continue;
9153
854b41e7
AM
9154 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9155 info->keep_memory);
c5614fa4 9156 if (relstart == NULL)
2915c55b
JK
9157 {
9158 free (used);
9159 goto error_ret;
9160 }
c5614fa4
AM
9161
9162 /* Mark toc entries referenced as used. */
c5614fa4 9163 do
d4f1ee75
AM
9164 {
9165 repeat = 0;
9166 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9167 {
9168 enum elf_ppc64_reloc_type r_type;
9169 unsigned long r_symndx;
9170 asection *sym_sec;
9171 struct elf_link_hash_entry *h;
9172 Elf_Internal_Sym *sym;
9173 bfd_vma val;
98528052 9174
d4f1ee75 9175 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
9176 switch (r_type)
9177 {
9178 case R_PPC64_TOC16:
9179 case R_PPC64_TOC16_LO:
9180 case R_PPC64_TOC16_HI:
9181 case R_PPC64_TOC16_HA:
9182 case R_PPC64_TOC16_DS:
9183 case R_PPC64_TOC16_LO_DS:
9184 /* In case we're taking addresses of toc entries. */
9185 case R_PPC64_ADDR64:
9186 break;
c5614fa4 9187
d4f1ee75
AM
9188 default:
9189 continue;
9190 }
c5614fa4 9191
d4f1ee75
AM
9192 r_symndx = ELF64_R_SYM (rel->r_info);
9193 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9194 r_symndx, ibfd))
9195 {
9196 free (used);
9197 goto error_ret;
9198 }
c5614fa4 9199
d4f1ee75
AM
9200 if (sym_sec != toc)
9201 continue;
c5614fa4 9202
d4f1ee75
AM
9203 if (h != NULL)
9204 val = h->root.u.def.value;
9205 else
9206 val = sym->st_value;
9207 val += rel->r_addend;
ba761f19 9208
d4f1ee75
AM
9209 if (val >= toc->size)
9210 continue;
ba761f19 9211
d4f1ee75
AM
9212 if ((skip[val >> 3] & can_optimize) != 0)
9213 {
9214 bfd_vma off;
9215 unsigned char opc;
9216
9217 switch (r_type)
9218 {
9219 case R_PPC64_TOC16_HA:
ba761f19 9220 break;
ba761f19 9221
d4f1ee75
AM
9222 case R_PPC64_TOC16_LO_DS:
9223 off = rel->r_offset;
9224 off += (bfd_big_endian (ibfd) ? -2 : 3);
9225 if (!bfd_get_section_contents (ibfd, sec, &opc,
9226 off, 1))
9227 {
9228 free (used);
9229 goto error_ret;
9230 }
9231 if ((opc & (0x3f << 2)) == (58u << 2))
9232 break;
1a0670f3 9233 /* Fall through. */
ba761f19 9234
d4f1ee75
AM
9235 default:
9236 /* Wrong sort of reloc, or not a ld. We may
9237 as well clear ref_from_discarded too. */
9238 skip[val >> 3] = 0;
9239 }
9240 }
9241
9242 if (sec != toc)
9243 used[val >> 3] = 1;
9244 /* For the toc section, we only mark as used if this
9245 entry itself isn't unused. */
9246 else if ((used[rel->r_offset >> 3]
9247 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9248 && !used[val >> 3])
9249 {
9250 /* Do all the relocs again, to catch reference
9251 chains. */
9252 repeat = 1;
9253 used[val >> 3] = 1;
9254 }
9255 }
9256 }
c5614fa4 9257 while (repeat);
854b41e7
AM
9258
9259 if (elf_section_data (sec)->relocs != relstart)
9260 free (relstart);
c5614fa4
AM
9261 }
9262
9263 /* Merge the used and skip arrays. Assume that TOC
9264 doublewords not appearing as either used or unused belong
de194d85 9265 to an entry more than one doubleword in size. */
c5614fa4
AM
9266 for (drop = skip, keep = used, last = 0, some_unused = 0;
9267 drop < skip + (toc->size + 7) / 8;
9268 ++drop, ++keep)
9269 {
9270 if (*keep)
9271 {
ba761f19
AM
9272 *drop &= ~ref_from_discarded;
9273 if ((*drop & can_optimize) != 0)
9274 some_unused = 1;
c5614fa4
AM
9275 last = 0;
9276 }
b140b010 9277 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9278 {
9279 some_unused = 1;
ba761f19 9280 last = ref_from_discarded;
c5614fa4
AM
9281 }
9282 else
9283 *drop = last;
9284 }
9285
9286 free (used);
9287
9288 if (some_unused)
9289 {
9290 bfd_byte *contents, *src;
9291 unsigned long off;
d62b3684 9292 Elf_Internal_Sym *sym;
0a1b45a2 9293 bool local_toc_syms = false;
c5614fa4
AM
9294
9295 /* Shuffle the toc contents, and at the same time convert the
9296 skip array from booleans into offsets. */
9297 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9298 goto error_ret;
9299
9300 elf_section_data (toc)->this_hdr.contents = contents;
9301
9302 for (src = contents, off = 0, drop = skip;
9303 src < contents + toc->size;
9304 src += 8, ++drop)
9305 {
ba761f19
AM
9306 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9307 off += 8;
c5614fa4
AM
9308 else if (off != 0)
9309 {
9310 *drop = off;
9311 memcpy (src - off, src, 8);
9312 }
9313 }
854b41e7 9314 *drop = off;
c5614fa4
AM
9315 toc->rawsize = toc->size;
9316 toc->size = src - contents - off;
9317
ba761f19
AM
9318 /* Adjust addends for relocs against the toc section sym,
9319 and optimize any accesses we can. */
c5614fa4
AM
9320 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9321 {
9322 if (sec->reloc_count == 0
dbaa2011 9323 || discarded_section (sec))
c5614fa4
AM
9324 continue;
9325
9326 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9327 info->keep_memory);
c5614fa4
AM
9328 if (relstart == NULL)
9329 goto error_ret;
9330
9331 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9332 {
9333 enum elf_ppc64_reloc_type r_type;
9334 unsigned long r_symndx;
9335 asection *sym_sec;
9336 struct elf_link_hash_entry *h;
854b41e7 9337 bfd_vma val;
c5614fa4
AM
9338
9339 r_type = ELF64_R_TYPE (rel->r_info);
9340 switch (r_type)
9341 {
9342 default:
9343 continue;
9344
9345 case R_PPC64_TOC16:
9346 case R_PPC64_TOC16_LO:
9347 case R_PPC64_TOC16_HI:
9348 case R_PPC64_TOC16_HA:
9349 case R_PPC64_TOC16_DS:
9350 case R_PPC64_TOC16_LO_DS:
9351 case R_PPC64_ADDR64:
9352 break;
9353 }
9354
9355 r_symndx = ELF64_R_SYM (rel->r_info);
9356 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9357 r_symndx, ibfd))
9358 goto error_ret;
9359
ba761f19 9360 if (sym_sec != toc)
c5614fa4
AM
9361 continue;
9362
ba761f19
AM
9363 if (h != NULL)
9364 val = h->root.u.def.value;
9365 else
9366 {
9367 val = sym->st_value;
9368 if (val != 0)
0a1b45a2 9369 local_toc_syms = true;
ba761f19
AM
9370 }
9371
9372 val += rel->r_addend;
854b41e7
AM
9373
9374 if (val > toc->rawsize)
9375 val = toc->rawsize;
ba761f19
AM
9376 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9377 continue;
9378 else if ((skip[val >> 3] & can_optimize) != 0)
9379 {
9380 Elf_Internal_Rela *tocrel
425b145b 9381 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9382 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9383
9384 switch (r_type)
9385 {
9386 case R_PPC64_TOC16_HA:
9387 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9388 break;
9389
9390 case R_PPC64_TOC16_LO_DS:
9391 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9392 break;
9393
9394 default:
28942f62
AM
9395 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9396 ppc_howto_init ();
b140b010 9397 info->callbacks->einfo
695344c0 9398 /* xgettext:c-format */
174d0a74 9399 (_("%H: %s references "
b140b010
AM
9400 "optimized away TOC entry\n"),
9401 ibfd, sec, rel->r_offset,
9402 ppc64_elf_howto_table[r_type]->name);
9403 bfd_set_error (bfd_error_bad_value);
9404 goto error_ret;
ba761f19
AM
9405 }
9406 rel->r_addend = tocrel->r_addend;
9407 elf_section_data (sec)->relocs = relstart;
9408 continue;
9409 }
9410
9411 if (h != NULL || sym->st_value != 0)
9412 continue;
854b41e7
AM
9413
9414 rel->r_addend -= skip[val >> 3];
9415 elf_section_data (sec)->relocs = relstart;
c5614fa4 9416 }
854b41e7
AM
9417
9418 if (elf_section_data (sec)->relocs != relstart)
9419 free (relstart);
c5614fa4
AM
9420 }
9421
9422 /* We shouldn't have local or global symbols defined in the TOC,
9423 but handle them anyway. */
df22d223
AM
9424 if (local_syms != NULL)
9425 for (sym = local_syms;
9426 sym < local_syms + symtab_hdr->sh_info;
9427 ++sym)
9428 if (sym->st_value != 0
9429 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9430 {
9431 unsigned long i;
854b41e7 9432
df22d223
AM
9433 if (sym->st_value > toc->rawsize)
9434 i = toc->rawsize >> 3;
9435 else
9436 i = sym->st_value >> 3;
854b41e7 9437
df22d223
AM
9438 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9439 {
9440 if (local_toc_syms)
4eca0228 9441 _bfd_error_handler
df22d223
AM
9442 (_("%s defined on removed toc entry"),
9443 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9444 do
9445 ++i;
9446 while ((skip[i] & (ref_from_discarded | can_optimize)));
9447 sym->st_value = (bfd_vma) i << 3;
9448 }
d62b3684 9449
df22d223
AM
9450 sym->st_value -= skip[i];
9451 symtab_hdr->contents = (unsigned char *) local_syms;
9452 }
c5614fa4 9453
854b41e7 9454 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9455 if (toc_inf.global_toc_syms)
9456 {
9457 toc_inf.toc = toc;
9458 toc_inf.skip = skip;
0a1b45a2 9459 toc_inf.global_toc_syms = false;
c5614fa4
AM
9460 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9461 &toc_inf);
9462 }
854b41e7
AM
9463
9464 if (toc->reloc_count != 0)
9465 {
d4730f92 9466 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9467 Elf_Internal_Rela *wrel;
9468 bfd_size_type sz;
9469
854b41e7 9470 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9471 if (toc_relocs == NULL)
9472 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9473 info->keep_memory);
9474 if (toc_relocs == NULL)
9475 goto error_ret;
9476
425b145b
AM
9477 wrel = toc_relocs;
9478 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9479 if ((skip[rel->r_offset >> 3]
9480 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9481 {
9482 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9483 wrel->r_info = rel->r_info;
9484 wrel->r_addend = rel->r_addend;
9485 ++wrel;
9486 }
1657026c 9487 else if (!dec_dynrel_count (rel, toc, info,
854b41e7
AM
9488 &local_syms, NULL, NULL))
9489 goto error_ret;
9490
425b145b
AM
9491 elf_section_data (toc)->relocs = toc_relocs;
9492 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9493 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9494 sz = rel_hdr->sh_entsize;
9495 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9496 }
c5614fa4 9497 }
c9594989 9498 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9499 free (toc_relocs);
c5614fa4
AM
9500
9501 if (local_syms != NULL
9502 && symtab_hdr->contents != (unsigned char *) local_syms)
9503 {
9504 if (!info->keep_memory)
9505 free (local_syms);
9506 else
9507 symtab_hdr->contents = (unsigned char *) local_syms;
9508 }
9509 free (skip);
9510 }
9511
066f4018 9512 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9513 a GOT relative or PC relative access, possibly reducing the
9514 number of GOT entries. */
066f4018
AM
9515 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9516 {
9517 asection *sec;
9518 Elf_Internal_Shdr *symtab_hdr;
9519 Elf_Internal_Sym *local_syms;
9520 Elf_Internal_Rela *relstart, *rel;
9521 bfd_vma got;
9522
9523 if (!is_ppc64_elf (ibfd))
9524 continue;
9525
903b777d 9526 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9527 continue;
9528
9529 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9530 got = 0;
9531 if (sec != NULL)
9532 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9533
9534 local_syms = NULL;
9535 symtab_hdr = &elf_symtab_hdr (ibfd);
9536
9537 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9538 {
9539 if (sec->reloc_count == 0
903b777d 9540 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9541 || discarded_section (sec))
9542 continue;
9543
9544 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9545 info->keep_memory);
9546 if (relstart == NULL)
9547 {
9548 got_error_ret:
c9594989 9549 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9550 free (local_syms);
9551 if (sec != NULL
066f4018
AM
9552 && elf_section_data (sec)->relocs != relstart)
9553 free (relstart);
0a1b45a2 9554 return false;
066f4018
AM
9555 }
9556
9557 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9558 {
9559 enum elf_ppc64_reloc_type r_type;
9560 unsigned long r_symndx;
9561 Elf_Internal_Sym *sym;
9562 asection *sym_sec;
9563 struct elf_link_hash_entry *h;
9564 struct got_entry *ent;
133a1f60 9565 bfd_vma val, pc;
4a421c53 9566 unsigned char buf[8];
066f4018 9567 unsigned int insn;
903b777d 9568 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9569
9570 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9571 switch (r_type)
9572 {
9573 default:
9574 insn_check = no_check;
9575 break;
9576
9577 case R_PPC64_PLT16_HA:
9578 case R_PPC64_GOT_TLSLD16_HA:
9579 case R_PPC64_GOT_TLSGD16_HA:
9580 case R_PPC64_GOT_TPREL16_HA:
9581 case R_PPC64_GOT_DTPREL16_HA:
9582 case R_PPC64_GOT16_HA:
9583 case R_PPC64_TOC16_HA:
9584 insn_check = check_ha;
9585 break;
9586
9587 case R_PPC64_PLT16_LO:
9588 case R_PPC64_PLT16_LO_DS:
9589 case R_PPC64_GOT_TLSLD16_LO:
9590 case R_PPC64_GOT_TLSGD16_LO:
9591 case R_PPC64_GOT_TPREL16_LO_DS:
9592 case R_PPC64_GOT_DTPREL16_LO_DS:
9593 case R_PPC64_GOT16_LO:
9594 case R_PPC64_GOT16_LO_DS:
9595 case R_PPC64_TOC16_LO:
9596 case R_PPC64_TOC16_LO_DS:
9597 insn_check = check_lo;
9598 break;
9599 }
9600
9601 if (insn_check != no_check)
9602 {
9603 bfd_vma off = rel->r_offset & ~3;
9604
9605 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9606 goto got_error_ret;
9607
9608 insn = bfd_get_32 (ibfd, buf);
9609 if (insn_check == check_lo
9610 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9611 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9612 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9613 {
9614 char str[12];
9615
9616 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9617 sprintf (str, "%#08x", insn);
9618 info->callbacks->einfo
9619 /* xgettext:c-format */
9620 (_("%H: got/toc optimization is not supported for"
9621 " %s instruction\n"),
9622 ibfd, sec, rel->r_offset & ~3, str);
9623 continue;
9624 }
9625 }
9626
066f4018
AM
9627 switch (r_type)
9628 {
bb22a418
AM
9629 /* Note that we don't delete GOT entries for
9630 R_PPC64_GOT16_DS since we'd need a lot more
9631 analysis. For starters, the preliminary layout is
9632 before the GOT, PLT, dynamic sections and stubs are
9633 laid out. Then we'd need to allow for changes in
9634 distance between sections caused by alignment. */
066f4018
AM
9635 default:
9636 continue;
9637
066f4018
AM
9638 case R_PPC64_GOT16_HA:
9639 case R_PPC64_GOT16_LO_DS:
4a421c53 9640 case R_PPC64_GOT_PCREL34:
066f4018
AM
9641 break;
9642 }
9643
9644 r_symndx = ELF64_R_SYM (rel->r_info);
9645 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9646 r_symndx, ibfd))
9647 goto got_error_ret;
9648
6d5554a6
AM
9649 if (sym_sec == NULL
9650 || sym_sec->output_section == NULL
9651 || discarded_section (sym_sec))
9652 continue;
9653
06507dab
AM
9654 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9655 continue;
9656
3a3a4c1f
AM
9657 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9658 || (bfd_link_pic (info)
9659 && sym_sec == bfd_abs_section_ptr))
066f4018
AM
9660 continue;
9661
9662 if (h != NULL)
9663 val = h->root.u.def.value;
9664 else
9665 val = sym->st_value;
133a1f60 9666 val += rel->r_addend;
066f4018
AM
9667 val += sym_sec->output_section->vma + sym_sec->output_offset;
9668
bb22a418
AM
9669/* Fudge factor to allow for the fact that the preliminary layout
9670 isn't exact. Reduce limits by this factor. */
9671#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9672
066f4018
AM
9673 switch (r_type)
9674 {
9675 default:
9676 continue;
9677
066f4018 9678 case R_PPC64_GOT16_HA:
bb22a418
AM
9679 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9680 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9681 continue;
9682
9683 if (!bfd_get_section_contents (ibfd, sec, buf,
9684 rel->r_offset & ~3, 4))
9685 goto got_error_ret;
9686 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9687 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9688 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9689 continue;
9690 break;
9691
9692 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9693 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9694 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9695 continue;
9696 if (!bfd_get_section_contents (ibfd, sec, buf,
9697 rel->r_offset & ~3, 4))
9698 goto got_error_ret;
9699 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9700 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9701 continue;
9702 break;
4a421c53
AM
9703
9704 case R_PPC64_GOT_PCREL34:
9705 pc = rel->r_offset;
9706 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9707 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9708 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9709 continue;
9710 if (!bfd_get_section_contents (ibfd, sec, buf,
9711 rel->r_offset & ~3, 8))
9712 goto got_error_ret;
9713 insn = bfd_get_32 (ibfd, buf);
9714 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9715 continue;
9716 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9717 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9718 continue;
9719 break;
066f4018 9720 }
bb22a418 9721#undef LIMIT_ADJUST
066f4018
AM
9722
9723 if (h != NULL)
9724 ent = h->got.glist;
9725 else
9726 {
9727 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9728 ent = local_got_ents[r_symndx];
9729 }
9730 for (; ent != NULL; ent = ent->next)
133a1f60 9731 if (ent->addend == rel->r_addend
066f4018
AM
9732 && ent->owner == ibfd
9733 && ent->tls_type == 0)
9734 break;
9735 BFD_ASSERT (ent && ent->got.refcount > 0);
9736 ent->got.refcount -= 1;
9737 }
9738
9739 if (elf_section_data (sec)->relocs != relstart)
9740 free (relstart);
9741 }
9742
9743 if (local_syms != NULL
9744 && symtab_hdr->contents != (unsigned char *) local_syms)
9745 {
9746 if (!info->keep_memory)
9747 free (local_syms);
9748 else
9749 symtab_hdr->contents = (unsigned char *) local_syms;
9750 }
9751 }
9752
0a1b45a2 9753 return true;
c5614fa4
AM
9754}
9755
1bbe0902
AM
9756/* Return true iff input section I references the TOC using
9757 instructions limited to +/-32k offsets. */
9758
0a1b45a2 9759bool
1bbe0902
AM
9760ppc64_elf_has_small_toc_reloc (asection *i)
9761{
9762 return (is_ppc64_elf (i->owner)
9763 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9764}
9765
927be08e
AM
9766/* Allocate space for one GOT entry. */
9767
9768static void
9769allocate_got (struct elf_link_hash_entry *h,
9770 struct bfd_link_info *info,
9771 struct got_entry *gent)
9772{
9773 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9774 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9775 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9776 ? 16 : 8);
9777 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9778 ? 2 : 1) * sizeof (Elf64_External_Rela);
9779 asection *got = ppc64_elf_tdata (gent->owner)->got;
9780
9781 gent->got.offset = got->size;
9782 got->size += entsize;
9783
19e08130 9784 if (h->type == STT_GNU_IFUNC)
927be08e 9785 {
33e44f2e 9786 htab->elf.irelplt->size += rentsize;
19e08130 9787 htab->got_reli_size += rentsize;
927be08e 9788 }
f15d0b54 9789 else if (((bfd_link_pic (info)
1657026c
AM
9790 && (gent->tls_type == 0
9791 ? !info->enable_dt_relr
9792 : !(bfd_link_executable (info)
3a3a4c1f
AM
9793 && SYMBOL_REFERENCES_LOCAL (info, h)))
9794 && !bfd_is_abs_symbol (&h->root))
f0158f44
AM
9795 || (htab->elf.dynamic_sections_created
9796 && h->dynindx != -1
9797 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9798 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9799 {
19e08130 9800 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9801 relgot->size += rentsize;
927be08e
AM
9802 }
9803}
9804
7865406b
AM
9805/* This function merges got entries in the same toc group. */
9806
9807static void
9808merge_got_entries (struct got_entry **pent)
9809{
9810 struct got_entry *ent, *ent2;
9811
9812 for (ent = *pent; ent != NULL; ent = ent->next)
9813 if (!ent->is_indirect)
9814 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9815 if (!ent2->is_indirect
9816 && ent2->addend == ent->addend
9817 && ent2->tls_type == ent->tls_type
9818 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9819 {
0a1b45a2 9820 ent2->is_indirect = true;
7865406b
AM
9821 ent2->got.ent = ent;
9822 }
9823}
9824
46434633 9825/* If H is undefined, make it dynamic if that makes sense. */
f0158f44 9826
0a1b45a2 9827static bool
46434633
AM
9828ensure_undef_dynamic (struct bfd_link_info *info,
9829 struct elf_link_hash_entry *h)
f0158f44
AM
9830{
9831 struct elf_link_hash_table *htab = elf_hash_table (info);
9832
9833 if (htab->dynamic_sections_created
46434633
AM
9834 && ((info->dynamic_undefined_weak != 0
9835 && h->root.type == bfd_link_hash_undefweak)
9836 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9837 && h->dynindx == -1
9838 && !h->forced_local
9839 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9840 return bfd_elf_link_record_dynamic_symbol (info, h);
0a1b45a2 9841 return true;
f0158f44
AM
9842}
9843
30845f11
AM
9844/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9845 usual htab->elf.splt section for a PLT entry. */
9846
9847static inline
0a1b45a2 9848bool use_local_plt (struct bfd_link_info *info,
30845f11
AM
9849 struct elf_link_hash_entry *h)
9850{
9851 return (h == NULL
9852 || h->dynindx == -1
9853 || !elf_hash_table (info)->dynamic_sections_created);
9854}
9855
65f38f15
AM
9856/* Allocate space in .plt, .got and associated reloc sections for
9857 dynamic relocs. */
5bd4f169 9858
0a1b45a2 9859static bool
4ce794b7 9860allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9861{
65f38f15
AM
9862 struct bfd_link_info *info;
9863 struct ppc_link_hash_table *htab;
5bd4f169 9864 asection *s;
65f38f15 9865 struct ppc_link_hash_entry *eh;
0b8bcf0d 9866 struct got_entry **pgent, *gent;
5bd4f169 9867
e92d460e 9868 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9869 return true;
5bd4f169 9870
65f38f15
AM
9871 info = (struct bfd_link_info *) inf;
9872 htab = ppc_hash_table (info);
4dfe6ac6 9873 if (htab == NULL)
0a1b45a2 9874 return false;
5bd4f169 9875
ed7007c1 9876 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9877 /* Run through the TLS GD got entries first if we're changing them
9878 to TPREL. */
b00a0a86 9879 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9880 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9881 if (gent->got.refcount > 0
9882 && (gent->tls_type & TLS_GD) != 0)
9883 {
9884 /* This was a GD entry that has been converted to TPREL. If
9885 there happens to be a TPREL entry we can use that one. */
9886 struct got_entry *ent;
9887 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9888 if (ent->got.refcount > 0
9889 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9890 && ent->addend == gent->addend
9891 && ent->owner == gent->owner)
951fd09b
AM
9892 {
9893 gent->got.refcount = 0;
9894 break;
9895 }
9896
9897 /* If not, then we'll be using our own TPREL entry. */
9898 if (gent->got.refcount != 0)
9899 gent->tls_type = TLS_TLS | TLS_TPREL;
9900 }
9901
7865406b
AM
9902 /* Remove any list entry that won't generate a word in the GOT before
9903 we call merge_got_entries. Otherwise we risk merging to empty
9904 entries. */
0b8bcf0d
AM
9905 pgent = &h->got.glist;
9906 while ((gent = *pgent) != NULL)
411e1bfb 9907 if (gent->got.refcount > 0)
7865406b
AM
9908 {
9909 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9910 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9911 {
9912 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9913 *pgent = gent->next;
9914 }
9915 else
9916 pgent = &gent->next;
9917 }
9918 else
9919 *pgent = gent->next;
9920
9921 if (!htab->do_multi_toc)
9922 merge_got_entries (&h->got.glist);
9923
9924 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9925 if (!gent->is_indirect)
411e1bfb 9926 {
ec73ddcd
AM
9927 /* Ensure we catch all the cases where this symbol should
9928 be made dynamic. */
46434633 9929 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9930 return false;
65f38f15 9931
0c8d6e5c 9932 if (!is_ppc64_elf (gent->owner))
927be08e 9933 abort ();
0ffa91dd 9934
927be08e 9935 allocate_got (h, info, gent);
411e1bfb 9936 }
65f38f15 9937
954b63d4
AM
9938 /* If no dynamic sections we can't have dynamic relocs, except for
9939 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9940 if (!htab->elf.dynamic_sections_created
9941 && h->type != STT_GNU_IFUNC)
190eb1dd 9942 h->dyn_relocs = NULL;
8a2058b5 9943
529fe20e
AM
9944 /* Discard relocs on undefined symbols that must be local. */
9945 else if (h->root.type == bfd_link_hash_undefined
9946 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9947 h->dyn_relocs = NULL;
529fe20e 9948
954b63d4
AM
9949 /* Also discard relocs on undefined weak syms with non-default
9950 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9951 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9952 h->dyn_relocs = NULL;
954b63d4 9953
190eb1dd 9954 if (h->dyn_relocs != NULL)
65f38f15 9955 {
1657026c 9956 struct ppc_dyn_relocs *p, **pp;
8a2058b5 9957
57e7d118
AM
9958 /* In the shared -Bsymbolic case, discard space allocated for
9959 dynamic pc-relative relocs against symbols which turn out to
9960 be defined in regular objects. For the normal shared case,
9961 discard space for relocs that have become local due to symbol
9962 visibility changes. */
57e7d118 9963 if (bfd_link_pic (info))
65f38f15 9964 {
57e7d118
AM
9965 /* Relocs that use pc_count are those that appear on a call
9966 insn, or certain REL relocs (see must_be_dyn_reloc) that
9967 can be generated via assembly. We want calls to
9968 protected symbols to resolve directly to the function
9969 rather than going via the plt. If people want function
9970 pointer comparisons to work as expected then they should
9971 avoid writing weird assembly. */
9972 if (SYMBOL_CALLS_LOCAL (info, h))
9973 {
1657026c
AM
9974 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9975 (p = *pp) != NULL;
9976 )
57e7d118
AM
9977 {
9978 p->count -= p->pc_count;
9979 p->pc_count = 0;
9980 if (p->count == 0)
9981 *pp = p->next;
9982 else
9983 pp = &p->next;
9984 }
9985 }
65f38f15 9986
190eb1dd 9987 if (h->dyn_relocs != NULL)
5bd4f169 9988 {
ec73ddcd
AM
9989 /* Ensure we catch all the cases where this symbol
9990 should be made dynamic. */
46434633 9991 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9992 return false;
5bd4f169 9993 }
65f38f15 9994 }
ec73ddcd
AM
9995
9996 /* For a fixed position executable, discard space for
9997 relocs against symbols which are not dynamic. */
9998 else if (h->type != STT_GNU_IFUNC)
57e7d118 9999 {
49160308
AM
10000 if ((h->dynamic_adjusted
10001 || (h->ref_regular
10002 && h->root.type == bfd_link_hash_undefweak
10003 && (info->dynamic_undefined_weak > 0
10004 || !_bfd_elf_readonly_dynrelocs (h))))
529fe20e
AM
10005 && !h->def_regular
10006 && !ELF_COMMON_DEF_P (h))
f0158f44 10007 {
ec73ddcd
AM
10008 /* Ensure we catch all the cases where this symbol
10009 should be made dynamic. */
46434633 10010 if (!ensure_undef_dynamic (info, h))
0a1b45a2 10011 return false;
dfbb6ac9 10012
ec73ddcd 10013 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 10014 if (h->dynindx == -1)
190eb1dd 10015 h->dyn_relocs = NULL;
f0158f44
AM
10016 }
10017 else
190eb1dd 10018 h->dyn_relocs = NULL;
57e7d118
AM
10019 }
10020
10021 /* Finally, allocate space. */
1657026c 10022 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
365bf300
AM
10023 if (!discarded_section (p->sec))
10024 {
10025 unsigned int count;
10026 asection *sreloc = elf_section_data (p->sec)->sreloc;
10027 if (eh->elf.type == STT_GNU_IFUNC)
10028 sreloc = htab->elf.irelplt;
10029 count = p->count;
10030 if (info->enable_dt_relr
10031 && ((!NO_OPD_RELOCS
10032 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10033 || (eh->elf.type != STT_GNU_IFUNC
10034 && SYMBOL_REFERENCES_LOCAL (info, h))))
10035 count -= p->rel_count;
10036 sreloc->size += count * sizeof (Elf64_External_Rela);
10037 }
65f38f15 10038 }
57e7d118 10039
2d7ad24e
AM
10040 /* We might need a PLT entry when the symbol
10041 a) is dynamic, or
10042 b) is an ifunc, or
10043 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10044 d) has plt16 relocs and we are linking statically. */
10045 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10046 || h->type == STT_GNU_IFUNC
10047 || (h->needs_plt && h->dynamic_adjusted)
10048 || (h->needs_plt
10049 && h->def_regular
10050 && !htab->elf.dynamic_sections_created
3e04d765 10051 && !htab->can_convert_all_inline_plt
ed7007c1 10052 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 10053 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10054 {
57e7d118 10055 struct plt_entry *pent;
0a1b45a2 10056 bool doneone = false;
57e7d118
AM
10057 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10058 if (pent->plt.refcount > 0)
10059 {
b2936612
AM
10060 if (!ensure_undef_dynamic (info, h))
10061 return false;
10062
30845f11 10063 if (use_local_plt (info, h))
57e7d118 10064 {
2d7ad24e
AM
10065 if (h->type == STT_GNU_IFUNC)
10066 {
10067 s = htab->elf.iplt;
10068 pent->plt.offset = s->size;
10069 s->size += PLT_ENTRY_SIZE (htab);
10070 s = htab->elf.irelplt;
10071 }
10072 else
10073 {
10074 s = htab->pltlocal;
10075 pent->plt.offset = s->size;
10076 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
1657026c
AM
10077 s = NULL;
10078 if (bfd_link_pic (info)
10079 && !(info->enable_dt_relr && !htab->opd_abi))
10080 s = htab->relpltlocal;
2d7ad24e 10081 }
57e7d118
AM
10082 }
10083 else
10084 {
10085 /* If this is the first .plt entry, make room for the special
10086 first entry. */
10087 s = htab->elf.splt;
10088 if (s->size == 0)
10089 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10090
57e7d118 10091 pent->plt.offset = s->size;
65f38f15 10092
57e7d118
AM
10093 /* Make room for this entry. */
10094 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10095
57e7d118
AM
10096 /* Make room for the .glink code. */
10097 s = htab->glink;
10098 if (s->size == 0)
9e390558 10099 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10100 if (htab->opd_abi)
10101 {
10102 /* We need bigger stubs past index 32767. */
9e390558 10103 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10104 s->size += 4;
10105 s->size += 2*4;
10106 }
10107 else
10108 s->size += 4;
65f38f15 10109
57e7d118
AM
10110 /* We also need to make an entry in the .rela.plt section. */
10111 s = htab->elf.srelplt;
10112 }
2d7ad24e
AM
10113 if (s != NULL)
10114 s->size += sizeof (Elf64_External_Rela);
0a1b45a2 10115 doneone = true;
57e7d118
AM
10116 }
10117 else
10118 pent->plt.offset = (bfd_vma) -1;
10119 if (!doneone)
10120 {
10121 h->plt.plist = NULL;
10122 h->needs_plt = 0;
10123 }
65f38f15 10124 }
57e7d118 10125 else
65f38f15 10126 {
57e7d118
AM
10127 h->plt.plist = NULL;
10128 h->needs_plt = 0;
65f38f15
AM
10129 }
10130
0a1b45a2 10131 return true;
65f38f15
AM
10132}
10133
9e390558
AM
10134#define PPC_LO(v) ((v) & 0xffff)
10135#define PPC_HI(v) (((v) >> 16) & 0xffff)
10136#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
10137#define D34(v) \
10138 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10139#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 10140
a345bc8d
AM
10141/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10142 to set up space for global entry stubs. These are put in glink,
10143 after the branch table. */
65f38f15 10144
0a1b45a2 10145static bool
a345bc8d 10146size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10147{
a345bc8d
AM
10148 struct bfd_link_info *info;
10149 struct ppc_link_hash_table *htab;
10150 struct plt_entry *pent;
9e390558 10151 asection *s, *plt;
65f38f15 10152
a345bc8d 10153 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 10154 return true;
65f38f15 10155
a345bc8d 10156 if (!h->pointer_equality_needed)
0a1b45a2 10157 return true;
65f38f15 10158
a345bc8d 10159 if (h->def_regular)
0a1b45a2 10160 return true;
65f38f15 10161
a345bc8d
AM
10162 info = inf;
10163 htab = ppc_hash_table (info);
10164 if (htab == NULL)
0a1b45a2 10165 return false;
a345bc8d 10166
9e390558
AM
10167 s = htab->global_entry;
10168 plt = htab->elf.splt;
a345bc8d
AM
10169 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10170 if (pent->plt.offset != (bfd_vma) -1
10171 && pent->addend == 0)
10172 {
afe397ea
AM
10173 /* For ELFv2, if this symbol is not defined in a regular file
10174 and we are not generating a shared library or pie, then we
10175 need to define the symbol in the executable on a call stub.
10176 This is to avoid text relocations. */
9e390558
AM
10177 bfd_vma off, stub_align, stub_off, stub_size;
10178 unsigned int align_power;
10179
10180 stub_size = 16;
10181 stub_off = s->size;
10182 if (htab->params->plt_stub_align >= 0)
10183 align_power = htab->params->plt_stub_align;
10184 else
10185 align_power = -htab->params->plt_stub_align;
10186 /* Setting section alignment is delayed until we know it is
10187 non-empty. Otherwise the .text output section will be
10188 aligned at least to plt_stub_align even when no global
10189 entry stubs are needed. */
10190 if (s->alignment_power < align_power)
10191 s->alignment_power = align_power;
10192 stub_align = (bfd_vma) 1 << align_power;
10193 if (htab->params->plt_stub_align >= 0
10194 || ((((stub_off + stub_size - 1) & -stub_align)
10195 - (stub_off & -stub_align))
10196 > ((stub_size - 1) & -stub_align)))
10197 stub_off = (stub_off + stub_align - 1) & -stub_align;
10198 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10199 off -= stub_off + s->output_offset + s->output_section->vma;
10200 /* Note that for --plt-stub-align negative we have a possible
10201 dependency between stub offset and size. Break that
10202 dependency by assuming the max stub size when calculating
10203 the stub offset. */
10204 if (PPC_HA (off) == 0)
10205 stub_size -= 4;
8a2058b5 10206 h->root.type = bfd_link_hash_defined;
afe397ea 10207 h->root.u.def.section = s;
9e390558
AM
10208 h->root.u.def.value = stub_off;
10209 s->size = stub_off + stub_size;
a345bc8d
AM
10210 break;
10211 }
0a1b45a2 10212 return true;
a345bc8d
AM
10213}
10214
65f38f15
AM
10215/* Set the sizes of the dynamic sections. */
10216
0a1b45a2 10217static bool
ee67d69a 10218ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10219 struct bfd_link_info *info)
65f38f15
AM
10220{
10221 struct ppc_link_hash_table *htab;
10222 bfd *dynobj;
10223 asection *s;
0a1b45a2 10224 bool relocs;
65f38f15 10225 bfd *ibfd;
7865406b 10226 struct got_entry *first_tlsld;
65f38f15
AM
10227
10228 htab = ppc_hash_table (info);
4dfe6ac6 10229 if (htab == NULL)
0a1b45a2 10230 return false;
4dfe6ac6 10231
65f38f15
AM
10232 dynobj = htab->elf.dynobj;
10233 if (dynobj == NULL)
10234 abort ();
10235
10236 if (htab->elf.dynamic_sections_created)
10237 {
10238 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10239 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10240 {
3d4d4302 10241 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10242 if (s == NULL)
10243 abort ();
eea6121a 10244 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10245 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10246 }
10247 }
10248
10249 /* Set up .got offsets for local syms, and space for local dynamic
10250 relocs. */
c72f2fb2 10251 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10252 {
411e1bfb
AM
10253 struct got_entry **lgot_ents;
10254 struct got_entry **end_lgot_ents;
e054468f
AM
10255 struct plt_entry **local_plt;
10256 struct plt_entry **end_local_plt;
f961d9dd 10257 unsigned char *lgot_masks;
65f38f15
AM
10258 bfd_size_type locsymcount;
10259 Elf_Internal_Shdr *symtab_hdr;
3a3a4c1f
AM
10260 Elf_Internal_Sym *local_syms;
10261 Elf_Internal_Sym *isym;
65f38f15 10262
0c8d6e5c 10263 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10264 continue;
10265
10266 for (s = ibfd->sections; s != NULL; s = s->next)
10267 {
1657026c 10268 struct ppc_local_dyn_relocs *p;
65f38f15 10269
6edfbbad 10270 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10271 {
365bf300 10272 if (discarded_section (p->sec))
ec338859
AM
10273 {
10274 /* Input section has been discarded, either because
10275 it is a copy of a linkonce section or due to
10276 linker script /DISCARD/, so we'll be discarding
10277 the relocs too. */
10278 }
248866a8 10279 else if (p->count != 0)
ec338859 10280 {
1657026c
AM
10281 unsigned int count;
10282 asection *srel;
10283
10284 count = p->count;
8ffb6df2
AM
10285 if (info->enable_dt_relr
10286 && ((!NO_OPD_RELOCS
10287 && (ppc64_elf_section_data (p->sec)->sec_type
10288 == sec_opd))
10289 || !p->ifunc))
1657026c
AM
10290 count -= p->rel_count;
10291 srel = elf_section_data (p->sec)->sreloc;
19e08130 10292 if (p->ifunc)
33e44f2e 10293 srel = htab->elf.irelplt;
1657026c 10294 srel->size += count * sizeof (Elf64_External_Rela);
248866a8
AM
10295 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10296 info->flags |= DF_TEXTREL;
ec338859 10297 }
65f38f15
AM
10298 }
10299 }
10300
411e1bfb
AM
10301 lgot_ents = elf_local_got_ents (ibfd);
10302 if (!lgot_ents)
65f38f15
AM
10303 continue;
10304
0ffa91dd 10305 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10306 locsymcount = symtab_hdr->sh_info;
411e1bfb 10307 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10308 local_plt = (struct plt_entry **) end_lgot_ents;
10309 end_local_plt = local_plt + locsymcount;
f961d9dd 10310 lgot_masks = (unsigned char *) end_local_plt;
c9fecd62
AM
10311 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10312 if (local_syms == NULL && locsymcount != 0)
3a3a4c1f 10313 {
c9fecd62
AM
10314 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10315 0, NULL, NULL, NULL);
10316 if (local_syms == NULL)
10317 return false;
3a3a4c1f 10318 }
e717da7e 10319 s = ppc64_elf_tdata (ibfd)->got;
3a3a4c1f
AM
10320 for (isym = local_syms;
10321 lgot_ents < end_lgot_ents;
c9fecd62 10322 ++lgot_ents, ++lgot_masks, isym++)
65f38f15 10323 {
0b8bcf0d 10324 struct got_entry **pent, *ent;
411e1bfb 10325
0b8bcf0d
AM
10326 pent = lgot_ents;
10327 while ((ent = *pent) != NULL)
411e1bfb
AM
10328 if (ent->got.refcount > 0)
10329 {
e7b938ca 10330 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10331 {
927be08e 10332 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10333 *pent = ent->next;
411e1bfb
AM
10334 }
10335 else
10336 {
19e08130
AM
10337 unsigned int ent_size = 8;
10338 unsigned int rel_size = sizeof (Elf64_External_Rela);
10339
eea6121a 10340 ent->got.offset = s->size;
e7b938ca 10341 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10342 {
19e08130
AM
10343 ent_size *= 2;
10344 rel_size *= 2;
10345 }
10346 s->size += ent_size;
37da22e5 10347 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10348 {
33e44f2e 10349 htab->elf.irelplt->size += rel_size;
19e08130
AM
10350 htab->got_reli_size += rel_size;
10351 }
93370e8e 10352 else if (bfd_link_pic (info)
8f22c953
AM
10353 && (ent->tls_type == 0
10354 ? !info->enable_dt_relr
3a3a4c1f
AM
10355 : !bfd_link_executable (info))
10356 && isym->st_shndx != SHN_ABS)
19e08130
AM
10357 {
10358 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10359 srel->size += rel_size;
927be08e 10360 }
0b8bcf0d 10361 pent = &ent->next;
411e1bfb
AM
10362 }
10363 }
10364 else
0b8bcf0d 10365 *pent = ent->next;
65f38f15 10366 }
3a3a4c1f
AM
10367 if (local_syms != NULL
10368 && symtab_hdr->contents != (unsigned char *) local_syms)
10369 {
10370 if (!info->keep_memory)
10371 free (local_syms);
10372 else
10373 symtab_hdr->contents = (unsigned char *) local_syms;
10374 }
e054468f 10375
2d7ad24e
AM
10376 /* Allocate space for plt calls to local syms. */
10377 lgot_masks = (unsigned char *) end_local_plt;
10378 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10379 {
10380 struct plt_entry *ent;
10381
10382 for (ent = *local_plt; ent != NULL; ent = ent->next)
10383 if (ent->plt.refcount > 0)
10384 {
2d7ad24e
AM
10385 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10386 {
10387 s = htab->elf.iplt;
10388 ent->plt.offset = s->size;
10389 s->size += PLT_ENTRY_SIZE (htab);
10390 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10391 }
3e04d765
AM
10392 else if (htab->can_convert_all_inline_plt
10393 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10394 ent->plt.offset = (bfd_vma) -1;
10395 else
10396 {
10397 s = htab->pltlocal;
10398 ent->plt.offset = s->size;
10399 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
edca615a
AM
10400 if (bfd_link_pic (info)
10401 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
10402 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10403 }
e054468f
AM
10404 }
10405 else
10406 ent->plt.offset = (bfd_vma) -1;
10407 }
65f38f15
AM
10408 }
10409
10410 /* Allocate global sym .plt and .got entries, and space for global
10411 sym dynamic relocs. */
4ce794b7 10412 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10413
0e1862bb 10414 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10415 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10416
7865406b 10417 first_tlsld = NULL;
c72f2fb2 10418 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10419 {
7865406b
AM
10420 struct got_entry *ent;
10421
0c8d6e5c 10422 if (!is_ppc64_elf (ibfd))
102890f0
AM
10423 continue;
10424
7865406b
AM
10425 ent = ppc64_tlsld_got (ibfd);
10426 if (ent->got.refcount > 0)
102890f0 10427 {
7865406b 10428 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10429 {
0a1b45a2 10430 ent->is_indirect = true;
7865406b
AM
10431 ent->got.ent = first_tlsld;
10432 }
10433 else
10434 {
10435 if (first_tlsld == NULL)
10436 first_tlsld = ent;
10437 s = ppc64_elf_tdata (ibfd)->got;
10438 ent->got.offset = s->size;
10439 ent->owner = ibfd;
10440 s->size += 16;
f749f26e 10441 if (bfd_link_dll (info))
7865406b
AM
10442 {
10443 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10444 srel->size += sizeof (Elf64_External_Rela);
10445 }
102890f0
AM
10446 }
10447 }
10448 else
7865406b 10449 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10450 }
10451
65f38f15
AM
10452 /* We now have determined the sizes of the various dynamic sections.
10453 Allocate memory for them. */
0a1b45a2 10454 relocs = false;
65f38f15
AM
10455 for (s = dynobj->sections; s != NULL; s = s->next)
10456 {
10457 if ((s->flags & SEC_LINKER_CREATED) == 0)
10458 continue;
10459
1657026c 10460 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
721956f4
AM
10461 /* These haven't been allocated yet; don't strip. */
10462 continue;
33e44f2e
AM
10463 else if (s == htab->elf.sgot
10464 || s == htab->elf.splt
10465 || s == htab->elf.iplt
2d7ad24e 10466 || s == htab->pltlocal
c456f082 10467 || s == htab->glink
9e390558 10468 || s == htab->global_entry
5474d94f
AM
10469 || s == htab->elf.sdynbss
10470 || s == htab->elf.sdynrelro)
65f38f15
AM
10471 {
10472 /* Strip this section if we don't need it; see the
10473 comment below. */
5bd4f169 10474 }
58d180e8
AM
10475 else if (s == htab->glink_eh_frame)
10476 {
10477 if (!bfd_is_abs_section (s->output_section))
10478 /* Not sized yet. */
10479 continue;
10480 }
08dedd66 10481 else if (startswith (s->name, ".rela"))
5bd4f169 10482 {
c456f082 10483 if (s->size != 0)
5bd4f169 10484 {
33e44f2e 10485 if (s != htab->elf.srelplt)
0a1b45a2 10486 relocs = true;
5bd4f169
AM
10487
10488 /* We use the reloc_count field as a counter if we need
10489 to copy relocs into the output file. */
10490 s->reloc_count = 0;
10491 }
10492 }
65f38f15 10493 else
5bd4f169
AM
10494 {
10495 /* It's not one of our sections, so don't allocate space. */
10496 continue;
10497 }
10498
eea6121a 10499 if (s->size == 0)
5bd4f169 10500 {
c456f082
AM
10501 /* If we don't need this section, strip it from the
10502 output file. This is mostly to handle .rela.bss and
10503 .rela.plt. We must create both sections in
10504 create_dynamic_sections, because they must be created
10505 before the linker maps input sections to output
10506 sections. The linker does that before
10507 adjust_dynamic_symbol is called, and it is that
10508 function which decides whether anything needs to go
10509 into these sections. */
8423293d 10510 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10511 continue;
10512 }
10513
06bcf541
AM
10514 if (bfd_is_abs_section (s->output_section))
10515 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10516 s->name);
10517
c456f082 10518 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10519 continue;
10520
65f38f15
AM
10521 /* Allocate memory for the section contents. We use bfd_zalloc
10522 here in case unused entries are not reclaimed before the
10523 section's contents are written out. This should not happen,
411e1bfb
AM
10524 but this way if it does we get a R_PPC64_NONE reloc in .rela
10525 sections instead of garbage.
10526 We also rely on the section contents being zero when writing
5474d94f 10527 the GOT and .dynrelro. */
eea6121a 10528 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10529 if (s->contents == NULL)
0a1b45a2 10530 return false;
5bd4f169
AM
10531 }
10532
c72f2fb2 10533 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10534 {
0c8d6e5c 10535 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10536 continue;
10537
e717da7e 10538 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10539 if (s != NULL && s != htab->elf.sgot)
e717da7e 10540 {
eea6121a 10541 if (s->size == 0)
8423293d 10542 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10543 else
10544 {
eea6121a 10545 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10546 if (s->contents == NULL)
0a1b45a2 10547 return false;
e717da7e
AM
10548 }
10549 }
10550 s = ppc64_elf_tdata (ibfd)->relgot;
10551 if (s != NULL)
10552 {
eea6121a 10553 if (s->size == 0)
8423293d 10554 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10555 else
10556 {
eea6121a 10557 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10558 if (s->contents == NULL)
0a1b45a2
AM
10559 return false;
10560 relocs = true;
e717da7e
AM
10561 s->reloc_count = 0;
10562 }
10563 }
10564 }
10565
e86ce104 10566 if (htab->elf.dynamic_sections_created)
5bd4f169 10567 {
0a1b45a2 10568 bool tls_opt;
e8910a83 10569
5bd4f169
AM
10570 /* Add some entries to the .dynamic section. We fill in the
10571 values later, in ppc64_elf_finish_dynamic_sections, but we
10572 must add the entries now so that we get the correct size for
10573 the .dynamic section. The DT_DEBUG entry is filled in by the
10574 dynamic linker and used by the debugger. */
dc810e39 10575#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10576 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10577
0e1862bb 10578 if (bfd_link_executable (info))
5bd4f169 10579 {
dc810e39 10580 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 10581 return false;
5bd4f169
AM
10582 }
10583
33e44f2e 10584 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10585 {
dc810e39
AM
10586 if (!add_dynamic_entry (DT_PLTGOT, 0)
10587 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10588 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10589 || !add_dynamic_entry (DT_JMPREL, 0)
10590 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
0a1b45a2 10591 return false;
5bd4f169
AM
10592 }
10593
ee67d69a 10594 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10595 {
10596 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10597 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
0a1b45a2 10598 return false;
19397422
AM
10599 }
10600
7c9cf415 10601 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10602 && ((htab->tls_get_addr_fd != NULL
10603 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10604 || (htab->tga_desc_fd != NULL
10605 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10606 if (tls_opt || !htab->opd_abi)
10607 {
10608 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
0a1b45a2 10609 return false;
e8910a83 10610 }
a7f2871e 10611
5bd4f169
AM
10612 if (relocs)
10613 {
dc810e39
AM
10614 if (!add_dynamic_entry (DT_RELA, 0)
10615 || !add_dynamic_entry (DT_RELASZ, 0)
10616 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
0a1b45a2 10617 return false;
5bd4f169 10618
65f38f15
AM
10619 /* If any dynamic relocs apply to a read-only section,
10620 then we need a DT_TEXTREL entry. */
248866a8 10621 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10622 elf_link_hash_traverse (&htab->elf,
10623 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10624
65f38f15 10625 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10626 {
65f38f15 10627 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 10628 return false;
5bd4f169 10629 }
5bd4f169 10630 }
5bd4f169 10631 }
65f38f15 10632#undef add_dynamic_entry
5bd4f169 10633
0a1b45a2 10634 return true;
5bd4f169
AM
10635}
10636
a345bc8d
AM
10637/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10638
0a1b45a2 10639static bool
a345bc8d
AM
10640ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10641{
10642 if (h->plt.plist != NULL
10643 && !h->def_regular
10644 && !h->pointer_equality_needed)
0a1b45a2 10645 return false;
a345bc8d
AM
10646
10647 return _bfd_elf_hash_symbol (h);
10648}
10649
721956f4 10650/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10651
7aba54da 10652static inline enum ppc_stub_main_type
4ce794b7
AM
10653ppc_type_of_stub (asection *input_sec,
10654 const Elf_Internal_Rela *rel,
10655 struct ppc_link_hash_entry **hash,
e054468f 10656 struct plt_entry **plt_ent,
6911b7dc
AM
10657 bfd_vma destination,
10658 unsigned long local_off)
5bd4f169 10659{
721956f4
AM
10660 struct ppc_link_hash_entry *h = *hash;
10661 bfd_vma location;
10662 bfd_vma branch_offset;
10663 bfd_vma max_branch_offset;
4ce794b7 10664 enum elf_ppc64_reloc_type r_type;
5bd4f169 10665
721956f4
AM
10666 if (h != NULL)
10667 {
e054468f 10668 struct plt_entry *ent;
7fe2b9a6 10669 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10670 if (h->oh != NULL
10671 && h->oh->is_func_descriptor)
7b8f6675
AM
10672 {
10673 fdh = ppc_follow_link (h->oh);
10674 *hash = fdh;
10675 }
8387904d 10676
e054468f
AM
10677 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10678 if (ent->addend == rel->r_addend
10679 && ent->plt.offset != (bfd_vma) -1)
10680 {
e054468f
AM
10681 *plt_ent = ent;
10682 return ppc_stub_plt_call;
10683 }
5bd4f169 10684
7fe2b9a6
AM
10685 /* Here, we know we don't have a plt entry. If we don't have a
10686 either a defined function descriptor or a defined entry symbol
10687 in a regular object file, then it is pointless trying to make
10688 any other type of stub. */
854b41e7
AM
10689 if (!is_static_defined (&fdh->elf)
10690 && !is_static_defined (&h->elf))
721956f4 10691 return ppc_stub_none;
5d1634d7 10692 }
e054468f
AM
10693 else if (elf_local_got_ents (input_sec->owner) != NULL)
10694 {
10695 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10696 struct plt_entry **local_plt = (struct plt_entry **)
10697 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10698 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10699
10700 if (local_plt[r_symndx] != NULL)
10701 {
10702 struct plt_entry *ent;
10703
10704 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10705 if (ent->addend == rel->r_addend
10706 && ent->plt.offset != (bfd_vma) -1)
10707 {
10708 *plt_ent = ent;
10709 return ppc_stub_plt_call;
10710 }
10711 }
10712 }
5d1634d7 10713
721956f4
AM
10714 /* Determine where the call point is. */
10715 location = (input_sec->output_offset
10716 + input_sec->output_section->vma
10717 + rel->r_offset);
5d1634d7 10718
721956f4
AM
10719 branch_offset = destination - location;
10720 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10721
721956f4
AM
10722 /* Determine if a long branch stub is needed. */
10723 max_branch_offset = 1 << 25;
23cedd1d
AM
10724 if (r_type == R_PPC64_REL14
10725 || r_type == R_PPC64_REL14_BRTAKEN
10726 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10727 max_branch_offset = 1 << 15;
5d1634d7 10728
6911b7dc 10729 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10730 /* We need a stub. Figure out whether a long_branch or plt_branch
10731 is needed later. */
10732 return ppc_stub_long_branch;
5d1634d7 10733
721956f4 10734 return ppc_stub_none;
5d1634d7
AM
10735}
10736
f891966f
AM
10737/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10738 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10739 . mflr %r12
10740 . bcl 20,31,1f
10741 .1: mflr %r11
10742 . mtlr %r12
05d0e962 10743 . lis %r12,xxx-1b@highest
f891966f 10744 . ori %r12,%r12,xxx-1b@higher
05d0e962 10745 . sldi %r12,%r12,32
f891966f 10746 . oris %r12,%r12,xxx-1b@high
05d0e962 10747 . ori %r12,%r12,xxx-1b@l
f891966f 10748 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10749
10750static bfd_byte *
0a1b45a2 10751build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
05d0e962 10752{
f891966f
AM
10753 bfd_put_32 (abfd, MFLR_R12, p);
10754 p += 4;
10755 bfd_put_32 (abfd, BCL_20_31, p);
10756 p += 4;
10757 bfd_put_32 (abfd, MFLR_R11, p);
10758 p += 4;
10759 bfd_put_32 (abfd, MTLR_R12, p);
10760 p += 4;
05d0e962
AM
10761 if (off + 0x8000 < 0x10000)
10762 {
10763 if (load)
10764 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10765 else
10766 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10767 p += 4;
10768 }
10769 else if (off + 0x80008000ULL < 0x100000000ULL)
10770 {
10771 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10772 p += 4;
10773 if (load)
10774 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10775 else
10776 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10777 p += 4;
10778 }
10779 else
10780 {
10781 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10782 {
10783 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10784 p += 4;
10785 }
10786 else
10787 {
10788 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10789 p += 4;
10790 if (((off >> 32) & 0xffff) != 0)
10791 {
10792 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10793 p += 4;
10794 }
10795 }
10796 if (((off >> 32) & 0xffffffffULL) != 0)
10797 {
10798 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10799 p += 4;
10800 }
10801 if (PPC_HI (off) != 0)
10802 {
10803 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10804 p += 4;
10805 }
10806 if (PPC_LO (off) != 0)
10807 {
10808 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10809 p += 4;
10810 }
10811 if (load)
10812 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10813 else
10814 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10815 p += 4;
10816 }
10817 return p;
10818}
10819
10820static unsigned int
10821size_offset (bfd_vma off)
10822{
10823 unsigned int size;
10824 if (off + 0x8000 < 0x10000)
10825 size = 4;
10826 else if (off + 0x80008000ULL < 0x100000000ULL)
10827 size = 8;
10828 else
10829 {
10830 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10831 size = 4;
10832 else
10833 {
10834 size = 4;
10835 if (((off >> 32) & 0xffff) != 0)
10836 size += 4;
10837 }
10838 if (((off >> 32) & 0xffffffffULL) != 0)
10839 size += 4;
10840 if (PPC_HI (off) != 0)
10841 size += 4;
10842 if (PPC_LO (off) != 0)
10843 size += 4;
10844 size += 4;
10845 }
f891966f 10846 return size + 16;
05d0e962
AM
10847}
10848
3d58e1fc
AM
10849static unsigned int
10850num_relocs_for_offset (bfd_vma off)
10851{
10852 unsigned int num_rel;
10853 if (off + 0x8000 < 0x10000)
10854 num_rel = 1;
10855 else if (off + 0x80008000ULL < 0x100000000ULL)
10856 num_rel = 2;
10857 else
10858 {
10859 num_rel = 1;
10860 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10861 && ((off >> 32) & 0xffff) != 0)
10862 num_rel += 1;
10863 if (PPC_HI (off) != 0)
10864 num_rel += 1;
10865 if (PPC_LO (off) != 0)
10866 num_rel += 1;
10867 }
10868 return num_rel;
10869}
10870
10871static Elf_Internal_Rela *
10872emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10873 bfd_vma roff, bfd_vma targ, bfd_vma off)
10874{
10875 bfd_vma relative_targ = targ - (roff - 8);
10876 if (bfd_big_endian (info->output_bfd))
10877 roff += 2;
10878 r->r_offset = roff;
10879 r->r_addend = relative_targ + roff;
10880 if (off + 0x8000 < 0x10000)
10881 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10882 else if (off + 0x80008000ULL < 0x100000000ULL)
10883 {
10884 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10885 ++r;
10886 roff += 4;
10887 r->r_offset = roff;
10888 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10889 r->r_addend = relative_targ + roff;
10890 }
10891 else
10892 {
10893 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10894 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10895 else
10896 {
10897 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10898 if (((off >> 32) & 0xffff) != 0)
10899 {
10900 ++r;
10901 roff += 4;
10902 r->r_offset = roff;
10903 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10904 r->r_addend = relative_targ + roff;
10905 }
10906 }
10907 if (((off >> 32) & 0xffffffffULL) != 0)
10908 roff += 4;
10909 if (PPC_HI (off) != 0)
10910 {
10911 ++r;
10912 roff += 4;
10913 r->r_offset = roff;
10914 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10915 r->r_addend = relative_targ + roff;
10916 }
10917 if (PPC_LO (off) != 0)
10918 {
10919 ++r;
10920 roff += 4;
10921 r->r_offset = roff;
10922 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10923 r->r_addend = relative_targ + roff;
10924 }
10925 }
10926 return r;
10927}
10928
04bdff6a 10929static bfd_byte *
7c1f4227 10930build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
0a1b45a2 10931 bool load)
04bdff6a
AM
10932{
10933 uint64_t insn;
10934 if (off - odd + (1ULL << 33) < 1ULL << 34)
10935 {
10936 off -= odd;
10937 if (odd)
10938 {
10939 bfd_put_32 (abfd, NOP, p);
10940 p += 4;
10941 }
10942 if (load)
10943 insn = PLD_R12_PC;
10944 else
10945 insn = PADDI_R12_PC;
10946 insn |= D34 (off);
10947 bfd_put_32 (abfd, insn >> 32, p);
10948 p += 4;
10949 bfd_put_32 (abfd, insn, p);
10950 }
10951 /* The minimum value for paddi is -0x200000000. The minimum value
10952 for li is -0x8000, which when shifted by 34 and added gives a
10953 minimum value of -0x2000200000000. The maximum value is
10954 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10955 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10956 {
10957 off -= 8 - odd;
10958 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10959 p += 4;
10960 if (!odd)
10961 {
10962 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10963 p += 4;
10964 }
10965 insn = PADDI_R12_PC | D34 (off);
10966 bfd_put_32 (abfd, insn >> 32, p);
10967 p += 4;
10968 bfd_put_32 (abfd, insn, p);
10969 p += 4;
10970 if (odd)
10971 {
10972 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10973 p += 4;
10974 }
10975 if (load)
10976 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10977 else
10978 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10979 }
10980 else
10981 {
10982 off -= odd + 8;
10983 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10984 p += 4;
10985 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10986 p += 4;
10987 if (odd)
10988 {
10989 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10990 p += 4;
10991 }
10992 insn = PADDI_R12_PC | D34 (off);
10993 bfd_put_32 (abfd, insn >> 32, p);
10994 p += 4;
10995 bfd_put_32 (abfd, insn, p);
10996 p += 4;
10997 if (!odd)
10998 {
10999 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
11000 p += 4;
11001 }
11002 if (load)
11003 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
11004 else
11005 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
11006 }
11007 p += 4;
11008 return p;
11009}
11010
11011static unsigned int
7c1f4227 11012size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
11013{
11014 if (off - odd + (1ULL << 33) < 1ULL << 34)
11015 return odd + 8;
11016 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11017 return 20;
11018 else
11019 return 24;
11020}
11021
11022static unsigned int
7c1f4227 11023num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
11024{
11025 if (off - odd + (1ULL << 33) < 1ULL << 34)
11026 return 1;
11027 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11028 return 2;
11029 else
11030 return 3;
11031}
11032
11033static Elf_Internal_Rela *
7c1f4227 11034emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
11035 Elf_Internal_Rela *r, bfd_vma roff,
11036 bfd_vma targ, bfd_vma off, int odd)
11037{
11038 if (off - odd + (1ULL << 33) < 1ULL << 34)
11039 roff += odd;
11040 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11041 {
11042 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11043 r->r_offset = roff + d_offset;
11044 r->r_addend = targ + 8 - odd - d_offset;
11045 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11046 ++r;
11047 roff += 8 - odd;
11048 }
11049 else
11050 {
11051 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11052 r->r_offset = roff + d_offset;
11053 r->r_addend = targ + 8 + odd - d_offset;
11054 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11055 ++r;
11056 roff += 4;
11057 r->r_offset = roff + d_offset;
11058 r->r_addend = targ + 4 + odd - d_offset;
11059 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11060 ++r;
11061 roff += 4 + odd;
11062 }
11063 r->r_offset = roff;
11064 r->r_addend = targ;
11065 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11066 return r;
11067}
11068
df136d64
AM
11069/* Emit .eh_frame opcode to advance pc by DELTA. */
11070
11071static bfd_byte *
11072eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11073{
11074 delta /= 4;
11075 if (delta < 64)
11076 *eh++ = DW_CFA_advance_loc + delta;
11077 else if (delta < 256)
11078 {
11079 *eh++ = DW_CFA_advance_loc1;
11080 *eh++ = delta;
11081 }
11082 else if (delta < 65536)
11083 {
11084 *eh++ = DW_CFA_advance_loc2;
11085 bfd_put_16 (abfd, delta, eh);
11086 eh += 2;
11087 }
11088 else
11089 {
11090 *eh++ = DW_CFA_advance_loc4;
11091 bfd_put_32 (abfd, delta, eh);
11092 eh += 4;
11093 }
11094 return eh;
11095}
11096
11097/* Size of required .eh_frame opcode to advance pc by DELTA. */
11098
11099static unsigned int
11100eh_advance_size (unsigned int delta)
11101{
11102 if (delta < 64 * 4)
11103 /* DW_CFA_advance_loc+[1..63]. */
11104 return 1;
11105 if (delta < 256 * 4)
11106 /* DW_CFA_advance_loc1, byte. */
11107 return 2;
11108 if (delta < 65536 * 4)
11109 /* DW_CFA_advance_loc2, 2 bytes. */
11110 return 3;
11111 /* DW_CFA_advance_loc4, 4 bytes. */
11112 return 5;
11113}
11114
794e51c0
AM
11115/* With power7 weakly ordered memory model, it is possible for ld.so
11116 to update a plt entry in one thread and have another thread see a
11117 stale zero toc entry. To avoid this we need some sort of acquire
11118 barrier in the call stub. One solution is to make the load of the
11119 toc word seem to appear to depend on the load of the function entry
11120 word. Another solution is to test for r2 being zero, and branch to
11121 the appropriate glink entry if so.
11122
11123 . fake dep barrier compare
71a39c98
AM
11124 . ld 12,xxx(2) ld 12,xxx(2)
11125 . mtctr 12 mtctr 12
11126 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
11127 . add 2,2,11 cmpldi 2,0
11128 . ld 2,xxx+8(2) bnectr+
11129 . bctr b <glink_entry>
11130
11131 The solution involving the compare turns out to be faster, so
11132 that's what we use unless the branch won't reach. */
11133
11134#define ALWAYS_USE_FAKE_DEP 0
11135#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 11136
794e51c0
AM
11137static inline unsigned int
11138plt_stub_size (struct ppc_link_hash_table *htab,
11139 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
11140 bfd_vma off,
11141 unsigned int odd)
794e51c0 11142{
05d0e962 11143 unsigned size;
b9e5796b 11144
7aba54da 11145 if (stub_entry->type.sub == ppc_stub_notoc)
05d0e962 11146 {
7aba54da
AM
11147 size = 8 + size_power10_offset (off, odd);
11148 if (stub_entry->type.r2save)
11149 size += 4;
11150 }
11151 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11152 {
11153 size = 8 + size_offset (off - 8);
11154 if (stub_entry->type.r2save)
05d0e962 11155 size += 4;
05d0e962 11156 }
29433886 11157 else
b9e5796b 11158 {
29433886 11159 size = 12;
7aba54da 11160 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
b9e5796b 11161 size += 4;
29433886 11162 if (PPC_HA (off) != 0)
b9e5796b 11163 size += 4;
29433886
AM
11164 if (htab->opd_abi)
11165 {
11166 size += 4;
11167 if (htab->params->plt_static_chain)
11168 size += 4;
11169 if (htab->params->plt_thread_safe
11170 && htab->elf.dynamic_sections_created
11171 && stub_entry->h != NULL
11172 && stub_entry->h->elf.dynindx != -1)
11173 size += 8;
11174 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11175 != PPC_HA (off))
11176 size += 4;
11177 }
b9e5796b 11178 }
794e51c0 11179 if (stub_entry->h != NULL
ed7007c1 11180 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11181 && htab->params->tls_get_addr_opt)
f378ab09 11182 {
29433886 11183 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11184 {
29433886 11185 size += 30 * 4;
7aba54da 11186 if (stub_entry->type.r2save)
29433886 11187 size += 4;
9e7028aa
AM
11188 }
11189 else
11190 {
29433886 11191 size += 7 * 4;
7aba54da 11192 if (stub_entry->type.r2save)
29433886 11193 size += 6 * 4;
9e7028aa 11194 }
f378ab09 11195 }
794e51c0
AM
11196 return size;
11197}
11198
2420fff6
AM
11199/* Depending on the sign of plt_stub_align:
11200 If positive, return the padding to align to a 2**plt_stub_align
11201 boundary.
11202 If negative, if this stub would cross fewer 2**plt_stub_align
11203 boundaries if we align, then return the padding needed to do so. */
11204
794e51c0 11205static inline unsigned int
0f0d9373 11206plt_stub_pad (int plt_stub_align,
2405fc40 11207 bfd_vma stub_off,
0f0d9373 11208 unsigned int stub_size)
794e51c0 11209{
0f0d9373 11210 unsigned int stub_align;
794e51c0 11211
0f0d9373
AM
11212 if (plt_stub_align >= 0)
11213 stub_align = 1u << plt_stub_align;
11214 else
2420fff6 11215 {
0f0d9373
AM
11216 stub_align = 1u << -plt_stub_align;
11217 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11218 <= ((stub_size - 1) & -stub_align))
11219 return 0;
2420fff6 11220 }
0f0d9373 11221 return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
794e51c0
AM
11222}
11223
7aba54da 11224/* Build a toc using .plt call stub. */
794e51c0
AM
11225
11226static inline bfd_byte *
11227build_plt_stub (struct ppc_link_hash_table *htab,
11228 struct ppc_stub_hash_entry *stub_entry,
11229 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11230{
e7d1c40c 11231 bfd *obfd = htab->params->stub_bfd;
0a1b45a2
AM
11232 bool plt_load_toc = htab->opd_abi;
11233 bool plt_static_chain = htab->params->plt_static_chain;
11234 bool plt_thread_safe = (htab->params->plt_thread_safe
11235 && htab->elf.dynamic_sections_created
11236 && stub_entry->h != NULL
11237 && stub_entry->h->elf.dynindx != -1);
11238 bool use_fake_dep = plt_thread_safe;
794e51c0
AM
11239 bfd_vma cmp_branch_off = 0;
11240
11241 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11242 && plt_load_toc
794e51c0 11243 && plt_thread_safe
9e51d549
AM
11244 && !(stub_entry->h != NULL
11245 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11246 && htab->params->tls_get_addr_opt))
794e51c0
AM
11247 {
11248 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11249 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11250 / PLT_ENTRY_SIZE (htab));
9e390558 11251 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11252 bfd_vma to, from;
11253
68d62958
AM
11254 if (pltindex > 32768)
11255 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11256 to = (glinkoff
11257 + htab->glink->output_offset
11258 + htab->glink->output_section->vma);
6f20ed8a 11259 from = (p - stub_entry->group->stub_sec->contents
7aba54da 11260 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0
AM
11261 + 4 * (PPC_HA (offset) != 0)
11262 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11263 != PPC_HA (offset))
11264 + 4 * (plt_static_chain != 0)
11265 + 20
6f20ed8a
AM
11266 + stub_entry->group->stub_sec->output_offset
11267 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11268 cmp_branch_off = to - from;
11269 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11270 }
11271
ac2df442
AM
11272 if (PPC_HA (offset) != 0)
11273 {
176a0d42
AM
11274 if (r != NULL)
11275 {
7aba54da 11276 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11277 r[0].r_offset += 4;
176a0d42 11278 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11279 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11280 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11281 r[1].r_addend = r[0].r_addend;
b9e5796b 11282 if (plt_load_toc)
176a0d42 11283 {
b9e5796b 11284 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11285 {
b9e5796b
AM
11286 r[2].r_offset = r[1].r_offset + 4;
11287 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11288 r[2].r_addend = r[0].r_addend;
11289 }
11290 else
11291 {
11292 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11293 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11294 r[2].r_addend = r[0].r_addend + 8;
11295 if (plt_static_chain)
11296 {
11297 r[3].r_offset = r[2].r_offset + 4;
11298 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11299 r[3].r_addend = r[0].r_addend + 16;
11300 }
c7131b65 11301 }
176a0d42
AM
11302 }
11303 }
7aba54da 11304 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11305 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11306 if (plt_load_toc)
11307 {
11308 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11309 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11310 }
11311 else
11312 {
11313 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11314 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11315 }
b9e5796b
AM
11316 if (plt_load_toc
11317 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11318 {
71a39c98 11319 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11320 offset = 0;
11321 }
71a39c98 11322 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11323 if (plt_load_toc)
794e51c0 11324 {
b9e5796b
AM
11325 if (use_fake_dep)
11326 {
11327 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11328 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11329 }
11330 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11331 if (plt_static_chain)
11332 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11333 }
ac2df442
AM
11334 }
11335 else
11336 {
176a0d42
AM
11337 if (r != NULL)
11338 {
7aba54da 11339 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11340 r[0].r_offset += 4;
176a0d42 11341 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11342 if (plt_load_toc)
176a0d42 11343 {
b9e5796b 11344 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11345 {
b9e5796b
AM
11346 r[1].r_offset = r[0].r_offset + 4;
11347 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11348 r[1].r_addend = r[0].r_addend;
11349 }
11350 else
11351 {
11352 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11353 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11354 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11355 if (plt_static_chain)
11356 {
11357 r[2].r_offset = r[1].r_offset + 4;
11358 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11359 r[2].r_addend = r[0].r_addend + 8;
11360 }
c7131b65 11361 }
176a0d42
AM
11362 }
11363 }
7aba54da 11364 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11365 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11366 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11367 if (plt_load_toc
11368 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11369 {
11370 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11371 offset = 0;
11372 }
71a39c98 11373 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11374 if (plt_load_toc)
794e51c0 11375 {
b9e5796b
AM
11376 if (use_fake_dep)
11377 {
11378 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11379 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11380 }
11381 if (plt_static_chain)
11382 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11383 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11384 }
ac2df442 11385 }
b9e5796b 11386 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11387 {
11388 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11389 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11390 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11391 }
11392 else
407aa07c 11393 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11394 return p;
11395}
11396
a7f2871e
AM
11397/* Build a special .plt call stub for __tls_get_addr. */
11398
b9ca1af6 11399#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11400#define LD_R12_0R3 0xe9830000
11401#define MR_R0_R3 0x7c601b78
b9ca1af6 11402#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11403#define ADD_R3_R12_R13 0x7c6c6a14
11404#define BEQLR 0x4d820020
11405#define MR_R3_R0 0x7c030378
a7f2871e 11406#define BCTRL 0x4e800421
a7f2871e 11407
29433886
AM
11408static bfd_byte *
11409build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11410 struct ppc_stub_hash_entry *stub_entry,
29433886 11411 bfd_byte *p)
a7f2871e 11412{
e7d1c40c 11413 bfd *obfd = htab->params->stub_bfd;
794e51c0 11414
b9ca1af6 11415 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11416 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11417 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11418 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11419 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11420 bfd_put_32 (obfd, BEQLR, p), p += 4;
11421 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11422
29433886
AM
11423 if (!htab->params->no_tls_get_addr_regsave)
11424 p = tls_get_addr_prologue (obfd, p, htab);
7aba54da 11425 else if (stub_entry->type.r2save)
29433886 11426 {
9e7028aa
AM
11427 bfd_put_32 (obfd, MFLR_R0, p);
11428 p += 4;
11429 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11430 p += 4;
29433886
AM
11431 }
11432 return p;
11433}
9e7028aa 11434
29433886
AM
11435static bfd_byte *
11436build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11437 struct ppc_stub_hash_entry *stub_entry,
11438 bfd_byte *p,
11439 bfd_byte *loc)
11440{
11441 bfd *obfd = htab->params->stub_bfd;
11442
11443 if (!htab->params->no_tls_get_addr_regsave)
11444 {
11445 bfd_put_32 (obfd, BCTRL, p - 4);
11446
7aba54da 11447 if (stub_entry->type.r2save)
29433886
AM
11448 {
11449 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11450 p += 4;
11451 }
11452 p = tls_get_addr_epilogue (obfd, p, htab);
11453 }
7aba54da 11454 else if (stub_entry->type.r2save)
29433886 11455 {
9e7028aa
AM
11456 bfd_put_32 (obfd, BCTRL, p - 4);
11457
11458 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11459 p += 4;
11460 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11461 p += 4;
11462 bfd_put_32 (obfd, MTLR_R0, p);
11463 p += 4;
11464 bfd_put_32 (obfd, BLR, p);
11465 p += 4;
11466 }
a7f2871e 11467
df136d64
AM
11468 if (htab->glink_eh_frame != NULL
11469 && htab->glink_eh_frame->size != 0)
11470 {
11471 bfd_byte *base, *eh;
df136d64
AM
11472
11473 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11474 eh = base + stub_entry->group->eh_size;
29433886
AM
11475
11476 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11477 {
29433886
AM
11478 unsigned int cfa_updt, delta, i;
11479
9e7028aa
AM
11480 /* After the bctrl, lr has been modified so we need to emit
11481 .eh_frame info saying the return address is on the stack. In
11482 fact we must put the EH info at or before the call rather
11483 than after it, because the EH info for a call needs to be
11484 specified by that point.
11485 See libgcc/unwind-dw2.c execute_cfa_program.
11486 Any stack pointer update must be described immediately after
11487 the instruction making the change, and since the stdu occurs
11488 after saving regs we put all the reg saves and the cfa
11489 change there. */
11490 cfa_updt = stub_entry->stub_offset + 18 * 4;
11491 delta = cfa_updt - stub_entry->group->lr_restore;
11492 stub_entry->group->lr_restore
11493 = stub_entry->stub_offset + (p - loc) - 4;
11494 eh = eh_advance (htab->elf.dynobj, eh, delta);
11495 *eh++ = DW_CFA_def_cfa_offset;
11496 if (htab->opd_abi)
11497 {
11498 *eh++ = 128;
11499 *eh++ = 1;
11500 }
11501 else
11502 *eh++ = 96;
11503 *eh++ = DW_CFA_offset_extended_sf;
11504 *eh++ = 65;
11505 *eh++ = (-16 / 8) & 0x7f;
11506 for (i = 4; i < 12; i++)
11507 {
11508 *eh++ = DW_CFA_offset + i;
11509 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11510 }
11511 *eh++ = (DW_CFA_advance_loc
11512 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11513 *eh++ = DW_CFA_def_cfa_offset;
11514 *eh++ = 0;
11515 for (i = 4; i < 12; i++)
11516 *eh++ = DW_CFA_restore + i;
11517 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11518 *eh++ = DW_CFA_restore_extended;
11519 *eh++ = 65;
11520 stub_entry->group->eh_size = eh - base;
11521 }
7aba54da 11522 else if (stub_entry->type.r2save)
29433886
AM
11523 {
11524 unsigned int lr_used, delta;
11525
11526 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11527 delta = lr_used - stub_entry->group->lr_restore;
11528 stub_entry->group->lr_restore = lr_used + 16;
11529 eh = eh_advance (htab->elf.dynobj, eh, delta);
11530 *eh++ = DW_CFA_offset_extended_sf;
11531 *eh++ = 65;
11532 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11533 *eh++ = DW_CFA_advance_loc + 4;
11534 *eh++ = DW_CFA_restore_extended;
11535 *eh++ = 65;
11536 stub_entry->group->eh_size = eh - base;
9e7028aa 11537 }
df136d64 11538 }
a7f2871e
AM
11539 return p;
11540}
11541
176a0d42
AM
11542static Elf_Internal_Rela *
11543get_relocs (asection *sec, int count)
11544{
11545 Elf_Internal_Rela *relocs;
11546 struct bfd_elf_section_data *elfsec_data;
11547
11548 elfsec_data = elf_section_data (sec);
11549 relocs = elfsec_data->relocs;
11550 if (relocs == NULL)
11551 {
11552 bfd_size_type relsize;
11553 relsize = sec->reloc_count * sizeof (*relocs);
11554 relocs = bfd_alloc (sec->owner, relsize);
11555 if (relocs == NULL)
11556 return NULL;
11557 elfsec_data->relocs = relocs;
d4730f92
BS
11558 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11559 sizeof (Elf_Internal_Shdr));
11560 if (elfsec_data->rela.hdr == NULL)
11561 return NULL;
11562 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11563 * sizeof (Elf64_External_Rela));
11564 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11565 sec->reloc_count = 0;
11566 }
11567 relocs += sec->reloc_count;
11568 sec->reloc_count += count;
11569 return relocs;
11570}
11571
3d58e1fc
AM
11572/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11573 forms, to the equivalent relocs against the global symbol given by
11574 STUB_ENTRY->H. */
11575
0a1b45a2 11576static bool
3d58e1fc
AM
11577use_global_in_relocs (struct ppc_link_hash_table *htab,
11578 struct ppc_stub_hash_entry *stub_entry,
11579 Elf_Internal_Rela *r, unsigned int num_rel)
11580{
11581 struct elf_link_hash_entry **hashes;
11582 unsigned long symndx;
11583 struct ppc_link_hash_entry *h;
11584 bfd_vma symval;
11585
11586 /* Relocs are always against symbols in their own object file. Fake
11587 up global sym hashes for the stub bfd (which has no symbols). */
11588 hashes = elf_sym_hashes (htab->params->stub_bfd);
11589 if (hashes == NULL)
11590 {
11591 bfd_size_type hsize;
11592
11593 /* When called the first time, stub_globals will contain the
11594 total number of symbols seen during stub sizing. After
11595 allocating, stub_globals is used as an index to fill the
11596 hashes array. */
11597 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11598 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11599 if (hashes == NULL)
0a1b45a2 11600 return false;
3d58e1fc
AM
11601 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11602 htab->stub_globals = 1;
11603 }
11604 symndx = htab->stub_globals++;
11605 h = stub_entry->h;
11606 hashes[symndx] = &h->elf;
11607 if (h->oh != NULL && h->oh->is_func)
11608 h = ppc_follow_link (h->oh);
11609 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11610 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11611 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11612 while (num_rel-- != 0)
11613 {
11614 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11615 if (h->elf.root.u.def.section != stub_entry->target_section)
11616 {
11617 /* H is an opd symbol. The addend must be zero, and the
11618 branch reloc is the only one we can convert. */
11619 r->r_addend = 0;
11620 break;
11621 }
11622 else
11623 r->r_addend -= symval;
11624 --r;
11625 }
0a1b45a2 11626 return true;
3d58e1fc
AM
11627}
11628
aa374f67 11629static bfd_vma
25f53a85 11630get_r2off (struct bfd_link_info *info,
aa374f67
AM
11631 struct ppc_stub_hash_entry *stub_entry)
11632{
25f53a85 11633 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11634 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11635
11636 if (r2off == 0)
11637 {
11638 /* Support linking -R objects. Get the toc pointer from the
11639 opd entry. */
11640 char buf[8];
b9e5796b
AM
11641 if (!htab->opd_abi)
11642 return r2off;
aa374f67
AM
11643 asection *opd = stub_entry->h->elf.root.u.def.section;
11644 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11645
11646 if (strcmp (opd->name, ".opd") != 0
11647 || opd->reloc_count != 0)
11648 {
2cdcc330
AM
11649 info->callbacks->einfo
11650 (_("%P: cannot find opd entry toc for `%pT'\n"),
11651 stub_entry->h->elf.root.root.string);
aa374f67 11652 bfd_set_error (bfd_error_bad_value);
a7c49797 11653 return (bfd_vma) -1;
aa374f67
AM
11654 }
11655 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11656 return (bfd_vma) -1;
aa374f67 11657 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11658 r2off -= elf_gp (info->output_bfd);
aa374f67 11659 }
6f20ed8a 11660 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11661 return r2off;
11662}
11663
46439c90
AM
11664/* Debug dump. */
11665
11666static void
ecc741e6
AM
11667dump_stub (const char *header,
11668 struct ppc_stub_hash_entry *stub_entry,
11669 size_t end_offset)
46439c90 11670{
ecc741e6
AM
11671 const char *t1, *t2, *t3;
11672 switch (stub_entry->type.main)
46439c90 11673 {
ecc741e6
AM
11674 case ppc_stub_none: t1 = "none"; break;
11675 case ppc_stub_long_branch: t1 = "long_branch"; break;
11676 case ppc_stub_plt_branch: t1 = "plt_branch"; break;
11677 case ppc_stub_plt_call: t1 = "plt_call"; break;
11678 case ppc_stub_global_entry: t1 = "global_entry"; break;
11679 case ppc_stub_save_res: t1 = "save_res"; break;
11680 default: t1 = "???"; break;
11681 }
11682 switch (stub_entry->type.sub)
11683 {
11684 case ppc_stub_toc: t2 = "toc"; break;
11685 case ppc_stub_notoc: t2 = "notoc"; break;
11686 case ppc_stub_p9notoc: t2 = "p9notoc"; break;
11687 default: t2 = "???"; break;
11688 }
11689 t3 = stub_entry->type.r2save ? "r2save" : "";
11690 fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
11691 header, stub_entry->id, t1, t2, t3);
11692 fprintf (stderr, "name = %s\n", stub_entry->root.string);
b8281767 11693 fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
ecc741e6
AM
11694 for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
11695 {
11696 asection *stub_sec = stub_entry->group->stub_sec;
11697 uint32_t *p = (uint32_t *) (stub_sec->contents + i);
11698 fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
46439c90 11699 }
ecc741e6 11700 fprintf (stderr, "\n");
46439c90
AM
11701}
11702
0a1b45a2 11703static bool
4ce794b7 11704ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11705{
721956f4
AM
11706 struct ppc_stub_hash_entry *stub_entry;
11707 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11708 struct bfd_link_info *info;
11709 struct ppc_link_hash_table *htab;
29433886 11710 bfd *obfd;
721956f4 11711 bfd_byte *loc;
3d58e1fc 11712 bfd_byte *p, *relp;
1aa42141 11713 bfd_vma targ, off;
176a0d42 11714 Elf_Internal_Rela *r;
e054468f 11715 asection *plt;
3d58e1fc 11716 int num_rel;
04bdff6a 11717 int odd;
0a1b45a2 11718 bool is_tga;
5d1634d7 11719
721956f4
AM
11720 /* Massage our args to the form they really have. */
11721 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11722 info = in_arg;
5d1634d7 11723
5d1634d7 11724 htab = ppc_hash_table (info);
4dfe6ac6 11725 if (htab == NULL)
0a1b45a2 11726 return false;
5d1634d7 11727
ecc741e6
AM
11728 struct _ppc64_elf_section_data *esd
11729 = ppc64_elf_section_data (stub_entry->group->stub_sec);
11730 ++htab->stub_id;
11731 if (stub_entry->id != htab->stub_id
570e911f
AM
11732 || (stub_entry->type.main != ppc_stub_save_res
11733 && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
46439c90
AM
11734 {
11735 BFD_ASSERT (0);
ecc741e6
AM
11736 if (stub_entry->id != htab->stub_id)
11737 fprintf (stderr, "Expected id %u, got %u\n",
11738 htab->stub_id, stub_entry->id);
11739 if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
b8281767
AM
11740 fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
11741 PRIx64 "\n", stub_entry->group->stub_sec->size,
ecc741e6
AM
11742 stub_entry->stub_offset);
11743 if (esd->sec_type == sec_stub)
11744 dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
11745 dump_stub ("Current:", stub_entry, 0);
46439c90 11746 }
ecc741e6
AM
11747 if (esd->sec_type == sec_normal)
11748 esd->sec_type = sec_stub;
11749 if (esd->sec_type == sec_stub)
11750 esd->u.last_ent = stub_entry;
6f20ed8a 11751 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11752
7aba54da
AM
11753 htab->stub_count[stub_entry->type.main - 1] += 1;
11754 if (stub_entry->type.main == ppc_stub_long_branch
11755 && stub_entry->type.sub == ppc_stub_toc)
5d1634d7 11756 {
721956f4 11757 /* Branches are relative. This is where we are going to. */
1aa42141 11758 targ = (stub_entry->target_value
6911b7dc
AM
11759 + stub_entry->target_section->output_offset
11760 + stub_entry->target_section->output_section->vma);
1aa42141 11761 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11762
721956f4 11763 /* And this is where we are coming from. */
1aa42141
AM
11764 off = (stub_entry->stub_offset
11765 + stub_entry->group->stub_sec->output_offset
11766 + stub_entry->group->stub_sec->output_section->vma);
11767 off = targ - off;
e86ce104 11768
9e390558 11769 p = loc;
29433886 11770 obfd = htab->params->stub_bfd;
7aba54da 11771 if (stub_entry->type.r2save)
ad8e1ba5 11772 {
25f53a85 11773 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11774
a7c49797 11775 if (r2off == (bfd_vma) -1)
aa374f67 11776 {
0a1b45a2
AM
11777 htab->stub_error = true;
11778 return false;
aa374f67 11779 }
29433886 11780 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11781 p += 4;
ac2df442
AM
11782 if (PPC_HA (r2off) != 0)
11783 {
29433886 11784 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11785 p += 4;
a7c49797
AM
11786 }
11787 if (PPC_LO (r2off) != 0)
11788 {
29433886 11789 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11790 p += 4;
ac2df442 11791 }
9e390558 11792 off -= p - loc;
ad8e1ba5 11793 }
29433886 11794 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11795 p += 4;
ad8e1ba5 11796
5c3dead3
AM
11797 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11798 {
cf97bcb0
AM
11799 _bfd_error_handler
11800 (_("long branch stub `%s' offset overflow"),
bc30df16 11801 stub_entry->root.string);
0a1b45a2
AM
11802 htab->stub_error = true;
11803 return false;
5c3dead3 11804 }
ee75fd95
AM
11805
11806 if (info->emitrelocations)
11807 {
6f20ed8a 11808 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42 11809 if (r == NULL)
0a1b45a2 11810 return false;
9e390558 11811 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11812 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11813 r->r_addend = targ;
3d58e1fc
AM
11814 if (stub_entry->h != NULL
11815 && !use_global_in_relocs (htab, stub_entry, r, 1))
0a1b45a2 11816 return false;
ee75fd95 11817 }
7aba54da
AM
11818 }
11819 else if (stub_entry->type.main == ppc_stub_plt_branch
11820 && stub_entry->type.sub == ppc_stub_toc)
11821 {
721956f4
AM
11822 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11823 stub_entry->root.string + 9,
0a1b45a2 11824 false, false);
721956f4
AM
11825 if (br_entry == NULL)
11826 {
cf97bcb0
AM
11827 _bfd_error_handler (_("can't find branch stub `%s'"),
11828 stub_entry->root.string);
0a1b45a2
AM
11829 htab->stub_error = true;
11830 return false;
721956f4
AM
11831 }
11832
1aa42141 11833 targ = (stub_entry->target_value
176a0d42
AM
11834 + stub_entry->target_section->output_offset
11835 + stub_entry->target_section->output_section->vma);
7aba54da 11836 if (!stub_entry->type.r2save)
1aa42141 11837 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11838
1aa42141 11839 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11840 htab->brlt->contents + br_entry->offset);
721956f4 11841
f94498ff 11842 if (br_entry->iter == htab->stub_iteration)
721956f4 11843 {
f94498ff 11844 br_entry->iter = 0;
84f5d08e 11845
1657026c 11846 if (htab->relbrlt != NULL && !info->enable_dt_relr)
84f5d08e 11847 {
f94498ff
AM
11848 /* Create a reloc for the branch lookup table entry. */
11849 Elf_Internal_Rela rela;
11850 bfd_byte *rl;
11851
11852 rela.r_offset = (br_entry->offset
11853 + htab->brlt->output_offset
11854 + htab->brlt->output_section->vma);
11855 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11856 rela.r_addend = targ;
f94498ff
AM
11857
11858 rl = htab->relbrlt->contents;
11859 rl += (htab->relbrlt->reloc_count++
11860 * sizeof (Elf64_External_Rela));
11861 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11862 }
11863 else if (info->emitrelocations)
11864 {
176a0d42
AM
11865 r = get_relocs (htab->brlt, 1);
11866 if (r == NULL)
0a1b45a2 11867 return false;
176a0d42
AM
11868 /* brlt, being SEC_LINKER_CREATED does not go through the
11869 normal reloc processing. Symbols and offsets are not
11870 translated from input file to output file form, so
11871 set up the offset per the output file. */
f94498ff
AM
11872 r->r_offset = (br_entry->offset
11873 + htab->brlt->output_offset
11874 + htab->brlt->output_section->vma);
11875 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11876 r->r_addend = targ;
84f5d08e 11877 }
84f5d08e 11878 }
721956f4 11879
1aa42141 11880 targ = (br_entry->offset
176a0d42
AM
11881 + htab->brlt->output_offset
11882 + htab->brlt->output_section->vma);
11883
1aa42141
AM
11884 off = (elf_gp (info->output_bfd)
11885 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11886 off = targ - off;
721956f4 11887
ad8e1ba5 11888 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11889 {
25f53a85 11890 info->callbacks->einfo
c1c8c1ef 11891 (_("%P: linkage table error against `%pT'\n"),
721956f4 11892 stub_entry->root.string);
5d1634d7 11893 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11894 htab->stub_error = true;
11895 return false;
5d1634d7 11896 }
41bd81ab 11897
176a0d42
AM
11898 if (info->emitrelocations)
11899 {
6f20ed8a 11900 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42 11901 if (r == NULL)
0a1b45a2 11902 return false;
6f20ed8a 11903 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11904 if (bfd_big_endian (info->output_bfd))
11905 r[0].r_offset += 2;
7aba54da 11906 if (stub_entry->type.r2save)
176a0d42
AM
11907 r[0].r_offset += 4;
11908 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11909 r[0].r_addend = targ;
176a0d42
AM
11910 if (PPC_HA (off) != 0)
11911 {
11912 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11913 r[1].r_offset = r[0].r_offset + 4;
11914 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11915 r[1].r_addend = r[0].r_addend;
11916 }
11917 }
11918
9e390558 11919 p = loc;
29433886 11920 obfd = htab->params->stub_bfd;
7aba54da 11921 if (!stub_entry->type.r2save)
ad8e1ba5 11922 {
176a0d42 11923 if (PPC_HA (off) != 0)
ac2df442 11924 {
29433886 11925 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11926 p += 4;
29433886 11927 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11928 }
11929 else
29433886 11930 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11931 }
11932 else
11933 {
25f53a85 11934 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11935
a7c49797 11936 if (r2off == (bfd_vma) -1)
aa374f67 11937 {
0a1b45a2
AM
11938 htab->stub_error = true;
11939 return false;
aa374f67 11940 }
ad8e1ba5 11941
29433886 11942 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11943 p += 4;
176a0d42 11944 if (PPC_HA (off) != 0)
ac2df442 11945 {
29433886 11946 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11947 p += 4;
29433886 11948 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11949 }
11950 else
29433886 11951 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11952
11953 if (PPC_HA (r2off) != 0)
11954 {
9e390558 11955 p += 4;
29433886 11956 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11957 }
11958 if (PPC_LO (r2off) != 0)
11959 {
9e390558 11960 p += 4;
29433886 11961 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11962 }
ad8e1ba5 11963 }
9e390558 11964 p += 4;
29433886 11965 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11966 p += 4;
29433886 11967 bfd_put_32 (obfd, BCTR, p);
407aa07c 11968 p += 4;
7aba54da
AM
11969 }
11970 else if (stub_entry->type.sub >= ppc_stub_notoc)
11971 {
11972 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
05d0e962 11973 p = loc;
f891966f 11974 off = (stub_entry->stub_offset
05d0e962
AM
11975 + stub_entry->group->stub_sec->output_offset
11976 + stub_entry->group->stub_sec->output_section->vma);
29433886 11977 obfd = htab->params->stub_bfd;
7aba54da 11978 is_tga = (is_plt
12cf8b93 11979 && stub_entry->h != NULL
29433886
AM
11980 && is_tls_get_addr (&stub_entry->h->elf, htab)
11981 && htab->params->tls_get_addr_opt);
11982 if (is_tga)
11983 {
11984 p = build_tls_get_addr_head (htab, stub_entry, p);
11985 off += p - loc;
11986 }
7aba54da 11987 if (stub_entry->type.r2save)
05d0e962
AM
11988 {
11989 off += 4;
29433886 11990 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11991 p += 4;
11992 }
7aba54da 11993 if (is_plt)
05d0e962
AM
11994 {
11995 targ = stub_entry->plt_ent->plt.offset & ~1;
11996 if (targ >= (bfd_vma) -2)
11997 abort ();
11998
11999 plt = htab->elf.splt;
30845f11 12000 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
12001 {
12002 if (stub_entry->symtype == STT_GNU_IFUNC)
12003 plt = htab->elf.iplt;
12004 else
12005 plt = htab->pltlocal;
12006 }
12007 targ += plt->output_offset + plt->output_section->vma;
12008 }
12009 else
12010 targ = (stub_entry->target_value
12011 + stub_entry->target_section->output_offset
12012 + stub_entry->target_section->output_section->vma);
04bdff6a 12013 odd = off & 4;
05d0e962 12014 off = targ - off;
f891966f 12015
3d58e1fc
AM
12016 relp = p;
12017 num_rel = 0;
7aba54da
AM
12018 if (stub_entry->type.sub == ppc_stub_notoc)
12019 p = build_power10_offset (obfd, p, off, odd, is_plt);
04bdff6a
AM
12020 else
12021 {
29433886
AM
12022 if (htab->glink_eh_frame != NULL
12023 && htab->glink_eh_frame->size != 0)
12024 {
12025 bfd_byte *base, *eh;
12026 unsigned int lr_used, delta;
12027
12028 base = (htab->glink_eh_frame->contents
12029 + stub_entry->group->eh_base + 17);
12030 eh = base + stub_entry->group->eh_size;
12031 lr_used = stub_entry->stub_offset + (p - loc) + 8;
12032 delta = lr_used - stub_entry->group->lr_restore;
12033 stub_entry->group->lr_restore = lr_used + 8;
12034 eh = eh_advance (htab->elf.dynobj, eh, delta);
12035 *eh++ = DW_CFA_register;
12036 *eh++ = 65;
12037 *eh++ = 12;
12038 *eh++ = DW_CFA_advance_loc + 2;
12039 *eh++ = DW_CFA_restore_extended;
12040 *eh++ = 65;
12041 stub_entry->group->eh_size = eh - base;
12042 }
12043
04bdff6a
AM
12044 /* The notoc stubs calculate their target (either a PLT entry or
12045 the global entry point of a function) relative to the PC
12046 returned by the "bcl" two instructions past the start of the
12047 sequence emitted by build_offset. The offset is therefore 8
12048 less than calculated from the start of the sequence. */
12049 off -= 8;
7aba54da 12050 p = build_offset (obfd, p, off, is_plt);
04bdff6a
AM
12051 }
12052
7aba54da 12053 if (stub_entry->type.main == ppc_stub_long_branch)
05d0e962 12054 {
f891966f 12055 bfd_vma from;
3d58e1fc 12056 num_rel = 1;
f891966f
AM
12057 from = (stub_entry->stub_offset
12058 + stub_entry->group->stub_sec->output_offset
12059 + stub_entry->group->stub_sec->output_section->vma
12060 + (p - loc));
29433886 12061 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
12062 }
12063 else
12064 {
29433886 12065 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 12066 p += 4;
29433886 12067 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
12068 }
12069 p += 4;
df136d64 12070
29433886
AM
12071 if (is_tga)
12072 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12073
3d58e1fc
AM
12074 if (info->emitrelocations)
12075 {
04bdff6a 12076 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
7aba54da 12077 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12078 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12079 else
12080 {
12081 num_rel += num_relocs_for_offset (off);
12082 roff += 16;
12083 }
3d58e1fc
AM
12084 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12085 if (r == NULL)
0a1b45a2 12086 return false;
7aba54da 12087 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12088 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
12089 else
12090 r = emit_relocs_for_offset (info, r, roff, targ, off);
7aba54da 12091 if (stub_entry->type.main == ppc_stub_long_branch)
3d58e1fc
AM
12092 {
12093 ++r;
12094 roff = p - 4 - stub_entry->group->stub_sec->contents;
12095 r->r_offset = roff;
12096 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12097 r->r_addend = targ;
12098 if (stub_entry->h != NULL
12099 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
0a1b45a2 12100 return false;
3d58e1fc
AM
12101 }
12102 }
7aba54da
AM
12103 }
12104 else if (stub_entry->type.main == ppc_stub_plt_call)
12105 {
e054468f 12106 if (stub_entry->h != NULL
b31867b6
AM
12107 && stub_entry->h->is_func_descriptor
12108 && stub_entry->h->oh != NULL)
c862ae31 12109 {
b31867b6
AM
12110 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12111
12112 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 12113 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
12114 if (fh->elf.root.type == bfd_link_hash_undefined
12115 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12116 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 12117 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
12118 }
12119
721956f4 12120 /* Now build the stub. */
1aa42141
AM
12121 targ = stub_entry->plt_ent->plt.offset & ~1;
12122 if (targ >= (bfd_vma) -2)
721956f4
AM
12123 abort ();
12124
33e44f2e 12125 plt = htab->elf.splt;
30845f11 12126 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
12127 {
12128 if (stub_entry->symtype == STT_GNU_IFUNC)
12129 plt = htab->elf.iplt;
12130 else
12131 plt = htab->pltlocal;
12132 }
1aa42141 12133 targ += plt->output_offset + plt->output_section->vma;
e054468f 12134
1aa42141
AM
12135 off = (elf_gp (info->output_bfd)
12136 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12137 off = targ - off;
721956f4 12138
ad8e1ba5 12139 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 12140 {
25f53a85 12141 info->callbacks->einfo
695344c0 12142 /* xgettext:c-format */
c1c8c1ef 12143 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
12144 stub_entry->h != NULL
12145 ? stub_entry->h->elf.root.root.string
12146 : "<local sym>");
721956f4 12147 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
12148 htab->stub_error = true;
12149 return false;
721956f4
AM
12150 }
12151
176a0d42
AM
12152 r = NULL;
12153 if (info->emitrelocations)
12154 {
6f20ed8a 12155 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
12156 ((PPC_HA (off) != 0)
12157 + (htab->opd_abi
e7d1c40c 12158 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
12159 && PPC_HA (off + 16) == PPC_HA (off))
12160 : 1)));
176a0d42 12161 if (r == NULL)
0a1b45a2 12162 return false;
6f20ed8a 12163 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
12164 if (bfd_big_endian (info->output_bfd))
12165 r[0].r_offset += 2;
1aa42141 12166 r[0].r_addend = targ;
176a0d42 12167 }
29433886
AM
12168 p = loc;
12169 obfd = htab->params->stub_bfd;
12170 is_tga = (stub_entry->h != NULL
12171 && is_tls_get_addr (&stub_entry->h->elf, htab)
12172 && htab->params->tls_get_addr_opt);
12173 if (is_tga)
12174 {
12175 p = build_tls_get_addr_head (htab, stub_entry, p);
12176 if (r != NULL)
12177 r[0].r_offset += p - loc;
12178 }
12179 p = build_plt_stub (htab, stub_entry, p, off, r);
12180 if (is_tga)
12181 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
7aba54da
AM
12182 }
12183 else if (stub_entry->type.main == ppc_stub_save_res)
12184 return true;
12185 else
12186 {
721956f4 12187 BFD_FAIL ();
0a1b45a2 12188 return false;
721956f4
AM
12189 }
12190
1aa42141 12191 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 12192
e7d1c40c 12193 if (htab->params->emit_stub_syms)
97b639ba
AM
12194 {
12195 struct elf_link_hash_entry *h;
ee75fd95
AM
12196 size_t len1, len2;
12197 char *name;
12198 const char *const stub_str[] = { "long_branch",
05d0e962 12199 "plt_branch",
ee75fd95
AM
12200 "plt_call" };
12201
7aba54da 12202 len1 = strlen (stub_str[stub_entry->type.main - 1]);
ee75fd95
AM
12203 len2 = strlen (stub_entry->root.string);
12204 name = bfd_malloc (len1 + len2 + 2);
12205 if (name == NULL)
0a1b45a2 12206 return false;
ee75fd95 12207 memcpy (name, stub_entry->root.string, 9);
7aba54da 12208 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
ee75fd95 12209 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
0a1b45a2 12210 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
97b639ba 12211 if (h == NULL)
0a1b45a2 12212 return false;
97b639ba
AM
12213 if (h->root.type == bfd_link_hash_new)
12214 {
12215 h->root.type = bfd_link_hash_defined;
6f20ed8a 12216 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 12217 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
12218 h->ref_regular = 1;
12219 h->def_regular = 1;
12220 h->ref_regular_nonweak = 1;
12221 h->forced_local = 1;
12222 h->non_elf = 0;
2ec55de3 12223 h->root.linker_def = 1;
97b639ba
AM
12224 }
12225 }
12226
0a1b45a2 12227 return true;
721956f4
AM
12228}
12229
12230/* As above, but don't actually build the stub. Just bump offset so
12231 we know stub section sizes, and select plt_branch stubs where
12232 long_branch stubs won't do. */
12233
0a1b45a2 12234static bool
4ce794b7 12235ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
12236{
12237 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 12238 struct bfd_link_info *info;
721956f4 12239 struct ppc_link_hash_table *htab;
f891966f
AM
12240 asection *plt;
12241 bfd_vma targ, off, r2off;
0f0d9373 12242 unsigned int size, pad, extra, lr_used, delta, odd;
0441f94f 12243 bfd_vma stub_offset;
721956f4
AM
12244
12245 /* Massage our args to the form they really have. */
12246 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
12247 info = in_arg;
12248
12249 htab = ppc_hash_table (info);
4dfe6ac6 12250 if (htab == NULL)
0a1b45a2 12251 return false;
721956f4 12252
abf874aa
CL
12253 /* Fail if the target section could not be assigned to an output
12254 section. The user should fix his linker script. */
12255 if (stub_entry->target_section != NULL
12256 && stub_entry->target_section->output_section == NULL
12257 && info->non_contiguous_regions)
c63d4862 12258 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
53215f21
CL
12259 "Retry without --enable-non-contiguous-regions.\n"),
12260 stub_entry->target_section);
abf874aa
CL
12261
12262 /* Same for the group. */
12263 if (stub_entry->group->stub_sec != NULL
12264 && stub_entry->group->stub_sec->output_section == NULL
12265 && info->non_contiguous_regions)
c63d4862
AM
12266 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
12267 "Retry without --enable-non-contiguous-regions.\n"),
12268 stub_entry->group->stub_sec);
abf874aa 12269
1aa42141 12270 /* Make a note of the offset within the stubs for this entry. */
0441f94f 12271 stub_offset = stub_entry->group->stub_sec->size;
2405fc40
AM
12272 if (htab->stub_iteration > STUB_SHRINK_ITER
12273 && stub_entry->stub_offset > stub_offset)
12274 stub_offset = stub_entry->stub_offset;
ecc741e6 12275 stub_entry->id = ++htab->stub_id;
1aa42141 12276
a4b6fadd
AM
12277 if (stub_entry->h != NULL
12278 && stub_entry->h->save_res
12279 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12280 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12281 {
12282 /* Don't make stubs to out-of-line register save/restore
12283 functions. Instead, emit copies of the functions. */
12284 stub_entry->group->needs_save_res = 1;
7aba54da
AM
12285 stub_entry->type.main = ppc_stub_save_res;
12286 stub_entry->type.sub = ppc_stub_toc;
12287 stub_entry->type.r2save = 0;
0a1b45a2 12288 return true;
a4b6fadd
AM
12289 }
12290
7aba54da 12291 if (stub_entry->type.main == ppc_stub_plt_branch)
721956f4 12292 {
f891966f
AM
12293 /* Reset the stub type from the plt branch variant in case we now
12294 can reach with a shorter stub. */
04cb922a 12295 stub_entry->type.main = ppc_stub_long_branch;
7aba54da
AM
12296 }
12297
12298 if (stub_entry->type.main == ppc_stub_long_branch
12299 && stub_entry->type.sub == ppc_stub_toc)
12300 {
1aa42141
AM
12301 targ = (stub_entry->target_value
12302 + stub_entry->target_section->output_offset
12303 + stub_entry->target_section->output_section->vma);
f891966f 12304 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
0441f94f 12305 off = (stub_offset
1aa42141
AM
12306 + stub_entry->group->stub_sec->output_offset
12307 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12308
ad8e1ba5 12309 size = 4;
f891966f 12310 r2off = 0;
7aba54da 12311 if (stub_entry->type.r2save)
ad8e1ba5 12312 {
25f53a85 12313 r2off = get_r2off (info, stub_entry);
a7c49797 12314 if (r2off == (bfd_vma) -1)
aa374f67 12315 {
0a1b45a2
AM
12316 htab->stub_error = true;
12317 return false;
aa374f67 12318 }
a7c49797 12319 size = 8;
ac2df442 12320 if (PPC_HA (r2off) != 0)
a7c49797
AM
12321 size += 4;
12322 if (PPC_LO (r2off) != 0)
12323 size += 4;
1aa42141 12324 off += size - 4;
ad8e1ba5 12325 }
1aa42141 12326 off = targ - off;
ad8e1ba5 12327
f891966f
AM
12328 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12329 Do the same for -R objects without function descriptors. */
7aba54da 12330 if ((stub_entry->type.r2save
f891966f
AM
12331 && r2off == 0
12332 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12333 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12334 {
f891966f 12335 struct ppc_branch_hash_entry *br_entry;
df136d64 12336
f891966f
AM
12337 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12338 stub_entry->root.string + 9,
0a1b45a2 12339 true, false);
f891966f 12340 if (br_entry == NULL)
721956f4 12341 {
f891966f
AM
12342 _bfd_error_handler (_("can't build branch stub `%s'"),
12343 stub_entry->root.string);
0a1b45a2
AM
12344 htab->stub_error = true;
12345 return false;
721956f4
AM
12346 }
12347
f891966f 12348 if (br_entry->iter != htab->stub_iteration)
721956f4 12349 {
f891966f
AM
12350 br_entry->iter = htab->stub_iteration;
12351 br_entry->offset = htab->brlt->size;
12352 htab->brlt->size += 8;
63bc6f6c 12353
1657026c 12354 if (htab->relbrlt != NULL && !info->enable_dt_relr)
f891966f
AM
12355 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12356 else if (info->emitrelocations)
84f5d08e 12357 {
f891966f
AM
12358 htab->brlt->reloc_count += 1;
12359 htab->brlt->flags |= SEC_RELOC;
05d0e962 12360 }
f891966f 12361 }
ac2df442 12362
f891966f
AM
12363 targ = (br_entry->offset
12364 + htab->brlt->output_offset
12365 + htab->brlt->output_section->vma);
12366 off = (elf_gp (info->output_bfd)
12367 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12368 off = targ - off;
176a0d42 12369
f891966f
AM
12370 if (info->emitrelocations)
12371 {
12372 stub_entry->group->stub_sec->reloc_count
12373 += 1 + (PPC_HA (off) != 0);
12374 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12375 }
05d0e962 12376
04cb922a 12377 stub_entry->type.main = ppc_stub_plt_branch;
7aba54da 12378 if (!stub_entry->type.r2save)
f891966f
AM
12379 {
12380 size = 12;
12381 if (PPC_HA (off) != 0)
12382 size = 16;
ac2df442 12383 }
f891966f 12384 else
ac2df442 12385 {
f891966f
AM
12386 size = 16;
12387 if (PPC_HA (off) != 0)
12388 size += 4;
12389
12390 if (PPC_HA (r2off) != 0)
12391 size += 4;
12392 if (PPC_LO (r2off) != 0)
12393 size += 4;
ac2df442 12394 }
f40bb390
AM
12395 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12396 stub_offset += pad;
721956f4 12397 }
f891966f
AM
12398 else if (info->emitrelocations)
12399 {
12400 stub_entry->group->stub_sec->reloc_count += 1;
12401 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12402 }
7aba54da
AM
12403 }
12404 else if (stub_entry->type.main == ppc_stub_long_branch)
12405 {
0441f94f 12406 off = (stub_offset
f891966f
AM
12407 + stub_entry->group->stub_sec->output_offset
12408 + stub_entry->group->stub_sec->output_section->vma);
12409 size = 0;
7aba54da 12410 if (stub_entry->type.r2save)
f891966f
AM
12411 size = 4;
12412 off += size;
12413 targ = (stub_entry->target_value
12414 + stub_entry->target_section->output_offset
12415 + stub_entry->target_section->output_section->vma);
04bdff6a 12416 odd = off & 4;
f891966f
AM
12417 off = targ - off;
12418
f40bb390
AM
12419 if (stub_entry->type.sub == ppc_stub_notoc)
12420 extra = size_power10_offset (off, odd);
12421 else
12422 extra = size_offset (off - 8);
12423 /* Include branch insn plus those in the offset sequence. */
12424 size += 4 + extra;
12425
12426 /* If the branch can't reach, use a plt_branch.
12427 The branch insn is at the end, or "extra" bytes along. So
12428 its offset will be "extra" bytes less that that already
12429 calculated. */
12430 if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
12431 {
12432 stub_entry->type.main = ppc_stub_plt_branch;
12433 size += 4;
12434 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12435 if (pad != 0)
12436 {
12437 stub_offset += pad;
12438 off -= pad;
12439 odd ^= pad & 4;
12440 size -= extra;
12441 if (stub_entry->type.sub == ppc_stub_notoc)
12442 extra = size_power10_offset (off, odd);
12443 else
12444 extra = size_offset (off - 8);
12445 size += extra;
12446 }
12447 }
12448 else if (info->emitrelocations)
12449 stub_entry->group->stub_sec->reloc_count +=1;
12450
3d58e1fc
AM
12451 if (info->emitrelocations)
12452 {
04bdff6a 12453 unsigned int num_rel;
7aba54da 12454 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12455 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12456 else
12457 num_rel = num_relocs_for_offset (off - 8);
12458 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12459 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12460 }
12461
7aba54da 12462 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12463 {
12464 /* After the bcl, lr has been modified so we need to emit
12465 .eh_frame info saying the return address is in r12. */
0441f94f 12466 lr_used = stub_offset + 8;
7aba54da 12467 if (stub_entry->type.r2save)
04bdff6a
AM
12468 lr_used += 4;
12469 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12470 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12471 DW_CFA_restore_extended 65. */
12472 delta = lr_used - stub_entry->group->lr_restore;
12473 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12474 stub_entry->group->lr_restore = lr_used + 8;
12475 }
7aba54da
AM
12476 }
12477 else if (stub_entry->type.sub >= ppc_stub_notoc)
12478 {
12479 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
29433886 12480 lr_used = 0;
12cf8b93
AM
12481 if (stub_entry->h != NULL
12482 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12483 && htab->params->tls_get_addr_opt)
12484 {
12485 lr_used += 7 * 4;
12486 if (!htab->params->no_tls_get_addr_regsave)
12487 lr_used += 11 * 4;
7aba54da 12488 else if (stub_entry->type.r2save)
29433886
AM
12489 lr_used += 2 * 4;
12490 }
7aba54da 12491 if (stub_entry->type.r2save)
29433886 12492 lr_used += 4;
f891966f
AM
12493 targ = stub_entry->plt_ent->plt.offset & ~1;
12494 if (targ >= (bfd_vma) -2)
12495 abort ();
12496
12497 plt = htab->elf.splt;
30845f11 12498 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12499 {
12500 if (stub_entry->symtype == STT_GNU_IFUNC)
12501 plt = htab->elf.iplt;
12502 else
12503 plt = htab->pltlocal;
12504 }
12505 targ += plt->output_offset + plt->output_section->vma;
0441f94f 12506 off = (stub_offset
29433886
AM
12507 + stub_entry->group->stub_sec->output_offset
12508 + stub_entry->group->stub_sec->output_section->vma
12509 + lr_used);
04bdff6a 12510 odd = off & 4;
f891966f
AM
12511 off = targ - off;
12512
0f0d9373
AM
12513 size = plt_stub_size (htab, stub_entry, off, odd);
12514 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12515 if (pad != 0)
f891966f 12516 {
2405fc40 12517 stub_offset += pad;
f891966f 12518 off -= pad;
29433886 12519 odd ^= pad & 4;
0f0d9373 12520 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f
AM
12521 }
12522
3d58e1fc
AM
12523 if (info->emitrelocations)
12524 {
04bdff6a 12525 unsigned int num_rel;
7aba54da 12526 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12527 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12528 else
12529 num_rel = num_relocs_for_offset (off - 8);
12530 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12531 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12532 }
12533
7aba54da 12534 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12535 {
12536 /* After the bcl, lr has been modified so we need to emit
12537 .eh_frame info saying the return address is in r12. */
0441f94f 12538 lr_used += stub_offset + 8;
04bdff6a
AM
12539 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12540 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12541 DW_CFA_restore_extended 65. */
12542 delta = lr_used - stub_entry->group->lr_restore;
12543 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12544 stub_entry->group->lr_restore = lr_used + 8;
12545 }
7aba54da 12546 if (stub_entry->h != NULL
29433886
AM
12547 && is_tls_get_addr (&stub_entry->h->elf, htab)
12548 && htab->params->tls_get_addr_opt)
12549 {
12550 if (!htab->params->no_tls_get_addr_regsave)
12551 {
0441f94f 12552 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12553 delta = cfa_updt - stub_entry->group->lr_restore;
12554 stub_entry->group->eh_size += eh_advance_size (delta);
12555 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
0441f94f 12556 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886 12557 }
7aba54da 12558 else if (stub_entry->type.r2save)
29433886 12559 {
0441f94f 12560 lr_used = stub_offset + size - 20;
29433886
AM
12561 delta = lr_used - stub_entry->group->lr_restore;
12562 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
0441f94f 12563 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886
AM
12564 }
12565 }
7aba54da
AM
12566 }
12567 else if (stub_entry->type.main == ppc_stub_plt_call)
12568 {
f891966f
AM
12569 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12570 if (targ >= (bfd_vma) -2)
12571 abort ();
12572 plt = htab->elf.splt;
30845f11 12573 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12574 {
12575 if (stub_entry->symtype == STT_GNU_IFUNC)
12576 plt = htab->elf.iplt;
12577 else
12578 plt = htab->pltlocal;
12579 }
12580 targ += plt->output_offset + plt->output_section->vma;
12581
12582 off = (elf_gp (info->output_bfd)
12583 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12584 off = targ - off;
12585
0f0d9373
AM
12586 size = plt_stub_size (htab, stub_entry, off, 0);
12587 pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
12588 stub_offset += pad;
f891966f
AM
12589
12590 if (info->emitrelocations)
12591 {
12592 stub_entry->group->stub_sec->reloc_count
12593 += ((PPC_HA (off) != 0)
12594 + (htab->opd_abi
12595 ? 2 + (htab->params->plt_static_chain
12596 && PPC_HA (off + 16) == PPC_HA (off))
12597 : 1));
12598 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12599 }
12600
f891966f 12601 if (stub_entry->h != NULL
ed7007c1 12602 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f 12603 && htab->params->tls_get_addr_opt
7aba54da 12604 && stub_entry->type.r2save)
f891966f 12605 {
29433886
AM
12606 if (!htab->params->no_tls_get_addr_regsave)
12607 {
12608 /* Adjustments to r1 need to be described. */
0441f94f 12609 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12610 delta = cfa_updt - stub_entry->group->lr_restore;
12611 stub_entry->group->eh_size += eh_advance_size (delta);
12612 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12613 }
12614 else
9e7028aa 12615 {
0441f94f 12616 lr_used = stub_offset + size - 20;
9e7028aa
AM
12617 /* The eh_frame info will consist of a DW_CFA_advance_loc
12618 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12619 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12620 delta = lr_used - stub_entry->group->lr_restore;
12621 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12622 }
0441f94f 12623 stub_entry->group->lr_restore = stub_offset + size - 4;
f891966f 12624 }
7aba54da
AM
12625 }
12626 else
12627 {
f891966f 12628 BFD_FAIL ();
0a1b45a2 12629 return false;
721956f4
AM
12630 }
12631
0441f94f
AM
12632 if (stub_entry->stub_offset != stub_offset)
12633 htab->stub_changed = true;
2405fc40
AM
12634 stub_entry->stub_offset = stub_offset;
12635 stub_entry->group->stub_sec->size = stub_offset + size;
0a1b45a2 12636 return true;
721956f4
AM
12637}
12638
12639/* Set up various things so that we can make a list of input sections
12640 for each output section included in the link. Returns -1 on error,
cedb70c5 12641 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12642
12643int
e7d1c40c 12644ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12645{
6f20ed8a 12646 unsigned int id;
986f0783 12647 size_t amt;
721956f4
AM
12648 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12649
4dfe6ac6
NC
12650 if (htab == NULL)
12651 return -1;
4c52953f 12652
7cf7fcc8 12653 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12654 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12655 htab->sec_info = bfd_zmalloc (amt);
12656 if (htab->sec_info == NULL)
721956f4
AM
12657 return -1;
12658
3d6f9012
AM
12659 /* Set toc_off for com, und, abs and ind sections. */
12660 for (id = 0; id < 3; id++)
6f20ed8a 12661 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12662
721956f4
AM
12663 return 1;
12664}
12665
927be08e
AM
12666/* Set up for first pass at multitoc partitioning. */
12667
12668void
12669ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12670{
12671 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12672
1c865ab2 12673 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12674 htab->toc_bfd = NULL;
12675 htab->toc_first_sec = NULL;
12676}
12677
e717da7e
AM
12678/* The linker repeatedly calls this function for each TOC input section
12679 and linker generated GOT section. Group input bfds such that the toc
927be08e 12680 within a group is less than 64k in size. */
ad8e1ba5 12681
0a1b45a2 12682bool
4ce794b7 12683ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12684{
12685 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12686 bfd_vma addr, off, limit;
ad8e1ba5 12687
4dfe6ac6 12688 if (htab == NULL)
0a1b45a2 12689 return false;
4dfe6ac6 12690
927be08e 12691 if (!htab->second_toc_pass)
4c52953f 12692 {
927be08e 12693 /* Keep track of the first .toc or .got section for this input bfd. */
0a1b45a2 12694 bool new_bfd = htab->toc_bfd != isec->owner;
a4fd3de5
AM
12695
12696 if (new_bfd)
bf102f86
AM
12697 {
12698 htab->toc_bfd = isec->owner;
12699 htab->toc_first_sec = isec;
12700 }
927be08e 12701
bf102f86
AM
12702 addr = isec->output_offset + isec->output_section->vma;
12703 off = addr - htab->toc_curr;
d77c8a4b
AM
12704 limit = 0x80008000;
12705 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12706 limit = 0x10000;
12707 if (off + isec->size > limit)
bf102f86
AM
12708 {
12709 addr = (htab->toc_first_sec->output_offset
12710 + htab->toc_first_sec->output_section->vma);
12711 htab->toc_curr = addr;
a27e685f 12712 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12713 }
99877b66 12714
927be08e
AM
12715 /* toc_curr is the base address of this toc group. Set elf_gp
12716 for the input section to be the offset relative to the
12717 output toc base plus 0x8000. Making the input elf_gp an
12718 offset allows us to move the toc as a whole without
12719 recalculating input elf_gp. */
06bcf541 12720 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12721 off += TOC_BASE_OFF;
12722
12723 /* Die if someone uses a linker script that doesn't keep input
12724 file .toc and .got together. */
a4fd3de5
AM
12725 if (new_bfd
12726 && elf_gp (isec->owner) != 0
927be08e 12727 && elf_gp (isec->owner) != off)
0a1b45a2 12728 return false;
927be08e
AM
12729
12730 elf_gp (isec->owner) = off;
0a1b45a2 12731 return true;
4c52953f 12732 }
927be08e
AM
12733
12734 /* During the second pass toc_first_sec points to the start of
12735 a toc group, and toc_curr is used to track the old elf_gp.
12736 We use toc_bfd to ensure we only look at each bfd once. */
12737 if (htab->toc_bfd == isec->owner)
0a1b45a2 12738 return true;
927be08e
AM
12739 htab->toc_bfd = isec->owner;
12740
12741 if (htab->toc_first_sec == NULL
12742 || htab->toc_curr != elf_gp (isec->owner))
12743 {
12744 htab->toc_curr = elf_gp (isec->owner);
12745 htab->toc_first_sec = isec;
12746 }
12747 addr = (htab->toc_first_sec->output_offset
12748 + htab->toc_first_sec->output_section->vma);
06bcf541 12749 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12750 elf_gp (isec->owner) = off;
12751
0a1b45a2 12752 return true;
ad8e1ba5
AM
12753}
12754
927be08e
AM
12755/* Called via elf_link_hash_traverse to merge GOT entries for global
12756 symbol H. */
12757
0a1b45a2 12758static bool
927be08e
AM
12759merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12760{
12761 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12762 return true;
927be08e 12763
927be08e
AM
12764 merge_got_entries (&h->got.glist);
12765
0a1b45a2 12766 return true;
927be08e
AM
12767}
12768
12769/* Called via elf_link_hash_traverse to allocate GOT entries for global
12770 symbol H. */
12771
0a1b45a2 12772static bool
927be08e
AM
12773reallocate_got (struct elf_link_hash_entry *h, void *inf)
12774{
12775 struct got_entry *gent;
12776
12777 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12778 return true;
927be08e 12779
927be08e
AM
12780 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12781 if (!gent->is_indirect)
12782 allocate_got (h, (struct bfd_link_info *) inf, gent);
0a1b45a2 12783 return true;
927be08e
AM
12784}
12785
12786/* Called on the first multitoc pass after the last call to
12787 ppc64_elf_next_toc_section. This function removes duplicate GOT
12788 entries. */
12789
0a1b45a2 12790bool
927be08e 12791ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12792{
12793 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e 12794 struct bfd *ibfd, *ibfd2;
0a1b45a2 12795 bool done_something;
927be08e
AM
12796
12797 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12798
7865406b 12799 if (!htab->do_multi_toc)
0a1b45a2 12800 return false;
7865406b 12801
d0fae19d 12802 /* Merge global sym got entries within a toc group. */
927be08e
AM
12803 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12804
12805 /* And tlsld_got. */
c72f2fb2 12806 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12807 {
12808 struct got_entry *ent, *ent2;
12809
12810 if (!is_ppc64_elf (ibfd))
12811 continue;
12812
12813 ent = ppc64_tlsld_got (ibfd);
12814 if (!ent->is_indirect
12815 && ent->got.offset != (bfd_vma) -1)
12816 {
c72f2fb2 12817 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12818 {
12819 if (!is_ppc64_elf (ibfd2))
12820 continue;
12821
12822 ent2 = ppc64_tlsld_got (ibfd2);
12823 if (!ent2->is_indirect
12824 && ent2->got.offset != (bfd_vma) -1
12825 && elf_gp (ibfd2) == elf_gp (ibfd))
12826 {
0a1b45a2 12827 ent2->is_indirect = true;
927be08e
AM
12828 ent2->got.ent = ent;
12829 }
12830 }
12831 }
12832 }
12833
12834 /* Zap sizes of got sections. */
33e44f2e
AM
12835 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12836 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12837 htab->got_reli_size = 0;
12838
c72f2fb2 12839 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12840 {
12841 asection *got, *relgot;
12842
12843 if (!is_ppc64_elf (ibfd))
12844 continue;
12845
12846 got = ppc64_elf_tdata (ibfd)->got;
12847 if (got != NULL)
12848 {
12849 got->rawsize = got->size;
12850 got->size = 0;
12851 relgot = ppc64_elf_tdata (ibfd)->relgot;
12852 relgot->rawsize = relgot->size;
12853 relgot->size = 0;
12854 }
12855 }
12856
12857 /* Now reallocate the got, local syms first. We don't need to
12858 allocate section contents again since we never increase size. */
c72f2fb2 12859 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12860 {
12861 struct got_entry **lgot_ents;
12862 struct got_entry **end_lgot_ents;
12863 struct plt_entry **local_plt;
12864 struct plt_entry **end_local_plt;
f961d9dd 12865 unsigned char *lgot_masks;
927be08e
AM
12866 bfd_size_type locsymcount;
12867 Elf_Internal_Shdr *symtab_hdr;
19e08130 12868 asection *s;
3a3a4c1f
AM
12869 Elf_Internal_Sym *local_syms;
12870 Elf_Internal_Sym *isym;
927be08e
AM
12871
12872 if (!is_ppc64_elf (ibfd))
12873 continue;
12874
12875 lgot_ents = elf_local_got_ents (ibfd);
12876 if (!lgot_ents)
12877 continue;
12878
12879 symtab_hdr = &elf_symtab_hdr (ibfd);
12880 locsymcount = symtab_hdr->sh_info;
12881 end_lgot_ents = lgot_ents + locsymcount;
12882 local_plt = (struct plt_entry **) end_lgot_ents;
12883 end_local_plt = local_plt + locsymcount;
f961d9dd 12884 lgot_masks = (unsigned char *) end_local_plt;
c9fecd62
AM
12885 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12886 if (local_syms == NULL && locsymcount != 0)
3a3a4c1f 12887 {
c9fecd62
AM
12888 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12889 0, NULL, NULL, NULL);
12890 if (local_syms == NULL)
12891 return false;
3a3a4c1f 12892 }
927be08e 12893 s = ppc64_elf_tdata (ibfd)->got;
3a3a4c1f
AM
12894 for (isym = local_syms;
12895 lgot_ents < end_lgot_ents;
c9fecd62 12896 ++lgot_ents, ++lgot_masks, isym++)
927be08e
AM
12897 {
12898 struct got_entry *ent;
12899
12900 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12901 {
19e08130
AM
12902 unsigned int ent_size = 8;
12903 unsigned int rel_size = sizeof (Elf64_External_Rela);
12904
d0fae19d
AM
12905 ent->got.offset = s->size;
12906 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12907 {
19e08130
AM
12908 ent_size *= 2;
12909 rel_size *= 2;
12910 }
12911 s->size += ent_size;
37da22e5 12912 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12913 {
33e44f2e 12914 htab->elf.irelplt->size += rel_size;
19e08130
AM
12915 htab->got_reli_size += rel_size;
12916 }
6a3858a6 12917 else if (bfd_link_pic (info)
8f22c953
AM
12918 && (ent->tls_type == 0
12919 ? !info->enable_dt_relr
3a3a4c1f
AM
12920 : !bfd_link_executable (info))
12921 && isym->st_shndx != SHN_ABS)
19e08130
AM
12922 {
12923 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12924 srel->size += rel_size;
d0fae19d
AM
12925 }
12926 }
927be08e
AM
12927 }
12928 }
12929
12930 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12931
c72f2fb2 12932 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12933 {
12934 struct got_entry *ent;
12935
12936 if (!is_ppc64_elf (ibfd))
12937 continue;
12938
12939 ent = ppc64_tlsld_got (ibfd);
12940 if (!ent->is_indirect
12941 && ent->got.offset != (bfd_vma) -1)
12942 {
12943 asection *s = ppc64_elf_tdata (ibfd)->got;
12944 ent->got.offset = s->size;
12945 s->size += 16;
f749f26e 12946 if (bfd_link_dll (info))
927be08e
AM
12947 {
12948 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12949 srel->size += sizeof (Elf64_External_Rela);
12950 }
12951 }
12952 }
12953
33e44f2e 12954 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12955 if (!done_something)
c72f2fb2 12956 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12957 {
12958 asection *got;
12959
12960 if (!is_ppc64_elf (ibfd))
12961 continue;
12962
12963 got = ppc64_elf_tdata (ibfd)->got;
12964 if (got != NULL)
12965 {
12966 done_something = got->rawsize != got->size;
12967 if (done_something)
12968 break;
12969 }
12970 }
12971
12972 if (done_something)
e7d1c40c 12973 (*htab->params->layout_sections_again) ();
927be08e
AM
12974
12975 /* Set up for second pass over toc sections to recalculate elf_gp
12976 on input sections. */
12977 htab->toc_bfd = NULL;
12978 htab->toc_first_sec = NULL;
0a1b45a2 12979 htab->second_toc_pass = true;
927be08e
AM
12980 return done_something;
12981}
12982
12983/* Called after second pass of multitoc partitioning. */
12984
12985void
12986ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12987{
12988 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12989
12990 /* After the second pass, toc_curr tracks the TOC offset used
12991 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12992 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12993}
12994
9b5ecbd0
AM
12995/* No toc references were found in ISEC. If the code in ISEC makes no
12996 calls, then there's no need to use toc adjusting stubs when branching
12997 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12998 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12999 needed, and 2 if a cyclical call-graph was found but no other reason
13000 for a stub was detected. If called from the top level, a return of
13001 2 means the same as a return of 0. */
9b5ecbd0
AM
13002
13003static int
4ce794b7 13004toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 13005{
9b5ecbd0 13006 int ret;
70cc837d
AM
13007
13008 /* Mark this section as checked. */
13009 isec->call_check_done = 1;
9b5ecbd0 13010
772119ce
AM
13011 /* We know none of our code bearing sections will need toc stubs. */
13012 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13013 return 0;
13014
eea6121a 13015 if (isec->size == 0)
082c50f8
AM
13016 return 0;
13017
4c52953f
AM
13018 if (isec->output_section == NULL)
13019 return 0;
13020
4c52953f 13021 ret = 0;
70cc837d 13022 if (isec->reloc_count != 0)
9b5ecbd0 13023 {
70cc837d
AM
13024 Elf_Internal_Rela *relstart, *rel;
13025 Elf_Internal_Sym *local_syms;
13026 struct ppc_link_hash_table *htab;
2917689a 13027
70cc837d
AM
13028 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
13029 info->keep_memory);
13030 if (relstart == NULL)
13031 return -1;
90aecf7a 13032
70cc837d
AM
13033 /* Look for branches to outside of this section. */
13034 local_syms = NULL;
13035 htab = ppc_hash_table (info);
13036 if (htab == NULL)
13037 return -1;
4c52953f 13038
70cc837d 13039 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 13040 {
70cc837d
AM
13041 enum elf_ppc64_reloc_type r_type;
13042 unsigned long r_symndx;
13043 struct elf_link_hash_entry *h;
13044 struct ppc_link_hash_entry *eh;
13045 Elf_Internal_Sym *sym;
13046 asection *sym_sec;
13047 struct _opd_sec_data *opd;
13048 bfd_vma sym_value;
13049 bfd_vma dest;
13050
13051 r_type = ELF64_R_TYPE (rel->r_info);
13052 if (r_type != R_PPC64_REL24
05d0e962 13053 && r_type != R_PPC64_REL24_NOTOC
7aba54da 13054 && r_type != R_PPC64_REL24_P9NOTOC
70cc837d
AM
13055 && r_type != R_PPC64_REL14
13056 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 13057 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
13058 && r_type != R_PPC64_PLTCALL
13059 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 13060 continue;
4c52953f 13061
70cc837d
AM
13062 r_symndx = ELF64_R_SYM (rel->r_info);
13063 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13064 isec->owner))
4c52953f 13065 {
70cc837d
AM
13066 ret = -1;
13067 break;
13068 }
4c52953f 13069
70cc837d
AM
13070 /* Calls to dynamic lib functions go through a plt call stub
13071 that uses r2. */
ed7007c1 13072 eh = ppc_elf_hash_entry (h);
70cc837d
AM
13073 if (eh != NULL
13074 && (eh->elf.plt.plist != NULL
13075 || (eh->oh != NULL
13076 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13077 {
13078 ret = 1;
13079 break;
4c52953f
AM
13080 }
13081
70cc837d
AM
13082 if (sym_sec == NULL)
13083 /* Ignore other undefined symbols. */
4c52953f 13084 continue;
4c52953f 13085
70cc837d
AM
13086 /* Assume branches to other sections not included in the
13087 link need stubs too, to cover -R and absolute syms. */
13088 if (sym_sec->output_section == NULL)
13089 {
13090 ret = 1;
13091 break;
13092 }
4c52953f 13093
70cc837d
AM
13094 if (h == NULL)
13095 sym_value = sym->st_value;
13096 else
13097 {
13098 if (h->root.type != bfd_link_hash_defined
13099 && h->root.type != bfd_link_hash_defweak)
13100 abort ();
13101 sym_value = h->root.u.def.value;
13102 }
13103 sym_value += rel->r_addend;
4c52953f 13104
70cc837d
AM
13105 /* If this branch reloc uses an opd sym, find the code section. */
13106 opd = get_opd_info (sym_sec);
13107 if (opd != NULL)
13108 {
13109 if (h == NULL && opd->adjust != NULL)
13110 {
13111 long adjust;
4c52953f 13112
92a9c616 13113 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
13114 if (adjust == -1)
13115 /* Assume deleted functions won't ever be called. */
13116 continue;
13117 sym_value += adjust;
13118 }
4c52953f 13119
aef36ac1 13120 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13121 &sym_sec, NULL, false);
70cc837d
AM
13122 if (dest == (bfd_vma) -1)
13123 continue;
13124 }
13125 else
13126 dest = (sym_value
13127 + sym_sec->output_offset
13128 + sym_sec->output_section->vma);
4c52953f 13129
70cc837d
AM
13130 /* Ignore branch to self. */
13131 if (sym_sec == isec)
13132 continue;
4c52953f 13133
70cc837d
AM
13134 /* If the called function uses the toc, we need a stub. */
13135 if (sym_sec->has_toc_reloc
13136 || sym_sec->makes_toc_func_call)
4c52953f 13137 {
70cc837d 13138 ret = 1;
4c52953f
AM
13139 break;
13140 }
70cc837d
AM
13141
13142 /* Assume any branch that needs a long branch stub might in fact
13143 need a plt_branch stub. A plt_branch stub uses r2. */
13144 else if (dest - (isec->output_offset
13145 + isec->output_section->vma
6911b7dc
AM
13146 + rel->r_offset) + (1 << 25)
13147 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13148 ? h->other
13149 : sym->st_other))
4c52953f 13150 {
70cc837d
AM
13151 ret = 1;
13152 break;
13153 }
13154
13155 /* If calling back to a section in the process of being
13156 tested, we can't say for sure that no toc adjusting stubs
13157 are needed, so don't return zero. */
13158 else if (sym_sec->call_check_in_progress)
13159 ret = 2;
13160
13161 /* Branches to another section that itself doesn't have any TOC
13162 references are OK. Recursively call ourselves to check. */
13163 else if (!sym_sec->call_check_done)
13164 {
13165 int recur;
13166
13167 /* Mark current section as indeterminate, so that other
13168 sections that call back to current won't be marked as
13169 known. */
13170 isec->call_check_in_progress = 1;
13171 recur = toc_adjusting_stub_needed (info, sym_sec);
13172 isec->call_check_in_progress = 0;
13173
4c52953f
AM
13174 if (recur != 0)
13175 {
70cc837d
AM
13176 ret = recur;
13177 if (recur != 2)
13178 break;
4c52953f
AM
13179 }
13180 }
4c52953f 13181 }
70cc837d 13182
c9594989
AM
13183 if (elf_symtab_hdr (isec->owner).contents
13184 != (unsigned char *) local_syms)
70cc837d
AM
13185 free (local_syms);
13186 if (elf_section_data (isec)->relocs != relstart)
13187 free (relstart);
9b5ecbd0
AM
13188 }
13189
70cc837d
AM
13190 if ((ret & 1) == 0
13191 && isec->map_head.s != NULL
13192 && (strcmp (isec->output_section->name, ".init") == 0
13193 || strcmp (isec->output_section->name, ".fini") == 0))
13194 {
13195 if (isec->map_head.s->has_toc_reloc
13196 || isec->map_head.s->makes_toc_func_call)
13197 ret = 1;
13198 else if (!isec->map_head.s->call_check_done)
13199 {
13200 int recur;
13201 isec->call_check_in_progress = 1;
13202 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13203 isec->call_check_in_progress = 0;
13204 if (recur != 0)
13205 ret = recur;
13206 }
13207 }
13208
13209 if (ret == 1)
13210 isec->makes_toc_func_call = 1;
4c52953f 13211
9b5ecbd0
AM
13212 return ret;
13213}
13214
721956f4
AM
13215/* The linker repeatedly calls this function for each input section,
13216 in the order that input sections are linked into output sections.
13217 Build lists of input sections to determine groupings between which
13218 we may insert linker stubs. */
13219
0a1b45a2 13220bool
4ce794b7 13221ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
13222{
13223 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13224
4dfe6ac6 13225 if (htab == NULL)
0a1b45a2 13226 return false;
4dfe6ac6 13227
734b6cf9 13228 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 13229 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 13230 {
3d6f9012
AM
13231 /* This happens to make the list in reverse order,
13232 which is what we want. */
6f20ed8a
AM
13233 htab->sec_info[isec->id].u.list
13234 = htab->sec_info[isec->output_section->id].u.list;
13235 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 13236 }
ad8e1ba5 13237
4c52953f 13238 if (htab->multi_toc_needed)
9b5ecbd0 13239 {
8b974ba3
AM
13240 /* Analyse sections that aren't already flagged as needing a
13241 valid toc pointer. Exclude .fixup for the linux kernel.
13242 .fixup contains branches, but only back to the function that
13243 hit an exception. */
13244 if (!(isec->has_toc_reloc
13245 || (isec->flags & SEC_CODE) == 0
13246 || strcmp (isec->name, ".fixup") == 0
13247 || isec->call_check_done))
13248 {
13249 if (toc_adjusting_stub_needed (info, isec) < 0)
0a1b45a2 13250 return false;
8b974ba3
AM
13251 }
13252 /* Make all sections use the TOC assigned for this object file.
13253 This will be wrong for pasted sections; We fix that in
13254 check_pasted_section(). */
13255 if (elf_gp (isec->owner) != 0)
13256 htab->toc_curr = elf_gp (isec->owner);
13257 }
13258
6f20ed8a 13259 htab->sec_info[isec->id].toc_off = htab->toc_curr;
0a1b45a2 13260 return true;
721956f4
AM
13261}
13262
70cc837d
AM
13263/* Check that all .init and .fini sections use the same toc, if they
13264 have toc relocs. */
13265
0a1b45a2 13266static bool
70cc837d
AM
13267check_pasted_section (struct bfd_link_info *info, const char *name)
13268{
13269 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13270
13271 if (o != NULL)
13272 {
13273 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13274 bfd_vma toc_off = 0;
13275 asection *i;
13276
13277 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13278 if (i->has_toc_reloc)
13279 {
13280 if (toc_off == 0)
6f20ed8a
AM
13281 toc_off = htab->sec_info[i->id].toc_off;
13282 else if (toc_off != htab->sec_info[i->id].toc_off)
0a1b45a2 13283 return false;
70cc837d 13284 }
6683a28d
AM
13285
13286 if (toc_off == 0)
13287 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13288 if (i->makes_toc_func_call)
13289 {
6f20ed8a 13290 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13291 break;
13292 }
13293
70cc837d
AM
13294 /* Make sure the whole pasted function uses the same toc offset. */
13295 if (toc_off != 0)
13296 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13297 htab->sec_info[i->id].toc_off = toc_off;
70cc837d 13298 }
0a1b45a2 13299 return true;
70cc837d
AM
13300}
13301
0a1b45a2 13302bool
70cc837d
AM
13303ppc64_elf_check_init_fini (struct bfd_link_info *info)
13304{
0c80c644
SM
13305 bool ret1 = check_pasted_section (info, ".init");
13306 bool ret2 = check_pasted_section (info, ".fini");
13307
13308 return ret1 && ret2;
70cc837d
AM
13309}
13310
721956f4
AM
13311/* See whether we can group stub sections together. Grouping stub
13312 sections may result in fewer stubs. More importantly, we need to
13313 put all .init* and .fini* stubs at the beginning of the .init or
13314 .fini output sections respectively, because glibc splits the
13315 _init and _fini functions into multiple parts. Putting a stub in
13316 the middle of a function is not a good idea. */
13317
0a1b45a2 13318static bool
6f20ed8a 13319group_sections (struct bfd_link_info *info,
4ce794b7 13320 bfd_size_type stub_group_size,
0a1b45a2 13321 bool stubs_always_before_branch)
721956f4 13322{
6f20ed8a
AM
13323 struct ppc_link_hash_table *htab;
13324 asection *osec;
0a1b45a2 13325 bool suppress_size_errors;
7c8fe5c4 13326
6f20ed8a
AM
13327 htab = ppc_hash_table (info);
13328 if (htab == NULL)
0a1b45a2 13329 return false;
6f20ed8a 13330
0a1b45a2 13331 suppress_size_errors = false;
7c8fe5c4
AM
13332 if (stub_group_size == 1)
13333 {
13334 /* Default values. */
13335 if (stubs_always_before_branch)
09f92717 13336 stub_group_size = 0x1e00000;
7c8fe5c4 13337 else
09f92717 13338 stub_group_size = 0x1c00000;
0a1b45a2 13339 suppress_size_errors = true;
7c8fe5c4
AM
13340 }
13341
6f20ed8a 13342 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13343 {
6f20ed8a
AM
13344 asection *tail;
13345
13346 if (osec->id >= htab->sec_info_arr_size)
13347 continue;
13348
13349 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13350 while (tail != NULL)
721956f4 13351 {
734b6cf9
AM
13352 asection *curr;
13353 asection *prev;
13354 bfd_size_type total;
0a1b45a2 13355 bool big_sec;
734b6cf9 13356 bfd_vma curr_toc;
6f20ed8a 13357 struct map_stub *group;
09f92717 13358 bfd_size_type group_size;
734b6cf9
AM
13359
13360 curr = tail;
eea6121a 13361 total = tail->size;
09f92717
AM
13362 group_size = (ppc64_elf_section_data (tail) != NULL
13363 && ppc64_elf_section_data (tail)->has_14bit_branch
13364 ? stub_group_size >> 10 : stub_group_size);
13365
13366 big_sec = total > group_size;
7c8fe5c4 13367 if (big_sec && !suppress_size_errors)
695344c0 13368 /* xgettext:c-format */
871b3ab2 13369 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13370 tail->owner, tail);
6f20ed8a 13371 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13372
6f20ed8a 13373 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13374 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13375 < (ppc64_elf_section_data (prev) != NULL
13376 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13377 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13378 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13379 curr = prev;
13380
13381 /* OK, the size from the start of CURR to the end is less
09f92717 13382 than group_size and thus can be handled by one stub
734b6cf9 13383 section. (or the tail section is itself larger than
09f92717
AM
13384 group_size, in which case we may be toast.) We should
13385 really be keeping track of the total size of stubs added
13386 here, as stubs contribute to the final output section
13387 size. That's a little tricky, and this way will only
13388 break if stubs added make the total size more than 2^25,
13389 ie. for the default stub_group_size, if stubs total more
13390 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13391 group = bfd_alloc (curr->owner, sizeof (*group));
13392 if (group == NULL)
0a1b45a2 13393 return false;
6f20ed8a
AM
13394 group->link_sec = curr;
13395 group->stub_sec = NULL;
a4b6fadd 13396 group->needs_save_res = 0;
df136d64
AM
13397 group->lr_restore = 0;
13398 group->eh_size = 0;
13399 group->eh_base = 0;
a4b6fadd
AM
13400 group->next = htab->group;
13401 htab->group = group;
734b6cf9 13402 do
721956f4 13403 {
6f20ed8a 13404 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13405 /* Set up this stub group. */
6f20ed8a 13406 htab->sec_info[tail->id].u.group = group;
721956f4 13407 }
734b6cf9
AM
13408 while (tail != curr && (tail = prev) != NULL);
13409
09f92717 13410 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13411 bytes before the stub section can be handled by it too.
13412 Don't do this if we have a really large section after the
13413 stubs, as adding more stubs increases the chance that
13414 branches may not reach into the stub section. */
13415 if (!stubs_always_before_branch && !big_sec)
13416 {
13417 total = 0;
13418 while (prev != NULL
13419 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13420 < (ppc64_elf_section_data (prev) != NULL
13421 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13422 ? (group_size = stub_group_size >> 10)
13423 : group_size))
6f20ed8a 13424 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13425 {
13426 tail = prev;
6f20ed8a
AM
13427 prev = htab->sec_info[tail->id].u.list;
13428 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13429 }
13430 }
13431 tail = prev;
721956f4
AM
13432 }
13433 }
0a1b45a2 13434 return true;
721956f4
AM
13435}
13436
58d180e8
AM
13437static const unsigned char glink_eh_frame_cie[] =
13438{
13439 0, 0, 0, 16, /* length. */
13440 0, 0, 0, 0, /* id. */
13441 1, /* CIE version. */
13442 'z', 'R', 0, /* Augmentation string. */
13443 4, /* Code alignment. */
13444 0x78, /* Data alignment. */
13445 65, /* RA reg. */
13446 1, /* Augmentation size. */
13447 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13448 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13449};
13450
d969d15f
AM
13451/* Stripping output sections is normally done before dynamic section
13452 symbols have been allocated. This function is called later, and
13453 handles cases like htab->brlt which is mapped to its own output
13454 section. */
13455
13456static void
13457maybe_strip_output (struct bfd_link_info *info, asection *isec)
13458{
13459 if (isec->size == 0
13460 && isec->output_section->size == 0
53d8967a 13461 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13462 && !bfd_section_removed_from_list (info->output_bfd,
13463 isec->output_section)
13464 && elf_section_data (isec->output_section)->dynindx == 0)
13465 {
13466 isec->output_section->flags |= SEC_EXCLUDE;
13467 bfd_section_list_remove (info->output_bfd, isec->output_section);
13468 info->output_bfd->section_count--;
13469 }
13470}
13471
0aac2413
AM
13472/* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13473 the array of such relocs. */
1657026c
AM
13474
13475static bool
0aac2413 13476append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
1657026c
AM
13477{
13478 if (htab->relr_count >= htab->relr_alloc)
13479 {
13480 if (htab->relr_alloc == 0)
13481 htab->relr_alloc = 4096;
13482 else
13483 htab->relr_alloc *= 2;
0aac2413
AM
13484 htab->relr = bfd_realloc (htab->relr,
13485 htab->relr_alloc * sizeof (*htab->relr));
13486 if (htab->relr == NULL)
1657026c
AM
13487 return false;
13488 }
0aac2413
AM
13489 htab->relr[htab->relr_count].sec = sec;
13490 htab->relr[htab->relr_count].off = off;
13491 htab->relr_count++;
1657026c
AM
13492 return true;
13493}
13494
0aac2413
AM
13495/* qsort comparator for bfd_vma args. */
13496
13497static int
13498compare_relr_address (const void *arg1, const void *arg2)
13499{
13500 bfd_vma a = *(bfd_vma *) arg1;
13501 bfd_vma b = *(bfd_vma *) arg2;
13502 return a < b ? -1 : a > b ? 1 : 0;
13503}
13504
13505/* Produce a malloc'd sorted array of reloc addresses from the info
13506 stored by append_relr_off. */
13507
13508static bfd_vma *
13509sort_relr (struct ppc_link_hash_table *htab)
13510{
13511 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13512 if (addr == NULL)
13513 return NULL;
13514
13515 for (size_t i = 0; i < htab->relr_count; i++)
13516 addr[i] = (htab->relr[i].sec->output_section->vma
13517 + htab->relr[i].sec->output_offset
13518 + htab->relr[i].off);
13519
13520 if (htab->relr_count > 1)
13521 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13522
13523 return addr;
13524}
13525
13526/* Look over GOT and PLT entries saved on elf_local_got_ents for all
e26ff4b5 13527 input files, stashing info about needed relative relocs. */
0aac2413 13528
1657026c
AM
13529static bool
13530got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13531{
13532 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13533 bfd *ibfd;
13534
13535 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13536 {
13537 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13538 struct plt_entry **local_plt, **lplt, **end_local_plt;
13539 Elf_Internal_Shdr *symtab_hdr;
13540 bfd_size_type locsymcount;
3a3a4c1f
AM
13541 Elf_Internal_Sym *local_syms;
13542 Elf_Internal_Sym *isym;
1657026c
AM
13543 struct plt_entry *pent;
13544 struct got_entry *gent;
13545
13546 if (!is_ppc64_elf (ibfd))
13547 continue;
13548
13549 lgot_ents = elf_local_got_ents (ibfd);
13550 if (!lgot_ents)
13551 continue;
13552
13553 symtab_hdr = &elf_symtab_hdr (ibfd);
13554 locsymcount = symtab_hdr->sh_info;
3a3a4c1f
AM
13555 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13556 if (local_syms == NULL && locsymcount != 0)
13557 {
13558 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13559 0, NULL, NULL, NULL);
13560 if (local_syms == NULL)
13561 return false;
13562 }
1657026c
AM
13563 end_lgot_ents = lgot_ents + locsymcount;
13564 local_plt = (struct plt_entry **) end_lgot_ents;
13565 end_local_plt = local_plt + locsymcount;
3a3a4c1f
AM
13566 for (lgot = lgot_ents, isym = local_syms;
13567 lgot < end_lgot_ents;
13568 ++lgot, ++isym)
1657026c
AM
13569 for (gent = *lgot; gent != NULL; gent = gent->next)
13570 if (!gent->is_indirect
13571 && gent->tls_type == 0
3a3a4c1f
AM
13572 && gent->got.offset != (bfd_vma) -1
13573 && isym->st_shndx != SHN_ABS)
1657026c
AM
13574 {
13575 asection *got = ppc64_elf_tdata (gent->owner)->got;
0aac2413 13576 if (!append_relr_off (htab, got, gent->got.offset))
1657026c
AM
13577 {
13578 htab->stub_error = true;
13579 return false;
13580 }
13581 }
13582
13583 if (!htab->opd_abi)
3a3a4c1f
AM
13584 for (lplt = local_plt, isym = local_syms;
13585 lplt < end_local_plt;
13586 ++lplt, ++isym)
1657026c 13587 for (pent = *lplt; pent != NULL; pent = pent->next)
3a3a4c1f
AM
13588 if (pent->plt.offset != (bfd_vma) -1
13589 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
1657026c 13590 {
0aac2413 13591 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
1657026c 13592 {
1657026c
AM
13593 if (symtab_hdr->contents != (unsigned char *) local_syms)
13594 free (local_syms);
13595 return false;
13596 }
1657026c
AM
13597 }
13598
13599 if (local_syms != NULL
13600 && symtab_hdr->contents != (unsigned char *) local_syms)
13601 {
13602 if (!info->keep_memory)
13603 free (local_syms);
13604 else
13605 symtab_hdr->contents = (unsigned char *) local_syms;
13606 }
13607 }
13608 return true;
13609}
13610
0aac2413
AM
13611/* Stash info about needed GOT and PLT entry relative relocs for
13612 global symbol H. */
13613
1657026c
AM
13614static bool
13615got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13616{
13617 struct bfd_link_info *info;
13618 struct ppc_link_hash_table *htab;
13619 struct plt_entry *pent;
13620 struct got_entry *gent;
13621
13622 if (h->root.type == bfd_link_hash_indirect)
13623 return true;
13624
13625 info = (struct bfd_link_info *) inf;
13626 htab = ppc_hash_table (info);
13627 if (htab == NULL)
13628 return false;
13629
13630 if (h->type != STT_GNU_IFUNC
13631 && h->def_regular
13632 && (h->root.type == bfd_link_hash_defined
13633 || h->root.type == bfd_link_hash_defweak))
13634 {
3a3a4c1f
AM
13635 if ((!htab->elf.dynamic_sections_created
13636 || h->dynindx == -1
13637 || SYMBOL_REFERENCES_LOCAL (info, h))
13638 && !bfd_is_abs_symbol (&h->root))
1657026c
AM
13639 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13640 if (!gent->is_indirect
13641 && gent->tls_type == 0
13642 && gent->got.offset != (bfd_vma) -1)
13643 {
13644 asection *got = ppc64_elf_tdata (gent->owner)->got;
0aac2413 13645 if (!append_relr_off (htab, got, gent->got.offset))
1657026c
AM
13646 {
13647 htab->stub_error = true;
13648 return false;
13649 }
13650 }
13651
13652 if (!htab->opd_abi
13653 && use_local_plt (info, h))
13654 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13655 if (pent->plt.offset != (bfd_vma) -1)
13656 {
0aac2413 13657 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
1657026c
AM
13658 {
13659 htab->stub_error = true;
13660 return false;
13661 }
13662 }
13663 }
13664 return true;
13665}
13666
721956f4
AM
13667/* Determine and set the size of the stub section for a final link.
13668
13669 The basic idea here is to examine all the relocations looking for
13670 PC-relative calls to a target that is unreachable with a "bl"
13671 instruction. */
13672
0a1b45a2 13673bool
e7d1c40c 13674ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13675{
13676 bfd_size_type stub_group_size;
0a1b45a2 13677 bool stubs_always_before_branch;
721956f4
AM
13678 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13679
4dfe6ac6 13680 if (htab == NULL)
0a1b45a2 13681 return false;
4dfe6ac6 13682
e10a07b3
AM
13683 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13684 htab->params->power10_stubs = 0;
13685
0e1862bb 13686 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13687 htab->params->plt_thread_safe = 1;
b9e5796b 13688 if (!htab->opd_abi)
e7d1c40c
AM
13689 htab->params->plt_thread_safe = 0;
13690 else if (htab->params->plt_thread_safe == -1)
794e51c0 13691 {
e2458743 13692 static const char *const thread_starter[] =
794e51c0
AM
13693 {
13694 "pthread_create",
13695 /* libstdc++ */
13696 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13697 /* librt */
13698 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13699 "mq_notify", "create_timer",
13700 /* libanl */
13701 "getaddrinfo_a",
13702 /* libgomp */
2300b5a1 13703 "GOMP_parallel",
794e51c0 13704 "GOMP_parallel_start",
2300b5a1 13705 "GOMP_parallel_loop_static",
794e51c0 13706 "GOMP_parallel_loop_static_start",
2300b5a1 13707 "GOMP_parallel_loop_dynamic",
794e51c0 13708 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13709 "GOMP_parallel_loop_guided",
794e51c0 13710 "GOMP_parallel_loop_guided_start",
2300b5a1 13711 "GOMP_parallel_loop_runtime",
794e51c0 13712 "GOMP_parallel_loop_runtime_start",
2300b5a1 13713 "GOMP_parallel_sections",
68ffbac6 13714 "GOMP_parallel_sections_start",
f9dffbf0
AM
13715 /* libgo */
13716 "__go_go",
794e51c0
AM
13717 };
13718 unsigned i;
13719
a4b6fadd 13720 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13721 {
13722 struct elf_link_hash_entry *h;
13723 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
0a1b45a2 13724 false, false, true);
e7d1c40c
AM
13725 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13726 if (htab->params->plt_thread_safe)
794e51c0
AM
13727 break;
13728 }
13729 }
e7d1c40c
AM
13730 stubs_always_before_branch = htab->params->group_size < 0;
13731 if (htab->params->group_size < 0)
13732 stub_group_size = -htab->params->group_size;
721956f4 13733 else
e7d1c40c 13734 stub_group_size = htab->params->group_size;
721956f4 13735
6f20ed8a 13736 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
0a1b45a2 13737 return false;
721956f4 13738
a804e476
AM
13739 htab->tga_group = NULL;
13740 if (!htab->params->no_tls_get_addr_regsave
13741 && htab->tga_desc_fd != NULL
13742 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13743 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13744 && htab->tls_get_addr_fd != NULL
13745 && is_static_defined (&htab->tls_get_addr_fd->elf))
13746 {
13747 asection *sym_sec, *code_sec, *stub_sec;
13748 bfd_vma sym_value;
13749 struct _opd_sec_data *opd;
13750
13751 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13752 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13753 code_sec = sym_sec;
13754 opd = get_opd_info (sym_sec);
13755 if (opd != NULL)
0a1b45a2 13756 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
a804e476
AM
13757 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13758 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13759 htab->tga_group->link_sec);
13760 if (stub_sec == NULL)
0a1b45a2 13761 return false;
a804e476
AM
13762 htab->tga_group->stub_sec = stub_sec;
13763
13764 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13765 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13766 htab->tga_desc_fd->elf.root.u.def.value = 0;
13767 htab->tga_desc_fd->elf.type = STT_FUNC;
13768 htab->tga_desc_fd->elf.def_regular = 1;
13769 htab->tga_desc_fd->elf.non_elf = 0;
0a1b45a2 13770 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
a804e476
AM
13771 }
13772
c9301e31 13773 /* Loop until no stubs added. After iteration 20 of this loop we may
0441f94f 13774 exit on a stub section shrinking. */
c9301e31 13775
721956f4
AM
13776 while (1)
13777 {
13778 bfd *input_bfd;
13779 unsigned int bfd_indx;
a4b6fadd 13780 struct map_stub *group;
721956f4
AM
13781
13782 htab->stub_iteration += 1;
1657026c 13783 htab->relr_count = 0;
721956f4
AM
13784
13785 for (input_bfd = info->input_bfds, bfd_indx = 0;
13786 input_bfd != NULL;
c72f2fb2 13787 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13788 {
13789 Elf_Internal_Shdr *symtab_hdr;
13790 asection *section;
6cdc0ccc 13791 Elf_Internal_Sym *local_syms = NULL;
721956f4 13792
0c8d6e5c 13793 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13794 continue;
13795
721956f4 13796 /* We'll need the symbol table in a second. */
0ffa91dd 13797 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13798 if (symtab_hdr->sh_info == 0)
13799 continue;
13800
721956f4
AM
13801 /* Walk over each section attached to the input bfd. */
13802 for (section = input_bfd->sections;
13803 section != NULL;
13804 section = section->next)
13805 {
721956f4 13806 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1657026c 13807 bool is_opd;
721956f4
AM
13808
13809 /* If there aren't any relocs, then there's nothing more
13810 to do. */
13811 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13812 || (section->flags & SEC_ALLOC) == 0
13813 || (section->flags & SEC_LOAD) == 0
721956f4
AM
13814 || section->reloc_count == 0)
13815 continue;
13816
1657026c
AM
13817 if (!info->enable_dt_relr
13818 && (section->flags & SEC_CODE) == 0)
13819 continue;
13820
721956f4
AM
13821 /* If this section is a link-once section that will be
13822 discarded, then don't create any stubs. */
13823 if (section->output_section == NULL
927be08e 13824 || section->output_section->owner != info->output_bfd)
721956f4
AM
13825 continue;
13826
1e2f5b6e
AM
13827 /* Get the relocs. */
13828 internal_relocs
4ce794b7 13829 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13830 info->keep_memory);
721956f4 13831 if (internal_relocs == NULL)
1e2f5b6e 13832 goto error_ret_free_local;
721956f4 13833
1657026c
AM
13834 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13835
721956f4
AM
13836 /* Now examine each relocation. */
13837 irela = internal_relocs;
13838 irelaend = irela + section->reloc_count;
13839 for (; irela < irelaend; irela++)
13840 {
4ce794b7
AM
13841 enum elf_ppc64_reloc_type r_type;
13842 unsigned int r_indx;
7aba54da 13843 struct ppc_stub_type stub_type;
721956f4 13844 struct ppc_stub_hash_entry *stub_entry;
8387904d 13845 asection *sym_sec, *code_sec;
e054468f 13846 bfd_vma sym_value, code_value;
721956f4 13847 bfd_vma destination;
6911b7dc 13848 unsigned long local_off;
0a1b45a2 13849 bool ok_dest;
721956f4 13850 struct ppc_link_hash_entry *hash;
8387904d 13851 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13852 struct elf_link_hash_entry *h;
13853 Elf_Internal_Sym *sym;
721956f4
AM
13854 char *stub_name;
13855 const asection *id_sec;
74f0fb50 13856 struct _opd_sec_data *opd;
e054468f 13857 struct plt_entry *plt_ent;
721956f4
AM
13858
13859 r_type = ELF64_R_TYPE (irela->r_info);
13860 r_indx = ELF64_R_SYM (irela->r_info);
13861
4ce794b7 13862 if (r_type >= R_PPC64_max)
721956f4
AM
13863 {
13864 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13865 goto error_ret_free_internal;
721956f4
AM
13866 }
13867
13868 /* Only look for stubs on branch instructions. */
1657026c
AM
13869 switch (r_type)
13870 {
13871 default:
13872 continue;
13873
13874 case R_PPC64_REL24:
13875 case R_PPC64_REL24_NOTOC:
13876 case R_PPC64_REL24_P9NOTOC:
13877 case R_PPC64_REL14:
13878 case R_PPC64_REL14_BRTAKEN:
13879 case R_PPC64_REL14_BRNTAKEN:
13880 if ((section->flags & SEC_CODE) != 0)
13881 break;
13882 continue;
13883
13884 case R_PPC64_ADDR64:
13885 case R_PPC64_TOC:
13886 if (info->enable_dt_relr
13887 && irela->r_offset % 2 == 0
13888 && section->alignment_power != 0)
13889 break;
13890 continue;
13891 }
721956f4
AM
13892
13893 /* Now determine the call target, its name, value,
13894 section. */
411e1bfb
AM
13895 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13896 r_indx, input_bfd))
13897 goto error_ret_free_internal;
411e1bfb 13898
1657026c
AM
13899 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13900 {
13901 /* Only locally defined symbols can possibly use
13902 relative relocations. */
13903 bfd_vma r_offset;
13904 if ((sym_sec == NULL
13905 || sym_sec->output_section == NULL)
13906 /* No symbol is OK too. */
13907 && !(sym != NULL && sym->st_shndx == 0)
13908 /* Hack for __ehdr_start, which is undefined
13909 at this point. */
13910 && !(h != NULL && h->root.linker_def))
13911 continue;
13912 if (NO_OPD_RELOCS && is_opd)
13913 continue;
13914 if (!is_opd
13915 && r_type == R_PPC64_ADDR64)
13916 {
13917 if (h != NULL
13918 ? h->type == STT_GNU_IFUNC
13919 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13920 continue;
3a3a4c1f
AM
13921 if (h != NULL
13922 ? bfd_is_abs_symbol (&h->root)
13923 : sym->st_shndx == SHN_ABS)
13924 continue;
1657026c
AM
13925 if (h != NULL
13926 && !SYMBOL_REFERENCES_LOCAL (info, h))
13927 continue;
13928 }
13929 r_offset = _bfd_elf_section_offset (info->output_bfd,
13930 info,
13931 section,
13932 irela->r_offset);
13933 if (r_offset >= (bfd_vma) -2)
13934 continue;
0aac2413 13935 if (!append_relr_off (htab, section, r_offset))
1657026c
AM
13936 goto error_ret_free_internal;
13937 continue;
13938 }
13939
13940 hash = ppc_elf_hash_entry (h);
0a1b45a2 13941 ok_dest = false;
8387904d 13942 fdh = NULL;
7fe2b9a6 13943 sym_value = 0;
411e1bfb 13944 if (hash == NULL)
721956f4 13945 {
411e1bfb 13946 sym_value = sym->st_value;
c27b8c2a
AM
13947 if (sym_sec != NULL
13948 && sym_sec->output_section != NULL)
0a1b45a2 13949 ok_dest = true;
721956f4 13950 }
7fe2b9a6
AM
13951 else if (hash->elf.root.type == bfd_link_hash_defined
13952 || hash->elf.root.type == bfd_link_hash_defweak)
13953 {
13954 sym_value = hash->elf.root.u.def.value;
13955 if (sym_sec->output_section != NULL)
0a1b45a2 13956 ok_dest = true;
7fe2b9a6
AM
13957 }
13958 else if (hash->elf.root.type == bfd_link_hash_undefweak
13959 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13960 {
99877b66 13961 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13962 use the func descriptor sym instead if it is
13963 defined. */
ceb1f1ef 13964 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13965 && hash->oh != NULL)
8387904d 13966 {
8c5b4e52 13967 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13968 if (fdh->elf.root.type == bfd_link_hash_defined
13969 || fdh->elf.root.type == bfd_link_hash_defweak)
13970 {
13971 sym_sec = fdh->elf.root.u.def.section;
13972 sym_value = fdh->elf.root.u.def.value;
13973 if (sym_sec->output_section != NULL)
0a1b45a2 13974 ok_dest = true;
8387904d 13975 }
99877b66
AM
13976 else
13977 fdh = NULL;
8387904d 13978 }
7fe2b9a6
AM
13979 }
13980 else
13981 {
13982 bfd_set_error (bfd_error_bad_value);
13983 goto error_ret_free_internal;
721956f4
AM
13984 }
13985
8843416a 13986 destination = 0;
6911b7dc 13987 local_off = 0;
8843416a
AM
13988 if (ok_dest)
13989 {
13990 sym_value += irela->r_addend;
13991 destination = (sym_value
13992 + sym_sec->output_offset
13993 + sym_sec->output_section->vma);
6911b7dc
AM
13994 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13995 ? hash->elf.other
13996 : sym->st_other);
8843416a
AM
13997 }
13998
8387904d 13999 code_sec = sym_sec;
e054468f 14000 code_value = sym_value;
74f0fb50
AM
14001 opd = get_opd_info (sym_sec);
14002 if (opd != NULL)
8387904d
AM
14003 {
14004 bfd_vma dest;
14005
74f0fb50 14006 if (hash == NULL && opd->adjust != NULL)
8387904d 14007 {
51aecdc5 14008 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
14009 if (adjust == -1)
14010 continue;
e054468f 14011 code_value += adjust;
8387904d
AM
14012 sym_value += adjust;
14013 }
14014 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 14015 &code_sec, &code_value, false);
8387904d
AM
14016 if (dest != (bfd_vma) -1)
14017 {
14018 destination = dest;
14019 if (fdh != NULL)
14020 {
14021 /* Fixup old ABI sym to point at code
14022 entry. */
99877b66 14023 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 14024 hash->elf.root.u.def.section = code_sec;
e054468f 14025 hash->elf.root.u.def.value = code_value;
8387904d
AM
14026 }
14027 }
14028 }
14029
721956f4 14030 /* Determine what (if any) linker stub is needed. */
e054468f 14031 plt_ent = NULL;
7aba54da
AM
14032 stub_type.main = ppc_type_of_stub (section, irela, &hash,
14033 &plt_ent, destination,
14034 local_off);
14035 stub_type.sub = ppc_stub_toc;
14036 stub_type.r2save = 0;
14037
14038 if (r_type == R_PPC64_REL24_NOTOC
14039 || r_type == R_PPC64_REL24_P9NOTOC)
05d0e962 14040 {
7aba54da
AM
14041 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
14042 if (htab->params->power10_stubs == 0
14043 || (r_type == R_PPC64_REL24_P9NOTOC
14044 && htab->params->power10_stubs != 1))
14045 notoc = ppc_stub_p9notoc;
14046 if (stub_type.main == ppc_stub_plt_call)
14047 stub_type.sub = notoc;
14048 else if (stub_type.main == ppc_stub_long_branch
05d0e962
AM
14049 || (code_sec != NULL
14050 && code_sec->output_section != NULL
14051 && (((hash ? hash->elf.other : sym->st_other)
14052 & STO_PPC64_LOCAL_MASK)
4a4e7361 14053 > 1 << STO_PPC64_LOCAL_BIT)))
7aba54da
AM
14054 {
14055 stub_type.main = ppc_stub_long_branch;
14056 stub_type.sub = notoc;
14057 stub_type.r2save = 0;
14058 }
05d0e962 14059 }
7aba54da 14060 else if (stub_type.main != ppc_stub_plt_call)
ad8e1ba5
AM
14061 {
14062 /* Check whether we need a TOC adjusting stub.
14063 Since the linker pastes together pieces from
14064 different object files when creating the
14065 _init and _fini functions, it may be that a
14066 call to what looks like a local sym is in
14067 fact a call needing a TOC adjustment. */
33cb30a1
AM
14068 if ((code_sec != NULL
14069 && code_sec->output_section != NULL
33cb30a1 14070 && (code_sec->has_toc_reloc
918dc783
AM
14071 || code_sec->makes_toc_func_call)
14072 && (htab->sec_info[code_sec->id].toc_off
14073 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
14074 || (((hash ? hash->elf.other : sym->st_other)
14075 & STO_PPC64_LOCAL_MASK)
14076 == 1 << STO_PPC64_LOCAL_BIT))
7aba54da
AM
14077 {
14078 stub_type.main = ppc_stub_long_branch;
14079 stub_type.sub = ppc_stub_toc;
14080 stub_type.r2save = 1;
14081 }
ad8e1ba5
AM
14082 }
14083
7aba54da 14084 if (stub_type.main == ppc_stub_none)
721956f4
AM
14085 continue;
14086
411e1bfb 14087 /* __tls_get_addr calls might be eliminated. */
7aba54da 14088 if (stub_type.main != ppc_stub_plt_call
411e1bfb 14089 && hash != NULL
ed7007c1 14090 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
14091 && section->has_tls_reloc
14092 && irela != internal_relocs)
14093 {
14094 /* Get tls info. */
f961d9dd 14095 unsigned char *tls_mask;
411e1bfb 14096
3a71aa26 14097 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
14098 irela - 1, input_bfd))
14099 goto error_ret_free_internal;
abc489c6
AM
14100 if ((*tls_mask & TLS_TLS) != 0
14101 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
14102 continue;
14103 }
14104
7aba54da
AM
14105 if (stub_type.main == ppc_stub_plt_call
14106 && stub_type.sub == ppc_stub_toc)
794e51c0 14107 {
6e1816be
AM
14108 if (!htab->opd_abi
14109 && htab->params->plt_localentry0 != 0
14110 && is_elfv2_localentry0 (&hash->elf))
14111 htab->has_plt_localentry0 = 1;
14112 else if (irela + 1 < irelaend
14113 && irela[1].r_offset == irela->r_offset + 4
14114 && (ELF64_R_TYPE (irela[1].r_info)
14115 == R_PPC64_TOCSAVE))
f378ab09
AM
14116 {
14117 if (!tocsave_find (htab, INSERT,
14118 &local_syms, irela + 1, input_bfd))
14119 goto error_ret_free_internal;
14120 }
f378ab09 14121 else
7aba54da 14122 stub_type.r2save = 1;
794e51c0 14123 }
3b421ab3 14124
721956f4 14125 /* Support for grouping stub sections. */
6f20ed8a 14126 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
14127
14128 /* Get the name of this stub. */
14129 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14130 if (!stub_name)
14131 goto error_ret_free_internal;
14132
14133 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 14134 stub_name, false, false);
721956f4
AM
14135 if (stub_entry != NULL)
14136 {
721956f4 14137 free (stub_name);
7aba54da 14138 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
e10a07b3 14139 {
7aba54da
AM
14140 /* xgettext:c-format */
14141 _bfd_error_handler
14142 (_("%pB: cannot create stub entry %s"),
14143 section->owner, stub_entry->root.string);
14144 goto error_ret_free_internal;
e10a07b3 14145 }
721956f4
AM
14146 continue;
14147 }
14148
25f53a85 14149 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
14150 if (stub_entry == NULL)
14151 {
14152 free (stub_name);
6cdc0ccc
AM
14153 error_ret_free_internal:
14154 if (elf_section_data (section)->relocs == NULL)
14155 free (internal_relocs);
14156 error_ret_free_local:
c9594989
AM
14157 if (symtab_hdr->contents
14158 != (unsigned char *) local_syms)
6cdc0ccc 14159 free (local_syms);
0a1b45a2 14160 return false;
721956f4
AM
14161 }
14162
7aba54da
AM
14163 stub_entry->type = stub_type;
14164 if (stub_type.main == ppc_stub_plt_call)
e054468f 14165 {
05d0e962
AM
14166 stub_entry->target_value = sym_value;
14167 stub_entry->target_section = sym_sec;
e054468f
AM
14168 }
14169 else
14170 {
05d0e962
AM
14171 stub_entry->target_value = code_value;
14172 stub_entry->target_section = code_sec;
e054468f 14173 }
721956f4 14174 stub_entry->h = hash;
e054468f 14175 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
14176 stub_entry->symtype
14177 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 14178 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 14179
3d58e1fc
AM
14180 if (hash != NULL
14181 && (hash->elf.root.type == bfd_link_hash_defined
14182 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 14183 htab->stub_globals += 1;
721956f4
AM
14184 }
14185
14186 /* We're done with the internal relocs, free them. */
6cdc0ccc 14187 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 14188 free (internal_relocs);
721956f4 14189 }
6cdc0ccc
AM
14190
14191 if (local_syms != NULL
14192 && symtab_hdr->contents != (unsigned char *) local_syms)
14193 {
14194 if (!info->keep_memory)
14195 free (local_syms);
14196 else
14197 symtab_hdr->contents = (unsigned char *) local_syms;
14198 }
721956f4
AM
14199 }
14200
5c3dead3 14201 /* We may have added some stubs. Find out the new size of the
721956f4 14202 stub sections. */
d4aaa2a0 14203 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14204 {
14205 group->lr_restore = 0;
14206 group->eh_size = 0;
14207 if (group->stub_sec != NULL)
14208 {
14209 asection *stub_sec = group->stub_sec;
14210
9ff8aa7d 14211 stub_sec->rawsize = stub_sec->size;
df136d64
AM
14212 stub_sec->size = 0;
14213 stub_sec->reloc_count = 0;
14214 stub_sec->flags &= ~SEC_RELOC;
14215 }
14216 }
a804e476
AM
14217 if (htab->tga_group != NULL)
14218 {
14219 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14220 htab->tga_group->eh_size
14221 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14222 htab->tga_group->lr_restore = 23 * 4;
14223 htab->tga_group->stub_sec->size = 24 * 4;
14224 }
eea6121a 14225
9ff8aa7d 14226 htab->brlt->rawsize = htab->brlt->size;
eea6121a 14227 htab->brlt->size = 0;
84f5d08e
AM
14228 htab->brlt->reloc_count = 0;
14229 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 14230 if (htab->relbrlt != NULL)
eea6121a 14231 htab->relbrlt->size = 0;
721956f4 14232
1657026c
AM
14233 if (htab->elf.srelrdyn != NULL)
14234 {
9ff8aa7d 14235 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
1657026c
AM
14236 htab->elf.srelrdyn->size = 0;
14237 }
14238
0441f94f 14239 htab->stub_changed = false;
ecc741e6 14240 htab->stub_id = 0;
63bc6f6c 14241 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 14242
a4b6fadd
AM
14243 for (group = htab->group; group != NULL; group = group->next)
14244 if (group->needs_save_res)
14245 group->stub_sec->size += htab->sfpr->size;
14246
176a0d42
AM
14247 if (info->emitrelocations
14248 && htab->glink != NULL && htab->glink->size != 0)
14249 {
14250 htab->glink->reloc_count = 1;
14251 htab->glink->flags |= SEC_RELOC;
14252 }
14253
58d180e8
AM
14254 if (htab->glink_eh_frame != NULL
14255 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 14256 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 14257 {
2e0ce1c8 14258 size_t size = 0, align = 4;
58d180e8 14259
d4aaa2a0 14260 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14261 if (group->eh_size != 0)
14262 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 14263 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 14264 size += (24 + align - 1) & -align;
58d180e8 14265 if (size != 0)
2e0ce1c8
AM
14266 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14267 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14268 size = (size + align - 1) & -align;
58d180e8
AM
14269 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14270 htab->glink_eh_frame->size = size;
14271 }
14272
e7d1c40c 14273 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14274 for (group = htab->group; group != NULL; group = group->next)
14275 if (group->stub_sec != NULL)
691d2e9a
AM
14276 {
14277 int align = abs (htab->params->plt_stub_align);
14278 group->stub_sec->size
14279 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14280 }
d4aaa2a0 14281
1657026c
AM
14282 if (htab->elf.srelrdyn != NULL)
14283 {
14284 bfd_vma r_offset;
14285
14286 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
0aac2413 14287 if (!append_relr_off (htab, htab->brlt, r_offset))
1657026c
AM
14288 return false;
14289
14290 if (!got_and_plt_relr_for_local_syms (info))
14291 return false;
14292 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14293 if (htab->stub_error)
14294 return false;
14295
0aac2413
AM
14296 bfd_vma *relr_addr = sort_relr (htab);
14297 if (htab->relr_count != 0 && relr_addr == NULL)
14298 return false;
1657026c
AM
14299
14300 size_t i = 0;
14301 while (i < htab->relr_count)
14302 {
0aac2413 14303 bfd_vma base = relr_addr[i];
1657026c
AM
14304 htab->elf.srelrdyn->size += 8;
14305 i++;
14306 /* Handle possible duplicate address. This can happen
14307 as sections increase in size when adding stubs. */
14308 while (i < htab->relr_count
0aac2413 14309 && relr_addr[i] == base)
1657026c
AM
14310 i++;
14311 base += 8;
14312 while (1)
14313 {
14314 size_t start_i = i;
14315 while (i < htab->relr_count
0aac2413
AM
14316 && relr_addr[i] - base < 63 * 8
14317 && (relr_addr[i] - base) % 8 == 0)
1657026c
AM
14318 i++;
14319 if (i == start_i)
14320 break;
14321 htab->elf.srelrdyn->size += 8;
14322 base += 63 * 8;
14323 }
14324 }
0aac2413 14325 free (relr_addr);
1657026c
AM
14326 }
14327
d4aaa2a0
AM
14328 for (group = htab->group; group != NULL; group = group->next)
14329 if (group->stub_sec != NULL
14330 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 14331 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 14332 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
14333 break;
14334
d4aaa2a0 14335 if (group == NULL
0441f94f
AM
14336 && (!htab->stub_changed
14337 || htab->stub_iteration > STUB_SHRINK_ITER)
ba21f564
AM
14338 && (htab->brlt->rawsize == htab->brlt->size
14339 || (htab->stub_iteration > STUB_SHRINK_ITER
14340 && htab->brlt->rawsize > htab->brlt->size))
1657026c
AM
14341 && (htab->elf.srelrdyn == NULL
14342 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14343 || (htab->stub_iteration > STUB_SHRINK_ITER
14344 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
58d180e8 14345 && (htab->glink_eh_frame == NULL
a804e476
AM
14346 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14347 && (htab->tga_group == NULL
14348 || htab->stub_iteration > 1))
5c3dead3
AM
14349 break;
14350
9ff8aa7d
AM
14351 if (htab->stub_iteration > STUB_SHRINK_ITER)
14352 {
14353 for (group = htab->group; group != NULL; group = group->next)
14354 if (group->stub_sec != NULL
14355 && group->stub_sec->size < group->stub_sec->rawsize)
14356 group->stub_sec->size = group->stub_sec->rawsize;
14357
14358 if (htab->brlt->size < htab->brlt->rawsize)
14359 htab->brlt->size = htab->brlt->rawsize;
14360
14361 if (htab->elf.srelrdyn != NULL
14362 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14363 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14364 }
14365
721956f4 14366 /* Ask the linker to do its stuff. */
e7d1c40c 14367 (*htab->params->layout_sections_again) ();
721956f4
AM
14368 }
14369
da44f4e5
AM
14370 if (htab->glink_eh_frame != NULL
14371 && htab->glink_eh_frame->size != 0)
14372 {
14373 bfd_vma val;
14374 bfd_byte *p, *last_fde;
14375 size_t last_fde_len, size, align, pad;
d4aaa2a0 14376 struct map_stub *group;
da44f4e5 14377
df136d64
AM
14378 /* It is necessary to at least have a rough outline of the
14379 linker generated CIEs and FDEs written before
14380 bfd_elf_discard_info is run, in order for these FDEs to be
14381 indexed in .eh_frame_hdr. */
da44f4e5
AM
14382 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14383 if (p == NULL)
0a1b45a2 14384 return false;
da44f4e5
AM
14385 htab->glink_eh_frame->contents = p;
14386 last_fde = p;
2e0ce1c8 14387 align = 4;
da44f4e5
AM
14388
14389 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14390 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 14391 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 14392 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 14393 p += last_fde_len + 4;
da44f4e5 14394
d4aaa2a0 14395 for (group = htab->group; group != NULL; group = group->next)
df136d64 14396 if (group->eh_size != 0)
da44f4e5 14397 {
df136d64 14398 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 14399 last_fde = p;
df136d64 14400 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 14401 /* FDE length. */
2e0ce1c8 14402 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14403 p += 4;
14404 /* CIE pointer. */
14405 val = p - htab->glink_eh_frame->contents;
14406 bfd_put_32 (htab->elf.dynobj, val, p);
14407 p += 4;
14408 /* Offset to stub section, written later. */
14409 p += 4;
14410 /* stub section size. */
d4aaa2a0 14411 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
14412 p += 4;
14413 /* Augmentation. */
14414 p += 1;
df136d64
AM
14415 /* Make sure we don't have all nops. This is enough for
14416 elf-eh-frame.c to detect the last non-nop opcode. */
14417 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 14418 p = last_fde + last_fde_len + 4;
da44f4e5
AM
14419 }
14420 if (htab->glink != NULL && htab->glink->size != 0)
14421 {
14422 last_fde = p;
2e0ce1c8 14423 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 14424 /* FDE length. */
2e0ce1c8 14425 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14426 p += 4;
14427 /* CIE pointer. */
14428 val = p - htab->glink_eh_frame->contents;
14429 bfd_put_32 (htab->elf.dynobj, val, p);
14430 p += 4;
14431 /* Offset to .glink, written later. */
14432 p += 4;
14433 /* .glink size. */
14434 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14435 p += 4;
14436 /* Augmentation. */
14437 p += 1;
14438
3cd7c7d7 14439 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
14440 *p++ = DW_CFA_register;
14441 *p++ = 65;
9f08fa5c 14442 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 14443 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
14444 *p++ = DW_CFA_restore_extended;
14445 *p++ = 65;
2e0ce1c8 14446 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
14447 }
14448 /* Subsume any padding into the last FDE if user .eh_frame
14449 sections are aligned more than glink_eh_frame. Otherwise any
14450 zero padding will be seen as a terminator. */
2e0ce1c8 14451 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 14452 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 14453 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
14454 htab->glink_eh_frame->size = size + pad;
14455 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14456 }
14457
d969d15f 14458 maybe_strip_output (info, htab->brlt);
2efec98b
AM
14459 if (htab->relbrlt != NULL)
14460 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
14461 if (htab->glink_eh_frame != NULL)
14462 maybe_strip_output (info, htab->glink_eh_frame);
1657026c
AM
14463 if (htab->elf.srelrdyn != NULL)
14464 maybe_strip_output (info, htab->elf.srelrdyn);
721956f4 14465
0a1b45a2 14466 return true;
721956f4
AM
14467}
14468
14469/* Called after we have determined section placement. If sections
805fc799 14470 move, we'll be called again. Provide a value for TOCstart. */
721956f4 14471
805fc799 14472bfd_vma
1c865ab2 14473ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 14474{
805fc799 14475 asection *s;
a27e685f 14476 bfd_vma TOCstart, adjust;
721956f4 14477
43417696
AM
14478 if (info != NULL)
14479 {
14480 struct elf_link_hash_entry *h;
14481 struct elf_link_hash_table *htab = elf_hash_table (info);
14482
2cc15b10 14483 if (is_elf_hash_table (&htab->root)
43417696
AM
14484 && htab->hgot != NULL)
14485 h = htab->hgot;
14486 else
14487 {
2cc15b10
AM
14488 h = (struct elf_link_hash_entry *)
14489 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14490 if (is_elf_hash_table (&htab->root))
43417696
AM
14491 htab->hgot = h;
14492 }
14493 if (h != NULL
14494 && h->root.type == bfd_link_hash_defined
14495 && !h->root.linker_def
2cc15b10 14496 && (!is_elf_hash_table (&htab->root)
43417696
AM
14497 || h->def_regular))
14498 {
ed7007c1 14499 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
14500 _bfd_set_gp_value (obfd, TOCstart);
14501 return TOCstart;
14502 }
14503 }
14504
805fc799
AM
14505 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14506 order. The TOC starts where the first of these sections starts. */
14507 s = bfd_get_section_by_name (obfd, ".got");
e054468f 14508 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14509 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 14510 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14511 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 14512 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14513 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 14514 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
14515 {
14516 /* This may happen for
14517 o references to TOC base (SYM@toc / TOC[tc0]) without a
14518 .toc directive
14519 o bad linker script
14520 o --gc-sections and empty TOC sections
14521
14522 FIXME: Warn user? */
14523
14524 /* Look for a likely section. We probably won't even be
14525 using TOCstart. */
14526 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14527 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14528 | SEC_EXCLUDE))
805fc799
AM
14529 == (SEC_ALLOC | SEC_SMALL_DATA))
14530 break;
721956f4 14531 if (s == NULL)
805fc799 14532 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14533 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14534 == (SEC_ALLOC | SEC_SMALL_DATA))
14535 break;
721956f4 14536 if (s == NULL)
805fc799 14537 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14538 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14539 == SEC_ALLOC)
805fc799 14540 break;
721956f4 14541 if (s == NULL)
805fc799 14542 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14543 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14544 break;
14545 }
721956f4 14546
805fc799
AM
14547 TOCstart = 0;
14548 if (s != NULL)
14549 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14550
a27e685f
AM
14551 /* Force alignment. */
14552 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14553 TOCstart -= adjust;
1c865ab2
AM
14554 _bfd_set_gp_value (obfd, TOCstart);
14555
810d4e75 14556 if (info != NULL && s != NULL)
1c865ab2
AM
14557 {
14558 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14559
810d4e75
AM
14560 if (htab != NULL)
14561 {
14562 if (htab->elf.hgot != NULL)
14563 {
a27e685f 14564 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14565 htab->elf.hgot->root.u.def.section = s;
14566 }
14567 }
14568 else
1c865ab2 14569 {
810d4e75
AM
14570 struct bfd_link_hash_entry *bh = NULL;
14571 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f 14572 s, TOC_BASE_OFF - adjust,
0a1b45a2 14573 NULL, false, false, &bh);
1c865ab2
AM
14574 }
14575 }
805fc799 14576 return TOCstart;
721956f4
AM
14577}
14578
a345bc8d 14579/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14580 write out any global entry stubs, and PLT relocations. */
a345bc8d 14581
0a1b45a2 14582static bool
49c09209 14583build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14584{
14585 struct bfd_link_info *info;
14586 struct ppc_link_hash_table *htab;
49c09209 14587 struct plt_entry *ent;
a345bc8d
AM
14588 asection *s;
14589
14590 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 14591 return true;
a345bc8d 14592
49c09209
AM
14593 info = inf;
14594 htab = ppc_hash_table (info);
14595 if (htab == NULL)
0a1b45a2 14596 return false;
49c09209
AM
14597
14598 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14599 if (ent->plt.offset != (bfd_vma) -1)
14600 {
14601 /* This symbol has an entry in the procedure linkage
14602 table. Set it up. */
14603 Elf_Internal_Rela rela;
2d7ad24e 14604 asection *plt, *relplt;
49c09209
AM
14605 bfd_byte *loc;
14606
30845f11 14607 if (use_local_plt (info, h))
49c09209
AM
14608 {
14609 if (!(h->def_regular
14610 && (h->root.type == bfd_link_hash_defined
14611 || h->root.type == bfd_link_hash_defweak)))
14612 continue;
2d7ad24e
AM
14613 if (h->type == STT_GNU_IFUNC)
14614 {
14615 plt = htab->elf.iplt;
14616 relplt = htab->elf.irelplt;
0a1b45a2 14617 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14618 if (htab->opd_abi)
14619 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14620 else
14621 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14622 }
49c09209 14623 else
2d7ad24e
AM
14624 {
14625 plt = htab->pltlocal;
1657026c
AM
14626 relplt = NULL;
14627 if (bfd_link_pic (info)
14628 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
14629 {
14630 relplt = htab->relpltlocal;
14631 if (htab->opd_abi)
14632 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14633 else
14634 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14635 }
2d7ad24e 14636 }
ed7007c1 14637 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14638
14639 if (relplt == NULL)
14640 {
14641 loc = plt->contents + ent->plt.offset;
14642 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14643 if (htab->opd_abi)
14644 {
14645 bfd_vma toc = elf_gp (info->output_bfd);
14646 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14647 bfd_put_64 (info->output_bfd, toc, loc + 8);
14648 }
14649 }
14650 else
14651 {
14652 rela.r_offset = (plt->output_section->vma
14653 + plt->output_offset
14654 + ent->plt.offset);
14655 loc = relplt->contents + (relplt->reloc_count++
14656 * sizeof (Elf64_External_Rela));
14657 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14658 }
49c09209
AM
14659 }
14660 else
14661 {
14662 rela.r_offset = (htab->elf.splt->output_section->vma
14663 + htab->elf.splt->output_offset
14664 + ent->plt.offset);
14665 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14666 rela.r_addend = ent->addend;
14667 loc = (htab->elf.srelplt->contents
14668 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14669 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14670 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
0a1b45a2 14671 htab->elf.ifunc_resolvers = true;
2d7ad24e 14672 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14673 }
49c09209
AM
14674 }
14675
a345bc8d 14676 if (!h->pointer_equality_needed)
0a1b45a2 14677 return true;
a345bc8d
AM
14678
14679 if (h->def_regular)
0a1b45a2 14680 return true;
a345bc8d 14681
9e390558 14682 s = htab->global_entry;
49c09209 14683 if (s == NULL || s->size == 0)
0a1b45a2 14684 return true;
49c09209
AM
14685
14686 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14687 if (ent->plt.offset != (bfd_vma) -1
14688 && ent->addend == 0)
a345bc8d
AM
14689 {
14690 bfd_byte *p;
14691 asection *plt;
14692 bfd_vma off;
14693
a345bc8d 14694 p = s->contents + h->root.u.def.value;
33e44f2e 14695 plt = htab->elf.splt;
30845f11 14696 if (use_local_plt (info, h))
2d7ad24e
AM
14697 {
14698 if (h->type == STT_GNU_IFUNC)
14699 plt = htab->elf.iplt;
14700 else
14701 plt = htab->pltlocal;
14702 }
49c09209 14703 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14704 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14705
14706 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14707 {
14708 info->callbacks->einfo
c1c8c1ef 14709 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14710 h->root.root.string);
14711 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14712 htab->stub_error = true;
a345bc8d
AM
14713 }
14714
7341d5e2
AM
14715 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14716 if (htab->params->emit_stub_syms)
14717 {
14718 size_t len = strlen (h->root.root.string);
14719 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14720
14721 if (name == NULL)
0a1b45a2 14722 return false;
7341d5e2
AM
14723
14724 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
0a1b45a2 14725 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
7341d5e2 14726 if (h == NULL)
0a1b45a2 14727 return false;
7341d5e2
AM
14728 if (h->root.type == bfd_link_hash_new)
14729 {
14730 h->root.type = bfd_link_hash_defined;
14731 h->root.u.def.section = s;
14732 h->root.u.def.value = p - s->contents;
14733 h->ref_regular = 1;
14734 h->def_regular = 1;
14735 h->ref_regular_nonweak = 1;
14736 h->forced_local = 1;
14737 h->non_elf = 0;
2ec55de3 14738 h->root.linker_def = 1;
7341d5e2
AM
14739 }
14740 }
14741
a345bc8d
AM
14742 if (PPC_HA (off) != 0)
14743 {
14744 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14745 p += 4;
14746 }
14747 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14748 p += 4;
14749 bfd_put_32 (s->owner, MTCTR_R12, p);
14750 p += 4;
407aa07c 14751 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14752 break;
14753 }
0a1b45a2 14754 return true;
a345bc8d
AM
14755}
14756
49c09209
AM
14757/* Write PLT relocs for locals. */
14758
0a1b45a2 14759static bool
49c09209
AM
14760write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14761{
14762 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14763 bfd *ibfd;
14764
14765 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14766 {
14767 struct got_entry **lgot_ents, **end_lgot_ents;
14768 struct plt_entry **local_plt, **lplt, **end_local_plt;
14769 Elf_Internal_Shdr *symtab_hdr;
14770 bfd_size_type locsymcount;
14771 Elf_Internal_Sym *local_syms = NULL;
14772 struct plt_entry *ent;
14773
14774 if (!is_ppc64_elf (ibfd))
14775 continue;
14776
14777 lgot_ents = elf_local_got_ents (ibfd);
14778 if (!lgot_ents)
14779 continue;
14780
14781 symtab_hdr = &elf_symtab_hdr (ibfd);
14782 locsymcount = symtab_hdr->sh_info;
14783 end_lgot_ents = lgot_ents + locsymcount;
14784 local_plt = (struct plt_entry **) end_lgot_ents;
14785 end_local_plt = local_plt + locsymcount;
14786 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14787 for (ent = *lplt; ent != NULL; ent = ent->next)
14788 if (ent->plt.offset != (bfd_vma) -1)
14789 {
14790 Elf_Internal_Sym *sym;
14791 asection *sym_sec;
14792 asection *plt, *relplt;
14793 bfd_byte *loc;
14794 bfd_vma val;
49c09209
AM
14795
14796 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14797 lplt - local_plt, ibfd))
14798 {
c9594989 14799 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209 14800 free (local_syms);
0a1b45a2 14801 return false;
49c09209
AM
14802 }
14803
14804 val = sym->st_value + ent->addend;
49c09209
AM
14805 if (sym_sec != NULL && sym_sec->output_section != NULL)
14806 val += sym_sec->output_offset + sym_sec->output_section->vma;
14807
2d7ad24e
AM
14808 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14809 {
0a1b45a2 14810 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14811 plt = htab->elf.iplt;
14812 relplt = htab->elf.irelplt;
14813 }
14814 else
14815 {
14816 plt = htab->pltlocal;
1657026c
AM
14817 relplt = NULL;
14818 if (bfd_link_pic (info)
14819 && !(info->enable_dt_relr && !htab->opd_abi))
14820 relplt = htab->relpltlocal;
2d7ad24e 14821 }
49c09209 14822
2d7ad24e
AM
14823 if (relplt == NULL)
14824 {
14825 loc = plt->contents + ent->plt.offset;
14826 bfd_put_64 (info->output_bfd, val, loc);
14827 if (htab->opd_abi)
14828 {
14829 bfd_vma toc = elf_gp (ibfd);
14830 bfd_put_64 (info->output_bfd, toc, loc + 8);
14831 }
14832 }
49c09209 14833 else
2d7ad24e
AM
14834 {
14835 Elf_Internal_Rela rela;
14836 rela.r_offset = (ent->plt.offset
14837 + plt->output_offset
14838 + plt->output_section->vma);
14839 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14840 {
14841 if (htab->opd_abi)
14842 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14843 else
14844 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14845 }
14846 else
14847 {
14848 if (htab->opd_abi)
14849 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14850 else
14851 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14852 }
14853 rela.r_addend = val;
14854 loc = relplt->contents + (relplt->reloc_count++
14855 * sizeof (Elf64_External_Rela));
14856 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14857 }
49c09209
AM
14858 }
14859
14860 if (local_syms != NULL
14861 && symtab_hdr->contents != (unsigned char *) local_syms)
14862 {
14863 if (!info->keep_memory)
14864 free (local_syms);
14865 else
14866 symtab_hdr->contents = (unsigned char *) local_syms;
14867 }
14868 }
0a1b45a2 14869 return true;
49c09209
AM
14870}
14871
a804e476
AM
14872/* Emit the static wrapper function preserving registers around a
14873 __tls_get_addr_opt call. */
14874
0a1b45a2 14875static bool
a804e476
AM
14876emit_tga_desc (struct ppc_link_hash_table *htab)
14877{
14878 asection *stub_sec = htab->tga_group->stub_sec;
14879 unsigned int cfa_updt = 11 * 4;
14880 bfd_byte *p;
14881 bfd_vma to, from, delta;
14882
14883 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14884 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14885 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14886 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14887 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14888 delta = to - from;
14889 if (delta + (1 << 25) >= 1 << 26)
14890 {
14891 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
0a1b45a2
AM
14892 htab->stub_error = true;
14893 return false;
a804e476
AM
14894 }
14895
14896 p = stub_sec->contents;
14897 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14898 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14899 p += 4;
14900 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14901 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14902}
14903
14904/* Emit eh_frame describing the static wrapper function. */
14905
14906static bfd_byte *
14907emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14908{
14909 unsigned int cfa_updt = 11 * 4;
14910 unsigned int i;
14911
14912 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14913 *p++ = DW_CFA_def_cfa_offset;
14914 if (htab->opd_abi)
14915 {
14916 *p++ = 128;
14917 *p++ = 1;
14918 }
14919 else
14920 *p++ = 96;
14921 *p++ = DW_CFA_offset_extended_sf;
14922 *p++ = 65;
14923 *p++ = (-16 / 8) & 0x7f;
14924 for (i = 4; i < 12; i++)
14925 {
14926 *p++ = DW_CFA_offset + i;
14927 *p++ = (htab->opd_abi ? 13 : 12) - i;
14928 }
14929 *p++ = DW_CFA_advance_loc + 10;
14930 *p++ = DW_CFA_def_cfa_offset;
14931 *p++ = 0;
14932 for (i = 4; i < 12; i++)
14933 *p++ = DW_CFA_restore + i;
14934 *p++ = DW_CFA_advance_loc + 2;
14935 *p++ = DW_CFA_restore_extended;
14936 *p++ = 65;
14937 return p;
14938}
14939
721956f4
AM
14940/* Build all the stubs associated with the current output file.
14941 The stubs are kept in a hash table attached to the main linker
14942 hash table. This function is called via gldelf64ppc_finish. */
14943
0a1b45a2 14944bool
e7d1c40c 14945ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14946 char **stats)
5d1634d7
AM
14947{
14948 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14949 struct map_stub *group;
721956f4 14950 asection *stub_sec;
5d1634d7 14951 bfd_byte *p;
e717da7e 14952 int stub_sec_count = 0;
5d1634d7 14953
4dfe6ac6 14954 if (htab == NULL)
0a1b45a2 14955 return false;
4dfe6ac6 14956
eea6121a 14957 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14958 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14959 {
14960 group->eh_size = 0;
14961 group->lr_restore = 0;
14962 if ((stub_sec = group->stub_sec) != NULL
14963 && stub_sec->size != 0)
14964 {
14965 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14966 stub_sec->size);
14967 if (stub_sec->contents == NULL)
0a1b45a2 14968 return false;
df136d64
AM
14969 stub_sec->size = 0;
14970 }
14971 }
5d1634d7 14972
23eb7e01 14973 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14974 {
9f951329 14975 unsigned int indx;
ad8e1ba5 14976 bfd_vma plt0;
9f951329 14977
721956f4 14978 /* Build the .glink plt call stub. */
e7d1c40c 14979 if (htab->params->emit_stub_syms)
97b639ba
AM
14980 {
14981 struct elf_link_hash_entry *h;
468392fb 14982 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
0a1b45a2 14983 true, false, false);
97b639ba 14984 if (h == NULL)
0a1b45a2 14985 return false;
97b639ba
AM
14986 if (h->root.type == bfd_link_hash_new)
14987 {
14988 h->root.type = bfd_link_hash_defined;
14989 h->root.u.def.section = htab->glink;
ee4bf8d2 14990 h->root.u.def.value = 8;
f5385ebf
AM
14991 h->ref_regular = 1;
14992 h->def_regular = 1;
14993 h->ref_regular_nonweak = 1;
14994 h->forced_local = 1;
14995 h->non_elf = 0;
2ec55de3 14996 h->root.linker_def = 1;
97b639ba
AM
14997 }
14998 }
33e44f2e
AM
14999 plt0 = (htab->elf.splt->output_section->vma
15000 + htab->elf.splt->output_offset
15001 - 16);
176a0d42
AM
15002 if (info->emitrelocations)
15003 {
15004 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
15005 if (r == NULL)
0a1b45a2 15006 return false;
176a0d42
AM
15007 r->r_offset = (htab->glink->output_offset
15008 + htab->glink->output_section->vma);
15009 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
15010 r->r_addend = plt0;
15011 }
4ce794b7 15012 p = htab->glink->contents;
176a0d42 15013 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
15014 bfd_put_64 (htab->glink->owner, plt0, p);
15015 p += 8;
b9e5796b
AM
15016 if (htab->opd_abi)
15017 {
15018 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
15019 p += 4;
15020 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15021 p += 4;
15022 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15023 p += 4;
15024 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
15025 p += 4;
15026 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
15027 p += 4;
15028 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
15029 p += 4;
15030 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15031 p += 4;
15032 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
15033 p += 4;
15034 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15035 p += 4;
15036 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
15037 p += 4;
15038 }
15039 else
15040 {
3cd7c7d7
AM
15041 unsigned int insn;
15042
15043 /* 0:
15044 . .quad plt0-1f # plt0 entry relative to 1:
15045 #
15046 # We get here with r12 initially @ a glink branch
15047 # Load the address of _dl_runtime_resolve from plt0 and
15048 # jump to it, with r0 set to the index of the PLT entry
15049 # to be resolved and r11 the link map.
15050 __glink_PLTresolve:
15051 . std %r2,24(%r1) # optional
15052 . mflr %r0
15053 . bcl 20,31,1f
15054 1:
15055 . mflr %r11
15056 . mtlr %r0
15057 . ld %r0,(0b-1b)(%r11)
15058 . sub %r12,%r12,%r11
15059 . add %r11,%r0,%r11
15060 . addi %r0,%r12,1b-2f
15061 . ld %r12,0(%r11)
15062 . srdi %r0,%r0,2
15063 . mtctr %r12
15064 . ld %r11,8(%r11)
15065 . bctr
15066 2:
15067 . b __glink_PLTresolve
15068 . ...
15069 . b __glink_PLTresolve */
15070
15071 if (htab->has_plt_localentry0)
15072 {
15073 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15074 p += 4;
15075 }
b9e5796b
AM
15076 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15077 p += 4;
15078 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15079 p += 4;
15080 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15081 p += 4;
b9e5796b
AM
15082 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15083 p += 4;
3cd7c7d7
AM
15084 if (htab->has_plt_localentry0)
15085 insn = LD_R0_0R11 | (-20 & 0xfffc);
15086 else
15087 insn = LD_R0_0R11 | (-16 & 0xfffc);
15088 bfd_put_32 (htab->glink->owner, insn, p);
15089 p += 4;
b9e5796b
AM
15090 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15091 p += 4;
3cd7c7d7 15092 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 15093 p += 4;
3cd7c7d7 15094 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
15095 p += 4;
15096 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15097 p += 4;
15098 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15099 p += 4;
15100 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15101 p += 4;
15102 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15103 p += 4;
15104 }
407aa07c
AM
15105 bfd_put_32 (htab->glink->owner, BCTR, p);
15106 p += 4;
c75bc4f7 15107 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 15108
9f951329
AM
15109 /* Build the .glink lazy link call stubs. */
15110 indx = 0;
9e390558 15111 while (p < htab->glink->contents + htab->glink->size)
9f951329 15112 {
b9e5796b 15113 if (htab->opd_abi)
9f951329 15114 {
b9e5796b
AM
15115 if (indx < 0x8000)
15116 {
15117 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15118 p += 4;
15119 }
15120 else
15121 {
15122 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15123 p += 4;
15124 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15125 p);
15126 p += 4;
15127 }
9f951329 15128 }
4ce794b7 15129 bfd_put_32 (htab->glink->owner,
ee4bf8d2 15130 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 15131 indx++;
9f951329
AM
15132 p += 4;
15133 }
5d1634d7 15134 }
5d1634d7 15135
a804e476
AM
15136 if (htab->tga_group != NULL)
15137 {
15138 htab->tga_group->lr_restore = 23 * 4;
15139 htab->tga_group->stub_sec->size = 24 * 4;
15140 if (!emit_tga_desc (htab))
0a1b45a2 15141 return false;
a804e476
AM
15142 if (htab->glink_eh_frame != NULL
15143 && htab->glink_eh_frame->size != 0)
15144 {
15145 size_t align = 4;
15146
15147 p = htab->glink_eh_frame->contents;
15148 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15149 p += 17;
15150 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15151 }
15152 }
15153
49c09209
AM
15154 /* Build .glink global entry stubs, and PLT relocs for globals. */
15155 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15156
15157 if (!write_plt_relocs_for_local_syms (info))
0a1b45a2 15158 return false;
9e390558 15159
7341d5e2 15160 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 15161 {
4ce794b7 15162 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 15163 htab->brlt->size);
4ce794b7 15164 if (htab->brlt->contents == NULL)
0a1b45a2 15165 return false;
721956f4 15166 }
ee75fd95 15167 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
15168 {
15169 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 15170 htab->relbrlt->size);
63bc6f6c 15171 if (htab->relbrlt->contents == NULL)
0a1b45a2 15172 return false;
63bc6f6c 15173 }
5d1634d7 15174
721956f4 15175 /* Build the stubs as directed by the stub hash table. */
ecc741e6 15176 htab->stub_id = 0;
721956f4 15177 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 15178
a4b6fadd
AM
15179 for (group = htab->group; group != NULL; group = group->next)
15180 if (group->needs_save_res)
7dda8d3c 15181 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 15182
aa8a7074
AM
15183 if (htab->relbrlt != NULL)
15184 htab->relbrlt->reloc_count = 0;
15185
e7d1c40c 15186 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
15187 for (group = htab->group; group != NULL; group = group->next)
15188 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
15189 {
15190 int align = abs (htab->params->plt_stub_align);
15191 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15192 }
794e51c0 15193
7dda8d3c
AM
15194 for (group = htab->group; group != NULL; group = group->next)
15195 if (group->needs_save_res)
15196 {
15197 stub_sec = group->stub_sec;
15198 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15199 htab->sfpr->contents, htab->sfpr->size);
15200 if (htab->params->emit_stub_syms)
15201 {
15202 unsigned int i;
15203
15204 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15205 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
0a1b45a2 15206 return false;
7dda8d3c
AM
15207 }
15208 }
15209
df136d64
AM
15210 if (htab->glink_eh_frame != NULL
15211 && htab->glink_eh_frame->size != 0)
15212 {
15213 bfd_vma val;
15214 size_t align = 4;
15215
15216 p = htab->glink_eh_frame->contents;
15217 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15218
15219 for (group = htab->group; group != NULL; group = group->next)
15220 if (group->eh_size != 0)
15221 {
15222 /* Offset to stub section. */
15223 val = (group->stub_sec->output_section->vma
15224 + group->stub_sec->output_offset);
15225 val -= (htab->glink_eh_frame->output_section->vma
15226 + htab->glink_eh_frame->output_offset
15227 + (p + 8 - htab->glink_eh_frame->contents));
15228 if (val + 0x80000000 > 0xffffffff)
15229 {
15230 _bfd_error_handler
15231 (_("%s offset too large for .eh_frame sdata4 encoding"),
15232 group->stub_sec->name);
0a1b45a2 15233 return false;
df136d64
AM
15234 }
15235 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15236 p += (group->eh_size + 17 + 3) & -4;
15237 }
15238 if (htab->glink != NULL && htab->glink->size != 0)
15239 {
15240 /* Offset to .glink. */
15241 val = (htab->glink->output_section->vma
15242 + htab->glink->output_offset
15243 + 8);
15244 val -= (htab->glink_eh_frame->output_section->vma
15245 + htab->glink_eh_frame->output_offset
15246 + (p + 8 - htab->glink_eh_frame->contents));
15247 if (val + 0x80000000 > 0xffffffff)
15248 {
15249 _bfd_error_handler
15250 (_("%s offset too large for .eh_frame sdata4 encoding"),
15251 htab->glink->name);
0a1b45a2 15252 return false;
df136d64
AM
15253 }
15254 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15255 p += (24 + align - 1) & -align;
15256 }
15257 }
15258
1657026c
AM
15259 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15260 {
15261 htab->elf.srelrdyn->contents
15262 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15263 if (htab->elf.srelrdyn->contents == NULL)
15264 return false;
15265
0aac2413
AM
15266 bfd_vma *relr_addr = sort_relr (htab);
15267 if (htab->relr_count != 0 && relr_addr == NULL)
15268 return false;
15269
1657026c
AM
15270 size_t i = 0;
15271 bfd_byte *loc = htab->elf.srelrdyn->contents;
15272 while (i < htab->relr_count)
15273 {
0aac2413 15274 bfd_vma base = relr_addr[i];
1657026c
AM
15275 BFD_ASSERT (base % 2 == 0);
15276 bfd_put_64 (htab->elf.dynobj, base, loc);
15277 loc += 8;
15278 i++;
15279 while (i < htab->relr_count
0aac2413 15280 && relr_addr[i] == base)
1657026c
AM
15281 {
15282 htab->stub_error = true;
15283 i++;
15284 }
15285 base += 8;
15286 while (1)
15287 {
15288 bfd_vma bits = 0;
15289 while (i < htab->relr_count
0aac2413
AM
15290 && relr_addr[i] - base < 63 * 8
15291 && (relr_addr[i] - base) % 8 == 0)
1657026c 15292 {
0aac2413 15293 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
1657026c
AM
15294 i++;
15295 }
15296 if (bits == 0)
15297 break;
15298 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15299 loc += 8;
15300 base += 63 * 8;
15301 }
15302 }
0aac2413 15303 free (relr_addr);
1657026c
AM
15304 /* Pad any excess with 1's, a do-nothing encoding. */
15305 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15306 < htab->elf.srelrdyn->size)
15307 {
15308 bfd_put_64 (htab->elf.dynobj, 1, loc);
15309 loc += 8;
15310 }
15311 }
15312
d4aaa2a0
AM
15313 for (group = htab->group; group != NULL; group = group->next)
15314 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
15315 {
15316 stub_sec_count += 1;
c9301e31
AM
15317 if (stub_sec->rawsize != stub_sec->size
15318 && (htab->stub_iteration <= STUB_SHRINK_ITER
15319 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
15320 break;
15321 }
5d1634d7 15322
25516cc5 15323 if (group != NULL)
1657026c
AM
15324 htab->stub_error = true;
15325
15326 if (htab->stub_error)
5d1634d7 15327 {
cf97bcb0 15328 _bfd_error_handler (_("stubs don't match calculated size"));
1657026c 15329 return false;
5d1634d7 15330 }
721956f4 15331
d2a300cf
AM
15332 if (stats != NULL)
15333 {
988b7300
AM
15334 char *groupmsg;
15335 if (asprintf (&groupmsg,
17a6f5f2
AM
15336 ngettext ("linker stubs in %u group",
15337 "linker stubs in %u groups",
988b7300
AM
15338 stub_sec_count),
15339 stub_sec_count) < 0)
15340 *stats = NULL;
15341 else
15342 {
17a6f5f2 15343 if (asprintf (stats, _("%s, iter %u\n"
988b7300 15344 " branch %lu\n"
988b7300 15345 " long branch %lu\n"
988b7300 15346 " plt call %lu\n"
988b7300 15347 " global entry %lu"),
17a6f5f2 15348 groupmsg, htab->stub_iteration,
988b7300 15349 htab->stub_count[ppc_stub_long_branch - 1],
988b7300 15350 htab->stub_count[ppc_stub_plt_branch - 1],
988b7300 15351 htab->stub_count[ppc_stub_plt_call - 1],
988b7300
AM
15352 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15353 *stats = NULL;
15354 free (groupmsg);
15355 }
d2a300cf 15356 }
0a1b45a2 15357 return true;
5bd4f169
AM
15358}
15359
60124e18
AM
15360/* What to do when ld finds relocations against symbols defined in
15361 discarded sections. */
15362
15363static unsigned int
15364ppc64_elf_action_discarded (asection *sec)
15365{
15366 if (strcmp (".opd", sec->name) == 0)
15367 return 0;
15368
15369 if (strcmp (".toc", sec->name) == 0)
15370 return 0;
15371
bce50a28
JJ
15372 if (strcmp (".toc1", sec->name) == 0)
15373 return 0;
15374
60124e18
AM
15375 return _bfd_elf_default_action_discarded (sec);
15376}
15377
e59a1001
AM
15378/* These are the dynamic relocations supported by glibc. */
15379
0a1b45a2 15380static bool
e59a1001
AM
15381ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15382{
15383 switch (r_type)
15384 {
15385 case R_PPC64_RELATIVE:
15386 case R_PPC64_NONE:
15387 case R_PPC64_ADDR64:
15388 case R_PPC64_GLOB_DAT:
15389 case R_PPC64_IRELATIVE:
15390 case R_PPC64_JMP_IREL:
15391 case R_PPC64_JMP_SLOT:
15392 case R_PPC64_DTPMOD64:
15393 case R_PPC64_DTPREL64:
15394 case R_PPC64_TPREL64:
15395 case R_PPC64_TPREL16_LO_DS:
15396 case R_PPC64_TPREL16_DS:
15397 case R_PPC64_TPREL16:
15398 case R_PPC64_TPREL16_LO:
15399 case R_PPC64_TPREL16_HI:
15400 case R_PPC64_TPREL16_HIGH:
15401 case R_PPC64_TPREL16_HA:
15402 case R_PPC64_TPREL16_HIGHA:
15403 case R_PPC64_TPREL16_HIGHER:
15404 case R_PPC64_TPREL16_HIGHEST:
15405 case R_PPC64_TPREL16_HIGHERA:
15406 case R_PPC64_TPREL16_HIGHESTA:
15407 case R_PPC64_ADDR16_LO_DS:
15408 case R_PPC64_ADDR16_LO:
15409 case R_PPC64_ADDR16_HI:
15410 case R_PPC64_ADDR16_HIGH:
15411 case R_PPC64_ADDR16_HA:
15412 case R_PPC64_ADDR16_HIGHA:
15413 case R_PPC64_REL30:
15414 case R_PPC64_COPY:
15415 case R_PPC64_UADDR64:
15416 case R_PPC64_UADDR32:
15417 case R_PPC64_ADDR32:
15418 case R_PPC64_ADDR24:
15419 case R_PPC64_ADDR16:
15420 case R_PPC64_UADDR16:
15421 case R_PPC64_ADDR16_DS:
15422 case R_PPC64_ADDR16_HIGHER:
15423 case R_PPC64_ADDR16_HIGHEST:
15424 case R_PPC64_ADDR16_HIGHERA:
15425 case R_PPC64_ADDR16_HIGHESTA:
15426 case R_PPC64_ADDR14:
15427 case R_PPC64_ADDR14_BRTAKEN:
15428 case R_PPC64_ADDR14_BRNTAKEN:
15429 case R_PPC64_REL32:
15430 case R_PPC64_REL64:
0a1b45a2 15431 return true;
e59a1001
AM
15432
15433 default:
0a1b45a2 15434 return false;
e59a1001
AM
15435 }
15436}
15437
5bd4f169
AM
15438/* The RELOCATE_SECTION function is called by the ELF backend linker
15439 to handle the relocations for a section.
15440
15441 The relocs are always passed as Rela structures; if the section
15442 actually uses Rel structures, the r_addend field will always be
15443 zero.
15444
15445 This function is responsible for adjust the section contents as
15446 necessary, and (if using Rela relocs and generating a
1049f94e 15447 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
15448 necessary.
15449
15450 This function does not have to worry about setting the reloc
15451 address or the reloc symbol index.
15452
15453 LOCAL_SYMS is a pointer to the swapped in local symbols.
15454
15455 LOCAL_SECTIONS is an array giving the section in the input file
15456 corresponding to the st_shndx field of each local symbol.
15457
15458 The global hash table entry for the global symbols can be found
15459 via elf_sym_hashes (input_bfd).
15460
1049f94e 15461 When generating relocatable output, this function must handle
5bd4f169
AM
15462 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15463 going to be the section symbol corresponding to the output
15464 section, which means that the addend must be adjusted
15465 accordingly. */
15466
0f684201 15467static int
4ce794b7
AM
15468ppc64_elf_relocate_section (bfd *output_bfd,
15469 struct bfd_link_info *info,
15470 bfd *input_bfd,
15471 asection *input_section,
15472 bfd_byte *contents,
15473 Elf_Internal_Rela *relocs,
15474 Elf_Internal_Sym *local_syms,
15475 asection **local_sections)
5bd4f169 15476{
65f38f15 15477 struct ppc_link_hash_table *htab;
5bd4f169
AM
15478 Elf_Internal_Shdr *symtab_hdr;
15479 struct elf_link_hash_entry **sym_hashes;
5bd4f169 15480 Elf_Internal_Rela *rel;
c316a17c 15481 Elf_Internal_Rela *wrel;
5bd4f169 15482 Elf_Internal_Rela *relend;
411e1bfb
AM
15483 Elf_Internal_Rela outrel;
15484 bfd_byte *loc;
411e1bfb 15485 struct got_entry **local_got_ents;
5bd4f169 15486 bfd_vma TOCstart;
0a1b45a2
AM
15487 bool ret = true;
15488 bool is_opd;
794e51c0 15489 /* Assume 'at' branch hints. */
0a1b45a2
AM
15490 bool is_isa_v2 = true;
15491 bool warned_dynamic = false;
95f0d0d2 15492 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 15493
65f38f15 15494 /* Initialize howto table if needed. */
5bd4f169 15495 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
15496 ppc_howto_init ();
15497
65f38f15 15498 htab = ppc_hash_table (info);
4dfe6ac6 15499 if (htab == NULL)
0a1b45a2 15500 return false;
ee75fd95
AM
15501
15502 /* Don't relocate stub sections. */
e7d1c40c 15503 if (input_section->owner == htab->params->stub_bfd)
0a1b45a2 15504 return true;
ee75fd95 15505
7af5d5c4
AM
15506 if (!is_ppc64_elf (input_bfd))
15507 {
15508 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 15509 return false;
7af5d5c4 15510 }
0ffa91dd 15511
411e1bfb 15512 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 15513 TOCstart = elf_gp (output_bfd);
0ffa91dd 15514 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 15515 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 15516 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 15517
c316a17c 15518 rel = wrel = relocs;
5bd4f169 15519 relend = relocs + input_section->reloc_count;
c316a17c 15520 for (; rel < relend; wrel++, rel++)
5bd4f169 15521 {
04c9666a 15522 enum elf_ppc64_reloc_type r_type;
31c76678 15523 bfd_vma addend;
5bd4f169
AM
15524 bfd_reloc_status_type r;
15525 Elf_Internal_Sym *sym;
15526 asection *sec;
039b3fef
AM
15527 struct elf_link_hash_entry *h_elf;
15528 struct ppc_link_hash_entry *h;
15529 struct ppc_link_hash_entry *fdh;
5bd4f169 15530 const char *sym_name;
0d4792f7 15531 unsigned long r_symndx, toc_symndx;
3a71aa26 15532 bfd_vma toc_addend;
f961d9dd
AM
15533 unsigned char tls_mask, tls_gd, tls_type;
15534 unsigned char sym_type;
5bd4f169 15535 bfd_vma relocation;
0a1b45a2
AM
15536 bool unresolved_reloc, save_unresolved_reloc;
15537 bool warned;
bc30df16 15538 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 15539 unsigned int insn;
e11840f9 15540 unsigned int mask;
721956f4
AM
15541 struct ppc_stub_hash_entry *stub_entry;
15542 bfd_vma max_br_offset;
15543 bfd_vma from;
c316a17c 15544 Elf_Internal_Rela orig_rel;
b80eed39
AM
15545 reloc_howto_type *howto;
15546 struct reloc_howto_struct alt_howto;
4a421c53
AM
15547 uint64_t pinsn;
15548 bfd_vma offset;
5bd4f169 15549
c316a17c
AM
15550 again:
15551 orig_rel = *rel;
15552
4ce794b7 15553 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 15554 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
15555
15556 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15557 symbol of the previous ADDR64 reloc. The symbol gives us the
15558 proper TOC base to use. */
15559 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
15560 && wrel != relocs
15561 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 15562 && is_opd)
c316a17c 15563 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 15564
4ce794b7
AM
15565 sym = NULL;
15566 sec = NULL;
039b3fef 15567 h_elf = NULL;
4ce794b7 15568 sym_name = NULL;
0a1b45a2
AM
15569 unresolved_reloc = false;
15570 warned = false;
65f38f15 15571
0b13192e 15572 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15573 {
15574 /* It's a local symbol. */
74f0fb50 15575 struct _opd_sec_data *opd;
4025353c 15576
5bd4f169
AM
15577 sym = local_syms + r_symndx;
15578 sec = local_sections[r_symndx];
26c61ae5 15579 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15580 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15581 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15582 opd = get_opd_info (sec);
15583 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15584 {
51aecdc5
AM
15585 long adjust = opd->adjust[OPD_NDX (sym->st_value
15586 + rel->r_addend)];
4025353c
AM
15587 if (adjust == -1)
15588 relocation = 0;
15589 else
4cc603a5
AM
15590 {
15591 /* If this is a relocation against the opd section sym
15592 and we have edited .opd, adjust the reloc addend so
15593 that ld -r and ld --emit-relocs output is correct.
15594 If it is a reloc against some other .opd symbol,
15595 then the symbol value will be adjusted later. */
15596 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15597 rel->r_addend += adjust;
15598 else
15599 relocation += adjust;
15600 }
1e2f5b6e 15601 }
5bd4f169
AM
15602 }
15603 else
15604 {
0a1b45a2 15605 bool ignored;
62d887d4 15606
b2a8e766
AM
15607 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15608 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15609 h_elf, sec, relocation,
62d887d4 15610 unresolved_reloc, warned, ignored);
039b3fef
AM
15611 sym_name = h_elf->root.root.string;
15612 sym_type = h_elf->type;
b69fdb4e
AM
15613 if (sec != NULL
15614 && sec->owner == output_bfd
15615 && strcmp (sec->name, ".opd") == 0)
15616 {
15617 /* This is a symbol defined in a linker script. All
15618 such are defined in output sections, even those
15619 defined by simple assignment from a symbol defined in
15620 an input section. Transfer the symbol to an
15621 appropriate input .opd section, so that a branch to
15622 this symbol will be mapped to the location specified
15623 by the opd entry. */
15624 struct bfd_link_order *lo;
15625 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15626 if (lo->type == bfd_indirect_link_order)
15627 {
15628 asection *isec = lo->u.indirect.section;
15629 if (h_elf->root.u.def.value >= isec->output_offset
15630 && h_elf->root.u.def.value < (isec->output_offset
15631 + isec->size))
15632 {
15633 h_elf->root.u.def.value -= isec->output_offset;
15634 h_elf->root.u.def.section = isec;
15635 sec = isec;
15636 break;
15637 }
15638 }
15639 }
5bd4f169 15640 }
ed7007c1 15641 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15642
dbaa2011 15643 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15644 {
15645 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15646 input_bfd, input_section,
0930cb30 15647 contents, rel->r_offset);
c316a17c
AM
15648 wrel->r_offset = rel->r_offset;
15649 wrel->r_info = 0;
15650 wrel->r_addend = 0;
15651
15652 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15653 symbols defined in discarded sections. Not done for
c316a17c
AM
15654 non-debug to preserve relocs in .eh_frame which the
15655 eh_frame editing code expects to be present. */
15656 if (bfd_link_relocatable (info)
15657 && (input_section->flags & SEC_DEBUGGING))
15658 wrel--;
15659
15660 continue;
15661 }
ab96bf03 15662
0e1862bb 15663 if (bfd_link_relocatable (info))
c316a17c 15664 goto copy_reloc;
ab96bf03 15665
f40da81b
AM
15666 if (h != NULL && &h->elf == htab->elf.hgot)
15667 {
6f20ed8a 15668 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b 15669 sec = bfd_abs_section_ptr;
0a1b45a2 15670 unresolved_reloc = false;
f40da81b
AM
15671 }
15672
951fd09b
AM
15673 /* TLS optimizations. Replace instruction sequences and relocs
15674 based on information we collected in tls_optimize. We edit
15675 RELOCS so that --emit-relocs will output something sensible
15676 for the final instruction stream. */
15677 tls_mask = 0;
15678 tls_gd = 0;
0d4792f7 15679 toc_symndx = 0;
727fc41e
AM
15680 if (h != NULL)
15681 tls_mask = h->tls_mask;
15682 else if (local_got_ents != NULL)
411e1bfb 15683 {
e054468f
AM
15684 struct plt_entry **local_plt = (struct plt_entry **)
15685 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15686 unsigned char *lgot_masks = (unsigned char *)
e054468f 15687 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15688 tls_mask = lgot_masks[r_symndx];
15689 }
37da22e5 15690 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15691 && (r_type == R_PPC64_TLS
15692 || r_type == R_PPC64_TLSGD
15693 || r_type == R_PPC64_TLSLD))
15694 {
15695 /* Check for toc tls entries. */
f961d9dd 15696 unsigned char *toc_tls;
0d4792f7 15697
727fc41e
AM
15698 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15699 &local_syms, rel, input_bfd))
0a1b45a2 15700 return false;
0d4792f7 15701
727fc41e
AM
15702 if (toc_tls)
15703 tls_mask = *toc_tls;
0d4792f7
AM
15704 }
15705
15706 /* Check that tls relocs are used with tls syms, and non-tls
15707 relocs are used with non-tls syms. */
cf35638d 15708 if (r_symndx != STN_UNDEF
0d4792f7
AM
15709 && r_type != R_PPC64_NONE
15710 && (h == NULL
039b3fef
AM
15711 || h->elf.root.type == bfd_link_hash_defined
15712 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15713 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15714 {
37da22e5 15715 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15716 && (r_type == R_PPC64_TLS
15717 || r_type == R_PPC64_TLSGD
15718 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15719 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15720 ;
15721 else
25f53a85 15722 info->callbacks->einfo
1d483afe 15723 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15724 /* xgettext:c-format */
c1c8c1ef 15725 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15726 /* xgettext:c-format */
c1c8c1ef 15727 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15728 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15729 ppc64_elf_howto_table[r_type]->name,
15730 sym_name);
411e1bfb
AM
15731 }
15732
15733 /* Ensure reloc mapping code below stays sane. */
15734 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15735 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15736 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15737 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15738 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15739 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15740 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15741 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15742 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15743 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15744 abort ();
0d4792f7 15745
411e1bfb
AM
15746 switch (r_type)
15747 {
15748 default:
411e1bfb
AM
15749 break;
15750
ba761f19 15751 case R_PPC64_LO_DS_OPT:
733ae98c
AM
15752 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15753 {
15754 insn = bfd_get_32 (input_bfd,
15755 contents + rel->r_offset - d_offset);
15756 if ((insn & (0x3fu << 26)) != 58u << 26)
15757 abort ();
15758 insn += (14u << 26) - (58u << 26);
15759 bfd_put_32 (input_bfd, insn,
15760 contents + rel->r_offset - d_offset);
15761 r_type = R_PPC64_TOC16_LO;
15762 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15763 }
ba761f19
AM
15764 break;
15765
411e1bfb
AM
15766 case R_PPC64_TOC16:
15767 case R_PPC64_TOC16_LO:
15768 case R_PPC64_TOC16_DS:
15769 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15770 {
15771 /* Check for toc tls entries. */
f961d9dd 15772 unsigned char *toc_tls;
951fd09b 15773 int retval;
411e1bfb 15774
3a71aa26
AM
15775 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15776 &local_syms, rel, input_bfd);
951fd09b 15777 if (retval == 0)
0a1b45a2 15778 return false;
411e1bfb
AM
15779
15780 if (toc_tls)
15781 {
951fd09b 15782 tls_mask = *toc_tls;
411e1bfb
AM
15783 if (r_type == R_PPC64_TOC16_DS
15784 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15785 {
37da22e5 15786 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15787 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15788 goto toctprel;
15789 }
411e1bfb 15790 else
951fd09b
AM
15791 {
15792 /* If we found a GD reloc pair, then we might be
15793 doing a GD->IE transition. */
15794 if (retval == 2)
15795 {
b00a0a86 15796 tls_gd = TLS_GDIE;
37da22e5
AM
15797 if ((tls_mask & TLS_TLS) != 0
15798 && (tls_mask & TLS_GD) == 0)
102890f0 15799 goto tls_ldgd_opt;
951fd09b
AM
15800 }
15801 else if (retval == 3)
15802 {
37da22e5
AM
15803 if ((tls_mask & TLS_TLS) != 0
15804 && (tls_mask & TLS_LD) == 0)
102890f0 15805 goto tls_ldgd_opt;
951fd09b
AM
15806 }
15807 }
411e1bfb
AM
15808 }
15809 }
15810 break;
15811
9d6ded02
AM
15812 case R_PPC64_GOT_TPREL16_HI:
15813 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15814 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15815 && (tls_mask & TLS_TPREL) == 0
15816 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
9d6ded02
AM
15817 {
15818 rel->r_offset -= d_offset;
95f0d0d2 15819 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15820 r_type = R_PPC64_NONE;
15821 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15822 }
15823 break;
15824
411e1bfb
AM
15825 case R_PPC64_GOT_TPREL16_DS:
15826 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15827 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15828 && (tls_mask & TLS_TPREL) == 0
15829 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
411e1bfb 15830 {
81407a69 15831 toctprel:
95f0d0d2 15832 insn = bfd_get_32 (input_bfd,
c316a17c 15833 contents + rel->r_offset - d_offset);
411e1bfb
AM
15834 insn &= 31 << 21;
15835 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15836 bfd_put_32 (input_bfd, insn,
c316a17c 15837 contents + rel->r_offset - d_offset);
411e1bfb 15838 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15839 if (toc_symndx != 0)
15840 {
15841 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15842 rel->r_addend = toc_addend;
0d4792f7
AM
15843 /* We changed the symbol. Start over in order to
15844 get h, sym, sec etc. right. */
c316a17c 15845 goto again;
0d4792f7
AM
15846 }
15847 else
15848 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15849 }
15850 break;
15851
87c69f97 15852 case R_PPC64_GOT_TPREL_PCREL34:
c213164a 15853 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15854 && (tls_mask & TLS_TPREL) == 0
15855 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15856 {
15857 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15858 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15859 pinsn <<= 32;
15860 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15861 pinsn += ((2ULL << 56) + (-1ULL << 52)
15862 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15863 bfd_put_32 (input_bfd, pinsn >> 32,
15864 contents + rel->r_offset);
15865 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15866 contents + rel->r_offset + 4);
15867 r_type = R_PPC64_TPREL34;
15868 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15869 }
15870 break;
15871
411e1bfb 15872 case R_PPC64_TLS:
37da22e5 15873 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15874 && (tls_mask & TLS_TPREL) == 0
15875 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15876 {
c213164a 15877 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15878 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15879 if (insn == 0)
c213164a
AM
15880 break;
15881 if ((rel->r_offset & 3) == 0)
0d4792f7 15882 {
c213164a
AM
15883 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15884 /* Was PPC64_TLS which sits on insn boundary, now
15885 PPC64_TPREL16_LO which is at low-order half-word. */
15886 rel->r_offset += d_offset;
15887 r_type = R_PPC64_TPREL16_LO;
15888 if (toc_symndx != 0)
15889 {
15890 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15891 rel->r_addend = toc_addend;
15892 /* We changed the symbol. Start over in order to
15893 get h, sym, sec etc. right. */
15894 goto again;
15895 }
15896 else
15897 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15898 }
15899 else if ((rel->r_offset & 3) == 1)
15900 {
15901 /* For pcrel IE to LE we already have the full
15902 offset and thus don't need an addi here. A nop
15903 or mr will do. */
2365f8d7 15904 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15905 {
15906 /* Extract regs from addi rt,ra,si. */
15907 unsigned int rt = (insn >> 21) & 0x1f;
15908 unsigned int ra = (insn >> 16) & 0x1f;
15909 if (rt == ra)
15910 insn = NOP;
15911 else
15912 {
15913 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15914 insn = (rt << 16) | (ra << 21) | (ra << 11);
15915 insn |= (31u << 26) | (444u << 1);
15916 }
15917 }
15918 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15919 }
411e1bfb
AM
15920 }
15921 break;
15922
411e1bfb
AM
15923 case R_PPC64_GOT_TLSGD16_HI:
15924 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15925 tls_gd = TLS_GDIE;
733ae98c
AM
15926 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15927 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b
AM
15928 goto tls_gdld_hi;
15929 break;
15930
411e1bfb
AM
15931 case R_PPC64_GOT_TLSLD16_HI:
15932 case R_PPC64_GOT_TLSLD16_HA:
733ae98c
AM
15933 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15934 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15935 {
951fd09b
AM
15936 tls_gdld_hi:
15937 if ((tls_mask & tls_gd) != 0)
15938 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15939 + R_PPC64_GOT_TPREL16_DS);
15940 else
411e1bfb 15941 {
4fe5ca5b 15942 rel->r_offset -= d_offset;
95f0d0d2 15943 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15944 r_type = R_PPC64_NONE;
411e1bfb 15945 }
951fd09b 15946 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15947 }
15948 break;
15949
951fd09b
AM
15950 case R_PPC64_GOT_TLSGD16:
15951 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15952 tls_gd = TLS_GDIE;
733ae98c
AM
15953 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15954 && offset_in_range (input_section, rel->r_offset & ~3, 4))
102890f0 15955 goto tls_ldgd_opt;
951fd09b 15956 break;
411e1bfb 15957
951fd09b
AM
15958 case R_PPC64_GOT_TLSLD16:
15959 case R_PPC64_GOT_TLSLD16_LO:
733ae98c
AM
15960 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15961 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b 15962 {
b9f04fe0 15963 unsigned int insn1, insn2;
102890f0
AM
15964
15965 tls_ldgd_opt:
727fc41e
AM
15966 offset = (bfd_vma) -1;
15967 /* If not using the newer R_PPC64_TLSGD/LD to mark
15968 __tls_get_addr calls, we must trust that the call
15969 stays with its arg setup insns, ie. that the next
15970 reloc is the __tls_get_addr call associated with
15971 the current reloc. Edit both insns. */
9737e8af 15972 if (input_section->nomark_tls_get_addr
727fc41e
AM
15973 && rel + 1 < relend
15974 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15975 htab->tls_get_addr_fd,
15976 htab->tga_desc_fd,
727fc41e 15977 htab->tls_get_addr,
9e7028aa 15978 htab->tga_desc))
727fc41e 15979 offset = rel[1].r_offset;
b86ac8e3
AM
15980 /* We read the low GOT_TLS (or TOC16) insn because we
15981 need to keep the destination reg. It may be
15982 something other than the usual r3, and moved to r3
15983 before the call by intervening code. */
95f0d0d2 15984 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15985 contents + rel->r_offset - d_offset);
102890f0 15986 if ((tls_mask & tls_gd) != 0)
411e1bfb 15987 {
102890f0 15988 /* IE */
b86ac8e3 15989 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15990 insn1 |= 58u << 26; /* ld */
102890f0 15991 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15992 if (offset != (bfd_vma) -1)
f58d5a2d 15993 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15994 if (r_type == R_PPC64_TOC16
15995 || r_type == R_PPC64_TOC16_LO)
102890f0 15996 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15997 else
15998 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15999 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
16000 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16001 }
16002 else
16003 {
16004 /* LE */
b86ac8e3
AM
16005 insn1 &= 0x1f << 21;
16006 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
16007 insn2 = 0x38630000; /* addi 3,3,0 */
16008 if (tls_gd == 0)
951fd09b 16009 {
102890f0 16010 /* Was an LD reloc. */
71c4e95a 16011 r_symndx = STN_UNDEF;
102890f0 16012 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 16013 }
102890f0 16014 else if (toc_symndx != 0)
3a71aa26
AM
16015 {
16016 r_symndx = toc_symndx;
16017 rel->r_addend = toc_addend;
16018 }
102890f0
AM
16019 r_type = R_PPC64_TPREL16_HA;
16020 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
16021 if (offset != (bfd_vma) -1)
16022 {
16023 rel[1].r_info = ELF64_R_INFO (r_symndx,
16024 R_PPC64_TPREL16_LO);
16025 rel[1].r_offset = offset + d_offset;
16026 rel[1].r_addend = rel->r_addend;
16027 }
102890f0 16028 }
95f0d0d2 16029 bfd_put_32 (input_bfd, insn1,
3a71aa26 16030 contents + rel->r_offset - d_offset);
733ae98c
AM
16031 if (offset != (bfd_vma) -1
16032 && offset_in_range (input_section, offset, 4))
c96e0573
AM
16033 {
16034 bfd_put_32 (input_bfd, insn2, contents + offset);
733ae98c 16035 if (offset_in_range (input_section, offset + 4, 4))
c96e0573
AM
16036 {
16037 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16038 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
16039 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
16040 }
16041 }
727fc41e
AM
16042 if ((tls_mask & tls_gd) == 0
16043 && (tls_gd == 0 || toc_symndx != 0))
16044 {
16045 /* We changed the symbol. Start over in order
16046 to get h, sym, sec etc. right. */
c316a17c 16047 goto again;
727fc41e
AM
16048 }
16049 }
16050 break;
16051
87c69f97 16052 case R_PPC64_GOT_TLSGD_PCREL34:
733ae98c
AM
16053 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16054 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
16055 {
16056 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16057 pinsn <<= 32;
16058 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16059 if ((tls_mask & TLS_GDIE) != 0)
16060 {
16061 /* IE, pla -> pld */
16062 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 16063 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
16064 }
16065 else
16066 {
16067 /* LE, pla pcrel -> paddi r13 */
16068 pinsn += (-1ULL << 52) + (13ULL << 16);
16069 r_type = R_PPC64_TPREL34;
16070 }
16071 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16072 bfd_put_32 (input_bfd, pinsn >> 32,
16073 contents + rel->r_offset);
16074 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16075 contents + rel->r_offset + 4);
16076 }
16077 break;
16078
87c69f97 16079 case R_PPC64_GOT_TLSLD_PCREL34:
733ae98c
AM
16080 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16081 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
16082 {
16083 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16084 pinsn <<= 32;
16085 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16086 pinsn += (-1ULL << 52) + (13ULL << 16);
16087 bfd_put_32 (input_bfd, pinsn >> 32,
16088 contents + rel->r_offset);
16089 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16090 contents + rel->r_offset + 4);
16091 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16092 r_symndx = STN_UNDEF;
16093 r_type = R_PPC64_TPREL34;
16094 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16095 goto again;
16096 }
16097 break;
16098
727fc41e 16099 case R_PPC64_TLSGD:
37da22e5 16100 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
733ae98c
AM
16101 && rel + 1 < relend
16102 && offset_in_range (input_section, rel->r_offset,
16103 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16104 ? 8 : 4))
727fc41e 16105 {
b9f04fe0 16106 unsigned int insn2;
5663e321 16107 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16108
4a421c53 16109 offset = rel->r_offset;
5663e321 16110 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16111 {
16112 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16113 if (r_type1 == R_PPC64_PLT_PCREL34
16114 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16115 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16116 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16117 break;
16118 }
16119
733ae98c 16120 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16121 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16122
b00a0a86 16123 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
16124 {
16125 /* IE */
16126 r_type = R_PPC64_NONE;
16127 insn2 = 0x7c636a14; /* add 3,3,13 */
16128 }
16129 else
16130 {
16131 /* LE */
16132 if (toc_symndx != 0)
16133 {
16134 r_symndx = toc_symndx;
16135 rel->r_addend = toc_addend;
16136 }
c213164a 16137 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16138 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16139 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16140 {
16141 r_type = R_PPC64_NONE;
16142 insn2 = NOP;
16143 }
16144 else
16145 {
16146 rel->r_offset = offset + d_offset;
16147 r_type = R_PPC64_TPREL16_LO;
16148 insn2 = 0x38630000; /* addi 3,3,0 */
16149 }
727fc41e
AM
16150 }
16151 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16152 /* Zap the reloc on the _tls_get_addr call too. */
16153 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16154 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16155 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16156 if ((tls_mask & TLS_GDIE) == 0
16157 && toc_symndx != 0
16158 && r_type != R_PPC64_NONE)
c316a17c 16159 goto again;
411e1bfb 16160 }
411e1bfb
AM
16161 break;
16162
727fc41e 16163 case R_PPC64_TLSLD:
37da22e5 16164 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
733ae98c
AM
16165 && rel + 1 < relend
16166 && offset_in_range (input_section, rel->r_offset,
16167 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16168 ? 8 : 4))
727fc41e 16169 {
b9f04fe0 16170 unsigned int insn2;
5663e321 16171 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16172
4a421c53 16173 offset = rel->r_offset;
5663e321 16174 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16175 {
16176 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16177 if (r_type1 == R_PPC64_PLT_PCREL34
16178 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16179 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16180 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16181 break;
16182 }
16183
733ae98c 16184 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16185 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16186
c213164a 16187 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16188 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16189 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16190 {
16191 r_type = R_PPC64_NONE;
16192 insn2 = NOP;
16193 }
16194 else
16195 {
16196 rel->r_offset = offset + d_offset;
16197 r_symndx = STN_UNDEF;
16198 r_type = R_PPC64_TPREL16_LO;
16199 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16200 insn2 = 0x38630000; /* addi 3,3,0 */
16201 }
727fc41e 16202 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
16203 /* Zap the reloc on the _tls_get_addr call too. */
16204 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16205 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16206 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16207 if (r_type != R_PPC64_NONE)
16208 goto again;
727fc41e
AM
16209 }
16210 break;
16211
411e1bfb 16212 case R_PPC64_DTPMOD64:
951fd09b
AM
16213 if (rel + 1 < relend
16214 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16215 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 16216 {
733ae98c
AM
16217 if ((tls_mask & TLS_GD) == 0
16218 && offset_in_range (input_section, rel->r_offset, 8))
951fd09b
AM
16219 {
16220 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 16221 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
16222 r_type = R_PPC64_TPREL64;
16223 else
16224 {
4ce794b7 16225 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
16226 r_type = R_PPC64_NONE;
16227 }
16228 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16229 }
16230 }
16231 else
16232 {
733ae98c
AM
16233 if ((tls_mask & TLS_LD) == 0
16234 && offset_in_range (input_section, rel->r_offset, 8))
411e1bfb 16235 {
4ce794b7 16236 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 16237 r_type = R_PPC64_NONE;
951fd09b 16238 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 16239 }
411e1bfb
AM
16240 }
16241 break;
16242
16243 case R_PPC64_TPREL64:
951fd09b 16244 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
16245 {
16246 r_type = R_PPC64_NONE;
16247 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16248 }
16249 break;
52a82034 16250
006589cf
AM
16251 case R_PPC64_ENTRY:
16252 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16253 if (!bfd_link_pic (info)
16254 && !info->traditional_format
733ae98c
AM
16255 && relocation + 0x80008000 <= 0xffffffff
16256 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16257 {
16258 unsigned int insn1, insn2;
16259
16260 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16261 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16262 if ((insn1 & ~0xfffc) == LD_R2_0R12
16263 && insn2 == ADD_R2_R2_R12)
16264 {
95f0d0d2 16265 bfd_put_32 (input_bfd,
006589cf
AM
16266 LIS_R2 + PPC_HA (relocation),
16267 contents + rel->r_offset);
95f0d0d2 16268 bfd_put_32 (input_bfd,
006589cf
AM
16269 ADDI_R2_R2 + PPC_LO (relocation),
16270 contents + rel->r_offset + 4);
16271 }
16272 }
16273 else
16274 {
16275 relocation -= (rel->r_offset
16276 + input_section->output_offset
16277 + input_section->output_section->vma);
733ae98c
AM
16278 if (relocation + 0x80008000 <= 0xffffffff
16279 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16280 {
16281 unsigned int insn1, insn2;
16282
16283 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16284 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16285 if ((insn1 & ~0xfffc) == LD_R2_0R12
16286 && insn2 == ADD_R2_R2_R12)
16287 {
95f0d0d2 16288 bfd_put_32 (input_bfd,
006589cf
AM
16289 ADDIS_R2_R12 + PPC_HA (relocation),
16290 contents + rel->r_offset);
95f0d0d2 16291 bfd_put_32 (input_bfd,
006589cf
AM
16292 ADDI_R2_R2 + PPC_LO (relocation),
16293 contents + rel->r_offset + 4);
16294 }
16295 }
16296 }
16297 break;
16298
52a82034
AM
16299 case R_PPC64_REL16_HA:
16300 /* If we are generating a non-PIC executable, edit
16301 . 0: addis 2,12,.TOC.-0b@ha
16302 . addi 2,2,.TOC.-0b@l
16303 used by ELFv2 global entry points to set up r2, to
16304 . lis 2,.TOC.@ha
16305 . addi 2,2,.TOC.@l
16306 if .TOC. is in range. */
0e1862bb 16307 if (!bfd_link_pic (info)
810d4e75 16308 && !info->traditional_format
006589cf 16309 && !htab->opd_abi
4f038ee5 16310 && rel->r_addend == d_offset
52a82034
AM
16311 && h != NULL && &h->elf == htab->elf.hgot
16312 && rel + 1 < relend
16313 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16314 && rel[1].r_offset == rel->r_offset + 4
16315 && rel[1].r_addend == rel->r_addend + 4
733ae98c
AM
16316 && relocation + 0x80008000 <= 0xffffffff
16317 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
52a82034
AM
16318 {
16319 unsigned int insn1, insn2;
4a421c53 16320 offset = rel->r_offset - d_offset;
95f0d0d2
AM
16321 insn1 = bfd_get_32 (input_bfd, contents + offset);
16322 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
16323 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16324 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
16325 {
16326 r_type = R_PPC64_ADDR16_HA;
16327 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16328 rel->r_addend -= d_offset;
16329 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16330 rel[1].r_addend -= d_offset + 4;
95f0d0d2 16331 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
16332 }
16333 }
16334 break;
411e1bfb
AM
16335 }
16336
16337 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 16338 insn = 0;
b25116a9
AM
16339 max_br_offset = 1 << 25;
16340 addend = rel->r_addend;
bc30df16 16341 reloc_dest = DEST_NORMAL;
65f38f15 16342 switch (r_type)
5bd4f169
AM
16343 {
16344 default:
65f38f15 16345 break;
5bd4f169 16346
3b421ab3
AM
16347 case R_PPC64_TOCSAVE:
16348 if (relocation + addend == (rel->r_offset
16349 + input_section->output_offset
16350 + input_section->output_section->vma)
16351 && tocsave_find (htab, NO_INSERT,
733ae98c
AM
16352 &local_syms, rel, input_bfd)
16353 && offset_in_range (input_section, rel->r_offset, 4))
3b421ab3
AM
16354 {
16355 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16356 if (insn == NOP
16357 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
16358 bfd_put_32 (input_bfd,
16359 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
16360 contents + rel->r_offset);
16361 }
16362 break;
16363
65f38f15
AM
16364 /* Branch taken prediction relocations. */
16365 case R_PPC64_ADDR14_BRTAKEN:
16366 case R_PPC64_REL14_BRTAKEN:
cedb70c5 16367 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 16368 /* Fall through. */
65f38f15 16369
86c76c7b 16370 /* Branch not taken prediction relocations. */
65f38f15
AM
16371 case R_PPC64_ADDR14_BRNTAKEN:
16372 case R_PPC64_REL14_BRNTAKEN:
733ae98c
AM
16373 if (!offset_in_range (input_section, rel->r_offset, 4))
16374 break;
95f0d0d2 16375 insn |= bfd_get_32 (input_bfd,
411e1bfb 16376 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 16377 /* Fall through. */
86c76c7b 16378
b25116a9
AM
16379 case R_PPC64_REL14:
16380 max_br_offset = 1 << 15;
1a0670f3 16381 /* Fall through. */
5bd4f169 16382
65f38f15 16383 case R_PPC64_REL24:
05d0e962 16384 case R_PPC64_REL24_NOTOC:
7aba54da 16385 case R_PPC64_REL24_P9NOTOC:
23cedd1d 16386 case R_PPC64_PLTCALL:
5663e321 16387 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
16388 /* Calls to functions with a different TOC, such as calls to
16389 shared objects, need to alter the TOC pointer. This is
16390 done using a linkage stub. A REL24 branching to these
16391 linkage stubs needs to be followed by a nop, as the nop
16392 will be replaced with an instruction to restore the TOC
16393 base pointer. */
8387904d 16394 fdh = h;
b31867b6
AM
16395 if (h != NULL
16396 && h->oh != NULL
16397 && h->oh->is_func_descriptor)
16398 fdh = ppc_follow_link (h->oh);
31c76678
DK
16399 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16400 htab);
5663e321
AM
16401 if ((r_type == R_PPC64_PLTCALL
16402 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 16403 && stub_entry != NULL
7aba54da 16404 && stub_entry->type.main == ppc_stub_plt_call)
23cedd1d
AM
16405 stub_entry = NULL;
16406
6abec6bc 16407 if (stub_entry != NULL
7aba54da
AM
16408 && (stub_entry->type.main == ppc_stub_plt_call
16409 || stub_entry->type.r2save))
41bd81ab 16410 {
0a1b45a2 16411 bool can_plt_call = false;
721956f4 16412
7aba54da
AM
16413 if (r_type == R_PPC64_REL24_NOTOC
16414 || r_type == R_PPC64_REL24_P9NOTOC)
6e1816be 16415 {
7aba54da 16416 /* NOTOC calls don't need to restore r2. */
0a1b45a2 16417 can_plt_call = true;
6e1816be 16418 }
7aba54da
AM
16419 else if (stub_entry->type.main == ppc_stub_plt_call
16420 && !htab->opd_abi
16421 && htab->params->plt_localentry0 != 0
16422 && h != NULL
16423 && is_elfv2_localentry0 (&h->elf))
05d0e962 16424 {
7aba54da 16425 /* The function doesn't use or change r2. */
0a1b45a2 16426 can_plt_call = true;
05d0e962 16427 }
6e1816be 16428
f378ab09 16429 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
16430 branch and link followed by a nop. The nop is
16431 replaced by an insn to restore r2. */
733ae98c 16432 else if (offset_in_range (input_section, rel->r_offset, 8))
41bd81ab 16433 {
ba8ca3e7
AM
16434 unsigned long br;
16435
16436 br = bfd_get_32 (input_bfd,
16437 contents + rel->r_offset);
16438 if ((br & 1) != 0)
41bd81ab 16439 {
ba8ca3e7
AM
16440 unsigned long nop;
16441
16442 nop = bfd_get_32 (input_bfd,
16443 contents + rel->r_offset + 4);
23cedd1d 16444 if (nop == LD_R2_0R1 + STK_TOC (htab))
0a1b45a2 16445 can_plt_call = true;
23cedd1d
AM
16446 else if (nop == NOP
16447 || nop == CROR_151515
16448 || nop == CROR_313131)
a7f2871e 16449 {
ba8ca3e7 16450 if (h != NULL
ed7007c1 16451 && is_tls_get_addr (&h->elf, htab)
7c9cf415 16452 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
16453 {
16454 /* Special stub used, leave nop alone. */
16455 }
16456 else
a078d95a
AM
16457 bfd_put_32 (input_bfd,
16458 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7 16459 contents + rel->r_offset + 4);
0a1b45a2 16460 can_plt_call = true;
a7f2871e 16461 }
41bd81ab 16462 }
5bd4f169 16463 }
721956f4 16464
ba8ca3e7 16465 if (!can_plt_call && h != NULL)
721956f4 16466 {
ba8ca3e7
AM
16467 const char *name = h->elf.root.root.string;
16468
16469 if (*name == '.')
16470 ++name;
16471
3f3328b8 16472 if (startswith (name, "__libc_start_main")
ba8ca3e7 16473 && (name[17] == 0 || name[17] == '@'))
6ab189d5 16474 {
ba8ca3e7
AM
16475 /* Allow crt1 branch to go via a toc adjusting
16476 stub. Other calls that never return could do
16477 the same, if we could detect such. */
0a1b45a2 16478 can_plt_call = true;
6ab189d5 16479 }
ba8ca3e7
AM
16480 }
16481
16482 if (!can_plt_call)
16483 {
16484 /* g++ as of 20130507 emits self-calls without a
16485 following nop. This is arguably wrong since we
16486 have conflicting information. On the one hand a
16487 global symbol and on the other a local call
16488 sequence, but don't error for this special case.
16489 It isn't possible to cheaply verify we have
16490 exactly such a call. Allow all calls to the same
16491 section. */
16492 asection *code_sec = sec;
16493
16494 if (get_opd_info (sec) != NULL)
ad8e1ba5 16495 {
ba8ca3e7
AM
16496 bfd_vma off = (relocation + addend
16497 - sec->output_section->vma
16498 - sec->output_offset);
bc30df16 16499
0a1b45a2 16500 opd_entry_value (sec, off, &code_sec, NULL, false);
ad8e1ba5 16501 }
ba8ca3e7 16502 if (code_sec == input_section)
0a1b45a2 16503 can_plt_call = true;
ba8ca3e7
AM
16504 }
16505
16506 if (!can_plt_call)
16507 {
7aba54da 16508 if (stub_entry->type.main == ppc_stub_plt_call)
4805fc55 16509 info->callbacks->einfo
695344c0 16510 /* xgettext:c-format */
c1c8c1ef 16511 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16512 "(plt call stub)\n"),
4805fc55
AM
16513 input_bfd, input_section, rel->r_offset, sym_name);
16514 else
16515 info->callbacks->einfo
695344c0 16516 /* xgettext:c-format */
c1c8c1ef 16517 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16518 "(toc save/adjust stub)\n"),
4805fc55 16519 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
16520
16521 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16522 ret = false;
721956f4
AM
16523 }
16524
b25116a9 16525 if (can_plt_call
7aba54da 16526 && stub_entry->type.main == ppc_stub_plt_call)
0a1b45a2 16527 unresolved_reloc = false;
b25116a9
AM
16528 }
16529
6abec6bc 16530 if ((stub_entry == NULL
7aba54da
AM
16531 || stub_entry->type.main == ppc_stub_long_branch
16532 || stub_entry->type.main == ppc_stub_plt_branch)
8387904d
AM
16533 && get_opd_info (sec) != NULL)
16534 {
16535 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
16536 bfd_vma off = (relocation + addend
16537 - sec->output_section->vma
16538 - sec->output_offset);
0a1b45a2 16539 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
8387904d
AM
16540 if (dest != (bfd_vma) -1)
16541 {
16542 relocation = dest;
16543 addend = 0;
bc30df16 16544 reloc_dest = DEST_OPD;
8387904d
AM
16545 }
16546 }
16547
b25116a9
AM
16548 /* If the branch is out of reach we ought to have a long
16549 branch stub. */
16550 from = (rel->r_offset
16551 + input_section->output_offset
16552 + input_section->output_section->vma);
16553
6911b7dc
AM
16554 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16555 ? fdh->elf.other
16556 : sym->st_other);
16557
6abec6bc 16558 if (stub_entry != NULL
7aba54da
AM
16559 && (stub_entry->type.main == ppc_stub_long_branch
16560 || stub_entry->type.main == ppc_stub_plt_branch))
16561 {
16562 if (stub_entry->type.sub == ppc_stub_toc
16563 && !stub_entry->type.r2save
16564 && (r_type == R_PPC64_ADDR14_BRTAKEN
16565 || r_type == R_PPC64_ADDR14_BRNTAKEN
16566 || (relocation + addend - from + max_br_offset
16567 < 2 * max_br_offset)))
16568 /* Don't use the stub if this branch is in range. */
16569 stub_entry = NULL;
16570
16571 if (stub_entry != NULL
16572 && stub_entry->type.sub >= ppc_stub_notoc
16573 && ((r_type != R_PPC64_REL24_NOTOC
16574 && r_type != R_PPC64_REL24_P9NOTOC)
16575 || ((fdh ? fdh->elf.other : sym->st_other)
16576 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16577 && (relocation + addend - from + max_br_offset
16578 < 2 * max_br_offset))
16579 stub_entry = NULL;
16580
16581 if (stub_entry != NULL
16582 && stub_entry->type.r2save
16583 && (r_type == R_PPC64_REL24_NOTOC
16584 || r_type == R_PPC64_REL24_P9NOTOC)
16585 && (relocation + addend - from + max_br_offset
16586 < 2 * max_br_offset))
16587 stub_entry = NULL;
16588 }
05d0e962 16589
b25116a9
AM
16590 if (stub_entry != NULL)
16591 {
16592 /* Munge up the value and addend so that we call the stub
16593 rather than the procedure directly. */
a4b6fadd
AM
16594 asection *stub_sec = stub_entry->group->stub_sec;
16595
7aba54da 16596 if (stub_entry->type.main == ppc_stub_save_res)
a4b6fadd
AM
16597 relocation += (stub_sec->output_offset
16598 + stub_sec->output_section->vma
16599 + stub_sec->size - htab->sfpr->size
16600 - htab->sfpr->output_offset
16601 - htab->sfpr->output_section->vma);
16602 else
16603 relocation = (stub_entry->stub_offset
16604 + stub_sec->output_offset
16605 + stub_sec->output_section->vma);
b25116a9 16606 addend = 0;
bc30df16 16607 reloc_dest = DEST_STUB;
3b421ab3 16608
7aba54da
AM
16609 if (((stub_entry->type.r2save
16610 && (r_type == R_PPC64_REL24_NOTOC
16611 || r_type == R_PPC64_REL24_P9NOTOC))
16612 || ((stub_entry->type.main == ppc_stub_plt_call
16613 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16614 && rel + 1 < relend
16615 && rel[1].r_offset == rel->r_offset + 4
16616 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16617 && !(stub_entry->type.main == ppc_stub_plt_call
16618 && htab->params->tls_get_addr_opt
16619 && h != NULL
16620 && is_tls_get_addr (&h->elf, htab)))
29433886
AM
16621 {
16622 /* Skip over the r2 store at the start of the stub. */
7aba54da 16623 relocation += 4;
29433886 16624 }
5663e321 16625
7aba54da
AM
16626 if ((r_type == R_PPC64_REL24_NOTOC
16627 || r_type == R_PPC64_REL24_P9NOTOC)
16628 && stub_entry->type.main == ppc_stub_plt_call
16629 && stub_entry->type.sub >= ppc_stub_notoc)
5663e321 16630 htab->notoc_plt = 1;
b25116a9
AM
16631 }
16632
16633 if (insn != 0)
16634 {
794e51c0 16635 if (is_isa_v2)
721956f4 16636 {
b25116a9
AM
16637 /* Set 'a' bit. This is 0b00010 in BO field for branch
16638 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16639 for branch on CTR insns (BO == 1a00t or 1a01t). */
16640 if ((insn & (0x14 << 21)) == (0x04 << 21))
16641 insn |= 0x02 << 21;
16642 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16643 insn |= 0x08 << 21;
16644 else
16645 break;
16646 }
16647 else
16648 {
16649 /* Invert 'y' bit if not the default. */
4cc603a5 16650 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16651 insn ^= 0x01 << 21;
721956f4 16652 }
b25116a9 16653
95f0d0d2 16654 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16655 }
e86ce104 16656
06da1e8e
AM
16657 /* NOP out calls to undefined weak functions.
16658 We can thus call a weak function without first
16659 checking whether the function is defined. */
b25116a9 16660 else if (h != NULL
039b3fef 16661 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16662 && h->elf.dynindx == -1
05d0e962 16663 && (r_type == R_PPC64_REL24
7aba54da
AM
16664 || r_type == R_PPC64_REL24_NOTOC
16665 || r_type == R_PPC64_REL24_P9NOTOC)
b25116a9 16666 && relocation == 0
733ae98c
AM
16667 && addend == 0
16668 && offset_in_range (input_section, rel->r_offset, 4))
e86ce104 16669 {
95f0d0d2 16670 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16671 goto copy_reloc;
e86ce104 16672 }
65f38f15 16673 break;
066f4018
AM
16674
16675 case R_PPC64_GOT16_DS:
6738c8a7 16676 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16677 || (bfd_link_pic (info)
16678 && sec == bfd_abs_section_ptr)
6738c8a7 16679 || !htab->do_toc_opt)
06507dab 16680 break;
066f4018
AM
16681 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16682 if (relocation + addend - from + 0x8000 < 0x10000
f5b9c288
AM
16683 && sec != NULL
16684 && sec->output_section != NULL
16685 && !discarded_section (sec)
733ae98c
AM
16686 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16687 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16688 {
16689 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16690 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16691 {
16692 insn += (14u << 26) - (58u << 26);
16693 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16694 r_type = R_PPC64_TOC16;
16695 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16696 }
16697 }
16698 break;
16699
16700 case R_PPC64_GOT16_LO_DS:
16701 case R_PPC64_GOT16_HA:
6738c8a7 16702 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16703 || (bfd_link_pic (info)
16704 && sec == bfd_abs_section_ptr)
6738c8a7 16705 || !htab->do_toc_opt)
06507dab 16706 break;
066f4018
AM
16707 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16708 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
f5b9c288
AM
16709 && sec != NULL
16710 && sec->output_section != NULL
16711 && !discarded_section (sec)
733ae98c
AM
16712 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16713 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16714 {
16715 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16716 if (r_type == R_PPC64_GOT16_LO_DS
16717 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16718 {
16719 insn += (14u << 26) - (58u << 26);
16720 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16721 r_type = R_PPC64_TOC16_LO;
16722 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16723 }
fba8689a
AM
16724 else if (r_type == R_PPC64_GOT16_HA
16725 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16726 {
16727 r_type = R_PPC64_TOC16_HA;
16728 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16729 }
16730 }
16731 break;
4a421c53
AM
16732
16733 case R_PPC64_GOT_PCREL34:
6738c8a7 16734 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
3a3a4c1f
AM
16735 || (bfd_link_pic (info)
16736 && sec == bfd_abs_section_ptr)
6738c8a7 16737 || !htab->do_toc_opt)
06507dab 16738 break;
4a421c53
AM
16739 from = (rel->r_offset
16740 + input_section->output_section->vma
16741 + input_section->output_offset);
6738c8a7 16742 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
f5b9c288
AM
16743 && sec != NULL
16744 && sec->output_section != NULL
16745 && !discarded_section (sec)
733ae98c
AM
16746 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16747 && offset_in_range (input_section, rel->r_offset, 8)))
6738c8a7
AM
16748 break;
16749
16750 offset = rel->r_offset;
16751 pinsn = bfd_get_32 (input_bfd, contents + offset);
16752 pinsn <<= 32;
16753 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16754 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16755 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16756 break;
16757
16758 /* Replace with paddi. */
16759 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16760 r_type = R_PPC64_PCREL34;
16761 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16762 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16763 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16764 /* Fall through. */
4a421c53
AM
16765
16766 case R_PPC64_PCREL34:
6738c8a7
AM
16767 if (!htab->params->no_pcrel_opt
16768 && rel + 1 < relend
16769 && rel[1].r_offset == rel->r_offset
16770 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
733ae98c
AM
16771 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16772 && offset_in_range (input_section, rel->r_offset, 8))
4a421c53
AM
16773 {
16774 offset = rel->r_offset;
16775 pinsn = bfd_get_32 (input_bfd, contents + offset);
16776 pinsn <<= 32;
16777 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16778 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16779 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16780 | (14ULL << 26) /* paddi */))
16781 {
6738c8a7
AM
16782 bfd_vma off2 = rel[1].r_addend;
16783 if (off2 == 0)
16784 /* zero means next insn. */
16785 off2 = 8;
16786 off2 += offset;
733ae98c 16787 if (offset_in_range (input_section, off2, 4))
4a421c53 16788 {
6738c8a7
AM
16789 uint64_t pinsn2;
16790 bfd_signed_vma addend_off;
16791 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16792 pinsn2 <<= 32;
16793 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16794 {
733ae98c 16795 if (!offset_in_range (input_section, off2, 8))
6738c8a7
AM
16796 break;
16797 pinsn2 |= bfd_get_32 (input_bfd,
16798 contents + off2 + 4);
16799 }
16800 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16801 {
16802 addend += addend_off;
16803 rel->r_addend = addend;
16804 bfd_put_32 (input_bfd, pinsn >> 32,
16805 contents + offset);
16806 bfd_put_32 (input_bfd, pinsn,
16807 contents + offset + 4);
16808 bfd_put_32 (input_bfd, pinsn2 >> 32,
16809 contents + off2);
4a421c53 16810 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16811 bfd_put_32 (input_bfd, pinsn2,
16812 contents + off2 + 4);
4a421c53
AM
16813 }
16814 }
16815 }
16816 }
16817 break;
65f38f15 16818 }
5bd4f169 16819
411e1bfb 16820 tls_type = 0;
23cedd1d 16821 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16822 switch (r_type)
16823 {
16824 default:
cf97bcb0
AM
16825 /* xgettext:c-format */
16826 _bfd_error_handler (_("%pB: %s unsupported"),
16827 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16828
65f38f15 16829 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16830 ret = false;
c316a17c 16831 goto copy_reloc;
5bd4f169 16832
65f38f15 16833 case R_PPC64_NONE:
411e1bfb 16834 case R_PPC64_TLS:
727fc41e
AM
16835 case R_PPC64_TLSGD:
16836 case R_PPC64_TLSLD:
3b421ab3 16837 case R_PPC64_TOCSAVE:
04c9666a
AM
16838 case R_PPC64_GNU_VTINHERIT:
16839 case R_PPC64_GNU_VTENTRY:
006589cf 16840 case R_PPC64_ENTRY:
4a421c53 16841 case R_PPC64_PCREL_OPT:
c316a17c 16842 goto copy_reloc;
5bd4f169
AM
16843
16844 /* GOT16 relocations. Like an ADDR16 using the symbol's
16845 address in the GOT as relocation value instead of the
411e1bfb 16846 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16847 symbol and put the symbol value there. */
411e1bfb
AM
16848 case R_PPC64_GOT_TLSGD16:
16849 case R_PPC64_GOT_TLSGD16_LO:
16850 case R_PPC64_GOT_TLSGD16_HI:
16851 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16852 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16853 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16854 goto dogot;
16855
16856 case R_PPC64_GOT_TLSLD16:
16857 case R_PPC64_GOT_TLSLD16_LO:
16858 case R_PPC64_GOT_TLSLD16_HI:
16859 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16860 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16861 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16862 goto dogot;
16863
16864 case R_PPC64_GOT_TPREL16_DS:
16865 case R_PPC64_GOT_TPREL16_LO_DS:
16866 case R_PPC64_GOT_TPREL16_HI:
16867 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16868 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16869 tls_type = TLS_TLS | TLS_TPREL;
16870 goto dogot;
16871
16872 case R_PPC64_GOT_DTPREL16_DS:
16873 case R_PPC64_GOT_DTPREL16_LO_DS:
16874 case R_PPC64_GOT_DTPREL16_HI:
16875 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16876 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16877 tls_type = TLS_TLS | TLS_DTPREL;
16878 goto dogot;
16879
65f38f15
AM
16880 case R_PPC64_GOT16:
16881 case R_PPC64_GOT16_LO:
16882 case R_PPC64_GOT16_HI:
16883 case R_PPC64_GOT16_HA:
16884 case R_PPC64_GOT16_DS:
16885 case R_PPC64_GOT16_LO_DS:
5663e321 16886 case R_PPC64_GOT_PCREL34:
411e1bfb 16887 dogot:
5bd4f169
AM
16888 {
16889 /* Relocation is to the entry for this symbol in the global
16890 offset table. */
e717da7e 16891 asection *got;
d881513a 16892 bfd_vma *offp;
5bd4f169 16893 bfd_vma off;
d881513a 16894 unsigned long indx = 0;
927be08e 16895 struct got_entry *ent;
65f38f15 16896
d881513a 16897 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16898 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16899 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16900 else
5bd4f169 16901 {
d881513a
AM
16902 if (h != NULL)
16903 {
f0158f44
AM
16904 if (!htab->elf.dynamic_sections_created
16905 || h->elf.dynindx == -1
16906 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16907 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16908 /* This is actually a static link, or it is a
16909 -Bsymbolic link and the symbol is defined
16910 locally, or the symbol was forced to be local
16911 because of a version file. */
16912 ;
16913 else
16914 {
039b3fef 16915 indx = h->elf.dynindx;
0a1b45a2 16916 unresolved_reloc = false;
d881513a 16917 }
039b3fef 16918 ent = h->elf.got.glist;
d881513a 16919 }
411e1bfb 16920 else
5bd4f169 16921 {
d881513a
AM
16922 if (local_got_ents == NULL)
16923 abort ();
16924 ent = local_got_ents[r_symndx];
5bd4f169 16925 }
d881513a
AM
16926
16927 for (; ent != NULL; ent = ent->next)
133a1f60 16928 if (ent->addend == orig_rel.r_addend
e717da7e 16929 && ent->owner == input_bfd
d881513a
AM
16930 && ent->tls_type == tls_type)
16931 break;
5bd4f169 16932 }
411e1bfb 16933
927be08e
AM
16934 if (ent == NULL)
16935 abort ();
16936 if (ent->is_indirect)
16937 ent = ent->got.ent;
16938 offp = &ent->got.offset;
16939 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16940 if (got == NULL)
16941 abort ();
16942
411e1bfb
AM
16943 /* The offset must always be a multiple of 8. We use the
16944 least significant bit to record whether we have already
16945 processed this entry. */
d881513a 16946 off = *offp;
411e1bfb
AM
16947 if ((off & 1) != 0)
16948 off &= ~1;
5bd4f169
AM
16949 else
16950 {
411e1bfb
AM
16951 /* Generate relocs for the dynamic linker, except in
16952 the case of TLSLD where we'll use one entry per
16953 module. */
25f23106 16954 asection *relgot;
0a1b45a2 16955 bool ifunc;
e717da7e 16956
d881513a 16957 *offp = off | 1;
25f23106
AM
16958 relgot = NULL;
16959 ifunc = (h != NULL
16960 ? h->elf.type == STT_GNU_IFUNC
16961 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16962 if (ifunc)
82e66161
AM
16963 {
16964 relgot = htab->elf.irelplt;
efb2a7b4 16965 if (indx == 0 || is_static_defined (&h->elf))
0a1b45a2 16966 htab->elf.ifunc_resolvers = true;
82e66161 16967 }
f0158f44
AM
16968 else if (indx != 0
16969 || (bfd_link_pic (info)
16970 && (h == NULL
f749f26e
AM
16971 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16972 && !(tls_type != 0
f15d0b54 16973 && bfd_link_executable (info)
9e51d549
AM
16974 && (h == NULL
16975 || SYMBOL_REFERENCES_LOCAL (info,
3a3a4c1f
AM
16976 &h->elf)))
16977 && (h != NULL
16978 ? !bfd_is_abs_symbol (&h->elf.root)
16979 : sym->st_shndx != SHN_ABS)))
16980
19e08130 16981 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16982 if (relgot != NULL)
5bd4f169 16983 {
e717da7e
AM
16984 outrel.r_offset = (got->output_section->vma
16985 + got->output_offset
411e1bfb 16986 + off);
133a1f60 16987 outrel.r_addend = orig_rel.r_addend;
d881513a 16988 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16989 {
411e1bfb 16990 outrel.r_addend = 0;
e515b051 16991 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16992 if (tls_type == (TLS_TLS | TLS_GD))
16993 {
e717da7e
AM
16994 loc = relgot->contents;
16995 loc += (relgot->reloc_count++
d881513a
AM
16996 * sizeof (Elf64_External_Rela));
16997 bfd_elf64_swap_reloca_out (output_bfd,
16998 &outrel, loc);
e515b051 16999 outrel.r_offset += 8;
133a1f60 17000 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
17001 outrel.r_info
17002 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 17003 }
411e1bfb 17004 }
951fd09b 17005 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 17006 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 17007 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 17008 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
17009 else if (indx != 0)
17010 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
17011 else
81407a69 17012 {
25f23106
AM
17013 if (ifunc)
17014 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17015 else
17016 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
17017
17018 /* Write the .got section contents for the sake
17019 of prelink. */
e717da7e 17020 loc = got->contents + off;
23fbd6fa
JJ
17021 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
17022 loc);
81407a69 17023 }
81407a69
AM
17024
17025 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
17026 {
17027 outrel.r_addend += relocation;
17028 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
17029 {
17030 if (htab->elf.tls_sec == NULL)
17031 outrel.r_addend = 0;
17032 else
17033 outrel.r_addend -= htab->elf.tls_sec->vma;
17034 }
e515b051 17035 }
1657026c
AM
17036 if (!(info->enable_dt_relr
17037 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
17038 {
17039 loc = relgot->contents;
17040 loc += (relgot->reloc_count++
17041 * sizeof (Elf64_External_Rela));
17042 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17043 }
411e1bfb
AM
17044 }
17045
ad8e1ba5 17046 /* Init the .got section contents here if we're not
81407a69 17047 emitting a reloc. */
d881513a 17048 else
411e1bfb 17049 {
133a1f60 17050 relocation += orig_rel.r_addend;
f0158f44 17051 if (tls_type != 0)
411e1bfb 17052 {
989f9879
AM
17053 if (htab->elf.tls_sec == NULL)
17054 relocation = 0;
17055 else
17056 {
f0158f44
AM
17057 if (tls_type & TLS_LD)
17058 relocation = 0;
17059 else
17060 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 17061 if (tls_type & TLS_TPREL)
989f9879
AM
17062 relocation += DTP_OFFSET - TP_OFFSET;
17063 }
5bd4f169 17064
f0158f44 17065 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
17066 {
17067 bfd_put_64 (output_bfd, relocation,
e717da7e 17068 got->contents + off + 8);
676ee2b5 17069 relocation = 1;
7b609f53 17070 }
411e1bfb
AM
17071 }
17072 bfd_put_64 (output_bfd, relocation,
e717da7e 17073 got->contents + off);
5bd4f169
AM
17074 }
17075 }
17076
65f38f15
AM
17077 if (off >= (bfd_vma) -2)
17078 abort ();
17079
bf102f86 17080 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 17081 addend = 0;
c213164a 17082 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
17083 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17084 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17085 || r_type == R_PPC64_GOT_TPREL_PCREL34
17086 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 17087 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 17088 }
65f38f15
AM
17089 break;
17090
17091 case R_PPC64_PLT16_HA:
17092 case R_PPC64_PLT16_HI:
17093 case R_PPC64_PLT16_LO:
08be3224 17094 case R_PPC64_PLT16_LO_DS:
5663e321
AM
17095 case R_PPC64_PLT_PCREL34:
17096 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
17097 case R_PPC64_PLT32:
17098 case R_PPC64_PLT64:
23cedd1d 17099 case R_PPC64_PLTSEQ:
5663e321 17100 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 17101 case R_PPC64_PLTCALL:
5663e321 17102 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
17103 /* Relocation is to the entry for this symbol in the
17104 procedure linkage table. */
0a1b45a2 17105 unresolved_reloc = true;
cbf95972
AM
17106 {
17107 struct plt_entry **plt_list = NULL;
17108 if (h != NULL)
17109 plt_list = &h->elf.plt.plist;
17110 else if (local_got_ents != NULL)
17111 {
17112 struct plt_entry **local_plt = (struct plt_entry **)
17113 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 17114 plt_list = local_plt + r_symndx;
cbf95972
AM
17115 }
17116 if (plt_list)
17117 {
17118 struct plt_entry *ent;
65f38f15 17119
cbf95972
AM
17120 for (ent = *plt_list; ent != NULL; ent = ent->next)
17121 if (ent->plt.offset != (bfd_vma) -1
133a1f60 17122 && ent->addend == orig_rel.r_addend)
cbf95972
AM
17123 {
17124 asection *plt;
08be3224 17125 bfd_vma got;
cbf95972
AM
17126
17127 plt = htab->elf.splt;
30845f11 17128 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
17129 {
17130 if (h != NULL
17131 ? h->elf.type == STT_GNU_IFUNC
17132 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17133 plt = htab->elf.iplt;
17134 else
17135 plt = htab->pltlocal;
17136 }
17137 relocation = (plt->output_section->vma
17138 + plt->output_offset
17139 + ent->plt.offset);
08be3224 17140 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
17141 || r_type == R_PPC64_PLT16_HI
17142 || r_type == R_PPC64_PLT16_LO
17143 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
17144 {
17145 got = (elf_gp (output_bfd)
17146 + htab->sec_info[input_section->id].toc_off);
17147 relocation -= got;
17148 }
133a1f60 17149 addend = 0;
0a1b45a2 17150 unresolved_reloc = false;
cbf95972
AM
17151 break;
17152 }
17153 }
17154 }
65f38f15 17155 break;
5bd4f169 17156
0b13192e
AM
17157 case R_PPC64_TOC:
17158 /* Relocation value is TOC base. */
17159 relocation = TOCstart;
cf35638d 17160 if (r_symndx == STN_UNDEF)
6f20ed8a 17161 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
17162 else if (unresolved_reloc)
17163 ;
6f20ed8a
AM
17164 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17165 relocation += htab->sec_info[sec->id].toc_off;
0b13192e 17166 else
0a1b45a2 17167 unresolved_reloc = true;
10040916
AM
17168 if (unresolved_reloc
17169 || (!is_opd
17170 && h != NULL
17171 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17172 info->callbacks->einfo
17173 /* xgettext:c-format */
17174 (_("%H: %s against %pT is not supported\n"),
17175 input_bfd, input_section, rel->r_offset,
17176 ppc64_elf_howto_table[r_type]->name, sym_name);
ab96bf03 17177 goto dodyn;
0b13192e 17178
5bd4f169
AM
17179 /* TOC16 relocs. We want the offset relative to the TOC base,
17180 which is the address of the start of the TOC plus 0x8000.
17181 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17182 in this order. */
65f38f15
AM
17183 case R_PPC64_TOC16:
17184 case R_PPC64_TOC16_LO:
17185 case R_PPC64_TOC16_HI:
17186 case R_PPC64_TOC16_DS:
17187 case R_PPC64_TOC16_LO_DS:
17188 case R_PPC64_TOC16_HA:
6f20ed8a 17189 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
17190 if (h != NULL)
17191 goto dodyn;
5bd4f169
AM
17192 break;
17193
17194 /* Relocate against the beginning of the section. */
65f38f15
AM
17195 case R_PPC64_SECTOFF:
17196 case R_PPC64_SECTOFF_LO:
17197 case R_PPC64_SECTOFF_HI:
17198 case R_PPC64_SECTOFF_DS:
17199 case R_PPC64_SECTOFF_LO_DS:
17200 case R_PPC64_SECTOFF_HA:
4ce794b7 17201 if (sec != NULL)
65f38f15 17202 addend -= sec->output_section->vma;
5bd4f169
AM
17203 break;
17204
25f23106
AM
17205 case R_PPC64_REL16:
17206 case R_PPC64_REL16_LO:
17207 case R_PPC64_REL16_HI:
17208 case R_PPC64_REL16_HA:
4a969973
AM
17209 case R_PPC64_REL16_HIGH:
17210 case R_PPC64_REL16_HIGHA:
17211 case R_PPC64_REL16_HIGHER:
17212 case R_PPC64_REL16_HIGHERA:
17213 case R_PPC64_REL16_HIGHEST:
17214 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
17215 case R_PPC64_REL16_HIGHER34:
17216 case R_PPC64_REL16_HIGHERA34:
17217 case R_PPC64_REL16_HIGHEST34:
17218 case R_PPC64_REL16_HIGHESTA34:
a680de9a 17219 case R_PPC64_REL16DX_HA:
721956f4
AM
17220 case R_PPC64_REL14:
17221 case R_PPC64_REL14_BRNTAKEN:
17222 case R_PPC64_REL14_BRTAKEN:
5d1634d7 17223 case R_PPC64_REL24:
05d0e962 17224 case R_PPC64_REL24_NOTOC:
7aba54da 17225 case R_PPC64_REL24_P9NOTOC:
5663e321
AM
17226 case R_PPC64_PCREL34:
17227 case R_PPC64_PCREL28:
5d1634d7
AM
17228 break;
17229
411e1bfb
AM
17230 case R_PPC64_TPREL16:
17231 case R_PPC64_TPREL16_LO:
17232 case R_PPC64_TPREL16_HI:
17233 case R_PPC64_TPREL16_HA:
17234 case R_PPC64_TPREL16_DS:
17235 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
17236 case R_PPC64_TPREL16_HIGH:
17237 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
17238 case R_PPC64_TPREL16_HIGHER:
17239 case R_PPC64_TPREL16_HIGHERA:
17240 case R_PPC64_TPREL16_HIGHEST:
17241 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
17242 if (h != NULL
17243 && h->elf.root.type == bfd_link_hash_undefweak
733ae98c
AM
17244 && h->elf.dynindx == -1
17245 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
766bc656
AM
17246 {
17247 /* Make this relocation against an undefined weak symbol
17248 resolve to zero. This is really just a tweak, since
17249 code using weak externs ought to check that they are
17250 defined before using them. */
17251 bfd_byte *p = contents + rel->r_offset - d_offset;
17252
95f0d0d2 17253 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
17254 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17255 if (insn != 0)
95f0d0d2 17256 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
17257 break;
17258 }
beaddc1a
AM
17259 /* Fall through. */
17260
17261 case R_PPC64_TPREL34:
989f9879
AM
17262 if (htab->elf.tls_sec != NULL)
17263 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
17264 /* The TPREL16 relocs shouldn't really be used in shared
17265 libs or with non-local symbols as that will result in
17266 DT_TEXTREL being set, but support them anyway. */
17267 goto dodyn;
411e1bfb
AM
17268
17269 case R_PPC64_DTPREL16:
17270 case R_PPC64_DTPREL16_LO:
17271 case R_PPC64_DTPREL16_HI:
17272 case R_PPC64_DTPREL16_HA:
17273 case R_PPC64_DTPREL16_DS:
17274 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
17275 case R_PPC64_DTPREL16_HIGH:
17276 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
17277 case R_PPC64_DTPREL16_HIGHER:
17278 case R_PPC64_DTPREL16_HIGHERA:
17279 case R_PPC64_DTPREL16_HIGHEST:
17280 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 17281 case R_PPC64_DTPREL34:
989f9879
AM
17282 if (htab->elf.tls_sec != NULL)
17283 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
17284 break;
17285
45965137
AM
17286 case R_PPC64_ADDR64_LOCAL:
17287 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17288 ? h->elf.other
17289 : sym->st_other);
17290 break;
17291
e515b051
AM
17292 case R_PPC64_DTPMOD64:
17293 relocation = 1;
17294 addend = 0;
17295 goto dodyn;
17296
411e1bfb 17297 case R_PPC64_TPREL64:
989f9879
AM
17298 if (htab->elf.tls_sec != NULL)
17299 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
17300 goto dodyn;
17301
17302 case R_PPC64_DTPREL64:
989f9879
AM
17303 if (htab->elf.tls_sec != NULL)
17304 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 17305 /* Fall through. */
411e1bfb 17306
65f38f15
AM
17307 /* Relocations that may need to be propagated if this is a
17308 dynamic object. */
04c9666a 17309 case R_PPC64_REL30:
65f38f15
AM
17310 case R_PPC64_REL32:
17311 case R_PPC64_REL64:
17312 case R_PPC64_ADDR14:
17313 case R_PPC64_ADDR14_BRNTAKEN:
17314 case R_PPC64_ADDR14_BRTAKEN:
17315 case R_PPC64_ADDR16:
17316 case R_PPC64_ADDR16_DS:
17317 case R_PPC64_ADDR16_HA:
17318 case R_PPC64_ADDR16_HI:
f9c6b907
AM
17319 case R_PPC64_ADDR16_HIGH:
17320 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17321 case R_PPC64_ADDR16_HIGHER:
17322 case R_PPC64_ADDR16_HIGHERA:
17323 case R_PPC64_ADDR16_HIGHEST:
17324 case R_PPC64_ADDR16_HIGHESTA:
17325 case R_PPC64_ADDR16_LO:
17326 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
17327 case R_PPC64_ADDR16_HIGHER34:
17328 case R_PPC64_ADDR16_HIGHERA34:
17329 case R_PPC64_ADDR16_HIGHEST34:
17330 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 17331 case R_PPC64_ADDR24:
65f38f15
AM
17332 case R_PPC64_ADDR32:
17333 case R_PPC64_ADDR64:
17334 case R_PPC64_UADDR16:
17335 case R_PPC64_UADDR32:
17336 case R_PPC64_UADDR64:
5663e321
AM
17337 case R_PPC64_D34:
17338 case R_PPC64_D34_LO:
17339 case R_PPC64_D34_HI30:
17340 case R_PPC64_D34_HA30:
17341 case R_PPC64_D28:
411e1bfb 17342 dodyn:
5d1634d7 17343 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
17344 break;
17345
41bd81ab
AM
17346 if (NO_OPD_RELOCS && is_opd)
17347 break;
17348
8a9e8e72 17349 if (bfd_link_pic (info)
b1b07054 17350 ? ((h == NULL
190eb1dd 17351 || h->elf.dyn_relocs != NULL)
b1b07054
AM
17352 && ((h != NULL && pc_dynrelocs (h))
17353 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 17354 : (h != NULL
190eb1dd 17355 ? h->elf.dyn_relocs != NULL
d311bc8b 17356 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 17357 {
0a1b45a2 17358 bool skip, relocate;
65f38f15 17359 asection *sreloc;
1cf1f670 17360 bfd_vma out_off;
82e66161 17361 long indx = 0;
65f38f15
AM
17362
17363 /* When generating a dynamic object, these relocations
17364 are copied into the output file to be resolved at run
17365 time. */
17366
0a1b45a2
AM
17367 skip = false;
17368 relocate = false;
65f38f15 17369
1cf1f670
AM
17370 out_off = _bfd_elf_section_offset (output_bfd, info,
17371 input_section, rel->r_offset);
17372 if (out_off == (bfd_vma) -1)
0a1b45a2 17373 skip = true;
1cf1f670 17374 else if (out_off == (bfd_vma) -2)
0a1b45a2 17375 skip = true, relocate = true;
1cf1f670
AM
17376 out_off += (input_section->output_section->vma
17377 + input_section->output_offset);
17378 outrel.r_offset = out_off;
411e1bfb 17379 outrel.r_addend = rel->r_addend;
65f38f15 17380
1cf1f670
AM
17381 /* Optimize unaligned reloc use. */
17382 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17383 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17384 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17385 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17386 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17387 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17388 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17389 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17390 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17391
65f38f15 17392 if (skip)
0bb2d96a 17393 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
17394 else if (h != NULL
17395 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
17396 && !is_opd
17397 && r_type != R_PPC64_TOC)
14acf4dc 17398 {
82e66161
AM
17399 indx = h->elf.dynindx;
17400 BFD_ASSERT (indx != -1);
17401 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 17402 }
65f38f15
AM
17403 else
17404 {
41bd81ab
AM
17405 /* This symbol is local, or marked to become local,
17406 or this is an opd section reloc which must point
17407 at a local function. */
65f38f15 17408 outrel.r_addend += relocation;
e86ce104 17409 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 17410 {
3fad3c7c 17411 if (is_opd && h != NULL)
afbe61cf
AM
17412 {
17413 /* Lie about opd entries. This case occurs
17414 when building shared libraries and we
17415 reference a function in another shared
3fad3c7c
AM
17416 lib. The same thing happens for a weak
17417 definition in an application that's
17418 overridden by a strong definition in a
17419 shared lib. (I believe this is a generic
17420 bug in binutils handling of weak syms.)
17421 In these cases we won't use the opd
1e2f5b6e 17422 entry in this lib. */
0a1b45a2 17423 unresolved_reloc = false;
afbe61cf 17424 }
25f23106
AM
17425 if (!is_opd
17426 && r_type == R_PPC64_ADDR64
17427 && (h != NULL
17428 ? h->elf.type == STT_GNU_IFUNC
17429 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17430 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17431 else
17432 {
17433 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 17434
25f23106
AM
17435 /* We need to relocate .opd contents for ld.so.
17436 Prelink also wants simple and consistent rules
17437 for relocs. This make all RELATIVE relocs have
17438 *r_offset equal to r_addend. */
0a1b45a2 17439 relocate = true;
25f23106 17440 }
65f38f15
AM
17441 }
17442 else
17443 {
25f23106
AM
17444 if (h != NULL
17445 ? h->elf.type == STT_GNU_IFUNC
17446 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17447 {
25f53a85 17448 info->callbacks->einfo
695344c0 17449 /* xgettext:c-format */
174d0a74 17450 (_("%H: %s for indirect "
c1c8c1ef 17451 "function `%pT' unsupported\n"),
25f53a85 17452 input_bfd, input_section, rel->r_offset,
25f23106
AM
17453 ppc64_elf_howto_table[r_type]->name,
17454 sym_name);
0a1b45a2 17455 ret = false;
25f23106 17456 }
cf35638d 17457 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
17458 ;
17459 else if (sec == NULL || sec->owner == NULL)
17460 {
17461 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17462 return false;
65f38f15
AM
17463 }
17464 else
17465 {
f26a3287 17466 asection *osec = sec->output_section;
65f38f15 17467
f26a3287
AM
17468 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17469 {
17470 /* TLS symbol values are relative to the
17471 TLS segment. Dynamic relocations for
17472 local TLS symbols therefore can't be
17473 reduced to a relocation against their
17474 section symbol because it holds the
17475 address of the section, not a value
17476 relative to the TLS segment. We could
17477 change the .tdata dynamic section symbol
17478 to be zero value but STN_UNDEF works
17479 and is used elsewhere, eg. for TPREL64
17480 GOT relocs against local TLS symbols. */
17481 osec = htab->elf.tls_sec;
17482 indx = 0;
17483 }
17484 else
74541ad4 17485 {
74541ad4 17486 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
17487 if (indx == 0)
17488 {
17489 if ((osec->flags & SEC_READONLY) == 0
17490 && htab->elf.data_index_section != NULL)
17491 osec = htab->elf.data_index_section;
17492 else
17493 osec = htab->elf.text_index_section;
17494 indx = elf_section_data (osec)->dynindx;
17495 }
17496 BFD_ASSERT (indx != 0);
74541ad4 17497 }
74541ad4 17498
65f38f15
AM
17499 /* We are turning this relocation into one
17500 against a section symbol, so subtract out
17501 the output section's address but not the
17502 offset of the input section in the output
17503 section. */
17504 outrel.r_addend -= osec->vma;
17505 }
17506
17507 outrel.r_info = ELF64_R_INFO (indx, r_type);
17508 }
17509 }
17510
1657026c
AM
17511 if (!(info->enable_dt_relr
17512 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17513 && rel->r_offset % 2 == 0
17514 && input_section->alignment_power != 0
17515 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
82e66161 17516 {
1657026c
AM
17517 sreloc = elf_section_data (input_section)->sreloc;
17518 if (h != NULL
17519 ? h->elf.type == STT_GNU_IFUNC
17520 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17521 {
17522 sreloc = htab->elf.irelplt;
17523 if (indx == 0 || is_static_defined (&h->elf))
17524 htab->elf.ifunc_resolvers = true;
17525 }
17526 if (sreloc == NULL)
17527 abort ();
17528
17529 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17530 >= sreloc->size)
17531 abort ();
17532 loc = sreloc->contents;
17533 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17534 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
82e66161 17535 }
65f38f15 17536
e59a1001
AM
17537 if (!warned_dynamic
17538 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17539 {
17540 info->callbacks->einfo
17541 /* xgettext:c-format */
17542 (_("%X%P: %pB: %s against %pT "
17543 "is not supported by glibc as a dynamic relocation\n"),
17544 input_bfd,
17545 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17546 sym_name);
0a1b45a2 17547 warned_dynamic = true;
e59a1001
AM
17548 }
17549
65f38f15
AM
17550 /* If this reloc is against an external symbol, it will
17551 be computed at runtime, so there's no need to do
81407a69
AM
17552 anything now. However, for the sake of prelink ensure
17553 that the section contents are a known value. */
2cdcc330 17554 if (!relocate)
81407a69 17555 {
0a1b45a2 17556 unresolved_reloc = false;
81407a69
AM
17557 /* The value chosen here is quite arbitrary as ld.so
17558 ignores section contents except for the special
17559 case of .opd where the contents might be accessed
17560 before relocation. Choose zero, as that won't
17561 cause reloc overflow. */
17562 relocation = 0;
17563 addend = 0;
17564 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17565 to improve backward compatibility with older
17566 versions of ld. */
17567 if (r_type == R_PPC64_ADDR64)
17568 addend = outrel.r_addend;
17569 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 17570 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 17571 addend = outrel.r_offset;
81407a69 17572 }
65f38f15 17573 }
5bd4f169
AM
17574 break;
17575
65f38f15
AM
17576 case R_PPC64_COPY:
17577 case R_PPC64_GLOB_DAT:
17578 case R_PPC64_JMP_SLOT:
25f23106 17579 case R_PPC64_JMP_IREL:
65f38f15
AM
17580 case R_PPC64_RELATIVE:
17581 /* We shouldn't ever see these dynamic relocs in relocatable
17582 files. */
ae9a127f 17583 /* Fall through. */
65f38f15
AM
17584
17585 case R_PPC64_PLTGOT16:
17586 case R_PPC64_PLTGOT16_DS:
17587 case R_PPC64_PLTGOT16_HA:
17588 case R_PPC64_PLTGOT16_HI:
17589 case R_PPC64_PLTGOT16_LO:
17590 case R_PPC64_PLTGOT16_LO_DS:
17591 case R_PPC64_PLTREL32:
17592 case R_PPC64_PLTREL64:
17593 /* These ones haven't been implemented yet. */
17594
25f53a85 17595 info->callbacks->einfo
695344c0 17596 /* xgettext:c-format */
c1c8c1ef 17597 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 17598 input_bfd,
4ce794b7 17599 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
17600
17601 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 17602 ret = false;
c316a17c 17603 goto copy_reloc;
65f38f15 17604 }
5bd4f169 17605
67f0cbdb
AM
17606 /* Multi-instruction sequences that access the TOC can be
17607 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 17608 to nop; addi rb,r2,x; */
67f0cbdb
AM
17609 switch (r_type)
17610 {
17611 default:
17612 break;
17613
17614 case R_PPC64_GOT_TLSLD16_HI:
17615 case R_PPC64_GOT_TLSGD16_HI:
17616 case R_PPC64_GOT_TPREL16_HI:
17617 case R_PPC64_GOT_DTPREL16_HI:
17618 case R_PPC64_GOT16_HI:
17619 case R_PPC64_TOC16_HI:
17620 /* These relocs would only be useful if building up an
17621 offset to later add to r2, perhaps in an indexed
17622 addressing mode instruction. Don't try to optimize.
17623 Unfortunately, the possibility of someone building up an
17624 offset like this or even with the HA relocs, means that
17625 we need to check the high insn when optimizing the low
17626 insn. */
17627 break;
17628
5663e321
AM
17629 case R_PPC64_PLTCALL_NOTOC:
17630 if (!unresolved_reloc)
17631 htab->notoc_plt = 1;
17632 /* Fall through. */
23cedd1d 17633 case R_PPC64_PLTCALL:
733ae98c
AM
17634 if (unresolved_reloc
17635 && offset_in_range (input_section, rel->r_offset,
17636 r_type == R_PPC64_PLTCALL ? 8 : 4))
23cedd1d
AM
17637 {
17638 /* No plt entry. Make this into a direct call. */
17639 bfd_byte *p = contents + rel->r_offset;
17640 insn = bfd_get_32 (input_bfd, p);
17641 insn &= 1;
17642 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
17643 if (r_type == R_PPC64_PLTCALL)
17644 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
17645 unresolved_reloc = save_unresolved_reloc;
17646 r_type = R_PPC64_REL24;
17647 }
17648 break;
17649
5663e321 17650 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17651 case R_PPC64_PLTSEQ:
17652 if (unresolved_reloc)
17653 {
0a1b45a2 17654 unresolved_reloc = false;
23cedd1d
AM
17655 goto nop_it;
17656 }
17657 break;
17658
5663e321
AM
17659 case R_PPC64_PLT_PCREL34_NOTOC:
17660 if (!unresolved_reloc)
17661 htab->notoc_plt = 1;
17662 /* Fall through. */
17663 case R_PPC64_PLT_PCREL34:
733ae98c
AM
17664 if (unresolved_reloc
17665 && offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17666 {
17667 bfd_byte *p = contents + rel->r_offset;
17668 bfd_put_32 (input_bfd, PNOP >> 32, p);
17669 bfd_put_32 (input_bfd, PNOP, p + 4);
0a1b45a2 17670 unresolved_reloc = false;
5663e321
AM
17671 goto copy_reloc;
17672 }
17673 break;
17674
23cedd1d
AM
17675 case R_PPC64_PLT16_HA:
17676 if (unresolved_reloc)
17677 {
0a1b45a2 17678 unresolved_reloc = false;
23cedd1d
AM
17679 goto nop_it;
17680 }
17681 /* Fall through. */
67f0cbdb
AM
17682 case R_PPC64_GOT_TLSLD16_HA:
17683 case R_PPC64_GOT_TLSGD16_HA:
17684 case R_PPC64_GOT_TPREL16_HA:
17685 case R_PPC64_GOT_DTPREL16_HA:
17686 case R_PPC64_GOT16_HA:
17687 case R_PPC64_TOC16_HA:
98528052 17688 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
3a3a4c1f
AM
17689 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17690 && !(bfd_link_pic (info)
17691 && (h != NULL
17692 ? bfd_is_abs_symbol (&h->elf.root)
17693 : sec == bfd_abs_section_ptr)))
98528052 17694 {
23cedd1d
AM
17695 bfd_byte *p;
17696 nop_it:
733ae98c
AM
17697 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17698 {
17699 p = contents + (rel->r_offset & ~3);
17700 bfd_put_32 (input_bfd, NOP, p);
17701 goto copy_reloc;
17702 }
98528052 17703 }
67f0cbdb
AM
17704 break;
17705
23cedd1d
AM
17706 case R_PPC64_PLT16_LO:
17707 case R_PPC64_PLT16_LO_DS:
17708 if (unresolved_reloc)
17709 {
0a1b45a2 17710 unresolved_reloc = false;
23cedd1d
AM
17711 goto nop_it;
17712 }
17713 /* Fall through. */
67f0cbdb
AM
17714 case R_PPC64_GOT_TLSLD16_LO:
17715 case R_PPC64_GOT_TLSGD16_LO:
17716 case R_PPC64_GOT_TPREL16_LO_DS:
17717 case R_PPC64_GOT_DTPREL16_LO_DS:
17718 case R_PPC64_GOT16_LO:
17719 case R_PPC64_GOT16_LO_DS:
17720 case R_PPC64_TOC16_LO:
17721 case R_PPC64_TOC16_LO_DS:
98528052 17722 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
733ae98c 17723 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
3a3a4c1f
AM
17724 && !(bfd_link_pic (info)
17725 && (h != NULL
17726 ? bfd_is_abs_symbol (&h->elf.root)
17727 : sec == bfd_abs_section_ptr))
733ae98c 17728 && offset_in_range (input_section, rel->r_offset & ~3, 4))
67f0cbdb
AM
17729 {
17730 bfd_byte *p = contents + (rel->r_offset & ~3);
17731 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17732 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17733 {
17734 /* Transform addic to addi when we change reg. */
2365f8d7 17735 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17736 insn |= (14u << 26) | (2 << 16);
17737 }
17738 else
67f0cbdb 17739 {
98528052
AM
17740 insn &= ~(0x1f << 16);
17741 insn |= 2 << 16;
67f0cbdb 17742 }
560c8763 17743 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17744 }
17745 break;
9a23f96e
AM
17746
17747 case R_PPC64_TPREL16_HA:
733ae98c
AM
17748 if (htab->do_tls_opt
17749 && relocation + addend + 0x8000 < 0x10000
17750 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17751 {
17752 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17753 bfd_put_32 (input_bfd, NOP, p);
17754 goto copy_reloc;
9a23f96e
AM
17755 }
17756 break;
17757
17758 case R_PPC64_TPREL16_LO:
17759 case R_PPC64_TPREL16_LO_DS:
733ae98c
AM
17760 if (htab->do_tls_opt
17761 && relocation + addend + 0x8000 < 0x10000
17762 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17763 {
17764 bfd_byte *p = contents + (rel->r_offset & ~3);
17765 insn = bfd_get_32 (input_bfd, p);
17766 insn &= ~(0x1f << 16);
17767 insn |= 13 << 16;
17768 bfd_put_32 (input_bfd, insn, p);
17769 }
17770 break;
67f0cbdb
AM
17771 }
17772
65f38f15
AM
17773 /* Do any further special processing. */
17774 switch (r_type)
17775 {
17776 default:
17777 break;
17778
25f23106 17779 case R_PPC64_REL16_HA:
4a969973
AM
17780 case R_PPC64_REL16_HIGHA:
17781 case R_PPC64_REL16_HIGHERA:
17782 case R_PPC64_REL16_HIGHESTA:
a680de9a 17783 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17784 case R_PPC64_ADDR16_HA:
17785 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17786 case R_PPC64_ADDR16_HIGHERA:
17787 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17788 case R_PPC64_TOC16_HA:
17789 case R_PPC64_SECTOFF_HA:
411e1bfb 17790 case R_PPC64_TPREL16_HA:
f9c6b907 17791 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17792 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17793 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17794 case R_PPC64_DTPREL16_HA:
17795 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17796 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17797 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17798 /* It's just possible that this symbol is a weak symbol
17799 that's not actually defined anywhere. In that case,
17800 'sec' would be NULL, and we should leave the symbol
17801 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17802 if (sec == NULL)
17803 break;
1a0670f3 17804 /* Fall through. */
5c5f6e17
AM
17805
17806 case R_PPC64_GOT16_HA:
17807 case R_PPC64_PLTGOT16_HA:
17808 case R_PPC64_PLT16_HA:
17809 case R_PPC64_GOT_TLSGD16_HA:
17810 case R_PPC64_GOT_TLSLD16_HA:
17811 case R_PPC64_GOT_TPREL16_HA:
17812 case R_PPC64_GOT_DTPREL16_HA:
17813 /* Add 0x10000 if sign bit in 0:15 is set.
17814 Bits 0:15 are not used. */
17815 addend += 0x8000;
65f38f15
AM
17816 break;
17817
5663e321
AM
17818 case R_PPC64_D34_HA30:
17819 case R_PPC64_ADDR16_HIGHERA34:
17820 case R_PPC64_ADDR16_HIGHESTA34:
17821 case R_PPC64_REL16_HIGHERA34:
17822 case R_PPC64_REL16_HIGHESTA34:
17823 if (sec != NULL)
17824 addend += 1ULL << 33;
17825 break;
17826
65f38f15
AM
17827 case R_PPC64_ADDR16_DS:
17828 case R_PPC64_ADDR16_LO_DS:
17829 case R_PPC64_GOT16_DS:
17830 case R_PPC64_GOT16_LO_DS:
17831 case R_PPC64_PLT16_LO_DS:
17832 case R_PPC64_SECTOFF_DS:
17833 case R_PPC64_SECTOFF_LO_DS:
17834 case R_PPC64_TOC16_DS:
17835 case R_PPC64_TOC16_LO_DS:
17836 case R_PPC64_PLTGOT16_DS:
17837 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17838 case R_PPC64_GOT_TPREL16_DS:
17839 case R_PPC64_GOT_TPREL16_LO_DS:
17840 case R_PPC64_GOT_DTPREL16_DS:
17841 case R_PPC64_GOT_DTPREL16_LO_DS:
17842 case R_PPC64_TPREL16_DS:
17843 case R_PPC64_TPREL16_LO_DS:
17844 case R_PPC64_DTPREL16_DS:
17845 case R_PPC64_DTPREL16_LO_DS:
733ae98c
AM
17846 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17847 break;
adadcc0c
AM
17848 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17849 mask = 3;
a680de9a
PB
17850 /* If this reloc is against an lq, lxv, or stxv insn, then
17851 the value must be a multiple of 16. This is somewhat of
17852 a hack, but the "correct" way to do this by defining _DQ
17853 forms of all the _DS relocs bloats all reloc switches in
17854 this file. It doesn't make much sense to use these
17855 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17856 if ((insn & (0x3fu << 26)) == (56u << 26)
17857 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17858 mask = 15;
a680de9a
PB
17859 relocation += addend;
17860 addend = insn & (mask ^ 3);
17861 if ((relocation & mask) != 0)
65f38f15 17862 {
a680de9a 17863 relocation ^= relocation & mask;
25f53a85 17864 info->callbacks->einfo
695344c0 17865 /* xgettext:c-format */
174d0a74 17866 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17867 input_bfd, input_section, rel->r_offset,
d830549d 17868 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17869 mask + 1);
65f38f15 17870 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17871 ret = false;
c316a17c 17872 goto copy_reloc;
65f38f15
AM
17873 }
17874 break;
5bd4f169
AM
17875 }
17876
239e1f3a
AM
17877 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17878 because such sections are not SEC_ALLOC and thus ld.so will
17879 not process them. */
d830549d 17880 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17881 if (unresolved_reloc
239e1f3a 17882 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17883 && h->elf.def_dynamic)
17884 && _bfd_elf_section_offset (output_bfd, info, input_section,
17885 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17886 {
25f53a85 17887 info->callbacks->einfo
695344c0 17888 /* xgettext:c-format */
c1c8c1ef 17889 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17890 input_bfd, input_section, rel->r_offset,
b80eed39 17891 howto->name,
039b3fef 17892 h->elf.root.root.string);
0a1b45a2 17893 ret = false;
9c07fe7c 17894 }
5bd4f169 17895
b80eed39
AM
17896 /* 16-bit fields in insns mostly have signed values, but a
17897 few insns have 16-bit unsigned values. Really, we should
17898 have different reloc types. */
17899 if (howto->complain_on_overflow != complain_overflow_dont
17900 && howto->dst_mask == 0xffff
733ae98c
AM
17901 && (input_section->flags & SEC_CODE) != 0
17902 && offset_in_range (input_section, rel->r_offset & ~3, 4))
b80eed39
AM
17903 {
17904 enum complain_overflow complain = complain_overflow_signed;
17905
17906 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17907 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17908 complain = complain_overflow_bitfield;
17909 else if (howto->rightshift == 0
2365f8d7
AM
17910 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17911 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17912 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17913 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17914 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17915 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17916 complain = complain_overflow_unsigned;
17917 if (howto->complain_on_overflow != complain)
17918 {
17919 alt_howto = *howto;
17920 alt_howto.complain_on_overflow = complain;
17921 howto = &alt_howto;
17922 }
17923 }
17924
5663e321 17925 switch (r_type)
a680de9a 17926 {
5663e321
AM
17927 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17928 case R_PPC64_D34:
17929 case R_PPC64_D34_LO:
17930 case R_PPC64_D34_HI30:
17931 case R_PPC64_D34_HA30:
17932 case R_PPC64_PCREL34:
17933 case R_PPC64_GOT_PCREL34:
c213164a
AM
17934 case R_PPC64_TPREL34:
17935 case R_PPC64_DTPREL34:
87c69f97
AM
17936 case R_PPC64_GOT_TLSGD_PCREL34:
17937 case R_PPC64_GOT_TLSLD_PCREL34:
17938 case R_PPC64_GOT_TPREL_PCREL34:
17939 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17940 case R_PPC64_PLT_PCREL34:
17941 case R_PPC64_PLT_PCREL34_NOTOC:
17942 case R_PPC64_D28:
17943 case R_PPC64_PCREL28:
733ae98c 17944 if (!offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17945 r = bfd_reloc_outofrange;
17946 else
17947 {
5663e321
AM
17948 relocation += addend;
17949 if (howto->pc_relative)
17950 relocation -= (rel->r_offset
17951 + input_section->output_offset
17952 + input_section->output_section->vma);
17953 relocation >>= howto->rightshift;
17954
17955 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17956 pinsn <<= 32;
17957 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17958
17959 pinsn &= ~howto->dst_mask;
17960 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17961 & howto->dst_mask);
17962 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17963 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17964 r = bfd_reloc_ok;
17965 if (howto->complain_on_overflow == complain_overflow_signed
17966 && (relocation + (1ULL << (howto->bitsize - 1))
17967 >= 1ULL << howto->bitsize))
17968 r = bfd_reloc_overflow;
17969 }
17970 break;
17971
17972 case R_PPC64_REL16DX_HA:
733ae98c 17973 if (!offset_in_range (input_section, rel->r_offset, 4))
a680de9a
PB
17974 r = bfd_reloc_outofrange;
17975 else
17976 {
17977 relocation += addend;
17978 relocation -= (rel->r_offset
17979 + input_section->output_offset
17980 + input_section->output_section->vma);
3de43e7b 17981 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17982 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17983 insn &= ~0x1fffc1;
3de43e7b 17984 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17985 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17986 r = bfd_reloc_ok;
3de43e7b 17987 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17988 r = bfd_reloc_overflow;
17989 }
5663e321
AM
17990 break;
17991
17992 default:
17993 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17994 contents, rel->r_offset,
17995 relocation, addend);
a680de9a 17996 }
5bd4f169 17997
ef60b7ff 17998 if (r != bfd_reloc_ok)
5bd4f169 17999 {
bc30df16 18000 char *more_info = NULL;
b80eed39 18001 const char *reloc_name = howto->name;
bc30df16
AM
18002
18003 if (reloc_dest != DEST_NORMAL)
18004 {
18005 more_info = bfd_malloc (strlen (reloc_name) + 8);
18006 if (more_info != NULL)
18007 {
18008 strcpy (more_info, reloc_name);
18009 strcat (more_info, (reloc_dest == DEST_OPD
18010 ? " (OPD)" : " (stub)"));
18011 reloc_name = more_info;
18012 }
18013 }
18014
cd27b276 18015 if (r == bfd_reloc_overflow)
5bd4f169 18016 {
8131c122
AM
18017 /* On code like "if (foo) foo();" don't report overflow
18018 on a branch to zero when foo is undefined. */
18019 if (!warned
18020 && (reloc_dest == DEST_STUB
18021 || !(h != NULL
18022 && (h->elf.root.type == bfd_link_hash_undefweak
18023 || h->elf.root.type == bfd_link_hash_undefined)
18024 && is_branch_reloc (r_type))))
9e51d549
AM
18025 info->callbacks->reloc_overflow
18026 (info, (struct bfd_link_hash_entry *) h, sym_name,
18027 reloc_name, orig_rel.r_addend, input_bfd, input_section,
18028 rel->r_offset);
ef60b7ff
AM
18029 }
18030 else
18031 {
25f53a85 18032 info->callbacks->einfo
695344c0 18033 /* xgettext:c-format */
c1c8c1ef 18034 (_("%H: %s against `%pT': error %d\n"),
25f53a85 18035 input_bfd, input_section, rel->r_offset,
bc30df16 18036 reloc_name, sym_name, (int) r);
0a1b45a2 18037 ret = false;
ef60b7ff 18038 }
c9594989 18039 free (more_info);
5bd4f169 18040 }
c316a17c
AM
18041 copy_reloc:
18042 if (wrel != rel)
18043 *wrel = *rel;
18044 }
18045
18046 if (wrel != rel)
18047 {
18048 Elf_Internal_Shdr *rel_hdr;
18049 size_t deleted = rel - wrel;
18050
18051 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
18052 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18053 if (rel_hdr->sh_size == 0)
18054 {
18055 /* It is too late to remove an empty reloc section. Leave
18056 one NONE reloc.
18057 ??? What is wrong with an empty section??? */
18058 rel_hdr->sh_size = rel_hdr->sh_entsize;
18059 deleted -= 1;
18060 }
18061 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18062 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18063 input_section->reloc_count -= deleted;
5bd4f169
AM
18064 }
18065
645ea6a9
AM
18066 /* If we're emitting relocations, then shortly after this function
18067 returns, reloc offsets and addends for this section will be
18068 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
18069 file rather than the input. Save a copy of the relocs for
18070 opd_entry_value. */
6313825c
AM
18071 if (is_opd
18072 && (info->emitrelocations || bfd_link_relocatable (info))
18073 && input_section->reloc_count != 0)
8860955f
AM
18074 {
18075 bfd_size_type amt;
18076 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18077 rel = bfd_alloc (input_bfd, amt);
6313825c 18078 ppc64_elf_section_data (input_section)->u.opd.u.relocs = rel;
8860955f 18079 if (rel == NULL)
0a1b45a2 18080 return false;
8860955f
AM
18081 memcpy (rel, relocs, amt);
18082 }
5bd4f169
AM
18083 return ret;
18084}
18085
754021d0
AM
18086/* Adjust the value of any local symbols in opd sections. */
18087
6e0b88f1 18088static int
754021d0
AM
18089ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18090 const char *name ATTRIBUTE_UNUSED,
18091 Elf_Internal_Sym *elfsym,
18092 asection *input_sec,
18093 struct elf_link_hash_entry *h)
18094{
74f0fb50
AM
18095 struct _opd_sec_data *opd;
18096 long adjust;
754021d0
AM
18097 bfd_vma value;
18098
4025353c 18099 if (h != NULL)
6e0b88f1 18100 return 1;
4025353c 18101
74f0fb50
AM
18102 opd = get_opd_info (input_sec);
18103 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 18104 return 1;
754021d0
AM
18105
18106 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 18107 if (!bfd_link_relocatable (info))
754021d0
AM
18108 value -= input_sec->output_section->vma;
18109
51aecdc5 18110 adjust = opd->adjust[OPD_NDX (value)];
4025353c 18111 if (adjust == -1)
6e0b88f1
AM
18112 return 2;
18113
18114 elfsym->st_value += adjust;
18115 return 1;
754021d0
AM
18116}
18117
5bd4f169
AM
18118/* Finish up dynamic symbol handling. We set the contents of various
18119 dynamic sections here. */
18120
0a1b45a2 18121static bool
4ce794b7
AM
18122ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18123 struct bfd_link_info *info,
18124 struct elf_link_hash_entry *h,
4aef7643 18125 Elf_Internal_Sym *sym)
5bd4f169 18126{
65f38f15 18127 struct ppc_link_hash_table *htab;
8387904d 18128 struct plt_entry *ent;
5bd4f169 18129
65f38f15 18130 htab = ppc_hash_table (info);
4dfe6ac6 18131 if (htab == NULL)
0a1b45a2 18132 return false;
5bd4f169 18133
49c09209
AM
18134 if (!htab->opd_abi && !h->def_regular)
18135 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18136 if (ent->plt.offset != (bfd_vma) -1)
18137 {
18138 /* Mark the symbol as undefined, rather than as
18139 defined in glink. Leave the value if there were
18140 any relocations where pointer equality matters
18141 (this is a clue for the dynamic linker, to make
18142 function pointer comparisons work between an
18143 application and shared library), otherwise set it
18144 to zero. */
18145 sym->st_shndx = SHN_UNDEF;
18146 if (!h->pointer_equality_needed)
18147 sym->st_value = 0;
18148 else if (!h->ref_regular_nonweak)
18149 {
18150 /* This breaks function pointer comparisons, but
18151 that is better than breaking tests for a NULL
18152 function pointer. */
18153 sym->st_value = 0;
18154 }
18155 break;
18156 }
5bd4f169 18157
1bdd8fac
AM
18158 if (h->needs_copy
18159 && (h->root.type == bfd_link_hash_defined
18160 || h->root.type == bfd_link_hash_defweak)
18161 && (h->root.u.def.section == htab->elf.sdynbss
18162 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 18163 {
65f38f15 18164 /* This symbol needs a copy reloc. Set it up. */
49c09209 18165 Elf_Internal_Rela rela;
5474d94f 18166 asection *srel;
49c09209 18167 bfd_byte *loc;
5bd4f169 18168
1bdd8fac 18169 if (h->dynindx == -1)
65f38f15 18170 abort ();
5bd4f169 18171
ed7007c1 18172 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
18173 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18174 rela.r_addend = 0;
afbf7e8e 18175 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
18176 srel = htab->elf.sreldynrelro;
18177 else
18178 srel = htab->elf.srelbss;
18179 loc = srel->contents;
18180 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 18181 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
18182 }
18183
0a1b45a2 18184 return true;
5bd4f169
AM
18185}
18186
65f38f15
AM
18187/* Used to decide how to sort relocs in an optimal manner for the
18188 dynamic linker, before writing them out. */
18189
18190static enum elf_reloc_type_class
7e612e98
AM
18191ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18192 const asection *rel_sec,
18193 const Elf_Internal_Rela *rela)
65f38f15 18194{
04c9666a 18195 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
18196 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18197
33e44f2e 18198 if (rel_sec == htab->elf.irelplt)
7e612e98 18199 return reloc_class_ifunc;
a33d1f77 18200
4ce794b7 18201 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 18202 switch (r_type)
65f38f15
AM
18203 {
18204 case R_PPC64_RELATIVE:
18205 return reloc_class_relative;
18206 case R_PPC64_JMP_SLOT:
18207 return reloc_class_plt;
18208 case R_PPC64_COPY:
18209 return reloc_class_copy;
18210 default:
18211 return reloc_class_normal;
18212 }
18213}
18214
5bd4f169
AM
18215/* Finish up the dynamic sections. */
18216
0a1b45a2 18217static bool
4ce794b7
AM
18218ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18219 struct bfd_link_info *info)
5bd4f169 18220{
65f38f15
AM
18221 struct ppc_link_hash_table *htab;
18222 bfd *dynobj;
5bd4f169 18223 asection *sdyn;
5bd4f169 18224
65f38f15 18225 htab = ppc_hash_table (info);
4dfe6ac6 18226 if (htab == NULL)
0a1b45a2 18227 return false;
4dfe6ac6 18228
65f38f15 18229 dynobj = htab->elf.dynobj;
3d4d4302 18230 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 18231
65f38f15 18232 if (htab->elf.dynamic_sections_created)
5bd4f169 18233 {
5bd4f169
AM
18234 Elf64_External_Dyn *dyncon, *dynconend;
18235
33e44f2e 18236 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 18237 abort ();
5bd4f169
AM
18238
18239 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 18240 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
18241 for (; dyncon < dynconend; dyncon++)
18242 {
18243 Elf_Internal_Dyn dyn;
19397422 18244 asection *s;
5bd4f169
AM
18245
18246 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18247
18248 switch (dyn.d_tag)
18249 {
65f38f15
AM
18250 default:
18251 continue;
5bd4f169 18252
5d1634d7 18253 case DT_PPC64_GLINK:
4ce794b7 18254 s = htab->glink;
6348e046 18255 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
18256 /* We stupidly defined DT_PPC64_GLINK to be the start
18257 of glink rather than the first entry point, which is
18258 what ld.so needs, and now have a bigger stub to
18259 support automatic multiple TOCs. */
9e390558 18260 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
18261 break;
18262
19397422
AM
18263 case DT_PPC64_OPD:
18264 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18265 if (s == NULL)
18266 continue;
18267 dyn.d_un.d_ptr = s->vma;
19397422
AM
18268 break;
18269
e8910a83 18270 case DT_PPC64_OPT:
5663e321
AM
18271 if ((htab->do_multi_toc && htab->multi_toc_needed)
18272 || htab->notoc_plt)
e8910a83 18273 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
18274 if (htab->has_plt_localentry0)
18275 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
18276 break;
18277
19397422
AM
18278 case DT_PPC64_OPDSZ:
18279 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18280 if (s == NULL)
18281 continue;
eea6121a 18282 dyn.d_un.d_val = s->size;
19397422
AM
18283 break;
18284
65f38f15 18285 case DT_PLTGOT:
33e44f2e 18286 s = htab->elf.splt;
6348e046 18287 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
18288 break;
18289
18290 case DT_JMPREL:
33e44f2e 18291 s = htab->elf.srelplt;
6348e046 18292 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 18293 break;
5bd4f169 18294
65f38f15 18295 case DT_PLTRELSZ:
33e44f2e 18296 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 18297 break;
82e66161
AM
18298
18299 case DT_TEXTREL:
cebd6b8a 18300 if (htab->elf.ifunc_resolvers)
82e66161
AM
18301 info->callbacks->einfo
18302 (_("%P: warning: text relocations and GNU indirect "
18303 "functions may result in a segfault at runtime\n"));
18304 continue;
5bd4f169 18305 }
5bd4f169 18306
65f38f15 18307 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 18308 }
5bd4f169
AM
18309 }
18310
6528b6eb
AM
18311 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18312 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18313 {
18314 /* Fill in the first entry in the global offset table.
18315 We use it to hold the link-time TOCbase. */
18316 bfd_put_64 (output_bfd,
60ee0d4a 18317 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 18318 htab->elf.sgot->contents);
5d1634d7
AM
18319
18320 /* Set .got entry size. */
2cdcc330
AM
18321 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18322 = 8;
5d1634d7
AM
18323 }
18324
6528b6eb
AM
18325 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18326 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18327 {
18328 /* Set .plt entry size. */
33e44f2e 18329 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 18330 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
18331 }
18332
84f5d08e
AM
18333 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18334 brlt ourselves if emitrelocations. */
18335 if (htab->brlt != NULL
18336 && htab->brlt->reloc_count != 0
18337 && !_bfd_elf_link_output_relocs (output_bfd,
18338 htab->brlt,
d4730f92 18339 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
18340 elf_section_data (htab->brlt)->relocs,
18341 NULL))
0a1b45a2 18342 return false;
84f5d08e 18343
176a0d42
AM
18344 if (htab->glink != NULL
18345 && htab->glink->reloc_count != 0
18346 && !_bfd_elf_link_output_relocs (output_bfd,
18347 htab->glink,
d4730f92 18348 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
18349 elf_section_data (htab->glink)->relocs,
18350 NULL))
0a1b45a2 18351 return false;
176a0d42 18352
da44f4e5 18353
df136d64
AM
18354 if (htab->glink_eh_frame != NULL
18355 && htab->glink_eh_frame->size != 0
18356 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18357 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18358 htab->glink_eh_frame,
18359 htab->glink_eh_frame->contents))
0a1b45a2 18360 return false;
58d180e8 18361
e717da7e 18362 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
18363 since we didn't add them to DYNOBJ. We know dynobj is the first
18364 bfd. */
c72f2fb2 18365 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
18366 {
18367 asection *s;
7b53ace3 18368
0c8d6e5c 18369 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
18370 continue;
18371
e717da7e
AM
18372 s = ppc64_elf_tdata (dynobj)->got;
18373 if (s != NULL
eea6121a 18374 && s->size != 0
e717da7e
AM
18375 && s->output_section != bfd_abs_section_ptr
18376 && !bfd_set_section_contents (output_bfd, s->output_section,
18377 s->contents, s->output_offset,
eea6121a 18378 s->size))
0a1b45a2 18379 return false;
e717da7e
AM
18380 s = ppc64_elf_tdata (dynobj)->relgot;
18381 if (s != NULL
eea6121a 18382 && s->size != 0
e717da7e
AM
18383 && s->output_section != bfd_abs_section_ptr
18384 && !bfd_set_section_contents (output_bfd, s->output_section,
18385 s->contents, s->output_offset,
eea6121a 18386 s->size))
0a1b45a2 18387 return false;
e717da7e 18388 }
f6c52c13 18389
0a1b45a2 18390 return true;
5bd4f169
AM
18391}
18392
6313825c
AM
18393static bool
18394ppc64_elf_free_cached_info (bfd *abfd)
18395{
18396 if (abfd->sections)
18397 for (asection *opd = bfd_get_section_by_name (abfd, ".opd");
18398 opd != NULL;
18399 opd = bfd_get_next_section_by_name (NULL, opd))
18400 if (opd->reloc_count == 0)
18401 free (ppc64_elf_section_data (opd)->u.opd.u.contents);
18402
09d757a5 18403 return _bfd_elf_free_cached_info (abfd);
6313825c
AM
18404}
18405
5bd4f169 18406#include "elf64-target.h"
7b8e7dad
AM
18407
18408/* FreeBSD support */
18409
18410#undef TARGET_LITTLE_SYM
6afcdeb3 18411#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 18412#undef TARGET_LITTLE_NAME
6afcdeb3 18413#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
18414
18415#undef TARGET_BIG_SYM
6d00b590 18416#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
18417#undef TARGET_BIG_NAME
18418#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18419
18420#undef ELF_OSABI
18421#define ELF_OSABI ELFOSABI_FREEBSD
18422
18423#undef elf64_bed
18424#define elf64_bed elf64_powerpc_fbsd_bed
18425
18426#include "elf64-target.h"