Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
250d07de 2 Copyright (C) 1999-2021 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
4bd7c902 28/* Don't generate unused section symbols. */
0a1b45a2 29#define TARGET_KEEP_UNUSED_SECTION_SYMBOLS false
4bd7c902 30
3db64b00 31#include "sysdep.h"
183e98be 32#include <stdarg.h>
5bd4f169 33#include "bfd.h"
5bd4f169
AM
34#include "bfdlink.h"
35#include "libbfd.h"
36#include "elf-bfd.h"
04c9666a 37#include "elf/ppc64.h"
5d1634d7 38#include "elf64-ppc.h"
58d180e8 39#include "dwarf2.h"
5bd4f169 40
bb294208
AM
41/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
42#define OCTETS_PER_BYTE(ABFD, SEC) 1
43
805fc799 44static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
46static bfd_reloc_status_type ppc64_elf_branch_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 56static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 58static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
5663e321
AM
60static bfd_reloc_status_type ppc64_elf_prefix_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 62static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016 64static bfd_vma opd_entry_value
0a1b45a2 65 (asection *, bfd_vma, asection **, bfd_vma *, bool);
5bd4f169 66
6d00b590 67#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
ad8e1ba5 68#define TARGET_LITTLE_NAME "elf64-powerpcle"
6d00b590 69#define TARGET_BIG_SYM powerpc_elf64_vec
ad8e1ba5
AM
70#define TARGET_BIG_NAME "elf64-powerpc"
71#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 72#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
73#define ELF_MACHINE_CODE EM_PPC64
74#define ELF_MAXPAGESIZE 0x10000
702d1671
AM
75#define ELF_COMMONPAGESIZE 0x1000
76#define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
ad8e1ba5
AM
77#define elf_info_to_howto ppc64_elf_info_to_howto
78
79#define elf_backend_want_got_sym 0
80#define elf_backend_want_plt_sym 0
81#define elf_backend_plt_alignment 3
82#define elf_backend_plt_not_loaded 1
ad8e1ba5 83#define elf_backend_got_header_size 8
5474d94f 84#define elf_backend_want_dynrelro 1
ad8e1ba5
AM
85#define elf_backend_can_gc_sections 1
86#define elf_backend_can_refcount 1
87#define elf_backend_rela_normal 1
64f52338 88#define elf_backend_dtrel_excludes_plt 1
6bfdb61b 89#define elf_backend_default_execstack 0
ad8e1ba5 90
e717da7e 91#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 92#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 93#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ee67d69a
AM
94#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
95#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
ad8e1ba5
AM
96#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
97#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90e3cdf2 98#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 99#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
8c5b4e52 100#define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
ad8e1ba5
AM
101
102#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
103#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
104#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 105#define elf_backend_write_core_note ppc64_elf_write_core_note
9d19e4fd 106#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
ad8e1ba5 107#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 108#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
f6c7c3e8 109#define elf_backend_check_directives ppc64_elf_before_check_relocs
e5034e59 110#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
8387904d 111#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 112#define elf_backend_check_relocs ppc64_elf_check_relocs
c0e331c7 113#define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
74f0fb50 114#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 115#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5 116#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
ad8e1ba5
AM
117#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
118#define elf_backend_hide_symbol ppc64_elf_hide_symbol
9f296da3 119#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
c9405344 120#define elf_backend_always_size_sections ppc64_elf_edit
ad8e1ba5 121#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
a345bc8d 122#define elf_backend_hash_symbol ppc64_elf_hash_symbol
74541ad4 123#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 124#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
125#define elf_backend_relocate_section ppc64_elf_relocate_section
126#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
127#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
128#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 129#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 130#define elf_backend_special_sections ppc64_elf_special_sections
bf577467 131#define elf_backend_section_flags ppc64_elf_section_flags
6911b7dc 132#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
8c5b4e52 133#define elf_backend_merge_symbol ppc64_elf_merge_symbol
bce964aa 134#define elf_backend_get_reloc_section bfd_get_section_by_name
ad8e1ba5 135
5bd4f169
AM
136/* The name of the dynamic interpreter. This is put in the .interp
137 section. */
138#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
139
140/* The size in bytes of an entry in the procedure linkage table. */
b9e5796b 141#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
2d7ad24e 142#define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
5bd4f169
AM
143
144/* The initial size of the plt reserved for the dynamic linker. */
b9e5796b 145#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
5bd4f169 146
a078d95a
AM
147/* Offsets to some stack save slots. */
148#define STK_LR 16
149#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
eb8d7fda 150/* This one is dodgy. ELFv2 does not have a linker word, so use the
a078d95a
AM
151 CR save slot. Used only by optimised __tls_get_addr call stub,
152 relying on __tls_get_addr_opt not saving CR.. */
153#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
154
5bd4f169 155/* TOC base pointers offset from start of TOC. */
411e1bfb 156#define TOC_BASE_OFF 0x8000
a27e685f
AM
157/* TOC base alignment. */
158#define TOC_BASE_ALIGN 256
411e1bfb
AM
159
160/* Offset of tp and dtp pointers from start of TLS block. */
161#define TP_OFFSET 0x7000
162#define DTP_OFFSET 0x8000
5bd4f169 163
ad8e1ba5
AM
164/* .plt call stub instructions. The normal stub is like this, but
165 sometimes the .plt entry crosses a 64k boundary and we need to
71a39c98 166 insert an addi to adjust r11. */
a078d95a 167#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
71a39c98
AM
168#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
169#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
170#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
171#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
172#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
173#define BCTR 0x4e800420 /* bctr */
174
07d6d2b8 175#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
05d0e962
AM
176#define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
177#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
07d6d2b8
AM
178#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
179#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
180
181#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
182#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
183#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
184#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
185#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
186#define BNECTR 0x4ca20420 /* bnectr+ */
187#define BNECTR_P4 0x4ce20420 /* bnectr+ */
794e51c0 188
71a39c98 189#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442 190#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
07d6d2b8 191#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
ac2df442 192
07d6d2b8
AM
193#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
194#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
195#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
ad8e1ba5 196
04bdff6a 197#define LI_R11_0 0x39600000 /* li %r11,0 */
07d6d2b8 198#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
04bdff6a 199#define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
05d0e962 200#define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
006589cf
AM
201#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
202#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
05d0e962 203#define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
a345bc8d 204#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
05d0e962 205#define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
04bdff6a 206#define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
05d0e962 207#define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
07d6d2b8 208#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
04bdff6a 209#define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
05d0e962
AM
210#define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
211#define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
212#define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
04bdff6a
AM
213#define PADDI_R12_PC 0x0610000039800000ULL
214#define PLD_R12_PC 0x04100000e5800000ULL
5663e321 215#define PNOP 0x0700000000000000ULL
a345bc8d 216
3cd7c7d7
AM
217/* __glink_PLTresolve stub instructions. We enter with the index in
218 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
9e390558 219#define GLINK_PLTRESOLVE_SIZE(htab) \
3cd7c7d7 220 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
ee4bf8d2
AM
221 /* 0: */
222 /* .quad plt0-1f */
223 /* __glink: */
224#define MFLR_R12 0x7d8802a6 /* mflr %12 */
225#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
226 /* 1: */
227#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 228 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 229#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
230#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
231 /* ld %12,0(%11) */
232 /* ld %2,8(%11) */
233 /* mtctr %12 */
234 /* ld %11,16(%11) */
ee4bf8d2 235 /* bctr */
3cd7c7d7
AM
236
237#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
238#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
239#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
240#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
241#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
242#define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
243#define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
5d1634d7
AM
244
245/* Pad with this. */
246#define NOP 0x60000000
247
721956f4
AM
248/* Some other nops. */
249#define CROR_151515 0x4def7b82
250#define CROR_313131 0x4ffffb82
251
cedb70c5 252/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
253#define LI_R0_0 0x38000000 /* li %r0,0 */
254#define B_DOT 0x48000000 /* b . */
255
256/* After that, we need two instructions to load the index, followed by
257 a branch. */
258#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 259#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 260
deb0e272
AM
261/* Instructions used by the save and restore reg functions. */
262#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
263#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
264#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
265#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
266#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
267#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
268#define LI_R12_0 0x39800000 /* li %r12,0 */
269#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
270#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
271#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
272#define BLR 0x4e800020 /* blr */
273
41bd81ab
AM
274/* Since .opd is an array of descriptors and each entry will end up
275 with identical R_PPC64_RELATIVE relocs, there is really no need to
276 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 277 relocate .opd without reloc entries. */
41bd81ab
AM
278#ifndef NO_OPD_RELOCS
279#define NO_OPD_RELOCS 0
280#endif
810d4e75 281
a4b6fadd
AM
282#ifndef ARRAY_SIZE
283#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
284#endif
285
810d4e75
AM
286static inline int
287abiversion (bfd *abfd)
288{
289 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
290}
291
292static inline void
293set_abiversion (bfd *abfd, int ver)
294{
295 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
296 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
297}
5bd4f169
AM
298\f
299/* Relocation HOWTO's. */
46807bf4
AM
300/* Like other ELF RELA targets that don't apply multiple
301 field-altering relocations to the same localation, src_mask is
302 always zero and pcrel_offset is the same as pc_relative.
303 PowerPC can always use a zero bitpos, even when the field is not at
304 the LSB. For example, a REL24 could use rightshift=2, bisize=24
305 and bitpos=2 which matches the ABI description, or as we do here,
306 rightshift=0, bitsize=26 and bitpos=0. */
307#define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
308 complain, special_func) \
309 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
310 complain_overflow_ ## complain, special_func, \
0a1b45a2 311 #type, false, 0, mask, pc_relative)
46807bf4 312
04c9666a 313static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169 314
f3185997
NC
315static reloc_howto_type ppc64_elf_howto_raw[] =
316{
5bd4f169 317 /* This reloc does nothing. */
0a1b45a2 318 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
46807bf4 319 bfd_elf_generic_reloc),
5bd4f169
AM
320
321 /* A standard 32 bit relocation. */
0a1b45a2 322 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 323 bfd_elf_generic_reloc),
5bd4f169
AM
324
325 /* An absolute 26 bit branch; the lower two bits must be zero.
326 FIXME: we don't check that, we just clear them. */
0a1b45a2 327 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
46807bf4 328 bfd_elf_generic_reloc),
5bd4f169
AM
329
330 /* A standard 16 bit relocation. */
0a1b45a2 331 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 332 bfd_elf_generic_reloc),
5bd4f169
AM
333
334 /* A 16 bit relocation without overflow. */
0a1b45a2 335 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 336 bfd_elf_generic_reloc),
5bd4f169
AM
337
338 /* Bits 16-31 of an address. */
0a1b45a2 339 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 340 bfd_elf_generic_reloc),
5bd4f169
AM
341
342 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
343 bits, treated as a signed number, is negative. */
0a1b45a2 344 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 345 ppc64_elf_ha_reloc),
5bd4f169
AM
346
347 /* An absolute 16 bit branch; the lower two bits must be zero.
348 FIXME: we don't check that, we just clear them. */
0a1b45a2 349 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 350 ppc64_elf_branch_reloc),
5bd4f169
AM
351
352 /* An absolute 16 bit branch, for which bit 10 should be set to
353 indicate that the branch is expected to be taken. The lower two
354 bits must be zero. */
0a1b45a2 355 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 356 ppc64_elf_brtaken_reloc),
5bd4f169
AM
357
358 /* An absolute 16 bit branch, for which bit 10 should be set to
359 indicate that the branch is not expected to be taken. The lower
360 two bits must be zero. */
0a1b45a2 361 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
46807bf4 362 ppc64_elf_brtaken_reloc),
5bd4f169
AM
363
364 /* A relative 26 bit branch; the lower two bits must be zero. */
0a1b45a2 365 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 366 ppc64_elf_branch_reloc),
5bd4f169 367
05d0e962 368 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
0a1b45a2 369 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
46807bf4 370 ppc64_elf_branch_reloc),
05d0e962 371
5bd4f169 372 /* A relative 16 bit branch; the lower two bits must be zero. */
0a1b45a2 373 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 374 ppc64_elf_branch_reloc),
5bd4f169
AM
375
376 /* A relative 16 bit branch. Bit 10 should be set to indicate that
377 the branch is expected to be taken. The lower two bits must be
378 zero. */
0a1b45a2 379 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 380 ppc64_elf_brtaken_reloc),
5bd4f169
AM
381
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is not expected to be taken. The lower two bits must
384 be zero. */
0a1b45a2 385 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
46807bf4 386 ppc64_elf_brtaken_reloc),
5bd4f169
AM
387
388 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
389 symbol. */
0a1b45a2 390 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
46807bf4 391 ppc64_elf_unhandled_reloc),
5bd4f169
AM
392
393 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
394 the symbol. */
0a1b45a2 395 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 396 ppc64_elf_unhandled_reloc),
5bd4f169
AM
397
398 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
399 the symbol. */
0a1b45a2 400 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 401 ppc64_elf_unhandled_reloc),
5bd4f169
AM
402
403 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
404 the symbol. */
0a1b45a2 405 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 406 ppc64_elf_unhandled_reloc),
5bd4f169
AM
407
408 /* This is used only by the dynamic linker. The symbol should exist
409 both in the object being run and in some shared library. The
410 dynamic linker copies the data addressed by the symbol from the
411 shared library into the object, because the object being
412 run has to have the data at some particular address. */
0a1b45a2 413 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
46807bf4 414 ppc64_elf_unhandled_reloc),
5bd4f169
AM
415
416 /* Like R_PPC64_ADDR64, but used when setting global offset table
417 entries. */
0a1b45a2 418 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 419 ppc64_elf_unhandled_reloc),
5bd4f169
AM
420
421 /* Created by the link editor. Marks a procedure linkage table
422 entry for a symbol. */
0a1b45a2 423 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
46807bf4 424 ppc64_elf_unhandled_reloc),
5bd4f169
AM
425
426 /* Used only by the dynamic linker. When the object is run, this
427 doubleword64 is set to the load address of the object, plus the
428 addend. */
0a1b45a2 429 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 430 bfd_elf_generic_reloc),
5bd4f169
AM
431
432 /* Like R_PPC64_ADDR32, but may be unaligned. */
0a1b45a2 433 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 434 bfd_elf_generic_reloc),
5bd4f169
AM
435
436 /* Like R_PPC64_ADDR16, but may be unaligned. */
0a1b45a2 437 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
46807bf4 438 bfd_elf_generic_reloc),
5bd4f169
AM
439
440 /* 32-bit PC relative. */
0a1b45a2 441 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 442 bfd_elf_generic_reloc),
5bd4f169 443
10ed1bba 444 /* 32-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 445 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
46807bf4 446 ppc64_elf_unhandled_reloc),
5bd4f169
AM
447
448 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
449 FIXME: R_PPC64_PLTREL32 not supported. */
0a1b45a2 450 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
46807bf4 451 ppc64_elf_unhandled_reloc),
5bd4f169
AM
452
453 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
454 the symbol. */
0a1b45a2 455 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 456 ppc64_elf_unhandled_reloc),
5bd4f169
AM
457
458 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
459 the symbol. */
0a1b45a2 460 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 461 ppc64_elf_unhandled_reloc),
5bd4f169
AM
462
463 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
464 the symbol. */
0a1b45a2 465 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 466 ppc64_elf_unhandled_reloc),
5bd4f169 467
c061c2d8 468 /* 16-bit section relative relocation. */
0a1b45a2 469 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
46807bf4 470 ppc64_elf_sectoff_reloc),
5bd4f169 471
c061c2d8 472 /* Like R_PPC64_SECTOFF, but no overflow warning. */
0a1b45a2 473 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 474 ppc64_elf_sectoff_reloc),
5bd4f169
AM
475
476 /* 16-bit upper half section relative relocation. */
0a1b45a2 477 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 478 ppc64_elf_sectoff_reloc),
5bd4f169
AM
479
480 /* 16-bit upper half adjusted section relative relocation. */
0a1b45a2 481 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 482 ppc64_elf_sectoff_ha_reloc),
5bd4f169 483
04c9666a 484 /* Like R_PPC64_REL24 without touching the two least significant bits. */
0a1b45a2 485 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
46807bf4 486 bfd_elf_generic_reloc),
5bd4f169
AM
487
488 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
489
490 /* A standard 64-bit relocation. */
0a1b45a2 491 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 492 bfd_elf_generic_reloc),
5bd4f169
AM
493
494 /* The bits 32-47 of an address. */
0a1b45a2 495 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 496 bfd_elf_generic_reloc),
5bd4f169
AM
497
498 /* The bits 32-47 of an address, plus 1 if the contents of the low
499 16 bits, treated as a signed number, is negative. */
0a1b45a2 500 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 501 ppc64_elf_ha_reloc),
5bd4f169
AM
502
503 /* The bits 48-63 of an address. */
0a1b45a2 504 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 505 bfd_elf_generic_reloc),
5bd4f169
AM
506
507 /* The bits 48-63 of an address, plus 1 if the contents of the low
508 16 bits, treated as a signed number, is negative. */
0a1b45a2 509 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 510 ppc64_elf_ha_reloc),
5bd4f169
AM
511
512 /* Like ADDR64, but may be unaligned. */
0a1b45a2 513 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 514 bfd_elf_generic_reloc),
5bd4f169
AM
515
516 /* 64-bit relative relocation. */
0a1b45a2 517 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 518 bfd_elf_generic_reloc),
5bd4f169 519
cedb70c5 520 /* 64-bit relocation to the symbol's procedure linkage table. */
0a1b45a2 521 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 522 ppc64_elf_unhandled_reloc),
5bd4f169
AM
523
524 /* 64-bit PC relative relocation to the symbol's procedure linkage
525 table. */
526 /* FIXME: R_PPC64_PLTREL64 not supported. */
0a1b45a2 527 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
46807bf4 528 ppc64_elf_unhandled_reloc),
5bd4f169
AM
529
530 /* 16 bit TOC-relative relocation. */
5bd4f169 531 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
0a1b45a2 532 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
46807bf4 533 ppc64_elf_toc_reloc),
5bd4f169
AM
534
535 /* 16 bit TOC-relative relocation without overflow. */
5bd4f169 536 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
0a1b45a2 537 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 538 ppc64_elf_toc_reloc),
5bd4f169
AM
539
540 /* 16 bit TOC-relative relocation, high 16 bits. */
5bd4f169 541 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
0a1b45a2 542 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 543 ppc64_elf_toc_reloc),
5bd4f169
AM
544
545 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
546 contents of the low 16 bits, treated as a signed number, is
547 negative. */
5bd4f169 548 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
0a1b45a2 549 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 550 ppc64_elf_toc_ha_reloc),
5bd4f169
AM
551
552 /* 64-bit relocation; insert value of TOC base (.TOC.). */
5bd4f169 553 /* R_PPC64_TOC 51 doubleword64 .TOC. */
0a1b45a2 554 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 555 ppc64_elf_toc64_reloc),
5bd4f169
AM
556
557 /* Like R_PPC64_GOT16, but also informs the link editor that the
558 value to relocate may (!) refer to a PLT entry which the link
559 editor (a) may replace with the symbol value. If the link editor
560 is unable to fully resolve the symbol, it may (b) create a PLT
561 entry and store the address to the new PLT entry in the GOT.
562 This permits lazy resolution of function symbols at run time.
563 The link editor may also skip all of this and just (c) emit a
564 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
565 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
0a1b45a2 566 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
46807bf4 567 ppc64_elf_unhandled_reloc),
411e1bfb
AM
568
569 /* Like R_PPC64_PLTGOT16, but without overflow. */
570 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 571 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 572 ppc64_elf_unhandled_reloc),
411e1bfb
AM
573
574 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
575 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
0a1b45a2 576 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 577 ppc64_elf_unhandled_reloc),
411e1bfb
AM
578
579 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
580 1 if the contents of the low 16 bits, treated as a signed number,
581 is negative. */
582 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
0a1b45a2 583 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 584 ppc64_elf_unhandled_reloc),
411e1bfb
AM
585
586 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
0a1b45a2 587 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 588 bfd_elf_generic_reloc),
411e1bfb
AM
589
590 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
0a1b45a2 591 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 592 bfd_elf_generic_reloc),
411e1bfb
AM
593
594 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
0a1b45a2 595 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 596 ppc64_elf_unhandled_reloc),
411e1bfb
AM
597
598 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
0a1b45a2 599 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 600 ppc64_elf_unhandled_reloc),
411e1bfb
AM
601
602 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
0a1b45a2 603 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 604 ppc64_elf_unhandled_reloc),
411e1bfb
AM
605
606 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
0a1b45a2 607 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 608 ppc64_elf_sectoff_reloc),
411e1bfb
AM
609
610 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
0a1b45a2 611 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 612 ppc64_elf_sectoff_reloc),
411e1bfb
AM
613
614 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
0a1b45a2 615 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 616 ppc64_elf_toc_reloc),
411e1bfb
AM
617
618 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
0a1b45a2 619 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 620 ppc64_elf_toc_reloc),
411e1bfb
AM
621
622 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
623 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
0a1b45a2 624 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 625 ppc64_elf_unhandled_reloc),
411e1bfb
AM
626
627 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
628 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
0a1b45a2 629 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 630 ppc64_elf_unhandled_reloc),
411e1bfb 631
727fc41e 632 /* Marker relocs for TLS. */
0a1b45a2 633 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
46807bf4
AM
634 bfd_elf_generic_reloc),
635
0a1b45a2 636 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
46807bf4
AM
637 bfd_elf_generic_reloc),
638
0a1b45a2 639 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
46807bf4 640 bfd_elf_generic_reloc),
727fc41e 641
23cedd1d
AM
642 /* Marker reloc for optimizing r2 save in prologue rather than on
643 each plt call stub. */
0a1b45a2 644 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
46807bf4 645 bfd_elf_generic_reloc),
3b421ab3 646
23cedd1d 647 /* Marker relocs on inline plt call instructions. */
0a1b45a2 648 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
46807bf4
AM
649 bfd_elf_generic_reloc),
650
0a1b45a2 651 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
46807bf4 652 bfd_elf_generic_reloc),
23cedd1d 653
411e1bfb
AM
654 /* Computes the load module index of the load module that contains the
655 definition of its TLS sym. */
0a1b45a2 656 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 657 ppc64_elf_unhandled_reloc),
411e1bfb
AM
658
659 /* Computes a dtv-relative displacement, the difference between the value
660 of sym+add and the base address of the thread-local storage block that
661 contains the definition of sym, minus 0x8000. */
0a1b45a2 662 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 663 ppc64_elf_unhandled_reloc),
411e1bfb
AM
664
665 /* A 16 bit dtprel reloc. */
0a1b45a2 666 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 667 ppc64_elf_unhandled_reloc),
411e1bfb
AM
668
669 /* Like DTPREL16, but no overflow. */
0a1b45a2 670 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 671 ppc64_elf_unhandled_reloc),
411e1bfb
AM
672
673 /* Like DTPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 674 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 675 ppc64_elf_unhandled_reloc),
411e1bfb
AM
676
677 /* Like DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 678 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 679 ppc64_elf_unhandled_reloc),
411e1bfb
AM
680
681 /* Like DTPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 682 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 683 ppc64_elf_unhandled_reloc),
411e1bfb
AM
684
685 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 686 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 687 ppc64_elf_unhandled_reloc),
411e1bfb
AM
688
689 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 690 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 691 ppc64_elf_unhandled_reloc),
411e1bfb
AM
692
693 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 694 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 695 ppc64_elf_unhandled_reloc),
411e1bfb
AM
696
697 /* Like DTPREL16, but for insns with a DS field. */
0a1b45a2 698 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 699 ppc64_elf_unhandled_reloc),
411e1bfb
AM
700
701 /* Like DTPREL16_DS, but no overflow. */
0a1b45a2 702 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 703 ppc64_elf_unhandled_reloc),
411e1bfb
AM
704
705 /* Computes a tp-relative displacement, the difference between the value of
706 sym+add and the value of the thread pointer (r13). */
0a1b45a2 707 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 708 ppc64_elf_unhandled_reloc),
411e1bfb
AM
709
710 /* A 16 bit tprel reloc. */
0a1b45a2 711 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
46807bf4 712 ppc64_elf_unhandled_reloc),
411e1bfb
AM
713
714 /* Like TPREL16, but no overflow. */
0a1b45a2 715 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 716 ppc64_elf_unhandled_reloc),
411e1bfb
AM
717
718 /* Like TPREL16_LO, but next higher group of 16 bits. */
0a1b45a2 719 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 720 ppc64_elf_unhandled_reloc),
411e1bfb
AM
721
722 /* Like TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 723 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 724 ppc64_elf_unhandled_reloc),
411e1bfb
AM
725
726 /* Like TPREL16_HI, but next higher group of 16 bits. */
0a1b45a2 727 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
46807bf4 728 ppc64_elf_unhandled_reloc),
411e1bfb
AM
729
730 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
0a1b45a2 731 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
46807bf4 732 ppc64_elf_unhandled_reloc),
411e1bfb
AM
733
734 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
0a1b45a2 735 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
46807bf4 736 ppc64_elf_unhandled_reloc),
411e1bfb
AM
737
738 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
0a1b45a2 739 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
46807bf4 740 ppc64_elf_unhandled_reloc),
411e1bfb
AM
741
742 /* Like TPREL16, but for insns with a DS field. */
0a1b45a2 743 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 744 ppc64_elf_unhandled_reloc),
411e1bfb
AM
745
746 /* Like TPREL16_DS, but no overflow. */
0a1b45a2 747 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 748 ppc64_elf_unhandled_reloc),
411e1bfb
AM
749
750 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
751 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
752 to the first entry relative to the TOC base (r2). */
0a1b45a2 753 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 754 ppc64_elf_unhandled_reloc),
5bd4f169 755
411e1bfb 756 /* Like GOT_TLSGD16, but no overflow. */
0a1b45a2 757 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 758 ppc64_elf_unhandled_reloc),
5bd4f169 759
411e1bfb 760 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
0a1b45a2 761 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 762 ppc64_elf_unhandled_reloc),
5bd4f169 763
411e1bfb 764 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
0a1b45a2 765 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 766 ppc64_elf_unhandled_reloc),
5bd4f169 767
411e1bfb
AM
768 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
769 with values (sym+add)@dtpmod and zero, and computes the offset to the
770 first entry relative to the TOC base (r2). */
0a1b45a2 771 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
46807bf4 772 ppc64_elf_unhandled_reloc),
5bd4f169 773
411e1bfb 774 /* Like GOT_TLSLD16, but no overflow. */
0a1b45a2 775 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
46807bf4 776 ppc64_elf_unhandled_reloc),
5bd4f169 777
411e1bfb 778 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
0a1b45a2 779 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 780 ppc64_elf_unhandled_reloc),
5bd4f169 781
411e1bfb 782 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
0a1b45a2 783 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 784 ppc64_elf_unhandled_reloc),
5bd4f169 785
411e1bfb
AM
786 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
787 the offset to the entry relative to the TOC base (r2). */
0a1b45a2 788 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 789 ppc64_elf_unhandled_reloc),
5bd4f169 790
411e1bfb 791 /* Like GOT_DTPREL16_DS, but no overflow. */
0a1b45a2 792 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 793 ppc64_elf_unhandled_reloc),
5bd4f169 794
411e1bfb 795 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 796 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 797 ppc64_elf_unhandled_reloc),
5bd4f169 798
411e1bfb 799 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 800 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4 801 ppc64_elf_unhandled_reloc),
411e1bfb
AM
802
803 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
804 offset to the entry relative to the TOC base (r2). */
0a1b45a2 805 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
46807bf4 806 ppc64_elf_unhandled_reloc),
5bd4f169 807
411e1bfb 808 /* Like GOT_TPREL16_DS, but no overflow. */
0a1b45a2 809 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
46807bf4 810 ppc64_elf_unhandled_reloc),
5bd4f169 811
411e1bfb 812 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
0a1b45a2 813 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
46807bf4 814 ppc64_elf_unhandled_reloc),
5bd4f169 815
411e1bfb 816 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
0a1b45a2 817 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
46807bf4
AM
818 ppc64_elf_unhandled_reloc),
819
0a1b45a2 820 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
46807bf4
AM
821 ppc64_elf_unhandled_reloc),
822
0a1b45a2 823 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 824 bfd_elf_generic_reloc),
e054468f 825
25f23106 826 /* A 16 bit relative relocation. */
0a1b45a2 827 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
46807bf4 828 bfd_elf_generic_reloc),
25f23106
AM
829
830 /* A 16 bit relative relocation without overflow. */
0a1b45a2 831 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
46807bf4 832 bfd_elf_generic_reloc),
25f23106
AM
833
834 /* The high order 16 bits of a relative address. */
0a1b45a2 835 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
46807bf4 836 bfd_elf_generic_reloc),
25f23106
AM
837
838 /* The high order 16 bits of a relative address, plus 1 if the contents of
839 the low 16 bits, treated as a signed number, is negative. */
0a1b45a2 840 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
46807bf4 841 ppc64_elf_ha_reloc),
25f23106 842
0a1b45a2 843 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
844 bfd_elf_generic_reloc),
845
0a1b45a2 846 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
4a969973
AM
847 ppc64_elf_ha_reloc),
848
0a1b45a2 849 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
850 bfd_elf_generic_reloc),
851
0a1b45a2 852 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
4a969973
AM
853 ppc64_elf_ha_reloc),
854
0a1b45a2 855 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
856 bfd_elf_generic_reloc),
857
0a1b45a2 858 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
4a969973
AM
859 ppc64_elf_ha_reloc),
860
a680de9a 861 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
0a1b45a2 862 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
46807bf4 863 ppc64_elf_ha_reloc),
a680de9a 864
7ba71655 865 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
0a1b45a2 866 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
46807bf4 867 ppc64_elf_ha_reloc),
7ba71655 868
f9c6b907 869 /* Like R_PPC64_ADDR16_HI, but no overflow. */
0a1b45a2 870 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 871 bfd_elf_generic_reloc),
f9c6b907
AM
872
873 /* Like R_PPC64_ADDR16_HA, but no overflow. */
0a1b45a2 874 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 875 ppc64_elf_ha_reloc),
f9c6b907
AM
876
877 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
0a1b45a2 878 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 879 ppc64_elf_unhandled_reloc),
f9c6b907
AM
880
881 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
0a1b45a2 882 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 883 ppc64_elf_unhandled_reloc),
f9c6b907
AM
884
885 /* Like R_PPC64_TPREL16_HI, but no overflow. */
0a1b45a2 886 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
46807bf4 887 ppc64_elf_unhandled_reloc),
f9c6b907
AM
888
889 /* Like R_PPC64_TPREL16_HA, but no overflow. */
0a1b45a2 890 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
46807bf4 891 ppc64_elf_unhandled_reloc),
f9c6b907 892
006589cf 893 /* Marker reloc on ELFv2 large-model function entry. */
0a1b45a2 894 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
46807bf4 895 bfd_elf_generic_reloc),
006589cf 896
45965137 897 /* Like ADDR64, but use local entry point of function. */
0a1b45a2 898 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
46807bf4 899 bfd_elf_generic_reloc),
45965137 900
0a1b45a2 901 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
902 bfd_elf_generic_reloc),
903
0a1b45a2 904 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
5663e321
AM
905 bfd_elf_generic_reloc),
906
0a1b45a2 907 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
5663e321
AM
908 bfd_elf_generic_reloc),
909
0a1b45a2 910 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
5663e321
AM
911 ppc64_elf_prefix_reloc),
912
0a1b45a2 913 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
5663e321
AM
914 ppc64_elf_prefix_reloc),
915
0a1b45a2 916 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
917 ppc64_elf_prefix_reloc),
918
0a1b45a2 919 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
5663e321
AM
920 ppc64_elf_prefix_reloc),
921
0a1b45a2 922 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
923 ppc64_elf_prefix_reloc),
924
0a1b45a2 925 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
926 ppc64_elf_unhandled_reloc),
927
0a1b45a2 928 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
929 ppc64_elf_unhandled_reloc),
930
0a1b45a2 931 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
5663e321
AM
932 ppc64_elf_unhandled_reloc),
933
0a1b45a2 934 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
935 ppc64_elf_unhandled_reloc),
936
0a1b45a2 937 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
c213164a
AM
938 ppc64_elf_unhandled_reloc),
939
0a1b45a2 940 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
941 ppc64_elf_unhandled_reloc),
942
0a1b45a2 943 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
944 ppc64_elf_unhandled_reloc),
945
0a1b45a2 946 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
947 ppc64_elf_unhandled_reloc),
948
0a1b45a2 949 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
c213164a
AM
950 ppc64_elf_unhandled_reloc),
951
0a1b45a2 952 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
953 bfd_elf_generic_reloc),
954
0a1b45a2 955 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
5663e321
AM
956 ppc64_elf_ha_reloc),
957
0a1b45a2 958 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
959 bfd_elf_generic_reloc),
960
0a1b45a2 961 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
5663e321
AM
962 ppc64_elf_ha_reloc),
963
0a1b45a2 964 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
965 bfd_elf_generic_reloc),
966
0a1b45a2 967 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
5663e321
AM
968 ppc64_elf_ha_reloc),
969
0a1b45a2 970 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
971 bfd_elf_generic_reloc),
972
0a1b45a2 973 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
5663e321
AM
974 ppc64_elf_ha_reloc),
975
0a1b45a2 976 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
5663e321
AM
977 ppc64_elf_prefix_reloc),
978
0a1b45a2 979 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
5663e321
AM
980 ppc64_elf_prefix_reloc),
981
5bd4f169 982 /* GNU extension to record C++ vtable hierarchy. */
0a1b45a2 983 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
46807bf4 984 NULL),
5bd4f169
AM
985
986 /* GNU extension to record C++ vtable member usage. */
0a1b45a2 987 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
46807bf4 988 NULL),
5bd4f169
AM
989};
990
991\f
992/* Initialize the ppc64_elf_howto_table, so that linear accesses can
993 be done. */
994
995static void
4ce794b7 996ppc_howto_init (void)
5bd4f169
AM
997{
998 unsigned int i, type;
999
a4b6fadd 1000 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
5bd4f169
AM
1001 {
1002 type = ppc64_elf_howto_raw[i].type;
a4b6fadd 1003 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
5bd4f169
AM
1004 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1005 }
1006}
1007
1008static reloc_howto_type *
87c69f97 1009ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
5bd4f169 1010{
411e1bfb 1011 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1012
1013 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1014 /* Initialize howto table if needed. */
1015 ppc_howto_init ();
1016
4ce794b7 1017 switch (code)
5bd4f169
AM
1018 {
1019 default:
f3185997 1020 /* xgettext:c-format */
2cdcc330
AM
1021 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1022 (int) code);
f3185997 1023 bfd_set_error (bfd_error_bad_value);
4ce794b7 1024 return NULL;
5bd4f169 1025
411e1bfb
AM
1026 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1027 break;
1028 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1029 break;
1030 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1031 break;
1032 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1033 break;
1034 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1035 break;
1036 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1037 break;
f9c6b907
AM
1038 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1039 break;
411e1bfb 1040 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1041 break;
f9c6b907
AM
1042 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1043 break;
411e1bfb 1044 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1045 break;
411e1bfb 1046 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1047 break;
411e1bfb 1048 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1049 break;
411e1bfb 1050 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1051 break;
05d0e962
AM
1052 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1053 break;
411e1bfb 1054 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1055 break;
411e1bfb 1056 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1057 break;
411e1bfb 1058 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1059 break;
411e1bfb 1060 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1061 break;
411e1bfb 1062 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1063 break;
411e1bfb 1064 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1065 break;
411e1bfb 1066 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1067 break;
411e1bfb 1068 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1069 break;
411e1bfb 1070 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1071 break;
411e1bfb 1072 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1073 break;
411e1bfb 1074 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1075 break;
411e1bfb 1076 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1077 break;
411e1bfb 1078 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1079 break;
411e1bfb 1080 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1081 break;
411e1bfb 1082 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1083 break;
411e1bfb 1084 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1085 break;
411e1bfb 1086 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1087 break;
411e1bfb 1088 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1089 break;
411e1bfb 1090 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1091 break;
411e1bfb 1092 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1093 break;
411e1bfb 1094 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1095 break;
411e1bfb 1096 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1097 break;
411e1bfb 1098 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1099 break;
411e1bfb 1100 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1101 break;
411e1bfb 1102 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1103 break;
411e1bfb 1104 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1105 break;
411e1bfb 1106 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1107 break;
411e1bfb 1108 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1109 break;
411e1bfb 1110 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1111 break;
411e1bfb 1112 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1113 break;
411e1bfb 1114 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1115 break;
411e1bfb 1116 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1117 break;
411e1bfb 1118 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1119 break;
411e1bfb 1120 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1121 break;
411e1bfb 1122 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1123 break;
411e1bfb 1124 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1125 break;
411e1bfb 1126 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1127 break;
411e1bfb 1128 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 1129 break;
411e1bfb 1130 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 1131 break;
411e1bfb 1132 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 1133 break;
411e1bfb 1134 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 1135 break;
411e1bfb 1136 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 1137 break;
411e1bfb 1138 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 1139 break;
411e1bfb 1140 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 1141 break;
411e1bfb 1142 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 1143 break;
411e1bfb 1144 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 1145 break;
411e1bfb 1146 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 1147 break;
411e1bfb 1148 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 1149 break;
c213164a 1150 case BFD_RELOC_PPC64_TLS_PCREL:
411e1bfb 1151 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 1152 break;
727fc41e
AM
1153 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1154 break;
1155 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1156 break;
411e1bfb 1157 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 1158 break;
411e1bfb 1159 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 1160 break;
411e1bfb 1161 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 1162 break;
411e1bfb 1163 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 1164 break;
f9c6b907
AM
1165 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1166 break;
411e1bfb 1167 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 1168 break;
f9c6b907
AM
1169 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1170 break;
411e1bfb 1171 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 1172 break;
411e1bfb
AM
1173 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1174 break;
1175 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1176 break;
1177 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1178 break;
f9c6b907
AM
1179 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1180 break;
411e1bfb
AM
1181 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1182 break;
f9c6b907
AM
1183 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1184 break;
411e1bfb
AM
1185 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1186 break;
1187 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1210 break;
1211 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1212 break;
1213 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1214 break;
1215 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1216 break;
1217 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1218 break;
1219 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1220 break;
1221 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1222 break;
1223 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1224 break;
1225 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1226 break;
1227 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1228 break;
1229 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1230 break;
1231 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1232 break;
1233 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1234 break;
1235 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1236 break;
1237 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1238 break;
1239 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1240 break;
1241 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1242 break;
25f23106
AM
1243 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1244 break;
1245 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1246 break;
1247 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1248 break;
1249 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1250 break;
4a969973
AM
1251 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1252 break;
1253 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1254 break;
1255 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1256 break;
1257 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1258 break;
1259 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1260 break;
1261 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1262 break;
7ba71655
AM
1263 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1264 break;
a680de9a
PB
1265 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1266 break;
006589cf
AM
1267 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1268 break;
45965137
AM
1269 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1270 break;
5663e321
AM
1271 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1272 break;
1273 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1274 break;
1275 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1276 break;
1277 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1278 break;
1279 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1280 break;
1281 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1282 break;
1283 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1284 break;
c213164a
AM
1285 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1286 break;
1287 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1288 break;
87c69f97 1289 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
c213164a 1290 break;
87c69f97 1291 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
c213164a 1292 break;
87c69f97 1293 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
c213164a 1294 break;
87c69f97 1295 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
c213164a 1296 break;
5663e321
AM
1297 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1298 break;
1299 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1300 break;
1301 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1302 break;
1303 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1304 break;
1305 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1306 break;
1307 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1308 break;
1309 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1310 break;
1311 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1312 break;
1313 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1314 break;
1315 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1316 break;
411e1bfb
AM
1317 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1318 break;
1319 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
1320 break;
1321 }
1322
4ce794b7 1323 return ppc64_elf_howto_table[r];
5bd4f169
AM
1324};
1325
157090f7 1326static reloc_howto_type *
87c69f97 1327ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
157090f7
AM
1328{
1329 unsigned int i;
87c69f97
AM
1330 static char *compat_map[][2] = {
1331 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1332 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1333 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1334 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1335 };
157090f7 1336
a4b6fadd 1337 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
157090f7
AM
1338 if (ppc64_elf_howto_raw[i].name != NULL
1339 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1340 return &ppc64_elf_howto_raw[i];
1341
87c69f97
AM
1342 /* Handle old names of relocations in case they were used by
1343 .reloc directives.
1344 FIXME: Remove this soon. Mapping the reloc names is very likely
1345 completely unnecessary. */
1346 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1347 if (strcasecmp (compat_map[i][0], r_name) == 0)
1348 {
1349 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1350 compat_map[i][1], compat_map[i][0]);
1351 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1352 }
1353
157090f7
AM
1354 return NULL;
1355}
1356
5bd4f169
AM
1357/* Set the howto pointer for a PowerPC ELF reloc. */
1358
0a1b45a2 1359static bool
4aef7643 1360ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
4ce794b7 1361 Elf_Internal_Rela *dst)
5bd4f169 1362{
65f38f15
AM
1363 unsigned int type;
1364
ef60b7ff 1365 /* Initialize howto table if needed. */
5bd4f169 1366 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
1367 ppc_howto_init ();
1368
65f38f15 1369 type = ELF64_R_TYPE (dst->r_info);
a4b6fadd 1370 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
d0fb9a8d 1371 {
695344c0 1372 /* xgettext:c-format */
0aa13fee 1373 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
cf97bcb0 1374 abfd, type);
f3185997 1375 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1376 return false;
d0fb9a8d 1377 }
65f38f15 1378 cache_ptr->howto = ppc64_elf_howto_table[type];
f3185997
NC
1379 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1380 {
1381 /* xgettext:c-format */
1382 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1383 abfd, type);
1384 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1385 return false;
f3185997 1386 }
2cdcc330 1387
0a1b45a2 1388 return true;
5bd4f169
AM
1389}
1390
04c9666a 1391/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
1392
1393static bfd_reloc_status_type
4ce794b7
AM
1394ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1395 void *data, asection *input_section,
1396 bfd *output_bfd, char **error_message)
5bd4f169 1397{
a680de9a
PB
1398 enum elf_ppc64_reloc_type r_type;
1399 long insn;
1400 bfd_size_type octets;
3de43e7b 1401 bfd_vma value;
a680de9a 1402
805fc799
AM
1403 /* If this is a relocatable link (output_bfd test tells us), just
1404 call the generic function. Any adjustment will be done at final
1405 link time. */
1406 if (output_bfd != NULL)
cedb70c5 1407 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1408 input_section, output_bfd, error_message);
1409
5663e321
AM
1410 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1411 We won't actually be using the low bits, so trashing them
805fc799 1412 doesn't matter. */
a680de9a 1413 r_type = reloc_entry->howto->type;
5663e321
AM
1414 if (r_type == R_PPC64_ADDR16_HIGHERA34
1415 || r_type == R_PPC64_ADDR16_HIGHESTA34
1416 || r_type == R_PPC64_REL16_HIGHERA34
1417 || r_type == R_PPC64_REL16_HIGHESTA34)
1418 reloc_entry->addend += 1ULL << 33;
1419 else
1420 reloc_entry->addend += 1U << 15;
a680de9a
PB
1421 if (r_type != R_PPC64_REL16DX_HA)
1422 return bfd_reloc_continue;
1423
1424 value = 0;
1425 if (!bfd_is_com_section (symbol->section))
1426 value = symbol->value;
1427 value += (reloc_entry->addend
1428 + symbol->section->output_offset
1429 + symbol->section->output_section->vma);
1430 value -= (reloc_entry->address
1431 + input_section->output_offset
1432 + input_section->output_section->vma);
3de43e7b 1433 value = (bfd_signed_vma) value >> 16;
a680de9a 1434
bb294208 1435 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
a680de9a
PB
1436 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1437 insn &= ~0x1fffc1;
3de43e7b 1438 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
a680de9a 1439 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
3de43e7b 1440 if (value + 0x8000 > 0xffff)
a680de9a
PB
1441 return bfd_reloc_overflow;
1442 return bfd_reloc_ok;
805fc799 1443}
5bd4f169 1444
2441e016
AM
1445static bfd_reloc_status_type
1446ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1447 void *data, asection *input_section,
1448 bfd *output_bfd, char **error_message)
1449{
1450 if (output_bfd != NULL)
1451 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1452 input_section, output_bfd, error_message);
1453
699733f6
AM
1454 if (strcmp (symbol->section->name, ".opd") == 0
1455 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
1456 {
1457 bfd_vma dest = opd_entry_value (symbol->section,
1458 symbol->value + reloc_entry->addend,
0a1b45a2 1459 NULL, NULL, false);
2441e016
AM
1460 if (dest != (bfd_vma) -1)
1461 reloc_entry->addend = dest - (symbol->value
1462 + symbol->section->output_section->vma
1463 + symbol->section->output_offset);
1464 }
810d4e75
AM
1465 else
1466 {
1467 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1468
1469 if (symbol->section->owner != abfd
9f284bf9 1470 && symbol->section->owner != NULL
810d4e75
AM
1471 && abiversion (symbol->section->owner) >= 2)
1472 {
1473 unsigned int i;
1474
1475 for (i = 0; i < symbol->section->owner->symcount; ++i)
1476 {
1477 asymbol *symdef = symbol->section->owner->outsymbols[i];
1478
1479 if (strcmp (symdef->name, symbol->name) == 0)
1480 {
1481 elfsym = (elf_symbol_type *) symdef;
1482 break;
1483 }
1484 }
1485 }
1486 reloc_entry->addend
1487 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1488 }
2441e016
AM
1489 return bfd_reloc_continue;
1490}
1491
805fc799 1492static bfd_reloc_status_type
4ce794b7
AM
1493ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1494 void *data, asection *input_section,
1495 bfd *output_bfd, char **error_message)
805fc799
AM
1496{
1497 long insn;
04c9666a 1498 enum elf_ppc64_reloc_type r_type;
805fc799 1499 bfd_size_type octets;
794e51c0 1500 /* Assume 'at' branch hints. */
0a1b45a2 1501 bool is_isa_v2 = true;
805fc799
AM
1502
1503 /* If this is a relocatable link (output_bfd test tells us), just
1504 call the generic function. Any adjustment will be done at final
1505 link time. */
5bd4f169 1506 if (output_bfd != NULL)
cedb70c5 1507 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1508 input_section, output_bfd, error_message);
1509
bb294208 1510 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
805fc799
AM
1511 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1512 insn &= ~(0x01 << 21);
4ce794b7 1513 r_type = reloc_entry->howto->type;
805fc799
AM
1514 if (r_type == R_PPC64_ADDR14_BRTAKEN
1515 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 1516 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 1517
794e51c0 1518 if (is_isa_v2)
5bd4f169 1519 {
805fc799
AM
1520 /* Set 'a' bit. This is 0b00010 in BO field for branch
1521 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1522 for branch on CTR insns (BO == 1a00t or 1a01t). */
1523 if ((insn & (0x14 << 21)) == (0x04 << 21))
1524 insn |= 0x02 << 21;
1525 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1526 insn |= 0x08 << 21;
1527 else
2441e016 1528 goto out;
5bd4f169 1529 }
805fc799
AM
1530 else
1531 {
1532 bfd_vma target = 0;
1533 bfd_vma from;
5bd4f169 1534
805fc799
AM
1535 if (!bfd_is_com_section (symbol->section))
1536 target = symbol->value;
1537 target += symbol->section->output_section->vma;
1538 target += symbol->section->output_offset;
1539 target += reloc_entry->addend;
5bd4f169 1540
805fc799
AM
1541 from = (reloc_entry->address
1542 + input_section->output_offset
1543 + input_section->output_section->vma);
5bd4f169 1544
805fc799
AM
1545 /* Invert 'y' bit if not the default. */
1546 if ((bfd_signed_vma) (target - from) < 0)
1547 insn ^= 0x01 << 21;
1548 }
4ce794b7 1549 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
1550 out:
1551 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1552 input_section, output_bfd, error_message);
805fc799 1553}
5bd4f169 1554
805fc799 1555static bfd_reloc_status_type
4ce794b7
AM
1556ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1557 void *data, asection *input_section,
1558 bfd *output_bfd, char **error_message)
805fc799
AM
1559{
1560 /* If this is a relocatable link (output_bfd test tells us), just
1561 call the generic function. Any adjustment will be done at final
1562 link time. */
1563 if (output_bfd != NULL)
cedb70c5 1564 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 1565 input_section, output_bfd, error_message);
5bd4f169 1566
805fc799
AM
1567 /* Subtract the symbol section base address. */
1568 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
1569 return bfd_reloc_continue;
1570}
1571
805fc799 1572static bfd_reloc_status_type
4ce794b7
AM
1573ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1574 void *data, asection *input_section,
1575 bfd *output_bfd, char **error_message)
805fc799
AM
1576{
1577 /* If this is a relocatable link (output_bfd test tells us), just
1578 call the generic function. Any adjustment will be done at final
1579 link time. */
1580 if (output_bfd != NULL)
cedb70c5 1581 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1582 input_section, output_bfd, error_message);
1583
1584 /* Subtract the symbol section base address. */
1585 reloc_entry->addend -= symbol->section->output_section->vma;
1586
1587 /* Adjust the addend for sign extension of the low 16 bits. */
1588 reloc_entry->addend += 0x8000;
1589 return bfd_reloc_continue;
1590}
1591
1592static bfd_reloc_status_type
4ce794b7
AM
1593ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1594 void *data, asection *input_section,
1595 bfd *output_bfd, char **error_message)
805fc799
AM
1596{
1597 bfd_vma TOCstart;
1598
1599 /* If this is a relocatable link (output_bfd test tells us), just
1600 call the generic function. Any adjustment will be done at final
1601 link time. */
1602 if (output_bfd != NULL)
cedb70c5 1603 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1604 input_section, output_bfd, error_message);
1605
1606 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1607 if (TOCstart == 0)
1c865ab2 1608 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1609
1610 /* Subtract the TOC base address. */
1611 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1612 return bfd_reloc_continue;
1613}
1614
1615static bfd_reloc_status_type
4ce794b7
AM
1616ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1617 void *data, asection *input_section,
1618 bfd *output_bfd, char **error_message)
805fc799
AM
1619{
1620 bfd_vma TOCstart;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
cedb70c5 1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1c865ab2 1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
1632
1633 /* Subtract the TOC base address. */
1634 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1635
1636 /* Adjust the addend for sign extension of the low 16 bits. */
1637 reloc_entry->addend += 0x8000;
1638 return bfd_reloc_continue;
1639}
1640
1641static bfd_reloc_status_type
4ce794b7
AM
1642ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1643 void *data, asection *input_section,
1644 bfd *output_bfd, char **error_message)
805fc799
AM
1645{
1646 bfd_vma TOCstart;
1647 bfd_size_type octets;
1648
1649 /* If this is a relocatable link (output_bfd test tells us), just
1650 call the generic function. Any adjustment will be done at final
1651 link time. */
1652 if (output_bfd != NULL)
cedb70c5 1653 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1654 input_section, output_bfd, error_message);
1655
1656 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1657 if (TOCstart == 0)
1c865ab2 1658 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799 1659
bb294208 1660 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
805fc799
AM
1661 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1662 return bfd_reloc_ok;
1663}
1664
5663e321
AM
1665static bfd_reloc_status_type
1666ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1667 void *data, asection *input_section,
1668 bfd *output_bfd, char **error_message)
1669{
1670 uint64_t insn;
1671 bfd_vma targ;
1672
1673 if (output_bfd != NULL)
1674 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1675 input_section, output_bfd, error_message);
1676
1677 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1678 insn <<= 32;
1679 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1680
1681 targ = (symbol->section->output_section->vma
1682 + symbol->section->output_offset
1683 + reloc_entry->addend);
1684 if (!bfd_is_com_section (symbol->section))
1685 targ += symbol->value;
1686 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1687 targ += 1ULL << 33;
1688 if (reloc_entry->howto->pc_relative)
1689 {
1690 bfd_vma from = (reloc_entry->address
1691 + input_section->output_offset
1692 + input_section->output_section->vma);
1693 targ -=from;
1694 }
1695 targ >>= reloc_entry->howto->rightshift;
1696 insn &= ~reloc_entry->howto->dst_mask;
1697 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1698 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1699 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1700 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1701 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1702 >= 1ULL << reloc_entry->howto->bitsize))
1703 return bfd_reloc_overflow;
1704 return bfd_reloc_ok;
1705}
1706
805fc799 1707static bfd_reloc_status_type
4ce794b7
AM
1708ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1709 void *data, asection *input_section,
1710 bfd *output_bfd, char **error_message)
805fc799
AM
1711{
1712 /* If this is a relocatable link (output_bfd test tells us), just
1713 call the generic function. Any adjustment will be done at final
1714 link time. */
1715 if (output_bfd != NULL)
cedb70c5 1716 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
1717 input_section, output_bfd, error_message);
1718
1719 if (error_message != NULL)
1720 {
7fbd5f4e
AM
1721 static char *message;
1722 free (message);
1723 if (asprintf (&message, _("generic linker can't handle %s"),
1724 reloc_entry->howto->name) < 0)
1725 message = NULL;
1726 *error_message = message;
805fc799
AM
1727 }
1728 return bfd_reloc_dangerous;
1729}
1730
927be08e
AM
1731/* Track GOT entries needed for a given symbol. We might need more
1732 than one got entry per symbol. */
1733struct got_entry
1734{
1735 struct got_entry *next;
1736
1737 /* The symbol addend that we'll be placing in the GOT. */
1738 bfd_vma addend;
1739
1740 /* Unlike other ELF targets, we use separate GOT entries for the same
1741 symbol referenced from different input files. This is to support
1742 automatic multiple TOC/GOT sections, where the TOC base can vary
1743 from one input file to another. After partitioning into TOC groups
1744 we merge entries within the group.
1745
1746 Point to the BFD owning this GOT entry. */
1747 bfd *owner;
1748
1749 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1750 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 1751 unsigned char tls_type;
927be08e
AM
1752
1753 /* Non-zero if got.ent points to real entry. */
f961d9dd 1754 unsigned char is_indirect;
927be08e
AM
1755
1756 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1757 union
2cdcc330
AM
1758 {
1759 bfd_signed_vma refcount;
1760 bfd_vma offset;
1761 struct got_entry *ent;
1762 } got;
927be08e
AM
1763};
1764
1765/* The same for PLT. */
1766struct plt_entry
1767{
1768 struct plt_entry *next;
1769
1770 bfd_vma addend;
1771
1772 union
2cdcc330
AM
1773 {
1774 bfd_signed_vma refcount;
1775 bfd_vma offset;
1776 } plt;
927be08e
AM
1777};
1778
e717da7e
AM
1779struct ppc64_elf_obj_tdata
1780{
1781 struct elf_obj_tdata elf;
1782
1783 /* Shortcuts to dynamic linker sections. */
1784 asection *got;
1785 asection *relgot;
1786
b3fac117
AM
1787 /* Used during garbage collection. We attach global symbols defined
1788 on removed .opd entries to this section so that the sym is removed. */
1789 asection *deleted_section;
81688140 1790
927be08e 1791 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 1792 sections means we potentially need one of these for each input bfd. */
927be08e 1793 struct got_entry tlsld_got;
8860955f 1794
2cdcc330
AM
1795 union
1796 {
729eabd5
AM
1797 /* A copy of relocs before they are modified for --emit-relocs. */
1798 Elf_Internal_Rela *relocs;
1799
1800 /* Section contents. */
1801 bfd_byte *contents;
1802 } opd;
d77c8a4b
AM
1803
1804 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1805 the reloc to be in the range -32768 to 32767. */
98528052
AM
1806 unsigned int has_small_toc_reloc : 1;
1807
560c8763
AM
1808 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1809 instruction not one we handle. */
1810 unsigned int unexpected_toc_insn : 1;
066f4018 1811
903b777d
AM
1812 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1813 this file. */
1814 unsigned int has_optrel : 1;
e717da7e
AM
1815};
1816
1817#define ppc64_elf_tdata(bfd) \
1818 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1819
1820#define ppc64_tlsld_got(bfd) \
1821 (&ppc64_elf_tdata (bfd)->tlsld_got)
1822
0c8d6e5c
AM
1823#define is_ppc64_elf(bfd) \
1824 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 1825 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 1826
e717da7e
AM
1827/* Override the generic function because we store some extras. */
1828
0a1b45a2 1829static bool
e717da7e
AM
1830ppc64_elf_mkobject (bfd *abfd)
1831{
0ffa91dd 1832 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 1833 PPC64_ELF_DATA);
e717da7e
AM
1834}
1835
feee612b 1836/* Fix bad default arch selected for a 64 bit input bfd when the
14b57c7c 1837 default is 32 bit. Also select arch based on apuinfo. */
feee612b 1838
0a1b45a2 1839static bool
4ce794b7 1840ppc64_elf_object_p (bfd *abfd)
feee612b 1841{
14b57c7c 1842 if (!abfd->arch_info->the_default)
0a1b45a2 1843 return true;
14b57c7c
AM
1844
1845 if (abfd->arch_info->bits_per_word == 32)
feee612b
AM
1846 {
1847 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1848
1849 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1850 {
1851 /* Relies on arch after 32 bit default being 64 bit default. */
1852 abfd->arch_info = abfd->arch_info->next;
1853 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1854 }
1855 }
14b57c7c 1856 return _bfd_elf_ppc_set_arch (abfd);
feee612b
AM
1857}
1858
d37c89e5
AM
1859/* Support for core dump NOTE sections. */
1860
0a1b45a2 1861static bool
d37c89e5
AM
1862ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1863{
eea6121a 1864 size_t offset, size;
d37c89e5
AM
1865
1866 if (note->descsz != 504)
0a1b45a2 1867 return false;
d37c89e5
AM
1868
1869 /* pr_cursig */
228e534f 1870 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
1871
1872 /* pr_pid */
228e534f 1873 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
1874
1875 /* pr_reg */
1876 offset = 112;
eea6121a 1877 size = 384;
d37c89e5
AM
1878
1879 /* Make a ".reg/999" section. */
1880 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 1881 size, note->descpos + offset);
d37c89e5
AM
1882}
1883
0a1b45a2 1884static bool
d37c89e5
AM
1885ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1886{
1887 if (note->descsz != 136)
0a1b45a2 1888 return false;
d37c89e5 1889
228e534f 1890 elf_tdata (abfd)->core->pid
bc989cdc 1891 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 1892 elf_tdata (abfd)->core->program
d37c89e5 1893 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 1894 elf_tdata (abfd)->core->command
d37c89e5
AM
1895 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1896
0a1b45a2 1897 return true;
d37c89e5
AM
1898}
1899
183e98be
AM
1900static char *
1901ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1902 ...)
1903{
1904 switch (note_type)
1905 {
1906 default:
1907 return NULL;
1908
1909 case NT_PRPSINFO:
1910 {
9ef6d1e3 1911 char data[136] ATTRIBUTE_NONSTRING;
183e98be
AM
1912 va_list ap;
1913
1914 va_start (ap, note_type);
75cd47ed 1915 memset (data, 0, sizeof (data));
183e98be 1916 strncpy (data + 40, va_arg (ap, const char *), 16);
be3e27bb 1917#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1918 DIAGNOSTIC_PUSH;
be3e27bb 1919 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
95da9854
L
1920 -Wstringop-truncation:
1921 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1922 */
95da9854
L
1923 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1924#endif
183e98be 1925 strncpy (data + 56, va_arg (ap, const char *), 80);
be3e27bb 1926#if GCC_VERSION == 8000 || GCC_VERSION == 8001
95da9854 1927 DIAGNOSTIC_POP;
fe75810f 1928#endif
183e98be
AM
1929 va_end (ap);
1930 return elfcore_write_note (abfd, buf, bufsiz,
1931 "CORE", note_type, data, sizeof (data));
1932 }
1933
1934 case NT_PRSTATUS:
1935 {
1936 char data[504];
1937 va_list ap;
1938 long pid;
1939 int cursig;
1940 const void *greg;
1941
1942 va_start (ap, note_type);
1943 memset (data, 0, 112);
1944 pid = va_arg (ap, long);
1945 bfd_put_32 (abfd, pid, data + 32);
1946 cursig = va_arg (ap, int);
1947 bfd_put_16 (abfd, cursig, data + 12);
1948 greg = va_arg (ap, const void *);
1949 memcpy (data + 112, greg, 384);
1950 memset (data + 496, 0, 8);
1951 va_end (ap);
1952 return elfcore_write_note (abfd, buf, bufsiz,
1953 "CORE", note_type, data, sizeof (data));
1954 }
1955 }
1956}
1957
5d35169e
AM
1958/* Add extra PPC sections. */
1959
2cdcc330 1960static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
7f4d3958 1961{
07d6d2b8
AM
1962 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1963 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1964 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
1965 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1966 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
0112cd26 1967 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 1968 { NULL, 0, 0, 0, 0 }
5d35169e
AM
1969};
1970
7c8fe5c4
AM
1971enum _ppc64_sec_type {
1972 sec_normal = 0,
1973 sec_opd = 1,
1974 sec_toc = 2
1975};
1976
f0abc2a1
AM
1977struct _ppc64_elf_section_data
1978{
1979 struct bfd_elf_section_data elf;
411e1bfb 1980
f0abc2a1
AM
1981 union
1982 {
51aecdc5
AM
1983 /* An array with one entry for each opd function descriptor,
1984 and some spares since opd entries may be either 16 or 24 bytes. */
1985#define OPD_NDX(OFF) ((OFF) >> 4)
74f0fb50
AM
1986 struct _opd_sec_data
1987 {
1988 /* Points to the function code section for local opd entries. */
1989 asection **func_sec;
1990
1991 /* After editing .opd, adjust references to opd local syms. */
1992 long *adjust;
1993 } opd;
7c8fe5c4 1994
3a71aa26
AM
1995 /* An array for toc sections, indexed by offset/8. */
1996 struct _toc_sec_data
1997 {
1998 /* Specifies the relocation symbol index used at a given toc offset. */
1999 unsigned *symndx;
2000
2001 /* And the relocation addend. */
2002 bfd_vma *add;
2003 } toc;
7c8fe5c4
AM
2004 } u;
2005
2006 enum _ppc64_sec_type sec_type:2;
411e1bfb 2007
7c8fe5c4
AM
2008 /* Flag set when small branches are detected. Used to
2009 select suitable defaults for the stub group size. */
2010 unsigned int has_14bit_branch:1;
3e04d765
AM
2011
2012 /* Flag set when PLTCALL relocs are detected. */
2013 unsigned int has_pltcall:1;
066f4018 2014
903b777d
AM
2015 /* Flag set when section has PLT/GOT/TOC relocations that can be
2016 optimised. */
2017 unsigned int has_optrel:1;
f0abc2a1
AM
2018};
2019
2020#define ppc64_elf_section_data(sec) \
411e1bfb 2021 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1 2022
0a1b45a2 2023static bool
4ce794b7 2024ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2025{
f592407e
AM
2026 if (!sec->used_by_bfd)
2027 {
2028 struct _ppc64_elf_section_data *sdata;
986f0783 2029 size_t amt = sizeof (*sdata);
f0abc2a1 2030
f592407e
AM
2031 sdata = bfd_zalloc (abfd, amt);
2032 if (sdata == NULL)
0a1b45a2 2033 return false;
f592407e
AM
2034 sec->used_by_bfd = sdata;
2035 }
f0abc2a1
AM
2036
2037 return _bfd_elf_new_section_hook (abfd, sec);
2038}
4025353c 2039
0a1b45a2 2040static bool
bf577467
AM
2041ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2042{
2043 const char *name = hdr->bfd_section->name;
2044
3f3328b8
ML
2045 if (startswith (name, ".sbss")
2046 || startswith (name, ".sdata"))
bf577467
AM
2047 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2048
0a1b45a2 2049 return true;
bf577467
AM
2050}
2051
74f0fb50 2052static struct _opd_sec_data *
4025353c
AM
2053get_opd_info (asection * sec)
2054{
2055 if (sec != NULL
2056 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2057 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2058 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2059 return NULL;
2060}
90e3cdf2
JJ
2061\f
2062/* Parameters for the qsort hook. */
0a1b45a2 2063static bool synthetic_relocatable;
8cb1c2c8 2064static const asection *synthetic_opd;
90e3cdf2 2065
699733f6 2066/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2067
2068static int
2069compare_symbols (const void *ap, const void *bp)
2070{
2cdcc330
AM
2071 const asymbol *a = *(const asymbol **) ap;
2072 const asymbol *b = *(const asymbol **) bp;
90e3cdf2 2073
699733f6
AM
2074 /* Section symbols first. */
2075 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2076 return -1;
699733f6 2077 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2078 return 1;
2079
699733f6 2080 /* then .opd symbols. */
cd285db5
AM
2081 if (synthetic_opd != NULL)
2082 {
2083 if (strcmp (a->section->name, ".opd") == 0
2084 && strcmp (b->section->name, ".opd") != 0)
2085 return -1;
2086 if (strcmp (a->section->name, ".opd") != 0
2087 && strcmp (b->section->name, ".opd") == 0)
2088 return 1;
2089 }
90e3cdf2 2090
699733f6 2091 /* then other code symbols. */
2cdcc330
AM
2092 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2093 == (SEC_CODE | SEC_ALLOC))
2094 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2095 != (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2096 return -1;
2097
2cdcc330
AM
2098 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2099 != (SEC_CODE | SEC_ALLOC))
2100 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2101 == (SEC_CODE | SEC_ALLOC)))
90e3cdf2
JJ
2102 return 1;
2103
2104 if (synthetic_relocatable)
2105 {
2106 if (a->section->id < b->section->id)
2107 return -1;
2108
2109 if (a->section->id > b->section->id)
2110 return 1;
2111 }
2112
2113 if (a->value + a->section->vma < b->value + b->section->vma)
2114 return -1;
2115
2116 if (a->value + a->section->vma > b->value + b->section->vma)
2117 return 1;
2118
4d35a0aa
AM
2119 /* For syms with the same value, prefer strong dynamic global function
2120 syms over other syms. */
2121 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2122 return -1;
2123
2124 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2125 return 1;
2126
2127 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2128 return -1;
2129
2130 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2131 return 1;
2132
2133 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2134 return -1;
2135
2136 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2137 return 1;
2138
2139 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2140 return -1;
2141
2142 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2143 return 1;
2144
dcea6a95
AM
2145 /* Finally, sort on where the symbol is in memory. The symbols will
2146 be in at most two malloc'd blocks, one for static syms, one for
2147 dynamic syms, and we distinguish the two blocks above by testing
2148 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2149 originally in the same order as the symbols (and we're not
2150 sorting the symbols themselves), this ensures a stable sort. */
2151 if (a < b)
2152 return -1;
2153 if (a > b)
2154 return 1;
2155 return 0;
90e3cdf2
JJ
2156}
2157
699733f6 2158/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2159
699733f6 2160static asymbol *
9ad9b810
AM
2161sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2162 bfd_vma value)
90e3cdf2 2163{
9ad9b810 2164 size_t mid;
90e3cdf2 2165
7292b3ac 2166 if (id == (unsigned) -1)
699733f6
AM
2167 {
2168 while (lo < hi)
2169 {
2170 mid = (lo + hi) >> 1;
2171 if (syms[mid]->value + syms[mid]->section->vma < value)
2172 lo = mid + 1;
2173 else if (syms[mid]->value + syms[mid]->section->vma > value)
2174 hi = mid;
2175 else
2176 return syms[mid];
2177 }
2178 }
2179 else
2180 {
2181 while (lo < hi)
2182 {
2183 mid = (lo + hi) >> 1;
2184 if (syms[mid]->section->id < id)
2185 lo = mid + 1;
2186 else if (syms[mid]->section->id > id)
2187 hi = mid;
2188 else if (syms[mid]->value < value)
2189 lo = mid + 1;
2190 else if (syms[mid]->value > value)
2191 hi = mid;
2192 else
2193 return syms[mid];
2194 }
2195 }
2196 return NULL;
90e3cdf2
JJ
2197}
2198
0a1b45a2 2199static bool
468392fb
AM
2200section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2201{
2202 bfd_vma vma = *(bfd_vma *) ptr;
2203 return ((section->flags & SEC_ALLOC) != 0
2204 && section->vma <= vma
2205 && vma < section->vma + section->size);
2206}
2207
699733f6 2208/* Create synthetic symbols, effectively restoring "dot-symbol" function
c4b0b099
AM
2209 entry syms. Also generate @plt symbols for the glink branch table.
2210 Returns count of synthetic symbols in RET or -1 on error. */
90e3cdf2
JJ
2211
2212static long
a7535cf3
AM
2213ppc64_elf_get_synthetic_symtab (bfd *abfd,
2214 long static_count, asymbol **static_syms,
2215 long dyn_count, asymbol **dyn_syms,
c9727e01 2216 asymbol **ret)
90e3cdf2
JJ
2217{
2218 asymbol *s;
0ccf57bd 2219 size_t i, j, count;
90e3cdf2 2220 char *names;
0ccf57bd 2221 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 2222 asection *opd = NULL;
0a1b45a2 2223 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2224 asymbol **syms;
ee67d69a 2225 int abi = abiversion (abfd);
90e3cdf2
JJ
2226
2227 *ret = NULL;
2228
ee67d69a
AM
2229 if (abi < 2)
2230 {
2231 opd = bfd_get_section_by_name (abfd, ".opd");
2232 if (opd == NULL && abi == 1)
2233 return 0;
2234 }
90e3cdf2 2235
a5259595
AM
2236 syms = NULL;
2237 codesecsym = 0;
2238 codesecsymend = 0;
2239 secsymend = 0;
2240 opdsymend = 0;
2241 symcount = 0;
2242 if (opd != NULL)
c9727e01 2243 {
a5259595
AM
2244 symcount = static_count;
2245 if (!relocatable)
2246 symcount += dyn_count;
2247 if (symcount == 0)
2248 return 0;
c9727e01 2249
a5259595
AM
2250 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2251 if (syms == NULL)
2252 return -1;
90e3cdf2 2253
a5259595
AM
2254 if (!relocatable && static_count != 0 && dyn_count != 0)
2255 {
2256 /* Use both symbol tables. */
2257 memcpy (syms, static_syms, static_count * sizeof (*syms));
2258 memcpy (syms + static_count, dyn_syms,
2259 (dyn_count + 1) * sizeof (*syms));
2260 }
2261 else if (!relocatable && static_count == 0)
2262 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2263 else
2264 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
90e3cdf2 2265
0ccf57bd
AM
2266 /* Trim uninteresting symbols. Interesting symbols are section,
2267 function, and notype symbols. */
2268 for (i = 0, j = 0; i < symcount; ++i)
2269 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2270 | BSF_RELC | BSF_SRELC)) == 0)
2271 syms[j++] = syms[i];
2272 symcount = j;
2273
a5259595
AM
2274 synthetic_relocatable = relocatable;
2275 synthetic_opd = opd;
2276 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2277
a5259595
AM
2278 if (!relocatable && symcount > 1)
2279 {
bfa5bd2a
PA
2280 /* Trim duplicate syms, since we may have merged the normal
2281 and dynamic symbols. Actually, we only care about syms
2282 that have different values, so trim any with the same
2283 value. Don't consider ifunc and ifunc resolver symbols
2284 duplicates however, because GDB wants to know whether a
2285 text symbol is an ifunc resolver. */
a5259595 2286 for (i = 1, j = 1; i < symcount; ++i)
bfa5bd2a
PA
2287 {
2288 const asymbol *s0 = syms[i - 1];
2289 const asymbol *s1 = syms[i];
2290
2291 if ((s0->value + s0->section->vma
2292 != s1->value + s1->section->vma)
2293 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2294 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2295 syms[j++] = syms[i];
2296 }
a5259595
AM
2297 symcount = j;
2298 }
699733f6 2299
a5259595
AM
2300 i = 0;
2301 /* Note that here and in compare_symbols we can't compare opd and
2302 sym->section directly. With separate debug info files, the
2303 symbols will be extracted from the debug file while abfd passed
2304 to this function is the real binary. */
8bd10d6b
AM
2305 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2306 && strcmp (syms[i]->section->name, ".opd") == 0)
a5259595
AM
2307 ++i;
2308 codesecsym = i;
2309
2310 for (; i < symcount; ++i)
2311 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2312 | SEC_THREAD_LOCAL))
2313 != (SEC_CODE | SEC_ALLOC))
2314 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2315 break;
2316 codesecsymend = i;
2317
2318 for (; i < symcount; ++i)
2319 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2320 break;
2321 secsymend = i;
2322
2323 for (; i < symcount; ++i)
2324 if (strcmp (syms[i]->section->name, ".opd") != 0)
2325 break;
2326 opdsymend = i;
2327
2328 for (; i < symcount; ++i)
2cdcc330
AM
2329 if (((syms[i]->section->flags
2330 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
a5259595
AM
2331 != (SEC_CODE | SEC_ALLOC))
2332 break;
2333 symcount = i;
2334 }
c9727e01 2335 count = 0;
90e3cdf2 2336
699733f6 2337 if (relocatable)
90e3cdf2 2338 {
0a1b45a2 2339 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
699733f6
AM
2340 arelent *r;
2341 size_t size;
0ccf57bd 2342 size_t relcount;
90e3cdf2 2343
468392fb
AM
2344 if (opdsymend == secsymend)
2345 goto done;
2346
699733f6 2347 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2348 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2349 if (relcount == 0)
c9727e01 2350 goto done;
90e3cdf2 2351
0a1b45a2 2352 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
7356fed5
AM
2353 {
2354 count = -1;
2355 goto done;
2356 }
2357
699733f6 2358 size = 0;
595da8c5 2359 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2360 {
2361 asymbol *sym;
90e3cdf2 2362
595da8c5 2363 while (r < opd->relocation + relcount
699733f6
AM
2364 && r->address < syms[i]->value + opd->vma)
2365 ++r;
90e3cdf2 2366
595da8c5 2367 if (r == opd->relocation + relcount)
699733f6 2368 break;
90e3cdf2 2369
699733f6
AM
2370 if (r->address != syms[i]->value + opd->vma)
2371 continue;
90e3cdf2 2372
699733f6
AM
2373 if (r->howto->type != R_PPC64_ADDR64)
2374 continue;
90e3cdf2 2375
699733f6
AM
2376 sym = *r->sym_ptr_ptr;
2377 if (!sym_exists_at (syms, opdsymend, symcount,
2378 sym->section->id, sym->value + r->addend))
2379 {
2380 ++count;
2381 size += sizeof (asymbol);
2382 size += strlen (syms[i]->name) + 2;
2383 }
2384 }
90e3cdf2 2385
c4b0b099
AM
2386 if (size == 0)
2387 goto done;
699733f6
AM
2388 s = *ret = bfd_malloc (size);
2389 if (s == NULL)
2390 {
7356fed5 2391 count = -1;
c9727e01 2392 goto done;
699733f6 2393 }
90e3cdf2 2394
699733f6 2395 names = (char *) (s + count);
90e3cdf2 2396
595da8c5 2397 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2398 {
699733f6 2399 asymbol *sym;
90e3cdf2 2400
595da8c5 2401 while (r < opd->relocation + relcount
699733f6
AM
2402 && r->address < syms[i]->value + opd->vma)
2403 ++r;
90e3cdf2 2404
595da8c5 2405 if (r == opd->relocation + relcount)
699733f6
AM
2406 break;
2407
2408 if (r->address != syms[i]->value + opd->vma)
2409 continue;
2410
2411 if (r->howto->type != R_PPC64_ADDR64)
2412 continue;
90e3cdf2 2413
699733f6
AM
2414 sym = *r->sym_ptr_ptr;
2415 if (!sym_exists_at (syms, opdsymend, symcount,
2416 sym->section->id, sym->value + r->addend))
2417 {
2418 size_t len;
2419
2420 *s = *syms[i];
6ba2a415 2421 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2422 s->section = sym->section;
2423 s->value = sym->value + r->addend;
2424 s->name = names;
2425 *names++ = '.';
2426 len = strlen (syms[i]->name);
2427 memcpy (names, syms[i]->name, len + 1);
2428 names += len + 1;
6f610d07
UW
2429 /* Have udata.p point back to the original symbol this
2430 synthetic symbol was derived from. */
2431 s->udata.p = syms[i];
699733f6
AM
2432 s++;
2433 }
2434 }
2435 }
2436 else
90e3cdf2 2437 {
0a1b45a2 2438 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
ee67d69a 2439 bfd_byte *contents = NULL;
699733f6 2440 size_t size;
0ccf57bd 2441 size_t plt_count = 0;
468392fb
AM
2442 bfd_vma glink_vma = 0, resolv_vma = 0;
2443 asection *dynamic, *glink = NULL, *relplt = NULL;
2444 arelent *p;
90e3cdf2 2445
ee67d69a 2446 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 2447 {
c4b0b099
AM
2448 free_contents_and_exit_err:
2449 count = -1;
ee67d69a 2450 free_contents_and_exit:
c9594989 2451 free (contents);
c9727e01 2452 goto done;
699733f6 2453 }
90e3cdf2 2454
699733f6
AM
2455 size = 0;
2456 for (i = secsymend; i < opdsymend; ++i)
2457 {
2458 bfd_vma ent;
90e3cdf2 2459
5ef11c02
AM
2460 /* Ignore bogus symbols. */
2461 if (syms[i]->value > opd->size - 8)
2462 continue;
2463
699733f6
AM
2464 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2465 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2466 {
2467 ++count;
2468 size += sizeof (asymbol);
2469 size += strlen (syms[i]->name) + 2;
2470 }
2471 }
90e3cdf2 2472
468392fb 2473 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
2474 if (dyn_count != 0
2475 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
2476 {
2477 bfd_byte *dynbuf, *extdyn, *extdynend;
2478 size_t extdynsize;
2479 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2480
2481 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
c4b0b099 2482 goto free_contents_and_exit_err;
468392fb
AM
2483
2484 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2485 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2486
2487 extdyn = dynbuf;
2488 extdynend = extdyn + dynamic->size;
2489 for (; extdyn < extdynend; extdyn += extdynsize)
2490 {
2491 Elf_Internal_Dyn dyn;
2492 (*swap_dyn_in) (abfd, extdyn, &dyn);
2493
2494 if (dyn.d_tag == DT_NULL)
2495 break;
2496
2497 if (dyn.d_tag == DT_PPC64_GLINK)
2498 {
9e390558
AM
2499 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2500 See comment in ppc64_elf_finish_dynamic_sections. */
2501 glink_vma = dyn.d_un.d_val + 8 * 4;
468392fb
AM
2502 /* The .glink section usually does not survive the final
2503 link; search for the section (usually .text) where the
2504 glink stubs now reside. */
2505 glink = bfd_sections_find_if (abfd, section_covers_vma,
2506 &glink_vma);
2507 break;
2508 }
2509 }
2510
2511 free (dynbuf);
2512 }
2513
2514 if (glink != NULL)
2515 {
2516 /* Determine __glink trampoline by reading the relative branch
2517 from the first glink stub. */
2518 bfd_byte buf[4];
b9e5796b
AM
2519 unsigned int off = 0;
2520
2521 while (bfd_get_section_contents (abfd, glink, buf,
2522 glink_vma + off - glink->vma, 4))
468392fb
AM
2523 {
2524 unsigned int insn = bfd_get_32 (abfd, buf);
2525 insn ^= B_DOT;
2526 if ((insn & ~0x3fffffc) == 0)
b9e5796b 2527 {
2cdcc330
AM
2528 resolv_vma
2529 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
b9e5796b
AM
2530 break;
2531 }
2532 off += 4;
2533 if (off > 4)
2534 break;
468392fb
AM
2535 }
2536
2537 if (resolv_vma)
2538 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 2539
066ee829
AM
2540 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2541 if (relplt != NULL)
2542 {
2543 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 2544 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
c4b0b099 2545 goto free_contents_and_exit_err;
68ffbac6 2546
066ee829
AM
2547 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2548 size += plt_count * sizeof (asymbol);
468392fb 2549
066ee829
AM
2550 p = relplt->relocation;
2551 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
2552 {
2553 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2554 if (p->addend != 0)
2555 size += sizeof ("+0x") - 1 + 16;
2556 }
066ee829 2557 }
468392fb
AM
2558 }
2559
c4b0b099
AM
2560 if (size == 0)
2561 goto free_contents_and_exit;
699733f6
AM
2562 s = *ret = bfd_malloc (size);
2563 if (s == NULL)
c4b0b099 2564 goto free_contents_and_exit_err;
90e3cdf2 2565
468392fb 2566 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 2567
699733f6 2568 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2569 {
699733f6 2570 bfd_vma ent;
90e3cdf2 2571
5ef11c02
AM
2572 if (syms[i]->value > opd->size - 8)
2573 continue;
2574
699733f6
AM
2575 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2576 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2577 {
0ccf57bd 2578 size_t lo, hi;
699733f6 2579 size_t len;
c9727e01 2580 asection *sec = abfd->sections;
90e3cdf2 2581
699733f6
AM
2582 *s = *syms[i];
2583 lo = codesecsym;
2584 hi = codesecsymend;
2585 while (lo < hi)
2586 {
0ccf57bd 2587 size_t mid = (lo + hi) >> 1;
699733f6
AM
2588 if (syms[mid]->section->vma < ent)
2589 lo = mid + 1;
2590 else if (syms[mid]->section->vma > ent)
2591 hi = mid;
2592 else
c9727e01
AM
2593 {
2594 sec = syms[mid]->section;
2595 break;
2596 }
699733f6
AM
2597 }
2598
c9727e01 2599 if (lo >= hi && lo > codesecsym)
699733f6 2600 sec = syms[lo - 1]->section;
699733f6
AM
2601
2602 for (; sec != NULL; sec = sec->next)
2603 {
2604 if (sec->vma > ent)
2605 break;
63524580
JK
2606 /* SEC_LOAD may not be set if SEC is from a separate debug
2607 info file. */
2608 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
2609 break;
2610 if ((sec->flags & SEC_CODE) != 0)
2611 s->section = sec;
2612 }
6ba2a415 2613 s->flags |= BSF_SYNTHETIC;
699733f6
AM
2614 s->value = ent - s->section->vma;
2615 s->name = names;
2616 *names++ = '.';
2617 len = strlen (syms[i]->name);
2618 memcpy (names, syms[i]->name, len + 1);
2619 names += len + 1;
6f610d07
UW
2620 /* Have udata.p point back to the original symbol this
2621 synthetic symbol was derived from. */
2622 s->udata.p = syms[i];
699733f6 2623 s++;
90e3cdf2 2624 }
90e3cdf2 2625 }
699733f6 2626 free (contents);
468392fb
AM
2627
2628 if (glink != NULL && relplt != NULL)
2629 {
2630 if (resolv_vma)
2631 {
2632 /* Add a symbol for the main glink trampoline. */
86a4952b 2633 memset (s, 0, sizeof *s);
468392fb 2634 s->the_bfd = abfd;
6ba2a415 2635 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
2636 s->section = glink;
2637 s->value = resolv_vma - glink->vma;
2638 s->name = names;
2cdcc330
AM
2639 memcpy (names, "__glink_PLTresolve",
2640 sizeof ("__glink_PLTresolve"));
468392fb
AM
2641 names += sizeof ("__glink_PLTresolve");
2642 s++;
2643 count++;
2644 }
2645
2646 /* FIXME: It would be very much nicer to put sym@plt on the
2647 stub rather than on the glink branch table entry. The
2648 objdump disassembler would then use a sensible symbol
2649 name on plt calls. The difficulty in doing so is
2650 a) finding the stubs, and,
2651 b) matching stubs against plt entries, and,
2652 c) there can be multiple stubs for a given plt entry.
2653
2654 Solving (a) could be done by code scanning, but older
2655 ppc64 binaries used different stubs to current code.
2656 (b) is the tricky one since you need to known the toc
2657 pointer for at least one function that uses a pic stub to
2658 be able to calculate the plt address referenced.
2659 (c) means gdb would need to set multiple breakpoints (or
2660 find the glink branch itself) when setting breakpoints
2661 for pending shared library loads. */
2662 p = relplt->relocation;
2663 for (i = 0; i < plt_count; i++, p++)
2664 {
2665 size_t len;
2666
2667 *s = **p->sym_ptr_ptr;
2668 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2669 we are defining a symbol, ensure one of them is set. */
2670 if ((s->flags & BSF_LOCAL) == 0)
2671 s->flags |= BSF_GLOBAL;
6ba2a415 2672 s->flags |= BSF_SYNTHETIC;
468392fb
AM
2673 s->section = glink;
2674 s->value = glink_vma - glink->vma;
2675 s->name = names;
2676 s->udata.p = NULL;
2677 len = strlen ((*p->sym_ptr_ptr)->name);
2678 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2679 names += len;
e054468f
AM
2680 if (p->addend != 0)
2681 {
2682 memcpy (names, "+0x", sizeof ("+0x") - 1);
2683 names += sizeof ("+0x") - 1;
2684 bfd_sprintf_vma (abfd, names, p->addend);
2685 names += strlen (names);
2686 }
468392fb
AM
2687 memcpy (names, "@plt", sizeof ("@plt"));
2688 names += sizeof ("@plt");
2689 s++;
b9e5796b
AM
2690 if (abi < 2)
2691 {
2692 glink_vma += 8;
2693 if (i >= 0x8000)
2694 glink_vma += 4;
2695 }
2696 else
468392fb
AM
2697 glink_vma += 4;
2698 }
2699 count += plt_count;
2700 }
90e3cdf2
JJ
2701 }
2702
c9727e01 2703 done:
a7535cf3 2704 free (syms);
90e3cdf2
JJ
2705 return count;
2706}
5bd4f169 2707\f
65f38f15
AM
2708/* The following functions are specific to the ELF linker, while
2709 functions above are used generally. Those named ppc64_elf_* are
2710 called by the main ELF linker code. They appear in this file more
2711 or less in the order in which they are called. eg.
2712 ppc64_elf_check_relocs is called early in the link process,
2713 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2714 called.
2715
2716 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2717 functions have both a function code symbol and a function descriptor
2718 symbol. A call to foo in a relocatable object file looks like:
2719
2720 . .text
2721 . x:
2722 . bl .foo
2723 . nop
2724
2725 The function definition in another object file might be:
2726
2727 . .section .opd
2728 . foo: .quad .foo
2729 . .quad .TOC.@tocbase
2730 . .quad 0
2731 .
2732 . .text
2733 . .foo: blr
2734
2735 When the linker resolves the call during a static link, the branch
2736 unsurprisingly just goes to .foo and the .opd information is unused.
2737 If the function definition is in a shared library, things are a little
2738 different: The call goes via a plt call stub, the opd information gets
2739 copied to the plt, and the linker patches the nop.
2740
2741 . x:
2742 . bl .foo_stub
2743 . ld 2,40(1)
2744 .
2745 .
2746 . .foo_stub:
71a39c98
AM
2747 . std 2,40(1) # in practice, the call stub
2748 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2749 . addi 11,11,Lfoo@toc@l # this is the general idea
2750 . ld 12,0(11)
2751 . ld 2,8(11)
2752 . mtctr 12
2753 . ld 11,16(11)
e86ce104
AM
2754 . bctr
2755 .
2756 . .section .plt
2757 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2758
2759 The "reloc ()" notation is supposed to indicate that the linker emits
2760 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2761 copying.
2762
2763 What are the difficulties here? Well, firstly, the relocations
2764 examined by the linker in check_relocs are against the function code
2765 sym .foo, while the dynamic relocation in the plt is emitted against
2766 the function descriptor symbol, foo. Somewhere along the line, we need
2767 to carefully copy dynamic link information from one symbol to the other.
2768 Secondly, the generic part of the elf linker will make .foo a dynamic
2769 symbol as is normal for most other backends. We need foo dynamic
2770 instead, at least for an application final link. However, when
2771 creating a shared library containing foo, we need to have both symbols
2772 dynamic so that references to .foo are satisfied during the early
2773 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
2774 definition from some other object, eg. a static library.
2775
2776 Update: As of August 2004, we support a new convention. Function
2777 calls may use the function descriptor symbol, ie. "bl foo". This
2778 behaves exactly as "bl .foo". */
65f38f15 2779
7c8bbca5
AM
2780/* Of those relocs that might be copied as dynamic relocs, this
2781 function selects those that must be copied when linking a shared
2782 library or PIE, even when the symbol is local. */
65f38f15 2783
1d483afe
AM
2784static int
2785must_be_dyn_reloc (struct bfd_link_info *info,
2786 enum elf_ppc64_reloc_type r_type)
2787{
2788 switch (r_type)
2789 {
2790 default:
7c8bbca5
AM
2791 /* Only relative relocs can be resolved when the object load
2792 address isn't fixed. DTPREL64 is excluded because the
2793 dynamic linker needs to differentiate global dynamic from
2794 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
1d483afe
AM
2795 return 1;
2796
2797 case R_PPC64_REL32:
2798 case R_PPC64_REL64:
2799 case R_PPC64_REL30:
1bdd8fac
AM
2800 case R_PPC64_TOC16:
2801 case R_PPC64_TOC16_DS:
2802 case R_PPC64_TOC16_LO:
2803 case R_PPC64_TOC16_HI:
2804 case R_PPC64_TOC16_HA:
2805 case R_PPC64_TOC16_LO_DS:
1d483afe
AM
2806 return 0;
2807
2808 case R_PPC64_TPREL16:
2809 case R_PPC64_TPREL16_LO:
2810 case R_PPC64_TPREL16_HI:
2811 case R_PPC64_TPREL16_HA:
2812 case R_PPC64_TPREL16_DS:
2813 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
2814 case R_PPC64_TPREL16_HIGH:
2815 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
2816 case R_PPC64_TPREL16_HIGHER:
2817 case R_PPC64_TPREL16_HIGHERA:
2818 case R_PPC64_TPREL16_HIGHEST:
2819 case R_PPC64_TPREL16_HIGHESTA:
2820 case R_PPC64_TPREL64:
c213164a 2821 case R_PPC64_TPREL34:
7c8bbca5
AM
2822 /* These relocations are relative but in a shared library the
2823 linker doesn't know the thread pointer base. */
2824 return bfd_link_dll (info);
1d483afe
AM
2825 }
2826}
65f38f15 2827
f4656909 2828/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
e1c6cf61 2829 copying dynamic variables from a shared lib into an app's .dynbss
f4656909 2830 section, and instead use a dynamic relocation to point into the
e1c6cf61
AM
2831 shared lib. With code that gcc generates it is vital that this be
2832 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2833 actually the address of a function descriptor which resides in the
2834 .opd section. gcc uses the descriptor directly rather than going
2835 via the GOT as some other ABIs do, which means that initialized
2836 function pointers reference the descriptor. Thus, a function
2837 pointer initialized to the address of a function in a shared
2838 library will either require a .dynbss copy and a copy reloc, or a
2839 dynamic reloc. Using a .dynbss copy redefines the function
2840 descriptor symbol to point to the copy. This presents a problem as
2841 a PLT entry for that function is also initialized from the function
2842 descriptor symbol and the copy may not be initialized first. */
a23b6845 2843#define ELIMINATE_COPY_RELOCS 1
f4656909 2844
721956f4 2845/* Section name for stubs is the associated section name plus this
29942be8
NC
2846 string. */
2847#define STUB_SUFFIX ".stub"
721956f4
AM
2848
2849/* Linker stubs.
2850 ppc_stub_long_branch:
2851 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2852 destination, but a 24 bit branch in a stub section will reach.
2853 . b dest
2854
2855 ppc_stub_plt_branch:
2856 Similar to the above, but a 24 bit branch in the stub section won't
2857 reach its destination.
6df4c9c2
AM
2858 . addis %r12,%r2,xxx@toc@ha
2859 . ld %r12,xxx@toc@l(%r12)
71a39c98 2860 . mtctr %r12
721956f4
AM
2861 . bctr
2862
2863 ppc_stub_plt_call:
2c66dc6c
AM
2864 Used to call a function in a shared library. If it so happens that
2865 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 2866 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
05d0e962 2867 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
71a39c98
AM
2868 . addis %r11,%r2,xxx@toc@ha
2869 . ld %r12,xxx+0@toc@l(%r11)
2870 . mtctr %r12
2871 . ld %r2,xxx+8@toc@l(%r11)
2872 . ld %r11,xxx+16@toc@l(%r11)
721956f4 2873 . bctr
ad8e1ba5
AM
2874
2875 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2876 code to adjust the value and save r2 to support multiple toc sections.
2877 A ppc_stub_long_branch with an r2 offset looks like:
2878 . std %r2,40(%r1)
2879 . addis %r2,%r2,off@ha
2880 . addi %r2,%r2,off@l
2881 . b dest
2882
2883 A ppc_stub_plt_branch with an r2 offset looks like:
2884 . std %r2,40(%r1)
6df4c9c2
AM
2885 . addis %r12,%r2,xxx@toc@ha
2886 . ld %r12,xxx@toc@l(%r12)
ad8e1ba5
AM
2887 . addis %r2,%r2,off@ha
2888 . addi %r2,%r2,off@l
71a39c98 2889 . mtctr %r12
ad8e1ba5 2890 . bctr
ac2df442 2891
05d0e962
AM
2892 All of the above stubs are shown as their ELFv1 variants. ELFv2
2893 variants exist too, simpler for plt calls since a new toc pointer
2894 and static chain are not loaded by the stub. In addition, ELFv2
2895 has some more complex stubs to handle calls marked with NOTOC
2896 relocs from functions where r2 is not a valid toc pointer. These
2897 come in two flavours, the ones shown below, and _both variants that
2898 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2899 one call is from a function where r2 is used as the toc pointer but
2900 needs a toc adjusting stub for small-model multi-toc, and another
2901 call is from a function where r2 is not valid.
2902 ppc_stub_long_branch_notoc:
2903 . mflr %r12
2904 . bcl 20,31,1f
2905 . 1:
2906 . mflr %r11
2907 . mtlr %r12
2cdcc330
AM
2908 . addis %r12,%r11,dest-1b@ha
2909 . addi %r12,%r12,dest-1b@l
05d0e962
AM
2910 . b dest
2911
2912 ppc_stub_plt_branch_notoc:
2913 . mflr %r12
2914 . bcl 20,31,1f
2915 . 1:
2916 . mflr %r11
2917 . mtlr %r12
2918 . lis %r12,xxx-1b@highest
f891966f 2919 . ori %r12,%r12,xxx-1b@higher
05d0e962 2920 . sldi %r12,%r12,32
f891966f 2921 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2922 . ori %r12,%r12,xxx-1b@l
2923 . add %r12,%r11,%r12
2924 . mtctr %r12
2925 . bctr
2926
2927 ppc_stub_plt_call_notoc:
2928 . mflr %r12
2929 . bcl 20,31,1f
2930 . 1:
2931 . mflr %r11
2932 . mtlr %r12
2933 . lis %r12,xxx-1b@highest
f891966f 2934 . ori %r12,%r12,xxx-1b@higher
05d0e962 2935 . sldi %r12,%r12,32
f891966f 2936 . oris %r12,%r12,xxx-1b@high
05d0e962
AM
2937 . ori %r12,%r12,xxx-1b@l
2938 . ldx %r12,%r11,%r12
2939 . mtctr %r12
2940 . bctr
2941
7c1f4227 2942 There are also ELFv1 power10 variants of these stubs.
04bdff6a 2943 ppc_stub_long_branch_notoc:
d4b87b1e 2944 . pla %r12,dest@pcrel
04bdff6a
AM
2945 . b dest
2946 ppc_stub_plt_branch_notoc:
2947 . lis %r11,(dest-1f)@highesta34
2948 . ori %r11,%r11,(dest-1f)@highera34
2949 . sldi %r11,%r11,34
d4b87b1e 2950 . 1: pla %r12,dest@pcrel
04bdff6a
AM
2951 . add %r12,%r11,%r12
2952 . mtctr %r12
2953 . bctr
2954 ppc_stub_plt_call_notoc:
2955 . lis %r11,(xxx-1f)@highesta34
2956 . ori %r11,%r11,(xxx-1f)@highera34
2957 . sldi %r11,%r11,34
d4b87b1e 2958 . 1: pla %r12,xxx@pcrel
04bdff6a
AM
2959 . ldx %r12,%r11,%r12
2960 . mtctr %r12
2961 . bctr
2962
05d0e962
AM
2963 In cases where the high instructions would add zero, they are
2964 omitted and following instructions modified in some cases.
7c1f4227 2965 For example, a power10 ppc_stub_plt_call_notoc might simplify down
04bdff6a
AM
2966 to
2967 . pld %r12,xxx@pcrel
2968 . mtctr %r12
2969 . bctr
05d0e962
AM
2970
2971 For a given stub group (a set of sections all using the same toc
2972 pointer value) there will be just one stub type used for any
2973 particular function symbol. For example, if printf is called from
2974 code with the tocsave optimization (ie. r2 saved in function
2975 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2976 and from other code without the tocsave optimization requiring a
2977 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2978 type will be created. Calls with the tocsave optimization will
2979 enter this stub after the instruction saving r2. A similar
2980 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2981 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2982 to call an external function like printf. If other calls to printf
2983 require a ppc_stub_plt_call linkage stub then a single
2984 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2985 call. If other calls to printf require a ppc_stub_plt_call_r2save
2986 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2987 be created and calls not requiring r2 to be saved will enter the
2988 stub after the r2 save instruction. There is an analogous
2989 hierarchy of long branch and plt branch stubs for local call
2990 linkage. */
721956f4 2991
2cdcc330
AM
2992enum ppc_stub_type
2993{
721956f4
AM
2994 ppc_stub_none,
2995 ppc_stub_long_branch,
ad8e1ba5 2996 ppc_stub_long_branch_r2off,
05d0e962
AM
2997 ppc_stub_long_branch_notoc,
2998 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
721956f4 2999 ppc_stub_plt_branch,
ad8e1ba5 3000 ppc_stub_plt_branch_r2off,
05d0e962
AM
3001 ppc_stub_plt_branch_notoc,
3002 ppc_stub_plt_branch_both,
794e51c0 3003 ppc_stub_plt_call,
7341d5e2 3004 ppc_stub_plt_call_r2save,
05d0e962
AM
3005 ppc_stub_plt_call_notoc,
3006 ppc_stub_plt_call_both,
a4b6fadd
AM
3007 ppc_stub_global_entry,
3008 ppc_stub_save_res
721956f4
AM
3009};
3010
6f20ed8a
AM
3011/* Information on stub grouping. */
3012struct map_stub
3013{
3014 /* The stub section. */
3015 asection *stub_sec;
3016 /* This is the section to which stubs in the group will be attached. */
3017 asection *link_sec;
a4b6fadd
AM
3018 /* Next group. */
3019 struct map_stub *next;
3020 /* Whether to emit a copy of register save/restore functions in this
3021 group. */
3022 int needs_save_res;
df136d64
AM
3023 /* Current offset within stubs after the insn restoring lr in a
3024 _notoc or _both stub using bcl for pc-relative addressing, or
3025 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3026 unsigned int lr_restore;
3027 /* Accumulated size of EH info emitted to describe return address
3028 if stubs modify lr. Does not include 17 byte FDE header. */
3029 unsigned int eh_size;
3030 /* Offset in glink_eh_frame to the start of EH info for this group. */
3031 unsigned int eh_base;
6f20ed8a
AM
3032};
3033
2cdcc330
AM
3034struct ppc_stub_hash_entry
3035{
721956f4
AM
3036 /* Base hash table entry structure. */
3037 struct bfd_hash_entry root;
3038
ad8e1ba5
AM
3039 enum ppc_stub_type stub_type;
3040
6f20ed8a
AM
3041 /* Group information. */
3042 struct map_stub *group;
721956f4
AM
3043
3044 /* Offset within stub_sec of the beginning of this stub. */
3045 bfd_vma stub_offset;
3046
3047 /* Given the symbol's value and its section we can determine its final
3048 value when building the stubs (so the stub knows where to jump. */
3049 bfd_vma target_value;
3050 asection *target_section;
3051
721956f4
AM
3052 /* The symbol table entry, if any, that this was derived from. */
3053 struct ppc_link_hash_entry *h;
e054468f 3054 struct plt_entry *plt_ent;
721956f4 3055
2d7ad24e
AM
3056 /* Symbol type. */
3057 unsigned char symtype;
3058
6911b7dc
AM
3059 /* Symbol st_other. */
3060 unsigned char other;
721956f4
AM
3061};
3062
2cdcc330
AM
3063struct ppc_branch_hash_entry
3064{
721956f4
AM
3065 /* Base hash table entry structure. */
3066 struct bfd_hash_entry root;
3067
c456f082 3068 /* Offset within branch lookup table. */
721956f4
AM
3069 unsigned int offset;
3070
3071 /* Generation marker. */
3072 unsigned int iter;
3073};
65f38f15 3074
19e08130
AM
3075/* Used to track dynamic relocations for local symbols. */
3076struct ppc_dyn_relocs
3077{
3078 struct ppc_dyn_relocs *next;
3079
3080 /* The input section of the reloc. */
3081 asection *sec;
3082
3083 /* Total number of relocs copied for the input section. */
3084 unsigned int count : 31;
3085
3086 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3087 unsigned int ifunc : 1;
3088};
3089
65f38f15
AM
3090struct ppc_link_hash_entry
3091{
3092 struct elf_link_hash_entry elf;
3093
2cdcc330
AM
3094 union
3095 {
b3fac117
AM
3096 /* A pointer to the most recently used stub hash entry against this
3097 symbol. */
3098 struct ppc_stub_hash_entry *stub_cache;
3099
3100 /* A pointer to the next symbol starting with a '.' */
3101 struct ppc_link_hash_entry *next_dot_sym;
3102 } u;
721956f4 3103
721956f4 3104 /* Link between function code and descriptor symbols. */
34814b9f 3105 struct ppc_link_hash_entry *oh;
721956f4 3106
e86ce104
AM
3107 /* Flag function code and descriptor symbols. */
3108 unsigned int is_func:1;
3109 unsigned int is_func_descriptor:1;
908b32fc 3110 unsigned int fake:1;
411e1bfb 3111
c5614fa4
AM
3112 /* Whether global opd/toc sym has been adjusted or not.
3113 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3114 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3115 unsigned int adjust_done:1;
3116
a4b6fadd
AM
3117 /* Set if this is an out-of-line register save/restore function,
3118 with non-standard calling convention. */
3119 unsigned int save_res:1;
3120
8b5f1ed8
AM
3121 /* Set if a duplicate symbol with non-zero localentry is detected,
3122 even when the duplicate symbol does not provide a definition. */
3123 unsigned int non_zero_localentry:1;
3124
411e1bfb 3125 /* Contexts in which symbol is used in the GOT (or TOC).
37da22e5
AM
3126 Bits are or'd into the mask as the corresponding relocs are
3127 encountered during check_relocs, with TLS_TLS being set when any
3128 of the other TLS bits are set. tls_optimize clears bits when
3129 optimizing to indicate the corresponding GOT entry type is not
3130 needed. If set, TLS_TLS is never cleared. tls_optimize may also
b00a0a86 3131 set TLS_GDIE when a GD reloc turns into an IE one.
37da22e5
AM
3132 These flags are also kept for local symbols. */
3133#define TLS_TLS 1 /* Any TLS reloc. */
3134#define TLS_GD 2 /* GD reloc. */
3135#define TLS_LD 4 /* LD reloc. */
3136#define TLS_TPREL 8 /* TPREL reloc, => IE. */
3137#define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3138#define TLS_MARK 32 /* __tls_get_addr call marked. */
b00a0a86 3139#define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
46e9995a 3140#define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
f961d9dd 3141 unsigned char tls_mask;
37da22e5
AM
3142
3143 /* The above field is also used to mark function symbols. In which
3144 case TLS_TLS will be 0. */
3145#define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
2d7ad24e 3146#define PLT_KEEP 4 /* inline plt call requires plt entry. */
37da22e5 3147#define NON_GOT 256 /* local symbol plt, not stored. */
65f38f15
AM
3148};
3149
ed7007c1
AM
3150static inline struct ppc_link_hash_entry *
3151ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3152{
3153 return (struct ppc_link_hash_entry *) ent;
3154}
3155
30845f11
AM
3156static inline struct elf_link_hash_entry *
3157elf_hash_entry (struct ppc_link_hash_entry *ent)
3158{
3159 return (struct elf_link_hash_entry *) ent;
3160}
3161
65f38f15
AM
3162/* ppc64 ELF linker hash table. */
3163
3164struct ppc_link_hash_table
3165{
3166 struct elf_link_hash_table elf;
3167
721956f4
AM
3168 /* The stub hash table. */
3169 struct bfd_hash_table stub_hash_table;
3170
3171 /* Another hash table for plt_branch stubs. */
3172 struct bfd_hash_table branch_hash_table;
3173
3b421ab3
AM
3174 /* Hash table for function prologue tocsave. */
3175 htab_t tocsave_htab;
3176
e7d1c40c
AM
3177 /* Various options and other info passed from the linker. */
3178 struct ppc64_elf_params *params;
721956f4 3179
6f20ed8a
AM
3180 /* The size of sec_info below. */
3181 unsigned int sec_info_arr_size;
3182
3183 /* Per-section array of extra section info. Done this way rather
3184 than as part of ppc64_elf_section_data so we have the info for
3185 non-ppc64 sections. */
3186 struct
3187 {
3188 /* Along with elf_gp, specifies the TOC pointer used by this section. */
ad8e1ba5 3189 bfd_vma toc_off;
6f20ed8a
AM
3190
3191 union
3192 {
3193 /* The section group that this section belongs to. */
3194 struct map_stub *group;
3195 /* A temp section list pointer. */
3196 asection *list;
3197 } u;
3198 } *sec_info;
721956f4 3199
a4b6fadd
AM
3200 /* Linked list of groups. */
3201 struct map_stub *group;
3202
ad8e1ba5
AM
3203 /* Temp used when calculating TOC pointers. */
3204 bfd_vma toc_curr;
bf102f86
AM
3205 bfd *toc_bfd;
3206 asection *toc_first_sec;
ad8e1ba5 3207
b3fac117
AM
3208 /* Used when adding symbols. */
3209 struct ppc_link_hash_entry *dot_syms;
3210
33e44f2e 3211 /* Shortcuts to get to dynamic linker sections. */
4ce794b7 3212 asection *glink;
9e390558 3213 asection *global_entry;
82bd7b59 3214 asection *sfpr;
2d7ad24e
AM
3215 asection *pltlocal;
3216 asection *relpltlocal;
4ce794b7
AM
3217 asection *brlt;
3218 asection *relbrlt;
58d180e8 3219 asection *glink_eh_frame;
ec338859 3220
8387904d
AM
3221 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3222 struct ppc_link_hash_entry *tls_get_addr;
3223 struct ppc_link_hash_entry *tls_get_addr_fd;
9e7028aa
AM
3224 struct ppc_link_hash_entry *tga_desc;
3225 struct ppc_link_hash_entry *tga_desc_fd;
a804e476 3226 struct map_stub *tga_group;
411e1bfb 3227
927be08e
AM
3228 /* The size of reliplt used by got entry relocs. */
3229 bfd_size_type got_reli_size;
3230
9b5ecbd0 3231 /* Statistics. */
7341d5e2 3232 unsigned long stub_count[ppc_stub_global_entry];
9b5ecbd0 3233
ee75fd95
AM
3234 /* Number of stubs against global syms. */
3235 unsigned long stub_globals;
3236
ee67d69a
AM
3237 /* Set if we're linking code with function descriptors. */
3238 unsigned int opd_abi:1;
3239
4c52953f 3240 /* Support for multiple toc sections. */
33c0ec9d 3241 unsigned int do_multi_toc:1;
4c52953f 3242 unsigned int multi_toc_needed:1;
927be08e 3243 unsigned int second_toc_pass:1;
67f0cbdb 3244 unsigned int do_toc_opt:1;
4c52953f 3245
9a23f96e
AM
3246 /* Set if tls optimization is enabled. */
3247 unsigned int do_tls_opt:1;
3248
3e04d765
AM
3249 /* Set if inline plt calls should be converted to direct calls. */
3250 unsigned int can_convert_all_inline_plt:1;
3251
5d1634d7 3252 /* Set on error. */
99877b66 3253 unsigned int stub_error:1;
721956f4 3254
8c5b4e52
AM
3255 /* Whether func_desc_adjust needs to be run over symbols. */
3256 unsigned int need_func_desc_adj:1;
721956f4 3257
f378ab09
AM
3258 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3259 unsigned int has_plt_localentry0:1;
3260
5663e321
AM
3261 /* Whether calls are made via the PLT from NOTOC functions. */
3262 unsigned int notoc_plt:1;
3263
e10a07b3
AM
3264 /* Whether any code linked seems to be Power10. */
3265 unsigned int has_power10_relocs:1;
3266
721956f4
AM
3267 /* Incremented every time we size stubs. */
3268 unsigned int stub_iteration;
65f38f15
AM
3269};
3270
4c52953f
AM
3271/* Rename some of the generic section flags to better document how they
3272 are used here. */
b0dddeec
AM
3273
3274/* Nonzero if this section has TLS related relocations. */
3275#define has_tls_reloc sec_flg0
3276
9737e8af
AM
3277/* Nonzero if this section has a call to __tls_get_addr lacking marker
3278 relocations. */
3279#define nomark_tls_get_addr sec_flg1
b0dddeec
AM
3280
3281/* Nonzero if this section has any toc or got relocs. */
3282#define has_toc_reloc sec_flg2
3283
3284/* Nonzero if this section has a call to another section that uses
3285 the toc or got. */
d77c8a4b 3286#define makes_toc_func_call sec_flg3
b0dddeec
AM
3287
3288/* Recursion protection when determining above flag. */
d77c8a4b 3289#define call_check_in_progress sec_flg4
70cc837d 3290#define call_check_done sec_flg5
4c52953f 3291
65f38f15
AM
3292/* Get the ppc64 ELF linker hash table from a link_info structure. */
3293
3294#define ppc_hash_table(p) \
573eb292
AM
3295 ((is_elf_hash_table ((p)->hash) \
3296 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3297 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
65f38f15 3298
721956f4
AM
3299#define ppc_stub_hash_lookup(table, string, create, copy) \
3300 ((struct ppc_stub_hash_entry *) \
3301 bfd_hash_lookup ((table), (string), (create), (copy)))
3302
3303#define ppc_branch_hash_lookup(table, string, create, copy) \
3304 ((struct ppc_branch_hash_entry *) \
3305 bfd_hash_lookup ((table), (string), (create), (copy)))
3306
3307/* Create an entry in the stub hash table. */
3308
3309static struct bfd_hash_entry *
4ce794b7
AM
3310stub_hash_newfunc (struct bfd_hash_entry *entry,
3311 struct bfd_hash_table *table,
3312 const char *string)
721956f4
AM
3313{
3314 /* Allocate the structure if it has not already been allocated by a
3315 subclass. */
3316 if (entry == NULL)
3317 {
3318 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3319 if (entry == NULL)
3320 return entry;
3321 }
3322
3323 /* Call the allocation method of the superclass. */
3324 entry = bfd_hash_newfunc (entry, table, string);
3325 if (entry != NULL)
3326 {
3327 struct ppc_stub_hash_entry *eh;
3328
3329 /* Initialize the local fields. */
3330 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3331 eh->stub_type = ppc_stub_none;
6f20ed8a 3332 eh->group = NULL;
721956f4
AM
3333 eh->stub_offset = 0;
3334 eh->target_value = 0;
3335 eh->target_section = NULL;
721956f4 3336 eh->h = NULL;
6911b7dc 3337 eh->plt_ent = NULL;
6911b7dc 3338 eh->other = 0;
721956f4
AM
3339 }
3340
3341 return entry;
3342}
3343
3344/* Create an entry in the branch hash table. */
3345
3346static struct bfd_hash_entry *
4ce794b7
AM
3347branch_hash_newfunc (struct bfd_hash_entry *entry,
3348 struct bfd_hash_table *table,
3349 const char *string)
721956f4
AM
3350{
3351 /* Allocate the structure if it has not already been allocated by a
3352 subclass. */
3353 if (entry == NULL)
3354 {
3355 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3356 if (entry == NULL)
3357 return entry;
3358 }
3359
3360 /* Call the allocation method of the superclass. */
3361 entry = bfd_hash_newfunc (entry, table, string);
3362 if (entry != NULL)
3363 {
3364 struct ppc_branch_hash_entry *eh;
3365
3366 /* Initialize the local fields. */
3367 eh = (struct ppc_branch_hash_entry *) entry;
3368 eh->offset = 0;
3369 eh->iter = 0;
3370 }
3371
3372 return entry;
3373}
3374
65f38f15
AM
3375/* Create an entry in a ppc64 ELF linker hash table. */
3376
3377static struct bfd_hash_entry *
4ce794b7
AM
3378link_hash_newfunc (struct bfd_hash_entry *entry,
3379 struct bfd_hash_table *table,
3380 const char *string)
65f38f15
AM
3381{
3382 /* Allocate the structure if it has not already been allocated by a
3383 subclass. */
3384 if (entry == NULL)
3385 {
3386 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3387 if (entry == NULL)
3388 return entry;
3389 }
3390
3391 /* Call the allocation method of the superclass. */
3392 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3393 if (entry != NULL)
3394 {
3395 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3396
b3fac117 3397 memset (&eh->u.stub_cache, 0,
908b32fc 3398 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3399 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3400
3401 /* When making function calls, old ABI code references function entry
3402 points (dot symbols), while new ABI code references the function
3403 descriptor symbol. We need to make any combination of reference and
3404 definition work together, without breaking archive linking.
3405
3406 For a defined function "foo" and an undefined call to "bar":
3407 An old object defines "foo" and ".foo", references ".bar" (possibly
3408 "bar" too).
3409 A new object defines "foo" and references "bar".
3410
3411 A new object thus has no problem with its undefined symbols being
3412 satisfied by definitions in an old object. On the other hand, the
3413 old object won't have ".bar" satisfied by a new object.
3414
3415 Keep a list of newly added dot-symbols. */
3416
3417 if (string[0] == '.')
3418 {
3419 struct ppc_link_hash_table *htab;
3420
3421 htab = (struct ppc_link_hash_table *) table;
3422 eh->u.next_dot_sym = htab->dot_syms;
3423 htab->dot_syms = eh;
3424 }
65f38f15
AM
3425 }
3426
3427 return entry;
3428}
3429
2cdcc330
AM
3430struct tocsave_entry
3431{
3b421ab3
AM
3432 asection *sec;
3433 bfd_vma offset;
3434};
3435
3436static hashval_t
3437tocsave_htab_hash (const void *p)
3438{
3439 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4aef7643 3440 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3b421ab3
AM
3441}
3442
3443static int
3444tocsave_htab_eq (const void *p1, const void *p2)
3445{
3446 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3447 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3448 return e1->sec == e2->sec && e1->offset == e2->offset;
3449}
3450
68faa637
AM
3451/* Destroy a ppc64 ELF linker hash table. */
3452
3453static void
d495ab0d 3454ppc64_elf_link_hash_table_free (bfd *obfd)
68faa637 3455{
d495ab0d 3456 struct ppc_link_hash_table *htab;
68faa637 3457
d495ab0d 3458 htab = (struct ppc_link_hash_table *) obfd->link.hash;
68faa637
AM
3459 if (htab->tocsave_htab)
3460 htab_delete (htab->tocsave_htab);
d495ab0d
AM
3461 bfd_hash_table_free (&htab->branch_hash_table);
3462 bfd_hash_table_free (&htab->stub_hash_table);
3463 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
3464}
3465
65f38f15
AM
3466/* Create a ppc64 ELF linker hash table. */
3467
3468static struct bfd_link_hash_table *
4ce794b7 3469ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3470{
3471 struct ppc_link_hash_table *htab;
986f0783 3472 size_t amt = sizeof (struct ppc_link_hash_table);
65f38f15 3473
4ce794b7 3474 htab = bfd_zmalloc (amt);
65f38f15
AM
3475 if (htab == NULL)
3476 return NULL;
3477
66eb6687 3478 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3479 sizeof (struct ppc_link_hash_entry),
3480 PPC64_ELF_DATA))
65f38f15 3481 {
e2d34d7d 3482 free (htab);
65f38f15
AM
3483 return NULL;
3484 }
3485
721956f4 3486 /* Init the stub hash table too. */
66eb6687
AM
3487 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3488 sizeof (struct ppc_stub_hash_entry)))
2915c55b 3489 {
d495ab0d 3490 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3491 return NULL;
3492 }
721956f4
AM
3493
3494 /* And the branch hash table. */
66eb6687
AM
3495 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3496 sizeof (struct ppc_branch_hash_entry)))
2915c55b
JK
3497 {
3498 bfd_hash_table_free (&htab->stub_hash_table);
d495ab0d 3499 _bfd_elf_link_hash_table_free (abfd);
2915c55b
JK
3500 return NULL;
3501 }
721956f4 3502
3b421ab3
AM
3503 htab->tocsave_htab = htab_try_create (1024,
3504 tocsave_htab_hash,
3505 tocsave_htab_eq,
3506 NULL);
3507 if (htab->tocsave_htab == NULL)
2915c55b 3508 {
d495ab0d 3509 ppc64_elf_link_hash_table_free (abfd);
2915c55b
JK
3510 return NULL;
3511 }
d495ab0d 3512 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3b421ab3 3513
3254fd24
AM
3514 /* Initializing two fields of the union is just cosmetic. We really
3515 only care about glist, but when compiled on a 32-bit host the
3516 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3517 debugger inspection of these fields look nicer. */
a6aa5195
AM
3518 htab->elf.init_got_refcount.refcount = 0;
3519 htab->elf.init_got_refcount.glist = NULL;
3520 htab->elf.init_plt_refcount.refcount = 0;
3521 htab->elf.init_plt_refcount.glist = NULL;
3522 htab->elf.init_got_offset.offset = 0;
3523 htab->elf.init_got_offset.glist = NULL;
3524 htab->elf.init_plt_offset.offset = 0;
3525 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3526
65f38f15
AM
3527 return &htab->elf.root;
3528}
3529
bfeb4a28
AM
3530/* Create sections for linker generated code. */
3531
0a1b45a2 3532static bool
bfeb4a28
AM
3533create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3534{
3535 struct ppc_link_hash_table *htab;
3536 flagword flags;
3537
3538 htab = ppc_hash_table (info);
3539
bfeb4a28
AM
3540 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3541 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
b32547cd
AM
3542 if (htab->params->save_restore_funcs)
3543 {
3544 /* Create .sfpr for code to save and restore fp regs. */
3545 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3546 flags);
3547 if (htab->sfpr == NULL
fd361982 3548 || !bfd_set_section_alignment (htab->sfpr, 2))
0a1b45a2 3549 return false;
b32547cd
AM
3550 }
3551
3552 if (bfd_link_relocatable (info))
0a1b45a2 3553 return true;
bfeb4a28
AM
3554
3555 /* Create .glink for lazy dynamic linking support. */
3556 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3557 flags);
3558 if (htab->glink == NULL
fd361982 3559 || !bfd_set_section_alignment (htab->glink, 3))
0a1b45a2 3560 return false;
bfeb4a28 3561
9e390558
AM
3562 /* The part of .glink used by global entry stubs, separate so that
3563 it can be aligned appropriately without affecting htab->glink. */
3564 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3565 flags);
3566 if (htab->global_entry == NULL
fd361982 3567 || !bfd_set_section_alignment (htab->global_entry, 2))
0a1b45a2 3568 return false;
9e390558 3569
bfeb4a28
AM
3570 if (!info->no_ld_generated_unwind_info)
3571 {
3572 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3573 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3574 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3575 ".eh_frame",
3576 flags);
3577 if (htab->glink_eh_frame == NULL
fd361982 3578 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
0a1b45a2 3579 return false;
bfeb4a28
AM
3580 }
3581
3582 flags = SEC_ALLOC | SEC_LINKER_CREATED;
33e44f2e
AM
3583 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3584 if (htab->elf.iplt == NULL
fd361982 3585 || !bfd_set_section_alignment (htab->elf.iplt, 3))
0a1b45a2 3586 return false;
bfeb4a28
AM
3587
3588 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3589 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
33e44f2e
AM
3590 htab->elf.irelplt
3591 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3592 if (htab->elf.irelplt == NULL
fd361982 3593 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
0a1b45a2 3594 return false;
bfeb4a28
AM
3595
3596 /* Create branch lookup table for plt_branch stubs. */
3597 flags = (SEC_ALLOC | SEC_LOAD
3598 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3599 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3600 flags);
3601 if (htab->brlt == NULL
fd361982 3602 || !bfd_set_section_alignment (htab->brlt, 3))
0a1b45a2 3603 return false;
bfeb4a28 3604
2d7ad24e
AM
3605 /* Local plt entries, put in .branch_lt but a separate section for
3606 convenience. */
3607 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3608 flags);
3609 if (htab->pltlocal == NULL
fd361982 3610 || !bfd_set_section_alignment (htab->pltlocal, 3))
0a1b45a2 3611 return false;
2d7ad24e 3612
0e1862bb 3613 if (!bfd_link_pic (info))
0a1b45a2 3614 return true;
bfeb4a28
AM
3615
3616 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3617 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2d7ad24e
AM
3618 htab->relbrlt
3619 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
bfeb4a28 3620 if (htab->relbrlt == NULL
fd361982 3621 || !bfd_set_section_alignment (htab->relbrlt, 3))
0a1b45a2 3622 return false;
bfeb4a28 3623
2d7ad24e
AM
3624 htab->relpltlocal
3625 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3626 if (htab->relpltlocal == NULL
fd361982 3627 || !bfd_set_section_alignment (htab->relpltlocal, 3))
0a1b45a2 3628 return false;
2d7ad24e 3629
0a1b45a2 3630 return true;
bfeb4a28
AM
3631}
3632
e717da7e
AM
3633/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3634
0a1b45a2 3635bool
e7d1c40c
AM
3636ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3637 struct ppc64_elf_params *params)
e717da7e
AM
3638{
3639 struct ppc_link_hash_table *htab;
3640
e7d1c40c 3641 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
e717da7e
AM
3642
3643/* Always hook our dynamic sections into the first bfd, which is the
3644 linker created stub bfd. This ensures that the GOT header is at
3645 the start of the output TOC section. */
3646 htab = ppc_hash_table (info);
e7d1c40c
AM
3647 htab->elf.dynobj = params->stub_bfd;
3648 htab->params = params;
bfeb4a28 3649
bfeb4a28 3650 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
3651}
3652
721956f4
AM
3653/* Build a name for an entry in the stub hash table. */
3654
3655static char *
4ce794b7
AM
3656ppc_stub_name (const asection *input_section,
3657 const asection *sym_sec,
3658 const struct ppc_link_hash_entry *h,
3659 const Elf_Internal_Rela *rel)
721956f4
AM
3660{
3661 char *stub_name;
bcaa2f82 3662 ssize_t len;
721956f4
AM
3663
3664 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3665 offsets from a sym as a branch target? In fact, we could
3666 probably assume the addend is always zero. */
3667 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3668
3669 if (h)
3670 {
3671 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3672 stub_name = bfd_malloc (len);
46de2a7c
AM
3673 if (stub_name == NULL)
3674 return stub_name;
3675
bcaa2f82
AM
3676 len = sprintf (stub_name, "%08x.%s+%x",
3677 input_section->id & 0xffffffff,
3678 h->elf.root.root.string,
3679 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3680 }
3681 else
3682 {
ad8e1ba5 3683 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3684 stub_name = bfd_malloc (len);
46de2a7c
AM
3685 if (stub_name == NULL)
3686 return stub_name;
3687
bcaa2f82
AM
3688 len = sprintf (stub_name, "%08x.%x:%x+%x",
3689 input_section->id & 0xffffffff,
3690 sym_sec->id & 0xffffffff,
3691 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3692 (int) rel->r_addend & 0xffffffff);
721956f4 3693 }
bcaa2f82 3694 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 3695 stub_name[len - 2] = 0;
721956f4
AM
3696 return stub_name;
3697}
3698
e10a07b3
AM
3699/* If mixing power10 with non-power10 code and --power10-stubs is not
3700 specified (or is auto) then calls using @notoc relocations that
3701 need a stub will utilize power10 instructions in the stub, and
3702 calls without @notoc relocations will not use power10 instructions.
3703 The two classes of stubs are stored in separate stub_hash_table
3704 entries having the same key string. The two entries will always be
3705 adjacent on entry->root.next chain, even if hash table resizing
3706 occurs. This function selects the correct entry to use. */
3707
3708static struct ppc_stub_hash_entry *
0a1b45a2 3709select_alt_stub (struct ppc_stub_hash_entry *entry, bool notoc)
e10a07b3 3710{
0a1b45a2 3711 bool have_notoc;
e10a07b3
AM
3712
3713 have_notoc = (entry->stub_type == ppc_stub_plt_call_notoc
3714 || entry->stub_type == ppc_stub_plt_branch_notoc
3715 || entry->stub_type == ppc_stub_long_branch_notoc);
3716
3717 if (have_notoc != notoc)
3718 {
3719 const char *stub_name = entry->root.string;
3720
3721 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3722 if (entry != NULL
3723 && entry->root.string != stub_name)
3724 entry = NULL;
3725 }
3726
3727 return entry;
3728}
3729
721956f4
AM
3730/* Look up an entry in the stub hash. Stub entries are cached because
3731 creating the stub name takes a bit of time. */
3732
3733static struct ppc_stub_hash_entry *
4ce794b7
AM
3734ppc_get_stub_entry (const asection *input_section,
3735 const asection *sym_sec,
039b3fef 3736 struct ppc_link_hash_entry *h,
4ce794b7
AM
3737 const Elf_Internal_Rela *rel,
3738 struct ppc_link_hash_table *htab)
721956f4
AM
3739{
3740 struct ppc_stub_hash_entry *stub_entry;
6f20ed8a 3741 struct map_stub *group;
721956f4
AM
3742
3743 /* If this input section is part of a group of sections sharing one
3744 stub section, then use the id of the first section in the group.
3745 Stub names need to include a section id, as there may well be
3746 more than one stub used to reach say, printf, and we need to
3747 distinguish between them. */
6f20ed8a 3748 group = htab->sec_info[input_section->id].u.group;
89d77b8a
AM
3749 if (group == NULL)
3750 return NULL;
721956f4 3751
b3fac117
AM
3752 if (h != NULL && h->u.stub_cache != NULL
3753 && h->u.stub_cache->h == h
6f20ed8a 3754 && h->u.stub_cache->group == group)
721956f4 3755 {
b3fac117 3756 stub_entry = h->u.stub_cache;
721956f4
AM
3757 }
3758 else
3759 {
3760 char *stub_name;
3761
6f20ed8a 3762 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
721956f4
AM
3763 if (stub_name == NULL)
3764 return NULL;
3765
3766 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 3767 stub_name, false, false);
721956f4 3768 if (h != NULL)
b3fac117 3769 h->u.stub_cache = stub_entry;
721956f4
AM
3770
3771 free (stub_name);
3772 }
3773
e10a07b3
AM
3774 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3775 {
0a1b45a2 3776 bool notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
e10a07b3
AM
3777
3778 stub_entry = select_alt_stub (stub_entry, notoc);
3779 }
3780
721956f4
AM
3781 return stub_entry;
3782}
3783
3784/* Add a new stub entry to the stub hash. Not all fields of the new
3785 stub entry are initialised. */
3786
3787static struct ppc_stub_hash_entry *
4ce794b7
AM
3788ppc_add_stub (const char *stub_name,
3789 asection *section,
25f53a85 3790 struct bfd_link_info *info)
721956f4 3791{
25f53a85 3792 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 3793 struct map_stub *group;
721956f4
AM
3794 asection *link_sec;
3795 asection *stub_sec;
3796 struct ppc_stub_hash_entry *stub_entry;
3797
6f20ed8a
AM
3798 group = htab->sec_info[section->id].u.group;
3799 link_sec = group->link_sec;
3800 stub_sec = group->stub_sec;
721956f4
AM
3801 if (stub_sec == NULL)
3802 {
6f20ed8a
AM
3803 size_t namelen;
3804 bfd_size_type len;
3805 char *s_name;
721956f4 3806
6f20ed8a
AM
3807 namelen = strlen (link_sec->name);
3808 len = namelen + sizeof (STUB_SUFFIX);
3809 s_name = bfd_alloc (htab->params->stub_bfd, len);
3810 if (s_name == NULL)
3811 return NULL;
721956f4 3812
6f20ed8a
AM
3813 memcpy (s_name, link_sec->name, namelen);
3814 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3815 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3816 if (stub_sec == NULL)
3817 return NULL;
3818 group->stub_sec = stub_sec;
721956f4
AM
3819 }
3820
3821 /* Enter this entry into the linker stub hash table. */
3822 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
0a1b45a2 3823 true, false);
721956f4
AM
3824 if (stub_entry == NULL)
3825 {
695344c0 3826 /* xgettext:c-format */
cf97bcb0
AM
3827 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3828 section->owner, stub_name);
721956f4
AM
3829 return NULL;
3830 }
3831
6f20ed8a 3832 stub_entry->group = group;
721956f4 3833 stub_entry->stub_offset = 0;
721956f4
AM
3834 return stub_entry;
3835}
3836
e717da7e
AM
3837/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3838 not already done. */
65f38f15 3839
0a1b45a2 3840static bool
e717da7e 3841create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3842{
e717da7e
AM
3843 asection *got, *relgot;
3844 flagword flags;
3845 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3846
0c8d6e5c 3847 if (!is_ppc64_elf (abfd))
0a1b45a2 3848 return false;
4dfe6ac6 3849 if (htab == NULL)
0a1b45a2 3850 return false;
0ffa91dd 3851
33e44f2e
AM
3852 if (!htab->elf.sgot
3853 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
0a1b45a2 3854 return false;
e717da7e
AM
3855
3856 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3857 | SEC_LINKER_CREATED);
3858
c456f082 3859 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3860 if (!got
fd361982 3861 || !bfd_set_section_alignment (got, 3))
0a1b45a2 3862 return false;
65f38f15 3863
c456f082
AM
3864 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3865 flags | SEC_READONLY);
e717da7e 3866 if (!relgot
fd361982 3867 || !bfd_set_section_alignment (relgot, 3))
0a1b45a2 3868 return false;
e717da7e
AM
3869
3870 ppc64_elf_tdata (abfd)->got = got;
3871 ppc64_elf_tdata (abfd)->relgot = relgot;
0a1b45a2 3872 return true;
65f38f15 3873}
5bd4f169 3874
b31867b6
AM
3875/* Follow indirect and warning symbol links. */
3876
3877static inline struct bfd_link_hash_entry *
3878follow_link (struct bfd_link_hash_entry *h)
3879{
3880 while (h->type == bfd_link_hash_indirect
3881 || h->type == bfd_link_hash_warning)
3882 h = h->u.i.link;
3883 return h;
3884}
3885
3886static inline struct elf_link_hash_entry *
3887elf_follow_link (struct elf_link_hash_entry *h)
3888{
3889 return (struct elf_link_hash_entry *) follow_link (&h->root);
3890}
3891
3892static inline struct ppc_link_hash_entry *
3893ppc_follow_link (struct ppc_link_hash_entry *h)
3894{
ed7007c1 3895 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
b31867b6
AM
3896}
3897
40d16e0b
AM
3898/* Merge PLT info on FROM with that on TO. */
3899
3900static void
3901move_plt_plist (struct ppc_link_hash_entry *from,
3902 struct ppc_link_hash_entry *to)
3903{
3904 if (from->elf.plt.plist != NULL)
3905 {
3906 if (to->elf.plt.plist != NULL)
3907 {
3908 struct plt_entry **entp;
3909 struct plt_entry *ent;
3910
3911 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3912 {
3913 struct plt_entry *dent;
3914
3915 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3916 if (dent->addend == ent->addend)
3917 {
3918 dent->plt.refcount += ent->plt.refcount;
3919 *entp = ent->next;
3920 break;
3921 }
3922 if (dent == NULL)
3923 entp = &ent->next;
3924 }
3925 *entp = to->elf.plt.plist;
3926 }
3927
3928 to->elf.plt.plist = from->elf.plt.plist;
3929 from->elf.plt.plist = NULL;
3930 }
3931}
3932
65f38f15
AM
3933/* Copy the extra info we tack onto an elf_link_hash_entry. */
3934
3935static void
fcfa13d2
AM
3936ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3937 struct elf_link_hash_entry *dir,
3938 struct elf_link_hash_entry *ind)
65f38f15
AM
3939{
3940 struct ppc_link_hash_entry *edir, *eind;
3941
ed7007c1
AM
3942 edir = ppc_elf_hash_entry (dir);
3943 eind = ppc_elf_hash_entry (ind);
65f38f15 3944
c79d6685
AM
3945 edir->is_func |= eind->is_func;
3946 edir->is_func_descriptor |= eind->is_func_descriptor;
3947 edir->tls_mask |= eind->tls_mask;
3948 if (eind->oh != NULL)
3949 edir->oh = ppc_follow_link (eind->oh);
3950
474436e6 3951 if (edir->elf.versioned != versioned_hidden)
e81830c5
AM
3952 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3953 edir->elf.ref_regular |= eind->elf.ref_regular;
3954 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4a7e5234 3955 edir->elf.non_got_ref |= eind->elf.non_got_ref;
e81830c5
AM
3956 edir->elf.needs_plt |= eind->elf.needs_plt;
3957 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
c79d6685 3958
d311bc8b
AM
3959 /* If we were called to copy over info for a weak sym, don't copy
3960 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3961 in order to simplify readonly_dynrelocs and save a field in the
3962 symbol hash entry, but that means dyn_relocs can't be used in any
3963 tests about a specific symbol, or affect other symbol flags which
ab2477e1 3964 are then tested. */
d311bc8b 3965 if (eind->elf.root.type != bfd_link_hash_indirect)
ab2477e1 3966 return;
d311bc8b 3967
411e1bfb 3968 /* Copy over any dynamic relocs we may have on the indirect sym. */
190eb1dd 3969 if (ind->dyn_relocs != NULL)
65f38f15 3970 {
190eb1dd 3971 if (dir->dyn_relocs != NULL)
bbd7ec4a 3972 {
6061a67d
AM
3973 struct elf_dyn_relocs **pp;
3974 struct elf_dyn_relocs *p;
bbd7ec4a 3975
fcfa13d2 3976 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a 3977 list. Merge any entries against the same section. */
190eb1dd 3978 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
bbd7ec4a 3979 {
6061a67d 3980 struct elf_dyn_relocs *q;
bbd7ec4a 3981
190eb1dd 3982 for (q = dir->dyn_relocs; q != NULL; q = q->next)
bbd7ec4a
AM
3983 if (q->sec == p->sec)
3984 {
3985 q->pc_count += p->pc_count;
3986 q->count += p->count;
3987 *pp = p->next;
3988 break;
3989 }
3990 if (q == NULL)
3991 pp = &p->next;
3992 }
190eb1dd 3993 *pp = dir->dyn_relocs;
bbd7ec4a
AM
3994 }
3995
190eb1dd
L
3996 dir->dyn_relocs = ind->dyn_relocs;
3997 ind->dyn_relocs = NULL;
65f38f15 3998 }
65f38f15 3999
81848ca0
AM
4000 /* Copy over got entries that we may have already seen to the
4001 symbol which just became indirect. */
411e1bfb
AM
4002 if (eind->elf.got.glist != NULL)
4003 {
4004 if (edir->elf.got.glist != NULL)
4005 {
4006 struct got_entry **entp;
4007 struct got_entry *ent;
4008
4009 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4010 {
4011 struct got_entry *dent;
4012
4013 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4014 if (dent->addend == ent->addend
e717da7e 4015 && dent->owner == ent->owner
411e1bfb
AM
4016 && dent->tls_type == ent->tls_type)
4017 {
4018 dent->got.refcount += ent->got.refcount;
4019 *entp = ent->next;
4020 break;
4021 }
4022 if (dent == NULL)
4023 entp = &ent->next;
4024 }
4025 *entp = edir->elf.got.glist;
4026 }
4027
4028 edir->elf.got.glist = eind->elf.got.glist;
4029 eind->elf.got.glist = NULL;
4030 }
4031
4032 /* And plt entries. */
40d16e0b 4033 move_plt_plist (eind, edir);
411e1bfb 4034
fcfa13d2 4035 if (eind->elf.dynindx != -1)
411e1bfb 4036 {
fcfa13d2
AM
4037 if (edir->elf.dynindx != -1)
4038 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4039 edir->elf.dynstr_index);
411e1bfb
AM
4040 edir->elf.dynindx = eind->elf.dynindx;
4041 edir->elf.dynstr_index = eind->elf.dynstr_index;
4042 eind->elf.dynindx = -1;
4043 eind->elf.dynstr_index = 0;
4044 }
411e1bfb
AM
4045}
4046
8387904d
AM
4047/* Find the function descriptor hash entry from the given function code
4048 hash entry FH. Link the entries via their OH fields. */
4049
4050static struct ppc_link_hash_entry *
b31867b6 4051lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4052{
4053 struct ppc_link_hash_entry *fdh = fh->oh;
4054
4055 if (fdh == NULL)
4056 {
4057 const char *fd_name = fh->elf.root.root.string + 1;
4058
ed7007c1 4059 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
0a1b45a2 4060 false, false, false));
b31867b6
AM
4061 if (fdh == NULL)
4062 return fdh;
4063
4064 fdh->is_func_descriptor = 1;
4065 fdh->oh = fh;
4066 fh->is_func = 1;
4067 fh->oh = fdh;
8387904d
AM
4068 }
4069
8c5b4e52
AM
4070 fdh = ppc_follow_link (fdh);
4071 fdh->is_func_descriptor = 1;
4072 fdh->oh = fh;
4073 return fdh;
8387904d
AM
4074}
4075
8c5b4e52 4076/* Make a fake function descriptor sym for the undefined code sym FH. */
bb700d78
AM
4077
4078static struct ppc_link_hash_entry *
4079make_fdh (struct bfd_link_info *info,
908b32fc 4080 struct ppc_link_hash_entry *fh)
bb700d78 4081{
8c5b4e52
AM
4082 bfd *abfd = fh->elf.root.u.undef.abfd;
4083 struct bfd_link_hash_entry *bh = NULL;
bb700d78 4084 struct ppc_link_hash_entry *fdh;
8c5b4e52
AM
4085 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4086 ? BSF_WEAK
4087 : BSF_GLOBAL);
4088
4089 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4090 fh->elf.root.root.string + 1,
4091 flags, bfd_und_section_ptr, 0,
0a1b45a2 4092 NULL, false, false, &bh))
bb700d78
AM
4093 return NULL;
4094
4095 fdh = (struct ppc_link_hash_entry *) bh;
4096 fdh->elf.non_elf = 0;
908b32fc
AM
4097 fdh->fake = 1;
4098 fdh->is_func_descriptor = 1;
4099 fdh->oh = fh;
4100 fh->is_func = 1;
4101 fh->oh = fdh;
bb700d78
AM
4102 return fdh;
4103}
4104
8387904d
AM
4105/* Fix function descriptor symbols defined in .opd sections to be
4106 function type. */
555cd476 4107
0a1b45a2 4108static bool
c16153ae 4109ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4110 struct bfd_link_info *info,
555cd476 4111 Elf_Internal_Sym *isym,
6911b7dc 4112 const char **name,
555cd476
AM
4113 flagword *flags ATTRIBUTE_UNUSED,
4114 asection **sec,
b53dfeb2 4115 bfd_vma *value)
555cd476 4116{
b53dfeb2 4117 if (*sec != NULL
f1885d1e 4118 && strcmp ((*sec)->name, ".opd") == 0)
b53dfeb2
AM
4119 {
4120 asection *code_sec;
4121
4122 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4123 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4124 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4125
4126 /* If the symbol is a function defined in .opd, and the function
4127 code is in a discarded group, let it appear to be undefined. */
0e1862bb 4128 if (!bfd_link_relocatable (info)
b53dfeb2
AM
4129 && (*sec)->reloc_count != 0
4130 && opd_entry_value (*sec, *value, &code_sec, NULL,
0a1b45a2 4131 false) != (bfd_vma) -1
b53dfeb2
AM
4132 && discarded_section (code_sec))
4133 {
4134 *sec = bfd_und_section_ptr;
4135 isym->st_shndx = SHN_UNDEF;
4136 }
4137 }
dbd1e97e
AM
4138 else if (*sec != NULL
4139 && strcmp ((*sec)->name, ".toc") == 0
4140 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4141 {
4142 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4143 if (htab != NULL)
4144 htab->params->object_in_toc = 1;
4145 }
433817dd 4146
6911b7dc
AM
4147 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4148 {
4149 if (abiversion (ibfd) == 0)
4150 set_abiversion (ibfd, 2);
4151 else if (abiversion (ibfd) == 1)
4152 {
cf97bcb0
AM
4153 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4154 " for ABI version 1"), *name);
6911b7dc 4155 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4156 return false;
6911b7dc
AM
4157 }
4158 }
4159
0a1b45a2 4160 return true;
555cd476
AM
4161}
4162
6911b7dc
AM
4163/* Merge non-visibility st_other attributes: local entry point. */
4164
4165static void
4166ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 4167 unsigned int st_other,
0a1b45a2
AM
4168 bool definition,
4169 bool dynamic)
6911b7dc 4170{
f378ab09 4171 if (definition && (!dynamic || !h->def_regular))
5160d0f3 4172 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
6911b7dc
AM
4173 | ELF_ST_VISIBILITY (h->other));
4174}
4175
8c5b4e52
AM
4176/* Hook called on merging a symbol. We use this to clear "fake" since
4177 we now have a real symbol. */
4178
0a1b45a2 4179static bool
8c5b4e52 4180ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4181 const Elf_Internal_Sym *isym,
8c5b4e52 4182 asection **psec ATTRIBUTE_UNUSED,
0a1b45a2
AM
4183 bool newdef ATTRIBUTE_UNUSED,
4184 bool olddef ATTRIBUTE_UNUSED,
8c5b4e52
AM
4185 bfd *oldbfd ATTRIBUTE_UNUSED,
4186 const asection *oldsec ATTRIBUTE_UNUSED)
4187{
ed7007c1 4188 ppc_elf_hash_entry (h)->fake = 0;
8b5f1ed8 4189 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
ed7007c1 4190 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
0a1b45a2 4191 return true;
8c5b4e52
AM
4192}
4193
8387904d 4194/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4195 inclusion of a new ABI object archive that defines "bar".
4196 NAME is a symbol defined in an archive. Return a symbol in the hash
4197 table that might be satisfied by the archive symbols. */
8387904d 4198
b585e899 4199static struct bfd_link_hash_entry *
8387904d
AM
4200ppc64_elf_archive_symbol_lookup (bfd *abfd,
4201 struct bfd_link_info *info,
4202 const char *name)
4203{
b585e899 4204 struct bfd_link_hash_entry *h;
8387904d
AM
4205 char *dot_name;
4206 size_t len;
4207
4208 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc 4209 if (h != NULL
b585e899 4210 && ppc_hash_table (info) != NULL
908b32fc
AM
4211 /* Don't return this sym if it is a fake function descriptor
4212 created by add_symbol_adjust. */
b585e899 4213 && !((struct ppc_link_hash_entry *) h)->fake)
8387904d
AM
4214 return h;
4215
4216 if (name[0] == '.')
4217 return h;
4218
4219 len = strlen (name);
4220 dot_name = bfd_alloc (abfd, len + 2);
4221 if (dot_name == NULL)
b585e899 4222 return (struct bfd_link_hash_entry *) -1;
8387904d
AM
4223 dot_name[0] = '.';
4224 memcpy (dot_name + 1, name, len + 1);
4225 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4226 bfd_release (abfd, dot_name);
a804e476
AM
4227 if (h != NULL)
4228 return h;
4229
4230 if (strcmp (name, "__tls_get_addr_opt") == 0)
4231 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4232 return h;
4233}
4234
4235/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4236 new ABI object defines "bar". Well, at least, undefined dot symbols
4237 are made weak. This stops later archive searches from including an
4238 object if we already have a function descriptor definition. It also
35b0ce59
AM
4239 prevents the linker complaining about undefined symbols.
4240 We also check and correct mismatched symbol visibility here. The
4241 most restrictive visibility of the function descriptor and the
4242 function entry symbol is used. */
8387904d 4243
0a1b45a2 4244static bool
b3fac117 4245add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4246{
8387904d
AM
4247 struct ppc_link_hash_table *htab;
4248 struct ppc_link_hash_entry *fdh;
4249
b3fac117
AM
4250 if (eh->elf.root.type == bfd_link_hash_warning)
4251 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4252
8400d40d 4253 if (eh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 4254 return true;
8400d40d 4255
b3fac117
AM
4256 if (eh->elf.root.root.string[0] != '.')
4257 abort ();
8387904d 4258
b3fac117 4259 htab = ppc_hash_table (info);
4dfe6ac6 4260 if (htab == NULL)
0a1b45a2 4261 return false;
4dfe6ac6 4262
b31867b6 4263 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4264 if (fdh == NULL
4265 && !bfd_link_relocatable (info)
4266 && (eh->elf.root.type == bfd_link_hash_undefined
4267 || eh->elf.root.type == bfd_link_hash_undefweak)
4268 && eh->elf.ref_regular)
4269 {
4270 /* Make an undefined function descriptor sym, in order to
4271 pull in an --as-needed shared lib. Archives are handled
4272 elsewhere. */
4273 fdh = make_fdh (info, eh);
4274 if (fdh == NULL)
0a1b45a2 4275 return false;
bb700d78 4276 }
8c5b4e52
AM
4277
4278 if (fdh != NULL)
8387904d 4279 {
35b0ce59
AM
4280 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4281 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4282
4283 /* Make both descriptor and entry symbol have the most
4284 constraining visibility of either symbol. */
35b0ce59
AM
4285 if (entry_vis < descr_vis)
4286 fdh->elf.other += entry_vis - descr_vis;
4287 else if (entry_vis > descr_vis)
4288 eh->elf.other += descr_vis - entry_vis;
4289
8c5b4e52
AM
4290 /* Propagate reference flags from entry symbol to function
4291 descriptor symbol. */
bc4e12de 4292 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4293 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4294 fdh->elf.ref_regular |= eh->elf.ref_regular;
4295 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4296
4297 if (!fdh->elf.forced_local
4298 && fdh->elf.dynindx == -1
4299 && fdh->elf.versioned != versioned_hidden
4300 && (bfd_link_dll (info)
4301 || fdh->elf.def_dynamic
4302 || fdh->elf.ref_dynamic)
4303 && (eh->elf.ref_regular
4304 || eh->elf.def_regular))
4305 {
2cdcc330 4306 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 4307 return false;
35b0ce59 4308 }
8387904d 4309 }
99877b66 4310
0a1b45a2 4311 return true;
8387904d
AM
4312}
4313
f6c7c3e8
AM
4314/* Set up opd section info and abiversion for IBFD, and process list
4315 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4316
0a1b45a2 4317static bool
f6c7c3e8 4318ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4319{
99877b66 4320 struct ppc_link_hash_table *htab;
b3fac117 4321 struct ppc_link_hash_entry **p, *eh;
459609d6 4322 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4323
459609d6 4324 if (opd != NULL && opd->size != 0)
b3fac117 4325 {
b9399fcf
AM
4326 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4327 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4328
459609d6
AM
4329 if (abiversion (ibfd) == 0)
4330 set_abiversion (ibfd, 1);
8a2058b5 4331 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4332 {
695344c0 4333 /* xgettext:c-format */
cf97bcb0
AM
4334 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4335 ibfd, abiversion (ibfd));
459609d6 4336 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4337 return false;
f6c7c3e8 4338 }
b9399fcf 4339 }
f6c7c3e8 4340
b9399fcf
AM
4341 if (is_ppc64_elf (info->output_bfd))
4342 {
4343 /* For input files without an explicit abiversion in e_flags
4344 we should have flagged any with symbol st_other bits set
4345 as ELFv1 and above flagged those with .opd as ELFv2.
4346 Set the output abiversion if not yet set, and for any input
4347 still ambiguous, take its abiversion from the output.
4348 Differences in ABI are reported later. */
4349 if (abiversion (info->output_bfd) == 0)
4350 set_abiversion (info->output_bfd, abiversion (ibfd));
4351 else if (abiversion (ibfd) == 0)
4352 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4353 }
4354
459609d6
AM
4355 htab = ppc_hash_table (info);
4356 if (htab == NULL)
0a1b45a2 4357 return true;
459609d6 4358
b9399fcf
AM
4359 if (opd != NULL && opd->size != 0
4360 && (ibfd->flags & DYNAMIC) == 0
4361 && (opd->flags & SEC_RELOC) != 0
4362 && opd->reloc_count != 0
4363 && !bfd_is_abs_section (opd->output_section)
4364 && info->gc_sections)
4365 {
4366 /* Garbage collection needs some extra help with .opd sections.
4367 We don't want to necessarily keep everything referenced by
4368 relocs in .opd, as that would keep all functions. Instead,
4369 if we reference an .opd symbol (a function descriptor), we
4370 want to keep the function code symbol's section. This is
4371 easy for global symbols, but for local syms we need to keep
4372 information about the associated function section. */
4373 bfd_size_type amt;
4374 asection **opd_sym_map;
4375 Elf_Internal_Shdr *symtab_hdr;
4376 Elf_Internal_Rela *relocs, *rel_end, *rel;
4377
4378 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4379 opd_sym_map = bfd_zalloc (ibfd, amt);
4380 if (opd_sym_map == NULL)
0a1b45a2 4381 return false;
b9399fcf
AM
4382 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4383 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4384 info->keep_memory);
4385 if (relocs == NULL)
0a1b45a2 4386 return false;
b9399fcf
AM
4387 symtab_hdr = &elf_symtab_hdr (ibfd);
4388 rel_end = relocs + opd->reloc_count - 1;
4389 for (rel = relocs; rel < rel_end; rel++)
4390 {
4391 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4392 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4393
4394 if (r_type == R_PPC64_ADDR64
4395 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4396 && r_symndx < symtab_hdr->sh_info)
4397 {
4398 Elf_Internal_Sym *isym;
4399 asection *s;
4400
f1dfbfdb
L
4401 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4402 r_symndx);
b9399fcf
AM
4403 if (isym == NULL)
4404 {
4405 if (elf_section_data (opd)->relocs != relocs)
4406 free (relocs);
0a1b45a2 4407 return false;
b9399fcf
AM
4408 }
4409
4410 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4411 if (s != NULL && s != opd)
4412 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4413 }
4414 }
4415 if (elf_section_data (opd)->relocs != relocs)
4416 free (relocs);
4417 }
459609d6
AM
4418
4419 p = &htab->dot_syms;
4420 while ((eh = *p) != NULL)
4421 {
4422 *p = NULL;
4423 if (&eh->elf == htab->elf.hgot)
4424 ;
4425 else if (htab->elf.hgot == NULL
4426 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4427 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4428 else if (abiversion (ibfd) <= 1)
4429 {
4430 htab->need_func_desc_adj = 1;
4431 if (!add_symbol_adjust (eh, info))
0a1b45a2 4432 return false;
8c5b4e52 4433 }
459609d6
AM
4434 p = &eh->u.next_dot_sym;
4435 }
0a1b45a2 4436 return true;
8387904d
AM
4437}
4438
97fed1c9
JJ
4439/* Undo hash table changes when an --as-needed input file is determined
4440 not to be needed. */
4441
0a1b45a2 4442static bool
e5034e59
AM
4443ppc64_elf_notice_as_needed (bfd *ibfd,
4444 struct bfd_link_info *info,
4445 enum notice_asneeded_action act)
97fed1c9 4446{
e5034e59
AM
4447 if (act == notice_not_needed)
4448 {
4449 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4450
e5034e59 4451 if (htab == NULL)
0a1b45a2 4452 return false;
4dfe6ac6 4453
e5034e59
AM
4454 htab->dot_syms = NULL;
4455 }
4456 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4457}
4458
aa374f67
AM
4459/* If --just-symbols against a final linked binary, then assume we need
4460 toc adjusting stubs when calling functions defined there. */
4461
4462static void
4463ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4464{
4465 if ((sec->flags & SEC_CODE) != 0
4466 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4467 && is_ppc64_elf (sec->owner))
4468 {
2c3f079f
AM
4469 if (abiversion (sec->owner) >= 2
4470 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4471 sec->has_toc_reloc = 1;
4472 }
4473 _bfd_elf_link_just_syms (sec, info);
4474}
4475
e054468f 4476static struct plt_entry **
4ce794b7
AM
4477update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4478 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4479{
4480 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4481 struct plt_entry **local_plt;
f961d9dd 4482 unsigned char *local_got_tls_masks;
411e1bfb
AM
4483
4484 if (local_got_ents == NULL)
4485 {
4486 bfd_size_type size = symtab_hdr->sh_info;
4487
e054468f
AM
4488 size *= (sizeof (*local_got_ents)
4489 + sizeof (*local_plt)
4490 + sizeof (*local_got_tls_masks));
4ce794b7 4491 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4492 if (local_got_ents == NULL)
e054468f 4493 return NULL;
411e1bfb
AM
4494 elf_local_got_ents (abfd) = local_got_ents;
4495 }
4496
37da22e5 4497 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4498 {
4499 struct got_entry *ent;
4500
4501 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4502 if (ent->addend == r_addend
4503 && ent->owner == abfd
4504 && ent->tls_type == tls_type)
411e1bfb
AM
4505 break;
4506 if (ent == NULL)
4507 {
986f0783 4508 size_t amt = sizeof (*ent);
4ce794b7 4509 ent = bfd_alloc (abfd, amt);
411e1bfb 4510 if (ent == NULL)
0a1b45a2 4511 return false;
411e1bfb
AM
4512 ent->next = local_got_ents[r_symndx];
4513 ent->addend = r_addend;
e717da7e 4514 ent->owner = abfd;
411e1bfb 4515 ent->tls_type = tls_type;
0a1b45a2 4516 ent->is_indirect = false;
411e1bfb
AM
4517 ent->got.refcount = 0;
4518 local_got_ents[r_symndx] = ent;
4519 }
4520 ent->got.refcount += 1;
4521 }
4522
e054468f 4523 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4524 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4525 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4526
4527 return local_plt + r_symndx;
65f38f15
AM
4528}
4529
0a1b45a2 4530static bool
e054468f 4531update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4532{
411e1bfb 4533 struct plt_entry *ent;
1e2f5b6e 4534
e054468f 4535 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4536 if (ent->addend == addend)
4537 break;
4538 if (ent == NULL)
1e2f5b6e 4539 {
986f0783 4540 size_t amt = sizeof (*ent);
4ce794b7 4541 ent = bfd_alloc (abfd, amt);
411e1bfb 4542 if (ent == NULL)
0a1b45a2 4543 return false;
e054468f 4544 ent->next = *plist;
411e1bfb
AM
4545 ent->addend = addend;
4546 ent->plt.refcount = 0;
e054468f 4547 *plist = ent;
1e2f5b6e 4548 }
411e1bfb 4549 ent->plt.refcount += 1;
0a1b45a2 4550 return true;
1e2f5b6e
AM
4551}
4552
0a1b45a2 4553static bool
e054468f
AM
4554is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4555{
4556 return (r_type == R_PPC64_REL24
05d0e962 4557 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4558 || r_type == R_PPC64_REL14
4559 || r_type == R_PPC64_REL14_BRTAKEN
4560 || r_type == R_PPC64_REL14_BRNTAKEN
4561 || r_type == R_PPC64_ADDR24
4562 || r_type == R_PPC64_ADDR14
4563 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4564 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4565 || r_type == R_PPC64_PLTCALL
4566 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4567}
4568
4569/* Relocs on inline plt call sequence insns prior to the call. */
4570
0a1b45a2 4571static bool
23cedd1d
AM
4572is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4573{
4574 return (r_type == R_PPC64_PLT16_HA
4575 || r_type == R_PPC64_PLT16_HI
4576 || r_type == R_PPC64_PLT16_LO
4577 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4578 || r_type == R_PPC64_PLT_PCREL34
4579 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4580 || r_type == R_PPC64_PLTSEQ
4581 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4582}
4583
5bd4f169 4584/* Look through the relocs for a section during the first phase, and
65f38f15 4585 calculate needed space in the global offset table, procedure
5d1634d7 4586 linkage table, and dynamic reloc sections. */
5bd4f169 4587
0a1b45a2 4588static bool
4ce794b7
AM
4589ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4590 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4591{
65f38f15 4592 struct ppc_link_hash_table *htab;
5bd4f169 4593 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4594 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4595 const Elf_Internal_Rela *rel;
4596 const Elf_Internal_Rela *rel_end;
5bd4f169 4597 asection *sreloc;
3a71aa26 4598 struct elf_link_hash_entry *tga, *dottga;
0a1b45a2 4599 bool is_opd;
5bd4f169 4600
0e1862bb 4601 if (bfd_link_relocatable (info))
0a1b45a2 4602 return true;
5bd4f169 4603
0c8d6e5c 4604 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4605
65f38f15 4606 htab = ppc_hash_table (info);
4dfe6ac6 4607 if (htab == NULL)
0a1b45a2 4608 return false;
4dfe6ac6 4609
3a71aa26 4610 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 4611 false, false, true);
3a71aa26 4612 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 4613 false, false, true);
0ffa91dd 4614 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4615 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4616 sreloc = NULL;
b9399fcf 4617 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4618 rel_end = relocs + sec->reloc_count;
4619 for (rel = relocs; rel < rel_end; rel++)
4620 {
4621 unsigned long r_symndx;
4622 struct elf_link_hash_entry *h;
04c9666a 4623 enum elf_ppc64_reloc_type r_type;
727fc41e 4624 int tls_type;
7c8fe5c4 4625 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4626 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4627
4628 r_symndx = ELF64_R_SYM (rel->r_info);
4629 if (r_symndx < symtab_hdr->sh_info)
4630 h = NULL;
4631 else
973a3492
L
4632 {
4633 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4634 h = elf_follow_link (h);
1c865ab2
AM
4635
4636 if (h == htab->elf.hgot)
4637 sec->has_toc_reloc = 1;
973a3492 4638 }
5bd4f169 4639
5663e321
AM
4640 r_type = ELF64_R_TYPE (rel->r_info);
4641 switch (r_type)
4642 {
04bdff6a
AM
4643 case R_PPC64_D34:
4644 case R_PPC64_D34_LO:
4645 case R_PPC64_D34_HI30:
4646 case R_PPC64_D34_HA30:
4647 case R_PPC64_D28:
c213164a
AM
4648 case R_PPC64_TPREL34:
4649 case R_PPC64_DTPREL34:
04bdff6a 4650 case R_PPC64_PCREL34:
5663e321 4651 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4652 case R_PPC64_GOT_TLSGD_PCREL34:
4653 case R_PPC64_GOT_TLSLD_PCREL34:
4654 case R_PPC64_GOT_TPREL_PCREL34:
4655 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4656 case R_PPC64_PLT_PCREL34:
4657 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4658 case R_PPC64_PCREL28:
e10a07b3 4659 htab->has_power10_relocs = 1;
133a1f60
AM
4660 break;
4661 default:
5663e321
AM
4662 break;
4663 }
903b777d
AM
4664
4665 switch (r_type)
4666 {
4667 case R_PPC64_PLT16_HA:
4668 case R_PPC64_GOT_TLSLD16_HA:
4669 case R_PPC64_GOT_TLSGD16_HA:
4670 case R_PPC64_GOT_TPREL16_HA:
4671 case R_PPC64_GOT_DTPREL16_HA:
4672 case R_PPC64_GOT16_HA:
4673 case R_PPC64_TOC16_HA:
4674 case R_PPC64_PLT16_LO:
4675 case R_PPC64_PLT16_LO_DS:
4676 case R_PPC64_GOT_TLSLD16_LO:
4677 case R_PPC64_GOT_TLSGD16_LO:
4678 case R_PPC64_GOT_TPREL16_LO_DS:
4679 case R_PPC64_GOT_DTPREL16_LO_DS:
4680 case R_PPC64_GOT16_LO:
4681 case R_PPC64_GOT16_LO_DS:
4682 case R_PPC64_TOC16_LO:
4683 case R_PPC64_TOC16_LO_DS:
4684 case R_PPC64_GOT_PCREL34:
4685 ppc64_elf_tdata (abfd)->has_optrel = 1;
4686 ppc64_elf_section_data (sec)->has_optrel = 1;
4687 break;
4688 default:
4689 break;
4690 }
4691
f749f26e 4692 ifunc = NULL;
25f23106
AM
4693 if (h != NULL)
4694 {
4695 if (h->type == STT_GNU_IFUNC)
4696 {
4697 h->needs_plt = 1;
4698 ifunc = &h->plt.plist;
4699 }
4700 }
4701 else
4702 {
f1dfbfdb 4703 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
25f23106
AM
4704 abfd, r_symndx);
4705 if (isym == NULL)
0a1b45a2 4706 return false;
25f23106
AM
4707
4708 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4709 {
4710 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4711 rel->r_addend,
37da22e5 4712 NON_GOT | PLT_IFUNC);
25f23106 4713 if (ifunc == NULL)
0a1b45a2 4714 return false;
25f23106
AM
4715 }
4716 }
727fc41e 4717
f749f26e 4718 tls_type = 0;
a33d1f77 4719 switch (r_type)
5bd4f169 4720 {
727fc41e
AM
4721 case R_PPC64_TLSGD:
4722 case R_PPC64_TLSLD:
4723 /* These special tls relocs tie a call to __tls_get_addr with
4724 its parameter symbol. */
37da22e5 4725 if (h != NULL)
ed7007c1 4726 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4727 else
4728 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4729 rel->r_addend,
37da22e5 4730 NON_GOT | TLS_TLS | TLS_MARK))
0a1b45a2 4731 return false;
37da22e5 4732 sec->has_tls_reloc = 1;
727fc41e
AM
4733 break;
4734
411e1bfb
AM
4735 case R_PPC64_GOT_TLSLD16:
4736 case R_PPC64_GOT_TLSLD16_LO:
4737 case R_PPC64_GOT_TLSLD16_HI:
4738 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4739 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4740 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4741 goto dogottls;
4742
4743 case R_PPC64_GOT_TLSGD16:
4744 case R_PPC64_GOT_TLSGD16_LO:
4745 case R_PPC64_GOT_TLSGD16_HI:
4746 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4747 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4748 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4749 goto dogottls;
4750
4751 case R_PPC64_GOT_TPREL16_DS:
4752 case R_PPC64_GOT_TPREL16_LO_DS:
4753 case R_PPC64_GOT_TPREL16_HI:
4754 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4755 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4756 if (bfd_link_dll (info))
411e1bfb
AM
4757 info->flags |= DF_STATIC_TLS;
4758 tls_type = TLS_TLS | TLS_TPREL;
4759 goto dogottls;
4760
4761 case R_PPC64_GOT_DTPREL16_DS:
4762 case R_PPC64_GOT_DTPREL16_LO_DS:
4763 case R_PPC64_GOT_DTPREL16_HI:
4764 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4765 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4766 tls_type = TLS_TLS | TLS_DTPREL;
4767 dogottls:
4768 sec->has_tls_reloc = 1;
066f4018 4769 goto dogot;
411e1bfb 4770
903b777d
AM
4771 case R_PPC64_GOT16:
4772 case R_PPC64_GOT16_LO:
4773 case R_PPC64_GOT16_HI:
65f38f15 4774 case R_PPC64_GOT16_HA:
903b777d 4775 case R_PPC64_GOT16_DS:
066f4018 4776 case R_PPC64_GOT16_LO_DS:
4a421c53 4777 case R_PPC64_GOT_PCREL34:
066f4018 4778 dogot:
65f38f15 4779 /* This symbol requires a global offset table entry. */
4c52953f 4780 sec->has_toc_reloc = 1;
33c0ec9d
AM
4781 if (r_type == R_PPC64_GOT_TLSLD16
4782 || r_type == R_PPC64_GOT_TLSGD16
4783 || r_type == R_PPC64_GOT_TPREL16_DS
4784 || r_type == R_PPC64_GOT_DTPREL16_DS
4785 || r_type == R_PPC64_GOT16
4786 || r_type == R_PPC64_GOT16_DS)
4787 {
4788 htab->do_multi_toc = 1;
d77c8a4b 4789 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4790 }
4791
e717da7e
AM
4792 if (ppc64_elf_tdata (abfd)->got == NULL
4793 && !create_got_section (abfd, info))
0a1b45a2 4794 return false;
5bd4f169
AM
4795
4796 if (h != NULL)
4797 {
411e1bfb
AM
4798 struct ppc_link_hash_entry *eh;
4799 struct got_entry *ent;
65f38f15 4800
ed7007c1 4801 eh = ppc_elf_hash_entry (h);
411e1bfb 4802 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4803 if (ent->addend == rel->r_addend
e717da7e 4804 && ent->owner == abfd
411e1bfb
AM
4805 && ent->tls_type == tls_type)
4806 break;
4807 if (ent == NULL)
5bd4f169 4808 {
986f0783 4809 size_t amt = sizeof (*ent);
4ce794b7 4810 ent = bfd_alloc (abfd, amt);
411e1bfb 4811 if (ent == NULL)
0a1b45a2 4812 return false;
411e1bfb 4813 ent->next = eh->elf.got.glist;
133a1f60 4814 ent->addend = rel->r_addend;
e717da7e 4815 ent->owner = abfd;
411e1bfb 4816 ent->tls_type = tls_type;
0a1b45a2 4817 ent->is_indirect = false;
411e1bfb
AM
4818 ent->got.refcount = 0;
4819 eh->elf.got.glist = ent;
5bd4f169 4820 }
411e1bfb 4821 ent->got.refcount += 1;
e7b938ca 4822 eh->tls_mask |= tls_type;
5bd4f169 4823 }
411e1bfb
AM
4824 else
4825 /* This is a global offset table entry for a local symbol. */
4826 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4827 rel->r_addend, tls_type))
0a1b45a2 4828 return false;
5bd4f169
AM
4829 break;
4830
5bd4f169 4831 case R_PPC64_PLT16_HA:
65f38f15
AM
4832 case R_PPC64_PLT16_HI:
4833 case R_PPC64_PLT16_LO:
08be3224 4834 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4835 case R_PPC64_PLT_PCREL34:
4836 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4837 case R_PPC64_PLT32:
4838 case R_PPC64_PLT64:
cbf95972
AM
4839 /* This symbol requires a procedure linkage table entry. */
4840 plt_list = ifunc;
4841 if (h != NULL)
e054468f 4842 {
e054468f
AM
4843 h->needs_plt = 1;
4844 if (h->root.root.string[0] == '.'
4845 && h->root.root.string[1] != '\0')
ed7007c1
AM
4846 ppc_elf_hash_entry (h)->is_func = 1;
4847 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4848 plt_list = &h->plt.plist;
4849 }
4850 if (plt_list == NULL)
2d7ad24e 4851 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4852 rel->r_addend,
2d7ad24e 4853 NON_GOT | PLT_KEEP);
133a1f60 4854 if (!update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 4855 return false;
5bd4f169
AM
4856 break;
4857
4858 /* The following relocations don't need to propagate the
4859 relocation if linking a shared object since they are
4860 section relative. */
4861 case R_PPC64_SECTOFF:
4862 case R_PPC64_SECTOFF_LO:
4863 case R_PPC64_SECTOFF_HI:
4864 case R_PPC64_SECTOFF_HA:
4865 case R_PPC64_SECTOFF_DS:
4866 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4867 case R_PPC64_DTPREL16:
4868 case R_PPC64_DTPREL16_LO:
4869 case R_PPC64_DTPREL16_HI:
4870 case R_PPC64_DTPREL16_HA:
4871 case R_PPC64_DTPREL16_DS:
4872 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4873 case R_PPC64_DTPREL16_HIGH:
4874 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4875 case R_PPC64_DTPREL16_HIGHER:
4876 case R_PPC64_DTPREL16_HIGHERA:
4877 case R_PPC64_DTPREL16_HIGHEST:
4878 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4879 break;
4880
ad8e1ba5 4881 /* Nor do these. */
25f23106
AM
4882 case R_PPC64_REL16:
4883 case R_PPC64_REL16_LO:
4884 case R_PPC64_REL16_HI:
4885 case R_PPC64_REL16_HA:
4a969973
AM
4886 case R_PPC64_REL16_HIGH:
4887 case R_PPC64_REL16_HIGHA:
4888 case R_PPC64_REL16_HIGHER:
4889 case R_PPC64_REL16_HIGHERA:
4890 case R_PPC64_REL16_HIGHEST:
4891 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4892 case R_PPC64_REL16_HIGHER34:
4893 case R_PPC64_REL16_HIGHERA34:
4894 case R_PPC64_REL16_HIGHEST34:
4895 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4896 case R_PPC64_REL16DX_HA:
25f23106
AM
4897 break;
4898
45965137
AM
4899 /* Not supported as a dynamic relocation. */
4900 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4901 if (bfd_link_pic (info))
45965137
AM
4902 {
4903 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4904 ppc_howto_init ();
695344c0 4905 /* xgettext:c-format */
174d0a74 4906 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4907 "in shared libraries and PIEs\n"),
45965137
AM
4908 abfd, sec, rel->r_offset,
4909 ppc64_elf_howto_table[r_type]->name);
4910 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4911 return false;
45965137
AM
4912 }
4913 break;
4914
ad8e1ba5 4915 case R_PPC64_TOC16:
33c0ec9d
AM
4916 case R_PPC64_TOC16_DS:
4917 htab->do_multi_toc = 1;
d77c8a4b 4918 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4919 /* Fall through. */
ad8e1ba5
AM
4920 case R_PPC64_TOC16_LO:
4921 case R_PPC64_TOC16_HI:
4922 case R_PPC64_TOC16_HA:
ad8e1ba5 4923 case R_PPC64_TOC16_LO_DS:
4c52953f 4924 sec->has_toc_reloc = 1;
ec73ddcd 4925 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
4926 {
4927 /* We may need a copy reloc. */
4928 h->non_got_ref = 1;
4929 /* Strongly prefer a copy reloc over a dynamic reloc.
4930 glibc ld.so as of 2019-08 will error out if one of
4931 these relocations is emitted. */
4932 h->needs_copy = 1;
4933 goto dodyn;
4934 }
ad8e1ba5
AM
4935 break;
4936
006589cf
AM
4937 /* Marker reloc. */
4938 case R_PPC64_ENTRY:
4939 break;
4940
5bd4f169
AM
4941 /* This relocation describes the C++ object vtable hierarchy.
4942 Reconstruct it for later use during GC. */
4943 case R_PPC64_GNU_VTINHERIT:
c152c796 4944 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
0a1b45a2 4945 return false;
5bd4f169
AM
4946 break;
4947
4948 /* This relocation describes which C++ vtable entries are actually
4949 used. Record for later use during GC. */
4950 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4951 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
0a1b45a2 4952 return false;
5bd4f169
AM
4953 break;
4954
721956f4
AM
4955 case R_PPC64_REL14:
4956 case R_PPC64_REL14_BRTAKEN:
4957 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4958 {
4959 asection *dest = NULL;
4960
4961 /* Heuristic: If jumping outside our section, chances are
4962 we are going to need a stub. */
4963 if (h != NULL)
4964 {
4965 /* If the sym is weak it may be overridden later, so
4966 don't assume we know where a weak sym lives. */
4967 if (h->root.type == bfd_link_hash_defined)
4968 dest = h->root.u.def.section;
4969 }
4970 else
87d72d41
AM
4971 {
4972 Elf_Internal_Sym *isym;
4973
f1dfbfdb 4974 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
4975 abfd, r_symndx);
4976 if (isym == NULL)
0a1b45a2 4977 return false;
87d72d41
AM
4978
4979 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4980 }
4981
220c76dd 4982 if (dest != sec)
7c8fe5c4 4983 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4984 }
3e04d765
AM
4985 goto rel24;
4986
4987 case R_PPC64_PLTCALL:
5663e321 4988 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4989 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4990 /* Fall through. */
4991
5d1634d7 4992 case R_PPC64_REL24:
05d0e962 4993 case R_PPC64_REL24_NOTOC:
3e04d765 4994 rel24:
cbf95972
AM
4995 plt_list = ifunc;
4996 if (h != NULL)
5d1634d7 4997 {
e054468f
AM
4998 h->needs_plt = 1;
4999 if (h->root.root.string[0] == '.'
5000 && h->root.root.string[1] != '\0')
ed7007c1 5001 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 5002
3a71aa26 5003 if (h == tga || h == dottga)
cbf95972
AM
5004 {
5005 sec->has_tls_reloc = 1;
5006 if (rel != relocs
5007 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5008 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5009 /* We have a new-style __tls_get_addr call with
5010 a marker reloc. */
5011 ;
5012 else
5013 /* Mark this section as having an old-style call. */
9737e8af 5014 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5015 }
5016 plt_list = &h->plt.plist;
411e1bfb 5017 }
cbf95972
AM
5018
5019 /* We may need a .plt entry if the function this reloc
5020 refers to is in a shared lib. */
5021 if (plt_list
133a1f60 5022 && !update_plt_info (abfd, plt_list, rel->r_addend))
0a1b45a2 5023 return false;
411e1bfb
AM
5024 break;
5025
cbf95972
AM
5026 case R_PPC64_ADDR14:
5027 case R_PPC64_ADDR14_BRNTAKEN:
5028 case R_PPC64_ADDR14_BRTAKEN:
5029 case R_PPC64_ADDR24:
5030 goto dodyn;
5031
411e1bfb
AM
5032 case R_PPC64_TPREL64:
5033 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5034 if (bfd_link_dll (info))
411e1bfb
AM
5035 info->flags |= DF_STATIC_TLS;
5036 goto dotlstoc;
5037
5038 case R_PPC64_DTPMOD64:
5039 if (rel + 1 < rel_end
5040 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5041 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5042 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5043 else
951fd09b 5044 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5045 goto dotlstoc;
5046
5047 case R_PPC64_DTPREL64:
5048 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5049 if (rel != relocs
5050 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5051 && rel[-1].r_offset == rel->r_offset - 8)
5052 /* This is the second reloc of a dtpmod, dtprel pair.
5053 Don't mark with TLS_DTPREL. */
5054 goto dodyn;
5055
5056 dotlstoc:
5057 sec->has_tls_reloc = 1;
5058 if (h != NULL)
ed7007c1 5059 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5060 else
5061 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5062 rel->r_addend, tls_type))
0a1b45a2 5063 return false;
411e1bfb 5064
7c8fe5c4
AM
5065 ppc64_sec = ppc64_elf_section_data (sec);
5066 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5067 {
3a71aa26
AM
5068 bfd_size_type amt;
5069
e7b938ca 5070 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5071 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5072 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5073 if (ppc64_sec->u.toc.symndx == NULL)
0a1b45a2 5074 return false;
3a71aa26
AM
5075 amt = sec->size * sizeof (bfd_vma) / 8;
5076 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5077 if (ppc64_sec->u.toc.add == NULL)
0a1b45a2 5078 return false;
7c8fe5c4
AM
5079 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5080 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5081 }
5082 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5083 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5084 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5085
5086 /* Mark the second slot of a GD or LD entry.
5087 -1 to indicate GD and -2 to indicate LD. */
5088 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5089 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5090 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5091 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5092 goto dodyn;
5093
411e1bfb
AM
5094 case R_PPC64_TPREL16_HI:
5095 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5096 case R_PPC64_TPREL16_HIGH:
5097 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5098 case R_PPC64_TPREL16_HIGHER:
5099 case R_PPC64_TPREL16_HIGHERA:
5100 case R_PPC64_TPREL16_HIGHEST:
5101 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5102 sec->has_tls_reloc = 1;
5103 /* Fall through. */
c213164a 5104 case R_PPC64_TPREL34:
252dcdf4
AM
5105 case R_PPC64_TPREL16:
5106 case R_PPC64_TPREL16_DS:
5107 case R_PPC64_TPREL16_LO:
5108 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5109 if (bfd_link_dll (info))
5110 info->flags |= DF_STATIC_TLS;
5111 goto dodyn;
5d1634d7 5112
e86ce104 5113 case R_PPC64_ADDR64:
b9399fcf 5114 if (is_opd
1e2f5b6e 5115 && rel + 1 < rel_end
4ce794b7 5116 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5117 {
8387904d 5118 if (h != NULL)
ed7007c1 5119 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5120 }
e86ce104
AM
5121 /* Fall through. */
5122
65f38f15
AM
5123 case R_PPC64_ADDR16:
5124 case R_PPC64_ADDR16_DS:
5125 case R_PPC64_ADDR16_HA:
5126 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5127 case R_PPC64_ADDR16_HIGH:
5128 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5129 case R_PPC64_ADDR16_HIGHER:
5130 case R_PPC64_ADDR16_HIGHERA:
5131 case R_PPC64_ADDR16_HIGHEST:
5132 case R_PPC64_ADDR16_HIGHESTA:
5133 case R_PPC64_ADDR16_LO:
5134 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5135 case R_PPC64_D34:
5136 case R_PPC64_D34_LO:
5137 case R_PPC64_D34_HI30:
5138 case R_PPC64_D34_HA30:
5139 case R_PPC64_ADDR16_HIGHER34:
5140 case R_PPC64_ADDR16_HIGHERA34:
5141 case R_PPC64_ADDR16_HIGHEST34:
5142 case R_PPC64_ADDR16_HIGHESTA34:
5143 case R_PPC64_D28:
0e1862bb 5144 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5145 && rel->r_addend == 0)
5146 {
5147 /* We may need a .plt entry if this reloc refers to a
5148 function in a shared lib. */
5663e321 5149 if (!update_plt_info (abfd, &h->plt.plist, 0))
0a1b45a2 5150 return false;
a345bc8d
AM
5151 h->pointer_equality_needed = 1;
5152 }
5153 /* Fall through. */
5154
5155 case R_PPC64_REL30:
5156 case R_PPC64_REL32:
5157 case R_PPC64_REL64:
65f38f15 5158 case R_PPC64_ADDR32:
65f38f15
AM
5159 case R_PPC64_UADDR16:
5160 case R_PPC64_UADDR32:
5161 case R_PPC64_UADDR64:
5bd4f169 5162 case R_PPC64_TOC:
ec73ddcd 5163 if (h != NULL && bfd_link_executable (info))
81848ca0 5164 /* We may need a copy reloc. */
f5385ebf 5165 h->non_got_ref = 1;
81848ca0 5166
41bd81ab 5167 /* Don't propagate .opd relocs. */
b9399fcf 5168 if (NO_OPD_RELOCS && is_opd)
e86ce104 5169 break;
e86ce104 5170
65f38f15
AM
5171 /* If we are creating a shared library, and this is a reloc
5172 against a global symbol, or a non PC relative reloc
5173 against a local symbol, then we need to copy the reloc
5174 into the shared library. However, if we are linking with
5175 -Bsymbolic, we do not need to copy a reloc against a
5176 global symbol which is defined in an object we are
5177 including in the link (i.e., DEF_REGULAR is set). At
5178 this point we have not seen all the input files, so it is
5179 possible that DEF_REGULAR is not set now but will be set
5180 later (it is never cleared). In case of a weak definition,
5181 DEF_REGULAR may be cleared later by a strong definition in
5182 a shared library. We account for that possibility below by
f4656909 5183 storing information in the dyn_relocs field of the hash
65f38f15
AM
5184 table entry. A similar situation occurs when creating
5185 shared libraries and symbol visibility changes render the
5186 symbol local.
5187
5188 If on the other hand, we are creating an executable, we
5189 may need to keep relocations for symbols satisfied by a
5190 dynamic library if we manage to avoid copy relocs for the
5191 symbol. */
411e1bfb 5192 dodyn:
ec73ddcd
AM
5193 if ((h != NULL
5194 && (h->root.type == bfd_link_hash_defweak
5195 || !h->def_regular))
5196 || (h != NULL
5197 && !bfd_link_executable (info)
5198 && !SYMBOLIC_BIND (info, h))
5199 || (bfd_link_pic (info)
5200 && must_be_dyn_reloc (info, r_type))
0e1862bb 5201 || (!bfd_link_pic (info)
25f23106 5202 && ifunc != NULL))
5bd4f169 5203 {
65f38f15
AM
5204 /* We must copy these reloc types into the output file.
5205 Create a reloc section in dynobj and make room for
5206 this reloc. */
5bd4f169
AM
5207 if (sreloc == NULL)
5208 {
83bac4b0 5209 sreloc = _bfd_elf_make_dynamic_reloc_section
0a1b45a2 5210 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
65f38f15 5211
5bd4f169 5212 if (sreloc == NULL)
0a1b45a2 5213 return false;
5bd4f169
AM
5214 }
5215
65f38f15
AM
5216 /* If this is a global symbol, we count the number of
5217 relocations we need for this symbol. */
5218 if (h != NULL)
5219 {
19e08130
AM
5220 struct elf_dyn_relocs *p;
5221 struct elf_dyn_relocs **head;
5222
190eb1dd 5223 head = &h->dyn_relocs;
19e08130
AM
5224 p = *head;
5225 if (p == NULL || p->sec != sec)
5226 {
5227 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5228 if (p == NULL)
0a1b45a2 5229 return false;
19e08130
AM
5230 p->next = *head;
5231 *head = p;
5232 p->sec = sec;
5233 p->count = 0;
5234 p->pc_count = 0;
5235 }
5236 p->count += 1;
5237 if (!must_be_dyn_reloc (info, r_type))
5238 p->pc_count += 1;
65f38f15
AM
5239 }
5240 else
5241 {
ec338859
AM
5242 /* Track dynamic relocs needed for local syms too.
5243 We really need local syms available to do this
5244 easily. Oh well. */
19e08130
AM
5245 struct ppc_dyn_relocs *p;
5246 struct ppc_dyn_relocs **head;
0a1b45a2 5247 bool is_ifunc;
ec338859 5248 asection *s;
6edfbbad 5249 void *vpp;
87d72d41 5250 Elf_Internal_Sym *isym;
6edfbbad 5251
f1dfbfdb 5252 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5253 abfd, r_symndx);
5254 if (isym == NULL)
0a1b45a2 5255 return false;
ec338859 5256
87d72d41
AM
5257 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5258 if (s == NULL)
5259 s = sec;
5260
6edfbbad 5261 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5262 head = (struct ppc_dyn_relocs **) vpp;
5263 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5264 p = *head;
5265 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5266 p = p->next;
5267 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5268 {
5269 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5270 if (p == NULL)
0a1b45a2 5271 return false;
19e08130
AM
5272 p->next = *head;
5273 *head = p;
5274 p->sec = sec;
5275 p->ifunc = is_ifunc;
5276 p->count = 0;
5277 }
5278 p->count += 1;
ec338859 5279 }
65f38f15 5280 }
5bd4f169 5281 break;
65f38f15
AM
5282
5283 default:
96e0dda4 5284 break;
5bd4f169
AM
5285 }
5286 }
5287
0a1b45a2 5288 return true;
5bd4f169
AM
5289}
5290
ee67d69a
AM
5291/* Merge backend specific data from an object file to the output
5292 object file when linking. */
5293
0a1b45a2 5294static bool
50e03d47 5295ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5296{
50e03d47 5297 bfd *obfd = info->output_bfd;
ee67d69a
AM
5298 unsigned long iflags, oflags;
5299
5300 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
0a1b45a2 5301 return true;
ee67d69a
AM
5302
5303 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
0a1b45a2 5304 return true;
ee67d69a 5305
50e03d47 5306 if (!_bfd_generic_verify_endian_match (ibfd, info))
0a1b45a2 5307 return false;
ee67d69a
AM
5308
5309 iflags = elf_elfheader (ibfd)->e_flags;
5310 oflags = elf_elfheader (obfd)->e_flags;
5311
f6c7c3e8 5312 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5313 {
4eca0228 5314 _bfd_error_handler
695344c0 5315 /* xgettext:c-format */
871b3ab2 5316 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a 5317 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5318 return false;
ee67d69a 5319 }
f6c7c3e8 5320 else if (iflags != oflags && iflags != 0)
ee67d69a 5321 {
4eca0228 5322 _bfd_error_handler
695344c0 5323 /* xgettext:c-format */
871b3ab2 5324 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5325 ibfd, iflags, oflags);
5326 bfd_set_error (bfd_error_bad_value);
0a1b45a2 5327 return false;
ee67d69a
AM
5328 }
5329
4a91d0ba 5330 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
0a1b45a2 5331 return false;
005d79fd 5332
ee67d69a 5333 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5334 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5335}
5336
0a1b45a2 5337static bool
ee67d69a
AM
5338ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5339{
5340 /* Print normal ELF private data. */
5341 _bfd_elf_print_private_bfd_data (abfd, ptr);
5342
5343 if (elf_elfheader (abfd)->e_flags != 0)
5344 {
5345 FILE *file = ptr;
5346
ee67d69a
AM
5347 fprintf (file, _("private flags = 0x%lx:"),
5348 elf_elfheader (abfd)->e_flags);
5349
5350 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5351 fprintf (file, _(" [abiv%ld]"),
5352 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5353 fputc ('\n', file);
5354 }
5355
0a1b45a2 5356 return true;
ee67d69a
AM
5357}
5358
8387904d 5359/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5360 of the code entry point, and its section, which must be in the same
5361 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5362
5363static bfd_vma
5364opd_entry_value (asection *opd_sec,
5365 bfd_vma offset,
5366 asection **code_sec,
aef36ac1 5367 bfd_vma *code_off,
0a1b45a2 5368 bool in_code_sec)
8387904d
AM
5369{
5370 bfd *opd_bfd = opd_sec->owner;
8860955f 5371 Elf_Internal_Rela *relocs;
8387904d 5372 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5373 bfd_vma val;
8387904d 5374
9f296da3
AM
5375 /* No relocs implies we are linking a --just-symbols object, or looking
5376 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5377 if (opd_sec->reloc_count == 0)
5378 {
729eabd5 5379 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5380
729eabd5
AM
5381 if (contents == NULL)
5382 {
5383 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5384 return (bfd_vma) -1;
5385 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5386 }
ee1e4ede 5387
dbb3fbbb 5388 /* PR 17512: file: 64b9dfbb. */
451dfd38 5389 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5390 return (bfd_vma) -1;
5391
729eabd5 5392 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5393 if (code_sec != NULL)
5394 {
5395 asection *sec, *likely = NULL;
ee1e4ede 5396
aef36ac1 5397 if (in_code_sec)
4b85d634 5398 {
aef36ac1
AM
5399 sec = *code_sec;
5400 if (sec->vma <= val
5401 && val < sec->vma + sec->size)
5402 likely = sec;
5403 else
5404 val = -1;
5405 }
5406 else
5407 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5408 if (sec->vma <= val
5409 && (sec->flags & SEC_LOAD) != 0
5410 && (sec->flags & SEC_ALLOC) != 0)
5411 likely = sec;
5412 if (likely != NULL)
5413 {
5414 *code_sec = likely;
5415 if (code_off != NULL)
5416 *code_off = val - likely->vma;
4b85d634
AM
5417 }
5418 }
aef36ac1 5419 return val;
4b85d634
AM
5420 }
5421
0c8d6e5c 5422 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5423
729eabd5 5424 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f 5425 if (relocs == NULL)
0a1b45a2 5426 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
877a8638
NC
5427 /* PR 17512: file: df8e1fd6. */
5428 if (relocs == NULL)
5429 return (bfd_vma) -1;
645ea6a9 5430
8387904d 5431 /* Go find the opd reloc at the sym address. */
8860955f 5432 lo = relocs;
8387904d 5433 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5434 val = (bfd_vma) -1;
8387904d
AM
5435 while (lo < hi)
5436 {
5437 look = lo + (hi - lo) / 2;
5438 if (look->r_offset < offset)
5439 lo = look + 1;
5440 else if (look->r_offset > offset)
5441 hi = look;
5442 else
5443 {
0ffa91dd
NC
5444 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5445
8387904d
AM
5446 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5447 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5448 {
5449 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5450 asection *sec = NULL;
8387904d 5451
b53dfeb2
AM
5452 if (symndx >= symtab_hdr->sh_info
5453 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5454 {
5455 struct elf_link_hash_entry **sym_hashes;
5456 struct elf_link_hash_entry *rh;
5457
5458 sym_hashes = elf_sym_hashes (opd_bfd);
5459 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5460 if (rh != NULL)
5461 {
5462 rh = elf_follow_link (rh);
bb854a36
AM
5463 if (rh->root.type != bfd_link_hash_defined
5464 && rh->root.type != bfd_link_hash_defweak)
5465 break;
5466 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5467 {
bb854a36
AM
5468 val = rh->root.u.def.value;
5469 sec = rh->root.u.def.section;
b53dfeb2
AM
5470 }
5471 }
5472 }
5473
5474 if (sec == NULL)
5475 {
5476 Elf_Internal_Sym *sym;
5477
5478 if (symndx < symtab_hdr->sh_info)
5479 {
5480 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5481 if (sym == NULL)
5482 {
5483 size_t symcnt = symtab_hdr->sh_info;
5484 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5485 symcnt, 0,
5486 NULL, NULL, NULL);
5487 if (sym == NULL)
5488 break;
5489 symtab_hdr->contents = (bfd_byte *) sym;
5490 }
5491 sym += symndx;
128205bb
AM
5492 }
5493 else
5494 {
b53dfeb2
AM
5495 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5496 1, symndx,
5497 NULL, NULL, NULL);
128205bb
AM
5498 if (sym == NULL)
5499 break;
128205bb 5500 }
b53dfeb2
AM
5501 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5502 if (sec == NULL)
5503 break;
5504 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5505 val = sym->st_value;
8387904d 5506 }
b53dfeb2 5507
8387904d
AM
5508 val += look->r_addend;
5509 if (code_off != NULL)
5510 *code_off = val;
5511 if (code_sec != NULL)
aef36ac1
AM
5512 {
5513 if (in_code_sec && *code_sec != sec)
5514 return -1;
5515 else
5516 *code_sec = sec;
5517 }
b53dfeb2 5518 if (sec->output_section != NULL)
8387904d 5519 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5520 }
5521 break;
5522 }
5523 }
645ea6a9 5524
645ea6a9 5525 return val;
8387904d
AM
5526}
5527
aef36ac1
AM
5528/* If the ELF symbol SYM might be a function in SEC, return the
5529 function size and set *CODE_OFF to the function's entry point,
5530 otherwise return zero. */
9f296da3 5531
aef36ac1
AM
5532static bfd_size_type
5533ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5534 bfd_vma *code_off)
9f296da3 5535{
aef36ac1 5536 bfd_size_type size;
24aebc79 5537 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
aef36ac1
AM
5538
5539 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5540 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5541 return 0;
5542
24aebc79
NC
5543 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5544
5545 /* In theory we should check that the symbol's type satisfies
5546 _bfd_elf_is_function_type(), but there are some function-like
5547 symbols which would fail this test. (eg _start). Instead
5548 we check for hidden, local, notype symbols with zero size.
5549 This type of symbol is generated by the annobin plugin for gcc
5550 and clang, and should not be considered to be a function symbol. */
5551 if (size == 0
5552 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5553 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5554 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5555 return 0;
aef36ac1
AM
5556
5557 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5558 {
b07bca4e
AM
5559 struct _opd_sec_data *opd = get_opd_info (sym->section);
5560 bfd_vma symval = sym->value;
5561
5562 if (opd != NULL
5563 && opd->adjust != NULL
5564 && elf_section_data (sym->section)->relocs != NULL)
5565 {
5566 /* opd_entry_value will use cached relocs that have been
5567 adjusted, but with raw symbols. That means both local
5568 and global symbols need adjusting. */
5569 long adjust = opd->adjust[OPD_NDX (symval)];
5570 if (adjust == -1)
5571 return 0;
5572 symval += adjust;
5573 }
5574
5575 if (opd_entry_value (sym->section, symval,
0a1b45a2 5576 &sec, code_off, true) == (bfd_vma) -1)
aef36ac1
AM
5577 return 0;
5578 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5579 symbol. This size has nothing to do with the code size of the
5580 function, which is what we're supposed to return, but the
5581 code size isn't available without looking up the dot-sym.
5582 However, doing that would be a waste of time particularly
5583 since elf_find_function will look at the dot-sym anyway.
5584 Now, elf_find_function will keep the largest size of any
5585 function sym found at the code address of interest, so return
5586 1 here to avoid it incorrectly caching a larger function size
5587 for a small function. This does mean we return the wrong
5588 size for a new-ABI function of size 24, but all that does is
5589 disable caching for such functions. */
5590 if (size == 24)
5591 size = 1;
9f296da3 5592 }
aef36ac1
AM
5593 else
5594 {
5595 if (sym->section != sec)
5596 return 0;
5597 *code_off = sym->value;
5598 }
24aebc79
NC
5599
5600 /* Do not return 0 for the function's size. */
5601 return size ? size : 1;
9f296da3
AM
5602}
5603
f378ab09
AM
5604/* Return true if symbol is a strong function defined in an ELFv2
5605 object with st_other localentry bits of zero, ie. its local entry
5606 point coincides with its global entry point. */
5607
0a1b45a2 5608static bool
f378ab09
AM
5609is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5610{
5611 return (h != NULL
5612 && h->type == STT_FUNC
5613 && h->root.type == bfd_link_hash_defined
5614 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5615 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5616 && is_ppc64_elf (h->root.u.def.section->owner)
5617 && abiversion (h->root.u.def.section->owner) >= 2);
5618}
5619
854b41e7
AM
5620/* Return true if symbol is defined in a regular object file. */
5621
0a1b45a2 5622static bool
854b41e7
AM
5623is_static_defined (struct elf_link_hash_entry *h)
5624{
5625 return ((h->root.type == bfd_link_hash_defined
5626 || h->root.type == bfd_link_hash_defweak)
5627 && h->root.u.def.section != NULL
5628 && h->root.u.def.section->output_section != NULL);
5629}
5630
b31867b6
AM
5631/* If FDH is a function descriptor symbol, return the associated code
5632 entry symbol if it is defined. Return NULL otherwise. */
5633
5634static struct ppc_link_hash_entry *
5635defined_code_entry (struct ppc_link_hash_entry *fdh)
5636{
5637 if (fdh->is_func_descriptor)
5638 {
5639 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5640 if (fh->elf.root.type == bfd_link_hash_defined
5641 || fh->elf.root.type == bfd_link_hash_defweak)
5642 return fh;
5643 }
5644 return NULL;
5645}
5646
5647/* If FH is a function code entry symbol, return the associated
5648 function descriptor symbol if it is defined. Return NULL otherwise. */
5649
5650static struct ppc_link_hash_entry *
5651defined_func_desc (struct ppc_link_hash_entry *fh)
5652{
5653 if (fh->oh != NULL
5654 && fh->oh->is_func_descriptor)
5655 {
5656 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5657 if (fdh->elf.root.type == bfd_link_hash_defined
5658 || fdh->elf.root.type == bfd_link_hash_defweak)
5659 return fdh;
5660 }
5661 return NULL;
5662}
5663
ed7007c1
AM
5664/* Given H is a symbol that satisfies is_static_defined, return the
5665 value in the output file. */
5666
5667static bfd_vma
5668defined_sym_val (struct elf_link_hash_entry *h)
5669{
5670 return (h->root.u.def.section->output_section->vma
5671 + h->root.u.def.section->output_offset
5672 + h->root.u.def.value);
5673}
5674
5675/* Return true if H matches __tls_get_addr or one of its variants. */
5676
0a1b45a2 5677static bool
ed7007c1
AM
5678is_tls_get_addr (struct elf_link_hash_entry *h,
5679 struct ppc_link_hash_table *htab)
5680{
30845f11
AM
5681 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5682 || h == elf_hash_entry (htab->tga_desc_fd)
5683 || h == elf_hash_entry (htab->tls_get_addr)
5684 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5685}
5686
0a1b45a2 5687static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
8c5b4e52
AM
5688
5689/* Garbage collect sections, after first dealing with dot-symbols. */
5690
0a1b45a2 5691static bool
8c5b4e52
AM
5692ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5693{
5694 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5695
5696 if (htab != NULL && htab->need_func_desc_adj)
5697 {
5698 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5699 htab->need_func_desc_adj = 0;
5700 }
5701 return bfd_elf_gc_sections (abfd, info);
5702}
5703
74f0fb50
AM
5704/* Mark all our entry sym sections, both opd and code section. */
5705
5706static void
5707ppc64_elf_gc_keep (struct bfd_link_info *info)
5708{
5709 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5710 struct bfd_sym_chain *sym;
5711
4dfe6ac6
NC
5712 if (htab == NULL)
5713 return;
5714
74f0fb50
AM
5715 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5716 {
b31867b6 5717 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5718 asection *sec;
5719
ed7007c1 5720 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
0a1b45a2 5721 false, false, true));
74f0fb50
AM
5722 if (eh == NULL)
5723 continue;
5724 if (eh->elf.root.type != bfd_link_hash_defined
5725 && eh->elf.root.type != bfd_link_hash_defweak)
5726 continue;
5727
b31867b6
AM
5728 fh = defined_code_entry (eh);
5729 if (fh != NULL)
74f0fb50 5730 {
b31867b6 5731 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5732 sec->flags |= SEC_KEEP;
5733 }
5734 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5735 && opd_entry_value (eh->elf.root.u.def.section,
5736 eh->elf.root.u.def.value,
0a1b45a2 5737 &sec, NULL, false) != (bfd_vma) -1)
74f0fb50
AM
5738 sec->flags |= SEC_KEEP;
5739
5740 sec = eh->elf.root.u.def.section;
5741 sec->flags |= SEC_KEEP;
5742 }
5743}
5744
64d03ab5
AM
5745/* Mark sections containing dynamically referenced symbols. When
5746 building shared libraries, we must assume that any visible symbol is
5747 referenced. */
5748
0a1b45a2 5749static bool
64d03ab5
AM
5750ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5751{
5752 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5753 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5754 struct ppc_link_hash_entry *fdh;
b407645f 5755 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5756
64d03ab5 5757 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5758 fdh = defined_func_desc (eh);
5759 if (fdh != NULL)
5760 eh = fdh;
64d03ab5
AM
5761
5762 if ((eh->elf.root.type == bfd_link_hash_defined
5763 || eh->elf.root.type == bfd_link_hash_defweak)
b80e421f
AM
5764 && (!eh->elf.start_stop
5765 || eh->elf.root.ldscript_def
5766 || !info->start_stop_gc)
87e79a65 5767 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5768 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5769 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5770 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5771 && (!bfd_link_executable (info)
e278ae05 5772 || info->gc_keep_exported
b407645f
AM
5773 || info->export_dynamic
5774 || (eh->elf.dynamic
5775 && d != NULL
2cdcc330
AM
5776 && (*d->match) (&d->head, NULL,
5777 eh->elf.root.root.string)))
e278ae05 5778 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5779 || !bfd_hide_sym_by_version (info->version_info,
5780 eh->elf.root.root.string)))))
64d03ab5
AM
5781 {
5782 asection *code_sec;
b31867b6 5783 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5784
5785 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5786
5787 /* Function descriptor syms cause the associated
5788 function code sym section to be marked. */
b31867b6
AM
5789 fh = defined_code_entry (eh);
5790 if (fh != NULL)
5791 {
5792 code_sec = fh->elf.root.u.def.section;
5793 code_sec->flags |= SEC_KEEP;
5794 }
64d03ab5
AM
5795 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5796 && opd_entry_value (eh->elf.root.u.def.section,
5797 eh->elf.root.u.def.value,
0a1b45a2 5798 &code_sec, NULL, false) != (bfd_vma) -1)
64d03ab5
AM
5799 code_sec->flags |= SEC_KEEP;
5800 }
5801
0a1b45a2 5802 return true;
64d03ab5
AM
5803}
5804
5bd4f169
AM
5805/* Return the section that should be marked against GC for a given
5806 relocation. */
5807
5808static asection *
4ce794b7 5809ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5810 struct bfd_link_info *info,
4ce794b7
AM
5811 Elf_Internal_Rela *rel,
5812 struct elf_link_hash_entry *h,
5813 Elf_Internal_Sym *sym)
5bd4f169 5814{
ccfa59ea
AM
5815 asection *rsec;
5816
ccfa59ea
AM
5817 /* Syms return NULL if we're marking .opd, so we avoid marking all
5818 function sections, as all functions are referenced in .opd. */
5819 rsec = NULL;
5820 if (get_opd_info (sec) != NULL)
5821 return rsec;
1e2f5b6e 5822
5bd4f169
AM
5823 if (h != NULL)
5824 {
04c9666a 5825 enum elf_ppc64_reloc_type r_type;
b31867b6 5826 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5827
4ce794b7 5828 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5829 switch (r_type)
5bd4f169
AM
5830 {
5831 case R_PPC64_GNU_VTINHERIT:
5832 case R_PPC64_GNU_VTENTRY:
5833 break;
5834
5835 default:
5836 switch (h->root.type)
5837 {
5838 case bfd_link_hash_defined:
5839 case bfd_link_hash_defweak:
ed7007c1 5840 eh = ppc_elf_hash_entry (h);
b31867b6
AM
5841 fdh = defined_func_desc (eh);
5842 if (fdh != NULL)
8c5b4e52
AM
5843 {
5844 /* -mcall-aixdesc code references the dot-symbol on
5845 a call reloc. Mark the function descriptor too
5846 against garbage collection. */
5847 fdh->elf.mark = 1;
60d67dc8
AM
5848 if (fdh->elf.is_weakalias)
5849 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5850 eh = fdh;
5851 }
1e2f5b6e
AM
5852
5853 /* Function descriptor syms cause the associated
5854 function code sym section to be marked. */
b31867b6
AM
5855 fh = defined_code_entry (eh);
5856 if (fh != NULL)
ccfa59ea
AM
5857 {
5858 /* They also mark their opd section. */
74f0fb50 5859 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5860
b31867b6 5861 rsec = fh->elf.root.u.def.section;
ccfa59ea 5862 }
8387904d
AM
5863 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5864 && opd_entry_value (eh->elf.root.u.def.section,
5865 eh->elf.root.u.def.value,
0a1b45a2 5866 &rsec, NULL, false) != (bfd_vma) -1)
74f0fb50 5867 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5868 else
1e2f5b6e
AM
5869 rsec = h->root.u.def.section;
5870 break;
5bd4f169
AM
5871
5872 case bfd_link_hash_common:
1e2f5b6e
AM
5873 rsec = h->root.u.c.p->section;
5874 break;
5bd4f169
AM
5875
5876 default:
fb34365b 5877 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5878 }
5879 }
5880 }
5881 else
5882 {
74f0fb50 5883 struct _opd_sec_data *opd;
1e2f5b6e
AM
5884
5885 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5886 opd = get_opd_info (rsec);
5887 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5888 {
74f0fb50 5889 rsec->gc_mark = 1;
ccfa59ea 5890
51aecdc5 5891 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5892 }
5bd4f169
AM
5893 }
5894
1e2f5b6e 5895 return rsec;
5bd4f169
AM
5896}
5897
deb0e272
AM
5898/* The maximum size of .sfpr. */
5899#define SFPR_MAX (218*4)
5900
5901struct sfpr_def_parms
5902{
699733f6
AM
5903 const char name[12];
5904 unsigned char lo, hi;
2cdcc330
AM
5905 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5906 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5907};
5908
a4b6fadd
AM
5909/* Auto-generate _save*, _rest* functions in .sfpr.
5910 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5911 instead. */
deb0e272 5912
0a1b45a2 5913static bool
a4b6fadd
AM
5914sfpr_define (struct bfd_link_info *info,
5915 const struct sfpr_def_parms *parm,
5916 asection *stub_sec)
deb0e272
AM
5917{
5918 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5919 unsigned int i;
5920 size_t len = strlen (parm->name);
0a1b45a2 5921 bool writing = false;
699733f6 5922 char sym[16];
deb0e272 5923
4dfe6ac6 5924 if (htab == NULL)
0a1b45a2 5925 return false;
4dfe6ac6 5926
deb0e272
AM
5927 memcpy (sym, parm->name, len);
5928 sym[len + 2] = 0;
5929
5930 for (i = parm->lo; i <= parm->hi; i++)
5931 {
a4b6fadd 5932 struct ppc_link_hash_entry *h;
deb0e272
AM
5933
5934 sym[len + 0] = i / 10 + '0';
5935 sym[len + 1] = i % 10 + '0';
ed7007c1 5936 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
0a1b45a2 5937 writing, true, true));
a4b6fadd 5938 if (stub_sec != NULL)
deb0e272 5939 {
a4b6fadd
AM
5940 if (h != NULL
5941 && h->elf.root.type == bfd_link_hash_defined
5942 && h->elf.root.u.def.section == htab->sfpr)
5943 {
5944 struct elf_link_hash_entry *s;
5945 char buf[32];
5946 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
0a1b45a2 5947 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
a4b6fadd 5948 if (s == NULL)
0a1b45a2 5949 return false;
779f2ae7 5950 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
5951 {
5952 s->root.type = bfd_link_hash_defined;
5953 s->root.u.def.section = stub_sec;
7dda8d3c 5954 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5955 + h->elf.root.u.def.value);
5956 s->ref_regular = 1;
5957 s->def_regular = 1;
5958 s->ref_regular_nonweak = 1;
5959 s->forced_local = 1;
5960 s->non_elf = 0;
5961 s->root.linker_def = 1;
5962 }
5963 }
5964 continue;
5965 }
5966 if (h != NULL)
5967 {
5968 h->save_res = 1;
5969 if (!h->elf.def_regular)
deb0e272 5970 {
a4b6fadd
AM
5971 h->elf.root.type = bfd_link_hash_defined;
5972 h->elf.root.u.def.section = htab->sfpr;
5973 h->elf.root.u.def.value = htab->sfpr->size;
5974 h->elf.type = STT_FUNC;
5975 h->elf.def_regular = 1;
b32547cd 5976 h->elf.non_elf = 0;
0a1b45a2
AM
5977 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
5978 writing = true;
deb0e272 5979 if (htab->sfpr->contents == NULL)
a4b6fadd 5980 {
2cdcc330
AM
5981 htab->sfpr->contents
5982 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd 5983 if (htab->sfpr->contents == NULL)
0a1b45a2 5984 return false;
a4b6fadd 5985 }
deb0e272
AM
5986 }
5987 }
5988 if (writing)
5989 {
5990 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5991 if (i != parm->hi)
5992 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5993 else
5994 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5995 htab->sfpr->size = p - htab->sfpr->contents;
5996 }
5997 }
5998
0a1b45a2 5999 return true;
deb0e272
AM
6000}
6001
6002static bfd_byte *
6003savegpr0 (bfd *abfd, bfd_byte *p, int r)
6004{
6005 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6006 return p + 4;
6007}
6008
6009static bfd_byte *
6010savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6011{
6012 p = savegpr0 (abfd, p, r);
a078d95a 6013 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6014 p = p + 4;
6015 bfd_put_32 (abfd, BLR, p);
6016 return p + 4;
6017}
6018
6019static bfd_byte *
6020restgpr0 (bfd *abfd, bfd_byte *p, int r)
6021{
6022 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6023 return p + 4;
6024}
6025
6026static bfd_byte *
6027restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6028{
a078d95a 6029 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6030 p = p + 4;
6031 p = restgpr0 (abfd, p, r);
6032 bfd_put_32 (abfd, MTLR_R0, p);
6033 p = p + 4;
6034 if (r == 29)
6035 {
6036 p = restgpr0 (abfd, p, 30);
6037 p = restgpr0 (abfd, p, 31);
6038 }
6039 bfd_put_32 (abfd, BLR, p);
6040 return p + 4;
6041}
6042
6043static bfd_byte *
6044savegpr1 (bfd *abfd, bfd_byte *p, int r)
6045{
6046 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6047 return p + 4;
6048}
6049
6050static bfd_byte *
6051savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6052{
6053 p = savegpr1 (abfd, p, r);
6054 bfd_put_32 (abfd, BLR, p);
6055 return p + 4;
6056}
6057
6058static bfd_byte *
6059restgpr1 (bfd *abfd, bfd_byte *p, int r)
6060{
6061 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6062 return p + 4;
6063}
6064
6065static bfd_byte *
6066restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6067{
6068 p = restgpr1 (abfd, p, r);
6069 bfd_put_32 (abfd, BLR, p);
6070 return p + 4;
6071}
6072
6073static bfd_byte *
6074savefpr (bfd *abfd, bfd_byte *p, int r)
6075{
6076 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6077 return p + 4;
6078}
6079
6080static bfd_byte *
6081savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6082{
6083 p = savefpr (abfd, p, r);
a078d95a 6084 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6085 p = p + 4;
6086 bfd_put_32 (abfd, BLR, p);
6087 return p + 4;
6088}
6089
6090static bfd_byte *
6091restfpr (bfd *abfd, bfd_byte *p, int r)
6092{
6093 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6094 return p + 4;
6095}
6096
6097static bfd_byte *
6098restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6099{
a078d95a 6100 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6101 p = p + 4;
6102 p = restfpr (abfd, p, r);
6103 bfd_put_32 (abfd, MTLR_R0, p);
6104 p = p + 4;
6105 if (r == 29)
6106 {
6107 p = restfpr (abfd, p, 30);
6108 p = restfpr (abfd, p, 31);
6109 }
6110 bfd_put_32 (abfd, BLR, p);
6111 return p + 4;
6112}
6113
6114static bfd_byte *
6115savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6116{
6117 p = savefpr (abfd, p, r);
6118 bfd_put_32 (abfd, BLR, p);
6119 return p + 4;
6120}
6121
6122static bfd_byte *
6123restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6124{
6125 p = restfpr (abfd, p, r);
6126 bfd_put_32 (abfd, BLR, p);
6127 return p + 4;
6128}
6129
6130static bfd_byte *
6131savevr (bfd *abfd, bfd_byte *p, int r)
6132{
6133 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6134 p = p + 4;
6135 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6136 return p + 4;
6137}
6138
6139static bfd_byte *
6140savevr_tail (bfd *abfd, bfd_byte *p, int r)
6141{
6142 p = savevr (abfd, p, r);
6143 bfd_put_32 (abfd, BLR, p);
6144 return p + 4;
6145}
6146
6147static bfd_byte *
6148restvr (bfd *abfd, bfd_byte *p, int r)
6149{
6150 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6151 p = p + 4;
6152 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6153 return p + 4;
6154}
6155
6156static bfd_byte *
6157restvr_tail (bfd *abfd, bfd_byte *p, int r)
6158{
6159 p = restvr (abfd, p, r);
6160 bfd_put_32 (abfd, BLR, p);
6161 return p + 4;
6162}
6163
9e7028aa
AM
6164#define STDU_R1_0R1 0xf8210001
6165#define ADDI_R1_R1 0x38210000
6166
6167/* Emit prologue of wrapper preserving regs around a call to
6168 __tls_get_addr_opt. */
6169
6170static bfd_byte *
6171tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6172{
6173 unsigned int i;
6174
6175 bfd_put_32 (obfd, MFLR_R0, p);
6176 p += 4;
6177 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6178 p += 4;
6179
6180 if (htab->opd_abi)
6181 {
6182 for (i = 4; i < 12; i++)
6183 {
6184 bfd_put_32 (obfd,
6185 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6186 p += 4;
6187 }
6188 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6189 p += 4;
6190 }
6191 else
6192 {
6193 for (i = 4; i < 12; i++)
6194 {
6195 bfd_put_32 (obfd,
6196 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6197 p += 4;
6198 }
6199 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6200 p += 4;
6201 }
6202 return p;
6203}
6204
6205/* Emit epilogue of wrapper preserving regs around a call to
6206 __tls_get_addr_opt. */
6207
6208static bfd_byte *
6209tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6210{
6211 unsigned int i;
6212
6213 if (htab->opd_abi)
6214 {
6215 for (i = 4; i < 12; i++)
6216 {
6217 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6218 p += 4;
6219 }
6220 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6221 p += 4;
6222 }
6223 else
6224 {
6225 for (i = 4; i < 12; i++)
6226 {
6227 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6228 p += 4;
6229 }
6230 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6231 p += 4;
6232 }
6233 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6234 p += 4;
6235 bfd_put_32 (obfd, MTLR_R0, p);
6236 p += 4;
6237 bfd_put_32 (obfd, BLR, p);
6238 p += 4;
6239 return p;
6240}
6241
e86ce104
AM
6242/* Called via elf_link_hash_traverse to transfer dynamic linking
6243 information on function code symbol entries to their corresponding
6244 function descriptor symbol entries. */
deb0e272 6245
0a1b45a2 6246static bool
4ce794b7 6247func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6248{
e86ce104 6249 struct bfd_link_info *info;
65f38f15 6250 struct ppc_link_hash_table *htab;
50bc7936
AM
6251 struct ppc_link_hash_entry *fh;
6252 struct ppc_link_hash_entry *fdh;
0a1b45a2 6253 bool force_local;
5bd4f169 6254
ed7007c1 6255 fh = ppc_elf_hash_entry (h);
50bc7936 6256 if (fh->elf.root.type == bfd_link_hash_indirect)
0a1b45a2 6257 return true;
e86ce104 6258
8c5b4e52 6259 if (!fh->is_func)
0a1b45a2 6260 return true;
8c5b4e52
AM
6261
6262 if (fh->elf.root.root.string[0] != '.'
6263 || fh->elf.root.root.string[1] == '\0')
0a1b45a2 6264 return true;
8c5b4e52 6265
4ce794b7 6266 info = inf;
65f38f15 6267 htab = ppc_hash_table (info);
4dfe6ac6 6268 if (htab == NULL)
0a1b45a2 6269 return false;
5bd4f169 6270
8c5b4e52
AM
6271 /* Find the corresponding function descriptor symbol. */
6272 fdh = lookup_fdh (fh, htab);
6273
c09bdfe5
AM
6274 /* Resolve undefined references to dot-symbols as the value
6275 in the function descriptor, if we have one in a regular object.
6276 This is to satisfy cases like ".quad .foo". Calls to functions
6277 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6278 if ((fh->elf.root.type == bfd_link_hash_undefined
6279 || fh->elf.root.type == bfd_link_hash_undefweak)
6280 && (fdh->elf.root.type == bfd_link_hash_defined
6281 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6282 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6283 && opd_entry_value (fdh->elf.root.u.def.section,
6284 fdh->elf.root.u.def.value,
c09bdfe5 6285 &fh->elf.root.u.def.section,
0a1b45a2 6286 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
c09bdfe5 6287 {
b31867b6 6288 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6289 fh->elf.forced_local = 1;
b31867b6
AM
6290 fh->elf.def_regular = fdh->elf.def_regular;
6291 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6292 }
6293
8c5b4e52
AM
6294 if (!fh->elf.dynamic)
6295 {
6296 struct plt_entry *ent;
5bd4f169 6297
8c5b4e52
AM
6298 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6299 if (ent->plt.refcount > 0)
6300 break;
6301 if (ent == NULL)
0a1b45a2 6302 return true;
8c5b4e52 6303 }
5bd4f169 6304
8c5b4e52 6305 /* Create a descriptor as undefined if necessary. */
50bc7936 6306 if (fdh == NULL
0e1862bb 6307 && !bfd_link_executable (info)
50bc7936
AM
6308 && (fh->elf.root.type == bfd_link_hash_undefined
6309 || fh->elf.root.type == bfd_link_hash_undefweak))
6310 {
908b32fc 6311 fdh = make_fdh (info, fh);
bb700d78 6312 if (fdh == NULL)
0a1b45a2 6313 return false;
50bc7936 6314 }
648cca2c 6315
8c5b4e52 6316 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6317 if (fdh != NULL
6318 && fdh->fake
8c5b4e52
AM
6319 && (fh->elf.root.type == bfd_link_hash_defined
6320 || fh->elf.root.type == bfd_link_hash_defweak))
0a1b45a2 6321 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
908b32fc 6322
8c5b4e52
AM
6323 /* Transfer dynamic linking information to the function descriptor. */
6324 if (fdh != NULL)
6325 {
f5385ebf
AM
6326 fdh->elf.ref_regular |= fh->elf.ref_regular;
6327 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6328 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6329 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6330 fdh->elf.dynamic |= fh->elf.dynamic;
6331 fdh->elf.needs_plt |= (fh->elf.needs_plt
6332 || fh->elf.type == STT_FUNC
6333 || fh->elf.type == STT_GNU_IFUNC);
6334 move_plt_plist (fh, fdh);
6335
6336 if (!fdh->elf.forced_local
6337 && fh->elf.dynindx != -1)
6338 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
0a1b45a2 6339 return false;
e86ce104
AM
6340 }
6341
50bc7936
AM
6342 /* Now that the info is on the function descriptor, clear the
6343 function code sym info. Any function code syms for which we
6344 don't have a definition in a regular file, we force local.
6345 This prevents a shared library from exporting syms that have
6346 been imported from another library. Function code syms that
6347 are really in the library we must leave global to prevent the
6348 linker dragging in a definition from a static library. */
93f3fa99
AM
6349 force_local = (!fh->elf.def_regular
6350 || fdh == NULL
6351 || !fdh->elf.def_regular
6352 || fdh->elf.forced_local);
50bc7936
AM
6353 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6354
0a1b45a2 6355 return true;
e86ce104 6356}
40b8271b 6357
a4b6fadd
AM
6358static const struct sfpr_def_parms save_res_funcs[] =
6359 {
6360 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6361 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6362 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6363 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6364 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6365 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6366 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6367 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6368 { "._savef", 14, 31, savefpr, savefpr1_tail },
6369 { "._restf", 14, 31, restfpr, restfpr1_tail },
6370 { "_savevr_", 20, 31, savevr, savevr_tail },
6371 { "_restvr_", 20, 31, restvr, restvr_tail }
6372 };
6373
e86ce104 6374/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6375 this hook to a) run the edit functions in this file, b) provide
6376 some gcc support functions, and c) transfer dynamic linking
6377 information gathered so far on function code symbol entries, to
6378 their corresponding function descriptor symbol entries. */
deb0e272 6379
0a1b45a2 6380static bool
c9405344 6381ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6382{
6383 struct ppc_link_hash_table *htab;
6384
6385 htab = ppc_hash_table (info);
4dfe6ac6 6386 if (htab == NULL)
0a1b45a2 6387 return false;
4dfe6ac6 6388
c9405344
AM
6389 /* Call back into the linker, which then runs the edit functions. */
6390 htab->params->edit ();
6391
b32547cd
AM
6392 /* Provide any missing _save* and _rest* functions. */
6393 if (htab->sfpr != NULL)
6394 {
6395 unsigned int i;
6396
6397 htab->sfpr->size = 0;
6398 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6399 if (!sfpr_define (info, &save_res_funcs[i], NULL))
0a1b45a2 6400 return false;
b32547cd
AM
6401 if (htab->sfpr->size == 0)
6402 htab->sfpr->flags |= SEC_EXCLUDE;
6403 }
6404
6405 if (bfd_link_relocatable (info))
0a1b45a2 6406 return true;
b32547cd
AM
6407
6408 if (htab->elf.hgot != NULL)
dba6fa9b 6409 {
0a1b45a2 6410 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
dba6fa9b
AM
6411 /* Make .TOC. defined so as to prevent it being made dynamic.
6412 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6413 if (!htab->elf.hgot->def_regular
6414 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6415 {
6416 htab->elf.hgot->root.type = bfd_link_hash_defined;
6417 htab->elf.hgot->root.u.def.value = 0;
6418 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6419 htab->elf.hgot->def_regular = 1;
6420 htab->elf.hgot->root.linker_def = 1;
6421 }
dba6fa9b 6422 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6423 htab->elf.hgot->other
6424 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6425 }
c66bb0ee 6426
8c5b4e52
AM
6427 if (htab->need_func_desc_adj)
6428 {
6429 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6430 htab->need_func_desc_adj = 0;
6431 }
805fc799 6432
0a1b45a2 6433 return true;
e86ce104
AM
6434}
6435
d311bc8b 6436/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6437 aliases, that apply to read-only sections. Cannot be used after
6438 size_dynamic_sections. */
d311bc8b 6439
0a1b45a2 6440static bool
d311bc8b
AM
6441alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6442{
ed7007c1 6443 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6444 do
6445 {
5dbc8b37 6446 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
0a1b45a2 6447 return true;
ed7007c1 6448 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6449 }
6450 while (eh != NULL && &eh->elf != h);
d311bc8b 6451
0a1b45a2 6452 return false;
d311bc8b 6453}
8a2058b5 6454
8a9e8e72
AM
6455/* Return whether EH has pc-relative dynamic relocs. */
6456
0a1b45a2 6457static bool
8a9e8e72
AM
6458pc_dynrelocs (struct ppc_link_hash_entry *eh)
6459{
6460 struct elf_dyn_relocs *p;
6461
190eb1dd 6462 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72 6463 if (p->pc_count != 0)
0a1b45a2
AM
6464 return true;
6465 return false;
8a9e8e72
AM
6466}
6467
8a2058b5
AM
6468/* Return true if a global entry stub will be created for H. Valid
6469 for ELFv2 before plt entries have been allocated. */
6470
0a1b45a2 6471static bool
8a2058b5
AM
6472global_entry_stub (struct elf_link_hash_entry *h)
6473{
6474 struct plt_entry *pent;
6475
6476 if (!h->pointer_equality_needed
6477 || h->def_regular)
0a1b45a2 6478 return false;
8a2058b5
AM
6479
6480 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6481 if (pent->plt.refcount > 0
6482 && pent->addend == 0)
0a1b45a2 6483 return true;
8a2058b5 6484
0a1b45a2 6485 return false;
8a2058b5
AM
6486}
6487
e86ce104
AM
6488/* Adjust a symbol defined by a dynamic object and referenced by a
6489 regular object. The current definition is in some section of the
6490 dynamic object, but we're not including those sections. We have to
6491 change the definition to something the rest of the link can
6492 understand. */
6493
0a1b45a2 6494static bool
4ce794b7
AM
6495ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6496 struct elf_link_hash_entry *h)
e86ce104
AM
6497{
6498 struct ppc_link_hash_table *htab;
5474d94f 6499 asection *s, *srel;
e86ce104
AM
6500
6501 htab = ppc_hash_table (info);
4dfe6ac6 6502 if (htab == NULL)
0a1b45a2 6503 return false;
e86ce104
AM
6504
6505 /* Deal with function syms. */
6506 if (h->type == STT_FUNC
e054468f 6507 || h->type == STT_GNU_IFUNC
f5385ebf 6508 || h->needs_plt)
e86ce104 6509 {
0a1b45a2
AM
6510 bool local = (ppc_elf_hash_entry (h)->save_res
6511 || SYMBOL_CALLS_LOCAL (info, h)
6512 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
529fe20e
AM
6513 /* Discard dyn_relocs when non-pic if we've decided that a
6514 function symbol is local and not an ifunc. We keep dynamic
6515 relocs for ifuncs when local rather than always emitting a
6516 plt call stub for them and defining the symbol on the call
6517 stub. We can't do that for ELFv1 anyway (a function symbol
6518 is defined on a descriptor, not code) and it can be faster at
6519 run-time due to not needing to bounce through a stub. The
6520 dyn_relocs for ifuncs will be applied even in a static
6521 executable. */
6522 if (!bfd_link_pic (info)
6523 && h->type != STT_GNU_IFUNC
6524 && local)
190eb1dd 6525 h->dyn_relocs = NULL;
529fe20e 6526
e86ce104
AM
6527 /* Clear procedure linkage table information for any symbol that
6528 won't need a .plt entry. */
411e1bfb
AM
6529 struct plt_entry *ent;
6530 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6531 if (ent->plt.refcount > 0)
6532 break;
8387904d 6533 if (ent == NULL
2d7ad24e
AM
6534 || (h->type != STT_GNU_IFUNC
6535 && local
3e04d765 6536 && (htab->can_convert_all_inline_plt
ed7007c1 6537 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6538 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6539 {
411e1bfb 6540 h->plt.plist = NULL;
f5385ebf 6541 h->needs_plt = 0;
d1eca1e4 6542 h->pointer_equality_needed = 0;
40b8271b 6543 }
8a2058b5 6544 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6545 {
d1eca1e4
AM
6546 /* Taking a function's address in a read/write section
6547 doesn't require us to define the function symbol in the
6548 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6549 be used instead. The reason we prefer a few more dynamic
6550 relocs is that calling via a global entry stub costs a
6551 few more instructions, and pointer_equality_needed causes
6552 extra work in ld.so when resolving these symbols. */
529fe20e 6553 if (global_entry_stub (h))
d1eca1e4 6554 {
5dbc8b37 6555 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6556 {
6557 h->pointer_equality_needed = 0;
04383fd1
AM
6558 /* If we haven't seen a branch reloc and the symbol
6559 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6560 if (!h->needs_plt)
6561 h->plt.plist = NULL;
6562 }
6563 else if (!bfd_link_pic (info))
6564 /* We are going to be defining the function symbol on the
6565 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6566 h->dyn_relocs = NULL;
d1eca1e4
AM
6567 }
6568
3988aed5 6569 /* ELFv2 function symbols can't have copy relocs. */
0a1b45a2 6570 return true;
3988aed5
AM
6571 }
6572 else if (!h->needs_plt
5dbc8b37 6573 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6574 {
04383fd1
AM
6575 /* If we haven't seen a branch reloc and the symbol isn't an
6576 ifunc then we don't need a plt entry. */
3988aed5
AM
6577 h->plt.plist = NULL;
6578 h->pointer_equality_needed = 0;
0a1b45a2 6579 return true;
a345bc8d 6580 }
5bd4f169 6581 }
bbd7ec4a 6582 else
411e1bfb 6583 h->plt.plist = NULL;
5bd4f169
AM
6584
6585 /* If this is a weak symbol, and there is a real definition, the
6586 processor independent code will have arranged for us to see the
6587 real definition first, and we can just use the same value. */
60d67dc8 6588 if (h->is_weakalias)
5bd4f169 6589 {
60d67dc8
AM
6590 struct elf_link_hash_entry *def = weakdef (h);
6591 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6592 h->root.u.def.section = def->root.u.def.section;
6593 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6594 if (def->root.u.def.section == htab->elf.sdynbss
6595 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6596 h->dyn_relocs = NULL;
0a1b45a2 6597 return true;
5bd4f169
AM
6598 }
6599
5bd4f169
AM
6600 /* If we are creating a shared library, we must presume that the
6601 only references to the symbol are via the global offset table.
6602 For such cases we need not do anything here; the relocations will
6603 be handled correctly by relocate_section. */
ec73ddcd 6604 if (!bfd_link_executable (info))
0a1b45a2 6605 return true;
5bd4f169 6606
65f38f15
AM
6607 /* If there are no references to this symbol that do not use the
6608 GOT, we don't need to generate a copy reloc. */
f5385ebf 6609 if (!h->non_got_ref)
0a1b45a2 6610 return true;
65f38f15 6611
b186458a 6612 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6613 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6614
d93d1c80
AM
6615 /* If -z nocopyreloc was given, don't generate them either. */
6616 || info->nocopyreloc
a127494f 6617
dce2246a 6618 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6619 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6620 || (ELIMINATE_COPY_RELOCS
6621 && !h->needs_copy
6622 && !alias_readonly_dynrelocs (h))
65f38f15 6623
d93d1c80
AM
6624 /* Protected variables do not work with .dynbss. The copy in
6625 .dynbss won't be used by the shared library with the protected
6626 definition for the variable. Text relocations are preferable
6627 to an incorrect program. */
6628 || h->protected_def)
0a1b45a2 6629 return true;
a127494f 6630
e1c6cf61
AM
6631 if (h->type == STT_FUNC
6632 || h->type == STT_GNU_IFUNC)
97b639ba 6633 {
e1c6cf61
AM
6634 /* .dynbss copies of function symbols only work if we have
6635 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6636 use dot-symbols and set the function symbol size to the text
6637 size of the function rather than the size of the descriptor.
6638 That's wrong for copying a descriptor. */
ed7007c1 6639 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61 6640 || !(h->size == 24 || h->size == 16))
0a1b45a2 6641 return true;
e1c6cf61
AM
6642
6643 /* We should never get here, but unfortunately there are old
6644 versions of gcc (circa gcc-3.2) that improperly for the
6645 ELFv1 ABI put initialized function pointers, vtable refs and
6646 suchlike in read-only sections. Allow them to proceed, but
6647 warn that this might break at runtime. */
25f53a85 6648 info->callbacks->einfo
c1c8c1ef 6649 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6650 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6651 h->root.root.string);
6652 }
5d35169e
AM
6653
6654 /* This is a reference to a symbol defined by a dynamic object which
6655 is not a function. */
6656
5bd4f169
AM
6657 /* We must allocate the symbol in our .dynbss section, which will
6658 become part of the .bss section of the executable. There will be
6659 an entry for this symbol in the .dynsym section. The dynamic
6660 object will contain position independent code, so all references
6661 from the dynamic object to this symbol will go through the global
6662 offset table. The dynamic linker will use the .dynsym entry to
6663 determine the address it must put in the global offset table, so
6664 both the dynamic object and the regular object will refer to the
6665 same memory location for the variable. */
5474d94f
AM
6666 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6667 {
6668 s = htab->elf.sdynrelro;
6669 srel = htab->elf.sreldynrelro;
6670 }
6671 else
6672 {
6673 s = htab->elf.sdynbss;
6674 srel = htab->elf.srelbss;
6675 }
1d7e9d18 6676 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6677 {
4a7e5234
AM
6678 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6679 linker to copy the initial value out of the dynamic object
6680 and into the runtime process image. */
5474d94f 6681 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6682 h->needs_copy = 1;
5bd4f169
AM
6683 }
6684
529fe20e 6685 /* We no longer want dyn_relocs. */
190eb1dd 6686 h->dyn_relocs = NULL;
6cabe1ea 6687 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6688}
6689
e86ce104
AM
6690/* If given a function descriptor symbol, hide both the function code
6691 sym and the descriptor. */
6692static void
4ce794b7
AM
6693ppc64_elf_hide_symbol (struct bfd_link_info *info,
6694 struct elf_link_hash_entry *h,
0a1b45a2 6695 bool force_local)
e86ce104 6696{
34814b9f 6697 struct ppc_link_hash_entry *eh;
e86ce104
AM
6698 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6699
87469ba2
AM
6700 if (ppc_hash_table (info) == NULL)
6701 return;
6702
ed7007c1 6703 eh = ppc_elf_hash_entry (h);
34814b9f 6704 if (eh->is_func_descriptor)
e86ce104 6705 {
34814b9f 6706 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6707
721956f4 6708 if (fh == NULL)
d1329ca3
AM
6709 {
6710 const char *p, *q;
b8ac2841 6711 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6712 char save;
6713
6714 /* We aren't supposed to use alloca in BFD because on
6715 systems which do not have alloca the version in libiberty
6716 calls xmalloc, which might cause the program to crash
6717 when it runs out of memory. This function doesn't have a
6718 return status, so there's no way to gracefully return an
6719 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6720 accessed; It's either a string in an ELF string table,
6721 or allocated in an objalloc structure. */
d1329ca3 6722
34814b9f 6723 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6724 save = *p;
6725 *(char *) p = '.';
0a1b45a2
AM
6726 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6727 false, false));
d1329ca3
AM
6728 *(char *) p = save;
6729
6730 /* Unfortunately, if it so happens that the string we were
6731 looking for was allocated immediately before this string,
6732 then we overwrote the string terminator. That's the only
6733 reason the lookup should fail. */
6734 if (fh == NULL)
6735 {
34814b9f
AM
6736 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6737 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6738 --q, --p;
34814b9f 6739 if (q < eh->elf.root.root.string && *p == '.')
0a1b45a2
AM
6740 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6741 false, false));
d1329ca3
AM
6742 }
6743 if (fh != NULL)
6744 {
34814b9f
AM
6745 eh->oh = fh;
6746 fh->oh = eh;
d1329ca3
AM
6747 }
6748 }
e86ce104 6749 if (fh != NULL)
34814b9f 6750 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6751 }
6752}
6753
0a1b45a2 6754static bool
8843416a
AM
6755get_sym_h (struct elf_link_hash_entry **hp,
6756 Elf_Internal_Sym **symp,
6757 asection **symsecp,
f961d9dd 6758 unsigned char **tls_maskp,
8843416a
AM
6759 Elf_Internal_Sym **locsymsp,
6760 unsigned long r_symndx,
6761 bfd *ibfd)
411e1bfb 6762{
0ffa91dd 6763 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6764
6765 if (r_symndx >= symtab_hdr->sh_info)
6766 {
6767 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6768 struct elf_link_hash_entry *h;
6769
6770 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6771 h = elf_follow_link (h);
411e1bfb
AM
6772
6773 if (hp != NULL)
6774 *hp = h;
6775
6776 if (symp != NULL)
6777 *symp = NULL;
6778
6779 if (symsecp != NULL)
6780 {
6781 asection *symsec = NULL;
6782 if (h->root.type == bfd_link_hash_defined
6783 || h->root.type == bfd_link_hash_defweak)
6784 symsec = h->root.u.def.section;
6785 *symsecp = symsec;
6786 }
6787
e7b938ca 6788 if (tls_maskp != NULL)
ed7007c1 6789 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6790 }
6791 else
6792 {
6793 Elf_Internal_Sym *sym;
6794 Elf_Internal_Sym *locsyms = *locsymsp;
6795
6796 if (locsyms == NULL)
6797 {
6798 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6799 if (locsyms == NULL)
6800 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6801 symtab_hdr->sh_info,
6802 0, NULL, NULL, NULL);
6803 if (locsyms == NULL)
0a1b45a2 6804 return false;
411e1bfb
AM
6805 *locsymsp = locsyms;
6806 }
6807 sym = locsyms + r_symndx;
6808
6809 if (hp != NULL)
6810 *hp = NULL;
6811
6812 if (symp != NULL)
6813 *symp = sym;
6814
6815 if (symsecp != NULL)
cb33740c 6816 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6817
e7b938ca 6818 if (tls_maskp != NULL)
411e1bfb
AM
6819 {
6820 struct got_entry **lgot_ents;
f961d9dd 6821 unsigned char *tls_mask;
411e1bfb 6822
e7b938ca 6823 tls_mask = NULL;
411e1bfb
AM
6824 lgot_ents = elf_local_got_ents (ibfd);
6825 if (lgot_ents != NULL)
6826 {
e054468f
AM
6827 struct plt_entry **local_plt = (struct plt_entry **)
6828 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6829 unsigned char *lgot_masks = (unsigned char *)
e054468f 6830 (local_plt + symtab_hdr->sh_info);
e7b938ca 6831 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6832 }
e7b938ca 6833 *tls_maskp = tls_mask;
411e1bfb
AM
6834 }
6835 }
0a1b45a2 6836 return true;
411e1bfb
AM
6837}
6838
e7b938ca 6839/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6840 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6841 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6842
6843static int
f961d9dd 6844get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6845 unsigned long *toc_symndx,
6846 bfd_vma *toc_addend,
0d4792f7 6847 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6848 const Elf_Internal_Rela *rel,
6849 bfd *ibfd)
411e1bfb
AM
6850{
6851 unsigned long r_symndx;
0d4792f7 6852 int next_r;
411e1bfb
AM
6853 struct elf_link_hash_entry *h;
6854 Elf_Internal_Sym *sym;
6855 asection *sec;
6856 bfd_vma off;
6857
6858 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6859 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6860 return 0;
411e1bfb 6861
37da22e5
AM
6862 if ((*tls_maskp != NULL
6863 && (**tls_maskp & TLS_TLS) != 0
6864 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6865 || sec == NULL
6bee8834 6866 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6867 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6868 return 1;
411e1bfb
AM
6869
6870 /* Look inside a TOC section too. */
6871 if (h != NULL)
6872 {
6873 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6874 off = h->root.u.def.value;
6875 }
6876 else
6877 off = sym->st_value;
6878 off += rel->r_addend;
6879 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6880 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6881 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6882 if (toc_symndx != NULL)
6883 *toc_symndx = r_symndx;
3a71aa26
AM
6884 if (toc_addend != NULL)
6885 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6886 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6887 return 0;
854b41e7 6888 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6889 && (next_r == -1 || next_r == -2))
6890 return 1 - next_r;
951fd09b 6891 return 1;
411e1bfb
AM
6892}
6893
3b421ab3
AM
6894/* Find (or create) an entry in the tocsave hash table. */
6895
6896static struct tocsave_entry *
6897tocsave_find (struct ppc_link_hash_table *htab,
6898 enum insert_option insert,
6899 Elf_Internal_Sym **local_syms,
6900 const Elf_Internal_Rela *irela,
6901 bfd *ibfd)
6902{
6903 unsigned long r_indx;
6904 struct elf_link_hash_entry *h;
6905 Elf_Internal_Sym *sym;
6906 struct tocsave_entry ent, *p;
6907 hashval_t hash;
6908 struct tocsave_entry **slot;
6909
6910 r_indx = ELF64_R_SYM (irela->r_info);
6911 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6912 return NULL;
6913 if (ent.sec == NULL || ent.sec->output_section == NULL)
6914 {
4eca0228 6915 _bfd_error_handler
871b3ab2 6916 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6917 return NULL;
6918 }
6919
6920 if (h != NULL)
6921 ent.offset = h->root.u.def.value;
6922 else
6923 ent.offset = sym->st_value;
6924 ent.offset += irela->r_addend;
6925
6926 hash = tocsave_htab_hash (&ent);
6927 slot = ((struct tocsave_entry **)
6928 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6929 if (slot == NULL)
6930 return NULL;
6931
6932 if (*slot == NULL)
6933 {
6934 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6935 if (p == NULL)
6936 return NULL;
6937 *p = ent;
6938 *slot = p;
6939 }
6940 return *slot;
6941}
6942
754021d0 6943/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6944 code for the old ABI, these will already have been done. */
754021d0 6945
0a1b45a2 6946static bool
754021d0
AM
6947adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6948{
6949 struct ppc_link_hash_entry *eh;
6950 asection *sym_sec;
74f0fb50 6951 struct _opd_sec_data *opd;
754021d0
AM
6952
6953 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 6954 return true;
754021d0 6955
754021d0
AM
6956 if (h->root.type != bfd_link_hash_defined
6957 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 6958 return true;
754021d0 6959
ed7007c1 6960 eh = ppc_elf_hash_entry (h);
754021d0 6961 if (eh->adjust_done)
0a1b45a2 6962 return true;
754021d0
AM
6963
6964 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6965 opd = get_opd_info (sym_sec);
6966 if (opd != NULL && opd->adjust != NULL)
754021d0 6967 {
51aecdc5 6968 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6969 if (adjust == -1)
6970 {
6971 /* This entry has been deleted. */
b3fac117 6972 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6973 if (dsec == NULL)
6974 {
6975 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6976 if (discarded_section (dsec))
81688140 6977 {
b3fac117 6978 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6979 break;
6980 }
6981 }
4025353c 6982 eh->elf.root.u.def.value = 0;
81688140 6983 eh->elf.root.u.def.section = dsec;
4025353c
AM
6984 }
6985 else
6986 eh->elf.root.u.def.value += adjust;
754021d0
AM
6987 eh->adjust_done = 1;
6988 }
0a1b45a2 6989 return true;
754021d0
AM
6990}
6991
8c1d1bb8 6992/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6993 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6994 have already been determined. */
6995
0a1b45a2 6996static bool
8c1d1bb8
AM
6997dec_dynrel_count (bfd_vma r_info,
6998 asection *sec,
6999 struct bfd_link_info *info,
7000 Elf_Internal_Sym **local_syms,
7001 struct elf_link_hash_entry *h,
19e08130 7002 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7003{
7004 enum elf_ppc64_reloc_type r_type;
19e08130 7005 asection *sym_sec = NULL;
8c1d1bb8
AM
7006
7007 /* Can this reloc be dynamic? This switch, and later tests here
7008 should be kept in sync with the code in check_relocs. */
7009 r_type = ELF64_R_TYPE (r_info);
7010 switch (r_type)
7011 {
7012 default:
0a1b45a2 7013 return true;
8c1d1bb8 7014
1bdd8fac
AM
7015 case R_PPC64_TOC16:
7016 case R_PPC64_TOC16_DS:
7017 case R_PPC64_TOC16_LO:
7018 case R_PPC64_TOC16_HI:
7019 case R_PPC64_TOC16_HA:
7020 case R_PPC64_TOC16_LO_DS:
7021 if (h == NULL)
0a1b45a2 7022 return true;
1bdd8fac
AM
7023 break;
7024
8c1d1bb8
AM
7025 case R_PPC64_TPREL16:
7026 case R_PPC64_TPREL16_LO:
7027 case R_PPC64_TPREL16_HI:
7028 case R_PPC64_TPREL16_HA:
7029 case R_PPC64_TPREL16_DS:
7030 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7031 case R_PPC64_TPREL16_HIGH:
7032 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7033 case R_PPC64_TPREL16_HIGHER:
7034 case R_PPC64_TPREL16_HIGHERA:
7035 case R_PPC64_TPREL16_HIGHEST:
7036 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7037 case R_PPC64_TPREL64:
c213164a 7038 case R_PPC64_TPREL34:
8c1d1bb8
AM
7039 case R_PPC64_DTPMOD64:
7040 case R_PPC64_DTPREL64:
7041 case R_PPC64_ADDR64:
7042 case R_PPC64_REL30:
7043 case R_PPC64_REL32:
7044 case R_PPC64_REL64:
7045 case R_PPC64_ADDR14:
7046 case R_PPC64_ADDR14_BRNTAKEN:
7047 case R_PPC64_ADDR14_BRTAKEN:
7048 case R_PPC64_ADDR16:
7049 case R_PPC64_ADDR16_DS:
7050 case R_PPC64_ADDR16_HA:
7051 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7052 case R_PPC64_ADDR16_HIGH:
7053 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7054 case R_PPC64_ADDR16_HIGHER:
7055 case R_PPC64_ADDR16_HIGHERA:
7056 case R_PPC64_ADDR16_HIGHEST:
7057 case R_PPC64_ADDR16_HIGHESTA:
7058 case R_PPC64_ADDR16_LO:
7059 case R_PPC64_ADDR16_LO_DS:
7060 case R_PPC64_ADDR24:
7061 case R_PPC64_ADDR32:
7062 case R_PPC64_UADDR16:
7063 case R_PPC64_UADDR32:
7064 case R_PPC64_UADDR64:
7065 case R_PPC64_TOC:
5663e321
AM
7066 case R_PPC64_D34:
7067 case R_PPC64_D34_LO:
7068 case R_PPC64_D34_HI30:
7069 case R_PPC64_D34_HA30:
7070 case R_PPC64_ADDR16_HIGHER34:
7071 case R_PPC64_ADDR16_HIGHERA34:
7072 case R_PPC64_ADDR16_HIGHEST34:
7073 case R_PPC64_ADDR16_HIGHESTA34:
7074 case R_PPC64_D28:
8c1d1bb8
AM
7075 break;
7076 }
7077
7078 if (local_syms != NULL)
7079 {
7080 unsigned long r_symndx;
8c1d1bb8
AM
7081 bfd *ibfd = sec->owner;
7082
7083 r_symndx = ELF64_R_SYM (r_info);
7084 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
0a1b45a2 7085 return false;
8c1d1bb8
AM
7086 }
7087
ec73ddcd
AM
7088 if ((h != NULL
7089 && (h->root.type == bfd_link_hash_defweak
7090 || !h->def_regular))
7091 || (h != NULL
7092 && !bfd_link_executable (info)
7093 && !SYMBOLIC_BIND (info, h))
7094 || (bfd_link_pic (info)
7095 && must_be_dyn_reloc (info, r_type))
7096 || (!bfd_link_pic (info)
7097 && (h != NULL
7098 ? h->type == STT_GNU_IFUNC
7099 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7100 ;
7101 else
0a1b45a2 7102 return true;
8c1d1bb8
AM
7103
7104 if (h != NULL)
6edfbbad 7105 {
19e08130
AM
7106 struct elf_dyn_relocs *p;
7107 struct elf_dyn_relocs **pp;
190eb1dd 7108 pp = &h->dyn_relocs;
19e08130
AM
7109
7110 /* elf_gc_sweep may have already removed all dyn relocs associated
7111 with local syms for a given section. Also, symbol flags are
7112 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7113 report a dynreloc miscount. */
7114 if (*pp == NULL && info->gc_sections)
0a1b45a2 7115 return true;
19e08130
AM
7116
7117 while ((p = *pp) != NULL)
60124e18 7118 {
19e08130
AM
7119 if (p->sec == sec)
7120 {
7121 if (!must_be_dyn_reloc (info, r_type))
7122 p->pc_count -= 1;
7123 p->count -= 1;
7124 if (p->count == 0)
7125 *pp = p->next;
0a1b45a2 7126 return true;
19e08130
AM
7127 }
7128 pp = &p->next;
60124e18 7129 }
6edfbbad 7130 }
19e08130
AM
7131 else
7132 {
7133 struct ppc_dyn_relocs *p;
7134 struct ppc_dyn_relocs **pp;
7135 void *vpp;
0a1b45a2 7136 bool is_ifunc;
8c1d1bb8 7137
19e08130
AM
7138 if (local_syms == NULL)
7139 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7140 if (sym_sec == NULL)
7141 sym_sec = sec;
c57da1a7 7142
19e08130
AM
7143 vpp = &elf_section_data (sym_sec)->local_dynrel;
7144 pp = (struct ppc_dyn_relocs **) vpp;
7145
7146 if (*pp == NULL && info->gc_sections)
0a1b45a2 7147 return true;
19e08130
AM
7148
7149 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7150 while ((p = *pp) != NULL)
8c1d1bb8 7151 {
19e08130
AM
7152 if (p->sec == sec && p->ifunc == is_ifunc)
7153 {
7154 p->count -= 1;
7155 if (p->count == 0)
7156 *pp = p->next;
0a1b45a2 7157 return true;
19e08130
AM
7158 }
7159 pp = &p->next;
8c1d1bb8 7160 }
8c1d1bb8
AM
7161 }
7162
695344c0 7163 /* xgettext:c-format */
cf97bcb0
AM
7164 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7165 sec->owner, sec);
8c1d1bb8 7166 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7167 return false;
8c1d1bb8
AM
7168}
7169
754021d0
AM
7170/* Remove unused Official Procedure Descriptor entries. Currently we
7171 only remove those associated with functions in discarded link-once
7172 sections, or weakly defined functions that have been overridden. It
7173 would be possible to remove many more entries for statically linked
7174 applications. */
7175
0a1b45a2 7176bool
e7d1c40c 7177ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7178{
7179 bfd *ibfd;
0a1b45a2 7180 bool some_edited = false;
3f764659 7181 asection *need_pad = NULL;
e7d1c40c
AM
7182 struct ppc_link_hash_table *htab;
7183
7184 htab = ppc_hash_table (info);
7185 if (htab == NULL)
0a1b45a2 7186 return false;
1e2f5b6e 7187
c72f2fb2 7188 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7189 {
7190 asection *sec;
7191 Elf_Internal_Rela *relstart, *rel, *relend;
7192 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7193 Elf_Internal_Sym *local_syms;
74f0fb50 7194 struct _opd_sec_data *opd;
0a1b45a2 7195 bool need_edit, add_aux_fields, broken;
3f764659 7196 bfd_size_type cnt_16b = 0;
1e2f5b6e 7197
854b41e7
AM
7198 if (!is_ppc64_elf (ibfd))
7199 continue;
7200
1e2f5b6e 7201 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7202 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7203 continue;
7204
dbaa2011 7205 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7206 continue;
7207
1e2f5b6e
AM
7208 if (sec->output_section == bfd_abs_section_ptr)
7209 continue;
7210
7211 /* Look through the section relocs. */
7212 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7213 continue;
7214
6cdc0ccc 7215 local_syms = NULL;
0ffa91dd 7216 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7217
7218 /* Read the relocations. */
4ce794b7 7219 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7220 info->keep_memory);
1e2f5b6e 7221 if (relstart == NULL)
0a1b45a2 7222 return false;
1e2f5b6e
AM
7223
7224 /* First run through the relocs to check they are sane, and to
7225 determine whether we need to edit this opd section. */
0a1b45a2
AM
7226 need_edit = false;
7227 broken = false;
3f764659 7228 need_pad = sec;
1e2f5b6e 7229 relend = relstart + sec->reloc_count;
50bc7936 7230 for (rel = relstart; rel < relend; )
1e2f5b6e 7231 {
04c9666a 7232 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7233 unsigned long r_symndx;
7234 asection *sym_sec;
7235 struct elf_link_hash_entry *h;
7236 Elf_Internal_Sym *sym;
51aecdc5 7237 bfd_vma offset;
1e2f5b6e 7238
51aecdc5 7239 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7240 only interested in the reloc pointing to a function entry
7241 point. */
51aecdc5
AM
7242 offset = rel->r_offset;
7243 if (rel + 1 == relend
7244 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7245 {
7246 /* If someone messes with .opd alignment then after a
7247 "ld -r" we might have padding in the middle of .opd.
7248 Also, there's nothing to prevent someone putting
7249 something silly in .opd with the assembler. No .opd
b34976b6 7250 optimization for them! */
3f764659 7251 broken_opd:
4eca0228 7252 _bfd_error_handler
871b3ab2 7253 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
0a1b45a2 7254 broken = true;
1e2f5b6e
AM
7255 break;
7256 }
7257
50bc7936
AM
7258 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7259 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7260 {
4eca0228 7261 _bfd_error_handler
695344c0 7262 /* xgettext:c-format */
871b3ab2 7263 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7264 ibfd, r_type);
0a1b45a2 7265 broken = true;
50bc7936
AM
7266 break;
7267 }
7268
1e2f5b6e 7269 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7270 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7271 r_symndx, ibfd))
50bc7936 7272 goto error_ret;
1e2f5b6e
AM
7273
7274 if (sym_sec == NULL || sym_sec->owner == NULL)
7275 {
411e1bfb
AM
7276 const char *sym_name;
7277 if (h != NULL)
7278 sym_name = h->root.root.string;
7279 else
26c61ae5
L
7280 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7281 sym_sec);
411e1bfb 7282
4eca0228 7283 _bfd_error_handler
695344c0 7284 /* xgettext:c-format */
871b3ab2 7285 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7286 ibfd, sym_name);
0a1b45a2 7287 broken = true;
1e2f5b6e
AM
7288 break;
7289 }
7290
51020317
AM
7291 /* opd entries are always for functions defined in the
7292 current input bfd. If the symbol isn't defined in the
7293 input bfd, then we won't be using the function in this
7294 bfd; It must be defined in a linkonce section in another
7295 bfd, or is weak. It's also possible that we are
7296 discarding the function due to a linker script /DISCARD/,
7297 which we test for via the output_section. */
7298 if (sym_sec->owner != ibfd
7299 || sym_sec->output_section == bfd_abs_section_ptr)
0a1b45a2 7300 need_edit = true;
1e2f5b6e 7301
50bc7936 7302 rel += 2;
51aecdc5
AM
7303 if (rel + 1 == relend
7304 || (rel + 2 < relend
7305 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7306 ++rel;
7307
7308 if (rel == relend)
3f764659
JJ
7309 {
7310 if (sec->size == offset + 24)
7311 {
7312 need_pad = NULL;
7313 break;
7314 }
51aecdc5 7315 if (sec->size == offset + 16)
3f764659
JJ
7316 {
7317 cnt_16b++;
7318 break;
7319 }
7320 goto broken_opd;
7321 }
3f764659
JJ
7322 else if (rel + 1 < relend
7323 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7324 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7325 {
51aecdc5
AM
7326 if (rel[0].r_offset == offset + 16)
7327 cnt_16b++;
7328 else if (rel[0].r_offset != offset + 24)
7329 goto broken_opd;
3f764659
JJ
7330 }
7331 else
7332 goto broken_opd;
1e2f5b6e
AM
7333 }
7334
e7d1c40c 7335 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7336
51aecdc5 7337 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7338 {
7339 Elf_Internal_Rela *write_rel;
d4730f92 7340 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7341 bfd_byte *rptr, *wptr;
983bddc8 7342 bfd_byte *new_contents;
74f0fb50
AM
7343 bfd_size_type amt;
7344
983bddc8 7345 new_contents = NULL;
51aecdc5 7346 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7347 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7348 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50 7349 if (opd->adjust == NULL)
0a1b45a2 7350 return false;
1e2f5b6e
AM
7351
7352 /* This seems a waste of time as input .opd sections are all
7353 zeros as generated by gcc, but I suppose there's no reason
7354 this will always be so. We might start putting something in
7355 the third word of .opd entries. */
7356 if ((sec->flags & SEC_IN_MEMORY) == 0)
7357 {
eea6121a
AM
7358 bfd_byte *loc;
7359 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7360 {
c9594989 7361 free (loc);
50bc7936 7362 error_ret:
c9594989 7363 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7364 free (local_syms);
6cdc0ccc
AM
7365 if (elf_section_data (sec)->relocs != relstart)
7366 free (relstart);
0a1b45a2 7367 return false;
6cdc0ccc 7368 }
1e2f5b6e
AM
7369 sec->contents = loc;
7370 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7371 }
7372
7373 elf_section_data (sec)->relocs = relstart;
7374
3f764659 7375 new_contents = sec->contents;
3f764659
JJ
7376 if (add_aux_fields)
7377 {
7378 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7379 if (new_contents == NULL)
0a1b45a2 7380 return false;
51aecdc5 7381 need_pad = NULL;
3f764659 7382 }
b4f4e59f
AM
7383 wptr = new_contents;
7384 rptr = sec->contents;
1e2f5b6e 7385 write_rel = relstart;
51aecdc5 7386 for (rel = relstart; rel < relend; )
1e2f5b6e 7387 {
50bc7936
AM
7388 unsigned long r_symndx;
7389 asection *sym_sec;
7390 struct elf_link_hash_entry *h;
51aecdc5 7391 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7392 Elf_Internal_Sym *sym;
51aecdc5
AM
7393 long opd_ent_size;
7394 Elf_Internal_Rela *next_rel;
0a1b45a2 7395 bool skip;
50bc7936
AM
7396
7397 r_symndx = ELF64_R_SYM (rel->r_info);
7398 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7399 r_symndx, ibfd))
50bc7936
AM
7400 goto error_ret;
7401
51aecdc5
AM
7402 next_rel = rel + 2;
7403 if (next_rel + 1 == relend
7404 || (next_rel + 2 < relend
7405 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7406 ++next_rel;
7407
7408 /* See if the .opd entry is full 24 byte or
7409 16 byte (with fd_aux entry overlapped with next
7410 fd_func). */
7411 opd_ent_size = 24;
7412 if (next_rel == relend)
1e2f5b6e 7413 {
51aecdc5 7414 if (sec->size == rel->r_offset + 16)
3f764659 7415 opd_ent_size = 16;
51aecdc5
AM
7416 }
7417 else if (next_rel->r_offset == rel->r_offset + 16)
7418 opd_ent_size = 16;
3f764659 7419
51aecdc5
AM
7420 if (h != NULL
7421 && h->root.root.string[0] == '.')
7422 {
ed7007c1 7423 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7424 if (fdh != NULL)
7425 {
7426 fdh = ppc_follow_link (fdh);
7427 if (fdh->elf.root.type != bfd_link_hash_defined
7428 && fdh->elf.root.type != bfd_link_hash_defweak)
7429 fdh = NULL;
7430 }
51aecdc5 7431 }
1e2f5b6e 7432
51aecdc5
AM
7433 skip = (sym_sec->owner != ibfd
7434 || sym_sec->output_section == bfd_abs_section_ptr);
7435 if (skip)
7436 {
7437 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7438 {
51aecdc5
AM
7439 /* Arrange for the function descriptor sym
7440 to be dropped. */
7441 fdh->elf.root.u.def.value = 0;
7442 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7443 }
51aecdc5 7444 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7445
0e1862bb 7446 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7447 rel = next_rel;
7448 else
7449 while (1)
7450 {
7451 if (!dec_dynrel_count (rel->r_info, sec, info,
7452 NULL, h, sym))
7453 goto error_ret;
754021d0 7454
51aecdc5
AM
7455 if (++rel == next_rel)
7456 break;
1e2f5b6e 7457
51aecdc5
AM
7458 r_symndx = ELF64_R_SYM (rel->r_info);
7459 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7460 r_symndx, ibfd))
7461 goto error_ret;
7462 }
50bc7936
AM
7463 }
7464 else
1e2f5b6e 7465 {
51aecdc5
AM
7466 /* We'll be keeping this opd entry. */
7467 long adjust;
7468
7469 if (fdh != NULL)
7470 {
7471 /* Redefine the function descriptor symbol to
7472 this location in the opd section. It is
7473 necessary to update the value here rather
7474 than using an array of adjustments as we do
7475 for local symbols, because various places
7476 in the generic ELF code use the value
7477 stored in u.def.value. */
7478 fdh->elf.root.u.def.value = wptr - new_contents;
7479 fdh->adjust_done = 1;
7480 }
7481
7482 /* Local syms are a bit tricky. We could
7483 tweak them as they can be cached, but
7484 we'd need to look through the local syms
7485 for the function descriptor sym which we
7486 don't have at the moment. So keep an
7487 array of adjustments. */
7488 adjust = (wptr - new_contents) - (rptr - sec->contents);
7489 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7490
7491 if (wptr != rptr)
7492 memcpy (wptr, rptr, opd_ent_size);
7493 wptr += opd_ent_size;
7494 if (add_aux_fields && opd_ent_size == 16)
7495 {
7496 memset (wptr, '\0', 8);
7497 wptr += 8;
7498 }
7499
50bc7936 7500 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7501 new opd entries. */
7502 for ( ; rel != next_rel; ++rel)
7503 {
7504 rel->r_offset += adjust;
7505 if (write_rel != rel)
7506 memcpy (write_rel, rel, sizeof (*rel));
7507 ++write_rel;
7508 }
1e2f5b6e 7509 }
51aecdc5
AM
7510
7511 rptr += opd_ent_size;
1e2f5b6e
AM
7512 }
7513
3f764659 7514 sec->size = wptr - new_contents;
1e2f5b6e 7515 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7516 if (add_aux_fields)
7517 {
7518 free (sec->contents);
7519 sec->contents = new_contents;
7520 }
7521
05bf9422 7522 /* Fudge the header size too, as this is used later in
cdcf6e38 7523 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7524 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7525 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
0a1b45a2 7526 some_edited = true;
1e2f5b6e 7527 }
6cdc0ccc 7528 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7529 free (relstart);
6cdc0ccc 7530
411e1bfb
AM
7531 if (local_syms != NULL
7532 && symtab_hdr->contents != (unsigned char *) local_syms)
7533 {
7534 if (!info->keep_memory)
7535 free (local_syms);
7536 else
7537 symtab_hdr->contents = (unsigned char *) local_syms;
7538 }
7539 }
7540
754021d0
AM
7541 if (some_edited)
7542 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7543
3f764659
JJ
7544 /* If we are doing a final link and the last .opd entry is just 16 byte
7545 long, add a 8 byte padding after it. */
0e1862bb 7546 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7547 {
7548 bfd_byte *p;
7549
7550 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7551 {
7552 BFD_ASSERT (need_pad->size > 0);
7553
7554 p = bfd_malloc (need_pad->size + 8);
7555 if (p == NULL)
0a1b45a2 7556 return false;
699733f6 7557
2cdcc330
AM
7558 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7559 p, 0, need_pad->size))
0a1b45a2 7560 return false;
3f764659
JJ
7561
7562 need_pad->contents = p;
7563 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7564 }
7565 else
7566 {
7567 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7568 if (p == NULL)
0a1b45a2 7569 return false;
3f764659
JJ
7570
7571 need_pad->contents = p;
7572 }
7573
7574 memset (need_pad->contents + need_pad->size, 0, 8);
7575 need_pad->size += 8;
7576 }
7577
0a1b45a2 7578 return true;
411e1bfb
AM
7579}
7580
3e04d765
AM
7581/* Analyze inline PLT call relocations to see whether calls to locally
7582 defined functions can be converted to direct calls. */
7583
0a1b45a2 7584bool
3e04d765
AM
7585ppc64_elf_inline_plt (struct bfd_link_info *info)
7586{
7587 struct ppc_link_hash_table *htab;
7588 bfd *ibfd;
7589 asection *sec;
7590 bfd_vma low_vma, high_vma, limit;
7591
7592 htab = ppc_hash_table (info);
7593 if (htab == NULL)
0a1b45a2 7594 return false;
3e04d765
AM
7595
7596 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7597 reduced somewhat to cater for possible stubs that might be added
7598 between the call and its destination. */
7599 if (htab->params->group_size < 0)
7600 {
7601 limit = -htab->params->group_size;
7602 if (limit == 1)
7603 limit = 0x1e00000;
7604 }
7605 else
7606 {
7607 limit = htab->params->group_size;
7608 if (limit == 1)
7609 limit = 0x1c00000;
7610 }
7611
7612 low_vma = -1;
7613 high_vma = 0;
7614 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7615 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7616 {
7617 if (low_vma > sec->vma)
7618 low_vma = sec->vma;
7619 if (high_vma < sec->vma + sec->size)
7620 high_vma = sec->vma + sec->size;
7621 }
7622
7623 /* If a "bl" can reach anywhere in local code sections, then we can
7624 convert all inline PLT sequences to direct calls when the symbol
7625 is local. */
7626 if (high_vma - low_vma < limit)
7627 {
7628 htab->can_convert_all_inline_plt = 1;
0a1b45a2 7629 return true;
3e04d765
AM
7630 }
7631
7632 /* Otherwise, go looking through relocs for cases where a direct
7633 call won't reach. Mark the symbol on any such reloc to disable
7634 the optimization and keep the PLT entry as it seems likely that
7635 this will be better than creating trampolines. Note that this
7636 will disable the optimization for all inline PLT calls to a
7637 particular symbol, not just those that won't reach. The
7638 difficulty in doing a more precise optimization is that the
7639 linker needs to make a decision depending on whether a
7640 particular R_PPC64_PLTCALL insn can be turned into a direct
7641 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7642 the sequence, and there is nothing that ties those relocs
7643 together except their symbol. */
7644
7645 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7646 {
7647 Elf_Internal_Shdr *symtab_hdr;
7648 Elf_Internal_Sym *local_syms;
7649
7650 if (!is_ppc64_elf (ibfd))
7651 continue;
7652
7653 local_syms = NULL;
7654 symtab_hdr = &elf_symtab_hdr (ibfd);
7655
7656 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7657 if (ppc64_elf_section_data (sec)->has_pltcall
7658 && !bfd_is_abs_section (sec->output_section))
7659 {
7660 Elf_Internal_Rela *relstart, *rel, *relend;
7661
7662 /* Read the relocations. */
7663 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7664 info->keep_memory);
7665 if (relstart == NULL)
0a1b45a2 7666 return false;
3e04d765
AM
7667
7668 relend = relstart + sec->reloc_count;
435edf0b 7669 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7670 {
7671 enum elf_ppc64_reloc_type r_type;
7672 unsigned long r_symndx;
7673 asection *sym_sec;
7674 struct elf_link_hash_entry *h;
7675 Elf_Internal_Sym *sym;
7676 unsigned char *tls_maskp;
7677
7678 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7679 if (r_type != R_PPC64_PLTCALL
7680 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7681 continue;
7682
7683 r_symndx = ELF64_R_SYM (rel->r_info);
7684 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7685 r_symndx, ibfd))
7686 {
7687 if (elf_section_data (sec)->relocs != relstart)
7688 free (relstart);
c9594989 7689 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765 7690 free (local_syms);
0a1b45a2 7691 return false;
3e04d765
AM
7692 }
7693
7694 if (sym_sec != NULL && sym_sec->output_section != NULL)
7695 {
7696 bfd_vma from, to;
7697 if (h != NULL)
7698 to = h->root.u.def.value;
7699 else
7700 to = sym->st_value;
7701 to += (rel->r_addend
7702 + sym_sec->output_offset
7703 + sym_sec->output_section->vma);
7704 from = (rel->r_offset
7705 + sec->output_offset
7706 + sec->output_section->vma);
5663e321
AM
7707 if (to - from + limit < 2 * limit
7708 && !(r_type == R_PPC64_PLTCALL_NOTOC
7709 && (((h ? h->other : sym->st_other)
7710 & STO_PPC64_LOCAL_MASK)
4a4e7361 7711 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7712 *tls_maskp &= ~PLT_KEEP;
7713 }
7714 }
7715 if (elf_section_data (sec)->relocs != relstart)
7716 free (relstart);
7717 }
7718
7719 if (local_syms != NULL
7720 && symtab_hdr->contents != (unsigned char *) local_syms)
7721 {
7722 if (!info->keep_memory)
7723 free (local_syms);
7724 else
7725 symtab_hdr->contents = (unsigned char *) local_syms;
7726 }
7727 }
7728
0a1b45a2 7729 return true;
3e04d765
AM
7730}
7731
c9405344
AM
7732/* Set htab->tls_get_addr and various other info specific to TLS.
7733 This needs to run before dynamic symbols are processed in
7734 bfd_elf_size_dynamic_sections. */
411e1bfb 7735
0a1b45a2 7736bool
e7d1c40c 7737ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7738{
411e1bfb 7739 struct ppc_link_hash_table *htab;
9e7028aa 7740 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7741
411e1bfb 7742 htab = ppc_hash_table (info);
4dfe6ac6 7743 if (htab == NULL)
0a1b45a2 7744 return false;
4dfe6ac6 7745
ee67d69a
AM
7746 if (abiversion (info->output_bfd) == 1)
7747 htab->opd_abi = 1;
7748
e7d1c40c 7749 if (htab->params->no_multi_toc)
33c0ec9d
AM
7750 htab->do_multi_toc = 0;
7751 else if (!htab->do_multi_toc)
e7d1c40c 7752 htab->params->no_multi_toc = 1;
33c0ec9d 7753
8b5f1ed8
AM
7754 /* Default to --no-plt-localentry, as this option can cause problems
7755 with symbol interposition. For example, glibc libpthread.so and
7756 libc.so duplicate many pthread symbols, with a fallback
7757 implementation in libc.so. In some cases the fallback does more
7758 work than the pthread implementation. __pthread_condattr_destroy
7759 is one such symbol: the libpthread.so implementation is
7760 localentry:0 while the libc.so implementation is localentry:8.
7761 An app that "cleverly" uses dlopen to only load necessary
7762 libraries at runtime may omit loading libpthread.so when not
7763 running multi-threaded, which then results in the libc.so
7764 fallback symbols being used and ld.so complaining. Now there
7765 are workarounds in ld (see non_zero_localentry) to detect the
7766 pthread situation, but that may not be the only case where
7767 --plt-localentry can cause trouble. */
f378ab09 7768 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7769 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7770 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7771 {
7772 /* The issue is that __glink_PLTresolve saves r2, which is done
7773 because glibc ld.so _dl_runtime_resolve restores r2 to support
7774 a glibc plt call optimisation where global entry code is
7775 skipped on calls that resolve to the same binary. The
7776 __glink_PLTresolve save of r2 is incompatible with code
7777 making tail calls, because the tail call might go via the
7778 resolver and thus overwrite the proper saved r2. */
7779 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7780 "power10 pc-relative code"));
7781 htab->params->plt_localentry0 = 0;
7782 }
d44c746a
AM
7783 if (htab->params->plt_localentry0
7784 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
0a1b45a2 7785 false, false, false) == NULL)
cf97bcb0
AM
7786 _bfd_error_handler
7787 (_("warning: --plt-localentry is especially dangerous without "
7788 "ld.so support to detect ABI violations"));
f378ab09 7789
9e7028aa 7790 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
0a1b45a2 7791 false, false, true);
9e7028aa
AM
7792 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7793
a7f2871e 7794 /* Move dynamic linking info to the function descriptor sym. */
9e7028aa
AM
7795 if (tga != NULL)
7796 func_desc_adjust (tga, info);
7797 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
0a1b45a2 7798 false, false, true);
9e7028aa
AM
7799 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7800
7801 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
0a1b45a2 7802 false, false, true);
9e7028aa
AM
7803 htab->tga_desc = ppc_elf_hash_entry (desc);
7804 if (desc != NULL)
7805 func_desc_adjust (desc, info);
7806 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
0a1b45a2 7807 false, false, true);
9e7028aa
AM
7808 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7809
7c9cf415 7810 if (htab->params->tls_get_addr_opt)
a7f2871e 7811 {
9e7028aa 7812 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7813
7814 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
0a1b45a2 7815 false, false, true);
a7f2871e
AM
7816 if (opt != NULL)
7817 func_desc_adjust (opt, info);
7818 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
0a1b45a2 7819 false, false, true);
a7f2871e
AM
7820 if (opt_fd != NULL
7821 && (opt_fd->root.type == bfd_link_hash_defined
7822 || opt_fd->root.type == bfd_link_hash_defweak))
7823 {
7824 /* If glibc supports an optimized __tls_get_addr call stub,
7825 signalled by the presence of __tls_get_addr_opt, and we'll
7826 be calling __tls_get_addr via a plt call stub, then
7827 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
7828 if (!(htab->elf.dynamic_sections_created
7829 && tga_fd != NULL
7830 && (tga_fd->type == STT_FUNC
7831 || tga_fd->needs_plt)
7832 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7833 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7834 tga_fd = NULL;
7835 if (!(htab->elf.dynamic_sections_created
7836 && desc_fd != NULL
7837 && (desc_fd->type == STT_FUNC
7838 || desc_fd->needs_plt)
7839 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7840 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7841 desc_fd = NULL;
7842
7843 if (tga_fd != NULL || desc_fd != NULL)
7844 {
7845 struct plt_entry *ent = NULL;
7846
7847 if (tga_fd != NULL)
7848 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7849 if (ent->plt.refcount > 0)
7850 break;
7851 if (ent == NULL && desc_fd != NULL)
7852 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7853 if (ent->plt.refcount > 0)
7854 break;
a7f2871e
AM
7855 if (ent != NULL)
7856 {
9e7028aa
AM
7857 if (tga_fd != NULL)
7858 {
7859 tga_fd->root.type = bfd_link_hash_indirect;
7860 tga_fd->root.u.i.link = &opt_fd->root;
7861 tga_fd->root.u.i.warning = NULL;
7862 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7863 }
7864 if (desc_fd != NULL)
7865 {
7866 desc_fd->root.type = bfd_link_hash_indirect;
7867 desc_fd->root.u.i.link = &opt_fd->root;
7868 desc_fd->root.u.i.warning = NULL;
7869 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7870 }
b531344c 7871 opt_fd->mark = 1;
a7f2871e
AM
7872 if (opt_fd->dynindx != -1)
7873 {
7874 /* Use __tls_get_addr_opt in dynamic relocations. */
7875 opt_fd->dynindx = -1;
7876 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7877 opt_fd->dynstr_index);
7878 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
0a1b45a2 7879 return false;
a7f2871e 7880 }
9e7028aa 7881 if (tga_fd != NULL)
a7f2871e 7882 {
9e7028aa 7883 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 7884 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
7885 if (opt != NULL && tga != NULL)
7886 {
7887 tga->root.type = bfd_link_hash_indirect;
7888 tga->root.u.i.link = &opt->root;
7889 tga->root.u.i.warning = NULL;
7890 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7891 opt->mark = 1;
7892 _bfd_elf_link_hash_hide_symbol (info, opt,
7893 tga->forced_local);
7894 htab->tls_get_addr = ppc_elf_hash_entry (opt);
7895 }
7896 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7897 htab->tls_get_addr_fd->is_func_descriptor = 1;
7898 if (htab->tls_get_addr != NULL)
7899 {
7900 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7901 htab->tls_get_addr->is_func = 1;
7902 }
a7f2871e 7903 }
9e7028aa 7904 if (desc_fd != NULL)
a7f2871e 7905 {
9e7028aa
AM
7906 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7907 if (opt != NULL && desc != NULL)
7908 {
7909 desc->root.type = bfd_link_hash_indirect;
7910 desc->root.u.i.link = &opt->root;
7911 desc->root.u.i.warning = NULL;
7912 ppc64_elf_copy_indirect_symbol (info, opt, desc);
7913 opt->mark = 1;
7914 _bfd_elf_link_hash_hide_symbol (info, opt,
7915 desc->forced_local);
7916 htab->tga_desc = ppc_elf_hash_entry (opt);
7917 }
7918 htab->tga_desc_fd->oh = htab->tga_desc;
7919 htab->tga_desc_fd->is_func_descriptor = 1;
7920 if (htab->tga_desc != NULL)
7921 {
7922 htab->tga_desc->oh = htab->tga_desc_fd;
7923 htab->tga_desc->is_func = 1;
7924 }
a7f2871e
AM
7925 }
7926 }
7927 }
7928 }
7c9cf415
AM
7929 else if (htab->params->tls_get_addr_opt < 0)
7930 htab->params->tls_get_addr_opt = 0;
a7f2871e 7931 }
9e7028aa
AM
7932
7933 if (htab->tga_desc_fd != NULL
7934 && htab->params->tls_get_addr_opt
7935 && htab->params->no_tls_get_addr_regsave == -1)
7936 htab->params->no_tls_get_addr_regsave = 0;
7937
0a1b45a2 7938 return true;
3a71aa26 7939}
8387904d 7940
3a71aa26 7941/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 7942 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 7943
0a1b45a2 7944static bool
30845f11
AM
7945branch_reloc_hash_match (bfd *ibfd,
7946 Elf_Internal_Rela *rel,
7947 struct ppc_link_hash_entry *hash1,
7948 struct ppc_link_hash_entry *hash2,
7949 struct ppc_link_hash_entry *hash3,
7950 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
7951{
7952 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7953 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7954 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7955
e054468f 7956 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7957 {
3a71aa26
AM
7958 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7959 struct elf_link_hash_entry *h;
8387904d 7960
3a71aa26 7961 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7962 h = elf_follow_link (h);
30845f11
AM
7963 if (h == elf_hash_entry (hash1)
7964 || h == elf_hash_entry (hash2)
7965 || h == elf_hash_entry (hash3)
7966 || h == elf_hash_entry (hash4))
0a1b45a2 7967 return true;
a48ebf4d 7968 }
0a1b45a2 7969 return false;
951fd09b 7970}
411e1bfb 7971
951fd09b
AM
7972/* Run through all the TLS relocs looking for optimization
7973 opportunities. The linker has been hacked (see ppc64elf.em) to do
7974 a preliminary section layout so that we know the TLS segment
7975 offsets. We can't optimize earlier because some optimizations need
7976 to know the tp offset, and we need to optimize before allocating
7977 dynamic relocations. */
7978
0a1b45a2 7979bool
33c0ec9d 7980ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7981{
7982 bfd *ibfd;
7983 asection *sec;
7984 struct ppc_link_hash_table *htab;
663a1470 7985 unsigned char *toc_ref;
102890f0 7986 int pass;
951fd09b 7987
3cbc1e5e 7988 if (!bfd_link_executable (info))
0a1b45a2 7989 return true;
411e1bfb 7990
951fd09b 7991 htab = ppc_hash_table (info);
4dfe6ac6 7992 if (htab == NULL)
0a1b45a2 7993 return false;
4dfe6ac6 7994
252dcdf4
AM
7995 htab->do_tls_opt = 1;
7996
663a1470
AM
7997 /* Make two passes over the relocs. On the first pass, mark toc
7998 entries involved with tls relocs, and check that tls relocs
7999 involved in setting up a tls_get_addr call are indeed followed by
8000 such a call. If they are not, we can't do any tls optimization.
8001 On the second pass twiddle tls_mask flags to notify
8002 relocate_section that optimization can be done, and adjust got
8003 and plt refcounts. */
8004 toc_ref = NULL;
8005 for (pass = 0; pass < 2; ++pass)
c72f2fb2 8006 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
8007 {
8008 Elf_Internal_Sym *locsyms = NULL;
8009 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8010
102890f0
AM
8011 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8012 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8013 {
8014 Elf_Internal_Rela *relstart, *rel, *relend;
0a1b45a2 8015 bool found_tls_get_addr_arg = 0;
411e1bfb 8016
102890f0
AM
8017 /* Read the relocations. */
8018 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8019 info->keep_memory);
8020 if (relstart == NULL)
2915c55b
JK
8021 {
8022 free (toc_ref);
0a1b45a2 8023 return false;
2915c55b 8024 }
411e1bfb 8025
102890f0
AM
8026 relend = relstart + sec->reloc_count;
8027 for (rel = relstart; rel < relend; rel++)
8028 {
8029 enum elf_ppc64_reloc_type r_type;
8030 unsigned long r_symndx;
8031 struct elf_link_hash_entry *h;
8032 Elf_Internal_Sym *sym;
8033 asection *sym_sec;
f961d9dd 8034 unsigned char *tls_mask;
46e9995a 8035 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0 8036 bfd_vma value;
0a1b45a2 8037 bool ok_tprel, is_local;
102890f0
AM
8038 long toc_ref_index = 0;
8039 int expecting_tls_get_addr = 0;
0a1b45a2 8040 bool ret = false;
411e1bfb 8041
102890f0
AM
8042 r_symndx = ELF64_R_SYM (rel->r_info);
8043 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8044 r_symndx, ibfd))
8045 {
8046 err_free_rel:
8047 if (elf_section_data (sec)->relocs != relstart)
8048 free (relstart);
c9594989
AM
8049 free (toc_ref);
8050 if (elf_symtab_hdr (ibfd).contents
8051 != (unsigned char *) locsyms)
102890f0 8052 free (locsyms);
663a1470 8053 return ret;
102890f0 8054 }
411e1bfb 8055
102890f0
AM
8056 if (h != NULL)
8057 {
766bc656
AM
8058 if (h->root.type == bfd_link_hash_defined
8059 || h->root.type == bfd_link_hash_defweak)
8060 value = h->root.u.def.value;
8061 else if (h->root.type == bfd_link_hash_undefweak)
8062 value = 0;
8063 else
663a1470
AM
8064 {
8065 found_tls_get_addr_arg = 0;
8066 continue;
8067 }
102890f0
AM
8068 }
8069 else
8070 /* Symbols referenced by TLS relocs must be of type
8071 STT_TLS. So no need for .opd local sym adjust. */
8072 value = sym->st_value;
8073
0a1b45a2 8074 ok_tprel = false;
f749f26e
AM
8075 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8076 if (is_local)
102890f0 8077 {
766bc656
AM
8078 if (h != NULL
8079 && h->root.type == bfd_link_hash_undefweak)
0a1b45a2 8080 ok_tprel = true;
c27b8c2a
AM
8081 else if (sym_sec != NULL
8082 && sym_sec->output_section != NULL)
766bc656
AM
8083 {
8084 value += sym_sec->output_offset;
8085 value += sym_sec->output_section->vma;
0b147428 8086 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8087 /* Note that even though the prefix insns
8088 allow a 1<<33 offset we use the same test
8089 as for addis;addi. There may be a mix of
8090 pcrel and non-pcrel code and the decision
8091 to optimise is per symbol, not per TLS
8092 sequence. */
0b147428 8093 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8094 }
102890f0 8095 }
951fd09b 8096
102890f0 8097 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8098 /* If this section has old-style __tls_get_addr calls
8099 without marker relocs, then check that each
8100 __tls_get_addr call reloc is preceded by a reloc
8101 that conceivably belongs to the __tls_get_addr arg
8102 setup insn. If we don't find matching arg setup
8103 relocs, don't do any tls optimization. */
8104 if (pass == 0
9737e8af 8105 && sec->nomark_tls_get_addr
663a1470 8106 && h != NULL
ed7007c1 8107 && is_tls_get_addr (h, htab)
663a1470
AM
8108 && !found_tls_get_addr_arg
8109 && is_branch_reloc (r_type))
8110 {
25f53a85 8111 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8112 "TLS optimization disabled\n"),
8113 ibfd, sec, rel->r_offset);
0a1b45a2 8114 ret = true;
663a1470
AM
8115 goto err_free_rel;
8116 }
8117
8118 found_tls_get_addr_arg = 0;
102890f0
AM
8119 switch (r_type)
8120 {
8121 case R_PPC64_GOT_TLSLD16:
8122 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8123 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8124 expecting_tls_get_addr = 1;
663a1470 8125 found_tls_get_addr_arg = 1;
1a0670f3 8126 /* Fall through. */
102890f0
AM
8127
8128 case R_PPC64_GOT_TLSLD16_HI:
8129 case R_PPC64_GOT_TLSLD16_HA:
8130 /* These relocs should never be against a symbol
8131 defined in a shared lib. Leave them alone if
8132 that turns out to be the case. */
8133 if (!is_local)
8134 continue;
411e1bfb 8135
102890f0 8136 /* LD -> LE */
411e1bfb 8137 tls_set = 0;
102890f0
AM
8138 tls_clear = TLS_LD;
8139 tls_type = TLS_TLS | TLS_LD;
8140 break;
411e1bfb 8141
102890f0
AM
8142 case R_PPC64_GOT_TLSGD16:
8143 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8144 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8145 expecting_tls_get_addr = 1;
663a1470 8146 found_tls_get_addr_arg = 1;
1a0670f3 8147 /* Fall through. */
102890f0
AM
8148
8149 case R_PPC64_GOT_TLSGD16_HI:
8150 case R_PPC64_GOT_TLSGD16_HA:
8151 if (ok_tprel)
8152 /* GD -> LE */
411e1bfb 8153 tls_set = 0;
102890f0
AM
8154 else
8155 /* GD -> IE */
b00a0a86 8156 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8157 tls_clear = TLS_GD;
8158 tls_type = TLS_TLS | TLS_GD;
8159 break;
8160
87c69f97 8161 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8162 case R_PPC64_GOT_TPREL16_DS:
8163 case R_PPC64_GOT_TPREL16_LO_DS:
8164 case R_PPC64_GOT_TPREL16_HI:
8165 case R_PPC64_GOT_TPREL16_HA:
8166 if (ok_tprel)
8167 {
8168 /* IE -> LE */
8169 tls_set = 0;
8170 tls_clear = TLS_TPREL;
8171 tls_type = TLS_TLS | TLS_TPREL;
8172 break;
8173 }
411e1bfb
AM
8174 continue;
8175
727fc41e 8176 case R_PPC64_TLSLD:
7d04a20a
AM
8177 if (!is_local)
8178 continue;
8179 /* Fall through. */
8180 case R_PPC64_TLSGD:
23cedd1d
AM
8181 if (rel + 1 < relend
8182 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8183 {
8184 if (pass != 0
2cdcc330 8185 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8186 != R_PPC64_PLTSEQ)
8187 && (ELF64_R_TYPE (rel[1].r_info)
8188 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8189 {
8190 r_symndx = ELF64_R_SYM (rel[1].r_info);
8191 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8192 r_symndx, ibfd))
23cedd1d
AM
8193 goto err_free_rel;
8194 if (h != NULL)
8195 {
8196 struct plt_entry *ent = NULL;
8197
8198 for (ent = h->plt.plist;
8199 ent != NULL;
8200 ent = ent->next)
8201 if (ent->addend == rel[1].r_addend)
8202 break;
8203
8204 if (ent != NULL
8205 && ent->plt.refcount > 0)
8206 ent->plt.refcount -= 1;
8207 }
8208 }
8209 continue;
8210 }
663a1470 8211 found_tls_get_addr_arg = 1;
1a0670f3 8212 /* Fall through. */
663a1470
AM
8213
8214 case R_PPC64_TLS:
8215 case R_PPC64_TOC16:
8216 case R_PPC64_TOC16_LO:
102890f0
AM
8217 if (sym_sec == NULL || sym_sec != toc)
8218 continue;
8219
8220 /* Mark this toc entry as referenced by a TLS
8221 code sequence. We can do that now in the
8222 case of R_PPC64_TLS, and after checking for
8223 tls_get_addr for the TOC16 relocs. */
8224 if (toc_ref == NULL)
2cdcc330
AM
8225 toc_ref
8226 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8227 if (toc_ref == NULL)
8228 goto err_free_rel;
8229
102890f0
AM
8230 if (h != NULL)
8231 value = h->root.u.def.value;
8232 else
8233 value = sym->st_value;
8234 value += rel->r_addend;
73242275
AM
8235 if (value % 8 != 0)
8236 continue;
8237 BFD_ASSERT (value < toc->size
8238 && toc->output_offset % 8 == 0);
663a1470 8239 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8240 if (r_type == R_PPC64_TLS
8241 || r_type == R_PPC64_TLSGD
8242 || r_type == R_PPC64_TLSLD)
102890f0
AM
8243 {
8244 toc_ref[toc_ref_index] = 1;
8245 continue;
8246 }
8247
8248 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8249 continue;
8250
8251 tls_set = 0;
8252 tls_clear = 0;
8253 expecting_tls_get_addr = 2;
8254 break;
8255
8256 case R_PPC64_TPREL64:
8257 if (pass == 0
8258 || sec != toc
8259 || toc_ref == NULL
663a1470 8260 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8261 continue;
8262 if (ok_tprel)
8263 {
8264 /* IE -> LE */
8265 tls_set = TLS_EXPLICIT;
8266 tls_clear = TLS_TPREL;
8267 break;
8268 }
8269 continue;
8270
8271 case R_PPC64_DTPMOD64:
8272 if (pass == 0
8273 || sec != toc
8274 || toc_ref == NULL
663a1470 8275 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8276 continue;
8277 if (rel + 1 < relend
8278 && (rel[1].r_info
8279 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8280 && rel[1].r_offset == rel->r_offset + 8)
8281 {
8282 if (ok_tprel)
8283 /* GD -> LE */
8284 tls_set = TLS_EXPLICIT | TLS_GD;
8285 else
8286 /* GD -> IE */
b00a0a86 8287 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8288 tls_clear = TLS_GD;
8289 }
8290 else
8291 {
8292 if (!is_local)
8293 continue;
8294
8295 /* LD -> LE */
8296 tls_set = TLS_EXPLICIT;
8297 tls_clear = TLS_LD;
8298 }
8299 break;
8300
252dcdf4
AM
8301 case R_PPC64_TPREL16_HA:
8302 if (pass == 0)
8303 {
8304 unsigned char buf[4];
8305 unsigned int insn;
8306 bfd_vma off = rel->r_offset & ~3;
8307 if (!bfd_get_section_contents (ibfd, sec, buf,
8308 off, 4))
8309 goto err_free_rel;
8310 insn = bfd_get_32 (ibfd, buf);
8311 /* addis rt,13,imm */
8312 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8313 != ((15u << 26) | (13 << 16)))
8314 {
8315 /* xgettext:c-format */
8316 info->callbacks->minfo
8317 (_("%H: warning: %s unexpected insn %#x.\n"),
8318 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8319 htab->do_tls_opt = 0;
8320 }
8321 }
8322 continue;
8323
8324 case R_PPC64_TPREL16_HI:
8325 case R_PPC64_TPREL16_HIGH:
8326 case R_PPC64_TPREL16_HIGHA:
8327 case R_PPC64_TPREL16_HIGHER:
8328 case R_PPC64_TPREL16_HIGHERA:
8329 case R_PPC64_TPREL16_HIGHEST:
8330 case R_PPC64_TPREL16_HIGHESTA:
8331 /* These can all be used in sequences along with
8332 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8333 able to verify easily. */
8334 htab->do_tls_opt = 0;
8335 continue;
8336
102890f0
AM
8337 default:
8338 continue;
8339 }
8340
8341 if (pass == 0)
8342 {
727fc41e 8343 if (!expecting_tls_get_addr
9737e8af 8344 || !sec->nomark_tls_get_addr)
102890f0
AM
8345 continue;
8346
3a71aa26
AM
8347 if (rel + 1 < relend
8348 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8349 htab->tls_get_addr_fd,
8350 htab->tga_desc_fd,
3a71aa26 8351 htab->tls_get_addr,
9e7028aa 8352 htab->tga_desc))
102890f0 8353 {
3a71aa26 8354 if (expecting_tls_get_addr == 2)
102890f0 8355 {
3a71aa26 8356 /* Check for toc tls entries. */
f961d9dd 8357 unsigned char *toc_tls;
3a71aa26
AM
8358 int retval;
8359
8360 retval = get_tls_mask (&toc_tls, NULL, NULL,
8361 &locsyms,
8362 rel, ibfd);
8363 if (retval == 0)
8364 goto err_free_rel;
663a1470
AM
8365 if (toc_tls != NULL)
8366 {
37da22e5
AM
8367 if ((*toc_tls & TLS_TLS) != 0
8368 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8369 found_tls_get_addr_arg = 1;
8370 if (retval > 1)
8371 toc_ref[toc_ref_index] = 1;
8372 }
102890f0 8373 }
3a71aa26 8374 continue;
102890f0
AM
8375 }
8376
102890f0
AM
8377 /* Uh oh, we didn't find the expected call. We
8378 could just mark this symbol to exclude it
8379 from tls optimization but it's safer to skip
663a1470 8380 the entire optimization. */
695344c0 8381 /* xgettext:c-format */
25f53a85 8382 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8383 "TLS optimization disabled\n"),
8384 ibfd, sec, rel->r_offset);
0a1b45a2 8385 ret = true;
663a1470 8386 goto err_free_rel;
102890f0
AM
8387 }
8388
37da22e5
AM
8389 /* If we don't have old-style __tls_get_addr calls
8390 without TLSGD/TLSLD marker relocs, and we haven't
8391 found a new-style __tls_get_addr call with a
8392 marker for this symbol, then we either have a
8393 broken object file or an -mlongcall style
8394 indirect call to __tls_get_addr without a marker.
8395 Disable optimization in this case. */
8396 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8397 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8398 && !sec->nomark_tls_get_addr
37da22e5
AM
8399 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8400 != (TLS_TLS | TLS_MARK)))
8401 continue;
8402
7d04a20a 8403 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8404 {
23cedd1d
AM
8405 struct plt_entry *ent = NULL;
8406
9e7028aa
AM
8407 if (htab->tls_get_addr_fd != NULL)
8408 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8409 ent != NULL;
8410 ent = ent->next)
8411 if (ent->addend == 0)
8412 break;
8413
8414 if (ent == NULL && htab->tga_desc_fd != NULL)
8415 for (ent = htab->tga_desc_fd->elf.plt.plist;
8416 ent != NULL;
8417 ent = ent->next)
8418 if (ent->addend == 0)
8419 break;
8420
8421 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8422 for (ent = htab->tls_get_addr->elf.plt.plist;
8423 ent != NULL;
8424 ent = ent->next)
8425 if (ent->addend == 0)
102890f0 8426 break;
411e1bfb 8427
9e7028aa
AM
8428 if (ent == NULL && htab->tga_desc != NULL)
8429 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8430 ent != NULL;
8431 ent = ent->next)
8432 if (ent->addend == 0)
102890f0 8433 break;
23cedd1d
AM
8434
8435 if (ent != NULL
8436 && ent->plt.refcount > 0)
8437 ent->plt.refcount -= 1;
102890f0 8438 }
411e1bfb 8439
102890f0 8440 if (tls_clear == 0)
30038c59
AM
8441 continue;
8442
102890f0
AM
8443 if ((tls_set & TLS_EXPLICIT) == 0)
8444 {
8445 struct got_entry *ent;
411e1bfb 8446
102890f0
AM
8447 /* Adjust got entry for this reloc. */
8448 if (h != NULL)
8449 ent = h->got.glist;
8450 else
8451 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8452
102890f0
AM
8453 for (; ent != NULL; ent = ent->next)
8454 if (ent->addend == rel->r_addend
8455 && ent->owner == ibfd
8456 && ent->tls_type == tls_type)
8457 break;
8458 if (ent == NULL)
8459 abort ();
411e1bfb 8460
102890f0
AM
8461 if (tls_set == 0)
8462 {
8463 /* We managed to get rid of a got entry. */
8464 if (ent->got.refcount > 0)
8465 ent->got.refcount -= 1;
8466 }
8467 }
8468 else
8469 {
8470 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8471 we'll lose one or two dyn relocs. */
8472 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8473 NULL, h, sym))
0a1b45a2 8474 return false;
411e1bfb 8475
102890f0
AM
8476 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8477 {
8478 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8479 NULL, h, sym))
0a1b45a2 8480 return false;
102890f0
AM
8481 }
8482 }
411e1bfb 8483
46e9995a 8484 *tls_mask |= tls_set & 0xff;
102890f0
AM
8485 *tls_mask &= ~tls_clear;
8486 }
8c1d1bb8 8487
102890f0
AM
8488 if (elf_section_data (sec)->relocs != relstart)
8489 free (relstart);
8490 }
411e1bfb 8491
663a1470
AM
8492 if (locsyms != NULL
8493 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8494 {
8495 if (!info->keep_memory)
8496 free (locsyms);
8497 else
8498 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8499 }
8500 }
411e1bfb 8501
c9594989 8502 free (toc_ref);
0a1b45a2 8503 return true;
1e2f5b6e 8504}
b34976b6 8505
c5614fa4
AM
8506/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8507 the values of any global symbols in a toc section that has been
8508 edited. Globals in toc sections should be a rarity, so this function
8509 sets a flag if any are found in toc sections other than the one just
de194d85 8510 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8511
8512struct adjust_toc_info
8513{
8514 asection *toc;
8515 unsigned long *skip;
0a1b45a2 8516 bool global_toc_syms;
c5614fa4
AM
8517};
8518
ba761f19
AM
8519enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8520
0a1b45a2 8521static bool
c5614fa4
AM
8522adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8523{
8524 struct ppc_link_hash_entry *eh;
8525 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8526 unsigned long i;
c5614fa4 8527
c5614fa4
AM
8528 if (h->root.type != bfd_link_hash_defined
8529 && h->root.type != bfd_link_hash_defweak)
0a1b45a2 8530 return true;
c5614fa4 8531
ed7007c1 8532 eh = ppc_elf_hash_entry (h);
c5614fa4 8533 if (eh->adjust_done)
0a1b45a2 8534 return true;
c5614fa4
AM
8535
8536 if (eh->elf.root.u.def.section == toc_inf->toc)
8537 {
854b41e7
AM
8538 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8539 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8540 else
854b41e7
AM
8541 i = eh->elf.root.u.def.value >> 3;
8542
ba761f19 8543 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8544 {
4eca0228 8545 _bfd_error_handler
854b41e7
AM
8546 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8547 do
8548 ++i;
ba761f19 8549 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8550 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8551 }
854b41e7
AM
8552
8553 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8554 eh->adjust_done = 1;
8555 }
8556 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
0a1b45a2 8557 toc_inf->global_toc_syms = true;
c5614fa4 8558
0a1b45a2 8559 return true;
c5614fa4
AM
8560}
8561
39eeab25
AM
8562/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8563 on a _LO variety toc/got reloc. */
560c8763 8564
0a1b45a2 8565static bool
39eeab25 8566ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8567{
2365f8d7
AM
8568 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8569 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8570 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8571 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8572 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8573 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8574 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8575 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8576 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8577 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8578 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8579 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8580 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8581 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8582 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8583 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8584 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8585 /* Exclude lfqu by testing reloc. If relocs are ever
8586 defined for the reduced D field in psq_lu then those
8587 will need testing too. */
8588 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8589 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8590 && (insn & 1) == 0)
2365f8d7
AM
8591 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8592 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8593 /* Exclude stfqu. psq_stu as above for psq_lu. */
8594 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8595 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8596 && (insn & 1) == 0));
560c8763
AM
8597}
8598
4a421c53
AM
8599/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8600 pld ra,symbol@got@pcrel
dd9b12c2 8601 load/store rt,off(ra)
4a421c53 8602 or
d4b87b1e 8603 pla ra,symbol@pcrel
dd9b12c2 8604 load/store rt,off(ra)
4a421c53 8605 may be translated to
dd9b12c2 8606 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8607 nop.
8608 This function returns true if the optimization is possible, placing
dd9b12c2 8609 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8610
8611 On entry to this function, the linker has already determined that
d4b87b1e 8612 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8613 while *PINSN2 is the second instruction. */
8614
0a1b45a2 8615static bool
dd9b12c2 8616xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8617{
77486630
AM
8618 uint64_t insn1 = *pinsn1;
8619 uint64_t insn2 = *pinsn2;
dd9b12c2 8620 bfd_signed_vma off;
4a421c53 8621
77486630
AM
8622 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8623 {
8624 /* Check that regs match. */
8625 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8626 return false;
77486630
AM
8627
8628 /* P8LS or PMLS form, non-pcrel. */
8629 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
0a1b45a2 8630 return false;
77486630
AM
8631
8632 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8633 *pinsn2 = PNOP;
8634 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8635 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
0a1b45a2 8636 return true;
77486630
AM
8637 }
8638
8639 insn2 >>= 32;
8640
4a421c53 8641 /* Check that regs match. */
77486630 8642 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
0a1b45a2 8643 return false;
4a421c53
AM
8644
8645 switch ((insn2 >> 26) & 63)
8646 {
8647 default:
0a1b45a2 8648 return false;
4a421c53
AM
8649
8650 case 32: /* lwz */
8651 case 34: /* lbz */
8652 case 36: /* stw */
8653 case 38: /* stb */
8654 case 40: /* lhz */
8655 case 42: /* lha */
8656 case 44: /* sth */
8657 case 48: /* lfs */
8658 case 50: /* lfd */
8659 case 52: /* stfs */
8660 case 54: /* stfd */
8661 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8662 on the insn. */
77486630 8663 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8664 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8665 off = insn2 & 0xffff;
4a421c53
AM
8666 break;
8667
8668 case 58: /* lwa, ld */
dd9b12c2 8669 if ((insn2 & 1) != 0)
0a1b45a2 8670 return false;
77486630 8671 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8672 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8673 | (insn2 & (31ULL << 21)));
dd9b12c2 8674 off = insn2 & 0xfffc;
4a421c53
AM
8675 break;
8676
8677 case 57: /* lxsd, lxssp */
dd9b12c2 8678 if ((insn2 & 3) < 2)
0a1b45a2 8679 return false;
77486630 8680 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8681 | ((40ULL | (insn2 & 3)) << 26)
8682 | (insn2 & (31ULL << 21)));
dd9b12c2 8683 off = insn2 & 0xfffc;
4a421c53
AM
8684 break;
8685
8686 case 61: /* stxsd, stxssp, lxv, stxv */
8687 if ((insn2 & 3) == 0)
0a1b45a2 8688 return false;
4a421c53
AM
8689 else if ((insn2 & 3) >= 2)
8690 {
77486630 8691 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8692 | ((44ULL | (insn2 & 3)) << 26)
8693 | (insn2 & (31ULL << 21)));
dd9b12c2 8694 off = insn2 & 0xfffc;
4a421c53
AM
8695 }
8696 else
8697 {
77486630 8698 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8699 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8700 | (insn2 & (31ULL << 21)));
dd9b12c2 8701 off = insn2 & 0xfff0;
4a421c53
AM
8702 }
8703 break;
8704
8705 case 56: /* lq */
77486630 8706 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8707 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8708 off = insn2 & 0xffff;
4a421c53
AM
8709 break;
8710
94ba9882
AM
8711 case 6: /* lxvp, stxvp */
8712 if ((insn2 & 0xe) != 0)
0a1b45a2 8713 return false;
94ba9882
AM
8714 insn1 = ((1ULL << 58) | (1ULL << 52)
8715 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8716 | (insn2 & (31ULL << 21)));
8717 off = insn2 & 0xfff0;
8718 break;
8719
4a421c53 8720 case 62: /* std, stq */
dd9b12c2 8721 if ((insn2 & 1) != 0)
0a1b45a2 8722 return false;
77486630 8723 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8724 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8725 | (insn2 & (31ULL << 21)));
dd9b12c2 8726 off = insn2 & 0xfffc;
4a421c53
AM
8727 break;
8728 }
8729
77486630 8730 *pinsn1 = insn1;
4a421c53 8731 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8732 *poff = (off ^ 0x8000) - 0x8000;
0a1b45a2 8733 return true;
4a421c53
AM
8734}
8735
c5614fa4
AM
8736/* Examine all relocs referencing .toc sections in order to remove
8737 unused .toc entries. */
8738
0a1b45a2 8739bool
33c0ec9d 8740ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8741{
8742 bfd *ibfd;
8743 struct adjust_toc_info toc_inf;
67f0cbdb 8744 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8745
67f0cbdb 8746 htab->do_toc_opt = 1;
0a1b45a2 8747 toc_inf.global_toc_syms = true;
c72f2fb2 8748 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8749 {
8750 asection *toc, *sec;
8751 Elf_Internal_Shdr *symtab_hdr;
8752 Elf_Internal_Sym *local_syms;
425b145b 8753 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8754 unsigned long *skip, *drop;
8755 unsigned char *used;
8756 unsigned char *keep, last, some_unused;
8757
854b41e7
AM
8758 if (!is_ppc64_elf (ibfd))
8759 continue;
8760
c5614fa4
AM
8761 toc = bfd_get_section_by_name (ibfd, ".toc");
8762 if (toc == NULL
92b7a70f 8763 || toc->size == 0
dbaa2011
AM
8764 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8765 || discarded_section (toc))
c5614fa4
AM
8766 continue;
8767
425b145b 8768 toc_relocs = NULL;
c5614fa4 8769 local_syms = NULL;
0ffa91dd 8770 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8771
8772 /* Look at sections dropped from the final link. */
8773 skip = NULL;
8774 relstart = NULL;
8775 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8776 {
8777 if (sec->reloc_count == 0
dbaa2011 8778 || !discarded_section (sec)
c5614fa4
AM
8779 || get_opd_info (sec)
8780 || (sec->flags & SEC_ALLOC) == 0
8781 || (sec->flags & SEC_DEBUGGING) != 0)
8782 continue;
8783
0a1b45a2 8784 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
c5614fa4
AM
8785 if (relstart == NULL)
8786 goto error_ret;
8787
8788 /* Run through the relocs to see which toc entries might be
8789 unused. */
8790 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8791 {
8792 enum elf_ppc64_reloc_type r_type;
8793 unsigned long r_symndx;
8794 asection *sym_sec;
8795 struct elf_link_hash_entry *h;
8796 Elf_Internal_Sym *sym;
8797 bfd_vma val;
8798
8799 r_type = ELF64_R_TYPE (rel->r_info);
8800 switch (r_type)
8801 {
8802 default:
8803 continue;
8804
8805 case R_PPC64_TOC16:
8806 case R_PPC64_TOC16_LO:
8807 case R_PPC64_TOC16_HI:
8808 case R_PPC64_TOC16_HA:
8809 case R_PPC64_TOC16_DS:
8810 case R_PPC64_TOC16_LO_DS:
8811 break;
8812 }
8813
8814 r_symndx = ELF64_R_SYM (rel->r_info);
8815 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8816 r_symndx, ibfd))
8817 goto error_ret;
8818
8819 if (sym_sec != toc)
8820 continue;
8821
8822 if (h != NULL)
8823 val = h->root.u.def.value;
8824 else
8825 val = sym->st_value;
8826 val += rel->r_addend;
8827
8828 if (val >= toc->size)
8829 continue;
8830
8831 /* Anything in the toc ought to be aligned to 8 bytes.
8832 If not, don't mark as unused. */
8833 if (val & 7)
8834 continue;
8835
8836 if (skip == NULL)
8837 {
854b41e7 8838 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8839 if (skip == NULL)
8840 goto error_ret;
8841 }
8842
ba761f19 8843 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8844 }
8845
8846 if (elf_section_data (sec)->relocs != relstart)
8847 free (relstart);
8848 }
8849
ba761f19
AM
8850 /* For largetoc loads of address constants, we can convert
8851 . addis rx,2,addr@got@ha
8852 . ld ry,addr@got@l(rx)
8853 to
8854 . addis rx,2,addr@toc@ha
8855 . addi ry,rx,addr@toc@l
8856 when addr is within 2G of the toc pointer. This then means
8857 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8858
ba761f19
AM
8859 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8860 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8861 && toc->reloc_count != 0)
8862 {
8863 /* Read toc relocs. */
425b145b
AM
8864 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8865 info->keep_memory);
8866 if (toc_relocs == NULL)
ba761f19
AM
8867 goto error_ret;
8868
425b145b 8869 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8870 {
8871 enum elf_ppc64_reloc_type r_type;
8872 unsigned long r_symndx;
8873 asection *sym_sec;
8874 struct elf_link_hash_entry *h;
8875 Elf_Internal_Sym *sym;
8876 bfd_vma val, addr;
8877
8878 r_type = ELF64_R_TYPE (rel->r_info);
8879 if (r_type != R_PPC64_ADDR64)
8880 continue;
8881
8882 r_symndx = ELF64_R_SYM (rel->r_info);
8883 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8884 r_symndx, ibfd))
8885 goto error_ret;
8886
425b145b 8887 if (sym_sec == NULL
c27b8c2a 8888 || sym_sec->output_section == NULL
dbaa2011 8889 || discarded_section (sym_sec))
425b145b
AM
8890 continue;
8891
afe397ea 8892 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8893 continue;
8894
8895 if (h != NULL)
bddc25c9
AM
8896 {
8897 if (h->type == STT_GNU_IFUNC)
8898 continue;
8899 val = h->root.u.def.value;
8900 }
ba761f19 8901 else
bddc25c9
AM
8902 {
8903 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8904 continue;
8905 val = sym->st_value;
8906 }
ba761f19
AM
8907 val += rel->r_addend;
8908 val += sym_sec->output_section->vma + sym_sec->output_offset;
8909
8910 /* We don't yet know the exact toc pointer value, but we
8911 know it will be somewhere in the toc section. Don't
8912 optimize if the difference from any possible toc
8913 pointer is outside [ff..f80008000, 7fff7fff]. */
8914 addr = toc->output_section->vma + TOC_BASE_OFF;
8915 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8916 continue;
8917
8918 addr = toc->output_section->vma + toc->output_section->rawsize;
8919 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8920 continue;
8921
8922 if (skip == NULL)
8923 {
8924 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8925 if (skip == NULL)
8926 goto error_ret;
8927 }
8928
8929 skip[rel->r_offset >> 3]
425b145b 8930 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8931 }
ba761f19
AM
8932 }
8933
c5614fa4
AM
8934 if (skip == NULL)
8935 continue;
8936
8937 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8938 if (used == NULL)
8939 {
8940 error_ret:
c9594989 8941 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
8942 free (local_syms);
8943 if (sec != NULL
c5614fa4
AM
8944 && elf_section_data (sec)->relocs != relstart)
8945 free (relstart);
c9594989 8946 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 8947 free (toc_relocs);
c9594989 8948 free (skip);
0a1b45a2 8949 return false;
c5614fa4
AM
8950 }
8951
30038c59
AM
8952 /* Now check all kept sections that might reference the toc.
8953 Check the toc itself last. */
8954 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8955 : ibfd->sections);
c5614fa4 8956 sec != NULL;
c5614fa4 8957 sec = (sec == toc ? NULL
c5614fa4 8958 : sec->next == NULL ? toc
30038c59 8959 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8960 : sec->next))
8961 {
8962 int repeat;
8963
8964 if (sec->reloc_count == 0
dbaa2011 8965 || discarded_section (sec)
c5614fa4
AM
8966 || get_opd_info (sec)
8967 || (sec->flags & SEC_ALLOC) == 0
8968 || (sec->flags & SEC_DEBUGGING) != 0)
8969 continue;
8970
854b41e7
AM
8971 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8972 info->keep_memory);
c5614fa4 8973 if (relstart == NULL)
2915c55b
JK
8974 {
8975 free (used);
8976 goto error_ret;
8977 }
c5614fa4
AM
8978
8979 /* Mark toc entries referenced as used. */
c5614fa4 8980 do
d4f1ee75
AM
8981 {
8982 repeat = 0;
8983 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8984 {
8985 enum elf_ppc64_reloc_type r_type;
8986 unsigned long r_symndx;
8987 asection *sym_sec;
8988 struct elf_link_hash_entry *h;
8989 Elf_Internal_Sym *sym;
8990 bfd_vma val;
98528052 8991
d4f1ee75 8992 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8993 switch (r_type)
8994 {
8995 case R_PPC64_TOC16:
8996 case R_PPC64_TOC16_LO:
8997 case R_PPC64_TOC16_HI:
8998 case R_PPC64_TOC16_HA:
8999 case R_PPC64_TOC16_DS:
9000 case R_PPC64_TOC16_LO_DS:
9001 /* In case we're taking addresses of toc entries. */
9002 case R_PPC64_ADDR64:
9003 break;
c5614fa4 9004
d4f1ee75
AM
9005 default:
9006 continue;
9007 }
c5614fa4 9008
d4f1ee75
AM
9009 r_symndx = ELF64_R_SYM (rel->r_info);
9010 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9011 r_symndx, ibfd))
9012 {
9013 free (used);
9014 goto error_ret;
9015 }
c5614fa4 9016
d4f1ee75
AM
9017 if (sym_sec != toc)
9018 continue;
c5614fa4 9019
d4f1ee75
AM
9020 if (h != NULL)
9021 val = h->root.u.def.value;
9022 else
9023 val = sym->st_value;
9024 val += rel->r_addend;
ba761f19 9025
d4f1ee75
AM
9026 if (val >= toc->size)
9027 continue;
ba761f19 9028
d4f1ee75
AM
9029 if ((skip[val >> 3] & can_optimize) != 0)
9030 {
9031 bfd_vma off;
9032 unsigned char opc;
9033
9034 switch (r_type)
9035 {
9036 case R_PPC64_TOC16_HA:
ba761f19 9037 break;
ba761f19 9038
d4f1ee75
AM
9039 case R_PPC64_TOC16_LO_DS:
9040 off = rel->r_offset;
9041 off += (bfd_big_endian (ibfd) ? -2 : 3);
9042 if (!bfd_get_section_contents (ibfd, sec, &opc,
9043 off, 1))
9044 {
9045 free (used);
9046 goto error_ret;
9047 }
9048 if ((opc & (0x3f << 2)) == (58u << 2))
9049 break;
1a0670f3 9050 /* Fall through. */
ba761f19 9051
d4f1ee75
AM
9052 default:
9053 /* Wrong sort of reloc, or not a ld. We may
9054 as well clear ref_from_discarded too. */
9055 skip[val >> 3] = 0;
9056 }
9057 }
9058
9059 if (sec != toc)
9060 used[val >> 3] = 1;
9061 /* For the toc section, we only mark as used if this
9062 entry itself isn't unused. */
9063 else if ((used[rel->r_offset >> 3]
9064 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9065 && !used[val >> 3])
9066 {
9067 /* Do all the relocs again, to catch reference
9068 chains. */
9069 repeat = 1;
9070 used[val >> 3] = 1;
9071 }
9072 }
9073 }
c5614fa4 9074 while (repeat);
854b41e7
AM
9075
9076 if (elf_section_data (sec)->relocs != relstart)
9077 free (relstart);
c5614fa4
AM
9078 }
9079
9080 /* Merge the used and skip arrays. Assume that TOC
9081 doublewords not appearing as either used or unused belong
de194d85 9082 to an entry more than one doubleword in size. */
c5614fa4
AM
9083 for (drop = skip, keep = used, last = 0, some_unused = 0;
9084 drop < skip + (toc->size + 7) / 8;
9085 ++drop, ++keep)
9086 {
9087 if (*keep)
9088 {
ba761f19
AM
9089 *drop &= ~ref_from_discarded;
9090 if ((*drop & can_optimize) != 0)
9091 some_unused = 1;
c5614fa4
AM
9092 last = 0;
9093 }
b140b010 9094 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9095 {
9096 some_unused = 1;
ba761f19 9097 last = ref_from_discarded;
c5614fa4
AM
9098 }
9099 else
9100 *drop = last;
9101 }
9102
9103 free (used);
9104
9105 if (some_unused)
9106 {
9107 bfd_byte *contents, *src;
9108 unsigned long off;
d62b3684 9109 Elf_Internal_Sym *sym;
0a1b45a2 9110 bool local_toc_syms = false;
c5614fa4
AM
9111
9112 /* Shuffle the toc contents, and at the same time convert the
9113 skip array from booleans into offsets. */
9114 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9115 goto error_ret;
9116
9117 elf_section_data (toc)->this_hdr.contents = contents;
9118
9119 for (src = contents, off = 0, drop = skip;
9120 src < contents + toc->size;
9121 src += 8, ++drop)
9122 {
ba761f19
AM
9123 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9124 off += 8;
c5614fa4
AM
9125 else if (off != 0)
9126 {
9127 *drop = off;
9128 memcpy (src - off, src, 8);
9129 }
9130 }
854b41e7 9131 *drop = off;
c5614fa4
AM
9132 toc->rawsize = toc->size;
9133 toc->size = src - contents - off;
9134
ba761f19
AM
9135 /* Adjust addends for relocs against the toc section sym,
9136 and optimize any accesses we can. */
c5614fa4
AM
9137 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9138 {
9139 if (sec->reloc_count == 0
dbaa2011 9140 || discarded_section (sec))
c5614fa4
AM
9141 continue;
9142
9143 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9144 info->keep_memory);
c5614fa4
AM
9145 if (relstart == NULL)
9146 goto error_ret;
9147
9148 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9149 {
9150 enum elf_ppc64_reloc_type r_type;
9151 unsigned long r_symndx;
9152 asection *sym_sec;
9153 struct elf_link_hash_entry *h;
854b41e7 9154 bfd_vma val;
c5614fa4
AM
9155
9156 r_type = ELF64_R_TYPE (rel->r_info);
9157 switch (r_type)
9158 {
9159 default:
9160 continue;
9161
9162 case R_PPC64_TOC16:
9163 case R_PPC64_TOC16_LO:
9164 case R_PPC64_TOC16_HI:
9165 case R_PPC64_TOC16_HA:
9166 case R_PPC64_TOC16_DS:
9167 case R_PPC64_TOC16_LO_DS:
9168 case R_PPC64_ADDR64:
9169 break;
9170 }
9171
9172 r_symndx = ELF64_R_SYM (rel->r_info);
9173 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9174 r_symndx, ibfd))
9175 goto error_ret;
9176
ba761f19 9177 if (sym_sec != toc)
c5614fa4
AM
9178 continue;
9179
ba761f19
AM
9180 if (h != NULL)
9181 val = h->root.u.def.value;
9182 else
9183 {
9184 val = sym->st_value;
9185 if (val != 0)
0a1b45a2 9186 local_toc_syms = true;
ba761f19
AM
9187 }
9188
9189 val += rel->r_addend;
854b41e7
AM
9190
9191 if (val > toc->rawsize)
9192 val = toc->rawsize;
ba761f19
AM
9193 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9194 continue;
9195 else if ((skip[val >> 3] & can_optimize) != 0)
9196 {
9197 Elf_Internal_Rela *tocrel
425b145b 9198 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9199 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9200
9201 switch (r_type)
9202 {
9203 case R_PPC64_TOC16_HA:
9204 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9205 break;
9206
9207 case R_PPC64_TOC16_LO_DS:
9208 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9209 break;
9210
9211 default:
28942f62
AM
9212 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9213 ppc_howto_init ();
b140b010 9214 info->callbacks->einfo
695344c0 9215 /* xgettext:c-format */
174d0a74 9216 (_("%H: %s references "
b140b010
AM
9217 "optimized away TOC entry\n"),
9218 ibfd, sec, rel->r_offset,
9219 ppc64_elf_howto_table[r_type]->name);
9220 bfd_set_error (bfd_error_bad_value);
9221 goto error_ret;
ba761f19
AM
9222 }
9223 rel->r_addend = tocrel->r_addend;
9224 elf_section_data (sec)->relocs = relstart;
9225 continue;
9226 }
9227
9228 if (h != NULL || sym->st_value != 0)
9229 continue;
854b41e7
AM
9230
9231 rel->r_addend -= skip[val >> 3];
9232 elf_section_data (sec)->relocs = relstart;
c5614fa4 9233 }
854b41e7
AM
9234
9235 if (elf_section_data (sec)->relocs != relstart)
9236 free (relstart);
c5614fa4
AM
9237 }
9238
9239 /* We shouldn't have local or global symbols defined in the TOC,
9240 but handle them anyway. */
df22d223
AM
9241 if (local_syms != NULL)
9242 for (sym = local_syms;
9243 sym < local_syms + symtab_hdr->sh_info;
9244 ++sym)
9245 if (sym->st_value != 0
9246 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9247 {
9248 unsigned long i;
854b41e7 9249
df22d223
AM
9250 if (sym->st_value > toc->rawsize)
9251 i = toc->rawsize >> 3;
9252 else
9253 i = sym->st_value >> 3;
854b41e7 9254
df22d223
AM
9255 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9256 {
9257 if (local_toc_syms)
4eca0228 9258 _bfd_error_handler
df22d223
AM
9259 (_("%s defined on removed toc entry"),
9260 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9261 do
9262 ++i;
9263 while ((skip[i] & (ref_from_discarded | can_optimize)));
9264 sym->st_value = (bfd_vma) i << 3;
9265 }
d62b3684 9266
df22d223
AM
9267 sym->st_value -= skip[i];
9268 symtab_hdr->contents = (unsigned char *) local_syms;
9269 }
c5614fa4 9270
854b41e7 9271 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9272 if (toc_inf.global_toc_syms)
9273 {
9274 toc_inf.toc = toc;
9275 toc_inf.skip = skip;
0a1b45a2 9276 toc_inf.global_toc_syms = false;
c5614fa4
AM
9277 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9278 &toc_inf);
9279 }
854b41e7
AM
9280
9281 if (toc->reloc_count != 0)
9282 {
d4730f92 9283 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9284 Elf_Internal_Rela *wrel;
9285 bfd_size_type sz;
9286
854b41e7 9287 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9288 if (toc_relocs == NULL)
9289 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9290 info->keep_memory);
9291 if (toc_relocs == NULL)
9292 goto error_ret;
9293
425b145b
AM
9294 wrel = toc_relocs;
9295 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9296 if ((skip[rel->r_offset >> 3]
9297 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9298 {
9299 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9300 wrel->r_info = rel->r_info;
9301 wrel->r_addend = rel->r_addend;
9302 ++wrel;
9303 }
9304 else if (!dec_dynrel_count (rel->r_info, toc, info,
9305 &local_syms, NULL, NULL))
9306 goto error_ret;
9307
425b145b
AM
9308 elf_section_data (toc)->relocs = toc_relocs;
9309 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9310 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9311 sz = rel_hdr->sh_entsize;
9312 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9313 }
c5614fa4 9314 }
c9594989 9315 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9316 free (toc_relocs);
c5614fa4
AM
9317
9318 if (local_syms != NULL
9319 && symtab_hdr->contents != (unsigned char *) local_syms)
9320 {
9321 if (!info->keep_memory)
9322 free (local_syms);
9323 else
9324 symtab_hdr->contents = (unsigned char *) local_syms;
9325 }
9326 free (skip);
9327 }
9328
066f4018 9329 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9330 a GOT relative or PC relative access, possibly reducing the
9331 number of GOT entries. */
066f4018
AM
9332 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9333 {
9334 asection *sec;
9335 Elf_Internal_Shdr *symtab_hdr;
9336 Elf_Internal_Sym *local_syms;
9337 Elf_Internal_Rela *relstart, *rel;
9338 bfd_vma got;
9339
9340 if (!is_ppc64_elf (ibfd))
9341 continue;
9342
903b777d 9343 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9344 continue;
9345
9346 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9347 got = 0;
9348 if (sec != NULL)
9349 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9350
9351 local_syms = NULL;
9352 symtab_hdr = &elf_symtab_hdr (ibfd);
9353
9354 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9355 {
9356 if (sec->reloc_count == 0
903b777d 9357 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9358 || discarded_section (sec))
9359 continue;
9360
9361 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9362 info->keep_memory);
9363 if (relstart == NULL)
9364 {
9365 got_error_ret:
c9594989 9366 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9367 free (local_syms);
9368 if (sec != NULL
066f4018
AM
9369 && elf_section_data (sec)->relocs != relstart)
9370 free (relstart);
0a1b45a2 9371 return false;
066f4018
AM
9372 }
9373
9374 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9375 {
9376 enum elf_ppc64_reloc_type r_type;
9377 unsigned long r_symndx;
9378 Elf_Internal_Sym *sym;
9379 asection *sym_sec;
9380 struct elf_link_hash_entry *h;
9381 struct got_entry *ent;
133a1f60 9382 bfd_vma val, pc;
4a421c53 9383 unsigned char buf[8];
066f4018 9384 unsigned int insn;
903b777d 9385 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9386
9387 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9388 switch (r_type)
9389 {
9390 default:
9391 insn_check = no_check;
9392 break;
9393
9394 case R_PPC64_PLT16_HA:
9395 case R_PPC64_GOT_TLSLD16_HA:
9396 case R_PPC64_GOT_TLSGD16_HA:
9397 case R_PPC64_GOT_TPREL16_HA:
9398 case R_PPC64_GOT_DTPREL16_HA:
9399 case R_PPC64_GOT16_HA:
9400 case R_PPC64_TOC16_HA:
9401 insn_check = check_ha;
9402 break;
9403
9404 case R_PPC64_PLT16_LO:
9405 case R_PPC64_PLT16_LO_DS:
9406 case R_PPC64_GOT_TLSLD16_LO:
9407 case R_PPC64_GOT_TLSGD16_LO:
9408 case R_PPC64_GOT_TPREL16_LO_DS:
9409 case R_PPC64_GOT_DTPREL16_LO_DS:
9410 case R_PPC64_GOT16_LO:
9411 case R_PPC64_GOT16_LO_DS:
9412 case R_PPC64_TOC16_LO:
9413 case R_PPC64_TOC16_LO_DS:
9414 insn_check = check_lo;
9415 break;
9416 }
9417
9418 if (insn_check != no_check)
9419 {
9420 bfd_vma off = rel->r_offset & ~3;
9421
9422 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9423 goto got_error_ret;
9424
9425 insn = bfd_get_32 (ibfd, buf);
9426 if (insn_check == check_lo
9427 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9428 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9429 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9430 {
9431 char str[12];
9432
9433 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9434 sprintf (str, "%#08x", insn);
9435 info->callbacks->einfo
9436 /* xgettext:c-format */
9437 (_("%H: got/toc optimization is not supported for"
9438 " %s instruction\n"),
9439 ibfd, sec, rel->r_offset & ~3, str);
9440 continue;
9441 }
9442 }
9443
066f4018
AM
9444 switch (r_type)
9445 {
bb22a418
AM
9446 /* Note that we don't delete GOT entries for
9447 R_PPC64_GOT16_DS since we'd need a lot more
9448 analysis. For starters, the preliminary layout is
9449 before the GOT, PLT, dynamic sections and stubs are
9450 laid out. Then we'd need to allow for changes in
9451 distance between sections caused by alignment. */
066f4018
AM
9452 default:
9453 continue;
9454
066f4018
AM
9455 case R_PPC64_GOT16_HA:
9456 case R_PPC64_GOT16_LO_DS:
4a421c53 9457 case R_PPC64_GOT_PCREL34:
066f4018
AM
9458 break;
9459 }
9460
9461 r_symndx = ELF64_R_SYM (rel->r_info);
9462 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9463 r_symndx, ibfd))
9464 goto got_error_ret;
9465
6d5554a6
AM
9466 if (sym_sec == NULL
9467 || sym_sec->output_section == NULL
9468 || discarded_section (sym_sec))
9469 continue;
9470
06507dab
AM
9471 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9472 continue;
9473
066f4018
AM
9474 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9475 continue;
9476
9477 if (h != NULL)
9478 val = h->root.u.def.value;
9479 else
9480 val = sym->st_value;
133a1f60 9481 val += rel->r_addend;
066f4018
AM
9482 val += sym_sec->output_section->vma + sym_sec->output_offset;
9483
bb22a418
AM
9484/* Fudge factor to allow for the fact that the preliminary layout
9485 isn't exact. Reduce limits by this factor. */
9486#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9487
066f4018
AM
9488 switch (r_type)
9489 {
9490 default:
9491 continue;
9492
066f4018 9493 case R_PPC64_GOT16_HA:
bb22a418
AM
9494 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9495 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9496 continue;
9497
9498 if (!bfd_get_section_contents (ibfd, sec, buf,
9499 rel->r_offset & ~3, 4))
9500 goto got_error_ret;
9501 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9502 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9503 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9504 continue;
9505 break;
9506
9507 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9508 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9509 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9510 continue;
9511 if (!bfd_get_section_contents (ibfd, sec, buf,
9512 rel->r_offset & ~3, 4))
9513 goto got_error_ret;
9514 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9515 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9516 continue;
9517 break;
4a421c53
AM
9518
9519 case R_PPC64_GOT_PCREL34:
9520 pc = rel->r_offset;
9521 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9522 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9523 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9524 continue;
9525 if (!bfd_get_section_contents (ibfd, sec, buf,
9526 rel->r_offset & ~3, 8))
9527 goto got_error_ret;
9528 insn = bfd_get_32 (ibfd, buf);
9529 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9530 continue;
9531 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9532 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9533 continue;
9534 break;
066f4018 9535 }
bb22a418 9536#undef LIMIT_ADJUST
066f4018
AM
9537
9538 if (h != NULL)
9539 ent = h->got.glist;
9540 else
9541 {
9542 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9543 ent = local_got_ents[r_symndx];
9544 }
9545 for (; ent != NULL; ent = ent->next)
133a1f60 9546 if (ent->addend == rel->r_addend
066f4018
AM
9547 && ent->owner == ibfd
9548 && ent->tls_type == 0)
9549 break;
9550 BFD_ASSERT (ent && ent->got.refcount > 0);
9551 ent->got.refcount -= 1;
9552 }
9553
9554 if (elf_section_data (sec)->relocs != relstart)
9555 free (relstart);
9556 }
9557
9558 if (local_syms != NULL
9559 && symtab_hdr->contents != (unsigned char *) local_syms)
9560 {
9561 if (!info->keep_memory)
9562 free (local_syms);
9563 else
9564 symtab_hdr->contents = (unsigned char *) local_syms;
9565 }
9566 }
9567
0a1b45a2 9568 return true;
c5614fa4
AM
9569}
9570
1bbe0902
AM
9571/* Return true iff input section I references the TOC using
9572 instructions limited to +/-32k offsets. */
9573
0a1b45a2 9574bool
1bbe0902
AM
9575ppc64_elf_has_small_toc_reloc (asection *i)
9576{
9577 return (is_ppc64_elf (i->owner)
9578 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9579}
9580
927be08e
AM
9581/* Allocate space for one GOT entry. */
9582
9583static void
9584allocate_got (struct elf_link_hash_entry *h,
9585 struct bfd_link_info *info,
9586 struct got_entry *gent)
9587{
9588 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9589 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9590 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9591 ? 16 : 8);
9592 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9593 ? 2 : 1) * sizeof (Elf64_External_Rela);
9594 asection *got = ppc64_elf_tdata (gent->owner)->got;
9595
9596 gent->got.offset = got->size;
9597 got->size += entsize;
9598
19e08130 9599 if (h->type == STT_GNU_IFUNC)
927be08e 9600 {
33e44f2e 9601 htab->elf.irelplt->size += rentsize;
19e08130 9602 htab->got_reli_size += rentsize;
927be08e 9603 }
f15d0b54 9604 else if (((bfd_link_pic (info)
f749f26e 9605 && !(gent->tls_type != 0
f15d0b54
AM
9606 && bfd_link_executable (info)
9607 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9608 || (htab->elf.dynamic_sections_created
9609 && h->dynindx != -1
9610 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9611 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9612 {
19e08130 9613 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9614 relgot->size += rentsize;
927be08e
AM
9615 }
9616}
9617
7865406b
AM
9618/* This function merges got entries in the same toc group. */
9619
9620static void
9621merge_got_entries (struct got_entry **pent)
9622{
9623 struct got_entry *ent, *ent2;
9624
9625 for (ent = *pent; ent != NULL; ent = ent->next)
9626 if (!ent->is_indirect)
9627 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9628 if (!ent2->is_indirect
9629 && ent2->addend == ent->addend
9630 && ent2->tls_type == ent->tls_type
9631 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9632 {
0a1b45a2 9633 ent2->is_indirect = true;
7865406b
AM
9634 ent2->got.ent = ent;
9635 }
9636}
9637
46434633 9638/* If H is undefined, make it dynamic if that makes sense. */
f0158f44 9639
0a1b45a2 9640static bool
46434633
AM
9641ensure_undef_dynamic (struct bfd_link_info *info,
9642 struct elf_link_hash_entry *h)
f0158f44
AM
9643{
9644 struct elf_link_hash_table *htab = elf_hash_table (info);
9645
9646 if (htab->dynamic_sections_created
46434633
AM
9647 && ((info->dynamic_undefined_weak != 0
9648 && h->root.type == bfd_link_hash_undefweak)
9649 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9650 && h->dynindx == -1
9651 && !h->forced_local
9652 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9653 return bfd_elf_link_record_dynamic_symbol (info, h);
0a1b45a2 9654 return true;
f0158f44
AM
9655}
9656
30845f11
AM
9657/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9658 usual htab->elf.splt section for a PLT entry. */
9659
9660static inline
0a1b45a2 9661bool use_local_plt (struct bfd_link_info *info,
30845f11
AM
9662 struct elf_link_hash_entry *h)
9663{
9664 return (h == NULL
9665 || h->dynindx == -1
9666 || !elf_hash_table (info)->dynamic_sections_created);
9667}
9668
65f38f15
AM
9669/* Allocate space in .plt, .got and associated reloc sections for
9670 dynamic relocs. */
5bd4f169 9671
0a1b45a2 9672static bool
4ce794b7 9673allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9674{
65f38f15
AM
9675 struct bfd_link_info *info;
9676 struct ppc_link_hash_table *htab;
5bd4f169 9677 asection *s;
65f38f15 9678 struct ppc_link_hash_entry *eh;
0b8bcf0d 9679 struct got_entry **pgent, *gent;
5bd4f169 9680
e92d460e 9681 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9682 return true;
5bd4f169 9683
65f38f15
AM
9684 info = (struct bfd_link_info *) inf;
9685 htab = ppc_hash_table (info);
4dfe6ac6 9686 if (htab == NULL)
0a1b45a2 9687 return false;
5bd4f169 9688
ed7007c1 9689 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9690 /* Run through the TLS GD got entries first if we're changing them
9691 to TPREL. */
b00a0a86 9692 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9693 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9694 if (gent->got.refcount > 0
9695 && (gent->tls_type & TLS_GD) != 0)
9696 {
9697 /* This was a GD entry that has been converted to TPREL. If
9698 there happens to be a TPREL entry we can use that one. */
9699 struct got_entry *ent;
9700 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9701 if (ent->got.refcount > 0
9702 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9703 && ent->addend == gent->addend
9704 && ent->owner == gent->owner)
951fd09b
AM
9705 {
9706 gent->got.refcount = 0;
9707 break;
9708 }
9709
9710 /* If not, then we'll be using our own TPREL entry. */
9711 if (gent->got.refcount != 0)
9712 gent->tls_type = TLS_TLS | TLS_TPREL;
9713 }
9714
7865406b
AM
9715 /* Remove any list entry that won't generate a word in the GOT before
9716 we call merge_got_entries. Otherwise we risk merging to empty
9717 entries. */
0b8bcf0d
AM
9718 pgent = &h->got.glist;
9719 while ((gent = *pgent) != NULL)
411e1bfb 9720 if (gent->got.refcount > 0)
7865406b
AM
9721 {
9722 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9723 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9724 {
9725 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9726 *pgent = gent->next;
9727 }
9728 else
9729 pgent = &gent->next;
9730 }
9731 else
9732 *pgent = gent->next;
9733
9734 if (!htab->do_multi_toc)
9735 merge_got_entries (&h->got.glist);
9736
9737 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9738 if (!gent->is_indirect)
411e1bfb 9739 {
ec73ddcd
AM
9740 /* Ensure we catch all the cases where this symbol should
9741 be made dynamic. */
46434633 9742 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9743 return false;
65f38f15 9744
0c8d6e5c 9745 if (!is_ppc64_elf (gent->owner))
927be08e 9746 abort ();
0ffa91dd 9747
927be08e 9748 allocate_got (h, info, gent);
411e1bfb 9749 }
65f38f15 9750
954b63d4
AM
9751 /* If no dynamic sections we can't have dynamic relocs, except for
9752 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9753 if (!htab->elf.dynamic_sections_created
9754 && h->type != STT_GNU_IFUNC)
190eb1dd 9755 h->dyn_relocs = NULL;
8a2058b5 9756
529fe20e
AM
9757 /* Discard relocs on undefined symbols that must be local. */
9758 else if (h->root.type == bfd_link_hash_undefined
9759 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9760 h->dyn_relocs = NULL;
529fe20e 9761
954b63d4
AM
9762 /* Also discard relocs on undefined weak syms with non-default
9763 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9764 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9765 h->dyn_relocs = NULL;
954b63d4 9766
190eb1dd 9767 if (h->dyn_relocs != NULL)
65f38f15 9768 {
8a2058b5
AM
9769 struct elf_dyn_relocs *p, **pp;
9770
57e7d118
AM
9771 /* In the shared -Bsymbolic case, discard space allocated for
9772 dynamic pc-relative relocs against symbols which turn out to
9773 be defined in regular objects. For the normal shared case,
9774 discard space for relocs that have become local due to symbol
9775 visibility changes. */
57e7d118 9776 if (bfd_link_pic (info))
65f38f15 9777 {
57e7d118
AM
9778 /* Relocs that use pc_count are those that appear on a call
9779 insn, or certain REL relocs (see must_be_dyn_reloc) that
9780 can be generated via assembly. We want calls to
9781 protected symbols to resolve directly to the function
9782 rather than going via the plt. If people want function
9783 pointer comparisons to work as expected then they should
9784 avoid writing weird assembly. */
9785 if (SYMBOL_CALLS_LOCAL (info, h))
9786 {
190eb1dd 9787 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
57e7d118
AM
9788 {
9789 p->count -= p->pc_count;
9790 p->pc_count = 0;
9791 if (p->count == 0)
9792 *pp = p->next;
9793 else
9794 pp = &p->next;
9795 }
9796 }
65f38f15 9797
190eb1dd 9798 if (h->dyn_relocs != NULL)
5bd4f169 9799 {
ec73ddcd
AM
9800 /* Ensure we catch all the cases where this symbol
9801 should be made dynamic. */
46434633 9802 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9803 return false;
5bd4f169 9804 }
65f38f15 9805 }
ec73ddcd
AM
9806
9807 /* For a fixed position executable, discard space for
9808 relocs against symbols which are not dynamic. */
9809 else if (h->type != STT_GNU_IFUNC)
57e7d118 9810 {
49160308
AM
9811 if ((h->dynamic_adjusted
9812 || (h->ref_regular
9813 && h->root.type == bfd_link_hash_undefweak
9814 && (info->dynamic_undefined_weak > 0
9815 || !_bfd_elf_readonly_dynrelocs (h))))
529fe20e
AM
9816 && !h->def_regular
9817 && !ELF_COMMON_DEF_P (h))
f0158f44 9818 {
ec73ddcd
AM
9819 /* Ensure we catch all the cases where this symbol
9820 should be made dynamic. */
46434633 9821 if (!ensure_undef_dynamic (info, h))
0a1b45a2 9822 return false;
dfbb6ac9 9823
ec73ddcd 9824 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 9825 if (h->dynindx == -1)
190eb1dd 9826 h->dyn_relocs = NULL;
f0158f44
AM
9827 }
9828 else
190eb1dd 9829 h->dyn_relocs = NULL;
57e7d118
AM
9830 }
9831
9832 /* Finally, allocate space. */
190eb1dd 9833 for (p = h->dyn_relocs; p != NULL; p = p->next)
57e7d118
AM
9834 {
9835 asection *sreloc = elf_section_data (p->sec)->sreloc;
9836 if (eh->elf.type == STT_GNU_IFUNC)
9837 sreloc = htab->elf.irelplt;
9838 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9839 }
65f38f15 9840 }
57e7d118 9841
2d7ad24e
AM
9842 /* We might need a PLT entry when the symbol
9843 a) is dynamic, or
9844 b) is an ifunc, or
9845 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9846 d) has plt16 relocs and we are linking statically. */
9847 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9848 || h->type == STT_GNU_IFUNC
9849 || (h->needs_plt && h->dynamic_adjusted)
9850 || (h->needs_plt
9851 && h->def_regular
9852 && !htab->elf.dynamic_sections_created
3e04d765 9853 && !htab->can_convert_all_inline_plt
ed7007c1 9854 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 9855 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9856 {
57e7d118 9857 struct plt_entry *pent;
0a1b45a2 9858 bool doneone = false;
57e7d118
AM
9859 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9860 if (pent->plt.refcount > 0)
9861 {
b2936612
AM
9862 if (!ensure_undef_dynamic (info, h))
9863 return false;
9864
30845f11 9865 if (use_local_plt (info, h))
57e7d118 9866 {
2d7ad24e
AM
9867 if (h->type == STT_GNU_IFUNC)
9868 {
9869 s = htab->elf.iplt;
9870 pent->plt.offset = s->size;
9871 s->size += PLT_ENTRY_SIZE (htab);
9872 s = htab->elf.irelplt;
9873 }
9874 else
9875 {
9876 s = htab->pltlocal;
9877 pent->plt.offset = s->size;
9878 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9879 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9880 }
57e7d118
AM
9881 }
9882 else
9883 {
9884 /* If this is the first .plt entry, make room for the special
9885 first entry. */
9886 s = htab->elf.splt;
9887 if (s->size == 0)
9888 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9889
57e7d118 9890 pent->plt.offset = s->size;
65f38f15 9891
57e7d118
AM
9892 /* Make room for this entry. */
9893 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9894
57e7d118
AM
9895 /* Make room for the .glink code. */
9896 s = htab->glink;
9897 if (s->size == 0)
9e390558 9898 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9899 if (htab->opd_abi)
9900 {
9901 /* We need bigger stubs past index 32767. */
9e390558 9902 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9903 s->size += 4;
9904 s->size += 2*4;
9905 }
9906 else
9907 s->size += 4;
65f38f15 9908
57e7d118
AM
9909 /* We also need to make an entry in the .rela.plt section. */
9910 s = htab->elf.srelplt;
9911 }
2d7ad24e
AM
9912 if (s != NULL)
9913 s->size += sizeof (Elf64_External_Rela);
0a1b45a2 9914 doneone = true;
57e7d118
AM
9915 }
9916 else
9917 pent->plt.offset = (bfd_vma) -1;
9918 if (!doneone)
9919 {
9920 h->plt.plist = NULL;
9921 h->needs_plt = 0;
9922 }
65f38f15 9923 }
57e7d118 9924 else
65f38f15 9925 {
57e7d118
AM
9926 h->plt.plist = NULL;
9927 h->needs_plt = 0;
65f38f15
AM
9928 }
9929
0a1b45a2 9930 return true;
65f38f15
AM
9931}
9932
9e390558
AM
9933#define PPC_LO(v) ((v) & 0xffff)
9934#define PPC_HI(v) (((v) >> 16) & 0xffff)
9935#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9936#define D34(v) \
9937 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9938#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9939
a345bc8d
AM
9940/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9941 to set up space for global entry stubs. These are put in glink,
9942 after the branch table. */
65f38f15 9943
0a1b45a2 9944static bool
a345bc8d 9945size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9946{
a345bc8d
AM
9947 struct bfd_link_info *info;
9948 struct ppc_link_hash_table *htab;
9949 struct plt_entry *pent;
9e390558 9950 asection *s, *plt;
65f38f15 9951
a345bc8d 9952 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9953 return true;
65f38f15 9954
a345bc8d 9955 if (!h->pointer_equality_needed)
0a1b45a2 9956 return true;
65f38f15 9957
a345bc8d 9958 if (h->def_regular)
0a1b45a2 9959 return true;
65f38f15 9960
a345bc8d
AM
9961 info = inf;
9962 htab = ppc_hash_table (info);
9963 if (htab == NULL)
0a1b45a2 9964 return false;
a345bc8d 9965
9e390558
AM
9966 s = htab->global_entry;
9967 plt = htab->elf.splt;
a345bc8d
AM
9968 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9969 if (pent->plt.offset != (bfd_vma) -1
9970 && pent->addend == 0)
9971 {
afe397ea
AM
9972 /* For ELFv2, if this symbol is not defined in a regular file
9973 and we are not generating a shared library or pie, then we
9974 need to define the symbol in the executable on a call stub.
9975 This is to avoid text relocations. */
9e390558
AM
9976 bfd_vma off, stub_align, stub_off, stub_size;
9977 unsigned int align_power;
9978
9979 stub_size = 16;
9980 stub_off = s->size;
9981 if (htab->params->plt_stub_align >= 0)
9982 align_power = htab->params->plt_stub_align;
9983 else
9984 align_power = -htab->params->plt_stub_align;
9985 /* Setting section alignment is delayed until we know it is
9986 non-empty. Otherwise the .text output section will be
9987 aligned at least to plt_stub_align even when no global
9988 entry stubs are needed. */
9989 if (s->alignment_power < align_power)
9990 s->alignment_power = align_power;
9991 stub_align = (bfd_vma) 1 << align_power;
9992 if (htab->params->plt_stub_align >= 0
9993 || ((((stub_off + stub_size - 1) & -stub_align)
9994 - (stub_off & -stub_align))
9995 > ((stub_size - 1) & -stub_align)))
9996 stub_off = (stub_off + stub_align - 1) & -stub_align;
9997 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9998 off -= stub_off + s->output_offset + s->output_section->vma;
9999 /* Note that for --plt-stub-align negative we have a possible
10000 dependency between stub offset and size. Break that
10001 dependency by assuming the max stub size when calculating
10002 the stub offset. */
10003 if (PPC_HA (off) == 0)
10004 stub_size -= 4;
8a2058b5 10005 h->root.type = bfd_link_hash_defined;
afe397ea 10006 h->root.u.def.section = s;
9e390558
AM
10007 h->root.u.def.value = stub_off;
10008 s->size = stub_off + stub_size;
a345bc8d
AM
10009 break;
10010 }
0a1b45a2 10011 return true;
a345bc8d
AM
10012}
10013
65f38f15
AM
10014/* Set the sizes of the dynamic sections. */
10015
0a1b45a2 10016static bool
ee67d69a 10017ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 10018 struct bfd_link_info *info)
65f38f15
AM
10019{
10020 struct ppc_link_hash_table *htab;
10021 bfd *dynobj;
10022 asection *s;
0a1b45a2 10023 bool relocs;
65f38f15 10024 bfd *ibfd;
7865406b 10025 struct got_entry *first_tlsld;
65f38f15
AM
10026
10027 htab = ppc_hash_table (info);
4dfe6ac6 10028 if (htab == NULL)
0a1b45a2 10029 return false;
4dfe6ac6 10030
65f38f15
AM
10031 dynobj = htab->elf.dynobj;
10032 if (dynobj == NULL)
10033 abort ();
10034
10035 if (htab->elf.dynamic_sections_created)
10036 {
10037 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10038 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10039 {
3d4d4302 10040 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10041 if (s == NULL)
10042 abort ();
eea6121a 10043 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10044 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10045 }
10046 }
10047
10048 /* Set up .got offsets for local syms, and space for local dynamic
10049 relocs. */
c72f2fb2 10050 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10051 {
411e1bfb
AM
10052 struct got_entry **lgot_ents;
10053 struct got_entry **end_lgot_ents;
e054468f
AM
10054 struct plt_entry **local_plt;
10055 struct plt_entry **end_local_plt;
f961d9dd 10056 unsigned char *lgot_masks;
65f38f15
AM
10057 bfd_size_type locsymcount;
10058 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10059
0c8d6e5c 10060 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10061 continue;
10062
10063 for (s = ibfd->sections; s != NULL; s = s->next)
10064 {
19e08130 10065 struct ppc_dyn_relocs *p;
65f38f15 10066
6edfbbad 10067 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10068 {
ec338859
AM
10069 if (!bfd_is_abs_section (p->sec)
10070 && bfd_is_abs_section (p->sec->output_section))
10071 {
10072 /* Input section has been discarded, either because
10073 it is a copy of a linkonce section or due to
10074 linker script /DISCARD/, so we'll be discarding
10075 the relocs too. */
10076 }
248866a8 10077 else if (p->count != 0)
ec338859 10078 {
19e08130
AM
10079 asection *srel = elf_section_data (p->sec)->sreloc;
10080 if (p->ifunc)
33e44f2e 10081 srel = htab->elf.irelplt;
eea6121a 10082 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10083 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10084 info->flags |= DF_TEXTREL;
ec338859 10085 }
65f38f15
AM
10086 }
10087 }
10088
411e1bfb
AM
10089 lgot_ents = elf_local_got_ents (ibfd);
10090 if (!lgot_ents)
65f38f15
AM
10091 continue;
10092
0ffa91dd 10093 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10094 locsymcount = symtab_hdr->sh_info;
411e1bfb 10095 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10096 local_plt = (struct plt_entry **) end_lgot_ents;
10097 end_local_plt = local_plt + locsymcount;
f961d9dd 10098 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10099 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10100 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10101 {
0b8bcf0d 10102 struct got_entry **pent, *ent;
411e1bfb 10103
0b8bcf0d
AM
10104 pent = lgot_ents;
10105 while ((ent = *pent) != NULL)
411e1bfb
AM
10106 if (ent->got.refcount > 0)
10107 {
e7b938ca 10108 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10109 {
927be08e 10110 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10111 *pent = ent->next;
411e1bfb
AM
10112 }
10113 else
10114 {
19e08130
AM
10115 unsigned int ent_size = 8;
10116 unsigned int rel_size = sizeof (Elf64_External_Rela);
10117
eea6121a 10118 ent->got.offset = s->size;
e7b938ca 10119 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10120 {
19e08130
AM
10121 ent_size *= 2;
10122 rel_size *= 2;
10123 }
10124 s->size += ent_size;
37da22e5 10125 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10126 {
33e44f2e 10127 htab->elf.irelplt->size += rel_size;
19e08130
AM
10128 htab->got_reli_size += rel_size;
10129 }
93370e8e
AM
10130 else if (bfd_link_pic (info)
10131 && !(ent->tls_type != 0
10132 && bfd_link_executable (info)))
19e08130
AM
10133 {
10134 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10135 srel->size += rel_size;
927be08e 10136 }
0b8bcf0d 10137 pent = &ent->next;
411e1bfb
AM
10138 }
10139 }
10140 else
0b8bcf0d 10141 *pent = ent->next;
65f38f15 10142 }
e054468f 10143
2d7ad24e
AM
10144 /* Allocate space for plt calls to local syms. */
10145 lgot_masks = (unsigned char *) end_local_plt;
10146 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10147 {
10148 struct plt_entry *ent;
10149
10150 for (ent = *local_plt; ent != NULL; ent = ent->next)
10151 if (ent->plt.refcount > 0)
10152 {
2d7ad24e
AM
10153 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10154 {
10155 s = htab->elf.iplt;
10156 ent->plt.offset = s->size;
10157 s->size += PLT_ENTRY_SIZE (htab);
10158 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10159 }
3e04d765
AM
10160 else if (htab->can_convert_all_inline_plt
10161 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10162 ent->plt.offset = (bfd_vma) -1;
10163 else
10164 {
10165 s = htab->pltlocal;
10166 ent->plt.offset = s->size;
10167 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10168 if (bfd_link_pic (info))
10169 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10170 }
e054468f
AM
10171 }
10172 else
10173 ent->plt.offset = (bfd_vma) -1;
10174 }
65f38f15
AM
10175 }
10176
10177 /* Allocate global sym .plt and .got entries, and space for global
10178 sym dynamic relocs. */
4ce794b7 10179 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10180
0e1862bb 10181 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10182 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10183
7865406b 10184 first_tlsld = NULL;
c72f2fb2 10185 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10186 {
7865406b
AM
10187 struct got_entry *ent;
10188
0c8d6e5c 10189 if (!is_ppc64_elf (ibfd))
102890f0
AM
10190 continue;
10191
7865406b
AM
10192 ent = ppc64_tlsld_got (ibfd);
10193 if (ent->got.refcount > 0)
102890f0 10194 {
7865406b 10195 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10196 {
0a1b45a2 10197 ent->is_indirect = true;
7865406b
AM
10198 ent->got.ent = first_tlsld;
10199 }
10200 else
10201 {
10202 if (first_tlsld == NULL)
10203 first_tlsld = ent;
10204 s = ppc64_elf_tdata (ibfd)->got;
10205 ent->got.offset = s->size;
10206 ent->owner = ibfd;
10207 s->size += 16;
f749f26e 10208 if (bfd_link_dll (info))
7865406b
AM
10209 {
10210 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10211 srel->size += sizeof (Elf64_External_Rela);
10212 }
102890f0
AM
10213 }
10214 }
10215 else
7865406b 10216 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10217 }
10218
65f38f15
AM
10219 /* We now have determined the sizes of the various dynamic sections.
10220 Allocate memory for them. */
0a1b45a2 10221 relocs = false;
65f38f15
AM
10222 for (s = dynobj->sections; s != NULL; s = s->next)
10223 {
10224 if ((s->flags & SEC_LINKER_CREATED) == 0)
10225 continue;
10226
4ce794b7 10227 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10228 /* These haven't been allocated yet; don't strip. */
10229 continue;
33e44f2e
AM
10230 else if (s == htab->elf.sgot
10231 || s == htab->elf.splt
10232 || s == htab->elf.iplt
2d7ad24e 10233 || s == htab->pltlocal
c456f082 10234 || s == htab->glink
9e390558 10235 || s == htab->global_entry
5474d94f
AM
10236 || s == htab->elf.sdynbss
10237 || s == htab->elf.sdynrelro)
65f38f15
AM
10238 {
10239 /* Strip this section if we don't need it; see the
10240 comment below. */
5bd4f169 10241 }
58d180e8
AM
10242 else if (s == htab->glink_eh_frame)
10243 {
10244 if (!bfd_is_abs_section (s->output_section))
10245 /* Not sized yet. */
10246 continue;
10247 }
08dedd66 10248 else if (startswith (s->name, ".rela"))
5bd4f169 10249 {
c456f082 10250 if (s->size != 0)
5bd4f169 10251 {
33e44f2e 10252 if (s != htab->elf.srelplt)
0a1b45a2 10253 relocs = true;
5bd4f169
AM
10254
10255 /* We use the reloc_count field as a counter if we need
10256 to copy relocs into the output file. */
10257 s->reloc_count = 0;
10258 }
10259 }
65f38f15 10260 else
5bd4f169
AM
10261 {
10262 /* It's not one of our sections, so don't allocate space. */
10263 continue;
10264 }
10265
eea6121a 10266 if (s->size == 0)
5bd4f169 10267 {
c456f082
AM
10268 /* If we don't need this section, strip it from the
10269 output file. This is mostly to handle .rela.bss and
10270 .rela.plt. We must create both sections in
10271 create_dynamic_sections, because they must be created
10272 before the linker maps input sections to output
10273 sections. The linker does that before
10274 adjust_dynamic_symbol is called, and it is that
10275 function which decides whether anything needs to go
10276 into these sections. */
8423293d 10277 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10278 continue;
10279 }
10280
06bcf541
AM
10281 if (bfd_is_abs_section (s->output_section))
10282 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10283 s->name);
10284
c456f082 10285 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10286 continue;
10287
65f38f15
AM
10288 /* Allocate memory for the section contents. We use bfd_zalloc
10289 here in case unused entries are not reclaimed before the
10290 section's contents are written out. This should not happen,
411e1bfb
AM
10291 but this way if it does we get a R_PPC64_NONE reloc in .rela
10292 sections instead of garbage.
10293 We also rely on the section contents being zero when writing
5474d94f 10294 the GOT and .dynrelro. */
eea6121a 10295 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10296 if (s->contents == NULL)
0a1b45a2 10297 return false;
5bd4f169
AM
10298 }
10299
c72f2fb2 10300 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10301 {
0c8d6e5c 10302 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10303 continue;
10304
e717da7e 10305 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10306 if (s != NULL && s != htab->elf.sgot)
e717da7e 10307 {
eea6121a 10308 if (s->size == 0)
8423293d 10309 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10310 else
10311 {
eea6121a 10312 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10313 if (s->contents == NULL)
0a1b45a2 10314 return false;
e717da7e
AM
10315 }
10316 }
10317 s = ppc64_elf_tdata (ibfd)->relgot;
10318 if (s != NULL)
10319 {
eea6121a 10320 if (s->size == 0)
8423293d 10321 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10322 else
10323 {
eea6121a 10324 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e 10325 if (s->contents == NULL)
0a1b45a2
AM
10326 return false;
10327 relocs = true;
e717da7e
AM
10328 s->reloc_count = 0;
10329 }
10330 }
10331 }
10332
e86ce104 10333 if (htab->elf.dynamic_sections_created)
5bd4f169 10334 {
0a1b45a2 10335 bool tls_opt;
e8910a83 10336
5bd4f169
AM
10337 /* Add some entries to the .dynamic section. We fill in the
10338 values later, in ppc64_elf_finish_dynamic_sections, but we
10339 must add the entries now so that we get the correct size for
10340 the .dynamic section. The DT_DEBUG entry is filled in by the
10341 dynamic linker and used by the debugger. */
dc810e39 10342#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10343 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10344
0e1862bb 10345 if (bfd_link_executable (info))
5bd4f169 10346 {
dc810e39 10347 if (!add_dynamic_entry (DT_DEBUG, 0))
0a1b45a2 10348 return false;
5bd4f169
AM
10349 }
10350
33e44f2e 10351 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10352 {
dc810e39
AM
10353 if (!add_dynamic_entry (DT_PLTGOT, 0)
10354 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10355 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10356 || !add_dynamic_entry (DT_JMPREL, 0)
10357 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
0a1b45a2 10358 return false;
5bd4f169
AM
10359 }
10360
ee67d69a 10361 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10362 {
10363 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10364 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
0a1b45a2 10365 return false;
19397422
AM
10366 }
10367
7c9cf415 10368 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10369 && ((htab->tls_get_addr_fd != NULL
10370 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10371 || (htab->tga_desc_fd != NULL
10372 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10373 if (tls_opt || !htab->opd_abi)
10374 {
10375 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
0a1b45a2 10376 return false;
e8910a83 10377 }
a7f2871e 10378
5bd4f169
AM
10379 if (relocs)
10380 {
dc810e39
AM
10381 if (!add_dynamic_entry (DT_RELA, 0)
10382 || !add_dynamic_entry (DT_RELASZ, 0)
10383 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
0a1b45a2 10384 return false;
5bd4f169 10385
65f38f15
AM
10386 /* If any dynamic relocs apply to a read-only section,
10387 then we need a DT_TEXTREL entry. */
248866a8 10388 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10389 elf_link_hash_traverse (&htab->elf,
10390 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10391
65f38f15 10392 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10393 {
65f38f15 10394 if (!add_dynamic_entry (DT_TEXTREL, 0))
0a1b45a2 10395 return false;
5bd4f169 10396 }
5bd4f169 10397 }
5bd4f169 10398 }
65f38f15 10399#undef add_dynamic_entry
5bd4f169 10400
0a1b45a2 10401 return true;
5bd4f169
AM
10402}
10403
a345bc8d
AM
10404/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10405
0a1b45a2 10406static bool
a345bc8d
AM
10407ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10408{
10409 if (h->plt.plist != NULL
10410 && !h->def_regular
10411 && !h->pointer_equality_needed)
0a1b45a2 10412 return false;
a345bc8d
AM
10413
10414 return _bfd_elf_hash_symbol (h);
10415}
10416
721956f4 10417/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10418
4ce794b7
AM
10419static inline enum ppc_stub_type
10420ppc_type_of_stub (asection *input_sec,
10421 const Elf_Internal_Rela *rel,
10422 struct ppc_link_hash_entry **hash,
e054468f 10423 struct plt_entry **plt_ent,
6911b7dc
AM
10424 bfd_vma destination,
10425 unsigned long local_off)
5bd4f169 10426{
721956f4
AM
10427 struct ppc_link_hash_entry *h = *hash;
10428 bfd_vma location;
10429 bfd_vma branch_offset;
10430 bfd_vma max_branch_offset;
4ce794b7 10431 enum elf_ppc64_reloc_type r_type;
5bd4f169 10432
721956f4
AM
10433 if (h != NULL)
10434 {
e054468f 10435 struct plt_entry *ent;
7fe2b9a6 10436 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10437 if (h->oh != NULL
10438 && h->oh->is_func_descriptor)
7b8f6675
AM
10439 {
10440 fdh = ppc_follow_link (h->oh);
10441 *hash = fdh;
10442 }
8387904d 10443
e054468f
AM
10444 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10445 if (ent->addend == rel->r_addend
10446 && ent->plt.offset != (bfd_vma) -1)
10447 {
e054468f
AM
10448 *plt_ent = ent;
10449 return ppc_stub_plt_call;
10450 }
5bd4f169 10451
7fe2b9a6
AM
10452 /* Here, we know we don't have a plt entry. If we don't have a
10453 either a defined function descriptor or a defined entry symbol
10454 in a regular object file, then it is pointless trying to make
10455 any other type of stub. */
854b41e7
AM
10456 if (!is_static_defined (&fdh->elf)
10457 && !is_static_defined (&h->elf))
721956f4 10458 return ppc_stub_none;
5d1634d7 10459 }
e054468f
AM
10460 else if (elf_local_got_ents (input_sec->owner) != NULL)
10461 {
10462 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10463 struct plt_entry **local_plt = (struct plt_entry **)
10464 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10465 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10466
10467 if (local_plt[r_symndx] != NULL)
10468 {
10469 struct plt_entry *ent;
10470
10471 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10472 if (ent->addend == rel->r_addend
10473 && ent->plt.offset != (bfd_vma) -1)
10474 {
10475 *plt_ent = ent;
10476 return ppc_stub_plt_call;
10477 }
10478 }
10479 }
5d1634d7 10480
721956f4
AM
10481 /* Determine where the call point is. */
10482 location = (input_sec->output_offset
10483 + input_sec->output_section->vma
10484 + rel->r_offset);
5d1634d7 10485
721956f4
AM
10486 branch_offset = destination - location;
10487 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10488
721956f4
AM
10489 /* Determine if a long branch stub is needed. */
10490 max_branch_offset = 1 << 25;
23cedd1d
AM
10491 if (r_type == R_PPC64_REL14
10492 || r_type == R_PPC64_REL14_BRTAKEN
10493 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10494 max_branch_offset = 1 << 15;
5d1634d7 10495
6911b7dc 10496 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10497 /* We need a stub. Figure out whether a long_branch or plt_branch
10498 is needed later. */
10499 return ppc_stub_long_branch;
5d1634d7 10500
721956f4 10501 return ppc_stub_none;
5d1634d7
AM
10502}
10503
f891966f
AM
10504/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10505 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10506 . mflr %r12
10507 . bcl 20,31,1f
10508 .1: mflr %r11
10509 . mtlr %r12
05d0e962 10510 . lis %r12,xxx-1b@highest
f891966f 10511 . ori %r12,%r12,xxx-1b@higher
05d0e962 10512 . sldi %r12,%r12,32
f891966f 10513 . oris %r12,%r12,xxx-1b@high
05d0e962 10514 . ori %r12,%r12,xxx-1b@l
f891966f 10515 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10516
10517static bfd_byte *
0a1b45a2 10518build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
05d0e962 10519{
f891966f
AM
10520 bfd_put_32 (abfd, MFLR_R12, p);
10521 p += 4;
10522 bfd_put_32 (abfd, BCL_20_31, p);
10523 p += 4;
10524 bfd_put_32 (abfd, MFLR_R11, p);
10525 p += 4;
10526 bfd_put_32 (abfd, MTLR_R12, p);
10527 p += 4;
05d0e962
AM
10528 if (off + 0x8000 < 0x10000)
10529 {
10530 if (load)
10531 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10532 else
10533 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10534 p += 4;
10535 }
10536 else if (off + 0x80008000ULL < 0x100000000ULL)
10537 {
10538 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10539 p += 4;
10540 if (load)
10541 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10542 else
10543 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10544 p += 4;
10545 }
10546 else
10547 {
10548 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10549 {
10550 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10551 p += 4;
10552 }
10553 else
10554 {
10555 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10556 p += 4;
10557 if (((off >> 32) & 0xffff) != 0)
10558 {
10559 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10560 p += 4;
10561 }
10562 }
10563 if (((off >> 32) & 0xffffffffULL) != 0)
10564 {
10565 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10566 p += 4;
10567 }
10568 if (PPC_HI (off) != 0)
10569 {
10570 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10571 p += 4;
10572 }
10573 if (PPC_LO (off) != 0)
10574 {
10575 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10576 p += 4;
10577 }
10578 if (load)
10579 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10580 else
10581 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10582 p += 4;
10583 }
10584 return p;
10585}
10586
10587static unsigned int
10588size_offset (bfd_vma off)
10589{
10590 unsigned int size;
10591 if (off + 0x8000 < 0x10000)
10592 size = 4;
10593 else if (off + 0x80008000ULL < 0x100000000ULL)
10594 size = 8;
10595 else
10596 {
10597 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10598 size = 4;
10599 else
10600 {
10601 size = 4;
10602 if (((off >> 32) & 0xffff) != 0)
10603 size += 4;
10604 }
10605 if (((off >> 32) & 0xffffffffULL) != 0)
10606 size += 4;
10607 if (PPC_HI (off) != 0)
10608 size += 4;
10609 if (PPC_LO (off) != 0)
10610 size += 4;
10611 size += 4;
10612 }
f891966f 10613 return size + 16;
05d0e962
AM
10614}
10615
3d58e1fc
AM
10616static unsigned int
10617num_relocs_for_offset (bfd_vma off)
10618{
10619 unsigned int num_rel;
10620 if (off + 0x8000 < 0x10000)
10621 num_rel = 1;
10622 else if (off + 0x80008000ULL < 0x100000000ULL)
10623 num_rel = 2;
10624 else
10625 {
10626 num_rel = 1;
10627 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10628 && ((off >> 32) & 0xffff) != 0)
10629 num_rel += 1;
10630 if (PPC_HI (off) != 0)
10631 num_rel += 1;
10632 if (PPC_LO (off) != 0)
10633 num_rel += 1;
10634 }
10635 return num_rel;
10636}
10637
10638static Elf_Internal_Rela *
10639emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10640 bfd_vma roff, bfd_vma targ, bfd_vma off)
10641{
10642 bfd_vma relative_targ = targ - (roff - 8);
10643 if (bfd_big_endian (info->output_bfd))
10644 roff += 2;
10645 r->r_offset = roff;
10646 r->r_addend = relative_targ + roff;
10647 if (off + 0x8000 < 0x10000)
10648 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10649 else if (off + 0x80008000ULL < 0x100000000ULL)
10650 {
10651 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10652 ++r;
10653 roff += 4;
10654 r->r_offset = roff;
10655 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10656 r->r_addend = relative_targ + roff;
10657 }
10658 else
10659 {
10660 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10661 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10662 else
10663 {
10664 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10665 if (((off >> 32) & 0xffff) != 0)
10666 {
10667 ++r;
10668 roff += 4;
10669 r->r_offset = roff;
10670 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10671 r->r_addend = relative_targ + roff;
10672 }
10673 }
10674 if (((off >> 32) & 0xffffffffULL) != 0)
10675 roff += 4;
10676 if (PPC_HI (off) != 0)
10677 {
10678 ++r;
10679 roff += 4;
10680 r->r_offset = roff;
10681 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10682 r->r_addend = relative_targ + roff;
10683 }
10684 if (PPC_LO (off) != 0)
10685 {
10686 ++r;
10687 roff += 4;
10688 r->r_offset = roff;
10689 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10690 r->r_addend = relative_targ + roff;
10691 }
10692 }
10693 return r;
10694}
10695
04bdff6a 10696static bfd_byte *
7c1f4227 10697build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
0a1b45a2 10698 bool load)
04bdff6a
AM
10699{
10700 uint64_t insn;
10701 if (off - odd + (1ULL << 33) < 1ULL << 34)
10702 {
10703 off -= odd;
10704 if (odd)
10705 {
10706 bfd_put_32 (abfd, NOP, p);
10707 p += 4;
10708 }
10709 if (load)
10710 insn = PLD_R12_PC;
10711 else
10712 insn = PADDI_R12_PC;
10713 insn |= D34 (off);
10714 bfd_put_32 (abfd, insn >> 32, p);
10715 p += 4;
10716 bfd_put_32 (abfd, insn, p);
10717 }
10718 /* The minimum value for paddi is -0x200000000. The minimum value
10719 for li is -0x8000, which when shifted by 34 and added gives a
10720 minimum value of -0x2000200000000. The maximum value is
10721 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10722 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10723 {
10724 off -= 8 - odd;
10725 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10726 p += 4;
10727 if (!odd)
10728 {
10729 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10730 p += 4;
10731 }
10732 insn = PADDI_R12_PC | D34 (off);
10733 bfd_put_32 (abfd, insn >> 32, p);
10734 p += 4;
10735 bfd_put_32 (abfd, insn, p);
10736 p += 4;
10737 if (odd)
10738 {
10739 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10740 p += 4;
10741 }
10742 if (load)
10743 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10744 else
10745 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10746 }
10747 else
10748 {
10749 off -= odd + 8;
10750 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10751 p += 4;
10752 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10753 p += 4;
10754 if (odd)
10755 {
10756 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10757 p += 4;
10758 }
10759 insn = PADDI_R12_PC | D34 (off);
10760 bfd_put_32 (abfd, insn >> 32, p);
10761 p += 4;
10762 bfd_put_32 (abfd, insn, p);
10763 p += 4;
10764 if (!odd)
10765 {
10766 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10767 p += 4;
10768 }
10769 if (load)
10770 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10771 else
10772 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10773 }
10774 p += 4;
10775 return p;
10776}
10777
10778static unsigned int
7c1f4227 10779size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10780{
10781 if (off - odd + (1ULL << 33) < 1ULL << 34)
10782 return odd + 8;
10783 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10784 return 20;
10785 else
10786 return 24;
10787}
10788
10789static unsigned int
7c1f4227 10790num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10791{
10792 if (off - odd + (1ULL << 33) < 1ULL << 34)
10793 return 1;
10794 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10795 return 2;
10796 else
10797 return 3;
10798}
10799
10800static Elf_Internal_Rela *
7c1f4227 10801emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
10802 Elf_Internal_Rela *r, bfd_vma roff,
10803 bfd_vma targ, bfd_vma off, int odd)
10804{
10805 if (off - odd + (1ULL << 33) < 1ULL << 34)
10806 roff += odd;
10807 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10808 {
10809 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10810 r->r_offset = roff + d_offset;
10811 r->r_addend = targ + 8 - odd - d_offset;
10812 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10813 ++r;
10814 roff += 8 - odd;
10815 }
10816 else
10817 {
10818 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10819 r->r_offset = roff + d_offset;
10820 r->r_addend = targ + 8 + odd - d_offset;
10821 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10822 ++r;
10823 roff += 4;
10824 r->r_offset = roff + d_offset;
10825 r->r_addend = targ + 4 + odd - d_offset;
10826 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10827 ++r;
10828 roff += 4 + odd;
10829 }
10830 r->r_offset = roff;
10831 r->r_addend = targ;
10832 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10833 return r;
10834}
10835
df136d64
AM
10836/* Emit .eh_frame opcode to advance pc by DELTA. */
10837
10838static bfd_byte *
10839eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10840{
10841 delta /= 4;
10842 if (delta < 64)
10843 *eh++ = DW_CFA_advance_loc + delta;
10844 else if (delta < 256)
10845 {
10846 *eh++ = DW_CFA_advance_loc1;
10847 *eh++ = delta;
10848 }
10849 else if (delta < 65536)
10850 {
10851 *eh++ = DW_CFA_advance_loc2;
10852 bfd_put_16 (abfd, delta, eh);
10853 eh += 2;
10854 }
10855 else
10856 {
10857 *eh++ = DW_CFA_advance_loc4;
10858 bfd_put_32 (abfd, delta, eh);
10859 eh += 4;
10860 }
10861 return eh;
10862}
10863
10864/* Size of required .eh_frame opcode to advance pc by DELTA. */
10865
10866static unsigned int
10867eh_advance_size (unsigned int delta)
10868{
10869 if (delta < 64 * 4)
10870 /* DW_CFA_advance_loc+[1..63]. */
10871 return 1;
10872 if (delta < 256 * 4)
10873 /* DW_CFA_advance_loc1, byte. */
10874 return 2;
10875 if (delta < 65536 * 4)
10876 /* DW_CFA_advance_loc2, 2 bytes. */
10877 return 3;
10878 /* DW_CFA_advance_loc4, 4 bytes. */
10879 return 5;
10880}
10881
794e51c0
AM
10882/* With power7 weakly ordered memory model, it is possible for ld.so
10883 to update a plt entry in one thread and have another thread see a
10884 stale zero toc entry. To avoid this we need some sort of acquire
10885 barrier in the call stub. One solution is to make the load of the
10886 toc word seem to appear to depend on the load of the function entry
10887 word. Another solution is to test for r2 being zero, and branch to
10888 the appropriate glink entry if so.
10889
10890 . fake dep barrier compare
71a39c98
AM
10891 . ld 12,xxx(2) ld 12,xxx(2)
10892 . mtctr 12 mtctr 12
10893 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10894 . add 2,2,11 cmpldi 2,0
10895 . ld 2,xxx+8(2) bnectr+
10896 . bctr b <glink_entry>
10897
10898 The solution involving the compare turns out to be faster, so
10899 that's what we use unless the branch won't reach. */
10900
10901#define ALWAYS_USE_FAKE_DEP 0
10902#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10903
794e51c0
AM
10904static inline unsigned int
10905plt_stub_size (struct ppc_link_hash_table *htab,
10906 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10907 bfd_vma off,
10908 unsigned int odd)
794e51c0 10909{
05d0e962 10910 unsigned size;
b9e5796b 10911
05d0e962
AM
10912 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10913 {
e10a07b3 10914 if (htab->params->power10_stubs != 0)
29433886 10915 size = 8 + size_power10_offset (off, odd);
04bdff6a
AM
10916 else
10917 size = 8 + size_offset (off - 8);
05d0e962
AM
10918 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10919 size += 4;
05d0e962 10920 }
29433886 10921 else
b9e5796b 10922 {
29433886
AM
10923 size = 12;
10924 if (ALWAYS_EMIT_R2SAVE
10925 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
b9e5796b 10926 size += 4;
29433886 10927 if (PPC_HA (off) != 0)
b9e5796b 10928 size += 4;
29433886
AM
10929 if (htab->opd_abi)
10930 {
10931 size += 4;
10932 if (htab->params->plt_static_chain)
10933 size += 4;
10934 if (htab->params->plt_thread_safe
10935 && htab->elf.dynamic_sections_created
10936 && stub_entry->h != NULL
10937 && stub_entry->h->elf.dynindx != -1)
10938 size += 8;
10939 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
10940 != PPC_HA (off))
10941 size += 4;
10942 }
b9e5796b 10943 }
794e51c0 10944 if (stub_entry->h != NULL
ed7007c1 10945 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10946 && htab->params->tls_get_addr_opt)
f378ab09 10947 {
29433886 10948 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 10949 {
29433886
AM
10950 size += 30 * 4;
10951 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10952 || stub_entry->stub_type == ppc_stub_plt_call_both)
10953 size += 4;
9e7028aa
AM
10954 }
10955 else
10956 {
29433886
AM
10957 size += 7 * 4;
10958 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10959 || stub_entry->stub_type == ppc_stub_plt_call_both)
10960 size += 6 * 4;
9e7028aa 10961 }
f378ab09 10962 }
794e51c0
AM
10963 return size;
10964}
10965
2420fff6
AM
10966/* Depending on the sign of plt_stub_align:
10967 If positive, return the padding to align to a 2**plt_stub_align
10968 boundary.
10969 If negative, if this stub would cross fewer 2**plt_stub_align
10970 boundaries if we align, then return the padding needed to do so. */
10971
794e51c0
AM
10972static inline unsigned int
10973plt_stub_pad (struct ppc_link_hash_table *htab,
10974 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10975 bfd_vma plt_off,
10976 unsigned int odd)
794e51c0 10977{
2420fff6 10978 int stub_align;
1aa42141 10979 unsigned stub_size;
6f20ed8a 10980 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10981
2420fff6
AM
10982 if (htab->params->plt_stub_align >= 0)
10983 {
10984 stub_align = 1 << htab->params->plt_stub_align;
10985 if ((stub_off & (stub_align - 1)) != 0)
10986 return stub_align - (stub_off & (stub_align - 1));
10987 return 0;
10988 }
10989
10990 stub_align = 1 << -htab->params->plt_stub_align;
29433886 10991 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 10992 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10993 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10994 return stub_align - (stub_off & (stub_align - 1));
10995 return 0;
10996}
10997
10998/* Build a .plt call stub. */
10999
11000static inline bfd_byte *
11001build_plt_stub (struct ppc_link_hash_table *htab,
11002 struct ppc_stub_hash_entry *stub_entry,
11003 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11004{
e7d1c40c 11005 bfd *obfd = htab->params->stub_bfd;
0a1b45a2
AM
11006 bool plt_load_toc = htab->opd_abi;
11007 bool plt_static_chain = htab->params->plt_static_chain;
11008 bool plt_thread_safe = (htab->params->plt_thread_safe
11009 && htab->elf.dynamic_sections_created
11010 && stub_entry->h != NULL
11011 && stub_entry->h->elf.dynindx != -1);
11012 bool use_fake_dep = plt_thread_safe;
794e51c0
AM
11013 bfd_vma cmp_branch_off = 0;
11014
11015 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 11016 && plt_load_toc
794e51c0 11017 && plt_thread_safe
9e51d549
AM
11018 && !(stub_entry->h != NULL
11019 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 11020 && htab->params->tls_get_addr_opt))
794e51c0
AM
11021 {
11022 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11023 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11024 / PLT_ENTRY_SIZE (htab));
9e390558 11025 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11026 bfd_vma to, from;
11027
68d62958
AM
11028 if (pltindex > 32768)
11029 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11030 to = (glinkoff
11031 + htab->glink->output_offset
11032 + htab->glink->output_section->vma);
6f20ed8a 11033 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
11034 + 4 * (ALWAYS_EMIT_R2SAVE
11035 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11036 + 4 * (PPC_HA (offset) != 0)
11037 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11038 != PPC_HA (offset))
11039 + 4 * (plt_static_chain != 0)
11040 + 20
6f20ed8a
AM
11041 + stub_entry->group->stub_sec->output_offset
11042 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11043 cmp_branch_off = to - from;
11044 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11045 }
11046
ac2df442
AM
11047 if (PPC_HA (offset) != 0)
11048 {
176a0d42
AM
11049 if (r != NULL)
11050 {
794e51c0
AM
11051 if (ALWAYS_EMIT_R2SAVE
11052 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11053 r[0].r_offset += 4;
176a0d42 11054 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11055 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11056 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11057 r[1].r_addend = r[0].r_addend;
b9e5796b 11058 if (plt_load_toc)
176a0d42 11059 {
b9e5796b 11060 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11061 {
b9e5796b
AM
11062 r[2].r_offset = r[1].r_offset + 4;
11063 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11064 r[2].r_addend = r[0].r_addend;
11065 }
11066 else
11067 {
11068 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11069 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11070 r[2].r_addend = r[0].r_addend + 8;
11071 if (plt_static_chain)
11072 {
11073 r[3].r_offset = r[2].r_offset + 4;
11074 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11075 r[3].r_addend = r[0].r_addend + 16;
11076 }
c7131b65 11077 }
176a0d42
AM
11078 }
11079 }
794e51c0
AM
11080 if (ALWAYS_EMIT_R2SAVE
11081 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11082 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11083 if (plt_load_toc)
11084 {
11085 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11086 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11087 }
11088 else
11089 {
11090 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11091 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11092 }
b9e5796b
AM
11093 if (plt_load_toc
11094 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11095 {
71a39c98 11096 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11097 offset = 0;
11098 }
71a39c98 11099 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11100 if (plt_load_toc)
794e51c0 11101 {
b9e5796b
AM
11102 if (use_fake_dep)
11103 {
11104 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11105 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11106 }
11107 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11108 if (plt_static_chain)
11109 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11110 }
ac2df442
AM
11111 }
11112 else
11113 {
176a0d42
AM
11114 if (r != NULL)
11115 {
794e51c0
AM
11116 if (ALWAYS_EMIT_R2SAVE
11117 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11118 r[0].r_offset += 4;
176a0d42 11119 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11120 if (plt_load_toc)
176a0d42 11121 {
b9e5796b 11122 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11123 {
b9e5796b
AM
11124 r[1].r_offset = r[0].r_offset + 4;
11125 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11126 r[1].r_addend = r[0].r_addend;
11127 }
11128 else
11129 {
11130 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11131 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11132 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11133 if (plt_static_chain)
11134 {
11135 r[2].r_offset = r[1].r_offset + 4;
11136 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11137 r[2].r_addend = r[0].r_addend + 8;
11138 }
c7131b65 11139 }
176a0d42
AM
11140 }
11141 }
794e51c0
AM
11142 if (ALWAYS_EMIT_R2SAVE
11143 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11144 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11145 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11146 if (plt_load_toc
11147 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11148 {
11149 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11150 offset = 0;
11151 }
71a39c98 11152 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11153 if (plt_load_toc)
794e51c0 11154 {
b9e5796b
AM
11155 if (use_fake_dep)
11156 {
11157 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11158 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11159 }
11160 if (plt_static_chain)
11161 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11162 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11163 }
ac2df442 11164 }
b9e5796b 11165 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11166 {
11167 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11168 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11169 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11170 }
11171 else
407aa07c 11172 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11173 return p;
11174}
11175
a7f2871e
AM
11176/* Build a special .plt call stub for __tls_get_addr. */
11177
b9ca1af6 11178#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11179#define LD_R12_0R3 0xe9830000
11180#define MR_R0_R3 0x7c601b78
b9ca1af6 11181#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11182#define ADD_R3_R12_R13 0x7c6c6a14
11183#define BEQLR 0x4d820020
11184#define MR_R3_R0 0x7c030378
a7f2871e 11185#define BCTRL 0x4e800421
a7f2871e 11186
29433886
AM
11187static bfd_byte *
11188build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11189 struct ppc_stub_hash_entry *stub_entry,
29433886 11190 bfd_byte *p)
a7f2871e 11191{
e7d1c40c 11192 bfd *obfd = htab->params->stub_bfd;
794e51c0 11193
b9ca1af6 11194 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11195 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11196 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11197 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11198 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11199 bfd_put_32 (obfd, BEQLR, p), p += 4;
11200 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11201
29433886
AM
11202 if (!htab->params->no_tls_get_addr_regsave)
11203 p = tls_get_addr_prologue (obfd, p, htab);
11204 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11205 || stub_entry->stub_type == ppc_stub_plt_call_both)
11206 {
9e7028aa
AM
11207 bfd_put_32 (obfd, MFLR_R0, p);
11208 p += 4;
11209 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11210 p += 4;
29433886
AM
11211 }
11212 return p;
11213}
9e7028aa 11214
29433886
AM
11215static bfd_byte *
11216build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11217 struct ppc_stub_hash_entry *stub_entry,
11218 bfd_byte *p,
11219 bfd_byte *loc)
11220{
11221 bfd *obfd = htab->params->stub_bfd;
11222
11223 if (!htab->params->no_tls_get_addr_regsave)
11224 {
11225 bfd_put_32 (obfd, BCTRL, p - 4);
11226
11227 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11228 || stub_entry->stub_type == ppc_stub_plt_call_both)
11229 {
11230 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11231 p += 4;
11232 }
11233 p = tls_get_addr_epilogue (obfd, p, htab);
11234 }
11235 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11236 || stub_entry->stub_type == ppc_stub_plt_call_both)
11237 {
9e7028aa
AM
11238 bfd_put_32 (obfd, BCTRL, p - 4);
11239
11240 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11241 p += 4;
11242 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11243 p += 4;
11244 bfd_put_32 (obfd, MTLR_R0, p);
11245 p += 4;
11246 bfd_put_32 (obfd, BLR, p);
11247 p += 4;
11248 }
a7f2871e 11249
df136d64
AM
11250 if (htab->glink_eh_frame != NULL
11251 && htab->glink_eh_frame->size != 0)
11252 {
11253 bfd_byte *base, *eh;
df136d64
AM
11254
11255 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11256 eh = base + stub_entry->group->eh_size;
29433886
AM
11257
11258 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11259 {
29433886
AM
11260 unsigned int cfa_updt, delta, i;
11261
9e7028aa
AM
11262 /* After the bctrl, lr has been modified so we need to emit
11263 .eh_frame info saying the return address is on the stack. In
11264 fact we must put the EH info at or before the call rather
11265 than after it, because the EH info for a call needs to be
11266 specified by that point.
11267 See libgcc/unwind-dw2.c execute_cfa_program.
11268 Any stack pointer update must be described immediately after
11269 the instruction making the change, and since the stdu occurs
11270 after saving regs we put all the reg saves and the cfa
11271 change there. */
11272 cfa_updt = stub_entry->stub_offset + 18 * 4;
11273 delta = cfa_updt - stub_entry->group->lr_restore;
11274 stub_entry->group->lr_restore
11275 = stub_entry->stub_offset + (p - loc) - 4;
11276 eh = eh_advance (htab->elf.dynobj, eh, delta);
11277 *eh++ = DW_CFA_def_cfa_offset;
11278 if (htab->opd_abi)
11279 {
11280 *eh++ = 128;
11281 *eh++ = 1;
11282 }
11283 else
11284 *eh++ = 96;
11285 *eh++ = DW_CFA_offset_extended_sf;
11286 *eh++ = 65;
11287 *eh++ = (-16 / 8) & 0x7f;
11288 for (i = 4; i < 12; i++)
11289 {
11290 *eh++ = DW_CFA_offset + i;
11291 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11292 }
11293 *eh++ = (DW_CFA_advance_loc
11294 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11295 *eh++ = DW_CFA_def_cfa_offset;
11296 *eh++ = 0;
11297 for (i = 4; i < 12; i++)
11298 *eh++ = DW_CFA_restore + i;
11299 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11300 *eh++ = DW_CFA_restore_extended;
11301 *eh++ = 65;
11302 stub_entry->group->eh_size = eh - base;
11303 }
11304 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11305 || stub_entry->stub_type == ppc_stub_plt_call_both)
11306 {
11307 unsigned int lr_used, delta;
11308
11309 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11310 delta = lr_used - stub_entry->group->lr_restore;
11311 stub_entry->group->lr_restore = lr_used + 16;
11312 eh = eh_advance (htab->elf.dynobj, eh, delta);
11313 *eh++ = DW_CFA_offset_extended_sf;
11314 *eh++ = 65;
11315 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11316 *eh++ = DW_CFA_advance_loc + 4;
11317 *eh++ = DW_CFA_restore_extended;
11318 *eh++ = 65;
11319 stub_entry->group->eh_size = eh - base;
9e7028aa 11320 }
df136d64 11321 }
a7f2871e
AM
11322 return p;
11323}
11324
176a0d42
AM
11325static Elf_Internal_Rela *
11326get_relocs (asection *sec, int count)
11327{
11328 Elf_Internal_Rela *relocs;
11329 struct bfd_elf_section_data *elfsec_data;
11330
11331 elfsec_data = elf_section_data (sec);
11332 relocs = elfsec_data->relocs;
11333 if (relocs == NULL)
11334 {
11335 bfd_size_type relsize;
11336 relsize = sec->reloc_count * sizeof (*relocs);
11337 relocs = bfd_alloc (sec->owner, relsize);
11338 if (relocs == NULL)
11339 return NULL;
11340 elfsec_data->relocs = relocs;
d4730f92
BS
11341 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11342 sizeof (Elf_Internal_Shdr));
11343 if (elfsec_data->rela.hdr == NULL)
11344 return NULL;
11345 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11346 * sizeof (Elf64_External_Rela));
11347 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11348 sec->reloc_count = 0;
11349 }
11350 relocs += sec->reloc_count;
11351 sec->reloc_count += count;
11352 return relocs;
11353}
11354
3d58e1fc
AM
11355/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11356 forms, to the equivalent relocs against the global symbol given by
11357 STUB_ENTRY->H. */
11358
0a1b45a2 11359static bool
3d58e1fc
AM
11360use_global_in_relocs (struct ppc_link_hash_table *htab,
11361 struct ppc_stub_hash_entry *stub_entry,
11362 Elf_Internal_Rela *r, unsigned int num_rel)
11363{
11364 struct elf_link_hash_entry **hashes;
11365 unsigned long symndx;
11366 struct ppc_link_hash_entry *h;
11367 bfd_vma symval;
11368
11369 /* Relocs are always against symbols in their own object file. Fake
11370 up global sym hashes for the stub bfd (which has no symbols). */
11371 hashes = elf_sym_hashes (htab->params->stub_bfd);
11372 if (hashes == NULL)
11373 {
11374 bfd_size_type hsize;
11375
11376 /* When called the first time, stub_globals will contain the
11377 total number of symbols seen during stub sizing. After
11378 allocating, stub_globals is used as an index to fill the
11379 hashes array. */
11380 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11381 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11382 if (hashes == NULL)
0a1b45a2 11383 return false;
3d58e1fc
AM
11384 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11385 htab->stub_globals = 1;
11386 }
11387 symndx = htab->stub_globals++;
11388 h = stub_entry->h;
11389 hashes[symndx] = &h->elf;
11390 if (h->oh != NULL && h->oh->is_func)
11391 h = ppc_follow_link (h->oh);
11392 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11393 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11394 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11395 while (num_rel-- != 0)
11396 {
11397 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11398 if (h->elf.root.u.def.section != stub_entry->target_section)
11399 {
11400 /* H is an opd symbol. The addend must be zero, and the
11401 branch reloc is the only one we can convert. */
11402 r->r_addend = 0;
11403 break;
11404 }
11405 else
11406 r->r_addend -= symval;
11407 --r;
11408 }
0a1b45a2 11409 return true;
3d58e1fc
AM
11410}
11411
aa374f67 11412static bfd_vma
25f53a85 11413get_r2off (struct bfd_link_info *info,
aa374f67
AM
11414 struct ppc_stub_hash_entry *stub_entry)
11415{
25f53a85 11416 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11417 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11418
11419 if (r2off == 0)
11420 {
11421 /* Support linking -R objects. Get the toc pointer from the
11422 opd entry. */
11423 char buf[8];
b9e5796b
AM
11424 if (!htab->opd_abi)
11425 return r2off;
aa374f67
AM
11426 asection *opd = stub_entry->h->elf.root.u.def.section;
11427 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11428
11429 if (strcmp (opd->name, ".opd") != 0
11430 || opd->reloc_count != 0)
11431 {
2cdcc330
AM
11432 info->callbacks->einfo
11433 (_("%P: cannot find opd entry toc for `%pT'\n"),
11434 stub_entry->h->elf.root.root.string);
aa374f67 11435 bfd_set_error (bfd_error_bad_value);
a7c49797 11436 return (bfd_vma) -1;
aa374f67
AM
11437 }
11438 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11439 return (bfd_vma) -1;
aa374f67 11440 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11441 r2off -= elf_gp (info->output_bfd);
aa374f67 11442 }
6f20ed8a 11443 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11444 return r2off;
11445}
11446
0a1b45a2 11447static bool
4ce794b7 11448ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11449{
721956f4
AM
11450 struct ppc_stub_hash_entry *stub_entry;
11451 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11452 struct bfd_link_info *info;
11453 struct ppc_link_hash_table *htab;
29433886 11454 bfd *obfd;
721956f4 11455 bfd_byte *loc;
3d58e1fc 11456 bfd_byte *p, *relp;
1aa42141 11457 bfd_vma targ, off;
176a0d42 11458 Elf_Internal_Rela *r;
e054468f 11459 asection *plt;
3d58e1fc 11460 int num_rel;
04bdff6a 11461 int odd;
0a1b45a2 11462 bool is_tga;
5d1634d7 11463
721956f4
AM
11464 /* Massage our args to the form they really have. */
11465 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11466 info = in_arg;
5d1634d7 11467
abf874aa
CL
11468 /* Fail if the target section could not be assigned to an output
11469 section. The user should fix his linker script. */
11470 if (stub_entry->target_section != NULL
11471 && stub_entry->target_section->output_section == NULL
11472 && info->non_contiguous_regions)
53215f21
CL
11473 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11474 "Retry without --enable-non-contiguous-regions.\n"),
11475 stub_entry->target_section);
abf874aa
CL
11476
11477 /* Same for the group. */
11478 if (stub_entry->group->stub_sec != NULL
11479 && stub_entry->group->stub_sec->output_section == NULL
11480 && info->non_contiguous_regions)
53215f21
CL
11481 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11482 "output section. Retry without "
11483 "--enable-non-contiguous-regions.\n"),
11484 stub_entry->group->stub_sec,
11485 stub_entry->target_section);
abf874aa 11486
5d1634d7 11487 htab = ppc_hash_table (info);
4dfe6ac6 11488 if (htab == NULL)
0a1b45a2 11489 return false;
5d1634d7 11490
1aa42141 11491 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11492 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11493
4ce794b7 11494 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11495 switch (stub_entry->stub_type)
5d1634d7 11496 {
721956f4 11497 case ppc_stub_long_branch:
ad8e1ba5 11498 case ppc_stub_long_branch_r2off:
721956f4 11499 /* Branches are relative. This is where we are going to. */
1aa42141 11500 targ = (stub_entry->target_value
6911b7dc
AM
11501 + stub_entry->target_section->output_offset
11502 + stub_entry->target_section->output_section->vma);
1aa42141 11503 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11504
721956f4 11505 /* And this is where we are coming from. */
1aa42141
AM
11506 off = (stub_entry->stub_offset
11507 + stub_entry->group->stub_sec->output_offset
11508 + stub_entry->group->stub_sec->output_section->vma);
11509 off = targ - off;
e86ce104 11510
9e390558 11511 p = loc;
29433886 11512 obfd = htab->params->stub_bfd;
ac2df442 11513 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11514 {
25f53a85 11515 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11516
a7c49797 11517 if (r2off == (bfd_vma) -1)
aa374f67 11518 {
0a1b45a2
AM
11519 htab->stub_error = true;
11520 return false;
aa374f67 11521 }
29433886 11522 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11523 p += 4;
ac2df442
AM
11524 if (PPC_HA (r2off) != 0)
11525 {
29433886 11526 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11527 p += 4;
a7c49797
AM
11528 }
11529 if (PPC_LO (r2off) != 0)
11530 {
29433886 11531 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11532 p += 4;
ac2df442 11533 }
9e390558 11534 off -= p - loc;
ad8e1ba5 11535 }
29433886 11536 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11537 p += 4;
ad8e1ba5 11538
5c3dead3
AM
11539 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11540 {
cf97bcb0
AM
11541 _bfd_error_handler
11542 (_("long branch stub `%s' offset overflow"),
bc30df16 11543 stub_entry->root.string);
0a1b45a2
AM
11544 htab->stub_error = true;
11545 return false;
5c3dead3 11546 }
ee75fd95
AM
11547
11548 if (info->emitrelocations)
11549 {
6f20ed8a 11550 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42 11551 if (r == NULL)
0a1b45a2 11552 return false;
9e390558 11553 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11554 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11555 r->r_addend = targ;
3d58e1fc
AM
11556 if (stub_entry->h != NULL
11557 && !use_global_in_relocs (htab, stub_entry, r, 1))
0a1b45a2 11558 return false;
ee75fd95 11559 }
721956f4 11560 break;
e86ce104 11561
721956f4 11562 case ppc_stub_plt_branch:
ad8e1ba5 11563 case ppc_stub_plt_branch_r2off:
721956f4
AM
11564 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11565 stub_entry->root.string + 9,
0a1b45a2 11566 false, false);
721956f4
AM
11567 if (br_entry == NULL)
11568 {
cf97bcb0
AM
11569 _bfd_error_handler (_("can't find branch stub `%s'"),
11570 stub_entry->root.string);
0a1b45a2
AM
11571 htab->stub_error = true;
11572 return false;
721956f4
AM
11573 }
11574
1aa42141 11575 targ = (stub_entry->target_value
176a0d42
AM
11576 + stub_entry->target_section->output_offset
11577 + stub_entry->target_section->output_section->vma);
6911b7dc 11578 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11579 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11580
1aa42141 11581 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11582 htab->brlt->contents + br_entry->offset);
721956f4 11583
f94498ff 11584 if (br_entry->iter == htab->stub_iteration)
721956f4 11585 {
f94498ff 11586 br_entry->iter = 0;
84f5d08e 11587
f94498ff 11588 if (htab->relbrlt != NULL)
84f5d08e 11589 {
f94498ff
AM
11590 /* Create a reloc for the branch lookup table entry. */
11591 Elf_Internal_Rela rela;
11592 bfd_byte *rl;
11593
11594 rela.r_offset = (br_entry->offset
11595 + htab->brlt->output_offset
11596 + htab->brlt->output_section->vma);
11597 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11598 rela.r_addend = targ;
f94498ff
AM
11599
11600 rl = htab->relbrlt->contents;
11601 rl += (htab->relbrlt->reloc_count++
11602 * sizeof (Elf64_External_Rela));
11603 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11604 }
11605 else if (info->emitrelocations)
11606 {
176a0d42
AM
11607 r = get_relocs (htab->brlt, 1);
11608 if (r == NULL)
0a1b45a2 11609 return false;
176a0d42
AM
11610 /* brlt, being SEC_LINKER_CREATED does not go through the
11611 normal reloc processing. Symbols and offsets are not
11612 translated from input file to output file form, so
11613 set up the offset per the output file. */
f94498ff
AM
11614 r->r_offset = (br_entry->offset
11615 + htab->brlt->output_offset
11616 + htab->brlt->output_section->vma);
11617 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11618 r->r_addend = targ;
84f5d08e 11619 }
84f5d08e 11620 }
721956f4 11621
1aa42141 11622 targ = (br_entry->offset
176a0d42
AM
11623 + htab->brlt->output_offset
11624 + htab->brlt->output_section->vma);
11625
1aa42141
AM
11626 off = (elf_gp (info->output_bfd)
11627 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11628 off = targ - off;
721956f4 11629
ad8e1ba5 11630 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11631 {
25f53a85 11632 info->callbacks->einfo
c1c8c1ef 11633 (_("%P: linkage table error against `%pT'\n"),
721956f4 11634 stub_entry->root.string);
5d1634d7 11635 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11636 htab->stub_error = true;
11637 return false;
5d1634d7 11638 }
41bd81ab 11639
176a0d42
AM
11640 if (info->emitrelocations)
11641 {
6f20ed8a 11642 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42 11643 if (r == NULL)
0a1b45a2 11644 return false;
6f20ed8a 11645 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11646 if (bfd_big_endian (info->output_bfd))
11647 r[0].r_offset += 2;
00f412ee 11648 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11649 r[0].r_offset += 4;
11650 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11651 r[0].r_addend = targ;
176a0d42
AM
11652 if (PPC_HA (off) != 0)
11653 {
11654 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11655 r[1].r_offset = r[0].r_offset + 4;
11656 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11657 r[1].r_addend = r[0].r_addend;
11658 }
11659 }
11660
9e390558 11661 p = loc;
29433886 11662 obfd = htab->params->stub_bfd;
00f412ee 11663 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11664 {
176a0d42 11665 if (PPC_HA (off) != 0)
ac2df442 11666 {
29433886 11667 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11668 p += 4;
29433886 11669 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11670 }
11671 else
29433886 11672 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11673 }
11674 else
11675 {
25f53a85 11676 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11677
a7c49797 11678 if (r2off == (bfd_vma) -1)
aa374f67 11679 {
0a1b45a2
AM
11680 htab->stub_error = true;
11681 return false;
aa374f67 11682 }
ad8e1ba5 11683
29433886 11684 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11685 p += 4;
176a0d42 11686 if (PPC_HA (off) != 0)
ac2df442 11687 {
29433886 11688 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11689 p += 4;
29433886 11690 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11691 }
11692 else
29433886 11693 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11694
11695 if (PPC_HA (r2off) != 0)
11696 {
9e390558 11697 p += 4;
29433886 11698 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11699 }
11700 if (PPC_LO (r2off) != 0)
11701 {
9e390558 11702 p += 4;
29433886 11703 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11704 }
ad8e1ba5 11705 }
9e390558 11706 p += 4;
29433886 11707 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11708 p += 4;
29433886 11709 bfd_put_32 (obfd, BCTR, p);
407aa07c 11710 p += 4;
721956f4 11711 break;
5d1634d7 11712
05d0e962
AM
11713 case ppc_stub_long_branch_notoc:
11714 case ppc_stub_long_branch_both:
11715 case ppc_stub_plt_branch_notoc:
11716 case ppc_stub_plt_branch_both:
11717 case ppc_stub_plt_call_notoc:
11718 case ppc_stub_plt_call_both:
11719 p = loc;
f891966f 11720 off = (stub_entry->stub_offset
05d0e962
AM
11721 + stub_entry->group->stub_sec->output_offset
11722 + stub_entry->group->stub_sec->output_section->vma);
29433886
AM
11723 obfd = htab->params->stub_bfd;
11724 is_tga = ((stub_entry->stub_type == ppc_stub_plt_call_notoc
11725 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 11726 && stub_entry->h != NULL
29433886
AM
11727 && is_tls_get_addr (&stub_entry->h->elf, htab)
11728 && htab->params->tls_get_addr_opt);
11729 if (is_tga)
11730 {
11731 p = build_tls_get_addr_head (htab, stub_entry, p);
11732 off += p - loc;
11733 }
05d0e962
AM
11734 if (stub_entry->stub_type == ppc_stub_long_branch_both
11735 || stub_entry->stub_type == ppc_stub_plt_branch_both
11736 || stub_entry->stub_type == ppc_stub_plt_call_both)
11737 {
11738 off += 4;
29433886 11739 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11740 p += 4;
11741 }
11742 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11743 {
11744 targ = stub_entry->plt_ent->plt.offset & ~1;
11745 if (targ >= (bfd_vma) -2)
11746 abort ();
11747
11748 plt = htab->elf.splt;
30845f11 11749 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
11750 {
11751 if (stub_entry->symtype == STT_GNU_IFUNC)
11752 plt = htab->elf.iplt;
11753 else
11754 plt = htab->pltlocal;
11755 }
11756 targ += plt->output_offset + plt->output_section->vma;
11757 }
11758 else
11759 targ = (stub_entry->target_value
11760 + stub_entry->target_section->output_offset
11761 + stub_entry->target_section->output_section->vma);
04bdff6a 11762 odd = off & 4;
05d0e962 11763 off = targ - off;
f891966f 11764
3d58e1fc
AM
11765 relp = p;
11766 num_rel = 0;
e10a07b3 11767 if (htab->params->power10_stubs != 0)
04bdff6a 11768 {
0a1b45a2 11769 bool load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
29433886 11770 p = build_power10_offset (obfd, p, off, odd, load);
04bdff6a
AM
11771 }
11772 else
11773 {
29433886
AM
11774 if (htab->glink_eh_frame != NULL
11775 && htab->glink_eh_frame->size != 0)
11776 {
11777 bfd_byte *base, *eh;
11778 unsigned int lr_used, delta;
11779
11780 base = (htab->glink_eh_frame->contents
11781 + stub_entry->group->eh_base + 17);
11782 eh = base + stub_entry->group->eh_size;
11783 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11784 delta = lr_used - stub_entry->group->lr_restore;
11785 stub_entry->group->lr_restore = lr_used + 8;
11786 eh = eh_advance (htab->elf.dynobj, eh, delta);
11787 *eh++ = DW_CFA_register;
11788 *eh++ = 65;
11789 *eh++ = 12;
11790 *eh++ = DW_CFA_advance_loc + 2;
11791 *eh++ = DW_CFA_restore_extended;
11792 *eh++ = 65;
11793 stub_entry->group->eh_size = eh - base;
11794 }
11795
04bdff6a
AM
11796 /* The notoc stubs calculate their target (either a PLT entry or
11797 the global entry point of a function) relative to the PC
11798 returned by the "bcl" two instructions past the start of the
11799 sequence emitted by build_offset. The offset is therefore 8
11800 less than calculated from the start of the sequence. */
11801 off -= 8;
29433886 11802 p = build_offset (obfd, p, off,
04bdff6a
AM
11803 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11804 }
11805
f891966f 11806 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11807 {
f891966f 11808 bfd_vma from;
3d58e1fc 11809 num_rel = 1;
f891966f
AM
11810 from = (stub_entry->stub_offset
11811 + stub_entry->group->stub_sec->output_offset
11812 + stub_entry->group->stub_sec->output_section->vma
11813 + (p - loc));
29433886 11814 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11815 }
11816 else
11817 {
29433886 11818 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 11819 p += 4;
29433886 11820 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
11821 }
11822 p += 4;
df136d64 11823
29433886
AM
11824 if (is_tga)
11825 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11826
3d58e1fc
AM
11827 if (info->emitrelocations)
11828 {
04bdff6a 11829 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
e10a07b3 11830 if (htab->params->power10_stubs != 0)
7c1f4227 11831 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
11832 else
11833 {
11834 num_rel += num_relocs_for_offset (off);
11835 roff += 16;
11836 }
3d58e1fc
AM
11837 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11838 if (r == NULL)
0a1b45a2 11839 return false;
e10a07b3 11840 if (htab->params->power10_stubs != 0)
7c1f4227 11841 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
11842 else
11843 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11844 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11845 || stub_entry->stub_type == ppc_stub_long_branch_both)
11846 {
11847 ++r;
11848 roff = p - 4 - stub_entry->group->stub_sec->contents;
11849 r->r_offset = roff;
11850 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11851 r->r_addend = targ;
11852 if (stub_entry->h != NULL
11853 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
0a1b45a2 11854 return false;
3d58e1fc
AM
11855 }
11856 }
05d0e962
AM
11857 break;
11858
721956f4 11859 case ppc_stub_plt_call:
794e51c0 11860 case ppc_stub_plt_call_r2save:
e054468f 11861 if (stub_entry->h != NULL
b31867b6
AM
11862 && stub_entry->h->is_func_descriptor
11863 && stub_entry->h->oh != NULL)
c862ae31 11864 {
b31867b6
AM
11865 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11866
11867 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11868 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11869 if (fh->elf.root.type == bfd_link_hash_undefined
11870 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11871 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11872 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11873 }
11874
721956f4 11875 /* Now build the stub. */
1aa42141
AM
11876 targ = stub_entry->plt_ent->plt.offset & ~1;
11877 if (targ >= (bfd_vma) -2)
721956f4
AM
11878 abort ();
11879
33e44f2e 11880 plt = htab->elf.splt;
30845f11 11881 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
11882 {
11883 if (stub_entry->symtype == STT_GNU_IFUNC)
11884 plt = htab->elf.iplt;
11885 else
11886 plt = htab->pltlocal;
11887 }
1aa42141 11888 targ += plt->output_offset + plt->output_section->vma;
e054468f 11889
1aa42141
AM
11890 off = (elf_gp (info->output_bfd)
11891 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11892 off = targ - off;
721956f4 11893
ad8e1ba5 11894 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11895 {
25f53a85 11896 info->callbacks->einfo
695344c0 11897 /* xgettext:c-format */
c1c8c1ef 11898 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11899 stub_entry->h != NULL
11900 ? stub_entry->h->elf.root.root.string
11901 : "<local sym>");
721956f4 11902 bfd_set_error (bfd_error_bad_value);
0a1b45a2
AM
11903 htab->stub_error = true;
11904 return false;
721956f4
AM
11905 }
11906
176a0d42
AM
11907 r = NULL;
11908 if (info->emitrelocations)
11909 {
6f20ed8a 11910 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11911 ((PPC_HA (off) != 0)
11912 + (htab->opd_abi
e7d1c40c 11913 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11914 && PPC_HA (off + 16) == PPC_HA (off))
11915 : 1)));
176a0d42 11916 if (r == NULL)
0a1b45a2 11917 return false;
6f20ed8a 11918 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11919 if (bfd_big_endian (info->output_bfd))
11920 r[0].r_offset += 2;
1aa42141 11921 r[0].r_addend = targ;
176a0d42 11922 }
29433886
AM
11923 p = loc;
11924 obfd = htab->params->stub_bfd;
11925 is_tga = (stub_entry->h != NULL
11926 && is_tls_get_addr (&stub_entry->h->elf, htab)
11927 && htab->params->tls_get_addr_opt);
11928 if (is_tga)
11929 {
11930 p = build_tls_get_addr_head (htab, stub_entry, p);
11931 if (r != NULL)
11932 r[0].r_offset += p - loc;
11933 }
11934 p = build_plt_stub (htab, stub_entry, p, off, r);
11935 if (is_tga)
11936 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
721956f4
AM
11937 break;
11938
a4b6fadd 11939 case ppc_stub_save_res:
0a1b45a2 11940 return true;
a4b6fadd 11941
721956f4
AM
11942 default:
11943 BFD_FAIL ();
0a1b45a2 11944 return false;
721956f4
AM
11945 }
11946
1aa42141 11947 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11948
e7d1c40c 11949 if (htab->params->emit_stub_syms)
97b639ba
AM
11950 {
11951 struct elf_link_hash_entry *h;
ee75fd95
AM
11952 size_t len1, len2;
11953 char *name;
11954 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11955 "long_branch",
11956 "long_branch",
3f6ff479
AM
11957 "long_branch",
11958 "plt_branch",
ee75fd95 11959 "plt_branch",
05d0e962
AM
11960 "plt_branch",
11961 "plt_branch",
11962 "plt_call",
11963 "plt_call",
794e51c0 11964 "plt_call",
ee75fd95
AM
11965 "plt_call" };
11966
11967 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11968 len2 = strlen (stub_entry->root.string);
11969 name = bfd_malloc (len1 + len2 + 2);
11970 if (name == NULL)
0a1b45a2 11971 return false;
ee75fd95
AM
11972 memcpy (name, stub_entry->root.string, 9);
11973 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11974 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
0a1b45a2 11975 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
97b639ba 11976 if (h == NULL)
0a1b45a2 11977 return false;
97b639ba
AM
11978 if (h->root.type == bfd_link_hash_new)
11979 {
11980 h->root.type = bfd_link_hash_defined;
6f20ed8a 11981 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11982 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11983 h->ref_regular = 1;
11984 h->def_regular = 1;
11985 h->ref_regular_nonweak = 1;
11986 h->forced_local = 1;
11987 h->non_elf = 0;
2ec55de3 11988 h->root.linker_def = 1;
97b639ba
AM
11989 }
11990 }
11991
0a1b45a2 11992 return true;
721956f4
AM
11993}
11994
11995/* As above, but don't actually build the stub. Just bump offset so
11996 we know stub section sizes, and select plt_branch stubs where
11997 long_branch stubs won't do. */
11998
0a1b45a2 11999static bool
4ce794b7 12000ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
12001{
12002 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 12003 struct bfd_link_info *info;
721956f4 12004 struct ppc_link_hash_table *htab;
f891966f
AM
12005 asection *plt;
12006 bfd_vma targ, off, r2off;
04bdff6a 12007 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
12008
12009 /* Massage our args to the form they really have. */
12010 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
12011 info = in_arg;
12012
12013 htab = ppc_hash_table (info);
4dfe6ac6 12014 if (htab == NULL)
0a1b45a2 12015 return false;
721956f4 12016
abf874aa
CL
12017 /* Fail if the target section could not be assigned to an output
12018 section. The user should fix his linker script. */
12019 if (stub_entry->target_section != NULL
12020 && stub_entry->target_section->output_section == NULL
12021 && info->non_contiguous_regions)
53215f21
CL
12022 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12023 "Retry without --enable-non-contiguous-regions.\n"),
12024 stub_entry->target_section);
abf874aa
CL
12025
12026 /* Same for the group. */
12027 if (stub_entry->group->stub_sec != NULL
12028 && stub_entry->group->stub_sec->output_section == NULL
12029 && info->non_contiguous_regions)
53215f21
CL
12030 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12031 "output section. Retry without "
12032 "--enable-non-contiguous-regions.\n"),
12033 stub_entry->group->stub_sec,
12034 stub_entry->target_section);
abf874aa 12035
1aa42141
AM
12036 /* Make a note of the offset within the stubs for this entry. */
12037 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12038
a4b6fadd
AM
12039 if (stub_entry->h != NULL
12040 && stub_entry->h->save_res
12041 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12042 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12043 {
12044 /* Don't make stubs to out-of-line register save/restore
12045 functions. Instead, emit copies of the functions. */
12046 stub_entry->group->needs_save_res = 1;
12047 stub_entry->stub_type = ppc_stub_save_res;
0a1b45a2 12048 return true;
a4b6fadd
AM
12049 }
12050
f891966f 12051 switch (stub_entry->stub_type)
721956f4 12052 {
f891966f
AM
12053 case ppc_stub_plt_branch:
12054 case ppc_stub_plt_branch_r2off:
12055 /* Reset the stub type from the plt branch variant in case we now
12056 can reach with a shorter stub. */
12057 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12058 /* Fall through. */
12059 case ppc_stub_long_branch:
12060 case ppc_stub_long_branch_r2off:
1aa42141
AM
12061 targ = (stub_entry->target_value
12062 + stub_entry->target_section->output_offset
12063 + stub_entry->target_section->output_section->vma);
f891966f 12064 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
12065 off = (stub_entry->stub_offset
12066 + stub_entry->group->stub_sec->output_offset
12067 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12068
ad8e1ba5 12069 size = 4;
f891966f 12070 r2off = 0;
ad8e1ba5
AM
12071 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12072 {
25f53a85 12073 r2off = get_r2off (info, stub_entry);
a7c49797 12074 if (r2off == (bfd_vma) -1)
aa374f67 12075 {
0a1b45a2
AM
12076 htab->stub_error = true;
12077 return false;
aa374f67 12078 }
a7c49797 12079 size = 8;
ac2df442 12080 if (PPC_HA (r2off) != 0)
a7c49797
AM
12081 size += 4;
12082 if (PPC_LO (r2off) != 0)
12083 size += 4;
1aa42141 12084 off += size - 4;
ad8e1ba5 12085 }
1aa42141 12086 off = targ - off;
ad8e1ba5 12087
f891966f
AM
12088 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12089 Do the same for -R objects without function descriptors. */
12090 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12091 && r2off == 0
12092 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12093 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12094 {
f891966f 12095 struct ppc_branch_hash_entry *br_entry;
df136d64 12096
f891966f
AM
12097 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12098 stub_entry->root.string + 9,
0a1b45a2 12099 true, false);
f891966f 12100 if (br_entry == NULL)
721956f4 12101 {
f891966f
AM
12102 _bfd_error_handler (_("can't build branch stub `%s'"),
12103 stub_entry->root.string);
0a1b45a2
AM
12104 htab->stub_error = true;
12105 return false;
721956f4
AM
12106 }
12107
f891966f 12108 if (br_entry->iter != htab->stub_iteration)
721956f4 12109 {
f891966f
AM
12110 br_entry->iter = htab->stub_iteration;
12111 br_entry->offset = htab->brlt->size;
12112 htab->brlt->size += 8;
63bc6f6c 12113
f891966f
AM
12114 if (htab->relbrlt != NULL)
12115 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12116 else if (info->emitrelocations)
84f5d08e 12117 {
f891966f
AM
12118 htab->brlt->reloc_count += 1;
12119 htab->brlt->flags |= SEC_RELOC;
05d0e962 12120 }
f891966f 12121 }
ac2df442 12122
f891966f
AM
12123 targ = (br_entry->offset
12124 + htab->brlt->output_offset
12125 + htab->brlt->output_section->vma);
12126 off = (elf_gp (info->output_bfd)
12127 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12128 off = targ - off;
176a0d42 12129
f891966f
AM
12130 if (info->emitrelocations)
12131 {
12132 stub_entry->group->stub_sec->reloc_count
12133 += 1 + (PPC_HA (off) != 0);
12134 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12135 }
05d0e962 12136
f891966f
AM
12137 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12138 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12139 {
12140 size = 12;
12141 if (PPC_HA (off) != 0)
12142 size = 16;
ac2df442 12143 }
f891966f 12144 else
ac2df442 12145 {
f891966f
AM
12146 size = 16;
12147 if (PPC_HA (off) != 0)
12148 size += 4;
12149
12150 if (PPC_HA (r2off) != 0)
12151 size += 4;
12152 if (PPC_LO (r2off) != 0)
12153 size += 4;
ac2df442 12154 }
721956f4 12155 }
f891966f
AM
12156 else if (info->emitrelocations)
12157 {
12158 stub_entry->group->stub_sec->reloc_count += 1;
12159 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12160 }
12161 break;
12162
12163 case ppc_stub_plt_branch_notoc:
12164 case ppc_stub_plt_branch_both:
12165 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12166 /* Fall through. */
12167 case ppc_stub_long_branch_notoc:
12168 case ppc_stub_long_branch_both:
12169 off = (stub_entry->stub_offset
12170 + stub_entry->group->stub_sec->output_offset
12171 + stub_entry->group->stub_sec->output_section->vma);
12172 size = 0;
12173 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12174 size = 4;
12175 off += size;
12176 targ = (stub_entry->target_value
12177 + stub_entry->target_section->output_offset
12178 + stub_entry->target_section->output_section->vma);
04bdff6a 12179 odd = off & 4;
f891966f
AM
12180 off = targ - off;
12181
3d58e1fc
AM
12182 if (info->emitrelocations)
12183 {
04bdff6a 12184 unsigned int num_rel;
e10a07b3 12185 if (htab->params->power10_stubs != 0)
7c1f4227 12186 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12187 else
12188 num_rel = num_relocs_for_offset (off - 8);
12189 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12190 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12191 }
12192
e10a07b3 12193 if (htab->params->power10_stubs != 0)
7c1f4227 12194 extra = size_power10_offset (off, odd);
04bdff6a
AM
12195 else
12196 extra = size_offset (off - 8);
f891966f
AM
12197 /* Include branch insn plus those in the offset sequence. */
12198 size += 4 + extra;
12199 /* The branch insn is at the end, or "extra" bytes along. So
12200 its offset will be "extra" bytes less that that already
12201 calculated. */
12202 off -= extra;
12203
e10a07b3 12204 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12205 {
12206 /* After the bcl, lr has been modified so we need to emit
12207 .eh_frame info saying the return address is in r12. */
12208 lr_used = stub_entry->stub_offset + 8;
12209 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12210 lr_used += 4;
12211 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12212 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12213 DW_CFA_restore_extended 65. */
12214 delta = lr_used - stub_entry->group->lr_restore;
12215 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12216 stub_entry->group->lr_restore = lr_used + 8;
12217 }
f891966f
AM
12218
12219 /* If the branch can't reach, use a plt_branch. */
12220 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12221 {
12222 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12223 - ppc_stub_long_branch_notoc);
12224 size += 4;
12225 }
3d58e1fc
AM
12226 else if (info->emitrelocations)
12227 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
12228 break;
12229
12230 case ppc_stub_plt_call_notoc:
12231 case ppc_stub_plt_call_both:
29433886 12232 lr_used = 0;
12cf8b93
AM
12233 if (stub_entry->h != NULL
12234 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12235 && htab->params->tls_get_addr_opt)
12236 {
12237 lr_used += 7 * 4;
12238 if (!htab->params->no_tls_get_addr_regsave)
12239 lr_used += 11 * 4;
12240 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12241 lr_used += 2 * 4;
12242 }
f891966f 12243 if (stub_entry->stub_type == ppc_stub_plt_call_both)
29433886 12244 lr_used += 4;
f891966f
AM
12245 targ = stub_entry->plt_ent->plt.offset & ~1;
12246 if (targ >= (bfd_vma) -2)
12247 abort ();
12248
12249 plt = htab->elf.splt;
30845f11 12250 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12251 {
12252 if (stub_entry->symtype == STT_GNU_IFUNC)
12253 plt = htab->elf.iplt;
12254 else
12255 plt = htab->pltlocal;
12256 }
12257 targ += plt->output_offset + plt->output_section->vma;
29433886
AM
12258 off = (stub_entry->stub_offset
12259 + stub_entry->group->stub_sec->output_offset
12260 + stub_entry->group->stub_sec->output_section->vma
12261 + lr_used);
04bdff6a 12262 odd = off & 4;
f891966f
AM
12263 off = targ - off;
12264
12265 if (htab->params->plt_stub_align != 0)
12266 {
29433886 12267 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
f891966f
AM
12268
12269 stub_entry->group->stub_sec->size += pad;
12270 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12271 off -= pad;
29433886 12272 odd ^= pad & 4;
f891966f
AM
12273 }
12274
3d58e1fc
AM
12275 if (info->emitrelocations)
12276 {
04bdff6a 12277 unsigned int num_rel;
e10a07b3 12278 if (htab->params->power10_stubs != 0)
7c1f4227 12279 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12280 else
12281 num_rel = num_relocs_for_offset (off - 8);
12282 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12283 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12284 }
12285
29433886 12286 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12287
e10a07b3 12288 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12289 {
12290 /* After the bcl, lr has been modified so we need to emit
12291 .eh_frame info saying the return address is in r12. */
29433886 12292 lr_used += stub_entry->stub_offset + 8;
04bdff6a
AM
12293 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12294 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12295 DW_CFA_restore_extended 65. */
12296 delta = lr_used - stub_entry->group->lr_restore;
12297 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12298 stub_entry->group->lr_restore = lr_used + 8;
12299 }
29433886
AM
12300 if ((stub_entry->stub_type == ppc_stub_plt_call_notoc
12301 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 12302 && stub_entry->h != NULL
29433886
AM
12303 && is_tls_get_addr (&stub_entry->h->elf, htab)
12304 && htab->params->tls_get_addr_opt)
12305 {
12306 if (!htab->params->no_tls_get_addr_regsave)
12307 {
12308 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12309 delta = cfa_updt - stub_entry->group->lr_restore;
12310 stub_entry->group->eh_size += eh_advance_size (delta);
12311 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12312 stub_entry->group->lr_restore
12313 = stub_entry->stub_offset + size - 4;
12314 }
12315 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12316 {
12317 lr_used = stub_entry->stub_offset + size - 20;
12318 delta = lr_used - stub_entry->group->lr_restore;
12319 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12320 stub_entry->group->lr_restore
12321 = stub_entry->stub_offset + size - 4;
12322 }
12323 }
f891966f
AM
12324 break;
12325
12326 case ppc_stub_plt_call:
12327 case ppc_stub_plt_call_r2save:
12328 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12329 if (targ >= (bfd_vma) -2)
12330 abort ();
12331 plt = htab->elf.splt;
30845f11 12332 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12333 {
12334 if (stub_entry->symtype == STT_GNU_IFUNC)
12335 plt = htab->elf.iplt;
12336 else
12337 plt = htab->pltlocal;
12338 }
12339 targ += plt->output_offset + plt->output_section->vma;
12340
12341 off = (elf_gp (info->output_bfd)
12342 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12343 off = targ - off;
12344
12345 if (htab->params->plt_stub_align != 0)
12346 {
29433886 12347 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
f891966f
AM
12348
12349 stub_entry->group->stub_sec->size += pad;
12350 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12351 }
12352
12353 if (info->emitrelocations)
12354 {
12355 stub_entry->group->stub_sec->reloc_count
12356 += ((PPC_HA (off) != 0)
12357 + (htab->opd_abi
12358 ? 2 + (htab->params->plt_static_chain
12359 && PPC_HA (off + 16) == PPC_HA (off))
12360 : 1));
12361 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12362 }
12363
29433886 12364 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12365
12366 if (stub_entry->h != NULL
ed7007c1 12367 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f
AM
12368 && htab->params->tls_get_addr_opt
12369 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12370 {
29433886
AM
12371 if (!htab->params->no_tls_get_addr_regsave)
12372 {
12373 /* Adjustments to r1 need to be described. */
12374 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12375 delta = cfa_updt - stub_entry->group->lr_restore;
12376 stub_entry->group->eh_size += eh_advance_size (delta);
12377 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12378 }
12379 else
9e7028aa
AM
12380 {
12381 lr_used = stub_entry->stub_offset + size - 20;
12382 /* The eh_frame info will consist of a DW_CFA_advance_loc
12383 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12384 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12385 delta = lr_used - stub_entry->group->lr_restore;
12386 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12387 }
29433886 12388 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
f891966f
AM
12389 }
12390 break;
12391
12392 default:
12393 BFD_FAIL ();
0a1b45a2 12394 return false;
721956f4
AM
12395 }
12396
6f20ed8a 12397 stub_entry->group->stub_sec->size += size;
0a1b45a2 12398 return true;
721956f4
AM
12399}
12400
12401/* Set up various things so that we can make a list of input sections
12402 for each output section included in the link. Returns -1 on error,
cedb70c5 12403 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12404
12405int
e7d1c40c 12406ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12407{
6f20ed8a 12408 unsigned int id;
986f0783 12409 size_t amt;
721956f4
AM
12410 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12411
4dfe6ac6
NC
12412 if (htab == NULL)
12413 return -1;
4c52953f 12414
7cf7fcc8 12415 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12416 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12417 htab->sec_info = bfd_zmalloc (amt);
12418 if (htab->sec_info == NULL)
721956f4
AM
12419 return -1;
12420
3d6f9012
AM
12421 /* Set toc_off for com, und, abs and ind sections. */
12422 for (id = 0; id < 3; id++)
6f20ed8a 12423 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12424
721956f4
AM
12425 return 1;
12426}
12427
927be08e
AM
12428/* Set up for first pass at multitoc partitioning. */
12429
12430void
12431ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12432{
12433 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12434
1c865ab2 12435 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12436 htab->toc_bfd = NULL;
12437 htab->toc_first_sec = NULL;
12438}
12439
e717da7e
AM
12440/* The linker repeatedly calls this function for each TOC input section
12441 and linker generated GOT section. Group input bfds such that the toc
927be08e 12442 within a group is less than 64k in size. */
ad8e1ba5 12443
0a1b45a2 12444bool
4ce794b7 12445ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12446{
12447 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12448 bfd_vma addr, off, limit;
ad8e1ba5 12449
4dfe6ac6 12450 if (htab == NULL)
0a1b45a2 12451 return false;
4dfe6ac6 12452
927be08e 12453 if (!htab->second_toc_pass)
4c52953f 12454 {
927be08e 12455 /* Keep track of the first .toc or .got section for this input bfd. */
0a1b45a2 12456 bool new_bfd = htab->toc_bfd != isec->owner;
a4fd3de5
AM
12457
12458 if (new_bfd)
bf102f86
AM
12459 {
12460 htab->toc_bfd = isec->owner;
12461 htab->toc_first_sec = isec;
12462 }
927be08e 12463
bf102f86
AM
12464 addr = isec->output_offset + isec->output_section->vma;
12465 off = addr - htab->toc_curr;
d77c8a4b
AM
12466 limit = 0x80008000;
12467 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12468 limit = 0x10000;
12469 if (off + isec->size > limit)
bf102f86
AM
12470 {
12471 addr = (htab->toc_first_sec->output_offset
12472 + htab->toc_first_sec->output_section->vma);
12473 htab->toc_curr = addr;
a27e685f 12474 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12475 }
99877b66 12476
927be08e
AM
12477 /* toc_curr is the base address of this toc group. Set elf_gp
12478 for the input section to be the offset relative to the
12479 output toc base plus 0x8000. Making the input elf_gp an
12480 offset allows us to move the toc as a whole without
12481 recalculating input elf_gp. */
06bcf541 12482 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12483 off += TOC_BASE_OFF;
12484
12485 /* Die if someone uses a linker script that doesn't keep input
12486 file .toc and .got together. */
a4fd3de5
AM
12487 if (new_bfd
12488 && elf_gp (isec->owner) != 0
927be08e 12489 && elf_gp (isec->owner) != off)
0a1b45a2 12490 return false;
927be08e
AM
12491
12492 elf_gp (isec->owner) = off;
0a1b45a2 12493 return true;
4c52953f 12494 }
927be08e
AM
12495
12496 /* During the second pass toc_first_sec points to the start of
12497 a toc group, and toc_curr is used to track the old elf_gp.
12498 We use toc_bfd to ensure we only look at each bfd once. */
12499 if (htab->toc_bfd == isec->owner)
0a1b45a2 12500 return true;
927be08e
AM
12501 htab->toc_bfd = isec->owner;
12502
12503 if (htab->toc_first_sec == NULL
12504 || htab->toc_curr != elf_gp (isec->owner))
12505 {
12506 htab->toc_curr = elf_gp (isec->owner);
12507 htab->toc_first_sec = isec;
12508 }
12509 addr = (htab->toc_first_sec->output_offset
12510 + htab->toc_first_sec->output_section->vma);
06bcf541 12511 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12512 elf_gp (isec->owner) = off;
12513
0a1b45a2 12514 return true;
ad8e1ba5
AM
12515}
12516
927be08e
AM
12517/* Called via elf_link_hash_traverse to merge GOT entries for global
12518 symbol H. */
12519
0a1b45a2 12520static bool
927be08e
AM
12521merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12522{
12523 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12524 return true;
927be08e 12525
927be08e
AM
12526 merge_got_entries (&h->got.glist);
12527
0a1b45a2 12528 return true;
927be08e
AM
12529}
12530
12531/* Called via elf_link_hash_traverse to allocate GOT entries for global
12532 symbol H. */
12533
0a1b45a2 12534static bool
927be08e
AM
12535reallocate_got (struct elf_link_hash_entry *h, void *inf)
12536{
12537 struct got_entry *gent;
12538
12539 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 12540 return true;
927be08e 12541
927be08e
AM
12542 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12543 if (!gent->is_indirect)
12544 allocate_got (h, (struct bfd_link_info *) inf, gent);
0a1b45a2 12545 return true;
927be08e
AM
12546}
12547
12548/* Called on the first multitoc pass after the last call to
12549 ppc64_elf_next_toc_section. This function removes duplicate GOT
12550 entries. */
12551
0a1b45a2 12552bool
927be08e 12553ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12554{
12555 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e 12556 struct bfd *ibfd, *ibfd2;
0a1b45a2 12557 bool done_something;
927be08e
AM
12558
12559 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12560
7865406b 12561 if (!htab->do_multi_toc)
0a1b45a2 12562 return false;
7865406b 12563
d0fae19d 12564 /* Merge global sym got entries within a toc group. */
927be08e
AM
12565 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12566
12567 /* And tlsld_got. */
c72f2fb2 12568 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12569 {
12570 struct got_entry *ent, *ent2;
12571
12572 if (!is_ppc64_elf (ibfd))
12573 continue;
12574
12575 ent = ppc64_tlsld_got (ibfd);
12576 if (!ent->is_indirect
12577 && ent->got.offset != (bfd_vma) -1)
12578 {
c72f2fb2 12579 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12580 {
12581 if (!is_ppc64_elf (ibfd2))
12582 continue;
12583
12584 ent2 = ppc64_tlsld_got (ibfd2);
12585 if (!ent2->is_indirect
12586 && ent2->got.offset != (bfd_vma) -1
12587 && elf_gp (ibfd2) == elf_gp (ibfd))
12588 {
0a1b45a2 12589 ent2->is_indirect = true;
927be08e
AM
12590 ent2->got.ent = ent;
12591 }
12592 }
12593 }
12594 }
12595
12596 /* Zap sizes of got sections. */
33e44f2e
AM
12597 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12598 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12599 htab->got_reli_size = 0;
12600
c72f2fb2 12601 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12602 {
12603 asection *got, *relgot;
12604
12605 if (!is_ppc64_elf (ibfd))
12606 continue;
12607
12608 got = ppc64_elf_tdata (ibfd)->got;
12609 if (got != NULL)
12610 {
12611 got->rawsize = got->size;
12612 got->size = 0;
12613 relgot = ppc64_elf_tdata (ibfd)->relgot;
12614 relgot->rawsize = relgot->size;
12615 relgot->size = 0;
12616 }
12617 }
12618
12619 /* Now reallocate the got, local syms first. We don't need to
12620 allocate section contents again since we never increase size. */
c72f2fb2 12621 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12622 {
12623 struct got_entry **lgot_ents;
12624 struct got_entry **end_lgot_ents;
12625 struct plt_entry **local_plt;
12626 struct plt_entry **end_local_plt;
f961d9dd 12627 unsigned char *lgot_masks;
927be08e
AM
12628 bfd_size_type locsymcount;
12629 Elf_Internal_Shdr *symtab_hdr;
19e08130 12630 asection *s;
927be08e
AM
12631
12632 if (!is_ppc64_elf (ibfd))
12633 continue;
12634
12635 lgot_ents = elf_local_got_ents (ibfd);
12636 if (!lgot_ents)
12637 continue;
12638
12639 symtab_hdr = &elf_symtab_hdr (ibfd);
12640 locsymcount = symtab_hdr->sh_info;
12641 end_lgot_ents = lgot_ents + locsymcount;
12642 local_plt = (struct plt_entry **) end_lgot_ents;
12643 end_local_plt = local_plt + locsymcount;
f961d9dd 12644 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12645 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12646 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12647 {
12648 struct got_entry *ent;
12649
12650 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12651 {
19e08130
AM
12652 unsigned int ent_size = 8;
12653 unsigned int rel_size = sizeof (Elf64_External_Rela);
12654
d0fae19d
AM
12655 ent->got.offset = s->size;
12656 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12657 {
19e08130
AM
12658 ent_size *= 2;
12659 rel_size *= 2;
12660 }
12661 s->size += ent_size;
37da22e5 12662 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12663 {
33e44f2e 12664 htab->elf.irelplt->size += rel_size;
19e08130
AM
12665 htab->got_reli_size += rel_size;
12666 }
6a3858a6 12667 else if (bfd_link_pic (info)
f749f26e 12668 && !(ent->tls_type != 0
6a3858a6 12669 && bfd_link_executable (info)))
19e08130
AM
12670 {
12671 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12672 srel->size += rel_size;
d0fae19d
AM
12673 }
12674 }
927be08e
AM
12675 }
12676 }
12677
12678 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12679
c72f2fb2 12680 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12681 {
12682 struct got_entry *ent;
12683
12684 if (!is_ppc64_elf (ibfd))
12685 continue;
12686
12687 ent = ppc64_tlsld_got (ibfd);
12688 if (!ent->is_indirect
12689 && ent->got.offset != (bfd_vma) -1)
12690 {
12691 asection *s = ppc64_elf_tdata (ibfd)->got;
12692 ent->got.offset = s->size;
12693 s->size += 16;
f749f26e 12694 if (bfd_link_dll (info))
927be08e
AM
12695 {
12696 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12697 srel->size += sizeof (Elf64_External_Rela);
12698 }
12699 }
12700 }
12701
33e44f2e 12702 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12703 if (!done_something)
c72f2fb2 12704 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12705 {
12706 asection *got;
12707
12708 if (!is_ppc64_elf (ibfd))
12709 continue;
12710
12711 got = ppc64_elf_tdata (ibfd)->got;
12712 if (got != NULL)
12713 {
12714 done_something = got->rawsize != got->size;
12715 if (done_something)
12716 break;
12717 }
12718 }
12719
12720 if (done_something)
e7d1c40c 12721 (*htab->params->layout_sections_again) ();
927be08e
AM
12722
12723 /* Set up for second pass over toc sections to recalculate elf_gp
12724 on input sections. */
12725 htab->toc_bfd = NULL;
12726 htab->toc_first_sec = NULL;
0a1b45a2 12727 htab->second_toc_pass = true;
927be08e
AM
12728 return done_something;
12729}
12730
12731/* Called after second pass of multitoc partitioning. */
12732
12733void
12734ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12735{
12736 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12737
12738 /* After the second pass, toc_curr tracks the TOC offset used
12739 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12740 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12741}
12742
9b5ecbd0
AM
12743/* No toc references were found in ISEC. If the code in ISEC makes no
12744 calls, then there's no need to use toc adjusting stubs when branching
12745 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12746 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12747 needed, and 2 if a cyclical call-graph was found but no other reason
12748 for a stub was detected. If called from the top level, a return of
12749 2 means the same as a return of 0. */
9b5ecbd0
AM
12750
12751static int
4ce794b7 12752toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12753{
9b5ecbd0 12754 int ret;
70cc837d
AM
12755
12756 /* Mark this section as checked. */
12757 isec->call_check_done = 1;
9b5ecbd0 12758
772119ce
AM
12759 /* We know none of our code bearing sections will need toc stubs. */
12760 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12761 return 0;
12762
eea6121a 12763 if (isec->size == 0)
082c50f8
AM
12764 return 0;
12765
4c52953f
AM
12766 if (isec->output_section == NULL)
12767 return 0;
12768
4c52953f 12769 ret = 0;
70cc837d 12770 if (isec->reloc_count != 0)
9b5ecbd0 12771 {
70cc837d
AM
12772 Elf_Internal_Rela *relstart, *rel;
12773 Elf_Internal_Sym *local_syms;
12774 struct ppc_link_hash_table *htab;
2917689a 12775
70cc837d
AM
12776 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12777 info->keep_memory);
12778 if (relstart == NULL)
12779 return -1;
90aecf7a 12780
70cc837d
AM
12781 /* Look for branches to outside of this section. */
12782 local_syms = NULL;
12783 htab = ppc_hash_table (info);
12784 if (htab == NULL)
12785 return -1;
4c52953f 12786
70cc837d 12787 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12788 {
70cc837d
AM
12789 enum elf_ppc64_reloc_type r_type;
12790 unsigned long r_symndx;
12791 struct elf_link_hash_entry *h;
12792 struct ppc_link_hash_entry *eh;
12793 Elf_Internal_Sym *sym;
12794 asection *sym_sec;
12795 struct _opd_sec_data *opd;
12796 bfd_vma sym_value;
12797 bfd_vma dest;
12798
12799 r_type = ELF64_R_TYPE (rel->r_info);
12800 if (r_type != R_PPC64_REL24
05d0e962 12801 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12802 && r_type != R_PPC64_REL14
12803 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12804 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12805 && r_type != R_PPC64_PLTCALL
12806 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12807 continue;
4c52953f 12808
70cc837d
AM
12809 r_symndx = ELF64_R_SYM (rel->r_info);
12810 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12811 isec->owner))
4c52953f 12812 {
70cc837d
AM
12813 ret = -1;
12814 break;
12815 }
4c52953f 12816
70cc837d
AM
12817 /* Calls to dynamic lib functions go through a plt call stub
12818 that uses r2. */
ed7007c1 12819 eh = ppc_elf_hash_entry (h);
70cc837d
AM
12820 if (eh != NULL
12821 && (eh->elf.plt.plist != NULL
12822 || (eh->oh != NULL
12823 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12824 {
12825 ret = 1;
12826 break;
4c52953f
AM
12827 }
12828
70cc837d
AM
12829 if (sym_sec == NULL)
12830 /* Ignore other undefined symbols. */
4c52953f 12831 continue;
4c52953f 12832
70cc837d
AM
12833 /* Assume branches to other sections not included in the
12834 link need stubs too, to cover -R and absolute syms. */
12835 if (sym_sec->output_section == NULL)
12836 {
12837 ret = 1;
12838 break;
12839 }
4c52953f 12840
70cc837d
AM
12841 if (h == NULL)
12842 sym_value = sym->st_value;
12843 else
12844 {
12845 if (h->root.type != bfd_link_hash_defined
12846 && h->root.type != bfd_link_hash_defweak)
12847 abort ();
12848 sym_value = h->root.u.def.value;
12849 }
12850 sym_value += rel->r_addend;
4c52953f 12851
70cc837d
AM
12852 /* If this branch reloc uses an opd sym, find the code section. */
12853 opd = get_opd_info (sym_sec);
12854 if (opd != NULL)
12855 {
12856 if (h == NULL && opd->adjust != NULL)
12857 {
12858 long adjust;
4c52953f 12859
92a9c616 12860 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12861 if (adjust == -1)
12862 /* Assume deleted functions won't ever be called. */
12863 continue;
12864 sym_value += adjust;
12865 }
4c52953f 12866
aef36ac1 12867 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 12868 &sym_sec, NULL, false);
70cc837d
AM
12869 if (dest == (bfd_vma) -1)
12870 continue;
12871 }
12872 else
12873 dest = (sym_value
12874 + sym_sec->output_offset
12875 + sym_sec->output_section->vma);
4c52953f 12876
70cc837d
AM
12877 /* Ignore branch to self. */
12878 if (sym_sec == isec)
12879 continue;
4c52953f 12880
70cc837d
AM
12881 /* If the called function uses the toc, we need a stub. */
12882 if (sym_sec->has_toc_reloc
12883 || sym_sec->makes_toc_func_call)
4c52953f 12884 {
70cc837d 12885 ret = 1;
4c52953f
AM
12886 break;
12887 }
70cc837d
AM
12888
12889 /* Assume any branch that needs a long branch stub might in fact
12890 need a plt_branch stub. A plt_branch stub uses r2. */
12891 else if (dest - (isec->output_offset
12892 + isec->output_section->vma
6911b7dc
AM
12893 + rel->r_offset) + (1 << 25)
12894 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12895 ? h->other
12896 : sym->st_other))
4c52953f 12897 {
70cc837d
AM
12898 ret = 1;
12899 break;
12900 }
12901
12902 /* If calling back to a section in the process of being
12903 tested, we can't say for sure that no toc adjusting stubs
12904 are needed, so don't return zero. */
12905 else if (sym_sec->call_check_in_progress)
12906 ret = 2;
12907
12908 /* Branches to another section that itself doesn't have any TOC
12909 references are OK. Recursively call ourselves to check. */
12910 else if (!sym_sec->call_check_done)
12911 {
12912 int recur;
12913
12914 /* Mark current section as indeterminate, so that other
12915 sections that call back to current won't be marked as
12916 known. */
12917 isec->call_check_in_progress = 1;
12918 recur = toc_adjusting_stub_needed (info, sym_sec);
12919 isec->call_check_in_progress = 0;
12920
4c52953f
AM
12921 if (recur != 0)
12922 {
70cc837d
AM
12923 ret = recur;
12924 if (recur != 2)
12925 break;
4c52953f
AM
12926 }
12927 }
4c52953f 12928 }
70cc837d 12929
c9594989
AM
12930 if (elf_symtab_hdr (isec->owner).contents
12931 != (unsigned char *) local_syms)
70cc837d
AM
12932 free (local_syms);
12933 if (elf_section_data (isec)->relocs != relstart)
12934 free (relstart);
9b5ecbd0
AM
12935 }
12936
70cc837d
AM
12937 if ((ret & 1) == 0
12938 && isec->map_head.s != NULL
12939 && (strcmp (isec->output_section->name, ".init") == 0
12940 || strcmp (isec->output_section->name, ".fini") == 0))
12941 {
12942 if (isec->map_head.s->has_toc_reloc
12943 || isec->map_head.s->makes_toc_func_call)
12944 ret = 1;
12945 else if (!isec->map_head.s->call_check_done)
12946 {
12947 int recur;
12948 isec->call_check_in_progress = 1;
12949 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12950 isec->call_check_in_progress = 0;
12951 if (recur != 0)
12952 ret = recur;
12953 }
12954 }
12955
12956 if (ret == 1)
12957 isec->makes_toc_func_call = 1;
4c52953f 12958
9b5ecbd0
AM
12959 return ret;
12960}
12961
721956f4
AM
12962/* The linker repeatedly calls this function for each input section,
12963 in the order that input sections are linked into output sections.
12964 Build lists of input sections to determine groupings between which
12965 we may insert linker stubs. */
12966
0a1b45a2 12967bool
4ce794b7 12968ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12969{
12970 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12971
4dfe6ac6 12972 if (htab == NULL)
0a1b45a2 12973 return false;
4dfe6ac6 12974
734b6cf9 12975 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12976 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12977 {
3d6f9012
AM
12978 /* This happens to make the list in reverse order,
12979 which is what we want. */
6f20ed8a
AM
12980 htab->sec_info[isec->id].u.list
12981 = htab->sec_info[isec->output_section->id].u.list;
12982 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12983 }
ad8e1ba5 12984
4c52953f 12985 if (htab->multi_toc_needed)
9b5ecbd0 12986 {
8b974ba3
AM
12987 /* Analyse sections that aren't already flagged as needing a
12988 valid toc pointer. Exclude .fixup for the linux kernel.
12989 .fixup contains branches, but only back to the function that
12990 hit an exception. */
12991 if (!(isec->has_toc_reloc
12992 || (isec->flags & SEC_CODE) == 0
12993 || strcmp (isec->name, ".fixup") == 0
12994 || isec->call_check_done))
12995 {
12996 if (toc_adjusting_stub_needed (info, isec) < 0)
0a1b45a2 12997 return false;
8b974ba3
AM
12998 }
12999 /* Make all sections use the TOC assigned for this object file.
13000 This will be wrong for pasted sections; We fix that in
13001 check_pasted_section(). */
13002 if (elf_gp (isec->owner) != 0)
13003 htab->toc_curr = elf_gp (isec->owner);
13004 }
13005
6f20ed8a 13006 htab->sec_info[isec->id].toc_off = htab->toc_curr;
0a1b45a2 13007 return true;
721956f4
AM
13008}
13009
70cc837d
AM
13010/* Check that all .init and .fini sections use the same toc, if they
13011 have toc relocs. */
13012
0a1b45a2 13013static bool
70cc837d
AM
13014check_pasted_section (struct bfd_link_info *info, const char *name)
13015{
13016 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13017
13018 if (o != NULL)
13019 {
13020 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13021 bfd_vma toc_off = 0;
13022 asection *i;
13023
13024 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13025 if (i->has_toc_reloc)
13026 {
13027 if (toc_off == 0)
6f20ed8a
AM
13028 toc_off = htab->sec_info[i->id].toc_off;
13029 else if (toc_off != htab->sec_info[i->id].toc_off)
0a1b45a2 13030 return false;
70cc837d 13031 }
6683a28d
AM
13032
13033 if (toc_off == 0)
13034 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13035 if (i->makes_toc_func_call)
13036 {
6f20ed8a 13037 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13038 break;
13039 }
13040
70cc837d
AM
13041 /* Make sure the whole pasted function uses the same toc offset. */
13042 if (toc_off != 0)
13043 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13044 htab->sec_info[i->id].toc_off = toc_off;
70cc837d 13045 }
0a1b45a2 13046 return true;
70cc837d
AM
13047}
13048
0a1b45a2 13049bool
70cc837d
AM
13050ppc64_elf_check_init_fini (struct bfd_link_info *info)
13051{
13052 return (check_pasted_section (info, ".init")
13053 & check_pasted_section (info, ".fini"));
13054}
13055
721956f4
AM
13056/* See whether we can group stub sections together. Grouping stub
13057 sections may result in fewer stubs. More importantly, we need to
13058 put all .init* and .fini* stubs at the beginning of the .init or
13059 .fini output sections respectively, because glibc splits the
13060 _init and _fini functions into multiple parts. Putting a stub in
13061 the middle of a function is not a good idea. */
13062
0a1b45a2 13063static bool
6f20ed8a 13064group_sections (struct bfd_link_info *info,
4ce794b7 13065 bfd_size_type stub_group_size,
0a1b45a2 13066 bool stubs_always_before_branch)
721956f4 13067{
6f20ed8a
AM
13068 struct ppc_link_hash_table *htab;
13069 asection *osec;
0a1b45a2 13070 bool suppress_size_errors;
7c8fe5c4 13071
6f20ed8a
AM
13072 htab = ppc_hash_table (info);
13073 if (htab == NULL)
0a1b45a2 13074 return false;
6f20ed8a 13075
0a1b45a2 13076 suppress_size_errors = false;
7c8fe5c4
AM
13077 if (stub_group_size == 1)
13078 {
13079 /* Default values. */
13080 if (stubs_always_before_branch)
09f92717 13081 stub_group_size = 0x1e00000;
7c8fe5c4 13082 else
09f92717 13083 stub_group_size = 0x1c00000;
0a1b45a2 13084 suppress_size_errors = true;
7c8fe5c4
AM
13085 }
13086
6f20ed8a 13087 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13088 {
6f20ed8a
AM
13089 asection *tail;
13090
13091 if (osec->id >= htab->sec_info_arr_size)
13092 continue;
13093
13094 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13095 while (tail != NULL)
721956f4 13096 {
734b6cf9
AM
13097 asection *curr;
13098 asection *prev;
13099 bfd_size_type total;
0a1b45a2 13100 bool big_sec;
734b6cf9 13101 bfd_vma curr_toc;
6f20ed8a 13102 struct map_stub *group;
09f92717 13103 bfd_size_type group_size;
734b6cf9
AM
13104
13105 curr = tail;
eea6121a 13106 total = tail->size;
09f92717
AM
13107 group_size = (ppc64_elf_section_data (tail) != NULL
13108 && ppc64_elf_section_data (tail)->has_14bit_branch
13109 ? stub_group_size >> 10 : stub_group_size);
13110
13111 big_sec = total > group_size;
7c8fe5c4 13112 if (big_sec && !suppress_size_errors)
695344c0 13113 /* xgettext:c-format */
871b3ab2 13114 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13115 tail->owner, tail);
6f20ed8a 13116 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13117
6f20ed8a 13118 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13119 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13120 < (ppc64_elf_section_data (prev) != NULL
13121 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13122 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13123 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13124 curr = prev;
13125
13126 /* OK, the size from the start of CURR to the end is less
09f92717 13127 than group_size and thus can be handled by one stub
734b6cf9 13128 section. (or the tail section is itself larger than
09f92717
AM
13129 group_size, in which case we may be toast.) We should
13130 really be keeping track of the total size of stubs added
13131 here, as stubs contribute to the final output section
13132 size. That's a little tricky, and this way will only
13133 break if stubs added make the total size more than 2^25,
13134 ie. for the default stub_group_size, if stubs total more
13135 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13136 group = bfd_alloc (curr->owner, sizeof (*group));
13137 if (group == NULL)
0a1b45a2 13138 return false;
6f20ed8a
AM
13139 group->link_sec = curr;
13140 group->stub_sec = NULL;
a4b6fadd 13141 group->needs_save_res = 0;
df136d64
AM
13142 group->lr_restore = 0;
13143 group->eh_size = 0;
13144 group->eh_base = 0;
a4b6fadd
AM
13145 group->next = htab->group;
13146 htab->group = group;
734b6cf9 13147 do
721956f4 13148 {
6f20ed8a 13149 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13150 /* Set up this stub group. */
6f20ed8a 13151 htab->sec_info[tail->id].u.group = group;
721956f4 13152 }
734b6cf9
AM
13153 while (tail != curr && (tail = prev) != NULL);
13154
09f92717 13155 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13156 bytes before the stub section can be handled by it too.
13157 Don't do this if we have a really large section after the
13158 stubs, as adding more stubs increases the chance that
13159 branches may not reach into the stub section. */
13160 if (!stubs_always_before_branch && !big_sec)
13161 {
13162 total = 0;
13163 while (prev != NULL
13164 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13165 < (ppc64_elf_section_data (prev) != NULL
13166 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13167 ? (group_size = stub_group_size >> 10)
13168 : group_size))
6f20ed8a 13169 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13170 {
13171 tail = prev;
6f20ed8a
AM
13172 prev = htab->sec_info[tail->id].u.list;
13173 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13174 }
13175 }
13176 tail = prev;
721956f4
AM
13177 }
13178 }
0a1b45a2 13179 return true;
721956f4
AM
13180}
13181
58d180e8
AM
13182static const unsigned char glink_eh_frame_cie[] =
13183{
13184 0, 0, 0, 16, /* length. */
13185 0, 0, 0, 0, /* id. */
13186 1, /* CIE version. */
13187 'z', 'R', 0, /* Augmentation string. */
13188 4, /* Code alignment. */
13189 0x78, /* Data alignment. */
13190 65, /* RA reg. */
13191 1, /* Augmentation size. */
13192 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13193 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13194};
13195
d969d15f
AM
13196/* Stripping output sections is normally done before dynamic section
13197 symbols have been allocated. This function is called later, and
13198 handles cases like htab->brlt which is mapped to its own output
13199 section. */
13200
13201static void
13202maybe_strip_output (struct bfd_link_info *info, asection *isec)
13203{
13204 if (isec->size == 0
13205 && isec->output_section->size == 0
53d8967a 13206 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13207 && !bfd_section_removed_from_list (info->output_bfd,
13208 isec->output_section)
13209 && elf_section_data (isec->output_section)->dynindx == 0)
13210 {
13211 isec->output_section->flags |= SEC_EXCLUDE;
13212 bfd_section_list_remove (info->output_bfd, isec->output_section);
13213 info->output_bfd->section_count--;
13214 }
13215}
13216
721956f4
AM
13217/* Determine and set the size of the stub section for a final link.
13218
13219 The basic idea here is to examine all the relocations looking for
13220 PC-relative calls to a target that is unreachable with a "bl"
13221 instruction. */
13222
0a1b45a2 13223bool
e7d1c40c 13224ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13225{
13226 bfd_size_type stub_group_size;
0a1b45a2 13227 bool stubs_always_before_branch;
721956f4
AM
13228 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13229
4dfe6ac6 13230 if (htab == NULL)
0a1b45a2 13231 return false;
4dfe6ac6 13232
e10a07b3
AM
13233 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13234 htab->params->power10_stubs = 0;
13235
0e1862bb 13236 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13237 htab->params->plt_thread_safe = 1;
b9e5796b 13238 if (!htab->opd_abi)
e7d1c40c
AM
13239 htab->params->plt_thread_safe = 0;
13240 else if (htab->params->plt_thread_safe == -1)
794e51c0 13241 {
e2458743 13242 static const char *const thread_starter[] =
794e51c0
AM
13243 {
13244 "pthread_create",
13245 /* libstdc++ */
13246 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13247 /* librt */
13248 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13249 "mq_notify", "create_timer",
13250 /* libanl */
13251 "getaddrinfo_a",
13252 /* libgomp */
2300b5a1 13253 "GOMP_parallel",
794e51c0 13254 "GOMP_parallel_start",
2300b5a1 13255 "GOMP_parallel_loop_static",
794e51c0 13256 "GOMP_parallel_loop_static_start",
2300b5a1 13257 "GOMP_parallel_loop_dynamic",
794e51c0 13258 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13259 "GOMP_parallel_loop_guided",
794e51c0 13260 "GOMP_parallel_loop_guided_start",
2300b5a1 13261 "GOMP_parallel_loop_runtime",
794e51c0 13262 "GOMP_parallel_loop_runtime_start",
2300b5a1 13263 "GOMP_parallel_sections",
68ffbac6 13264 "GOMP_parallel_sections_start",
f9dffbf0
AM
13265 /* libgo */
13266 "__go_go",
794e51c0
AM
13267 };
13268 unsigned i;
13269
a4b6fadd 13270 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13271 {
13272 struct elf_link_hash_entry *h;
13273 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
0a1b45a2 13274 false, false, true);
e7d1c40c
AM
13275 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13276 if (htab->params->plt_thread_safe)
794e51c0
AM
13277 break;
13278 }
13279 }
e7d1c40c
AM
13280 stubs_always_before_branch = htab->params->group_size < 0;
13281 if (htab->params->group_size < 0)
13282 stub_group_size = -htab->params->group_size;
721956f4 13283 else
e7d1c40c 13284 stub_group_size = htab->params->group_size;
721956f4 13285
6f20ed8a 13286 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
0a1b45a2 13287 return false;
721956f4 13288
a804e476
AM
13289 htab->tga_group = NULL;
13290 if (!htab->params->no_tls_get_addr_regsave
13291 && htab->tga_desc_fd != NULL
13292 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13293 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13294 && htab->tls_get_addr_fd != NULL
13295 && is_static_defined (&htab->tls_get_addr_fd->elf))
13296 {
13297 asection *sym_sec, *code_sec, *stub_sec;
13298 bfd_vma sym_value;
13299 struct _opd_sec_data *opd;
13300
13301 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13302 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13303 code_sec = sym_sec;
13304 opd = get_opd_info (sym_sec);
13305 if (opd != NULL)
0a1b45a2 13306 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
a804e476
AM
13307 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13308 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13309 htab->tga_group->link_sec);
13310 if (stub_sec == NULL)
0a1b45a2 13311 return false;
a804e476
AM
13312 htab->tga_group->stub_sec = stub_sec;
13313
13314 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13315 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13316 htab->tga_desc_fd->elf.root.u.def.value = 0;
13317 htab->tga_desc_fd->elf.type = STT_FUNC;
13318 htab->tga_desc_fd->elf.def_regular = 1;
13319 htab->tga_desc_fd->elf.non_elf = 0;
0a1b45a2 13320 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
a804e476
AM
13321 }
13322
c9301e31
AM
13323#define STUB_SHRINK_ITER 20
13324 /* Loop until no stubs added. After iteration 20 of this loop we may
13325 exit on a stub section shrinking. This is to break out of a
13326 pathological case where adding stubs on one iteration decreases
13327 section gaps (perhaps due to alignment), which then requires
13328 fewer or smaller stubs on the next iteration. */
13329
721956f4
AM
13330 while (1)
13331 {
13332 bfd *input_bfd;
13333 unsigned int bfd_indx;
a4b6fadd 13334 struct map_stub *group;
721956f4
AM
13335
13336 htab->stub_iteration += 1;
721956f4
AM
13337
13338 for (input_bfd = info->input_bfds, bfd_indx = 0;
13339 input_bfd != NULL;
c72f2fb2 13340 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13341 {
13342 Elf_Internal_Shdr *symtab_hdr;
13343 asection *section;
6cdc0ccc 13344 Elf_Internal_Sym *local_syms = NULL;
721956f4 13345
0c8d6e5c 13346 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13347 continue;
13348
721956f4 13349 /* We'll need the symbol table in a second. */
0ffa91dd 13350 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13351 if (symtab_hdr->sh_info == 0)
13352 continue;
13353
721956f4
AM
13354 /* Walk over each section attached to the input bfd. */
13355 for (section = input_bfd->sections;
13356 section != NULL;
13357 section = section->next)
13358 {
721956f4 13359 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
13360
13361 /* If there aren't any relocs, then there's nothing more
13362 to do. */
13363 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13364 || (section->flags & SEC_ALLOC) == 0
13365 || (section->flags & SEC_LOAD) == 0
13366 || (section->flags & SEC_CODE) == 0
721956f4
AM
13367 || section->reloc_count == 0)
13368 continue;
13369
13370 /* If this section is a link-once section that will be
13371 discarded, then don't create any stubs. */
13372 if (section->output_section == NULL
927be08e 13373 || section->output_section->owner != info->output_bfd)
721956f4
AM
13374 continue;
13375
1e2f5b6e
AM
13376 /* Get the relocs. */
13377 internal_relocs
4ce794b7 13378 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13379 info->keep_memory);
721956f4 13380 if (internal_relocs == NULL)
1e2f5b6e 13381 goto error_ret_free_local;
721956f4
AM
13382
13383 /* Now examine each relocation. */
13384 irela = internal_relocs;
13385 irelaend = irela + section->reloc_count;
13386 for (; irela < irelaend; irela++)
13387 {
4ce794b7
AM
13388 enum elf_ppc64_reloc_type r_type;
13389 unsigned int r_indx;
721956f4
AM
13390 enum ppc_stub_type stub_type;
13391 struct ppc_stub_hash_entry *stub_entry;
8387904d 13392 asection *sym_sec, *code_sec;
e054468f 13393 bfd_vma sym_value, code_value;
721956f4 13394 bfd_vma destination;
6911b7dc 13395 unsigned long local_off;
0a1b45a2 13396 bool ok_dest;
721956f4 13397 struct ppc_link_hash_entry *hash;
8387904d 13398 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13399 struct elf_link_hash_entry *h;
13400 Elf_Internal_Sym *sym;
721956f4
AM
13401 char *stub_name;
13402 const asection *id_sec;
74f0fb50 13403 struct _opd_sec_data *opd;
e054468f 13404 struct plt_entry *plt_ent;
721956f4
AM
13405
13406 r_type = ELF64_R_TYPE (irela->r_info);
13407 r_indx = ELF64_R_SYM (irela->r_info);
13408
4ce794b7 13409 if (r_type >= R_PPC64_max)
721956f4
AM
13410 {
13411 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13412 goto error_ret_free_internal;
721956f4
AM
13413 }
13414
13415 /* Only look for stubs on branch instructions. */
4ce794b7 13416 if (r_type != R_PPC64_REL24
05d0e962 13417 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
13418 && r_type != R_PPC64_REL14
13419 && r_type != R_PPC64_REL14_BRTAKEN
13420 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
13421 continue;
13422
13423 /* Now determine the call target, its name, value,
13424 section. */
411e1bfb
AM
13425 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13426 r_indx, input_bfd))
13427 goto error_ret_free_internal;
ed7007c1 13428 hash = ppc_elf_hash_entry (h);
411e1bfb 13429
0a1b45a2 13430 ok_dest = false;
8387904d 13431 fdh = NULL;
7fe2b9a6 13432 sym_value = 0;
411e1bfb 13433 if (hash == NULL)
721956f4 13434 {
411e1bfb 13435 sym_value = sym->st_value;
c27b8c2a
AM
13436 if (sym_sec != NULL
13437 && sym_sec->output_section != NULL)
0a1b45a2 13438 ok_dest = true;
721956f4 13439 }
7fe2b9a6
AM
13440 else if (hash->elf.root.type == bfd_link_hash_defined
13441 || hash->elf.root.type == bfd_link_hash_defweak)
13442 {
13443 sym_value = hash->elf.root.u.def.value;
13444 if (sym_sec->output_section != NULL)
0a1b45a2 13445 ok_dest = true;
7fe2b9a6
AM
13446 }
13447 else if (hash->elf.root.type == bfd_link_hash_undefweak
13448 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13449 {
99877b66 13450 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13451 use the func descriptor sym instead if it is
13452 defined. */
ceb1f1ef 13453 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13454 && hash->oh != NULL)
8387904d 13455 {
8c5b4e52 13456 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13457 if (fdh->elf.root.type == bfd_link_hash_defined
13458 || fdh->elf.root.type == bfd_link_hash_defweak)
13459 {
13460 sym_sec = fdh->elf.root.u.def.section;
13461 sym_value = fdh->elf.root.u.def.value;
13462 if (sym_sec->output_section != NULL)
0a1b45a2 13463 ok_dest = true;
8387904d 13464 }
99877b66
AM
13465 else
13466 fdh = NULL;
8387904d 13467 }
7fe2b9a6
AM
13468 }
13469 else
13470 {
13471 bfd_set_error (bfd_error_bad_value);
13472 goto error_ret_free_internal;
721956f4
AM
13473 }
13474
8843416a 13475 destination = 0;
6911b7dc 13476 local_off = 0;
8843416a
AM
13477 if (ok_dest)
13478 {
13479 sym_value += irela->r_addend;
13480 destination = (sym_value
13481 + sym_sec->output_offset
13482 + sym_sec->output_section->vma);
6911b7dc
AM
13483 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13484 ? hash->elf.other
13485 : sym->st_other);
8843416a
AM
13486 }
13487
8387904d 13488 code_sec = sym_sec;
e054468f 13489 code_value = sym_value;
74f0fb50
AM
13490 opd = get_opd_info (sym_sec);
13491 if (opd != NULL)
8387904d
AM
13492 {
13493 bfd_vma dest;
13494
74f0fb50 13495 if (hash == NULL && opd->adjust != NULL)
8387904d 13496 {
51aecdc5 13497 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13498 if (adjust == -1)
13499 continue;
e054468f 13500 code_value += adjust;
8387904d
AM
13501 sym_value += adjust;
13502 }
13503 dest = opd_entry_value (sym_sec, sym_value,
0a1b45a2 13504 &code_sec, &code_value, false);
8387904d
AM
13505 if (dest != (bfd_vma) -1)
13506 {
13507 destination = dest;
13508 if (fdh != NULL)
13509 {
13510 /* Fixup old ABI sym to point at code
13511 entry. */
99877b66 13512 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13513 hash->elf.root.u.def.section = code_sec;
e054468f 13514 hash->elf.root.u.def.value = code_value;
8387904d
AM
13515 }
13516 }
13517 }
13518
721956f4 13519 /* Determine what (if any) linker stub is needed. */
e054468f 13520 plt_ent = NULL;
721956f4 13521 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13522 &plt_ent, destination,
13523 local_off);
ad8e1ba5 13524
05d0e962
AM
13525 if (r_type == R_PPC64_REL24_NOTOC)
13526 {
13527 if (stub_type == ppc_stub_plt_call)
13528 stub_type = ppc_stub_plt_call_notoc;
13529 else if (stub_type == ppc_stub_long_branch
13530 || (code_sec != NULL
13531 && code_sec->output_section != NULL
13532 && (((hash ? hash->elf.other : sym->st_other)
13533 & STO_PPC64_LOCAL_MASK)
4a4e7361 13534 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
13535 stub_type = ppc_stub_long_branch_notoc;
13536 }
13537 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13538 {
13539 /* Check whether we need a TOC adjusting stub.
13540 Since the linker pastes together pieces from
13541 different object files when creating the
13542 _init and _fini functions, it may be that a
13543 call to what looks like a local sym is in
13544 fact a call needing a TOC adjustment. */
33cb30a1
AM
13545 if ((code_sec != NULL
13546 && code_sec->output_section != NULL
33cb30a1 13547 && (code_sec->has_toc_reloc
918dc783
AM
13548 || code_sec->makes_toc_func_call)
13549 && (htab->sec_info[code_sec->id].toc_off
13550 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13551 || (((hash ? hash->elf.other : sym->st_other)
13552 & STO_PPC64_LOCAL_MASK)
13553 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13554 stub_type = ppc_stub_long_branch_r2off;
13555 }
13556
721956f4
AM
13557 if (stub_type == ppc_stub_none)
13558 continue;
13559
411e1bfb
AM
13560 /* __tls_get_addr calls might be eliminated. */
13561 if (stub_type != ppc_stub_plt_call
05d0e962 13562 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13563 && hash != NULL
ed7007c1 13564 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
13565 && section->has_tls_reloc
13566 && irela != internal_relocs)
13567 {
13568 /* Get tls info. */
f961d9dd 13569 unsigned char *tls_mask;
411e1bfb 13570
3a71aa26 13571 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13572 irela - 1, input_bfd))
13573 goto error_ret_free_internal;
abc489c6
AM
13574 if ((*tls_mask & TLS_TLS) != 0
13575 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
13576 continue;
13577 }
13578
f378ab09 13579 if (stub_type == ppc_stub_plt_call)
794e51c0 13580 {
6e1816be
AM
13581 if (!htab->opd_abi
13582 && htab->params->plt_localentry0 != 0
13583 && is_elfv2_localentry0 (&hash->elf))
13584 htab->has_plt_localentry0 = 1;
13585 else if (irela + 1 < irelaend
13586 && irela[1].r_offset == irela->r_offset + 4
13587 && (ELF64_R_TYPE (irela[1].r_info)
13588 == R_PPC64_TOCSAVE))
f378ab09
AM
13589 {
13590 if (!tocsave_find (htab, INSERT,
13591 &local_syms, irela + 1, input_bfd))
13592 goto error_ret_free_internal;
13593 }
f378ab09
AM
13594 else
13595 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13596 }
3b421ab3 13597
721956f4 13598 /* Support for grouping stub sections. */
6f20ed8a 13599 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13600
13601 /* Get the name of this stub. */
13602 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13603 if (!stub_name)
13604 goto error_ret_free_internal;
13605
13606 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
0a1b45a2 13607 stub_name, false, false);
721956f4
AM
13608 if (stub_entry != NULL)
13609 {
05d0e962 13610 enum ppc_stub_type old_type;
e10a07b3 13611
05d0e962
AM
13612 /* A stub has already been created, but it may
13613 not be the required type. We shouldn't be
13614 transitioning from plt_call to long_branch
13615 stubs or vice versa, but we might be
13616 upgrading from plt_call to plt_call_r2save or
13617 from long_branch to long_branch_r2off. */
721956f4 13618 free (stub_name);
e10a07b3
AM
13619 if (htab->params->power10_stubs == -1)
13620 {
13621 /* For --power10-stubs=auto, don't merge _notoc
13622 and other varieties of stubs. (The _both
13623 variety won't be created.) */
0a1b45a2 13624 bool notoc = r_type == R_PPC64_REL24_NOTOC;
e10a07b3
AM
13625 struct ppc_stub_hash_entry *alt_stub
13626 = select_alt_stub (stub_entry, notoc);
13627
13628 if (alt_stub == NULL)
13629 {
13630 alt_stub = (struct ppc_stub_hash_entry *)
13631 stub_hash_newfunc (NULL,
13632 &htab->stub_hash_table,
13633 stub_entry->root.string);
13634 if (alt_stub == NULL)
13635 {
13636 /* xgettext:c-format */
13637 _bfd_error_handler
13638 (_("%pB: cannot create stub entry %s"),
13639 section->owner, stub_entry->root.string);
13640 goto error_ret_free_internal;
13641 }
13642 *alt_stub = *stub_entry;
13643 stub_entry->root.next = &alt_stub->root;
13644 if (notoc)
13645 /* Sort notoc stubs first, for no good
13646 reason. */
13647 alt_stub = stub_entry;
13648 alt_stub->stub_type = stub_type;
13649 }
13650 stub_entry = alt_stub;
13651 }
05d0e962
AM
13652 old_type = stub_entry->stub_type;
13653 switch (old_type)
13654 {
13655 default:
13656 abort ();
13657
13658 case ppc_stub_save_res:
13659 continue;
13660
13661 case ppc_stub_plt_call:
13662 case ppc_stub_plt_call_r2save:
13663 case ppc_stub_plt_call_notoc:
13664 case ppc_stub_plt_call_both:
13665 if (stub_type == ppc_stub_plt_call)
13666 continue;
13667 else if (stub_type == ppc_stub_plt_call_r2save)
13668 {
13669 if (old_type == ppc_stub_plt_call_notoc)
13670 stub_type = ppc_stub_plt_call_both;
13671 }
13672 else if (stub_type == ppc_stub_plt_call_notoc)
13673 {
13674 if (old_type == ppc_stub_plt_call_r2save)
13675 stub_type = ppc_stub_plt_call_both;
13676 }
13677 else
13678 abort ();
13679 break;
13680
13681 case ppc_stub_plt_branch:
13682 case ppc_stub_plt_branch_r2off:
13683 case ppc_stub_plt_branch_notoc:
13684 case ppc_stub_plt_branch_both:
13685 old_type += (ppc_stub_long_branch
13686 - ppc_stub_plt_branch);
13687 /* Fall through. */
13688 case ppc_stub_long_branch:
13689 case ppc_stub_long_branch_r2off:
13690 case ppc_stub_long_branch_notoc:
13691 case ppc_stub_long_branch_both:
13692 if (stub_type == ppc_stub_long_branch)
13693 continue;
13694 else if (stub_type == ppc_stub_long_branch_r2off)
13695 {
13696 if (old_type == ppc_stub_long_branch_notoc)
13697 stub_type = ppc_stub_long_branch_both;
13698 }
13699 else if (stub_type == ppc_stub_long_branch_notoc)
13700 {
13701 if (old_type == ppc_stub_long_branch_r2off)
13702 stub_type = ppc_stub_long_branch_both;
13703 }
13704 else
13705 abort ();
13706 break;
13707 }
13708 if (old_type < stub_type)
794e51c0 13709 stub_entry->stub_type = stub_type;
721956f4
AM
13710 continue;
13711 }
13712
25f53a85 13713 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13714 if (stub_entry == NULL)
13715 {
13716 free (stub_name);
6cdc0ccc
AM
13717 error_ret_free_internal:
13718 if (elf_section_data (section)->relocs == NULL)
13719 free (internal_relocs);
13720 error_ret_free_local:
c9594989
AM
13721 if (symtab_hdr->contents
13722 != (unsigned char *) local_syms)
6cdc0ccc 13723 free (local_syms);
0a1b45a2 13724 return false;
721956f4
AM
13725 }
13726
ad8e1ba5 13727 stub_entry->stub_type = stub_type;
05d0e962
AM
13728 if (stub_type >= ppc_stub_plt_call
13729 && stub_type <= ppc_stub_plt_call_both)
e054468f 13730 {
05d0e962
AM
13731 stub_entry->target_value = sym_value;
13732 stub_entry->target_section = sym_sec;
e054468f
AM
13733 }
13734 else
13735 {
05d0e962
AM
13736 stub_entry->target_value = code_value;
13737 stub_entry->target_section = code_sec;
e054468f 13738 }
721956f4 13739 stub_entry->h = hash;
e054468f 13740 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13741 stub_entry->symtype
13742 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13743 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13744
3d58e1fc
AM
13745 if (hash != NULL
13746 && (hash->elf.root.type == bfd_link_hash_defined
13747 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13748 htab->stub_globals += 1;
721956f4
AM
13749 }
13750
13751 /* We're done with the internal relocs, free them. */
6cdc0ccc 13752 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13753 free (internal_relocs);
721956f4 13754 }
6cdc0ccc
AM
13755
13756 if (local_syms != NULL
13757 && symtab_hdr->contents != (unsigned char *) local_syms)
13758 {
13759 if (!info->keep_memory)
13760 free (local_syms);
13761 else
13762 symtab_hdr->contents = (unsigned char *) local_syms;
13763 }
721956f4
AM
13764 }
13765
5c3dead3 13766 /* We may have added some stubs. Find out the new size of the
721956f4 13767 stub sections. */
d4aaa2a0 13768 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13769 {
13770 group->lr_restore = 0;
13771 group->eh_size = 0;
13772 if (group->stub_sec != NULL)
13773 {
13774 asection *stub_sec = group->stub_sec;
13775
13776 if (htab->stub_iteration <= STUB_SHRINK_ITER
13777 || stub_sec->rawsize < stub_sec->size)
13778 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13779 stub_sec->rawsize = stub_sec->size;
13780 stub_sec->size = 0;
13781 stub_sec->reloc_count = 0;
13782 stub_sec->flags &= ~SEC_RELOC;
13783 }
13784 }
a804e476
AM
13785 if (htab->tga_group != NULL)
13786 {
13787 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13788 htab->tga_group->eh_size
13789 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13790 htab->tga_group->lr_restore = 23 * 4;
13791 htab->tga_group->stub_sec->size = 24 * 4;
13792 }
eea6121a 13793
ba21f564
AM
13794 if (htab->stub_iteration <= STUB_SHRINK_ITER
13795 || htab->brlt->rawsize < htab->brlt->size)
13796 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13797 htab->brlt->size = 0;
84f5d08e
AM
13798 htab->brlt->reloc_count = 0;
13799 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13800 if (htab->relbrlt != NULL)
eea6121a 13801 htab->relbrlt->size = 0;
721956f4 13802
63bc6f6c 13803 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13804
a4b6fadd
AM
13805 for (group = htab->group; group != NULL; group = group->next)
13806 if (group->needs_save_res)
13807 group->stub_sec->size += htab->sfpr->size;
13808
176a0d42
AM
13809 if (info->emitrelocations
13810 && htab->glink != NULL && htab->glink->size != 0)
13811 {
13812 htab->glink->reloc_count = 1;
13813 htab->glink->flags |= SEC_RELOC;
13814 }
13815
58d180e8
AM
13816 if (htab->glink_eh_frame != NULL
13817 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13818 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13819 {
2e0ce1c8 13820 size_t size = 0, align = 4;
58d180e8 13821
d4aaa2a0 13822 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13823 if (group->eh_size != 0)
13824 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13825 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13826 size += (24 + align - 1) & -align;
58d180e8 13827 if (size != 0)
2e0ce1c8
AM
13828 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13829 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13830 size = (size + align - 1) & -align;
58d180e8
AM
13831 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13832 htab->glink_eh_frame->size = size;
13833 }
13834
e7d1c40c 13835 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13836 for (group = htab->group; group != NULL; group = group->next)
13837 if (group->stub_sec != NULL)
691d2e9a
AM
13838 {
13839 int align = abs (htab->params->plt_stub_align);
13840 group->stub_sec->size
13841 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13842 }
d4aaa2a0
AM
13843
13844 for (group = htab->group; group != NULL; group = group->next)
13845 if (group->stub_sec != NULL
13846 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13847 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13848 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13849 break;
13850
d4aaa2a0 13851 if (group == NULL
ba21f564
AM
13852 && (htab->brlt->rawsize == htab->brlt->size
13853 || (htab->stub_iteration > STUB_SHRINK_ITER
13854 && htab->brlt->rawsize > htab->brlt->size))
58d180e8 13855 && (htab->glink_eh_frame == NULL
a804e476
AM
13856 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13857 && (htab->tga_group == NULL
13858 || htab->stub_iteration > 1))
5c3dead3
AM
13859 break;
13860
721956f4 13861 /* Ask the linker to do its stuff. */
e7d1c40c 13862 (*htab->params->layout_sections_again) ();
721956f4
AM
13863 }
13864
da44f4e5
AM
13865 if (htab->glink_eh_frame != NULL
13866 && htab->glink_eh_frame->size != 0)
13867 {
13868 bfd_vma val;
13869 bfd_byte *p, *last_fde;
13870 size_t last_fde_len, size, align, pad;
d4aaa2a0 13871 struct map_stub *group;
da44f4e5 13872
df136d64
AM
13873 /* It is necessary to at least have a rough outline of the
13874 linker generated CIEs and FDEs written before
13875 bfd_elf_discard_info is run, in order for these FDEs to be
13876 indexed in .eh_frame_hdr. */
da44f4e5
AM
13877 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13878 if (p == NULL)
0a1b45a2 13879 return false;
da44f4e5
AM
13880 htab->glink_eh_frame->contents = p;
13881 last_fde = p;
2e0ce1c8 13882 align = 4;
da44f4e5
AM
13883
13884 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13885 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13886 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13887 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13888 p += last_fde_len + 4;
da44f4e5 13889
d4aaa2a0 13890 for (group = htab->group; group != NULL; group = group->next)
df136d64 13891 if (group->eh_size != 0)
da44f4e5 13892 {
df136d64 13893 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13894 last_fde = p;
df136d64 13895 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13896 /* FDE length. */
2e0ce1c8 13897 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13898 p += 4;
13899 /* CIE pointer. */
13900 val = p - htab->glink_eh_frame->contents;
13901 bfd_put_32 (htab->elf.dynobj, val, p);
13902 p += 4;
13903 /* Offset to stub section, written later. */
13904 p += 4;
13905 /* stub section size. */
d4aaa2a0 13906 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13907 p += 4;
13908 /* Augmentation. */
13909 p += 1;
df136d64
AM
13910 /* Make sure we don't have all nops. This is enough for
13911 elf-eh-frame.c to detect the last non-nop opcode. */
13912 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13913 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13914 }
13915 if (htab->glink != NULL && htab->glink->size != 0)
13916 {
13917 last_fde = p;
2e0ce1c8 13918 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13919 /* FDE length. */
2e0ce1c8 13920 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13921 p += 4;
13922 /* CIE pointer. */
13923 val = p - htab->glink_eh_frame->contents;
13924 bfd_put_32 (htab->elf.dynobj, val, p);
13925 p += 4;
13926 /* Offset to .glink, written later. */
13927 p += 4;
13928 /* .glink size. */
13929 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13930 p += 4;
13931 /* Augmentation. */
13932 p += 1;
13933
3cd7c7d7 13934 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
13935 *p++ = DW_CFA_register;
13936 *p++ = 65;
9f08fa5c 13937 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 13938 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
13939 *p++ = DW_CFA_restore_extended;
13940 *p++ = 65;
2e0ce1c8 13941 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13942 }
13943 /* Subsume any padding into the last FDE if user .eh_frame
13944 sections are aligned more than glink_eh_frame. Otherwise any
13945 zero padding will be seen as a terminator. */
2e0ce1c8 13946 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13947 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13948 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13949 htab->glink_eh_frame->size = size + pad;
13950 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13951 }
13952
d969d15f 13953 maybe_strip_output (info, htab->brlt);
2efec98b
AM
13954 if (htab->relbrlt != NULL)
13955 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
13956 if (htab->glink_eh_frame != NULL)
13957 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13958
0a1b45a2 13959 return true;
721956f4
AM
13960}
13961
13962/* Called after we have determined section placement. If sections
805fc799 13963 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13964
805fc799 13965bfd_vma
1c865ab2 13966ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13967{
805fc799 13968 asection *s;
a27e685f 13969 bfd_vma TOCstart, adjust;
721956f4 13970
43417696
AM
13971 if (info != NULL)
13972 {
13973 struct elf_link_hash_entry *h;
13974 struct elf_link_hash_table *htab = elf_hash_table (info);
13975
2cc15b10 13976 if (is_elf_hash_table (&htab->root)
43417696
AM
13977 && htab->hgot != NULL)
13978 h = htab->hgot;
13979 else
13980 {
2cc15b10
AM
13981 h = (struct elf_link_hash_entry *)
13982 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
13983 if (is_elf_hash_table (&htab->root))
43417696
AM
13984 htab->hgot = h;
13985 }
13986 if (h != NULL
13987 && h->root.type == bfd_link_hash_defined
13988 && !h->root.linker_def
2cc15b10 13989 && (!is_elf_hash_table (&htab->root)
43417696
AM
13990 || h->def_regular))
13991 {
ed7007c1 13992 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
13993 _bfd_set_gp_value (obfd, TOCstart);
13994 return TOCstart;
13995 }
13996 }
13997
805fc799
AM
13998 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13999 order. The TOC starts where the first of these sections starts. */
14000 s = bfd_get_section_by_name (obfd, ".got");
e054468f 14001 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14002 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 14003 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14004 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 14005 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 14006 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 14007 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
14008 {
14009 /* This may happen for
14010 o references to TOC base (SYM@toc / TOC[tc0]) without a
14011 .toc directive
14012 o bad linker script
14013 o --gc-sections and empty TOC sections
14014
14015 FIXME: Warn user? */
14016
14017 /* Look for a likely section. We probably won't even be
14018 using TOCstart. */
14019 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14020 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14021 | SEC_EXCLUDE))
805fc799
AM
14022 == (SEC_ALLOC | SEC_SMALL_DATA))
14023 break;
721956f4 14024 if (s == NULL)
805fc799 14025 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14026 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14027 == (SEC_ALLOC | SEC_SMALL_DATA))
14028 break;
721956f4 14029 if (s == NULL)
805fc799 14030 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14031 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14032 == SEC_ALLOC)
805fc799 14033 break;
721956f4 14034 if (s == NULL)
805fc799 14035 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14036 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14037 break;
14038 }
721956f4 14039
805fc799
AM
14040 TOCstart = 0;
14041 if (s != NULL)
14042 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14043
a27e685f
AM
14044 /* Force alignment. */
14045 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14046 TOCstart -= adjust;
1c865ab2
AM
14047 _bfd_set_gp_value (obfd, TOCstart);
14048
810d4e75 14049 if (info != NULL && s != NULL)
1c865ab2
AM
14050 {
14051 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14052
810d4e75
AM
14053 if (htab != NULL)
14054 {
14055 if (htab->elf.hgot != NULL)
14056 {
a27e685f 14057 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14058 htab->elf.hgot->root.u.def.section = s;
14059 }
14060 }
14061 else
1c865ab2 14062 {
810d4e75
AM
14063 struct bfd_link_hash_entry *bh = NULL;
14064 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f 14065 s, TOC_BASE_OFF - adjust,
0a1b45a2 14066 NULL, false, false, &bh);
1c865ab2
AM
14067 }
14068 }
805fc799 14069 return TOCstart;
721956f4
AM
14070}
14071
a345bc8d 14072/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14073 write out any global entry stubs, and PLT relocations. */
a345bc8d 14074
0a1b45a2 14075static bool
49c09209 14076build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14077{
14078 struct bfd_link_info *info;
14079 struct ppc_link_hash_table *htab;
49c09209 14080 struct plt_entry *ent;
a345bc8d
AM
14081 asection *s;
14082
14083 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 14084 return true;
a345bc8d 14085
49c09209
AM
14086 info = inf;
14087 htab = ppc_hash_table (info);
14088 if (htab == NULL)
0a1b45a2 14089 return false;
49c09209
AM
14090
14091 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14092 if (ent->plt.offset != (bfd_vma) -1)
14093 {
14094 /* This symbol has an entry in the procedure linkage
14095 table. Set it up. */
14096 Elf_Internal_Rela rela;
2d7ad24e 14097 asection *plt, *relplt;
49c09209
AM
14098 bfd_byte *loc;
14099
30845f11 14100 if (use_local_plt (info, h))
49c09209
AM
14101 {
14102 if (!(h->def_regular
14103 && (h->root.type == bfd_link_hash_defined
14104 || h->root.type == bfd_link_hash_defweak)))
14105 continue;
2d7ad24e
AM
14106 if (h->type == STT_GNU_IFUNC)
14107 {
14108 plt = htab->elf.iplt;
14109 relplt = htab->elf.irelplt;
0a1b45a2 14110 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14111 if (htab->opd_abi)
14112 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14113 else
14114 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14115 }
49c09209 14116 else
2d7ad24e
AM
14117 {
14118 plt = htab->pltlocal;
14119 if (bfd_link_pic (info))
14120 {
14121 relplt = htab->relpltlocal;
14122 if (htab->opd_abi)
14123 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14124 else
14125 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14126 }
14127 else
14128 relplt = NULL;
14129 }
ed7007c1 14130 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14131
14132 if (relplt == NULL)
14133 {
14134 loc = plt->contents + ent->plt.offset;
14135 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14136 if (htab->opd_abi)
14137 {
14138 bfd_vma toc = elf_gp (info->output_bfd);
14139 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14140 bfd_put_64 (info->output_bfd, toc, loc + 8);
14141 }
14142 }
14143 else
14144 {
14145 rela.r_offset = (plt->output_section->vma
14146 + plt->output_offset
14147 + ent->plt.offset);
14148 loc = relplt->contents + (relplt->reloc_count++
14149 * sizeof (Elf64_External_Rela));
14150 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14151 }
49c09209
AM
14152 }
14153 else
14154 {
14155 rela.r_offset = (htab->elf.splt->output_section->vma
14156 + htab->elf.splt->output_offset
14157 + ent->plt.offset);
14158 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14159 rela.r_addend = ent->addend;
14160 loc = (htab->elf.srelplt->contents
14161 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14162 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14163 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
0a1b45a2 14164 htab->elf.ifunc_resolvers = true;
2d7ad24e 14165 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14166 }
49c09209
AM
14167 }
14168
a345bc8d 14169 if (!h->pointer_equality_needed)
0a1b45a2 14170 return true;
a345bc8d
AM
14171
14172 if (h->def_regular)
0a1b45a2 14173 return true;
a345bc8d 14174
9e390558 14175 s = htab->global_entry;
49c09209 14176 if (s == NULL || s->size == 0)
0a1b45a2 14177 return true;
49c09209
AM
14178
14179 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14180 if (ent->plt.offset != (bfd_vma) -1
14181 && ent->addend == 0)
a345bc8d
AM
14182 {
14183 bfd_byte *p;
14184 asection *plt;
14185 bfd_vma off;
14186
a345bc8d 14187 p = s->contents + h->root.u.def.value;
33e44f2e 14188 plt = htab->elf.splt;
30845f11 14189 if (use_local_plt (info, h))
2d7ad24e
AM
14190 {
14191 if (h->type == STT_GNU_IFUNC)
14192 plt = htab->elf.iplt;
14193 else
14194 plt = htab->pltlocal;
14195 }
49c09209 14196 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14197 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14198
14199 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14200 {
14201 info->callbacks->einfo
c1c8c1ef 14202 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14203 h->root.root.string);
14204 bfd_set_error (bfd_error_bad_value);
0a1b45a2 14205 htab->stub_error = true;
a345bc8d
AM
14206 }
14207
7341d5e2
AM
14208 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14209 if (htab->params->emit_stub_syms)
14210 {
14211 size_t len = strlen (h->root.root.string);
14212 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14213
14214 if (name == NULL)
0a1b45a2 14215 return false;
7341d5e2
AM
14216
14217 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
0a1b45a2 14218 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
7341d5e2 14219 if (h == NULL)
0a1b45a2 14220 return false;
7341d5e2
AM
14221 if (h->root.type == bfd_link_hash_new)
14222 {
14223 h->root.type = bfd_link_hash_defined;
14224 h->root.u.def.section = s;
14225 h->root.u.def.value = p - s->contents;
14226 h->ref_regular = 1;
14227 h->def_regular = 1;
14228 h->ref_regular_nonweak = 1;
14229 h->forced_local = 1;
14230 h->non_elf = 0;
2ec55de3 14231 h->root.linker_def = 1;
7341d5e2
AM
14232 }
14233 }
14234
a345bc8d
AM
14235 if (PPC_HA (off) != 0)
14236 {
14237 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14238 p += 4;
14239 }
14240 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14241 p += 4;
14242 bfd_put_32 (s->owner, MTCTR_R12, p);
14243 p += 4;
407aa07c 14244 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14245 break;
14246 }
0a1b45a2 14247 return true;
a345bc8d
AM
14248}
14249
49c09209
AM
14250/* Write PLT relocs for locals. */
14251
0a1b45a2 14252static bool
49c09209
AM
14253write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14254{
14255 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14256 bfd *ibfd;
14257
14258 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14259 {
14260 struct got_entry **lgot_ents, **end_lgot_ents;
14261 struct plt_entry **local_plt, **lplt, **end_local_plt;
14262 Elf_Internal_Shdr *symtab_hdr;
14263 bfd_size_type locsymcount;
14264 Elf_Internal_Sym *local_syms = NULL;
14265 struct plt_entry *ent;
14266
14267 if (!is_ppc64_elf (ibfd))
14268 continue;
14269
14270 lgot_ents = elf_local_got_ents (ibfd);
14271 if (!lgot_ents)
14272 continue;
14273
14274 symtab_hdr = &elf_symtab_hdr (ibfd);
14275 locsymcount = symtab_hdr->sh_info;
14276 end_lgot_ents = lgot_ents + locsymcount;
14277 local_plt = (struct plt_entry **) end_lgot_ents;
14278 end_local_plt = local_plt + locsymcount;
14279 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14280 for (ent = *lplt; ent != NULL; ent = ent->next)
14281 if (ent->plt.offset != (bfd_vma) -1)
14282 {
14283 Elf_Internal_Sym *sym;
14284 asection *sym_sec;
14285 asection *plt, *relplt;
14286 bfd_byte *loc;
14287 bfd_vma val;
49c09209
AM
14288
14289 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14290 lplt - local_plt, ibfd))
14291 {
c9594989 14292 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209 14293 free (local_syms);
0a1b45a2 14294 return false;
49c09209
AM
14295 }
14296
14297 val = sym->st_value + ent->addend;
49c09209
AM
14298 if (sym_sec != NULL && sym_sec->output_section != NULL)
14299 val += sym_sec->output_offset + sym_sec->output_section->vma;
14300
2d7ad24e
AM
14301 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14302 {
0a1b45a2 14303 htab->elf.ifunc_resolvers = true;
2d7ad24e
AM
14304 plt = htab->elf.iplt;
14305 relplt = htab->elf.irelplt;
14306 }
14307 else
14308 {
14309 plt = htab->pltlocal;
14310 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14311 }
49c09209 14312
2d7ad24e
AM
14313 if (relplt == NULL)
14314 {
14315 loc = plt->contents + ent->plt.offset;
14316 bfd_put_64 (info->output_bfd, val, loc);
14317 if (htab->opd_abi)
14318 {
14319 bfd_vma toc = elf_gp (ibfd);
14320 bfd_put_64 (info->output_bfd, toc, loc + 8);
14321 }
14322 }
49c09209 14323 else
2d7ad24e
AM
14324 {
14325 Elf_Internal_Rela rela;
14326 rela.r_offset = (ent->plt.offset
14327 + plt->output_offset
14328 + plt->output_section->vma);
14329 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14330 {
14331 if (htab->opd_abi)
14332 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14333 else
14334 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14335 }
14336 else
14337 {
14338 if (htab->opd_abi)
14339 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14340 else
14341 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14342 }
14343 rela.r_addend = val;
14344 loc = relplt->contents + (relplt->reloc_count++
14345 * sizeof (Elf64_External_Rela));
14346 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14347 }
49c09209
AM
14348 }
14349
14350 if (local_syms != NULL
14351 && symtab_hdr->contents != (unsigned char *) local_syms)
14352 {
14353 if (!info->keep_memory)
14354 free (local_syms);
14355 else
14356 symtab_hdr->contents = (unsigned char *) local_syms;
14357 }
14358 }
0a1b45a2 14359 return true;
49c09209
AM
14360}
14361
a804e476
AM
14362/* Emit the static wrapper function preserving registers around a
14363 __tls_get_addr_opt call. */
14364
0a1b45a2 14365static bool
a804e476
AM
14366emit_tga_desc (struct ppc_link_hash_table *htab)
14367{
14368 asection *stub_sec = htab->tga_group->stub_sec;
14369 unsigned int cfa_updt = 11 * 4;
14370 bfd_byte *p;
14371 bfd_vma to, from, delta;
14372
14373 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14374 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14375 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14376 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14377 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14378 delta = to - from;
14379 if (delta + (1 << 25) >= 1 << 26)
14380 {
14381 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
0a1b45a2
AM
14382 htab->stub_error = true;
14383 return false;
a804e476
AM
14384 }
14385
14386 p = stub_sec->contents;
14387 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14388 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14389 p += 4;
14390 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14391 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14392}
14393
14394/* Emit eh_frame describing the static wrapper function. */
14395
14396static bfd_byte *
14397emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14398{
14399 unsigned int cfa_updt = 11 * 4;
14400 unsigned int i;
14401
14402 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14403 *p++ = DW_CFA_def_cfa_offset;
14404 if (htab->opd_abi)
14405 {
14406 *p++ = 128;
14407 *p++ = 1;
14408 }
14409 else
14410 *p++ = 96;
14411 *p++ = DW_CFA_offset_extended_sf;
14412 *p++ = 65;
14413 *p++ = (-16 / 8) & 0x7f;
14414 for (i = 4; i < 12; i++)
14415 {
14416 *p++ = DW_CFA_offset + i;
14417 *p++ = (htab->opd_abi ? 13 : 12) - i;
14418 }
14419 *p++ = DW_CFA_advance_loc + 10;
14420 *p++ = DW_CFA_def_cfa_offset;
14421 *p++ = 0;
14422 for (i = 4; i < 12; i++)
14423 *p++ = DW_CFA_restore + i;
14424 *p++ = DW_CFA_advance_loc + 2;
14425 *p++ = DW_CFA_restore_extended;
14426 *p++ = 65;
14427 return p;
14428}
14429
721956f4
AM
14430/* Build all the stubs associated with the current output file.
14431 The stubs are kept in a hash table attached to the main linker
14432 hash table. This function is called via gldelf64ppc_finish. */
14433
0a1b45a2 14434bool
e7d1c40c 14435ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14436 char **stats)
5d1634d7
AM
14437{
14438 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14439 struct map_stub *group;
721956f4 14440 asection *stub_sec;
5d1634d7 14441 bfd_byte *p;
e717da7e 14442 int stub_sec_count = 0;
5d1634d7 14443
4dfe6ac6 14444 if (htab == NULL)
0a1b45a2 14445 return false;
4dfe6ac6 14446
eea6121a 14447 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14448 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14449 {
14450 group->eh_size = 0;
14451 group->lr_restore = 0;
14452 if ((stub_sec = group->stub_sec) != NULL
14453 && stub_sec->size != 0)
14454 {
14455 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14456 stub_sec->size);
14457 if (stub_sec->contents == NULL)
0a1b45a2 14458 return false;
df136d64
AM
14459 stub_sec->size = 0;
14460 }
14461 }
5d1634d7 14462
23eb7e01 14463 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14464 {
9f951329 14465 unsigned int indx;
ad8e1ba5 14466 bfd_vma plt0;
9f951329 14467
721956f4 14468 /* Build the .glink plt call stub. */
e7d1c40c 14469 if (htab->params->emit_stub_syms)
97b639ba
AM
14470 {
14471 struct elf_link_hash_entry *h;
468392fb 14472 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
0a1b45a2 14473 true, false, false);
97b639ba 14474 if (h == NULL)
0a1b45a2 14475 return false;
97b639ba
AM
14476 if (h->root.type == bfd_link_hash_new)
14477 {
14478 h->root.type = bfd_link_hash_defined;
14479 h->root.u.def.section = htab->glink;
ee4bf8d2 14480 h->root.u.def.value = 8;
f5385ebf
AM
14481 h->ref_regular = 1;
14482 h->def_regular = 1;
14483 h->ref_regular_nonweak = 1;
14484 h->forced_local = 1;
14485 h->non_elf = 0;
2ec55de3 14486 h->root.linker_def = 1;
97b639ba
AM
14487 }
14488 }
33e44f2e
AM
14489 plt0 = (htab->elf.splt->output_section->vma
14490 + htab->elf.splt->output_offset
14491 - 16);
176a0d42
AM
14492 if (info->emitrelocations)
14493 {
14494 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14495 if (r == NULL)
0a1b45a2 14496 return false;
176a0d42
AM
14497 r->r_offset = (htab->glink->output_offset
14498 + htab->glink->output_section->vma);
14499 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14500 r->r_addend = plt0;
14501 }
4ce794b7 14502 p = htab->glink->contents;
176a0d42 14503 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14504 bfd_put_64 (htab->glink->owner, plt0, p);
14505 p += 8;
b9e5796b
AM
14506 if (htab->opd_abi)
14507 {
14508 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14509 p += 4;
14510 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14511 p += 4;
14512 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14513 p += 4;
14514 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14515 p += 4;
14516 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14517 p += 4;
14518 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14519 p += 4;
14520 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14521 p += 4;
14522 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14523 p += 4;
14524 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14525 p += 4;
14526 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14527 p += 4;
14528 }
14529 else
14530 {
3cd7c7d7
AM
14531 unsigned int insn;
14532
14533 /* 0:
14534 . .quad plt0-1f # plt0 entry relative to 1:
14535 #
14536 # We get here with r12 initially @ a glink branch
14537 # Load the address of _dl_runtime_resolve from plt0 and
14538 # jump to it, with r0 set to the index of the PLT entry
14539 # to be resolved and r11 the link map.
14540 __glink_PLTresolve:
14541 . std %r2,24(%r1) # optional
14542 . mflr %r0
14543 . bcl 20,31,1f
14544 1:
14545 . mflr %r11
14546 . mtlr %r0
14547 . ld %r0,(0b-1b)(%r11)
14548 . sub %r12,%r12,%r11
14549 . add %r11,%r0,%r11
14550 . addi %r0,%r12,1b-2f
14551 . ld %r12,0(%r11)
14552 . srdi %r0,%r0,2
14553 . mtctr %r12
14554 . ld %r11,8(%r11)
14555 . bctr
14556 2:
14557 . b __glink_PLTresolve
14558 . ...
14559 . b __glink_PLTresolve */
14560
14561 if (htab->has_plt_localentry0)
14562 {
14563 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14564 p += 4;
14565 }
b9e5796b
AM
14566 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14567 p += 4;
14568 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14569 p += 4;
14570 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14571 p += 4;
b9e5796b
AM
14572 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14573 p += 4;
3cd7c7d7
AM
14574 if (htab->has_plt_localentry0)
14575 insn = LD_R0_0R11 | (-20 & 0xfffc);
14576 else
14577 insn = LD_R0_0R11 | (-16 & 0xfffc);
14578 bfd_put_32 (htab->glink->owner, insn, p);
14579 p += 4;
b9e5796b
AM
14580 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14581 p += 4;
3cd7c7d7 14582 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 14583 p += 4;
3cd7c7d7 14584 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
14585 p += 4;
14586 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14587 p += 4;
14588 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14589 p += 4;
14590 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14591 p += 4;
14592 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14593 p += 4;
14594 }
407aa07c
AM
14595 bfd_put_32 (htab->glink->owner, BCTR, p);
14596 p += 4;
c75bc4f7 14597 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14598
9f951329
AM
14599 /* Build the .glink lazy link call stubs. */
14600 indx = 0;
9e390558 14601 while (p < htab->glink->contents + htab->glink->size)
9f951329 14602 {
b9e5796b 14603 if (htab->opd_abi)
9f951329 14604 {
b9e5796b
AM
14605 if (indx < 0x8000)
14606 {
14607 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14608 p += 4;
14609 }
14610 else
14611 {
14612 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14613 p += 4;
14614 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14615 p);
14616 p += 4;
14617 }
9f951329 14618 }
4ce794b7 14619 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14620 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14621 indx++;
9f951329
AM
14622 p += 4;
14623 }
5d1634d7 14624 }
5d1634d7 14625
a804e476
AM
14626 if (htab->tga_group != NULL)
14627 {
14628 htab->tga_group->lr_restore = 23 * 4;
14629 htab->tga_group->stub_sec->size = 24 * 4;
14630 if (!emit_tga_desc (htab))
0a1b45a2 14631 return false;
a804e476
AM
14632 if (htab->glink_eh_frame != NULL
14633 && htab->glink_eh_frame->size != 0)
14634 {
14635 size_t align = 4;
14636
14637 p = htab->glink_eh_frame->contents;
14638 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14639 p += 17;
14640 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14641 }
14642 }
14643
49c09209
AM
14644 /* Build .glink global entry stubs, and PLT relocs for globals. */
14645 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14646
14647 if (!write_plt_relocs_for_local_syms (info))
0a1b45a2 14648 return false;
9e390558 14649
7341d5e2 14650 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 14651 {
4ce794b7 14652 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 14653 htab->brlt->size);
4ce794b7 14654 if (htab->brlt->contents == NULL)
0a1b45a2 14655 return false;
721956f4 14656 }
ee75fd95 14657 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
14658 {
14659 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 14660 htab->relbrlt->size);
63bc6f6c 14661 if (htab->relbrlt->contents == NULL)
0a1b45a2 14662 return false;
63bc6f6c 14663 }
5d1634d7 14664
721956f4
AM
14665 /* Build the stubs as directed by the stub hash table. */
14666 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 14667
a4b6fadd
AM
14668 for (group = htab->group; group != NULL; group = group->next)
14669 if (group->needs_save_res)
7dda8d3c 14670 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 14671
aa8a7074
AM
14672 if (htab->relbrlt != NULL)
14673 htab->relbrlt->reloc_count = 0;
14674
e7d1c40c 14675 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14676 for (group = htab->group; group != NULL; group = group->next)
14677 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
14678 {
14679 int align = abs (htab->params->plt_stub_align);
14680 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14681 }
794e51c0 14682
7dda8d3c
AM
14683 for (group = htab->group; group != NULL; group = group->next)
14684 if (group->needs_save_res)
14685 {
14686 stub_sec = group->stub_sec;
14687 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14688 htab->sfpr->contents, htab->sfpr->size);
14689 if (htab->params->emit_stub_syms)
14690 {
14691 unsigned int i;
14692
14693 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14694 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
0a1b45a2 14695 return false;
7dda8d3c
AM
14696 }
14697 }
14698
df136d64
AM
14699 if (htab->glink_eh_frame != NULL
14700 && htab->glink_eh_frame->size != 0)
14701 {
14702 bfd_vma val;
14703 size_t align = 4;
14704
14705 p = htab->glink_eh_frame->contents;
14706 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14707
14708 for (group = htab->group; group != NULL; group = group->next)
14709 if (group->eh_size != 0)
14710 {
14711 /* Offset to stub section. */
14712 val = (group->stub_sec->output_section->vma
14713 + group->stub_sec->output_offset);
14714 val -= (htab->glink_eh_frame->output_section->vma
14715 + htab->glink_eh_frame->output_offset
14716 + (p + 8 - htab->glink_eh_frame->contents));
14717 if (val + 0x80000000 > 0xffffffff)
14718 {
14719 _bfd_error_handler
14720 (_("%s offset too large for .eh_frame sdata4 encoding"),
14721 group->stub_sec->name);
0a1b45a2 14722 return false;
df136d64
AM
14723 }
14724 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14725 p += (group->eh_size + 17 + 3) & -4;
14726 }
14727 if (htab->glink != NULL && htab->glink->size != 0)
14728 {
14729 /* Offset to .glink. */
14730 val = (htab->glink->output_section->vma
14731 + htab->glink->output_offset
14732 + 8);
14733 val -= (htab->glink_eh_frame->output_section->vma
14734 + htab->glink_eh_frame->output_offset
14735 + (p + 8 - htab->glink_eh_frame->contents));
14736 if (val + 0x80000000 > 0xffffffff)
14737 {
14738 _bfd_error_handler
14739 (_("%s offset too large for .eh_frame sdata4 encoding"),
14740 htab->glink->name);
0a1b45a2 14741 return false;
df136d64
AM
14742 }
14743 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14744 p += (24 + align - 1) & -align;
14745 }
14746 }
14747
d4aaa2a0
AM
14748 for (group = htab->group; group != NULL; group = group->next)
14749 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14750 {
14751 stub_sec_count += 1;
c9301e31
AM
14752 if (stub_sec->rawsize != stub_sec->size
14753 && (htab->stub_iteration <= STUB_SHRINK_ITER
14754 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14755 break;
14756 }
5d1634d7 14757
25516cc5 14758 if (group != NULL)
5d1634d7 14759 {
0a1b45a2 14760 htab->stub_error = true;
cf97bcb0 14761 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14762 }
721956f4 14763
d2a300cf 14764 if (htab->stub_error)
0a1b45a2 14765 return false;
d2a300cf
AM
14766
14767 if (stats != NULL)
14768 {
988b7300
AM
14769 char *groupmsg;
14770 if (asprintf (&groupmsg,
14771 ngettext ("linker stubs in %u group\n",
14772 "linker stubs in %u groups\n",
14773 stub_sec_count),
14774 stub_sec_count) < 0)
14775 *stats = NULL;
14776 else
14777 {
14778 if (asprintf (stats, _("%s"
14779 " branch %lu\n"
14780 " branch toc adj %lu\n"
14781 " branch notoc %lu\n"
14782 " branch both %lu\n"
14783 " long branch %lu\n"
14784 " long toc adj %lu\n"
14785 " long notoc %lu\n"
14786 " long both %lu\n"
14787 " plt call %lu\n"
14788 " plt call save %lu\n"
14789 " plt call notoc %lu\n"
14790 " plt call both %lu\n"
14791 " global entry %lu"),
14792 groupmsg,
14793 htab->stub_count[ppc_stub_long_branch - 1],
14794 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14795 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14796 htab->stub_count[ppc_stub_long_branch_both - 1],
14797 htab->stub_count[ppc_stub_plt_branch - 1],
14798 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14799 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14800 htab->stub_count[ppc_stub_plt_branch_both - 1],
14801 htab->stub_count[ppc_stub_plt_call - 1],
14802 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14803 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14804 htab->stub_count[ppc_stub_plt_call_both - 1],
14805 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14806 *stats = NULL;
14807 free (groupmsg);
14808 }
d2a300cf 14809 }
0a1b45a2 14810 return true;
5bd4f169
AM
14811}
14812
60124e18
AM
14813/* What to do when ld finds relocations against symbols defined in
14814 discarded sections. */
14815
14816static unsigned int
14817ppc64_elf_action_discarded (asection *sec)
14818{
14819 if (strcmp (".opd", sec->name) == 0)
14820 return 0;
14821
14822 if (strcmp (".toc", sec->name) == 0)
14823 return 0;
14824
bce50a28
JJ
14825 if (strcmp (".toc1", sec->name) == 0)
14826 return 0;
14827
60124e18
AM
14828 return _bfd_elf_default_action_discarded (sec);
14829}
14830
e59a1001
AM
14831/* These are the dynamic relocations supported by glibc. */
14832
0a1b45a2 14833static bool
e59a1001
AM
14834ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14835{
14836 switch (r_type)
14837 {
14838 case R_PPC64_RELATIVE:
14839 case R_PPC64_NONE:
14840 case R_PPC64_ADDR64:
14841 case R_PPC64_GLOB_DAT:
14842 case R_PPC64_IRELATIVE:
14843 case R_PPC64_JMP_IREL:
14844 case R_PPC64_JMP_SLOT:
14845 case R_PPC64_DTPMOD64:
14846 case R_PPC64_DTPREL64:
14847 case R_PPC64_TPREL64:
14848 case R_PPC64_TPREL16_LO_DS:
14849 case R_PPC64_TPREL16_DS:
14850 case R_PPC64_TPREL16:
14851 case R_PPC64_TPREL16_LO:
14852 case R_PPC64_TPREL16_HI:
14853 case R_PPC64_TPREL16_HIGH:
14854 case R_PPC64_TPREL16_HA:
14855 case R_PPC64_TPREL16_HIGHA:
14856 case R_PPC64_TPREL16_HIGHER:
14857 case R_PPC64_TPREL16_HIGHEST:
14858 case R_PPC64_TPREL16_HIGHERA:
14859 case R_PPC64_TPREL16_HIGHESTA:
14860 case R_PPC64_ADDR16_LO_DS:
14861 case R_PPC64_ADDR16_LO:
14862 case R_PPC64_ADDR16_HI:
14863 case R_PPC64_ADDR16_HIGH:
14864 case R_PPC64_ADDR16_HA:
14865 case R_PPC64_ADDR16_HIGHA:
14866 case R_PPC64_REL30:
14867 case R_PPC64_COPY:
14868 case R_PPC64_UADDR64:
14869 case R_PPC64_UADDR32:
14870 case R_PPC64_ADDR32:
14871 case R_PPC64_ADDR24:
14872 case R_PPC64_ADDR16:
14873 case R_PPC64_UADDR16:
14874 case R_PPC64_ADDR16_DS:
14875 case R_PPC64_ADDR16_HIGHER:
14876 case R_PPC64_ADDR16_HIGHEST:
14877 case R_PPC64_ADDR16_HIGHERA:
14878 case R_PPC64_ADDR16_HIGHESTA:
14879 case R_PPC64_ADDR14:
14880 case R_PPC64_ADDR14_BRTAKEN:
14881 case R_PPC64_ADDR14_BRNTAKEN:
14882 case R_PPC64_REL32:
14883 case R_PPC64_REL64:
0a1b45a2 14884 return true;
e59a1001
AM
14885
14886 default:
0a1b45a2 14887 return false;
e59a1001
AM
14888 }
14889}
14890
5bd4f169
AM
14891/* The RELOCATE_SECTION function is called by the ELF backend linker
14892 to handle the relocations for a section.
14893
14894 The relocs are always passed as Rela structures; if the section
14895 actually uses Rel structures, the r_addend field will always be
14896 zero.
14897
14898 This function is responsible for adjust the section contents as
14899 necessary, and (if using Rela relocs and generating a
1049f94e 14900 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14901 necessary.
14902
14903 This function does not have to worry about setting the reloc
14904 address or the reloc symbol index.
14905
14906 LOCAL_SYMS is a pointer to the swapped in local symbols.
14907
14908 LOCAL_SECTIONS is an array giving the section in the input file
14909 corresponding to the st_shndx field of each local symbol.
14910
14911 The global hash table entry for the global symbols can be found
14912 via elf_sym_hashes (input_bfd).
14913
1049f94e 14914 When generating relocatable output, this function must handle
5bd4f169
AM
14915 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14916 going to be the section symbol corresponding to the output
14917 section, which means that the addend must be adjusted
14918 accordingly. */
14919
0f684201 14920static int
4ce794b7
AM
14921ppc64_elf_relocate_section (bfd *output_bfd,
14922 struct bfd_link_info *info,
14923 bfd *input_bfd,
14924 asection *input_section,
14925 bfd_byte *contents,
14926 Elf_Internal_Rela *relocs,
14927 Elf_Internal_Sym *local_syms,
14928 asection **local_sections)
5bd4f169 14929{
65f38f15 14930 struct ppc_link_hash_table *htab;
5bd4f169
AM
14931 Elf_Internal_Shdr *symtab_hdr;
14932 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14933 Elf_Internal_Rela *rel;
c316a17c 14934 Elf_Internal_Rela *wrel;
5bd4f169 14935 Elf_Internal_Rela *relend;
411e1bfb
AM
14936 Elf_Internal_Rela outrel;
14937 bfd_byte *loc;
411e1bfb 14938 struct got_entry **local_got_ents;
5bd4f169 14939 bfd_vma TOCstart;
0a1b45a2
AM
14940 bool ret = true;
14941 bool is_opd;
794e51c0 14942 /* Assume 'at' branch hints. */
0a1b45a2
AM
14943 bool is_isa_v2 = true;
14944 bool warned_dynamic = false;
95f0d0d2 14945 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14946
65f38f15 14947 /* Initialize howto table if needed. */
5bd4f169 14948 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14949 ppc_howto_init ();
14950
65f38f15 14951 htab = ppc_hash_table (info);
4dfe6ac6 14952 if (htab == NULL)
0a1b45a2 14953 return false;
ee75fd95
AM
14954
14955 /* Don't relocate stub sections. */
e7d1c40c 14956 if (input_section->owner == htab->params->stub_bfd)
0a1b45a2 14957 return true;
ee75fd95 14958
7af5d5c4
AM
14959 if (!is_ppc64_elf (input_bfd))
14960 {
14961 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 14962 return false;
7af5d5c4 14963 }
0ffa91dd 14964
411e1bfb 14965 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14966 TOCstart = elf_gp (output_bfd);
0ffa91dd 14967 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14968 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14969 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14970
c316a17c 14971 rel = wrel = relocs;
5bd4f169 14972 relend = relocs + input_section->reloc_count;
c316a17c 14973 for (; rel < relend; wrel++, rel++)
5bd4f169 14974 {
04c9666a 14975 enum elf_ppc64_reloc_type r_type;
31c76678 14976 bfd_vma addend;
5bd4f169
AM
14977 bfd_reloc_status_type r;
14978 Elf_Internal_Sym *sym;
14979 asection *sec;
039b3fef
AM
14980 struct elf_link_hash_entry *h_elf;
14981 struct ppc_link_hash_entry *h;
14982 struct ppc_link_hash_entry *fdh;
5bd4f169 14983 const char *sym_name;
0d4792f7 14984 unsigned long r_symndx, toc_symndx;
3a71aa26 14985 bfd_vma toc_addend;
f961d9dd
AM
14986 unsigned char tls_mask, tls_gd, tls_type;
14987 unsigned char sym_type;
5bd4f169 14988 bfd_vma relocation;
0a1b45a2
AM
14989 bool unresolved_reloc, save_unresolved_reloc;
14990 bool warned;
bc30df16 14991 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14992 unsigned int insn;
e11840f9 14993 unsigned int mask;
721956f4
AM
14994 struct ppc_stub_hash_entry *stub_entry;
14995 bfd_vma max_br_offset;
14996 bfd_vma from;
c316a17c 14997 Elf_Internal_Rela orig_rel;
b80eed39
AM
14998 reloc_howto_type *howto;
14999 struct reloc_howto_struct alt_howto;
4a421c53
AM
15000 uint64_t pinsn;
15001 bfd_vma offset;
5bd4f169 15002
c316a17c
AM
15003 again:
15004 orig_rel = *rel;
15005
4ce794b7 15006 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 15007 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
15008
15009 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15010 symbol of the previous ADDR64 reloc. The symbol gives us the
15011 proper TOC base to use. */
15012 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
15013 && wrel != relocs
15014 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 15015 && is_opd)
c316a17c 15016 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 15017
4ce794b7
AM
15018 sym = NULL;
15019 sec = NULL;
039b3fef 15020 h_elf = NULL;
4ce794b7 15021 sym_name = NULL;
0a1b45a2
AM
15022 unresolved_reloc = false;
15023 warned = false;
65f38f15 15024
0b13192e 15025 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15026 {
15027 /* It's a local symbol. */
74f0fb50 15028 struct _opd_sec_data *opd;
4025353c 15029
5bd4f169
AM
15030 sym = local_syms + r_symndx;
15031 sec = local_sections[r_symndx];
26c61ae5 15032 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15033 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15034 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15035 opd = get_opd_info (sec);
15036 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15037 {
51aecdc5
AM
15038 long adjust = opd->adjust[OPD_NDX (sym->st_value
15039 + rel->r_addend)];
4025353c
AM
15040 if (adjust == -1)
15041 relocation = 0;
15042 else
4cc603a5
AM
15043 {
15044 /* If this is a relocation against the opd section sym
15045 and we have edited .opd, adjust the reloc addend so
15046 that ld -r and ld --emit-relocs output is correct.
15047 If it is a reloc against some other .opd symbol,
15048 then the symbol value will be adjusted later. */
15049 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15050 rel->r_addend += adjust;
15051 else
15052 relocation += adjust;
15053 }
1e2f5b6e 15054 }
5bd4f169
AM
15055 }
15056 else
15057 {
0a1b45a2 15058 bool ignored;
62d887d4 15059
b2a8e766
AM
15060 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15061 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15062 h_elf, sec, relocation,
62d887d4 15063 unresolved_reloc, warned, ignored);
039b3fef
AM
15064 sym_name = h_elf->root.root.string;
15065 sym_type = h_elf->type;
b69fdb4e
AM
15066 if (sec != NULL
15067 && sec->owner == output_bfd
15068 && strcmp (sec->name, ".opd") == 0)
15069 {
15070 /* This is a symbol defined in a linker script. All
15071 such are defined in output sections, even those
15072 defined by simple assignment from a symbol defined in
15073 an input section. Transfer the symbol to an
15074 appropriate input .opd section, so that a branch to
15075 this symbol will be mapped to the location specified
15076 by the opd entry. */
15077 struct bfd_link_order *lo;
15078 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15079 if (lo->type == bfd_indirect_link_order)
15080 {
15081 asection *isec = lo->u.indirect.section;
15082 if (h_elf->root.u.def.value >= isec->output_offset
15083 && h_elf->root.u.def.value < (isec->output_offset
15084 + isec->size))
15085 {
15086 h_elf->root.u.def.value -= isec->output_offset;
15087 h_elf->root.u.def.section = isec;
15088 sec = isec;
15089 break;
15090 }
15091 }
15092 }
5bd4f169 15093 }
ed7007c1 15094 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15095
dbaa2011 15096 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15097 {
15098 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15099 input_bfd, input_section,
0930cb30 15100 contents, rel->r_offset);
c316a17c
AM
15101 wrel->r_offset = rel->r_offset;
15102 wrel->r_info = 0;
15103 wrel->r_addend = 0;
15104
15105 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15106 symbols defined in discarded sections. Not done for
c316a17c
AM
15107 non-debug to preserve relocs in .eh_frame which the
15108 eh_frame editing code expects to be present. */
15109 if (bfd_link_relocatable (info)
15110 && (input_section->flags & SEC_DEBUGGING))
15111 wrel--;
15112
15113 continue;
15114 }
ab96bf03 15115
0e1862bb 15116 if (bfd_link_relocatable (info))
c316a17c 15117 goto copy_reloc;
ab96bf03 15118
f40da81b
AM
15119 if (h != NULL && &h->elf == htab->elf.hgot)
15120 {
6f20ed8a 15121 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b 15122 sec = bfd_abs_section_ptr;
0a1b45a2 15123 unresolved_reloc = false;
f40da81b
AM
15124 }
15125
951fd09b
AM
15126 /* TLS optimizations. Replace instruction sequences and relocs
15127 based on information we collected in tls_optimize. We edit
15128 RELOCS so that --emit-relocs will output something sensible
15129 for the final instruction stream. */
15130 tls_mask = 0;
15131 tls_gd = 0;
0d4792f7 15132 toc_symndx = 0;
727fc41e
AM
15133 if (h != NULL)
15134 tls_mask = h->tls_mask;
15135 else if (local_got_ents != NULL)
411e1bfb 15136 {
e054468f
AM
15137 struct plt_entry **local_plt = (struct plt_entry **)
15138 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15139 unsigned char *lgot_masks = (unsigned char *)
e054468f 15140 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15141 tls_mask = lgot_masks[r_symndx];
15142 }
37da22e5 15143 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15144 && (r_type == R_PPC64_TLS
15145 || r_type == R_PPC64_TLSGD
15146 || r_type == R_PPC64_TLSLD))
15147 {
15148 /* Check for toc tls entries. */
f961d9dd 15149 unsigned char *toc_tls;
0d4792f7 15150
727fc41e
AM
15151 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15152 &local_syms, rel, input_bfd))
0a1b45a2 15153 return false;
0d4792f7 15154
727fc41e
AM
15155 if (toc_tls)
15156 tls_mask = *toc_tls;
0d4792f7
AM
15157 }
15158
15159 /* Check that tls relocs are used with tls syms, and non-tls
15160 relocs are used with non-tls syms. */
cf35638d 15161 if (r_symndx != STN_UNDEF
0d4792f7
AM
15162 && r_type != R_PPC64_NONE
15163 && (h == NULL
039b3fef
AM
15164 || h->elf.root.type == bfd_link_hash_defined
15165 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15166 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15167 {
37da22e5 15168 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15169 && (r_type == R_PPC64_TLS
15170 || r_type == R_PPC64_TLSGD
15171 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15172 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15173 ;
15174 else
25f53a85 15175 info->callbacks->einfo
1d483afe 15176 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15177 /* xgettext:c-format */
c1c8c1ef 15178 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15179 /* xgettext:c-format */
c1c8c1ef 15180 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15181 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15182 ppc64_elf_howto_table[r_type]->name,
15183 sym_name);
411e1bfb
AM
15184 }
15185
15186 /* Ensure reloc mapping code below stays sane. */
15187 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15188 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15189 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15190 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15191 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15192 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15193 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15194 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15195 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15196 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15197 abort ();
0d4792f7 15198
411e1bfb
AM
15199 switch (r_type)
15200 {
15201 default:
411e1bfb
AM
15202 break;
15203
ba761f19 15204 case R_PPC64_LO_DS_OPT:
95f0d0d2 15205 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
2365f8d7 15206 if ((insn & (0x3fu << 26)) != 58u << 26)
ba761f19
AM
15207 abort ();
15208 insn += (14u << 26) - (58u << 26);
95f0d0d2 15209 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
15210 r_type = R_PPC64_TOC16_LO;
15211 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15212 break;
15213
411e1bfb
AM
15214 case R_PPC64_TOC16:
15215 case R_PPC64_TOC16_LO:
15216 case R_PPC64_TOC16_DS:
15217 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15218 {
15219 /* Check for toc tls entries. */
f961d9dd 15220 unsigned char *toc_tls;
951fd09b 15221 int retval;
411e1bfb 15222
3a71aa26
AM
15223 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15224 &local_syms, rel, input_bfd);
951fd09b 15225 if (retval == 0)
0a1b45a2 15226 return false;
411e1bfb
AM
15227
15228 if (toc_tls)
15229 {
951fd09b 15230 tls_mask = *toc_tls;
411e1bfb
AM
15231 if (r_type == R_PPC64_TOC16_DS
15232 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15233 {
37da22e5 15234 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15235 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15236 goto toctprel;
15237 }
411e1bfb 15238 else
951fd09b
AM
15239 {
15240 /* If we found a GD reloc pair, then we might be
15241 doing a GD->IE transition. */
15242 if (retval == 2)
15243 {
b00a0a86 15244 tls_gd = TLS_GDIE;
37da22e5
AM
15245 if ((tls_mask & TLS_TLS) != 0
15246 && (tls_mask & TLS_GD) == 0)
102890f0 15247 goto tls_ldgd_opt;
951fd09b
AM
15248 }
15249 else if (retval == 3)
15250 {
37da22e5
AM
15251 if ((tls_mask & TLS_TLS) != 0
15252 && (tls_mask & TLS_LD) == 0)
102890f0 15253 goto tls_ldgd_opt;
951fd09b
AM
15254 }
15255 }
411e1bfb
AM
15256 }
15257 }
15258 break;
15259
9d6ded02
AM
15260 case R_PPC64_GOT_TPREL16_HI:
15261 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15262 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
15263 && (tls_mask & TLS_TPREL) == 0)
15264 {
15265 rel->r_offset -= d_offset;
95f0d0d2 15266 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15267 r_type = R_PPC64_NONE;
15268 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15269 }
15270 break;
15271
411e1bfb
AM
15272 case R_PPC64_GOT_TPREL16_DS:
15273 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15274 if ((tls_mask & TLS_TLS) != 0
951fd09b 15275 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15276 {
81407a69 15277 toctprel:
95f0d0d2 15278 insn = bfd_get_32 (input_bfd,
c316a17c 15279 contents + rel->r_offset - d_offset);
411e1bfb
AM
15280 insn &= 31 << 21;
15281 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15282 bfd_put_32 (input_bfd, insn,
c316a17c 15283 contents + rel->r_offset - d_offset);
411e1bfb 15284 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15285 if (toc_symndx != 0)
15286 {
15287 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15288 rel->r_addend = toc_addend;
0d4792f7
AM
15289 /* We changed the symbol. Start over in order to
15290 get h, sym, sec etc. right. */
c316a17c 15291 goto again;
0d4792f7
AM
15292 }
15293 else
15294 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15295 }
15296 break;
15297
87c69f97 15298 case R_PPC64_GOT_TPREL_PCREL34:
c213164a
AM
15299 if ((tls_mask & TLS_TLS) != 0
15300 && (tls_mask & TLS_TPREL) == 0)
15301 {
15302 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15303 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15304 pinsn <<= 32;
15305 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15306 pinsn += ((2ULL << 56) + (-1ULL << 52)
15307 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15308 bfd_put_32 (input_bfd, pinsn >> 32,
15309 contents + rel->r_offset);
15310 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15311 contents + rel->r_offset + 4);
15312 r_type = R_PPC64_TPREL34;
15313 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15314 }
15315 break;
15316
411e1bfb 15317 case R_PPC64_TLS:
37da22e5 15318 if ((tls_mask & TLS_TLS) != 0
951fd09b 15319 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15320 {
c213164a 15321 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15322 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15323 if (insn == 0)
c213164a
AM
15324 break;
15325 if ((rel->r_offset & 3) == 0)
0d4792f7 15326 {
c213164a
AM
15327 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15328 /* Was PPC64_TLS which sits on insn boundary, now
15329 PPC64_TPREL16_LO which is at low-order half-word. */
15330 rel->r_offset += d_offset;
15331 r_type = R_PPC64_TPREL16_LO;
15332 if (toc_symndx != 0)
15333 {
15334 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15335 rel->r_addend = toc_addend;
15336 /* We changed the symbol. Start over in order to
15337 get h, sym, sec etc. right. */
15338 goto again;
15339 }
15340 else
15341 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15342 }
15343 else if ((rel->r_offset & 3) == 1)
15344 {
15345 /* For pcrel IE to LE we already have the full
15346 offset and thus don't need an addi here. A nop
15347 or mr will do. */
2365f8d7 15348 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15349 {
15350 /* Extract regs from addi rt,ra,si. */
15351 unsigned int rt = (insn >> 21) & 0x1f;
15352 unsigned int ra = (insn >> 16) & 0x1f;
15353 if (rt == ra)
15354 insn = NOP;
15355 else
15356 {
15357 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15358 insn = (rt << 16) | (ra << 21) | (ra << 11);
15359 insn |= (31u << 26) | (444u << 1);
15360 }
15361 }
15362 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15363 }
411e1bfb
AM
15364 }
15365 break;
15366
411e1bfb
AM
15367 case R_PPC64_GOT_TLSGD16_HI:
15368 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15369 tls_gd = TLS_GDIE;
37da22e5 15370 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
15371 goto tls_gdld_hi;
15372 break;
15373
411e1bfb
AM
15374 case R_PPC64_GOT_TLSLD16_HI:
15375 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 15376 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 15377 {
951fd09b
AM
15378 tls_gdld_hi:
15379 if ((tls_mask & tls_gd) != 0)
15380 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15381 + R_PPC64_GOT_TPREL16_DS);
15382 else
411e1bfb 15383 {
4fe5ca5b 15384 rel->r_offset -= d_offset;
95f0d0d2 15385 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15386 r_type = R_PPC64_NONE;
411e1bfb 15387 }
951fd09b 15388 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15389 }
15390 break;
15391
951fd09b
AM
15392 case R_PPC64_GOT_TLSGD16:
15393 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15394 tls_gd = TLS_GDIE;
37da22e5 15395 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 15396 goto tls_ldgd_opt;
951fd09b 15397 break;
411e1bfb 15398
951fd09b
AM
15399 case R_PPC64_GOT_TLSLD16:
15400 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 15401 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 15402 {
b9f04fe0 15403 unsigned int insn1, insn2;
102890f0
AM
15404
15405 tls_ldgd_opt:
727fc41e
AM
15406 offset = (bfd_vma) -1;
15407 /* If not using the newer R_PPC64_TLSGD/LD to mark
15408 __tls_get_addr calls, we must trust that the call
15409 stays with its arg setup insns, ie. that the next
15410 reloc is the __tls_get_addr call associated with
15411 the current reloc. Edit both insns. */
9737e8af 15412 if (input_section->nomark_tls_get_addr
727fc41e
AM
15413 && rel + 1 < relend
15414 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15415 htab->tls_get_addr_fd,
15416 htab->tga_desc_fd,
727fc41e 15417 htab->tls_get_addr,
9e7028aa 15418 htab->tga_desc))
727fc41e 15419 offset = rel[1].r_offset;
b86ac8e3
AM
15420 /* We read the low GOT_TLS (or TOC16) insn because we
15421 need to keep the destination reg. It may be
15422 something other than the usual r3, and moved to r3
15423 before the call by intervening code. */
95f0d0d2 15424 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15425 contents + rel->r_offset - d_offset);
102890f0 15426 if ((tls_mask & tls_gd) != 0)
411e1bfb 15427 {
102890f0 15428 /* IE */
b86ac8e3 15429 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15430 insn1 |= 58u << 26; /* ld */
102890f0 15431 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15432 if (offset != (bfd_vma) -1)
f58d5a2d 15433 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15434 if (r_type == R_PPC64_TOC16
15435 || r_type == R_PPC64_TOC16_LO)
102890f0 15436 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15437 else
15438 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15439 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15440 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15441 }
15442 else
15443 {
15444 /* LE */
b86ac8e3
AM
15445 insn1 &= 0x1f << 21;
15446 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15447 insn2 = 0x38630000; /* addi 3,3,0 */
15448 if (tls_gd == 0)
951fd09b 15449 {
102890f0 15450 /* Was an LD reloc. */
71c4e95a 15451 r_symndx = STN_UNDEF;
102890f0 15452 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15453 }
102890f0 15454 else if (toc_symndx != 0)
3a71aa26
AM
15455 {
15456 r_symndx = toc_symndx;
15457 rel->r_addend = toc_addend;
15458 }
102890f0
AM
15459 r_type = R_PPC64_TPREL16_HA;
15460 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15461 if (offset != (bfd_vma) -1)
15462 {
15463 rel[1].r_info = ELF64_R_INFO (r_symndx,
15464 R_PPC64_TPREL16_LO);
15465 rel[1].r_offset = offset + d_offset;
15466 rel[1].r_addend = rel->r_addend;
15467 }
102890f0 15468 }
95f0d0d2 15469 bfd_put_32 (input_bfd, insn1,
3a71aa26 15470 contents + rel->r_offset - d_offset);
727fc41e 15471 if (offset != (bfd_vma) -1)
c96e0573
AM
15472 {
15473 bfd_put_32 (input_bfd, insn2, contents + offset);
15474 if (offset + 8 <= input_section->size)
15475 {
15476 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15477 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15478 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15479 }
15480 }
727fc41e
AM
15481 if ((tls_mask & tls_gd) == 0
15482 && (tls_gd == 0 || toc_symndx != 0))
15483 {
15484 /* We changed the symbol. Start over in order
15485 to get h, sym, sec etc. right. */
c316a17c 15486 goto again;
727fc41e
AM
15487 }
15488 }
15489 break;
15490
87c69f97 15491 case R_PPC64_GOT_TLSGD_PCREL34:
c213164a
AM
15492 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15493 {
15494 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15495 pinsn <<= 32;
15496 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15497 if ((tls_mask & TLS_GDIE) != 0)
15498 {
15499 /* IE, pla -> pld */
15500 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15501 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15502 }
15503 else
15504 {
15505 /* LE, pla pcrel -> paddi r13 */
15506 pinsn += (-1ULL << 52) + (13ULL << 16);
15507 r_type = R_PPC64_TPREL34;
15508 }
15509 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15510 bfd_put_32 (input_bfd, pinsn >> 32,
15511 contents + rel->r_offset);
15512 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15513 contents + rel->r_offset + 4);
15514 }
15515 break;
15516
87c69f97 15517 case R_PPC64_GOT_TLSLD_PCREL34:
c213164a
AM
15518 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15519 {
15520 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15521 pinsn <<= 32;
15522 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15523 pinsn += (-1ULL << 52) + (13ULL << 16);
15524 bfd_put_32 (input_bfd, pinsn >> 32,
15525 contents + rel->r_offset);
15526 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15527 contents + rel->r_offset + 4);
15528 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15529 r_symndx = STN_UNDEF;
15530 r_type = R_PPC64_TPREL34;
15531 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15532 goto again;
15533 }
15534 break;
15535
727fc41e 15536 case R_PPC64_TLSGD:
37da22e5 15537 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 15538 && rel + 1 < relend)
727fc41e 15539 {
b9f04fe0 15540 unsigned int insn2;
5663e321 15541 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15542
4a421c53 15543 offset = rel->r_offset;
5663e321 15544 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15545 {
15546 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15547 if (r_type1 == R_PPC64_PLT_PCREL34
15548 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15549 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15550 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15551 break;
15552 }
15553
15554 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15555 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15556
b00a0a86 15557 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
15558 {
15559 /* IE */
15560 r_type = R_PPC64_NONE;
15561 insn2 = 0x7c636a14; /* add 3,3,13 */
15562 }
15563 else
15564 {
15565 /* LE */
15566 if (toc_symndx != 0)
15567 {
15568 r_symndx = toc_symndx;
15569 rel->r_addend = toc_addend;
15570 }
c213164a
AM
15571 if (r_type1 == R_PPC64_REL24_NOTOC
15572 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15573 {
15574 r_type = R_PPC64_NONE;
15575 insn2 = NOP;
15576 }
15577 else
15578 {
15579 rel->r_offset = offset + d_offset;
15580 r_type = R_PPC64_TPREL16_LO;
15581 insn2 = 0x38630000; /* addi 3,3,0 */
15582 }
727fc41e
AM
15583 }
15584 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15585 /* Zap the reloc on the _tls_get_addr call too. */
15586 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15587 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15588 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15589 if ((tls_mask & TLS_GDIE) == 0
15590 && toc_symndx != 0
15591 && r_type != R_PPC64_NONE)
c316a17c 15592 goto again;
411e1bfb 15593 }
411e1bfb
AM
15594 break;
15595
727fc41e 15596 case R_PPC64_TLSLD:
37da22e5 15597 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 15598 && rel + 1 < relend)
727fc41e 15599 {
b9f04fe0 15600 unsigned int insn2;
5663e321 15601 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15602
4a421c53 15603 offset = rel->r_offset;
5663e321 15604 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15605 {
15606 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15607 if (r_type1 == R_PPC64_PLT_PCREL34
15608 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15609 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15610 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15611 break;
15612 }
15613
15614 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15615 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15616
c213164a
AM
15617 if (r_type1 == R_PPC64_REL24_NOTOC
15618 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15619 {
15620 r_type = R_PPC64_NONE;
15621 insn2 = NOP;
15622 }
15623 else
15624 {
15625 rel->r_offset = offset + d_offset;
15626 r_symndx = STN_UNDEF;
15627 r_type = R_PPC64_TPREL16_LO;
15628 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15629 insn2 = 0x38630000; /* addi 3,3,0 */
15630 }
727fc41e 15631 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15632 /* Zap the reloc on the _tls_get_addr call too. */
15633 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15634 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15635 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15636 if (r_type != R_PPC64_NONE)
15637 goto again;
727fc41e
AM
15638 }
15639 break;
15640
411e1bfb 15641 case R_PPC64_DTPMOD64:
951fd09b
AM
15642 if (rel + 1 < relend
15643 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15644 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 15645 {
951fd09b
AM
15646 if ((tls_mask & TLS_GD) == 0)
15647 {
15648 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 15649 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
15650 r_type = R_PPC64_TPREL64;
15651 else
15652 {
4ce794b7 15653 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
15654 r_type = R_PPC64_NONE;
15655 }
15656 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15657 }
15658 }
15659 else
15660 {
15661 if ((tls_mask & TLS_LD) == 0)
411e1bfb 15662 {
4ce794b7 15663 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 15664 r_type = R_PPC64_NONE;
951fd09b 15665 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 15666 }
411e1bfb
AM
15667 }
15668 break;
15669
15670 case R_PPC64_TPREL64:
951fd09b 15671 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
15672 {
15673 r_type = R_PPC64_NONE;
15674 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15675 }
15676 break;
52a82034 15677
006589cf
AM
15678 case R_PPC64_ENTRY:
15679 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15680 if (!bfd_link_pic (info)
15681 && !info->traditional_format
15682 && relocation + 0x80008000 <= 0xffffffff)
15683 {
15684 unsigned int insn1, insn2;
15685
15686 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15687 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15688 if ((insn1 & ~0xfffc) == LD_R2_0R12
15689 && insn2 == ADD_R2_R2_R12)
15690 {
95f0d0d2 15691 bfd_put_32 (input_bfd,
006589cf
AM
15692 LIS_R2 + PPC_HA (relocation),
15693 contents + rel->r_offset);
95f0d0d2 15694 bfd_put_32 (input_bfd,
006589cf
AM
15695 ADDI_R2_R2 + PPC_LO (relocation),
15696 contents + rel->r_offset + 4);
15697 }
15698 }
15699 else
15700 {
15701 relocation -= (rel->r_offset
15702 + input_section->output_offset
15703 + input_section->output_section->vma);
15704 if (relocation + 0x80008000 <= 0xffffffff)
15705 {
15706 unsigned int insn1, insn2;
15707
15708 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15709 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15710 if ((insn1 & ~0xfffc) == LD_R2_0R12
15711 && insn2 == ADD_R2_R2_R12)
15712 {
95f0d0d2 15713 bfd_put_32 (input_bfd,
006589cf
AM
15714 ADDIS_R2_R12 + PPC_HA (relocation),
15715 contents + rel->r_offset);
95f0d0d2 15716 bfd_put_32 (input_bfd,
006589cf
AM
15717 ADDI_R2_R2 + PPC_LO (relocation),
15718 contents + rel->r_offset + 4);
15719 }
15720 }
15721 }
15722 break;
15723
52a82034
AM
15724 case R_PPC64_REL16_HA:
15725 /* If we are generating a non-PIC executable, edit
15726 . 0: addis 2,12,.TOC.-0b@ha
15727 . addi 2,2,.TOC.-0b@l
15728 used by ELFv2 global entry points to set up r2, to
15729 . lis 2,.TOC.@ha
15730 . addi 2,2,.TOC.@l
15731 if .TOC. is in range. */
0e1862bb 15732 if (!bfd_link_pic (info)
810d4e75 15733 && !info->traditional_format
006589cf 15734 && !htab->opd_abi
4f038ee5 15735 && rel->r_addend == d_offset
52a82034
AM
15736 && h != NULL && &h->elf == htab->elf.hgot
15737 && rel + 1 < relend
15738 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15739 && rel[1].r_offset == rel->r_offset + 4
15740 && rel[1].r_addend == rel->r_addend + 4
15741 && relocation + 0x80008000 <= 0xffffffff)
15742 {
15743 unsigned int insn1, insn2;
4a421c53 15744 offset = rel->r_offset - d_offset;
95f0d0d2
AM
15745 insn1 = bfd_get_32 (input_bfd, contents + offset);
15746 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15747 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15748 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15749 {
15750 r_type = R_PPC64_ADDR16_HA;
15751 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15752 rel->r_addend -= d_offset;
15753 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15754 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15755 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15756 }
15757 }
15758 break;
411e1bfb
AM
15759 }
15760
15761 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15762 insn = 0;
b25116a9
AM
15763 max_br_offset = 1 << 25;
15764 addend = rel->r_addend;
bc30df16 15765 reloc_dest = DEST_NORMAL;
65f38f15 15766 switch (r_type)
5bd4f169
AM
15767 {
15768 default:
65f38f15 15769 break;
5bd4f169 15770
3b421ab3
AM
15771 case R_PPC64_TOCSAVE:
15772 if (relocation + addend == (rel->r_offset
15773 + input_section->output_offset
15774 + input_section->output_section->vma)
15775 && tocsave_find (htab, NO_INSERT,
15776 &local_syms, rel, input_bfd))
15777 {
15778 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15779 if (insn == NOP
15780 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15781 bfd_put_32 (input_bfd,
15782 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15783 contents + rel->r_offset);
15784 }
15785 break;
15786
65f38f15
AM
15787 /* Branch taken prediction relocations. */
15788 case R_PPC64_ADDR14_BRTAKEN:
15789 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15790 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15791 /* Fall through. */
65f38f15 15792
86c76c7b 15793 /* Branch not taken prediction relocations. */
65f38f15
AM
15794 case R_PPC64_ADDR14_BRNTAKEN:
15795 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15796 insn |= bfd_get_32 (input_bfd,
411e1bfb 15797 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15798 /* Fall through. */
86c76c7b 15799
b25116a9
AM
15800 case R_PPC64_REL14:
15801 max_br_offset = 1 << 15;
1a0670f3 15802 /* Fall through. */
5bd4f169 15803
65f38f15 15804 case R_PPC64_REL24:
05d0e962 15805 case R_PPC64_REL24_NOTOC:
23cedd1d 15806 case R_PPC64_PLTCALL:
5663e321 15807 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15808 /* Calls to functions with a different TOC, such as calls to
15809 shared objects, need to alter the TOC pointer. This is
15810 done using a linkage stub. A REL24 branching to these
15811 linkage stubs needs to be followed by a nop, as the nop
15812 will be replaced with an instruction to restore the TOC
15813 base pointer. */
8387904d 15814 fdh = h;
b31867b6
AM
15815 if (h != NULL
15816 && h->oh != NULL
15817 && h->oh->is_func_descriptor)
15818 fdh = ppc_follow_link (h->oh);
31c76678
DK
15819 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15820 htab);
5663e321
AM
15821 if ((r_type == R_PPC64_PLTCALL
15822 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15823 && stub_entry != NULL
05d0e962
AM
15824 && stub_entry->stub_type >= ppc_stub_plt_call
15825 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15826 stub_entry = NULL;
15827
6abec6bc 15828 if (stub_entry != NULL
d0abeec8
AM
15829 && ((stub_entry->stub_type >= ppc_stub_plt_call
15830 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15831 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15832 || stub_entry->stub_type == ppc_stub_plt_branch_both
15833 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15834 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15835 {
0a1b45a2 15836 bool can_plt_call = false;
721956f4 15837
6e1816be
AM
15838 if (stub_entry->stub_type == ppc_stub_plt_call
15839 && !htab->opd_abi
15840 && htab->params->plt_localentry0 != 0
9e51d549 15841 && h != NULL
6e1816be
AM
15842 && is_elfv2_localentry0 (&h->elf))
15843 {
15844 /* The function doesn't use or change r2. */
0a1b45a2 15845 can_plt_call = true;
6e1816be 15846 }
05d0e962
AM
15847 else if (r_type == R_PPC64_REL24_NOTOC)
15848 {
15849 /* NOTOC calls don't need to restore r2. */
0a1b45a2 15850 can_plt_call = true;
05d0e962 15851 }
6e1816be 15852
f378ab09 15853 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15854 branch and link followed by a nop. The nop is
15855 replaced by an insn to restore r2. */
6e1816be 15856 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15857 {
ba8ca3e7
AM
15858 unsigned long br;
15859
15860 br = bfd_get_32 (input_bfd,
15861 contents + rel->r_offset);
15862 if ((br & 1) != 0)
41bd81ab 15863 {
ba8ca3e7
AM
15864 unsigned long nop;
15865
15866 nop = bfd_get_32 (input_bfd,
15867 contents + rel->r_offset + 4);
23cedd1d 15868 if (nop == LD_R2_0R1 + STK_TOC (htab))
0a1b45a2 15869 can_plt_call = true;
23cedd1d
AM
15870 else if (nop == NOP
15871 || nop == CROR_151515
15872 || nop == CROR_313131)
a7f2871e 15873 {
ba8ca3e7 15874 if (h != NULL
ed7007c1 15875 && is_tls_get_addr (&h->elf, htab)
7c9cf415 15876 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15877 {
15878 /* Special stub used, leave nop alone. */
15879 }
15880 else
a078d95a
AM
15881 bfd_put_32 (input_bfd,
15882 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7 15883 contents + rel->r_offset + 4);
0a1b45a2 15884 can_plt_call = true;
a7f2871e 15885 }
41bd81ab 15886 }
5bd4f169 15887 }
721956f4 15888
ba8ca3e7 15889 if (!can_plt_call && h != NULL)
721956f4 15890 {
ba8ca3e7
AM
15891 const char *name = h->elf.root.root.string;
15892
15893 if (*name == '.')
15894 ++name;
15895
3f3328b8 15896 if (startswith (name, "__libc_start_main")
ba8ca3e7 15897 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15898 {
ba8ca3e7
AM
15899 /* Allow crt1 branch to go via a toc adjusting
15900 stub. Other calls that never return could do
15901 the same, if we could detect such. */
0a1b45a2 15902 can_plt_call = true;
6ab189d5 15903 }
ba8ca3e7
AM
15904 }
15905
15906 if (!can_plt_call)
15907 {
15908 /* g++ as of 20130507 emits self-calls without a
15909 following nop. This is arguably wrong since we
15910 have conflicting information. On the one hand a
15911 global symbol and on the other a local call
15912 sequence, but don't error for this special case.
15913 It isn't possible to cheaply verify we have
15914 exactly such a call. Allow all calls to the same
15915 section. */
15916 asection *code_sec = sec;
15917
15918 if (get_opd_info (sec) != NULL)
ad8e1ba5 15919 {
ba8ca3e7
AM
15920 bfd_vma off = (relocation + addend
15921 - sec->output_section->vma
15922 - sec->output_offset);
bc30df16 15923
0a1b45a2 15924 opd_entry_value (sec, off, &code_sec, NULL, false);
ad8e1ba5 15925 }
ba8ca3e7 15926 if (code_sec == input_section)
0a1b45a2 15927 can_plt_call = true;
ba8ca3e7
AM
15928 }
15929
15930 if (!can_plt_call)
15931 {
05d0e962
AM
15932 if (stub_entry->stub_type >= ppc_stub_plt_call
15933 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15934 info->callbacks->einfo
695344c0 15935 /* xgettext:c-format */
c1c8c1ef 15936 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15937 "(plt call stub)\n"),
4805fc55
AM
15938 input_bfd, input_section, rel->r_offset, sym_name);
15939 else
15940 info->callbacks->einfo
695344c0 15941 /* xgettext:c-format */
c1c8c1ef 15942 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15943 "(toc save/adjust stub)\n"),
4805fc55 15944 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15945
15946 bfd_set_error (bfd_error_bad_value);
0a1b45a2 15947 ret = false;
721956f4
AM
15948 }
15949
b25116a9 15950 if (can_plt_call
05d0e962
AM
15951 && stub_entry->stub_type >= ppc_stub_plt_call
15952 && stub_entry->stub_type <= ppc_stub_plt_call_both)
0a1b45a2 15953 unresolved_reloc = false;
b25116a9
AM
15954 }
15955
6abec6bc
AM
15956 if ((stub_entry == NULL
15957 || stub_entry->stub_type == ppc_stub_long_branch
15958 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15959 && get_opd_info (sec) != NULL)
15960 {
15961 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15962 bfd_vma off = (relocation + addend
15963 - sec->output_section->vma
15964 - sec->output_offset);
0a1b45a2 15965 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
8387904d
AM
15966 if (dest != (bfd_vma) -1)
15967 {
15968 relocation = dest;
15969 addend = 0;
bc30df16 15970 reloc_dest = DEST_OPD;
8387904d
AM
15971 }
15972 }
15973
b25116a9
AM
15974 /* If the branch is out of reach we ought to have a long
15975 branch stub. */
15976 from = (rel->r_offset
15977 + input_section->output_offset
15978 + input_section->output_section->vma);
15979
6911b7dc
AM
15980 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15981 ? fdh->elf.other
15982 : sym->st_other);
15983
6abec6bc
AM
15984 if (stub_entry != NULL
15985 && (stub_entry->stub_type == ppc_stub_long_branch
15986 || stub_entry->stub_type == ppc_stub_plt_branch)
15987 && (r_type == R_PPC64_ADDR14_BRTAKEN
15988 || r_type == R_PPC64_ADDR14_BRNTAKEN
15989 || (relocation + addend - from + max_br_offset
15990 < 2 * max_br_offset)))
15991 /* Don't use the stub if this branch is in range. */
15992 stub_entry = NULL;
b25116a9 15993
05d0e962
AM
15994 if (stub_entry != NULL
15995 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15996 || stub_entry->stub_type == ppc_stub_long_branch_both
15997 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15998 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15999 && (r_type != R_PPC64_REL24_NOTOC
16000 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 16001 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
16002 && (relocation + addend - from + max_br_offset
16003 < 2 * max_br_offset))
16004 stub_entry = NULL;
16005
16006 if (stub_entry != NULL
16007 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
16008 || stub_entry->stub_type == ppc_stub_long_branch_both
16009 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
16010 || stub_entry->stub_type == ppc_stub_plt_branch_both)
16011 && r_type == R_PPC64_REL24_NOTOC
16012 && (relocation + addend - from + max_br_offset
16013 < 2 * max_br_offset))
16014 stub_entry = NULL;
16015
b25116a9
AM
16016 if (stub_entry != NULL)
16017 {
16018 /* Munge up the value and addend so that we call the stub
16019 rather than the procedure directly. */
a4b6fadd
AM
16020 asection *stub_sec = stub_entry->group->stub_sec;
16021
16022 if (stub_entry->stub_type == ppc_stub_save_res)
16023 relocation += (stub_sec->output_offset
16024 + stub_sec->output_section->vma
16025 + stub_sec->size - htab->sfpr->size
16026 - htab->sfpr->output_offset
16027 - htab->sfpr->output_section->vma);
16028 else
16029 relocation = (stub_entry->stub_offset
16030 + stub_sec->output_offset
16031 + stub_sec->output_section->vma);
b25116a9 16032 addend = 0;
bc30df16 16033 reloc_dest = DEST_STUB;
3b421ab3 16034
29433886
AM
16035 if ((((stub_entry->stub_type == ppc_stub_plt_call
16036 && ALWAYS_EMIT_R2SAVE)
16037 || stub_entry->stub_type == ppc_stub_plt_call_r2save
16038 || stub_entry->stub_type == ppc_stub_plt_call_both)
16039 && rel + 1 < relend
16040 && rel[1].r_offset == rel->r_offset + 4
16041 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
16042 || ((stub_entry->stub_type == ppc_stub_long_branch_both
16043 || stub_entry->stub_type == ppc_stub_plt_branch_both
16044 || stub_entry->stub_type == ppc_stub_plt_call_both)
16045 && r_type == R_PPC64_REL24_NOTOC))
16046 {
16047 /* Skip over the r2 store at the start of the stub. */
16048 if (!(stub_entry->stub_type >= ppc_stub_plt_call
16049 && htab->params->tls_get_addr_opt
16050 && h != NULL
16051 && is_tls_get_addr (&h->elf, htab)))
16052 relocation += 4;
16053 }
5663e321
AM
16054
16055 if (r_type == R_PPC64_REL24_NOTOC
16056 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
16057 || stub_entry->stub_type == ppc_stub_plt_call_both))
16058 htab->notoc_plt = 1;
b25116a9
AM
16059 }
16060
16061 if (insn != 0)
16062 {
794e51c0 16063 if (is_isa_v2)
721956f4 16064 {
b25116a9
AM
16065 /* Set 'a' bit. This is 0b00010 in BO field for branch
16066 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16067 for branch on CTR insns (BO == 1a00t or 1a01t). */
16068 if ((insn & (0x14 << 21)) == (0x04 << 21))
16069 insn |= 0x02 << 21;
16070 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16071 insn |= 0x08 << 21;
16072 else
16073 break;
16074 }
16075 else
16076 {
16077 /* Invert 'y' bit if not the default. */
4cc603a5 16078 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16079 insn ^= 0x01 << 21;
721956f4 16080 }
b25116a9 16081
95f0d0d2 16082 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16083 }
e86ce104 16084
06da1e8e
AM
16085 /* NOP out calls to undefined weak functions.
16086 We can thus call a weak function without first
16087 checking whether the function is defined. */
b25116a9 16088 else if (h != NULL
039b3fef 16089 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16090 && h->elf.dynindx == -1
05d0e962
AM
16091 && (r_type == R_PPC64_REL24
16092 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 16093 && relocation == 0
4cc603a5 16094 && addend == 0)
e86ce104 16095 {
95f0d0d2 16096 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16097 goto copy_reloc;
e86ce104 16098 }
65f38f15 16099 break;
066f4018
AM
16100
16101 case R_PPC64_GOT16_DS:
6738c8a7
AM
16102 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16103 || !htab->do_toc_opt)
06507dab 16104 break;
066f4018
AM
16105 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16106 if (relocation + addend - from + 0x8000 < 0x10000
f5b9c288
AM
16107 && sec != NULL
16108 && sec->output_section != NULL
16109 && !discarded_section (sec)
9e51d549 16110 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16111 {
16112 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16113 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16114 {
16115 insn += (14u << 26) - (58u << 26);
16116 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16117 r_type = R_PPC64_TOC16;
16118 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16119 }
16120 }
16121 break;
16122
16123 case R_PPC64_GOT16_LO_DS:
16124 case R_PPC64_GOT16_HA:
6738c8a7
AM
16125 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16126 || !htab->do_toc_opt)
06507dab 16127 break;
066f4018
AM
16128 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16129 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
f5b9c288
AM
16130 && sec != NULL
16131 && sec->output_section != NULL
16132 && !discarded_section (sec)
9e51d549 16133 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16134 {
16135 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16136 if (r_type == R_PPC64_GOT16_LO_DS
16137 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16138 {
16139 insn += (14u << 26) - (58u << 26);
16140 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16141 r_type = R_PPC64_TOC16_LO;
16142 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16143 }
fba8689a
AM
16144 else if (r_type == R_PPC64_GOT16_HA
16145 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16146 {
16147 r_type = R_PPC64_TOC16_HA;
16148 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16149 }
16150 }
16151 break;
4a421c53
AM
16152
16153 case R_PPC64_GOT_PCREL34:
6738c8a7
AM
16154 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16155 || !htab->do_toc_opt)
06507dab 16156 break;
4a421c53
AM
16157 from = (rel->r_offset
16158 + input_section->output_section->vma
16159 + input_section->output_offset);
6738c8a7 16160 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
f5b9c288
AM
16161 && sec != NULL
16162 && sec->output_section != NULL
16163 && !discarded_section (sec)
9e51d549 16164 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
6738c8a7
AM
16165 break;
16166
16167 offset = rel->r_offset;
16168 pinsn = bfd_get_32 (input_bfd, contents + offset);
16169 pinsn <<= 32;
16170 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16171 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16172 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16173 break;
16174
16175 /* Replace with paddi. */
16176 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16177 r_type = R_PPC64_PCREL34;
16178 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16179 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16180 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16181 /* Fall through. */
4a421c53
AM
16182
16183 case R_PPC64_PCREL34:
6738c8a7
AM
16184 if (!htab->params->no_pcrel_opt
16185 && rel + 1 < relend
16186 && rel[1].r_offset == rel->r_offset
16187 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
9e51d549 16188 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
4a421c53
AM
16189 {
16190 offset = rel->r_offset;
16191 pinsn = bfd_get_32 (input_bfd, contents + offset);
16192 pinsn <<= 32;
16193 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16194 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16195 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16196 | (14ULL << 26) /* paddi */))
16197 {
6738c8a7
AM
16198 bfd_vma off2 = rel[1].r_addend;
16199 if (off2 == 0)
16200 /* zero means next insn. */
16201 off2 = 8;
16202 off2 += offset;
16203 if (off2 + 4 <= input_section->size)
4a421c53 16204 {
6738c8a7
AM
16205 uint64_t pinsn2;
16206 bfd_signed_vma addend_off;
16207 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16208 pinsn2 <<= 32;
16209 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16210 {
6738c8a7
AM
16211 if (off2 + 8 > input_section->size)
16212 break;
16213 pinsn2 |= bfd_get_32 (input_bfd,
16214 contents + off2 + 4);
16215 }
16216 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16217 {
16218 addend += addend_off;
16219 rel->r_addend = addend;
16220 bfd_put_32 (input_bfd, pinsn >> 32,
16221 contents + offset);
16222 bfd_put_32 (input_bfd, pinsn,
16223 contents + offset + 4);
16224 bfd_put_32 (input_bfd, pinsn2 >> 32,
16225 contents + off2);
4a421c53 16226 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16227 bfd_put_32 (input_bfd, pinsn2,
16228 contents + off2 + 4);
4a421c53
AM
16229 }
16230 }
16231 }
16232 }
16233 break;
65f38f15 16234 }
5bd4f169 16235
411e1bfb 16236 tls_type = 0;
23cedd1d 16237 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16238 switch (r_type)
16239 {
16240 default:
cf97bcb0
AM
16241 /* xgettext:c-format */
16242 _bfd_error_handler (_("%pB: %s unsupported"),
16243 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16244
65f38f15 16245 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16246 ret = false;
c316a17c 16247 goto copy_reloc;
5bd4f169 16248
65f38f15 16249 case R_PPC64_NONE:
411e1bfb 16250 case R_PPC64_TLS:
727fc41e
AM
16251 case R_PPC64_TLSGD:
16252 case R_PPC64_TLSLD:
3b421ab3 16253 case R_PPC64_TOCSAVE:
04c9666a
AM
16254 case R_PPC64_GNU_VTINHERIT:
16255 case R_PPC64_GNU_VTENTRY:
006589cf 16256 case R_PPC64_ENTRY:
4a421c53 16257 case R_PPC64_PCREL_OPT:
c316a17c 16258 goto copy_reloc;
5bd4f169
AM
16259
16260 /* GOT16 relocations. Like an ADDR16 using the symbol's
16261 address in the GOT as relocation value instead of the
411e1bfb 16262 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16263 symbol and put the symbol value there. */
411e1bfb
AM
16264 case R_PPC64_GOT_TLSGD16:
16265 case R_PPC64_GOT_TLSGD16_LO:
16266 case R_PPC64_GOT_TLSGD16_HI:
16267 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16268 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16269 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16270 goto dogot;
16271
16272 case R_PPC64_GOT_TLSLD16:
16273 case R_PPC64_GOT_TLSLD16_LO:
16274 case R_PPC64_GOT_TLSLD16_HI:
16275 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16276 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16277 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16278 goto dogot;
16279
16280 case R_PPC64_GOT_TPREL16_DS:
16281 case R_PPC64_GOT_TPREL16_LO_DS:
16282 case R_PPC64_GOT_TPREL16_HI:
16283 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16284 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16285 tls_type = TLS_TLS | TLS_TPREL;
16286 goto dogot;
16287
16288 case R_PPC64_GOT_DTPREL16_DS:
16289 case R_PPC64_GOT_DTPREL16_LO_DS:
16290 case R_PPC64_GOT_DTPREL16_HI:
16291 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16292 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16293 tls_type = TLS_TLS | TLS_DTPREL;
16294 goto dogot;
16295
65f38f15
AM
16296 case R_PPC64_GOT16:
16297 case R_PPC64_GOT16_LO:
16298 case R_PPC64_GOT16_HI:
16299 case R_PPC64_GOT16_HA:
16300 case R_PPC64_GOT16_DS:
16301 case R_PPC64_GOT16_LO_DS:
5663e321 16302 case R_PPC64_GOT_PCREL34:
411e1bfb 16303 dogot:
5bd4f169
AM
16304 {
16305 /* Relocation is to the entry for this symbol in the global
16306 offset table. */
e717da7e 16307 asection *got;
d881513a 16308 bfd_vma *offp;
5bd4f169 16309 bfd_vma off;
d881513a 16310 unsigned long indx = 0;
927be08e 16311 struct got_entry *ent;
65f38f15 16312
d881513a 16313 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16314 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16315 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16316 else
5bd4f169 16317 {
d881513a
AM
16318 if (h != NULL)
16319 {
f0158f44
AM
16320 if (!htab->elf.dynamic_sections_created
16321 || h->elf.dynindx == -1
16322 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16323 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16324 /* This is actually a static link, or it is a
16325 -Bsymbolic link and the symbol is defined
16326 locally, or the symbol was forced to be local
16327 because of a version file. */
16328 ;
16329 else
16330 {
039b3fef 16331 indx = h->elf.dynindx;
0a1b45a2 16332 unresolved_reloc = false;
d881513a 16333 }
039b3fef 16334 ent = h->elf.got.glist;
d881513a 16335 }
411e1bfb 16336 else
5bd4f169 16337 {
d881513a
AM
16338 if (local_got_ents == NULL)
16339 abort ();
16340 ent = local_got_ents[r_symndx];
5bd4f169 16341 }
d881513a
AM
16342
16343 for (; ent != NULL; ent = ent->next)
133a1f60 16344 if (ent->addend == orig_rel.r_addend
e717da7e 16345 && ent->owner == input_bfd
d881513a
AM
16346 && ent->tls_type == tls_type)
16347 break;
5bd4f169 16348 }
411e1bfb 16349
927be08e
AM
16350 if (ent == NULL)
16351 abort ();
16352 if (ent->is_indirect)
16353 ent = ent->got.ent;
16354 offp = &ent->got.offset;
16355 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16356 if (got == NULL)
16357 abort ();
16358
411e1bfb
AM
16359 /* The offset must always be a multiple of 8. We use the
16360 least significant bit to record whether we have already
16361 processed this entry. */
d881513a 16362 off = *offp;
411e1bfb
AM
16363 if ((off & 1) != 0)
16364 off &= ~1;
5bd4f169
AM
16365 else
16366 {
411e1bfb
AM
16367 /* Generate relocs for the dynamic linker, except in
16368 the case of TLSLD where we'll use one entry per
16369 module. */
25f23106 16370 asection *relgot;
0a1b45a2 16371 bool ifunc;
e717da7e 16372
d881513a 16373 *offp = off | 1;
25f23106
AM
16374 relgot = NULL;
16375 ifunc = (h != NULL
16376 ? h->elf.type == STT_GNU_IFUNC
16377 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16378 if (ifunc)
82e66161
AM
16379 {
16380 relgot = htab->elf.irelplt;
efb2a7b4 16381 if (indx == 0 || is_static_defined (&h->elf))
0a1b45a2 16382 htab->elf.ifunc_resolvers = true;
82e66161 16383 }
f0158f44
AM
16384 else if (indx != 0
16385 || (bfd_link_pic (info)
16386 && (h == NULL
f749f26e
AM
16387 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16388 && !(tls_type != 0
f15d0b54 16389 && bfd_link_executable (info)
9e51d549
AM
16390 && (h == NULL
16391 || SYMBOL_REFERENCES_LOCAL (info,
16392 &h->elf)))))
19e08130 16393 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16394 if (relgot != NULL)
5bd4f169 16395 {
e717da7e
AM
16396 outrel.r_offset = (got->output_section->vma
16397 + got->output_offset
411e1bfb 16398 + off);
133a1f60 16399 outrel.r_addend = orig_rel.r_addend;
d881513a 16400 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16401 {
411e1bfb 16402 outrel.r_addend = 0;
e515b051 16403 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16404 if (tls_type == (TLS_TLS | TLS_GD))
16405 {
e717da7e
AM
16406 loc = relgot->contents;
16407 loc += (relgot->reloc_count++
d881513a
AM
16408 * sizeof (Elf64_External_Rela));
16409 bfd_elf64_swap_reloca_out (output_bfd,
16410 &outrel, loc);
e515b051 16411 outrel.r_offset += 8;
133a1f60 16412 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16413 outrel.r_info
16414 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16415 }
411e1bfb 16416 }
951fd09b 16417 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16418 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16419 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16420 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16421 else if (indx != 0)
16422 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16423 else
81407a69 16424 {
25f23106
AM
16425 if (ifunc)
16426 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16427 else
16428 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16429
16430 /* Write the .got section contents for the sake
16431 of prelink. */
e717da7e 16432 loc = got->contents + off;
23fbd6fa
JJ
16433 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16434 loc);
81407a69 16435 }
81407a69
AM
16436
16437 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16438 {
16439 outrel.r_addend += relocation;
16440 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16441 {
16442 if (htab->elf.tls_sec == NULL)
16443 outrel.r_addend = 0;
16444 else
16445 outrel.r_addend -= htab->elf.tls_sec->vma;
16446 }
e515b051 16447 }
e717da7e
AM
16448 loc = relgot->contents;
16449 loc += (relgot->reloc_count++
411e1bfb
AM
16450 * sizeof (Elf64_External_Rela));
16451 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16452 }
16453
ad8e1ba5 16454 /* Init the .got section contents here if we're not
81407a69 16455 emitting a reloc. */
d881513a 16456 else
411e1bfb 16457 {
133a1f60 16458 relocation += orig_rel.r_addend;
f0158f44 16459 if (tls_type != 0)
411e1bfb 16460 {
989f9879
AM
16461 if (htab->elf.tls_sec == NULL)
16462 relocation = 0;
16463 else
16464 {
f0158f44
AM
16465 if (tls_type & TLS_LD)
16466 relocation = 0;
16467 else
16468 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16469 if (tls_type & TLS_TPREL)
989f9879
AM
16470 relocation += DTP_OFFSET - TP_OFFSET;
16471 }
5bd4f169 16472
f0158f44 16473 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16474 {
16475 bfd_put_64 (output_bfd, relocation,
e717da7e 16476 got->contents + off + 8);
676ee2b5 16477 relocation = 1;
7b609f53 16478 }
411e1bfb
AM
16479 }
16480 bfd_put_64 (output_bfd, relocation,
e717da7e 16481 got->contents + off);
5bd4f169
AM
16482 }
16483 }
16484
65f38f15
AM
16485 if (off >= (bfd_vma) -2)
16486 abort ();
16487
bf102f86 16488 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16489 addend = 0;
c213164a 16490 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16491 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16492 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16493 || r_type == R_PPC64_GOT_TPREL_PCREL34
16494 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 16495 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 16496 }
65f38f15
AM
16497 break;
16498
16499 case R_PPC64_PLT16_HA:
16500 case R_PPC64_PLT16_HI:
16501 case R_PPC64_PLT16_LO:
08be3224 16502 case R_PPC64_PLT16_LO_DS:
5663e321
AM
16503 case R_PPC64_PLT_PCREL34:
16504 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
16505 case R_PPC64_PLT32:
16506 case R_PPC64_PLT64:
23cedd1d 16507 case R_PPC64_PLTSEQ:
5663e321 16508 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 16509 case R_PPC64_PLTCALL:
5663e321 16510 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
16511 /* Relocation is to the entry for this symbol in the
16512 procedure linkage table. */
0a1b45a2 16513 unresolved_reloc = true;
cbf95972
AM
16514 {
16515 struct plt_entry **plt_list = NULL;
16516 if (h != NULL)
16517 plt_list = &h->elf.plt.plist;
16518 else if (local_got_ents != NULL)
16519 {
16520 struct plt_entry **local_plt = (struct plt_entry **)
16521 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 16522 plt_list = local_plt + r_symndx;
cbf95972
AM
16523 }
16524 if (plt_list)
16525 {
16526 struct plt_entry *ent;
65f38f15 16527
cbf95972
AM
16528 for (ent = *plt_list; ent != NULL; ent = ent->next)
16529 if (ent->plt.offset != (bfd_vma) -1
133a1f60 16530 && ent->addend == orig_rel.r_addend)
cbf95972
AM
16531 {
16532 asection *plt;
08be3224 16533 bfd_vma got;
cbf95972
AM
16534
16535 plt = htab->elf.splt;
30845f11 16536 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
16537 {
16538 if (h != NULL
16539 ? h->elf.type == STT_GNU_IFUNC
16540 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16541 plt = htab->elf.iplt;
16542 else
16543 plt = htab->pltlocal;
16544 }
16545 relocation = (plt->output_section->vma
16546 + plt->output_offset
16547 + ent->plt.offset);
08be3224 16548 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
16549 || r_type == R_PPC64_PLT16_HI
16550 || r_type == R_PPC64_PLT16_LO
16551 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
16552 {
16553 got = (elf_gp (output_bfd)
16554 + htab->sec_info[input_section->id].toc_off);
16555 relocation -= got;
16556 }
133a1f60 16557 addend = 0;
0a1b45a2 16558 unresolved_reloc = false;
cbf95972
AM
16559 break;
16560 }
16561 }
16562 }
65f38f15 16563 break;
5bd4f169 16564
0b13192e
AM
16565 case R_PPC64_TOC:
16566 /* Relocation value is TOC base. */
16567 relocation = TOCstart;
cf35638d 16568 if (r_symndx == STN_UNDEF)
6f20ed8a 16569 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
16570 else if (unresolved_reloc)
16571 ;
6f20ed8a
AM
16572 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16573 relocation += htab->sec_info[sec->id].toc_off;
0b13192e 16574 else
0a1b45a2 16575 unresolved_reloc = true;
ab96bf03 16576 goto dodyn;
0b13192e 16577
5bd4f169
AM
16578 /* TOC16 relocs. We want the offset relative to the TOC base,
16579 which is the address of the start of the TOC plus 0x8000.
16580 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16581 in this order. */
65f38f15
AM
16582 case R_PPC64_TOC16:
16583 case R_PPC64_TOC16_LO:
16584 case R_PPC64_TOC16_HI:
16585 case R_PPC64_TOC16_DS:
16586 case R_PPC64_TOC16_LO_DS:
16587 case R_PPC64_TOC16_HA:
6f20ed8a 16588 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
16589 if (h != NULL)
16590 goto dodyn;
5bd4f169
AM
16591 break;
16592
16593 /* Relocate against the beginning of the section. */
65f38f15
AM
16594 case R_PPC64_SECTOFF:
16595 case R_PPC64_SECTOFF_LO:
16596 case R_PPC64_SECTOFF_HI:
16597 case R_PPC64_SECTOFF_DS:
16598 case R_PPC64_SECTOFF_LO_DS:
16599 case R_PPC64_SECTOFF_HA:
4ce794b7 16600 if (sec != NULL)
65f38f15 16601 addend -= sec->output_section->vma;
5bd4f169
AM
16602 break;
16603
25f23106
AM
16604 case R_PPC64_REL16:
16605 case R_PPC64_REL16_LO:
16606 case R_PPC64_REL16_HI:
16607 case R_PPC64_REL16_HA:
4a969973
AM
16608 case R_PPC64_REL16_HIGH:
16609 case R_PPC64_REL16_HIGHA:
16610 case R_PPC64_REL16_HIGHER:
16611 case R_PPC64_REL16_HIGHERA:
16612 case R_PPC64_REL16_HIGHEST:
16613 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
16614 case R_PPC64_REL16_HIGHER34:
16615 case R_PPC64_REL16_HIGHERA34:
16616 case R_PPC64_REL16_HIGHEST34:
16617 case R_PPC64_REL16_HIGHESTA34:
a680de9a 16618 case R_PPC64_REL16DX_HA:
721956f4
AM
16619 case R_PPC64_REL14:
16620 case R_PPC64_REL14_BRNTAKEN:
16621 case R_PPC64_REL14_BRTAKEN:
5d1634d7 16622 case R_PPC64_REL24:
05d0e962 16623 case R_PPC64_REL24_NOTOC:
5663e321
AM
16624 case R_PPC64_PCREL34:
16625 case R_PPC64_PCREL28:
5d1634d7
AM
16626 break;
16627
411e1bfb
AM
16628 case R_PPC64_TPREL16:
16629 case R_PPC64_TPREL16_LO:
16630 case R_PPC64_TPREL16_HI:
16631 case R_PPC64_TPREL16_HA:
16632 case R_PPC64_TPREL16_DS:
16633 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
16634 case R_PPC64_TPREL16_HIGH:
16635 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
16636 case R_PPC64_TPREL16_HIGHER:
16637 case R_PPC64_TPREL16_HIGHERA:
16638 case R_PPC64_TPREL16_HIGHEST:
16639 case R_PPC64_TPREL16_HIGHESTA:
c213164a 16640 case R_PPC64_TPREL34:
766bc656
AM
16641 if (h != NULL
16642 && h->elf.root.type == bfd_link_hash_undefweak
16643 && h->elf.dynindx == -1)
16644 {
16645 /* Make this relocation against an undefined weak symbol
16646 resolve to zero. This is really just a tweak, since
16647 code using weak externs ought to check that they are
16648 defined before using them. */
16649 bfd_byte *p = contents + rel->r_offset - d_offset;
16650
95f0d0d2 16651 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
16652 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16653 if (insn != 0)
95f0d0d2 16654 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
16655 break;
16656 }
989f9879
AM
16657 if (htab->elf.tls_sec != NULL)
16658 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
16659 /* The TPREL16 relocs shouldn't really be used in shared
16660 libs or with non-local symbols as that will result in
16661 DT_TEXTREL being set, but support them anyway. */
16662 goto dodyn;
411e1bfb
AM
16663
16664 case R_PPC64_DTPREL16:
16665 case R_PPC64_DTPREL16_LO:
16666 case R_PPC64_DTPREL16_HI:
16667 case R_PPC64_DTPREL16_HA:
16668 case R_PPC64_DTPREL16_DS:
16669 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
16670 case R_PPC64_DTPREL16_HIGH:
16671 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
16672 case R_PPC64_DTPREL16_HIGHER:
16673 case R_PPC64_DTPREL16_HIGHERA:
16674 case R_PPC64_DTPREL16_HIGHEST:
16675 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 16676 case R_PPC64_DTPREL34:
989f9879
AM
16677 if (htab->elf.tls_sec != NULL)
16678 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
16679 break;
16680
45965137
AM
16681 case R_PPC64_ADDR64_LOCAL:
16682 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16683 ? h->elf.other
16684 : sym->st_other);
16685 break;
16686
e515b051
AM
16687 case R_PPC64_DTPMOD64:
16688 relocation = 1;
16689 addend = 0;
16690 goto dodyn;
16691
411e1bfb 16692 case R_PPC64_TPREL64:
989f9879
AM
16693 if (htab->elf.tls_sec != NULL)
16694 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
16695 goto dodyn;
16696
16697 case R_PPC64_DTPREL64:
989f9879
AM
16698 if (htab->elf.tls_sec != NULL)
16699 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 16700 /* Fall through. */
411e1bfb 16701
65f38f15
AM
16702 /* Relocations that may need to be propagated if this is a
16703 dynamic object. */
04c9666a 16704 case R_PPC64_REL30:
65f38f15
AM
16705 case R_PPC64_REL32:
16706 case R_PPC64_REL64:
16707 case R_PPC64_ADDR14:
16708 case R_PPC64_ADDR14_BRNTAKEN:
16709 case R_PPC64_ADDR14_BRTAKEN:
16710 case R_PPC64_ADDR16:
16711 case R_PPC64_ADDR16_DS:
16712 case R_PPC64_ADDR16_HA:
16713 case R_PPC64_ADDR16_HI:
f9c6b907
AM
16714 case R_PPC64_ADDR16_HIGH:
16715 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16716 case R_PPC64_ADDR16_HIGHER:
16717 case R_PPC64_ADDR16_HIGHERA:
16718 case R_PPC64_ADDR16_HIGHEST:
16719 case R_PPC64_ADDR16_HIGHESTA:
16720 case R_PPC64_ADDR16_LO:
16721 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
16722 case R_PPC64_ADDR16_HIGHER34:
16723 case R_PPC64_ADDR16_HIGHERA34:
16724 case R_PPC64_ADDR16_HIGHEST34:
16725 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 16726 case R_PPC64_ADDR24:
65f38f15
AM
16727 case R_PPC64_ADDR32:
16728 case R_PPC64_ADDR64:
16729 case R_PPC64_UADDR16:
16730 case R_PPC64_UADDR32:
16731 case R_PPC64_UADDR64:
5663e321
AM
16732 case R_PPC64_D34:
16733 case R_PPC64_D34_LO:
16734 case R_PPC64_D34_HI30:
16735 case R_PPC64_D34_HA30:
16736 case R_PPC64_D28:
411e1bfb 16737 dodyn:
5d1634d7 16738 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
16739 break;
16740
41bd81ab
AM
16741 if (NO_OPD_RELOCS && is_opd)
16742 break;
16743
8a9e8e72 16744 if (bfd_link_pic (info)
b1b07054 16745 ? ((h == NULL
190eb1dd 16746 || h->elf.dyn_relocs != NULL)
b1b07054
AM
16747 && ((h != NULL && pc_dynrelocs (h))
16748 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 16749 : (h != NULL
190eb1dd 16750 ? h->elf.dyn_relocs != NULL
d311bc8b 16751 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 16752 {
0a1b45a2 16753 bool skip, relocate;
65f38f15 16754 asection *sreloc;
1cf1f670 16755 bfd_vma out_off;
82e66161 16756 long indx = 0;
65f38f15
AM
16757
16758 /* When generating a dynamic object, these relocations
16759 are copied into the output file to be resolved at run
16760 time. */
16761
0a1b45a2
AM
16762 skip = false;
16763 relocate = false;
65f38f15 16764
1cf1f670
AM
16765 out_off = _bfd_elf_section_offset (output_bfd, info,
16766 input_section, rel->r_offset);
16767 if (out_off == (bfd_vma) -1)
0a1b45a2 16768 skip = true;
1cf1f670 16769 else if (out_off == (bfd_vma) -2)
0a1b45a2 16770 skip = true, relocate = true;
1cf1f670
AM
16771 out_off += (input_section->output_section->vma
16772 + input_section->output_offset);
16773 outrel.r_offset = out_off;
411e1bfb 16774 outrel.r_addend = rel->r_addend;
65f38f15 16775
1cf1f670
AM
16776 /* Optimize unaligned reloc use. */
16777 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16778 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16779 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16780 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16781 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16782 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16783 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16784 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16785 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16786
65f38f15 16787 if (skip)
0bb2d96a 16788 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
16789 else if (h != NULL
16790 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16791 && !is_opd
16792 && r_type != R_PPC64_TOC)
14acf4dc 16793 {
82e66161
AM
16794 indx = h->elf.dynindx;
16795 BFD_ASSERT (indx != -1);
16796 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16797 }
65f38f15
AM
16798 else
16799 {
41bd81ab
AM
16800 /* This symbol is local, or marked to become local,
16801 or this is an opd section reloc which must point
16802 at a local function. */
65f38f15 16803 outrel.r_addend += relocation;
e86ce104 16804 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16805 {
3fad3c7c 16806 if (is_opd && h != NULL)
afbe61cf
AM
16807 {
16808 /* Lie about opd entries. This case occurs
16809 when building shared libraries and we
16810 reference a function in another shared
3fad3c7c
AM
16811 lib. The same thing happens for a weak
16812 definition in an application that's
16813 overridden by a strong definition in a
16814 shared lib. (I believe this is a generic
16815 bug in binutils handling of weak syms.)
16816 In these cases we won't use the opd
1e2f5b6e 16817 entry in this lib. */
0a1b45a2 16818 unresolved_reloc = false;
afbe61cf 16819 }
25f23106
AM
16820 if (!is_opd
16821 && r_type == R_PPC64_ADDR64
16822 && (h != NULL
16823 ? h->elf.type == STT_GNU_IFUNC
16824 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16825 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16826 else
16827 {
16828 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16829
25f23106
AM
16830 /* We need to relocate .opd contents for ld.so.
16831 Prelink also wants simple and consistent rules
16832 for relocs. This make all RELATIVE relocs have
16833 *r_offset equal to r_addend. */
0a1b45a2 16834 relocate = true;
25f23106 16835 }
65f38f15
AM
16836 }
16837 else
16838 {
25f23106
AM
16839 if (h != NULL
16840 ? h->elf.type == STT_GNU_IFUNC
16841 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16842 {
25f53a85 16843 info->callbacks->einfo
695344c0 16844 /* xgettext:c-format */
174d0a74 16845 (_("%H: %s for indirect "
c1c8c1ef 16846 "function `%pT' unsupported\n"),
25f53a85 16847 input_bfd, input_section, rel->r_offset,
25f23106
AM
16848 ppc64_elf_howto_table[r_type]->name,
16849 sym_name);
0a1b45a2 16850 ret = false;
25f23106 16851 }
cf35638d 16852 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16853 ;
16854 else if (sec == NULL || sec->owner == NULL)
16855 {
16856 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16857 return false;
65f38f15
AM
16858 }
16859 else
16860 {
f26a3287 16861 asection *osec = sec->output_section;
65f38f15 16862
f26a3287
AM
16863 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16864 {
16865 /* TLS symbol values are relative to the
16866 TLS segment. Dynamic relocations for
16867 local TLS symbols therefore can't be
16868 reduced to a relocation against their
16869 section symbol because it holds the
16870 address of the section, not a value
16871 relative to the TLS segment. We could
16872 change the .tdata dynamic section symbol
16873 to be zero value but STN_UNDEF works
16874 and is used elsewhere, eg. for TPREL64
16875 GOT relocs against local TLS symbols. */
16876 osec = htab->elf.tls_sec;
16877 indx = 0;
16878 }
16879 else
74541ad4 16880 {
74541ad4 16881 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16882 if (indx == 0)
16883 {
16884 if ((osec->flags & SEC_READONLY) == 0
16885 && htab->elf.data_index_section != NULL)
16886 osec = htab->elf.data_index_section;
16887 else
16888 osec = htab->elf.text_index_section;
16889 indx = elf_section_data (osec)->dynindx;
16890 }
16891 BFD_ASSERT (indx != 0);
74541ad4 16892 }
74541ad4 16893
65f38f15
AM
16894 /* We are turning this relocation into one
16895 against a section symbol, so subtract out
16896 the output section's address but not the
16897 offset of the input section in the output
16898 section. */
16899 outrel.r_addend -= osec->vma;
16900 }
16901
16902 outrel.r_info = ELF64_R_INFO (indx, r_type);
16903 }
16904 }
16905
16906 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16907 if (h != NULL
16908 ? h->elf.type == STT_GNU_IFUNC
16909 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16910 {
16911 sreloc = htab->elf.irelplt;
efb2a7b4 16912 if (indx == 0 || is_static_defined (&h->elf))
0a1b45a2 16913 htab->elf.ifunc_resolvers = true;
82e66161 16914 }
65f38f15
AM
16915 if (sreloc == NULL)
16916 abort ();
16917
dfbb6ac9
AM
16918 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16919 >= sreloc->size)
16920 abort ();
947216bf
AM
16921 loc = sreloc->contents;
16922 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16923 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16924
e59a1001
AM
16925 if (!warned_dynamic
16926 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16927 {
16928 info->callbacks->einfo
16929 /* xgettext:c-format */
16930 (_("%X%P: %pB: %s against %pT "
16931 "is not supported by glibc as a dynamic relocation\n"),
16932 input_bfd,
16933 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16934 sym_name);
0a1b45a2 16935 warned_dynamic = true;
e59a1001
AM
16936 }
16937
65f38f15
AM
16938 /* If this reloc is against an external symbol, it will
16939 be computed at runtime, so there's no need to do
81407a69
AM
16940 anything now. However, for the sake of prelink ensure
16941 that the section contents are a known value. */
2cdcc330 16942 if (!relocate)
81407a69 16943 {
0a1b45a2 16944 unresolved_reloc = false;
81407a69
AM
16945 /* The value chosen here is quite arbitrary as ld.so
16946 ignores section contents except for the special
16947 case of .opd where the contents might be accessed
16948 before relocation. Choose zero, as that won't
16949 cause reloc overflow. */
16950 relocation = 0;
16951 addend = 0;
16952 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16953 to improve backward compatibility with older
16954 versions of ld. */
16955 if (r_type == R_PPC64_ADDR64)
16956 addend = outrel.r_addend;
16957 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16958 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16959 addend = outrel.r_offset;
81407a69 16960 }
65f38f15 16961 }
5bd4f169
AM
16962 break;
16963
65f38f15
AM
16964 case R_PPC64_COPY:
16965 case R_PPC64_GLOB_DAT:
16966 case R_PPC64_JMP_SLOT:
25f23106 16967 case R_PPC64_JMP_IREL:
65f38f15
AM
16968 case R_PPC64_RELATIVE:
16969 /* We shouldn't ever see these dynamic relocs in relocatable
16970 files. */
ae9a127f 16971 /* Fall through. */
65f38f15
AM
16972
16973 case R_PPC64_PLTGOT16:
16974 case R_PPC64_PLTGOT16_DS:
16975 case R_PPC64_PLTGOT16_HA:
16976 case R_PPC64_PLTGOT16_HI:
16977 case R_PPC64_PLTGOT16_LO:
16978 case R_PPC64_PLTGOT16_LO_DS:
16979 case R_PPC64_PLTREL32:
16980 case R_PPC64_PLTREL64:
16981 /* These ones haven't been implemented yet. */
16982
25f53a85 16983 info->callbacks->einfo
695344c0 16984 /* xgettext:c-format */
c1c8c1ef 16985 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16986 input_bfd,
4ce794b7 16987 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16988
16989 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 16990 ret = false;
c316a17c 16991 goto copy_reloc;
65f38f15 16992 }
5bd4f169 16993
67f0cbdb
AM
16994 /* Multi-instruction sequences that access the TOC can be
16995 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16996 to nop; addi rb,r2,x; */
67f0cbdb
AM
16997 switch (r_type)
16998 {
16999 default:
17000 break;
17001
17002 case R_PPC64_GOT_TLSLD16_HI:
17003 case R_PPC64_GOT_TLSGD16_HI:
17004 case R_PPC64_GOT_TPREL16_HI:
17005 case R_PPC64_GOT_DTPREL16_HI:
17006 case R_PPC64_GOT16_HI:
17007 case R_PPC64_TOC16_HI:
17008 /* These relocs would only be useful if building up an
17009 offset to later add to r2, perhaps in an indexed
17010 addressing mode instruction. Don't try to optimize.
17011 Unfortunately, the possibility of someone building up an
17012 offset like this or even with the HA relocs, means that
17013 we need to check the high insn when optimizing the low
17014 insn. */
17015 break;
17016
5663e321
AM
17017 case R_PPC64_PLTCALL_NOTOC:
17018 if (!unresolved_reloc)
17019 htab->notoc_plt = 1;
17020 /* Fall through. */
23cedd1d
AM
17021 case R_PPC64_PLTCALL:
17022 if (unresolved_reloc)
17023 {
17024 /* No plt entry. Make this into a direct call. */
17025 bfd_byte *p = contents + rel->r_offset;
17026 insn = bfd_get_32 (input_bfd, p);
17027 insn &= 1;
17028 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
17029 if (r_type == R_PPC64_PLTCALL)
17030 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
17031 unresolved_reloc = save_unresolved_reloc;
17032 r_type = R_PPC64_REL24;
17033 }
17034 break;
17035
5663e321 17036 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17037 case R_PPC64_PLTSEQ:
17038 if (unresolved_reloc)
17039 {
0a1b45a2 17040 unresolved_reloc = false;
23cedd1d
AM
17041 goto nop_it;
17042 }
17043 break;
17044
5663e321
AM
17045 case R_PPC64_PLT_PCREL34_NOTOC:
17046 if (!unresolved_reloc)
17047 htab->notoc_plt = 1;
17048 /* Fall through. */
17049 case R_PPC64_PLT_PCREL34:
17050 if (unresolved_reloc)
17051 {
17052 bfd_byte *p = contents + rel->r_offset;
17053 bfd_put_32 (input_bfd, PNOP >> 32, p);
17054 bfd_put_32 (input_bfd, PNOP, p + 4);
0a1b45a2 17055 unresolved_reloc = false;
5663e321
AM
17056 goto copy_reloc;
17057 }
17058 break;
17059
23cedd1d
AM
17060 case R_PPC64_PLT16_HA:
17061 if (unresolved_reloc)
17062 {
0a1b45a2 17063 unresolved_reloc = false;
23cedd1d
AM
17064 goto nop_it;
17065 }
17066 /* Fall through. */
67f0cbdb
AM
17067 case R_PPC64_GOT_TLSLD16_HA:
17068 case R_PPC64_GOT_TLSGD16_HA:
17069 case R_PPC64_GOT_TPREL16_HA:
17070 case R_PPC64_GOT_DTPREL16_HA:
17071 case R_PPC64_GOT16_HA:
17072 case R_PPC64_TOC16_HA:
98528052 17073 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17074 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 17075 {
23cedd1d
AM
17076 bfd_byte *p;
17077 nop_it:
17078 p = contents + (rel->r_offset & ~3);
98528052 17079 bfd_put_32 (input_bfd, NOP, p);
d830549d 17080 goto copy_reloc;
98528052 17081 }
67f0cbdb
AM
17082 break;
17083
23cedd1d
AM
17084 case R_PPC64_PLT16_LO:
17085 case R_PPC64_PLT16_LO_DS:
17086 if (unresolved_reloc)
17087 {
0a1b45a2 17088 unresolved_reloc = false;
23cedd1d
AM
17089 goto nop_it;
17090 }
17091 /* Fall through. */
67f0cbdb
AM
17092 case R_PPC64_GOT_TLSLD16_LO:
17093 case R_PPC64_GOT_TLSGD16_LO:
17094 case R_PPC64_GOT_TPREL16_LO_DS:
17095 case R_PPC64_GOT_DTPREL16_LO_DS:
17096 case R_PPC64_GOT16_LO:
17097 case R_PPC64_GOT16_LO_DS:
17098 case R_PPC64_TOC16_LO:
17099 case R_PPC64_TOC16_LO_DS:
98528052 17100 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17101 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
17102 {
17103 bfd_byte *p = contents + (rel->r_offset & ~3);
17104 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17105 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17106 {
17107 /* Transform addic to addi when we change reg. */
2365f8d7 17108 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17109 insn |= (14u << 26) | (2 << 16);
17110 }
17111 else
67f0cbdb 17112 {
98528052
AM
17113 insn &= ~(0x1f << 16);
17114 insn |= 2 << 16;
67f0cbdb 17115 }
560c8763 17116 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17117 }
17118 break;
9a23f96e
AM
17119
17120 case R_PPC64_TPREL16_HA:
17121 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17122 {
17123 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17124 bfd_put_32 (input_bfd, NOP, p);
17125 goto copy_reloc;
9a23f96e
AM
17126 }
17127 break;
17128
17129 case R_PPC64_TPREL16_LO:
17130 case R_PPC64_TPREL16_LO_DS:
17131 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17132 {
17133 bfd_byte *p = contents + (rel->r_offset & ~3);
17134 insn = bfd_get_32 (input_bfd, p);
17135 insn &= ~(0x1f << 16);
17136 insn |= 13 << 16;
17137 bfd_put_32 (input_bfd, insn, p);
17138 }
17139 break;
67f0cbdb
AM
17140 }
17141
65f38f15
AM
17142 /* Do any further special processing. */
17143 switch (r_type)
17144 {
17145 default:
17146 break;
17147
25f23106 17148 case R_PPC64_REL16_HA:
4a969973
AM
17149 case R_PPC64_REL16_HIGHA:
17150 case R_PPC64_REL16_HIGHERA:
17151 case R_PPC64_REL16_HIGHESTA:
a680de9a 17152 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17153 case R_PPC64_ADDR16_HA:
17154 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17155 case R_PPC64_ADDR16_HIGHERA:
17156 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17157 case R_PPC64_TOC16_HA:
17158 case R_PPC64_SECTOFF_HA:
411e1bfb 17159 case R_PPC64_TPREL16_HA:
f9c6b907 17160 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17161 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17162 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17163 case R_PPC64_DTPREL16_HA:
17164 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17165 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17166 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17167 /* It's just possible that this symbol is a weak symbol
17168 that's not actually defined anywhere. In that case,
17169 'sec' would be NULL, and we should leave the symbol
17170 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17171 if (sec == NULL)
17172 break;
1a0670f3 17173 /* Fall through. */
5c5f6e17
AM
17174
17175 case R_PPC64_GOT16_HA:
17176 case R_PPC64_PLTGOT16_HA:
17177 case R_PPC64_PLT16_HA:
17178 case R_PPC64_GOT_TLSGD16_HA:
17179 case R_PPC64_GOT_TLSLD16_HA:
17180 case R_PPC64_GOT_TPREL16_HA:
17181 case R_PPC64_GOT_DTPREL16_HA:
17182 /* Add 0x10000 if sign bit in 0:15 is set.
17183 Bits 0:15 are not used. */
17184 addend += 0x8000;
65f38f15
AM
17185 break;
17186
5663e321
AM
17187 case R_PPC64_D34_HA30:
17188 case R_PPC64_ADDR16_HIGHERA34:
17189 case R_PPC64_ADDR16_HIGHESTA34:
17190 case R_PPC64_REL16_HIGHERA34:
17191 case R_PPC64_REL16_HIGHESTA34:
17192 if (sec != NULL)
17193 addend += 1ULL << 33;
17194 break;
17195
65f38f15
AM
17196 case R_PPC64_ADDR16_DS:
17197 case R_PPC64_ADDR16_LO_DS:
17198 case R_PPC64_GOT16_DS:
17199 case R_PPC64_GOT16_LO_DS:
17200 case R_PPC64_PLT16_LO_DS:
17201 case R_PPC64_SECTOFF_DS:
17202 case R_PPC64_SECTOFF_LO_DS:
17203 case R_PPC64_TOC16_DS:
17204 case R_PPC64_TOC16_LO_DS:
17205 case R_PPC64_PLTGOT16_DS:
17206 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17207 case R_PPC64_GOT_TPREL16_DS:
17208 case R_PPC64_GOT_TPREL16_LO_DS:
17209 case R_PPC64_GOT_DTPREL16_DS:
17210 case R_PPC64_GOT_DTPREL16_LO_DS:
17211 case R_PPC64_TPREL16_DS:
17212 case R_PPC64_TPREL16_LO_DS:
17213 case R_PPC64_DTPREL16_DS:
17214 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
17215 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17216 mask = 3;
a680de9a
PB
17217 /* If this reloc is against an lq, lxv, or stxv insn, then
17218 the value must be a multiple of 16. This is somewhat of
17219 a hack, but the "correct" way to do this by defining _DQ
17220 forms of all the _DS relocs bloats all reloc switches in
17221 this file. It doesn't make much sense to use these
17222 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17223 if ((insn & (0x3fu << 26)) == (56u << 26)
17224 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17225 mask = 15;
a680de9a
PB
17226 relocation += addend;
17227 addend = insn & (mask ^ 3);
17228 if ((relocation & mask) != 0)
65f38f15 17229 {
a680de9a 17230 relocation ^= relocation & mask;
25f53a85 17231 info->callbacks->einfo
695344c0 17232 /* xgettext:c-format */
174d0a74 17233 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17234 input_bfd, input_section, rel->r_offset,
d830549d 17235 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17236 mask + 1);
65f38f15 17237 bfd_set_error (bfd_error_bad_value);
0a1b45a2 17238 ret = false;
c316a17c 17239 goto copy_reloc;
65f38f15
AM
17240 }
17241 break;
5bd4f169
AM
17242 }
17243
239e1f3a
AM
17244 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17245 because such sections are not SEC_ALLOC and thus ld.so will
17246 not process them. */
d830549d 17247 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17248 if (unresolved_reloc
239e1f3a 17249 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17250 && h->elf.def_dynamic)
17251 && _bfd_elf_section_offset (output_bfd, info, input_section,
17252 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17253 {
25f53a85 17254 info->callbacks->einfo
695344c0 17255 /* xgettext:c-format */
c1c8c1ef 17256 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17257 input_bfd, input_section, rel->r_offset,
b80eed39 17258 howto->name,
039b3fef 17259 h->elf.root.root.string);
0a1b45a2 17260 ret = false;
9c07fe7c 17261 }
5bd4f169 17262
b80eed39
AM
17263 /* 16-bit fields in insns mostly have signed values, but a
17264 few insns have 16-bit unsigned values. Really, we should
17265 have different reloc types. */
17266 if (howto->complain_on_overflow != complain_overflow_dont
17267 && howto->dst_mask == 0xffff
17268 && (input_section->flags & SEC_CODE) != 0)
17269 {
17270 enum complain_overflow complain = complain_overflow_signed;
17271
17272 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17273 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17274 complain = complain_overflow_bitfield;
17275 else if (howto->rightshift == 0
2365f8d7
AM
17276 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17277 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17278 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17279 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17280 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17281 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17282 complain = complain_overflow_unsigned;
17283 if (howto->complain_on_overflow != complain)
17284 {
17285 alt_howto = *howto;
17286 alt_howto.complain_on_overflow = complain;
17287 howto = &alt_howto;
17288 }
17289 }
17290
5663e321 17291 switch (r_type)
a680de9a 17292 {
5663e321
AM
17293 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17294 case R_PPC64_D34:
17295 case R_PPC64_D34_LO:
17296 case R_PPC64_D34_HI30:
17297 case R_PPC64_D34_HA30:
17298 case R_PPC64_PCREL34:
17299 case R_PPC64_GOT_PCREL34:
c213164a
AM
17300 case R_PPC64_TPREL34:
17301 case R_PPC64_DTPREL34:
87c69f97
AM
17302 case R_PPC64_GOT_TLSGD_PCREL34:
17303 case R_PPC64_GOT_TLSLD_PCREL34:
17304 case R_PPC64_GOT_TPREL_PCREL34:
17305 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17306 case R_PPC64_PLT_PCREL34:
17307 case R_PPC64_PLT_PCREL34_NOTOC:
17308 case R_PPC64_D28:
17309 case R_PPC64_PCREL28:
17310 if (rel->r_offset + 8 > input_section->size)
17311 r = bfd_reloc_outofrange;
17312 else
17313 {
5663e321
AM
17314 relocation += addend;
17315 if (howto->pc_relative)
17316 relocation -= (rel->r_offset
17317 + input_section->output_offset
17318 + input_section->output_section->vma);
17319 relocation >>= howto->rightshift;
17320
17321 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17322 pinsn <<= 32;
17323 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17324
17325 pinsn &= ~howto->dst_mask;
17326 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17327 & howto->dst_mask);
17328 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17329 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17330 r = bfd_reloc_ok;
17331 if (howto->complain_on_overflow == complain_overflow_signed
17332 && (relocation + (1ULL << (howto->bitsize - 1))
17333 >= 1ULL << howto->bitsize))
17334 r = bfd_reloc_overflow;
17335 }
17336 break;
17337
17338 case R_PPC64_REL16DX_HA:
a680de9a
PB
17339 if (rel->r_offset + 4 > input_section->size)
17340 r = bfd_reloc_outofrange;
17341 else
17342 {
17343 relocation += addend;
17344 relocation -= (rel->r_offset
17345 + input_section->output_offset
17346 + input_section->output_section->vma);
3de43e7b 17347 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17348 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17349 insn &= ~0x1fffc1;
3de43e7b 17350 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17351 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17352 r = bfd_reloc_ok;
3de43e7b 17353 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17354 r = bfd_reloc_overflow;
17355 }
5663e321
AM
17356 break;
17357
17358 default:
17359 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17360 contents, rel->r_offset,
17361 relocation, addend);
a680de9a 17362 }
5bd4f169 17363
ef60b7ff 17364 if (r != bfd_reloc_ok)
5bd4f169 17365 {
bc30df16 17366 char *more_info = NULL;
b80eed39 17367 const char *reloc_name = howto->name;
bc30df16
AM
17368
17369 if (reloc_dest != DEST_NORMAL)
17370 {
17371 more_info = bfd_malloc (strlen (reloc_name) + 8);
17372 if (more_info != NULL)
17373 {
17374 strcpy (more_info, reloc_name);
17375 strcat (more_info, (reloc_dest == DEST_OPD
17376 ? " (OPD)" : " (stub)"));
17377 reloc_name = more_info;
17378 }
17379 }
17380
cd27b276 17381 if (r == bfd_reloc_overflow)
5bd4f169 17382 {
8131c122
AM
17383 /* On code like "if (foo) foo();" don't report overflow
17384 on a branch to zero when foo is undefined. */
17385 if (!warned
17386 && (reloc_dest == DEST_STUB
17387 || !(h != NULL
17388 && (h->elf.root.type == bfd_link_hash_undefweak
17389 || h->elf.root.type == bfd_link_hash_undefined)
17390 && is_branch_reloc (r_type))))
9e51d549
AM
17391 info->callbacks->reloc_overflow
17392 (info, (struct bfd_link_hash_entry *) h, sym_name,
17393 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17394 rel->r_offset);
ef60b7ff
AM
17395 }
17396 else
17397 {
25f53a85 17398 info->callbacks->einfo
695344c0 17399 /* xgettext:c-format */
c1c8c1ef 17400 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17401 input_bfd, input_section, rel->r_offset,
bc30df16 17402 reloc_name, sym_name, (int) r);
0a1b45a2 17403 ret = false;
ef60b7ff 17404 }
c9594989 17405 free (more_info);
5bd4f169 17406 }
c316a17c
AM
17407 copy_reloc:
17408 if (wrel != rel)
17409 *wrel = *rel;
17410 }
17411
17412 if (wrel != rel)
17413 {
17414 Elf_Internal_Shdr *rel_hdr;
17415 size_t deleted = rel - wrel;
17416
17417 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17418 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17419 if (rel_hdr->sh_size == 0)
17420 {
17421 /* It is too late to remove an empty reloc section. Leave
17422 one NONE reloc.
17423 ??? What is wrong with an empty section??? */
17424 rel_hdr->sh_size = rel_hdr->sh_entsize;
17425 deleted -= 1;
17426 }
17427 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17428 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17429 input_section->reloc_count -= deleted;
5bd4f169
AM
17430 }
17431
645ea6a9
AM
17432 /* If we're emitting relocations, then shortly after this function
17433 returns, reloc offsets and addends for this section will be
17434 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17435 file rather than the input. Save a copy of the relocs for
17436 opd_entry_value. */
0e1862bb 17437 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17438 {
17439 bfd_size_type amt;
17440 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17441 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17442 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17443 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f 17444 if (rel == NULL)
0a1b45a2 17445 return false;
8860955f
AM
17446 memcpy (rel, relocs, amt);
17447 }
5bd4f169
AM
17448 return ret;
17449}
17450
754021d0
AM
17451/* Adjust the value of any local symbols in opd sections. */
17452
6e0b88f1 17453static int
754021d0
AM
17454ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17455 const char *name ATTRIBUTE_UNUSED,
17456 Elf_Internal_Sym *elfsym,
17457 asection *input_sec,
17458 struct elf_link_hash_entry *h)
17459{
74f0fb50
AM
17460 struct _opd_sec_data *opd;
17461 long adjust;
754021d0
AM
17462 bfd_vma value;
17463
4025353c 17464 if (h != NULL)
6e0b88f1 17465 return 1;
4025353c 17466
74f0fb50
AM
17467 opd = get_opd_info (input_sec);
17468 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 17469 return 1;
754021d0
AM
17470
17471 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 17472 if (!bfd_link_relocatable (info))
754021d0
AM
17473 value -= input_sec->output_section->vma;
17474
51aecdc5 17475 adjust = opd->adjust[OPD_NDX (value)];
4025353c 17476 if (adjust == -1)
6e0b88f1
AM
17477 return 2;
17478
17479 elfsym->st_value += adjust;
17480 return 1;
754021d0
AM
17481}
17482
5bd4f169
AM
17483/* Finish up dynamic symbol handling. We set the contents of various
17484 dynamic sections here. */
17485
0a1b45a2 17486static bool
4ce794b7
AM
17487ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17488 struct bfd_link_info *info,
17489 struct elf_link_hash_entry *h,
4aef7643 17490 Elf_Internal_Sym *sym)
5bd4f169 17491{
65f38f15 17492 struct ppc_link_hash_table *htab;
8387904d 17493 struct plt_entry *ent;
5bd4f169 17494
65f38f15 17495 htab = ppc_hash_table (info);
4dfe6ac6 17496 if (htab == NULL)
0a1b45a2 17497 return false;
5bd4f169 17498
49c09209
AM
17499 if (!htab->opd_abi && !h->def_regular)
17500 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17501 if (ent->plt.offset != (bfd_vma) -1)
17502 {
17503 /* Mark the symbol as undefined, rather than as
17504 defined in glink. Leave the value if there were
17505 any relocations where pointer equality matters
17506 (this is a clue for the dynamic linker, to make
17507 function pointer comparisons work between an
17508 application and shared library), otherwise set it
17509 to zero. */
17510 sym->st_shndx = SHN_UNDEF;
17511 if (!h->pointer_equality_needed)
17512 sym->st_value = 0;
17513 else if (!h->ref_regular_nonweak)
17514 {
17515 /* This breaks function pointer comparisons, but
17516 that is better than breaking tests for a NULL
17517 function pointer. */
17518 sym->st_value = 0;
17519 }
17520 break;
17521 }
5bd4f169 17522
1bdd8fac
AM
17523 if (h->needs_copy
17524 && (h->root.type == bfd_link_hash_defined
17525 || h->root.type == bfd_link_hash_defweak)
17526 && (h->root.u.def.section == htab->elf.sdynbss
17527 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 17528 {
65f38f15 17529 /* This symbol needs a copy reloc. Set it up. */
49c09209 17530 Elf_Internal_Rela rela;
5474d94f 17531 asection *srel;
49c09209 17532 bfd_byte *loc;
5bd4f169 17533
1bdd8fac 17534 if (h->dynindx == -1)
65f38f15 17535 abort ();
5bd4f169 17536
ed7007c1 17537 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
17538 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17539 rela.r_addend = 0;
afbf7e8e 17540 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
17541 srel = htab->elf.sreldynrelro;
17542 else
17543 srel = htab->elf.srelbss;
17544 loc = srel->contents;
17545 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 17546 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
17547 }
17548
0a1b45a2 17549 return true;
5bd4f169
AM
17550}
17551
65f38f15
AM
17552/* Used to decide how to sort relocs in an optimal manner for the
17553 dynamic linker, before writing them out. */
17554
17555static enum elf_reloc_type_class
7e612e98
AM
17556ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17557 const asection *rel_sec,
17558 const Elf_Internal_Rela *rela)
65f38f15 17559{
04c9666a 17560 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
17561 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17562
33e44f2e 17563 if (rel_sec == htab->elf.irelplt)
7e612e98 17564 return reloc_class_ifunc;
a33d1f77 17565
4ce794b7 17566 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 17567 switch (r_type)
65f38f15
AM
17568 {
17569 case R_PPC64_RELATIVE:
17570 return reloc_class_relative;
17571 case R_PPC64_JMP_SLOT:
17572 return reloc_class_plt;
17573 case R_PPC64_COPY:
17574 return reloc_class_copy;
17575 default:
17576 return reloc_class_normal;
17577 }
17578}
17579
5bd4f169
AM
17580/* Finish up the dynamic sections. */
17581
0a1b45a2 17582static bool
4ce794b7
AM
17583ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17584 struct bfd_link_info *info)
5bd4f169 17585{
65f38f15
AM
17586 struct ppc_link_hash_table *htab;
17587 bfd *dynobj;
5bd4f169 17588 asection *sdyn;
5bd4f169 17589
65f38f15 17590 htab = ppc_hash_table (info);
4dfe6ac6 17591 if (htab == NULL)
0a1b45a2 17592 return false;
4dfe6ac6 17593
65f38f15 17594 dynobj = htab->elf.dynobj;
3d4d4302 17595 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 17596
65f38f15 17597 if (htab->elf.dynamic_sections_created)
5bd4f169 17598 {
5bd4f169
AM
17599 Elf64_External_Dyn *dyncon, *dynconend;
17600
33e44f2e 17601 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 17602 abort ();
5bd4f169
AM
17603
17604 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 17605 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
17606 for (; dyncon < dynconend; dyncon++)
17607 {
17608 Elf_Internal_Dyn dyn;
19397422 17609 asection *s;
5bd4f169
AM
17610
17611 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17612
17613 switch (dyn.d_tag)
17614 {
65f38f15
AM
17615 default:
17616 continue;
5bd4f169 17617
5d1634d7 17618 case DT_PPC64_GLINK:
4ce794b7 17619 s = htab->glink;
6348e046 17620 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
17621 /* We stupidly defined DT_PPC64_GLINK to be the start
17622 of glink rather than the first entry point, which is
17623 what ld.so needs, and now have a bigger stub to
17624 support automatic multiple TOCs. */
9e390558 17625 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
17626 break;
17627
19397422
AM
17628 case DT_PPC64_OPD:
17629 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17630 if (s == NULL)
17631 continue;
17632 dyn.d_un.d_ptr = s->vma;
19397422
AM
17633 break;
17634
e8910a83 17635 case DT_PPC64_OPT:
5663e321
AM
17636 if ((htab->do_multi_toc && htab->multi_toc_needed)
17637 || htab->notoc_plt)
e8910a83 17638 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
17639 if (htab->has_plt_localentry0)
17640 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
17641 break;
17642
19397422
AM
17643 case DT_PPC64_OPDSZ:
17644 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17645 if (s == NULL)
17646 continue;
eea6121a 17647 dyn.d_un.d_val = s->size;
19397422
AM
17648 break;
17649
65f38f15 17650 case DT_PLTGOT:
33e44f2e 17651 s = htab->elf.splt;
6348e046 17652 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
17653 break;
17654
17655 case DT_JMPREL:
33e44f2e 17656 s = htab->elf.srelplt;
6348e046 17657 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 17658 break;
5bd4f169 17659
65f38f15 17660 case DT_PLTRELSZ:
33e44f2e 17661 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 17662 break;
82e66161
AM
17663
17664 case DT_TEXTREL:
cebd6b8a 17665 if (htab->elf.ifunc_resolvers)
82e66161
AM
17666 info->callbacks->einfo
17667 (_("%P: warning: text relocations and GNU indirect "
17668 "functions may result in a segfault at runtime\n"));
17669 continue;
5bd4f169 17670 }
5bd4f169 17671
65f38f15 17672 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 17673 }
5bd4f169
AM
17674 }
17675
6528b6eb
AM
17676 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17677 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17678 {
17679 /* Fill in the first entry in the global offset table.
17680 We use it to hold the link-time TOCbase. */
17681 bfd_put_64 (output_bfd,
60ee0d4a 17682 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 17683 htab->elf.sgot->contents);
5d1634d7
AM
17684
17685 /* Set .got entry size. */
2cdcc330
AM
17686 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17687 = 8;
5d1634d7
AM
17688 }
17689
6528b6eb
AM
17690 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17691 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17692 {
17693 /* Set .plt entry size. */
33e44f2e 17694 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 17695 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
17696 }
17697
84f5d08e
AM
17698 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17699 brlt ourselves if emitrelocations. */
17700 if (htab->brlt != NULL
17701 && htab->brlt->reloc_count != 0
17702 && !_bfd_elf_link_output_relocs (output_bfd,
17703 htab->brlt,
d4730f92 17704 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
17705 elf_section_data (htab->brlt)->relocs,
17706 NULL))
0a1b45a2 17707 return false;
84f5d08e 17708
176a0d42
AM
17709 if (htab->glink != NULL
17710 && htab->glink->reloc_count != 0
17711 && !_bfd_elf_link_output_relocs (output_bfd,
17712 htab->glink,
d4730f92 17713 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
17714 elf_section_data (htab->glink)->relocs,
17715 NULL))
0a1b45a2 17716 return false;
176a0d42 17717
da44f4e5 17718
df136d64
AM
17719 if (htab->glink_eh_frame != NULL
17720 && htab->glink_eh_frame->size != 0
17721 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17722 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17723 htab->glink_eh_frame,
17724 htab->glink_eh_frame->contents))
0a1b45a2 17725 return false;
58d180e8 17726
e717da7e 17727 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
17728 since we didn't add them to DYNOBJ. We know dynobj is the first
17729 bfd. */
c72f2fb2 17730 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
17731 {
17732 asection *s;
7b53ace3 17733
0c8d6e5c 17734 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
17735 continue;
17736
e717da7e
AM
17737 s = ppc64_elf_tdata (dynobj)->got;
17738 if (s != NULL
eea6121a 17739 && s->size != 0
e717da7e
AM
17740 && s->output_section != bfd_abs_section_ptr
17741 && !bfd_set_section_contents (output_bfd, s->output_section,
17742 s->contents, s->output_offset,
eea6121a 17743 s->size))
0a1b45a2 17744 return false;
e717da7e
AM
17745 s = ppc64_elf_tdata (dynobj)->relgot;
17746 if (s != NULL
eea6121a 17747 && s->size != 0
e717da7e
AM
17748 && s->output_section != bfd_abs_section_ptr
17749 && !bfd_set_section_contents (output_bfd, s->output_section,
17750 s->contents, s->output_offset,
eea6121a 17751 s->size))
0a1b45a2 17752 return false;
e717da7e 17753 }
f6c52c13 17754
0a1b45a2 17755 return true;
5bd4f169
AM
17756}
17757
5bd4f169 17758#include "elf64-target.h"
7b8e7dad
AM
17759
17760/* FreeBSD support */
17761
17762#undef TARGET_LITTLE_SYM
6afcdeb3 17763#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 17764#undef TARGET_LITTLE_NAME
6afcdeb3 17765#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
17766
17767#undef TARGET_BIG_SYM
6d00b590 17768#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
17769#undef TARGET_BIG_NAME
17770#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17771
17772#undef ELF_OSABI
17773#define ELF_OSABI ELFOSABI_FREEBSD
17774
17775#undef elf64_bed
17776#define elf64_bed elf64_powerpc_fbsd_bed
17777
17778#include "elf64-target.h"
This page took 3.21413 seconds and 4 git commands to generate.