]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
PowerPC: Don't generate unused section symbols
[thirdparty/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
AM
28/* Don't generate unused section symbols. */
29#define TARGET_KEEP_UNUSED_SECTION_SYMBOLS FALSE
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
aef36ac1 65 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
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, \
311 #type, FALSE, 0, mask, pc_relative)
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. */
46807bf4
AM
318 HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
319 bfd_elf_generic_reloc),
5bd4f169
AM
320
321 /* A standard 32 bit relocation. */
46807bf4
AM
322 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
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. */
46807bf4
AM
327 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
328 bfd_elf_generic_reloc),
5bd4f169
AM
329
330 /* A standard 16 bit relocation. */
46807bf4
AM
331 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
332 bfd_elf_generic_reloc),
5bd4f169
AM
333
334 /* A 16 bit relocation without overflow. */
46807bf4
AM
335 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
336 bfd_elf_generic_reloc),
5bd4f169
AM
337
338 /* Bits 16-31 of an address. */
46807bf4
AM
339 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
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. */
46807bf4
AM
344 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
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. */
46807bf4
AM
349 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
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. */
46807bf4
AM
355 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
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. */
46807bf4
AM
361 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
362 ppc64_elf_brtaken_reloc),
5bd4f169
AM
363
364 /* A relative 26 bit branch; the lower two bits must be zero. */
46807bf4
AM
365 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
366 ppc64_elf_branch_reloc),
5bd4f169 367
05d0e962 368 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
46807bf4
AM
369 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
370 ppc64_elf_branch_reloc),
05d0e962 371
5bd4f169 372 /* A relative 16 bit branch; the lower two bits must be zero. */
46807bf4
AM
373 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
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. */
46807bf4
AM
379 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
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. */
46807bf4
AM
385 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
386 ppc64_elf_brtaken_reloc),
5bd4f169
AM
387
388 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
389 symbol. */
46807bf4
AM
390 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
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. */
46807bf4
AM
395 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
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. */
46807bf4
AM
400 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
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. */
46807bf4
AM
405 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
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. */
46807bf4
AM
413 HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
414 ppc64_elf_unhandled_reloc),
5bd4f169
AM
415
416 /* Like R_PPC64_ADDR64, but used when setting global offset table
417 entries. */
46807bf4
AM
418 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
419 ppc64_elf_unhandled_reloc),
5bd4f169
AM
420
421 /* Created by the link editor. Marks a procedure linkage table
422 entry for a symbol. */
46807bf4
AM
423 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
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. */
46807bf4
AM
429 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
430 bfd_elf_generic_reloc),
5bd4f169
AM
431
432 /* Like R_PPC64_ADDR32, but may be unaligned. */
46807bf4
AM
433 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
434 bfd_elf_generic_reloc),
5bd4f169
AM
435
436 /* Like R_PPC64_ADDR16, but may be unaligned. */
46807bf4
AM
437 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
438 bfd_elf_generic_reloc),
5bd4f169
AM
439
440 /* 32-bit PC relative. */
46807bf4
AM
441 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
442 bfd_elf_generic_reloc),
5bd4f169 443
10ed1bba 444 /* 32-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
445 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
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. */
46807bf4
AM
450 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
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. */
46807bf4
AM
455 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
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. */
46807bf4
AM
460 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
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. */
46807bf4
AM
465 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
466 ppc64_elf_unhandled_reloc),
5bd4f169 467
c061c2d8 468 /* 16-bit section relative relocation. */
46807bf4
AM
469 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
470 ppc64_elf_sectoff_reloc),
5bd4f169 471
c061c2d8 472 /* Like R_PPC64_SECTOFF, but no overflow warning. */
46807bf4
AM
473 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
474 ppc64_elf_sectoff_reloc),
5bd4f169
AM
475
476 /* 16-bit upper half section relative relocation. */
46807bf4
AM
477 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
478 ppc64_elf_sectoff_reloc),
5bd4f169
AM
479
480 /* 16-bit upper half adjusted section relative relocation. */
46807bf4
AM
481 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
482 ppc64_elf_sectoff_ha_reloc),
5bd4f169 483
04c9666a 484 /* Like R_PPC64_REL24 without touching the two least significant bits. */
46807bf4
AM
485 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
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. */
46807bf4
AM
491 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
492 bfd_elf_generic_reloc),
5bd4f169
AM
493
494 /* The bits 32-47 of an address. */
46807bf4
AM
495 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
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. */
46807bf4
AM
500 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
501 ppc64_elf_ha_reloc),
5bd4f169
AM
502
503 /* The bits 48-63 of an address. */
46807bf4
AM
504 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
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. */
46807bf4
AM
509 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
510 ppc64_elf_ha_reloc),
5bd4f169
AM
511
512 /* Like ADDR64, but may be unaligned. */
46807bf4
AM
513 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
514 bfd_elf_generic_reloc),
5bd4f169
AM
515
516 /* 64-bit relative relocation. */
46807bf4
AM
517 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
518 bfd_elf_generic_reloc),
5bd4f169 519
cedb70c5 520 /* 64-bit relocation to the symbol's procedure linkage table. */
46807bf4
AM
521 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
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. */
46807bf4
AM
527 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
528 ppc64_elf_unhandled_reloc),
5bd4f169
AM
529
530 /* 16 bit TOC-relative relocation. */
5bd4f169 531 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
46807bf4
AM
532 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
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.) */
46807bf4
AM
537 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
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.) */
46807bf4
AM
542 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
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.) */
46807bf4
AM
549 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
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. */
46807bf4
AM
554 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
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. */
46807bf4
AM
566 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
567 ppc64_elf_unhandled_reloc),
411e1bfb
AM
568
569 /* Like R_PPC64_PLTGOT16, but without overflow. */
570 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
46807bf4
AM
571 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
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. */
46807bf4
AM
576 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
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. */
46807bf4
AM
583 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
584 ppc64_elf_unhandled_reloc),
411e1bfb
AM
585
586 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
46807bf4
AM
587 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
588 bfd_elf_generic_reloc),
411e1bfb
AM
589
590 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
46807bf4
AM
591 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
592 bfd_elf_generic_reloc),
411e1bfb
AM
593
594 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
46807bf4
AM
595 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
596 ppc64_elf_unhandled_reloc),
411e1bfb
AM
597
598 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
46807bf4
AM
599 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
600 ppc64_elf_unhandled_reloc),
411e1bfb
AM
601
602 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
46807bf4
AM
603 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
604 ppc64_elf_unhandled_reloc),
411e1bfb
AM
605
606 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
46807bf4
AM
607 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
608 ppc64_elf_sectoff_reloc),
411e1bfb
AM
609
610 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
46807bf4
AM
611 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
612 ppc64_elf_sectoff_reloc),
411e1bfb
AM
613
614 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
46807bf4
AM
615 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
616 ppc64_elf_toc_reloc),
411e1bfb
AM
617
618 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
46807bf4
AM
619 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
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. */
46807bf4
AM
624 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
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. */
46807bf4
AM
629 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
630 ppc64_elf_unhandled_reloc),
411e1bfb 631
727fc41e 632 /* Marker relocs for TLS. */
46807bf4
AM
633 HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
634 bfd_elf_generic_reloc),
635
636 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
637 bfd_elf_generic_reloc),
638
639 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
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. */
46807bf4
AM
644 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
645 bfd_elf_generic_reloc),
3b421ab3 646
23cedd1d 647 /* Marker relocs on inline plt call instructions. */
46807bf4
AM
648 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
649 bfd_elf_generic_reloc),
650
651 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
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. */
46807bf4
AM
656 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
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. */
46807bf4
AM
662 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
663 ppc64_elf_unhandled_reloc),
411e1bfb
AM
664
665 /* A 16 bit dtprel reloc. */
46807bf4
AM
666 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
667 ppc64_elf_unhandled_reloc),
411e1bfb
AM
668
669 /* Like DTPREL16, but no overflow. */
46807bf4
AM
670 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
671 ppc64_elf_unhandled_reloc),
411e1bfb
AM
672
673 /* Like DTPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
674 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
675 ppc64_elf_unhandled_reloc),
411e1bfb
AM
676
677 /* Like DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
678 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
679 ppc64_elf_unhandled_reloc),
411e1bfb
AM
680
681 /* Like DTPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
682 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
683 ppc64_elf_unhandled_reloc),
411e1bfb
AM
684
685 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
686 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
687 ppc64_elf_unhandled_reloc),
411e1bfb
AM
688
689 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
690 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
691 ppc64_elf_unhandled_reloc),
411e1bfb
AM
692
693 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
694 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
695 ppc64_elf_unhandled_reloc),
411e1bfb
AM
696
697 /* Like DTPREL16, but for insns with a DS field. */
46807bf4
AM
698 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
699 ppc64_elf_unhandled_reloc),
411e1bfb
AM
700
701 /* Like DTPREL16_DS, but no overflow. */
46807bf4
AM
702 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
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). */
46807bf4
AM
707 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
708 ppc64_elf_unhandled_reloc),
411e1bfb
AM
709
710 /* A 16 bit tprel reloc. */
46807bf4
AM
711 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
712 ppc64_elf_unhandled_reloc),
411e1bfb
AM
713
714 /* Like TPREL16, but no overflow. */
46807bf4
AM
715 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
716 ppc64_elf_unhandled_reloc),
411e1bfb
AM
717
718 /* Like TPREL16_LO, but next higher group of 16 bits. */
46807bf4
AM
719 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
720 ppc64_elf_unhandled_reloc),
411e1bfb
AM
721
722 /* Like TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
723 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
724 ppc64_elf_unhandled_reloc),
411e1bfb
AM
725
726 /* Like TPREL16_HI, but next higher group of 16 bits. */
46807bf4
AM
727 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
728 ppc64_elf_unhandled_reloc),
411e1bfb
AM
729
730 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
46807bf4
AM
731 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
732 ppc64_elf_unhandled_reloc),
411e1bfb
AM
733
734 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
46807bf4
AM
735 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
736 ppc64_elf_unhandled_reloc),
411e1bfb
AM
737
738 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
46807bf4
AM
739 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
740 ppc64_elf_unhandled_reloc),
411e1bfb
AM
741
742 /* Like TPREL16, but for insns with a DS field. */
46807bf4
AM
743 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
744 ppc64_elf_unhandled_reloc),
411e1bfb
AM
745
746 /* Like TPREL16_DS, but no overflow. */
46807bf4
AM
747 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
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). */
46807bf4
AM
753 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
754 ppc64_elf_unhandled_reloc),
5bd4f169 755
411e1bfb 756 /* Like GOT_TLSGD16, but no overflow. */
46807bf4
AM
757 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
758 ppc64_elf_unhandled_reloc),
5bd4f169 759
411e1bfb 760 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
46807bf4
AM
761 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
762 ppc64_elf_unhandled_reloc),
5bd4f169 763
411e1bfb 764 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
46807bf4
AM
765 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
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). */
46807bf4
AM
771 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
772 ppc64_elf_unhandled_reloc),
5bd4f169 773
411e1bfb 774 /* Like GOT_TLSLD16, but no overflow. */
46807bf4
AM
775 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
776 ppc64_elf_unhandled_reloc),
5bd4f169 777
411e1bfb 778 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
46807bf4
AM
779 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
780 ppc64_elf_unhandled_reloc),
5bd4f169 781
411e1bfb 782 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
46807bf4
AM
783 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
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). */
46807bf4
AM
788 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
789 ppc64_elf_unhandled_reloc),
5bd4f169 790
411e1bfb 791 /* Like GOT_DTPREL16_DS, but no overflow. */
46807bf4
AM
792 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
793 ppc64_elf_unhandled_reloc),
5bd4f169 794
411e1bfb 795 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
796 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
797 ppc64_elf_unhandled_reloc),
5bd4f169 798
411e1bfb 799 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
800 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
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). */
46807bf4
AM
805 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
806 ppc64_elf_unhandled_reloc),
5bd4f169 807
411e1bfb 808 /* Like GOT_TPREL16_DS, but no overflow. */
46807bf4
AM
809 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
810 ppc64_elf_unhandled_reloc),
5bd4f169 811
411e1bfb 812 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
46807bf4
AM
813 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
814 ppc64_elf_unhandled_reloc),
5bd4f169 815
411e1bfb 816 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
46807bf4
AM
817 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
818 ppc64_elf_unhandled_reloc),
819
820 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
821 ppc64_elf_unhandled_reloc),
822
823 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
824 bfd_elf_generic_reloc),
e054468f 825
25f23106 826 /* A 16 bit relative relocation. */
46807bf4
AM
827 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
828 bfd_elf_generic_reloc),
25f23106
AM
829
830 /* A 16 bit relative relocation without overflow. */
46807bf4
AM
831 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
832 bfd_elf_generic_reloc),
25f23106
AM
833
834 /* The high order 16 bits of a relative address. */
46807bf4
AM
835 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
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. */
46807bf4
AM
840 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
841 ppc64_elf_ha_reloc),
25f23106 842
4a969973
AM
843 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
844 bfd_elf_generic_reloc),
845
846 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
847 ppc64_elf_ha_reloc),
848
849 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
850 bfd_elf_generic_reloc),
851
852 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
853 ppc64_elf_ha_reloc),
854
855 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
856 bfd_elf_generic_reloc),
857
858 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
859 ppc64_elf_ha_reloc),
860
a680de9a 861 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
46807bf4
AM
862 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
863 ppc64_elf_ha_reloc),
a680de9a 864
7ba71655 865 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
46807bf4
AM
866 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
867 ppc64_elf_ha_reloc),
7ba71655 868
f9c6b907 869 /* Like R_PPC64_ADDR16_HI, but no overflow. */
46807bf4
AM
870 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
871 bfd_elf_generic_reloc),
f9c6b907
AM
872
873 /* Like R_PPC64_ADDR16_HA, but no overflow. */
46807bf4
AM
874 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
875 ppc64_elf_ha_reloc),
f9c6b907
AM
876
877 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
46807bf4
AM
878 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
879 ppc64_elf_unhandled_reloc),
f9c6b907
AM
880
881 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
46807bf4
AM
882 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
883 ppc64_elf_unhandled_reloc),
f9c6b907
AM
884
885 /* Like R_PPC64_TPREL16_HI, but no overflow. */
46807bf4
AM
886 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
887 ppc64_elf_unhandled_reloc),
f9c6b907
AM
888
889 /* Like R_PPC64_TPREL16_HA, but no overflow. */
46807bf4
AM
890 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
891 ppc64_elf_unhandled_reloc),
f9c6b907 892
006589cf 893 /* Marker reloc on ELFv2 large-model function entry. */
46807bf4
AM
894 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
895 bfd_elf_generic_reloc),
006589cf 896
45965137 897 /* Like ADDR64, but use local entry point of function. */
46807bf4
AM
898 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
899 bfd_elf_generic_reloc),
45965137 900
5663e321
AM
901 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
902 bfd_elf_generic_reloc),
903
904 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
905 bfd_elf_generic_reloc),
906
907 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
908 bfd_elf_generic_reloc),
909
910 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
911 ppc64_elf_prefix_reloc),
912
913 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
914 ppc64_elf_prefix_reloc),
915
916 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
917 ppc64_elf_prefix_reloc),
918
919 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
920 ppc64_elf_prefix_reloc),
921
922 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
923 ppc64_elf_prefix_reloc),
924
925 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
926 ppc64_elf_unhandled_reloc),
927
928 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
929 ppc64_elf_unhandled_reloc),
930
931 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
932 ppc64_elf_unhandled_reloc),
933
c213164a
AM
934 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
935 ppc64_elf_unhandled_reloc),
936
937 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
938 ppc64_elf_unhandled_reloc),
939
87c69f97 940 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
941 ppc64_elf_unhandled_reloc),
942
87c69f97 943 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
944 ppc64_elf_unhandled_reloc),
945
87c69f97 946 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
947 ppc64_elf_unhandled_reloc),
948
87c69f97 949 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
c213164a
AM
950 ppc64_elf_unhandled_reloc),
951
5663e321
AM
952 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
953 bfd_elf_generic_reloc),
954
955 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
956 ppc64_elf_ha_reloc),
957
958 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
959 bfd_elf_generic_reloc),
960
961 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
962 ppc64_elf_ha_reloc),
963
964 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
965 bfd_elf_generic_reloc),
966
967 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
968 ppc64_elf_ha_reloc),
969
970 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
971 bfd_elf_generic_reloc),
972
973 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
974 ppc64_elf_ha_reloc),
975
976 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
977 ppc64_elf_prefix_reloc),
978
979 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
980 ppc64_elf_prefix_reloc),
981
5bd4f169 982 /* GNU extension to record C++ vtable hierarchy. */
46807bf4
AM
983 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
984 NULL),
5bd4f169
AM
985
986 /* GNU extension to record C++ vtable member usage. */
46807bf4
AM
987 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
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
f3185997 1359static bfd_boolean
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
NC
1375 bfd_set_error (bfd_error_bad_value);
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);
1385 return FALSE;
1386 }
2cdcc330 1387
f3185997 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,
aef36ac1 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
AM
1500 /* Assume 'at' branch hints. */
1501 bfd_boolean 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
1829static bfd_boolean
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
b34976b6 1839static bfd_boolean
4ce794b7 1840ppc64_elf_object_p (bfd *abfd)
feee612b 1841{
14b57c7c
AM
1842 if (!abfd->arch_info->the_default)
1843 return TRUE;
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
1861static bfd_boolean
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)
1867 return FALSE;
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
1884static bfd_boolean
1885ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1886{
1887 if (note->descsz != 136)
1888 return FALSE;
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
1897 return TRUE;
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
AM
2022
2023static bfd_boolean
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)
2033 return FALSE;
2034 sec->used_by_bfd = sdata;
2035 }
f0abc2a1
AM
2036
2037 return _bfd_elf_new_section_hook (abfd, sec);
2038}
4025353c 2039
bf577467
AM
2040static bfd_boolean
2041ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2042{
2043 const char *name = hdr->bfd_section->name;
2044
2045 if (strncmp (name, ".sbss", 5) == 0
2046 || strncmp (name, ".sdata", 6) == 0)
2047 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2048
2049 return TRUE;
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. */
90e3cdf2 2063static bfd_boolean 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
468392fb
AM
2199static bfd_boolean
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;
90e3cdf2 2223 bfd_boolean 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 {
699733f6
AM
2339 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
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
7356fed5
AM
2352 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
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 {
468392fb 2438 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
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;
2cdcc330 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
3532static bfd_boolean
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))
b32547cd
AM
3549 return FALSE;
3550 }
3551
3552 if (bfd_link_relocatable (info))
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))
bfeb4a28
AM
3560 return FALSE;
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))
9e390558
AM
3568 return FALSE;
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))
bfeb4a28
AM
3579 return FALSE;
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))
bfeb4a28
AM
3586 return FALSE;
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))
bfeb4a28
AM
3594 return FALSE;
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))
bfeb4a28
AM
3603 return FALSE;
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))
2d7ad24e
AM
3611 return FALSE;
3612
0e1862bb 3613 if (!bfd_link_pic (info))
bfeb4a28
AM
3614 return TRUE;
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))
bfeb4a28
AM
3622 return FALSE;
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))
2d7ad24e
AM
3628 return FALSE;
3629
bfeb4a28
AM
3630 return TRUE;
3631}
3632
e717da7e
AM
3633/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3634
bfeb4a28 3635bfd_boolean
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 *
3709select_alt_stub (struct ppc_stub_hash_entry *entry, bfd_boolean notoc)
3710{
3711 bfd_boolean have_notoc;
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,
b34976b6 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 {
3776 bfd_boolean notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
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,
b34976b6 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
b34976b6 3840static bfd_boolean
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))
0ffa91dd 3848 return FALSE;
4dfe6ac6
NC
3849 if (htab == NULL)
3850 return FALSE;
0ffa91dd 3851
33e44f2e
AM
3852 if (!htab->elf.sgot
3853 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
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))
e717da7e 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))
b34976b6 3868 return FALSE;
e717da7e
AM
3869
3870 ppc64_elf_tdata (abfd)->got = got;
3871 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 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
AM
4059 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
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,
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
AM
4107
4108static bfd_boolean
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,
4131 FALSE) != (bfd_vma) -1
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
AM
4155 bfd_set_error (bfd_error_bad_value);
4156 return FALSE;
4157 }
4158 }
4159
555cd476
AM
4160 return TRUE;
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,
6911b7dc
AM
4168 bfd_boolean definition,
4169 bfd_boolean dynamic)
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
4179static bfd_boolean
4180ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
8b5f1ed8 4181 const Elf_Internal_Sym *isym,
8c5b4e52
AM
4182 asection **psec ATTRIBUTE_UNUSED,
4183 bfd_boolean newdef ATTRIBUTE_UNUSED,
4184 bfd_boolean olddef ATTRIBUTE_UNUSED,
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;
8c5b4e52
AM
4191 return TRUE;
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
AM
4198
4199static struct elf_link_hash_entry *
4200ppc64_elf_archive_symbol_lookup (bfd *abfd,
4201 struct bfd_link_info *info,
4202 const char *name)
4203{
4204 struct elf_link_hash_entry *h;
4205 char *dot_name;
4206 size_t len;
4207
4208 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4209 if (h != NULL
4210 /* Don't return this sym if it is a fake function descriptor
4211 created by add_symbol_adjust. */
ed7007c1 4212 && !ppc_elf_hash_entry (h)->fake)
8387904d
AM
4213 return h;
4214
4215 if (name[0] == '.')
4216 return h;
4217
4218 len = strlen (name);
4219 dot_name = bfd_alloc (abfd, len + 2);
4220 if (dot_name == NULL)
e99955cd 4221 return (struct elf_link_hash_entry *) -1;
8387904d
AM
4222 dot_name[0] = '.';
4223 memcpy (dot_name + 1, name, len + 1);
4224 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4225 bfd_release (abfd, dot_name);
a804e476
AM
4226 if (h != NULL)
4227 return h;
4228
4229 if (strcmp (name, "__tls_get_addr_opt") == 0)
4230 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
8387904d
AM
4231 return h;
4232}
4233
4234/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4235 new ABI object defines "bar". Well, at least, undefined dot symbols
4236 are made weak. This stops later archive searches from including an
4237 object if we already have a function descriptor definition. It also
35b0ce59
AM
4238 prevents the linker complaining about undefined symbols.
4239 We also check and correct mismatched symbol visibility here. The
4240 most restrictive visibility of the function descriptor and the
4241 function entry symbol is used. */
8387904d
AM
4242
4243static bfd_boolean
b3fac117 4244add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4245{
8387904d
AM
4246 struct ppc_link_hash_table *htab;
4247 struct ppc_link_hash_entry *fdh;
4248
b3fac117
AM
4249 if (eh->elf.root.type == bfd_link_hash_warning)
4250 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4251
8400d40d
AM
4252 if (eh->elf.root.type == bfd_link_hash_indirect)
4253 return TRUE;
4254
b3fac117
AM
4255 if (eh->elf.root.root.string[0] != '.')
4256 abort ();
8387904d 4257
b3fac117 4258 htab = ppc_hash_table (info);
4dfe6ac6
NC
4259 if (htab == NULL)
4260 return FALSE;
4261
b31867b6 4262 fdh = lookup_fdh (eh, htab);
8c5b4e52
AM
4263 if (fdh == NULL
4264 && !bfd_link_relocatable (info)
4265 && (eh->elf.root.type == bfd_link_hash_undefined
4266 || eh->elf.root.type == bfd_link_hash_undefweak)
4267 && eh->elf.ref_regular)
4268 {
4269 /* Make an undefined function descriptor sym, in order to
4270 pull in an --as-needed shared lib. Archives are handled
4271 elsewhere. */
4272 fdh = make_fdh (info, eh);
4273 if (fdh == NULL)
4274 return FALSE;
bb700d78 4275 }
8c5b4e52
AM
4276
4277 if (fdh != NULL)
8387904d 4278 {
35b0ce59
AM
4279 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4280 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
8c5b4e52
AM
4281
4282 /* Make both descriptor and entry symbol have the most
4283 constraining visibility of either symbol. */
35b0ce59
AM
4284 if (entry_vis < descr_vis)
4285 fdh->elf.other += entry_vis - descr_vis;
4286 else if (entry_vis > descr_vis)
4287 eh->elf.other += descr_vis - entry_vis;
4288
8c5b4e52
AM
4289 /* Propagate reference flags from entry symbol to function
4290 descriptor symbol. */
bc4e12de 4291 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4070765b 4292 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
8c5b4e52
AM
4293 fdh->elf.ref_regular |= eh->elf.ref_regular;
4294 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4295
4296 if (!fdh->elf.forced_local
4297 && fdh->elf.dynindx == -1
4298 && fdh->elf.versioned != versioned_hidden
4299 && (bfd_link_dll (info)
4300 || fdh->elf.def_dynamic
4301 || fdh->elf.ref_dynamic)
4302 && (eh->elf.ref_regular
4303 || eh->elf.def_regular))
4304 {
2cdcc330 4305 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
8c5b4e52 4306 return FALSE;
35b0ce59 4307 }
8387904d 4308 }
99877b66 4309
8387904d
AM
4310 return TRUE;
4311}
4312
f6c7c3e8
AM
4313/* Set up opd section info and abiversion for IBFD, and process list
4314 of dot-symbols we made in link_hash_newfunc. */
b3fac117 4315
8387904d 4316static bfd_boolean
f6c7c3e8 4317ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
8387904d 4318{
99877b66 4319 struct ppc_link_hash_table *htab;
b3fac117 4320 struct ppc_link_hash_entry **p, *eh;
459609d6 4321 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
433817dd 4322
459609d6 4323 if (opd != NULL && opd->size != 0)
b3fac117 4324 {
b9399fcf
AM
4325 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4326 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4327
459609d6
AM
4328 if (abiversion (ibfd) == 0)
4329 set_abiversion (ibfd, 1);
8a2058b5 4330 else if (abiversion (ibfd) >= 2)
f6c7c3e8 4331 {
695344c0 4332 /* xgettext:c-format */
cf97bcb0
AM
4333 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4334 ibfd, abiversion (ibfd));
459609d6
AM
4335 bfd_set_error (bfd_error_bad_value);
4336 return FALSE;
f6c7c3e8 4337 }
b9399fcf 4338 }
f6c7c3e8 4339
b9399fcf
AM
4340 if (is_ppc64_elf (info->output_bfd))
4341 {
4342 /* For input files without an explicit abiversion in e_flags
4343 we should have flagged any with symbol st_other bits set
4344 as ELFv1 and above flagged those with .opd as ELFv2.
4345 Set the output abiversion if not yet set, and for any input
4346 still ambiguous, take its abiversion from the output.
4347 Differences in ABI are reported later. */
4348 if (abiversion (info->output_bfd) == 0)
4349 set_abiversion (info->output_bfd, abiversion (ibfd));
4350 else if (abiversion (ibfd) == 0)
4351 set_abiversion (ibfd, abiversion (info->output_bfd));
b3fac117
AM
4352 }
4353
459609d6
AM
4354 htab = ppc_hash_table (info);
4355 if (htab == NULL)
b9399fcf 4356 return TRUE;
459609d6 4357
b9399fcf
AM
4358 if (opd != NULL && opd->size != 0
4359 && (ibfd->flags & DYNAMIC) == 0
4360 && (opd->flags & SEC_RELOC) != 0
4361 && opd->reloc_count != 0
4362 && !bfd_is_abs_section (opd->output_section)
4363 && info->gc_sections)
4364 {
4365 /* Garbage collection needs some extra help with .opd sections.
4366 We don't want to necessarily keep everything referenced by
4367 relocs in .opd, as that would keep all functions. Instead,
4368 if we reference an .opd symbol (a function descriptor), we
4369 want to keep the function code symbol's section. This is
4370 easy for global symbols, but for local syms we need to keep
4371 information about the associated function section. */
4372 bfd_size_type amt;
4373 asection **opd_sym_map;
4374 Elf_Internal_Shdr *symtab_hdr;
4375 Elf_Internal_Rela *relocs, *rel_end, *rel;
4376
4377 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4378 opd_sym_map = bfd_zalloc (ibfd, amt);
4379 if (opd_sym_map == NULL)
4380 return FALSE;
4381 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4382 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4383 info->keep_memory);
4384 if (relocs == NULL)
4385 return FALSE;
4386 symtab_hdr = &elf_symtab_hdr (ibfd);
4387 rel_end = relocs + opd->reloc_count - 1;
4388 for (rel = relocs; rel < rel_end; rel++)
4389 {
4390 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4391 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4392
4393 if (r_type == R_PPC64_ADDR64
4394 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4395 && r_symndx < symtab_hdr->sh_info)
4396 {
4397 Elf_Internal_Sym *isym;
4398 asection *s;
4399
f1dfbfdb
L
4400 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4401 r_symndx);
b9399fcf
AM
4402 if (isym == NULL)
4403 {
4404 if (elf_section_data (opd)->relocs != relocs)
4405 free (relocs);
4406 return FALSE;
4407 }
4408
4409 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4410 if (s != NULL && s != opd)
4411 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4412 }
4413 }
4414 if (elf_section_data (opd)->relocs != relocs)
4415 free (relocs);
4416 }
459609d6
AM
4417
4418 p = &htab->dot_syms;
4419 while ((eh = *p) != NULL)
4420 {
4421 *p = NULL;
4422 if (&eh->elf == htab->elf.hgot)
4423 ;
4424 else if (htab->elf.hgot == NULL
4425 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4426 htab->elf.hgot = &eh->elf;
8c5b4e52
AM
4427 else if (abiversion (ibfd) <= 1)
4428 {
4429 htab->need_func_desc_adj = 1;
4430 if (!add_symbol_adjust (eh, info))
4431 return FALSE;
4432 }
459609d6
AM
4433 p = &eh->u.next_dot_sym;
4434 }
b3fac117 4435 return TRUE;
8387904d
AM
4436}
4437
97fed1c9
JJ
4438/* Undo hash table changes when an --as-needed input file is determined
4439 not to be needed. */
4440
4441static bfd_boolean
e5034e59
AM
4442ppc64_elf_notice_as_needed (bfd *ibfd,
4443 struct bfd_link_info *info,
4444 enum notice_asneeded_action act)
97fed1c9 4445{
e5034e59
AM
4446 if (act == notice_not_needed)
4447 {
4448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4449
e5034e59
AM
4450 if (htab == NULL)
4451 return FALSE;
4dfe6ac6 4452
e5034e59
AM
4453 htab->dot_syms = NULL;
4454 }
4455 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4456}
4457
aa374f67
AM
4458/* If --just-symbols against a final linked binary, then assume we need
4459 toc adjusting stubs when calling functions defined there. */
4460
4461static void
4462ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4463{
4464 if ((sec->flags & SEC_CODE) != 0
4465 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4466 && is_ppc64_elf (sec->owner))
4467 {
2c3f079f
AM
4468 if (abiversion (sec->owner) >= 2
4469 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
aa374f67
AM
4470 sec->has_toc_reloc = 1;
4471 }
4472 _bfd_elf_link_just_syms (sec, info);
4473}
4474
e054468f 4475static struct plt_entry **
4ce794b7
AM
4476update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4477 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4478{
4479 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4480 struct plt_entry **local_plt;
f961d9dd 4481 unsigned char *local_got_tls_masks;
411e1bfb
AM
4482
4483 if (local_got_ents == NULL)
4484 {
4485 bfd_size_type size = symtab_hdr->sh_info;
4486
e054468f
AM
4487 size *= (sizeof (*local_got_ents)
4488 + sizeof (*local_plt)
4489 + sizeof (*local_got_tls_masks));
4ce794b7 4490 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4491 if (local_got_ents == NULL)
e054468f 4492 return NULL;
411e1bfb
AM
4493 elf_local_got_ents (abfd) = local_got_ents;
4494 }
4495
37da22e5 4496 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4497 {
4498 struct got_entry *ent;
4499
4500 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4501 if (ent->addend == r_addend
4502 && ent->owner == abfd
4503 && ent->tls_type == tls_type)
411e1bfb
AM
4504 break;
4505 if (ent == NULL)
4506 {
986f0783 4507 size_t amt = sizeof (*ent);
4ce794b7 4508 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4509 if (ent == NULL)
4510 return FALSE;
4511 ent->next = local_got_ents[r_symndx];
4512 ent->addend = r_addend;
e717da7e 4513 ent->owner = abfd;
411e1bfb 4514 ent->tls_type = tls_type;
927be08e 4515 ent->is_indirect = FALSE;
411e1bfb
AM
4516 ent->got.refcount = 0;
4517 local_got_ents[r_symndx] = ent;
4518 }
4519 ent->got.refcount += 1;
4520 }
4521
e054468f 4522 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4523 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
37da22e5 4524 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
e054468f
AM
4525
4526 return local_plt + r_symndx;
65f38f15
AM
4527}
4528
411e1bfb 4529static bfd_boolean
e054468f 4530update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4531{
411e1bfb 4532 struct plt_entry *ent;
1e2f5b6e 4533
e054468f 4534 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4535 if (ent->addend == addend)
4536 break;
4537 if (ent == NULL)
1e2f5b6e 4538 {
986f0783 4539 size_t amt = sizeof (*ent);
4ce794b7 4540 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4541 if (ent == NULL)
4542 return FALSE;
e054468f 4543 ent->next = *plist;
411e1bfb
AM
4544 ent->addend = addend;
4545 ent->plt.refcount = 0;
e054468f 4546 *plist = ent;
1e2f5b6e 4547 }
411e1bfb 4548 ent->plt.refcount += 1;
b34976b6 4549 return TRUE;
1e2f5b6e
AM
4550}
4551
e054468f
AM
4552static bfd_boolean
4553is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4554{
4555 return (r_type == R_PPC64_REL24
05d0e962 4556 || r_type == R_PPC64_REL24_NOTOC
e054468f
AM
4557 || r_type == R_PPC64_REL14
4558 || r_type == R_PPC64_REL14_BRTAKEN
4559 || r_type == R_PPC64_REL14_BRNTAKEN
4560 || r_type == R_PPC64_ADDR24
4561 || r_type == R_PPC64_ADDR14
4562 || r_type == R_PPC64_ADDR14_BRTAKEN
23cedd1d 4563 || r_type == R_PPC64_ADDR14_BRNTAKEN
5663e321
AM
4564 || r_type == R_PPC64_PLTCALL
4565 || r_type == R_PPC64_PLTCALL_NOTOC);
23cedd1d
AM
4566}
4567
4568/* Relocs on inline plt call sequence insns prior to the call. */
4569
4570static bfd_boolean
4571is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4572{
4573 return (r_type == R_PPC64_PLT16_HA
4574 || r_type == R_PPC64_PLT16_HI
4575 || r_type == R_PPC64_PLT16_LO
4576 || r_type == R_PPC64_PLT16_LO_DS
5663e321
AM
4577 || r_type == R_PPC64_PLT_PCREL34
4578 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4579 || r_type == R_PPC64_PLTSEQ
4580 || r_type == R_PPC64_PLTSEQ_NOTOC);
e054468f
AM
4581}
4582
5bd4f169 4583/* Look through the relocs for a section during the first phase, and
65f38f15 4584 calculate needed space in the global offset table, procedure
5d1634d7 4585 linkage table, and dynamic reloc sections. */
5bd4f169 4586
b34976b6 4587static bfd_boolean
4ce794b7
AM
4588ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4589 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4590{
65f38f15 4591 struct ppc_link_hash_table *htab;
5bd4f169 4592 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4593 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4594 const Elf_Internal_Rela *rel;
4595 const Elf_Internal_Rela *rel_end;
5bd4f169 4596 asection *sreloc;
3a71aa26 4597 struct elf_link_hash_entry *tga, *dottga;
b9399fcf 4598 bfd_boolean is_opd;
5bd4f169 4599
0e1862bb 4600 if (bfd_link_relocatable (info))
b34976b6 4601 return TRUE;
5bd4f169 4602
0c8d6e5c 4603 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4604
65f38f15 4605 htab = ppc_hash_table (info);
4dfe6ac6
NC
4606 if (htab == NULL)
4607 return FALSE;
4608
3a71aa26
AM
4609 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4610 FALSE, FALSE, TRUE);
4611 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4612 FALSE, FALSE, TRUE);
0ffa91dd 4613 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4614 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4615 sreloc = NULL;
b9399fcf 4616 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5bd4f169
AM
4617 rel_end = relocs + sec->reloc_count;
4618 for (rel = relocs; rel < rel_end; rel++)
4619 {
4620 unsigned long r_symndx;
4621 struct elf_link_hash_entry *h;
04c9666a 4622 enum elf_ppc64_reloc_type r_type;
727fc41e 4623 int tls_type;
7c8fe5c4 4624 struct _ppc64_elf_section_data *ppc64_sec;
cbf95972 4625 struct plt_entry **ifunc, **plt_list;
5bd4f169
AM
4626
4627 r_symndx = ELF64_R_SYM (rel->r_info);
4628 if (r_symndx < symtab_hdr->sh_info)
4629 h = NULL;
4630 else
973a3492
L
4631 {
4632 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4633 h = elf_follow_link (h);
1c865ab2
AM
4634
4635 if (h == htab->elf.hgot)
4636 sec->has_toc_reloc = 1;
973a3492 4637 }
5bd4f169 4638
5663e321
AM
4639 r_type = ELF64_R_TYPE (rel->r_info);
4640 switch (r_type)
4641 {
04bdff6a
AM
4642 case R_PPC64_D34:
4643 case R_PPC64_D34_LO:
4644 case R_PPC64_D34_HI30:
4645 case R_PPC64_D34_HA30:
4646 case R_PPC64_D28:
c213164a
AM
4647 case R_PPC64_TPREL34:
4648 case R_PPC64_DTPREL34:
04bdff6a 4649 case R_PPC64_PCREL34:
5663e321 4650 case R_PPC64_GOT_PCREL34:
87c69f97
AM
4651 case R_PPC64_GOT_TLSGD_PCREL34:
4652 case R_PPC64_GOT_TLSLD_PCREL34:
4653 case R_PPC64_GOT_TPREL_PCREL34:
4654 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
4655 case R_PPC64_PLT_PCREL34:
4656 case R_PPC64_PLT_PCREL34_NOTOC:
04bdff6a 4657 case R_PPC64_PCREL28:
e10a07b3 4658 htab->has_power10_relocs = 1;
133a1f60
AM
4659 break;
4660 default:
5663e321
AM
4661 break;
4662 }
903b777d
AM
4663
4664 switch (r_type)
4665 {
4666 case R_PPC64_PLT16_HA:
4667 case R_PPC64_GOT_TLSLD16_HA:
4668 case R_PPC64_GOT_TLSGD16_HA:
4669 case R_PPC64_GOT_TPREL16_HA:
4670 case R_PPC64_GOT_DTPREL16_HA:
4671 case R_PPC64_GOT16_HA:
4672 case R_PPC64_TOC16_HA:
4673 case R_PPC64_PLT16_LO:
4674 case R_PPC64_PLT16_LO_DS:
4675 case R_PPC64_GOT_TLSLD16_LO:
4676 case R_PPC64_GOT_TLSGD16_LO:
4677 case R_PPC64_GOT_TPREL16_LO_DS:
4678 case R_PPC64_GOT_DTPREL16_LO_DS:
4679 case R_PPC64_GOT16_LO:
4680 case R_PPC64_GOT16_LO_DS:
4681 case R_PPC64_TOC16_LO:
4682 case R_PPC64_TOC16_LO_DS:
4683 case R_PPC64_GOT_PCREL34:
4684 ppc64_elf_tdata (abfd)->has_optrel = 1;
4685 ppc64_elf_section_data (sec)->has_optrel = 1;
4686 break;
4687 default:
4688 break;
4689 }
4690
f749f26e 4691 ifunc = NULL;
25f23106
AM
4692 if (h != NULL)
4693 {
4694 if (h->type == STT_GNU_IFUNC)
4695 {
4696 h->needs_plt = 1;
4697 ifunc = &h->plt.plist;
4698 }
4699 }
4700 else
4701 {
f1dfbfdb 4702 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
25f23106
AM
4703 abfd, r_symndx);
4704 if (isym == NULL)
4705 return FALSE;
4706
4707 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4708 {
4709 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4710 rel->r_addend,
37da22e5 4711 NON_GOT | PLT_IFUNC);
25f23106
AM
4712 if (ifunc == NULL)
4713 return FALSE;
4714 }
4715 }
727fc41e 4716
f749f26e 4717 tls_type = 0;
a33d1f77 4718 switch (r_type)
5bd4f169 4719 {
727fc41e
AM
4720 case R_PPC64_TLSGD:
4721 case R_PPC64_TLSLD:
4722 /* These special tls relocs tie a call to __tls_get_addr with
4723 its parameter symbol. */
37da22e5 4724 if (h != NULL)
ed7007c1 4725 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
37da22e5
AM
4726 else
4727 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4728 rel->r_addend,
37da22e5
AM
4729 NON_GOT | TLS_TLS | TLS_MARK))
4730 return FALSE;
4731 sec->has_tls_reloc = 1;
727fc41e
AM
4732 break;
4733
411e1bfb
AM
4734 case R_PPC64_GOT_TLSLD16:
4735 case R_PPC64_GOT_TLSLD16_LO:
4736 case R_PPC64_GOT_TLSLD16_HI:
4737 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 4738 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 4739 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4740 goto dogottls;
4741
4742 case R_PPC64_GOT_TLSGD16:
4743 case R_PPC64_GOT_TLSGD16_LO:
4744 case R_PPC64_GOT_TLSGD16_HI:
4745 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 4746 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 4747 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4748 goto dogottls;
4749
4750 case R_PPC64_GOT_TPREL16_DS:
4751 case R_PPC64_GOT_TPREL16_LO_DS:
4752 case R_PPC64_GOT_TPREL16_HI:
4753 case R_PPC64_GOT_TPREL16_HA:
87c69f97 4754 case R_PPC64_GOT_TPREL_PCREL34:
7c8bbca5 4755 if (bfd_link_dll (info))
411e1bfb
AM
4756 info->flags |= DF_STATIC_TLS;
4757 tls_type = TLS_TLS | TLS_TPREL;
4758 goto dogottls;
4759
4760 case R_PPC64_GOT_DTPREL16_DS:
4761 case R_PPC64_GOT_DTPREL16_LO_DS:
4762 case R_PPC64_GOT_DTPREL16_HI:
4763 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 4764 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
4765 tls_type = TLS_TLS | TLS_DTPREL;
4766 dogottls:
4767 sec->has_tls_reloc = 1;
066f4018 4768 goto dogot;
411e1bfb 4769
903b777d
AM
4770 case R_PPC64_GOT16:
4771 case R_PPC64_GOT16_LO:
4772 case R_PPC64_GOT16_HI:
65f38f15 4773 case R_PPC64_GOT16_HA:
903b777d 4774 case R_PPC64_GOT16_DS:
066f4018 4775 case R_PPC64_GOT16_LO_DS:
4a421c53 4776 case R_PPC64_GOT_PCREL34:
066f4018 4777 dogot:
65f38f15 4778 /* This symbol requires a global offset table entry. */
4c52953f 4779 sec->has_toc_reloc = 1;
33c0ec9d
AM
4780 if (r_type == R_PPC64_GOT_TLSLD16
4781 || r_type == R_PPC64_GOT_TLSGD16
4782 || r_type == R_PPC64_GOT_TPREL16_DS
4783 || r_type == R_PPC64_GOT_DTPREL16_DS
4784 || r_type == R_PPC64_GOT16
4785 || r_type == R_PPC64_GOT16_DS)
4786 {
4787 htab->do_multi_toc = 1;
d77c8a4b 4788 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
4789 }
4790
e717da7e
AM
4791 if (ppc64_elf_tdata (abfd)->got == NULL
4792 && !create_got_section (abfd, info))
b34976b6 4793 return FALSE;
5bd4f169
AM
4794
4795 if (h != NULL)
4796 {
411e1bfb
AM
4797 struct ppc_link_hash_entry *eh;
4798 struct got_entry *ent;
65f38f15 4799
ed7007c1 4800 eh = ppc_elf_hash_entry (h);
411e1bfb 4801 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
133a1f60 4802 if (ent->addend == rel->r_addend
e717da7e 4803 && ent->owner == abfd
411e1bfb
AM
4804 && ent->tls_type == tls_type)
4805 break;
4806 if (ent == NULL)
5bd4f169 4807 {
986f0783 4808 size_t amt = sizeof (*ent);
4ce794b7 4809 ent = bfd_alloc (abfd, amt);
411e1bfb 4810 if (ent == NULL)
b34976b6 4811 return FALSE;
411e1bfb 4812 ent->next = eh->elf.got.glist;
133a1f60 4813 ent->addend = rel->r_addend;
e717da7e 4814 ent->owner = abfd;
411e1bfb 4815 ent->tls_type = tls_type;
927be08e 4816 ent->is_indirect = FALSE;
411e1bfb
AM
4817 ent->got.refcount = 0;
4818 eh->elf.got.glist = ent;
5bd4f169 4819 }
411e1bfb 4820 ent->got.refcount += 1;
e7b938ca 4821 eh->tls_mask |= tls_type;
5bd4f169 4822 }
411e1bfb
AM
4823 else
4824 /* This is a global offset table entry for a local symbol. */
4825 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4826 rel->r_addend, tls_type))
411e1bfb 4827 return FALSE;
5bd4f169
AM
4828 break;
4829
5bd4f169 4830 case R_PPC64_PLT16_HA:
65f38f15
AM
4831 case R_PPC64_PLT16_HI:
4832 case R_PPC64_PLT16_LO:
08be3224 4833 case R_PPC64_PLT16_LO_DS:
5663e321
AM
4834 case R_PPC64_PLT_PCREL34:
4835 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
4836 case R_PPC64_PLT32:
4837 case R_PPC64_PLT64:
cbf95972
AM
4838 /* This symbol requires a procedure linkage table entry. */
4839 plt_list = ifunc;
4840 if (h != NULL)
e054468f 4841 {
e054468f
AM
4842 h->needs_plt = 1;
4843 if (h->root.root.string[0] == '.'
4844 && h->root.root.string[1] != '\0')
ed7007c1
AM
4845 ppc_elf_hash_entry (h)->is_func = 1;
4846 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
cbf95972
AM
4847 plt_list = &h->plt.plist;
4848 }
4849 if (plt_list == NULL)
2d7ad24e 4850 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 4851 rel->r_addend,
2d7ad24e 4852 NON_GOT | PLT_KEEP);
133a1f60 4853 if (!update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 4854 return FALSE;
5bd4f169
AM
4855 break;
4856
4857 /* The following relocations don't need to propagate the
4858 relocation if linking a shared object since they are
4859 section relative. */
4860 case R_PPC64_SECTOFF:
4861 case R_PPC64_SECTOFF_LO:
4862 case R_PPC64_SECTOFF_HI:
4863 case R_PPC64_SECTOFF_HA:
4864 case R_PPC64_SECTOFF_DS:
4865 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4866 case R_PPC64_DTPREL16:
4867 case R_PPC64_DTPREL16_LO:
4868 case R_PPC64_DTPREL16_HI:
4869 case R_PPC64_DTPREL16_HA:
4870 case R_PPC64_DTPREL16_DS:
4871 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
4872 case R_PPC64_DTPREL16_HIGH:
4873 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
4874 case R_PPC64_DTPREL16_HIGHER:
4875 case R_PPC64_DTPREL16_HIGHERA:
4876 case R_PPC64_DTPREL16_HIGHEST:
4877 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4878 break;
4879
ad8e1ba5 4880 /* Nor do these. */
25f23106
AM
4881 case R_PPC64_REL16:
4882 case R_PPC64_REL16_LO:
4883 case R_PPC64_REL16_HI:
4884 case R_PPC64_REL16_HA:
4a969973
AM
4885 case R_PPC64_REL16_HIGH:
4886 case R_PPC64_REL16_HIGHA:
4887 case R_PPC64_REL16_HIGHER:
4888 case R_PPC64_REL16_HIGHERA:
4889 case R_PPC64_REL16_HIGHEST:
4890 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
4891 case R_PPC64_REL16_HIGHER34:
4892 case R_PPC64_REL16_HIGHERA34:
4893 case R_PPC64_REL16_HIGHEST34:
4894 case R_PPC64_REL16_HIGHESTA34:
a680de9a 4895 case R_PPC64_REL16DX_HA:
25f23106
AM
4896 break;
4897
45965137
AM
4898 /* Not supported as a dynamic relocation. */
4899 case R_PPC64_ADDR64_LOCAL:
0e1862bb 4900 if (bfd_link_pic (info))
45965137
AM
4901 {
4902 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4903 ppc_howto_init ();
695344c0 4904 /* xgettext:c-format */
174d0a74 4905 info->callbacks->einfo (_("%H: %s reloc unsupported "
cf97bcb0 4906 "in shared libraries and PIEs\n"),
45965137
AM
4907 abfd, sec, rel->r_offset,
4908 ppc64_elf_howto_table[r_type]->name);
4909 bfd_set_error (bfd_error_bad_value);
4910 return FALSE;
4911 }
4912 break;
4913
ad8e1ba5 4914 case R_PPC64_TOC16:
33c0ec9d
AM
4915 case R_PPC64_TOC16_DS:
4916 htab->do_multi_toc = 1;
d77c8a4b 4917 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
1a0670f3 4918 /* Fall through. */
ad8e1ba5
AM
4919 case R_PPC64_TOC16_LO:
4920 case R_PPC64_TOC16_HI:
4921 case R_PPC64_TOC16_HA:
ad8e1ba5 4922 case R_PPC64_TOC16_LO_DS:
4c52953f 4923 sec->has_toc_reloc = 1;
ec73ddcd 4924 if (h != NULL && bfd_link_executable (info))
1bdd8fac
AM
4925 {
4926 /* We may need a copy reloc. */
4927 h->non_got_ref = 1;
4928 /* Strongly prefer a copy reloc over a dynamic reloc.
4929 glibc ld.so as of 2019-08 will error out if one of
4930 these relocations is emitted. */
4931 h->needs_copy = 1;
4932 goto dodyn;
4933 }
ad8e1ba5
AM
4934 break;
4935
006589cf
AM
4936 /* Marker reloc. */
4937 case R_PPC64_ENTRY:
4938 break;
4939
5bd4f169
AM
4940 /* This relocation describes the C++ object vtable hierarchy.
4941 Reconstruct it for later use during GC. */
4942 case R_PPC64_GNU_VTINHERIT:
c152c796 4943 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4944 return FALSE;
5bd4f169
AM
4945 break;
4946
4947 /* This relocation describes which C++ vtable entries are actually
4948 used. Record for later use during GC. */
4949 case R_PPC64_GNU_VTENTRY:
a0ea3a14 4950 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4951 return FALSE;
5bd4f169
AM
4952 break;
4953
721956f4
AM
4954 case R_PPC64_REL14:
4955 case R_PPC64_REL14_BRTAKEN:
4956 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4957 {
4958 asection *dest = NULL;
4959
4960 /* Heuristic: If jumping outside our section, chances are
4961 we are going to need a stub. */
4962 if (h != NULL)
4963 {
4964 /* If the sym is weak it may be overridden later, so
4965 don't assume we know where a weak sym lives. */
4966 if (h->root.type == bfd_link_hash_defined)
4967 dest = h->root.u.def.section;
4968 }
4969 else
87d72d41
AM
4970 {
4971 Elf_Internal_Sym *isym;
4972
f1dfbfdb 4973 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
4974 abfd, r_symndx);
4975 if (isym == NULL)
4976 return FALSE;
4977
4978 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4979 }
4980
220c76dd 4981 if (dest != sec)
7c8fe5c4 4982 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4983 }
3e04d765
AM
4984 goto rel24;
4985
4986 case R_PPC64_PLTCALL:
5663e321 4987 case R_PPC64_PLTCALL_NOTOC:
3e04d765 4988 ppc64_elf_section_data (sec)->has_pltcall = 1;
721956f4
AM
4989 /* Fall through. */
4990
5d1634d7 4991 case R_PPC64_REL24:
05d0e962 4992 case R_PPC64_REL24_NOTOC:
3e04d765 4993 rel24:
cbf95972
AM
4994 plt_list = ifunc;
4995 if (h != NULL)
5d1634d7 4996 {
e054468f
AM
4997 h->needs_plt = 1;
4998 if (h->root.root.string[0] == '.'
4999 && h->root.root.string[1] != '\0')
ed7007c1 5000 ppc_elf_hash_entry (h)->is_func = 1;
cbf95972 5001
3a71aa26 5002 if (h == tga || h == dottga)
cbf95972
AM
5003 {
5004 sec->has_tls_reloc = 1;
5005 if (rel != relocs
5006 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5007 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5008 /* We have a new-style __tls_get_addr call with
5009 a marker reloc. */
5010 ;
5011 else
5012 /* Mark this section as having an old-style call. */
9737e8af 5013 sec->nomark_tls_get_addr = 1;
cbf95972
AM
5014 }
5015 plt_list = &h->plt.plist;
411e1bfb 5016 }
cbf95972
AM
5017
5018 /* We may need a .plt entry if the function this reloc
5019 refers to is in a shared lib. */
5020 if (plt_list
133a1f60 5021 && !update_plt_info (abfd, plt_list, rel->r_addend))
cbf95972 5022 return FALSE;
411e1bfb
AM
5023 break;
5024
cbf95972
AM
5025 case R_PPC64_ADDR14:
5026 case R_PPC64_ADDR14_BRNTAKEN:
5027 case R_PPC64_ADDR14_BRTAKEN:
5028 case R_PPC64_ADDR24:
5029 goto dodyn;
5030
411e1bfb
AM
5031 case R_PPC64_TPREL64:
5032 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
7c8bbca5 5033 if (bfd_link_dll (info))
411e1bfb
AM
5034 info->flags |= DF_STATIC_TLS;
5035 goto dotlstoc;
5036
5037 case R_PPC64_DTPMOD64:
5038 if (rel + 1 < rel_end
5039 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5040 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5041 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5042 else
951fd09b 5043 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5044 goto dotlstoc;
5045
5046 case R_PPC64_DTPREL64:
5047 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5048 if (rel != relocs
5049 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5050 && rel[-1].r_offset == rel->r_offset - 8)
5051 /* This is the second reloc of a dtpmod, dtprel pair.
5052 Don't mark with TLS_DTPREL. */
5053 goto dodyn;
5054
5055 dotlstoc:
5056 sec->has_tls_reloc = 1;
5057 if (h != NULL)
ed7007c1 5058 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
411e1bfb
AM
5059 else
5060 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
133a1f60 5061 rel->r_addend, tls_type))
411e1bfb
AM
5062 return FALSE;
5063
7c8fe5c4
AM
5064 ppc64_sec = ppc64_elf_section_data (sec);
5065 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5066 {
3a71aa26
AM
5067 bfd_size_type amt;
5068
e7b938ca 5069 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5070 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5071 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5072 if (ppc64_sec->u.toc.symndx == NULL)
5073 return FALSE;
5074 amt = sec->size * sizeof (bfd_vma) / 8;
5075 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5076 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5077 return FALSE;
7c8fe5c4
AM
5078 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5079 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5080 }
5081 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26 5082 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
133a1f60 5083 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5084
5085 /* Mark the second slot of a GD or LD entry.
5086 -1 to indicate GD and -2 to indicate LD. */
5087 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5088 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5089 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5090 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5091 goto dodyn;
5092
411e1bfb
AM
5093 case R_PPC64_TPREL16_HI:
5094 case R_PPC64_TPREL16_HA:
f9c6b907
AM
5095 case R_PPC64_TPREL16_HIGH:
5096 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5097 case R_PPC64_TPREL16_HIGHER:
5098 case R_PPC64_TPREL16_HIGHERA:
5099 case R_PPC64_TPREL16_HIGHEST:
5100 case R_PPC64_TPREL16_HIGHESTA:
252dcdf4
AM
5101 sec->has_tls_reloc = 1;
5102 /* Fall through. */
c213164a 5103 case R_PPC64_TPREL34:
252dcdf4
AM
5104 case R_PPC64_TPREL16:
5105 case R_PPC64_TPREL16_DS:
5106 case R_PPC64_TPREL16_LO:
5107 case R_PPC64_TPREL16_LO_DS:
7c8bbca5
AM
5108 if (bfd_link_dll (info))
5109 info->flags |= DF_STATIC_TLS;
5110 goto dodyn;
5d1634d7 5111
e86ce104 5112 case R_PPC64_ADDR64:
b9399fcf 5113 if (is_opd
1e2f5b6e 5114 && rel + 1 < rel_end
4ce794b7 5115 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5116 {
8387904d 5117 if (h != NULL)
ed7007c1 5118 ppc_elf_hash_entry (h)->is_func = 1;
1e2f5b6e 5119 }
e86ce104
AM
5120 /* Fall through. */
5121
65f38f15
AM
5122 case R_PPC64_ADDR16:
5123 case R_PPC64_ADDR16_DS:
5124 case R_PPC64_ADDR16_HA:
5125 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5126 case R_PPC64_ADDR16_HIGH:
5127 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5128 case R_PPC64_ADDR16_HIGHER:
5129 case R_PPC64_ADDR16_HIGHERA:
5130 case R_PPC64_ADDR16_HIGHEST:
5131 case R_PPC64_ADDR16_HIGHESTA:
5132 case R_PPC64_ADDR16_LO:
5133 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
5134 case R_PPC64_D34:
5135 case R_PPC64_D34_LO:
5136 case R_PPC64_D34_HI30:
5137 case R_PPC64_D34_HA30:
5138 case R_PPC64_ADDR16_HIGHER34:
5139 case R_PPC64_ADDR16_HIGHERA34:
5140 case R_PPC64_ADDR16_HIGHEST34:
5141 case R_PPC64_ADDR16_HIGHESTA34:
5142 case R_PPC64_D28:
0e1862bb 5143 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
a345bc8d
AM
5144 && rel->r_addend == 0)
5145 {
5146 /* We may need a .plt entry if this reloc refers to a
5147 function in a shared lib. */
5663e321 5148 if (!update_plt_info (abfd, &h->plt.plist, 0))
a345bc8d
AM
5149 return FALSE;
5150 h->pointer_equality_needed = 1;
5151 }
5152 /* Fall through. */
5153
5154 case R_PPC64_REL30:
5155 case R_PPC64_REL32:
5156 case R_PPC64_REL64:
65f38f15 5157 case R_PPC64_ADDR32:
65f38f15
AM
5158 case R_PPC64_UADDR16:
5159 case R_PPC64_UADDR32:
5160 case R_PPC64_UADDR64:
5bd4f169 5161 case R_PPC64_TOC:
ec73ddcd 5162 if (h != NULL && bfd_link_executable (info))
81848ca0 5163 /* We may need a copy reloc. */
f5385ebf 5164 h->non_got_ref = 1;
81848ca0 5165
41bd81ab 5166 /* Don't propagate .opd relocs. */
b9399fcf 5167 if (NO_OPD_RELOCS && is_opd)
e86ce104 5168 break;
e86ce104 5169
65f38f15
AM
5170 /* If we are creating a shared library, and this is a reloc
5171 against a global symbol, or a non PC relative reloc
5172 against a local symbol, then we need to copy the reloc
5173 into the shared library. However, if we are linking with
5174 -Bsymbolic, we do not need to copy a reloc against a
5175 global symbol which is defined in an object we are
5176 including in the link (i.e., DEF_REGULAR is set). At
5177 this point we have not seen all the input files, so it is
5178 possible that DEF_REGULAR is not set now but will be set
5179 later (it is never cleared). In case of a weak definition,
5180 DEF_REGULAR may be cleared later by a strong definition in
5181 a shared library. We account for that possibility below by
f4656909 5182 storing information in the dyn_relocs field of the hash
65f38f15
AM
5183 table entry. A similar situation occurs when creating
5184 shared libraries and symbol visibility changes render the
5185 symbol local.
5186
5187 If on the other hand, we are creating an executable, we
5188 may need to keep relocations for symbols satisfied by a
5189 dynamic library if we manage to avoid copy relocs for the
5190 symbol. */
411e1bfb 5191 dodyn:
ec73ddcd
AM
5192 if ((h != NULL
5193 && (h->root.type == bfd_link_hash_defweak
5194 || !h->def_regular))
5195 || (h != NULL
5196 && !bfd_link_executable (info)
5197 && !SYMBOLIC_BIND (info, h))
5198 || (bfd_link_pic (info)
5199 && must_be_dyn_reloc (info, r_type))
0e1862bb 5200 || (!bfd_link_pic (info)
25f23106 5201 && ifunc != NULL))
5bd4f169 5202 {
65f38f15
AM
5203 /* We must copy these reloc types into the output file.
5204 Create a reloc section in dynobj and make room for
5205 this reloc. */
5bd4f169
AM
5206 if (sreloc == NULL)
5207 {
83bac4b0
NC
5208 sreloc = _bfd_elf_make_dynamic_reloc_section
5209 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5210
5bd4f169 5211 if (sreloc == NULL)
83bac4b0 5212 return FALSE;
5bd4f169
AM
5213 }
5214
65f38f15
AM
5215 /* If this is a global symbol, we count the number of
5216 relocations we need for this symbol. */
5217 if (h != NULL)
5218 {
19e08130
AM
5219 struct elf_dyn_relocs *p;
5220 struct elf_dyn_relocs **head;
5221
190eb1dd 5222 head = &h->dyn_relocs;
19e08130
AM
5223 p = *head;
5224 if (p == NULL || p->sec != sec)
5225 {
5226 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5227 if (p == NULL)
5228 return FALSE;
5229 p->next = *head;
5230 *head = p;
5231 p->sec = sec;
5232 p->count = 0;
5233 p->pc_count = 0;
5234 }
5235 p->count += 1;
5236 if (!must_be_dyn_reloc (info, r_type))
5237 p->pc_count += 1;
65f38f15
AM
5238 }
5239 else
5240 {
ec338859
AM
5241 /* Track dynamic relocs needed for local syms too.
5242 We really need local syms available to do this
5243 easily. Oh well. */
19e08130
AM
5244 struct ppc_dyn_relocs *p;
5245 struct ppc_dyn_relocs **head;
5246 bfd_boolean is_ifunc;
ec338859 5247 asection *s;
6edfbbad 5248 void *vpp;
87d72d41 5249 Elf_Internal_Sym *isym;
6edfbbad 5250
f1dfbfdb 5251 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
5252 abfd, r_symndx);
5253 if (isym == NULL)
b34976b6 5254 return FALSE;
ec338859 5255
87d72d41
AM
5256 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5257 if (s == NULL)
5258 s = sec;
5259
6edfbbad 5260 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5261 head = (struct ppc_dyn_relocs **) vpp;
5262 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5263 p = *head;
5264 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5265 p = p->next;
5266 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5267 {
5268 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5269 if (p == NULL)
5270 return FALSE;
5271 p->next = *head;
5272 *head = p;
5273 p->sec = sec;
5274 p->ifunc = is_ifunc;
5275 p->count = 0;
5276 }
5277 p->count += 1;
ec338859 5278 }
65f38f15 5279 }
5bd4f169 5280 break;
65f38f15
AM
5281
5282 default:
96e0dda4 5283 break;
5bd4f169
AM
5284 }
5285 }
5286
b34976b6 5287 return TRUE;
5bd4f169
AM
5288}
5289
ee67d69a
AM
5290/* Merge backend specific data from an object file to the output
5291 object file when linking. */
5292
5293static bfd_boolean
50e03d47 5294ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
ee67d69a 5295{
50e03d47 5296 bfd *obfd = info->output_bfd;
ee67d69a
AM
5297 unsigned long iflags, oflags;
5298
5299 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5300 return TRUE;
5301
5302 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5303 return TRUE;
5304
50e03d47 5305 if (!_bfd_generic_verify_endian_match (ibfd, info))
ee67d69a
AM
5306 return FALSE;
5307
5308 iflags = elf_elfheader (ibfd)->e_flags;
5309 oflags = elf_elfheader (obfd)->e_flags;
5310
f6c7c3e8 5311 if (iflags & ~EF_PPC64_ABI)
ee67d69a 5312 {
4eca0228 5313 _bfd_error_handler
695344c0 5314 /* xgettext:c-format */
871b3ab2 5315 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
ee67d69a
AM
5316 bfd_set_error (bfd_error_bad_value);
5317 return FALSE;
5318 }
f6c7c3e8 5319 else if (iflags != oflags && iflags != 0)
ee67d69a 5320 {
4eca0228 5321 _bfd_error_handler
695344c0 5322 /* xgettext:c-format */
871b3ab2 5323 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
ee67d69a
AM
5324 ibfd, iflags, oflags);
5325 bfd_set_error (bfd_error_bad_value);
5326 return FALSE;
5327 }
5328
4a91d0ba
AM
5329 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5330 return FALSE;
005d79fd 5331
ee67d69a 5332 /* Merge Tag_compatibility attributes and any common GNU ones. */
8d2c8c3d 5333 return _bfd_elf_merge_object_attributes (ibfd, info);
ee67d69a
AM
5334}
5335
5336static bfd_boolean
5337ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5338{
5339 /* Print normal ELF private data. */
5340 _bfd_elf_print_private_bfd_data (abfd, ptr);
5341
5342 if (elf_elfheader (abfd)->e_flags != 0)
5343 {
5344 FILE *file = ptr;
5345
ee67d69a
AM
5346 fprintf (file, _("private flags = 0x%lx:"),
5347 elf_elfheader (abfd)->e_flags);
5348
5349 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5350 fprintf (file, _(" [abiv%ld]"),
5351 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5352 fputc ('\n', file);
5353 }
5354
5355 return TRUE;
5356}
5357
8387904d 5358/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
b53dfeb2
AM
5359 of the code entry point, and its section, which must be in the same
5360 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
8387904d
AM
5361
5362static bfd_vma
5363opd_entry_value (asection *opd_sec,
5364 bfd_vma offset,
5365 asection **code_sec,
aef36ac1
AM
5366 bfd_vma *code_off,
5367 bfd_boolean in_code_sec)
8387904d
AM
5368{
5369 bfd *opd_bfd = opd_sec->owner;
8860955f 5370 Elf_Internal_Rela *relocs;
8387904d 5371 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5372 bfd_vma val;
8387904d 5373
9f296da3
AM
5374 /* No relocs implies we are linking a --just-symbols object, or looking
5375 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5376 if (opd_sec->reloc_count == 0)
5377 {
729eabd5 5378 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5379
729eabd5
AM
5380 if (contents == NULL)
5381 {
5382 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5383 return (bfd_vma) -1;
5384 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5385 }
ee1e4ede 5386
dbb3fbbb 5387 /* PR 17512: file: 64b9dfbb. */
451dfd38 5388 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
dbb3fbbb
NC
5389 return (bfd_vma) -1;
5390
729eabd5 5391 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5392 if (code_sec != NULL)
5393 {
5394 asection *sec, *likely = NULL;
ee1e4ede 5395
aef36ac1 5396 if (in_code_sec)
4b85d634 5397 {
aef36ac1
AM
5398 sec = *code_sec;
5399 if (sec->vma <= val
5400 && val < sec->vma + sec->size)
5401 likely = sec;
5402 else
5403 val = -1;
5404 }
5405 else
5406 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5407 if (sec->vma <= val
5408 && (sec->flags & SEC_LOAD) != 0
5409 && (sec->flags & SEC_ALLOC) != 0)
5410 likely = sec;
5411 if (likely != NULL)
5412 {
5413 *code_sec = likely;
5414 if (code_off != NULL)
5415 *code_off = val - likely->vma;
4b85d634
AM
5416 }
5417 }
aef36ac1 5418 return val;
4b85d634
AM
5419 }
5420
0c8d6e5c 5421 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5422
729eabd5 5423 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5424 if (relocs == NULL)
5425 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
877a8638
NC
5426 /* PR 17512: file: df8e1fd6. */
5427 if (relocs == NULL)
5428 return (bfd_vma) -1;
645ea6a9 5429
8387904d 5430 /* Go find the opd reloc at the sym address. */
8860955f 5431 lo = relocs;
8387904d 5432 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5433 val = (bfd_vma) -1;
8387904d
AM
5434 while (lo < hi)
5435 {
5436 look = lo + (hi - lo) / 2;
5437 if (look->r_offset < offset)
5438 lo = look + 1;
5439 else if (look->r_offset > offset)
5440 hi = look;
5441 else
5442 {
0ffa91dd
NC
5443 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5444
8387904d
AM
5445 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5446 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5447 {
5448 unsigned long symndx = ELF64_R_SYM (look->r_info);
b53dfeb2 5449 asection *sec = NULL;
8387904d 5450
b53dfeb2
AM
5451 if (symndx >= symtab_hdr->sh_info
5452 && elf_sym_hashes (opd_bfd) != NULL)
8387904d
AM
5453 {
5454 struct elf_link_hash_entry **sym_hashes;
5455 struct elf_link_hash_entry *rh;
5456
5457 sym_hashes = elf_sym_hashes (opd_bfd);
5458 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5459 if (rh != NULL)
5460 {
5461 rh = elf_follow_link (rh);
bb854a36
AM
5462 if (rh->root.type != bfd_link_hash_defined
5463 && rh->root.type != bfd_link_hash_defweak)
5464 break;
5465 if (rh->root.u.def.section->owner == opd_bfd)
b53dfeb2 5466 {
bb854a36
AM
5467 val = rh->root.u.def.value;
5468 sec = rh->root.u.def.section;
b53dfeb2
AM
5469 }
5470 }
5471 }
5472
5473 if (sec == NULL)
5474 {
5475 Elf_Internal_Sym *sym;
5476
5477 if (symndx < symtab_hdr->sh_info)
5478 {
5479 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5480 if (sym == NULL)
5481 {
5482 size_t symcnt = symtab_hdr->sh_info;
5483 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5484 symcnt, 0,
5485 NULL, NULL, NULL);
5486 if (sym == NULL)
5487 break;
5488 symtab_hdr->contents = (bfd_byte *) sym;
5489 }
5490 sym += symndx;
128205bb
AM
5491 }
5492 else
5493 {
b53dfeb2
AM
5494 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5495 1, symndx,
5496 NULL, NULL, NULL);
128205bb
AM
5497 if (sym == NULL)
5498 break;
128205bb 5499 }
b53dfeb2
AM
5500 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5501 if (sec == NULL)
5502 break;
5503 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5504 val = sym->st_value;
8387904d 5505 }
b53dfeb2 5506
8387904d
AM
5507 val += look->r_addend;
5508 if (code_off != NULL)
5509 *code_off = val;
5510 if (code_sec != NULL)
aef36ac1
AM
5511 {
5512 if (in_code_sec && *code_sec != sec)
5513 return -1;
5514 else
5515 *code_sec = sec;
5516 }
b53dfeb2 5517 if (sec->output_section != NULL)
8387904d 5518 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5519 }
5520 break;
5521 }
5522 }
645ea6a9 5523
645ea6a9 5524 return val;
8387904d
AM
5525}
5526
aef36ac1
AM
5527/* If the ELF symbol SYM might be a function in SEC, return the
5528 function size and set *CODE_OFF to the function's entry point,
5529 otherwise return zero. */
9f296da3 5530
aef36ac1
AM
5531static bfd_size_type
5532ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5533 bfd_vma *code_off)
9f296da3 5534{
aef36ac1
AM
5535 bfd_size_type size;
5536
5537 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5538 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5539 return 0;
5540
5541 size = 0;
5542 if (!(sym->flags & BSF_SYNTHETIC))
5543 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5544
5545 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5546 {
b07bca4e
AM
5547 struct _opd_sec_data *opd = get_opd_info (sym->section);
5548 bfd_vma symval = sym->value;
5549
5550 if (opd != NULL
5551 && opd->adjust != NULL
5552 && elf_section_data (sym->section)->relocs != NULL)
5553 {
5554 /* opd_entry_value will use cached relocs that have been
5555 adjusted, but with raw symbols. That means both local
5556 and global symbols need adjusting. */
5557 long adjust = opd->adjust[OPD_NDX (symval)];
5558 if (adjust == -1)
5559 return 0;
5560 symval += adjust;
5561 }
5562
5563 if (opd_entry_value (sym->section, symval,
aef36ac1
AM
5564 &sec, code_off, TRUE) == (bfd_vma) -1)
5565 return 0;
5566 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5567 symbol. This size has nothing to do with the code size of the
5568 function, which is what we're supposed to return, but the
5569 code size isn't available without looking up the dot-sym.
5570 However, doing that would be a waste of time particularly
5571 since elf_find_function will look at the dot-sym anyway.
5572 Now, elf_find_function will keep the largest size of any
5573 function sym found at the code address of interest, so return
5574 1 here to avoid it incorrectly caching a larger function size
5575 for a small function. This does mean we return the wrong
5576 size for a new-ABI function of size 24, but all that does is
5577 disable caching for such functions. */
5578 if (size == 24)
5579 size = 1;
9f296da3 5580 }
aef36ac1
AM
5581 else
5582 {
5583 if (sym->section != sec)
5584 return 0;
5585 *code_off = sym->value;
5586 }
5587 if (size == 0)
5588 size = 1;
5589 return size;
9f296da3
AM
5590}
5591
f378ab09
AM
5592/* Return true if symbol is a strong function defined in an ELFv2
5593 object with st_other localentry bits of zero, ie. its local entry
5594 point coincides with its global entry point. */
5595
5596static bfd_boolean
5597is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5598{
5599 return (h != NULL
5600 && h->type == STT_FUNC
5601 && h->root.type == bfd_link_hash_defined
5602 && (STO_PPC64_LOCAL_MASK & h->other) == 0
ed7007c1 5603 && !ppc_elf_hash_entry (h)->non_zero_localentry
f378ab09
AM
5604 && is_ppc64_elf (h->root.u.def.section->owner)
5605 && abiversion (h->root.u.def.section->owner) >= 2);
5606}
5607
854b41e7
AM
5608/* Return true if symbol is defined in a regular object file. */
5609
5610static bfd_boolean
5611is_static_defined (struct elf_link_hash_entry *h)
5612{
5613 return ((h->root.type == bfd_link_hash_defined
5614 || h->root.type == bfd_link_hash_defweak)
5615 && h->root.u.def.section != NULL
5616 && h->root.u.def.section->output_section != NULL);
5617}
5618
b31867b6
AM
5619/* If FDH is a function descriptor symbol, return the associated code
5620 entry symbol if it is defined. Return NULL otherwise. */
5621
5622static struct ppc_link_hash_entry *
5623defined_code_entry (struct ppc_link_hash_entry *fdh)
5624{
5625 if (fdh->is_func_descriptor)
5626 {
5627 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5628 if (fh->elf.root.type == bfd_link_hash_defined
5629 || fh->elf.root.type == bfd_link_hash_defweak)
5630 return fh;
5631 }
5632 return NULL;
5633}
5634
5635/* If FH is a function code entry symbol, return the associated
5636 function descriptor symbol if it is defined. Return NULL otherwise. */
5637
5638static struct ppc_link_hash_entry *
5639defined_func_desc (struct ppc_link_hash_entry *fh)
5640{
5641 if (fh->oh != NULL
5642 && fh->oh->is_func_descriptor)
5643 {
5644 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5645 if (fdh->elf.root.type == bfd_link_hash_defined
5646 || fdh->elf.root.type == bfd_link_hash_defweak)
5647 return fdh;
5648 }
5649 return NULL;
5650}
5651
ed7007c1
AM
5652/* Given H is a symbol that satisfies is_static_defined, return the
5653 value in the output file. */
5654
5655static bfd_vma
5656defined_sym_val (struct elf_link_hash_entry *h)
5657{
5658 return (h->root.u.def.section->output_section->vma
5659 + h->root.u.def.section->output_offset
5660 + h->root.u.def.value);
5661}
5662
5663/* Return true if H matches __tls_get_addr or one of its variants. */
5664
5665static bfd_boolean
5666is_tls_get_addr (struct elf_link_hash_entry *h,
5667 struct ppc_link_hash_table *htab)
5668{
30845f11
AM
5669 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5670 || h == elf_hash_entry (htab->tga_desc_fd)
5671 || h == elf_hash_entry (htab->tls_get_addr)
5672 || h == elf_hash_entry (htab->tga_desc));
ed7007c1
AM
5673}
5674
8c5b4e52
AM
5675static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
5676
5677/* Garbage collect sections, after first dealing with dot-symbols. */
5678
5679static bfd_boolean
5680ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5681{
5682 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5683
5684 if (htab != NULL && htab->need_func_desc_adj)
5685 {
5686 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5687 htab->need_func_desc_adj = 0;
5688 }
5689 return bfd_elf_gc_sections (abfd, info);
5690}
5691
74f0fb50
AM
5692/* Mark all our entry sym sections, both opd and code section. */
5693
5694static void
5695ppc64_elf_gc_keep (struct bfd_link_info *info)
5696{
5697 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5698 struct bfd_sym_chain *sym;
5699
4dfe6ac6
NC
5700 if (htab == NULL)
5701 return;
5702
74f0fb50
AM
5703 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5704 {
b31867b6 5705 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5706 asection *sec;
5707
ed7007c1
AM
5708 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5709 FALSE, FALSE, TRUE));
74f0fb50
AM
5710 if (eh == NULL)
5711 continue;
5712 if (eh->elf.root.type != bfd_link_hash_defined
5713 && eh->elf.root.type != bfd_link_hash_defweak)
5714 continue;
5715
b31867b6
AM
5716 fh = defined_code_entry (eh);
5717 if (fh != NULL)
74f0fb50 5718 {
b31867b6 5719 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5720 sec->flags |= SEC_KEEP;
5721 }
5722 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5723 && opd_entry_value (eh->elf.root.u.def.section,
5724 eh->elf.root.u.def.value,
aef36ac1 5725 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
5726 sec->flags |= SEC_KEEP;
5727
5728 sec = eh->elf.root.u.def.section;
5729 sec->flags |= SEC_KEEP;
5730 }
5731}
5732
64d03ab5
AM
5733/* Mark sections containing dynamically referenced symbols. When
5734 building shared libraries, we must assume that any visible symbol is
5735 referenced. */
5736
5737static bfd_boolean
5738ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5739{
5740 struct bfd_link_info *info = (struct bfd_link_info *) inf;
ed7007c1 5741 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
b31867b6 5742 struct ppc_link_hash_entry *fdh;
b407645f 5743 struct bfd_elf_dynamic_list *d = info->dynamic_list;
64d03ab5 5744
64d03ab5 5745 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5746 fdh = defined_func_desc (eh);
5747 if (fdh != NULL)
5748 eh = fdh;
64d03ab5
AM
5749
5750 if ((eh->elf.root.type == bfd_link_hash_defined
5751 || eh->elf.root.type == bfd_link_hash_defweak)
87e79a65 5752 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
1c9177d9 5753 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
64d03ab5 5754 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8 5755 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
0e1862bb 5756 && (!bfd_link_executable (info)
e278ae05 5757 || info->gc_keep_exported
b407645f
AM
5758 || info->export_dynamic
5759 || (eh->elf.dynamic
5760 && d != NULL
2cdcc330
AM
5761 && (*d->match) (&d->head, NULL,
5762 eh->elf.root.root.string)))
e278ae05 5763 && (eh->elf.versioned >= versioned
4c58e0d8
AM
5764 || !bfd_hide_sym_by_version (info->version_info,
5765 eh->elf.root.root.string)))))
64d03ab5
AM
5766 {
5767 asection *code_sec;
b31867b6 5768 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5769
5770 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5771
5772 /* Function descriptor syms cause the associated
5773 function code sym section to be marked. */
b31867b6
AM
5774 fh = defined_code_entry (eh);
5775 if (fh != NULL)
5776 {
5777 code_sec = fh->elf.root.u.def.section;
5778 code_sec->flags |= SEC_KEEP;
5779 }
64d03ab5
AM
5780 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5781 && opd_entry_value (eh->elf.root.u.def.section,
5782 eh->elf.root.u.def.value,
aef36ac1 5783 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
5784 code_sec->flags |= SEC_KEEP;
5785 }
5786
5787 return TRUE;
5788}
5789
5bd4f169
AM
5790/* Return the section that should be marked against GC for a given
5791 relocation. */
5792
5793static asection *
4ce794b7 5794ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5795 struct bfd_link_info *info,
4ce794b7
AM
5796 Elf_Internal_Rela *rel,
5797 struct elf_link_hash_entry *h,
5798 Elf_Internal_Sym *sym)
5bd4f169 5799{
ccfa59ea
AM
5800 asection *rsec;
5801
ccfa59ea
AM
5802 /* Syms return NULL if we're marking .opd, so we avoid marking all
5803 function sections, as all functions are referenced in .opd. */
5804 rsec = NULL;
5805 if (get_opd_info (sec) != NULL)
5806 return rsec;
1e2f5b6e 5807
5bd4f169
AM
5808 if (h != NULL)
5809 {
04c9666a 5810 enum elf_ppc64_reloc_type r_type;
b31867b6 5811 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5812
4ce794b7 5813 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5814 switch (r_type)
5bd4f169
AM
5815 {
5816 case R_PPC64_GNU_VTINHERIT:
5817 case R_PPC64_GNU_VTENTRY:
5818 break;
5819
5820 default:
5821 switch (h->root.type)
5822 {
5823 case bfd_link_hash_defined:
5824 case bfd_link_hash_defweak:
ed7007c1 5825 eh = ppc_elf_hash_entry (h);
b31867b6
AM
5826 fdh = defined_func_desc (eh);
5827 if (fdh != NULL)
8c5b4e52
AM
5828 {
5829 /* -mcall-aixdesc code references the dot-symbol on
5830 a call reloc. Mark the function descriptor too
5831 against garbage collection. */
5832 fdh->elf.mark = 1;
60d67dc8
AM
5833 if (fdh->elf.is_weakalias)
5834 weakdef (&fdh->elf)->mark = 1;
8c5b4e52
AM
5835 eh = fdh;
5836 }
1e2f5b6e
AM
5837
5838 /* Function descriptor syms cause the associated
5839 function code sym section to be marked. */
b31867b6
AM
5840 fh = defined_code_entry (eh);
5841 if (fh != NULL)
ccfa59ea
AM
5842 {
5843 /* They also mark their opd section. */
74f0fb50 5844 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5845
b31867b6 5846 rsec = fh->elf.root.u.def.section;
ccfa59ea 5847 }
8387904d
AM
5848 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5849 && opd_entry_value (eh->elf.root.u.def.section,
5850 eh->elf.root.u.def.value,
aef36ac1 5851 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 5852 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5853 else
1e2f5b6e
AM
5854 rsec = h->root.u.def.section;
5855 break;
5bd4f169
AM
5856
5857 case bfd_link_hash_common:
1e2f5b6e
AM
5858 rsec = h->root.u.c.p->section;
5859 break;
5bd4f169
AM
5860
5861 default:
fb34365b 5862 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5863 }
5864 }
5865 }
5866 else
5867 {
74f0fb50 5868 struct _opd_sec_data *opd;
1e2f5b6e
AM
5869
5870 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5871 opd = get_opd_info (rsec);
5872 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5873 {
74f0fb50 5874 rsec->gc_mark = 1;
ccfa59ea 5875
51aecdc5 5876 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
ccfa59ea 5877 }
5bd4f169
AM
5878 }
5879
1e2f5b6e 5880 return rsec;
5bd4f169
AM
5881}
5882
deb0e272
AM
5883/* The maximum size of .sfpr. */
5884#define SFPR_MAX (218*4)
5885
5886struct sfpr_def_parms
5887{
699733f6
AM
5888 const char name[12];
5889 unsigned char lo, hi;
2cdcc330
AM
5890 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5891 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
deb0e272
AM
5892};
5893
a4b6fadd
AM
5894/* Auto-generate _save*, _rest* functions in .sfpr.
5895 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5896 instead. */
deb0e272 5897
4dfe6ac6 5898static bfd_boolean
a4b6fadd
AM
5899sfpr_define (struct bfd_link_info *info,
5900 const struct sfpr_def_parms *parm,
5901 asection *stub_sec)
deb0e272
AM
5902{
5903 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5904 unsigned int i;
5905 size_t len = strlen (parm->name);
5906 bfd_boolean writing = FALSE;
699733f6 5907 char sym[16];
deb0e272 5908
4dfe6ac6
NC
5909 if (htab == NULL)
5910 return FALSE;
5911
deb0e272
AM
5912 memcpy (sym, parm->name, len);
5913 sym[len + 2] = 0;
5914
5915 for (i = parm->lo; i <= parm->hi; i++)
5916 {
a4b6fadd 5917 struct ppc_link_hash_entry *h;
deb0e272
AM
5918
5919 sym[len + 0] = i / 10 + '0';
5920 sym[len + 1] = i % 10 + '0';
ed7007c1
AM
5921 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
5922 writing, TRUE, TRUE));
a4b6fadd 5923 if (stub_sec != NULL)
deb0e272 5924 {
a4b6fadd
AM
5925 if (h != NULL
5926 && h->elf.root.type == bfd_link_hash_defined
5927 && h->elf.root.u.def.section == htab->sfpr)
5928 {
5929 struct elf_link_hash_entry *s;
5930 char buf[32];
5931 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5932 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
5933 if (s == NULL)
5934 return FALSE;
779f2ae7 5935 if (s->root.type == bfd_link_hash_new)
a4b6fadd
AM
5936 {
5937 s->root.type = bfd_link_hash_defined;
5938 s->root.u.def.section = stub_sec;
7dda8d3c 5939 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
a4b6fadd
AM
5940 + h->elf.root.u.def.value);
5941 s->ref_regular = 1;
5942 s->def_regular = 1;
5943 s->ref_regular_nonweak = 1;
5944 s->forced_local = 1;
5945 s->non_elf = 0;
5946 s->root.linker_def = 1;
5947 }
5948 }
5949 continue;
5950 }
5951 if (h != NULL)
5952 {
5953 h->save_res = 1;
5954 if (!h->elf.def_regular)
deb0e272 5955 {
a4b6fadd
AM
5956 h->elf.root.type = bfd_link_hash_defined;
5957 h->elf.root.u.def.section = htab->sfpr;
5958 h->elf.root.u.def.value = htab->sfpr->size;
5959 h->elf.type = STT_FUNC;
5960 h->elf.def_regular = 1;
b32547cd 5961 h->elf.non_elf = 0;
a4b6fadd
AM
5962 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
5963 writing = TRUE;
deb0e272 5964 if (htab->sfpr->contents == NULL)
a4b6fadd 5965 {
2cdcc330
AM
5966 htab->sfpr->contents
5967 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
a4b6fadd
AM
5968 if (htab->sfpr->contents == NULL)
5969 return FALSE;
5970 }
deb0e272
AM
5971 }
5972 }
5973 if (writing)
5974 {
5975 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5976 if (i != parm->hi)
5977 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5978 else
5979 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5980 htab->sfpr->size = p - htab->sfpr->contents;
5981 }
5982 }
5983
5984 return TRUE;
5985}
5986
5987static bfd_byte *
5988savegpr0 (bfd *abfd, bfd_byte *p, int r)
5989{
5990 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5991 return p + 4;
5992}
5993
5994static bfd_byte *
5995savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5996{
5997 p = savegpr0 (abfd, p, r);
a078d95a 5998 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
5999 p = p + 4;
6000 bfd_put_32 (abfd, BLR, p);
6001 return p + 4;
6002}
6003
6004static bfd_byte *
6005restgpr0 (bfd *abfd, bfd_byte *p, int r)
6006{
6007 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6008 return p + 4;
6009}
6010
6011static bfd_byte *
6012restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6013{
a078d95a 6014 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6015 p = p + 4;
6016 p = restgpr0 (abfd, p, r);
6017 bfd_put_32 (abfd, MTLR_R0, p);
6018 p = p + 4;
6019 if (r == 29)
6020 {
6021 p = restgpr0 (abfd, p, 30);
6022 p = restgpr0 (abfd, p, 31);
6023 }
6024 bfd_put_32 (abfd, BLR, p);
6025 return p + 4;
6026}
6027
6028static bfd_byte *
6029savegpr1 (bfd *abfd, bfd_byte *p, int r)
6030{
6031 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6032 return p + 4;
6033}
6034
6035static bfd_byte *
6036savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6037{
6038 p = savegpr1 (abfd, p, r);
6039 bfd_put_32 (abfd, BLR, p);
6040 return p + 4;
6041}
6042
6043static bfd_byte *
6044restgpr1 (bfd *abfd, bfd_byte *p, int r)
6045{
6046 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6047 return p + 4;
6048}
6049
6050static bfd_byte *
6051restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6052{
6053 p = restgpr1 (abfd, p, r);
6054 bfd_put_32 (abfd, BLR, p);
6055 return p + 4;
6056}
6057
6058static bfd_byte *
6059savefpr (bfd *abfd, bfd_byte *p, int r)
6060{
6061 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6062 return p + 4;
6063}
6064
6065static bfd_byte *
6066savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6067{
6068 p = savefpr (abfd, p, r);
a078d95a 6069 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
deb0e272
AM
6070 p = p + 4;
6071 bfd_put_32 (abfd, BLR, p);
6072 return p + 4;
6073}
6074
6075static bfd_byte *
6076restfpr (bfd *abfd, bfd_byte *p, int r)
6077{
6078 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6079 return p + 4;
6080}
6081
6082static bfd_byte *
6083restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6084{
a078d95a 6085 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
deb0e272
AM
6086 p = p + 4;
6087 p = restfpr (abfd, p, r);
6088 bfd_put_32 (abfd, MTLR_R0, p);
6089 p = p + 4;
6090 if (r == 29)
6091 {
6092 p = restfpr (abfd, p, 30);
6093 p = restfpr (abfd, p, 31);
6094 }
6095 bfd_put_32 (abfd, BLR, p);
6096 return p + 4;
6097}
6098
6099static bfd_byte *
6100savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6101{
6102 p = savefpr (abfd, p, r);
6103 bfd_put_32 (abfd, BLR, p);
6104 return p + 4;
6105}
6106
6107static bfd_byte *
6108restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6109{
6110 p = restfpr (abfd, p, r);
6111 bfd_put_32 (abfd, BLR, p);
6112 return p + 4;
6113}
6114
6115static bfd_byte *
6116savevr (bfd *abfd, bfd_byte *p, int r)
6117{
6118 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6119 p = p + 4;
6120 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6121 return p + 4;
6122}
6123
6124static bfd_byte *
6125savevr_tail (bfd *abfd, bfd_byte *p, int r)
6126{
6127 p = savevr (abfd, p, r);
6128 bfd_put_32 (abfd, BLR, p);
6129 return p + 4;
6130}
6131
6132static bfd_byte *
6133restvr (bfd *abfd, bfd_byte *p, int r)
6134{
6135 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6136 p = p + 4;
6137 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6138 return p + 4;
6139}
6140
6141static bfd_byte *
6142restvr_tail (bfd *abfd, bfd_byte *p, int r)
6143{
6144 p = restvr (abfd, p, r);
6145 bfd_put_32 (abfd, BLR, p);
6146 return p + 4;
6147}
6148
9e7028aa
AM
6149#define STDU_R1_0R1 0xf8210001
6150#define ADDI_R1_R1 0x38210000
6151
6152/* Emit prologue of wrapper preserving regs around a call to
6153 __tls_get_addr_opt. */
6154
6155static bfd_byte *
6156tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6157{
6158 unsigned int i;
6159
6160 bfd_put_32 (obfd, MFLR_R0, p);
6161 p += 4;
6162 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6163 p += 4;
6164
6165 if (htab->opd_abi)
6166 {
6167 for (i = 4; i < 12; i++)
6168 {
6169 bfd_put_32 (obfd,
6170 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6171 p += 4;
6172 }
6173 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6174 p += 4;
6175 }
6176 else
6177 {
6178 for (i = 4; i < 12; i++)
6179 {
6180 bfd_put_32 (obfd,
6181 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6182 p += 4;
6183 }
6184 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6185 p += 4;
6186 }
6187 return p;
6188}
6189
6190/* Emit epilogue of wrapper preserving regs around a call to
6191 __tls_get_addr_opt. */
6192
6193static bfd_byte *
6194tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6195{
6196 unsigned int i;
6197
6198 if (htab->opd_abi)
6199 {
6200 for (i = 4; i < 12; i++)
6201 {
6202 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6203 p += 4;
6204 }
6205 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6206 p += 4;
6207 }
6208 else
6209 {
6210 for (i = 4; i < 12; i++)
6211 {
6212 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6213 p += 4;
6214 }
6215 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6216 p += 4;
6217 }
6218 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6219 p += 4;
6220 bfd_put_32 (obfd, MTLR_R0, p);
6221 p += 4;
6222 bfd_put_32 (obfd, BLR, p);
6223 p += 4;
6224 return p;
6225}
6226
e86ce104
AM
6227/* Called via elf_link_hash_traverse to transfer dynamic linking
6228 information on function code symbol entries to their corresponding
6229 function descriptor symbol entries. */
deb0e272 6230
b34976b6 6231static bfd_boolean
4ce794b7 6232func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6233{
e86ce104 6234 struct bfd_link_info *info;
65f38f15 6235 struct ppc_link_hash_table *htab;
50bc7936
AM
6236 struct ppc_link_hash_entry *fh;
6237 struct ppc_link_hash_entry *fdh;
6238 bfd_boolean force_local;
5bd4f169 6239
ed7007c1 6240 fh = ppc_elf_hash_entry (h);
50bc7936 6241 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6242 return TRUE;
e86ce104 6243
8c5b4e52
AM
6244 if (!fh->is_func)
6245 return TRUE;
6246
6247 if (fh->elf.root.root.string[0] != '.'
6248 || fh->elf.root.root.string[1] == '\0')
6249 return TRUE;
6250
4ce794b7 6251 info = inf;
65f38f15 6252 htab = ppc_hash_table (info);
4dfe6ac6
NC
6253 if (htab == NULL)
6254 return FALSE;
5bd4f169 6255
8c5b4e52
AM
6256 /* Find the corresponding function descriptor symbol. */
6257 fdh = lookup_fdh (fh, htab);
6258
c09bdfe5
AM
6259 /* Resolve undefined references to dot-symbols as the value
6260 in the function descriptor, if we have one in a regular object.
6261 This is to satisfy cases like ".quad .foo". Calls to functions
6262 in dynamic objects are handled elsewhere. */
8c5b4e52
AM
6263 if ((fh->elf.root.type == bfd_link_hash_undefined
6264 || fh->elf.root.type == bfd_link_hash_undefweak)
6265 && (fdh->elf.root.type == bfd_link_hash_defined
6266 || fdh->elf.root.type == bfd_link_hash_defweak)
b31867b6
AM
6267 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6268 && opd_entry_value (fdh->elf.root.u.def.section,
6269 fdh->elf.root.u.def.value,
c09bdfe5 6270 &fh->elf.root.u.def.section,
aef36ac1 6271 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6272 {
b31867b6 6273 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6274 fh->elf.forced_local = 1;
b31867b6
AM
6275 fh->elf.def_regular = fdh->elf.def_regular;
6276 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6277 }
6278
8c5b4e52
AM
6279 if (!fh->elf.dynamic)
6280 {
6281 struct plt_entry *ent;
5bd4f169 6282
8c5b4e52
AM
6283 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6284 if (ent->plt.refcount > 0)
6285 break;
6286 if (ent == NULL)
6287 return TRUE;
6288 }
5bd4f169 6289
8c5b4e52 6290 /* Create a descriptor as undefined if necessary. */
50bc7936 6291 if (fdh == NULL
0e1862bb 6292 && !bfd_link_executable (info)
50bc7936
AM
6293 && (fh->elf.root.type == bfd_link_hash_undefined
6294 || fh->elf.root.type == bfd_link_hash_undefweak))
6295 {
908b32fc 6296 fdh = make_fdh (info, fh);
bb700d78
AM
6297 if (fdh == NULL)
6298 return FALSE;
50bc7936 6299 }
648cca2c 6300
8c5b4e52 6301 /* We can't support overriding of symbols on a fake descriptor. */
908b32fc
AM
6302 if (fdh != NULL
6303 && fdh->fake
8c5b4e52
AM
6304 && (fh->elf.root.type == bfd_link_hash_defined
6305 || fh->elf.root.type == bfd_link_hash_defweak))
6306 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
908b32fc 6307
8c5b4e52
AM
6308 /* Transfer dynamic linking information to the function descriptor. */
6309 if (fdh != NULL)
6310 {
f5385ebf
AM
6311 fdh->elf.ref_regular |= fh->elf.ref_regular;
6312 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6313 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6314 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
8c5b4e52
AM
6315 fdh->elf.dynamic |= fh->elf.dynamic;
6316 fdh->elf.needs_plt |= (fh->elf.needs_plt
6317 || fh->elf.type == STT_FUNC
6318 || fh->elf.type == STT_GNU_IFUNC);
6319 move_plt_plist (fh, fdh);
6320
6321 if (!fdh->elf.forced_local
6322 && fh->elf.dynindx != -1)
6323 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6324 return FALSE;
e86ce104
AM
6325 }
6326
50bc7936
AM
6327 /* Now that the info is on the function descriptor, clear the
6328 function code sym info. Any function code syms for which we
6329 don't have a definition in a regular file, we force local.
6330 This prevents a shared library from exporting syms that have
6331 been imported from another library. Function code syms that
6332 are really in the library we must leave global to prevent the
6333 linker dragging in a definition from a static library. */
93f3fa99
AM
6334 force_local = (!fh->elf.def_regular
6335 || fdh == NULL
6336 || !fdh->elf.def_regular
6337 || fdh->elf.forced_local);
50bc7936
AM
6338 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6339
b34976b6 6340 return TRUE;
e86ce104 6341}
40b8271b 6342
a4b6fadd
AM
6343static const struct sfpr_def_parms save_res_funcs[] =
6344 {
6345 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6346 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6347 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6348 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6349 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6350 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6351 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6352 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6353 { "._savef", 14, 31, savefpr, savefpr1_tail },
6354 { "._restf", 14, 31, restfpr, restfpr1_tail },
6355 { "_savevr_", 20, 31, savevr, savevr_tail },
6356 { "_restvr_", 20, 31, restvr, restvr_tail }
6357 };
6358
e86ce104 6359/* Called near the start of bfd_elf_size_dynamic_sections. We use
c9405344
AM
6360 this hook to a) run the edit functions in this file, b) provide
6361 some gcc support functions, and c) transfer dynamic linking
6362 information gathered so far on function code symbol entries, to
6363 their corresponding function descriptor symbol entries. */
deb0e272 6364
b34976b6 6365static bfd_boolean
c9405344 6366ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
e86ce104
AM
6367{
6368 struct ppc_link_hash_table *htab;
6369
6370 htab = ppc_hash_table (info);
4dfe6ac6
NC
6371 if (htab == NULL)
6372 return FALSE;
6373
c9405344
AM
6374 /* Call back into the linker, which then runs the edit functions. */
6375 htab->params->edit ();
6376
b32547cd
AM
6377 /* Provide any missing _save* and _rest* functions. */
6378 if (htab->sfpr != NULL)
6379 {
6380 unsigned int i;
6381
6382 htab->sfpr->size = 0;
6383 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6384 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6385 return FALSE;
6386 if (htab->sfpr->size == 0)
6387 htab->sfpr->flags |= SEC_EXCLUDE;
6388 }
6389
6390 if (bfd_link_relocatable (info))
6391 return TRUE;
6392
6393 if (htab->elf.hgot != NULL)
dba6fa9b
AM
6394 {
6395 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6396 /* Make .TOC. defined so as to prevent it being made dynamic.
6397 The wrong value here is fixed later in ppc64_elf_set_toc. */
43417696
AM
6398 if (!htab->elf.hgot->def_regular
6399 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6400 {
6401 htab->elf.hgot->root.type = bfd_link_hash_defined;
6402 htab->elf.hgot->root.u.def.value = 0;
6403 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6404 htab->elf.hgot->def_regular = 1;
6405 htab->elf.hgot->root.linker_def = 1;
6406 }
dba6fa9b 6407 htab->elf.hgot->type = STT_OBJECT;
2cdcc330
AM
6408 htab->elf.hgot->other
6409 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
dba6fa9b 6410 }
c66bb0ee 6411
8c5b4e52
AM
6412 if (htab->need_func_desc_adj)
6413 {
6414 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6415 htab->need_func_desc_adj = 0;
6416 }
805fc799 6417
b34976b6 6418 return TRUE;
e86ce104
AM
6419}
6420
d311bc8b 6421/* Return true if we have dynamic relocs against H or any of its weak
ab2477e1
AM
6422 aliases, that apply to read-only sections. Cannot be used after
6423 size_dynamic_sections. */
d311bc8b
AM
6424
6425static bfd_boolean
6426alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6427{
ed7007c1 6428 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
d311bc8b
AM
6429 do
6430 {
5dbc8b37 6431 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
d311bc8b 6432 return TRUE;
ed7007c1 6433 eh = ppc_elf_hash_entry (eh->elf.u.alias);
2cdcc330
AM
6434 }
6435 while (eh != NULL && &eh->elf != h);
d311bc8b
AM
6436
6437 return FALSE;
6438}
8a2058b5 6439
8a9e8e72
AM
6440/* Return whether EH has pc-relative dynamic relocs. */
6441
6442static bfd_boolean
6443pc_dynrelocs (struct ppc_link_hash_entry *eh)
6444{
6445 struct elf_dyn_relocs *p;
6446
190eb1dd 6447 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
8a9e8e72
AM
6448 if (p->pc_count != 0)
6449 return TRUE;
6450 return FALSE;
6451}
6452
8a2058b5
AM
6453/* Return true if a global entry stub will be created for H. Valid
6454 for ELFv2 before plt entries have been allocated. */
6455
6456static bfd_boolean
6457global_entry_stub (struct elf_link_hash_entry *h)
6458{
6459 struct plt_entry *pent;
6460
6461 if (!h->pointer_equality_needed
6462 || h->def_regular)
6463 return FALSE;
6464
6465 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6466 if (pent->plt.refcount > 0
6467 && pent->addend == 0)
6468 return TRUE;
6469
6470 return FALSE;
6471}
6472
e86ce104
AM
6473/* Adjust a symbol defined by a dynamic object and referenced by a
6474 regular object. The current definition is in some section of the
6475 dynamic object, but we're not including those sections. We have to
6476 change the definition to something the rest of the link can
6477 understand. */
6478
b34976b6 6479static bfd_boolean
4ce794b7
AM
6480ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6481 struct elf_link_hash_entry *h)
e86ce104
AM
6482{
6483 struct ppc_link_hash_table *htab;
5474d94f 6484 asection *s, *srel;
e86ce104
AM
6485
6486 htab = ppc_hash_table (info);
4dfe6ac6
NC
6487 if (htab == NULL)
6488 return FALSE;
e86ce104
AM
6489
6490 /* Deal with function syms. */
6491 if (h->type == STT_FUNC
e054468f 6492 || h->type == STT_GNU_IFUNC
f5385ebf 6493 || h->needs_plt)
e86ce104 6494 {
ed7007c1 6495 bfd_boolean local = (ppc_elf_hash_entry (h)->save_res
529fe20e
AM
6496 || SYMBOL_CALLS_LOCAL (info, h)
6497 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6498 /* Discard dyn_relocs when non-pic if we've decided that a
6499 function symbol is local and not an ifunc. We keep dynamic
6500 relocs for ifuncs when local rather than always emitting a
6501 plt call stub for them and defining the symbol on the call
6502 stub. We can't do that for ELFv1 anyway (a function symbol
6503 is defined on a descriptor, not code) and it can be faster at
6504 run-time due to not needing to bounce through a stub. The
6505 dyn_relocs for ifuncs will be applied even in a static
6506 executable. */
6507 if (!bfd_link_pic (info)
6508 && h->type != STT_GNU_IFUNC
6509 && local)
190eb1dd 6510 h->dyn_relocs = NULL;
529fe20e 6511
e86ce104
AM
6512 /* Clear procedure linkage table information for any symbol that
6513 won't need a .plt entry. */
411e1bfb
AM
6514 struct plt_entry *ent;
6515 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6516 if (ent->plt.refcount > 0)
6517 break;
8387904d 6518 if (ent == NULL
2d7ad24e
AM
6519 || (h->type != STT_GNU_IFUNC
6520 && local
3e04d765 6521 && (htab->can_convert_all_inline_plt
ed7007c1 6522 || (ppc_elf_hash_entry (h)->tls_mask
3e04d765 6523 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
40b8271b 6524 {
411e1bfb 6525 h->plt.plist = NULL;
f5385ebf 6526 h->needs_plt = 0;
d1eca1e4 6527 h->pointer_equality_needed = 0;
40b8271b 6528 }
8a2058b5 6529 else if (abiversion (info->output_bfd) >= 2)
a345bc8d 6530 {
d1eca1e4
AM
6531 /* Taking a function's address in a read/write section
6532 doesn't require us to define the function symbol in the
6533 executable on a global entry stub. A dynamic reloc can
8a2058b5
AM
6534 be used instead. The reason we prefer a few more dynamic
6535 relocs is that calling via a global entry stub costs a
6536 few more instructions, and pointer_equality_needed causes
6537 extra work in ld.so when resolving these symbols. */
529fe20e 6538 if (global_entry_stub (h))
d1eca1e4 6539 {
5dbc8b37 6540 if (!_bfd_elf_readonly_dynrelocs (h))
529fe20e
AM
6541 {
6542 h->pointer_equality_needed = 0;
04383fd1
AM
6543 /* If we haven't seen a branch reloc and the symbol
6544 isn't an ifunc then we don't need a plt entry. */
529fe20e
AM
6545 if (!h->needs_plt)
6546 h->plt.plist = NULL;
6547 }
6548 else if (!bfd_link_pic (info))
6549 /* We are going to be defining the function symbol on the
6550 plt stub, so no dyn_relocs needed when non-pic. */
190eb1dd 6551 h->dyn_relocs = NULL;
d1eca1e4
AM
6552 }
6553
3988aed5
AM
6554 /* ELFv2 function symbols can't have copy relocs. */
6555 return TRUE;
6556 }
6557 else if (!h->needs_plt
5dbc8b37 6558 && !_bfd_elf_readonly_dynrelocs (h))
3988aed5 6559 {
04383fd1
AM
6560 /* If we haven't seen a branch reloc and the symbol isn't an
6561 ifunc then we don't need a plt entry. */
3988aed5
AM
6562 h->plt.plist = NULL;
6563 h->pointer_equality_needed = 0;
a345bc8d
AM
6564 return TRUE;
6565 }
5bd4f169 6566 }
bbd7ec4a 6567 else
411e1bfb 6568 h->plt.plist = NULL;
5bd4f169
AM
6569
6570 /* If this is a weak symbol, and there is a real definition, the
6571 processor independent code will have arranged for us to see the
6572 real definition first, and we can just use the same value. */
60d67dc8 6573 if (h->is_weakalias)
5bd4f169 6574 {
60d67dc8
AM
6575 struct elf_link_hash_entry *def = weakdef (h);
6576 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6577 h->root.u.def.section = def->root.u.def.section;
6578 h->root.u.def.value = def->root.u.def.value;
4a7e5234
AM
6579 if (def->root.u.def.section == htab->elf.sdynbss
6580 || def->root.u.def.section == htab->elf.sdynrelro)
190eb1dd 6581 h->dyn_relocs = NULL;
b34976b6 6582 return TRUE;
5bd4f169
AM
6583 }
6584
5bd4f169
AM
6585 /* If we are creating a shared library, we must presume that the
6586 only references to the symbol are via the global offset table.
6587 For such cases we need not do anything here; the relocations will
6588 be handled correctly by relocate_section. */
ec73ddcd 6589 if (!bfd_link_executable (info))
b34976b6 6590 return TRUE;
5bd4f169 6591
65f38f15
AM
6592 /* If there are no references to this symbol that do not use the
6593 GOT, we don't need to generate a copy reloc. */
f5385ebf 6594 if (!h->non_got_ref)
b34976b6 6595 return TRUE;
65f38f15 6596
b186458a 6597 /* Don't generate a copy reloc for symbols defined in the executable. */
d93d1c80 6598 if (!h->def_dynamic || !h->ref_regular || h->def_regular
b186458a 6599
d93d1c80
AM
6600 /* If -z nocopyreloc was given, don't generate them either. */
6601 || info->nocopyreloc
a127494f 6602
dce2246a 6603 /* If we don't find any dynamic relocs in read-only sections, then
d93d1c80 6604 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1bdd8fac
AM
6605 || (ELIMINATE_COPY_RELOCS
6606 && !h->needs_copy
6607 && !alias_readonly_dynrelocs (h))
65f38f15 6608
d93d1c80
AM
6609 /* Protected variables do not work with .dynbss. The copy in
6610 .dynbss won't be used by the shared library with the protected
6611 definition for the variable. Text relocations are preferable
6612 to an incorrect program. */
6613 || h->protected_def)
529fe20e 6614 return TRUE;
a127494f 6615
e1c6cf61
AM
6616 if (h->type == STT_FUNC
6617 || h->type == STT_GNU_IFUNC)
97b639ba 6618 {
e1c6cf61
AM
6619 /* .dynbss copies of function symbols only work if we have
6620 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6621 use dot-symbols and set the function symbol size to the text
6622 size of the function rather than the size of the descriptor.
6623 That's wrong for copying a descriptor. */
ed7007c1 6624 if (ppc_elf_hash_entry (h)->oh == NULL
e1c6cf61
AM
6625 || !(h->size == 24 || h->size == 16))
6626 return TRUE;
6627
6628 /* We should never get here, but unfortunately there are old
6629 versions of gcc (circa gcc-3.2) that improperly for the
6630 ELFv1 ABI put initialized function pointers, vtable refs and
6631 suchlike in read-only sections. Allow them to proceed, but
6632 warn that this might break at runtime. */
25f53a85 6633 info->callbacks->einfo
c1c8c1ef 6634 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
25f53a85 6635 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6636 h->root.root.string);
6637 }
5d35169e
AM
6638
6639 /* This is a reference to a symbol defined by a dynamic object which
6640 is not a function. */
6641
5bd4f169
AM
6642 /* We must allocate the symbol in our .dynbss section, which will
6643 become part of the .bss section of the executable. There will be
6644 an entry for this symbol in the .dynsym section. The dynamic
6645 object will contain position independent code, so all references
6646 from the dynamic object to this symbol will go through the global
6647 offset table. The dynamic linker will use the .dynsym entry to
6648 determine the address it must put in the global offset table, so
6649 both the dynamic object and the regular object will refer to the
6650 same memory location for the variable. */
5474d94f
AM
6651 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6652 {
6653 s = htab->elf.sdynrelro;
6654 srel = htab->elf.sreldynrelro;
6655 }
6656 else
6657 {
6658 s = htab->elf.sdynbss;
6659 srel = htab->elf.srelbss;
6660 }
1d7e9d18 6661 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6662 {
4a7e5234
AM
6663 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6664 linker to copy the initial value out of the dynamic object
6665 and into the runtime process image. */
5474d94f 6666 srel->size += sizeof (Elf64_External_Rela);
f5385ebf 6667 h->needs_copy = 1;
5bd4f169
AM
6668 }
6669
529fe20e 6670 /* We no longer want dyn_relocs. */
190eb1dd 6671 h->dyn_relocs = NULL;
6cabe1ea 6672 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5bd4f169
AM
6673}
6674
e86ce104
AM
6675/* If given a function descriptor symbol, hide both the function code
6676 sym and the descriptor. */
6677static void
4ce794b7
AM
6678ppc64_elf_hide_symbol (struct bfd_link_info *info,
6679 struct elf_link_hash_entry *h,
6680 bfd_boolean force_local)
e86ce104 6681{
34814b9f 6682 struct ppc_link_hash_entry *eh;
e86ce104
AM
6683 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6684
87469ba2
AM
6685 if (ppc_hash_table (info) == NULL)
6686 return;
6687
ed7007c1 6688 eh = ppc_elf_hash_entry (h);
34814b9f 6689 if (eh->is_func_descriptor)
e86ce104 6690 {
34814b9f 6691 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6692
721956f4 6693 if (fh == NULL)
d1329ca3
AM
6694 {
6695 const char *p, *q;
b8ac2841 6696 struct elf_link_hash_table *htab = elf_hash_table (info);
d1329ca3
AM
6697 char save;
6698
6699 /* We aren't supposed to use alloca in BFD because on
6700 systems which do not have alloca the version in libiberty
6701 calls xmalloc, which might cause the program to crash
6702 when it runs out of memory. This function doesn't have a
6703 return status, so there's no way to gracefully return an
6704 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6705 accessed; It's either a string in an ELF string table,
6706 or allocated in an objalloc structure. */
d1329ca3 6707
34814b9f 6708 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6709 save = *p;
6710 *(char *) p = '.';
ed7007c1
AM
6711 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6712 FALSE, FALSE));
d1329ca3
AM
6713 *(char *) p = save;
6714
6715 /* Unfortunately, if it so happens that the string we were
6716 looking for was allocated immediately before this string,
6717 then we overwrote the string terminator. That's the only
6718 reason the lookup should fail. */
6719 if (fh == NULL)
6720 {
34814b9f
AM
6721 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6722 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6723 --q, --p;
34814b9f 6724 if (q < eh->elf.root.root.string && *p == '.')
ed7007c1
AM
6725 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
6726 FALSE, FALSE));
d1329ca3
AM
6727 }
6728 if (fh != NULL)
6729 {
34814b9f
AM
6730 eh->oh = fh;
6731 fh->oh = eh;
d1329ca3
AM
6732 }
6733 }
e86ce104 6734 if (fh != NULL)
34814b9f 6735 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6736 }
6737}
6738
411e1bfb 6739static bfd_boolean
8843416a
AM
6740get_sym_h (struct elf_link_hash_entry **hp,
6741 Elf_Internal_Sym **symp,
6742 asection **symsecp,
f961d9dd 6743 unsigned char **tls_maskp,
8843416a
AM
6744 Elf_Internal_Sym **locsymsp,
6745 unsigned long r_symndx,
6746 bfd *ibfd)
411e1bfb 6747{
0ffa91dd 6748 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6749
6750 if (r_symndx >= symtab_hdr->sh_info)
6751 {
6752 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6753 struct elf_link_hash_entry *h;
6754
6755 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6756 h = elf_follow_link (h);
411e1bfb
AM
6757
6758 if (hp != NULL)
6759 *hp = h;
6760
6761 if (symp != NULL)
6762 *symp = NULL;
6763
6764 if (symsecp != NULL)
6765 {
6766 asection *symsec = NULL;
6767 if (h->root.type == bfd_link_hash_defined
6768 || h->root.type == bfd_link_hash_defweak)
6769 symsec = h->root.u.def.section;
6770 *symsecp = symsec;
6771 }
6772
e7b938ca 6773 if (tls_maskp != NULL)
ed7007c1 6774 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
411e1bfb
AM
6775 }
6776 else
6777 {
6778 Elf_Internal_Sym *sym;
6779 Elf_Internal_Sym *locsyms = *locsymsp;
6780
6781 if (locsyms == NULL)
6782 {
6783 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6784 if (locsyms == NULL)
6785 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6786 symtab_hdr->sh_info,
6787 0, NULL, NULL, NULL);
6788 if (locsyms == NULL)
6789 return FALSE;
6790 *locsymsp = locsyms;
6791 }
6792 sym = locsyms + r_symndx;
6793
6794 if (hp != NULL)
6795 *hp = NULL;
6796
6797 if (symp != NULL)
6798 *symp = sym;
6799
6800 if (symsecp != NULL)
cb33740c 6801 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6802
e7b938ca 6803 if (tls_maskp != NULL)
411e1bfb
AM
6804 {
6805 struct got_entry **lgot_ents;
f961d9dd 6806 unsigned char *tls_mask;
411e1bfb 6807
e7b938ca 6808 tls_mask = NULL;
411e1bfb
AM
6809 lgot_ents = elf_local_got_ents (ibfd);
6810 if (lgot_ents != NULL)
6811 {
e054468f
AM
6812 struct plt_entry **local_plt = (struct plt_entry **)
6813 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6814 unsigned char *lgot_masks = (unsigned char *)
e054468f 6815 (local_plt + symtab_hdr->sh_info);
e7b938ca 6816 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6817 }
e7b938ca 6818 *tls_maskp = tls_mask;
411e1bfb
AM
6819 }
6820 }
6821 return TRUE;
6822}
6823
e7b938ca 6824/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6825 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6826 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6827
6828static int
f961d9dd 6829get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6830 unsigned long *toc_symndx,
6831 bfd_vma *toc_addend,
0d4792f7 6832 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6833 const Elf_Internal_Rela *rel,
6834 bfd *ibfd)
411e1bfb
AM
6835{
6836 unsigned long r_symndx;
0d4792f7 6837 int next_r;
411e1bfb
AM
6838 struct elf_link_hash_entry *h;
6839 Elf_Internal_Sym *sym;
6840 asection *sec;
6841 bfd_vma off;
6842
6843 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6844 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6845 return 0;
411e1bfb 6846
37da22e5
AM
6847 if ((*tls_maskp != NULL
6848 && (**tls_maskp & TLS_TLS) != 0
6849 && **tls_maskp != (TLS_TLS | TLS_MARK))
411e1bfb 6850 || sec == NULL
6bee8834 6851 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6852 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6853 return 1;
411e1bfb
AM
6854
6855 /* Look inside a TOC section too. */
6856 if (h != NULL)
6857 {
6858 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6859 off = h->root.u.def.value;
6860 }
6861 else
6862 off = sym->st_value;
6863 off += rel->r_addend;
6864 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6865 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6866 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6867 if (toc_symndx != NULL)
6868 *toc_symndx = r_symndx;
3a71aa26
AM
6869 if (toc_addend != NULL)
6870 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6871 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6872 return 0;
854b41e7 6873 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6874 && (next_r == -1 || next_r == -2))
6875 return 1 - next_r;
951fd09b 6876 return 1;
411e1bfb
AM
6877}
6878
3b421ab3
AM
6879/* Find (or create) an entry in the tocsave hash table. */
6880
6881static struct tocsave_entry *
6882tocsave_find (struct ppc_link_hash_table *htab,
6883 enum insert_option insert,
6884 Elf_Internal_Sym **local_syms,
6885 const Elf_Internal_Rela *irela,
6886 bfd *ibfd)
6887{
6888 unsigned long r_indx;
6889 struct elf_link_hash_entry *h;
6890 Elf_Internal_Sym *sym;
6891 struct tocsave_entry ent, *p;
6892 hashval_t hash;
6893 struct tocsave_entry **slot;
6894
6895 r_indx = ELF64_R_SYM (irela->r_info);
6896 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6897 return NULL;
6898 if (ent.sec == NULL || ent.sec->output_section == NULL)
6899 {
4eca0228 6900 _bfd_error_handler
871b3ab2 6901 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
3b421ab3
AM
6902 return NULL;
6903 }
6904
6905 if (h != NULL)
6906 ent.offset = h->root.u.def.value;
6907 else
6908 ent.offset = sym->st_value;
6909 ent.offset += irela->r_addend;
6910
6911 hash = tocsave_htab_hash (&ent);
6912 slot = ((struct tocsave_entry **)
6913 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6914 if (slot == NULL)
6915 return NULL;
6916
6917 if (*slot == NULL)
6918 {
6919 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6920 if (p == NULL)
6921 return NULL;
6922 *p = ent;
6923 *slot = p;
6924 }
6925 return *slot;
6926}
6927
754021d0 6928/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6929 code for the old ABI, these will already have been done. */
754021d0
AM
6930
6931static bfd_boolean
6932adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6933{
6934 struct ppc_link_hash_entry *eh;
6935 asection *sym_sec;
74f0fb50 6936 struct _opd_sec_data *opd;
754021d0
AM
6937
6938 if (h->root.type == bfd_link_hash_indirect)
6939 return TRUE;
6940
754021d0
AM
6941 if (h->root.type != bfd_link_hash_defined
6942 && h->root.type != bfd_link_hash_defweak)
6943 return TRUE;
6944
ed7007c1 6945 eh = ppc_elf_hash_entry (h);
754021d0
AM
6946 if (eh->adjust_done)
6947 return TRUE;
6948
6949 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6950 opd = get_opd_info (sym_sec);
6951 if (opd != NULL && opd->adjust != NULL)
754021d0 6952 {
51aecdc5 6953 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
4025353c
AM
6954 if (adjust == -1)
6955 {
6956 /* This entry has been deleted. */
b3fac117 6957 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6958 if (dsec == NULL)
6959 {
6960 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 6961 if (discarded_section (dsec))
81688140 6962 {
b3fac117 6963 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6964 break;
6965 }
6966 }
4025353c 6967 eh->elf.root.u.def.value = 0;
81688140 6968 eh->elf.root.u.def.section = dsec;
4025353c
AM
6969 }
6970 else
6971 eh->elf.root.u.def.value += adjust;
754021d0
AM
6972 eh->adjust_done = 1;
6973 }
6974 return TRUE;
6975}
6976
8c1d1bb8 6977/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 6978 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
6979 have already been determined. */
6980
6981static bfd_boolean
6982dec_dynrel_count (bfd_vma r_info,
6983 asection *sec,
6984 struct bfd_link_info *info,
6985 Elf_Internal_Sym **local_syms,
6986 struct elf_link_hash_entry *h,
19e08130 6987 Elf_Internal_Sym *sym)
8c1d1bb8
AM
6988{
6989 enum elf_ppc64_reloc_type r_type;
19e08130 6990 asection *sym_sec = NULL;
8c1d1bb8
AM
6991
6992 /* Can this reloc be dynamic? This switch, and later tests here
6993 should be kept in sync with the code in check_relocs. */
6994 r_type = ELF64_R_TYPE (r_info);
6995 switch (r_type)
6996 {
6997 default:
6998 return TRUE;
6999
1bdd8fac
AM
7000 case R_PPC64_TOC16:
7001 case R_PPC64_TOC16_DS:
7002 case R_PPC64_TOC16_LO:
7003 case R_PPC64_TOC16_HI:
7004 case R_PPC64_TOC16_HA:
7005 case R_PPC64_TOC16_LO_DS:
7006 if (h == NULL)
7007 return TRUE;
7008 break;
7009
8c1d1bb8
AM
7010 case R_PPC64_TPREL16:
7011 case R_PPC64_TPREL16_LO:
7012 case R_PPC64_TPREL16_HI:
7013 case R_PPC64_TPREL16_HA:
7014 case R_PPC64_TPREL16_DS:
7015 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7016 case R_PPC64_TPREL16_HIGH:
7017 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7018 case R_PPC64_TPREL16_HIGHER:
7019 case R_PPC64_TPREL16_HIGHERA:
7020 case R_PPC64_TPREL16_HIGHEST:
7021 case R_PPC64_TPREL16_HIGHESTA:
8c1d1bb8 7022 case R_PPC64_TPREL64:
c213164a 7023 case R_PPC64_TPREL34:
8c1d1bb8
AM
7024 case R_PPC64_DTPMOD64:
7025 case R_PPC64_DTPREL64:
7026 case R_PPC64_ADDR64:
7027 case R_PPC64_REL30:
7028 case R_PPC64_REL32:
7029 case R_PPC64_REL64:
7030 case R_PPC64_ADDR14:
7031 case R_PPC64_ADDR14_BRNTAKEN:
7032 case R_PPC64_ADDR14_BRTAKEN:
7033 case R_PPC64_ADDR16:
7034 case R_PPC64_ADDR16_DS:
7035 case R_PPC64_ADDR16_HA:
7036 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7037 case R_PPC64_ADDR16_HIGH:
7038 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7039 case R_PPC64_ADDR16_HIGHER:
7040 case R_PPC64_ADDR16_HIGHERA:
7041 case R_PPC64_ADDR16_HIGHEST:
7042 case R_PPC64_ADDR16_HIGHESTA:
7043 case R_PPC64_ADDR16_LO:
7044 case R_PPC64_ADDR16_LO_DS:
7045 case R_PPC64_ADDR24:
7046 case R_PPC64_ADDR32:
7047 case R_PPC64_UADDR16:
7048 case R_PPC64_UADDR32:
7049 case R_PPC64_UADDR64:
7050 case R_PPC64_TOC:
5663e321
AM
7051 case R_PPC64_D34:
7052 case R_PPC64_D34_LO:
7053 case R_PPC64_D34_HI30:
7054 case R_PPC64_D34_HA30:
7055 case R_PPC64_ADDR16_HIGHER34:
7056 case R_PPC64_ADDR16_HIGHERA34:
7057 case R_PPC64_ADDR16_HIGHEST34:
7058 case R_PPC64_ADDR16_HIGHESTA34:
7059 case R_PPC64_D28:
8c1d1bb8
AM
7060 break;
7061 }
7062
7063 if (local_syms != NULL)
7064 {
7065 unsigned long r_symndx;
8c1d1bb8
AM
7066 bfd *ibfd = sec->owner;
7067
7068 r_symndx = ELF64_R_SYM (r_info);
7069 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7070 return FALSE;
7071 }
7072
ec73ddcd
AM
7073 if ((h != NULL
7074 && (h->root.type == bfd_link_hash_defweak
7075 || !h->def_regular))
7076 || (h != NULL
7077 && !bfd_link_executable (info)
7078 && !SYMBOLIC_BIND (info, h))
7079 || (bfd_link_pic (info)
7080 && must_be_dyn_reloc (info, r_type))
7081 || (!bfd_link_pic (info)
7082 && (h != NULL
7083 ? h->type == STT_GNU_IFUNC
7084 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
8c1d1bb8
AM
7085 ;
7086 else
7087 return TRUE;
7088
7089 if (h != NULL)
6edfbbad 7090 {
19e08130
AM
7091 struct elf_dyn_relocs *p;
7092 struct elf_dyn_relocs **pp;
190eb1dd 7093 pp = &h->dyn_relocs;
19e08130
AM
7094
7095 /* elf_gc_sweep may have already removed all dyn relocs associated
7096 with local syms for a given section. Also, symbol flags are
7097 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7098 report a dynreloc miscount. */
7099 if (*pp == NULL && info->gc_sections)
7100 return TRUE;
7101
7102 while ((p = *pp) != NULL)
60124e18 7103 {
19e08130
AM
7104 if (p->sec == sec)
7105 {
7106 if (!must_be_dyn_reloc (info, r_type))
7107 p->pc_count -= 1;
7108 p->count -= 1;
7109 if (p->count == 0)
7110 *pp = p->next;
7111 return TRUE;
7112 }
7113 pp = &p->next;
60124e18 7114 }
6edfbbad 7115 }
19e08130
AM
7116 else
7117 {
7118 struct ppc_dyn_relocs *p;
7119 struct ppc_dyn_relocs **pp;
7120 void *vpp;
7121 bfd_boolean is_ifunc;
8c1d1bb8 7122
19e08130
AM
7123 if (local_syms == NULL)
7124 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7125 if (sym_sec == NULL)
7126 sym_sec = sec;
c57da1a7 7127
19e08130
AM
7128 vpp = &elf_section_data (sym_sec)->local_dynrel;
7129 pp = (struct ppc_dyn_relocs **) vpp;
7130
7131 if (*pp == NULL && info->gc_sections)
7132 return TRUE;
7133
7134 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7135 while ((p = *pp) != NULL)
8c1d1bb8 7136 {
19e08130
AM
7137 if (p->sec == sec && p->ifunc == is_ifunc)
7138 {
7139 p->count -= 1;
7140 if (p->count == 0)
7141 *pp = p->next;
7142 return TRUE;
7143 }
7144 pp = &p->next;
8c1d1bb8 7145 }
8c1d1bb8
AM
7146 }
7147
695344c0 7148 /* xgettext:c-format */
cf97bcb0
AM
7149 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7150 sec->owner, sec);
8c1d1bb8
AM
7151 bfd_set_error (bfd_error_bad_value);
7152 return FALSE;
7153}
7154
754021d0
AM
7155/* Remove unused Official Procedure Descriptor entries. Currently we
7156 only remove those associated with functions in discarded link-once
7157 sections, or weakly defined functions that have been overridden. It
7158 would be possible to remove many more entries for statically linked
7159 applications. */
7160
b34976b6 7161bfd_boolean
e7d1c40c 7162ppc64_elf_edit_opd (struct bfd_link_info *info)
1e2f5b6e
AM
7163{
7164 bfd *ibfd;
754021d0 7165 bfd_boolean some_edited = FALSE;
3f764659 7166 asection *need_pad = NULL;
e7d1c40c
AM
7167 struct ppc_link_hash_table *htab;
7168
7169 htab = ppc_hash_table (info);
7170 if (htab == NULL)
7171 return FALSE;
1e2f5b6e 7172
c72f2fb2 7173 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1e2f5b6e
AM
7174 {
7175 asection *sec;
7176 Elf_Internal_Rela *relstart, *rel, *relend;
7177 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7178 Elf_Internal_Sym *local_syms;
74f0fb50 7179 struct _opd_sec_data *opd;
51aecdc5 7180 bfd_boolean need_edit, add_aux_fields, broken;
3f764659 7181 bfd_size_type cnt_16b = 0;
1e2f5b6e 7182
854b41e7
AM
7183 if (!is_ppc64_elf (ibfd))
7184 continue;
7185
1e2f5b6e 7186 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7187 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7188 continue;
7189
dbaa2011 7190 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7191 continue;
7192
1e2f5b6e
AM
7193 if (sec->output_section == bfd_abs_section_ptr)
7194 continue;
7195
7196 /* Look through the section relocs. */
7197 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7198 continue;
7199
6cdc0ccc 7200 local_syms = NULL;
0ffa91dd 7201 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7202
7203 /* Read the relocations. */
4ce794b7 7204 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7205 info->keep_memory);
1e2f5b6e 7206 if (relstart == NULL)
b34976b6 7207 return FALSE;
1e2f5b6e
AM
7208
7209 /* First run through the relocs to check they are sane, and to
7210 determine whether we need to edit this opd section. */
b34976b6 7211 need_edit = FALSE;
51aecdc5 7212 broken = FALSE;
3f764659 7213 need_pad = sec;
1e2f5b6e 7214 relend = relstart + sec->reloc_count;
50bc7936 7215 for (rel = relstart; rel < relend; )
1e2f5b6e 7216 {
04c9666a 7217 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7218 unsigned long r_symndx;
7219 asection *sym_sec;
7220 struct elf_link_hash_entry *h;
7221 Elf_Internal_Sym *sym;
51aecdc5 7222 bfd_vma offset;
1e2f5b6e 7223
51aecdc5 7224 /* .opd contains an array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7225 only interested in the reloc pointing to a function entry
7226 point. */
51aecdc5
AM
7227 offset = rel->r_offset;
7228 if (rel + 1 == relend
7229 || rel[1].r_offset != offset + 8)
1e2f5b6e
AM
7230 {
7231 /* If someone messes with .opd alignment then after a
7232 "ld -r" we might have padding in the middle of .opd.
7233 Also, there's nothing to prevent someone putting
7234 something silly in .opd with the assembler. No .opd
b34976b6 7235 optimization for them! */
3f764659 7236 broken_opd:
4eca0228 7237 _bfd_error_handler
871b3ab2 7238 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
51aecdc5 7239 broken = TRUE;
1e2f5b6e
AM
7240 break;
7241 }
7242
50bc7936
AM
7243 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7244 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7245 {
4eca0228 7246 _bfd_error_handler
695344c0 7247 /* xgettext:c-format */
871b3ab2 7248 (_("%pB: unexpected reloc type %u in .opd section"),
d003868e 7249 ibfd, r_type);
51aecdc5 7250 broken = TRUE;
50bc7936
AM
7251 break;
7252 }
7253
1e2f5b6e 7254 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7255 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7256 r_symndx, ibfd))
50bc7936 7257 goto error_ret;
1e2f5b6e
AM
7258
7259 if (sym_sec == NULL || sym_sec->owner == NULL)
7260 {
411e1bfb
AM
7261 const char *sym_name;
7262 if (h != NULL)
7263 sym_name = h->root.root.string;
7264 else
26c61ae5
L
7265 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7266 sym_sec);
411e1bfb 7267
4eca0228 7268 _bfd_error_handler
695344c0 7269 /* xgettext:c-format */
871b3ab2 7270 (_("%pB: undefined sym `%s' in .opd section"),
d003868e 7271 ibfd, sym_name);
51aecdc5 7272 broken = TRUE;
1e2f5b6e
AM
7273 break;
7274 }
7275
51020317
AM
7276 /* opd entries are always for functions defined in the
7277 current input bfd. If the symbol isn't defined in the
7278 input bfd, then we won't be using the function in this
7279 bfd; It must be defined in a linkonce section in another
7280 bfd, or is weak. It's also possible that we are
7281 discarding the function due to a linker script /DISCARD/,
7282 which we test for via the output_section. */
7283 if (sym_sec->owner != ibfd
7284 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7285 need_edit = TRUE;
1e2f5b6e 7286
50bc7936 7287 rel += 2;
51aecdc5
AM
7288 if (rel + 1 == relend
7289 || (rel + 2 < relend
7290 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7291 ++rel;
7292
7293 if (rel == relend)
3f764659
JJ
7294 {
7295 if (sec->size == offset + 24)
7296 {
7297 need_pad = NULL;
7298 break;
7299 }
51aecdc5 7300 if (sec->size == offset + 16)
3f764659
JJ
7301 {
7302 cnt_16b++;
7303 break;
7304 }
7305 goto broken_opd;
7306 }
3f764659
JJ
7307 else if (rel + 1 < relend
7308 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7309 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7310 {
51aecdc5
AM
7311 if (rel[0].r_offset == offset + 16)
7312 cnt_16b++;
7313 else if (rel[0].r_offset != offset + 24)
7314 goto broken_opd;
3f764659
JJ
7315 }
7316 else
7317 goto broken_opd;
1e2f5b6e
AM
7318 }
7319
e7d1c40c 7320 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
3f764659 7321
51aecdc5 7322 if (!broken && (need_edit || add_aux_fields))
1e2f5b6e
AM
7323 {
7324 Elf_Internal_Rela *write_rel;
d4730f92 7325 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7326 bfd_byte *rptr, *wptr;
983bddc8 7327 bfd_byte *new_contents;
74f0fb50
AM
7328 bfd_size_type amt;
7329
983bddc8 7330 new_contents = NULL;
51aecdc5 7331 amt = OPD_NDX (sec->size) * sizeof (long);
74f0fb50 7332 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7333 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7334 if (opd->adjust == NULL)
7335 return FALSE;
1e2f5b6e
AM
7336
7337 /* This seems a waste of time as input .opd sections are all
7338 zeros as generated by gcc, but I suppose there's no reason
7339 this will always be so. We might start putting something in
7340 the third word of .opd entries. */
7341 if ((sec->flags & SEC_IN_MEMORY) == 0)
7342 {
eea6121a
AM
7343 bfd_byte *loc;
7344 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7345 {
c9594989 7346 free (loc);
50bc7936 7347 error_ret:
c9594989 7348 if (symtab_hdr->contents != (unsigned char *) local_syms)
6cdc0ccc 7349 free (local_syms);
6cdc0ccc
AM
7350 if (elf_section_data (sec)->relocs != relstart)
7351 free (relstart);
b34976b6 7352 return FALSE;
6cdc0ccc 7353 }
1e2f5b6e
AM
7354 sec->contents = loc;
7355 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7356 }
7357
7358 elf_section_data (sec)->relocs = relstart;
7359
3f764659 7360 new_contents = sec->contents;
3f764659
JJ
7361 if (add_aux_fields)
7362 {
7363 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7364 if (new_contents == NULL)
7365 return FALSE;
51aecdc5 7366 need_pad = NULL;
3f764659 7367 }
b4f4e59f
AM
7368 wptr = new_contents;
7369 rptr = sec->contents;
1e2f5b6e 7370 write_rel = relstart;
51aecdc5 7371 for (rel = relstart; rel < relend; )
1e2f5b6e 7372 {
50bc7936
AM
7373 unsigned long r_symndx;
7374 asection *sym_sec;
7375 struct elf_link_hash_entry *h;
51aecdc5 7376 struct ppc_link_hash_entry *fdh = NULL;
50bc7936 7377 Elf_Internal_Sym *sym;
51aecdc5
AM
7378 long opd_ent_size;
7379 Elf_Internal_Rela *next_rel;
7380 bfd_boolean skip;
50bc7936
AM
7381
7382 r_symndx = ELF64_R_SYM (rel->r_info);
7383 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7384 r_symndx, ibfd))
50bc7936
AM
7385 goto error_ret;
7386
51aecdc5
AM
7387 next_rel = rel + 2;
7388 if (next_rel + 1 == relend
7389 || (next_rel + 2 < relend
7390 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7391 ++next_rel;
7392
7393 /* See if the .opd entry is full 24 byte or
7394 16 byte (with fd_aux entry overlapped with next
7395 fd_func). */
7396 opd_ent_size = 24;
7397 if (next_rel == relend)
1e2f5b6e 7398 {
51aecdc5 7399 if (sec->size == rel->r_offset + 16)
3f764659 7400 opd_ent_size = 16;
51aecdc5
AM
7401 }
7402 else if (next_rel->r_offset == rel->r_offset + 16)
7403 opd_ent_size = 16;
3f764659 7404
51aecdc5
AM
7405 if (h != NULL
7406 && h->root.root.string[0] == '.')
7407 {
ed7007c1 7408 fdh = ppc_elf_hash_entry (h)->oh;
8c5b4e52
AM
7409 if (fdh != NULL)
7410 {
7411 fdh = ppc_follow_link (fdh);
7412 if (fdh->elf.root.type != bfd_link_hash_defined
7413 && fdh->elf.root.type != bfd_link_hash_defweak)
7414 fdh = NULL;
7415 }
51aecdc5 7416 }
1e2f5b6e 7417
51aecdc5
AM
7418 skip = (sym_sec->owner != ibfd
7419 || sym_sec->output_section == bfd_abs_section_ptr);
7420 if (skip)
7421 {
7422 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7 7423 {
51aecdc5
AM
7424 /* Arrange for the function descriptor sym
7425 to be dropped. */
7426 fdh->elf.root.u.def.value = 0;
7427 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7428 }
51aecdc5 7429 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
1e2f5b6e 7430
0e1862bb 7431 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
51aecdc5
AM
7432 rel = next_rel;
7433 else
7434 while (1)
7435 {
7436 if (!dec_dynrel_count (rel->r_info, sec, info,
7437 NULL, h, sym))
7438 goto error_ret;
754021d0 7439
51aecdc5
AM
7440 if (++rel == next_rel)
7441 break;
1e2f5b6e 7442
51aecdc5
AM
7443 r_symndx = ELF64_R_SYM (rel->r_info);
7444 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7445 r_symndx, ibfd))
7446 goto error_ret;
7447 }
50bc7936
AM
7448 }
7449 else
1e2f5b6e 7450 {
51aecdc5
AM
7451 /* We'll be keeping this opd entry. */
7452 long adjust;
7453
7454 if (fdh != NULL)
7455 {
7456 /* Redefine the function descriptor symbol to
7457 this location in the opd section. It is
7458 necessary to update the value here rather
7459 than using an array of adjustments as we do
7460 for local symbols, because various places
7461 in the generic ELF code use the value
7462 stored in u.def.value. */
7463 fdh->elf.root.u.def.value = wptr - new_contents;
7464 fdh->adjust_done = 1;
7465 }
7466
7467 /* Local syms are a bit tricky. We could
7468 tweak them as they can be cached, but
7469 we'd need to look through the local syms
7470 for the function descriptor sym which we
7471 don't have at the moment. So keep an
7472 array of adjustments. */
7473 adjust = (wptr - new_contents) - (rptr - sec->contents);
7474 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7475
7476 if (wptr != rptr)
7477 memcpy (wptr, rptr, opd_ent_size);
7478 wptr += opd_ent_size;
7479 if (add_aux_fields && opd_ent_size == 16)
7480 {
7481 memset (wptr, '\0', 8);
7482 wptr += 8;
7483 }
7484
50bc7936 7485 /* We need to adjust any reloc offsets to point to the
51aecdc5
AM
7486 new opd entries. */
7487 for ( ; rel != next_rel; ++rel)
7488 {
7489 rel->r_offset += adjust;
7490 if (write_rel != rel)
7491 memcpy (write_rel, rel, sizeof (*rel));
7492 ++write_rel;
7493 }
1e2f5b6e 7494 }
51aecdc5
AM
7495
7496 rptr += opd_ent_size;
1e2f5b6e
AM
7497 }
7498
3f764659 7499 sec->size = wptr - new_contents;
1e2f5b6e 7500 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7501 if (add_aux_fields)
7502 {
7503 free (sec->contents);
7504 sec->contents = new_contents;
7505 }
7506
05bf9422 7507 /* Fudge the header size too, as this is used later in
cdcf6e38 7508 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7509 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7510 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7511 some_edited = TRUE;
1e2f5b6e 7512 }
6cdc0ccc 7513 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7514 free (relstart);
6cdc0ccc 7515
411e1bfb
AM
7516 if (local_syms != NULL
7517 && symtab_hdr->contents != (unsigned char *) local_syms)
7518 {
7519 if (!info->keep_memory)
7520 free (local_syms);
7521 else
7522 symtab_hdr->contents = (unsigned char *) local_syms;
7523 }
7524 }
7525
754021d0
AM
7526 if (some_edited)
7527 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7528
3f764659
JJ
7529 /* If we are doing a final link and the last .opd entry is just 16 byte
7530 long, add a 8 byte padding after it. */
0e1862bb 7531 if (need_pad != NULL && !bfd_link_relocatable (info))
3f764659
JJ
7532 {
7533 bfd_byte *p;
7534
7535 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7536 {
7537 BFD_ASSERT (need_pad->size > 0);
7538
7539 p = bfd_malloc (need_pad->size + 8);
7540 if (p == NULL)
7541 return FALSE;
699733f6 7542
2cdcc330
AM
7543 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7544 p, 0, need_pad->size))
3f764659
JJ
7545 return FALSE;
7546
7547 need_pad->contents = p;
7548 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7549 }
7550 else
7551 {
7552 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7553 if (p == NULL)
7554 return FALSE;
7555
7556 need_pad->contents = p;
7557 }
7558
7559 memset (need_pad->contents + need_pad->size, 0, 8);
7560 need_pad->size += 8;
7561 }
7562
411e1bfb
AM
7563 return TRUE;
7564}
7565
3e04d765
AM
7566/* Analyze inline PLT call relocations to see whether calls to locally
7567 defined functions can be converted to direct calls. */
7568
7569bfd_boolean
7570ppc64_elf_inline_plt (struct bfd_link_info *info)
7571{
7572 struct ppc_link_hash_table *htab;
7573 bfd *ibfd;
7574 asection *sec;
7575 bfd_vma low_vma, high_vma, limit;
7576
7577 htab = ppc_hash_table (info);
7578 if (htab == NULL)
7579 return FALSE;
7580
7581 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7582 reduced somewhat to cater for possible stubs that might be added
7583 between the call and its destination. */
7584 if (htab->params->group_size < 0)
7585 {
7586 limit = -htab->params->group_size;
7587 if (limit == 1)
7588 limit = 0x1e00000;
7589 }
7590 else
7591 {
7592 limit = htab->params->group_size;
7593 if (limit == 1)
7594 limit = 0x1c00000;
7595 }
7596
7597 low_vma = -1;
7598 high_vma = 0;
7599 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7600 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7601 {
7602 if (low_vma > sec->vma)
7603 low_vma = sec->vma;
7604 if (high_vma < sec->vma + sec->size)
7605 high_vma = sec->vma + sec->size;
7606 }
7607
7608 /* If a "bl" can reach anywhere in local code sections, then we can
7609 convert all inline PLT sequences to direct calls when the symbol
7610 is local. */
7611 if (high_vma - low_vma < limit)
7612 {
7613 htab->can_convert_all_inline_plt = 1;
7614 return TRUE;
7615 }
7616
7617 /* Otherwise, go looking through relocs for cases where a direct
7618 call won't reach. Mark the symbol on any such reloc to disable
7619 the optimization and keep the PLT entry as it seems likely that
7620 this will be better than creating trampolines. Note that this
7621 will disable the optimization for all inline PLT calls to a
7622 particular symbol, not just those that won't reach. The
7623 difficulty in doing a more precise optimization is that the
7624 linker needs to make a decision depending on whether a
7625 particular R_PPC64_PLTCALL insn can be turned into a direct
7626 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7627 the sequence, and there is nothing that ties those relocs
7628 together except their symbol. */
7629
7630 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7631 {
7632 Elf_Internal_Shdr *symtab_hdr;
7633 Elf_Internal_Sym *local_syms;
7634
7635 if (!is_ppc64_elf (ibfd))
7636 continue;
7637
7638 local_syms = NULL;
7639 symtab_hdr = &elf_symtab_hdr (ibfd);
7640
7641 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7642 if (ppc64_elf_section_data (sec)->has_pltcall
7643 && !bfd_is_abs_section (sec->output_section))
7644 {
7645 Elf_Internal_Rela *relstart, *rel, *relend;
7646
7647 /* Read the relocations. */
7648 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7649 info->keep_memory);
7650 if (relstart == NULL)
7651 return FALSE;
7652
7653 relend = relstart + sec->reloc_count;
435edf0b 7654 for (rel = relstart; rel < relend; rel++)
3e04d765
AM
7655 {
7656 enum elf_ppc64_reloc_type r_type;
7657 unsigned long r_symndx;
7658 asection *sym_sec;
7659 struct elf_link_hash_entry *h;
7660 Elf_Internal_Sym *sym;
7661 unsigned char *tls_maskp;
7662
7663 r_type = ELF64_R_TYPE (rel->r_info);
5663e321
AM
7664 if (r_type != R_PPC64_PLTCALL
7665 && r_type != R_PPC64_PLTCALL_NOTOC)
3e04d765
AM
7666 continue;
7667
7668 r_symndx = ELF64_R_SYM (rel->r_info);
7669 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7670 r_symndx, ibfd))
7671 {
7672 if (elf_section_data (sec)->relocs != relstart)
7673 free (relstart);
c9594989 7674 if (symtab_hdr->contents != (bfd_byte *) local_syms)
3e04d765
AM
7675 free (local_syms);
7676 return FALSE;
7677 }
7678
7679 if (sym_sec != NULL && sym_sec->output_section != NULL)
7680 {
7681 bfd_vma from, to;
7682 if (h != NULL)
7683 to = h->root.u.def.value;
7684 else
7685 to = sym->st_value;
7686 to += (rel->r_addend
7687 + sym_sec->output_offset
7688 + sym_sec->output_section->vma);
7689 from = (rel->r_offset
7690 + sec->output_offset
7691 + sec->output_section->vma);
5663e321
AM
7692 if (to - from + limit < 2 * limit
7693 && !(r_type == R_PPC64_PLTCALL_NOTOC
7694 && (((h ? h->other : sym->st_other)
7695 & STO_PPC64_LOCAL_MASK)
4a4e7361 7696 > 1 << STO_PPC64_LOCAL_BIT)))
3e04d765
AM
7697 *tls_maskp &= ~PLT_KEEP;
7698 }
7699 }
7700 if (elf_section_data (sec)->relocs != relstart)
7701 free (relstart);
7702 }
7703
7704 if (local_syms != NULL
7705 && symtab_hdr->contents != (unsigned char *) local_syms)
7706 {
7707 if (!info->keep_memory)
7708 free (local_syms);
7709 else
7710 symtab_hdr->contents = (unsigned char *) local_syms;
7711 }
7712 }
7713
7714 return TRUE;
7715}
7716
c9405344
AM
7717/* Set htab->tls_get_addr and various other info specific to TLS.
7718 This needs to run before dynamic symbols are processed in
7719 bfd_elf_size_dynamic_sections. */
411e1bfb 7720
c9405344 7721bfd_boolean
e7d1c40c 7722ppc64_elf_tls_setup (struct bfd_link_info *info)
411e1bfb 7723{
411e1bfb 7724 struct ppc_link_hash_table *htab;
9e7028aa 7725 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
411e1bfb 7726
411e1bfb 7727 htab = ppc_hash_table (info);
4dfe6ac6 7728 if (htab == NULL)
c9405344 7729 return FALSE;
4dfe6ac6 7730
ee67d69a
AM
7731 if (abiversion (info->output_bfd) == 1)
7732 htab->opd_abi = 1;
7733
e7d1c40c 7734 if (htab->params->no_multi_toc)
33c0ec9d
AM
7735 htab->do_multi_toc = 0;
7736 else if (!htab->do_multi_toc)
e7d1c40c 7737 htab->params->no_multi_toc = 1;
33c0ec9d 7738
8b5f1ed8
AM
7739 /* Default to --no-plt-localentry, as this option can cause problems
7740 with symbol interposition. For example, glibc libpthread.so and
7741 libc.so duplicate many pthread symbols, with a fallback
7742 implementation in libc.so. In some cases the fallback does more
7743 work than the pthread implementation. __pthread_condattr_destroy
7744 is one such symbol: the libpthread.so implementation is
7745 localentry:0 while the libc.so implementation is localentry:8.
7746 An app that "cleverly" uses dlopen to only load necessary
7747 libraries at runtime may omit loading libpthread.so when not
7748 running multi-threaded, which then results in the libc.so
7749 fallback symbols being used and ld.so complaining. Now there
7750 are workarounds in ld (see non_zero_localentry) to detect the
7751 pthread situation, but that may not be the only case where
7752 --plt-localentry can cause trouble. */
f378ab09 7753 if (htab->params->plt_localentry0 < 0)
8b5f1ed8 7754 htab->params->plt_localentry0 = 0;
3cd7c7d7
AM
7755 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7756 {
7757 /* The issue is that __glink_PLTresolve saves r2, which is done
7758 because glibc ld.so _dl_runtime_resolve restores r2 to support
7759 a glibc plt call optimisation where global entry code is
7760 skipped on calls that resolve to the same binary. The
7761 __glink_PLTresolve save of r2 is incompatible with code
7762 making tail calls, because the tail call might go via the
7763 resolver and thus overwrite the proper saved r2. */
7764 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7765 "power10 pc-relative code"));
7766 htab->params->plt_localentry0 = 0;
7767 }
d44c746a
AM
7768 if (htab->params->plt_localentry0
7769 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7770 FALSE, FALSE, FALSE) == NULL)
cf97bcb0
AM
7771 _bfd_error_handler
7772 (_("warning: --plt-localentry is especially dangerous without "
7773 "ld.so support to detect ABI violations"));
f378ab09 7774
9e7028aa
AM
7775 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7776 FALSE, FALSE, TRUE);
7777 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7778
a7f2871e 7779 /* Move dynamic linking info to the function descriptor sym. */
9e7028aa
AM
7780 if (tga != NULL)
7781 func_desc_adjust (tga, info);
7782 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7783 FALSE, FALSE, TRUE);
7784 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7785
7786 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7787 FALSE, FALSE, TRUE);
7788 htab->tga_desc = ppc_elf_hash_entry (desc);
7789 if (desc != NULL)
7790 func_desc_adjust (desc, info);
7791 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7792 FALSE, FALSE, TRUE);
7793 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7794
7c9cf415 7795 if (htab->params->tls_get_addr_opt)
a7f2871e 7796 {
9e7028aa 7797 struct elf_link_hash_entry *opt, *opt_fd;
a7f2871e
AM
7798
7799 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7800 FALSE, FALSE, TRUE);
7801 if (opt != NULL)
7802 func_desc_adjust (opt, info);
7803 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7804 FALSE, FALSE, TRUE);
7805 if (opt_fd != NULL
7806 && (opt_fd->root.type == bfd_link_hash_defined
7807 || opt_fd->root.type == bfd_link_hash_defweak))
7808 {
7809 /* If glibc supports an optimized __tls_get_addr call stub,
7810 signalled by the presence of __tls_get_addr_opt, and we'll
7811 be calling __tls_get_addr via a plt call stub, then
7812 make __tls_get_addr point to __tls_get_addr_opt. */
9e7028aa
AM
7813 if (!(htab->elf.dynamic_sections_created
7814 && tga_fd != NULL
7815 && (tga_fd->type == STT_FUNC
7816 || tga_fd->needs_plt)
7817 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7818 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7819 tga_fd = NULL;
7820 if (!(htab->elf.dynamic_sections_created
7821 && desc_fd != NULL
7822 && (desc_fd->type == STT_FUNC
7823 || desc_fd->needs_plt)
7824 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7825 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7826 desc_fd = NULL;
7827
7828 if (tga_fd != NULL || desc_fd != NULL)
7829 {
7830 struct plt_entry *ent = NULL;
7831
7832 if (tga_fd != NULL)
7833 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7834 if (ent->plt.refcount > 0)
7835 break;
7836 if (ent == NULL && desc_fd != NULL)
7837 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7838 if (ent->plt.refcount > 0)
7839 break;
a7f2871e
AM
7840 if (ent != NULL)
7841 {
9e7028aa
AM
7842 if (tga_fd != NULL)
7843 {
7844 tga_fd->root.type = bfd_link_hash_indirect;
7845 tga_fd->root.u.i.link = &opt_fd->root;
7846 tga_fd->root.u.i.warning = NULL;
7847 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7848 }
7849 if (desc_fd != NULL)
7850 {
7851 desc_fd->root.type = bfd_link_hash_indirect;
7852 desc_fd->root.u.i.link = &opt_fd->root;
7853 desc_fd->root.u.i.warning = NULL;
7854 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7855 }
b531344c 7856 opt_fd->mark = 1;
a7f2871e
AM
7857 if (opt_fd->dynindx != -1)
7858 {
7859 /* Use __tls_get_addr_opt in dynamic relocations. */
7860 opt_fd->dynindx = -1;
7861 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7862 opt_fd->dynstr_index);
7863 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
c9405344 7864 return FALSE;
a7f2871e 7865 }
9e7028aa 7866 if (tga_fd != NULL)
a7f2871e 7867 {
9e7028aa 7868 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
30845f11 7869 tga = elf_hash_entry (htab->tls_get_addr);
9e7028aa
AM
7870 if (opt != NULL && tga != NULL)
7871 {
7872 tga->root.type = bfd_link_hash_indirect;
7873 tga->root.u.i.link = &opt->root;
7874 tga->root.u.i.warning = NULL;
7875 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7876 opt->mark = 1;
7877 _bfd_elf_link_hash_hide_symbol (info, opt,
7878 tga->forced_local);
7879 htab->tls_get_addr = ppc_elf_hash_entry (opt);
7880 }
7881 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7882 htab->tls_get_addr_fd->is_func_descriptor = 1;
7883 if (htab->tls_get_addr != NULL)
7884 {
7885 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7886 htab->tls_get_addr->is_func = 1;
7887 }
a7f2871e 7888 }
9e7028aa 7889 if (desc_fd != NULL)
a7f2871e 7890 {
9e7028aa
AM
7891 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7892 if (opt != NULL && desc != NULL)
7893 {
7894 desc->root.type = bfd_link_hash_indirect;
7895 desc->root.u.i.link = &opt->root;
7896 desc->root.u.i.warning = NULL;
7897 ppc64_elf_copy_indirect_symbol (info, opt, desc);
7898 opt->mark = 1;
7899 _bfd_elf_link_hash_hide_symbol (info, opt,
7900 desc->forced_local);
7901 htab->tga_desc = ppc_elf_hash_entry (opt);
7902 }
7903 htab->tga_desc_fd->oh = htab->tga_desc;
7904 htab->tga_desc_fd->is_func_descriptor = 1;
7905 if (htab->tga_desc != NULL)
7906 {
7907 htab->tga_desc->oh = htab->tga_desc_fd;
7908 htab->tga_desc->is_func = 1;
7909 }
a7f2871e
AM
7910 }
7911 }
7912 }
7913 }
7c9cf415
AM
7914 else if (htab->params->tls_get_addr_opt < 0)
7915 htab->params->tls_get_addr_opt = 0;
a7f2871e 7916 }
9e7028aa
AM
7917
7918 if (htab->tga_desc_fd != NULL
7919 && htab->params->tls_get_addr_opt
7920 && htab->params->no_tls_get_addr_regsave == -1)
7921 htab->params->no_tls_get_addr_regsave = 0;
7922
c9405344 7923 return TRUE;
3a71aa26 7924}
8387904d 7925
3a71aa26 7926/* Return TRUE iff REL is a branch reloc with a global symbol matching
9e7028aa 7927 any of HASH1, HASH2, HASH3, or HASH4. */
8387904d 7928
3a71aa26 7929static bfd_boolean
30845f11
AM
7930branch_reloc_hash_match (bfd *ibfd,
7931 Elf_Internal_Rela *rel,
7932 struct ppc_link_hash_entry *hash1,
7933 struct ppc_link_hash_entry *hash2,
7934 struct ppc_link_hash_entry *hash3,
7935 struct ppc_link_hash_entry *hash4)
3a71aa26
AM
7936{
7937 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7938 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7939 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7940
e054468f 7941 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7942 {
3a71aa26
AM
7943 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7944 struct elf_link_hash_entry *h;
8387904d 7945
3a71aa26 7946 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7947 h = elf_follow_link (h);
30845f11
AM
7948 if (h == elf_hash_entry (hash1)
7949 || h == elf_hash_entry (hash2)
7950 || h == elf_hash_entry (hash3)
7951 || h == elf_hash_entry (hash4))
3a71aa26 7952 return TRUE;
a48ebf4d 7953 }
3a71aa26 7954 return FALSE;
951fd09b 7955}
411e1bfb 7956
951fd09b
AM
7957/* Run through all the TLS relocs looking for optimization
7958 opportunities. The linker has been hacked (see ppc64elf.em) to do
7959 a preliminary section layout so that we know the TLS segment
7960 offsets. We can't optimize earlier because some optimizations need
7961 to know the tp offset, and we need to optimize before allocating
7962 dynamic relocations. */
7963
7964bfd_boolean
33c0ec9d 7965ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7966{
7967 bfd *ibfd;
7968 asection *sec;
7969 struct ppc_link_hash_table *htab;
663a1470 7970 unsigned char *toc_ref;
102890f0 7971 int pass;
951fd09b 7972
3cbc1e5e 7973 if (!bfd_link_executable (info))
411e1bfb
AM
7974 return TRUE;
7975
951fd09b 7976 htab = ppc_hash_table (info);
4dfe6ac6
NC
7977 if (htab == NULL)
7978 return FALSE;
7979
252dcdf4
AM
7980 htab->do_tls_opt = 1;
7981
663a1470
AM
7982 /* Make two passes over the relocs. On the first pass, mark toc
7983 entries involved with tls relocs, and check that tls relocs
7984 involved in setting up a tls_get_addr call are indeed followed by
7985 such a call. If they are not, we can't do any tls optimization.
7986 On the second pass twiddle tls_mask flags to notify
7987 relocate_section that optimization can be done, and adjust got
7988 and plt refcounts. */
7989 toc_ref = NULL;
7990 for (pass = 0; pass < 2; ++pass)
c72f2fb2 7991 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
663a1470
AM
7992 {
7993 Elf_Internal_Sym *locsyms = NULL;
7994 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7995
102890f0
AM
7996 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7997 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7998 {
7999 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8000 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8001
102890f0
AM
8002 /* Read the relocations. */
8003 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8004 info->keep_memory);
8005 if (relstart == NULL)
2915c55b
JK
8006 {
8007 free (toc_ref);
8008 return FALSE;
8009 }
411e1bfb 8010
102890f0
AM
8011 relend = relstart + sec->reloc_count;
8012 for (rel = relstart; rel < relend; rel++)
8013 {
8014 enum elf_ppc64_reloc_type r_type;
8015 unsigned long r_symndx;
8016 struct elf_link_hash_entry *h;
8017 Elf_Internal_Sym *sym;
8018 asection *sym_sec;
f961d9dd 8019 unsigned char *tls_mask;
46e9995a 8020 unsigned int tls_set, tls_clear, tls_type = 0;
102890f0
AM
8021 bfd_vma value;
8022 bfd_boolean ok_tprel, is_local;
8023 long toc_ref_index = 0;
8024 int expecting_tls_get_addr = 0;
663a1470 8025 bfd_boolean ret = FALSE;
411e1bfb 8026
102890f0
AM
8027 r_symndx = ELF64_R_SYM (rel->r_info);
8028 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8029 r_symndx, ibfd))
8030 {
8031 err_free_rel:
8032 if (elf_section_data (sec)->relocs != relstart)
8033 free (relstart);
c9594989
AM
8034 free (toc_ref);
8035 if (elf_symtab_hdr (ibfd).contents
8036 != (unsigned char *) locsyms)
102890f0 8037 free (locsyms);
663a1470 8038 return ret;
102890f0 8039 }
411e1bfb 8040
102890f0
AM
8041 if (h != NULL)
8042 {
766bc656
AM
8043 if (h->root.type == bfd_link_hash_defined
8044 || h->root.type == bfd_link_hash_defweak)
8045 value = h->root.u.def.value;
8046 else if (h->root.type == bfd_link_hash_undefweak)
8047 value = 0;
8048 else
663a1470
AM
8049 {
8050 found_tls_get_addr_arg = 0;
8051 continue;
8052 }
102890f0
AM
8053 }
8054 else
8055 /* Symbols referenced by TLS relocs must be of type
8056 STT_TLS. So no need for .opd local sym adjust. */
8057 value = sym->st_value;
8058
8059 ok_tprel = FALSE;
f749f26e
AM
8060 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8061 if (is_local)
102890f0 8062 {
766bc656
AM
8063 if (h != NULL
8064 && h->root.type == bfd_link_hash_undefweak)
8065 ok_tprel = TRUE;
c27b8c2a
AM
8066 else if (sym_sec != NULL
8067 && sym_sec->output_section != NULL)
766bc656
AM
8068 {
8069 value += sym_sec->output_offset;
8070 value += sym_sec->output_section->vma;
0b147428 8071 value -= htab->elf.tls_sec->vma + TP_OFFSET;
c213164a
AM
8072 /* Note that even though the prefix insns
8073 allow a 1<<33 offset we use the same test
8074 as for addis;addi. There may be a mix of
8075 pcrel and non-pcrel code and the decision
8076 to optimise is per symbol, not per TLS
8077 sequence. */
0b147428 8078 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
766bc656 8079 }
102890f0 8080 }
951fd09b 8081
102890f0 8082 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8083 /* If this section has old-style __tls_get_addr calls
8084 without marker relocs, then check that each
8085 __tls_get_addr call reloc is preceded by a reloc
8086 that conceivably belongs to the __tls_get_addr arg
8087 setup insn. If we don't find matching arg setup
8088 relocs, don't do any tls optimization. */
8089 if (pass == 0
9737e8af 8090 && sec->nomark_tls_get_addr
663a1470 8091 && h != NULL
ed7007c1 8092 && is_tls_get_addr (h, htab)
663a1470
AM
8093 && !found_tls_get_addr_arg
8094 && is_branch_reloc (r_type))
8095 {
25f53a85 8096 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8097 "TLS optimization disabled\n"),
8098 ibfd, sec, rel->r_offset);
8099 ret = TRUE;
8100 goto err_free_rel;
8101 }
8102
8103 found_tls_get_addr_arg = 0;
102890f0
AM
8104 switch (r_type)
8105 {
8106 case R_PPC64_GOT_TLSLD16:
8107 case R_PPC64_GOT_TLSLD16_LO:
87c69f97 8108 case R_PPC64_GOT_TLSLD_PCREL34:
102890f0 8109 expecting_tls_get_addr = 1;
663a1470 8110 found_tls_get_addr_arg = 1;
1a0670f3 8111 /* Fall through. */
102890f0
AM
8112
8113 case R_PPC64_GOT_TLSLD16_HI:
8114 case R_PPC64_GOT_TLSLD16_HA:
8115 /* These relocs should never be against a symbol
8116 defined in a shared lib. Leave them alone if
8117 that turns out to be the case. */
8118 if (!is_local)
8119 continue;
411e1bfb 8120
102890f0 8121 /* LD -> LE */
411e1bfb 8122 tls_set = 0;
102890f0
AM
8123 tls_clear = TLS_LD;
8124 tls_type = TLS_TLS | TLS_LD;
8125 break;
411e1bfb 8126
102890f0
AM
8127 case R_PPC64_GOT_TLSGD16:
8128 case R_PPC64_GOT_TLSGD16_LO:
87c69f97 8129 case R_PPC64_GOT_TLSGD_PCREL34:
102890f0 8130 expecting_tls_get_addr = 1;
663a1470 8131 found_tls_get_addr_arg = 1;
1a0670f3 8132 /* Fall through. */
102890f0
AM
8133
8134 case R_PPC64_GOT_TLSGD16_HI:
8135 case R_PPC64_GOT_TLSGD16_HA:
8136 if (ok_tprel)
8137 /* GD -> LE */
411e1bfb 8138 tls_set = 0;
102890f0
AM
8139 else
8140 /* GD -> IE */
b00a0a86 8141 tls_set = TLS_TLS | TLS_GDIE;
102890f0
AM
8142 tls_clear = TLS_GD;
8143 tls_type = TLS_TLS | TLS_GD;
8144 break;
8145
87c69f97 8146 case R_PPC64_GOT_TPREL_PCREL34:
102890f0
AM
8147 case R_PPC64_GOT_TPREL16_DS:
8148 case R_PPC64_GOT_TPREL16_LO_DS:
8149 case R_PPC64_GOT_TPREL16_HI:
8150 case R_PPC64_GOT_TPREL16_HA:
8151 if (ok_tprel)
8152 {
8153 /* IE -> LE */
8154 tls_set = 0;
8155 tls_clear = TLS_TPREL;
8156 tls_type = TLS_TLS | TLS_TPREL;
8157 break;
8158 }
411e1bfb
AM
8159 continue;
8160
727fc41e 8161 case R_PPC64_TLSLD:
7d04a20a
AM
8162 if (!is_local)
8163 continue;
8164 /* Fall through. */
8165 case R_PPC64_TLSGD:
23cedd1d
AM
8166 if (rel + 1 < relend
8167 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8168 {
8169 if (pass != 0
2cdcc330 8170 && (ELF64_R_TYPE (rel[1].r_info)
5663e321
AM
8171 != R_PPC64_PLTSEQ)
8172 && (ELF64_R_TYPE (rel[1].r_info)
8173 != R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
8174 {
8175 r_symndx = ELF64_R_SYM (rel[1].r_info);
8176 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
2cdcc330 8177 r_symndx, ibfd))
23cedd1d
AM
8178 goto err_free_rel;
8179 if (h != NULL)
8180 {
8181 struct plt_entry *ent = NULL;
8182
8183 for (ent = h->plt.plist;
8184 ent != NULL;
8185 ent = ent->next)
8186 if (ent->addend == rel[1].r_addend)
8187 break;
8188
8189 if (ent != NULL
8190 && ent->plt.refcount > 0)
8191 ent->plt.refcount -= 1;
8192 }
8193 }
8194 continue;
8195 }
663a1470 8196 found_tls_get_addr_arg = 1;
1a0670f3 8197 /* Fall through. */
663a1470
AM
8198
8199 case R_PPC64_TLS:
8200 case R_PPC64_TOC16:
8201 case R_PPC64_TOC16_LO:
102890f0
AM
8202 if (sym_sec == NULL || sym_sec != toc)
8203 continue;
8204
8205 /* Mark this toc entry as referenced by a TLS
8206 code sequence. We can do that now in the
8207 case of R_PPC64_TLS, and after checking for
8208 tls_get_addr for the TOC16 relocs. */
8209 if (toc_ref == NULL)
2cdcc330
AM
8210 toc_ref
8211 = bfd_zmalloc (toc->output_section->rawsize / 8);
663a1470
AM
8212 if (toc_ref == NULL)
8213 goto err_free_rel;
8214
102890f0
AM
8215 if (h != NULL)
8216 value = h->root.u.def.value;
8217 else
8218 value = sym->st_value;
8219 value += rel->r_addend;
73242275
AM
8220 if (value % 8 != 0)
8221 continue;
8222 BFD_ASSERT (value < toc->size
8223 && toc->output_offset % 8 == 0);
663a1470 8224 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8225 if (r_type == R_PPC64_TLS
8226 || r_type == R_PPC64_TLSGD
8227 || r_type == R_PPC64_TLSLD)
102890f0
AM
8228 {
8229 toc_ref[toc_ref_index] = 1;
8230 continue;
8231 }
8232
8233 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8234 continue;
8235
8236 tls_set = 0;
8237 tls_clear = 0;
8238 expecting_tls_get_addr = 2;
8239 break;
8240
8241 case R_PPC64_TPREL64:
8242 if (pass == 0
8243 || sec != toc
8244 || toc_ref == NULL
663a1470 8245 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8246 continue;
8247 if (ok_tprel)
8248 {
8249 /* IE -> LE */
8250 tls_set = TLS_EXPLICIT;
8251 tls_clear = TLS_TPREL;
8252 break;
8253 }
8254 continue;
8255
8256 case R_PPC64_DTPMOD64:
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 (rel + 1 < relend
8263 && (rel[1].r_info
8264 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8265 && rel[1].r_offset == rel->r_offset + 8)
8266 {
8267 if (ok_tprel)
8268 /* GD -> LE */
8269 tls_set = TLS_EXPLICIT | TLS_GD;
8270 else
8271 /* GD -> IE */
b00a0a86 8272 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
102890f0
AM
8273 tls_clear = TLS_GD;
8274 }
8275 else
8276 {
8277 if (!is_local)
8278 continue;
8279
8280 /* LD -> LE */
8281 tls_set = TLS_EXPLICIT;
8282 tls_clear = TLS_LD;
8283 }
8284 break;
8285
252dcdf4
AM
8286 case R_PPC64_TPREL16_HA:
8287 if (pass == 0)
8288 {
8289 unsigned char buf[4];
8290 unsigned int insn;
8291 bfd_vma off = rel->r_offset & ~3;
8292 if (!bfd_get_section_contents (ibfd, sec, buf,
8293 off, 4))
8294 goto err_free_rel;
8295 insn = bfd_get_32 (ibfd, buf);
8296 /* addis rt,13,imm */
8297 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8298 != ((15u << 26) | (13 << 16)))
8299 {
8300 /* xgettext:c-format */
8301 info->callbacks->minfo
8302 (_("%H: warning: %s unexpected insn %#x.\n"),
8303 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8304 htab->do_tls_opt = 0;
8305 }
8306 }
8307 continue;
8308
8309 case R_PPC64_TPREL16_HI:
8310 case R_PPC64_TPREL16_HIGH:
8311 case R_PPC64_TPREL16_HIGHA:
8312 case R_PPC64_TPREL16_HIGHER:
8313 case R_PPC64_TPREL16_HIGHERA:
8314 case R_PPC64_TPREL16_HIGHEST:
8315 case R_PPC64_TPREL16_HIGHESTA:
8316 /* These can all be used in sequences along with
8317 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8318 able to verify easily. */
8319 htab->do_tls_opt = 0;
8320 continue;
8321
102890f0
AM
8322 default:
8323 continue;
8324 }
8325
8326 if (pass == 0)
8327 {
727fc41e 8328 if (!expecting_tls_get_addr
9737e8af 8329 || !sec->nomark_tls_get_addr)
102890f0
AM
8330 continue;
8331
3a71aa26
AM
8332 if (rel + 1 < relend
8333 && branch_reloc_hash_match (ibfd, rel + 1,
9e7028aa
AM
8334 htab->tls_get_addr_fd,
8335 htab->tga_desc_fd,
3a71aa26 8336 htab->tls_get_addr,
9e7028aa 8337 htab->tga_desc))
102890f0 8338 {
3a71aa26 8339 if (expecting_tls_get_addr == 2)
102890f0 8340 {
3a71aa26 8341 /* Check for toc tls entries. */
f961d9dd 8342 unsigned char *toc_tls;
3a71aa26
AM
8343 int retval;
8344
8345 retval = get_tls_mask (&toc_tls, NULL, NULL,
8346 &locsyms,
8347 rel, ibfd);
8348 if (retval == 0)
8349 goto err_free_rel;
663a1470
AM
8350 if (toc_tls != NULL)
8351 {
37da22e5
AM
8352 if ((*toc_tls & TLS_TLS) != 0
8353 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
663a1470
AM
8354 found_tls_get_addr_arg = 1;
8355 if (retval > 1)
8356 toc_ref[toc_ref_index] = 1;
8357 }
102890f0 8358 }
3a71aa26 8359 continue;
102890f0
AM
8360 }
8361
102890f0
AM
8362 /* Uh oh, we didn't find the expected call. We
8363 could just mark this symbol to exclude it
8364 from tls optimization but it's safer to skip
663a1470 8365 the entire optimization. */
695344c0 8366 /* xgettext:c-format */
25f53a85 8367 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8368 "TLS optimization disabled\n"),
8369 ibfd, sec, rel->r_offset);
8370 ret = TRUE;
8371 goto err_free_rel;
102890f0
AM
8372 }
8373
37da22e5
AM
8374 /* If we don't have old-style __tls_get_addr calls
8375 without TLSGD/TLSLD marker relocs, and we haven't
8376 found a new-style __tls_get_addr call with a
8377 marker for this symbol, then we either have a
8378 broken object file or an -mlongcall style
8379 indirect call to __tls_get_addr without a marker.
8380 Disable optimization in this case. */
8381 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8382 && (tls_set & TLS_EXPLICIT) == 0
9737e8af 8383 && !sec->nomark_tls_get_addr
37da22e5
AM
8384 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8385 != (TLS_TLS | TLS_MARK)))
8386 continue;
8387
7d04a20a 8388 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
102890f0 8389 {
23cedd1d
AM
8390 struct plt_entry *ent = NULL;
8391
9e7028aa
AM
8392 if (htab->tls_get_addr_fd != NULL)
8393 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8394 ent != NULL;
8395 ent = ent->next)
8396 if (ent->addend == 0)
8397 break;
8398
8399 if (ent == NULL && htab->tga_desc_fd != NULL)
8400 for (ent = htab->tga_desc_fd->elf.plt.plist;
8401 ent != NULL;
8402 ent = ent->next)
8403 if (ent->addend == 0)
8404 break;
8405
8406 if (ent == NULL && htab->tls_get_addr != NULL)
23cedd1d
AM
8407 for (ent = htab->tls_get_addr->elf.plt.plist;
8408 ent != NULL;
8409 ent = ent->next)
8410 if (ent->addend == 0)
102890f0 8411 break;
411e1bfb 8412
9e7028aa
AM
8413 if (ent == NULL && htab->tga_desc != NULL)
8414 for (ent = htab->tga_desc->elf.plt.plist;
23cedd1d
AM
8415 ent != NULL;
8416 ent = ent->next)
8417 if (ent->addend == 0)
102890f0 8418 break;
23cedd1d
AM
8419
8420 if (ent != NULL
8421 && ent->plt.refcount > 0)
8422 ent->plt.refcount -= 1;
102890f0 8423 }
411e1bfb 8424
102890f0 8425 if (tls_clear == 0)
30038c59
AM
8426 continue;
8427
102890f0
AM
8428 if ((tls_set & TLS_EXPLICIT) == 0)
8429 {
8430 struct got_entry *ent;
411e1bfb 8431
102890f0
AM
8432 /* Adjust got entry for this reloc. */
8433 if (h != NULL)
8434 ent = h->got.glist;
8435 else
8436 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8437
102890f0
AM
8438 for (; ent != NULL; ent = ent->next)
8439 if (ent->addend == rel->r_addend
8440 && ent->owner == ibfd
8441 && ent->tls_type == tls_type)
8442 break;
8443 if (ent == NULL)
8444 abort ();
411e1bfb 8445
102890f0
AM
8446 if (tls_set == 0)
8447 {
8448 /* We managed to get rid of a got entry. */
8449 if (ent->got.refcount > 0)
8450 ent->got.refcount -= 1;
8451 }
8452 }
8453 else
8454 {
8455 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8456 we'll lose one or two dyn relocs. */
8457 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8458 NULL, h, sym))
102890f0 8459 return FALSE;
411e1bfb 8460
102890f0
AM
8461 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8462 {
8463 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8464 NULL, h, sym))
102890f0
AM
8465 return FALSE;
8466 }
8467 }
411e1bfb 8468
46e9995a 8469 *tls_mask |= tls_set & 0xff;
102890f0
AM
8470 *tls_mask &= ~tls_clear;
8471 }
8c1d1bb8 8472
102890f0
AM
8473 if (elf_section_data (sec)->relocs != relstart)
8474 free (relstart);
8475 }
411e1bfb 8476
663a1470
AM
8477 if (locsyms != NULL
8478 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8479 {
8480 if (!info->keep_memory)
8481 free (locsyms);
8482 else
8483 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8484 }
8485 }
411e1bfb 8486
c9594989 8487 free (toc_ref);
b34976b6 8488 return TRUE;
1e2f5b6e 8489}
b34976b6 8490
c5614fa4
AM
8491/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8492 the values of any global symbols in a toc section that has been
8493 edited. Globals in toc sections should be a rarity, so this function
8494 sets a flag if any are found in toc sections other than the one just
de194d85 8495 edited, so that further hash table traversals can be avoided. */
c5614fa4
AM
8496
8497struct adjust_toc_info
8498{
8499 asection *toc;
8500 unsigned long *skip;
8501 bfd_boolean global_toc_syms;
8502};
8503
ba761f19
AM
8504enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8505
c5614fa4
AM
8506static bfd_boolean
8507adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8508{
8509 struct ppc_link_hash_entry *eh;
8510 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8511 unsigned long i;
c5614fa4 8512
c5614fa4
AM
8513 if (h->root.type != bfd_link_hash_defined
8514 && h->root.type != bfd_link_hash_defweak)
8515 return TRUE;
8516
ed7007c1 8517 eh = ppc_elf_hash_entry (h);
c5614fa4
AM
8518 if (eh->adjust_done)
8519 return TRUE;
8520
8521 if (eh->elf.root.u.def.section == toc_inf->toc)
8522 {
854b41e7
AM
8523 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8524 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8525 else
854b41e7
AM
8526 i = eh->elf.root.u.def.value >> 3;
8527
ba761f19 8528 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4 8529 {
4eca0228 8530 _bfd_error_handler
854b41e7
AM
8531 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8532 do
8533 ++i;
ba761f19 8534 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8535 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8536 }
854b41e7
AM
8537
8538 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8539 eh->adjust_done = 1;
8540 }
8541 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8542 toc_inf->global_toc_syms = TRUE;
8543
8544 return TRUE;
8545}
8546
39eeab25
AM
8547/* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8548 on a _LO variety toc/got reloc. */
560c8763
AM
8549
8550static bfd_boolean
39eeab25 8551ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
560c8763 8552{
2365f8d7
AM
8553 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8554 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8555 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8556 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8557 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8558 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8559 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8560 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8561 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8562 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8563 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8564 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8565 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8566 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8567 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8568 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8569 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
39eeab25
AM
8570 /* Exclude lfqu by testing reloc. If relocs are ever
8571 defined for the reduced D field in psq_lu then those
8572 will need testing too. */
8573 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8574 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
39eeab25 8575 && (insn & 1) == 0)
2365f8d7
AM
8576 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8577 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
39eeab25
AM
8578 /* Exclude stfqu. psq_stu as above for psq_lu. */
8579 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
2365f8d7 8580 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
39eeab25 8581 && (insn & 1) == 0));
560c8763
AM
8582}
8583
4a421c53
AM
8584/* PCREL_OPT in one instance flags to the linker that a pair of insns:
8585 pld ra,symbol@got@pcrel
dd9b12c2 8586 load/store rt,off(ra)
4a421c53 8587 or
d4b87b1e 8588 pla ra,symbol@pcrel
dd9b12c2 8589 load/store rt,off(ra)
4a421c53 8590 may be translated to
dd9b12c2 8591 pload/pstore rt,symbol+off@pcrel
4a421c53
AM
8592 nop.
8593 This function returns true if the optimization is possible, placing
dd9b12c2 8594 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
4a421c53
AM
8595
8596 On entry to this function, the linker has already determined that
d4b87b1e 8597 the pld can be replaced with pla: *PINSN1 is that pla insn,
4a421c53
AM
8598 while *PINSN2 is the second instruction. */
8599
8600static bfd_boolean
dd9b12c2 8601xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
4a421c53 8602{
77486630
AM
8603 uint64_t insn1 = *pinsn1;
8604 uint64_t insn2 = *pinsn2;
dd9b12c2 8605 bfd_signed_vma off;
4a421c53 8606
77486630
AM
8607 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8608 {
8609 /* Check that regs match. */
8610 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8611 return FALSE;
8612
8613 /* P8LS or PMLS form, non-pcrel. */
8614 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8615 return FALSE;
8616
8617 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8618 *pinsn2 = PNOP;
8619 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8620 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8621 return TRUE;
8622 }
8623
8624 insn2 >>= 32;
8625
4a421c53 8626 /* Check that regs match. */
77486630 8627 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
4a421c53
AM
8628 return FALSE;
8629
8630 switch ((insn2 >> 26) & 63)
8631 {
8632 default:
8633 return FALSE;
8634
8635 case 32: /* lwz */
8636 case 34: /* lbz */
8637 case 36: /* stw */
8638 case 38: /* stb */
8639 case 40: /* lhz */
8640 case 42: /* lha */
8641 case 44: /* sth */
8642 case 48: /* lfs */
8643 case 50: /* lfd */
8644 case 52: /* stfs */
8645 case 54: /* stfd */
8646 /* These are the PMLS cases, where we just need to tack a prefix
dd9b12c2 8647 on the insn. */
77486630 8648 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
4a421c53 8649 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8650 off = insn2 & 0xffff;
4a421c53
AM
8651 break;
8652
8653 case 58: /* lwa, ld */
dd9b12c2 8654 if ((insn2 & 1) != 0)
4a421c53 8655 return FALSE;
77486630 8656 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8657 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8658 | (insn2 & (31ULL << 21)));
dd9b12c2 8659 off = insn2 & 0xfffc;
4a421c53
AM
8660 break;
8661
8662 case 57: /* lxsd, lxssp */
dd9b12c2 8663 if ((insn2 & 3) < 2)
4a421c53 8664 return FALSE;
77486630 8665 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8666 | ((40ULL | (insn2 & 3)) << 26)
8667 | (insn2 & (31ULL << 21)));
dd9b12c2 8668 off = insn2 & 0xfffc;
4a421c53
AM
8669 break;
8670
8671 case 61: /* stxsd, stxssp, lxv, stxv */
8672 if ((insn2 & 3) == 0)
8673 return FALSE;
8674 else if ((insn2 & 3) >= 2)
8675 {
77486630 8676 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8677 | ((44ULL | (insn2 & 3)) << 26)
8678 | (insn2 & (31ULL << 21)));
dd9b12c2 8679 off = insn2 & 0xfffc;
4a421c53
AM
8680 }
8681 else
8682 {
77486630 8683 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8684 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8685 | (insn2 & (31ULL << 21)));
dd9b12c2 8686 off = insn2 & 0xfff0;
4a421c53
AM
8687 }
8688 break;
8689
8690 case 56: /* lq */
77486630 8691 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53 8692 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
dd9b12c2 8693 off = insn2 & 0xffff;
4a421c53
AM
8694 break;
8695
94ba9882
AM
8696 case 6: /* lxvp, stxvp */
8697 if ((insn2 & 0xe) != 0)
8698 return FALSE;
8699 insn1 = ((1ULL << 58) | (1ULL << 52)
8700 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8701 | (insn2 & (31ULL << 21)));
8702 off = insn2 & 0xfff0;
8703 break;
8704
4a421c53 8705 case 62: /* std, stq */
dd9b12c2 8706 if ((insn2 & 1) != 0)
4a421c53 8707 return FALSE;
77486630 8708 insn1 = ((1ULL << 58) | (1ULL << 52)
4a421c53
AM
8709 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8710 | (insn2 & (31ULL << 21)));
dd9b12c2 8711 off = insn2 & 0xfffc;
4a421c53
AM
8712 break;
8713 }
8714
77486630 8715 *pinsn1 = insn1;
4a421c53 8716 *pinsn2 = (uint64_t) NOP << 32;
dd9b12c2 8717 *poff = (off ^ 0x8000) - 0x8000;
4a421c53
AM
8718 return TRUE;
8719}
8720
c5614fa4
AM
8721/* Examine all relocs referencing .toc sections in order to remove
8722 unused .toc entries. */
8723
8724bfd_boolean
33c0ec9d 8725ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8726{
8727 bfd *ibfd;
8728 struct adjust_toc_info toc_inf;
67f0cbdb 8729 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8730
67f0cbdb 8731 htab->do_toc_opt = 1;
c5614fa4 8732 toc_inf.global_toc_syms = TRUE;
c72f2fb2 8733 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
c5614fa4
AM
8734 {
8735 asection *toc, *sec;
8736 Elf_Internal_Shdr *symtab_hdr;
8737 Elf_Internal_Sym *local_syms;
425b145b 8738 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8739 unsigned long *skip, *drop;
8740 unsigned char *used;
8741 unsigned char *keep, last, some_unused;
8742
854b41e7
AM
8743 if (!is_ppc64_elf (ibfd))
8744 continue;
8745
c5614fa4
AM
8746 toc = bfd_get_section_by_name (ibfd, ".toc");
8747 if (toc == NULL
92b7a70f 8748 || toc->size == 0
dbaa2011
AM
8749 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8750 || discarded_section (toc))
c5614fa4
AM
8751 continue;
8752
425b145b 8753 toc_relocs = NULL;
c5614fa4 8754 local_syms = NULL;
0ffa91dd 8755 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8756
8757 /* Look at sections dropped from the final link. */
8758 skip = NULL;
8759 relstart = NULL;
8760 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8761 {
8762 if (sec->reloc_count == 0
dbaa2011 8763 || !discarded_section (sec)
c5614fa4
AM
8764 || get_opd_info (sec)
8765 || (sec->flags & SEC_ALLOC) == 0
8766 || (sec->flags & SEC_DEBUGGING) != 0)
8767 continue;
8768
8769 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8770 if (relstart == NULL)
8771 goto error_ret;
8772
8773 /* Run through the relocs to see which toc entries might be
8774 unused. */
8775 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8776 {
8777 enum elf_ppc64_reloc_type r_type;
8778 unsigned long r_symndx;
8779 asection *sym_sec;
8780 struct elf_link_hash_entry *h;
8781 Elf_Internal_Sym *sym;
8782 bfd_vma val;
8783
8784 r_type = ELF64_R_TYPE (rel->r_info);
8785 switch (r_type)
8786 {
8787 default:
8788 continue;
8789
8790 case R_PPC64_TOC16:
8791 case R_PPC64_TOC16_LO:
8792 case R_PPC64_TOC16_HI:
8793 case R_PPC64_TOC16_HA:
8794 case R_PPC64_TOC16_DS:
8795 case R_PPC64_TOC16_LO_DS:
8796 break;
8797 }
8798
8799 r_symndx = ELF64_R_SYM (rel->r_info);
8800 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8801 r_symndx, ibfd))
8802 goto error_ret;
8803
8804 if (sym_sec != toc)
8805 continue;
8806
8807 if (h != NULL)
8808 val = h->root.u.def.value;
8809 else
8810 val = sym->st_value;
8811 val += rel->r_addend;
8812
8813 if (val >= toc->size)
8814 continue;
8815
8816 /* Anything in the toc ought to be aligned to 8 bytes.
8817 If not, don't mark as unused. */
8818 if (val & 7)
8819 continue;
8820
8821 if (skip == NULL)
8822 {
854b41e7 8823 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8824 if (skip == NULL)
8825 goto error_ret;
8826 }
8827
ba761f19 8828 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8829 }
8830
8831 if (elf_section_data (sec)->relocs != relstart)
8832 free (relstart);
8833 }
8834
ba761f19
AM
8835 /* For largetoc loads of address constants, we can convert
8836 . addis rx,2,addr@got@ha
8837 . ld ry,addr@got@l(rx)
8838 to
8839 . addis rx,2,addr@toc@ha
8840 . addi ry,rx,addr@toc@l
8841 when addr is within 2G of the toc pointer. This then means
8842 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8843
ba761f19
AM
8844 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8845 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8846 && toc->reloc_count != 0)
8847 {
8848 /* Read toc relocs. */
425b145b
AM
8849 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8850 info->keep_memory);
8851 if (toc_relocs == NULL)
ba761f19
AM
8852 goto error_ret;
8853
425b145b 8854 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8855 {
8856 enum elf_ppc64_reloc_type r_type;
8857 unsigned long r_symndx;
8858 asection *sym_sec;
8859 struct elf_link_hash_entry *h;
8860 Elf_Internal_Sym *sym;
8861 bfd_vma val, addr;
8862
8863 r_type = ELF64_R_TYPE (rel->r_info);
8864 if (r_type != R_PPC64_ADDR64)
8865 continue;
8866
8867 r_symndx = ELF64_R_SYM (rel->r_info);
8868 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8869 r_symndx, ibfd))
8870 goto error_ret;
8871
425b145b 8872 if (sym_sec == NULL
c27b8c2a 8873 || sym_sec->output_section == NULL
dbaa2011 8874 || discarded_section (sym_sec))
425b145b
AM
8875 continue;
8876
afe397ea 8877 if (!SYMBOL_REFERENCES_LOCAL (info, h))
ba761f19
AM
8878 continue;
8879
8880 if (h != NULL)
bddc25c9
AM
8881 {
8882 if (h->type == STT_GNU_IFUNC)
8883 continue;
8884 val = h->root.u.def.value;
8885 }
ba761f19 8886 else
bddc25c9
AM
8887 {
8888 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8889 continue;
8890 val = sym->st_value;
8891 }
ba761f19
AM
8892 val += rel->r_addend;
8893 val += sym_sec->output_section->vma + sym_sec->output_offset;
8894
8895 /* We don't yet know the exact toc pointer value, but we
8896 know it will be somewhere in the toc section. Don't
8897 optimize if the difference from any possible toc
8898 pointer is outside [ff..f80008000, 7fff7fff]. */
8899 addr = toc->output_section->vma + TOC_BASE_OFF;
8900 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8901 continue;
8902
8903 addr = toc->output_section->vma + toc->output_section->rawsize;
8904 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8905 continue;
8906
8907 if (skip == NULL)
8908 {
8909 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8910 if (skip == NULL)
8911 goto error_ret;
8912 }
8913
8914 skip[rel->r_offset >> 3]
425b145b 8915 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8916 }
ba761f19
AM
8917 }
8918
c5614fa4
AM
8919 if (skip == NULL)
8920 continue;
8921
8922 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8923 if (used == NULL)
8924 {
8925 error_ret:
c9594989 8926 if (symtab_hdr->contents != (unsigned char *) local_syms)
c5614fa4
AM
8927 free (local_syms);
8928 if (sec != NULL
c5614fa4
AM
8929 && elf_section_data (sec)->relocs != relstart)
8930 free (relstart);
c9594989 8931 if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 8932 free (toc_relocs);
c9594989 8933 free (skip);
c5614fa4
AM
8934 return FALSE;
8935 }
8936
30038c59
AM
8937 /* Now check all kept sections that might reference the toc.
8938 Check the toc itself last. */
8939 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8940 : ibfd->sections);
c5614fa4 8941 sec != NULL;
c5614fa4 8942 sec = (sec == toc ? NULL
c5614fa4 8943 : sec->next == NULL ? toc
30038c59 8944 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8945 : sec->next))
8946 {
8947 int repeat;
8948
8949 if (sec->reloc_count == 0
dbaa2011 8950 || discarded_section (sec)
c5614fa4
AM
8951 || get_opd_info (sec)
8952 || (sec->flags & SEC_ALLOC) == 0
8953 || (sec->flags & SEC_DEBUGGING) != 0)
8954 continue;
8955
854b41e7
AM
8956 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8957 info->keep_memory);
c5614fa4 8958 if (relstart == NULL)
2915c55b
JK
8959 {
8960 free (used);
8961 goto error_ret;
8962 }
c5614fa4
AM
8963
8964 /* Mark toc entries referenced as used. */
c5614fa4 8965 do
d4f1ee75
AM
8966 {
8967 repeat = 0;
8968 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8969 {
8970 enum elf_ppc64_reloc_type r_type;
8971 unsigned long r_symndx;
8972 asection *sym_sec;
8973 struct elf_link_hash_entry *h;
8974 Elf_Internal_Sym *sym;
8975 bfd_vma val;
98528052 8976
d4f1ee75 8977 r_type = ELF64_R_TYPE (rel->r_info);
d4f1ee75
AM
8978 switch (r_type)
8979 {
8980 case R_PPC64_TOC16:
8981 case R_PPC64_TOC16_LO:
8982 case R_PPC64_TOC16_HI:
8983 case R_PPC64_TOC16_HA:
8984 case R_PPC64_TOC16_DS:
8985 case R_PPC64_TOC16_LO_DS:
8986 /* In case we're taking addresses of toc entries. */
8987 case R_PPC64_ADDR64:
8988 break;
c5614fa4 8989
d4f1ee75
AM
8990 default:
8991 continue;
8992 }
c5614fa4 8993
d4f1ee75
AM
8994 r_symndx = ELF64_R_SYM (rel->r_info);
8995 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8996 r_symndx, ibfd))
8997 {
8998 free (used);
8999 goto error_ret;
9000 }
c5614fa4 9001
d4f1ee75
AM
9002 if (sym_sec != toc)
9003 continue;
c5614fa4 9004
d4f1ee75
AM
9005 if (h != NULL)
9006 val = h->root.u.def.value;
9007 else
9008 val = sym->st_value;
9009 val += rel->r_addend;
ba761f19 9010
d4f1ee75
AM
9011 if (val >= toc->size)
9012 continue;
ba761f19 9013
d4f1ee75
AM
9014 if ((skip[val >> 3] & can_optimize) != 0)
9015 {
9016 bfd_vma off;
9017 unsigned char opc;
9018
9019 switch (r_type)
9020 {
9021 case R_PPC64_TOC16_HA:
ba761f19 9022 break;
ba761f19 9023
d4f1ee75
AM
9024 case R_PPC64_TOC16_LO_DS:
9025 off = rel->r_offset;
9026 off += (bfd_big_endian (ibfd) ? -2 : 3);
9027 if (!bfd_get_section_contents (ibfd, sec, &opc,
9028 off, 1))
9029 {
9030 free (used);
9031 goto error_ret;
9032 }
9033 if ((opc & (0x3f << 2)) == (58u << 2))
9034 break;
1a0670f3 9035 /* Fall through. */
ba761f19 9036
d4f1ee75
AM
9037 default:
9038 /* Wrong sort of reloc, or not a ld. We may
9039 as well clear ref_from_discarded too. */
9040 skip[val >> 3] = 0;
9041 }
9042 }
9043
9044 if (sec != toc)
9045 used[val >> 3] = 1;
9046 /* For the toc section, we only mark as used if this
9047 entry itself isn't unused. */
9048 else if ((used[rel->r_offset >> 3]
9049 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9050 && !used[val >> 3])
9051 {
9052 /* Do all the relocs again, to catch reference
9053 chains. */
9054 repeat = 1;
9055 used[val >> 3] = 1;
9056 }
9057 }
9058 }
c5614fa4 9059 while (repeat);
854b41e7
AM
9060
9061 if (elf_section_data (sec)->relocs != relstart)
9062 free (relstart);
c5614fa4
AM
9063 }
9064
9065 /* Merge the used and skip arrays. Assume that TOC
9066 doublewords not appearing as either used or unused belong
de194d85 9067 to an entry more than one doubleword in size. */
c5614fa4
AM
9068 for (drop = skip, keep = used, last = 0, some_unused = 0;
9069 drop < skip + (toc->size + 7) / 8;
9070 ++drop, ++keep)
9071 {
9072 if (*keep)
9073 {
ba761f19
AM
9074 *drop &= ~ref_from_discarded;
9075 if ((*drop & can_optimize) != 0)
9076 some_unused = 1;
c5614fa4
AM
9077 last = 0;
9078 }
b140b010 9079 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
9080 {
9081 some_unused = 1;
ba761f19 9082 last = ref_from_discarded;
c5614fa4
AM
9083 }
9084 else
9085 *drop = last;
9086 }
9087
9088 free (used);
9089
9090 if (some_unused)
9091 {
9092 bfd_byte *contents, *src;
9093 unsigned long off;
d62b3684 9094 Elf_Internal_Sym *sym;
ba761f19 9095 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
9096
9097 /* Shuffle the toc contents, and at the same time convert the
9098 skip array from booleans into offsets. */
9099 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9100 goto error_ret;
9101
9102 elf_section_data (toc)->this_hdr.contents = contents;
9103
9104 for (src = contents, off = 0, drop = skip;
9105 src < contents + toc->size;
9106 src += 8, ++drop)
9107 {
ba761f19
AM
9108 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9109 off += 8;
c5614fa4
AM
9110 else if (off != 0)
9111 {
9112 *drop = off;
9113 memcpy (src - off, src, 8);
9114 }
9115 }
854b41e7 9116 *drop = off;
c5614fa4
AM
9117 toc->rawsize = toc->size;
9118 toc->size = src - contents - off;
9119
ba761f19
AM
9120 /* Adjust addends for relocs against the toc section sym,
9121 and optimize any accesses we can. */
c5614fa4
AM
9122 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9123 {
9124 if (sec->reloc_count == 0
dbaa2011 9125 || discarded_section (sec))
c5614fa4
AM
9126 continue;
9127
9128 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 9129 info->keep_memory);
c5614fa4
AM
9130 if (relstart == NULL)
9131 goto error_ret;
9132
9133 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9134 {
9135 enum elf_ppc64_reloc_type r_type;
9136 unsigned long r_symndx;
9137 asection *sym_sec;
9138 struct elf_link_hash_entry *h;
854b41e7 9139 bfd_vma val;
c5614fa4
AM
9140
9141 r_type = ELF64_R_TYPE (rel->r_info);
9142 switch (r_type)
9143 {
9144 default:
9145 continue;
9146
9147 case R_PPC64_TOC16:
9148 case R_PPC64_TOC16_LO:
9149 case R_PPC64_TOC16_HI:
9150 case R_PPC64_TOC16_HA:
9151 case R_PPC64_TOC16_DS:
9152 case R_PPC64_TOC16_LO_DS:
9153 case R_PPC64_ADDR64:
9154 break;
9155 }
9156
9157 r_symndx = ELF64_R_SYM (rel->r_info);
9158 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9159 r_symndx, ibfd))
9160 goto error_ret;
9161
ba761f19 9162 if (sym_sec != toc)
c5614fa4
AM
9163 continue;
9164
ba761f19
AM
9165 if (h != NULL)
9166 val = h->root.u.def.value;
9167 else
9168 {
9169 val = sym->st_value;
9170 if (val != 0)
9171 local_toc_syms = TRUE;
9172 }
9173
9174 val += rel->r_addend;
854b41e7
AM
9175
9176 if (val > toc->rawsize)
9177 val = toc->rawsize;
ba761f19
AM
9178 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9179 continue;
9180 else if ((skip[val >> 3] & can_optimize) != 0)
9181 {
9182 Elf_Internal_Rela *tocrel
425b145b 9183 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
9184 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9185
9186 switch (r_type)
9187 {
9188 case R_PPC64_TOC16_HA:
9189 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9190 break;
9191
9192 case R_PPC64_TOC16_LO_DS:
9193 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9194 break;
9195
9196 default:
28942f62
AM
9197 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9198 ppc_howto_init ();
b140b010 9199 info->callbacks->einfo
695344c0 9200 /* xgettext:c-format */
174d0a74 9201 (_("%H: %s references "
b140b010
AM
9202 "optimized away TOC entry\n"),
9203 ibfd, sec, rel->r_offset,
9204 ppc64_elf_howto_table[r_type]->name);
9205 bfd_set_error (bfd_error_bad_value);
9206 goto error_ret;
ba761f19
AM
9207 }
9208 rel->r_addend = tocrel->r_addend;
9209 elf_section_data (sec)->relocs = relstart;
9210 continue;
9211 }
9212
9213 if (h != NULL || sym->st_value != 0)
9214 continue;
854b41e7
AM
9215
9216 rel->r_addend -= skip[val >> 3];
9217 elf_section_data (sec)->relocs = relstart;
c5614fa4 9218 }
854b41e7
AM
9219
9220 if (elf_section_data (sec)->relocs != relstart)
9221 free (relstart);
c5614fa4
AM
9222 }
9223
9224 /* We shouldn't have local or global symbols defined in the TOC,
9225 but handle them anyway. */
df22d223
AM
9226 if (local_syms != NULL)
9227 for (sym = local_syms;
9228 sym < local_syms + symtab_hdr->sh_info;
9229 ++sym)
9230 if (sym->st_value != 0
9231 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9232 {
9233 unsigned long i;
854b41e7 9234
df22d223
AM
9235 if (sym->st_value > toc->rawsize)
9236 i = toc->rawsize >> 3;
9237 else
9238 i = sym->st_value >> 3;
854b41e7 9239
df22d223
AM
9240 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9241 {
9242 if (local_toc_syms)
4eca0228 9243 _bfd_error_handler
df22d223
AM
9244 (_("%s defined on removed toc entry"),
9245 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9246 do
9247 ++i;
9248 while ((skip[i] & (ref_from_discarded | can_optimize)));
9249 sym->st_value = (bfd_vma) i << 3;
9250 }
d62b3684 9251
df22d223
AM
9252 sym->st_value -= skip[i];
9253 symtab_hdr->contents = (unsigned char *) local_syms;
9254 }
c5614fa4 9255
854b41e7 9256 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9257 if (toc_inf.global_toc_syms)
9258 {
9259 toc_inf.toc = toc;
9260 toc_inf.skip = skip;
9261 toc_inf.global_toc_syms = FALSE;
9262 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9263 &toc_inf);
9264 }
854b41e7
AM
9265
9266 if (toc->reloc_count != 0)
9267 {
d4730f92 9268 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9269 Elf_Internal_Rela *wrel;
9270 bfd_size_type sz;
9271
854b41e7 9272 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9273 if (toc_relocs == NULL)
9274 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9275 info->keep_memory);
9276 if (toc_relocs == NULL)
9277 goto error_ret;
9278
425b145b
AM
9279 wrel = toc_relocs;
9280 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9281 if ((skip[rel->r_offset >> 3]
9282 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9283 {
9284 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9285 wrel->r_info = rel->r_info;
9286 wrel->r_addend = rel->r_addend;
9287 ++wrel;
9288 }
9289 else if (!dec_dynrel_count (rel->r_info, toc, info,
9290 &local_syms, NULL, NULL))
9291 goto error_ret;
9292
425b145b
AM
9293 elf_section_data (toc)->relocs = toc_relocs;
9294 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9295 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9296 sz = rel_hdr->sh_entsize;
9297 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9298 }
c5614fa4 9299 }
c9594989 9300 else if (elf_section_data (toc)->relocs != toc_relocs)
425b145b 9301 free (toc_relocs);
c5614fa4
AM
9302
9303 if (local_syms != NULL
9304 && symtab_hdr->contents != (unsigned char *) local_syms)
9305 {
9306 if (!info->keep_memory)
9307 free (local_syms);
9308 else
9309 symtab_hdr->contents = (unsigned char *) local_syms;
9310 }
9311 free (skip);
9312 }
9313
066f4018 9314 /* Look for cases where we can change an indirect GOT access to
4a421c53
AM
9315 a GOT relative or PC relative access, possibly reducing the
9316 number of GOT entries. */
066f4018
AM
9317 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9318 {
9319 asection *sec;
9320 Elf_Internal_Shdr *symtab_hdr;
9321 Elf_Internal_Sym *local_syms;
9322 Elf_Internal_Rela *relstart, *rel;
9323 bfd_vma got;
9324
9325 if (!is_ppc64_elf (ibfd))
9326 continue;
9327
903b777d 9328 if (!ppc64_elf_tdata (ibfd)->has_optrel)
066f4018
AM
9329 continue;
9330
9331 sec = ppc64_elf_tdata (ibfd)->got;
903b777d
AM
9332 got = 0;
9333 if (sec != NULL)
9334 got = sec->output_section->vma + sec->output_offset + 0x8000;
066f4018
AM
9335
9336 local_syms = NULL;
9337 symtab_hdr = &elf_symtab_hdr (ibfd);
9338
9339 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9340 {
9341 if (sec->reloc_count == 0
903b777d 9342 || !ppc64_elf_section_data (sec)->has_optrel
066f4018
AM
9343 || discarded_section (sec))
9344 continue;
9345
9346 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9347 info->keep_memory);
9348 if (relstart == NULL)
9349 {
9350 got_error_ret:
c9594989 9351 if (symtab_hdr->contents != (unsigned char *) local_syms)
066f4018
AM
9352 free (local_syms);
9353 if (sec != NULL
066f4018
AM
9354 && elf_section_data (sec)->relocs != relstart)
9355 free (relstart);
9356 return FALSE;
9357 }
9358
9359 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9360 {
9361 enum elf_ppc64_reloc_type r_type;
9362 unsigned long r_symndx;
9363 Elf_Internal_Sym *sym;
9364 asection *sym_sec;
9365 struct elf_link_hash_entry *h;
9366 struct got_entry *ent;
133a1f60 9367 bfd_vma val, pc;
4a421c53 9368 unsigned char buf[8];
066f4018 9369 unsigned int insn;
903b777d 9370 enum {no_check, check_lo, check_ha} insn_check;
066f4018
AM
9371
9372 r_type = ELF64_R_TYPE (rel->r_info);
903b777d
AM
9373 switch (r_type)
9374 {
9375 default:
9376 insn_check = no_check;
9377 break;
9378
9379 case R_PPC64_PLT16_HA:
9380 case R_PPC64_GOT_TLSLD16_HA:
9381 case R_PPC64_GOT_TLSGD16_HA:
9382 case R_PPC64_GOT_TPREL16_HA:
9383 case R_PPC64_GOT_DTPREL16_HA:
9384 case R_PPC64_GOT16_HA:
9385 case R_PPC64_TOC16_HA:
9386 insn_check = check_ha;
9387 break;
9388
9389 case R_PPC64_PLT16_LO:
9390 case R_PPC64_PLT16_LO_DS:
9391 case R_PPC64_GOT_TLSLD16_LO:
9392 case R_PPC64_GOT_TLSGD16_LO:
9393 case R_PPC64_GOT_TPREL16_LO_DS:
9394 case R_PPC64_GOT_DTPREL16_LO_DS:
9395 case R_PPC64_GOT16_LO:
9396 case R_PPC64_GOT16_LO_DS:
9397 case R_PPC64_TOC16_LO:
9398 case R_PPC64_TOC16_LO_DS:
9399 insn_check = check_lo;
9400 break;
9401 }
9402
9403 if (insn_check != no_check)
9404 {
9405 bfd_vma off = rel->r_offset & ~3;
9406
9407 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9408 goto got_error_ret;
9409
9410 insn = bfd_get_32 (ibfd, buf);
9411 if (insn_check == check_lo
9412 ? !ok_lo_toc_insn (insn, r_type)
2365f8d7 9413 : ((insn & ((0x3fu << 26) | 0x1f << 16))
903b777d
AM
9414 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9415 {
9416 char str[12];
9417
9418 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9419 sprintf (str, "%#08x", insn);
9420 info->callbacks->einfo
9421 /* xgettext:c-format */
9422 (_("%H: got/toc optimization is not supported for"
9423 " %s instruction\n"),
9424 ibfd, sec, rel->r_offset & ~3, str);
9425 continue;
9426 }
9427 }
9428
066f4018
AM
9429 switch (r_type)
9430 {
bb22a418
AM
9431 /* Note that we don't delete GOT entries for
9432 R_PPC64_GOT16_DS since we'd need a lot more
9433 analysis. For starters, the preliminary layout is
9434 before the GOT, PLT, dynamic sections and stubs are
9435 laid out. Then we'd need to allow for changes in
9436 distance between sections caused by alignment. */
066f4018
AM
9437 default:
9438 continue;
9439
066f4018
AM
9440 case R_PPC64_GOT16_HA:
9441 case R_PPC64_GOT16_LO_DS:
4a421c53 9442 case R_PPC64_GOT_PCREL34:
066f4018
AM
9443 break;
9444 }
9445
9446 r_symndx = ELF64_R_SYM (rel->r_info);
9447 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9448 r_symndx, ibfd))
9449 goto got_error_ret;
9450
6d5554a6
AM
9451 if (sym_sec == NULL
9452 || sym_sec->output_section == NULL
9453 || discarded_section (sym_sec))
9454 continue;
9455
06507dab
AM
9456 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9457 continue;
9458
066f4018
AM
9459 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9460 continue;
9461
9462 if (h != NULL)
9463 val = h->root.u.def.value;
9464 else
9465 val = sym->st_value;
133a1f60 9466 val += rel->r_addend;
066f4018
AM
9467 val += sym_sec->output_section->vma + sym_sec->output_offset;
9468
bb22a418
AM
9469/* Fudge factor to allow for the fact that the preliminary layout
9470 isn't exact. Reduce limits by this factor. */
9471#define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9472
066f4018
AM
9473 switch (r_type)
9474 {
9475 default:
9476 continue;
9477
066f4018 9478 case R_PPC64_GOT16_HA:
bb22a418
AM
9479 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9480 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9481 continue;
9482
9483 if (!bfd_get_section_contents (ibfd, sec, buf,
9484 rel->r_offset & ~3, 4))
9485 goto got_error_ret;
9486 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9487 if (((insn & ((0x3fu << 26) | 0x1f << 16))
066f4018
AM
9488 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9489 continue;
9490 break;
9491
9492 case R_PPC64_GOT16_LO_DS:
bb22a418
AM
9493 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9494 >= LIMIT_ADJUST (0x100000000ULL))
066f4018
AM
9495 continue;
9496 if (!bfd_get_section_contents (ibfd, sec, buf,
9497 rel->r_offset & ~3, 4))
9498 goto got_error_ret;
9499 insn = bfd_get_32 (ibfd, buf);
2365f8d7 9500 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
066f4018
AM
9501 continue;
9502 break;
4a421c53
AM
9503
9504 case R_PPC64_GOT_PCREL34:
9505 pc = rel->r_offset;
9506 pc += sec->output_section->vma + sec->output_offset;
bb22a418
AM
9507 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9508 >= LIMIT_ADJUST (1ULL << 34))
4a421c53
AM
9509 continue;
9510 if (!bfd_get_section_contents (ibfd, sec, buf,
9511 rel->r_offset & ~3, 8))
9512 goto got_error_ret;
9513 insn = bfd_get_32 (ibfd, buf);
9514 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9515 continue;
9516 insn = bfd_get_32 (ibfd, buf + 4);
2365f8d7 9517 if ((insn & (0x3fu << 26)) != 57u << 26)
4a421c53
AM
9518 continue;
9519 break;
066f4018 9520 }
bb22a418 9521#undef LIMIT_ADJUST
066f4018
AM
9522
9523 if (h != NULL)
9524 ent = h->got.glist;
9525 else
9526 {
9527 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9528 ent = local_got_ents[r_symndx];
9529 }
9530 for (; ent != NULL; ent = ent->next)
133a1f60 9531 if (ent->addend == rel->r_addend
066f4018
AM
9532 && ent->owner == ibfd
9533 && ent->tls_type == 0)
9534 break;
9535 BFD_ASSERT (ent && ent->got.refcount > 0);
9536 ent->got.refcount -= 1;
9537 }
9538
9539 if (elf_section_data (sec)->relocs != relstart)
9540 free (relstart);
9541 }
9542
9543 if (local_syms != NULL
9544 && symtab_hdr->contents != (unsigned char *) local_syms)
9545 {
9546 if (!info->keep_memory)
9547 free (local_syms);
9548 else
9549 symtab_hdr->contents = (unsigned char *) local_syms;
9550 }
9551 }
9552
c5614fa4
AM
9553 return TRUE;
9554}
9555
1bbe0902
AM
9556/* Return true iff input section I references the TOC using
9557 instructions limited to +/-32k offsets. */
9558
9559bfd_boolean
9560ppc64_elf_has_small_toc_reloc (asection *i)
9561{
9562 return (is_ppc64_elf (i->owner)
9563 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9564}
9565
927be08e
AM
9566/* Allocate space for one GOT entry. */
9567
9568static void
9569allocate_got (struct elf_link_hash_entry *h,
9570 struct bfd_link_info *info,
9571 struct got_entry *gent)
9572{
9573 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ed7007c1 9574 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
927be08e
AM
9575 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9576 ? 16 : 8);
9577 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9578 ? 2 : 1) * sizeof (Elf64_External_Rela);
9579 asection *got = ppc64_elf_tdata (gent->owner)->got;
9580
9581 gent->got.offset = got->size;
9582 got->size += entsize;
9583
19e08130 9584 if (h->type == STT_GNU_IFUNC)
927be08e 9585 {
33e44f2e 9586 htab->elf.irelplt->size += rentsize;
19e08130 9587 htab->got_reli_size += rentsize;
927be08e 9588 }
f15d0b54 9589 else if (((bfd_link_pic (info)
f749f26e 9590 && !(gent->tls_type != 0
f15d0b54
AM
9591 && bfd_link_executable (info)
9592 && SYMBOL_REFERENCES_LOCAL (info, h)))
f0158f44
AM
9593 || (htab->elf.dynamic_sections_created
9594 && h->dynindx != -1
9595 && !SYMBOL_REFERENCES_LOCAL (info, h)))
21d68fcd 9596 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
927be08e 9597 {
19e08130 9598 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9599 relgot->size += rentsize;
927be08e
AM
9600 }
9601}
9602
7865406b
AM
9603/* This function merges got entries in the same toc group. */
9604
9605static void
9606merge_got_entries (struct got_entry **pent)
9607{
9608 struct got_entry *ent, *ent2;
9609
9610 for (ent = *pent; ent != NULL; ent = ent->next)
9611 if (!ent->is_indirect)
9612 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9613 if (!ent2->is_indirect
9614 && ent2->addend == ent->addend
9615 && ent2->tls_type == ent->tls_type
9616 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9617 {
9618 ent2->is_indirect = TRUE;
9619 ent2->got.ent = ent;
9620 }
9621}
9622
46434633 9623/* If H is undefined, make it dynamic if that makes sense. */
f0158f44
AM
9624
9625static bfd_boolean
46434633
AM
9626ensure_undef_dynamic (struct bfd_link_info *info,
9627 struct elf_link_hash_entry *h)
f0158f44
AM
9628{
9629 struct elf_link_hash_table *htab = elf_hash_table (info);
9630
9631 if (htab->dynamic_sections_created
46434633
AM
9632 && ((info->dynamic_undefined_weak != 0
9633 && h->root.type == bfd_link_hash_undefweak)
9634 || h->root.type == bfd_link_hash_undefined)
f0158f44
AM
9635 && h->dynindx == -1
9636 && !h->forced_local
9637 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9638 return bfd_elf_link_record_dynamic_symbol (info, h);
9639 return TRUE;
9640}
9641
30845f11
AM
9642/* Choose whether to use htab->iplt or htab->pltlocal rather than the
9643 usual htab->elf.splt section for a PLT entry. */
9644
9645static inline
9646bfd_boolean use_local_plt (struct bfd_link_info *info,
9647 struct elf_link_hash_entry *h)
9648{
9649 return (h == NULL
9650 || h->dynindx == -1
9651 || !elf_hash_table (info)->dynamic_sections_created);
9652}
9653
65f38f15
AM
9654/* Allocate space in .plt, .got and associated reloc sections for
9655 dynamic relocs. */
5bd4f169 9656
b34976b6 9657static bfd_boolean
4ce794b7 9658allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9659{
65f38f15
AM
9660 struct bfd_link_info *info;
9661 struct ppc_link_hash_table *htab;
5bd4f169 9662 asection *s;
65f38f15 9663 struct ppc_link_hash_entry *eh;
0b8bcf0d 9664 struct got_entry **pgent, *gent;
5bd4f169 9665
e92d460e 9666 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9667 return TRUE;
5bd4f169 9668
65f38f15
AM
9669 info = (struct bfd_link_info *) inf;
9670 htab = ppc_hash_table (info);
4dfe6ac6
NC
9671 if (htab == NULL)
9672 return FALSE;
5bd4f169 9673
ed7007c1 9674 eh = ppc_elf_hash_entry (h);
951fd09b
AM
9675 /* Run through the TLS GD got entries first if we're changing them
9676 to TPREL. */
b00a0a86 9677 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
951fd09b
AM
9678 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9679 if (gent->got.refcount > 0
9680 && (gent->tls_type & TLS_GD) != 0)
9681 {
9682 /* This was a GD entry that has been converted to TPREL. If
9683 there happens to be a TPREL entry we can use that one. */
9684 struct got_entry *ent;
9685 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9686 if (ent->got.refcount > 0
9687 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9688 && ent->addend == gent->addend
9689 && ent->owner == gent->owner)
951fd09b
AM
9690 {
9691 gent->got.refcount = 0;
9692 break;
9693 }
9694
9695 /* If not, then we'll be using our own TPREL entry. */
9696 if (gent->got.refcount != 0)
9697 gent->tls_type = TLS_TLS | TLS_TPREL;
9698 }
9699
7865406b
AM
9700 /* Remove any list entry that won't generate a word in the GOT before
9701 we call merge_got_entries. Otherwise we risk merging to empty
9702 entries. */
0b8bcf0d
AM
9703 pgent = &h->got.glist;
9704 while ((gent = *pgent) != NULL)
411e1bfb 9705 if (gent->got.refcount > 0)
7865406b
AM
9706 {
9707 if ((gent->tls_type & TLS_LD) != 0
f749f26e 9708 && SYMBOL_REFERENCES_LOCAL (info, h))
7865406b
AM
9709 {
9710 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9711 *pgent = gent->next;
9712 }
9713 else
9714 pgent = &gent->next;
9715 }
9716 else
9717 *pgent = gent->next;
9718
9719 if (!htab->do_multi_toc)
9720 merge_got_entries (&h->got.glist);
9721
9722 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9723 if (!gent->is_indirect)
411e1bfb 9724 {
ec73ddcd
AM
9725 /* Ensure we catch all the cases where this symbol should
9726 be made dynamic. */
46434633 9727 if (!ensure_undef_dynamic (info, h))
f0158f44 9728 return FALSE;
65f38f15 9729
0c8d6e5c 9730 if (!is_ppc64_elf (gent->owner))
927be08e 9731 abort ();
0ffa91dd 9732
927be08e 9733 allocate_got (h, info, gent);
411e1bfb 9734 }
65f38f15 9735
954b63d4
AM
9736 /* If no dynamic sections we can't have dynamic relocs, except for
9737 IFUNCs which are handled even in static executables. */
8a2058b5
AM
9738 if (!htab->elf.dynamic_sections_created
9739 && h->type != STT_GNU_IFUNC)
190eb1dd 9740 h->dyn_relocs = NULL;
8a2058b5 9741
529fe20e
AM
9742 /* Discard relocs on undefined symbols that must be local. */
9743 else if (h->root.type == bfd_link_hash_undefined
9744 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
190eb1dd 9745 h->dyn_relocs = NULL;
529fe20e 9746
954b63d4
AM
9747 /* Also discard relocs on undefined weak syms with non-default
9748 visibility, or when dynamic_undefined_weak says so. */
21d68fcd 9749 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 9750 h->dyn_relocs = NULL;
954b63d4 9751
190eb1dd 9752 if (h->dyn_relocs != NULL)
65f38f15 9753 {
8a2058b5
AM
9754 struct elf_dyn_relocs *p, **pp;
9755
57e7d118
AM
9756 /* In the shared -Bsymbolic case, discard space allocated for
9757 dynamic pc-relative relocs against symbols which turn out to
9758 be defined in regular objects. For the normal shared case,
9759 discard space for relocs that have become local due to symbol
9760 visibility changes. */
57e7d118 9761 if (bfd_link_pic (info))
65f38f15 9762 {
57e7d118
AM
9763 /* Relocs that use pc_count are those that appear on a call
9764 insn, or certain REL relocs (see must_be_dyn_reloc) that
9765 can be generated via assembly. We want calls to
9766 protected symbols to resolve directly to the function
9767 rather than going via the plt. If people want function
9768 pointer comparisons to work as expected then they should
9769 avoid writing weird assembly. */
9770 if (SYMBOL_CALLS_LOCAL (info, h))
9771 {
190eb1dd 9772 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
57e7d118
AM
9773 {
9774 p->count -= p->pc_count;
9775 p->pc_count = 0;
9776 if (p->count == 0)
9777 *pp = p->next;
9778 else
9779 pp = &p->next;
9780 }
9781 }
65f38f15 9782
190eb1dd 9783 if (h->dyn_relocs != NULL)
5bd4f169 9784 {
ec73ddcd
AM
9785 /* Ensure we catch all the cases where this symbol
9786 should be made dynamic. */
46434633 9787 if (!ensure_undef_dynamic (info, h))
f0158f44 9788 return FALSE;
5bd4f169 9789 }
65f38f15 9790 }
ec73ddcd
AM
9791
9792 /* For a fixed position executable, discard space for
9793 relocs against symbols which are not dynamic. */
9794 else if (h->type != STT_GNU_IFUNC)
57e7d118 9795 {
529fe20e
AM
9796 if (h->dynamic_adjusted
9797 && !h->def_regular
9798 && !ELF_COMMON_DEF_P (h))
f0158f44 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))
f0158f44 9803 return FALSE;
dfbb6ac9 9804
ec73ddcd 9805 /* But if that didn't work out, discard dynamic relocs. */
f0158f44 9806 if (h->dynindx == -1)
190eb1dd 9807 h->dyn_relocs = NULL;
f0158f44
AM
9808 }
9809 else
190eb1dd 9810 h->dyn_relocs = NULL;
57e7d118
AM
9811 }
9812
9813 /* Finally, allocate space. */
190eb1dd 9814 for (p = h->dyn_relocs; p != NULL; p = p->next)
57e7d118
AM
9815 {
9816 asection *sreloc = elf_section_data (p->sec)->sreloc;
9817 if (eh->elf.type == STT_GNU_IFUNC)
9818 sreloc = htab->elf.irelplt;
9819 sreloc->size += p->count * sizeof (Elf64_External_Rela);
dfbb6ac9 9820 }
65f38f15 9821 }
57e7d118 9822
2d7ad24e
AM
9823 /* We might need a PLT entry when the symbol
9824 a) is dynamic, or
9825 b) is an ifunc, or
9826 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9827 d) has plt16 relocs and we are linking statically. */
9828 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9829 || h->type == STT_GNU_IFUNC
9830 || (h->needs_plt && h->dynamic_adjusted)
9831 || (h->needs_plt
9832 && h->def_regular
9833 && !htab->elf.dynamic_sections_created
3e04d765 9834 && !htab->can_convert_all_inline_plt
ed7007c1 9835 && (ppc_elf_hash_entry (h)->tls_mask
2d7ad24e 9836 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
65f38f15 9837 {
57e7d118
AM
9838 struct plt_entry *pent;
9839 bfd_boolean doneone = FALSE;
9840 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9841 if (pent->plt.refcount > 0)
9842 {
30845f11 9843 if (use_local_plt (info, h))
57e7d118 9844 {
2d7ad24e
AM
9845 if (h->type == STT_GNU_IFUNC)
9846 {
9847 s = htab->elf.iplt;
9848 pent->plt.offset = s->size;
9849 s->size += PLT_ENTRY_SIZE (htab);
9850 s = htab->elf.irelplt;
9851 }
9852 else
9853 {
9854 s = htab->pltlocal;
9855 pent->plt.offset = s->size;
9856 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9857 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9858 }
57e7d118
AM
9859 }
9860 else
9861 {
9862 /* If this is the first .plt entry, make room for the special
9863 first entry. */
9864 s = htab->elf.splt;
9865 if (s->size == 0)
9866 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
65f38f15 9867
57e7d118 9868 pent->plt.offset = s->size;
65f38f15 9869
57e7d118
AM
9870 /* Make room for this entry. */
9871 s->size += PLT_ENTRY_SIZE (htab);
65f38f15 9872
57e7d118
AM
9873 /* Make room for the .glink code. */
9874 s = htab->glink;
9875 if (s->size == 0)
9e390558 9876 s->size += GLINK_PLTRESOLVE_SIZE (htab);
57e7d118
AM
9877 if (htab->opd_abi)
9878 {
9879 /* We need bigger stubs past index 32767. */
9e390558 9880 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
57e7d118
AM
9881 s->size += 4;
9882 s->size += 2*4;
9883 }
9884 else
9885 s->size += 4;
65f38f15 9886
57e7d118
AM
9887 /* We also need to make an entry in the .rela.plt section. */
9888 s = htab->elf.srelplt;
9889 }
2d7ad24e
AM
9890 if (s != NULL)
9891 s->size += sizeof (Elf64_External_Rela);
57e7d118
AM
9892 doneone = TRUE;
9893 }
9894 else
9895 pent->plt.offset = (bfd_vma) -1;
9896 if (!doneone)
9897 {
9898 h->plt.plist = NULL;
9899 h->needs_plt = 0;
9900 }
65f38f15 9901 }
57e7d118 9902 else
65f38f15 9903 {
57e7d118
AM
9904 h->plt.plist = NULL;
9905 h->needs_plt = 0;
65f38f15
AM
9906 }
9907
b34976b6 9908 return TRUE;
65f38f15
AM
9909}
9910
9e390558
AM
9911#define PPC_LO(v) ((v) & 0xffff)
9912#define PPC_HI(v) (((v) >> 16) & 0xffff)
9913#define PPC_HA(v) PPC_HI ((v) + 0x8000)
04bdff6a
AM
9914#define D34(v) \
9915 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9916#define HA34(v) ((v + (1ULL << 33)) >> 34)
9e390558 9917
a345bc8d
AM
9918/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9919 to set up space for global entry stubs. These are put in glink,
9920 after the branch table. */
65f38f15 9921
b34976b6 9922static bfd_boolean
a345bc8d 9923size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
65f38f15 9924{
a345bc8d
AM
9925 struct bfd_link_info *info;
9926 struct ppc_link_hash_table *htab;
9927 struct plt_entry *pent;
9e390558 9928 asection *s, *plt;
65f38f15 9929
a345bc8d
AM
9930 if (h->root.type == bfd_link_hash_indirect)
9931 return TRUE;
65f38f15 9932
a345bc8d
AM
9933 if (!h->pointer_equality_needed)
9934 return TRUE;
65f38f15 9935
a345bc8d
AM
9936 if (h->def_regular)
9937 return TRUE;
65f38f15 9938
a345bc8d
AM
9939 info = inf;
9940 htab = ppc_hash_table (info);
9941 if (htab == NULL)
9942 return FALSE;
9943
9e390558
AM
9944 s = htab->global_entry;
9945 plt = htab->elf.splt;
a345bc8d
AM
9946 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9947 if (pent->plt.offset != (bfd_vma) -1
9948 && pent->addend == 0)
9949 {
afe397ea
AM
9950 /* For ELFv2, if this symbol is not defined in a regular file
9951 and we are not generating a shared library or pie, then we
9952 need to define the symbol in the executable on a call stub.
9953 This is to avoid text relocations. */
9e390558
AM
9954 bfd_vma off, stub_align, stub_off, stub_size;
9955 unsigned int align_power;
9956
9957 stub_size = 16;
9958 stub_off = s->size;
9959 if (htab->params->plt_stub_align >= 0)
9960 align_power = htab->params->plt_stub_align;
9961 else
9962 align_power = -htab->params->plt_stub_align;
9963 /* Setting section alignment is delayed until we know it is
9964 non-empty. Otherwise the .text output section will be
9965 aligned at least to plt_stub_align even when no global
9966 entry stubs are needed. */
9967 if (s->alignment_power < align_power)
9968 s->alignment_power = align_power;
9969 stub_align = (bfd_vma) 1 << align_power;
9970 if (htab->params->plt_stub_align >= 0
9971 || ((((stub_off + stub_size - 1) & -stub_align)
9972 - (stub_off & -stub_align))
9973 > ((stub_size - 1) & -stub_align)))
9974 stub_off = (stub_off + stub_align - 1) & -stub_align;
9975 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9976 off -= stub_off + s->output_offset + s->output_section->vma;
9977 /* Note that for --plt-stub-align negative we have a possible
9978 dependency between stub offset and size. Break that
9979 dependency by assuming the max stub size when calculating
9980 the stub offset. */
9981 if (PPC_HA (off) == 0)
9982 stub_size -= 4;
8a2058b5 9983 h->root.type = bfd_link_hash_defined;
afe397ea 9984 h->root.u.def.section = s;
9e390558
AM
9985 h->root.u.def.value = stub_off;
9986 s->size = stub_off + stub_size;
a345bc8d
AM
9987 break;
9988 }
9989 return TRUE;
9990}
9991
65f38f15
AM
9992/* Set the sizes of the dynamic sections. */
9993
b34976b6 9994static bfd_boolean
ee67d69a 9995ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9996 struct bfd_link_info *info)
65f38f15
AM
9997{
9998 struct ppc_link_hash_table *htab;
9999 bfd *dynobj;
10000 asection *s;
b34976b6 10001 bfd_boolean relocs;
65f38f15 10002 bfd *ibfd;
7865406b 10003 struct got_entry *first_tlsld;
65f38f15
AM
10004
10005 htab = ppc_hash_table (info);
4dfe6ac6
NC
10006 if (htab == NULL)
10007 return FALSE;
10008
65f38f15
AM
10009 dynobj = htab->elf.dynobj;
10010 if (dynobj == NULL)
10011 abort ();
10012
10013 if (htab->elf.dynamic_sections_created)
10014 {
10015 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 10016 if (bfd_link_executable (info) && !info->nointerp)
65f38f15 10017 {
3d4d4302 10018 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
10019 if (s == NULL)
10020 abort ();
eea6121a 10021 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
10022 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10023 }
10024 }
10025
10026 /* Set up .got offsets for local syms, and space for local dynamic
10027 relocs. */
c72f2fb2 10028 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
65f38f15 10029 {
411e1bfb
AM
10030 struct got_entry **lgot_ents;
10031 struct got_entry **end_lgot_ents;
e054468f
AM
10032 struct plt_entry **local_plt;
10033 struct plt_entry **end_local_plt;
f961d9dd 10034 unsigned char *lgot_masks;
65f38f15
AM
10035 bfd_size_type locsymcount;
10036 Elf_Internal_Shdr *symtab_hdr;
65f38f15 10037
0c8d6e5c 10038 if (!is_ppc64_elf (ibfd))
65f38f15
AM
10039 continue;
10040
10041 for (s = ibfd->sections; s != NULL; s = s->next)
10042 {
19e08130 10043 struct ppc_dyn_relocs *p;
65f38f15 10044
6edfbbad 10045 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 10046 {
ec338859
AM
10047 if (!bfd_is_abs_section (p->sec)
10048 && bfd_is_abs_section (p->sec->output_section))
10049 {
10050 /* Input section has been discarded, either because
10051 it is a copy of a linkonce section or due to
10052 linker script /DISCARD/, so we'll be discarding
10053 the relocs too. */
10054 }
248866a8 10055 else if (p->count != 0)
ec338859 10056 {
19e08130
AM
10057 asection *srel = elf_section_data (p->sec)->sreloc;
10058 if (p->ifunc)
33e44f2e 10059 srel = htab->elf.irelplt;
eea6121a 10060 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
10061 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10062 info->flags |= DF_TEXTREL;
ec338859 10063 }
65f38f15
AM
10064 }
10065 }
10066
411e1bfb
AM
10067 lgot_ents = elf_local_got_ents (ibfd);
10068 if (!lgot_ents)
65f38f15
AM
10069 continue;
10070
0ffa91dd 10071 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 10072 locsymcount = symtab_hdr->sh_info;
411e1bfb 10073 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
10074 local_plt = (struct plt_entry **) end_lgot_ents;
10075 end_local_plt = local_plt + locsymcount;
f961d9dd 10076 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 10077 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 10078 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 10079 {
0b8bcf0d 10080 struct got_entry **pent, *ent;
411e1bfb 10081
0b8bcf0d
AM
10082 pent = lgot_ents;
10083 while ((ent = *pent) != NULL)
411e1bfb
AM
10084 if (ent->got.refcount > 0)
10085 {
e7b938ca 10086 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 10087 {
927be08e 10088 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 10089 *pent = ent->next;
411e1bfb
AM
10090 }
10091 else
10092 {
19e08130
AM
10093 unsigned int ent_size = 8;
10094 unsigned int rel_size = sizeof (Elf64_External_Rela);
10095
eea6121a 10096 ent->got.offset = s->size;
e7b938ca 10097 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 10098 {
19e08130
AM
10099 ent_size *= 2;
10100 rel_size *= 2;
10101 }
10102 s->size += ent_size;
37da22e5 10103 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 10104 {
33e44f2e 10105 htab->elf.irelplt->size += rel_size;
19e08130
AM
10106 htab->got_reli_size += rel_size;
10107 }
93370e8e
AM
10108 else if (bfd_link_pic (info)
10109 && !(ent->tls_type != 0
10110 && bfd_link_executable (info)))
19e08130
AM
10111 {
10112 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10113 srel->size += rel_size;
927be08e 10114 }
0b8bcf0d 10115 pent = &ent->next;
411e1bfb
AM
10116 }
10117 }
10118 else
0b8bcf0d 10119 *pent = ent->next;
65f38f15 10120 }
e054468f 10121
2d7ad24e
AM
10122 /* Allocate space for plt calls to local syms. */
10123 lgot_masks = (unsigned char *) end_local_plt;
10124 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
e054468f
AM
10125 {
10126 struct plt_entry *ent;
10127
10128 for (ent = *local_plt; ent != NULL; ent = ent->next)
10129 if (ent->plt.refcount > 0)
10130 {
2d7ad24e
AM
10131 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10132 {
10133 s = htab->elf.iplt;
10134 ent->plt.offset = s->size;
10135 s->size += PLT_ENTRY_SIZE (htab);
10136 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10137 }
3e04d765
AM
10138 else if (htab->can_convert_all_inline_plt
10139 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
2d7ad24e
AM
10140 ent->plt.offset = (bfd_vma) -1;
10141 else
10142 {
10143 s = htab->pltlocal;
10144 ent->plt.offset = s->size;
10145 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10146 if (bfd_link_pic (info))
10147 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10148 }
e054468f
AM
10149 }
10150 else
10151 ent->plt.offset = (bfd_vma) -1;
10152 }
65f38f15
AM
10153 }
10154
10155 /* Allocate global sym .plt and .got entries, and space for global
10156 sym dynamic relocs. */
4ce794b7 10157 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
a345bc8d 10158
0e1862bb 10159 if (!htab->opd_abi && !bfd_link_pic (info))
a345bc8d 10160 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
65f38f15 10161
7865406b 10162 first_tlsld = NULL;
c72f2fb2 10163 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
102890f0 10164 {
7865406b
AM
10165 struct got_entry *ent;
10166
0c8d6e5c 10167 if (!is_ppc64_elf (ibfd))
102890f0
AM
10168 continue;
10169
7865406b
AM
10170 ent = ppc64_tlsld_got (ibfd);
10171 if (ent->got.refcount > 0)
102890f0 10172 {
7865406b 10173 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 10174 {
7865406b
AM
10175 ent->is_indirect = TRUE;
10176 ent->got.ent = first_tlsld;
10177 }
10178 else
10179 {
10180 if (first_tlsld == NULL)
10181 first_tlsld = ent;
10182 s = ppc64_elf_tdata (ibfd)->got;
10183 ent->got.offset = s->size;
10184 ent->owner = ibfd;
10185 s->size += 16;
f749f26e 10186 if (bfd_link_dll (info))
7865406b
AM
10187 {
10188 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10189 srel->size += sizeof (Elf64_External_Rela);
10190 }
102890f0
AM
10191 }
10192 }
10193 else
7865406b 10194 ent->got.offset = (bfd_vma) -1;
102890f0
AM
10195 }
10196
65f38f15
AM
10197 /* We now have determined the sizes of the various dynamic sections.
10198 Allocate memory for them. */
b34976b6 10199 relocs = FALSE;
65f38f15
AM
10200 for (s = dynobj->sections; s != NULL; s = s->next)
10201 {
10202 if ((s->flags & SEC_LINKER_CREATED) == 0)
10203 continue;
10204
4ce794b7 10205 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
10206 /* These haven't been allocated yet; don't strip. */
10207 continue;
33e44f2e
AM
10208 else if (s == htab->elf.sgot
10209 || s == htab->elf.splt
10210 || s == htab->elf.iplt
2d7ad24e 10211 || s == htab->pltlocal
c456f082 10212 || s == htab->glink
9e390558 10213 || s == htab->global_entry
5474d94f
AM
10214 || s == htab->elf.sdynbss
10215 || s == htab->elf.sdynrelro)
65f38f15
AM
10216 {
10217 /* Strip this section if we don't need it; see the
10218 comment below. */
5bd4f169 10219 }
58d180e8
AM
10220 else if (s == htab->glink_eh_frame)
10221 {
10222 if (!bfd_is_abs_section (s->output_section))
10223 /* Not sized yet. */
10224 continue;
10225 }
70cc837d 10226 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 10227 {
c456f082 10228 if (s->size != 0)
5bd4f169 10229 {
33e44f2e 10230 if (s != htab->elf.srelplt)
b34976b6 10231 relocs = TRUE;
5bd4f169
AM
10232
10233 /* We use the reloc_count field as a counter if we need
10234 to copy relocs into the output file. */
10235 s->reloc_count = 0;
10236 }
10237 }
65f38f15 10238 else
5bd4f169
AM
10239 {
10240 /* It's not one of our sections, so don't allocate space. */
10241 continue;
10242 }
10243
eea6121a 10244 if (s->size == 0)
5bd4f169 10245 {
c456f082
AM
10246 /* If we don't need this section, strip it from the
10247 output file. This is mostly to handle .rela.bss and
10248 .rela.plt. We must create both sections in
10249 create_dynamic_sections, because they must be created
10250 before the linker maps input sections to output
10251 sections. The linker does that before
10252 adjust_dynamic_symbol is called, and it is that
10253 function which decides whether anything needs to go
10254 into these sections. */
8423293d 10255 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
10256 continue;
10257 }
10258
06bcf541
AM
10259 if (bfd_is_abs_section (s->output_section))
10260 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10261 s->name);
10262
c456f082 10263 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
10264 continue;
10265
65f38f15
AM
10266 /* Allocate memory for the section contents. We use bfd_zalloc
10267 here in case unused entries are not reclaimed before the
10268 section's contents are written out. This should not happen,
411e1bfb
AM
10269 but this way if it does we get a R_PPC64_NONE reloc in .rela
10270 sections instead of garbage.
10271 We also rely on the section contents being zero when writing
5474d94f 10272 the GOT and .dynrelro. */
eea6121a 10273 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 10274 if (s->contents == NULL)
b34976b6 10275 return FALSE;
5bd4f169
AM
10276 }
10277
c72f2fb2 10278 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
e717da7e 10279 {
0c8d6e5c 10280 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
10281 continue;
10282
e717da7e 10283 s = ppc64_elf_tdata (ibfd)->got;
33e44f2e 10284 if (s != NULL && s != htab->elf.sgot)
e717da7e 10285 {
eea6121a 10286 if (s->size == 0)
8423293d 10287 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10288 else
10289 {
eea6121a 10290 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10291 if (s->contents == NULL)
10292 return FALSE;
10293 }
10294 }
10295 s = ppc64_elf_tdata (ibfd)->relgot;
10296 if (s != NULL)
10297 {
eea6121a 10298 if (s->size == 0)
8423293d 10299 s->flags |= SEC_EXCLUDE;
e717da7e
AM
10300 else
10301 {
eea6121a 10302 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
10303 if (s->contents == NULL)
10304 return FALSE;
10305 relocs = TRUE;
10306 s->reloc_count = 0;
10307 }
10308 }
10309 }
10310
e86ce104 10311 if (htab->elf.dynamic_sections_created)
5bd4f169 10312 {
e8910a83
AM
10313 bfd_boolean tls_opt;
10314
5bd4f169
AM
10315 /* Add some entries to the .dynamic section. We fill in the
10316 values later, in ppc64_elf_finish_dynamic_sections, but we
10317 must add the entries now so that we get the correct size for
10318 the .dynamic section. The DT_DEBUG entry is filled in by the
10319 dynamic linker and used by the debugger. */
dc810e39 10320#define add_dynamic_entry(TAG, VAL) \
5a580b3a 10321 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 10322
0e1862bb 10323 if (bfd_link_executable (info))
5bd4f169 10324 {
dc810e39 10325 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 10326 return FALSE;
5bd4f169
AM
10327 }
10328
33e44f2e 10329 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
5bd4f169 10330 {
dc810e39
AM
10331 if (!add_dynamic_entry (DT_PLTGOT, 0)
10332 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10333 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
10334 || !add_dynamic_entry (DT_JMPREL, 0)
10335 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 10336 return FALSE;
5bd4f169
AM
10337 }
10338
ee67d69a 10339 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
10340 {
10341 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10342 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 10343 return FALSE;
19397422
AM
10344 }
10345
7c9cf415 10346 tls_opt = (htab->params->tls_get_addr_opt
9e7028aa
AM
10347 && ((htab->tls_get_addr_fd != NULL
10348 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10349 || (htab->tga_desc_fd != NULL
10350 && htab->tga_desc_fd->elf.plt.plist != NULL)));
e8910a83
AM
10351 if (tls_opt || !htab->opd_abi)
10352 {
10353 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10354 return FALSE;
10355 }
a7f2871e 10356
5bd4f169
AM
10357 if (relocs)
10358 {
dc810e39
AM
10359 if (!add_dynamic_entry (DT_RELA, 0)
10360 || !add_dynamic_entry (DT_RELASZ, 0)
10361 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 10362 return FALSE;
5bd4f169 10363
65f38f15
AM
10364 /* If any dynamic relocs apply to a read-only section,
10365 then we need a DT_TEXTREL entry. */
248866a8 10366 if ((info->flags & DF_TEXTREL) == 0)
d49e5065
L
10367 elf_link_hash_traverse (&htab->elf,
10368 _bfd_elf_maybe_set_textrel, info);
5bd4f169 10369
65f38f15 10370 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 10371 {
65f38f15 10372 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 10373 return FALSE;
5bd4f169 10374 }
5bd4f169 10375 }
5bd4f169 10376 }
65f38f15 10377#undef add_dynamic_entry
5bd4f169 10378
b34976b6 10379 return TRUE;
5bd4f169
AM
10380}
10381
a345bc8d
AM
10382/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10383
10384static bfd_boolean
10385ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10386{
10387 if (h->plt.plist != NULL
10388 && !h->def_regular
10389 && !h->pointer_equality_needed)
10390 return FALSE;
10391
10392 return _bfd_elf_hash_symbol (h);
10393}
10394
721956f4 10395/* Determine the type of stub needed, if any, for a call. */
5bd4f169 10396
4ce794b7
AM
10397static inline enum ppc_stub_type
10398ppc_type_of_stub (asection *input_sec,
10399 const Elf_Internal_Rela *rel,
10400 struct ppc_link_hash_entry **hash,
e054468f 10401 struct plt_entry **plt_ent,
6911b7dc
AM
10402 bfd_vma destination,
10403 unsigned long local_off)
5bd4f169 10404{
721956f4
AM
10405 struct ppc_link_hash_entry *h = *hash;
10406 bfd_vma location;
10407 bfd_vma branch_offset;
10408 bfd_vma max_branch_offset;
4ce794b7 10409 enum elf_ppc64_reloc_type r_type;
5bd4f169 10410
721956f4
AM
10411 if (h != NULL)
10412 {
e054468f 10413 struct plt_entry *ent;
7fe2b9a6 10414 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
10415 if (h->oh != NULL
10416 && h->oh->is_func_descriptor)
7b8f6675
AM
10417 {
10418 fdh = ppc_follow_link (h->oh);
10419 *hash = fdh;
10420 }
8387904d 10421
e054468f
AM
10422 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10423 if (ent->addend == rel->r_addend
10424 && ent->plt.offset != (bfd_vma) -1)
10425 {
e054468f
AM
10426 *plt_ent = ent;
10427 return ppc_stub_plt_call;
10428 }
5bd4f169 10429
7fe2b9a6
AM
10430 /* Here, we know we don't have a plt entry. If we don't have a
10431 either a defined function descriptor or a defined entry symbol
10432 in a regular object file, then it is pointless trying to make
10433 any other type of stub. */
854b41e7
AM
10434 if (!is_static_defined (&fdh->elf)
10435 && !is_static_defined (&h->elf))
721956f4 10436 return ppc_stub_none;
5d1634d7 10437 }
e054468f
AM
10438 else if (elf_local_got_ents (input_sec->owner) != NULL)
10439 {
10440 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10441 struct plt_entry **local_plt = (struct plt_entry **)
10442 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10443 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10444
10445 if (local_plt[r_symndx] != NULL)
10446 {
10447 struct plt_entry *ent;
10448
10449 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10450 if (ent->addend == rel->r_addend
10451 && ent->plt.offset != (bfd_vma) -1)
10452 {
10453 *plt_ent = ent;
10454 return ppc_stub_plt_call;
10455 }
10456 }
10457 }
5d1634d7 10458
721956f4
AM
10459 /* Determine where the call point is. */
10460 location = (input_sec->output_offset
10461 + input_sec->output_section->vma
10462 + rel->r_offset);
5d1634d7 10463
721956f4
AM
10464 branch_offset = destination - location;
10465 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 10466
721956f4
AM
10467 /* Determine if a long branch stub is needed. */
10468 max_branch_offset = 1 << 25;
23cedd1d
AM
10469 if (r_type == R_PPC64_REL14
10470 || r_type == R_PPC64_REL14_BRTAKEN
10471 || r_type == R_PPC64_REL14_BRNTAKEN)
721956f4 10472 max_branch_offset = 1 << 15;
5d1634d7 10473
6911b7dc 10474 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
10475 /* We need a stub. Figure out whether a long_branch or plt_branch
10476 is needed later. */
10477 return ppc_stub_long_branch;
5d1634d7 10478
721956f4 10479 return ppc_stub_none;
5d1634d7
AM
10480}
10481
f891966f
AM
10482/* Gets the address of a label (1:) in r11 and builds an offset in r12,
10483 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10484 . mflr %r12
10485 . bcl 20,31,1f
10486 .1: mflr %r11
10487 . mtlr %r12
05d0e962 10488 . lis %r12,xxx-1b@highest
f891966f 10489 . ori %r12,%r12,xxx-1b@higher
05d0e962 10490 . sldi %r12,%r12,32
f891966f 10491 . oris %r12,%r12,xxx-1b@high
05d0e962 10492 . ori %r12,%r12,xxx-1b@l
f891966f 10493 . add/ldx %r12,%r11,%r12 */
05d0e962
AM
10494
10495static bfd_byte *
10496build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10497{
f891966f
AM
10498 bfd_put_32 (abfd, MFLR_R12, p);
10499 p += 4;
10500 bfd_put_32 (abfd, BCL_20_31, p);
10501 p += 4;
10502 bfd_put_32 (abfd, MFLR_R11, p);
10503 p += 4;
10504 bfd_put_32 (abfd, MTLR_R12, p);
10505 p += 4;
05d0e962
AM
10506 if (off + 0x8000 < 0x10000)
10507 {
10508 if (load)
10509 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10510 else
10511 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10512 p += 4;
10513 }
10514 else if (off + 0x80008000ULL < 0x100000000ULL)
10515 {
10516 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10517 p += 4;
10518 if (load)
10519 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10520 else
10521 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10522 p += 4;
10523 }
10524 else
10525 {
10526 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10527 {
10528 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10529 p += 4;
10530 }
10531 else
10532 {
10533 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10534 p += 4;
10535 if (((off >> 32) & 0xffff) != 0)
10536 {
10537 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10538 p += 4;
10539 }
10540 }
10541 if (((off >> 32) & 0xffffffffULL) != 0)
10542 {
10543 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10544 p += 4;
10545 }
10546 if (PPC_HI (off) != 0)
10547 {
10548 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10549 p += 4;
10550 }
10551 if (PPC_LO (off) != 0)
10552 {
10553 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10554 p += 4;
10555 }
10556 if (load)
10557 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10558 else
10559 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10560 p += 4;
10561 }
10562 return p;
10563}
10564
10565static unsigned int
10566size_offset (bfd_vma off)
10567{
10568 unsigned int size;
10569 if (off + 0x8000 < 0x10000)
10570 size = 4;
10571 else if (off + 0x80008000ULL < 0x100000000ULL)
10572 size = 8;
10573 else
10574 {
10575 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10576 size = 4;
10577 else
10578 {
10579 size = 4;
10580 if (((off >> 32) & 0xffff) != 0)
10581 size += 4;
10582 }
10583 if (((off >> 32) & 0xffffffffULL) != 0)
10584 size += 4;
10585 if (PPC_HI (off) != 0)
10586 size += 4;
10587 if (PPC_LO (off) != 0)
10588 size += 4;
10589 size += 4;
10590 }
f891966f 10591 return size + 16;
05d0e962
AM
10592}
10593
3d58e1fc
AM
10594static unsigned int
10595num_relocs_for_offset (bfd_vma off)
10596{
10597 unsigned int num_rel;
10598 if (off + 0x8000 < 0x10000)
10599 num_rel = 1;
10600 else if (off + 0x80008000ULL < 0x100000000ULL)
10601 num_rel = 2;
10602 else
10603 {
10604 num_rel = 1;
10605 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10606 && ((off >> 32) & 0xffff) != 0)
10607 num_rel += 1;
10608 if (PPC_HI (off) != 0)
10609 num_rel += 1;
10610 if (PPC_LO (off) != 0)
10611 num_rel += 1;
10612 }
10613 return num_rel;
10614}
10615
10616static Elf_Internal_Rela *
10617emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10618 bfd_vma roff, bfd_vma targ, bfd_vma off)
10619{
10620 bfd_vma relative_targ = targ - (roff - 8);
10621 if (bfd_big_endian (info->output_bfd))
10622 roff += 2;
10623 r->r_offset = roff;
10624 r->r_addend = relative_targ + roff;
10625 if (off + 0x8000 < 0x10000)
10626 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10627 else if (off + 0x80008000ULL < 0x100000000ULL)
10628 {
10629 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10630 ++r;
10631 roff += 4;
10632 r->r_offset = roff;
10633 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10634 r->r_addend = relative_targ + roff;
10635 }
10636 else
10637 {
10638 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10639 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10640 else
10641 {
10642 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10643 if (((off >> 32) & 0xffff) != 0)
10644 {
10645 ++r;
10646 roff += 4;
10647 r->r_offset = roff;
10648 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10649 r->r_addend = relative_targ + roff;
10650 }
10651 }
10652 if (((off >> 32) & 0xffffffffULL) != 0)
10653 roff += 4;
10654 if (PPC_HI (off) != 0)
10655 {
10656 ++r;
10657 roff += 4;
10658 r->r_offset = roff;
10659 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10660 r->r_addend = relative_targ + roff;
10661 }
10662 if (PPC_LO (off) != 0)
10663 {
10664 ++r;
10665 roff += 4;
10666 r->r_offset = roff;
10667 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10668 r->r_addend = relative_targ + roff;
10669 }
10670 }
10671 return r;
10672}
10673
04bdff6a 10674static bfd_byte *
7c1f4227 10675build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
04bdff6a
AM
10676 bfd_boolean load)
10677{
10678 uint64_t insn;
10679 if (off - odd + (1ULL << 33) < 1ULL << 34)
10680 {
10681 off -= odd;
10682 if (odd)
10683 {
10684 bfd_put_32 (abfd, NOP, p);
10685 p += 4;
10686 }
10687 if (load)
10688 insn = PLD_R12_PC;
10689 else
10690 insn = PADDI_R12_PC;
10691 insn |= D34 (off);
10692 bfd_put_32 (abfd, insn >> 32, p);
10693 p += 4;
10694 bfd_put_32 (abfd, insn, p);
10695 }
10696 /* The minimum value for paddi is -0x200000000. The minimum value
10697 for li is -0x8000, which when shifted by 34 and added gives a
10698 minimum value of -0x2000200000000. The maximum value is
10699 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10700 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10701 {
10702 off -= 8 - odd;
10703 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10704 p += 4;
10705 if (!odd)
10706 {
10707 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10708 p += 4;
10709 }
10710 insn = PADDI_R12_PC | D34 (off);
10711 bfd_put_32 (abfd, insn >> 32, p);
10712 p += 4;
10713 bfd_put_32 (abfd, insn, p);
10714 p += 4;
10715 if (odd)
10716 {
10717 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10718 p += 4;
10719 }
10720 if (load)
10721 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10722 else
10723 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10724 }
10725 else
10726 {
10727 off -= odd + 8;
10728 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10729 p += 4;
10730 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10731 p += 4;
10732 if (odd)
10733 {
10734 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10735 p += 4;
10736 }
10737 insn = PADDI_R12_PC | D34 (off);
10738 bfd_put_32 (abfd, insn >> 32, p);
10739 p += 4;
10740 bfd_put_32 (abfd, insn, p);
10741 p += 4;
10742 if (!odd)
10743 {
10744 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10745 p += 4;
10746 }
10747 if (load)
10748 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10749 else
10750 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10751 }
10752 p += 4;
10753 return p;
10754}
10755
10756static unsigned int
7c1f4227 10757size_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10758{
10759 if (off - odd + (1ULL << 33) < 1ULL << 34)
10760 return odd + 8;
10761 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10762 return 20;
10763 else
10764 return 24;
10765}
10766
10767static unsigned int
7c1f4227 10768num_relocs_for_power10_offset (bfd_vma off, int odd)
04bdff6a
AM
10769{
10770 if (off - odd + (1ULL << 33) < 1ULL << 34)
10771 return 1;
10772 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10773 return 2;
10774 else
10775 return 3;
10776}
10777
10778static Elf_Internal_Rela *
7c1f4227 10779emit_relocs_for_power10_offset (struct bfd_link_info *info,
04bdff6a
AM
10780 Elf_Internal_Rela *r, bfd_vma roff,
10781 bfd_vma targ, bfd_vma off, int odd)
10782{
10783 if (off - odd + (1ULL << 33) < 1ULL << 34)
10784 roff += odd;
10785 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10786 {
10787 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10788 r->r_offset = roff + d_offset;
10789 r->r_addend = targ + 8 - odd - d_offset;
10790 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10791 ++r;
10792 roff += 8 - odd;
10793 }
10794 else
10795 {
10796 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10797 r->r_offset = roff + d_offset;
10798 r->r_addend = targ + 8 + odd - d_offset;
10799 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10800 ++r;
10801 roff += 4;
10802 r->r_offset = roff + d_offset;
10803 r->r_addend = targ + 4 + odd - d_offset;
10804 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10805 ++r;
10806 roff += 4 + odd;
10807 }
10808 r->r_offset = roff;
10809 r->r_addend = targ;
10810 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10811 return r;
10812}
10813
df136d64
AM
10814/* Emit .eh_frame opcode to advance pc by DELTA. */
10815
10816static bfd_byte *
10817eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10818{
10819 delta /= 4;
10820 if (delta < 64)
10821 *eh++ = DW_CFA_advance_loc + delta;
10822 else if (delta < 256)
10823 {
10824 *eh++ = DW_CFA_advance_loc1;
10825 *eh++ = delta;
10826 }
10827 else if (delta < 65536)
10828 {
10829 *eh++ = DW_CFA_advance_loc2;
10830 bfd_put_16 (abfd, delta, eh);
10831 eh += 2;
10832 }
10833 else
10834 {
10835 *eh++ = DW_CFA_advance_loc4;
10836 bfd_put_32 (abfd, delta, eh);
10837 eh += 4;
10838 }
10839 return eh;
10840}
10841
10842/* Size of required .eh_frame opcode to advance pc by DELTA. */
10843
10844static unsigned int
10845eh_advance_size (unsigned int delta)
10846{
10847 if (delta < 64 * 4)
10848 /* DW_CFA_advance_loc+[1..63]. */
10849 return 1;
10850 if (delta < 256 * 4)
10851 /* DW_CFA_advance_loc1, byte. */
10852 return 2;
10853 if (delta < 65536 * 4)
10854 /* DW_CFA_advance_loc2, 2 bytes. */
10855 return 3;
10856 /* DW_CFA_advance_loc4, 4 bytes. */
10857 return 5;
10858}
10859
794e51c0
AM
10860/* With power7 weakly ordered memory model, it is possible for ld.so
10861 to update a plt entry in one thread and have another thread see a
10862 stale zero toc entry. To avoid this we need some sort of acquire
10863 barrier in the call stub. One solution is to make the load of the
10864 toc word seem to appear to depend on the load of the function entry
10865 word. Another solution is to test for r2 being zero, and branch to
10866 the appropriate glink entry if so.
10867
10868 . fake dep barrier compare
71a39c98
AM
10869 . ld 12,xxx(2) ld 12,xxx(2)
10870 . mtctr 12 mtctr 12
10871 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
10872 . add 2,2,11 cmpldi 2,0
10873 . ld 2,xxx+8(2) bnectr+
10874 . bctr b <glink_entry>
10875
10876 The solution involving the compare turns out to be faster, so
10877 that's what we use unless the branch won't reach. */
10878
10879#define ALWAYS_USE_FAKE_DEP 0
10880#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 10881
794e51c0
AM
10882static inline unsigned int
10883plt_stub_size (struct ppc_link_hash_table *htab,
10884 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10885 bfd_vma off,
10886 unsigned int odd)
794e51c0 10887{
05d0e962 10888 unsigned size;
b9e5796b 10889
05d0e962
AM
10890 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10891 {
e10a07b3 10892 if (htab->params->power10_stubs != 0)
29433886 10893 size = 8 + size_power10_offset (off, odd);
04bdff6a
AM
10894 else
10895 size = 8 + size_offset (off - 8);
05d0e962
AM
10896 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10897 size += 4;
05d0e962 10898 }
29433886 10899 else
b9e5796b 10900 {
29433886
AM
10901 size = 12;
10902 if (ALWAYS_EMIT_R2SAVE
10903 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
b9e5796b 10904 size += 4;
29433886 10905 if (PPC_HA (off) != 0)
b9e5796b 10906 size += 4;
29433886
AM
10907 if (htab->opd_abi)
10908 {
10909 size += 4;
10910 if (htab->params->plt_static_chain)
10911 size += 4;
10912 if (htab->params->plt_thread_safe
10913 && htab->elf.dynamic_sections_created
10914 && stub_entry->h != NULL
10915 && stub_entry->h->elf.dynindx != -1)
10916 size += 8;
10917 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
10918 != PPC_HA (off))
10919 size += 4;
10920 }
b9e5796b 10921 }
794e51c0 10922 if (stub_entry->h != NULL
ed7007c1 10923 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10924 && htab->params->tls_get_addr_opt)
f378ab09 10925 {
29433886 10926 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 10927 {
29433886
AM
10928 size += 30 * 4;
10929 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10930 || stub_entry->stub_type == ppc_stub_plt_call_both)
10931 size += 4;
9e7028aa
AM
10932 }
10933 else
10934 {
29433886
AM
10935 size += 7 * 4;
10936 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10937 || stub_entry->stub_type == ppc_stub_plt_call_both)
10938 size += 6 * 4;
9e7028aa 10939 }
f378ab09 10940 }
794e51c0
AM
10941 return size;
10942}
10943
2420fff6
AM
10944/* Depending on the sign of plt_stub_align:
10945 If positive, return the padding to align to a 2**plt_stub_align
10946 boundary.
10947 If negative, if this stub would cross fewer 2**plt_stub_align
10948 boundaries if we align, then return the padding needed to do so. */
10949
794e51c0
AM
10950static inline unsigned int
10951plt_stub_pad (struct ppc_link_hash_table *htab,
10952 struct ppc_stub_hash_entry *stub_entry,
29433886
AM
10953 bfd_vma plt_off,
10954 unsigned int odd)
794e51c0 10955{
2420fff6 10956 int stub_align;
1aa42141 10957 unsigned stub_size;
6f20ed8a 10958 bfd_vma stub_off = stub_entry->group->stub_sec->size;
794e51c0 10959
2420fff6
AM
10960 if (htab->params->plt_stub_align >= 0)
10961 {
10962 stub_align = 1 << htab->params->plt_stub_align;
10963 if ((stub_off & (stub_align - 1)) != 0)
10964 return stub_align - (stub_off & (stub_align - 1));
10965 return 0;
10966 }
10967
10968 stub_align = 1 << -htab->params->plt_stub_align;
29433886 10969 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
794e51c0 10970 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
e05fa0ba 10971 > ((stub_size - 1) & -stub_align))
794e51c0
AM
10972 return stub_align - (stub_off & (stub_align - 1));
10973 return 0;
10974}
10975
10976/* Build a .plt call stub. */
10977
10978static inline bfd_byte *
10979build_plt_stub (struct ppc_link_hash_table *htab,
10980 struct ppc_stub_hash_entry *stub_entry,
10981 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10982{
e7d1c40c 10983 bfd *obfd = htab->params->stub_bfd;
b9e5796b 10984 bfd_boolean plt_load_toc = htab->opd_abi;
e7d1c40c 10985 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
bd4d2eaa
AM
10986 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10987 && htab->elf.dynamic_sections_created
10988 && stub_entry->h != NULL
10989 && stub_entry->h->elf.dynindx != -1);
794e51c0
AM
10990 bfd_boolean use_fake_dep = plt_thread_safe;
10991 bfd_vma cmp_branch_off = 0;
10992
10993 if (!ALWAYS_USE_FAKE_DEP
b9e5796b 10994 && plt_load_toc
794e51c0 10995 && plt_thread_safe
9e51d549
AM
10996 && !(stub_entry->h != NULL
10997 && is_tls_get_addr (&stub_entry->h->elf, htab)
7c9cf415 10998 && htab->params->tls_get_addr_opt))
794e51c0
AM
10999 {
11000 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
b9e5796b
AM
11001 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11002 / PLT_ENTRY_SIZE (htab));
9e390558 11003 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
794e51c0
AM
11004 bfd_vma to, from;
11005
68d62958
AM
11006 if (pltindex > 32768)
11007 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
11008 to = (glinkoff
11009 + htab->glink->output_offset
11010 + htab->glink->output_section->vma);
6f20ed8a 11011 from = (p - stub_entry->group->stub_sec->contents
794e51c0
AM
11012 + 4 * (ALWAYS_EMIT_R2SAVE
11013 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11014 + 4 * (PPC_HA (offset) != 0)
11015 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11016 != PPC_HA (offset))
11017 + 4 * (plt_static_chain != 0)
11018 + 20
6f20ed8a
AM
11019 + stub_entry->group->stub_sec->output_offset
11020 + stub_entry->group->stub_sec->output_section->vma);
794e51c0
AM
11021 cmp_branch_off = to - from;
11022 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11023 }
11024
ac2df442
AM
11025 if (PPC_HA (offset) != 0)
11026 {
176a0d42
AM
11027 if (r != NULL)
11028 {
794e51c0
AM
11029 if (ALWAYS_EMIT_R2SAVE
11030 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11031 r[0].r_offset += 4;
176a0d42 11032 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 11033 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
11034 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11035 r[1].r_addend = r[0].r_addend;
b9e5796b 11036 if (plt_load_toc)
176a0d42 11037 {
b9e5796b 11038 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11039 {
b9e5796b
AM
11040 r[2].r_offset = r[1].r_offset + 4;
11041 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11042 r[2].r_addend = r[0].r_addend;
11043 }
11044 else
11045 {
11046 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11047 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11048 r[2].r_addend = r[0].r_addend + 8;
11049 if (plt_static_chain)
11050 {
11051 r[3].r_offset = r[2].r_offset + 4;
11052 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11053 r[3].r_addend = r[0].r_addend + 16;
11054 }
c7131b65 11055 }
176a0d42
AM
11056 }
11057 }
794e51c0
AM
11058 if (ALWAYS_EMIT_R2SAVE
11059 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11060 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
397998fc
AM
11061 if (plt_load_toc)
11062 {
11063 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11064 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11065 }
11066 else
11067 {
11068 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11069 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11070 }
b9e5796b
AM
11071 if (plt_load_toc
11072 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 11073 {
71a39c98 11074 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
11075 offset = 0;
11076 }
71a39c98 11077 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11078 if (plt_load_toc)
794e51c0 11079 {
b9e5796b
AM
11080 if (use_fake_dep)
11081 {
11082 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11083 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11084 }
11085 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11086 if (plt_static_chain)
11087 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
794e51c0 11088 }
ac2df442
AM
11089 }
11090 else
11091 {
176a0d42
AM
11092 if (r != NULL)
11093 {
794e51c0
AM
11094 if (ALWAYS_EMIT_R2SAVE
11095 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11096 r[0].r_offset += 4;
176a0d42 11097 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
b9e5796b 11098 if (plt_load_toc)
176a0d42 11099 {
b9e5796b 11100 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
c7131b65 11101 {
b9e5796b
AM
11102 r[1].r_offset = r[0].r_offset + 4;
11103 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11104 r[1].r_addend = r[0].r_addend;
11105 }
11106 else
11107 {
11108 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11109 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11110 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11111 if (plt_static_chain)
11112 {
11113 r[2].r_offset = r[1].r_offset + 4;
11114 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11115 r[2].r_addend = r[0].r_addend + 8;
11116 }
c7131b65 11117 }
176a0d42
AM
11118 }
11119 }
794e51c0
AM
11120 if (ALWAYS_EMIT_R2SAVE
11121 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
a078d95a 11122 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
71a39c98 11123 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
b9e5796b
AM
11124 if (plt_load_toc
11125 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
11126 {
11127 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11128 offset = 0;
11129 }
71a39c98 11130 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
b9e5796b 11131 if (plt_load_toc)
794e51c0 11132 {
b9e5796b
AM
11133 if (use_fake_dep)
11134 {
11135 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11136 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11137 }
11138 if (plt_static_chain)
11139 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11140 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
794e51c0 11141 }
ac2df442 11142 }
b9e5796b 11143 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
794e51c0
AM
11144 {
11145 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11146 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 11147 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
11148 }
11149 else
407aa07c 11150 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
11151 return p;
11152}
11153
a7f2871e
AM
11154/* Build a special .plt call stub for __tls_get_addr. */
11155
b9ca1af6 11156#define LD_R0_0R3 0xe8030000
a7f2871e
AM
11157#define LD_R12_0R3 0xe9830000
11158#define MR_R0_R3 0x7c601b78
b9ca1af6 11159#define CMPDI_R0_0 0x2c200000
a7f2871e
AM
11160#define ADD_R3_R12_R13 0x7c6c6a14
11161#define BEQLR 0x4d820020
11162#define MR_R3_R0 0x7c030378
a7f2871e 11163#define BCTRL 0x4e800421
a7f2871e 11164
29433886
AM
11165static bfd_byte *
11166build_tls_get_addr_head (struct ppc_link_hash_table *htab,
794e51c0 11167 struct ppc_stub_hash_entry *stub_entry,
29433886 11168 bfd_byte *p)
a7f2871e 11169{
e7d1c40c 11170 bfd *obfd = htab->params->stub_bfd;
794e51c0 11171
b9ca1af6 11172 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
a7f2871e 11173 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
b9ca1af6 11174 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
a7f2871e 11175 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
a7f2871e
AM
11176 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11177 bfd_put_32 (obfd, BEQLR, p), p += 4;
11178 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9e7028aa 11179
29433886
AM
11180 if (!htab->params->no_tls_get_addr_regsave)
11181 p = tls_get_addr_prologue (obfd, p, htab);
11182 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11183 || stub_entry->stub_type == ppc_stub_plt_call_both)
11184 {
9e7028aa
AM
11185 bfd_put_32 (obfd, MFLR_R0, p);
11186 p += 4;
11187 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11188 p += 4;
29433886
AM
11189 }
11190 return p;
11191}
9e7028aa 11192
29433886
AM
11193static bfd_byte *
11194build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11195 struct ppc_stub_hash_entry *stub_entry,
11196 bfd_byte *p,
11197 bfd_byte *loc)
11198{
11199 bfd *obfd = htab->params->stub_bfd;
11200
11201 if (!htab->params->no_tls_get_addr_regsave)
11202 {
11203 bfd_put_32 (obfd, BCTRL, p - 4);
11204
11205 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11206 || stub_entry->stub_type == ppc_stub_plt_call_both)
11207 {
11208 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11209 p += 4;
11210 }
11211 p = tls_get_addr_epilogue (obfd, p, htab);
11212 }
11213 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11214 || stub_entry->stub_type == ppc_stub_plt_call_both)
11215 {
9e7028aa
AM
11216 bfd_put_32 (obfd, BCTRL, p - 4);
11217
11218 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11219 p += 4;
11220 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11221 p += 4;
11222 bfd_put_32 (obfd, MTLR_R0, p);
11223 p += 4;
11224 bfd_put_32 (obfd, BLR, p);
11225 p += 4;
11226 }
a7f2871e 11227
df136d64
AM
11228 if (htab->glink_eh_frame != NULL
11229 && htab->glink_eh_frame->size != 0)
11230 {
11231 bfd_byte *base, *eh;
df136d64
AM
11232
11233 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11234 eh = base + stub_entry->group->eh_size;
29433886
AM
11235
11236 if (!htab->params->no_tls_get_addr_regsave)
9e7028aa 11237 {
29433886
AM
11238 unsigned int cfa_updt, delta, i;
11239
9e7028aa
AM
11240 /* After the bctrl, lr has been modified so we need to emit
11241 .eh_frame info saying the return address is on the stack. In
11242 fact we must put the EH info at or before the call rather
11243 than after it, because the EH info for a call needs to be
11244 specified by that point.
11245 See libgcc/unwind-dw2.c execute_cfa_program.
11246 Any stack pointer update must be described immediately after
11247 the instruction making the change, and since the stdu occurs
11248 after saving regs we put all the reg saves and the cfa
11249 change there. */
11250 cfa_updt = stub_entry->stub_offset + 18 * 4;
11251 delta = cfa_updt - stub_entry->group->lr_restore;
11252 stub_entry->group->lr_restore
11253 = stub_entry->stub_offset + (p - loc) - 4;
11254 eh = eh_advance (htab->elf.dynobj, eh, delta);
11255 *eh++ = DW_CFA_def_cfa_offset;
11256 if (htab->opd_abi)
11257 {
11258 *eh++ = 128;
11259 *eh++ = 1;
11260 }
11261 else
11262 *eh++ = 96;
11263 *eh++ = DW_CFA_offset_extended_sf;
11264 *eh++ = 65;
11265 *eh++ = (-16 / 8) & 0x7f;
11266 for (i = 4; i < 12; i++)
11267 {
11268 *eh++ = DW_CFA_offset + i;
11269 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11270 }
11271 *eh++ = (DW_CFA_advance_loc
11272 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11273 *eh++ = DW_CFA_def_cfa_offset;
11274 *eh++ = 0;
11275 for (i = 4; i < 12; i++)
11276 *eh++ = DW_CFA_restore + i;
11277 *eh++ = DW_CFA_advance_loc + 2;
29433886
AM
11278 *eh++ = DW_CFA_restore_extended;
11279 *eh++ = 65;
11280 stub_entry->group->eh_size = eh - base;
11281 }
11282 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11283 || stub_entry->stub_type == ppc_stub_plt_call_both)
11284 {
11285 unsigned int lr_used, delta;
11286
11287 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11288 delta = lr_used - stub_entry->group->lr_restore;
11289 stub_entry->group->lr_restore = lr_used + 16;
11290 eh = eh_advance (htab->elf.dynobj, eh, delta);
11291 *eh++ = DW_CFA_offset_extended_sf;
11292 *eh++ = 65;
11293 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11294 *eh++ = DW_CFA_advance_loc + 4;
11295 *eh++ = DW_CFA_restore_extended;
11296 *eh++ = 65;
11297 stub_entry->group->eh_size = eh - base;
9e7028aa 11298 }
df136d64 11299 }
a7f2871e
AM
11300 return p;
11301}
11302
176a0d42
AM
11303static Elf_Internal_Rela *
11304get_relocs (asection *sec, int count)
11305{
11306 Elf_Internal_Rela *relocs;
11307 struct bfd_elf_section_data *elfsec_data;
11308
11309 elfsec_data = elf_section_data (sec);
11310 relocs = elfsec_data->relocs;
11311 if (relocs == NULL)
11312 {
11313 bfd_size_type relsize;
11314 relsize = sec->reloc_count * sizeof (*relocs);
11315 relocs = bfd_alloc (sec->owner, relsize);
11316 if (relocs == NULL)
11317 return NULL;
11318 elfsec_data->relocs = relocs;
d4730f92
BS
11319 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11320 sizeof (Elf_Internal_Shdr));
11321 if (elfsec_data->rela.hdr == NULL)
11322 return NULL;
11323 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11324 * sizeof (Elf64_External_Rela));
11325 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
11326 sec->reloc_count = 0;
11327 }
11328 relocs += sec->reloc_count;
11329 sec->reloc_count += count;
11330 return relocs;
11331}
11332
3d58e1fc
AM
11333/* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11334 forms, to the equivalent relocs against the global symbol given by
11335 STUB_ENTRY->H. */
11336
11337static bfd_boolean
11338use_global_in_relocs (struct ppc_link_hash_table *htab,
11339 struct ppc_stub_hash_entry *stub_entry,
11340 Elf_Internal_Rela *r, unsigned int num_rel)
11341{
11342 struct elf_link_hash_entry **hashes;
11343 unsigned long symndx;
11344 struct ppc_link_hash_entry *h;
11345 bfd_vma symval;
11346
11347 /* Relocs are always against symbols in their own object file. Fake
11348 up global sym hashes for the stub bfd (which has no symbols). */
11349 hashes = elf_sym_hashes (htab->params->stub_bfd);
11350 if (hashes == NULL)
11351 {
11352 bfd_size_type hsize;
11353
11354 /* When called the first time, stub_globals will contain the
11355 total number of symbols seen during stub sizing. After
11356 allocating, stub_globals is used as an index to fill the
11357 hashes array. */
11358 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11359 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11360 if (hashes == NULL)
11361 return FALSE;
11362 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11363 htab->stub_globals = 1;
11364 }
11365 symndx = htab->stub_globals++;
11366 h = stub_entry->h;
11367 hashes[symndx] = &h->elf;
11368 if (h->oh != NULL && h->oh->is_func)
11369 h = ppc_follow_link (h->oh);
11370 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11371 || h->elf.root.type == bfd_link_hash_defweak);
ed7007c1 11372 symval = defined_sym_val (&h->elf);
3d58e1fc
AM
11373 while (num_rel-- != 0)
11374 {
11375 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11376 if (h->elf.root.u.def.section != stub_entry->target_section)
11377 {
11378 /* H is an opd symbol. The addend must be zero, and the
11379 branch reloc is the only one we can convert. */
11380 r->r_addend = 0;
11381 break;
11382 }
11383 else
11384 r->r_addend -= symval;
11385 --r;
11386 }
11387 return TRUE;
11388}
11389
aa374f67 11390static bfd_vma
25f53a85 11391get_r2off (struct bfd_link_info *info,
aa374f67
AM
11392 struct ppc_stub_hash_entry *stub_entry)
11393{
25f53a85 11394 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6f20ed8a 11395 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
aa374f67
AM
11396
11397 if (r2off == 0)
11398 {
11399 /* Support linking -R objects. Get the toc pointer from the
11400 opd entry. */
11401 char buf[8];
b9e5796b
AM
11402 if (!htab->opd_abi)
11403 return r2off;
aa374f67
AM
11404 asection *opd = stub_entry->h->elf.root.u.def.section;
11405 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11406
11407 if (strcmp (opd->name, ".opd") != 0
11408 || opd->reloc_count != 0)
11409 {
2cdcc330
AM
11410 info->callbacks->einfo
11411 (_("%P: cannot find opd entry toc for `%pT'\n"),
11412 stub_entry->h->elf.root.root.string);
aa374f67 11413 bfd_set_error (bfd_error_bad_value);
a7c49797 11414 return (bfd_vma) -1;
aa374f67
AM
11415 }
11416 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
a7c49797 11417 return (bfd_vma) -1;
aa374f67 11418 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 11419 r2off -= elf_gp (info->output_bfd);
aa374f67 11420 }
6f20ed8a 11421 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
aa374f67
AM
11422 return r2off;
11423}
11424
b34976b6 11425static bfd_boolean
4ce794b7 11426ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 11427{
721956f4
AM
11428 struct ppc_stub_hash_entry *stub_entry;
11429 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
11430 struct bfd_link_info *info;
11431 struct ppc_link_hash_table *htab;
29433886 11432 bfd *obfd;
721956f4 11433 bfd_byte *loc;
3d58e1fc 11434 bfd_byte *p, *relp;
1aa42141 11435 bfd_vma targ, off;
176a0d42 11436 Elf_Internal_Rela *r;
e054468f 11437 asection *plt;
3d58e1fc 11438 int num_rel;
04bdff6a 11439 int odd;
29433886 11440 bfd_boolean is_tga;
5d1634d7 11441
721956f4
AM
11442 /* Massage our args to the form they really have. */
11443 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 11444 info = in_arg;
5d1634d7 11445
abf874aa
CL
11446 /* Fail if the target section could not be assigned to an output
11447 section. The user should fix his linker script. */
11448 if (stub_entry->target_section != NULL
11449 && stub_entry->target_section->output_section == NULL
11450 && info->non_contiguous_regions)
53215f21
CL
11451 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11452 "Retry without --enable-non-contiguous-regions.\n"),
11453 stub_entry->target_section);
abf874aa
CL
11454
11455 /* Same for the group. */
11456 if (stub_entry->group->stub_sec != NULL
11457 && stub_entry->group->stub_sec->output_section == NULL
11458 && info->non_contiguous_regions)
53215f21
CL
11459 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11460 "output section. Retry without "
11461 "--enable-non-contiguous-regions.\n"),
11462 stub_entry->group->stub_sec,
11463 stub_entry->target_section);
abf874aa 11464
5d1634d7 11465 htab = ppc_hash_table (info);
4dfe6ac6
NC
11466 if (htab == NULL)
11467 return FALSE;
5d1634d7 11468
1aa42141 11469 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
6f20ed8a 11470 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
721956f4 11471
4ce794b7 11472 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 11473 switch (stub_entry->stub_type)
5d1634d7 11474 {
721956f4 11475 case ppc_stub_long_branch:
ad8e1ba5 11476 case ppc_stub_long_branch_r2off:
721956f4 11477 /* Branches are relative. This is where we are going to. */
1aa42141 11478 targ = (stub_entry->target_value
6911b7dc
AM
11479 + stub_entry->target_section->output_offset
11480 + stub_entry->target_section->output_section->vma);
1aa42141 11481 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
5d1634d7 11482
721956f4 11483 /* And this is where we are coming from. */
1aa42141
AM
11484 off = (stub_entry->stub_offset
11485 + stub_entry->group->stub_sec->output_offset
11486 + stub_entry->group->stub_sec->output_section->vma);
11487 off = targ - off;
e86ce104 11488
9e390558 11489 p = loc;
29433886 11490 obfd = htab->params->stub_bfd;
ac2df442 11491 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 11492 {
25f53a85 11493 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 11494
a7c49797 11495 if (r2off == (bfd_vma) -1)
aa374f67
AM
11496 {
11497 htab->stub_error = TRUE;
11498 return FALSE;
11499 }
29433886 11500 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11501 p += 4;
ac2df442
AM
11502 if (PPC_HA (r2off) != 0)
11503 {
29433886 11504 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
9e390558 11505 p += 4;
a7c49797
AM
11506 }
11507 if (PPC_LO (r2off) != 0)
11508 {
29433886 11509 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
9e390558 11510 p += 4;
ac2df442 11511 }
9e390558 11512 off -= p - loc;
ad8e1ba5 11513 }
29433886 11514 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
9e390558 11515 p += 4;
ad8e1ba5 11516
5c3dead3
AM
11517 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11518 {
cf97bcb0
AM
11519 _bfd_error_handler
11520 (_("long branch stub `%s' offset overflow"),
bc30df16 11521 stub_entry->root.string);
5c3dead3
AM
11522 htab->stub_error = TRUE;
11523 return FALSE;
11524 }
ee75fd95
AM
11525
11526 if (info->emitrelocations)
11527 {
6f20ed8a 11528 r = get_relocs (stub_entry->group->stub_sec, 1);
176a0d42
AM
11529 if (r == NULL)
11530 return FALSE;
9e390558 11531 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
ee75fd95 11532 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
1aa42141 11533 r->r_addend = targ;
3d58e1fc
AM
11534 if (stub_entry->h != NULL
11535 && !use_global_in_relocs (htab, stub_entry, r, 1))
11536 return FALSE;
ee75fd95 11537 }
721956f4 11538 break;
e86ce104 11539
721956f4 11540 case ppc_stub_plt_branch:
ad8e1ba5 11541 case ppc_stub_plt_branch_r2off:
721956f4
AM
11542 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11543 stub_entry->root.string + 9,
b34976b6 11544 FALSE, FALSE);
721956f4
AM
11545 if (br_entry == NULL)
11546 {
cf97bcb0
AM
11547 _bfd_error_handler (_("can't find branch stub `%s'"),
11548 stub_entry->root.string);
b34976b6
AM
11549 htab->stub_error = TRUE;
11550 return FALSE;
721956f4
AM
11551 }
11552
1aa42141 11553 targ = (stub_entry->target_value
176a0d42
AM
11554 + stub_entry->target_section->output_offset
11555 + stub_entry->target_section->output_section->vma);
6911b7dc 11556 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
1aa42141 11557 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 11558
1aa42141 11559 bfd_put_64 (htab->brlt->owner, targ,
4ce794b7 11560 htab->brlt->contents + br_entry->offset);
721956f4 11561
f94498ff 11562 if (br_entry->iter == htab->stub_iteration)
721956f4 11563 {
f94498ff 11564 br_entry->iter = 0;
84f5d08e 11565
f94498ff 11566 if (htab->relbrlt != NULL)
84f5d08e 11567 {
f94498ff
AM
11568 /* Create a reloc for the branch lookup table entry. */
11569 Elf_Internal_Rela rela;
11570 bfd_byte *rl;
11571
11572 rela.r_offset = (br_entry->offset
11573 + htab->brlt->output_offset
11574 + htab->brlt->output_section->vma);
11575 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11576 rela.r_addend = targ;
f94498ff
AM
11577
11578 rl = htab->relbrlt->contents;
11579 rl += (htab->relbrlt->reloc_count++
11580 * sizeof (Elf64_External_Rela));
11581 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11582 }
11583 else if (info->emitrelocations)
11584 {
176a0d42
AM
11585 r = get_relocs (htab->brlt, 1);
11586 if (r == NULL)
11587 return FALSE;
11588 /* brlt, being SEC_LINKER_CREATED does not go through the
11589 normal reloc processing. Symbols and offsets are not
11590 translated from input file to output file form, so
11591 set up the offset per the output file. */
f94498ff
AM
11592 r->r_offset = (br_entry->offset
11593 + htab->brlt->output_offset
11594 + htab->brlt->output_section->vma);
11595 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
1aa42141 11596 r->r_addend = targ;
84f5d08e 11597 }
84f5d08e 11598 }
721956f4 11599
1aa42141 11600 targ = (br_entry->offset
176a0d42
AM
11601 + htab->brlt->output_offset
11602 + htab->brlt->output_section->vma);
11603
1aa42141
AM
11604 off = (elf_gp (info->output_bfd)
11605 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11606 off = targ - off;
721956f4 11607
ad8e1ba5 11608 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 11609 {
25f53a85 11610 info->callbacks->einfo
c1c8c1ef 11611 (_("%P: linkage table error against `%pT'\n"),
721956f4 11612 stub_entry->root.string);
5d1634d7 11613 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11614 htab->stub_error = TRUE;
11615 return FALSE;
5d1634d7 11616 }
41bd81ab 11617
176a0d42
AM
11618 if (info->emitrelocations)
11619 {
6f20ed8a 11620 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
176a0d42
AM
11621 if (r == NULL)
11622 return FALSE;
6f20ed8a 11623 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11624 if (bfd_big_endian (info->output_bfd))
11625 r[0].r_offset += 2;
00f412ee 11626 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
176a0d42
AM
11627 r[0].r_offset += 4;
11628 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
1aa42141 11629 r[0].r_addend = targ;
176a0d42
AM
11630 if (PPC_HA (off) != 0)
11631 {
11632 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11633 r[1].r_offset = r[0].r_offset + 4;
11634 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11635 r[1].r_addend = r[0].r_addend;
11636 }
11637 }
11638
9e390558 11639 p = loc;
29433886 11640 obfd = htab->params->stub_bfd;
00f412ee 11641 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
ad8e1ba5 11642 {
176a0d42 11643 if (PPC_HA (off) != 0)
ac2df442 11644 {
29433886 11645 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11646 p += 4;
29433886 11647 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11648 }
11649 else
29433886 11650 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ad8e1ba5
AM
11651 }
11652 else
11653 {
25f53a85 11654 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67 11655
a7c49797 11656 if (r2off == (bfd_vma) -1)
aa374f67
AM
11657 {
11658 htab->stub_error = TRUE;
11659 return FALSE;
11660 }
ad8e1ba5 11661
29433886 11662 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
9e390558 11663 p += 4;
176a0d42 11664 if (PPC_HA (off) != 0)
ac2df442 11665 {
29433886 11666 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
9e390558 11667 p += 4;
29433886 11668 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
ac2df442
AM
11669 }
11670 else
29433886 11671 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
ac2df442
AM
11672
11673 if (PPC_HA (r2off) != 0)
11674 {
9e390558 11675 p += 4;
29433886 11676 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
00f412ee
AM
11677 }
11678 if (PPC_LO (r2off) != 0)
11679 {
9e390558 11680 p += 4;
29433886 11681 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
ac2df442 11682 }
ad8e1ba5 11683 }
9e390558 11684 p += 4;
29433886 11685 bfd_put_32 (obfd, MTCTR_R12, p);
9e390558 11686 p += 4;
29433886 11687 bfd_put_32 (obfd, BCTR, p);
407aa07c 11688 p += 4;
721956f4 11689 break;
5d1634d7 11690
05d0e962
AM
11691 case ppc_stub_long_branch_notoc:
11692 case ppc_stub_long_branch_both:
11693 case ppc_stub_plt_branch_notoc:
11694 case ppc_stub_plt_branch_both:
11695 case ppc_stub_plt_call_notoc:
11696 case ppc_stub_plt_call_both:
11697 p = loc;
f891966f 11698 off = (stub_entry->stub_offset
05d0e962
AM
11699 + stub_entry->group->stub_sec->output_offset
11700 + stub_entry->group->stub_sec->output_section->vma);
29433886
AM
11701 obfd = htab->params->stub_bfd;
11702 is_tga = ((stub_entry->stub_type == ppc_stub_plt_call_notoc
11703 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 11704 && stub_entry->h != NULL
29433886
AM
11705 && is_tls_get_addr (&stub_entry->h->elf, htab)
11706 && htab->params->tls_get_addr_opt);
11707 if (is_tga)
11708 {
11709 p = build_tls_get_addr_head (htab, stub_entry, p);
11710 off += p - loc;
11711 }
05d0e962
AM
11712 if (stub_entry->stub_type == ppc_stub_long_branch_both
11713 || stub_entry->stub_type == ppc_stub_plt_branch_both
11714 || stub_entry->stub_type == ppc_stub_plt_call_both)
11715 {
11716 off += 4;
29433886 11717 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
05d0e962
AM
11718 p += 4;
11719 }
11720 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11721 {
11722 targ = stub_entry->plt_ent->plt.offset & ~1;
11723 if (targ >= (bfd_vma) -2)
11724 abort ();
11725
11726 plt = htab->elf.splt;
30845f11 11727 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
05d0e962
AM
11728 {
11729 if (stub_entry->symtype == STT_GNU_IFUNC)
11730 plt = htab->elf.iplt;
11731 else
11732 plt = htab->pltlocal;
11733 }
11734 targ += plt->output_offset + plt->output_section->vma;
11735 }
11736 else
11737 targ = (stub_entry->target_value
11738 + stub_entry->target_section->output_offset
11739 + stub_entry->target_section->output_section->vma);
04bdff6a 11740 odd = off & 4;
05d0e962 11741 off = targ - off;
f891966f 11742
3d58e1fc
AM
11743 relp = p;
11744 num_rel = 0;
e10a07b3 11745 if (htab->params->power10_stubs != 0)
04bdff6a
AM
11746 {
11747 bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
29433886 11748 p = build_power10_offset (obfd, p, off, odd, load);
04bdff6a
AM
11749 }
11750 else
11751 {
29433886
AM
11752 if (htab->glink_eh_frame != NULL
11753 && htab->glink_eh_frame->size != 0)
11754 {
11755 bfd_byte *base, *eh;
11756 unsigned int lr_used, delta;
11757
11758 base = (htab->glink_eh_frame->contents
11759 + stub_entry->group->eh_base + 17);
11760 eh = base + stub_entry->group->eh_size;
11761 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11762 delta = lr_used - stub_entry->group->lr_restore;
11763 stub_entry->group->lr_restore = lr_used + 8;
11764 eh = eh_advance (htab->elf.dynobj, eh, delta);
11765 *eh++ = DW_CFA_register;
11766 *eh++ = 65;
11767 *eh++ = 12;
11768 *eh++ = DW_CFA_advance_loc + 2;
11769 *eh++ = DW_CFA_restore_extended;
11770 *eh++ = 65;
11771 stub_entry->group->eh_size = eh - base;
11772 }
11773
04bdff6a
AM
11774 /* The notoc stubs calculate their target (either a PLT entry or
11775 the global entry point of a function) relative to the PC
11776 returned by the "bcl" two instructions past the start of the
11777 sequence emitted by build_offset. The offset is therefore 8
11778 less than calculated from the start of the sequence. */
11779 off -= 8;
29433886 11780 p = build_offset (obfd, p, off,
04bdff6a
AM
11781 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11782 }
11783
f891966f 11784 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
05d0e962 11785 {
f891966f 11786 bfd_vma from;
3d58e1fc 11787 num_rel = 1;
f891966f
AM
11788 from = (stub_entry->stub_offset
11789 + stub_entry->group->stub_sec->output_offset
11790 + stub_entry->group->stub_sec->output_section->vma
11791 + (p - loc));
29433886 11792 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
05d0e962
AM
11793 }
11794 else
11795 {
29433886 11796 bfd_put_32 (obfd, MTCTR_R12, p);
05d0e962 11797 p += 4;
29433886 11798 bfd_put_32 (obfd, BCTR, p);
05d0e962
AM
11799 }
11800 p += 4;
df136d64 11801
29433886
AM
11802 if (is_tga)
11803 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11804
3d58e1fc
AM
11805 if (info->emitrelocations)
11806 {
04bdff6a 11807 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
e10a07b3 11808 if (htab->params->power10_stubs != 0)
7c1f4227 11809 num_rel += num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
11810 else
11811 {
11812 num_rel += num_relocs_for_offset (off);
11813 roff += 16;
11814 }
3d58e1fc
AM
11815 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11816 if (r == NULL)
11817 return FALSE;
e10a07b3 11818 if (htab->params->power10_stubs != 0)
7c1f4227 11819 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
04bdff6a
AM
11820 else
11821 r = emit_relocs_for_offset (info, r, roff, targ, off);
3d58e1fc
AM
11822 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11823 || stub_entry->stub_type == ppc_stub_long_branch_both)
11824 {
11825 ++r;
11826 roff = p - 4 - stub_entry->group->stub_sec->contents;
11827 r->r_offset = roff;
11828 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11829 r->r_addend = targ;
11830 if (stub_entry->h != NULL
11831 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11832 return FALSE;
11833 }
11834 }
05d0e962
AM
11835 break;
11836
721956f4 11837 case ppc_stub_plt_call:
794e51c0 11838 case ppc_stub_plt_call_r2save:
e054468f 11839 if (stub_entry->h != NULL
b31867b6
AM
11840 && stub_entry->h->is_func_descriptor
11841 && stub_entry->h->oh != NULL)
c862ae31 11842 {
b31867b6
AM
11843 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11844
11845 /* If the old-ABI "dot-symbol" is undefined make it weak so
6f20ed8a 11846 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
8c5b4e52
AM
11847 if (fh->elf.root.type == bfd_link_hash_undefined
11848 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11849 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
b31867b6 11850 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
11851 }
11852
721956f4 11853 /* Now build the stub. */
1aa42141
AM
11854 targ = stub_entry->plt_ent->plt.offset & ~1;
11855 if (targ >= (bfd_vma) -2)
721956f4
AM
11856 abort ();
11857
33e44f2e 11858 plt = htab->elf.splt;
30845f11 11859 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
2d7ad24e
AM
11860 {
11861 if (stub_entry->symtype == STT_GNU_IFUNC)
11862 plt = htab->elf.iplt;
11863 else
11864 plt = htab->pltlocal;
11865 }
1aa42141 11866 targ += plt->output_offset + plt->output_section->vma;
e054468f 11867
1aa42141
AM
11868 off = (elf_gp (info->output_bfd)
11869 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11870 off = targ - off;
721956f4 11871
ad8e1ba5 11872 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 11873 {
25f53a85 11874 info->callbacks->einfo
695344c0 11875 /* xgettext:c-format */
c1c8c1ef 11876 (_("%P: linkage table error against `%pT'\n"),
e054468f
AM
11877 stub_entry->h != NULL
11878 ? stub_entry->h->elf.root.root.string
11879 : "<local sym>");
721956f4 11880 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
11881 htab->stub_error = TRUE;
11882 return FALSE;
721956f4
AM
11883 }
11884
176a0d42
AM
11885 r = NULL;
11886 if (info->emitrelocations)
11887 {
6f20ed8a 11888 r = get_relocs (stub_entry->group->stub_sec,
3ba720c7
AM
11889 ((PPC_HA (off) != 0)
11890 + (htab->opd_abi
e7d1c40c 11891 ? 2 + (htab->params->plt_static_chain
3ba720c7
AM
11892 && PPC_HA (off + 16) == PPC_HA (off))
11893 : 1)));
176a0d42
AM
11894 if (r == NULL)
11895 return FALSE;
6f20ed8a 11896 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
7cfbafbc
AM
11897 if (bfd_big_endian (info->output_bfd))
11898 r[0].r_offset += 2;
1aa42141 11899 r[0].r_addend = targ;
176a0d42 11900 }
29433886
AM
11901 p = loc;
11902 obfd = htab->params->stub_bfd;
11903 is_tga = (stub_entry->h != NULL
11904 && is_tls_get_addr (&stub_entry->h->elf, htab)
11905 && htab->params->tls_get_addr_opt);
11906 if (is_tga)
11907 {
11908 p = build_tls_get_addr_head (htab, stub_entry, p);
11909 if (r != NULL)
11910 r[0].r_offset += p - loc;
11911 }
11912 p = build_plt_stub (htab, stub_entry, p, off, r);
11913 if (is_tga)
11914 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
721956f4
AM
11915 break;
11916
a4b6fadd
AM
11917 case ppc_stub_save_res:
11918 return TRUE;
11919
721956f4
AM
11920 default:
11921 BFD_FAIL ();
b34976b6 11922 return FALSE;
721956f4
AM
11923 }
11924
1aa42141 11925 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
97b639ba 11926
e7d1c40c 11927 if (htab->params->emit_stub_syms)
97b639ba
AM
11928 {
11929 struct elf_link_hash_entry *h;
ee75fd95
AM
11930 size_t len1, len2;
11931 char *name;
11932 const char *const stub_str[] = { "long_branch",
05d0e962
AM
11933 "long_branch",
11934 "long_branch",
3f6ff479
AM
11935 "long_branch",
11936 "plt_branch",
ee75fd95 11937 "plt_branch",
05d0e962
AM
11938 "plt_branch",
11939 "plt_branch",
11940 "plt_call",
11941 "plt_call",
794e51c0 11942 "plt_call",
ee75fd95
AM
11943 "plt_call" };
11944
11945 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11946 len2 = strlen (stub_entry->root.string);
11947 name = bfd_malloc (len1 + len2 + 2);
11948 if (name == NULL)
11949 return FALSE;
11950 memcpy (name, stub_entry->root.string, 9);
11951 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11952 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11953 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
11954 if (h == NULL)
11955 return FALSE;
11956 if (h->root.type == bfd_link_hash_new)
11957 {
11958 h->root.type = bfd_link_hash_defined;
6f20ed8a 11959 h->root.u.def.section = stub_entry->group->stub_sec;
97b639ba 11960 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
11961 h->ref_regular = 1;
11962 h->def_regular = 1;
11963 h->ref_regular_nonweak = 1;
11964 h->forced_local = 1;
11965 h->non_elf = 0;
2ec55de3 11966 h->root.linker_def = 1;
97b639ba
AM
11967 }
11968 }
11969
b34976b6 11970 return TRUE;
721956f4
AM
11971}
11972
11973/* As above, but don't actually build the stub. Just bump offset so
11974 we know stub section sizes, and select plt_branch stubs where
11975 long_branch stubs won't do. */
11976
b34976b6 11977static bfd_boolean
4ce794b7 11978ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
11979{
11980 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 11981 struct bfd_link_info *info;
721956f4 11982 struct ppc_link_hash_table *htab;
f891966f
AM
11983 asection *plt;
11984 bfd_vma targ, off, r2off;
04bdff6a 11985 unsigned int size, extra, lr_used, delta, odd;
721956f4
AM
11986
11987 /* Massage our args to the form they really have. */
11988 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
11989 info = in_arg;
11990
11991 htab = ppc_hash_table (info);
4dfe6ac6
NC
11992 if (htab == NULL)
11993 return FALSE;
721956f4 11994
abf874aa
CL
11995 /* Fail if the target section could not be assigned to an output
11996 section. The user should fix his linker script. */
11997 if (stub_entry->target_section != NULL
11998 && stub_entry->target_section->output_section == NULL
11999 && info->non_contiguous_regions)
53215f21
CL
12000 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12001 "Retry without --enable-non-contiguous-regions.\n"),
12002 stub_entry->target_section);
abf874aa
CL
12003
12004 /* Same for the group. */
12005 if (stub_entry->group->stub_sec != NULL
12006 && stub_entry->group->stub_sec->output_section == NULL
12007 && info->non_contiguous_regions)
53215f21
CL
12008 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12009 "output section. Retry without "
12010 "--enable-non-contiguous-regions.\n"),
12011 stub_entry->group->stub_sec,
12012 stub_entry->target_section);
abf874aa 12013
1aa42141
AM
12014 /* Make a note of the offset within the stubs for this entry. */
12015 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12016
a4b6fadd
AM
12017 if (stub_entry->h != NULL
12018 && stub_entry->h->save_res
12019 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12020 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12021 {
12022 /* Don't make stubs to out-of-line register save/restore
12023 functions. Instead, emit copies of the functions. */
12024 stub_entry->group->needs_save_res = 1;
12025 stub_entry->stub_type = ppc_stub_save_res;
12026 return TRUE;
12027 }
12028
f891966f 12029 switch (stub_entry->stub_type)
721956f4 12030 {
f891966f
AM
12031 case ppc_stub_plt_branch:
12032 case ppc_stub_plt_branch_r2off:
12033 /* Reset the stub type from the plt branch variant in case we now
12034 can reach with a shorter stub. */
12035 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12036 /* Fall through. */
12037 case ppc_stub_long_branch:
12038 case ppc_stub_long_branch_r2off:
1aa42141
AM
12039 targ = (stub_entry->target_value
12040 + stub_entry->target_section->output_offset
12041 + stub_entry->target_section->output_section->vma);
f891966f 12042 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
1aa42141
AM
12043 off = (stub_entry->stub_offset
12044 + stub_entry->group->stub_sec->output_offset
12045 + stub_entry->group->stub_sec->output_section->vma);
721956f4 12046
ad8e1ba5 12047 size = 4;
f891966f 12048 r2off = 0;
ad8e1ba5
AM
12049 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12050 {
25f53a85 12051 r2off = get_r2off (info, stub_entry);
a7c49797 12052 if (r2off == (bfd_vma) -1)
aa374f67
AM
12053 {
12054 htab->stub_error = TRUE;
12055 return FALSE;
12056 }
a7c49797 12057 size = 8;
ac2df442 12058 if (PPC_HA (r2off) != 0)
a7c49797
AM
12059 size += 4;
12060 if (PPC_LO (r2off) != 0)
12061 size += 4;
1aa42141 12062 off += size - 4;
ad8e1ba5 12063 }
1aa42141 12064 off = targ - off;
ad8e1ba5 12065
f891966f
AM
12066 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12067 Do the same for -R objects without function descriptors. */
12068 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12069 && r2off == 0
12070 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12071 || off + (1 << 25) >= (bfd_vma) (1 << 26))
721956f4 12072 {
f891966f 12073 struct ppc_branch_hash_entry *br_entry;
df136d64 12074
f891966f
AM
12075 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12076 stub_entry->root.string + 9,
12077 TRUE, FALSE);
12078 if (br_entry == NULL)
721956f4 12079 {
f891966f
AM
12080 _bfd_error_handler (_("can't build branch stub `%s'"),
12081 stub_entry->root.string);
12082 htab->stub_error = TRUE;
12083 return FALSE;
721956f4
AM
12084 }
12085
f891966f 12086 if (br_entry->iter != htab->stub_iteration)
721956f4 12087 {
f891966f
AM
12088 br_entry->iter = htab->stub_iteration;
12089 br_entry->offset = htab->brlt->size;
12090 htab->brlt->size += 8;
63bc6f6c 12091
f891966f
AM
12092 if (htab->relbrlt != NULL)
12093 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12094 else if (info->emitrelocations)
84f5d08e 12095 {
f891966f
AM
12096 htab->brlt->reloc_count += 1;
12097 htab->brlt->flags |= SEC_RELOC;
05d0e962 12098 }
f891966f 12099 }
ac2df442 12100
f891966f
AM
12101 targ = (br_entry->offset
12102 + htab->brlt->output_offset
12103 + htab->brlt->output_section->vma);
12104 off = (elf_gp (info->output_bfd)
12105 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12106 off = targ - off;
176a0d42 12107
f891966f
AM
12108 if (info->emitrelocations)
12109 {
12110 stub_entry->group->stub_sec->reloc_count
12111 += 1 + (PPC_HA (off) != 0);
12112 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12113 }
05d0e962 12114
f891966f
AM
12115 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12116 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12117 {
12118 size = 12;
12119 if (PPC_HA (off) != 0)
12120 size = 16;
ac2df442 12121 }
f891966f 12122 else
ac2df442 12123 {
f891966f
AM
12124 size = 16;
12125 if (PPC_HA (off) != 0)
12126 size += 4;
12127
12128 if (PPC_HA (r2off) != 0)
12129 size += 4;
12130 if (PPC_LO (r2off) != 0)
12131 size += 4;
ac2df442 12132 }
721956f4 12133 }
f891966f
AM
12134 else if (info->emitrelocations)
12135 {
12136 stub_entry->group->stub_sec->reloc_count += 1;
12137 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12138 }
12139 break;
12140
12141 case ppc_stub_plt_branch_notoc:
12142 case ppc_stub_plt_branch_both:
12143 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12144 /* Fall through. */
12145 case ppc_stub_long_branch_notoc:
12146 case ppc_stub_long_branch_both:
12147 off = (stub_entry->stub_offset
12148 + stub_entry->group->stub_sec->output_offset
12149 + stub_entry->group->stub_sec->output_section->vma);
12150 size = 0;
12151 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12152 size = 4;
12153 off += size;
12154 targ = (stub_entry->target_value
12155 + stub_entry->target_section->output_offset
12156 + stub_entry->target_section->output_section->vma);
04bdff6a 12157 odd = off & 4;
f891966f
AM
12158 off = targ - off;
12159
3d58e1fc
AM
12160 if (info->emitrelocations)
12161 {
04bdff6a 12162 unsigned int num_rel;
e10a07b3 12163 if (htab->params->power10_stubs != 0)
7c1f4227 12164 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12165 else
12166 num_rel = num_relocs_for_offset (off - 8);
12167 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12168 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12169 }
12170
e10a07b3 12171 if (htab->params->power10_stubs != 0)
7c1f4227 12172 extra = size_power10_offset (off, odd);
04bdff6a
AM
12173 else
12174 extra = size_offset (off - 8);
f891966f
AM
12175 /* Include branch insn plus those in the offset sequence. */
12176 size += 4 + extra;
12177 /* The branch insn is at the end, or "extra" bytes along. So
12178 its offset will be "extra" bytes less that that already
12179 calculated. */
12180 off -= extra;
12181
e10a07b3 12182 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12183 {
12184 /* After the bcl, lr has been modified so we need to emit
12185 .eh_frame info saying the return address is in r12. */
12186 lr_used = stub_entry->stub_offset + 8;
12187 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12188 lr_used += 4;
12189 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12190 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12191 DW_CFA_restore_extended 65. */
12192 delta = lr_used - stub_entry->group->lr_restore;
12193 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12194 stub_entry->group->lr_restore = lr_used + 8;
12195 }
f891966f
AM
12196
12197 /* If the branch can't reach, use a plt_branch. */
12198 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12199 {
12200 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12201 - ppc_stub_long_branch_notoc);
12202 size += 4;
12203 }
3d58e1fc
AM
12204 else if (info->emitrelocations)
12205 stub_entry->group->stub_sec->reloc_count +=1;
f891966f
AM
12206 break;
12207
12208 case ppc_stub_plt_call_notoc:
12209 case ppc_stub_plt_call_both:
29433886 12210 lr_used = 0;
12cf8b93
AM
12211 if (stub_entry->h != NULL
12212 && is_tls_get_addr (&stub_entry->h->elf, htab)
29433886
AM
12213 && htab->params->tls_get_addr_opt)
12214 {
12215 lr_used += 7 * 4;
12216 if (!htab->params->no_tls_get_addr_regsave)
12217 lr_used += 11 * 4;
12218 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12219 lr_used += 2 * 4;
12220 }
f891966f 12221 if (stub_entry->stub_type == ppc_stub_plt_call_both)
29433886 12222 lr_used += 4;
f891966f
AM
12223 targ = stub_entry->plt_ent->plt.offset & ~1;
12224 if (targ >= (bfd_vma) -2)
12225 abort ();
12226
12227 plt = htab->elf.splt;
30845f11 12228 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12229 {
12230 if (stub_entry->symtype == STT_GNU_IFUNC)
12231 plt = htab->elf.iplt;
12232 else
12233 plt = htab->pltlocal;
12234 }
12235 targ += plt->output_offset + plt->output_section->vma;
29433886
AM
12236 off = (stub_entry->stub_offset
12237 + stub_entry->group->stub_sec->output_offset
12238 + stub_entry->group->stub_sec->output_section->vma
12239 + lr_used);
04bdff6a 12240 odd = off & 4;
f891966f
AM
12241 off = targ - off;
12242
12243 if (htab->params->plt_stub_align != 0)
12244 {
29433886 12245 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
f891966f
AM
12246
12247 stub_entry->group->stub_sec->size += pad;
12248 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12249 off -= pad;
29433886 12250 odd ^= pad & 4;
f891966f
AM
12251 }
12252
3d58e1fc
AM
12253 if (info->emitrelocations)
12254 {
04bdff6a 12255 unsigned int num_rel;
e10a07b3 12256 if (htab->params->power10_stubs != 0)
7c1f4227 12257 num_rel = num_relocs_for_power10_offset (off, odd);
04bdff6a
AM
12258 else
12259 num_rel = num_relocs_for_offset (off - 8);
12260 stub_entry->group->stub_sec->reloc_count += num_rel;
3d58e1fc
AM
12261 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12262 }
12263
29433886 12264 size = plt_stub_size (htab, stub_entry, off, odd);
f891966f 12265
e10a07b3 12266 if (htab->params->power10_stubs == 0)
04bdff6a
AM
12267 {
12268 /* After the bcl, lr has been modified so we need to emit
12269 .eh_frame info saying the return address is in r12. */
29433886 12270 lr_used += stub_entry->stub_offset + 8;
04bdff6a
AM
12271 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12272 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12273 DW_CFA_restore_extended 65. */
12274 delta = lr_used - stub_entry->group->lr_restore;
12275 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12276 stub_entry->group->lr_restore = lr_used + 8;
12277 }
29433886
AM
12278 if ((stub_entry->stub_type == ppc_stub_plt_call_notoc
12279 || stub_entry->stub_type == ppc_stub_plt_call_both)
12cf8b93 12280 && stub_entry->h != NULL
29433886
AM
12281 && is_tls_get_addr (&stub_entry->h->elf, htab)
12282 && htab->params->tls_get_addr_opt)
12283 {
12284 if (!htab->params->no_tls_get_addr_regsave)
12285 {
12286 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12287 delta = cfa_updt - stub_entry->group->lr_restore;
12288 stub_entry->group->eh_size += eh_advance_size (delta);
12289 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12290 stub_entry->group->lr_restore
12291 = stub_entry->stub_offset + size - 4;
12292 }
12293 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12294 {
12295 lr_used = stub_entry->stub_offset + size - 20;
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
12299 = stub_entry->stub_offset + size - 4;
12300 }
12301 }
f891966f
AM
12302 break;
12303
12304 case ppc_stub_plt_call:
12305 case ppc_stub_plt_call_r2save:
12306 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12307 if (targ >= (bfd_vma) -2)
12308 abort ();
12309 plt = htab->elf.splt;
30845f11 12310 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
f891966f
AM
12311 {
12312 if (stub_entry->symtype == STT_GNU_IFUNC)
12313 plt = htab->elf.iplt;
12314 else
12315 plt = htab->pltlocal;
12316 }
12317 targ += plt->output_offset + plt->output_section->vma;
12318
12319 off = (elf_gp (info->output_bfd)
12320 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12321 off = targ - off;
12322
12323 if (htab->params->plt_stub_align != 0)
12324 {
29433886 12325 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
f891966f
AM
12326
12327 stub_entry->group->stub_sec->size += pad;
12328 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12329 }
12330
12331 if (info->emitrelocations)
12332 {
12333 stub_entry->group->stub_sec->reloc_count
12334 += ((PPC_HA (off) != 0)
12335 + (htab->opd_abi
12336 ? 2 + (htab->params->plt_static_chain
12337 && PPC_HA (off + 16) == PPC_HA (off))
12338 : 1));
12339 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12340 }
12341
29433886 12342 size = plt_stub_size (htab, stub_entry, off, 0);
f891966f
AM
12343
12344 if (stub_entry->h != NULL
ed7007c1 12345 && is_tls_get_addr (&stub_entry->h->elf, htab)
f891966f
AM
12346 && htab->params->tls_get_addr_opt
12347 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12348 {
29433886
AM
12349 if (!htab->params->no_tls_get_addr_regsave)
12350 {
12351 /* Adjustments to r1 need to be described. */
12352 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12353 delta = cfa_updt - stub_entry->group->lr_restore;
12354 stub_entry->group->eh_size += eh_advance_size (delta);
12355 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12356 }
12357 else
9e7028aa
AM
12358 {
12359 lr_used = stub_entry->stub_offset + size - 20;
12360 /* The eh_frame info will consist of a DW_CFA_advance_loc
12361 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12362 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12363 delta = lr_used - stub_entry->group->lr_restore;
12364 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12365 }
29433886 12366 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
f891966f
AM
12367 }
12368 break;
12369
12370 default:
12371 BFD_FAIL ();
12372 return FALSE;
721956f4
AM
12373 }
12374
6f20ed8a 12375 stub_entry->group->stub_sec->size += size;
b34976b6 12376 return TRUE;
721956f4
AM
12377}
12378
12379/* Set up various things so that we can make a list of input sections
12380 for each output section included in the link. Returns -1 on error,
cedb70c5 12381 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
12382
12383int
e7d1c40c 12384ppc64_elf_setup_section_lists (struct bfd_link_info *info)
721956f4 12385{
6f20ed8a 12386 unsigned int id;
986f0783 12387 size_t amt;
721956f4
AM
12388 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12389
4dfe6ac6
NC
12390 if (htab == NULL)
12391 return -1;
4c52953f 12392
7cf7fcc8 12393 htab->sec_info_arr_size = _bfd_section_id;
6f20ed8a
AM
12394 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12395 htab->sec_info = bfd_zmalloc (amt);
12396 if (htab->sec_info == NULL)
721956f4
AM
12397 return -1;
12398
3d6f9012
AM
12399 /* Set toc_off for com, und, abs and ind sections. */
12400 for (id = 0; id < 3; id++)
6f20ed8a 12401 htab->sec_info[id].toc_off = TOC_BASE_OFF;
734b6cf9 12402
721956f4
AM
12403 return 1;
12404}
12405
927be08e
AM
12406/* Set up for first pass at multitoc partitioning. */
12407
12408void
12409ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12410{
12411 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12412
1c865ab2 12413 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
12414 htab->toc_bfd = NULL;
12415 htab->toc_first_sec = NULL;
12416}
12417
e717da7e
AM
12418/* The linker repeatedly calls this function for each TOC input section
12419 and linker generated GOT section. Group input bfds such that the toc
927be08e 12420 within a group is less than 64k in size. */
ad8e1ba5 12421
927be08e 12422bfd_boolean
4ce794b7 12423ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
12424{
12425 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 12426 bfd_vma addr, off, limit;
ad8e1ba5 12427
4dfe6ac6
NC
12428 if (htab == NULL)
12429 return FALSE;
12430
927be08e 12431 if (!htab->second_toc_pass)
4c52953f 12432 {
927be08e 12433 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
12434 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12435
12436 if (new_bfd)
bf102f86
AM
12437 {
12438 htab->toc_bfd = isec->owner;
12439 htab->toc_first_sec = isec;
12440 }
927be08e 12441
bf102f86
AM
12442 addr = isec->output_offset + isec->output_section->vma;
12443 off = addr - htab->toc_curr;
d77c8a4b
AM
12444 limit = 0x80008000;
12445 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12446 limit = 0x10000;
12447 if (off + isec->size > limit)
bf102f86
AM
12448 {
12449 addr = (htab->toc_first_sec->output_offset
12450 + htab->toc_first_sec->output_section->vma);
12451 htab->toc_curr = addr;
a27e685f 12452 htab->toc_curr &= -TOC_BASE_ALIGN;
bf102f86 12453 }
99877b66 12454
927be08e
AM
12455 /* toc_curr is the base address of this toc group. Set elf_gp
12456 for the input section to be the offset relative to the
12457 output toc base plus 0x8000. Making the input elf_gp an
12458 offset allows us to move the toc as a whole without
12459 recalculating input elf_gp. */
06bcf541 12460 off = htab->toc_curr - elf_gp (info->output_bfd);
927be08e
AM
12461 off += TOC_BASE_OFF;
12462
12463 /* Die if someone uses a linker script that doesn't keep input
12464 file .toc and .got together. */
a4fd3de5
AM
12465 if (new_bfd
12466 && elf_gp (isec->owner) != 0
927be08e
AM
12467 && elf_gp (isec->owner) != off)
12468 return FALSE;
12469
12470 elf_gp (isec->owner) = off;
12471 return TRUE;
4c52953f 12472 }
927be08e
AM
12473
12474 /* During the second pass toc_first_sec points to the start of
12475 a toc group, and toc_curr is used to track the old elf_gp.
12476 We use toc_bfd to ensure we only look at each bfd once. */
12477 if (htab->toc_bfd == isec->owner)
12478 return TRUE;
12479 htab->toc_bfd = isec->owner;
12480
12481 if (htab->toc_first_sec == NULL
12482 || htab->toc_curr != elf_gp (isec->owner))
12483 {
12484 htab->toc_curr = elf_gp (isec->owner);
12485 htab->toc_first_sec = isec;
12486 }
12487 addr = (htab->toc_first_sec->output_offset
12488 + htab->toc_first_sec->output_section->vma);
06bcf541 12489 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
927be08e
AM
12490 elf_gp (isec->owner) = off;
12491
12492 return TRUE;
ad8e1ba5
AM
12493}
12494
927be08e
AM
12495/* Called via elf_link_hash_traverse to merge GOT entries for global
12496 symbol H. */
12497
12498static bfd_boolean
12499merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12500{
12501 if (h->root.type == bfd_link_hash_indirect)
12502 return TRUE;
12503
927be08e
AM
12504 merge_got_entries (&h->got.glist);
12505
12506 return TRUE;
12507}
12508
12509/* Called via elf_link_hash_traverse to allocate GOT entries for global
12510 symbol H. */
12511
12512static bfd_boolean
12513reallocate_got (struct elf_link_hash_entry *h, void *inf)
12514{
12515 struct got_entry *gent;
12516
12517 if (h->root.type == bfd_link_hash_indirect)
12518 return TRUE;
12519
927be08e
AM
12520 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12521 if (!gent->is_indirect)
12522 allocate_got (h, (struct bfd_link_info *) inf, gent);
12523 return TRUE;
12524}
12525
12526/* Called on the first multitoc pass after the last call to
12527 ppc64_elf_next_toc_section. This function removes duplicate GOT
12528 entries. */
12529
12530bfd_boolean
12531ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
12532{
12533 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
12534 struct bfd *ibfd, *ibfd2;
12535 bfd_boolean done_something;
12536
12537 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 12538
7865406b
AM
12539 if (!htab->do_multi_toc)
12540 return FALSE;
12541
d0fae19d 12542 /* Merge global sym got entries within a toc group. */
927be08e
AM
12543 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12544
12545 /* And tlsld_got. */
c72f2fb2 12546 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12547 {
12548 struct got_entry *ent, *ent2;
12549
12550 if (!is_ppc64_elf (ibfd))
12551 continue;
12552
12553 ent = ppc64_tlsld_got (ibfd);
12554 if (!ent->is_indirect
12555 && ent->got.offset != (bfd_vma) -1)
12556 {
c72f2fb2 12557 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
927be08e
AM
12558 {
12559 if (!is_ppc64_elf (ibfd2))
12560 continue;
12561
12562 ent2 = ppc64_tlsld_got (ibfd2);
12563 if (!ent2->is_indirect
12564 && ent2->got.offset != (bfd_vma) -1
12565 && elf_gp (ibfd2) == elf_gp (ibfd))
12566 {
12567 ent2->is_indirect = TRUE;
12568 ent2->got.ent = ent;
12569 }
12570 }
12571 }
12572 }
12573
12574 /* Zap sizes of got sections. */
33e44f2e
AM
12575 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12576 htab->elf.irelplt->size -= htab->got_reli_size;
927be08e
AM
12577 htab->got_reli_size = 0;
12578
c72f2fb2 12579 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12580 {
12581 asection *got, *relgot;
12582
12583 if (!is_ppc64_elf (ibfd))
12584 continue;
12585
12586 got = ppc64_elf_tdata (ibfd)->got;
12587 if (got != NULL)
12588 {
12589 got->rawsize = got->size;
12590 got->size = 0;
12591 relgot = ppc64_elf_tdata (ibfd)->relgot;
12592 relgot->rawsize = relgot->size;
12593 relgot->size = 0;
12594 }
12595 }
12596
12597 /* Now reallocate the got, local syms first. We don't need to
12598 allocate section contents again since we never increase size. */
c72f2fb2 12599 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12600 {
12601 struct got_entry **lgot_ents;
12602 struct got_entry **end_lgot_ents;
12603 struct plt_entry **local_plt;
12604 struct plt_entry **end_local_plt;
f961d9dd 12605 unsigned char *lgot_masks;
927be08e
AM
12606 bfd_size_type locsymcount;
12607 Elf_Internal_Shdr *symtab_hdr;
19e08130 12608 asection *s;
927be08e
AM
12609
12610 if (!is_ppc64_elf (ibfd))
12611 continue;
12612
12613 lgot_ents = elf_local_got_ents (ibfd);
12614 if (!lgot_ents)
12615 continue;
12616
12617 symtab_hdr = &elf_symtab_hdr (ibfd);
12618 locsymcount = symtab_hdr->sh_info;
12619 end_lgot_ents = lgot_ents + locsymcount;
12620 local_plt = (struct plt_entry **) end_lgot_ents;
12621 end_local_plt = local_plt + locsymcount;
f961d9dd 12622 lgot_masks = (unsigned char *) end_local_plt;
927be08e 12623 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
12624 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12625 {
12626 struct got_entry *ent;
12627
12628 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 12629 {
19e08130
AM
12630 unsigned int ent_size = 8;
12631 unsigned int rel_size = sizeof (Elf64_External_Rela);
12632
d0fae19d
AM
12633 ent->got.offset = s->size;
12634 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 12635 {
19e08130
AM
12636 ent_size *= 2;
12637 rel_size *= 2;
12638 }
12639 s->size += ent_size;
37da22e5 12640 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
19e08130 12641 {
33e44f2e 12642 htab->elf.irelplt->size += rel_size;
19e08130
AM
12643 htab->got_reli_size += rel_size;
12644 }
6a3858a6 12645 else if (bfd_link_pic (info)
f749f26e 12646 && !(ent->tls_type != 0
6a3858a6 12647 && bfd_link_executable (info)))
19e08130
AM
12648 {
12649 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12650 srel->size += rel_size;
d0fae19d
AM
12651 }
12652 }
927be08e
AM
12653 }
12654 }
12655
12656 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12657
c72f2fb2 12658 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12659 {
12660 struct got_entry *ent;
12661
12662 if (!is_ppc64_elf (ibfd))
12663 continue;
12664
12665 ent = ppc64_tlsld_got (ibfd);
12666 if (!ent->is_indirect
12667 && ent->got.offset != (bfd_vma) -1)
12668 {
12669 asection *s = ppc64_elf_tdata (ibfd)->got;
12670 ent->got.offset = s->size;
12671 s->size += 16;
f749f26e 12672 if (bfd_link_dll (info))
927be08e
AM
12673 {
12674 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12675 srel->size += sizeof (Elf64_External_Rela);
12676 }
12677 }
12678 }
12679
33e44f2e 12680 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
927be08e 12681 if (!done_something)
c72f2fb2 12682 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
927be08e
AM
12683 {
12684 asection *got;
12685
12686 if (!is_ppc64_elf (ibfd))
12687 continue;
12688
12689 got = ppc64_elf_tdata (ibfd)->got;
12690 if (got != NULL)
12691 {
12692 done_something = got->rawsize != got->size;
12693 if (done_something)
12694 break;
12695 }
12696 }
12697
12698 if (done_something)
e7d1c40c 12699 (*htab->params->layout_sections_again) ();
927be08e
AM
12700
12701 /* Set up for second pass over toc sections to recalculate elf_gp
12702 on input sections. */
12703 htab->toc_bfd = NULL;
12704 htab->toc_first_sec = NULL;
12705 htab->second_toc_pass = TRUE;
12706 return done_something;
12707}
12708
12709/* Called after second pass of multitoc partitioning. */
12710
12711void
12712ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12713{
12714 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12715
12716 /* After the second pass, toc_curr tracks the TOC offset used
12717 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 12718 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
12719}
12720
9b5ecbd0
AM
12721/* No toc references were found in ISEC. If the code in ISEC makes no
12722 calls, then there's no need to use toc adjusting stubs when branching
12723 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
12724 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12725 needed, and 2 if a cyclical call-graph was found but no other reason
12726 for a stub was detected. If called from the top level, a return of
12727 2 means the same as a return of 0. */
9b5ecbd0
AM
12728
12729static int
4ce794b7 12730toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 12731{
9b5ecbd0 12732 int ret;
70cc837d
AM
12733
12734 /* Mark this section as checked. */
12735 isec->call_check_done = 1;
9b5ecbd0 12736
772119ce
AM
12737 /* We know none of our code bearing sections will need toc stubs. */
12738 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12739 return 0;
12740
eea6121a 12741 if (isec->size == 0)
082c50f8
AM
12742 return 0;
12743
4c52953f
AM
12744 if (isec->output_section == NULL)
12745 return 0;
12746
4c52953f 12747 ret = 0;
70cc837d 12748 if (isec->reloc_count != 0)
9b5ecbd0 12749 {
70cc837d
AM
12750 Elf_Internal_Rela *relstart, *rel;
12751 Elf_Internal_Sym *local_syms;
12752 struct ppc_link_hash_table *htab;
2917689a 12753
70cc837d
AM
12754 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12755 info->keep_memory);
12756 if (relstart == NULL)
12757 return -1;
90aecf7a 12758
70cc837d
AM
12759 /* Look for branches to outside of this section. */
12760 local_syms = NULL;
12761 htab = ppc_hash_table (info);
12762 if (htab == NULL)
12763 return -1;
4c52953f 12764
70cc837d 12765 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 12766 {
70cc837d
AM
12767 enum elf_ppc64_reloc_type r_type;
12768 unsigned long r_symndx;
12769 struct elf_link_hash_entry *h;
12770 struct ppc_link_hash_entry *eh;
12771 Elf_Internal_Sym *sym;
12772 asection *sym_sec;
12773 struct _opd_sec_data *opd;
12774 bfd_vma sym_value;
12775 bfd_vma dest;
12776
12777 r_type = ELF64_R_TYPE (rel->r_info);
12778 if (r_type != R_PPC64_REL24
05d0e962 12779 && r_type != R_PPC64_REL24_NOTOC
70cc837d
AM
12780 && r_type != R_PPC64_REL14
12781 && r_type != R_PPC64_REL14_BRTAKEN
23cedd1d 12782 && r_type != R_PPC64_REL14_BRNTAKEN
5663e321
AM
12783 && r_type != R_PPC64_PLTCALL
12784 && r_type != R_PPC64_PLTCALL_NOTOC)
70cc837d 12785 continue;
4c52953f 12786
70cc837d
AM
12787 r_symndx = ELF64_R_SYM (rel->r_info);
12788 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12789 isec->owner))
4c52953f 12790 {
70cc837d
AM
12791 ret = -1;
12792 break;
12793 }
4c52953f 12794
70cc837d
AM
12795 /* Calls to dynamic lib functions go through a plt call stub
12796 that uses r2. */
ed7007c1 12797 eh = ppc_elf_hash_entry (h);
70cc837d
AM
12798 if (eh != NULL
12799 && (eh->elf.plt.plist != NULL
12800 || (eh->oh != NULL
12801 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12802 {
12803 ret = 1;
12804 break;
4c52953f
AM
12805 }
12806
70cc837d
AM
12807 if (sym_sec == NULL)
12808 /* Ignore other undefined symbols. */
4c52953f 12809 continue;
4c52953f 12810
70cc837d
AM
12811 /* Assume branches to other sections not included in the
12812 link need stubs too, to cover -R and absolute syms. */
12813 if (sym_sec->output_section == NULL)
12814 {
12815 ret = 1;
12816 break;
12817 }
4c52953f 12818
70cc837d
AM
12819 if (h == NULL)
12820 sym_value = sym->st_value;
12821 else
12822 {
12823 if (h->root.type != bfd_link_hash_defined
12824 && h->root.type != bfd_link_hash_defweak)
12825 abort ();
12826 sym_value = h->root.u.def.value;
12827 }
12828 sym_value += rel->r_addend;
4c52953f 12829
70cc837d
AM
12830 /* If this branch reloc uses an opd sym, find the code section. */
12831 opd = get_opd_info (sym_sec);
12832 if (opd != NULL)
12833 {
12834 if (h == NULL && opd->adjust != NULL)
12835 {
12836 long adjust;
4c52953f 12837
92a9c616 12838 adjust = opd->adjust[OPD_NDX (sym_value)];
70cc837d
AM
12839 if (adjust == -1)
12840 /* Assume deleted functions won't ever be called. */
12841 continue;
12842 sym_value += adjust;
12843 }
4c52953f 12844
aef36ac1
AM
12845 dest = opd_entry_value (sym_sec, sym_value,
12846 &sym_sec, NULL, FALSE);
70cc837d
AM
12847 if (dest == (bfd_vma) -1)
12848 continue;
12849 }
12850 else
12851 dest = (sym_value
12852 + sym_sec->output_offset
12853 + sym_sec->output_section->vma);
4c52953f 12854
70cc837d
AM
12855 /* Ignore branch to self. */
12856 if (sym_sec == isec)
12857 continue;
4c52953f 12858
70cc837d
AM
12859 /* If the called function uses the toc, we need a stub. */
12860 if (sym_sec->has_toc_reloc
12861 || sym_sec->makes_toc_func_call)
4c52953f 12862 {
70cc837d 12863 ret = 1;
4c52953f
AM
12864 break;
12865 }
70cc837d
AM
12866
12867 /* Assume any branch that needs a long branch stub might in fact
12868 need a plt_branch stub. A plt_branch stub uses r2. */
12869 else if (dest - (isec->output_offset
12870 + isec->output_section->vma
6911b7dc
AM
12871 + rel->r_offset) + (1 << 25)
12872 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12873 ? h->other
12874 : sym->st_other))
4c52953f 12875 {
70cc837d
AM
12876 ret = 1;
12877 break;
12878 }
12879
12880 /* If calling back to a section in the process of being
12881 tested, we can't say for sure that no toc adjusting stubs
12882 are needed, so don't return zero. */
12883 else if (sym_sec->call_check_in_progress)
12884 ret = 2;
12885
12886 /* Branches to another section that itself doesn't have any TOC
12887 references are OK. Recursively call ourselves to check. */
12888 else if (!sym_sec->call_check_done)
12889 {
12890 int recur;
12891
12892 /* Mark current section as indeterminate, so that other
12893 sections that call back to current won't be marked as
12894 known. */
12895 isec->call_check_in_progress = 1;
12896 recur = toc_adjusting_stub_needed (info, sym_sec);
12897 isec->call_check_in_progress = 0;
12898
4c52953f
AM
12899 if (recur != 0)
12900 {
70cc837d
AM
12901 ret = recur;
12902 if (recur != 2)
12903 break;
4c52953f
AM
12904 }
12905 }
4c52953f 12906 }
70cc837d 12907
c9594989
AM
12908 if (elf_symtab_hdr (isec->owner).contents
12909 != (unsigned char *) local_syms)
70cc837d
AM
12910 free (local_syms);
12911 if (elf_section_data (isec)->relocs != relstart)
12912 free (relstart);
9b5ecbd0
AM
12913 }
12914
70cc837d
AM
12915 if ((ret & 1) == 0
12916 && isec->map_head.s != NULL
12917 && (strcmp (isec->output_section->name, ".init") == 0
12918 || strcmp (isec->output_section->name, ".fini") == 0))
12919 {
12920 if (isec->map_head.s->has_toc_reloc
12921 || isec->map_head.s->makes_toc_func_call)
12922 ret = 1;
12923 else if (!isec->map_head.s->call_check_done)
12924 {
12925 int recur;
12926 isec->call_check_in_progress = 1;
12927 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12928 isec->call_check_in_progress = 0;
12929 if (recur != 0)
12930 ret = recur;
12931 }
12932 }
12933
12934 if (ret == 1)
12935 isec->makes_toc_func_call = 1;
4c52953f 12936
9b5ecbd0
AM
12937 return ret;
12938}
12939
721956f4
AM
12940/* The linker repeatedly calls this function for each input section,
12941 in the order that input sections are linked into output sections.
12942 Build lists of input sections to determine groupings between which
12943 we may insert linker stubs. */
12944
9b5ecbd0 12945bfd_boolean
4ce794b7 12946ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
12947{
12948 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12949
4dfe6ac6
NC
12950 if (htab == NULL)
12951 return FALSE;
12952
734b6cf9 12953 if ((isec->output_section->flags & SEC_CODE) != 0
6f20ed8a 12954 && isec->output_section->id < htab->sec_info_arr_size)
721956f4 12955 {
3d6f9012
AM
12956 /* This happens to make the list in reverse order,
12957 which is what we want. */
6f20ed8a
AM
12958 htab->sec_info[isec->id].u.list
12959 = htab->sec_info[isec->output_section->id].u.list;
12960 htab->sec_info[isec->output_section->id].u.list = isec;
721956f4 12961 }
ad8e1ba5 12962
4c52953f 12963 if (htab->multi_toc_needed)
9b5ecbd0 12964 {
8b974ba3
AM
12965 /* Analyse sections that aren't already flagged as needing a
12966 valid toc pointer. Exclude .fixup for the linux kernel.
12967 .fixup contains branches, but only back to the function that
12968 hit an exception. */
12969 if (!(isec->has_toc_reloc
12970 || (isec->flags & SEC_CODE) == 0
12971 || strcmp (isec->name, ".fixup") == 0
12972 || isec->call_check_done))
12973 {
12974 if (toc_adjusting_stub_needed (info, isec) < 0)
6683a28d 12975 return FALSE;
8b974ba3
AM
12976 }
12977 /* Make all sections use the TOC assigned for this object file.
12978 This will be wrong for pasted sections; We fix that in
12979 check_pasted_section(). */
12980 if (elf_gp (isec->owner) != 0)
12981 htab->toc_curr = elf_gp (isec->owner);
12982 }
12983
6f20ed8a 12984 htab->sec_info[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 12985 return TRUE;
721956f4
AM
12986}
12987
70cc837d
AM
12988/* Check that all .init and .fini sections use the same toc, if they
12989 have toc relocs. */
12990
12991static bfd_boolean
12992check_pasted_section (struct bfd_link_info *info, const char *name)
12993{
12994 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12995
12996 if (o != NULL)
12997 {
12998 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12999 bfd_vma toc_off = 0;
13000 asection *i;
13001
13002 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13003 if (i->has_toc_reloc)
13004 {
13005 if (toc_off == 0)
6f20ed8a
AM
13006 toc_off = htab->sec_info[i->id].toc_off;
13007 else if (toc_off != htab->sec_info[i->id].toc_off)
70cc837d
AM
13008 return FALSE;
13009 }
6683a28d
AM
13010
13011 if (toc_off == 0)
13012 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13013 if (i->makes_toc_func_call)
13014 {
6f20ed8a 13015 toc_off = htab->sec_info[i->id].toc_off;
6683a28d
AM
13016 break;
13017 }
13018
70cc837d
AM
13019 /* Make sure the whole pasted function uses the same toc offset. */
13020 if (toc_off != 0)
13021 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
6f20ed8a 13022 htab->sec_info[i->id].toc_off = toc_off;
70cc837d
AM
13023 }
13024 return TRUE;
13025}
13026
13027bfd_boolean
13028ppc64_elf_check_init_fini (struct bfd_link_info *info)
13029{
13030 return (check_pasted_section (info, ".init")
13031 & check_pasted_section (info, ".fini"));
13032}
13033
721956f4
AM
13034/* See whether we can group stub sections together. Grouping stub
13035 sections may result in fewer stubs. More importantly, we need to
13036 put all .init* and .fini* stubs at the beginning of the .init or
13037 .fini output sections respectively, because glibc splits the
13038 _init and _fini functions into multiple parts. Putting a stub in
13039 the middle of a function is not a good idea. */
13040
6f20ed8a
AM
13041static bfd_boolean
13042group_sections (struct bfd_link_info *info,
4ce794b7
AM
13043 bfd_size_type stub_group_size,
13044 bfd_boolean stubs_always_before_branch)
721956f4 13045{
6f20ed8a
AM
13046 struct ppc_link_hash_table *htab;
13047 asection *osec;
7c8fe5c4
AM
13048 bfd_boolean suppress_size_errors;
13049
6f20ed8a
AM
13050 htab = ppc_hash_table (info);
13051 if (htab == NULL)
13052 return FALSE;
13053
7c8fe5c4 13054 suppress_size_errors = FALSE;
7c8fe5c4
AM
13055 if (stub_group_size == 1)
13056 {
13057 /* Default values. */
13058 if (stubs_always_before_branch)
09f92717 13059 stub_group_size = 0x1e00000;
7c8fe5c4 13060 else
09f92717 13061 stub_group_size = 0x1c00000;
7c8fe5c4
AM
13062 suppress_size_errors = TRUE;
13063 }
13064
6f20ed8a 13065 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
721956f4 13066 {
6f20ed8a
AM
13067 asection *tail;
13068
13069 if (osec->id >= htab->sec_info_arr_size)
13070 continue;
13071
13072 tail = htab->sec_info[osec->id].u.list;
734b6cf9 13073 while (tail != NULL)
721956f4 13074 {
734b6cf9
AM
13075 asection *curr;
13076 asection *prev;
13077 bfd_size_type total;
13078 bfd_boolean big_sec;
13079 bfd_vma curr_toc;
6f20ed8a 13080 struct map_stub *group;
09f92717 13081 bfd_size_type group_size;
734b6cf9
AM
13082
13083 curr = tail;
eea6121a 13084 total = tail->size;
09f92717
AM
13085 group_size = (ppc64_elf_section_data (tail) != NULL
13086 && ppc64_elf_section_data (tail)->has_14bit_branch
13087 ? stub_group_size >> 10 : stub_group_size);
13088
13089 big_sec = total > group_size;
7c8fe5c4 13090 if (big_sec && !suppress_size_errors)
695344c0 13091 /* xgettext:c-format */
871b3ab2 13092 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
4eca0228 13093 tail->owner, tail);
6f20ed8a 13094 curr_toc = htab->sec_info[tail->id].toc_off;
734b6cf9 13095
6f20ed8a 13096 while ((prev = htab->sec_info[curr->id].u.list) != NULL
734b6cf9 13097 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
13098 < (ppc64_elf_section_data (prev) != NULL
13099 && ppc64_elf_section_data (prev)->has_14bit_branch
09f92717 13100 ? (group_size = stub_group_size >> 10) : group_size))
6f20ed8a 13101 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13102 curr = prev;
13103
13104 /* OK, the size from the start of CURR to the end is less
09f92717 13105 than group_size and thus can be handled by one stub
734b6cf9 13106 section. (or the tail section is itself larger than
09f92717
AM
13107 group_size, in which case we may be toast.) We should
13108 really be keeping track of the total size of stubs added
13109 here, as stubs contribute to the final output section
13110 size. That's a little tricky, and this way will only
13111 break if stubs added make the total size more than 2^25,
13112 ie. for the default stub_group_size, if stubs total more
13113 than 2097152 bytes, or nearly 75000 plt call stubs. */
6f20ed8a
AM
13114 group = bfd_alloc (curr->owner, sizeof (*group));
13115 if (group == NULL)
13116 return FALSE;
13117 group->link_sec = curr;
13118 group->stub_sec = NULL;
a4b6fadd 13119 group->needs_save_res = 0;
df136d64
AM
13120 group->lr_restore = 0;
13121 group->eh_size = 0;
13122 group->eh_base = 0;
a4b6fadd
AM
13123 group->next = htab->group;
13124 htab->group = group;
734b6cf9 13125 do
721956f4 13126 {
6f20ed8a 13127 prev = htab->sec_info[tail->id].u.list;
734b6cf9 13128 /* Set up this stub group. */
6f20ed8a 13129 htab->sec_info[tail->id].u.group = group;
721956f4 13130 }
734b6cf9
AM
13131 while (tail != curr && (tail = prev) != NULL);
13132
09f92717 13133 /* But wait, there's more! Input sections up to group_size
734b6cf9
AM
13134 bytes before the stub section can be handled by it too.
13135 Don't do this if we have a really large section after the
13136 stubs, as adding more stubs increases the chance that
13137 branches may not reach into the stub section. */
13138 if (!stubs_always_before_branch && !big_sec)
13139 {
13140 total = 0;
13141 while (prev != NULL
13142 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
13143 < (ppc64_elf_section_data (prev) != NULL
13144 && ppc64_elf_section_data (prev)->has_14bit_branch
2cdcc330
AM
13145 ? (group_size = stub_group_size >> 10)
13146 : group_size))
6f20ed8a 13147 && htab->sec_info[prev->id].toc_off == curr_toc)
734b6cf9
AM
13148 {
13149 tail = prev;
6f20ed8a
AM
13150 prev = htab->sec_info[tail->id].u.list;
13151 htab->sec_info[tail->id].u.group = group;
734b6cf9
AM
13152 }
13153 }
13154 tail = prev;
721956f4
AM
13155 }
13156 }
6f20ed8a 13157 return TRUE;
721956f4
AM
13158}
13159
58d180e8
AM
13160static const unsigned char glink_eh_frame_cie[] =
13161{
13162 0, 0, 0, 16, /* length. */
13163 0, 0, 0, 0, /* id. */
13164 1, /* CIE version. */
13165 'z', 'R', 0, /* Augmentation string. */
13166 4, /* Code alignment. */
13167 0x78, /* Data alignment. */
13168 65, /* RA reg. */
13169 1, /* Augmentation size. */
13170 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
2e0ce1c8 13171 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
58d180e8
AM
13172};
13173
d969d15f
AM
13174/* Stripping output sections is normally done before dynamic section
13175 symbols have been allocated. This function is called later, and
13176 handles cases like htab->brlt which is mapped to its own output
13177 section. */
13178
13179static void
13180maybe_strip_output (struct bfd_link_info *info, asection *isec)
13181{
13182 if (isec->size == 0
13183 && isec->output_section->size == 0
53d8967a 13184 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
13185 && !bfd_section_removed_from_list (info->output_bfd,
13186 isec->output_section)
13187 && elf_section_data (isec->output_section)->dynindx == 0)
13188 {
13189 isec->output_section->flags |= SEC_EXCLUDE;
13190 bfd_section_list_remove (info->output_bfd, isec->output_section);
13191 info->output_bfd->section_count--;
13192 }
13193}
13194
721956f4
AM
13195/* Determine and set the size of the stub section for a final link.
13196
13197 The basic idea here is to examine all the relocations looking for
13198 PC-relative calls to a target that is unreachable with a "bl"
13199 instruction. */
13200
b34976b6 13201bfd_boolean
e7d1c40c 13202ppc64_elf_size_stubs (struct bfd_link_info *info)
721956f4
AM
13203{
13204 bfd_size_type stub_group_size;
b34976b6 13205 bfd_boolean stubs_always_before_branch;
721956f4
AM
13206 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13207
4dfe6ac6
NC
13208 if (htab == NULL)
13209 return FALSE;
13210
e10a07b3
AM
13211 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13212 htab->params->power10_stubs = 0;
13213
0e1862bb 13214 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
e7d1c40c 13215 htab->params->plt_thread_safe = 1;
b9e5796b 13216 if (!htab->opd_abi)
e7d1c40c
AM
13217 htab->params->plt_thread_safe = 0;
13218 else if (htab->params->plt_thread_safe == -1)
794e51c0 13219 {
e2458743 13220 static const char *const thread_starter[] =
794e51c0
AM
13221 {
13222 "pthread_create",
13223 /* libstdc++ */
13224 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13225 /* librt */
13226 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13227 "mq_notify", "create_timer",
13228 /* libanl */
13229 "getaddrinfo_a",
13230 /* libgomp */
2300b5a1 13231 "GOMP_parallel",
794e51c0 13232 "GOMP_parallel_start",
2300b5a1 13233 "GOMP_parallel_loop_static",
794e51c0 13234 "GOMP_parallel_loop_static_start",
2300b5a1 13235 "GOMP_parallel_loop_dynamic",
794e51c0 13236 "GOMP_parallel_loop_dynamic_start",
2300b5a1 13237 "GOMP_parallel_loop_guided",
794e51c0 13238 "GOMP_parallel_loop_guided_start",
2300b5a1 13239 "GOMP_parallel_loop_runtime",
794e51c0 13240 "GOMP_parallel_loop_runtime_start",
2300b5a1 13241 "GOMP_parallel_sections",
68ffbac6 13242 "GOMP_parallel_sections_start",
f9dffbf0
AM
13243 /* libgo */
13244 "__go_go",
794e51c0
AM
13245 };
13246 unsigned i;
13247
a4b6fadd 13248 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
794e51c0
AM
13249 {
13250 struct elf_link_hash_entry *h;
13251 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13252 FALSE, FALSE, TRUE);
e7d1c40c
AM
13253 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13254 if (htab->params->plt_thread_safe)
794e51c0
AM
13255 break;
13256 }
13257 }
e7d1c40c
AM
13258 stubs_always_before_branch = htab->params->group_size < 0;
13259 if (htab->params->group_size < 0)
13260 stub_group_size = -htab->params->group_size;
721956f4 13261 else
e7d1c40c 13262 stub_group_size = htab->params->group_size;
721956f4 13263
6f20ed8a
AM
13264 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13265 return FALSE;
721956f4 13266
a804e476
AM
13267 htab->tga_group = NULL;
13268 if (!htab->params->no_tls_get_addr_regsave
13269 && htab->tga_desc_fd != NULL
13270 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13271 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13272 && htab->tls_get_addr_fd != NULL
13273 && is_static_defined (&htab->tls_get_addr_fd->elf))
13274 {
13275 asection *sym_sec, *code_sec, *stub_sec;
13276 bfd_vma sym_value;
13277 struct _opd_sec_data *opd;
13278
13279 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13280 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13281 code_sec = sym_sec;
13282 opd = get_opd_info (sym_sec);
13283 if (opd != NULL)
13284 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, FALSE);
13285 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13286 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13287 htab->tga_group->link_sec);
13288 if (stub_sec == NULL)
13289 return FALSE;
13290 htab->tga_group->stub_sec = stub_sec;
13291
13292 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13293 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13294 htab->tga_desc_fd->elf.root.u.def.value = 0;
13295 htab->tga_desc_fd->elf.type = STT_FUNC;
13296 htab->tga_desc_fd->elf.def_regular = 1;
13297 htab->tga_desc_fd->elf.non_elf = 0;
13298 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, TRUE);
13299 }
13300
c9301e31
AM
13301#define STUB_SHRINK_ITER 20
13302 /* Loop until no stubs added. After iteration 20 of this loop we may
13303 exit on a stub section shrinking. This is to break out of a
13304 pathological case where adding stubs on one iteration decreases
13305 section gaps (perhaps due to alignment), which then requires
13306 fewer or smaller stubs on the next iteration. */
13307
721956f4
AM
13308 while (1)
13309 {
13310 bfd *input_bfd;
13311 unsigned int bfd_indx;
a4b6fadd 13312 struct map_stub *group;
721956f4
AM
13313
13314 htab->stub_iteration += 1;
721956f4
AM
13315
13316 for (input_bfd = info->input_bfds, bfd_indx = 0;
13317 input_bfd != NULL;
c72f2fb2 13318 input_bfd = input_bfd->link.next, bfd_indx++)
721956f4
AM
13319 {
13320 Elf_Internal_Shdr *symtab_hdr;
13321 asection *section;
6cdc0ccc 13322 Elf_Internal_Sym *local_syms = NULL;
721956f4 13323
0c8d6e5c 13324 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
13325 continue;
13326
721956f4 13327 /* We'll need the symbol table in a second. */
0ffa91dd 13328 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
13329 if (symtab_hdr->sh_info == 0)
13330 continue;
13331
721956f4
AM
13332 /* Walk over each section attached to the input bfd. */
13333 for (section = input_bfd->sections;
13334 section != NULL;
13335 section = section->next)
13336 {
721956f4 13337 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
13338
13339 /* If there aren't any relocs, then there's nothing more
13340 to do. */
13341 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
13342 || (section->flags & SEC_ALLOC) == 0
13343 || (section->flags & SEC_LOAD) == 0
13344 || (section->flags & SEC_CODE) == 0
721956f4
AM
13345 || section->reloc_count == 0)
13346 continue;
13347
13348 /* If this section is a link-once section that will be
13349 discarded, then don't create any stubs. */
13350 if (section->output_section == NULL
927be08e 13351 || section->output_section->owner != info->output_bfd)
721956f4
AM
13352 continue;
13353
1e2f5b6e
AM
13354 /* Get the relocs. */
13355 internal_relocs
4ce794b7 13356 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 13357 info->keep_memory);
721956f4 13358 if (internal_relocs == NULL)
1e2f5b6e 13359 goto error_ret_free_local;
721956f4
AM
13360
13361 /* Now examine each relocation. */
13362 irela = internal_relocs;
13363 irelaend = irela + section->reloc_count;
13364 for (; irela < irelaend; irela++)
13365 {
4ce794b7
AM
13366 enum elf_ppc64_reloc_type r_type;
13367 unsigned int r_indx;
721956f4
AM
13368 enum ppc_stub_type stub_type;
13369 struct ppc_stub_hash_entry *stub_entry;
8387904d 13370 asection *sym_sec, *code_sec;
e054468f 13371 bfd_vma sym_value, code_value;
721956f4 13372 bfd_vma destination;
6911b7dc 13373 unsigned long local_off;
8843416a 13374 bfd_boolean ok_dest;
721956f4 13375 struct ppc_link_hash_entry *hash;
8387904d 13376 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
13377 struct elf_link_hash_entry *h;
13378 Elf_Internal_Sym *sym;
721956f4
AM
13379 char *stub_name;
13380 const asection *id_sec;
74f0fb50 13381 struct _opd_sec_data *opd;
e054468f 13382 struct plt_entry *plt_ent;
721956f4
AM
13383
13384 r_type = ELF64_R_TYPE (irela->r_info);
13385 r_indx = ELF64_R_SYM (irela->r_info);
13386
4ce794b7 13387 if (r_type >= R_PPC64_max)
721956f4
AM
13388 {
13389 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 13390 goto error_ret_free_internal;
721956f4
AM
13391 }
13392
13393 /* Only look for stubs on branch instructions. */
4ce794b7 13394 if (r_type != R_PPC64_REL24
05d0e962 13395 && r_type != R_PPC64_REL24_NOTOC
4ce794b7
AM
13396 && r_type != R_PPC64_REL14
13397 && r_type != R_PPC64_REL14_BRTAKEN
13398 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
13399 continue;
13400
13401 /* Now determine the call target, its name, value,
13402 section. */
411e1bfb
AM
13403 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13404 r_indx, input_bfd))
13405 goto error_ret_free_internal;
ed7007c1 13406 hash = ppc_elf_hash_entry (h);
411e1bfb 13407
8843416a 13408 ok_dest = FALSE;
8387904d 13409 fdh = NULL;
7fe2b9a6 13410 sym_value = 0;
411e1bfb 13411 if (hash == NULL)
721956f4 13412 {
411e1bfb 13413 sym_value = sym->st_value;
c27b8c2a
AM
13414 if (sym_sec != NULL
13415 && sym_sec->output_section != NULL)
13416 ok_dest = TRUE;
721956f4 13417 }
7fe2b9a6
AM
13418 else if (hash->elf.root.type == bfd_link_hash_defined
13419 || hash->elf.root.type == bfd_link_hash_defweak)
13420 {
13421 sym_value = hash->elf.root.u.def.value;
13422 if (sym_sec->output_section != NULL)
13423 ok_dest = TRUE;
13424 }
13425 else if (hash->elf.root.type == bfd_link_hash_undefweak
13426 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 13427 {
99877b66 13428 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
13429 use the func descriptor sym instead if it is
13430 defined. */
ceb1f1ef 13431 if (hash->elf.root.root.string[0] == '.'
8c5b4e52 13432 && hash->oh != NULL)
8387904d 13433 {
8c5b4e52 13434 fdh = ppc_follow_link (hash->oh);
8387904d
AM
13435 if (fdh->elf.root.type == bfd_link_hash_defined
13436 || fdh->elf.root.type == bfd_link_hash_defweak)
13437 {
13438 sym_sec = fdh->elf.root.u.def.section;
13439 sym_value = fdh->elf.root.u.def.value;
13440 if (sym_sec->output_section != NULL)
13441 ok_dest = TRUE;
13442 }
99877b66
AM
13443 else
13444 fdh = NULL;
8387904d 13445 }
7fe2b9a6
AM
13446 }
13447 else
13448 {
13449 bfd_set_error (bfd_error_bad_value);
13450 goto error_ret_free_internal;
721956f4
AM
13451 }
13452
8843416a 13453 destination = 0;
6911b7dc 13454 local_off = 0;
8843416a
AM
13455 if (ok_dest)
13456 {
13457 sym_value += irela->r_addend;
13458 destination = (sym_value
13459 + sym_sec->output_offset
13460 + sym_sec->output_section->vma);
6911b7dc
AM
13461 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13462 ? hash->elf.other
13463 : sym->st_other);
8843416a
AM
13464 }
13465
8387904d 13466 code_sec = sym_sec;
e054468f 13467 code_value = sym_value;
74f0fb50
AM
13468 opd = get_opd_info (sym_sec);
13469 if (opd != NULL)
8387904d
AM
13470 {
13471 bfd_vma dest;
13472
74f0fb50 13473 if (hash == NULL && opd->adjust != NULL)
8387904d 13474 {
51aecdc5 13475 long adjust = opd->adjust[OPD_NDX (sym_value)];
8387904d
AM
13476 if (adjust == -1)
13477 continue;
e054468f 13478 code_value += adjust;
8387904d
AM
13479 sym_value += adjust;
13480 }
13481 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 13482 &code_sec, &code_value, FALSE);
8387904d
AM
13483 if (dest != (bfd_vma) -1)
13484 {
13485 destination = dest;
13486 if (fdh != NULL)
13487 {
13488 /* Fixup old ABI sym to point at code
13489 entry. */
99877b66 13490 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 13491 hash->elf.root.u.def.section = code_sec;
e054468f 13492 hash->elf.root.u.def.value = code_value;
8387904d
AM
13493 }
13494 }
13495 }
13496
721956f4 13497 /* Determine what (if any) linker stub is needed. */
e054468f 13498 plt_ent = NULL;
721956f4 13499 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
13500 &plt_ent, destination,
13501 local_off);
ad8e1ba5 13502
05d0e962
AM
13503 if (r_type == R_PPC64_REL24_NOTOC)
13504 {
13505 if (stub_type == ppc_stub_plt_call)
13506 stub_type = ppc_stub_plt_call_notoc;
13507 else if (stub_type == ppc_stub_long_branch
13508 || (code_sec != NULL
13509 && code_sec->output_section != NULL
13510 && (((hash ? hash->elf.other : sym->st_other)
13511 & STO_PPC64_LOCAL_MASK)
4a4e7361 13512 > 1 << STO_PPC64_LOCAL_BIT)))
05d0e962
AM
13513 stub_type = ppc_stub_long_branch_notoc;
13514 }
13515 else if (stub_type != ppc_stub_plt_call)
ad8e1ba5
AM
13516 {
13517 /* Check whether we need a TOC adjusting stub.
13518 Since the linker pastes together pieces from
13519 different object files when creating the
13520 _init and _fini functions, it may be that a
13521 call to what looks like a local sym is in
13522 fact a call needing a TOC adjustment. */
33cb30a1
AM
13523 if ((code_sec != NULL
13524 && code_sec->output_section != NULL
33cb30a1 13525 && (code_sec->has_toc_reloc
918dc783
AM
13526 || code_sec->makes_toc_func_call)
13527 && (htab->sec_info[code_sec->id].toc_off
13528 != htab->sec_info[section->id].toc_off))
33cb30a1
AM
13529 || (((hash ? hash->elf.other : sym->st_other)
13530 & STO_PPC64_LOCAL_MASK)
13531 == 1 << STO_PPC64_LOCAL_BIT))
ad8e1ba5
AM
13532 stub_type = ppc_stub_long_branch_r2off;
13533 }
13534
721956f4
AM
13535 if (stub_type == ppc_stub_none)
13536 continue;
13537
411e1bfb
AM
13538 /* __tls_get_addr calls might be eliminated. */
13539 if (stub_type != ppc_stub_plt_call
05d0e962 13540 && stub_type != ppc_stub_plt_call_notoc
411e1bfb 13541 && hash != NULL
ed7007c1 13542 && is_tls_get_addr (&hash->elf, htab)
411e1bfb
AM
13543 && section->has_tls_reloc
13544 && irela != internal_relocs)
13545 {
13546 /* Get tls info. */
f961d9dd 13547 unsigned char *tls_mask;
411e1bfb 13548
3a71aa26 13549 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
13550 irela - 1, input_bfd))
13551 goto error_ret_free_internal;
abc489c6
AM
13552 if ((*tls_mask & TLS_TLS) != 0
13553 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
411e1bfb
AM
13554 continue;
13555 }
13556
f378ab09 13557 if (stub_type == ppc_stub_plt_call)
794e51c0 13558 {
6e1816be
AM
13559 if (!htab->opd_abi
13560 && htab->params->plt_localentry0 != 0
13561 && is_elfv2_localentry0 (&hash->elf))
13562 htab->has_plt_localentry0 = 1;
13563 else if (irela + 1 < irelaend
13564 && irela[1].r_offset == irela->r_offset + 4
13565 && (ELF64_R_TYPE (irela[1].r_info)
13566 == R_PPC64_TOCSAVE))
f378ab09
AM
13567 {
13568 if (!tocsave_find (htab, INSERT,
13569 &local_syms, irela + 1, input_bfd))
13570 goto error_ret_free_internal;
13571 }
f378ab09
AM
13572 else
13573 stub_type = ppc_stub_plt_call_r2save;
794e51c0 13574 }
3b421ab3 13575
721956f4 13576 /* Support for grouping stub sections. */
6f20ed8a 13577 id_sec = htab->sec_info[section->id].u.group->link_sec;
721956f4
AM
13578
13579 /* Get the name of this stub. */
13580 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13581 if (!stub_name)
13582 goto error_ret_free_internal;
13583
13584 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 13585 stub_name, FALSE, FALSE);
721956f4
AM
13586 if (stub_entry != NULL)
13587 {
05d0e962 13588 enum ppc_stub_type old_type;
e10a07b3 13589
05d0e962
AM
13590 /* A stub has already been created, but it may
13591 not be the required type. We shouldn't be
13592 transitioning from plt_call to long_branch
13593 stubs or vice versa, but we might be
13594 upgrading from plt_call to plt_call_r2save or
13595 from long_branch to long_branch_r2off. */
721956f4 13596 free (stub_name);
e10a07b3
AM
13597 if (htab->params->power10_stubs == -1)
13598 {
13599 /* For --power10-stubs=auto, don't merge _notoc
13600 and other varieties of stubs. (The _both
13601 variety won't be created.) */
13602 bfd_boolean notoc = r_type == R_PPC64_REL24_NOTOC;
13603 struct ppc_stub_hash_entry *alt_stub
13604 = select_alt_stub (stub_entry, notoc);
13605
13606 if (alt_stub == NULL)
13607 {
13608 alt_stub = (struct ppc_stub_hash_entry *)
13609 stub_hash_newfunc (NULL,
13610 &htab->stub_hash_table,
13611 stub_entry->root.string);
13612 if (alt_stub == NULL)
13613 {
13614 /* xgettext:c-format */
13615 _bfd_error_handler
13616 (_("%pB: cannot create stub entry %s"),
13617 section->owner, stub_entry->root.string);
13618 goto error_ret_free_internal;
13619 }
13620 *alt_stub = *stub_entry;
13621 stub_entry->root.next = &alt_stub->root;
13622 if (notoc)
13623 /* Sort notoc stubs first, for no good
13624 reason. */
13625 alt_stub = stub_entry;
13626 alt_stub->stub_type = stub_type;
13627 }
13628 stub_entry = alt_stub;
13629 }
05d0e962
AM
13630 old_type = stub_entry->stub_type;
13631 switch (old_type)
13632 {
13633 default:
13634 abort ();
13635
13636 case ppc_stub_save_res:
13637 continue;
13638
13639 case ppc_stub_plt_call:
13640 case ppc_stub_plt_call_r2save:
13641 case ppc_stub_plt_call_notoc:
13642 case ppc_stub_plt_call_both:
13643 if (stub_type == ppc_stub_plt_call)
13644 continue;
13645 else if (stub_type == ppc_stub_plt_call_r2save)
13646 {
13647 if (old_type == ppc_stub_plt_call_notoc)
13648 stub_type = ppc_stub_plt_call_both;
13649 }
13650 else if (stub_type == ppc_stub_plt_call_notoc)
13651 {
13652 if (old_type == ppc_stub_plt_call_r2save)
13653 stub_type = ppc_stub_plt_call_both;
13654 }
13655 else
13656 abort ();
13657 break;
13658
13659 case ppc_stub_plt_branch:
13660 case ppc_stub_plt_branch_r2off:
13661 case ppc_stub_plt_branch_notoc:
13662 case ppc_stub_plt_branch_both:
13663 old_type += (ppc_stub_long_branch
13664 - ppc_stub_plt_branch);
13665 /* Fall through. */
13666 case ppc_stub_long_branch:
13667 case ppc_stub_long_branch_r2off:
13668 case ppc_stub_long_branch_notoc:
13669 case ppc_stub_long_branch_both:
13670 if (stub_type == ppc_stub_long_branch)
13671 continue;
13672 else if (stub_type == ppc_stub_long_branch_r2off)
13673 {
13674 if (old_type == ppc_stub_long_branch_notoc)
13675 stub_type = ppc_stub_long_branch_both;
13676 }
13677 else if (stub_type == ppc_stub_long_branch_notoc)
13678 {
13679 if (old_type == ppc_stub_long_branch_r2off)
13680 stub_type = ppc_stub_long_branch_both;
13681 }
13682 else
13683 abort ();
13684 break;
13685 }
13686 if (old_type < stub_type)
794e51c0 13687 stub_entry->stub_type = stub_type;
721956f4
AM
13688 continue;
13689 }
13690
25f53a85 13691 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
13692 if (stub_entry == NULL)
13693 {
13694 free (stub_name);
6cdc0ccc
AM
13695 error_ret_free_internal:
13696 if (elf_section_data (section)->relocs == NULL)
13697 free (internal_relocs);
13698 error_ret_free_local:
c9594989
AM
13699 if (symtab_hdr->contents
13700 != (unsigned char *) local_syms)
6cdc0ccc 13701 free (local_syms);
b34976b6 13702 return FALSE;
721956f4
AM
13703 }
13704
ad8e1ba5 13705 stub_entry->stub_type = stub_type;
05d0e962
AM
13706 if (stub_type >= ppc_stub_plt_call
13707 && stub_type <= ppc_stub_plt_call_both)
e054468f 13708 {
05d0e962
AM
13709 stub_entry->target_value = sym_value;
13710 stub_entry->target_section = sym_sec;
e054468f
AM
13711 }
13712 else
13713 {
05d0e962
AM
13714 stub_entry->target_value = code_value;
13715 stub_entry->target_section = code_sec;
e054468f 13716 }
721956f4 13717 stub_entry->h = hash;
e054468f 13718 stub_entry->plt_ent = plt_ent;
2d7ad24e
AM
13719 stub_entry->symtype
13720 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
6911b7dc 13721 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95 13722
3d58e1fc
AM
13723 if (hash != NULL
13724 && (hash->elf.root.type == bfd_link_hash_defined
13725 || hash->elf.root.type == bfd_link_hash_defweak))
ee75fd95 13726 htab->stub_globals += 1;
721956f4
AM
13727 }
13728
13729 /* We're done with the internal relocs, free them. */
6cdc0ccc 13730 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 13731 free (internal_relocs);
721956f4 13732 }
6cdc0ccc
AM
13733
13734 if (local_syms != NULL
13735 && symtab_hdr->contents != (unsigned char *) local_syms)
13736 {
13737 if (!info->keep_memory)
13738 free (local_syms);
13739 else
13740 symtab_hdr->contents = (unsigned char *) local_syms;
13741 }
721956f4
AM
13742 }
13743
5c3dead3 13744 /* We may have added some stubs. Find out the new size of the
721956f4 13745 stub sections. */
d4aaa2a0 13746 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13747 {
13748 group->lr_restore = 0;
13749 group->eh_size = 0;
13750 if (group->stub_sec != NULL)
13751 {
13752 asection *stub_sec = group->stub_sec;
13753
13754 if (htab->stub_iteration <= STUB_SHRINK_ITER
13755 || stub_sec->rawsize < stub_sec->size)
13756 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13757 stub_sec->rawsize = stub_sec->size;
13758 stub_sec->size = 0;
13759 stub_sec->reloc_count = 0;
13760 stub_sec->flags &= ~SEC_RELOC;
13761 }
13762 }
a804e476
AM
13763 if (htab->tga_group != NULL)
13764 {
13765 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13766 htab->tga_group->eh_size
13767 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13768 htab->tga_group->lr_restore = 23 * 4;
13769 htab->tga_group->stub_sec->size = 24 * 4;
13770 }
eea6121a 13771
ba21f564
AM
13772 if (htab->stub_iteration <= STUB_SHRINK_ITER
13773 || htab->brlt->rawsize < htab->brlt->size)
13774 htab->brlt->rawsize = htab->brlt->size;
eea6121a 13775 htab->brlt->size = 0;
84f5d08e
AM
13776 htab->brlt->reloc_count = 0;
13777 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 13778 if (htab->relbrlt != NULL)
eea6121a 13779 htab->relbrlt->size = 0;
721956f4 13780
63bc6f6c 13781 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 13782
a4b6fadd
AM
13783 for (group = htab->group; group != NULL; group = group->next)
13784 if (group->needs_save_res)
13785 group->stub_sec->size += htab->sfpr->size;
13786
176a0d42
AM
13787 if (info->emitrelocations
13788 && htab->glink != NULL && htab->glink->size != 0)
13789 {
13790 htab->glink->reloc_count = 1;
13791 htab->glink->flags |= SEC_RELOC;
13792 }
13793
58d180e8
AM
13794 if (htab->glink_eh_frame != NULL
13795 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
2d0d44d5 13796 && htab->glink_eh_frame->output_section->size > 8)
58d180e8 13797 {
2e0ce1c8 13798 size_t size = 0, align = 4;
58d180e8 13799
d4aaa2a0 13800 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
13801 if (group->eh_size != 0)
13802 size += (group->eh_size + 17 + align - 1) & -align;
58d180e8 13803 if (htab->glink != NULL && htab->glink->size != 0)
2e0ce1c8 13804 size += (24 + align - 1) & -align;
58d180e8 13805 if (size != 0)
2e0ce1c8
AM
13806 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13807 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13808 size = (size + align - 1) & -align;
58d180e8
AM
13809 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13810 htab->glink_eh_frame->size = size;
13811 }
13812
e7d1c40c 13813 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
13814 for (group = htab->group; group != NULL; group = group->next)
13815 if (group->stub_sec != NULL)
691d2e9a
AM
13816 {
13817 int align = abs (htab->params->plt_stub_align);
13818 group->stub_sec->size
13819 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13820 }
d4aaa2a0
AM
13821
13822 for (group = htab->group; group != NULL; group = group->next)
13823 if (group->stub_sec != NULL
13824 && group->stub_sec->rawsize != group->stub_sec->size
c9301e31 13825 && (htab->stub_iteration <= STUB_SHRINK_ITER
d4aaa2a0 13826 || group->stub_sec->rawsize < group->stub_sec->size))
5c3dead3
AM
13827 break;
13828
d4aaa2a0 13829 if (group == NULL
ba21f564
AM
13830 && (htab->brlt->rawsize == htab->brlt->size
13831 || (htab->stub_iteration > STUB_SHRINK_ITER
13832 && htab->brlt->rawsize > htab->brlt->size))
58d180e8 13833 && (htab->glink_eh_frame == NULL
a804e476
AM
13834 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13835 && (htab->tga_group == NULL
13836 || htab->stub_iteration > 1))
5c3dead3
AM
13837 break;
13838
721956f4 13839 /* Ask the linker to do its stuff. */
e7d1c40c 13840 (*htab->params->layout_sections_again) ();
721956f4
AM
13841 }
13842
da44f4e5
AM
13843 if (htab->glink_eh_frame != NULL
13844 && htab->glink_eh_frame->size != 0)
13845 {
13846 bfd_vma val;
13847 bfd_byte *p, *last_fde;
13848 size_t last_fde_len, size, align, pad;
d4aaa2a0 13849 struct map_stub *group;
da44f4e5 13850
df136d64
AM
13851 /* It is necessary to at least have a rough outline of the
13852 linker generated CIEs and FDEs written before
13853 bfd_elf_discard_info is run, in order for these FDEs to be
13854 indexed in .eh_frame_hdr. */
da44f4e5
AM
13855 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13856 if (p == NULL)
13857 return FALSE;
13858 htab->glink_eh_frame->contents = p;
13859 last_fde = p;
2e0ce1c8 13860 align = 4;
da44f4e5
AM
13861
13862 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13863 /* CIE length (rewrite in case little-endian). */
2e0ce1c8 13864 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
da44f4e5 13865 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
2e0ce1c8 13866 p += last_fde_len + 4;
da44f4e5 13867
d4aaa2a0 13868 for (group = htab->group; group != NULL; group = group->next)
df136d64 13869 if (group->eh_size != 0)
da44f4e5 13870 {
df136d64 13871 group->eh_base = p - htab->glink_eh_frame->contents;
da44f4e5 13872 last_fde = p;
df136d64 13873 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
da44f4e5 13874 /* FDE length. */
2e0ce1c8 13875 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13876 p += 4;
13877 /* CIE pointer. */
13878 val = p - htab->glink_eh_frame->contents;
13879 bfd_put_32 (htab->elf.dynobj, val, p);
13880 p += 4;
13881 /* Offset to stub section, written later. */
13882 p += 4;
13883 /* stub section size. */
d4aaa2a0 13884 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
da44f4e5
AM
13885 p += 4;
13886 /* Augmentation. */
13887 p += 1;
df136d64
AM
13888 /* Make sure we don't have all nops. This is enough for
13889 elf-eh-frame.c to detect the last non-nop opcode. */
13890 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
d4aaa2a0 13891 p = last_fde + last_fde_len + 4;
da44f4e5
AM
13892 }
13893 if (htab->glink != NULL && htab->glink->size != 0)
13894 {
13895 last_fde = p;
2e0ce1c8 13896 last_fde_len = ((24 + align - 1) & -align) - 4;
da44f4e5 13897 /* FDE length. */
2e0ce1c8 13898 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
da44f4e5
AM
13899 p += 4;
13900 /* CIE pointer. */
13901 val = p - htab->glink_eh_frame->contents;
13902 bfd_put_32 (htab->elf.dynobj, val, p);
13903 p += 4;
13904 /* Offset to .glink, written later. */
13905 p += 4;
13906 /* .glink size. */
13907 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13908 p += 4;
13909 /* Augmentation. */
13910 p += 1;
13911
3cd7c7d7 13912 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
da44f4e5
AM
13913 *p++ = DW_CFA_register;
13914 *p++ = 65;
9f08fa5c 13915 *p++ = htab->opd_abi ? 12 : 0;
3cd7c7d7 13916 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
da44f4e5
AM
13917 *p++ = DW_CFA_restore_extended;
13918 *p++ = 65;
2e0ce1c8 13919 p += ((24 + align - 1) & -align) - 24;
da44f4e5
AM
13920 }
13921 /* Subsume any padding into the last FDE if user .eh_frame
13922 sections are aligned more than glink_eh_frame. Otherwise any
13923 zero padding will be seen as a terminator. */
2e0ce1c8 13924 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
da44f4e5 13925 size = p - htab->glink_eh_frame->contents;
2e0ce1c8 13926 pad = ((size + align - 1) & -align) - size;
da44f4e5
AM
13927 htab->glink_eh_frame->size = size + pad;
13928 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13929 }
13930
d969d15f 13931 maybe_strip_output (info, htab->brlt);
2efec98b
AM
13932 if (htab->relbrlt != NULL)
13933 maybe_strip_output (info, htab->relbrlt);
d969d15f
AM
13934 if (htab->glink_eh_frame != NULL)
13935 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 13936
b34976b6 13937 return TRUE;
721956f4
AM
13938}
13939
13940/* Called after we have determined section placement. If sections
805fc799 13941 move, we'll be called again. Provide a value for TOCstart. */
721956f4 13942
805fc799 13943bfd_vma
1c865ab2 13944ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 13945{
805fc799 13946 asection *s;
a27e685f 13947 bfd_vma TOCstart, adjust;
721956f4 13948
43417696
AM
13949 if (info != NULL)
13950 {
13951 struct elf_link_hash_entry *h;
13952 struct elf_link_hash_table *htab = elf_hash_table (info);
13953
13954 if (is_elf_hash_table (htab)
13955 && htab->hgot != NULL)
13956 h = htab->hgot;
13957 else
13958 {
13959 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13960 if (is_elf_hash_table (htab))
13961 htab->hgot = h;
13962 }
13963 if (h != NULL
13964 && h->root.type == bfd_link_hash_defined
13965 && !h->root.linker_def
13966 && (!is_elf_hash_table (htab)
13967 || h->def_regular))
13968 {
ed7007c1 13969 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
43417696
AM
13970 _bfd_set_gp_value (obfd, TOCstart);
13971 return TOCstart;
13972 }
13973 }
13974
805fc799
AM
13975 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13976 order. The TOC starts where the first of these sections starts. */
13977 s = bfd_get_section_by_name (obfd, ".got");
e054468f 13978 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13979 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 13980 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13981 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 13982 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 13983 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 13984 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
13985 {
13986 /* This may happen for
13987 o references to TOC base (SYM@toc / TOC[tc0]) without a
13988 .toc directive
13989 o bad linker script
13990 o --gc-sections and empty TOC sections
13991
13992 FIXME: Warn user? */
13993
13994 /* Look for a likely section. We probably won't even be
13995 using TOCstart. */
13996 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
13997 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13998 | SEC_EXCLUDE))
805fc799
AM
13999 == (SEC_ALLOC | SEC_SMALL_DATA))
14000 break;
721956f4 14001 if (s == NULL)
805fc799 14002 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14003 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
14004 == (SEC_ALLOC | SEC_SMALL_DATA))
14005 break;
721956f4 14006 if (s == NULL)
805fc799 14007 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
14008 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14009 == SEC_ALLOC)
805fc799 14010 break;
721956f4 14011 if (s == NULL)
805fc799 14012 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 14013 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
14014 break;
14015 }
721956f4 14016
805fc799
AM
14017 TOCstart = 0;
14018 if (s != NULL)
14019 TOCstart = s->output_section->vma + s->output_offset;
721956f4 14020
a27e685f
AM
14021 /* Force alignment. */
14022 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14023 TOCstart -= adjust;
1c865ab2
AM
14024 _bfd_set_gp_value (obfd, TOCstart);
14025
810d4e75 14026 if (info != NULL && s != NULL)
1c865ab2
AM
14027 {
14028 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14029
810d4e75
AM
14030 if (htab != NULL)
14031 {
14032 if (htab->elf.hgot != NULL)
14033 {
a27e685f 14034 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
810d4e75
AM
14035 htab->elf.hgot->root.u.def.section = s;
14036 }
14037 }
14038 else
1c865ab2 14039 {
810d4e75
AM
14040 struct bfd_link_hash_entry *bh = NULL;
14041 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
a27e685f
AM
14042 s, TOC_BASE_OFF - adjust,
14043 NULL, FALSE, FALSE, &bh);
1c865ab2
AM
14044 }
14045 }
805fc799 14046 return TOCstart;
721956f4
AM
14047}
14048
a345bc8d 14049/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
49c09209 14050 write out any global entry stubs, and PLT relocations. */
a345bc8d
AM
14051
14052static bfd_boolean
49c09209 14053build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
a345bc8d
AM
14054{
14055 struct bfd_link_info *info;
14056 struct ppc_link_hash_table *htab;
49c09209 14057 struct plt_entry *ent;
a345bc8d
AM
14058 asection *s;
14059
14060 if (h->root.type == bfd_link_hash_indirect)
14061 return TRUE;
14062
49c09209
AM
14063 info = inf;
14064 htab = ppc_hash_table (info);
14065 if (htab == NULL)
14066 return FALSE;
14067
14068 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14069 if (ent->plt.offset != (bfd_vma) -1)
14070 {
14071 /* This symbol has an entry in the procedure linkage
14072 table. Set it up. */
14073 Elf_Internal_Rela rela;
2d7ad24e 14074 asection *plt, *relplt;
49c09209
AM
14075 bfd_byte *loc;
14076
30845f11 14077 if (use_local_plt (info, h))
49c09209
AM
14078 {
14079 if (!(h->def_regular
14080 && (h->root.type == bfd_link_hash_defined
14081 || h->root.type == bfd_link_hash_defweak)))
14082 continue;
2d7ad24e
AM
14083 if (h->type == STT_GNU_IFUNC)
14084 {
14085 plt = htab->elf.iplt;
14086 relplt = htab->elf.irelplt;
cebd6b8a 14087 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14088 if (htab->opd_abi)
14089 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14090 else
14091 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14092 }
49c09209 14093 else
2d7ad24e
AM
14094 {
14095 plt = htab->pltlocal;
14096 if (bfd_link_pic (info))
14097 {
14098 relplt = htab->relpltlocal;
14099 if (htab->opd_abi)
14100 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14101 else
14102 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14103 }
14104 else
14105 relplt = NULL;
14106 }
ed7007c1 14107 rela.r_addend = defined_sym_val (h) + ent->addend;
2d7ad24e
AM
14108
14109 if (relplt == NULL)
14110 {
14111 loc = plt->contents + ent->plt.offset;
14112 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14113 if (htab->opd_abi)
14114 {
14115 bfd_vma toc = elf_gp (info->output_bfd);
14116 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14117 bfd_put_64 (info->output_bfd, toc, loc + 8);
14118 }
14119 }
14120 else
14121 {
14122 rela.r_offset = (plt->output_section->vma
14123 + plt->output_offset
14124 + ent->plt.offset);
14125 loc = relplt->contents + (relplt->reloc_count++
14126 * sizeof (Elf64_External_Rela));
14127 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14128 }
49c09209
AM
14129 }
14130 else
14131 {
14132 rela.r_offset = (htab->elf.splt->output_section->vma
14133 + htab->elf.splt->output_offset
14134 + ent->plt.offset);
14135 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14136 rela.r_addend = ent->addend;
14137 loc = (htab->elf.srelplt->contents
14138 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14139 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14140 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
cebd6b8a 14141 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e 14142 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
49c09209 14143 }
49c09209
AM
14144 }
14145
a345bc8d
AM
14146 if (!h->pointer_equality_needed)
14147 return TRUE;
14148
14149 if (h->def_regular)
14150 return TRUE;
14151
9e390558 14152 s = htab->global_entry;
49c09209
AM
14153 if (s == NULL || s->size == 0)
14154 return TRUE;
14155
14156 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14157 if (ent->plt.offset != (bfd_vma) -1
14158 && ent->addend == 0)
a345bc8d
AM
14159 {
14160 bfd_byte *p;
14161 asection *plt;
14162 bfd_vma off;
14163
a345bc8d 14164 p = s->contents + h->root.u.def.value;
33e44f2e 14165 plt = htab->elf.splt;
30845f11 14166 if (use_local_plt (info, h))
2d7ad24e
AM
14167 {
14168 if (h->type == STT_GNU_IFUNC)
14169 plt = htab->elf.iplt;
14170 else
14171 plt = htab->pltlocal;
14172 }
49c09209 14173 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
a345bc8d
AM
14174 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14175
14176 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14177 {
14178 info->callbacks->einfo
c1c8c1ef 14179 (_("%P: linkage table error against `%pT'\n"),
a345bc8d
AM
14180 h->root.root.string);
14181 bfd_set_error (bfd_error_bad_value);
14182 htab->stub_error = TRUE;
14183 }
14184
7341d5e2
AM
14185 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14186 if (htab->params->emit_stub_syms)
14187 {
14188 size_t len = strlen (h->root.root.string);
14189 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14190
14191 if (name == NULL)
14192 return FALSE;
14193
14194 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14195 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
14196 if (h == NULL)
14197 return FALSE;
14198 if (h->root.type == bfd_link_hash_new)
14199 {
14200 h->root.type = bfd_link_hash_defined;
14201 h->root.u.def.section = s;
14202 h->root.u.def.value = p - s->contents;
14203 h->ref_regular = 1;
14204 h->def_regular = 1;
14205 h->ref_regular_nonweak = 1;
14206 h->forced_local = 1;
14207 h->non_elf = 0;
2ec55de3 14208 h->root.linker_def = 1;
7341d5e2
AM
14209 }
14210 }
14211
a345bc8d
AM
14212 if (PPC_HA (off) != 0)
14213 {
14214 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14215 p += 4;
14216 }
14217 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14218 p += 4;
14219 bfd_put_32 (s->owner, MTCTR_R12, p);
14220 p += 4;
407aa07c 14221 bfd_put_32 (s->owner, BCTR, p);
a345bc8d
AM
14222 break;
14223 }
14224 return TRUE;
14225}
14226
49c09209
AM
14227/* Write PLT relocs for locals. */
14228
14229static bfd_boolean
14230write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14231{
14232 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14233 bfd *ibfd;
14234
14235 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14236 {
14237 struct got_entry **lgot_ents, **end_lgot_ents;
14238 struct plt_entry **local_plt, **lplt, **end_local_plt;
14239 Elf_Internal_Shdr *symtab_hdr;
14240 bfd_size_type locsymcount;
14241 Elf_Internal_Sym *local_syms = NULL;
14242 struct plt_entry *ent;
14243
14244 if (!is_ppc64_elf (ibfd))
14245 continue;
14246
14247 lgot_ents = elf_local_got_ents (ibfd);
14248 if (!lgot_ents)
14249 continue;
14250
14251 symtab_hdr = &elf_symtab_hdr (ibfd);
14252 locsymcount = symtab_hdr->sh_info;
14253 end_lgot_ents = lgot_ents + locsymcount;
14254 local_plt = (struct plt_entry **) end_lgot_ents;
14255 end_local_plt = local_plt + locsymcount;
14256 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14257 for (ent = *lplt; ent != NULL; ent = ent->next)
14258 if (ent->plt.offset != (bfd_vma) -1)
14259 {
14260 Elf_Internal_Sym *sym;
14261 asection *sym_sec;
14262 asection *plt, *relplt;
14263 bfd_byte *loc;
14264 bfd_vma val;
49c09209
AM
14265
14266 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14267 lplt - local_plt, ibfd))
14268 {
c9594989 14269 if (symtab_hdr->contents != (unsigned char *) local_syms)
49c09209
AM
14270 free (local_syms);
14271 return FALSE;
14272 }
14273
14274 val = sym->st_value + ent->addend;
49c09209
AM
14275 if (sym_sec != NULL && sym_sec->output_section != NULL)
14276 val += sym_sec->output_offset + sym_sec->output_section->vma;
14277
2d7ad24e
AM
14278 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14279 {
cebd6b8a 14280 htab->elf.ifunc_resolvers = TRUE;
2d7ad24e
AM
14281 plt = htab->elf.iplt;
14282 relplt = htab->elf.irelplt;
14283 }
14284 else
14285 {
14286 plt = htab->pltlocal;
14287 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14288 }
49c09209 14289
2d7ad24e
AM
14290 if (relplt == NULL)
14291 {
14292 loc = plt->contents + ent->plt.offset;
14293 bfd_put_64 (info->output_bfd, val, loc);
14294 if (htab->opd_abi)
14295 {
14296 bfd_vma toc = elf_gp (ibfd);
14297 bfd_put_64 (info->output_bfd, toc, loc + 8);
14298 }
14299 }
49c09209 14300 else
2d7ad24e
AM
14301 {
14302 Elf_Internal_Rela rela;
14303 rela.r_offset = (ent->plt.offset
14304 + plt->output_offset
14305 + plt->output_section->vma);
14306 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14307 {
14308 if (htab->opd_abi)
14309 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14310 else
14311 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14312 }
14313 else
14314 {
14315 if (htab->opd_abi)
14316 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14317 else
14318 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14319 }
14320 rela.r_addend = val;
14321 loc = relplt->contents + (relplt->reloc_count++
14322 * sizeof (Elf64_External_Rela));
14323 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14324 }
49c09209
AM
14325 }
14326
14327 if (local_syms != NULL
14328 && symtab_hdr->contents != (unsigned char *) local_syms)
14329 {
14330 if (!info->keep_memory)
14331 free (local_syms);
14332 else
14333 symtab_hdr->contents = (unsigned char *) local_syms;
14334 }
14335 }
14336 return TRUE;
14337}
14338
a804e476
AM
14339/* Emit the static wrapper function preserving registers around a
14340 __tls_get_addr_opt call. */
14341
14342static bfd_boolean
14343emit_tga_desc (struct ppc_link_hash_table *htab)
14344{
14345 asection *stub_sec = htab->tga_group->stub_sec;
14346 unsigned int cfa_updt = 11 * 4;
14347 bfd_byte *p;
14348 bfd_vma to, from, delta;
14349
14350 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14351 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14352 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14353 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14354 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14355 delta = to - from;
14356 if (delta + (1 << 25) >= 1 << 26)
14357 {
14358 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14359 htab->stub_error = TRUE;
14360 return FALSE;
14361 }
14362
14363 p = stub_sec->contents;
14364 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14365 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14366 p += 4;
14367 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14368 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14369}
14370
14371/* Emit eh_frame describing the static wrapper function. */
14372
14373static bfd_byte *
14374emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14375{
14376 unsigned int cfa_updt = 11 * 4;
14377 unsigned int i;
14378
14379 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14380 *p++ = DW_CFA_def_cfa_offset;
14381 if (htab->opd_abi)
14382 {
14383 *p++ = 128;
14384 *p++ = 1;
14385 }
14386 else
14387 *p++ = 96;
14388 *p++ = DW_CFA_offset_extended_sf;
14389 *p++ = 65;
14390 *p++ = (-16 / 8) & 0x7f;
14391 for (i = 4; i < 12; i++)
14392 {
14393 *p++ = DW_CFA_offset + i;
14394 *p++ = (htab->opd_abi ? 13 : 12) - i;
14395 }
14396 *p++ = DW_CFA_advance_loc + 10;
14397 *p++ = DW_CFA_def_cfa_offset;
14398 *p++ = 0;
14399 for (i = 4; i < 12; i++)
14400 *p++ = DW_CFA_restore + i;
14401 *p++ = DW_CFA_advance_loc + 2;
14402 *p++ = DW_CFA_restore_extended;
14403 *p++ = 65;
14404 return p;
14405}
14406
721956f4
AM
14407/* Build all the stubs associated with the current output file.
14408 The stubs are kept in a hash table attached to the main linker
14409 hash table. This function is called via gldelf64ppc_finish. */
14410
b34976b6 14411bfd_boolean
e7d1c40c 14412ppc64_elf_build_stubs (struct bfd_link_info *info,
4ce794b7 14413 char **stats)
5d1634d7
AM
14414{
14415 struct ppc_link_hash_table *htab = ppc_hash_table (info);
a4b6fadd 14416 struct map_stub *group;
721956f4 14417 asection *stub_sec;
5d1634d7 14418 bfd_byte *p;
e717da7e 14419 int stub_sec_count = 0;
5d1634d7 14420
4dfe6ac6
NC
14421 if (htab == NULL)
14422 return FALSE;
14423
eea6121a 14424 /* Allocate memory to hold the linker stubs. */
d4aaa2a0 14425 for (group = htab->group; group != NULL; group = group->next)
df136d64
AM
14426 {
14427 group->eh_size = 0;
14428 group->lr_restore = 0;
14429 if ((stub_sec = group->stub_sec) != NULL
14430 && stub_sec->size != 0)
14431 {
14432 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14433 stub_sec->size);
14434 if (stub_sec->contents == NULL)
14435 return FALSE;
14436 stub_sec->size = 0;
14437 }
14438 }
5d1634d7 14439
23eb7e01 14440 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 14441 {
9f951329 14442 unsigned int indx;
ad8e1ba5 14443 bfd_vma plt0;
9f951329 14444
721956f4 14445 /* Build the .glink plt call stub. */
e7d1c40c 14446 if (htab->params->emit_stub_syms)
97b639ba
AM
14447 {
14448 struct elf_link_hash_entry *h;
468392fb
AM
14449 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14450 TRUE, FALSE, FALSE);
97b639ba
AM
14451 if (h == NULL)
14452 return FALSE;
14453 if (h->root.type == bfd_link_hash_new)
14454 {
14455 h->root.type = bfd_link_hash_defined;
14456 h->root.u.def.section = htab->glink;
ee4bf8d2 14457 h->root.u.def.value = 8;
f5385ebf
AM
14458 h->ref_regular = 1;
14459 h->def_regular = 1;
14460 h->ref_regular_nonweak = 1;
14461 h->forced_local = 1;
14462 h->non_elf = 0;
2ec55de3 14463 h->root.linker_def = 1;
97b639ba
AM
14464 }
14465 }
33e44f2e
AM
14466 plt0 = (htab->elf.splt->output_section->vma
14467 + htab->elf.splt->output_offset
14468 - 16);
176a0d42
AM
14469 if (info->emitrelocations)
14470 {
14471 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14472 if (r == NULL)
14473 return FALSE;
14474 r->r_offset = (htab->glink->output_offset
14475 + htab->glink->output_section->vma);
14476 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14477 r->r_addend = plt0;
14478 }
4ce794b7 14479 p = htab->glink->contents;
176a0d42 14480 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
14481 bfd_put_64 (htab->glink->owner, plt0, p);
14482 p += 8;
b9e5796b
AM
14483 if (htab->opd_abi)
14484 {
14485 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14486 p += 4;
14487 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14488 p += 4;
14489 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14490 p += 4;
14491 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14492 p += 4;
14493 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14494 p += 4;
14495 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14496 p += 4;
14497 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14498 p += 4;
14499 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14500 p += 4;
14501 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14502 p += 4;
14503 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14504 p += 4;
14505 }
14506 else
14507 {
3cd7c7d7
AM
14508 unsigned int insn;
14509
14510 /* 0:
14511 . .quad plt0-1f # plt0 entry relative to 1:
14512 #
14513 # We get here with r12 initially @ a glink branch
14514 # Load the address of _dl_runtime_resolve from plt0 and
14515 # jump to it, with r0 set to the index of the PLT entry
14516 # to be resolved and r11 the link map.
14517 __glink_PLTresolve:
14518 . std %r2,24(%r1) # optional
14519 . mflr %r0
14520 . bcl 20,31,1f
14521 1:
14522 . mflr %r11
14523 . mtlr %r0
14524 . ld %r0,(0b-1b)(%r11)
14525 . sub %r12,%r12,%r11
14526 . add %r11,%r0,%r11
14527 . addi %r0,%r12,1b-2f
14528 . ld %r12,0(%r11)
14529 . srdi %r0,%r0,2
14530 . mtctr %r12
14531 . ld %r11,8(%r11)
14532 . bctr
14533 2:
14534 . b __glink_PLTresolve
14535 . ...
14536 . b __glink_PLTresolve */
14537
14538 if (htab->has_plt_localentry0)
14539 {
14540 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14541 p += 4;
14542 }
b9e5796b
AM
14543 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14544 p += 4;
14545 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14546 p += 4;
14547 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14548 p += 4;
b9e5796b
AM
14549 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14550 p += 4;
3cd7c7d7
AM
14551 if (htab->has_plt_localentry0)
14552 insn = LD_R0_0R11 | (-20 & 0xfffc);
14553 else
14554 insn = LD_R0_0R11 | (-16 & 0xfffc);
14555 bfd_put_32 (htab->glink->owner, insn, p);
14556 p += 4;
b9e5796b
AM
14557 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14558 p += 4;
3cd7c7d7 14559 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
b9e5796b 14560 p += 4;
3cd7c7d7 14561 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
b9e5796b
AM
14562 p += 4;
14563 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14564 p += 4;
14565 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14566 p += 4;
14567 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14568 p += 4;
14569 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14570 p += 4;
14571 }
407aa07c
AM
14572 bfd_put_32 (htab->glink->owner, BCTR, p);
14573 p += 4;
c75bc4f7 14574 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
ad8e1ba5 14575
9f951329
AM
14576 /* Build the .glink lazy link call stubs. */
14577 indx = 0;
9e390558 14578 while (p < htab->glink->contents + htab->glink->size)
9f951329 14579 {
b9e5796b 14580 if (htab->opd_abi)
9f951329 14581 {
b9e5796b
AM
14582 if (indx < 0x8000)
14583 {
14584 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14585 p += 4;
14586 }
14587 else
14588 {
14589 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14590 p += 4;
14591 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14592 p);
14593 p += 4;
14594 }
9f951329 14595 }
4ce794b7 14596 bfd_put_32 (htab->glink->owner,
ee4bf8d2 14597 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 14598 indx++;
9f951329
AM
14599 p += 4;
14600 }
5d1634d7 14601 }
5d1634d7 14602
a804e476
AM
14603 if (htab->tga_group != NULL)
14604 {
14605 htab->tga_group->lr_restore = 23 * 4;
14606 htab->tga_group->stub_sec->size = 24 * 4;
14607 if (!emit_tga_desc (htab))
14608 return FALSE;
14609 if (htab->glink_eh_frame != NULL
14610 && htab->glink_eh_frame->size != 0)
14611 {
14612 size_t align = 4;
14613
14614 p = htab->glink_eh_frame->contents;
14615 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14616 p += 17;
14617 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14618 }
14619 }
14620
49c09209
AM
14621 /* Build .glink global entry stubs, and PLT relocs for globals. */
14622 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14623
14624 if (!write_plt_relocs_for_local_syms (info))
14625 return FALSE;
9e390558 14626
7341d5e2 14627 if (htab->brlt != NULL && htab->brlt->size != 0)
721956f4 14628 {
4ce794b7 14629 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 14630 htab->brlt->size);
4ce794b7 14631 if (htab->brlt->contents == NULL)
b34976b6 14632 return FALSE;
721956f4 14633 }
ee75fd95 14634 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
14635 {
14636 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 14637 htab->relbrlt->size);
63bc6f6c
AM
14638 if (htab->relbrlt->contents == NULL)
14639 return FALSE;
14640 }
5d1634d7 14641
721956f4
AM
14642 /* Build the stubs as directed by the stub hash table. */
14643 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 14644
a4b6fadd
AM
14645 for (group = htab->group; group != NULL; group = group->next)
14646 if (group->needs_save_res)
7dda8d3c 14647 group->stub_sec->size += htab->sfpr->size;
a4b6fadd 14648
aa8a7074
AM
14649 if (htab->relbrlt != NULL)
14650 htab->relbrlt->reloc_count = 0;
14651
e7d1c40c 14652 if (htab->params->plt_stub_align != 0)
d4aaa2a0
AM
14653 for (group = htab->group; group != NULL; group = group->next)
14654 if ((stub_sec = group->stub_sec) != NULL)
691d2e9a
AM
14655 {
14656 int align = abs (htab->params->plt_stub_align);
14657 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14658 }
794e51c0 14659
7dda8d3c
AM
14660 for (group = htab->group; group != NULL; group = group->next)
14661 if (group->needs_save_res)
14662 {
14663 stub_sec = group->stub_sec;
14664 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14665 htab->sfpr->contents, htab->sfpr->size);
14666 if (htab->params->emit_stub_syms)
14667 {
14668 unsigned int i;
14669
14670 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14671 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14672 return FALSE;
14673 }
14674 }
14675
df136d64
AM
14676 if (htab->glink_eh_frame != NULL
14677 && htab->glink_eh_frame->size != 0)
14678 {
14679 bfd_vma val;
14680 size_t align = 4;
14681
14682 p = htab->glink_eh_frame->contents;
14683 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14684
14685 for (group = htab->group; group != NULL; group = group->next)
14686 if (group->eh_size != 0)
14687 {
14688 /* Offset to stub section. */
14689 val = (group->stub_sec->output_section->vma
14690 + group->stub_sec->output_offset);
14691 val -= (htab->glink_eh_frame->output_section->vma
14692 + htab->glink_eh_frame->output_offset
14693 + (p + 8 - htab->glink_eh_frame->contents));
14694 if (val + 0x80000000 > 0xffffffff)
14695 {
14696 _bfd_error_handler
14697 (_("%s offset too large for .eh_frame sdata4 encoding"),
14698 group->stub_sec->name);
14699 return FALSE;
14700 }
14701 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14702 p += (group->eh_size + 17 + 3) & -4;
14703 }
14704 if (htab->glink != NULL && htab->glink->size != 0)
14705 {
14706 /* Offset to .glink. */
14707 val = (htab->glink->output_section->vma
14708 + htab->glink->output_offset
14709 + 8);
14710 val -= (htab->glink_eh_frame->output_section->vma
14711 + htab->glink_eh_frame->output_offset
14712 + (p + 8 - htab->glink_eh_frame->contents));
14713 if (val + 0x80000000 > 0xffffffff)
14714 {
14715 _bfd_error_handler
14716 (_("%s offset too large for .eh_frame sdata4 encoding"),
14717 htab->glink->name);
14718 return FALSE;
14719 }
14720 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14721 p += (24 + align - 1) & -align;
14722 }
14723 }
14724
d4aaa2a0
AM
14725 for (group = htab->group; group != NULL; group = group->next)
14726 if ((stub_sec = group->stub_sec) != NULL)
e717da7e
AM
14727 {
14728 stub_sec_count += 1;
c9301e31
AM
14729 if (stub_sec->rawsize != stub_sec->size
14730 && (htab->stub_iteration <= STUB_SHRINK_ITER
14731 || stub_sec->rawsize < stub_sec->size))
e717da7e
AM
14732 break;
14733 }
5d1634d7 14734
25516cc5 14735 if (group != NULL)
5d1634d7 14736 {
b34976b6 14737 htab->stub_error = TRUE;
cf97bcb0 14738 _bfd_error_handler (_("stubs don't match calculated size"));
5d1634d7 14739 }
721956f4 14740
d2a300cf
AM
14741 if (htab->stub_error)
14742 return FALSE;
14743
14744 if (stats != NULL)
14745 {
988b7300
AM
14746 char *groupmsg;
14747 if (asprintf (&groupmsg,
14748 ngettext ("linker stubs in %u group\n",
14749 "linker stubs in %u groups\n",
14750 stub_sec_count),
14751 stub_sec_count) < 0)
14752 *stats = NULL;
14753 else
14754 {
14755 if (asprintf (stats, _("%s"
14756 " branch %lu\n"
14757 " branch toc adj %lu\n"
14758 " branch notoc %lu\n"
14759 " branch both %lu\n"
14760 " long branch %lu\n"
14761 " long toc adj %lu\n"
14762 " long notoc %lu\n"
14763 " long both %lu\n"
14764 " plt call %lu\n"
14765 " plt call save %lu\n"
14766 " plt call notoc %lu\n"
14767 " plt call both %lu\n"
14768 " global entry %lu"),
14769 groupmsg,
14770 htab->stub_count[ppc_stub_long_branch - 1],
14771 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14772 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14773 htab->stub_count[ppc_stub_long_branch_both - 1],
14774 htab->stub_count[ppc_stub_plt_branch - 1],
14775 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14776 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14777 htab->stub_count[ppc_stub_plt_branch_both - 1],
14778 htab->stub_count[ppc_stub_plt_call - 1],
14779 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14780 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14781 htab->stub_count[ppc_stub_plt_call_both - 1],
14782 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14783 *stats = NULL;
14784 free (groupmsg);
14785 }
d2a300cf
AM
14786 }
14787 return TRUE;
5bd4f169
AM
14788}
14789
60124e18
AM
14790/* What to do when ld finds relocations against symbols defined in
14791 discarded sections. */
14792
14793static unsigned int
14794ppc64_elf_action_discarded (asection *sec)
14795{
14796 if (strcmp (".opd", sec->name) == 0)
14797 return 0;
14798
14799 if (strcmp (".toc", sec->name) == 0)
14800 return 0;
14801
bce50a28
JJ
14802 if (strcmp (".toc1", sec->name) == 0)
14803 return 0;
14804
60124e18
AM
14805 return _bfd_elf_default_action_discarded (sec);
14806}
14807
e59a1001
AM
14808/* These are the dynamic relocations supported by glibc. */
14809
14810static bfd_boolean
14811ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14812{
14813 switch (r_type)
14814 {
14815 case R_PPC64_RELATIVE:
14816 case R_PPC64_NONE:
14817 case R_PPC64_ADDR64:
14818 case R_PPC64_GLOB_DAT:
14819 case R_PPC64_IRELATIVE:
14820 case R_PPC64_JMP_IREL:
14821 case R_PPC64_JMP_SLOT:
14822 case R_PPC64_DTPMOD64:
14823 case R_PPC64_DTPREL64:
14824 case R_PPC64_TPREL64:
14825 case R_PPC64_TPREL16_LO_DS:
14826 case R_PPC64_TPREL16_DS:
14827 case R_PPC64_TPREL16:
14828 case R_PPC64_TPREL16_LO:
14829 case R_PPC64_TPREL16_HI:
14830 case R_PPC64_TPREL16_HIGH:
14831 case R_PPC64_TPREL16_HA:
14832 case R_PPC64_TPREL16_HIGHA:
14833 case R_PPC64_TPREL16_HIGHER:
14834 case R_PPC64_TPREL16_HIGHEST:
14835 case R_PPC64_TPREL16_HIGHERA:
14836 case R_PPC64_TPREL16_HIGHESTA:
14837 case R_PPC64_ADDR16_LO_DS:
14838 case R_PPC64_ADDR16_LO:
14839 case R_PPC64_ADDR16_HI:
14840 case R_PPC64_ADDR16_HIGH:
14841 case R_PPC64_ADDR16_HA:
14842 case R_PPC64_ADDR16_HIGHA:
14843 case R_PPC64_REL30:
14844 case R_PPC64_COPY:
14845 case R_PPC64_UADDR64:
14846 case R_PPC64_UADDR32:
14847 case R_PPC64_ADDR32:
14848 case R_PPC64_ADDR24:
14849 case R_PPC64_ADDR16:
14850 case R_PPC64_UADDR16:
14851 case R_PPC64_ADDR16_DS:
14852 case R_PPC64_ADDR16_HIGHER:
14853 case R_PPC64_ADDR16_HIGHEST:
14854 case R_PPC64_ADDR16_HIGHERA:
14855 case R_PPC64_ADDR16_HIGHESTA:
14856 case R_PPC64_ADDR14:
14857 case R_PPC64_ADDR14_BRTAKEN:
14858 case R_PPC64_ADDR14_BRNTAKEN:
14859 case R_PPC64_REL32:
14860 case R_PPC64_REL64:
14861 return TRUE;
14862
14863 default:
14864 return FALSE;
14865 }
14866}
14867
5bd4f169
AM
14868/* The RELOCATE_SECTION function is called by the ELF backend linker
14869 to handle the relocations for a section.
14870
14871 The relocs are always passed as Rela structures; if the section
14872 actually uses Rel structures, the r_addend field will always be
14873 zero.
14874
14875 This function is responsible for adjust the section contents as
14876 necessary, and (if using Rela relocs and generating a
1049f94e 14877 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
14878 necessary.
14879
14880 This function does not have to worry about setting the reloc
14881 address or the reloc symbol index.
14882
14883 LOCAL_SYMS is a pointer to the swapped in local symbols.
14884
14885 LOCAL_SECTIONS is an array giving the section in the input file
14886 corresponding to the st_shndx field of each local symbol.
14887
14888 The global hash table entry for the global symbols can be found
14889 via elf_sym_hashes (input_bfd).
14890
1049f94e 14891 When generating relocatable output, this function must handle
5bd4f169
AM
14892 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14893 going to be the section symbol corresponding to the output
14894 section, which means that the addend must be adjusted
14895 accordingly. */
14896
b34976b6 14897static bfd_boolean
4ce794b7
AM
14898ppc64_elf_relocate_section (bfd *output_bfd,
14899 struct bfd_link_info *info,
14900 bfd *input_bfd,
14901 asection *input_section,
14902 bfd_byte *contents,
14903 Elf_Internal_Rela *relocs,
14904 Elf_Internal_Sym *local_syms,
14905 asection **local_sections)
5bd4f169 14906{
65f38f15 14907 struct ppc_link_hash_table *htab;
5bd4f169
AM
14908 Elf_Internal_Shdr *symtab_hdr;
14909 struct elf_link_hash_entry **sym_hashes;
5bd4f169 14910 Elf_Internal_Rela *rel;
c316a17c 14911 Elf_Internal_Rela *wrel;
5bd4f169 14912 Elf_Internal_Rela *relend;
411e1bfb
AM
14913 Elf_Internal_Rela outrel;
14914 bfd_byte *loc;
411e1bfb 14915 struct got_entry **local_got_ents;
5bd4f169 14916 bfd_vma TOCstart;
b34976b6
AM
14917 bfd_boolean ret = TRUE;
14918 bfd_boolean is_opd;
794e51c0
AM
14919 /* Assume 'at' branch hints. */
14920 bfd_boolean is_isa_v2 = TRUE;
e59a1001 14921 bfd_boolean warned_dynamic = FALSE;
95f0d0d2 14922 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
5bd4f169 14923
65f38f15 14924 /* Initialize howto table if needed. */
5bd4f169 14925 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
14926 ppc_howto_init ();
14927
65f38f15 14928 htab = ppc_hash_table (info);
4dfe6ac6
NC
14929 if (htab == NULL)
14930 return FALSE;
ee75fd95
AM
14931
14932 /* Don't relocate stub sections. */
e7d1c40c 14933 if (input_section->owner == htab->params->stub_bfd)
ee75fd95
AM
14934 return TRUE;
14935
7af5d5c4
AM
14936 if (!is_ppc64_elf (input_bfd))
14937 {
14938 bfd_set_error (bfd_error_wrong_format);
14939 return FALSE;
14940 }
0ffa91dd 14941
411e1bfb 14942 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 14943 TOCstart = elf_gp (output_bfd);
0ffa91dd 14944 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 14945 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 14946 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 14947
c316a17c 14948 rel = wrel = relocs;
5bd4f169 14949 relend = relocs + input_section->reloc_count;
c316a17c 14950 for (; rel < relend; wrel++, rel++)
5bd4f169 14951 {
04c9666a 14952 enum elf_ppc64_reloc_type r_type;
31c76678 14953 bfd_vma addend;
5bd4f169
AM
14954 bfd_reloc_status_type r;
14955 Elf_Internal_Sym *sym;
14956 asection *sec;
039b3fef
AM
14957 struct elf_link_hash_entry *h_elf;
14958 struct ppc_link_hash_entry *h;
14959 struct ppc_link_hash_entry *fdh;
5bd4f169 14960 const char *sym_name;
0d4792f7 14961 unsigned long r_symndx, toc_symndx;
3a71aa26 14962 bfd_vma toc_addend;
f961d9dd
AM
14963 unsigned char tls_mask, tls_gd, tls_type;
14964 unsigned char sym_type;
5bd4f169 14965 bfd_vma relocation;
23cedd1d 14966 bfd_boolean unresolved_reloc, save_unresolved_reloc;
b34976b6 14967 bfd_boolean warned;
bc30df16 14968 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 14969 unsigned int insn;
e11840f9 14970 unsigned int mask;
721956f4
AM
14971 struct ppc_stub_hash_entry *stub_entry;
14972 bfd_vma max_br_offset;
14973 bfd_vma from;
c316a17c 14974 Elf_Internal_Rela orig_rel;
b80eed39
AM
14975 reloc_howto_type *howto;
14976 struct reloc_howto_struct alt_howto;
4a421c53
AM
14977 uint64_t pinsn;
14978 bfd_vma offset;
5bd4f169 14979
c316a17c
AM
14980 again:
14981 orig_rel = *rel;
14982
4ce794b7 14983 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 14984 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
14985
14986 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14987 symbol of the previous ADDR64 reloc. The symbol gives us the
14988 proper TOC base to use. */
14989 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
c316a17c
AM
14990 && wrel != relocs
14991 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
ee87f2da 14992 && is_opd)
c316a17c 14993 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
ee87f2da 14994
4ce794b7
AM
14995 sym = NULL;
14996 sec = NULL;
039b3fef 14997 h_elf = NULL;
4ce794b7 14998 sym_name = NULL;
b34976b6
AM
14999 unresolved_reloc = FALSE;
15000 warned = FALSE;
65f38f15 15001
0b13192e 15002 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
15003 {
15004 /* It's a local symbol. */
74f0fb50 15005 struct _opd_sec_data *opd;
4025353c 15006
5bd4f169
AM
15007 sym = local_syms + r_symndx;
15008 sec = local_sections[r_symndx];
26c61ae5 15009 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 15010 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 15011 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
15012 opd = get_opd_info (sec);
15013 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 15014 {
51aecdc5
AM
15015 long adjust = opd->adjust[OPD_NDX (sym->st_value
15016 + rel->r_addend)];
4025353c
AM
15017 if (adjust == -1)
15018 relocation = 0;
15019 else
4cc603a5
AM
15020 {
15021 /* If this is a relocation against the opd section sym
15022 and we have edited .opd, adjust the reloc addend so
15023 that ld -r and ld --emit-relocs output is correct.
15024 If it is a reloc against some other .opd symbol,
15025 then the symbol value will be adjusted later. */
15026 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15027 rel->r_addend += adjust;
15028 else
15029 relocation += adjust;
15030 }
1e2f5b6e 15031 }
5bd4f169
AM
15032 }
15033 else
15034 {
62d887d4
L
15035 bfd_boolean ignored;
15036
b2a8e766
AM
15037 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15038 r_symndx, symtab_hdr, sym_hashes,
039b3fef 15039 h_elf, sec, relocation,
62d887d4 15040 unresolved_reloc, warned, ignored);
039b3fef
AM
15041 sym_name = h_elf->root.root.string;
15042 sym_type = h_elf->type;
b69fdb4e
AM
15043 if (sec != NULL
15044 && sec->owner == output_bfd
15045 && strcmp (sec->name, ".opd") == 0)
15046 {
15047 /* This is a symbol defined in a linker script. All
15048 such are defined in output sections, even those
15049 defined by simple assignment from a symbol defined in
15050 an input section. Transfer the symbol to an
15051 appropriate input .opd section, so that a branch to
15052 this symbol will be mapped to the location specified
15053 by the opd entry. */
15054 struct bfd_link_order *lo;
15055 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15056 if (lo->type == bfd_indirect_link_order)
15057 {
15058 asection *isec = lo->u.indirect.section;
15059 if (h_elf->root.u.def.value >= isec->output_offset
15060 && h_elf->root.u.def.value < (isec->output_offset
15061 + isec->size))
15062 {
15063 h_elf->root.u.def.value -= isec->output_offset;
15064 h_elf->root.u.def.section = isec;
15065 sec = isec;
15066 break;
15067 }
15068 }
15069 }
5bd4f169 15070 }
ed7007c1 15071 h = ppc_elf_hash_entry (h_elf);
5bd4f169 15072
dbaa2011 15073 if (sec != NULL && discarded_section (sec))
c316a17c
AM
15074 {
15075 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15076 input_bfd, input_section,
0930cb30 15077 contents, rel->r_offset);
c316a17c
AM
15078 wrel->r_offset = rel->r_offset;
15079 wrel->r_info = 0;
15080 wrel->r_addend = 0;
15081
15082 /* For ld -r, remove relocations in debug sections against
dcd2b8a0 15083 symbols defined in discarded sections. Not done for
c316a17c
AM
15084 non-debug to preserve relocs in .eh_frame which the
15085 eh_frame editing code expects to be present. */
15086 if (bfd_link_relocatable (info)
15087 && (input_section->flags & SEC_DEBUGGING))
15088 wrel--;
15089
15090 continue;
15091 }
ab96bf03 15092
0e1862bb 15093 if (bfd_link_relocatable (info))
c316a17c 15094 goto copy_reloc;
ab96bf03 15095
f40da81b
AM
15096 if (h != NULL && &h->elf == htab->elf.hgot)
15097 {
6f20ed8a 15098 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
f40da81b
AM
15099 sec = bfd_abs_section_ptr;
15100 unresolved_reloc = FALSE;
15101 }
15102
951fd09b
AM
15103 /* TLS optimizations. Replace instruction sequences and relocs
15104 based on information we collected in tls_optimize. We edit
15105 RELOCS so that --emit-relocs will output something sensible
15106 for the final instruction stream. */
15107 tls_mask = 0;
15108 tls_gd = 0;
0d4792f7 15109 toc_symndx = 0;
727fc41e
AM
15110 if (h != NULL)
15111 tls_mask = h->tls_mask;
15112 else if (local_got_ents != NULL)
411e1bfb 15113 {
e054468f
AM
15114 struct plt_entry **local_plt = (struct plt_entry **)
15115 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 15116 unsigned char *lgot_masks = (unsigned char *)
e054468f 15117 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
15118 tls_mask = lgot_masks[r_symndx];
15119 }
37da22e5 15120 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
727fc41e
AM
15121 && (r_type == R_PPC64_TLS
15122 || r_type == R_PPC64_TLSGD
15123 || r_type == R_PPC64_TLSLD))
15124 {
15125 /* Check for toc tls entries. */
f961d9dd 15126 unsigned char *toc_tls;
0d4792f7 15127
727fc41e
AM
15128 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15129 &local_syms, rel, input_bfd))
15130 return FALSE;
0d4792f7 15131
727fc41e
AM
15132 if (toc_tls)
15133 tls_mask = *toc_tls;
0d4792f7
AM
15134 }
15135
15136 /* Check that tls relocs are used with tls syms, and non-tls
15137 relocs are used with non-tls syms. */
cf35638d 15138 if (r_symndx != STN_UNDEF
0d4792f7
AM
15139 && r_type != R_PPC64_NONE
15140 && (h == NULL
039b3fef
AM
15141 || h->elf.root.type == bfd_link_hash_defined
15142 || h->elf.root.type == bfd_link_hash_defweak)
71c4e95a 15143 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
0d4792f7 15144 {
37da22e5 15145 if ((tls_mask & TLS_TLS) != 0
727fc41e
AM
15146 && (r_type == R_PPC64_TLS
15147 || r_type == R_PPC64_TLSGD
15148 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
15149 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15150 ;
15151 else
25f53a85 15152 info->callbacks->einfo
1d483afe 15153 (!IS_PPC64_TLS_RELOC (r_type)
695344c0 15154 /* xgettext:c-format */
c1c8c1ef 15155 ? _("%H: %s used with TLS symbol `%pT'\n")
695344c0 15156 /* xgettext:c-format */
c1c8c1ef 15157 : _("%H: %s used with non-TLS symbol `%pT'\n"),
25f53a85 15158 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
15159 ppc64_elf_howto_table[r_type]->name,
15160 sym_name);
411e1bfb
AM
15161 }
15162
15163 /* Ensure reloc mapping code below stays sane. */
15164 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15165 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15166 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15167 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15168 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15169 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15170 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15171 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15172 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15173 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15174 abort ();
0d4792f7 15175
411e1bfb
AM
15176 switch (r_type)
15177 {
15178 default:
411e1bfb
AM
15179 break;
15180
ba761f19 15181 case R_PPC64_LO_DS_OPT:
95f0d0d2 15182 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
2365f8d7 15183 if ((insn & (0x3fu << 26)) != 58u << 26)
ba761f19
AM
15184 abort ();
15185 insn += (14u << 26) - (58u << 26);
95f0d0d2 15186 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
ba761f19
AM
15187 r_type = R_PPC64_TOC16_LO;
15188 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15189 break;
15190
411e1bfb
AM
15191 case R_PPC64_TOC16:
15192 case R_PPC64_TOC16_LO:
15193 case R_PPC64_TOC16_DS:
15194 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
15195 {
15196 /* Check for toc tls entries. */
f961d9dd 15197 unsigned char *toc_tls;
951fd09b 15198 int retval;
411e1bfb 15199
3a71aa26
AM
15200 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15201 &local_syms, rel, input_bfd);
951fd09b 15202 if (retval == 0)
411e1bfb
AM
15203 return FALSE;
15204
15205 if (toc_tls)
15206 {
951fd09b 15207 tls_mask = *toc_tls;
411e1bfb
AM
15208 if (r_type == R_PPC64_TOC16_DS
15209 || r_type == R_PPC64_TOC16_LO_DS)
81407a69 15210 {
37da22e5 15211 if ((tls_mask & TLS_TLS) != 0
81407a69
AM
15212 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15213 goto toctprel;
15214 }
411e1bfb 15215 else
951fd09b
AM
15216 {
15217 /* If we found a GD reloc pair, then we might be
15218 doing a GD->IE transition. */
15219 if (retval == 2)
15220 {
b00a0a86 15221 tls_gd = TLS_GDIE;
37da22e5
AM
15222 if ((tls_mask & TLS_TLS) != 0
15223 && (tls_mask & TLS_GD) == 0)
102890f0 15224 goto tls_ldgd_opt;
951fd09b
AM
15225 }
15226 else if (retval == 3)
15227 {
37da22e5
AM
15228 if ((tls_mask & TLS_TLS) != 0
15229 && (tls_mask & TLS_LD) == 0)
102890f0 15230 goto tls_ldgd_opt;
951fd09b
AM
15231 }
15232 }
411e1bfb
AM
15233 }
15234 }
15235 break;
15236
9d6ded02
AM
15237 case R_PPC64_GOT_TPREL16_HI:
15238 case R_PPC64_GOT_TPREL16_HA:
37da22e5 15239 if ((tls_mask & TLS_TLS) != 0
9d6ded02
AM
15240 && (tls_mask & TLS_TPREL) == 0)
15241 {
15242 rel->r_offset -= d_offset;
95f0d0d2 15243 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
9d6ded02
AM
15244 r_type = R_PPC64_NONE;
15245 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15246 }
15247 break;
15248
411e1bfb
AM
15249 case R_PPC64_GOT_TPREL16_DS:
15250 case R_PPC64_GOT_TPREL16_LO_DS:
37da22e5 15251 if ((tls_mask & TLS_TLS) != 0
951fd09b 15252 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15253 {
81407a69 15254 toctprel:
95f0d0d2 15255 insn = bfd_get_32 (input_bfd,
c316a17c 15256 contents + rel->r_offset - d_offset);
411e1bfb
AM
15257 insn &= 31 << 21;
15258 insn |= 0x3c0d0000; /* addis 0,13,0 */
95f0d0d2 15259 bfd_put_32 (input_bfd, insn,
c316a17c 15260 contents + rel->r_offset - d_offset);
411e1bfb 15261 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
15262 if (toc_symndx != 0)
15263 {
15264 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 15265 rel->r_addend = toc_addend;
0d4792f7
AM
15266 /* We changed the symbol. Start over in order to
15267 get h, sym, sec etc. right. */
c316a17c 15268 goto again;
0d4792f7
AM
15269 }
15270 else
15271 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15272 }
15273 break;
15274
87c69f97 15275 case R_PPC64_GOT_TPREL_PCREL34:
c213164a
AM
15276 if ((tls_mask & TLS_TLS) != 0
15277 && (tls_mask & TLS_TPREL) == 0)
15278 {
15279 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15280 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15281 pinsn <<= 32;
15282 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15283 pinsn += ((2ULL << 56) + (-1ULL << 52)
15284 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15285 bfd_put_32 (input_bfd, pinsn >> 32,
15286 contents + rel->r_offset);
15287 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15288 contents + rel->r_offset + 4);
15289 r_type = R_PPC64_TPREL34;
15290 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15291 }
15292 break;
15293
411e1bfb 15294 case R_PPC64_TLS:
37da22e5 15295 if ((tls_mask & TLS_TLS) != 0
951fd09b 15296 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 15297 {
c213164a 15298 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2d0f3896
AM
15299 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15300 if (insn == 0)
c213164a
AM
15301 break;
15302 if ((rel->r_offset & 3) == 0)
0d4792f7 15303 {
c213164a
AM
15304 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15305 /* Was PPC64_TLS which sits on insn boundary, now
15306 PPC64_TPREL16_LO which is at low-order half-word. */
15307 rel->r_offset += d_offset;
15308 r_type = R_PPC64_TPREL16_LO;
15309 if (toc_symndx != 0)
15310 {
15311 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15312 rel->r_addend = toc_addend;
15313 /* We changed the symbol. Start over in order to
15314 get h, sym, sec etc. right. */
15315 goto again;
15316 }
15317 else
15318 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15319 }
15320 else if ((rel->r_offset & 3) == 1)
15321 {
15322 /* For pcrel IE to LE we already have the full
15323 offset and thus don't need an addi here. A nop
15324 or mr will do. */
2365f8d7 15325 if ((insn & (0x3fu << 26)) == 14 << 26)
c213164a
AM
15326 {
15327 /* Extract regs from addi rt,ra,si. */
15328 unsigned int rt = (insn >> 21) & 0x1f;
15329 unsigned int ra = (insn >> 16) & 0x1f;
15330 if (rt == ra)
15331 insn = NOP;
15332 else
15333 {
15334 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15335 insn = (rt << 16) | (ra << 21) | (ra << 11);
15336 insn |= (31u << 26) | (444u << 1);
15337 }
15338 }
15339 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
0d4792f7 15340 }
411e1bfb
AM
15341 }
15342 break;
15343
411e1bfb
AM
15344 case R_PPC64_GOT_TLSGD16_HI:
15345 case R_PPC64_GOT_TLSGD16_HA:
b00a0a86 15346 tls_gd = TLS_GDIE;
37da22e5 15347 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
951fd09b
AM
15348 goto tls_gdld_hi;
15349 break;
15350
411e1bfb
AM
15351 case R_PPC64_GOT_TLSLD16_HI:
15352 case R_PPC64_GOT_TLSLD16_HA:
37da22e5 15353 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 15354 {
951fd09b
AM
15355 tls_gdld_hi:
15356 if ((tls_mask & tls_gd) != 0)
15357 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15358 + R_PPC64_GOT_TPREL16_DS);
15359 else
411e1bfb 15360 {
4fe5ca5b 15361 rel->r_offset -= d_offset;
95f0d0d2 15362 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
951fd09b 15363 r_type = R_PPC64_NONE;
411e1bfb 15364 }
951fd09b 15365 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
15366 }
15367 break;
15368
951fd09b
AM
15369 case R_PPC64_GOT_TLSGD16:
15370 case R_PPC64_GOT_TLSGD16_LO:
b00a0a86 15371 tls_gd = TLS_GDIE;
37da22e5 15372 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
102890f0 15373 goto tls_ldgd_opt;
951fd09b 15374 break;
411e1bfb 15375
951fd09b
AM
15376 case R_PPC64_GOT_TLSLD16:
15377 case R_PPC64_GOT_TLSLD16_LO:
37da22e5 15378 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
951fd09b 15379 {
b9f04fe0 15380 unsigned int insn1, insn2;
102890f0
AM
15381
15382 tls_ldgd_opt:
727fc41e
AM
15383 offset = (bfd_vma) -1;
15384 /* If not using the newer R_PPC64_TLSGD/LD to mark
15385 __tls_get_addr calls, we must trust that the call
15386 stays with its arg setup insns, ie. that the next
15387 reloc is the __tls_get_addr call associated with
15388 the current reloc. Edit both insns. */
9737e8af 15389 if (input_section->nomark_tls_get_addr
727fc41e
AM
15390 && rel + 1 < relend
15391 && branch_reloc_hash_match (input_bfd, rel + 1,
9e7028aa
AM
15392 htab->tls_get_addr_fd,
15393 htab->tga_desc_fd,
727fc41e 15394 htab->tls_get_addr,
9e7028aa 15395 htab->tga_desc))
727fc41e 15396 offset = rel[1].r_offset;
b86ac8e3
AM
15397 /* We read the low GOT_TLS (or TOC16) insn because we
15398 need to keep the destination reg. It may be
15399 something other than the usual r3, and moved to r3
15400 before the call by intervening code. */
95f0d0d2 15401 insn1 = bfd_get_32 (input_bfd,
b86ac8e3 15402 contents + rel->r_offset - d_offset);
102890f0 15403 if ((tls_mask & tls_gd) != 0)
411e1bfb 15404 {
102890f0 15405 /* IE */
b86ac8e3 15406 insn1 &= (0x1f << 21) | (0x1f << 16);
2365f8d7 15407 insn1 |= 58u << 26; /* ld */
102890f0 15408 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 15409 if (offset != (bfd_vma) -1)
f58d5a2d 15410 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
46e9995a
AM
15411 if (r_type == R_PPC64_TOC16
15412 || r_type == R_PPC64_TOC16_LO)
102890f0 15413 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
46e9995a
AM
15414 else
15415 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15416 + R_PPC64_GOT_TPREL16_DS);
102890f0
AM
15417 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15418 }
15419 else
15420 {
15421 /* LE */
b86ac8e3
AM
15422 insn1 &= 0x1f << 21;
15423 insn1 |= 0x3c0d0000; /* addis r,13,0 */
102890f0
AM
15424 insn2 = 0x38630000; /* addi 3,3,0 */
15425 if (tls_gd == 0)
951fd09b 15426 {
102890f0 15427 /* Was an LD reloc. */
71c4e95a 15428 r_symndx = STN_UNDEF;
102890f0 15429 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 15430 }
102890f0 15431 else if (toc_symndx != 0)
3a71aa26
AM
15432 {
15433 r_symndx = toc_symndx;
15434 rel->r_addend = toc_addend;
15435 }
102890f0
AM
15436 r_type = R_PPC64_TPREL16_HA;
15437 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15438 if (offset != (bfd_vma) -1)
15439 {
15440 rel[1].r_info = ELF64_R_INFO (r_symndx,
15441 R_PPC64_TPREL16_LO);
15442 rel[1].r_offset = offset + d_offset;
15443 rel[1].r_addend = rel->r_addend;
15444 }
102890f0 15445 }
95f0d0d2 15446 bfd_put_32 (input_bfd, insn1,
3a71aa26 15447 contents + rel->r_offset - d_offset);
727fc41e 15448 if (offset != (bfd_vma) -1)
c96e0573
AM
15449 {
15450 bfd_put_32 (input_bfd, insn2, contents + offset);
15451 if (offset + 8 <= input_section->size)
15452 {
15453 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15454 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15455 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15456 }
15457 }
727fc41e
AM
15458 if ((tls_mask & tls_gd) == 0
15459 && (tls_gd == 0 || toc_symndx != 0))
15460 {
15461 /* We changed the symbol. Start over in order
15462 to get h, sym, sec etc. right. */
c316a17c 15463 goto again;
727fc41e
AM
15464 }
15465 }
15466 break;
15467
87c69f97 15468 case R_PPC64_GOT_TLSGD_PCREL34:
c213164a
AM
15469 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15470 {
15471 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15472 pinsn <<= 32;
15473 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15474 if ((tls_mask & TLS_GDIE) != 0)
15475 {
15476 /* IE, pla -> pld */
15477 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
87c69f97 15478 r_type = R_PPC64_GOT_TPREL_PCREL34;
c213164a
AM
15479 }
15480 else
15481 {
15482 /* LE, pla pcrel -> paddi r13 */
15483 pinsn += (-1ULL << 52) + (13ULL << 16);
15484 r_type = R_PPC64_TPREL34;
15485 }
15486 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15487 bfd_put_32 (input_bfd, pinsn >> 32,
15488 contents + rel->r_offset);
15489 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15490 contents + rel->r_offset + 4);
15491 }
15492 break;
15493
87c69f97 15494 case R_PPC64_GOT_TLSLD_PCREL34:
c213164a
AM
15495 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15496 {
15497 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15498 pinsn <<= 32;
15499 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15500 pinsn += (-1ULL << 52) + (13ULL << 16);
15501 bfd_put_32 (input_bfd, pinsn >> 32,
15502 contents + rel->r_offset);
15503 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15504 contents + rel->r_offset + 4);
15505 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15506 r_symndx = STN_UNDEF;
15507 r_type = R_PPC64_TPREL34;
15508 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15509 goto again;
15510 }
15511 break;
15512
727fc41e 15513 case R_PPC64_TLSGD:
37da22e5 15514 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
675e2809 15515 && rel + 1 < relend)
727fc41e 15516 {
b9f04fe0 15517 unsigned int insn2;
5663e321 15518 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15519
4a421c53 15520 offset = rel->r_offset;
5663e321 15521 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15522 {
15523 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15524 if (r_type1 == R_PPC64_PLT_PCREL34
15525 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15526 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15527 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15528 break;
15529 }
15530
15531 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15532 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15533
b00a0a86 15534 if ((tls_mask & TLS_GDIE) != 0)
727fc41e
AM
15535 {
15536 /* IE */
15537 r_type = R_PPC64_NONE;
15538 insn2 = 0x7c636a14; /* add 3,3,13 */
15539 }
15540 else
15541 {
15542 /* LE */
15543 if (toc_symndx != 0)
15544 {
15545 r_symndx = toc_symndx;
15546 rel->r_addend = toc_addend;
15547 }
c213164a
AM
15548 if (r_type1 == R_PPC64_REL24_NOTOC
15549 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15550 {
15551 r_type = R_PPC64_NONE;
15552 insn2 = NOP;
15553 }
15554 else
15555 {
15556 rel->r_offset = offset + d_offset;
15557 r_type = R_PPC64_TPREL16_LO;
15558 insn2 = 0x38630000; /* addi 3,3,0 */
15559 }
727fc41e
AM
15560 }
15561 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15562 /* Zap the reloc on the _tls_get_addr call too. */
15563 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15564 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15565 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15566 if ((tls_mask & TLS_GDIE) == 0
15567 && toc_symndx != 0
15568 && r_type != R_PPC64_NONE)
c316a17c 15569 goto again;
411e1bfb 15570 }
411e1bfb
AM
15571 break;
15572
727fc41e 15573 case R_PPC64_TLSLD:
37da22e5 15574 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
675e2809 15575 && rel + 1 < relend)
727fc41e 15576 {
b9f04fe0 15577 unsigned int insn2;
5663e321 15578 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
727fc41e 15579
4a421c53 15580 offset = rel->r_offset;
5663e321 15581 if (is_plt_seq_reloc (r_type1))
23cedd1d
AM
15582 {
15583 bfd_put_32 (output_bfd, NOP, contents + offset);
5663e321
AM
15584 if (r_type1 == R_PPC64_PLT_PCREL34
15585 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15586 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
23cedd1d
AM
15587 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15588 break;
15589 }
15590
15591 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15592 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15593
c213164a
AM
15594 if (r_type1 == R_PPC64_REL24_NOTOC
15595 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15596 {
15597 r_type = R_PPC64_NONE;
15598 insn2 = NOP;
15599 }
15600 else
15601 {
15602 rel->r_offset = offset + d_offset;
15603 r_symndx = STN_UNDEF;
15604 r_type = R_PPC64_TPREL16_LO;
15605 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15606 insn2 = 0x38630000; /* addi 3,3,0 */
15607 }
727fc41e 15608 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
15609 /* Zap the reloc on the _tls_get_addr call too. */
15610 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 15611 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
95f0d0d2 15612 bfd_put_32 (input_bfd, insn2, contents + offset);
c213164a
AM
15613 if (r_type != R_PPC64_NONE)
15614 goto again;
727fc41e
AM
15615 }
15616 break;
15617
411e1bfb 15618 case R_PPC64_DTPMOD64:
951fd09b
AM
15619 if (rel + 1 < relend
15620 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15621 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 15622 {
951fd09b
AM
15623 if ((tls_mask & TLS_GD) == 0)
15624 {
15625 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
b00a0a86 15626 if ((tls_mask & TLS_GDIE) != 0)
951fd09b
AM
15627 r_type = R_PPC64_TPREL64;
15628 else
15629 {
4ce794b7 15630 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
15631 r_type = R_PPC64_NONE;
15632 }
15633 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15634 }
15635 }
15636 else
15637 {
15638 if ((tls_mask & TLS_LD) == 0)
411e1bfb 15639 {
4ce794b7 15640 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 15641 r_type = R_PPC64_NONE;
951fd09b 15642 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 15643 }
411e1bfb
AM
15644 }
15645 break;
15646
15647 case R_PPC64_TPREL64:
951fd09b 15648 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
15649 {
15650 r_type = R_PPC64_NONE;
15651 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15652 }
15653 break;
52a82034 15654
006589cf
AM
15655 case R_PPC64_ENTRY:
15656 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15657 if (!bfd_link_pic (info)
15658 && !info->traditional_format
15659 && relocation + 0x80008000 <= 0xffffffff)
15660 {
15661 unsigned int insn1, insn2;
15662
15663 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15664 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15665 if ((insn1 & ~0xfffc) == LD_R2_0R12
15666 && insn2 == ADD_R2_R2_R12)
15667 {
95f0d0d2 15668 bfd_put_32 (input_bfd,
006589cf
AM
15669 LIS_R2 + PPC_HA (relocation),
15670 contents + rel->r_offset);
95f0d0d2 15671 bfd_put_32 (input_bfd,
006589cf
AM
15672 ADDI_R2_R2 + PPC_LO (relocation),
15673 contents + rel->r_offset + 4);
15674 }
15675 }
15676 else
15677 {
15678 relocation -= (rel->r_offset
15679 + input_section->output_offset
15680 + input_section->output_section->vma);
15681 if (relocation + 0x80008000 <= 0xffffffff)
15682 {
15683 unsigned int insn1, insn2;
15684
15685 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15686 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15687 if ((insn1 & ~0xfffc) == LD_R2_0R12
15688 && insn2 == ADD_R2_R2_R12)
15689 {
95f0d0d2 15690 bfd_put_32 (input_bfd,
006589cf
AM
15691 ADDIS_R2_R12 + PPC_HA (relocation),
15692 contents + rel->r_offset);
95f0d0d2 15693 bfd_put_32 (input_bfd,
006589cf
AM
15694 ADDI_R2_R2 + PPC_LO (relocation),
15695 contents + rel->r_offset + 4);
15696 }
15697 }
15698 }
15699 break;
15700
52a82034
AM
15701 case R_PPC64_REL16_HA:
15702 /* If we are generating a non-PIC executable, edit
15703 . 0: addis 2,12,.TOC.-0b@ha
15704 . addi 2,2,.TOC.-0b@l
15705 used by ELFv2 global entry points to set up r2, to
15706 . lis 2,.TOC.@ha
15707 . addi 2,2,.TOC.@l
15708 if .TOC. is in range. */
0e1862bb 15709 if (!bfd_link_pic (info)
810d4e75 15710 && !info->traditional_format
006589cf 15711 && !htab->opd_abi
4f038ee5 15712 && rel->r_addend == d_offset
52a82034
AM
15713 && h != NULL && &h->elf == htab->elf.hgot
15714 && rel + 1 < relend
15715 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15716 && rel[1].r_offset == rel->r_offset + 4
15717 && rel[1].r_addend == rel->r_addend + 4
15718 && relocation + 0x80008000 <= 0xffffffff)
15719 {
15720 unsigned int insn1, insn2;
4a421c53 15721 offset = rel->r_offset - d_offset;
95f0d0d2
AM
15722 insn1 = bfd_get_32 (input_bfd, contents + offset);
15723 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
006589cf
AM
15724 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15725 && (insn2 & 0xffff0000) == ADDI_R2_R2)
52a82034
AM
15726 {
15727 r_type = R_PPC64_ADDR16_HA;
15728 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15729 rel->r_addend -= d_offset;
15730 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15731 rel[1].r_addend -= d_offset + 4;
95f0d0d2 15732 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
52a82034
AM
15733 }
15734 }
15735 break;
411e1bfb
AM
15736 }
15737
15738 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 15739 insn = 0;
b25116a9
AM
15740 max_br_offset = 1 << 25;
15741 addend = rel->r_addend;
bc30df16 15742 reloc_dest = DEST_NORMAL;
65f38f15 15743 switch (r_type)
5bd4f169
AM
15744 {
15745 default:
65f38f15 15746 break;
5bd4f169 15747
3b421ab3
AM
15748 case R_PPC64_TOCSAVE:
15749 if (relocation + addend == (rel->r_offset
15750 + input_section->output_offset
15751 + input_section->output_section->vma)
15752 && tocsave_find (htab, NO_INSERT,
15753 &local_syms, rel, input_bfd))
15754 {
15755 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15756 if (insn == NOP
15757 || insn == CROR_151515 || insn == CROR_313131)
a078d95a
AM
15758 bfd_put_32 (input_bfd,
15759 STD_R2_0R1 + STK_TOC (htab),
3b421ab3
AM
15760 contents + rel->r_offset);
15761 }
15762 break;
15763
65f38f15
AM
15764 /* Branch taken prediction relocations. */
15765 case R_PPC64_ADDR14_BRTAKEN:
15766 case R_PPC64_REL14_BRTAKEN:
cedb70c5 15767 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1a0670f3 15768 /* Fall through. */
65f38f15 15769
86c76c7b 15770 /* Branch not taken prediction relocations. */
65f38f15
AM
15771 case R_PPC64_ADDR14_BRNTAKEN:
15772 case R_PPC64_REL14_BRNTAKEN:
95f0d0d2 15773 insn |= bfd_get_32 (input_bfd,
411e1bfb 15774 contents + rel->r_offset) & ~(0x01 << 21);
1a0670f3 15775 /* Fall through. */
86c76c7b 15776
b25116a9
AM
15777 case R_PPC64_REL14:
15778 max_br_offset = 1 << 15;
1a0670f3 15779 /* Fall through. */
5bd4f169 15780
65f38f15 15781 case R_PPC64_REL24:
05d0e962 15782 case R_PPC64_REL24_NOTOC:
23cedd1d 15783 case R_PPC64_PLTCALL:
5663e321 15784 case R_PPC64_PLTCALL_NOTOC:
ad8e1ba5
AM
15785 /* Calls to functions with a different TOC, such as calls to
15786 shared objects, need to alter the TOC pointer. This is
15787 done using a linkage stub. A REL24 branching to these
15788 linkage stubs needs to be followed by a nop, as the nop
15789 will be replaced with an instruction to restore the TOC
15790 base pointer. */
8387904d 15791 fdh = h;
b31867b6
AM
15792 if (h != NULL
15793 && h->oh != NULL
15794 && h->oh->is_func_descriptor)
15795 fdh = ppc_follow_link (h->oh);
31c76678
DK
15796 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15797 htab);
5663e321
AM
15798 if ((r_type == R_PPC64_PLTCALL
15799 || r_type == R_PPC64_PLTCALL_NOTOC)
23cedd1d 15800 && stub_entry != NULL
05d0e962
AM
15801 && stub_entry->stub_type >= ppc_stub_plt_call
15802 && stub_entry->stub_type <= ppc_stub_plt_call_both)
23cedd1d
AM
15803 stub_entry = NULL;
15804
6abec6bc 15805 if (stub_entry != NULL
d0abeec8
AM
15806 && ((stub_entry->stub_type >= ppc_stub_plt_call
15807 && stub_entry->stub_type <= ppc_stub_plt_call_both)
ad8e1ba5 15808 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
05d0e962
AM
15809 || stub_entry->stub_type == ppc_stub_plt_branch_both
15810 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15811 || stub_entry->stub_type == ppc_stub_long_branch_both))
41bd81ab 15812 {
b25116a9 15813 bfd_boolean can_plt_call = FALSE;
721956f4 15814
6e1816be
AM
15815 if (stub_entry->stub_type == ppc_stub_plt_call
15816 && !htab->opd_abi
15817 && htab->params->plt_localentry0 != 0
9e51d549 15818 && h != NULL
6e1816be
AM
15819 && is_elfv2_localentry0 (&h->elf))
15820 {
15821 /* The function doesn't use or change r2. */
15822 can_plt_call = TRUE;
15823 }
05d0e962
AM
15824 else if (r_type == R_PPC64_REL24_NOTOC)
15825 {
15826 /* NOTOC calls don't need to restore r2. */
15827 can_plt_call = TRUE;
15828 }
6e1816be 15829
f378ab09 15830 /* All of these stubs may modify r2, so there must be a
ba8ca3e7
AM
15831 branch and link followed by a nop. The nop is
15832 replaced by an insn to restore r2. */
6e1816be 15833 else if (rel->r_offset + 8 <= input_section->size)
41bd81ab 15834 {
ba8ca3e7
AM
15835 unsigned long br;
15836
15837 br = bfd_get_32 (input_bfd,
15838 contents + rel->r_offset);
15839 if ((br & 1) != 0)
41bd81ab 15840 {
ba8ca3e7
AM
15841 unsigned long nop;
15842
15843 nop = bfd_get_32 (input_bfd,
15844 contents + rel->r_offset + 4);
23cedd1d
AM
15845 if (nop == LD_R2_0R1 + STK_TOC (htab))
15846 can_plt_call = TRUE;
15847 else if (nop == NOP
15848 || nop == CROR_151515
15849 || nop == CROR_313131)
a7f2871e 15850 {
ba8ca3e7 15851 if (h != NULL
ed7007c1 15852 && is_tls_get_addr (&h->elf, htab)
7c9cf415 15853 && htab->params->tls_get_addr_opt)
ba8ca3e7
AM
15854 {
15855 /* Special stub used, leave nop alone. */
15856 }
15857 else
a078d95a
AM
15858 bfd_put_32 (input_bfd,
15859 LD_R2_0R1 + STK_TOC (htab),
ba8ca3e7
AM
15860 contents + rel->r_offset + 4);
15861 can_plt_call = TRUE;
a7f2871e 15862 }
41bd81ab 15863 }
5bd4f169 15864 }
721956f4 15865
ba8ca3e7 15866 if (!can_plt_call && h != NULL)
721956f4 15867 {
ba8ca3e7
AM
15868 const char *name = h->elf.root.root.string;
15869
15870 if (*name == '.')
15871 ++name;
15872
15873 if (strncmp (name, "__libc_start_main", 17) == 0
15874 && (name[17] == 0 || name[17] == '@'))
6ab189d5 15875 {
ba8ca3e7
AM
15876 /* Allow crt1 branch to go via a toc adjusting
15877 stub. Other calls that never return could do
15878 the same, if we could detect such. */
b25116a9 15879 can_plt_call = TRUE;
6ab189d5 15880 }
ba8ca3e7
AM
15881 }
15882
15883 if (!can_plt_call)
15884 {
15885 /* g++ as of 20130507 emits self-calls without a
15886 following nop. This is arguably wrong since we
15887 have conflicting information. On the one hand a
15888 global symbol and on the other a local call
15889 sequence, but don't error for this special case.
15890 It isn't possible to cheaply verify we have
15891 exactly such a call. Allow all calls to the same
15892 section. */
15893 asection *code_sec = sec;
15894
15895 if (get_opd_info (sec) != NULL)
ad8e1ba5 15896 {
ba8ca3e7
AM
15897 bfd_vma off = (relocation + addend
15898 - sec->output_section->vma
15899 - sec->output_offset);
bc30df16 15900
ba8ca3e7 15901 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 15902 }
ba8ca3e7
AM
15903 if (code_sec == input_section)
15904 can_plt_call = TRUE;
15905 }
15906
15907 if (!can_plt_call)
15908 {
05d0e962
AM
15909 if (stub_entry->stub_type >= ppc_stub_plt_call
15910 && stub_entry->stub_type <= ppc_stub_plt_call_both)
4805fc55 15911 info->callbacks->einfo
695344c0 15912 /* xgettext:c-format */
c1c8c1ef 15913 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15914 "(plt call stub)\n"),
4805fc55
AM
15915 input_bfd, input_section, rel->r_offset, sym_name);
15916 else
15917 info->callbacks->einfo
695344c0 15918 /* xgettext:c-format */
c1c8c1ef 15919 (_("%H: call to `%pT' lacks nop, can't restore toc; "
f53ad3cf 15920 "(toc save/adjust stub)\n"),
4805fc55 15921 input_bfd, input_section, rel->r_offset, sym_name);
ba8ca3e7
AM
15922
15923 bfd_set_error (bfd_error_bad_value);
15924 ret = FALSE;
721956f4
AM
15925 }
15926
b25116a9 15927 if (can_plt_call
05d0e962
AM
15928 && stub_entry->stub_type >= ppc_stub_plt_call
15929 && stub_entry->stub_type <= ppc_stub_plt_call_both)
b25116a9
AM
15930 unresolved_reloc = FALSE;
15931 }
15932
6abec6bc
AM
15933 if ((stub_entry == NULL
15934 || stub_entry->stub_type == ppc_stub_long_branch
15935 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
15936 && get_opd_info (sec) != NULL)
15937 {
15938 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
15939 bfd_vma off = (relocation + addend
15940 - sec->output_section->vma
15941 - sec->output_offset);
aef36ac1 15942 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
15943 if (dest != (bfd_vma) -1)
15944 {
15945 relocation = dest;
15946 addend = 0;
bc30df16 15947 reloc_dest = DEST_OPD;
8387904d
AM
15948 }
15949 }
15950
b25116a9
AM
15951 /* If the branch is out of reach we ought to have a long
15952 branch stub. */
15953 from = (rel->r_offset
15954 + input_section->output_offset
15955 + input_section->output_section->vma);
15956
6911b7dc
AM
15957 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15958 ? fdh->elf.other
15959 : sym->st_other);
15960
6abec6bc
AM
15961 if (stub_entry != NULL
15962 && (stub_entry->stub_type == ppc_stub_long_branch
15963 || stub_entry->stub_type == ppc_stub_plt_branch)
15964 && (r_type == R_PPC64_ADDR14_BRTAKEN
15965 || r_type == R_PPC64_ADDR14_BRNTAKEN
15966 || (relocation + addend - from + max_br_offset
15967 < 2 * max_br_offset)))
15968 /* Don't use the stub if this branch is in range. */
15969 stub_entry = NULL;
b25116a9 15970
05d0e962
AM
15971 if (stub_entry != NULL
15972 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15973 || stub_entry->stub_type == ppc_stub_long_branch_both
15974 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15975 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15976 && (r_type != R_PPC64_REL24_NOTOC
15977 || ((fdh ? fdh->elf.other : sym->st_other)
4a4e7361 15978 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
05d0e962
AM
15979 && (relocation + addend - from + max_br_offset
15980 < 2 * max_br_offset))
15981 stub_entry = NULL;
15982
15983 if (stub_entry != NULL
15984 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15985 || stub_entry->stub_type == ppc_stub_long_branch_both
15986 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15987 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15988 && r_type == R_PPC64_REL24_NOTOC
15989 && (relocation + addend - from + max_br_offset
15990 < 2 * max_br_offset))
15991 stub_entry = NULL;
15992
b25116a9
AM
15993 if (stub_entry != NULL)
15994 {
15995 /* Munge up the value and addend so that we call the stub
15996 rather than the procedure directly. */
a4b6fadd
AM
15997 asection *stub_sec = stub_entry->group->stub_sec;
15998
15999 if (stub_entry->stub_type == ppc_stub_save_res)
16000 relocation += (stub_sec->output_offset
16001 + stub_sec->output_section->vma
16002 + stub_sec->size - htab->sfpr->size
16003 - htab->sfpr->output_offset
16004 - htab->sfpr->output_section->vma);
16005 else
16006 relocation = (stub_entry->stub_offset
16007 + stub_sec->output_offset
16008 + stub_sec->output_section->vma);
b25116a9 16009 addend = 0;
bc30df16 16010 reloc_dest = DEST_STUB;
3b421ab3 16011
29433886
AM
16012 if ((((stub_entry->stub_type == ppc_stub_plt_call
16013 && ALWAYS_EMIT_R2SAVE)
16014 || stub_entry->stub_type == ppc_stub_plt_call_r2save
16015 || stub_entry->stub_type == ppc_stub_plt_call_both)
16016 && rel + 1 < relend
16017 && rel[1].r_offset == rel->r_offset + 4
16018 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
16019 || ((stub_entry->stub_type == ppc_stub_long_branch_both
16020 || stub_entry->stub_type == ppc_stub_plt_branch_both
16021 || stub_entry->stub_type == ppc_stub_plt_call_both)
16022 && r_type == R_PPC64_REL24_NOTOC))
16023 {
16024 /* Skip over the r2 store at the start of the stub. */
16025 if (!(stub_entry->stub_type >= ppc_stub_plt_call
16026 && htab->params->tls_get_addr_opt
16027 && h != NULL
16028 && is_tls_get_addr (&h->elf, htab)))
16029 relocation += 4;
16030 }
5663e321
AM
16031
16032 if (r_type == R_PPC64_REL24_NOTOC
16033 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
16034 || stub_entry->stub_type == ppc_stub_plt_call_both))
16035 htab->notoc_plt = 1;
b25116a9
AM
16036 }
16037
16038 if (insn != 0)
16039 {
794e51c0 16040 if (is_isa_v2)
721956f4 16041 {
b25116a9
AM
16042 /* Set 'a' bit. This is 0b00010 in BO field for branch
16043 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16044 for branch on CTR insns (BO == 1a00t or 1a01t). */
16045 if ((insn & (0x14 << 21)) == (0x04 << 21))
16046 insn |= 0x02 << 21;
16047 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16048 insn |= 0x08 << 21;
16049 else
16050 break;
16051 }
16052 else
16053 {
16054 /* Invert 'y' bit if not the default. */
4cc603a5 16055 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 16056 insn ^= 0x01 << 21;
721956f4 16057 }
b25116a9 16058
95f0d0d2 16059 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5bd4f169 16060 }
e86ce104 16061
06da1e8e
AM
16062 /* NOP out calls to undefined weak functions.
16063 We can thus call a weak function without first
16064 checking whether the function is defined. */
b25116a9 16065 else if (h != NULL
039b3fef 16066 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 16067 && h->elf.dynindx == -1
05d0e962
AM
16068 && (r_type == R_PPC64_REL24
16069 || r_type == R_PPC64_REL24_NOTOC)
b25116a9 16070 && relocation == 0
4cc603a5 16071 && addend == 0)
e86ce104 16072 {
95f0d0d2 16073 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
c316a17c 16074 goto copy_reloc;
e86ce104 16075 }
65f38f15 16076 break;
066f4018
AM
16077
16078 case R_PPC64_GOT16_DS:
6738c8a7
AM
16079 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16080 || !htab->do_toc_opt)
06507dab 16081 break;
066f4018
AM
16082 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16083 if (relocation + addend - from + 0x8000 < 0x10000
9e51d549 16084 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16085 {
16086 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 16087 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16088 {
16089 insn += (14u << 26) - (58u << 26);
16090 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16091 r_type = R_PPC64_TOC16;
16092 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16093 }
16094 }
16095 break;
16096
16097 case R_PPC64_GOT16_LO_DS:
16098 case R_PPC64_GOT16_HA:
6738c8a7
AM
16099 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16100 || !htab->do_toc_opt)
06507dab 16101 break;
066f4018
AM
16102 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16103 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
9e51d549 16104 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
066f4018
AM
16105 {
16106 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
fba8689a
AM
16107 if (r_type == R_PPC64_GOT16_LO_DS
16108 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
066f4018
AM
16109 {
16110 insn += (14u << 26) - (58u << 26);
16111 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16112 r_type = R_PPC64_TOC16_LO;
16113 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16114 }
fba8689a
AM
16115 else if (r_type == R_PPC64_GOT16_HA
16116 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
066f4018
AM
16117 {
16118 r_type = R_PPC64_TOC16_HA;
16119 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16120 }
16121 }
16122 break;
4a421c53
AM
16123
16124 case R_PPC64_GOT_PCREL34:
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;
4a421c53
AM
16128 from = (rel->r_offset
16129 + input_section->output_section->vma
16130 + input_section->output_offset);
6738c8a7 16131 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
9e51d549 16132 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
6738c8a7
AM
16133 break;
16134
16135 offset = rel->r_offset;
16136 pinsn = bfd_get_32 (input_bfd, contents + offset);
16137 pinsn <<= 32;
16138 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16139 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16140 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16141 break;
16142
16143 /* Replace with paddi. */
16144 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16145 r_type = R_PPC64_PCREL34;
16146 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16147 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16148 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16149 /* Fall through. */
4a421c53
AM
16150
16151 case R_PPC64_PCREL34:
6738c8a7
AM
16152 if (!htab->params->no_pcrel_opt
16153 && rel + 1 < relend
16154 && rel[1].r_offset == rel->r_offset
16155 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
9e51d549 16156 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
4a421c53
AM
16157 {
16158 offset = rel->r_offset;
16159 pinsn = bfd_get_32 (input_bfd, contents + offset);
16160 pinsn <<= 32;
16161 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16162 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16163 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16164 | (14ULL << 26) /* paddi */))
16165 {
6738c8a7
AM
16166 bfd_vma off2 = rel[1].r_addend;
16167 if (off2 == 0)
16168 /* zero means next insn. */
16169 off2 = 8;
16170 off2 += offset;
16171 if (off2 + 4 <= input_section->size)
4a421c53 16172 {
6738c8a7
AM
16173 uint64_t pinsn2;
16174 bfd_signed_vma addend_off;
16175 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16176 pinsn2 <<= 32;
16177 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
4a421c53 16178 {
6738c8a7
AM
16179 if (off2 + 8 > input_section->size)
16180 break;
16181 pinsn2 |= bfd_get_32 (input_bfd,
16182 contents + off2 + 4);
16183 }
16184 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16185 {
16186 addend += addend_off;
16187 rel->r_addend = addend;
16188 bfd_put_32 (input_bfd, pinsn >> 32,
16189 contents + offset);
16190 bfd_put_32 (input_bfd, pinsn,
16191 contents + offset + 4);
16192 bfd_put_32 (input_bfd, pinsn2 >> 32,
16193 contents + off2);
4a421c53 16194 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
6738c8a7
AM
16195 bfd_put_32 (input_bfd, pinsn2,
16196 contents + off2 + 4);
4a421c53
AM
16197 }
16198 }
16199 }
16200 }
16201 break;
65f38f15 16202 }
5bd4f169 16203
411e1bfb 16204 tls_type = 0;
23cedd1d 16205 save_unresolved_reloc = unresolved_reloc;
65f38f15
AM
16206 switch (r_type)
16207 {
16208 default:
cf97bcb0
AM
16209 /* xgettext:c-format */
16210 _bfd_error_handler (_("%pB: %s unsupported"),
16211 input_bfd, ppc64_elf_howto_table[r_type]->name);
5bd4f169 16212
65f38f15 16213 bfd_set_error (bfd_error_bad_value);
b34976b6 16214 ret = FALSE;
c316a17c 16215 goto copy_reloc;
5bd4f169 16216
65f38f15 16217 case R_PPC64_NONE:
411e1bfb 16218 case R_PPC64_TLS:
727fc41e
AM
16219 case R_PPC64_TLSGD:
16220 case R_PPC64_TLSLD:
3b421ab3 16221 case R_PPC64_TOCSAVE:
04c9666a
AM
16222 case R_PPC64_GNU_VTINHERIT:
16223 case R_PPC64_GNU_VTENTRY:
006589cf 16224 case R_PPC64_ENTRY:
4a421c53 16225 case R_PPC64_PCREL_OPT:
c316a17c 16226 goto copy_reloc;
5bd4f169
AM
16227
16228 /* GOT16 relocations. Like an ADDR16 using the symbol's
16229 address in the GOT as relocation value instead of the
411e1bfb 16230 symbol's value itself. Also, create a GOT entry for the
5bd4f169 16231 symbol and put the symbol value there. */
411e1bfb
AM
16232 case R_PPC64_GOT_TLSGD16:
16233 case R_PPC64_GOT_TLSGD16_LO:
16234 case R_PPC64_GOT_TLSGD16_HI:
16235 case R_PPC64_GOT_TLSGD16_HA:
87c69f97 16236 case R_PPC64_GOT_TLSGD_PCREL34:
951fd09b 16237 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
16238 goto dogot;
16239
16240 case R_PPC64_GOT_TLSLD16:
16241 case R_PPC64_GOT_TLSLD16_LO:
16242 case R_PPC64_GOT_TLSLD16_HI:
16243 case R_PPC64_GOT_TLSLD16_HA:
87c69f97 16244 case R_PPC64_GOT_TLSLD_PCREL34:
951fd09b 16245 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
16246 goto dogot;
16247
16248 case R_PPC64_GOT_TPREL16_DS:
16249 case R_PPC64_GOT_TPREL16_LO_DS:
16250 case R_PPC64_GOT_TPREL16_HI:
16251 case R_PPC64_GOT_TPREL16_HA:
87c69f97 16252 case R_PPC64_GOT_TPREL_PCREL34:
411e1bfb
AM
16253 tls_type = TLS_TLS | TLS_TPREL;
16254 goto dogot;
16255
16256 case R_PPC64_GOT_DTPREL16_DS:
16257 case R_PPC64_GOT_DTPREL16_LO_DS:
16258 case R_PPC64_GOT_DTPREL16_HI:
16259 case R_PPC64_GOT_DTPREL16_HA:
87c69f97 16260 case R_PPC64_GOT_DTPREL_PCREL34:
411e1bfb
AM
16261 tls_type = TLS_TLS | TLS_DTPREL;
16262 goto dogot;
16263
65f38f15
AM
16264 case R_PPC64_GOT16:
16265 case R_PPC64_GOT16_LO:
16266 case R_PPC64_GOT16_HI:
16267 case R_PPC64_GOT16_HA:
16268 case R_PPC64_GOT16_DS:
16269 case R_PPC64_GOT16_LO_DS:
5663e321 16270 case R_PPC64_GOT_PCREL34:
411e1bfb 16271 dogot:
5bd4f169
AM
16272 {
16273 /* Relocation is to the entry for this symbol in the global
16274 offset table. */
e717da7e 16275 asection *got;
d881513a 16276 bfd_vma *offp;
5bd4f169 16277 bfd_vma off;
d881513a 16278 unsigned long indx = 0;
927be08e 16279 struct got_entry *ent;
65f38f15 16280
d881513a 16281 if (tls_type == (TLS_TLS | TLS_LD)
9e51d549 16282 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
927be08e 16283 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 16284 else
5bd4f169 16285 {
d881513a
AM
16286 if (h != NULL)
16287 {
f0158f44
AM
16288 if (!htab->elf.dynamic_sections_created
16289 || h->elf.dynindx == -1
16290 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
21d68fcd 16291 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
d881513a
AM
16292 /* This is actually a static link, or it is a
16293 -Bsymbolic link and the symbol is defined
16294 locally, or the symbol was forced to be local
16295 because of a version file. */
16296 ;
16297 else
16298 {
039b3fef 16299 indx = h->elf.dynindx;
d881513a
AM
16300 unresolved_reloc = FALSE;
16301 }
039b3fef 16302 ent = h->elf.got.glist;
d881513a 16303 }
411e1bfb 16304 else
5bd4f169 16305 {
d881513a
AM
16306 if (local_got_ents == NULL)
16307 abort ();
16308 ent = local_got_ents[r_symndx];
5bd4f169 16309 }
d881513a
AM
16310
16311 for (; ent != NULL; ent = ent->next)
133a1f60 16312 if (ent->addend == orig_rel.r_addend
e717da7e 16313 && ent->owner == input_bfd
d881513a
AM
16314 && ent->tls_type == tls_type)
16315 break;
5bd4f169 16316 }
411e1bfb 16317
927be08e
AM
16318 if (ent == NULL)
16319 abort ();
16320 if (ent->is_indirect)
16321 ent = ent->got.ent;
16322 offp = &ent->got.offset;
16323 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
16324 if (got == NULL)
16325 abort ();
16326
411e1bfb
AM
16327 /* The offset must always be a multiple of 8. We use the
16328 least significant bit to record whether we have already
16329 processed this entry. */
d881513a 16330 off = *offp;
411e1bfb
AM
16331 if ((off & 1) != 0)
16332 off &= ~1;
5bd4f169
AM
16333 else
16334 {
411e1bfb
AM
16335 /* Generate relocs for the dynamic linker, except in
16336 the case of TLSLD where we'll use one entry per
16337 module. */
25f23106
AM
16338 asection *relgot;
16339 bfd_boolean ifunc;
e717da7e 16340
d881513a 16341 *offp = off | 1;
25f23106
AM
16342 relgot = NULL;
16343 ifunc = (h != NULL
16344 ? h->elf.type == STT_GNU_IFUNC
16345 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 16346 if (ifunc)
82e66161
AM
16347 {
16348 relgot = htab->elf.irelplt;
efb2a7b4 16349 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16350 htab->elf.ifunc_resolvers = TRUE;
82e66161 16351 }
f0158f44
AM
16352 else if (indx != 0
16353 || (bfd_link_pic (info)
16354 && (h == NULL
f749f26e
AM
16355 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16356 && !(tls_type != 0
f15d0b54 16357 && bfd_link_executable (info)
9e51d549
AM
16358 && (h == NULL
16359 || SYMBOL_REFERENCES_LOCAL (info,
16360 &h->elf)))))
19e08130 16361 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 16362 if (relgot != NULL)
5bd4f169 16363 {
e717da7e
AM
16364 outrel.r_offset = (got->output_section->vma
16365 + got->output_offset
411e1bfb 16366 + off);
133a1f60 16367 outrel.r_addend = orig_rel.r_addend;
d881513a 16368 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 16369 {
411e1bfb 16370 outrel.r_addend = 0;
e515b051 16371 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
16372 if (tls_type == (TLS_TLS | TLS_GD))
16373 {
e717da7e
AM
16374 loc = relgot->contents;
16375 loc += (relgot->reloc_count++
d881513a
AM
16376 * sizeof (Elf64_External_Rela));
16377 bfd_elf64_swap_reloca_out (output_bfd,
16378 &outrel, loc);
e515b051 16379 outrel.r_offset += 8;
133a1f60 16380 outrel.r_addend = orig_rel.r_addend;
d881513a
AM
16381 outrel.r_info
16382 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 16383 }
411e1bfb 16384 }
951fd09b 16385 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 16386 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 16387 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 16388 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
16389 else if (indx != 0)
16390 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16391 else
81407a69 16392 {
25f23106
AM
16393 if (ifunc)
16394 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16395 else
16396 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
16397
16398 /* Write the .got section contents for the sake
16399 of prelink. */
e717da7e 16400 loc = got->contents + off;
23fbd6fa
JJ
16401 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16402 loc);
81407a69 16403 }
81407a69
AM
16404
16405 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
16406 {
16407 outrel.r_addend += relocation;
16408 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
989f9879
AM
16409 {
16410 if (htab->elf.tls_sec == NULL)
16411 outrel.r_addend = 0;
16412 else
16413 outrel.r_addend -= htab->elf.tls_sec->vma;
16414 }
e515b051 16415 }
e717da7e
AM
16416 loc = relgot->contents;
16417 loc += (relgot->reloc_count++
411e1bfb
AM
16418 * sizeof (Elf64_External_Rela));
16419 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16420 }
16421
ad8e1ba5 16422 /* Init the .got section contents here if we're not
81407a69 16423 emitting a reloc. */
d881513a 16424 else
411e1bfb 16425 {
133a1f60 16426 relocation += orig_rel.r_addend;
f0158f44 16427 if (tls_type != 0)
411e1bfb 16428 {
989f9879
AM
16429 if (htab->elf.tls_sec == NULL)
16430 relocation = 0;
16431 else
16432 {
f0158f44
AM
16433 if (tls_type & TLS_LD)
16434 relocation = 0;
16435 else
16436 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
676ee2b5 16437 if (tls_type & TLS_TPREL)
989f9879
AM
16438 relocation += DTP_OFFSET - TP_OFFSET;
16439 }
5bd4f169 16440
f0158f44 16441 if (tls_type & (TLS_GD | TLS_LD))
7b609f53
AM
16442 {
16443 bfd_put_64 (output_bfd, relocation,
e717da7e 16444 got->contents + off + 8);
676ee2b5 16445 relocation = 1;
7b609f53 16446 }
411e1bfb
AM
16447 }
16448 bfd_put_64 (output_bfd, relocation,
e717da7e 16449 got->contents + off);
5bd4f169
AM
16450 }
16451 }
16452
65f38f15
AM
16453 if (off >= (bfd_vma) -2)
16454 abort ();
16455
bf102f86 16456 relocation = got->output_section->vma + got->output_offset + off;
133a1f60 16457 addend = 0;
c213164a 16458 if (!(r_type == R_PPC64_GOT_PCREL34
87c69f97
AM
16459 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16460 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16461 || r_type == R_PPC64_GOT_TPREL_PCREL34
16462 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
5663e321 16463 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
5bd4f169 16464 }
65f38f15
AM
16465 break;
16466
16467 case R_PPC64_PLT16_HA:
16468 case R_PPC64_PLT16_HI:
16469 case R_PPC64_PLT16_LO:
08be3224 16470 case R_PPC64_PLT16_LO_DS:
5663e321
AM
16471 case R_PPC64_PLT_PCREL34:
16472 case R_PPC64_PLT_PCREL34_NOTOC:
65f38f15
AM
16473 case R_PPC64_PLT32:
16474 case R_PPC64_PLT64:
23cedd1d 16475 case R_PPC64_PLTSEQ:
5663e321 16476 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d 16477 case R_PPC64_PLTCALL:
5663e321 16478 case R_PPC64_PLTCALL_NOTOC:
65f38f15
AM
16479 /* Relocation is to the entry for this symbol in the
16480 procedure linkage table. */
23cedd1d 16481 unresolved_reloc = TRUE;
cbf95972
AM
16482 {
16483 struct plt_entry **plt_list = NULL;
16484 if (h != NULL)
16485 plt_list = &h->elf.plt.plist;
16486 else if (local_got_ents != NULL)
16487 {
16488 struct plt_entry **local_plt = (struct plt_entry **)
16489 (local_got_ents + symtab_hdr->sh_info);
2d7ad24e 16490 plt_list = local_plt + r_symndx;
cbf95972
AM
16491 }
16492 if (plt_list)
16493 {
16494 struct plt_entry *ent;
65f38f15 16495
cbf95972
AM
16496 for (ent = *plt_list; ent != NULL; ent = ent->next)
16497 if (ent->plt.offset != (bfd_vma) -1
133a1f60 16498 && ent->addend == orig_rel.r_addend)
cbf95972
AM
16499 {
16500 asection *plt;
08be3224 16501 bfd_vma got;
cbf95972
AM
16502
16503 plt = htab->elf.splt;
30845f11 16504 if (use_local_plt (info, elf_hash_entry (h)))
2d7ad24e
AM
16505 {
16506 if (h != NULL
16507 ? h->elf.type == STT_GNU_IFUNC
16508 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16509 plt = htab->elf.iplt;
16510 else
16511 plt = htab->pltlocal;
16512 }
16513 relocation = (plt->output_section->vma
16514 + plt->output_offset
16515 + ent->plt.offset);
08be3224 16516 if (r_type == R_PPC64_PLT16_HA
2cdcc330
AM
16517 || r_type == R_PPC64_PLT16_HI
16518 || r_type == R_PPC64_PLT16_LO
16519 || r_type == R_PPC64_PLT16_LO_DS)
08be3224
AM
16520 {
16521 got = (elf_gp (output_bfd)
16522 + htab->sec_info[input_section->id].toc_off);
16523 relocation -= got;
16524 }
133a1f60 16525 addend = 0;
cbf95972
AM
16526 unresolved_reloc = FALSE;
16527 break;
16528 }
16529 }
16530 }
65f38f15 16531 break;
5bd4f169 16532
0b13192e
AM
16533 case R_PPC64_TOC:
16534 /* Relocation value is TOC base. */
16535 relocation = TOCstart;
cf35638d 16536 if (r_symndx == STN_UNDEF)
6f20ed8a 16537 relocation += htab->sec_info[input_section->id].toc_off;
8517fae7
AM
16538 else if (unresolved_reloc)
16539 ;
6f20ed8a
AM
16540 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16541 relocation += htab->sec_info[sec->id].toc_off;
0b13192e
AM
16542 else
16543 unresolved_reloc = TRUE;
ab96bf03 16544 goto dodyn;
0b13192e 16545
5bd4f169
AM
16546 /* TOC16 relocs. We want the offset relative to the TOC base,
16547 which is the address of the start of the TOC plus 0x8000.
16548 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16549 in this order. */
65f38f15
AM
16550 case R_PPC64_TOC16:
16551 case R_PPC64_TOC16_LO:
16552 case R_PPC64_TOC16_HI:
16553 case R_PPC64_TOC16_DS:
16554 case R_PPC64_TOC16_LO_DS:
16555 case R_PPC64_TOC16_HA:
6f20ed8a 16556 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
1bdd8fac
AM
16557 if (h != NULL)
16558 goto dodyn;
5bd4f169
AM
16559 break;
16560
16561 /* Relocate against the beginning of the section. */
65f38f15
AM
16562 case R_PPC64_SECTOFF:
16563 case R_PPC64_SECTOFF_LO:
16564 case R_PPC64_SECTOFF_HI:
16565 case R_PPC64_SECTOFF_DS:
16566 case R_PPC64_SECTOFF_LO_DS:
16567 case R_PPC64_SECTOFF_HA:
4ce794b7 16568 if (sec != NULL)
65f38f15 16569 addend -= sec->output_section->vma;
5bd4f169
AM
16570 break;
16571
25f23106
AM
16572 case R_PPC64_REL16:
16573 case R_PPC64_REL16_LO:
16574 case R_PPC64_REL16_HI:
16575 case R_PPC64_REL16_HA:
4a969973
AM
16576 case R_PPC64_REL16_HIGH:
16577 case R_PPC64_REL16_HIGHA:
16578 case R_PPC64_REL16_HIGHER:
16579 case R_PPC64_REL16_HIGHERA:
16580 case R_PPC64_REL16_HIGHEST:
16581 case R_PPC64_REL16_HIGHESTA:
5663e321
AM
16582 case R_PPC64_REL16_HIGHER34:
16583 case R_PPC64_REL16_HIGHERA34:
16584 case R_PPC64_REL16_HIGHEST34:
16585 case R_PPC64_REL16_HIGHESTA34:
a680de9a 16586 case R_PPC64_REL16DX_HA:
721956f4
AM
16587 case R_PPC64_REL14:
16588 case R_PPC64_REL14_BRNTAKEN:
16589 case R_PPC64_REL14_BRTAKEN:
5d1634d7 16590 case R_PPC64_REL24:
05d0e962 16591 case R_PPC64_REL24_NOTOC:
5663e321
AM
16592 case R_PPC64_PCREL34:
16593 case R_PPC64_PCREL28:
5d1634d7
AM
16594 break;
16595
411e1bfb
AM
16596 case R_PPC64_TPREL16:
16597 case R_PPC64_TPREL16_LO:
16598 case R_PPC64_TPREL16_HI:
16599 case R_PPC64_TPREL16_HA:
16600 case R_PPC64_TPREL16_DS:
16601 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
16602 case R_PPC64_TPREL16_HIGH:
16603 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
16604 case R_PPC64_TPREL16_HIGHER:
16605 case R_PPC64_TPREL16_HIGHERA:
16606 case R_PPC64_TPREL16_HIGHEST:
16607 case R_PPC64_TPREL16_HIGHESTA:
c213164a 16608 case R_PPC64_TPREL34:
766bc656
AM
16609 if (h != NULL
16610 && h->elf.root.type == bfd_link_hash_undefweak
16611 && h->elf.dynindx == -1)
16612 {
16613 /* Make this relocation against an undefined weak symbol
16614 resolve to zero. This is really just a tweak, since
16615 code using weak externs ought to check that they are
16616 defined before using them. */
16617 bfd_byte *p = contents + rel->r_offset - d_offset;
16618
95f0d0d2 16619 insn = bfd_get_32 (input_bfd, p);
766bc656
AM
16620 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16621 if (insn != 0)
95f0d0d2 16622 bfd_put_32 (input_bfd, insn, p);
766bc656
AM
16623 break;
16624 }
989f9879
AM
16625 if (htab->elf.tls_sec != NULL)
16626 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7c8bbca5
AM
16627 /* The TPREL16 relocs shouldn't really be used in shared
16628 libs or with non-local symbols as that will result in
16629 DT_TEXTREL being set, but support them anyway. */
16630 goto dodyn;
411e1bfb
AM
16631
16632 case R_PPC64_DTPREL16:
16633 case R_PPC64_DTPREL16_LO:
16634 case R_PPC64_DTPREL16_HI:
16635 case R_PPC64_DTPREL16_HA:
16636 case R_PPC64_DTPREL16_DS:
16637 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
16638 case R_PPC64_DTPREL16_HIGH:
16639 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
16640 case R_PPC64_DTPREL16_HIGHER:
16641 case R_PPC64_DTPREL16_HIGHERA:
16642 case R_PPC64_DTPREL16_HIGHEST:
16643 case R_PPC64_DTPREL16_HIGHESTA:
c213164a 16644 case R_PPC64_DTPREL34:
989f9879
AM
16645 if (htab->elf.tls_sec != NULL)
16646 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
16647 break;
16648
45965137
AM
16649 case R_PPC64_ADDR64_LOCAL:
16650 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16651 ? h->elf.other
16652 : sym->st_other);
16653 break;
16654
e515b051
AM
16655 case R_PPC64_DTPMOD64:
16656 relocation = 1;
16657 addend = 0;
16658 goto dodyn;
16659
411e1bfb 16660 case R_PPC64_TPREL64:
989f9879
AM
16661 if (htab->elf.tls_sec != NULL)
16662 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
16663 goto dodyn;
16664
16665 case R_PPC64_DTPREL64:
989f9879
AM
16666 if (htab->elf.tls_sec != NULL)
16667 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
1a0670f3 16668 /* Fall through. */
411e1bfb 16669
65f38f15
AM
16670 /* Relocations that may need to be propagated if this is a
16671 dynamic object. */
04c9666a 16672 case R_PPC64_REL30:
65f38f15
AM
16673 case R_PPC64_REL32:
16674 case R_PPC64_REL64:
16675 case R_PPC64_ADDR14:
16676 case R_PPC64_ADDR14_BRNTAKEN:
16677 case R_PPC64_ADDR14_BRTAKEN:
16678 case R_PPC64_ADDR16:
16679 case R_PPC64_ADDR16_DS:
16680 case R_PPC64_ADDR16_HA:
16681 case R_PPC64_ADDR16_HI:
f9c6b907
AM
16682 case R_PPC64_ADDR16_HIGH:
16683 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
16684 case R_PPC64_ADDR16_HIGHER:
16685 case R_PPC64_ADDR16_HIGHERA:
16686 case R_PPC64_ADDR16_HIGHEST:
16687 case R_PPC64_ADDR16_HIGHESTA:
16688 case R_PPC64_ADDR16_LO:
16689 case R_PPC64_ADDR16_LO_DS:
5663e321
AM
16690 case R_PPC64_ADDR16_HIGHER34:
16691 case R_PPC64_ADDR16_HIGHERA34:
16692 case R_PPC64_ADDR16_HIGHEST34:
16693 case R_PPC64_ADDR16_HIGHESTA34:
65f38f15 16694 case R_PPC64_ADDR24:
65f38f15
AM
16695 case R_PPC64_ADDR32:
16696 case R_PPC64_ADDR64:
16697 case R_PPC64_UADDR16:
16698 case R_PPC64_UADDR32:
16699 case R_PPC64_UADDR64:
5663e321
AM
16700 case R_PPC64_D34:
16701 case R_PPC64_D34_LO:
16702 case R_PPC64_D34_HI30:
16703 case R_PPC64_D34_HA30:
16704 case R_PPC64_D28:
411e1bfb 16705 dodyn:
5d1634d7 16706 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
16707 break;
16708
41bd81ab
AM
16709 if (NO_OPD_RELOCS && is_opd)
16710 break;
16711
8a9e8e72 16712 if (bfd_link_pic (info)
b1b07054 16713 ? ((h == NULL
190eb1dd 16714 || h->elf.dyn_relocs != NULL)
b1b07054
AM
16715 && ((h != NULL && pc_dynrelocs (h))
16716 || must_be_dyn_reloc (info, r_type)))
8a9e8e72 16717 : (h != NULL
190eb1dd 16718 ? h->elf.dyn_relocs != NULL
d311bc8b 16719 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
65f38f15 16720 {
b34976b6 16721 bfd_boolean skip, relocate;
65f38f15 16722 asection *sreloc;
1cf1f670 16723 bfd_vma out_off;
82e66161 16724 long indx = 0;
65f38f15
AM
16725
16726 /* When generating a dynamic object, these relocations
16727 are copied into the output file to be resolved at run
16728 time. */
16729
b34976b6
AM
16730 skip = FALSE;
16731 relocate = FALSE;
65f38f15 16732
1cf1f670
AM
16733 out_off = _bfd_elf_section_offset (output_bfd, info,
16734 input_section, rel->r_offset);
16735 if (out_off == (bfd_vma) -1)
b34976b6 16736 skip = TRUE;
1cf1f670 16737 else if (out_off == (bfd_vma) -2)
b34976b6 16738 skip = TRUE, relocate = TRUE;
1cf1f670
AM
16739 out_off += (input_section->output_section->vma
16740 + input_section->output_offset);
16741 outrel.r_offset = out_off;
411e1bfb 16742 outrel.r_addend = rel->r_addend;
65f38f15 16743
1cf1f670
AM
16744 /* Optimize unaligned reloc use. */
16745 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16746 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16747 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16748 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16749 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16750 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16751 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16752 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16753 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16754
65f38f15 16755 if (skip)
0bb2d96a 16756 memset (&outrel, 0, sizeof outrel);
9e51d549
AM
16757 else if (h != NULL
16758 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
16759 && !is_opd
16760 && r_type != R_PPC64_TOC)
14acf4dc 16761 {
82e66161
AM
16762 indx = h->elf.dynindx;
16763 BFD_ASSERT (indx != -1);
16764 outrel.r_info = ELF64_R_INFO (indx, r_type);
14acf4dc 16765 }
65f38f15
AM
16766 else
16767 {
41bd81ab
AM
16768 /* This symbol is local, or marked to become local,
16769 or this is an opd section reloc which must point
16770 at a local function. */
65f38f15 16771 outrel.r_addend += relocation;
e86ce104 16772 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 16773 {
3fad3c7c 16774 if (is_opd && h != NULL)
afbe61cf
AM
16775 {
16776 /* Lie about opd entries. This case occurs
16777 when building shared libraries and we
16778 reference a function in another shared
3fad3c7c
AM
16779 lib. The same thing happens for a weak
16780 definition in an application that's
16781 overridden by a strong definition in a
16782 shared lib. (I believe this is a generic
16783 bug in binutils handling of weak syms.)
16784 In these cases we won't use the opd
1e2f5b6e 16785 entry in this lib. */
b34976b6 16786 unresolved_reloc = FALSE;
afbe61cf 16787 }
25f23106
AM
16788 if (!is_opd
16789 && r_type == R_PPC64_ADDR64
16790 && (h != NULL
16791 ? h->elf.type == STT_GNU_IFUNC
16792 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16793 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16794 else
16795 {
16796 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 16797
25f23106
AM
16798 /* We need to relocate .opd contents for ld.so.
16799 Prelink also wants simple and consistent rules
16800 for relocs. This make all RELATIVE relocs have
16801 *r_offset equal to r_addend. */
16802 relocate = TRUE;
16803 }
65f38f15
AM
16804 }
16805 else
16806 {
25f23106
AM
16807 if (h != NULL
16808 ? h->elf.type == STT_GNU_IFUNC
16809 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16810 {
25f53a85 16811 info->callbacks->einfo
695344c0 16812 /* xgettext:c-format */
174d0a74 16813 (_("%H: %s for indirect "
c1c8c1ef 16814 "function `%pT' unsupported\n"),
25f53a85 16815 input_bfd, input_section, rel->r_offset,
25f23106
AM
16816 ppc64_elf_howto_table[r_type]->name,
16817 sym_name);
16818 ret = FALSE;
16819 }
cf35638d 16820 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
16821 ;
16822 else if (sec == NULL || sec->owner == NULL)
16823 {
16824 bfd_set_error (bfd_error_bad_value);
b34976b6 16825 return FALSE;
65f38f15
AM
16826 }
16827 else
16828 {
f26a3287 16829 asection *osec = sec->output_section;
65f38f15 16830
f26a3287
AM
16831 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16832 {
16833 /* TLS symbol values are relative to the
16834 TLS segment. Dynamic relocations for
16835 local TLS symbols therefore can't be
16836 reduced to a relocation against their
16837 section symbol because it holds the
16838 address of the section, not a value
16839 relative to the TLS segment. We could
16840 change the .tdata dynamic section symbol
16841 to be zero value but STN_UNDEF works
16842 and is used elsewhere, eg. for TPREL64
16843 GOT relocs against local TLS symbols. */
16844 osec = htab->elf.tls_sec;
16845 indx = 0;
16846 }
16847 else
74541ad4 16848 {
74541ad4 16849 indx = elf_section_data (osec)->dynindx;
f26a3287
AM
16850 if (indx == 0)
16851 {
16852 if ((osec->flags & SEC_READONLY) == 0
16853 && htab->elf.data_index_section != NULL)
16854 osec = htab->elf.data_index_section;
16855 else
16856 osec = htab->elf.text_index_section;
16857 indx = elf_section_data (osec)->dynindx;
16858 }
16859 BFD_ASSERT (indx != 0);
74541ad4 16860 }
74541ad4 16861
65f38f15
AM
16862 /* We are turning this relocation into one
16863 against a section symbol, so subtract out
16864 the output section's address but not the
16865 offset of the input section in the output
16866 section. */
16867 outrel.r_addend -= osec->vma;
16868 }
16869
16870 outrel.r_info = ELF64_R_INFO (indx, r_type);
16871 }
16872 }
16873
16874 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
16875 if (h != NULL
16876 ? h->elf.type == STT_GNU_IFUNC
16877 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
82e66161
AM
16878 {
16879 sreloc = htab->elf.irelplt;
efb2a7b4 16880 if (indx == 0 || is_static_defined (&h->elf))
cebd6b8a 16881 htab->elf.ifunc_resolvers = TRUE;
82e66161 16882 }
65f38f15
AM
16883 if (sreloc == NULL)
16884 abort ();
16885
dfbb6ac9
AM
16886 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16887 >= sreloc->size)
16888 abort ();
947216bf
AM
16889 loc = sreloc->contents;
16890 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
16891 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16892
e59a1001
AM
16893 if (!warned_dynamic
16894 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16895 {
16896 info->callbacks->einfo
16897 /* xgettext:c-format */
16898 (_("%X%P: %pB: %s against %pT "
16899 "is not supported by glibc as a dynamic relocation\n"),
16900 input_bfd,
16901 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16902 sym_name);
16903 warned_dynamic = TRUE;
16904 }
16905
65f38f15
AM
16906 /* If this reloc is against an external symbol, it will
16907 be computed at runtime, so there's no need to do
81407a69
AM
16908 anything now. However, for the sake of prelink ensure
16909 that the section contents are a known value. */
2cdcc330 16910 if (!relocate)
81407a69
AM
16911 {
16912 unresolved_reloc = FALSE;
16913 /* The value chosen here is quite arbitrary as ld.so
16914 ignores section contents except for the special
16915 case of .opd where the contents might be accessed
16916 before relocation. Choose zero, as that won't
16917 cause reloc overflow. */
16918 relocation = 0;
16919 addend = 0;
16920 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16921 to improve backward compatibility with older
16922 versions of ld. */
16923 if (r_type == R_PPC64_ADDR64)
16924 addend = outrel.r_addend;
16925 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 16926 else if (ppc64_elf_howto_table[r_type]->pc_relative)
f0158f44 16927 addend = outrel.r_offset;
81407a69 16928 }
65f38f15 16929 }
5bd4f169
AM
16930 break;
16931
65f38f15
AM
16932 case R_PPC64_COPY:
16933 case R_PPC64_GLOB_DAT:
16934 case R_PPC64_JMP_SLOT:
25f23106 16935 case R_PPC64_JMP_IREL:
65f38f15
AM
16936 case R_PPC64_RELATIVE:
16937 /* We shouldn't ever see these dynamic relocs in relocatable
16938 files. */
ae9a127f 16939 /* Fall through. */
65f38f15
AM
16940
16941 case R_PPC64_PLTGOT16:
16942 case R_PPC64_PLTGOT16_DS:
16943 case R_PPC64_PLTGOT16_HA:
16944 case R_PPC64_PLTGOT16_HI:
16945 case R_PPC64_PLTGOT16_LO:
16946 case R_PPC64_PLTGOT16_LO_DS:
16947 case R_PPC64_PLTREL32:
16948 case R_PPC64_PLTREL64:
16949 /* These ones haven't been implemented yet. */
16950
25f53a85 16951 info->callbacks->einfo
695344c0 16952 /* xgettext:c-format */
c1c8c1ef 16953 (_("%P: %pB: %s is not supported for `%pT'\n"),
d003868e 16954 input_bfd,
4ce794b7 16955 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
16956
16957 bfd_set_error (bfd_error_invalid_operation);
b34976b6 16958 ret = FALSE;
c316a17c 16959 goto copy_reloc;
65f38f15 16960 }
5bd4f169 16961
67f0cbdb
AM
16962 /* Multi-instruction sequences that access the TOC can be
16963 optimized, eg. addis ra,r2,0; addi rb,ra,x;
07d6d2b8 16964 to nop; addi rb,r2,x; */
67f0cbdb
AM
16965 switch (r_type)
16966 {
16967 default:
16968 break;
16969
16970 case R_PPC64_GOT_TLSLD16_HI:
16971 case R_PPC64_GOT_TLSGD16_HI:
16972 case R_PPC64_GOT_TPREL16_HI:
16973 case R_PPC64_GOT_DTPREL16_HI:
16974 case R_PPC64_GOT16_HI:
16975 case R_PPC64_TOC16_HI:
16976 /* These relocs would only be useful if building up an
16977 offset to later add to r2, perhaps in an indexed
16978 addressing mode instruction. Don't try to optimize.
16979 Unfortunately, the possibility of someone building up an
16980 offset like this or even with the HA relocs, means that
16981 we need to check the high insn when optimizing the low
16982 insn. */
16983 break;
16984
5663e321
AM
16985 case R_PPC64_PLTCALL_NOTOC:
16986 if (!unresolved_reloc)
16987 htab->notoc_plt = 1;
16988 /* Fall through. */
23cedd1d
AM
16989 case R_PPC64_PLTCALL:
16990 if (unresolved_reloc)
16991 {
16992 /* No plt entry. Make this into a direct call. */
16993 bfd_byte *p = contents + rel->r_offset;
16994 insn = bfd_get_32 (input_bfd, p);
16995 insn &= 1;
16996 bfd_put_32 (input_bfd, B_DOT | insn, p);
5663e321
AM
16997 if (r_type == R_PPC64_PLTCALL)
16998 bfd_put_32 (input_bfd, NOP, p + 4);
23cedd1d
AM
16999 unresolved_reloc = save_unresolved_reloc;
17000 r_type = R_PPC64_REL24;
17001 }
17002 break;
17003
5663e321 17004 case R_PPC64_PLTSEQ_NOTOC:
23cedd1d
AM
17005 case R_PPC64_PLTSEQ:
17006 if (unresolved_reloc)
17007 {
17008 unresolved_reloc = FALSE;
17009 goto nop_it;
17010 }
17011 break;
17012
5663e321
AM
17013 case R_PPC64_PLT_PCREL34_NOTOC:
17014 if (!unresolved_reloc)
17015 htab->notoc_plt = 1;
17016 /* Fall through. */
17017 case R_PPC64_PLT_PCREL34:
17018 if (unresolved_reloc)
17019 {
17020 bfd_byte *p = contents + rel->r_offset;
17021 bfd_put_32 (input_bfd, PNOP >> 32, p);
17022 bfd_put_32 (input_bfd, PNOP, p + 4);
17023 unresolved_reloc = FALSE;
17024 goto copy_reloc;
17025 }
17026 break;
17027
23cedd1d
AM
17028 case R_PPC64_PLT16_HA:
17029 if (unresolved_reloc)
17030 {
17031 unresolved_reloc = FALSE;
17032 goto nop_it;
17033 }
17034 /* Fall through. */
67f0cbdb
AM
17035 case R_PPC64_GOT_TLSLD16_HA:
17036 case R_PPC64_GOT_TLSGD16_HA:
17037 case R_PPC64_GOT_TPREL16_HA:
17038 case R_PPC64_GOT_DTPREL16_HA:
17039 case R_PPC64_GOT16_HA:
17040 case R_PPC64_TOC16_HA:
98528052 17041 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17042 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052 17043 {
23cedd1d
AM
17044 bfd_byte *p;
17045 nop_it:
17046 p = contents + (rel->r_offset & ~3);
98528052 17047 bfd_put_32 (input_bfd, NOP, p);
d830549d 17048 goto copy_reloc;
98528052 17049 }
67f0cbdb
AM
17050 break;
17051
23cedd1d
AM
17052 case R_PPC64_PLT16_LO:
17053 case R_PPC64_PLT16_LO_DS:
17054 if (unresolved_reloc)
17055 {
17056 unresolved_reloc = FALSE;
17057 goto nop_it;
17058 }
17059 /* Fall through. */
67f0cbdb
AM
17060 case R_PPC64_GOT_TLSLD16_LO:
17061 case R_PPC64_GOT_TLSGD16_LO:
17062 case R_PPC64_GOT_TPREL16_LO_DS:
17063 case R_PPC64_GOT_DTPREL16_LO_DS:
17064 case R_PPC64_GOT16_LO:
17065 case R_PPC64_GOT16_LO_DS:
17066 case R_PPC64_TOC16_LO:
17067 case R_PPC64_TOC16_LO_DS:
98528052 17068 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 17069 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
17070 {
17071 bfd_byte *p = contents + (rel->r_offset & ~3);
17072 insn = bfd_get_32 (input_bfd, p);
2365f8d7 17073 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
560c8763
AM
17074 {
17075 /* Transform addic to addi when we change reg. */
2365f8d7 17076 insn &= ~((0x3fu << 26) | (0x1f << 16));
560c8763
AM
17077 insn |= (14u << 26) | (2 << 16);
17078 }
17079 else
67f0cbdb 17080 {
98528052
AM
17081 insn &= ~(0x1f << 16);
17082 insn |= 2 << 16;
67f0cbdb 17083 }
560c8763 17084 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
17085 }
17086 break;
9a23f96e
AM
17087
17088 case R_PPC64_TPREL16_HA:
17089 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17090 {
17091 bfd_byte *p = contents + (rel->r_offset & ~3);
252dcdf4
AM
17092 bfd_put_32 (input_bfd, NOP, p);
17093 goto copy_reloc;
9a23f96e
AM
17094 }
17095 break;
17096
17097 case R_PPC64_TPREL16_LO:
17098 case R_PPC64_TPREL16_LO_DS:
17099 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17100 {
17101 bfd_byte *p = contents + (rel->r_offset & ~3);
17102 insn = bfd_get_32 (input_bfd, p);
17103 insn &= ~(0x1f << 16);
17104 insn |= 13 << 16;
17105 bfd_put_32 (input_bfd, insn, p);
17106 }
17107 break;
67f0cbdb
AM
17108 }
17109
65f38f15
AM
17110 /* Do any further special processing. */
17111 switch (r_type)
17112 {
17113 default:
17114 break;
17115
25f23106 17116 case R_PPC64_REL16_HA:
4a969973
AM
17117 case R_PPC64_REL16_HIGHA:
17118 case R_PPC64_REL16_HIGHERA:
17119 case R_PPC64_REL16_HIGHESTA:
a680de9a 17120 case R_PPC64_REL16DX_HA:
f9c6b907
AM
17121 case R_PPC64_ADDR16_HA:
17122 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
17123 case R_PPC64_ADDR16_HIGHERA:
17124 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
17125 case R_PPC64_TOC16_HA:
17126 case R_PPC64_SECTOFF_HA:
411e1bfb 17127 case R_PPC64_TPREL16_HA:
f9c6b907 17128 case R_PPC64_TPREL16_HIGHA:
411e1bfb 17129 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 17130 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
17131 case R_PPC64_DTPREL16_HA:
17132 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 17133 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 17134 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
17135 /* It's just possible that this symbol is a weak symbol
17136 that's not actually defined anywhere. In that case,
17137 'sec' would be NULL, and we should leave the symbol
17138 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
17139 if (sec == NULL)
17140 break;
1a0670f3 17141 /* Fall through. */
5c5f6e17
AM
17142
17143 case R_PPC64_GOT16_HA:
17144 case R_PPC64_PLTGOT16_HA:
17145 case R_PPC64_PLT16_HA:
17146 case R_PPC64_GOT_TLSGD16_HA:
17147 case R_PPC64_GOT_TLSLD16_HA:
17148 case R_PPC64_GOT_TPREL16_HA:
17149 case R_PPC64_GOT_DTPREL16_HA:
17150 /* Add 0x10000 if sign bit in 0:15 is set.
17151 Bits 0:15 are not used. */
17152 addend += 0x8000;
65f38f15
AM
17153 break;
17154
5663e321
AM
17155 case R_PPC64_D34_HA30:
17156 case R_PPC64_ADDR16_HIGHERA34:
17157 case R_PPC64_ADDR16_HIGHESTA34:
17158 case R_PPC64_REL16_HIGHERA34:
17159 case R_PPC64_REL16_HIGHESTA34:
17160 if (sec != NULL)
17161 addend += 1ULL << 33;
17162 break;
17163
65f38f15
AM
17164 case R_PPC64_ADDR16_DS:
17165 case R_PPC64_ADDR16_LO_DS:
17166 case R_PPC64_GOT16_DS:
17167 case R_PPC64_GOT16_LO_DS:
17168 case R_PPC64_PLT16_LO_DS:
17169 case R_PPC64_SECTOFF_DS:
17170 case R_PPC64_SECTOFF_LO_DS:
17171 case R_PPC64_TOC16_DS:
17172 case R_PPC64_TOC16_LO_DS:
17173 case R_PPC64_PLTGOT16_DS:
17174 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
17175 case R_PPC64_GOT_TPREL16_DS:
17176 case R_PPC64_GOT_TPREL16_LO_DS:
17177 case R_PPC64_GOT_DTPREL16_DS:
17178 case R_PPC64_GOT_DTPREL16_LO_DS:
17179 case R_PPC64_TPREL16_DS:
17180 case R_PPC64_TPREL16_LO_DS:
17181 case R_PPC64_DTPREL16_DS:
17182 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
17183 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17184 mask = 3;
a680de9a
PB
17185 /* If this reloc is against an lq, lxv, or stxv insn, then
17186 the value must be a multiple of 16. This is somewhat of
17187 a hack, but the "correct" way to do this by defining _DQ
17188 forms of all the _DS relocs bloats all reloc switches in
17189 this file. It doesn't make much sense to use these
17190 relocs in data, so testing the insn should be safe. */
2365f8d7
AM
17191 if ((insn & (0x3fu << 26)) == (56u << 26)
17192 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
adadcc0c 17193 mask = 15;
a680de9a
PB
17194 relocation += addend;
17195 addend = insn & (mask ^ 3);
17196 if ((relocation & mask) != 0)
65f38f15 17197 {
a680de9a 17198 relocation ^= relocation & mask;
25f53a85 17199 info->callbacks->einfo
695344c0 17200 /* xgettext:c-format */
174d0a74 17201 (_("%H: error: %s not a multiple of %u\n"),
25f53a85 17202 input_bfd, input_section, rel->r_offset,
d830549d 17203 ppc64_elf_howto_table[r_type]->name,
adadcc0c 17204 mask + 1);
65f38f15 17205 bfd_set_error (bfd_error_bad_value);
b34976b6 17206 ret = FALSE;
c316a17c 17207 goto copy_reloc;
65f38f15
AM
17208 }
17209 break;
5bd4f169
AM
17210 }
17211
239e1f3a
AM
17212 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17213 because such sections are not SEC_ALLOC and thus ld.so will
17214 not process them. */
d830549d 17215 howto = ppc64_elf_howto_table[(int) r_type];
65f38f15 17216 if (unresolved_reloc
239e1f3a 17217 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
17218 && h->elf.def_dynamic)
17219 && _bfd_elf_section_offset (output_bfd, info, input_section,
17220 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 17221 {
25f53a85 17222 info->callbacks->einfo
695344c0 17223 /* xgettext:c-format */
c1c8c1ef 17224 (_("%H: unresolvable %s against `%pT'\n"),
25f53a85 17225 input_bfd, input_section, rel->r_offset,
b80eed39 17226 howto->name,
039b3fef 17227 h->elf.root.root.string);
b34976b6 17228 ret = FALSE;
9c07fe7c 17229 }
5bd4f169 17230
b80eed39
AM
17231 /* 16-bit fields in insns mostly have signed values, but a
17232 few insns have 16-bit unsigned values. Really, we should
17233 have different reloc types. */
17234 if (howto->complain_on_overflow != complain_overflow_dont
17235 && howto->dst_mask == 0xffff
17236 && (input_section->flags & SEC_CODE) != 0)
17237 {
17238 enum complain_overflow complain = complain_overflow_signed;
17239
17240 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
2365f8d7 17241 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
a47622ac
AM
17242 complain = complain_overflow_bitfield;
17243 else if (howto->rightshift == 0
2365f8d7
AM
17244 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17245 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17246 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17247 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17248 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17249 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
b80eed39
AM
17250 complain = complain_overflow_unsigned;
17251 if (howto->complain_on_overflow != complain)
17252 {
17253 alt_howto = *howto;
17254 alt_howto.complain_on_overflow = complain;
17255 howto = &alt_howto;
17256 }
17257 }
17258
5663e321 17259 switch (r_type)
a680de9a 17260 {
5663e321
AM
17261 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17262 case R_PPC64_D34:
17263 case R_PPC64_D34_LO:
17264 case R_PPC64_D34_HI30:
17265 case R_PPC64_D34_HA30:
17266 case R_PPC64_PCREL34:
17267 case R_PPC64_GOT_PCREL34:
c213164a
AM
17268 case R_PPC64_TPREL34:
17269 case R_PPC64_DTPREL34:
87c69f97
AM
17270 case R_PPC64_GOT_TLSGD_PCREL34:
17271 case R_PPC64_GOT_TLSLD_PCREL34:
17272 case R_PPC64_GOT_TPREL_PCREL34:
17273 case R_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
17274 case R_PPC64_PLT_PCREL34:
17275 case R_PPC64_PLT_PCREL34_NOTOC:
17276 case R_PPC64_D28:
17277 case R_PPC64_PCREL28:
17278 if (rel->r_offset + 8 > input_section->size)
17279 r = bfd_reloc_outofrange;
17280 else
17281 {
5663e321
AM
17282 relocation += addend;
17283 if (howto->pc_relative)
17284 relocation -= (rel->r_offset
17285 + input_section->output_offset
17286 + input_section->output_section->vma);
17287 relocation >>= howto->rightshift;
17288
17289 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17290 pinsn <<= 32;
17291 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17292
17293 pinsn &= ~howto->dst_mask;
17294 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17295 & howto->dst_mask);
17296 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17297 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17298 r = bfd_reloc_ok;
17299 if (howto->complain_on_overflow == complain_overflow_signed
17300 && (relocation + (1ULL << (howto->bitsize - 1))
17301 >= 1ULL << howto->bitsize))
17302 r = bfd_reloc_overflow;
17303 }
17304 break;
17305
17306 case R_PPC64_REL16DX_HA:
a680de9a
PB
17307 if (rel->r_offset + 4 > input_section->size)
17308 r = bfd_reloc_outofrange;
17309 else
17310 {
17311 relocation += addend;
17312 relocation -= (rel->r_offset
17313 + input_section->output_offset
17314 + input_section->output_section->vma);
3de43e7b 17315 relocation = (bfd_signed_vma) relocation >> 16;
a680de9a
PB
17316 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17317 insn &= ~0x1fffc1;
3de43e7b 17318 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
a680de9a
PB
17319 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17320 r = bfd_reloc_ok;
3de43e7b 17321 if (relocation + 0x8000 > 0xffff)
a680de9a
PB
17322 r = bfd_reloc_overflow;
17323 }
5663e321
AM
17324 break;
17325
17326 default:
17327 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17328 contents, rel->r_offset,
17329 relocation, addend);
a680de9a 17330 }
5bd4f169 17331
ef60b7ff 17332 if (r != bfd_reloc_ok)
5bd4f169 17333 {
bc30df16 17334 char *more_info = NULL;
b80eed39 17335 const char *reloc_name = howto->name;
bc30df16
AM
17336
17337 if (reloc_dest != DEST_NORMAL)
17338 {
17339 more_info = bfd_malloc (strlen (reloc_name) + 8);
17340 if (more_info != NULL)
17341 {
17342 strcpy (more_info, reloc_name);
17343 strcat (more_info, (reloc_dest == DEST_OPD
17344 ? " (OPD)" : " (stub)"));
17345 reloc_name = more_info;
17346 }
17347 }
17348
cd27b276 17349 if (r == bfd_reloc_overflow)
5bd4f169 17350 {
8131c122
AM
17351 /* On code like "if (foo) foo();" don't report overflow
17352 on a branch to zero when foo is undefined. */
17353 if (!warned
17354 && (reloc_dest == DEST_STUB
17355 || !(h != NULL
17356 && (h->elf.root.type == bfd_link_hash_undefweak
17357 || h->elf.root.type == bfd_link_hash_undefined)
17358 && is_branch_reloc (r_type))))
9e51d549
AM
17359 info->callbacks->reloc_overflow
17360 (info, (struct bfd_link_hash_entry *) h, sym_name,
17361 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17362 rel->r_offset);
ef60b7ff
AM
17363 }
17364 else
17365 {
25f53a85 17366 info->callbacks->einfo
695344c0 17367 /* xgettext:c-format */
c1c8c1ef 17368 (_("%H: %s against `%pT': error %d\n"),
25f53a85 17369 input_bfd, input_section, rel->r_offset,
bc30df16 17370 reloc_name, sym_name, (int) r);
b34976b6 17371 ret = FALSE;
ef60b7ff 17372 }
c9594989 17373 free (more_info);
5bd4f169 17374 }
c316a17c
AM
17375 copy_reloc:
17376 if (wrel != rel)
17377 *wrel = *rel;
17378 }
17379
17380 if (wrel != rel)
17381 {
17382 Elf_Internal_Shdr *rel_hdr;
17383 size_t deleted = rel - wrel;
17384
17385 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17386 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17387 if (rel_hdr->sh_size == 0)
17388 {
17389 /* It is too late to remove an empty reloc section. Leave
17390 one NONE reloc.
17391 ??? What is wrong with an empty section??? */
17392 rel_hdr->sh_size = rel_hdr->sh_entsize;
17393 deleted -= 1;
17394 }
17395 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17396 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17397 input_section->reloc_count -= deleted;
5bd4f169
AM
17398 }
17399
645ea6a9
AM
17400 /* If we're emitting relocations, then shortly after this function
17401 returns, reloc offsets and addends for this section will be
17402 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
17403 file rather than the input. Save a copy of the relocs for
17404 opd_entry_value. */
0e1862bb 17405 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
8860955f
AM
17406 {
17407 bfd_size_type amt;
17408 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17409 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
17410 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17411 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
17412 if (rel == NULL)
17413 return FALSE;
17414 memcpy (rel, relocs, amt);
17415 }
5bd4f169
AM
17416 return ret;
17417}
17418
754021d0
AM
17419/* Adjust the value of any local symbols in opd sections. */
17420
6e0b88f1 17421static int
754021d0
AM
17422ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17423 const char *name ATTRIBUTE_UNUSED,
17424 Elf_Internal_Sym *elfsym,
17425 asection *input_sec,
17426 struct elf_link_hash_entry *h)
17427{
74f0fb50
AM
17428 struct _opd_sec_data *opd;
17429 long adjust;
754021d0
AM
17430 bfd_vma value;
17431
4025353c 17432 if (h != NULL)
6e0b88f1 17433 return 1;
4025353c 17434
74f0fb50
AM
17435 opd = get_opd_info (input_sec);
17436 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 17437 return 1;
754021d0
AM
17438
17439 value = elfsym->st_value - input_sec->output_offset;
0e1862bb 17440 if (!bfd_link_relocatable (info))
754021d0
AM
17441 value -= input_sec->output_section->vma;
17442
51aecdc5 17443 adjust = opd->adjust[OPD_NDX (value)];
4025353c 17444 if (adjust == -1)
6e0b88f1
AM
17445 return 2;
17446
17447 elfsym->st_value += adjust;
17448 return 1;
754021d0
AM
17449}
17450
5bd4f169
AM
17451/* Finish up dynamic symbol handling. We set the contents of various
17452 dynamic sections here. */
17453
b34976b6 17454static bfd_boolean
4ce794b7
AM
17455ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17456 struct bfd_link_info *info,
17457 struct elf_link_hash_entry *h,
4aef7643 17458 Elf_Internal_Sym *sym)
5bd4f169 17459{
65f38f15 17460 struct ppc_link_hash_table *htab;
8387904d 17461 struct plt_entry *ent;
5bd4f169 17462
65f38f15 17463 htab = ppc_hash_table (info);
4dfe6ac6
NC
17464 if (htab == NULL)
17465 return FALSE;
5bd4f169 17466
49c09209
AM
17467 if (!htab->opd_abi && !h->def_regular)
17468 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17469 if (ent->plt.offset != (bfd_vma) -1)
17470 {
17471 /* Mark the symbol as undefined, rather than as
17472 defined in glink. Leave the value if there were
17473 any relocations where pointer equality matters
17474 (this is a clue for the dynamic linker, to make
17475 function pointer comparisons work between an
17476 application and shared library), otherwise set it
17477 to zero. */
17478 sym->st_shndx = SHN_UNDEF;
17479 if (!h->pointer_equality_needed)
17480 sym->st_value = 0;
17481 else if (!h->ref_regular_nonweak)
17482 {
17483 /* This breaks function pointer comparisons, but
17484 that is better than breaking tests for a NULL
17485 function pointer. */
17486 sym->st_value = 0;
17487 }
17488 break;
17489 }
5bd4f169 17490
1bdd8fac
AM
17491 if (h->needs_copy
17492 && (h->root.type == bfd_link_hash_defined
17493 || h->root.type == bfd_link_hash_defweak)
17494 && (h->root.u.def.section == htab->elf.sdynbss
17495 || h->root.u.def.section == htab->elf.sdynrelro))
5bd4f169 17496 {
65f38f15 17497 /* This symbol needs a copy reloc. Set it up. */
49c09209 17498 Elf_Internal_Rela rela;
5474d94f 17499 asection *srel;
49c09209 17500 bfd_byte *loc;
5bd4f169 17501
1bdd8fac 17502 if (h->dynindx == -1)
65f38f15 17503 abort ();
5bd4f169 17504
ed7007c1 17505 rela.r_offset = defined_sym_val (h);
5bd4f169
AM
17506 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17507 rela.r_addend = 0;
afbf7e8e 17508 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
17509 srel = htab->elf.sreldynrelro;
17510 else
17511 srel = htab->elf.srelbss;
17512 loc = srel->contents;
17513 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 17514 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
17515 }
17516
b34976b6 17517 return TRUE;
5bd4f169
AM
17518}
17519
65f38f15
AM
17520/* Used to decide how to sort relocs in an optimal manner for the
17521 dynamic linker, before writing them out. */
17522
17523static enum elf_reloc_type_class
7e612e98
AM
17524ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17525 const asection *rel_sec,
17526 const Elf_Internal_Rela *rela)
65f38f15 17527{
04c9666a 17528 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
17529 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17530
33e44f2e 17531 if (rel_sec == htab->elf.irelplt)
7e612e98 17532 return reloc_class_ifunc;
a33d1f77 17533
4ce794b7 17534 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 17535 switch (r_type)
65f38f15
AM
17536 {
17537 case R_PPC64_RELATIVE:
17538 return reloc_class_relative;
17539 case R_PPC64_JMP_SLOT:
17540 return reloc_class_plt;
17541 case R_PPC64_COPY:
17542 return reloc_class_copy;
17543 default:
17544 return reloc_class_normal;
17545 }
17546}
17547
5bd4f169
AM
17548/* Finish up the dynamic sections. */
17549
b34976b6 17550static bfd_boolean
4ce794b7
AM
17551ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17552 struct bfd_link_info *info)
5bd4f169 17553{
65f38f15
AM
17554 struct ppc_link_hash_table *htab;
17555 bfd *dynobj;
5bd4f169 17556 asection *sdyn;
5bd4f169 17557
65f38f15 17558 htab = ppc_hash_table (info);
4dfe6ac6
NC
17559 if (htab == NULL)
17560 return FALSE;
17561
65f38f15 17562 dynobj = htab->elf.dynobj;
3d4d4302 17563 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 17564
65f38f15 17565 if (htab->elf.dynamic_sections_created)
5bd4f169 17566 {
5bd4f169
AM
17567 Elf64_External_Dyn *dyncon, *dynconend;
17568
33e44f2e 17569 if (sdyn == NULL || htab->elf.sgot == NULL)
65f38f15 17570 abort ();
5bd4f169
AM
17571
17572 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 17573 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
17574 for (; dyncon < dynconend; dyncon++)
17575 {
17576 Elf_Internal_Dyn dyn;
19397422 17577 asection *s;
5bd4f169
AM
17578
17579 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17580
17581 switch (dyn.d_tag)
17582 {
65f38f15
AM
17583 default:
17584 continue;
5bd4f169 17585
5d1634d7 17586 case DT_PPC64_GLINK:
4ce794b7 17587 s = htab->glink;
6348e046 17588 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
17589 /* We stupidly defined DT_PPC64_GLINK to be the start
17590 of glink rather than the first entry point, which is
17591 what ld.so needs, and now have a bigger stub to
17592 support automatic multiple TOCs. */
9e390558 17593 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
5d1634d7
AM
17594 break;
17595
19397422
AM
17596 case DT_PPC64_OPD:
17597 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17598 if (s == NULL)
17599 continue;
17600 dyn.d_un.d_ptr = s->vma;
19397422
AM
17601 break;
17602
e8910a83 17603 case DT_PPC64_OPT:
5663e321
AM
17604 if ((htab->do_multi_toc && htab->multi_toc_needed)
17605 || htab->notoc_plt)
e8910a83 17606 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
f378ab09
AM
17607 if (htab->has_plt_localentry0)
17608 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
e8910a83
AM
17609 break;
17610
19397422
AM
17611 case DT_PPC64_OPDSZ:
17612 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
17613 if (s == NULL)
17614 continue;
eea6121a 17615 dyn.d_un.d_val = s->size;
19397422
AM
17616 break;
17617
65f38f15 17618 case DT_PLTGOT:
33e44f2e 17619 s = htab->elf.splt;
6348e046 17620 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
17621 break;
17622
17623 case DT_JMPREL:
33e44f2e 17624 s = htab->elf.srelplt;
6348e046 17625 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 17626 break;
5bd4f169 17627
65f38f15 17628 case DT_PLTRELSZ:
33e44f2e 17629 dyn.d_un.d_val = htab->elf.srelplt->size;
5d1634d7 17630 break;
82e66161
AM
17631
17632 case DT_TEXTREL:
cebd6b8a 17633 if (htab->elf.ifunc_resolvers)
82e66161
AM
17634 info->callbacks->einfo
17635 (_("%P: warning: text relocations and GNU indirect "
17636 "functions may result in a segfault at runtime\n"));
17637 continue;
5bd4f169 17638 }
5bd4f169 17639
65f38f15 17640 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 17641 }
5bd4f169
AM
17642 }
17643
6528b6eb
AM
17644 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17645 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17646 {
17647 /* Fill in the first entry in the global offset table.
17648 We use it to hold the link-time TOCbase. */
17649 bfd_put_64 (output_bfd,
60ee0d4a 17650 elf_gp (output_bfd) + TOC_BASE_OFF,
33e44f2e 17651 htab->elf.sgot->contents);
5d1634d7
AM
17652
17653 /* Set .got entry size. */
2cdcc330
AM
17654 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17655 = 8;
5d1634d7
AM
17656 }
17657
6528b6eb
AM
17658 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17659 && htab->elf.splt->output_section != bfd_abs_section_ptr)
5d1634d7
AM
17660 {
17661 /* Set .plt entry size. */
33e44f2e 17662 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
b9e5796b 17663 = PLT_ENTRY_SIZE (htab);
5d1634d7
AM
17664 }
17665
84f5d08e
AM
17666 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17667 brlt ourselves if emitrelocations. */
17668 if (htab->brlt != NULL
17669 && htab->brlt->reloc_count != 0
17670 && !_bfd_elf_link_output_relocs (output_bfd,
17671 htab->brlt,
d4730f92 17672 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
17673 elf_section_data (htab->brlt)->relocs,
17674 NULL))
17675 return FALSE;
17676
176a0d42
AM
17677 if (htab->glink != NULL
17678 && htab->glink->reloc_count != 0
17679 && !_bfd_elf_link_output_relocs (output_bfd,
17680 htab->glink,
d4730f92 17681 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
17682 elf_section_data (htab->glink)->relocs,
17683 NULL))
17684 return FALSE;
17685
da44f4e5 17686
df136d64
AM
17687 if (htab->glink_eh_frame != NULL
17688 && htab->glink_eh_frame->size != 0
17689 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17690 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17691 htab->glink_eh_frame,
17692 htab->glink_eh_frame->contents))
17693 return FALSE;
58d180e8 17694
e717da7e 17695 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
17696 since we didn't add them to DYNOBJ. We know dynobj is the first
17697 bfd. */
c72f2fb2 17698 while ((dynobj = dynobj->link.next) != NULL)
e717da7e
AM
17699 {
17700 asection *s;
7b53ace3 17701
0c8d6e5c 17702 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
17703 continue;
17704
e717da7e
AM
17705 s = ppc64_elf_tdata (dynobj)->got;
17706 if (s != NULL
eea6121a 17707 && s->size != 0
e717da7e
AM
17708 && s->output_section != bfd_abs_section_ptr
17709 && !bfd_set_section_contents (output_bfd, s->output_section,
17710 s->contents, s->output_offset,
eea6121a 17711 s->size))
e717da7e
AM
17712 return FALSE;
17713 s = ppc64_elf_tdata (dynobj)->relgot;
17714 if (s != NULL
eea6121a 17715 && s->size != 0
e717da7e
AM
17716 && s->output_section != bfd_abs_section_ptr
17717 && !bfd_set_section_contents (output_bfd, s->output_section,
17718 s->contents, s->output_offset,
eea6121a 17719 s->size))
e717da7e
AM
17720 return FALSE;
17721 }
f6c52c13 17722
b34976b6 17723 return TRUE;
5bd4f169
AM
17724}
17725
5bd4f169 17726#include "elf64-target.h"
7b8e7dad
AM
17727
17728/* FreeBSD support */
17729
17730#undef TARGET_LITTLE_SYM
6afcdeb3 17731#define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
7b8e7dad 17732#undef TARGET_LITTLE_NAME
6afcdeb3 17733#define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
7b8e7dad
AM
17734
17735#undef TARGET_BIG_SYM
6d00b590 17736#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
7b8e7dad
AM
17737#undef TARGET_BIG_NAME
17738#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17739
17740#undef ELF_OSABI
17741#define ELF_OSABI ELFOSABI_FREEBSD
17742
17743#undef elf64_bed
17744#define elf64_bed elf64_powerpc_fbsd_bed
17745
17746#include "elf64-target.h"