]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
Update PowerPC64 symtocbase test
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
a2c58332 2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
5bd4f169
AM
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 5 Largely rewritten by Alan Modra.
5bd4f169 6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
5bd4f169 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
5bd4f169 18
4ce794b7
AM
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 22
cd123cb7 23
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 27
c09c8b42
AM
28/* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31#define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
4bd7c902 32
3db64b00 33#include "sysdep.h"
183e98be 34#include <stdarg.h>
5bd4f169 35#include "bfd.h"
5bd4f169
AM
36#include "bfdlink.h"
37#include "libbfd.h"
38#include "elf-bfd.h"
04c9666a 39#include "elf/ppc64.h"
5d1634d7 40#include "elf64-ppc.h"
58d180e8 41#include "dwarf2.h"
5bd4f169 42
bb294208
AM
43/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44#define OCTETS_PER_BYTE(ABFD, SEC) 1
45
805fc799 46static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
48static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 58static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 60static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
62static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 64static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 66static bfd_vma opd_entry_value
0a1b45a2 67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
5bd4f169 68
6d00b590 69#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 70#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 71#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
72#define TARGET_BIG_NAME "elf64-powerpc"
73#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 74#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
75#define ELF_MACHINE_CODE EM_PPC64
76#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
77#define ELF_COMMONPAGESIZE 0x1000
78#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
79#define elf_info_to_howto ppc64_elf_info_to_howto
80
81#define elf_backend_want_got_sym 0
82#define elf_backend_want_plt_sym 0
83#define elf_backend_plt_alignment 3
84#define elf_backend_plt_not_loaded 1
ad8e1ba5 85#define elf_backend_got_header_size 8
5474d94f 86#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
87#define elf_backend_can_gc_sections 1
88#define elf_backend_can_refcount 1
89#define elf_backend_rela_normal 1
64f52338 90#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 91#define elf_backend_default_execstack 0
ad8e1ba5 92
e717da7e 93#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 94#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 95#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
96#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
97#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
98#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
99#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 100#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 101#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 102#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
103
104#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
105#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
106#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 107#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 108#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 109#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 110#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 111#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 112#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 113#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 114#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 115#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 116#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 117#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 118#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
119#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
120#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 121#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
c9405344 122#define elf_backend_always_size_sections ppc64_elf_edit
ad8e1ba5 123#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 124#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 125#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 126#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
127#define elf_backend_relocate_section ppc64_elf_relocate_section
128#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
129#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
130#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 131#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 132#define elf_backend_special_sections ppc64_elf_special_sections
bf577467 133#define elf_backend_section_flags ppc64_elf_section_flags
6911b7dc 134#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 135#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 136#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 137
5bd4f169
AM
138/* The name of the dynamic interpreter. This is put in the .interp
139 section. */
140#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
141
142/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 143#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 144#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
145
146/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 147#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 148
a078d95a
AM
149/* Offsets to some stack save slots. */
150#define STK_LR 16
151#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 152/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
153 CR save slot. Used only by optimised __tls_get_addr call stub,
154 relying on __tls_get_addr_opt not saving CR.. */
155#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
156
5bd4f169 157/* TOC base pointers offset from start of TOC. */
411e1bfb 158#define TOC_BASE_OFF 0x8000
a27e685f
AM
159/* TOC base alignment. */
160#define TOC_BASE_ALIGN 256
411e1bfb
AM
161
162/* Offset of tp and dtp pointers from start of TLS block. */
163#define TP_OFFSET 0x7000
164#define DTP_OFFSET 0x8000
5bd4f169 165
ad8e1ba5
AM
166/* .plt call stub instructions. The normal stub is like this, but
167 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 168 insert an addi to adjust r11. */
a078d95a 169#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
170#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
171#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
172#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
173#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
174#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
175#define BCTR 0x4e800420 /* bctr */
176
07d6d2b8 177#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
178#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
179#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
180#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
181#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
182
183#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
184#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
185#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
186#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
187#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
188#define BNECTR 0x4ca20420 /* bnectr+ */
189#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 190
71a39c98 191#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 192#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 193#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 194
07d6d2b8
AM
195#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
196#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
197#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 198
04bdff6a 199#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 200#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 201#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 202#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
203#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
204#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 205#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 206#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 207#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 208#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 209#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 210#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 211#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
212#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
213#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
214#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
215#define PADDI_R12_PC 0x0610000039800000ULL
216#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 217#define PNOP 0x0700000000000000ULL
a345bc8d 218
3cd7c7d7
AM
219/* __glink_PLTresolve stub instructions. We enter with the index in
220 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
9e390558 221#define GLINK_PLTRESOLVE_SIZE(htab) \
3cd7c7d7 222 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
ee4bf8d2
AM
223 /* 0: */
224 /* .quad plt0-1f */
225 /* __glink: */
226#define MFLR_R12 0x7d8802a6 /* mflr %12 */
227#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
228 /* 1: */
229#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 230 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 231#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
232#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
233 /* ld %12,0(%11) */
234 /* ld %2,8(%11) */
235 /* mtctr %12 */
236 /* ld %11,16(%11) */
ee4bf8d2 237 /* bctr */
3cd7c7d7
AM
238
239#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
240#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
241#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
242#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
243#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
244#define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
245#define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
5d1634d7
AM
246
247/* Pad with this. */
248#define NOP 0x60000000
249
721956f4
AM
250/* Some other nops. */
251#define CROR_151515 0x4def7b82
252#define CROR_313131 0x4ffffb82
253
cedb70c5 254/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
255#define LI_R0_0 0x38000000 /* li %r0,0 */
256#define B_DOT 0x48000000 /* b . */
257
258/* After that, we need two instructions to load the index, followed by
259 a branch. */
260#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 261#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 262
deb0e272
AM
263/* Instructions used by the save and restore reg functions. */
264#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
265#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
266#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
267#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
268#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
269#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
270#define LI_R12_0 0x39800000 /* li %r12,0 */
271#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
272#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
273#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
274#define BLR 0x4e800020 /* blr */
275
41bd81ab
AM
276/* Since .opd is an array of descriptors and each entry will end up
277 with identical R_PPC64_RELATIVE relocs, there is really no need to
278 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 279 relocate .opd without reloc entries. */
41bd81ab
AM
280#ifndef NO_OPD_RELOCS
281#define NO_OPD_RELOCS 0
282#endif
810d4e75 283
a4b6fadd
AM
284#ifndef ARRAY_SIZE
285#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
286#endif
287
810d4e75
AM
288static inline int
289abiversion (bfd *abfd)
290{
291 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
292}
293
294static inline void
295set_abiversion (bfd *abfd, int ver)
296{
297 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
298 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
299}
5bd4f169
AM
300\f
301/* Relocation HOWTO's. */
46807bf4
AM
302/* Like other ELF RELA targets that don't apply multiple
303 field-altering relocations to the same localation, src_mask is
304 always zero and pcrel_offset is the same as pc_relative.
305 PowerPC can always use a zero bitpos, even when the field is not at
306 the LSB. For example, a REL24 could use rightshift=2, bisize=24
307 and bitpos=2 which matches the ABI description, or as we do here,
308 rightshift=0, bitsize=26 and bitpos=0. */
309#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
310 complain, special_func) \
311 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
312 complain_overflow_ ## complain, special_func, \
0a1b45a2 313 #type, false, 0, mask, pc_relative)
46807bf4 314
04c9666a 315static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 316
f3185997
NC
317static reloc_howto_type ppc64_elf_howto_raw[] =
318{
5bd4f169 319 /* This reloc does nothing. */
0a1b45a2 320 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
46807bf4 321 bfd_elf_generic_reloc),
5bd4f169
AM
322
323 /* A standard 32 bit relocation. */
0a1b45a2 324 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 325 bfd_elf_generic_reloc),
5bd4f169
AM
326
327 /* An absolute 26 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
0a1b45a2 329 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
46807bf4 330 bfd_elf_generic_reloc),
5bd4f169
AM
331
332 /* A standard 16 bit relocation. */
0a1b45a2 333 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 334 bfd_elf_generic_reloc),
5bd4f169
AM
335
336 /* A 16 bit relocation without overflow. */
0a1b45a2 337 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 338 bfd_elf_generic_reloc),
5bd4f169
AM
339
340 /* Bits 16-31 of an address. */
0a1b45a2 341 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 342 bfd_elf_generic_reloc),
5bd4f169
AM
343
344 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
345 bits, treated as a signed number, is negative. */
0a1b45a2 346 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 347 ppc64_elf_ha_reloc),
5bd4f169
AM
348
349 /* An absolute 16 bit branch; the lower two bits must be zero.
350 FIXME: we don't check that, we just clear them. */
0a1b45a2 351 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 352 ppc64_elf_branch_reloc),
5bd4f169
AM
353
354 /* An absolute 16 bit branch, for which bit 10 should be set to
355 indicate that the branch is expected to be taken. The lower two
356 bits must be zero. */
0a1b45a2 357 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 358 ppc64_elf_brtaken_reloc),
5bd4f169
AM
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
0a1b45a2 363 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 364 ppc64_elf_brtaken_reloc),
5bd4f169
AM
365
366 /* A relative 26 bit branch; the lower two bits must be zero. */
0a1b45a2 367 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 368 ppc64_elf_branch_reloc),
5bd4f169 369
05d0e962 370 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
0a1b45a2 371 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 372 ppc64_elf_branch_reloc),
05d0e962 373
7aba54da
AM
374 /* Another variant, when p10 insns can't be used on stubs. */
375 HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
376 ppc64_elf_branch_reloc),
377
5bd4f169 378 /* A relative 16 bit branch; the lower two bits must be zero. */
0a1b45a2 379 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 380 ppc64_elf_branch_reloc),
5bd4f169
AM
381
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is expected to be taken. The lower two bits must be
384 zero. */
0a1b45a2 385 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 386 ppc64_elf_brtaken_reloc),
5bd4f169
AM
387
388 /* A relative 16 bit branch. Bit 10 should be set to indicate that
389 the branch is not expected to be taken. The lower two bits must
390 be zero. */
0a1b45a2 391 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 392 ppc64_elf_brtaken_reloc),
5bd4f169
AM
393
394 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
395 symbol. */
0a1b45a2 396 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
46807bf4 397 ppc64_elf_unhandled_reloc),
5bd4f169
AM
398
399 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
400 the symbol. */
0a1b45a2 401 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 402 ppc64_elf_unhandled_reloc),
5bd4f169
AM
403
404 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
405 the symbol. */
0a1b45a2 406 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 407 ppc64_elf_unhandled_reloc),
5bd4f169
AM
408
409 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
410 the symbol. */
0a1b45a2 411 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 412 ppc64_elf_unhandled_reloc),
5bd4f169
AM
413
414 /* This is used only by the dynamic linker. The symbol should exist
415 both in the object being run and in some shared library. The
416 dynamic linker copies the data addressed by the symbol from the
417 shared library into the object, because the object being
418 run has to have the data at some particular address. */
0a1b45a2 419 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
46807bf4 420 ppc64_elf_unhandled_reloc),
5bd4f169
AM
421
422 /* Like R_PPC64_ADDR64, but used when setting global offset table
423 entries. */
0a1b45a2 424 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 425 ppc64_elf_unhandled_reloc),
5bd4f169
AM
426
427 /* Created by the link editor. Marks a procedure linkage table
428 entry for a symbol. */
0a1b45a2 429 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
46807bf4 430 ppc64_elf_unhandled_reloc),
5bd4f169
AM
431
432 /* Used only by the dynamic linker. When the object is run, this
433 doubleword64 is set to the load address of the object, plus the
434 addend. */
0a1b45a2 435 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 436 bfd_elf_generic_reloc),
5bd4f169
AM
437
438 /* Like R_PPC64_ADDR32, but may be unaligned. */
0a1b45a2 439 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 440 bfd_elf_generic_reloc),
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16, but may be unaligned. */
0a1b45a2 443 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 444 bfd_elf_generic_reloc),
5bd4f169
AM
445
446 /* 32-bit PC relative. */
0a1b45a2 447 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 448 bfd_elf_generic_reloc),
5bd4f169 449
10ed1bba 450 /* 32-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 451 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 452 ppc64_elf_unhandled_reloc),
5bd4f169
AM
453
454 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
455 FIXME: R_PPC64_PLTREL32 not supported. */
0a1b45a2 456 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 457 ppc64_elf_unhandled_reloc),
5bd4f169
AM
458
459 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
460 the symbol. */
0a1b45a2 461 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 462 ppc64_elf_unhandled_reloc),
5bd4f169
AM
463
464 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
465 the symbol. */
0a1b45a2 466 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 467 ppc64_elf_unhandled_reloc),
5bd4f169
AM
468
469 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
470 the symbol. */
0a1b45a2 471 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 472 ppc64_elf_unhandled_reloc),
5bd4f169 473
c061c2d8 474 /* 16-bit section relative relocation. */
0a1b45a2 475 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
46807bf4 476 ppc64_elf_sectoff_reloc),
5bd4f169 477
c061c2d8 478 /* Like R_PPC64_SECTOFF, but no overflow warning. */
0a1b45a2 479 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 480 ppc64_elf_sectoff_reloc),
5bd4f169
AM
481
482 /* 16-bit upper half section relative relocation. */
0a1b45a2 483 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 484 ppc64_elf_sectoff_reloc),
5bd4f169
AM
485
486 /* 16-bit upper half adjusted section relative relocation. */
0a1b45a2 487 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 488 ppc64_elf_sectoff_ha_reloc),
5bd4f169 489
04c9666a 490 /* Like R_PPC64_REL24 without touching the two least significant bits. */
0a1b45a2 491 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
46807bf4 492 bfd_elf_generic_reloc),
5bd4f169
AM
493
494 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
495
496 /* A standard 64-bit relocation. */
0a1b45a2 497 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 498 bfd_elf_generic_reloc),
5bd4f169
AM
499
500 /* The bits 32-47 of an address. */
0a1b45a2 501 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 502 bfd_elf_generic_reloc),
5bd4f169
AM
503
504 /* The bits 32-47 of an address, plus 1 if the contents of the low
505 16 bits, treated as a signed number, is negative. */
0a1b45a2 506 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 507 ppc64_elf_ha_reloc),
5bd4f169
AM
508
509 /* The bits 48-63 of an address. */
0a1b45a2 510 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 511 bfd_elf_generic_reloc),
5bd4f169
AM
512
513 /* The bits 48-63 of an address, plus 1 if the contents of the low
514 16 bits, treated as a signed number, is negative. */
0a1b45a2 515 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 516 ppc64_elf_ha_reloc),
5bd4f169
AM
517
518 /* Like ADDR64, but may be unaligned. */
0a1b45a2 519 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 520 bfd_elf_generic_reloc),
5bd4f169
AM
521
522 /* 64-bit relative relocation. */
0a1b45a2 523 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 524 bfd_elf_generic_reloc),
5bd4f169 525
cedb70c5 526 /* 64-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 527 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 528 ppc64_elf_unhandled_reloc),
5bd4f169
AM
529
530 /* 64-bit PC relative relocation to the symbol's procedure linkage
531 table. */
532 /* FIXME: R_PPC64_PLTREL64 not supported. */
0a1b45a2 533 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 534 ppc64_elf_unhandled_reloc),
5bd4f169
AM
535
536 /* 16 bit TOC-relative relocation. */
5bd4f169 537 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
0a1b45a2 538 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
46807bf4 539 ppc64_elf_toc_reloc),
5bd4f169
AM
540
541 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 542 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
0a1b45a2 543 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 544 ppc64_elf_toc_reloc),
5bd4f169
AM
545
546 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 547 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
0a1b45a2 548 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 549 ppc64_elf_toc_reloc),
5bd4f169
AM
550
551 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
552 contents of the low 16 bits, treated as a signed number, is
553 negative. */
5bd4f169 554 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
0a1b45a2 555 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 556 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
557
558 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 559 /* R_PPC64_TOC 51 doubleword64 .TOC. */
0a1b45a2 560 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 561 ppc64_elf_toc64_reloc),
5bd4f169
AM
562
563 /* Like R_PPC64_GOT16, but also informs the link editor that the
564 value to relocate may (!) refer to a PLT entry which the link
565 editor (a) may replace with the symbol value. If the link editor
566 is unable to fully resolve the symbol, it may (b) create a PLT
567 entry and store the address to the new PLT entry in the GOT.
568 This permits lazy resolution of function symbols at run time.
569 The link editor may also skip all of this and just (c) emit a
570 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
571 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
0a1b45a2 572 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
46807bf4 573 ppc64_elf_unhandled_reloc),
411e1bfb
AM
574
575 /* Like R_PPC64_PLTGOT16, but without overflow. */
576 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 577 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 578 ppc64_elf_unhandled_reloc),
411e1bfb
AM
579
580 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
581 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
0a1b45a2 582 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 583 ppc64_elf_unhandled_reloc),
411e1bfb
AM
584
585 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
586 1 if the contents of the low 16 bits, treated as a signed number,
587 is negative. */
588 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
0a1b45a2 589 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 590 ppc64_elf_unhandled_reloc),
411e1bfb
AM
591
592 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
0a1b45a2 593 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 594 bfd_elf_generic_reloc),
411e1bfb
AM
595
596 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
0a1b45a2 597 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 598 bfd_elf_generic_reloc),
411e1bfb
AM
599
600 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
0a1b45a2 601 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 602 ppc64_elf_unhandled_reloc),
411e1bfb
AM
603
604 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
0a1b45a2 605 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 606 ppc64_elf_unhandled_reloc),
411e1bfb
AM
607
608 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
0a1b45a2 609 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 610 ppc64_elf_unhandled_reloc),
411e1bfb
AM
611
612 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
0a1b45a2 613 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 614 ppc64_elf_sectoff_reloc),
411e1bfb
AM
615
616 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
0a1b45a2 617 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 618 ppc64_elf_sectoff_reloc),
411e1bfb
AM
619
620 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
0a1b45a2 621 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 622 ppc64_elf_toc_reloc),
411e1bfb
AM
623
624 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
0a1b45a2 625 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 626 ppc64_elf_toc_reloc),
411e1bfb
AM
627
628 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
629 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
0a1b45a2 630 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 631 ppc64_elf_unhandled_reloc),
411e1bfb
AM
632
633 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
634 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 635 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 636 ppc64_elf_unhandled_reloc),
411e1bfb 637
727fc41e 638 /* Marker relocs for TLS. */
0a1b45a2 639 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
46807bf4
AM
640 bfd_elf_generic_reloc),
641
0a1b45a2 642 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
46807bf4
AM
643 bfd_elf_generic_reloc),
644
0a1b45a2 645 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
46807bf4 646 bfd_elf_generic_reloc),
727fc41e 647
23cedd1d
AM
648 /* Marker reloc for optimizing r2 save in prologue rather than on
649 each plt call stub. */
0a1b45a2 650 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
46807bf4 651 bfd_elf_generic_reloc),
3b421ab3 652
23cedd1d 653 /* Marker relocs on inline plt call instructions. */
0a1b45a2 654 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
46807bf4
AM
655 bfd_elf_generic_reloc),
656
0a1b45a2 657 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
46807bf4 658 bfd_elf_generic_reloc),
23cedd1d 659
411e1bfb
AM
660 /* Computes the load module index of the load module that contains the
661 definition of its TLS sym. */
0a1b45a2 662 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 663 ppc64_elf_unhandled_reloc),
411e1bfb
AM
664
665 /* Computes a dtv-relative displacement, the difference between the value
666 of sym+add and the base address of the thread-local storage block that
667 contains the definition of sym, minus 0x8000. */
0a1b45a2 668 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 669 ppc64_elf_unhandled_reloc),
411e1bfb
AM
670
671 /* A 16 bit dtprel reloc. */
0a1b45a2 672 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 673 ppc64_elf_unhandled_reloc),
411e1bfb
AM
674
675 /* Like DTPREL16, but no overflow. */
0a1b45a2 676 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 677 ppc64_elf_unhandled_reloc),
411e1bfb
AM
678
679 /* Like DTPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 680 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 681 ppc64_elf_unhandled_reloc),
411e1bfb
AM
682
683 /* Like DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 684 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 685 ppc64_elf_unhandled_reloc),
411e1bfb
AM
686
687 /* Like DTPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 688 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 689 ppc64_elf_unhandled_reloc),
411e1bfb
AM
690
691 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 692 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 693 ppc64_elf_unhandled_reloc),
411e1bfb
AM
694
695 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 696 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 697 ppc64_elf_unhandled_reloc),
411e1bfb
AM
698
699 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 700 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 701 ppc64_elf_unhandled_reloc),
411e1bfb
AM
702
703 /* Like DTPREL16, but for insns with a DS field. */
0a1b45a2 704 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 705 ppc64_elf_unhandled_reloc),
411e1bfb
AM
706
707 /* Like DTPREL16_DS, but no overflow. */
0a1b45a2 708 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 709 ppc64_elf_unhandled_reloc),
411e1bfb
AM
710
711 /* Computes a tp-relative displacement, the difference between the value of
712 sym+add and the value of the thread pointer (r13). */
0a1b45a2 713 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 714 ppc64_elf_unhandled_reloc),
411e1bfb
AM
715
716 /* A 16 bit tprel reloc. */
0a1b45a2 717 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 718 ppc64_elf_unhandled_reloc),
411e1bfb
AM
719
720 /* Like TPREL16, but no overflow. */
0a1b45a2 721 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 722 ppc64_elf_unhandled_reloc),
411e1bfb
AM
723
724 /* Like TPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 725 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 726 ppc64_elf_unhandled_reloc),
411e1bfb
AM
727
728 /* Like TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 729 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 730 ppc64_elf_unhandled_reloc),
411e1bfb
AM
731
732 /* Like TPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 733 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 734 ppc64_elf_unhandled_reloc),
411e1bfb
AM
735
736 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 737 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 738 ppc64_elf_unhandled_reloc),
411e1bfb
AM
739
740 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 741 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 742 ppc64_elf_unhandled_reloc),
411e1bfb
AM
743
744 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 745 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 746 ppc64_elf_unhandled_reloc),
411e1bfb
AM
747
748 /* Like TPREL16, but for insns with a DS field. */
0a1b45a2 749 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 750 ppc64_elf_unhandled_reloc),
411e1bfb
AM
751
752 /* Like TPREL16_DS, but no overflow. */
0a1b45a2 753 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 754 ppc64_elf_unhandled_reloc),
411e1bfb
AM
755
756 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
757 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
758 to the first entry relative to the TOC base (r2). */
0a1b45a2 759 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 760 ppc64_elf_unhandled_reloc),
5bd4f169 761
411e1bfb 762 /* Like GOT_TLSGD16, but no overflow. */
0a1b45a2 763 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 764 ppc64_elf_unhandled_reloc),
5bd4f169 765
411e1bfb 766 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
0a1b45a2 767 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 768 ppc64_elf_unhandled_reloc),
5bd4f169 769
411e1bfb 770 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
0a1b45a2 771 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 772 ppc64_elf_unhandled_reloc),
5bd4f169 773
411e1bfb
AM
774 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
775 with values (sym+add)@dtpmod and zero, and computes the offset to the
776 first entry relative to the TOC base (r2). */
0a1b45a2 777 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 778 ppc64_elf_unhandled_reloc),
5bd4f169 779
411e1bfb 780 /* Like GOT_TLSLD16, but no overflow. */
0a1b45a2 781 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 782 ppc64_elf_unhandled_reloc),
5bd4f169 783
411e1bfb 784 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
0a1b45a2 785 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 786 ppc64_elf_unhandled_reloc),
5bd4f169 787
411e1bfb 788 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
0a1b45a2 789 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 790 ppc64_elf_unhandled_reloc),
5bd4f169 791
411e1bfb
AM
792 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
793 the offset to the entry relative to the TOC base (r2). */
0a1b45a2 794 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 795 ppc64_elf_unhandled_reloc),
5bd4f169 796
411e1bfb 797 /* Like GOT_DTPREL16_DS, but no overflow. */
0a1b45a2 798 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 799 ppc64_elf_unhandled_reloc),
5bd4f169 800
411e1bfb 801 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 802 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 803 ppc64_elf_unhandled_reloc),
5bd4f169 804
411e1bfb 805 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 806 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 807 ppc64_elf_unhandled_reloc),
411e1bfb
AM
808
809 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
810 offset to the entry relative to the TOC base (r2). */
0a1b45a2 811 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 812 ppc64_elf_unhandled_reloc),
5bd4f169 813
411e1bfb 814 /* Like GOT_TPREL16_DS, but no overflow. */
0a1b45a2 815 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 816 ppc64_elf_unhandled_reloc),
5bd4f169 817
411e1bfb 818 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 819 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 820 ppc64_elf_unhandled_reloc),
5bd4f169 821
411e1bfb 822 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 823 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4
AM
824 ppc64_elf_unhandled_reloc),
825
0a1b45a2 826 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
46807bf4
AM
827 ppc64_elf_unhandled_reloc),
828
0a1b45a2 829 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 830 bfd_elf_generic_reloc),
e054468f 831
25f23106 832 /* A 16 bit relative relocation. */
0a1b45a2 833 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
46807bf4 834 bfd_elf_generic_reloc),
25f23106
AM
835
836 /* A 16 bit relative relocation without overflow. */
0a1b45a2 837 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
46807bf4 838 bfd_elf_generic_reloc),
25f23106
AM
839
840 /* The high order 16 bits of a relative address. */
0a1b45a2 841 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
46807bf4 842 bfd_elf_generic_reloc),
25f23106
AM
843
844 /* The high order 16 bits of a relative address, plus 1 if the contents of
845 the low 16 bits, treated as a signed number, is negative. */
0a1b45a2 846 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
46807bf4 847 ppc64_elf_ha_reloc),
25f23106 848
0a1b45a2 849 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
850 bfd_elf_generic_reloc),
851
0a1b45a2 852 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
853 ppc64_elf_ha_reloc),
854
0a1b45a2 855 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
856 bfd_elf_generic_reloc),
857
0a1b45a2 858 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
859 ppc64_elf_ha_reloc),
860
0a1b45a2 861 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
862 bfd_elf_generic_reloc),
863
0a1b45a2 864 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
865 ppc64_elf_ha_reloc),
866
a680de9a 867 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
0a1b45a2 868 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
46807bf4 869 ppc64_elf_ha_reloc),
a680de9a 870
7ba71655 871 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
0a1b45a2 872 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
46807bf4 873 ppc64_elf_ha_reloc),
7ba71655 874
f9c6b907 875 /* Like R_PPC64_ADDR16_HI, but no overflow. */
0a1b45a2 876 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 877 bfd_elf_generic_reloc),
f9c6b907
AM
878
879 /* Like R_PPC64_ADDR16_HA, but no overflow. */
0a1b45a2 880 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 881 ppc64_elf_ha_reloc),
f9c6b907
AM
882
883 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
0a1b45a2 884 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 885 ppc64_elf_unhandled_reloc),
f9c6b907
AM
886
887 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
0a1b45a2 888 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 889 ppc64_elf_unhandled_reloc),
f9c6b907
AM
890
891 /* Like R_PPC64_TPREL16_HI, but no overflow. */
0a1b45a2 892 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 893 ppc64_elf_unhandled_reloc),
f9c6b907
AM
894
895 /* Like R_PPC64_TPREL16_HA, but no overflow. */
0a1b45a2 896 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 897 ppc64_elf_unhandled_reloc),
f9c6b907 898
006589cf 899 /* Marker reloc on ELFv2 large-model function entry. */
0a1b45a2 900 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
46807bf4 901 bfd_elf_generic_reloc),
006589cf 902
45965137 903 /* Like ADDR64, but use local entry point of function. */
0a1b45a2 904 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 905 bfd_elf_generic_reloc),
45965137 906
0a1b45a2 907 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
908 bfd_elf_generic_reloc),
909
0a1b45a2 910 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
911 bfd_elf_generic_reloc),
912
0a1b45a2 913 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
5663e321
AM
914 bfd_elf_generic_reloc),
915
0a1b45a2 916 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
5663e321
AM
917 ppc64_elf_prefix_reloc),
918
0a1b45a2 919 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
5663e321
AM
920 ppc64_elf_prefix_reloc),
921
0a1b45a2 922 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
923 ppc64_elf_prefix_reloc),
924
0a1b45a2 925 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
926 ppc64_elf_prefix_reloc),
927
0a1b45a2 928 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
929 ppc64_elf_prefix_reloc),
930
0a1b45a2 931 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
932 ppc64_elf_unhandled_reloc),
933
0a1b45a2 934 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
935 ppc64_elf_unhandled_reloc),
936
0a1b45a2 937 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
938 ppc64_elf_unhandled_reloc),
939
0a1b45a2 940 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
941 ppc64_elf_unhandled_reloc),
942
0a1b45a2 943 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
944 ppc64_elf_unhandled_reloc),
945
0a1b45a2 946 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
947 ppc64_elf_unhandled_reloc),
948
0a1b45a2 949 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
950 ppc64_elf_unhandled_reloc),
951
0a1b45a2 952 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
953 ppc64_elf_unhandled_reloc),
954
0a1b45a2 955 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
956 ppc64_elf_unhandled_reloc),
957
0a1b45a2 958 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
959 bfd_elf_generic_reloc),
960
0a1b45a2 961 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
962 ppc64_elf_ha_reloc),
963
0a1b45a2 964 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
965 bfd_elf_generic_reloc),
966
0a1b45a2 967 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
968 ppc64_elf_ha_reloc),
969
0a1b45a2 970 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
971 bfd_elf_generic_reloc),
972
0a1b45a2 973 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
974 ppc64_elf_ha_reloc),
975
0a1b45a2 976 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
977 bfd_elf_generic_reloc),
978
0a1b45a2 979 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
980 ppc64_elf_ha_reloc),
981
0a1b45a2 982 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
5663e321
AM
983 ppc64_elf_prefix_reloc),
984
0a1b45a2 985 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
5663e321
AM
986 ppc64_elf_prefix_reloc),
987
5bd4f169 988 /* GNU extension to record C++ vtable hierarchy. */
0a1b45a2 989 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
46807bf4 990 NULL),
5bd4f169
AM
991
992 /* GNU extension to record C++ vtable member usage. */
0a1b45a2 993 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
46807bf4 994 NULL),
5bd4f169
AM
995};
996
997\f
998/* Initialize the ppc64_elf_howto_table, so that linear accesses can
999 be done. */
1000
1001static void
4ce794b7 1002ppc_howto_init (void)
5bd4f169
AM
1003{
1004 unsigned int i, type;
1005
a4b6fadd 1006 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
1007 {
1008 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 1009 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
1010 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1011 }
1012}
1013
1014static reloc_howto_type *
87c69f97 1015ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
5bd4f169 1016{
411e1bfb 1017 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1018
1019 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1020 /* Initialize howto table if needed. */
1021 ppc_howto_init ();
1022
4ce794b7 1023 switch (code)
5bd4f169
AM
1024 {
1025 default:
f3185997 1026 /* xgettext:c-format */
2cdcc330
AM
1027 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1028 (int) code);
f3185997 1029 bfd_set_error (bfd_error_bad_value);
4ce794b7 1030 return NULL;
5bd4f169 1031
411e1bfb
AM
1032 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1033 break;
1034 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1035 break;
1036 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1037 break;
1038 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1039 break;
1040 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1041 break;
1042 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1043 break;
f9c6b907
AM
1044 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1045 break;
411e1bfb 1046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1047 break;
f9c6b907
AM
1048 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1049 break;
411e1bfb 1050 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1051 break;
411e1bfb 1052 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1053 break;
411e1bfb 1054 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1057 break;
05d0e962
AM
1058 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1059 break;
7aba54da
AM
1060 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1061 break;
411e1bfb 1062 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1139 break;
411e1bfb 1140 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1141 break;
411e1bfb 1142 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1143 break;
411e1bfb 1144 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1145 break;
411e1bfb 1146 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1147 break;
411e1bfb 1148 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1149 break;
411e1bfb 1150 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1151 break;
411e1bfb 1152 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1153 break;
411e1bfb 1154 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1155 break;
411e1bfb 1156 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1157 break;
c213164a 1158 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1159 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1160 break;
727fc41e
AM
1161 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1162 break;
1163 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1164 break;
411e1bfb 1165 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1166 break;
411e1bfb 1167 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1168 break;
411e1bfb 1169 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1170 break;
411e1bfb 1171 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1172 break;
f9c6b907
AM
1173 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1174 break;
411e1bfb 1175 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1176 break;
f9c6b907
AM
1177 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1178 break;
411e1bfb 1179 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1180 break;
411e1bfb
AM
1181 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1182 break;
1183 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1184 break;
1185 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1186 break;
f9c6b907
AM
1187 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1188 break;
411e1bfb
AM
1189 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1190 break;
f9c6b907
AM
1191 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1192 break;
411e1bfb
AM
1193 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1218 break;
1219 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1220 break;
1221 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1222 break;
1223 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1226 break;
1227 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1228 break;
1229 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1238 break;
1239 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1240 break;
1241 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1250 break;
25f23106
AM
1251 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1252 break;
1253 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1254 break;
1255 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1256 break;
1257 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1258 break;
4a969973
AM
1259 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1260 break;
1261 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1262 break;
1263 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1270 break;
7ba71655
AM
1271 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1272 break;
a680de9a
PB
1273 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1274 break;
006589cf
AM
1275 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1276 break;
45965137
AM
1277 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1278 break;
5663e321
AM
1279 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1280 break;
1281 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1282 break;
1283 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1284 break;
1285 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1286 break;
1287 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1288 break;
1289 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1290 break;
1291 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1292 break;
c213164a
AM
1293 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1294 break;
1295 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1296 break;
87c69f97 1297 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
c213164a 1298 break;
87c69f97 1299 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
c213164a 1300 break;
87c69f97 1301 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
c213164a 1302 break;
87c69f97 1303 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
c213164a 1304 break;
5663e321
AM
1305 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1306 break;
1307 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1308 break;
1309 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1312 break;
1313 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1314 break;
1315 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1316 break;
1317 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1320 break;
1321 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1322 break;
1323 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1324 break;
411e1bfb
AM
1325 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1326 break;
1327 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1328 break;
1329 }
1330
4ce794b7 1331 return ppc64_elf_howto_table[r];
5bd4f169
AM
1332};
1333
157090f7 1334static reloc_howto_type *
87c69f97 1335ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
157090f7
AM
1336{
1337 unsigned int i;
87c69f97
AM
1338 static char *compat_map[][2] = {
1339 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1340 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1341 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1342 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1343 };
157090f7 1344
a4b6fadd 1345 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1346 if (ppc64_elf_howto_raw[i].name != NULL
1347 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1348 return &ppc64_elf_howto_raw[i];
1349
87c69f97
AM
1350 /* Handle old names of relocations in case they were used by
1351 .reloc directives.
1352 FIXME: Remove this soon. Mapping the reloc names is very likely
1353 completely unnecessary. */
1354 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1355 if (strcasecmp (compat_map[i][0], r_name) == 0)
1356 {
1357 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1358 compat_map[i][1], compat_map[i][0]);
1359 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1360 }
1361
157090f7
AM
1362 return NULL;
1363}
1364
5bd4f169
AM
1365/* Set the howto pointer for a PowerPC ELF reloc. */
1366
0a1b45a2 1367static bool
4aef7643 1368ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1369 Elf_Internal_Rela *dst)
5bd4f169 1370{
65f38f15
AM
1371 unsigned int type;
1372
ef60b7ff 1373 /* Initialize howto table if needed. */
5bd4f169 1374 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1375 ppc_howto_init ();
1376
65f38f15 1377 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1378 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1379 {
695344c0 1380 /* xgettext:c-format */
0aa13fee 1381 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1382 abfd, type);
f3185997 1383 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1384 return false;
d0fb9a8d 1385 }
65f38f15 1386 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1387 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1388 {
1389 /* xgettext:c-format */
1390 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1391 abfd, type);
1392 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1393 return false;
f3185997 1394 }
2cdcc330 1395
0a1b45a2 1396 return true;
5bd4f169
AM
1397}
1398
04c9666a 1399/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1400
1401static bfd_reloc_status_type
4ce794b7
AM
1402ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1403 void *data, asection *input_section,
1404 bfd *output_bfd, char **error_message)
5bd4f169 1405{
a680de9a
PB
1406 enum elf_ppc64_reloc_type r_type;
1407 long insn;
1408 bfd_size_type octets;
3de43e7b 1409 bfd_vma value;
a680de9a 1410
805fc799
AM
1411 /* If this is a relocatable link (output_bfd test tells us), just
1412 call the generic function. Any adjustment will be done at final
1413 link time. */
1414 if (output_bfd != NULL)
cedb70c5 1415 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1416 input_section, output_bfd, error_message);
1417
5663e321
AM
1418 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1419 We won't actually be using the low bits, so trashing them
805fc799 1420 doesn't matter. */
a680de9a 1421 r_type = reloc_entry->howto->type;
5663e321
AM
1422 if (r_type == R_PPC64_ADDR16_HIGHERA34
1423 || r_type == R_PPC64_ADDR16_HIGHESTA34
1424 || r_type == R_PPC64_REL16_HIGHERA34
1425 || r_type == R_PPC64_REL16_HIGHESTA34)
1426 reloc_entry->addend += 1ULL << 33;
1427 else
1428 reloc_entry->addend += 1U << 15;
a680de9a
PB
1429 if (r_type != R_PPC64_REL16DX_HA)
1430 return bfd_reloc_continue;
1431
1432 value = 0;
1433 if (!bfd_is_com_section (symbol->section))
1434 value = symbol->value;
1435 value += (reloc_entry->addend
1436 + symbol->section->output_offset
1437 + symbol->section->output_section->vma);
1438 value -= (reloc_entry->address
1439 + input_section->output_offset
1440 + input_section->output_section->vma);
3de43e7b 1441 value = (bfd_signed_vma) value >> 16;
a680de9a 1442
bb294208 1443 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1444 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1445 input_section, octets))
1446 return bfd_reloc_outofrange;
1447
a680de9a
PB
1448 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1449 insn &= ~0x1fffc1;
3de43e7b 1450 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1451 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1452 if (value + 0x8000 > 0xffff)
a680de9a
PB
1453 return bfd_reloc_overflow;
1454 return bfd_reloc_ok;
805fc799 1455}
5bd4f169 1456
2441e016
AM
1457static bfd_reloc_status_type
1458ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1459 void *data, asection *input_section,
1460 bfd *output_bfd, char **error_message)
1461{
1462 if (output_bfd != NULL)
1463 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1464 input_section, output_bfd, error_message);
1465
699733f6
AM
1466 if (strcmp (symbol->section->name, ".opd") == 0
1467 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1468 {
1469 bfd_vma dest = opd_entry_value (symbol->section,
1470 symbol->value + reloc_entry->addend,
0a1b45a2 1471 NULL, NULL, false);
2441e016
AM
1472 if (dest != (bfd_vma) -1)
1473 reloc_entry->addend = dest - (symbol->value
1474 + symbol->section->output_section->vma
1475 + symbol->section->output_offset);
1476 }
810d4e75
AM
1477 else
1478 {
1479 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1480
1481 if (symbol->section->owner != abfd
9f284bf9 1482 && symbol->section->owner != NULL
810d4e75
AM
1483 && abiversion (symbol->section->owner) >= 2)
1484 {
1485 unsigned int i;
1486
1487 for (i = 0; i < symbol->section->owner->symcount; ++i)
1488 {
1489 asymbol *symdef = symbol->section->owner->outsymbols[i];
1490
1491 if (strcmp (symdef->name, symbol->name) == 0)
1492 {
1493 elfsym = (elf_symbol_type *) symdef;
1494 break;
1495 }
1496 }
1497 }
1498 reloc_entry->addend
1499 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1500 }
2441e016
AM
1501 return bfd_reloc_continue;
1502}
1503
805fc799 1504static bfd_reloc_status_type
4ce794b7
AM
1505ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1506 void *data, asection *input_section,
1507 bfd *output_bfd, char **error_message)
805fc799
AM
1508{
1509 long insn;
04c9666a 1510 enum elf_ppc64_reloc_type r_type;
805fc799 1511 bfd_size_type octets;
794e51c0 1512 /* Assume 'at' branch hints. */
0a1b45a2 1513 bool is_isa_v2 = true;
805fc799
AM
1514
1515 /* If this is a relocatable link (output_bfd test tells us), just
1516 call the generic function. Any adjustment will be done at final
1517 link time. */
5bd4f169 1518 if (output_bfd != NULL)
cedb70c5 1519 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1520 input_section, output_bfd, error_message);
1521
bb294208 1522 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
b54509b8
AM
1523 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1524 input_section, octets))
1525 return bfd_reloc_outofrange;
1526
805fc799
AM
1527 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1528 insn &= ~(0x01 << 21);
4ce794b7 1529 r_type = reloc_entry->howto->type;
805fc799
AM
1530 if (r_type == R_PPC64_ADDR14_BRTAKEN
1531 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1532 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1533
794e51c0 1534 if (is_isa_v2)
5bd4f169 1535 {
805fc799
AM
1536 /* Set 'a' bit. This is 0b00010 in BO field for branch
1537 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1538 for branch on CTR insns (BO == 1a00t or 1a01t). */
1539 if ((insn & (0x14 << 21)) == (0x04 << 21))
1540 insn |= 0x02 << 21;
1541 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1542 insn |= 0x08 << 21;
1543 else
2441e016 1544 goto out;
5bd4f169 1545 }
805fc799
AM
1546 else
1547 {
1548 bfd_vma target = 0;
1549 bfd_vma from;
5bd4f169 1550
805fc799
AM
1551 if (!bfd_is_com_section (symbol->section))
1552 target = symbol->value;
1553 target += symbol->section->output_section->vma;
1554 target += symbol->section->output_offset;
1555 target += reloc_entry->addend;
5bd4f169 1556
805fc799
AM
1557 from = (reloc_entry->address
1558 + input_section->output_offset
1559 + input_section->output_section->vma);
5bd4f169 1560
805fc799
AM
1561 /* Invert 'y' bit if not the default. */
1562 if ((bfd_signed_vma) (target - from) < 0)
1563 insn ^= 0x01 << 21;
1564 }
4ce794b7 1565 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1566 out:
1567 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1568 input_section, output_bfd, error_message);
805fc799 1569}
5bd4f169 1570
805fc799 1571static bfd_reloc_status_type
4ce794b7
AM
1572ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1573 void *data, asection *input_section,
1574 bfd *output_bfd, char **error_message)
805fc799
AM
1575{
1576 /* If this is a relocatable link (output_bfd test tells us), just
1577 call the generic function. Any adjustment will be done at final
1578 link time. */
1579 if (output_bfd != NULL)
cedb70c5 1580 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1581 input_section, output_bfd, error_message);
5bd4f169 1582
805fc799
AM
1583 /* Subtract the symbol section base address. */
1584 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1585 return bfd_reloc_continue;
1586}
1587
805fc799 1588static bfd_reloc_status_type
4ce794b7
AM
1589ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
805fc799
AM
1592{
1593 /* If this is a relocatable link (output_bfd test tells us), just
1594 call the generic function. Any adjustment will be done at final
1595 link time. */
1596 if (output_bfd != NULL)
cedb70c5 1597 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1598 input_section, output_bfd, error_message);
1599
1600 /* Subtract the symbol section base address. */
1601 reloc_entry->addend -= symbol->section->output_section->vma;
1602
1603 /* Adjust the addend for sign extension of the low 16 bits. */
1604 reloc_entry->addend += 0x8000;
1605 return bfd_reloc_continue;
1606}
1607
1608static bfd_reloc_status_type
4ce794b7
AM
1609ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1610 void *data, asection *input_section,
1611 bfd *output_bfd, char **error_message)
805fc799
AM
1612{
1613 bfd_vma TOCstart;
1614
1615 /* If this is a relocatable link (output_bfd test tells us), just
1616 call the generic function. Any adjustment will be done at final
1617 link time. */
1618 if (output_bfd != NULL)
cedb70c5 1619 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1620 input_section, output_bfd, error_message);
1621
1622 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1623 if (TOCstart == 0)
1c865ab2 1624 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1625
1626 /* Subtract the TOC base address. */
1627 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1628 return bfd_reloc_continue;
1629}
1630
1631static bfd_reloc_status_type
4ce794b7
AM
1632ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1633 void *data, asection *input_section,
1634 bfd *output_bfd, char **error_message)
805fc799
AM
1635{
1636 bfd_vma TOCstart;
1637
1638 /* If this is a relocatable link (output_bfd test tells us), just
1639 call the generic function. Any adjustment will be done at final
1640 link time. */
1641 if (output_bfd != NULL)
cedb70c5 1642 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1643 input_section, output_bfd, error_message);
1644
1645 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1646 if (TOCstart == 0)
1c865ab2 1647 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1648
1649 /* Subtract the TOC base address. */
1650 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1651
1652 /* Adjust the addend for sign extension of the low 16 bits. */
1653 reloc_entry->addend += 0x8000;
1654 return bfd_reloc_continue;
1655}
1656
1657static bfd_reloc_status_type
4ce794b7
AM
1658ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1659 void *data, asection *input_section,
1660 bfd *output_bfd, char **error_message)
805fc799
AM
1661{
1662 bfd_vma TOCstart;
1663 bfd_size_type octets;
1664
1665 /* If this is a relocatable link (output_bfd test tells us), just
1666 call the generic function. Any adjustment will be done at final
1667 link time. */
1668 if (output_bfd != NULL)
cedb70c5 1669 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1670 input_section, output_bfd, error_message);
1671
b54509b8
AM
1672 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1673 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1674 input_section, octets))
1675 return bfd_reloc_outofrange;
1676
805fc799
AM
1677 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1678 if (TOCstart == 0)
1c865ab2 1679 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799 1680
805fc799
AM
1681 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1682 return bfd_reloc_ok;
1683}
1684
5663e321
AM
1685static bfd_reloc_status_type
1686ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1687 void *data, asection *input_section,
1688 bfd *output_bfd, char **error_message)
1689{
1690 uint64_t insn;
1691 bfd_vma targ;
b54509b8 1692 bfd_size_type octets;
5663e321
AM
1693
1694 if (output_bfd != NULL)
1695 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1696 input_section, output_bfd, error_message);
1697
b54509b8
AM
1698 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1699 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1700 input_section, octets))
1701 return bfd_reloc_outofrange;
1702
1703 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
5663e321 1704 insn <<= 32;
b54509b8 1705 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
5663e321
AM
1706
1707 targ = (symbol->section->output_section->vma
1708 + symbol->section->output_offset
1709 + reloc_entry->addend);
1710 if (!bfd_is_com_section (symbol->section))
1711 targ += symbol->value;
1712 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1713 targ += 1ULL << 33;
1714 if (reloc_entry->howto->pc_relative)
1715 {
1716 bfd_vma from = (reloc_entry->address
1717 + input_section->output_offset
1718 + input_section->output_section->vma);
1719 targ -=from;
1720 }
1721 targ >>= reloc_entry->howto->rightshift;
1722 insn &= ~reloc_entry->howto->dst_mask;
1723 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
b54509b8
AM
1724 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1725 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
5663e321
AM
1726 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1727 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1728 >= 1ULL << reloc_entry->howto->bitsize))
1729 return bfd_reloc_overflow;
1730 return bfd_reloc_ok;
1731}
1732
805fc799 1733static bfd_reloc_status_type
4ce794b7
AM
1734ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1735 void *data, asection *input_section,
1736 bfd *output_bfd, char **error_message)
805fc799
AM
1737{
1738 /* If this is a relocatable link (output_bfd test tells us), just
1739 call the generic function. Any adjustment will be done at final
1740 link time. */
1741 if (output_bfd != NULL)
cedb70c5 1742 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1743 input_section, output_bfd, error_message);
1744
1745 if (error_message != NULL)
1746 {
7fbd5f4e
AM
1747 static char *message;
1748 free (message);
1749 if (asprintf (&message, _("generic linker can't handle %s"),
1750 reloc_entry->howto->name) < 0)
1751 message = NULL;
1752 *error_message = message;
805fc799
AM
1753 }
1754 return bfd_reloc_dangerous;
1755}
1756
927be08e
AM
1757/* Track GOT entries needed for a given symbol. We might need more
1758 than one got entry per symbol. */
1759struct got_entry
1760{
1761 struct got_entry *next;
1762
1763 /* The symbol addend that we'll be placing in the GOT. */
1764 bfd_vma addend;
1765
1766 /* Unlike other ELF targets, we use separate GOT entries for the same
1767 symbol referenced from different input files. This is to support
1768 automatic multiple TOC/GOT sections, where the TOC base can vary
1769 from one input file to another. After partitioning into TOC groups
1770 we merge entries within the group.
1771
1772 Point to the BFD owning this GOT entry. */
1773 bfd *owner;
1774
1775 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1776 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1777 unsigned char tls_type;
927be08e
AM
1778
1779 /* Non-zero if got.ent points to real entry. */
f961d9dd 1780 unsigned char is_indirect;
927be08e
AM
1781
1782 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1783 union
2cdcc330
AM
1784 {
1785 bfd_signed_vma refcount;
1786 bfd_vma offset;
1787 struct got_entry *ent;
1788 } got;
927be08e
AM
1789};
1790
1791/* The same for PLT. */
1792struct plt_entry
1793{
1794 struct plt_entry *next;
1795
1796 bfd_vma addend;
1797
1798 union
2cdcc330
AM
1799 {
1800 bfd_signed_vma refcount;
1801 bfd_vma offset;
1802 } plt;
927be08e
AM
1803};
1804
e717da7e
AM
1805struct ppc64_elf_obj_tdata
1806{
1807 struct elf_obj_tdata elf;
1808
1809 /* Shortcuts to dynamic linker sections. */
1810 asection *got;
1811 asection *relgot;
1812
b3fac117
AM
1813 /* Used during garbage collection. We attach global symbols defined
1814 on removed .opd entries to this section so that the sym is removed. */
1815 asection *deleted_section;
81688140 1816
927be08e 1817 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1818 sections means we potentially need one of these for each input bfd. */
927be08e 1819 struct got_entry tlsld_got;
8860955f 1820
2cdcc330
AM
1821 union
1822 {
729eabd5
AM
1823 /* A copy of relocs before they are modified for --emit-relocs. */
1824 Elf_Internal_Rela *relocs;
1825
1826 /* Section contents. */
1827 bfd_byte *contents;
1828 } opd;
d77c8a4b
AM
1829
1830 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1831 the reloc to be in the range -32768 to 32767. */
98528052
AM
1832 unsigned int has_small_toc_reloc : 1;
1833
560c8763
AM
1834 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1835 instruction not one we handle. */
1836 unsigned int unexpected_toc_insn : 1;
066f4018 1837
903b777d
AM
1838 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1839 this file. */
1840 unsigned int has_optrel : 1;
e717da7e
AM
1841};
1842
1843#define ppc64_elf_tdata(bfd) \
1844 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1845
1846#define ppc64_tlsld_got(bfd) \
1847 (&ppc64_elf_tdata (bfd)->tlsld_got)
1848
0c8d6e5c
AM
1849#define is_ppc64_elf(bfd) \
1850 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1851 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1852
e717da7e
AM
1853/* Override the generic function because we store some extras. */
1854
0a1b45a2 1855static bool
e717da7e
AM
1856ppc64_elf_mkobject (bfd *abfd)
1857{
0ffa91dd 1858 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1859 PPC64_ELF_DATA);
e717da7e
AM
1860}
1861
feee612b 1862/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1863 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1864
0a1b45a2 1865static bool
4ce794b7 1866ppc64_elf_object_p (bfd *abfd)
feee612b 1867{
14b57c7c 1868 if (!abfd->arch_info->the_default)
0a1b45a2 1869 return true;
14b57c7c
AM
1870
1871 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1872 {
1873 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1874
1875 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1876 {
1877 /* Relies on arch after 32 bit default being 64 bit default. */
1878 abfd->arch_info = abfd->arch_info->next;
1879 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1880 }
1881 }
14b57c7c 1882 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1883}
1884
d37c89e5
AM
1885/* Support for core dump NOTE sections. */
1886
0a1b45a2 1887static bool
d37c89e5
AM
1888ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1889{
eea6121a 1890 size_t offset, size;
d37c89e5
AM
1891
1892 if (note->descsz != 504)
0a1b45a2 1893 return false;
d37c89e5
AM
1894
1895 /* pr_cursig */
228e534f 1896 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1897
1898 /* pr_pid */
228e534f 1899 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1900
1901 /* pr_reg */
1902 offset = 112;
eea6121a 1903 size = 384;
d37c89e5
AM
1904
1905 /* Make a ".reg/999" section. */
1906 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1907 size, note->descpos + offset);
d37c89e5
AM
1908}
1909
0a1b45a2 1910static bool
d37c89e5
AM
1911ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1912{
1913 if (note->descsz != 136)
0a1b45a2 1914 return false;
d37c89e5 1915
228e534f 1916 elf_tdata (abfd)->core->pid
bc989cdc 1917 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1918 elf_tdata (abfd)->core->program
d37c89e5 1919 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1920 elf_tdata (abfd)->core->command
d37c89e5
AM
1921 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1922
0a1b45a2 1923 return true;
d37c89e5
AM
1924}
1925
183e98be
AM
1926static char *
1927ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1928 ...)
1929{
1930 switch (note_type)
1931 {
1932 default:
1933 return NULL;
1934
1935 case NT_PRPSINFO:
1936 {
9ef6d1e3 1937 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1938 va_list ap;
1939
1940 va_start (ap, note_type);
75cd47ed 1941 memset (data, 0, sizeof (data));
183e98be 1942 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1943#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1944 DIAGNOSTIC_PUSH;
be3e27bb 1945 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1946 -Wstringop-truncation:
1947 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1948 */
95da9854
L
1949 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1950#endif
183e98be 1951 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1952#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1953 DIAGNOSTIC_POP;
fe75810f 1954#endif
183e98be
AM
1955 va_end (ap);
1956 return elfcore_write_note (abfd, buf, bufsiz,
1957 "CORE", note_type, data, sizeof (data));
1958 }
1959
1960 case NT_PRSTATUS:
1961 {
1962 char data[504];
1963 va_list ap;
1964 long pid;
1965 int cursig;
1966 const void *greg;
1967
1968 va_start (ap, note_type);
1969 memset (data, 0, 112);
1970 pid = va_arg (ap, long);
1971 bfd_put_32 (abfd, pid, data + 32);
1972 cursig = va_arg (ap, int);
1973 bfd_put_16 (abfd, cursig, data + 12);
1974 greg = va_arg (ap, const void *);
1975 memcpy (data + 112, greg, 384);
1976 memset (data + 496, 0, 8);
1977 va_end (ap);
1978 return elfcore_write_note (abfd, buf, bufsiz,
1979 "CORE", note_type, data, sizeof (data));
1980 }
1981 }
1982}
1983
5d35169e
AM
1984/* Add extra PPC sections. */
1985
2cdcc330 1986static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1987{
07d6d2b8
AM
1988 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1989 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1990 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1991 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1993 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1994 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1995};
1996
7c8fe5c4
AM
1997enum _ppc64_sec_type {
1998 sec_normal = 0,
1999 sec_opd = 1,
2000 sec_toc = 2
2001};
2002
f0abc2a1
AM
2003struct _ppc64_elf_section_data
2004{
2005 struct bfd_elf_section_data elf;
411e1bfb 2006
f0abc2a1
AM
2007 union
2008 {
51aecdc5
AM
2009 /* An array with one entry for each opd function descriptor,
2010 and some spares since opd entries may be either 16 or 24 bytes. */
2011#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
2012 struct _opd_sec_data
2013 {
2014 /* Points to the function code section for local opd entries. */
2015 asection **func_sec;
2016
2017 /* After editing .opd, adjust references to opd local syms. */
2018 long *adjust;
2019 } opd;
7c8fe5c4 2020
3a71aa26
AM
2021 /* An array for toc sections, indexed by offset/8. */
2022 struct _toc_sec_data
2023 {
2024 /* Specifies the relocation symbol index used at a given toc offset. */
2025 unsigned *symndx;
2026
2027 /* And the relocation addend. */
2028 bfd_vma *add;
2029 } toc;
7c8fe5c4
AM
2030 } u;
2031
2032 enum _ppc64_sec_type sec_type:2;
411e1bfb 2033
7c8fe5c4
AM
2034 /* Flag set when small branches are detected. Used to
2035 select suitable defaults for the stub group size. */
2036 unsigned int has_14bit_branch:1;
3e04d765
AM
2037
2038 /* Flag set when PLTCALL relocs are detected. */
2039 unsigned int has_pltcall:1;
066f4018 2040
903b777d
AM
2041 /* Flag set when section has PLT/GOT/TOC relocations that can be
2042 optimised. */
2043 unsigned int has_optrel:1;
f0abc2a1
AM
2044};
2045
2046#define ppc64_elf_section_data(sec) \
411e1bfb 2047 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1 2048
0a1b45a2 2049static bool
4ce794b7 2050ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2051{
f592407e
AM
2052 if (!sec->used_by_bfd)
2053 {
2054 struct _ppc64_elf_section_data *sdata;
986f0783 2055 size_t amt = sizeof (*sdata);
f0abc2a1 2056
f592407e
AM
2057 sdata = bfd_zalloc (abfd, amt);
2058 if (sdata == NULL)
0a1b45a2 2059 return false;
f592407e
AM
2060 sec->used_by_bfd = sdata;
2061 }
f0abc2a1
AM
2062
2063 return _bfd_elf_new_section_hook (abfd, sec);
2064}
4025353c 2065
0a1b45a2 2066static bool
bf577467
AM
2067ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2068{
2069 const char *name = hdr->bfd_section->name;
2070
3f3328b8
ML
2071 if (startswith (name, ".sbss")
2072 || startswith (name, ".sdata"))
bf577467
AM
2073 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2074
0a1b45a2 2075 return true;
bf577467
AM
2076}
2077
74f0fb50 2078static struct _opd_sec_data *
4025353c
AM
2079get_opd_info (asection * sec)
2080{
2081 if (sec != NULL
2082 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2083 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2084 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2085 return NULL;
2086}
90e3cdf2
JJ
2087\f
2088/* Parameters for the qsort hook. */
0a1b45a2 2089static bool synthetic_relocatable;
8cb1c2c8 2090static const asection *synthetic_opd;
90e3cdf2 2091
699733f6 2092/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2093
2094static int
2095compare_symbols (const void *ap, const void *bp)
2096{
2cdcc330
AM
2097 const asymbol *a = *(const asymbol **) ap;
2098 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2099
699733f6
AM
2100 /* Section symbols first. */
2101 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2102 return -1;
699733f6 2103 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2104 return 1;
2105
699733f6 2106 /* then .opd symbols. */
cd285db5
AM
2107 if (synthetic_opd != NULL)
2108 {
2109 if (strcmp (a->section->name, ".opd") == 0
2110 && strcmp (b->section->name, ".opd") != 0)
2111 return -1;
2112 if (strcmp (a->section->name, ".opd") != 0
2113 && strcmp (b->section->name, ".opd") == 0)
2114 return 1;
2115 }
90e3cdf2 2116
699733f6 2117 /* then other code symbols. */
2cdcc330
AM
2118 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2119 == (SEC_CODE | SEC_ALLOC))
2120 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2121 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2122 return -1;
2123
2cdcc330
AM
2124 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2125 != (SEC_CODE | SEC_ALLOC))
2126 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2127 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2128 return 1;
2129
2130 if (synthetic_relocatable)
2131 {
2132 if (a->section->id < b->section->id)
2133 return -1;
2134
2135 if (a->section->id > b->section->id)
2136 return 1;
2137 }
2138
2139 if (a->value + a->section->vma < b->value + b->section->vma)
2140 return -1;
2141
2142 if (a->value + a->section->vma > b->value + b->section->vma)
2143 return 1;
2144
4d35a0aa
AM
2145 /* For syms with the same value, prefer strong dynamic global function
2146 syms over other syms. */
2147 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2148 return -1;
2149
2150 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2151 return 1;
2152
2153 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2154 return -1;
2155
2156 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2157 return 1;
2158
2159 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2160 return -1;
2161
2162 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2163 return 1;
2164
2165 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2166 return -1;
2167
2168 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2169 return 1;
2170
dcea6a95
AM
2171 /* Finally, sort on where the symbol is in memory. The symbols will
2172 be in at most two malloc'd blocks, one for static syms, one for
2173 dynamic syms, and we distinguish the two blocks above by testing
2174 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2175 originally in the same order as the symbols (and we're not
2176 sorting the symbols themselves), this ensures a stable sort. */
2177 if (a < b)
2178 return -1;
2179 if (a > b)
2180 return 1;
2181 return 0;
90e3cdf2
JJ
2182}
2183
699733f6 2184/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2185
699733f6 2186static asymbol *
9ad9b810
AM
2187sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2188 bfd_vma value)
90e3cdf2 2189{
9ad9b810 2190 size_t mid;
90e3cdf2 2191
7292b3ac 2192 if (id == (unsigned) -1)
699733f6
AM
2193 {
2194 while (lo < hi)
2195 {
2196 mid = (lo + hi) >> 1;
2197 if (syms[mid]->value + syms[mid]->section->vma < value)
2198 lo = mid + 1;
2199 else if (syms[mid]->value + syms[mid]->section->vma > value)
2200 hi = mid;
2201 else
2202 return syms[mid];
2203 }
2204 }
2205 else
2206 {
2207 while (lo < hi)
2208 {
2209 mid = (lo + hi) >> 1;
2210 if (syms[mid]->section->id < id)
2211 lo = mid + 1;
2212 else if (syms[mid]->section->id > id)
2213 hi = mid;
2214 else if (syms[mid]->value < value)
2215 lo = mid + 1;
2216 else if (syms[mid]->value > value)
2217 hi = mid;
2218 else
2219 return syms[mid];
2220 }
2221 }
2222 return NULL;
90e3cdf2
JJ
2223}
2224
0a1b45a2 2225static bool
468392fb
AM
2226section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2227{
2228 bfd_vma vma = *(bfd_vma *) ptr;
2229 return ((section->flags & SEC_ALLOC) != 0
2230 && section->vma <= vma
2231 && vma < section->vma + section->size);
2232}
2233
699733f6 2234/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2235 entry syms. Also generate @plt symbols for the glink branch table.
2236 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2237
2238static long
a7535cf3
AM
2239ppc64_elf_get_synthetic_symtab (bfd *abfd,
2240 long static_count, asymbol **static_syms,
2241 long dyn_count, asymbol **dyn_syms,
c9727e01 2242 asymbol **ret)
90e3cdf2
JJ
2243{
2244 asymbol *s;
0ccf57bd 2245 size_t i, j, count;
90e3cdf2 2246 char *names;
0ccf57bd 2247 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2248 asection *opd = NULL;
0a1b45a2 2249 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2250 asymbol **syms;
ee67d69a 2251 int abi = abiversion (abfd);
90e3cdf2
JJ
2252
2253 *ret = NULL;
2254
ee67d69a
AM
2255 if (abi < 2)
2256 {
2257 opd = bfd_get_section_by_name (abfd, ".opd");
2258 if (opd == NULL && abi == 1)
2259 return 0;
2260 }
90e3cdf2 2261
a5259595
AM
2262 syms = NULL;
2263 codesecsym = 0;
2264 codesecsymend = 0;
2265 secsymend = 0;
2266 opdsymend = 0;
2267 symcount = 0;
2268 if (opd != NULL)
c9727e01 2269 {
a5259595
AM
2270 symcount = static_count;
2271 if (!relocatable)
2272 symcount += dyn_count;
2273 if (symcount == 0)
2274 return 0;
c9727e01 2275
a5259595
AM
2276 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2277 if (syms == NULL)
2278 return -1;
90e3cdf2 2279
a5259595
AM
2280 if (!relocatable && static_count != 0 && dyn_count != 0)
2281 {
2282 /* Use both symbol tables. */
2283 memcpy (syms, static_syms, static_count * sizeof (*syms));
2284 memcpy (syms + static_count, dyn_syms,
2285 (dyn_count + 1) * sizeof (*syms));
2286 }
2287 else if (!relocatable && static_count == 0)
2288 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2289 else
2290 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2291
0ccf57bd
AM
2292 /* Trim uninteresting symbols. Interesting symbols are section,
2293 function, and notype symbols. */
2294 for (i = 0, j = 0; i < symcount; ++i)
2295 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2296 | BSF_RELC | BSF_SRELC)) == 0)
2297 syms[j++] = syms[i];
2298 symcount = j;
2299
a5259595
AM
2300 synthetic_relocatable = relocatable;
2301 synthetic_opd = opd;
2302 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2303
a5259595
AM
2304 if (!relocatable && symcount > 1)
2305 {
bfa5bd2a
PA
2306 /* Trim duplicate syms, since we may have merged the normal
2307 and dynamic symbols. Actually, we only care about syms
2308 that have different values, so trim any with the same
2309 value. Don't consider ifunc and ifunc resolver symbols
2310 duplicates however, because GDB wants to know whether a
2311 text symbol is an ifunc resolver. */
a5259595 2312 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2313 {
2314 const asymbol *s0 = syms[i - 1];
2315 const asymbol *s1 = syms[i];
2316
2317 if ((s0->value + s0->section->vma
2318 != s1->value + s1->section->vma)
2319 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2320 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2321 syms[j++] = syms[i];
2322 }
a5259595
AM
2323 symcount = j;
2324 }
699733f6 2325
a5259595
AM
2326 i = 0;
2327 /* Note that here and in compare_symbols we can't compare opd and
2328 sym->section directly. With separate debug info files, the
2329 symbols will be extracted from the debug file while abfd passed
2330 to this function is the real binary. */
8bd10d6b
AM
2331 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2332 && strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2333 ++i;
2334 codesecsym = i;
2335
2336 for (; i < symcount; ++i)
2337 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2338 | SEC_THREAD_LOCAL))
2339 != (SEC_CODE | SEC_ALLOC))
2340 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2341 break;
2342 codesecsymend = i;
2343
2344 for (; i < symcount; ++i)
2345 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2346 break;
2347 secsymend = i;
2348
2349 for (; i < symcount; ++i)
2350 if (strcmp (syms[i]->section->name, ".opd") != 0)
2351 break;
2352 opdsymend = i;
2353
2354 for (; i < symcount; ++i)
2cdcc330
AM
2355 if (((syms[i]->section->flags
2356 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2357 != (SEC_CODE | SEC_ALLOC))
2358 break;
2359 symcount = i;
2360 }
c9727e01 2361 count = 0;
90e3cdf2 2362
699733f6 2363 if (relocatable)
90e3cdf2 2364 {
0a1b45a2 2365 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
699733f6
AM
2366 arelent *r;
2367 size_t size;
0ccf57bd 2368 size_t relcount;
90e3cdf2 2369
468392fb
AM
2370 if (opdsymend == secsymend)
2371 goto done;
2372
699733f6 2373 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2374 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2375 if (relcount == 0)
c9727e01 2376 goto done;
90e3cdf2 2377
0a1b45a2 2378 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
7356fed5
AM
2379 {
2380 count = -1;
2381 goto done;
2382 }
2383
699733f6 2384 size = 0;
595da8c5 2385 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2386 {
2387 asymbol *sym;
90e3cdf2 2388
595da8c5 2389 while (r < opd->relocation + relcount
699733f6
AM
2390 && r->address < syms[i]->value + opd->vma)
2391 ++r;
90e3cdf2 2392
595da8c5 2393 if (r == opd->relocation + relcount)
699733f6 2394 break;
90e3cdf2 2395
699733f6
AM
2396 if (r->address != syms[i]->value + opd->vma)
2397 continue;
90e3cdf2 2398
699733f6
AM
2399 if (r->howto->type != R_PPC64_ADDR64)
2400 continue;
90e3cdf2 2401
699733f6
AM
2402 sym = *r->sym_ptr_ptr;
2403 if (!sym_exists_at (syms, opdsymend, symcount,
2404 sym->section->id, sym->value + r->addend))
2405 {
2406 ++count;
2407 size += sizeof (asymbol);
2408 size += strlen (syms[i]->name) + 2;
2409 }
2410 }
90e3cdf2 2411
c4b0b099
AM
2412 if (size == 0)
2413 goto done;
699733f6
AM
2414 s = *ret = bfd_malloc (size);
2415 if (s == NULL)
2416 {
7356fed5 2417 count = -1;
c9727e01 2418 goto done;
699733f6 2419 }
90e3cdf2 2420
699733f6 2421 names = (char *) (s + count);
90e3cdf2 2422
595da8c5 2423 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2424 {
699733f6 2425 asymbol *sym;
90e3cdf2 2426
595da8c5 2427 while (r < opd->relocation + relcount
699733f6
AM
2428 && r->address < syms[i]->value + opd->vma)
2429 ++r;
90e3cdf2 2430
595da8c5 2431 if (r == opd->relocation + relcount)
699733f6
AM
2432 break;
2433
2434 if (r->address != syms[i]->value + opd->vma)
2435 continue;
2436
2437 if (r->howto->type != R_PPC64_ADDR64)
2438 continue;
90e3cdf2 2439
699733f6
AM
2440 sym = *r->sym_ptr_ptr;
2441 if (!sym_exists_at (syms, opdsymend, symcount,
2442 sym->section->id, sym->value + r->addend))
2443 {
2444 size_t len;
2445
2446 *s = *syms[i];
6ba2a415 2447 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2448 s->section = sym->section;
2449 s->value = sym->value + r->addend;
2450 s->name = names;
2451 *names++ = '.';
2452 len = strlen (syms[i]->name);
2453 memcpy (names, syms[i]->name, len + 1);
2454 names += len + 1;
6f610d07
UW
2455 /* Have udata.p point back to the original symbol this
2456 synthetic symbol was derived from. */
2457 s->udata.p = syms[i];
699733f6
AM
2458 s++;
2459 }
2460 }
2461 }
2462 else
90e3cdf2 2463 {
0a1b45a2 2464 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
ee67d69a 2465 bfd_byte *contents = NULL;
699733f6 2466 size_t size;
0ccf57bd 2467 size_t plt_count = 0;
468392fb
AM
2468 bfd_vma glink_vma = 0, resolv_vma = 0;
2469 asection *dynamic, *glink = NULL, *relplt = NULL;
2470 arelent *p;
90e3cdf2 2471
ee67d69a 2472 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2473 {
c4b0b099
AM
2474 free_contents_and_exit_err:
2475 count = -1;
ee67d69a 2476 free_contents_and_exit:
c9594989 2477 free (contents);
c9727e01 2478 goto done;
699733f6 2479 }
90e3cdf2 2480
699733f6
AM
2481 size = 0;
2482 for (i = secsymend; i < opdsymend; ++i)
2483 {
2484 bfd_vma ent;
90e3cdf2 2485
5ef11c02
AM
2486 /* Ignore bogus symbols. */
2487 if (syms[i]->value > opd->size - 8)
2488 continue;
2489
699733f6
AM
2490 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2491 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2492 {
2493 ++count;
2494 size += sizeof (asymbol);
2495 size += strlen (syms[i]->name) + 2;
2496 }
2497 }
90e3cdf2 2498
468392fb 2499 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2500 if (dyn_count != 0
2501 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2502 {
2503 bfd_byte *dynbuf, *extdyn, *extdynend;
2504 size_t extdynsize;
2505 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2506
2507 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2508 goto free_contents_and_exit_err;
468392fb
AM
2509
2510 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2511 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2512
2513 extdyn = dynbuf;
2514 extdynend = extdyn + dynamic->size;
2515 for (; extdyn < extdynend; extdyn += extdynsize)
2516 {
2517 Elf_Internal_Dyn dyn;
2518 (*swap_dyn_in) (abfd, extdyn, &dyn);
2519
2520 if (dyn.d_tag == DT_NULL)
2521 break;
2522
2523 if (dyn.d_tag == DT_PPC64_GLINK)
2524 {
9e390558
AM
2525 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2526 See comment in ppc64_elf_finish_dynamic_sections. */
2527 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2528 /* The .glink section usually does not survive the final
2529 link; search for the section (usually .text) where the
2530 glink stubs now reside. */
2531 glink = bfd_sections_find_if (abfd, section_covers_vma,
2532 &glink_vma);
2533 break;
2534 }
2535 }
2536
2537 free (dynbuf);
2538 }
2539
2540 if (glink != NULL)
2541 {
2542 /* Determine __glink trampoline by reading the relative branch
2543 from the first glink stub. */
2544 bfd_byte buf[4];
b9e5796b
AM
2545 unsigned int off = 0;
2546
2547 while (bfd_get_section_contents (abfd, glink, buf,
2548 glink_vma + off - glink->vma, 4))
468392fb
AM
2549 {
2550 unsigned int insn = bfd_get_32 (abfd, buf);
2551 insn ^= B_DOT;
2552 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2553 {
2cdcc330
AM
2554 resolv_vma
2555 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2556 break;
2557 }
2558 off += 4;
2559 if (off > 4)
2560 break;
468392fb
AM
2561 }
2562
2563 if (resolv_vma)
2564 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2565
066ee829
AM
2566 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2567 if (relplt != NULL)
2568 {
2569 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 2570 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
c4b0b099 2571 goto free_contents_and_exit_err;
68ffbac6 2572
066ee829
AM
2573 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2574 size += plt_count * sizeof (asymbol);
468392fb 2575
066ee829
AM
2576 p = relplt->relocation;
2577 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2578 {
2579 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2580 if (p->addend != 0)
2581 size += sizeof ("+0x") - 1 + 16;
2582 }
066ee829 2583 }
468392fb
AM
2584 }
2585
c4b0b099
AM
2586 if (size == 0)
2587 goto free_contents_and_exit;
699733f6
AM
2588 s = *ret = bfd_malloc (size);
2589 if (s == NULL)
c4b0b099 2590 goto free_contents_and_exit_err;
90e3cdf2 2591
468392fb 2592 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2593
699733f6 2594 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2595 {
699733f6 2596 bfd_vma ent;
90e3cdf2 2597
5ef11c02
AM
2598 if (syms[i]->value > opd->size - 8)
2599 continue;
2600
699733f6
AM
2601 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2602 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2603 {
0ccf57bd 2604 size_t lo, hi;
699733f6 2605 size_t len;
c9727e01 2606 asection *sec = abfd->sections;
90e3cdf2 2607
699733f6
AM
2608 *s = *syms[i];
2609 lo = codesecsym;
2610 hi = codesecsymend;
2611 while (lo < hi)
2612 {
0ccf57bd 2613 size_t mid = (lo + hi) >> 1;
699733f6
AM
2614 if (syms[mid]->section->vma < ent)
2615 lo = mid + 1;
2616 else if (syms[mid]->section->vma > ent)
2617 hi = mid;
2618 else
c9727e01
AM
2619 {
2620 sec = syms[mid]->section;
2621 break;
2622 }
699733f6
AM
2623 }
2624
c9727e01 2625 if (lo >= hi && lo > codesecsym)
699733f6 2626 sec = syms[lo - 1]->section;
699733f6
AM
2627
2628 for (; sec != NULL; sec = sec->next)
2629 {
2630 if (sec->vma > ent)
2631 break;
63524580
JK
2632 /* SEC_LOAD may not be set if SEC is from a separate debug
2633 info file. */
2634 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2635 break;
2636 if ((sec->flags & SEC_CODE) != 0)
2637 s->section = sec;
2638 }
6ba2a415 2639 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2640 s->value = ent - s->section->vma;
2641 s->name = names;
2642 *names++ = '.';
2643 len = strlen (syms[i]->name);
2644 memcpy (names, syms[i]->name, len + 1);
2645 names += len + 1;
6f610d07
UW
2646 /* Have udata.p point back to the original symbol this
2647 synthetic symbol was derived from. */
2648 s->udata.p = syms[i];
699733f6 2649 s++;
90e3cdf2 2650 }
90e3cdf2 2651 }
699733f6 2652 free (contents);
468392fb
AM
2653
2654 if (glink != NULL && relplt != NULL)
2655 {
2656 if (resolv_vma)
2657 {
2658 /* Add a symbol for the main glink trampoline. */
86a4952b 2659 memset (s, 0, sizeof *s);
468392fb 2660 s->the_bfd = abfd;
6ba2a415 2661 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2662 s->section = glink;
2663 s->value = resolv_vma - glink->vma;
2664 s->name = names;
2cdcc330
AM
2665 memcpy (names, "__glink_PLTresolve",
2666 sizeof ("__glink_PLTresolve"));
468392fb
AM
2667 names += sizeof ("__glink_PLTresolve");
2668 s++;
2669 count++;
2670 }
2671
2672 /* FIXME: It would be very much nicer to put sym@plt on the
2673 stub rather than on the glink branch table entry. The
2674 objdump disassembler would then use a sensible symbol
2675 name on plt calls. The difficulty in doing so is
2676 a) finding the stubs, and,
2677 b) matching stubs against plt entries, and,
2678 c) there can be multiple stubs for a given plt entry.
2679
2680 Solving (a) could be done by code scanning, but older
2681 ppc64 binaries used different stubs to current code.
2682 (b) is the tricky one since you need to known the toc
2683 pointer for at least one function that uses a pic stub to
2684 be able to calculate the plt address referenced.
2685 (c) means gdb would need to set multiple breakpoints (or
2686 find the glink branch itself) when setting breakpoints
2687 for pending shared library loads. */
2688 p = relplt->relocation;
2689 for (i = 0; i < plt_count; i++, p++)
2690 {
2691 size_t len;
2692
2693 *s = **p->sym_ptr_ptr;
2694 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2695 we are defining a symbol, ensure one of them is set. */
2696 if ((s->flags & BSF_LOCAL) == 0)
2697 s->flags |= BSF_GLOBAL;
6ba2a415 2698 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2699 s->section = glink;
2700 s->value = glink_vma - glink->vma;
2701 s->name = names;
2702 s->udata.p = NULL;
2703 len = strlen ((*p->sym_ptr_ptr)->name);
2704 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2705 names += len;
e054468f
AM
2706 if (p->addend != 0)
2707 {
2708 memcpy (names, "+0x", sizeof ("+0x") - 1);
2709 names += sizeof ("+0x") - 1;
2710 bfd_sprintf_vma (abfd, names, p->addend);
2711 names += strlen (names);
2712 }
468392fb
AM
2713 memcpy (names, "@plt", sizeof ("@plt"));
2714 names += sizeof ("@plt");
2715 s++;
b9e5796b
AM
2716 if (abi < 2)
2717 {
2718 glink_vma += 8;
2719 if (i >= 0x8000)
2720 glink_vma += 4;
2721 }
2722 else
468392fb
AM
2723 glink_vma += 4;
2724 }
2725 count += plt_count;
2726 }
90e3cdf2
JJ
2727 }
2728
c9727e01 2729 done:
a7535cf3 2730 free (syms);
90e3cdf2
JJ
2731 return count;
2732}
5bd4f169 2733\f
65f38f15
AM
2734/* The following functions are specific to the ELF linker, while
2735 functions above are used generally. Those named ppc64_elf_* are
2736 called by the main ELF linker code. They appear in this file more
2737 or less in the order in which they are called. eg.
2738 ppc64_elf_check_relocs is called early in the link process,
2739 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2740 called.
2741
2742 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2743 functions have both a function code symbol and a function descriptor
2744 symbol. A call to foo in a relocatable object file looks like:
2745
2746 . .text
2747 . x:
2748 . bl .foo
2749 . nop
2750
2751 The function definition in another object file might be:
2752
2753 . .section .opd
2754 . foo: .quad .foo
2755 . .quad .TOC.@tocbase
2756 . .quad 0
2757 .
2758 . .text
2759 . .foo: blr
2760
2761 When the linker resolves the call during a static link, the branch
2762 unsurprisingly just goes to .foo and the .opd information is unused.
2763 If the function definition is in a shared library, things are a little
2764 different: The call goes via a plt call stub, the opd information gets
2765 copied to the plt, and the linker patches the nop.
2766
2767 . x:
2768 . bl .foo_stub
2769 . ld 2,40(1)
2770 .
2771 .
2772 . .foo_stub:
71a39c98
AM
2773 . std 2,40(1) # in practice, the call stub
2774 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2775 . addi 11,11,Lfoo@toc@l # this is the general idea
2776 . ld 12,0(11)
2777 . ld 2,8(11)
2778 . mtctr 12
2779 . ld 11,16(11)
e86ce104
AM
2780 . bctr
2781 .
2782 . .section .plt
2783 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2784
2785 The "reloc ()" notation is supposed to indicate that the linker emits
2786 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2787 copying.
2788
2789 What are the difficulties here? Well, firstly, the relocations
2790 examined by the linker in check_relocs are against the function code
2791 sym .foo, while the dynamic relocation in the plt is emitted against
2792 the function descriptor symbol, foo. Somewhere along the line, we need
2793 to carefully copy dynamic link information from one symbol to the other.
2794 Secondly, the generic part of the elf linker will make .foo a dynamic
2795 symbol as is normal for most other backends. We need foo dynamic
2796 instead, at least for an application final link. However, when
2797 creating a shared library containing foo, we need to have both symbols
2798 dynamic so that references to .foo are satisfied during the early
2799 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2800 definition from some other object, eg. a static library.
2801
2802 Update: As of August 2004, we support a new convention. Function
2803 calls may use the function descriptor symbol, ie. "bl foo". This
2804 behaves exactly as "bl .foo". */
65f38f15 2805
7c8bbca5
AM
2806/* Of those relocs that might be copied as dynamic relocs, this
2807 function selects those that must be copied when linking a shared
2808 library or PIE, even when the symbol is local. */
65f38f15 2809
1d483afe
AM
2810static int
2811must_be_dyn_reloc (struct bfd_link_info *info,
2812 enum elf_ppc64_reloc_type r_type)
2813{
2814 switch (r_type)
2815 {
2816 default:
7c8bbca5
AM
2817 /* Only relative relocs can be resolved when the object load
2818 address isn't fixed. DTPREL64 is excluded because the
2819 dynamic linker needs to differentiate global dynamic from
2820 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2821 return 1;
2822
2823 case R_PPC64_REL32:
2824 case R_PPC64_REL64:
2825 case R_PPC64_REL30:
1bdd8fac
AM
2826 case R_PPC64_TOC16:
2827 case R_PPC64_TOC16_DS:
2828 case R_PPC64_TOC16_LO:
2829 case R_PPC64_TOC16_HI:
2830 case R_PPC64_TOC16_HA:
2831 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2832 return 0;
2833
2834 case R_PPC64_TPREL16:
2835 case R_PPC64_TPREL16_LO:
2836 case R_PPC64_TPREL16_HI:
2837 case R_PPC64_TPREL16_HA:
2838 case R_PPC64_TPREL16_DS:
2839 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2840 case R_PPC64_TPREL16_HIGH:
2841 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2842 case R_PPC64_TPREL16_HIGHER:
2843 case R_PPC64_TPREL16_HIGHERA:
2844 case R_PPC64_TPREL16_HIGHEST:
2845 case R_PPC64_TPREL16_HIGHESTA:
2846 case R_PPC64_TPREL64:
c213164a 2847 case R_PPC64_TPREL34:
7c8bbca5
AM
2848 /* These relocations are relative but in a shared library the
2849 linker doesn't know the thread pointer base. */
2850 return bfd_link_dll (info);
1d483afe
AM
2851 }
2852}
65f38f15 2853
f4656909 2854/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2855 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2856 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2857 shared lib. With code that gcc generates it is vital that this be
2858 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2859 actually the address of a function descriptor which resides in the
2860 .opd section. gcc uses the descriptor directly rather than going
2861 via the GOT as some other ABIs do, which means that initialized
2862 function pointers reference the descriptor. Thus, a function
2863 pointer initialized to the address of a function in a shared
2864 library will either require a .dynbss copy and a copy reloc, or a
2865 dynamic reloc. Using a .dynbss copy redefines the function
2866 descriptor symbol to point to the copy. This presents a problem as
2867 a PLT entry for that function is also initialized from the function
2868 descriptor symbol and the copy may not be initialized first. */
a23b6845 2869#define ELIMINATE_COPY_RELOCS 1
f4656909 2870
721956f4 2871/* Section name for stubs is the associated section name plus this
29942be8
NC
2872 string. */
2873#define STUB_SUFFIX ".stub"
721956f4
AM
2874
2875/* Linker stubs.
2876 ppc_stub_long_branch:
2877 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2878 destination, but a 24 bit branch in a stub section will reach.
2879 . b dest
2880
2881 ppc_stub_plt_branch:
2882 Similar to the above, but a 24 bit branch in the stub section won't
2883 reach its destination.
6df4c9c2
AM
2884 . addis %r12,%r2,xxx@toc@ha
2885 . ld %r12,xxx@toc@l(%r12)
71a39c98 2886 . mtctr %r12
721956f4
AM
2887 . bctr
2888
2889 ppc_stub_plt_call:
2c66dc6c
AM
2890 Used to call a function in a shared library. If it so happens that
2891 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2892 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
7aba54da 2893 An r2save variant starts with "std %r2,40(%r1)".
71a39c98
AM
2894 . addis %r11,%r2,xxx@toc@ha
2895 . ld %r12,xxx+0@toc@l(%r11)
2896 . mtctr %r12
2897 . ld %r2,xxx+8@toc@l(%r11)
2898 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2899 . bctr
ad8e1ba5
AM
2900
2901 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2902 code to adjust the value and save r2 to support multiple toc sections.
2903 A ppc_stub_long_branch with an r2 offset looks like:
2904 . std %r2,40(%r1)
2905 . addis %r2,%r2,off@ha
2906 . addi %r2,%r2,off@l
2907 . b dest
2908
2909 A ppc_stub_plt_branch with an r2 offset looks like:
2910 . std %r2,40(%r1)
6df4c9c2
AM
2911 . addis %r12,%r2,xxx@toc@ha
2912 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2913 . addis %r2,%r2,off@ha
2914 . addi %r2,%r2,off@l
71a39c98 2915 . mtctr %r12
ad8e1ba5 2916 . bctr
ac2df442 2917
05d0e962
AM
2918 All of the above stubs are shown as their ELFv1 variants. ELFv2
2919 variants exist too, simpler for plt calls since a new toc pointer
2920 and static chain are not loaded by the stub. In addition, ELFv2
2921 has some more complex stubs to handle calls marked with NOTOC
7aba54da
AM
2922 relocs from functions where r2 is not a valid toc pointer.
2923 ppc_stub_long_branch_p9notoc:
05d0e962
AM
2924 . mflr %r12
2925 . bcl 20,31,1f
2926 . 1:
2927 . mflr %r11
2928 . mtlr %r12
2cdcc330
AM
2929 . addis %r12,%r11,dest-1b@ha
2930 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2931 . b dest
2932
7aba54da 2933 ppc_stub_plt_branch_p9notoc:
05d0e962
AM
2934 . mflr %r12
2935 . bcl 20,31,1f
2936 . 1:
2937 . mflr %r11
2938 . mtlr %r12
2939 . lis %r12,xxx-1b@highest
f891966f 2940 . ori %r12,%r12,xxx-1b@higher
05d0e962 2941 . sldi %r12,%r12,32
f891966f 2942 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2943 . ori %r12,%r12,xxx-1b@l
2944 . add %r12,%r11,%r12
2945 . mtctr %r12
2946 . bctr
2947
7aba54da 2948 ppc_stub_plt_call_p9notoc:
05d0e962
AM
2949 . mflr %r12
2950 . bcl 20,31,1f
2951 . 1:
2952 . mflr %r11
2953 . mtlr %r12
2954 . lis %r12,xxx-1b@highest
f891966f 2955 . ori %r12,%r12,xxx-1b@higher
05d0e962 2956 . sldi %r12,%r12,32
f891966f 2957 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2958 . ori %r12,%r12,xxx-1b@l
2959 . ldx %r12,%r11,%r12
2960 . mtctr %r12
2961 . bctr
2962
7c1f4227 2963 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2964 ppc_stub_long_branch_notoc:
d4b87b1e 2965 . pla %r12,dest@pcrel
04bdff6a
AM
2966 . b dest
2967 ppc_stub_plt_branch_notoc:
2968 . lis %r11,(dest-1f)@highesta34
2969 . ori %r11,%r11,(dest-1f)@highera34
2970 . sldi %r11,%r11,34
d4b87b1e 2971 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2972 . add %r12,%r11,%r12
2973 . mtctr %r12
2974 . bctr
2975 ppc_stub_plt_call_notoc:
2976 . lis %r11,(xxx-1f)@highesta34
2977 . ori %r11,%r11,(xxx-1f)@highera34
2978 . sldi %r11,%r11,34
d4b87b1e 2979 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2980 . ldx %r12,%r11,%r12
2981 . mtctr %r12
2982 . bctr
2983
05d0e962
AM
2984 In cases where the high instructions would add zero, they are
2985 omitted and following instructions modified in some cases.
7c1f4227 2986 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2987 to
2988 . pld %r12,xxx@pcrel
2989 . mtctr %r12
2990 . bctr
05d0e962 2991
7aba54da 2992 Stub variants may be merged. For example, if printf is called from
05d0e962
AM
2993 code with the tocsave optimization (ie. r2 saved in function
2994 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2995 and from other code without the tocsave optimization requiring a
2996 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2997 type will be created. Calls with the tocsave optimization will
2998 enter this stub after the instruction saving r2. A similar
2999 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3000 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3001 to call an external function like printf. If other calls to printf
3002 require a ppc_stub_plt_call linkage stub then a single
7aba54da
AM
3003 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3004 call. */
3005
3006enum ppc_stub_main_type
2cdcc330 3007{
721956f4
AM
3008 ppc_stub_none,
3009 ppc_stub_long_branch,
3010 ppc_stub_plt_branch,
794e51c0 3011 ppc_stub_plt_call,
a4b6fadd
AM
3012 ppc_stub_global_entry,
3013 ppc_stub_save_res
721956f4
AM
3014};
3015
7aba54da
AM
3016/* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3017 these variations. */
3018
3019enum ppc_stub_sub_type
3020{
3021 ppc_stub_toc,
3022 ppc_stub_notoc,
3023 ppc_stub_p9notoc
3024};
3025
3026struct ppc_stub_type
3027{
3028 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3029 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3030 unsigned int r2save : 1;
3031};
3032
6f20ed8a
AM
3033/* Information on stub grouping. */
3034struct map_stub
3035{
3036 /* The stub section. */
3037 asection *stub_sec;
3038 /* This is the section to which stubs in the group will be attached. */
3039 asection *link_sec;
a4b6fadd
AM
3040 /* Next group. */
3041 struct map_stub *next;
3042 /* Whether to emit a copy of register save/restore functions in this
3043 group. */
3044 int needs_save_res;
df136d64
AM
3045 /* Current offset within stubs after the insn restoring lr in a
3046 _notoc or _both stub using bcl for pc-relative addressing, or
3047 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3048 unsigned int lr_restore;
3049 /* Accumulated size of EH info emitted to describe return address
3050 if stubs modify lr. Does not include 17 byte FDE header. */
3051 unsigned int eh_size;
3052 /* Offset in glink_eh_frame to the start of EH info for this group. */
3053 unsigned int eh_base;
6f20ed8a
AM
3054};
3055
2cdcc330
AM
3056struct ppc_stub_hash_entry
3057{
721956f4
AM
3058 /* Base hash table entry structure. */
3059 struct bfd_hash_entry root;
3060
7aba54da 3061 struct ppc_stub_type type;
ad8e1ba5 3062
6f20ed8a
AM
3063 /* Group information. */
3064 struct map_stub *group;
721956f4
AM
3065
3066 /* Offset within stub_sec of the beginning of this stub. */
3067 bfd_vma stub_offset;
3068
3069 /* Given the symbol's value and its section we can determine its final
3070 value when building the stubs (so the stub knows where to jump. */
3071 bfd_vma target_value;
3072 asection *target_section;
3073
721956f4
AM
3074 /* The symbol table entry, if any, that this was derived from. */
3075 struct ppc_link_hash_entry *h;
e054468f 3076 struct plt_entry *plt_ent;
721956f4 3077
2d7ad24e
AM
3078 /* Symbol type. */
3079 unsigned char symtype;
3080
6911b7dc
AM
3081 /* Symbol st_other. */
3082 unsigned char other;
721956f4
AM
3083};
3084
2cdcc330
AM
3085struct ppc_branch_hash_entry
3086{
721956f4
AM
3087 /* Base hash table entry structure. */
3088 struct bfd_hash_entry root;
3089
c456f082 3090 /* Offset within branch lookup table. */
721956f4
AM
3091 unsigned int offset;
3092
3093 /* Generation marker. */
3094 unsigned int iter;
3095};
65f38f15 3096
1657026c 3097/* Used to track dynamic relocations. */
19e08130
AM
3098struct ppc_dyn_relocs
3099{
3100 struct ppc_dyn_relocs *next;
3101
3102 /* The input section of the reloc. */
3103 asection *sec;
3104
3105 /* Total number of relocs copied for the input section. */
1657026c
AM
3106 unsigned int count;
3107
3108 /* Number of pc-relative relocs copied for the input section. */
3109 unsigned int pc_count;
3110
3111 /* Number of relocs that might become R_PPC64_RELATIVE. */
3112 unsigned int rel_count;
3113};
3114
3115struct ppc_local_dyn_relocs
3116{
3117 struct ppc_local_dyn_relocs *next;
3118
3119 /* The input section of the reloc. */
3120 asection *sec;
3121
3122 /* Total number of relocs copied for the input section. */
3123 unsigned int count;
3124
3125 /* Number of relocs that might become R_PPC64_RELATIVE. */
3126 unsigned int rel_count : 31;
19e08130
AM
3127
3128 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3129 unsigned int ifunc : 1;
3130};
3131
65f38f15
AM
3132struct ppc_link_hash_entry
3133{
3134 struct elf_link_hash_entry elf;
3135
2cdcc330
AM
3136 union
3137 {
b3fac117
AM
3138 /* A pointer to the most recently used stub hash entry against this
3139 symbol. */
3140 struct ppc_stub_hash_entry *stub_cache;
3141
3142 /* A pointer to the next symbol starting with a '.' */
3143 struct ppc_link_hash_entry *next_dot_sym;
3144 } u;
721956f4 3145
721956f4 3146 /* Link between function code and descriptor symbols. */
34814b9f 3147 struct ppc_link_hash_entry *oh;
721956f4 3148
e86ce104
AM
3149 /* Flag function code and descriptor symbols. */
3150 unsigned int is_func:1;
3151 unsigned int is_func_descriptor:1;
908b32fc 3152 unsigned int fake:1;
411e1bfb 3153
c5614fa4
AM
3154 /* Whether global opd/toc sym has been adjusted or not.
3155 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3156 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3157 unsigned int adjust_done:1;
3158
a4b6fadd
AM
3159 /* Set if this is an out-of-line register save/restore function,
3160 with non-standard calling convention. */
3161 unsigned int save_res:1;
3162
8b5f1ed8
AM
3163 /* Set if a duplicate symbol with non-zero localentry is detected,
3164 even when the duplicate symbol does not provide a definition. */
3165 unsigned int non_zero_localentry:1;
3166
411e1bfb 3167 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3168 Bits are or'd into the mask as the corresponding relocs are
3169 encountered during check_relocs, with TLS_TLS being set when any
3170 of the other TLS bits are set. tls_optimize clears bits when
3171 optimizing to indicate the corresponding GOT entry type is not
3172 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3173 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3174 These flags are also kept for local symbols. */
3175#define TLS_TLS 1 /* Any TLS reloc. */
3176#define TLS_GD 2 /* GD reloc. */
3177#define TLS_LD 4 /* LD reloc. */
3178#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3179#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3180#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3181#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3182#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3183 unsigned char tls_mask;
37da22e5
AM
3184
3185 /* The above field is also used to mark function symbols. In which
3186 case TLS_TLS will be 0. */
3187#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3188#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3189#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3190};
3191
ed7007c1
AM
3192static inline struct ppc_link_hash_entry *
3193ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3194{
3195 return (struct ppc_link_hash_entry *) ent;
3196}
3197
30845f11
AM
3198static inline struct elf_link_hash_entry *
3199elf_hash_entry (struct ppc_link_hash_entry *ent)
3200{
3201 return (struct elf_link_hash_entry *) ent;
3202}
3203
65f38f15
AM
3204/* ppc64 ELF linker hash table. */
3205
3206struct ppc_link_hash_table
3207{
3208 struct elf_link_hash_table elf;
3209
721956f4
AM
3210 /* The stub hash table. */
3211 struct bfd_hash_table stub_hash_table;
3212
3213 /* Another hash table for plt_branch stubs. */
3214 struct bfd_hash_table branch_hash_table;
3215
3b421ab3
AM
3216 /* Hash table for function prologue tocsave. */
3217 htab_t tocsave_htab;
3218
e7d1c40c
AM
3219 /* Various options and other info passed from the linker. */
3220 struct ppc64_elf_params *params;
721956f4 3221
6f20ed8a
AM
3222 /* The size of sec_info below. */
3223 unsigned int sec_info_arr_size;
3224
3225 /* Per-section array of extra section info. Done this way rather
3226 than as part of ppc64_elf_section_data so we have the info for
3227 non-ppc64 sections. */
3228 struct
3229 {
3230 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3231 bfd_vma toc_off;
6f20ed8a
AM
3232
3233 union
3234 {
3235 /* The section group that this section belongs to. */
3236 struct map_stub *group;
3237 /* A temp section list pointer. */
3238 asection *list;
3239 } u;
3240 } *sec_info;
721956f4 3241
a4b6fadd
AM
3242 /* Linked list of groups. */
3243 struct map_stub *group;
3244
ad8e1ba5
AM
3245 /* Temp used when calculating TOC pointers. */
3246 bfd_vma toc_curr;
bf102f86
AM
3247 bfd *toc_bfd;
3248 asection *toc_first_sec;
ad8e1ba5 3249
b3fac117
AM
3250 /* Used when adding symbols. */
3251 struct ppc_link_hash_entry *dot_syms;
3252
33e44f2e 3253 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3254 asection *glink;
9e390558 3255 asection *global_entry;
82bd7b59 3256 asection *sfpr;
2d7ad24e
AM
3257 asection *pltlocal;
3258 asection *relpltlocal;
4ce794b7
AM
3259 asection *brlt;
3260 asection *relbrlt;
58d180e8 3261 asection *glink_eh_frame;
ec338859 3262
8387904d
AM
3263 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3264 struct ppc_link_hash_entry *tls_get_addr;
3265 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3266 struct ppc_link_hash_entry *tga_desc;
3267 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3268 struct map_stub *tga_group;
411e1bfb 3269
927be08e
AM
3270 /* The size of reliplt used by got entry relocs. */
3271 bfd_size_type got_reli_size;
3272
1657026c
AM
3273 /* DT_RELR array of r_offset. */
3274 size_t relr_alloc;
3275 size_t relr_count;
3276 bfd_vma *relr_addr;
3277
9b5ecbd0 3278 /* Statistics. */
7aba54da 3279 unsigned long stub_count[ppc_stub_save_res];
9b5ecbd0 3280
ee75fd95
AM
3281 /* Number of stubs against global syms. */
3282 unsigned long stub_globals;
3283
ee67d69a
AM
3284 /* Set if we're linking code with function descriptors. */
3285 unsigned int opd_abi:1;
3286
4c52953f 3287 /* Support for multiple toc sections. */
33c0ec9d 3288 unsigned int do_multi_toc:1;
4c52953f 3289 unsigned int multi_toc_needed:1;
927be08e 3290 unsigned int second_toc_pass:1;
67f0cbdb 3291 unsigned int do_toc_opt:1;
4c52953f 3292
9a23f96e
AM
3293 /* Set if tls optimization is enabled. */
3294 unsigned int do_tls_opt:1;
3295
3e04d765
AM
3296 /* Set if inline plt calls should be converted to direct calls. */
3297 unsigned int can_convert_all_inline_plt:1;
3298
0441f94f
AM
3299 /* Set if a stub_offset changed. */
3300 unsigned int stub_changed:1;
3301
5d1634d7 3302 /* Set on error. */
99877b66 3303 unsigned int stub_error:1;
721956f4 3304
8c5b4e52
AM
3305 /* Whether func_desc_adjust needs to be run over symbols. */
3306 unsigned int need_func_desc_adj:1;
721956f4 3307
f378ab09
AM
3308 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3309 unsigned int has_plt_localentry0:1;
3310
5663e321
AM
3311 /* Whether calls are made via the PLT from NOTOC functions. */
3312 unsigned int notoc_plt:1;
3313
e10a07b3
AM
3314 /* Whether any code linked seems to be Power10. */
3315 unsigned int has_power10_relocs:1;
3316
721956f4
AM
3317 /* Incremented every time we size stubs. */
3318 unsigned int stub_iteration;
0441f94f
AM
3319
3320/* After 20 iterations of stub sizing we no longer allow stubs to
3321 shrink. This is to break out of a pathological case where adding
3322 stubs or increasing their size on one iteration decreases section
3323 gaps (perhaps due to alignment), which then results in smaller
3324 stubs on the next iteration. */
3325#define STUB_SHRINK_ITER 20
65f38f15
AM
3326};
3327
4c52953f
AM
3328/* Rename some of the generic section flags to better document how they
3329 are used here. */
b0dddeec
AM
3330
3331/* Nonzero if this section has TLS related relocations. */
3332#define has_tls_reloc sec_flg0
3333
9737e8af
AM
3334/* Nonzero if this section has a call to __tls_get_addr lacking marker
3335 relocations. */
3336#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3337
3338/* Nonzero if this section has any toc or got relocs. */
3339#define has_toc_reloc sec_flg2
3340
3341/* Nonzero if this section has a call to another section that uses
3342 the toc or got. */
d77c8a4b 3343#define makes_toc_func_call sec_flg3
b0dddeec
AM
3344
3345/* Recursion protection when determining above flag. */
d77c8a4b 3346#define call_check_in_progress sec_flg4
70cc837d 3347#define call_check_done sec_flg5
4c52953f 3348
65f38f15
AM
3349/* Get the ppc64 ELF linker hash table from a link_info structure. */
3350
3351#define ppc_hash_table(p) \
573eb292
AM
3352 ((is_elf_hash_table ((p)->hash) \
3353 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3354 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3355
721956f4
AM
3356#define ppc_stub_hash_lookup(table, string, create, copy) \
3357 ((struct ppc_stub_hash_entry *) \
3358 bfd_hash_lookup ((table), (string), (create), (copy)))
3359
3360#define ppc_branch_hash_lookup(table, string, create, copy) \
3361 ((struct ppc_branch_hash_entry *) \
3362 bfd_hash_lookup ((table), (string), (create), (copy)))
3363
3364/* Create an entry in the stub hash table. */
3365
3366static struct bfd_hash_entry *
4ce794b7
AM
3367stub_hash_newfunc (struct bfd_hash_entry *entry,
3368 struct bfd_hash_table *table,
3369 const char *string)
721956f4
AM
3370{
3371 /* Allocate the structure if it has not already been allocated by a
3372 subclass. */
3373 if (entry == NULL)
3374 {
3375 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3376 if (entry == NULL)
3377 return entry;
3378 }
3379
3380 /* Call the allocation method of the superclass. */
3381 entry = bfd_hash_newfunc (entry, table, string);
3382 if (entry != NULL)
3383 {
3384 struct ppc_stub_hash_entry *eh;
3385
3386 /* Initialize the local fields. */
3387 eh = (struct ppc_stub_hash_entry *) entry;
7aba54da
AM
3388 eh->type.main = ppc_stub_none;
3389 eh->type.sub = ppc_stub_toc;
3390 eh->type.r2save = 0;
6f20ed8a 3391 eh->group = NULL;
721956f4
AM
3392 eh->stub_offset = 0;
3393 eh->target_value = 0;
3394 eh->target_section = NULL;
721956f4 3395 eh->h = NULL;
6911b7dc 3396 eh->plt_ent = NULL;
6911b7dc 3397 eh->other = 0;
721956f4
AM
3398 }
3399
3400 return entry;
3401}
3402
3403/* Create an entry in the branch hash table. */
3404
3405static struct bfd_hash_entry *
4ce794b7
AM
3406branch_hash_newfunc (struct bfd_hash_entry *entry,
3407 struct bfd_hash_table *table,
3408 const char *string)
721956f4
AM
3409{
3410 /* Allocate the structure if it has not already been allocated by a
3411 subclass. */
3412 if (entry == NULL)
3413 {
3414 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3415 if (entry == NULL)
3416 return entry;
3417 }
3418
3419 /* Call the allocation method of the superclass. */
3420 entry = bfd_hash_newfunc (entry, table, string);
3421 if (entry != NULL)
3422 {
3423 struct ppc_branch_hash_entry *eh;
3424
3425 /* Initialize the local fields. */
3426 eh = (struct ppc_branch_hash_entry *) entry;
3427 eh->offset = 0;
3428 eh->iter = 0;
3429 }
3430
3431 return entry;
3432}
3433
65f38f15
AM
3434/* Create an entry in a ppc64 ELF linker hash table. */
3435
3436static struct bfd_hash_entry *
4ce794b7
AM
3437link_hash_newfunc (struct bfd_hash_entry *entry,
3438 struct bfd_hash_table *table,
3439 const char *string)
65f38f15
AM
3440{
3441 /* Allocate the structure if it has not already been allocated by a
3442 subclass. */
3443 if (entry == NULL)
3444 {
3445 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3446 if (entry == NULL)
3447 return entry;
3448 }
3449
3450 /* Call the allocation method of the superclass. */
3451 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3452 if (entry != NULL)
3453 {
3454 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3455
b3fac117 3456 memset (&eh->u.stub_cache, 0,
908b32fc 3457 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3458 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3459
3460 /* When making function calls, old ABI code references function entry
3461 points (dot symbols), while new ABI code references the function
3462 descriptor symbol. We need to make any combination of reference and
3463 definition work together, without breaking archive linking.
3464
3465 For a defined function "foo" and an undefined call to "bar":
3466 An old object defines "foo" and ".foo", references ".bar" (possibly
3467 "bar" too).
3468 A new object defines "foo" and references "bar".
3469
3470 A new object thus has no problem with its undefined symbols being
3471 satisfied by definitions in an old object. On the other hand, the
3472 old object won't have ".bar" satisfied by a new object.
3473
3474 Keep a list of newly added dot-symbols. */
3475
3476 if (string[0] == '.')
3477 {
3478 struct ppc_link_hash_table *htab;
3479
3480 htab = (struct ppc_link_hash_table *) table;
3481 eh->u.next_dot_sym = htab->dot_syms;
3482 htab->dot_syms = eh;
3483 }
65f38f15
AM
3484 }
3485
3486 return entry;
3487}
3488
2cdcc330
AM
3489struct tocsave_entry
3490{
3b421ab3
AM
3491 asection *sec;
3492 bfd_vma offset;
3493};
3494
3495static hashval_t
3496tocsave_htab_hash (const void *p)
3497{
3498 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3499 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3500}
3501
3502static int
3503tocsave_htab_eq (const void *p1, const void *p2)
3504{
3505 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3506 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3507 return e1->sec == e2->sec && e1->offset == e2->offset;
3508}
3509
68faa637
AM
3510/* Destroy a ppc64 ELF linker hash table. */
3511
3512static void
d495ab0d 3513ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3514{
d495ab0d 3515 struct ppc_link_hash_table *htab;
68faa637 3516
d495ab0d 3517 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3518 if (htab->tocsave_htab)
3519 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3520 bfd_hash_table_free (&htab->branch_hash_table);
3521 bfd_hash_table_free (&htab->stub_hash_table);
3522 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3523}
3524
65f38f15
AM
3525/* Create a ppc64 ELF linker hash table. */
3526
3527static struct bfd_link_hash_table *
4ce794b7 3528ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3529{
3530 struct ppc_link_hash_table *htab;
986f0783 3531 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3532
4ce794b7 3533 htab = bfd_zmalloc (amt);
65f38f15
AM
3534 if (htab == NULL)
3535 return NULL;
3536
66eb6687 3537 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3538 sizeof (struct ppc_link_hash_entry),
3539 PPC64_ELF_DATA))
65f38f15 3540 {
e2d34d7d 3541 free (htab);
65f38f15
AM
3542 return NULL;
3543 }
3544
721956f4 3545 /* Init the stub hash table too. */
66eb6687
AM
3546 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3547 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3548 {
d495ab0d 3549 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3550 return NULL;
3551 }
721956f4
AM
3552
3553 /* And the branch hash table. */
66eb6687
AM
3554 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3555 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3556 {
3557 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3558 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3559 return NULL;
3560 }
721956f4 3561
3b421ab3
AM
3562 htab->tocsave_htab = htab_try_create (1024,
3563 tocsave_htab_hash,
3564 tocsave_htab_eq,
3565 NULL);
3566 if (htab->tocsave_htab == NULL)
2915c55b 3567 {
d495ab0d 3568 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3569 return NULL;
3570 }
d495ab0d 3571 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3572
3254fd24
AM
3573 /* Initializing two fields of the union is just cosmetic. We really
3574 only care about glist, but when compiled on a 32-bit host the
3575 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3576 debugger inspection of these fields look nicer. */
a6aa5195
AM
3577 htab->elf.init_got_refcount.refcount = 0;
3578 htab->elf.init_got_refcount.glist = NULL;
3579 htab->elf.init_plt_refcount.refcount = 0;
3580 htab->elf.init_plt_refcount.glist = NULL;
3581 htab->elf.init_got_offset.offset = 0;
3582 htab->elf.init_got_offset.glist = NULL;
3583 htab->elf.init_plt_offset.offset = 0;
3584 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3585
65f38f15
AM
3586 return &htab->elf.root;
3587}
3588
bfeb4a28
AM
3589/* Create sections for linker generated code. */
3590
0a1b45a2 3591static bool
bfeb4a28
AM
3592create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3593{
3594 struct ppc_link_hash_table *htab;
3595 flagword flags;
3596
3597 htab = ppc_hash_table (info);
3598
bfeb4a28
AM
3599 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3600 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3601 if (htab->params->save_restore_funcs)
3602 {
3603 /* Create .sfpr for code to save and restore fp regs. */
3604 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3605 flags);
3606 if (htab->sfpr == NULL
fd361982 3607 || !bfd_set_section_alignment (htab->sfpr, 2))
0a1b45a2 3608 return false;
b32547cd
AM
3609 }
3610
3611 if (bfd_link_relocatable (info))
0a1b45a2 3612 return true;
bfeb4a28
AM
3613
3614 /* Create .glink for lazy dynamic linking support. */
3615 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3616 flags);
3617 if (htab->glink == NULL
fd361982 3618 || !bfd_set_section_alignment (htab->glink, 3))
0a1b45a2 3619 return false;
bfeb4a28 3620
9e390558
AM
3621 /* The part of .glink used by global entry stubs, separate so that
3622 it can be aligned appropriately without affecting htab->glink. */
3623 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3624 flags);
3625 if (htab->global_entry == NULL
fd361982 3626 || !bfd_set_section_alignment (htab->global_entry, 2))
0a1b45a2 3627 return false;
9e390558 3628
bfeb4a28
AM
3629 if (!info->no_ld_generated_unwind_info)
3630 {
3631 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3632 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3633 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3634 ".eh_frame",
3635 flags);
3636 if (htab->glink_eh_frame == NULL
fd361982 3637 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
0a1b45a2 3638 return false;
bfeb4a28
AM
3639 }
3640
3641 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3642 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3643 if (htab->elf.iplt == NULL
fd361982 3644 || !bfd_set_section_alignment (htab->elf.iplt, 3))
0a1b45a2 3645 return false;
bfeb4a28
AM
3646
3647 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3648 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3649 htab->elf.irelplt
3650 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3651 if (htab->elf.irelplt == NULL
fd361982 3652 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
0a1b45a2 3653 return false;
bfeb4a28
AM
3654
3655 /* Create branch lookup table for plt_branch stubs. */
3656 flags = (SEC_ALLOC | SEC_LOAD
3657 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3658 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3659 flags);
3660 if (htab->brlt == NULL
fd361982 3661 || !bfd_set_section_alignment (htab->brlt, 3))
0a1b45a2 3662 return false;
bfeb4a28 3663
2d7ad24e
AM
3664 /* Local plt entries, put in .branch_lt but a separate section for
3665 convenience. */
3666 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3667 flags);
3668 if (htab->pltlocal == NULL
fd361982 3669 || !bfd_set_section_alignment (htab->pltlocal, 3))
0a1b45a2 3670 return false;
2d7ad24e 3671
0e1862bb 3672 if (!bfd_link_pic (info))
0a1b45a2 3673 return true;
bfeb4a28
AM
3674
3675 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3676 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3677 htab->relbrlt
3678 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3679 if (htab->relbrlt == NULL
fd361982 3680 || !bfd_set_section_alignment (htab->relbrlt, 3))
0a1b45a2 3681 return false;
bfeb4a28 3682
2d7ad24e
AM
3683 htab->relpltlocal
3684 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3685 if (htab->relpltlocal == NULL
fd361982 3686 || !bfd_set_section_alignment (htab->relpltlocal, 3))
0a1b45a2 3687 return false;
2d7ad24e 3688
0a1b45a2 3689 return true;
bfeb4a28
AM
3690}
3691
e717da7e
AM
3692/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3693
0a1b45a2 3694bool
e7d1c40c
AM
3695ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3696 struct ppc64_elf_params *params)
e717da7e
AM
3697{
3698 struct ppc_link_hash_table *htab;
3699
e7d1c40c 3700 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3701
3702/* Always hook our dynamic sections into the first bfd, which is the
3703 linker created stub bfd. This ensures that the GOT header is at
3704 the start of the output TOC section. */
3705 htab = ppc_hash_table (info);
e7d1c40c
AM
3706 htab->elf.dynobj = params->stub_bfd;
3707 htab->params = params;
bfeb4a28 3708
bfeb4a28 3709 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3710}
3711
721956f4
AM
3712/* Build a name for an entry in the stub hash table. */
3713
3714static char *
4ce794b7
AM
3715ppc_stub_name (const asection *input_section,
3716 const asection *sym_sec,
3717 const struct ppc_link_hash_entry *h,
3718 const Elf_Internal_Rela *rel)
721956f4
AM
3719{
3720 char *stub_name;
bcaa2f82 3721 ssize_t len;
721956f4
AM
3722
3723 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3724 offsets from a sym as a branch target? In fact, we could
3725 probably assume the addend is always zero. */
3726 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3727
3728 if (h)
3729 {
3730 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3731 stub_name = bfd_malloc (len);
46de2a7c
AM
3732 if (stub_name == NULL)
3733 return stub_name;
3734
bcaa2f82
AM
3735 len = sprintf (stub_name, "%08x.%s+%x",
3736 input_section->id & 0xffffffff,
3737 h->elf.root.root.string,
3738 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3739 }
3740 else
3741 {
ad8e1ba5 3742 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3743 stub_name = bfd_malloc (len);
46de2a7c
AM
3744 if (stub_name == NULL)
3745 return stub_name;
3746
bcaa2f82
AM
3747 len = sprintf (stub_name, "%08x.%x:%x+%x",
3748 input_section->id & 0xffffffff,
3749 sym_sec->id & 0xffffffff,
3750 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3751 (int) rel->r_addend & 0xffffffff);
721956f4 3752 }
bcaa2f82 3753 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3754 stub_name[len - 2] = 0;
721956f4
AM
3755 return stub_name;
3756}
3757
e10a07b3 3758/* If mixing power10 with non-power10 code and --power10-stubs is not
7aba54da
AM
3759 specified (or is auto) then there may be multiple stub types for any
3760 given symbol. Up to three classes of stubs are stored in separate
3761 stub_hash_table entries having the same key string. The entries
3762 will always be adjacent on entry->root.next chain, even if hash
3763 table resizing occurs. This function selects the correct entry to
3764 use. */
e10a07b3
AM
3765
3766static struct ppc_stub_hash_entry *
7aba54da
AM
3767select_alt_stub (struct ppc_stub_hash_entry *entry,
3768 enum elf_ppc64_reloc_type r_type)
e10a07b3 3769{
7aba54da 3770 enum ppc_stub_sub_type subt;
e10a07b3 3771
7aba54da
AM
3772 switch (r_type)
3773 {
3774 case R_PPC64_REL24_NOTOC:
3775 subt = ppc_stub_notoc;
3776 break;
3777 case R_PPC64_REL24_P9NOTOC:
3778 subt = ppc_stub_p9notoc;
3779 break;
3780 default:
3781 subt = ppc_stub_toc;
3782 break;
3783 }
e10a07b3 3784
7aba54da 3785 while (entry != NULL && entry->type.sub != subt)
e10a07b3
AM
3786 {
3787 const char *stub_name = entry->root.string;
3788
3789 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3790 if (entry != NULL
3791 && entry->root.string != stub_name)
3792 entry = NULL;
3793 }
3794
3795 return entry;
3796}
3797
721956f4
AM
3798/* Look up an entry in the stub hash. Stub entries are cached because
3799 creating the stub name takes a bit of time. */
3800
3801static struct ppc_stub_hash_entry *
4ce794b7
AM
3802ppc_get_stub_entry (const asection *input_section,
3803 const asection *sym_sec,
039b3fef 3804 struct ppc_link_hash_entry *h,
4ce794b7
AM
3805 const Elf_Internal_Rela *rel,
3806 struct ppc_link_hash_table *htab)
721956f4
AM
3807{
3808 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3809 struct map_stub *group;
721956f4
AM
3810
3811 /* If this input section is part of a group of sections sharing one
3812 stub section, then use the id of the first section in the group.
3813 Stub names need to include a section id, as there may well be
3814 more than one stub used to reach say, printf, and we need to
3815 distinguish between them. */
6f20ed8a 3816 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3817 if (group == NULL)
3818 return NULL;
721956f4 3819
b3fac117
AM
3820 if (h != NULL && h->u.stub_cache != NULL
3821 && h->u.stub_cache->h == h
6f20ed8a 3822 && h->u.stub_cache->group == group)
721956f4 3823 {
b3fac117 3824 stub_entry = h->u.stub_cache;
721956f4
AM
3825 }
3826 else
3827 {
3828 char *stub_name;
3829
6f20ed8a 3830 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3831 if (stub_name == NULL)
3832 return NULL;
3833
3834 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 3835 stub_name, false, false);
721956f4 3836 if (h != NULL)
b3fac117 3837 h->u.stub_cache = stub_entry;
721956f4
AM
3838
3839 free (stub_name);
3840 }
3841
e10a07b3 3842 if (stub_entry != NULL && htab->params->power10_stubs == -1)
7aba54da 3843 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
e10a07b3 3844
721956f4
AM
3845 return stub_entry;
3846}
3847
3848/* Add a new stub entry to the stub hash. Not all fields of the new
3849 stub entry are initialised. */
3850
3851static struct ppc_stub_hash_entry *
4ce794b7
AM
3852ppc_add_stub (const char *stub_name,
3853 asection *section,
25f53a85 3854 struct bfd_link_info *info)
721956f4 3855{
25f53a85 3856 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3857 struct map_stub *group;
721956f4
AM
3858 asection *link_sec;
3859 asection *stub_sec;
3860 struct ppc_stub_hash_entry *stub_entry;
3861
6f20ed8a
AM
3862 group = htab->sec_info[section->id].u.group;
3863 link_sec = group->link_sec;
3864 stub_sec = group->stub_sec;
721956f4
AM
3865 if (stub_sec == NULL)
3866 {
6f20ed8a
AM
3867 size_t namelen;
3868 bfd_size_type len;
3869 char *s_name;
721956f4 3870
6f20ed8a
AM
3871 namelen = strlen (link_sec->name);
3872 len = namelen + sizeof (STUB_SUFFIX);
3873 s_name = bfd_alloc (htab->params->stub_bfd, len);
3874 if (s_name == NULL)
3875 return NULL;
721956f4 3876
6f20ed8a
AM
3877 memcpy (s_name, link_sec->name, namelen);
3878 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3879 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3880 if (stub_sec == NULL)
3881 return NULL;
3882 group->stub_sec = stub_sec;
721956f4
AM
3883 }
3884
3885 /* Enter this entry into the linker stub hash table. */
3886 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
0a1b45a2 3887 true, false);
721956f4
AM
3888 if (stub_entry == NULL)
3889 {
695344c0 3890 /* xgettext:c-format */
cf97bcb0
AM
3891 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3892 section->owner, stub_name);
721956f4
AM
3893 return NULL;
3894 }
3895
6f20ed8a 3896 stub_entry->group = group;
721956f4 3897 stub_entry->stub_offset = 0;
721956f4
AM
3898 return stub_entry;
3899}
3900
7aba54da
AM
3901/* A stub has already been created, but it may not be the required
3902 type. We shouldn't be transitioning from plt_call to long_branch
3903 stubs or vice versa, but we might be upgrading from plt_call to
3904 plt_call with r2save for example. */
3905
3906static bool
3907ppc_merge_stub (struct ppc_link_hash_table *htab,
3908 struct ppc_stub_hash_entry *stub_entry,
3909 struct ppc_stub_type stub_type,
3910 enum elf_ppc64_reloc_type r_type)
3911{
3912 struct ppc_stub_type old_type = stub_entry->type;
3913
3914 if (old_type.main == ppc_stub_save_res)
3915 return true;
3916
3917 if (htab->params->power10_stubs == -1)
3918 {
3919 /* For --power10-stubs=auto, don't merge _notoc and other
3920 varieties of stubs. */
3921 struct ppc_stub_hash_entry *alt_stub;
3922
3923 alt_stub = select_alt_stub (stub_entry, r_type);
3924 if (alt_stub == NULL)
3925 {
3926 alt_stub = ((struct ppc_stub_hash_entry *)
3927 stub_hash_newfunc (NULL,
3928 &htab->stub_hash_table,
3929 stub_entry->root.string));
3930 if (alt_stub == NULL)
3931 return false;
3932
3933 *alt_stub = *stub_entry;
3934 stub_entry->root.next = &alt_stub->root;
3935
3936 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3937 Not that it matters, this just puts smaller stubs first. */
3938 if (stub_type.sub == ppc_stub_notoc)
3939 alt_stub = stub_entry;
3940 else if (stub_type.sub == ppc_stub_p9notoc
3941 && alt_stub->root.next
3942 && alt_stub->root.next->string == alt_stub->root.string)
3943 {
3944 struct ppc_stub_hash_entry *next
3945 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3946 alt_stub->type = next->type;
3947 alt_stub = next;
3948 }
3949 alt_stub->type = stub_type;
3950 return true;
3951 }
3952 stub_entry = alt_stub;
3953 }
3954
3955 old_type = stub_entry->type;
3956 if (old_type.main == ppc_stub_plt_branch)
3957 old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
3958
3959 if (old_type.main != stub_type.main
3960 || (old_type.sub != stub_type.sub
3961 && old_type.sub != ppc_stub_toc
3962 && stub_type.sub != ppc_stub_toc))
3963 abort ();
3964
3965 stub_entry->type.sub |= stub_type.sub;
3966 stub_entry->type.r2save |= stub_type.r2save;
3967 return true;
3968}
3969
e717da7e
AM
3970/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3971 not already done. */
65f38f15 3972
0a1b45a2 3973static bool
e717da7e 3974create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3975{
e717da7e
AM
3976 asection *got, *relgot;
3977 flagword flags;
3978 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3979
0c8d6e5c 3980 if (!is_ppc64_elf (abfd))
0a1b45a2 3981 return false;
4dfe6ac6 3982 if (htab == NULL)
0a1b45a2 3983 return false;
0ffa91dd 3984
33e44f2e
AM
3985 if (!htab->elf.sgot
3986 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
0a1b45a2 3987 return false;
e717da7e
AM
3988
3989 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3990 | SEC_LINKER_CREATED);
3991
c456f082 3992 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3993 if (!got
fd361982 3994 || !bfd_set_section_alignment (got, 3))
0a1b45a2 3995 return false;
65f38f15 3996
c456f082
AM
3997 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3998 flags | SEC_READONLY);
e717da7e 3999 if (!relgot
fd361982 4000 || !bfd_set_section_alignment (relgot, 3))
0a1b45a2 4001 return false;
e717da7e
AM
4002
4003 ppc64_elf_tdata (abfd)->got = got;
4004 ppc64_elf_tdata (abfd)->relgot = relgot;
0a1b45a2 4005 return true;
65f38f15 4006}
5bd4f169 4007
b31867b6
AM
4008/* Follow indirect and warning symbol links. */
4009
4010static inline struct bfd_link_hash_entry *
4011follow_link (struct bfd_link_hash_entry *h)
4012{
4013 while (h->type == bfd_link_hash_indirect
4014 || h->type == bfd_link_hash_warning)
4015 h = h->u.i.link;
4016 return h;
4017}
4018
4019static inline struct elf_link_hash_entry *
4020elf_follow_link (struct elf_link_hash_entry *h)
4021{
4022 return (struct elf_link_hash_entry *) follow_link (&h->root);
4023}
4024
4025static inline struct ppc_link_hash_entry *
4026ppc_follow_link (struct ppc_link_hash_entry *h)
4027{
ed7007c1 4028 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
4029}
4030
40d16e0b
AM
4031/* Merge PLT info on FROM with that on TO. */
4032
4033static void
4034move_plt_plist (struct ppc_link_hash_entry *from,
4035 struct ppc_link_hash_entry *to)
4036{
4037 if (from->elf.plt.plist != NULL)
4038 {
4039 if (to->elf.plt.plist != NULL)
4040 {
4041 struct plt_entry **entp;
4042 struct plt_entry *ent;
4043
4044 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4045 {
4046 struct plt_entry *dent;
4047
4048 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4049 if (dent->addend == ent->addend)
4050 {
4051 dent->plt.refcount += ent->plt.refcount;
4052 *entp = ent->next;
4053 break;
4054 }
4055 if (dent == NULL)
4056 entp = &ent->next;
4057 }
4058 *entp = to->elf.plt.plist;
4059 }
4060
4061 to->elf.plt.plist = from->elf.plt.plist;
4062 from->elf.plt.plist = NULL;
4063 }
4064}
4065
65f38f15
AM
4066/* Copy the extra info we tack onto an elf_link_hash_entry. */
4067
4068static void
fcfa13d2
AM
4069ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4070 struct elf_link_hash_entry *dir,
4071 struct elf_link_hash_entry *ind)
65f38f15
AM
4072{
4073 struct ppc_link_hash_entry *edir, *eind;
4074
ed7007c1
AM
4075 edir = ppc_elf_hash_entry (dir);
4076 eind = ppc_elf_hash_entry (ind);
65f38f15 4077
c79d6685
AM
4078 edir->is_func |= eind->is_func;
4079 edir->is_func_descriptor |= eind->is_func_descriptor;
4080 edir->tls_mask |= eind->tls_mask;
4081 if (eind->oh != NULL)
4082 edir->oh = ppc_follow_link (eind->oh);
4083
474436e6 4084 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
4085 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4086 edir->elf.ref_regular |= eind->elf.ref_regular;
4087 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 4088 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
4089 edir->elf.needs_plt |= eind->elf.needs_plt;
4090 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 4091
d311bc8b
AM
4092 /* If we were called to copy over info for a weak sym, don't copy
4093 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4094 in order to simplify readonly_dynrelocs and save a field in the
4095 symbol hash entry, but that means dyn_relocs can't be used in any
4096 tests about a specific symbol, or affect other symbol flags which
ab2477e1 4097 are then tested. */
d311bc8b 4098 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 4099 return;
d311bc8b 4100
411e1bfb 4101 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 4102 if (ind->dyn_relocs != NULL)
65f38f15 4103 {
190eb1dd 4104 if (dir->dyn_relocs != NULL)
bbd7ec4a 4105 {
1657026c
AM
4106 struct ppc_dyn_relocs **pp;
4107 struct ppc_dyn_relocs *p;
bbd7ec4a 4108
fcfa13d2 4109 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 4110 list. Merge any entries against the same section. */
1657026c
AM
4111 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4112 (p = *pp) != NULL;
4113 )
bbd7ec4a 4114 {
1657026c 4115 struct ppc_dyn_relocs *q;
bbd7ec4a 4116
1657026c
AM
4117 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4118 q != NULL;
4119 q = q->next)
bbd7ec4a
AM
4120 if (q->sec == p->sec)
4121 {
bbd7ec4a 4122 q->count += p->count;
1657026c
AM
4123 q->pc_count += p->pc_count;
4124 q->rel_count += p->rel_count;
bbd7ec4a
AM
4125 *pp = p->next;
4126 break;
4127 }
4128 if (q == NULL)
4129 pp = &p->next;
4130 }
1657026c 4131 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
bbd7ec4a
AM
4132 }
4133
190eb1dd
L
4134 dir->dyn_relocs = ind->dyn_relocs;
4135 ind->dyn_relocs = NULL;
65f38f15 4136 }
65f38f15 4137
81848ca0
AM
4138 /* Copy over got entries that we may have already seen to the
4139 symbol which just became indirect. */
411e1bfb
AM
4140 if (eind->elf.got.glist != NULL)
4141 {
4142 if (edir->elf.got.glist != NULL)
4143 {
4144 struct got_entry **entp;
4145 struct got_entry *ent;
4146
4147 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4148 {
4149 struct got_entry *dent;
4150
4151 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4152 if (dent->addend == ent->addend
e717da7e 4153 && dent->owner == ent->owner
411e1bfb
AM
4154 && dent->tls_type == ent->tls_type)
4155 {
4156 dent->got.refcount += ent->got.refcount;
4157 *entp = ent->next;
4158 break;
4159 }
4160 if (dent == NULL)
4161 entp = &ent->next;
4162 }
4163 *entp = edir->elf.got.glist;
4164 }
4165
4166 edir->elf.got.glist = eind->elf.got.glist;
4167 eind->elf.got.glist = NULL;
4168 }
4169
4170 /* And plt entries. */
40d16e0b 4171 move_plt_plist (eind, edir);
411e1bfb 4172
fcfa13d2 4173 if (eind->elf.dynindx != -1)
411e1bfb 4174 {
fcfa13d2
AM
4175 if (edir->elf.dynindx != -1)
4176 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4177 edir->elf.dynstr_index);
411e1bfb
AM
4178 edir->elf.dynindx = eind->elf.dynindx;
4179 edir->elf.dynstr_index = eind->elf.dynstr_index;
4180 eind->elf.dynindx = -1;
4181 eind->elf.dynstr_index = 0;
4182 }
411e1bfb
AM
4183}
4184
8387904d
AM
4185/* Find the function descriptor hash entry from the given function code
4186 hash entry FH. Link the entries via their OH fields. */
4187
4188static struct ppc_link_hash_entry *
b31867b6 4189lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4190{
4191 struct ppc_link_hash_entry *fdh = fh->oh;
4192
4193 if (fdh == NULL)
4194 {
4195 const char *fd_name = fh->elf.root.root.string + 1;
4196
ed7007c1 4197 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
0a1b45a2 4198 false, false, false));
b31867b6
AM
4199 if (fdh == NULL)
4200 return fdh;
4201
4202 fdh->is_func_descriptor = 1;
4203 fdh->oh = fh;
4204 fh->is_func = 1;
4205 fh->oh = fdh;
8387904d
AM
4206 }
4207
8c5b4e52
AM
4208 fdh = ppc_follow_link (fdh);
4209 fdh->is_func_descriptor = 1;
4210 fdh->oh = fh;
4211 return fdh;
8387904d
AM
4212}
4213
8c5b4e52 4214/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4215
4216static struct ppc_link_hash_entry *
4217make_fdh (struct bfd_link_info *info,
908b32fc 4218 struct ppc_link_hash_entry *fh)
bb700d78 4219{
8c5b4e52
AM
4220 bfd *abfd = fh->elf.root.u.undef.abfd;
4221 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4222 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4223 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4224 ? BSF_WEAK
4225 : BSF_GLOBAL);
4226
4227 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4228 fh->elf.root.root.string + 1,
4229 flags, bfd_und_section_ptr, 0,
0a1b45a2 4230 NULL, false, false, &bh))
bb700d78
AM
4231 return NULL;
4232
4233 fdh = (struct ppc_link_hash_entry *) bh;
4234 fdh->elf.non_elf = 0;
908b32fc
AM
4235 fdh->fake = 1;
4236 fdh->is_func_descriptor = 1;
4237 fdh->oh = fh;
4238 fh->is_func = 1;
4239 fh->oh = fdh;
bb700d78
AM
4240 return fdh;
4241}
4242
8387904d
AM
4243/* Fix function descriptor symbols defined in .opd sections to be
4244 function type. */
555cd476 4245
0a1b45a2 4246static bool
c16153ae 4247ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4248 struct bfd_link_info *info,
555cd476 4249 Elf_Internal_Sym *isym,
6911b7dc 4250 const char **name,
555cd476
AM
4251 flagword *flags ATTRIBUTE_UNUSED,
4252 asection **sec,
b53dfeb2 4253 bfd_vma *value)
555cd476 4254{
b53dfeb2 4255 if (*sec != NULL
f1885d1e 4256 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4257 {
4258 asection *code_sec;
4259
4260 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4261 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4262 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4263
4264 /* If the symbol is a function defined in .opd, and the function
4265 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4266 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4267 && (*sec)->reloc_count != 0
4268 && opd_entry_value (*sec, *value, &code_sec, NULL,
0a1b45a2 4269 false) != (bfd_vma) -1
b53dfeb2
AM
4270 && discarded_section (code_sec))
4271 {
4272 *sec = bfd_und_section_ptr;
4273 isym->st_shndx = SHN_UNDEF;
4274 }
4275 }
dbd1e97e
AM
4276 else if (*sec != NULL
4277 && strcmp ((*sec)->name, ".toc") == 0
4278 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4279 {
4280 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4281 if (htab != NULL)
4282 htab->params->object_in_toc = 1;
4283 }
433817dd 4284
6911b7dc
AM
4285 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4286 {
4287 if (abiversion (ibfd) == 0)
4288 set_abiversion (ibfd, 2);
4289 else if (abiversion (ibfd) == 1)
4290 {
cf97bcb0
AM
4291 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4292 " for ABI version 1"), *name);
6911b7dc 4293 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4294 return false;
6911b7dc
AM
4295 }
4296 }
4297
0a1b45a2 4298 return true;
555cd476
AM
4299}
4300
6911b7dc
AM
4301/* Merge non-visibility st_other attributes: local entry point. */
4302
4303static void
4304ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4305 unsigned int st_other,
0a1b45a2
AM
4306 bool definition,
4307 bool dynamic)
6911b7dc 4308{
f378ab09 4309 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4310 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4311 | ELF_ST_VISIBILITY (h->other));
4312}
4313
8c5b4e52
AM
4314/* Hook called on merging a symbol. We use this to clear "fake" since
4315 we now have a real symbol. */
4316
0a1b45a2 4317static bool
8c5b4e52 4318ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4319 const Elf_Internal_Sym *isym,
8c5b4e52 4320 asection **psec ATTRIBUTE_UNUSED,
0a1b45a2
AM
4321 bool newdef ATTRIBUTE_UNUSED,
4322 bool olddef ATTRIBUTE_UNUSED,
8c5b4e52
AM
4323 bfd *oldbfd ATTRIBUTE_UNUSED,
4324 const asection *oldsec ATTRIBUTE_UNUSED)
4325{
ed7007c1 4326 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4327 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4328 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
0a1b45a2 4329 return true;
8c5b4e52
AM
4330}
4331
8387904d 4332/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4333 inclusion of a new ABI object archive that defines "bar".
4334 NAME is a symbol defined in an archive. Return a symbol in the hash
4335 table that might be satisfied by the archive symbols. */
8387904d 4336
b585e899 4337static struct bfd_link_hash_entry *
8387904d
AM
4338ppc64_elf_archive_symbol_lookup (bfd *abfd,
4339 struct bfd_link_info *info,
4340 const char *name)
4341{
b585e899 4342 struct bfd_link_hash_entry *h;
8387904d
AM
4343 char *dot_name;
4344 size_t len;
4345
4346 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc 4347 if (h != NULL
b585e899 4348 && ppc_hash_table (info) != NULL
908b32fc
AM
4349 /* Don't return this sym if it is a fake function descriptor
4350 created by add_symbol_adjust. */
b585e899 4351 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4352 return h;
4353
4354 if (name[0] == '.')
4355 return h;
4356
4357 len = strlen (name);
4358 dot_name = bfd_alloc (abfd, len + 2);
4359 if (dot_name == NULL)
b585e899 4360 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
4361 dot_name[0] = '.';
4362 memcpy (dot_name + 1, name, len + 1);
4363 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4364 bfd_release (abfd, dot_name);
a804e476
AM
4365 if (h != NULL)
4366 return h;
4367
4368 if (strcmp (name, "__tls_get_addr_opt") == 0)
4369 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4370 return h;
4371}
4372
4373/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4374 new ABI object defines "bar". Well, at least, undefined dot symbols
4375 are made weak. This stops later archive searches from including an
4376 object if we already have a function descriptor definition. It also
35b0ce59
AM
4377 prevents the linker complaining about undefined symbols.
4378 We also check and correct mismatched symbol visibility here. The
4379 most restrictive visibility of the function descriptor and the
4380 function entry symbol is used. */
8387904d 4381
0a1b45a2 4382static bool
b3fac117 4383add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4384{
8387904d
AM
4385 struct ppc_link_hash_table *htab;
4386 struct ppc_link_hash_entry *fdh;
4387
b3fac117
AM
4388 if (eh->elf.root.type == bfd_link_hash_warning)
4389 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4390
8400d40d 4391 if (eh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 4392 return true;
8400d40d 4393
b3fac117
AM
4394 if (eh->elf.root.root.string[0] != '.')
4395 abort ();
8387904d 4396
b3fac117 4397 htab = ppc_hash_table (info);
4dfe6ac6 4398 if (htab == NULL)
0a1b45a2 4399 return false;
4dfe6ac6 4400
b31867b6 4401 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4402 if (fdh == NULL
4403 && !bfd_link_relocatable (info)
4404 && (eh->elf.root.type == bfd_link_hash_undefined
4405 || eh->elf.root.type == bfd_link_hash_undefweak)
4406 && eh->elf.ref_regular)
4407 {
4408 /* Make an undefined function descriptor sym, in order to
4409 pull in an --as-needed shared lib. Archives are handled
4410 elsewhere. */
4411 fdh = make_fdh (info, eh);
4412 if (fdh == NULL)
0a1b45a2 4413 return false;
bb700d78 4414 }
8c5b4e52
AM
4415
4416 if (fdh != NULL)
8387904d 4417 {
35b0ce59
AM
4418 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4419 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4420
4421 /* Make both descriptor and entry symbol have the most
4422 constraining visibility of either symbol. */
35b0ce59
AM
4423 if (entry_vis < descr_vis)
4424 fdh->elf.other += entry_vis - descr_vis;
4425 else if (entry_vis > descr_vis)
4426 eh->elf.other += descr_vis - entry_vis;
4427
8c5b4e52
AM
4428 /* Propagate reference flags from entry symbol to function
4429 descriptor symbol. */
bc4e12de 4430 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4431 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4432 fdh->elf.ref_regular |= eh->elf.ref_regular;
4433 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4434
4435 if (!fdh->elf.forced_local
4436 && fdh->elf.dynindx == -1
4437 && fdh->elf.versioned != versioned_hidden
4438 && (bfd_link_dll (info)
4439 || fdh->elf.def_dynamic
4440 || fdh->elf.ref_dynamic)
4441 && (eh->elf.ref_regular
4442 || eh->elf.def_regular))
4443 {
2cdcc330 4444 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 4445 return false;
35b0ce59 4446 }
8387904d 4447 }
99877b66 4448
0a1b45a2 4449 return true;
8387904d
AM
4450}
4451
f6c7c3e8
AM
4452/* Set up opd section info and abiversion for IBFD, and process list
4453 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4454
0a1b45a2 4455static bool
f6c7c3e8 4456ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4457{
99877b66 4458 struct ppc_link_hash_table *htab;
b3fac117 4459 struct ppc_link_hash_entry **p, *eh;
459609d6 4460 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4461
459609d6 4462 if (opd != NULL && opd->size != 0)
b3fac117 4463 {
b9399fcf
AM
4464 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4465 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4466
459609d6
AM
4467 if (abiversion (ibfd) == 0)
4468 set_abiversion (ibfd, 1);
8a2058b5 4469 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4470 {
695344c0 4471 /* xgettext:c-format */
cf97bcb0
AM
4472 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4473 ibfd, abiversion (ibfd));
459609d6 4474 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4475 return false;
f6c7c3e8 4476 }
b9399fcf 4477 }
f6c7c3e8 4478
b9399fcf
AM
4479 if (is_ppc64_elf (info->output_bfd))
4480 {
4481 /* For input files without an explicit abiversion in e_flags
4482 we should have flagged any with symbol st_other bits set
4483 as ELFv1 and above flagged those with .opd as ELFv2.
4484 Set the output abiversion if not yet set, and for any input
4485 still ambiguous, take its abiversion from the output.
4486 Differences in ABI are reported later. */
4487 if (abiversion (info->output_bfd) == 0)
4488 set_abiversion (info->output_bfd, abiversion (ibfd));
4489 else if (abiversion (ibfd) == 0)
4490 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4491 }
4492
459609d6
AM
4493 htab = ppc_hash_table (info);
4494 if (htab == NULL)
0a1b45a2 4495 return true;
459609d6 4496
b9399fcf
AM
4497 if (opd != NULL && opd->size != 0
4498 && (ibfd->flags & DYNAMIC) == 0
4499 && (opd->flags & SEC_RELOC) != 0
4500 && opd->reloc_count != 0
4501 && !bfd_is_abs_section (opd->output_section)
4502 && info->gc_sections)
4503 {
4504 /* Garbage collection needs some extra help with .opd sections.
4505 We don't want to necessarily keep everything referenced by
4506 relocs in .opd, as that would keep all functions. Instead,
4507 if we reference an .opd symbol (a function descriptor), we
4508 want to keep the function code symbol's section. This is
4509 easy for global symbols, but for local syms we need to keep
4510 information about the associated function section. */
4511 bfd_size_type amt;
4512 asection **opd_sym_map;
4513 Elf_Internal_Shdr *symtab_hdr;
4514 Elf_Internal_Rela *relocs, *rel_end, *rel;
4515
4516 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4517 opd_sym_map = bfd_zalloc (ibfd, amt);
4518 if (opd_sym_map == NULL)
0a1b45a2 4519 return false;
b9399fcf
AM
4520 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4521 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4522 info->keep_memory);
4523 if (relocs == NULL)
0a1b45a2 4524 return false;
b9399fcf
AM
4525 symtab_hdr = &elf_symtab_hdr (ibfd);
4526 rel_end = relocs + opd->reloc_count - 1;
4527 for (rel = relocs; rel < rel_end; rel++)
4528 {
4529 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4530 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4531
4532 if (r_type == R_PPC64_ADDR64
4533 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4534 && r_symndx < symtab_hdr->sh_info)
4535 {
4536 Elf_Internal_Sym *isym;
4537 asection *s;
4538
f1dfbfdb
L
4539 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4540 r_symndx);
b9399fcf
AM
4541 if (isym == NULL)
4542 {
4543 if (elf_section_data (opd)->relocs != relocs)
4544 free (relocs);
0a1b45a2 4545 return false;
b9399fcf
AM
4546 }
4547
4548 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4549 if (s != NULL && s != opd)
4550 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4551 }
4552 }
4553 if (elf_section_data (opd)->relocs != relocs)
4554 free (relocs);
4555 }
459609d6
AM
4556
4557 p = &htab->dot_syms;
4558 while ((eh = *p) != NULL)
4559 {
4560 *p = NULL;
4561 if (&eh->elf == htab->elf.hgot)
4562 ;
4563 else if (htab->elf.hgot == NULL
4564 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4565 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4566 else if (abiversion (ibfd) <= 1)
4567 {
4568 htab->need_func_desc_adj = 1;
4569 if (!add_symbol_adjust (eh, info))
0a1b45a2 4570 return false;
8c5b4e52 4571 }
459609d6
AM
4572 p = &eh->u.next_dot_sym;
4573 }
0a1b45a2 4574 return true;
8387904d
AM
4575}
4576
97fed1c9
JJ
4577/* Undo hash table changes when an --as-needed input file is determined
4578 not to be needed. */
4579
0a1b45a2 4580static bool
e5034e59
AM
4581ppc64_elf_notice_as_needed (bfd *ibfd,
4582 struct bfd_link_info *info,
4583 enum notice_asneeded_action act)
97fed1c9 4584{
e5034e59
AM
4585 if (act == notice_not_needed)
4586 {
4587 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4588
e5034e59 4589 if (htab == NULL)
0a1b45a2 4590 return false;
4dfe6ac6 4591
e5034e59
AM
4592 htab->dot_syms = NULL;
4593 }
4594 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4595}
4596
aa374f67
AM
4597/* If --just-symbols against a final linked binary, then assume we need
4598 toc adjusting stubs when calling functions defined there. */
4599
4600static void
4601ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4602{
4603 if ((sec->flags & SEC_CODE) != 0
4604 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4605 && is_ppc64_elf (sec->owner))
4606 {
2c3f079f
AM
4607 if (abiversion (sec->owner) >= 2
4608 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4609 sec->has_toc_reloc = 1;
4610 }
4611 _bfd_elf_link_just_syms (sec, info);
4612}
4613
e054468f 4614static struct plt_entry **
4ce794b7
AM
4615update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4616 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4617{
4618 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4619 struct plt_entry **local_plt;
f961d9dd 4620 unsigned char *local_got_tls_masks;
411e1bfb
AM
4621
4622 if (local_got_ents == NULL)
4623 {
4624 bfd_size_type size = symtab_hdr->sh_info;
4625
e054468f
AM
4626 size *= (sizeof (*local_got_ents)
4627 + sizeof (*local_plt)
4628 + sizeof (*local_got_tls_masks));
4ce794b7 4629 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4630 if (local_got_ents == NULL)
e054468f 4631 return NULL;
411e1bfb
AM
4632 elf_local_got_ents (abfd) = local_got_ents;
4633 }
4634
37da22e5 4635 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4636 {
4637 struct got_entry *ent;
4638
4639 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4640 if (ent->addend == r_addend
4641 && ent->owner == abfd
4642 && ent->tls_type == tls_type)
411e1bfb
AM
4643 break;
4644 if (ent == NULL)
4645 {
986f0783 4646 size_t amt = sizeof (*ent);
4ce794b7 4647 ent = bfd_alloc (abfd, amt);
411e1bfb 4648 if (ent == NULL)
0a1b45a2 4649 return false;
411e1bfb
AM
4650 ent->next = local_got_ents[r_symndx];
4651 ent->addend = r_addend;
e717da7e 4652 ent->owner = abfd;
411e1bfb 4653 ent->tls_type = tls_type;
0a1b45a2 4654 ent->is_indirect = false;
411e1bfb
AM
4655 ent->got.refcount = 0;
4656 local_got_ents[r_symndx] = ent;
4657 }
4658 ent->got.refcount += 1;
4659 }
4660
e054468f 4661 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4662 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4663 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4664
4665 return local_plt + r_symndx;
65f38f15
AM
4666}
4667
0a1b45a2 4668static bool
e054468f 4669update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4670{
411e1bfb 4671 struct plt_entry *ent;
1e2f5b6e 4672
e054468f 4673 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4674 if (ent->addend == addend)
4675 break;
4676 if (ent == NULL)
1e2f5b6e 4677 {
986f0783 4678 size_t amt = sizeof (*ent);
4ce794b7 4679 ent = bfd_alloc (abfd, amt);
411e1bfb 4680 if (ent == NULL)
0a1b45a2 4681 return false;
e054468f 4682 ent->next = *plist;
411e1bfb
AM
4683 ent->addend = addend;
4684 ent->plt.refcount = 0;
e054468f 4685 *plist = ent;
1e2f5b6e 4686 }
411e1bfb 4687 ent->plt.refcount += 1;
0a1b45a2 4688 return true;
1e2f5b6e
AM
4689}
4690
0a1b45a2 4691static bool
e054468f
AM
4692is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4693{
4694 return (r_type == R_PPC64_REL24
05d0e962 4695 || r_type == R_PPC64_REL24_NOTOC
7aba54da 4696 || r_type == R_PPC64_REL24_P9NOTOC
e054468f
AM
4697 || r_type == R_PPC64_REL14
4698 || r_type == R_PPC64_REL14_BRTAKEN
4699 || r_type == R_PPC64_REL14_BRNTAKEN
4700 || r_type == R_PPC64_ADDR24
4701 || r_type == R_PPC64_ADDR14
4702 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4703 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4704 || r_type == R_PPC64_PLTCALL
4705 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4706}
4707
4708/* Relocs on inline plt call sequence insns prior to the call. */
4709
0a1b45a2 4710static bool
23cedd1d
AM
4711is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4712{
4713 return (r_type == R_PPC64_PLT16_HA
4714 || r_type == R_PPC64_PLT16_HI
4715 || r_type == R_PPC64_PLT16_LO
4716 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4717 || r_type == R_PPC64_PLT_PCREL34
4718 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4719 || r_type == R_PPC64_PLTSEQ
4720 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4721}
4722
733ae98c
AM
4723/* Of relocs which might appear paired with TLSGD and TLSLD marker
4724 relocs, return true for those that operate on a dword. */
4725
4726static bool
4727is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4728{
4729 return (r_type == R_PPC64_PLT_PCREL34
4730 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4731 || r_type == R_PPC64_PLTCALL);
4732}
4733
4734/* Like bfd_reloc_offset_in_range but without a howto. Return true
4735 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4736
4737static bool
4738offset_in_range (asection *sec, bfd_vma offset, size_t size)
4739{
4740 return offset <= sec->size && size <= sec->size - offset;
4741}
4742
5bd4f169 4743/* Look through the relocs for a section during the first phase, and
65f38f15 4744 calculate needed space in the global offset table, procedure
5d1634d7 4745 linkage table, and dynamic reloc sections. */
5bd4f169 4746
0a1b45a2 4747static bool
4ce794b7
AM
4748ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4749 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4750{
65f38f15 4751 struct ppc_link_hash_table *htab;
5bd4f169 4752 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4753 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4754 const Elf_Internal_Rela *rel;
4755 const Elf_Internal_Rela *rel_end;
5bd4f169 4756 asection *sreloc;
3a71aa26 4757 struct elf_link_hash_entry *tga, *dottga;
0a1b45a2 4758 bool is_opd;
5bd4f169 4759
0e1862bb 4760 if (bfd_link_relocatable (info))
0a1b45a2 4761 return true;
5bd4f169 4762
0c8d6e5c 4763 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4764
65f38f15 4765 htab = ppc_hash_table (info);
4dfe6ac6 4766 if (htab == NULL)
0a1b45a2 4767 return false;
4dfe6ac6 4768
3a71aa26 4769 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 4770 false, false, true);
3a71aa26 4771 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 4772 false, false, true);
0ffa91dd 4773 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4774 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4775 sreloc = NULL;
b9399fcf 4776 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4777 rel_end = relocs + sec->reloc_count;
4778 for (rel = relocs; rel < rel_end; rel++)
4779 {
4780 unsigned long r_symndx;
4781 struct elf_link_hash_entry *h;
04c9666a 4782 enum elf_ppc64_reloc_type r_type;
727fc41e 4783 int tls_type;
7c8fe5c4 4784 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4785 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4786
4787 r_symndx = ELF64_R_SYM (rel->r_info);
4788 if (r_symndx < symtab_hdr->sh_info)
4789 h = NULL;
4790 else
973a3492
L
4791 {
4792 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4793 h = elf_follow_link (h);
1c865ab2
AM
4794
4795 if (h == htab->elf.hgot)
4796 sec->has_toc_reloc = 1;
973a3492 4797 }
5bd4f169 4798
5663e321
AM
4799 r_type = ELF64_R_TYPE (rel->r_info);
4800 switch (r_type)
4801 {
04bdff6a
AM
4802 case R_PPC64_D34:
4803 case R_PPC64_D34_LO:
4804 case R_PPC64_D34_HI30:
4805 case R_PPC64_D34_HA30:
4806 case R_PPC64_D28:
c213164a
AM
4807 case R_PPC64_TPREL34:
4808 case R_PPC64_DTPREL34:
04bdff6a 4809 case R_PPC64_PCREL34:
5663e321 4810 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4811 case R_PPC64_GOT_TLSGD_PCREL34:
4812 case R_PPC64_GOT_TLSLD_PCREL34:
4813 case R_PPC64_GOT_TPREL_PCREL34:
4814 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4815 case R_PPC64_PLT_PCREL34:
4816 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4817 case R_PPC64_PCREL28:
e10a07b3 4818 htab->has_power10_relocs = 1;
133a1f60
AM
4819 break;
4820 default:
5663e321
AM
4821 break;
4822 }
903b777d
AM
4823
4824 switch (r_type)
4825 {
4826 case R_PPC64_PLT16_HA:
4827 case R_PPC64_GOT_TLSLD16_HA:
4828 case R_PPC64_GOT_TLSGD16_HA:
4829 case R_PPC64_GOT_TPREL16_HA:
4830 case R_PPC64_GOT_DTPREL16_HA:
4831 case R_PPC64_GOT16_HA:
4832 case R_PPC64_TOC16_HA:
4833 case R_PPC64_PLT16_LO:
4834 case R_PPC64_PLT16_LO_DS:
4835 case R_PPC64_GOT_TLSLD16_LO:
4836 case R_PPC64_GOT_TLSGD16_LO:
4837 case R_PPC64_GOT_TPREL16_LO_DS:
4838 case R_PPC64_GOT_DTPREL16_LO_DS:
4839 case R_PPC64_GOT16_LO:
4840 case R_PPC64_GOT16_LO_DS:
4841 case R_PPC64_TOC16_LO:
4842 case R_PPC64_TOC16_LO_DS:
4843 case R_PPC64_GOT_PCREL34:
4844 ppc64_elf_tdata (abfd)->has_optrel = 1;
4845 ppc64_elf_section_data (sec)->has_optrel = 1;
4846 break;
4847 default:
4848 break;
4849 }
4850
f749f26e 4851 ifunc = NULL;
25f23106
AM
4852 if (h != NULL)
4853 {
4854 if (h->type == STT_GNU_IFUNC)
4855 {
4856 h->needs_plt = 1;
4857 ifunc = &h->plt.plist;
4858 }
4859 }
4860 else
4861 {
f1dfbfdb 4862 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
25f23106
AM
4863 abfd, r_symndx);
4864 if (isym == NULL)
0a1b45a2 4865 return false;
25f23106
AM
4866
4867 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4868 {
4869 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4870 rel->r_addend,
37da22e5 4871 NON_GOT | PLT_IFUNC);
25f23106 4872 if (ifunc == NULL)
0a1b45a2 4873 return false;
25f23106
AM
4874 }
4875 }
727fc41e 4876
f749f26e 4877 tls_type = 0;
a33d1f77 4878 switch (r_type)
5bd4f169 4879 {
727fc41e
AM
4880 case R_PPC64_TLSGD:
4881 case R_PPC64_TLSLD:
4882 /* These special tls relocs tie a call to __tls_get_addr with
4883 its parameter symbol. */
37da22e5 4884 if (h != NULL)
ed7007c1 4885 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4886 else
4887 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4888 rel->r_addend,
37da22e5 4889 NON_GOT | TLS_TLS | TLS_MARK))
0a1b45a2 4890 return false;
37da22e5 4891 sec->has_tls_reloc = 1;
727fc41e
AM
4892 break;
4893
411e1bfb
AM
4894 case R_PPC64_GOT_TLSLD16:
4895 case R_PPC64_GOT_TLSLD16_LO:
4896 case R_PPC64_GOT_TLSLD16_HI:
4897 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4898 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4899 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4900 goto dogottls;
4901
4902 case R_PPC64_GOT_TLSGD16:
4903 case R_PPC64_GOT_TLSGD16_LO:
4904 case R_PPC64_GOT_TLSGD16_HI:
4905 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4906 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4907 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4908 goto dogottls;
4909
4910 case R_PPC64_GOT_TPREL16_DS:
4911 case R_PPC64_GOT_TPREL16_LO_DS:
4912 case R_PPC64_GOT_TPREL16_HI:
4913 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4914 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4915 if (bfd_link_dll (info))
411e1bfb
AM
4916 info->flags |= DF_STATIC_TLS;
4917 tls_type = TLS_TLS | TLS_TPREL;
4918 goto dogottls;
4919
4920 case R_PPC64_GOT_DTPREL16_DS:
4921 case R_PPC64_GOT_DTPREL16_LO_DS:
4922 case R_PPC64_GOT_DTPREL16_HI:
4923 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4924 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4925 tls_type = TLS_TLS | TLS_DTPREL;
4926 dogottls:
4927 sec->has_tls_reloc = 1;
066f4018 4928 goto dogot;
411e1bfb 4929
903b777d
AM
4930 case R_PPC64_GOT16:
4931 case R_PPC64_GOT16_LO:
4932 case R_PPC64_GOT16_HI:
65f38f15 4933 case R_PPC64_GOT16_HA:
903b777d 4934 case R_PPC64_GOT16_DS:
066f4018 4935 case R_PPC64_GOT16_LO_DS:
4a421c53 4936 case R_PPC64_GOT_PCREL34:
066f4018 4937 dogot:
65f38f15 4938 /* This symbol requires a global offset table entry. */
4c52953f 4939 sec->has_toc_reloc = 1;
33c0ec9d
AM
4940 if (r_type == R_PPC64_GOT_TLSLD16
4941 || r_type == R_PPC64_GOT_TLSGD16
4942 || r_type == R_PPC64_GOT_TPREL16_DS
4943 || r_type == R_PPC64_GOT_DTPREL16_DS
4944 || r_type == R_PPC64_GOT16
4945 || r_type == R_PPC64_GOT16_DS)
4946 {
4947 htab->do_multi_toc = 1;
d77c8a4b 4948 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4949 }
4950
e717da7e
AM
4951 if (ppc64_elf_tdata (abfd)->got == NULL
4952 && !create_got_section (abfd, info))
0a1b45a2 4953 return false;
5bd4f169
AM
4954
4955 if (h != NULL)
4956 {
411e1bfb
AM
4957 struct ppc_link_hash_entry *eh;
4958 struct got_entry *ent;
65f38f15 4959
ed7007c1 4960 eh = ppc_elf_hash_entry (h);
411e1bfb 4961 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4962 if (ent->addend == rel->r_addend
e717da7e 4963 && ent->owner == abfd
411e1bfb
AM
4964 && ent->tls_type == tls_type)
4965 break;
4966 if (ent == NULL)
5bd4f169 4967 {
986f0783 4968 size_t amt = sizeof (*ent);
4ce794b7 4969 ent = bfd_alloc (abfd, amt);
411e1bfb 4970 if (ent == NULL)
0a1b45a2 4971 return false;
411e1bfb 4972 ent->next = eh->elf.got.glist;
133a1f60 4973 ent->addend = rel->r_addend;
e717da7e 4974 ent->owner = abfd;
411e1bfb 4975 ent->tls_type = tls_type;
0a1b45a2 4976 ent->is_indirect = false;
411e1bfb
AM
4977 ent->got.refcount = 0;
4978 eh->elf.got.glist = ent;
5bd4f169 4979 }
411e1bfb 4980 ent->got.refcount += 1;
e7b938ca 4981 eh->tls_mask |= tls_type;
5bd4f169 4982 }
411e1bfb
AM
4983 else
4984 /* This is a global offset table entry for a local symbol. */
4985 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4986 rel->r_addend, tls_type))
0a1b45a2 4987 return false;
5bd4f169
AM
4988 break;
4989
5bd4f169 4990 case R_PPC64_PLT16_HA:
65f38f15
AM
4991 case R_PPC64_PLT16_HI:
4992 case R_PPC64_PLT16_LO:
08be3224 4993 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4994 case R_PPC64_PLT_PCREL34:
4995 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4996 case R_PPC64_PLT32:
4997 case R_PPC64_PLT64:
cbf95972
AM
4998 /* This symbol requires a procedure linkage table entry. */
4999 plt_list = ifunc;
5000 if (h != NULL)
e054468f 5001 {
e054468f
AM
5002 h->needs_plt = 1;
5003 if (h->root.root.string[0] == '.'
5004 && h->root.root.string[1] != '\0')
ed7007c1
AM
5005 ppc_elf_hash_entry (h)->is_func = 1;
5006 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
5007 plt_list = &h->plt.plist;
5008 }
5009 if (plt_list == NULL)
2d7ad24e 5010 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5011 rel->r_addend,
2d7ad24e 5012 NON_GOT | PLT_KEEP);
133a1f60 5013 if (!update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5014 return false;
5bd4f169
AM
5015 break;
5016
5017 /* The following relocations don't need to propagate the
5018 relocation if linking a shared object since they are
5019 section relative. */
5020 case R_PPC64_SECTOFF:
5021 case R_PPC64_SECTOFF_LO:
5022 case R_PPC64_SECTOFF_HI:
5023 case R_PPC64_SECTOFF_HA:
5024 case R_PPC64_SECTOFF_DS:
5025 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5026 case R_PPC64_DTPREL16:
5027 case R_PPC64_DTPREL16_LO:
5028 case R_PPC64_DTPREL16_HI:
5029 case R_PPC64_DTPREL16_HA:
5030 case R_PPC64_DTPREL16_DS:
5031 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5032 case R_PPC64_DTPREL16_HIGH:
5033 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5034 case R_PPC64_DTPREL16_HIGHER:
5035 case R_PPC64_DTPREL16_HIGHERA:
5036 case R_PPC64_DTPREL16_HIGHEST:
5037 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5038 break;
5039
ad8e1ba5 5040 /* Nor do these. */
25f23106
AM
5041 case R_PPC64_REL16:
5042 case R_PPC64_REL16_LO:
5043 case R_PPC64_REL16_HI:
5044 case R_PPC64_REL16_HA:
4a969973
AM
5045 case R_PPC64_REL16_HIGH:
5046 case R_PPC64_REL16_HIGHA:
5047 case R_PPC64_REL16_HIGHER:
5048 case R_PPC64_REL16_HIGHERA:
5049 case R_PPC64_REL16_HIGHEST:
5050 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
5051 case R_PPC64_REL16_HIGHER34:
5052 case R_PPC64_REL16_HIGHERA34:
5053 case R_PPC64_REL16_HIGHEST34:
5054 case R_PPC64_REL16_HIGHESTA34:
a680de9a 5055 case R_PPC64_REL16DX_HA:
25f23106
AM
5056 break;
5057
45965137
AM
5058 /* Not supported as a dynamic relocation. */
5059 case R_PPC64_ADDR64_LOCAL:
0e1862bb 5060 if (bfd_link_pic (info))
45965137
AM
5061 {
5062 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5063 ppc_howto_init ();
695344c0 5064 /* xgettext:c-format */
174d0a74 5065 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 5066 "in shared libraries and PIEs\n"),
45965137
AM
5067 abfd, sec, rel->r_offset,
5068 ppc64_elf_howto_table[r_type]->name);
5069 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5070 return false;
45965137
AM
5071 }
5072 break;
5073
ad8e1ba5 5074 case R_PPC64_TOC16:
33c0ec9d
AM
5075 case R_PPC64_TOC16_DS:
5076 htab->do_multi_toc = 1;
d77c8a4b 5077 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 5078 /* Fall through. */
ad8e1ba5
AM
5079 case R_PPC64_TOC16_LO:
5080 case R_PPC64_TOC16_HI:
5081 case R_PPC64_TOC16_HA:
ad8e1ba5 5082 case R_PPC64_TOC16_LO_DS:
4c52953f 5083 sec->has_toc_reloc = 1;
ec73ddcd 5084 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
5085 {
5086 /* We may need a copy reloc. */
5087 h->non_got_ref = 1;
5088 /* Strongly prefer a copy reloc over a dynamic reloc.
5089 glibc ld.so as of 2019-08 will error out if one of
5090 these relocations is emitted. */
5091 h->needs_copy = 1;
5092 goto dodyn;
5093 }
ad8e1ba5
AM
5094 break;
5095
006589cf
AM
5096 /* Marker reloc. */
5097 case R_PPC64_ENTRY:
5098 break;
5099
5bd4f169
AM
5100 /* This relocation describes the C++ object vtable hierarchy.
5101 Reconstruct it for later use during GC. */
5102 case R_PPC64_GNU_VTINHERIT:
c152c796 5103 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
0a1b45a2 5104 return false;
5bd4f169
AM
5105 break;
5106
5107 /* This relocation describes which C++ vtable entries are actually
5108 used. Record for later use during GC. */
5109 case R_PPC64_GNU_VTENTRY:
a0ea3a14 5110 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
0a1b45a2 5111 return false;
5bd4f169
AM
5112 break;
5113
721956f4
AM
5114 case R_PPC64_REL14:
5115 case R_PPC64_REL14_BRTAKEN:
5116 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5117 {
5118 asection *dest = NULL;
5119
5120 /* Heuristic: If jumping outside our section, chances are
5121 we are going to need a stub. */
5122 if (h != NULL)
5123 {
5124 /* If the sym is weak it may be overridden later, so
5125 don't assume we know where a weak sym lives. */
5126 if (h->root.type == bfd_link_hash_defined)
5127 dest = h->root.u.def.section;
5128 }
5129 else
87d72d41
AM
5130 {
5131 Elf_Internal_Sym *isym;
5132
f1dfbfdb 5133 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5134 abfd, r_symndx);
5135 if (isym == NULL)
0a1b45a2 5136 return false;
87d72d41
AM
5137
5138 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5139 }
5140
220c76dd 5141 if (dest != sec)
7c8fe5c4 5142 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5143 }
3e04d765
AM
5144 goto rel24;
5145
5146 case R_PPC64_PLTCALL:
5663e321 5147 case R_PPC64_PLTCALL_NOTOC:
3e04d765 5148 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
5149 /* Fall through. */
5150
5d1634d7 5151 case R_PPC64_REL24:
05d0e962 5152 case R_PPC64_REL24_NOTOC:
7aba54da 5153 case R_PPC64_REL24_P9NOTOC:
3e04d765 5154 rel24:
cbf95972
AM
5155 plt_list = ifunc;
5156 if (h != NULL)
5d1634d7 5157 {
e054468f
AM
5158 h->needs_plt = 1;
5159 if (h->root.root.string[0] == '.'
5160 && h->root.root.string[1] != '\0')
ed7007c1 5161 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 5162
3a71aa26 5163 if (h == tga || h == dottga)
cbf95972
AM
5164 {
5165 sec->has_tls_reloc = 1;
5166 if (rel != relocs
5167 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5168 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5169 /* We have a new-style __tls_get_addr call with
5170 a marker reloc. */
5171 ;
5172 else
5173 /* Mark this section as having an old-style call. */
9737e8af 5174 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5175 }
5176 plt_list = &h->plt.plist;
411e1bfb 5177 }
cbf95972
AM
5178
5179 /* We may need a .plt entry if the function this reloc
5180 refers to is in a shared lib. */
5181 if (plt_list
133a1f60 5182 && !update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5183 return false;
411e1bfb
AM
5184 break;
5185
cbf95972
AM
5186 case R_PPC64_ADDR14:
5187 case R_PPC64_ADDR14_BRNTAKEN:
5188 case R_PPC64_ADDR14_BRTAKEN:
5189 case R_PPC64_ADDR24:
5190 goto dodyn;
5191
411e1bfb
AM
5192 case R_PPC64_TPREL64:
5193 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5194 if (bfd_link_dll (info))
411e1bfb
AM
5195 info->flags |= DF_STATIC_TLS;
5196 goto dotlstoc;
5197
5198 case R_PPC64_DTPMOD64:
5199 if (rel + 1 < rel_end
5200 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5201 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5202 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5203 else
951fd09b 5204 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5205 goto dotlstoc;
5206
5207 case R_PPC64_DTPREL64:
5208 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5209 if (rel != relocs
5210 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5211 && rel[-1].r_offset == rel->r_offset - 8)
5212 /* This is the second reloc of a dtpmod, dtprel pair.
5213 Don't mark with TLS_DTPREL. */
5214 goto dodyn;
5215
5216 dotlstoc:
5217 sec->has_tls_reloc = 1;
5218 if (h != NULL)
ed7007c1 5219 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5220 else
5221 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5222 rel->r_addend, tls_type))
0a1b45a2 5223 return false;
411e1bfb 5224
7c8fe5c4
AM
5225 ppc64_sec = ppc64_elf_section_data (sec);
5226 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5227 {
3a71aa26
AM
5228 bfd_size_type amt;
5229
e7b938ca 5230 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5231 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5232 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5233 if (ppc64_sec->u.toc.symndx == NULL)
0a1b45a2 5234 return false;
3a71aa26
AM
5235 amt = sec->size * sizeof (bfd_vma) / 8;
5236 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5237 if (ppc64_sec->u.toc.add == NULL)
0a1b45a2 5238 return false;
7c8fe5c4
AM
5239 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5240 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5241 }
5242 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5243 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5244 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5245
5246 /* Mark the second slot of a GD or LD entry.
5247 -1 to indicate GD and -2 to indicate LD. */
5248 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5249 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5250 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5251 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5252 goto dodyn;
5253
411e1bfb
AM
5254 case R_PPC64_TPREL16_HI:
5255 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5256 case R_PPC64_TPREL16_HIGH:
5257 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5258 case R_PPC64_TPREL16_HIGHER:
5259 case R_PPC64_TPREL16_HIGHERA:
5260 case R_PPC64_TPREL16_HIGHEST:
5261 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5262 sec->has_tls_reloc = 1;
5263 /* Fall through. */
c213164a 5264 case R_PPC64_TPREL34:
252dcdf4
AM
5265 case R_PPC64_TPREL16:
5266 case R_PPC64_TPREL16_DS:
5267 case R_PPC64_TPREL16_LO:
5268 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5269 if (bfd_link_dll (info))
5270 info->flags |= DF_STATIC_TLS;
5271 goto dodyn;
5d1634d7 5272
e86ce104 5273 case R_PPC64_ADDR64:
b9399fcf 5274 if (is_opd
1e2f5b6e 5275 && rel + 1 < rel_end
4ce794b7 5276 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5277 {
8387904d 5278 if (h != NULL)
ed7007c1 5279 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5280 }
e86ce104
AM
5281 /* Fall through. */
5282
65f38f15
AM
5283 case R_PPC64_ADDR16:
5284 case R_PPC64_ADDR16_DS:
5285 case R_PPC64_ADDR16_HA:
5286 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5287 case R_PPC64_ADDR16_HIGH:
5288 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5289 case R_PPC64_ADDR16_HIGHER:
5290 case R_PPC64_ADDR16_HIGHERA:
5291 case R_PPC64_ADDR16_HIGHEST:
5292 case R_PPC64_ADDR16_HIGHESTA:
5293 case R_PPC64_ADDR16_LO:
5294 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5295 case R_PPC64_D34:
5296 case R_PPC64_D34_LO:
5297 case R_PPC64_D34_HI30:
5298 case R_PPC64_D34_HA30:
5299 case R_PPC64_ADDR16_HIGHER34:
5300 case R_PPC64_ADDR16_HIGHERA34:
5301 case R_PPC64_ADDR16_HIGHEST34:
5302 case R_PPC64_ADDR16_HIGHESTA34:
5303 case R_PPC64_D28:
0e1862bb 5304 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5305 && rel->r_addend == 0)
5306 {
5307 /* We may need a .plt entry if this reloc refers to a
5308 function in a shared lib. */
5663e321 5309 if (!update_plt_info (abfd, &h->plt.plist, 0))
0a1b45a2 5310 return false;
a345bc8d
AM
5311 h->pointer_equality_needed = 1;
5312 }
5313 /* Fall through. */
5314
5315 case R_PPC64_REL30:
5316 case R_PPC64_REL32:
5317 case R_PPC64_REL64:
65f38f15 5318 case R_PPC64_ADDR32:
65f38f15
AM
5319 case R_PPC64_UADDR16:
5320 case R_PPC64_UADDR32:
5321 case R_PPC64_UADDR64:
5bd4f169 5322 case R_PPC64_TOC:
ec73ddcd 5323 if (h != NULL && bfd_link_executable (info))
81848ca0 5324 /* We may need a copy reloc. */
f5385ebf 5325 h->non_got_ref = 1;
81848ca0 5326
41bd81ab 5327 /* Don't propagate .opd relocs. */
b9399fcf 5328 if (NO_OPD_RELOCS && is_opd)
e86ce104 5329 break;
e86ce104 5330
65f38f15
AM
5331 /* If we are creating a shared library, and this is a reloc
5332 against a global symbol, or a non PC relative reloc
5333 against a local symbol, then we need to copy the reloc
5334 into the shared library. However, if we are linking with
5335 -Bsymbolic, we do not need to copy a reloc against a
5336 global symbol which is defined in an object we are
5337 including in the link (i.e., DEF_REGULAR is set). At
5338 this point we have not seen all the input files, so it is
5339 possible that DEF_REGULAR is not set now but will be set
5340 later (it is never cleared). In case of a weak definition,
5341 DEF_REGULAR may be cleared later by a strong definition in
5342 a shared library. We account for that possibility below by
f4656909 5343 storing information in the dyn_relocs field of the hash
65f38f15
AM
5344 table entry. A similar situation occurs when creating
5345 shared libraries and symbol visibility changes render the
5346 symbol local.
5347
5348 If on the other hand, we are creating an executable, we
5349 may need to keep relocations for symbols satisfied by a
5350 dynamic library if we manage to avoid copy relocs for the
5351 symbol. */
411e1bfb 5352 dodyn:
ec73ddcd
AM
5353 if ((h != NULL
5354 && (h->root.type == bfd_link_hash_defweak
5355 || !h->def_regular))
5356 || (h != NULL
5357 && !bfd_link_executable (info)
5358 && !SYMBOLIC_BIND (info, h))
5359 || (bfd_link_pic (info)
5360 && must_be_dyn_reloc (info, r_type))
0e1862bb 5361 || (!bfd_link_pic (info)
25f23106 5362 && ifunc != NULL))
5bd4f169 5363 {
65f38f15
AM
5364 /* We must copy these reloc types into the output file.
5365 Create a reloc section in dynobj and make room for
5366 this reloc. */
5bd4f169
AM
5367 if (sreloc == NULL)
5368 {
83bac4b0 5369 sreloc = _bfd_elf_make_dynamic_reloc_section
0a1b45a2 5370 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
65f38f15 5371
5bd4f169 5372 if (sreloc == NULL)
0a1b45a2 5373 return false;
5bd4f169
AM
5374 }
5375
65f38f15
AM
5376 /* If this is a global symbol, we count the number of
5377 relocations we need for this symbol. */
5378 if (h != NULL)
5379 {
1657026c
AM
5380 struct ppc_dyn_relocs *p;
5381 struct ppc_dyn_relocs **head;
19e08130 5382
1657026c 5383 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
5384 p = *head;
5385 if (p == NULL || p->sec != sec)
5386 {
5387 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5388 if (p == NULL)
0a1b45a2 5389 return false;
19e08130
AM
5390 p->next = *head;
5391 *head = p;
5392 p->sec = sec;
5393 p->count = 0;
5394 p->pc_count = 0;
1657026c 5395 p->rel_count = 0;
19e08130
AM
5396 }
5397 p->count += 1;
5398 if (!must_be_dyn_reloc (info, r_type))
5399 p->pc_count += 1;
1657026c
AM
5400 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5401 && rel->r_offset % 2 == 0
8ffb6df2 5402 && sec->alignment_power != 0)
1657026c 5403 p->rel_count += 1;
65f38f15
AM
5404 }
5405 else
5406 {
ec338859
AM
5407 /* Track dynamic relocs needed for local syms too.
5408 We really need local syms available to do this
5409 easily. Oh well. */
1657026c
AM
5410 struct ppc_local_dyn_relocs *p;
5411 struct ppc_local_dyn_relocs **head;
0a1b45a2 5412 bool is_ifunc;
ec338859 5413 asection *s;
6edfbbad 5414 void *vpp;
87d72d41 5415 Elf_Internal_Sym *isym;
6edfbbad 5416
f1dfbfdb 5417 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5418 abfd, r_symndx);
5419 if (isym == NULL)
0a1b45a2 5420 return false;
ec338859 5421
87d72d41
AM
5422 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5423 if (s == NULL)
5424 s = sec;
5425
6edfbbad 5426 vpp = &elf_section_data (s)->local_dynrel;
1657026c 5427 head = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
5428 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5429 p = *head;
5430 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5431 p = p->next;
5432 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5433 {
5434 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5435 if (p == NULL)
0a1b45a2 5436 return false;
19e08130
AM
5437 p->next = *head;
5438 *head = p;
5439 p->sec = sec;
19e08130 5440 p->count = 0;
1657026c
AM
5441 p->rel_count = 0;
5442 p->ifunc = is_ifunc;
19e08130
AM
5443 }
5444 p->count += 1;
1657026c
AM
5445 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5446 && rel->r_offset % 2 == 0
8ffb6df2 5447 && sec->alignment_power != 0)
1657026c 5448 p->rel_count += 1;
ec338859 5449 }
65f38f15 5450 }
5bd4f169 5451 break;
65f38f15
AM
5452
5453 default:
96e0dda4 5454 break;
5bd4f169
AM
5455 }
5456 }
5457
0a1b45a2 5458 return true;
5bd4f169
AM
5459}
5460
ee67d69a
AM
5461/* Merge backend specific data from an object file to the output
5462 object file when linking. */
5463
0a1b45a2 5464static bool
50e03d47 5465ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5466{
50e03d47 5467 bfd *obfd = info->output_bfd;
ee67d69a
AM
5468 unsigned long iflags, oflags;
5469
5470 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
0a1b45a2 5471 return true;
ee67d69a
AM
5472
5473 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
0a1b45a2 5474 return true;
ee67d69a 5475
50e03d47 5476 if (!_bfd_generic_verify_endian_match (ibfd, info))
0a1b45a2 5477 return false;
ee67d69a
AM
5478
5479 iflags = elf_elfheader (ibfd)->e_flags;
5480 oflags = elf_elfheader (obfd)->e_flags;
5481
f6c7c3e8 5482 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5483 {
4eca0228 5484 _bfd_error_handler
695344c0 5485 /* xgettext:c-format */
871b3ab2 5486 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a 5487 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5488 return false;
ee67d69a 5489 }
f6c7c3e8 5490 else if (iflags != oflags && iflags != 0)
ee67d69a 5491 {
4eca0228 5492 _bfd_error_handler
695344c0 5493 /* xgettext:c-format */
871b3ab2 5494 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5495 ibfd, iflags, oflags);
5496 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5497 return false;
ee67d69a
AM
5498 }
5499
4a91d0ba 5500 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
0a1b45a2 5501 return false;
005d79fd 5502
ee67d69a 5503 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5504 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5505}
5506
0a1b45a2 5507static bool
ee67d69a
AM
5508ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5509{
5510 /* Print normal ELF private data. */
5511 _bfd_elf_print_private_bfd_data (abfd, ptr);
5512
5513 if (elf_elfheader (abfd)->e_flags != 0)
5514 {
5515 FILE *file = ptr;
5516
ee67d69a
AM
5517 fprintf (file, _("private flags = 0x%lx:"),
5518 elf_elfheader (abfd)->e_flags);
5519
5520 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5521 fprintf (file, _(" [abiv%ld]"),
5522 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5523 fputc ('\n', file);
5524 }
5525
0a1b45a2 5526 return true;
ee67d69a
AM
5527}
5528
8387904d 5529/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5530 of the code entry point, and its section, which must be in the same
5531 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5532
5533static bfd_vma
5534opd_entry_value (asection *opd_sec,
5535 bfd_vma offset,
5536 asection **code_sec,
aef36ac1 5537 bfd_vma *code_off,
0a1b45a2 5538 bool in_code_sec)
8387904d
AM
5539{
5540 bfd *opd_bfd = opd_sec->owner;
8860955f 5541 Elf_Internal_Rela *relocs;
8387904d 5542 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5543 bfd_vma val;
8387904d 5544
9f296da3
AM
5545 /* No relocs implies we are linking a --just-symbols object, or looking
5546 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5547 if (opd_sec->reloc_count == 0)
5548 {
729eabd5 5549 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5550
729eabd5
AM
5551 if (contents == NULL)
5552 {
5553 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5554 return (bfd_vma) -1;
5555 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5556 }
ee1e4ede 5557
dbb3fbbb 5558 /* PR 17512: file: 64b9dfbb. */
451dfd38 5559 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5560 return (bfd_vma) -1;
5561
729eabd5 5562 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5563 if (code_sec != NULL)
5564 {
5565 asection *sec, *likely = NULL;
ee1e4ede 5566
aef36ac1 5567 if (in_code_sec)
4b85d634 5568 {
aef36ac1
AM
5569 sec = *code_sec;
5570 if (sec->vma <= val
5571 && val < sec->vma + sec->size)
5572 likely = sec;
5573 else
5574 val = -1;
5575 }
5576 else
5577 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5578 if (sec->vma <= val
5579 && (sec->flags & SEC_LOAD) != 0
5580 && (sec->flags & SEC_ALLOC) != 0)
5581 likely = sec;
5582 if (likely != NULL)
5583 {
5584 *code_sec = likely;
5585 if (code_off != NULL)
5586 *code_off = val - likely->vma;
4b85d634
AM
5587 }
5588 }
aef36ac1 5589 return val;
4b85d634
AM
5590 }
5591
0c8d6e5c 5592 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5593
729eabd5 5594 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f 5595 if (relocs == NULL)
0a1b45a2 5596 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
877a8638
NC
5597 /* PR 17512: file: df8e1fd6. */
5598 if (relocs == NULL)
5599 return (bfd_vma) -1;
645ea6a9 5600
8387904d 5601 /* Go find the opd reloc at the sym address. */
8860955f 5602 lo = relocs;
8387904d 5603 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5604 val = (bfd_vma) -1;
8387904d
AM
5605 while (lo < hi)
5606 {
5607 look = lo + (hi - lo) / 2;
5608 if (look->r_offset < offset)
5609 lo = look + 1;
5610 else if (look->r_offset > offset)
5611 hi = look;
5612 else
5613 {
0ffa91dd
NC
5614 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5615
8387904d
AM
5616 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5617 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5618 {
5619 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5620 asection *sec = NULL;
8387904d 5621
b53dfeb2
AM
5622 if (symndx >= symtab_hdr->sh_info
5623 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5624 {
5625 struct elf_link_hash_entry **sym_hashes;
5626 struct elf_link_hash_entry *rh;
5627
5628 sym_hashes = elf_sym_hashes (opd_bfd);
5629 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5630 if (rh != NULL)
5631 {
5632 rh = elf_follow_link (rh);
bb854a36
AM
5633 if (rh->root.type != bfd_link_hash_defined
5634 && rh->root.type != bfd_link_hash_defweak)
5635 break;
5636 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5637 {
bb854a36
AM
5638 val = rh->root.u.def.value;
5639 sec = rh->root.u.def.section;
b53dfeb2
AM
5640 }
5641 }
5642 }
5643
5644 if (sec == NULL)
5645 {
5646 Elf_Internal_Sym *sym;
5647
5648 if (symndx < symtab_hdr->sh_info)
5649 {
5650 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5651 if (sym == NULL)
5652 {
5653 size_t symcnt = symtab_hdr->sh_info;
5654 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5655 symcnt, 0,
5656 NULL, NULL, NULL);
5657 if (sym == NULL)
5658 break;
5659 symtab_hdr->contents = (bfd_byte *) sym;
5660 }
5661 sym += symndx;
128205bb
AM
5662 }
5663 else
5664 {
b53dfeb2
AM
5665 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5666 1, symndx,
5667 NULL, NULL, NULL);
128205bb
AM
5668 if (sym == NULL)
5669 break;
128205bb 5670 }
b53dfeb2
AM
5671 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5672 if (sec == NULL)
5673 break;
5674 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5675 val = sym->st_value;
8387904d 5676 }
b53dfeb2 5677
8387904d
AM
5678 val += look->r_addend;
5679 if (code_off != NULL)
5680 *code_off = val;
5681 if (code_sec != NULL)
aef36ac1
AM
5682 {
5683 if (in_code_sec && *code_sec != sec)
5684 return -1;
5685 else
5686 *code_sec = sec;
5687 }
b53dfeb2 5688 if (sec->output_section != NULL)
8387904d 5689 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5690 }
5691 break;
5692 }
5693 }
645ea6a9 5694
645ea6a9 5695 return val;
8387904d
AM
5696}
5697
aef36ac1
AM
5698/* If the ELF symbol SYM might be a function in SEC, return the
5699 function size and set *CODE_OFF to the function's entry point,
5700 otherwise return zero. */
9f296da3 5701
aef36ac1
AM
5702static bfd_size_type
5703ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5704 bfd_vma *code_off)
9f296da3 5705{
aef36ac1 5706 bfd_size_type size;
24aebc79 5707 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1
AM
5708
5709 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5710 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5711 return 0;
5712
24aebc79
NC
5713 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5714
5715 /* In theory we should check that the symbol's type satisfies
5716 _bfd_elf_is_function_type(), but there are some function-like
5717 symbols which would fail this test. (eg _start). Instead
5718 we check for hidden, local, notype symbols with zero size.
5719 This type of symbol is generated by the annobin plugin for gcc
5720 and clang, and should not be considered to be a function symbol. */
5721 if (size == 0
5722 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5723 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5724 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5725 return 0;
aef36ac1
AM
5726
5727 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5728 {
b07bca4e
AM
5729 struct _opd_sec_data *opd = get_opd_info (sym->section);
5730 bfd_vma symval = sym->value;
5731
5732 if (opd != NULL
5733 && opd->adjust != NULL
5734 && elf_section_data (sym->section)->relocs != NULL)
5735 {
5736 /* opd_entry_value will use cached relocs that have been
5737 adjusted, but with raw symbols. That means both local
5738 and global symbols need adjusting. */
5739 long adjust = opd->adjust[OPD_NDX (symval)];
5740 if (adjust == -1)
5741 return 0;
5742 symval += adjust;
5743 }
5744
5745 if (opd_entry_value (sym->section, symval,
0a1b45a2 5746 &sec, code_off, true) == (bfd_vma) -1)
aef36ac1
AM
5747 return 0;
5748 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5749 symbol. This size has nothing to do with the code size of the
5750 function, which is what we're supposed to return, but the
5751 code size isn't available without looking up the dot-sym.
5752 However, doing that would be a waste of time particularly
5753 since elf_find_function will look at the dot-sym anyway.
5754 Now, elf_find_function will keep the largest size of any
5755 function sym found at the code address of interest, so return
5756 1 here to avoid it incorrectly caching a larger function size
5757 for a small function. This does mean we return the wrong
5758 size for a new-ABI function of size 24, but all that does is
5759 disable caching for such functions. */
5760 if (size == 24)
5761 size = 1;
9f296da3 5762 }
aef36ac1
AM
5763 else
5764 {
5765 if (sym->section != sec)
5766 return 0;
5767 *code_off = sym->value;
5768 }
24aebc79
NC
5769
5770 /* Do not return 0 for the function's size. */
5771 return size ? size : 1;
9f296da3
AM
5772}
5773
f378ab09
AM
5774/* Return true if symbol is a strong function defined in an ELFv2
5775 object with st_other localentry bits of zero, ie. its local entry
5776 point coincides with its global entry point. */
5777
0a1b45a2 5778static bool
f378ab09
AM
5779is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5780{
5781 return (h != NULL
5782 && h->type == STT_FUNC
5783 && h->root.type == bfd_link_hash_defined
5784 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5785 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5786 && is_ppc64_elf (h->root.u.def.section->owner)
5787 && abiversion (h->root.u.def.section->owner) >= 2);
5788}
5789
854b41e7
AM
5790/* Return true if symbol is defined in a regular object file. */
5791
0a1b45a2 5792static bool
854b41e7
AM
5793is_static_defined (struct elf_link_hash_entry *h)
5794{
5795 return ((h->root.type == bfd_link_hash_defined
5796 || h->root.type == bfd_link_hash_defweak)
5797 && h->root.u.def.section != NULL
5798 && h->root.u.def.section->output_section != NULL);
5799}
5800
b31867b6
AM
5801/* If FDH is a function descriptor symbol, return the associated code
5802 entry symbol if it is defined. Return NULL otherwise. */
5803
5804static struct ppc_link_hash_entry *
5805defined_code_entry (struct ppc_link_hash_entry *fdh)
5806{
5807 if (fdh->is_func_descriptor)
5808 {
5809 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5810 if (fh->elf.root.type == bfd_link_hash_defined
5811 || fh->elf.root.type == bfd_link_hash_defweak)
5812 return fh;
5813 }
5814 return NULL;
5815}
5816
5817/* If FH is a function code entry symbol, return the associated
5818 function descriptor symbol if it is defined. Return NULL otherwise. */
5819
5820static struct ppc_link_hash_entry *
5821defined_func_desc (struct ppc_link_hash_entry *fh)
5822{
5823 if (fh->oh != NULL
5824 && fh->oh->is_func_descriptor)
5825 {
5826 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5827 if (fdh->elf.root.type == bfd_link_hash_defined
5828 || fdh->elf.root.type == bfd_link_hash_defweak)
5829 return fdh;
5830 }
5831 return NULL;
5832}
5833
ed7007c1
AM
5834/* Given H is a symbol that satisfies is_static_defined, return the
5835 value in the output file. */
5836
5837static bfd_vma
5838defined_sym_val (struct elf_link_hash_entry *h)
5839{
5840 return (h->root.u.def.section->output_section->vma
5841 + h->root.u.def.section->output_offset
5842 + h->root.u.def.value);
5843}
5844
5845/* Return true if H matches __tls_get_addr or one of its variants. */
5846
0a1b45a2 5847static bool
ed7007c1
AM
5848is_tls_get_addr (struct elf_link_hash_entry *h,
5849 struct ppc_link_hash_table *htab)
5850{
30845f11
AM
5851 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5852 || h == elf_hash_entry (htab->tga_desc_fd)
5853 || h == elf_hash_entry (htab->tls_get_addr)
5854 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5855}
5856
0a1b45a2 5857static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
8c5b4e52
AM
5858
5859/* Garbage collect sections, after first dealing with dot-symbols. */
5860
0a1b45a2 5861static bool
8c5b4e52
AM
5862ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5863{
5864 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5865
5866 if (htab != NULL && htab->need_func_desc_adj)
5867 {
5868 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5869 htab->need_func_desc_adj = 0;
5870 }
5871 return bfd_elf_gc_sections (abfd, info);
5872}
5873
74f0fb50
AM
5874/* Mark all our entry sym sections, both opd and code section. */
5875
5876static void
5877ppc64_elf_gc_keep (struct bfd_link_info *info)
5878{
5879 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5880 struct bfd_sym_chain *sym;
5881
4dfe6ac6
NC
5882 if (htab == NULL)
5883 return;
5884
74f0fb50
AM
5885 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5886 {
b31867b6 5887 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5888 asection *sec;
5889
ed7007c1 5890 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
0a1b45a2 5891 false, false, true));
74f0fb50
AM
5892 if (eh == NULL)
5893 continue;
5894 if (eh->elf.root.type != bfd_link_hash_defined
5895 && eh->elf.root.type != bfd_link_hash_defweak)
5896 continue;
5897
b31867b6
AM
5898 fh = defined_code_entry (eh);
5899 if (fh != NULL)
74f0fb50 5900 {
b31867b6 5901 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5902 sec->flags |= SEC_KEEP;
5903 }
5904 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5905 && opd_entry_value (eh->elf.root.u.def.section,
5906 eh->elf.root.u.def.value,
0a1b45a2 5907 &sec, NULL, false) != (bfd_vma) -1)
74f0fb50
AM
5908 sec->flags |= SEC_KEEP;
5909
5910 sec = eh->elf.root.u.def.section;
5911 sec->flags |= SEC_KEEP;
5912 }
5913}
5914
64d03ab5
AM
5915/* Mark sections containing dynamically referenced symbols. When
5916 building shared libraries, we must assume that any visible symbol is
5917 referenced. */
5918
0a1b45a2 5919static bool
64d03ab5
AM
5920ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5921{
5922 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5923 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5924 struct ppc_link_hash_entry *fdh;
b407645f 5925 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5926
64d03ab5 5927 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5928 fdh = defined_func_desc (eh);
5929 if (fdh != NULL)
5930 eh = fdh;
64d03ab5
AM
5931
5932 if ((eh->elf.root.type == bfd_link_hash_defined
5933 || eh->elf.root.type == bfd_link_hash_defweak)
b80e421f
AM
5934 && (!eh->elf.start_stop
5935 || eh->elf.root.ldscript_def
5936 || !info->start_stop_gc)
87e79a65 5937 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5938 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5939 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5940 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5941 && (!bfd_link_executable (info)
e278ae05 5942 || info->gc_keep_exported
b407645f
AM
5943 || info->export_dynamic
5944 || (eh->elf.dynamic
5945 && d != NULL
2cdcc330
AM
5946 && (*d->match) (&d->head, NULL,
5947 eh->elf.root.root.string)))
e278ae05 5948 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5949 || !bfd_hide_sym_by_version (info->version_info,
5950 eh->elf.root.root.string)))))
64d03ab5
AM
5951 {
5952 asection *code_sec;
b31867b6 5953 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5954
5955 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5956
5957 /* Function descriptor syms cause the associated
5958 function code sym section to be marked. */
b31867b6
AM
5959 fh = defined_code_entry (eh);
5960 if (fh != NULL)
5961 {
5962 code_sec = fh->elf.root.u.def.section;
5963 code_sec->flags |= SEC_KEEP;
5964 }
64d03ab5
AM
5965 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5966 && opd_entry_value (eh->elf.root.u.def.section,
5967 eh->elf.root.u.def.value,
0a1b45a2 5968 &code_sec, NULL, false) != (bfd_vma) -1)
64d03ab5
AM
5969 code_sec->flags |= SEC_KEEP;
5970 }
5971
0a1b45a2 5972 return true;
64d03ab5
AM
5973}
5974
5bd4f169
AM
5975/* Return the section that should be marked against GC for a given
5976 relocation. */
5977
5978static asection *
4ce794b7 5979ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5980 struct bfd_link_info *info,
4ce794b7
AM
5981 Elf_Internal_Rela *rel,
5982 struct elf_link_hash_entry *h,
5983 Elf_Internal_Sym *sym)
5bd4f169 5984{
ccfa59ea
AM
5985 asection *rsec;
5986
ccfa59ea
AM
5987 /* Syms return NULL if we're marking .opd, so we avoid marking all
5988 function sections, as all functions are referenced in .opd. */
5989 rsec = NULL;
5990 if (get_opd_info (sec) != NULL)
5991 return rsec;
1e2f5b6e 5992
5bd4f169
AM
5993 if (h != NULL)
5994 {
04c9666a 5995 enum elf_ppc64_reloc_type r_type;
b31867b6 5996 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5997
4ce794b7 5998 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5999 switch (r_type)
5bd4f169
AM
6000 {
6001 case R_PPC64_GNU_VTINHERIT:
6002 case R_PPC64_GNU_VTENTRY:
6003 break;
6004
6005 default:
6006 switch (h->root.type)
6007 {
6008 case bfd_link_hash_defined:
6009 case bfd_link_hash_defweak:
ed7007c1 6010 eh = ppc_elf_hash_entry (h);
b31867b6
AM
6011 fdh = defined_func_desc (eh);
6012 if (fdh != NULL)
8c5b4e52
AM
6013 {
6014 /* -mcall-aixdesc code references the dot-symbol on
6015 a call reloc. Mark the function descriptor too
6016 against garbage collection. */
6017 fdh->elf.mark = 1;
60d67dc8
AM
6018 if (fdh->elf.is_weakalias)
6019 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
6020 eh = fdh;
6021 }
1e2f5b6e
AM
6022
6023 /* Function descriptor syms cause the associated
6024 function code sym section to be marked. */
b31867b6
AM
6025 fh = defined_code_entry (eh);
6026 if (fh != NULL)
ccfa59ea
AM
6027 {
6028 /* They also mark their opd section. */
74f0fb50 6029 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6030
b31867b6 6031 rsec = fh->elf.root.u.def.section;
ccfa59ea 6032 }
8387904d
AM
6033 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6034 && opd_entry_value (eh->elf.root.u.def.section,
6035 eh->elf.root.u.def.value,
0a1b45a2 6036 &rsec, NULL, false) != (bfd_vma) -1)
74f0fb50 6037 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6038 else
1e2f5b6e
AM
6039 rsec = h->root.u.def.section;
6040 break;
5bd4f169
AM
6041
6042 case bfd_link_hash_common:
1e2f5b6e
AM
6043 rsec = h->root.u.c.p->section;
6044 break;
5bd4f169
AM
6045
6046 default:
fb34365b 6047 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6048 }
6049 }
6050 }
6051 else
6052 {
74f0fb50 6053 struct _opd_sec_data *opd;
1e2f5b6e
AM
6054
6055 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6056 opd = get_opd_info (rsec);
6057 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6058 {
74f0fb50 6059 rsec->gc_mark = 1;
ccfa59ea 6060
51aecdc5 6061 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 6062 }
5bd4f169
AM
6063 }
6064
1e2f5b6e 6065 return rsec;
5bd4f169
AM
6066}
6067
deb0e272
AM
6068/* The maximum size of .sfpr. */
6069#define SFPR_MAX (218*4)
6070
6071struct sfpr_def_parms
6072{
699733f6
AM
6073 const char name[12];
6074 unsigned char lo, hi;
2cdcc330
AM
6075 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6076 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
6077};
6078
a4b6fadd
AM
6079/* Auto-generate _save*, _rest* functions in .sfpr.
6080 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6081 instead. */
deb0e272 6082
0a1b45a2 6083static bool
a4b6fadd
AM
6084sfpr_define (struct bfd_link_info *info,
6085 const struct sfpr_def_parms *parm,
6086 asection *stub_sec)
deb0e272
AM
6087{
6088 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6089 unsigned int i;
6090 size_t len = strlen (parm->name);
0a1b45a2 6091 bool writing = false;
699733f6 6092 char sym[16];
deb0e272 6093
4dfe6ac6 6094 if (htab == NULL)
0a1b45a2 6095 return false;
4dfe6ac6 6096
deb0e272
AM
6097 memcpy (sym, parm->name, len);
6098 sym[len + 2] = 0;
6099
6100 for (i = parm->lo; i <= parm->hi; i++)
6101 {
a4b6fadd 6102 struct ppc_link_hash_entry *h;
deb0e272
AM
6103
6104 sym[len + 0] = i / 10 + '0';
6105 sym[len + 1] = i % 10 + '0';
ed7007c1 6106 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
0a1b45a2 6107 writing, true, true));
a4b6fadd 6108 if (stub_sec != NULL)
deb0e272 6109 {
a4b6fadd
AM
6110 if (h != NULL
6111 && h->elf.root.type == bfd_link_hash_defined
6112 && h->elf.root.u.def.section == htab->sfpr)
6113 {
6114 struct elf_link_hash_entry *s;
6115 char buf[32];
6116 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
0a1b45a2 6117 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
a4b6fadd 6118 if (s == NULL)
0a1b45a2 6119 return false;
779f2ae7 6120 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
6121 {
6122 s->root.type = bfd_link_hash_defined;
6123 s->root.u.def.section = stub_sec;
7dda8d3c 6124 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
6125 + h->elf.root.u.def.value);
6126 s->ref_regular = 1;
6127 s->def_regular = 1;
6128 s->ref_regular_nonweak = 1;
6129 s->forced_local = 1;
6130 s->non_elf = 0;
6131 s->root.linker_def = 1;
6132 }
6133 }
6134 continue;
6135 }
6136 if (h != NULL)
6137 {
6138 h->save_res = 1;
6139 if (!h->elf.def_regular)
deb0e272 6140 {
a4b6fadd
AM
6141 h->elf.root.type = bfd_link_hash_defined;
6142 h->elf.root.u.def.section = htab->sfpr;
6143 h->elf.root.u.def.value = htab->sfpr->size;
6144 h->elf.type = STT_FUNC;
6145 h->elf.def_regular = 1;
b32547cd 6146 h->elf.non_elf = 0;
0a1b45a2
AM
6147 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6148 writing = true;
deb0e272 6149 if (htab->sfpr->contents == NULL)
a4b6fadd 6150 {
2cdcc330
AM
6151 htab->sfpr->contents
6152 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd 6153 if (htab->sfpr->contents == NULL)
0a1b45a2 6154 return false;
a4b6fadd 6155 }
deb0e272
AM
6156 }
6157 }
6158 if (writing)
6159 {
6160 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6161 if (i != parm->hi)
6162 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6163 else
6164 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6165 htab->sfpr->size = p - htab->sfpr->contents;
6166 }
6167 }
6168
0a1b45a2 6169 return true;
deb0e272
AM
6170}
6171
6172static bfd_byte *
6173savegpr0 (bfd *abfd, bfd_byte *p, int r)
6174{
6175 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6176 return p + 4;
6177}
6178
6179static bfd_byte *
6180savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6181{
6182 p = savegpr0 (abfd, p, r);
a078d95a 6183 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6184 p = p + 4;
6185 bfd_put_32 (abfd, BLR, p);
6186 return p + 4;
6187}
6188
6189static bfd_byte *
6190restgpr0 (bfd *abfd, bfd_byte *p, int r)
6191{
6192 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6193 return p + 4;
6194}
6195
6196static bfd_byte *
6197restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6198{
a078d95a 6199 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6200 p = p + 4;
6201 p = restgpr0 (abfd, p, r);
6202 bfd_put_32 (abfd, MTLR_R0, p);
6203 p = p + 4;
6204 if (r == 29)
6205 {
6206 p = restgpr0 (abfd, p, 30);
6207 p = restgpr0 (abfd, p, 31);
6208 }
6209 bfd_put_32 (abfd, BLR, p);
6210 return p + 4;
6211}
6212
6213static bfd_byte *
6214savegpr1 (bfd *abfd, bfd_byte *p, int r)
6215{
6216 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6217 return p + 4;
6218}
6219
6220static bfd_byte *
6221savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6222{
6223 p = savegpr1 (abfd, p, r);
6224 bfd_put_32 (abfd, BLR, p);
6225 return p + 4;
6226}
6227
6228static bfd_byte *
6229restgpr1 (bfd *abfd, bfd_byte *p, int r)
6230{
6231 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6232 return p + 4;
6233}
6234
6235static bfd_byte *
6236restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6237{
6238 p = restgpr1 (abfd, p, r);
6239 bfd_put_32 (abfd, BLR, p);
6240 return p + 4;
6241}
6242
6243static bfd_byte *
6244savefpr (bfd *abfd, bfd_byte *p, int r)
6245{
6246 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6247 return p + 4;
6248}
6249
6250static bfd_byte *
6251savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6252{
6253 p = savefpr (abfd, p, r);
a078d95a 6254 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6255 p = p + 4;
6256 bfd_put_32 (abfd, BLR, p);
6257 return p + 4;
6258}
6259
6260static bfd_byte *
6261restfpr (bfd *abfd, bfd_byte *p, int r)
6262{
6263 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6264 return p + 4;
6265}
6266
6267static bfd_byte *
6268restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6269{
a078d95a 6270 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6271 p = p + 4;
6272 p = restfpr (abfd, p, r);
6273 bfd_put_32 (abfd, MTLR_R0, p);
6274 p = p + 4;
6275 if (r == 29)
6276 {
6277 p = restfpr (abfd, p, 30);
6278 p = restfpr (abfd, p, 31);
6279 }
6280 bfd_put_32 (abfd, BLR, p);
6281 return p + 4;
6282}
6283
6284static bfd_byte *
6285savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6286{
6287 p = savefpr (abfd, p, r);
6288 bfd_put_32 (abfd, BLR, p);
6289 return p + 4;
6290}
6291
6292static bfd_byte *
6293restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6294{
6295 p = restfpr (abfd, p, r);
6296 bfd_put_32 (abfd, BLR, p);
6297 return p + 4;
6298}
6299
6300static bfd_byte *
6301savevr (bfd *abfd, bfd_byte *p, int r)
6302{
6303 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6304 p = p + 4;
6305 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6306 return p + 4;
6307}
6308
6309static bfd_byte *
6310savevr_tail (bfd *abfd, bfd_byte *p, int r)
6311{
6312 p = savevr (abfd, p, r);
6313 bfd_put_32 (abfd, BLR, p);
6314 return p + 4;
6315}
6316
6317static bfd_byte *
6318restvr (bfd *abfd, bfd_byte *p, int r)
6319{
6320 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6321 p = p + 4;
6322 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6323 return p + 4;
6324}
6325
6326static bfd_byte *
6327restvr_tail (bfd *abfd, bfd_byte *p, int r)
6328{
6329 p = restvr (abfd, p, r);
6330 bfd_put_32 (abfd, BLR, p);
6331 return p + 4;
6332}
6333
9e7028aa
AM
6334#define STDU_R1_0R1 0xf8210001
6335#define ADDI_R1_R1 0x38210000
6336
6337/* Emit prologue of wrapper preserving regs around a call to
6338 __tls_get_addr_opt. */
6339
6340static bfd_byte *
6341tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6342{
6343 unsigned int i;
6344
6345 bfd_put_32 (obfd, MFLR_R0, p);
6346 p += 4;
6347 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6348 p += 4;
6349
6350 if (htab->opd_abi)
6351 {
6352 for (i = 4; i < 12; i++)
6353 {
6354 bfd_put_32 (obfd,
6355 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6356 p += 4;
6357 }
6358 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6359 p += 4;
6360 }
6361 else
6362 {
6363 for (i = 4; i < 12; i++)
6364 {
6365 bfd_put_32 (obfd,
6366 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6367 p += 4;
6368 }
6369 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6370 p += 4;
6371 }
6372 return p;
6373}
6374
6375/* Emit epilogue of wrapper preserving regs around a call to
6376 __tls_get_addr_opt. */
6377
6378static bfd_byte *
6379tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6380{
6381 unsigned int i;
6382
6383 if (htab->opd_abi)
6384 {
6385 for (i = 4; i < 12; i++)
6386 {
6387 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6388 p += 4;
6389 }
6390 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6391 p += 4;
6392 }
6393 else
6394 {
6395 for (i = 4; i < 12; i++)
6396 {
6397 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6398 p += 4;
6399 }
6400 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6401 p += 4;
6402 }
6403 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6404 p += 4;
6405 bfd_put_32 (obfd, MTLR_R0, p);
6406 p += 4;
6407 bfd_put_32 (obfd, BLR, p);
6408 p += 4;
6409 return p;
6410}
6411
e86ce104
AM
6412/* Called via elf_link_hash_traverse to transfer dynamic linking
6413 information on function code symbol entries to their corresponding
a442059f
AM
6414 function descriptor symbol entries. Must not be called twice for
6415 any given code symbol. */
deb0e272 6416
0a1b45a2 6417static bool
4ce794b7 6418func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6419{
e86ce104 6420 struct bfd_link_info *info;
65f38f15 6421 struct ppc_link_hash_table *htab;
50bc7936
AM
6422 struct ppc_link_hash_entry *fh;
6423 struct ppc_link_hash_entry *fdh;
0a1b45a2 6424 bool force_local;
5bd4f169 6425
ed7007c1 6426 fh = ppc_elf_hash_entry (h);
50bc7936 6427 if (fh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 6428 return true;
e86ce104 6429
8c5b4e52 6430 if (!fh->is_func)
0a1b45a2 6431 return true;
8c5b4e52
AM
6432
6433 if (fh->elf.root.root.string[0] != '.'
6434 || fh->elf.root.root.string[1] == '\0')
0a1b45a2 6435 return true;
8c5b4e52 6436
4ce794b7 6437 info = inf;
65f38f15 6438 htab = ppc_hash_table (info);
4dfe6ac6 6439 if (htab == NULL)
0a1b45a2 6440 return false;
5bd4f169 6441
8c5b4e52
AM
6442 /* Find the corresponding function descriptor symbol. */
6443 fdh = lookup_fdh (fh, htab);
6444
c09bdfe5
AM
6445 /* Resolve undefined references to dot-symbols as the value
6446 in the function descriptor, if we have one in a regular object.
6447 This is to satisfy cases like ".quad .foo". Calls to functions
6448 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6449 if ((fh->elf.root.type == bfd_link_hash_undefined
6450 || fh->elf.root.type == bfd_link_hash_undefweak)
6451 && (fdh->elf.root.type == bfd_link_hash_defined
6452 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6453 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6454 && opd_entry_value (fdh->elf.root.u.def.section,
6455 fdh->elf.root.u.def.value,
c09bdfe5 6456 &fh->elf.root.u.def.section,
0a1b45a2 6457 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
c09bdfe5 6458 {
b31867b6 6459 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6460 fh->elf.forced_local = 1;
b31867b6
AM
6461 fh->elf.def_regular = fdh->elf.def_regular;
6462 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6463 }
6464
8c5b4e52
AM
6465 if (!fh->elf.dynamic)
6466 {
6467 struct plt_entry *ent;
5bd4f169 6468
8c5b4e52
AM
6469 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6470 if (ent->plt.refcount > 0)
6471 break;
6472 if (ent == NULL)
a442059f
AM
6473 {
6474 if (fdh != NULL && fdh->fake)
6475 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6476 return true;
6477 }
8c5b4e52 6478 }
5bd4f169 6479
8c5b4e52 6480 /* Create a descriptor as undefined if necessary. */
50bc7936 6481 if (fdh == NULL
0e1862bb 6482 && !bfd_link_executable (info)
50bc7936
AM
6483 && (fh->elf.root.type == bfd_link_hash_undefined
6484 || fh->elf.root.type == bfd_link_hash_undefweak))
6485 {
908b32fc 6486 fdh = make_fdh (info, fh);
bb700d78 6487 if (fdh == NULL)
0a1b45a2 6488 return false;
50bc7936 6489 }
648cca2c 6490
8c5b4e52 6491 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6492 if (fdh != NULL
6493 && fdh->fake
8c5b4e52
AM
6494 && (fh->elf.root.type == bfd_link_hash_defined
6495 || fh->elf.root.type == bfd_link_hash_defweak))
0a1b45a2 6496 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
908b32fc 6497
8c5b4e52
AM
6498 /* Transfer dynamic linking information to the function descriptor. */
6499 if (fdh != NULL)
6500 {
f5385ebf
AM
6501 fdh->elf.ref_regular |= fh->elf.ref_regular;
6502 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6503 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6504 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6505 fdh->elf.dynamic |= fh->elf.dynamic;
6506 fdh->elf.needs_plt |= (fh->elf.needs_plt
6507 || fh->elf.type == STT_FUNC
6508 || fh->elf.type == STT_GNU_IFUNC);
6509 move_plt_plist (fh, fdh);
6510
6511 if (!fdh->elf.forced_local
6512 && fh->elf.dynindx != -1)
6513 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 6514 return false;
e86ce104
AM
6515 }
6516
50bc7936
AM
6517 /* Now that the info is on the function descriptor, clear the
6518 function code sym info. Any function code syms for which we
6519 don't have a definition in a regular file, we force local.
6520 This prevents a shared library from exporting syms that have
6521 been imported from another library. Function code syms that
6522 are really in the library we must leave global to prevent the
6523 linker dragging in a definition from a static library. */
93f3fa99
AM
6524 force_local = (!fh->elf.def_regular
6525 || fdh == NULL
6526 || !fdh->elf.def_regular
6527 || fdh->elf.forced_local);
50bc7936
AM
6528 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6529
0a1b45a2 6530 return true;
e86ce104 6531}
40b8271b 6532
a4b6fadd
AM
6533static const struct sfpr_def_parms save_res_funcs[] =
6534 {
6535 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6536 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6537 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6538 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6539 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6540 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6541 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6542 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6543 { "._savef", 14, 31, savefpr, savefpr1_tail },
6544 { "._restf", 14, 31, restfpr, restfpr1_tail },
6545 { "_savevr_", 20, 31, savevr, savevr_tail },
6546 { "_restvr_", 20, 31, restvr, restvr_tail }
6547 };
6548
e86ce104 6549/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6550 this hook to a) run the edit functions in this file, b) provide
6551 some gcc support functions, and c) transfer dynamic linking
6552 information gathered so far on function code symbol entries, to
6553 their corresponding function descriptor symbol entries. */
deb0e272 6554
0a1b45a2 6555static bool
c9405344 6556ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6557{
6558 struct ppc_link_hash_table *htab;
6559
6560 htab = ppc_hash_table (info);
4dfe6ac6 6561 if (htab == NULL)
0a1b45a2 6562 return false;
4dfe6ac6 6563
c9405344
AM
6564 /* Call back into the linker, which then runs the edit functions. */
6565 htab->params->edit ();
6566
b32547cd
AM
6567 /* Provide any missing _save* and _rest* functions. */
6568 if (htab->sfpr != NULL)
6569 {
6570 unsigned int i;
6571
6572 htab->sfpr->size = 0;
6573 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6574 if (!sfpr_define (info, &save_res_funcs[i], NULL))
0a1b45a2 6575 return false;
b32547cd
AM
6576 if (htab->sfpr->size == 0)
6577 htab->sfpr->flags |= SEC_EXCLUDE;
6578 }
6579
6580 if (bfd_link_relocatable (info))
0a1b45a2 6581 return true;
b32547cd
AM
6582
6583 if (htab->elf.hgot != NULL)
dba6fa9b 6584 {
0a1b45a2 6585 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
dba6fa9b
AM
6586 /* Make .TOC. defined so as to prevent it being made dynamic.
6587 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6588 if (!htab->elf.hgot->def_regular
6589 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6590 {
6591 htab->elf.hgot->root.type = bfd_link_hash_defined;
6592 htab->elf.hgot->root.u.def.value = 0;
6593 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6594 htab->elf.hgot->def_regular = 1;
6595 htab->elf.hgot->root.linker_def = 1;
6596 }
dba6fa9b 6597 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6598 htab->elf.hgot->other
6599 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6600 }
c66bb0ee 6601
0a1b45a2 6602 return true;
e86ce104
AM
6603}
6604
d311bc8b 6605/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6606 aliases, that apply to read-only sections. Cannot be used after
6607 size_dynamic_sections. */
d311bc8b 6608
0a1b45a2 6609static bool
d311bc8b
AM
6610alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6611{
ed7007c1 6612 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6613 do
6614 {
5dbc8b37 6615 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
0a1b45a2 6616 return true;
ed7007c1 6617 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6618 }
6619 while (eh != NULL && &eh->elf != h);
d311bc8b 6620
0a1b45a2 6621 return false;
d311bc8b 6622}
8a2058b5 6623
8a9e8e72
AM
6624/* Return whether EH has pc-relative dynamic relocs. */
6625
0a1b45a2 6626static bool
8a9e8e72
AM
6627pc_dynrelocs (struct ppc_link_hash_entry *eh)
6628{
1657026c 6629 struct ppc_dyn_relocs *p;
8a9e8e72 6630
1657026c 6631 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72 6632 if (p->pc_count != 0)
0a1b45a2
AM
6633 return true;
6634 return false;
8a9e8e72
AM
6635}
6636
8a2058b5
AM
6637/* Return true if a global entry stub will be created for H. Valid
6638 for ELFv2 before plt entries have been allocated. */
6639
0a1b45a2 6640static bool
8a2058b5
AM
6641global_entry_stub (struct elf_link_hash_entry *h)
6642{
6643 struct plt_entry *pent;
6644
6645 if (!h->pointer_equality_needed
6646 || h->def_regular)
0a1b45a2 6647 return false;
8a2058b5
AM
6648
6649 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6650 if (pent->plt.refcount > 0
6651 && pent->addend == 0)
0a1b45a2 6652 return true;
8a2058b5 6653
0a1b45a2 6654 return false;
8a2058b5
AM
6655}
6656
e86ce104
AM
6657/* Adjust a symbol defined by a dynamic object and referenced by a
6658 regular object. The current definition is in some section of the
6659 dynamic object, but we're not including those sections. We have to
6660 change the definition to something the rest of the link can
6661 understand. */
6662
0a1b45a2 6663static bool
4ce794b7
AM
6664ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6665 struct elf_link_hash_entry *h)
e86ce104
AM
6666{
6667 struct ppc_link_hash_table *htab;
5474d94f 6668 asection *s, *srel;
e86ce104
AM
6669
6670 htab = ppc_hash_table (info);
4dfe6ac6 6671 if (htab == NULL)
0a1b45a2 6672 return false;
e86ce104
AM
6673
6674 /* Deal with function syms. */
6675 if (h->type == STT_FUNC
e054468f 6676 || h->type == STT_GNU_IFUNC
f5385ebf 6677 || h->needs_plt)
e86ce104 6678 {
0a1b45a2
AM
6679 bool local = (ppc_elf_hash_entry (h)->save_res
6680 || SYMBOL_CALLS_LOCAL (info, h)
6681 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
529fe20e
AM
6682 /* Discard dyn_relocs when non-pic if we've decided that a
6683 function symbol is local and not an ifunc. We keep dynamic
6684 relocs for ifuncs when local rather than always emitting a
6685 plt call stub for them and defining the symbol on the call
6686 stub. We can't do that for ELFv1 anyway (a function symbol
6687 is defined on a descriptor, not code) and it can be faster at
6688 run-time due to not needing to bounce through a stub. The
6689 dyn_relocs for ifuncs will be applied even in a static
6690 executable. */
6691 if (!bfd_link_pic (info)
6692 && h->type != STT_GNU_IFUNC
6693 && local)
190eb1dd 6694 h->dyn_relocs = NULL;
529fe20e 6695
e86ce104
AM
6696 /* Clear procedure linkage table information for any symbol that
6697 won't need a .plt entry. */
411e1bfb
AM
6698 struct plt_entry *ent;
6699 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6700 if (ent->plt.refcount > 0)
6701 break;
8387904d 6702 if (ent == NULL
2d7ad24e
AM
6703 || (h->type != STT_GNU_IFUNC
6704 && local
3e04d765 6705 && (htab->can_convert_all_inline_plt
ed7007c1 6706 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6707 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6708 {
411e1bfb 6709 h->plt.plist = NULL;
f5385ebf 6710 h->needs_plt = 0;
d1eca1e4 6711 h->pointer_equality_needed = 0;
40b8271b 6712 }
8a2058b5 6713 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6714 {
d1eca1e4
AM
6715 /* Taking a function's address in a read/write section
6716 doesn't require us to define the function symbol in the
6717 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6718 be used instead. The reason we prefer a few more dynamic
6719 relocs is that calling via a global entry stub costs a
6720 few more instructions, and pointer_equality_needed causes
6721 extra work in ld.so when resolving these symbols. */
529fe20e 6722 if (global_entry_stub (h))
d1eca1e4 6723 {
5dbc8b37 6724 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6725 {
6726 h->pointer_equality_needed = 0;
04383fd1
AM
6727 /* If we haven't seen a branch reloc and the symbol
6728 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6729 if (!h->needs_plt)
6730 h->plt.plist = NULL;
6731 }
6732 else if (!bfd_link_pic (info))
6733 /* We are going to be defining the function symbol on the
6734 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6735 h->dyn_relocs = NULL;
d1eca1e4
AM
6736 }
6737
3988aed5 6738 /* ELFv2 function symbols can't have copy relocs. */
0a1b45a2 6739 return true;
3988aed5
AM
6740 }
6741 else if (!h->needs_plt
5dbc8b37 6742 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6743 {
04383fd1
AM
6744 /* If we haven't seen a branch reloc and the symbol isn't an
6745 ifunc then we don't need a plt entry. */
3988aed5
AM
6746 h->plt.plist = NULL;
6747 h->pointer_equality_needed = 0;
0a1b45a2 6748 return true;
a345bc8d 6749 }
5bd4f169 6750 }
bbd7ec4a 6751 else
411e1bfb 6752 h->plt.plist = NULL;
5bd4f169
AM
6753
6754 /* If this is a weak symbol, and there is a real definition, the
6755 processor independent code will have arranged for us to see the
6756 real definition first, and we can just use the same value. */
60d67dc8 6757 if (h->is_weakalias)
5bd4f169 6758 {
60d67dc8
AM
6759 struct elf_link_hash_entry *def = weakdef (h);
6760 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6761 h->root.u.def.section = def->root.u.def.section;
6762 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6763 if (def->root.u.def.section == htab->elf.sdynbss
6764 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6765 h->dyn_relocs = NULL;
0a1b45a2 6766 return true;
5bd4f169
AM
6767 }
6768
5bd4f169
AM
6769 /* If we are creating a shared library, we must presume that the
6770 only references to the symbol are via the global offset table.
6771 For such cases we need not do anything here; the relocations will
6772 be handled correctly by relocate_section. */
ec73ddcd 6773 if (!bfd_link_executable (info))
0a1b45a2 6774 return true;
5bd4f169 6775
65f38f15
AM
6776 /* If there are no references to this symbol that do not use the
6777 GOT, we don't need to generate a copy reloc. */
f5385ebf 6778 if (!h->non_got_ref)
0a1b45a2 6779 return true;
65f38f15 6780
b186458a 6781 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6782 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6783
d93d1c80
AM
6784 /* If -z nocopyreloc was given, don't generate them either. */
6785 || info->nocopyreloc
a127494f 6786
dce2246a 6787 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6788 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6789 || (ELIMINATE_COPY_RELOCS
6790 && !h->needs_copy
6791 && !alias_readonly_dynrelocs (h))
65f38f15 6792
d93d1c80
AM
6793 /* Protected variables do not work with .dynbss. The copy in
6794 .dynbss won't be used by the shared library with the protected
6795 definition for the variable. Text relocations are preferable
6796 to an incorrect program. */
6797 || h->protected_def)
0a1b45a2 6798 return true;
a127494f 6799
e1c6cf61
AM
6800 if (h->type == STT_FUNC
6801 || h->type == STT_GNU_IFUNC)
97b639ba 6802 {
e1c6cf61
AM
6803 /* .dynbss copies of function symbols only work if we have
6804 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6805 use dot-symbols and set the function symbol size to the text
6806 size of the function rather than the size of the descriptor.
6807 That's wrong for copying a descriptor. */
ed7007c1 6808 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61 6809 || !(h->size == 24 || h->size == 16))
0a1b45a2 6810 return true;
e1c6cf61
AM
6811
6812 /* We should never get here, but unfortunately there are old
6813 versions of gcc (circa gcc-3.2) that improperly for the
6814 ELFv1 ABI put initialized function pointers, vtable refs and
6815 suchlike in read-only sections. Allow them to proceed, but
6816 warn that this might break at runtime. */
25f53a85 6817 info->callbacks->einfo
c1c8c1ef 6818 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6819 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6820 h->root.root.string);
6821 }
5d35169e
AM
6822
6823 /* This is a reference to a symbol defined by a dynamic object which
6824 is not a function. */
6825
5bd4f169
AM
6826 /* We must allocate the symbol in our .dynbss section, which will
6827 become part of the .bss section of the executable. There will be
6828 an entry for this symbol in the .dynsym section. The dynamic
6829 object will contain position independent code, so all references
6830 from the dynamic object to this symbol will go through the global
6831 offset table. The dynamic linker will use the .dynsym entry to
6832 determine the address it must put in the global offset table, so
6833 both the dynamic object and the regular object will refer to the
6834 same memory location for the variable. */
5474d94f
AM
6835 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6836 {
6837 s = htab->elf.sdynrelro;
6838 srel = htab->elf.sreldynrelro;
6839 }
6840 else
6841 {
6842 s = htab->elf.sdynbss;
6843 srel = htab->elf.srelbss;
6844 }
1d7e9d18 6845 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6846 {
4a7e5234
AM
6847 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6848 linker to copy the initial value out of the dynamic object
6849 and into the runtime process image. */
5474d94f 6850 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6851 h->needs_copy = 1;
5bd4f169
AM
6852 }
6853
529fe20e 6854 /* We no longer want dyn_relocs. */
190eb1dd 6855 h->dyn_relocs = NULL;
6cabe1ea 6856 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6857}
6858
e86ce104
AM
6859/* If given a function descriptor symbol, hide both the function code
6860 sym and the descriptor. */
6861static void
4ce794b7
AM
6862ppc64_elf_hide_symbol (struct bfd_link_info *info,
6863 struct elf_link_hash_entry *h,
0a1b45a2 6864 bool force_local)
e86ce104 6865{
34814b9f 6866 struct ppc_link_hash_entry *eh;
e86ce104
AM
6867 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6868
87469ba2
AM
6869 if (ppc_hash_table (info) == NULL)
6870 return;
6871
ed7007c1 6872 eh = ppc_elf_hash_entry (h);
34814b9f 6873 if (eh->is_func_descriptor)
e86ce104 6874 {
34814b9f 6875 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6876
721956f4 6877 if (fh == NULL)
d1329ca3
AM
6878 {
6879 const char *p, *q;
b8ac2841 6880 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6881 char save;
6882
6883 /* We aren't supposed to use alloca in BFD because on
6884 systems which do not have alloca the version in libiberty
6885 calls xmalloc, which might cause the program to crash
6886 when it runs out of memory. This function doesn't have a
6887 return status, so there's no way to gracefully return an
6888 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6889 accessed; It's either a string in an ELF string table,
6890 or allocated in an objalloc structure. */
d1329ca3 6891
34814b9f 6892 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6893 save = *p;
6894 *(char *) p = '.';
0a1b45a2
AM
6895 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6896 false, false));
d1329ca3
AM
6897 *(char *) p = save;
6898
6899 /* Unfortunately, if it so happens that the string we were
6900 looking for was allocated immediately before this string,
6901 then we overwrote the string terminator. That's the only
6902 reason the lookup should fail. */
6903 if (fh == NULL)
6904 {
34814b9f
AM
6905 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6906 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6907 --q, --p;
34814b9f 6908 if (q < eh->elf.root.root.string && *p == '.')
0a1b45a2
AM
6909 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6910 false, false));
d1329ca3
AM
6911 }
6912 if (fh != NULL)
6913 {
34814b9f
AM
6914 eh->oh = fh;
6915 fh->oh = eh;
d1329ca3
AM
6916 }
6917 }
e86ce104 6918 if (fh != NULL)
34814b9f 6919 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6920 }
6921}
6922
0a1b45a2 6923static bool
8843416a
AM
6924get_sym_h (struct elf_link_hash_entry **hp,
6925 Elf_Internal_Sym **symp,
6926 asection **symsecp,
f961d9dd 6927 unsigned char **tls_maskp,
8843416a
AM
6928 Elf_Internal_Sym **locsymsp,
6929 unsigned long r_symndx,
6930 bfd *ibfd)
411e1bfb 6931{
0ffa91dd 6932 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6933
6934 if (r_symndx >= symtab_hdr->sh_info)
6935 {
6936 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6937 struct elf_link_hash_entry *h;
6938
6939 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6940 h = elf_follow_link (h);
411e1bfb
AM
6941
6942 if (hp != NULL)
6943 *hp = h;
6944
6945 if (symp != NULL)
6946 *symp = NULL;
6947
6948 if (symsecp != NULL)
6949 {
6950 asection *symsec = NULL;
6951 if (h->root.type == bfd_link_hash_defined
6952 || h->root.type == bfd_link_hash_defweak)
6953 symsec = h->root.u.def.section;
6954 *symsecp = symsec;
6955 }
6956
e7b938ca 6957 if (tls_maskp != NULL)
ed7007c1 6958 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6959 }
6960 else
6961 {
6962 Elf_Internal_Sym *sym;
6963 Elf_Internal_Sym *locsyms = *locsymsp;
6964
6965 if (locsyms == NULL)
6966 {
6967 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6968 if (locsyms == NULL)
6969 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6970 symtab_hdr->sh_info,
6971 0, NULL, NULL, NULL);
6972 if (locsyms == NULL)
0a1b45a2 6973 return false;
411e1bfb
AM
6974 *locsymsp = locsyms;
6975 }
6976 sym = locsyms + r_symndx;
6977
6978 if (hp != NULL)
6979 *hp = NULL;
6980
6981 if (symp != NULL)
6982 *symp = sym;
6983
6984 if (symsecp != NULL)
cb33740c 6985 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6986
e7b938ca 6987 if (tls_maskp != NULL)
411e1bfb
AM
6988 {
6989 struct got_entry **lgot_ents;
f961d9dd 6990 unsigned char *tls_mask;
411e1bfb 6991
e7b938ca 6992 tls_mask = NULL;
411e1bfb
AM
6993 lgot_ents = elf_local_got_ents (ibfd);
6994 if (lgot_ents != NULL)
6995 {
e054468f
AM
6996 struct plt_entry **local_plt = (struct plt_entry **)
6997 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6998 unsigned char *lgot_masks = (unsigned char *)
e054468f 6999 (local_plt + symtab_hdr->sh_info);
e7b938ca 7000 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7001 }
e7b938ca 7002 *tls_maskp = tls_mask;
411e1bfb
AM
7003 }
7004 }
0a1b45a2 7005 return true;
411e1bfb
AM
7006}
7007
e7b938ca 7008/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7009 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7010 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7011
7012static int
f961d9dd 7013get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7014 unsigned long *toc_symndx,
7015 bfd_vma *toc_addend,
0d4792f7 7016 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7017 const Elf_Internal_Rela *rel,
7018 bfd *ibfd)
411e1bfb
AM
7019{
7020 unsigned long r_symndx;
0d4792f7 7021 int next_r;
411e1bfb
AM
7022 struct elf_link_hash_entry *h;
7023 Elf_Internal_Sym *sym;
7024 asection *sec;
7025 bfd_vma off;
7026
7027 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7028 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7029 return 0;
411e1bfb 7030
37da22e5
AM
7031 if ((*tls_maskp != NULL
7032 && (**tls_maskp & TLS_TLS) != 0
7033 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 7034 || sec == NULL
6bee8834 7035 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7036 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7037 return 1;
411e1bfb
AM
7038
7039 /* Look inside a TOC section too. */
7040 if (h != NULL)
7041 {
7042 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7043 off = h->root.u.def.value;
7044 }
7045 else
7046 off = sym->st_value;
7047 off += rel->r_addend;
7048 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7049 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7050 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7051 if (toc_symndx != NULL)
7052 *toc_symndx = r_symndx;
3a71aa26
AM
7053 if (toc_addend != NULL)
7054 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7055 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7056 return 0;
854b41e7 7057 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7058 && (next_r == -1 || next_r == -2))
7059 return 1 - next_r;
951fd09b 7060 return 1;
411e1bfb
AM
7061}
7062
3b421ab3
AM
7063/* Find (or create) an entry in the tocsave hash table. */
7064
7065static struct tocsave_entry *
7066tocsave_find (struct ppc_link_hash_table *htab,
7067 enum insert_option insert,
7068 Elf_Internal_Sym **local_syms,
7069 const Elf_Internal_Rela *irela,
7070 bfd *ibfd)
7071{
7072 unsigned long r_indx;
7073 struct elf_link_hash_entry *h;
7074 Elf_Internal_Sym *sym;
7075 struct tocsave_entry ent, *p;
7076 hashval_t hash;
7077 struct tocsave_entry **slot;
7078
7079 r_indx = ELF64_R_SYM (irela->r_info);
7080 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7081 return NULL;
7082 if (ent.sec == NULL || ent.sec->output_section == NULL)
7083 {
4eca0228 7084 _bfd_error_handler
871b3ab2 7085 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
7086 return NULL;
7087 }
7088
7089 if (h != NULL)
7090 ent.offset = h->root.u.def.value;
7091 else
7092 ent.offset = sym->st_value;
7093 ent.offset += irela->r_addend;
7094
7095 hash = tocsave_htab_hash (&ent);
7096 slot = ((struct tocsave_entry **)
7097 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7098 if (slot == NULL)
7099 return NULL;
7100
7101 if (*slot == NULL)
7102 {
7103 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7104 if (p == NULL)
7105 return NULL;
7106 *p = ent;
7107 *slot = p;
7108 }
7109 return *slot;
7110}
7111
754021d0 7112/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7113 code for the old ABI, these will already have been done. */
754021d0 7114
0a1b45a2 7115static bool
754021d0
AM
7116adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7117{
7118 struct ppc_link_hash_entry *eh;
7119 asection *sym_sec;
74f0fb50 7120 struct _opd_sec_data *opd;
754021d0
AM
7121
7122 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 7123 return true;
754021d0 7124
754021d0
AM
7125 if (h->root.type != bfd_link_hash_defined
7126 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 7127 return true;
754021d0 7128
ed7007c1 7129 eh = ppc_elf_hash_entry (h);
754021d0 7130 if (eh->adjust_done)
0a1b45a2 7131 return true;
754021d0
AM
7132
7133 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7134 opd = get_opd_info (sym_sec);
7135 if (opd != NULL && opd->adjust != NULL)
754021d0 7136 {
51aecdc5 7137 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
7138 if (adjust == -1)
7139 {
7140 /* This entry has been deleted. */
b3fac117 7141 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7142 if (dsec == NULL)
7143 {
7144 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7145 if (discarded_section (dsec))
81688140 7146 {
b3fac117 7147 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7148 break;
7149 }
7150 }
4025353c 7151 eh->elf.root.u.def.value = 0;
81688140 7152 eh->elf.root.u.def.section = dsec;
4025353c
AM
7153 }
7154 else
7155 eh->elf.root.u.def.value += adjust;
754021d0
AM
7156 eh->adjust_done = 1;
7157 }
0a1b45a2 7158 return true;
754021d0
AM
7159}
7160
8c1d1bb8 7161/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7162 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7163 have already been determined. */
7164
0a1b45a2 7165static bool
1657026c 7166dec_dynrel_count (const Elf_Internal_Rela *rel,
8c1d1bb8
AM
7167 asection *sec,
7168 struct bfd_link_info *info,
7169 Elf_Internal_Sym **local_syms,
7170 struct elf_link_hash_entry *h,
19e08130 7171 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7172{
7173 enum elf_ppc64_reloc_type r_type;
19e08130 7174 asection *sym_sec = NULL;
8c1d1bb8
AM
7175
7176 /* Can this reloc be dynamic? This switch, and later tests here
7177 should be kept in sync with the code in check_relocs. */
1657026c 7178 r_type = ELF64_R_TYPE (rel->r_info);
8c1d1bb8
AM
7179 switch (r_type)
7180 {
7181 default:
0a1b45a2 7182 return true;
8c1d1bb8 7183
1bdd8fac
AM
7184 case R_PPC64_TOC16:
7185 case R_PPC64_TOC16_DS:
7186 case R_PPC64_TOC16_LO:
7187 case R_PPC64_TOC16_HI:
7188 case R_PPC64_TOC16_HA:
7189 case R_PPC64_TOC16_LO_DS:
7190 if (h == NULL)
0a1b45a2 7191 return true;
1bdd8fac
AM
7192 break;
7193
8c1d1bb8
AM
7194 case R_PPC64_TPREL16:
7195 case R_PPC64_TPREL16_LO:
7196 case R_PPC64_TPREL16_HI:
7197 case R_PPC64_TPREL16_HA:
7198 case R_PPC64_TPREL16_DS:
7199 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7200 case R_PPC64_TPREL16_HIGH:
7201 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7202 case R_PPC64_TPREL16_HIGHER:
7203 case R_PPC64_TPREL16_HIGHERA:
7204 case R_PPC64_TPREL16_HIGHEST:
7205 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7206 case R_PPC64_TPREL64:
c213164a 7207 case R_PPC64_TPREL34:
8c1d1bb8
AM
7208 case R_PPC64_DTPMOD64:
7209 case R_PPC64_DTPREL64:
7210 case R_PPC64_ADDR64:
7211 case R_PPC64_REL30:
7212 case R_PPC64_REL32:
7213 case R_PPC64_REL64:
7214 case R_PPC64_ADDR14:
7215 case R_PPC64_ADDR14_BRNTAKEN:
7216 case R_PPC64_ADDR14_BRTAKEN:
7217 case R_PPC64_ADDR16:
7218 case R_PPC64_ADDR16_DS:
7219 case R_PPC64_ADDR16_HA:
7220 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7221 case R_PPC64_ADDR16_HIGH:
7222 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7223 case R_PPC64_ADDR16_HIGHER:
7224 case R_PPC64_ADDR16_HIGHERA:
7225 case R_PPC64_ADDR16_HIGHEST:
7226 case R_PPC64_ADDR16_HIGHESTA:
7227 case R_PPC64_ADDR16_LO:
7228 case R_PPC64_ADDR16_LO_DS:
7229 case R_PPC64_ADDR24:
7230 case R_PPC64_ADDR32:
7231 case R_PPC64_UADDR16:
7232 case R_PPC64_UADDR32:
7233 case R_PPC64_UADDR64:
7234 case R_PPC64_TOC:
5663e321
AM
7235 case R_PPC64_D34:
7236 case R_PPC64_D34_LO:
7237 case R_PPC64_D34_HI30:
7238 case R_PPC64_D34_HA30:
7239 case R_PPC64_ADDR16_HIGHER34:
7240 case R_PPC64_ADDR16_HIGHERA34:
7241 case R_PPC64_ADDR16_HIGHEST34:
7242 case R_PPC64_ADDR16_HIGHESTA34:
7243 case R_PPC64_D28:
8c1d1bb8
AM
7244 break;
7245 }
7246
7247 if (local_syms != NULL)
7248 {
7249 unsigned long r_symndx;
8c1d1bb8
AM
7250 bfd *ibfd = sec->owner;
7251
1657026c 7252 r_symndx = ELF64_R_SYM (rel->r_info);
8c1d1bb8 7253 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
0a1b45a2 7254 return false;
8c1d1bb8
AM
7255 }
7256
ec73ddcd
AM
7257 if ((h != NULL
7258 && (h->root.type == bfd_link_hash_defweak
7259 || !h->def_regular))
7260 || (h != NULL
7261 && !bfd_link_executable (info)
7262 && !SYMBOLIC_BIND (info, h))
7263 || (bfd_link_pic (info)
7264 && must_be_dyn_reloc (info, r_type))
7265 || (!bfd_link_pic (info)
7266 && (h != NULL
7267 ? h->type == STT_GNU_IFUNC
7268 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7269 ;
7270 else
0a1b45a2 7271 return true;
8c1d1bb8
AM
7272
7273 if (h != NULL)
6edfbbad 7274 {
1657026c
AM
7275 struct ppc_dyn_relocs *p;
7276 struct ppc_dyn_relocs **pp;
7277 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
19e08130
AM
7278
7279 /* elf_gc_sweep may have already removed all dyn relocs associated
7280 with local syms for a given section. Also, symbol flags are
7281 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7282 report a dynreloc miscount. */
7283 if (*pp == NULL && info->gc_sections)
0a1b45a2 7284 return true;
19e08130
AM
7285
7286 while ((p = *pp) != NULL)
60124e18 7287 {
19e08130
AM
7288 if (p->sec == sec)
7289 {
7290 if (!must_be_dyn_reloc (info, r_type))
7291 p->pc_count -= 1;
1657026c
AM
7292 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7293 && rel->r_offset % 2 == 0
8ffb6df2 7294 && sec->alignment_power != 0)
1657026c 7295 p->rel_count -= 1;
19e08130
AM
7296 p->count -= 1;
7297 if (p->count == 0)
7298 *pp = p->next;
0a1b45a2 7299 return true;
19e08130
AM
7300 }
7301 pp = &p->next;
60124e18 7302 }
6edfbbad 7303 }
19e08130
AM
7304 else
7305 {
1657026c
AM
7306 struct ppc_local_dyn_relocs *p;
7307 struct ppc_local_dyn_relocs **pp;
19e08130 7308 void *vpp;
0a1b45a2 7309 bool is_ifunc;
8c1d1bb8 7310
19e08130
AM
7311 if (local_syms == NULL)
7312 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7313 if (sym_sec == NULL)
7314 sym_sec = sec;
c57da1a7 7315
19e08130 7316 vpp = &elf_section_data (sym_sec)->local_dynrel;
1657026c 7317 pp = (struct ppc_local_dyn_relocs **) vpp;
19e08130
AM
7318
7319 if (*pp == NULL && info->gc_sections)
0a1b45a2 7320 return true;
19e08130
AM
7321
7322 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7323 while ((p = *pp) != NULL)
8c1d1bb8 7324 {
19e08130
AM
7325 if (p->sec == sec && p->ifunc == is_ifunc)
7326 {
1657026c
AM
7327 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7328 && rel->r_offset % 2 == 0
8ffb6df2 7329 && sec->alignment_power != 0)
1657026c 7330 p->rel_count -= 1;
19e08130
AM
7331 p->count -= 1;
7332 if (p->count == 0)
7333 *pp = p->next;
0a1b45a2 7334 return true;
19e08130
AM
7335 }
7336 pp = &p->next;
8c1d1bb8 7337 }
8c1d1bb8
AM
7338 }
7339
695344c0 7340 /* xgettext:c-format */
cf97bcb0
AM
7341 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7342 sec->owner, sec);
8c1d1bb8 7343 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7344 return false;
8c1d1bb8
AM
7345}
7346
754021d0
AM
7347/* Remove unused Official Procedure Descriptor entries. Currently we
7348 only remove those associated with functions in discarded link-once
7349 sections, or weakly defined functions that have been overridden. It
7350 would be possible to remove many more entries for statically linked
7351 applications. */
7352
0a1b45a2 7353bool
e7d1c40c 7354ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7355{
7356 bfd *ibfd;
0a1b45a2 7357 bool some_edited = false;
3f764659 7358 asection *need_pad = NULL;
e7d1c40c
AM
7359 struct ppc_link_hash_table *htab;
7360
7361 htab = ppc_hash_table (info);
7362 if (htab == NULL)
0a1b45a2 7363 return false;
1e2f5b6e 7364
c72f2fb2 7365 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7366 {
7367 asection *sec;
7368 Elf_Internal_Rela *relstart, *rel, *relend;
7369 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7370 Elf_Internal_Sym *local_syms;
74f0fb50 7371 struct _opd_sec_data *opd;
0a1b45a2 7372 bool need_edit, add_aux_fields, broken;
3f764659 7373 bfd_size_type cnt_16b = 0;
1e2f5b6e 7374
854b41e7
AM
7375 if (!is_ppc64_elf (ibfd))
7376 continue;
7377
1e2f5b6e 7378 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7379 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7380 continue;
7381
dbaa2011 7382 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7383 continue;
7384
1e2f5b6e
AM
7385 if (sec->output_section == bfd_abs_section_ptr)
7386 continue;
7387
7388 /* Look through the section relocs. */
7389 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7390 continue;
7391
6cdc0ccc 7392 local_syms = NULL;
0ffa91dd 7393 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7394
7395 /* Read the relocations. */
4ce794b7 7396 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7397 info->keep_memory);
1e2f5b6e 7398 if (relstart == NULL)
0a1b45a2 7399 return false;
1e2f5b6e
AM
7400
7401 /* First run through the relocs to check they are sane, and to
7402 determine whether we need to edit this opd section. */
0a1b45a2
AM
7403 need_edit = false;
7404 broken = false;
3f764659 7405 need_pad = sec;
1e2f5b6e 7406 relend = relstart + sec->reloc_count;
50bc7936 7407 for (rel = relstart; rel < relend; )
1e2f5b6e 7408 {
04c9666a 7409 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7410 unsigned long r_symndx;
7411 asection *sym_sec;
7412 struct elf_link_hash_entry *h;
7413 Elf_Internal_Sym *sym;
51aecdc5 7414 bfd_vma offset;
1e2f5b6e 7415
51aecdc5 7416 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7417 only interested in the reloc pointing to a function entry
7418 point. */
51aecdc5
AM
7419 offset = rel->r_offset;
7420 if (rel + 1 == relend
7421 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7422 {
7423 /* If someone messes with .opd alignment then after a
7424 "ld -r" we might have padding in the middle of .opd.
7425 Also, there's nothing to prevent someone putting
7426 something silly in .opd with the assembler. No .opd
b34976b6 7427 optimization for them! */
3f764659 7428 broken_opd:
4eca0228 7429 _bfd_error_handler
871b3ab2 7430 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
0a1b45a2 7431 broken = true;
1e2f5b6e
AM
7432 break;
7433 }
7434
50bc7936
AM
7435 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7436 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7437 {
4eca0228 7438 _bfd_error_handler
695344c0 7439 /* xgettext:c-format */
871b3ab2 7440 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7441 ibfd, r_type);
0a1b45a2 7442 broken = true;
50bc7936
AM
7443 break;
7444 }
7445
1e2f5b6e 7446 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7447 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7448 r_symndx, ibfd))
50bc7936 7449 goto error_ret;
1e2f5b6e
AM
7450
7451 if (sym_sec == NULL || sym_sec->owner == NULL)
7452 {
411e1bfb
AM
7453 const char *sym_name;
7454 if (h != NULL)
7455 sym_name = h->root.root.string;
7456 else
26c61ae5
L
7457 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7458 sym_sec);
411e1bfb 7459
4eca0228 7460 _bfd_error_handler
695344c0 7461 /* xgettext:c-format */
871b3ab2 7462 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7463 ibfd, sym_name);
0a1b45a2 7464 broken = true;
1e2f5b6e
AM
7465 break;
7466 }
7467
51020317
AM
7468 /* opd entries are always for functions defined in the
7469 current input bfd. If the symbol isn't defined in the
7470 input bfd, then we won't be using the function in this
7471 bfd; It must be defined in a linkonce section in another
7472 bfd, or is weak. It's also possible that we are
7473 discarding the function due to a linker script /DISCARD/,
7474 which we test for via the output_section. */
7475 if (sym_sec->owner != ibfd
7476 || sym_sec->output_section == bfd_abs_section_ptr)
0a1b45a2 7477 need_edit = true;
1e2f5b6e 7478
50bc7936 7479 rel += 2;
51aecdc5
AM
7480 if (rel + 1 == relend
7481 || (rel + 2 < relend
7482 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7483 ++rel;
7484
7485 if (rel == relend)
3f764659
JJ
7486 {
7487 if (sec->size == offset + 24)
7488 {
7489 need_pad = NULL;
7490 break;
7491 }
51aecdc5 7492 if (sec->size == offset + 16)
3f764659
JJ
7493 {
7494 cnt_16b++;
7495 break;
7496 }
7497 goto broken_opd;
7498 }
3f764659
JJ
7499 else if (rel + 1 < relend
7500 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7501 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7502 {
51aecdc5
AM
7503 if (rel[0].r_offset == offset + 16)
7504 cnt_16b++;
7505 else if (rel[0].r_offset != offset + 24)
7506 goto broken_opd;
3f764659
JJ
7507 }
7508 else
7509 goto broken_opd;
1e2f5b6e
AM
7510 }
7511
e7d1c40c 7512 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7513
51aecdc5 7514 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7515 {
7516 Elf_Internal_Rela *write_rel;
d4730f92 7517 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7518 bfd_byte *rptr, *wptr;
983bddc8 7519 bfd_byte *new_contents;
74f0fb50
AM
7520 bfd_size_type amt;
7521
983bddc8 7522 new_contents = NULL;
51aecdc5 7523 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7524 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7525 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50 7526 if (opd->adjust == NULL)
0a1b45a2 7527 return false;
1e2f5b6e
AM
7528
7529 /* This seems a waste of time as input .opd sections are all
7530 zeros as generated by gcc, but I suppose there's no reason
7531 this will always be so. We might start putting something in
7532 the third word of .opd entries. */
7533 if ((sec->flags & SEC_IN_MEMORY) == 0)
7534 {
eea6121a
AM
7535 bfd_byte *loc;
7536 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7537 {
c9594989 7538 free (loc);
50bc7936 7539 error_ret:
c9594989 7540 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7541 free (local_syms);
6cdc0ccc
AM
7542 if (elf_section_data (sec)->relocs != relstart)
7543 free (relstart);
0a1b45a2 7544 return false;
6cdc0ccc 7545 }
1e2f5b6e
AM
7546 sec->contents = loc;
7547 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7548 }
7549
7550 elf_section_data (sec)->relocs = relstart;
7551
3f764659 7552 new_contents = sec->contents;
3f764659
JJ
7553 if (add_aux_fields)
7554 {
7555 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7556 if (new_contents == NULL)
0a1b45a2 7557 return false;
51aecdc5 7558 need_pad = NULL;
3f764659 7559 }
b4f4e59f
AM
7560 wptr = new_contents;
7561 rptr = sec->contents;
1e2f5b6e 7562 write_rel = relstart;
51aecdc5 7563 for (rel = relstart; rel < relend; )
1e2f5b6e 7564 {
50bc7936
AM
7565 unsigned long r_symndx;
7566 asection *sym_sec;
7567 struct elf_link_hash_entry *h;
51aecdc5 7568 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7569 Elf_Internal_Sym *sym;
51aecdc5
AM
7570 long opd_ent_size;
7571 Elf_Internal_Rela *next_rel;
0a1b45a2 7572 bool skip;
50bc7936
AM
7573
7574 r_symndx = ELF64_R_SYM (rel->r_info);
7575 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7576 r_symndx, ibfd))
50bc7936
AM
7577 goto error_ret;
7578
51aecdc5
AM
7579 next_rel = rel + 2;
7580 if (next_rel + 1 == relend
7581 || (next_rel + 2 < relend
7582 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7583 ++next_rel;
7584
7585 /* See if the .opd entry is full 24 byte or
7586 16 byte (with fd_aux entry overlapped with next
7587 fd_func). */
7588 opd_ent_size = 24;
7589 if (next_rel == relend)
1e2f5b6e 7590 {
51aecdc5 7591 if (sec->size == rel->r_offset + 16)
3f764659 7592 opd_ent_size = 16;
51aecdc5
AM
7593 }
7594 else if (next_rel->r_offset == rel->r_offset + 16)
7595 opd_ent_size = 16;
3f764659 7596
51aecdc5
AM
7597 if (h != NULL
7598 && h->root.root.string[0] == '.')
7599 {
ed7007c1 7600 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7601 if (fdh != NULL)
7602 {
7603 fdh = ppc_follow_link (fdh);
7604 if (fdh->elf.root.type != bfd_link_hash_defined
7605 && fdh->elf.root.type != bfd_link_hash_defweak)
7606 fdh = NULL;
7607 }
51aecdc5 7608 }
1e2f5b6e 7609
51aecdc5
AM
7610 skip = (sym_sec->owner != ibfd
7611 || sym_sec->output_section == bfd_abs_section_ptr);
7612 if (skip)
7613 {
7614 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7615 {
51aecdc5
AM
7616 /* Arrange for the function descriptor sym
7617 to be dropped. */
7618 fdh->elf.root.u.def.value = 0;
7619 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7620 }
51aecdc5 7621 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7622
0e1862bb 7623 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7624 rel = next_rel;
7625 else
7626 while (1)
7627 {
1657026c 7628 if (!dec_dynrel_count (rel, sec, info,
51aecdc5
AM
7629 NULL, h, sym))
7630 goto error_ret;
754021d0 7631
51aecdc5
AM
7632 if (++rel == next_rel)
7633 break;
1e2f5b6e 7634
51aecdc5
AM
7635 r_symndx = ELF64_R_SYM (rel->r_info);
7636 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7637 r_symndx, ibfd))
7638 goto error_ret;
7639 }
50bc7936
AM
7640 }
7641 else
1e2f5b6e 7642 {
51aecdc5
AM
7643 /* We'll be keeping this opd entry. */
7644 long adjust;
7645
7646 if (fdh != NULL)
7647 {
7648 /* Redefine the function descriptor symbol to
7649 this location in the opd section. It is
7650 necessary to update the value here rather
7651 than using an array of adjustments as we do
7652 for local symbols, because various places
7653 in the generic ELF code use the value
7654 stored in u.def.value. */
7655 fdh->elf.root.u.def.value = wptr - new_contents;
7656 fdh->adjust_done = 1;
7657 }
7658
7659 /* Local syms are a bit tricky. We could
7660 tweak them as they can be cached, but
7661 we'd need to look through the local syms
7662 for the function descriptor sym which we
7663 don't have at the moment. So keep an
7664 array of adjustments. */
7665 adjust = (wptr - new_contents) - (rptr - sec->contents);
7666 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7667
7668 if (wptr != rptr)
7669 memcpy (wptr, rptr, opd_ent_size);
7670 wptr += opd_ent_size;
7671 if (add_aux_fields && opd_ent_size == 16)
7672 {
7673 memset (wptr, '\0', 8);
7674 wptr += 8;
7675 }
7676
50bc7936 7677 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7678 new opd entries. */
7679 for ( ; rel != next_rel; ++rel)
7680 {
7681 rel->r_offset += adjust;
7682 if (write_rel != rel)
7683 memcpy (write_rel, rel, sizeof (*rel));
7684 ++write_rel;
7685 }
1e2f5b6e 7686 }
51aecdc5
AM
7687
7688 rptr += opd_ent_size;
1e2f5b6e
AM
7689 }
7690
3f764659 7691 sec->size = wptr - new_contents;
1e2f5b6e 7692 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7693 if (add_aux_fields)
7694 {
7695 free (sec->contents);
7696 sec->contents = new_contents;
7697 }
7698
05bf9422 7699 /* Fudge the header size too, as this is used later in
cdcf6e38 7700 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7701 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7702 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
0a1b45a2 7703 some_edited = true;
1e2f5b6e 7704 }
6cdc0ccc 7705 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7706 free (relstart);
6cdc0ccc 7707
411e1bfb
AM
7708 if (local_syms != NULL
7709 && symtab_hdr->contents != (unsigned char *) local_syms)
7710 {
7711 if (!info->keep_memory)
7712 free (local_syms);
7713 else
7714 symtab_hdr->contents = (unsigned char *) local_syms;
7715 }
7716 }
7717
754021d0
AM
7718 if (some_edited)
7719 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7720
3f764659
JJ
7721 /* If we are doing a final link and the last .opd entry is just 16 byte
7722 long, add a 8 byte padding after it. */
0e1862bb 7723 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7724 {
7725 bfd_byte *p;
7726
7727 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7728 {
7729 BFD_ASSERT (need_pad->size > 0);
7730
7731 p = bfd_malloc (need_pad->size + 8);
7732 if (p == NULL)
0a1b45a2 7733 return false;
699733f6 7734
2cdcc330
AM
7735 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7736 p, 0, need_pad->size))
0a1b45a2 7737 return false;
3f764659
JJ
7738
7739 need_pad->contents = p;
7740 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7741 }
7742 else
7743 {
7744 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7745 if (p == NULL)
0a1b45a2 7746 return false;
3f764659
JJ
7747
7748 need_pad->contents = p;
7749 }
7750
7751 memset (need_pad->contents + need_pad->size, 0, 8);
7752 need_pad->size += 8;
7753 }
7754
0a1b45a2 7755 return true;
411e1bfb
AM
7756}
7757
3e04d765
AM
7758/* Analyze inline PLT call relocations to see whether calls to locally
7759 defined functions can be converted to direct calls. */
7760
0a1b45a2 7761bool
3e04d765
AM
7762ppc64_elf_inline_plt (struct bfd_link_info *info)
7763{
7764 struct ppc_link_hash_table *htab;
7765 bfd *ibfd;
7766 asection *sec;
7767 bfd_vma low_vma, high_vma, limit;
7768
7769 htab = ppc_hash_table (info);
7770 if (htab == NULL)
0a1b45a2 7771 return false;
3e04d765
AM
7772
7773 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7774 reduced somewhat to cater for possible stubs that might be added
7775 between the call and its destination. */
7776 if (htab->params->group_size < 0)
7777 {
7778 limit = -htab->params->group_size;
7779 if (limit == 1)
7780 limit = 0x1e00000;
7781 }
7782 else
7783 {
7784 limit = htab->params->group_size;
7785 if (limit == 1)
7786 limit = 0x1c00000;
7787 }
7788
7789 low_vma = -1;
7790 high_vma = 0;
7791 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7792 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7793 {
7794 if (low_vma > sec->vma)
7795 low_vma = sec->vma;
7796 if (high_vma < sec->vma + sec->size)
7797 high_vma = sec->vma + sec->size;
7798 }
7799
7800 /* If a "bl" can reach anywhere in local code sections, then we can
7801 convert all inline PLT sequences to direct calls when the symbol
7802 is local. */
7803 if (high_vma - low_vma < limit)
7804 {
7805 htab->can_convert_all_inline_plt = 1;
0a1b45a2 7806 return true;
3e04d765
AM
7807 }
7808
7809 /* Otherwise, go looking through relocs for cases where a direct
7810 call won't reach. Mark the symbol on any such reloc to disable
7811 the optimization and keep the PLT entry as it seems likely that
7812 this will be better than creating trampolines. Note that this
7813 will disable the optimization for all inline PLT calls to a
7814 particular symbol, not just those that won't reach. The
7815 difficulty in doing a more precise optimization is that the
7816 linker needs to make a decision depending on whether a
7817 particular R_PPC64_PLTCALL insn can be turned into a direct
7818 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7819 the sequence, and there is nothing that ties those relocs
7820 together except their symbol. */
7821
7822 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7823 {
7824 Elf_Internal_Shdr *symtab_hdr;
7825 Elf_Internal_Sym *local_syms;
7826
7827 if (!is_ppc64_elf (ibfd))
7828 continue;
7829
7830 local_syms = NULL;
7831 symtab_hdr = &elf_symtab_hdr (ibfd);
7832
7833 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7834 if (ppc64_elf_section_data (sec)->has_pltcall
7835 && !bfd_is_abs_section (sec->output_section))
7836 {
7837 Elf_Internal_Rela *relstart, *rel, *relend;
7838
7839 /* Read the relocations. */
7840 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7841 info->keep_memory);
7842 if (relstart == NULL)
0a1b45a2 7843 return false;
3e04d765
AM
7844
7845 relend = relstart + sec->reloc_count;
435edf0b 7846 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7847 {
7848 enum elf_ppc64_reloc_type r_type;
7849 unsigned long r_symndx;
7850 asection *sym_sec;
7851 struct elf_link_hash_entry *h;
7852 Elf_Internal_Sym *sym;
7853 unsigned char *tls_maskp;
7854
7855 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7856 if (r_type != R_PPC64_PLTCALL
7857 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7858 continue;
7859
7860 r_symndx = ELF64_R_SYM (rel->r_info);
7861 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7862 r_symndx, ibfd))
7863 {
7864 if (elf_section_data (sec)->relocs != relstart)
7865 free (relstart);
c9594989 7866 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765 7867 free (local_syms);
0a1b45a2 7868 return false;
3e04d765
AM
7869 }
7870
7871 if (sym_sec != NULL && sym_sec->output_section != NULL)
7872 {
7873 bfd_vma from, to;
7874 if (h != NULL)
7875 to = h->root.u.def.value;
7876 else
7877 to = sym->st_value;
7878 to += (rel->r_addend
7879 + sym_sec->output_offset
7880 + sym_sec->output_section->vma);
7881 from = (rel->r_offset
7882 + sec->output_offset
7883 + sec->output_section->vma);
5663e321
AM
7884 if (to - from + limit < 2 * limit
7885 && !(r_type == R_PPC64_PLTCALL_NOTOC
7886 && (((h ? h->other : sym->st_other)
7887 & STO_PPC64_LOCAL_MASK)
4a4e7361 7888 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7889 *tls_maskp &= ~PLT_KEEP;
7890 }
7891 }
7892 if (elf_section_data (sec)->relocs != relstart)
7893 free (relstart);
7894 }
7895
7896 if (local_syms != NULL
7897 && symtab_hdr->contents != (unsigned char *) local_syms)
7898 {
7899 if (!info->keep_memory)
7900 free (local_syms);
7901 else
7902 symtab_hdr->contents = (unsigned char *) local_syms;
7903 }
7904 }
7905
0a1b45a2 7906 return true;
3e04d765
AM
7907}
7908
c9405344
AM
7909/* Set htab->tls_get_addr and various other info specific to TLS.
7910 This needs to run before dynamic symbols are processed in
7911 bfd_elf_size_dynamic_sections. */
411e1bfb 7912
0a1b45a2 7913bool
e7d1c40c 7914ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7915{
411e1bfb 7916 struct ppc_link_hash_table *htab;
9e7028aa 7917 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7918
411e1bfb 7919 htab = ppc_hash_table (info);
4dfe6ac6 7920 if (htab == NULL)
0a1b45a2 7921 return false;
4dfe6ac6 7922
a442059f
AM
7923 /* Move dynamic linking info to the function descriptor sym. */
7924 if (htab->need_func_desc_adj)
7925 {
7926 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7927 htab->need_func_desc_adj = 0;
7928 }
7929
ee67d69a
AM
7930 if (abiversion (info->output_bfd) == 1)
7931 htab->opd_abi = 1;
7932
e7d1c40c 7933 if (htab->params->no_multi_toc)
33c0ec9d
AM
7934 htab->do_multi_toc = 0;
7935 else if (!htab->do_multi_toc)
e7d1c40c 7936 htab->params->no_multi_toc = 1;
33c0ec9d 7937
8b5f1ed8
AM
7938 /* Default to --no-plt-localentry, as this option can cause problems
7939 with symbol interposition. For example, glibc libpthread.so and
7940 libc.so duplicate many pthread symbols, with a fallback
7941 implementation in libc.so. In some cases the fallback does more
7942 work than the pthread implementation. __pthread_condattr_destroy
7943 is one such symbol: the libpthread.so implementation is
7944 localentry:0 while the libc.so implementation is localentry:8.
7945 An app that "cleverly" uses dlopen to only load necessary
7946 libraries at runtime may omit loading libpthread.so when not
7947 running multi-threaded, which then results in the libc.so
7948 fallback symbols being used and ld.so complaining. Now there
7949 are workarounds in ld (see non_zero_localentry) to detect the
7950 pthread situation, but that may not be the only case where
7951 --plt-localentry can cause trouble. */
f378ab09 7952 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7953 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7954 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7955 {
7956 /* The issue is that __glink_PLTresolve saves r2, which is done
7957 because glibc ld.so _dl_runtime_resolve restores r2 to support
7958 a glibc plt call optimisation where global entry code is
7959 skipped on calls that resolve to the same binary. The
7960 __glink_PLTresolve save of r2 is incompatible with code
7961 making tail calls, because the tail call might go via the
7962 resolver and thus overwrite the proper saved r2. */
7963 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7964 "power10 pc-relative code"));
7965 htab->params->plt_localentry0 = 0;
7966 }
d44c746a
AM
7967 if (htab->params->plt_localentry0
7968 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
0a1b45a2 7969 false, false, false) == NULL)
cf97bcb0
AM
7970 _bfd_error_handler
7971 (_("warning: --plt-localentry is especially dangerous without "
7972 "ld.so support to detect ABI violations"));
f378ab09 7973
9e7028aa 7974 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 7975 false, false, true);
9e7028aa 7976 htab->tls_get_addr = ppc_elf_hash_entry (tga);
9e7028aa 7977 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 7978 false, false, true);
9e7028aa
AM
7979 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7980
7981 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
0a1b45a2 7982 false, false, true);
9e7028aa 7983 htab->tga_desc = ppc_elf_hash_entry (desc);
9e7028aa 7984 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
0a1b45a2 7985 false, false, true);
9e7028aa
AM
7986 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7987
7c9cf415 7988 if (htab->params->tls_get_addr_opt)
a7f2871e 7989 {
9e7028aa 7990 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7991
7992 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
0a1b45a2 7993 false, false, true);
a7f2871e 7994 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
0a1b45a2 7995 false, false, true);
a7f2871e
AM
7996 if (opt_fd != NULL
7997 && (opt_fd->root.type == bfd_link_hash_defined
7998 || opt_fd->root.type == bfd_link_hash_defweak))
7999 {
8000 /* If glibc supports an optimized __tls_get_addr call stub,
8001 signalled by the presence of __tls_get_addr_opt, and we'll
8002 be calling __tls_get_addr via a plt call stub, then
8003 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
8004 if (!(htab->elf.dynamic_sections_created
8005 && tga_fd != NULL
8006 && (tga_fd->type == STT_FUNC
8007 || tga_fd->needs_plt)
8008 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8009 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8010 tga_fd = NULL;
8011 if (!(htab->elf.dynamic_sections_created
8012 && desc_fd != NULL
8013 && (desc_fd->type == STT_FUNC
8014 || desc_fd->needs_plt)
8015 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8016 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8017 desc_fd = NULL;
8018
8019 if (tga_fd != NULL || desc_fd != NULL)
8020 {
8021 struct plt_entry *ent = NULL;
8022
8023 if (tga_fd != NULL)
8024 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8025 if (ent->plt.refcount > 0)
8026 break;
8027 if (ent == NULL && desc_fd != NULL)
8028 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8029 if (ent->plt.refcount > 0)
8030 break;
a7f2871e
AM
8031 if (ent != NULL)
8032 {
9e7028aa
AM
8033 if (tga_fd != NULL)
8034 {
8035 tga_fd->root.type = bfd_link_hash_indirect;
8036 tga_fd->root.u.i.link = &opt_fd->root;
8037 tga_fd->root.u.i.warning = NULL;
8038 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8039 }
8040 if (desc_fd != NULL)
8041 {
8042 desc_fd->root.type = bfd_link_hash_indirect;
8043 desc_fd->root.u.i.link = &opt_fd->root;
8044 desc_fd->root.u.i.warning = NULL;
8045 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8046 }
b531344c 8047 opt_fd->mark = 1;
a7f2871e
AM
8048 if (opt_fd->dynindx != -1)
8049 {
8050 /* Use __tls_get_addr_opt in dynamic relocations. */
8051 opt_fd->dynindx = -1;
8052 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8053 opt_fd->dynstr_index);
8054 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
0a1b45a2 8055 return false;
a7f2871e 8056 }
9e7028aa 8057 if (tga_fd != NULL)
a7f2871e 8058 {
9e7028aa 8059 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 8060 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
8061 if (opt != NULL && tga != NULL)
8062 {
8063 tga->root.type = bfd_link_hash_indirect;
8064 tga->root.u.i.link = &opt->root;
8065 tga->root.u.i.warning = NULL;
8066 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8067 opt->mark = 1;
8068 _bfd_elf_link_hash_hide_symbol (info, opt,
8069 tga->forced_local);
8070 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8071 }
8072 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8073 htab->tls_get_addr_fd->is_func_descriptor = 1;
8074 if (htab->tls_get_addr != NULL)
8075 {
8076 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8077 htab->tls_get_addr->is_func = 1;
8078 }
a7f2871e 8079 }
9e7028aa 8080 if (desc_fd != NULL)
a7f2871e 8081 {
9e7028aa
AM
8082 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8083 if (opt != NULL && desc != NULL)
8084 {
8085 desc->root.type = bfd_link_hash_indirect;
8086 desc->root.u.i.link = &opt->root;
8087 desc->root.u.i.warning = NULL;
8088 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8089 opt->mark = 1;
8090 _bfd_elf_link_hash_hide_symbol (info, opt,
8091 desc->forced_local);
8092 htab->tga_desc = ppc_elf_hash_entry (opt);
8093 }
8094 htab->tga_desc_fd->oh = htab->tga_desc;
8095 htab->tga_desc_fd->is_func_descriptor = 1;
8096 if (htab->tga_desc != NULL)
8097 {
8098 htab->tga_desc->oh = htab->tga_desc_fd;
8099 htab->tga_desc->is_func = 1;
8100 }
a7f2871e
AM
8101 }
8102 }
8103 }
8104 }
7c9cf415
AM
8105 else if (htab->params->tls_get_addr_opt < 0)
8106 htab->params->tls_get_addr_opt = 0;
a7f2871e 8107 }
9e7028aa
AM
8108
8109 if (htab->tga_desc_fd != NULL
8110 && htab->params->tls_get_addr_opt
8111 && htab->params->no_tls_get_addr_regsave == -1)
8112 htab->params->no_tls_get_addr_regsave = 0;
8113
0a1b45a2 8114 return true;
3a71aa26 8115}
8387904d 8116
3a71aa26 8117/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 8118 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 8119
0a1b45a2 8120static bool
30845f11
AM
8121branch_reloc_hash_match (bfd *ibfd,
8122 Elf_Internal_Rela *rel,
8123 struct ppc_link_hash_entry *hash1,
8124 struct ppc_link_hash_entry *hash2,
8125 struct ppc_link_hash_entry *hash3,
8126 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
8127{
8128 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8129 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8130 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8131
e054468f 8132 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 8133 {
3a71aa26
AM
8134 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8135 struct elf_link_hash_entry *h;
8387904d 8136
3a71aa26 8137 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 8138 h = elf_follow_link (h);
30845f11
AM
8139 if (h == elf_hash_entry (hash1)
8140 || h == elf_hash_entry (hash2)
8141 || h == elf_hash_entry (hash3)
8142 || h == elf_hash_entry (hash4))
0a1b45a2 8143 return true;
a48ebf4d 8144 }
0a1b45a2 8145 return false;
951fd09b 8146}
411e1bfb 8147
951fd09b
AM
8148/* Run through all the TLS relocs looking for optimization
8149 opportunities. The linker has been hacked (see ppc64elf.em) to do
8150 a preliminary section layout so that we know the TLS segment
8151 offsets. We can't optimize earlier because some optimizations need
8152 to know the tp offset, and we need to optimize before allocating
8153 dynamic relocations. */
8154
0a1b45a2 8155bool
33c0ec9d 8156ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
8157{
8158 bfd *ibfd;
8159 asection *sec;
8160 struct ppc_link_hash_table *htab;
663a1470 8161 unsigned char *toc_ref;
102890f0 8162 int pass;
951fd09b 8163
3cbc1e5e 8164 if (!bfd_link_executable (info))
0a1b45a2 8165 return true;
411e1bfb 8166
951fd09b 8167 htab = ppc_hash_table (info);
4dfe6ac6 8168 if (htab == NULL)
0a1b45a2 8169 return false;
4dfe6ac6 8170
252dcdf4
AM
8171 htab->do_tls_opt = 1;
8172
663a1470
AM
8173 /* Make two passes over the relocs. On the first pass, mark toc
8174 entries involved with tls relocs, and check that tls relocs
8175 involved in setting up a tls_get_addr call are indeed followed by
8176 such a call. If they are not, we can't do any tls optimization.
8177 On the second pass twiddle tls_mask flags to notify
8178 relocate_section that optimization can be done, and adjust got
8179 and plt refcounts. */
8180 toc_ref = NULL;
8181 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8182 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8183 {
8184 Elf_Internal_Sym *locsyms = NULL;
8185 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8186
102890f0
AM
8187 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8188 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8189 {
8190 Elf_Internal_Rela *relstart, *rel, *relend;
0a1b45a2 8191 bool found_tls_get_addr_arg = 0;
411e1bfb 8192
102890f0
AM
8193 /* Read the relocations. */
8194 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8195 info->keep_memory);
8196 if (relstart == NULL)
2915c55b
JK
8197 {
8198 free (toc_ref);
0a1b45a2 8199 return false;
2915c55b 8200 }
411e1bfb 8201
102890f0
AM
8202 relend = relstart + sec->reloc_count;
8203 for (rel = relstart; rel < relend; rel++)
8204 {
8205 enum elf_ppc64_reloc_type r_type;
8206 unsigned long r_symndx;
8207 struct elf_link_hash_entry *h;
8208 Elf_Internal_Sym *sym;
8209 asection *sym_sec;
f961d9dd 8210 unsigned char *tls_mask;
46e9995a 8211 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0 8212 bfd_vma value;
0a1b45a2 8213 bool ok_tprel, is_local;
102890f0
AM
8214 long toc_ref_index = 0;
8215 int expecting_tls_get_addr = 0;
0a1b45a2 8216 bool ret = false;
411e1bfb 8217
102890f0
AM
8218 r_symndx = ELF64_R_SYM (rel->r_info);
8219 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8220 r_symndx, ibfd))
8221 {
8222 err_free_rel:
8223 if (elf_section_data (sec)->relocs != relstart)
8224 free (relstart);
c9594989
AM
8225 free (toc_ref);
8226 if (elf_symtab_hdr (ibfd).contents
8227 != (unsigned char *) locsyms)
102890f0 8228 free (locsyms);
663a1470 8229 return ret;
102890f0 8230 }
411e1bfb 8231
102890f0
AM
8232 if (h != NULL)
8233 {
766bc656
AM
8234 if (h->root.type == bfd_link_hash_defined
8235 || h->root.type == bfd_link_hash_defweak)
8236 value = h->root.u.def.value;
8237 else if (h->root.type == bfd_link_hash_undefweak)
8238 value = 0;
8239 else
663a1470
AM
8240 {
8241 found_tls_get_addr_arg = 0;
8242 continue;
8243 }
102890f0
AM
8244 }
8245 else
8246 /* Symbols referenced by TLS relocs must be of type
8247 STT_TLS. So no need for .opd local sym adjust. */
8248 value = sym->st_value;
8249
0a1b45a2 8250 ok_tprel = false;
f749f26e
AM
8251 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8252 if (is_local)
102890f0 8253 {
766bc656
AM
8254 if (h != NULL
8255 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 8256 ok_tprel = true;
c27b8c2a
AM
8257 else if (sym_sec != NULL
8258 && sym_sec->output_section != NULL)
766bc656
AM
8259 {
8260 value += sym_sec->output_offset;
8261 value += sym_sec->output_section->vma;
0b147428 8262 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8263 /* Note that even though the prefix insns
8264 allow a 1<<33 offset we use the same test
8265 as for addis;addi. There may be a mix of
8266 pcrel and non-pcrel code and the decision
8267 to optimise is per symbol, not per TLS
8268 sequence. */
0b147428 8269 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8270 }
102890f0 8271 }
951fd09b 8272
102890f0 8273 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8274 /* If this section has old-style __tls_get_addr calls
8275 without marker relocs, then check that each
8276 __tls_get_addr call reloc is preceded by a reloc
8277 that conceivably belongs to the __tls_get_addr arg
8278 setup insn. If we don't find matching arg setup
8279 relocs, don't do any tls optimization. */
8280 if (pass == 0
9737e8af 8281 && sec->nomark_tls_get_addr
663a1470 8282 && h != NULL
ed7007c1 8283 && is_tls_get_addr (h, htab)
663a1470
AM
8284 && !found_tls_get_addr_arg
8285 && is_branch_reloc (r_type))
8286 {
25f53a85 8287 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8288 "TLS optimization disabled\n"),
8289 ibfd, sec, rel->r_offset);
0a1b45a2 8290 ret = true;
663a1470
AM
8291 goto err_free_rel;
8292 }
8293
8294 found_tls_get_addr_arg = 0;
102890f0
AM
8295 switch (r_type)
8296 {
8297 case R_PPC64_GOT_TLSLD16:
8298 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8299 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8300 expecting_tls_get_addr = 1;
663a1470 8301 found_tls_get_addr_arg = 1;
1a0670f3 8302 /* Fall through. */
102890f0
AM
8303
8304 case R_PPC64_GOT_TLSLD16_HI:
8305 case R_PPC64_GOT_TLSLD16_HA:
8306 /* These relocs should never be against a symbol
8307 defined in a shared lib. Leave them alone if
8308 that turns out to be the case. */
8309 if (!is_local)
8310 continue;
411e1bfb 8311
102890f0 8312 /* LD -> LE */
411e1bfb 8313 tls_set = 0;
102890f0
AM
8314 tls_clear = TLS_LD;
8315 tls_type = TLS_TLS | TLS_LD;
8316 break;
411e1bfb 8317
102890f0
AM
8318 case R_PPC64_GOT_TLSGD16:
8319 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8320 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8321 expecting_tls_get_addr = 1;
663a1470 8322 found_tls_get_addr_arg = 1;
1a0670f3 8323 /* Fall through. */
102890f0
AM
8324
8325 case R_PPC64_GOT_TLSGD16_HI:
8326 case R_PPC64_GOT_TLSGD16_HA:
8327 if (ok_tprel)
8328 /* GD -> LE */
411e1bfb 8329 tls_set = 0;
102890f0
AM
8330 else
8331 /* GD -> IE */
b00a0a86 8332 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8333 tls_clear = TLS_GD;
8334 tls_type = TLS_TLS | TLS_GD;
8335 break;
8336
87c69f97 8337 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8338 case R_PPC64_GOT_TPREL16_DS:
8339 case R_PPC64_GOT_TPREL16_LO_DS:
8340 case R_PPC64_GOT_TPREL16_HI:
8341 case R_PPC64_GOT_TPREL16_HA:
8342 if (ok_tprel)
8343 {
8344 /* IE -> LE */
8345 tls_set = 0;
8346 tls_clear = TLS_TPREL;
8347 tls_type = TLS_TLS | TLS_TPREL;
8348 break;
8349 }
411e1bfb
AM
8350 continue;
8351
727fc41e 8352 case R_PPC64_TLSLD:
7d04a20a
AM
8353 if (!is_local)
8354 continue;
8355 /* Fall through. */
8356 case R_PPC64_TLSGD:
23cedd1d
AM
8357 if (rel + 1 < relend
8358 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8359 {
8360 if (pass != 0
2cdcc330 8361 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8362 != R_PPC64_PLTSEQ)
8363 && (ELF64_R_TYPE (rel[1].r_info)
8364 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8365 {
8366 r_symndx = ELF64_R_SYM (rel[1].r_info);
8367 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8368 r_symndx, ibfd))
23cedd1d
AM
8369 goto err_free_rel;
8370 if (h != NULL)
8371 {
8372 struct plt_entry *ent = NULL;
8373
8374 for (ent = h->plt.plist;
8375 ent != NULL;
8376 ent = ent->next)
8377 if (ent->addend == rel[1].r_addend)
8378 break;
8379
8380 if (ent != NULL
8381 && ent->plt.refcount > 0)
8382 ent->plt.refcount -= 1;
8383 }
8384 }
8385 continue;
8386 }
663a1470 8387 found_tls_get_addr_arg = 1;
1a0670f3 8388 /* Fall through. */
663a1470
AM
8389
8390 case R_PPC64_TLS:
8391 case R_PPC64_TOC16:
8392 case R_PPC64_TOC16_LO:
102890f0
AM
8393 if (sym_sec == NULL || sym_sec != toc)
8394 continue;
8395
8396 /* Mark this toc entry as referenced by a TLS
8397 code sequence. We can do that now in the
8398 case of R_PPC64_TLS, and after checking for
8399 tls_get_addr for the TOC16 relocs. */
8400 if (toc_ref == NULL)
2cdcc330
AM
8401 toc_ref
8402 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8403 if (toc_ref == NULL)
8404 goto err_free_rel;
8405
102890f0
AM
8406 if (h != NULL)
8407 value = h->root.u.def.value;
8408 else
8409 value = sym->st_value;
8410 value += rel->r_addend;
73242275
AM
8411 if (value % 8 != 0)
8412 continue;
8413 BFD_ASSERT (value < toc->size
8414 && toc->output_offset % 8 == 0);
663a1470 8415 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8416 if (r_type == R_PPC64_TLS
8417 || r_type == R_PPC64_TLSGD
8418 || r_type == R_PPC64_TLSLD)
102890f0
AM
8419 {
8420 toc_ref[toc_ref_index] = 1;
8421 continue;
8422 }
8423
8424 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8425 continue;
8426
8427 tls_set = 0;
8428 tls_clear = 0;
8429 expecting_tls_get_addr = 2;
8430 break;
8431
8432 case R_PPC64_TPREL64:
8433 if (pass == 0
8434 || sec != toc
8435 || toc_ref == NULL
663a1470 8436 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8437 continue;
8438 if (ok_tprel)
8439 {
8440 /* IE -> LE */
8441 tls_set = TLS_EXPLICIT;
8442 tls_clear = TLS_TPREL;
8443 break;
8444 }
8445 continue;
8446
8447 case R_PPC64_DTPMOD64:
8448 if (pass == 0
8449 || sec != toc
8450 || toc_ref == NULL
663a1470 8451 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8452 continue;
8453 if (rel + 1 < relend
8454 && (rel[1].r_info
8455 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8456 && rel[1].r_offset == rel->r_offset + 8)
8457 {
8458 if (ok_tprel)
8459 /* GD -> LE */
8460 tls_set = TLS_EXPLICIT | TLS_GD;
8461 else
8462 /* GD -> IE */
b00a0a86 8463 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8464 tls_clear = TLS_GD;
8465 }
8466 else
8467 {
8468 if (!is_local)
8469 continue;
8470
8471 /* LD -> LE */
8472 tls_set = TLS_EXPLICIT;
8473 tls_clear = TLS_LD;
8474 }
8475 break;
8476
252dcdf4
AM
8477 case R_PPC64_TPREL16_HA:
8478 if (pass == 0)
8479 {
8480 unsigned char buf[4];
8481 unsigned int insn;
8482 bfd_vma off = rel->r_offset & ~3;
8483 if (!bfd_get_section_contents (ibfd, sec, buf,
8484 off, 4))
8485 goto err_free_rel;
8486 insn = bfd_get_32 (ibfd, buf);
8487 /* addis rt,13,imm */
8488 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8489 != ((15u << 26) | (13 << 16)))
8490 {
8491 /* xgettext:c-format */
8492 info->callbacks->minfo
8493 (_("%H: warning: %s unexpected insn %#x.\n"),
8494 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8495 htab->do_tls_opt = 0;
8496 }
8497 }
8498 continue;
8499
8500 case R_PPC64_TPREL16_HI:
8501 case R_PPC64_TPREL16_HIGH:
8502 case R_PPC64_TPREL16_HIGHA:
8503 case R_PPC64_TPREL16_HIGHER:
8504 case R_PPC64_TPREL16_HIGHERA:
8505 case R_PPC64_TPREL16_HIGHEST:
8506 case R_PPC64_TPREL16_HIGHESTA:
8507 /* These can all be used in sequences along with
8508 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8509 able to verify easily. */
8510 htab->do_tls_opt = 0;
8511 continue;
8512
102890f0
AM
8513 default:
8514 continue;
8515 }
8516
8517 if (pass == 0)
8518 {
727fc41e 8519 if (!expecting_tls_get_addr
9737e8af 8520 || !sec->nomark_tls_get_addr)
102890f0
AM
8521 continue;
8522
3a71aa26
AM
8523 if (rel + 1 < relend
8524 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8525 htab->tls_get_addr_fd,
8526 htab->tga_desc_fd,
3a71aa26 8527 htab->tls_get_addr,
9e7028aa 8528 htab->tga_desc))
102890f0 8529 {
3a71aa26 8530 if (expecting_tls_get_addr == 2)
102890f0 8531 {
3a71aa26 8532 /* Check for toc tls entries. */
f961d9dd 8533 unsigned char *toc_tls;
3a71aa26
AM
8534 int retval;
8535
8536 retval = get_tls_mask (&toc_tls, NULL, NULL,
8537 &locsyms,
8538 rel, ibfd);
8539 if (retval == 0)
8540 goto err_free_rel;
663a1470
AM
8541 if (toc_tls != NULL)
8542 {
37da22e5
AM
8543 if ((*toc_tls & TLS_TLS) != 0
8544 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8545 found_tls_get_addr_arg = 1;
8546 if (retval > 1)
8547 toc_ref[toc_ref_index] = 1;
8548 }
102890f0 8549 }
3a71aa26 8550 continue;
102890f0
AM
8551 }
8552
102890f0
AM
8553 /* Uh oh, we didn't find the expected call. We
8554 could just mark this symbol to exclude it
8555 from tls optimization but it's safer to skip
663a1470 8556 the entire optimization. */
695344c0 8557 /* xgettext:c-format */
25f53a85 8558 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8559 "TLS optimization disabled\n"),
8560 ibfd, sec, rel->r_offset);
0a1b45a2 8561 ret = true;
663a1470 8562 goto err_free_rel;
102890f0
AM
8563 }
8564
37da22e5
AM
8565 /* If we don't have old-style __tls_get_addr calls
8566 without TLSGD/TLSLD marker relocs, and we haven't
8567 found a new-style __tls_get_addr call with a
8568 marker for this symbol, then we either have a
8569 broken object file or an -mlongcall style
8570 indirect call to __tls_get_addr without a marker.
8571 Disable optimization in this case. */
8572 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8573 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8574 && !sec->nomark_tls_get_addr
37da22e5
AM
8575 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8576 != (TLS_TLS | TLS_MARK)))
8577 continue;
8578
7d04a20a 8579 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8580 {
23cedd1d
AM
8581 struct plt_entry *ent = NULL;
8582
9e7028aa
AM
8583 if (htab->tls_get_addr_fd != NULL)
8584 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8585 ent != NULL;
8586 ent = ent->next)
8587 if (ent->addend == 0)
8588 break;
8589
8590 if (ent == NULL && htab->tga_desc_fd != NULL)
8591 for (ent = htab->tga_desc_fd->elf.plt.plist;
8592 ent != NULL;
8593 ent = ent->next)
8594 if (ent->addend == 0)
8595 break;
8596
8597 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8598 for (ent = htab->tls_get_addr->elf.plt.plist;
8599 ent != NULL;
8600 ent = ent->next)
8601 if (ent->addend == 0)
102890f0 8602 break;
411e1bfb 8603
9e7028aa
AM
8604 if (ent == NULL && htab->tga_desc != NULL)
8605 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8606 ent != NULL;
8607 ent = ent->next)
8608 if (ent->addend == 0)
102890f0 8609 break;
23cedd1d
AM
8610
8611 if (ent != NULL
8612 && ent->plt.refcount > 0)
8613 ent->plt.refcount -= 1;
102890f0 8614 }
411e1bfb 8615
102890f0 8616 if (tls_clear == 0)
30038c59
AM
8617 continue;
8618
102890f0
AM
8619 if ((tls_set & TLS_EXPLICIT) == 0)
8620 {
8621 struct got_entry *ent;
411e1bfb 8622
102890f0
AM
8623 /* Adjust got entry for this reloc. */
8624 if (h != NULL)
8625 ent = h->got.glist;
8626 else
8627 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8628
102890f0
AM
8629 for (; ent != NULL; ent = ent->next)
8630 if (ent->addend == rel->r_addend
8631 && ent->owner == ibfd
8632 && ent->tls_type == tls_type)
8633 break;
8634 if (ent == NULL)
8635 abort ();
411e1bfb 8636
102890f0
AM
8637 if (tls_set == 0)
8638 {
8639 /* We managed to get rid of a got entry. */
8640 if (ent->got.refcount > 0)
8641 ent->got.refcount -= 1;
8642 }
8643 }
8644 else
8645 {
8646 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8647 we'll lose one or two dyn relocs. */
1657026c 8648 if (!dec_dynrel_count (rel, sec, info,
19e08130 8649 NULL, h, sym))
0a1b45a2 8650 return false;
411e1bfb 8651
102890f0
AM
8652 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8653 {
1657026c 8654 if (!dec_dynrel_count (rel + 1, sec, info,
19e08130 8655 NULL, h, sym))
0a1b45a2 8656 return false;
102890f0
AM
8657 }
8658 }
411e1bfb 8659
46e9995a 8660 *tls_mask |= tls_set & 0xff;
102890f0
AM
8661 *tls_mask &= ~tls_clear;
8662 }
8c1d1bb8 8663
102890f0
AM
8664 if (elf_section_data (sec)->relocs != relstart)
8665 free (relstart);
8666 }
411e1bfb 8667
663a1470
AM
8668 if (locsyms != NULL
8669 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8670 {
8671 if (!info->keep_memory)
8672 free (locsyms);
8673 else
8674 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8675 }
8676 }
411e1bfb 8677
c9594989 8678 free (toc_ref);
0a1b45a2 8679 return true;
1e2f5b6e 8680}
b34976b6 8681
c5614fa4
AM
8682/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8683 the values of any global symbols in a toc section that has been
8684 edited. Globals in toc sections should be a rarity, so this function
8685 sets a flag if any are found in toc sections other than the one just
de194d85 8686 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8687
8688struct adjust_toc_info
8689{
8690 asection *toc;
8691 unsigned long *skip;
0a1b45a2 8692 bool global_toc_syms;
c5614fa4
AM
8693};
8694
ba761f19
AM
8695enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8696
0a1b45a2 8697static bool
c5614fa4
AM
8698adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8699{
8700 struct ppc_link_hash_entry *eh;
8701 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8702 unsigned long i;
c5614fa4 8703
c5614fa4
AM
8704 if (h->root.type != bfd_link_hash_defined
8705 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 8706 return true;
c5614fa4 8707
ed7007c1 8708 eh = ppc_elf_hash_entry (h);
c5614fa4 8709 if (eh->adjust_done)
0a1b45a2 8710 return true;
c5614fa4
AM
8711
8712 if (eh->elf.root.u.def.section == toc_inf->toc)
8713 {
854b41e7
AM
8714 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8715 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8716 else
854b41e7
AM
8717 i = eh->elf.root.u.def.value >> 3;
8718
ba761f19 8719 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8720 {
4eca0228 8721 _bfd_error_handler
854b41e7
AM
8722 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8723 do
8724 ++i;
ba761f19 8725 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8726 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8727 }
854b41e7
AM
8728
8729 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8730 eh->adjust_done = 1;
8731 }
8732 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
0a1b45a2 8733 toc_inf->global_toc_syms = true;
c5614fa4 8734
0a1b45a2 8735 return true;
c5614fa4
AM
8736}
8737
39eeab25
AM
8738/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8739 on a _LO variety toc/got reloc. */
560c8763 8740
0a1b45a2 8741static bool
39eeab25 8742ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8743{
2365f8d7
AM
8744 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8745 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8746 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8747 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8748 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8749 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8750 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8751 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8752 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8753 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8754 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8755 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8756 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8757 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8758 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8759 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8760 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8761 /* Exclude lfqu by testing reloc. If relocs are ever
8762 defined for the reduced D field in psq_lu then those
8763 will need testing too. */
8764 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8765 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8766 && (insn & 1) == 0)
2365f8d7
AM
8767 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8768 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8769 /* Exclude stfqu. psq_stu as above for psq_lu. */
8770 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8771 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8772 && (insn & 1) == 0));
560c8763
AM
8773}
8774
4a421c53
AM
8775/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8776 pld ra,symbol@got@pcrel
dd9b12c2 8777 load/store rt,off(ra)
4a421c53 8778 or
d4b87b1e 8779 pla ra,symbol@pcrel
dd9b12c2 8780 load/store rt,off(ra)
4a421c53 8781 may be translated to
dd9b12c2 8782 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8783 nop.
8784 This function returns true if the optimization is possible, placing
dd9b12c2 8785 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8786
8787 On entry to this function, the linker has already determined that
d4b87b1e 8788 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8789 while *PINSN2 is the second instruction. */
8790
0a1b45a2 8791static bool
dd9b12c2 8792xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8793{
77486630
AM
8794 uint64_t insn1 = *pinsn1;
8795 uint64_t insn2 = *pinsn2;
dd9b12c2 8796 bfd_signed_vma off;
4a421c53 8797
77486630
AM
8798 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8799 {
8800 /* Check that regs match. */
8801 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8802 return false;
77486630
AM
8803
8804 /* P8LS or PMLS form, non-pcrel. */
8805 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
0a1b45a2 8806 return false;
77486630
AM
8807
8808 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8809 *pinsn2 = PNOP;
8810 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8811 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
0a1b45a2 8812 return true;
77486630
AM
8813 }
8814
8815 insn2 >>= 32;
8816
4a421c53 8817 /* Check that regs match. */
77486630 8818 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8819 return false;
4a421c53
AM
8820
8821 switch ((insn2 >> 26) & 63)
8822 {
8823 default:
0a1b45a2 8824 return false;
4a421c53
AM
8825
8826 case 32: /* lwz */
8827 case 34: /* lbz */
8828 case 36: /* stw */
8829 case 38: /* stb */
8830 case 40: /* lhz */
8831 case 42: /* lha */
8832 case 44: /* sth */
8833 case 48: /* lfs */
8834 case 50: /* lfd */
8835 case 52: /* stfs */
8836 case 54: /* stfd */
8837 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8838 on the insn. */
77486630 8839 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8840 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8841 off = insn2 & 0xffff;
4a421c53
AM
8842 break;
8843
8844 case 58: /* lwa, ld */
dd9b12c2 8845 if ((insn2 & 1) != 0)
0a1b45a2 8846 return false;
77486630 8847 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8848 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8849 | (insn2 & (31ULL << 21)));
dd9b12c2 8850 off = insn2 & 0xfffc;
4a421c53
AM
8851 break;
8852
8853 case 57: /* lxsd, lxssp */
dd9b12c2 8854 if ((insn2 & 3) < 2)
0a1b45a2 8855 return false;
77486630 8856 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8857 | ((40ULL | (insn2 & 3)) << 26)
8858 | (insn2 & (31ULL << 21)));
dd9b12c2 8859 off = insn2 & 0xfffc;
4a421c53
AM
8860 break;
8861
8862 case 61: /* stxsd, stxssp, lxv, stxv */
8863 if ((insn2 & 3) == 0)
0a1b45a2 8864 return false;
4a421c53
AM
8865 else if ((insn2 & 3) >= 2)
8866 {
77486630 8867 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8868 | ((44ULL | (insn2 & 3)) << 26)
8869 | (insn2 & (31ULL << 21)));
dd9b12c2 8870 off = insn2 & 0xfffc;
4a421c53
AM
8871 }
8872 else
8873 {
77486630 8874 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8875 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8876 | (insn2 & (31ULL << 21)));
dd9b12c2 8877 off = insn2 & 0xfff0;
4a421c53
AM
8878 }
8879 break;
8880
8881 case 56: /* lq */
77486630 8882 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8883 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8884 off = insn2 & 0xffff;
4a421c53
AM
8885 break;
8886
94ba9882
AM
8887 case 6: /* lxvp, stxvp */
8888 if ((insn2 & 0xe) != 0)
0a1b45a2 8889 return false;
94ba9882
AM
8890 insn1 = ((1ULL << 58) | (1ULL << 52)
8891 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8892 | (insn2 & (31ULL << 21)));
8893 off = insn2 & 0xfff0;
8894 break;
8895
4a421c53 8896 case 62: /* std, stq */
dd9b12c2 8897 if ((insn2 & 1) != 0)
0a1b45a2 8898 return false;
77486630 8899 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8900 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8901 | (insn2 & (31ULL << 21)));
dd9b12c2 8902 off = insn2 & 0xfffc;
4a421c53
AM
8903 break;
8904 }
8905
77486630 8906 *pinsn1 = insn1;
4a421c53 8907 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8908 *poff = (off ^ 0x8000) - 0x8000;
0a1b45a2 8909 return true;
4a421c53
AM
8910}
8911
c5614fa4
AM
8912/* Examine all relocs referencing .toc sections in order to remove
8913 unused .toc entries. */
8914
0a1b45a2 8915bool
33c0ec9d 8916ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8917{
8918 bfd *ibfd;
8919 struct adjust_toc_info toc_inf;
67f0cbdb 8920 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8921
67f0cbdb 8922 htab->do_toc_opt = 1;
0a1b45a2 8923 toc_inf.global_toc_syms = true;
c72f2fb2 8924 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8925 {
8926 asection *toc, *sec;
8927 Elf_Internal_Shdr *symtab_hdr;
8928 Elf_Internal_Sym *local_syms;
425b145b 8929 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8930 unsigned long *skip, *drop;
8931 unsigned char *used;
8932 unsigned char *keep, last, some_unused;
8933
854b41e7
AM
8934 if (!is_ppc64_elf (ibfd))
8935 continue;
8936
c5614fa4
AM
8937 toc = bfd_get_section_by_name (ibfd, ".toc");
8938 if (toc == NULL
92b7a70f 8939 || toc->size == 0
dbaa2011
AM
8940 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8941 || discarded_section (toc))
c5614fa4
AM
8942 continue;
8943
425b145b 8944 toc_relocs = NULL;
c5614fa4 8945 local_syms = NULL;
0ffa91dd 8946 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8947
8948 /* Look at sections dropped from the final link. */
8949 skip = NULL;
8950 relstart = NULL;
8951 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8952 {
8953 if (sec->reloc_count == 0
dbaa2011 8954 || !discarded_section (sec)
c5614fa4
AM
8955 || get_opd_info (sec)
8956 || (sec->flags & SEC_ALLOC) == 0
8957 || (sec->flags & SEC_DEBUGGING) != 0)
8958 continue;
8959
0a1b45a2 8960 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
c5614fa4
AM
8961 if (relstart == NULL)
8962 goto error_ret;
8963
8964 /* Run through the relocs to see which toc entries might be
8965 unused. */
8966 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8967 {
8968 enum elf_ppc64_reloc_type r_type;
8969 unsigned long r_symndx;
8970 asection *sym_sec;
8971 struct elf_link_hash_entry *h;
8972 Elf_Internal_Sym *sym;
8973 bfd_vma val;
8974
8975 r_type = ELF64_R_TYPE (rel->r_info);
8976 switch (r_type)
8977 {
8978 default:
8979 continue;
8980
8981 case R_PPC64_TOC16:
8982 case R_PPC64_TOC16_LO:
8983 case R_PPC64_TOC16_HI:
8984 case R_PPC64_TOC16_HA:
8985 case R_PPC64_TOC16_DS:
8986 case R_PPC64_TOC16_LO_DS:
8987 break;
8988 }
8989
8990 r_symndx = ELF64_R_SYM (rel->r_info);
8991 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8992 r_symndx, ibfd))
8993 goto error_ret;
8994
8995 if (sym_sec != toc)
8996 continue;
8997
8998 if (h != NULL)
8999 val = h->root.u.def.value;
9000 else
9001 val = sym->st_value;
9002 val += rel->r_addend;
9003
9004 if (val >= toc->size)
9005 continue;
9006
9007 /* Anything in the toc ought to be aligned to 8 bytes.
9008 If not, don't mark as unused. */
9009 if (val & 7)
9010 continue;
9011
9012 if (skip == NULL)
9013 {
854b41e7 9014 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
9015 if (skip == NULL)
9016 goto error_ret;
9017 }
9018
ba761f19 9019 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
9020 }
9021
9022 if (elf_section_data (sec)->relocs != relstart)
9023 free (relstart);
9024 }
9025
ba761f19
AM
9026 /* For largetoc loads of address constants, we can convert
9027 . addis rx,2,addr@got@ha
9028 . ld ry,addr@got@l(rx)
9029 to
9030 . addis rx,2,addr@toc@ha
9031 . addi ry,rx,addr@toc@l
9032 when addr is within 2G of the toc pointer. This then means
9033 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 9034
ba761f19
AM
9035 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9036 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9037 && toc->reloc_count != 0)
9038 {
9039 /* Read toc relocs. */
425b145b
AM
9040 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9041 info->keep_memory);
9042 if (toc_relocs == NULL)
ba761f19
AM
9043 goto error_ret;
9044
425b145b 9045 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9046 {
9047 enum elf_ppc64_reloc_type r_type;
9048 unsigned long r_symndx;
9049 asection *sym_sec;
9050 struct elf_link_hash_entry *h;
9051 Elf_Internal_Sym *sym;
9052 bfd_vma val, addr;
9053
9054 r_type = ELF64_R_TYPE (rel->r_info);
9055 if (r_type != R_PPC64_ADDR64)
9056 continue;
9057
9058 r_symndx = ELF64_R_SYM (rel->r_info);
9059 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9060 r_symndx, ibfd))
9061 goto error_ret;
9062
425b145b 9063 if (sym_sec == NULL
c27b8c2a 9064 || sym_sec->output_section == NULL
dbaa2011 9065 || discarded_section (sym_sec))
425b145b
AM
9066 continue;
9067
afe397ea 9068 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
9069 continue;
9070
9071 if (h != NULL)
bddc25c9
AM
9072 {
9073 if (h->type == STT_GNU_IFUNC)
9074 continue;
9075 val = h->root.u.def.value;
9076 }
ba761f19 9077 else
bddc25c9
AM
9078 {
9079 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9080 continue;
9081 val = sym->st_value;
9082 }
ba761f19
AM
9083 val += rel->r_addend;
9084 val += sym_sec->output_section->vma + sym_sec->output_offset;
9085
9086 /* We don't yet know the exact toc pointer value, but we
9087 know it will be somewhere in the toc section. Don't
9088 optimize if the difference from any possible toc
9089 pointer is outside [ff..f80008000, 7fff7fff]. */
9090 addr = toc->output_section->vma + TOC_BASE_OFF;
9091 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9092 continue;
9093
9094 addr = toc->output_section->vma + toc->output_section->rawsize;
9095 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9096 continue;
9097
9098 if (skip == NULL)
9099 {
9100 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9101 if (skip == NULL)
9102 goto error_ret;
9103 }
9104
9105 skip[rel->r_offset >> 3]
425b145b 9106 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 9107 }
ba761f19
AM
9108 }
9109
c5614fa4
AM
9110 if (skip == NULL)
9111 continue;
9112
9113 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9114 if (used == NULL)
9115 {
9116 error_ret:
c9594989 9117 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
9118 free (local_syms);
9119 if (sec != NULL
c5614fa4
AM
9120 && elf_section_data (sec)->relocs != relstart)
9121 free (relstart);
c9594989 9122 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9123 free (toc_relocs);
c9594989 9124 free (skip);
0a1b45a2 9125 return false;
c5614fa4
AM
9126 }
9127
30038c59
AM
9128 /* Now check all kept sections that might reference the toc.
9129 Check the toc itself last. */
9130 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9131 : ibfd->sections);
c5614fa4 9132 sec != NULL;
c5614fa4 9133 sec = (sec == toc ? NULL
c5614fa4 9134 : sec->next == NULL ? toc
30038c59 9135 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
9136 : sec->next))
9137 {
9138 int repeat;
9139
9140 if (sec->reloc_count == 0
dbaa2011 9141 || discarded_section (sec)
c5614fa4
AM
9142 || get_opd_info (sec)
9143 || (sec->flags & SEC_ALLOC) == 0
9144 || (sec->flags & SEC_DEBUGGING) != 0)
9145 continue;
9146
854b41e7
AM
9147 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9148 info->keep_memory);
c5614fa4 9149 if (relstart == NULL)
2915c55b
JK
9150 {
9151 free (used);
9152 goto error_ret;
9153 }
c5614fa4
AM
9154
9155 /* Mark toc entries referenced as used. */
c5614fa4 9156 do
d4f1ee75
AM
9157 {
9158 repeat = 0;
9159 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9160 {
9161 enum elf_ppc64_reloc_type r_type;
9162 unsigned long r_symndx;
9163 asection *sym_sec;
9164 struct elf_link_hash_entry *h;
9165 Elf_Internal_Sym *sym;
9166 bfd_vma val;
98528052 9167
d4f1ee75 9168 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
9169 switch (r_type)
9170 {
9171 case R_PPC64_TOC16:
9172 case R_PPC64_TOC16_LO:
9173 case R_PPC64_TOC16_HI:
9174 case R_PPC64_TOC16_HA:
9175 case R_PPC64_TOC16_DS:
9176 case R_PPC64_TOC16_LO_DS:
9177 /* In case we're taking addresses of toc entries. */
9178 case R_PPC64_ADDR64:
9179 break;
c5614fa4 9180
d4f1ee75
AM
9181 default:
9182 continue;
9183 }
c5614fa4 9184
d4f1ee75
AM
9185 r_symndx = ELF64_R_SYM (rel->r_info);
9186 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9187 r_symndx, ibfd))
9188 {
9189 free (used);
9190 goto error_ret;
9191 }
c5614fa4 9192
d4f1ee75
AM
9193 if (sym_sec != toc)
9194 continue;
c5614fa4 9195
d4f1ee75
AM
9196 if (h != NULL)
9197 val = h->root.u.def.value;
9198 else
9199 val = sym->st_value;
9200 val += rel->r_addend;
ba761f19 9201
d4f1ee75
AM
9202 if (val >= toc->size)
9203 continue;
ba761f19 9204
d4f1ee75
AM
9205 if ((skip[val >> 3] & can_optimize) != 0)
9206 {
9207 bfd_vma off;
9208 unsigned char opc;
9209
9210 switch (r_type)
9211 {
9212 case R_PPC64_TOC16_HA:
ba761f19 9213 break;
ba761f19 9214
d4f1ee75
AM
9215 case R_PPC64_TOC16_LO_DS:
9216 off = rel->r_offset;
9217 off += (bfd_big_endian (ibfd) ? -2 : 3);
9218 if (!bfd_get_section_contents (ibfd, sec, &opc,
9219 off, 1))
9220 {
9221 free (used);
9222 goto error_ret;
9223 }
9224 if ((opc & (0x3f << 2)) == (58u << 2))
9225 break;
1a0670f3 9226 /* Fall through. */
ba761f19 9227
d4f1ee75
AM
9228 default:
9229 /* Wrong sort of reloc, or not a ld. We may
9230 as well clear ref_from_discarded too. */
9231 skip[val >> 3] = 0;
9232 }
9233 }
9234
9235 if (sec != toc)
9236 used[val >> 3] = 1;
9237 /* For the toc section, we only mark as used if this
9238 entry itself isn't unused. */
9239 else if ((used[rel->r_offset >> 3]
9240 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9241 && !used[val >> 3])
9242 {
9243 /* Do all the relocs again, to catch reference
9244 chains. */
9245 repeat = 1;
9246 used[val >> 3] = 1;
9247 }
9248 }
9249 }
c5614fa4 9250 while (repeat);
854b41e7
AM
9251
9252 if (elf_section_data (sec)->relocs != relstart)
9253 free (relstart);
c5614fa4
AM
9254 }
9255
9256 /* Merge the used and skip arrays. Assume that TOC
9257 doublewords not appearing as either used or unused belong
de194d85 9258 to an entry more than one doubleword in size. */
c5614fa4
AM
9259 for (drop = skip, keep = used, last = 0, some_unused = 0;
9260 drop < skip + (toc->size + 7) / 8;
9261 ++drop, ++keep)
9262 {
9263 if (*keep)
9264 {
ba761f19
AM
9265 *drop &= ~ref_from_discarded;
9266 if ((*drop & can_optimize) != 0)
9267 some_unused = 1;
c5614fa4
AM
9268 last = 0;
9269 }
b140b010 9270 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9271 {
9272 some_unused = 1;
ba761f19 9273 last = ref_from_discarded;
c5614fa4
AM
9274 }
9275 else
9276 *drop = last;
9277 }
9278
9279 free (used);
9280
9281 if (some_unused)
9282 {
9283 bfd_byte *contents, *src;
9284 unsigned long off;
d62b3684 9285 Elf_Internal_Sym *sym;
0a1b45a2 9286 bool local_toc_syms = false;
c5614fa4
AM
9287
9288 /* Shuffle the toc contents, and at the same time convert the
9289 skip array from booleans into offsets. */
9290 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9291 goto error_ret;
9292
9293 elf_section_data (toc)->this_hdr.contents = contents;
9294
9295 for (src = contents, off = 0, drop = skip;
9296 src < contents + toc->size;
9297 src += 8, ++drop)
9298 {
ba761f19
AM
9299 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9300 off += 8;
c5614fa4
AM
9301 else if (off != 0)
9302 {
9303 *drop = off;
9304 memcpy (src - off, src, 8);
9305 }
9306 }
854b41e7 9307 *drop = off;
c5614fa4
AM
9308 toc->rawsize = toc->size;
9309 toc->size = src - contents - off;
9310
ba761f19
AM
9311 /* Adjust addends for relocs against the toc section sym,
9312 and optimize any accesses we can. */
c5614fa4
AM
9313 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9314 {
9315 if (sec->reloc_count == 0
dbaa2011 9316 || discarded_section (sec))
c5614fa4
AM
9317 continue;
9318
9319 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9320 info->keep_memory);
c5614fa4
AM
9321 if (relstart == NULL)
9322 goto error_ret;
9323
9324 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9325 {
9326 enum elf_ppc64_reloc_type r_type;
9327 unsigned long r_symndx;
9328 asection *sym_sec;
9329 struct elf_link_hash_entry *h;
854b41e7 9330 bfd_vma val;
c5614fa4
AM
9331
9332 r_type = ELF64_R_TYPE (rel->r_info);
9333 switch (r_type)
9334 {
9335 default:
9336 continue;
9337
9338 case R_PPC64_TOC16:
9339 case R_PPC64_TOC16_LO:
9340 case R_PPC64_TOC16_HI:
9341 case R_PPC64_TOC16_HA:
9342 case R_PPC64_TOC16_DS:
9343 case R_PPC64_TOC16_LO_DS:
9344 case R_PPC64_ADDR64:
9345 break;
9346 }
9347
9348 r_symndx = ELF64_R_SYM (rel->r_info);
9349 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9350 r_symndx, ibfd))
9351 goto error_ret;
9352
ba761f19 9353 if (sym_sec != toc)
c5614fa4
AM
9354 continue;
9355
ba761f19
AM
9356 if (h != NULL)
9357 val = h->root.u.def.value;
9358 else
9359 {
9360 val = sym->st_value;
9361 if (val != 0)
0a1b45a2 9362 local_toc_syms = true;
ba761f19
AM
9363 }
9364
9365 val += rel->r_addend;
854b41e7
AM
9366
9367 if (val > toc->rawsize)
9368 val = toc->rawsize;
ba761f19
AM
9369 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9370 continue;
9371 else if ((skip[val >> 3] & can_optimize) != 0)
9372 {
9373 Elf_Internal_Rela *tocrel
425b145b 9374 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9375 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9376
9377 switch (r_type)
9378 {
9379 case R_PPC64_TOC16_HA:
9380 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9381 break;
9382
9383 case R_PPC64_TOC16_LO_DS:
9384 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9385 break;
9386
9387 default:
28942f62
AM
9388 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9389 ppc_howto_init ();
b140b010 9390 info->callbacks->einfo
695344c0 9391 /* xgettext:c-format */
174d0a74 9392 (_("%H: %s references "
b140b010
AM
9393 "optimized away TOC entry\n"),
9394 ibfd, sec, rel->r_offset,
9395 ppc64_elf_howto_table[r_type]->name);
9396 bfd_set_error (bfd_error_bad_value);
9397 goto error_ret;
ba761f19
AM
9398 }
9399 rel->r_addend = tocrel->r_addend;
9400 elf_section_data (sec)->relocs = relstart;
9401 continue;
9402 }
9403
9404 if (h != NULL || sym->st_value != 0)
9405 continue;
854b41e7
AM
9406
9407 rel->r_addend -= skip[val >> 3];
9408 elf_section_data (sec)->relocs = relstart;
c5614fa4 9409 }
854b41e7
AM
9410
9411 if (elf_section_data (sec)->relocs != relstart)
9412 free (relstart);
c5614fa4
AM
9413 }
9414
9415 /* We shouldn't have local or global symbols defined in the TOC,
9416 but handle them anyway. */
df22d223
AM
9417 if (local_syms != NULL)
9418 for (sym = local_syms;
9419 sym < local_syms + symtab_hdr->sh_info;
9420 ++sym)
9421 if (sym->st_value != 0
9422 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9423 {
9424 unsigned long i;
854b41e7 9425
df22d223
AM
9426 if (sym->st_value > toc->rawsize)
9427 i = toc->rawsize >> 3;
9428 else
9429 i = sym->st_value >> 3;
854b41e7 9430
df22d223
AM
9431 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9432 {
9433 if (local_toc_syms)
4eca0228 9434 _bfd_error_handler
df22d223
AM
9435 (_("%s defined on removed toc entry"),
9436 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9437 do
9438 ++i;
9439 while ((skip[i] & (ref_from_discarded | can_optimize)));
9440 sym->st_value = (bfd_vma) i << 3;
9441 }
d62b3684 9442
df22d223
AM
9443 sym->st_value -= skip[i];
9444 symtab_hdr->contents = (unsigned char *) local_syms;
9445 }
c5614fa4 9446
854b41e7 9447 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9448 if (toc_inf.global_toc_syms)
9449 {
9450 toc_inf.toc = toc;
9451 toc_inf.skip = skip;
0a1b45a2 9452 toc_inf.global_toc_syms = false;
c5614fa4
AM
9453 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9454 &toc_inf);
9455 }
854b41e7
AM
9456
9457 if (toc->reloc_count != 0)
9458 {
d4730f92 9459 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9460 Elf_Internal_Rela *wrel;
9461 bfd_size_type sz;
9462
854b41e7 9463 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9464 if (toc_relocs == NULL)
9465 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9466 info->keep_memory);
9467 if (toc_relocs == NULL)
9468 goto error_ret;
9469
425b145b
AM
9470 wrel = toc_relocs;
9471 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9472 if ((skip[rel->r_offset >> 3]
9473 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9474 {
9475 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9476 wrel->r_info = rel->r_info;
9477 wrel->r_addend = rel->r_addend;
9478 ++wrel;
9479 }
1657026c 9480 else if (!dec_dynrel_count (rel, toc, info,
854b41e7
AM
9481 &local_syms, NULL, NULL))
9482 goto error_ret;
9483
425b145b
AM
9484 elf_section_data (toc)->relocs = toc_relocs;
9485 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9486 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9487 sz = rel_hdr->sh_entsize;
9488 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9489 }
c5614fa4 9490 }
c9594989 9491 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9492 free (toc_relocs);
c5614fa4
AM
9493
9494 if (local_syms != NULL
9495 && symtab_hdr->contents != (unsigned char *) local_syms)
9496 {
9497 if (!info->keep_memory)
9498 free (local_syms);
9499 else
9500 symtab_hdr->contents = (unsigned char *) local_syms;
9501 }
9502 free (skip);
9503 }
9504
066f4018 9505 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9506 a GOT relative or PC relative access, possibly reducing the
9507 number of GOT entries. */
066f4018
AM
9508 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9509 {
9510 asection *sec;
9511 Elf_Internal_Shdr *symtab_hdr;
9512 Elf_Internal_Sym *local_syms;
9513 Elf_Internal_Rela *relstart, *rel;
9514 bfd_vma got;
9515
9516 if (!is_ppc64_elf (ibfd))
9517 continue;
9518
903b777d 9519 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9520 continue;
9521
9522 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9523 got = 0;
9524 if (sec != NULL)
9525 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9526
9527 local_syms = NULL;
9528 symtab_hdr = &elf_symtab_hdr (ibfd);
9529
9530 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9531 {
9532 if (sec->reloc_count == 0
903b777d 9533 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9534 || discarded_section (sec))
9535 continue;
9536
9537 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9538 info->keep_memory);
9539 if (relstart == NULL)
9540 {
9541 got_error_ret:
c9594989 9542 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9543 free (local_syms);
9544 if (sec != NULL
066f4018
AM
9545 && elf_section_data (sec)->relocs != relstart)
9546 free (relstart);
0a1b45a2 9547 return false;
066f4018
AM
9548 }
9549
9550 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9551 {
9552 enum elf_ppc64_reloc_type r_type;
9553 unsigned long r_symndx;
9554 Elf_Internal_Sym *sym;
9555 asection *sym_sec;
9556 struct elf_link_hash_entry *h;
9557 struct got_entry *ent;
133a1f60 9558 bfd_vma val, pc;
4a421c53 9559 unsigned char buf[8];
066f4018 9560 unsigned int insn;
903b777d 9561 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9562
9563 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9564 switch (r_type)
9565 {
9566 default:
9567 insn_check = no_check;
9568 break;
9569
9570 case R_PPC64_PLT16_HA:
9571 case R_PPC64_GOT_TLSLD16_HA:
9572 case R_PPC64_GOT_TLSGD16_HA:
9573 case R_PPC64_GOT_TPREL16_HA:
9574 case R_PPC64_GOT_DTPREL16_HA:
9575 case R_PPC64_GOT16_HA:
9576 case R_PPC64_TOC16_HA:
9577 insn_check = check_ha;
9578 break;
9579
9580 case R_PPC64_PLT16_LO:
9581 case R_PPC64_PLT16_LO_DS:
9582 case R_PPC64_GOT_TLSLD16_LO:
9583 case R_PPC64_GOT_TLSGD16_LO:
9584 case R_PPC64_GOT_TPREL16_LO_DS:
9585 case R_PPC64_GOT_DTPREL16_LO_DS:
9586 case R_PPC64_GOT16_LO:
9587 case R_PPC64_GOT16_LO_DS:
9588 case R_PPC64_TOC16_LO:
9589 case R_PPC64_TOC16_LO_DS:
9590 insn_check = check_lo;
9591 break;
9592 }
9593
9594 if (insn_check != no_check)
9595 {
9596 bfd_vma off = rel->r_offset & ~3;
9597
9598 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9599 goto got_error_ret;
9600
9601 insn = bfd_get_32 (ibfd, buf);
9602 if (insn_check == check_lo
9603 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9604 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9605 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9606 {
9607 char str[12];
9608
9609 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9610 sprintf (str, "%#08x", insn);
9611 info->callbacks->einfo
9612 /* xgettext:c-format */
9613 (_("%H: got/toc optimization is not supported for"
9614 " %s instruction\n"),
9615 ibfd, sec, rel->r_offset & ~3, str);
9616 continue;
9617 }
9618 }
9619
066f4018
AM
9620 switch (r_type)
9621 {
bb22a418
AM
9622 /* Note that we don't delete GOT entries for
9623 R_PPC64_GOT16_DS since we'd need a lot more
9624 analysis. For starters, the preliminary layout is
9625 before the GOT, PLT, dynamic sections and stubs are
9626 laid out. Then we'd need to allow for changes in
9627 distance between sections caused by alignment. */
066f4018
AM
9628 default:
9629 continue;
9630
066f4018
AM
9631 case R_PPC64_GOT16_HA:
9632 case R_PPC64_GOT16_LO_DS:
4a421c53 9633 case R_PPC64_GOT_PCREL34:
066f4018
AM
9634 break;
9635 }
9636
9637 r_symndx = ELF64_R_SYM (rel->r_info);
9638 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9639 r_symndx, ibfd))
9640 goto got_error_ret;
9641
6d5554a6
AM
9642 if (sym_sec == NULL
9643 || sym_sec->output_section == NULL
9644 || discarded_section (sym_sec))
9645 continue;
9646
06507dab
AM
9647 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9648 continue;
9649
066f4018
AM
9650 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9651 continue;
9652
9653 if (h != NULL)
9654 val = h->root.u.def.value;
9655 else
9656 val = sym->st_value;
133a1f60 9657 val += rel->r_addend;
066f4018
AM
9658 val += sym_sec->output_section->vma + sym_sec->output_offset;
9659
bb22a418
AM
9660/* Fudge factor to allow for the fact that the preliminary layout
9661 isn't exact. Reduce limits by this factor. */
9662#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9663
066f4018
AM
9664 switch (r_type)
9665 {
9666 default:
9667 continue;
9668
066f4018 9669 case R_PPC64_GOT16_HA:
bb22a418
AM
9670 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9671 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9672 continue;
9673
9674 if (!bfd_get_section_contents (ibfd, sec, buf,
9675 rel->r_offset & ~3, 4))
9676 goto got_error_ret;
9677 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9678 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9679 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9680 continue;
9681 break;
9682
9683 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9684 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9685 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9686 continue;
9687 if (!bfd_get_section_contents (ibfd, sec, buf,
9688 rel->r_offset & ~3, 4))
9689 goto got_error_ret;
9690 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9691 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9692 continue;
9693 break;
4a421c53
AM
9694
9695 case R_PPC64_GOT_PCREL34:
9696 pc = rel->r_offset;
9697 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9698 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9699 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9700 continue;
9701 if (!bfd_get_section_contents (ibfd, sec, buf,
9702 rel->r_offset & ~3, 8))
9703 goto got_error_ret;
9704 insn = bfd_get_32 (ibfd, buf);
9705 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9706 continue;
9707 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9708 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9709 continue;
9710 break;
066f4018 9711 }
bb22a418 9712#undef LIMIT_ADJUST
066f4018
AM
9713
9714 if (h != NULL)
9715 ent = h->got.glist;
9716 else
9717 {
9718 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9719 ent = local_got_ents[r_symndx];
9720 }
9721 for (; ent != NULL; ent = ent->next)
133a1f60 9722 if (ent->addend == rel->r_addend
066f4018
AM
9723 && ent->owner == ibfd
9724 && ent->tls_type == 0)
9725 break;
9726 BFD_ASSERT (ent && ent->got.refcount > 0);
9727 ent->got.refcount -= 1;
9728 }
9729
9730 if (elf_section_data (sec)->relocs != relstart)
9731 free (relstart);
9732 }
9733
9734 if (local_syms != NULL
9735 && symtab_hdr->contents != (unsigned char *) local_syms)
9736 {
9737 if (!info->keep_memory)
9738 free (local_syms);
9739 else
9740 symtab_hdr->contents = (unsigned char *) local_syms;
9741 }
9742 }
9743
0a1b45a2 9744 return true;
c5614fa4
AM
9745}
9746
1bbe0902
AM
9747/* Return true iff input section I references the TOC using
9748 instructions limited to +/-32k offsets. */
9749
0a1b45a2 9750bool
1bbe0902
AM
9751ppc64_elf_has_small_toc_reloc (asection *i)
9752{
9753 return (is_ppc64_elf (i->owner)
9754 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9755}
9756
927be08e
AM
9757/* Allocate space for one GOT entry. */
9758
9759static void
9760allocate_got (struct elf_link_hash_entry *h,
9761 struct bfd_link_info *info,
9762 struct got_entry *gent)
9763{
9764 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9765 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9766 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9767 ? 16 : 8);
9768 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9769 ? 2 : 1) * sizeof (Elf64_External_Rela);
9770 asection *got = ppc64_elf_tdata (gent->owner)->got;
9771
9772 gent->got.offset = got->size;
9773 got->size += entsize;
9774
19e08130 9775 if (h->type == STT_GNU_IFUNC)
927be08e 9776 {
33e44f2e 9777 htab->elf.irelplt->size += rentsize;
19e08130 9778 htab->got_reli_size += rentsize;
927be08e 9779 }
f15d0b54 9780 else if (((bfd_link_pic (info)
1657026c
AM
9781 && (gent->tls_type == 0
9782 ? !info->enable_dt_relr
9783 : !(bfd_link_executable (info)
9784 && SYMBOL_REFERENCES_LOCAL (info, h))))
f0158f44
AM
9785 || (htab->elf.dynamic_sections_created
9786 && h->dynindx != -1
9787 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9788 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9789 {
19e08130 9790 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9791 relgot->size += rentsize;
927be08e
AM
9792 }
9793}
9794
7865406b
AM
9795/* This function merges got entries in the same toc group. */
9796
9797static void
9798merge_got_entries (struct got_entry **pent)
9799{
9800 struct got_entry *ent, *ent2;
9801
9802 for (ent = *pent; ent != NULL; ent = ent->next)
9803 if (!ent->is_indirect)
9804 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9805 if (!ent2->is_indirect
9806 && ent2->addend == ent->addend
9807 && ent2->tls_type == ent->tls_type
9808 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9809 {
0a1b45a2 9810 ent2->is_indirect = true;
7865406b
AM
9811 ent2->got.ent = ent;
9812 }
9813}
9814
46434633 9815/* If H is undefined, make it dynamic if that makes sense. */
f0158f44 9816
0a1b45a2 9817static bool
46434633
AM
9818ensure_undef_dynamic (struct bfd_link_info *info,
9819 struct elf_link_hash_entry *h)
f0158f44
AM
9820{
9821 struct elf_link_hash_table *htab = elf_hash_table (info);
9822
9823 if (htab->dynamic_sections_created
46434633
AM
9824 && ((info->dynamic_undefined_weak != 0
9825 && h->root.type == bfd_link_hash_undefweak)
9826 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9827 && h->dynindx == -1
9828 && !h->forced_local
9829 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9830 return bfd_elf_link_record_dynamic_symbol (info, h);
0a1b45a2 9831 return true;
f0158f44
AM
9832}
9833
30845f11
AM
9834/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9835 usual htab->elf.splt section for a PLT entry. */
9836
9837static inline
0a1b45a2 9838bool use_local_plt (struct bfd_link_info *info,
30845f11
AM
9839 struct elf_link_hash_entry *h)
9840{
9841 return (h == NULL
9842 || h->dynindx == -1
9843 || !elf_hash_table (info)->dynamic_sections_created);
9844}
9845
65f38f15
AM
9846/* Allocate space in .plt, .got and associated reloc sections for
9847 dynamic relocs. */
5bd4f169 9848
0a1b45a2 9849static bool
4ce794b7 9850allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9851{
65f38f15
AM
9852 struct bfd_link_info *info;
9853 struct ppc_link_hash_table *htab;
5bd4f169 9854 asection *s;
65f38f15 9855 struct ppc_link_hash_entry *eh;
0b8bcf0d 9856 struct got_entry **pgent, *gent;
5bd4f169 9857
e92d460e 9858 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9859 return true;
5bd4f169 9860
65f38f15
AM
9861 info = (struct bfd_link_info *) inf;
9862 htab = ppc_hash_table (info);
4dfe6ac6 9863 if (htab == NULL)
0a1b45a2 9864 return false;
5bd4f169 9865
ed7007c1 9866 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9867 /* Run through the TLS GD got entries first if we're changing them
9868 to TPREL. */
b00a0a86 9869 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9870 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9871 if (gent->got.refcount > 0
9872 && (gent->tls_type & TLS_GD) != 0)
9873 {
9874 /* This was a GD entry that has been converted to TPREL. If
9875 there happens to be a TPREL entry we can use that one. */
9876 struct got_entry *ent;
9877 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9878 if (ent->got.refcount > 0
9879 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9880 && ent->addend == gent->addend
9881 && ent->owner == gent->owner)
951fd09b
AM
9882 {
9883 gent->got.refcount = 0;
9884 break;
9885 }
9886
9887 /* If not, then we'll be using our own TPREL entry. */
9888 if (gent->got.refcount != 0)
9889 gent->tls_type = TLS_TLS | TLS_TPREL;
9890 }
9891
7865406b
AM
9892 /* Remove any list entry that won't generate a word in the GOT before
9893 we call merge_got_entries. Otherwise we risk merging to empty
9894 entries. */
0b8bcf0d
AM
9895 pgent = &h->got.glist;
9896 while ((gent = *pgent) != NULL)
411e1bfb 9897 if (gent->got.refcount > 0)
7865406b
AM
9898 {
9899 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9900 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9901 {
9902 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9903 *pgent = gent->next;
9904 }
9905 else
9906 pgent = &gent->next;
9907 }
9908 else
9909 *pgent = gent->next;
9910
9911 if (!htab->do_multi_toc)
9912 merge_got_entries (&h->got.glist);
9913
9914 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9915 if (!gent->is_indirect)
411e1bfb 9916 {
ec73ddcd
AM
9917 /* Ensure we catch all the cases where this symbol should
9918 be made dynamic. */
46434633 9919 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9920 return false;
65f38f15 9921
0c8d6e5c 9922 if (!is_ppc64_elf (gent->owner))
927be08e 9923 abort ();
0ffa91dd 9924
927be08e 9925 allocate_got (h, info, gent);
411e1bfb 9926 }
65f38f15 9927
954b63d4
AM
9928 /* If no dynamic sections we can't have dynamic relocs, except for
9929 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9930 if (!htab->elf.dynamic_sections_created
9931 && h->type != STT_GNU_IFUNC)
190eb1dd 9932 h->dyn_relocs = NULL;
8a2058b5 9933
529fe20e
AM
9934 /* Discard relocs on undefined symbols that must be local. */
9935 else if (h->root.type == bfd_link_hash_undefined
9936 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9937 h->dyn_relocs = NULL;
529fe20e 9938
954b63d4
AM
9939 /* Also discard relocs on undefined weak syms with non-default
9940 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9941 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9942 h->dyn_relocs = NULL;
954b63d4 9943
190eb1dd 9944 if (h->dyn_relocs != NULL)
65f38f15 9945 {
1657026c 9946 struct ppc_dyn_relocs *p, **pp;
8a2058b5 9947
57e7d118
AM
9948 /* In the shared -Bsymbolic case, discard space allocated for
9949 dynamic pc-relative relocs against symbols which turn out to
9950 be defined in regular objects. For the normal shared case,
9951 discard space for relocs that have become local due to symbol
9952 visibility changes. */
57e7d118 9953 if (bfd_link_pic (info))
65f38f15 9954 {
57e7d118
AM
9955 /* Relocs that use pc_count are those that appear on a call
9956 insn, or certain REL relocs (see must_be_dyn_reloc) that
9957 can be generated via assembly. We want calls to
9958 protected symbols to resolve directly to the function
9959 rather than going via the plt. If people want function
9960 pointer comparisons to work as expected then they should
9961 avoid writing weird assembly. */
9962 if (SYMBOL_CALLS_LOCAL (info, h))
9963 {
1657026c
AM
9964 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9965 (p = *pp) != NULL;
9966 )
57e7d118
AM
9967 {
9968 p->count -= p->pc_count;
9969 p->pc_count = 0;
9970 if (p->count == 0)
9971 *pp = p->next;
9972 else
9973 pp = &p->next;
9974 }
9975 }
65f38f15 9976
190eb1dd 9977 if (h->dyn_relocs != NULL)
5bd4f169 9978 {
ec73ddcd
AM
9979 /* Ensure we catch all the cases where this symbol
9980 should be made dynamic. */
46434633 9981 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9982 return false;
5bd4f169 9983 }
65f38f15 9984 }
ec73ddcd
AM
9985
9986 /* For a fixed position executable, discard space for
9987 relocs against symbols which are not dynamic. */
9988 else if (h->type != STT_GNU_IFUNC)
57e7d118 9989 {
49160308
AM
9990 if ((h->dynamic_adjusted
9991 || (h->ref_regular
9992 && h->root.type == bfd_link_hash_undefweak
9993 && (info->dynamic_undefined_weak > 0
9994 || !_bfd_elf_readonly_dynrelocs (h))))
529fe20e
AM
9995 && !h->def_regular
9996 && !ELF_COMMON_DEF_P (h))
f0158f44 9997 {
ec73ddcd
AM
9998 /* Ensure we catch all the cases where this symbol
9999 should be made dynamic. */
46434633 10000 if (!ensure_undef_dynamic (info, h))
0a1b45a2 10001 return false;
dfbb6ac9 10002
ec73ddcd 10003 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 10004 if (h->dynindx == -1)
190eb1dd 10005 h->dyn_relocs = NULL;
f0158f44
AM
10006 }
10007 else
190eb1dd 10008 h->dyn_relocs = NULL;
57e7d118
AM
10009 }
10010
10011 /* Finally, allocate space. */
1657026c 10012 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
57e7d118 10013 {
1657026c 10014 unsigned int count;
57e7d118
AM
10015 asection *sreloc = elf_section_data (p->sec)->sreloc;
10016 if (eh->elf.type == STT_GNU_IFUNC)
10017 sreloc = htab->elf.irelplt;
1657026c 10018 count = p->count;
8ffb6df2
AM
10019 if (info->enable_dt_relr
10020 && ((!NO_OPD_RELOCS
10021 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10022 || (eh->elf.type != STT_GNU_IFUNC
10023 && SYMBOL_REFERENCES_LOCAL (info, h))))
1657026c
AM
10024 count -= p->rel_count;
10025 sreloc->size += count * sizeof (Elf64_External_Rela);
dfbb6ac9 10026 }
65f38f15 10027 }
57e7d118 10028
2d7ad24e
AM
10029 /* We might need a PLT entry when the symbol
10030 a) is dynamic, or
10031 b) is an ifunc, or
10032 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10033 d) has plt16 relocs and we are linking statically. */
10034 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10035 || h->type == STT_GNU_IFUNC
10036 || (h->needs_plt && h->dynamic_adjusted)
10037 || (h->needs_plt
10038 && h->def_regular
10039 && !htab->elf.dynamic_sections_created
3e04d765 10040 && !htab->can_convert_all_inline_plt
ed7007c1 10041 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 10042 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 10043 {
57e7d118 10044 struct plt_entry *pent;
0a1b45a2 10045 bool doneone = false;
57e7d118
AM
10046 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10047 if (pent->plt.refcount > 0)
10048 {
b2936612
AM
10049 if (!ensure_undef_dynamic (info, h))
10050 return false;
10051
30845f11 10052 if (use_local_plt (info, h))
57e7d118 10053 {
2d7ad24e
AM
10054 if (h->type == STT_GNU_IFUNC)
10055 {
10056 s = htab->elf.iplt;
10057 pent->plt.offset = s->size;
10058 s->size += PLT_ENTRY_SIZE (htab);
10059 s = htab->elf.irelplt;
10060 }
10061 else
10062 {
10063 s = htab->pltlocal;
10064 pent->plt.offset = s->size;
10065 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
1657026c
AM
10066 s = NULL;
10067 if (bfd_link_pic (info)
10068 && !(info->enable_dt_relr && !htab->opd_abi))
10069 s = htab->relpltlocal;
2d7ad24e 10070 }
57e7d118
AM
10071 }
10072 else
10073 {
10074 /* If this is the first .plt entry, make room for the special
10075 first entry. */
10076 s = htab->elf.splt;
10077 if (s->size == 0)
10078 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 10079
57e7d118 10080 pent->plt.offset = s->size;
65f38f15 10081
57e7d118
AM
10082 /* Make room for this entry. */
10083 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 10084
57e7d118
AM
10085 /* Make room for the .glink code. */
10086 s = htab->glink;
10087 if (s->size == 0)
9e390558 10088 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
10089 if (htab->opd_abi)
10090 {
10091 /* We need bigger stubs past index 32767. */
9e390558 10092 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
10093 s->size += 4;
10094 s->size += 2*4;
10095 }
10096 else
10097 s->size += 4;
65f38f15 10098
57e7d118
AM
10099 /* We also need to make an entry in the .rela.plt section. */
10100 s = htab->elf.srelplt;
10101 }
2d7ad24e
AM
10102 if (s != NULL)
10103 s->size += sizeof (Elf64_External_Rela);
0a1b45a2 10104 doneone = true;
57e7d118
AM
10105 }
10106 else
10107 pent->plt.offset = (bfd_vma) -1;
10108 if (!doneone)
10109 {
10110 h->plt.plist = NULL;
10111 h->needs_plt = 0;
10112 }
65f38f15 10113 }
57e7d118 10114 else
65f38f15 10115 {
57e7d118
AM
10116 h->plt.plist = NULL;
10117 h->needs_plt = 0;
65f38f15
AM
10118 }
10119
0a1b45a2 10120 return true;
65f38f15
AM
10121}
10122
9e390558
AM
10123#define PPC_LO(v) ((v) & 0xffff)
10124#define PPC_HI(v) (((v) >> 16) & 0xffff)
10125#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
10126#define D34(v) \
10127 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10128#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 10129
a345bc8d
AM
10130/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10131 to set up space for global entry stubs. These are put in glink,
10132 after the branch table. */
65f38f15 10133
0a1b45a2 10134static bool
a345bc8d 10135size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 10136{
a345bc8d
AM
10137 struct bfd_link_info *info;
10138 struct ppc_link_hash_table *htab;
10139 struct plt_entry *pent;
9e390558 10140 asection *s, *plt;
65f38f15 10141
a345bc8d 10142 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 10143 return true;
65f38f15 10144
a345bc8d 10145 if (!h->pointer_equality_needed)
0a1b45a2 10146 return true;
65f38f15 10147
a345bc8d 10148 if (h->def_regular)
0a1b45a2 10149 return true;
65f38f15 10150
a345bc8d
AM
10151 info = inf;
10152 htab = ppc_hash_table (info);
10153 if (htab == NULL)
0a1b45a2 10154 return false;
a345bc8d 10155
9e390558
AM
10156 s = htab->global_entry;
10157 plt = htab->elf.splt;
a345bc8d
AM
10158 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10159 if (pent->plt.offset != (bfd_vma) -1
10160 && pent->addend == 0)
10161 {
afe397ea
AM
10162 /* For ELFv2, if this symbol is not defined in a regular file
10163 and we are not generating a shared library or pie, then we
10164 need to define the symbol in the executable on a call stub.
10165 This is to avoid text relocations. */
9e390558
AM
10166 bfd_vma off, stub_align, stub_off, stub_size;
10167 unsigned int align_power;
10168
10169 stub_size = 16;
10170 stub_off = s->size;
10171 if (htab->params->plt_stub_align >= 0)
10172 align_power = htab->params->plt_stub_align;
10173 else
10174 align_power = -htab->params->plt_stub_align;
10175 /* Setting section alignment is delayed until we know it is
10176 non-empty. Otherwise the .text output section will be
10177 aligned at least to plt_stub_align even when no global
10178 entry stubs are needed. */
10179 if (s->alignment_power < align_power)
10180 s->alignment_power = align_power;
10181 stub_align = (bfd_vma) 1 << align_power;
10182 if (htab->params->plt_stub_align >= 0
10183 || ((((stub_off + stub_size - 1) & -stub_align)
10184 - (stub_off & -stub_align))
10185 > ((stub_size - 1) & -stub_align)))
10186 stub_off = (stub_off + stub_align - 1) & -stub_align;
10187 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10188 off -= stub_off + s->output_offset + s->output_section->vma;
10189 /* Note that for --plt-stub-align negative we have a possible
10190 dependency between stub offset and size. Break that
10191 dependency by assuming the max stub size when calculating
10192 the stub offset. */
10193 if (PPC_HA (off) == 0)
10194 stub_size -= 4;
8a2058b5 10195 h->root.type = bfd_link_hash_defined;
afe397ea 10196 h->root.u.def.section = s;
9e390558
AM
10197 h->root.u.def.value = stub_off;
10198 s->size = stub_off + stub_size;
a345bc8d
AM
10199 break;
10200 }
0a1b45a2 10201 return true;
a345bc8d
AM
10202}
10203
65f38f15
AM
10204/* Set the sizes of the dynamic sections. */
10205
0a1b45a2 10206static bool
ee67d69a 10207ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10208 struct bfd_link_info *info)
65f38f15
AM
10209{
10210 struct ppc_link_hash_table *htab;
10211 bfd *dynobj;
10212 asection *s;
0a1b45a2 10213 bool relocs;
65f38f15 10214 bfd *ibfd;
7865406b 10215 struct got_entry *first_tlsld;
65f38f15
AM
10216
10217 htab = ppc_hash_table (info);
4dfe6ac6 10218 if (htab == NULL)
0a1b45a2 10219 return false;
4dfe6ac6 10220
65f38f15
AM
10221 dynobj = htab->elf.dynobj;
10222 if (dynobj == NULL)
10223 abort ();
10224
10225 if (htab->elf.dynamic_sections_created)
10226 {
10227 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10228 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10229 {
3d4d4302 10230 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10231 if (s == NULL)
10232 abort ();
eea6121a 10233 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10234 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10235 }
10236 }
10237
10238 /* Set up .got offsets for local syms, and space for local dynamic
10239 relocs. */
c72f2fb2 10240 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10241 {
411e1bfb
AM
10242 struct got_entry **lgot_ents;
10243 struct got_entry **end_lgot_ents;
e054468f
AM
10244 struct plt_entry **local_plt;
10245 struct plt_entry **end_local_plt;
f961d9dd 10246 unsigned char *lgot_masks;
65f38f15
AM
10247 bfd_size_type locsymcount;
10248 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10249
0c8d6e5c 10250 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10251 continue;
10252
10253 for (s = ibfd->sections; s != NULL; s = s->next)
10254 {
1657026c 10255 struct ppc_local_dyn_relocs *p;
65f38f15 10256
6edfbbad 10257 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10258 {
ec338859
AM
10259 if (!bfd_is_abs_section (p->sec)
10260 && bfd_is_abs_section (p->sec->output_section))
10261 {
10262 /* Input section has been discarded, either because
10263 it is a copy of a linkonce section or due to
10264 linker script /DISCARD/, so we'll be discarding
10265 the relocs too. */
10266 }
248866a8 10267 else if (p->count != 0)
ec338859 10268 {
1657026c
AM
10269 unsigned int count;
10270 asection *srel;
10271
10272 count = p->count;
8ffb6df2
AM
10273 if (info->enable_dt_relr
10274 && ((!NO_OPD_RELOCS
10275 && (ppc64_elf_section_data (p->sec)->sec_type
10276 == sec_opd))
10277 || !p->ifunc))
1657026c
AM
10278 count -= p->rel_count;
10279 srel = elf_section_data (p->sec)->sreloc;
19e08130 10280 if (p->ifunc)
33e44f2e 10281 srel = htab->elf.irelplt;
1657026c 10282 srel->size += count * sizeof (Elf64_External_Rela);
248866a8
AM
10283 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10284 info->flags |= DF_TEXTREL;
ec338859 10285 }
65f38f15
AM
10286 }
10287 }
10288
411e1bfb
AM
10289 lgot_ents = elf_local_got_ents (ibfd);
10290 if (!lgot_ents)
65f38f15
AM
10291 continue;
10292
0ffa91dd 10293 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10294 locsymcount = symtab_hdr->sh_info;
411e1bfb 10295 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10296 local_plt = (struct plt_entry **) end_lgot_ents;
10297 end_local_plt = local_plt + locsymcount;
f961d9dd 10298 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10299 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10300 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10301 {
0b8bcf0d 10302 struct got_entry **pent, *ent;
411e1bfb 10303
0b8bcf0d
AM
10304 pent = lgot_ents;
10305 while ((ent = *pent) != NULL)
411e1bfb
AM
10306 if (ent->got.refcount > 0)
10307 {
e7b938ca 10308 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10309 {
927be08e 10310 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10311 *pent = ent->next;
411e1bfb
AM
10312 }
10313 else
10314 {
19e08130
AM
10315 unsigned int ent_size = 8;
10316 unsigned int rel_size = sizeof (Elf64_External_Rela);
10317
eea6121a 10318 ent->got.offset = s->size;
e7b938ca 10319 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10320 {
19e08130
AM
10321 ent_size *= 2;
10322 rel_size *= 2;
10323 }
10324 s->size += ent_size;
37da22e5 10325 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10326 {
33e44f2e 10327 htab->elf.irelplt->size += rel_size;
19e08130
AM
10328 htab->got_reli_size += rel_size;
10329 }
93370e8e 10330 else if (bfd_link_pic (info)
8f22c953
AM
10331 && (ent->tls_type == 0
10332 ? !info->enable_dt_relr
10333 : !bfd_link_executable (info)))
19e08130
AM
10334 {
10335 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10336 srel->size += rel_size;
927be08e 10337 }
0b8bcf0d 10338 pent = &ent->next;
411e1bfb
AM
10339 }
10340 }
10341 else
0b8bcf0d 10342 *pent = ent->next;
65f38f15 10343 }
e054468f 10344
2d7ad24e
AM
10345 /* Allocate space for plt calls to local syms. */
10346 lgot_masks = (unsigned char *) end_local_plt;
10347 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10348 {
10349 struct plt_entry *ent;
10350
10351 for (ent = *local_plt; ent != NULL; ent = ent->next)
10352 if (ent->plt.refcount > 0)
10353 {
2d7ad24e
AM
10354 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10355 {
10356 s = htab->elf.iplt;
10357 ent->plt.offset = s->size;
10358 s->size += PLT_ENTRY_SIZE (htab);
10359 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10360 }
3e04d765
AM
10361 else if (htab->can_convert_all_inline_plt
10362 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10363 ent->plt.offset = (bfd_vma) -1;
10364 else
10365 {
10366 s = htab->pltlocal;
10367 ent->plt.offset = s->size;
10368 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
edca615a
AM
10369 if (bfd_link_pic (info)
10370 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
10371 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10372 }
e054468f
AM
10373 }
10374 else
10375 ent->plt.offset = (bfd_vma) -1;
10376 }
65f38f15
AM
10377 }
10378
10379 /* Allocate global sym .plt and .got entries, and space for global
10380 sym dynamic relocs. */
4ce794b7 10381 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10382
0e1862bb 10383 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10384 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10385
7865406b 10386 first_tlsld = NULL;
c72f2fb2 10387 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10388 {
7865406b
AM
10389 struct got_entry *ent;
10390
0c8d6e5c 10391 if (!is_ppc64_elf (ibfd))
102890f0
AM
10392 continue;
10393
7865406b
AM
10394 ent = ppc64_tlsld_got (ibfd);
10395 if (ent->got.refcount > 0)
102890f0 10396 {
7865406b 10397 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10398 {
0a1b45a2 10399 ent->is_indirect = true;
7865406b
AM
10400 ent->got.ent = first_tlsld;
10401 }
10402 else
10403 {
10404 if (first_tlsld == NULL)
10405 first_tlsld = ent;
10406 s = ppc64_elf_tdata (ibfd)->got;
10407 ent->got.offset = s->size;
10408 ent->owner = ibfd;
10409 s->size += 16;
f749f26e 10410 if (bfd_link_dll (info))
7865406b
AM
10411 {
10412 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10413 srel->size += sizeof (Elf64_External_Rela);
10414 }
102890f0
AM
10415 }
10416 }
10417 else
7865406b 10418 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10419 }
10420
65f38f15
AM
10421 /* We now have determined the sizes of the various dynamic sections.
10422 Allocate memory for them. */
0a1b45a2 10423 relocs = false;
65f38f15
AM
10424 for (s = dynobj->sections; s != NULL; s = s->next)
10425 {
10426 if ((s->flags & SEC_LINKER_CREATED) == 0)
10427 continue;
10428
1657026c 10429 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
721956f4
AM
10430 /* These haven't been allocated yet; don't strip. */
10431 continue;
33e44f2e
AM
10432 else if (s == htab->elf.sgot
10433 || s == htab->elf.splt
10434 || s == htab->elf.iplt
2d7ad24e 10435 || s == htab->pltlocal
c456f082 10436 || s == htab->glink
9e390558 10437 || s == htab->global_entry
5474d94f
AM
10438 || s == htab->elf.sdynbss
10439 || s == htab->elf.sdynrelro)
65f38f15
AM
10440 {
10441 /* Strip this section if we don't need it; see the
10442 comment below. */
5bd4f169 10443 }
58d180e8
AM
10444 else if (s == htab->glink_eh_frame)
10445 {
10446 if (!bfd_is_abs_section (s->output_section))
10447 /* Not sized yet. */
10448 continue;
10449 }
08dedd66 10450 else if (startswith (s->name, ".rela"))
5bd4f169 10451 {
c456f082 10452 if (s->size != 0)
5bd4f169 10453 {
33e44f2e 10454 if (s != htab->elf.srelplt)
0a1b45a2 10455 relocs = true;
5bd4f169
AM
10456
10457 /* We use the reloc_count field as a counter if we need
10458 to copy relocs into the output file. */
10459 s->reloc_count = 0;
10460 }
10461 }
65f38f15 10462 else
5bd4f169
AM
10463 {
10464 /* It's not one of our sections, so don't allocate space. */
10465 continue;
10466 }
10467
eea6121a 10468 if (s->size == 0)
5bd4f169 10469 {
c456f082
AM
10470 /* If we don't need this section, strip it from the
10471 output file. This is mostly to handle .rela.bss and
10472 .rela.plt. We must create both sections in
10473 create_dynamic_sections, because they must be created
10474 before the linker maps input sections to output
10475 sections. The linker does that before
10476 adjust_dynamic_symbol is called, and it is that
10477 function which decides whether anything needs to go
10478 into these sections. */
8423293d 10479 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10480 continue;
10481 }
10482
06bcf541
AM
10483 if (bfd_is_abs_section (s->output_section))
10484 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10485 s->name);
10486
c456f082 10487 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10488 continue;
10489
65f38f15
AM
10490 /* Allocate memory for the section contents. We use bfd_zalloc
10491 here in case unused entries are not reclaimed before the
10492 section's contents are written out. This should not happen,
411e1bfb
AM
10493 but this way if it does we get a R_PPC64_NONE reloc in .rela
10494 sections instead of garbage.
10495 We also rely on the section contents being zero when writing
5474d94f 10496 the GOT and .dynrelro. */
eea6121a 10497 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10498 if (s->contents == NULL)
0a1b45a2 10499 return false;
5bd4f169
AM
10500 }
10501
c72f2fb2 10502 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10503 {
0c8d6e5c 10504 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10505 continue;
10506
e717da7e 10507 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10508 if (s != NULL && s != htab->elf.sgot)
e717da7e 10509 {
eea6121a 10510 if (s->size == 0)
8423293d 10511 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10512 else
10513 {
eea6121a 10514 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10515 if (s->contents == NULL)
0a1b45a2 10516 return false;
e717da7e
AM
10517 }
10518 }
10519 s = ppc64_elf_tdata (ibfd)->relgot;
10520 if (s != NULL)
10521 {
eea6121a 10522 if (s->size == 0)
8423293d 10523 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10524 else
10525 {
eea6121a 10526 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10527 if (s->contents == NULL)
0a1b45a2
AM
10528 return false;
10529 relocs = true;
e717da7e
AM
10530 s->reloc_count = 0;
10531 }
10532 }
10533 }
10534
e86ce104 10535 if (htab->elf.dynamic_sections_created)
5bd4f169 10536 {
0a1b45a2 10537 bool tls_opt;
e8910a83 10538
5bd4f169
AM
10539 /* Add some entries to the .dynamic section. We fill in the
10540 values later, in ppc64_elf_finish_dynamic_sections, but we
10541 must add the entries now so that we get the correct size for
10542 the .dynamic section. The DT_DEBUG entry is filled in by the
10543 dynamic linker and used by the debugger. */
dc810e39 10544#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10545 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10546
0e1862bb 10547 if (bfd_link_executable (info))
5bd4f169 10548 {
dc810e39 10549 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 10550 return false;
5bd4f169
AM
10551 }
10552
33e44f2e 10553 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10554 {
dc810e39
AM
10555 if (!add_dynamic_entry (DT_PLTGOT, 0)
10556 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10557 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10558 || !add_dynamic_entry (DT_JMPREL, 0)
10559 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
0a1b45a2 10560 return false;
5bd4f169
AM
10561 }
10562
ee67d69a 10563 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10564 {
10565 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10566 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
0a1b45a2 10567 return false;
19397422
AM
10568 }
10569
7c9cf415 10570 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10571 && ((htab->tls_get_addr_fd != NULL
10572 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10573 || (htab->tga_desc_fd != NULL
10574 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10575 if (tls_opt || !htab->opd_abi)
10576 {
10577 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
0a1b45a2 10578 return false;
e8910a83 10579 }
a7f2871e 10580
5bd4f169
AM
10581 if (relocs)
10582 {
dc810e39
AM
10583 if (!add_dynamic_entry (DT_RELA, 0)
10584 || !add_dynamic_entry (DT_RELASZ, 0)
10585 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
0a1b45a2 10586 return false;
5bd4f169 10587
65f38f15
AM
10588 /* If any dynamic relocs apply to a read-only section,
10589 then we need a DT_TEXTREL entry. */
248866a8 10590 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10591 elf_link_hash_traverse (&htab->elf,
10592 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10593
65f38f15 10594 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10595 {
65f38f15 10596 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 10597 return false;
5bd4f169 10598 }
5bd4f169 10599 }
5bd4f169 10600 }
65f38f15 10601#undef add_dynamic_entry
5bd4f169 10602
0a1b45a2 10603 return true;
5bd4f169
AM
10604}
10605
a345bc8d
AM
10606/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10607
0a1b45a2 10608static bool
a345bc8d
AM
10609ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10610{
10611 if (h->plt.plist != NULL
10612 && !h->def_regular
10613 && !h->pointer_equality_needed)
0a1b45a2 10614 return false;
a345bc8d
AM
10615
10616 return _bfd_elf_hash_symbol (h);
10617}
10618
721956f4 10619/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10620
7aba54da 10621static inline enum ppc_stub_main_type
4ce794b7
AM
10622ppc_type_of_stub (asection *input_sec,
10623 const Elf_Internal_Rela *rel,
10624 struct ppc_link_hash_entry **hash,
e054468f 10625 struct plt_entry **plt_ent,
6911b7dc
AM
10626 bfd_vma destination,
10627 unsigned long local_off)
5bd4f169 10628{
721956f4
AM
10629 struct ppc_link_hash_entry *h = *hash;
10630 bfd_vma location;
10631 bfd_vma branch_offset;
10632 bfd_vma max_branch_offset;
4ce794b7 10633 enum elf_ppc64_reloc_type r_type;
5bd4f169 10634
721956f4
AM
10635 if (h != NULL)
10636 {
e054468f 10637 struct plt_entry *ent;
7fe2b9a6 10638 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10639 if (h->oh != NULL
10640 && h->oh->is_func_descriptor)
7b8f6675
AM
10641 {
10642 fdh = ppc_follow_link (h->oh);
10643 *hash = fdh;
10644 }
8387904d 10645
e054468f
AM
10646 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10647 if (ent->addend == rel->r_addend
10648 && ent->plt.offset != (bfd_vma) -1)
10649 {
e054468f
AM
10650 *plt_ent = ent;
10651 return ppc_stub_plt_call;
10652 }
5bd4f169 10653
7fe2b9a6
AM
10654 /* Here, we know we don't have a plt entry. If we don't have a
10655 either a defined function descriptor or a defined entry symbol
10656 in a regular object file, then it is pointless trying to make
10657 any other type of stub. */
854b41e7
AM
10658 if (!is_static_defined (&fdh->elf)
10659 && !is_static_defined (&h->elf))
721956f4 10660 return ppc_stub_none;
5d1634d7 10661 }
e054468f
AM
10662 else if (elf_local_got_ents (input_sec->owner) != NULL)
10663 {
10664 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10665 struct plt_entry **local_plt = (struct plt_entry **)
10666 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10667 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10668
10669 if (local_plt[r_symndx] != NULL)
10670 {
10671 struct plt_entry *ent;
10672
10673 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10674 if (ent->addend == rel->r_addend
10675 && ent->plt.offset != (bfd_vma) -1)
10676 {
10677 *plt_ent = ent;
10678 return ppc_stub_plt_call;
10679 }
10680 }
10681 }
5d1634d7 10682
721956f4
AM
10683 /* Determine where the call point is. */
10684 location = (input_sec->output_offset
10685 + input_sec->output_section->vma
10686 + rel->r_offset);
5d1634d7 10687
721956f4
AM
10688 branch_offset = destination - location;
10689 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10690
721956f4
AM
10691 /* Determine if a long branch stub is needed. */
10692 max_branch_offset = 1 << 25;
23cedd1d
AM
10693 if (r_type == R_PPC64_REL14
10694 || r_type == R_PPC64_REL14_BRTAKEN
10695 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10696 max_branch_offset = 1 << 15;
5d1634d7 10697
6911b7dc 10698 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10699 /* We need a stub. Figure out whether a long_branch or plt_branch
10700 is needed later. */
10701 return ppc_stub_long_branch;
5d1634d7 10702
721956f4 10703 return ppc_stub_none;
5d1634d7
AM
10704}
10705
f891966f
AM
10706/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10707 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10708 . mflr %r12
10709 . bcl 20,31,1f
10710 .1: mflr %r11
10711 . mtlr %r12
05d0e962 10712 . lis %r12,xxx-1b@highest
f891966f 10713 . ori %r12,%r12,xxx-1b@higher
05d0e962 10714 . sldi %r12,%r12,32
f891966f 10715 . oris %r12,%r12,xxx-1b@high
05d0e962 10716 . ori %r12,%r12,xxx-1b@l
f891966f 10717 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10718
10719static bfd_byte *
0a1b45a2 10720build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
05d0e962 10721{
f891966f
AM
10722 bfd_put_32 (abfd, MFLR_R12, p);
10723 p += 4;
10724 bfd_put_32 (abfd, BCL_20_31, p);
10725 p += 4;
10726 bfd_put_32 (abfd, MFLR_R11, p);
10727 p += 4;
10728 bfd_put_32 (abfd, MTLR_R12, p);
10729 p += 4;
05d0e962
AM
10730 if (off + 0x8000 < 0x10000)
10731 {
10732 if (load)
10733 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10734 else
10735 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10736 p += 4;
10737 }
10738 else if (off + 0x80008000ULL < 0x100000000ULL)
10739 {
10740 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10741 p += 4;
10742 if (load)
10743 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10744 else
10745 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10746 p += 4;
10747 }
10748 else
10749 {
10750 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10751 {
10752 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10753 p += 4;
10754 }
10755 else
10756 {
10757 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10758 p += 4;
10759 if (((off >> 32) & 0xffff) != 0)
10760 {
10761 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10762 p += 4;
10763 }
10764 }
10765 if (((off >> 32) & 0xffffffffULL) != 0)
10766 {
10767 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10768 p += 4;
10769 }
10770 if (PPC_HI (off) != 0)
10771 {
10772 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10773 p += 4;
10774 }
10775 if (PPC_LO (off) != 0)
10776 {
10777 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10778 p += 4;
10779 }
10780 if (load)
10781 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10782 else
10783 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10784 p += 4;
10785 }
10786 return p;
10787}
10788
10789static unsigned int
10790size_offset (bfd_vma off)
10791{
10792 unsigned int size;
10793 if (off + 0x8000 < 0x10000)
10794 size = 4;
10795 else if (off + 0x80008000ULL < 0x100000000ULL)
10796 size = 8;
10797 else
10798 {
10799 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10800 size = 4;
10801 else
10802 {
10803 size = 4;
10804 if (((off >> 32) & 0xffff) != 0)
10805 size += 4;
10806 }
10807 if (((off >> 32) & 0xffffffffULL) != 0)
10808 size += 4;
10809 if (PPC_HI (off) != 0)
10810 size += 4;
10811 if (PPC_LO (off) != 0)
10812 size += 4;
10813 size += 4;
10814 }
f891966f 10815 return size + 16;
05d0e962
AM
10816}
10817
3d58e1fc
AM
10818static unsigned int
10819num_relocs_for_offset (bfd_vma off)
10820{
10821 unsigned int num_rel;
10822 if (off + 0x8000 < 0x10000)
10823 num_rel = 1;
10824 else if (off + 0x80008000ULL < 0x100000000ULL)
10825 num_rel = 2;
10826 else
10827 {
10828 num_rel = 1;
10829 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10830 && ((off >> 32) & 0xffff) != 0)
10831 num_rel += 1;
10832 if (PPC_HI (off) != 0)
10833 num_rel += 1;
10834 if (PPC_LO (off) != 0)
10835 num_rel += 1;
10836 }
10837 return num_rel;
10838}
10839
10840static Elf_Internal_Rela *
10841emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10842 bfd_vma roff, bfd_vma targ, bfd_vma off)
10843{
10844 bfd_vma relative_targ = targ - (roff - 8);
10845 if (bfd_big_endian (info->output_bfd))
10846 roff += 2;
10847 r->r_offset = roff;
10848 r->r_addend = relative_targ + roff;
10849 if (off + 0x8000 < 0x10000)
10850 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10851 else if (off + 0x80008000ULL < 0x100000000ULL)
10852 {
10853 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10854 ++r;
10855 roff += 4;
10856 r->r_offset = roff;
10857 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10858 r->r_addend = relative_targ + roff;
10859 }
10860 else
10861 {
10862 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10863 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10864 else
10865 {
10866 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10867 if (((off >> 32) & 0xffff) != 0)
10868 {
10869 ++r;
10870 roff += 4;
10871 r->r_offset = roff;
10872 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10873 r->r_addend = relative_targ + roff;
10874 }
10875 }
10876 if (((off >> 32) & 0xffffffffULL) != 0)
10877 roff += 4;
10878 if (PPC_HI (off) != 0)
10879 {
10880 ++r;
10881 roff += 4;
10882 r->r_offset = roff;
10883 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10884 r->r_addend = relative_targ + roff;
10885 }
10886 if (PPC_LO (off) != 0)
10887 {
10888 ++r;
10889 roff += 4;
10890 r->r_offset = roff;
10891 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10892 r->r_addend = relative_targ + roff;
10893 }
10894 }
10895 return r;
10896}
10897
04bdff6a 10898static bfd_byte *
7c1f4227 10899build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
0a1b45a2 10900 bool load)
04bdff6a
AM
10901{
10902 uint64_t insn;
10903 if (off - odd + (1ULL << 33) < 1ULL << 34)
10904 {
10905 off -= odd;
10906 if (odd)
10907 {
10908 bfd_put_32 (abfd, NOP, p);
10909 p += 4;
10910 }
10911 if (load)
10912 insn = PLD_R12_PC;
10913 else
10914 insn = PADDI_R12_PC;
10915 insn |= D34 (off);
10916 bfd_put_32 (abfd, insn >> 32, p);
10917 p += 4;
10918 bfd_put_32 (abfd, insn, p);
10919 }
10920 /* The minimum value for paddi is -0x200000000. The minimum value
10921 for li is -0x8000, which when shifted by 34 and added gives a
10922 minimum value of -0x2000200000000. The maximum value is
10923 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10924 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10925 {
10926 off -= 8 - odd;
10927 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10928 p += 4;
10929 if (!odd)
10930 {
10931 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10932 p += 4;
10933 }
10934 insn = PADDI_R12_PC | D34 (off);
10935 bfd_put_32 (abfd, insn >> 32, p);
10936 p += 4;
10937 bfd_put_32 (abfd, insn, p);
10938 p += 4;
10939 if (odd)
10940 {
10941 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10942 p += 4;
10943 }
10944 if (load)
10945 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10946 else
10947 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10948 }
10949 else
10950 {
10951 off -= odd + 8;
10952 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10953 p += 4;
10954 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10955 p += 4;
10956 if (odd)
10957 {
10958 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10959 p += 4;
10960 }
10961 insn = PADDI_R12_PC | D34 (off);
10962 bfd_put_32 (abfd, insn >> 32, p);
10963 p += 4;
10964 bfd_put_32 (abfd, insn, p);
10965 p += 4;
10966 if (!odd)
10967 {
10968 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10969 p += 4;
10970 }
10971 if (load)
10972 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10973 else
10974 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10975 }
10976 p += 4;
10977 return p;
10978}
10979
10980static unsigned int
7c1f4227 10981size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10982{
10983 if (off - odd + (1ULL << 33) < 1ULL << 34)
10984 return odd + 8;
10985 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10986 return 20;
10987 else
10988 return 24;
10989}
10990
10991static unsigned int
7c1f4227 10992num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10993{
10994 if (off - odd + (1ULL << 33) < 1ULL << 34)
10995 return 1;
10996 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10997 return 2;
10998 else
10999 return 3;
11000}
11001
11002static Elf_Internal_Rela *
7c1f4227 11003emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
11004 Elf_Internal_Rela *r, bfd_vma roff,
11005 bfd_vma targ, bfd_vma off, int odd)
11006{
11007 if (off - odd + (1ULL << 33) < 1ULL << 34)
11008 roff += odd;
11009 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11010 {
11011 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11012 r->r_offset = roff + d_offset;
11013 r->r_addend = targ + 8 - odd - d_offset;
11014 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11015 ++r;
11016 roff += 8 - odd;
11017 }
11018 else
11019 {
11020 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11021 r->r_offset = roff + d_offset;
11022 r->r_addend = targ + 8 + odd - d_offset;
11023 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11024 ++r;
11025 roff += 4;
11026 r->r_offset = roff + d_offset;
11027 r->r_addend = targ + 4 + odd - d_offset;
11028 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11029 ++r;
11030 roff += 4 + odd;
11031 }
11032 r->r_offset = roff;
11033 r->r_addend = targ;
11034 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11035 return r;
11036}
11037
df136d64
AM
11038/* Emit .eh_frame opcode to advance pc by DELTA. */
11039
11040static bfd_byte *
11041eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11042{
11043 delta /= 4;
11044 if (delta < 64)
11045 *eh++ = DW_CFA_advance_loc + delta;
11046 else if (delta < 256)
11047 {
11048 *eh++ = DW_CFA_advance_loc1;
11049 *eh++ = delta;
11050 }
11051 else if (delta < 65536)
11052 {
11053 *eh++ = DW_CFA_advance_loc2;
11054 bfd_put_16 (abfd, delta, eh);
11055 eh += 2;
11056 }
11057 else
11058 {
11059 *eh++ = DW_CFA_advance_loc4;
11060 bfd_put_32 (abfd, delta, eh);
11061 eh += 4;
11062 }
11063 return eh;
11064}
11065
11066/* Size of required .eh_frame opcode to advance pc by DELTA. */
11067
11068static unsigned int
11069eh_advance_size (unsigned int delta)
11070{
11071 if (delta < 64 * 4)
11072 /* DW_CFA_advance_loc+[1..63]. */
11073 return 1;
11074 if (delta < 256 * 4)
11075 /* DW_CFA_advance_loc1, byte. */
11076 return 2;
11077 if (delta < 65536 * 4)
11078 /* DW_CFA_advance_loc2, 2 bytes. */
11079 return 3;
11080 /* DW_CFA_advance_loc4, 4 bytes. */
11081 return 5;
11082}
11083
794e51c0
AM
11084/* With power7 weakly ordered memory model, it is possible for ld.so
11085 to update a plt entry in one thread and have another thread see a
11086 stale zero toc entry. To avoid this we need some sort of acquire
11087 barrier in the call stub. One solution is to make the load of the
11088 toc word seem to appear to depend on the load of the function entry
11089 word. Another solution is to test for r2 being zero, and branch to
11090 the appropriate glink entry if so.
11091
11092 . fake dep barrier compare
71a39c98
AM
11093 . ld 12,xxx(2) ld 12,xxx(2)
11094 . mtctr 12 mtctr 12
11095 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
11096 . add 2,2,11 cmpldi 2,0
11097 . ld 2,xxx+8(2) bnectr+
11098 . bctr b <glink_entry>
11099
11100 The solution involving the compare turns out to be faster, so
11101 that's what we use unless the branch won't reach. */
11102
11103#define ALWAYS_USE_FAKE_DEP 0
11104#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 11105
794e51c0
AM
11106static inline unsigned int
11107plt_stub_size (struct ppc_link_hash_table *htab,
11108 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
11109 bfd_vma off,
11110 unsigned int odd)
794e51c0 11111{
05d0e962 11112 unsigned size;
b9e5796b 11113
7aba54da 11114 if (stub_entry->type.sub == ppc_stub_notoc)
05d0e962 11115 {
7aba54da
AM
11116 size = 8 + size_power10_offset (off, odd);
11117 if (stub_entry->type.r2save)
11118 size += 4;
11119 }
11120 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11121 {
11122 size = 8 + size_offset (off - 8);
11123 if (stub_entry->type.r2save)
05d0e962 11124 size += 4;
05d0e962 11125 }
29433886 11126 else
b9e5796b 11127 {
29433886 11128 size = 12;
7aba54da 11129 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
b9e5796b 11130 size += 4;
29433886 11131 if (PPC_HA (off) != 0)
b9e5796b 11132 size += 4;
29433886
AM
11133 if (htab->opd_abi)
11134 {
11135 size += 4;
11136 if (htab->params->plt_static_chain)
11137 size += 4;
11138 if (htab->params->plt_thread_safe
11139 && htab->elf.dynamic_sections_created
11140 && stub_entry->h != NULL
11141 && stub_entry->h->elf.dynindx != -1)
11142 size += 8;
11143 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11144 != PPC_HA (off))
11145 size += 4;
11146 }
b9e5796b 11147 }
794e51c0 11148 if (stub_entry->h != NULL
ed7007c1 11149 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11150 && htab->params->tls_get_addr_opt)
f378ab09 11151 {
29433886 11152 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11153 {
29433886 11154 size += 30 * 4;
7aba54da 11155 if (stub_entry->type.r2save)
29433886 11156 size += 4;
9e7028aa
AM
11157 }
11158 else
11159 {
29433886 11160 size += 7 * 4;
7aba54da 11161 if (stub_entry->type.r2save)
29433886 11162 size += 6 * 4;
9e7028aa 11163 }
f378ab09 11164 }
794e51c0
AM
11165 return size;
11166}
11167
2420fff6
AM
11168/* Depending on the sign of plt_stub_align:
11169 If positive, return the padding to align to a 2**plt_stub_align
11170 boundary.
11171 If negative, if this stub would cross fewer 2**plt_stub_align
11172 boundaries if we align, then return the padding needed to do so. */
11173
794e51c0
AM
11174static inline unsigned int
11175plt_stub_pad (struct ppc_link_hash_table *htab,
11176 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
11177 bfd_vma plt_off,
11178 unsigned int odd)
794e51c0 11179{
2420fff6 11180 int stub_align;
1aa42141 11181 unsigned stub_size;
6f20ed8a 11182 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 11183
2420fff6
AM
11184 if (htab->params->plt_stub_align >= 0)
11185 {
11186 stub_align = 1 << htab->params->plt_stub_align;
11187 if ((stub_off & (stub_align - 1)) != 0)
11188 return stub_align - (stub_off & (stub_align - 1));
11189 return 0;
11190 }
11191
11192 stub_align = 1 << -htab->params->plt_stub_align;
29433886 11193 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 11194 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 11195 > ((stub_size - 1) & -stub_align))
794e51c0
AM
11196 return stub_align - (stub_off & (stub_align - 1));
11197 return 0;
11198}
11199
7aba54da 11200/* Build a toc using .plt call stub. */
794e51c0
AM
11201
11202static inline bfd_byte *
11203build_plt_stub (struct ppc_link_hash_table *htab,
11204 struct ppc_stub_hash_entry *stub_entry,
11205 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11206{
e7d1c40c 11207 bfd *obfd = htab->params->stub_bfd;
0a1b45a2
AM
11208 bool plt_load_toc = htab->opd_abi;
11209 bool plt_static_chain = htab->params->plt_static_chain;
11210 bool plt_thread_safe = (htab->params->plt_thread_safe
11211 && htab->elf.dynamic_sections_created
11212 && stub_entry->h != NULL
11213 && stub_entry->h->elf.dynindx != -1);
11214 bool use_fake_dep = plt_thread_safe;
794e51c0
AM
11215 bfd_vma cmp_branch_off = 0;
11216
11217 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11218 && plt_load_toc
794e51c0 11219 && plt_thread_safe
9e51d549
AM
11220 && !(stub_entry->h != NULL
11221 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11222 && htab->params->tls_get_addr_opt))
794e51c0
AM
11223 {
11224 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11225 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11226 / PLT_ENTRY_SIZE (htab));
9e390558 11227 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11228 bfd_vma to, from;
11229
68d62958
AM
11230 if (pltindex > 32768)
11231 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11232 to = (glinkoff
11233 + htab->glink->output_offset
11234 + htab->glink->output_section->vma);
6f20ed8a 11235 from = (p - stub_entry->group->stub_sec->contents
7aba54da 11236 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0
AM
11237 + 4 * (PPC_HA (offset) != 0)
11238 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11239 != PPC_HA (offset))
11240 + 4 * (plt_static_chain != 0)
11241 + 20
6f20ed8a
AM
11242 + stub_entry->group->stub_sec->output_offset
11243 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11244 cmp_branch_off = to - from;
11245 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11246 }
11247
ac2df442
AM
11248 if (PPC_HA (offset) != 0)
11249 {
176a0d42
AM
11250 if (r != NULL)
11251 {
7aba54da 11252 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11253 r[0].r_offset += 4;
176a0d42 11254 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11255 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11256 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11257 r[1].r_addend = r[0].r_addend;
b9e5796b 11258 if (plt_load_toc)
176a0d42 11259 {
b9e5796b 11260 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11261 {
b9e5796b
AM
11262 r[2].r_offset = r[1].r_offset + 4;
11263 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11264 r[2].r_addend = r[0].r_addend;
11265 }
11266 else
11267 {
11268 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11269 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11270 r[2].r_addend = r[0].r_addend + 8;
11271 if (plt_static_chain)
11272 {
11273 r[3].r_offset = r[2].r_offset + 4;
11274 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11275 r[3].r_addend = r[0].r_addend + 16;
11276 }
c7131b65 11277 }
176a0d42
AM
11278 }
11279 }
7aba54da 11280 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11281 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11282 if (plt_load_toc)
11283 {
11284 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11285 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11286 }
11287 else
11288 {
11289 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11290 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11291 }
b9e5796b
AM
11292 if (plt_load_toc
11293 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11294 {
71a39c98 11295 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11296 offset = 0;
11297 }
71a39c98 11298 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11299 if (plt_load_toc)
794e51c0 11300 {
b9e5796b
AM
11301 if (use_fake_dep)
11302 {
11303 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11304 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11305 }
11306 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11307 if (plt_static_chain)
11308 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11309 }
ac2df442
AM
11310 }
11311 else
11312 {
176a0d42
AM
11313 if (r != NULL)
11314 {
7aba54da 11315 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
794e51c0 11316 r[0].r_offset += 4;
176a0d42 11317 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11318 if (plt_load_toc)
176a0d42 11319 {
b9e5796b 11320 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11321 {
b9e5796b
AM
11322 r[1].r_offset = r[0].r_offset + 4;
11323 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11324 r[1].r_addend = r[0].r_addend;
11325 }
11326 else
11327 {
11328 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11329 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11330 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11331 if (plt_static_chain)
11332 {
11333 r[2].r_offset = r[1].r_offset + 4;
11334 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11335 r[2].r_addend = r[0].r_addend + 8;
11336 }
c7131b65 11337 }
176a0d42
AM
11338 }
11339 }
7aba54da 11340 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
a078d95a 11341 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11342 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11343 if (plt_load_toc
11344 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11345 {
11346 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11347 offset = 0;
11348 }
71a39c98 11349 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11350 if (plt_load_toc)
794e51c0 11351 {
b9e5796b
AM
11352 if (use_fake_dep)
11353 {
11354 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11355 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11356 }
11357 if (plt_static_chain)
11358 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11359 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11360 }
ac2df442 11361 }
b9e5796b 11362 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11363 {
11364 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11365 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11366 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11367 }
11368 else
407aa07c 11369 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11370 return p;
11371}
11372
a7f2871e
AM
11373/* Build a special .plt call stub for __tls_get_addr. */
11374
b9ca1af6 11375#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11376#define LD_R12_0R3 0xe9830000
11377#define MR_R0_R3 0x7c601b78
b9ca1af6 11378#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11379#define ADD_R3_R12_R13 0x7c6c6a14
11380#define BEQLR 0x4d820020
11381#define MR_R3_R0 0x7c030378
a7f2871e 11382#define BCTRL 0x4e800421
a7f2871e 11383
29433886
AM
11384static bfd_byte *
11385build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11386 struct ppc_stub_hash_entry *stub_entry,
29433886 11387 bfd_byte *p)
a7f2871e 11388{
e7d1c40c 11389 bfd *obfd = htab->params->stub_bfd;
794e51c0 11390
b9ca1af6 11391 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11392 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11393 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11394 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11395 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11396 bfd_put_32 (obfd, BEQLR, p), p += 4;
11397 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11398
29433886
AM
11399 if (!htab->params->no_tls_get_addr_regsave)
11400 p = tls_get_addr_prologue (obfd, p, htab);
7aba54da 11401 else if (stub_entry->type.r2save)
29433886 11402 {
9e7028aa
AM
11403 bfd_put_32 (obfd, MFLR_R0, p);
11404 p += 4;
11405 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11406 p += 4;
29433886
AM
11407 }
11408 return p;
11409}
9e7028aa 11410
29433886
AM
11411static bfd_byte *
11412build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11413 struct ppc_stub_hash_entry *stub_entry,
11414 bfd_byte *p,
11415 bfd_byte *loc)
11416{
11417 bfd *obfd = htab->params->stub_bfd;
11418
11419 if (!htab->params->no_tls_get_addr_regsave)
11420 {
11421 bfd_put_32 (obfd, BCTRL, p - 4);
11422
7aba54da 11423 if (stub_entry->type.r2save)
29433886
AM
11424 {
11425 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11426 p += 4;
11427 }
11428 p = tls_get_addr_epilogue (obfd, p, htab);
11429 }
7aba54da 11430 else if (stub_entry->type.r2save)
29433886 11431 {
9e7028aa
AM
11432 bfd_put_32 (obfd, BCTRL, p - 4);
11433
11434 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11435 p += 4;
11436 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11437 p += 4;
11438 bfd_put_32 (obfd, MTLR_R0, p);
11439 p += 4;
11440 bfd_put_32 (obfd, BLR, p);
11441 p += 4;
11442 }
a7f2871e 11443
df136d64
AM
11444 if (htab->glink_eh_frame != NULL
11445 && htab->glink_eh_frame->size != 0)
11446 {
11447 bfd_byte *base, *eh;
df136d64
AM
11448
11449 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11450 eh = base + stub_entry->group->eh_size;
29433886
AM
11451
11452 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11453 {
29433886
AM
11454 unsigned int cfa_updt, delta, i;
11455
9e7028aa
AM
11456 /* After the bctrl, lr has been modified so we need to emit
11457 .eh_frame info saying the return address is on the stack. In
11458 fact we must put the EH info at or before the call rather
11459 than after it, because the EH info for a call needs to be
11460 specified by that point.
11461 See libgcc/unwind-dw2.c execute_cfa_program.
11462 Any stack pointer update must be described immediately after
11463 the instruction making the change, and since the stdu occurs
11464 after saving regs we put all the reg saves and the cfa
11465 change there. */
11466 cfa_updt = stub_entry->stub_offset + 18 * 4;
11467 delta = cfa_updt - stub_entry->group->lr_restore;
11468 stub_entry->group->lr_restore
11469 = stub_entry->stub_offset + (p - loc) - 4;
11470 eh = eh_advance (htab->elf.dynobj, eh, delta);
11471 *eh++ = DW_CFA_def_cfa_offset;
11472 if (htab->opd_abi)
11473 {
11474 *eh++ = 128;
11475 *eh++ = 1;
11476 }
11477 else
11478 *eh++ = 96;
11479 *eh++ = DW_CFA_offset_extended_sf;
11480 *eh++ = 65;
11481 *eh++ = (-16 / 8) & 0x7f;
11482 for (i = 4; i < 12; i++)
11483 {
11484 *eh++ = DW_CFA_offset + i;
11485 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11486 }
11487 *eh++ = (DW_CFA_advance_loc
11488 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11489 *eh++ = DW_CFA_def_cfa_offset;
11490 *eh++ = 0;
11491 for (i = 4; i < 12; i++)
11492 *eh++ = DW_CFA_restore + i;
11493 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11494 *eh++ = DW_CFA_restore_extended;
11495 *eh++ = 65;
11496 stub_entry->group->eh_size = eh - base;
11497 }
7aba54da 11498 else if (stub_entry->type.r2save)
29433886
AM
11499 {
11500 unsigned int lr_used, delta;
11501
11502 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11503 delta = lr_used - stub_entry->group->lr_restore;
11504 stub_entry->group->lr_restore = lr_used + 16;
11505 eh = eh_advance (htab->elf.dynobj, eh, delta);
11506 *eh++ = DW_CFA_offset_extended_sf;
11507 *eh++ = 65;
11508 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11509 *eh++ = DW_CFA_advance_loc + 4;
11510 *eh++ = DW_CFA_restore_extended;
11511 *eh++ = 65;
11512 stub_entry->group->eh_size = eh - base;
9e7028aa 11513 }
df136d64 11514 }
a7f2871e
AM
11515 return p;
11516}
11517
176a0d42
AM
11518static Elf_Internal_Rela *
11519get_relocs (asection *sec, int count)
11520{
11521 Elf_Internal_Rela *relocs;
11522 struct bfd_elf_section_data *elfsec_data;
11523
11524 elfsec_data = elf_section_data (sec);
11525 relocs = elfsec_data->relocs;
11526 if (relocs == NULL)
11527 {
11528 bfd_size_type relsize;
11529 relsize = sec->reloc_count * sizeof (*relocs);
11530 relocs = bfd_alloc (sec->owner, relsize);
11531 if (relocs == NULL)
11532 return NULL;
11533 elfsec_data->relocs = relocs;
d4730f92
BS
11534 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11535 sizeof (Elf_Internal_Shdr));
11536 if (elfsec_data->rela.hdr == NULL)
11537 return NULL;
11538 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11539 * sizeof (Elf64_External_Rela));
11540 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11541 sec->reloc_count = 0;
11542 }
11543 relocs += sec->reloc_count;
11544 sec->reloc_count += count;
11545 return relocs;
11546}
11547
3d58e1fc
AM
11548/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11549 forms, to the equivalent relocs against the global symbol given by
11550 STUB_ENTRY->H. */
11551
0a1b45a2 11552static bool
3d58e1fc
AM
11553use_global_in_relocs (struct ppc_link_hash_table *htab,
11554 struct ppc_stub_hash_entry *stub_entry,
11555 Elf_Internal_Rela *r, unsigned int num_rel)
11556{
11557 struct elf_link_hash_entry **hashes;
11558 unsigned long symndx;
11559 struct ppc_link_hash_entry *h;
11560 bfd_vma symval;
11561
11562 /* Relocs are always against symbols in their own object file. Fake
11563 up global sym hashes for the stub bfd (which has no symbols). */
11564 hashes = elf_sym_hashes (htab->params->stub_bfd);
11565 if (hashes == NULL)
11566 {
11567 bfd_size_type hsize;
11568
11569 /* When called the first time, stub_globals will contain the
11570 total number of symbols seen during stub sizing. After
11571 allocating, stub_globals is used as an index to fill the
11572 hashes array. */
11573 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11574 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11575 if (hashes == NULL)
0a1b45a2 11576 return false;
3d58e1fc
AM
11577 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11578 htab->stub_globals = 1;
11579 }
11580 symndx = htab->stub_globals++;
11581 h = stub_entry->h;
11582 hashes[symndx] = &h->elf;
11583 if (h->oh != NULL && h->oh->is_func)
11584 h = ppc_follow_link (h->oh);
11585 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11586 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11587 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11588 while (num_rel-- != 0)
11589 {
11590 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11591 if (h->elf.root.u.def.section != stub_entry->target_section)
11592 {
11593 /* H is an opd symbol. The addend must be zero, and the
11594 branch reloc is the only one we can convert. */
11595 r->r_addend = 0;
11596 break;
11597 }
11598 else
11599 r->r_addend -= symval;
11600 --r;
11601 }
0a1b45a2 11602 return true;
3d58e1fc
AM
11603}
11604
aa374f67 11605static bfd_vma
25f53a85 11606get_r2off (struct bfd_link_info *info,
aa374f67
AM
11607 struct ppc_stub_hash_entry *stub_entry)
11608{
25f53a85 11609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11610 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11611
11612 if (r2off == 0)
11613 {
11614 /* Support linking -R objects. Get the toc pointer from the
11615 opd entry. */
11616 char buf[8];
b9e5796b
AM
11617 if (!htab->opd_abi)
11618 return r2off;
aa374f67
AM
11619 asection *opd = stub_entry->h->elf.root.u.def.section;
11620 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11621
11622 if (strcmp (opd->name, ".opd") != 0
11623 || opd->reloc_count != 0)
11624 {
2cdcc330
AM
11625 info->callbacks->einfo
11626 (_("%P: cannot find opd entry toc for `%pT'\n"),
11627 stub_entry->h->elf.root.root.string);
aa374f67 11628 bfd_set_error (bfd_error_bad_value);
a7c49797 11629 return (bfd_vma) -1;
aa374f67
AM
11630 }
11631 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11632 return (bfd_vma) -1;
aa374f67 11633 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11634 r2off -= elf_gp (info->output_bfd);
aa374f67 11635 }
6f20ed8a 11636 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11637 return r2off;
11638}
11639
0a1b45a2 11640static bool
4ce794b7 11641ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11642{
721956f4
AM
11643 struct ppc_stub_hash_entry *stub_entry;
11644 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11645 struct bfd_link_info *info;
11646 struct ppc_link_hash_table *htab;
29433886 11647 bfd *obfd;
721956f4 11648 bfd_byte *loc;
3d58e1fc 11649 bfd_byte *p, *relp;
1aa42141 11650 bfd_vma targ, off;
176a0d42 11651 Elf_Internal_Rela *r;
e054468f 11652 asection *plt;
3d58e1fc 11653 int num_rel;
04bdff6a 11654 int odd;
0a1b45a2 11655 bool is_tga;
5d1634d7 11656
721956f4
AM
11657 /* Massage our args to the form they really have. */
11658 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11659 info = in_arg;
5d1634d7 11660
abf874aa
CL
11661 /* Fail if the target section could not be assigned to an output
11662 section. The user should fix his linker script. */
11663 if (stub_entry->target_section != NULL
11664 && stub_entry->target_section->output_section == NULL
11665 && info->non_contiguous_regions)
53215f21
CL
11666 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11667 "Retry without --enable-non-contiguous-regions.\n"),
11668 stub_entry->target_section);
abf874aa
CL
11669
11670 /* Same for the group. */
11671 if (stub_entry->group->stub_sec != NULL
11672 && stub_entry->group->stub_sec->output_section == NULL
11673 && info->non_contiguous_regions)
53215f21
CL
11674 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11675 "output section. Retry without "
11676 "--enable-non-contiguous-regions.\n"),
11677 stub_entry->group->stub_sec,
11678 stub_entry->target_section);
abf874aa 11679
5d1634d7 11680 htab = ppc_hash_table (info);
4dfe6ac6 11681 if (htab == NULL)
0a1b45a2 11682 return false;
5d1634d7 11683
1aa42141 11684 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11685 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11686
7aba54da
AM
11687 htab->stub_count[stub_entry->type.main - 1] += 1;
11688 if (stub_entry->type.main == ppc_stub_long_branch
11689 && stub_entry->type.sub == ppc_stub_toc)
5d1634d7 11690 {
721956f4 11691 /* Branches are relative. This is where we are going to. */
1aa42141 11692 targ = (stub_entry->target_value
6911b7dc
AM
11693 + stub_entry->target_section->output_offset
11694 + stub_entry->target_section->output_section->vma);
1aa42141 11695 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11696
721956f4 11697 /* And this is where we are coming from. */
1aa42141
AM
11698 off = (stub_entry->stub_offset
11699 + stub_entry->group->stub_sec->output_offset
11700 + stub_entry->group->stub_sec->output_section->vma);
11701 off = targ - off;
e86ce104 11702
9e390558 11703 p = loc;
29433886 11704 obfd = htab->params->stub_bfd;
7aba54da 11705 if (stub_entry->type.r2save)
ad8e1ba5 11706 {
25f53a85 11707 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11708
a7c49797 11709 if (r2off == (bfd_vma) -1)
aa374f67 11710 {
0a1b45a2
AM
11711 htab->stub_error = true;
11712 return false;
aa374f67 11713 }
29433886 11714 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11715 p += 4;
ac2df442
AM
11716 if (PPC_HA (r2off) != 0)
11717 {
29433886 11718 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11719 p += 4;
a7c49797
AM
11720 }
11721 if (PPC_LO (r2off) != 0)
11722 {
29433886 11723 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11724 p += 4;
ac2df442 11725 }
9e390558 11726 off -= p - loc;
ad8e1ba5 11727 }
29433886 11728 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11729 p += 4;
ad8e1ba5 11730
5c3dead3
AM
11731 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11732 {
cf97bcb0
AM
11733 _bfd_error_handler
11734 (_("long branch stub `%s' offset overflow"),
bc30df16 11735 stub_entry->root.string);
0a1b45a2
AM
11736 htab->stub_error = true;
11737 return false;
5c3dead3 11738 }
ee75fd95
AM
11739
11740 if (info->emitrelocations)
11741 {
6f20ed8a 11742 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42 11743 if (r == NULL)
0a1b45a2 11744 return false;
9e390558 11745 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11746 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11747 r->r_addend = targ;
3d58e1fc
AM
11748 if (stub_entry->h != NULL
11749 && !use_global_in_relocs (htab, stub_entry, r, 1))
0a1b45a2 11750 return false;
ee75fd95 11751 }
7aba54da
AM
11752 }
11753 else if (stub_entry->type.main == ppc_stub_plt_branch
11754 && stub_entry->type.sub == ppc_stub_toc)
11755 {
721956f4
AM
11756 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11757 stub_entry->root.string + 9,
0a1b45a2 11758 false, false);
721956f4
AM
11759 if (br_entry == NULL)
11760 {
cf97bcb0
AM
11761 _bfd_error_handler (_("can't find branch stub `%s'"),
11762 stub_entry->root.string);
0a1b45a2
AM
11763 htab->stub_error = true;
11764 return false;
721956f4
AM
11765 }
11766
1aa42141 11767 targ = (stub_entry->target_value
176a0d42
AM
11768 + stub_entry->target_section->output_offset
11769 + stub_entry->target_section->output_section->vma);
7aba54da 11770 if (!stub_entry->type.r2save)
1aa42141 11771 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11772
1aa42141 11773 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11774 htab->brlt->contents + br_entry->offset);
721956f4 11775
f94498ff 11776 if (br_entry->iter == htab->stub_iteration)
721956f4 11777 {
f94498ff 11778 br_entry->iter = 0;
84f5d08e 11779
1657026c 11780 if (htab->relbrlt != NULL && !info->enable_dt_relr)
84f5d08e 11781 {
f94498ff
AM
11782 /* Create a reloc for the branch lookup table entry. */
11783 Elf_Internal_Rela rela;
11784 bfd_byte *rl;
11785
11786 rela.r_offset = (br_entry->offset
11787 + htab->brlt->output_offset
11788 + htab->brlt->output_section->vma);
11789 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11790 rela.r_addend = targ;
f94498ff
AM
11791
11792 rl = htab->relbrlt->contents;
11793 rl += (htab->relbrlt->reloc_count++
11794 * sizeof (Elf64_External_Rela));
11795 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11796 }
11797 else if (info->emitrelocations)
11798 {
176a0d42
AM
11799 r = get_relocs (htab->brlt, 1);
11800 if (r == NULL)
0a1b45a2 11801 return false;
176a0d42
AM
11802 /* brlt, being SEC_LINKER_CREATED does not go through the
11803 normal reloc processing. Symbols and offsets are not
11804 translated from input file to output file form, so
11805 set up the offset per the output file. */
f94498ff
AM
11806 r->r_offset = (br_entry->offset
11807 + htab->brlt->output_offset
11808 + htab->brlt->output_section->vma);
11809 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11810 r->r_addend = targ;
84f5d08e 11811 }
84f5d08e 11812 }
721956f4 11813
1aa42141 11814 targ = (br_entry->offset
176a0d42
AM
11815 + htab->brlt->output_offset
11816 + htab->brlt->output_section->vma);
11817
1aa42141
AM
11818 off = (elf_gp (info->output_bfd)
11819 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11820 off = targ - off;
721956f4 11821
ad8e1ba5 11822 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11823 {
25f53a85 11824 info->callbacks->einfo
c1c8c1ef 11825 (_("%P: linkage table error against `%pT'\n"),
721956f4 11826 stub_entry->root.string);
5d1634d7 11827 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11828 htab->stub_error = true;
11829 return false;
5d1634d7 11830 }
41bd81ab 11831
176a0d42
AM
11832 if (info->emitrelocations)
11833 {
6f20ed8a 11834 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42 11835 if (r == NULL)
0a1b45a2 11836 return false;
6f20ed8a 11837 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11838 if (bfd_big_endian (info->output_bfd))
11839 r[0].r_offset += 2;
7aba54da 11840 if (stub_entry->type.r2save)
176a0d42
AM
11841 r[0].r_offset += 4;
11842 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11843 r[0].r_addend = targ;
176a0d42
AM
11844 if (PPC_HA (off) != 0)
11845 {
11846 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11847 r[1].r_offset = r[0].r_offset + 4;
11848 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11849 r[1].r_addend = r[0].r_addend;
11850 }
11851 }
11852
9e390558 11853 p = loc;
29433886 11854 obfd = htab->params->stub_bfd;
7aba54da 11855 if (!stub_entry->type.r2save)
ad8e1ba5 11856 {
176a0d42 11857 if (PPC_HA (off) != 0)
ac2df442 11858 {
29433886 11859 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11860 p += 4;
29433886 11861 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11862 }
11863 else
29433886 11864 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11865 }
11866 else
11867 {
25f53a85 11868 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11869
a7c49797 11870 if (r2off == (bfd_vma) -1)
aa374f67 11871 {
0a1b45a2
AM
11872 htab->stub_error = true;
11873 return false;
aa374f67 11874 }
ad8e1ba5 11875
29433886 11876 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11877 p += 4;
176a0d42 11878 if (PPC_HA (off) != 0)
ac2df442 11879 {
29433886 11880 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11881 p += 4;
29433886 11882 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11883 }
11884 else
29433886 11885 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11886
11887 if (PPC_HA (r2off) != 0)
11888 {
9e390558 11889 p += 4;
29433886 11890 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11891 }
11892 if (PPC_LO (r2off) != 0)
11893 {
9e390558 11894 p += 4;
29433886 11895 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11896 }
ad8e1ba5 11897 }
9e390558 11898 p += 4;
29433886 11899 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11900 p += 4;
29433886 11901 bfd_put_32 (obfd, BCTR, p);
407aa07c 11902 p += 4;
7aba54da
AM
11903 }
11904 else if (stub_entry->type.sub >= ppc_stub_notoc)
11905 {
11906 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
05d0e962 11907 p = loc;
f891966f 11908 off = (stub_entry->stub_offset
05d0e962
AM
11909 + stub_entry->group->stub_sec->output_offset
11910 + stub_entry->group->stub_sec->output_section->vma);
29433886 11911 obfd = htab->params->stub_bfd;
7aba54da 11912 is_tga = (is_plt
12cf8b93 11913 && stub_entry->h != NULL
29433886
AM
11914 && is_tls_get_addr (&stub_entry->h->elf, htab)
11915 && htab->params->tls_get_addr_opt);
11916 if (is_tga)
11917 {
11918 p = build_tls_get_addr_head (htab, stub_entry, p);
11919 off += p - loc;
11920 }
7aba54da 11921 if (stub_entry->type.r2save)
05d0e962
AM
11922 {
11923 off += 4;
29433886 11924 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11925 p += 4;
11926 }
7aba54da 11927 if (is_plt)
05d0e962
AM
11928 {
11929 targ = stub_entry->plt_ent->plt.offset & ~1;
11930 if (targ >= (bfd_vma) -2)
11931 abort ();
11932
11933 plt = htab->elf.splt;
30845f11 11934 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
11935 {
11936 if (stub_entry->symtype == STT_GNU_IFUNC)
11937 plt = htab->elf.iplt;
11938 else
11939 plt = htab->pltlocal;
11940 }
11941 targ += plt->output_offset + plt->output_section->vma;
11942 }
11943 else
11944 targ = (stub_entry->target_value
11945 + stub_entry->target_section->output_offset
11946 + stub_entry->target_section->output_section->vma);
04bdff6a 11947 odd = off & 4;
05d0e962 11948 off = targ - off;
f891966f 11949
3d58e1fc
AM
11950 relp = p;
11951 num_rel = 0;
7aba54da
AM
11952 if (stub_entry->type.sub == ppc_stub_notoc)
11953 p = build_power10_offset (obfd, p, off, odd, is_plt);
04bdff6a
AM
11954 else
11955 {
29433886
AM
11956 if (htab->glink_eh_frame != NULL
11957 && htab->glink_eh_frame->size != 0)
11958 {
11959 bfd_byte *base, *eh;
11960 unsigned int lr_used, delta;
11961
11962 base = (htab->glink_eh_frame->contents
11963 + stub_entry->group->eh_base + 17);
11964 eh = base + stub_entry->group->eh_size;
11965 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11966 delta = lr_used - stub_entry->group->lr_restore;
11967 stub_entry->group->lr_restore = lr_used + 8;
11968 eh = eh_advance (htab->elf.dynobj, eh, delta);
11969 *eh++ = DW_CFA_register;
11970 *eh++ = 65;
11971 *eh++ = 12;
11972 *eh++ = DW_CFA_advance_loc + 2;
11973 *eh++ = DW_CFA_restore_extended;
11974 *eh++ = 65;
11975 stub_entry->group->eh_size = eh - base;
11976 }
11977
04bdff6a
AM
11978 /* The notoc stubs calculate their target (either a PLT entry or
11979 the global entry point of a function) relative to the PC
11980 returned by the "bcl" two instructions past the start of the
11981 sequence emitted by build_offset. The offset is therefore 8
11982 less than calculated from the start of the sequence. */
11983 off -= 8;
7aba54da 11984 p = build_offset (obfd, p, off, is_plt);
04bdff6a
AM
11985 }
11986
7aba54da 11987 if (stub_entry->type.main == ppc_stub_long_branch)
05d0e962 11988 {
f891966f 11989 bfd_vma from;
3d58e1fc 11990 num_rel = 1;
f891966f
AM
11991 from = (stub_entry->stub_offset
11992 + stub_entry->group->stub_sec->output_offset
11993 + stub_entry->group->stub_sec->output_section->vma
11994 + (p - loc));
29433886 11995 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11996 }
11997 else
11998 {
29433886 11999 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 12000 p += 4;
29433886 12001 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
12002 }
12003 p += 4;
df136d64 12004
29433886
AM
12005 if (is_tga)
12006 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12007
3d58e1fc
AM
12008 if (info->emitrelocations)
12009 {
04bdff6a 12010 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
7aba54da 12011 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12012 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12013 else
12014 {
12015 num_rel += num_relocs_for_offset (off);
12016 roff += 16;
12017 }
3d58e1fc
AM
12018 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12019 if (r == NULL)
0a1b45a2 12020 return false;
7aba54da 12021 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12022 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
12023 else
12024 r = emit_relocs_for_offset (info, r, roff, targ, off);
7aba54da 12025 if (stub_entry->type.main == ppc_stub_long_branch)
3d58e1fc
AM
12026 {
12027 ++r;
12028 roff = p - 4 - stub_entry->group->stub_sec->contents;
12029 r->r_offset = roff;
12030 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12031 r->r_addend = targ;
12032 if (stub_entry->h != NULL
12033 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
0a1b45a2 12034 return false;
3d58e1fc
AM
12035 }
12036 }
7aba54da
AM
12037 }
12038 else if (stub_entry->type.main == ppc_stub_plt_call)
12039 {
e054468f 12040 if (stub_entry->h != NULL
b31867b6
AM
12041 && stub_entry->h->is_func_descriptor
12042 && stub_entry->h->oh != NULL)
c862ae31 12043 {
b31867b6
AM
12044 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12045
12046 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 12047 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
12048 if (fh->elf.root.type == bfd_link_hash_undefined
12049 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12050 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 12051 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
12052 }
12053
721956f4 12054 /* Now build the stub. */
1aa42141
AM
12055 targ = stub_entry->plt_ent->plt.offset & ~1;
12056 if (targ >= (bfd_vma) -2)
721956f4
AM
12057 abort ();
12058
33e44f2e 12059 plt = htab->elf.splt;
30845f11 12060 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
12061 {
12062 if (stub_entry->symtype == STT_GNU_IFUNC)
12063 plt = htab->elf.iplt;
12064 else
12065 plt = htab->pltlocal;
12066 }
1aa42141 12067 targ += plt->output_offset + plt->output_section->vma;
e054468f 12068
1aa42141
AM
12069 off = (elf_gp (info->output_bfd)
12070 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12071 off = targ - off;
721956f4 12072
ad8e1ba5 12073 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 12074 {
25f53a85 12075 info->callbacks->einfo
695344c0 12076 /* xgettext:c-format */
c1c8c1ef 12077 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
12078 stub_entry->h != NULL
12079 ? stub_entry->h->elf.root.root.string
12080 : "<local sym>");
721956f4 12081 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
12082 htab->stub_error = true;
12083 return false;
721956f4
AM
12084 }
12085
176a0d42
AM
12086 r = NULL;
12087 if (info->emitrelocations)
12088 {
6f20ed8a 12089 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
12090 ((PPC_HA (off) != 0)
12091 + (htab->opd_abi
e7d1c40c 12092 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
12093 && PPC_HA (off + 16) == PPC_HA (off))
12094 : 1)));
176a0d42 12095 if (r == NULL)
0a1b45a2 12096 return false;
6f20ed8a 12097 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
12098 if (bfd_big_endian (info->output_bfd))
12099 r[0].r_offset += 2;
1aa42141 12100 r[0].r_addend = targ;
176a0d42 12101 }
29433886
AM
12102 p = loc;
12103 obfd = htab->params->stub_bfd;
12104 is_tga = (stub_entry->h != NULL
12105 && is_tls_get_addr (&stub_entry->h->elf, htab)
12106 && htab->params->tls_get_addr_opt);
12107 if (is_tga)
12108 {
12109 p = build_tls_get_addr_head (htab, stub_entry, p);
12110 if (r != NULL)
12111 r[0].r_offset += p - loc;
12112 }
12113 p = build_plt_stub (htab, stub_entry, p, off, r);
12114 if (is_tga)
12115 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
7aba54da
AM
12116 }
12117 else if (stub_entry->type.main == ppc_stub_save_res)
12118 return true;
12119 else
12120 {
721956f4 12121 BFD_FAIL ();
0a1b45a2 12122 return false;
721956f4
AM
12123 }
12124
1aa42141 12125 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 12126
e7d1c40c 12127 if (htab->params->emit_stub_syms)
97b639ba
AM
12128 {
12129 struct elf_link_hash_entry *h;
ee75fd95
AM
12130 size_t len1, len2;
12131 char *name;
12132 const char *const stub_str[] = { "long_branch",
05d0e962 12133 "plt_branch",
ee75fd95
AM
12134 "plt_call" };
12135
7aba54da 12136 len1 = strlen (stub_str[stub_entry->type.main - 1]);
ee75fd95
AM
12137 len2 = strlen (stub_entry->root.string);
12138 name = bfd_malloc (len1 + len2 + 2);
12139 if (name == NULL)
0a1b45a2 12140 return false;
ee75fd95 12141 memcpy (name, stub_entry->root.string, 9);
7aba54da 12142 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
ee75fd95 12143 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
0a1b45a2 12144 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
97b639ba 12145 if (h == NULL)
0a1b45a2 12146 return false;
97b639ba
AM
12147 if (h->root.type == bfd_link_hash_new)
12148 {
12149 h->root.type = bfd_link_hash_defined;
6f20ed8a 12150 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 12151 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
12152 h->ref_regular = 1;
12153 h->def_regular = 1;
12154 h->ref_regular_nonweak = 1;
12155 h->forced_local = 1;
12156 h->non_elf = 0;
2ec55de3 12157 h->root.linker_def = 1;
97b639ba
AM
12158 }
12159 }
12160
0a1b45a2 12161 return true;
721956f4
AM
12162}
12163
12164/* As above, but don't actually build the stub. Just bump offset so
12165 we know stub section sizes, and select plt_branch stubs where
12166 long_branch stubs won't do. */
12167
0a1b45a2 12168static bool
4ce794b7 12169ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
12170{
12171 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 12172 struct bfd_link_info *info;
721956f4 12173 struct ppc_link_hash_table *htab;
f891966f
AM
12174 asection *plt;
12175 bfd_vma targ, off, r2off;
04bdff6a 12176 unsigned int size, extra, lr_used, delta, odd;
0441f94f 12177 bfd_vma stub_offset;
721956f4
AM
12178
12179 /* Massage our args to the form they really have. */
12180 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
12181 info = in_arg;
12182
12183 htab = ppc_hash_table (info);
4dfe6ac6 12184 if (htab == NULL)
0a1b45a2 12185 return false;
721956f4 12186
abf874aa
CL
12187 /* Fail if the target section could not be assigned to an output
12188 section. The user should fix his linker script. */
12189 if (stub_entry->target_section != NULL
12190 && stub_entry->target_section->output_section == NULL
12191 && info->non_contiguous_regions)
53215f21
CL
12192 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12193 "Retry without --enable-non-contiguous-regions.\n"),
12194 stub_entry->target_section);
abf874aa
CL
12195
12196 /* Same for the group. */
12197 if (stub_entry->group->stub_sec != NULL
12198 && stub_entry->group->stub_sec->output_section == NULL
12199 && info->non_contiguous_regions)
53215f21
CL
12200 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12201 "output section. Retry without "
12202 "--enable-non-contiguous-regions.\n"),
12203 stub_entry->group->stub_sec,
12204 stub_entry->target_section);
abf874aa 12205
1aa42141 12206 /* Make a note of the offset within the stubs for this entry. */
0441f94f 12207 stub_offset = stub_entry->group->stub_sec->size;
1aa42141 12208
a4b6fadd
AM
12209 if (stub_entry->h != NULL
12210 && stub_entry->h->save_res
12211 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12212 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12213 {
12214 /* Don't make stubs to out-of-line register save/restore
12215 functions. Instead, emit copies of the functions. */
12216 stub_entry->group->needs_save_res = 1;
7aba54da
AM
12217 stub_entry->type.main = ppc_stub_save_res;
12218 stub_entry->type.sub = ppc_stub_toc;
12219 stub_entry->type.r2save = 0;
0a1b45a2 12220 return true;
a4b6fadd
AM
12221 }
12222
7aba54da 12223 if (stub_entry->type.main == ppc_stub_plt_branch)
721956f4 12224 {
f891966f
AM
12225 /* Reset the stub type from the plt branch variant in case we now
12226 can reach with a shorter stub. */
7aba54da
AM
12227 stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
12228 }
12229
12230 if (stub_entry->type.main == ppc_stub_long_branch
12231 && stub_entry->type.sub == ppc_stub_toc)
12232 {
1aa42141
AM
12233 targ = (stub_entry->target_value
12234 + stub_entry->target_section->output_offset
12235 + stub_entry->target_section->output_section->vma);
f891966f 12236 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
0441f94f 12237 off = (stub_offset
1aa42141
AM
12238 + stub_entry->group->stub_sec->output_offset
12239 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12240
ad8e1ba5 12241 size = 4;
f891966f 12242 r2off = 0;
7aba54da 12243 if (stub_entry->type.r2save)
ad8e1ba5 12244 {
25f53a85 12245 r2off = get_r2off (info, stub_entry);
a7c49797 12246 if (r2off == (bfd_vma) -1)
aa374f67 12247 {
0a1b45a2
AM
12248 htab->stub_error = true;
12249 return false;
aa374f67 12250 }
a7c49797 12251 size = 8;
ac2df442 12252 if (PPC_HA (r2off) != 0)
a7c49797
AM
12253 size += 4;
12254 if (PPC_LO (r2off) != 0)
12255 size += 4;
1aa42141 12256 off += size - 4;
ad8e1ba5 12257 }
1aa42141 12258 off = targ - off;
ad8e1ba5 12259
f891966f
AM
12260 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12261 Do the same for -R objects without function descriptors. */
7aba54da 12262 if ((stub_entry->type.r2save
f891966f
AM
12263 && r2off == 0
12264 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12265 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12266 {
f891966f 12267 struct ppc_branch_hash_entry *br_entry;
df136d64 12268
f891966f
AM
12269 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12270 stub_entry->root.string + 9,
0a1b45a2 12271 true, false);
f891966f 12272 if (br_entry == NULL)
721956f4 12273 {
f891966f
AM
12274 _bfd_error_handler (_("can't build branch stub `%s'"),
12275 stub_entry->root.string);
0a1b45a2
AM
12276 htab->stub_error = true;
12277 return false;
721956f4
AM
12278 }
12279
f891966f 12280 if (br_entry->iter != htab->stub_iteration)
721956f4 12281 {
f891966f
AM
12282 br_entry->iter = htab->stub_iteration;
12283 br_entry->offset = htab->brlt->size;
12284 htab->brlt->size += 8;
63bc6f6c 12285
1657026c 12286 if (htab->relbrlt != NULL && !info->enable_dt_relr)
f891966f
AM
12287 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12288 else if (info->emitrelocations)
84f5d08e 12289 {
f891966f
AM
12290 htab->brlt->reloc_count += 1;
12291 htab->brlt->flags |= SEC_RELOC;
05d0e962 12292 }
f891966f 12293 }
ac2df442 12294
f891966f
AM
12295 targ = (br_entry->offset
12296 + htab->brlt->output_offset
12297 + htab->brlt->output_section->vma);
12298 off = (elf_gp (info->output_bfd)
12299 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12300 off = targ - off;
176a0d42 12301
f891966f
AM
12302 if (info->emitrelocations)
12303 {
12304 stub_entry->group->stub_sec->reloc_count
12305 += 1 + (PPC_HA (off) != 0);
12306 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12307 }
05d0e962 12308
7aba54da
AM
12309 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12310 if (!stub_entry->type.r2save)
f891966f
AM
12311 {
12312 size = 12;
12313 if (PPC_HA (off) != 0)
12314 size = 16;
ac2df442 12315 }
f891966f 12316 else
ac2df442 12317 {
f891966f
AM
12318 size = 16;
12319 if (PPC_HA (off) != 0)
12320 size += 4;
12321
12322 if (PPC_HA (r2off) != 0)
12323 size += 4;
12324 if (PPC_LO (r2off) != 0)
12325 size += 4;
ac2df442 12326 }
721956f4 12327 }
f891966f
AM
12328 else if (info->emitrelocations)
12329 {
12330 stub_entry->group->stub_sec->reloc_count += 1;
12331 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12332 }
7aba54da
AM
12333 }
12334 else if (stub_entry->type.main == ppc_stub_long_branch)
12335 {
0441f94f 12336 off = (stub_offset
f891966f
AM
12337 + stub_entry->group->stub_sec->output_offset
12338 + stub_entry->group->stub_sec->output_section->vma);
12339 size = 0;
7aba54da 12340 if (stub_entry->type.r2save)
f891966f
AM
12341 size = 4;
12342 off += size;
12343 targ = (stub_entry->target_value
12344 + stub_entry->target_section->output_offset
12345 + stub_entry->target_section->output_section->vma);
04bdff6a 12346 odd = off & 4;
f891966f
AM
12347 off = targ - off;
12348
3d58e1fc
AM
12349 if (info->emitrelocations)
12350 {
04bdff6a 12351 unsigned int num_rel;
7aba54da 12352 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12353 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12354 else
12355 num_rel = num_relocs_for_offset (off - 8);
12356 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12357 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12358 }
12359
7aba54da 12360 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12361 extra = size_power10_offset (off, odd);
04bdff6a
AM
12362 else
12363 extra = size_offset (off - 8);
f891966f
AM
12364 /* Include branch insn plus those in the offset sequence. */
12365 size += 4 + extra;
12366 /* The branch insn is at the end, or "extra" bytes along. So
12367 its offset will be "extra" bytes less that that already
12368 calculated. */
12369 off -= extra;
12370
7aba54da 12371 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12372 {
12373 /* After the bcl, lr has been modified so we need to emit
12374 .eh_frame info saying the return address is in r12. */
0441f94f 12375 lr_used = stub_offset + 8;
7aba54da 12376 if (stub_entry->type.r2save)
04bdff6a
AM
12377 lr_used += 4;
12378 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12379 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12380 DW_CFA_restore_extended 65. */
12381 delta = lr_used - stub_entry->group->lr_restore;
12382 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12383 stub_entry->group->lr_restore = lr_used + 8;
12384 }
f891966f
AM
12385
12386 /* If the branch can't reach, use a plt_branch. */
12387 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12388 {
7aba54da 12389 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
f891966f
AM
12390 size += 4;
12391 }
3d58e1fc
AM
12392 else if (info->emitrelocations)
12393 stub_entry->group->stub_sec->reloc_count +=1;
7aba54da
AM
12394 }
12395 else if (stub_entry->type.sub >= ppc_stub_notoc)
12396 {
12397 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
29433886 12398 lr_used = 0;
12cf8b93
AM
12399 if (stub_entry->h != NULL
12400 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12401 && htab->params->tls_get_addr_opt)
12402 {
12403 lr_used += 7 * 4;
12404 if (!htab->params->no_tls_get_addr_regsave)
12405 lr_used += 11 * 4;
7aba54da 12406 else if (stub_entry->type.r2save)
29433886
AM
12407 lr_used += 2 * 4;
12408 }
7aba54da 12409 if (stub_entry->type.r2save)
29433886 12410 lr_used += 4;
f891966f
AM
12411 targ = stub_entry->plt_ent->plt.offset & ~1;
12412 if (targ >= (bfd_vma) -2)
12413 abort ();
12414
12415 plt = htab->elf.splt;
30845f11 12416 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12417 {
12418 if (stub_entry->symtype == STT_GNU_IFUNC)
12419 plt = htab->elf.iplt;
12420 else
12421 plt = htab->pltlocal;
12422 }
12423 targ += plt->output_offset + plt->output_section->vma;
0441f94f 12424 off = (stub_offset
29433886
AM
12425 + stub_entry->group->stub_sec->output_offset
12426 + stub_entry->group->stub_sec->output_section->vma
12427 + lr_used);
04bdff6a 12428 odd = off & 4;
f891966f
AM
12429 off = targ - off;
12430
12431 if (htab->params->plt_stub_align != 0)
12432 {
29433886 12433 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
f891966f
AM
12434
12435 stub_entry->group->stub_sec->size += pad;
0441f94f 12436 stub_offset = stub_entry->group->stub_sec->size;
f891966f 12437 off -= pad;
29433886 12438 odd ^= pad & 4;
f891966f
AM
12439 }
12440
3d58e1fc
AM
12441 if (info->emitrelocations)
12442 {
04bdff6a 12443 unsigned int num_rel;
7aba54da 12444 if (stub_entry->type.sub == ppc_stub_notoc)
7c1f4227 12445 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12446 else
12447 num_rel = num_relocs_for_offset (off - 8);
12448 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12449 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12450 }
12451
29433886 12452 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12453
7aba54da 12454 if (stub_entry->type.sub != ppc_stub_notoc)
04bdff6a
AM
12455 {
12456 /* After the bcl, lr has been modified so we need to emit
12457 .eh_frame info saying the return address is in r12. */
0441f94f 12458 lr_used += stub_offset + 8;
04bdff6a
AM
12459 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12460 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12461 DW_CFA_restore_extended 65. */
12462 delta = lr_used - stub_entry->group->lr_restore;
12463 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12464 stub_entry->group->lr_restore = lr_used + 8;
12465 }
7aba54da 12466 if (stub_entry->h != NULL
29433886
AM
12467 && is_tls_get_addr (&stub_entry->h->elf, htab)
12468 && htab->params->tls_get_addr_opt)
12469 {
12470 if (!htab->params->no_tls_get_addr_regsave)
12471 {
0441f94f 12472 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12473 delta = cfa_updt - stub_entry->group->lr_restore;
12474 stub_entry->group->eh_size += eh_advance_size (delta);
12475 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
0441f94f 12476 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886 12477 }
7aba54da 12478 else if (stub_entry->type.r2save)
29433886 12479 {
0441f94f 12480 lr_used = stub_offset + size - 20;
29433886
AM
12481 delta = lr_used - stub_entry->group->lr_restore;
12482 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
0441f94f 12483 stub_entry->group->lr_restore = stub_offset + size - 4;
29433886
AM
12484 }
12485 }
7aba54da
AM
12486 }
12487 else if (stub_entry->type.main == ppc_stub_plt_call)
12488 {
f891966f
AM
12489 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12490 if (targ >= (bfd_vma) -2)
12491 abort ();
12492 plt = htab->elf.splt;
30845f11 12493 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12494 {
12495 if (stub_entry->symtype == STT_GNU_IFUNC)
12496 plt = htab->elf.iplt;
12497 else
12498 plt = htab->pltlocal;
12499 }
12500 targ += plt->output_offset + plt->output_section->vma;
12501
12502 off = (elf_gp (info->output_bfd)
12503 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12504 off = targ - off;
12505
12506 if (htab->params->plt_stub_align != 0)
12507 {
29433886 12508 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
f891966f
AM
12509
12510 stub_entry->group->stub_sec->size += pad;
0441f94f 12511 stub_offset = stub_entry->group->stub_sec->size;
f891966f
AM
12512 }
12513
12514 if (info->emitrelocations)
12515 {
12516 stub_entry->group->stub_sec->reloc_count
12517 += ((PPC_HA (off) != 0)
12518 + (htab->opd_abi
12519 ? 2 + (htab->params->plt_static_chain
12520 && PPC_HA (off + 16) == PPC_HA (off))
12521 : 1));
12522 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12523 }
12524
29433886 12525 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12526
12527 if (stub_entry->h != NULL
ed7007c1 12528 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f 12529 && htab->params->tls_get_addr_opt
7aba54da 12530 && stub_entry->type.r2save)
f891966f 12531 {
29433886
AM
12532 if (!htab->params->no_tls_get_addr_regsave)
12533 {
12534 /* Adjustments to r1 need to be described. */
0441f94f 12535 unsigned int cfa_updt = stub_offset + 18 * 4;
29433886
AM
12536 delta = cfa_updt - stub_entry->group->lr_restore;
12537 stub_entry->group->eh_size += eh_advance_size (delta);
12538 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12539 }
12540 else
9e7028aa 12541 {
0441f94f 12542 lr_used = stub_offset + size - 20;
9e7028aa
AM
12543 /* The eh_frame info will consist of a DW_CFA_advance_loc
12544 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12545 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12546 delta = lr_used - stub_entry->group->lr_restore;
12547 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12548 }
0441f94f 12549 stub_entry->group->lr_restore = stub_offset + size - 4;
f891966f 12550 }
7aba54da
AM
12551 }
12552 else
12553 {
f891966f 12554 BFD_FAIL ();
0a1b45a2 12555 return false;
721956f4
AM
12556 }
12557
0441f94f
AM
12558 if (stub_entry->stub_offset != stub_offset)
12559 htab->stub_changed = true;
12560 if (htab->stub_iteration <= STUB_SHRINK_ITER
12561 || stub_entry->stub_offset < stub_offset)
12562 stub_entry->stub_offset = stub_offset;
12563 stub_entry->group->stub_sec->size = stub_entry->stub_offset + size;
0a1b45a2 12564 return true;
721956f4
AM
12565}
12566
12567/* Set up various things so that we can make a list of input sections
12568 for each output section included in the link. Returns -1 on error,
cedb70c5 12569 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12570
12571int
e7d1c40c 12572ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12573{
6f20ed8a 12574 unsigned int id;
986f0783 12575 size_t amt;
721956f4
AM
12576 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12577
4dfe6ac6
NC
12578 if (htab == NULL)
12579 return -1;
4c52953f 12580
7cf7fcc8 12581 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12582 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12583 htab->sec_info = bfd_zmalloc (amt);
12584 if (htab->sec_info == NULL)
721956f4
AM
12585 return -1;
12586
3d6f9012
AM
12587 /* Set toc_off for com, und, abs and ind sections. */
12588 for (id = 0; id < 3; id++)
6f20ed8a 12589 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12590
721956f4
AM
12591 return 1;
12592}
12593
927be08e
AM
12594/* Set up for first pass at multitoc partitioning. */
12595
12596void
12597ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12598{
12599 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12600
1c865ab2 12601 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12602 htab->toc_bfd = NULL;
12603 htab->toc_first_sec = NULL;
12604}
12605
e717da7e
AM
12606/* The linker repeatedly calls this function for each TOC input section
12607 and linker generated GOT section. Group input bfds such that the toc
927be08e 12608 within a group is less than 64k in size. */
ad8e1ba5 12609
0a1b45a2 12610bool
4ce794b7 12611ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12612{
12613 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12614 bfd_vma addr, off, limit;
ad8e1ba5 12615
4dfe6ac6 12616 if (htab == NULL)
0a1b45a2 12617 return false;
4dfe6ac6 12618
927be08e 12619 if (!htab->second_toc_pass)
4c52953f 12620 {
927be08e 12621 /* Keep track of the first .toc or .got section for this input bfd. */
0a1b45a2 12622 bool new_bfd = htab->toc_bfd != isec->owner;
a4fd3de5
AM
12623
12624 if (new_bfd)
bf102f86
AM
12625 {
12626 htab->toc_bfd = isec->owner;
12627 htab->toc_first_sec = isec;
12628 }
927be08e 12629
bf102f86
AM
12630 addr = isec->output_offset + isec->output_section->vma;
12631 off = addr - htab->toc_curr;
d77c8a4b
AM
12632 limit = 0x80008000;
12633 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12634 limit = 0x10000;
12635 if (off + isec->size > limit)
bf102f86
AM
12636 {
12637 addr = (htab->toc_first_sec->output_offset
12638 + htab->toc_first_sec->output_section->vma);
12639 htab->toc_curr = addr;
a27e685f 12640 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12641 }
99877b66 12642
927be08e
AM
12643 /* toc_curr is the base address of this toc group. Set elf_gp
12644 for the input section to be the offset relative to the
12645 output toc base plus 0x8000. Making the input elf_gp an
12646 offset allows us to move the toc as a whole without
12647 recalculating input elf_gp. */
06bcf541 12648 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12649 off += TOC_BASE_OFF;
12650
12651 /* Die if someone uses a linker script that doesn't keep input
12652 file .toc and .got together. */
a4fd3de5
AM
12653 if (new_bfd
12654 && elf_gp (isec->owner) != 0
927be08e 12655 && elf_gp (isec->owner) != off)
0a1b45a2 12656 return false;
927be08e
AM
12657
12658 elf_gp (isec->owner) = off;
0a1b45a2 12659 return true;
4c52953f 12660 }
927be08e
AM
12661
12662 /* During the second pass toc_first_sec points to the start of
12663 a toc group, and toc_curr is used to track the old elf_gp.
12664 We use toc_bfd to ensure we only look at each bfd once. */
12665 if (htab->toc_bfd == isec->owner)
0a1b45a2 12666 return true;
927be08e
AM
12667 htab->toc_bfd = isec->owner;
12668
12669 if (htab->toc_first_sec == NULL
12670 || htab->toc_curr != elf_gp (isec->owner))
12671 {
12672 htab->toc_curr = elf_gp (isec->owner);
12673 htab->toc_first_sec = isec;
12674 }
12675 addr = (htab->toc_first_sec->output_offset
12676 + htab->toc_first_sec->output_section->vma);
06bcf541 12677 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12678 elf_gp (isec->owner) = off;
12679
0a1b45a2 12680 return true;
ad8e1ba5
AM
12681}
12682
927be08e
AM
12683/* Called via elf_link_hash_traverse to merge GOT entries for global
12684 symbol H. */
12685
0a1b45a2 12686static bool
927be08e
AM
12687merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12688{
12689 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12690 return true;
927be08e 12691
927be08e
AM
12692 merge_got_entries (&h->got.glist);
12693
0a1b45a2 12694 return true;
927be08e
AM
12695}
12696
12697/* Called via elf_link_hash_traverse to allocate GOT entries for global
12698 symbol H. */
12699
0a1b45a2 12700static bool
927be08e
AM
12701reallocate_got (struct elf_link_hash_entry *h, void *inf)
12702{
12703 struct got_entry *gent;
12704
12705 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12706 return true;
927be08e 12707
927be08e
AM
12708 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12709 if (!gent->is_indirect)
12710 allocate_got (h, (struct bfd_link_info *) inf, gent);
0a1b45a2 12711 return true;
927be08e
AM
12712}
12713
12714/* Called on the first multitoc pass after the last call to
12715 ppc64_elf_next_toc_section. This function removes duplicate GOT
12716 entries. */
12717
0a1b45a2 12718bool
927be08e 12719ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12720{
12721 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e 12722 struct bfd *ibfd, *ibfd2;
0a1b45a2 12723 bool done_something;
927be08e
AM
12724
12725 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12726
7865406b 12727 if (!htab->do_multi_toc)
0a1b45a2 12728 return false;
7865406b 12729
d0fae19d 12730 /* Merge global sym got entries within a toc group. */
927be08e
AM
12731 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12732
12733 /* And tlsld_got. */
c72f2fb2 12734 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12735 {
12736 struct got_entry *ent, *ent2;
12737
12738 if (!is_ppc64_elf (ibfd))
12739 continue;
12740
12741 ent = ppc64_tlsld_got (ibfd);
12742 if (!ent->is_indirect
12743 && ent->got.offset != (bfd_vma) -1)
12744 {
c72f2fb2 12745 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12746 {
12747 if (!is_ppc64_elf (ibfd2))
12748 continue;
12749
12750 ent2 = ppc64_tlsld_got (ibfd2);
12751 if (!ent2->is_indirect
12752 && ent2->got.offset != (bfd_vma) -1
12753 && elf_gp (ibfd2) == elf_gp (ibfd))
12754 {
0a1b45a2 12755 ent2->is_indirect = true;
927be08e
AM
12756 ent2->got.ent = ent;
12757 }
12758 }
12759 }
12760 }
12761
12762 /* Zap sizes of got sections. */
33e44f2e
AM
12763 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12764 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12765 htab->got_reli_size = 0;
12766
c72f2fb2 12767 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12768 {
12769 asection *got, *relgot;
12770
12771 if (!is_ppc64_elf (ibfd))
12772 continue;
12773
12774 got = ppc64_elf_tdata (ibfd)->got;
12775 if (got != NULL)
12776 {
12777 got->rawsize = got->size;
12778 got->size = 0;
12779 relgot = ppc64_elf_tdata (ibfd)->relgot;
12780 relgot->rawsize = relgot->size;
12781 relgot->size = 0;
12782 }
12783 }
12784
12785 /* Now reallocate the got, local syms first. We don't need to
12786 allocate section contents again since we never increase size. */
c72f2fb2 12787 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12788 {
12789 struct got_entry **lgot_ents;
12790 struct got_entry **end_lgot_ents;
12791 struct plt_entry **local_plt;
12792 struct plt_entry **end_local_plt;
f961d9dd 12793 unsigned char *lgot_masks;
927be08e
AM
12794 bfd_size_type locsymcount;
12795 Elf_Internal_Shdr *symtab_hdr;
19e08130 12796 asection *s;
927be08e
AM
12797
12798 if (!is_ppc64_elf (ibfd))
12799 continue;
12800
12801 lgot_ents = elf_local_got_ents (ibfd);
12802 if (!lgot_ents)
12803 continue;
12804
12805 symtab_hdr = &elf_symtab_hdr (ibfd);
12806 locsymcount = symtab_hdr->sh_info;
12807 end_lgot_ents = lgot_ents + locsymcount;
12808 local_plt = (struct plt_entry **) end_lgot_ents;
12809 end_local_plt = local_plt + locsymcount;
f961d9dd 12810 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12811 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12812 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12813 {
12814 struct got_entry *ent;
12815
12816 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12817 {
19e08130
AM
12818 unsigned int ent_size = 8;
12819 unsigned int rel_size = sizeof (Elf64_External_Rela);
12820
d0fae19d
AM
12821 ent->got.offset = s->size;
12822 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12823 {
19e08130
AM
12824 ent_size *= 2;
12825 rel_size *= 2;
12826 }
12827 s->size += ent_size;
37da22e5 12828 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12829 {
33e44f2e 12830 htab->elf.irelplt->size += rel_size;
19e08130
AM
12831 htab->got_reli_size += rel_size;
12832 }
6a3858a6 12833 else if (bfd_link_pic (info)
8f22c953
AM
12834 && (ent->tls_type == 0
12835 ? !info->enable_dt_relr
12836 : !bfd_link_executable (info)))
19e08130
AM
12837 {
12838 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12839 srel->size += rel_size;
d0fae19d
AM
12840 }
12841 }
927be08e
AM
12842 }
12843 }
12844
12845 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12846
c72f2fb2 12847 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12848 {
12849 struct got_entry *ent;
12850
12851 if (!is_ppc64_elf (ibfd))
12852 continue;
12853
12854 ent = ppc64_tlsld_got (ibfd);
12855 if (!ent->is_indirect
12856 && ent->got.offset != (bfd_vma) -1)
12857 {
12858 asection *s = ppc64_elf_tdata (ibfd)->got;
12859 ent->got.offset = s->size;
12860 s->size += 16;
f749f26e 12861 if (bfd_link_dll (info))
927be08e
AM
12862 {
12863 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12864 srel->size += sizeof (Elf64_External_Rela);
12865 }
12866 }
12867 }
12868
33e44f2e 12869 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12870 if (!done_something)
c72f2fb2 12871 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12872 {
12873 asection *got;
12874
12875 if (!is_ppc64_elf (ibfd))
12876 continue;
12877
12878 got = ppc64_elf_tdata (ibfd)->got;
12879 if (got != NULL)
12880 {
12881 done_something = got->rawsize != got->size;
12882 if (done_something)
12883 break;
12884 }
12885 }
12886
12887 if (done_something)
e7d1c40c 12888 (*htab->params->layout_sections_again) ();
927be08e
AM
12889
12890 /* Set up for second pass over toc sections to recalculate elf_gp
12891 on input sections. */
12892 htab->toc_bfd = NULL;
12893 htab->toc_first_sec = NULL;
0a1b45a2 12894 htab->second_toc_pass = true;
927be08e
AM
12895 return done_something;
12896}
12897
12898/* Called after second pass of multitoc partitioning. */
12899
12900void
12901ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12902{
12903 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12904
12905 /* After the second pass, toc_curr tracks the TOC offset used
12906 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12907 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12908}
12909
9b5ecbd0
AM
12910/* No toc references were found in ISEC. If the code in ISEC makes no
12911 calls, then there's no need to use toc adjusting stubs when branching
12912 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12913 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12914 needed, and 2 if a cyclical call-graph was found but no other reason
12915 for a stub was detected. If called from the top level, a return of
12916 2 means the same as a return of 0. */
9b5ecbd0
AM
12917
12918static int
4ce794b7 12919toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12920{
9b5ecbd0 12921 int ret;
70cc837d
AM
12922
12923 /* Mark this section as checked. */
12924 isec->call_check_done = 1;
9b5ecbd0 12925
772119ce
AM
12926 /* We know none of our code bearing sections will need toc stubs. */
12927 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12928 return 0;
12929
eea6121a 12930 if (isec->size == 0)
082c50f8
AM
12931 return 0;
12932
4c52953f
AM
12933 if (isec->output_section == NULL)
12934 return 0;
12935
4c52953f 12936 ret = 0;
70cc837d 12937 if (isec->reloc_count != 0)
9b5ecbd0 12938 {
70cc837d
AM
12939 Elf_Internal_Rela *relstart, *rel;
12940 Elf_Internal_Sym *local_syms;
12941 struct ppc_link_hash_table *htab;
2917689a 12942
70cc837d
AM
12943 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12944 info->keep_memory);
12945 if (relstart == NULL)
12946 return -1;
90aecf7a 12947
70cc837d
AM
12948 /* Look for branches to outside of this section. */
12949 local_syms = NULL;
12950 htab = ppc_hash_table (info);
12951 if (htab == NULL)
12952 return -1;
4c52953f 12953
70cc837d 12954 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12955 {
70cc837d
AM
12956 enum elf_ppc64_reloc_type r_type;
12957 unsigned long r_symndx;
12958 struct elf_link_hash_entry *h;
12959 struct ppc_link_hash_entry *eh;
12960 Elf_Internal_Sym *sym;
12961 asection *sym_sec;
12962 struct _opd_sec_data *opd;
12963 bfd_vma sym_value;
12964 bfd_vma dest;
12965
12966 r_type = ELF64_R_TYPE (rel->r_info);
12967 if (r_type != R_PPC64_REL24
05d0e962 12968 && r_type != R_PPC64_REL24_NOTOC
7aba54da 12969 && r_type != R_PPC64_REL24_P9NOTOC
70cc837d
AM
12970 && r_type != R_PPC64_REL14
12971 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12972 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12973 && r_type != R_PPC64_PLTCALL
12974 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12975 continue;
4c52953f 12976
70cc837d
AM
12977 r_symndx = ELF64_R_SYM (rel->r_info);
12978 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12979 isec->owner))
4c52953f 12980 {
70cc837d
AM
12981 ret = -1;
12982 break;
12983 }
4c52953f 12984
70cc837d
AM
12985 /* Calls to dynamic lib functions go through a plt call stub
12986 that uses r2. */
ed7007c1 12987 eh = ppc_elf_hash_entry (h);
70cc837d
AM
12988 if (eh != NULL
12989 && (eh->elf.plt.plist != NULL
12990 || (eh->oh != NULL
12991 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12992 {
12993 ret = 1;
12994 break;
4c52953f
AM
12995 }
12996
70cc837d
AM
12997 if (sym_sec == NULL)
12998 /* Ignore other undefined symbols. */
4c52953f 12999 continue;
4c52953f 13000
70cc837d
AM
13001 /* Assume branches to other sections not included in the
13002 link need stubs too, to cover -R and absolute syms. */
13003 if (sym_sec->output_section == NULL)
13004 {
13005 ret = 1;
13006 break;
13007 }
4c52953f 13008
70cc837d
AM
13009 if (h == NULL)
13010 sym_value = sym->st_value;
13011 else
13012 {
13013 if (h->root.type != bfd_link_hash_defined
13014 && h->root.type != bfd_link_hash_defweak)
13015 abort ();
13016 sym_value = h->root.u.def.value;
13017 }
13018 sym_value += rel->r_addend;
4c52953f 13019
70cc837d
AM
13020 /* If this branch reloc uses an opd sym, find the code section. */
13021 opd = get_opd_info (sym_sec);
13022 if (opd != NULL)
13023 {
13024 if (h == NULL && opd->adjust != NULL)
13025 {
13026 long adjust;
4c52953f 13027
92a9c616 13028 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
13029 if (adjust == -1)
13030 /* Assume deleted functions won't ever be called. */
13031 continue;
13032 sym_value += adjust;
13033 }
4c52953f 13034
aef36ac1 13035 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13036 &sym_sec, NULL, false);
70cc837d
AM
13037 if (dest == (bfd_vma) -1)
13038 continue;
13039 }
13040 else
13041 dest = (sym_value
13042 + sym_sec->output_offset
13043 + sym_sec->output_section->vma);
4c52953f 13044
70cc837d
AM
13045 /* Ignore branch to self. */
13046 if (sym_sec == isec)
13047 continue;
4c52953f 13048
70cc837d
AM
13049 /* If the called function uses the toc, we need a stub. */
13050 if (sym_sec->has_toc_reloc
13051 || sym_sec->makes_toc_func_call)
4c52953f 13052 {
70cc837d 13053 ret = 1;
4c52953f
AM
13054 break;
13055 }
70cc837d
AM
13056
13057 /* Assume any branch that needs a long branch stub might in fact
13058 need a plt_branch stub. A plt_branch stub uses r2. */
13059 else if (dest - (isec->output_offset
13060 + isec->output_section->vma
6911b7dc
AM
13061 + rel->r_offset) + (1 << 25)
13062 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13063 ? h->other
13064 : sym->st_other))
4c52953f 13065 {
70cc837d
AM
13066 ret = 1;
13067 break;
13068 }
13069
13070 /* If calling back to a section in the process of being
13071 tested, we can't say for sure that no toc adjusting stubs
13072 are needed, so don't return zero. */
13073 else if (sym_sec->call_check_in_progress)
13074 ret = 2;
13075
13076 /* Branches to another section that itself doesn't have any TOC
13077 references are OK. Recursively call ourselves to check. */
13078 else if (!sym_sec->call_check_done)
13079 {
13080 int recur;
13081
13082 /* Mark current section as indeterminate, so that other
13083 sections that call back to current won't be marked as
13084 known. */
13085 isec->call_check_in_progress = 1;
13086 recur = toc_adjusting_stub_needed (info, sym_sec);
13087 isec->call_check_in_progress = 0;
13088
4c52953f
AM
13089 if (recur != 0)
13090 {
70cc837d
AM
13091 ret = recur;
13092 if (recur != 2)
13093 break;
4c52953f
AM
13094 }
13095 }
4c52953f 13096 }
70cc837d 13097
c9594989
AM
13098 if (elf_symtab_hdr (isec->owner).contents
13099 != (unsigned char *) local_syms)
70cc837d
AM
13100 free (local_syms);
13101 if (elf_section_data (isec)->relocs != relstart)
13102 free (relstart);
9b5ecbd0
AM
13103 }
13104
70cc837d
AM
13105 if ((ret & 1) == 0
13106 && isec->map_head.s != NULL
13107 && (strcmp (isec->output_section->name, ".init") == 0
13108 || strcmp (isec->output_section->name, ".fini") == 0))
13109 {
13110 if (isec->map_head.s->has_toc_reloc
13111 || isec->map_head.s->makes_toc_func_call)
13112 ret = 1;
13113 else if (!isec->map_head.s->call_check_done)
13114 {
13115 int recur;
13116 isec->call_check_in_progress = 1;
13117 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13118 isec->call_check_in_progress = 0;
13119 if (recur != 0)
13120 ret = recur;
13121 }
13122 }
13123
13124 if (ret == 1)
13125 isec->makes_toc_func_call = 1;
4c52953f 13126
9b5ecbd0
AM
13127 return ret;
13128}
13129
721956f4
AM
13130/* The linker repeatedly calls this function for each input section,
13131 in the order that input sections are linked into output sections.
13132 Build lists of input sections to determine groupings between which
13133 we may insert linker stubs. */
13134
0a1b45a2 13135bool
4ce794b7 13136ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
13137{
13138 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13139
4dfe6ac6 13140 if (htab == NULL)
0a1b45a2 13141 return false;
4dfe6ac6 13142
734b6cf9 13143 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 13144 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 13145 {
3d6f9012
AM
13146 /* This happens to make the list in reverse order,
13147 which is what we want. */
6f20ed8a
AM
13148 htab->sec_info[isec->id].u.list
13149 = htab->sec_info[isec->output_section->id].u.list;
13150 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 13151 }
ad8e1ba5 13152
4c52953f 13153 if (htab->multi_toc_needed)
9b5ecbd0 13154 {
8b974ba3
AM
13155 /* Analyse sections that aren't already flagged as needing a
13156 valid toc pointer. Exclude .fixup for the linux kernel.
13157 .fixup contains branches, but only back to the function that
13158 hit an exception. */
13159 if (!(isec->has_toc_reloc
13160 || (isec->flags & SEC_CODE) == 0
13161 || strcmp (isec->name, ".fixup") == 0
13162 || isec->call_check_done))
13163 {
13164 if (toc_adjusting_stub_needed (info, isec) < 0)
0a1b45a2 13165 return false;
8b974ba3
AM
13166 }
13167 /* Make all sections use the TOC assigned for this object file.
13168 This will be wrong for pasted sections; We fix that in
13169 check_pasted_section(). */
13170 if (elf_gp (isec->owner) != 0)
13171 htab->toc_curr = elf_gp (isec->owner);
13172 }
13173
6f20ed8a 13174 htab->sec_info[isec->id].toc_off = htab->toc_curr;
0a1b45a2 13175 return true;
721956f4
AM
13176}
13177
70cc837d
AM
13178/* Check that all .init and .fini sections use the same toc, if they
13179 have toc relocs. */
13180
0a1b45a2 13181static bool
70cc837d
AM
13182check_pasted_section (struct bfd_link_info *info, const char *name)
13183{
13184 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13185
13186 if (o != NULL)
13187 {
13188 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13189 bfd_vma toc_off = 0;
13190 asection *i;
13191
13192 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13193 if (i->has_toc_reloc)
13194 {
13195 if (toc_off == 0)
6f20ed8a
AM
13196 toc_off = htab->sec_info[i->id].toc_off;
13197 else if (toc_off != htab->sec_info[i->id].toc_off)
0a1b45a2 13198 return false;
70cc837d 13199 }
6683a28d
AM
13200
13201 if (toc_off == 0)
13202 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13203 if (i->makes_toc_func_call)
13204 {
6f20ed8a 13205 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13206 break;
13207 }
13208
70cc837d
AM
13209 /* Make sure the whole pasted function uses the same toc offset. */
13210 if (toc_off != 0)
13211 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13212 htab->sec_info[i->id].toc_off = toc_off;
70cc837d 13213 }
0a1b45a2 13214 return true;
70cc837d
AM
13215}
13216
0a1b45a2 13217bool
70cc837d
AM
13218ppc64_elf_check_init_fini (struct bfd_link_info *info)
13219{
0c80c644
SM
13220 bool ret1 = check_pasted_section (info, ".init");
13221 bool ret2 = check_pasted_section (info, ".fini");
13222
13223 return ret1 && ret2;
70cc837d
AM
13224}
13225
721956f4
AM
13226/* See whether we can group stub sections together. Grouping stub
13227 sections may result in fewer stubs. More importantly, we need to
13228 put all .init* and .fini* stubs at the beginning of the .init or
13229 .fini output sections respectively, because glibc splits the
13230 _init and _fini functions into multiple parts. Putting a stub in
13231 the middle of a function is not a good idea. */
13232
0a1b45a2 13233static bool
6f20ed8a 13234group_sections (struct bfd_link_info *info,
4ce794b7 13235 bfd_size_type stub_group_size,
0a1b45a2 13236 bool stubs_always_before_branch)
721956f4 13237{
6f20ed8a
AM
13238 struct ppc_link_hash_table *htab;
13239 asection *osec;
0a1b45a2 13240 bool suppress_size_errors;
7c8fe5c4 13241
6f20ed8a
AM
13242 htab = ppc_hash_table (info);
13243 if (htab == NULL)
0a1b45a2 13244 return false;
6f20ed8a 13245
0a1b45a2 13246 suppress_size_errors = false;
7c8fe5c4
AM
13247 if (stub_group_size == 1)
13248 {
13249 /* Default values. */
13250 if (stubs_always_before_branch)
09f92717 13251 stub_group_size = 0x1e00000;
7c8fe5c4 13252 else
09f92717 13253 stub_group_size = 0x1c00000;
0a1b45a2 13254 suppress_size_errors = true;
7c8fe5c4
AM
13255 }
13256
6f20ed8a 13257 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13258 {
6f20ed8a
AM
13259 asection *tail;
13260
13261 if (osec->id >= htab->sec_info_arr_size)
13262 continue;
13263
13264 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13265 while (tail != NULL)
721956f4 13266 {
734b6cf9
AM
13267 asection *curr;
13268 asection *prev;
13269 bfd_size_type total;
0a1b45a2 13270 bool big_sec;
734b6cf9 13271 bfd_vma curr_toc;
6f20ed8a 13272 struct map_stub *group;
09f92717 13273 bfd_size_type group_size;
734b6cf9
AM
13274
13275 curr = tail;
eea6121a 13276 total = tail->size;
09f92717
AM
13277 group_size = (ppc64_elf_section_data (tail) != NULL
13278 && ppc64_elf_section_data (tail)->has_14bit_branch
13279 ? stub_group_size >> 10 : stub_group_size);
13280
13281 big_sec = total > group_size;
7c8fe5c4 13282 if (big_sec && !suppress_size_errors)
695344c0 13283 /* xgettext:c-format */
871b3ab2 13284 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13285 tail->owner, tail);
6f20ed8a 13286 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13287
6f20ed8a 13288 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13289 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13290 < (ppc64_elf_section_data (prev) != NULL
13291 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13292 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13293 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13294 curr = prev;
13295
13296 /* OK, the size from the start of CURR to the end is less
09f92717 13297 than group_size and thus can be handled by one stub
734b6cf9 13298 section. (or the tail section is itself larger than
09f92717
AM
13299 group_size, in which case we may be toast.) We should
13300 really be keeping track of the total size of stubs added
13301 here, as stubs contribute to the final output section
13302 size. That's a little tricky, and this way will only
13303 break if stubs added make the total size more than 2^25,
13304 ie. for the default stub_group_size, if stubs total more
13305 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13306 group = bfd_alloc (curr->owner, sizeof (*group));
13307 if (group == NULL)
0a1b45a2 13308 return false;
6f20ed8a
AM
13309 group->link_sec = curr;
13310 group->stub_sec = NULL;
a4b6fadd 13311 group->needs_save_res = 0;
df136d64
AM
13312 group->lr_restore = 0;
13313 group->eh_size = 0;
13314 group->eh_base = 0;
a4b6fadd
AM
13315 group->next = htab->group;
13316 htab->group = group;
734b6cf9 13317 do
721956f4 13318 {
6f20ed8a 13319 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13320 /* Set up this stub group. */
6f20ed8a 13321 htab->sec_info[tail->id].u.group = group;
721956f4 13322 }
734b6cf9
AM
13323 while (tail != curr && (tail = prev) != NULL);
13324
09f92717 13325 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13326 bytes before the stub section can be handled by it too.
13327 Don't do this if we have a really large section after the
13328 stubs, as adding more stubs increases the chance that
13329 branches may not reach into the stub section. */
13330 if (!stubs_always_before_branch && !big_sec)
13331 {
13332 total = 0;
13333 while (prev != NULL
13334 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13335 < (ppc64_elf_section_data (prev) != NULL
13336 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13337 ? (group_size = stub_group_size >> 10)
13338 : group_size))
6f20ed8a 13339 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13340 {
13341 tail = prev;
6f20ed8a
AM
13342 prev = htab->sec_info[tail->id].u.list;
13343 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13344 }
13345 }
13346 tail = prev;
721956f4
AM
13347 }
13348 }
0a1b45a2 13349 return true;
721956f4
AM
13350}
13351
58d180e8
AM
13352static const unsigned char glink_eh_frame_cie[] =
13353{
13354 0, 0, 0, 16, /* length. */
13355 0, 0, 0, 0, /* id. */
13356 1, /* CIE version. */
13357 'z', 'R', 0, /* Augmentation string. */
13358 4, /* Code alignment. */
13359 0x78, /* Data alignment. */
13360 65, /* RA reg. */
13361 1, /* Augmentation size. */
13362 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13363 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13364};
13365
d969d15f
AM
13366/* Stripping output sections is normally done before dynamic section
13367 symbols have been allocated. This function is called later, and
13368 handles cases like htab->brlt which is mapped to its own output
13369 section. */
13370
13371static void
13372maybe_strip_output (struct bfd_link_info *info, asection *isec)
13373{
13374 if (isec->size == 0
13375 && isec->output_section->size == 0
53d8967a 13376 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13377 && !bfd_section_removed_from_list (info->output_bfd,
13378 isec->output_section)
13379 && elf_section_data (isec->output_section)->dynindx == 0)
13380 {
13381 isec->output_section->flags |= SEC_EXCLUDE;
13382 bfd_section_list_remove (info->output_bfd, isec->output_section);
13383 info->output_bfd->section_count--;
13384 }
13385}
13386
1657026c
AM
13387static int
13388compare_relr_address (const void *arg1, const void *arg2)
13389{
13390 bfd_vma a = *(bfd_vma *) arg1;
13391 bfd_vma b = *(bfd_vma *) arg2;
13392 return a < b ? -1 : a > b ? 1 : 0;
13393}
13394
13395static bool
13396append_relr_off (struct ppc_link_hash_table *htab, bfd_vma off)
13397{
13398 if (htab->relr_count >= htab->relr_alloc)
13399 {
13400 if (htab->relr_alloc == 0)
13401 htab->relr_alloc = 4096;
13402 else
13403 htab->relr_alloc *= 2;
13404 htab->relr_addr
13405 = bfd_realloc (htab->relr_addr,
13406 htab->relr_alloc * sizeof (htab->relr_addr[0]));
13407 if (htab->relr_addr == NULL)
13408 return false;
13409 }
13410 htab->relr_addr[htab->relr_count++] = off;
13411 return true;
13412}
13413
13414static bool
13415got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13416{
13417 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13418 bfd *ibfd;
13419
13420 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13421 {
13422 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13423 struct plt_entry **local_plt, **lplt, **end_local_plt;
13424 Elf_Internal_Shdr *symtab_hdr;
13425 bfd_size_type locsymcount;
13426 Elf_Internal_Sym *local_syms = NULL;
13427 struct plt_entry *pent;
13428 struct got_entry *gent;
13429
13430 if (!is_ppc64_elf (ibfd))
13431 continue;
13432
13433 lgot_ents = elf_local_got_ents (ibfd);
13434 if (!lgot_ents)
13435 continue;
13436
13437 symtab_hdr = &elf_symtab_hdr (ibfd);
13438 locsymcount = symtab_hdr->sh_info;
13439 end_lgot_ents = lgot_ents + locsymcount;
13440 local_plt = (struct plt_entry **) end_lgot_ents;
13441 end_local_plt = local_plt + locsymcount;
13442 for (lgot = lgot_ents; lgot < end_lgot_ents; ++lgot)
13443 for (gent = *lgot; gent != NULL; gent = gent->next)
13444 if (!gent->is_indirect
13445 && gent->tls_type == 0
13446 && gent->got.offset != (bfd_vma) -1)
13447 {
13448 asection *got = ppc64_elf_tdata (gent->owner)->got;
13449 bfd_vma r_offset = (got->output_section->vma
13450 + got->output_offset
13451 + gent->got.offset);
13452 if (!append_relr_off (htab, r_offset))
13453 {
13454 htab->stub_error = true;
13455 return false;
13456 }
13457 }
13458
13459 if (!htab->opd_abi)
13460 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13461 for (pent = *lplt; pent != NULL; pent = pent->next)
13462 if (pent->plt.offset != (bfd_vma) -1)
13463 {
13464 Elf_Internal_Sym *sym;
13465
13466 if (!get_sym_h (NULL, &sym, NULL, NULL, &local_syms,
13467 lplt - local_plt, ibfd))
13468 {
13469 err_exit:
13470 if (symtab_hdr->contents != (unsigned char *) local_syms)
13471 free (local_syms);
13472 return false;
13473 }
13474
13475 if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
13476 {
13477 bfd_vma r_offset = (pent->plt.offset
13478 + htab->pltlocal->output_offset
13479 + htab->pltlocal->output_section->vma);
13480 if (!append_relr_off (htab, r_offset))
13481 goto err_exit;
13482 }
13483 }
13484
13485 if (local_syms != NULL
13486 && symtab_hdr->contents != (unsigned char *) local_syms)
13487 {
13488 if (!info->keep_memory)
13489 free (local_syms);
13490 else
13491 symtab_hdr->contents = (unsigned char *) local_syms;
13492 }
13493 }
13494 return true;
13495}
13496
13497static bool
13498got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13499{
13500 struct bfd_link_info *info;
13501 struct ppc_link_hash_table *htab;
13502 struct plt_entry *pent;
13503 struct got_entry *gent;
13504
13505 if (h->root.type == bfd_link_hash_indirect)
13506 return true;
13507
13508 info = (struct bfd_link_info *) inf;
13509 htab = ppc_hash_table (info);
13510 if (htab == NULL)
13511 return false;
13512
13513 if (h->type != STT_GNU_IFUNC
13514 && h->def_regular
13515 && (h->root.type == bfd_link_hash_defined
13516 || h->root.type == bfd_link_hash_defweak))
13517 {
13518 if (!htab->elf.dynamic_sections_created
13519 || h->dynindx == -1
13520 || SYMBOL_REFERENCES_LOCAL (info, h))
13521 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13522 if (!gent->is_indirect
13523 && gent->tls_type == 0
13524 && gent->got.offset != (bfd_vma) -1)
13525 {
13526 asection *got = ppc64_elf_tdata (gent->owner)->got;
13527 bfd_vma r_offset = (got->output_section->vma
13528 + got->output_offset
13529 + gent->got.offset);
13530 if (!append_relr_off (htab, r_offset))
13531 {
13532 htab->stub_error = true;
13533 return false;
13534 }
13535 }
13536
13537 if (!htab->opd_abi
13538 && use_local_plt (info, h))
13539 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13540 if (pent->plt.offset != (bfd_vma) -1)
13541 {
13542 bfd_vma r_offset = (htab->pltlocal->output_section->vma
13543 + htab->pltlocal->output_offset
13544 + pent->plt.offset);
13545 if (!append_relr_off (htab, r_offset))
13546 {
13547 htab->stub_error = true;
13548 return false;
13549 }
13550 }
13551 }
13552 return true;
13553}
13554
721956f4
AM
13555/* Determine and set the size of the stub section for a final link.
13556
13557 The basic idea here is to examine all the relocations looking for
13558 PC-relative calls to a target that is unreachable with a "bl"
13559 instruction. */
13560
0a1b45a2 13561bool
e7d1c40c 13562ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13563{
13564 bfd_size_type stub_group_size;
0a1b45a2 13565 bool stubs_always_before_branch;
721956f4
AM
13566 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13567
4dfe6ac6 13568 if (htab == NULL)
0a1b45a2 13569 return false;
4dfe6ac6 13570
e10a07b3
AM
13571 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13572 htab->params->power10_stubs = 0;
13573
0e1862bb 13574 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13575 htab->params->plt_thread_safe = 1;
b9e5796b 13576 if (!htab->opd_abi)
e7d1c40c
AM
13577 htab->params->plt_thread_safe = 0;
13578 else if (htab->params->plt_thread_safe == -1)
794e51c0 13579 {
e2458743 13580 static const char *const thread_starter[] =
794e51c0
AM
13581 {
13582 "pthread_create",
13583 /* libstdc++ */
13584 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13585 /* librt */
13586 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13587 "mq_notify", "create_timer",
13588 /* libanl */
13589 "getaddrinfo_a",
13590 /* libgomp */
2300b5a1 13591 "GOMP_parallel",
794e51c0 13592 "GOMP_parallel_start",
2300b5a1 13593 "GOMP_parallel_loop_static",
794e51c0 13594 "GOMP_parallel_loop_static_start",
2300b5a1 13595 "GOMP_parallel_loop_dynamic",
794e51c0 13596 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13597 "GOMP_parallel_loop_guided",
794e51c0 13598 "GOMP_parallel_loop_guided_start",
2300b5a1 13599 "GOMP_parallel_loop_runtime",
794e51c0 13600 "GOMP_parallel_loop_runtime_start",
2300b5a1 13601 "GOMP_parallel_sections",
68ffbac6 13602 "GOMP_parallel_sections_start",
f9dffbf0
AM
13603 /* libgo */
13604 "__go_go",
794e51c0
AM
13605 };
13606 unsigned i;
13607
a4b6fadd 13608 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13609 {
13610 struct elf_link_hash_entry *h;
13611 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
0a1b45a2 13612 false, false, true);
e7d1c40c
AM
13613 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13614 if (htab->params->plt_thread_safe)
794e51c0
AM
13615 break;
13616 }
13617 }
e7d1c40c
AM
13618 stubs_always_before_branch = htab->params->group_size < 0;
13619 if (htab->params->group_size < 0)
13620 stub_group_size = -htab->params->group_size;
721956f4 13621 else
e7d1c40c 13622 stub_group_size = htab->params->group_size;
721956f4 13623
6f20ed8a 13624 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
0a1b45a2 13625 return false;
721956f4 13626
a804e476
AM
13627 htab->tga_group = NULL;
13628 if (!htab->params->no_tls_get_addr_regsave
13629 && htab->tga_desc_fd != NULL
13630 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13631 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13632 && htab->tls_get_addr_fd != NULL
13633 && is_static_defined (&htab->tls_get_addr_fd->elf))
13634 {
13635 asection *sym_sec, *code_sec, *stub_sec;
13636 bfd_vma sym_value;
13637 struct _opd_sec_data *opd;
13638
13639 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13640 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13641 code_sec = sym_sec;
13642 opd = get_opd_info (sym_sec);
13643 if (opd != NULL)
0a1b45a2 13644 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
a804e476
AM
13645 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13646 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13647 htab->tga_group->link_sec);
13648 if (stub_sec == NULL)
0a1b45a2 13649 return false;
a804e476
AM
13650 htab->tga_group->stub_sec = stub_sec;
13651
13652 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13653 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13654 htab->tga_desc_fd->elf.root.u.def.value = 0;
13655 htab->tga_desc_fd->elf.type = STT_FUNC;
13656 htab->tga_desc_fd->elf.def_regular = 1;
13657 htab->tga_desc_fd->elf.non_elf = 0;
0a1b45a2 13658 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
a804e476
AM
13659 }
13660
c9301e31 13661 /* Loop until no stubs added. After iteration 20 of this loop we may
0441f94f 13662 exit on a stub section shrinking. */
c9301e31 13663
721956f4
AM
13664 while (1)
13665 {
13666 bfd *input_bfd;
13667 unsigned int bfd_indx;
a4b6fadd 13668 struct map_stub *group;
721956f4
AM
13669
13670 htab->stub_iteration += 1;
1657026c 13671 htab->relr_count = 0;
721956f4
AM
13672
13673 for (input_bfd = info->input_bfds, bfd_indx = 0;
13674 input_bfd != NULL;
c72f2fb2 13675 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13676 {
13677 Elf_Internal_Shdr *symtab_hdr;
13678 asection *section;
6cdc0ccc 13679 Elf_Internal_Sym *local_syms = NULL;
721956f4 13680
0c8d6e5c 13681 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13682 continue;
13683
721956f4 13684 /* We'll need the symbol table in a second. */
0ffa91dd 13685 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13686 if (symtab_hdr->sh_info == 0)
13687 continue;
13688
721956f4
AM
13689 /* Walk over each section attached to the input bfd. */
13690 for (section = input_bfd->sections;
13691 section != NULL;
13692 section = section->next)
13693 {
721956f4 13694 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1657026c 13695 bool is_opd;
721956f4
AM
13696
13697 /* If there aren't any relocs, then there's nothing more
13698 to do. */
13699 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13700 || (section->flags & SEC_ALLOC) == 0
13701 || (section->flags & SEC_LOAD) == 0
721956f4
AM
13702 || section->reloc_count == 0)
13703 continue;
13704
1657026c
AM
13705 if (!info->enable_dt_relr
13706 && (section->flags & SEC_CODE) == 0)
13707 continue;
13708
721956f4
AM
13709 /* If this section is a link-once section that will be
13710 discarded, then don't create any stubs. */
13711 if (section->output_section == NULL
927be08e 13712 || section->output_section->owner != info->output_bfd)
721956f4
AM
13713 continue;
13714
1e2f5b6e
AM
13715 /* Get the relocs. */
13716 internal_relocs
4ce794b7 13717 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13718 info->keep_memory);
721956f4 13719 if (internal_relocs == NULL)
1e2f5b6e 13720 goto error_ret_free_local;
721956f4 13721
1657026c
AM
13722 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13723
721956f4
AM
13724 /* Now examine each relocation. */
13725 irela = internal_relocs;
13726 irelaend = irela + section->reloc_count;
13727 for (; irela < irelaend; irela++)
13728 {
4ce794b7
AM
13729 enum elf_ppc64_reloc_type r_type;
13730 unsigned int r_indx;
7aba54da 13731 struct ppc_stub_type stub_type;
721956f4 13732 struct ppc_stub_hash_entry *stub_entry;
8387904d 13733 asection *sym_sec, *code_sec;
e054468f 13734 bfd_vma sym_value, code_value;
721956f4 13735 bfd_vma destination;
6911b7dc 13736 unsigned long local_off;
0a1b45a2 13737 bool ok_dest;
721956f4 13738 struct ppc_link_hash_entry *hash;
8387904d 13739 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13740 struct elf_link_hash_entry *h;
13741 Elf_Internal_Sym *sym;
721956f4
AM
13742 char *stub_name;
13743 const asection *id_sec;
74f0fb50 13744 struct _opd_sec_data *opd;
e054468f 13745 struct plt_entry *plt_ent;
721956f4
AM
13746
13747 r_type = ELF64_R_TYPE (irela->r_info);
13748 r_indx = ELF64_R_SYM (irela->r_info);
13749
4ce794b7 13750 if (r_type >= R_PPC64_max)
721956f4
AM
13751 {
13752 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13753 goto error_ret_free_internal;
721956f4
AM
13754 }
13755
13756 /* Only look for stubs on branch instructions. */
1657026c
AM
13757 switch (r_type)
13758 {
13759 default:
13760 continue;
13761
13762 case R_PPC64_REL24:
13763 case R_PPC64_REL24_NOTOC:
13764 case R_PPC64_REL24_P9NOTOC:
13765 case R_PPC64_REL14:
13766 case R_PPC64_REL14_BRTAKEN:
13767 case R_PPC64_REL14_BRNTAKEN:
13768 if ((section->flags & SEC_CODE) != 0)
13769 break;
13770 continue;
13771
13772 case R_PPC64_ADDR64:
13773 case R_PPC64_TOC:
13774 if (info->enable_dt_relr
13775 && irela->r_offset % 2 == 0
13776 && section->alignment_power != 0)
13777 break;
13778 continue;
13779 }
721956f4
AM
13780
13781 /* Now determine the call target, its name, value,
13782 section. */
411e1bfb
AM
13783 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13784 r_indx, input_bfd))
13785 goto error_ret_free_internal;
411e1bfb 13786
1657026c
AM
13787 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13788 {
13789 /* Only locally defined symbols can possibly use
13790 relative relocations. */
13791 bfd_vma r_offset;
13792 if ((sym_sec == NULL
13793 || sym_sec->output_section == NULL)
13794 /* No symbol is OK too. */
13795 && !(sym != NULL && sym->st_shndx == 0)
13796 /* Hack for __ehdr_start, which is undefined
13797 at this point. */
13798 && !(h != NULL && h->root.linker_def))
13799 continue;
13800 if (NO_OPD_RELOCS && is_opd)
13801 continue;
13802 if (!is_opd
13803 && r_type == R_PPC64_ADDR64)
13804 {
13805 if (h != NULL
13806 ? h->type == STT_GNU_IFUNC
13807 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13808 continue;
13809 if (h != NULL
13810 && !SYMBOL_REFERENCES_LOCAL (info, h))
13811 continue;
13812 }
13813 r_offset = _bfd_elf_section_offset (info->output_bfd,
13814 info,
13815 section,
13816 irela->r_offset);
13817 if (r_offset >= (bfd_vma) -2)
13818 continue;
13819 r_offset += (section->output_section->vma
13820 + section->output_offset);
13821 if (!append_relr_off (htab, r_offset))
13822 goto error_ret_free_internal;
13823 continue;
13824 }
13825
13826 hash = ppc_elf_hash_entry (h);
0a1b45a2 13827 ok_dest = false;
8387904d 13828 fdh = NULL;
7fe2b9a6 13829 sym_value = 0;
411e1bfb 13830 if (hash == NULL)
721956f4 13831 {
411e1bfb 13832 sym_value = sym->st_value;
c27b8c2a
AM
13833 if (sym_sec != NULL
13834 && sym_sec->output_section != NULL)
0a1b45a2 13835 ok_dest = true;
721956f4 13836 }
7fe2b9a6
AM
13837 else if (hash->elf.root.type == bfd_link_hash_defined
13838 || hash->elf.root.type == bfd_link_hash_defweak)
13839 {
13840 sym_value = hash->elf.root.u.def.value;
13841 if (sym_sec->output_section != NULL)
0a1b45a2 13842 ok_dest = true;
7fe2b9a6
AM
13843 }
13844 else if (hash->elf.root.type == bfd_link_hash_undefweak
13845 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13846 {
99877b66 13847 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13848 use the func descriptor sym instead if it is
13849 defined. */
ceb1f1ef 13850 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13851 && hash->oh != NULL)
8387904d 13852 {
8c5b4e52 13853 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13854 if (fdh->elf.root.type == bfd_link_hash_defined
13855 || fdh->elf.root.type == bfd_link_hash_defweak)
13856 {
13857 sym_sec = fdh->elf.root.u.def.section;
13858 sym_value = fdh->elf.root.u.def.value;
13859 if (sym_sec->output_section != NULL)
0a1b45a2 13860 ok_dest = true;
8387904d 13861 }
99877b66
AM
13862 else
13863 fdh = NULL;
8387904d 13864 }
7fe2b9a6
AM
13865 }
13866 else
13867 {
13868 bfd_set_error (bfd_error_bad_value);
13869 goto error_ret_free_internal;
721956f4
AM
13870 }
13871
8843416a 13872 destination = 0;
6911b7dc 13873 local_off = 0;
8843416a
AM
13874 if (ok_dest)
13875 {
13876 sym_value += irela->r_addend;
13877 destination = (sym_value
13878 + sym_sec->output_offset
13879 + sym_sec->output_section->vma);
6911b7dc
AM
13880 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13881 ? hash->elf.other
13882 : sym->st_other);
8843416a
AM
13883 }
13884
8387904d 13885 code_sec = sym_sec;
e054468f 13886 code_value = sym_value;
74f0fb50
AM
13887 opd = get_opd_info (sym_sec);
13888 if (opd != NULL)
8387904d
AM
13889 {
13890 bfd_vma dest;
13891
74f0fb50 13892 if (hash == NULL && opd->adjust != NULL)
8387904d 13893 {
51aecdc5 13894 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13895 if (adjust == -1)
13896 continue;
e054468f 13897 code_value += adjust;
8387904d
AM
13898 sym_value += adjust;
13899 }
13900 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13901 &code_sec, &code_value, false);
8387904d
AM
13902 if (dest != (bfd_vma) -1)
13903 {
13904 destination = dest;
13905 if (fdh != NULL)
13906 {
13907 /* Fixup old ABI sym to point at code
13908 entry. */
99877b66 13909 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13910 hash->elf.root.u.def.section = code_sec;
e054468f 13911 hash->elf.root.u.def.value = code_value;
8387904d
AM
13912 }
13913 }
13914 }
13915
721956f4 13916 /* Determine what (if any) linker stub is needed. */
e054468f 13917 plt_ent = NULL;
7aba54da
AM
13918 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13919 &plt_ent, destination,
13920 local_off);
13921 stub_type.sub = ppc_stub_toc;
13922 stub_type.r2save = 0;
13923
13924 if (r_type == R_PPC64_REL24_NOTOC
13925 || r_type == R_PPC64_REL24_P9NOTOC)
05d0e962 13926 {
7aba54da
AM
13927 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13928 if (htab->params->power10_stubs == 0
13929 || (r_type == R_PPC64_REL24_P9NOTOC
13930 && htab->params->power10_stubs != 1))
13931 notoc = ppc_stub_p9notoc;
13932 if (stub_type.main == ppc_stub_plt_call)
13933 stub_type.sub = notoc;
13934 else if (stub_type.main == ppc_stub_long_branch
05d0e962
AM
13935 || (code_sec != NULL
13936 && code_sec->output_section != NULL
13937 && (((hash ? hash->elf.other : sym->st_other)
13938 & STO_PPC64_LOCAL_MASK)
4a4e7361 13939 > 1 << STO_PPC64_LOCAL_BIT)))
7aba54da
AM
13940 {
13941 stub_type.main = ppc_stub_long_branch;
13942 stub_type.sub = notoc;
13943 stub_type.r2save = 0;
13944 }
05d0e962 13945 }
7aba54da 13946 else if (stub_type.main != ppc_stub_plt_call)
ad8e1ba5
AM
13947 {
13948 /* Check whether we need a TOC adjusting stub.
13949 Since the linker pastes together pieces from
13950 different object files when creating the
13951 _init and _fini functions, it may be that a
13952 call to what looks like a local sym is in
13953 fact a call needing a TOC adjustment. */
33cb30a1
AM
13954 if ((code_sec != NULL
13955 && code_sec->output_section != NULL
33cb30a1 13956 && (code_sec->has_toc_reloc
918dc783
AM
13957 || code_sec->makes_toc_func_call)
13958 && (htab->sec_info[code_sec->id].toc_off
13959 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13960 || (((hash ? hash->elf.other : sym->st_other)
13961 & STO_PPC64_LOCAL_MASK)
13962 == 1 << STO_PPC64_LOCAL_BIT))
7aba54da
AM
13963 {
13964 stub_type.main = ppc_stub_long_branch;
13965 stub_type.sub = ppc_stub_toc;
13966 stub_type.r2save = 1;
13967 }
ad8e1ba5
AM
13968 }
13969
7aba54da 13970 if (stub_type.main == ppc_stub_none)
721956f4
AM
13971 continue;
13972
411e1bfb 13973 /* __tls_get_addr calls might be eliminated. */
7aba54da 13974 if (stub_type.main != ppc_stub_plt_call
411e1bfb 13975 && hash != NULL
ed7007c1 13976 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
13977 && section->has_tls_reloc
13978 && irela != internal_relocs)
13979 {
13980 /* Get tls info. */
f961d9dd 13981 unsigned char *tls_mask;
411e1bfb 13982
3a71aa26 13983 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13984 irela - 1, input_bfd))
13985 goto error_ret_free_internal;
abc489c6
AM
13986 if ((*tls_mask & TLS_TLS) != 0
13987 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
13988 continue;
13989 }
13990
7aba54da
AM
13991 if (stub_type.main == ppc_stub_plt_call
13992 && stub_type.sub == ppc_stub_toc)
794e51c0 13993 {
6e1816be
AM
13994 if (!htab->opd_abi
13995 && htab->params->plt_localentry0 != 0
13996 && is_elfv2_localentry0 (&hash->elf))
13997 htab->has_plt_localentry0 = 1;
13998 else if (irela + 1 < irelaend
13999 && irela[1].r_offset == irela->r_offset + 4
14000 && (ELF64_R_TYPE (irela[1].r_info)
14001 == R_PPC64_TOCSAVE))
f378ab09
AM
14002 {
14003 if (!tocsave_find (htab, INSERT,
14004 &local_syms, irela + 1, input_bfd))
14005 goto error_ret_free_internal;
14006 }
f378ab09 14007 else
7aba54da 14008 stub_type.r2save = 1;
794e51c0 14009 }
3b421ab3 14010
721956f4 14011 /* Support for grouping stub sections. */
6f20ed8a 14012 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
14013
14014 /* Get the name of this stub. */
14015 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14016 if (!stub_name)
14017 goto error_ret_free_internal;
14018
14019 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 14020 stub_name, false, false);
721956f4
AM
14021 if (stub_entry != NULL)
14022 {
721956f4 14023 free (stub_name);
7aba54da 14024 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
e10a07b3 14025 {
7aba54da
AM
14026 /* xgettext:c-format */
14027 _bfd_error_handler
14028 (_("%pB: cannot create stub entry %s"),
14029 section->owner, stub_entry->root.string);
14030 goto error_ret_free_internal;
e10a07b3 14031 }
721956f4
AM
14032 continue;
14033 }
14034
25f53a85 14035 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
14036 if (stub_entry == NULL)
14037 {
14038 free (stub_name);
6cdc0ccc
AM
14039 error_ret_free_internal:
14040 if (elf_section_data (section)->relocs == NULL)
14041 free (internal_relocs);
14042 error_ret_free_local:
c9594989
AM
14043 if (symtab_hdr->contents
14044 != (unsigned char *) local_syms)
6cdc0ccc 14045 free (local_syms);
0a1b45a2 14046 return false;
721956f4
AM
14047 }
14048
7aba54da
AM
14049 stub_entry->type = stub_type;
14050 if (stub_type.main == ppc_stub_plt_call)
e054468f 14051 {
05d0e962
AM
14052 stub_entry->target_value = sym_value;
14053 stub_entry->target_section = sym_sec;
e054468f
AM
14054 }
14055 else
14056 {
05d0e962
AM
14057 stub_entry->target_value = code_value;
14058 stub_entry->target_section = code_sec;
e054468f 14059 }
721956f4 14060 stub_entry->h = hash;
e054468f 14061 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
14062 stub_entry->symtype
14063 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 14064 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 14065
3d58e1fc
AM
14066 if (hash != NULL
14067 && (hash->elf.root.type == bfd_link_hash_defined
14068 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 14069 htab->stub_globals += 1;
721956f4
AM
14070 }
14071
14072 /* We're done with the internal relocs, free them. */
6cdc0ccc 14073 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 14074 free (internal_relocs);
721956f4 14075 }
6cdc0ccc
AM
14076
14077 if (local_syms != NULL
14078 && symtab_hdr->contents != (unsigned char *) local_syms)
14079 {
14080 if (!info->keep_memory)
14081 free (local_syms);
14082 else
14083 symtab_hdr->contents = (unsigned char *) local_syms;
14084 }
721956f4
AM
14085 }
14086
5c3dead3 14087 /* We may have added some stubs. Find out the new size of the
721956f4 14088 stub sections. */
d4aaa2a0 14089 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14090 {
14091 group->lr_restore = 0;
14092 group->eh_size = 0;
14093 if (group->stub_sec != NULL)
14094 {
14095 asection *stub_sec = group->stub_sec;
14096
14097 if (htab->stub_iteration <= STUB_SHRINK_ITER
14098 || stub_sec->rawsize < stub_sec->size)
14099 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
14100 stub_sec->rawsize = stub_sec->size;
14101 stub_sec->size = 0;
14102 stub_sec->reloc_count = 0;
14103 stub_sec->flags &= ~SEC_RELOC;
14104 }
14105 }
a804e476
AM
14106 if (htab->tga_group != NULL)
14107 {
14108 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14109 htab->tga_group->eh_size
14110 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14111 htab->tga_group->lr_restore = 23 * 4;
14112 htab->tga_group->stub_sec->size = 24 * 4;
14113 }
eea6121a 14114
ba21f564
AM
14115 if (htab->stub_iteration <= STUB_SHRINK_ITER
14116 || htab->brlt->rawsize < htab->brlt->size)
14117 htab->brlt->rawsize = htab->brlt->size;
eea6121a 14118 htab->brlt->size = 0;
84f5d08e
AM
14119 htab->brlt->reloc_count = 0;
14120 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 14121 if (htab->relbrlt != NULL)
eea6121a 14122 htab->relbrlt->size = 0;
721956f4 14123
1657026c
AM
14124 if (htab->elf.srelrdyn != NULL)
14125 {
14126 if (htab->stub_iteration <= STUB_SHRINK_ITER
14127 || htab->elf.srelrdyn->rawsize < htab->elf.srelrdyn->size)
14128 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14129 htab->elf.srelrdyn->size = 0;
14130 }
14131
0441f94f 14132 htab->stub_changed = false;
63bc6f6c 14133 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 14134
a4b6fadd
AM
14135 for (group = htab->group; group != NULL; group = group->next)
14136 if (group->needs_save_res)
14137 group->stub_sec->size += htab->sfpr->size;
14138
176a0d42
AM
14139 if (info->emitrelocations
14140 && htab->glink != NULL && htab->glink->size != 0)
14141 {
14142 htab->glink->reloc_count = 1;
14143 htab->glink->flags |= SEC_RELOC;
14144 }
14145
58d180e8
AM
14146 if (htab->glink_eh_frame != NULL
14147 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 14148 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 14149 {
2e0ce1c8 14150 size_t size = 0, align = 4;
58d180e8 14151
d4aaa2a0 14152 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14153 if (group->eh_size != 0)
14154 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 14155 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 14156 size += (24 + align - 1) & -align;
58d180e8 14157 if (size != 0)
2e0ce1c8
AM
14158 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14159 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14160 size = (size + align - 1) & -align;
58d180e8
AM
14161 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14162 htab->glink_eh_frame->size = size;
14163 }
14164
e7d1c40c 14165 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14166 for (group = htab->group; group != NULL; group = group->next)
14167 if (group->stub_sec != NULL)
691d2e9a
AM
14168 {
14169 int align = abs (htab->params->plt_stub_align);
14170 group->stub_sec->size
14171 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14172 }
d4aaa2a0 14173
1657026c
AM
14174 if (htab->elf.srelrdyn != NULL)
14175 {
14176 bfd_vma r_offset;
14177
14178 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14179 if (!append_relr_off (htab, (r_offset
14180 + htab->brlt->output_section->vma
14181 + htab->brlt->output_offset)))
14182 return false;
14183
14184 if (!got_and_plt_relr_for_local_syms (info))
14185 return false;
14186 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14187 if (htab->stub_error)
14188 return false;
14189
14190 if (htab->relr_count > 1)
14191 qsort (htab->relr_addr, htab->relr_count, sizeof (*htab->relr_addr),
14192 compare_relr_address);
14193
14194 size_t i = 0;
14195 while (i < htab->relr_count)
14196 {
14197 bfd_vma base = htab->relr_addr[i];
14198 htab->elf.srelrdyn->size += 8;
14199 i++;
14200 /* Handle possible duplicate address. This can happen
14201 as sections increase in size when adding stubs. */
14202 while (i < htab->relr_count
14203 && htab->relr_addr[i] == base)
14204 i++;
14205 base += 8;
14206 while (1)
14207 {
14208 size_t start_i = i;
14209 while (i < htab->relr_count
14210 && htab->relr_addr[i] - base < 63 * 8
14211 && (htab->relr_addr[i] - base) % 8 == 0)
14212 i++;
14213 if (i == start_i)
14214 break;
14215 htab->elf.srelrdyn->size += 8;
14216 base += 63 * 8;
14217 }
14218 }
14219 }
14220
d4aaa2a0
AM
14221 for (group = htab->group; group != NULL; group = group->next)
14222 if (group->stub_sec != NULL
14223 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 14224 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 14225 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
14226 break;
14227
d4aaa2a0 14228 if (group == NULL
0441f94f
AM
14229 && (!htab->stub_changed
14230 || htab->stub_iteration > STUB_SHRINK_ITER)
ba21f564
AM
14231 && (htab->brlt->rawsize == htab->brlt->size
14232 || (htab->stub_iteration > STUB_SHRINK_ITER
14233 && htab->brlt->rawsize > htab->brlt->size))
1657026c
AM
14234 && (htab->elf.srelrdyn == NULL
14235 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14236 || (htab->stub_iteration > STUB_SHRINK_ITER
14237 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
58d180e8 14238 && (htab->glink_eh_frame == NULL
a804e476
AM
14239 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14240 && (htab->tga_group == NULL
14241 || htab->stub_iteration > 1))
5c3dead3
AM
14242 break;
14243
721956f4 14244 /* Ask the linker to do its stuff. */
e7d1c40c 14245 (*htab->params->layout_sections_again) ();
721956f4
AM
14246 }
14247
da44f4e5
AM
14248 if (htab->glink_eh_frame != NULL
14249 && htab->glink_eh_frame->size != 0)
14250 {
14251 bfd_vma val;
14252 bfd_byte *p, *last_fde;
14253 size_t last_fde_len, size, align, pad;
d4aaa2a0 14254 struct map_stub *group;
da44f4e5 14255
df136d64
AM
14256 /* It is necessary to at least have a rough outline of the
14257 linker generated CIEs and FDEs written before
14258 bfd_elf_discard_info is run, in order for these FDEs to be
14259 indexed in .eh_frame_hdr. */
da44f4e5
AM
14260 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14261 if (p == NULL)
0a1b45a2 14262 return false;
da44f4e5
AM
14263 htab->glink_eh_frame->contents = p;
14264 last_fde = p;
2e0ce1c8 14265 align = 4;
da44f4e5
AM
14266
14267 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14268 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 14269 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 14270 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 14271 p += last_fde_len + 4;
da44f4e5 14272
d4aaa2a0 14273 for (group = htab->group; group != NULL; group = group->next)
df136d64 14274 if (group->eh_size != 0)
da44f4e5 14275 {
df136d64 14276 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 14277 last_fde = p;
df136d64 14278 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 14279 /* FDE length. */
2e0ce1c8 14280 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14281 p += 4;
14282 /* CIE pointer. */
14283 val = p - htab->glink_eh_frame->contents;
14284 bfd_put_32 (htab->elf.dynobj, val, p);
14285 p += 4;
14286 /* Offset to stub section, written later. */
14287 p += 4;
14288 /* stub section size. */
d4aaa2a0 14289 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
14290 p += 4;
14291 /* Augmentation. */
14292 p += 1;
df136d64
AM
14293 /* Make sure we don't have all nops. This is enough for
14294 elf-eh-frame.c to detect the last non-nop opcode. */
14295 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 14296 p = last_fde + last_fde_len + 4;
da44f4e5
AM
14297 }
14298 if (htab->glink != NULL && htab->glink->size != 0)
14299 {
14300 last_fde = p;
2e0ce1c8 14301 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 14302 /* FDE length. */
2e0ce1c8 14303 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
14304 p += 4;
14305 /* CIE pointer. */
14306 val = p - htab->glink_eh_frame->contents;
14307 bfd_put_32 (htab->elf.dynobj, val, p);
14308 p += 4;
14309 /* Offset to .glink, written later. */
14310 p += 4;
14311 /* .glink size. */
14312 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14313 p += 4;
14314 /* Augmentation. */
14315 p += 1;
14316
3cd7c7d7 14317 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
14318 *p++ = DW_CFA_register;
14319 *p++ = 65;
9f08fa5c 14320 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 14321 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
14322 *p++ = DW_CFA_restore_extended;
14323 *p++ = 65;
2e0ce1c8 14324 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
14325 }
14326 /* Subsume any padding into the last FDE if user .eh_frame
14327 sections are aligned more than glink_eh_frame. Otherwise any
14328 zero padding will be seen as a terminator. */
2e0ce1c8 14329 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 14330 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 14331 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
14332 htab->glink_eh_frame->size = size + pad;
14333 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14334 }
14335
d969d15f 14336 maybe_strip_output (info, htab->brlt);
2efec98b
AM
14337 if (htab->relbrlt != NULL)
14338 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
14339 if (htab->glink_eh_frame != NULL)
14340 maybe_strip_output (info, htab->glink_eh_frame);
1657026c
AM
14341 if (htab->elf.srelrdyn != NULL)
14342 maybe_strip_output (info, htab->elf.srelrdyn);
721956f4 14343
0a1b45a2 14344 return true;
721956f4
AM
14345}
14346
14347/* Called after we have determined section placement. If sections
805fc799 14348 move, we'll be called again. Provide a value for TOCstart. */
721956f4 14349
805fc799 14350bfd_vma
1c865ab2 14351ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 14352{
805fc799 14353 asection *s;
a27e685f 14354 bfd_vma TOCstart, adjust;
721956f4 14355
43417696
AM
14356 if (info != NULL)
14357 {
14358 struct elf_link_hash_entry *h;
14359 struct elf_link_hash_table *htab = elf_hash_table (info);
14360
2cc15b10 14361 if (is_elf_hash_table (&htab->root)
43417696
AM
14362 && htab->hgot != NULL)
14363 h = htab->hgot;
14364 else
14365 {
2cc15b10
AM
14366 h = (struct elf_link_hash_entry *)
14367 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14368 if (is_elf_hash_table (&htab->root))
43417696
AM
14369 htab->hgot = h;
14370 }
14371 if (h != NULL
14372 && h->root.type == bfd_link_hash_defined
14373 && !h->root.linker_def
2cc15b10 14374 && (!is_elf_hash_table (&htab->root)
43417696
AM
14375 || h->def_regular))
14376 {
ed7007c1 14377 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
14378 _bfd_set_gp_value (obfd, TOCstart);
14379 return TOCstart;
14380 }
14381 }
14382
805fc799
AM
14383 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14384 order. The TOC starts where the first of these sections starts. */
14385 s = bfd_get_section_by_name (obfd, ".got");
e054468f 14386 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14387 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 14388 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14389 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 14390 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14391 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 14392 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
14393 {
14394 /* This may happen for
14395 o references to TOC base (SYM@toc / TOC[tc0]) without a
14396 .toc directive
14397 o bad linker script
14398 o --gc-sections and empty TOC sections
14399
14400 FIXME: Warn user? */
14401
14402 /* Look for a likely section. We probably won't even be
14403 using TOCstart. */
14404 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14405 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14406 | SEC_EXCLUDE))
805fc799
AM
14407 == (SEC_ALLOC | SEC_SMALL_DATA))
14408 break;
721956f4 14409 if (s == NULL)
805fc799 14410 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14411 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14412 == (SEC_ALLOC | SEC_SMALL_DATA))
14413 break;
721956f4 14414 if (s == NULL)
805fc799 14415 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14416 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14417 == SEC_ALLOC)
805fc799 14418 break;
721956f4 14419 if (s == NULL)
805fc799 14420 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14421 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14422 break;
14423 }
721956f4 14424
805fc799
AM
14425 TOCstart = 0;
14426 if (s != NULL)
14427 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14428
a27e685f
AM
14429 /* Force alignment. */
14430 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14431 TOCstart -= adjust;
1c865ab2
AM
14432 _bfd_set_gp_value (obfd, TOCstart);
14433
810d4e75 14434 if (info != NULL && s != NULL)
1c865ab2
AM
14435 {
14436 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14437
810d4e75
AM
14438 if (htab != NULL)
14439 {
14440 if (htab->elf.hgot != NULL)
14441 {
a27e685f 14442 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14443 htab->elf.hgot->root.u.def.section = s;
14444 }
14445 }
14446 else
1c865ab2 14447 {
810d4e75
AM
14448 struct bfd_link_hash_entry *bh = NULL;
14449 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f 14450 s, TOC_BASE_OFF - adjust,
0a1b45a2 14451 NULL, false, false, &bh);
1c865ab2
AM
14452 }
14453 }
805fc799 14454 return TOCstart;
721956f4
AM
14455}
14456
a345bc8d 14457/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14458 write out any global entry stubs, and PLT relocations. */
a345bc8d 14459
0a1b45a2 14460static bool
49c09209 14461build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14462{
14463 struct bfd_link_info *info;
14464 struct ppc_link_hash_table *htab;
49c09209 14465 struct plt_entry *ent;
a345bc8d
AM
14466 asection *s;
14467
14468 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 14469 return true;
a345bc8d 14470
49c09209
AM
14471 info = inf;
14472 htab = ppc_hash_table (info);
14473 if (htab == NULL)
0a1b45a2 14474 return false;
49c09209
AM
14475
14476 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14477 if (ent->plt.offset != (bfd_vma) -1)
14478 {
14479 /* This symbol has an entry in the procedure linkage
14480 table. Set it up. */
14481 Elf_Internal_Rela rela;
2d7ad24e 14482 asection *plt, *relplt;
49c09209
AM
14483 bfd_byte *loc;
14484
30845f11 14485 if (use_local_plt (info, h))
49c09209
AM
14486 {
14487 if (!(h->def_regular
14488 && (h->root.type == bfd_link_hash_defined
14489 || h->root.type == bfd_link_hash_defweak)))
14490 continue;
2d7ad24e
AM
14491 if (h->type == STT_GNU_IFUNC)
14492 {
14493 plt = htab->elf.iplt;
14494 relplt = htab->elf.irelplt;
0a1b45a2 14495 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14496 if (htab->opd_abi)
14497 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14498 else
14499 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14500 }
49c09209 14501 else
2d7ad24e
AM
14502 {
14503 plt = htab->pltlocal;
1657026c
AM
14504 relplt = NULL;
14505 if (bfd_link_pic (info)
14506 && !(info->enable_dt_relr && !htab->opd_abi))
2d7ad24e
AM
14507 {
14508 relplt = htab->relpltlocal;
14509 if (htab->opd_abi)
14510 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14511 else
14512 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14513 }
2d7ad24e 14514 }
ed7007c1 14515 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14516
14517 if (relplt == NULL)
14518 {
14519 loc = plt->contents + ent->plt.offset;
14520 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14521 if (htab->opd_abi)
14522 {
14523 bfd_vma toc = elf_gp (info->output_bfd);
14524 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14525 bfd_put_64 (info->output_bfd, toc, loc + 8);
14526 }
14527 }
14528 else
14529 {
14530 rela.r_offset = (plt->output_section->vma
14531 + plt->output_offset
14532 + ent->plt.offset);
14533 loc = relplt->contents + (relplt->reloc_count++
14534 * sizeof (Elf64_External_Rela));
14535 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14536 }
49c09209
AM
14537 }
14538 else
14539 {
14540 rela.r_offset = (htab->elf.splt->output_section->vma
14541 + htab->elf.splt->output_offset
14542 + ent->plt.offset);
14543 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14544 rela.r_addend = ent->addend;
14545 loc = (htab->elf.srelplt->contents
14546 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14547 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14548 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
0a1b45a2 14549 htab->elf.ifunc_resolvers = true;
2d7ad24e 14550 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14551 }
49c09209
AM
14552 }
14553
a345bc8d 14554 if (!h->pointer_equality_needed)
0a1b45a2 14555 return true;
a345bc8d
AM
14556
14557 if (h->def_regular)
0a1b45a2 14558 return true;
a345bc8d 14559
9e390558 14560 s = htab->global_entry;
49c09209 14561 if (s == NULL || s->size == 0)
0a1b45a2 14562 return true;
49c09209
AM
14563
14564 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14565 if (ent->plt.offset != (bfd_vma) -1
14566 && ent->addend == 0)
a345bc8d
AM
14567 {
14568 bfd_byte *p;
14569 asection *plt;
14570 bfd_vma off;
14571
a345bc8d 14572 p = s->contents + h->root.u.def.value;
33e44f2e 14573 plt = htab->elf.splt;
30845f11 14574 if (use_local_plt (info, h))
2d7ad24e
AM
14575 {
14576 if (h->type == STT_GNU_IFUNC)
14577 plt = htab->elf.iplt;
14578 else
14579 plt = htab->pltlocal;
14580 }
49c09209 14581 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14582 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14583
14584 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14585 {
14586 info->callbacks->einfo
c1c8c1ef 14587 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14588 h->root.root.string);
14589 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14590 htab->stub_error = true;
a345bc8d
AM
14591 }
14592
7341d5e2
AM
14593 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14594 if (htab->params->emit_stub_syms)
14595 {
14596 size_t len = strlen (h->root.root.string);
14597 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14598
14599 if (name == NULL)
0a1b45a2 14600 return false;
7341d5e2
AM
14601
14602 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
0a1b45a2 14603 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
7341d5e2 14604 if (h == NULL)
0a1b45a2 14605 return false;
7341d5e2
AM
14606 if (h->root.type == bfd_link_hash_new)
14607 {
14608 h->root.type = bfd_link_hash_defined;
14609 h->root.u.def.section = s;
14610 h->root.u.def.value = p - s->contents;
14611 h->ref_regular = 1;
14612 h->def_regular = 1;
14613 h->ref_regular_nonweak = 1;
14614 h->forced_local = 1;
14615 h->non_elf = 0;
2ec55de3 14616 h->root.linker_def = 1;
7341d5e2
AM
14617 }
14618 }
14619
a345bc8d
AM
14620 if (PPC_HA (off) != 0)
14621 {
14622 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14623 p += 4;
14624 }
14625 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14626 p += 4;
14627 bfd_put_32 (s->owner, MTCTR_R12, p);
14628 p += 4;
407aa07c 14629 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14630 break;
14631 }
0a1b45a2 14632 return true;
a345bc8d
AM
14633}
14634
49c09209
AM
14635/* Write PLT relocs for locals. */
14636
0a1b45a2 14637static bool
49c09209
AM
14638write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14639{
14640 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14641 bfd *ibfd;
14642
14643 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14644 {
14645 struct got_entry **lgot_ents, **end_lgot_ents;
14646 struct plt_entry **local_plt, **lplt, **end_local_plt;
14647 Elf_Internal_Shdr *symtab_hdr;
14648 bfd_size_type locsymcount;
14649 Elf_Internal_Sym *local_syms = NULL;
14650 struct plt_entry *ent;
14651
14652 if (!is_ppc64_elf (ibfd))
14653 continue;
14654
14655 lgot_ents = elf_local_got_ents (ibfd);
14656 if (!lgot_ents)
14657 continue;
14658
14659 symtab_hdr = &elf_symtab_hdr (ibfd);
14660 locsymcount = symtab_hdr->sh_info;
14661 end_lgot_ents = lgot_ents + locsymcount;
14662 local_plt = (struct plt_entry **) end_lgot_ents;
14663 end_local_plt = local_plt + locsymcount;
14664 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14665 for (ent = *lplt; ent != NULL; ent = ent->next)
14666 if (ent->plt.offset != (bfd_vma) -1)
14667 {
14668 Elf_Internal_Sym *sym;
14669 asection *sym_sec;
14670 asection *plt, *relplt;
14671 bfd_byte *loc;
14672 bfd_vma val;
49c09209
AM
14673
14674 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14675 lplt - local_plt, ibfd))
14676 {
c9594989 14677 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209 14678 free (local_syms);
0a1b45a2 14679 return false;
49c09209
AM
14680 }
14681
14682 val = sym->st_value + ent->addend;
49c09209
AM
14683 if (sym_sec != NULL && sym_sec->output_section != NULL)
14684 val += sym_sec->output_offset + sym_sec->output_section->vma;
14685
2d7ad24e
AM
14686 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14687 {
0a1b45a2 14688 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14689 plt = htab->elf.iplt;
14690 relplt = htab->elf.irelplt;
14691 }
14692 else
14693 {
14694 plt = htab->pltlocal;
1657026c
AM
14695 relplt = NULL;
14696 if (bfd_link_pic (info)
14697 && !(info->enable_dt_relr && !htab->opd_abi))
14698 relplt = htab->relpltlocal;
2d7ad24e 14699 }
49c09209 14700
2d7ad24e
AM
14701 if (relplt == NULL)
14702 {
14703 loc = plt->contents + ent->plt.offset;
14704 bfd_put_64 (info->output_bfd, val, loc);
14705 if (htab->opd_abi)
14706 {
14707 bfd_vma toc = elf_gp (ibfd);
14708 bfd_put_64 (info->output_bfd, toc, loc + 8);
14709 }
14710 }
49c09209 14711 else
2d7ad24e
AM
14712 {
14713 Elf_Internal_Rela rela;
14714 rela.r_offset = (ent->plt.offset
14715 + plt->output_offset
14716 + plt->output_section->vma);
14717 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14718 {
14719 if (htab->opd_abi)
14720 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14721 else
14722 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14723 }
14724 else
14725 {
14726 if (htab->opd_abi)
14727 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14728 else
14729 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14730 }
14731 rela.r_addend = val;
14732 loc = relplt->contents + (relplt->reloc_count++
14733 * sizeof (Elf64_External_Rela));
14734 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14735 }
49c09209
AM
14736 }
14737
14738 if (local_syms != NULL
14739 && symtab_hdr->contents != (unsigned char *) local_syms)
14740 {
14741 if (!info->keep_memory)
14742 free (local_syms);
14743 else
14744 symtab_hdr->contents = (unsigned char *) local_syms;
14745 }
14746 }
0a1b45a2 14747 return true;
49c09209
AM
14748}
14749
a804e476
AM
14750/* Emit the static wrapper function preserving registers around a
14751 __tls_get_addr_opt call. */
14752
0a1b45a2 14753static bool
a804e476
AM
14754emit_tga_desc (struct ppc_link_hash_table *htab)
14755{
14756 asection *stub_sec = htab->tga_group->stub_sec;
14757 unsigned int cfa_updt = 11 * 4;
14758 bfd_byte *p;
14759 bfd_vma to, from, delta;
14760
14761 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14762 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14763 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14764 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14765 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14766 delta = to - from;
14767 if (delta + (1 << 25) >= 1 << 26)
14768 {
14769 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
0a1b45a2
AM
14770 htab->stub_error = true;
14771 return false;
a804e476
AM
14772 }
14773
14774 p = stub_sec->contents;
14775 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14776 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14777 p += 4;
14778 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14779 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14780}
14781
14782/* Emit eh_frame describing the static wrapper function. */
14783
14784static bfd_byte *
14785emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14786{
14787 unsigned int cfa_updt = 11 * 4;
14788 unsigned int i;
14789
14790 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14791 *p++ = DW_CFA_def_cfa_offset;
14792 if (htab->opd_abi)
14793 {
14794 *p++ = 128;
14795 *p++ = 1;
14796 }
14797 else
14798 *p++ = 96;
14799 *p++ = DW_CFA_offset_extended_sf;
14800 *p++ = 65;
14801 *p++ = (-16 / 8) & 0x7f;
14802 for (i = 4; i < 12; i++)
14803 {
14804 *p++ = DW_CFA_offset + i;
14805 *p++ = (htab->opd_abi ? 13 : 12) - i;
14806 }
14807 *p++ = DW_CFA_advance_loc + 10;
14808 *p++ = DW_CFA_def_cfa_offset;
14809 *p++ = 0;
14810 for (i = 4; i < 12; i++)
14811 *p++ = DW_CFA_restore + i;
14812 *p++ = DW_CFA_advance_loc + 2;
14813 *p++ = DW_CFA_restore_extended;
14814 *p++ = 65;
14815 return p;
14816}
14817
721956f4
AM
14818/* Build all the stubs associated with the current output file.
14819 The stubs are kept in a hash table attached to the main linker
14820 hash table. This function is called via gldelf64ppc_finish. */
14821
0a1b45a2 14822bool
e7d1c40c 14823ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14824 char **stats)
5d1634d7
AM
14825{
14826 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14827 struct map_stub *group;
721956f4 14828 asection *stub_sec;
5d1634d7 14829 bfd_byte *p;
e717da7e 14830 int stub_sec_count = 0;
5d1634d7 14831
4dfe6ac6 14832 if (htab == NULL)
0a1b45a2 14833 return false;
4dfe6ac6 14834
eea6121a 14835 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14836 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14837 {
14838 group->eh_size = 0;
14839 group->lr_restore = 0;
14840 if ((stub_sec = group->stub_sec) != NULL
14841 && stub_sec->size != 0)
14842 {
14843 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14844 stub_sec->size);
14845 if (stub_sec->contents == NULL)
0a1b45a2 14846 return false;
df136d64
AM
14847 stub_sec->size = 0;
14848 }
14849 }
5d1634d7 14850
23eb7e01 14851 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14852 {
9f951329 14853 unsigned int indx;
ad8e1ba5 14854 bfd_vma plt0;
9f951329 14855
721956f4 14856 /* Build the .glink plt call stub. */
e7d1c40c 14857 if (htab->params->emit_stub_syms)
97b639ba
AM
14858 {
14859 struct elf_link_hash_entry *h;
468392fb 14860 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
0a1b45a2 14861 true, false, false);
97b639ba 14862 if (h == NULL)
0a1b45a2 14863 return false;
97b639ba
AM
14864 if (h->root.type == bfd_link_hash_new)
14865 {
14866 h->root.type = bfd_link_hash_defined;
14867 h->root.u.def.section = htab->glink;
ee4bf8d2 14868 h->root.u.def.value = 8;
f5385ebf
AM
14869 h->ref_regular = 1;
14870 h->def_regular = 1;
14871 h->ref_regular_nonweak = 1;
14872 h->forced_local = 1;
14873 h->non_elf = 0;
2ec55de3 14874 h->root.linker_def = 1;
97b639ba
AM
14875 }
14876 }
33e44f2e
AM
14877 plt0 = (htab->elf.splt->output_section->vma
14878 + htab->elf.splt->output_offset
14879 - 16);
176a0d42
AM
14880 if (info->emitrelocations)
14881 {
14882 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14883 if (r == NULL)
0a1b45a2 14884 return false;
176a0d42
AM
14885 r->r_offset = (htab->glink->output_offset
14886 + htab->glink->output_section->vma);
14887 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14888 r->r_addend = plt0;
14889 }
4ce794b7 14890 p = htab->glink->contents;
176a0d42 14891 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14892 bfd_put_64 (htab->glink->owner, plt0, p);
14893 p += 8;
b9e5796b
AM
14894 if (htab->opd_abi)
14895 {
14896 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14897 p += 4;
14898 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14899 p += 4;
14900 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14901 p += 4;
14902 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14903 p += 4;
14904 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14905 p += 4;
14906 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14907 p += 4;
14908 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14909 p += 4;
14910 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14911 p += 4;
14912 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14913 p += 4;
14914 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14915 p += 4;
14916 }
14917 else
14918 {
3cd7c7d7
AM
14919 unsigned int insn;
14920
14921 /* 0:
14922 . .quad plt0-1f # plt0 entry relative to 1:
14923 #
14924 # We get here with r12 initially @ a glink branch
14925 # Load the address of _dl_runtime_resolve from plt0 and
14926 # jump to it, with r0 set to the index of the PLT entry
14927 # to be resolved and r11 the link map.
14928 __glink_PLTresolve:
14929 . std %r2,24(%r1) # optional
14930 . mflr %r0
14931 . bcl 20,31,1f
14932 1:
14933 . mflr %r11
14934 . mtlr %r0
14935 . ld %r0,(0b-1b)(%r11)
14936 . sub %r12,%r12,%r11
14937 . add %r11,%r0,%r11
14938 . addi %r0,%r12,1b-2f
14939 . ld %r12,0(%r11)
14940 . srdi %r0,%r0,2
14941 . mtctr %r12
14942 . ld %r11,8(%r11)
14943 . bctr
14944 2:
14945 . b __glink_PLTresolve
14946 . ...
14947 . b __glink_PLTresolve */
14948
14949 if (htab->has_plt_localentry0)
14950 {
14951 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14952 p += 4;
14953 }
b9e5796b
AM
14954 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14955 p += 4;
14956 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14957 p += 4;
14958 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14959 p += 4;
b9e5796b
AM
14960 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14961 p += 4;
3cd7c7d7
AM
14962 if (htab->has_plt_localentry0)
14963 insn = LD_R0_0R11 | (-20 & 0xfffc);
14964 else
14965 insn = LD_R0_0R11 | (-16 & 0xfffc);
14966 bfd_put_32 (htab->glink->owner, insn, p);
14967 p += 4;
b9e5796b
AM
14968 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14969 p += 4;
3cd7c7d7 14970 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 14971 p += 4;
3cd7c7d7 14972 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
14973 p += 4;
14974 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14975 p += 4;
14976 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14977 p += 4;
14978 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14979 p += 4;
14980 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14981 p += 4;
14982 }
407aa07c
AM
14983 bfd_put_32 (htab->glink->owner, BCTR, p);
14984 p += 4;
c75bc4f7 14985 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14986
9f951329
AM
14987 /* Build the .glink lazy link call stubs. */
14988 indx = 0;
9e390558 14989 while (p < htab->glink->contents + htab->glink->size)
9f951329 14990 {
b9e5796b 14991 if (htab->opd_abi)
9f951329 14992 {
b9e5796b
AM
14993 if (indx < 0x8000)
14994 {
14995 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14996 p += 4;
14997 }
14998 else
14999 {
15000 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15001 p += 4;
15002 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15003 p);
15004 p += 4;
15005 }
9f951329 15006 }
4ce794b7 15007 bfd_put_32 (htab->glink->owner,
ee4bf8d2 15008 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 15009 indx++;
9f951329
AM
15010 p += 4;
15011 }
5d1634d7 15012 }
5d1634d7 15013
a804e476
AM
15014 if (htab->tga_group != NULL)
15015 {
15016 htab->tga_group->lr_restore = 23 * 4;
15017 htab->tga_group->stub_sec->size = 24 * 4;
15018 if (!emit_tga_desc (htab))
0a1b45a2 15019 return false;
a804e476
AM
15020 if (htab->glink_eh_frame != NULL
15021 && htab->glink_eh_frame->size != 0)
15022 {
15023 size_t align = 4;
15024
15025 p = htab->glink_eh_frame->contents;
15026 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15027 p += 17;
15028 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15029 }
15030 }
15031
49c09209
AM
15032 /* Build .glink global entry stubs, and PLT relocs for globals. */
15033 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15034
15035 if (!write_plt_relocs_for_local_syms (info))
0a1b45a2 15036 return false;
9e390558 15037
7341d5e2 15038 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 15039 {
4ce794b7 15040 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 15041 htab->brlt->size);
4ce794b7 15042 if (htab->brlt->contents == NULL)
0a1b45a2 15043 return false;
721956f4 15044 }
ee75fd95 15045 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
15046 {
15047 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 15048 htab->relbrlt->size);
63bc6f6c 15049 if (htab->relbrlt->contents == NULL)
0a1b45a2 15050 return false;
63bc6f6c 15051 }
5d1634d7 15052
721956f4
AM
15053 /* Build the stubs as directed by the stub hash table. */
15054 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 15055
a4b6fadd
AM
15056 for (group = htab->group; group != NULL; group = group->next)
15057 if (group->needs_save_res)
7dda8d3c 15058 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 15059
aa8a7074
AM
15060 if (htab->relbrlt != NULL)
15061 htab->relbrlt->reloc_count = 0;
15062
e7d1c40c 15063 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
15064 for (group = htab->group; group != NULL; group = group->next)
15065 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
15066 {
15067 int align = abs (htab->params->plt_stub_align);
15068 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15069 }
794e51c0 15070
7dda8d3c
AM
15071 for (group = htab->group; group != NULL; group = group->next)
15072 if (group->needs_save_res)
15073 {
15074 stub_sec = group->stub_sec;
15075 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15076 htab->sfpr->contents, htab->sfpr->size);
15077 if (htab->params->emit_stub_syms)
15078 {
15079 unsigned int i;
15080
15081 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15082 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
0a1b45a2 15083 return false;
7dda8d3c
AM
15084 }
15085 }
15086
df136d64
AM
15087 if (htab->glink_eh_frame != NULL
15088 && htab->glink_eh_frame->size != 0)
15089 {
15090 bfd_vma val;
15091 size_t align = 4;
15092
15093 p = htab->glink_eh_frame->contents;
15094 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15095
15096 for (group = htab->group; group != NULL; group = group->next)
15097 if (group->eh_size != 0)
15098 {
15099 /* Offset to stub section. */
15100 val = (group->stub_sec->output_section->vma
15101 + group->stub_sec->output_offset);
15102 val -= (htab->glink_eh_frame->output_section->vma
15103 + htab->glink_eh_frame->output_offset
15104 + (p + 8 - htab->glink_eh_frame->contents));
15105 if (val + 0x80000000 > 0xffffffff)
15106 {
15107 _bfd_error_handler
15108 (_("%s offset too large for .eh_frame sdata4 encoding"),
15109 group->stub_sec->name);
0a1b45a2 15110 return false;
df136d64
AM
15111 }
15112 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15113 p += (group->eh_size + 17 + 3) & -4;
15114 }
15115 if (htab->glink != NULL && htab->glink->size != 0)
15116 {
15117 /* Offset to .glink. */
15118 val = (htab->glink->output_section->vma
15119 + htab->glink->output_offset
15120 + 8);
15121 val -= (htab->glink_eh_frame->output_section->vma
15122 + htab->glink_eh_frame->output_offset
15123 + (p + 8 - htab->glink_eh_frame->contents));
15124 if (val + 0x80000000 > 0xffffffff)
15125 {
15126 _bfd_error_handler
15127 (_("%s offset too large for .eh_frame sdata4 encoding"),
15128 htab->glink->name);
0a1b45a2 15129 return false;
df136d64
AM
15130 }
15131 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15132 p += (24 + align - 1) & -align;
15133 }
15134 }
15135
1657026c
AM
15136 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15137 {
15138 htab->elf.srelrdyn->contents
15139 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15140 if (htab->elf.srelrdyn->contents == NULL)
15141 return false;
15142
15143 size_t i = 0;
15144 bfd_byte *loc = htab->elf.srelrdyn->contents;
15145 while (i < htab->relr_count)
15146 {
15147 bfd_vma base = htab->relr_addr[i];
15148 BFD_ASSERT (base % 2 == 0);
15149 bfd_put_64 (htab->elf.dynobj, base, loc);
15150 loc += 8;
15151 i++;
15152 while (i < htab->relr_count
15153 && htab->relr_addr[i] == base)
15154 {
15155 htab->stub_error = true;
15156 i++;
15157 }
15158 base += 8;
15159 while (1)
15160 {
15161 bfd_vma bits = 0;
15162 while (i < htab->relr_count
15163 && htab->relr_addr[i] - base < 63 * 8
15164 && (htab->relr_addr[i] - base) % 8 == 0)
15165 {
15166 bits |= (bfd_vma) 1 << ((htab->relr_addr[i] - base) / 8);
15167 i++;
15168 }
15169 if (bits == 0)
15170 break;
15171 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15172 loc += 8;
15173 base += 63 * 8;
15174 }
15175 }
15176 /* Pad any excess with 1's, a do-nothing encoding. */
15177 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15178 < htab->elf.srelrdyn->size)
15179 {
15180 bfd_put_64 (htab->elf.dynobj, 1, loc);
15181 loc += 8;
15182 }
15183 }
15184
d4aaa2a0
AM
15185 for (group = htab->group; group != NULL; group = group->next)
15186 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
15187 {
15188 stub_sec_count += 1;
c9301e31
AM
15189 if (stub_sec->rawsize != stub_sec->size
15190 && (htab->stub_iteration <= STUB_SHRINK_ITER
15191 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
15192 break;
15193 }
5d1634d7 15194
25516cc5 15195 if (group != NULL)
1657026c
AM
15196 htab->stub_error = true;
15197
15198 if (htab->stub_error)
5d1634d7 15199 {
cf97bcb0 15200 _bfd_error_handler (_("stubs don't match calculated size"));
1657026c 15201 return false;
5d1634d7 15202 }
721956f4 15203
d2a300cf
AM
15204 if (stats != NULL)
15205 {
988b7300
AM
15206 char *groupmsg;
15207 if (asprintf (&groupmsg,
15208 ngettext ("linker stubs in %u group\n",
15209 "linker stubs in %u groups\n",
15210 stub_sec_count),
15211 stub_sec_count) < 0)
15212 *stats = NULL;
15213 else
15214 {
15215 if (asprintf (stats, _("%s"
15216 " branch %lu\n"
988b7300 15217 " long branch %lu\n"
988b7300 15218 " plt call %lu\n"
988b7300
AM
15219 " global entry %lu"),
15220 groupmsg,
15221 htab->stub_count[ppc_stub_long_branch - 1],
988b7300 15222 htab->stub_count[ppc_stub_plt_branch - 1],
988b7300 15223 htab->stub_count[ppc_stub_plt_call - 1],
988b7300
AM
15224 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15225 *stats = NULL;
15226 free (groupmsg);
15227 }
d2a300cf 15228 }
0a1b45a2 15229 return true;
5bd4f169
AM
15230}
15231
60124e18
AM
15232/* What to do when ld finds relocations against symbols defined in
15233 discarded sections. */
15234
15235static unsigned int
15236ppc64_elf_action_discarded (asection *sec)
15237{
15238 if (strcmp (".opd", sec->name) == 0)
15239 return 0;
15240
15241 if (strcmp (".toc", sec->name) == 0)
15242 return 0;
15243
bce50a28
JJ
15244 if (strcmp (".toc1", sec->name) == 0)
15245 return 0;
15246
60124e18
AM
15247 return _bfd_elf_default_action_discarded (sec);
15248}
15249
e59a1001
AM
15250/* These are the dynamic relocations supported by glibc. */
15251
0a1b45a2 15252static bool
e59a1001
AM
15253ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15254{
15255 switch (r_type)
15256 {
15257 case R_PPC64_RELATIVE:
15258 case R_PPC64_NONE:
15259 case R_PPC64_ADDR64:
15260 case R_PPC64_GLOB_DAT:
15261 case R_PPC64_IRELATIVE:
15262 case R_PPC64_JMP_IREL:
15263 case R_PPC64_JMP_SLOT:
15264 case R_PPC64_DTPMOD64:
15265 case R_PPC64_DTPREL64:
15266 case R_PPC64_TPREL64:
15267 case R_PPC64_TPREL16_LO_DS:
15268 case R_PPC64_TPREL16_DS:
15269 case R_PPC64_TPREL16:
15270 case R_PPC64_TPREL16_LO:
15271 case R_PPC64_TPREL16_HI:
15272 case R_PPC64_TPREL16_HIGH:
15273 case R_PPC64_TPREL16_HA:
15274 case R_PPC64_TPREL16_HIGHA:
15275 case R_PPC64_TPREL16_HIGHER:
15276 case R_PPC64_TPREL16_HIGHEST:
15277 case R_PPC64_TPREL16_HIGHERA:
15278 case R_PPC64_TPREL16_HIGHESTA:
15279 case R_PPC64_ADDR16_LO_DS:
15280 case R_PPC64_ADDR16_LO:
15281 case R_PPC64_ADDR16_HI:
15282 case R_PPC64_ADDR16_HIGH:
15283 case R_PPC64_ADDR16_HA:
15284 case R_PPC64_ADDR16_HIGHA:
15285 case R_PPC64_REL30:
15286 case R_PPC64_COPY:
15287 case R_PPC64_UADDR64:
15288 case R_PPC64_UADDR32:
15289 case R_PPC64_ADDR32:
15290 case R_PPC64_ADDR24:
15291 case R_PPC64_ADDR16:
15292 case R_PPC64_UADDR16:
15293 case R_PPC64_ADDR16_DS:
15294 case R_PPC64_ADDR16_HIGHER:
15295 case R_PPC64_ADDR16_HIGHEST:
15296 case R_PPC64_ADDR16_HIGHERA:
15297 case R_PPC64_ADDR16_HIGHESTA:
15298 case R_PPC64_ADDR14:
15299 case R_PPC64_ADDR14_BRTAKEN:
15300 case R_PPC64_ADDR14_BRNTAKEN:
15301 case R_PPC64_REL32:
15302 case R_PPC64_REL64:
0a1b45a2 15303 return true;
e59a1001
AM
15304
15305 default:
0a1b45a2 15306 return false;
e59a1001
AM
15307 }
15308}
15309
5bd4f169
AM
15310/* The RELOCATE_SECTION function is called by the ELF backend linker
15311 to handle the relocations for a section.
15312
15313 The relocs are always passed as Rela structures; if the section
15314 actually uses Rel structures, the r_addend field will always be
15315 zero.
15316
15317 This function is responsible for adjust the section contents as
15318 necessary, and (if using Rela relocs and generating a
1049f94e 15319 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
15320 necessary.
15321
15322 This function does not have to worry about setting the reloc
15323 address or the reloc symbol index.
15324
15325 LOCAL_SYMS is a pointer to the swapped in local symbols.
15326
15327 LOCAL_SECTIONS is an array giving the section in the input file
15328 corresponding to the st_shndx field of each local symbol.
15329
15330 The global hash table entry for the global symbols can be found
15331 via elf_sym_hashes (input_bfd).
15332
1049f94e 15333 When generating relocatable output, this function must handle
5bd4f169
AM
15334 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15335 going to be the section symbol corresponding to the output
15336 section, which means that the addend must be adjusted
15337 accordingly. */
15338
0f684201 15339static int
4ce794b7
AM
15340ppc64_elf_relocate_section (bfd *output_bfd,
15341 struct bfd_link_info *info,
15342 bfd *input_bfd,
15343 asection *input_section,
15344 bfd_byte *contents,
15345 Elf_Internal_Rela *relocs,
15346 Elf_Internal_Sym *local_syms,
15347 asection **local_sections)
5bd4f169 15348{
65f38f15 15349 struct ppc_link_hash_table *htab;
5bd4f169
AM
15350 Elf_Internal_Shdr *symtab_hdr;
15351 struct elf_link_hash_entry **sym_hashes;
5bd4f169 15352 Elf_Internal_Rela *rel;
c316a17c 15353 Elf_Internal_Rela *wrel;
5bd4f169 15354 Elf_Internal_Rela *relend;
411e1bfb
AM
15355 Elf_Internal_Rela outrel;
15356 bfd_byte *loc;
411e1bfb 15357 struct got_entry **local_got_ents;
5bd4f169 15358 bfd_vma TOCstart;
0a1b45a2
AM
15359 bool ret = true;
15360 bool is_opd;
794e51c0 15361 /* Assume 'at' branch hints. */
0a1b45a2
AM
15362 bool is_isa_v2 = true;
15363 bool warned_dynamic = false;
95f0d0d2 15364 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 15365
65f38f15 15366 /* Initialize howto table if needed. */
5bd4f169 15367 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
15368 ppc_howto_init ();
15369
65f38f15 15370 htab = ppc_hash_table (info);
4dfe6ac6 15371 if (htab == NULL)
0a1b45a2 15372 return false;
ee75fd95
AM
15373
15374 /* Don't relocate stub sections. */
e7d1c40c 15375 if (input_section->owner == htab->params->stub_bfd)
0a1b45a2 15376 return true;
ee75fd95 15377
7af5d5c4
AM
15378 if (!is_ppc64_elf (input_bfd))
15379 {
15380 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 15381 return false;
7af5d5c4 15382 }
0ffa91dd 15383
411e1bfb 15384 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 15385 TOCstart = elf_gp (output_bfd);
0ffa91dd 15386 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 15387 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 15388 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 15389
c316a17c 15390 rel = wrel = relocs;
5bd4f169 15391 relend = relocs + input_section->reloc_count;
c316a17c 15392 for (; rel < relend; wrel++, rel++)
5bd4f169 15393 {
04c9666a 15394 enum elf_ppc64_reloc_type r_type;
31c76678 15395 bfd_vma addend;
5bd4f169
AM
15396 bfd_reloc_status_type r;
15397 Elf_Internal_Sym *sym;
15398 asection *sec;
039b3fef
AM
15399 struct elf_link_hash_entry *h_elf;
15400 struct ppc_link_hash_entry *h;
15401 struct ppc_link_hash_entry *fdh;
5bd4f169 15402 const char *sym_name;
0d4792f7 15403 unsigned long r_symndx, toc_symndx;
3a71aa26 15404 bfd_vma toc_addend;
f961d9dd
AM
15405 unsigned char tls_mask, tls_gd, tls_type;
15406 unsigned char sym_type;
5bd4f169 15407 bfd_vma relocation;
0a1b45a2
AM
15408 bool unresolved_reloc, save_unresolved_reloc;
15409 bool warned;
bc30df16 15410 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 15411 unsigned int insn;
e11840f9 15412 unsigned int mask;
721956f4
AM
15413 struct ppc_stub_hash_entry *stub_entry;
15414 bfd_vma max_br_offset;
15415 bfd_vma from;
c316a17c 15416 Elf_Internal_Rela orig_rel;
b80eed39
AM
15417 reloc_howto_type *howto;
15418 struct reloc_howto_struct alt_howto;
4a421c53
AM
15419 uint64_t pinsn;
15420 bfd_vma offset;
5bd4f169 15421
c316a17c
AM
15422 again:
15423 orig_rel = *rel;
15424
4ce794b7 15425 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 15426 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
15427
15428 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15429 symbol of the previous ADDR64 reloc. The symbol gives us the
15430 proper TOC base to use. */
15431 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
15432 && wrel != relocs
15433 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 15434 && is_opd)
c316a17c 15435 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 15436
4ce794b7
AM
15437 sym = NULL;
15438 sec = NULL;
039b3fef 15439 h_elf = NULL;
4ce794b7 15440 sym_name = NULL;
0a1b45a2
AM
15441 unresolved_reloc = false;
15442 warned = false;
65f38f15 15443
0b13192e 15444 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15445 {
15446 /* It's a local symbol. */
74f0fb50 15447 struct _opd_sec_data *opd;
4025353c 15448
5bd4f169
AM
15449 sym = local_syms + r_symndx;
15450 sec = local_sections[r_symndx];
26c61ae5 15451 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15452 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15453 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15454 opd = get_opd_info (sec);
15455 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15456 {
51aecdc5
AM
15457 long adjust = opd->adjust[OPD_NDX (sym->st_value
15458 + rel->r_addend)];
4025353c
AM
15459 if (adjust == -1)
15460 relocation = 0;
15461 else
4cc603a5
AM
15462 {
15463 /* If this is a relocation against the opd section sym
15464 and we have edited .opd, adjust the reloc addend so
15465 that ld -r and ld --emit-relocs output is correct.
15466 If it is a reloc against some other .opd symbol,
15467 then the symbol value will be adjusted later. */
15468 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15469 rel->r_addend += adjust;
15470 else
15471 relocation += adjust;
15472 }
1e2f5b6e 15473 }
5bd4f169
AM
15474 }
15475 else
15476 {
0a1b45a2 15477 bool ignored;
62d887d4 15478
b2a8e766
AM
15479 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15480 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15481 h_elf, sec, relocation,
62d887d4 15482 unresolved_reloc, warned, ignored);
039b3fef
AM
15483 sym_name = h_elf->root.root.string;
15484 sym_type = h_elf->type;
b69fdb4e
AM
15485 if (sec != NULL
15486 && sec->owner == output_bfd
15487 && strcmp (sec->name, ".opd") == 0)
15488 {
15489 /* This is a symbol defined in a linker script. All
15490 such are defined in output sections, even those
15491 defined by simple assignment from a symbol defined in
15492 an input section. Transfer the symbol to an
15493 appropriate input .opd section, so that a branch to
15494 this symbol will be mapped to the location specified
15495 by the opd entry. */
15496 struct bfd_link_order *lo;
15497 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15498 if (lo->type == bfd_indirect_link_order)
15499 {
15500 asection *isec = lo->u.indirect.section;
15501 if (h_elf->root.u.def.value >= isec->output_offset
15502 && h_elf->root.u.def.value < (isec->output_offset
15503 + isec->size))
15504 {
15505 h_elf->root.u.def.value -= isec->output_offset;
15506 h_elf->root.u.def.section = isec;
15507 sec = isec;
15508 break;
15509 }
15510 }
15511 }
5bd4f169 15512 }
ed7007c1 15513 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15514
dbaa2011 15515 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15516 {
15517 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15518 input_bfd, input_section,
0930cb30 15519 contents, rel->r_offset);
c316a17c
AM
15520 wrel->r_offset = rel->r_offset;
15521 wrel->r_info = 0;
15522 wrel->r_addend = 0;
15523
15524 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15525 symbols defined in discarded sections. Not done for
c316a17c
AM
15526 non-debug to preserve relocs in .eh_frame which the
15527 eh_frame editing code expects to be present. */
15528 if (bfd_link_relocatable (info)
15529 && (input_section->flags & SEC_DEBUGGING))
15530 wrel--;
15531
15532 continue;
15533 }
ab96bf03 15534
0e1862bb 15535 if (bfd_link_relocatable (info))
c316a17c 15536 goto copy_reloc;
ab96bf03 15537
f40da81b
AM
15538 if (h != NULL && &h->elf == htab->elf.hgot)
15539 {
6f20ed8a 15540 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b 15541 sec = bfd_abs_section_ptr;
0a1b45a2 15542 unresolved_reloc = false;
f40da81b
AM
15543 }
15544
951fd09b
AM
15545 /* TLS optimizations. Replace instruction sequences and relocs
15546 based on information we collected in tls_optimize. We edit
15547 RELOCS so that --emit-relocs will output something sensible
15548 for the final instruction stream. */
15549 tls_mask = 0;
15550 tls_gd = 0;
0d4792f7 15551 toc_symndx = 0;
727fc41e
AM
15552 if (h != NULL)
15553 tls_mask = h->tls_mask;
15554 else if (local_got_ents != NULL)
411e1bfb 15555 {
e054468f
AM
15556 struct plt_entry **local_plt = (struct plt_entry **)
15557 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15558 unsigned char *lgot_masks = (unsigned char *)
e054468f 15559 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15560 tls_mask = lgot_masks[r_symndx];
15561 }
37da22e5 15562 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15563 && (r_type == R_PPC64_TLS
15564 || r_type == R_PPC64_TLSGD
15565 || r_type == R_PPC64_TLSLD))
15566 {
15567 /* Check for toc tls entries. */
f961d9dd 15568 unsigned char *toc_tls;
0d4792f7 15569
727fc41e
AM
15570 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15571 &local_syms, rel, input_bfd))
0a1b45a2 15572 return false;
0d4792f7 15573
727fc41e
AM
15574 if (toc_tls)
15575 tls_mask = *toc_tls;
0d4792f7
AM
15576 }
15577
15578 /* Check that tls relocs are used with tls syms, and non-tls
15579 relocs are used with non-tls syms. */
cf35638d 15580 if (r_symndx != STN_UNDEF
0d4792f7
AM
15581 && r_type != R_PPC64_NONE
15582 && (h == NULL
039b3fef
AM
15583 || h->elf.root.type == bfd_link_hash_defined
15584 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15585 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15586 {
37da22e5 15587 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15588 && (r_type == R_PPC64_TLS
15589 || r_type == R_PPC64_TLSGD
15590 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15591 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15592 ;
15593 else
25f53a85 15594 info->callbacks->einfo
1d483afe 15595 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15596 /* xgettext:c-format */
c1c8c1ef 15597 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15598 /* xgettext:c-format */
c1c8c1ef 15599 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15600 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15601 ppc64_elf_howto_table[r_type]->name,
15602 sym_name);
411e1bfb
AM
15603 }
15604
15605 /* Ensure reloc mapping code below stays sane. */
15606 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15607 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15608 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15609 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15610 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15611 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15612 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15613 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15614 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15615 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15616 abort ();
0d4792f7 15617
411e1bfb
AM
15618 switch (r_type)
15619 {
15620 default:
411e1bfb
AM
15621 break;
15622
ba761f19 15623 case R_PPC64_LO_DS_OPT:
733ae98c
AM
15624 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15625 {
15626 insn = bfd_get_32 (input_bfd,
15627 contents + rel->r_offset - d_offset);
15628 if ((insn & (0x3fu << 26)) != 58u << 26)
15629 abort ();
15630 insn += (14u << 26) - (58u << 26);
15631 bfd_put_32 (input_bfd, insn,
15632 contents + rel->r_offset - d_offset);
15633 r_type = R_PPC64_TOC16_LO;
15634 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15635 }
ba761f19
AM
15636 break;
15637
411e1bfb
AM
15638 case R_PPC64_TOC16:
15639 case R_PPC64_TOC16_LO:
15640 case R_PPC64_TOC16_DS:
15641 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15642 {
15643 /* Check for toc tls entries. */
f961d9dd 15644 unsigned char *toc_tls;
951fd09b 15645 int retval;
411e1bfb 15646
3a71aa26
AM
15647 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15648 &local_syms, rel, input_bfd);
951fd09b 15649 if (retval == 0)
0a1b45a2 15650 return false;
411e1bfb
AM
15651
15652 if (toc_tls)
15653 {
951fd09b 15654 tls_mask = *toc_tls;
411e1bfb
AM
15655 if (r_type == R_PPC64_TOC16_DS
15656 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15657 {
37da22e5 15658 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15659 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15660 goto toctprel;
15661 }
411e1bfb 15662 else
951fd09b
AM
15663 {
15664 /* If we found a GD reloc pair, then we might be
15665 doing a GD->IE transition. */
15666 if (retval == 2)
15667 {
b00a0a86 15668 tls_gd = TLS_GDIE;
37da22e5
AM
15669 if ((tls_mask & TLS_TLS) != 0
15670 && (tls_mask & TLS_GD) == 0)
102890f0 15671 goto tls_ldgd_opt;
951fd09b
AM
15672 }
15673 else if (retval == 3)
15674 {
37da22e5
AM
15675 if ((tls_mask & TLS_TLS) != 0
15676 && (tls_mask & TLS_LD) == 0)
102890f0 15677 goto tls_ldgd_opt;
951fd09b
AM
15678 }
15679 }
411e1bfb
AM
15680 }
15681 }
15682 break;
15683
9d6ded02
AM
15684 case R_PPC64_GOT_TPREL16_HI:
15685 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15686 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15687 && (tls_mask & TLS_TPREL) == 0
15688 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
9d6ded02
AM
15689 {
15690 rel->r_offset -= d_offset;
95f0d0d2 15691 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15692 r_type = R_PPC64_NONE;
15693 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15694 }
15695 break;
15696
411e1bfb
AM
15697 case R_PPC64_GOT_TPREL16_DS:
15698 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15699 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15700 && (tls_mask & TLS_TPREL) == 0
15701 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
411e1bfb 15702 {
81407a69 15703 toctprel:
95f0d0d2 15704 insn = bfd_get_32 (input_bfd,
c316a17c 15705 contents + rel->r_offset - d_offset);
411e1bfb
AM
15706 insn &= 31 << 21;
15707 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15708 bfd_put_32 (input_bfd, insn,
c316a17c 15709 contents + rel->r_offset - d_offset);
411e1bfb 15710 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15711 if (toc_symndx != 0)
15712 {
15713 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15714 rel->r_addend = toc_addend;
0d4792f7
AM
15715 /* We changed the symbol. Start over in order to
15716 get h, sym, sec etc. right. */
c316a17c 15717 goto again;
0d4792f7
AM
15718 }
15719 else
15720 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15721 }
15722 break;
15723
87c69f97 15724 case R_PPC64_GOT_TPREL_PCREL34:
c213164a 15725 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15726 && (tls_mask & TLS_TPREL) == 0
15727 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15728 {
15729 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15730 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15731 pinsn <<= 32;
15732 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15733 pinsn += ((2ULL << 56) + (-1ULL << 52)
15734 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15735 bfd_put_32 (input_bfd, pinsn >> 32,
15736 contents + rel->r_offset);
15737 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15738 contents + rel->r_offset + 4);
15739 r_type = R_PPC64_TPREL34;
15740 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15741 }
15742 break;
15743
411e1bfb 15744 case R_PPC64_TLS:
37da22e5 15745 if ((tls_mask & TLS_TLS) != 0
733ae98c
AM
15746 && (tls_mask & TLS_TPREL) == 0
15747 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15748 {
c213164a 15749 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15750 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15751 if (insn == 0)
c213164a
AM
15752 break;
15753 if ((rel->r_offset & 3) == 0)
0d4792f7 15754 {
c213164a
AM
15755 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15756 /* Was PPC64_TLS which sits on insn boundary, now
15757 PPC64_TPREL16_LO which is at low-order half-word. */
15758 rel->r_offset += d_offset;
15759 r_type = R_PPC64_TPREL16_LO;
15760 if (toc_symndx != 0)
15761 {
15762 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15763 rel->r_addend = toc_addend;
15764 /* We changed the symbol. Start over in order to
15765 get h, sym, sec etc. right. */
15766 goto again;
15767 }
15768 else
15769 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15770 }
15771 else if ((rel->r_offset & 3) == 1)
15772 {
15773 /* For pcrel IE to LE we already have the full
15774 offset and thus don't need an addi here. A nop
15775 or mr will do. */
2365f8d7 15776 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15777 {
15778 /* Extract regs from addi rt,ra,si. */
15779 unsigned int rt = (insn >> 21) & 0x1f;
15780 unsigned int ra = (insn >> 16) & 0x1f;
15781 if (rt == ra)
15782 insn = NOP;
15783 else
15784 {
15785 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15786 insn = (rt << 16) | (ra << 21) | (ra << 11);
15787 insn |= (31u << 26) | (444u << 1);
15788 }
15789 }
15790 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15791 }
411e1bfb
AM
15792 }
15793 break;
15794
411e1bfb
AM
15795 case R_PPC64_GOT_TLSGD16_HI:
15796 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15797 tls_gd = TLS_GDIE;
733ae98c
AM
15798 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15799 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b
AM
15800 goto tls_gdld_hi;
15801 break;
15802
411e1bfb
AM
15803 case R_PPC64_GOT_TLSLD16_HI:
15804 case R_PPC64_GOT_TLSLD16_HA:
733ae98c
AM
15805 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15806 && offset_in_range (input_section, rel->r_offset & ~3, 4))
411e1bfb 15807 {
951fd09b
AM
15808 tls_gdld_hi:
15809 if ((tls_mask & tls_gd) != 0)
15810 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15811 + R_PPC64_GOT_TPREL16_DS);
15812 else
411e1bfb 15813 {
4fe5ca5b 15814 rel->r_offset -= d_offset;
95f0d0d2 15815 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15816 r_type = R_PPC64_NONE;
411e1bfb 15817 }
951fd09b 15818 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15819 }
15820 break;
15821
951fd09b
AM
15822 case R_PPC64_GOT_TLSGD16:
15823 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15824 tls_gd = TLS_GDIE;
733ae98c
AM
15825 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15826 && offset_in_range (input_section, rel->r_offset & ~3, 4))
102890f0 15827 goto tls_ldgd_opt;
951fd09b 15828 break;
411e1bfb 15829
951fd09b
AM
15830 case R_PPC64_GOT_TLSLD16:
15831 case R_PPC64_GOT_TLSLD16_LO:
733ae98c
AM
15832 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15833 && offset_in_range (input_section, rel->r_offset & ~3, 4))
951fd09b 15834 {
b9f04fe0 15835 unsigned int insn1, insn2;
102890f0
AM
15836
15837 tls_ldgd_opt:
727fc41e
AM
15838 offset = (bfd_vma) -1;
15839 /* If not using the newer R_PPC64_TLSGD/LD to mark
15840 __tls_get_addr calls, we must trust that the call
15841 stays with its arg setup insns, ie. that the next
15842 reloc is the __tls_get_addr call associated with
15843 the current reloc. Edit both insns. */
9737e8af 15844 if (input_section->nomark_tls_get_addr
727fc41e
AM
15845 && rel + 1 < relend
15846 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15847 htab->tls_get_addr_fd,
15848 htab->tga_desc_fd,
727fc41e 15849 htab->tls_get_addr,
9e7028aa 15850 htab->tga_desc))
727fc41e 15851 offset = rel[1].r_offset;
b86ac8e3
AM
15852 /* We read the low GOT_TLS (or TOC16) insn because we
15853 need to keep the destination reg. It may be
15854 something other than the usual r3, and moved to r3
15855 before the call by intervening code. */
95f0d0d2 15856 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15857 contents + rel->r_offset - d_offset);
102890f0 15858 if ((tls_mask & tls_gd) != 0)
411e1bfb 15859 {
102890f0 15860 /* IE */
b86ac8e3 15861 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15862 insn1 |= 58u << 26; /* ld */
102890f0 15863 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15864 if (offset != (bfd_vma) -1)
f58d5a2d 15865 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15866 if (r_type == R_PPC64_TOC16
15867 || r_type == R_PPC64_TOC16_LO)
102890f0 15868 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15869 else
15870 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15871 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15872 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15873 }
15874 else
15875 {
15876 /* LE */
b86ac8e3
AM
15877 insn1 &= 0x1f << 21;
15878 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15879 insn2 = 0x38630000; /* addi 3,3,0 */
15880 if (tls_gd == 0)
951fd09b 15881 {
102890f0 15882 /* Was an LD reloc. */
71c4e95a 15883 r_symndx = STN_UNDEF;
102890f0 15884 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15885 }
102890f0 15886 else if (toc_symndx != 0)
3a71aa26
AM
15887 {
15888 r_symndx = toc_symndx;
15889 rel->r_addend = toc_addend;
15890 }
102890f0
AM
15891 r_type = R_PPC64_TPREL16_HA;
15892 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15893 if (offset != (bfd_vma) -1)
15894 {
15895 rel[1].r_info = ELF64_R_INFO (r_symndx,
15896 R_PPC64_TPREL16_LO);
15897 rel[1].r_offset = offset + d_offset;
15898 rel[1].r_addend = rel->r_addend;
15899 }
102890f0 15900 }
95f0d0d2 15901 bfd_put_32 (input_bfd, insn1,
3a71aa26 15902 contents + rel->r_offset - d_offset);
733ae98c
AM
15903 if (offset != (bfd_vma) -1
15904 && offset_in_range (input_section, offset, 4))
c96e0573
AM
15905 {
15906 bfd_put_32 (input_bfd, insn2, contents + offset);
733ae98c 15907 if (offset_in_range (input_section, offset + 4, 4))
c96e0573
AM
15908 {
15909 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15910 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15911 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15912 }
15913 }
727fc41e
AM
15914 if ((tls_mask & tls_gd) == 0
15915 && (tls_gd == 0 || toc_symndx != 0))
15916 {
15917 /* We changed the symbol. Start over in order
15918 to get h, sym, sec etc. right. */
c316a17c 15919 goto again;
727fc41e
AM
15920 }
15921 }
15922 break;
15923
87c69f97 15924 case R_PPC64_GOT_TLSGD_PCREL34:
733ae98c
AM
15925 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15926 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15927 {
15928 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15929 pinsn <<= 32;
15930 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15931 if ((tls_mask & TLS_GDIE) != 0)
15932 {
15933 /* IE, pla -> pld */
15934 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15935 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15936 }
15937 else
15938 {
15939 /* LE, pla pcrel -> paddi r13 */
15940 pinsn += (-1ULL << 52) + (13ULL << 16);
15941 r_type = R_PPC64_TPREL34;
15942 }
15943 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15944 bfd_put_32 (input_bfd, pinsn >> 32,
15945 contents + rel->r_offset);
15946 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15947 contents + rel->r_offset + 4);
15948 }
15949 break;
15950
87c69f97 15951 case R_PPC64_GOT_TLSLD_PCREL34:
733ae98c
AM
15952 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15953 && offset_in_range (input_section, rel->r_offset, 8))
c213164a
AM
15954 {
15955 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15956 pinsn <<= 32;
15957 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15958 pinsn += (-1ULL << 52) + (13ULL << 16);
15959 bfd_put_32 (input_bfd, pinsn >> 32,
15960 contents + rel->r_offset);
15961 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15962 contents + rel->r_offset + 4);
15963 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15964 r_symndx = STN_UNDEF;
15965 r_type = R_PPC64_TPREL34;
15966 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15967 goto again;
15968 }
15969 break;
15970
727fc41e 15971 case R_PPC64_TLSGD:
37da22e5 15972 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
733ae98c
AM
15973 && rel + 1 < relend
15974 && offset_in_range (input_section, rel->r_offset,
15975 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15976 ? 8 : 4))
727fc41e 15977 {
b9f04fe0 15978 unsigned int insn2;
5663e321 15979 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15980
4a421c53 15981 offset = rel->r_offset;
5663e321 15982 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15983 {
15984 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15985 if (r_type1 == R_PPC64_PLT_PCREL34
15986 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15987 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15988 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15989 break;
15990 }
15991
733ae98c 15992 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
15993 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15994
b00a0a86 15995 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
15996 {
15997 /* IE */
15998 r_type = R_PPC64_NONE;
15999 insn2 = 0x7c636a14; /* add 3,3,13 */
16000 }
16001 else
16002 {
16003 /* LE */
16004 if (toc_symndx != 0)
16005 {
16006 r_symndx = toc_symndx;
16007 rel->r_addend = toc_addend;
16008 }
c213164a 16009 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16010 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16011 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16012 {
16013 r_type = R_PPC64_NONE;
16014 insn2 = NOP;
16015 }
16016 else
16017 {
16018 rel->r_offset = offset + d_offset;
16019 r_type = R_PPC64_TPREL16_LO;
16020 insn2 = 0x38630000; /* addi 3,3,0 */
16021 }
727fc41e
AM
16022 }
16023 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16024 /* Zap the reloc on the _tls_get_addr call too. */
16025 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16026 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16027 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16028 if ((tls_mask & TLS_GDIE) == 0
16029 && toc_symndx != 0
16030 && r_type != R_PPC64_NONE)
c316a17c 16031 goto again;
411e1bfb 16032 }
411e1bfb
AM
16033 break;
16034
727fc41e 16035 case R_PPC64_TLSLD:
37da22e5 16036 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
733ae98c
AM
16037 && rel + 1 < relend
16038 && offset_in_range (input_section, rel->r_offset,
16039 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16040 ? 8 : 4))
727fc41e 16041 {
b9f04fe0 16042 unsigned int insn2;
5663e321 16043 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 16044
4a421c53 16045 offset = rel->r_offset;
5663e321 16046 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
16047 {
16048 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
16049 if (r_type1 == R_PPC64_PLT_PCREL34
16050 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16051 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
16052 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16053 break;
16054 }
16055
733ae98c 16056 if (r_type1 == R_PPC64_PLTCALL)
23cedd1d
AM
16057 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16058
c213164a 16059 if (r_type1 == R_PPC64_REL24_NOTOC
7aba54da 16060 || r_type1 == R_PPC64_REL24_P9NOTOC
c213164a
AM
16061 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16062 {
16063 r_type = R_PPC64_NONE;
16064 insn2 = NOP;
16065 }
16066 else
16067 {
16068 rel->r_offset = offset + d_offset;
16069 r_symndx = STN_UNDEF;
16070 r_type = R_PPC64_TPREL16_LO;
16071 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16072 insn2 = 0x38630000; /* addi 3,3,0 */
16073 }
727fc41e 16074 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
16075 /* Zap the reloc on the _tls_get_addr call too. */
16076 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 16077 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 16078 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
16079 if (r_type != R_PPC64_NONE)
16080 goto again;
727fc41e
AM
16081 }
16082 break;
16083
411e1bfb 16084 case R_PPC64_DTPMOD64:
951fd09b
AM
16085 if (rel + 1 < relend
16086 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16087 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 16088 {
733ae98c
AM
16089 if ((tls_mask & TLS_GD) == 0
16090 && offset_in_range (input_section, rel->r_offset, 8))
951fd09b
AM
16091 {
16092 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 16093 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
16094 r_type = R_PPC64_TPREL64;
16095 else
16096 {
4ce794b7 16097 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
16098 r_type = R_PPC64_NONE;
16099 }
16100 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16101 }
16102 }
16103 else
16104 {
733ae98c
AM
16105 if ((tls_mask & TLS_LD) == 0
16106 && offset_in_range (input_section, rel->r_offset, 8))
411e1bfb 16107 {
4ce794b7 16108 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 16109 r_type = R_PPC64_NONE;
951fd09b 16110 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 16111 }
411e1bfb
AM
16112 }
16113 break;
16114
16115 case R_PPC64_TPREL64:
951fd09b 16116 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
16117 {
16118 r_type = R_PPC64_NONE;
16119 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16120 }
16121 break;
52a82034 16122
006589cf
AM
16123 case R_PPC64_ENTRY:
16124 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16125 if (!bfd_link_pic (info)
16126 && !info->traditional_format
733ae98c
AM
16127 && relocation + 0x80008000 <= 0xffffffff
16128 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16129 {
16130 unsigned int insn1, insn2;
16131
16132 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16133 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16134 if ((insn1 & ~0xfffc) == LD_R2_0R12
16135 && insn2 == ADD_R2_R2_R12)
16136 {
95f0d0d2 16137 bfd_put_32 (input_bfd,
006589cf
AM
16138 LIS_R2 + PPC_HA (relocation),
16139 contents + rel->r_offset);
95f0d0d2 16140 bfd_put_32 (input_bfd,
006589cf
AM
16141 ADDI_R2_R2 + PPC_LO (relocation),
16142 contents + rel->r_offset + 4);
16143 }
16144 }
16145 else
16146 {
16147 relocation -= (rel->r_offset
16148 + input_section->output_offset
16149 + input_section->output_section->vma);
733ae98c
AM
16150 if (relocation + 0x80008000 <= 0xffffffff
16151 && offset_in_range (input_section, rel->r_offset, 8))
006589cf
AM
16152 {
16153 unsigned int insn1, insn2;
16154
16155 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16156 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16157 if ((insn1 & ~0xfffc) == LD_R2_0R12
16158 && insn2 == ADD_R2_R2_R12)
16159 {
95f0d0d2 16160 bfd_put_32 (input_bfd,
006589cf
AM
16161 ADDIS_R2_R12 + PPC_HA (relocation),
16162 contents + rel->r_offset);
95f0d0d2 16163 bfd_put_32 (input_bfd,
006589cf
AM
16164 ADDI_R2_R2 + PPC_LO (relocation),
16165 contents + rel->r_offset + 4);
16166 }
16167 }
16168 }
16169 break;
16170
52a82034
AM
16171 case R_PPC64_REL16_HA:
16172 /* If we are generating a non-PIC executable, edit
16173 . 0: addis 2,12,.TOC.-0b@ha
16174 . addi 2,2,.TOC.-0b@l
16175 used by ELFv2 global entry points to set up r2, to
16176 . lis 2,.TOC.@ha
16177 . addi 2,2,.TOC.@l
16178 if .TOC. is in range. */
0e1862bb 16179 if (!bfd_link_pic (info)
810d4e75 16180 && !info->traditional_format
006589cf 16181 && !htab->opd_abi
4f038ee5 16182 && rel->r_addend == d_offset
52a82034
AM
16183 && h != NULL && &h->elf == htab->elf.hgot
16184 && rel + 1 < relend
16185 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16186 && rel[1].r_offset == rel->r_offset + 4
16187 && rel[1].r_addend == rel->r_addend + 4
733ae98c
AM
16188 && relocation + 0x80008000 <= 0xffffffff
16189 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
52a82034
AM
16190 {
16191 unsigned int insn1, insn2;
4a421c53 16192 offset = rel->r_offset - d_offset;
95f0d0d2
AM
16193 insn1 = bfd_get_32 (input_bfd, contents + offset);
16194 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
16195 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16196 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
16197 {
16198 r_type = R_PPC64_ADDR16_HA;
16199 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16200 rel->r_addend -= d_offset;
16201 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16202 rel[1].r_addend -= d_offset + 4;
95f0d0d2 16203 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
16204 }
16205 }
16206 break;
411e1bfb
AM
16207 }
16208
16209 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 16210 insn = 0;
b25116a9
AM
16211 max_br_offset = 1 << 25;
16212 addend = rel->r_addend;
bc30df16 16213 reloc_dest = DEST_NORMAL;
65f38f15 16214 switch (r_type)
5bd4f169
AM
16215 {
16216 default:
65f38f15 16217 break;
5bd4f169 16218
3b421ab3
AM
16219 case R_PPC64_TOCSAVE:
16220 if (relocation + addend == (rel->r_offset
16221 + input_section->output_offset
16222 + input_section->output_section->vma)
16223 && tocsave_find (htab, NO_INSERT,
733ae98c
AM
16224 &local_syms, rel, input_bfd)
16225 && offset_in_range (input_section, rel->r_offset, 4))
3b421ab3
AM
16226 {
16227 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16228 if (insn == NOP
16229 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
16230 bfd_put_32 (input_bfd,
16231 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
16232 contents + rel->r_offset);
16233 }
16234 break;
16235
65f38f15
AM
16236 /* Branch taken prediction relocations. */
16237 case R_PPC64_ADDR14_BRTAKEN:
16238 case R_PPC64_REL14_BRTAKEN:
cedb70c5 16239 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 16240 /* Fall through. */
65f38f15 16241
86c76c7b 16242 /* Branch not taken prediction relocations. */
65f38f15
AM
16243 case R_PPC64_ADDR14_BRNTAKEN:
16244 case R_PPC64_REL14_BRNTAKEN:
733ae98c
AM
16245 if (!offset_in_range (input_section, rel->r_offset, 4))
16246 break;
95f0d0d2 16247 insn |= bfd_get_32 (input_bfd,
411e1bfb 16248 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 16249 /* Fall through. */
86c76c7b 16250
b25116a9
AM
16251 case R_PPC64_REL14:
16252 max_br_offset = 1 << 15;
1a0670f3 16253 /* Fall through. */
5bd4f169 16254
65f38f15 16255 case R_PPC64_REL24:
05d0e962 16256 case R_PPC64_REL24_NOTOC:
7aba54da 16257 case R_PPC64_REL24_P9NOTOC:
23cedd1d 16258 case R_PPC64_PLTCALL:
5663e321 16259 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
16260 /* Calls to functions with a different TOC, such as calls to
16261 shared objects, need to alter the TOC pointer. This is
16262 done using a linkage stub. A REL24 branching to these
16263 linkage stubs needs to be followed by a nop, as the nop
16264 will be replaced with an instruction to restore the TOC
16265 base pointer. */
8387904d 16266 fdh = h;
b31867b6
AM
16267 if (h != NULL
16268 && h->oh != NULL
16269 && h->oh->is_func_descriptor)
16270 fdh = ppc_follow_link (h->oh);
31c76678
DK
16271 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16272 htab);
5663e321
AM
16273 if ((r_type == R_PPC64_PLTCALL
16274 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 16275 && stub_entry != NULL
7aba54da 16276 && stub_entry->type.main == ppc_stub_plt_call)
23cedd1d
AM
16277 stub_entry = NULL;
16278
6abec6bc 16279 if (stub_entry != NULL
7aba54da
AM
16280 && (stub_entry->type.main == ppc_stub_plt_call
16281 || stub_entry->type.r2save))
41bd81ab 16282 {
0a1b45a2 16283 bool can_plt_call = false;
721956f4 16284
7aba54da
AM
16285 if (r_type == R_PPC64_REL24_NOTOC
16286 || r_type == R_PPC64_REL24_P9NOTOC)
6e1816be 16287 {
7aba54da 16288 /* NOTOC calls don't need to restore r2. */
0a1b45a2 16289 can_plt_call = true;
6e1816be 16290 }
7aba54da
AM
16291 else if (stub_entry->type.main == ppc_stub_plt_call
16292 && !htab->opd_abi
16293 && htab->params->plt_localentry0 != 0
16294 && h != NULL
16295 && is_elfv2_localentry0 (&h->elf))
05d0e962 16296 {
7aba54da 16297 /* The function doesn't use or change r2. */
0a1b45a2 16298 can_plt_call = true;
05d0e962 16299 }
6e1816be 16300
f378ab09 16301 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
16302 branch and link followed by a nop. The nop is
16303 replaced by an insn to restore r2. */
733ae98c 16304 else if (offset_in_range (input_section, rel->r_offset, 8))
41bd81ab 16305 {
ba8ca3e7
AM
16306 unsigned long br;
16307
16308 br = bfd_get_32 (input_bfd,
16309 contents + rel->r_offset);
16310 if ((br & 1) != 0)
41bd81ab 16311 {
ba8ca3e7
AM
16312 unsigned long nop;
16313
16314 nop = bfd_get_32 (input_bfd,
16315 contents + rel->r_offset + 4);
23cedd1d 16316 if (nop == LD_R2_0R1 + STK_TOC (htab))
0a1b45a2 16317 can_plt_call = true;
23cedd1d
AM
16318 else if (nop == NOP
16319 || nop == CROR_151515
16320 || nop == CROR_313131)
a7f2871e 16321 {
ba8ca3e7 16322 if (h != NULL
ed7007c1 16323 && is_tls_get_addr (&h->elf, htab)
7c9cf415 16324 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
16325 {
16326 /* Special stub used, leave nop alone. */
16327 }
16328 else
a078d95a
AM
16329 bfd_put_32 (input_bfd,
16330 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7 16331 contents + rel->r_offset + 4);
0a1b45a2 16332 can_plt_call = true;
a7f2871e 16333 }
41bd81ab 16334 }
5bd4f169 16335 }
721956f4 16336
ba8ca3e7 16337 if (!can_plt_call && h != NULL)
721956f4 16338 {
ba8ca3e7
AM
16339 const char *name = h->elf.root.root.string;
16340
16341 if (*name == '.')
16342 ++name;
16343
3f3328b8 16344 if (startswith (name, "__libc_start_main")
ba8ca3e7 16345 && (name[17] == 0 || name[17] == '@'))
6ab189d5 16346 {
ba8ca3e7
AM
16347 /* Allow crt1 branch to go via a toc adjusting
16348 stub. Other calls that never return could do
16349 the same, if we could detect such. */
0a1b45a2 16350 can_plt_call = true;
6ab189d5 16351 }
ba8ca3e7
AM
16352 }
16353
16354 if (!can_plt_call)
16355 {
16356 /* g++ as of 20130507 emits self-calls without a
16357 following nop. This is arguably wrong since we
16358 have conflicting information. On the one hand a
16359 global symbol and on the other a local call
16360 sequence, but don't error for this special case.
16361 It isn't possible to cheaply verify we have
16362 exactly such a call. Allow all calls to the same
16363 section. */
16364 asection *code_sec = sec;
16365
16366 if (get_opd_info (sec) != NULL)
ad8e1ba5 16367 {
ba8ca3e7
AM
16368 bfd_vma off = (relocation + addend
16369 - sec->output_section->vma
16370 - sec->output_offset);
bc30df16 16371
0a1b45a2 16372 opd_entry_value (sec, off, &code_sec, NULL, false);
ad8e1ba5 16373 }
ba8ca3e7 16374 if (code_sec == input_section)
0a1b45a2 16375 can_plt_call = true;
ba8ca3e7
AM
16376 }
16377
16378 if (!can_plt_call)
16379 {
7aba54da 16380 if (stub_entry->type.main == ppc_stub_plt_call)
4805fc55 16381 info->callbacks->einfo
695344c0 16382 /* xgettext:c-format */
c1c8c1ef 16383 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16384 "(plt call stub)\n"),
4805fc55
AM
16385 input_bfd, input_section, rel->r_offset, sym_name);
16386 else
16387 info->callbacks->einfo
695344c0 16388 /* xgettext:c-format */
c1c8c1ef 16389 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 16390 "(toc save/adjust stub)\n"),
4805fc55 16391 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
16392
16393 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16394 ret = false;
721956f4
AM
16395 }
16396
b25116a9 16397 if (can_plt_call
7aba54da 16398 && stub_entry->type.main == ppc_stub_plt_call)
0a1b45a2 16399 unresolved_reloc = false;
b25116a9
AM
16400 }
16401
6abec6bc 16402 if ((stub_entry == NULL
7aba54da
AM
16403 || stub_entry->type.main == ppc_stub_long_branch
16404 || stub_entry->type.main == ppc_stub_plt_branch)
8387904d
AM
16405 && get_opd_info (sec) != NULL)
16406 {
16407 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
16408 bfd_vma off = (relocation + addend
16409 - sec->output_section->vma
16410 - sec->output_offset);
0a1b45a2 16411 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
8387904d
AM
16412 if (dest != (bfd_vma) -1)
16413 {
16414 relocation = dest;
16415 addend = 0;
bc30df16 16416 reloc_dest = DEST_OPD;
8387904d
AM
16417 }
16418 }
16419
b25116a9
AM
16420 /* If the branch is out of reach we ought to have a long
16421 branch stub. */
16422 from = (rel->r_offset
16423 + input_section->output_offset
16424 + input_section->output_section->vma);
16425
6911b7dc
AM
16426 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16427 ? fdh->elf.other
16428 : sym->st_other);
16429
6abec6bc 16430 if (stub_entry != NULL
7aba54da
AM
16431 && (stub_entry->type.main == ppc_stub_long_branch
16432 || stub_entry->type.main == ppc_stub_plt_branch))
16433 {
16434 if (stub_entry->type.sub == ppc_stub_toc
16435 && !stub_entry->type.r2save
16436 && (r_type == R_PPC64_ADDR14_BRTAKEN
16437 || r_type == R_PPC64_ADDR14_BRNTAKEN
16438 || (relocation + addend - from + max_br_offset
16439 < 2 * max_br_offset)))
16440 /* Don't use the stub if this branch is in range. */
16441 stub_entry = NULL;
16442
16443 if (stub_entry != NULL
16444 && stub_entry->type.sub >= ppc_stub_notoc
16445 && ((r_type != R_PPC64_REL24_NOTOC
16446 && r_type != R_PPC64_REL24_P9NOTOC)
16447 || ((fdh ? fdh->elf.other : sym->st_other)
16448 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16449 && (relocation + addend - from + max_br_offset
16450 < 2 * max_br_offset))
16451 stub_entry = NULL;
16452
16453 if (stub_entry != NULL
16454 && stub_entry->type.r2save
16455 && (r_type == R_PPC64_REL24_NOTOC
16456 || r_type == R_PPC64_REL24_P9NOTOC)
16457 && (relocation + addend - from + max_br_offset
16458 < 2 * max_br_offset))
16459 stub_entry = NULL;
16460 }
05d0e962 16461
b25116a9
AM
16462 if (stub_entry != NULL)
16463 {
16464 /* Munge up the value and addend so that we call the stub
16465 rather than the procedure directly. */
a4b6fadd
AM
16466 asection *stub_sec = stub_entry->group->stub_sec;
16467
7aba54da 16468 if (stub_entry->type.main == ppc_stub_save_res)
a4b6fadd
AM
16469 relocation += (stub_sec->output_offset
16470 + stub_sec->output_section->vma
16471 + stub_sec->size - htab->sfpr->size
16472 - htab->sfpr->output_offset
16473 - htab->sfpr->output_section->vma);
16474 else
16475 relocation = (stub_entry->stub_offset
16476 + stub_sec->output_offset
16477 + stub_sec->output_section->vma);
b25116a9 16478 addend = 0;
bc30df16 16479 reloc_dest = DEST_STUB;
3b421ab3 16480
7aba54da
AM
16481 if (((stub_entry->type.r2save
16482 && (r_type == R_PPC64_REL24_NOTOC
16483 || r_type == R_PPC64_REL24_P9NOTOC))
16484 || ((stub_entry->type.main == ppc_stub_plt_call
16485 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16486 && rel + 1 < relend
16487 && rel[1].r_offset == rel->r_offset + 4
16488 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16489 && !(stub_entry->type.main == ppc_stub_plt_call
16490 && htab->params->tls_get_addr_opt
16491 && h != NULL
16492 && is_tls_get_addr (&h->elf, htab)))
29433886
AM
16493 {
16494 /* Skip over the r2 store at the start of the stub. */
7aba54da 16495 relocation += 4;
29433886 16496 }
5663e321 16497
7aba54da
AM
16498 if ((r_type == R_PPC64_REL24_NOTOC
16499 || r_type == R_PPC64_REL24_P9NOTOC)
16500 && stub_entry->type.main == ppc_stub_plt_call
16501 && stub_entry->type.sub >= ppc_stub_notoc)
5663e321 16502 htab->notoc_plt = 1;
b25116a9
AM
16503 }
16504
16505 if (insn != 0)
16506 {
794e51c0 16507 if (is_isa_v2)
721956f4 16508 {
b25116a9
AM
16509 /* Set 'a' bit. This is 0b00010 in BO field for branch
16510 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16511 for branch on CTR insns (BO == 1a00t or 1a01t). */
16512 if ((insn & (0x14 << 21)) == (0x04 << 21))
16513 insn |= 0x02 << 21;
16514 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16515 insn |= 0x08 << 21;
16516 else
16517 break;
16518 }
16519 else
16520 {
16521 /* Invert 'y' bit if not the default. */
4cc603a5 16522 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16523 insn ^= 0x01 << 21;
721956f4 16524 }
b25116a9 16525
95f0d0d2 16526 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16527 }
e86ce104 16528
06da1e8e
AM
16529 /* NOP out calls to undefined weak functions.
16530 We can thus call a weak function without first
16531 checking whether the function is defined. */
b25116a9 16532 else if (h != NULL
039b3fef 16533 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16534 && h->elf.dynindx == -1
05d0e962 16535 && (r_type == R_PPC64_REL24
7aba54da
AM
16536 || r_type == R_PPC64_REL24_NOTOC
16537 || r_type == R_PPC64_REL24_P9NOTOC)
b25116a9 16538 && relocation == 0
733ae98c
AM
16539 && addend == 0
16540 && offset_in_range (input_section, rel->r_offset, 4))
e86ce104 16541 {
95f0d0d2 16542 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16543 goto copy_reloc;
e86ce104 16544 }
65f38f15 16545 break;
066f4018
AM
16546
16547 case R_PPC64_GOT16_DS:
6738c8a7
AM
16548 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16549 || !htab->do_toc_opt)
06507dab 16550 break;
066f4018
AM
16551 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16552 if (relocation + addend - from + 0x8000 < 0x10000
f5b9c288
AM
16553 && sec != NULL
16554 && sec->output_section != NULL
16555 && !discarded_section (sec)
733ae98c
AM
16556 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16557 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16558 {
16559 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16560 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16561 {
16562 insn += (14u << 26) - (58u << 26);
16563 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16564 r_type = R_PPC64_TOC16;
16565 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16566 }
16567 }
16568 break;
16569
16570 case R_PPC64_GOT16_LO_DS:
16571 case R_PPC64_GOT16_HA:
6738c8a7
AM
16572 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16573 || !htab->do_toc_opt)
06507dab 16574 break;
066f4018
AM
16575 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16576 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
f5b9c288
AM
16577 && sec != NULL
16578 && sec->output_section != NULL
16579 && !discarded_section (sec)
733ae98c
AM
16580 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16581 && offset_in_range (input_section, rel->r_offset & ~3, 4))
066f4018
AM
16582 {
16583 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16584 if (r_type == R_PPC64_GOT16_LO_DS
16585 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16586 {
16587 insn += (14u << 26) - (58u << 26);
16588 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16589 r_type = R_PPC64_TOC16_LO;
16590 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16591 }
fba8689a
AM
16592 else if (r_type == R_PPC64_GOT16_HA
16593 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16594 {
16595 r_type = R_PPC64_TOC16_HA;
16596 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16597 }
16598 }
16599 break;
4a421c53
AM
16600
16601 case R_PPC64_GOT_PCREL34:
6738c8a7
AM
16602 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16603 || !htab->do_toc_opt)
06507dab 16604 break;
4a421c53
AM
16605 from = (rel->r_offset
16606 + input_section->output_section->vma
16607 + input_section->output_offset);
6738c8a7 16608 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
f5b9c288
AM
16609 && sec != NULL
16610 && sec->output_section != NULL
16611 && !discarded_section (sec)
733ae98c
AM
16612 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16613 && offset_in_range (input_section, rel->r_offset, 8)))
6738c8a7
AM
16614 break;
16615
16616 offset = rel->r_offset;
16617 pinsn = bfd_get_32 (input_bfd, contents + offset);
16618 pinsn <<= 32;
16619 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16620 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16621 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16622 break;
16623
16624 /* Replace with paddi. */
16625 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16626 r_type = R_PPC64_PCREL34;
16627 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16628 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16629 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16630 /* Fall through. */
4a421c53
AM
16631
16632 case R_PPC64_PCREL34:
6738c8a7
AM
16633 if (!htab->params->no_pcrel_opt
16634 && rel + 1 < relend
16635 && rel[1].r_offset == rel->r_offset
16636 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
733ae98c
AM
16637 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16638 && offset_in_range (input_section, rel->r_offset, 8))
4a421c53
AM
16639 {
16640 offset = rel->r_offset;
16641 pinsn = bfd_get_32 (input_bfd, contents + offset);
16642 pinsn <<= 32;
16643 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16644 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16645 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16646 | (14ULL << 26) /* paddi */))
16647 {
6738c8a7
AM
16648 bfd_vma off2 = rel[1].r_addend;
16649 if (off2 == 0)
16650 /* zero means next insn. */
16651 off2 = 8;
16652 off2 += offset;
733ae98c 16653 if (offset_in_range (input_section, off2, 4))
4a421c53 16654 {
6738c8a7
AM
16655 uint64_t pinsn2;
16656 bfd_signed_vma addend_off;
16657 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16658 pinsn2 <<= 32;
16659 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16660 {
733ae98c 16661 if (!offset_in_range (input_section, off2, 8))
6738c8a7
AM
16662 break;
16663 pinsn2 |= bfd_get_32 (input_bfd,
16664 contents + off2 + 4);
16665 }
16666 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16667 {
16668 addend += addend_off;
16669 rel->r_addend = addend;
16670 bfd_put_32 (input_bfd, pinsn >> 32,
16671 contents + offset);
16672 bfd_put_32 (input_bfd, pinsn,
16673 contents + offset + 4);
16674 bfd_put_32 (input_bfd, pinsn2 >> 32,
16675 contents + off2);
4a421c53 16676 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16677 bfd_put_32 (input_bfd, pinsn2,
16678 contents + off2 + 4);
4a421c53
AM
16679 }
16680 }
16681 }
16682 }
16683 break;
65f38f15 16684 }
5bd4f169 16685
411e1bfb 16686 tls_type = 0;
23cedd1d 16687 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16688 switch (r_type)
16689 {
16690 default:
cf97bcb0
AM
16691 /* xgettext:c-format */
16692 _bfd_error_handler (_("%pB: %s unsupported"),
16693 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16694
65f38f15 16695 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16696 ret = false;
c316a17c 16697 goto copy_reloc;
5bd4f169 16698
65f38f15 16699 case R_PPC64_NONE:
411e1bfb 16700 case R_PPC64_TLS:
727fc41e
AM
16701 case R_PPC64_TLSGD:
16702 case R_PPC64_TLSLD:
3b421ab3 16703 case R_PPC64_TOCSAVE:
04c9666a
AM
16704 case R_PPC64_GNU_VTINHERIT:
16705 case R_PPC64_GNU_VTENTRY:
006589cf 16706 case R_PPC64_ENTRY:
4a421c53 16707 case R_PPC64_PCREL_OPT:
c316a17c 16708 goto copy_reloc;
5bd4f169
AM
16709
16710 /* GOT16 relocations. Like an ADDR16 using the symbol's
16711 address in the GOT as relocation value instead of the
411e1bfb 16712 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16713 symbol and put the symbol value there. */
411e1bfb
AM
16714 case R_PPC64_GOT_TLSGD16:
16715 case R_PPC64_GOT_TLSGD16_LO:
16716 case R_PPC64_GOT_TLSGD16_HI:
16717 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16718 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16719 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16720 goto dogot;
16721
16722 case R_PPC64_GOT_TLSLD16:
16723 case R_PPC64_GOT_TLSLD16_LO:
16724 case R_PPC64_GOT_TLSLD16_HI:
16725 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16726 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16727 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16728 goto dogot;
16729
16730 case R_PPC64_GOT_TPREL16_DS:
16731 case R_PPC64_GOT_TPREL16_LO_DS:
16732 case R_PPC64_GOT_TPREL16_HI:
16733 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16734 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16735 tls_type = TLS_TLS | TLS_TPREL;
16736 goto dogot;
16737
16738 case R_PPC64_GOT_DTPREL16_DS:
16739 case R_PPC64_GOT_DTPREL16_LO_DS:
16740 case R_PPC64_GOT_DTPREL16_HI:
16741 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16742 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16743 tls_type = TLS_TLS | TLS_DTPREL;
16744 goto dogot;
16745
65f38f15
AM
16746 case R_PPC64_GOT16:
16747 case R_PPC64_GOT16_LO:
16748 case R_PPC64_GOT16_HI:
16749 case R_PPC64_GOT16_HA:
16750 case R_PPC64_GOT16_DS:
16751 case R_PPC64_GOT16_LO_DS:
5663e321 16752 case R_PPC64_GOT_PCREL34:
411e1bfb 16753 dogot:
5bd4f169
AM
16754 {
16755 /* Relocation is to the entry for this symbol in the global
16756 offset table. */
e717da7e 16757 asection *got;
d881513a 16758 bfd_vma *offp;
5bd4f169 16759 bfd_vma off;
d881513a 16760 unsigned long indx = 0;
927be08e 16761 struct got_entry *ent;
65f38f15 16762
d881513a 16763 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16764 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16765 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16766 else
5bd4f169 16767 {
d881513a
AM
16768 if (h != NULL)
16769 {
f0158f44
AM
16770 if (!htab->elf.dynamic_sections_created
16771 || h->elf.dynindx == -1
16772 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16773 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16774 /* This is actually a static link, or it is a
16775 -Bsymbolic link and the symbol is defined
16776 locally, or the symbol was forced to be local
16777 because of a version file. */
16778 ;
16779 else
16780 {
039b3fef 16781 indx = h->elf.dynindx;
0a1b45a2 16782 unresolved_reloc = false;
d881513a 16783 }
039b3fef 16784 ent = h->elf.got.glist;
d881513a 16785 }
411e1bfb 16786 else
5bd4f169 16787 {
d881513a
AM
16788 if (local_got_ents == NULL)
16789 abort ();
16790 ent = local_got_ents[r_symndx];
5bd4f169 16791 }
d881513a
AM
16792
16793 for (; ent != NULL; ent = ent->next)
133a1f60 16794 if (ent->addend == orig_rel.r_addend
e717da7e 16795 && ent->owner == input_bfd
d881513a
AM
16796 && ent->tls_type == tls_type)
16797 break;
5bd4f169 16798 }
411e1bfb 16799
927be08e
AM
16800 if (ent == NULL)
16801 abort ();
16802 if (ent->is_indirect)
16803 ent = ent->got.ent;
16804 offp = &ent->got.offset;
16805 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16806 if (got == NULL)
16807 abort ();
16808
411e1bfb
AM
16809 /* The offset must always be a multiple of 8. We use the
16810 least significant bit to record whether we have already
16811 processed this entry. */
d881513a 16812 off = *offp;
411e1bfb
AM
16813 if ((off & 1) != 0)
16814 off &= ~1;
5bd4f169
AM
16815 else
16816 {
411e1bfb
AM
16817 /* Generate relocs for the dynamic linker, except in
16818 the case of TLSLD where we'll use one entry per
16819 module. */
25f23106 16820 asection *relgot;
0a1b45a2 16821 bool ifunc;
e717da7e 16822
d881513a 16823 *offp = off | 1;
25f23106
AM
16824 relgot = NULL;
16825 ifunc = (h != NULL
16826 ? h->elf.type == STT_GNU_IFUNC
16827 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16828 if (ifunc)
82e66161
AM
16829 {
16830 relgot = htab->elf.irelplt;
efb2a7b4 16831 if (indx == 0 || is_static_defined (&h->elf))
0a1b45a2 16832 htab->elf.ifunc_resolvers = true;
82e66161 16833 }
f0158f44
AM
16834 else if (indx != 0
16835 || (bfd_link_pic (info)
16836 && (h == NULL
f749f26e
AM
16837 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16838 && !(tls_type != 0
f15d0b54 16839 && bfd_link_executable (info)
9e51d549
AM
16840 && (h == NULL
16841 || SYMBOL_REFERENCES_LOCAL (info,
16842 &h->elf)))))
19e08130 16843 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16844 if (relgot != NULL)
5bd4f169 16845 {
e717da7e
AM
16846 outrel.r_offset = (got->output_section->vma
16847 + got->output_offset
411e1bfb 16848 + off);
133a1f60 16849 outrel.r_addend = orig_rel.r_addend;
d881513a 16850 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16851 {
411e1bfb 16852 outrel.r_addend = 0;
e515b051 16853 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16854 if (tls_type == (TLS_TLS | TLS_GD))
16855 {
e717da7e
AM
16856 loc = relgot->contents;
16857 loc += (relgot->reloc_count++
d881513a
AM
16858 * sizeof (Elf64_External_Rela));
16859 bfd_elf64_swap_reloca_out (output_bfd,
16860 &outrel, loc);
e515b051 16861 outrel.r_offset += 8;
133a1f60 16862 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16863 outrel.r_info
16864 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16865 }
411e1bfb 16866 }
951fd09b 16867 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16868 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16869 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16870 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16871 else if (indx != 0)
16872 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16873 else
81407a69 16874 {
25f23106
AM
16875 if (ifunc)
16876 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16877 else
16878 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16879
16880 /* Write the .got section contents for the sake
16881 of prelink. */
e717da7e 16882 loc = got->contents + off;
23fbd6fa
JJ
16883 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16884 loc);
81407a69 16885 }
81407a69
AM
16886
16887 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16888 {
16889 outrel.r_addend += relocation;
16890 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16891 {
16892 if (htab->elf.tls_sec == NULL)
16893 outrel.r_addend = 0;
16894 else
16895 outrel.r_addend -= htab->elf.tls_sec->vma;
16896 }
e515b051 16897 }
1657026c
AM
16898 if (!(info->enable_dt_relr
16899 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
16900 {
16901 loc = relgot->contents;
16902 loc += (relgot->reloc_count++
16903 * sizeof (Elf64_External_Rela));
16904 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16905 }
411e1bfb
AM
16906 }
16907
ad8e1ba5 16908 /* Init the .got section contents here if we're not
81407a69 16909 emitting a reloc. */
d881513a 16910 else
411e1bfb 16911 {
133a1f60 16912 relocation += orig_rel.r_addend;
f0158f44 16913 if (tls_type != 0)
411e1bfb 16914 {
989f9879
AM
16915 if (htab->elf.tls_sec == NULL)
16916 relocation = 0;
16917 else
16918 {
f0158f44
AM
16919 if (tls_type & TLS_LD)
16920 relocation = 0;
16921 else
16922 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16923 if (tls_type & TLS_TPREL)
989f9879
AM
16924 relocation += DTP_OFFSET - TP_OFFSET;
16925 }
5bd4f169 16926
f0158f44 16927 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16928 {
16929 bfd_put_64 (output_bfd, relocation,
e717da7e 16930 got->contents + off + 8);
676ee2b5 16931 relocation = 1;
7b609f53 16932 }
411e1bfb
AM
16933 }
16934 bfd_put_64 (output_bfd, relocation,
e717da7e 16935 got->contents + off);
5bd4f169
AM
16936 }
16937 }
16938
65f38f15
AM
16939 if (off >= (bfd_vma) -2)
16940 abort ();
16941
bf102f86 16942 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16943 addend = 0;
c213164a 16944 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16945 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16946 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16947 || r_type == R_PPC64_GOT_TPREL_PCREL34
16948 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 16949 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 16950 }
65f38f15
AM
16951 break;
16952
16953 case R_PPC64_PLT16_HA:
16954 case R_PPC64_PLT16_HI:
16955 case R_PPC64_PLT16_LO:
08be3224 16956 case R_PPC64_PLT16_LO_DS:
5663e321
AM
16957 case R_PPC64_PLT_PCREL34:
16958 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
16959 case R_PPC64_PLT32:
16960 case R_PPC64_PLT64:
23cedd1d 16961 case R_PPC64_PLTSEQ:
5663e321 16962 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 16963 case R_PPC64_PLTCALL:
5663e321 16964 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
16965 /* Relocation is to the entry for this symbol in the
16966 procedure linkage table. */
0a1b45a2 16967 unresolved_reloc = true;
cbf95972
AM
16968 {
16969 struct plt_entry **plt_list = NULL;
16970 if (h != NULL)
16971 plt_list = &h->elf.plt.plist;
16972 else if (local_got_ents != NULL)
16973 {
16974 struct plt_entry **local_plt = (struct plt_entry **)
16975 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 16976 plt_list = local_plt + r_symndx;
cbf95972
AM
16977 }
16978 if (plt_list)
16979 {
16980 struct plt_entry *ent;
65f38f15 16981
cbf95972
AM
16982 for (ent = *plt_list; ent != NULL; ent = ent->next)
16983 if (ent->plt.offset != (bfd_vma) -1
133a1f60 16984 && ent->addend == orig_rel.r_addend)
cbf95972
AM
16985 {
16986 asection *plt;
08be3224 16987 bfd_vma got;
cbf95972
AM
16988
16989 plt = htab->elf.splt;
30845f11 16990 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
16991 {
16992 if (h != NULL
16993 ? h->elf.type == STT_GNU_IFUNC
16994 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16995 plt = htab->elf.iplt;
16996 else
16997 plt = htab->pltlocal;
16998 }
16999 relocation = (plt->output_section->vma
17000 + plt->output_offset
17001 + ent->plt.offset);
08be3224 17002 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
17003 || r_type == R_PPC64_PLT16_HI
17004 || r_type == R_PPC64_PLT16_LO
17005 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
17006 {
17007 got = (elf_gp (output_bfd)
17008 + htab->sec_info[input_section->id].toc_off);
17009 relocation -= got;
17010 }
133a1f60 17011 addend = 0;
0a1b45a2 17012 unresolved_reloc = false;
cbf95972
AM
17013 break;
17014 }
17015 }
17016 }
65f38f15 17017 break;
5bd4f169 17018
0b13192e
AM
17019 case R_PPC64_TOC:
17020 /* Relocation value is TOC base. */
17021 relocation = TOCstart;
cf35638d 17022 if (r_symndx == STN_UNDEF)
6f20ed8a 17023 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
17024 else if (unresolved_reloc)
17025 ;
6f20ed8a
AM
17026 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17027 relocation += htab->sec_info[sec->id].toc_off;
0b13192e 17028 else
0a1b45a2 17029 unresolved_reloc = true;
ab96bf03 17030 goto dodyn;
0b13192e 17031
5bd4f169
AM
17032 /* TOC16 relocs. We want the offset relative to the TOC base,
17033 which is the address of the start of the TOC plus 0x8000.
17034 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17035 in this order. */
65f38f15
AM
17036 case R_PPC64_TOC16:
17037 case R_PPC64_TOC16_LO:
17038 case R_PPC64_TOC16_HI:
17039 case R_PPC64_TOC16_DS:
17040 case R_PPC64_TOC16_LO_DS:
17041 case R_PPC64_TOC16_HA:
6f20ed8a 17042 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
17043 if (h != NULL)
17044 goto dodyn;
5bd4f169
AM
17045 break;
17046
17047 /* Relocate against the beginning of the section. */
65f38f15
AM
17048 case R_PPC64_SECTOFF:
17049 case R_PPC64_SECTOFF_LO:
17050 case R_PPC64_SECTOFF_HI:
17051 case R_PPC64_SECTOFF_DS:
17052 case R_PPC64_SECTOFF_LO_DS:
17053 case R_PPC64_SECTOFF_HA:
4ce794b7 17054 if (sec != NULL)
65f38f15 17055 addend -= sec->output_section->vma;
5bd4f169
AM
17056 break;
17057
25f23106
AM
17058 case R_PPC64_REL16:
17059 case R_PPC64_REL16_LO:
17060 case R_PPC64_REL16_HI:
17061 case R_PPC64_REL16_HA:
4a969973
AM
17062 case R_PPC64_REL16_HIGH:
17063 case R_PPC64_REL16_HIGHA:
17064 case R_PPC64_REL16_HIGHER:
17065 case R_PPC64_REL16_HIGHERA:
17066 case R_PPC64_REL16_HIGHEST:
17067 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
17068 case R_PPC64_REL16_HIGHER34:
17069 case R_PPC64_REL16_HIGHERA34:
17070 case R_PPC64_REL16_HIGHEST34:
17071 case R_PPC64_REL16_HIGHESTA34:
a680de9a 17072 case R_PPC64_REL16DX_HA:
721956f4
AM
17073 case R_PPC64_REL14:
17074 case R_PPC64_REL14_BRNTAKEN:
17075 case R_PPC64_REL14_BRTAKEN:
5d1634d7 17076 case R_PPC64_REL24:
05d0e962 17077 case R_PPC64_REL24_NOTOC:
7aba54da 17078 case R_PPC64_REL24_P9NOTOC:
5663e321
AM
17079 case R_PPC64_PCREL34:
17080 case R_PPC64_PCREL28:
5d1634d7
AM
17081 break;
17082
411e1bfb
AM
17083 case R_PPC64_TPREL16:
17084 case R_PPC64_TPREL16_LO:
17085 case R_PPC64_TPREL16_HI:
17086 case R_PPC64_TPREL16_HA:
17087 case R_PPC64_TPREL16_DS:
17088 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
17089 case R_PPC64_TPREL16_HIGH:
17090 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
17091 case R_PPC64_TPREL16_HIGHER:
17092 case R_PPC64_TPREL16_HIGHERA:
17093 case R_PPC64_TPREL16_HIGHEST:
17094 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
17095 if (h != NULL
17096 && h->elf.root.type == bfd_link_hash_undefweak
733ae98c
AM
17097 && h->elf.dynindx == -1
17098 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
766bc656
AM
17099 {
17100 /* Make this relocation against an undefined weak symbol
17101 resolve to zero. This is really just a tweak, since
17102 code using weak externs ought to check that they are
17103 defined before using them. */
17104 bfd_byte *p = contents + rel->r_offset - d_offset;
17105
95f0d0d2 17106 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
17107 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17108 if (insn != 0)
95f0d0d2 17109 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
17110 break;
17111 }
beaddc1a
AM
17112 /* Fall through. */
17113
17114 case R_PPC64_TPREL34:
989f9879
AM
17115 if (htab->elf.tls_sec != NULL)
17116 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
17117 /* The TPREL16 relocs shouldn't really be used in shared
17118 libs or with non-local symbols as that will result in
17119 DT_TEXTREL being set, but support them anyway. */
17120 goto dodyn;
411e1bfb
AM
17121
17122 case R_PPC64_DTPREL16:
17123 case R_PPC64_DTPREL16_LO:
17124 case R_PPC64_DTPREL16_HI:
17125 case R_PPC64_DTPREL16_HA:
17126 case R_PPC64_DTPREL16_DS:
17127 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
17128 case R_PPC64_DTPREL16_HIGH:
17129 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
17130 case R_PPC64_DTPREL16_HIGHER:
17131 case R_PPC64_DTPREL16_HIGHERA:
17132 case R_PPC64_DTPREL16_HIGHEST:
17133 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 17134 case R_PPC64_DTPREL34:
989f9879
AM
17135 if (htab->elf.tls_sec != NULL)
17136 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
17137 break;
17138
45965137
AM
17139 case R_PPC64_ADDR64_LOCAL:
17140 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17141 ? h->elf.other
17142 : sym->st_other);
17143 break;
17144
e515b051
AM
17145 case R_PPC64_DTPMOD64:
17146 relocation = 1;
17147 addend = 0;
17148 goto dodyn;
17149
411e1bfb 17150 case R_PPC64_TPREL64:
989f9879
AM
17151 if (htab->elf.tls_sec != NULL)
17152 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
17153 goto dodyn;
17154
17155 case R_PPC64_DTPREL64:
989f9879
AM
17156 if (htab->elf.tls_sec != NULL)
17157 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 17158 /* Fall through. */
411e1bfb 17159
65f38f15
AM
17160 /* Relocations that may need to be propagated if this is a
17161 dynamic object. */
04c9666a 17162 case R_PPC64_REL30:
65f38f15
AM
17163 case R_PPC64_REL32:
17164 case R_PPC64_REL64:
17165 case R_PPC64_ADDR14:
17166 case R_PPC64_ADDR14_BRNTAKEN:
17167 case R_PPC64_ADDR14_BRTAKEN:
17168 case R_PPC64_ADDR16:
17169 case R_PPC64_ADDR16_DS:
17170 case R_PPC64_ADDR16_HA:
17171 case R_PPC64_ADDR16_HI:
f9c6b907
AM
17172 case R_PPC64_ADDR16_HIGH:
17173 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17174 case R_PPC64_ADDR16_HIGHER:
17175 case R_PPC64_ADDR16_HIGHERA:
17176 case R_PPC64_ADDR16_HIGHEST:
17177 case R_PPC64_ADDR16_HIGHESTA:
17178 case R_PPC64_ADDR16_LO:
17179 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
17180 case R_PPC64_ADDR16_HIGHER34:
17181 case R_PPC64_ADDR16_HIGHERA34:
17182 case R_PPC64_ADDR16_HIGHEST34:
17183 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 17184 case R_PPC64_ADDR24:
65f38f15
AM
17185 case R_PPC64_ADDR32:
17186 case R_PPC64_ADDR64:
17187 case R_PPC64_UADDR16:
17188 case R_PPC64_UADDR32:
17189 case R_PPC64_UADDR64:
5663e321
AM
17190 case R_PPC64_D34:
17191 case R_PPC64_D34_LO:
17192 case R_PPC64_D34_HI30:
17193 case R_PPC64_D34_HA30:
17194 case R_PPC64_D28:
411e1bfb 17195 dodyn:
5d1634d7 17196 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
17197 break;
17198
41bd81ab
AM
17199 if (NO_OPD_RELOCS && is_opd)
17200 break;
17201
8a9e8e72 17202 if (bfd_link_pic (info)
b1b07054 17203 ? ((h == NULL
190eb1dd 17204 || h->elf.dyn_relocs != NULL)
b1b07054
AM
17205 && ((h != NULL && pc_dynrelocs (h))
17206 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 17207 : (h != NULL
190eb1dd 17208 ? h->elf.dyn_relocs != NULL
d311bc8b 17209 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 17210 {
0a1b45a2 17211 bool skip, relocate;
65f38f15 17212 asection *sreloc;
1cf1f670 17213 bfd_vma out_off;
82e66161 17214 long indx = 0;
65f38f15
AM
17215
17216 /* When generating a dynamic object, these relocations
17217 are copied into the output file to be resolved at run
17218 time. */
17219
0a1b45a2
AM
17220 skip = false;
17221 relocate = false;
65f38f15 17222
1cf1f670
AM
17223 out_off = _bfd_elf_section_offset (output_bfd, info,
17224 input_section, rel->r_offset);
17225 if (out_off == (bfd_vma) -1)
0a1b45a2 17226 skip = true;
1cf1f670 17227 else if (out_off == (bfd_vma) -2)
0a1b45a2 17228 skip = true, relocate = true;
1cf1f670
AM
17229 out_off += (input_section->output_section->vma
17230 + input_section->output_offset);
17231 outrel.r_offset = out_off;
411e1bfb 17232 outrel.r_addend = rel->r_addend;
65f38f15 17233
1cf1f670
AM
17234 /* Optimize unaligned reloc use. */
17235 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17236 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17237 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17238 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17239 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17240 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17241 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17242 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17243 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17244
65f38f15 17245 if (skip)
0bb2d96a 17246 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
17247 else if (h != NULL
17248 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
17249 && !is_opd
17250 && r_type != R_PPC64_TOC)
14acf4dc 17251 {
82e66161
AM
17252 indx = h->elf.dynindx;
17253 BFD_ASSERT (indx != -1);
17254 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 17255 }
65f38f15
AM
17256 else
17257 {
41bd81ab
AM
17258 /* This symbol is local, or marked to become local,
17259 or this is an opd section reloc which must point
17260 at a local function. */
65f38f15 17261 outrel.r_addend += relocation;
e86ce104 17262 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 17263 {
3fad3c7c 17264 if (is_opd && h != NULL)
afbe61cf
AM
17265 {
17266 /* Lie about opd entries. This case occurs
17267 when building shared libraries and we
17268 reference a function in another shared
3fad3c7c
AM
17269 lib. The same thing happens for a weak
17270 definition in an application that's
17271 overridden by a strong definition in a
17272 shared lib. (I believe this is a generic
17273 bug in binutils handling of weak syms.)
17274 In these cases we won't use the opd
1e2f5b6e 17275 entry in this lib. */
0a1b45a2 17276 unresolved_reloc = false;
afbe61cf 17277 }
25f23106
AM
17278 if (!is_opd
17279 && r_type == R_PPC64_ADDR64
17280 && (h != NULL
17281 ? h->elf.type == STT_GNU_IFUNC
17282 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17283 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17284 else
17285 {
17286 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 17287
25f23106
AM
17288 /* We need to relocate .opd contents for ld.so.
17289 Prelink also wants simple and consistent rules
17290 for relocs. This make all RELATIVE relocs have
17291 *r_offset equal to r_addend. */
0a1b45a2 17292 relocate = true;
25f23106 17293 }
65f38f15
AM
17294 }
17295 else
17296 {
25f23106
AM
17297 if (h != NULL
17298 ? h->elf.type == STT_GNU_IFUNC
17299 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17300 {
25f53a85 17301 info->callbacks->einfo
695344c0 17302 /* xgettext:c-format */
174d0a74 17303 (_("%H: %s for indirect "
c1c8c1ef 17304 "function `%pT' unsupported\n"),
25f53a85 17305 input_bfd, input_section, rel->r_offset,
25f23106
AM
17306 ppc64_elf_howto_table[r_type]->name,
17307 sym_name);
0a1b45a2 17308 ret = false;
25f23106 17309 }
cf35638d 17310 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
17311 ;
17312 else if (sec == NULL || sec->owner == NULL)
17313 {
17314 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17315 return false;
65f38f15
AM
17316 }
17317 else
17318 {
f26a3287 17319 asection *osec = sec->output_section;
65f38f15 17320
f26a3287
AM
17321 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17322 {
17323 /* TLS symbol values are relative to the
17324 TLS segment. Dynamic relocations for
17325 local TLS symbols therefore can't be
17326 reduced to a relocation against their
17327 section symbol because it holds the
17328 address of the section, not a value
17329 relative to the TLS segment. We could
17330 change the .tdata dynamic section symbol
17331 to be zero value but STN_UNDEF works
17332 and is used elsewhere, eg. for TPREL64
17333 GOT relocs against local TLS symbols. */
17334 osec = htab->elf.tls_sec;
17335 indx = 0;
17336 }
17337 else
74541ad4 17338 {
74541ad4 17339 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
17340 if (indx == 0)
17341 {
17342 if ((osec->flags & SEC_READONLY) == 0
17343 && htab->elf.data_index_section != NULL)
17344 osec = htab->elf.data_index_section;
17345 else
17346 osec = htab->elf.text_index_section;
17347 indx = elf_section_data (osec)->dynindx;
17348 }
17349 BFD_ASSERT (indx != 0);
74541ad4 17350 }
74541ad4 17351
65f38f15
AM
17352 /* We are turning this relocation into one
17353 against a section symbol, so subtract out
17354 the output section's address but not the
17355 offset of the input section in the output
17356 section. */
17357 outrel.r_addend -= osec->vma;
17358 }
17359
17360 outrel.r_info = ELF64_R_INFO (indx, r_type);
17361 }
17362 }
17363
1657026c
AM
17364 if (!(info->enable_dt_relr
17365 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17366 && rel->r_offset % 2 == 0
17367 && input_section->alignment_power != 0
17368 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
82e66161 17369 {
1657026c
AM
17370 sreloc = elf_section_data (input_section)->sreloc;
17371 if (h != NULL
17372 ? h->elf.type == STT_GNU_IFUNC
17373 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17374 {
17375 sreloc = htab->elf.irelplt;
17376 if (indx == 0 || is_static_defined (&h->elf))
17377 htab->elf.ifunc_resolvers = true;
17378 }
17379 if (sreloc == NULL)
17380 abort ();
17381
17382 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17383 >= sreloc->size)
17384 abort ();
17385 loc = sreloc->contents;
17386 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17387 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
82e66161 17388 }
65f38f15 17389
e59a1001
AM
17390 if (!warned_dynamic
17391 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17392 {
17393 info->callbacks->einfo
17394 /* xgettext:c-format */
17395 (_("%X%P: %pB: %s against %pT "
17396 "is not supported by glibc as a dynamic relocation\n"),
17397 input_bfd,
17398 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17399 sym_name);
0a1b45a2 17400 warned_dynamic = true;
e59a1001
AM
17401 }
17402
65f38f15
AM
17403 /* If this reloc is against an external symbol, it will
17404 be computed at runtime, so there's no need to do
81407a69
AM
17405 anything now. However, for the sake of prelink ensure
17406 that the section contents are a known value. */
2cdcc330 17407 if (!relocate)
81407a69 17408 {
0a1b45a2 17409 unresolved_reloc = false;
81407a69
AM
17410 /* The value chosen here is quite arbitrary as ld.so
17411 ignores section contents except for the special
17412 case of .opd where the contents might be accessed
17413 before relocation. Choose zero, as that won't
17414 cause reloc overflow. */
17415 relocation = 0;
17416 addend = 0;
17417 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17418 to improve backward compatibility with older
17419 versions of ld. */
17420 if (r_type == R_PPC64_ADDR64)
17421 addend = outrel.r_addend;
17422 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 17423 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 17424 addend = outrel.r_offset;
81407a69 17425 }
65f38f15 17426 }
5bd4f169
AM
17427 break;
17428
65f38f15
AM
17429 case R_PPC64_COPY:
17430 case R_PPC64_GLOB_DAT:
17431 case R_PPC64_JMP_SLOT:
25f23106 17432 case R_PPC64_JMP_IREL:
65f38f15
AM
17433 case R_PPC64_RELATIVE:
17434 /* We shouldn't ever see these dynamic relocs in relocatable
17435 files. */
ae9a127f 17436 /* Fall through. */
65f38f15
AM
17437
17438 case R_PPC64_PLTGOT16:
17439 case R_PPC64_PLTGOT16_DS:
17440 case R_PPC64_PLTGOT16_HA:
17441 case R_PPC64_PLTGOT16_HI:
17442 case R_PPC64_PLTGOT16_LO:
17443 case R_PPC64_PLTGOT16_LO_DS:
17444 case R_PPC64_PLTREL32:
17445 case R_PPC64_PLTREL64:
17446 /* These ones haven't been implemented yet. */
17447
25f53a85 17448 info->callbacks->einfo
695344c0 17449 /* xgettext:c-format */
c1c8c1ef 17450 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 17451 input_bfd,
4ce794b7 17452 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
17453
17454 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 17455 ret = false;
c316a17c 17456 goto copy_reloc;
65f38f15 17457 }
5bd4f169 17458
67f0cbdb
AM
17459 /* Multi-instruction sequences that access the TOC can be
17460 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 17461 to nop; addi rb,r2,x; */
67f0cbdb
AM
17462 switch (r_type)
17463 {
17464 default:
17465 break;
17466
17467 case R_PPC64_GOT_TLSLD16_HI:
17468 case R_PPC64_GOT_TLSGD16_HI:
17469 case R_PPC64_GOT_TPREL16_HI:
17470 case R_PPC64_GOT_DTPREL16_HI:
17471 case R_PPC64_GOT16_HI:
17472 case R_PPC64_TOC16_HI:
17473 /* These relocs would only be useful if building up an
17474 offset to later add to r2, perhaps in an indexed
17475 addressing mode instruction. Don't try to optimize.
17476 Unfortunately, the possibility of someone building up an
17477 offset like this or even with the HA relocs, means that
17478 we need to check the high insn when optimizing the low
17479 insn. */
17480 break;
17481
5663e321
AM
17482 case R_PPC64_PLTCALL_NOTOC:
17483 if (!unresolved_reloc)
17484 htab->notoc_plt = 1;
17485 /* Fall through. */
23cedd1d 17486 case R_PPC64_PLTCALL:
733ae98c
AM
17487 if (unresolved_reloc
17488 && offset_in_range (input_section, rel->r_offset,
17489 r_type == R_PPC64_PLTCALL ? 8 : 4))
23cedd1d
AM
17490 {
17491 /* No plt entry. Make this into a direct call. */
17492 bfd_byte *p = contents + rel->r_offset;
17493 insn = bfd_get_32 (input_bfd, p);
17494 insn &= 1;
17495 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
17496 if (r_type == R_PPC64_PLTCALL)
17497 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
17498 unresolved_reloc = save_unresolved_reloc;
17499 r_type = R_PPC64_REL24;
17500 }
17501 break;
17502
5663e321 17503 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17504 case R_PPC64_PLTSEQ:
17505 if (unresolved_reloc)
17506 {
0a1b45a2 17507 unresolved_reloc = false;
23cedd1d
AM
17508 goto nop_it;
17509 }
17510 break;
17511
5663e321
AM
17512 case R_PPC64_PLT_PCREL34_NOTOC:
17513 if (!unresolved_reloc)
17514 htab->notoc_plt = 1;
17515 /* Fall through. */
17516 case R_PPC64_PLT_PCREL34:
733ae98c
AM
17517 if (unresolved_reloc
17518 && offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17519 {
17520 bfd_byte *p = contents + rel->r_offset;
17521 bfd_put_32 (input_bfd, PNOP >> 32, p);
17522 bfd_put_32 (input_bfd, PNOP, p + 4);
0a1b45a2 17523 unresolved_reloc = false;
5663e321
AM
17524 goto copy_reloc;
17525 }
17526 break;
17527
23cedd1d
AM
17528 case R_PPC64_PLT16_HA:
17529 if (unresolved_reloc)
17530 {
0a1b45a2 17531 unresolved_reloc = false;
23cedd1d
AM
17532 goto nop_it;
17533 }
17534 /* Fall through. */
67f0cbdb
AM
17535 case R_PPC64_GOT_TLSLD16_HA:
17536 case R_PPC64_GOT_TLSGD16_HA:
17537 case R_PPC64_GOT_TPREL16_HA:
17538 case R_PPC64_GOT_DTPREL16_HA:
17539 case R_PPC64_GOT16_HA:
17540 case R_PPC64_TOC16_HA:
98528052 17541 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17542 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 17543 {
23cedd1d
AM
17544 bfd_byte *p;
17545 nop_it:
733ae98c
AM
17546 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17547 {
17548 p = contents + (rel->r_offset & ~3);
17549 bfd_put_32 (input_bfd, NOP, p);
17550 goto copy_reloc;
17551 }
98528052 17552 }
67f0cbdb
AM
17553 break;
17554
23cedd1d
AM
17555 case R_PPC64_PLT16_LO:
17556 case R_PPC64_PLT16_LO_DS:
17557 if (unresolved_reloc)
17558 {
0a1b45a2 17559 unresolved_reloc = false;
23cedd1d
AM
17560 goto nop_it;
17561 }
17562 /* Fall through. */
67f0cbdb
AM
17563 case R_PPC64_GOT_TLSLD16_LO:
17564 case R_PPC64_GOT_TLSGD16_LO:
17565 case R_PPC64_GOT_TPREL16_LO_DS:
17566 case R_PPC64_GOT_DTPREL16_LO_DS:
17567 case R_PPC64_GOT16_LO:
17568 case R_PPC64_GOT16_LO_DS:
17569 case R_PPC64_TOC16_LO:
17570 case R_PPC64_TOC16_LO_DS:
98528052 17571 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
733ae98c
AM
17572 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17573 && offset_in_range (input_section, rel->r_offset & ~3, 4))
67f0cbdb
AM
17574 {
17575 bfd_byte *p = contents + (rel->r_offset & ~3);
17576 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17577 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17578 {
17579 /* Transform addic to addi when we change reg. */
2365f8d7 17580 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17581 insn |= (14u << 26) | (2 << 16);
17582 }
17583 else
67f0cbdb 17584 {
98528052
AM
17585 insn &= ~(0x1f << 16);
17586 insn |= 2 << 16;
67f0cbdb 17587 }
560c8763 17588 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17589 }
17590 break;
9a23f96e
AM
17591
17592 case R_PPC64_TPREL16_HA:
733ae98c
AM
17593 if (htab->do_tls_opt
17594 && relocation + addend + 0x8000 < 0x10000
17595 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17596 {
17597 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17598 bfd_put_32 (input_bfd, NOP, p);
17599 goto copy_reloc;
9a23f96e
AM
17600 }
17601 break;
17602
17603 case R_PPC64_TPREL16_LO:
17604 case R_PPC64_TPREL16_LO_DS:
733ae98c
AM
17605 if (htab->do_tls_opt
17606 && relocation + addend + 0x8000 < 0x10000
17607 && offset_in_range (input_section, rel->r_offset & ~3, 4))
9a23f96e
AM
17608 {
17609 bfd_byte *p = contents + (rel->r_offset & ~3);
17610 insn = bfd_get_32 (input_bfd, p);
17611 insn &= ~(0x1f << 16);
17612 insn |= 13 << 16;
17613 bfd_put_32 (input_bfd, insn, p);
17614 }
17615 break;
67f0cbdb
AM
17616 }
17617
65f38f15
AM
17618 /* Do any further special processing. */
17619 switch (r_type)
17620 {
17621 default:
17622 break;
17623
25f23106 17624 case R_PPC64_REL16_HA:
4a969973
AM
17625 case R_PPC64_REL16_HIGHA:
17626 case R_PPC64_REL16_HIGHERA:
17627 case R_PPC64_REL16_HIGHESTA:
a680de9a 17628 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17629 case R_PPC64_ADDR16_HA:
17630 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17631 case R_PPC64_ADDR16_HIGHERA:
17632 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17633 case R_PPC64_TOC16_HA:
17634 case R_PPC64_SECTOFF_HA:
411e1bfb 17635 case R_PPC64_TPREL16_HA:
f9c6b907 17636 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17637 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17638 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17639 case R_PPC64_DTPREL16_HA:
17640 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17641 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17642 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17643 /* It's just possible that this symbol is a weak symbol
17644 that's not actually defined anywhere. In that case,
17645 'sec' would be NULL, and we should leave the symbol
17646 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17647 if (sec == NULL)
17648 break;
1a0670f3 17649 /* Fall through. */
5c5f6e17
AM
17650
17651 case R_PPC64_GOT16_HA:
17652 case R_PPC64_PLTGOT16_HA:
17653 case R_PPC64_PLT16_HA:
17654 case R_PPC64_GOT_TLSGD16_HA:
17655 case R_PPC64_GOT_TLSLD16_HA:
17656 case R_PPC64_GOT_TPREL16_HA:
17657 case R_PPC64_GOT_DTPREL16_HA:
17658 /* Add 0x10000 if sign bit in 0:15 is set.
17659 Bits 0:15 are not used. */
17660 addend += 0x8000;
65f38f15
AM
17661 break;
17662
5663e321
AM
17663 case R_PPC64_D34_HA30:
17664 case R_PPC64_ADDR16_HIGHERA34:
17665 case R_PPC64_ADDR16_HIGHESTA34:
17666 case R_PPC64_REL16_HIGHERA34:
17667 case R_PPC64_REL16_HIGHESTA34:
17668 if (sec != NULL)
17669 addend += 1ULL << 33;
17670 break;
17671
65f38f15
AM
17672 case R_PPC64_ADDR16_DS:
17673 case R_PPC64_ADDR16_LO_DS:
17674 case R_PPC64_GOT16_DS:
17675 case R_PPC64_GOT16_LO_DS:
17676 case R_PPC64_PLT16_LO_DS:
17677 case R_PPC64_SECTOFF_DS:
17678 case R_PPC64_SECTOFF_LO_DS:
17679 case R_PPC64_TOC16_DS:
17680 case R_PPC64_TOC16_LO_DS:
17681 case R_PPC64_PLTGOT16_DS:
17682 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17683 case R_PPC64_GOT_TPREL16_DS:
17684 case R_PPC64_GOT_TPREL16_LO_DS:
17685 case R_PPC64_GOT_DTPREL16_DS:
17686 case R_PPC64_GOT_DTPREL16_LO_DS:
17687 case R_PPC64_TPREL16_DS:
17688 case R_PPC64_TPREL16_LO_DS:
17689 case R_PPC64_DTPREL16_DS:
17690 case R_PPC64_DTPREL16_LO_DS:
733ae98c
AM
17691 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17692 break;
adadcc0c
AM
17693 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17694 mask = 3;
a680de9a
PB
17695 /* If this reloc is against an lq, lxv, or stxv insn, then
17696 the value must be a multiple of 16. This is somewhat of
17697 a hack, but the "correct" way to do this by defining _DQ
17698 forms of all the _DS relocs bloats all reloc switches in
17699 this file. It doesn't make much sense to use these
17700 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17701 if ((insn & (0x3fu << 26)) == (56u << 26)
17702 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17703 mask = 15;
a680de9a
PB
17704 relocation += addend;
17705 addend = insn & (mask ^ 3);
17706 if ((relocation & mask) != 0)
65f38f15 17707 {
a680de9a 17708 relocation ^= relocation & mask;
25f53a85 17709 info->callbacks->einfo
695344c0 17710 /* xgettext:c-format */
174d0a74 17711 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17712 input_bfd, input_section, rel->r_offset,
d830549d 17713 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17714 mask + 1);
65f38f15 17715 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17716 ret = false;
c316a17c 17717 goto copy_reloc;
65f38f15
AM
17718 }
17719 break;
5bd4f169
AM
17720 }
17721
239e1f3a
AM
17722 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17723 because such sections are not SEC_ALLOC and thus ld.so will
17724 not process them. */
d830549d 17725 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17726 if (unresolved_reloc
239e1f3a 17727 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17728 && h->elf.def_dynamic)
17729 && _bfd_elf_section_offset (output_bfd, info, input_section,
17730 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17731 {
25f53a85 17732 info->callbacks->einfo
695344c0 17733 /* xgettext:c-format */
c1c8c1ef 17734 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17735 input_bfd, input_section, rel->r_offset,
b80eed39 17736 howto->name,
039b3fef 17737 h->elf.root.root.string);
0a1b45a2 17738 ret = false;
9c07fe7c 17739 }
5bd4f169 17740
b80eed39
AM
17741 /* 16-bit fields in insns mostly have signed values, but a
17742 few insns have 16-bit unsigned values. Really, we should
17743 have different reloc types. */
17744 if (howto->complain_on_overflow != complain_overflow_dont
17745 && howto->dst_mask == 0xffff
733ae98c
AM
17746 && (input_section->flags & SEC_CODE) != 0
17747 && offset_in_range (input_section, rel->r_offset & ~3, 4))
b80eed39
AM
17748 {
17749 enum complain_overflow complain = complain_overflow_signed;
17750
17751 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17752 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17753 complain = complain_overflow_bitfield;
17754 else if (howto->rightshift == 0
2365f8d7
AM
17755 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17756 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17757 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17758 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17759 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17760 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17761 complain = complain_overflow_unsigned;
17762 if (howto->complain_on_overflow != complain)
17763 {
17764 alt_howto = *howto;
17765 alt_howto.complain_on_overflow = complain;
17766 howto = &alt_howto;
17767 }
17768 }
17769
5663e321 17770 switch (r_type)
a680de9a 17771 {
5663e321
AM
17772 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17773 case R_PPC64_D34:
17774 case R_PPC64_D34_LO:
17775 case R_PPC64_D34_HI30:
17776 case R_PPC64_D34_HA30:
17777 case R_PPC64_PCREL34:
17778 case R_PPC64_GOT_PCREL34:
c213164a
AM
17779 case R_PPC64_TPREL34:
17780 case R_PPC64_DTPREL34:
87c69f97
AM
17781 case R_PPC64_GOT_TLSGD_PCREL34:
17782 case R_PPC64_GOT_TLSLD_PCREL34:
17783 case R_PPC64_GOT_TPREL_PCREL34:
17784 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17785 case R_PPC64_PLT_PCREL34:
17786 case R_PPC64_PLT_PCREL34_NOTOC:
17787 case R_PPC64_D28:
17788 case R_PPC64_PCREL28:
733ae98c 17789 if (!offset_in_range (input_section, rel->r_offset, 8))
5663e321
AM
17790 r = bfd_reloc_outofrange;
17791 else
17792 {
5663e321
AM
17793 relocation += addend;
17794 if (howto->pc_relative)
17795 relocation -= (rel->r_offset
17796 + input_section->output_offset
17797 + input_section->output_section->vma);
17798 relocation >>= howto->rightshift;
17799
17800 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17801 pinsn <<= 32;
17802 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17803
17804 pinsn &= ~howto->dst_mask;
17805 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17806 & howto->dst_mask);
17807 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17808 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17809 r = bfd_reloc_ok;
17810 if (howto->complain_on_overflow == complain_overflow_signed
17811 && (relocation + (1ULL << (howto->bitsize - 1))
17812 >= 1ULL << howto->bitsize))
17813 r = bfd_reloc_overflow;
17814 }
17815 break;
17816
17817 case R_PPC64_REL16DX_HA:
733ae98c 17818 if (!offset_in_range (input_section, rel->r_offset, 4))
a680de9a
PB
17819 r = bfd_reloc_outofrange;
17820 else
17821 {
17822 relocation += addend;
17823 relocation -= (rel->r_offset
17824 + input_section->output_offset
17825 + input_section->output_section->vma);
3de43e7b 17826 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17827 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17828 insn &= ~0x1fffc1;
3de43e7b 17829 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17830 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17831 r = bfd_reloc_ok;
3de43e7b 17832 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17833 r = bfd_reloc_overflow;
17834 }
5663e321
AM
17835 break;
17836
17837 default:
17838 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17839 contents, rel->r_offset,
17840 relocation, addend);
a680de9a 17841 }
5bd4f169 17842
ef60b7ff 17843 if (r != bfd_reloc_ok)
5bd4f169 17844 {
bc30df16 17845 char *more_info = NULL;
b80eed39 17846 const char *reloc_name = howto->name;
bc30df16
AM
17847
17848 if (reloc_dest != DEST_NORMAL)
17849 {
17850 more_info = bfd_malloc (strlen (reloc_name) + 8);
17851 if (more_info != NULL)
17852 {
17853 strcpy (more_info, reloc_name);
17854 strcat (more_info, (reloc_dest == DEST_OPD
17855 ? " (OPD)" : " (stub)"));
17856 reloc_name = more_info;
17857 }
17858 }
17859
cd27b276 17860 if (r == bfd_reloc_overflow)
5bd4f169 17861 {
8131c122
AM
17862 /* On code like "if (foo) foo();" don't report overflow
17863 on a branch to zero when foo is undefined. */
17864 if (!warned
17865 && (reloc_dest == DEST_STUB
17866 || !(h != NULL
17867 && (h->elf.root.type == bfd_link_hash_undefweak
17868 || h->elf.root.type == bfd_link_hash_undefined)
17869 && is_branch_reloc (r_type))))
9e51d549
AM
17870 info->callbacks->reloc_overflow
17871 (info, (struct bfd_link_hash_entry *) h, sym_name,
17872 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17873 rel->r_offset);
ef60b7ff
AM
17874 }
17875 else
17876 {
25f53a85 17877 info->callbacks->einfo
695344c0 17878 /* xgettext:c-format */
c1c8c1ef 17879 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17880 input_bfd, input_section, rel->r_offset,
bc30df16 17881 reloc_name, sym_name, (int) r);
0a1b45a2 17882 ret = false;
ef60b7ff 17883 }
c9594989 17884 free (more_info);
5bd4f169 17885 }
c316a17c
AM
17886 copy_reloc:
17887 if (wrel != rel)
17888 *wrel = *rel;
17889 }
17890
17891 if (wrel != rel)
17892 {
17893 Elf_Internal_Shdr *rel_hdr;
17894 size_t deleted = rel - wrel;
17895
17896 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17897 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17898 if (rel_hdr->sh_size == 0)
17899 {
17900 /* It is too late to remove an empty reloc section. Leave
17901 one NONE reloc.
17902 ??? What is wrong with an empty section??? */
17903 rel_hdr->sh_size = rel_hdr->sh_entsize;
17904 deleted -= 1;
17905 }
17906 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17907 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17908 input_section->reloc_count -= deleted;
5bd4f169
AM
17909 }
17910
645ea6a9
AM
17911 /* If we're emitting relocations, then shortly after this function
17912 returns, reloc offsets and addends for this section will be
17913 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17914 file rather than the input. Save a copy of the relocs for
17915 opd_entry_value. */
0e1862bb 17916 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17917 {
17918 bfd_size_type amt;
17919 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17920 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17921 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17922 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f 17923 if (rel == NULL)
0a1b45a2 17924 return false;
8860955f
AM
17925 memcpy (rel, relocs, amt);
17926 }
5bd4f169
AM
17927 return ret;
17928}
17929
754021d0
AM
17930/* Adjust the value of any local symbols in opd sections. */
17931
6e0b88f1 17932static int
754021d0
AM
17933ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17934 const char *name ATTRIBUTE_UNUSED,
17935 Elf_Internal_Sym *elfsym,
17936 asection *input_sec,
17937 struct elf_link_hash_entry *h)
17938{
74f0fb50
AM
17939 struct _opd_sec_data *opd;
17940 long adjust;
754021d0
AM
17941 bfd_vma value;
17942
4025353c 17943 if (h != NULL)
6e0b88f1 17944 return 1;
4025353c 17945
74f0fb50
AM
17946 opd = get_opd_info (input_sec);
17947 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 17948 return 1;
754021d0
AM
17949
17950 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 17951 if (!bfd_link_relocatable (info))
754021d0
AM
17952 value -= input_sec->output_section->vma;
17953
51aecdc5 17954 adjust = opd->adjust[OPD_NDX (value)];
4025353c 17955 if (adjust == -1)
6e0b88f1
AM
17956 return 2;
17957
17958 elfsym->st_value += adjust;
17959 return 1;
754021d0
AM
17960}
17961
5bd4f169
AM
17962/* Finish up dynamic symbol handling. We set the contents of various
17963 dynamic sections here. */
17964
0a1b45a2 17965static bool
4ce794b7
AM
17966ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17967 struct bfd_link_info *info,
17968 struct elf_link_hash_entry *h,
4aef7643 17969 Elf_Internal_Sym *sym)
5bd4f169 17970{
65f38f15 17971 struct ppc_link_hash_table *htab;
8387904d 17972 struct plt_entry *ent;
5bd4f169 17973
65f38f15 17974 htab = ppc_hash_table (info);
4dfe6ac6 17975 if (htab == NULL)
0a1b45a2 17976 return false;
5bd4f169 17977
49c09209
AM
17978 if (!htab->opd_abi && !h->def_regular)
17979 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17980 if (ent->plt.offset != (bfd_vma) -1)
17981 {
17982 /* Mark the symbol as undefined, rather than as
17983 defined in glink. Leave the value if there were
17984 any relocations where pointer equality matters
17985 (this is a clue for the dynamic linker, to make
17986 function pointer comparisons work between an
17987 application and shared library), otherwise set it
17988 to zero. */
17989 sym->st_shndx = SHN_UNDEF;
17990 if (!h->pointer_equality_needed)
17991 sym->st_value = 0;
17992 else if (!h->ref_regular_nonweak)
17993 {
17994 /* This breaks function pointer comparisons, but
17995 that is better than breaking tests for a NULL
17996 function pointer. */
17997 sym->st_value = 0;
17998 }
17999 break;
18000 }
5bd4f169 18001
1bdd8fac
AM
18002 if (h->needs_copy
18003 && (h->root.type == bfd_link_hash_defined
18004 || h->root.type == bfd_link_hash_defweak)
18005 && (h->root.u.def.section == htab->elf.sdynbss
18006 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 18007 {
65f38f15 18008 /* This symbol needs a copy reloc. Set it up. */
49c09209 18009 Elf_Internal_Rela rela;
5474d94f 18010 asection *srel;
49c09209 18011 bfd_byte *loc;
5bd4f169 18012
1bdd8fac 18013 if (h->dynindx == -1)
65f38f15 18014 abort ();
5bd4f169 18015
ed7007c1 18016 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
18017 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18018 rela.r_addend = 0;
afbf7e8e 18019 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
18020 srel = htab->elf.sreldynrelro;
18021 else
18022 srel = htab->elf.srelbss;
18023 loc = srel->contents;
18024 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 18025 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
18026 }
18027
0a1b45a2 18028 return true;
5bd4f169
AM
18029}
18030
65f38f15
AM
18031/* Used to decide how to sort relocs in an optimal manner for the
18032 dynamic linker, before writing them out. */
18033
18034static enum elf_reloc_type_class
7e612e98
AM
18035ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18036 const asection *rel_sec,
18037 const Elf_Internal_Rela *rela)
65f38f15 18038{
04c9666a 18039 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
18040 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18041
33e44f2e 18042 if (rel_sec == htab->elf.irelplt)
7e612e98 18043 return reloc_class_ifunc;
a33d1f77 18044
4ce794b7 18045 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 18046 switch (r_type)
65f38f15
AM
18047 {
18048 case R_PPC64_RELATIVE:
18049 return reloc_class_relative;
18050 case R_PPC64_JMP_SLOT:
18051 return reloc_class_plt;
18052 case R_PPC64_COPY:
18053 return reloc_class_copy;
18054 default:
18055 return reloc_class_normal;
18056 }
18057}
18058
5bd4f169
AM
18059/* Finish up the dynamic sections. */
18060
0a1b45a2 18061static bool
4ce794b7
AM
18062ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18063 struct bfd_link_info *info)
5bd4f169 18064{
65f38f15
AM
18065 struct ppc_link_hash_table *htab;
18066 bfd *dynobj;
5bd4f169 18067 asection *sdyn;
5bd4f169 18068
65f38f15 18069 htab = ppc_hash_table (info);
4dfe6ac6 18070 if (htab == NULL)
0a1b45a2 18071 return false;
4dfe6ac6 18072
65f38f15 18073 dynobj = htab->elf.dynobj;
3d4d4302 18074 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 18075
65f38f15 18076 if (htab->elf.dynamic_sections_created)
5bd4f169 18077 {
5bd4f169
AM
18078 Elf64_External_Dyn *dyncon, *dynconend;
18079
33e44f2e 18080 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 18081 abort ();
5bd4f169
AM
18082
18083 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 18084 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
18085 for (; dyncon < dynconend; dyncon++)
18086 {
18087 Elf_Internal_Dyn dyn;
19397422 18088 asection *s;
5bd4f169
AM
18089
18090 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18091
18092 switch (dyn.d_tag)
18093 {
65f38f15
AM
18094 default:
18095 continue;
5bd4f169 18096
5d1634d7 18097 case DT_PPC64_GLINK:
4ce794b7 18098 s = htab->glink;
6348e046 18099 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
18100 /* We stupidly defined DT_PPC64_GLINK to be the start
18101 of glink rather than the first entry point, which is
18102 what ld.so needs, and now have a bigger stub to
18103 support automatic multiple TOCs. */
9e390558 18104 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
18105 break;
18106
19397422
AM
18107 case DT_PPC64_OPD:
18108 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18109 if (s == NULL)
18110 continue;
18111 dyn.d_un.d_ptr = s->vma;
19397422
AM
18112 break;
18113
e8910a83 18114 case DT_PPC64_OPT:
5663e321
AM
18115 if ((htab->do_multi_toc && htab->multi_toc_needed)
18116 || htab->notoc_plt)
e8910a83 18117 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
18118 if (htab->has_plt_localentry0)
18119 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
18120 break;
18121
19397422
AM
18122 case DT_PPC64_OPDSZ:
18123 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
18124 if (s == NULL)
18125 continue;
eea6121a 18126 dyn.d_un.d_val = s->size;
19397422
AM
18127 break;
18128
65f38f15 18129 case DT_PLTGOT:
33e44f2e 18130 s = htab->elf.splt;
6348e046 18131 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
18132 break;
18133
18134 case DT_JMPREL:
33e44f2e 18135 s = htab->elf.srelplt;
6348e046 18136 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 18137 break;
5bd4f169 18138
65f38f15 18139 case DT_PLTRELSZ:
33e44f2e 18140 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 18141 break;
82e66161
AM
18142
18143 case DT_TEXTREL:
cebd6b8a 18144 if (htab->elf.ifunc_resolvers)
82e66161
AM
18145 info->callbacks->einfo
18146 (_("%P: warning: text relocations and GNU indirect "
18147 "functions may result in a segfault at runtime\n"));
18148 continue;
5bd4f169 18149 }
5bd4f169 18150
65f38f15 18151 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 18152 }
5bd4f169
AM
18153 }
18154
6528b6eb
AM
18155 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18156 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18157 {
18158 /* Fill in the first entry in the global offset table.
18159 We use it to hold the link-time TOCbase. */
18160 bfd_put_64 (output_bfd,
60ee0d4a 18161 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 18162 htab->elf.sgot->contents);
5d1634d7
AM
18163
18164 /* Set .got entry size. */
2cdcc330
AM
18165 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18166 = 8;
5d1634d7
AM
18167 }
18168
6528b6eb
AM
18169 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18170 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
18171 {
18172 /* Set .plt entry size. */
33e44f2e 18173 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 18174 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
18175 }
18176
84f5d08e
AM
18177 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18178 brlt ourselves if emitrelocations. */
18179 if (htab->brlt != NULL
18180 && htab->brlt->reloc_count != 0
18181 && !_bfd_elf_link_output_relocs (output_bfd,
18182 htab->brlt,
d4730f92 18183 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
18184 elf_section_data (htab->brlt)->relocs,
18185 NULL))
0a1b45a2 18186 return false;
84f5d08e 18187
176a0d42
AM
18188 if (htab->glink != NULL
18189 && htab->glink->reloc_count != 0
18190 && !_bfd_elf_link_output_relocs (output_bfd,
18191 htab->glink,
d4730f92 18192 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
18193 elf_section_data (htab->glink)->relocs,
18194 NULL))
0a1b45a2 18195 return false;
176a0d42 18196
da44f4e5 18197
df136d64
AM
18198 if (htab->glink_eh_frame != NULL
18199 && htab->glink_eh_frame->size != 0
18200 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18201 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18202 htab->glink_eh_frame,
18203 htab->glink_eh_frame->contents))
0a1b45a2 18204 return false;
58d180e8 18205
e717da7e 18206 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
18207 since we didn't add them to DYNOBJ. We know dynobj is the first
18208 bfd. */
c72f2fb2 18209 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
18210 {
18211 asection *s;
7b53ace3 18212
0c8d6e5c 18213 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
18214 continue;
18215
e717da7e
AM
18216 s = ppc64_elf_tdata (dynobj)->got;
18217 if (s != NULL
eea6121a 18218 && s->size != 0
e717da7e
AM
18219 && s->output_section != bfd_abs_section_ptr
18220 && !bfd_set_section_contents (output_bfd, s->output_section,
18221 s->contents, s->output_offset,
eea6121a 18222 s->size))
0a1b45a2 18223 return false;
e717da7e
AM
18224 s = ppc64_elf_tdata (dynobj)->relgot;
18225 if (s != NULL
eea6121a 18226 && s->size != 0
e717da7e
AM
18227 && s->output_section != bfd_abs_section_ptr
18228 && !bfd_set_section_contents (output_bfd, s->output_section,
18229 s->contents, s->output_offset,
eea6121a 18230 s->size))
0a1b45a2 18231 return false;
e717da7e 18232 }
f6c52c13 18233
0a1b45a2 18234 return true;
5bd4f169
AM
18235}
18236
5bd4f169 18237#include "elf64-target.h"
7b8e7dad
AM
18238
18239/* FreeBSD support */
18240
18241#undef TARGET_LITTLE_SYM
6afcdeb3 18242#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 18243#undef TARGET_LITTLE_NAME
6afcdeb3 18244#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
18245
18246#undef TARGET_BIG_SYM
6d00b590 18247#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
18248#undef TARGET_BIG_NAME
18249#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18250
18251#undef ELF_OSABI
18252#define ELF_OSABI ELFOSABI_FREEBSD
18253
18254#undef elf64_bed
18255#define elf64_bed elf64_powerpc_fbsd_bed
18256
18257#include "elf64-target.h"